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