2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/callout.h>
43 #include <sys/ioctl.h>
44 #include <sys/linker.h>
45 #include <sys/module.h>
46 #include <sys/queue.h>
52 #include <capsicum_helpers.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/ctl/ctl.h>
64 #include <cam/ctl/ctl_io.h>
65 #include <cam/ctl/ctl_backend.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_util.h>
68 #include <cam/ctl/ctl_scsi_all.h>
72 #ifdef ICL_KERNEL_PROXY
76 #define NVLIST_BUFSIZE 1024
78 extern bool proxy_mode;
80 static int ctl_fd = 0;
85 int retval, saved_errno;
87 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
88 if (ctl_fd < 0 && errno == ENOENT) {
90 retval = kldload("ctl");
92 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
97 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
101 if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
102 log_warn("couldn't load cfiscsi");
109 * Name/value pair used for per-LUN attributes.
114 STAILQ_ENTRY(cctl_lun_nv) links;
118 * Backend LUN information.
124 uint64_t size_blocks;
129 STAILQ_HEAD(,cctl_lun_nv) attr_list;
130 STAILQ_ENTRY(cctl_lun) links;
140 char *cfiscsi_target;
141 uint16_t cfiscsi_portal_group_tag;
142 char *ctld_portal_group_name;
143 STAILQ_HEAD(,cctl_lun_nv) attr_list;
144 STAILQ_ENTRY(cctl_port) links;
147 struct cctl_devlist_data {
149 STAILQ_HEAD(,cctl_lun) lun_list;
150 struct cctl_lun *cur_lun;
152 STAILQ_HEAD(,cctl_port) port_list;
153 struct cctl_port *cur_port;
155 struct sbuf *cur_sb[32];
159 cctl_start_element(void *user_data, const char *name, const char **attr)
162 struct cctl_devlist_data *devlist;
163 struct cctl_lun *cur_lun;
165 devlist = (struct cctl_devlist_data *)user_data;
166 cur_lun = devlist->cur_lun;
168 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
169 sizeof(devlist->cur_sb[0])))
170 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
171 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
173 devlist->cur_sb[devlist->level] = sbuf_new_auto();
174 if (devlist->cur_sb[devlist->level] == NULL)
175 log_err(1, "%s: unable to allocate sbuf", __func__);
177 if (strcmp(name, "lun") == 0) {
179 log_errx(1, "%s: improper lun element nesting",
182 cur_lun = calloc(1, sizeof(*cur_lun));
184 log_err(1, "%s: cannot allocate %zd bytes", __func__,
188 devlist->cur_lun = cur_lun;
190 STAILQ_INIT(&cur_lun->attr_list);
191 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
193 for (i = 0; attr[i] != NULL; i += 2) {
194 if (strcmp(attr[i], "id") == 0) {
195 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
197 log_errx(1, "%s: invalid LUN attribute %s = %s",
198 __func__, attr[i], attr[i+1]);
205 cctl_end_element(void *user_data, const char *name)
207 struct cctl_devlist_data *devlist;
208 struct cctl_lun *cur_lun;
211 devlist = (struct cctl_devlist_data *)user_data;
212 cur_lun = devlist->cur_lun;
214 if ((cur_lun == NULL)
215 && (strcmp(name, "ctllunlist") != 0))
216 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
218 if (devlist->cur_sb[devlist->level] == NULL)
219 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
220 devlist->level, name);
222 sbuf_finish(devlist->cur_sb[devlist->level]);
223 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
225 if (strlen(str) == 0) {
230 sbuf_delete(devlist->cur_sb[devlist->level]);
231 devlist->cur_sb[devlist->level] = NULL;
234 if (strcmp(name, "backend_type") == 0) {
235 cur_lun->backend_type = str;
237 } else if (strcmp(name, "lun_type") == 0) {
239 log_errx(1, "%s: %s missing its argument", __func__, name);
240 cur_lun->device_type = strtoull(str, NULL, 0);
241 } else if (strcmp(name, "size") == 0) {
243 log_errx(1, "%s: %s missing its argument", __func__, name);
244 cur_lun->size_blocks = strtoull(str, NULL, 0);
245 } else if (strcmp(name, "blocksize") == 0) {
247 log_errx(1, "%s: %s missing its argument", __func__, name);
248 cur_lun->blocksize = strtoul(str, NULL, 0);
249 } else if (strcmp(name, "serial_number") == 0) {
250 cur_lun->serial_number = str;
252 } else if (strcmp(name, "device_id") == 0) {
253 cur_lun->device_id = str;
255 } else if (strcmp(name, "ctld_name") == 0) {
256 cur_lun->ctld_name = str;
258 } else if (strcmp(name, "lun") == 0) {
259 devlist->cur_lun = NULL;
260 } else if (strcmp(name, "ctllunlist") == 0) {
263 struct cctl_lun_nv *nv;
265 nv = calloc(1, sizeof(*nv));
267 log_err(1, "%s: can't allocate %zd bytes for nv pair",
268 __func__, sizeof(*nv));
270 nv->name = checked_strdup(name);
274 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
281 cctl_start_pelement(void *user_data, const char *name, const char **attr)
284 struct cctl_devlist_data *devlist;
285 struct cctl_port *cur_port;
287 devlist = (struct cctl_devlist_data *)user_data;
288 cur_port = devlist->cur_port;
290 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
291 sizeof(devlist->cur_sb[0])))
292 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
293 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
295 devlist->cur_sb[devlist->level] = sbuf_new_auto();
296 if (devlist->cur_sb[devlist->level] == NULL)
297 log_err(1, "%s: unable to allocate sbuf", __func__);
299 if (strcmp(name, "targ_port") == 0) {
300 if (cur_port != NULL)
301 log_errx(1, "%s: improper port element nesting (%s)",
304 cur_port = calloc(1, sizeof(*cur_port));
305 if (cur_port == NULL)
306 log_err(1, "%s: cannot allocate %zd bytes", __func__,
309 devlist->num_ports++;
310 devlist->cur_port = cur_port;
312 STAILQ_INIT(&cur_port->attr_list);
313 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
315 for (i = 0; attr[i] != NULL; i += 2) {
316 if (strcmp(attr[i], "id") == 0) {
317 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
319 log_errx(1, "%s: invalid LUN attribute %s = %s",
320 __func__, attr[i], attr[i+1]);
327 cctl_end_pelement(void *user_data, const char *name)
329 struct cctl_devlist_data *devlist;
330 struct cctl_port *cur_port;
333 devlist = (struct cctl_devlist_data *)user_data;
334 cur_port = devlist->cur_port;
336 if ((cur_port == NULL)
337 && (strcmp(name, "ctlportlist") != 0))
338 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
340 if (devlist->cur_sb[devlist->level] == NULL)
341 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
342 devlist->level, name);
344 sbuf_finish(devlist->cur_sb[devlist->level]);
345 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
347 if (strlen(str) == 0) {
352 sbuf_delete(devlist->cur_sb[devlist->level]);
353 devlist->cur_sb[devlist->level] = NULL;
356 if (strcmp(name, "frontend_type") == 0) {
357 cur_port->port_frontend = str;
359 } else if (strcmp(name, "port_name") == 0) {
360 cur_port->port_name = str;
362 } else if (strcmp(name, "physical_port") == 0) {
364 log_errx(1, "%s: %s missing its argument", __func__, name);
365 cur_port->pp = strtoul(str, NULL, 0);
366 } else if (strcmp(name, "virtual_port") == 0) {
368 log_errx(1, "%s: %s missing its argument", __func__, name);
369 cur_port->vp = strtoul(str, NULL, 0);
370 } else if (strcmp(name, "cfiscsi_target") == 0) {
371 cur_port->cfiscsi_target = str;
373 } else if (strcmp(name, "cfiscsi_state") == 0) {
375 log_errx(1, "%s: %s missing its argument", __func__, name);
376 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
377 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
379 log_errx(1, "%s: %s missing its argument", __func__, name);
380 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
381 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
382 cur_port->ctld_portal_group_name = str;
384 } else if (strcmp(name, "targ_port") == 0) {
385 devlist->cur_port = NULL;
386 } else if (strcmp(name, "ctlportlist") == 0) {
389 struct cctl_lun_nv *nv;
391 nv = calloc(1, sizeof(*nv));
393 log_err(1, "%s: can't allocate %zd bytes for nv pair",
394 __func__, sizeof(*nv));
396 nv->name = checked_strdup(name);
400 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
407 cctl_char_handler(void *user_data, const XML_Char *str, int len)
409 struct cctl_devlist_data *devlist;
411 devlist = (struct cctl_devlist_data *)user_data;
413 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
417 conf_new_from_kernel(void)
419 struct conf *conf = NULL;
421 struct portal_group *pg;
426 struct ctl_lun_list list;
427 struct cctl_devlist_data devlist;
428 struct cctl_lun *lun;
429 struct cctl_port *port;
434 bzero(&devlist, sizeof(devlist));
435 STAILQ_INIT(&devlist.lun_list);
436 STAILQ_INIT(&devlist.port_list);
438 log_debugx("obtaining previously configured CTL luns from the kernel");
443 str = realloc(str, len);
445 log_err(1, "realloc");
447 bzero(&list, sizeof(list));
448 list.alloc_len = len;
449 list.status = CTL_LUN_LIST_NONE;
452 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
453 log_warn("error issuing CTL_LUN_LIST ioctl");
458 if (list.status == CTL_LUN_LIST_ERROR) {
459 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
465 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
470 parser = XML_ParserCreate(NULL);
471 if (parser == NULL) {
472 log_warnx("unable to create XML parser");
477 XML_SetUserData(parser, &devlist);
478 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
479 XML_SetCharacterDataHandler(parser, cctl_char_handler);
481 retval = XML_Parse(parser, str, strlen(str), 1);
482 XML_ParserFree(parser);
485 log_warnx("XML_Parse failed");
492 str = realloc(str, len);
494 log_err(1, "realloc");
496 bzero(&list, sizeof(list));
497 list.alloc_len = len;
498 list.status = CTL_LUN_LIST_NONE;
501 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
502 log_warn("error issuing CTL_PORT_LIST ioctl");
507 if (list.status == CTL_LUN_LIST_ERROR) {
508 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
514 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
519 parser = XML_ParserCreate(NULL);
520 if (parser == NULL) {
521 log_warnx("unable to create XML parser");
526 XML_SetUserData(parser, &devlist);
527 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
528 XML_SetCharacterDataHandler(parser, cctl_char_handler);
530 retval = XML_Parse(parser, str, strlen(str), 1);
531 XML_ParserFree(parser);
534 log_warnx("XML_Parse failed");
541 STAILQ_FOREACH(port, &devlist.port_list, links) {
542 if (strcmp(port->port_frontend, "ha") == 0)
545 if (port->pp == 0 && port->vp == 0) {
546 name = checked_strdup(port->port_name);
547 } else if (port->vp == 0) {
548 retval = asprintf(&name, "%s/%d",
549 port->port_name, port->pp);
551 log_err(1, "asprintf");
553 retval = asprintf(&name, "%s/%d/%d",
554 port->port_name, port->pp, port->vp);
556 log_err(1, "asprintf");
559 if (port->cfiscsi_target == NULL) {
560 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
561 port->port_id, name);
562 pp = pport_find(conf, name);
565 log_debugx("found new kernel port %u \"%s\"",
566 port->port_id, name);
568 pp = pport_new(conf, name, port->port_id);
570 log_warnx("pport_new failed");
576 if (port->cfiscsi_state != 1) {
577 log_debugx("CTL port %ju is not active (%d); ignoring",
578 (uintmax_t)port->port_id, port->cfiscsi_state);
582 targ = target_find(conf, port->cfiscsi_target);
585 log_debugx("found new kernel target %s for CTL port %ld",
586 port->cfiscsi_target, port->port_id);
588 targ = target_new(conf, port->cfiscsi_target);
590 log_warnx("target_new failed");
595 if (port->ctld_portal_group_name == NULL)
597 pg = portal_group_find(conf, port->ctld_portal_group_name);
600 log_debugx("found new kernel portal group %s for CTL port %ld",
601 port->ctld_portal_group_name, port->port_id);
603 pg = portal_group_new(conf, port->ctld_portal_group_name);
605 log_warnx("portal_group_new failed");
609 pg->pg_tag = port->cfiscsi_portal_group_tag;
610 cp = port_new(conf, targ, pg);
612 log_warnx("port_new failed");
615 cp->p_ctl_port = port->port_id;
619 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
620 struct cctl_lun_nv *nv;
622 if (lun->ctld_name == NULL) {
623 log_debugx("CTL lun %ju wasn't managed by ctld; "
624 "ignoring", (uintmax_t)lun->lun_id);
628 cl = lun_find(conf, lun->ctld_name);
630 log_warnx("found CTL lun %ju \"%s\", "
631 "also backed by CTL lun %d; ignoring",
632 (uintmax_t)lun->lun_id, lun->ctld_name,
637 log_debugx("found CTL lun %ju \"%s\"",
638 (uintmax_t)lun->lun_id, lun->ctld_name);
640 cl = lun_new(conf, lun->ctld_name);
642 log_warnx("lun_new failed");
645 lun_set_backend(cl, lun->backend_type);
646 lun_set_device_type(cl, lun->device_type);
647 lun_set_blocksize(cl, lun->blocksize);
648 lun_set_device_id(cl, lun->device_id);
649 lun_set_serial(cl, lun->serial_number);
650 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
651 lun_set_ctl_lun(cl, lun->lun_id);
653 STAILQ_FOREACH(nv, &lun->attr_list, links) {
654 if (strcmp(nv->name, "file") == 0 ||
655 strcmp(nv->name, "dev") == 0) {
656 lun_set_path(cl, nv->value);
659 o = option_new(&cl->l_options, nv->name, nv->value);
661 log_warnx("unable to add CTL lun option %s "
662 "for CTL lun %ju \"%s\"",
663 nv->name, (uintmax_t) lun->lun_id,
672 kernel_lun_add(struct lun *lun)
675 struct ctl_lun_req req;
678 bzero(&req, sizeof(req));
680 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
681 req.reqtype = CTL_LUNREQ_CREATE;
683 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
685 if (lun->l_size != 0)
686 req.reqdata.create.lun_size_bytes = lun->l_size;
688 if (lun->l_ctl_lun >= 0) {
689 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
690 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
693 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
694 req.reqdata.create.device_type = lun->l_device_type;
696 if (lun->l_serial != NULL) {
697 strncpy(req.reqdata.create.serial_num, lun->l_serial,
698 sizeof(req.reqdata.create.serial_num));
699 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
702 if (lun->l_device_id != NULL) {
703 strncpy(req.reqdata.create.device_id, lun->l_device_id,
704 sizeof(req.reqdata.create.device_id));
705 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
708 if (lun->l_path != NULL) {
709 o = option_find(&lun->l_options, "file");
711 option_set(o, lun->l_path);
713 o = option_new(&lun->l_options, "file", lun->l_path);
718 o = option_find(&lun->l_options, "ctld_name");
720 option_set(o, lun->l_name);
722 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
726 o = option_find(&lun->l_options, "scsiname");
727 if (o == NULL && lun->l_scsiname != NULL) {
728 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
732 if (!TAILQ_EMPTY(&lun->l_options)) {
733 req.args_nvl = nvlist_create(0);
734 if (req.args_nvl == NULL) {
735 log_warn("error allocating nvlist");
739 TAILQ_FOREACH(o, &lun->l_options, o_next)
740 nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
742 req.args = nvlist_pack(req.args_nvl, &req.args_len);
743 if (req.args == NULL) {
744 log_warn("error packing nvlist");
749 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
750 nvlist_destroy(req.args_nvl);
753 log_warn("error issuing CTL_LUN_REQ ioctl");
757 switch (req.status) {
759 log_warnx("LUN creation error: %s", req.error_str);
761 case CTL_LUN_WARNING:
762 log_warnx("LUN creation warning: %s", req.error_str);
767 log_warnx("unknown LUN creation status: %d",
772 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
777 kernel_lun_modify(struct lun *lun)
780 struct ctl_lun_req req;
783 bzero(&req, sizeof(req));
785 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
786 req.reqtype = CTL_LUNREQ_MODIFY;
788 req.reqdata.modify.lun_id = lun->l_ctl_lun;
789 req.reqdata.modify.lun_size_bytes = lun->l_size;
791 if (lun->l_path != NULL) {
792 o = option_find(&lun->l_options, "file");
794 option_set(o, lun->l_path);
796 o = option_new(&lun->l_options, "file", lun->l_path);
801 o = option_find(&lun->l_options, "ctld_name");
803 option_set(o, lun->l_name);
805 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
809 o = option_find(&lun->l_options, "scsiname");
810 if (o == NULL && lun->l_scsiname != NULL) {
811 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
815 if (!TAILQ_EMPTY(&lun->l_options)) {
816 req.args_nvl = nvlist_create(0);
817 if (req.args_nvl == NULL) {
818 log_warn("error allocating nvlist");
822 TAILQ_FOREACH(o, &lun->l_options, o_next)
823 nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
825 req.args = nvlist_pack(req.args_nvl, &req.args_len);
826 if (req.args == NULL) {
827 log_warn("error packing nvlist");
832 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
833 nvlist_destroy(req.args_nvl);
836 log_warn("error issuing CTL_LUN_REQ ioctl");
840 switch (req.status) {
842 log_warnx("LUN modification error: %s", req.error_str);
844 case CTL_LUN_WARNING:
845 log_warnx("LUN modification warning: %s", req.error_str);
850 log_warnx("unknown LUN modification status: %d",
859 kernel_lun_remove(struct lun *lun)
861 struct ctl_lun_req req;
863 bzero(&req, sizeof(req));
865 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
866 req.reqtype = CTL_LUNREQ_RM;
868 req.reqdata.rm.lun_id = lun->l_ctl_lun;
870 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
871 log_warn("error issuing CTL_LUN_REQ ioctl");
875 switch (req.status) {
877 log_warnx("LUN removal error: %s", req.error_str);
879 case CTL_LUN_WARNING:
880 log_warnx("LUN removal warning: %s", req.error_str);
885 log_warnx("unknown LUN removal status: %d", req.status);
893 kernel_handoff(struct ctld_connection *conn)
895 struct ctl_iscsi req;
897 bzero(&req, sizeof(req));
899 req.type = CTL_ISCSI_HANDOFF;
900 strlcpy(req.data.handoff.initiator_name,
901 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
902 strlcpy(req.data.handoff.initiator_addr,
903 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
904 if (conn->conn_initiator_alias != NULL) {
905 strlcpy(req.data.handoff.initiator_alias,
906 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
908 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
909 sizeof(req.data.handoff.initiator_isid));
910 strlcpy(req.data.handoff.target_name,
911 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
912 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
913 strlcpy(req.data.handoff.offload,
914 conn->conn_portal->p_portal_group->pg_offload,
915 sizeof(req.data.handoff.offload));
917 #ifdef ICL_KERNEL_PROXY
919 req.data.handoff.connection_id = conn->conn.conn_socket;
921 req.data.handoff.socket = conn->conn.conn_socket;
923 req.data.handoff.socket = conn->conn.conn_socket;
925 req.data.handoff.portal_group_tag =
926 conn->conn_portal->p_portal_group->pg_tag;
927 if (conn->conn.conn_header_digest == CONN_DIGEST_CRC32C)
928 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
929 if (conn->conn.conn_data_digest == CONN_DIGEST_CRC32C)
930 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
931 req.data.handoff.cmdsn = conn->conn.conn_cmdsn;
932 req.data.handoff.statsn = conn->conn.conn_statsn;
933 req.data.handoff.max_recv_data_segment_length =
934 conn->conn.conn_max_recv_data_segment_length;
935 req.data.handoff.max_send_data_segment_length =
936 conn->conn.conn_max_send_data_segment_length;
937 req.data.handoff.max_burst_length = conn->conn.conn_max_burst_length;
938 req.data.handoff.first_burst_length =
939 conn->conn.conn_first_burst_length;
940 req.data.handoff.immediate_data = conn->conn.conn_immediate_data;
942 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
943 log_err(1, "error issuing CTL_ISCSI ioctl; "
944 "dropping connection");
947 if (req.status != CTL_ISCSI_OK) {
948 log_errx(1, "error returned from CTL iSCSI handoff request: "
949 "%s; dropping connection", req.error_str);
954 kernel_limits(const char *offload, int s, int *max_recv_dsl, int *max_send_dsl,
955 int *max_burst_length, int *first_burst_length)
957 struct ctl_iscsi req;
958 struct ctl_iscsi_limits_params *cilp;
960 bzero(&req, sizeof(req));
962 req.type = CTL_ISCSI_LIMITS;
963 cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
964 if (offload != NULL) {
965 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
969 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
970 log_err(1, "error issuing CTL_ISCSI ioctl; "
971 "dropping connection");
974 if (req.status != CTL_ISCSI_OK) {
975 log_errx(1, "error returned from CTL iSCSI limits request: "
976 "%s; dropping connection", req.error_str);
979 if (cilp->max_recv_data_segment_length != 0) {
980 *max_recv_dsl = cilp->max_recv_data_segment_length;
981 *max_send_dsl = cilp->max_recv_data_segment_length;
983 if (cilp->max_send_data_segment_length != 0)
984 *max_send_dsl = cilp->max_send_data_segment_length;
985 if (cilp->max_burst_length != 0)
986 *max_burst_length = cilp->max_burst_length;
987 if (cilp->first_burst_length != 0)
988 *first_burst_length = cilp->first_burst_length;
989 if (*max_burst_length < *first_burst_length)
990 *first_burst_length = *max_burst_length;
992 if (offload != NULL) {
993 log_debugx("Kernel limits for offload \"%s\" are "
994 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
995 "MaxBurstLength=%d, FirstBurstLength=%d",
996 offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
997 *first_burst_length);
999 log_debugx("Kernel limits are "
1000 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
1001 "MaxBurstLength=%d, FirstBurstLength=%d",
1002 *max_recv_dsl, *max_send_dsl, *max_burst_length,
1003 *first_burst_length);
1008 kernel_port_add(struct port *port)
1011 struct ctl_port_entry entry;
1013 struct ctl_lun_map lm;
1014 struct target *targ = port->p_target;
1015 struct portal_group *pg = port->p_portal_group;
1016 char result_buf[NVLIST_BUFSIZE];
1019 /* Create iSCSI port. */
1020 if (port->p_portal_group || port->p_ioctl_port) {
1021 bzero(&req, sizeof(req));
1022 req.reqtype = CTL_REQ_CREATE;
1024 if (port->p_portal_group) {
1025 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1026 req.args_nvl = nvlist_create(0);
1027 nvlist_add_string(req.args_nvl, "cfiscsi_target",
1029 nvlist_add_string(req.args_nvl,
1030 "ctld_portal_group_name", pg->pg_name);
1031 nvlist_add_stringf(req.args_nvl,
1032 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1034 if (targ->t_alias) {
1035 nvlist_add_string(req.args_nvl,
1036 "cfiscsi_target_alias", targ->t_alias);
1039 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1040 nvlist_add_string(req.args_nvl, o->o_name,
1044 if (port->p_ioctl_port) {
1045 strlcpy(req.driver, "ioctl", sizeof(req.driver));
1046 req.args_nvl = nvlist_create(0);
1047 nvlist_add_stringf(req.args_nvl, "pp", "%d",
1049 nvlist_add_stringf(req.args_nvl, "vp", "%d",
1053 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1054 if (req.args == NULL) {
1055 log_warn("error packing nvlist");
1059 req.result = result_buf;
1060 req.result_len = sizeof(result_buf);
1061 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1062 nvlist_destroy(req.args_nvl);
1065 log_warn("error issuing CTL_PORT_REQ ioctl");
1068 if (req.status == CTL_LUN_ERROR) {
1069 log_warnx("error returned from port creation request: %s",
1073 if (req.status != CTL_LUN_OK) {
1074 log_warnx("unknown port creation request status %d",
1079 req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0);
1080 if (req.result_nvl == NULL) {
1081 log_warnx("error unpacking result nvlist");
1085 port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id");
1086 nvlist_destroy(req.result_nvl);
1087 } else if (port->p_pport) {
1088 port->p_ctl_port = port->p_pport->pp_ctl_port;
1090 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1091 strlen(targ->t_name) == 20) {
1092 bzero(&entry, sizeof(entry));
1093 entry.port_type = CTL_PORT_NONE;
1094 entry.targ_port = port->p_ctl_port;
1095 entry.flags |= CTL_PORT_WWNN_VALID;
1096 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1097 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1098 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1102 /* Explicitly enable mapping to block any access except allowed. */
1103 lm.port = port->p_ctl_port;
1104 lm.plun = UINT32_MAX;
1106 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1108 log_warn("CTL_LUN_MAP ioctl failed");
1110 /* Map configured LUNs */
1111 for (i = 0; i < MAX_LUNS; i++) {
1112 if (targ->t_luns[i] == NULL)
1114 lm.port = port->p_ctl_port;
1116 lm.lun = targ->t_luns[i]->l_ctl_lun;
1117 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1119 log_warn("CTL_LUN_MAP ioctl failed");
1123 bzero(&entry, sizeof(entry));
1124 entry.targ_port = port->p_ctl_port;
1125 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1127 log_warn("CTL_ENABLE_PORT ioctl failed");
1135 kernel_port_update(struct port *port, struct port *oport)
1137 struct ctl_lun_map lm;
1138 struct target *targ = port->p_target;
1139 struct target *otarg = oport->p_target;
1143 /* Map configured LUNs and unmap others */
1144 for (i = 0; i < MAX_LUNS; i++) {
1145 lm.port = port->p_ctl_port;
1147 if (targ->t_luns[i] == NULL)
1148 lm.lun = UINT32_MAX;
1150 lm.lun = targ->t_luns[i]->l_ctl_lun;
1151 if (otarg->t_luns[i] == NULL)
1154 olun = otarg->t_luns[i]->l_ctl_lun;
1157 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1159 log_warn("CTL_LUN_MAP ioctl failed");
1165 kernel_port_remove(struct port *port)
1167 struct ctl_port_entry entry;
1168 struct ctl_lun_map lm;
1170 struct target *targ = port->p_target;
1171 struct portal_group *pg = port->p_portal_group;
1175 bzero(&entry, sizeof(entry));
1176 entry.targ_port = port->p_ctl_port;
1177 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1179 log_warn("CTL_DISABLE_PORT ioctl failed");
1183 /* Remove iSCSI or ioctl port. */
1184 if (port->p_portal_group || port->p_ioctl_port) {
1185 bzero(&req, sizeof(req));
1186 strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi",
1187 sizeof(req.driver));
1188 req.reqtype = CTL_REQ_REMOVE;
1189 req.args_nvl = nvlist_create(0);
1190 if (req.args_nvl == NULL)
1191 log_err(1, "nvlist_create");
1193 if (port->p_ioctl_port)
1194 nvlist_add_stringf(req.args_nvl, "port_id", "%d",
1197 nvlist_add_string(req.args_nvl, "cfiscsi_target",
1199 nvlist_add_stringf(req.args_nvl,
1200 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1203 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1204 if (req.args == NULL) {
1205 log_warn("error packing nvlist");
1209 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1210 nvlist_destroy(req.args_nvl);
1213 log_warn("error issuing CTL_PORT_REQ ioctl");
1216 if (req.status == CTL_LUN_ERROR) {
1217 log_warnx("error returned from port removal request: %s",
1221 if (req.status != CTL_LUN_OK) {
1222 log_warnx("unknown port removal request status %d",
1227 /* Disable LUN mapping. */
1228 lm.port = port->p_ctl_port;
1229 lm.plun = UINT32_MAX;
1230 lm.lun = UINT32_MAX;
1231 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1233 log_warn("CTL_LUN_MAP ioctl failed");
1238 #ifdef ICL_KERNEL_PROXY
1240 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1242 struct ctl_iscsi req;
1244 bzero(&req, sizeof(req));
1246 req.type = CTL_ISCSI_LISTEN;
1247 req.data.listen.iser = iser;
1248 req.data.listen.domain = ai->ai_family;
1249 req.data.listen.socktype = ai->ai_socktype;
1250 req.data.listen.protocol = ai->ai_protocol;
1251 req.data.listen.addr = ai->ai_addr;
1252 req.data.listen.addrlen = ai->ai_addrlen;
1253 req.data.listen.portal_id = portal_id;
1255 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1256 log_err(1, "error issuing CTL_ISCSI ioctl");
1258 if (req.status != CTL_ISCSI_OK) {
1259 log_errx(1, "error returned from CTL iSCSI listen: %s",
1265 kernel_accept(int *connection_id, int *portal_id,
1266 struct sockaddr *client_sa, socklen_t *client_salen)
1268 struct ctl_iscsi req;
1269 struct sockaddr_storage ss;
1271 bzero(&req, sizeof(req));
1273 req.type = CTL_ISCSI_ACCEPT;
1274 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1276 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1277 log_err(1, "error issuing CTL_ISCSI ioctl");
1279 if (req.status != CTL_ISCSI_OK) {
1280 log_errx(1, "error returned from CTL iSCSI accept: %s",
1284 *connection_id = req.data.accept.connection_id;
1285 *portal_id = req.data.accept.portal_id;
1286 *client_salen = req.data.accept.initiator_addrlen;
1287 memcpy(client_sa, &ss, *client_salen);
1291 kernel_send(struct pdu *pdu)
1293 struct ctl_iscsi req;
1295 bzero(&req, sizeof(req));
1297 req.type = CTL_ISCSI_SEND;
1298 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1299 req.data.send.bhs = pdu->pdu_bhs;
1300 req.data.send.data_segment_len = pdu->pdu_data_len;
1301 req.data.send.data_segment = pdu->pdu_data;
1303 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1304 log_err(1, "error issuing CTL_ISCSI ioctl; "
1305 "dropping connection");
1308 if (req.status != CTL_ISCSI_OK) {
1309 log_errx(1, "error returned from CTL iSCSI send: "
1310 "%s; dropping connection", req.error_str);
1315 kernel_receive(struct pdu *pdu)
1317 struct connection *conn;
1318 struct ctl_iscsi req;
1320 conn = pdu->pdu_connection;
1321 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1322 if (pdu->pdu_data == NULL)
1323 log_err(1, "malloc");
1325 bzero(&req, sizeof(req));
1327 req.type = CTL_ISCSI_RECEIVE;
1328 req.data.receive.connection_id = conn->conn_socket;
1329 req.data.receive.bhs = pdu->pdu_bhs;
1330 req.data.receive.data_segment_len =
1331 conn->conn_max_recv_data_segment_length;
1332 req.data.receive.data_segment = pdu->pdu_data;
1334 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1335 log_err(1, "error issuing CTL_ISCSI ioctl; "
1336 "dropping connection");
1339 if (req.status != CTL_ISCSI_OK) {
1340 log_errx(1, "error returned from CTL iSCSI receive: "
1341 "%s; dropping connection", req.error_str);
1346 #endif /* ICL_KERNEL_PROXY */
1349 * XXX: I CANT INTO LATIN
1352 kernel_capsicate(void)
1354 cap_rights_t rights;
1355 const unsigned long cmds[] = { CTL_ISCSI };
1357 cap_rights_init(&rights, CAP_IOCTL);
1358 if (caph_rights_limit(ctl_fd, &rights) < 0)
1359 log_err(1, "cap_rights_limit");
1361 if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0)
1362 log_err(1, "cap_ioctls_limit");
1364 if (caph_enter() < 0)
1365 log_err(1, "cap_enter");
1367 if (cap_sandboxed())
1368 log_debugx("Capsicum capability mode enabled");
1370 log_warnx("Capsicum capability mode not supported");