kernel.c revision 278354
1171568Sscottl/*- 2330449Seadler * Copyright (c) 2003, 2004 Silicon Graphics International Corp. 3330449Seadler * Copyright (c) 1997-2007 Kenneth D. Merry 4211095Sdes * Copyright (c) 2012 The FreeBSD Foundation 5171568Sscottl * All rights reserved. 6171568Sscottl * 7171568Sscottl * Portions of this software were developed by Edward Tomasz Napierala 8171568Sscottl * under sponsorship from the FreeBSD Foundation. 9171568Sscottl * 10171568Sscottl * Redistribution and use in source and binary forms, with or without 11171568Sscottl * modification, are permitted provided that the following conditions 12171568Sscottl * are met: 13171568Sscottl * 1. Redistributions of source code must retain the above copyright 14171568Sscottl * notice, this list of conditions, and the following disclaimer, 15171568Sscottl * without modification. 16171568Sscottl * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17171568Sscottl * substantially similar to the "NO WARRANTY" disclaimer below 18171568Sscottl * ("Disclaimer") and any redistribution must be conditioned upon 19171568Sscottl * including a substantially similar Disclaimer requirement for further 20171568Sscottl * binary redistribution. 21171568Sscottl * 22171568Sscottl * NO WARRANTY 23171568Sscottl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24171568Sscottl * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25171568Sscottl * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26171568Sscottl * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27171568Sscottl * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28171568Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29171568Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30171568Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31171568Sscottl * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32171568Sscottl * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33171568Sscottl * POSSIBILITY OF SUCH DAMAGES. 34171568Sscottl * 35171568Sscottl */ 36171568Sscottl 37171568Sscottl#include <sys/cdefs.h> 38171568Sscottl__FBSDID("$FreeBSD: head/usr.sbin/ctld/kernel.c 278354 2015-02-07 13:19:04Z mav $"); 39171568Sscottl 40171568Sscottl#include <sys/ioctl.h> 41171568Sscottl#include <sys/types.h> 42171568Sscottl#include <sys/stat.h> 43171568Sscottl#include <sys/param.h> 44171568Sscottl#include <sys/linker.h> 45171568Sscottl#include <sys/queue.h> 46171568Sscottl#include <sys/callout.h> 47171568Sscottl#include <sys/sbuf.h> 48171568Sscottl#include <sys/capsicum.h> 49254657Strasz#include <assert.h> 50211095Sdes#include <bsdxml.h> 51211095Sdes#include <ctype.h> 52171568Sscottl#include <errno.h> 53171568Sscottl#include <fcntl.h> 54171568Sscottl#include <stdint.h> 55171568Sscottl#include <stdio.h> 56171568Sscottl#include <stdlib.h> 57171568Sscottl#include <string.h> 58171568Sscottl#include <strings.h> 59171568Sscottl#include <cam/scsi/scsi_all.h> 60171568Sscottl#include <cam/scsi/scsi_message.h> 61171568Sscottl#include <cam/ctl/ctl.h> 62171568Sscottl#include <cam/ctl/ctl_io.h> 63171568Sscottl#include <cam/ctl/ctl_frontend_internal.h> 64171568Sscottl#include <cam/ctl/ctl_backend.h> 65171568Sscottl#include <cam/ctl/ctl_ioctl.h> 66171568Sscottl#include <cam/ctl/ctl_backend_block.h> 67171568Sscottl#include <cam/ctl/ctl_util.h> 68171568Sscottl#include <cam/ctl/ctl_scsi_all.h> 69171568Sscottl 70171568Sscottl#include "ctld.h" 71171568Sscottl 72171568Sscottl#ifdef ICL_KERNEL_PROXY 73171568Sscottl#include <netdb.h> 74171568Sscottl#endif 75171568Sscottl 76171568Sscottlextern bool proxy_mode; 77171568Sscottl 78171568Sscottlstatic int ctl_fd = 0; 79171568Sscottl 80171568Sscottlvoid 81171568Sscottlkernel_init(void) 82171568Sscottl{ 83171568Sscottl int retval, saved_errno; 84171568Sscottl 85171568Sscottl ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR); 86171568Sscottl if (ctl_fd < 0 && errno == ENOENT) { 87171568Sscottl saved_errno = errno; 88171568Sscottl retval = kldload("ctl"); 89171568Sscottl if (retval != -1) 90171568Sscottl ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR); 91171568Sscottl else 92171568Sscottl errno = saved_errno; 93171568Sscottl } 94171568Sscottl if (ctl_fd < 0) 95171568Sscottl log_err(1, "failed to open %s", CTL_DEFAULT_DEV); 96171568Sscottl} 97171568Sscottl 98171568Sscottl/* 99171568Sscottl * Name/value pair used for per-LUN attributes. 100171568Sscottl */ 101171568Sscottlstruct cctl_lun_nv { 102171568Sscottl char *name; 103171568Sscottl char *value; 104171568Sscottl STAILQ_ENTRY(cctl_lun_nv) links; 105171568Sscottl}; 106171568Sscottl 107171568Sscottl/* 108171568Sscottl * Backend LUN information. 109171568Sscottl */ 110171568Sscottlstruct cctl_lun { 111171568Sscottl uint64_t lun_id; 112171568Sscottl char *backend_type; 113171568Sscottl uint64_t size_blocks; 114171568Sscottl uint32_t blocksize; 115171568Sscottl char *serial_number; 116171568Sscottl char *device_id; 117171568Sscottl char *ctld_name; 118171568Sscottl STAILQ_HEAD(,cctl_lun_nv) attr_list; 119171568Sscottl STAILQ_ENTRY(cctl_lun) links; 120171568Sscottl}; 121171568Sscottl 122171568Sscottlstruct cctl_port { 123171568Sscottl uint32_t port_id; 124171568Sscottl char *port_name; 125171568Sscottl int cfiscsi_state; 126171568Sscottl char *cfiscsi_target; 127171568Sscottl uint16_t cfiscsi_portal_group_tag; 128171568Sscottl char *ctld_portal_group_name; 129171568Sscottl STAILQ_HEAD(,cctl_lun_nv) attr_list; 130171568Sscottl STAILQ_ENTRY(cctl_port) links; 131171568Sscottl}; 132171568Sscottl 133171568Sscottlstruct cctl_devlist_data { 134171568Sscottl int num_luns; 135171568Sscottl STAILQ_HEAD(,cctl_lun) lun_list; 136171568Sscottl struct cctl_lun *cur_lun; 137171568Sscottl int num_ports; 138171568Sscottl STAILQ_HEAD(,cctl_port) port_list; 139171568Sscottl struct cctl_port *cur_port; 140171568Sscottl int level; 141171568Sscottl struct sbuf *cur_sb[32]; 142171568Sscottl}; 143171568Sscottl 144171568Sscottlstatic void 145171568Sscottlcctl_start_element(void *user_data, const char *name, const char **attr) 146171568Sscottl{ 147171568Sscottl int i; 148171568Sscottl struct cctl_devlist_data *devlist; 149171568Sscottl struct cctl_lun *cur_lun; 150171568Sscottl 151171568Sscottl devlist = (struct cctl_devlist_data *)user_data; 152171568Sscottl cur_lun = devlist->cur_lun; 153171568Sscottl devlist->level++; 154171568Sscottl if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 155171568Sscottl sizeof(devlist->cur_sb[0]))) 156171568Sscottl log_errx(1, "%s: too many nesting levels, %zd max", __func__, 157171568Sscottl sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0])); 158171568Sscottl 159171568Sscottl devlist->cur_sb[devlist->level] = sbuf_new_auto(); 160171568Sscottl if (devlist->cur_sb[devlist->level] == NULL) 161171568Sscottl log_err(1, "%s: unable to allocate sbuf", __func__); 162171568Sscottl 163176033Spb if (strcmp(name, "lun") == 0) { 164176033Spb if (cur_lun != NULL) 165171568Sscottl log_errx(1, "%s: improper lun element nesting", 166171568Sscottl __func__); 167171568Sscottl 168171568Sscottl cur_lun = calloc(1, sizeof(*cur_lun)); 169171568Sscottl if (cur_lun == NULL) 170171568Sscottl log_err(1, "%s: cannot allocate %zd bytes", __func__, 171171568Sscottl sizeof(*cur_lun)); 172171568Sscottl 173176033Spb devlist->num_luns++; 174171568Sscottl devlist->cur_lun = cur_lun; 175171568Sscottl 176171568Sscottl STAILQ_INIT(&cur_lun->attr_list); 177171568Sscottl STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links); 178171568Sscottl 179171568Sscottl for (i = 0; attr[i] != NULL; i += 2) { 180171568Sscottl if (strcmp(attr[i], "id") == 0) { 181171568Sscottl cur_lun->lun_id = strtoull(attr[i+1], NULL, 0); 182171568Sscottl } else { 183171568Sscottl log_errx(1, "%s: invalid LUN attribute %s = %s", 184171568Sscottl __func__, attr[i], attr[i+1]); 185171568Sscottl } 186171568Sscottl } 187171568Sscottl } 188171568Sscottl} 189176033Spb 190176034Spbstatic void 191171568Sscottlcctl_end_element(void *user_data, const char *name) 192171568Sscottl{ 193171568Sscottl struct cctl_devlist_data *devlist; 194171568Sscottl struct cctl_lun *cur_lun; 195171568Sscottl char *str; 196171568Sscottl 197171568Sscottl devlist = (struct cctl_devlist_data *)user_data; 198171568Sscottl cur_lun = devlist->cur_lun; 199171568Sscottl 200171568Sscottl if ((cur_lun == NULL) 201171568Sscottl && (strcmp(name, "ctllunlist") != 0)) 202171568Sscottl log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name); 203171568Sscottl 204171568Sscottl if (devlist->cur_sb[devlist->level] == NULL) 205171568Sscottl log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 206171568Sscottl devlist->level, name); 207171568Sscottl 208171568Sscottl sbuf_finish(devlist->cur_sb[devlist->level]); 209171568Sscottl str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 210171568Sscottl 211171568Sscottl if (strlen(str) == 0) { 212171568Sscottl free(str); 213171568Sscottl str = NULL; 214171568Sscottl } 215171568Sscottl 216171568Sscottl sbuf_delete(devlist->cur_sb[devlist->level]); 217171568Sscottl devlist->cur_sb[devlist->level] = NULL; 218171568Sscottl devlist->level--; 219171568Sscottl 220171568Sscottl if (strcmp(name, "backend_type") == 0) { 221171568Sscottl cur_lun->backend_type = str; 222171568Sscottl str = NULL; 223171568Sscottl } else if (strcmp(name, "size") == 0) { 224171568Sscottl cur_lun->size_blocks = strtoull(str, NULL, 0); 225171568Sscottl } else if (strcmp(name, "blocksize") == 0) { 226171568Sscottl cur_lun->blocksize = strtoul(str, NULL, 0); 227171568Sscottl } else if (strcmp(name, "serial_number") == 0) { 228171568Sscottl cur_lun->serial_number = str; 229 str = NULL; 230 } else if (strcmp(name, "device_id") == 0) { 231 cur_lun->device_id = str; 232 str = NULL; 233 } else if (strcmp(name, "ctld_name") == 0) { 234 cur_lun->ctld_name = str; 235 str = NULL; 236 } else if (strcmp(name, "lun") == 0) { 237 devlist->cur_lun = NULL; 238 } else if (strcmp(name, "ctllunlist") == 0) { 239 /* Nothing. */ 240 } else { 241 struct cctl_lun_nv *nv; 242 243 nv = calloc(1, sizeof(*nv)); 244 if (nv == NULL) 245 log_err(1, "%s: can't allocate %zd bytes for nv pair", 246 __func__, sizeof(*nv)); 247 248 nv->name = checked_strdup(name); 249 250 nv->value = str; 251 str = NULL; 252 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links); 253 } 254 255 free(str); 256} 257 258static void 259cctl_start_pelement(void *user_data, const char *name, const char **attr) 260{ 261 int i; 262 struct cctl_devlist_data *devlist; 263 struct cctl_port *cur_port; 264 265 devlist = (struct cctl_devlist_data *)user_data; 266 cur_port = devlist->cur_port; 267 devlist->level++; 268 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) / 269 sizeof(devlist->cur_sb[0]))) 270 log_errx(1, "%s: too many nesting levels, %zd max", __func__, 271 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0])); 272 273 devlist->cur_sb[devlist->level] = sbuf_new_auto(); 274 if (devlist->cur_sb[devlist->level] == NULL) 275 log_err(1, "%s: unable to allocate sbuf", __func__); 276 277 if (strcmp(name, "targ_port") == 0) { 278 if (cur_port != NULL) 279 log_errx(1, "%s: improper port element nesting (%s)", 280 __func__, name); 281 282 cur_port = calloc(1, sizeof(*cur_port)); 283 if (cur_port == NULL) 284 log_err(1, "%s: cannot allocate %zd bytes", __func__, 285 sizeof(*cur_port)); 286 287 devlist->num_ports++; 288 devlist->cur_port = cur_port; 289 290 STAILQ_INIT(&cur_port->attr_list); 291 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links); 292 293 for (i = 0; attr[i] != NULL; i += 2) { 294 if (strcmp(attr[i], "id") == 0) { 295 cur_port->port_id = strtoul(attr[i+1], NULL, 0); 296 } else { 297 log_errx(1, "%s: invalid LUN attribute %s = %s", 298 __func__, attr[i], attr[i+1]); 299 } 300 } 301 } 302} 303 304static void 305cctl_end_pelement(void *user_data, const char *name) 306{ 307 struct cctl_devlist_data *devlist; 308 struct cctl_port *cur_port; 309 char *str; 310 311 devlist = (struct cctl_devlist_data *)user_data; 312 cur_port = devlist->cur_port; 313 314 if ((cur_port == NULL) 315 && (strcmp(name, "ctlportlist") != 0)) 316 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name); 317 318 if (devlist->cur_sb[devlist->level] == NULL) 319 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 320 devlist->level, name); 321 322 sbuf_finish(devlist->cur_sb[devlist->level]); 323 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level])); 324 325 if (strlen(str) == 0) { 326 free(str); 327 str = NULL; 328 } 329 330 sbuf_delete(devlist->cur_sb[devlist->level]); 331 devlist->cur_sb[devlist->level] = NULL; 332 devlist->level--; 333 334 if (strcmp(name, "port_name") == 0) { 335 cur_port->port_name = str; 336 str = NULL; 337 } else if (strcmp(name, "cfiscsi_target") == 0) { 338 cur_port->cfiscsi_target = str; 339 str = NULL; 340 } else if (strcmp(name, "cfiscsi_state") == 0) { 341 cur_port->cfiscsi_state = strtoul(str, NULL, 0); 342 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) { 343 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0); 344 } else if (strcmp(name, "ctld_portal_group_name") == 0) { 345 cur_port->ctld_portal_group_name = str; 346 str = NULL; 347 } else if (strcmp(name, "targ_port") == 0) { 348 devlist->cur_port = NULL; 349 } else if (strcmp(name, "ctlportlist") == 0) { 350 /* Nothing. */ 351 } else { 352 struct cctl_lun_nv *nv; 353 354 nv = calloc(1, sizeof(*nv)); 355 if (nv == NULL) 356 log_err(1, "%s: can't allocate %zd bytes for nv pair", 357 __func__, sizeof(*nv)); 358 359 nv->name = checked_strdup(name); 360 361 nv->value = str; 362 str = NULL; 363 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links); 364 } 365 366 free(str); 367} 368 369static void 370cctl_char_handler(void *user_data, const XML_Char *str, int len) 371{ 372 struct cctl_devlist_data *devlist; 373 374 devlist = (struct cctl_devlist_data *)user_data; 375 376 sbuf_bcat(devlist->cur_sb[devlist->level], str, len); 377} 378 379struct conf * 380conf_new_from_kernel(void) 381{ 382 struct conf *conf = NULL; 383 struct target *targ; 384 struct portal_group *pg; 385 struct pport *pp; 386 struct port *cp; 387 struct lun *cl; 388 struct lun_option *lo; 389 struct ctl_lun_list list; 390 struct cctl_devlist_data devlist; 391 struct cctl_lun *lun; 392 struct cctl_port *port; 393 XML_Parser parser; 394 char *str; 395 int len, retval; 396 397 bzero(&devlist, sizeof(devlist)); 398 STAILQ_INIT(&devlist.lun_list); 399 STAILQ_INIT(&devlist.port_list); 400 401 log_debugx("obtaining previously configured CTL luns from the kernel"); 402 403 str = NULL; 404 len = 4096; 405retry: 406 str = realloc(str, len); 407 if (str == NULL) 408 log_err(1, "realloc"); 409 410 bzero(&list, sizeof(list)); 411 list.alloc_len = len; 412 list.status = CTL_LUN_LIST_NONE; 413 list.lun_xml = str; 414 415 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) { 416 log_warn("error issuing CTL_LUN_LIST ioctl"); 417 free(str); 418 return (NULL); 419 } 420 421 if (list.status == CTL_LUN_LIST_ERROR) { 422 log_warnx("error returned from CTL_LUN_LIST ioctl: %s", 423 list.error_str); 424 free(str); 425 return (NULL); 426 } 427 428 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 429 len = len << 1; 430 goto retry; 431 } 432 433 parser = XML_ParserCreate(NULL); 434 if (parser == NULL) { 435 log_warnx("unable to create XML parser"); 436 free(str); 437 return (NULL); 438 } 439 440 XML_SetUserData(parser, &devlist); 441 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element); 442 XML_SetCharacterDataHandler(parser, cctl_char_handler); 443 444 retval = XML_Parse(parser, str, strlen(str), 1); 445 XML_ParserFree(parser); 446 free(str); 447 if (retval != 1) { 448 log_warnx("XML_Parse failed"); 449 return (NULL); 450 } 451 452 str = NULL; 453 len = 4096; 454retry_port: 455 str = realloc(str, len); 456 if (str == NULL) 457 log_err(1, "realloc"); 458 459 bzero(&list, sizeof(list)); 460 list.alloc_len = len; 461 list.status = CTL_LUN_LIST_NONE; 462 list.lun_xml = str; 463 464 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) { 465 log_warn("error issuing CTL_PORT_LIST ioctl"); 466 free(str); 467 return (NULL); 468 } 469 470 if (list.status == CTL_PORT_LIST_ERROR) { 471 log_warnx("error returned from CTL_PORT_LIST ioctl: %s", 472 list.error_str); 473 free(str); 474 return (NULL); 475 } 476 477 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 478 len = len << 1; 479 goto retry_port; 480 } 481 482 parser = XML_ParserCreate(NULL); 483 if (parser == NULL) { 484 log_warnx("unable to create XML parser"); 485 free(str); 486 return (NULL); 487 } 488 489 XML_SetUserData(parser, &devlist); 490 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement); 491 XML_SetCharacterDataHandler(parser, cctl_char_handler); 492 493 retval = XML_Parse(parser, str, strlen(str), 1); 494 XML_ParserFree(parser); 495 free(str); 496 if (retval != 1) { 497 log_warnx("XML_Parse failed"); 498 return (NULL); 499 } 500 501 conf = conf_new(); 502 503 STAILQ_FOREACH(port, &devlist.port_list, links) { 504 505 if (port->cfiscsi_target == NULL) { 506 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ", 507 port->port_id, port->port_name); 508 pp = pport_find(conf, port->port_name); 509 if (pp == NULL) { 510#if 0 511 log_debugx("found new kernel port %u \"%s\"", 512 port->port_id, port->port_name); 513#endif 514 pp = pport_new(conf, port->port_name, port->port_id); 515 if (pp == NULL) { 516 log_warnx("pport_new failed"); 517 continue; 518 } 519 } 520 continue; 521 } 522 if (port->cfiscsi_state != 1) { 523 log_debugx("CTL port %ju is not active (%d); ignoring", 524 (uintmax_t)port->port_id, port->cfiscsi_state); 525 continue; 526 } 527 528 targ = target_find(conf, port->cfiscsi_target); 529 if (targ == NULL) { 530#if 0 531 log_debugx("found new kernel target %s for CTL port %ld", 532 port->cfiscsi_target, port->port_id); 533#endif 534 targ = target_new(conf, port->cfiscsi_target); 535 if (targ == NULL) { 536 log_warnx("target_new failed"); 537 continue; 538 } 539 } 540 541 if (port->ctld_portal_group_name == NULL) 542 continue; 543 pg = portal_group_find(conf, port->ctld_portal_group_name); 544 if (pg == NULL) { 545#if 0 546 log_debugx("found new kernel portal group %s for CTL port %ld", 547 port->ctld_portal_group_name, port->port_id); 548#endif 549 pg = portal_group_new(conf, port->ctld_portal_group_name); 550 if (pg == NULL) { 551 log_warnx("portal_group_new failed"); 552 continue; 553 } 554 } 555 pg->pg_tag = port->cfiscsi_portal_group_tag; 556 cp = port_new(conf, targ, pg); 557 if (cp == NULL) { 558 log_warnx("port_new failed"); 559 continue; 560 } 561 cp->p_ctl_port = port->port_id; 562 } 563 564 STAILQ_FOREACH(lun, &devlist.lun_list, links) { 565 struct cctl_lun_nv *nv; 566 567 if (lun->ctld_name == NULL) { 568 log_debugx("CTL lun %ju wasn't managed by ctld; " 569 "ignoring", (uintmax_t)lun->lun_id); 570 continue; 571 } 572 573 cl = lun_find(conf, lun->ctld_name); 574 if (cl != NULL) { 575 log_warnx("found CTL lun %ju \"%s\", " 576 "also backed by CTL lun %d; ignoring", 577 (uintmax_t)lun->lun_id, lun->ctld_name, 578 cl->l_ctl_lun); 579 continue; 580 } 581 582 log_debugx("found CTL lun %ju \"%s\"", 583 (uintmax_t)lun->lun_id, lun->ctld_name); 584 585 cl = lun_new(conf, lun->ctld_name); 586 if (cl == NULL) { 587 log_warnx("lun_new failed"); 588 continue; 589 } 590 lun_set_backend(cl, lun->backend_type); 591 lun_set_blocksize(cl, lun->blocksize); 592 lun_set_device_id(cl, lun->device_id); 593 lun_set_serial(cl, lun->serial_number); 594 lun_set_size(cl, lun->size_blocks * cl->l_blocksize); 595 lun_set_ctl_lun(cl, lun->lun_id); 596 597 STAILQ_FOREACH(nv, &lun->attr_list, links) { 598 if (strcmp(nv->name, "file") == 0 || 599 strcmp(nv->name, "dev") == 0) { 600 lun_set_path(cl, nv->value); 601 continue; 602 } 603 lo = lun_option_new(cl, nv->name, nv->value); 604 if (lo == NULL) 605 log_warnx("unable to add CTL lun option %s " 606 "for CTL lun %ju \"%s\"", 607 nv->name, (uintmax_t) lun->lun_id, 608 cl->l_name); 609 } 610 } 611 612 return (conf); 613} 614 615static void 616str_arg(struct ctl_be_arg *arg, const char *name, const char *value) 617{ 618 619 arg->namelen = strlen(name) + 1; 620 arg->name = __DECONST(char *, name); 621 arg->vallen = strlen(value) + 1; 622 arg->value = __DECONST(char *, value); 623 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD; 624} 625 626int 627kernel_lun_add(struct lun *lun) 628{ 629 struct lun_option *lo; 630 struct ctl_lun_req req; 631 int error, i, num_options; 632 633 bzero(&req, sizeof(req)); 634 635 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 636 req.reqtype = CTL_LUNREQ_CREATE; 637 638 req.reqdata.create.blocksize_bytes = lun->l_blocksize; 639 640 if (lun->l_size != 0) 641 req.reqdata.create.lun_size_bytes = lun->l_size; 642 643 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE; 644 req.reqdata.create.device_type = T_DIRECT; 645 646 if (lun->l_serial != NULL) { 647 strncpy(req.reqdata.create.serial_num, lun->l_serial, 648 sizeof(req.reqdata.create.serial_num)); 649 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM; 650 } 651 652 if (lun->l_device_id != NULL) { 653 strncpy(req.reqdata.create.device_id, lun->l_device_id, 654 sizeof(req.reqdata.create.device_id)); 655 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID; 656 } 657 658 if (lun->l_path != NULL) { 659 lo = lun_option_find(lun, "file"); 660 if (lo != NULL) { 661 lun_option_set(lo, lun->l_path); 662 } else { 663 lo = lun_option_new(lun, "file", lun->l_path); 664 assert(lo != NULL); 665 } 666 } 667 668 lo = lun_option_find(lun, "ctld_name"); 669 if (lo != NULL) { 670 lun_option_set(lo, lun->l_name); 671 } else { 672 lo = lun_option_new(lun, "ctld_name", lun->l_name); 673 assert(lo != NULL); 674 } 675 676 lo = lun_option_find(lun, "scsiname"); 677 if (lo == NULL && lun->l_scsiname != NULL) { 678 lo = lun_option_new(lun, "scsiname", lun->l_scsiname); 679 assert(lo != NULL); 680 } 681 682 num_options = 0; 683 TAILQ_FOREACH(lo, &lun->l_options, lo_next) 684 num_options++; 685 686 req.num_be_args = num_options; 687 if (num_options > 0) { 688 req.be_args = malloc(num_options * sizeof(*req.be_args)); 689 if (req.be_args == NULL) { 690 log_warn("error allocating %zd bytes", 691 num_options * sizeof(*req.be_args)); 692 return (1); 693 } 694 695 i = 0; 696 TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 697 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value); 698 i++; 699 } 700 assert(i == num_options); 701 } 702 703 error = ioctl(ctl_fd, CTL_LUN_REQ, &req); 704 free(req.be_args); 705 if (error != 0) { 706 log_warn("error issuing CTL_LUN_REQ ioctl"); 707 return (1); 708 } 709 710 switch (req.status) { 711 case CTL_LUN_ERROR: 712 log_warnx("LUN creation error: %s", req.error_str); 713 return (1); 714 case CTL_LUN_WARNING: 715 log_warnx("LUN creation warning: %s", req.error_str); 716 break; 717 case CTL_LUN_OK: 718 break; 719 default: 720 log_warnx("unknown LUN creation status: %d", 721 req.status); 722 return (1); 723 } 724 725 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id); 726 return (0); 727} 728 729int 730kernel_lun_resize(struct lun *lun) 731{ 732 struct ctl_lun_req req; 733 734 bzero(&req, sizeof(req)); 735 736 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 737 req.reqtype = CTL_LUNREQ_MODIFY; 738 739 req.reqdata.modify.lun_id = lun->l_ctl_lun; 740 req.reqdata.modify.lun_size_bytes = lun->l_size; 741 742 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) { 743 log_warn("error issuing CTL_LUN_REQ ioctl"); 744 return (1); 745 } 746 747 switch (req.status) { 748 case CTL_LUN_ERROR: 749 log_warnx("LUN modification error: %s", req.error_str); 750 return (1); 751 case CTL_LUN_WARNING: 752 log_warnx("LUN modification warning: %s", req.error_str); 753 break; 754 case CTL_LUN_OK: 755 break; 756 default: 757 log_warnx("unknown LUN modification status: %d", 758 req.status); 759 return (1); 760 } 761 762 return (0); 763} 764 765int 766kernel_lun_remove(struct lun *lun) 767{ 768 struct ctl_lun_req req; 769 770 bzero(&req, sizeof(req)); 771 772 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 773 req.reqtype = CTL_LUNREQ_RM; 774 775 req.reqdata.rm.lun_id = lun->l_ctl_lun; 776 777 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) { 778 log_warn("error issuing CTL_LUN_REQ ioctl"); 779 return (1); 780 } 781 782 switch (req.status) { 783 case CTL_LUN_ERROR: 784 log_warnx("LUN removal error: %s", req.error_str); 785 return (1); 786 case CTL_LUN_WARNING: 787 log_warnx("LUN removal warning: %s", req.error_str); 788 break; 789 case CTL_LUN_OK: 790 break; 791 default: 792 log_warnx("unknown LUN removal status: %d", req.status); 793 return (1); 794 } 795 796 return (0); 797} 798 799void 800kernel_handoff(struct connection *conn) 801{ 802 struct ctl_iscsi req; 803 804 bzero(&req, sizeof(req)); 805 806 req.type = CTL_ISCSI_HANDOFF; 807 strlcpy(req.data.handoff.initiator_name, 808 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name)); 809 strlcpy(req.data.handoff.initiator_addr, 810 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr)); 811 if (conn->conn_initiator_alias != NULL) { 812 strlcpy(req.data.handoff.initiator_alias, 813 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias)); 814 } 815 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid, 816 sizeof(req.data.handoff.initiator_isid)); 817 strlcpy(req.data.handoff.target_name, 818 conn->conn_target->t_name, sizeof(req.data.handoff.target_name)); 819 if (conn->conn_target->t_offload != NULL) { 820 strlcpy(req.data.handoff.offload, 821 conn->conn_target->t_offload, sizeof(req.data.handoff.offload)); 822 } 823#ifdef ICL_KERNEL_PROXY 824 if (proxy_mode) 825 req.data.handoff.connection_id = conn->conn_socket; 826 else 827 req.data.handoff.socket = conn->conn_socket; 828#else 829 req.data.handoff.socket = conn->conn_socket; 830#endif 831 req.data.handoff.portal_group_tag = 832 conn->conn_portal->p_portal_group->pg_tag; 833 if (conn->conn_header_digest == CONN_DIGEST_CRC32C) 834 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C; 835 if (conn->conn_data_digest == CONN_DIGEST_CRC32C) 836 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C; 837 req.data.handoff.cmdsn = conn->conn_cmdsn; 838 req.data.handoff.statsn = conn->conn_statsn; 839 req.data.handoff.max_recv_data_segment_length = 840 conn->conn_max_data_segment_length; 841 req.data.handoff.max_burst_length = conn->conn_max_burst_length; 842 req.data.handoff.immediate_data = conn->conn_immediate_data; 843 844 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 845 log_err(1, "error issuing CTL_ISCSI ioctl; " 846 "dropping connection"); 847 } 848 849 if (req.status != CTL_ISCSI_OK) { 850 log_errx(1, "error returned from CTL iSCSI handoff request: " 851 "%s; dropping connection", req.error_str); 852 } 853} 854 855void 856kernel_limits(const char *offload, size_t *max_data_segment_length) 857{ 858 struct ctl_iscsi req; 859 860 bzero(&req, sizeof(req)); 861 862 req.type = CTL_ISCSI_LIMITS; 863 if (offload != NULL) { 864 strlcpy(req.data.limits.offload, offload, 865 sizeof(req.data.limits.offload)); 866 } 867 868 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 869 log_err(1, "error issuing CTL_ISCSI ioctl; " 870 "dropping connection"); 871 } 872 873 if (req.status != CTL_ISCSI_OK) { 874 log_errx(1, "error returned from CTL iSCSI limits request: " 875 "%s; dropping connection", req.error_str); 876 } 877 878 *max_data_segment_length = req.data.limits.data_segment_limit; 879 if (offload != NULL) { 880 log_debugx("MaxRecvDataSegment kernel limit for offload " 881 "\"%s\" is %zd", offload, *max_data_segment_length); 882 } else { 883 log_debugx("MaxRecvDataSegment kernel limit is %zd", 884 *max_data_segment_length); 885 } 886} 887 888int 889kernel_port_add(struct port *port) 890{ 891 struct ctl_port_entry entry; 892 struct ctl_req req; 893 struct ctl_lun_map lm; 894 struct target *targ = port->p_target; 895 struct portal_group *pg = port->p_portal_group; 896 char tagstr[16]; 897 int error, i, n; 898 899 /* Create iSCSI port. */ 900 if (port->p_portal_group) { 901 bzero(&req, sizeof(req)); 902 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 903 req.reqtype = CTL_REQ_CREATE; 904 req.args = malloc(req.num_args * sizeof(*req.args)); 905 n = 0; 906 req.args[n].namelen = sizeof("port_id"); 907 req.args[n].name = __DECONST(char *, "port_id"); 908 req.args[n].vallen = sizeof(port->p_ctl_port); 909 req.args[n].value = &port->p_ctl_port; 910 req.args[n++].flags = CTL_BEARG_WR; 911 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name); 912 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag); 913 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr); 914 if (targ->t_alias) 915 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias); 916 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name); 917 req.num_args = n; 918 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 919 free(req.args); 920 if (error != 0) { 921 log_warn("error issuing CTL_PORT_REQ ioctl"); 922 return (1); 923 } 924 if (req.status == CTL_LUN_ERROR) { 925 log_warnx("error returned from port creation request: %s", 926 req.error_str); 927 return (1); 928 } 929 if (req.status != CTL_LUN_OK) { 930 log_warnx("unknown port creation request status %d", 931 req.status); 932 return (1); 933 } 934 } else if (port->p_pport) 935 port->p_ctl_port = port->p_pport->pp_ctl_port; 936 937 /* Explicitly enable mapping to block any access except allowed. */ 938 lm.port = port->p_ctl_port; 939 lm.plun = UINT32_MAX; 940 lm.lun = 0; 941 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 942 if (error != 0) 943 log_warn("CTL_LUN_MAP ioctl failed"); 944 945 /* Map configured LUNs */ 946 for (i = 0; i < MAX_LUNS; i++) { 947 if (targ->t_luns[i] == NULL) 948 continue; 949 lm.port = port->p_ctl_port; 950 lm.plun = i; 951 lm.lun = targ->t_luns[i]->l_ctl_lun; 952 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 953 if (error != 0) 954 log_warn("CTL_LUN_MAP ioctl failed"); 955 } 956 957 /* Enable port */ 958 bzero(&entry, sizeof(entry)); 959 entry.targ_port = port->p_ctl_port; 960 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry); 961 if (error != 0) { 962 log_warn("CTL_ENABLE_PORT ioctl failed"); 963 return (-1); 964 } 965 966 return (0); 967} 968 969int 970kernel_port_update(struct port *port) 971{ 972 struct ctl_lun_map lm; 973 struct target *targ = port->p_target; 974 int error, i; 975 976 /* Map configured LUNs and unmap others */ 977 for (i = 0; i < MAX_LUNS; i++) { 978 lm.port = port->p_ctl_port; 979 lm.plun = i; 980 if (targ->t_luns[i] == NULL) 981 lm.lun = UINT32_MAX; 982 else 983 lm.lun = targ->t_luns[i]->l_ctl_lun; 984 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 985 if (error != 0) 986 log_warn("CTL_LUN_MAP ioctl failed"); 987 } 988 return (0); 989} 990 991int 992kernel_port_remove(struct port *port) 993{ 994 struct ctl_port_entry entry; 995 struct ctl_lun_map lm; 996 struct ctl_req req; 997 char tagstr[16]; 998 struct target *targ = port->p_target; 999 struct portal_group *pg = port->p_portal_group; 1000 int error; 1001 1002 /* Disable port */ 1003 bzero(&entry, sizeof(entry)); 1004 entry.targ_port = port->p_ctl_port; 1005 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry); 1006 if (error != 0) { 1007 log_warn("CTL_DISABLE_PORT ioctl failed"); 1008 return (-1); 1009 } 1010 1011 /* Remove iSCSI port. */ 1012 if (port->p_portal_group) { 1013 bzero(&req, sizeof(req)); 1014 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 1015 req.reqtype = CTL_REQ_REMOVE; 1016 req.num_args = 2; 1017 req.args = malloc(req.num_args * sizeof(*req.args)); 1018 str_arg(&req.args[0], "cfiscsi_target", targ->t_name); 1019 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag); 1020 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr); 1021 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 1022 free(req.args); 1023 if (error != 0) { 1024 log_warn("error issuing CTL_PORT_REQ ioctl"); 1025 return (1); 1026 } 1027 if (req.status == CTL_LUN_ERROR) { 1028 log_warnx("error returned from port removal request: %s", 1029 req.error_str); 1030 return (1); 1031 } 1032 if (req.status != CTL_LUN_OK) { 1033 log_warnx("unknown port removal request status %d", 1034 req.status); 1035 return (1); 1036 } 1037 } else { 1038 /* Disable LUN mapping. */ 1039 lm.port = port->p_ctl_port; 1040 lm.plun = UINT32_MAX; 1041 lm.lun = UINT32_MAX; 1042 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 1043 if (error != 0) 1044 log_warn("CTL_LUN_MAP ioctl failed"); 1045 } 1046 return (0); 1047} 1048 1049#ifdef ICL_KERNEL_PROXY 1050void 1051kernel_listen(struct addrinfo *ai, bool iser, int portal_id) 1052{ 1053 struct ctl_iscsi req; 1054 1055 bzero(&req, sizeof(req)); 1056 1057 req.type = CTL_ISCSI_LISTEN; 1058 req.data.listen.iser = iser; 1059 req.data.listen.domain = ai->ai_family; 1060 req.data.listen.socktype = ai->ai_socktype; 1061 req.data.listen.protocol = ai->ai_protocol; 1062 req.data.listen.addr = ai->ai_addr; 1063 req.data.listen.addrlen = ai->ai_addrlen; 1064 req.data.listen.portal_id = portal_id; 1065 1066 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 1067 log_err(1, "error issuing CTL_ISCSI ioctl"); 1068 1069 if (req.status != CTL_ISCSI_OK) { 1070 log_errx(1, "error returned from CTL iSCSI listen: %s", 1071 req.error_str); 1072 } 1073} 1074 1075void 1076kernel_accept(int *connection_id, int *portal_id, 1077 struct sockaddr *client_sa, socklen_t *client_salen) 1078{ 1079 struct ctl_iscsi req; 1080 struct sockaddr_storage ss; 1081 1082 bzero(&req, sizeof(req)); 1083 1084 req.type = CTL_ISCSI_ACCEPT; 1085 req.data.accept.initiator_addr = (struct sockaddr *)&ss; 1086 1087 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) 1088 log_err(1, "error issuing CTL_ISCSI ioctl"); 1089 1090 if (req.status != CTL_ISCSI_OK) { 1091 log_errx(1, "error returned from CTL iSCSI accept: %s", 1092 req.error_str); 1093 } 1094 1095 *connection_id = req.data.accept.connection_id; 1096 *portal_id = req.data.accept.portal_id; 1097 *client_salen = req.data.accept.initiator_addrlen; 1098 memcpy(client_sa, &ss, *client_salen); 1099} 1100 1101void 1102kernel_send(struct pdu *pdu) 1103{ 1104 struct ctl_iscsi req; 1105 1106 bzero(&req, sizeof(req)); 1107 1108 req.type = CTL_ISCSI_SEND; 1109 req.data.send.connection_id = pdu->pdu_connection->conn_socket; 1110 req.data.send.bhs = pdu->pdu_bhs; 1111 req.data.send.data_segment_len = pdu->pdu_data_len; 1112 req.data.send.data_segment = pdu->pdu_data; 1113 1114 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1115 log_err(1, "error issuing CTL_ISCSI ioctl; " 1116 "dropping connection"); 1117 } 1118 1119 if (req.status != CTL_ISCSI_OK) { 1120 log_errx(1, "error returned from CTL iSCSI send: " 1121 "%s; dropping connection", req.error_str); 1122 } 1123} 1124 1125void 1126kernel_receive(struct pdu *pdu) 1127{ 1128 struct ctl_iscsi req; 1129 1130 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH); 1131 if (pdu->pdu_data == NULL) 1132 log_err(1, "malloc"); 1133 1134 bzero(&req, sizeof(req)); 1135 1136 req.type = CTL_ISCSI_RECEIVE; 1137 req.data.receive.connection_id = pdu->pdu_connection->conn_socket; 1138 req.data.receive.bhs = pdu->pdu_bhs; 1139 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH; 1140 req.data.receive.data_segment = pdu->pdu_data; 1141 1142 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) { 1143 log_err(1, "error issuing CTL_ISCSI ioctl; " 1144 "dropping connection"); 1145 } 1146 1147 if (req.status != CTL_ISCSI_OK) { 1148 log_errx(1, "error returned from CTL iSCSI receive: " 1149 "%s; dropping connection", req.error_str); 1150 } 1151 1152} 1153 1154#endif /* ICL_KERNEL_PROXY */ 1155 1156/* 1157 * XXX: I CANT INTO LATIN 1158 */ 1159void 1160kernel_capsicate(void) 1161{ 1162 int error; 1163 cap_rights_t rights; 1164 const unsigned long cmds[] = { CTL_ISCSI }; 1165 1166 cap_rights_init(&rights, CAP_IOCTL); 1167 error = cap_rights_limit(ctl_fd, &rights); 1168 if (error != 0 && errno != ENOSYS) 1169 log_err(1, "cap_rights_limit"); 1170 1171 error = cap_ioctls_limit(ctl_fd, cmds, 1172 sizeof(cmds) / sizeof(cmds[0])); 1173 if (error != 0 && errno != ENOSYS) 1174 log_err(1, "cap_ioctls_limit"); 1175 1176 error = cap_enter(); 1177 if (error != 0 && errno != ENOSYS) 1178 log_err(1, "cap_enter"); 1179 1180 if (cap_sandboxed()) 1181 log_debugx("Capsicum capability mode enabled"); 1182 else 1183 log_warnx("Capsicum capability mode not supported"); 1184} 1185 1186