1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>. 3 * Copyright (c) Intel Corporation. 4 * All rights reserved. 5 */ 6 7 #include "spdk/stdinc.h" 8 9 #include "spdk/sock.h" 10 #include "spdk/scsi.h" 11 12 #include "spdk/log.h" 13 14 #include "iscsi/iscsi.h" 15 #include "iscsi/conn.h" 16 #include "iscsi/tgt_node.h" 17 #include "iscsi/portal_grp.h" 18 #include "iscsi/init_grp.h" 19 #include "iscsi/task.h" 20 21 #define MAX_TMPBUF 4096 22 #define MAX_MASKBUF 128 23 24 25 #define MAX_TMP_NAME_BUF (11 /* TargetName= */ + MAX_TARGET_NAME + 1 /* null */) 26 #define MAX_TMP_ADDR_BUF (14 /* TargetAddress= */ + MAX_PORTAL_ADDR + 1 /* : */ + \ 27 MAX_PORTAL_PORT + 1 /* , */ + 10 /* max length of int in Decimal */ + 1 /* null */) 28 29 static bool 30 iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr) 31 { 32 struct in6_addr in6_mask; 33 struct in6_addr in6_addr; 34 char mask[MAX_MASKBUF]; 35 const char *p; 36 size_t n; 37 int bits, bmask; 38 int i; 39 40 if (netmask[0] != '[') { 41 return false; 42 } 43 p = strchr(netmask, ']'); 44 if (p == NULL) { 45 return false; 46 } 47 n = p - (netmask + 1); 48 if (n + 1 > sizeof mask) { 49 return false; 50 } 51 52 memcpy(mask, netmask + 1, n); 53 mask[n] = '\0'; 54 p++; 55 56 if (p[0] == '/') { 57 bits = (int) strtol(p + 1, NULL, 10); 58 if (bits <= 0 || bits > 128) { 59 return false; 60 } 61 } else { 62 bits = 128; 63 } 64 65 #if 0 66 SPDK_DEBUGLOG(iscsi, "input %s\n", addr); 67 SPDK_DEBUGLOG(iscsi, "mask %s / %d\n", mask, bits); 68 #endif 69 70 /* presentation to network order binary */ 71 if (inet_pton(AF_INET6, mask, &in6_mask) <= 0 72 || inet_pton(AF_INET6, addr, &in6_addr) <= 0) { 73 return false; 74 } 75 76 /* check 128bits */ 77 for (i = 0; i < (bits / 8); i++) { 78 if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) { 79 return false; 80 } 81 } 82 if (bits % 8) { 83 bmask = (0xffU << (8 - (bits % 8))) & 0xffU; 84 if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) { 85 return false; 86 } 87 } 88 89 /* match */ 90 return true; 91 } 92 93 static bool 94 iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr) 95 { 96 struct in_addr in4_mask; 97 struct in_addr in4_addr; 98 char mask[MAX_MASKBUF]; 99 const char *p; 100 uint32_t bmask; 101 size_t n; 102 int bits; 103 104 p = strchr(netmask, '/'); 105 if (p == NULL) { 106 p = netmask + strlen(netmask); 107 } 108 n = p - netmask; 109 if (n + 1 > sizeof mask) { 110 return false; 111 } 112 113 memcpy(mask, netmask, n); 114 mask[n] = '\0'; 115 116 if (p[0] == '/') { 117 bits = (int) strtol(p + 1, NULL, 10); 118 if (bits <= 0 || bits > 32) { 119 return false; 120 } 121 } else { 122 bits = 32; 123 } 124 125 /* presentation to network order binary */ 126 if (inet_pton(AF_INET, mask, &in4_mask) <= 0 127 || inet_pton(AF_INET, addr, &in4_addr) <= 0) { 128 return false; 129 } 130 131 /* check 32bits */ 132 bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU; 133 if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) { 134 return false; 135 } 136 137 /* match */ 138 return true; 139 } 140 141 static bool 142 iscsi_netmask_allow_addr(const char *netmask, const char *addr) 143 { 144 if (netmask == NULL || addr == NULL) { 145 return false; 146 } 147 if (strcasecmp(netmask, "ANY") == 0) { 148 return true; 149 } 150 if (netmask[0] == '[') { 151 /* IPv6 */ 152 if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) { 153 return true; 154 } 155 } else { 156 /* IPv4 */ 157 if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) { 158 return true; 159 } 160 } 161 return false; 162 } 163 164 static bool 165 iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp, 166 const char *addr) 167 { 168 struct spdk_iscsi_initiator_netmask *imask; 169 170 TAILQ_FOREACH(imask, &igp->netmask_head, tailq) { 171 SPDK_DEBUGLOG(iscsi, "netmask=%s, addr=%s\n", 172 imask->mask, addr); 173 if (iscsi_netmask_allow_addr(imask->mask, addr)) { 174 return true; 175 } 176 } 177 return false; 178 } 179 180 static int 181 iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp, 182 const char *iqn, bool *result) 183 { 184 struct spdk_iscsi_initiator_name *iname; 185 186 TAILQ_FOREACH(iname, &igp->initiator_head, tailq) { 187 /* denied if iqn is matched */ 188 if ((iname->name[0] == '!') 189 && (strcasecmp(&iname->name[1], "ANY") == 0 190 || strcasecmp(&iname->name[1], iqn) == 0)) { 191 *result = false; 192 return 0; 193 } 194 /* allowed if iqn is matched */ 195 if (strcasecmp(iname->name, "ANY") == 0 196 || strcasecmp(iname->name, iqn) == 0) { 197 *result = true; 198 return 0; 199 } 200 } 201 return -1; 202 } 203 204 static struct spdk_iscsi_pg_map * 205 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target, 206 struct spdk_iscsi_portal_grp *pg); 207 208 bool 209 iscsi_tgt_node_access(struct spdk_iscsi_conn *conn, 210 struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr) 211 { 212 struct spdk_iscsi_portal_grp *pg; 213 struct spdk_iscsi_pg_map *pg_map; 214 struct spdk_iscsi_ig_map *ig_map; 215 int rc; 216 bool allowed = false; 217 218 if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) { 219 return false; 220 } 221 pg = conn->portal->group; 222 223 SPDK_DEBUGLOG(iscsi, "pg=%d, iqn=%s, addr=%s\n", 224 pg->tag, iqn, addr); 225 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 226 if (pg_map == NULL) { 227 return false; 228 } 229 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 230 rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed); 231 if (rc == 0) { 232 if (allowed == false) { 233 goto denied; 234 } else { 235 if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) { 236 return true; 237 } 238 } 239 } else { 240 /* netmask is denied in this initiator group */ 241 } 242 } 243 244 denied: 245 SPDK_DEBUGLOG(iscsi, "access denied from %s (%s) to %s (%s:%s,%d)\n", 246 iqn, addr, target->name, conn->portal_host, 247 conn->portal_port, conn->pg_tag); 248 return false; 249 } 250 251 static bool 252 iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn) 253 { 254 struct spdk_iscsi_pg_map *pg_map; 255 struct spdk_iscsi_ig_map *ig_map; 256 int rc; 257 bool result = false; 258 259 if (target == NULL || iqn == NULL) { 260 return false; 261 } 262 263 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 264 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 265 rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result); 266 if (rc == 0) { 267 return result; 268 } 269 } 270 } 271 272 return false; 273 } 274 275 static bool 276 iscsi_copy_str(char *data, int *total, int alloc_len, 277 int *previous_completed_len, int expected_size, char *src) 278 { 279 int len = 0; 280 281 assert(*previous_completed_len >= 0); 282 283 if (alloc_len - *total < 1) { 284 return true; 285 } 286 287 if (*previous_completed_len < expected_size) { 288 len = spdk_min(alloc_len - *total, expected_size - *previous_completed_len); 289 memcpy((char *)data + *total, src + *previous_completed_len, len); 290 *total += len; 291 *previous_completed_len = 0; 292 } else { 293 *previous_completed_len -= expected_size; 294 } 295 296 return false; 297 } 298 299 static int 300 iscsi_send_tgt_portals(struct spdk_iscsi_conn *conn, 301 struct spdk_iscsi_tgt_node *target, 302 uint8_t *data, int alloc_len, int total, 303 int *previous_completed_len, bool *no_buf_space) 304 { 305 char buf[MAX_TARGET_ADDR + 2]; 306 struct spdk_iscsi_portal_grp *pg; 307 struct spdk_iscsi_pg_map *pg_map; 308 struct spdk_iscsi_portal *p; 309 char *host; 310 char tmp_buf[MAX_TMP_ADDR_BUF]; 311 int len; 312 313 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 314 pg = pg_map->pg; 315 316 if (pg->is_private) { 317 /* Skip the private portal group. Portals in the private portal group 318 * will be returned only by temporary login redirection responses. 319 */ 320 continue; 321 } 322 323 TAILQ_FOREACH(p, &pg->head, per_pg_tailq) { 324 host = p->host; 325 /* wildcard? */ 326 if (strcasecmp(host, "[::]") == 0 || strcasecmp(host, "0.0.0.0") == 0) { 327 if (spdk_sock_is_ipv6(conn->sock)) { 328 snprintf(buf, sizeof buf, "[%s]", conn->target_addr); 329 host = buf; 330 } else if (spdk_sock_is_ipv4(conn->sock)) { 331 snprintf(buf, sizeof buf, "%s", conn->target_addr); 332 host = buf; 333 } else { 334 /* skip portal for the family */ 335 continue; 336 } 337 } 338 SPDK_DEBUGLOG(iscsi, "TargetAddress=%s:%s,%d\n", 339 host, p->port, pg->tag); 340 341 memset(tmp_buf, 0, sizeof(tmp_buf)); 342 /* Calculate the whole string size */ 343 len = snprintf(NULL, 0, "TargetAddress=%s:%s,%d", host, p->port, pg->tag); 344 assert(len < MAX_TMPBUF); 345 346 /* string contents are not fully copied */ 347 if (*previous_completed_len < len) { 348 /* Copy the string into the temporary buffer */ 349 snprintf(tmp_buf, len + 1, "TargetAddress=%s:%s,%d", host, p->port, pg->tag); 350 } 351 352 *no_buf_space = iscsi_copy_str(data, &total, alloc_len, previous_completed_len, 353 len + 1, tmp_buf); 354 if (*no_buf_space) { 355 break; 356 } 357 } 358 } 359 360 return total; 361 } 362 363 int 364 iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn, 365 const char *tiqn, uint8_t *data, int alloc_len, int data_len) 366 { 367 struct spdk_iscsi_tgt_node *target; 368 int total; 369 int len; 370 int rc; 371 int previous_completed_size = 0; 372 bool no_buf_space = false; 373 char tmp_buf[MAX_TMP_NAME_BUF]; 374 375 if (conn == NULL) { 376 return 0; 377 } 378 previous_completed_size = conn->send_tgt_completed_size; 379 380 total = data_len; 381 if (alloc_len < 1) { 382 return 0; 383 } 384 if (total >= alloc_len) { 385 total = alloc_len; 386 data[total - 1] = '\0'; 387 return total; 388 } 389 390 pthread_mutex_lock(&g_iscsi.mutex); 391 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 392 if (strcasecmp(tiqn, "ALL") != 0 393 && strcasecmp(tiqn, target->name) != 0) { 394 continue; 395 } 396 rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn); 397 if (rc == 0) { 398 continue; 399 } 400 401 memset(tmp_buf, 0, sizeof(tmp_buf)); 402 /* Calculate the whole string size */ 403 len = snprintf(NULL, 0, "TargetName=%s", target->name); 404 assert(len < MAX_TMPBUF); 405 406 /* String contents are not copied */ 407 if (previous_completed_size < len) { 408 /* Copy the string into the temporary buffer */ 409 snprintf(tmp_buf, len + 1, "TargetName=%s", target->name); 410 } 411 412 no_buf_space = iscsi_copy_str(data, &total, alloc_len, &previous_completed_size, 413 len + 1, tmp_buf); 414 if (no_buf_space) { 415 break; 416 } 417 418 total = iscsi_send_tgt_portals(conn, target, data, alloc_len, total, 419 &previous_completed_size, &no_buf_space); 420 if (no_buf_space) { 421 break; 422 } 423 } 424 pthread_mutex_unlock(&g_iscsi.mutex); 425 426 /* Only set it when it is not successfully completed */ 427 if (no_buf_space) { 428 conn->send_tgt_completed_size += total; 429 } else { 430 conn->send_tgt_completed_size = 0; 431 } 432 433 return total; 434 } 435 436 struct spdk_iscsi_tgt_node * 437 iscsi_find_tgt_node(const char *target_name) 438 { 439 struct spdk_iscsi_tgt_node *target; 440 441 if (target_name == NULL) { 442 return NULL; 443 } 444 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 445 if (strcasecmp(target_name, target->name) == 0) { 446 return target; 447 } 448 } 449 return NULL; 450 } 451 452 static int 453 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target) 454 { 455 pthread_mutex_lock(&g_iscsi.mutex); 456 457 if (iscsi_find_tgt_node(target->name) != NULL) { 458 pthread_mutex_unlock(&g_iscsi.mutex); 459 return -EEXIST; 460 } 461 462 TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq); 463 464 pthread_mutex_unlock(&g_iscsi.mutex); 465 return 0; 466 } 467 468 static int 469 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target) 470 { 471 struct spdk_iscsi_tgt_node *t; 472 473 TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) { 474 if (t == target) { 475 TAILQ_REMOVE(&g_iscsi.target_head, t, tailq); 476 return 0; 477 } 478 } 479 480 return -1; 481 } 482 483 static struct spdk_iscsi_ig_map * 484 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map, 485 struct spdk_iscsi_init_grp *ig) 486 { 487 struct spdk_iscsi_ig_map *ig_map; 488 489 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 490 if (ig_map->ig == ig) { 491 return ig_map; 492 } 493 } 494 495 return NULL; 496 } 497 498 static struct spdk_iscsi_ig_map * 499 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map, 500 struct spdk_iscsi_init_grp *ig) 501 { 502 struct spdk_iscsi_ig_map *ig_map; 503 504 if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) { 505 return NULL; 506 } 507 508 ig_map = malloc(sizeof(*ig_map)); 509 if (ig_map == NULL) { 510 return NULL; 511 } 512 513 ig_map->ig = ig; 514 ig->ref++; 515 pg_map->num_ig_maps++; 516 TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq); 517 518 return ig_map; 519 } 520 521 static void 522 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 523 struct spdk_iscsi_ig_map *ig_map) 524 { 525 TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq); 526 pg_map->num_ig_maps--; 527 ig_map->ig->ref--; 528 free(ig_map); 529 } 530 531 static int 532 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 533 struct spdk_iscsi_init_grp *ig) 534 { 535 struct spdk_iscsi_ig_map *ig_map; 536 537 ig_map = iscsi_pg_map_find_ig_map(pg_map, ig); 538 if (ig_map == NULL) { 539 return -ENOENT; 540 } 541 542 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 543 return 0; 544 } 545 546 static void 547 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map) 548 { 549 struct spdk_iscsi_ig_map *ig_map, *tmp; 550 551 TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) { 552 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 553 } 554 } 555 556 static struct spdk_iscsi_pg_map * 557 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target, 558 struct spdk_iscsi_portal_grp *pg) 559 { 560 struct spdk_iscsi_pg_map *pg_map; 561 562 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 563 if (pg_map->pg == pg) { 564 return pg_map; 565 } 566 } 567 568 return NULL; 569 } 570 571 static struct spdk_iscsi_pg_map * 572 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target, 573 struct spdk_iscsi_portal_grp *pg) 574 { 575 struct spdk_iscsi_pg_map *pg_map; 576 char port_name[MAX_TMPBUF]; 577 int rc; 578 579 if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) { 580 return NULL; 581 } 582 583 if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) { 584 SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n", 585 SPDK_SCSI_DEV_MAX_PORTS); 586 return NULL; 587 } 588 589 pg_map = calloc(1, sizeof(*pg_map)); 590 if (pg_map == NULL) { 591 return NULL; 592 } 593 594 snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x", 595 spdk_scsi_dev_get_name(target->dev), pg->tag); 596 rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name); 597 if (rc != 0) { 598 free(pg_map); 599 return NULL; 600 } 601 602 TAILQ_INIT(&pg_map->ig_map_head); 603 pg_map->num_ig_maps = 0; 604 pg->ref++; 605 pg_map->pg = pg; 606 target->num_pg_maps++; 607 TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq); 608 609 return pg_map; 610 } 611 612 static void 613 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 614 struct spdk_iscsi_pg_map *pg_map) 615 { 616 TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq); 617 target->num_pg_maps--; 618 pg_map->pg->ref--; 619 620 spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag); 621 622 free(pg_map); 623 } 624 625 static int 626 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 627 struct spdk_iscsi_portal_grp *pg) 628 { 629 struct spdk_iscsi_pg_map *pg_map; 630 631 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 632 if (pg_map == NULL) { 633 return -ENOENT; 634 } 635 636 if (pg_map->num_ig_maps > 0) { 637 SPDK_DEBUGLOG(iscsi, "delete %d ig_maps forcefully\n", 638 pg_map->num_ig_maps); 639 } 640 641 iscsi_pg_map_delete_all_ig_maps(pg_map); 642 _iscsi_tgt_node_delete_pg_map(target, pg_map); 643 return 0; 644 } 645 646 static void 647 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target, 648 struct spdk_iscsi_init_grp *ig) 649 { 650 struct spdk_iscsi_pg_map *pg_map, *tmp; 651 652 TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) { 653 iscsi_pg_map_delete_ig_map(pg_map, ig); 654 if (pg_map->num_ig_maps == 0) { 655 _iscsi_tgt_node_delete_pg_map(target, pg_map); 656 } 657 } 658 } 659 660 static void 661 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target) 662 { 663 struct spdk_iscsi_pg_map *pg_map, *tmp; 664 665 TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) { 666 iscsi_pg_map_delete_all_ig_maps(pg_map); 667 _iscsi_tgt_node_delete_pg_map(target, pg_map); 668 } 669 } 670 671 static void 672 _iscsi_tgt_node_destruct(void *cb_arg, int rc) 673 { 674 struct spdk_iscsi_tgt_node *target = cb_arg; 675 iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn; 676 void *destruct_cb_arg = target->destruct_cb_arg; 677 678 if (rc != 0) { 679 if (destruct_cb_fn) { 680 destruct_cb_fn(destruct_cb_arg, rc); 681 } 682 return; 683 } 684 685 pthread_mutex_lock(&g_iscsi.mutex); 686 iscsi_tgt_node_delete_all_pg_maps(target); 687 pthread_mutex_unlock(&g_iscsi.mutex); 688 689 pthread_mutex_destroy(&target->mutex); 690 free(target); 691 692 if (destruct_cb_fn) { 693 destruct_cb_fn(destruct_cb_arg, 0); 694 } 695 } 696 697 static int 698 iscsi_tgt_node_check_active_conns(void *arg) 699 { 700 struct spdk_iscsi_tgt_node *target = arg; 701 702 if (iscsi_get_active_conns(target) != 0) { 703 return SPDK_POLLER_BUSY; 704 } 705 706 spdk_poller_unregister(&target->destruct_poller); 707 708 spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target); 709 710 return SPDK_POLLER_BUSY; 711 } 712 713 static void 714 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target, 715 iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg) 716 { 717 if (target == NULL) { 718 if (cb_fn) { 719 cb_fn(cb_arg, -ENOENT); 720 } 721 return; 722 } 723 724 if (target->destructed) { 725 SPDK_ERRLOG("Destructing %s is already started\n", target->name); 726 if (cb_fn) { 727 cb_fn(cb_arg, -EBUSY); 728 } 729 return; 730 } 731 732 target->destructed = true; 733 target->destruct_cb_fn = cb_fn; 734 target->destruct_cb_arg = cb_arg; 735 736 iscsi_conns_request_logout(target, -1); 737 738 if (iscsi_get_active_conns(target) != 0) { 739 target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns, 740 target, 10); 741 } else { 742 spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target); 743 } 744 745 } 746 747 static int 748 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target, 749 int pg_tag, int ig_tag) 750 { 751 struct spdk_iscsi_portal_grp *pg; 752 struct spdk_iscsi_init_grp *ig; 753 struct spdk_iscsi_pg_map *pg_map; 754 struct spdk_iscsi_ig_map *ig_map; 755 756 pg = iscsi_portal_grp_find_by_tag(pg_tag); 757 if (pg == NULL) { 758 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 759 return -ENOENT; 760 } 761 ig = iscsi_init_grp_find_by_tag(ig_tag); 762 if (ig == NULL) { 763 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 764 return -ENOENT; 765 } 766 767 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 768 if (pg_map == NULL) { 769 SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag); 770 return -ENOENT; 771 } 772 ig_map = iscsi_pg_map_find_ig_map(pg_map, ig); 773 if (ig_map == NULL) { 774 SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag); 775 return -ENOENT; 776 } 777 778 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 779 if (pg_map->num_ig_maps == 0) { 780 _iscsi_tgt_node_delete_pg_map(target, pg_map); 781 } 782 783 return 0; 784 } 785 786 static int 787 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target, 788 int pg_tag, int ig_tag) 789 { 790 struct spdk_iscsi_portal_grp *pg; 791 struct spdk_iscsi_pg_map *pg_map; 792 struct spdk_iscsi_init_grp *ig; 793 struct spdk_iscsi_ig_map *ig_map; 794 bool new_pg_map = false; 795 796 pg = iscsi_portal_grp_find_by_tag(pg_tag); 797 if (pg == NULL) { 798 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 799 return -ENOENT; 800 } 801 ig = iscsi_init_grp_find_by_tag(ig_tag); 802 if (ig == NULL) { 803 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 804 return -ENOENT; 805 } 806 807 /* get existing pg_map or create new pg_map and add it to target */ 808 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 809 if (pg_map == NULL) { 810 pg_map = iscsi_tgt_node_add_pg_map(target, pg); 811 if (pg_map == NULL) { 812 goto failed; 813 } 814 new_pg_map = true; 815 } 816 817 /* create new ig_map and add it to pg_map */ 818 ig_map = iscsi_pg_map_add_ig_map(pg_map, ig); 819 if (ig_map == NULL) { 820 goto failed; 821 } 822 823 return 0; 824 825 failed: 826 if (new_pg_map) { 827 _iscsi_tgt_node_delete_pg_map(target, pg_map); 828 } 829 830 return -1; 831 } 832 833 int 834 iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 835 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 836 { 837 uint16_t i; 838 int rc; 839 840 pthread_mutex_lock(&g_iscsi.mutex); 841 for (i = 0; i < num_maps; i++) { 842 rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i], 843 ig_tag_list[i]); 844 if (rc != 0) { 845 SPDK_ERRLOG("could not add map to target\n"); 846 goto invalid; 847 } 848 } 849 pthread_mutex_unlock(&g_iscsi.mutex); 850 return 0; 851 852 invalid: 853 for (; i > 0; --i) { 854 iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1], 855 ig_tag_list[i - 1]); 856 } 857 pthread_mutex_unlock(&g_iscsi.mutex); 858 return -1; 859 } 860 861 int 862 iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 863 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 864 { 865 uint16_t i; 866 int rc; 867 868 pthread_mutex_lock(&g_iscsi.mutex); 869 for (i = 0; i < num_maps; i++) { 870 rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i], 871 ig_tag_list[i]); 872 if (rc != 0) { 873 SPDK_ERRLOG("could not delete map from target\n"); 874 goto invalid; 875 } 876 } 877 pthread_mutex_unlock(&g_iscsi.mutex); 878 return 0; 879 880 invalid: 881 for (; i > 0; --i) { 882 rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1], 883 ig_tag_list[i - 1]); 884 if (rc != 0) { 885 iscsi_tgt_node_delete_all_pg_maps(target); 886 break; 887 } 888 } 889 pthread_mutex_unlock(&g_iscsi.mutex); 890 return -1; 891 } 892 893 int 894 iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node *target, int pg_tag, 895 const char *host, const char *port) 896 { 897 struct spdk_iscsi_portal_grp *pg; 898 struct spdk_iscsi_pg_map *pg_map; 899 struct sockaddr_storage sa; 900 901 if (target == NULL) { 902 return -EINVAL; 903 } 904 905 pg = iscsi_portal_grp_find_by_tag(pg_tag); 906 if (pg == NULL) { 907 SPDK_ERRLOG("Portal group %d is not found.\n", pg_tag); 908 return -EINVAL; 909 } 910 911 if (pg->is_private) { 912 SPDK_ERRLOG("Portal group %d is not public portal group.\n", pg_tag); 913 return -EINVAL; 914 } 915 916 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 917 if (pg_map == NULL) { 918 SPDK_ERRLOG("Portal group %d is not mapped.\n", pg_tag); 919 return -EINVAL; 920 } 921 922 if (host == NULL && port == NULL) { 923 /* Clear redirect setting. */ 924 memset(pg_map->redirect_host, 0, MAX_PORTAL_ADDR + 1); 925 memset(pg_map->redirect_port, 0, MAX_PORTAL_PORT + 1); 926 } else { 927 if (iscsi_parse_redirect_addr(&sa, host, port) != 0) { 928 SPDK_ERRLOG("IP address-port pair is not valid.\n"); 929 return -EINVAL; 930 } 931 932 if (iscsi_portal_grp_find_portal_by_addr(pg, port, host) != NULL) { 933 SPDK_ERRLOG("IP address-port pair must be chosen from a " 934 "different private portal group\n"); 935 return -EINVAL; 936 } 937 938 snprintf(pg_map->redirect_host, MAX_PORTAL_ADDR + 1, "%s", host); 939 snprintf(pg_map->redirect_port, MAX_PORTAL_PORT + 1, "%s", port); 940 } 941 942 return 0; 943 } 944 945 bool 946 iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn *conn, 947 struct spdk_iscsi_tgt_node *target, 948 char *buf, int buf_len) 949 { 950 struct spdk_iscsi_pg_map *pg_map; 951 952 if (conn == NULL || target == NULL || buf == NULL || buf_len == 0) { 953 return false; 954 } 955 956 pg_map = iscsi_tgt_node_find_pg_map(target, conn->portal->group); 957 if (pg_map == NULL) { 958 return false; 959 } 960 961 if (pg_map->redirect_host[0] == '\0' || pg_map->redirect_port[0] == '\0') { 962 return false; 963 } 964 965 snprintf(buf, buf_len, "%s:%s", pg_map->redirect_host, pg_map->redirect_port); 966 967 return true; 968 } 969 970 static int 971 check_iscsi_name(const char *name) 972 { 973 const unsigned char *up = (const unsigned char *) name; 974 size_t n; 975 976 /* valid iSCSI name no larger than 223 bytes */ 977 if (strlen(name) > MAX_TARGET_NAME) { 978 return -1; 979 } 980 981 /* valid iSCSI name? */ 982 for (n = 0; up[n] != 0; n++) { 983 if (up[n] > 0x00U && up[n] <= 0x2cU) { 984 return -1; 985 } 986 if (up[n] == 0x2fU) { 987 return -1; 988 } 989 if (up[n] >= 0x3bU && up[n] <= 0x40U) { 990 return -1; 991 } 992 if (up[n] >= 0x5bU && up[n] <= 0x60U) { 993 return -1; 994 } 995 if (up[n] >= 0x7bU && up[n] <= 0x7fU) { 996 return -1; 997 } 998 if (isspace(up[n])) { 999 return -1; 1000 } 1001 } 1002 /* valid format? */ 1003 if (strncasecmp(name, "iqn.", 4) == 0) { 1004 /* iqn.YYYY-MM.reversed.domain.name */ 1005 if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6]) 1006 || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9]) 1007 || !isdigit(up[10]) || up[11] != '.') { 1008 SPDK_ERRLOG("invalid iqn format. " 1009 "expect \"iqn.YYYY-MM.reversed.domain.name\"\n"); 1010 return -1; 1011 } 1012 } else if (strncasecmp(name, "eui.", 4) == 0) { 1013 /* EUI-64 -> 16bytes */ 1014 /* XXX */ 1015 } else if (strncasecmp(name, "naa.", 4) == 0) { 1016 /* 64bit -> 16bytes, 128bit -> 32bytes */ 1017 /* XXX */ 1018 } 1019 /* OK */ 1020 return 0; 1021 } 1022 1023 bool 1024 iscsi_check_chap_params(bool disable, bool require, bool mutual, int group) 1025 { 1026 if (group < 0) { 1027 SPDK_ERRLOG("Invalid auth group ID (%d)\n", group); 1028 return false; 1029 } 1030 if ((!disable && !require && !mutual) || /* Auto */ 1031 (disable && !require && !mutual) || /* None */ 1032 (!disable && require && !mutual) || /* CHAP */ 1033 (!disable && require && mutual)) { /* CHAP Mutual */ 1034 return true; 1035 } 1036 SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n", 1037 disable, require, mutual); 1038 return false; 1039 } 1040 1041 struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index, 1042 const char *name, const char *alias, 1043 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps, 1044 const char *bdev_name_list[], int *lun_id_list, int num_luns, 1045 int queue_depth, 1046 bool disable_chap, bool require_chap, bool mutual_chap, int chap_group, 1047 bool header_digest, bool data_digest) 1048 { 1049 char fullname[MAX_TMPBUF]; 1050 struct spdk_iscsi_tgt_node *target; 1051 int rc; 1052 1053 if (!iscsi_check_chap_params(disable_chap, require_chap, 1054 mutual_chap, chap_group)) { 1055 return NULL; 1056 } 1057 1058 if (num_maps == 0) { 1059 SPDK_ERRLOG("num_maps = 0\n"); 1060 return NULL; 1061 } 1062 1063 if (name == NULL) { 1064 SPDK_ERRLOG("TargetName not found\n"); 1065 return NULL; 1066 } 1067 1068 if (strncasecmp(name, "iqn.", 4) != 0 1069 && strncasecmp(name, "eui.", 4) != 0 1070 && strncasecmp(name, "naa.", 4) != 0) { 1071 snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name); 1072 } else { 1073 snprintf(fullname, sizeof(fullname), "%s", name); 1074 } 1075 1076 if (check_iscsi_name(fullname) != 0) { 1077 SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n", 1078 name); 1079 return NULL; 1080 } 1081 1082 target = calloc(1, sizeof(*target)); 1083 if (!target) { 1084 SPDK_ERRLOG("could not allocate target\n"); 1085 return NULL; 1086 } 1087 1088 rc = pthread_mutex_init(&target->mutex, NULL); 1089 if (rc != 0) { 1090 SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num); 1091 iscsi_tgt_node_destruct(target, NULL, NULL); 1092 return NULL; 1093 } 1094 1095 target->num = target_index; 1096 1097 memcpy(target->name, fullname, strlen(fullname)); 1098 1099 if (alias != NULL) { 1100 if (strlen(alias) > MAX_TARGET_NAME) { 1101 iscsi_tgt_node_destruct(target, NULL, NULL); 1102 return NULL; 1103 } 1104 memcpy(target->alias, alias, strlen(alias)); 1105 } 1106 1107 target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns, 1108 SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL); 1109 if (!target->dev) { 1110 SPDK_ERRLOG("Could not construct SCSI device\n"); 1111 iscsi_tgt_node_destruct(target, NULL, NULL); 1112 return NULL; 1113 } 1114 1115 TAILQ_INIT(&target->pg_map_head); 1116 rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list, 1117 ig_tag_list, num_maps); 1118 if (rc != 0) { 1119 SPDK_ERRLOG("could not add map to target\n"); 1120 iscsi_tgt_node_destruct(target, NULL, NULL); 1121 return NULL; 1122 } 1123 1124 target->disable_chap = disable_chap; 1125 target->require_chap = require_chap; 1126 target->mutual_chap = mutual_chap; 1127 target->chap_group = chap_group; 1128 target->header_digest = header_digest; 1129 target->data_digest = data_digest; 1130 1131 if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) { 1132 target->queue_depth = queue_depth; 1133 } else { 1134 SPDK_DEBUGLOG(iscsi, "QueueDepth %d is invalid and %d is used instead.\n", 1135 queue_depth, g_iscsi.MaxQueueDepth); 1136 target->queue_depth = g_iscsi.MaxQueueDepth; 1137 } 1138 1139 rc = iscsi_tgt_node_register(target); 1140 if (rc != 0) { 1141 SPDK_ERRLOG("register target is failed\n"); 1142 iscsi_tgt_node_destruct(target, NULL, NULL); 1143 return NULL; 1144 } 1145 1146 return target; 1147 } 1148 1149 void 1150 iscsi_shutdown_tgt_nodes(void) 1151 { 1152 struct spdk_iscsi_tgt_node *target; 1153 1154 pthread_mutex_lock(&g_iscsi.mutex); 1155 while (!TAILQ_EMPTY(&g_iscsi.target_head)) { 1156 target = TAILQ_FIRST(&g_iscsi.target_head); 1157 TAILQ_REMOVE(&g_iscsi.target_head, target, tailq); 1158 1159 pthread_mutex_unlock(&g_iscsi.mutex); 1160 1161 iscsi_tgt_node_destruct(target, NULL, NULL); 1162 1163 pthread_mutex_lock(&g_iscsi.mutex); 1164 } 1165 pthread_mutex_unlock(&g_iscsi.mutex); 1166 } 1167 1168 void 1169 iscsi_shutdown_tgt_node_by_name(const char *target_name, 1170 iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg) 1171 { 1172 struct spdk_iscsi_tgt_node *target; 1173 1174 pthread_mutex_lock(&g_iscsi.mutex); 1175 target = iscsi_find_tgt_node(target_name); 1176 if (target != NULL) { 1177 iscsi_tgt_node_unregister(target); 1178 pthread_mutex_unlock(&g_iscsi.mutex); 1179 1180 iscsi_tgt_node_destruct(target, cb_fn, cb_arg); 1181 1182 return; 1183 } 1184 pthread_mutex_unlock(&g_iscsi.mutex); 1185 1186 if (cb_fn) { 1187 cb_fn(cb_arg, -ENOENT); 1188 } 1189 } 1190 1191 bool 1192 iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target) 1193 { 1194 return target->destructed; 1195 } 1196 1197 int 1198 iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn, 1199 struct spdk_iscsi_tgt_node *target) 1200 { 1201 struct spdk_scsi_lun *lun; 1202 struct spdk_iscsi_task *task; 1203 1204 for (lun = spdk_scsi_dev_get_first_lun(target->dev); lun != NULL; 1205 lun = spdk_scsi_dev_get_next_lun(lun)) { 1206 /* we create a fake management task per LUN to cleanup */ 1207 task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl); 1208 if (!task) { 1209 SPDK_ERRLOG("Unable to acquire task\n"); 1210 return -1; 1211 } 1212 1213 task->scsi.target_port = conn->target_port; 1214 task->scsi.initiator_port = conn->initiator_port; 1215 task->scsi.lun = lun; 1216 1217 iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET); 1218 } 1219 1220 return 0; 1221 } 1222 1223 void iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group, 1224 struct spdk_iscsi_init_grp *initiator_group) 1225 { 1226 struct spdk_iscsi_tgt_node *target; 1227 1228 pthread_mutex_lock(&g_iscsi.mutex); 1229 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 1230 if (portal_group) { 1231 iscsi_tgt_node_delete_pg_map(target, portal_group); 1232 } 1233 if (initiator_group) { 1234 iscsi_tgt_node_delete_ig_maps(target, initiator_group); 1235 } 1236 } 1237 pthread_mutex_unlock(&g_iscsi.mutex); 1238 } 1239 1240 int 1241 iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target, 1242 const char *bdev_name, int lun_id) 1243 { 1244 struct spdk_scsi_dev *dev; 1245 int rc; 1246 1247 if (target->num_active_conns > 0) { 1248 SPDK_ERRLOG("Target has active connections (count=%d)\n", 1249 target->num_active_conns); 1250 return -1; 1251 } 1252 1253 if (lun_id < -1) { 1254 SPDK_ERRLOG("Specified LUN ID (%d) is negative\n", lun_id); 1255 return -1; 1256 } 1257 1258 dev = target->dev; 1259 if (dev == NULL) { 1260 SPDK_ERRLOG("SCSI device is not found\n"); 1261 return -1; 1262 } 1263 1264 rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL); 1265 if (rc != 0) { 1266 SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n"); 1267 return -1; 1268 } 1269 1270 return 0; 1271 } 1272 1273 int 1274 iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target, 1275 bool disable_chap, bool require_chap, 1276 bool mutual_chap, int32_t chap_group) 1277 { 1278 if (!iscsi_check_chap_params(disable_chap, require_chap, 1279 mutual_chap, chap_group)) { 1280 return -EINVAL; 1281 } 1282 1283 pthread_mutex_lock(&target->mutex); 1284 target->disable_chap = disable_chap; 1285 target->require_chap = require_chap; 1286 target->mutual_chap = mutual_chap; 1287 target->chap_group = chap_group; 1288 pthread_mutex_unlock(&target->mutex); 1289 1290 return 0; 1291 } 1292 1293 static void 1294 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target, 1295 struct spdk_json_write_ctx *w) 1296 { 1297 struct spdk_iscsi_pg_map *pg_map; 1298 struct spdk_iscsi_ig_map *ig_map; 1299 struct spdk_scsi_lun *lun; 1300 1301 spdk_json_write_object_begin(w); 1302 1303 spdk_json_write_named_string(w, "name", target->name); 1304 1305 if (target->alias[0] != '\0') { 1306 spdk_json_write_named_string(w, "alias_name", target->alias); 1307 } 1308 1309 spdk_json_write_named_array_begin(w, "pg_ig_maps"); 1310 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 1311 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 1312 spdk_json_write_object_begin(w); 1313 spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag); 1314 spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag); 1315 spdk_json_write_object_end(w); 1316 } 1317 } 1318 spdk_json_write_array_end(w); 1319 1320 spdk_json_write_named_array_begin(w, "luns"); 1321 for (lun = spdk_scsi_dev_get_first_lun(target->dev); lun != NULL; 1322 lun = spdk_scsi_dev_get_next_lun(lun)) { 1323 spdk_json_write_object_begin(w); 1324 spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun)); 1325 spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun)); 1326 spdk_json_write_object_end(w); 1327 } 1328 spdk_json_write_array_end(w); 1329 1330 spdk_json_write_named_int32(w, "queue_depth", target->queue_depth); 1331 1332 spdk_json_write_named_bool(w, "disable_chap", target->disable_chap); 1333 spdk_json_write_named_bool(w, "require_chap", target->require_chap); 1334 spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap); 1335 spdk_json_write_named_int32(w, "chap_group", target->chap_group); 1336 1337 spdk_json_write_named_bool(w, "header_digest", target->header_digest); 1338 spdk_json_write_named_bool(w, "data_digest", target->data_digest); 1339 1340 spdk_json_write_object_end(w); 1341 } 1342 1343 static void 1344 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target, 1345 struct spdk_json_write_ctx *w) 1346 { 1347 spdk_json_write_object_begin(w); 1348 1349 spdk_json_write_named_string(w, "method", "iscsi_create_target_node"); 1350 1351 spdk_json_write_name(w, "params"); 1352 iscsi_tgt_node_info_json(target, w); 1353 1354 spdk_json_write_object_end(w); 1355 } 1356 1357 void 1358 iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w) 1359 { 1360 struct spdk_iscsi_tgt_node *target; 1361 1362 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 1363 iscsi_tgt_node_info_json(target, w); 1364 } 1365 } 1366 1367 void 1368 iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w) 1369 { 1370 struct spdk_iscsi_tgt_node *target; 1371 1372 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 1373 iscsi_tgt_node_config_json(target, w); 1374 } 1375 } 1376