kernel.c revision 268302
188846Sache/*- 229967Sache * Copyright (c) 2003, 2004 Silicon Graphics International Corp. 329967Sache * Copyright (c) 1997-2007 Kenneth D. Merry 429967Sache * Copyright (c) 2012 The FreeBSD Foundation 522373Spst * All rights reserved. 622373Spst * 722373Spst * Portions of this software were developed by Edward Tomasz Napierala 822373Spst * under sponsorship from the FreeBSD Foundation. 922373Spst * 1022373Spst * Redistribution and use in source and binary forms, with or without 1122373Spst * modification, are permitted provided that the following conditions 1222373Spst * are met: 1322373Spst * 1. Redistributions of source code must retain the above copyright 1422373Spst * notice, this list of conditions, and the following disclaimer, 1522373Spst * without modification. 1622373Spst * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1722373Spst * substantially similar to the "NO WARRANTY" disclaimer below 1822373Spst * ("Disclaimer") and any redistribution must be conditioned upon 1922373Spst * including a substantially similar Disclaimer requirement for further 2022373Spst * binary redistribution. 2122373Spst * 2222373Spst * NO WARRANTY 2322373Spst * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2422373Spst * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2522373Spst * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 2622373Spst * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2722373Spst * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2822373Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2922373Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3022373Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 3122373Spst * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 3222373Spst * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3322373Spst * POSSIBILITY OF SUCH DAMAGES. 3422373Spst * 3522373Spst * $FreeBSD: head/usr.sbin/ctld/kernel.c 268302 2014-07-05 21:18:33Z mav $ 3622373Spst */ 3722373Spst 3822373Spst#include <sys/ioctl.h> 3922373Spst#include <sys/types.h> 4022373Spst#include <sys/stat.h> 4122373Spst#include <sys/param.h> 4222373Spst#include <sys/linker.h> 4322373Spst#include <sys/queue.h> 4422373Spst#include <sys/callout.h> 4522373Spst#include <sys/sbuf.h> 4622373Spst#include <sys/capsicum.h> 4729967Sache#include <assert.h> 4829967Sache#include <bsdxml.h> 4929967Sache#include <ctype.h> 5022373Spst#include <errno.h> 5122373Spst#include <fcntl.h> 5222373Spst#include <stdint.h> 5322373Spst#include <stdio.h> 5422373Spst#include <stdlib.h> 5522373Spst#include <string.h> 5622373Spst#include <strings.h> 57230653Seadler#include <cam/scsi/scsi_all.h> 5822373Spst#include <cam/scsi/scsi_message.h> 5922373Spst#include <cam/ctl/ctl.h> 6022373Spst#include <cam/ctl/ctl_io.h> 6122373Spst#include <cam/ctl/ctl_frontend_internal.h> 6222373Spst#include <cam/ctl/ctl_backend.h> 6322373Spst#include <cam/ctl/ctl_ioctl.h> 6422373Spst#include <cam/ctl/ctl_backend_block.h> 6522373Spst#include <cam/ctl/ctl_util.h> 6622373Spst#include <cam/ctl/ctl_scsi_all.h> 6722373Spst 6822373Spst#include "ctld.h" 6922373Spst 7022373Spst#ifdef ICL_KERNEL_PROXY 7122373Spst#include <netdb.h> 7222373Spst#endif 7322373Spst 7422373Spstextern bool proxy_mode; 7522373Spst 7622373Spststatic int ctl_fd = 0; 7722373Spst 7822373Spstvoid 7922373Spstkernel_init(void) 8022373Spst{ 8122373Spst int retval, saved_errno; 8222373Spst 8322373Spst ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR); 8422373Spst if (ctl_fd < 0 && errno == ENOENT) { 8529967Sache saved_errno = errno; 8629967Sache retval = kldload("ctl"); 8729967Sache if (retval != -1) 8829967Sache ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR); 8929967Sache else 9029967Sache errno = saved_errno; 9129967Sache } 9229967Sache if (ctl_fd < 0) 9329967Sache log_err(1, "failed to open %s", CTL_DEFAULT_DEV); 9422373Spst} 9522373Spst 9622373Spst/* 9722373Spst * Name/value pair used for per-LUN attributes. 9822373Spst */ 9922373Spststruct cctl_lun_nv { 10022373Spst char *name; 10122373Spst char *value; 10222373Spst STAILQ_ENTRY(cctl_lun_nv) links; 10322373Spst}; 10422373Spst 10522373Spst/* 10622373Spst * Backend LUN information. 10722373Spst */ 10822373Spststruct cctl_lun { 10922373Spst uint64_t lun_id; 11022373Spst char *backend_type; 11122373Spst uint64_t size_blocks; 11222373Spst uint32_t blocksize; 11322373Spst char *serial_number; 11422373Spst char *device_id; 11522373Spst char *cfiscsi_target; 11622373Spst int cfiscsi_lun; 11722373Spst STAILQ_HEAD(,cctl_lun_nv) attr_list; 11822373Spst STAILQ_ENTRY(cctl_lun) links; 11922373Spst}; 12022373Spst 12122373Spststruct cctl_port { 12222373Spst uint32_t port_id; 12322373Spst char *cfiscsi_target; 12422373Spst uint16_t cfiscsi_portal_group_tag; 12522373Spst STAILQ_HEAD(,cctl_lun_nv) attr_list; 12622373Spst STAILQ_ENTRY(cctl_port) links; 12722373Spst}; 12822373Spst 12922373Spststruct cctl_devlist_data { 13022373Spst int num_luns; 13122373Spst STAILQ_HEAD(,cctl_lun) lun_list; 13222373Spst struct cctl_lun *cur_lun; 13322373Spst int num_ports; 13422373Spst STAILQ_HEAD(,cctl_port) port_list; 13522373Spst struct cctl_port *cur_port; 13622373Spst int level; 13722373Spst struct sbuf *cur_sb[32]; 13822373Spst}; 13922373Spst 14022373Spststatic void 14122373Spstcctl_start_element(void *user_data, const char *name, const char **attr) 14222373Spst{ 14322373Spst int i; 14422373Spst struct cctl_devlist_data *devlist; 14522373Spst struct cctl_lun *cur_lun; 14622373Spst 14722373Spst devlist = (struct cctl_devlist_data *)user_data; 14822373Spst cur_lun = devlist->cur_lun; 14922373Spst devlist->level++; 15022373Spst if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 15122373Spst sizeof(devlist->cur_sb[0]))) 15222373Spst log_errx(1, "%s: too many nesting levels, %zd max", __func__, 15322373Spst sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0])); 15429967Sache 15529967Sache devlist->cur_sb[devlist->level] = sbuf_new_auto(); 15629967Sache if (devlist->cur_sb[devlist->level] == NULL) 15729967Sache log_err(1, "%s: unable to allocate sbuf", __func__); 15829967Sache 15929967Sache if (strcmp(name, "lun") == 0) { 16029967Sache if (cur_lun != NULL) 16129967Sache log_errx(1, "%s: improper lun element nesting", 16229967Sache __func__); 16329967Sache 16429967Sache cur_lun = calloc(1, sizeof(*cur_lun)); 16529967Sache if (cur_lun == NULL) 16629967Sache log_err(1, "%s: cannot allocate %zd bytes", __func__, 16729967Sache sizeof(*cur_lun)); 16829967Sache 16929967Sache devlist->num_luns++; 17029967Sache devlist->cur_lun = cur_lun; 17129967Sache 17229967Sache STAILQ_INIT(&cur_lun->attr_list); 17330035Sache STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links); 17429967Sache 17529967Sache for (i = 0; attr[i] != NULL; i += 2) { 17629967Sache if (strcmp(attr[i], "id") == 0) { 17729967Sache cur_lun->lun_id = strtoull(attr[i+1], NULL, 0); 17829967Sache } else { 17929967Sache log_errx(1, "%s: invalid LUN attribute %s = %s", 18029967Sache __func__, attr[i], attr[i+1]); 18188846Sache } 18229967Sache } 18388846Sache } 18429967Sache} 18522373Spst 18622373Spststatic void 18722373Spstcctl_end_element(void *user_data, const char *name) 18822373Spst{ 18922373Spst struct cctl_devlist_data *devlist; 19022373Spst struct cctl_lun *cur_lun; 19122373Spst char *str; 19222373Spst 19322373Spst devlist = (struct cctl_devlist_data *)user_data; 19422373Spst cur_lun = devlist->cur_lun; 19522373Spst 19622373Spst if ((cur_lun == NULL) 19722373Spst && (strcmp(name, "ctllunlist") != 0)) 19822373Spst log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name); 19922373Spst 20022373Spst if (devlist->cur_sb[devlist->level] == NULL) 20122373Spst log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 20222373Spst devlist->level, name); 20322373Spst 20422373Spst sbuf_finish(devlist->cur_sb[devlist->level]); 20522373Spst str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 20629967Sache 20729967Sache if (strlen(str) == 0) { 20822373Spst free(str); 20922373Spst str = NULL; 21022373Spst } 21122373Spst 21222373Spst sbuf_delete(devlist->cur_sb[devlist->level]); 21322373Spst devlist->cur_sb[devlist->level] = NULL; 21422373Spst devlist->level--; 21529967Sache 21629967Sache if (strcmp(name, "backend_type") == 0) { 21729967Sache cur_lun->backend_type = str; 21822373Spst str = NULL; 219202196Sed } else if (strcmp(name, "size") == 0) { 22022373Spst cur_lun->size_blocks = strtoull(str, NULL, 0); 22122373Spst } else if (strcmp(name, "blocksize") == 0) { 22222373Spst cur_lun->blocksize = strtoul(str, NULL, 0); 22322373Spst } else if (strcmp(name, "serial_number") == 0) { 22422373Spst cur_lun->serial_number = str; 22522373Spst str = NULL; 22622373Spst } else if (strcmp(name, "device_id") == 0) { 22722373Spst cur_lun->device_id = str; 22822373Spst str = NULL; 22922373Spst } else if (strcmp(name, "cfiscsi_target") == 0) { 23029967Sache cur_lun->cfiscsi_target = str; 23129967Sache str = NULL; 23229967Sache } else if (strcmp(name, "cfiscsi_lun") == 0) { 23322373Spst cur_lun->cfiscsi_lun = strtoul(str, NULL, 0); 234202196Sed } else if (strcmp(name, "lun") == 0) { 23522373Spst devlist->cur_lun = NULL; 23622373Spst } else if (strcmp(name, "ctllunlist") == 0) { 23722373Spst 23822373Spst } else { 23922373Spst struct cctl_lun_nv *nv; 24022373Spst 24122373Spst nv = calloc(1, sizeof(*nv)); 24222373Spst if (nv == NULL) 24322373Spst log_err(1, "%s: can't allocate %zd bytes for nv pair", 24422373Spst __func__, sizeof(*nv)); 24522373Spst 24622373Spst nv->name = checked_strdup(name); 24722373Spst 24822373Spst nv->value = str; 24929967Sache str = NULL; 25022373Spst STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links); 25122373Spst } 25222373Spst 25322373Spst free(str); 25422373Spst} 25522373Spst 25622373Spststatic void 25722373Spstcctl_start_pelement(void *user_data, const char *name, const char **attr) 25829967Sache{ 25922373Spst int i; 26022373Spst struct cctl_devlist_data *devlist; 26122373Spst struct cctl_port *cur_port; 26222373Spst 26329967Sache devlist = (struct cctl_devlist_data *)user_data; 26429967Sache cur_port = devlist->cur_port; 26529967Sache devlist->level++; 26622373Spst if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 26722373Spst sizeof(devlist->cur_sb[0]))) 26822373Spst log_errx(1, "%s: too many nesting levels, %zd max", __func__, 26929967Sache sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0])); 27029967Sache 27129967Sache devlist->cur_sb[devlist->level] = sbuf_new_auto(); 27222373Spst if (devlist->cur_sb[devlist->level] == NULL) 27322373Spst log_err(1, "%s: unable to allocate sbuf", __func__); 27422373Spst 27522373Spst if (strcmp(name, "targ_port") == 0) { 27622373Spst if (cur_port != NULL) 27722373Spst log_errx(1, "%s: improper port element nesting (%s)", 27829967Sache __func__, name); 27929967Sache 28029967Sache cur_port = calloc(1, sizeof(*cur_port)); 28122373Spst if (cur_port == NULL) 28222373Spst log_err(1, "%s: cannot allocate %zd bytes", __func__, 28322373Spst sizeof(*cur_port)); 28429967Sache 28529967Sache devlist->num_ports++; 28629967Sache devlist->cur_port = cur_port; 28722373Spst 28822373Spst STAILQ_INIT(&cur_port->attr_list); 28922373Spst STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links); 29022373Spst 29122373Spst for (i = 0; attr[i] != NULL; i += 2) { 29222373Spst if (strcmp(attr[i], "id") == 0) { 29329967Sache cur_port->port_id = strtoul(attr[i+1], NULL, 0); 29429967Sache } else { 29529967Sache log_errx(1, "%s: invalid LUN attribute %s = %s", 29622373Spst __func__, attr[i], attr[i+1]); 29722373Spst } 29822373Spst } 29922373Spst } 30022373Spst} 30122373Spst 30222373Spststatic void 30322373Spstcctl_end_pelement(void *user_data, const char *name) 30422373Spst{ 30522373Spst struct cctl_devlist_data *devlist; 30622373Spst struct cctl_port *cur_port; 30722373Spst char *str; 30822373Spst 30922373Spst devlist = (struct cctl_devlist_data *)user_data; 31022373Spst cur_port = devlist->cur_port; 31122373Spst 31222373Spst if ((cur_port == NULL) 31322373Spst && (strcmp(name, "ctlportlist") != 0)) 31422373Spst log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name); 31522373Spst 31622373Spst if (devlist->cur_sb[devlist->level] == NULL) 31722373Spst log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 31822373Spst devlist->level, name); 31922373Spst 32022373Spst sbuf_finish(devlist->cur_sb[devlist->level]); 32122373Spst str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 32222373Spst 32322373Spst if (strlen(str) == 0) { 32422373Spst free(str); 32522373Spst str = NULL; 32622373Spst } 32722373Spst 32822373Spst sbuf_delete(devlist->cur_sb[devlist->level]); 32922373Spst devlist->cur_sb[devlist->level] = NULL; 33022373Spst devlist->level--; 33122373Spst 33222373Spst if (strcmp(name, "cfiscsi_target") == 0) { 33322373Spst cur_port->cfiscsi_target = str; 33422373Spst str = NULL; 33522373Spst } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) { 33622373Spst cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0); 33722373Spst } else if (strcmp(name, "targ_port") == 0) { 33822373Spst devlist->cur_port = NULL; 33922373Spst } else if (strcmp(name, "ctlportlist") == 0) { 34022373Spst 34122373Spst } else { 34222373Spst struct cctl_lun_nv *nv; 34322373Spst 34422373Spst nv = calloc(1, sizeof(*nv)); 34522373Spst if (nv == NULL) 34622373Spst log_err(1, "%s: can't allocate %zd bytes for nv pair", 34729967Sache __func__, sizeof(*nv)); 34829967Sache 34929967Sache nv->name = checked_strdup(name); 35022373Spst 35122373Spst nv->value = str; 35222373Spst str = NULL; 35322373Spst STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links); 35422373Spst } 35522373Spst 35622373Spst free(str); 35722373Spst} 35822373Spst 35922373Spststatic void 36022373Spstcctl_char_handler(void *user_data, const XML_Char *str, int len) 36122373Spst{ 36222373Spst struct cctl_devlist_data *devlist; 36322373Spst 36422373Spst devlist = (struct cctl_devlist_data *)user_data; 36522373Spst 36622373Spst sbuf_bcat(devlist->cur_sb[devlist->level], str, len); 36722373Spst} 36822373Spst 369202196Sedstruct conf * 37022373Spstconf_new_from_kernel(void) 37122373Spst{ 37222373Spst struct conf *conf = NULL; 37322373Spst struct target *targ; 37422373Spst struct lun *cl; 37522373Spst struct lun_option *lo; 37622373Spst struct ctl_lun_list list; 37722373Spst struct cctl_devlist_data devlist; 37822373Spst struct cctl_lun *lun; 37922373Spst struct cctl_port *port; 38022373Spst XML_Parser parser; 38122373Spst char *str; 382 int len, retval; 383 384 bzero(&devlist, sizeof(devlist)); 385 STAILQ_INIT(&devlist.lun_list); 386 STAILQ_INIT(&devlist.port_list); 387 388 log_debugx("obtaining previously configured CTL luns from the kernel"); 389 390 str = NULL; 391 len = 4096; 392retry: 393 str = realloc(str, len); 394 if (str == NULL) 395 log_err(1, "realloc"); 396 397 bzero(&list, sizeof(list)); 398 list.alloc_len = len; 399 list.status = CTL_LUN_LIST_NONE; 400 list.lun_xml = str; 401 402 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) { 403 log_warn("error issuing CTL_LUN_LIST ioctl"); 404 free(str); 405 return (NULL); 406 } 407 408 if (list.status == CTL_LUN_LIST_ERROR) { 409 log_warnx("error returned from CTL_LUN_LIST ioctl: %s", 410 list.error_str); 411 free(str); 412 return (NULL); 413 } 414 415 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 416 len = len << 1; 417 goto retry; 418 } 419 420 parser = XML_ParserCreate(NULL); 421 if (parser == NULL) { 422 log_warnx("unable to create XML parser"); 423 free(str); 424 return (NULL); 425 } 426 427 XML_SetUserData(parser, &devlist); 428 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element); 429 XML_SetCharacterDataHandler(parser, cctl_char_handler); 430 431 retval = XML_Parse(parser, str, strlen(str), 1); 432 XML_ParserFree(parser); 433 free(str); 434 if (retval != 1) { 435 log_warnx("XML_Parse failed"); 436 return (NULL); 437 } 438 439 str = NULL; 440 len = 4096; 441retry_port: 442 str = realloc(str, len); 443 if (str == NULL) 444 log_err(1, "realloc"); 445 446 bzero(&list, sizeof(list)); 447 list.alloc_len = len; 448 list.status = CTL_LUN_LIST_NONE; 449 list.lun_xml = str; 450 451 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) { 452 log_warn("error issuing CTL_PORT_LIST ioctl"); 453 free(str); 454 return (NULL); 455 } 456 457 if (list.status == CTL_PORT_LIST_ERROR) { 458 log_warnx("error returned from CTL_PORT_LIST ioctl: %s", 459 list.error_str); 460 free(str); 461 return (NULL); 462 } 463 464 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 465 len = len << 1; 466 goto retry_port; 467 } 468 469 parser = XML_ParserCreate(NULL); 470 if (parser == NULL) { 471 log_warnx("unable to create XML parser"); 472 free(str); 473 return (NULL); 474 } 475 476 XML_SetUserData(parser, &devlist); 477 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement); 478 XML_SetCharacterDataHandler(parser, cctl_char_handler); 479 480 retval = XML_Parse(parser, str, strlen(str), 1); 481 XML_ParserFree(parser); 482 free(str); 483 if (retval != 1) { 484 log_warnx("XML_Parse failed"); 485 return (NULL); 486 } 487 488 conf = conf_new(); 489 490 STAILQ_FOREACH(port, &devlist.port_list, links) { 491 492 if (port->cfiscsi_target == NULL) { 493 log_debugx("CTL port %ju wasn't managed by ctld; " 494 "ignoring", (uintmax_t)port->port_id); 495 continue; 496 } 497 498 targ = target_find(conf, port->cfiscsi_target); 499 if (targ == NULL) { 500#if 0 501 log_debugx("found new kernel target %s for CTL port %ld", 502 port->cfiscsi_target, port->port_id); 503#endif 504 targ = target_new(conf, port->cfiscsi_target); 505 if (targ == NULL) { 506 log_warnx("target_new failed"); 507 continue; 508 } 509 } 510 } 511 512 STAILQ_FOREACH(lun, &devlist.lun_list, links) { 513 struct cctl_lun_nv *nv; 514 515 if (lun->cfiscsi_target == NULL) { 516 log_debugx("CTL lun %ju wasn't managed by ctld; " 517 "ignoring", (uintmax_t)lun->lun_id); 518 continue; 519 } 520 521 targ = target_find(conf, lun->cfiscsi_target); 522 if (targ == NULL) { 523#if 0 524 log_debugx("found new kernel target %s for CTL lun %ld", 525 lun->cfiscsi_target, lun->lun_id); 526#endif 527 targ = target_new(conf, lun->cfiscsi_target); 528 if (targ == NULL) { 529 log_warnx("target_new failed"); 530 continue; 531 } 532 } 533 534 cl = lun_find(targ, lun->cfiscsi_lun); 535 if (cl != NULL) { 536 log_warnx("found CTL lun %ju, backing lun %d, target " 537 "%s, also backed by CTL lun %d; ignoring", 538 (uintmax_t) lun->lun_id, cl->l_lun, 539 cl->l_target->t_name, cl->l_ctl_lun); 540 continue; 541 } 542 543 log_debugx("found CTL lun %ju, backing lun %d, target %s", 544 (uintmax_t)lun->lun_id, lun->cfiscsi_lun, lun->cfiscsi_target); 545 546 cl = lun_new(targ, lun->cfiscsi_lun); 547 if (cl == NULL) { 548 log_warnx("lun_new failed"); 549 continue; 550 } 551 lun_set_backend(cl, lun->backend_type); 552 lun_set_blocksize(cl, lun->blocksize); 553 lun_set_device_id(cl, lun->device_id); 554 lun_set_serial(cl, lun->serial_number); 555 lun_set_size(cl, lun->size_blocks * cl->l_blocksize); 556 lun_set_ctl_lun(cl, lun->lun_id); 557 558 STAILQ_FOREACH(nv, &lun->attr_list, links) { 559 if (strcmp(nv->name, "file") == 0 || 560 strcmp(nv->name, "dev") == 0) { 561 lun_set_path(cl, nv->value); 562 continue; 563 } 564 lo = lun_option_new(cl, nv->name, nv->value); 565 if (lo == NULL) 566 log_warnx("unable to add CTL lun option %s " 567 "for CTL lun %ju for lun %d, target %s", 568 nv->name, (uintmax_t) lun->lun_id, 569 cl->l_lun, cl->l_target->t_name); 570 } 571 } 572 573 return (conf); 574} 575 576static void 577str_arg(struct ctl_be_arg *arg, const char *name, const char *value) 578{ 579 580 arg->namelen = strlen(name) + 1; 581 arg->name = __DECONST(char *, name); 582 arg->vallen = strlen(value) + 1; 583 arg->value = __DECONST(char *, value); 584 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD; 585} 586 587int 588kernel_lun_add(struct lun *lun) 589{ 590 struct lun_option *lo; 591 struct ctl_lun_req req; 592 char *tmp; 593 int error, i, num_options; 594 595 bzero(&req, sizeof(req)); 596 597 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 598 req.reqtype = CTL_LUNREQ_CREATE; 599 600 req.reqdata.create.blocksize_bytes = lun->l_blocksize; 601 602 if (lun->l_size != 0) 603 req.reqdata.create.lun_size_bytes = lun->l_size; 604 605 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE; 606 req.reqdata.create.device_type = T_DIRECT; 607 608 if (lun->l_serial != NULL) { 609 strncpy(req.reqdata.create.serial_num, lun->l_serial, 610 sizeof(req.reqdata.create.serial_num)); 611 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM; 612 } 613 614 if (lun->l_device_id != NULL) { 615 strncpy(req.reqdata.create.device_id, lun->l_device_id, 616 sizeof(req.reqdata.create.device_id)); 617 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID; 618 } 619 620 if (lun->l_path != NULL) { 621 lo = lun_option_find(lun, "file"); 622 if (lo != NULL) { 623 lun_option_set(lo, lun->l_path); 624 } else { 625 lo = lun_option_new(lun, "file", lun->l_path); 626 assert(lo != NULL); 627 } 628 } 629 630 lo = lun_option_find(lun, "cfiscsi_target"); 631 if (lo != NULL) { 632 lun_option_set(lo, lun->l_target->t_name); 633 } else { 634 lo = lun_option_new(lun, "cfiscsi_target", 635 lun->l_target->t_name); 636 assert(lo != NULL); 637 } 638 639 asprintf(&tmp, "%d", lun->l_lun); 640 if (tmp == NULL) 641 log_errx(1, "asprintf"); 642 lo = lun_option_find(lun, "cfiscsi_lun"); 643 if (lo != NULL) { 644 lun_option_set(lo, tmp); 645 free(tmp); 646 } else { 647 lo = lun_option_new(lun, "cfiscsi_lun", tmp); 648 free(tmp); 649 assert(lo != NULL); 650 } 651 652 asprintf(&tmp, "%s,lun,%d", lun->l_target->t_name, lun->l_lun); 653 if (tmp == NULL) 654 log_errx(1, "asprintf"); 655 lo = lun_option_find(lun, "scsiname"); 656 if (lo != NULL) { 657 lun_option_set(lo, tmp); 658 free(tmp); 659 } else { 660 lo = lun_option_new(lun, "scsiname", tmp); 661 free(tmp); 662 assert(lo != NULL); 663 } 664 665 num_options = 0; 666 TAILQ_FOREACH(lo, &lun->l_options, lo_next) 667 num_options++; 668 669 req.num_be_args = num_options; 670 if (num_options > 0) { 671 req.be_args = malloc(num_options * sizeof(*req.be_args)); 672 if (req.be_args == NULL) { 673 log_warn("error allocating %zd bytes", 674 num_options * sizeof(*req.be_args)); 675 return (1); 676 } 677 678 i = 0; 679 TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 680 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value); 681 i++; 682 } 683 assert(i == num_options); 684 } 685 686 error = ioctl(ctl_fd, CTL_LUN_REQ, &req); 687 free(req.be_args); 688 if (error != 0) { 689 log_warn("error issuing CTL_LUN_REQ ioctl"); 690 return (1); 691 } 692 693 if (req.status == CTL_LUN_ERROR) { 694 log_warnx("error returned from LUN creation request: %s", 695 req.error_str); 696 return (1); 697 } 698 699 if (req.status != CTL_LUN_OK) { 700 log_warnx("unknown LUN creation request status %d", 701 req.status); 702 return (1); 703 } 704 705 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id); 706 707 return (0); 708} 709 710int 711kernel_lun_resize(struct lun *lun) 712{ 713 struct ctl_lun_req req; 714 715 bzero(&req, sizeof(req)); 716 717 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 718 req.reqtype = CTL_LUNREQ_MODIFY; 719 720 req.reqdata.modify.lun_id = lun->l_ctl_lun; 721 req.reqdata.modify.lun_size_bytes = lun->l_size; 722 723 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) { 724 log_warn("error issuing CTL_LUN_REQ ioctl"); 725 return (1); 726 } 727 728 if (req.status == CTL_LUN_ERROR) { 729 log_warnx("error returned from LUN modification request: %s", 730 req.error_str); 731 return (1); 732 } 733 734 if (req.status != CTL_LUN_OK) { 735 log_warnx("unknown LUN modification request status %d", 736 req.status); 737 return (1); 738 } 739 740 return (0); 741} 742 743int 744kernel_lun_remove(struct lun *lun) 745{ 746 struct ctl_lun_req req; 747 748 bzero(&req, sizeof(req)); 749 750 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 751 req.reqtype = CTL_LUNREQ_RM; 752 753 req.reqdata.rm.lun_id = lun->l_ctl_lun; 754 755 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) { 756 log_warn("error issuing CTL_LUN_REQ ioctl"); 757 return (1); 758 } 759 760 if (req.status == CTL_LUN_ERROR) { 761 log_warnx("error returned from LUN removal request: %s", 762 req.error_str); 763 return (1); 764 } 765 766 if (req.status != CTL_LUN_OK) { 767 log_warnx("unknown LUN removal request status %d", req.status); 768 return (1); 769 } 770 771 return (0); 772} 773 774void 775kernel_handoff(struct connection *conn) 776{ 777 struct ctl_iscsi req; 778 779 bzero(&req, sizeof(req)); 780 781 req.type = CTL_ISCSI_HANDOFF; 782 strlcpy(req.data.handoff.initiator_name, 783 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name)); 784 strlcpy(req.data.handoff.initiator_addr, 785 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr)); 786 if (conn->conn_initiator_alias != NULL) { 787 strlcpy(req.data.handoff.initiator_alias, 788 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias)); 789 } 790 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid, 791 sizeof(req.data.handoff.initiator_isid)); 792 strlcpy(req.data.handoff.target_name, 793 conn->conn_target->t_name, sizeof(req.data.handoff.target_name)); 794#ifdef ICL_KERNEL_PROXY 795 if (proxy_mode) 796 req.data.handoff.connection_id = conn->conn_socket; 797 else 798 req.data.handoff.socket = conn->conn_socket; 799#else 800 req.data.handoff.socket = conn->conn_socket; 801#endif 802 req.data.handoff.portal_group_tag = 803 conn->conn_portal->p_portal_group->pg_tag; 804 if (conn->conn_header_digest == CONN_DIGEST_CRC32C) 805 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C; 806 if (conn->conn_data_digest == CONN_DIGEST_CRC32C) 807 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C; 808 req.data.handoff.cmdsn = conn->conn_cmdsn; 809 req.data.handoff.statsn = conn->conn_statsn; 810 req.data.handoff.max_recv_data_segment_length = 811 conn->conn_max_data_segment_length; 812 req.data.handoff.max_burst_length = conn->conn_max_burst_length; 813 req.data.handoff.immediate_data = conn->conn_immediate_data; 814 815 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 816 log_err(1, "error issuing CTL_ISCSI ioctl; " 817 "dropping connection"); 818 } 819 820 if (req.status != CTL_ISCSI_OK) { 821 log_errx(1, "error returned from CTL iSCSI handoff request: " 822 "%s; dropping connection", req.error_str); 823 } 824} 825 826int 827kernel_port_add(struct target *targ) 828{ 829 struct ctl_port_entry entry; 830 struct ctl_req req; 831 char tagstr[16]; 832 int error; 833 uint32_t port_id = -1; 834 835 bzero(&req, sizeof(req)); 836 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 837 req.reqtype = CTL_REQ_CREATE; 838 req.num_args = 4; 839 req.args = malloc(req.num_args * sizeof(*req.args)); 840 req.args[0].namelen = sizeof("port_id"); 841 req.args[0].name = __DECONST(char *, "port_id"); 842 req.args[0].vallen = sizeof(port_id); 843 req.args[0].value = &port_id; 844 req.args[0].flags = CTL_BEARG_WR; 845 str_arg(&req.args[1], "cfiscsi_target", targ->t_name); 846 str_arg(&req.args[2], "cfiscsi_target_alias", targ->t_alias); 847 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag); 848 str_arg(&req.args[3], "cfiscsi_portal_group_tag", tagstr); 849 850 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 851 free(req.args); 852 if (error != 0) { 853 log_warn("error issuing CTL_PORT_REQ ioctl"); 854 return (1); 855 } 856 857 if (req.status == CTL_LUN_ERROR) { 858 log_warnx("error returned from port creation request: %s", 859 req.error_str); 860 return (1); 861 } 862 863 if (req.status != CTL_LUN_OK) { 864 log_warnx("unknown port creation request status %d", 865 req.status); 866 return (1); 867 } 868 869 bzero(&entry, sizeof(entry)); 870 entry.targ_port = port_id; 871 872 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry); 873 if (error != 0) { 874 log_warn("CTL_ENABLE_PORT ioctl failed"); 875 return (-1); 876 } 877 878 return (0); 879} 880 881int 882kernel_port_remove(struct target *targ) 883{ 884 struct ctl_req req; 885 char tagstr[16]; 886 int error; 887 888 bzero(&req, sizeof(req)); 889 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 890 req.reqtype = CTL_REQ_REMOVE; 891 req.num_args = 2; 892 req.args = malloc(req.num_args * sizeof(*req.args)); 893 str_arg(&req.args[0], "cfiscsi_target", targ->t_name); 894 if (targ->t_portal_group) { 895 snprintf(tagstr, sizeof(tagstr), "%d", 896 targ->t_portal_group->pg_tag); 897 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr); 898 } else 899 req.num_args--; 900 901 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 902 free(req.args); 903 if (error != 0) { 904 log_warn("error issuing CTL_PORT_REQ ioctl"); 905 return (1); 906 } 907 908 if (req.status == CTL_LUN_ERROR) { 909 log_warnx("error returned from port removal request: %s", 910 req.error_str); 911 return (1); 912 } 913 914 if (req.status != CTL_LUN_OK) { 915 log_warnx("unknown port removal request status %d", 916 req.status); 917 return (1); 918 } 919 920 return (0); 921} 922 923#ifdef ICL_KERNEL_PROXY 924void 925kernel_listen(struct addrinfo *ai, bool iser, int portal_id) 926{ 927 struct ctl_iscsi req; 928 929 bzero(&req, sizeof(req)); 930 931 req.type = CTL_ISCSI_LISTEN; 932 req.data.listen.iser = iser; 933 req.data.listen.domain = ai->ai_family; 934 req.data.listen.socktype = ai->ai_socktype; 935 req.data.listen.protocol = ai->ai_protocol; 936 req.data.listen.addr = ai->ai_addr; 937 req.data.listen.addrlen = ai->ai_addrlen; 938 req.data.listen.portal_id = portal_id; 939 940 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 941 log_err(1, "error issuing CTL_ISCSI ioctl"); 942 943 if (req.status != CTL_ISCSI_OK) { 944 log_errx(1, "error returned from CTL iSCSI listen: %s", 945 req.error_str); 946 } 947} 948 949void 950kernel_accept(int *connection_id, int *portal_id, 951 struct sockaddr *client_sa, socklen_t *client_salen) 952{ 953 struct ctl_iscsi req; 954 struct sockaddr_storage ss; 955 956 bzero(&req, sizeof(req)); 957 958 req.type = CTL_ISCSI_ACCEPT; 959 req.data.accept.initiator_addr = (struct sockaddr *)&ss; 960 961 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 962 log_err(1, "error issuing CTL_ISCSI ioctl"); 963 964 if (req.status != CTL_ISCSI_OK) { 965 log_errx(1, "error returned from CTL iSCSI accept: %s", 966 req.error_str); 967 } 968 969 *connection_id = req.data.accept.connection_id; 970 *portal_id = req.data.accept.portal_id; 971 *client_salen = req.data.accept.initiator_addrlen; 972 memcpy(client_sa, &ss, *client_salen); 973} 974 975void 976kernel_send(struct pdu *pdu) 977{ 978 struct ctl_iscsi req; 979 980 bzero(&req, sizeof(req)); 981 982 req.type = CTL_ISCSI_SEND; 983 req.data.send.connection_id = pdu->pdu_connection->conn_socket; 984 req.data.send.bhs = pdu->pdu_bhs; 985 req.data.send.data_segment_len = pdu->pdu_data_len; 986 req.data.send.data_segment = pdu->pdu_data; 987 988 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 989 log_err(1, "error issuing CTL_ISCSI ioctl; " 990 "dropping connection"); 991 } 992 993 if (req.status != CTL_ISCSI_OK) { 994 log_errx(1, "error returned from CTL iSCSI send: " 995 "%s; dropping connection", req.error_str); 996 } 997} 998 999void 1000kernel_receive(struct pdu *pdu) 1001{ 1002 struct ctl_iscsi req; 1003 1004 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH); 1005 if (pdu->pdu_data == NULL) 1006 log_err(1, "malloc"); 1007 1008 bzero(&req, sizeof(req)); 1009 1010 req.type = CTL_ISCSI_RECEIVE; 1011 req.data.receive.connection_id = pdu->pdu_connection->conn_socket; 1012 req.data.receive.bhs = pdu->pdu_bhs; 1013 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH; 1014 req.data.receive.data_segment = pdu->pdu_data; 1015 1016 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1017 log_err(1, "error issuing CTL_ISCSI ioctl; " 1018 "dropping connection"); 1019 } 1020 1021 if (req.status != CTL_ISCSI_OK) { 1022 log_errx(1, "error returned from CTL iSCSI receive: " 1023 "%s; dropping connection", req.error_str); 1024 } 1025 1026} 1027 1028#endif /* ICL_KERNEL_PROXY */ 1029 1030/* 1031 * XXX: I CANT INTO LATIN 1032 */ 1033void 1034kernel_capsicate(void) 1035{ 1036 int error; 1037 cap_rights_t rights; 1038 const unsigned long cmds[] = { CTL_ISCSI }; 1039 1040 cap_rights_init(&rights, CAP_IOCTL); 1041 error = cap_rights_limit(ctl_fd, &rights); 1042 if (error != 0 && errno != ENOSYS) 1043 log_err(1, "cap_rights_limit"); 1044 1045 error = cap_ioctls_limit(ctl_fd, cmds, 1046 sizeof(cmds) / sizeof(cmds[0])); 1047 if (error != 0 && errno != ENOSYS) 1048 log_err(1, "cap_ioctls_limit"); 1049 1050 error = cap_enter(); 1051 if (error != 0 && errno != ENOSYS) 1052 log_err(1, "cap_enter"); 1053 1054 if (cap_sandboxed()) 1055 log_debugx("Capsicum capability mode enabled"); 1056 else 1057 log_warnx("Capsicum capability mode not supported"); 1058} 1059 1060