kernel.c revision 268291
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 * $FreeBSD: head/usr.sbin/ctld/kernel.c 268291 2014-07-05 18:15:00Z mav $
36 */
37
38#include <sys/ioctl.h>
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <sys/param.h>
42#include <sys/linker.h>
43#include <sys/queue.h>
44#include <sys/callout.h>
45#include <sys/sbuf.h>
46#include <sys/capsicum.h>
47#include <assert.h>
48#include <bsdxml.h>
49#include <ctype.h>
50#include <errno.h>
51#include <fcntl.h>
52#include <stdint.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <strings.h>
57#include <cam/scsi/scsi_all.h>
58#include <cam/scsi/scsi_message.h>
59#include <cam/ctl/ctl.h>
60#include <cam/ctl/ctl_io.h>
61#include <cam/ctl/ctl_frontend_internal.h>
62#include <cam/ctl/ctl_backend.h>
63#include <cam/ctl/ctl_ioctl.h>
64#include <cam/ctl/ctl_backend_block.h>
65#include <cam/ctl/ctl_util.h>
66#include <cam/ctl/ctl_scsi_all.h>
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 *cfiscsi_target;
116	char *cfiscsi_target_alias;
117	int cfiscsi_lun;
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 *cfiscsi_target;
125	uint16_t cfiscsi_portal_group_tag;
126	STAILQ_HEAD(,cctl_lun_nv) attr_list;
127	STAILQ_ENTRY(cctl_port) links;
128};
129
130struct cctl_devlist_data {
131	int num_luns;
132	STAILQ_HEAD(,cctl_lun) lun_list;
133	struct cctl_lun *cur_lun;
134	int num_ports;
135	STAILQ_HEAD(,cctl_port) port_list;
136	struct cctl_port *cur_port;
137	int level;
138	struct sbuf *cur_sb[32];
139};
140
141static void
142cctl_start_element(void *user_data, const char *name, const char **attr)
143{
144	int i;
145	struct cctl_devlist_data *devlist;
146	struct cctl_lun *cur_lun;
147
148	devlist = (struct cctl_devlist_data *)user_data;
149	cur_lun = devlist->cur_lun;
150	devlist->level++;
151	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
152	    sizeof(devlist->cur_sb[0])))
153		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
154		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
155
156	devlist->cur_sb[devlist->level] = sbuf_new_auto();
157	if (devlist->cur_sb[devlist->level] == NULL)
158		log_err(1, "%s: unable to allocate sbuf", __func__);
159
160	if (strcmp(name, "lun") == 0) {
161		if (cur_lun != NULL)
162			log_errx(1, "%s: improper lun element nesting",
163			    __func__);
164
165		cur_lun = calloc(1, sizeof(*cur_lun));
166		if (cur_lun == NULL)
167			log_err(1, "%s: cannot allocate %zd bytes", __func__,
168			    sizeof(*cur_lun));
169
170		devlist->num_luns++;
171		devlist->cur_lun = cur_lun;
172
173		STAILQ_INIT(&cur_lun->attr_list);
174		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
175
176		for (i = 0; attr[i] != NULL; i += 2) {
177			if (strcmp(attr[i], "id") == 0) {
178				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
179			} else {
180				log_errx(1, "%s: invalid LUN attribute %s = %s",
181				     __func__, attr[i], attr[i+1]);
182			}
183		}
184	}
185}
186
187static void
188cctl_end_element(void *user_data, const char *name)
189{
190	struct cctl_devlist_data *devlist;
191	struct cctl_lun *cur_lun;
192	char *str;
193
194	devlist = (struct cctl_devlist_data *)user_data;
195	cur_lun = devlist->cur_lun;
196
197	if ((cur_lun == NULL)
198	 && (strcmp(name, "ctllunlist") != 0))
199		log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
200
201	if (devlist->cur_sb[devlist->level] == NULL)
202		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
203		     devlist->level, name);
204
205	sbuf_finish(devlist->cur_sb[devlist->level]);
206	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
207
208	if (strlen(str) == 0) {
209		free(str);
210		str = NULL;
211	}
212
213	sbuf_delete(devlist->cur_sb[devlist->level]);
214	devlist->cur_sb[devlist->level] = NULL;
215	devlist->level--;
216
217	if (strcmp(name, "backend_type") == 0) {
218		cur_lun->backend_type = str;
219		str = NULL;
220	} else if (strcmp(name, "size") == 0) {
221		cur_lun->size_blocks = strtoull(str, NULL, 0);
222	} else if (strcmp(name, "blocksize") == 0) {
223		cur_lun->blocksize = strtoul(str, NULL, 0);
224	} else if (strcmp(name, "serial_number") == 0) {
225		cur_lun->serial_number = str;
226		str = NULL;
227	} else if (strcmp(name, "device_id") == 0) {
228		cur_lun->device_id = str;
229		str = NULL;
230	} else if (strcmp(name, "cfiscsi_target") == 0) {
231		cur_lun->cfiscsi_target = str;
232		str = NULL;
233	} else if (strcmp(name, "cfiscsi_target_alias") == 0) {
234		cur_lun->cfiscsi_target_alias = str;
235		str = NULL;
236	} else if (strcmp(name, "cfiscsi_lun") == 0) {
237		cur_lun->cfiscsi_lun = strtoul(str, NULL, 0);
238	} else if (strcmp(name, "lun") == 0) {
239		devlist->cur_lun = NULL;
240	} else if (strcmp(name, "ctllunlist") == 0) {
241
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, "cfiscsi_target") == 0) {
337		cur_port->cfiscsi_target = str;
338		str = NULL;
339	} else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
340		cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
341	} else if (strcmp(name, "targ_port") == 0) {
342		devlist->cur_port = NULL;
343	} else if (strcmp(name, "ctlportlist") == 0) {
344
345	} else {
346		struct cctl_lun_nv *nv;
347
348		nv = calloc(1, sizeof(*nv));
349		if (nv == NULL)
350			log_err(1, "%s: can't allocate %zd bytes for nv pair",
351			    __func__, sizeof(*nv));
352
353		nv->name = checked_strdup(name);
354
355		nv->value = str;
356		str = NULL;
357		STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
358	}
359
360	free(str);
361}
362
363static void
364cctl_char_handler(void *user_data, const XML_Char *str, int len)
365{
366	struct cctl_devlist_data *devlist;
367
368	devlist = (struct cctl_devlist_data *)user_data;
369
370	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
371}
372
373struct conf *
374conf_new_from_kernel(void)
375{
376	struct conf *conf = NULL;
377	struct target *targ;
378	struct lun *cl;
379	struct lun_option *lo;
380	struct ctl_lun_list list;
381	struct cctl_devlist_data devlist;
382	struct cctl_lun *lun;
383	struct cctl_port *port;
384	XML_Parser parser;
385	char *str;
386	int len, retval;
387
388	bzero(&devlist, sizeof(devlist));
389	STAILQ_INIT(&devlist.lun_list);
390	STAILQ_INIT(&devlist.port_list);
391
392	log_debugx("obtaining previously configured CTL luns from the kernel");
393
394	str = NULL;
395	len = 4096;
396retry:
397	str = realloc(str, len);
398	if (str == NULL)
399		log_err(1, "realloc");
400
401	bzero(&list, sizeof(list));
402	list.alloc_len = len;
403	list.status = CTL_LUN_LIST_NONE;
404	list.lun_xml = str;
405
406	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
407		log_warn("error issuing CTL_LUN_LIST ioctl");
408		free(str);
409		return (NULL);
410	}
411
412	if (list.status == CTL_LUN_LIST_ERROR) {
413		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
414		    list.error_str);
415		free(str);
416		return (NULL);
417	}
418
419	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
420		len = len << 1;
421		goto retry;
422	}
423
424	parser = XML_ParserCreate(NULL);
425	if (parser == NULL) {
426		log_warnx("unable to create XML parser");
427		free(str);
428		return (NULL);
429	}
430
431	XML_SetUserData(parser, &devlist);
432	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
433	XML_SetCharacterDataHandler(parser, cctl_char_handler);
434
435	retval = XML_Parse(parser, str, strlen(str), 1);
436	XML_ParserFree(parser);
437	free(str);
438	if (retval != 1) {
439		log_warnx("XML_Parse failed");
440		return (NULL);
441	}
442
443	str = NULL;
444	len = 4096;
445retry_port:
446	str = realloc(str, len);
447	if (str == NULL)
448		log_err(1, "realloc");
449
450	bzero(&list, sizeof(list));
451	list.alloc_len = len;
452	list.status = CTL_LUN_LIST_NONE;
453	list.lun_xml = str;
454
455	if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
456		log_warn("error issuing CTL_PORT_LIST ioctl");
457		free(str);
458		return (NULL);
459	}
460
461	if (list.status == CTL_PORT_LIST_ERROR) {
462		log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
463		    list.error_str);
464		free(str);
465		return (NULL);
466	}
467
468	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
469		len = len << 1;
470		goto retry_port;
471	}
472
473	parser = XML_ParserCreate(NULL);
474	if (parser == NULL) {
475		log_warnx("unable to create XML parser");
476		free(str);
477		return (NULL);
478	}
479
480	XML_SetUserData(parser, &devlist);
481	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
482	XML_SetCharacterDataHandler(parser, cctl_char_handler);
483
484	retval = XML_Parse(parser, str, strlen(str), 1);
485	XML_ParserFree(parser);
486	free(str);
487	if (retval != 1) {
488		log_warnx("XML_Parse failed");
489		return (NULL);
490	}
491
492	conf = conf_new();
493
494	STAILQ_FOREACH(port, &devlist.port_list, links) {
495
496		if (port->cfiscsi_target == NULL) {
497			log_debugx("CTL port %ju wasn't managed by ctld; "
498			    "ignoring", (uintmax_t)port->port_id);
499			continue;
500		}
501
502		targ = target_find(conf, port->cfiscsi_target);
503		if (targ == NULL) {
504#if 0
505			log_debugx("found new kernel target %s for CTL port %ld",
506			    port->cfiscsi_target, port->port_id);
507#endif
508			targ = target_new(conf, port->cfiscsi_target);
509			if (targ == NULL) {
510				log_warnx("target_new failed");
511				continue;
512			}
513		}
514	}
515
516	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
517		struct cctl_lun_nv *nv;
518
519		if (lun->cfiscsi_target == NULL) {
520			log_debugx("CTL lun %ju wasn't managed by ctld; "
521			    "ignoring", (uintmax_t)lun->lun_id);
522			continue;
523		}
524
525		targ = target_find(conf, lun->cfiscsi_target);
526		if (targ == NULL) {
527#if 0
528			log_debugx("found new kernel target %s for CTL lun %ld",
529			    lun->cfiscsi_target, lun->lun_id);
530#endif
531			targ = target_new(conf, lun->cfiscsi_target);
532			if (targ == NULL) {
533				log_warnx("target_new failed");
534				continue;
535			}
536		}
537
538		cl = lun_find(targ, lun->cfiscsi_lun);
539		if (cl != NULL) {
540			log_warnx("found CTL lun %ju, backing lun %d, target "
541			    "%s, also backed by CTL lun %d; ignoring",
542			    (uintmax_t) lun->lun_id, cl->l_lun,
543			    cl->l_target->t_name, cl->l_ctl_lun);
544			continue;
545		}
546
547		log_debugx("found CTL lun %ju, backing lun %d, target %s",
548		    (uintmax_t)lun->lun_id, lun->cfiscsi_lun, lun->cfiscsi_target);
549
550		cl = lun_new(targ, lun->cfiscsi_lun);
551		if (cl == NULL) {
552			log_warnx("lun_new failed");
553			continue;
554		}
555		lun_set_backend(cl, lun->backend_type);
556		lun_set_blocksize(cl, lun->blocksize);
557		lun_set_device_id(cl, lun->device_id);
558		lun_set_serial(cl, lun->serial_number);
559		lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
560		lun_set_ctl_lun(cl, lun->lun_id);
561
562		STAILQ_FOREACH(nv, &lun->attr_list, links) {
563			if (strcmp(nv->name, "file") == 0 ||
564			    strcmp(nv->name, "dev") == 0) {
565				lun_set_path(cl, nv->value);
566				continue;
567			}
568			lo = lun_option_new(cl, nv->name, nv->value);
569			if (lo == NULL)
570				log_warnx("unable to add CTL lun option %s "
571				    "for CTL lun %ju for lun %d, target %s",
572				    nv->name, (uintmax_t) lun->lun_id,
573				    cl->l_lun, cl->l_target->t_name);
574		}
575	}
576
577	return (conf);
578}
579
580static void
581str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
582{
583
584	arg->namelen = strlen(name) + 1;
585	arg->name = __DECONST(char *, name);
586	arg->vallen = strlen(value) + 1;
587	arg->value = __DECONST(char *, value);
588	arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
589}
590
591int
592kernel_lun_add(struct lun *lun)
593{
594	struct lun_option *lo;
595	struct ctl_lun_req req;
596	char *tmp;
597	int error, i, num_options;
598
599	bzero(&req, sizeof(req));
600
601	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
602	req.reqtype = CTL_LUNREQ_CREATE;
603
604	req.reqdata.create.blocksize_bytes = lun->l_blocksize;
605
606	if (lun->l_size != 0)
607		req.reqdata.create.lun_size_bytes = lun->l_size;
608
609	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
610	req.reqdata.create.device_type = T_DIRECT;
611
612	if (lun->l_serial != NULL) {
613		strncpy(req.reqdata.create.serial_num, lun->l_serial,
614			sizeof(req.reqdata.create.serial_num));
615		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
616	}
617
618	if (lun->l_device_id != NULL) {
619		strncpy(req.reqdata.create.device_id, lun->l_device_id,
620			sizeof(req.reqdata.create.device_id));
621		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
622	}
623
624	if (lun->l_path != NULL) {
625		lo = lun_option_find(lun, "file");
626		if (lo != NULL) {
627			lun_option_set(lo, lun->l_path);
628		} else {
629			lo = lun_option_new(lun, "file", lun->l_path);
630			assert(lo != NULL);
631		}
632	}
633
634	lo = lun_option_find(lun, "cfiscsi_target");
635	if (lo != NULL) {
636		lun_option_set(lo, lun->l_target->t_name);
637	} else {
638		lo = lun_option_new(lun, "cfiscsi_target",
639		    lun->l_target->t_name);
640		assert(lo != NULL);
641	}
642
643	if (lun->l_target->t_alias != NULL) {
644		lo = lun_option_find(lun, "cfiscsi_target_alias");
645		if (lo != NULL) {
646			lun_option_set(lo, lun->l_target->t_alias);
647		} else {
648			lo = lun_option_new(lun, "cfiscsi_target_alias",
649			    lun->l_target->t_alias);
650			assert(lo != NULL);
651		}
652	}
653
654	asprintf(&tmp, "%d", lun->l_lun);
655	if (tmp == NULL)
656		log_errx(1, "asprintf");
657	lo = lun_option_find(lun, "cfiscsi_lun");
658	if (lo != NULL) {
659		lun_option_set(lo, tmp);
660		free(tmp);
661	} else {
662		lo = lun_option_new(lun, "cfiscsi_lun", tmp);
663		free(tmp);
664		assert(lo != NULL);
665	}
666
667	num_options = 0;
668	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
669		num_options++;
670
671	req.num_be_args = num_options;
672	if (num_options > 0) {
673		req.be_args = malloc(num_options * sizeof(*req.be_args));
674		if (req.be_args == NULL) {
675			log_warn("error allocating %zd bytes",
676			    num_options * sizeof(*req.be_args));
677			return (1);
678		}
679
680		i = 0;
681		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
682			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
683			i++;
684		}
685		assert(i == num_options);
686	}
687
688	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
689	free(req.be_args);
690	if (error != 0) {
691		log_warn("error issuing CTL_LUN_REQ ioctl");
692		return (1);
693	}
694
695	if (req.status == CTL_LUN_ERROR) {
696		log_warnx("error returned from LUN creation request: %s",
697		    req.error_str);
698		return (1);
699	}
700
701	if (req.status != CTL_LUN_OK) {
702		log_warnx("unknown LUN creation request status %d",
703		    req.status);
704		return (1);
705	}
706
707	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
708
709	return (0);
710}
711
712int
713kernel_lun_resize(struct lun *lun)
714{
715	struct ctl_lun_req req;
716
717	bzero(&req, sizeof(req));
718
719	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
720	req.reqtype = CTL_LUNREQ_MODIFY;
721
722	req.reqdata.modify.lun_id = lun->l_ctl_lun;
723	req.reqdata.modify.lun_size_bytes = lun->l_size;
724
725	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
726		log_warn("error issuing CTL_LUN_REQ ioctl");
727		return (1);
728	}
729
730	if (req.status == CTL_LUN_ERROR) {
731		log_warnx("error returned from LUN modification request: %s",
732		    req.error_str);
733		return (1);
734	}
735
736	if (req.status != CTL_LUN_OK) {
737		log_warnx("unknown LUN modification request status %d",
738		    req.status);
739		return (1);
740	}
741
742	return (0);
743}
744
745int
746kernel_lun_remove(struct lun *lun)
747{
748	struct ctl_lun_req req;
749
750	bzero(&req, sizeof(req));
751
752	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
753	req.reqtype = CTL_LUNREQ_RM;
754
755	req.reqdata.rm.lun_id = lun->l_ctl_lun;
756
757	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
758		log_warn("error issuing CTL_LUN_REQ ioctl");
759		return (1);
760	}
761
762	if (req.status == CTL_LUN_ERROR) {
763		log_warnx("error returned from LUN removal request: %s",
764		    req.error_str);
765		return (1);
766	}
767
768	if (req.status != CTL_LUN_OK) {
769		log_warnx("unknown LUN removal request status %d", req.status);
770		return (1);
771	}
772
773	return (0);
774}
775
776void
777kernel_handoff(struct connection *conn)
778{
779	struct ctl_iscsi req;
780
781	bzero(&req, sizeof(req));
782
783	req.type = CTL_ISCSI_HANDOFF;
784	strlcpy(req.data.handoff.initiator_name,
785	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
786	strlcpy(req.data.handoff.initiator_addr,
787	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
788	if (conn->conn_initiator_alias != NULL) {
789		strlcpy(req.data.handoff.initiator_alias,
790		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
791	}
792	strlcpy(req.data.handoff.target_name,
793	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
794#ifdef ICL_KERNEL_PROXY
795	if (proxy_mode)
796		req.data.handoff.connection_id = conn->conn_socket;
797	else
798		req.data.handoff.socket = conn->conn_socket;
799#else
800	req.data.handoff.socket = conn->conn_socket;
801#endif
802	req.data.handoff.portal_group_tag =
803	    conn->conn_portal->p_portal_group->pg_tag;
804	if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
805		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
806	if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
807		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
808	req.data.handoff.cmdsn = conn->conn_cmdsn;
809	req.data.handoff.statsn = conn->conn_statsn;
810	req.data.handoff.max_recv_data_segment_length =
811	    conn->conn_max_data_segment_length;
812	req.data.handoff.max_burst_length = conn->conn_max_burst_length;
813	req.data.handoff.immediate_data = conn->conn_immediate_data;
814
815	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
816		log_err(1, "error issuing CTL_ISCSI ioctl; "
817		    "dropping connection");
818	}
819
820	if (req.status != CTL_ISCSI_OK) {
821		log_errx(1, "error returned from CTL iSCSI handoff request: "
822		    "%s; dropping connection", req.error_str);
823	}
824}
825
826int
827kernel_port_add(struct target *targ)
828{
829	struct ctl_port_entry entry;
830	struct ctl_req req;
831	char tagstr[16];
832	int error;
833	uint32_t port_id = -1;
834
835	bzero(&req, sizeof(req));
836	strlcpy(req.driver, "iscsi", sizeof(req.driver));
837	req.reqtype = CTL_REQ_CREATE;
838	req.num_args = 4;
839	req.args = malloc(req.num_args * sizeof(*req.args));
840	req.args[0].namelen = sizeof("port_id");
841	req.args[0].name = __DECONST(char *, "port_id");
842	req.args[0].vallen = sizeof(port_id);
843	req.args[0].value = &port_id;
844	req.args[0].flags = CTL_BEARG_WR;
845	str_arg(&req.args[1], "cfiscsi_target", targ->t_name);
846	str_arg(&req.args[2], "cfiscsi_target_alias", targ->t_alias);
847	snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
848	str_arg(&req.args[3], "cfiscsi_portal_group_tag", tagstr);
849
850	error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
851	free(req.args);
852	if (error != 0) {
853		log_warn("error issuing CTL_PORT_REQ ioctl");
854		return (1);
855	}
856
857	if (req.status == CTL_LUN_ERROR) {
858		log_warnx("error returned from port creation request: %s",
859		    req.error_str);
860		return (1);
861	}
862
863	if (req.status != CTL_LUN_OK) {
864		log_warnx("unknown port creation request status %d",
865		    req.status);
866		return (1);
867	}
868
869	bzero(&entry, sizeof(entry));
870	entry.targ_port = port_id;
871
872	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
873	if (error != 0) {
874		log_warn("CTL_ENABLE_PORT ioctl failed");
875		return (-1);
876	}
877
878	return (0);
879}
880
881int
882kernel_port_remove(struct target *targ)
883{
884	struct ctl_req req;
885	char tagstr[16];
886	int error;
887
888	bzero(&req, sizeof(req));
889	strlcpy(req.driver, "iscsi", sizeof(req.driver));
890	req.reqtype = CTL_REQ_REMOVE;
891	req.num_args = 2;
892	req.args = malloc(req.num_args * sizeof(*req.args));
893	str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
894	if (targ->t_portal_group) {
895		snprintf(tagstr, sizeof(tagstr), "%d",
896		    targ->t_portal_group->pg_tag);
897		str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
898	} else
899		req.num_args--;
900
901	error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
902	free(req.args);
903	if (error != 0) {
904		log_warn("error issuing CTL_PORT_REQ ioctl");
905		return (1);
906	}
907
908	if (req.status == CTL_LUN_ERROR) {
909		log_warnx("error returned from port removal request: %s",
910		    req.error_str);
911		return (1);
912	}
913
914	if (req.status != CTL_LUN_OK) {
915		log_warnx("unknown port removal request status %d",
916		    req.status);
917		return (1);
918	}
919
920	return (0);
921}
922
923#ifdef ICL_KERNEL_PROXY
924void
925kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
926{
927	struct ctl_iscsi req;
928
929	bzero(&req, sizeof(req));
930
931	req.type = CTL_ISCSI_LISTEN;
932	req.data.listen.iser = iser;
933	req.data.listen.domain = ai->ai_family;
934	req.data.listen.socktype = ai->ai_socktype;
935	req.data.listen.protocol = ai->ai_protocol;
936	req.data.listen.addr = ai->ai_addr;
937	req.data.listen.addrlen = ai->ai_addrlen;
938	req.data.listen.portal_id = portal_id;
939
940	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
941		log_err(1, "error issuing CTL_ISCSI ioctl");
942
943	if (req.status != CTL_ISCSI_OK) {
944		log_errx(1, "error returned from CTL iSCSI listen: %s",
945		    req.error_str);
946	}
947}
948
949void
950kernel_accept(int *connection_id, int *portal_id,
951    struct sockaddr *client_sa, socklen_t *client_salen)
952{
953	struct ctl_iscsi req;
954	struct sockaddr_storage ss;
955
956	bzero(&req, sizeof(req));
957
958	req.type = CTL_ISCSI_ACCEPT;
959	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
960
961	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
962		log_err(1, "error issuing CTL_ISCSI ioctl");
963
964	if (req.status != CTL_ISCSI_OK) {
965		log_errx(1, "error returned from CTL iSCSI accept: %s",
966		    req.error_str);
967	}
968
969	*connection_id = req.data.accept.connection_id;
970	*portal_id = req.data.accept.portal_id;
971	*client_salen = req.data.accept.initiator_addrlen;
972	memcpy(client_sa, &ss, *client_salen);
973}
974
975void
976kernel_send(struct pdu *pdu)
977{
978	struct ctl_iscsi req;
979
980	bzero(&req, sizeof(req));
981
982	req.type = CTL_ISCSI_SEND;
983	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
984	req.data.send.bhs = pdu->pdu_bhs;
985	req.data.send.data_segment_len = pdu->pdu_data_len;
986	req.data.send.data_segment = pdu->pdu_data;
987
988	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
989		log_err(1, "error issuing CTL_ISCSI ioctl; "
990		    "dropping connection");
991	}
992
993	if (req.status != CTL_ISCSI_OK) {
994		log_errx(1, "error returned from CTL iSCSI send: "
995		    "%s; dropping connection", req.error_str);
996	}
997}
998
999void
1000kernel_receive(struct pdu *pdu)
1001{
1002	struct ctl_iscsi req;
1003
1004	pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1005	if (pdu->pdu_data == NULL)
1006		log_err(1, "malloc");
1007
1008	bzero(&req, sizeof(req));
1009
1010	req.type = CTL_ISCSI_RECEIVE;
1011	req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1012	req.data.receive.bhs = pdu->pdu_bhs;
1013	req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1014	req.data.receive.data_segment = pdu->pdu_data;
1015
1016	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1017		log_err(1, "error issuing CTL_ISCSI ioctl; "
1018		    "dropping connection");
1019	}
1020
1021	if (req.status != CTL_ISCSI_OK) {
1022		log_errx(1, "error returned from CTL iSCSI receive: "
1023		    "%s; dropping connection", req.error_str);
1024	}
1025
1026}
1027
1028#endif /* ICL_KERNEL_PROXY */
1029
1030/*
1031 * XXX: I CANT INTO LATIN
1032 */
1033void
1034kernel_capsicate(void)
1035{
1036	int error;
1037	cap_rights_t rights;
1038	const unsigned long cmds[] = { CTL_ISCSI };
1039
1040	cap_rights_init(&rights, CAP_IOCTL);
1041	error = cap_rights_limit(ctl_fd, &rights);
1042	if (error != 0 && errno != ENOSYS)
1043		log_err(1, "cap_rights_limit");
1044
1045	error = cap_ioctls_limit(ctl_fd, cmds,
1046	    sizeof(cmds) / sizeof(cmds[0]));
1047	if (error != 0 && errno != ENOSYS)
1048		log_err(1, "cap_ioctls_limit");
1049
1050	error = cap_enter();
1051	if (error != 0 && errno != ENOSYS)
1052		log_err(1, "cap_enter");
1053
1054	if (cap_sandboxed())
1055		log_debugx("Capsicum capability mode enabled");
1056	else
1057		log_warnx("Capsicum capability mode not supported");
1058}
1059
1060