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;
127 char *cfiscsi_target;
128 uint16_t cfiscsi_portal_group_tag;
129 char *ctld_portal_group_name;
130 STAILQ_HEAD(,cctl_lun_nv) attr_list;
131 STAILQ_ENTRY(cctl_port) links;
134 struct cctl_devlist_data {
136 STAILQ_HEAD(,cctl_lun) lun_list;
137 struct cctl_lun *cur_lun;
139 STAILQ_HEAD(,cctl_port) port_list;
140 struct cctl_port *cur_port;
142 struct sbuf *cur_sb[32];
146 cctl_start_element(void *user_data, const char *name, const char **attr)
149 struct cctl_devlist_data *devlist;
150 struct cctl_lun *cur_lun;
152 devlist = (struct cctl_devlist_data *)user_data;
153 cur_lun = devlist->cur_lun;
155 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
156 sizeof(devlist->cur_sb[0])))
157 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
158 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
160 devlist->cur_sb[devlist->level] = sbuf_new_auto();
161 if (devlist->cur_sb[devlist->level] == NULL)
162 log_err(1, "%s: unable to allocate sbuf", __func__);
164 if (strcmp(name, "lun") == 0) {
166 log_errx(1, "%s: improper lun element nesting",
169 cur_lun = calloc(1, sizeof(*cur_lun));
171 log_err(1, "%s: cannot allocate %zd bytes", __func__,
175 devlist->cur_lun = cur_lun;
177 STAILQ_INIT(&cur_lun->attr_list);
178 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
180 for (i = 0; attr[i] != NULL; i += 2) {
181 if (strcmp(attr[i], "id") == 0) {
182 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
184 log_errx(1, "%s: invalid LUN attribute %s = %s",
185 __func__, attr[i], attr[i+1]);
192 cctl_end_element(void *user_data, const char *name)
194 struct cctl_devlist_data *devlist;
195 struct cctl_lun *cur_lun;
198 devlist = (struct cctl_devlist_data *)user_data;
199 cur_lun = devlist->cur_lun;
201 if ((cur_lun == NULL)
202 && (strcmp(name, "ctllunlist") != 0))
203 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
205 if (devlist->cur_sb[devlist->level] == NULL)
206 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
207 devlist->level, name);
209 sbuf_finish(devlist->cur_sb[devlist->level]);
210 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
212 if (strlen(str) == 0) {
217 sbuf_delete(devlist->cur_sb[devlist->level]);
218 devlist->cur_sb[devlist->level] = NULL;
221 if (strcmp(name, "backend_type") == 0) {
222 cur_lun->backend_type = str;
224 } else if (strcmp(name, "size") == 0) {
225 cur_lun->size_blocks = strtoull(str, NULL, 0);
226 } else if (strcmp(name, "blocksize") == 0) {
227 cur_lun->blocksize = strtoul(str, NULL, 0);
228 } else if (strcmp(name, "serial_number") == 0) {
229 cur_lun->serial_number = str;
231 } else if (strcmp(name, "device_id") == 0) {
232 cur_lun->device_id = str;
234 } else if (strcmp(name, "ctld_name") == 0) {
235 cur_lun->ctld_name = str;
237 } else if (strcmp(name, "lun") == 0) {
238 devlist->cur_lun = NULL;
239 } else if (strcmp(name, "ctllunlist") == 0) {
242 struct cctl_lun_nv *nv;
244 nv = calloc(1, sizeof(*nv));
246 log_err(1, "%s: can't allocate %zd bytes for nv pair",
247 __func__, sizeof(*nv));
249 nv->name = checked_strdup(name);
253 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
260 cctl_start_pelement(void *user_data, const char *name, const char **attr)
263 struct cctl_devlist_data *devlist;
264 struct cctl_port *cur_port;
266 devlist = (struct cctl_devlist_data *)user_data;
267 cur_port = devlist->cur_port;
269 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
270 sizeof(devlist->cur_sb[0])))
271 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
272 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
274 devlist->cur_sb[devlist->level] = sbuf_new_auto();
275 if (devlist->cur_sb[devlist->level] == NULL)
276 log_err(1, "%s: unable to allocate sbuf", __func__);
278 if (strcmp(name, "targ_port") == 0) {
279 if (cur_port != NULL)
280 log_errx(1, "%s: improper port element nesting (%s)",
283 cur_port = calloc(1, sizeof(*cur_port));
284 if (cur_port == NULL)
285 log_err(1, "%s: cannot allocate %zd bytes", __func__,
288 devlist->num_ports++;
289 devlist->cur_port = cur_port;
291 STAILQ_INIT(&cur_port->attr_list);
292 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
294 for (i = 0; attr[i] != NULL; i += 2) {
295 if (strcmp(attr[i], "id") == 0) {
296 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
298 log_errx(1, "%s: invalid LUN attribute %s = %s",
299 __func__, attr[i], attr[i+1]);
306 cctl_end_pelement(void *user_data, const char *name)
308 struct cctl_devlist_data *devlist;
309 struct cctl_port *cur_port;
312 devlist = (struct cctl_devlist_data *)user_data;
313 cur_port = devlist->cur_port;
315 if ((cur_port == NULL)
316 && (strcmp(name, "ctlportlist") != 0))
317 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
319 if (devlist->cur_sb[devlist->level] == NULL)
320 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
321 devlist->level, name);
323 sbuf_finish(devlist->cur_sb[devlist->level]);
324 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
326 if (strlen(str) == 0) {
331 sbuf_delete(devlist->cur_sb[devlist->level]);
332 devlist->cur_sb[devlist->level] = NULL;
335 if (strcmp(name, "frontend_type") == 0) {
336 cur_port->port_frontend = str;
338 } else if (strcmp(name, "port_name") == 0) {
339 cur_port->port_name = str;
341 } else if (strcmp(name, "physical_port") == 0) {
342 cur_port->pp = strtoul(str, NULL, 0);
343 } else if (strcmp(name, "virtual_port") == 0) {
344 cur_port->vp = strtoul(str, NULL, 0);
345 } else if (strcmp(name, "cfiscsi_target") == 0) {
346 cur_port->cfiscsi_target = str;
348 } else if (strcmp(name, "cfiscsi_state") == 0) {
349 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
350 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
351 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
352 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
353 cur_port->ctld_portal_group_name = str;
355 } else if (strcmp(name, "targ_port") == 0) {
356 devlist->cur_port = NULL;
357 } else if (strcmp(name, "ctlportlist") == 0) {
360 struct cctl_lun_nv *nv;
362 nv = calloc(1, sizeof(*nv));
364 log_err(1, "%s: can't allocate %zd bytes for nv pair",
365 __func__, sizeof(*nv));
367 nv->name = checked_strdup(name);
371 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
378 cctl_char_handler(void *user_data, const XML_Char *str, int len)
380 struct cctl_devlist_data *devlist;
382 devlist = (struct cctl_devlist_data *)user_data;
384 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
388 conf_new_from_kernel(void)
390 struct conf *conf = NULL;
392 struct portal_group *pg;
396 struct lun_option *lo;
397 struct ctl_lun_list list;
398 struct cctl_devlist_data devlist;
399 struct cctl_lun *lun;
400 struct cctl_port *port;
405 bzero(&devlist, sizeof(devlist));
406 STAILQ_INIT(&devlist.lun_list);
407 STAILQ_INIT(&devlist.port_list);
409 log_debugx("obtaining previously configured CTL luns from the kernel");
414 str = realloc(str, len);
416 log_err(1, "realloc");
418 bzero(&list, sizeof(list));
419 list.alloc_len = len;
420 list.status = CTL_LUN_LIST_NONE;
423 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
424 log_warn("error issuing CTL_LUN_LIST ioctl");
429 if (list.status == CTL_LUN_LIST_ERROR) {
430 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
436 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
441 parser = XML_ParserCreate(NULL);
442 if (parser == NULL) {
443 log_warnx("unable to create XML parser");
448 XML_SetUserData(parser, &devlist);
449 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
450 XML_SetCharacterDataHandler(parser, cctl_char_handler);
452 retval = XML_Parse(parser, str, strlen(str), 1);
453 XML_ParserFree(parser);
456 log_warnx("XML_Parse failed");
463 str = realloc(str, len);
465 log_err(1, "realloc");
467 bzero(&list, sizeof(list));
468 list.alloc_len = len;
469 list.status = CTL_LUN_LIST_NONE;
472 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
473 log_warn("error issuing CTL_PORT_LIST ioctl");
478 if (list.status == CTL_PORT_LIST_ERROR) {
479 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
485 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
490 parser = XML_ParserCreate(NULL);
491 if (parser == NULL) {
492 log_warnx("unable to create XML parser");
497 XML_SetUserData(parser, &devlist);
498 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
499 XML_SetCharacterDataHandler(parser, cctl_char_handler);
501 retval = XML_Parse(parser, str, strlen(str), 1);
502 XML_ParserFree(parser);
505 log_warnx("XML_Parse failed");
512 STAILQ_FOREACH(port, &devlist.port_list, links) {
513 if (strcmp(port->port_frontend, "ha") == 0)
517 if (port->pp == 0 && port->vp == 0)
518 name = checked_strdup(port->port_name);
519 else if (port->vp == 0)
520 asprintf(&name, "%s/%d", port->port_name, port->pp);
522 asprintf(&name, "%s/%d/%d", port->port_name, port->pp,
525 if (port->cfiscsi_target == NULL) {
526 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
527 port->port_id, name);
528 pp = pport_find(conf, name);
531 log_debugx("found new kernel port %u \"%s\"",
532 port->port_id, name);
534 pp = pport_new(conf, name, port->port_id);
536 log_warnx("pport_new failed");
542 if (port->cfiscsi_state != 1) {
543 log_debugx("CTL port %ju is not active (%d); ignoring",
544 (uintmax_t)port->port_id, port->cfiscsi_state);
548 targ = target_find(conf, port->cfiscsi_target);
551 log_debugx("found new kernel target %s for CTL port %ld",
552 port->cfiscsi_target, port->port_id);
554 targ = target_new(conf, port->cfiscsi_target);
556 log_warnx("target_new failed");
561 if (port->ctld_portal_group_name == NULL)
563 pg = portal_group_find(conf, port->ctld_portal_group_name);
566 log_debugx("found new kernel portal group %s for CTL port %ld",
567 port->ctld_portal_group_name, port->port_id);
569 pg = portal_group_new(conf, port->ctld_portal_group_name);
571 log_warnx("portal_group_new failed");
575 pg->pg_tag = port->cfiscsi_portal_group_tag;
576 cp = port_new(conf, targ, pg);
578 log_warnx("port_new failed");
581 cp->p_ctl_port = port->port_id;
586 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
587 struct cctl_lun_nv *nv;
589 if (lun->ctld_name == NULL) {
590 log_debugx("CTL lun %ju wasn't managed by ctld; "
591 "ignoring", (uintmax_t)lun->lun_id);
595 cl = lun_find(conf, lun->ctld_name);
597 log_warnx("found CTL lun %ju \"%s\", "
598 "also backed by CTL lun %d; ignoring",
599 (uintmax_t)lun->lun_id, lun->ctld_name,
604 log_debugx("found CTL lun %ju \"%s\"",
605 (uintmax_t)lun->lun_id, lun->ctld_name);
607 cl = lun_new(conf, lun->ctld_name);
609 log_warnx("lun_new failed");
612 lun_set_backend(cl, lun->backend_type);
613 lun_set_blocksize(cl, lun->blocksize);
614 lun_set_device_id(cl, lun->device_id);
615 lun_set_serial(cl, lun->serial_number);
616 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
617 lun_set_ctl_lun(cl, lun->lun_id);
619 STAILQ_FOREACH(nv, &lun->attr_list, links) {
620 if (strcmp(nv->name, "file") == 0 ||
621 strcmp(nv->name, "dev") == 0) {
622 lun_set_path(cl, nv->value);
625 lo = lun_option_new(cl, nv->name, nv->value);
627 log_warnx("unable to add CTL lun option %s "
628 "for CTL lun %ju \"%s\"",
629 nv->name, (uintmax_t) lun->lun_id,
638 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
641 arg->namelen = strlen(name) + 1;
642 arg->name = __DECONST(char *, name);
643 arg->vallen = strlen(value) + 1;
644 arg->value = __DECONST(char *, value);
645 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
649 kernel_lun_add(struct lun *lun)
651 struct lun_option *lo;
652 struct ctl_lun_req req;
653 int error, i, num_options;
655 bzero(&req, sizeof(req));
657 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
658 req.reqtype = CTL_LUNREQ_CREATE;
660 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
662 if (lun->l_size != 0)
663 req.reqdata.create.lun_size_bytes = lun->l_size;
665 if (lun->l_ctl_lun >= 0) {
666 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
667 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
670 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
671 req.reqdata.create.device_type = T_DIRECT;
673 if (lun->l_serial != NULL) {
674 strncpy(req.reqdata.create.serial_num, lun->l_serial,
675 sizeof(req.reqdata.create.serial_num));
676 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
679 if (lun->l_device_id != NULL) {
680 strncpy(req.reqdata.create.device_id, lun->l_device_id,
681 sizeof(req.reqdata.create.device_id));
682 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
685 if (lun->l_path != NULL) {
686 lo = lun_option_find(lun, "file");
688 lun_option_set(lo, lun->l_path);
690 lo = lun_option_new(lun, "file", lun->l_path);
695 lo = lun_option_find(lun, "ctld_name");
697 lun_option_set(lo, lun->l_name);
699 lo = lun_option_new(lun, "ctld_name", lun->l_name);
703 lo = lun_option_find(lun, "scsiname");
704 if (lo == NULL && lun->l_scsiname != NULL) {
705 lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
710 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
713 req.num_be_args = num_options;
714 if (num_options > 0) {
715 req.be_args = malloc(num_options * sizeof(*req.be_args));
716 if (req.be_args == NULL) {
717 log_warn("error allocating %zd bytes",
718 num_options * sizeof(*req.be_args));
723 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
724 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
727 assert(i == num_options);
730 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
733 log_warn("error issuing CTL_LUN_REQ ioctl");
737 switch (req.status) {
739 log_warnx("LUN creation error: %s", req.error_str);
741 case CTL_LUN_WARNING:
742 log_warnx("LUN creation warning: %s", req.error_str);
747 log_warnx("unknown LUN creation status: %d",
752 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
757 kernel_lun_modify(struct lun *lun)
759 struct lun_option *lo;
760 struct ctl_lun_req req;
761 int error, i, num_options;
763 bzero(&req, sizeof(req));
765 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
766 req.reqtype = CTL_LUNREQ_MODIFY;
768 req.reqdata.modify.lun_id = lun->l_ctl_lun;
769 req.reqdata.modify.lun_size_bytes = lun->l_size;
772 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
775 req.num_be_args = num_options;
776 if (num_options > 0) {
777 req.be_args = malloc(num_options * sizeof(*req.be_args));
778 if (req.be_args == NULL) {
779 log_warn("error allocating %zd bytes",
780 num_options * sizeof(*req.be_args));
785 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
786 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
789 assert(i == num_options);
792 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
795 log_warn("error issuing CTL_LUN_REQ ioctl");
799 switch (req.status) {
801 log_warnx("LUN modification error: %s", req.error_str);
803 case CTL_LUN_WARNING:
804 log_warnx("LUN modification warning: %s", req.error_str);
809 log_warnx("unknown LUN modification status: %d",
818 kernel_lun_remove(struct lun *lun)
820 struct ctl_lun_req req;
822 bzero(&req, sizeof(req));
824 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
825 req.reqtype = CTL_LUNREQ_RM;
827 req.reqdata.rm.lun_id = lun->l_ctl_lun;
829 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
830 log_warn("error issuing CTL_LUN_REQ ioctl");
834 switch (req.status) {
836 log_warnx("LUN removal error: %s", req.error_str);
838 case CTL_LUN_WARNING:
839 log_warnx("LUN removal warning: %s", req.error_str);
844 log_warnx("unknown LUN removal status: %d", req.status);
852 kernel_handoff(struct connection *conn)
854 struct ctl_iscsi req;
856 bzero(&req, sizeof(req));
858 req.type = CTL_ISCSI_HANDOFF;
859 strlcpy(req.data.handoff.initiator_name,
860 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
861 strlcpy(req.data.handoff.initiator_addr,
862 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
863 if (conn->conn_initiator_alias != NULL) {
864 strlcpy(req.data.handoff.initiator_alias,
865 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
867 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
868 sizeof(req.data.handoff.initiator_isid));
869 strlcpy(req.data.handoff.target_name,
870 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
871 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
872 strlcpy(req.data.handoff.offload,
873 conn->conn_portal->p_portal_group->pg_offload,
874 sizeof(req.data.handoff.offload));
876 #ifdef ICL_KERNEL_PROXY
878 req.data.handoff.connection_id = conn->conn_socket;
880 req.data.handoff.socket = conn->conn_socket;
882 req.data.handoff.socket = conn->conn_socket;
884 req.data.handoff.portal_group_tag =
885 conn->conn_portal->p_portal_group->pg_tag;
886 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
887 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
888 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
889 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
890 req.data.handoff.cmdsn = conn->conn_cmdsn;
891 req.data.handoff.statsn = conn->conn_statsn;
892 req.data.handoff.max_recv_data_segment_length =
893 conn->conn_max_data_segment_length;
894 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
895 req.data.handoff.immediate_data = conn->conn_immediate_data;
897 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
898 log_err(1, "error issuing CTL_ISCSI ioctl; "
899 "dropping connection");
902 if (req.status != CTL_ISCSI_OK) {
903 log_errx(1, "error returned from CTL iSCSI handoff request: "
904 "%s; dropping connection", req.error_str);
909 kernel_limits(const char *offload, size_t *max_data_segment_length)
911 struct ctl_iscsi req;
913 bzero(&req, sizeof(req));
915 req.type = CTL_ISCSI_LIMITS;
916 if (offload != NULL) {
917 strlcpy(req.data.limits.offload, offload,
918 sizeof(req.data.limits.offload));
921 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
922 log_err(1, "error issuing CTL_ISCSI ioctl; "
923 "dropping connection");
926 if (req.status != CTL_ISCSI_OK) {
927 log_errx(1, "error returned from CTL iSCSI limits request: "
928 "%s; dropping connection", req.error_str);
931 *max_data_segment_length = req.data.limits.data_segment_limit;
932 if (offload != NULL) {
933 log_debugx("MaxRecvDataSegment kernel limit for offload "
934 "\"%s\" is %zd", offload, *max_data_segment_length);
936 log_debugx("MaxRecvDataSegment kernel limit is %zd",
937 *max_data_segment_length);
942 kernel_port_add(struct port *port)
944 struct ctl_port_entry entry;
946 struct ctl_lun_map lm;
947 struct target *targ = port->p_target;
948 struct portal_group *pg = port->p_portal_group;
952 /* Create iSCSI port. */
953 if (port->p_portal_group) {
954 bzero(&req, sizeof(req));
955 strlcpy(req.driver, "iscsi", sizeof(req.driver));
956 req.reqtype = CTL_REQ_CREATE;
958 req.args = malloc(req.num_args * sizeof(*req.args));
959 if (req.args == NULL)
960 log_err(1, "malloc");
962 req.args[n].namelen = sizeof("port_id");
963 req.args[n].name = __DECONST(char *, "port_id");
964 req.args[n].vallen = sizeof(port->p_ctl_port);
965 req.args[n].value = &port->p_ctl_port;
966 req.args[n++].flags = CTL_BEARG_WR;
967 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
968 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
969 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
971 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
972 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
974 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
977 log_warn("error issuing CTL_PORT_REQ ioctl");
980 if (req.status == CTL_LUN_ERROR) {
981 log_warnx("error returned from port creation request: %s",
985 if (req.status != CTL_LUN_OK) {
986 log_warnx("unknown port creation request status %d",
990 } else if (port->p_pport) {
991 port->p_ctl_port = port->p_pport->pp_ctl_port;
993 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
994 strlen(targ->t_name) == 20) {
995 bzero(&entry, sizeof(entry));
996 entry.port_type = CTL_PORT_NONE;
997 entry.targ_port = port->p_ctl_port;
998 entry.flags |= CTL_PORT_WWNN_VALID;
999 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1000 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1001 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1005 /* Explicitly enable mapping to block any access except allowed. */
1006 lm.port = port->p_ctl_port;
1007 lm.plun = UINT32_MAX;
1009 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1011 log_warn("CTL_LUN_MAP ioctl failed");
1013 /* Map configured LUNs */
1014 for (i = 0; i < MAX_LUNS; i++) {
1015 if (targ->t_luns[i] == NULL)
1017 lm.port = port->p_ctl_port;
1019 lm.lun = targ->t_luns[i]->l_ctl_lun;
1020 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1022 log_warn("CTL_LUN_MAP ioctl failed");
1026 bzero(&entry, sizeof(entry));
1027 entry.targ_port = port->p_ctl_port;
1028 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1030 log_warn("CTL_ENABLE_PORT ioctl failed");
1038 kernel_port_update(struct port *port, struct port *oport)
1040 struct ctl_lun_map lm;
1041 struct target *targ = port->p_target;
1042 struct target *otarg = oport->p_target;
1046 /* Map configured LUNs and unmap others */
1047 for (i = 0; i < MAX_LUNS; i++) {
1048 lm.port = port->p_ctl_port;
1050 if (targ->t_luns[i] == NULL)
1051 lm.lun = UINT32_MAX;
1053 lm.lun = targ->t_luns[i]->l_ctl_lun;
1054 if (otarg->t_luns[i] == NULL)
1057 olun = otarg->t_luns[i]->l_ctl_lun;
1060 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1062 log_warn("CTL_LUN_MAP ioctl failed");
1068 kernel_port_remove(struct port *port)
1070 struct ctl_port_entry entry;
1071 struct ctl_lun_map lm;
1074 struct target *targ = port->p_target;
1075 struct portal_group *pg = port->p_portal_group;
1079 bzero(&entry, sizeof(entry));
1080 entry.targ_port = port->p_ctl_port;
1081 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1083 log_warn("CTL_DISABLE_PORT ioctl failed");
1087 /* Remove iSCSI port. */
1088 if (port->p_portal_group) {
1089 bzero(&req, sizeof(req));
1090 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1091 req.reqtype = CTL_REQ_REMOVE;
1093 req.args = malloc(req.num_args * sizeof(*req.args));
1094 if (req.args == NULL)
1095 log_err(1, "malloc");
1096 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1097 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1098 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1099 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1102 log_warn("error issuing CTL_PORT_REQ ioctl");
1105 if (req.status == CTL_LUN_ERROR) {
1106 log_warnx("error returned from port removal request: %s",
1110 if (req.status != CTL_LUN_OK) {
1111 log_warnx("unknown port removal request status %d",
1116 /* Disable LUN mapping. */
1117 lm.port = port->p_ctl_port;
1118 lm.plun = UINT32_MAX;
1119 lm.lun = UINT32_MAX;
1120 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1122 log_warn("CTL_LUN_MAP ioctl failed");
1127 #ifdef ICL_KERNEL_PROXY
1129 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1131 struct ctl_iscsi req;
1133 bzero(&req, sizeof(req));
1135 req.type = CTL_ISCSI_LISTEN;
1136 req.data.listen.iser = iser;
1137 req.data.listen.domain = ai->ai_family;
1138 req.data.listen.socktype = ai->ai_socktype;
1139 req.data.listen.protocol = ai->ai_protocol;
1140 req.data.listen.addr = ai->ai_addr;
1141 req.data.listen.addrlen = ai->ai_addrlen;
1142 req.data.listen.portal_id = portal_id;
1144 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1145 log_err(1, "error issuing CTL_ISCSI ioctl");
1147 if (req.status != CTL_ISCSI_OK) {
1148 log_errx(1, "error returned from CTL iSCSI listen: %s",
1154 kernel_accept(int *connection_id, int *portal_id,
1155 struct sockaddr *client_sa, socklen_t *client_salen)
1157 struct ctl_iscsi req;
1158 struct sockaddr_storage ss;
1160 bzero(&req, sizeof(req));
1162 req.type = CTL_ISCSI_ACCEPT;
1163 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1165 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1166 log_err(1, "error issuing CTL_ISCSI ioctl");
1168 if (req.status != CTL_ISCSI_OK) {
1169 log_errx(1, "error returned from CTL iSCSI accept: %s",
1173 *connection_id = req.data.accept.connection_id;
1174 *portal_id = req.data.accept.portal_id;
1175 *client_salen = req.data.accept.initiator_addrlen;
1176 memcpy(client_sa, &ss, *client_salen);
1180 kernel_send(struct pdu *pdu)
1182 struct ctl_iscsi req;
1184 bzero(&req, sizeof(req));
1186 req.type = CTL_ISCSI_SEND;
1187 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1188 req.data.send.bhs = pdu->pdu_bhs;
1189 req.data.send.data_segment_len = pdu->pdu_data_len;
1190 req.data.send.data_segment = pdu->pdu_data;
1192 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1193 log_err(1, "error issuing CTL_ISCSI ioctl; "
1194 "dropping connection");
1197 if (req.status != CTL_ISCSI_OK) {
1198 log_errx(1, "error returned from CTL iSCSI send: "
1199 "%s; dropping connection", req.error_str);
1204 kernel_receive(struct pdu *pdu)
1206 struct ctl_iscsi req;
1208 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1209 if (pdu->pdu_data == NULL)
1210 log_err(1, "malloc");
1212 bzero(&req, sizeof(req));
1214 req.type = CTL_ISCSI_RECEIVE;
1215 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1216 req.data.receive.bhs = pdu->pdu_bhs;
1217 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1218 req.data.receive.data_segment = pdu->pdu_data;
1220 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1221 log_err(1, "error issuing CTL_ISCSI ioctl; "
1222 "dropping connection");
1225 if (req.status != CTL_ISCSI_OK) {
1226 log_errx(1, "error returned from CTL iSCSI receive: "
1227 "%s; dropping connection", req.error_str);
1232 #endif /* ICL_KERNEL_PROXY */
1235 * XXX: I CANT INTO LATIN
1238 kernel_capsicate(void)
1241 cap_rights_t rights;
1242 const unsigned long cmds[] = { CTL_ISCSI };
1244 cap_rights_init(&rights, CAP_IOCTL);
1245 error = cap_rights_limit(ctl_fd, &rights);
1246 if (error != 0 && errno != ENOSYS)
1247 log_err(1, "cap_rights_limit");
1249 error = cap_ioctls_limit(ctl_fd, cmds,
1250 sizeof(cmds) / sizeof(cmds[0]));
1251 if (error != 0 && errno != ENOSYS)
1252 log_err(1, "cap_ioctls_limit");
1254 error = cap_enter();
1255 if (error != 0 && errno != ENOSYS)
1256 log_err(1, "cap_enter");
1258 if (cap_sandboxed())
1259 log_debugx("Capsicum capability mode enabled");
1261 log_warnx("Capsicum capability mode not supported");