2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/ioctl.h>
41 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/linker.h>
45 #include <sys/queue.h>
46 #include <sys/callout.h>
48 #include <sys/capsicum.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_io.h>
63 #include <cam/ctl/ctl_frontend_internal.h>
64 #include <cam/ctl/ctl_backend.h>
65 #include <cam/ctl/ctl_ioctl.h>
66 #include <cam/ctl/ctl_backend_block.h>
67 #include <cam/ctl/ctl_util.h>
68 #include <cam/ctl/ctl_scsi_all.h>
72 #ifdef ICL_KERNEL_PROXY
76 extern bool proxy_mode;
78 static int ctl_fd = 0;
83 int retval, saved_errno;
85 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
86 if (ctl_fd < 0 && errno == ENOENT) {
88 retval = kldload("ctl");
90 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
95 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
99 * Name/value pair used for per-LUN attributes.
104 STAILQ_ENTRY(cctl_lun_nv) links;
108 * Backend LUN information.
113 uint64_t size_blocks;
118 STAILQ_HEAD(,cctl_lun_nv) attr_list;
119 STAILQ_ENTRY(cctl_lun) links;
128 char *cfiscsi_target;
129 uint16_t cfiscsi_portal_group_tag;
130 char *ctld_portal_group_name;
131 STAILQ_HEAD(,cctl_lun_nv) attr_list;
132 STAILQ_ENTRY(cctl_port) links;
135 struct cctl_devlist_data {
137 STAILQ_HEAD(,cctl_lun) lun_list;
138 struct cctl_lun *cur_lun;
140 STAILQ_HEAD(,cctl_port) port_list;
141 struct cctl_port *cur_port;
143 struct sbuf *cur_sb[32];
147 cctl_start_element(void *user_data, const char *name, const char **attr)
150 struct cctl_devlist_data *devlist;
151 struct cctl_lun *cur_lun;
153 devlist = (struct cctl_devlist_data *)user_data;
154 cur_lun = devlist->cur_lun;
156 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
157 sizeof(devlist->cur_sb[0])))
158 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
159 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
161 devlist->cur_sb[devlist->level] = sbuf_new_auto();
162 if (devlist->cur_sb[devlist->level] == NULL)
163 log_err(1, "%s: unable to allocate sbuf", __func__);
165 if (strcmp(name, "lun") == 0) {
167 log_errx(1, "%s: improper lun element nesting",
170 cur_lun = calloc(1, sizeof(*cur_lun));
172 log_err(1, "%s: cannot allocate %zd bytes", __func__,
176 devlist->cur_lun = cur_lun;
178 STAILQ_INIT(&cur_lun->attr_list);
179 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
181 for (i = 0; attr[i] != NULL; i += 2) {
182 if (strcmp(attr[i], "id") == 0) {
183 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
185 log_errx(1, "%s: invalid LUN attribute %s = %s",
186 __func__, attr[i], attr[i+1]);
193 cctl_end_element(void *user_data, const char *name)
195 struct cctl_devlist_data *devlist;
196 struct cctl_lun *cur_lun;
199 devlist = (struct cctl_devlist_data *)user_data;
200 cur_lun = devlist->cur_lun;
202 if ((cur_lun == NULL)
203 && (strcmp(name, "ctllunlist") != 0))
204 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
206 if (devlist->cur_sb[devlist->level] == NULL)
207 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
208 devlist->level, name);
210 sbuf_finish(devlist->cur_sb[devlist->level]);
211 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
213 if (strlen(str) == 0) {
218 sbuf_delete(devlist->cur_sb[devlist->level]);
219 devlist->cur_sb[devlist->level] = NULL;
222 if (strcmp(name, "backend_type") == 0) {
223 cur_lun->backend_type = str;
225 } else if (strcmp(name, "size") == 0) {
226 cur_lun->size_blocks = strtoull(str, NULL, 0);
227 } else if (strcmp(name, "blocksize") == 0) {
228 cur_lun->blocksize = strtoul(str, NULL, 0);
229 } else if (strcmp(name, "serial_number") == 0) {
230 cur_lun->serial_number = str;
232 } else if (strcmp(name, "device_id") == 0) {
233 cur_lun->device_id = str;
235 } else if (strcmp(name, "ctld_name") == 0) {
236 cur_lun->ctld_name = str;
238 } else if (strcmp(name, "lun") == 0) {
239 devlist->cur_lun = NULL;
240 } else if (strcmp(name, "ctllunlist") == 0) {
243 struct cctl_lun_nv *nv;
245 nv = calloc(1, sizeof(*nv));
247 log_err(1, "%s: can't allocate %zd bytes for nv pair",
248 __func__, sizeof(*nv));
250 nv->name = checked_strdup(name);
254 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
261 cctl_start_pelement(void *user_data, const char *name, const char **attr)
264 struct cctl_devlist_data *devlist;
265 struct cctl_port *cur_port;
267 devlist = (struct cctl_devlist_data *)user_data;
268 cur_port = devlist->cur_port;
270 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
271 sizeof(devlist->cur_sb[0])))
272 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
273 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
275 devlist->cur_sb[devlist->level] = sbuf_new_auto();
276 if (devlist->cur_sb[devlist->level] == NULL)
277 log_err(1, "%s: unable to allocate sbuf", __func__);
279 if (strcmp(name, "targ_port") == 0) {
280 if (cur_port != NULL)
281 log_errx(1, "%s: improper port element nesting (%s)",
284 cur_port = calloc(1, sizeof(*cur_port));
285 if (cur_port == NULL)
286 log_err(1, "%s: cannot allocate %zd bytes", __func__,
289 devlist->num_ports++;
290 devlist->cur_port = cur_port;
292 STAILQ_INIT(&cur_port->attr_list);
293 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
295 for (i = 0; attr[i] != NULL; i += 2) {
296 if (strcmp(attr[i], "id") == 0) {
297 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
299 log_errx(1, "%s: invalid LUN attribute %s = %s",
300 __func__, attr[i], attr[i+1]);
307 cctl_end_pelement(void *user_data, const char *name)
309 struct cctl_devlist_data *devlist;
310 struct cctl_port *cur_port;
313 devlist = (struct cctl_devlist_data *)user_data;
314 cur_port = devlist->cur_port;
316 if ((cur_port == NULL)
317 && (strcmp(name, "ctlportlist") != 0))
318 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
320 if (devlist->cur_sb[devlist->level] == NULL)
321 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
322 devlist->level, name);
324 sbuf_finish(devlist->cur_sb[devlist->level]);
325 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
327 if (strlen(str) == 0) {
332 sbuf_delete(devlist->cur_sb[devlist->level]);
333 devlist->cur_sb[devlist->level] = NULL;
336 if (strcmp(name, "port_name") == 0) {
337 cur_port->port_name = str;
339 } else if (strcmp(name, "physical_port") == 0) {
340 cur_port->pp = strtoul(str, NULL, 0);
341 } else if (strcmp(name, "virtual_port") == 0) {
342 cur_port->vp = strtoul(str, NULL, 0);
343 } else if (strcmp(name, "cfiscsi_target") == 0) {
344 cur_port->cfiscsi_target = str;
346 } else if (strcmp(name, "cfiscsi_state") == 0) {
347 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
348 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
349 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
350 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
351 cur_port->ctld_portal_group_name = str;
353 } else if (strcmp(name, "targ_port") == 0) {
354 devlist->cur_port = NULL;
355 } else if (strcmp(name, "ctlportlist") == 0) {
358 struct cctl_lun_nv *nv;
360 nv = calloc(1, sizeof(*nv));
362 log_err(1, "%s: can't allocate %zd bytes for nv pair",
363 __func__, sizeof(*nv));
365 nv->name = checked_strdup(name);
369 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
376 cctl_char_handler(void *user_data, const XML_Char *str, int len)
378 struct cctl_devlist_data *devlist;
380 devlist = (struct cctl_devlist_data *)user_data;
382 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
386 conf_new_from_kernel(void)
388 struct conf *conf = NULL;
390 struct portal_group *pg;
394 struct lun_option *lo;
395 struct ctl_lun_list list;
396 struct cctl_devlist_data devlist;
397 struct cctl_lun *lun;
398 struct cctl_port *port;
403 bzero(&devlist, sizeof(devlist));
404 STAILQ_INIT(&devlist.lun_list);
405 STAILQ_INIT(&devlist.port_list);
407 log_debugx("obtaining previously configured CTL luns from the kernel");
412 str = realloc(str, len);
414 log_err(1, "realloc");
416 bzero(&list, sizeof(list));
417 list.alloc_len = len;
418 list.status = CTL_LUN_LIST_NONE;
421 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
422 log_warn("error issuing CTL_LUN_LIST ioctl");
427 if (list.status == CTL_LUN_LIST_ERROR) {
428 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
434 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
439 parser = XML_ParserCreate(NULL);
440 if (parser == NULL) {
441 log_warnx("unable to create XML parser");
446 XML_SetUserData(parser, &devlist);
447 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
448 XML_SetCharacterDataHandler(parser, cctl_char_handler);
450 retval = XML_Parse(parser, str, strlen(str), 1);
451 XML_ParserFree(parser);
454 log_warnx("XML_Parse failed");
461 str = realloc(str, len);
463 log_err(1, "realloc");
465 bzero(&list, sizeof(list));
466 list.alloc_len = len;
467 list.status = CTL_LUN_LIST_NONE;
470 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
471 log_warn("error issuing CTL_PORT_LIST ioctl");
476 if (list.status == CTL_PORT_LIST_ERROR) {
477 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
483 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
488 parser = XML_ParserCreate(NULL);
489 if (parser == NULL) {
490 log_warnx("unable to create XML parser");
495 XML_SetUserData(parser, &devlist);
496 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
497 XML_SetCharacterDataHandler(parser, cctl_char_handler);
499 retval = XML_Parse(parser, str, strlen(str), 1);
500 XML_ParserFree(parser);
503 log_warnx("XML_Parse failed");
510 STAILQ_FOREACH(port, &devlist.port_list, links) {
513 if (port->pp == 0 && port->vp == 0)
514 name = checked_strdup(port->port_name);
515 else if (port->vp == 0)
516 asprintf(&name, "%s/%d", port->port_name, port->pp);
518 asprintf(&name, "%s/%d/%d", port->port_name, port->pp,
521 if (port->cfiscsi_target == NULL) {
522 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
523 port->port_id, name);
524 pp = pport_find(conf, name);
527 log_debugx("found new kernel port %u \"%s\"",
528 port->port_id, name);
530 pp = pport_new(conf, name, port->port_id);
532 log_warnx("pport_new failed");
538 if (port->cfiscsi_state != 1) {
539 log_debugx("CTL port %ju is not active (%d); ignoring",
540 (uintmax_t)port->port_id, port->cfiscsi_state);
544 targ = target_find(conf, port->cfiscsi_target);
547 log_debugx("found new kernel target %s for CTL port %ld",
548 port->cfiscsi_target, port->port_id);
550 targ = target_new(conf, port->cfiscsi_target);
552 log_warnx("target_new failed");
557 if (port->ctld_portal_group_name == NULL)
559 pg = portal_group_find(conf, port->ctld_portal_group_name);
562 log_debugx("found new kernel portal group %s for CTL port %ld",
563 port->ctld_portal_group_name, port->port_id);
565 pg = portal_group_new(conf, port->ctld_portal_group_name);
567 log_warnx("portal_group_new failed");
571 pg->pg_tag = port->cfiscsi_portal_group_tag;
572 cp = port_new(conf, targ, pg);
574 log_warnx("port_new failed");
577 cp->p_ctl_port = port->port_id;
582 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
583 struct cctl_lun_nv *nv;
585 if (lun->ctld_name == NULL) {
586 log_debugx("CTL lun %ju wasn't managed by ctld; "
587 "ignoring", (uintmax_t)lun->lun_id);
591 cl = lun_find(conf, lun->ctld_name);
593 log_warnx("found CTL lun %ju \"%s\", "
594 "also backed by CTL lun %d; ignoring",
595 (uintmax_t)lun->lun_id, lun->ctld_name,
600 log_debugx("found CTL lun %ju \"%s\"",
601 (uintmax_t)lun->lun_id, lun->ctld_name);
603 cl = lun_new(conf, lun->ctld_name);
605 log_warnx("lun_new failed");
608 lun_set_backend(cl, lun->backend_type);
609 lun_set_blocksize(cl, lun->blocksize);
610 lun_set_device_id(cl, lun->device_id);
611 lun_set_serial(cl, lun->serial_number);
612 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
613 lun_set_ctl_lun(cl, lun->lun_id);
615 STAILQ_FOREACH(nv, &lun->attr_list, links) {
616 if (strcmp(nv->name, "file") == 0 ||
617 strcmp(nv->name, "dev") == 0) {
618 lun_set_path(cl, nv->value);
621 lo = lun_option_new(cl, nv->name, nv->value);
623 log_warnx("unable to add CTL lun option %s "
624 "for CTL lun %ju \"%s\"",
625 nv->name, (uintmax_t) lun->lun_id,
634 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
637 arg->namelen = strlen(name) + 1;
638 arg->name = __DECONST(char *, name);
639 arg->vallen = strlen(value) + 1;
640 arg->value = __DECONST(char *, value);
641 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
645 kernel_lun_add(struct lun *lun)
647 struct lun_option *lo;
648 struct ctl_lun_req req;
649 int error, i, num_options;
651 bzero(&req, sizeof(req));
653 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
654 req.reqtype = CTL_LUNREQ_CREATE;
656 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
658 if (lun->l_size != 0)
659 req.reqdata.create.lun_size_bytes = lun->l_size;
661 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
662 req.reqdata.create.device_type = T_DIRECT;
664 if (lun->l_serial != NULL) {
665 strncpy(req.reqdata.create.serial_num, lun->l_serial,
666 sizeof(req.reqdata.create.serial_num));
667 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
670 if (lun->l_device_id != NULL) {
671 strncpy(req.reqdata.create.device_id, lun->l_device_id,
672 sizeof(req.reqdata.create.device_id));
673 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
676 if (lun->l_path != NULL) {
677 lo = lun_option_find(lun, "file");
679 lun_option_set(lo, lun->l_path);
681 lo = lun_option_new(lun, "file", lun->l_path);
686 lo = lun_option_find(lun, "ctld_name");
688 lun_option_set(lo, lun->l_name);
690 lo = lun_option_new(lun, "ctld_name", lun->l_name);
694 lo = lun_option_find(lun, "scsiname");
695 if (lo == NULL && lun->l_scsiname != NULL) {
696 lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
701 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
704 req.num_be_args = num_options;
705 if (num_options > 0) {
706 req.be_args = malloc(num_options * sizeof(*req.be_args));
707 if (req.be_args == NULL) {
708 log_warn("error allocating %zd bytes",
709 num_options * sizeof(*req.be_args));
714 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
715 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
718 assert(i == num_options);
721 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
724 log_warn("error issuing CTL_LUN_REQ ioctl");
728 switch (req.status) {
730 log_warnx("LUN creation error: %s", req.error_str);
732 case CTL_LUN_WARNING:
733 log_warnx("LUN creation warning: %s", req.error_str);
738 log_warnx("unknown LUN creation status: %d",
743 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
748 kernel_lun_resize(struct lun *lun)
750 struct ctl_lun_req req;
752 bzero(&req, sizeof(req));
754 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
755 req.reqtype = CTL_LUNREQ_MODIFY;
757 req.reqdata.modify.lun_id = lun->l_ctl_lun;
758 req.reqdata.modify.lun_size_bytes = lun->l_size;
760 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
761 log_warn("error issuing CTL_LUN_REQ ioctl");
765 switch (req.status) {
767 log_warnx("LUN modification error: %s", req.error_str);
769 case CTL_LUN_WARNING:
770 log_warnx("LUN modification warning: %s", req.error_str);
775 log_warnx("unknown LUN modification status: %d",
784 kernel_lun_remove(struct lun *lun)
786 struct ctl_lun_req req;
788 bzero(&req, sizeof(req));
790 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
791 req.reqtype = CTL_LUNREQ_RM;
793 req.reqdata.rm.lun_id = lun->l_ctl_lun;
795 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
796 log_warn("error issuing CTL_LUN_REQ ioctl");
800 switch (req.status) {
802 log_warnx("LUN removal error: %s", req.error_str);
804 case CTL_LUN_WARNING:
805 log_warnx("LUN removal warning: %s", req.error_str);
810 log_warnx("unknown LUN removal status: %d", req.status);
818 kernel_handoff(struct connection *conn)
820 struct ctl_iscsi req;
822 bzero(&req, sizeof(req));
824 req.type = CTL_ISCSI_HANDOFF;
825 strlcpy(req.data.handoff.initiator_name,
826 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
827 strlcpy(req.data.handoff.initiator_addr,
828 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
829 if (conn->conn_initiator_alias != NULL) {
830 strlcpy(req.data.handoff.initiator_alias,
831 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
833 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
834 sizeof(req.data.handoff.initiator_isid));
835 strlcpy(req.data.handoff.target_name,
836 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
837 #ifdef ICL_KERNEL_PROXY
839 req.data.handoff.connection_id = conn->conn_socket;
841 req.data.handoff.socket = conn->conn_socket;
843 req.data.handoff.socket = conn->conn_socket;
845 req.data.handoff.portal_group_tag =
846 conn->conn_portal->p_portal_group->pg_tag;
847 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
848 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
849 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
850 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
851 req.data.handoff.cmdsn = conn->conn_cmdsn;
852 req.data.handoff.statsn = conn->conn_statsn;
853 req.data.handoff.max_recv_data_segment_length =
854 conn->conn_max_data_segment_length;
855 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
856 req.data.handoff.immediate_data = conn->conn_immediate_data;
858 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
859 log_err(1, "error issuing CTL_ISCSI ioctl; "
860 "dropping connection");
863 if (req.status != CTL_ISCSI_OK) {
864 log_errx(1, "error returned from CTL iSCSI handoff request: "
865 "%s; dropping connection", req.error_str);
870 kernel_port_add(struct port *port)
872 struct ctl_port_entry entry;
874 struct ctl_lun_map lm;
875 struct target *targ = port->p_target;
876 struct portal_group *pg = port->p_portal_group;
880 /* Create iSCSI port. */
881 if (port->p_portal_group) {
882 bzero(&req, sizeof(req));
883 strlcpy(req.driver, "iscsi", sizeof(req.driver));
884 req.reqtype = CTL_REQ_CREATE;
886 req.args = malloc(req.num_args * sizeof(*req.args));
887 if (req.args == NULL)
888 log_err(1, "malloc");
890 req.args[n].namelen = sizeof("port_id");
891 req.args[n].name = __DECONST(char *, "port_id");
892 req.args[n].vallen = sizeof(port->p_ctl_port);
893 req.args[n].value = &port->p_ctl_port;
894 req.args[n++].flags = CTL_BEARG_WR;
895 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
896 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
897 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
899 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
900 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
902 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
905 log_warn("error issuing CTL_PORT_REQ ioctl");
908 if (req.status == CTL_LUN_ERROR) {
909 log_warnx("error returned from port creation request: %s",
913 if (req.status != CTL_LUN_OK) {
914 log_warnx("unknown port creation request status %d",
918 } else if (port->p_pport) {
919 port->p_ctl_port = port->p_pport->pp_ctl_port;
921 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
922 strlen(targ->t_name) == 20) {
923 bzero(&entry, sizeof(entry));
924 entry.port_type = CTL_PORT_NONE;
925 entry.targ_port = port->p_ctl_port;
926 entry.flags |= CTL_PORT_WWNN_VALID;
927 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
928 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
929 log_warn("CTL_SET_PORT_WWNS ioctl failed");
933 /* Explicitly enable mapping to block any access except allowed. */
934 lm.port = port->p_ctl_port;
935 lm.plun = UINT32_MAX;
937 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
939 log_warn("CTL_LUN_MAP ioctl failed");
941 /* Map configured LUNs */
942 for (i = 0; i < MAX_LUNS; i++) {
943 if (targ->t_luns[i] == NULL)
945 lm.port = port->p_ctl_port;
947 lm.lun = targ->t_luns[i]->l_ctl_lun;
948 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
950 log_warn("CTL_LUN_MAP ioctl failed");
954 bzero(&entry, sizeof(entry));
955 entry.targ_port = port->p_ctl_port;
956 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
958 log_warn("CTL_ENABLE_PORT ioctl failed");
966 kernel_port_update(struct port *port)
968 struct ctl_lun_map lm;
969 struct target *targ = port->p_target;
972 /* Map configured LUNs and unmap others */
973 for (i = 0; i < MAX_LUNS; i++) {
974 lm.port = port->p_ctl_port;
976 if (targ->t_luns[i] == NULL)
979 lm.lun = targ->t_luns[i]->l_ctl_lun;
980 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
982 log_warn("CTL_LUN_MAP ioctl failed");
988 kernel_port_remove(struct port *port)
990 struct ctl_port_entry entry;
991 struct ctl_lun_map lm;
994 struct target *targ = port->p_target;
995 struct portal_group *pg = port->p_portal_group;
999 bzero(&entry, sizeof(entry));
1000 entry.targ_port = port->p_ctl_port;
1001 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1003 log_warn("CTL_DISABLE_PORT ioctl failed");
1007 /* Remove iSCSI port. */
1008 if (port->p_portal_group) {
1009 bzero(&req, sizeof(req));
1010 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1011 req.reqtype = CTL_REQ_REMOVE;
1013 req.args = malloc(req.num_args * sizeof(*req.args));
1014 if (req.args == NULL)
1015 log_err(1, "malloc");
1016 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1017 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1018 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1019 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1022 log_warn("error issuing CTL_PORT_REQ ioctl");
1025 if (req.status == CTL_LUN_ERROR) {
1026 log_warnx("error returned from port removal request: %s",
1030 if (req.status != CTL_LUN_OK) {
1031 log_warnx("unknown port removal request status %d",
1036 /* Disable LUN mapping. */
1037 lm.port = port->p_ctl_port;
1038 lm.plun = UINT32_MAX;
1039 lm.lun = UINT32_MAX;
1040 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1042 log_warn("CTL_LUN_MAP ioctl failed");
1047 #ifdef ICL_KERNEL_PROXY
1049 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1051 struct ctl_iscsi req;
1053 bzero(&req, sizeof(req));
1055 req.type = CTL_ISCSI_LISTEN;
1056 req.data.listen.iser = iser;
1057 req.data.listen.domain = ai->ai_family;
1058 req.data.listen.socktype = ai->ai_socktype;
1059 req.data.listen.protocol = ai->ai_protocol;
1060 req.data.listen.addr = ai->ai_addr;
1061 req.data.listen.addrlen = ai->ai_addrlen;
1062 req.data.listen.portal_id = portal_id;
1064 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1065 log_err(1, "error issuing CTL_ISCSI ioctl");
1067 if (req.status != CTL_ISCSI_OK) {
1068 log_errx(1, "error returned from CTL iSCSI listen: %s",
1074 kernel_accept(int *connection_id, int *portal_id,
1075 struct sockaddr *client_sa, socklen_t *client_salen)
1077 struct ctl_iscsi req;
1078 struct sockaddr_storage ss;
1080 bzero(&req, sizeof(req));
1082 req.type = CTL_ISCSI_ACCEPT;
1083 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1085 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1086 log_err(1, "error issuing CTL_ISCSI ioctl");
1088 if (req.status != CTL_ISCSI_OK) {
1089 log_errx(1, "error returned from CTL iSCSI accept: %s",
1093 *connection_id = req.data.accept.connection_id;
1094 *portal_id = req.data.accept.portal_id;
1095 *client_salen = req.data.accept.initiator_addrlen;
1096 memcpy(client_sa, &ss, *client_salen);
1100 kernel_send(struct pdu *pdu)
1102 struct ctl_iscsi req;
1104 bzero(&req, sizeof(req));
1106 req.type = CTL_ISCSI_SEND;
1107 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1108 req.data.send.bhs = pdu->pdu_bhs;
1109 req.data.send.data_segment_len = pdu->pdu_data_len;
1110 req.data.send.data_segment = pdu->pdu_data;
1112 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1113 log_err(1, "error issuing CTL_ISCSI ioctl; "
1114 "dropping connection");
1117 if (req.status != CTL_ISCSI_OK) {
1118 log_errx(1, "error returned from CTL iSCSI send: "
1119 "%s; dropping connection", req.error_str);
1124 kernel_receive(struct pdu *pdu)
1126 struct ctl_iscsi req;
1128 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1129 if (pdu->pdu_data == NULL)
1130 log_err(1, "malloc");
1132 bzero(&req, sizeof(req));
1134 req.type = CTL_ISCSI_RECEIVE;
1135 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1136 req.data.receive.bhs = pdu->pdu_bhs;
1137 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1138 req.data.receive.data_segment = pdu->pdu_data;
1140 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1141 log_err(1, "error issuing CTL_ISCSI ioctl; "
1142 "dropping connection");
1145 if (req.status != CTL_ISCSI_OK) {
1146 log_errx(1, "error returned from CTL iSCSI receive: "
1147 "%s; dropping connection", req.error_str);
1152 #endif /* ICL_KERNEL_PROXY */
1155 * XXX: I CANT INTO LATIN
1158 kernel_capsicate(void)
1161 cap_rights_t rights;
1162 const unsigned long cmds[] = { CTL_ISCSI };
1164 cap_rights_init(&rights, CAP_IOCTL);
1165 error = cap_rights_limit(ctl_fd, &rights);
1166 if (error != 0 && errno != ENOSYS)
1167 log_err(1, "cap_rights_limit");
1169 error = cap_ioctls_limit(ctl_fd, cmds,
1170 sizeof(cmds) / sizeof(cmds[0]));
1171 if (error != 0 && errno != ENOSYS)
1172 log_err(1, "cap_ioctls_limit");
1174 error = cap_enter();
1175 if (error != 0 && errno != ENOSYS)
1176 log_err(1, "cap_enter");
1178 if (cap_sandboxed())
1179 log_debugx("Capsicum capability mode enabled");
1181 log_warnx("Capsicum capability mode not supported");