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