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/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/queue.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 #include <cam/ctl/ctl.h>
61 #include <cam/ctl/ctl_io.h>
62 #include <cam/ctl/ctl_backend.h>
63 #include <cam/ctl/ctl_ioctl.h>
64 #include <cam/ctl/ctl_util.h>
65 #include <cam/ctl/ctl_scsi_all.h>
69 #ifdef ICL_KERNEL_PROXY
73 extern bool proxy_mode;
75 static int ctl_fd = 0;
80 int retval, saved_errno;
82 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
83 if (ctl_fd < 0 && errno == ENOENT) {
85 retval = kldload("ctl");
87 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
92 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
96 * Name/value pair used for per-LUN attributes.
101 STAILQ_ENTRY(cctl_lun_nv) links;
105 * 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, "lun_type") == 0) {
225 cur_lun->device_type = strtoull(str, NULL, 0);
226 } else if (strcmp(name, "size") == 0) {
227 cur_lun->size_blocks = strtoull(str, NULL, 0);
228 } else if (strcmp(name, "blocksize") == 0) {
229 cur_lun->blocksize = strtoul(str, NULL, 0);
230 } else if (strcmp(name, "serial_number") == 0) {
231 cur_lun->serial_number = str;
233 } else if (strcmp(name, "device_id") == 0) {
234 cur_lun->device_id = str;
236 } else if (strcmp(name, "ctld_name") == 0) {
237 cur_lun->ctld_name = str;
239 } else if (strcmp(name, "lun") == 0) {
240 devlist->cur_lun = NULL;
241 } else if (strcmp(name, "ctllunlist") == 0) {
244 struct cctl_lun_nv *nv;
246 nv = calloc(1, sizeof(*nv));
248 log_err(1, "%s: can't allocate %zd bytes for nv pair",
249 __func__, sizeof(*nv));
251 nv->name = checked_strdup(name);
255 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
262 cctl_start_pelement(void *user_data, const char *name, const char **attr)
265 struct cctl_devlist_data *devlist;
266 struct cctl_port *cur_port;
268 devlist = (struct cctl_devlist_data *)user_data;
269 cur_port = devlist->cur_port;
271 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
272 sizeof(devlist->cur_sb[0])))
273 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
274 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
276 devlist->cur_sb[devlist->level] = sbuf_new_auto();
277 if (devlist->cur_sb[devlist->level] == NULL)
278 log_err(1, "%s: unable to allocate sbuf", __func__);
280 if (strcmp(name, "targ_port") == 0) {
281 if (cur_port != NULL)
282 log_errx(1, "%s: improper port element nesting (%s)",
285 cur_port = calloc(1, sizeof(*cur_port));
286 if (cur_port == NULL)
287 log_err(1, "%s: cannot allocate %zd bytes", __func__,
290 devlist->num_ports++;
291 devlist->cur_port = cur_port;
293 STAILQ_INIT(&cur_port->attr_list);
294 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
296 for (i = 0; attr[i] != NULL; i += 2) {
297 if (strcmp(attr[i], "id") == 0) {
298 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
300 log_errx(1, "%s: invalid LUN attribute %s = %s",
301 __func__, attr[i], attr[i+1]);
308 cctl_end_pelement(void *user_data, const char *name)
310 struct cctl_devlist_data *devlist;
311 struct cctl_port *cur_port;
314 devlist = (struct cctl_devlist_data *)user_data;
315 cur_port = devlist->cur_port;
317 if ((cur_port == NULL)
318 && (strcmp(name, "ctlportlist") != 0))
319 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
321 if (devlist->cur_sb[devlist->level] == NULL)
322 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
323 devlist->level, name);
325 sbuf_finish(devlist->cur_sb[devlist->level]);
326 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
328 if (strlen(str) == 0) {
333 sbuf_delete(devlist->cur_sb[devlist->level]);
334 devlist->cur_sb[devlist->level] = NULL;
337 if (strcmp(name, "frontend_type") == 0) {
338 cur_port->port_frontend = str;
340 } else if (strcmp(name, "port_name") == 0) {
341 cur_port->port_name = str;
343 } else if (strcmp(name, "physical_port") == 0) {
344 cur_port->pp = strtoul(str, NULL, 0);
345 } else if (strcmp(name, "virtual_port") == 0) {
346 cur_port->vp = strtoul(str, NULL, 0);
347 } else if (strcmp(name, "cfiscsi_target") == 0) {
348 cur_port->cfiscsi_target = str;
350 } else if (strcmp(name, "cfiscsi_state") == 0) {
351 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
352 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
353 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
354 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
355 cur_port->ctld_portal_group_name = str;
357 } else if (strcmp(name, "targ_port") == 0) {
358 devlist->cur_port = NULL;
359 } else if (strcmp(name, "ctlportlist") == 0) {
362 struct cctl_lun_nv *nv;
364 nv = calloc(1, sizeof(*nv));
366 log_err(1, "%s: can't allocate %zd bytes for nv pair",
367 __func__, sizeof(*nv));
369 nv->name = checked_strdup(name);
373 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
380 cctl_char_handler(void *user_data, const XML_Char *str, int len)
382 struct cctl_devlist_data *devlist;
384 devlist = (struct cctl_devlist_data *)user_data;
386 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
390 conf_new_from_kernel(void)
392 struct conf *conf = NULL;
394 struct portal_group *pg;
399 struct ctl_lun_list list;
400 struct cctl_devlist_data devlist;
401 struct cctl_lun *lun;
402 struct cctl_port *port;
407 bzero(&devlist, sizeof(devlist));
408 STAILQ_INIT(&devlist.lun_list);
409 STAILQ_INIT(&devlist.port_list);
411 log_debugx("obtaining previously configured CTL luns from the kernel");
416 str = realloc(str, len);
418 log_err(1, "realloc");
420 bzero(&list, sizeof(list));
421 list.alloc_len = len;
422 list.status = CTL_LUN_LIST_NONE;
425 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
426 log_warn("error issuing CTL_LUN_LIST ioctl");
431 if (list.status == CTL_LUN_LIST_ERROR) {
432 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
438 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
443 parser = XML_ParserCreate(NULL);
444 if (parser == NULL) {
445 log_warnx("unable to create XML parser");
450 XML_SetUserData(parser, &devlist);
451 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
452 XML_SetCharacterDataHandler(parser, cctl_char_handler);
454 retval = XML_Parse(parser, str, strlen(str), 1);
455 XML_ParserFree(parser);
458 log_warnx("XML_Parse failed");
465 str = realloc(str, len);
467 log_err(1, "realloc");
469 bzero(&list, sizeof(list));
470 list.alloc_len = len;
471 list.status = CTL_LUN_LIST_NONE;
474 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
475 log_warn("error issuing CTL_PORT_LIST ioctl");
480 if (list.status == CTL_LUN_LIST_ERROR) {
481 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
487 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
492 parser = XML_ParserCreate(NULL);
493 if (parser == NULL) {
494 log_warnx("unable to create XML parser");
499 XML_SetUserData(parser, &devlist);
500 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
501 XML_SetCharacterDataHandler(parser, cctl_char_handler);
503 retval = XML_Parse(parser, str, strlen(str), 1);
504 XML_ParserFree(parser);
507 log_warnx("XML_Parse failed");
514 STAILQ_FOREACH(port, &devlist.port_list, links) {
515 if (strcmp(port->port_frontend, "ha") == 0)
519 if (port->pp == 0 && port->vp == 0)
520 name = checked_strdup(port->port_name);
521 else if (port->vp == 0)
522 asprintf(&name, "%s/%d", port->port_name, port->pp);
524 asprintf(&name, "%s/%d/%d", port->port_name, port->pp,
527 if (port->cfiscsi_target == NULL) {
528 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
529 port->port_id, name);
530 pp = pport_find(conf, name);
533 log_debugx("found new kernel port %u \"%s\"",
534 port->port_id, name);
536 pp = pport_new(conf, name, port->port_id);
538 log_warnx("pport_new failed");
544 if (port->cfiscsi_state != 1) {
545 log_debugx("CTL port %ju is not active (%d); ignoring",
546 (uintmax_t)port->port_id, port->cfiscsi_state);
550 targ = target_find(conf, port->cfiscsi_target);
553 log_debugx("found new kernel target %s for CTL port %ld",
554 port->cfiscsi_target, port->port_id);
556 targ = target_new(conf, port->cfiscsi_target);
558 log_warnx("target_new failed");
563 if (port->ctld_portal_group_name == NULL)
565 pg = portal_group_find(conf, port->ctld_portal_group_name);
568 log_debugx("found new kernel portal group %s for CTL port %ld",
569 port->ctld_portal_group_name, port->port_id);
571 pg = portal_group_new(conf, port->ctld_portal_group_name);
573 log_warnx("portal_group_new failed");
577 pg->pg_tag = port->cfiscsi_portal_group_tag;
578 cp = port_new(conf, targ, pg);
580 log_warnx("port_new failed");
583 cp->p_ctl_port = port->port_id;
588 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
589 struct cctl_lun_nv *nv;
591 if (lun->ctld_name == NULL) {
592 log_debugx("CTL lun %ju wasn't managed by ctld; "
593 "ignoring", (uintmax_t)lun->lun_id);
597 cl = lun_find(conf, lun->ctld_name);
599 log_warnx("found CTL lun %ju \"%s\", "
600 "also backed by CTL lun %d; ignoring",
601 (uintmax_t)lun->lun_id, lun->ctld_name,
606 log_debugx("found CTL lun %ju \"%s\"",
607 (uintmax_t)lun->lun_id, lun->ctld_name);
609 cl = lun_new(conf, lun->ctld_name);
611 log_warnx("lun_new failed");
614 lun_set_backend(cl, lun->backend_type);
615 lun_set_device_type(cl, lun->device_type);
616 lun_set_blocksize(cl, lun->blocksize);
617 lun_set_device_id(cl, lun->device_id);
618 lun_set_serial(cl, lun->serial_number);
619 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
620 lun_set_ctl_lun(cl, lun->lun_id);
622 STAILQ_FOREACH(nv, &lun->attr_list, links) {
623 if (strcmp(nv->name, "file") == 0 ||
624 strcmp(nv->name, "dev") == 0) {
625 lun_set_path(cl, nv->value);
628 o = option_new(&cl->l_options, nv->name, nv->value);
630 log_warnx("unable to add CTL lun option %s "
631 "for CTL lun %ju \"%s\"",
632 nv->name, (uintmax_t) lun->lun_id,
641 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
644 arg->namelen = strlen(name) + 1;
645 arg->name = __DECONST(char *, name);
646 arg->vallen = strlen(value) + 1;
647 arg->value = __DECONST(char *, value);
648 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
652 kernel_lun_add(struct lun *lun)
655 struct ctl_lun_req req;
656 int error, i, num_options;
658 bzero(&req, sizeof(req));
660 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
661 req.reqtype = CTL_LUNREQ_CREATE;
663 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
665 if (lun->l_size != 0)
666 req.reqdata.create.lun_size_bytes = lun->l_size;
668 if (lun->l_ctl_lun >= 0) {
669 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
670 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
673 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
674 req.reqdata.create.device_type = lun->l_device_type;
676 if (lun->l_serial != NULL) {
677 strncpy(req.reqdata.create.serial_num, lun->l_serial,
678 sizeof(req.reqdata.create.serial_num));
679 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
682 if (lun->l_device_id != NULL) {
683 strncpy(req.reqdata.create.device_id, lun->l_device_id,
684 sizeof(req.reqdata.create.device_id));
685 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
688 if (lun->l_path != NULL) {
689 o = option_find(&lun->l_options, "file");
691 option_set(o, lun->l_path);
693 o = option_new(&lun->l_options, "file", lun->l_path);
698 o = option_find(&lun->l_options, "ctld_name");
700 option_set(o, lun->l_name);
702 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
706 o = option_find(&lun->l_options, "scsiname");
707 if (o == NULL && lun->l_scsiname != NULL) {
708 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
713 TAILQ_FOREACH(o, &lun->l_options, o_next)
716 req.num_be_args = num_options;
717 if (num_options > 0) {
718 req.be_args = malloc(num_options * sizeof(*req.be_args));
719 if (req.be_args == NULL) {
720 log_warn("error allocating %zd bytes",
721 num_options * sizeof(*req.be_args));
726 TAILQ_FOREACH(o, &lun->l_options, o_next) {
727 str_arg(&req.be_args[i], o->o_name, o->o_value);
730 assert(i == num_options);
733 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
736 log_warn("error issuing CTL_LUN_REQ ioctl");
740 switch (req.status) {
742 log_warnx("LUN creation error: %s", req.error_str);
744 case CTL_LUN_WARNING:
745 log_warnx("LUN creation warning: %s", req.error_str);
750 log_warnx("unknown LUN creation status: %d",
755 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
760 kernel_lun_modify(struct lun *lun)
763 struct ctl_lun_req req;
764 int error, i, num_options;
766 bzero(&req, sizeof(req));
768 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
769 req.reqtype = CTL_LUNREQ_MODIFY;
771 req.reqdata.modify.lun_id = lun->l_ctl_lun;
772 req.reqdata.modify.lun_size_bytes = lun->l_size;
775 TAILQ_FOREACH(o, &lun->l_options, o_next)
778 req.num_be_args = num_options;
779 if (num_options > 0) {
780 req.be_args = malloc(num_options * sizeof(*req.be_args));
781 if (req.be_args == NULL) {
782 log_warn("error allocating %zd bytes",
783 num_options * sizeof(*req.be_args));
788 TAILQ_FOREACH(o, &lun->l_options, o_next) {
789 str_arg(&req.be_args[i], o->o_name, o->o_value);
792 assert(i == num_options);
795 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
798 log_warn("error issuing CTL_LUN_REQ ioctl");
802 switch (req.status) {
804 log_warnx("LUN modification error: %s", req.error_str);
806 case CTL_LUN_WARNING:
807 log_warnx("LUN modification warning: %s", req.error_str);
812 log_warnx("unknown LUN modification status: %d",
821 kernel_lun_remove(struct lun *lun)
823 struct ctl_lun_req req;
825 bzero(&req, sizeof(req));
827 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
828 req.reqtype = CTL_LUNREQ_RM;
830 req.reqdata.rm.lun_id = lun->l_ctl_lun;
832 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
833 log_warn("error issuing CTL_LUN_REQ ioctl");
837 switch (req.status) {
839 log_warnx("LUN removal error: %s", req.error_str);
841 case CTL_LUN_WARNING:
842 log_warnx("LUN removal warning: %s", req.error_str);
847 log_warnx("unknown LUN removal status: %d", req.status);
855 kernel_handoff(struct connection *conn)
857 struct ctl_iscsi req;
859 bzero(&req, sizeof(req));
861 req.type = CTL_ISCSI_HANDOFF;
862 strlcpy(req.data.handoff.initiator_name,
863 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
864 strlcpy(req.data.handoff.initiator_addr,
865 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
866 if (conn->conn_initiator_alias != NULL) {
867 strlcpy(req.data.handoff.initiator_alias,
868 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
870 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
871 sizeof(req.data.handoff.initiator_isid));
872 strlcpy(req.data.handoff.target_name,
873 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
874 #ifdef ICL_KERNEL_PROXY
876 req.data.handoff.connection_id = conn->conn_socket;
878 req.data.handoff.socket = conn->conn_socket;
880 req.data.handoff.socket = conn->conn_socket;
882 req.data.handoff.portal_group_tag =
883 conn->conn_portal->p_portal_group->pg_tag;
884 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
885 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
886 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
887 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
888 req.data.handoff.cmdsn = conn->conn_cmdsn;
889 req.data.handoff.statsn = conn->conn_statsn;
890 req.data.handoff.max_recv_data_segment_length =
891 conn->conn_max_data_segment_length;
892 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
893 req.data.handoff.immediate_data = conn->conn_immediate_data;
895 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
896 log_err(1, "error issuing CTL_ISCSI ioctl; "
897 "dropping connection");
900 if (req.status != CTL_ISCSI_OK) {
901 log_errx(1, "error returned from CTL iSCSI handoff request: "
902 "%s; dropping connection", req.error_str);
907 kernel_port_add(struct port *port)
910 struct ctl_port_entry entry;
912 struct ctl_lun_map lm;
913 struct target *targ = port->p_target;
914 struct portal_group *pg = port->p_portal_group;
918 /* Create iSCSI port. */
919 if (port->p_portal_group) {
920 bzero(&req, sizeof(req));
921 strlcpy(req.driver, "iscsi", sizeof(req.driver));
922 req.reqtype = CTL_REQ_CREATE;
924 TAILQ_FOREACH(o, &pg->pg_options, o_next)
926 req.args = malloc(req.num_args * sizeof(*req.args));
927 if (req.args == NULL)
928 log_err(1, "malloc");
930 req.args[n].namelen = sizeof("port_id");
931 req.args[n].name = __DECONST(char *, "port_id");
932 req.args[n].vallen = sizeof(port->p_ctl_port);
933 req.args[n].value = &port->p_ctl_port;
934 req.args[n++].flags = CTL_BEARG_WR;
935 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
936 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
937 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
939 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
940 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
941 TAILQ_FOREACH(o, &pg->pg_options, o_next)
942 str_arg(&req.args[n++], o->o_name, o->o_value);
944 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
947 log_warn("error issuing CTL_PORT_REQ ioctl");
950 if (req.status == CTL_LUN_ERROR) {
951 log_warnx("error returned from port creation request: %s",
955 if (req.status != CTL_LUN_OK) {
956 log_warnx("unknown port creation request status %d",
960 } else if (port->p_pport) {
961 port->p_ctl_port = port->p_pport->pp_ctl_port;
963 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
964 strlen(targ->t_name) == 20) {
965 bzero(&entry, sizeof(entry));
966 entry.port_type = CTL_PORT_NONE;
967 entry.targ_port = port->p_ctl_port;
968 entry.flags |= CTL_PORT_WWNN_VALID;
969 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
970 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
971 log_warn("CTL_SET_PORT_WWNS ioctl failed");
975 /* Explicitly enable mapping to block any access except allowed. */
976 lm.port = port->p_ctl_port;
977 lm.plun = UINT32_MAX;
979 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
981 log_warn("CTL_LUN_MAP ioctl failed");
983 /* Map configured LUNs */
984 for (i = 0; i < MAX_LUNS; i++) {
985 if (targ->t_luns[i] == NULL)
987 lm.port = port->p_ctl_port;
989 lm.lun = targ->t_luns[i]->l_ctl_lun;
990 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
992 log_warn("CTL_LUN_MAP ioctl failed");
996 bzero(&entry, sizeof(entry));
997 entry.targ_port = port->p_ctl_port;
998 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1000 log_warn("CTL_ENABLE_PORT ioctl failed");
1008 kernel_port_update(struct port *port, struct port *oport)
1010 struct ctl_lun_map lm;
1011 struct target *targ = port->p_target;
1012 struct target *otarg = oport->p_target;
1016 /* Map configured LUNs and unmap others */
1017 for (i = 0; i < MAX_LUNS; i++) {
1018 lm.port = port->p_ctl_port;
1020 if (targ->t_luns[i] == NULL)
1021 lm.lun = UINT32_MAX;
1023 lm.lun = targ->t_luns[i]->l_ctl_lun;
1024 if (otarg->t_luns[i] == NULL)
1027 olun = otarg->t_luns[i]->l_ctl_lun;
1030 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1032 log_warn("CTL_LUN_MAP ioctl failed");
1038 kernel_port_remove(struct port *port)
1040 struct ctl_port_entry entry;
1041 struct ctl_lun_map lm;
1044 struct target *targ = port->p_target;
1045 struct portal_group *pg = port->p_portal_group;
1049 bzero(&entry, sizeof(entry));
1050 entry.targ_port = port->p_ctl_port;
1051 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1053 log_warn("CTL_DISABLE_PORT ioctl failed");
1057 /* Remove iSCSI port. */
1058 if (port->p_portal_group) {
1059 bzero(&req, sizeof(req));
1060 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1061 req.reqtype = CTL_REQ_REMOVE;
1063 req.args = malloc(req.num_args * sizeof(*req.args));
1064 if (req.args == NULL)
1065 log_err(1, "malloc");
1066 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1067 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1068 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1069 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1072 log_warn("error issuing CTL_PORT_REQ ioctl");
1075 if (req.status == CTL_LUN_ERROR) {
1076 log_warnx("error returned from port removal request: %s",
1080 if (req.status != CTL_LUN_OK) {
1081 log_warnx("unknown port removal request status %d",
1086 /* Disable LUN mapping. */
1087 lm.port = port->p_ctl_port;
1088 lm.plun = UINT32_MAX;
1089 lm.lun = UINT32_MAX;
1090 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1092 log_warn("CTL_LUN_MAP ioctl failed");
1097 #ifdef ICL_KERNEL_PROXY
1099 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1101 struct ctl_iscsi req;
1103 bzero(&req, sizeof(req));
1105 req.type = CTL_ISCSI_LISTEN;
1106 req.data.listen.iser = iser;
1107 req.data.listen.domain = ai->ai_family;
1108 req.data.listen.socktype = ai->ai_socktype;
1109 req.data.listen.protocol = ai->ai_protocol;
1110 req.data.listen.addr = ai->ai_addr;
1111 req.data.listen.addrlen = ai->ai_addrlen;
1112 req.data.listen.portal_id = portal_id;
1114 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1115 log_err(1, "error issuing CTL_ISCSI ioctl");
1117 if (req.status != CTL_ISCSI_OK) {
1118 log_errx(1, "error returned from CTL iSCSI listen: %s",
1124 kernel_accept(int *connection_id, int *portal_id,
1125 struct sockaddr *client_sa, socklen_t *client_salen)
1127 struct ctl_iscsi req;
1128 struct sockaddr_storage ss;
1130 bzero(&req, sizeof(req));
1132 req.type = CTL_ISCSI_ACCEPT;
1133 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1135 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1136 log_err(1, "error issuing CTL_ISCSI ioctl");
1138 if (req.status != CTL_ISCSI_OK) {
1139 log_errx(1, "error returned from CTL iSCSI accept: %s",
1143 *connection_id = req.data.accept.connection_id;
1144 *portal_id = req.data.accept.portal_id;
1145 *client_salen = req.data.accept.initiator_addrlen;
1146 memcpy(client_sa, &ss, *client_salen);
1150 kernel_send(struct pdu *pdu)
1152 struct ctl_iscsi req;
1154 bzero(&req, sizeof(req));
1156 req.type = CTL_ISCSI_SEND;
1157 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1158 req.data.send.bhs = pdu->pdu_bhs;
1159 req.data.send.data_segment_len = pdu->pdu_data_len;
1160 req.data.send.data_segment = pdu->pdu_data;
1162 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1163 log_err(1, "error issuing CTL_ISCSI ioctl; "
1164 "dropping connection");
1167 if (req.status != CTL_ISCSI_OK) {
1168 log_errx(1, "error returned from CTL iSCSI send: "
1169 "%s; dropping connection", req.error_str);
1174 kernel_receive(struct pdu *pdu)
1176 struct ctl_iscsi req;
1178 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1179 if (pdu->pdu_data == NULL)
1180 log_err(1, "malloc");
1182 bzero(&req, sizeof(req));
1184 req.type = CTL_ISCSI_RECEIVE;
1185 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1186 req.data.receive.bhs = pdu->pdu_bhs;
1187 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1188 req.data.receive.data_segment = pdu->pdu_data;
1190 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1191 log_err(1, "error issuing CTL_ISCSI ioctl; "
1192 "dropping connection");
1195 if (req.status != CTL_ISCSI_OK) {
1196 log_errx(1, "error returned from CTL iSCSI receive: "
1197 "%s; dropping connection", req.error_str);
1202 #endif /* ICL_KERNEL_PROXY */
1205 * XXX: I CANT INTO LATIN
1208 kernel_capsicate(void)
1211 cap_rights_t rights;
1212 const unsigned long cmds[] = { CTL_ISCSI };
1214 cap_rights_init(&rights, CAP_IOCTL);
1215 error = cap_rights_limit(ctl_fd, &rights);
1216 if (error != 0 && errno != ENOSYS)
1217 log_err(1, "cap_rights_limit");
1219 error = cap_ioctls_limit(ctl_fd, cmds,
1220 sizeof(cmds) / sizeof(cmds[0]));
1221 if (error != 0 && errno != ENOSYS)
1222 log_err(1, "cap_ioctls_limit");
1224 error = cap_enter();
1225 if (error != 0 && errno != ENOSYS)
1226 log_err(1, "cap_enter");
1228 if (cap_sandboxed())
1229 log_debugx("Capsicum capability mode enabled");
1231 log_warnx("Capsicum capability mode not supported");