1 /*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/types.h> 35 #include <sys/time.h> 36 #include <sys/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 53 #include "ctld.h" 54 #include "isns.h" 55 56 bool proxy_mode = false; 57 58 static volatile bool sighup_received = false; 59 static volatile bool sigterm_received = false; 60 static volatile bool sigalrm_received = false; 61 62 static int nchildren = 0; 63 static uint16_t last_portal_group_tag = 0xff; 64 65 static void 66 usage(void) 67 { 68 69 fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n"); 70 exit(1); 71 } 72 73 char * 74 checked_strdup(const char *s) 75 { 76 char *c; 77 78 c = strdup(s); 79 if (c == NULL) 80 log_err(1, "strdup"); 81 return (c); 82 } 83 84 struct conf * 85 conf_new(void) 86 { 87 struct conf *conf; 88 89 conf = calloc(1, sizeof(*conf)); 90 if (conf == NULL) 91 log_err(1, "calloc"); 92 TAILQ_INIT(&conf->conf_luns); 93 TAILQ_INIT(&conf->conf_targets); 94 TAILQ_INIT(&conf->conf_auth_groups); 95 TAILQ_INIT(&conf->conf_ports); 96 TAILQ_INIT(&conf->conf_portal_groups); 97 TAILQ_INIT(&conf->conf_pports); 98 TAILQ_INIT(&conf->conf_isns); 99 100 conf->conf_isns_period = 900; 101 conf->conf_isns_timeout = 5; 102 conf->conf_debug = 0; 103 conf->conf_timeout = 60; 104 conf->conf_maxproc = 30; 105 106 return (conf); 107 } 108 109 void 110 conf_delete(struct conf *conf) 111 { 112 struct lun *lun, *ltmp; 113 struct target *targ, *tmp; 114 struct auth_group *ag, *cagtmp; 115 struct portal_group *pg, *cpgtmp; 116 struct pport *pp, *pptmp; 117 struct isns *is, *istmp; 118 119 assert(conf->conf_pidfh == NULL); 120 121 TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp) 122 lun_delete(lun); 123 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp) 124 target_delete(targ); 125 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp) 126 auth_group_delete(ag); 127 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp) 128 portal_group_delete(pg); 129 TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp) 130 pport_delete(pp); 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_initator_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_initator_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_initator_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_initator_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_initator_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 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 532 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 533 return (ag); 534 } 535 536 return (NULL); 537 } 538 539 int 540 auth_group_set_type(struct auth_group *ag, const char *str) 541 { 542 int type; 543 544 if (strcmp(str, "none") == 0) { 545 type = AG_TYPE_NO_AUTHENTICATION; 546 } else if (strcmp(str, "deny") == 0) { 547 type = AG_TYPE_DENY; 548 } else if (strcmp(str, "chap") == 0) { 549 type = AG_TYPE_CHAP; 550 } else if (strcmp(str, "chap-mutual") == 0) { 551 type = AG_TYPE_CHAP_MUTUAL; 552 } else { 553 if (ag->ag_name != NULL) 554 log_warnx("invalid auth-type \"%s\" for auth-group " 555 "\"%s\"", str, ag->ag_name); 556 else 557 log_warnx("invalid auth-type \"%s\" for target " 558 "\"%s\"", str, ag->ag_target->t_name); 559 return (1); 560 } 561 562 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) { 563 if (ag->ag_name != NULL) { 564 log_warnx("cannot set auth-type to \"%s\" for " 565 "auth-group \"%s\"; already has a different " 566 "type", str, ag->ag_name); 567 } else { 568 log_warnx("cannot set auth-type to \"%s\" for target " 569 "\"%s\"; already has a different type", 570 str, ag->ag_target->t_name); 571 } 572 return (1); 573 } 574 575 ag->ag_type = type; 576 577 return (0); 578 } 579 580 static struct portal * 581 portal_new(struct portal_group *pg) 582 { 583 struct portal *portal; 584 585 portal = calloc(1, sizeof(*portal)); 586 if (portal == NULL) 587 log_err(1, "calloc"); 588 TAILQ_INIT(&portal->p_targets); 589 portal->p_portal_group = pg; 590 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 591 return (portal); 592 } 593 594 static void 595 portal_delete(struct portal *portal) 596 { 597 598 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 599 if (portal->p_ai != NULL) 600 freeaddrinfo(portal->p_ai); 601 free(portal->p_listen); 602 free(portal); 603 } 604 605 struct portal_group * 606 portal_group_new(struct conf *conf, const char *name) 607 { 608 struct portal_group *pg; 609 610 pg = portal_group_find(conf, name); 611 if (pg != NULL) { 612 log_warnx("duplicated portal-group \"%s\"", name); 613 return (NULL); 614 } 615 616 pg = calloc(1, sizeof(*pg)); 617 if (pg == NULL) 618 log_err(1, "calloc"); 619 pg->pg_name = checked_strdup(name); 620 TAILQ_INIT(&pg->pg_options); 621 TAILQ_INIT(&pg->pg_portals); 622 TAILQ_INIT(&pg->pg_ports); 623 pg->pg_conf = conf; 624 pg->pg_tag = 0; /* Assigned later in conf_apply(). */ 625 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 626 627 return (pg); 628 } 629 630 void 631 portal_group_delete(struct portal_group *pg) 632 { 633 struct portal *portal, *tmp; 634 struct port *port, *tport; 635 struct option *o, *otmp; 636 637 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport) 638 port_delete(port); 639 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 640 641 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 642 portal_delete(portal); 643 TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp) 644 option_delete(&pg->pg_options, o); 645 free(pg->pg_name); 646 free(pg->pg_offload); 647 free(pg->pg_redirection); 648 free(pg); 649 } 650 651 struct portal_group * 652 portal_group_find(const struct conf *conf, const char *name) 653 { 654 struct portal_group *pg; 655 656 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 657 if (strcmp(pg->pg_name, name) == 0) 658 return (pg); 659 } 660 661 return (NULL); 662 } 663 664 static int 665 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai) 666 { 667 struct addrinfo hints; 668 char *str, *addr, *ch; 669 const char *port; 670 int error, colons = 0; 671 672 str = arg = strdup(arg); 673 if (arg[0] == '[') { 674 /* 675 * IPv6 address in square brackets, perhaps with port. 676 */ 677 arg++; 678 addr = strsep(&arg, "]"); 679 if (arg == NULL) { 680 free(str); 681 return (1); 682 } 683 if (arg[0] == '\0') { 684 port = def_port; 685 } else if (arg[0] == ':') { 686 port = arg + 1; 687 } else { 688 free(str); 689 return (1); 690 } 691 } else { 692 /* 693 * Either IPv6 address without brackets - and without 694 * a port - or IPv4 address. Just count the colons. 695 */ 696 for (ch = arg; *ch != '\0'; ch++) { 697 if (*ch == ':') 698 colons++; 699 } 700 if (colons > 1) { 701 addr = arg; 702 port = def_port; 703 } else { 704 addr = strsep(&arg, ":"); 705 if (arg == NULL) 706 port = def_port; 707 else 708 port = arg; 709 } 710 } 711 712 memset(&hints, 0, sizeof(hints)); 713 hints.ai_family = PF_UNSPEC; 714 hints.ai_socktype = SOCK_STREAM; 715 hints.ai_flags = AI_PASSIVE; 716 error = getaddrinfo(addr, port, &hints, ai); 717 free(str); 718 return ((error != 0) ? 1 : 0); 719 } 720 721 int 722 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 723 { 724 struct portal *portal; 725 726 portal = portal_new(pg); 727 portal->p_listen = checked_strdup(value); 728 portal->p_iser = iser; 729 730 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) { 731 log_warnx("invalid listen address %s", portal->p_listen); 732 portal_delete(portal); 733 return (1); 734 } 735 736 /* 737 * XXX: getaddrinfo(3) may return multiple addresses; we should turn 738 * those into multiple portals. 739 */ 740 741 return (0); 742 } 743 744 int 745 isns_new(struct conf *conf, const char *addr) 746 { 747 struct isns *isns; 748 749 isns = calloc(1, sizeof(*isns)); 750 if (isns == NULL) 751 log_err(1, "calloc"); 752 isns->i_conf = conf; 753 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next); 754 isns->i_addr = checked_strdup(addr); 755 756 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) { 757 log_warnx("invalid iSNS address %s", isns->i_addr); 758 isns_delete(isns); 759 return (1); 760 } 761 762 /* 763 * XXX: getaddrinfo(3) may return multiple addresses; we should turn 764 * those into multiple servers. 765 */ 766 767 return (0); 768 } 769 770 void 771 isns_delete(struct isns *isns) 772 { 773 774 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next); 775 free(isns->i_addr); 776 if (isns->i_ai != NULL) 777 freeaddrinfo(isns->i_ai); 778 free(isns); 779 } 780 781 static int 782 isns_do_connect(struct isns *isns) 783 { 784 int s; 785 786 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype, 787 isns->i_ai->ai_protocol); 788 if (s < 0) { 789 log_warn("socket(2) failed for %s", isns->i_addr); 790 return (-1); 791 } 792 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) { 793 log_warn("connect(2) failed for %s", isns->i_addr); 794 close(s); 795 return (-1); 796 } 797 return(s); 798 } 799 800 static int 801 isns_do_register(struct isns *isns, int s, const char *hostname) 802 { 803 struct conf *conf = isns->i_conf; 804 struct target *target; 805 struct portal *portal; 806 struct portal_group *pg; 807 struct port *port; 808 struct isns_req *req; 809 int res = 0; 810 uint32_t error; 811 812 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT); 813 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 814 isns_req_add_delim(req); 815 isns_req_add_str(req, 1, hostname); 816 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */ 817 isns_req_add_32(req, 6, conf->conf_isns_period); 818 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 819 if (pg->pg_unassigned) 820 continue; 821 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 822 isns_req_add_addr(req, 16, portal->p_ai); 823 isns_req_add_port(req, 17, portal->p_ai); 824 } 825 } 826 TAILQ_FOREACH(target, &conf->conf_targets, t_next) { 827 isns_req_add_str(req, 32, target->t_name); 828 isns_req_add_32(req, 33, 1); /* 1 -- Target*/ 829 if (target->t_alias != NULL) 830 isns_req_add_str(req, 34, target->t_alias); 831 TAILQ_FOREACH(port, &target->t_ports, p_ts) { 832 if ((pg = port->p_portal_group) == NULL) 833 continue; 834 isns_req_add_32(req, 51, pg->pg_tag); 835 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 836 isns_req_add_addr(req, 49, portal->p_ai); 837 isns_req_add_port(req, 50, portal->p_ai); 838 } 839 } 840 } 841 res = isns_req_send(s, req); 842 if (res < 0) { 843 log_warn("send(2) failed for %s", isns->i_addr); 844 goto quit; 845 } 846 res = isns_req_receive(s, req); 847 if (res < 0) { 848 log_warn("receive(2) failed for %s", isns->i_addr); 849 goto quit; 850 } 851 error = isns_req_get_status(req); 852 if (error != 0) { 853 log_warnx("iSNS register error %d for %s", error, isns->i_addr); 854 res = -1; 855 } 856 quit: 857 isns_req_free(req); 858 return (res); 859 } 860 861 static int 862 isns_do_check(struct isns *isns, int s, const char *hostname) 863 { 864 struct conf *conf = isns->i_conf; 865 struct isns_req *req; 866 int res = 0; 867 uint32_t error; 868 869 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT); 870 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 871 isns_req_add_str(req, 1, hostname); 872 isns_req_add_delim(req); 873 isns_req_add(req, 2, 0, NULL); 874 res = isns_req_send(s, req); 875 if (res < 0) { 876 log_warn("send(2) failed for %s", isns->i_addr); 877 goto quit; 878 } 879 res = isns_req_receive(s, req); 880 if (res < 0) { 881 log_warn("receive(2) failed for %s", isns->i_addr); 882 goto quit; 883 } 884 error = isns_req_get_status(req); 885 if (error != 0) { 886 log_warnx("iSNS check error %d for %s", error, isns->i_addr); 887 res = -1; 888 } 889 quit: 890 isns_req_free(req); 891 return (res); 892 } 893 894 static int 895 isns_do_deregister(struct isns *isns, int s, const char *hostname) 896 { 897 struct conf *conf = isns->i_conf; 898 struct isns_req *req; 899 int res = 0; 900 uint32_t error; 901 902 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT); 903 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 904 isns_req_add_delim(req); 905 isns_req_add_str(req, 1, hostname); 906 res = isns_req_send(s, req); 907 if (res < 0) { 908 log_warn("send(2) failed for %s", isns->i_addr); 909 goto quit; 910 } 911 res = isns_req_receive(s, req); 912 if (res < 0) { 913 log_warn("receive(2) failed for %s", isns->i_addr); 914 goto quit; 915 } 916 error = isns_req_get_status(req); 917 if (error != 0) { 918 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr); 919 res = -1; 920 } 921 quit: 922 isns_req_free(req); 923 return (res); 924 } 925 926 void 927 isns_register(struct isns *isns, struct isns *oldisns) 928 { 929 struct conf *conf = isns->i_conf; 930 int s; 931 char hostname[256]; 932 933 if (TAILQ_EMPTY(&conf->conf_targets) || 934 TAILQ_EMPTY(&conf->conf_portal_groups)) 935 return; 936 set_timeout(conf->conf_isns_timeout, false); 937 s = isns_do_connect(isns); 938 if (s < 0) { 939 set_timeout(0, false); 940 return; 941 } 942 gethostname(hostname, sizeof(hostname)); 943 944 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets)) 945 oldisns = isns; 946 isns_do_deregister(oldisns, s, hostname); 947 isns_do_register(isns, s, hostname); 948 close(s); 949 set_timeout(0, false); 950 } 951 952 void 953 isns_check(struct isns *isns) 954 { 955 struct conf *conf = isns->i_conf; 956 int s, res; 957 char hostname[256]; 958 959 if (TAILQ_EMPTY(&conf->conf_targets) || 960 TAILQ_EMPTY(&conf->conf_portal_groups)) 961 return; 962 set_timeout(conf->conf_isns_timeout, false); 963 s = isns_do_connect(isns); 964 if (s < 0) { 965 set_timeout(0, false); 966 return; 967 } 968 gethostname(hostname, sizeof(hostname)); 969 970 res = isns_do_check(isns, s, hostname); 971 if (res < 0) { 972 isns_do_deregister(isns, s, hostname); 973 isns_do_register(isns, s, hostname); 974 } 975 close(s); 976 set_timeout(0, false); 977 } 978 979 void 980 isns_deregister(struct isns *isns) 981 { 982 struct conf *conf = isns->i_conf; 983 int s; 984 char hostname[256]; 985 986 if (TAILQ_EMPTY(&conf->conf_targets) || 987 TAILQ_EMPTY(&conf->conf_portal_groups)) 988 return; 989 set_timeout(conf->conf_isns_timeout, false); 990 s = isns_do_connect(isns); 991 if (s < 0) 992 return; 993 gethostname(hostname, sizeof(hostname)); 994 995 isns_do_deregister(isns, s, hostname); 996 close(s); 997 set_timeout(0, false); 998 } 999 1000 int 1001 portal_group_set_filter(struct portal_group *pg, const char *str) 1002 { 1003 int filter; 1004 1005 if (strcmp(str, "none") == 0) { 1006 filter = PG_FILTER_NONE; 1007 } else if (strcmp(str, "portal") == 0) { 1008 filter = PG_FILTER_PORTAL; 1009 } else if (strcmp(str, "portal-name") == 0) { 1010 filter = PG_FILTER_PORTAL_NAME; 1011 } else if (strcmp(str, "portal-name-auth") == 0) { 1012 filter = PG_FILTER_PORTAL_NAME_AUTH; 1013 } else { 1014 log_warnx("invalid discovery-filter \"%s\" for portal-group " 1015 "\"%s\"; valid values are \"none\", \"portal\", " 1016 "\"portal-name\", and \"portal-name-auth\"", 1017 str, pg->pg_name); 1018 return (1); 1019 } 1020 1021 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN && 1022 pg->pg_discovery_filter != filter) { 1023 log_warnx("cannot set discovery-filter to \"%s\" for " 1024 "portal-group \"%s\"; already has a different " 1025 "value", str, pg->pg_name); 1026 return (1); 1027 } 1028 1029 pg->pg_discovery_filter = filter; 1030 1031 return (0); 1032 } 1033 1034 int 1035 portal_group_set_offload(struct portal_group *pg, const char *offload) 1036 { 1037 1038 if (pg->pg_offload != NULL) { 1039 log_warnx("cannot set offload to \"%s\" for " 1040 "portal-group \"%s\"; already defined", 1041 offload, pg->pg_name); 1042 return (1); 1043 } 1044 1045 pg->pg_offload = checked_strdup(offload); 1046 1047 return (0); 1048 } 1049 1050 int 1051 portal_group_set_redirection(struct portal_group *pg, const char *addr) 1052 { 1053 1054 if (pg->pg_redirection != NULL) { 1055 log_warnx("cannot set redirection to \"%s\" for " 1056 "portal-group \"%s\"; already defined", 1057 addr, pg->pg_name); 1058 return (1); 1059 } 1060 1061 pg->pg_redirection = checked_strdup(addr); 1062 1063 return (0); 1064 } 1065 1066 static bool 1067 valid_hex(const char ch) 1068 { 1069 switch (ch) { 1070 case '0': 1071 case '1': 1072 case '2': 1073 case '3': 1074 case '4': 1075 case '5': 1076 case '6': 1077 case '7': 1078 case '8': 1079 case '9': 1080 case 'a': 1081 case 'A': 1082 case 'b': 1083 case 'B': 1084 case 'c': 1085 case 'C': 1086 case 'd': 1087 case 'D': 1088 case 'e': 1089 case 'E': 1090 case 'f': 1091 case 'F': 1092 return (true); 1093 default: 1094 return (false); 1095 } 1096 } 1097 1098 bool 1099 valid_iscsi_name(const char *name) 1100 { 1101 int i; 1102 1103 if (strlen(name) >= MAX_NAME_LEN) { 1104 log_warnx("overlong name for target \"%s\"; max length allowed " 1105 "by iSCSI specification is %d characters", 1106 name, MAX_NAME_LEN); 1107 return (false); 1108 } 1109 1110 /* 1111 * In the cases below, we don't return an error, just in case the admin 1112 * was right, and we're wrong. 1113 */ 1114 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 1115 for (i = strlen("iqn."); name[i] != '\0'; i++) { 1116 /* 1117 * XXX: We should verify UTF-8 normalisation, as defined 1118 * by 3.2.6.2: iSCSI Name Encoding. 1119 */ 1120 if (isalnum(name[i])) 1121 continue; 1122 if (name[i] == '-' || name[i] == '.' || name[i] == ':') 1123 continue; 1124 log_warnx("invalid character \"%c\" in target name " 1125 "\"%s\"; allowed characters are letters, digits, " 1126 "'-', '.', and ':'", name[i], name); 1127 break; 1128 } 1129 /* 1130 * XXX: Check more stuff: valid date and a valid reversed domain. 1131 */ 1132 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 1133 if (strlen(name) != strlen("eui.") + 16) 1134 log_warnx("invalid target name \"%s\"; the \"eui.\" " 1135 "should be followed by exactly 16 hexadecimal " 1136 "digits", name); 1137 for (i = strlen("eui."); name[i] != '\0'; i++) { 1138 if (!valid_hex(name[i])) { 1139 log_warnx("invalid character \"%c\" in target " 1140 "name \"%s\"; allowed characters are 1-9 " 1141 "and A-F", name[i], name); 1142 break; 1143 } 1144 } 1145 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 1146 if (strlen(name) > strlen("naa.") + 32) 1147 log_warnx("invalid target name \"%s\"; the \"naa.\" " 1148 "should be followed by at most 32 hexadecimal " 1149 "digits", name); 1150 for (i = strlen("naa."); name[i] != '\0'; i++) { 1151 if (!valid_hex(name[i])) { 1152 log_warnx("invalid character \"%c\" in target " 1153 "name \"%s\"; allowed characters are 1-9 " 1154 "and A-F", name[i], name); 1155 break; 1156 } 1157 } 1158 } else { 1159 log_warnx("invalid target name \"%s\"; should start with " 1160 "either \"iqn.\", \"eui.\", or \"naa.\"", 1161 name); 1162 } 1163 return (true); 1164 } 1165 1166 struct pport * 1167 pport_new(struct conf *conf, const char *name, uint32_t ctl_port) 1168 { 1169 struct pport *pp; 1170 1171 pp = calloc(1, sizeof(*pp)); 1172 if (pp == NULL) 1173 log_err(1, "calloc"); 1174 pp->pp_conf = conf; 1175 pp->pp_name = checked_strdup(name); 1176 pp->pp_ctl_port = ctl_port; 1177 TAILQ_INIT(&pp->pp_ports); 1178 TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next); 1179 return (pp); 1180 } 1181 1182 struct pport * 1183 pport_find(const struct conf *conf, const char *name) 1184 { 1185 struct pport *pp; 1186 1187 TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) { 1188 if (strcasecmp(pp->pp_name, name) == 0) 1189 return (pp); 1190 } 1191 return (NULL); 1192 } 1193 1194 struct pport * 1195 pport_copy(struct pport *pp, struct conf *conf) 1196 { 1197 struct pport *ppnew; 1198 1199 ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port); 1200 return (ppnew); 1201 } 1202 1203 void 1204 pport_delete(struct pport *pp) 1205 { 1206 struct port *port, *tport; 1207 1208 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport) 1209 port_delete(port); 1210 TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next); 1211 free(pp->pp_name); 1212 free(pp); 1213 } 1214 1215 struct port * 1216 port_new(struct conf *conf, struct target *target, struct portal_group *pg) 1217 { 1218 struct port *port; 1219 char *name; 1220 int ret; 1221 1222 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name); 1223 if (ret <= 0) 1224 log_err(1, "asprintf"); 1225 if (port_find(conf, name) != NULL) { 1226 log_warnx("duplicate port \"%s\"", name); 1227 free(name); 1228 return (NULL); 1229 } 1230 port = calloc(1, sizeof(*port)); 1231 if (port == NULL) 1232 log_err(1, "calloc"); 1233 port->p_conf = conf; 1234 port->p_name = name; 1235 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1236 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1237 port->p_target = target; 1238 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs); 1239 port->p_portal_group = pg; 1240 port->p_foreign = pg->pg_foreign; 1241 return (port); 1242 } 1243 1244 struct port * 1245 port_new_pp(struct conf *conf, struct target *target, struct pport *pp) 1246 { 1247 struct port *port; 1248 char *name; 1249 int ret; 1250 1251 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name); 1252 if (ret <= 0) 1253 log_err(1, "asprintf"); 1254 if (port_find(conf, name) != NULL) { 1255 log_warnx("duplicate port \"%s\"", name); 1256 free(name); 1257 return (NULL); 1258 } 1259 port = calloc(1, sizeof(*port)); 1260 if (port == NULL) 1261 log_err(1, "calloc"); 1262 port->p_conf = conf; 1263 port->p_name = name; 1264 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1265 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1266 port->p_target = target; 1267 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps); 1268 port->p_pport = pp; 1269 return (port); 1270 } 1271 1272 struct port * 1273 port_find(const struct conf *conf, const char *name) 1274 { 1275 struct port *port; 1276 1277 TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 1278 if (strcasecmp(port->p_name, name) == 0) 1279 return (port); 1280 } 1281 1282 return (NULL); 1283 } 1284 1285 struct port * 1286 port_find_in_pg(const struct portal_group *pg, const char *target) 1287 { 1288 struct port *port; 1289 1290 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) { 1291 if (strcasecmp(port->p_target->t_name, target) == 0) 1292 return (port); 1293 } 1294 1295 return (NULL); 1296 } 1297 1298 void 1299 port_delete(struct port *port) 1300 { 1301 1302 if (port->p_portal_group) 1303 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs); 1304 if (port->p_pport) 1305 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps); 1306 if (port->p_target) 1307 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts); 1308 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next); 1309 free(port->p_name); 1310 free(port); 1311 } 1312 1313 struct target * 1314 target_new(struct conf *conf, const char *name) 1315 { 1316 struct target *targ; 1317 int i, len; 1318 1319 targ = target_find(conf, name); 1320 if (targ != NULL) { 1321 log_warnx("duplicated target \"%s\"", name); 1322 return (NULL); 1323 } 1324 if (valid_iscsi_name(name) == false) { 1325 log_warnx("target name \"%s\" is invalid", name); 1326 return (NULL); 1327 } 1328 targ = calloc(1, sizeof(*targ)); 1329 if (targ == NULL) 1330 log_err(1, "calloc"); 1331 targ->t_name = checked_strdup(name); 1332 1333 /* 1334 * RFC 3722 requires us to normalize the name to lowercase. 1335 */ 1336 len = strlen(name); 1337 for (i = 0; i < len; i++) 1338 targ->t_name[i] = tolower(targ->t_name[i]); 1339 1340 targ->t_conf = conf; 1341 TAILQ_INIT(&targ->t_ports); 1342 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 1343 1344 return (targ); 1345 } 1346 1347 void 1348 target_delete(struct target *targ) 1349 { 1350 struct port *port, *tport; 1351 1352 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport) 1353 port_delete(port); 1354 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 1355 1356 free(targ->t_name); 1357 free(targ->t_redirection); 1358 free(targ); 1359 } 1360 1361 struct target * 1362 target_find(struct conf *conf, const char *name) 1363 { 1364 struct target *targ; 1365 1366 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1367 if (strcasecmp(targ->t_name, name) == 0) 1368 return (targ); 1369 } 1370 1371 return (NULL); 1372 } 1373 1374 int 1375 target_set_redirection(struct target *target, const char *addr) 1376 { 1377 1378 if (target->t_redirection != NULL) { 1379 log_warnx("cannot set redirection to \"%s\" for " 1380 "target \"%s\"; already defined", 1381 addr, target->t_name); 1382 return (1); 1383 } 1384 1385 target->t_redirection = checked_strdup(addr); 1386 1387 return (0); 1388 } 1389 1390 struct lun * 1391 lun_new(struct conf *conf, const char *name) 1392 { 1393 struct lun *lun; 1394 1395 lun = lun_find(conf, name); 1396 if (lun != NULL) { 1397 log_warnx("duplicated lun \"%s\"", name); 1398 return (NULL); 1399 } 1400 1401 lun = calloc(1, sizeof(*lun)); 1402 if (lun == NULL) 1403 log_err(1, "calloc"); 1404 lun->l_conf = conf; 1405 lun->l_name = checked_strdup(name); 1406 TAILQ_INIT(&lun->l_options); 1407 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next); 1408 lun->l_ctl_lun = -1; 1409 1410 return (lun); 1411 } 1412 1413 void 1414 lun_delete(struct lun *lun) 1415 { 1416 struct target *targ; 1417 struct option *o, *tmp; 1418 int i; 1419 1420 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) { 1421 for (i = 0; i < MAX_LUNS; i++) { 1422 if (targ->t_luns[i] == lun) 1423 targ->t_luns[i] = NULL; 1424 } 1425 } 1426 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next); 1427 1428 TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp) 1429 option_delete(&lun->l_options, o); 1430 free(lun->l_name); 1431 free(lun->l_backend); 1432 free(lun->l_device_id); 1433 free(lun->l_path); 1434 free(lun->l_scsiname); 1435 free(lun->l_serial); 1436 free(lun); 1437 } 1438 1439 struct lun * 1440 lun_find(const struct conf *conf, const char *name) 1441 { 1442 struct lun *lun; 1443 1444 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1445 if (strcmp(lun->l_name, name) == 0) 1446 return (lun); 1447 } 1448 1449 return (NULL); 1450 } 1451 1452 void 1453 lun_set_backend(struct lun *lun, const char *value) 1454 { 1455 free(lun->l_backend); 1456 lun->l_backend = checked_strdup(value); 1457 } 1458 1459 void 1460 lun_set_blocksize(struct lun *lun, size_t value) 1461 { 1462 1463 lun->l_blocksize = value; 1464 } 1465 1466 void 1467 lun_set_device_type(struct lun *lun, uint8_t value) 1468 { 1469 1470 lun->l_device_type = value; 1471 } 1472 1473 void 1474 lun_set_device_id(struct lun *lun, const char *value) 1475 { 1476 free(lun->l_device_id); 1477 lun->l_device_id = checked_strdup(value); 1478 } 1479 1480 void 1481 lun_set_path(struct lun *lun, const char *value) 1482 { 1483 free(lun->l_path); 1484 lun->l_path = checked_strdup(value); 1485 } 1486 1487 void 1488 lun_set_scsiname(struct lun *lun, const char *value) 1489 { 1490 free(lun->l_scsiname); 1491 lun->l_scsiname = checked_strdup(value); 1492 } 1493 1494 void 1495 lun_set_serial(struct lun *lun, const char *value) 1496 { 1497 free(lun->l_serial); 1498 lun->l_serial = checked_strdup(value); 1499 } 1500 1501 void 1502 lun_set_size(struct lun *lun, size_t value) 1503 { 1504 1505 lun->l_size = value; 1506 } 1507 1508 void 1509 lun_set_ctl_lun(struct lun *lun, uint32_t value) 1510 { 1511 1512 lun->l_ctl_lun = value; 1513 } 1514 1515 struct option * 1516 option_new(struct options *options, const char *name, const char *value) 1517 { 1518 struct option *o; 1519 1520 o = option_find(options, name); 1521 if (o != NULL) { 1522 log_warnx("duplicated option \"%s\"", name); 1523 return (NULL); 1524 } 1525 1526 o = calloc(1, sizeof(*o)); 1527 if (o == NULL) 1528 log_err(1, "calloc"); 1529 o->o_name = checked_strdup(name); 1530 o->o_value = checked_strdup(value); 1531 TAILQ_INSERT_TAIL(options, o, o_next); 1532 1533 return (o); 1534 } 1535 1536 void 1537 option_delete(struct options *options, struct option *o) 1538 { 1539 1540 TAILQ_REMOVE(options, o, o_next); 1541 free(o->o_name); 1542 free(o->o_value); 1543 free(o); 1544 } 1545 1546 struct option * 1547 option_find(const struct options *options, const char *name) 1548 { 1549 struct option *o; 1550 1551 TAILQ_FOREACH(o, options, o_next) { 1552 if (strcmp(o->o_name, name) == 0) 1553 return (o); 1554 } 1555 1556 return (NULL); 1557 } 1558 1559 void 1560 option_set(struct option *o, const char *value) 1561 { 1562 1563 free(o->o_value); 1564 o->o_value = checked_strdup(value); 1565 } 1566 1567 static struct connection * 1568 connection_new(struct portal *portal, int fd, const char *host, 1569 const struct sockaddr *client_sa) 1570 { 1571 struct connection *conn; 1572 1573 conn = calloc(1, sizeof(*conn)); 1574 if (conn == NULL) 1575 log_err(1, "calloc"); 1576 conn->conn_portal = portal; 1577 conn->conn_socket = fd; 1578 conn->conn_initiator_addr = checked_strdup(host); 1579 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len); 1580 1581 /* 1582 * Default values, from RFC 3720, section 12. 1583 */ 1584 conn->conn_max_recv_data_segment_length = 8192; 1585 conn->conn_max_send_data_segment_length = 8192; 1586 conn->conn_max_burst_length = 262144; 1587 conn->conn_first_burst_length = 65536; 1588 conn->conn_immediate_data = true; 1589 1590 return (conn); 1591 } 1592 1593 #if 0 1594 static void 1595 conf_print(struct conf *conf) 1596 { 1597 struct auth_group *ag; 1598 struct auth *auth; 1599 struct auth_name *auth_name; 1600 struct auth_portal *auth_portal; 1601 struct portal_group *pg; 1602 struct portal *portal; 1603 struct target *targ; 1604 struct lun *lun; 1605 struct option *o; 1606 1607 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1608 fprintf(stderr, "auth-group %s {\n", ag->ag_name); 1609 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) 1610 fprintf(stderr, "\t chap-mutual %s %s %s %s\n", 1611 auth->a_user, auth->a_secret, 1612 auth->a_mutual_user, auth->a_mutual_secret); 1613 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) 1614 fprintf(stderr, "\t initiator-name %s\n", 1615 auth_name->an_initator_name); 1616 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next) 1617 fprintf(stderr, "\t initiator-portal %s\n", 1618 auth_portal->an_initator_portal); 1619 fprintf(stderr, "}\n"); 1620 } 1621 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1622 fprintf(stderr, "portal-group %s {\n", pg->pg_name); 1623 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1624 fprintf(stderr, "\t listen %s\n", portal->p_listen); 1625 fprintf(stderr, "}\n"); 1626 } 1627 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1628 fprintf(stderr, "\tlun %s {\n", lun->l_name); 1629 fprintf(stderr, "\t\tpath %s\n", lun->l_path); 1630 TAILQ_FOREACH(o, &lun->l_options, o_next) 1631 fprintf(stderr, "\t\toption %s %s\n", 1632 lo->o_name, lo->o_value); 1633 fprintf(stderr, "\t}\n"); 1634 } 1635 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1636 fprintf(stderr, "target %s {\n", targ->t_name); 1637 if (targ->t_alias != NULL) 1638 fprintf(stderr, "\t alias %s\n", targ->t_alias); 1639 fprintf(stderr, "}\n"); 1640 } 1641 } 1642 #endif 1643 1644 static int 1645 conf_verify_lun(struct lun *lun) 1646 { 1647 const struct lun *lun2; 1648 1649 if (lun->l_backend == NULL) 1650 lun_set_backend(lun, "block"); 1651 if (strcmp(lun->l_backend, "block") == 0) { 1652 if (lun->l_path == NULL) { 1653 log_warnx("missing path for lun \"%s\"", 1654 lun->l_name); 1655 return (1); 1656 } 1657 } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 1658 if (lun->l_size == 0) { 1659 log_warnx("missing size for ramdisk-backed lun \"%s\"", 1660 lun->l_name); 1661 return (1); 1662 } 1663 if (lun->l_path != NULL) { 1664 log_warnx("path must not be specified " 1665 "for ramdisk-backed lun \"%s\"", 1666 lun->l_name); 1667 return (1); 1668 } 1669 } 1670 if (lun->l_blocksize == 0) { 1671 if (lun->l_device_type == 5) 1672 lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE); 1673 else 1674 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 1675 } else if (lun->l_blocksize < 0) { 1676 log_warnx("invalid blocksize for lun \"%s\"; " 1677 "must be larger than 0", lun->l_name); 1678 return (1); 1679 } 1680 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 1681 log_warnx("invalid size for lun \"%s\"; " 1682 "must be multiple of blocksize", lun->l_name); 1683 return (1); 1684 } 1685 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) { 1686 if (lun == lun2) 1687 continue; 1688 if (lun->l_path != NULL && lun2->l_path != NULL && 1689 strcmp(lun->l_path, lun2->l_path) == 0) { 1690 log_debugx("WARNING: path \"%s\" duplicated " 1691 "between lun \"%s\", and " 1692 "lun \"%s\"", lun->l_path, 1693 lun->l_name, lun2->l_name); 1694 } 1695 } 1696 1697 return (0); 1698 } 1699 1700 int 1701 conf_verify(struct conf *conf) 1702 { 1703 struct auth_group *ag; 1704 struct portal_group *pg; 1705 struct port *port; 1706 struct target *targ; 1707 struct lun *lun; 1708 bool found; 1709 int error, i; 1710 1711 if (conf->conf_pidfile_path == NULL) 1712 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1713 1714 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1715 error = conf_verify_lun(lun); 1716 if (error != 0) 1717 return (error); 1718 } 1719 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1720 if (targ->t_auth_group == NULL) { 1721 targ->t_auth_group = auth_group_find(conf, 1722 "default"); 1723 assert(targ->t_auth_group != NULL); 1724 } 1725 if (TAILQ_EMPTY(&targ->t_ports)) { 1726 pg = portal_group_find(conf, "default"); 1727 assert(pg != NULL); 1728 port_new(conf, targ, pg); 1729 } 1730 found = false; 1731 for (i = 0; i < MAX_LUNS; i++) { 1732 if (targ->t_luns[i] != NULL) 1733 found = true; 1734 } 1735 if (!found && targ->t_redirection == NULL) { 1736 log_warnx("no LUNs defined for target \"%s\"", 1737 targ->t_name); 1738 } 1739 if (found && targ->t_redirection != NULL) { 1740 log_debugx("target \"%s\" contains luns, " 1741 " but configured for redirection", 1742 targ->t_name); 1743 } 1744 } 1745 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1746 assert(pg->pg_name != NULL); 1747 if (pg->pg_discovery_auth_group == NULL) { 1748 pg->pg_discovery_auth_group = 1749 auth_group_find(conf, "default"); 1750 assert(pg->pg_discovery_auth_group != NULL); 1751 } 1752 1753 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN) 1754 pg->pg_discovery_filter = PG_FILTER_NONE; 1755 1756 if (pg->pg_redirection != NULL) { 1757 if (!TAILQ_EMPTY(&pg->pg_ports)) { 1758 log_debugx("portal-group \"%s\" assigned " 1759 "to target, but configured " 1760 "for redirection", 1761 pg->pg_name); 1762 } 1763 pg->pg_unassigned = false; 1764 } else if (!TAILQ_EMPTY(&pg->pg_ports)) { 1765 pg->pg_unassigned = false; 1766 } else { 1767 if (strcmp(pg->pg_name, "default") != 0) 1768 log_warnx("portal-group \"%s\" not assigned " 1769 "to any target", pg->pg_name); 1770 pg->pg_unassigned = true; 1771 } 1772 } 1773 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1774 if (ag->ag_name == NULL) 1775 assert(ag->ag_target != NULL); 1776 else 1777 assert(ag->ag_target == NULL); 1778 1779 found = false; 1780 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1781 if (targ->t_auth_group == ag) { 1782 found = true; 1783 break; 1784 } 1785 } 1786 TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 1787 if (port->p_auth_group == ag) { 1788 found = true; 1789 break; 1790 } 1791 } 1792 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1793 if (pg->pg_discovery_auth_group == ag) { 1794 found = true; 1795 break; 1796 } 1797 } 1798 if (!found && ag->ag_name != NULL && 1799 strcmp(ag->ag_name, "default") != 0 && 1800 strcmp(ag->ag_name, "no-authentication") != 0 && 1801 strcmp(ag->ag_name, "no-access") != 0) { 1802 log_warnx("auth-group \"%s\" not assigned " 1803 "to any target", ag->ag_name); 1804 } 1805 } 1806 1807 return (0); 1808 } 1809 1810 static int 1811 conf_apply(struct conf *oldconf, struct conf *newconf) 1812 { 1813 struct lun *oldlun, *newlun, *tmplun; 1814 struct portal_group *oldpg, *newpg; 1815 struct portal *oldp, *newp; 1816 struct port *oldport, *newport, *tmpport; 1817 struct isns *oldns, *newns; 1818 pid_t otherpid; 1819 int changed, cumulated_error = 0, error, sockbuf; 1820 int one = 1; 1821 1822 if (oldconf->conf_debug != newconf->conf_debug) { 1823 log_debugx("changing debug level to %d", newconf->conf_debug); 1824 log_init(newconf->conf_debug); 1825 } 1826 1827 if (oldconf->conf_pidfh != NULL) { 1828 assert(oldconf->conf_pidfile_path != NULL); 1829 if (newconf->conf_pidfile_path != NULL && 1830 strcmp(oldconf->conf_pidfile_path, 1831 newconf->conf_pidfile_path) == 0) { 1832 newconf->conf_pidfh = oldconf->conf_pidfh; 1833 oldconf->conf_pidfh = NULL; 1834 } else { 1835 log_debugx("removing pidfile %s", 1836 oldconf->conf_pidfile_path); 1837 pidfile_remove(oldconf->conf_pidfh); 1838 oldconf->conf_pidfh = NULL; 1839 } 1840 } 1841 1842 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1843 log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1844 newconf->conf_pidfh = 1845 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1846 if (newconf->conf_pidfh == NULL) { 1847 if (errno == EEXIST) 1848 log_errx(1, "daemon already running, pid: %jd.", 1849 (intmax_t)otherpid); 1850 log_err(1, "cannot open or create pidfile \"%s\"", 1851 newconf->conf_pidfile_path); 1852 } 1853 } 1854 1855 /* 1856 * Go through the new portal groups, assigning tags or preserving old. 1857 */ 1858 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1859 if (newpg->pg_tag != 0) 1860 continue; 1861 oldpg = portal_group_find(oldconf, newpg->pg_name); 1862 if (oldpg != NULL) 1863 newpg->pg_tag = oldpg->pg_tag; 1864 else 1865 newpg->pg_tag = ++last_portal_group_tag; 1866 } 1867 1868 /* Deregister on removed iSNS servers. */ 1869 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1870 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1871 if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1872 break; 1873 } 1874 if (newns == NULL) 1875 isns_deregister(oldns); 1876 } 1877 1878 /* 1879 * XXX: If target or lun removal fails, we should somehow "move" 1880 * the old lun or target into newconf, so that subsequent 1881 * conf_apply() would try to remove them again. That would 1882 * be somewhat hairy, though, and lun deletion failures don't 1883 * really happen, so leave it as it is for now. 1884 */ 1885 /* 1886 * First, remove any ports present in the old configuration 1887 * and missing in the new one. 1888 */ 1889 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) { 1890 if (oldport->p_foreign) 1891 continue; 1892 newport = port_find(newconf, oldport->p_name); 1893 if (newport != NULL && !newport->p_foreign) 1894 continue; 1895 log_debugx("removing port \"%s\"", oldport->p_name); 1896 error = kernel_port_remove(oldport); 1897 if (error != 0) { 1898 log_warnx("failed to remove port %s", 1899 oldport->p_name); 1900 /* 1901 * XXX: Uncomment after fixing the root cause. 1902 * 1903 * cumulated_error++; 1904 */ 1905 } 1906 } 1907 1908 /* 1909 * Second, remove any LUNs present in the old configuration 1910 * and missing in the new one. 1911 */ 1912 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) { 1913 newlun = lun_find(newconf, oldlun->l_name); 1914 if (newlun == NULL) { 1915 log_debugx("lun \"%s\", CTL lun %d " 1916 "not found in new configuration; " 1917 "removing", oldlun->l_name, oldlun->l_ctl_lun); 1918 error = kernel_lun_remove(oldlun); 1919 if (error != 0) { 1920 log_warnx("failed to remove lun \"%s\", " 1921 "CTL lun %d", 1922 oldlun->l_name, oldlun->l_ctl_lun); 1923 cumulated_error++; 1924 } 1925 continue; 1926 } 1927 1928 /* 1929 * Also remove the LUNs changed by more than size. 1930 */ 1931 changed = 0; 1932 assert(oldlun->l_backend != NULL); 1933 assert(newlun->l_backend != NULL); 1934 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1935 log_debugx("backend for lun \"%s\", " 1936 "CTL lun %d changed; removing", 1937 oldlun->l_name, oldlun->l_ctl_lun); 1938 changed = 1; 1939 } 1940 if (oldlun->l_blocksize != newlun->l_blocksize) { 1941 log_debugx("blocksize for lun \"%s\", " 1942 "CTL lun %d changed; removing", 1943 oldlun->l_name, oldlun->l_ctl_lun); 1944 changed = 1; 1945 } 1946 if (newlun->l_device_id != NULL && 1947 (oldlun->l_device_id == NULL || 1948 strcmp(oldlun->l_device_id, newlun->l_device_id) != 1949 0)) { 1950 log_debugx("device-id for lun \"%s\", " 1951 "CTL lun %d changed; removing", 1952 oldlun->l_name, oldlun->l_ctl_lun); 1953 changed = 1; 1954 } 1955 if (newlun->l_path != NULL && 1956 (oldlun->l_path == NULL || 1957 strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1958 log_debugx("path for lun \"%s\", " 1959 "CTL lun %d, changed; removing", 1960 oldlun->l_name, oldlun->l_ctl_lun); 1961 changed = 1; 1962 } 1963 if (newlun->l_serial != NULL && 1964 (oldlun->l_serial == NULL || 1965 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1966 log_debugx("serial for lun \"%s\", " 1967 "CTL lun %d changed; removing", 1968 oldlun->l_name, oldlun->l_ctl_lun); 1969 changed = 1; 1970 } 1971 if (changed) { 1972 error = kernel_lun_remove(oldlun); 1973 if (error != 0) { 1974 log_warnx("failed to remove lun \"%s\", " 1975 "CTL lun %d", 1976 oldlun->l_name, oldlun->l_ctl_lun); 1977 cumulated_error++; 1978 } 1979 lun_delete(oldlun); 1980 continue; 1981 } 1982 1983 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1984 } 1985 1986 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) { 1987 oldlun = lun_find(oldconf, newlun->l_name); 1988 if (oldlun != NULL) { 1989 log_debugx("modifying lun \"%s\", CTL lun %d", 1990 newlun->l_name, newlun->l_ctl_lun); 1991 error = kernel_lun_modify(newlun); 1992 if (error != 0) { 1993 log_warnx("failed to " 1994 "modify lun \"%s\", CTL lun %d", 1995 newlun->l_name, newlun->l_ctl_lun); 1996 cumulated_error++; 1997 } 1998 continue; 1999 } 2000 log_debugx("adding lun \"%s\"", newlun->l_name); 2001 error = kernel_lun_add(newlun); 2002 if (error != 0) { 2003 log_warnx("failed to add lun \"%s\"", newlun->l_name); 2004 lun_delete(newlun); 2005 cumulated_error++; 2006 } 2007 } 2008 2009 /* 2010 * Now add new ports or modify existing ones. 2011 */ 2012 TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) { 2013 if (newport->p_foreign) 2014 continue; 2015 oldport = port_find(oldconf, newport->p_name); 2016 2017 if (oldport == NULL || oldport->p_foreign) { 2018 log_debugx("adding port \"%s\"", newport->p_name); 2019 error = kernel_port_add(newport); 2020 } else { 2021 log_debugx("updating port \"%s\"", newport->p_name); 2022 newport->p_ctl_port = oldport->p_ctl_port; 2023 error = kernel_port_update(newport, oldport); 2024 } 2025 if (error != 0) { 2026 log_warnx("failed to %s port %s", 2027 (oldport == NULL) ? "add" : "update", 2028 newport->p_name); 2029 /* 2030 * XXX: Uncomment after fixing the root cause. 2031 * 2032 * cumulated_error++; 2033 */ 2034 } 2035 } 2036 2037 /* 2038 * Go through the new portals, opening the sockets as necessary. 2039 */ 2040 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 2041 if (newpg->pg_foreign) 2042 continue; 2043 if (newpg->pg_unassigned) { 2044 log_debugx("not listening on portal-group \"%s\", " 2045 "not assigned to any target", 2046 newpg->pg_name); 2047 continue; 2048 } 2049 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 2050 /* 2051 * Try to find already open portal and reuse 2052 * the listening socket. We don't care about 2053 * what portal or portal group that was, what 2054 * matters is the listening address. 2055 */ 2056 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 2057 pg_next) { 2058 TAILQ_FOREACH(oldp, &oldpg->pg_portals, 2059 p_next) { 2060 if (strcmp(newp->p_listen, 2061 oldp->p_listen) == 0 && 2062 oldp->p_socket > 0) { 2063 newp->p_socket = 2064 oldp->p_socket; 2065 oldp->p_socket = 0; 2066 break; 2067 } 2068 } 2069 } 2070 if (newp->p_socket > 0) { 2071 /* 2072 * We're done with this portal. 2073 */ 2074 continue; 2075 } 2076 2077 #ifdef ICL_KERNEL_PROXY 2078 if (proxy_mode) { 2079 newpg->pg_conf->conf_portal_id++; 2080 newp->p_id = newpg->pg_conf->conf_portal_id; 2081 log_debugx("listening on %s, portal-group " 2082 "\"%s\", portal id %d, using ICL proxy", 2083 newp->p_listen, newpg->pg_name, newp->p_id); 2084 kernel_listen(newp->p_ai, newp->p_iser, 2085 newp->p_id); 2086 continue; 2087 } 2088 #endif 2089 assert(proxy_mode == false); 2090 assert(newp->p_iser == false); 2091 2092 log_debugx("listening on %s, portal-group \"%s\"", 2093 newp->p_listen, newpg->pg_name); 2094 newp->p_socket = socket(newp->p_ai->ai_family, 2095 newp->p_ai->ai_socktype, 2096 newp->p_ai->ai_protocol); 2097 if (newp->p_socket < 0) { 2098 log_warn("socket(2) failed for %s", 2099 newp->p_listen); 2100 cumulated_error++; 2101 continue; 2102 } 2103 sockbuf = SOCKBUF_SIZE; 2104 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF, 2105 &sockbuf, sizeof(sockbuf)) == -1) 2106 log_warn("setsockopt(SO_RCVBUF) failed " 2107 "for %s", newp->p_listen); 2108 sockbuf = SOCKBUF_SIZE; 2109 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF, 2110 &sockbuf, sizeof(sockbuf)) == -1) 2111 log_warn("setsockopt(SO_SNDBUF) failed " 2112 "for %s", newp->p_listen); 2113 error = setsockopt(newp->p_socket, SOL_SOCKET, 2114 SO_REUSEADDR, &one, sizeof(one)); 2115 if (error != 0) { 2116 log_warn("setsockopt(SO_REUSEADDR) failed " 2117 "for %s", newp->p_listen); 2118 close(newp->p_socket); 2119 newp->p_socket = 0; 2120 cumulated_error++; 2121 continue; 2122 } 2123 error = bind(newp->p_socket, newp->p_ai->ai_addr, 2124 newp->p_ai->ai_addrlen); 2125 if (error != 0) { 2126 log_warn("bind(2) failed for %s", 2127 newp->p_listen); 2128 close(newp->p_socket); 2129 newp->p_socket = 0; 2130 cumulated_error++; 2131 continue; 2132 } 2133 error = listen(newp->p_socket, -1); 2134 if (error != 0) { 2135 log_warn("listen(2) failed for %s", 2136 newp->p_listen); 2137 close(newp->p_socket); 2138 newp->p_socket = 0; 2139 cumulated_error++; 2140 continue; 2141 } 2142 } 2143 } 2144 2145 /* 2146 * Go through the no longer used sockets, closing them. 2147 */ 2148 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 2149 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 2150 if (oldp->p_socket <= 0) 2151 continue; 2152 log_debugx("closing socket for %s, portal-group \"%s\"", 2153 oldp->p_listen, oldpg->pg_name); 2154 close(oldp->p_socket); 2155 oldp->p_socket = 0; 2156 } 2157 } 2158 2159 /* (Re-)Register on remaining/new iSNS servers. */ 2160 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 2161 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 2162 if (strcmp(oldns->i_addr, newns->i_addr) == 0) 2163 break; 2164 } 2165 isns_register(newns, oldns); 2166 } 2167 2168 /* Schedule iSNS update */ 2169 if (!TAILQ_EMPTY(&newconf->conf_isns)) 2170 set_timeout((newconf->conf_isns_period + 2) / 3, false); 2171 2172 return (cumulated_error); 2173 } 2174 2175 bool 2176 timed_out(void) 2177 { 2178 2179 return (sigalrm_received); 2180 } 2181 2182 static void 2183 sigalrm_handler_fatal(int dummy __unused) 2184 { 2185 /* 2186 * It would be easiest to just log an error and exit. We can't 2187 * do this, though, because log_errx() is not signal safe, since 2188 * it calls syslog(3). Instead, set a flag checked by pdu_send() 2189 * and pdu_receive(), to call log_errx() there. Should they fail 2190 * to notice, we'll exit here one second later. 2191 */ 2192 if (sigalrm_received) { 2193 /* 2194 * Oh well. Just give up and quit. 2195 */ 2196 _exit(2); 2197 } 2198 2199 sigalrm_received = true; 2200 } 2201 2202 static void 2203 sigalrm_handler(int dummy __unused) 2204 { 2205 2206 sigalrm_received = true; 2207 } 2208 2209 void 2210 set_timeout(int timeout, int fatal) 2211 { 2212 struct sigaction sa; 2213 struct itimerval itv; 2214 int error; 2215 2216 if (timeout <= 0) { 2217 log_debugx("session timeout disabled"); 2218 bzero(&itv, sizeof(itv)); 2219 error = setitimer(ITIMER_REAL, &itv, NULL); 2220 if (error != 0) 2221 log_err(1, "setitimer"); 2222 sigalrm_received = false; 2223 return; 2224 } 2225 2226 sigalrm_received = false; 2227 bzero(&sa, sizeof(sa)); 2228 if (fatal) 2229 sa.sa_handler = sigalrm_handler_fatal; 2230 else 2231 sa.sa_handler = sigalrm_handler; 2232 sigfillset(&sa.sa_mask); 2233 error = sigaction(SIGALRM, &sa, NULL); 2234 if (error != 0) 2235 log_err(1, "sigaction"); 2236 2237 /* 2238 * First SIGALRM will arive after conf_timeout seconds. 2239 * If we do nothing, another one will arrive a second later. 2240 */ 2241 log_debugx("setting session timeout to %d seconds", timeout); 2242 bzero(&itv, sizeof(itv)); 2243 itv.it_interval.tv_sec = 1; 2244 itv.it_value.tv_sec = timeout; 2245 error = setitimer(ITIMER_REAL, &itv, NULL); 2246 if (error != 0) 2247 log_err(1, "setitimer"); 2248 } 2249 2250 static int 2251 wait_for_children(bool block) 2252 { 2253 pid_t pid; 2254 int status; 2255 int num = 0; 2256 2257 for (;;) { 2258 /* 2259 * If "block" is true, wait for at least one process. 2260 */ 2261 if (block && num == 0) 2262 pid = wait4(-1, &status, 0, NULL); 2263 else 2264 pid = wait4(-1, &status, WNOHANG, NULL); 2265 if (pid <= 0) 2266 break; 2267 if (WIFSIGNALED(status)) { 2268 log_warnx("child process %d terminated with signal %d", 2269 pid, WTERMSIG(status)); 2270 } else if (WEXITSTATUS(status) != 0) { 2271 log_warnx("child process %d terminated with exit status %d", 2272 pid, WEXITSTATUS(status)); 2273 } else { 2274 log_debugx("child process %d terminated gracefully", pid); 2275 } 2276 num++; 2277 } 2278 2279 return (num); 2280 } 2281 2282 static void 2283 handle_connection(struct portal *portal, int fd, 2284 const struct sockaddr *client_sa, bool dont_fork) 2285 { 2286 struct connection *conn; 2287 int error; 2288 pid_t pid; 2289 char host[NI_MAXHOST + 1]; 2290 struct conf *conf; 2291 2292 conf = portal->p_portal_group->pg_conf; 2293 2294 if (dont_fork) { 2295 log_debugx("incoming connection; not forking due to -d flag"); 2296 } else { 2297 nchildren -= wait_for_children(false); 2298 assert(nchildren >= 0); 2299 2300 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 2301 log_debugx("maxproc limit of %d child processes hit; " 2302 "waiting for child process to exit", conf->conf_maxproc); 2303 nchildren -= wait_for_children(true); 2304 assert(nchildren >= 0); 2305 } 2306 log_debugx("incoming connection; forking child process #%d", 2307 nchildren); 2308 nchildren++; 2309 pid = fork(); 2310 if (pid < 0) 2311 log_err(1, "fork"); 2312 if (pid > 0) { 2313 close(fd); 2314 return; 2315 } 2316 } 2317 pidfile_close(conf->conf_pidfh); 2318 2319 error = getnameinfo(client_sa, client_sa->sa_len, 2320 host, sizeof(host), NULL, 0, NI_NUMERICHOST); 2321 if (error != 0) 2322 log_errx(1, "getnameinfo: %s", gai_strerror(error)); 2323 2324 log_debugx("accepted connection from %s; portal group \"%s\"", 2325 host, portal->p_portal_group->pg_name); 2326 log_set_peer_addr(host); 2327 setproctitle("%s", host); 2328 2329 conn = connection_new(portal, fd, host, client_sa); 2330 set_timeout(conf->conf_timeout, true); 2331 kernel_capsicate(); 2332 login(conn); 2333 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 2334 kernel_handoff(conn); 2335 log_debugx("connection handed off to the kernel"); 2336 } else { 2337 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 2338 discovery(conn); 2339 } 2340 log_debugx("nothing more to do; exiting"); 2341 exit(0); 2342 } 2343 2344 static int 2345 fd_add(int fd, fd_set *fdset, int nfds) 2346 { 2347 2348 /* 2349 * Skip sockets which we failed to bind. 2350 */ 2351 if (fd <= 0) 2352 return (nfds); 2353 2354 FD_SET(fd, fdset); 2355 if (fd > nfds) 2356 nfds = fd; 2357 return (nfds); 2358 } 2359 2360 static void 2361 main_loop(struct conf *conf, bool dont_fork) 2362 { 2363 struct portal_group *pg; 2364 struct portal *portal; 2365 struct sockaddr_storage client_sa; 2366 socklen_t client_salen; 2367 #ifdef ICL_KERNEL_PROXY 2368 int connection_id; 2369 int portal_id; 2370 #endif 2371 fd_set fdset; 2372 int error, nfds, client_fd; 2373 2374 pidfile_write(conf->conf_pidfh); 2375 2376 for (;;) { 2377 if (sighup_received || sigterm_received || timed_out()) 2378 return; 2379 2380 #ifdef ICL_KERNEL_PROXY 2381 if (proxy_mode) { 2382 client_salen = sizeof(client_sa); 2383 kernel_accept(&connection_id, &portal_id, 2384 (struct sockaddr *)&client_sa, &client_salen); 2385 assert(client_salen >= client_sa.ss_len); 2386 2387 log_debugx("incoming connection, id %d, portal id %d", 2388 connection_id, portal_id); 2389 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2390 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2391 if (portal->p_id == portal_id) { 2392 goto found; 2393 } 2394 } 2395 } 2396 2397 log_errx(1, "kernel returned invalid portal_id %d", 2398 portal_id); 2399 2400 found: 2401 handle_connection(portal, connection_id, 2402 (struct sockaddr *)&client_sa, dont_fork); 2403 } else { 2404 #endif 2405 assert(proxy_mode == false); 2406 2407 FD_ZERO(&fdset); 2408 nfds = 0; 2409 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2410 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 2411 nfds = fd_add(portal->p_socket, &fdset, nfds); 2412 } 2413 error = select(nfds + 1, &fdset, NULL, NULL, NULL); 2414 if (error <= 0) { 2415 if (errno == EINTR) 2416 return; 2417 log_err(1, "select"); 2418 } 2419 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2420 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2421 if (!FD_ISSET(portal->p_socket, &fdset)) 2422 continue; 2423 client_salen = sizeof(client_sa); 2424 client_fd = accept(portal->p_socket, 2425 (struct sockaddr *)&client_sa, 2426 &client_salen); 2427 if (client_fd < 0) { 2428 if (errno == ECONNABORTED) 2429 continue; 2430 log_err(1, "accept"); 2431 } 2432 assert(client_salen >= client_sa.ss_len); 2433 2434 handle_connection(portal, client_fd, 2435 (struct sockaddr *)&client_sa, 2436 dont_fork); 2437 break; 2438 } 2439 } 2440 #ifdef ICL_KERNEL_PROXY 2441 } 2442 #endif 2443 } 2444 } 2445 2446 static void 2447 sighup_handler(int dummy __unused) 2448 { 2449 2450 sighup_received = true; 2451 } 2452 2453 static void 2454 sigterm_handler(int dummy __unused) 2455 { 2456 2457 sigterm_received = true; 2458 } 2459 2460 static void 2461 sigchld_handler(int dummy __unused) 2462 { 2463 2464 /* 2465 * The only purpose of this handler is to make SIGCHLD 2466 * interrupt the ISCSIDWAIT ioctl(2), so we can call 2467 * wait_for_children(). 2468 */ 2469 } 2470 2471 static void 2472 register_signals(void) 2473 { 2474 struct sigaction sa; 2475 int error; 2476 2477 bzero(&sa, sizeof(sa)); 2478 sa.sa_handler = sighup_handler; 2479 sigfillset(&sa.sa_mask); 2480 error = sigaction(SIGHUP, &sa, NULL); 2481 if (error != 0) 2482 log_err(1, "sigaction"); 2483 2484 sa.sa_handler = sigterm_handler; 2485 error = sigaction(SIGTERM, &sa, NULL); 2486 if (error != 0) 2487 log_err(1, "sigaction"); 2488 2489 sa.sa_handler = sigterm_handler; 2490 error = sigaction(SIGINT, &sa, NULL); 2491 if (error != 0) 2492 log_err(1, "sigaction"); 2493 2494 sa.sa_handler = sigchld_handler; 2495 error = sigaction(SIGCHLD, &sa, NULL); 2496 if (error != 0) 2497 log_err(1, "sigaction"); 2498 } 2499 2500 static void 2501 check_perms(const char *path) 2502 { 2503 struct stat sb; 2504 int error; 2505 2506 error = stat(path, &sb); 2507 if (error != 0) { 2508 log_warn("stat"); 2509 return; 2510 } 2511 if (sb.st_mode & S_IWOTH) { 2512 log_warnx("%s is world-writable", path); 2513 } else if (sb.st_mode & S_IROTH) { 2514 log_warnx("%s is world-readable", path); 2515 } else if (sb.st_mode & S_IXOTH) { 2516 /* 2517 * Ok, this one doesn't matter, but still do it, 2518 * just for consistency. 2519 */ 2520 log_warnx("%s is world-executable", path); 2521 } 2522 2523 /* 2524 * XXX: Should we also check for owner != 0? 2525 */ 2526 } 2527 2528 static struct conf * 2529 conf_new_from_file(const char *path, struct conf *oldconf, bool ucl) 2530 { 2531 struct conf *conf; 2532 struct auth_group *ag; 2533 struct portal_group *pg; 2534 struct pport *pp; 2535 int error; 2536 2537 log_debugx("obtaining configuration from %s", path); 2538 2539 conf = conf_new(); 2540 2541 TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next) 2542 pport_copy(pp, conf); 2543 2544 ag = auth_group_new(conf, "default"); 2545 assert(ag != NULL); 2546 2547 ag = auth_group_new(conf, "no-authentication"); 2548 assert(ag != NULL); 2549 ag->ag_type = AG_TYPE_NO_AUTHENTICATION; 2550 2551 ag = auth_group_new(conf, "no-access"); 2552 assert(ag != NULL); 2553 ag->ag_type = AG_TYPE_DENY; 2554 2555 pg = portal_group_new(conf, "default"); 2556 assert(pg != NULL); 2557 2558 if (ucl) 2559 error = uclparse_conf(conf, path); 2560 else 2561 error = parse_conf(conf, path); 2562 2563 if (error != 0) { 2564 conf_delete(conf); 2565 return (NULL); 2566 } 2567 2568 check_perms(path); 2569 2570 if (conf->conf_default_ag_defined == false) { 2571 log_debugx("auth-group \"default\" not defined; " 2572 "going with defaults"); 2573 ag = auth_group_find(conf, "default"); 2574 assert(ag != NULL); 2575 ag->ag_type = AG_TYPE_DENY; 2576 } 2577 2578 if (conf->conf_default_pg_defined == false) { 2579 log_debugx("portal-group \"default\" not defined; " 2580 "going with defaults"); 2581 pg = portal_group_find(conf, "default"); 2582 assert(pg != NULL); 2583 portal_group_add_listen(pg, "0.0.0.0:3260", false); 2584 portal_group_add_listen(pg, "[::]:3260", false); 2585 } 2586 2587 conf->conf_kernel_port_on = true; 2588 2589 error = conf_verify(conf); 2590 if (error != 0) { 2591 conf_delete(conf); 2592 return (NULL); 2593 } 2594 2595 return (conf); 2596 } 2597 2598 int 2599 main(int argc, char **argv) 2600 { 2601 struct conf *oldconf, *newconf, *tmpconf; 2602 struct isns *newns; 2603 const char *config_path = DEFAULT_CONFIG_PATH; 2604 int debug = 0, ch, error; 2605 bool dont_daemonize = false; 2606 bool use_ucl = false; 2607 2608 while ((ch = getopt(argc, argv, "duf:R")) != -1) { 2609 switch (ch) { 2610 case 'd': 2611 dont_daemonize = true; 2612 debug++; 2613 break; 2614 case 'u': 2615 use_ucl = true; 2616 break; 2617 case 'f': 2618 config_path = optarg; 2619 break; 2620 case 'R': 2621 #ifndef ICL_KERNEL_PROXY 2622 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY " 2623 "does not support iSER protocol"); 2624 #endif 2625 proxy_mode = true; 2626 break; 2627 case '?': 2628 default: 2629 usage(); 2630 } 2631 } 2632 argc -= optind; 2633 if (argc != 0) 2634 usage(); 2635 2636 log_init(debug); 2637 kernel_init(); 2638 2639 oldconf = conf_new_from_kernel(); 2640 newconf = conf_new_from_file(config_path, oldconf, use_ucl); 2641 2642 if (newconf == NULL) 2643 log_errx(1, "configuration error; exiting"); 2644 if (debug > 0) { 2645 oldconf->conf_debug = debug; 2646 newconf->conf_debug = debug; 2647 } 2648 2649 error = conf_apply(oldconf, newconf); 2650 if (error != 0) 2651 log_errx(1, "failed to apply configuration; exiting"); 2652 2653 conf_delete(oldconf); 2654 oldconf = NULL; 2655 2656 register_signals(); 2657 2658 if (dont_daemonize == false) { 2659 log_debugx("daemonizing"); 2660 if (daemon(0, 0) == -1) { 2661 log_warn("cannot daemonize"); 2662 pidfile_remove(newconf->conf_pidfh); 2663 exit(1); 2664 } 2665 } 2666 2667 /* Schedule iSNS update */ 2668 if (!TAILQ_EMPTY(&newconf->conf_isns)) 2669 set_timeout((newconf->conf_isns_period + 2) / 3, false); 2670 2671 for (;;) { 2672 main_loop(newconf, dont_daemonize); 2673 if (sighup_received) { 2674 sighup_received = false; 2675 log_debugx("received SIGHUP, reloading configuration"); 2676 tmpconf = conf_new_from_file(config_path, newconf, 2677 use_ucl); 2678 2679 if (tmpconf == NULL) { 2680 log_warnx("configuration error, " 2681 "continuing with old configuration"); 2682 } else { 2683 if (debug > 0) 2684 tmpconf->conf_debug = debug; 2685 oldconf = newconf; 2686 newconf = tmpconf; 2687 error = conf_apply(oldconf, newconf); 2688 if (error != 0) 2689 log_warnx("failed to reload " 2690 "configuration"); 2691 conf_delete(oldconf); 2692 oldconf = NULL; 2693 } 2694 } else if (sigterm_received) { 2695 log_debugx("exiting on signal; " 2696 "reloading empty configuration"); 2697 2698 log_debugx("removing CTL iSCSI ports " 2699 "and terminating all connections"); 2700 2701 oldconf = newconf; 2702 newconf = conf_new(); 2703 if (debug > 0) 2704 newconf->conf_debug = debug; 2705 error = conf_apply(oldconf, newconf); 2706 if (error != 0) 2707 log_warnx("failed to apply configuration"); 2708 conf_delete(oldconf); 2709 oldconf = NULL; 2710 2711 log_warnx("exiting on signal"); 2712 exit(0); 2713 } else { 2714 nchildren -= wait_for_children(false); 2715 assert(nchildren >= 0); 2716 if (timed_out()) { 2717 set_timeout(0, false); 2718 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) 2719 isns_check(newns); 2720 /* Schedule iSNS update */ 2721 if (!TAILQ_EMPTY(&newconf->conf_isns)) { 2722 set_timeout((newconf->conf_isns_period 2723 + 2) / 3, 2724 false); 2725 } 2726 } 2727 } 2728 } 2729 /* NOTREACHED */ 2730 } 2731