kernel.c revision 286806
1/*-
2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions, and the following disclaimer,
15 *    without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 *    substantially similar to the "NO WARRANTY" disclaimer below
18 *    ("Disclaimer") and any redistribution must be conditioned upon
19 *    including a substantially similar Disclaimer requirement for further
20 *    binary redistribution.
21 *
22 * NO WARRANTY
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
34 *
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/usr.sbin/ctld/kernel.c 286806 2015-08-15 13:34:38Z 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	if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
837		strlcpy(req.data.handoff.offload,
838		    conn->conn_portal->p_portal_group->pg_offload,
839		    sizeof(req.data.handoff.offload));
840	}
841#ifdef ICL_KERNEL_PROXY
842	if (proxy_mode)
843		req.data.handoff.connection_id = conn->conn_socket;
844	else
845		req.data.handoff.socket = conn->conn_socket;
846#else
847	req.data.handoff.socket = conn->conn_socket;
848#endif
849	req.data.handoff.portal_group_tag =
850	    conn->conn_portal->p_portal_group->pg_tag;
851	if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
852		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
853	if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
854		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
855	req.data.handoff.cmdsn = conn->conn_cmdsn;
856	req.data.handoff.statsn = conn->conn_statsn;
857	req.data.handoff.max_recv_data_segment_length =
858	    conn->conn_max_data_segment_length;
859	req.data.handoff.max_burst_length = conn->conn_max_burst_length;
860	req.data.handoff.immediate_data = conn->conn_immediate_data;
861
862	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
863		log_err(1, "error issuing CTL_ISCSI ioctl; "
864		    "dropping connection");
865	}
866
867	if (req.status != CTL_ISCSI_OK) {
868		log_errx(1, "error returned from CTL iSCSI handoff request: "
869		    "%s; dropping connection", req.error_str);
870	}
871}
872
873void
874kernel_limits(const char *offload, size_t *max_data_segment_length)
875{
876	struct ctl_iscsi req;
877
878	bzero(&req, sizeof(req));
879
880	req.type = CTL_ISCSI_LIMITS;
881	if (offload != NULL) {
882		strlcpy(req.data.limits.offload, offload,
883		    sizeof(req.data.limits.offload));
884	}
885
886	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
887		log_err(1, "error issuing CTL_ISCSI ioctl; "
888		    "dropping connection");
889	}
890
891	if (req.status != CTL_ISCSI_OK) {
892		log_errx(1, "error returned from CTL iSCSI limits request: "
893		    "%s; dropping connection", req.error_str);
894	}
895
896	*max_data_segment_length = req.data.limits.data_segment_limit;
897	if (offload != NULL) {
898		log_debugx("MaxRecvDataSegment kernel limit for offload "
899		    "\"%s\" is %zd", offload, *max_data_segment_length);
900	} else {
901		log_debugx("MaxRecvDataSegment kernel limit is %zd",
902		    *max_data_segment_length);
903	}
904}
905
906int
907kernel_port_add(struct port *port)
908{
909	struct ctl_port_entry entry;
910	struct ctl_req req;
911	struct ctl_lun_map lm;
912	struct target *targ = port->p_target;
913	struct portal_group *pg = port->p_portal_group;
914	char tagstr[16];
915	int error, i, n;
916
917	/* Create iSCSI port. */
918	if (port->p_portal_group) {
919		bzero(&req, sizeof(req));
920		strlcpy(req.driver, "iscsi", sizeof(req.driver));
921		req.reqtype = CTL_REQ_CREATE;
922		req.num_args = 5;
923		req.args = malloc(req.num_args * sizeof(*req.args));
924		if (req.args == NULL)
925			log_err(1, "malloc");
926		n = 0;
927		req.args[n].namelen = sizeof("port_id");
928		req.args[n].name = __DECONST(char *, "port_id");
929		req.args[n].vallen = sizeof(port->p_ctl_port);
930		req.args[n].value = &port->p_ctl_port;
931		req.args[n++].flags = CTL_BEARG_WR;
932		str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
933		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
934		str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
935		if (targ->t_alias)
936			str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
937		str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
938		req.num_args = n;
939		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
940		free(req.args);
941		if (error != 0) {
942			log_warn("error issuing CTL_PORT_REQ ioctl");
943			return (1);
944		}
945		if (req.status == CTL_LUN_ERROR) {
946			log_warnx("error returned from port creation request: %s",
947			    req.error_str);
948			return (1);
949		}
950		if (req.status != CTL_LUN_OK) {
951			log_warnx("unknown port creation request status %d",
952			    req.status);
953			return (1);
954		}
955	} else if (port->p_pport) {
956		port->p_ctl_port = port->p_pport->pp_ctl_port;
957
958		if (strncmp(targ->t_name, "naa.", 4) == 0 &&
959		    strlen(targ->t_name) == 20) {
960			bzero(&entry, sizeof(entry));
961			entry.port_type = CTL_PORT_NONE;
962			entry.targ_port = port->p_ctl_port;
963			entry.flags |= CTL_PORT_WWNN_VALID;
964			entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
965			if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
966				log_warn("CTL_SET_PORT_WWNS ioctl failed");
967		}
968	}
969
970	/* Explicitly enable mapping to block any access except allowed. */
971	lm.port = port->p_ctl_port;
972	lm.plun = UINT32_MAX;
973	lm.lun = 0;
974	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
975	if (error != 0)
976		log_warn("CTL_LUN_MAP ioctl failed");
977
978	/* Map configured LUNs */
979	for (i = 0; i < MAX_LUNS; i++) {
980		if (targ->t_luns[i] == NULL)
981			continue;
982		lm.port = port->p_ctl_port;
983		lm.plun = i;
984		lm.lun = targ->t_luns[i]->l_ctl_lun;
985		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
986		if (error != 0)
987			log_warn("CTL_LUN_MAP ioctl failed");
988	}
989
990	/* Enable port */
991	bzero(&entry, sizeof(entry));
992	entry.targ_port = port->p_ctl_port;
993	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
994	if (error != 0) {
995		log_warn("CTL_ENABLE_PORT ioctl failed");
996		return (-1);
997	}
998
999	return (0);
1000}
1001
1002int
1003kernel_port_update(struct port *port)
1004{
1005	struct ctl_lun_map lm;
1006	struct target *targ = port->p_target;
1007	int error, i;
1008
1009	/* Map configured LUNs and unmap others */
1010	for (i = 0; i < MAX_LUNS; i++) {
1011		lm.port = port->p_ctl_port;
1012		lm.plun = i;
1013		if (targ->t_luns[i] == NULL)
1014			lm.lun = UINT32_MAX;
1015		else
1016			lm.lun = targ->t_luns[i]->l_ctl_lun;
1017		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1018		if (error != 0)
1019			log_warn("CTL_LUN_MAP ioctl failed");
1020	}
1021	return (0);
1022}
1023
1024int
1025kernel_port_remove(struct port *port)
1026{
1027	struct ctl_port_entry entry;
1028	struct ctl_lun_map lm;
1029	struct ctl_req req;
1030	char tagstr[16];
1031	struct target *targ = port->p_target;
1032	struct portal_group *pg = port->p_portal_group;
1033	int error;
1034
1035	/* Disable port */
1036	bzero(&entry, sizeof(entry));
1037	entry.targ_port = port->p_ctl_port;
1038	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1039	if (error != 0) {
1040		log_warn("CTL_DISABLE_PORT ioctl failed");
1041		return (-1);
1042	}
1043
1044	/* Remove iSCSI port. */
1045	if (port->p_portal_group) {
1046		bzero(&req, sizeof(req));
1047		strlcpy(req.driver, "iscsi", sizeof(req.driver));
1048		req.reqtype = CTL_REQ_REMOVE;
1049		req.num_args = 2;
1050		req.args = malloc(req.num_args * sizeof(*req.args));
1051		if (req.args == NULL)
1052			log_err(1, "malloc");
1053		str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1054		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1055		str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1056		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1057		free(req.args);
1058		if (error != 0) {
1059			log_warn("error issuing CTL_PORT_REQ ioctl");
1060			return (1);
1061		}
1062		if (req.status == CTL_LUN_ERROR) {
1063			log_warnx("error returned from port removal request: %s",
1064			    req.error_str);
1065			return (1);
1066		}
1067		if (req.status != CTL_LUN_OK) {
1068			log_warnx("unknown port removal request status %d",
1069			    req.status);
1070			return (1);
1071		}
1072	} else {
1073		/* Disable LUN mapping. */
1074		lm.port = port->p_ctl_port;
1075		lm.plun = UINT32_MAX;
1076		lm.lun = UINT32_MAX;
1077		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1078		if (error != 0)
1079			log_warn("CTL_LUN_MAP ioctl failed");
1080	}
1081	return (0);
1082}
1083
1084#ifdef ICL_KERNEL_PROXY
1085void
1086kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1087{
1088	struct ctl_iscsi req;
1089
1090	bzero(&req, sizeof(req));
1091
1092	req.type = CTL_ISCSI_LISTEN;
1093	req.data.listen.iser = iser;
1094	req.data.listen.domain = ai->ai_family;
1095	req.data.listen.socktype = ai->ai_socktype;
1096	req.data.listen.protocol = ai->ai_protocol;
1097	req.data.listen.addr = ai->ai_addr;
1098	req.data.listen.addrlen = ai->ai_addrlen;
1099	req.data.listen.portal_id = portal_id;
1100
1101	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1102		log_err(1, "error issuing CTL_ISCSI ioctl");
1103
1104	if (req.status != CTL_ISCSI_OK) {
1105		log_errx(1, "error returned from CTL iSCSI listen: %s",
1106		    req.error_str);
1107	}
1108}
1109
1110void
1111kernel_accept(int *connection_id, int *portal_id,
1112    struct sockaddr *client_sa, socklen_t *client_salen)
1113{
1114	struct ctl_iscsi req;
1115	struct sockaddr_storage ss;
1116
1117	bzero(&req, sizeof(req));
1118
1119	req.type = CTL_ISCSI_ACCEPT;
1120	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1121
1122	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1123		log_err(1, "error issuing CTL_ISCSI ioctl");
1124
1125	if (req.status != CTL_ISCSI_OK) {
1126		log_errx(1, "error returned from CTL iSCSI accept: %s",
1127		    req.error_str);
1128	}
1129
1130	*connection_id = req.data.accept.connection_id;
1131	*portal_id = req.data.accept.portal_id;
1132	*client_salen = req.data.accept.initiator_addrlen;
1133	memcpy(client_sa, &ss, *client_salen);
1134}
1135
1136void
1137kernel_send(struct pdu *pdu)
1138{
1139	struct ctl_iscsi req;
1140
1141	bzero(&req, sizeof(req));
1142
1143	req.type = CTL_ISCSI_SEND;
1144	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1145	req.data.send.bhs = pdu->pdu_bhs;
1146	req.data.send.data_segment_len = pdu->pdu_data_len;
1147	req.data.send.data_segment = pdu->pdu_data;
1148
1149	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1150		log_err(1, "error issuing CTL_ISCSI ioctl; "
1151		    "dropping connection");
1152	}
1153
1154	if (req.status != CTL_ISCSI_OK) {
1155		log_errx(1, "error returned from CTL iSCSI send: "
1156		    "%s; dropping connection", req.error_str);
1157	}
1158}
1159
1160void
1161kernel_receive(struct pdu *pdu)
1162{
1163	struct ctl_iscsi req;
1164
1165	pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1166	if (pdu->pdu_data == NULL)
1167		log_err(1, "malloc");
1168
1169	bzero(&req, sizeof(req));
1170
1171	req.type = CTL_ISCSI_RECEIVE;
1172	req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1173	req.data.receive.bhs = pdu->pdu_bhs;
1174	req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1175	req.data.receive.data_segment = pdu->pdu_data;
1176
1177	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1178		log_err(1, "error issuing CTL_ISCSI ioctl; "
1179		    "dropping connection");
1180	}
1181
1182	if (req.status != CTL_ISCSI_OK) {
1183		log_errx(1, "error returned from CTL iSCSI receive: "
1184		    "%s; dropping connection", req.error_str);
1185	}
1186
1187}
1188
1189#endif /* ICL_KERNEL_PROXY */
1190
1191/*
1192 * XXX: I CANT INTO LATIN
1193 */
1194void
1195kernel_capsicate(void)
1196{
1197	int error;
1198	cap_rights_t rights;
1199	const unsigned long cmds[] = { CTL_ISCSI };
1200
1201	cap_rights_init(&rights, CAP_IOCTL);
1202	error = cap_rights_limit(ctl_fd, &rights);
1203	if (error != 0 && errno != ENOSYS)
1204		log_err(1, "cap_rights_limit");
1205
1206	error = cap_ioctls_limit(ctl_fd, cmds,
1207	    sizeof(cmds) / sizeof(cmds[0]));
1208	if (error != 0 && errno != ENOSYS)
1209		log_err(1, "cap_ioctls_limit");
1210
1211	error = cap_enter();
1212	if (error != 0 && errno != ENOSYS)
1213		log_err(1, "cap_enter");
1214
1215	if (cap_sandboxed())
1216		log_debugx("Capsicum capability mode enabled");
1217	else
1218		log_warnx("Capsicum capability mode not supported");
1219}
1220
1221