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.
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/linker.h>
43 #include <sys/queue.h>
44 #include <sys/callout.h>
46 #include <sys/capability.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/ctl/ctl.h>
60 #include <cam/ctl/ctl_io.h>
61 #include <cam/ctl/ctl_frontend_internal.h>
62 #include <cam/ctl/ctl_backend.h>
63 #include <cam/ctl/ctl_ioctl.h>
64 #include <cam/ctl/ctl_backend_block.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_scsi_all.h>
68 #ifdef ICL_KERNEL_PROXY
74 static int ctl_fd = 0;
79 int retval, saved_errno;
81 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
82 if (ctl_fd < 0 && errno == ENOENT) {
84 retval = kldload("ctl");
86 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
91 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
95 * Name/value pair used for per-LUN attributes.
100 STAILQ_ENTRY(cctl_lun_nv) links;
104 * Backend LUN information.
109 uint64_t size_blocks;
113 char *cfiscsi_target;
114 char *cfiscsi_target_alias;
116 STAILQ_HEAD(,cctl_lun_nv) attr_list;
117 STAILQ_ENTRY(cctl_lun) links;
120 struct cctl_devlist_data {
122 STAILQ_HEAD(,cctl_lun) lun_list;
123 struct cctl_lun *cur_lun;
125 struct sbuf *cur_sb[32];
129 cctl_start_element(void *user_data, const char *name, const char **attr)
132 struct cctl_devlist_data *devlist;
133 struct cctl_lun *cur_lun;
135 devlist = (struct cctl_devlist_data *)user_data;
136 cur_lun = devlist->cur_lun;
138 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
139 sizeof(devlist->cur_sb[0])))
140 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
141 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
143 devlist->cur_sb[devlist->level] = sbuf_new_auto();
144 if (devlist->cur_sb[devlist->level] == NULL)
145 log_err(1, "%s: unable to allocate sbuf", __func__);
147 if (strcmp(name, "lun") == 0) {
149 log_errx(1, "%s: improper lun element nesting",
152 cur_lun = calloc(1, sizeof(*cur_lun));
154 log_err(1, "%s: cannot allocate %zd bytes", __func__,
158 devlist->cur_lun = cur_lun;
160 STAILQ_INIT(&cur_lun->attr_list);
161 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
163 for (i = 0; attr[i] != NULL; i += 2) {
164 if (strcmp(attr[i], "id") == 0) {
165 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
167 log_errx(1, "%s: invalid LUN attribute %s = %s",
168 __func__, attr[i], attr[i+1]);
175 cctl_end_element(void *user_data, const char *name)
177 struct cctl_devlist_data *devlist;
178 struct cctl_lun *cur_lun;
181 devlist = (struct cctl_devlist_data *)user_data;
182 cur_lun = devlist->cur_lun;
184 if ((cur_lun == NULL)
185 && (strcmp(name, "ctllunlist") != 0))
186 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
188 if (devlist->cur_sb[devlist->level] == NULL)
189 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
190 devlist->level, name);
192 sbuf_finish(devlist->cur_sb[devlist->level]);
193 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
195 if (strlen(str) == 0) {
200 sbuf_delete(devlist->cur_sb[devlist->level]);
201 devlist->cur_sb[devlist->level] = NULL;
204 if (strcmp(name, "backend_type") == 0) {
205 cur_lun->backend_type = str;
207 } else if (strcmp(name, "size") == 0) {
208 cur_lun->size_blocks = strtoull(str, NULL, 0);
209 } else if (strcmp(name, "blocksize") == 0) {
210 cur_lun->blocksize = strtoul(str, NULL, 0);
211 } else if (strcmp(name, "serial_number") == 0) {
212 cur_lun->serial_number = str;
214 } else if (strcmp(name, "device_id") == 0) {
215 cur_lun->device_id = str;
217 } else if (strcmp(name, "cfiscsi_target") == 0) {
218 cur_lun->cfiscsi_target = str;
220 } else if (strcmp(name, "cfiscsi_target_alias") == 0) {
221 cur_lun->cfiscsi_target_alias = str;
223 } else if (strcmp(name, "cfiscsi_lun") == 0) {
224 cur_lun->cfiscsi_lun = strtoul(str, NULL, 0);
225 } else if (strcmp(name, "lun") == 0) {
226 devlist->cur_lun = NULL;
227 } else if (strcmp(name, "ctllunlist") == 0) {
230 struct cctl_lun_nv *nv;
232 nv = calloc(1, sizeof(*nv));
234 log_err(1, "%s: can't allocate %zd bytes for nv pair",
235 __func__, sizeof(*nv));
237 nv->name = checked_strdup(name);
241 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
248 cctl_char_handler(void *user_data, const XML_Char *str, int len)
250 struct cctl_devlist_data *devlist;
252 devlist = (struct cctl_devlist_data *)user_data;
254 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
258 conf_new_from_kernel(void)
260 struct conf *conf = NULL;
263 struct lun_option *lo;
264 struct ctl_lun_list list;
265 struct cctl_devlist_data devlist;
266 struct cctl_lun *lun;
268 char *lun_str = NULL;
274 bzero(&devlist, sizeof(devlist));
275 STAILQ_INIT(&devlist.lun_list);
277 log_debugx("obtaining previously configured CTL luns from the kernel");
280 lun_str = realloc(lun_str, lun_len);
282 log_err(1, "realloc");
284 bzero(&list, sizeof(list));
285 list.alloc_len = lun_len;
286 list.status = CTL_LUN_LIST_NONE;
287 list.lun_xml = lun_str;
289 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
290 log_warn("error issuing CTL_LUN_LIST ioctl");
295 if (list.status == CTL_LUN_LIST_ERROR) {
296 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
302 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
303 lun_len = lun_len << 1;
307 parser = XML_ParserCreate(NULL);
308 if (parser == NULL) {
309 log_warnx("unable to create XML parser");
314 XML_SetUserData(parser, &devlist);
315 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
316 XML_SetCharacterDataHandler(parser, cctl_char_handler);
318 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
319 XML_ParserFree(parser);
322 log_warnx("XML_Parse failed");
328 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
329 struct cctl_lun_nv *nv;
331 if (lun->cfiscsi_target == NULL) {
332 log_debugx("CTL lun %ju wasn't managed by ctld; "
333 "ignoring", (uintmax_t)lun->lun_id);
337 targ = target_find(conf, lun->cfiscsi_target);
340 log_debugx("found new kernel target %s for CTL lun %ld",
341 lun->cfiscsi_target, lun->lun_id);
343 targ = target_new(conf, lun->cfiscsi_target);
345 log_warnx("target_new failed");
350 cl = lun_find(targ, lun->cfiscsi_lun);
352 log_warnx("found CTL lun %ju, backing lun %d, target "
353 "%s, also backed by CTL lun %d; ignoring",
354 (uintmax_t) lun->lun_id, cl->l_lun,
355 cl->l_target->t_iqn, cl->l_ctl_lun);
359 log_debugx("found CTL lun %ju, backing lun %d, target %s",
360 (uintmax_t)lun->lun_id, lun->cfiscsi_lun, lun->cfiscsi_target);
362 cl = lun_new(targ, lun->cfiscsi_lun);
364 log_warnx("lun_new failed");
367 lun_set_backend(cl, lun->backend_type);
368 lun_set_blocksize(cl, lun->blocksize);
369 lun_set_device_id(cl, lun->device_id);
370 lun_set_serial(cl, lun->serial_number);
371 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
372 lun_set_ctl_lun(cl, lun->lun_id);
374 STAILQ_FOREACH(nv, &lun->attr_list, links) {
375 if (strcmp(nv->name, "file") == 0 ||
376 strcmp(nv->name, "dev") == 0) {
377 lun_set_path(cl, nv->value);
380 lo = lun_option_new(cl, nv->name, nv->value);
382 log_warnx("unable to add CTL lun option %s "
383 "for CTL lun %ju for lun %d, target %s",
384 nv->name, (uintmax_t) lun->lun_id,
385 cl->l_lun, cl->l_target->t_iqn);
393 kernel_lun_add(struct lun *lun)
395 struct lun_option *lo;
396 struct ctl_lun_req req;
398 int error, i, num_options;
400 bzero(&req, sizeof(req));
402 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
403 req.reqtype = CTL_LUNREQ_CREATE;
405 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
407 if (lun->l_size != 0)
408 req.reqdata.create.lun_size_bytes = lun->l_size;
410 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
411 req.reqdata.create.device_type = T_DIRECT;
413 if (lun->l_serial != NULL) {
414 strlcpy(req.reqdata.create.serial_num, lun->l_serial,
415 sizeof(req.reqdata.create.serial_num));
416 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
419 if (lun->l_device_id != NULL) {
420 strlcpy(req.reqdata.create.device_id, lun->l_device_id,
421 sizeof(req.reqdata.create.device_id));
422 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
425 if (lun->l_path != NULL) {
426 lo = lun_option_find(lun, "file");
428 lun_option_set(lo, lun->l_path);
430 lo = lun_option_new(lun, "file", lun->l_path);
435 lo = lun_option_find(lun, "cfiscsi_target");
437 lun_option_set(lo, lun->l_target->t_iqn);
439 lo = lun_option_new(lun, "cfiscsi_target",
440 lun->l_target->t_iqn);
444 if (lun->l_target->t_alias != NULL) {
445 lo = lun_option_find(lun, "cfiscsi_target_alias");
447 lun_option_set(lo, lun->l_target->t_alias);
449 lo = lun_option_new(lun, "cfiscsi_target_alias",
450 lun->l_target->t_alias);
455 asprintf(&tmp, "%d", lun->l_lun);
457 log_errx(1, "asprintf");
458 lo = lun_option_find(lun, "cfiscsi_lun");
460 lun_option_set(lo, tmp);
463 lo = lun_option_new(lun, "cfiscsi_lun", tmp);
469 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
472 req.num_be_args = num_options;
473 if (num_options > 0) {
474 req.be_args = malloc(num_options * sizeof(*req.be_args));
475 if (req.be_args == NULL) {
476 log_warn("error allocating %zd bytes",
477 num_options * sizeof(*req.be_args));
482 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
484 * +1 for the terminating '\0'
486 req.be_args[i].namelen = strlen(lo->lo_name) + 1;
487 req.be_args[i].name = lo->lo_name;
488 req.be_args[i].vallen = strlen(lo->lo_value) + 1;
489 req.be_args[i].value = lo->lo_value;
490 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
493 assert(i == num_options);
496 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
499 log_warn("error issuing CTL_LUN_REQ ioctl");
503 if (req.status == CTL_LUN_ERROR) {
504 log_warnx("error returned from LUN creation request: %s",
509 if (req.status != CTL_LUN_OK) {
510 log_warnx("unknown LUN creation request status %d",
515 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
521 kernel_lun_resize(struct lun *lun)
523 struct ctl_lun_req req;
525 bzero(&req, sizeof(req));
527 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
528 req.reqtype = CTL_LUNREQ_MODIFY;
530 req.reqdata.modify.lun_id = lun->l_ctl_lun;
531 req.reqdata.modify.lun_size_bytes = lun->l_size;
533 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
534 log_warn("error issuing CTL_LUN_REQ ioctl");
538 if (req.status == CTL_LUN_ERROR) {
539 log_warnx("error returned from LUN modification request: %s",
544 if (req.status != CTL_LUN_OK) {
545 log_warnx("unknown LUN modification request status %d",
554 kernel_lun_remove(struct lun *lun)
556 struct ctl_lun_req req;
558 bzero(&req, sizeof(req));
560 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
561 req.reqtype = CTL_LUNREQ_RM;
563 req.reqdata.rm.lun_id = lun->l_ctl_lun;
565 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
566 log_warn("error issuing CTL_LUN_REQ ioctl");
570 if (req.status == CTL_LUN_ERROR) {
571 log_warnx("error returned from LUN removal request: %s",
576 if (req.status != CTL_LUN_OK) {
577 log_warnx("unknown LUN removal request status %d", req.status);
585 kernel_handoff(struct connection *conn)
587 struct ctl_iscsi req;
589 bzero(&req, sizeof(req));
591 req.type = CTL_ISCSI_HANDOFF;
592 strlcpy(req.data.handoff.initiator_name,
593 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
594 strlcpy(req.data.handoff.initiator_addr,
595 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
596 if (conn->conn_initiator_alias != NULL) {
597 strlcpy(req.data.handoff.initiator_alias,
598 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
600 strlcpy(req.data.handoff.target_name,
601 conn->conn_target->t_iqn, sizeof(req.data.handoff.target_name));
602 req.data.handoff.socket = conn->conn_socket;
603 req.data.handoff.portal_group_tag =
604 conn->conn_portal->p_portal_group->pg_tag;
605 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
606 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
607 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
608 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
609 req.data.handoff.cmdsn = conn->conn_cmdsn;
610 req.data.handoff.statsn = conn->conn_statsn;
611 req.data.handoff.max_recv_data_segment_length =
612 conn->conn_max_data_segment_length;
613 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
614 req.data.handoff.immediate_data = conn->conn_immediate_data;
616 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
617 log_err(1, "error issuing CTL_ISCSI ioctl; "
618 "dropping connection");
620 if (req.status != CTL_ISCSI_OK)
621 log_errx(1, "error returned from CTL iSCSI handoff request: "
622 "%s; dropping connection", req.error_str);
628 struct ctl_port_entry entry;
631 bzero(&entry, sizeof(entry));
633 entry.port_type = CTL_PORT_ISCSI;
634 entry.targ_port = -1;
636 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
638 log_warn("CTL_ENABLE_PORT ioctl failed");
646 kernel_port_off(void)
648 struct ctl_port_entry entry;
651 bzero(&entry, sizeof(entry));
653 entry.port_type = CTL_PORT_ISCSI;
654 entry.targ_port = -1;
656 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
658 log_warn("CTL_DISABLE_PORT ioctl failed");
665 #ifdef ICL_KERNEL_PROXY
667 kernel_listen(struct addrinfo *ai, bool iser)
669 struct ctl_iscsi req;
671 bzero(&req, sizeof(req));
673 req.type = CTL_ISCSI_LISTEN;
674 req.data.listen.iser = iser;
675 req.data.listen.domain = ai->ai_family;
676 req.data.listen.socktype = ai->ai_socktype;
677 req.data.listen.protocol = ai->ai_protocol;
678 req.data.listen.addr = ai->ai_addr;
679 req.data.listen.addrlen = ai->ai_addrlen;
681 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
682 log_warn("error issuing CTL_ISCSI_LISTEN ioctl");
688 struct ctl_iscsi req;
690 bzero(&req, sizeof(req));
692 req.type = CTL_ISCSI_ACCEPT;
694 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
695 log_warn("error issuing CTL_ISCSI_LISTEN ioctl");
699 return (req.data.accept.connection_id);
703 kernel_send(struct pdu *pdu)
705 struct ctl_iscsi req;
707 bzero(&req, sizeof(req));
709 req.type = CTL_ISCSI_SEND;
710 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
711 req.data.send.bhs = pdu->pdu_bhs;
712 req.data.send.data_segment_len = pdu->pdu_data_len;
713 req.data.send.data_segment = pdu->pdu_data;
715 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
716 log_err(1, "error issuing CTL_ISCSI ioctl; "
717 "dropping connection");
719 if (req.status != CTL_ISCSI_OK)
720 log_errx(1, "error returned from CTL iSCSI send: "
721 "%s; dropping connection", req.error_str);
725 kernel_receive(struct pdu *pdu)
727 struct ctl_iscsi req;
729 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
730 if (pdu->pdu_data == NULL)
731 log_err(1, "malloc");
733 bzero(&req, sizeof(req));
735 req.type = CTL_ISCSI_RECEIVE;
736 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
737 req.data.receive.bhs = pdu->pdu_bhs;
738 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
739 req.data.receive.data_segment = pdu->pdu_data;
741 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
742 log_err(1, "error issuing CTL_ISCSI ioctl; "
743 "dropping connection");
745 if (req.status != CTL_ISCSI_OK)
746 log_errx(1, "error returned from CTL iSCSI receive: "
747 "%s; dropping connection", req.error_str);
751 #endif /* ICL_KERNEL_PROXY */
754 * XXX: I CANT INTO LATIN
757 kernel_capsicate(void)
761 const unsigned long cmds[] = { CTL_ISCSI };
763 cap_rights_init(&rights, CAP_IOCTL);
764 error = cap_rights_limit(ctl_fd, &rights);
765 if (error != 0 && errno != ENOSYS)
766 log_err(1, "cap_rights_limit");
768 error = cap_ioctls_limit(ctl_fd, cmds,
769 sizeof(cmds) / sizeof(cmds[0]));
770 if (error != 0 && errno != ENOSYS)
771 log_err(1, "cap_ioctls_limit");
774 if (error != 0 && errno != ENOSYS)
775 log_err(1, "cap_enter");
778 log_debugx("Capsicum capability mode enabled");
780 log_warnx("Capsicum capability mode not supported");