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_backend.h>
64 #include <cam/ctl/ctl_ioctl.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_scsi_all.h>
70 #ifdef ICL_KERNEL_PROXY
74 extern bool proxy_mode;
76 static int ctl_fd = 0;
81 int retval, saved_errno;
83 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84 if (ctl_fd < 0 && errno == ENOENT) {
86 retval = kldload("ctl");
88 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
93 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
97 * Name/value pair used for per-LUN attributes.
102 STAILQ_ENTRY(cctl_lun_nv) links;
106 * Backend LUN information.
111 uint64_t size_blocks;
116 STAILQ_HEAD(,cctl_lun_nv) attr_list;
117 STAILQ_ENTRY(cctl_lun) links;
126 char *cfiscsi_target;
127 uint16_t cfiscsi_portal_group_tag;
128 char *ctld_portal_group_name;
129 STAILQ_HEAD(,cctl_lun_nv) attr_list;
130 STAILQ_ENTRY(cctl_port) links;
133 struct cctl_devlist_data {
135 STAILQ_HEAD(,cctl_lun) lun_list;
136 struct cctl_lun *cur_lun;
138 STAILQ_HEAD(,cctl_port) port_list;
139 struct cctl_port *cur_port;
141 struct sbuf *cur_sb[32];
145 cctl_start_element(void *user_data, const char *name, const char **attr)
148 struct cctl_devlist_data *devlist;
149 struct cctl_lun *cur_lun;
151 devlist = (struct cctl_devlist_data *)user_data;
152 cur_lun = devlist->cur_lun;
154 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
155 sizeof(devlist->cur_sb[0])))
156 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
157 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
159 devlist->cur_sb[devlist->level] = sbuf_new_auto();
160 if (devlist->cur_sb[devlist->level] == NULL)
161 log_err(1, "%s: unable to allocate sbuf", __func__);
163 if (strcmp(name, "lun") == 0) {
165 log_errx(1, "%s: improper lun element nesting",
168 cur_lun = calloc(1, sizeof(*cur_lun));
170 log_err(1, "%s: cannot allocate %zd bytes", __func__,
174 devlist->cur_lun = cur_lun;
176 STAILQ_INIT(&cur_lun->attr_list);
177 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
179 for (i = 0; attr[i] != NULL; i += 2) {
180 if (strcmp(attr[i], "id") == 0) {
181 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
183 log_errx(1, "%s: invalid LUN attribute %s = %s",
184 __func__, attr[i], attr[i+1]);
191 cctl_end_element(void *user_data, const char *name)
193 struct cctl_devlist_data *devlist;
194 struct cctl_lun *cur_lun;
197 devlist = (struct cctl_devlist_data *)user_data;
198 cur_lun = devlist->cur_lun;
200 if ((cur_lun == NULL)
201 && (strcmp(name, "ctllunlist") != 0))
202 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
204 if (devlist->cur_sb[devlist->level] == NULL)
205 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
206 devlist->level, name);
208 sbuf_finish(devlist->cur_sb[devlist->level]);
209 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
211 if (strlen(str) == 0) {
216 sbuf_delete(devlist->cur_sb[devlist->level]);
217 devlist->cur_sb[devlist->level] = NULL;
220 if (strcmp(name, "backend_type") == 0) {
221 cur_lun->backend_type = str;
223 } else if (strcmp(name, "size") == 0) {
224 cur_lun->size_blocks = strtoull(str, NULL, 0);
225 } else if (strcmp(name, "blocksize") == 0) {
226 cur_lun->blocksize = strtoul(str, NULL, 0);
227 } else if (strcmp(name, "serial_number") == 0) {
228 cur_lun->serial_number = str;
230 } else if (strcmp(name, "device_id") == 0) {
231 cur_lun->device_id = str;
233 } else if (strcmp(name, "ctld_name") == 0) {
234 cur_lun->ctld_name = str;
236 } else if (strcmp(name, "lun") == 0) {
237 devlist->cur_lun = NULL;
238 } else if (strcmp(name, "ctllunlist") == 0) {
241 struct cctl_lun_nv *nv;
243 nv = calloc(1, sizeof(*nv));
245 log_err(1, "%s: can't allocate %zd bytes for nv pair",
246 __func__, sizeof(*nv));
248 nv->name = checked_strdup(name);
252 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
259 cctl_start_pelement(void *user_data, const char *name, const char **attr)
262 struct cctl_devlist_data *devlist;
263 struct cctl_port *cur_port;
265 devlist = (struct cctl_devlist_data *)user_data;
266 cur_port = devlist->cur_port;
268 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
269 sizeof(devlist->cur_sb[0])))
270 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
271 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
273 devlist->cur_sb[devlist->level] = sbuf_new_auto();
274 if (devlist->cur_sb[devlist->level] == NULL)
275 log_err(1, "%s: unable to allocate sbuf", __func__);
277 if (strcmp(name, "targ_port") == 0) {
278 if (cur_port != NULL)
279 log_errx(1, "%s: improper port element nesting (%s)",
282 cur_port = calloc(1, sizeof(*cur_port));
283 if (cur_port == NULL)
284 log_err(1, "%s: cannot allocate %zd bytes", __func__,
287 devlist->num_ports++;
288 devlist->cur_port = cur_port;
290 STAILQ_INIT(&cur_port->attr_list);
291 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
293 for (i = 0; attr[i] != NULL; i += 2) {
294 if (strcmp(attr[i], "id") == 0) {
295 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
297 log_errx(1, "%s: invalid LUN attribute %s = %s",
298 __func__, attr[i], attr[i+1]);
305 cctl_end_pelement(void *user_data, const char *name)
307 struct cctl_devlist_data *devlist;
308 struct cctl_port *cur_port;
311 devlist = (struct cctl_devlist_data *)user_data;
312 cur_port = devlist->cur_port;
314 if ((cur_port == NULL)
315 && (strcmp(name, "ctlportlist") != 0))
316 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
318 if (devlist->cur_sb[devlist->level] == NULL)
319 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
320 devlist->level, name);
322 sbuf_finish(devlist->cur_sb[devlist->level]);
323 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
325 if (strlen(str) == 0) {
330 sbuf_delete(devlist->cur_sb[devlist->level]);
331 devlist->cur_sb[devlist->level] = NULL;
334 if (strcmp(name, "port_name") == 0) {
335 cur_port->port_name = str;
337 } else if (strcmp(name, "physical_port") == 0) {
338 cur_port->pp = strtoul(str, NULL, 0);
339 } else if (strcmp(name, "virtual_port") == 0) {
340 cur_port->vp = strtoul(str, NULL, 0);
341 } else if (strcmp(name, "cfiscsi_target") == 0) {
342 cur_port->cfiscsi_target = str;
344 } else if (strcmp(name, "cfiscsi_state") == 0) {
345 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
346 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
347 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
348 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
349 cur_port->ctld_portal_group_name = str;
351 } else if (strcmp(name, "targ_port") == 0) {
352 devlist->cur_port = NULL;
353 } else if (strcmp(name, "ctlportlist") == 0) {
356 struct cctl_lun_nv *nv;
358 nv = calloc(1, sizeof(*nv));
360 log_err(1, "%s: can't allocate %zd bytes for nv pair",
361 __func__, sizeof(*nv));
363 nv->name = checked_strdup(name);
367 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
374 cctl_char_handler(void *user_data, const XML_Char *str, int len)
376 struct cctl_devlist_data *devlist;
378 devlist = (struct cctl_devlist_data *)user_data;
380 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
384 conf_new_from_kernel(void)
386 struct conf *conf = NULL;
388 struct portal_group *pg;
392 struct lun_option *lo;
393 struct ctl_lun_list list;
394 struct cctl_devlist_data devlist;
395 struct cctl_lun *lun;
396 struct cctl_port *port;
401 bzero(&devlist, sizeof(devlist));
402 STAILQ_INIT(&devlist.lun_list);
403 STAILQ_INIT(&devlist.port_list);
405 log_debugx("obtaining previously configured CTL luns from the kernel");
410 str = realloc(str, len);
412 log_err(1, "realloc");
414 bzero(&list, sizeof(list));
415 list.alloc_len = len;
416 list.status = CTL_LUN_LIST_NONE;
419 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
420 log_warn("error issuing CTL_LUN_LIST ioctl");
425 if (list.status == CTL_LUN_LIST_ERROR) {
426 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
432 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
437 parser = XML_ParserCreate(NULL);
438 if (parser == NULL) {
439 log_warnx("unable to create XML parser");
444 XML_SetUserData(parser, &devlist);
445 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
446 XML_SetCharacterDataHandler(parser, cctl_char_handler);
448 retval = XML_Parse(parser, str, strlen(str), 1);
449 XML_ParserFree(parser);
452 log_warnx("XML_Parse failed");
459 str = realloc(str, len);
461 log_err(1, "realloc");
463 bzero(&list, sizeof(list));
464 list.alloc_len = len;
465 list.status = CTL_LUN_LIST_NONE;
468 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
469 log_warn("error issuing CTL_PORT_LIST ioctl");
474 if (list.status == CTL_PORT_LIST_ERROR) {
475 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
481 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
486 parser = XML_ParserCreate(NULL);
487 if (parser == NULL) {
488 log_warnx("unable to create XML parser");
493 XML_SetUserData(parser, &devlist);
494 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
495 XML_SetCharacterDataHandler(parser, cctl_char_handler);
497 retval = XML_Parse(parser, str, strlen(str), 1);
498 XML_ParserFree(parser);
501 log_warnx("XML_Parse failed");
508 STAILQ_FOREACH(port, &devlist.port_list, links) {
511 if (port->pp == 0 && port->vp == 0)
512 name = checked_strdup(port->port_name);
513 else if (port->vp == 0)
514 asprintf(&name, "%s/%d", port->port_name, port->pp);
516 asprintf(&name, "%s/%d/%d", port->port_name, port->pp,
519 if (port->cfiscsi_target == NULL) {
520 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
521 port->port_id, name);
522 pp = pport_find(conf, name);
525 log_debugx("found new kernel port %u \"%s\"",
526 port->port_id, name);
528 pp = pport_new(conf, name, port->port_id);
530 log_warnx("pport_new failed");
536 if (port->cfiscsi_state != 1) {
537 log_debugx("CTL port %ju is not active (%d); ignoring",
538 (uintmax_t)port->port_id, port->cfiscsi_state);
542 targ = target_find(conf, port->cfiscsi_target);
545 log_debugx("found new kernel target %s for CTL port %ld",
546 port->cfiscsi_target, port->port_id);
548 targ = target_new(conf, port->cfiscsi_target);
550 log_warnx("target_new failed");
555 if (port->ctld_portal_group_name == NULL)
557 pg = portal_group_find(conf, port->ctld_portal_group_name);
560 log_debugx("found new kernel portal group %s for CTL port %ld",
561 port->ctld_portal_group_name, port->port_id);
563 pg = portal_group_new(conf, port->ctld_portal_group_name);
565 log_warnx("portal_group_new failed");
569 pg->pg_tag = port->cfiscsi_portal_group_tag;
570 cp = port_new(conf, targ, pg);
572 log_warnx("port_new failed");
575 cp->p_ctl_port = port->port_id;
580 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
581 struct cctl_lun_nv *nv;
583 if (lun->ctld_name == NULL) {
584 log_debugx("CTL lun %ju wasn't managed by ctld; "
585 "ignoring", (uintmax_t)lun->lun_id);
589 cl = lun_find(conf, lun->ctld_name);
591 log_warnx("found CTL lun %ju \"%s\", "
592 "also backed by CTL lun %d; ignoring",
593 (uintmax_t)lun->lun_id, lun->ctld_name,
598 log_debugx("found CTL lun %ju \"%s\"",
599 (uintmax_t)lun->lun_id, lun->ctld_name);
601 cl = lun_new(conf, lun->ctld_name);
603 log_warnx("lun_new failed");
606 lun_set_backend(cl, lun->backend_type);
607 lun_set_blocksize(cl, lun->blocksize);
608 lun_set_device_id(cl, lun->device_id);
609 lun_set_serial(cl, lun->serial_number);
610 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
611 lun_set_ctl_lun(cl, lun->lun_id);
613 STAILQ_FOREACH(nv, &lun->attr_list, links) {
614 if (strcmp(nv->name, "file") == 0 ||
615 strcmp(nv->name, "dev") == 0) {
616 lun_set_path(cl, nv->value);
619 lo = lun_option_new(cl, nv->name, nv->value);
621 log_warnx("unable to add CTL lun option %s "
622 "for CTL lun %ju \"%s\"",
623 nv->name, (uintmax_t) lun->lun_id,
632 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
635 arg->namelen = strlen(name) + 1;
636 arg->name = __DECONST(char *, name);
637 arg->vallen = strlen(value) + 1;
638 arg->value = __DECONST(char *, value);
639 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
643 kernel_lun_add(struct lun *lun)
645 struct lun_option *lo;
646 struct ctl_lun_req req;
647 int error, i, num_options;
649 bzero(&req, sizeof(req));
651 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
652 req.reqtype = CTL_LUNREQ_CREATE;
654 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
656 if (lun->l_size != 0)
657 req.reqdata.create.lun_size_bytes = lun->l_size;
659 if (lun->l_ctl_lun >= 0) {
660 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
661 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
664 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
665 req.reqdata.create.device_type = T_DIRECT;
667 if (lun->l_serial != NULL) {
668 strncpy(req.reqdata.create.serial_num, lun->l_serial,
669 sizeof(req.reqdata.create.serial_num));
670 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
673 if (lun->l_device_id != NULL) {
674 strncpy(req.reqdata.create.device_id, lun->l_device_id,
675 sizeof(req.reqdata.create.device_id));
676 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
679 if (lun->l_path != NULL) {
680 lo = lun_option_find(lun, "file");
682 lun_option_set(lo, lun->l_path);
684 lo = lun_option_new(lun, "file", lun->l_path);
689 lo = lun_option_find(lun, "ctld_name");
691 lun_option_set(lo, lun->l_name);
693 lo = lun_option_new(lun, "ctld_name", lun->l_name);
697 lo = lun_option_find(lun, "scsiname");
698 if (lo == NULL && lun->l_scsiname != NULL) {
699 lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
704 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
707 req.num_be_args = num_options;
708 if (num_options > 0) {
709 req.be_args = malloc(num_options * sizeof(*req.be_args));
710 if (req.be_args == NULL) {
711 log_warn("error allocating %zd bytes",
712 num_options * sizeof(*req.be_args));
717 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
718 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
721 assert(i == num_options);
724 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
727 log_warn("error issuing CTL_LUN_REQ ioctl");
731 switch (req.status) {
733 log_warnx("LUN creation error: %s", req.error_str);
735 case CTL_LUN_WARNING:
736 log_warnx("LUN creation warning: %s", req.error_str);
741 log_warnx("unknown LUN creation status: %d",
746 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
751 kernel_lun_modify(struct lun *lun)
753 struct lun_option *lo;
754 struct ctl_lun_req req;
755 int error, i, num_options;
757 bzero(&req, sizeof(req));
759 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
760 req.reqtype = CTL_LUNREQ_MODIFY;
762 req.reqdata.modify.lun_id = lun->l_ctl_lun;
763 req.reqdata.modify.lun_size_bytes = lun->l_size;
766 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
769 req.num_be_args = num_options;
770 if (num_options > 0) {
771 req.be_args = malloc(num_options * sizeof(*req.be_args));
772 if (req.be_args == NULL) {
773 log_warn("error allocating %zd bytes",
774 num_options * sizeof(*req.be_args));
779 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
780 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
783 assert(i == num_options);
786 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
789 log_warn("error issuing CTL_LUN_REQ ioctl");
793 switch (req.status) {
795 log_warnx("LUN modification error: %s", req.error_str);
797 case CTL_LUN_WARNING:
798 log_warnx("LUN modification warning: %s", req.error_str);
803 log_warnx("unknown LUN modification status: %d",
812 kernel_lun_remove(struct lun *lun)
814 struct ctl_lun_req req;
816 bzero(&req, sizeof(req));
818 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
819 req.reqtype = CTL_LUNREQ_RM;
821 req.reqdata.rm.lun_id = lun->l_ctl_lun;
823 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
824 log_warn("error issuing CTL_LUN_REQ ioctl");
828 switch (req.status) {
830 log_warnx("LUN removal error: %s", req.error_str);
832 case CTL_LUN_WARNING:
833 log_warnx("LUN removal warning: %s", req.error_str);
838 log_warnx("unknown LUN removal status: %d", req.status);
846 kernel_handoff(struct connection *conn)
848 struct ctl_iscsi req;
850 bzero(&req, sizeof(req));
852 req.type = CTL_ISCSI_HANDOFF;
853 strlcpy(req.data.handoff.initiator_name,
854 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
855 strlcpy(req.data.handoff.initiator_addr,
856 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
857 if (conn->conn_initiator_alias != NULL) {
858 strlcpy(req.data.handoff.initiator_alias,
859 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
861 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
862 sizeof(req.data.handoff.initiator_isid));
863 strlcpy(req.data.handoff.target_name,
864 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
865 #ifdef ICL_KERNEL_PROXY
867 req.data.handoff.connection_id = conn->conn_socket;
869 req.data.handoff.socket = conn->conn_socket;
871 req.data.handoff.socket = conn->conn_socket;
873 req.data.handoff.portal_group_tag =
874 conn->conn_portal->p_portal_group->pg_tag;
875 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
876 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
877 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
878 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
879 req.data.handoff.cmdsn = conn->conn_cmdsn;
880 req.data.handoff.statsn = conn->conn_statsn;
881 req.data.handoff.max_recv_data_segment_length =
882 conn->conn_max_data_segment_length;
883 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
884 req.data.handoff.immediate_data = conn->conn_immediate_data;
886 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
887 log_err(1, "error issuing CTL_ISCSI ioctl; "
888 "dropping connection");
891 if (req.status != CTL_ISCSI_OK) {
892 log_errx(1, "error returned from CTL iSCSI handoff request: "
893 "%s; dropping connection", req.error_str);
898 kernel_port_add(struct port *port)
900 struct ctl_port_entry entry;
902 struct ctl_lun_map lm;
903 struct target *targ = port->p_target;
904 struct portal_group *pg = port->p_portal_group;
908 /* Create iSCSI port. */
909 if (port->p_portal_group) {
910 bzero(&req, sizeof(req));
911 strlcpy(req.driver, "iscsi", sizeof(req.driver));
912 req.reqtype = CTL_REQ_CREATE;
914 req.args = malloc(req.num_args * sizeof(*req.args));
915 if (req.args == NULL)
916 log_err(1, "malloc");
918 req.args[n].namelen = sizeof("port_id");
919 req.args[n].name = __DECONST(char *, "port_id");
920 req.args[n].vallen = sizeof(port->p_ctl_port);
921 req.args[n].value = &port->p_ctl_port;
922 req.args[n++].flags = CTL_BEARG_WR;
923 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
924 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
925 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
927 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
928 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
930 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
933 log_warn("error issuing CTL_PORT_REQ ioctl");
936 if (req.status == CTL_LUN_ERROR) {
937 log_warnx("error returned from port creation request: %s",
941 if (req.status != CTL_LUN_OK) {
942 log_warnx("unknown port creation request status %d",
946 } else if (port->p_pport) {
947 port->p_ctl_port = port->p_pport->pp_ctl_port;
949 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
950 strlen(targ->t_name) == 20) {
951 bzero(&entry, sizeof(entry));
952 entry.port_type = CTL_PORT_NONE;
953 entry.targ_port = port->p_ctl_port;
954 entry.flags |= CTL_PORT_WWNN_VALID;
955 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
956 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
957 log_warn("CTL_SET_PORT_WWNS ioctl failed");
961 /* Explicitly enable mapping to block any access except allowed. */
962 lm.port = port->p_ctl_port;
963 lm.plun = UINT32_MAX;
965 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
967 log_warn("CTL_LUN_MAP ioctl failed");
969 /* Map configured LUNs */
970 for (i = 0; i < MAX_LUNS; i++) {
971 if (targ->t_luns[i] == NULL)
973 lm.port = port->p_ctl_port;
975 lm.lun = targ->t_luns[i]->l_ctl_lun;
976 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
978 log_warn("CTL_LUN_MAP ioctl failed");
982 bzero(&entry, sizeof(entry));
983 entry.targ_port = port->p_ctl_port;
984 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
986 log_warn("CTL_ENABLE_PORT ioctl failed");
994 kernel_port_update(struct port *port, struct port *oport)
996 struct ctl_lun_map lm;
997 struct target *targ = port->p_target;
998 struct target *otarg = oport->p_target;
1002 /* Map configured LUNs and unmap others */
1003 for (i = 0; i < MAX_LUNS; i++) {
1004 lm.port = port->p_ctl_port;
1006 if (targ->t_luns[i] == NULL)
1007 lm.lun = UINT32_MAX;
1009 lm.lun = targ->t_luns[i]->l_ctl_lun;
1010 if (otarg->t_luns[i] == NULL)
1013 olun = otarg->t_luns[i]->l_ctl_lun;
1016 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1018 log_warn("CTL_LUN_MAP ioctl failed");
1024 kernel_port_remove(struct port *port)
1026 struct ctl_port_entry entry;
1027 struct ctl_lun_map lm;
1030 struct target *targ = port->p_target;
1031 struct portal_group *pg = port->p_portal_group;
1035 bzero(&entry, sizeof(entry));
1036 entry.targ_port = port->p_ctl_port;
1037 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1039 log_warn("CTL_DISABLE_PORT ioctl failed");
1043 /* Remove iSCSI port. */
1044 if (port->p_portal_group) {
1045 bzero(&req, sizeof(req));
1046 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1047 req.reqtype = CTL_REQ_REMOVE;
1049 req.args = malloc(req.num_args * sizeof(*req.args));
1050 if (req.args == NULL)
1051 log_err(1, "malloc");
1052 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1053 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1054 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1055 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1058 log_warn("error issuing CTL_PORT_REQ ioctl");
1061 if (req.status == CTL_LUN_ERROR) {
1062 log_warnx("error returned from port removal request: %s",
1066 if (req.status != CTL_LUN_OK) {
1067 log_warnx("unknown port removal request status %d",
1072 /* Disable LUN mapping. */
1073 lm.port = port->p_ctl_port;
1074 lm.plun = UINT32_MAX;
1075 lm.lun = UINT32_MAX;
1076 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1078 log_warn("CTL_LUN_MAP ioctl failed");
1083 #ifdef ICL_KERNEL_PROXY
1085 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1087 struct ctl_iscsi req;
1089 bzero(&req, sizeof(req));
1091 req.type = CTL_ISCSI_LISTEN;
1092 req.data.listen.iser = iser;
1093 req.data.listen.domain = ai->ai_family;
1094 req.data.listen.socktype = ai->ai_socktype;
1095 req.data.listen.protocol = ai->ai_protocol;
1096 req.data.listen.addr = ai->ai_addr;
1097 req.data.listen.addrlen = ai->ai_addrlen;
1098 req.data.listen.portal_id = portal_id;
1100 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1101 log_err(1, "error issuing CTL_ISCSI ioctl");
1103 if (req.status != CTL_ISCSI_OK) {
1104 log_errx(1, "error returned from CTL iSCSI listen: %s",
1110 kernel_accept(int *connection_id, int *portal_id,
1111 struct sockaddr *client_sa, socklen_t *client_salen)
1113 struct ctl_iscsi req;
1114 struct sockaddr_storage ss;
1116 bzero(&req, sizeof(req));
1118 req.type = CTL_ISCSI_ACCEPT;
1119 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1121 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1122 log_err(1, "error issuing CTL_ISCSI ioctl");
1124 if (req.status != CTL_ISCSI_OK) {
1125 log_errx(1, "error returned from CTL iSCSI accept: %s",
1129 *connection_id = req.data.accept.connection_id;
1130 *portal_id = req.data.accept.portal_id;
1131 *client_salen = req.data.accept.initiator_addrlen;
1132 memcpy(client_sa, &ss, *client_salen);
1136 kernel_send(struct pdu *pdu)
1138 struct ctl_iscsi req;
1140 bzero(&req, sizeof(req));
1142 req.type = CTL_ISCSI_SEND;
1143 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1144 req.data.send.bhs = pdu->pdu_bhs;
1145 req.data.send.data_segment_len = pdu->pdu_data_len;
1146 req.data.send.data_segment = pdu->pdu_data;
1148 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1149 log_err(1, "error issuing CTL_ISCSI ioctl; "
1150 "dropping connection");
1153 if (req.status != CTL_ISCSI_OK) {
1154 log_errx(1, "error returned from CTL iSCSI send: "
1155 "%s; dropping connection", req.error_str);
1160 kernel_receive(struct pdu *pdu)
1162 struct ctl_iscsi req;
1164 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1165 if (pdu->pdu_data == NULL)
1166 log_err(1, "malloc");
1168 bzero(&req, sizeof(req));
1170 req.type = CTL_ISCSI_RECEIVE;
1171 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1172 req.data.receive.bhs = pdu->pdu_bhs;
1173 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1174 req.data.receive.data_segment = pdu->pdu_data;
1176 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1177 log_err(1, "error issuing CTL_ISCSI ioctl; "
1178 "dropping connection");
1181 if (req.status != CTL_ISCSI_OK) {
1182 log_errx(1, "error returned from CTL iSCSI receive: "
1183 "%s; dropping connection", req.error_str);
1188 #endif /* ICL_KERNEL_PROXY */
1191 * XXX: I CANT INTO LATIN
1194 kernel_capsicate(void)
1197 cap_rights_t rights;
1198 const unsigned long cmds[] = { CTL_ISCSI };
1200 cap_rights_init(&rights, CAP_IOCTL);
1201 error = cap_rights_limit(ctl_fd, &rights);
1202 if (error != 0 && errno != ENOSYS)
1203 log_err(1, "cap_rights_limit");
1205 error = cap_ioctls_limit(ctl_fd, cmds,
1206 sizeof(cmds) / sizeof(cmds[0]));
1207 if (error != 0 && errno != ENOSYS)
1208 log_err(1, "cap_ioctls_limit");
1210 error = cap_enter();
1211 if (error != 0 && errno != ENOSYS)
1212 log_err(1, "cap_enter");
1214 if (cap_sandboxed())
1215 log_debugx("Capsicum capability mode enabled");
1217 log_warnx("Capsicum capability mode not supported");