2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * 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/capability.h>
56 static volatile bool sigalrm_received = false;
58 static int nchildren = 0;
64 fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
69 checked_strdup(const char *s)
80 resolve_addr(const struct connection *conn, const char *address,
81 struct addrinfo **ai, bool initiator_side)
83 struct addrinfo hints;
84 char *arg, *addr, *ch;
86 int error, colons = 0;
88 arg = checked_strdup(address);
91 fail(conn, "empty address");
92 log_errx(1, "empty address");
96 * IPv6 address in square brackets, perhaps with port.
99 addr = strsep(&arg, "]");
101 fail(conn, "malformed address");
102 log_errx(1, "malformed address %s", address);
104 if (arg[0] == '\0') {
106 } else if (arg[0] == ':') {
109 fail(conn, "malformed address");
110 log_errx(1, "malformed address %s", address);
114 * Either IPv6 address without brackets - and without
115 * a port - or IPv4 address. Just count the colons.
117 for (ch = arg; *ch != '\0'; ch++) {
125 addr = strsep(&arg, ":");
133 if (port == NULL && !initiator_side)
136 memset(&hints, 0, sizeof(hints));
137 hints.ai_family = PF_UNSPEC;
138 hints.ai_socktype = SOCK_STREAM;
139 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
141 hints.ai_flags |= AI_PASSIVE;
143 error = getaddrinfo(addr, port, &hints, ai);
145 fail(conn, gai_strerror(error));
146 log_errx(1, "getaddrinfo for %s failed: %s",
147 address, gai_strerror(error));
151 static struct connection *
152 connection_new(unsigned int session_id, const uint8_t isid[8], uint16_t tsih,
153 const struct iscsi_session_conf *conf, int iscsi_fd)
155 struct connection *conn;
156 struct addrinfo *from_ai, *to_ai;
157 const char *from_addr, *to_addr;
158 #ifdef ICL_KERNEL_PROXY
159 struct iscsi_daemon_connect idc;
163 conn = calloc(1, sizeof(*conn));
165 log_err(1, "calloc");
168 * Default values, from RFC 3720, section 12.
170 conn->conn_header_digest = CONN_DIGEST_NONE;
171 conn->conn_data_digest = CONN_DIGEST_NONE;
172 conn->conn_initial_r2t = true;
173 conn->conn_immediate_data = true;
174 conn->conn_max_data_segment_length = 8192;
175 conn->conn_max_burst_length = 262144;
176 conn->conn_first_burst_length = 65536;
178 conn->conn_session_id = session_id;
179 memcpy(&conn->conn_isid, isid, sizeof(conn->conn_isid));
180 conn->conn_tsih = tsih;
181 conn->conn_iscsi_fd = iscsi_fd;
184 * XXX: Should we sanitize this somehow?
186 memcpy(&conn->conn_conf, conf, sizeof(conn->conn_conf));
188 from_addr = conn->conn_conf.isc_initiator_addr;
189 to_addr = conn->conn_conf.isc_target_addr;
191 if (from_addr[0] != '\0')
192 resolve_addr(conn, from_addr, &from_ai, true);
196 resolve_addr(conn, to_addr, &to_ai, false);
198 #ifdef ICL_KERNEL_PROXY
199 if (conn->conn_conf.isc_iser) {
200 memset(&idc, 0, sizeof(idc));
201 idc.idc_session_id = conn->conn_session_id;
202 if (conn->conn_conf.isc_iser)
204 idc.idc_domain = to_ai->ai_family;
205 idc.idc_socktype = to_ai->ai_socktype;
206 idc.idc_protocol = to_ai->ai_protocol;
207 if (from_ai != NULL) {
208 idc.idc_from_addr = from_ai->ai_addr;
209 idc.idc_from_addrlen = from_ai->ai_addrlen;
211 idc.idc_to_addr = to_ai->ai_addr;
212 idc.idc_to_addrlen = to_ai->ai_addrlen;
214 log_debugx("connecting to %s using ICL kernel proxy", to_addr);
215 error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
217 fail(conn, strerror(errno));
218 log_err(1, "failed to connect to %s "
219 "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
224 #endif /* ICL_KERNEL_PROXY */
226 if (conn->conn_conf.isc_iser) {
227 fail(conn, "iSER not supported");
228 log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
229 "does not support iSER");
232 conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
234 if (conn->conn_socket < 0) {
235 fail(conn, strerror(errno));
236 log_err(1, "failed to create socket for %s", from_addr);
238 if (from_ai != NULL) {
239 error = bind(conn->conn_socket, from_ai->ai_addr,
240 from_ai->ai_addrlen);
242 fail(conn, strerror(errno));
243 log_err(1, "failed to bind to %s", from_addr);
246 log_debugx("connecting to %s", to_addr);
247 error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen);
249 fail(conn, strerror(errno));
250 log_err(1, "failed to connect to %s", to_addr);
257 handoff(struct connection *conn)
259 struct iscsi_daemon_handoff idh;
262 log_debugx("handing off connection to the kernel");
264 memset(&idh, 0, sizeof(idh));
265 idh.idh_session_id = conn->conn_session_id;
266 idh.idh_socket = conn->conn_socket;
267 strlcpy(idh.idh_target_alias, conn->conn_target_alias,
268 sizeof(idh.idh_target_alias));
269 idh.idh_tsih = conn->conn_tsih;
270 idh.idh_statsn = conn->conn_statsn;
271 idh.idh_header_digest = conn->conn_header_digest;
272 idh.idh_data_digest = conn->conn_data_digest;
273 idh.idh_initial_r2t = conn->conn_initial_r2t;
274 idh.idh_immediate_data = conn->conn_immediate_data;
275 idh.idh_max_data_segment_length = conn->conn_max_data_segment_length;
276 idh.idh_max_burst_length = conn->conn_max_burst_length;
277 idh.idh_first_burst_length = conn->conn_first_burst_length;
279 error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
281 log_err(1, "ISCSIDHANDOFF");
285 fail(const struct connection *conn, const char *reason)
287 struct iscsi_daemon_fail idf;
290 memset(&idf, 0, sizeof(idf));
291 idf.idf_session_id = conn->conn_session_id;
292 strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
294 error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
296 log_err(1, "ISCSIDFAIL");
300 * XXX: I CANT INTO LATIN
303 capsicate(struct connection *conn)
307 #ifdef ICL_KERNEL_PROXY
308 const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE,
309 ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY };
311 const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD,
312 ISCSISREMOVE, ISCSISMODIFY };
315 cap_rights_init(&rights, CAP_IOCTL);
316 error = cap_rights_limit(conn->conn_iscsi_fd, &rights);
317 if (error != 0 && errno != ENOSYS)
318 log_err(1, "cap_rights_limit");
320 error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds,
321 sizeof(cmds) / sizeof(cmds[0]));
322 if (error != 0 && errno != ENOSYS)
323 log_err(1, "cap_ioctls_limit");
326 if (error != 0 && errno != ENOSYS)
327 log_err(1, "cap_enter");
330 log_debugx("Capsicum capability mode enabled");
332 log_warnx("Capsicum capability mode not supported");
339 return (sigalrm_received);
343 sigalrm_handler(int dummy __unused)
346 * It would be easiest to just log an error and exit. We can't
347 * do this, though, because log_errx() is not signal safe, since
348 * it calls syslog(3). Instead, set a flag checked by pdu_send()
349 * and pdu_receive(), to call log_errx() there. Should they fail
350 * to notice, we'll exit here one second later.
352 if (sigalrm_received) {
354 * Oh well. Just give up and quit.
359 sigalrm_received = true;
363 set_timeout(int timeout)
366 struct itimerval itv;
370 log_debugx("session timeout disabled");
374 bzero(&sa, sizeof(sa));
375 sa.sa_handler = sigalrm_handler;
376 sigfillset(&sa.sa_mask);
377 error = sigaction(SIGALRM, &sa, NULL);
379 log_err(1, "sigaction");
382 * First SIGALRM will arive after conf_timeout seconds.
383 * If we do nothing, another one will arrive a second later.
385 bzero(&itv, sizeof(itv));
386 itv.it_interval.tv_sec = 1;
387 itv.it_value.tv_sec = timeout;
389 log_debugx("setting session timeout to %d seconds",
391 error = setitimer(ITIMER_REAL, &itv, NULL);
393 log_err(1, "setitimer");
397 sigchld_handler(int dummy __unused)
401 * The only purpose of this handler is to make SIGCHLD
402 * interrupt the ISCSIDWAIT ioctl(2), so we can call
403 * wait_for_children().
408 register_sigchld(void)
413 bzero(&sa, sizeof(sa));
414 sa.sa_handler = sigchld_handler;
415 sigfillset(&sa.sa_mask);
416 error = sigaction(SIGCHLD, &sa, NULL);
418 log_err(1, "sigaction");
423 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
425 struct connection *conn;
427 log_set_peer_addr(request->idr_conf.isc_target_addr);
428 if (request->idr_conf.isc_target[0] != '\0') {
429 log_set_peer_name(request->idr_conf.isc_target);
430 setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
432 setproctitle("%s", request->idr_conf.isc_target_addr);
435 conn = connection_new(request->idr_session_id, request->idr_isid,
436 request->idr_tsih, &request->idr_conf, iscsi_fd);
437 set_timeout(timeout);
440 if (conn->conn_conf.isc_discovery != 0)
445 log_debugx("nothing more to do; exiting");
450 wait_for_children(bool block)
458 * If "block" is true, wait for at least one process.
460 if (block && num == 0)
461 pid = wait4(-1, &status, 0, NULL);
463 pid = wait4(-1, &status, WNOHANG, NULL);
466 if (WIFSIGNALED(status)) {
467 log_warnx("child process %d terminated with signal %d",
468 pid, WTERMSIG(status));
469 } else if (WEXITSTATUS(status) != 0) {
470 log_warnx("child process %d terminated with exit status %d",
471 pid, WEXITSTATUS(status));
473 log_debugx("child process %d terminated gracefully", pid);
482 main(int argc, char **argv)
484 int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
486 bool dont_daemonize = false;
489 const char *pidfile_path = DEFAULT_PIDFILE;
490 struct iscsi_daemon_request request;
492 while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
495 pidfile_path = optarg;
498 dont_daemonize = true;
502 debug = atoi(optarg);
505 maxproc = atoi(optarg);
508 timeout = atoi(optarg);
521 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
524 log_errx(1, "daemon already running, pid: %jd.",
526 log_err(1, "cannot open or create pidfile \"%s\"",
530 iscsi_fd = open(ISCSI_PATH, O_RDWR);
531 if (iscsi_fd < 0 && errno == ENOENT) {
533 retval = kldload("iscsi");
535 iscsi_fd = open(ISCSI_PATH, O_RDWR);
540 log_err(1, "failed to open %s", ISCSI_PATH);
542 if (dont_daemonize == false) {
543 if (daemon(0, 0) == -1) {
544 log_warn("cannot daemonize");
545 pidfile_remove(pidfh);
550 pidfile_write(pidfh);
555 log_debugx("waiting for request from the kernel");
557 memset(&request, 0, sizeof(request));
558 error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
560 if (errno == EINTR) {
561 nchildren -= wait_for_children(false);
562 assert(nchildren >= 0);
566 log_err(1, "ISCSIDWAIT");
569 if (dont_daemonize) {
570 log_debugx("not forking due to -d flag; "
571 "will exit after servicing a single request");
573 nchildren -= wait_for_children(false);
574 assert(nchildren >= 0);
576 while (maxproc > 0 && nchildren >= maxproc) {
577 log_debugx("maxproc limit of %d child processes hit; "
578 "waiting for child process to exit", maxproc);
579 nchildren -= wait_for_children(true);
580 assert(nchildren >= 0);
582 log_debugx("incoming connection; forking child process #%d",
593 pidfile_close(pidfh);
594 handle_request(iscsi_fd, &request, timeout);