1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 *
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/types.h>
37 #include <sys/time.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>
43 #include <sys/wait.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <libutil.h>
48 #include <netdb.h>
49 #include <signal.h>
50 #include <stdbool.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include "iscsid.h"
58
59 static volatile bool sigalrm_received = false;
60
61 static int nchildren = 0;
62
63 static void
usage(void)64 usage(void)
65 {
66
67 fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
68 exit(1);
69 }
70
71 char *
checked_strdup(const char * s)72 checked_strdup(const char *s)
73 {
74 char *c;
75
76 c = strdup(s);
77 if (c == NULL)
78 log_err(1, "strdup");
79 return (c);
80 }
81
82 static void
resolve_addr(const struct connection * conn,const char * address,struct addrinfo ** ai,bool initiator_side)83 resolve_addr(const struct connection *conn, const char *address,
84 struct addrinfo **ai, bool initiator_side)
85 {
86 struct addrinfo hints;
87 char *arg, *addr, *ch;
88 const char *port;
89 int error, colons = 0;
90
91 arg = checked_strdup(address);
92
93 if (arg[0] == '\0') {
94 fail(conn, "empty address");
95 log_errx(1, "empty address");
96 }
97 if (arg[0] == '[') {
98 /*
99 * IPv6 address in square brackets, perhaps with port.
100 */
101 arg++;
102 addr = strsep(&arg, "]");
103 if (arg == NULL) {
104 fail(conn, "malformed address");
105 log_errx(1, "malformed address %s", address);
106 }
107 if (arg[0] == '\0') {
108 port = NULL;
109 } else if (arg[0] == ':') {
110 port = arg + 1;
111 } else {
112 fail(conn, "malformed address");
113 log_errx(1, "malformed address %s", address);
114 }
115 } else {
116 /*
117 * Either IPv6 address without brackets - and without
118 * a port - or IPv4 address. Just count the colons.
119 */
120 for (ch = arg; *ch != '\0'; ch++) {
121 if (*ch == ':')
122 colons++;
123 }
124 if (colons > 1) {
125 addr = arg;
126 port = NULL;
127 } else {
128 addr = strsep(&arg, ":");
129 if (arg == NULL)
130 port = NULL;
131 else
132 port = arg;
133 }
134 }
135
136 if (port == NULL && !initiator_side)
137 port = "3260";
138
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;
143 if (initiator_side)
144 hints.ai_flags |= AI_PASSIVE;
145
146 error = getaddrinfo(addr, port, &hints, ai);
147 if (error != 0) {
148 fail(conn, gai_strerror(error));
149 log_errx(1, "getaddrinfo for %s failed: %s",
150 address, gai_strerror(error));
151 }
152 }
153
154 static struct connection *
connection_new(int iscsi_fd,const struct iscsi_daemon_request * request)155 connection_new(int iscsi_fd, const struct iscsi_daemon_request *request)
156 {
157 struct connection *conn;
158 struct iscsi_session_limits *isl;
159 struct addrinfo *from_ai, *to_ai;
160 const char *from_addr, *to_addr;
161 #ifdef ICL_KERNEL_PROXY
162 struct iscsi_daemon_connect idc;
163 #endif
164 int error, sockbuf;
165
166 conn = calloc(1, sizeof(*conn));
167 if (conn == NULL)
168 log_err(1, "calloc");
169
170 /*
171 * Default values, from RFC 3720, section 12.
172 */
173 conn->conn_header_digest = CONN_DIGEST_NONE;
174 conn->conn_data_digest = CONN_DIGEST_NONE;
175 conn->conn_initial_r2t = true;
176 conn->conn_immediate_data = true;
177 conn->conn_max_recv_data_segment_length = 8192;
178 conn->conn_max_send_data_segment_length = 8192;
179 conn->conn_max_burst_length = 262144;
180 conn->conn_first_burst_length = 65536;
181 conn->conn_iscsi_fd = iscsi_fd;
182
183 conn->conn_session_id = request->idr_session_id;
184 memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf));
185 memcpy(&conn->conn_isid, &request->idr_isid, sizeof(conn->conn_isid));
186 conn->conn_tsih = request->idr_tsih;
187
188 /*
189 * Read the driver limits and provide reasonable defaults for the ones
190 * the driver doesn't care about. If a max_snd_dsl is not explicitly
191 * provided by the driver then we'll make sure both conn->max_snd_dsl
192 * and isl->max_snd_dsl are set to the rcv_dsl. This preserves historic
193 * behavior.
194 */
195 isl = &conn->conn_limits;
196 memcpy(isl, &request->idr_limits, sizeof(*isl));
197 if (isl->isl_max_recv_data_segment_length == 0)
198 isl->isl_max_recv_data_segment_length = (1 << 24) - 1;
199 if (isl->isl_max_send_data_segment_length == 0)
200 isl->isl_max_send_data_segment_length =
201 isl->isl_max_recv_data_segment_length;
202 if (isl->isl_max_burst_length == 0)
203 isl->isl_max_burst_length = (1 << 24) - 1;
204 if (isl->isl_first_burst_length == 0)
205 isl->isl_first_burst_length = (1 << 24) - 1;
206 if (isl->isl_first_burst_length > isl->isl_max_burst_length)
207 isl->isl_first_burst_length = isl->isl_max_burst_length;
208
209 /*
210 * Limit default send length in case it won't be negotiated.
211 * We can't do it for other limits, since they may affect both
212 * sender and receiver operation, and we must obey defaults.
213 */
214 if (conn->conn_max_send_data_segment_length >
215 isl->isl_max_send_data_segment_length) {
216 conn->conn_max_send_data_segment_length =
217 isl->isl_max_send_data_segment_length;
218 }
219
220 from_addr = conn->conn_conf.isc_initiator_addr;
221 to_addr = conn->conn_conf.isc_target_addr;
222
223 if (from_addr[0] != '\0')
224 resolve_addr(conn, from_addr, &from_ai, true);
225 else
226 from_ai = NULL;
227
228 resolve_addr(conn, to_addr, &to_ai, false);
229
230 #ifdef ICL_KERNEL_PROXY
231 if (conn->conn_conf.isc_iser) {
232 memset(&idc, 0, sizeof(idc));
233 idc.idc_session_id = conn->conn_session_id;
234 if (conn->conn_conf.isc_iser)
235 idc.idc_iser = 1;
236 idc.idc_domain = to_ai->ai_family;
237 idc.idc_socktype = to_ai->ai_socktype;
238 idc.idc_protocol = to_ai->ai_protocol;
239 if (from_ai != NULL) {
240 idc.idc_from_addr = from_ai->ai_addr;
241 idc.idc_from_addrlen = from_ai->ai_addrlen;
242 }
243 idc.idc_to_addr = to_ai->ai_addr;
244 idc.idc_to_addrlen = to_ai->ai_addrlen;
245
246 log_debugx("connecting to %s using ICL kernel proxy", to_addr);
247 error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
248 if (error != 0) {
249 fail(conn, strerror(errno));
250 log_err(1, "failed to connect to %s "
251 "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
252 }
253
254 return (conn);
255 }
256 #endif /* ICL_KERNEL_PROXY */
257
258 if (conn->conn_conf.isc_iser) {
259 fail(conn, "iSER not supported");
260 log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
261 "does not support iSER");
262 }
263
264 conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
265 to_ai->ai_protocol);
266 if (conn->conn_socket < 0) {
267 fail(conn, strerror(errno));
268 log_err(1, "failed to create socket for %s", from_addr);
269 }
270 sockbuf = SOCKBUF_SIZE;
271 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF,
272 &sockbuf, sizeof(sockbuf)) == -1)
273 log_warn("setsockopt(SO_RCVBUF) failed");
274 sockbuf = SOCKBUF_SIZE;
275 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF,
276 &sockbuf, sizeof(sockbuf)) == -1)
277 log_warn("setsockopt(SO_SNDBUF) failed");
278 if (from_ai != NULL) {
279 error = bind(conn->conn_socket, from_ai->ai_addr,
280 from_ai->ai_addrlen);
281 if (error != 0) {
282 fail(conn, strerror(errno));
283 log_err(1, "failed to bind to %s", from_addr);
284 }
285 }
286 log_debugx("connecting to %s", to_addr);
287 error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen);
288 if (error != 0) {
289 fail(conn, strerror(errno));
290 log_err(1, "failed to connect to %s", to_addr);
291 }
292
293 return (conn);
294 }
295
296 static void
handoff(struct connection * conn)297 handoff(struct connection *conn)
298 {
299 struct iscsi_daemon_handoff idh;
300 int error;
301
302 log_debugx("handing off connection to the kernel");
303
304 memset(&idh, 0, sizeof(idh));
305 idh.idh_session_id = conn->conn_session_id;
306 idh.idh_socket = conn->conn_socket;
307 strlcpy(idh.idh_target_alias, conn->conn_target_alias,
308 sizeof(idh.idh_target_alias));
309 idh.idh_tsih = conn->conn_tsih;
310 idh.idh_statsn = conn->conn_statsn;
311 idh.idh_header_digest = conn->conn_header_digest;
312 idh.idh_data_digest = conn->conn_data_digest;
313 idh.idh_initial_r2t = conn->conn_initial_r2t;
314 idh.idh_immediate_data = conn->conn_immediate_data;
315 idh.idh_max_recv_data_segment_length =
316 conn->conn_max_recv_data_segment_length;
317 idh.idh_max_send_data_segment_length =
318 conn->conn_max_send_data_segment_length;
319 idh.idh_max_burst_length = conn->conn_max_burst_length;
320 idh.idh_first_burst_length = conn->conn_first_burst_length;
321
322 error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
323 if (error != 0)
324 log_err(1, "ISCSIDHANDOFF");
325 }
326
327 void
fail(const struct connection * conn,const char * reason)328 fail(const struct connection *conn, const char *reason)
329 {
330 struct iscsi_daemon_fail idf;
331 int error, saved_errno;
332
333 saved_errno = errno;
334
335 memset(&idf, 0, sizeof(idf));
336 idf.idf_session_id = conn->conn_session_id;
337 strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
338
339 error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
340 if (error != 0)
341 log_err(1, "ISCSIDFAIL");
342
343 errno = saved_errno;
344 }
345
346 /*
347 * XXX: I CANT INTO LATIN
348 */
349 static void
capsicate(struct connection * conn)350 capsicate(struct connection *conn)
351 {
352 int error;
353 cap_rights_t rights;
354 #ifdef ICL_KERNEL_PROXY
355 const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE,
356 ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY };
357 #else
358 const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD,
359 ISCSISREMOVE, ISCSISMODIFY };
360 #endif
361
362 cap_rights_init(&rights, CAP_IOCTL);
363 error = cap_rights_limit(conn->conn_iscsi_fd, &rights);
364 if (error != 0 && errno != ENOSYS)
365 log_err(1, "cap_rights_limit");
366
367 error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, nitems(cmds));
368
369 if (error != 0 && errno != ENOSYS)
370 log_err(1, "cap_ioctls_limit");
371
372 error = cap_enter();
373 if (error != 0 && errno != ENOSYS)
374 log_err(1, "cap_enter");
375
376 if (cap_sandboxed())
377 log_debugx("Capsicum capability mode enabled");
378 else
379 log_warnx("Capsicum capability mode not supported");
380 }
381
382 bool
timed_out(void)383 timed_out(void)
384 {
385
386 return (sigalrm_received);
387 }
388
389 static void
sigalrm_handler(int dummy __unused)390 sigalrm_handler(int dummy __unused)
391 {
392 /*
393 * It would be easiest to just log an error and exit. We can't
394 * do this, though, because log_errx() is not signal safe, since
395 * it calls syslog(3). Instead, set a flag checked by pdu_send()
396 * and pdu_receive(), to call log_errx() there. Should they fail
397 * to notice, we'll exit here one second later.
398 */
399 if (sigalrm_received) {
400 /*
401 * Oh well. Just give up and quit.
402 */
403 _exit(2);
404 }
405
406 sigalrm_received = true;
407 }
408
409 static void
set_timeout(int timeout)410 set_timeout(int timeout)
411 {
412 struct sigaction sa;
413 struct itimerval itv;
414 int error;
415
416 if (timeout <= 0) {
417 log_debugx("session timeout disabled");
418 return;
419 }
420
421 bzero(&sa, sizeof(sa));
422 sa.sa_handler = sigalrm_handler;
423 sigfillset(&sa.sa_mask);
424 error = sigaction(SIGALRM, &sa, NULL);
425 if (error != 0)
426 log_err(1, "sigaction");
427
428 /*
429 * First SIGALRM will arive after conf_timeout seconds.
430 * If we do nothing, another one will arrive a second later.
431 */
432 bzero(&itv, sizeof(itv));
433 itv.it_interval.tv_sec = 1;
434 itv.it_value.tv_sec = timeout;
435
436 log_debugx("setting session timeout to %d seconds",
437 timeout);
438 error = setitimer(ITIMER_REAL, &itv, NULL);
439 if (error != 0)
440 log_err(1, "setitimer");
441 }
442
443 static void
sigchld_handler(int dummy __unused)444 sigchld_handler(int dummy __unused)
445 {
446
447 /*
448 * The only purpose of this handler is to make SIGCHLD
449 * interrupt the ISCSIDWAIT ioctl(2), so we can call
450 * wait_for_children().
451 */
452 }
453
454 static void
register_sigchld(void)455 register_sigchld(void)
456 {
457 struct sigaction sa;
458 int error;
459
460 bzero(&sa, sizeof(sa));
461 sa.sa_handler = sigchld_handler;
462 sigfillset(&sa.sa_mask);
463 error = sigaction(SIGCHLD, &sa, NULL);
464 if (error != 0)
465 log_err(1, "sigaction");
466
467 }
468
469 static void
handle_request(int iscsi_fd,const struct iscsi_daemon_request * request,int timeout)470 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
471 {
472 struct connection *conn;
473
474 log_set_peer_addr(request->idr_conf.isc_target_addr);
475 if (request->idr_conf.isc_target[0] != '\0') {
476 log_set_peer_name(request->idr_conf.isc_target);
477 setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
478 } else {
479 setproctitle("%s", request->idr_conf.isc_target_addr);
480 }
481
482 conn = connection_new(iscsi_fd, request);
483 set_timeout(timeout);
484 capsicate(conn);
485 login(conn);
486 if (conn->conn_conf.isc_discovery != 0)
487 discovery(conn);
488 else
489 handoff(conn);
490
491 log_debugx("nothing more to do; exiting");
492 exit (0);
493 }
494
495 static int
wait_for_children(bool block)496 wait_for_children(bool block)
497 {
498 pid_t pid;
499 int status;
500 int num = 0;
501
502 for (;;) {
503 /*
504 * If "block" is true, wait for at least one process.
505 */
506 if (block && num == 0)
507 pid = wait4(-1, &status, 0, NULL);
508 else
509 pid = wait4(-1, &status, WNOHANG, NULL);
510 if (pid <= 0)
511 break;
512 if (WIFSIGNALED(status)) {
513 log_warnx("child process %d terminated with signal %d",
514 pid, WTERMSIG(status));
515 } else if (WEXITSTATUS(status) != 0) {
516 log_warnx("child process %d terminated with exit status %d",
517 pid, WEXITSTATUS(status));
518 } else {
519 log_debugx("child process %d terminated gracefully", pid);
520 }
521 num++;
522 }
523
524 return (num);
525 }
526
527 int
main(int argc,char ** argv)528 main(int argc, char **argv)
529 {
530 int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
531 timeout = 60;
532 bool dont_daemonize = false;
533 struct pidfh *pidfh;
534 pid_t pid, otherpid;
535 const char *pidfile_path = DEFAULT_PIDFILE;
536 struct iscsi_daemon_request request;
537
538 while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
539 switch (ch) {
540 case 'P':
541 pidfile_path = optarg;
542 break;
543 case 'd':
544 dont_daemonize = true;
545 debug++;
546 break;
547 case 'l':
548 debug = atoi(optarg);
549 break;
550 case 'm':
551 maxproc = atoi(optarg);
552 break;
553 case 't':
554 timeout = atoi(optarg);
555 break;
556 case '?':
557 default:
558 usage();
559 }
560 }
561 argc -= optind;
562 if (argc != 0)
563 usage();
564
565 log_init(debug);
566
567 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
568 if (pidfh == NULL) {
569 if (errno == EEXIST)
570 log_errx(1, "daemon already running, pid: %jd.",
571 (intmax_t)otherpid);
572 log_err(1, "cannot open or create pidfile \"%s\"",
573 pidfile_path);
574 }
575
576 iscsi_fd = open(ISCSI_PATH, O_RDWR);
577 if (iscsi_fd < 0 && errno == ENOENT) {
578 saved_errno = errno;
579 retval = kldload("iscsi");
580 if (retval != -1)
581 iscsi_fd = open(ISCSI_PATH, O_RDWR);
582 else
583 errno = saved_errno;
584 }
585 if (iscsi_fd < 0)
586 log_err(1, "failed to open %s", ISCSI_PATH);
587
588 if (dont_daemonize == false) {
589 if (daemon(0, 0) == -1) {
590 log_warn("cannot daemonize");
591 pidfile_remove(pidfh);
592 exit(1);
593 }
594 }
595
596 pidfile_write(pidfh);
597
598 register_sigchld();
599
600 for (;;) {
601 log_debugx("waiting for request from the kernel");
602
603 memset(&request, 0, sizeof(request));
604 error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
605 if (error != 0) {
606 if (errno == EINTR) {
607 nchildren -= wait_for_children(false);
608 assert(nchildren >= 0);
609 continue;
610 }
611
612 log_err(1, "ISCSIDWAIT");
613 }
614
615 if (dont_daemonize) {
616 log_debugx("not forking due to -d flag; "
617 "will exit after servicing a single request");
618 } else {
619 nchildren -= wait_for_children(false);
620 assert(nchildren >= 0);
621
622 while (maxproc > 0 && nchildren >= maxproc) {
623 log_debugx("maxproc limit of %d child processes hit; "
624 "waiting for child process to exit", maxproc);
625 nchildren -= wait_for_children(true);
626 assert(nchildren >= 0);
627 }
628 log_debugx("incoming connection; forking child process #%d",
629 nchildren);
630 nchildren++;
631
632 pid = fork();
633 if (pid < 0)
634 log_err(1, "fork");
635 if (pid > 0)
636 continue;
637 }
638
639 pidfile_close(pidfh);
640 handle_request(iscsi_fd, &request, timeout);
641 }
642
643 return (0);
644 }
645