1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003, 2004 Silicon Graphics International Corp. 5 * Copyright (c) 1997-2007 Kenneth D. Merry 6 * Copyright (c) 2012 The FreeBSD Foundation 7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org> 8 * All rights reserved. 9 * 10 * Portions of this software were developed by Edward Tomasz Napierala 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions, and the following disclaimer, 18 * without modification. 19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 20 * substantially similar to the "NO WARRANTY" disclaimer below 21 * ("Disclaimer") and any redistribution must be conditioned upon 22 * including a substantially similar Disclaimer requirement for further 23 * binary redistribution. 24 * 25 * NO WARRANTY 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGES. 37 * 38 */ 39 40 #include <sys/param.h> 41 #include <sys/capsicum.h> 42 #include <sys/callout.h> 43 #include <sys/cnv.h> 44 #include <sys/ioctl.h> 45 #include <sys/linker.h> 46 #include <sys/module.h> 47 #include <sys/queue.h> 48 #include <sys/sbuf.h> 49 #include <sys/nv.h> 50 #include <sys/stat.h> 51 #include <assert.h> 52 #include <bsdxml.h> 53 #include <capsicum_helpers.h> 54 #include <ctype.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <stdint.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <strings.h> 62 #include <cam/scsi/scsi_all.h> 63 #include <cam/scsi/scsi_message.h> 64 #include <cam/ctl/ctl.h> 65 #include <cam/ctl/ctl_io.h> 66 #include <cam/ctl/ctl_backend.h> 67 #include <cam/ctl/ctl_ioctl.h> 68 #include <cam/ctl/ctl_util.h> 69 #include <cam/ctl/ctl_scsi_all.h> 70 71 #include "ctld.h" 72 73 #ifdef ICL_KERNEL_PROXY 74 #include <netdb.h> 75 #endif 76 77 #define NVLIST_BUFSIZE 1024 78 79 extern bool proxy_mode; 80 81 int ctl_fd = 0; 82 83 void 84 kernel_init(void) 85 { 86 int retval, saved_errno; 87 88 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR); 89 if (ctl_fd < 0 && errno == ENOENT) { 90 saved_errno = errno; 91 retval = kldload("ctl"); 92 if (retval != -1) 93 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR); 94 else 95 errno = saved_errno; 96 } 97 if (ctl_fd < 0) 98 log_err(1, "failed to open %s", CTL_DEFAULT_DEV); 99 #ifdef WANT_ISCSI 100 else { 101 saved_errno = errno; 102 if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1) 103 log_warn("couldn't load cfiscsi"); 104 errno = saved_errno; 105 } 106 #endif 107 } 108 109 /* 110 * Backend LUN information. 111 */ 112 struct cctl_lun { 113 uint64_t lun_id; 114 char *backend_type; 115 uint8_t device_type; 116 uint64_t size_blocks; 117 uint32_t blocksize; 118 char *serial_number; 119 char *device_id; 120 char *ctld_name; 121 nvlist_t *attr_list; 122 STAILQ_ENTRY(cctl_lun) links; 123 }; 124 125 struct cctl_port { 126 uint32_t port_id; 127 char *port_frontend; 128 char *port_name; 129 int pp; 130 int vp; 131 int cfiscsi_state; 132 char *cfiscsi_target; 133 uint16_t cfiscsi_portal_group_tag; 134 char *ctld_portal_group_name; 135 nvlist_t *attr_list; 136 STAILQ_ENTRY(cctl_port) links; 137 }; 138 139 struct cctl_devlist_data { 140 int num_luns; 141 STAILQ_HEAD(,cctl_lun) lun_list; 142 struct cctl_lun *cur_lun; 143 int num_ports; 144 STAILQ_HEAD(,cctl_port) port_list; 145 struct cctl_port *cur_port; 146 int level; 147 struct sbuf *cur_sb[32]; 148 }; 149 150 static void 151 cctl_start_element(void *user_data, const char *name, const char **attr) 152 { 153 int i; 154 struct cctl_devlist_data *devlist; 155 struct cctl_lun *cur_lun; 156 157 devlist = (struct cctl_devlist_data *)user_data; 158 cur_lun = devlist->cur_lun; 159 devlist->level++; 160 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 161 sizeof(devlist->cur_sb[0]))) 162 log_errx(1, "%s: too many nesting levels, %zd max", __func__, 163 nitems(devlist->cur_sb)); 164 165 devlist->cur_sb[devlist->level] = sbuf_new_auto(); 166 if (devlist->cur_sb[devlist->level] == NULL) 167 log_err(1, "%s: unable to allocate sbuf", __func__); 168 169 if (strcmp(name, "lun") == 0) { 170 if (cur_lun != NULL) 171 log_errx(1, "%s: improper lun element nesting", 172 __func__); 173 174 cur_lun = reinterpret_cast<struct cctl_lun *>(calloc(1, sizeof(*cur_lun))); 175 if (cur_lun == NULL) 176 log_err(1, "%s: cannot allocate %zd bytes", __func__, 177 sizeof(*cur_lun)); 178 179 devlist->num_luns++; 180 devlist->cur_lun = cur_lun; 181 182 cur_lun->attr_list = nvlist_create(0); 183 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links); 184 185 for (i = 0; attr[i] != NULL; i += 2) { 186 if (strcmp(attr[i], "id") == 0) { 187 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0); 188 } else { 189 log_errx(1, "%s: invalid LUN attribute %s = %s", 190 __func__, attr[i], attr[i+1]); 191 } 192 } 193 } 194 } 195 196 static void 197 cctl_end_element(void *user_data, const char *name) 198 { 199 struct cctl_devlist_data *devlist; 200 struct cctl_lun *cur_lun; 201 char *str; 202 int error; 203 204 devlist = (struct cctl_devlist_data *)user_data; 205 cur_lun = devlist->cur_lun; 206 207 if ((cur_lun == NULL) 208 && (strcmp(name, "ctllunlist") != 0)) 209 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name); 210 211 if (devlist->cur_sb[devlist->level] == NULL) 212 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 213 devlist->level, name); 214 215 sbuf_finish(devlist->cur_sb[devlist->level]); 216 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 217 218 if (strlen(str) == 0) { 219 free(str); 220 str = NULL; 221 } 222 223 sbuf_delete(devlist->cur_sb[devlist->level]); 224 devlist->cur_sb[devlist->level] = NULL; 225 devlist->level--; 226 227 if (strcmp(name, "backend_type") == 0) { 228 cur_lun->backend_type = str; 229 str = NULL; 230 } else if (strcmp(name, "lun_type") == 0) { 231 if (str == NULL) 232 log_errx(1, "%s: %s missing its argument", __func__, name); 233 cur_lun->device_type = strtoull(str, NULL, 0); 234 } else if (strcmp(name, "size") == 0) { 235 if (str == NULL) 236 log_errx(1, "%s: %s missing its argument", __func__, name); 237 cur_lun->size_blocks = strtoull(str, NULL, 0); 238 } else if (strcmp(name, "blocksize") == 0) { 239 if (str == NULL) 240 log_errx(1, "%s: %s missing its argument", __func__, name); 241 cur_lun->blocksize = strtoul(str, NULL, 0); 242 } else if (strcmp(name, "serial_number") == 0) { 243 cur_lun->serial_number = str; 244 str = NULL; 245 } else if (strcmp(name, "device_id") == 0) { 246 cur_lun->device_id = str; 247 str = NULL; 248 } else if (strcmp(name, "ctld_name") == 0) { 249 cur_lun->ctld_name = str; 250 str = NULL; 251 } else if (strcmp(name, "lun") == 0) { 252 devlist->cur_lun = NULL; 253 } else if (strcmp(name, "ctllunlist") == 0) { 254 /* Nothing. */ 255 } else { 256 nvlist_move_string(cur_lun->attr_list, name, str); 257 error = nvlist_error(cur_lun->attr_list); 258 if (error != 0) 259 log_errc(1, error, "%s: failed to add nv pair for %s", 260 __func__, name); 261 str = NULL; 262 } 263 264 free(str); 265 } 266 267 static void 268 cctl_start_pelement(void *user_data, const char *name, const char **attr) 269 { 270 int i; 271 struct cctl_devlist_data *devlist; 272 struct cctl_port *cur_port; 273 274 devlist = (struct cctl_devlist_data *)user_data; 275 cur_port = devlist->cur_port; 276 devlist->level++; 277 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 278 sizeof(devlist->cur_sb[0]))) 279 log_errx(1, "%s: too many nesting levels, %zd max", __func__, 280 nitems(devlist->cur_sb)); 281 282 devlist->cur_sb[devlist->level] = sbuf_new_auto(); 283 if (devlist->cur_sb[devlist->level] == NULL) 284 log_err(1, "%s: unable to allocate sbuf", __func__); 285 286 if (strcmp(name, "targ_port") == 0) { 287 if (cur_port != NULL) 288 log_errx(1, "%s: improper port element nesting (%s)", 289 __func__, name); 290 291 cur_port = reinterpret_cast<struct cctl_port *>(calloc(1, sizeof(*cur_port))); 292 if (cur_port == NULL) 293 log_err(1, "%s: cannot allocate %zd bytes", __func__, 294 sizeof(*cur_port)); 295 296 devlist->num_ports++; 297 devlist->cur_port = cur_port; 298 299 cur_port->attr_list = nvlist_create(0); 300 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links); 301 302 for (i = 0; attr[i] != NULL; i += 2) { 303 if (strcmp(attr[i], "id") == 0) { 304 cur_port->port_id = strtoul(attr[i+1], NULL, 0); 305 } else { 306 log_errx(1, "%s: invalid LUN attribute %s = %s", 307 __func__, attr[i], attr[i+1]); 308 } 309 } 310 } 311 } 312 313 static void 314 cctl_end_pelement(void *user_data, const char *name) 315 { 316 struct cctl_devlist_data *devlist; 317 struct cctl_port *cur_port; 318 char *str; 319 int error; 320 321 devlist = (struct cctl_devlist_data *)user_data; 322 cur_port = devlist->cur_port; 323 324 if ((cur_port == NULL) 325 && (strcmp(name, "ctlportlist") != 0)) 326 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name); 327 328 if (devlist->cur_sb[devlist->level] == NULL) 329 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 330 devlist->level, name); 331 332 sbuf_finish(devlist->cur_sb[devlist->level]); 333 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 334 335 if (strlen(str) == 0) { 336 free(str); 337 str = NULL; 338 } 339 340 sbuf_delete(devlist->cur_sb[devlist->level]); 341 devlist->cur_sb[devlist->level] = NULL; 342 devlist->level--; 343 344 if (strcmp(name, "frontend_type") == 0) { 345 cur_port->port_frontend = str; 346 str = NULL; 347 } else if (strcmp(name, "port_name") == 0) { 348 cur_port->port_name = str; 349 str = NULL; 350 } else if (strcmp(name, "physical_port") == 0) { 351 if (str == NULL) 352 log_errx(1, "%s: %s missing its argument", __func__, name); 353 cur_port->pp = strtoul(str, NULL, 0); 354 } else if (strcmp(name, "virtual_port") == 0) { 355 if (str == NULL) 356 log_errx(1, "%s: %s missing its argument", __func__, name); 357 cur_port->vp = strtoul(str, NULL, 0); 358 } else if (strcmp(name, "cfiscsi_target") == 0) { 359 cur_port->cfiscsi_target = str; 360 str = NULL; 361 } else if (strcmp(name, "cfiscsi_state") == 0) { 362 if (str == NULL) 363 log_errx(1, "%s: %s missing its argument", __func__, name); 364 cur_port->cfiscsi_state = strtoul(str, NULL, 0); 365 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) { 366 if (str == NULL) 367 log_errx(1, "%s: %s missing its argument", __func__, name); 368 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0); 369 } else if (strcmp(name, "ctld_portal_group_name") == 0) { 370 cur_port->ctld_portal_group_name = str; 371 str = NULL; 372 } else if (strcmp(name, "targ_port") == 0) { 373 devlist->cur_port = NULL; 374 } else if (strcmp(name, "ctlportlist") == 0) { 375 /* Nothing. */ 376 } else { 377 nvlist_move_string(cur_port->attr_list, name, str); 378 error = nvlist_error(cur_port->attr_list); 379 if (error != 0) 380 log_errc(1, error, "%s: failed to add nv pair for %s", 381 __func__, name); 382 str = NULL; 383 } 384 385 free(str); 386 } 387 388 static void 389 cctl_char_handler(void *user_data, const XML_Char *str, int len) 390 { 391 struct cctl_devlist_data *devlist; 392 393 devlist = (struct cctl_devlist_data *)user_data; 394 395 sbuf_bcat(devlist->cur_sb[devlist->level], str, len); 396 } 397 398 struct conf * 399 conf_new_from_kernel(struct kports *kports) 400 { 401 struct conf *conf = NULL; 402 struct target *targ; 403 struct portal_group *pg; 404 struct pport *pp; 405 struct port *cp; 406 struct lun *cl; 407 struct ctl_lun_list list; 408 struct cctl_devlist_data devlist; 409 struct cctl_lun *lun; 410 struct cctl_port *port; 411 XML_Parser parser; 412 const char *key; 413 char *str, *name; 414 void *cookie; 415 int error, len, retval; 416 417 bzero(&devlist, sizeof(devlist)); 418 STAILQ_INIT(&devlist.lun_list); 419 STAILQ_INIT(&devlist.port_list); 420 421 log_debugx("obtaining previously configured CTL luns from the kernel"); 422 423 str = NULL; 424 len = 4096; 425 retry: 426 str = reinterpret_cast<char *>(realloc(str, len)); 427 if (str == NULL) 428 log_err(1, "realloc"); 429 430 bzero(&list, sizeof(list)); 431 list.alloc_len = len; 432 list.status = CTL_LUN_LIST_NONE; 433 list.lun_xml = str; 434 435 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) { 436 log_warn("error issuing CTL_LUN_LIST ioctl"); 437 free(str); 438 return (NULL); 439 } 440 441 if (list.status == CTL_LUN_LIST_ERROR) { 442 log_warnx("error returned from CTL_LUN_LIST ioctl: %s", 443 list.error_str); 444 free(str); 445 return (NULL); 446 } 447 448 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 449 len = len << 1; 450 goto retry; 451 } 452 453 parser = XML_ParserCreate(NULL); 454 if (parser == NULL) { 455 log_warnx("unable to create XML parser"); 456 free(str); 457 return (NULL); 458 } 459 460 XML_SetUserData(parser, &devlist); 461 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element); 462 XML_SetCharacterDataHandler(parser, cctl_char_handler); 463 464 retval = XML_Parse(parser, str, strlen(str), 1); 465 XML_ParserFree(parser); 466 free(str); 467 if (retval != 1) { 468 log_warnx("XML_Parse failed"); 469 return (NULL); 470 } 471 472 str = NULL; 473 len = 4096; 474 retry_port: 475 str = reinterpret_cast<char *>(realloc(str, len)); 476 if (str == NULL) 477 log_err(1, "realloc"); 478 479 bzero(&list, sizeof(list)); 480 list.alloc_len = len; 481 list.status = CTL_LUN_LIST_NONE; 482 list.lun_xml = str; 483 484 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) { 485 log_warn("error issuing CTL_PORT_LIST ioctl"); 486 free(str); 487 return (NULL); 488 } 489 490 if (list.status == CTL_LUN_LIST_ERROR) { 491 log_warnx("error returned from CTL_PORT_LIST ioctl: %s", 492 list.error_str); 493 free(str); 494 return (NULL); 495 } 496 497 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 498 len = len << 1; 499 goto retry_port; 500 } 501 502 parser = XML_ParserCreate(NULL); 503 if (parser == NULL) { 504 log_warnx("unable to create XML parser"); 505 free(str); 506 return (NULL); 507 } 508 509 XML_SetUserData(parser, &devlist); 510 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement); 511 XML_SetCharacterDataHandler(parser, cctl_char_handler); 512 513 retval = XML_Parse(parser, str, strlen(str), 1); 514 XML_ParserFree(parser); 515 free(str); 516 if (retval != 1) { 517 log_warnx("XML_Parse failed"); 518 return (NULL); 519 } 520 521 conf = conf_new(); 522 523 name = NULL; 524 STAILQ_FOREACH(port, &devlist.port_list, links) { 525 if (strcmp(port->port_frontend, "ha") == 0) 526 continue; 527 free(name); 528 if (port->pp == 0 && port->vp == 0) { 529 name = checked_strdup(port->port_name); 530 } else if (port->vp == 0) { 531 retval = asprintf(&name, "%s/%d", 532 port->port_name, port->pp); 533 if (retval <= 0) 534 log_err(1, "asprintf"); 535 } else { 536 retval = asprintf(&name, "%s/%d/%d", 537 port->port_name, port->pp, port->vp); 538 if (retval <= 0) 539 log_err(1, "asprintf"); 540 } 541 542 if (port->cfiscsi_target == NULL) { 543 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ", 544 port->port_id, name); 545 pp = pport_find(kports, name); 546 if (pp == NULL) { 547 pp = pport_new(kports, name, port->port_id); 548 if (pp == NULL) { 549 log_warnx("pport_new failed"); 550 continue; 551 } 552 } 553 continue; 554 } 555 if (port->cfiscsi_state != 1) { 556 log_debugx("CTL port %ju is not active (%d); ignoring", 557 (uintmax_t)port->port_id, port->cfiscsi_state); 558 continue; 559 } 560 561 targ = target_find(conf, port->cfiscsi_target); 562 if (targ == NULL) { 563 targ = target_new(conf, port->cfiscsi_target); 564 if (targ == NULL) { 565 log_warnx("target_new failed"); 566 continue; 567 } 568 } 569 570 if (port->ctld_portal_group_name == NULL) 571 continue; 572 pg = portal_group_find(conf, port->ctld_portal_group_name); 573 if (pg == NULL) { 574 pg = portal_group_new(conf, port->ctld_portal_group_name); 575 if (pg == NULL) { 576 log_warnx("portal_group_new failed"); 577 continue; 578 } 579 } 580 pg->pg_tag = port->cfiscsi_portal_group_tag; 581 cp = port_new(conf, targ, pg); 582 if (cp == NULL) { 583 log_warnx("port_new failed"); 584 continue; 585 } 586 cp->p_ctl_port = port->port_id; 587 } 588 while ((port = STAILQ_FIRST(&devlist.port_list))) { 589 STAILQ_REMOVE_HEAD(&devlist.port_list, links); 590 free(port->port_frontend); 591 free(port->port_name); 592 free(port->cfiscsi_target); 593 free(port->ctld_portal_group_name); 594 nvlist_destroy(port->attr_list); 595 free(port); 596 } 597 free(name); 598 599 STAILQ_FOREACH(lun, &devlist.lun_list, links) { 600 if (lun->ctld_name == NULL) { 601 log_debugx("CTL lun %ju wasn't managed by ctld; " 602 "ignoring", (uintmax_t)lun->lun_id); 603 continue; 604 } 605 606 cl = lun_find(conf, lun->ctld_name); 607 if (cl != NULL) { 608 log_warnx("found CTL lun %ju \"%s\", " 609 "also backed by CTL lun %d; ignoring", 610 (uintmax_t)lun->lun_id, lun->ctld_name, 611 cl->l_ctl_lun); 612 continue; 613 } 614 615 log_debugx("found CTL lun %ju \"%s\"", 616 (uintmax_t)lun->lun_id, lun->ctld_name); 617 618 cl = lun_new(conf, lun->ctld_name); 619 if (cl == NULL) { 620 log_warnx("lun_new failed"); 621 continue; 622 } 623 cl->l_backend = lun->backend_type; 624 lun->backend_type = NULL; 625 cl->l_device_type = lun->device_type; 626 cl->l_blocksize = lun->blocksize; 627 cl->l_device_id = lun->device_id; 628 lun->device_id = NULL; 629 cl->l_serial = lun->serial_number; 630 lun->serial_number = NULL; 631 cl->l_size = lun->size_blocks * cl->l_blocksize; 632 cl->l_ctl_lun = lun->lun_id; 633 634 cookie = NULL; 635 while ((key = nvlist_next(lun->attr_list, NULL, &cookie)) != 636 NULL) { 637 if (strcmp(key, "file") == 0 || 638 strcmp(key, "dev") == 0) { 639 cl->l_path = checked_strdup( 640 cnvlist_get_string(cookie)); 641 continue; 642 } 643 nvlist_add_string(cl->l_options, key, 644 cnvlist_get_string(cookie)); 645 error = nvlist_error(cl->l_options); 646 if (error != 0) 647 log_warnc(error, "unable to add CTL lun option " 648 "%s for CTL lun %ju \"%s\"", 649 key, (uintmax_t)lun->lun_id, 650 cl->l_name); 651 } 652 } 653 while ((lun = STAILQ_FIRST(&devlist.lun_list))) { 654 STAILQ_REMOVE_HEAD(&devlist.lun_list, links); 655 nvlist_destroy(lun->attr_list); 656 free(lun); 657 } 658 659 return (conf); 660 } 661 662 static void 663 nvlist_replace_string(nvlist_t *nvl, const char *name, const char *value) 664 { 665 if (nvlist_exists_string(nvl, name)) 666 nvlist_free_string(nvl, name); 667 nvlist_add_string(nvl, name, value); 668 } 669 670 int 671 kernel_lun_add(struct lun *lun) 672 { 673 struct ctl_lun_req req; 674 int error; 675 676 bzero(&req, sizeof(req)); 677 678 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 679 req.reqtype = CTL_LUNREQ_CREATE; 680 681 req.reqdata.create.blocksize_bytes = lun->l_blocksize; 682 683 if (lun->l_size != 0) 684 req.reqdata.create.lun_size_bytes = lun->l_size; 685 686 if (lun->l_ctl_lun >= 0) { 687 req.reqdata.create.req_lun_id = lun->l_ctl_lun; 688 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ; 689 } 690 691 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE; 692 req.reqdata.create.device_type = lun->l_device_type; 693 694 if (lun->l_serial != NULL) { 695 strncpy((char *)req.reqdata.create.serial_num, lun->l_serial, 696 sizeof(req.reqdata.create.serial_num)); 697 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM; 698 } 699 700 if (lun->l_device_id != NULL) { 701 strncpy((char *)req.reqdata.create.device_id, lun->l_device_id, 702 sizeof(req.reqdata.create.device_id)); 703 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID; 704 } 705 706 if (lun->l_path != NULL) 707 nvlist_replace_string(lun->l_options, "file", lun->l_path); 708 709 nvlist_replace_string(lun->l_options, "ctld_name", lun->l_name); 710 711 if (!nvlist_exists_string(lun->l_options, "scsiname") && 712 lun->l_scsiname != NULL) 713 nvlist_add_string(lun->l_options, "scsiname", lun->l_scsiname); 714 715 if (!nvlist_empty(lun->l_options)) { 716 req.args = nvlist_pack(lun->l_options, &req.args_len); 717 if (req.args == NULL) { 718 log_warn("error packing nvlist"); 719 return (1); 720 } 721 } 722 723 error = ioctl(ctl_fd, CTL_LUN_REQ, &req); 724 free(req.args); 725 726 if (error != 0) { 727 log_warn("error issuing CTL_LUN_REQ ioctl"); 728 return (1); 729 } 730 731 switch (req.status) { 732 case CTL_LUN_ERROR: 733 log_warnx("LUN creation error: %s", req.error_str); 734 return (1); 735 case CTL_LUN_WARNING: 736 log_warnx("LUN creation warning: %s", req.error_str); 737 break; 738 case CTL_LUN_OK: 739 break; 740 default: 741 log_warnx("unknown LUN creation status: %d", 742 req.status); 743 return (1); 744 } 745 746 lun->l_ctl_lun = req.reqdata.create.req_lun_id; 747 return (0); 748 } 749 750 int 751 kernel_lun_modify(struct lun *lun) 752 { 753 struct ctl_lun_req req; 754 int error; 755 756 bzero(&req, sizeof(req)); 757 758 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 759 req.reqtype = CTL_LUNREQ_MODIFY; 760 761 req.reqdata.modify.lun_id = lun->l_ctl_lun; 762 req.reqdata.modify.lun_size_bytes = lun->l_size; 763 764 if (lun->l_path != NULL) 765 nvlist_replace_string(lun->l_options, "file", lun->l_path); 766 767 nvlist_replace_string(lun->l_options, "ctld_name", lun->l_name); 768 769 if (!nvlist_exists_string(lun->l_options, "scsiname") && 770 lun->l_scsiname != NULL) 771 nvlist_add_string(lun->l_options, "scsiname", lun->l_scsiname); 772 773 if (!nvlist_empty(lun->l_options)) { 774 req.args = nvlist_pack(lun->l_options, &req.args_len); 775 if (req.args == NULL) { 776 log_warn("error packing nvlist"); 777 return (1); 778 } 779 } 780 781 error = ioctl(ctl_fd, CTL_LUN_REQ, &req); 782 free(req.args); 783 784 if (error != 0) { 785 log_warn("error issuing CTL_LUN_REQ ioctl"); 786 return (1); 787 } 788 789 switch (req.status) { 790 case CTL_LUN_ERROR: 791 log_warnx("LUN modification error: %s", req.error_str); 792 return (1); 793 case CTL_LUN_WARNING: 794 log_warnx("LUN modification warning: %s", req.error_str); 795 break; 796 case CTL_LUN_OK: 797 break; 798 default: 799 log_warnx("unknown LUN modification status: %d", 800 req.status); 801 return (1); 802 } 803 804 return (0); 805 } 806 807 int 808 kernel_lun_remove(struct lun *lun) 809 { 810 struct ctl_lun_req req; 811 812 bzero(&req, sizeof(req)); 813 814 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 815 req.reqtype = CTL_LUNREQ_RM; 816 817 req.reqdata.rm.lun_id = lun->l_ctl_lun; 818 819 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) { 820 log_warn("error issuing CTL_LUN_REQ ioctl"); 821 return (1); 822 } 823 824 switch (req.status) { 825 case CTL_LUN_ERROR: 826 log_warnx("LUN removal error: %s", req.error_str); 827 return (1); 828 case CTL_LUN_WARNING: 829 log_warnx("LUN removal warning: %s", req.error_str); 830 break; 831 case CTL_LUN_OK: 832 break; 833 default: 834 log_warnx("unknown LUN removal status: %d", req.status); 835 return (1); 836 } 837 838 return (0); 839 } 840 841 void 842 kernel_handoff(struct ctld_connection *conn) 843 { 844 struct ctl_iscsi req; 845 846 bzero(&req, sizeof(req)); 847 848 req.type = CTL_ISCSI_HANDOFF; 849 strlcpy(req.data.handoff.initiator_name, 850 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name)); 851 strlcpy(req.data.handoff.initiator_addr, 852 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr)); 853 if (conn->conn_initiator_alias != NULL) { 854 strlcpy(req.data.handoff.initiator_alias, 855 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias)); 856 } 857 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid, 858 sizeof(req.data.handoff.initiator_isid)); 859 strlcpy(req.data.handoff.target_name, 860 conn->conn_target->t_name, sizeof(req.data.handoff.target_name)); 861 if (conn->conn_portal->p_portal_group->pg_offload != NULL) { 862 strlcpy(req.data.handoff.offload, 863 conn->conn_portal->p_portal_group->pg_offload, 864 sizeof(req.data.handoff.offload)); 865 } 866 #ifdef ICL_KERNEL_PROXY 867 if (proxy_mode) 868 req.data.handoff.connection_id = conn->conn.conn_socket; 869 else 870 req.data.handoff.socket = conn->conn.conn_socket; 871 #else 872 req.data.handoff.socket = conn->conn.conn_socket; 873 #endif 874 req.data.handoff.portal_group_tag = 875 conn->conn_portal->p_portal_group->pg_tag; 876 if (conn->conn.conn_header_digest == CONN_DIGEST_CRC32C) 877 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C; 878 if (conn->conn.conn_data_digest == CONN_DIGEST_CRC32C) 879 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C; 880 req.data.handoff.cmdsn = conn->conn.conn_cmdsn; 881 req.data.handoff.statsn = conn->conn.conn_statsn; 882 req.data.handoff.max_recv_data_segment_length = 883 conn->conn.conn_max_recv_data_segment_length; 884 req.data.handoff.max_send_data_segment_length = 885 conn->conn.conn_max_send_data_segment_length; 886 req.data.handoff.max_burst_length = conn->conn.conn_max_burst_length; 887 req.data.handoff.first_burst_length = 888 conn->conn.conn_first_burst_length; 889 req.data.handoff.immediate_data = conn->conn.conn_immediate_data; 890 891 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 892 log_err(1, "error issuing CTL_ISCSI ioctl; " 893 "dropping connection"); 894 } 895 896 if (req.status != CTL_ISCSI_OK) { 897 log_errx(1, "error returned from CTL iSCSI handoff request: " 898 "%s; dropping connection", req.error_str); 899 } 900 } 901 902 int 903 kernel_port_add(struct port *port) 904 { 905 struct ctl_port_entry entry; 906 struct ctl_req req; 907 struct ctl_lun_map lm; 908 struct target *targ = port->p_target; 909 struct portal_group *pg = port->p_portal_group; 910 char result_buf[NVLIST_BUFSIZE]; 911 int error, i; 912 913 /* Create iSCSI port. */ 914 if (port->p_portal_group || port->p_ioctl_port) { 915 bzero(&req, sizeof(req)); 916 req.reqtype = CTL_REQ_CREATE; 917 918 if (port->p_portal_group) { 919 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 920 req.args_nvl = nvlist_clone(pg->pg_options); 921 nvlist_add_string(req.args_nvl, "cfiscsi_target", 922 targ->t_name); 923 nvlist_add_string(req.args_nvl, 924 "ctld_portal_group_name", pg->pg_name); 925 nvlist_add_stringf(req.args_nvl, 926 "cfiscsi_portal_group_tag", "%u", pg->pg_tag); 927 928 if (targ->t_alias) { 929 nvlist_add_string(req.args_nvl, 930 "cfiscsi_target_alias", targ->t_alias); 931 } 932 } 933 934 if (port->p_ioctl_port) { 935 strlcpy(req.driver, "ioctl", sizeof(req.driver)); 936 req.args_nvl = nvlist_create(0); 937 nvlist_add_stringf(req.args_nvl, "pp", "%d", 938 port->p_ioctl_pp); 939 nvlist_add_stringf(req.args_nvl, "vp", "%d", 940 port->p_ioctl_vp); 941 } 942 943 req.args = nvlist_pack(req.args_nvl, &req.args_len); 944 if (req.args == NULL) { 945 nvlist_destroy(req.args_nvl); 946 log_warn("error packing nvlist"); 947 return (1); 948 } 949 950 req.result = result_buf; 951 req.result_len = sizeof(result_buf); 952 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 953 free(req.args); 954 nvlist_destroy(req.args_nvl); 955 956 if (error != 0) { 957 log_warn("error issuing CTL_PORT_REQ ioctl"); 958 return (1); 959 } 960 if (req.status == CTL_LUN_ERROR) { 961 log_warnx("error returned from port creation request: %s", 962 req.error_str); 963 return (1); 964 } 965 if (req.status != CTL_LUN_OK) { 966 log_warnx("unknown port creation request status %d", 967 req.status); 968 return (1); 969 } 970 971 req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0); 972 if (req.result_nvl == NULL) { 973 log_warnx("error unpacking result nvlist"); 974 return (1); 975 } 976 977 port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id"); 978 nvlist_destroy(req.result_nvl); 979 } else if (port->p_pport) { 980 port->p_ctl_port = port->p_pport->pp_ctl_port; 981 982 if (strncmp(targ->t_name, "naa.", 4) == 0 && 983 strlen(targ->t_name) == 20) { 984 bzero(&entry, sizeof(entry)); 985 entry.port_type = CTL_PORT_NONE; 986 entry.targ_port = port->p_ctl_port; 987 entry.flags |= CTL_PORT_WWNN_VALID; 988 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16); 989 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1) 990 log_warn("CTL_SET_PORT_WWNS ioctl failed"); 991 } 992 } 993 994 /* Explicitly enable mapping to block any access except allowed. */ 995 lm.port = port->p_ctl_port; 996 lm.plun = UINT32_MAX; 997 lm.lun = 0; 998 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 999 if (error != 0) 1000 log_warn("CTL_LUN_MAP ioctl failed"); 1001 1002 /* Map configured LUNs */ 1003 for (i = 0; i < MAX_LUNS; i++) { 1004 if (targ->t_luns[i] == NULL) 1005 continue; 1006 lm.port = port->p_ctl_port; 1007 lm.plun = i; 1008 lm.lun = targ->t_luns[i]->l_ctl_lun; 1009 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1010 if (error != 0) 1011 log_warn("CTL_LUN_MAP ioctl failed"); 1012 } 1013 1014 /* Enable port */ 1015 bzero(&entry, sizeof(entry)); 1016 entry.targ_port = port->p_ctl_port; 1017 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry); 1018 if (error != 0) { 1019 log_warn("CTL_ENABLE_PORT ioctl failed"); 1020 return (-1); 1021 } 1022 1023 return (0); 1024 } 1025 1026 int 1027 kernel_port_update(struct port *port, struct port *oport) 1028 { 1029 struct ctl_lun_map lm; 1030 struct target *targ = port->p_target; 1031 struct target *otarg = oport->p_target; 1032 int error, i; 1033 uint32_t olun; 1034 1035 /* Map configured LUNs and unmap others */ 1036 for (i = 0; i < MAX_LUNS; i++) { 1037 lm.port = port->p_ctl_port; 1038 lm.plun = i; 1039 if (targ->t_luns[i] == NULL) 1040 lm.lun = UINT32_MAX; 1041 else 1042 lm.lun = targ->t_luns[i]->l_ctl_lun; 1043 if (otarg->t_luns[i] == NULL) 1044 olun = UINT32_MAX; 1045 else 1046 olun = otarg->t_luns[i]->l_ctl_lun; 1047 if (lm.lun == olun) 1048 continue; 1049 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1050 if (error != 0) 1051 log_warn("CTL_LUN_MAP ioctl failed"); 1052 } 1053 return (0); 1054 } 1055 1056 int 1057 kernel_port_remove(struct port *port) 1058 { 1059 struct ctl_port_entry entry; 1060 struct ctl_lun_map lm; 1061 struct ctl_req req; 1062 struct target *targ = port->p_target; 1063 struct portal_group *pg = port->p_portal_group; 1064 int error; 1065 1066 /* Disable port */ 1067 bzero(&entry, sizeof(entry)); 1068 entry.targ_port = port->p_ctl_port; 1069 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry); 1070 if (error != 0) { 1071 log_warn("CTL_DISABLE_PORT ioctl failed"); 1072 return (-1); 1073 } 1074 1075 /* Remove iSCSI or ioctl port. */ 1076 if (port->p_portal_group || port->p_ioctl_port) { 1077 bzero(&req, sizeof(req)); 1078 strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi", 1079 sizeof(req.driver)); 1080 req.reqtype = CTL_REQ_REMOVE; 1081 req.args_nvl = nvlist_create(0); 1082 if (req.args_nvl == NULL) 1083 log_err(1, "nvlist_create"); 1084 1085 if (port->p_ioctl_port) 1086 nvlist_add_stringf(req.args_nvl, "port_id", "%d", 1087 port->p_ctl_port); 1088 else { 1089 nvlist_add_string(req.args_nvl, "cfiscsi_target", 1090 targ->t_name); 1091 nvlist_add_stringf(req.args_nvl, 1092 "cfiscsi_portal_group_tag", "%u", pg->pg_tag); 1093 } 1094 1095 req.args = nvlist_pack(req.args_nvl, &req.args_len); 1096 if (req.args == NULL) { 1097 nvlist_destroy(req.args_nvl); 1098 log_warn("error packing nvlist"); 1099 return (1); 1100 } 1101 1102 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 1103 free(req.args); 1104 nvlist_destroy(req.args_nvl); 1105 1106 if (error != 0) { 1107 log_warn("error issuing CTL_PORT_REQ ioctl"); 1108 return (1); 1109 } 1110 if (req.status == CTL_LUN_ERROR) { 1111 log_warnx("error returned from port removal request: %s", 1112 req.error_str); 1113 return (1); 1114 } 1115 if (req.status != CTL_LUN_OK) { 1116 log_warnx("unknown port removal request status %d", 1117 req.status); 1118 return (1); 1119 } 1120 } else { 1121 /* Disable LUN mapping. */ 1122 lm.port = port->p_ctl_port; 1123 lm.plun = UINT32_MAX; 1124 lm.lun = UINT32_MAX; 1125 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1126 if (error != 0) 1127 log_warn("CTL_LUN_MAP ioctl failed"); 1128 } 1129 return (0); 1130 } 1131 1132 #ifdef ICL_KERNEL_PROXY 1133 void 1134 kernel_listen(struct addrinfo *ai, bool iser, int portal_id) 1135 { 1136 struct ctl_iscsi req; 1137 1138 bzero(&req, sizeof(req)); 1139 1140 req.type = CTL_ISCSI_LISTEN; 1141 req.data.listen.iser = iser; 1142 req.data.listen.domain = ai->ai_family; 1143 req.data.listen.socktype = ai->ai_socktype; 1144 req.data.listen.protocol = ai->ai_protocol; 1145 req.data.listen.addr = ai->ai_addr; 1146 req.data.listen.addrlen = ai->ai_addrlen; 1147 req.data.listen.portal_id = portal_id; 1148 1149 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 1150 log_err(1, "error issuing CTL_ISCSI ioctl"); 1151 1152 if (req.status != CTL_ISCSI_OK) { 1153 log_errx(1, "error returned from CTL iSCSI listen: %s", 1154 req.error_str); 1155 } 1156 } 1157 1158 void 1159 kernel_accept(int *connection_id, int *portal_id, 1160 struct sockaddr *client_sa, socklen_t *client_salen) 1161 { 1162 struct ctl_iscsi req; 1163 struct sockaddr_storage ss; 1164 1165 bzero(&req, sizeof(req)); 1166 1167 req.type = CTL_ISCSI_ACCEPT; 1168 req.data.accept.initiator_addr = (struct sockaddr *)&ss; 1169 1170 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 1171 log_err(1, "error issuing CTL_ISCSI ioctl"); 1172 1173 if (req.status != CTL_ISCSI_OK) { 1174 log_errx(1, "error returned from CTL iSCSI accept: %s", 1175 req.error_str); 1176 } 1177 1178 *connection_id = req.data.accept.connection_id; 1179 *portal_id = req.data.accept.portal_id; 1180 *client_salen = req.data.accept.initiator_addrlen; 1181 memcpy(client_sa, &ss, *client_salen); 1182 } 1183 1184 void 1185 kernel_send(struct pdu *pdu) 1186 { 1187 struct ctl_iscsi req; 1188 1189 bzero(&req, sizeof(req)); 1190 1191 req.type = CTL_ISCSI_SEND; 1192 req.data.send.connection_id = pdu->pdu_connection->conn_socket; 1193 req.data.send.bhs = pdu->pdu_bhs; 1194 req.data.send.data_segment_len = pdu->pdu_data_len; 1195 req.data.send.data_segment = pdu->pdu_data; 1196 1197 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1198 log_err(1, "error issuing CTL_ISCSI ioctl; " 1199 "dropping connection"); 1200 } 1201 1202 if (req.status != CTL_ISCSI_OK) { 1203 log_errx(1, "error returned from CTL iSCSI send: " 1204 "%s; dropping connection", req.error_str); 1205 } 1206 } 1207 1208 void 1209 kernel_receive(struct pdu *pdu) 1210 { 1211 struct connection *conn; 1212 struct ctl_iscsi req; 1213 1214 conn = pdu->pdu_connection; 1215 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length); 1216 if (pdu->pdu_data == NULL) 1217 log_err(1, "malloc"); 1218 1219 bzero(&req, sizeof(req)); 1220 1221 req.type = CTL_ISCSI_RECEIVE; 1222 req.data.receive.connection_id = conn->conn_socket; 1223 req.data.receive.bhs = pdu->pdu_bhs; 1224 req.data.receive.data_segment_len = 1225 conn->conn_max_recv_data_segment_length; 1226 req.data.receive.data_segment = pdu->pdu_data; 1227 1228 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1229 log_err(1, "error issuing CTL_ISCSI ioctl; " 1230 "dropping connection"); 1231 } 1232 1233 if (req.status != CTL_ISCSI_OK) { 1234 log_errx(1, "error returned from CTL iSCSI receive: " 1235 "%s; dropping connection", req.error_str); 1236 } 1237 1238 } 1239 1240 #endif /* ICL_KERNEL_PROXY */ 1241 1242 /* 1243 * XXX: I CANT INTO LATIN 1244 */ 1245 void 1246 kernel_capsicate(void) 1247 { 1248 cap_rights_t rights; 1249 const unsigned long cmds[] = { CTL_ISCSI }; 1250 1251 cap_rights_init(&rights, CAP_IOCTL); 1252 if (caph_rights_limit(ctl_fd, &rights) < 0) 1253 log_err(1, "cap_rights_limit"); 1254 1255 if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0) 1256 log_err(1, "cap_ioctls_limit"); 1257 1258 if (caph_enter() < 0) 1259 log_err(1, "cap_enter"); 1260 1261 if (cap_sandboxed()) 1262 log_debugx("Capsicum capability mode enabled"); 1263 else 1264 log_warnx("Capsicum capability mode not supported"); 1265 } 1266 1267