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