2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
5 * Copyright (c) 1997-2007 Kenneth D. Merry
6 * Copyright (c) 2012 The FreeBSD Foundation
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGES.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/capsicum.h>
44 #include <sys/callout.h>
45 #include <sys/ioctl.h>
46 #include <sys/linker.h>
47 #include <sys/module.h>
48 #include <sys/queue.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/ctl/ctl.h>
64 #include <cam/ctl/ctl_io.h>
65 #include <cam/ctl/ctl_backend.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_util.h>
68 #include <cam/ctl/ctl_scsi_all.h>
72 #ifdef ICL_KERNEL_PROXY
76 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 if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
100 log_warn("couldn't load cfiscsi");
107 * Name/value pair used for per-LUN attributes.
112 STAILQ_ENTRY(cctl_lun_nv) links;
116 * Backend LUN information.
122 uint64_t size_blocks;
127 STAILQ_HEAD(,cctl_lun_nv) attr_list;
128 STAILQ_ENTRY(cctl_lun) links;
138 char *cfiscsi_target;
139 uint16_t cfiscsi_portal_group_tag;
140 char *ctld_portal_group_name;
141 STAILQ_HEAD(,cctl_lun_nv) attr_list;
142 STAILQ_ENTRY(cctl_port) links;
145 struct cctl_devlist_data {
147 STAILQ_HEAD(,cctl_lun) lun_list;
148 struct cctl_lun *cur_lun;
150 STAILQ_HEAD(,cctl_port) port_list;
151 struct cctl_port *cur_port;
153 struct sbuf *cur_sb[32];
157 cctl_start_element(void *user_data, const char *name, const char **attr)
160 struct cctl_devlist_data *devlist;
161 struct cctl_lun *cur_lun;
163 devlist = (struct cctl_devlist_data *)user_data;
164 cur_lun = devlist->cur_lun;
166 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
167 sizeof(devlist->cur_sb[0])))
168 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
169 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
171 devlist->cur_sb[devlist->level] = sbuf_new_auto();
172 if (devlist->cur_sb[devlist->level] == NULL)
173 log_err(1, "%s: unable to allocate sbuf", __func__);
175 if (strcmp(name, "lun") == 0) {
177 log_errx(1, "%s: improper lun element nesting",
180 cur_lun = calloc(1, sizeof(*cur_lun));
182 log_err(1, "%s: cannot allocate %zd bytes", __func__,
186 devlist->cur_lun = cur_lun;
188 STAILQ_INIT(&cur_lun->attr_list);
189 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
191 for (i = 0; attr[i] != NULL; i += 2) {
192 if (strcmp(attr[i], "id") == 0) {
193 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
195 log_errx(1, "%s: invalid LUN attribute %s = %s",
196 __func__, attr[i], attr[i+1]);
203 cctl_end_element(void *user_data, const char *name)
205 struct cctl_devlist_data *devlist;
206 struct cctl_lun *cur_lun;
209 devlist = (struct cctl_devlist_data *)user_data;
210 cur_lun = devlist->cur_lun;
212 if ((cur_lun == NULL)
213 && (strcmp(name, "ctllunlist") != 0))
214 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
216 if (devlist->cur_sb[devlist->level] == NULL)
217 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
218 devlist->level, name);
220 sbuf_finish(devlist->cur_sb[devlist->level]);
221 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
223 if (strlen(str) == 0) {
228 sbuf_delete(devlist->cur_sb[devlist->level]);
229 devlist->cur_sb[devlist->level] = NULL;
232 if (strcmp(name, "backend_type") == 0) {
233 cur_lun->backend_type = str;
235 } else if (strcmp(name, "lun_type") == 0) {
236 cur_lun->device_type = strtoull(str, NULL, 0);
237 } else if (strcmp(name, "size") == 0) {
238 cur_lun->size_blocks = strtoull(str, NULL, 0);
239 } else if (strcmp(name, "blocksize") == 0) {
240 cur_lun->blocksize = strtoul(str, NULL, 0);
241 } else if (strcmp(name, "serial_number") == 0) {
242 cur_lun->serial_number = str;
244 } else if (strcmp(name, "device_id") == 0) {
245 cur_lun->device_id = str;
247 } else if (strcmp(name, "ctld_name") == 0) {
248 cur_lun->ctld_name = str;
250 } else if (strcmp(name, "lun") == 0) {
251 devlist->cur_lun = NULL;
252 } else if (strcmp(name, "ctllunlist") == 0) {
255 struct cctl_lun_nv *nv;
257 nv = calloc(1, sizeof(*nv));
259 log_err(1, "%s: can't allocate %zd bytes for nv pair",
260 __func__, sizeof(*nv));
262 nv->name = checked_strdup(name);
266 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
273 cctl_start_pelement(void *user_data, const char *name, const char **attr)
276 struct cctl_devlist_data *devlist;
277 struct cctl_port *cur_port;
279 devlist = (struct cctl_devlist_data *)user_data;
280 cur_port = devlist->cur_port;
282 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
283 sizeof(devlist->cur_sb[0])))
284 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
285 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
287 devlist->cur_sb[devlist->level] = sbuf_new_auto();
288 if (devlist->cur_sb[devlist->level] == NULL)
289 log_err(1, "%s: unable to allocate sbuf", __func__);
291 if (strcmp(name, "targ_port") == 0) {
292 if (cur_port != NULL)
293 log_errx(1, "%s: improper port element nesting (%s)",
296 cur_port = calloc(1, sizeof(*cur_port));
297 if (cur_port == NULL)
298 log_err(1, "%s: cannot allocate %zd bytes", __func__,
301 devlist->num_ports++;
302 devlist->cur_port = cur_port;
304 STAILQ_INIT(&cur_port->attr_list);
305 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
307 for (i = 0; attr[i] != NULL; i += 2) {
308 if (strcmp(attr[i], "id") == 0) {
309 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
311 log_errx(1, "%s: invalid LUN attribute %s = %s",
312 __func__, attr[i], attr[i+1]);
319 cctl_end_pelement(void *user_data, const char *name)
321 struct cctl_devlist_data *devlist;
322 struct cctl_port *cur_port;
325 devlist = (struct cctl_devlist_data *)user_data;
326 cur_port = devlist->cur_port;
328 if ((cur_port == NULL)
329 && (strcmp(name, "ctlportlist") != 0))
330 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
332 if (devlist->cur_sb[devlist->level] == NULL)
333 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
334 devlist->level, name);
336 sbuf_finish(devlist->cur_sb[devlist->level]);
337 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
339 if (strlen(str) == 0) {
344 sbuf_delete(devlist->cur_sb[devlist->level]);
345 devlist->cur_sb[devlist->level] = NULL;
348 if (strcmp(name, "frontend_type") == 0) {
349 cur_port->port_frontend = str;
351 } else if (strcmp(name, "port_name") == 0) {
352 cur_port->port_name = str;
354 } else if (strcmp(name, "physical_port") == 0) {
355 cur_port->pp = strtoul(str, NULL, 0);
356 } else if (strcmp(name, "virtual_port") == 0) {
357 cur_port->vp = strtoul(str, NULL, 0);
358 } else if (strcmp(name, "cfiscsi_target") == 0) {
359 cur_port->cfiscsi_target = str;
361 } else if (strcmp(name, "cfiscsi_state") == 0) {
362 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
363 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
364 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
365 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
366 cur_port->ctld_portal_group_name = str;
368 } else if (strcmp(name, "targ_port") == 0) {
369 devlist->cur_port = NULL;
370 } else if (strcmp(name, "ctlportlist") == 0) {
373 struct cctl_lun_nv *nv;
375 nv = calloc(1, sizeof(*nv));
377 log_err(1, "%s: can't allocate %zd bytes for nv pair",
378 __func__, sizeof(*nv));
380 nv->name = checked_strdup(name);
384 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
391 cctl_char_handler(void *user_data, const XML_Char *str, int len)
393 struct cctl_devlist_data *devlist;
395 devlist = (struct cctl_devlist_data *)user_data;
397 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
401 conf_new_from_kernel(void)
403 struct conf *conf = NULL;
405 struct portal_group *pg;
410 struct ctl_lun_list list;
411 struct cctl_devlist_data devlist;
412 struct cctl_lun *lun;
413 struct cctl_port *port;
418 bzero(&devlist, sizeof(devlist));
419 STAILQ_INIT(&devlist.lun_list);
420 STAILQ_INIT(&devlist.port_list);
422 log_debugx("obtaining previously configured CTL luns from the kernel");
427 str = realloc(str, len);
429 log_err(1, "realloc");
431 bzero(&list, sizeof(list));
432 list.alloc_len = len;
433 list.status = CTL_LUN_LIST_NONE;
436 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
437 log_warn("error issuing CTL_LUN_LIST ioctl");
442 if (list.status == CTL_LUN_LIST_ERROR) {
443 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
449 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
454 parser = XML_ParserCreate(NULL);
455 if (parser == NULL) {
456 log_warnx("unable to create XML parser");
461 XML_SetUserData(parser, &devlist);
462 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
463 XML_SetCharacterDataHandler(parser, cctl_char_handler);
465 retval = XML_Parse(parser, str, strlen(str), 1);
466 XML_ParserFree(parser);
469 log_warnx("XML_Parse failed");
476 str = realloc(str, len);
478 log_err(1, "realloc");
480 bzero(&list, sizeof(list));
481 list.alloc_len = len;
482 list.status = CTL_LUN_LIST_NONE;
485 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
486 log_warn("error issuing CTL_PORT_LIST ioctl");
491 if (list.status == CTL_LUN_LIST_ERROR) {
492 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
498 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
503 parser = XML_ParserCreate(NULL);
504 if (parser == NULL) {
505 log_warnx("unable to create XML parser");
510 XML_SetUserData(parser, &devlist);
511 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
512 XML_SetCharacterDataHandler(parser, cctl_char_handler);
514 retval = XML_Parse(parser, str, strlen(str), 1);
515 XML_ParserFree(parser);
518 log_warnx("XML_Parse failed");
525 STAILQ_FOREACH(port, &devlist.port_list, links) {
526 if (strcmp(port->port_frontend, "ha") == 0)
529 if (port->pp == 0 && port->vp == 0) {
530 name = checked_strdup(port->port_name);
531 } else if (port->vp == 0) {
532 retval = asprintf(&name, "%s/%d",
533 port->port_name, port->pp);
535 log_err(1, "asprintf");
537 retval = asprintf(&name, "%s/%d/%d",
538 port->port_name, port->pp, port->vp);
540 log_err(1, "asprintf");
543 if (port->cfiscsi_target == NULL) {
544 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
545 port->port_id, name);
546 pp = pport_find(conf, name);
549 log_debugx("found new kernel port %u \"%s\"",
550 port->port_id, name);
552 pp = pport_new(conf, name, port->port_id);
554 log_warnx("pport_new failed");
560 if (port->cfiscsi_state != 1) {
561 log_debugx("CTL port %ju is not active (%d); ignoring",
562 (uintmax_t)port->port_id, port->cfiscsi_state);
566 targ = target_find(conf, port->cfiscsi_target);
569 log_debugx("found new kernel target %s for CTL port %ld",
570 port->cfiscsi_target, port->port_id);
572 targ = target_new(conf, port->cfiscsi_target);
574 log_warnx("target_new failed");
579 if (port->ctld_portal_group_name == NULL)
581 pg = portal_group_find(conf, port->ctld_portal_group_name);
584 log_debugx("found new kernel portal group %s for CTL port %ld",
585 port->ctld_portal_group_name, port->port_id);
587 pg = portal_group_new(conf, port->ctld_portal_group_name);
589 log_warnx("portal_group_new failed");
593 pg->pg_tag = port->cfiscsi_portal_group_tag;
594 cp = port_new(conf, targ, pg);
596 log_warnx("port_new failed");
599 cp->p_ctl_port = port->port_id;
603 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
604 struct cctl_lun_nv *nv;
606 if (lun->ctld_name == NULL) {
607 log_debugx("CTL lun %ju wasn't managed by ctld; "
608 "ignoring", (uintmax_t)lun->lun_id);
612 cl = lun_find(conf, lun->ctld_name);
614 log_warnx("found CTL lun %ju \"%s\", "
615 "also backed by CTL lun %d; ignoring",
616 (uintmax_t)lun->lun_id, lun->ctld_name,
621 log_debugx("found CTL lun %ju \"%s\"",
622 (uintmax_t)lun->lun_id, lun->ctld_name);
624 cl = lun_new(conf, lun->ctld_name);
626 log_warnx("lun_new failed");
629 lun_set_backend(cl, lun->backend_type);
630 lun_set_device_type(cl, lun->device_type);
631 lun_set_blocksize(cl, lun->blocksize);
632 lun_set_device_id(cl, lun->device_id);
633 lun_set_serial(cl, lun->serial_number);
634 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
635 lun_set_ctl_lun(cl, lun->lun_id);
637 STAILQ_FOREACH(nv, &lun->attr_list, links) {
638 if (strcmp(nv->name, "file") == 0 ||
639 strcmp(nv->name, "dev") == 0) {
640 lun_set_path(cl, nv->value);
643 o = option_new(&cl->l_options, nv->name, nv->value);
645 log_warnx("unable to add CTL lun option %s "
646 "for CTL lun %ju \"%s\"",
647 nv->name, (uintmax_t) lun->lun_id,
656 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
659 arg->namelen = strlen(name) + 1;
660 arg->name = __DECONST(char *, name);
661 arg->vallen = strlen(value) + 1;
662 arg->value = __DECONST(char *, value);
663 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
667 kernel_lun_add(struct lun *lun)
670 struct ctl_lun_req req;
671 int error, i, num_options;
673 bzero(&req, sizeof(req));
675 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
676 req.reqtype = CTL_LUNREQ_CREATE;
678 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
680 if (lun->l_size != 0)
681 req.reqdata.create.lun_size_bytes = lun->l_size;
683 if (lun->l_ctl_lun >= 0) {
684 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
685 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
688 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
689 req.reqdata.create.device_type = lun->l_device_type;
691 if (lun->l_serial != NULL) {
692 strncpy(req.reqdata.create.serial_num, lun->l_serial,
693 sizeof(req.reqdata.create.serial_num));
694 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
697 if (lun->l_device_id != NULL) {
698 strncpy(req.reqdata.create.device_id, lun->l_device_id,
699 sizeof(req.reqdata.create.device_id));
700 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
703 if (lun->l_path != NULL) {
704 o = option_find(&lun->l_options, "file");
706 option_set(o, lun->l_path);
708 o = option_new(&lun->l_options, "file", lun->l_path);
713 o = option_find(&lun->l_options, "ctld_name");
715 option_set(o, lun->l_name);
717 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
721 o = option_find(&lun->l_options, "scsiname");
722 if (o == NULL && lun->l_scsiname != NULL) {
723 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
728 TAILQ_FOREACH(o, &lun->l_options, o_next)
731 req.num_be_args = num_options;
732 if (num_options > 0) {
733 req.be_args = malloc(num_options * sizeof(*req.be_args));
734 if (req.be_args == NULL) {
735 log_warn("error allocating %zd bytes",
736 num_options * sizeof(*req.be_args));
741 TAILQ_FOREACH(o, &lun->l_options, o_next) {
742 str_arg(&req.be_args[i], o->o_name, o->o_value);
745 assert(i == num_options);
748 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
751 log_warn("error issuing CTL_LUN_REQ ioctl");
755 switch (req.status) {
757 log_warnx("LUN creation error: %s", req.error_str);
759 case CTL_LUN_WARNING:
760 log_warnx("LUN creation warning: %s", req.error_str);
765 log_warnx("unknown LUN creation status: %d",
770 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
775 kernel_lun_modify(struct lun *lun)
778 struct ctl_lun_req req;
779 int error, i, num_options;
781 bzero(&req, sizeof(req));
783 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
784 req.reqtype = CTL_LUNREQ_MODIFY;
786 req.reqdata.modify.lun_id = lun->l_ctl_lun;
787 req.reqdata.modify.lun_size_bytes = lun->l_size;
790 TAILQ_FOREACH(o, &lun->l_options, o_next)
793 req.num_be_args = num_options;
794 if (num_options > 0) {
795 req.be_args = malloc(num_options * sizeof(*req.be_args));
796 if (req.be_args == NULL) {
797 log_warn("error allocating %zd bytes",
798 num_options * sizeof(*req.be_args));
803 TAILQ_FOREACH(o, &lun->l_options, o_next) {
804 str_arg(&req.be_args[i], o->o_name, o->o_value);
807 assert(i == num_options);
810 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
813 log_warn("error issuing CTL_LUN_REQ ioctl");
817 switch (req.status) {
819 log_warnx("LUN modification error: %s", req.error_str);
821 case CTL_LUN_WARNING:
822 log_warnx("LUN modification warning: %s", req.error_str);
827 log_warnx("unknown LUN modification status: %d",
836 kernel_lun_remove(struct lun *lun)
838 struct ctl_lun_req req;
840 bzero(&req, sizeof(req));
842 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
843 req.reqtype = CTL_LUNREQ_RM;
845 req.reqdata.rm.lun_id = lun->l_ctl_lun;
847 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
848 log_warn("error issuing CTL_LUN_REQ ioctl");
852 switch (req.status) {
854 log_warnx("LUN removal error: %s", req.error_str);
856 case CTL_LUN_WARNING:
857 log_warnx("LUN removal warning: %s", req.error_str);
862 log_warnx("unknown LUN removal status: %d", req.status);
870 kernel_handoff(struct connection *conn)
872 struct ctl_iscsi req;
874 bzero(&req, sizeof(req));
876 req.type = CTL_ISCSI_HANDOFF;
877 strlcpy(req.data.handoff.initiator_name,
878 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
879 strlcpy(req.data.handoff.initiator_addr,
880 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
881 if (conn->conn_initiator_alias != NULL) {
882 strlcpy(req.data.handoff.initiator_alias,
883 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
885 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
886 sizeof(req.data.handoff.initiator_isid));
887 strlcpy(req.data.handoff.target_name,
888 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
889 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
890 strlcpy(req.data.handoff.offload,
891 conn->conn_portal->p_portal_group->pg_offload,
892 sizeof(req.data.handoff.offload));
894 #ifdef ICL_KERNEL_PROXY
896 req.data.handoff.connection_id = conn->conn_socket;
898 req.data.handoff.socket = conn->conn_socket;
900 req.data.handoff.socket = conn->conn_socket;
902 req.data.handoff.portal_group_tag =
903 conn->conn_portal->p_portal_group->pg_tag;
904 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
905 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
906 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
907 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
908 req.data.handoff.cmdsn = conn->conn_cmdsn;
909 req.data.handoff.statsn = conn->conn_statsn;
910 req.data.handoff.max_recv_data_segment_length =
911 conn->conn_max_recv_data_segment_length;
912 req.data.handoff.max_send_data_segment_length =
913 conn->conn_max_send_data_segment_length;
914 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
915 req.data.handoff.first_burst_length = conn->conn_first_burst_length;
916 req.data.handoff.immediate_data = conn->conn_immediate_data;
918 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
919 log_err(1, "error issuing CTL_ISCSI ioctl; "
920 "dropping connection");
923 if (req.status != CTL_ISCSI_OK) {
924 log_errx(1, "error returned from CTL iSCSI handoff request: "
925 "%s; dropping connection", req.error_str);
930 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
931 int *max_burst_length, int *first_burst_length)
933 struct ctl_iscsi req;
934 struct ctl_iscsi_limits_params *cilp;
936 bzero(&req, sizeof(req));
938 req.type = CTL_ISCSI_LIMITS;
939 cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
940 if (offload != NULL) {
941 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
944 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
945 log_err(1, "error issuing CTL_ISCSI ioctl; "
946 "dropping connection");
949 if (req.status != CTL_ISCSI_OK) {
950 log_errx(1, "error returned from CTL iSCSI limits request: "
951 "%s; dropping connection", req.error_str);
954 if (cilp->max_recv_data_segment_length != 0) {
955 *max_recv_dsl = cilp->max_recv_data_segment_length;
956 *max_send_dsl = cilp->max_recv_data_segment_length;
958 if (cilp->max_send_data_segment_length != 0)
959 *max_send_dsl = cilp->max_send_data_segment_length;
960 if (cilp->max_burst_length != 0)
961 *max_burst_length = cilp->max_burst_length;
962 if (cilp->first_burst_length != 0)
963 *first_burst_length = cilp->first_burst_length;
964 if (*max_burst_length < *first_burst_length)
965 *first_burst_length = *max_burst_length;
967 if (offload != NULL) {
968 log_debugx("Kernel limits for offload \"%s\" are "
969 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
970 "MaxBurstLength=%d, FirstBurstLength=%d",
971 offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
972 *first_burst_length);
974 log_debugx("Kernel limits are "
975 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
976 "MaxBurstLength=%d, FirstBurstLength=%d",
977 *max_recv_dsl, *max_send_dsl, *max_burst_length,
978 *first_burst_length);
983 kernel_port_add(struct port *port)
986 struct ctl_port_entry entry;
988 struct ctl_lun_map lm;
989 struct target *targ = port->p_target;
990 struct portal_group *pg = port->p_portal_group;
994 /* Create iSCSI port. */
995 if (port->p_portal_group) {
996 bzero(&req, sizeof(req));
997 strlcpy(req.driver, "iscsi", sizeof(req.driver));
998 req.reqtype = CTL_REQ_CREATE;
1000 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1002 req.args = malloc(req.num_args * sizeof(*req.args));
1003 if (req.args == NULL)
1004 log_err(1, "malloc");
1006 req.args[n].namelen = sizeof("port_id");
1007 req.args[n].name = __DECONST(char *, "port_id");
1008 req.args[n].vallen = sizeof(port->p_ctl_port);
1009 req.args[n].value = &port->p_ctl_port;
1010 req.args[n++].flags = CTL_BEARG_WR;
1011 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
1012 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1013 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
1015 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
1016 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
1017 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1018 str_arg(&req.args[n++], o->o_name, o->o_value);
1020 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1023 log_warn("error issuing CTL_PORT_REQ ioctl");
1026 if (req.status == CTL_LUN_ERROR) {
1027 log_warnx("error returned from port creation request: %s",
1031 if (req.status != CTL_LUN_OK) {
1032 log_warnx("unknown port creation request status %d",
1036 } else if (port->p_pport) {
1037 port->p_ctl_port = port->p_pport->pp_ctl_port;
1039 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1040 strlen(targ->t_name) == 20) {
1041 bzero(&entry, sizeof(entry));
1042 entry.port_type = CTL_PORT_NONE;
1043 entry.targ_port = port->p_ctl_port;
1044 entry.flags |= CTL_PORT_WWNN_VALID;
1045 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1046 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1047 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1051 /* Explicitly enable mapping to block any access except allowed. */
1052 lm.port = port->p_ctl_port;
1053 lm.plun = UINT32_MAX;
1055 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1057 log_warn("CTL_LUN_MAP ioctl failed");
1059 /* Map configured LUNs */
1060 for (i = 0; i < MAX_LUNS; i++) {
1061 if (targ->t_luns[i] == NULL)
1063 lm.port = port->p_ctl_port;
1065 lm.lun = targ->t_luns[i]->l_ctl_lun;
1066 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1068 log_warn("CTL_LUN_MAP ioctl failed");
1072 bzero(&entry, sizeof(entry));
1073 entry.targ_port = port->p_ctl_port;
1074 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1076 log_warn("CTL_ENABLE_PORT ioctl failed");
1084 kernel_port_update(struct port *port, struct port *oport)
1086 struct ctl_lun_map lm;
1087 struct target *targ = port->p_target;
1088 struct target *otarg = oport->p_target;
1092 /* Map configured LUNs and unmap others */
1093 for (i = 0; i < MAX_LUNS; i++) {
1094 lm.port = port->p_ctl_port;
1096 if (targ->t_luns[i] == NULL)
1097 lm.lun = UINT32_MAX;
1099 lm.lun = targ->t_luns[i]->l_ctl_lun;
1100 if (otarg->t_luns[i] == NULL)
1103 olun = otarg->t_luns[i]->l_ctl_lun;
1106 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1108 log_warn("CTL_LUN_MAP ioctl failed");
1114 kernel_port_remove(struct port *port)
1116 struct ctl_port_entry entry;
1117 struct ctl_lun_map lm;
1120 struct target *targ = port->p_target;
1121 struct portal_group *pg = port->p_portal_group;
1125 bzero(&entry, sizeof(entry));
1126 entry.targ_port = port->p_ctl_port;
1127 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1129 log_warn("CTL_DISABLE_PORT ioctl failed");
1133 /* Remove iSCSI port. */
1134 if (port->p_portal_group) {
1135 bzero(&req, sizeof(req));
1136 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1137 req.reqtype = CTL_REQ_REMOVE;
1139 req.args = malloc(req.num_args * sizeof(*req.args));
1140 if (req.args == NULL)
1141 log_err(1, "malloc");
1142 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1143 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1144 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1145 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1148 log_warn("error issuing CTL_PORT_REQ ioctl");
1151 if (req.status == CTL_LUN_ERROR) {
1152 log_warnx("error returned from port removal request: %s",
1156 if (req.status != CTL_LUN_OK) {
1157 log_warnx("unknown port removal request status %d",
1162 /* Disable LUN mapping. */
1163 lm.port = port->p_ctl_port;
1164 lm.plun = UINT32_MAX;
1165 lm.lun = UINT32_MAX;
1166 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1168 log_warn("CTL_LUN_MAP ioctl failed");
1173 #ifdef ICL_KERNEL_PROXY
1175 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1177 struct ctl_iscsi req;
1179 bzero(&req, sizeof(req));
1181 req.type = CTL_ISCSI_LISTEN;
1182 req.data.listen.iser = iser;
1183 req.data.listen.domain = ai->ai_family;
1184 req.data.listen.socktype = ai->ai_socktype;
1185 req.data.listen.protocol = ai->ai_protocol;
1186 req.data.listen.addr = ai->ai_addr;
1187 req.data.listen.addrlen = ai->ai_addrlen;
1188 req.data.listen.portal_id = portal_id;
1190 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1191 log_err(1, "error issuing CTL_ISCSI ioctl");
1193 if (req.status != CTL_ISCSI_OK) {
1194 log_errx(1, "error returned from CTL iSCSI listen: %s",
1200 kernel_accept(int *connection_id, int *portal_id,
1201 struct sockaddr *client_sa, socklen_t *client_salen)
1203 struct ctl_iscsi req;
1204 struct sockaddr_storage ss;
1206 bzero(&req, sizeof(req));
1208 req.type = CTL_ISCSI_ACCEPT;
1209 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1211 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1212 log_err(1, "error issuing CTL_ISCSI ioctl");
1214 if (req.status != CTL_ISCSI_OK) {
1215 log_errx(1, "error returned from CTL iSCSI accept: %s",
1219 *connection_id = req.data.accept.connection_id;
1220 *portal_id = req.data.accept.portal_id;
1221 *client_salen = req.data.accept.initiator_addrlen;
1222 memcpy(client_sa, &ss, *client_salen);
1226 kernel_send(struct pdu *pdu)
1228 struct ctl_iscsi req;
1230 bzero(&req, sizeof(req));
1232 req.type = CTL_ISCSI_SEND;
1233 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1234 req.data.send.bhs = pdu->pdu_bhs;
1235 req.data.send.data_segment_len = pdu->pdu_data_len;
1236 req.data.send.data_segment = pdu->pdu_data;
1238 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1239 log_err(1, "error issuing CTL_ISCSI ioctl; "
1240 "dropping connection");
1243 if (req.status != CTL_ISCSI_OK) {
1244 log_errx(1, "error returned from CTL iSCSI send: "
1245 "%s; dropping connection", req.error_str);
1250 kernel_receive(struct pdu *pdu)
1252 struct connection *conn;
1253 struct ctl_iscsi req;
1255 conn = pdu->pdu_connection;
1256 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1257 if (pdu->pdu_data == NULL)
1258 log_err(1, "malloc");
1260 bzero(&req, sizeof(req));
1262 req.type = CTL_ISCSI_RECEIVE;
1263 req.data.receive.connection_id = conn->conn_socket;
1264 req.data.receive.bhs = pdu->pdu_bhs;
1265 req.data.receive.data_segment_len =
1266 conn->conn_max_recv_data_segment_length;
1267 req.data.receive.data_segment = pdu->pdu_data;
1269 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1270 log_err(1, "error issuing CTL_ISCSI ioctl; "
1271 "dropping connection");
1274 if (req.status != CTL_ISCSI_OK) {
1275 log_errx(1, "error returned from CTL iSCSI receive: "
1276 "%s; dropping connection", req.error_str);
1281 #endif /* ICL_KERNEL_PROXY */
1284 * XXX: I CANT INTO LATIN
1287 kernel_capsicate(void)
1290 cap_rights_t rights;
1291 const unsigned long cmds[] = { CTL_ISCSI };
1293 cap_rights_init(&rights, CAP_IOCTL);
1294 error = cap_rights_limit(ctl_fd, &rights);
1295 if (error != 0 && errno != ENOSYS)
1296 log_err(1, "cap_rights_limit");
1298 error = cap_ioctls_limit(ctl_fd, cmds, nitems(cmds));
1300 if (error != 0 && errno != ENOSYS)
1301 log_err(1, "cap_ioctls_limit");
1303 error = cap_enter();
1304 if (error != 0 && errno != ENOSYS)
1305 log_err(1, "cap_enter");
1307 if (cap_sandboxed())
1308 log_debugx("Capsicum capability mode enabled");
1310 log_warnx("Capsicum capability mode not supported");