2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
5 * Copyright (c) 1997-2007 Kenneth D. Merry
6 * Copyright (c) 2012 The FreeBSD Foundation
7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions, and the following disclaimer,
18 * without modification.
19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20 * substantially similar to the "NO WARRANTY" disclaimer below
21 * ("Disclaimer") and any redistribution must be conditioned upon
22 * including a substantially similar Disclaimer requirement for further
23 * binary redistribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGES.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/capsicum.h>
45 #include <sys/callout.h>
46 #include <sys/ioctl.h>
47 #include <sys/linker.h>
48 #include <sys/module.h>
49 #include <sys/queue.h>
55 #include <capsicum_helpers.h>
64 #include <cam/scsi/scsi_all.h>
65 #include <cam/scsi/scsi_message.h>
66 #include <cam/ctl/ctl.h>
67 #include <cam/ctl/ctl_io.h>
68 #include <cam/ctl/ctl_backend.h>
69 #include <cam/ctl/ctl_ioctl.h>
70 #include <cam/ctl/ctl_util.h>
71 #include <cam/ctl/ctl_scsi_all.h>
75 #ifdef ICL_KERNEL_PROXY
79 #define NVLIST_BUFSIZE 1024
81 extern bool proxy_mode;
83 static int ctl_fd = 0;
88 int retval, saved_errno;
90 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
91 if (ctl_fd < 0 && errno == ENOENT) {
93 retval = kldload("ctl");
95 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
100 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
104 if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
105 log_warn("couldn't load cfiscsi");
112 * Name/value pair used for per-LUN attributes.
117 STAILQ_ENTRY(cctl_lun_nv) links;
121 * Backend LUN information.
127 uint64_t size_blocks;
132 STAILQ_HEAD(,cctl_lun_nv) attr_list;
133 STAILQ_ENTRY(cctl_lun) links;
143 char *cfiscsi_target;
144 uint16_t cfiscsi_portal_group_tag;
145 char *ctld_portal_group_name;
146 STAILQ_HEAD(,cctl_lun_nv) attr_list;
147 STAILQ_ENTRY(cctl_port) links;
150 struct cctl_devlist_data {
152 STAILQ_HEAD(,cctl_lun) lun_list;
153 struct cctl_lun *cur_lun;
155 STAILQ_HEAD(,cctl_port) port_list;
156 struct cctl_port *cur_port;
158 struct sbuf *cur_sb[32];
162 cctl_start_element(void *user_data, const char *name, const char **attr)
165 struct cctl_devlist_data *devlist;
166 struct cctl_lun *cur_lun;
168 devlist = (struct cctl_devlist_data *)user_data;
169 cur_lun = devlist->cur_lun;
171 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
172 sizeof(devlist->cur_sb[0])))
173 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
174 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
176 devlist->cur_sb[devlist->level] = sbuf_new_auto();
177 if (devlist->cur_sb[devlist->level] == NULL)
178 log_err(1, "%s: unable to allocate sbuf", __func__);
180 if (strcmp(name, "lun") == 0) {
182 log_errx(1, "%s: improper lun element nesting",
185 cur_lun = calloc(1, sizeof(*cur_lun));
187 log_err(1, "%s: cannot allocate %zd bytes", __func__,
191 devlist->cur_lun = cur_lun;
193 STAILQ_INIT(&cur_lun->attr_list);
194 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
196 for (i = 0; attr[i] != NULL; i += 2) {
197 if (strcmp(attr[i], "id") == 0) {
198 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
200 log_errx(1, "%s: invalid LUN attribute %s = %s",
201 __func__, attr[i], attr[i+1]);
208 cctl_end_element(void *user_data, const char *name)
210 struct cctl_devlist_data *devlist;
211 struct cctl_lun *cur_lun;
214 devlist = (struct cctl_devlist_data *)user_data;
215 cur_lun = devlist->cur_lun;
217 if ((cur_lun == NULL)
218 && (strcmp(name, "ctllunlist") != 0))
219 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
221 if (devlist->cur_sb[devlist->level] == NULL)
222 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
223 devlist->level, name);
225 sbuf_finish(devlist->cur_sb[devlist->level]);
226 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
228 if (strlen(str) == 0) {
233 sbuf_delete(devlist->cur_sb[devlist->level]);
234 devlist->cur_sb[devlist->level] = NULL;
237 if (strcmp(name, "backend_type") == 0) {
238 cur_lun->backend_type = str;
240 } else if (strcmp(name, "lun_type") == 0) {
241 cur_lun->device_type = strtoull(str, NULL, 0);
242 } else if (strcmp(name, "size") == 0) {
243 cur_lun->size_blocks = strtoull(str, NULL, 0);
244 } else if (strcmp(name, "blocksize") == 0) {
245 cur_lun->blocksize = strtoul(str, NULL, 0);
246 } else if (strcmp(name, "serial_number") == 0) {
247 cur_lun->serial_number = str;
249 } else if (strcmp(name, "device_id") == 0) {
250 cur_lun->device_id = str;
252 } else if (strcmp(name, "ctld_name") == 0) {
253 cur_lun->ctld_name = str;
255 } else if (strcmp(name, "lun") == 0) {
256 devlist->cur_lun = NULL;
257 } else if (strcmp(name, "ctllunlist") == 0) {
260 struct cctl_lun_nv *nv;
262 nv = calloc(1, sizeof(*nv));
264 log_err(1, "%s: can't allocate %zd bytes for nv pair",
265 __func__, sizeof(*nv));
267 nv->name = checked_strdup(name);
271 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
278 cctl_start_pelement(void *user_data, const char *name, const char **attr)
281 struct cctl_devlist_data *devlist;
282 struct cctl_port *cur_port;
284 devlist = (struct cctl_devlist_data *)user_data;
285 cur_port = devlist->cur_port;
287 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
288 sizeof(devlist->cur_sb[0])))
289 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
290 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
292 devlist->cur_sb[devlist->level] = sbuf_new_auto();
293 if (devlist->cur_sb[devlist->level] == NULL)
294 log_err(1, "%s: unable to allocate sbuf", __func__);
296 if (strcmp(name, "targ_port") == 0) {
297 if (cur_port != NULL)
298 log_errx(1, "%s: improper port element nesting (%s)",
301 cur_port = calloc(1, sizeof(*cur_port));
302 if (cur_port == NULL)
303 log_err(1, "%s: cannot allocate %zd bytes", __func__,
306 devlist->num_ports++;
307 devlist->cur_port = cur_port;
309 STAILQ_INIT(&cur_port->attr_list);
310 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
312 for (i = 0; attr[i] != NULL; i += 2) {
313 if (strcmp(attr[i], "id") == 0) {
314 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
316 log_errx(1, "%s: invalid LUN attribute %s = %s",
317 __func__, attr[i], attr[i+1]);
324 cctl_end_pelement(void *user_data, const char *name)
326 struct cctl_devlist_data *devlist;
327 struct cctl_port *cur_port;
330 devlist = (struct cctl_devlist_data *)user_data;
331 cur_port = devlist->cur_port;
333 if ((cur_port == NULL)
334 && (strcmp(name, "ctlportlist") != 0))
335 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
337 if (devlist->cur_sb[devlist->level] == NULL)
338 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
339 devlist->level, name);
341 sbuf_finish(devlist->cur_sb[devlist->level]);
342 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
344 if (strlen(str) == 0) {
349 sbuf_delete(devlist->cur_sb[devlist->level]);
350 devlist->cur_sb[devlist->level] = NULL;
353 if (strcmp(name, "frontend_type") == 0) {
354 cur_port->port_frontend = str;
356 } else if (strcmp(name, "port_name") == 0) {
357 cur_port->port_name = str;
359 } else if (strcmp(name, "physical_port") == 0) {
360 cur_port->pp = strtoul(str, NULL, 0);
361 } else if (strcmp(name, "virtual_port") == 0) {
362 cur_port->vp = strtoul(str, NULL, 0);
363 } else if (strcmp(name, "cfiscsi_target") == 0) {
364 cur_port->cfiscsi_target = str;
366 } else if (strcmp(name, "cfiscsi_state") == 0) {
367 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
368 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
369 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
370 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
371 cur_port->ctld_portal_group_name = str;
373 } else if (strcmp(name, "targ_port") == 0) {
374 devlist->cur_port = NULL;
375 } else if (strcmp(name, "ctlportlist") == 0) {
378 struct cctl_lun_nv *nv;
380 nv = calloc(1, sizeof(*nv));
382 log_err(1, "%s: can't allocate %zd bytes for nv pair",
383 __func__, sizeof(*nv));
385 nv->name = checked_strdup(name);
389 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
396 cctl_char_handler(void *user_data, const XML_Char *str, int len)
398 struct cctl_devlist_data *devlist;
400 devlist = (struct cctl_devlist_data *)user_data;
402 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
406 conf_new_from_kernel(void)
408 struct conf *conf = NULL;
410 struct portal_group *pg;
415 struct ctl_lun_list list;
416 struct cctl_devlist_data devlist;
417 struct cctl_lun *lun;
418 struct cctl_port *port;
423 bzero(&devlist, sizeof(devlist));
424 STAILQ_INIT(&devlist.lun_list);
425 STAILQ_INIT(&devlist.port_list);
427 log_debugx("obtaining previously configured CTL luns from the kernel");
432 str = realloc(str, len);
434 log_err(1, "realloc");
436 bzero(&list, sizeof(list));
437 list.alloc_len = len;
438 list.status = CTL_LUN_LIST_NONE;
441 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
442 log_warn("error issuing CTL_LUN_LIST ioctl");
447 if (list.status == CTL_LUN_LIST_ERROR) {
448 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
454 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
459 parser = XML_ParserCreate(NULL);
460 if (parser == NULL) {
461 log_warnx("unable to create XML parser");
466 XML_SetUserData(parser, &devlist);
467 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
468 XML_SetCharacterDataHandler(parser, cctl_char_handler);
470 retval = XML_Parse(parser, str, strlen(str), 1);
471 XML_ParserFree(parser);
474 log_warnx("XML_Parse failed");
481 str = realloc(str, len);
483 log_err(1, "realloc");
485 bzero(&list, sizeof(list));
486 list.alloc_len = len;
487 list.status = CTL_LUN_LIST_NONE;
490 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
491 log_warn("error issuing CTL_PORT_LIST ioctl");
496 if (list.status == CTL_LUN_LIST_ERROR) {
497 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
503 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
508 parser = XML_ParserCreate(NULL);
509 if (parser == NULL) {
510 log_warnx("unable to create XML parser");
515 XML_SetUserData(parser, &devlist);
516 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
517 XML_SetCharacterDataHandler(parser, cctl_char_handler);
519 retval = XML_Parse(parser, str, strlen(str), 1);
520 XML_ParserFree(parser);
523 log_warnx("XML_Parse failed");
530 STAILQ_FOREACH(port, &devlist.port_list, links) {
531 if (strcmp(port->port_frontend, "ha") == 0)
534 if (port->pp == 0 && port->vp == 0) {
535 name = checked_strdup(port->port_name);
536 } else if (port->vp == 0) {
537 retval = asprintf(&name, "%s/%d",
538 port->port_name, port->pp);
540 log_err(1, "asprintf");
542 retval = asprintf(&name, "%s/%d/%d",
543 port->port_name, port->pp, port->vp);
545 log_err(1, "asprintf");
548 if (port->cfiscsi_target == NULL) {
549 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
550 port->port_id, name);
551 pp = pport_find(conf, name);
554 log_debugx("found new kernel port %u \"%s\"",
555 port->port_id, name);
557 pp = pport_new(conf, name, port->port_id);
559 log_warnx("pport_new failed");
565 if (port->cfiscsi_state != 1) {
566 log_debugx("CTL port %ju is not active (%d); ignoring",
567 (uintmax_t)port->port_id, port->cfiscsi_state);
571 targ = target_find(conf, port->cfiscsi_target);
574 log_debugx("found new kernel target %s for CTL port %ld",
575 port->cfiscsi_target, port->port_id);
577 targ = target_new(conf, port->cfiscsi_target);
579 log_warnx("target_new failed");
584 if (port->ctld_portal_group_name == NULL)
586 pg = portal_group_find(conf, port->ctld_portal_group_name);
589 log_debugx("found new kernel portal group %s for CTL port %ld",
590 port->ctld_portal_group_name, port->port_id);
592 pg = portal_group_new(conf, port->ctld_portal_group_name);
594 log_warnx("portal_group_new failed");
598 pg->pg_tag = port->cfiscsi_portal_group_tag;
599 cp = port_new(conf, targ, pg);
601 log_warnx("port_new failed");
604 cp->p_ctl_port = port->port_id;
608 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
609 struct cctl_lun_nv *nv;
611 if (lun->ctld_name == NULL) {
612 log_debugx("CTL lun %ju wasn't managed by ctld; "
613 "ignoring", (uintmax_t)lun->lun_id);
617 cl = lun_find(conf, lun->ctld_name);
619 log_warnx("found CTL lun %ju \"%s\", "
620 "also backed by CTL lun %d; ignoring",
621 (uintmax_t)lun->lun_id, lun->ctld_name,
626 log_debugx("found CTL lun %ju \"%s\"",
627 (uintmax_t)lun->lun_id, lun->ctld_name);
629 cl = lun_new(conf, lun->ctld_name);
631 log_warnx("lun_new failed");
634 lun_set_backend(cl, lun->backend_type);
635 lun_set_device_type(cl, lun->device_type);
636 lun_set_blocksize(cl, lun->blocksize);
637 lun_set_device_id(cl, lun->device_id);
638 lun_set_serial(cl, lun->serial_number);
639 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
640 lun_set_ctl_lun(cl, lun->lun_id);
642 STAILQ_FOREACH(nv, &lun->attr_list, links) {
643 if (strcmp(nv->name, "file") == 0 ||
644 strcmp(nv->name, "dev") == 0) {
645 lun_set_path(cl, nv->value);
648 o = option_new(&cl->l_options, nv->name, nv->value);
650 log_warnx("unable to add CTL lun option %s "
651 "for CTL lun %ju \"%s\"",
652 nv->name, (uintmax_t) lun->lun_id,
661 kernel_lun_add(struct lun *lun)
664 struct ctl_lun_req req;
667 bzero(&req, sizeof(req));
669 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
670 req.reqtype = CTL_LUNREQ_CREATE;
672 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
674 if (lun->l_size != 0)
675 req.reqdata.create.lun_size_bytes = lun->l_size;
677 if (lun->l_ctl_lun >= 0) {
678 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
679 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
682 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
683 req.reqdata.create.device_type = lun->l_device_type;
685 if (lun->l_serial != NULL) {
686 strncpy(req.reqdata.create.serial_num, lun->l_serial,
687 sizeof(req.reqdata.create.serial_num));
688 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
691 if (lun->l_device_id != NULL) {
692 strncpy(req.reqdata.create.device_id, lun->l_device_id,
693 sizeof(req.reqdata.create.device_id));
694 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
697 if (lun->l_path != NULL) {
698 o = option_find(&lun->l_options, "file");
700 option_set(o, lun->l_path);
702 o = option_new(&lun->l_options, "file", lun->l_path);
707 o = option_find(&lun->l_options, "ctld_name");
709 option_set(o, lun->l_name);
711 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
715 o = option_find(&lun->l_options, "scsiname");
716 if (o == NULL && lun->l_scsiname != NULL) {
717 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
721 if (!TAILQ_EMPTY(&lun->l_options)) {
722 req.args_nvl = nvlist_create(0);
723 if (req.args_nvl == NULL) {
724 log_warn("error allocating nvlist");
728 TAILQ_FOREACH(o, &lun->l_options, o_next)
729 nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
731 req.args = nvlist_pack(req.args_nvl, &req.args_len);
732 if (req.args == NULL) {
733 log_warn("error packing nvlist");
738 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
739 nvlist_destroy(req.args_nvl);
742 log_warn("error issuing CTL_LUN_REQ ioctl");
746 switch (req.status) {
748 log_warnx("LUN creation error: %s", req.error_str);
750 case CTL_LUN_WARNING:
751 log_warnx("LUN creation warning: %s", req.error_str);
756 log_warnx("unknown LUN creation status: %d",
761 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
766 kernel_lun_modify(struct lun *lun)
769 struct ctl_lun_req req;
772 bzero(&req, sizeof(req));
774 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
775 req.reqtype = CTL_LUNREQ_MODIFY;
777 req.reqdata.modify.lun_id = lun->l_ctl_lun;
778 req.reqdata.modify.lun_size_bytes = lun->l_size;
780 if (lun->l_path != NULL) {
781 o = option_find(&lun->l_options, "file");
783 option_set(o, lun->l_path);
785 o = option_new(&lun->l_options, "file", lun->l_path);
790 o = option_find(&lun->l_options, "ctld_name");
792 option_set(o, lun->l_name);
794 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
798 o = option_find(&lun->l_options, "scsiname");
799 if (o == NULL && lun->l_scsiname != NULL) {
800 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
804 if (!TAILQ_EMPTY(&lun->l_options)) {
805 req.args_nvl = nvlist_create(0);
806 if (req.args_nvl == NULL) {
807 log_warn("error allocating nvlist");
811 TAILQ_FOREACH(o, &lun->l_options, o_next)
812 nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
814 req.args = nvlist_pack(req.args_nvl, &req.args_len);
815 if (req.args == NULL) {
816 log_warn("error packing nvlist");
821 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
822 nvlist_destroy(req.args_nvl);
825 log_warn("error issuing CTL_LUN_REQ ioctl");
829 switch (req.status) {
831 log_warnx("LUN modification error: %s", req.error_str);
833 case CTL_LUN_WARNING:
834 log_warnx("LUN modification warning: %s", req.error_str);
839 log_warnx("unknown LUN modification status: %d",
848 kernel_lun_remove(struct lun *lun)
850 struct ctl_lun_req req;
852 bzero(&req, sizeof(req));
854 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
855 req.reqtype = CTL_LUNREQ_RM;
857 req.reqdata.rm.lun_id = lun->l_ctl_lun;
859 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
860 log_warn("error issuing CTL_LUN_REQ ioctl");
864 switch (req.status) {
866 log_warnx("LUN removal error: %s", req.error_str);
868 case CTL_LUN_WARNING:
869 log_warnx("LUN removal warning: %s", req.error_str);
874 log_warnx("unknown LUN removal status: %d", req.status);
882 kernel_handoff(struct connection *conn)
884 struct ctl_iscsi req;
886 bzero(&req, sizeof(req));
888 req.type = CTL_ISCSI_HANDOFF;
889 strlcpy(req.data.handoff.initiator_name,
890 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
891 strlcpy(req.data.handoff.initiator_addr,
892 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
893 if (conn->conn_initiator_alias != NULL) {
894 strlcpy(req.data.handoff.initiator_alias,
895 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
897 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
898 sizeof(req.data.handoff.initiator_isid));
899 strlcpy(req.data.handoff.target_name,
900 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
901 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
902 strlcpy(req.data.handoff.offload,
903 conn->conn_portal->p_portal_group->pg_offload,
904 sizeof(req.data.handoff.offload));
906 #ifdef ICL_KERNEL_PROXY
908 req.data.handoff.connection_id = conn->conn_socket;
910 req.data.handoff.socket = conn->conn_socket;
912 req.data.handoff.socket = conn->conn_socket;
914 req.data.handoff.portal_group_tag =
915 conn->conn_portal->p_portal_group->pg_tag;
916 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
917 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
918 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
919 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
920 req.data.handoff.cmdsn = conn->conn_cmdsn;
921 req.data.handoff.statsn = conn->conn_statsn;
922 req.data.handoff.max_recv_data_segment_length =
923 conn->conn_max_recv_data_segment_length;
924 req.data.handoff.max_send_data_segment_length =
925 conn->conn_max_send_data_segment_length;
926 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
927 req.data.handoff.first_burst_length = conn->conn_first_burst_length;
928 req.data.handoff.immediate_data = conn->conn_immediate_data;
930 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
931 log_err(1, "error issuing CTL_ISCSI ioctl; "
932 "dropping connection");
935 if (req.status != CTL_ISCSI_OK) {
936 log_errx(1, "error returned from CTL iSCSI handoff request: "
937 "%s; dropping connection", req.error_str);
942 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
943 int *max_burst_length, int *first_burst_length)
945 struct ctl_iscsi req;
946 struct ctl_iscsi_limits_params *cilp;
948 bzero(&req, sizeof(req));
950 req.type = CTL_ISCSI_LIMITS;
951 cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
952 if (offload != NULL) {
953 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
956 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
957 log_err(1, "error issuing CTL_ISCSI ioctl; "
958 "dropping connection");
961 if (req.status != CTL_ISCSI_OK) {
962 log_errx(1, "error returned from CTL iSCSI limits request: "
963 "%s; dropping connection", req.error_str);
966 if (cilp->max_recv_data_segment_length != 0) {
967 *max_recv_dsl = cilp->max_recv_data_segment_length;
968 *max_send_dsl = cilp->max_recv_data_segment_length;
970 if (cilp->max_send_data_segment_length != 0)
971 *max_send_dsl = cilp->max_send_data_segment_length;
972 if (cilp->max_burst_length != 0)
973 *max_burst_length = cilp->max_burst_length;
974 if (cilp->first_burst_length != 0)
975 *first_burst_length = cilp->first_burst_length;
976 if (*max_burst_length < *first_burst_length)
977 *first_burst_length = *max_burst_length;
979 if (offload != NULL) {
980 log_debugx("Kernel limits for offload \"%s\" are "
981 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
982 "MaxBurstLength=%d, FirstBurstLength=%d",
983 offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
984 *first_burst_length);
986 log_debugx("Kernel limits are "
987 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
988 "MaxBurstLength=%d, FirstBurstLength=%d",
989 *max_recv_dsl, *max_send_dsl, *max_burst_length,
990 *first_burst_length);
995 kernel_port_add(struct port *port)
998 struct ctl_port_entry entry;
1000 struct ctl_lun_map lm;
1001 struct target *targ = port->p_target;
1002 struct portal_group *pg = port->p_portal_group;
1003 char result_buf[NVLIST_BUFSIZE];
1006 /* Create iSCSI port. */
1007 if (port->p_portal_group || port->p_ioctl_port) {
1008 bzero(&req, sizeof(req));
1009 req.reqtype = CTL_REQ_CREATE;
1011 if (port->p_portal_group) {
1012 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1013 req.args_nvl = nvlist_create(0);
1014 nvlist_add_string(req.args_nvl, "cfiscsi_target",
1016 nvlist_add_string(req.args_nvl,
1017 "ctld_portal_group_name", pg->pg_name);
1018 nvlist_add_stringf(req.args_nvl,
1019 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1021 if (targ->t_alias) {
1022 nvlist_add_string(req.args_nvl,
1023 "cfiscsi_target_alias", targ->t_alias);
1026 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1027 nvlist_add_string(req.args_nvl, o->o_name,
1031 if (port->p_ioctl_port) {
1032 strlcpy(req.driver, "ioctl", sizeof(req.driver));
1033 req.args_nvl = nvlist_create(0);
1034 nvlist_add_stringf(req.args_nvl, "pp", "%d",
1036 nvlist_add_stringf(req.args_nvl, "vp", "%d",
1040 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1041 if (req.args == NULL) {
1042 log_warn("error packing nvlist");
1046 req.result = result_buf;
1047 req.result_len = sizeof(result_buf);
1048 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1049 nvlist_destroy(req.args_nvl);
1052 log_warn("error issuing CTL_PORT_REQ ioctl");
1055 if (req.status == CTL_LUN_ERROR) {
1056 log_warnx("error returned from port creation request: %s",
1060 if (req.status != CTL_LUN_OK) {
1061 log_warnx("unknown port creation request status %d",
1066 req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0);
1067 if (req.result_nvl == NULL) {
1068 log_warnx("error unpacking result nvlist");
1072 port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id");
1073 nvlist_destroy(req.result_nvl);
1074 } else if (port->p_pport) {
1075 port->p_ctl_port = port->p_pport->pp_ctl_port;
1077 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1078 strlen(targ->t_name) == 20) {
1079 bzero(&entry, sizeof(entry));
1080 entry.port_type = CTL_PORT_NONE;
1081 entry.targ_port = port->p_ctl_port;
1082 entry.flags |= CTL_PORT_WWNN_VALID;
1083 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1084 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1085 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1089 /* Explicitly enable mapping to block any access except allowed. */
1090 lm.port = port->p_ctl_port;
1091 lm.plun = UINT32_MAX;
1093 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1095 log_warn("CTL_LUN_MAP ioctl failed");
1097 /* Map configured LUNs */
1098 for (i = 0; i < MAX_LUNS; i++) {
1099 if (targ->t_luns[i] == NULL)
1101 lm.port = port->p_ctl_port;
1103 lm.lun = targ->t_luns[i]->l_ctl_lun;
1104 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1106 log_warn("CTL_LUN_MAP ioctl failed");
1110 bzero(&entry, sizeof(entry));
1111 entry.targ_port = port->p_ctl_port;
1112 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1114 log_warn("CTL_ENABLE_PORT ioctl failed");
1122 kernel_port_update(struct port *port, struct port *oport)
1124 struct ctl_lun_map lm;
1125 struct target *targ = port->p_target;
1126 struct target *otarg = oport->p_target;
1130 /* Map configured LUNs and unmap others */
1131 for (i = 0; i < MAX_LUNS; i++) {
1132 lm.port = port->p_ctl_port;
1134 if (targ->t_luns[i] == NULL)
1135 lm.lun = UINT32_MAX;
1137 lm.lun = targ->t_luns[i]->l_ctl_lun;
1138 if (otarg->t_luns[i] == NULL)
1141 olun = otarg->t_luns[i]->l_ctl_lun;
1144 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1146 log_warn("CTL_LUN_MAP ioctl failed");
1152 kernel_port_remove(struct port *port)
1154 struct ctl_port_entry entry;
1155 struct ctl_lun_map lm;
1157 struct target *targ = port->p_target;
1158 struct portal_group *pg = port->p_portal_group;
1162 bzero(&entry, sizeof(entry));
1163 entry.targ_port = port->p_ctl_port;
1164 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1166 log_warn("CTL_DISABLE_PORT ioctl failed");
1170 /* Remove iSCSI or ioctl port. */
1171 if (port->p_portal_group || port->p_ioctl_port) {
1172 bzero(&req, sizeof(req));
1173 strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi",
1174 sizeof(req.driver));
1175 req.reqtype = CTL_REQ_REMOVE;
1176 req.args_nvl = nvlist_create(0);
1177 if (req.args_nvl == NULL)
1178 log_err(1, "nvlist_create");
1180 if (port->p_ioctl_port)
1181 nvlist_add_stringf(req.args_nvl, "port_id", "%d",
1184 nvlist_add_string(req.args_nvl, "cfiscsi_target",
1186 nvlist_add_stringf(req.args_nvl,
1187 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1190 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1191 if (req.args == NULL) {
1192 log_warn("error packing nvlist");
1196 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1197 nvlist_destroy(req.args_nvl);
1200 log_warn("error issuing CTL_PORT_REQ ioctl");
1203 if (req.status == CTL_LUN_ERROR) {
1204 log_warnx("error returned from port removal request: %s",
1208 if (req.status != CTL_LUN_OK) {
1209 log_warnx("unknown port removal request status %d",
1214 /* Disable LUN mapping. */
1215 lm.port = port->p_ctl_port;
1216 lm.plun = UINT32_MAX;
1217 lm.lun = UINT32_MAX;
1218 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1220 log_warn("CTL_LUN_MAP ioctl failed");
1225 #ifdef ICL_KERNEL_PROXY
1227 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1229 struct ctl_iscsi req;
1231 bzero(&req, sizeof(req));
1233 req.type = CTL_ISCSI_LISTEN;
1234 req.data.listen.iser = iser;
1235 req.data.listen.domain = ai->ai_family;
1236 req.data.listen.socktype = ai->ai_socktype;
1237 req.data.listen.protocol = ai->ai_protocol;
1238 req.data.listen.addr = ai->ai_addr;
1239 req.data.listen.addrlen = ai->ai_addrlen;
1240 req.data.listen.portal_id = portal_id;
1242 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1243 log_err(1, "error issuing CTL_ISCSI ioctl");
1245 if (req.status != CTL_ISCSI_OK) {
1246 log_errx(1, "error returned from CTL iSCSI listen: %s",
1252 kernel_accept(int *connection_id, int *portal_id,
1253 struct sockaddr *client_sa, socklen_t *client_salen)
1255 struct ctl_iscsi req;
1256 struct sockaddr_storage ss;
1258 bzero(&req, sizeof(req));
1260 req.type = CTL_ISCSI_ACCEPT;
1261 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1263 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1264 log_err(1, "error issuing CTL_ISCSI ioctl");
1266 if (req.status != CTL_ISCSI_OK) {
1267 log_errx(1, "error returned from CTL iSCSI accept: %s",
1271 *connection_id = req.data.accept.connection_id;
1272 *portal_id = req.data.accept.portal_id;
1273 *client_salen = req.data.accept.initiator_addrlen;
1274 memcpy(client_sa, &ss, *client_salen);
1278 kernel_send(struct pdu *pdu)
1280 struct ctl_iscsi req;
1282 bzero(&req, sizeof(req));
1284 req.type = CTL_ISCSI_SEND;
1285 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1286 req.data.send.bhs = pdu->pdu_bhs;
1287 req.data.send.data_segment_len = pdu->pdu_data_len;
1288 req.data.send.data_segment = pdu->pdu_data;
1290 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1291 log_err(1, "error issuing CTL_ISCSI ioctl; "
1292 "dropping connection");
1295 if (req.status != CTL_ISCSI_OK) {
1296 log_errx(1, "error returned from CTL iSCSI send: "
1297 "%s; dropping connection", req.error_str);
1302 kernel_receive(struct pdu *pdu)
1304 struct connection *conn;
1305 struct ctl_iscsi req;
1307 conn = pdu->pdu_connection;
1308 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1309 if (pdu->pdu_data == NULL)
1310 log_err(1, "malloc");
1312 bzero(&req, sizeof(req));
1314 req.type = CTL_ISCSI_RECEIVE;
1315 req.data.receive.connection_id = conn->conn_socket;
1316 req.data.receive.bhs = pdu->pdu_bhs;
1317 req.data.receive.data_segment_len =
1318 conn->conn_max_recv_data_segment_length;
1319 req.data.receive.data_segment = pdu->pdu_data;
1321 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1322 log_err(1, "error issuing CTL_ISCSI ioctl; "
1323 "dropping connection");
1326 if (req.status != CTL_ISCSI_OK) {
1327 log_errx(1, "error returned from CTL iSCSI receive: "
1328 "%s; dropping connection", req.error_str);
1333 #endif /* ICL_KERNEL_PROXY */
1336 * XXX: I CANT INTO LATIN
1339 kernel_capsicate(void)
1341 cap_rights_t rights;
1342 const unsigned long cmds[] = { CTL_ISCSI };
1344 cap_rights_init(&rights, CAP_IOCTL);
1345 if (caph_rights_limit(ctl_fd, &rights) < 0)
1346 log_err(1, "cap_rights_limit");
1348 if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0)
1349 log_err(1, "cap_ioctls_limit");
1351 if (caph_enter() < 0)
1352 log_err(1, "cap_enter");
1354 if (cap_sandboxed())
1355 log_debugx("Capsicum capability mode enabled");
1357 log_warnx("Capsicum capability mode not supported");