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/capability.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_frontend_internal.h>
64 #include <cam/ctl/ctl_backend.h>
65 #include <cam/ctl/ctl_ioctl.h>
66 #include <cam/ctl/ctl_backend_block.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 * Name/value pair used for per-LUN attributes.
104 STAILQ_ENTRY(cctl_lun_nv) links;
108 * Backend LUN information.
113 uint64_t size_blocks;
118 STAILQ_HEAD(,cctl_lun_nv) attr_list;
119 STAILQ_ENTRY(cctl_lun) links;
125 char *cfiscsi_target;
126 uint16_t cfiscsi_portal_group_tag;
127 STAILQ_HEAD(,cctl_lun_nv) attr_list;
128 STAILQ_ENTRY(cctl_port) links;
131 struct cctl_devlist_data {
133 STAILQ_HEAD(,cctl_lun) lun_list;
134 struct cctl_lun *cur_lun;
136 STAILQ_HEAD(,cctl_port) port_list;
137 struct cctl_port *cur_port;
139 struct sbuf *cur_sb[32];
143 cctl_start_element(void *user_data, const char *name, const char **attr)
146 struct cctl_devlist_data *devlist;
147 struct cctl_lun *cur_lun;
149 devlist = (struct cctl_devlist_data *)user_data;
150 cur_lun = devlist->cur_lun;
152 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
153 sizeof(devlist->cur_sb[0])))
154 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
155 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
157 devlist->cur_sb[devlist->level] = sbuf_new_auto();
158 if (devlist->cur_sb[devlist->level] == NULL)
159 log_err(1, "%s: unable to allocate sbuf", __func__);
161 if (strcmp(name, "lun") == 0) {
163 log_errx(1, "%s: improper lun element nesting",
166 cur_lun = calloc(1, sizeof(*cur_lun));
168 log_err(1, "%s: cannot allocate %zd bytes", __func__,
172 devlist->cur_lun = cur_lun;
174 STAILQ_INIT(&cur_lun->attr_list);
175 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
177 for (i = 0; attr[i] != NULL; i += 2) {
178 if (strcmp(attr[i], "id") == 0) {
179 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
181 log_errx(1, "%s: invalid LUN attribute %s = %s",
182 __func__, attr[i], attr[i+1]);
189 cctl_end_element(void *user_data, const char *name)
191 struct cctl_devlist_data *devlist;
192 struct cctl_lun *cur_lun;
195 devlist = (struct cctl_devlist_data *)user_data;
196 cur_lun = devlist->cur_lun;
198 if ((cur_lun == NULL)
199 && (strcmp(name, "ctllunlist") != 0))
200 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
202 if (devlist->cur_sb[devlist->level] == NULL)
203 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
204 devlist->level, name);
206 sbuf_finish(devlist->cur_sb[devlist->level]);
207 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
209 if (strlen(str) == 0) {
214 sbuf_delete(devlist->cur_sb[devlist->level]);
215 devlist->cur_sb[devlist->level] = NULL;
218 if (strcmp(name, "backend_type") == 0) {
219 cur_lun->backend_type = str;
221 } else if (strcmp(name, "size") == 0) {
222 cur_lun->size_blocks = strtoull(str, NULL, 0);
223 } else if (strcmp(name, "blocksize") == 0) {
224 cur_lun->blocksize = strtoul(str, NULL, 0);
225 } else if (strcmp(name, "serial_number") == 0) {
226 cur_lun->serial_number = str;
228 } else if (strcmp(name, "device_id") == 0) {
229 cur_lun->device_id = str;
231 } else if (strcmp(name, "ctld_name") == 0) {
232 cur_lun->ctld_name = str;
234 } else if (strcmp(name, "lun") == 0) {
235 devlist->cur_lun = NULL;
236 } else if (strcmp(name, "ctllunlist") == 0) {
239 struct cctl_lun_nv *nv;
241 nv = calloc(1, sizeof(*nv));
243 log_err(1, "%s: can't allocate %zd bytes for nv pair",
244 __func__, sizeof(*nv));
246 nv->name = checked_strdup(name);
250 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
257 cctl_start_pelement(void *user_data, const char *name, const char **attr)
260 struct cctl_devlist_data *devlist;
261 struct cctl_port *cur_port;
263 devlist = (struct cctl_devlist_data *)user_data;
264 cur_port = devlist->cur_port;
266 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
267 sizeof(devlist->cur_sb[0])))
268 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
269 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
271 devlist->cur_sb[devlist->level] = sbuf_new_auto();
272 if (devlist->cur_sb[devlist->level] == NULL)
273 log_err(1, "%s: unable to allocate sbuf", __func__);
275 if (strcmp(name, "targ_port") == 0) {
276 if (cur_port != NULL)
277 log_errx(1, "%s: improper port element nesting (%s)",
280 cur_port = calloc(1, sizeof(*cur_port));
281 if (cur_port == NULL)
282 log_err(1, "%s: cannot allocate %zd bytes", __func__,
285 devlist->num_ports++;
286 devlist->cur_port = cur_port;
288 STAILQ_INIT(&cur_port->attr_list);
289 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
291 for (i = 0; attr[i] != NULL; i += 2) {
292 if (strcmp(attr[i], "id") == 0) {
293 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
295 log_errx(1, "%s: invalid LUN attribute %s = %s",
296 __func__, attr[i], attr[i+1]);
303 cctl_end_pelement(void *user_data, const char *name)
305 struct cctl_devlist_data *devlist;
306 struct cctl_port *cur_port;
309 devlist = (struct cctl_devlist_data *)user_data;
310 cur_port = devlist->cur_port;
312 if ((cur_port == NULL)
313 && (strcmp(name, "ctlportlist") != 0))
314 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
316 if (devlist->cur_sb[devlist->level] == NULL)
317 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
318 devlist->level, name);
320 sbuf_finish(devlist->cur_sb[devlist->level]);
321 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
323 if (strlen(str) == 0) {
328 sbuf_delete(devlist->cur_sb[devlist->level]);
329 devlist->cur_sb[devlist->level] = NULL;
332 if (strcmp(name, "cfiscsi_target") == 0) {
333 cur_port->cfiscsi_target = str;
335 } else if (strcmp(name, "cfiscsi_status") == 0) {
336 cur_port->cfiscsi_status = strtoul(str, NULL, 0);
337 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
338 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
339 } else if (strcmp(name, "targ_port") == 0) {
340 devlist->cur_port = NULL;
341 } else if (strcmp(name, "ctlportlist") == 0) {
344 struct cctl_lun_nv *nv;
346 nv = calloc(1, sizeof(*nv));
348 log_err(1, "%s: can't allocate %zd bytes for nv pair",
349 __func__, sizeof(*nv));
351 nv->name = checked_strdup(name);
355 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
362 cctl_char_handler(void *user_data, const XML_Char *str, int len)
364 struct cctl_devlist_data *devlist;
366 devlist = (struct cctl_devlist_data *)user_data;
368 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
372 conf_new_from_kernel(void)
374 struct conf *conf = NULL;
377 struct lun_option *lo;
378 struct ctl_lun_list list;
379 struct cctl_devlist_data devlist;
380 struct cctl_lun *lun;
381 struct cctl_port *port;
386 bzero(&devlist, sizeof(devlist));
387 STAILQ_INIT(&devlist.lun_list);
388 STAILQ_INIT(&devlist.port_list);
390 log_debugx("obtaining previously configured CTL luns from the kernel");
395 str = realloc(str, len);
397 log_err(1, "realloc");
399 bzero(&list, sizeof(list));
400 list.alloc_len = len;
401 list.status = CTL_LUN_LIST_NONE;
404 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
405 log_warn("error issuing CTL_LUN_LIST ioctl");
410 if (list.status == CTL_LUN_LIST_ERROR) {
411 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
417 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
422 parser = XML_ParserCreate(NULL);
423 if (parser == NULL) {
424 log_warnx("unable to create XML parser");
429 XML_SetUserData(parser, &devlist);
430 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
431 XML_SetCharacterDataHandler(parser, cctl_char_handler);
433 retval = XML_Parse(parser, str, strlen(str), 1);
434 XML_ParserFree(parser);
437 log_warnx("XML_Parse failed");
444 str = realloc(str, len);
446 log_err(1, "realloc");
448 bzero(&list, sizeof(list));
449 list.alloc_len = len;
450 list.status = CTL_LUN_LIST_NONE;
453 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
454 log_warn("error issuing CTL_PORT_LIST ioctl");
459 if (list.status == CTL_PORT_LIST_ERROR) {
460 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
466 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
471 parser = XML_ParserCreate(NULL);
472 if (parser == NULL) {
473 log_warnx("unable to create XML parser");
478 XML_SetUserData(parser, &devlist);
479 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
480 XML_SetCharacterDataHandler(parser, cctl_char_handler);
482 retval = XML_Parse(parser, str, strlen(str), 1);
483 XML_ParserFree(parser);
486 log_warnx("XML_Parse failed");
492 STAILQ_FOREACH(port, &devlist.port_list, links) {
494 if (port->cfiscsi_target == NULL) {
495 log_debugx("CTL port %ju wasn't managed by ctld; "
496 "ignoring", (uintmax_t)port->port_id);
499 if (port->cfiscsi_status != 1) {
500 log_debugx("CTL port %ju is not active (%d); ignoring",
501 (uintmax_t)port->port_id, port->cfiscsi_status);
505 targ = target_find(conf, port->cfiscsi_target);
508 log_debugx("found new kernel target %s for CTL port %ld",
509 port->cfiscsi_target, port->port_id);
511 targ = target_new(conf, port->cfiscsi_target);
513 log_warnx("target_new failed");
519 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
520 struct cctl_lun_nv *nv;
522 if (lun->ctld_name == NULL) {
523 log_debugx("CTL lun %ju wasn't managed by ctld; "
524 "ignoring", (uintmax_t)lun->lun_id);
528 cl = lun_find(conf, lun->ctld_name);
530 log_warnx("found CTL lun %ju \"%s\", "
531 "also backed by CTL lun %d; ignoring",
532 (uintmax_t)lun->lun_id, lun->ctld_name,
537 log_debugx("found CTL lun %ju \"%s\"",
538 (uintmax_t)lun->lun_id, lun->ctld_name);
540 cl = lun_new(conf, lun->ctld_name);
542 log_warnx("lun_new failed");
545 lun_set_backend(cl, lun->backend_type);
546 lun_set_blocksize(cl, lun->blocksize);
547 lun_set_device_id(cl, lun->device_id);
548 lun_set_serial(cl, lun->serial_number);
549 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
550 lun_set_ctl_lun(cl, lun->lun_id);
552 STAILQ_FOREACH(nv, &lun->attr_list, links) {
553 if (strcmp(nv->name, "file") == 0 ||
554 strcmp(nv->name, "dev") == 0) {
555 lun_set_path(cl, nv->value);
558 lo = lun_option_new(cl, nv->name, nv->value);
560 log_warnx("unable to add CTL lun option %s "
561 "for CTL lun %ju \"%s\"",
562 nv->name, (uintmax_t) lun->lun_id,
571 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
574 arg->namelen = strlen(name) + 1;
575 arg->name = __DECONST(char *, name);
576 arg->vallen = strlen(value) + 1;
577 arg->value = __DECONST(char *, value);
578 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
582 kernel_lun_add(struct lun *lun)
584 struct lun_option *lo;
585 struct ctl_lun_req req;
586 int error, i, num_options;
588 bzero(&req, sizeof(req));
590 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
591 req.reqtype = CTL_LUNREQ_CREATE;
593 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
595 if (lun->l_size != 0)
596 req.reqdata.create.lun_size_bytes = lun->l_size;
598 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
599 req.reqdata.create.device_type = T_DIRECT;
601 if (lun->l_serial != NULL) {
602 strncpy(req.reqdata.create.serial_num, lun->l_serial,
603 sizeof(req.reqdata.create.serial_num));
604 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
607 if (lun->l_device_id != NULL) {
608 strncpy(req.reqdata.create.device_id, lun->l_device_id,
609 sizeof(req.reqdata.create.device_id));
610 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
613 if (lun->l_path != NULL) {
614 lo = lun_option_find(lun, "file");
616 lun_option_set(lo, lun->l_path);
618 lo = lun_option_new(lun, "file", lun->l_path);
623 lo = lun_option_find(lun, "ctld_name");
625 lun_option_set(lo, lun->l_name);
627 lo = lun_option_new(lun, "ctld_name", lun->l_name);
631 lo = lun_option_find(lun, "scsiname");
632 if (lo == NULL && lun->l_scsiname != NULL) {
633 lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
638 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
641 req.num_be_args = num_options;
642 if (num_options > 0) {
643 req.be_args = malloc(num_options * sizeof(*req.be_args));
644 if (req.be_args == NULL) {
645 log_warn("error allocating %zd bytes",
646 num_options * sizeof(*req.be_args));
651 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
652 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
655 assert(i == num_options);
658 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
661 log_warn("error issuing CTL_LUN_REQ ioctl");
665 switch (req.status) {
667 log_warnx("LUN creation error: %s", req.error_str);
669 case CTL_LUN_WARNING:
670 log_warnx("LUN creation warning: %s", req.error_str);
675 log_warnx("unknown LUN creation status: %d",
680 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
685 kernel_lun_resize(struct lun *lun)
687 struct ctl_lun_req req;
689 bzero(&req, sizeof(req));
691 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
692 req.reqtype = CTL_LUNREQ_MODIFY;
694 req.reqdata.modify.lun_id = lun->l_ctl_lun;
695 req.reqdata.modify.lun_size_bytes = lun->l_size;
697 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
698 log_warn("error issuing CTL_LUN_REQ ioctl");
702 switch (req.status) {
704 log_warnx("LUN modification error: %s", req.error_str);
706 case CTL_LUN_WARNING:
707 log_warnx("LUN modification warning: %s", req.error_str);
712 log_warnx("unknown LUN modification status: %d",
721 kernel_lun_remove(struct lun *lun)
723 struct ctl_lun_req req;
725 bzero(&req, sizeof(req));
727 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
728 req.reqtype = CTL_LUNREQ_RM;
730 req.reqdata.rm.lun_id = lun->l_ctl_lun;
732 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
733 log_warn("error issuing CTL_LUN_REQ ioctl");
737 switch (req.status) {
739 log_warnx("LUN removal error: %s", req.error_str);
741 case CTL_LUN_WARNING:
742 log_warnx("LUN removal warning: %s", req.error_str);
747 log_warnx("unknown LUN removal status: %d", req.status);
755 kernel_handoff(struct connection *conn)
757 struct ctl_iscsi req;
759 bzero(&req, sizeof(req));
761 req.type = CTL_ISCSI_HANDOFF;
762 strlcpy(req.data.handoff.initiator_name,
763 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
764 strlcpy(req.data.handoff.initiator_addr,
765 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
766 if (conn->conn_initiator_alias != NULL) {
767 strlcpy(req.data.handoff.initiator_alias,
768 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
770 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
771 sizeof(req.data.handoff.initiator_isid));
772 strlcpy(req.data.handoff.target_name,
773 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
774 #ifdef ICL_KERNEL_PROXY
776 req.data.handoff.connection_id = conn->conn_socket;
778 req.data.handoff.socket = conn->conn_socket;
780 req.data.handoff.socket = conn->conn_socket;
782 req.data.handoff.portal_group_tag =
783 conn->conn_portal->p_portal_group->pg_tag;
784 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
785 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
786 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
787 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
788 req.data.handoff.cmdsn = conn->conn_cmdsn;
789 req.data.handoff.statsn = conn->conn_statsn;
790 req.data.handoff.max_recv_data_segment_length =
791 conn->conn_max_data_segment_length;
792 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
793 req.data.handoff.immediate_data = conn->conn_immediate_data;
795 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
796 log_err(1, "error issuing CTL_ISCSI ioctl; "
797 "dropping connection");
800 if (req.status != CTL_ISCSI_OK) {
801 log_errx(1, "error returned from CTL iSCSI handoff request: "
802 "%s; dropping connection", req.error_str);
807 kernel_port_add(struct target *targ)
809 struct ctl_port_entry entry;
811 struct ctl_lun_map lm;
815 /* Create iSCSI port. */
816 bzero(&req, sizeof(req));
817 strlcpy(req.driver, "iscsi", sizeof(req.driver));
818 req.reqtype = CTL_REQ_CREATE;
820 req.args = malloc(req.num_args * sizeof(*req.args));
821 req.args[0].namelen = sizeof("port_id");
822 req.args[0].name = __DECONST(char *, "port_id");
823 req.args[0].vallen = sizeof(targ->t_ctl_port);
824 req.args[0].value = &targ->t_ctl_port;
825 req.args[0].flags = CTL_BEARG_WR;
826 str_arg(&req.args[1], "cfiscsi_target", targ->t_name);
827 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
828 str_arg(&req.args[2], "cfiscsi_portal_group_tag", tagstr);
830 str_arg(&req.args[3], "cfiscsi_target_alias", targ->t_alias);
833 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
836 log_warn("error issuing CTL_PORT_REQ ioctl");
839 if (req.status == CTL_LUN_ERROR) {
840 log_warnx("error returned from port creation request: %s",
844 if (req.status != CTL_LUN_OK) {
845 log_warnx("unknown port creation request status %d",
850 /* Explicitly enable mapping to block any access except allowed. */
851 lm.port = targ->t_ctl_port;
852 lm.plun = UINT32_MAX;
854 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
856 log_warn("CTL_LUN_MAP ioctl failed");
858 /* Map configured LUNs */
859 for (i = 0; i < MAX_LUNS; i++) {
860 if (targ->t_luns[i] == NULL)
862 lm.port = targ->t_ctl_port;
864 lm.lun = targ->t_luns[i]->l_ctl_lun;
865 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
867 log_warn("CTL_LUN_MAP ioctl failed");
871 bzero(&entry, sizeof(entry));
872 entry.targ_port = targ->t_ctl_port;
873 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
875 log_warn("CTL_ENABLE_PORT ioctl failed");
883 kernel_port_update(struct target *targ)
885 struct ctl_lun_map lm;
888 /* Map configured LUNs and unmap others */
889 for (i = 0; i < MAX_LUNS; i++) {
890 lm.port = targ->t_ctl_port;
892 if (targ->t_luns[i] == NULL)
895 lm.lun = targ->t_luns[i]->l_ctl_lun;
896 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
898 log_warn("CTL_LUN_MAP ioctl failed");
904 kernel_port_remove(struct target *targ)
910 bzero(&req, sizeof(req));
911 strlcpy(req.driver, "iscsi", sizeof(req.driver));
912 req.reqtype = CTL_REQ_REMOVE;
914 req.args = malloc(req.num_args * sizeof(*req.args));
915 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
916 if (targ->t_portal_group) {
917 snprintf(tagstr, sizeof(tagstr), "%d",
918 targ->t_portal_group->pg_tag);
919 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
923 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
926 log_warn("error issuing CTL_PORT_REQ ioctl");
930 if (req.status == CTL_LUN_ERROR) {
931 log_warnx("error returned from port removal request: %s",
936 if (req.status != CTL_LUN_OK) {
937 log_warnx("unknown port removal request status %d",
945 #ifdef ICL_KERNEL_PROXY
947 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
949 struct ctl_iscsi req;
951 bzero(&req, sizeof(req));
953 req.type = CTL_ISCSI_LISTEN;
954 req.data.listen.iser = iser;
955 req.data.listen.domain = ai->ai_family;
956 req.data.listen.socktype = ai->ai_socktype;
957 req.data.listen.protocol = ai->ai_protocol;
958 req.data.listen.addr = ai->ai_addr;
959 req.data.listen.addrlen = ai->ai_addrlen;
960 req.data.listen.portal_id = portal_id;
962 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
963 log_err(1, "error issuing CTL_ISCSI ioctl");
965 if (req.status != CTL_ISCSI_OK) {
966 log_errx(1, "error returned from CTL iSCSI listen: %s",
972 kernel_accept(int *connection_id, int *portal_id,
973 struct sockaddr *client_sa, socklen_t *client_salen)
975 struct ctl_iscsi req;
976 struct sockaddr_storage ss;
978 bzero(&req, sizeof(req));
980 req.type = CTL_ISCSI_ACCEPT;
981 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
983 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
984 log_err(1, "error issuing CTL_ISCSI ioctl");
986 if (req.status != CTL_ISCSI_OK) {
987 log_errx(1, "error returned from CTL iSCSI accept: %s",
991 *connection_id = req.data.accept.connection_id;
992 *portal_id = req.data.accept.portal_id;
993 *client_salen = req.data.accept.initiator_addrlen;
994 memcpy(client_sa, &ss, *client_salen);
998 kernel_send(struct pdu *pdu)
1000 struct ctl_iscsi req;
1002 bzero(&req, sizeof(req));
1004 req.type = CTL_ISCSI_SEND;
1005 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1006 req.data.send.bhs = pdu->pdu_bhs;
1007 req.data.send.data_segment_len = pdu->pdu_data_len;
1008 req.data.send.data_segment = pdu->pdu_data;
1010 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1011 log_err(1, "error issuing CTL_ISCSI ioctl; "
1012 "dropping connection");
1015 if (req.status != CTL_ISCSI_OK) {
1016 log_errx(1, "error returned from CTL iSCSI send: "
1017 "%s; dropping connection", req.error_str);
1022 kernel_receive(struct pdu *pdu)
1024 struct ctl_iscsi req;
1026 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1027 if (pdu->pdu_data == NULL)
1028 log_err(1, "malloc");
1030 bzero(&req, sizeof(req));
1032 req.type = CTL_ISCSI_RECEIVE;
1033 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1034 req.data.receive.bhs = pdu->pdu_bhs;
1035 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1036 req.data.receive.data_segment = pdu->pdu_data;
1038 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1039 log_err(1, "error issuing CTL_ISCSI ioctl; "
1040 "dropping connection");
1043 if (req.status != CTL_ISCSI_OK) {
1044 log_errx(1, "error returned from CTL iSCSI receive: "
1045 "%s; dropping connection", req.error_str);
1050 #endif /* ICL_KERNEL_PROXY */
1053 * XXX: I CANT INTO LATIN
1056 kernel_capsicate(void)
1059 cap_rights_t rights;
1060 const unsigned long cmds[] = { CTL_ISCSI };
1062 cap_rights_init(&rights, CAP_IOCTL);
1063 error = cap_rights_limit(ctl_fd, &rights);
1064 if (error != 0 && errno != ENOSYS)
1065 log_err(1, "cap_rights_limit");
1067 error = cap_ioctls_limit(ctl_fd, cmds,
1068 sizeof(cmds) / sizeof(cmds[0]));
1069 if (error != 0 && errno != ENOSYS)
1070 log_err(1, "cap_ioctls_limit");
1072 error = cap_enter();
1073 if (error != 0 && errno != ENOSYS)
1074 log_err(1, "cap_enter");
1076 if (cap_sandboxed())
1077 log_debugx("Capsicum capability mode enabled");
1079 log_warnx("Capsicum capability mode not supported");