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