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
7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions, and the following disclaimer,
18 * without modification.
19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20 * substantially similar to the "NO WARRANTY" disclaimer below
21 * ("Disclaimer") and any redistribution must be conditioned upon
22 * including a substantially similar Disclaimer requirement for further
23 * binary redistribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGES.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/capsicum.h>
45 #include <sys/callout.h>
46 #include <sys/ioctl.h>
47 #include <sys/linker.h>
48 #include <sys/module.h>
49 #include <sys/queue.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/ctl/ctl.h>
66 #include <cam/ctl/ctl_io.h>
67 #include <cam/ctl/ctl_backend.h>
68 #include <cam/ctl/ctl_ioctl.h>
69 #include <cam/ctl/ctl_util.h>
70 #include <cam/ctl/ctl_scsi_all.h>
74 #ifdef ICL_KERNEL_PROXY
78 #define NVLIST_BUFSIZE 1024
80 extern bool proxy_mode;
82 static int ctl_fd = 0;
87 int retval, saved_errno;
89 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
90 if (ctl_fd < 0 && errno == ENOENT) {
92 retval = kldload("ctl");
94 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
99 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
103 if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
104 log_warn("couldn't load cfiscsi");
111 * Name/value pair used for per-LUN attributes.
116 STAILQ_ENTRY(cctl_lun_nv) links;
120 * Backend LUN information.
126 uint64_t size_blocks;
131 STAILQ_HEAD(,cctl_lun_nv) attr_list;
132 STAILQ_ENTRY(cctl_lun) links;
142 char *cfiscsi_target;
143 uint16_t cfiscsi_portal_group_tag;
144 char *ctld_portal_group_name;
145 STAILQ_HEAD(,cctl_lun_nv) attr_list;
146 STAILQ_ENTRY(cctl_port) links;
149 struct cctl_devlist_data {
151 STAILQ_HEAD(,cctl_lun) lun_list;
152 struct cctl_lun *cur_lun;
154 STAILQ_HEAD(,cctl_port) port_list;
155 struct cctl_port *cur_port;
157 struct sbuf *cur_sb[32];
161 cctl_start_element(void *user_data, const char *name, const char **attr)
164 struct cctl_devlist_data *devlist;
165 struct cctl_lun *cur_lun;
167 devlist = (struct cctl_devlist_data *)user_data;
168 cur_lun = devlist->cur_lun;
170 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
171 sizeof(devlist->cur_sb[0])))
172 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
173 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
175 devlist->cur_sb[devlist->level] = sbuf_new_auto();
176 if (devlist->cur_sb[devlist->level] == NULL)
177 log_err(1, "%s: unable to allocate sbuf", __func__);
179 if (strcmp(name, "lun") == 0) {
181 log_errx(1, "%s: improper lun element nesting",
184 cur_lun = calloc(1, sizeof(*cur_lun));
186 log_err(1, "%s: cannot allocate %zd bytes", __func__,
190 devlist->cur_lun = cur_lun;
192 STAILQ_INIT(&cur_lun->attr_list);
193 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
195 for (i = 0; attr[i] != NULL; i += 2) {
196 if (strcmp(attr[i], "id") == 0) {
197 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
199 log_errx(1, "%s: invalid LUN attribute %s = %s",
200 __func__, attr[i], attr[i+1]);
207 cctl_end_element(void *user_data, const char *name)
209 struct cctl_devlist_data *devlist;
210 struct cctl_lun *cur_lun;
213 devlist = (struct cctl_devlist_data *)user_data;
214 cur_lun = devlist->cur_lun;
216 if ((cur_lun == NULL)
217 && (strcmp(name, "ctllunlist") != 0))
218 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
220 if (devlist->cur_sb[devlist->level] == NULL)
221 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
222 devlist->level, name);
224 sbuf_finish(devlist->cur_sb[devlist->level]);
225 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
227 if (strlen(str) == 0) {
232 sbuf_delete(devlist->cur_sb[devlist->level]);
233 devlist->cur_sb[devlist->level] = NULL;
236 if (strcmp(name, "backend_type") == 0) {
237 cur_lun->backend_type = str;
239 } else if (strcmp(name, "lun_type") == 0) {
240 cur_lun->device_type = strtoull(str, NULL, 0);
241 } else if (strcmp(name, "size") == 0) {
242 cur_lun->size_blocks = strtoull(str, NULL, 0);
243 } else if (strcmp(name, "blocksize") == 0) {
244 cur_lun->blocksize = strtoul(str, NULL, 0);
245 } else if (strcmp(name, "serial_number") == 0) {
246 cur_lun->serial_number = str;
248 } else if (strcmp(name, "device_id") == 0) {
249 cur_lun->device_id = str;
251 } else if (strcmp(name, "ctld_name") == 0) {
252 cur_lun->ctld_name = str;
254 } else if (strcmp(name, "lun") == 0) {
255 devlist->cur_lun = NULL;
256 } else if (strcmp(name, "ctllunlist") == 0) {
259 struct cctl_lun_nv *nv;
261 nv = calloc(1, sizeof(*nv));
263 log_err(1, "%s: can't allocate %zd bytes for nv pair",
264 __func__, sizeof(*nv));
266 nv->name = checked_strdup(name);
270 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
277 cctl_start_pelement(void *user_data, const char *name, const char **attr)
280 struct cctl_devlist_data *devlist;
281 struct cctl_port *cur_port;
283 devlist = (struct cctl_devlist_data *)user_data;
284 cur_port = devlist->cur_port;
286 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
287 sizeof(devlist->cur_sb[0])))
288 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
289 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
291 devlist->cur_sb[devlist->level] = sbuf_new_auto();
292 if (devlist->cur_sb[devlist->level] == NULL)
293 log_err(1, "%s: unable to allocate sbuf", __func__);
295 if (strcmp(name, "targ_port") == 0) {
296 if (cur_port != NULL)
297 log_errx(1, "%s: improper port element nesting (%s)",
300 cur_port = calloc(1, sizeof(*cur_port));
301 if (cur_port == NULL)
302 log_err(1, "%s: cannot allocate %zd bytes", __func__,
305 devlist->num_ports++;
306 devlist->cur_port = cur_port;
308 STAILQ_INIT(&cur_port->attr_list);
309 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
311 for (i = 0; attr[i] != NULL; i += 2) {
312 if (strcmp(attr[i], "id") == 0) {
313 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
315 log_errx(1, "%s: invalid LUN attribute %s = %s",
316 __func__, attr[i], attr[i+1]);
323 cctl_end_pelement(void *user_data, const char *name)
325 struct cctl_devlist_data *devlist;
326 struct cctl_port *cur_port;
329 devlist = (struct cctl_devlist_data *)user_data;
330 cur_port = devlist->cur_port;
332 if ((cur_port == NULL)
333 && (strcmp(name, "ctlportlist") != 0))
334 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
336 if (devlist->cur_sb[devlist->level] == NULL)
337 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
338 devlist->level, name);
340 sbuf_finish(devlist->cur_sb[devlist->level]);
341 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
343 if (strlen(str) == 0) {
348 sbuf_delete(devlist->cur_sb[devlist->level]);
349 devlist->cur_sb[devlist->level] = NULL;
352 if (strcmp(name, "frontend_type") == 0) {
353 cur_port->port_frontend = str;
355 } else if (strcmp(name, "port_name") == 0) {
356 cur_port->port_name = str;
358 } else if (strcmp(name, "physical_port") == 0) {
359 cur_port->pp = strtoul(str, NULL, 0);
360 } else if (strcmp(name, "virtual_port") == 0) {
361 cur_port->vp = strtoul(str, NULL, 0);
362 } else if (strcmp(name, "cfiscsi_target") == 0) {
363 cur_port->cfiscsi_target = str;
365 } else if (strcmp(name, "cfiscsi_state") == 0) {
366 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
367 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
368 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
369 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
370 cur_port->ctld_portal_group_name = str;
372 } else if (strcmp(name, "targ_port") == 0) {
373 devlist->cur_port = NULL;
374 } else if (strcmp(name, "ctlportlist") == 0) {
377 struct cctl_lun_nv *nv;
379 nv = calloc(1, sizeof(*nv));
381 log_err(1, "%s: can't allocate %zd bytes for nv pair",
382 __func__, sizeof(*nv));
384 nv->name = checked_strdup(name);
388 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
395 cctl_char_handler(void *user_data, const XML_Char *str, int len)
397 struct cctl_devlist_data *devlist;
399 devlist = (struct cctl_devlist_data *)user_data;
401 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
405 conf_new_from_kernel(void)
407 struct conf *conf = NULL;
409 struct portal_group *pg;
414 struct ctl_lun_list list;
415 struct cctl_devlist_data devlist;
416 struct cctl_lun *lun;
417 struct cctl_port *port;
422 bzero(&devlist, sizeof(devlist));
423 STAILQ_INIT(&devlist.lun_list);
424 STAILQ_INIT(&devlist.port_list);
426 log_debugx("obtaining previously configured CTL luns from the kernel");
431 str = realloc(str, len);
433 log_err(1, "realloc");
435 bzero(&list, sizeof(list));
436 list.alloc_len = len;
437 list.status = CTL_LUN_LIST_NONE;
440 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
441 log_warn("error issuing CTL_LUN_LIST ioctl");
446 if (list.status == CTL_LUN_LIST_ERROR) {
447 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
453 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
458 parser = XML_ParserCreate(NULL);
459 if (parser == NULL) {
460 log_warnx("unable to create XML parser");
465 XML_SetUserData(parser, &devlist);
466 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
467 XML_SetCharacterDataHandler(parser, cctl_char_handler);
469 retval = XML_Parse(parser, str, strlen(str), 1);
470 XML_ParserFree(parser);
473 log_warnx("XML_Parse failed");
480 str = realloc(str, len);
482 log_err(1, "realloc");
484 bzero(&list, sizeof(list));
485 list.alloc_len = len;
486 list.status = CTL_LUN_LIST_NONE;
489 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
490 log_warn("error issuing CTL_PORT_LIST ioctl");
495 if (list.status == CTL_LUN_LIST_ERROR) {
496 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
502 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
507 parser = XML_ParserCreate(NULL);
508 if (parser == NULL) {
509 log_warnx("unable to create XML parser");
514 XML_SetUserData(parser, &devlist);
515 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
516 XML_SetCharacterDataHandler(parser, cctl_char_handler);
518 retval = XML_Parse(parser, str, strlen(str), 1);
519 XML_ParserFree(parser);
522 log_warnx("XML_Parse failed");
529 STAILQ_FOREACH(port, &devlist.port_list, links) {
530 if (strcmp(port->port_frontend, "ha") == 0)
533 if (port->pp == 0 && port->vp == 0) {
534 name = checked_strdup(port->port_name);
535 } else if (port->vp == 0) {
536 retval = asprintf(&name, "%s/%d",
537 port->port_name, port->pp);
539 log_err(1, "asprintf");
541 retval = asprintf(&name, "%s/%d/%d",
542 port->port_name, port->pp, port->vp);
544 log_err(1, "asprintf");
547 if (port->cfiscsi_target == NULL) {
548 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
549 port->port_id, name);
550 pp = pport_find(conf, name);
553 log_debugx("found new kernel port %u \"%s\"",
554 port->port_id, name);
556 pp = pport_new(conf, name, port->port_id);
558 log_warnx("pport_new failed");
564 if (port->cfiscsi_state != 1) {
565 log_debugx("CTL port %ju is not active (%d); ignoring",
566 (uintmax_t)port->port_id, port->cfiscsi_state);
570 targ = target_find(conf, port->cfiscsi_target);
573 log_debugx("found new kernel target %s for CTL port %ld",
574 port->cfiscsi_target, port->port_id);
576 targ = target_new(conf, port->cfiscsi_target);
578 log_warnx("target_new failed");
583 if (port->ctld_portal_group_name == NULL)
585 pg = portal_group_find(conf, port->ctld_portal_group_name);
588 log_debugx("found new kernel portal group %s for CTL port %ld",
589 port->ctld_portal_group_name, port->port_id);
591 pg = portal_group_new(conf, port->ctld_portal_group_name);
593 log_warnx("portal_group_new failed");
597 pg->pg_tag = port->cfiscsi_portal_group_tag;
598 cp = port_new(conf, targ, pg);
600 log_warnx("port_new failed");
603 cp->p_ctl_port = port->port_id;
607 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
608 struct cctl_lun_nv *nv;
610 if (lun->ctld_name == NULL) {
611 log_debugx("CTL lun %ju wasn't managed by ctld; "
612 "ignoring", (uintmax_t)lun->lun_id);
616 cl = lun_find(conf, lun->ctld_name);
618 log_warnx("found CTL lun %ju \"%s\", "
619 "also backed by CTL lun %d; ignoring",
620 (uintmax_t)lun->lun_id, lun->ctld_name,
625 log_debugx("found CTL lun %ju \"%s\"",
626 (uintmax_t)lun->lun_id, lun->ctld_name);
628 cl = lun_new(conf, lun->ctld_name);
630 log_warnx("lun_new failed");
633 lun_set_backend(cl, lun->backend_type);
634 lun_set_device_type(cl, lun->device_type);
635 lun_set_blocksize(cl, lun->blocksize);
636 lun_set_device_id(cl, lun->device_id);
637 lun_set_serial(cl, lun->serial_number);
638 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
639 lun_set_ctl_lun(cl, lun->lun_id);
641 STAILQ_FOREACH(nv, &lun->attr_list, links) {
642 if (strcmp(nv->name, "file") == 0 ||
643 strcmp(nv->name, "dev") == 0) {
644 lun_set_path(cl, nv->value);
647 o = option_new(&cl->l_options, nv->name, nv->value);
649 log_warnx("unable to add CTL lun option %s "
650 "for CTL lun %ju \"%s\"",
651 nv->name, (uintmax_t) lun->lun_id,
660 kernel_lun_add(struct lun *lun)
663 struct ctl_lun_req req;
666 bzero(&req, sizeof(req));
668 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
669 req.reqtype = CTL_LUNREQ_CREATE;
671 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
673 if (lun->l_size != 0)
674 req.reqdata.create.lun_size_bytes = lun->l_size;
676 if (lun->l_ctl_lun >= 0) {
677 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
678 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
681 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
682 req.reqdata.create.device_type = lun->l_device_type;
684 if (lun->l_serial != NULL) {
685 strncpy(req.reqdata.create.serial_num, lun->l_serial,
686 sizeof(req.reqdata.create.serial_num));
687 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
690 if (lun->l_device_id != NULL) {
691 strncpy(req.reqdata.create.device_id, lun->l_device_id,
692 sizeof(req.reqdata.create.device_id));
693 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
696 if (lun->l_path != NULL) {
697 o = option_find(&lun->l_options, "file");
699 option_set(o, lun->l_path);
701 o = option_new(&lun->l_options, "file", lun->l_path);
706 o = option_find(&lun->l_options, "ctld_name");
708 option_set(o, lun->l_name);
710 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
714 o = option_find(&lun->l_options, "scsiname");
715 if (o == NULL && lun->l_scsiname != NULL) {
716 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
720 if (!TAILQ_EMPTY(&lun->l_options)) {
721 req.args_nvl = nvlist_create(0);
722 if (req.args_nvl == NULL) {
723 log_warn("error allocating nvlist");
727 TAILQ_FOREACH(o, &lun->l_options, o_next)
728 nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
730 req.args = nvlist_pack(req.args_nvl, &req.args_len);
731 if (req.args == NULL) {
732 log_warn("error packing nvlist");
737 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
738 nvlist_destroy(req.args_nvl);
741 log_warn("error issuing CTL_LUN_REQ ioctl");
745 switch (req.status) {
747 log_warnx("LUN creation error: %s", req.error_str);
749 case CTL_LUN_WARNING:
750 log_warnx("LUN creation warning: %s", req.error_str);
755 log_warnx("unknown LUN creation status: %d",
760 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
765 kernel_lun_modify(struct lun *lun)
768 struct ctl_lun_req req;
771 bzero(&req, sizeof(req));
773 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
774 req.reqtype = CTL_LUNREQ_MODIFY;
776 req.reqdata.modify.lun_id = lun->l_ctl_lun;
777 req.reqdata.modify.lun_size_bytes = lun->l_size;
779 if (!TAILQ_EMPTY(&lun->l_options)) {
780 req.args_nvl = nvlist_create(0);
781 if (req.args_nvl == NULL) {
782 log_warn("error allocating nvlist");
786 TAILQ_FOREACH(o, &lun->l_options, o_next)
787 nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
789 req.args = nvlist_pack(req.args_nvl, &req.args_len);
790 if (req.args == NULL) {
791 log_warn("error packing nvlist");
796 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
797 nvlist_destroy(req.args_nvl);
800 log_warn("error issuing CTL_LUN_REQ ioctl");
804 switch (req.status) {
806 log_warnx("LUN modification error: %s", req.error_str);
808 case CTL_LUN_WARNING:
809 log_warnx("LUN modification warning: %s", req.error_str);
814 log_warnx("unknown LUN modification status: %d",
823 kernel_lun_remove(struct lun *lun)
825 struct ctl_lun_req req;
827 bzero(&req, sizeof(req));
829 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
830 req.reqtype = CTL_LUNREQ_RM;
832 req.reqdata.rm.lun_id = lun->l_ctl_lun;
834 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
835 log_warn("error issuing CTL_LUN_REQ ioctl");
839 switch (req.status) {
841 log_warnx("LUN removal error: %s", req.error_str);
843 case CTL_LUN_WARNING:
844 log_warnx("LUN removal warning: %s", req.error_str);
849 log_warnx("unknown LUN removal status: %d", req.status);
857 kernel_handoff(struct connection *conn)
859 struct ctl_iscsi req;
861 bzero(&req, sizeof(req));
863 req.type = CTL_ISCSI_HANDOFF;
864 strlcpy(req.data.handoff.initiator_name,
865 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
866 strlcpy(req.data.handoff.initiator_addr,
867 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
868 if (conn->conn_initiator_alias != NULL) {
869 strlcpy(req.data.handoff.initiator_alias,
870 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
872 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
873 sizeof(req.data.handoff.initiator_isid));
874 strlcpy(req.data.handoff.target_name,
875 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
876 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
877 strlcpy(req.data.handoff.offload,
878 conn->conn_portal->p_portal_group->pg_offload,
879 sizeof(req.data.handoff.offload));
881 #ifdef ICL_KERNEL_PROXY
883 req.data.handoff.connection_id = conn->conn_socket;
885 req.data.handoff.socket = conn->conn_socket;
887 req.data.handoff.socket = conn->conn_socket;
889 req.data.handoff.portal_group_tag =
890 conn->conn_portal->p_portal_group->pg_tag;
891 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
892 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
893 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
894 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
895 req.data.handoff.cmdsn = conn->conn_cmdsn;
896 req.data.handoff.statsn = conn->conn_statsn;
897 req.data.handoff.max_recv_data_segment_length =
898 conn->conn_max_recv_data_segment_length;
899 req.data.handoff.max_send_data_segment_length =
900 conn->conn_max_send_data_segment_length;
901 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
902 req.data.handoff.first_burst_length = conn->conn_first_burst_length;
903 req.data.handoff.immediate_data = conn->conn_immediate_data;
905 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
906 log_err(1, "error issuing CTL_ISCSI ioctl; "
907 "dropping connection");
910 if (req.status != CTL_ISCSI_OK) {
911 log_errx(1, "error returned from CTL iSCSI handoff request: "
912 "%s; dropping connection", req.error_str);
917 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
918 int *max_burst_length, int *first_burst_length)
920 struct ctl_iscsi req;
921 struct ctl_iscsi_limits_params *cilp;
923 bzero(&req, sizeof(req));
925 req.type = CTL_ISCSI_LIMITS;
926 cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
927 if (offload != NULL) {
928 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
931 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
932 log_err(1, "error issuing CTL_ISCSI ioctl; "
933 "dropping connection");
936 if (req.status != CTL_ISCSI_OK) {
937 log_errx(1, "error returned from CTL iSCSI limits request: "
938 "%s; dropping connection", req.error_str);
941 if (cilp->max_recv_data_segment_length != 0) {
942 *max_recv_dsl = cilp->max_recv_data_segment_length;
943 *max_send_dsl = cilp->max_recv_data_segment_length;
945 if (cilp->max_send_data_segment_length != 0)
946 *max_send_dsl = cilp->max_send_data_segment_length;
947 if (cilp->max_burst_length != 0)
948 *max_burst_length = cilp->max_burst_length;
949 if (cilp->first_burst_length != 0)
950 *first_burst_length = cilp->first_burst_length;
951 if (*max_burst_length < *first_burst_length)
952 *first_burst_length = *max_burst_length;
954 if (offload != NULL) {
955 log_debugx("Kernel limits for offload \"%s\" are "
956 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
957 "MaxBurstLength=%d, FirstBurstLength=%d",
958 offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
959 *first_burst_length);
961 log_debugx("Kernel limits are "
962 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
963 "MaxBurstLength=%d, FirstBurstLength=%d",
964 *max_recv_dsl, *max_send_dsl, *max_burst_length,
965 *first_burst_length);
970 kernel_port_add(struct port *port)
973 struct ctl_port_entry entry;
975 struct ctl_lun_map lm;
976 struct target *targ = port->p_target;
977 struct portal_group *pg = port->p_portal_group;
978 char result_buf[NVLIST_BUFSIZE];
981 /* Create iSCSI port. */
982 if (port->p_portal_group || port->p_ioctl_port) {
983 bzero(&req, sizeof(req));
984 req.reqtype = CTL_REQ_CREATE;
986 if (port->p_portal_group) {
987 strlcpy(req.driver, "iscsi", sizeof(req.driver));
988 req.args_nvl = nvlist_create(0);
989 nvlist_add_string(req.args_nvl, "cfiscsi_target",
991 nvlist_add_string(req.args_nvl,
992 "ctld_portal_group_name", pg->pg_name);
993 nvlist_add_stringf(req.args_nvl,
994 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
997 nvlist_add_string(req.args_nvl,
998 "cfiscsi_target_alias", targ->t_alias);
1001 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1002 nvlist_add_string(req.args_nvl, o->o_name,
1006 if (port->p_ioctl_port) {
1007 strlcpy(req.driver, "ioctl", sizeof(req.driver));
1008 req.args_nvl = nvlist_create(0);
1009 nvlist_add_stringf(req.args_nvl, "pp", "%d",
1011 nvlist_add_stringf(req.args_nvl, "vp", "%d",
1015 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1016 if (req.args == NULL) {
1017 log_warn("error packing nvlist");
1021 req.result = result_buf;
1022 req.result_len = sizeof(result_buf);
1023 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1024 nvlist_destroy(req.args_nvl);
1027 log_warn("error issuing CTL_PORT_REQ ioctl");
1030 if (req.status == CTL_LUN_ERROR) {
1031 log_warnx("error returned from port creation request: %s",
1035 if (req.status != CTL_LUN_OK) {
1036 log_warnx("unknown port creation request status %d",
1041 req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0);
1042 if (req.result_nvl == NULL) {
1043 log_warnx("error unpacking result nvlist");
1047 port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id");
1048 nvlist_destroy(req.result_nvl);
1049 } else if (port->p_pport) {
1050 port->p_ctl_port = port->p_pport->pp_ctl_port;
1052 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1053 strlen(targ->t_name) == 20) {
1054 bzero(&entry, sizeof(entry));
1055 entry.port_type = CTL_PORT_NONE;
1056 entry.targ_port = port->p_ctl_port;
1057 entry.flags |= CTL_PORT_WWNN_VALID;
1058 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1059 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1060 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1064 /* Explicitly enable mapping to block any access except allowed. */
1065 lm.port = port->p_ctl_port;
1066 lm.plun = UINT32_MAX;
1068 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1070 log_warn("CTL_LUN_MAP ioctl failed");
1072 /* Map configured LUNs */
1073 for (i = 0; i < MAX_LUNS; i++) {
1074 if (targ->t_luns[i] == NULL)
1076 lm.port = port->p_ctl_port;
1078 lm.lun = targ->t_luns[i]->l_ctl_lun;
1079 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1081 log_warn("CTL_LUN_MAP ioctl failed");
1085 bzero(&entry, sizeof(entry));
1086 entry.targ_port = port->p_ctl_port;
1087 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1089 log_warn("CTL_ENABLE_PORT ioctl failed");
1097 kernel_port_update(struct port *port, struct port *oport)
1099 struct ctl_lun_map lm;
1100 struct target *targ = port->p_target;
1101 struct target *otarg = oport->p_target;
1105 /* Map configured LUNs and unmap others */
1106 for (i = 0; i < MAX_LUNS; i++) {
1107 lm.port = port->p_ctl_port;
1109 if (targ->t_luns[i] == NULL)
1110 lm.lun = UINT32_MAX;
1112 lm.lun = targ->t_luns[i]->l_ctl_lun;
1113 if (otarg->t_luns[i] == NULL)
1116 olun = otarg->t_luns[i]->l_ctl_lun;
1119 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1121 log_warn("CTL_LUN_MAP ioctl failed");
1127 kernel_port_remove(struct port *port)
1129 struct ctl_port_entry entry;
1130 struct ctl_lun_map lm;
1132 struct target *targ = port->p_target;
1133 struct portal_group *pg = port->p_portal_group;
1137 bzero(&entry, sizeof(entry));
1138 entry.targ_port = port->p_ctl_port;
1139 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1141 log_warn("CTL_DISABLE_PORT ioctl failed");
1145 /* Remove iSCSI or ioctl port. */
1146 if (port->p_portal_group || port->p_ioctl_port) {
1147 bzero(&req, sizeof(req));
1148 strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi",
1149 sizeof(req.driver));
1150 req.reqtype = CTL_REQ_REMOVE;
1151 req.args_nvl = nvlist_create(0);
1152 if (req.args_nvl == NULL)
1153 log_err(1, "nvlist_create");
1155 if (port->p_ioctl_port)
1156 nvlist_add_stringf(req.args_nvl, "port_id", "%d",
1159 nvlist_add_string(req.args_nvl, "cfiscsi_target",
1161 nvlist_add_stringf(req.args_nvl,
1162 "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1165 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1166 if (req.args == NULL) {
1167 log_warn("error packing nvlist");
1171 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1172 nvlist_destroy(req.args_nvl);
1175 log_warn("error issuing CTL_PORT_REQ ioctl");
1178 if (req.status == CTL_LUN_ERROR) {
1179 log_warnx("error returned from port removal request: %s",
1183 if (req.status != CTL_LUN_OK) {
1184 log_warnx("unknown port removal request status %d",
1189 /* Disable LUN mapping. */
1190 lm.port = port->p_ctl_port;
1191 lm.plun = UINT32_MAX;
1192 lm.lun = UINT32_MAX;
1193 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1195 log_warn("CTL_LUN_MAP ioctl failed");
1200 #ifdef ICL_KERNEL_PROXY
1202 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1204 struct ctl_iscsi req;
1206 bzero(&req, sizeof(req));
1208 req.type = CTL_ISCSI_LISTEN;
1209 req.data.listen.iser = iser;
1210 req.data.listen.domain = ai->ai_family;
1211 req.data.listen.socktype = ai->ai_socktype;
1212 req.data.listen.protocol = ai->ai_protocol;
1213 req.data.listen.addr = ai->ai_addr;
1214 req.data.listen.addrlen = ai->ai_addrlen;
1215 req.data.listen.portal_id = portal_id;
1217 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1218 log_err(1, "error issuing CTL_ISCSI ioctl");
1220 if (req.status != CTL_ISCSI_OK) {
1221 log_errx(1, "error returned from CTL iSCSI listen: %s",
1227 kernel_accept(int *connection_id, int *portal_id,
1228 struct sockaddr *client_sa, socklen_t *client_salen)
1230 struct ctl_iscsi req;
1231 struct sockaddr_storage ss;
1233 bzero(&req, sizeof(req));
1235 req.type = CTL_ISCSI_ACCEPT;
1236 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1238 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1239 log_err(1, "error issuing CTL_ISCSI ioctl");
1241 if (req.status != CTL_ISCSI_OK) {
1242 log_errx(1, "error returned from CTL iSCSI accept: %s",
1246 *connection_id = req.data.accept.connection_id;
1247 *portal_id = req.data.accept.portal_id;
1248 *client_salen = req.data.accept.initiator_addrlen;
1249 memcpy(client_sa, &ss, *client_salen);
1253 kernel_send(struct pdu *pdu)
1255 struct ctl_iscsi req;
1257 bzero(&req, sizeof(req));
1259 req.type = CTL_ISCSI_SEND;
1260 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1261 req.data.send.bhs = pdu->pdu_bhs;
1262 req.data.send.data_segment_len = pdu->pdu_data_len;
1263 req.data.send.data_segment = pdu->pdu_data;
1265 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1266 log_err(1, "error issuing CTL_ISCSI ioctl; "
1267 "dropping connection");
1270 if (req.status != CTL_ISCSI_OK) {
1271 log_errx(1, "error returned from CTL iSCSI send: "
1272 "%s; dropping connection", req.error_str);
1277 kernel_receive(struct pdu *pdu)
1279 struct connection *conn;
1280 struct ctl_iscsi req;
1282 conn = pdu->pdu_connection;
1283 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1284 if (pdu->pdu_data == NULL)
1285 log_err(1, "malloc");
1287 bzero(&req, sizeof(req));
1289 req.type = CTL_ISCSI_RECEIVE;
1290 req.data.receive.connection_id = conn->conn_socket;
1291 req.data.receive.bhs = pdu->pdu_bhs;
1292 req.data.receive.data_segment_len =
1293 conn->conn_max_recv_data_segment_length;
1294 req.data.receive.data_segment = pdu->pdu_data;
1296 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1297 log_err(1, "error issuing CTL_ISCSI ioctl; "
1298 "dropping connection");
1301 if (req.status != CTL_ISCSI_OK) {
1302 log_errx(1, "error returned from CTL iSCSI receive: "
1303 "%s; dropping connection", req.error_str);
1308 #endif /* ICL_KERNEL_PROXY */
1311 * XXX: I CANT INTO LATIN
1314 kernel_capsicate(void)
1317 cap_rights_t rights;
1318 const unsigned long cmds[] = { CTL_ISCSI };
1320 cap_rights_init(&rights, CAP_IOCTL);
1321 error = cap_rights_limit(ctl_fd, &rights);
1322 if (error != 0 && errno != ENOSYS)
1323 log_err(1, "cap_rights_limit");
1325 error = cap_ioctls_limit(ctl_fd, cmds, nitems(cmds));
1327 if (error != 0 && errno != ENOSYS)
1328 log_err(1, "cap_ioctls_limit");
1330 error = cap_enter();
1331 if (error != 0 && errno != ENOSYS)
1332 log_err(1, "cap_enter");
1334 if (cap_sandboxed())
1335 log_debugx("Capsicum capability mode enabled");
1337 log_warnx("Capsicum capability mode not supported");