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