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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/linker.h>
39 #include <sys/socket.h>
40 #include <sys/capsicum.h>
58 static volatile bool sigalrm_received = false;
60 static int nchildren = 0;
66 fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
71 checked_strdup(const char *s)
82 resolve_addr(const struct connection *conn, const char *address,
83 struct addrinfo **ai, bool initiator_side)
85 struct addrinfo hints;
86 char *arg, *addr, *ch;
88 int error, colons = 0;
90 arg = checked_strdup(address);
93 fail(conn, "empty address");
94 log_errx(1, "empty address");
98 * IPv6 address in square brackets, perhaps with port.
101 addr = strsep(&arg, "]");
103 fail(conn, "malformed address");
104 log_errx(1, "malformed address %s", address);
106 if (arg[0] == '\0') {
108 } else if (arg[0] == ':') {
111 fail(conn, "malformed address");
112 log_errx(1, "malformed address %s", address);
116 * Either IPv6 address without brackets - and without
117 * a port - or IPv4 address. Just count the colons.
119 for (ch = arg; *ch != '\0'; ch++) {
127 addr = strsep(&arg, ":");
135 if (port == NULL && !initiator_side)
138 memset(&hints, 0, sizeof(hints));
139 hints.ai_family = PF_UNSPEC;
140 hints.ai_socktype = SOCK_STREAM;
141 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
143 hints.ai_flags |= AI_PASSIVE;
145 error = getaddrinfo(addr, port, &hints, ai);
147 fail(conn, gai_strerror(error));
148 log_errx(1, "getaddrinfo for %s failed: %s",
149 address, gai_strerror(error));
153 static struct connection *
154 connection_new(unsigned int session_id, const uint8_t isid[8], uint16_t tsih,
155 const struct iscsi_session_conf *conf, int iscsi_fd)
157 struct connection *conn;
158 struct addrinfo *from_ai, *to_ai;
159 const char *from_addr, *to_addr;
160 #ifdef ICL_KERNEL_PROXY
161 struct iscsi_daemon_connect idc;
165 conn = calloc(1, sizeof(*conn));
167 log_err(1, "calloc");
170 * Default values, from RFC 3720, section 12.
172 conn->conn_header_digest = CONN_DIGEST_NONE;
173 conn->conn_data_digest = CONN_DIGEST_NONE;
174 conn->conn_initial_r2t = true;
175 conn->conn_immediate_data = true;
176 conn->conn_max_data_segment_length = 8192;
177 conn->conn_max_burst_length = 262144;
178 conn->conn_first_burst_length = 65536;
180 conn->conn_session_id = session_id;
181 memcpy(&conn->conn_isid, isid, sizeof(conn->conn_isid));
182 conn->conn_tsih = tsih;
183 conn->conn_iscsi_fd = iscsi_fd;
186 * XXX: Should we sanitize this somehow?
188 memcpy(&conn->conn_conf, conf, sizeof(conn->conn_conf));
190 from_addr = conn->conn_conf.isc_initiator_addr;
191 to_addr = conn->conn_conf.isc_target_addr;
193 if (from_addr[0] != '\0')
194 resolve_addr(conn, from_addr, &from_ai, true);
198 resolve_addr(conn, to_addr, &to_ai, false);
200 #ifdef ICL_KERNEL_PROXY
201 if (conn->conn_conf.isc_iser) {
202 memset(&idc, 0, sizeof(idc));
203 idc.idc_session_id = conn->conn_session_id;
204 if (conn->conn_conf.isc_iser)
206 idc.idc_domain = to_ai->ai_family;
207 idc.idc_socktype = to_ai->ai_socktype;
208 idc.idc_protocol = to_ai->ai_protocol;
209 if (from_ai != NULL) {
210 idc.idc_from_addr = from_ai->ai_addr;
211 idc.idc_from_addrlen = from_ai->ai_addrlen;
213 idc.idc_to_addr = to_ai->ai_addr;
214 idc.idc_to_addrlen = to_ai->ai_addrlen;
216 log_debugx("connecting to %s using ICL kernel proxy", to_addr);
217 error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
219 fail(conn, strerror(errno));
220 log_err(1, "failed to connect to %s "
221 "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
226 #endif /* ICL_KERNEL_PROXY */
228 if (conn->conn_conf.isc_iser) {
229 fail(conn, "iSER not supported");
230 log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
231 "does not support iSER");
234 conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
236 if (conn->conn_socket < 0) {
237 fail(conn, strerror(errno));
238 log_err(1, "failed to create socket for %s", from_addr);
240 sockbuf = SOCKBUF_SIZE;
241 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF,
242 &sockbuf, sizeof(sockbuf)) == -1)
243 log_warn("setsockopt(SO_RCVBUF) failed");
244 sockbuf = SOCKBUF_SIZE;
245 if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF,
246 &sockbuf, sizeof(sockbuf)) == -1)
247 log_warn("setsockopt(SO_SNDBUF) failed");
248 if (from_ai != NULL) {
249 error = bind(conn->conn_socket, from_ai->ai_addr,
250 from_ai->ai_addrlen);
252 fail(conn, strerror(errno));
253 log_err(1, "failed to bind to %s", from_addr);
256 log_debugx("connecting to %s", to_addr);
257 error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen);
259 fail(conn, strerror(errno));
260 log_err(1, "failed to connect to %s", to_addr);
267 handoff(struct connection *conn)
269 struct iscsi_daemon_handoff idh;
272 log_debugx("handing off connection to the kernel");
274 memset(&idh, 0, sizeof(idh));
275 idh.idh_session_id = conn->conn_session_id;
276 idh.idh_socket = conn->conn_socket;
277 strlcpy(idh.idh_target_alias, conn->conn_target_alias,
278 sizeof(idh.idh_target_alias));
279 idh.idh_tsih = conn->conn_tsih;
280 idh.idh_statsn = conn->conn_statsn;
281 idh.idh_header_digest = conn->conn_header_digest;
282 idh.idh_data_digest = conn->conn_data_digest;
283 idh.idh_initial_r2t = conn->conn_initial_r2t;
284 idh.idh_immediate_data = conn->conn_immediate_data;
285 idh.idh_max_data_segment_length = conn->conn_max_data_segment_length;
286 idh.idh_max_burst_length = conn->conn_max_burst_length;
287 idh.idh_first_burst_length = conn->conn_first_burst_length;
289 error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
291 log_err(1, "ISCSIDHANDOFF");
295 fail(const struct connection *conn, const char *reason)
297 struct iscsi_daemon_fail idf;
300 memset(&idf, 0, sizeof(idf));
301 idf.idf_session_id = conn->conn_session_id;
302 strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
304 error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
306 log_err(1, "ISCSIDFAIL");
310 * XXX: I CANT INTO LATIN
313 capsicate(struct connection *conn)
317 #ifdef ICL_KERNEL_PROXY
318 const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE,
319 ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY };
321 const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD,
322 ISCSISREMOVE, ISCSISMODIFY };
325 cap_rights_init(&rights, CAP_IOCTL);
326 error = cap_rights_limit(conn->conn_iscsi_fd, &rights);
327 if (error != 0 && errno != ENOSYS)
328 log_err(1, "cap_rights_limit");
330 error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds,
331 sizeof(cmds) / sizeof(cmds[0]));
332 if (error != 0 && errno != ENOSYS)
333 log_err(1, "cap_ioctls_limit");
336 if (error != 0 && errno != ENOSYS)
337 log_err(1, "cap_enter");
340 log_debugx("Capsicum capability mode enabled");
342 log_warnx("Capsicum capability mode not supported");
349 return (sigalrm_received);
353 sigalrm_handler(int dummy __unused)
356 * It would be easiest to just log an error and exit. We can't
357 * do this, though, because log_errx() is not signal safe, since
358 * it calls syslog(3). Instead, set a flag checked by pdu_send()
359 * and pdu_receive(), to call log_errx() there. Should they fail
360 * to notice, we'll exit here one second later.
362 if (sigalrm_received) {
364 * Oh well. Just give up and quit.
369 sigalrm_received = true;
373 set_timeout(int timeout)
376 struct itimerval itv;
380 log_debugx("session timeout disabled");
384 bzero(&sa, sizeof(sa));
385 sa.sa_handler = sigalrm_handler;
386 sigfillset(&sa.sa_mask);
387 error = sigaction(SIGALRM, &sa, NULL);
389 log_err(1, "sigaction");
392 * First SIGALRM will arive after conf_timeout seconds.
393 * If we do nothing, another one will arrive a second later.
395 bzero(&itv, sizeof(itv));
396 itv.it_interval.tv_sec = 1;
397 itv.it_value.tv_sec = timeout;
399 log_debugx("setting session timeout to %d seconds",
401 error = setitimer(ITIMER_REAL, &itv, NULL);
403 log_err(1, "setitimer");
407 sigchld_handler(int dummy __unused)
411 * The only purpose of this handler is to make SIGCHLD
412 * interrupt the ISCSIDWAIT ioctl(2), so we can call
413 * wait_for_children().
418 register_sigchld(void)
423 bzero(&sa, sizeof(sa));
424 sa.sa_handler = sigchld_handler;
425 sigfillset(&sa.sa_mask);
426 error = sigaction(SIGCHLD, &sa, NULL);
428 log_err(1, "sigaction");
433 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
435 struct connection *conn;
437 log_set_peer_addr(request->idr_conf.isc_target_addr);
438 if (request->idr_conf.isc_target[0] != '\0') {
439 log_set_peer_name(request->idr_conf.isc_target);
440 setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
442 setproctitle("%s", request->idr_conf.isc_target_addr);
445 conn = connection_new(request->idr_session_id, request->idr_isid,
446 request->idr_tsih, &request->idr_conf, iscsi_fd);
447 set_timeout(timeout);
450 if (conn->conn_conf.isc_discovery != 0)
455 log_debugx("nothing more to do; exiting");
460 wait_for_children(bool block)
468 * If "block" is true, wait for at least one process.
470 if (block && num == 0)
471 pid = wait4(-1, &status, 0, NULL);
473 pid = wait4(-1, &status, WNOHANG, NULL);
476 if (WIFSIGNALED(status)) {
477 log_warnx("child process %d terminated with signal %d",
478 pid, WTERMSIG(status));
479 } else if (WEXITSTATUS(status) != 0) {
480 log_warnx("child process %d terminated with exit status %d",
481 pid, WEXITSTATUS(status));
483 log_debugx("child process %d terminated gracefully", pid);
492 main(int argc, char **argv)
494 int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
496 bool dont_daemonize = false;
499 const char *pidfile_path = DEFAULT_PIDFILE;
500 struct iscsi_daemon_request request;
502 while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
505 pidfile_path = optarg;
508 dont_daemonize = true;
512 debug = atoi(optarg);
515 maxproc = atoi(optarg);
518 timeout = atoi(optarg);
531 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
534 log_errx(1, "daemon already running, pid: %jd.",
536 log_err(1, "cannot open or create pidfile \"%s\"",
540 iscsi_fd = open(ISCSI_PATH, O_RDWR);
541 if (iscsi_fd < 0 && errno == ENOENT) {
543 retval = kldload("iscsi");
545 iscsi_fd = open(ISCSI_PATH, O_RDWR);
550 log_err(1, "failed to open %s", ISCSI_PATH);
552 if (dont_daemonize == false) {
553 if (daemon(0, 0) == -1) {
554 log_warn("cannot daemonize");
555 pidfile_remove(pidfh);
560 pidfile_write(pidfh);
565 log_debugx("waiting for request from the kernel");
567 memset(&request, 0, sizeof(request));
568 error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
570 if (errno == EINTR) {
571 nchildren -= wait_for_children(false);
572 assert(nchildren >= 0);
576 log_err(1, "ISCSIDWAIT");
579 if (dont_daemonize) {
580 log_debugx("not forking due to -d flag; "
581 "will exit after servicing a single request");
583 nchildren -= wait_for_children(false);
584 assert(nchildren >= 0);
586 while (maxproc > 0 && nchildren >= maxproc) {
587 log_debugx("maxproc limit of %d child processes hit; "
588 "waiting for child process to exit", maxproc);
589 nchildren -= wait_for_children(true);
590 assert(nchildren >= 0);
592 log_debugx("incoming connection; forking child process #%d",
603 pidfile_close(pidfh);
604 handle_request(iscsi_fd, &request, timeout);