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