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/cdefs.h>
33 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/param.h>
37 #include <sys/linker.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/capsicum.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
45 #include <capsicum_helpers.h>
60 static bool timed_out(void);
61 #ifdef ICL_KERNEL_PROXY
62 static void pdu_receive_proxy(struct pdu *pdu);
63 static void pdu_send_proxy(struct pdu *pdu);
64 #endif /* ICL_KERNEL_PROXY */
66 static volatile bool sigalrm_received = false;
68 static int nchildren = 0;
70 static struct connection_ops conn_ops = {
71 .timed_out = timed_out,
72 #ifdef ICL_KERNEL_PROXY
73 .pdu_receive_proxy = pdu_receive_proxy,
74 .pdu_send_proxy = pdu_send_proxy,
83 fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
87 #ifdef ICL_KERNEL_PROXY
90 pdu_receive_proxy(struct pdu *pdu)
92 struct iscsid_connection *conn;
93 struct iscsi_daemon_receive idr;
97 conn = (struct iscsid_connection *)pdu->pdu_connection;
98 assert(conn->conn_conf.isc_iser != 0);
100 pdu->pdu_data = malloc(conn->conn.conn_max_recv_data_segment_length);
101 if (pdu->pdu_data == NULL)
102 log_err(1, "malloc");
104 memset(&idr, 0, sizeof(idr));
105 idr.idr_session_id = conn->conn_session_id;
106 idr.idr_bhs = pdu->pdu_bhs;
107 idr.idr_data_segment_len = conn->conn.conn_max_recv_data_segment_length;
108 idr.idr_data_segment = pdu->pdu_data;
110 error = ioctl(conn->conn_iscsi_fd, ISCSIDRECEIVE, &idr);
112 log_err(1, "ISCSIDRECEIVE");
114 len = pdu_ahs_length(pdu);
116 log_errx(1, "protocol error: non-empty AHS");
118 len = pdu_data_segment_length(pdu);
119 assert(len <= (size_t)conn->conn.conn_max_recv_data_segment_length);
120 pdu->pdu_data_len = len;
124 pdu_send_proxy(struct pdu *pdu)
126 struct iscsid_connection *conn;
127 struct iscsi_daemon_send ids;
130 conn = (struct iscsid_connection *)pdu->pdu_connection;
131 assert(conn->conn_conf.isc_iser != 0);
133 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
135 memset(&ids, 0, sizeof(ids));
136 ids.ids_session_id = conn->conn_session_id;
137 ids.ids_bhs = pdu->pdu_bhs;
138 ids.ids_data_segment_len = pdu->pdu_data_len;
139 ids.ids_data_segment = pdu->pdu_data;
141 error = ioctl(conn->conn_iscsi_fd, ISCSIDSEND, &ids);
143 log_err(1, "ISCSIDSEND");
146 #endif /* ICL_KERNEL_PROXY */
149 resolve_addr(const struct connection *conn, const char *address,
150 struct addrinfo **ai, bool initiator_side)
152 struct addrinfo hints;
153 char *arg, *addr, *ch, *tofree;
155 int error, colons = 0;
157 tofree = arg = checked_strdup(address);
159 if (arg[0] == '\0') {
160 fail(conn, "empty address");
161 log_errx(1, "empty address");
165 * IPv6 address in square brackets, perhaps with port.
168 addr = strsep(&arg, "]");
170 fail(conn, "malformed address");
171 log_errx(1, "malformed address %s", address);
173 if (arg[0] == '\0') {
175 } else if (arg[0] == ':') {
178 fail(conn, "malformed address");
179 log_errx(1, "malformed address %s", address);
183 * Either IPv6 address without brackets - and without
184 * a port - or IPv4 address. Just count the colons.
186 for (ch = arg; *ch != '\0'; ch++) {
194 addr = strsep(&arg, ":");
202 if (port == NULL && !initiator_side)
205 memset(&hints, 0, sizeof(hints));
206 hints.ai_family = PF_UNSPEC;
207 hints.ai_socktype = SOCK_STREAM;
208 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
210 hints.ai_flags |= AI_PASSIVE;
212 error = getaddrinfo(addr, port, &hints, ai);
214 fail(conn, gai_strerror(error));
215 log_errx(1, "getaddrinfo for %s failed: %s",
216 address, gai_strerror(error));
222 static struct iscsid_connection *
223 connection_new(int iscsi_fd, const struct iscsi_daemon_request *request)
225 struct iscsid_connection *conn;
226 struct addrinfo *from_ai, *to_ai;
227 const char *from_addr, *to_addr;
228 #ifdef ICL_KERNEL_PROXY
229 struct iscsi_daemon_connect idc;
233 conn = calloc(1, sizeof(*conn));
235 log_err(1, "calloc");
237 connection_init(&conn->conn, &conn_ops,
238 request->idr_conf.isc_iser != 0);
239 conn->conn_protocol_level = 0;
240 conn->conn_initial_r2t = true;
241 conn->conn_iscsi_fd = iscsi_fd;
243 conn->conn_session_id = request->idr_session_id;
244 memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf));
245 memcpy(&conn->conn.conn_isid, &request->idr_isid,
246 sizeof(conn->conn.conn_isid));
247 conn->conn.conn_tsih = request->idr_tsih;
249 from_addr = conn->conn_conf.isc_initiator_addr;
250 to_addr = conn->conn_conf.isc_target_addr;
252 if (from_addr[0] != '\0')
253 resolve_addr(&conn->conn, from_addr, &from_ai, true);
257 resolve_addr(&conn->conn, to_addr, &to_ai, false);
259 #ifdef ICL_KERNEL_PROXY
260 if (conn->conn_conf.isc_iser) {
261 memset(&idc, 0, sizeof(idc));
262 idc.idc_session_id = conn->conn_session_id;
263 if (conn->conn_conf.isc_iser)
265 idc.idc_domain = to_ai->ai_family;
266 idc.idc_socktype = to_ai->ai_socktype;
267 idc.idc_protocol = to_ai->ai_protocol;
268 if (from_ai != NULL) {
269 idc.idc_from_addr = from_ai->ai_addr;
270 idc.idc_from_addrlen = from_ai->ai_addrlen;
272 idc.idc_to_addr = to_ai->ai_addr;
273 idc.idc_to_addrlen = to_ai->ai_addrlen;
275 log_debugx("connecting to %s using ICL kernel proxy", to_addr);
276 error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
278 fail(&conn->conn, strerror(errno));
279 log_err(1, "failed to connect to %s "
280 "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
284 freeaddrinfo(from_ai);
289 #endif /* ICL_KERNEL_PROXY */
291 if (conn->conn_conf.isc_iser) {
292 fail(&conn->conn, "iSER not supported");
293 log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
294 "does not support iSER");
297 conn->conn.conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
299 if (conn->conn.conn_socket < 0) {
300 fail(&conn->conn, strerror(errno));
301 log_err(1, "failed to create socket for %s", from_addr);
303 optval = SOCKBUF_SIZE;
304 if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_RCVBUF,
305 &optval, sizeof(optval)) == -1)
306 log_warn("setsockopt(SO_RCVBUF) failed");
307 optval = SOCKBUF_SIZE;
308 if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_SNDBUF,
309 &optval, sizeof(optval)) == -1)
310 log_warn("setsockopt(SO_SNDBUF) failed");
312 if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_NO_DDP,
313 &optval, sizeof(optval)) == -1)
314 log_warn("setsockopt(SO_NO_DDP) failed");
315 if (conn->conn_conf.isc_dscp != -1) {
316 int tos = conn->conn_conf.isc_dscp << 2;
317 if (to_ai->ai_family == AF_INET) {
318 if (setsockopt(conn->conn.conn_socket,
320 &tos, sizeof(tos)) == -1)
321 log_warn("setsockopt(IP_TOS) "
325 if (to_ai->ai_family == AF_INET6) {
326 if (setsockopt(conn->conn.conn_socket,
327 IPPROTO_IPV6, IPV6_TCLASS,
328 &tos, sizeof(tos)) == -1)
329 log_warn("setsockopt(IPV6_TCLASS) "
334 if (conn->conn_conf.isc_pcp != -1) {
335 int pcp = conn->conn_conf.isc_pcp;
336 if (to_ai->ai_family == AF_INET) {
337 if (setsockopt(conn->conn.conn_socket,
338 IPPROTO_IP, IP_VLAN_PCP,
339 &pcp, sizeof(pcp)) == -1)
340 log_warn("setsockopt(IP_VLAN_PCP) "
344 if (to_ai->ai_family == AF_INET6) {
345 if (setsockopt(conn->conn.conn_socket,
346 IPPROTO_IPV6, IPV6_VLAN_PCP,
347 &pcp, sizeof(pcp)) == -1)
348 log_warn("setsockopt(IPV6_VLAN_PCP) "
354 * Reduce TCP SYN_SENT timeout while
355 * no connectivity exists, to allow
356 * rapid reuse of the available slots.
359 if (conn->conn_conf.isc_login_timeout > 0) {
360 keepinit = conn->conn_conf.isc_login_timeout;
361 log_debugx("session specific LoginTimeout at %d sec",
364 if (conn->conn_conf.isc_login_timeout == -1) {
366 size_t size = sizeof(value);
367 if (sysctlbyname("kern.iscsi.login_timeout",
368 &value, &size, NULL, 0) == 0) {
370 log_debugx("global login_timeout at %d sec",
375 if (setsockopt(conn->conn.conn_socket,
376 IPPROTO_TCP, TCP_KEEPINIT,
377 &keepinit, sizeof(keepinit)) == -1)
378 log_warnx("setsockopt(TCP_KEEPINIT) "
379 "failed for %s", to_addr);
381 if (from_ai != NULL) {
382 error = bind(conn->conn.conn_socket, from_ai->ai_addr,
383 from_ai->ai_addrlen);
385 fail(&conn->conn, strerror(errno));
386 log_err(1, "failed to bind to %s", from_addr);
389 log_debugx("connecting to %s", to_addr);
390 error = connect(conn->conn.conn_socket, to_ai->ai_addr,
393 fail(&conn->conn, strerror(errno));
394 log_err(1, "failed to connect to %s", to_addr);
398 freeaddrinfo(from_ai);
405 limits(struct iscsid_connection *conn)
407 struct iscsi_daemon_limits idl;
408 struct iscsi_session_limits *isl;
411 log_debugx("fetching limits from the kernel");
413 memset(&idl, 0, sizeof(idl));
414 idl.idl_session_id = conn->conn_session_id;
415 idl.idl_socket = conn->conn.conn_socket;
417 error = ioctl(conn->conn_iscsi_fd, ISCSIDLIMITS, &idl);
419 log_err(1, "ISCSIDLIMITS");
422 * Read the driver limits and provide reasonable defaults for the ones
423 * the driver doesn't care about. If a max_snd_dsl is not explicitly
424 * provided by the driver then we'll make sure both conn->max_snd_dsl
425 * and isl->max_snd_dsl are set to the rcv_dsl. This preserves historic
428 isl = &conn->conn_limits;
429 memcpy(isl, &idl.idl_limits, sizeof(*isl));
430 if (isl->isl_max_recv_data_segment_length == 0)
431 isl->isl_max_recv_data_segment_length = (1 << 24) - 1;
432 if (isl->isl_max_send_data_segment_length == 0)
433 isl->isl_max_send_data_segment_length =
434 isl->isl_max_recv_data_segment_length;
435 if (isl->isl_max_burst_length == 0)
436 isl->isl_max_burst_length = (1 << 24) - 1;
437 if (isl->isl_first_burst_length == 0)
438 isl->isl_first_burst_length = (1 << 24) - 1;
439 if (isl->isl_first_burst_length > isl->isl_max_burst_length)
440 isl->isl_first_burst_length = isl->isl_max_burst_length;
443 * Limit default send length in case it won't be negotiated.
444 * We can't do it for other limits, since they may affect both
445 * sender and receiver operation, and we must obey defaults.
447 if (conn->conn.conn_max_send_data_segment_length >
448 isl->isl_max_send_data_segment_length) {
449 conn->conn.conn_max_send_data_segment_length =
450 isl->isl_max_send_data_segment_length;
455 handoff(struct iscsid_connection *conn)
457 struct iscsi_daemon_handoff idh;
460 log_debugx("handing off connection to the kernel");
462 memset(&idh, 0, sizeof(idh));
463 idh.idh_session_id = conn->conn_session_id;
464 idh.idh_socket = conn->conn.conn_socket;
465 strlcpy(idh.idh_target_alias, conn->conn_target_alias,
466 sizeof(idh.idh_target_alias));
467 idh.idh_tsih = conn->conn.conn_tsih;
468 idh.idh_statsn = conn->conn.conn_statsn;
469 idh.idh_protocol_level = conn->conn_protocol_level;
470 idh.idh_header_digest = conn->conn.conn_header_digest;
471 idh.idh_data_digest = conn->conn.conn_data_digest;
472 idh.idh_initial_r2t = conn->conn_initial_r2t;
473 idh.idh_immediate_data = conn->conn.conn_immediate_data;
474 idh.idh_max_recv_data_segment_length =
475 conn->conn.conn_max_recv_data_segment_length;
476 idh.idh_max_send_data_segment_length =
477 conn->conn.conn_max_send_data_segment_length;
478 idh.idh_max_burst_length = conn->conn.conn_max_burst_length;
479 idh.idh_first_burst_length = conn->conn.conn_first_burst_length;
481 error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
483 log_err(1, "ISCSIDHANDOFF");
487 fail(const struct connection *base_conn, const char *reason)
489 const struct iscsid_connection *conn;
490 struct iscsi_daemon_fail idf;
491 int error, saved_errno;
493 conn = (const struct iscsid_connection *)base_conn;
496 memset(&idf, 0, sizeof(idf));
497 idf.idf_session_id = conn->conn_session_id;
498 strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
500 error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
502 log_err(1, "ISCSIDFAIL");
508 * XXX: I CANT INTO LATIN
511 capsicate(struct iscsid_connection *conn)
514 const unsigned long cmds[] = {
515 #ifdef ICL_KERNEL_PROXY
528 cap_rights_init(&rights, CAP_IOCTL);
529 if (caph_rights_limit(conn->conn_iscsi_fd, &rights) < 0)
530 log_err(1, "cap_rights_limit");
532 if (caph_ioctls_limit(conn->conn_iscsi_fd, cmds, nitems(cmds)) < 0)
533 log_err(1, "cap_ioctls_limit");
535 if (caph_enter() != 0)
536 log_err(1, "cap_enter");
539 log_debugx("Capsicum capability mode enabled");
541 log_warnx("Capsicum capability mode not supported");
548 return (sigalrm_received);
552 sigalrm_handler(int dummy __unused)
555 * It would be easiest to just log an error and exit. We can't
556 * do this, though, because log_errx() is not signal safe, since
557 * it calls syslog(3). Instead, set a flag checked by pdu_send()
558 * and pdu_receive(), to call log_errx() there. Should they fail
559 * to notice, we'll exit here one second later.
561 if (sigalrm_received) {
563 * Oh well. Just give up and quit.
568 sigalrm_received = true;
572 set_timeout(int timeout)
575 struct itimerval itv;
579 log_debugx("session timeout disabled");
583 bzero(&sa, sizeof(sa));
584 sa.sa_handler = sigalrm_handler;
585 sigfillset(&sa.sa_mask);
586 error = sigaction(SIGALRM, &sa, NULL);
588 log_err(1, "sigaction");
591 * First SIGALRM will arive after conf_timeout seconds.
592 * If we do nothing, another one will arrive a second later.
594 bzero(&itv, sizeof(itv));
595 itv.it_interval.tv_sec = 1;
596 itv.it_value.tv_sec = timeout;
598 log_debugx("setting session timeout to %d seconds",
600 error = setitimer(ITIMER_REAL, &itv, NULL);
602 log_err(1, "setitimer");
606 sigchld_handler(int dummy __unused)
610 * The only purpose of this handler is to make SIGCHLD
611 * interrupt the ISCSIDWAIT ioctl(2), so we can call
612 * wait_for_children().
617 register_sigchld(void)
622 bzero(&sa, sizeof(sa));
623 sa.sa_handler = sigchld_handler;
624 sigfillset(&sa.sa_mask);
625 error = sigaction(SIGCHLD, &sa, NULL);
627 log_err(1, "sigaction");
632 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
634 struct iscsid_connection *conn;
636 log_set_peer_addr(request->idr_conf.isc_target_addr);
637 if (request->idr_conf.isc_target[0] != '\0') {
638 log_set_peer_name(request->idr_conf.isc_target);
639 setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
641 setproctitle("%s", request->idr_conf.isc_target_addr);
644 conn = connection_new(iscsi_fd, request);
647 set_timeout(timeout);
649 if (conn->conn_conf.isc_discovery != 0)
654 log_debugx("nothing more to do; exiting");
659 wait_for_children(bool block)
667 * If "block" is true, wait for at least one process.
669 if (block && num == 0)
670 pid = wait4(-1, &status, 0, NULL);
672 pid = wait4(-1, &status, WNOHANG, NULL);
675 if (WIFSIGNALED(status)) {
676 log_warnx("child process %d terminated with signal %d",
677 pid, WTERMSIG(status));
678 } else if (WEXITSTATUS(status) != 0) {
679 log_warnx("child process %d terminated with exit status %d",
680 pid, WEXITSTATUS(status));
682 log_debugx("child process %d terminated gracefully", pid);
691 main(int argc, char **argv)
693 int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
695 bool dont_daemonize = false;
698 const char *pidfile_path = DEFAULT_PIDFILE;
699 struct iscsi_daemon_request request;
701 while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
704 pidfile_path = optarg;
707 dont_daemonize = true;
711 debug = atoi(optarg);
714 maxproc = atoi(optarg);
717 timeout = atoi(optarg);
730 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
733 log_errx(1, "daemon already running, pid: %jd.",
735 log_err(1, "cannot open or create pidfile \"%s\"",
739 iscsi_fd = open(ISCSI_PATH, O_RDWR);
740 if (iscsi_fd < 0 && errno == ENOENT) {
742 retval = kldload("iscsi");
744 iscsi_fd = open(ISCSI_PATH, O_RDWR);
749 log_err(1, "failed to open %s", ISCSI_PATH);
751 if (dont_daemonize == false) {
752 if (daemon(0, 0) == -1) {
753 log_warn("cannot daemonize");
754 pidfile_remove(pidfh);
759 pidfile_write(pidfh);
764 log_debugx("waiting for request from the kernel");
766 memset(&request, 0, sizeof(request));
767 error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
769 if (errno == EINTR) {
770 nchildren -= wait_for_children(false);
771 assert(nchildren >= 0);
775 log_err(1, "ISCSIDWAIT");
778 if (dont_daemonize) {
779 log_debugx("not forking due to -d flag; "
780 "will exit after servicing a single request");
782 nchildren -= wait_for_children(false);
783 assert(nchildren >= 0);
785 while (maxproc > 0 && nchildren >= maxproc) {
786 log_debugx("maxproc limit of %d child processes hit; "
787 "waiting for child process to exit", maxproc);
788 nchildren -= wait_for_children(true);
789 assert(nchildren >= 0);
791 log_debugx("incoming connection; forking child process #%d",
802 pidfile_close(pidfh);
803 handle_request(iscsi_fd, &request, timeout);