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>
44 #include <netinet/in.h>
46 #include <capsicum_helpers.h>
61 static volatile bool sigalrm_received = false;
63 static int nchildren = 0;
69 fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
74 checked_strdup(const char *s)
85 resolve_addr(const struct connection *conn, const char *address,
86 struct addrinfo **ai, bool initiator_side)
88 struct addrinfo hints;
89 char *arg, *addr, *ch;
91 int error, colons = 0;
93 arg = checked_strdup(address);
96 fail(conn, "empty address");
97 log_errx(1, "empty address");
101 * IPv6 address in square brackets, perhaps with port.
104 addr = strsep(&arg, "]");
106 fail(conn, "malformed address");
107 log_errx(1, "malformed address %s", address);
109 if (arg[0] == '\0') {
111 } else if (arg[0] == ':') {
114 fail(conn, "malformed address");
115 log_errx(1, "malformed address %s", address);
119 * Either IPv6 address without brackets - and without
120 * a port - or IPv4 address. Just count the colons.
122 for (ch = arg; *ch != '\0'; ch++) {
130 addr = strsep(&arg, ":");
138 if (port == NULL && !initiator_side)
141 memset(&hints, 0, sizeof(hints));
142 hints.ai_family = PF_UNSPEC;
143 hints.ai_socktype = SOCK_STREAM;
144 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
146 hints.ai_flags |= AI_PASSIVE;
148 error = getaddrinfo(addr, port, &hints, ai);
150 fail(conn, gai_strerror(error));
151 log_errx(1, "getaddrinfo for %s failed: %s",
152 address, gai_strerror(error));
156 static struct connection *
157 connection_new(int iscsi_fd, const struct iscsi_daemon_request *request)
159 struct connection *conn;
160 struct iscsi_session_limits *isl;
161 struct addrinfo *from_ai, *to_ai;
162 const char *from_addr, *to_addr;
163 #ifdef ICL_KERNEL_PROXY
164 struct iscsi_daemon_connect idc;
168 conn = calloc(1, sizeof(*conn));
170 log_err(1, "calloc");
173 * Default values, from RFC 3720, section 12.
175 conn->conn_protocol_level = 0;
176 conn->conn_header_digest = CONN_DIGEST_NONE;
177 conn->conn_data_digest = CONN_DIGEST_NONE;
178 conn->conn_initial_r2t = true;
179 conn->conn_immediate_data = true;
180 conn->conn_max_recv_data_segment_length = 8192;
181 conn->conn_max_send_data_segment_length = 8192;
182 conn->conn_max_burst_length = 262144;
183 conn->conn_first_burst_length = 65536;
184 conn->conn_iscsi_fd = iscsi_fd;
186 conn->conn_session_id = request->idr_session_id;
187 memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf));
188 memcpy(&conn->conn_isid, &request->idr_isid, sizeof(conn->conn_isid));
189 conn->conn_tsih = request->idr_tsih;
192 * Read the driver limits and provide reasonable defaults for the ones
193 * the driver doesn't care about. If a max_snd_dsl is not explicitly
194 * provided by the driver then we'll make sure both conn->max_snd_dsl
195 * and isl->max_snd_dsl are set to the rcv_dsl. This preserves historic
198 isl = &conn->conn_limits;
199 memcpy(isl, &request->idr_limits, sizeof(*isl));
200 if (isl->isl_max_recv_data_segment_length == 0)
201 isl->isl_max_recv_data_segment_length = (1 << 24) - 1;
202 if (isl->isl_max_send_data_segment_length == 0)
203 isl->isl_max_send_data_segment_length =
204 isl->isl_max_recv_data_segment_length;
205 if (isl->isl_max_burst_length == 0)
206 isl->isl_max_burst_length = (1 << 24) - 1;
207 if (isl->isl_first_burst_length == 0)
208 isl->isl_first_burst_length = (1 << 24) - 1;
209 if (isl->isl_first_burst_length > isl->isl_max_burst_length)
210 isl->isl_first_burst_length = isl->isl_max_burst_length;
213 * Limit default send length in case it won't be negotiated.
214 * We can't do it for other limits, since they may affect both
215 * sender and receiver operation, and we must obey defaults.
217 if (conn->conn_max_send_data_segment_length >
218 isl->isl_max_send_data_segment_length) {
219 conn->conn_max_send_data_segment_length =
220 isl->isl_max_send_data_segment_length;
223 from_addr = conn->conn_conf.isc_initiator_addr;
224 to_addr = conn->conn_conf.isc_target_addr;
226 if (from_addr[0] != '\0')
227 resolve_addr(conn, from_addr, &from_ai, true);
231 resolve_addr(conn, to_addr, &to_ai, false);
233 #ifdef ICL_KERNEL_PROXY
234 if (conn->conn_conf.isc_iser) {
235 memset(&idc, 0, sizeof(idc));
236 idc.idc_session_id = conn->conn_session_id;
237 if (conn->conn_conf.isc_iser)
239 idc.idc_domain = to_ai->ai_family;
240 idc.idc_socktype = to_ai->ai_socktype;
241 idc.idc_protocol = to_ai->ai_protocol;
242 if (from_ai != NULL) {
243 idc.idc_from_addr = from_ai->ai_addr;
244 idc.idc_from_addrlen = from_ai->ai_addrlen;
246 idc.idc_to_addr = to_ai->ai_addr;
247 idc.idc_to_addrlen = to_ai->ai_addrlen;
249 log_debugx("connecting to %s using ICL kernel proxy", to_addr);
250 error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
252 fail(conn, strerror(errno));
253 log_err(1, "failed to connect to %s "
254 "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
259 #endif /* ICL_KERNEL_PROXY */
261 if (conn->conn_conf.isc_iser) {
262 fail(conn, "iSER not supported");
263 log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
264 "does not support iSER");
267 conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
269 if (conn->conn_socket < 0) {
270 fail(conn, strerror(errno));
271 log_err(1, "failed to create socket for %s", from_addr);
273 sockbuf = SOCKBUF_SIZE;
274 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF,
275 &sockbuf, sizeof(sockbuf)) == -1)
276 log_warn("setsockopt(SO_RCVBUF) failed");
277 sockbuf = SOCKBUF_SIZE;
278 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF,
279 &sockbuf, sizeof(sockbuf)) == -1)
280 log_warn("setsockopt(SO_SNDBUF) failed");
281 if (conn->conn_conf.isc_dscp != -1) {
282 int tos = conn->conn_conf.isc_dscp << 2;
283 if (to_ai->ai_family == AF_INET) {
284 if (setsockopt(conn->conn_socket,
286 &tos, sizeof(tos)) == -1)
287 log_warn("setsockopt(IP_TOS) "
291 if (to_ai->ai_family == AF_INET6) {
292 if (setsockopt(conn->conn_socket,
293 IPPROTO_IPV6, IPV6_TCLASS,
294 &tos, sizeof(tos)) == -1)
295 log_warn("setsockopt(IPV6_TCLASS) "
300 if (from_ai != NULL) {
301 error = bind(conn->conn_socket, from_ai->ai_addr,
302 from_ai->ai_addrlen);
304 fail(conn, strerror(errno));
305 log_err(1, "failed to bind to %s", from_addr);
308 log_debugx("connecting to %s", to_addr);
309 error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen);
311 fail(conn, strerror(errno));
312 log_err(1, "failed to connect to %s", to_addr);
319 handoff(struct connection *conn)
321 struct iscsi_daemon_handoff idh;
324 log_debugx("handing off connection to the kernel");
326 memset(&idh, 0, sizeof(idh));
327 idh.idh_session_id = conn->conn_session_id;
328 idh.idh_socket = conn->conn_socket;
329 strlcpy(idh.idh_target_alias, conn->conn_target_alias,
330 sizeof(idh.idh_target_alias));
331 idh.idh_tsih = conn->conn_tsih;
332 idh.idh_statsn = conn->conn_statsn;
333 idh.idh_protocol_level = conn->conn_protocol_level;
334 idh.idh_header_digest = conn->conn_header_digest;
335 idh.idh_data_digest = conn->conn_data_digest;
336 idh.idh_initial_r2t = conn->conn_initial_r2t;
337 idh.idh_immediate_data = conn->conn_immediate_data;
338 idh.idh_max_recv_data_segment_length =
339 conn->conn_max_recv_data_segment_length;
340 idh.idh_max_send_data_segment_length =
341 conn->conn_max_send_data_segment_length;
342 idh.idh_max_burst_length = conn->conn_max_burst_length;
343 idh.idh_first_burst_length = conn->conn_first_burst_length;
345 error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
347 log_err(1, "ISCSIDHANDOFF");
351 fail(const struct connection *conn, const char *reason)
353 struct iscsi_daemon_fail idf;
354 int error, saved_errno;
358 memset(&idf, 0, sizeof(idf));
359 idf.idf_session_id = conn->conn_session_id;
360 strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
362 error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
364 log_err(1, "ISCSIDFAIL");
370 * XXX: I CANT INTO LATIN
373 capsicate(struct connection *conn)
376 #ifdef ICL_KERNEL_PROXY
377 const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE,
378 ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY };
380 const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD,
381 ISCSISREMOVE, ISCSISMODIFY };
384 cap_rights_init(&rights, CAP_IOCTL);
385 if (caph_rights_limit(conn->conn_iscsi_fd, &rights) < 0)
386 log_err(1, "cap_rights_limit");
388 if (caph_ioctls_limit(conn->conn_iscsi_fd, cmds, nitems(cmds)) < 0)
389 log_err(1, "cap_ioctls_limit");
391 if (caph_enter() != 0)
392 log_err(1, "cap_enter");
395 log_debugx("Capsicum capability mode enabled");
397 log_warnx("Capsicum capability mode not supported");
404 return (sigalrm_received);
408 sigalrm_handler(int dummy __unused)
411 * It would be easiest to just log an error and exit. We can't
412 * do this, though, because log_errx() is not signal safe, since
413 * it calls syslog(3). Instead, set a flag checked by pdu_send()
414 * and pdu_receive(), to call log_errx() there. Should they fail
415 * to notice, we'll exit here one second later.
417 if (sigalrm_received) {
419 * Oh well. Just give up and quit.
424 sigalrm_received = true;
428 set_timeout(int timeout)
431 struct itimerval itv;
435 log_debugx("session timeout disabled");
439 bzero(&sa, sizeof(sa));
440 sa.sa_handler = sigalrm_handler;
441 sigfillset(&sa.sa_mask);
442 error = sigaction(SIGALRM, &sa, NULL);
444 log_err(1, "sigaction");
447 * First SIGALRM will arive after conf_timeout seconds.
448 * If we do nothing, another one will arrive a second later.
450 bzero(&itv, sizeof(itv));
451 itv.it_interval.tv_sec = 1;
452 itv.it_value.tv_sec = timeout;
454 log_debugx("setting session timeout to %d seconds",
456 error = setitimer(ITIMER_REAL, &itv, NULL);
458 log_err(1, "setitimer");
462 sigchld_handler(int dummy __unused)
466 * The only purpose of this handler is to make SIGCHLD
467 * interrupt the ISCSIDWAIT ioctl(2), so we can call
468 * wait_for_children().
473 register_sigchld(void)
478 bzero(&sa, sizeof(sa));
479 sa.sa_handler = sigchld_handler;
480 sigfillset(&sa.sa_mask);
481 error = sigaction(SIGCHLD, &sa, NULL);
483 log_err(1, "sigaction");
488 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
490 struct connection *conn;
492 log_set_peer_addr(request->idr_conf.isc_target_addr);
493 if (request->idr_conf.isc_target[0] != '\0') {
494 log_set_peer_name(request->idr_conf.isc_target);
495 setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
497 setproctitle("%s", request->idr_conf.isc_target_addr);
500 conn = connection_new(iscsi_fd, request);
501 set_timeout(timeout);
504 if (conn->conn_conf.isc_discovery != 0)
509 log_debugx("nothing more to do; exiting");
514 wait_for_children(bool block)
522 * If "block" is true, wait for at least one process.
524 if (block && num == 0)
525 pid = wait4(-1, &status, 0, NULL);
527 pid = wait4(-1, &status, WNOHANG, NULL);
530 if (WIFSIGNALED(status)) {
531 log_warnx("child process %d terminated with signal %d",
532 pid, WTERMSIG(status));
533 } else if (WEXITSTATUS(status) != 0) {
534 log_warnx("child process %d terminated with exit status %d",
535 pid, WEXITSTATUS(status));
537 log_debugx("child process %d terminated gracefully", pid);
546 main(int argc, char **argv)
548 int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
550 bool dont_daemonize = false;
553 const char *pidfile_path = DEFAULT_PIDFILE;
554 struct iscsi_daemon_request request;
556 while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
559 pidfile_path = optarg;
562 dont_daemonize = true;
566 debug = atoi(optarg);
569 maxproc = atoi(optarg);
572 timeout = atoi(optarg);
585 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
588 log_errx(1, "daemon already running, pid: %jd.",
590 log_err(1, "cannot open or create pidfile \"%s\"",
594 iscsi_fd = open(ISCSI_PATH, O_RDWR);
595 if (iscsi_fd < 0 && errno == ENOENT) {
597 retval = kldload("iscsi");
599 iscsi_fd = open(ISCSI_PATH, O_RDWR);
604 log_err(1, "failed to open %s", ISCSI_PATH);
606 if (dont_daemonize == false) {
607 if (daemon(0, 0) == -1) {
608 log_warn("cannot daemonize");
609 pidfile_remove(pidfh);
614 pidfile_write(pidfh);
619 log_debugx("waiting for request from the kernel");
621 memset(&request, 0, sizeof(request));
622 error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
624 if (errno == EINTR) {
625 nchildren -= wait_for_children(false);
626 assert(nchildren >= 0);
630 log_err(1, "ISCSIDWAIT");
633 if (dont_daemonize) {
634 log_debugx("not forking due to -d flag; "
635 "will exit after servicing a single request");
637 nchildren -= wait_for_children(false);
638 assert(nchildren >= 0);
640 while (maxproc > 0 && nchildren >= maxproc) {
641 log_debugx("maxproc limit of %d child processes hit; "
642 "waiting for child process to exit", maxproc);
643 nchildren -= wait_for_children(true);
644 assert(nchildren >= 0);
646 log_debugx("incoming connection; forking child process #%d",
657 pidfile_close(pidfh);
658 handle_request(iscsi_fd, &request, timeout);