2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/param.h>
40 #include <sys/linker.h>
41 #include <sys/socket.h>
42 #include <sys/capsicum.h>
59 static volatile bool sigalrm_received = false;
61 static int nchildren = 0;
67 fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
72 checked_strdup(const char *s)
83 resolve_addr(const struct connection *conn, const char *address,
84 struct addrinfo **ai, bool initiator_side)
86 struct addrinfo hints;
87 char *arg, *addr, *ch;
89 int error, colons = 0;
91 arg = checked_strdup(address);
94 fail(conn, "empty address");
95 log_errx(1, "empty address");
99 * IPv6 address in square brackets, perhaps with port.
102 addr = strsep(&arg, "]");
104 fail(conn, "malformed address");
105 log_errx(1, "malformed address %s", address);
107 if (arg[0] == '\0') {
109 } else if (arg[0] == ':') {
112 fail(conn, "malformed address");
113 log_errx(1, "malformed address %s", address);
117 * Either IPv6 address without brackets - and without
118 * a port - or IPv4 address. Just count the colons.
120 for (ch = arg; *ch != '\0'; ch++) {
128 addr = strsep(&arg, ":");
136 if (port == NULL && !initiator_side)
139 memset(&hints, 0, sizeof(hints));
140 hints.ai_family = PF_UNSPEC;
141 hints.ai_socktype = SOCK_STREAM;
142 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
144 hints.ai_flags |= AI_PASSIVE;
146 error = getaddrinfo(addr, port, &hints, ai);
148 fail(conn, gai_strerror(error));
149 log_errx(1, "getaddrinfo for %s failed: %s",
150 address, gai_strerror(error));
154 static struct connection *
155 connection_new(int iscsi_fd, const struct iscsi_daemon_request *request)
157 struct connection *conn;
158 struct addrinfo *from_ai, *to_ai;
159 const char *from_addr, *to_addr;
160 #ifdef ICL_KERNEL_PROXY
161 struct iscsi_daemon_connect idc;
165 conn = calloc(1, sizeof(*conn));
167 log_err(1, "calloc");
170 * Default values, from RFC 3720, section 12.
172 conn->conn_header_digest = CONN_DIGEST_NONE;
173 conn->conn_data_digest = CONN_DIGEST_NONE;
174 conn->conn_initial_r2t = true;
175 conn->conn_immediate_data = true;
176 conn->conn_max_data_segment_length = 8192;
177 conn->conn_max_burst_length = 262144;
178 conn->conn_first_burst_length = 65536;
179 conn->conn_iscsi_fd = iscsi_fd;
181 conn->conn_session_id = request->idr_session_id;
182 memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf));
183 memcpy(&conn->conn_isid, &request->idr_isid, sizeof(conn->conn_isid));
184 conn->conn_tsih = request->idr_tsih;
185 memcpy(&conn->conn_limits, &request->idr_limits, sizeof(conn->conn_limits));
187 from_addr = conn->conn_conf.isc_initiator_addr;
188 to_addr = conn->conn_conf.isc_target_addr;
190 if (from_addr[0] != '\0')
191 resolve_addr(conn, from_addr, &from_ai, true);
195 resolve_addr(conn, to_addr, &to_ai, false);
197 #ifdef ICL_KERNEL_PROXY
198 if (conn->conn_conf.isc_iser) {
199 memset(&idc, 0, sizeof(idc));
200 idc.idc_session_id = conn->conn_session_id;
201 if (conn->conn_conf.isc_iser)
203 idc.idc_domain = to_ai->ai_family;
204 idc.idc_socktype = to_ai->ai_socktype;
205 idc.idc_protocol = to_ai->ai_protocol;
206 if (from_ai != NULL) {
207 idc.idc_from_addr = from_ai->ai_addr;
208 idc.idc_from_addrlen = from_ai->ai_addrlen;
210 idc.idc_to_addr = to_ai->ai_addr;
211 idc.idc_to_addrlen = to_ai->ai_addrlen;
213 log_debugx("connecting to %s using ICL kernel proxy", to_addr);
214 error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
216 fail(conn, strerror(errno));
217 log_err(1, "failed to connect to %s "
218 "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
223 #endif /* ICL_KERNEL_PROXY */
225 if (conn->conn_conf.isc_iser) {
226 fail(conn, "iSER not supported");
227 log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
228 "does not support iSER");
231 conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
233 if (conn->conn_socket < 0) {
234 fail(conn, strerror(errno));
235 log_err(1, "failed to create socket for %s", from_addr);
237 sockbuf = SOCKBUF_SIZE;
238 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF,
239 &sockbuf, sizeof(sockbuf)) == -1)
240 log_warn("setsockopt(SO_RCVBUF) failed");
241 sockbuf = SOCKBUF_SIZE;
242 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF,
243 &sockbuf, sizeof(sockbuf)) == -1)
244 log_warn("setsockopt(SO_SNDBUF) failed");
245 if (from_ai != NULL) {
246 error = bind(conn->conn_socket, from_ai->ai_addr,
247 from_ai->ai_addrlen);
249 fail(conn, strerror(errno));
250 log_err(1, "failed to bind to %s", from_addr);
253 log_debugx("connecting to %s", to_addr);
254 error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen);
256 fail(conn, strerror(errno));
257 log_err(1, "failed to connect to %s", to_addr);
264 handoff(struct connection *conn)
266 struct iscsi_daemon_handoff idh;
269 log_debugx("handing off connection to the kernel");
271 memset(&idh, 0, sizeof(idh));
272 idh.idh_session_id = conn->conn_session_id;
273 idh.idh_socket = conn->conn_socket;
274 strlcpy(idh.idh_target_alias, conn->conn_target_alias,
275 sizeof(idh.idh_target_alias));
276 idh.idh_tsih = conn->conn_tsih;
277 idh.idh_statsn = conn->conn_statsn;
278 idh.idh_header_digest = conn->conn_header_digest;
279 idh.idh_data_digest = conn->conn_data_digest;
280 idh.idh_initial_r2t = conn->conn_initial_r2t;
281 idh.idh_immediate_data = conn->conn_immediate_data;
282 idh.idh_max_data_segment_length = conn->conn_max_data_segment_length;
283 idh.idh_max_burst_length = conn->conn_max_burst_length;
284 idh.idh_first_burst_length = conn->conn_first_burst_length;
286 error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
288 log_err(1, "ISCSIDHANDOFF");
292 fail(const struct connection *conn, const char *reason)
294 struct iscsi_daemon_fail idf;
295 int error, saved_errno;
299 memset(&idf, 0, sizeof(idf));
300 idf.idf_session_id = conn->conn_session_id;
301 strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
303 error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
305 log_err(1, "ISCSIDFAIL");
311 * XXX: I CANT INTO LATIN
314 capsicate(struct connection *conn)
318 #ifdef ICL_KERNEL_PROXY
319 const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE,
320 ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY };
322 const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD,
323 ISCSISREMOVE, ISCSISMODIFY };
326 cap_rights_init(&rights, CAP_IOCTL);
327 error = cap_rights_limit(conn->conn_iscsi_fd, &rights);
328 if (error != 0 && errno != ENOSYS)
329 log_err(1, "cap_rights_limit");
331 error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, nitems(cmds));
333 if (error != 0 && errno != ENOSYS)
334 log_err(1, "cap_ioctls_limit");
337 if (error != 0 && errno != ENOSYS)
338 log_err(1, "cap_enter");
341 log_debugx("Capsicum capability mode enabled");
343 log_warnx("Capsicum capability mode not supported");
350 return (sigalrm_received);
354 sigalrm_handler(int dummy __unused)
357 * It would be easiest to just log an error and exit. We can't
358 * do this, though, because log_errx() is not signal safe, since
359 * it calls syslog(3). Instead, set a flag checked by pdu_send()
360 * and pdu_receive(), to call log_errx() there. Should they fail
361 * to notice, we'll exit here one second later.
363 if (sigalrm_received) {
365 * Oh well. Just give up and quit.
370 sigalrm_received = true;
374 set_timeout(int timeout)
377 struct itimerval itv;
381 log_debugx("session timeout disabled");
385 bzero(&sa, sizeof(sa));
386 sa.sa_handler = sigalrm_handler;
387 sigfillset(&sa.sa_mask);
388 error = sigaction(SIGALRM, &sa, NULL);
390 log_err(1, "sigaction");
393 * First SIGALRM will arive after conf_timeout seconds.
394 * If we do nothing, another one will arrive a second later.
396 bzero(&itv, sizeof(itv));
397 itv.it_interval.tv_sec = 1;
398 itv.it_value.tv_sec = timeout;
400 log_debugx("setting session timeout to %d seconds",
402 error = setitimer(ITIMER_REAL, &itv, NULL);
404 log_err(1, "setitimer");
408 sigchld_handler(int dummy __unused)
412 * The only purpose of this handler is to make SIGCHLD
413 * interrupt the ISCSIDWAIT ioctl(2), so we can call
414 * wait_for_children().
419 register_sigchld(void)
424 bzero(&sa, sizeof(sa));
425 sa.sa_handler = sigchld_handler;
426 sigfillset(&sa.sa_mask);
427 error = sigaction(SIGCHLD, &sa, NULL);
429 log_err(1, "sigaction");
434 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
436 struct connection *conn;
438 log_set_peer_addr(request->idr_conf.isc_target_addr);
439 if (request->idr_conf.isc_target[0] != '\0') {
440 log_set_peer_name(request->idr_conf.isc_target);
441 setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
443 setproctitle("%s", request->idr_conf.isc_target_addr);
446 conn = connection_new(iscsi_fd, request);
447 set_timeout(timeout);
450 if (conn->conn_conf.isc_discovery != 0)
455 log_debugx("nothing more to do; exiting");
460 wait_for_children(bool block)
468 * If "block" is true, wait for at least one process.
470 if (block && num == 0)
471 pid = wait4(-1, &status, 0, NULL);
473 pid = wait4(-1, &status, WNOHANG, NULL);
476 if (WIFSIGNALED(status)) {
477 log_warnx("child process %d terminated with signal %d",
478 pid, WTERMSIG(status));
479 } else if (WEXITSTATUS(status) != 0) {
480 log_warnx("child process %d terminated with exit status %d",
481 pid, WEXITSTATUS(status));
483 log_debugx("child process %d terminated gracefully", pid);
492 main(int argc, char **argv)
494 int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
496 bool dont_daemonize = false;
499 const char *pidfile_path = DEFAULT_PIDFILE;
500 struct iscsi_daemon_request request;
502 while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
505 pidfile_path = optarg;
508 dont_daemonize = true;
512 debug = atoi(optarg);
515 maxproc = atoi(optarg);
518 timeout = atoi(optarg);
531 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
534 log_errx(1, "daemon already running, pid: %jd.",
536 log_err(1, "cannot open or create pidfile \"%s\"",
540 iscsi_fd = open(ISCSI_PATH, O_RDWR);
541 if (iscsi_fd < 0 && errno == ENOENT) {
543 retval = kldload("iscsi");
545 iscsi_fd = open(ISCSI_PATH, O_RDWR);
550 log_err(1, "failed to open %s", ISCSI_PATH);
552 if (dont_daemonize == false) {
553 if (daemon(0, 0) == -1) {
554 log_warn("cannot daemonize");
555 pidfile_remove(pidfh);
560 pidfile_write(pidfh);
565 log_debugx("waiting for request from the kernel");
567 memset(&request, 0, sizeof(request));
568 error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
570 if (errno == EINTR) {
571 nchildren -= wait_for_children(false);
572 assert(nchildren >= 0);
576 log_err(1, "ISCSIDWAIT");
579 if (dont_daemonize) {
580 log_debugx("not forking due to -d flag; "
581 "will exit after servicing a single request");
583 nchildren -= wait_for_children(false);
584 assert(nchildren >= 0);
586 while (maxproc > 0 && nchildren >= maxproc) {
587 log_debugx("maxproc limit of %d child processes hit; "
588 "waiting for child process to exit", maxproc);
589 nchildren -= wait_for_children(true);
590 assert(nchildren >= 0);
592 log_debugx("incoming connection; forking child process #%d",
603 pidfile_close(pidfh);
604 handle_request(iscsi_fd, &request, timeout);