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: stable/11/usr.sbin/ctld/kernel.c 330449 2018-03-05 07:26:05Z eadler $"); 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 76extern bool proxy_mode; 77 78static int ctl_fd = 0; 79 80void 81kernel_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 */ 109struct cctl_lun_nv { 110 char *name; 111 char *value; 112 STAILQ_ENTRY(cctl_lun_nv) links; 113}; 114 115/* 116 * Backend LUN information. 117 */ 118struct 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 131struct 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 145struct 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 156static void 157cctl_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 202static void 203cctl_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 272static void 273cctl_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 318static void 319cctl_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 390static void 391cctl_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 400struct conf * 401conf_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; 426retry: 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; 475retry_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 655static void 656str_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 666int 667kernel_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 774int 775kernel_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 835int 836kernel_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 869void 870kernel_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_data_segment_length; 912 req.data.handoff.max_burst_length = conn->conn_max_burst_length; 913 req.data.handoff.first_burst_length = conn->conn_first_burst_length; 914 req.data.handoff.immediate_data = conn->conn_immediate_data; 915 916 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 917 log_err(1, "error issuing CTL_ISCSI ioctl; " 918 "dropping connection"); 919 } 920 921 if (req.status != CTL_ISCSI_OK) { 922 log_errx(1, "error returned from CTL iSCSI handoff request: " 923 "%s; dropping connection", req.error_str); 924 } 925} 926 927void 928kernel_limits(const char *offload, size_t *max_data_segment_length) 929{ 930 struct ctl_iscsi req; 931 932 bzero(&req, sizeof(req)); 933 934 req.type = CTL_ISCSI_LIMITS; 935 if (offload != NULL) { 936 strlcpy(req.data.limits.offload, offload, 937 sizeof(req.data.limits.offload)); 938 } 939 940 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 941 log_err(1, "error issuing CTL_ISCSI ioctl; " 942 "dropping connection"); 943 } 944 945 if (req.status != CTL_ISCSI_OK) { 946 log_errx(1, "error returned from CTL iSCSI limits request: " 947 "%s; dropping connection", req.error_str); 948 } 949 950 *max_data_segment_length = req.data.limits.data_segment_limit; 951 if (offload != NULL) { 952 log_debugx("MaxRecvDataSegment kernel limit for offload " 953 "\"%s\" is %zd", offload, *max_data_segment_length); 954 } else { 955 log_debugx("MaxRecvDataSegment kernel limit is %zd", 956 *max_data_segment_length); 957 } 958} 959 960int 961kernel_port_add(struct port *port) 962{ 963 struct option *o; 964 struct ctl_port_entry entry; 965 struct ctl_req req; 966 struct ctl_lun_map lm; 967 struct target *targ = port->p_target; 968 struct portal_group *pg = port->p_portal_group; 969 char tagstr[16]; 970 int error, i, n; 971 972 /* Create iSCSI port. */ 973 if (port->p_portal_group) { 974 bzero(&req, sizeof(req)); 975 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 976 req.reqtype = CTL_REQ_CREATE; 977 req.num_args = 5; 978 TAILQ_FOREACH(o, &pg->pg_options, o_next) 979 req.num_args++; 980 req.args = malloc(req.num_args * sizeof(*req.args)); 981 if (req.args == NULL) 982 log_err(1, "malloc"); 983 n = 0; 984 req.args[n].namelen = sizeof("port_id"); 985 req.args[n].name = __DECONST(char *, "port_id"); 986 req.args[n].vallen = sizeof(port->p_ctl_port); 987 req.args[n].value = &port->p_ctl_port; 988 req.args[n++].flags = CTL_BEARG_WR; 989 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name); 990 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag); 991 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr); 992 if (targ->t_alias) 993 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias); 994 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name); 995 TAILQ_FOREACH(o, &pg->pg_options, o_next) 996 str_arg(&req.args[n++], o->o_name, o->o_value); 997 req.num_args = n; 998 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 999 free(req.args); 1000 if (error != 0) { 1001 log_warn("error issuing CTL_PORT_REQ ioctl"); 1002 return (1); 1003 } 1004 if (req.status == CTL_LUN_ERROR) { 1005 log_warnx("error returned from port creation request: %s", 1006 req.error_str); 1007 return (1); 1008 } 1009 if (req.status != CTL_LUN_OK) { 1010 log_warnx("unknown port creation request status %d", 1011 req.status); 1012 return (1); 1013 } 1014 } else if (port->p_pport) { 1015 port->p_ctl_port = port->p_pport->pp_ctl_port; 1016 1017 if (strncmp(targ->t_name, "naa.", 4) == 0 && 1018 strlen(targ->t_name) == 20) { 1019 bzero(&entry, sizeof(entry)); 1020 entry.port_type = CTL_PORT_NONE; 1021 entry.targ_port = port->p_ctl_port; 1022 entry.flags |= CTL_PORT_WWNN_VALID; 1023 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16); 1024 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1) 1025 log_warn("CTL_SET_PORT_WWNS ioctl failed"); 1026 } 1027 } 1028 1029 /* Explicitly enable mapping to block any access except allowed. */ 1030 lm.port = port->p_ctl_port; 1031 lm.plun = UINT32_MAX; 1032 lm.lun = 0; 1033 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1034 if (error != 0) 1035 log_warn("CTL_LUN_MAP ioctl failed"); 1036 1037 /* Map configured LUNs */ 1038 for (i = 0; i < MAX_LUNS; i++) { 1039 if (targ->t_luns[i] == NULL) 1040 continue; 1041 lm.port = port->p_ctl_port; 1042 lm.plun = i; 1043 lm.lun = targ->t_luns[i]->l_ctl_lun; 1044 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1045 if (error != 0) 1046 log_warn("CTL_LUN_MAP ioctl failed"); 1047 } 1048 1049 /* Enable port */ 1050 bzero(&entry, sizeof(entry)); 1051 entry.targ_port = port->p_ctl_port; 1052 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry); 1053 if (error != 0) { 1054 log_warn("CTL_ENABLE_PORT ioctl failed"); 1055 return (-1); 1056 } 1057 1058 return (0); 1059} 1060 1061int 1062kernel_port_update(struct port *port, struct port *oport) 1063{ 1064 struct ctl_lun_map lm; 1065 struct target *targ = port->p_target; 1066 struct target *otarg = oport->p_target; 1067 int error, i; 1068 uint32_t olun; 1069 1070 /* Map configured LUNs and unmap others */ 1071 for (i = 0; i < MAX_LUNS; i++) { 1072 lm.port = port->p_ctl_port; 1073 lm.plun = i; 1074 if (targ->t_luns[i] == NULL) 1075 lm.lun = UINT32_MAX; 1076 else 1077 lm.lun = targ->t_luns[i]->l_ctl_lun; 1078 if (otarg->t_luns[i] == NULL) 1079 olun = UINT32_MAX; 1080 else 1081 olun = otarg->t_luns[i]->l_ctl_lun; 1082 if (lm.lun == olun) 1083 continue; 1084 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1085 if (error != 0) 1086 log_warn("CTL_LUN_MAP ioctl failed"); 1087 } 1088 return (0); 1089} 1090 1091int 1092kernel_port_remove(struct port *port) 1093{ 1094 struct ctl_port_entry entry; 1095 struct ctl_lun_map lm; 1096 struct ctl_req req; 1097 char tagstr[16]; 1098 struct target *targ = port->p_target; 1099 struct portal_group *pg = port->p_portal_group; 1100 int error; 1101 1102 /* Disable port */ 1103 bzero(&entry, sizeof(entry)); 1104 entry.targ_port = port->p_ctl_port; 1105 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry); 1106 if (error != 0) { 1107 log_warn("CTL_DISABLE_PORT ioctl failed"); 1108 return (-1); 1109 } 1110 1111 /* Remove iSCSI port. */ 1112 if (port->p_portal_group) { 1113 bzero(&req, sizeof(req)); 1114 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 1115 req.reqtype = CTL_REQ_REMOVE; 1116 req.num_args = 2; 1117 req.args = malloc(req.num_args * sizeof(*req.args)); 1118 if (req.args == NULL) 1119 log_err(1, "malloc"); 1120 str_arg(&req.args[0], "cfiscsi_target", targ->t_name); 1121 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag); 1122 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr); 1123 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 1124 free(req.args); 1125 if (error != 0) { 1126 log_warn("error issuing CTL_PORT_REQ ioctl"); 1127 return (1); 1128 } 1129 if (req.status == CTL_LUN_ERROR) { 1130 log_warnx("error returned from port removal request: %s", 1131 req.error_str); 1132 return (1); 1133 } 1134 if (req.status != CTL_LUN_OK) { 1135 log_warnx("unknown port removal request status %d", 1136 req.status); 1137 return (1); 1138 } 1139 } else { 1140 /* Disable LUN mapping. */ 1141 lm.port = port->p_ctl_port; 1142 lm.plun = UINT32_MAX; 1143 lm.lun = UINT32_MAX; 1144 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1145 if (error != 0) 1146 log_warn("CTL_LUN_MAP ioctl failed"); 1147 } 1148 return (0); 1149} 1150 1151#ifdef ICL_KERNEL_PROXY 1152void 1153kernel_listen(struct addrinfo *ai, bool iser, int portal_id) 1154{ 1155 struct ctl_iscsi req; 1156 1157 bzero(&req, sizeof(req)); 1158 1159 req.type = CTL_ISCSI_LISTEN; 1160 req.data.listen.iser = iser; 1161 req.data.listen.domain = ai->ai_family; 1162 req.data.listen.socktype = ai->ai_socktype; 1163 req.data.listen.protocol = ai->ai_protocol; 1164 req.data.listen.addr = ai->ai_addr; 1165 req.data.listen.addrlen = ai->ai_addrlen; 1166 req.data.listen.portal_id = portal_id; 1167 1168 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 1169 log_err(1, "error issuing CTL_ISCSI ioctl"); 1170 1171 if (req.status != CTL_ISCSI_OK) { 1172 log_errx(1, "error returned from CTL iSCSI listen: %s", 1173 req.error_str); 1174 } 1175} 1176 1177void 1178kernel_accept(int *connection_id, int *portal_id, 1179 struct sockaddr *client_sa, socklen_t *client_salen) 1180{ 1181 struct ctl_iscsi req; 1182 struct sockaddr_storage ss; 1183 1184 bzero(&req, sizeof(req)); 1185 1186 req.type = CTL_ISCSI_ACCEPT; 1187 req.data.accept.initiator_addr = (struct sockaddr *)&ss; 1188 1189 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 1190 log_err(1, "error issuing CTL_ISCSI ioctl"); 1191 1192 if (req.status != CTL_ISCSI_OK) { 1193 log_errx(1, "error returned from CTL iSCSI accept: %s", 1194 req.error_str); 1195 } 1196 1197 *connection_id = req.data.accept.connection_id; 1198 *portal_id = req.data.accept.portal_id; 1199 *client_salen = req.data.accept.initiator_addrlen; 1200 memcpy(client_sa, &ss, *client_salen); 1201} 1202 1203void 1204kernel_send(struct pdu *pdu) 1205{ 1206 struct ctl_iscsi req; 1207 1208 bzero(&req, sizeof(req)); 1209 1210 req.type = CTL_ISCSI_SEND; 1211 req.data.send.connection_id = pdu->pdu_connection->conn_socket; 1212 req.data.send.bhs = pdu->pdu_bhs; 1213 req.data.send.data_segment_len = pdu->pdu_data_len; 1214 req.data.send.data_segment = pdu->pdu_data; 1215 1216 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1217 log_err(1, "error issuing CTL_ISCSI ioctl; " 1218 "dropping connection"); 1219 } 1220 1221 if (req.status != CTL_ISCSI_OK) { 1222 log_errx(1, "error returned from CTL iSCSI send: " 1223 "%s; dropping connection", req.error_str); 1224 } 1225} 1226 1227void 1228kernel_receive(struct pdu *pdu) 1229{ 1230 struct ctl_iscsi req; 1231 1232 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH); 1233 if (pdu->pdu_data == NULL) 1234 log_err(1, "malloc"); 1235 1236 bzero(&req, sizeof(req)); 1237 1238 req.type = CTL_ISCSI_RECEIVE; 1239 req.data.receive.connection_id = pdu->pdu_connection->conn_socket; 1240 req.data.receive.bhs = pdu->pdu_bhs; 1241 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH; 1242 req.data.receive.data_segment = pdu->pdu_data; 1243 1244 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1245 log_err(1, "error issuing CTL_ISCSI ioctl; " 1246 "dropping connection"); 1247 } 1248 1249 if (req.status != CTL_ISCSI_OK) { 1250 log_errx(1, "error returned from CTL iSCSI receive: " 1251 "%s; dropping connection", req.error_str); 1252 } 1253 1254} 1255 1256#endif /* ICL_KERNEL_PROXY */ 1257 1258/* 1259 * XXX: I CANT INTO LATIN 1260 */ 1261void 1262kernel_capsicate(void) 1263{ 1264 int error; 1265 cap_rights_t rights; 1266 const unsigned long cmds[] = { CTL_ISCSI }; 1267 1268 cap_rights_init(&rights, CAP_IOCTL); 1269 error = cap_rights_limit(ctl_fd, &rights); 1270 if (error != 0 && errno != ENOSYS) 1271 log_err(1, "cap_rights_limit"); 1272 1273 error = cap_ioctls_limit(ctl_fd, cmds, nitems(cmds)); 1274 1275 if (error != 0 && errno != ENOSYS) 1276 log_err(1, "cap_ioctls_limit"); 1277 1278 error = cap_enter(); 1279 if (error != 0 && errno != ENOSYS) 1280 log_err(1, "cap_enter"); 1281 1282 if (cap_sandboxed()) 1283 log_debugx("Capsicum capability mode enabled"); 1284 else 1285 log_warnx("Capsicum capability mode not supported"); 1286} 1287 1288