2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/param.h>
36 #include <sys/linker.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 #include <sys/capsicum.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
44 #include <capsicum_helpers.h>
59 static bool timed_out(void);
60 #ifdef ICL_KERNEL_PROXY
61 static void pdu_receive_proxy(struct pdu *pdu);
62 static void pdu_send_proxy(struct pdu *pdu);
63 #endif /* ICL_KERNEL_PROXY */
65 static volatile bool sigalrm_received = false;
67 static int nchildren = 0;
69 static struct connection_ops conn_ops = {
70 .timed_out = timed_out,
71 #ifdef ICL_KERNEL_PROXY
72 .pdu_receive_proxy = pdu_receive_proxy,
73 .pdu_send_proxy = pdu_send_proxy,
82 fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
86 #ifdef ICL_KERNEL_PROXY
89 pdu_receive_proxy(struct pdu *pdu)
91 struct iscsid_connection *conn;
92 struct iscsi_daemon_receive idr;
96 conn = (struct iscsid_connection *)pdu->pdu_connection;
97 assert(conn->conn_conf.isc_iser != 0);
99 pdu->pdu_data = malloc(conn->conn.conn_max_recv_data_segment_length);
100 if (pdu->pdu_data == NULL)
101 log_err(1, "malloc");
103 memset(&idr, 0, sizeof(idr));
104 idr.idr_session_id = conn->conn_session_id;
105 idr.idr_bhs = pdu->pdu_bhs;
106 idr.idr_data_segment_len = conn->conn.conn_max_recv_data_segment_length;
107 idr.idr_data_segment = pdu->pdu_data;
109 error = ioctl(conn->conn_iscsi_fd, ISCSIDRECEIVE, &idr);
111 log_err(1, "ISCSIDRECEIVE");
113 len = pdu_ahs_length(pdu);
115 log_errx(1, "protocol error: non-empty AHS");
117 len = pdu_data_segment_length(pdu);
118 assert(len <= (size_t)conn->conn.conn_max_recv_data_segment_length);
119 pdu->pdu_data_len = len;
123 pdu_send_proxy(struct pdu *pdu)
125 struct iscsid_connection *conn;
126 struct iscsi_daemon_send ids;
129 conn = (struct iscsid_connection *)pdu->pdu_connection;
130 assert(conn->conn_conf.isc_iser != 0);
132 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
134 memset(&ids, 0, sizeof(ids));
135 ids.ids_session_id = conn->conn_session_id;
136 ids.ids_bhs = pdu->pdu_bhs;
137 ids.ids_data_segment_len = pdu->pdu_data_len;
138 ids.ids_data_segment = pdu->pdu_data;
140 error = ioctl(conn->conn_iscsi_fd, ISCSIDSEND, &ids);
142 log_err(1, "ISCSIDSEND");
145 #endif /* ICL_KERNEL_PROXY */
148 resolve_addr(const struct connection *conn, const char *address,
149 struct addrinfo **ai, bool initiator_side)
151 struct addrinfo hints;
152 char *arg, *addr, *ch, *tofree;
154 int error, colons = 0;
156 tofree = arg = checked_strdup(address);
158 if (arg[0] == '\0') {
159 fail(conn, "empty address");
160 log_errx(1, "empty address");
164 * IPv6 address in square brackets, perhaps with port.
167 addr = strsep(&arg, "]");
169 fail(conn, "malformed address");
170 log_errx(1, "malformed address %s", address);
172 if (arg[0] == '\0') {
174 } else if (arg[0] == ':') {
177 fail(conn, "malformed address");
178 log_errx(1, "malformed address %s", address);
182 * Either IPv6 address without brackets - and without
183 * a port - or IPv4 address. Just count the colons.
185 for (ch = arg; *ch != '\0'; ch++) {
193 addr = strsep(&arg, ":");
201 if (port == NULL && !initiator_side)
204 memset(&hints, 0, sizeof(hints));
205 hints.ai_family = PF_UNSPEC;
206 hints.ai_socktype = SOCK_STREAM;
207 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
209 hints.ai_flags |= AI_PASSIVE;
211 error = getaddrinfo(addr, port, &hints, ai);
213 fail(conn, gai_strerror(error));
214 log_errx(1, "getaddrinfo for %s failed: %s",
215 address, gai_strerror(error));
221 static struct iscsid_connection *
222 connection_new(int iscsi_fd, const struct iscsi_daemon_request *request)
224 struct iscsid_connection *conn;
225 struct addrinfo *from_ai, *to_ai;
226 const char *from_addr, *to_addr;
227 #ifdef ICL_KERNEL_PROXY
228 struct iscsi_daemon_connect idc;
232 conn = calloc(1, sizeof(*conn));
234 log_err(1, "calloc");
236 connection_init(&conn->conn, &conn_ops,
237 request->idr_conf.isc_iser != 0);
238 conn->conn_protocol_level = 0;
239 conn->conn_initial_r2t = true;
240 conn->conn_iscsi_fd = iscsi_fd;
242 conn->conn_session_id = request->idr_session_id;
243 memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf));
244 memcpy(&conn->conn.conn_isid, &request->idr_isid,
245 sizeof(conn->conn.conn_isid));
246 conn->conn.conn_tsih = request->idr_tsih;
248 from_addr = conn->conn_conf.isc_initiator_addr;
249 to_addr = conn->conn_conf.isc_target_addr;
251 if (from_addr[0] != '\0')
252 resolve_addr(&conn->conn, from_addr, &from_ai, true);
256 resolve_addr(&conn->conn, to_addr, &to_ai, false);
258 #ifdef ICL_KERNEL_PROXY
259 if (conn->conn_conf.isc_iser) {
260 memset(&idc, 0, sizeof(idc));
261 idc.idc_session_id = conn->conn_session_id;
262 if (conn->conn_conf.isc_iser)
264 idc.idc_domain = to_ai->ai_family;
265 idc.idc_socktype = to_ai->ai_socktype;
266 idc.idc_protocol = to_ai->ai_protocol;
267 if (from_ai != NULL) {
268 idc.idc_from_addr = from_ai->ai_addr;
269 idc.idc_from_addrlen = from_ai->ai_addrlen;
271 idc.idc_to_addr = to_ai->ai_addr;
272 idc.idc_to_addrlen = to_ai->ai_addrlen;
274 log_debugx("connecting to %s using ICL kernel proxy", to_addr);
275 error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
277 fail(&conn->conn, strerror(errno));
278 log_err(1, "failed to connect to %s "
279 "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
283 freeaddrinfo(from_ai);
288 #endif /* ICL_KERNEL_PROXY */
290 if (conn->conn_conf.isc_iser) {
291 fail(&conn->conn, "iSER not supported");
292 log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
293 "does not support iSER");
296 conn->conn.conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
298 if (conn->conn.conn_socket < 0) {
299 fail(&conn->conn, strerror(errno));
300 log_err(1, "failed to create socket for %s", from_addr);
302 optval = SOCKBUF_SIZE;
303 if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_RCVBUF,
304 &optval, sizeof(optval)) == -1)
305 log_warn("setsockopt(SO_RCVBUF) failed");
306 optval = SOCKBUF_SIZE;
307 if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_SNDBUF,
308 &optval, sizeof(optval)) == -1)
309 log_warn("setsockopt(SO_SNDBUF) failed");
311 if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_NO_DDP,
312 &optval, sizeof(optval)) == -1)
313 log_warn("setsockopt(SO_NO_DDP) failed");
314 if (conn->conn_conf.isc_dscp != -1) {
315 int tos = conn->conn_conf.isc_dscp << 2;
316 if (to_ai->ai_family == AF_INET) {
317 if (setsockopt(conn->conn.conn_socket,
319 &tos, sizeof(tos)) == -1)
320 log_warn("setsockopt(IP_TOS) "
324 if (to_ai->ai_family == AF_INET6) {
325 if (setsockopt(conn->conn.conn_socket,
326 IPPROTO_IPV6, IPV6_TCLASS,
327 &tos, sizeof(tos)) == -1)
328 log_warn("setsockopt(IPV6_TCLASS) "
333 if (conn->conn_conf.isc_pcp != -1) {
334 int pcp = conn->conn_conf.isc_pcp;
335 if (to_ai->ai_family == AF_INET) {
336 if (setsockopt(conn->conn.conn_socket,
337 IPPROTO_IP, IP_VLAN_PCP,
338 &pcp, sizeof(pcp)) == -1)
339 log_warn("setsockopt(IP_VLAN_PCP) "
343 if (to_ai->ai_family == AF_INET6) {
344 if (setsockopt(conn->conn.conn_socket,
345 IPPROTO_IPV6, IPV6_VLAN_PCP,
346 &pcp, sizeof(pcp)) == -1)
347 log_warn("setsockopt(IPV6_VLAN_PCP) "
353 * Reduce TCP SYN_SENT timeout while
354 * no connectivity exists, to allow
355 * rapid reuse of the available slots.
358 if (conn->conn_conf.isc_login_timeout > 0) {
359 keepinit = conn->conn_conf.isc_login_timeout;
360 log_debugx("session specific LoginTimeout at %d sec",
363 if (conn->conn_conf.isc_login_timeout == -1) {
365 size_t size = sizeof(value);
366 if (sysctlbyname("kern.iscsi.login_timeout",
367 &value, &size, NULL, 0) == 0) {
369 log_debugx("global login_timeout at %d sec",
374 if (setsockopt(conn->conn.conn_socket,
375 IPPROTO_TCP, TCP_KEEPINIT,
376 &keepinit, sizeof(keepinit)) == -1)
377 log_warnx("setsockopt(TCP_KEEPINIT) "
378 "failed for %s", to_addr);
380 if (from_ai != NULL) {
381 error = bind(conn->conn.conn_socket, from_ai->ai_addr,
382 from_ai->ai_addrlen);
384 fail(&conn->conn, strerror(errno));
385 log_err(1, "failed to bind to %s", from_addr);
388 log_debugx("connecting to %s", to_addr);
389 error = connect(conn->conn.conn_socket, to_ai->ai_addr,
392 fail(&conn->conn, strerror(errno));
393 log_err(1, "failed to connect to %s", to_addr);
397 freeaddrinfo(from_ai);
404 limits(struct iscsid_connection *conn)
406 struct iscsi_daemon_limits idl;
407 struct iscsi_session_limits *isl;
410 log_debugx("fetching limits from the kernel");
412 memset(&idl, 0, sizeof(idl));
413 idl.idl_session_id = conn->conn_session_id;
414 idl.idl_socket = conn->conn.conn_socket;
416 error = ioctl(conn->conn_iscsi_fd, ISCSIDLIMITS, &idl);
418 log_err(1, "ISCSIDLIMITS");
421 * Read the driver limits and provide reasonable defaults for the ones
422 * the driver doesn't care about. If a max_snd_dsl is not explicitly
423 * provided by the driver then we'll make sure both conn->max_snd_dsl
424 * and isl->max_snd_dsl are set to the rcv_dsl. This preserves historic
427 isl = &conn->conn_limits;
428 memcpy(isl, &idl.idl_limits, sizeof(*isl));
429 if (isl->isl_max_recv_data_segment_length == 0)
430 isl->isl_max_recv_data_segment_length = (1 << 24) - 1;
431 if (isl->isl_max_send_data_segment_length == 0)
432 isl->isl_max_send_data_segment_length =
433 isl->isl_max_recv_data_segment_length;
434 if (isl->isl_max_burst_length == 0)
435 isl->isl_max_burst_length = (1 << 24) - 1;
436 if (isl->isl_first_burst_length == 0)
437 isl->isl_first_burst_length = (1 << 24) - 1;
438 if (isl->isl_first_burst_length > isl->isl_max_burst_length)
439 isl->isl_first_burst_length = isl->isl_max_burst_length;
442 * Limit default send length in case it won't be negotiated.
443 * We can't do it for other limits, since they may affect both
444 * sender and receiver operation, and we must obey defaults.
446 if (conn->conn.conn_max_send_data_segment_length >
447 isl->isl_max_send_data_segment_length) {
448 conn->conn.conn_max_send_data_segment_length =
449 isl->isl_max_send_data_segment_length;
454 handoff(struct iscsid_connection *conn)
456 struct iscsi_daemon_handoff idh;
459 log_debugx("handing off connection to the kernel");
461 memset(&idh, 0, sizeof(idh));
462 idh.idh_session_id = conn->conn_session_id;
463 idh.idh_socket = conn->conn.conn_socket;
464 strlcpy(idh.idh_target_alias, conn->conn_target_alias,
465 sizeof(idh.idh_target_alias));
466 idh.idh_tsih = conn->conn.conn_tsih;
467 idh.idh_statsn = conn->conn.conn_statsn;
468 idh.idh_protocol_level = conn->conn_protocol_level;
469 idh.idh_header_digest = conn->conn.conn_header_digest;
470 idh.idh_data_digest = conn->conn.conn_data_digest;
471 idh.idh_initial_r2t = conn->conn_initial_r2t;
472 idh.idh_immediate_data = conn->conn.conn_immediate_data;
473 idh.idh_max_recv_data_segment_length =
474 conn->conn.conn_max_recv_data_segment_length;
475 idh.idh_max_send_data_segment_length =
476 conn->conn.conn_max_send_data_segment_length;
477 idh.idh_max_burst_length = conn->conn.conn_max_burst_length;
478 idh.idh_first_burst_length = conn->conn.conn_first_burst_length;
480 error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
482 log_err(1, "ISCSIDHANDOFF");
486 fail(const struct connection *base_conn, const char *reason)
488 const struct iscsid_connection *conn;
489 struct iscsi_daemon_fail idf;
490 int error, saved_errno;
492 conn = (const struct iscsid_connection *)base_conn;
495 memset(&idf, 0, sizeof(idf));
496 idf.idf_session_id = conn->conn_session_id;
497 strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
499 error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
501 log_err(1, "ISCSIDFAIL");
507 * XXX: I CANT INTO LATIN
510 capsicate(struct iscsid_connection *conn)
513 const unsigned long cmds[] = {
514 #ifdef ICL_KERNEL_PROXY
527 cap_rights_init(&rights, CAP_IOCTL);
528 if (caph_rights_limit(conn->conn_iscsi_fd, &rights) < 0)
529 log_err(1, "cap_rights_limit");
531 if (caph_ioctls_limit(conn->conn_iscsi_fd, cmds, nitems(cmds)) < 0)
532 log_err(1, "cap_ioctls_limit");
534 if (caph_enter() != 0)
535 log_err(1, "cap_enter");
538 log_debugx("Capsicum capability mode enabled");
540 log_warnx("Capsicum capability mode not supported");
547 return (sigalrm_received);
551 sigalrm_handler(int dummy __unused)
554 * It would be easiest to just log an error and exit. We can't
555 * do this, though, because log_errx() is not signal safe, since
556 * it calls syslog(3). Instead, set a flag checked by pdu_send()
557 * and pdu_receive(), to call log_errx() there. Should they fail
558 * to notice, we'll exit here one second later.
560 if (sigalrm_received) {
562 * Oh well. Just give up and quit.
567 sigalrm_received = true;
571 set_timeout(int timeout)
574 struct itimerval itv;
578 log_debugx("session timeout disabled");
582 bzero(&sa, sizeof(sa));
583 sa.sa_handler = sigalrm_handler;
584 sigfillset(&sa.sa_mask);
585 error = sigaction(SIGALRM, &sa, NULL);
587 log_err(1, "sigaction");
590 * First SIGALRM will arive after conf_timeout seconds.
591 * If we do nothing, another one will arrive a second later.
593 bzero(&itv, sizeof(itv));
594 itv.it_interval.tv_sec = 1;
595 itv.it_value.tv_sec = timeout;
597 log_debugx("setting session timeout to %d seconds",
599 error = setitimer(ITIMER_REAL, &itv, NULL);
601 log_err(1, "setitimer");
605 sigchld_handler(int dummy __unused)
609 * The only purpose of this handler is to make SIGCHLD
610 * interrupt the ISCSIDWAIT ioctl(2), so we can call
611 * wait_for_children().
616 register_sigchld(void)
621 bzero(&sa, sizeof(sa));
622 sa.sa_handler = sigchld_handler;
623 sigfillset(&sa.sa_mask);
624 error = sigaction(SIGCHLD, &sa, NULL);
626 log_err(1, "sigaction");
631 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
633 struct iscsid_connection *conn;
635 log_set_peer_addr(request->idr_conf.isc_target_addr);
636 if (request->idr_conf.isc_target[0] != '\0') {
637 log_set_peer_name(request->idr_conf.isc_target);
638 setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
640 setproctitle("%s", request->idr_conf.isc_target_addr);
643 conn = connection_new(iscsi_fd, request);
646 set_timeout(timeout);
648 if (conn->conn_conf.isc_discovery != 0)
653 log_debugx("nothing more to do; exiting");
658 wait_for_children(bool block)
666 * If "block" is true, wait for at least one process.
668 if (block && num == 0)
669 pid = wait4(-1, &status, 0, NULL);
671 pid = wait4(-1, &status, WNOHANG, NULL);
674 if (WIFSIGNALED(status)) {
675 log_warnx("child process %d terminated with signal %d",
676 pid, WTERMSIG(status));
677 } else if (WEXITSTATUS(status) != 0) {
678 log_warnx("child process %d terminated with exit status %d",
679 pid, WEXITSTATUS(status));
681 log_debugx("child process %d terminated gracefully", pid);
690 main(int argc, char **argv)
692 int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
694 bool dont_daemonize = false;
697 const char *pidfile_path = DEFAULT_PIDFILE;
698 struct iscsi_daemon_request request;
700 while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
703 pidfile_path = optarg;
706 dont_daemonize = true;
710 debug = atoi(optarg);
713 maxproc = atoi(optarg);
716 timeout = atoi(optarg);
729 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
732 log_errx(1, "daemon already running, pid: %jd.",
734 log_err(1, "cannot open or create pidfile \"%s\"",
738 iscsi_fd = open(ISCSI_PATH, O_RDWR);
739 if (iscsi_fd < 0 && errno == ENOENT) {
741 retval = kldload("iscsi");
743 iscsi_fd = open(ISCSI_PATH, O_RDWR);
748 log_err(1, "failed to open %s", ISCSI_PATH);
750 if (dont_daemonize == false) {
751 if (daemon(0, 0) == -1) {
752 log_warn("cannot daemonize");
753 pidfile_remove(pidfh);
758 pidfile_write(pidfh);
763 log_debugx("waiting for request from the kernel");
765 memset(&request, 0, sizeof(request));
766 error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
768 if (errno == EINTR) {
769 nchildren -= wait_for_children(false);
770 assert(nchildren >= 0);
774 log_err(1, "ISCSIDWAIT");
777 if (dont_daemonize) {
778 log_debugx("not forking due to -d flag; "
779 "will exit after servicing a single request");
781 nchildren -= wait_for_children(false);
782 assert(nchildren >= 0);
784 while (maxproc > 0 && nchildren >= maxproc) {
785 log_debugx("maxproc limit of %d child processes hit; "
786 "waiting for child process to exit", maxproc);
787 nchildren -= wait_for_children(true);
788 assert(nchildren >= 0);
790 log_debugx("incoming connection; forking child process #%d",
801 pidfile_close(pidfh);
802 handle_request(iscsi_fd, &request, timeout);