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 * $FreeBSD$ 36 */ 37 38 #include <sys/ioctl.h> 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 #include <sys/param.h> 42 #include <sys/linker.h> 43 #include <sys/queue.h> 44 #include <sys/callout.h> 45 #include <sys/sbuf.h> 46 #include <sys/capsicum.h> 47 #include <assert.h> 48 #include <bsdxml.h> 49 #include <ctype.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <strings.h> 57 #include <cam/scsi/scsi_all.h> 58 #include <cam/scsi/scsi_message.h> 59 #include <cam/ctl/ctl.h> 60 #include <cam/ctl/ctl_io.h> 61 #include <cam/ctl/ctl_frontend_internal.h> 62 #include <cam/ctl/ctl_backend.h> 63 #include <cam/ctl/ctl_ioctl.h> 64 #include <cam/ctl/ctl_backend_block.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 74 extern bool proxy_mode; 75 76 static int ctl_fd = 0; 77 78 void 79 kernel_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 */ 99 struct cctl_lun_nv { 100 char *name; 101 char *value; 102 STAILQ_ENTRY(cctl_lun_nv) links; 103 }; 104 105 /* 106 * Backend LUN information. 107 */ 108 struct 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 *cfiscsi_target; 116 int cfiscsi_lun; 117 STAILQ_HEAD(,cctl_lun_nv) attr_list; 118 STAILQ_ENTRY(cctl_lun) links; 119 }; 120 121 struct cctl_port { 122 uint32_t port_id; 123 int cfiscsi_status; 124 char *cfiscsi_target; 125 uint16_t cfiscsi_portal_group_tag; 126 STAILQ_HEAD(,cctl_lun_nv) attr_list; 127 STAILQ_ENTRY(cctl_port) links; 128 }; 129 130 struct cctl_devlist_data { 131 int num_luns; 132 STAILQ_HEAD(,cctl_lun) lun_list; 133 struct cctl_lun *cur_lun; 134 int num_ports; 135 STAILQ_HEAD(,cctl_port) port_list; 136 struct cctl_port *cur_port; 137 int level; 138 struct sbuf *cur_sb[32]; 139 }; 140 141 static void 142 cctl_start_element(void *user_data, const char *name, const char **attr) 143 { 144 int i; 145 struct cctl_devlist_data *devlist; 146 struct cctl_lun *cur_lun; 147 148 devlist = (struct cctl_devlist_data *)user_data; 149 cur_lun = devlist->cur_lun; 150 devlist->level++; 151 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 152 sizeof(devlist->cur_sb[0]))) 153 log_errx(1, "%s: too many nesting levels, %zd max", __func__, 154 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0])); 155 156 devlist->cur_sb[devlist->level] = sbuf_new_auto(); 157 if (devlist->cur_sb[devlist->level] == NULL) 158 log_err(1, "%s: unable to allocate sbuf", __func__); 159 160 if (strcmp(name, "lun") == 0) { 161 if (cur_lun != NULL) 162 log_errx(1, "%s: improper lun element nesting", 163 __func__); 164 165 cur_lun = calloc(1, sizeof(*cur_lun)); 166 if (cur_lun == NULL) 167 log_err(1, "%s: cannot allocate %zd bytes", __func__, 168 sizeof(*cur_lun)); 169 170 devlist->num_luns++; 171 devlist->cur_lun = cur_lun; 172 173 STAILQ_INIT(&cur_lun->attr_list); 174 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links); 175 176 for (i = 0; attr[i] != NULL; i += 2) { 177 if (strcmp(attr[i], "id") == 0) { 178 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0); 179 } else { 180 log_errx(1, "%s: invalid LUN attribute %s = %s", 181 __func__, attr[i], attr[i+1]); 182 } 183 } 184 } 185 } 186 187 static void 188 cctl_end_element(void *user_data, const char *name) 189 { 190 struct cctl_devlist_data *devlist; 191 struct cctl_lun *cur_lun; 192 char *str; 193 194 devlist = (struct cctl_devlist_data *)user_data; 195 cur_lun = devlist->cur_lun; 196 197 if ((cur_lun == NULL) 198 && (strcmp(name, "ctllunlist") != 0)) 199 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name); 200 201 if (devlist->cur_sb[devlist->level] == NULL) 202 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 203 devlist->level, name); 204 205 sbuf_finish(devlist->cur_sb[devlist->level]); 206 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 207 208 if (strlen(str) == 0) { 209 free(str); 210 str = NULL; 211 } 212 213 sbuf_delete(devlist->cur_sb[devlist->level]); 214 devlist->cur_sb[devlist->level] = NULL; 215 devlist->level--; 216 217 if (strcmp(name, "backend_type") == 0) { 218 cur_lun->backend_type = str; 219 str = NULL; 220 } else if (strcmp(name, "size") == 0) { 221 cur_lun->size_blocks = strtoull(str, NULL, 0); 222 } else if (strcmp(name, "blocksize") == 0) { 223 cur_lun->blocksize = strtoul(str, NULL, 0); 224 } else if (strcmp(name, "serial_number") == 0) { 225 cur_lun->serial_number = str; 226 str = NULL; 227 } else if (strcmp(name, "device_id") == 0) { 228 cur_lun->device_id = str; 229 str = NULL; 230 } else if (strcmp(name, "cfiscsi_target") == 0) { 231 cur_lun->cfiscsi_target = str; 232 str = NULL; 233 } else if (strcmp(name, "cfiscsi_lun") == 0) { 234 cur_lun->cfiscsi_lun = strtoul(str, NULL, 0); 235 } else if (strcmp(name, "lun") == 0) { 236 devlist->cur_lun = NULL; 237 } else if (strcmp(name, "ctllunlist") == 0) { 238 239 } else { 240 struct cctl_lun_nv *nv; 241 242 nv = calloc(1, sizeof(*nv)); 243 if (nv == NULL) 244 log_err(1, "%s: can't allocate %zd bytes for nv pair", 245 __func__, sizeof(*nv)); 246 247 nv->name = checked_strdup(name); 248 249 nv->value = str; 250 str = NULL; 251 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links); 252 } 253 254 free(str); 255 } 256 257 static void 258 cctl_start_pelement(void *user_data, const char *name, const char **attr) 259 { 260 int i; 261 struct cctl_devlist_data *devlist; 262 struct cctl_port *cur_port; 263 264 devlist = (struct cctl_devlist_data *)user_data; 265 cur_port = devlist->cur_port; 266 devlist->level++; 267 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 268 sizeof(devlist->cur_sb[0]))) 269 log_errx(1, "%s: too many nesting levels, %zd max", __func__, 270 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0])); 271 272 devlist->cur_sb[devlist->level] = sbuf_new_auto(); 273 if (devlist->cur_sb[devlist->level] == NULL) 274 log_err(1, "%s: unable to allocate sbuf", __func__); 275 276 if (strcmp(name, "targ_port") == 0) { 277 if (cur_port != NULL) 278 log_errx(1, "%s: improper port element nesting (%s)", 279 __func__, name); 280 281 cur_port = calloc(1, sizeof(*cur_port)); 282 if (cur_port == NULL) 283 log_err(1, "%s: cannot allocate %zd bytes", __func__, 284 sizeof(*cur_port)); 285 286 devlist->num_ports++; 287 devlist->cur_port = cur_port; 288 289 STAILQ_INIT(&cur_port->attr_list); 290 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links); 291 292 for (i = 0; attr[i] != NULL; i += 2) { 293 if (strcmp(attr[i], "id") == 0) { 294 cur_port->port_id = strtoul(attr[i+1], NULL, 0); 295 } else { 296 log_errx(1, "%s: invalid LUN attribute %s = %s", 297 __func__, attr[i], attr[i+1]); 298 } 299 } 300 } 301 } 302 303 static void 304 cctl_end_pelement(void *user_data, const char *name) 305 { 306 struct cctl_devlist_data *devlist; 307 struct cctl_port *cur_port; 308 char *str; 309 310 devlist = (struct cctl_devlist_data *)user_data; 311 cur_port = devlist->cur_port; 312 313 if ((cur_port == NULL) 314 && (strcmp(name, "ctlportlist") != 0)) 315 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name); 316 317 if (devlist->cur_sb[devlist->level] == NULL) 318 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 319 devlist->level, name); 320 321 sbuf_finish(devlist->cur_sb[devlist->level]); 322 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 323 324 if (strlen(str) == 0) { 325 free(str); 326 str = NULL; 327 } 328 329 sbuf_delete(devlist->cur_sb[devlist->level]); 330 devlist->cur_sb[devlist->level] = NULL; 331 devlist->level--; 332 333 if (strcmp(name, "cfiscsi_target") == 0) { 334 cur_port->cfiscsi_target = str; 335 str = NULL; 336 } else if (strcmp(name, "cfiscsi_status") == 0) { 337 cur_port->cfiscsi_status = strtoul(str, NULL, 0); 338 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) { 339 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0); 340 } else if (strcmp(name, "targ_port") == 0) { 341 devlist->cur_port = NULL; 342 } else if (strcmp(name, "ctlportlist") == 0) { 343 344 } else { 345 struct cctl_lun_nv *nv; 346 347 nv = calloc(1, sizeof(*nv)); 348 if (nv == NULL) 349 log_err(1, "%s: can't allocate %zd bytes for nv pair", 350 __func__, sizeof(*nv)); 351 352 nv->name = checked_strdup(name); 353 354 nv->value = str; 355 str = NULL; 356 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links); 357 } 358 359 free(str); 360 } 361 362 static void 363 cctl_char_handler(void *user_data, const XML_Char *str, int len) 364 { 365 struct cctl_devlist_data *devlist; 366 367 devlist = (struct cctl_devlist_data *)user_data; 368 369 sbuf_bcat(devlist->cur_sb[devlist->level], str, len); 370 } 371 372 struct conf * 373 conf_new_from_kernel(void) 374 { 375 struct conf *conf = NULL; 376 struct target *targ; 377 struct lun *cl; 378 struct lun_option *lo; 379 struct ctl_lun_list list; 380 struct cctl_devlist_data devlist; 381 struct cctl_lun *lun; 382 struct cctl_port *port; 383 XML_Parser parser; 384 char *str; 385 int len, retval; 386 387 bzero(&devlist, sizeof(devlist)); 388 STAILQ_INIT(&devlist.lun_list); 389 STAILQ_INIT(&devlist.port_list); 390 391 log_debugx("obtaining previously configured CTL luns from the kernel"); 392 393 str = NULL; 394 len = 4096; 395 retry: 396 str = realloc(str, len); 397 if (str == NULL) 398 log_err(1, "realloc"); 399 400 bzero(&list, sizeof(list)); 401 list.alloc_len = len; 402 list.status = CTL_LUN_LIST_NONE; 403 list.lun_xml = str; 404 405 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) { 406 log_warn("error issuing CTL_LUN_LIST ioctl"); 407 free(str); 408 return (NULL); 409 } 410 411 if (list.status == CTL_LUN_LIST_ERROR) { 412 log_warnx("error returned from CTL_LUN_LIST ioctl: %s", 413 list.error_str); 414 free(str); 415 return (NULL); 416 } 417 418 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 419 len = len << 1; 420 goto retry; 421 } 422 423 parser = XML_ParserCreate(NULL); 424 if (parser == NULL) { 425 log_warnx("unable to create XML parser"); 426 free(str); 427 return (NULL); 428 } 429 430 XML_SetUserData(parser, &devlist); 431 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element); 432 XML_SetCharacterDataHandler(parser, cctl_char_handler); 433 434 retval = XML_Parse(parser, str, strlen(str), 1); 435 XML_ParserFree(parser); 436 free(str); 437 if (retval != 1) { 438 log_warnx("XML_Parse failed"); 439 return (NULL); 440 } 441 442 str = NULL; 443 len = 4096; 444 retry_port: 445 str = realloc(str, len); 446 if (str == NULL) 447 log_err(1, "realloc"); 448 449 bzero(&list, sizeof(list)); 450 list.alloc_len = len; 451 list.status = CTL_LUN_LIST_NONE; 452 list.lun_xml = str; 453 454 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) { 455 log_warn("error issuing CTL_PORT_LIST ioctl"); 456 free(str); 457 return (NULL); 458 } 459 460 if (list.status == CTL_PORT_LIST_ERROR) { 461 log_warnx("error returned from CTL_PORT_LIST ioctl: %s", 462 list.error_str); 463 free(str); 464 return (NULL); 465 } 466 467 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 468 len = len << 1; 469 goto retry_port; 470 } 471 472 parser = XML_ParserCreate(NULL); 473 if (parser == NULL) { 474 log_warnx("unable to create XML parser"); 475 free(str); 476 return (NULL); 477 } 478 479 XML_SetUserData(parser, &devlist); 480 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement); 481 XML_SetCharacterDataHandler(parser, cctl_char_handler); 482 483 retval = XML_Parse(parser, str, strlen(str), 1); 484 XML_ParserFree(parser); 485 free(str); 486 if (retval != 1) { 487 log_warnx("XML_Parse failed"); 488 return (NULL); 489 } 490 491 conf = conf_new(); 492 493 STAILQ_FOREACH(port, &devlist.port_list, links) { 494 495 if (port->cfiscsi_target == NULL) { 496 log_debugx("CTL port %ju wasn't managed by ctld; " 497 "ignoring", (uintmax_t)port->port_id); 498 continue; 499 } 500 if (port->cfiscsi_status != 1) { 501 log_debugx("CTL port %ju is not active (%d); ignoring", 502 (uintmax_t)port->port_id, port->cfiscsi_status); 503 continue; 504 } 505 506 targ = target_find(conf, port->cfiscsi_target); 507 if (targ == NULL) { 508 #if 0 509 log_debugx("found new kernel target %s for CTL port %ld", 510 port->cfiscsi_target, port->port_id); 511 #endif 512 targ = target_new(conf, port->cfiscsi_target); 513 if (targ == NULL) { 514 log_warnx("target_new failed"); 515 continue; 516 } 517 } 518 } 519 520 STAILQ_FOREACH(lun, &devlist.lun_list, links) { 521 struct cctl_lun_nv *nv; 522 523 if (lun->cfiscsi_target == NULL) { 524 log_debugx("CTL lun %ju wasn't managed by ctld; " 525 "ignoring", (uintmax_t)lun->lun_id); 526 continue; 527 } 528 529 targ = target_find(conf, lun->cfiscsi_target); 530 if (targ == NULL) { 531 #if 0 532 log_debugx("found new kernel target %s for CTL lun %ld", 533 lun->cfiscsi_target, lun->lun_id); 534 #endif 535 targ = target_new(conf, lun->cfiscsi_target); 536 if (targ == NULL) { 537 log_warnx("target_new failed"); 538 continue; 539 } 540 } 541 542 cl = lun_find(targ, lun->cfiscsi_lun); 543 if (cl != NULL) { 544 log_warnx("found CTL lun %ju, backing lun %d, target " 545 "%s, also backed by CTL lun %d; ignoring", 546 (uintmax_t) lun->lun_id, cl->l_lun, 547 cl->l_target->t_name, cl->l_ctl_lun); 548 continue; 549 } 550 551 log_debugx("found CTL lun %ju, backing lun %d, target %s", 552 (uintmax_t)lun->lun_id, lun->cfiscsi_lun, lun->cfiscsi_target); 553 554 cl = lun_new(targ, lun->cfiscsi_lun); 555 if (cl == NULL) { 556 log_warnx("lun_new failed"); 557 continue; 558 } 559 lun_set_backend(cl, lun->backend_type); 560 lun_set_blocksize(cl, lun->blocksize); 561 lun_set_device_id(cl, lun->device_id); 562 lun_set_serial(cl, lun->serial_number); 563 lun_set_size(cl, lun->size_blocks * cl->l_blocksize); 564 lun_set_ctl_lun(cl, lun->lun_id); 565 566 STAILQ_FOREACH(nv, &lun->attr_list, links) { 567 if (strcmp(nv->name, "file") == 0 || 568 strcmp(nv->name, "dev") == 0) { 569 lun_set_path(cl, nv->value); 570 continue; 571 } 572 lo = lun_option_new(cl, nv->name, nv->value); 573 if (lo == NULL) 574 log_warnx("unable to add CTL lun option %s " 575 "for CTL lun %ju for lun %d, target %s", 576 nv->name, (uintmax_t) lun->lun_id, 577 cl->l_lun, cl->l_target->t_name); 578 } 579 } 580 581 return (conf); 582 } 583 584 static void 585 str_arg(struct ctl_be_arg *arg, const char *name, const char *value) 586 { 587 588 arg->namelen = strlen(name) + 1; 589 arg->name = __DECONST(char *, name); 590 arg->vallen = strlen(value) + 1; 591 arg->value = __DECONST(char *, value); 592 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD; 593 } 594 595 int 596 kernel_lun_add(struct lun *lun) 597 { 598 struct lun_option *lo; 599 struct ctl_lun_req req; 600 char *tmp; 601 int error, i, num_options; 602 603 bzero(&req, sizeof(req)); 604 605 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 606 req.reqtype = CTL_LUNREQ_CREATE; 607 608 req.reqdata.create.blocksize_bytes = lun->l_blocksize; 609 610 if (lun->l_size != 0) 611 req.reqdata.create.lun_size_bytes = lun->l_size; 612 613 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE; 614 req.reqdata.create.device_type = T_DIRECT; 615 616 if (lun->l_serial != NULL) { 617 strncpy(req.reqdata.create.serial_num, lun->l_serial, 618 sizeof(req.reqdata.create.serial_num)); 619 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM; 620 } 621 622 if (lun->l_device_id != NULL) { 623 strncpy(req.reqdata.create.device_id, lun->l_device_id, 624 sizeof(req.reqdata.create.device_id)); 625 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID; 626 } 627 628 if (lun->l_path != NULL) { 629 lo = lun_option_find(lun, "file"); 630 if (lo != NULL) { 631 lun_option_set(lo, lun->l_path); 632 } else { 633 lo = lun_option_new(lun, "file", lun->l_path); 634 assert(lo != NULL); 635 } 636 } 637 638 lo = lun_option_find(lun, "cfiscsi_target"); 639 if (lo != NULL) { 640 lun_option_set(lo, lun->l_target->t_name); 641 } else { 642 lo = lun_option_new(lun, "cfiscsi_target", 643 lun->l_target->t_name); 644 assert(lo != NULL); 645 } 646 647 asprintf(&tmp, "%d", lun->l_lun); 648 if (tmp == NULL) 649 log_errx(1, "asprintf"); 650 lo = lun_option_find(lun, "cfiscsi_lun"); 651 if (lo != NULL) { 652 lun_option_set(lo, tmp); 653 free(tmp); 654 } else { 655 lo = lun_option_new(lun, "cfiscsi_lun", tmp); 656 free(tmp); 657 assert(lo != NULL); 658 } 659 660 asprintf(&tmp, "%s,lun,%d", lun->l_target->t_name, lun->l_lun); 661 if (tmp == NULL) 662 log_errx(1, "asprintf"); 663 lo = lun_option_find(lun, "scsiname"); 664 if (lo != NULL) { 665 lun_option_set(lo, tmp); 666 free(tmp); 667 } else { 668 lo = lun_option_new(lun, "scsiname", tmp); 669 free(tmp); 670 assert(lo != NULL); 671 } 672 673 num_options = 0; 674 TAILQ_FOREACH(lo, &lun->l_options, lo_next) 675 num_options++; 676 677 req.num_be_args = num_options; 678 if (num_options > 0) { 679 req.be_args = malloc(num_options * sizeof(*req.be_args)); 680 if (req.be_args == NULL) { 681 log_warn("error allocating %zd bytes", 682 num_options * sizeof(*req.be_args)); 683 return (1); 684 } 685 686 i = 0; 687 TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 688 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value); 689 i++; 690 } 691 assert(i == num_options); 692 } 693 694 error = ioctl(ctl_fd, CTL_LUN_REQ, &req); 695 free(req.be_args); 696 if (error != 0) { 697 log_warn("error issuing CTL_LUN_REQ ioctl"); 698 return (1); 699 } 700 701 if (req.status == CTL_LUN_ERROR) { 702 log_warnx("error returned from LUN creation request: %s", 703 req.error_str); 704 return (1); 705 } 706 707 if (req.status != CTL_LUN_OK) { 708 log_warnx("unknown LUN creation request status %d", 709 req.status); 710 return (1); 711 } 712 713 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id); 714 715 return (0); 716 } 717 718 int 719 kernel_lun_resize(struct lun *lun) 720 { 721 struct ctl_lun_req req; 722 723 bzero(&req, sizeof(req)); 724 725 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 726 req.reqtype = CTL_LUNREQ_MODIFY; 727 728 req.reqdata.modify.lun_id = lun->l_ctl_lun; 729 req.reqdata.modify.lun_size_bytes = lun->l_size; 730 731 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) { 732 log_warn("error issuing CTL_LUN_REQ ioctl"); 733 return (1); 734 } 735 736 if (req.status == CTL_LUN_ERROR) { 737 log_warnx("error returned from LUN modification request: %s", 738 req.error_str); 739 return (1); 740 } 741 742 if (req.status != CTL_LUN_OK) { 743 log_warnx("unknown LUN modification request status %d", 744 req.status); 745 return (1); 746 } 747 748 return (0); 749 } 750 751 int 752 kernel_lun_remove(struct lun *lun) 753 { 754 struct ctl_lun_req req; 755 756 bzero(&req, sizeof(req)); 757 758 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 759 req.reqtype = CTL_LUNREQ_RM; 760 761 req.reqdata.rm.lun_id = lun->l_ctl_lun; 762 763 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) { 764 log_warn("error issuing CTL_LUN_REQ ioctl"); 765 return (1); 766 } 767 768 if (req.status == CTL_LUN_ERROR) { 769 log_warnx("error returned from LUN removal request: %s", 770 req.error_str); 771 return (1); 772 } 773 774 if (req.status != CTL_LUN_OK) { 775 log_warnx("unknown LUN removal request status %d", req.status); 776 return (1); 777 } 778 779 return (0); 780 } 781 782 void 783 kernel_handoff(struct connection *conn) 784 { 785 struct ctl_iscsi req; 786 787 bzero(&req, sizeof(req)); 788 789 req.type = CTL_ISCSI_HANDOFF; 790 strlcpy(req.data.handoff.initiator_name, 791 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name)); 792 strlcpy(req.data.handoff.initiator_addr, 793 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr)); 794 if (conn->conn_initiator_alias != NULL) { 795 strlcpy(req.data.handoff.initiator_alias, 796 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias)); 797 } 798 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid, 799 sizeof(req.data.handoff.initiator_isid)); 800 strlcpy(req.data.handoff.target_name, 801 conn->conn_target->t_name, sizeof(req.data.handoff.target_name)); 802 #ifdef ICL_KERNEL_PROXY 803 if (proxy_mode) 804 req.data.handoff.connection_id = conn->conn_socket; 805 else 806 req.data.handoff.socket = conn->conn_socket; 807 #else 808 req.data.handoff.socket = conn->conn_socket; 809 #endif 810 req.data.handoff.portal_group_tag = 811 conn->conn_portal->p_portal_group->pg_tag; 812 if (conn->conn_header_digest == CONN_DIGEST_CRC32C) 813 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C; 814 if (conn->conn_data_digest == CONN_DIGEST_CRC32C) 815 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C; 816 req.data.handoff.cmdsn = conn->conn_cmdsn; 817 req.data.handoff.statsn = conn->conn_statsn; 818 req.data.handoff.max_recv_data_segment_length = 819 conn->conn_max_data_segment_length; 820 req.data.handoff.max_burst_length = conn->conn_max_burst_length; 821 req.data.handoff.immediate_data = conn->conn_immediate_data; 822 823 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 824 log_err(1, "error issuing CTL_ISCSI ioctl; " 825 "dropping connection"); 826 } 827 828 if (req.status != CTL_ISCSI_OK) { 829 log_errx(1, "error returned from CTL iSCSI handoff request: " 830 "%s; dropping connection", req.error_str); 831 } 832 } 833 834 int 835 kernel_port_add(struct target *targ) 836 { 837 struct ctl_port_entry entry; 838 struct ctl_req req; 839 char tagstr[16]; 840 int error; 841 uint32_t port_id = -1; 842 843 bzero(&req, sizeof(req)); 844 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 845 req.reqtype = CTL_REQ_CREATE; 846 req.num_args = 4; 847 req.args = malloc(req.num_args * sizeof(*req.args)); 848 req.args[0].namelen = sizeof("port_id"); 849 req.args[0].name = __DECONST(char *, "port_id"); 850 req.args[0].vallen = sizeof(port_id); 851 req.args[0].value = &port_id; 852 req.args[0].flags = CTL_BEARG_WR; 853 str_arg(&req.args[1], "cfiscsi_target", targ->t_name); 854 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag); 855 str_arg(&req.args[2], "cfiscsi_portal_group_tag", tagstr); 856 if (targ->t_alias) 857 str_arg(&req.args[3], "cfiscsi_target_alias", targ->t_alias); 858 else 859 req.num_args--; 860 861 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 862 free(req.args); 863 if (error != 0) { 864 log_warn("error issuing CTL_PORT_REQ ioctl"); 865 return (1); 866 } 867 868 if (req.status == CTL_LUN_ERROR) { 869 log_warnx("error returned from port creation request: %s", 870 req.error_str); 871 return (1); 872 } 873 874 if (req.status != CTL_LUN_OK) { 875 log_warnx("unknown port creation request status %d", 876 req.status); 877 return (1); 878 } 879 880 bzero(&entry, sizeof(entry)); 881 entry.targ_port = port_id; 882 883 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry); 884 if (error != 0) { 885 log_warn("CTL_ENABLE_PORT ioctl failed"); 886 return (-1); 887 } 888 889 return (0); 890 } 891 892 int 893 kernel_port_remove(struct target *targ) 894 { 895 struct ctl_req req; 896 char tagstr[16]; 897 int error; 898 899 bzero(&req, sizeof(req)); 900 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 901 req.reqtype = CTL_REQ_REMOVE; 902 req.num_args = 2; 903 req.args = malloc(req.num_args * sizeof(*req.args)); 904 str_arg(&req.args[0], "cfiscsi_target", targ->t_name); 905 if (targ->t_portal_group) { 906 snprintf(tagstr, sizeof(tagstr), "%d", 907 targ->t_portal_group->pg_tag); 908 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr); 909 } else 910 req.num_args--; 911 912 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 913 free(req.args); 914 if (error != 0) { 915 log_warn("error issuing CTL_PORT_REQ ioctl"); 916 return (1); 917 } 918 919 if (req.status == CTL_LUN_ERROR) { 920 log_warnx("error returned from port removal request: %s", 921 req.error_str); 922 return (1); 923 } 924 925 if (req.status != CTL_LUN_OK) { 926 log_warnx("unknown port removal request status %d", 927 req.status); 928 return (1); 929 } 930 931 return (0); 932 } 933 934 #ifdef ICL_KERNEL_PROXY 935 void 936 kernel_listen(struct addrinfo *ai, bool iser, int portal_id) 937 { 938 struct ctl_iscsi req; 939 940 bzero(&req, sizeof(req)); 941 942 req.type = CTL_ISCSI_LISTEN; 943 req.data.listen.iser = iser; 944 req.data.listen.domain = ai->ai_family; 945 req.data.listen.socktype = ai->ai_socktype; 946 req.data.listen.protocol = ai->ai_protocol; 947 req.data.listen.addr = ai->ai_addr; 948 req.data.listen.addrlen = ai->ai_addrlen; 949 req.data.listen.portal_id = portal_id; 950 951 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 952 log_err(1, "error issuing CTL_ISCSI ioctl"); 953 954 if (req.status != CTL_ISCSI_OK) { 955 log_errx(1, "error returned from CTL iSCSI listen: %s", 956 req.error_str); 957 } 958 } 959 960 void 961 kernel_accept(int *connection_id, int *portal_id, 962 struct sockaddr *client_sa, socklen_t *client_salen) 963 { 964 struct ctl_iscsi req; 965 struct sockaddr_storage ss; 966 967 bzero(&req, sizeof(req)); 968 969 req.type = CTL_ISCSI_ACCEPT; 970 req.data.accept.initiator_addr = (struct sockaddr *)&ss; 971 972 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 973 log_err(1, "error issuing CTL_ISCSI ioctl"); 974 975 if (req.status != CTL_ISCSI_OK) { 976 log_errx(1, "error returned from CTL iSCSI accept: %s", 977 req.error_str); 978 } 979 980 *connection_id = req.data.accept.connection_id; 981 *portal_id = req.data.accept.portal_id; 982 *client_salen = req.data.accept.initiator_addrlen; 983 memcpy(client_sa, &ss, *client_salen); 984 } 985 986 void 987 kernel_send(struct pdu *pdu) 988 { 989 struct ctl_iscsi req; 990 991 bzero(&req, sizeof(req)); 992 993 req.type = CTL_ISCSI_SEND; 994 req.data.send.connection_id = pdu->pdu_connection->conn_socket; 995 req.data.send.bhs = pdu->pdu_bhs; 996 req.data.send.data_segment_len = pdu->pdu_data_len; 997 req.data.send.data_segment = pdu->pdu_data; 998 999 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1000 log_err(1, "error issuing CTL_ISCSI ioctl; " 1001 "dropping connection"); 1002 } 1003 1004 if (req.status != CTL_ISCSI_OK) { 1005 log_errx(1, "error returned from CTL iSCSI send: " 1006 "%s; dropping connection", req.error_str); 1007 } 1008 } 1009 1010 void 1011 kernel_receive(struct pdu *pdu) 1012 { 1013 struct ctl_iscsi req; 1014 1015 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH); 1016 if (pdu->pdu_data == NULL) 1017 log_err(1, "malloc"); 1018 1019 bzero(&req, sizeof(req)); 1020 1021 req.type = CTL_ISCSI_RECEIVE; 1022 req.data.receive.connection_id = pdu->pdu_connection->conn_socket; 1023 req.data.receive.bhs = pdu->pdu_bhs; 1024 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH; 1025 req.data.receive.data_segment = pdu->pdu_data; 1026 1027 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1028 log_err(1, "error issuing CTL_ISCSI ioctl; " 1029 "dropping connection"); 1030 } 1031 1032 if (req.status != CTL_ISCSI_OK) { 1033 log_errx(1, "error returned from CTL iSCSI receive: " 1034 "%s; dropping connection", req.error_str); 1035 } 1036 1037 } 1038 1039 #endif /* ICL_KERNEL_PROXY */ 1040 1041 /* 1042 * XXX: I CANT INTO LATIN 1043 */ 1044 void 1045 kernel_capsicate(void) 1046 { 1047 int error; 1048 cap_rights_t rights; 1049 const unsigned long cmds[] = { CTL_ISCSI }; 1050 1051 cap_rights_init(&rights, CAP_IOCTL); 1052 error = cap_rights_limit(ctl_fd, &rights); 1053 if (error != 0 && errno != ENOSYS) 1054 log_err(1, "cap_rights_limit"); 1055 1056 error = cap_ioctls_limit(ctl_fd, cmds, 1057 sizeof(cmds) / sizeof(cmds[0])); 1058 if (error != 0 && errno != ENOSYS) 1059 log_err(1, "cap_ioctls_limit"); 1060 1061 error = cap_enter(); 1062 if (error != 0 && errno != ENOSYS) 1063 log_err(1, "cap_enter"); 1064 1065 if (cap_sandboxed()) 1066 log_debugx("Capsicum capability mode enabled"); 1067 else 1068 log_warnx("Capsicum capability mode not supported"); 1069 } 1070 1071