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