1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * 6 * This software was developed by Edward Tomasz Napierala under sponsorship 7 * from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #include <sys/types.h> 33 #include <sys/event.h> 34 #include <sys/nv.h> 35 #include <sys/time.h> 36 #include <sys/socket.h> 37 #include <sys/stat.h> 38 #include <sys/wait.h> 39 #include <netinet/in.h> 40 #include <arpa/inet.h> 41 #include <assert.h> 42 #include <ctype.h> 43 #include <errno.h> 44 #include <netdb.h> 45 #include <signal.h> 46 #include <stdbool.h> 47 #include <stdio.h> 48 #include <stdint.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <cam/scsi/scsi_all.h> 53 54 #include "conf.h" 55 #include "ctld.h" 56 #include "isns.h" 57 58 static bool timed_out(void); 59 #ifdef ICL_KERNEL_PROXY 60 static void pdu_receive_proxy(struct pdu *pdu); 61 static void pdu_send_proxy(struct pdu *pdu); 62 #endif /* ICL_KERNEL_PROXY */ 63 static void pdu_fail(const struct connection *conn, const char *reason); 64 65 bool proxy_mode = false; 66 67 static volatile bool sighup_received = false; 68 static volatile bool sigterm_received = false; 69 static volatile bool sigalrm_received = false; 70 71 static int kqfd; 72 static int nchildren = 0; 73 static uint16_t last_portal_group_tag = 0xff; 74 75 static struct connection_ops conn_ops = { 76 .timed_out = timed_out, 77 #ifdef ICL_KERNEL_PROXY 78 .pdu_receive_proxy = pdu_receive_proxy, 79 .pdu_send_proxy = pdu_send_proxy, 80 #else 81 .pdu_receive_proxy = nullptr, 82 .pdu_send_proxy = nullptr, 83 #endif 84 .fail = pdu_fail, 85 }; 86 87 static void 88 usage(void) 89 { 90 91 fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n"); 92 fprintf(stderr, " ctld -t [-u][-f config-file]\n"); 93 exit(1); 94 } 95 96 struct conf * 97 conf_new(void) 98 { 99 struct conf *conf; 100 101 conf = reinterpret_cast<struct conf *>(calloc(1, sizeof(*conf))); 102 if (conf == NULL) 103 log_err(1, "calloc"); 104 TAILQ_INIT(&conf->conf_luns); 105 TAILQ_INIT(&conf->conf_targets); 106 TAILQ_INIT(&conf->conf_auth_groups); 107 TAILQ_INIT(&conf->conf_ports); 108 TAILQ_INIT(&conf->conf_portal_groups); 109 TAILQ_INIT(&conf->conf_isns); 110 111 conf->conf_isns_period = 900; 112 conf->conf_isns_timeout = 5; 113 conf->conf_debug = 0; 114 conf->conf_timeout = 60; 115 conf->conf_maxproc = 30; 116 117 return (conf); 118 } 119 120 void 121 conf_delete(struct conf *conf) 122 { 123 struct lun *lun, *ltmp; 124 struct target *targ, *tmp; 125 struct auth_group *ag, *cagtmp; 126 struct portal_group *pg, *cpgtmp; 127 struct isns *is, *istmp; 128 129 assert(conf->conf_pidfh == NULL); 130 131 TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp) 132 lun_delete(lun); 133 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp) 134 target_delete(targ); 135 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp) 136 auth_group_delete(ag); 137 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp) 138 portal_group_delete(pg); 139 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp) 140 isns_delete(is); 141 assert(TAILQ_EMPTY(&conf->conf_ports)); 142 free(conf->conf_pidfile_path); 143 free(conf); 144 } 145 146 static struct auth * 147 auth_new(struct auth_group *ag) 148 { 149 struct auth *auth; 150 151 auth = reinterpret_cast<struct auth *>(calloc(1, sizeof(*auth))); 152 if (auth == NULL) 153 log_err(1, "calloc"); 154 auth->a_auth_group = ag; 155 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next); 156 return (auth); 157 } 158 159 static void 160 auth_delete(struct auth *auth) 161 { 162 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next); 163 164 free(auth->a_user); 165 free(auth->a_secret); 166 free(auth->a_mutual_user); 167 free(auth->a_mutual_secret); 168 free(auth); 169 } 170 171 const struct auth * 172 auth_find(const struct auth_group *ag, const char *user) 173 { 174 const struct auth *auth; 175 176 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) { 177 if (strcmp(auth->a_user, user) == 0) 178 return (auth); 179 } 180 181 return (NULL); 182 } 183 184 static void 185 auth_check_secret_length(const struct auth_group *ag, const char *user, 186 const char *secret, const char *secret_type) 187 { 188 size_t len; 189 190 len = strlen(secret); 191 if (len > 16) { 192 log_warnx("%s for user \"%s\", %s, is too long; it should be " 193 "at most 16 characters long", secret_type, user, 194 ag->ag_label); 195 } 196 if (len < 12) { 197 log_warnx("%s for user \"%s\", %s, is too short; it should be " 198 "at least 12 characters long", secret_type, user, 199 ag->ag_label); 200 } 201 } 202 203 bool 204 auth_new_chap(struct auth_group *ag, const char *user, 205 const char *secret) 206 { 207 struct auth *auth; 208 209 if (ag->ag_type == AG_TYPE_UNKNOWN) 210 ag->ag_type = AG_TYPE_CHAP; 211 if (ag->ag_type != AG_TYPE_CHAP) { 212 log_warnx("cannot mix \"chap\" authentication with " 213 "other types for %s", ag->ag_label); 214 return (false); 215 } 216 217 auth_check_secret_length(ag, user, secret, "secret"); 218 219 auth = auth_new(ag); 220 auth->a_user = checked_strdup(user); 221 auth->a_secret = checked_strdup(secret); 222 223 return (true); 224 } 225 226 bool 227 auth_new_chap_mutual(struct auth_group *ag, const char *user, 228 const char *secret, const char *user2, const char *secret2) 229 { 230 struct auth *auth; 231 232 if (ag->ag_type == AG_TYPE_UNKNOWN) 233 ag->ag_type = AG_TYPE_CHAP_MUTUAL; 234 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 235 log_warnx("cannot mix \"chap-mutual\" authentication " 236 "with other types for %s", ag->ag_label); 237 return (false); 238 } 239 240 auth_check_secret_length(ag, user, secret, "secret"); 241 auth_check_secret_length(ag, user, secret2, "mutual secret"); 242 243 auth = auth_new(ag); 244 auth->a_user = checked_strdup(user); 245 auth->a_secret = checked_strdup(secret); 246 auth->a_mutual_user = checked_strdup(user2); 247 auth->a_mutual_secret = checked_strdup(secret2); 248 249 return (true); 250 } 251 252 bool 253 auth_name_new(struct auth_group *ag, const char *name) 254 { 255 struct auth_name *an; 256 257 an = reinterpret_cast<struct auth_name *>(calloc(1, sizeof(*an))); 258 if (an == NULL) 259 log_err(1, "calloc"); 260 an->an_auth_group = ag; 261 an->an_initiator_name = checked_strdup(name); 262 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next); 263 return (true); 264 } 265 266 static void 267 auth_name_delete(struct auth_name *an) 268 { 269 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next); 270 271 free(an->an_initiator_name); 272 free(an); 273 } 274 275 bool 276 auth_name_defined(const struct auth_group *ag) 277 { 278 if (TAILQ_EMPTY(&ag->ag_names)) 279 return (false); 280 return (true); 281 } 282 283 const struct auth_name * 284 auth_name_find(const struct auth_group *ag, const char *name) 285 { 286 const struct auth_name *auth_name; 287 288 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) { 289 if (strcmp(auth_name->an_initiator_name, name) == 0) 290 return (auth_name); 291 } 292 293 return (NULL); 294 } 295 296 bool 297 auth_name_check(const struct auth_group *ag, const char *initiator_name) 298 { 299 if (!auth_name_defined(ag)) 300 return (true); 301 302 if (auth_name_find(ag, initiator_name) == NULL) 303 return (false); 304 305 return (true); 306 } 307 308 bool 309 auth_portal_new(struct auth_group *ag, const char *portal) 310 { 311 struct auth_portal *ap; 312 char *net, *mask, *str, *tmp; 313 int len, dm, m; 314 315 ap = reinterpret_cast<struct auth_portal *>(calloc(1, sizeof(*ap))); 316 if (ap == NULL) 317 log_err(1, "calloc"); 318 ap->ap_auth_group = ag; 319 ap->ap_initiator_portal = checked_strdup(portal); 320 mask = str = checked_strdup(portal); 321 net = strsep(&mask, "/"); 322 if (net[0] == '[') { 323 net++; 324 len = strlen(net); 325 if (len < 2) 326 goto error; 327 if (net[len - 1] != ']') 328 goto error; 329 net[len - 1] = 0; 330 } else if (net[0] == '\0') 331 goto error; 332 if (str[0] == '[' || strchr(net, ':') != NULL) { 333 struct sockaddr_in6 *sin6 = 334 (struct sockaddr_in6 *)&ap->ap_sa; 335 336 sin6->sin6_len = sizeof(*sin6); 337 sin6->sin6_family = AF_INET6; 338 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0) 339 goto error; 340 dm = 128; 341 } else { 342 struct sockaddr_in *sin = 343 (struct sockaddr_in *)&ap->ap_sa; 344 345 sin->sin_len = sizeof(*sin); 346 sin->sin_family = AF_INET; 347 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0) 348 goto error; 349 dm = 32; 350 } 351 if (mask != NULL) { 352 if (mask[0] == '\0') 353 goto error; 354 m = strtol(mask, &tmp, 0); 355 if (m < 0 || m > dm || tmp[0] != 0) 356 goto error; 357 } else 358 m = dm; 359 ap->ap_mask = m; 360 free(str); 361 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next); 362 return (true); 363 364 error: 365 free(str); 366 free(ap); 367 log_warnx("incorrect initiator portal \"%s\"", portal); 368 return (false); 369 } 370 371 static void 372 auth_portal_delete(struct auth_portal *ap) 373 { 374 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next); 375 376 free(ap->ap_initiator_portal); 377 free(ap); 378 } 379 380 bool 381 auth_portal_defined(const struct auth_group *ag) 382 { 383 if (TAILQ_EMPTY(&ag->ag_portals)) 384 return (false); 385 return (true); 386 } 387 388 const struct auth_portal * 389 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss) 390 { 391 const struct auth_portal *ap; 392 const uint8_t *a, *b; 393 int i; 394 uint8_t bmask; 395 396 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) { 397 if (ap->ap_sa.ss_family != ss->ss_family) 398 continue; 399 if (ss->ss_family == AF_INET) { 400 a = (const uint8_t *) 401 &((const struct sockaddr_in *)ss)->sin_addr; 402 b = (const uint8_t *) 403 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr; 404 } else { 405 a = (const uint8_t *) 406 &((const struct sockaddr_in6 *)ss)->sin6_addr; 407 b = (const uint8_t *) 408 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr; 409 } 410 for (i = 0; i < ap->ap_mask / 8; i++) { 411 if (a[i] != b[i]) 412 goto next; 413 } 414 if (ap->ap_mask % 8) { 415 bmask = 0xff << (8 - (ap->ap_mask % 8)); 416 if ((a[i] & bmask) != (b[i] & bmask)) 417 goto next; 418 } 419 return (ap); 420 next: 421 ; 422 } 423 424 return (NULL); 425 } 426 427 bool 428 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa) 429 { 430 431 if (!auth_portal_defined(ag)) 432 return (true); 433 434 if (auth_portal_find(ag, sa) == NULL) 435 return (false); 436 437 return (true); 438 } 439 440 static struct auth_group * 441 auth_group_create(struct conf *conf, const char *name, char *label) 442 { 443 struct auth_group *ag; 444 445 ag = reinterpret_cast<struct auth_group *>(calloc(1, sizeof(*ag))); 446 if (ag == NULL) 447 log_err(1, "calloc"); 448 if (name != NULL) 449 ag->ag_name = checked_strdup(name); 450 ag->ag_label = label; 451 TAILQ_INIT(&ag->ag_auths); 452 TAILQ_INIT(&ag->ag_names); 453 TAILQ_INIT(&ag->ag_portals); 454 ag->ag_conf = conf; 455 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next); 456 457 return (ag); 458 } 459 460 struct auth_group * 461 auth_group_new(struct conf *conf, const char *name) 462 { 463 struct auth_group *ag; 464 char *label; 465 466 ag = auth_group_find(conf, name); 467 if (ag != NULL) { 468 log_warnx("duplicated auth-group \"%s\"", name); 469 return (NULL); 470 } 471 472 asprintf(&label, "auth-group \"%s\"", name); 473 return (auth_group_create(conf, name, label)); 474 } 475 476 struct auth_group * 477 auth_group_new(struct conf *conf, struct target *target) 478 { 479 char *label; 480 481 asprintf(&label, "target \"%s\"", target->t_name); 482 return (auth_group_create(conf, NULL, label)); 483 } 484 485 void 486 auth_group_delete(struct auth_group *ag) 487 { 488 struct auth *auth, *auth_tmp; 489 struct auth_name *auth_name, *auth_name_tmp; 490 struct auth_portal *auth_portal, *auth_portal_tmp; 491 492 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next); 493 494 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp) 495 auth_delete(auth); 496 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp) 497 auth_name_delete(auth_name); 498 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next, 499 auth_portal_tmp) 500 auth_portal_delete(auth_portal); 501 free(ag->ag_label); 502 free(ag->ag_name); 503 free(ag); 504 } 505 506 struct auth_group * 507 auth_group_find(const struct conf *conf, const char *name) 508 { 509 struct auth_group *ag; 510 511 assert(name != NULL); 512 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 513 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 514 return (ag); 515 } 516 517 return (NULL); 518 } 519 520 static struct portal * 521 portal_new(struct portal_group *pg) 522 { 523 struct portal *portal; 524 525 portal = reinterpret_cast<struct portal *>(calloc(1, sizeof(*portal))); 526 if (portal == NULL) 527 log_err(1, "calloc"); 528 TAILQ_INIT(&portal->p_targets); 529 portal->p_portal_group = pg; 530 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 531 return (portal); 532 } 533 534 static void 535 portal_delete(struct portal *portal) 536 { 537 538 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 539 if (portal->p_ai != NULL) 540 freeaddrinfo(portal->p_ai); 541 free(portal->p_listen); 542 free(portal); 543 } 544 545 struct portal_group * 546 portal_group_new(struct conf *conf, const char *name) 547 { 548 struct portal_group *pg; 549 550 pg = portal_group_find(conf, name); 551 if (pg != NULL) { 552 log_warnx("duplicated portal-group \"%s\"", name); 553 return (NULL); 554 } 555 556 pg = reinterpret_cast<struct portal_group *>(calloc(1, sizeof(*pg))); 557 if (pg == NULL) 558 log_err(1, "calloc"); 559 pg->pg_name = checked_strdup(name); 560 pg->pg_options = nvlist_create(0); 561 TAILQ_INIT(&pg->pg_portals); 562 TAILQ_INIT(&pg->pg_ports); 563 pg->pg_conf = conf; 564 pg->pg_tag = 0; /* Assigned later in conf_apply(). */ 565 pg->pg_dscp = -1; 566 pg->pg_pcp = -1; 567 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 568 569 return (pg); 570 } 571 572 void 573 portal_group_delete(struct portal_group *pg) 574 { 575 struct portal *portal, *tmp; 576 struct port *port, *tport; 577 578 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport) 579 port_delete(port); 580 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 581 582 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 583 portal_delete(portal); 584 nvlist_destroy(pg->pg_options); 585 free(pg->pg_name); 586 free(pg->pg_offload); 587 free(pg->pg_redirection); 588 free(pg); 589 } 590 591 struct portal_group * 592 portal_group_find(const struct conf *conf, const char *name) 593 { 594 struct portal_group *pg; 595 596 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 597 if (strcmp(pg->pg_name, name) == 0) 598 return (pg); 599 } 600 601 return (NULL); 602 } 603 604 static int 605 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai) 606 { 607 struct addrinfo hints; 608 char *str, *addr, *ch; 609 const char *port; 610 int error, colons = 0; 611 612 str = arg = strdup(arg); 613 if (arg[0] == '[') { 614 /* 615 * IPv6 address in square brackets, perhaps with port. 616 */ 617 arg++; 618 addr = strsep(&arg, "]"); 619 if (arg == NULL) { 620 free(str); 621 return (1); 622 } 623 if (arg[0] == '\0') { 624 port = def_port; 625 } else if (arg[0] == ':') { 626 port = arg + 1; 627 } else { 628 free(str); 629 return (1); 630 } 631 } else { 632 /* 633 * Either IPv6 address without brackets - and without 634 * a port - or IPv4 address. Just count the colons. 635 */ 636 for (ch = arg; *ch != '\0'; ch++) { 637 if (*ch == ':') 638 colons++; 639 } 640 if (colons > 1) { 641 addr = arg; 642 port = def_port; 643 } else { 644 addr = strsep(&arg, ":"); 645 if (arg == NULL) 646 port = def_port; 647 else 648 port = arg; 649 } 650 } 651 652 memset(&hints, 0, sizeof(hints)); 653 hints.ai_family = PF_UNSPEC; 654 hints.ai_socktype = SOCK_STREAM; 655 hints.ai_flags = AI_PASSIVE; 656 error = getaddrinfo(addr, port, &hints, ai); 657 free(str); 658 return ((error != 0) ? 1 : 0); 659 } 660 661 bool 662 portal_group_add_portal(struct portal_group *pg, const char *value, bool iser) 663 { 664 struct portal *portal; 665 666 portal = portal_new(pg); 667 portal->p_listen = checked_strdup(value); 668 portal->p_iser = iser; 669 670 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) { 671 log_warnx("invalid listen address %s", portal->p_listen); 672 portal_delete(portal); 673 return (false); 674 } 675 676 /* 677 * XXX: getaddrinfo(3) may return multiple addresses; we should turn 678 * those into multiple portals. 679 */ 680 681 return (true); 682 } 683 684 bool 685 isns_new(struct conf *conf, const char *addr) 686 { 687 struct isns *isns; 688 689 isns = reinterpret_cast<struct isns *>(calloc(1, sizeof(*isns))); 690 if (isns == NULL) 691 log_err(1, "calloc"); 692 isns->i_conf = conf; 693 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next); 694 isns->i_addr = checked_strdup(addr); 695 696 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) { 697 log_warnx("invalid iSNS address %s", isns->i_addr); 698 isns_delete(isns); 699 return (false); 700 } 701 702 /* 703 * XXX: getaddrinfo(3) may return multiple addresses; we should turn 704 * those into multiple servers. 705 */ 706 707 return (true); 708 } 709 710 void 711 isns_delete(struct isns *isns) 712 { 713 714 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next); 715 free(isns->i_addr); 716 if (isns->i_ai != NULL) 717 freeaddrinfo(isns->i_ai); 718 free(isns); 719 } 720 721 static int 722 isns_do_connect(struct isns *isns) 723 { 724 int s; 725 726 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype, 727 isns->i_ai->ai_protocol); 728 if (s < 0) { 729 log_warn("socket(2) failed for %s", isns->i_addr); 730 return (-1); 731 } 732 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) { 733 log_warn("connect(2) failed for %s", isns->i_addr); 734 close(s); 735 return (-1); 736 } 737 return(s); 738 } 739 740 static int 741 isns_do_register(struct isns *isns, int s, const char *hostname) 742 { 743 struct conf *conf = isns->i_conf; 744 struct target *target; 745 struct portal *portal; 746 struct portal_group *pg; 747 struct port *port; 748 struct isns_req *req; 749 int res = 0; 750 uint32_t error; 751 752 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT); 753 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 754 isns_req_add_delim(req); 755 isns_req_add_str(req, 1, hostname); 756 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */ 757 isns_req_add_32(req, 6, conf->conf_isns_period); 758 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 759 if (pg->pg_unassigned) 760 continue; 761 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 762 isns_req_add_addr(req, 16, portal->p_ai); 763 isns_req_add_port(req, 17, portal->p_ai); 764 } 765 } 766 TAILQ_FOREACH(target, &conf->conf_targets, t_next) { 767 isns_req_add_str(req, 32, target->t_name); 768 isns_req_add_32(req, 33, 1); /* 1 -- Target*/ 769 if (target->t_alias != NULL) 770 isns_req_add_str(req, 34, target->t_alias); 771 TAILQ_FOREACH(port, &target->t_ports, p_ts) { 772 if ((pg = port->p_portal_group) == NULL) 773 continue; 774 isns_req_add_32(req, 51, pg->pg_tag); 775 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 776 isns_req_add_addr(req, 49, portal->p_ai); 777 isns_req_add_port(req, 50, portal->p_ai); 778 } 779 } 780 } 781 res = isns_req_send(s, req); 782 if (res < 0) { 783 log_warn("send(2) failed for %s", isns->i_addr); 784 goto quit; 785 } 786 res = isns_req_receive(s, req); 787 if (res < 0) { 788 log_warn("receive(2) failed for %s", isns->i_addr); 789 goto quit; 790 } 791 error = isns_req_get_status(req); 792 if (error != 0) { 793 log_warnx("iSNS register error %d for %s", error, isns->i_addr); 794 res = -1; 795 } 796 quit: 797 isns_req_free(req); 798 return (res); 799 } 800 801 static int 802 isns_do_check(struct isns *isns, int s, const char *hostname) 803 { 804 struct conf *conf = isns->i_conf; 805 struct isns_req *req; 806 int res = 0; 807 uint32_t error; 808 809 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT); 810 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 811 isns_req_add_str(req, 1, hostname); 812 isns_req_add_delim(req); 813 isns_req_add(req, 2, 0, NULL); 814 res = isns_req_send(s, req); 815 if (res < 0) { 816 log_warn("send(2) failed for %s", isns->i_addr); 817 goto quit; 818 } 819 res = isns_req_receive(s, req); 820 if (res < 0) { 821 log_warn("receive(2) failed for %s", isns->i_addr); 822 goto quit; 823 } 824 error = isns_req_get_status(req); 825 if (error != 0) { 826 log_warnx("iSNS check error %d for %s", error, isns->i_addr); 827 res = -1; 828 } 829 quit: 830 isns_req_free(req); 831 return (res); 832 } 833 834 static int 835 isns_do_deregister(struct isns *isns, int s, const char *hostname) 836 { 837 struct conf *conf = isns->i_conf; 838 struct isns_req *req; 839 int res = 0; 840 uint32_t error; 841 842 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT); 843 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 844 isns_req_add_delim(req); 845 isns_req_add_str(req, 1, hostname); 846 res = isns_req_send(s, req); 847 if (res < 0) { 848 log_warn("send(2) failed for %s", isns->i_addr); 849 goto quit; 850 } 851 res = isns_req_receive(s, req); 852 if (res < 0) { 853 log_warn("receive(2) failed for %s", isns->i_addr); 854 goto quit; 855 } 856 error = isns_req_get_status(req); 857 if (error != 0) { 858 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr); 859 res = -1; 860 } 861 quit: 862 isns_req_free(req); 863 return (res); 864 } 865 866 void 867 isns_register(struct isns *isns, struct isns *oldisns) 868 { 869 struct conf *conf = isns->i_conf; 870 int error, s; 871 char hostname[256]; 872 873 if (TAILQ_EMPTY(&conf->conf_targets) || 874 TAILQ_EMPTY(&conf->conf_portal_groups)) 875 return; 876 set_timeout(conf->conf_isns_timeout, false); 877 s = isns_do_connect(isns); 878 if (s < 0) { 879 set_timeout(0, false); 880 return; 881 } 882 error = gethostname(hostname, sizeof(hostname)); 883 if (error != 0) 884 log_err(1, "gethostname"); 885 886 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets)) 887 oldisns = isns; 888 isns_do_deregister(oldisns, s, hostname); 889 isns_do_register(isns, s, hostname); 890 close(s); 891 set_timeout(0, false); 892 } 893 894 void 895 isns_check(struct isns *isns) 896 { 897 struct conf *conf = isns->i_conf; 898 int error, s, res; 899 char hostname[256]; 900 901 if (TAILQ_EMPTY(&conf->conf_targets) || 902 TAILQ_EMPTY(&conf->conf_portal_groups)) 903 return; 904 set_timeout(conf->conf_isns_timeout, false); 905 s = isns_do_connect(isns); 906 if (s < 0) { 907 set_timeout(0, false); 908 return; 909 } 910 error = gethostname(hostname, sizeof(hostname)); 911 if (error != 0) 912 log_err(1, "gethostname"); 913 914 res = isns_do_check(isns, s, hostname); 915 if (res < 0) { 916 isns_do_deregister(isns, s, hostname); 917 isns_do_register(isns, s, hostname); 918 } 919 close(s); 920 set_timeout(0, false); 921 } 922 923 void 924 isns_deregister(struct isns *isns) 925 { 926 struct conf *conf = isns->i_conf; 927 int error, s; 928 char hostname[256]; 929 930 if (TAILQ_EMPTY(&conf->conf_targets) || 931 TAILQ_EMPTY(&conf->conf_portal_groups)) 932 return; 933 set_timeout(conf->conf_isns_timeout, false); 934 s = isns_do_connect(isns); 935 if (s < 0) 936 return; 937 error = gethostname(hostname, sizeof(hostname)); 938 if (error != 0) 939 log_err(1, "gethostname"); 940 941 isns_do_deregister(isns, s, hostname); 942 close(s); 943 set_timeout(0, false); 944 } 945 946 struct pport * 947 pport_new(struct kports *kports, const char *name, uint32_t ctl_port) 948 { 949 struct pport *pp; 950 951 pp = reinterpret_cast<struct pport *>(calloc(1, sizeof(*pp))); 952 if (pp == NULL) 953 log_err(1, "calloc"); 954 pp->pp_kports = kports; 955 pp->pp_name = checked_strdup(name); 956 pp->pp_ctl_port = ctl_port; 957 TAILQ_INIT(&pp->pp_ports); 958 TAILQ_INSERT_TAIL(&kports->pports, pp, pp_next); 959 return (pp); 960 } 961 962 struct pport * 963 pport_find(const struct kports *kports, const char *name) 964 { 965 struct pport *pp; 966 967 TAILQ_FOREACH(pp, &kports->pports, pp_next) { 968 if (strcasecmp(pp->pp_name, name) == 0) 969 return (pp); 970 } 971 return (NULL); 972 } 973 974 struct pport * 975 pport_copy(struct pport *pp, struct kports *kports) 976 { 977 struct pport *ppnew; 978 979 ppnew = pport_new(kports, pp->pp_name, pp->pp_ctl_port); 980 return (ppnew); 981 } 982 983 void 984 pport_delete(struct pport *pp) 985 { 986 struct port *port, *tport; 987 988 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport) 989 port_delete(port); 990 TAILQ_REMOVE(&pp->pp_kports->pports, pp, pp_next); 991 free(pp->pp_name); 992 free(pp); 993 } 994 995 struct port * 996 port_new(struct conf *conf, struct target *target, struct portal_group *pg) 997 { 998 struct port *port; 999 char *name; 1000 int ret; 1001 1002 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name); 1003 if (ret <= 0) 1004 log_err(1, "asprintf"); 1005 if (port_find(conf, name) != NULL) { 1006 log_warnx("duplicate port \"%s\"", name); 1007 free(name); 1008 return (NULL); 1009 } 1010 port = reinterpret_cast<struct port *>(calloc(1, sizeof(*port))); 1011 if (port == NULL) 1012 log_err(1, "calloc"); 1013 port->p_conf = conf; 1014 port->p_name = name; 1015 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1016 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1017 port->p_target = target; 1018 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs); 1019 port->p_portal_group = pg; 1020 return (port); 1021 } 1022 1023 struct port * 1024 port_new_ioctl(struct conf *conf, struct kports *kports, struct target *target, 1025 int pp, int vp) 1026 { 1027 struct pport *pport; 1028 struct port *port; 1029 char *pname; 1030 char *name; 1031 int ret; 1032 1033 ret = asprintf(&pname, "ioctl/%d/%d", pp, vp); 1034 if (ret <= 0) { 1035 log_err(1, "asprintf"); 1036 return (NULL); 1037 } 1038 1039 pport = pport_find(kports, pname); 1040 if (pport != NULL) { 1041 free(pname); 1042 return (port_new_pp(conf, target, pport)); 1043 } 1044 1045 ret = asprintf(&name, "%s-%s", pname, target->t_name); 1046 free(pname); 1047 1048 if (ret <= 0) 1049 log_err(1, "asprintf"); 1050 if (port_find(conf, name) != NULL) { 1051 log_warnx("duplicate port \"%s\"", name); 1052 free(name); 1053 return (NULL); 1054 } 1055 port = reinterpret_cast<struct port *>(calloc(1, sizeof(*port))); 1056 if (port == NULL) 1057 log_err(1, "calloc"); 1058 port->p_conf = conf; 1059 port->p_name = name; 1060 port->p_ioctl_port = true; 1061 port->p_ioctl_pp = pp; 1062 port->p_ioctl_vp = vp; 1063 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1064 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1065 port->p_target = target; 1066 return (port); 1067 } 1068 1069 struct port * 1070 port_new_pp(struct conf *conf, struct target *target, struct pport *pp) 1071 { 1072 struct port *port; 1073 char *name; 1074 int ret; 1075 1076 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name); 1077 if (ret <= 0) 1078 log_err(1, "asprintf"); 1079 if (port_find(conf, name) != NULL) { 1080 log_warnx("duplicate port \"%s\"", name); 1081 free(name); 1082 return (NULL); 1083 } 1084 port = reinterpret_cast<struct port *>(calloc(1, sizeof(*port))); 1085 if (port == NULL) 1086 log_err(1, "calloc"); 1087 port->p_conf = conf; 1088 port->p_name = name; 1089 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1090 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1091 port->p_target = target; 1092 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps); 1093 port->p_pport = pp; 1094 return (port); 1095 } 1096 1097 struct port * 1098 port_find(const struct conf *conf, const char *name) 1099 { 1100 struct port *port; 1101 1102 TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 1103 if (strcasecmp(port->p_name, name) == 0) 1104 return (port); 1105 } 1106 1107 return (NULL); 1108 } 1109 1110 struct port * 1111 port_find_in_pg(const struct portal_group *pg, const char *target) 1112 { 1113 struct port *port; 1114 1115 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) { 1116 if (strcasecmp(port->p_target->t_name, target) == 0) 1117 return (port); 1118 } 1119 1120 return (NULL); 1121 } 1122 1123 void 1124 port_delete(struct port *port) 1125 { 1126 1127 if (port->p_portal_group) 1128 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs); 1129 if (port->p_pport) 1130 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps); 1131 if (port->p_target) 1132 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts); 1133 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next); 1134 free(port->p_name); 1135 free(port); 1136 } 1137 1138 bool 1139 port_is_dummy(struct port *port) 1140 { 1141 1142 if (port->p_portal_group) { 1143 if (port->p_portal_group->pg_foreign) 1144 return (true); 1145 if (TAILQ_EMPTY(&port->p_portal_group->pg_portals)) 1146 return (true); 1147 } 1148 return (false); 1149 } 1150 1151 struct target * 1152 target_new(struct conf *conf, const char *name) 1153 { 1154 struct target *targ; 1155 int i, len; 1156 1157 targ = target_find(conf, name); 1158 if (targ != NULL) { 1159 log_warnx("duplicated target \"%s\"", name); 1160 return (NULL); 1161 } 1162 if (valid_iscsi_name(name, log_warnx) == false) { 1163 return (NULL); 1164 } 1165 targ = reinterpret_cast<struct target *>(calloc(1, sizeof(*targ))); 1166 if (targ == NULL) 1167 log_err(1, "calloc"); 1168 targ->t_name = checked_strdup(name); 1169 1170 /* 1171 * RFC 3722 requires us to normalize the name to lowercase. 1172 */ 1173 len = strlen(name); 1174 for (i = 0; i < len; i++) 1175 targ->t_name[i] = tolower(targ->t_name[i]); 1176 1177 targ->t_conf = conf; 1178 TAILQ_INIT(&targ->t_ports); 1179 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 1180 1181 return (targ); 1182 } 1183 1184 void 1185 target_delete(struct target *targ) 1186 { 1187 struct port *port, *tport; 1188 1189 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport) 1190 port_delete(port); 1191 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 1192 1193 free(targ->t_pport); 1194 free(targ->t_name); 1195 free(targ->t_redirection); 1196 free(targ); 1197 } 1198 1199 struct target * 1200 target_find(struct conf *conf, const char *name) 1201 { 1202 struct target *targ; 1203 1204 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1205 if (strcasecmp(targ->t_name, name) == 0) 1206 return (targ); 1207 } 1208 1209 return (NULL); 1210 } 1211 1212 struct lun * 1213 lun_new(struct conf *conf, const char *name) 1214 { 1215 struct lun *lun; 1216 1217 lun = lun_find(conf, name); 1218 if (lun != NULL) { 1219 log_warnx("duplicated lun \"%s\"", name); 1220 return (NULL); 1221 } 1222 1223 lun = reinterpret_cast<struct lun *>(calloc(1, sizeof(*lun))); 1224 if (lun == NULL) 1225 log_err(1, "calloc"); 1226 lun->l_conf = conf; 1227 lun->l_name = checked_strdup(name); 1228 lun->l_options = nvlist_create(0); 1229 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next); 1230 lun->l_ctl_lun = -1; 1231 1232 return (lun); 1233 } 1234 1235 void 1236 lun_delete(struct lun *lun) 1237 { 1238 struct target *targ; 1239 int i; 1240 1241 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) { 1242 for (i = 0; i < MAX_LUNS; i++) { 1243 if (targ->t_luns[i] == lun) 1244 targ->t_luns[i] = NULL; 1245 } 1246 } 1247 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next); 1248 1249 nvlist_destroy(lun->l_options); 1250 free(lun->l_name); 1251 free(lun->l_backend); 1252 free(lun->l_device_id); 1253 free(lun->l_path); 1254 free(lun->l_scsiname); 1255 free(lun->l_serial); 1256 free(lun); 1257 } 1258 1259 struct lun * 1260 lun_find(const struct conf *conf, const char *name) 1261 { 1262 struct lun *lun; 1263 1264 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1265 if (strcmp(lun->l_name, name) == 0) 1266 return (lun); 1267 } 1268 1269 return (NULL); 1270 } 1271 1272 void 1273 lun_set_scsiname(struct lun *lun, const char *value) 1274 { 1275 free(lun->l_scsiname); 1276 lun->l_scsiname = checked_strdup(value); 1277 } 1278 1279 bool 1280 option_new(nvlist_t *nvl, const char *name, const char *value) 1281 { 1282 int error; 1283 1284 if (nvlist_exists_string(nvl, name)) { 1285 log_warnx("duplicated option \"%s\"", name); 1286 return (false); 1287 } 1288 1289 nvlist_add_string(nvl, name, value); 1290 error = nvlist_error(nvl); 1291 if (error != 0) { 1292 log_warnc(error, "failed to add option \"%s\"", name); 1293 return (false); 1294 } 1295 return (true); 1296 } 1297 1298 #ifdef ICL_KERNEL_PROXY 1299 1300 static void 1301 pdu_receive_proxy(struct pdu *pdu) 1302 { 1303 struct connection *conn; 1304 size_t len; 1305 1306 assert(proxy_mode); 1307 conn = pdu->pdu_connection; 1308 1309 kernel_receive(pdu); 1310 1311 len = pdu_ahs_length(pdu); 1312 if (len > 0) 1313 log_errx(1, "protocol error: non-empty AHS"); 1314 1315 len = pdu_data_segment_length(pdu); 1316 assert(len <= (size_t)conn->conn_max_recv_data_segment_length); 1317 pdu->pdu_data_len = len; 1318 } 1319 1320 static void 1321 pdu_send_proxy(struct pdu *pdu) 1322 { 1323 1324 assert(proxy_mode); 1325 1326 pdu_set_data_segment_length(pdu, pdu->pdu_data_len); 1327 kernel_send(pdu); 1328 } 1329 1330 #endif /* ICL_KERNEL_PROXY */ 1331 1332 static void 1333 pdu_fail(const struct connection *conn __unused, const char *reason __unused) 1334 { 1335 } 1336 1337 static struct ctld_connection * 1338 connection_new(struct portal *portal, int fd, const char *host, 1339 const struct sockaddr *client_sa) 1340 { 1341 struct ctld_connection *conn; 1342 1343 conn = reinterpret_cast<struct ctld_connection *>(calloc(1, sizeof(*conn))); 1344 if (conn == NULL) 1345 log_err(1, "calloc"); 1346 connection_init(&conn->conn, &conn_ops, proxy_mode); 1347 conn->conn.conn_socket = fd; 1348 conn->conn_portal = portal; 1349 conn->conn_initiator_addr = checked_strdup(host); 1350 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len); 1351 1352 return (conn); 1353 } 1354 1355 static bool 1356 conf_verify_lun(struct lun *lun) 1357 { 1358 const struct lun *lun2; 1359 1360 if (lun->l_backend == NULL) 1361 lun->l_backend = checked_strdup("block"); 1362 if (strcmp(lun->l_backend, "block") == 0) { 1363 if (lun->l_path == NULL) { 1364 log_warnx("missing path for lun \"%s\"", 1365 lun->l_name); 1366 return (false); 1367 } 1368 } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 1369 if (lun->l_size == 0) { 1370 log_warnx("missing size for ramdisk-backed lun \"%s\"", 1371 lun->l_name); 1372 return (false); 1373 } 1374 if (lun->l_path != NULL) { 1375 log_warnx("path must not be specified " 1376 "for ramdisk-backed lun \"%s\"", 1377 lun->l_name); 1378 return (false); 1379 } 1380 } 1381 if (lun->l_blocksize == 0) { 1382 if (lun->l_device_type == T_CDROM) 1383 lun->l_blocksize = DEFAULT_CD_BLOCKSIZE; 1384 else 1385 lun->l_blocksize = DEFAULT_BLOCKSIZE; 1386 } else if (lun->l_blocksize < 0) { 1387 log_warnx("invalid blocksize for lun \"%s\"; " 1388 "must be larger than 0", lun->l_name); 1389 return (false); 1390 } 1391 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 1392 log_warnx("invalid size for lun \"%s\"; " 1393 "must be multiple of blocksize", lun->l_name); 1394 return (false); 1395 } 1396 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) { 1397 if (lun == lun2) 1398 continue; 1399 if (lun->l_path != NULL && lun2->l_path != NULL && 1400 strcmp(lun->l_path, lun2->l_path) == 0) { 1401 log_debugx("WARNING: path \"%s\" duplicated " 1402 "between lun \"%s\", and " 1403 "lun \"%s\"", lun->l_path, 1404 lun->l_name, lun2->l_name); 1405 } 1406 } 1407 1408 return (true); 1409 } 1410 1411 bool 1412 conf_verify(struct conf *conf) 1413 { 1414 struct auth_group *ag; 1415 struct portal_group *pg; 1416 struct port *port; 1417 struct target *targ; 1418 struct lun *lun; 1419 bool found; 1420 int i; 1421 1422 if (conf->conf_pidfile_path == NULL) 1423 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1424 1425 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1426 if (!conf_verify_lun(lun)) 1427 return (false); 1428 } 1429 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1430 if (targ->t_auth_group == NULL) { 1431 targ->t_auth_group = auth_group_find(conf, 1432 "default"); 1433 assert(targ->t_auth_group != NULL); 1434 } 1435 if (TAILQ_EMPTY(&targ->t_ports)) { 1436 pg = portal_group_find(conf, "default"); 1437 assert(pg != NULL); 1438 port_new(conf, targ, pg); 1439 } 1440 found = false; 1441 for (i = 0; i < MAX_LUNS; i++) { 1442 if (targ->t_luns[i] != NULL) 1443 found = true; 1444 } 1445 if (!found && targ->t_redirection == NULL) { 1446 log_warnx("no LUNs defined for target \"%s\"", 1447 targ->t_name); 1448 } 1449 if (found && targ->t_redirection != NULL) { 1450 log_debugx("target \"%s\" contains luns, " 1451 " but configured for redirection", 1452 targ->t_name); 1453 } 1454 } 1455 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1456 assert(pg->pg_name != NULL); 1457 if (pg->pg_discovery_auth_group == NULL) { 1458 pg->pg_discovery_auth_group = 1459 auth_group_find(conf, "default"); 1460 assert(pg->pg_discovery_auth_group != NULL); 1461 } 1462 1463 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN) 1464 pg->pg_discovery_filter = PG_FILTER_NONE; 1465 1466 if (pg->pg_redirection != NULL) { 1467 if (!TAILQ_EMPTY(&pg->pg_ports)) { 1468 log_debugx("portal-group \"%s\" assigned " 1469 "to target, but configured " 1470 "for redirection", 1471 pg->pg_name); 1472 } 1473 pg->pg_unassigned = false; 1474 } else if (!TAILQ_EMPTY(&pg->pg_ports)) { 1475 pg->pg_unassigned = false; 1476 } else { 1477 if (strcmp(pg->pg_name, "default") != 0) 1478 log_warnx("portal-group \"%s\" not assigned " 1479 "to any target", pg->pg_name); 1480 pg->pg_unassigned = true; 1481 } 1482 } 1483 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1484 found = false; 1485 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1486 if (targ->t_auth_group == ag) { 1487 found = true; 1488 break; 1489 } 1490 } 1491 TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 1492 if (port->p_auth_group == ag) { 1493 found = true; 1494 break; 1495 } 1496 } 1497 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1498 if (pg->pg_discovery_auth_group == ag) { 1499 found = true; 1500 break; 1501 } 1502 } 1503 if (!found && ag->ag_name != NULL && 1504 strcmp(ag->ag_name, "default") != 0 && 1505 strcmp(ag->ag_name, "no-authentication") != 0 && 1506 strcmp(ag->ag_name, "no-access") != 0) { 1507 log_warnx("auth-group \"%s\" not assigned " 1508 "to any target", ag->ag_name); 1509 } 1510 } 1511 1512 return (true); 1513 } 1514 1515 static bool 1516 portal_reuse_socket(struct portal *oldp, struct portal *newp) 1517 { 1518 struct kevent kev; 1519 1520 if (strcmp(newp->p_listen, oldp->p_listen) != 0) 1521 return (false); 1522 1523 if (oldp->p_socket <= 0) 1524 return (false); 1525 1526 EV_SET(&kev, oldp->p_socket, EVFILT_READ, EV_ADD, 0, 0, newp); 1527 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) == -1) 1528 return (false); 1529 1530 newp->p_socket = oldp->p_socket; 1531 oldp->p_socket = 0; 1532 return (true); 1533 } 1534 1535 static bool 1536 portal_init_socket(struct portal *p) 1537 { 1538 struct portal_group *pg = p->p_portal_group; 1539 struct kevent kev; 1540 int error, sockbuf; 1541 int one = 1; 1542 1543 log_debugx("listening on %s, portal-group \"%s\"", 1544 p->p_listen, pg->pg_name); 1545 p->p_socket = socket(p->p_ai->ai_family, p->p_ai->ai_socktype, 1546 p->p_ai->ai_protocol); 1547 if (p->p_socket < 0) { 1548 log_warn("socket(2) failed for %s", 1549 p->p_listen); 1550 return (false); 1551 } 1552 1553 sockbuf = SOCKBUF_SIZE; 1554 if (setsockopt(p->p_socket, SOL_SOCKET, SO_RCVBUF, &sockbuf, 1555 sizeof(sockbuf)) == -1) 1556 log_warn("setsockopt(SO_RCVBUF) failed for %s", 1557 p->p_listen); 1558 sockbuf = SOCKBUF_SIZE; 1559 if (setsockopt(p->p_socket, SOL_SOCKET, SO_SNDBUF, &sockbuf, 1560 sizeof(sockbuf)) == -1) 1561 log_warn("setsockopt(SO_SNDBUF) failed for %s", p->p_listen); 1562 if (setsockopt(p->p_socket, SOL_SOCKET, SO_NO_DDP, &one, 1563 sizeof(one)) == -1) 1564 log_warn("setsockopt(SO_NO_DDP) failed for %s", p->p_listen); 1565 error = setsockopt(p->p_socket, SOL_SOCKET, SO_REUSEADDR, &one, 1566 sizeof(one)); 1567 if (error != 0) { 1568 log_warn("setsockopt(SO_REUSEADDR) failed for %s", p->p_listen); 1569 close(p->p_socket); 1570 p->p_socket = 0; 1571 return (false); 1572 } 1573 1574 if (pg->pg_dscp != -1) { 1575 /* Only allow the 6-bit DSCP field to be modified */ 1576 int tos = pg->pg_dscp << 2; 1577 switch (p->p_ai->ai_family) { 1578 case AF_INET: 1579 if (setsockopt(p->p_socket, IPPROTO_IP, IP_TOS, 1580 &tos, sizeof(tos)) == -1) 1581 log_warn("setsockopt(IP_TOS) failed for %s", 1582 p->p_listen); 1583 break; 1584 case AF_INET6: 1585 if (setsockopt(p->p_socket, IPPROTO_IPV6, IPV6_TCLASS, 1586 &tos, sizeof(tos)) == -1) 1587 log_warn("setsockopt(IPV6_TCLASS) failed for %s", 1588 p->p_listen); 1589 break; 1590 } 1591 } 1592 if (pg->pg_pcp != -1) { 1593 int pcp = pg->pg_pcp; 1594 switch (p->p_ai->ai_family) { 1595 case AF_INET: 1596 if (setsockopt(p->p_socket, IPPROTO_IP, IP_VLAN_PCP, 1597 &pcp, sizeof(pcp)) == -1) 1598 log_warn("setsockopt(IP_VLAN_PCP) failed for %s", 1599 p->p_listen); 1600 break; 1601 case AF_INET6: 1602 if (setsockopt(p->p_socket, IPPROTO_IPV6, IPV6_VLAN_PCP, 1603 &pcp, sizeof(pcp)) == -1) 1604 log_warn("setsockopt(IPV6_VLAN_PCP) failed for %s", 1605 p->p_listen); 1606 break; 1607 } 1608 } 1609 1610 error = bind(p->p_socket, p->p_ai->ai_addr, 1611 p->p_ai->ai_addrlen); 1612 if (error != 0) { 1613 log_warn("bind(2) failed for %s", p->p_listen); 1614 close(p->p_socket); 1615 p->p_socket = 0; 1616 return (false); 1617 } 1618 error = listen(p->p_socket, -1); 1619 if (error != 0) { 1620 log_warn("listen(2) failed for %s", p->p_listen); 1621 close(p->p_socket); 1622 p->p_socket = 0; 1623 return (false); 1624 } 1625 EV_SET(&kev, p->p_socket, EVFILT_READ, EV_ADD, 0, 0, p); 1626 error = kevent(kqfd, &kev, 1, NULL, 0, NULL); 1627 if (error == -1) { 1628 log_warn("kevent(2) failed to register for %s", p->p_listen); 1629 close(p->p_socket); 1630 p->p_socket = 0; 1631 return (false); 1632 } 1633 return (true); 1634 } 1635 1636 static int 1637 conf_apply(struct conf *oldconf, struct conf *newconf) 1638 { 1639 struct lun *oldlun, *newlun, *tmplun; 1640 struct portal_group *oldpg, *newpg; 1641 struct portal *oldp, *newp; 1642 struct port *oldport, *newport, *tmpport; 1643 struct isns *oldns, *newns; 1644 int changed, cumulated_error = 0, error; 1645 1646 if (oldconf->conf_debug != newconf->conf_debug) { 1647 log_debugx("changing debug level to %d", newconf->conf_debug); 1648 log_init(newconf->conf_debug); 1649 } 1650 1651 if (oldconf->conf_pidfile_path != NULL && 1652 newconf->conf_pidfile_path != NULL) 1653 { 1654 if (strcmp(oldconf->conf_pidfile_path, 1655 newconf->conf_pidfile_path) != 0) 1656 { 1657 /* pidfile has changed. rename it */ 1658 log_debugx("moving pidfile to %s", 1659 newconf->conf_pidfile_path); 1660 if (rename(oldconf->conf_pidfile_path, 1661 newconf->conf_pidfile_path)) 1662 { 1663 log_err(1, "renaming pidfile %s -> %s", 1664 oldconf->conf_pidfile_path, 1665 newconf->conf_pidfile_path); 1666 } 1667 } 1668 newconf->conf_pidfh = oldconf->conf_pidfh; 1669 oldconf->conf_pidfh = NULL; 1670 } 1671 1672 /* 1673 * Go through the new portal groups, assigning tags or preserving old. 1674 */ 1675 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1676 if (newpg->pg_tag != 0) 1677 continue; 1678 oldpg = portal_group_find(oldconf, newpg->pg_name); 1679 if (oldpg != NULL) 1680 newpg->pg_tag = oldpg->pg_tag; 1681 else 1682 newpg->pg_tag = ++last_portal_group_tag; 1683 } 1684 1685 /* Deregister on removed iSNS servers. */ 1686 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1687 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1688 if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1689 break; 1690 } 1691 if (newns == NULL) 1692 isns_deregister(oldns); 1693 } 1694 1695 /* 1696 * XXX: If target or lun removal fails, we should somehow "move" 1697 * the old lun or target into newconf, so that subsequent 1698 * conf_apply() would try to remove them again. That would 1699 * be somewhat hairy, though, and lun deletion failures don't 1700 * really happen, so leave it as it is for now. 1701 */ 1702 /* 1703 * First, remove any ports present in the old configuration 1704 * and missing in the new one. 1705 */ 1706 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) { 1707 if (port_is_dummy(oldport)) 1708 continue; 1709 newport = port_find(newconf, oldport->p_name); 1710 if (newport != NULL && !port_is_dummy(newport)) 1711 continue; 1712 log_debugx("removing port \"%s\"", oldport->p_name); 1713 error = kernel_port_remove(oldport); 1714 if (error != 0) { 1715 log_warnx("failed to remove port %s", 1716 oldport->p_name); 1717 /* 1718 * XXX: Uncomment after fixing the root cause. 1719 * 1720 * cumulated_error++; 1721 */ 1722 } 1723 } 1724 1725 /* 1726 * Second, remove any LUNs present in the old configuration 1727 * and missing in the new one. 1728 */ 1729 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) { 1730 newlun = lun_find(newconf, oldlun->l_name); 1731 if (newlun == NULL) { 1732 log_debugx("lun \"%s\", CTL lun %d " 1733 "not found in new configuration; " 1734 "removing", oldlun->l_name, oldlun->l_ctl_lun); 1735 error = kernel_lun_remove(oldlun); 1736 if (error != 0) { 1737 log_warnx("failed to remove lun \"%s\", " 1738 "CTL lun %d", 1739 oldlun->l_name, oldlun->l_ctl_lun); 1740 cumulated_error++; 1741 } 1742 continue; 1743 } 1744 1745 /* 1746 * Also remove the LUNs changed by more than size. 1747 */ 1748 changed = 0; 1749 assert(oldlun->l_backend != NULL); 1750 assert(newlun->l_backend != NULL); 1751 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1752 log_debugx("backend for lun \"%s\", " 1753 "CTL lun %d changed; removing", 1754 oldlun->l_name, oldlun->l_ctl_lun); 1755 changed = 1; 1756 } 1757 if (oldlun->l_blocksize != newlun->l_blocksize) { 1758 log_debugx("blocksize for lun \"%s\", " 1759 "CTL lun %d changed; removing", 1760 oldlun->l_name, oldlun->l_ctl_lun); 1761 changed = 1; 1762 } 1763 if (newlun->l_device_id != NULL && 1764 (oldlun->l_device_id == NULL || 1765 strcmp(oldlun->l_device_id, newlun->l_device_id) != 1766 0)) { 1767 log_debugx("device-id for lun \"%s\", " 1768 "CTL lun %d changed; removing", 1769 oldlun->l_name, oldlun->l_ctl_lun); 1770 changed = 1; 1771 } 1772 if (newlun->l_path != NULL && 1773 (oldlun->l_path == NULL || 1774 strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1775 log_debugx("path for lun \"%s\", " 1776 "CTL lun %d, changed; removing", 1777 oldlun->l_name, oldlun->l_ctl_lun); 1778 changed = 1; 1779 } 1780 if (newlun->l_serial != NULL && 1781 (oldlun->l_serial == NULL || 1782 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1783 log_debugx("serial for lun \"%s\", " 1784 "CTL lun %d changed; removing", 1785 oldlun->l_name, oldlun->l_ctl_lun); 1786 changed = 1; 1787 } 1788 if (changed) { 1789 error = kernel_lun_remove(oldlun); 1790 if (error != 0) { 1791 log_warnx("failed to remove lun \"%s\", " 1792 "CTL lun %d", 1793 oldlun->l_name, oldlun->l_ctl_lun); 1794 cumulated_error++; 1795 } 1796 lun_delete(oldlun); 1797 continue; 1798 } 1799 1800 newlun->l_ctl_lun = oldlun->l_ctl_lun; 1801 } 1802 1803 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) { 1804 oldlun = lun_find(oldconf, newlun->l_name); 1805 if (oldlun != NULL) { 1806 log_debugx("modifying lun \"%s\", CTL lun %d", 1807 newlun->l_name, newlun->l_ctl_lun); 1808 error = kernel_lun_modify(newlun); 1809 if (error != 0) { 1810 log_warnx("failed to " 1811 "modify lun \"%s\", CTL lun %d", 1812 newlun->l_name, newlun->l_ctl_lun); 1813 cumulated_error++; 1814 } 1815 continue; 1816 } 1817 log_debugx("adding lun \"%s\"", newlun->l_name); 1818 error = kernel_lun_add(newlun); 1819 if (error != 0) { 1820 log_warnx("failed to add lun \"%s\"", newlun->l_name); 1821 lun_delete(newlun); 1822 cumulated_error++; 1823 } 1824 } 1825 1826 /* 1827 * Now add new ports or modify existing ones. 1828 */ 1829 TAILQ_FOREACH_SAFE(newport, &newconf->conf_ports, p_next, tmpport) { 1830 if (port_is_dummy(newport)) 1831 continue; 1832 oldport = port_find(oldconf, newport->p_name); 1833 1834 if (oldport == NULL || port_is_dummy(oldport)) { 1835 log_debugx("adding port \"%s\"", newport->p_name); 1836 error = kernel_port_add(newport); 1837 } else { 1838 log_debugx("updating port \"%s\"", newport->p_name); 1839 newport->p_ctl_port = oldport->p_ctl_port; 1840 error = kernel_port_update(newport, oldport); 1841 } 1842 if (error != 0) { 1843 log_warnx("failed to %s port %s", 1844 (oldport == NULL) ? "add" : "update", 1845 newport->p_name); 1846 if (oldport == NULL || port_is_dummy(oldport)) 1847 port_delete(newport); 1848 /* 1849 * XXX: Uncomment after fixing the root cause. 1850 * 1851 * cumulated_error++; 1852 */ 1853 } 1854 } 1855 1856 /* 1857 * Go through the new portals, opening the sockets as necessary. 1858 */ 1859 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1860 if (newpg->pg_foreign) 1861 continue; 1862 if (newpg->pg_unassigned) { 1863 log_debugx("not listening on portal-group \"%s\", " 1864 "not assigned to any target", 1865 newpg->pg_name); 1866 continue; 1867 } 1868 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 1869 /* 1870 * Try to find already open portal and reuse 1871 * the listening socket. We don't care about 1872 * what portal or portal group that was, what 1873 * matters is the listening address. 1874 */ 1875 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 1876 pg_next) { 1877 TAILQ_FOREACH(oldp, &oldpg->pg_portals, 1878 p_next) { 1879 if (portal_reuse_socket(oldp, newp)) 1880 goto reused; 1881 } 1882 } 1883 reused: 1884 if (newp->p_socket > 0) { 1885 /* 1886 * We're done with this portal. 1887 */ 1888 continue; 1889 } 1890 1891 #ifdef ICL_KERNEL_PROXY 1892 if (proxy_mode) { 1893 newpg->pg_conf->conf_portal_id++; 1894 newp->p_id = newpg->pg_conf->conf_portal_id; 1895 log_debugx("listening on %s, portal-group " 1896 "\"%s\", portal id %d, using ICL proxy", 1897 newp->p_listen, newpg->pg_name, newp->p_id); 1898 kernel_listen(newp->p_ai, newp->p_iser, 1899 newp->p_id); 1900 continue; 1901 } 1902 #endif 1903 assert(proxy_mode == false); 1904 assert(newp->p_iser == false); 1905 1906 if (!portal_init_socket(newp)) { 1907 cumulated_error++; 1908 continue; 1909 } 1910 } 1911 } 1912 1913 /* 1914 * Go through the no longer used sockets, closing them. 1915 */ 1916 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 1917 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 1918 if (oldp->p_socket <= 0) 1919 continue; 1920 log_debugx("closing socket for %s, portal-group \"%s\"", 1921 oldp->p_listen, oldpg->pg_name); 1922 close(oldp->p_socket); 1923 oldp->p_socket = 0; 1924 } 1925 } 1926 1927 /* (Re-)Register on remaining/new iSNS servers. */ 1928 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1929 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1930 if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1931 break; 1932 } 1933 isns_register(newns, oldns); 1934 } 1935 1936 /* Schedule iSNS update */ 1937 if (!TAILQ_EMPTY(&newconf->conf_isns)) 1938 set_timeout((newconf->conf_isns_period + 2) / 3, false); 1939 1940 return (cumulated_error); 1941 } 1942 1943 static bool 1944 timed_out(void) 1945 { 1946 1947 return (sigalrm_received); 1948 } 1949 1950 static void 1951 sigalrm_handler_fatal(int dummy __unused) 1952 { 1953 /* 1954 * It would be easiest to just log an error and exit. We can't 1955 * do this, though, because log_errx() is not signal safe, since 1956 * it calls syslog(3). Instead, set a flag checked by pdu_send() 1957 * and pdu_receive(), to call log_errx() there. Should they fail 1958 * to notice, we'll exit here one second later. 1959 */ 1960 if (sigalrm_received) { 1961 /* 1962 * Oh well. Just give up and quit. 1963 */ 1964 _exit(2); 1965 } 1966 1967 sigalrm_received = true; 1968 } 1969 1970 static void 1971 sigalrm_handler(int dummy __unused) 1972 { 1973 1974 sigalrm_received = true; 1975 } 1976 1977 void 1978 set_timeout(int timeout, int fatal) 1979 { 1980 struct sigaction sa; 1981 struct itimerval itv; 1982 int error; 1983 1984 if (timeout <= 0) { 1985 log_debugx("session timeout disabled"); 1986 bzero(&itv, sizeof(itv)); 1987 error = setitimer(ITIMER_REAL, &itv, NULL); 1988 if (error != 0) 1989 log_err(1, "setitimer"); 1990 sigalrm_received = false; 1991 return; 1992 } 1993 1994 sigalrm_received = false; 1995 bzero(&sa, sizeof(sa)); 1996 if (fatal) 1997 sa.sa_handler = sigalrm_handler_fatal; 1998 else 1999 sa.sa_handler = sigalrm_handler; 2000 sigfillset(&sa.sa_mask); 2001 error = sigaction(SIGALRM, &sa, NULL); 2002 if (error != 0) 2003 log_err(1, "sigaction"); 2004 2005 /* 2006 * First SIGALRM will arive after conf_timeout seconds. 2007 * If we do nothing, another one will arrive a second later. 2008 */ 2009 log_debugx("setting session timeout to %d seconds", timeout); 2010 bzero(&itv, sizeof(itv)); 2011 itv.it_interval.tv_sec = 1; 2012 itv.it_value.tv_sec = timeout; 2013 error = setitimer(ITIMER_REAL, &itv, NULL); 2014 if (error != 0) 2015 log_err(1, "setitimer"); 2016 } 2017 2018 static int 2019 wait_for_children(bool block) 2020 { 2021 pid_t pid; 2022 int status; 2023 int num = 0; 2024 2025 for (;;) { 2026 /* 2027 * If "block" is true, wait for at least one process. 2028 */ 2029 if (block && num == 0) 2030 pid = wait4(-1, &status, 0, NULL); 2031 else 2032 pid = wait4(-1, &status, WNOHANG, NULL); 2033 if (pid <= 0) 2034 break; 2035 if (WIFSIGNALED(status)) { 2036 log_warnx("child process %d terminated with signal %d", 2037 pid, WTERMSIG(status)); 2038 } else if (WEXITSTATUS(status) != 0) { 2039 log_warnx("child process %d terminated with exit status %d", 2040 pid, WEXITSTATUS(status)); 2041 } else { 2042 log_debugx("child process %d terminated gracefully", pid); 2043 } 2044 num++; 2045 } 2046 2047 return (num); 2048 } 2049 2050 static void 2051 handle_connection(struct portal *portal, int fd, 2052 const struct sockaddr *client_sa, bool dont_fork) 2053 { 2054 struct ctld_connection *conn; 2055 int error; 2056 pid_t pid; 2057 char host[NI_MAXHOST + 1]; 2058 struct conf *conf; 2059 2060 conf = portal->p_portal_group->pg_conf; 2061 2062 if (dont_fork) { 2063 log_debugx("incoming connection; not forking due to -d flag"); 2064 } else { 2065 nchildren -= wait_for_children(false); 2066 assert(nchildren >= 0); 2067 2068 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 2069 log_debugx("maxproc limit of %d child processes hit; " 2070 "waiting for child process to exit", conf->conf_maxproc); 2071 nchildren -= wait_for_children(true); 2072 assert(nchildren >= 0); 2073 } 2074 log_debugx("incoming connection; forking child process #%d", 2075 nchildren); 2076 nchildren++; 2077 pid = fork(); 2078 if (pid < 0) 2079 log_err(1, "fork"); 2080 if (pid > 0) { 2081 close(fd); 2082 return; 2083 } 2084 pidfile_close(conf->conf_pidfh); 2085 } 2086 2087 error = getnameinfo(client_sa, client_sa->sa_len, 2088 host, sizeof(host), NULL, 0, NI_NUMERICHOST); 2089 if (error != 0) 2090 log_errx(1, "getnameinfo: %s", gai_strerror(error)); 2091 2092 log_debugx("accepted connection from %s; portal group \"%s\"", 2093 host, portal->p_portal_group->pg_name); 2094 log_set_peer_addr(host); 2095 setproctitle("%s", host); 2096 2097 conn = connection_new(portal, fd, host, client_sa); 2098 set_timeout(conf->conf_timeout, true); 2099 kernel_capsicate(); 2100 login(conn); 2101 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 2102 kernel_handoff(conn); 2103 log_debugx("connection handed off to the kernel"); 2104 } else { 2105 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 2106 discovery(conn); 2107 } 2108 log_debugx("nothing more to do; exiting"); 2109 exit(0); 2110 } 2111 2112 static void 2113 main_loop(bool dont_fork) 2114 { 2115 struct kevent kev; 2116 struct portal *portal; 2117 struct sockaddr_storage client_sa; 2118 socklen_t client_salen; 2119 #ifdef ICL_KERNEL_PROXY 2120 int connection_id; 2121 int portal_id; 2122 #endif 2123 int error, client_fd; 2124 2125 for (;;) { 2126 if (sighup_received || sigterm_received || timed_out()) 2127 return; 2128 2129 #ifdef ICL_KERNEL_PROXY 2130 if (proxy_mode) { 2131 client_salen = sizeof(client_sa); 2132 kernel_accept(&connection_id, &portal_id, 2133 (struct sockaddr *)&client_sa, &client_salen); 2134 assert(client_salen >= client_sa.ss_len); 2135 2136 log_debugx("incoming connection, id %d, portal id %d", 2137 connection_id, portal_id); 2138 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2139 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2140 if (portal->p_id == portal_id) { 2141 goto found; 2142 } 2143 } 2144 } 2145 2146 log_errx(1, "kernel returned invalid portal_id %d", 2147 portal_id); 2148 2149 found: 2150 handle_connection(portal, connection_id, 2151 (struct sockaddr *)&client_sa, dont_fork); 2152 } else { 2153 #endif 2154 assert(proxy_mode == false); 2155 2156 error = kevent(kqfd, NULL, 0, &kev, 1, NULL); 2157 if (error == -1) { 2158 if (errno == EINTR) 2159 continue; 2160 log_err(1, "kevent"); 2161 } 2162 2163 switch (kev.filter) { 2164 case EVFILT_READ: 2165 portal = reinterpret_cast<struct portal *>(kev.udata); 2166 assert(portal->p_socket == (int)kev.ident); 2167 2168 client_salen = sizeof(client_sa); 2169 client_fd = accept(portal->p_socket, 2170 (struct sockaddr *)&client_sa, 2171 &client_salen); 2172 if (client_fd < 0) { 2173 if (errno == ECONNABORTED) 2174 continue; 2175 log_err(1, "accept"); 2176 } 2177 assert(client_salen >= client_sa.ss_len); 2178 2179 handle_connection(portal, client_fd, 2180 (struct sockaddr *)&client_sa, dont_fork); 2181 break; 2182 default: 2183 __assert_unreachable(); 2184 } 2185 #ifdef ICL_KERNEL_PROXY 2186 } 2187 #endif 2188 } 2189 } 2190 2191 static void 2192 sighup_handler(int dummy __unused) 2193 { 2194 2195 sighup_received = true; 2196 } 2197 2198 static void 2199 sigterm_handler(int dummy __unused) 2200 { 2201 2202 sigterm_received = true; 2203 } 2204 2205 static void 2206 sigchld_handler(int dummy __unused) 2207 { 2208 2209 /* 2210 * The only purpose of this handler is to make SIGCHLD 2211 * interrupt the ISCSIDWAIT ioctl(2), so we can call 2212 * wait_for_children(). 2213 */ 2214 } 2215 2216 static void 2217 register_signals(void) 2218 { 2219 struct sigaction sa; 2220 int error; 2221 2222 bzero(&sa, sizeof(sa)); 2223 sa.sa_handler = sighup_handler; 2224 sigfillset(&sa.sa_mask); 2225 error = sigaction(SIGHUP, &sa, NULL); 2226 if (error != 0) 2227 log_err(1, "sigaction"); 2228 2229 sa.sa_handler = sigterm_handler; 2230 error = sigaction(SIGTERM, &sa, NULL); 2231 if (error != 0) 2232 log_err(1, "sigaction"); 2233 2234 sa.sa_handler = sigterm_handler; 2235 error = sigaction(SIGINT, &sa, NULL); 2236 if (error != 0) 2237 log_err(1, "sigaction"); 2238 2239 sa.sa_handler = sigchld_handler; 2240 error = sigaction(SIGCHLD, &sa, NULL); 2241 if (error != 0) 2242 log_err(1, "sigaction"); 2243 } 2244 2245 static void 2246 check_perms(const char *path) 2247 { 2248 struct stat sb; 2249 int error; 2250 2251 error = stat(path, &sb); 2252 if (error != 0) { 2253 log_warn("stat"); 2254 return; 2255 } 2256 if (sb.st_mode & S_IWOTH) { 2257 log_warnx("%s is world-writable", path); 2258 } else if (sb.st_mode & S_IROTH) { 2259 log_warnx("%s is world-readable", path); 2260 } else if (sb.st_mode & S_IXOTH) { 2261 /* 2262 * Ok, this one doesn't matter, but still do it, 2263 * just for consistency. 2264 */ 2265 log_warnx("%s is world-executable", path); 2266 } 2267 2268 /* 2269 * XXX: Should we also check for owner != 0? 2270 */ 2271 } 2272 2273 static struct conf * 2274 conf_new_from_file(const char *path, bool ucl) 2275 { 2276 struct conf *conf; 2277 struct auth_group *ag; 2278 struct portal_group *pg; 2279 bool valid; 2280 2281 log_debugx("obtaining configuration from %s", path); 2282 2283 conf = conf_new(); 2284 2285 ag = auth_group_new(conf, "default"); 2286 assert(ag != NULL); 2287 2288 ag = auth_group_new(conf, "no-authentication"); 2289 assert(ag != NULL); 2290 ag->ag_type = AG_TYPE_NO_AUTHENTICATION; 2291 2292 ag = auth_group_new(conf, "no-access"); 2293 assert(ag != NULL); 2294 ag->ag_type = AG_TYPE_DENY; 2295 2296 pg = portal_group_new(conf, "default"); 2297 assert(pg != NULL); 2298 2299 conf_start(conf); 2300 if (ucl) 2301 valid = uclparse_conf(path); 2302 else 2303 valid = parse_conf(path); 2304 conf_finish(); 2305 2306 if (!valid) { 2307 conf_delete(conf); 2308 return (NULL); 2309 } 2310 2311 check_perms(path); 2312 2313 if (conf->conf_default_ag_defined == false) { 2314 log_debugx("auth-group \"default\" not defined; " 2315 "going with defaults"); 2316 ag = auth_group_find(conf, "default"); 2317 assert(ag != NULL); 2318 ag->ag_type = AG_TYPE_DENY; 2319 } 2320 2321 if (conf->conf_default_pg_defined == false) { 2322 log_debugx("portal-group \"default\" not defined; " 2323 "going with defaults"); 2324 pg = portal_group_find(conf, "default"); 2325 assert(pg != NULL); 2326 portal_group_add_portal(pg, "0.0.0.0", false); 2327 portal_group_add_portal(pg, "[::]", false); 2328 } 2329 2330 conf->conf_kernel_port_on = true; 2331 2332 if (!conf_verify(conf)) { 2333 conf_delete(conf); 2334 return (NULL); 2335 } 2336 2337 return (conf); 2338 } 2339 2340 /* 2341 * If the config file specifies physical ports for any target, associate them 2342 * with the config file. If necessary, create them. 2343 */ 2344 static bool 2345 new_pports_from_conf(struct conf *conf, struct kports *kports) 2346 { 2347 struct target *targ; 2348 struct pport *pp; 2349 struct port *tp; 2350 int ret, i_pp, i_vp; 2351 2352 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 2353 if (!targ->t_pport) 2354 continue; 2355 2356 ret = sscanf(targ->t_pport, "ioctl/%d/%d", &i_pp, &i_vp); 2357 if (ret > 0) { 2358 tp = port_new_ioctl(conf, kports, targ, i_pp, i_vp); 2359 if (tp == NULL) { 2360 log_warnx("can't create new ioctl port " 2361 "for target \"%s\"", targ->t_name); 2362 return (false); 2363 } 2364 2365 continue; 2366 } 2367 2368 pp = pport_find(kports, targ->t_pport); 2369 if (pp == NULL) { 2370 log_warnx("unknown port \"%s\" for target \"%s\"", 2371 targ->t_pport, targ->t_name); 2372 return (false); 2373 } 2374 if (!TAILQ_EMPTY(&pp->pp_ports)) { 2375 log_warnx("can't link port \"%s\" to target \"%s\", " 2376 "port already linked to some target", 2377 targ->t_pport, targ->t_name); 2378 return (false); 2379 } 2380 tp = port_new_pp(conf, targ, pp); 2381 if (tp == NULL) { 2382 log_warnx("can't link port \"%s\" to target \"%s\"", 2383 targ->t_pport, targ->t_name); 2384 return (false); 2385 } 2386 } 2387 return (true); 2388 } 2389 2390 int 2391 main(int argc, char **argv) 2392 { 2393 struct kports kports; 2394 struct conf *oldconf, *newconf, *tmpconf; 2395 struct isns *newns; 2396 const char *config_path = DEFAULT_CONFIG_PATH; 2397 int debug = 0, ch, error; 2398 pid_t otherpid; 2399 bool daemonize = true; 2400 bool test_config = false; 2401 bool use_ucl = false; 2402 2403 while ((ch = getopt(argc, argv, "dtuf:R")) != -1) { 2404 switch (ch) { 2405 case 'd': 2406 daemonize = false; 2407 debug++; 2408 break; 2409 case 't': 2410 test_config = true; 2411 break; 2412 case 'u': 2413 use_ucl = true; 2414 break; 2415 case 'f': 2416 config_path = optarg; 2417 break; 2418 case 'R': 2419 #ifndef ICL_KERNEL_PROXY 2420 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY " 2421 "does not support iSER protocol"); 2422 #endif 2423 proxy_mode = true; 2424 break; 2425 case '?': 2426 default: 2427 usage(); 2428 } 2429 } 2430 argc -= optind; 2431 if (argc != 0) 2432 usage(); 2433 2434 log_init(debug); 2435 kernel_init(); 2436 2437 TAILQ_INIT(&kports.pports); 2438 newconf = conf_new_from_file(config_path, use_ucl); 2439 2440 if (newconf == NULL) 2441 log_errx(1, "configuration error; exiting"); 2442 2443 if (test_config) 2444 return (0); 2445 2446 assert(newconf->conf_pidfile_path != NULL); 2447 log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 2448 newconf->conf_pidfh = pidfile_open(newconf->conf_pidfile_path, 0600, 2449 &otherpid); 2450 if (newconf->conf_pidfh == NULL) { 2451 if (errno == EEXIST) 2452 log_errx(1, "daemon already running, pid: %jd.", 2453 (intmax_t)otherpid); 2454 log_err(1, "cannot open or create pidfile \"%s\"", 2455 newconf->conf_pidfile_path); 2456 } 2457 2458 register_signals(); 2459 2460 oldconf = conf_new_from_kernel(&kports); 2461 2462 if (debug > 0) { 2463 oldconf->conf_debug = debug; 2464 newconf->conf_debug = debug; 2465 } 2466 2467 if (!new_pports_from_conf(newconf, &kports)) 2468 log_errx(1, "Error associating physical ports; exiting"); 2469 2470 if (daemonize) { 2471 log_debugx("daemonizing"); 2472 if (daemon(0, 0) == -1) { 2473 log_warn("cannot daemonize"); 2474 pidfile_remove(newconf->conf_pidfh); 2475 exit(1); 2476 } 2477 } 2478 2479 kqfd = kqueue(); 2480 if (kqfd == -1) { 2481 log_warn("Cannot create kqueue"); 2482 pidfile_remove(newconf->conf_pidfh); 2483 exit(1); 2484 } 2485 2486 error = conf_apply(oldconf, newconf); 2487 if (error != 0) 2488 log_errx(1, "failed to apply configuration; exiting"); 2489 2490 conf_delete(oldconf); 2491 oldconf = NULL; 2492 2493 pidfile_write(newconf->conf_pidfh); 2494 2495 /* Schedule iSNS update */ 2496 if (!TAILQ_EMPTY(&newconf->conf_isns)) 2497 set_timeout((newconf->conf_isns_period + 2) / 3, false); 2498 2499 for (;;) { 2500 main_loop(!daemonize); 2501 if (sighup_received) { 2502 sighup_received = false; 2503 log_debugx("received SIGHUP, reloading configuration"); 2504 tmpconf = conf_new_from_file(config_path, use_ucl); 2505 2506 if (tmpconf == NULL) { 2507 log_warnx("configuration error, " 2508 "continuing with old configuration"); 2509 } else if (!new_pports_from_conf(tmpconf, &kports)) { 2510 log_warnx("Error associating physical ports, " 2511 "continuing with old configuration"); 2512 conf_delete(tmpconf); 2513 } else { 2514 if (debug > 0) 2515 tmpconf->conf_debug = debug; 2516 oldconf = newconf; 2517 newconf = tmpconf; 2518 2519 error = conf_apply(oldconf, newconf); 2520 if (error != 0) 2521 log_warnx("failed to reload " 2522 "configuration"); 2523 conf_delete(oldconf); 2524 oldconf = NULL; 2525 } 2526 } else if (sigterm_received) { 2527 log_debugx("exiting on signal; " 2528 "reloading empty configuration"); 2529 2530 log_debugx("removing CTL iSCSI ports " 2531 "and terminating all connections"); 2532 2533 oldconf = newconf; 2534 newconf = conf_new(); 2535 if (debug > 0) 2536 newconf->conf_debug = debug; 2537 error = conf_apply(oldconf, newconf); 2538 if (error != 0) 2539 log_warnx("failed to apply configuration"); 2540 if (oldconf->conf_pidfh) { 2541 pidfile_remove(oldconf->conf_pidfh); 2542 oldconf->conf_pidfh = NULL; 2543 } 2544 conf_delete(newconf); 2545 conf_delete(oldconf); 2546 oldconf = NULL; 2547 2548 log_warnx("exiting on signal"); 2549 exit(0); 2550 } else { 2551 nchildren -= wait_for_children(false); 2552 assert(nchildren >= 0); 2553 if (timed_out()) { 2554 set_timeout(0, false); 2555 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) 2556 isns_check(newns); 2557 /* Schedule iSNS update */ 2558 if (!TAILQ_EMPTY(&newconf->conf_isns)) { 2559 set_timeout((newconf->conf_isns_period 2560 + 2) / 3, 2561 false); 2562 } 2563 } 2564 } 2565 } 2566 /* NOTREACHED */ 2567 } 2568