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 (!TAILQ_EMPTY(&lun->l_options)) {
781 req.args_nvl = nvlist_create(0);
782 if (req.args_nvl == NULL) {
783 log_warn("error allocating nvlist");
787 TAILQ_FOREACH(o, &lun->l_options, o_next)
788 nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
790 req.args = nvlist_pack(req.args_nvl, &req.args_len);
791 if (req.args == NULL) {
792 log_warn("error packing nvlist");
797 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
798 nvlist_destroy(req.args_nvl);
801 log_warn("error issuing CTL_LUN_REQ ioctl");
805 switch (req.status) {
807 log_warnx("LUN modification error: %s", req.error_str);
809 case CTL_LUN_WARNING:
810 log_warnx("LUN modification warning: %s", req.error_str);
815 log_warnx("unknown LUN modification status: %d",
824 kernel_lun_remove(struct lun *lun)
826 struct ctl_lun_req req;
828 bzero(&req, sizeof(req));
830 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
831 req.reqtype = CTL_LUNREQ_RM;
833 req.reqdata.rm.lun_id = lun->l_ctl_lun;
835 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
836 log_warn("error issuing CTL_LUN_REQ ioctl");
840 switch (req.status) {
842 log_warnx("LUN removal error: %s", req.error_str);
844 case CTL_LUN_WARNING:
845 log_warnx("LUN removal warning: %s", req.error_str);
850 log_warnx("unknown LUN removal status: %d", req.status);
858 kernel_handoff(struct connection *conn)
860 struct ctl_iscsi req;
862 bzero(&req, sizeof(req));
864 req.type = CTL_ISCSI_HANDOFF;
865 strlcpy(req.data.handoff.initiator_name,
866 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
867 strlcpy(req.data.handoff.initiator_addr,
868 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
869 if (conn->conn_initiator_alias != NULL) {
870 strlcpy(req.data.handoff.initiator_alias,
871 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
873 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
874 sizeof(req.data.handoff.initiator_isid));
875 strlcpy(req.data.handoff.target_name,
876 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
877 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
878 strlcpy(req.data.handoff.offload,
879 conn->conn_portal->p_portal_group->pg_offload,
880 sizeof(req.data.handoff.offload));
882 #ifdef ICL_KERNEL_PROXY
884 req.data.handoff.connection_id = conn->conn_socket;
886 req.data.handoff.socket = conn->conn_socket;
888 req.data.handoff.socket = conn->conn_socket;
890 req.data.handoff.portal_group_tag =
891 conn->conn_portal->p_portal_group->pg_tag;
892 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
893 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
894 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
895 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
896 req.data.handoff.cmdsn = conn->conn_cmdsn;
897 req.data.handoff.statsn = conn->conn_statsn;
898 req.data.handoff.max_recv_data_segment_length =
899 conn->conn_max_recv_data_segment_length;
900 req.data.handoff.max_send_data_segment_length =
901 conn->conn_max_send_data_segment_length;
902 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
903 req.data.handoff.first_burst_length = conn->conn_first_burst_length;
904 req.data.handoff.immediate_data = conn->conn_immediate_data;
906 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
907 log_err(1, "error issuing CTL_ISCSI ioctl; "
908 "dropping connection");
911 if (req.status != CTL_ISCSI_OK) {
912 log_errx(1, "error returned from CTL iSCSI handoff request: "
913 "%s; dropping connection", req.error_str);
918 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
919 int *max_burst_length, int *first_burst_length)
921 struct ctl_iscsi req;
922 struct ctl_iscsi_limits_params *cilp;
924 bzero(&req, sizeof(req));
926 req.type = CTL_ISCSI_LIMITS;
927 cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
928 if (offload != NULL) {
929 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
932 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
933 log_err(1, "error issuing CTL_ISCSI ioctl; "
934 "dropping connection");
937 if (req.status != CTL_ISCSI_OK) {
938 log_errx(1, "error returned from CTL iSCSI limits request: "
939 "%s; dropping connection", req.error_str);
942 if (cilp->max_recv_data_segment_length != 0) {
943 *max_recv_dsl = cilp->max_recv_data_segment_length;
944 *max_send_dsl = cilp->max_recv_data_segment_length;
946 if (cilp->max_send_data_segment_length != 0)
947 *max_send_dsl = cilp->max_send_data_segment_length;
948 if (cilp->max_burst_length != 0)
949 *max_burst_length = cilp->max_burst_length;
950 if (cilp->first_burst_length != 0)
951 *first_burst_length = cilp->first_burst_length;
952 if (*max_burst_length < *first_burst_length)
953 *first_burst_length = *max_burst_length;
955 if (offload != NULL) {
956 log_debugx("Kernel limits for offload \"%s\" are "
957 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
958 "MaxBurstLength=%d, FirstBurstLength=%d",
959 offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
960 *first_burst_length);
962 log_debugx("Kernel limits are "
963 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
964 "MaxBurstLength=%d, FirstBurstLength=%d",
965 *max_recv_dsl, *max_send_dsl, *max_burst_length,
966 *first_burst_length);
971 kernel_port_add(struct port *port)
974 struct ctl_port_entry entry;
976 struct ctl_lun_map lm;
977 struct target *targ = port->p_target;
978 struct portal_group *pg = port->p_portal_group;
979 char result_buf[NVLIST_BUFSIZE];
982 /* Create iSCSI port. */
983 if (port->p_portal_group || port->p_ioctl_port) {
984 bzero(&req, sizeof(req));
985 req.reqtype = CTL_REQ_CREATE;
987 if (port->p_portal_group) {
988 strlcpy(req.driver, "iscsi", sizeof(req.driver));
989 req.args_nvl = nvlist_create(0);
990 nvlist_add_string(req.args_nvl, "cfiscsi_target",
992 nvlist_add_string(req.args_nvl,
993 "ctld_portal_group_name", pg->pg_name);
994 nvlist_add_stringf(req.args_nvl,
995 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
998 nvlist_add_string(req.args_nvl,
999 "cfiscsi_target_alias", targ->t_alias);
1002 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1003 nvlist_add_string(req.args_nvl, o->o_name,
1007 if (port->p_ioctl_port) {
1008 strlcpy(req.driver, "ioctl", sizeof(req.driver));
1009 req.args_nvl = nvlist_create(0);
1010 nvlist_add_stringf(req.args_nvl, "pp", "%d",
1012 nvlist_add_stringf(req.args_nvl, "vp", "%d",
1016 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1017 if (req.args == NULL) {
1018 log_warn("error packing nvlist");
1022 req.result = result_buf;
1023 req.result_len = sizeof(result_buf);
1024 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1025 nvlist_destroy(req.args_nvl);
1028 log_warn("error issuing CTL_PORT_REQ ioctl");
1031 if (req.status == CTL_LUN_ERROR) {
1032 log_warnx("error returned from port creation request: %s",
1036 if (req.status != CTL_LUN_OK) {
1037 log_warnx("unknown port creation request status %d",
1042 req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0);
1043 if (req.result_nvl == NULL) {
1044 log_warnx("error unpacking result nvlist");
1048 port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id");
1049 nvlist_destroy(req.result_nvl);
1050 } else if (port->p_pport) {
1051 port->p_ctl_port = port->p_pport->pp_ctl_port;
1053 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1054 strlen(targ->t_name) == 20) {
1055 bzero(&entry, sizeof(entry));
1056 entry.port_type = CTL_PORT_NONE;
1057 entry.targ_port = port->p_ctl_port;
1058 entry.flags |= CTL_PORT_WWNN_VALID;
1059 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1060 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1061 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1065 /* Explicitly enable mapping to block any access except allowed. */
1066 lm.port = port->p_ctl_port;
1067 lm.plun = UINT32_MAX;
1069 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1071 log_warn("CTL_LUN_MAP ioctl failed");
1073 /* Map configured LUNs */
1074 for (i = 0; i < MAX_LUNS; i++) {
1075 if (targ->t_luns[i] == NULL)
1077 lm.port = port->p_ctl_port;
1079 lm.lun = targ->t_luns[i]->l_ctl_lun;
1080 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1082 log_warn("CTL_LUN_MAP ioctl failed");
1086 bzero(&entry, sizeof(entry));
1087 entry.targ_port = port->p_ctl_port;
1088 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1090 log_warn("CTL_ENABLE_PORT ioctl failed");
1098 kernel_port_update(struct port *port, struct port *oport)
1100 struct ctl_lun_map lm;
1101 struct target *targ = port->p_target;
1102 struct target *otarg = oport->p_target;
1106 /* Map configured LUNs and unmap others */
1107 for (i = 0; i < MAX_LUNS; i++) {
1108 lm.port = port->p_ctl_port;
1110 if (targ->t_luns[i] == NULL)
1111 lm.lun = UINT32_MAX;
1113 lm.lun = targ->t_luns[i]->l_ctl_lun;
1114 if (otarg->t_luns[i] == NULL)
1117 olun = otarg->t_luns[i]->l_ctl_lun;
1120 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1122 log_warn("CTL_LUN_MAP ioctl failed");
1128 kernel_port_remove(struct port *port)
1130 struct ctl_port_entry entry;
1131 struct ctl_lun_map lm;
1133 struct target *targ = port->p_target;
1134 struct portal_group *pg = port->p_portal_group;
1138 bzero(&entry, sizeof(entry));
1139 entry.targ_port = port->p_ctl_port;
1140 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1142 log_warn("CTL_DISABLE_PORT ioctl failed");
1146 /* Remove iSCSI or ioctl port. */
1147 if (port->p_portal_group || port->p_ioctl_port) {
1148 bzero(&req, sizeof(req));
1149 strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi",
1150 sizeof(req.driver));
1151 req.reqtype = CTL_REQ_REMOVE;
1152 req.args_nvl = nvlist_create(0);
1153 if (req.args_nvl == NULL)
1154 log_err(1, "nvlist_create");
1156 if (port->p_ioctl_port)
1157 nvlist_add_stringf(req.args_nvl, "port_id", "%d",
1160 nvlist_add_string(req.args_nvl, "cfiscsi_target",
1162 nvlist_add_stringf(req.args_nvl,
1163 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1166 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1167 if (req.args == NULL) {
1168 log_warn("error packing nvlist");
1172 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1173 nvlist_destroy(req.args_nvl);
1176 log_warn("error issuing CTL_PORT_REQ ioctl");
1179 if (req.status == CTL_LUN_ERROR) {
1180 log_warnx("error returned from port removal request: %s",
1184 if (req.status != CTL_LUN_OK) {
1185 log_warnx("unknown port removal request status %d",
1190 /* Disable LUN mapping. */
1191 lm.port = port->p_ctl_port;
1192 lm.plun = UINT32_MAX;
1193 lm.lun = UINT32_MAX;
1194 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1196 log_warn("CTL_LUN_MAP ioctl failed");
1201 #ifdef ICL_KERNEL_PROXY
1203 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1205 struct ctl_iscsi req;
1207 bzero(&req, sizeof(req));
1209 req.type = CTL_ISCSI_LISTEN;
1210 req.data.listen.iser = iser;
1211 req.data.listen.domain = ai->ai_family;
1212 req.data.listen.socktype = ai->ai_socktype;
1213 req.data.listen.protocol = ai->ai_protocol;
1214 req.data.listen.addr = ai->ai_addr;
1215 req.data.listen.addrlen = ai->ai_addrlen;
1216 req.data.listen.portal_id = portal_id;
1218 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1219 log_err(1, "error issuing CTL_ISCSI ioctl");
1221 if (req.status != CTL_ISCSI_OK) {
1222 log_errx(1, "error returned from CTL iSCSI listen: %s",
1228 kernel_accept(int *connection_id, int *portal_id,
1229 struct sockaddr *client_sa, socklen_t *client_salen)
1231 struct ctl_iscsi req;
1232 struct sockaddr_storage ss;
1234 bzero(&req, sizeof(req));
1236 req.type = CTL_ISCSI_ACCEPT;
1237 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1239 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1240 log_err(1, "error issuing CTL_ISCSI ioctl");
1242 if (req.status != CTL_ISCSI_OK) {
1243 log_errx(1, "error returned from CTL iSCSI accept: %s",
1247 *connection_id = req.data.accept.connection_id;
1248 *portal_id = req.data.accept.portal_id;
1249 *client_salen = req.data.accept.initiator_addrlen;
1250 memcpy(client_sa, &ss, *client_salen);
1254 kernel_send(struct pdu *pdu)
1256 struct ctl_iscsi req;
1258 bzero(&req, sizeof(req));
1260 req.type = CTL_ISCSI_SEND;
1261 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1262 req.data.send.bhs = pdu->pdu_bhs;
1263 req.data.send.data_segment_len = pdu->pdu_data_len;
1264 req.data.send.data_segment = pdu->pdu_data;
1266 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1267 log_err(1, "error issuing CTL_ISCSI ioctl; "
1268 "dropping connection");
1271 if (req.status != CTL_ISCSI_OK) {
1272 log_errx(1, "error returned from CTL iSCSI send: "
1273 "%s; dropping connection", req.error_str);
1278 kernel_receive(struct pdu *pdu)
1280 struct connection *conn;
1281 struct ctl_iscsi req;
1283 conn = pdu->pdu_connection;
1284 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1285 if (pdu->pdu_data == NULL)
1286 log_err(1, "malloc");
1288 bzero(&req, sizeof(req));
1290 req.type = CTL_ISCSI_RECEIVE;
1291 req.data.receive.connection_id = conn->conn_socket;
1292 req.data.receive.bhs = pdu->pdu_bhs;
1293 req.data.receive.data_segment_len =
1294 conn->conn_max_recv_data_segment_length;
1295 req.data.receive.data_segment = pdu->pdu_data;
1297 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1298 log_err(1, "error issuing CTL_ISCSI ioctl; "
1299 "dropping connection");
1302 if (req.status != CTL_ISCSI_OK) {
1303 log_errx(1, "error returned from CTL iSCSI receive: "
1304 "%s; dropping connection", req.error_str);
1309 #endif /* ICL_KERNEL_PROXY */
1312 * XXX: I CANT INTO LATIN
1315 kernel_capsicate(void)
1317 cap_rights_t rights;
1318 const unsigned long cmds[] = { CTL_ISCSI };
1320 cap_rights_init(&rights, CAP_IOCTL);
1321 if (caph_rights_limit(ctl_fd, &rights) < 0)
1322 log_err(1, "cap_rights_limit");
1324 if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0)
1325 log_err(1, "cap_ioctls_limit");
1327 if (caph_enter() < 0)
1328 log_err(1, "cap_enter");
1330 if (cap_sandboxed())
1331 log_debugx("Capsicum capability mode enabled");
1333 log_warnx("Capsicum capability mode not supported");