kernel.c revision 287757
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: head/usr.sbin/ctld/kernel.c 287757 2015-09-13 15:08:06Z 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	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
660	req.reqdata.create.device_type = T_DIRECT;
661
662	if (lun->l_serial != NULL) {
663		strncpy(req.reqdata.create.serial_num, lun->l_serial,
664			sizeof(req.reqdata.create.serial_num));
665		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
666	}
667
668	if (lun->l_device_id != NULL) {
669		strncpy(req.reqdata.create.device_id, lun->l_device_id,
670			sizeof(req.reqdata.create.device_id));
671		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
672	}
673
674	if (lun->l_path != NULL) {
675		lo = lun_option_find(lun, "file");
676		if (lo != NULL) {
677			lun_option_set(lo, lun->l_path);
678		} else {
679			lo = lun_option_new(lun, "file", lun->l_path);
680			assert(lo != NULL);
681		}
682	}
683
684	lo = lun_option_find(lun, "ctld_name");
685	if (lo != NULL) {
686		lun_option_set(lo, lun->l_name);
687	} else {
688		lo = lun_option_new(lun, "ctld_name", lun->l_name);
689		assert(lo != NULL);
690	}
691
692	lo = lun_option_find(lun, "scsiname");
693	if (lo == NULL && lun->l_scsiname != NULL) {
694		lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
695		assert(lo != NULL);
696	}
697
698	num_options = 0;
699	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
700		num_options++;
701
702	req.num_be_args = num_options;
703	if (num_options > 0) {
704		req.be_args = malloc(num_options * sizeof(*req.be_args));
705		if (req.be_args == NULL) {
706			log_warn("error allocating %zd bytes",
707			    num_options * sizeof(*req.be_args));
708			return (1);
709		}
710
711		i = 0;
712		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
713			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
714			i++;
715		}
716		assert(i == num_options);
717	}
718
719	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
720	free(req.be_args);
721	if (error != 0) {
722		log_warn("error issuing CTL_LUN_REQ ioctl");
723		return (1);
724	}
725
726	switch (req.status) {
727	case CTL_LUN_ERROR:
728		log_warnx("LUN creation error: %s", req.error_str);
729		return (1);
730	case CTL_LUN_WARNING:
731		log_warnx("LUN creation warning: %s", req.error_str);
732		break;
733	case CTL_LUN_OK:
734		break;
735	default:
736		log_warnx("unknown LUN creation status: %d",
737		    req.status);
738		return (1);
739	}
740
741	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
742	return (0);
743}
744
745int
746kernel_lun_modify(struct lun *lun)
747{
748	struct lun_option *lo;
749	struct ctl_lun_req req;
750	int error, i, num_options;
751
752	bzero(&req, sizeof(req));
753
754	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
755	req.reqtype = CTL_LUNREQ_MODIFY;
756
757	req.reqdata.modify.lun_id = lun->l_ctl_lun;
758	req.reqdata.modify.lun_size_bytes = lun->l_size;
759
760	num_options = 0;
761	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
762		num_options++;
763
764	req.num_be_args = num_options;
765	if (num_options > 0) {
766		req.be_args = malloc(num_options * sizeof(*req.be_args));
767		if (req.be_args == NULL) {
768			log_warn("error allocating %zd bytes",
769			    num_options * sizeof(*req.be_args));
770			return (1);
771		}
772
773		i = 0;
774		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
775			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
776			i++;
777		}
778		assert(i == num_options);
779	}
780
781	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
782	free(req.be_args);
783	if (error != 0) {
784		log_warn("error issuing CTL_LUN_REQ ioctl");
785		return (1);
786	}
787
788	switch (req.status) {
789	case CTL_LUN_ERROR:
790		log_warnx("LUN modification error: %s", req.error_str);
791		return (1);
792	case CTL_LUN_WARNING:
793		log_warnx("LUN modification warning: %s", req.error_str);
794		break;
795	case CTL_LUN_OK:
796		break;
797	default:
798		log_warnx("unknown LUN modification status: %d",
799		    req.status);
800		return (1);
801	}
802
803	return (0);
804}
805
806int
807kernel_lun_remove(struct lun *lun)
808{
809	struct ctl_lun_req req;
810
811	bzero(&req, sizeof(req));
812
813	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
814	req.reqtype = CTL_LUNREQ_RM;
815
816	req.reqdata.rm.lun_id = lun->l_ctl_lun;
817
818	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
819		log_warn("error issuing CTL_LUN_REQ ioctl");
820		return (1);
821	}
822
823	switch (req.status) {
824	case CTL_LUN_ERROR:
825		log_warnx("LUN removal error: %s", req.error_str);
826		return (1);
827	case CTL_LUN_WARNING:
828		log_warnx("LUN removal warning: %s", req.error_str);
829		break;
830	case CTL_LUN_OK:
831		break;
832	default:
833		log_warnx("unknown LUN removal status: %d", req.status);
834		return (1);
835	}
836
837	return (0);
838}
839
840void
841kernel_handoff(struct connection *conn)
842{
843	struct ctl_iscsi req;
844
845	bzero(&req, sizeof(req));
846
847	req.type = CTL_ISCSI_HANDOFF;
848	strlcpy(req.data.handoff.initiator_name,
849	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
850	strlcpy(req.data.handoff.initiator_addr,
851	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
852	if (conn->conn_initiator_alias != NULL) {
853		strlcpy(req.data.handoff.initiator_alias,
854		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
855	}
856	memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
857	    sizeof(req.data.handoff.initiator_isid));
858	strlcpy(req.data.handoff.target_name,
859	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
860	if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
861		strlcpy(req.data.handoff.offload,
862		    conn->conn_portal->p_portal_group->pg_offload,
863		    sizeof(req.data.handoff.offload));
864	}
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
897void
898kernel_limits(const char *offload, size_t *max_data_segment_length)
899{
900	struct ctl_iscsi req;
901
902	bzero(&req, sizeof(req));
903
904	req.type = CTL_ISCSI_LIMITS;
905	if (offload != NULL) {
906		strlcpy(req.data.limits.offload, offload,
907		    sizeof(req.data.limits.offload));
908	}
909
910	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
911		log_err(1, "error issuing CTL_ISCSI ioctl; "
912		    "dropping connection");
913	}
914
915	if (req.status != CTL_ISCSI_OK) {
916		log_errx(1, "error returned from CTL iSCSI limits request: "
917		    "%s; dropping connection", req.error_str);
918	}
919
920	*max_data_segment_length = req.data.limits.data_segment_limit;
921	if (offload != NULL) {
922		log_debugx("MaxRecvDataSegment kernel limit for offload "
923		    "\"%s\" is %zd", offload, *max_data_segment_length);
924	} else {
925		log_debugx("MaxRecvDataSegment kernel limit is %zd",
926		    *max_data_segment_length);
927	}
928}
929
930int
931kernel_port_add(struct port *port)
932{
933	struct ctl_port_entry entry;
934	struct ctl_req req;
935	struct ctl_lun_map lm;
936	struct target *targ = port->p_target;
937	struct portal_group *pg = port->p_portal_group;
938	char tagstr[16];
939	int error, i, n;
940
941	/* Create iSCSI port. */
942	if (port->p_portal_group) {
943		bzero(&req, sizeof(req));
944		strlcpy(req.driver, "iscsi", sizeof(req.driver));
945		req.reqtype = CTL_REQ_CREATE;
946		req.num_args = 5;
947		req.args = malloc(req.num_args * sizeof(*req.args));
948		if (req.args == NULL)
949			log_err(1, "malloc");
950		n = 0;
951		req.args[n].namelen = sizeof("port_id");
952		req.args[n].name = __DECONST(char *, "port_id");
953		req.args[n].vallen = sizeof(port->p_ctl_port);
954		req.args[n].value = &port->p_ctl_port;
955		req.args[n++].flags = CTL_BEARG_WR;
956		str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
957		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
958		str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
959		if (targ->t_alias)
960			str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
961		str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
962		req.num_args = n;
963		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
964		free(req.args);
965		if (error != 0) {
966			log_warn("error issuing CTL_PORT_REQ ioctl");
967			return (1);
968		}
969		if (req.status == CTL_LUN_ERROR) {
970			log_warnx("error returned from port creation request: %s",
971			    req.error_str);
972			return (1);
973		}
974		if (req.status != CTL_LUN_OK) {
975			log_warnx("unknown port creation request status %d",
976			    req.status);
977			return (1);
978		}
979	} else if (port->p_pport) {
980		port->p_ctl_port = port->p_pport->pp_ctl_port;
981
982		if (strncmp(targ->t_name, "naa.", 4) == 0 &&
983		    strlen(targ->t_name) == 20) {
984			bzero(&entry, sizeof(entry));
985			entry.port_type = CTL_PORT_NONE;
986			entry.targ_port = port->p_ctl_port;
987			entry.flags |= CTL_PORT_WWNN_VALID;
988			entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
989			if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
990				log_warn("CTL_SET_PORT_WWNS ioctl failed");
991		}
992	}
993
994	/* Explicitly enable mapping to block any access except allowed. */
995	lm.port = port->p_ctl_port;
996	lm.plun = UINT32_MAX;
997	lm.lun = 0;
998	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
999	if (error != 0)
1000		log_warn("CTL_LUN_MAP ioctl failed");
1001
1002	/* Map configured LUNs */
1003	for (i = 0; i < MAX_LUNS; i++) {
1004		if (targ->t_luns[i] == NULL)
1005			continue;
1006		lm.port = port->p_ctl_port;
1007		lm.plun = i;
1008		lm.lun = targ->t_luns[i]->l_ctl_lun;
1009		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1010		if (error != 0)
1011			log_warn("CTL_LUN_MAP ioctl failed");
1012	}
1013
1014	/* Enable port */
1015	bzero(&entry, sizeof(entry));
1016	entry.targ_port = port->p_ctl_port;
1017	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1018	if (error != 0) {
1019		log_warn("CTL_ENABLE_PORT ioctl failed");
1020		return (-1);
1021	}
1022
1023	return (0);
1024}
1025
1026int
1027kernel_port_update(struct port *port, struct port *oport)
1028{
1029	struct ctl_lun_map lm;
1030	struct target *targ = port->p_target;
1031	struct target *otarg = oport->p_target;
1032	int error, i;
1033	uint32_t olun;
1034
1035	/* Map configured LUNs and unmap others */
1036	for (i = 0; i < MAX_LUNS; i++) {
1037		lm.port = port->p_ctl_port;
1038		lm.plun = i;
1039		if (targ->t_luns[i] == NULL)
1040			lm.lun = UINT32_MAX;
1041		else
1042			lm.lun = targ->t_luns[i]->l_ctl_lun;
1043		if (otarg->t_luns[i] == NULL)
1044			olun = UINT32_MAX;
1045		else
1046			olun = otarg->t_luns[i]->l_ctl_lun;
1047		if (lm.lun == olun)
1048			continue;
1049		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1050		if (error != 0)
1051			log_warn("CTL_LUN_MAP ioctl failed");
1052	}
1053	return (0);
1054}
1055
1056int
1057kernel_port_remove(struct port *port)
1058{
1059	struct ctl_port_entry entry;
1060	struct ctl_lun_map lm;
1061	struct ctl_req req;
1062	char tagstr[16];
1063	struct target *targ = port->p_target;
1064	struct portal_group *pg = port->p_portal_group;
1065	int error;
1066
1067	/* Disable port */
1068	bzero(&entry, sizeof(entry));
1069	entry.targ_port = port->p_ctl_port;
1070	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1071	if (error != 0) {
1072		log_warn("CTL_DISABLE_PORT ioctl failed");
1073		return (-1);
1074	}
1075
1076	/* Remove iSCSI port. */
1077	if (port->p_portal_group) {
1078		bzero(&req, sizeof(req));
1079		strlcpy(req.driver, "iscsi", sizeof(req.driver));
1080		req.reqtype = CTL_REQ_REMOVE;
1081		req.num_args = 2;
1082		req.args = malloc(req.num_args * sizeof(*req.args));
1083		if (req.args == NULL)
1084			log_err(1, "malloc");
1085		str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1086		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1087		str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1088		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1089		free(req.args);
1090		if (error != 0) {
1091			log_warn("error issuing CTL_PORT_REQ ioctl");
1092			return (1);
1093		}
1094		if (req.status == CTL_LUN_ERROR) {
1095			log_warnx("error returned from port removal request: %s",
1096			    req.error_str);
1097			return (1);
1098		}
1099		if (req.status != CTL_LUN_OK) {
1100			log_warnx("unknown port removal request status %d",
1101			    req.status);
1102			return (1);
1103		}
1104	} else {
1105		/* Disable LUN mapping. */
1106		lm.port = port->p_ctl_port;
1107		lm.plun = UINT32_MAX;
1108		lm.lun = UINT32_MAX;
1109		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1110		if (error != 0)
1111			log_warn("CTL_LUN_MAP ioctl failed");
1112	}
1113	return (0);
1114}
1115
1116#ifdef ICL_KERNEL_PROXY
1117void
1118kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1119{
1120	struct ctl_iscsi req;
1121
1122	bzero(&req, sizeof(req));
1123
1124	req.type = CTL_ISCSI_LISTEN;
1125	req.data.listen.iser = iser;
1126	req.data.listen.domain = ai->ai_family;
1127	req.data.listen.socktype = ai->ai_socktype;
1128	req.data.listen.protocol = ai->ai_protocol;
1129	req.data.listen.addr = ai->ai_addr;
1130	req.data.listen.addrlen = ai->ai_addrlen;
1131	req.data.listen.portal_id = portal_id;
1132
1133	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1134		log_err(1, "error issuing CTL_ISCSI ioctl");
1135
1136	if (req.status != CTL_ISCSI_OK) {
1137		log_errx(1, "error returned from CTL iSCSI listen: %s",
1138		    req.error_str);
1139	}
1140}
1141
1142void
1143kernel_accept(int *connection_id, int *portal_id,
1144    struct sockaddr *client_sa, socklen_t *client_salen)
1145{
1146	struct ctl_iscsi req;
1147	struct sockaddr_storage ss;
1148
1149	bzero(&req, sizeof(req));
1150
1151	req.type = CTL_ISCSI_ACCEPT;
1152	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1153
1154	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1155		log_err(1, "error issuing CTL_ISCSI ioctl");
1156
1157	if (req.status != CTL_ISCSI_OK) {
1158		log_errx(1, "error returned from CTL iSCSI accept: %s",
1159		    req.error_str);
1160	}
1161
1162	*connection_id = req.data.accept.connection_id;
1163	*portal_id = req.data.accept.portal_id;
1164	*client_salen = req.data.accept.initiator_addrlen;
1165	memcpy(client_sa, &ss, *client_salen);
1166}
1167
1168void
1169kernel_send(struct pdu *pdu)
1170{
1171	struct ctl_iscsi req;
1172
1173	bzero(&req, sizeof(req));
1174
1175	req.type = CTL_ISCSI_SEND;
1176	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1177	req.data.send.bhs = pdu->pdu_bhs;
1178	req.data.send.data_segment_len = pdu->pdu_data_len;
1179	req.data.send.data_segment = pdu->pdu_data;
1180
1181	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1182		log_err(1, "error issuing CTL_ISCSI ioctl; "
1183		    "dropping connection");
1184	}
1185
1186	if (req.status != CTL_ISCSI_OK) {
1187		log_errx(1, "error returned from CTL iSCSI send: "
1188		    "%s; dropping connection", req.error_str);
1189	}
1190}
1191
1192void
1193kernel_receive(struct pdu *pdu)
1194{
1195	struct ctl_iscsi req;
1196
1197	pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1198	if (pdu->pdu_data == NULL)
1199		log_err(1, "malloc");
1200
1201	bzero(&req, sizeof(req));
1202
1203	req.type = CTL_ISCSI_RECEIVE;
1204	req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1205	req.data.receive.bhs = pdu->pdu_bhs;
1206	req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1207	req.data.receive.data_segment = pdu->pdu_data;
1208
1209	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1210		log_err(1, "error issuing CTL_ISCSI ioctl; "
1211		    "dropping connection");
1212	}
1213
1214	if (req.status != CTL_ISCSI_OK) {
1215		log_errx(1, "error returned from CTL iSCSI receive: "
1216		    "%s; dropping connection", req.error_str);
1217	}
1218
1219}
1220
1221#endif /* ICL_KERNEL_PROXY */
1222
1223/*
1224 * XXX: I CANT INTO LATIN
1225 */
1226void
1227kernel_capsicate(void)
1228{
1229	int error;
1230	cap_rights_t rights;
1231	const unsigned long cmds[] = { CTL_ISCSI };
1232
1233	cap_rights_init(&rights, CAP_IOCTL);
1234	error = cap_rights_limit(ctl_fd, &rights);
1235	if (error != 0 && errno != ENOSYS)
1236		log_err(1, "cap_rights_limit");
1237
1238	error = cap_ioctls_limit(ctl_fd, cmds,
1239	    sizeof(cmds) / sizeof(cmds[0]));
1240	if (error != 0 && errno != ENOSYS)
1241		log_err(1, "cap_ioctls_limit");
1242
1243	error = cap_enter();
1244	if (error != 0 && errno != ENOSYS)
1245		log_err(1, "cap_enter");
1246
1247	if (cap_sandboxed())
1248		log_debugx("Capsicum capability mode enabled");
1249	else
1250		log_warnx("Capsicum capability mode not supported");
1251}
1252
1253