kernel.c revision 288760
1/*-
2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
22 * NO WARRANTY
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.
34 *
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: stable/10/usr.sbin/ctld/kernel.c 288760 2015-10-05 09:22:31Z mav $");
39
40#include <sys/ioctl.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <sys/param.h>
44#include <sys/linker.h>
45#include <sys/queue.h>
46#include <sys/callout.h>
47#include <sys/sbuf.h>
48#include <sys/capsicum.h>
49#include <assert.h>
50#include <bsdxml.h>
51#include <ctype.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <stdint.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <strings.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_backend.h>
64#include <cam/ctl/ctl_ioctl.h>
65#include <cam/ctl/ctl_util.h>
66#include <cam/ctl/ctl_scsi_all.h>
67
68#include "ctld.h"
69
70#ifdef ICL_KERNEL_PROXY
71#include <netdb.h>
72#endif
73
74extern bool proxy_mode;
75
76static int	ctl_fd = 0;
77
78void
79kernel_init(void)
80{
81	int retval, saved_errno;
82
83	ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84	if (ctl_fd < 0 && errno == ENOENT) {
85		saved_errno = errno;
86		retval = kldload("ctl");
87		if (retval != -1)
88			ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
89		else
90			errno = saved_errno;
91	}
92	if (ctl_fd < 0)
93		log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
94}
95
96/*
97 * Name/value pair used for per-LUN attributes.
98 */
99struct cctl_lun_nv {
100	char *name;
101	char *value;
102	STAILQ_ENTRY(cctl_lun_nv) links;
103};
104
105/*
106 * Backend LUN information.
107 */
108struct cctl_lun {
109	uint64_t lun_id;
110	char *backend_type;
111	uint64_t size_blocks;
112	uint32_t blocksize;
113	char *serial_number;
114	char *device_id;
115	char *ctld_name;
116	STAILQ_HEAD(,cctl_lun_nv) attr_list;
117	STAILQ_ENTRY(cctl_lun) links;
118};
119
120struct cctl_port {
121	uint32_t port_id;
122	char *port_name;
123	int pp;
124	int vp;
125	int cfiscsi_state;
126	char *cfiscsi_target;
127	uint16_t cfiscsi_portal_group_tag;
128	char *ctld_portal_group_name;
129	STAILQ_HEAD(,cctl_lun_nv) attr_list;
130	STAILQ_ENTRY(cctl_port) links;
131};
132
133struct cctl_devlist_data {
134	int num_luns;
135	STAILQ_HEAD(,cctl_lun) lun_list;
136	struct cctl_lun *cur_lun;
137	int num_ports;
138	STAILQ_HEAD(,cctl_port) port_list;
139	struct cctl_port *cur_port;
140	int level;
141	struct sbuf *cur_sb[32];
142};
143
144static void
145cctl_start_element(void *user_data, const char *name, const char **attr)
146{
147	int i;
148	struct cctl_devlist_data *devlist;
149	struct cctl_lun *cur_lun;
150
151	devlist = (struct cctl_devlist_data *)user_data;
152	cur_lun = devlist->cur_lun;
153	devlist->level++;
154	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
155	    sizeof(devlist->cur_sb[0])))
156		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
157		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
158
159	devlist->cur_sb[devlist->level] = sbuf_new_auto();
160	if (devlist->cur_sb[devlist->level] == NULL)
161		log_err(1, "%s: unable to allocate sbuf", __func__);
162
163	if (strcmp(name, "lun") == 0) {
164		if (cur_lun != NULL)
165			log_errx(1, "%s: improper lun element nesting",
166			    __func__);
167
168		cur_lun = calloc(1, sizeof(*cur_lun));
169		if (cur_lun == NULL)
170			log_err(1, "%s: cannot allocate %zd bytes", __func__,
171			    sizeof(*cur_lun));
172
173		devlist->num_luns++;
174		devlist->cur_lun = cur_lun;
175
176		STAILQ_INIT(&cur_lun->attr_list);
177		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
178
179		for (i = 0; attr[i] != NULL; i += 2) {
180			if (strcmp(attr[i], "id") == 0) {
181				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
182			} else {
183				log_errx(1, "%s: invalid LUN attribute %s = %s",
184				     __func__, attr[i], attr[i+1]);
185			}
186		}
187	}
188}
189
190static void
191cctl_end_element(void *user_data, const char *name)
192{
193	struct cctl_devlist_data *devlist;
194	struct cctl_lun *cur_lun;
195	char *str;
196
197	devlist = (struct cctl_devlist_data *)user_data;
198	cur_lun = devlist->cur_lun;
199
200	if ((cur_lun == NULL)
201	 && (strcmp(name, "ctllunlist") != 0))
202		log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
203
204	if (devlist->cur_sb[devlist->level] == NULL)
205		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
206		     devlist->level, name);
207
208	sbuf_finish(devlist->cur_sb[devlist->level]);
209	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
210
211	if (strlen(str) == 0) {
212		free(str);
213		str = NULL;
214	}
215
216	sbuf_delete(devlist->cur_sb[devlist->level]);
217	devlist->cur_sb[devlist->level] = NULL;
218	devlist->level--;
219
220	if (strcmp(name, "backend_type") == 0) {
221		cur_lun->backend_type = str;
222		str = NULL;
223	} else if (strcmp(name, "size") == 0) {
224		cur_lun->size_blocks = strtoull(str, NULL, 0);
225	} else if (strcmp(name, "blocksize") == 0) {
226		cur_lun->blocksize = strtoul(str, NULL, 0);
227	} else if (strcmp(name, "serial_number") == 0) {
228		cur_lun->serial_number = str;
229		str = NULL;
230	} else if (strcmp(name, "device_id") == 0) {
231		cur_lun->device_id = str;
232		str = NULL;
233	} else if (strcmp(name, "ctld_name") == 0) {
234		cur_lun->ctld_name = str;
235		str = NULL;
236	} else if (strcmp(name, "lun") == 0) {
237		devlist->cur_lun = NULL;
238	} else if (strcmp(name, "ctllunlist") == 0) {
239		/* Nothing. */
240	} else {
241		struct cctl_lun_nv *nv;
242
243		nv = calloc(1, sizeof(*nv));
244		if (nv == NULL)
245			log_err(1, "%s: can't allocate %zd bytes for nv pair",
246			    __func__, sizeof(*nv));
247
248		nv->name = checked_strdup(name);
249
250		nv->value = str;
251		str = NULL;
252		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
253	}
254
255	free(str);
256}
257
258static void
259cctl_start_pelement(void *user_data, const char *name, const char **attr)
260{
261	int i;
262	struct cctl_devlist_data *devlist;
263	struct cctl_port *cur_port;
264
265	devlist = (struct cctl_devlist_data *)user_data;
266	cur_port = devlist->cur_port;
267	devlist->level++;
268	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
269	    sizeof(devlist->cur_sb[0])))
270		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
271		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
272
273	devlist->cur_sb[devlist->level] = sbuf_new_auto();
274	if (devlist->cur_sb[devlist->level] == NULL)
275		log_err(1, "%s: unable to allocate sbuf", __func__);
276
277	if (strcmp(name, "targ_port") == 0) {
278		if (cur_port != NULL)
279			log_errx(1, "%s: improper port element nesting (%s)",
280			    __func__, name);
281
282		cur_port = calloc(1, sizeof(*cur_port));
283		if (cur_port == NULL)
284			log_err(1, "%s: cannot allocate %zd bytes", __func__,
285			    sizeof(*cur_port));
286
287		devlist->num_ports++;
288		devlist->cur_port = cur_port;
289
290		STAILQ_INIT(&cur_port->attr_list);
291		STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
292
293		for (i = 0; attr[i] != NULL; i += 2) {
294			if (strcmp(attr[i], "id") == 0) {
295				cur_port->port_id = strtoul(attr[i+1], NULL, 0);
296			} else {
297				log_errx(1, "%s: invalid LUN attribute %s = %s",
298				     __func__, attr[i], attr[i+1]);
299			}
300		}
301	}
302}
303
304static void
305cctl_end_pelement(void *user_data, const char *name)
306{
307	struct cctl_devlist_data *devlist;
308	struct cctl_port *cur_port;
309	char *str;
310
311	devlist = (struct cctl_devlist_data *)user_data;
312	cur_port = devlist->cur_port;
313
314	if ((cur_port == NULL)
315	 && (strcmp(name, "ctlportlist") != 0))
316		log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
317
318	if (devlist->cur_sb[devlist->level] == NULL)
319		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
320		     devlist->level, name);
321
322	sbuf_finish(devlist->cur_sb[devlist->level]);
323	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
324
325	if (strlen(str) == 0) {
326		free(str);
327		str = NULL;
328	}
329
330	sbuf_delete(devlist->cur_sb[devlist->level]);
331	devlist->cur_sb[devlist->level] = NULL;
332	devlist->level--;
333
334	if (strcmp(name, "port_name") == 0) {
335		cur_port->port_name = str;
336		str = NULL;
337	} else if (strcmp(name, "physical_port") == 0) {
338		cur_port->pp = strtoul(str, NULL, 0);
339	} else if (strcmp(name, "virtual_port") == 0) {
340		cur_port->vp = strtoul(str, NULL, 0);
341	} else if (strcmp(name, "cfiscsi_target") == 0) {
342		cur_port->cfiscsi_target = str;
343		str = NULL;
344	} else if (strcmp(name, "cfiscsi_state") == 0) {
345		cur_port->cfiscsi_state = strtoul(str, NULL, 0);
346	} else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
347		cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
348	} else if (strcmp(name, "ctld_portal_group_name") == 0) {
349		cur_port->ctld_portal_group_name = str;
350		str = NULL;
351	} else if (strcmp(name, "targ_port") == 0) {
352		devlist->cur_port = NULL;
353	} else if (strcmp(name, "ctlportlist") == 0) {
354		/* Nothing. */
355	} else {
356		struct cctl_lun_nv *nv;
357
358		nv = calloc(1, sizeof(*nv));
359		if (nv == NULL)
360			log_err(1, "%s: can't allocate %zd bytes for nv pair",
361			    __func__, sizeof(*nv));
362
363		nv->name = checked_strdup(name);
364
365		nv->value = str;
366		str = NULL;
367		STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
368	}
369
370	free(str);
371}
372
373static void
374cctl_char_handler(void *user_data, const XML_Char *str, int len)
375{
376	struct cctl_devlist_data *devlist;
377
378	devlist = (struct cctl_devlist_data *)user_data;
379
380	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
381}
382
383struct conf *
384conf_new_from_kernel(void)
385{
386	struct conf *conf = NULL;
387	struct target *targ;
388	struct portal_group *pg;
389	struct pport *pp;
390	struct port *cp;
391	struct lun *cl;
392	struct lun_option *lo;
393	struct ctl_lun_list list;
394	struct cctl_devlist_data devlist;
395	struct cctl_lun *lun;
396	struct cctl_port *port;
397	XML_Parser parser;
398	char *str, *name;
399	int len, retval;
400
401	bzero(&devlist, sizeof(devlist));
402	STAILQ_INIT(&devlist.lun_list);
403	STAILQ_INIT(&devlist.port_list);
404
405	log_debugx("obtaining previously configured CTL luns from the kernel");
406
407	str = NULL;
408	len = 4096;
409retry:
410	str = realloc(str, len);
411	if (str == NULL)
412		log_err(1, "realloc");
413
414	bzero(&list, sizeof(list));
415	list.alloc_len = len;
416	list.status = CTL_LUN_LIST_NONE;
417	list.lun_xml = str;
418
419	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
420		log_warn("error issuing CTL_LUN_LIST ioctl");
421		free(str);
422		return (NULL);
423	}
424
425	if (list.status == CTL_LUN_LIST_ERROR) {
426		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
427		    list.error_str);
428		free(str);
429		return (NULL);
430	}
431
432	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
433		len = len << 1;
434		goto retry;
435	}
436
437	parser = XML_ParserCreate(NULL);
438	if (parser == NULL) {
439		log_warnx("unable to create XML parser");
440		free(str);
441		return (NULL);
442	}
443
444	XML_SetUserData(parser, &devlist);
445	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
446	XML_SetCharacterDataHandler(parser, cctl_char_handler);
447
448	retval = XML_Parse(parser, str, strlen(str), 1);
449	XML_ParserFree(parser);
450	free(str);
451	if (retval != 1) {
452		log_warnx("XML_Parse failed");
453		return (NULL);
454	}
455
456	str = NULL;
457	len = 4096;
458retry_port:
459	str = realloc(str, len);
460	if (str == NULL)
461		log_err(1, "realloc");
462
463	bzero(&list, sizeof(list));
464	list.alloc_len = len;
465	list.status = CTL_LUN_LIST_NONE;
466	list.lun_xml = str;
467
468	if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
469		log_warn("error issuing CTL_PORT_LIST ioctl");
470		free(str);
471		return (NULL);
472	}
473
474	if (list.status == CTL_PORT_LIST_ERROR) {
475		log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
476		    list.error_str);
477		free(str);
478		return (NULL);
479	}
480
481	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
482		len = len << 1;
483		goto retry_port;
484	}
485
486	parser = XML_ParserCreate(NULL);
487	if (parser == NULL) {
488		log_warnx("unable to create XML parser");
489		free(str);
490		return (NULL);
491	}
492
493	XML_SetUserData(parser, &devlist);
494	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
495	XML_SetCharacterDataHandler(parser, cctl_char_handler);
496
497	retval = XML_Parse(parser, str, strlen(str), 1);
498	XML_ParserFree(parser);
499	free(str);
500	if (retval != 1) {
501		log_warnx("XML_Parse failed");
502		return (NULL);
503	}
504
505	conf = conf_new();
506
507	name = NULL;
508	STAILQ_FOREACH(port, &devlist.port_list, links) {
509		if (name)
510			free(name);
511		if (port->pp == 0 && port->vp == 0)
512			name = checked_strdup(port->port_name);
513		else if (port->vp == 0)
514			asprintf(&name, "%s/%d", port->port_name, port->pp);
515		else
516			asprintf(&name, "%s/%d/%d", port->port_name, port->pp,
517			    port->vp);
518
519		if (port->cfiscsi_target == NULL) {
520			log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
521			    port->port_id, name);
522			pp = pport_find(conf, name);
523			if (pp == NULL) {
524#if 0
525				log_debugx("found new kernel port %u \"%s\"",
526				    port->port_id, name);
527#endif
528				pp = pport_new(conf, name, port->port_id);
529				if (pp == NULL) {
530					log_warnx("pport_new failed");
531					continue;
532				}
533			}
534			continue;
535		}
536		if (port->cfiscsi_state != 1) {
537			log_debugx("CTL port %ju is not active (%d); ignoring",
538			    (uintmax_t)port->port_id, port->cfiscsi_state);
539			continue;
540		}
541
542		targ = target_find(conf, port->cfiscsi_target);
543		if (targ == NULL) {
544#if 0
545			log_debugx("found new kernel target %s for CTL port %ld",
546			    port->cfiscsi_target, port->port_id);
547#endif
548			targ = target_new(conf, port->cfiscsi_target);
549			if (targ == NULL) {
550				log_warnx("target_new failed");
551				continue;
552			}
553		}
554
555		if (port->ctld_portal_group_name == NULL)
556			continue;
557		pg = portal_group_find(conf, port->ctld_portal_group_name);
558		if (pg == NULL) {
559#if 0
560			log_debugx("found new kernel portal group %s for CTL port %ld",
561			    port->ctld_portal_group_name, port->port_id);
562#endif
563			pg = portal_group_new(conf, port->ctld_portal_group_name);
564			if (pg == NULL) {
565				log_warnx("portal_group_new failed");
566				continue;
567			}
568		}
569		pg->pg_tag = port->cfiscsi_portal_group_tag;
570		cp = port_new(conf, targ, pg);
571		if (cp == NULL) {
572			log_warnx("port_new failed");
573			continue;
574		}
575		cp->p_ctl_port = port->port_id;
576	}
577	if (name)
578		free(name);
579
580	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
581		struct cctl_lun_nv *nv;
582
583		if (lun->ctld_name == NULL) {
584			log_debugx("CTL lun %ju wasn't managed by ctld; "
585			    "ignoring", (uintmax_t)lun->lun_id);
586			continue;
587		}
588
589		cl = lun_find(conf, lun->ctld_name);
590		if (cl != NULL) {
591			log_warnx("found CTL lun %ju \"%s\", "
592			    "also backed by CTL lun %d; ignoring",
593			    (uintmax_t)lun->lun_id, lun->ctld_name,
594			    cl->l_ctl_lun);
595			continue;
596		}
597
598		log_debugx("found CTL lun %ju \"%s\"",
599		    (uintmax_t)lun->lun_id, lun->ctld_name);
600
601		cl = lun_new(conf, lun->ctld_name);
602		if (cl == NULL) {
603			log_warnx("lun_new failed");
604			continue;
605		}
606		lun_set_backend(cl, lun->backend_type);
607		lun_set_blocksize(cl, lun->blocksize);
608		lun_set_device_id(cl, lun->device_id);
609		lun_set_serial(cl, lun->serial_number);
610		lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
611		lun_set_ctl_lun(cl, lun->lun_id);
612
613		STAILQ_FOREACH(nv, &lun->attr_list, links) {
614			if (strcmp(nv->name, "file") == 0 ||
615			    strcmp(nv->name, "dev") == 0) {
616				lun_set_path(cl, nv->value);
617				continue;
618			}
619			lo = lun_option_new(cl, nv->name, nv->value);
620			if (lo == NULL)
621				log_warnx("unable to add CTL lun option %s "
622				    "for CTL lun %ju \"%s\"",
623				    nv->name, (uintmax_t) lun->lun_id,
624				    cl->l_name);
625		}
626	}
627
628	return (conf);
629}
630
631static void
632str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
633{
634
635	arg->namelen = strlen(name) + 1;
636	arg->name = __DECONST(char *, name);
637	arg->vallen = strlen(value) + 1;
638	arg->value = __DECONST(char *, value);
639	arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
640}
641
642int
643kernel_lun_add(struct lun *lun)
644{
645	struct lun_option *lo;
646	struct ctl_lun_req req;
647	int error, i, num_options;
648
649	bzero(&req, sizeof(req));
650
651	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
652	req.reqtype = CTL_LUNREQ_CREATE;
653
654	req.reqdata.create.blocksize_bytes = lun->l_blocksize;
655
656	if (lun->l_size != 0)
657		req.reqdata.create.lun_size_bytes = lun->l_size;
658
659	if (lun->l_ctl_lun >= 0) {
660		req.reqdata.create.req_lun_id = lun->l_ctl_lun;
661		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
662	}
663
664	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
665	req.reqdata.create.device_type = T_DIRECT;
666
667	if (lun->l_serial != NULL) {
668		strncpy(req.reqdata.create.serial_num, lun->l_serial,
669			sizeof(req.reqdata.create.serial_num));
670		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
671	}
672
673	if (lun->l_device_id != NULL) {
674		strncpy(req.reqdata.create.device_id, lun->l_device_id,
675			sizeof(req.reqdata.create.device_id));
676		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
677	}
678
679	if (lun->l_path != NULL) {
680		lo = lun_option_find(lun, "file");
681		if (lo != NULL) {
682			lun_option_set(lo, lun->l_path);
683		} else {
684			lo = lun_option_new(lun, "file", lun->l_path);
685			assert(lo != NULL);
686		}
687	}
688
689	lo = lun_option_find(lun, "ctld_name");
690	if (lo != NULL) {
691		lun_option_set(lo, lun->l_name);
692	} else {
693		lo = lun_option_new(lun, "ctld_name", lun->l_name);
694		assert(lo != NULL);
695	}
696
697	lo = lun_option_find(lun, "scsiname");
698	if (lo == NULL && lun->l_scsiname != NULL) {
699		lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
700		assert(lo != NULL);
701	}
702
703	num_options = 0;
704	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
705		num_options++;
706
707	req.num_be_args = num_options;
708	if (num_options > 0) {
709		req.be_args = malloc(num_options * sizeof(*req.be_args));
710		if (req.be_args == NULL) {
711			log_warn("error allocating %zd bytes",
712			    num_options * sizeof(*req.be_args));
713			return (1);
714		}
715
716		i = 0;
717		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
718			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
719			i++;
720		}
721		assert(i == num_options);
722	}
723
724	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
725	free(req.be_args);
726	if (error != 0) {
727		log_warn("error issuing CTL_LUN_REQ ioctl");
728		return (1);
729	}
730
731	switch (req.status) {
732	case CTL_LUN_ERROR:
733		log_warnx("LUN creation error: %s", req.error_str);
734		return (1);
735	case CTL_LUN_WARNING:
736		log_warnx("LUN creation warning: %s", req.error_str);
737		break;
738	case CTL_LUN_OK:
739		break;
740	default:
741		log_warnx("unknown LUN creation status: %d",
742		    req.status);
743		return (1);
744	}
745
746	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
747	return (0);
748}
749
750int
751kernel_lun_modify(struct lun *lun)
752{
753	struct lun_option *lo;
754	struct ctl_lun_req req;
755	int error, i, num_options;
756
757	bzero(&req, sizeof(req));
758
759	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
760	req.reqtype = CTL_LUNREQ_MODIFY;
761
762	req.reqdata.modify.lun_id = lun->l_ctl_lun;
763	req.reqdata.modify.lun_size_bytes = lun->l_size;
764
765	num_options = 0;
766	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
767		num_options++;
768
769	req.num_be_args = num_options;
770	if (num_options > 0) {
771		req.be_args = malloc(num_options * sizeof(*req.be_args));
772		if (req.be_args == NULL) {
773			log_warn("error allocating %zd bytes",
774			    num_options * sizeof(*req.be_args));
775			return (1);
776		}
777
778		i = 0;
779		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
780			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
781			i++;
782		}
783		assert(i == num_options);
784	}
785
786	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
787	free(req.be_args);
788	if (error != 0) {
789		log_warn("error issuing CTL_LUN_REQ ioctl");
790		return (1);
791	}
792
793	switch (req.status) {
794	case CTL_LUN_ERROR:
795		log_warnx("LUN modification error: %s", req.error_str);
796		return (1);
797	case CTL_LUN_WARNING:
798		log_warnx("LUN modification warning: %s", req.error_str);
799		break;
800	case CTL_LUN_OK:
801		break;
802	default:
803		log_warnx("unknown LUN modification status: %d",
804		    req.status);
805		return (1);
806	}
807
808	return (0);
809}
810
811int
812kernel_lun_remove(struct lun *lun)
813{
814	struct ctl_lun_req req;
815
816	bzero(&req, sizeof(req));
817
818	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
819	req.reqtype = CTL_LUNREQ_RM;
820
821	req.reqdata.rm.lun_id = lun->l_ctl_lun;
822
823	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
824		log_warn("error issuing CTL_LUN_REQ ioctl");
825		return (1);
826	}
827
828	switch (req.status) {
829	case CTL_LUN_ERROR:
830		log_warnx("LUN removal error: %s", req.error_str);
831		return (1);
832	case CTL_LUN_WARNING:
833		log_warnx("LUN removal warning: %s", req.error_str);
834		break;
835	case CTL_LUN_OK:
836		break;
837	default:
838		log_warnx("unknown LUN removal status: %d", req.status);
839		return (1);
840	}
841
842	return (0);
843}
844
845void
846kernel_handoff(struct connection *conn)
847{
848	struct ctl_iscsi req;
849
850	bzero(&req, sizeof(req));
851
852	req.type = CTL_ISCSI_HANDOFF;
853	strlcpy(req.data.handoff.initiator_name,
854	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
855	strlcpy(req.data.handoff.initiator_addr,
856	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
857	if (conn->conn_initiator_alias != NULL) {
858		strlcpy(req.data.handoff.initiator_alias,
859		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
860	}
861	memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
862	    sizeof(req.data.handoff.initiator_isid));
863	strlcpy(req.data.handoff.target_name,
864	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
865#ifdef ICL_KERNEL_PROXY
866	if (proxy_mode)
867		req.data.handoff.connection_id = conn->conn_socket;
868	else
869		req.data.handoff.socket = conn->conn_socket;
870#else
871	req.data.handoff.socket = conn->conn_socket;
872#endif
873	req.data.handoff.portal_group_tag =
874	    conn->conn_portal->p_portal_group->pg_tag;
875	if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
876		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
877	if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
878		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
879	req.data.handoff.cmdsn = conn->conn_cmdsn;
880	req.data.handoff.statsn = conn->conn_statsn;
881	req.data.handoff.max_recv_data_segment_length =
882	    conn->conn_max_data_segment_length;
883	req.data.handoff.max_burst_length = conn->conn_max_burst_length;
884	req.data.handoff.immediate_data = conn->conn_immediate_data;
885
886	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
887		log_err(1, "error issuing CTL_ISCSI ioctl; "
888		    "dropping connection");
889	}
890
891	if (req.status != CTL_ISCSI_OK) {
892		log_errx(1, "error returned from CTL iSCSI handoff request: "
893		    "%s; dropping connection", req.error_str);
894	}
895}
896
897int
898kernel_port_add(struct port *port)
899{
900	struct ctl_port_entry entry;
901	struct ctl_req req;
902	struct ctl_lun_map lm;
903	struct target *targ = port->p_target;
904	struct portal_group *pg = port->p_portal_group;
905	char tagstr[16];
906	int error, i, n;
907
908	/* Create iSCSI port. */
909	if (port->p_portal_group) {
910		bzero(&req, sizeof(req));
911		strlcpy(req.driver, "iscsi", sizeof(req.driver));
912		req.reqtype = CTL_REQ_CREATE;
913		req.num_args = 5;
914		req.args = malloc(req.num_args * sizeof(*req.args));
915		if (req.args == NULL)
916			log_err(1, "malloc");
917		n = 0;
918		req.args[n].namelen = sizeof("port_id");
919		req.args[n].name = __DECONST(char *, "port_id");
920		req.args[n].vallen = sizeof(port->p_ctl_port);
921		req.args[n].value = &port->p_ctl_port;
922		req.args[n++].flags = CTL_BEARG_WR;
923		str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
924		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
925		str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
926		if (targ->t_alias)
927			str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
928		str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
929		req.num_args = n;
930		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
931		free(req.args);
932		if (error != 0) {
933			log_warn("error issuing CTL_PORT_REQ ioctl");
934			return (1);
935		}
936		if (req.status == CTL_LUN_ERROR) {
937			log_warnx("error returned from port creation request: %s",
938			    req.error_str);
939			return (1);
940		}
941		if (req.status != CTL_LUN_OK) {
942			log_warnx("unknown port creation request status %d",
943			    req.status);
944			return (1);
945		}
946	} else if (port->p_pport) {
947		port->p_ctl_port = port->p_pport->pp_ctl_port;
948
949		if (strncmp(targ->t_name, "naa.", 4) == 0 &&
950		    strlen(targ->t_name) == 20) {
951			bzero(&entry, sizeof(entry));
952			entry.port_type = CTL_PORT_NONE;
953			entry.targ_port = port->p_ctl_port;
954			entry.flags |= CTL_PORT_WWNN_VALID;
955			entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
956			if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
957				log_warn("CTL_SET_PORT_WWNS ioctl failed");
958		}
959	}
960
961	/* Explicitly enable mapping to block any access except allowed. */
962	lm.port = port->p_ctl_port;
963	lm.plun = UINT32_MAX;
964	lm.lun = 0;
965	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
966	if (error != 0)
967		log_warn("CTL_LUN_MAP ioctl failed");
968
969	/* Map configured LUNs */
970	for (i = 0; i < MAX_LUNS; i++) {
971		if (targ->t_luns[i] == NULL)
972			continue;
973		lm.port = port->p_ctl_port;
974		lm.plun = i;
975		lm.lun = targ->t_luns[i]->l_ctl_lun;
976		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
977		if (error != 0)
978			log_warn("CTL_LUN_MAP ioctl failed");
979	}
980
981	/* Enable port */
982	bzero(&entry, sizeof(entry));
983	entry.targ_port = port->p_ctl_port;
984	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
985	if (error != 0) {
986		log_warn("CTL_ENABLE_PORT ioctl failed");
987		return (-1);
988	}
989
990	return (0);
991}
992
993int
994kernel_port_update(struct port *port, struct port *oport)
995{
996	struct ctl_lun_map lm;
997	struct target *targ = port->p_target;
998	struct target *otarg = oport->p_target;
999	int error, i;
1000	uint32_t olun;
1001
1002	/* Map configured LUNs and unmap others */
1003	for (i = 0; i < MAX_LUNS; i++) {
1004		lm.port = port->p_ctl_port;
1005		lm.plun = i;
1006		if (targ->t_luns[i] == NULL)
1007			lm.lun = UINT32_MAX;
1008		else
1009			lm.lun = targ->t_luns[i]->l_ctl_lun;
1010		if (otarg->t_luns[i] == NULL)
1011			olun = UINT32_MAX;
1012		else
1013			olun = otarg->t_luns[i]->l_ctl_lun;
1014		if (lm.lun == olun)
1015			continue;
1016		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1017		if (error != 0)
1018			log_warn("CTL_LUN_MAP ioctl failed");
1019	}
1020	return (0);
1021}
1022
1023int
1024kernel_port_remove(struct port *port)
1025{
1026	struct ctl_port_entry entry;
1027	struct ctl_lun_map lm;
1028	struct ctl_req req;
1029	char tagstr[16];
1030	struct target *targ = port->p_target;
1031	struct portal_group *pg = port->p_portal_group;
1032	int error;
1033
1034	/* Disable port */
1035	bzero(&entry, sizeof(entry));
1036	entry.targ_port = port->p_ctl_port;
1037	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1038	if (error != 0) {
1039		log_warn("CTL_DISABLE_PORT ioctl failed");
1040		return (-1);
1041	}
1042
1043	/* Remove iSCSI port. */
1044	if (port->p_portal_group) {
1045		bzero(&req, sizeof(req));
1046		strlcpy(req.driver, "iscsi", sizeof(req.driver));
1047		req.reqtype = CTL_REQ_REMOVE;
1048		req.num_args = 2;
1049		req.args = malloc(req.num_args * sizeof(*req.args));
1050		if (req.args == NULL)
1051			log_err(1, "malloc");
1052		str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1053		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1054		str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1055		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1056		free(req.args);
1057		if (error != 0) {
1058			log_warn("error issuing CTL_PORT_REQ ioctl");
1059			return (1);
1060		}
1061		if (req.status == CTL_LUN_ERROR) {
1062			log_warnx("error returned from port removal request: %s",
1063			    req.error_str);
1064			return (1);
1065		}
1066		if (req.status != CTL_LUN_OK) {
1067			log_warnx("unknown port removal request status %d",
1068			    req.status);
1069			return (1);
1070		}
1071	} else {
1072		/* Disable LUN mapping. */
1073		lm.port = port->p_ctl_port;
1074		lm.plun = UINT32_MAX;
1075		lm.lun = UINT32_MAX;
1076		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1077		if (error != 0)
1078			log_warn("CTL_LUN_MAP ioctl failed");
1079	}
1080	return (0);
1081}
1082
1083#ifdef ICL_KERNEL_PROXY
1084void
1085kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1086{
1087	struct ctl_iscsi req;
1088
1089	bzero(&req, sizeof(req));
1090
1091	req.type = CTL_ISCSI_LISTEN;
1092	req.data.listen.iser = iser;
1093	req.data.listen.domain = ai->ai_family;
1094	req.data.listen.socktype = ai->ai_socktype;
1095	req.data.listen.protocol = ai->ai_protocol;
1096	req.data.listen.addr = ai->ai_addr;
1097	req.data.listen.addrlen = ai->ai_addrlen;
1098	req.data.listen.portal_id = portal_id;
1099
1100	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1101		log_err(1, "error issuing CTL_ISCSI ioctl");
1102
1103	if (req.status != CTL_ISCSI_OK) {
1104		log_errx(1, "error returned from CTL iSCSI listen: %s",
1105		    req.error_str);
1106	}
1107}
1108
1109void
1110kernel_accept(int *connection_id, int *portal_id,
1111    struct sockaddr *client_sa, socklen_t *client_salen)
1112{
1113	struct ctl_iscsi req;
1114	struct sockaddr_storage ss;
1115
1116	bzero(&req, sizeof(req));
1117
1118	req.type = CTL_ISCSI_ACCEPT;
1119	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1120
1121	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1122		log_err(1, "error issuing CTL_ISCSI ioctl");
1123
1124	if (req.status != CTL_ISCSI_OK) {
1125		log_errx(1, "error returned from CTL iSCSI accept: %s",
1126		    req.error_str);
1127	}
1128
1129	*connection_id = req.data.accept.connection_id;
1130	*portal_id = req.data.accept.portal_id;
1131	*client_salen = req.data.accept.initiator_addrlen;
1132	memcpy(client_sa, &ss, *client_salen);
1133}
1134
1135void
1136kernel_send(struct pdu *pdu)
1137{
1138	struct ctl_iscsi req;
1139
1140	bzero(&req, sizeof(req));
1141
1142	req.type = CTL_ISCSI_SEND;
1143	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1144	req.data.send.bhs = pdu->pdu_bhs;
1145	req.data.send.data_segment_len = pdu->pdu_data_len;
1146	req.data.send.data_segment = pdu->pdu_data;
1147
1148	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1149		log_err(1, "error issuing CTL_ISCSI ioctl; "
1150		    "dropping connection");
1151	}
1152
1153	if (req.status != CTL_ISCSI_OK) {
1154		log_errx(1, "error returned from CTL iSCSI send: "
1155		    "%s; dropping connection", req.error_str);
1156	}
1157}
1158
1159void
1160kernel_receive(struct pdu *pdu)
1161{
1162	struct ctl_iscsi req;
1163
1164	pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1165	if (pdu->pdu_data == NULL)
1166		log_err(1, "malloc");
1167
1168	bzero(&req, sizeof(req));
1169
1170	req.type = CTL_ISCSI_RECEIVE;
1171	req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1172	req.data.receive.bhs = pdu->pdu_bhs;
1173	req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1174	req.data.receive.data_segment = pdu->pdu_data;
1175
1176	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1177		log_err(1, "error issuing CTL_ISCSI ioctl; "
1178		    "dropping connection");
1179	}
1180
1181	if (req.status != CTL_ISCSI_OK) {
1182		log_errx(1, "error returned from CTL iSCSI receive: "
1183		    "%s; dropping connection", req.error_str);
1184	}
1185
1186}
1187
1188#endif /* ICL_KERNEL_PROXY */
1189
1190/*
1191 * XXX: I CANT INTO LATIN
1192 */
1193void
1194kernel_capsicate(void)
1195{
1196	int error;
1197	cap_rights_t rights;
1198	const unsigned long cmds[] = { CTL_ISCSI };
1199
1200	cap_rights_init(&rights, CAP_IOCTL);
1201	error = cap_rights_limit(ctl_fd, &rights);
1202	if (error != 0 && errno != ENOSYS)
1203		log_err(1, "cap_rights_limit");
1204
1205	error = cap_ioctls_limit(ctl_fd, cmds,
1206	    sizeof(cmds) / sizeof(cmds[0]));
1207	if (error != 0 && errno != ENOSYS)
1208		log_err(1, "cap_ioctls_limit");
1209
1210	error = cap_enter();
1211	if (error != 0 && errno != ENOSYS)
1212		log_err(1, "cap_enter");
1213
1214	if (cap_sandboxed())
1215		log_debugx("Capsicum capability mode enabled");
1216	else
1217		log_warnx("Capsicum capability mode not supported");
1218}
1219
1220