kernel.c revision 288805
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 288805 2015-10-05 11:25:48Z mav $");
39
40#include <sys/ioctl.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <sys/param.h>
44#include <sys/linker.h>
45#include <sys/queue.h>
46#include <sys/callout.h>
47#include <sys/sbuf.h>
48#include <sys/capsicum.h>
49#include <assert.h>
50#include <bsdxml.h>
51#include <ctype.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <stdint.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <strings.h>
59#include <cam/scsi/scsi_all.h>
60#include <cam/scsi/scsi_message.h>
61#include <cam/ctl/ctl.h>
62#include <cam/ctl/ctl_io.h>
63#include <cam/ctl/ctl_backend.h>
64#include <cam/ctl/ctl_ioctl.h>
65#include <cam/ctl/ctl_util.h>
66#include <cam/ctl/ctl_scsi_all.h>
67
68#include "ctld.h"
69
70#ifdef ICL_KERNEL_PROXY
71#include <netdb.h>
72#endif
73
74extern bool proxy_mode;
75
76static int	ctl_fd = 0;
77
78void
79kernel_init(void)
80{
81	int retval, saved_errno;
82
83	ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84	if (ctl_fd < 0 && errno == ENOENT) {
85		saved_errno = errno;
86		retval = kldload("ctl");
87		if (retval != -1)
88			ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
89		else
90			errno = saved_errno;
91	}
92	if (ctl_fd < 0)
93		log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
94}
95
96/*
97 * Name/value pair used for per-LUN attributes.
98 */
99struct cctl_lun_nv {
100	char *name;
101	char *value;
102	STAILQ_ENTRY(cctl_lun_nv) links;
103};
104
105/*
106 * Backend LUN information.
107 */
108struct cctl_lun {
109	uint64_t lun_id;
110	char *backend_type;
111	uint64_t size_blocks;
112	uint32_t blocksize;
113	char *serial_number;
114	char *device_id;
115	char *ctld_name;
116	STAILQ_HEAD(,cctl_lun_nv) attr_list;
117	STAILQ_ENTRY(cctl_lun) links;
118};
119
120struct cctl_port {
121	uint32_t port_id;
122	char *port_frontend;
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, "frontend_type") == 0) {
336		cur_port->port_frontend = str;
337		str = NULL;
338	} else if (strcmp(name, "port_name") == 0) {
339		cur_port->port_name = str;
340		str = NULL;
341	} else if (strcmp(name, "physical_port") == 0) {
342		cur_port->pp = strtoul(str, NULL, 0);
343	} else if (strcmp(name, "virtual_port") == 0) {
344		cur_port->vp = strtoul(str, NULL, 0);
345	} else if (strcmp(name, "cfiscsi_target") == 0) {
346		cur_port->cfiscsi_target = str;
347		str = NULL;
348	} else if (strcmp(name, "cfiscsi_state") == 0) {
349		cur_port->cfiscsi_state = strtoul(str, NULL, 0);
350	} else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
351		cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
352	} else if (strcmp(name, "ctld_portal_group_name") == 0) {
353		cur_port->ctld_portal_group_name = str;
354		str = NULL;
355	} else if (strcmp(name, "targ_port") == 0) {
356		devlist->cur_port = NULL;
357	} else if (strcmp(name, "ctlportlist") == 0) {
358		/* Nothing. */
359	} else {
360		struct cctl_lun_nv *nv;
361
362		nv = calloc(1, sizeof(*nv));
363		if (nv == NULL)
364			log_err(1, "%s: can't allocate %zd bytes for nv pair",
365			    __func__, sizeof(*nv));
366
367		nv->name = checked_strdup(name);
368
369		nv->value = str;
370		str = NULL;
371		STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
372	}
373
374	free(str);
375}
376
377static void
378cctl_char_handler(void *user_data, const XML_Char *str, int len)
379{
380	struct cctl_devlist_data *devlist;
381
382	devlist = (struct cctl_devlist_data *)user_data;
383
384	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
385}
386
387struct conf *
388conf_new_from_kernel(void)
389{
390	struct conf *conf = NULL;
391	struct target *targ;
392	struct portal_group *pg;
393	struct pport *pp;
394	struct port *cp;
395	struct lun *cl;
396	struct lun_option *lo;
397	struct ctl_lun_list list;
398	struct cctl_devlist_data devlist;
399	struct cctl_lun *lun;
400	struct cctl_port *port;
401	XML_Parser parser;
402	char *str, *name;
403	int len, retval;
404
405	bzero(&devlist, sizeof(devlist));
406	STAILQ_INIT(&devlist.lun_list);
407	STAILQ_INIT(&devlist.port_list);
408
409	log_debugx("obtaining previously configured CTL luns from the kernel");
410
411	str = NULL;
412	len = 4096;
413retry:
414	str = realloc(str, len);
415	if (str == NULL)
416		log_err(1, "realloc");
417
418	bzero(&list, sizeof(list));
419	list.alloc_len = len;
420	list.status = CTL_LUN_LIST_NONE;
421	list.lun_xml = str;
422
423	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
424		log_warn("error issuing CTL_LUN_LIST ioctl");
425		free(str);
426		return (NULL);
427	}
428
429	if (list.status == CTL_LUN_LIST_ERROR) {
430		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
431		    list.error_str);
432		free(str);
433		return (NULL);
434	}
435
436	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
437		len = len << 1;
438		goto retry;
439	}
440
441	parser = XML_ParserCreate(NULL);
442	if (parser == NULL) {
443		log_warnx("unable to create XML parser");
444		free(str);
445		return (NULL);
446	}
447
448	XML_SetUserData(parser, &devlist);
449	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
450	XML_SetCharacterDataHandler(parser, cctl_char_handler);
451
452	retval = XML_Parse(parser, str, strlen(str), 1);
453	XML_ParserFree(parser);
454	free(str);
455	if (retval != 1) {
456		log_warnx("XML_Parse failed");
457		return (NULL);
458	}
459
460	str = NULL;
461	len = 4096;
462retry_port:
463	str = realloc(str, len);
464	if (str == NULL)
465		log_err(1, "realloc");
466
467	bzero(&list, sizeof(list));
468	list.alloc_len = len;
469	list.status = CTL_LUN_LIST_NONE;
470	list.lun_xml = str;
471
472	if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
473		log_warn("error issuing CTL_PORT_LIST ioctl");
474		free(str);
475		return (NULL);
476	}
477
478	if (list.status == CTL_LUN_LIST_ERROR) {
479		log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
480		    list.error_str);
481		free(str);
482		return (NULL);
483	}
484
485	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
486		len = len << 1;
487		goto retry_port;
488	}
489
490	parser = XML_ParserCreate(NULL);
491	if (parser == NULL) {
492		log_warnx("unable to create XML parser");
493		free(str);
494		return (NULL);
495	}
496
497	XML_SetUserData(parser, &devlist);
498	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
499	XML_SetCharacterDataHandler(parser, cctl_char_handler);
500
501	retval = XML_Parse(parser, str, strlen(str), 1);
502	XML_ParserFree(parser);
503	free(str);
504	if (retval != 1) {
505		log_warnx("XML_Parse failed");
506		return (NULL);
507	}
508
509	conf = conf_new();
510
511	name = NULL;
512	STAILQ_FOREACH(port, &devlist.port_list, links) {
513		if (strcmp(port->port_frontend, "ha") == 0)
514			continue;
515		if (name)
516			free(name);
517		if (port->pp == 0 && port->vp == 0)
518			name = checked_strdup(port->port_name);
519		else if (port->vp == 0)
520			asprintf(&name, "%s/%d", port->port_name, port->pp);
521		else
522			asprintf(&name, "%s/%d/%d", port->port_name, port->pp,
523			    port->vp);
524
525		if (port->cfiscsi_target == NULL) {
526			log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
527			    port->port_id, name);
528			pp = pport_find(conf, name);
529			if (pp == NULL) {
530#if 0
531				log_debugx("found new kernel port %u \"%s\"",
532				    port->port_id, name);
533#endif
534				pp = pport_new(conf, name, port->port_id);
535				if (pp == NULL) {
536					log_warnx("pport_new failed");
537					continue;
538				}
539			}
540			continue;
541		}
542		if (port->cfiscsi_state != 1) {
543			log_debugx("CTL port %ju is not active (%d); ignoring",
544			    (uintmax_t)port->port_id, port->cfiscsi_state);
545			continue;
546		}
547
548		targ = target_find(conf, port->cfiscsi_target);
549		if (targ == NULL) {
550#if 0
551			log_debugx("found new kernel target %s for CTL port %ld",
552			    port->cfiscsi_target, port->port_id);
553#endif
554			targ = target_new(conf, port->cfiscsi_target);
555			if (targ == NULL) {
556				log_warnx("target_new failed");
557				continue;
558			}
559		}
560
561		if (port->ctld_portal_group_name == NULL)
562			continue;
563		pg = portal_group_find(conf, port->ctld_portal_group_name);
564		if (pg == NULL) {
565#if 0
566			log_debugx("found new kernel portal group %s for CTL port %ld",
567			    port->ctld_portal_group_name, port->port_id);
568#endif
569			pg = portal_group_new(conf, port->ctld_portal_group_name);
570			if (pg == NULL) {
571				log_warnx("portal_group_new failed");
572				continue;
573			}
574		}
575		pg->pg_tag = port->cfiscsi_portal_group_tag;
576		cp = port_new(conf, targ, pg);
577		if (cp == NULL) {
578			log_warnx("port_new failed");
579			continue;
580		}
581		cp->p_ctl_port = port->port_id;
582	}
583	if (name)
584		free(name);
585
586	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
587		struct cctl_lun_nv *nv;
588
589		if (lun->ctld_name == NULL) {
590			log_debugx("CTL lun %ju wasn't managed by ctld; "
591			    "ignoring", (uintmax_t)lun->lun_id);
592			continue;
593		}
594
595		cl = lun_find(conf, lun->ctld_name);
596		if (cl != NULL) {
597			log_warnx("found CTL lun %ju \"%s\", "
598			    "also backed by CTL lun %d; ignoring",
599			    (uintmax_t)lun->lun_id, lun->ctld_name,
600			    cl->l_ctl_lun);
601			continue;
602		}
603
604		log_debugx("found CTL lun %ju \"%s\"",
605		    (uintmax_t)lun->lun_id, lun->ctld_name);
606
607		cl = lun_new(conf, lun->ctld_name);
608		if (cl == NULL) {
609			log_warnx("lun_new failed");
610			continue;
611		}
612		lun_set_backend(cl, lun->backend_type);
613		lun_set_blocksize(cl, lun->blocksize);
614		lun_set_device_id(cl, lun->device_id);
615		lun_set_serial(cl, lun->serial_number);
616		lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
617		lun_set_ctl_lun(cl, lun->lun_id);
618
619		STAILQ_FOREACH(nv, &lun->attr_list, links) {
620			if (strcmp(nv->name, "file") == 0 ||
621			    strcmp(nv->name, "dev") == 0) {
622				lun_set_path(cl, nv->value);
623				continue;
624			}
625			lo = lun_option_new(cl, nv->name, nv->value);
626			if (lo == NULL)
627				log_warnx("unable to add CTL lun option %s "
628				    "for CTL lun %ju \"%s\"",
629				    nv->name, (uintmax_t) lun->lun_id,
630				    cl->l_name);
631		}
632	}
633
634	return (conf);
635}
636
637static void
638str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
639{
640
641	arg->namelen = strlen(name) + 1;
642	arg->name = __DECONST(char *, name);
643	arg->vallen = strlen(value) + 1;
644	arg->value = __DECONST(char *, value);
645	arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
646}
647
648int
649kernel_lun_add(struct lun *lun)
650{
651	struct lun_option *lo;
652	struct ctl_lun_req req;
653	int error, i, num_options;
654
655	bzero(&req, sizeof(req));
656
657	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
658	req.reqtype = CTL_LUNREQ_CREATE;
659
660	req.reqdata.create.blocksize_bytes = lun->l_blocksize;
661
662	if (lun->l_size != 0)
663		req.reqdata.create.lun_size_bytes = lun->l_size;
664
665	if (lun->l_ctl_lun >= 0) {
666		req.reqdata.create.req_lun_id = lun->l_ctl_lun;
667		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
668	}
669
670	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
671	req.reqdata.create.device_type = T_DIRECT;
672
673	if (lun->l_serial != NULL) {
674		strncpy(req.reqdata.create.serial_num, lun->l_serial,
675			sizeof(req.reqdata.create.serial_num));
676		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
677	}
678
679	if (lun->l_device_id != NULL) {
680		strncpy(req.reqdata.create.device_id, lun->l_device_id,
681			sizeof(req.reqdata.create.device_id));
682		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
683	}
684
685	if (lun->l_path != NULL) {
686		lo = lun_option_find(lun, "file");
687		if (lo != NULL) {
688			lun_option_set(lo, lun->l_path);
689		} else {
690			lo = lun_option_new(lun, "file", lun->l_path);
691			assert(lo != NULL);
692		}
693	}
694
695	lo = lun_option_find(lun, "ctld_name");
696	if (lo != NULL) {
697		lun_option_set(lo, lun->l_name);
698	} else {
699		lo = lun_option_new(lun, "ctld_name", lun->l_name);
700		assert(lo != NULL);
701	}
702
703	lo = lun_option_find(lun, "scsiname");
704	if (lo == NULL && lun->l_scsiname != NULL) {
705		lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
706		assert(lo != NULL);
707	}
708
709	num_options = 0;
710	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
711		num_options++;
712
713	req.num_be_args = num_options;
714	if (num_options > 0) {
715		req.be_args = malloc(num_options * sizeof(*req.be_args));
716		if (req.be_args == NULL) {
717			log_warn("error allocating %zd bytes",
718			    num_options * sizeof(*req.be_args));
719			return (1);
720		}
721
722		i = 0;
723		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
724			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
725			i++;
726		}
727		assert(i == num_options);
728	}
729
730	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
731	free(req.be_args);
732	if (error != 0) {
733		log_warn("error issuing CTL_LUN_REQ ioctl");
734		return (1);
735	}
736
737	switch (req.status) {
738	case CTL_LUN_ERROR:
739		log_warnx("LUN creation error: %s", req.error_str);
740		return (1);
741	case CTL_LUN_WARNING:
742		log_warnx("LUN creation warning: %s", req.error_str);
743		break;
744	case CTL_LUN_OK:
745		break;
746	default:
747		log_warnx("unknown LUN creation status: %d",
748		    req.status);
749		return (1);
750	}
751
752	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
753	return (0);
754}
755
756int
757kernel_lun_modify(struct lun *lun)
758{
759	struct lun_option *lo;
760	struct ctl_lun_req req;
761	int error, i, num_options;
762
763	bzero(&req, sizeof(req));
764
765	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
766	req.reqtype = CTL_LUNREQ_MODIFY;
767
768	req.reqdata.modify.lun_id = lun->l_ctl_lun;
769	req.reqdata.modify.lun_size_bytes = lun->l_size;
770
771	num_options = 0;
772	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
773		num_options++;
774
775	req.num_be_args = num_options;
776	if (num_options > 0) {
777		req.be_args = malloc(num_options * sizeof(*req.be_args));
778		if (req.be_args == NULL) {
779			log_warn("error allocating %zd bytes",
780			    num_options * sizeof(*req.be_args));
781			return (1);
782		}
783
784		i = 0;
785		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
786			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
787			i++;
788		}
789		assert(i == num_options);
790	}
791
792	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
793	free(req.be_args);
794	if (error != 0) {
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 modification error: %s", req.error_str);
802		return (1);
803	case CTL_LUN_WARNING:
804		log_warnx("LUN modification warning: %s", req.error_str);
805		break;
806	case CTL_LUN_OK:
807		break;
808	default:
809		log_warnx("unknown LUN modification status: %d",
810		    req.status);
811		return (1);
812	}
813
814	return (0);
815}
816
817int
818kernel_lun_remove(struct lun *lun)
819{
820	struct ctl_lun_req req;
821
822	bzero(&req, sizeof(req));
823
824	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
825	req.reqtype = CTL_LUNREQ_RM;
826
827	req.reqdata.rm.lun_id = lun->l_ctl_lun;
828
829	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
830		log_warn("error issuing CTL_LUN_REQ ioctl");
831		return (1);
832	}
833
834	switch (req.status) {
835	case CTL_LUN_ERROR:
836		log_warnx("LUN removal error: %s", req.error_str);
837		return (1);
838	case CTL_LUN_WARNING:
839		log_warnx("LUN removal warning: %s", req.error_str);
840		break;
841	case CTL_LUN_OK:
842		break;
843	default:
844		log_warnx("unknown LUN removal status: %d", req.status);
845		return (1);
846	}
847
848	return (0);
849}
850
851void
852kernel_handoff(struct connection *conn)
853{
854	struct ctl_iscsi req;
855
856	bzero(&req, sizeof(req));
857
858	req.type = CTL_ISCSI_HANDOFF;
859	strlcpy(req.data.handoff.initiator_name,
860	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
861	strlcpy(req.data.handoff.initiator_addr,
862	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
863	if (conn->conn_initiator_alias != NULL) {
864		strlcpy(req.data.handoff.initiator_alias,
865		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
866	}
867	memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
868	    sizeof(req.data.handoff.initiator_isid));
869	strlcpy(req.data.handoff.target_name,
870	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
871#ifdef ICL_KERNEL_PROXY
872	if (proxy_mode)
873		req.data.handoff.connection_id = conn->conn_socket;
874	else
875		req.data.handoff.socket = conn->conn_socket;
876#else
877	req.data.handoff.socket = conn->conn_socket;
878#endif
879	req.data.handoff.portal_group_tag =
880	    conn->conn_portal->p_portal_group->pg_tag;
881	if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
882		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
883	if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
884		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
885	req.data.handoff.cmdsn = conn->conn_cmdsn;
886	req.data.handoff.statsn = conn->conn_statsn;
887	req.data.handoff.max_recv_data_segment_length =
888	    conn->conn_max_data_segment_length;
889	req.data.handoff.max_burst_length = conn->conn_max_burst_length;
890	req.data.handoff.immediate_data = conn->conn_immediate_data;
891
892	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
893		log_err(1, "error issuing CTL_ISCSI ioctl; "
894		    "dropping connection");
895	}
896
897	if (req.status != CTL_ISCSI_OK) {
898		log_errx(1, "error returned from CTL iSCSI handoff request: "
899		    "%s; dropping connection", req.error_str);
900	}
901}
902
903int
904kernel_port_add(struct port *port)
905{
906	struct ctl_port_entry entry;
907	struct ctl_req req;
908	struct ctl_lun_map lm;
909	struct target *targ = port->p_target;
910	struct portal_group *pg = port->p_portal_group;
911	char tagstr[16];
912	int error, i, n;
913
914	/* Create iSCSI port. */
915	if (port->p_portal_group) {
916		bzero(&req, sizeof(req));
917		strlcpy(req.driver, "iscsi", sizeof(req.driver));
918		req.reqtype = CTL_REQ_CREATE;
919		req.num_args = 5;
920		req.args = malloc(req.num_args * sizeof(*req.args));
921		if (req.args == NULL)
922			log_err(1, "malloc");
923		n = 0;
924		req.args[n].namelen = sizeof("port_id");
925		req.args[n].name = __DECONST(char *, "port_id");
926		req.args[n].vallen = sizeof(port->p_ctl_port);
927		req.args[n].value = &port->p_ctl_port;
928		req.args[n++].flags = CTL_BEARG_WR;
929		str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
930		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
931		str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
932		if (targ->t_alias)
933			str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
934		str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
935		req.num_args = n;
936		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
937		free(req.args);
938		if (error != 0) {
939			log_warn("error issuing CTL_PORT_REQ ioctl");
940			return (1);
941		}
942		if (req.status == CTL_LUN_ERROR) {
943			log_warnx("error returned from port creation request: %s",
944			    req.error_str);
945			return (1);
946		}
947		if (req.status != CTL_LUN_OK) {
948			log_warnx("unknown port creation request status %d",
949			    req.status);
950			return (1);
951		}
952	} else if (port->p_pport) {
953		port->p_ctl_port = port->p_pport->pp_ctl_port;
954
955		if (strncmp(targ->t_name, "naa.", 4) == 0 &&
956		    strlen(targ->t_name) == 20) {
957			bzero(&entry, sizeof(entry));
958			entry.port_type = CTL_PORT_NONE;
959			entry.targ_port = port->p_ctl_port;
960			entry.flags |= CTL_PORT_WWNN_VALID;
961			entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
962			if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
963				log_warn("CTL_SET_PORT_WWNS ioctl failed");
964		}
965	}
966
967	/* Explicitly enable mapping to block any access except allowed. */
968	lm.port = port->p_ctl_port;
969	lm.plun = UINT32_MAX;
970	lm.lun = 0;
971	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
972	if (error != 0)
973		log_warn("CTL_LUN_MAP ioctl failed");
974
975	/* Map configured LUNs */
976	for (i = 0; i < MAX_LUNS; i++) {
977		if (targ->t_luns[i] == NULL)
978			continue;
979		lm.port = port->p_ctl_port;
980		lm.plun = i;
981		lm.lun = targ->t_luns[i]->l_ctl_lun;
982		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
983		if (error != 0)
984			log_warn("CTL_LUN_MAP ioctl failed");
985	}
986
987	/* Enable port */
988	bzero(&entry, sizeof(entry));
989	entry.targ_port = port->p_ctl_port;
990	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
991	if (error != 0) {
992		log_warn("CTL_ENABLE_PORT ioctl failed");
993		return (-1);
994	}
995
996	return (0);
997}
998
999int
1000kernel_port_update(struct port *port, struct port *oport)
1001{
1002	struct ctl_lun_map lm;
1003	struct target *targ = port->p_target;
1004	struct target *otarg = oport->p_target;
1005	int error, i;
1006	uint32_t olun;
1007
1008	/* Map configured LUNs and unmap others */
1009	for (i = 0; i < MAX_LUNS; i++) {
1010		lm.port = port->p_ctl_port;
1011		lm.plun = i;
1012		if (targ->t_luns[i] == NULL)
1013			lm.lun = UINT32_MAX;
1014		else
1015			lm.lun = targ->t_luns[i]->l_ctl_lun;
1016		if (otarg->t_luns[i] == NULL)
1017			olun = UINT32_MAX;
1018		else
1019			olun = otarg->t_luns[i]->l_ctl_lun;
1020		if (lm.lun == olun)
1021			continue;
1022		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1023		if (error != 0)
1024			log_warn("CTL_LUN_MAP ioctl failed");
1025	}
1026	return (0);
1027}
1028
1029int
1030kernel_port_remove(struct port *port)
1031{
1032	struct ctl_port_entry entry;
1033	struct ctl_lun_map lm;
1034	struct ctl_req req;
1035	char tagstr[16];
1036	struct target *targ = port->p_target;
1037	struct portal_group *pg = port->p_portal_group;
1038	int error;
1039
1040	/* Disable port */
1041	bzero(&entry, sizeof(entry));
1042	entry.targ_port = port->p_ctl_port;
1043	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1044	if (error != 0) {
1045		log_warn("CTL_DISABLE_PORT ioctl failed");
1046		return (-1);
1047	}
1048
1049	/* Remove iSCSI port. */
1050	if (port->p_portal_group) {
1051		bzero(&req, sizeof(req));
1052		strlcpy(req.driver, "iscsi", sizeof(req.driver));
1053		req.reqtype = CTL_REQ_REMOVE;
1054		req.num_args = 2;
1055		req.args = malloc(req.num_args * sizeof(*req.args));
1056		if (req.args == NULL)
1057			log_err(1, "malloc");
1058		str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1059		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1060		str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1061		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1062		free(req.args);
1063		if (error != 0) {
1064			log_warn("error issuing CTL_PORT_REQ ioctl");
1065			return (1);
1066		}
1067		if (req.status == CTL_LUN_ERROR) {
1068			log_warnx("error returned from port removal request: %s",
1069			    req.error_str);
1070			return (1);
1071		}
1072		if (req.status != CTL_LUN_OK) {
1073			log_warnx("unknown port removal request status %d",
1074			    req.status);
1075			return (1);
1076		}
1077	} else {
1078		/* Disable LUN mapping. */
1079		lm.port = port->p_ctl_port;
1080		lm.plun = UINT32_MAX;
1081		lm.lun = UINT32_MAX;
1082		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1083		if (error != 0)
1084			log_warn("CTL_LUN_MAP ioctl failed");
1085	}
1086	return (0);
1087}
1088
1089#ifdef ICL_KERNEL_PROXY
1090void
1091kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1092{
1093	struct ctl_iscsi req;
1094
1095	bzero(&req, sizeof(req));
1096
1097	req.type = CTL_ISCSI_LISTEN;
1098	req.data.listen.iser = iser;
1099	req.data.listen.domain = ai->ai_family;
1100	req.data.listen.socktype = ai->ai_socktype;
1101	req.data.listen.protocol = ai->ai_protocol;
1102	req.data.listen.addr = ai->ai_addr;
1103	req.data.listen.addrlen = ai->ai_addrlen;
1104	req.data.listen.portal_id = portal_id;
1105
1106	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1107		log_err(1, "error issuing CTL_ISCSI ioctl");
1108
1109	if (req.status != CTL_ISCSI_OK) {
1110		log_errx(1, "error returned from CTL iSCSI listen: %s",
1111		    req.error_str);
1112	}
1113}
1114
1115void
1116kernel_accept(int *connection_id, int *portal_id,
1117    struct sockaddr *client_sa, socklen_t *client_salen)
1118{
1119	struct ctl_iscsi req;
1120	struct sockaddr_storage ss;
1121
1122	bzero(&req, sizeof(req));
1123
1124	req.type = CTL_ISCSI_ACCEPT;
1125	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1126
1127	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1128		log_err(1, "error issuing CTL_ISCSI ioctl");
1129
1130	if (req.status != CTL_ISCSI_OK) {
1131		log_errx(1, "error returned from CTL iSCSI accept: %s",
1132		    req.error_str);
1133	}
1134
1135	*connection_id = req.data.accept.connection_id;
1136	*portal_id = req.data.accept.portal_id;
1137	*client_salen = req.data.accept.initiator_addrlen;
1138	memcpy(client_sa, &ss, *client_salen);
1139}
1140
1141void
1142kernel_send(struct pdu *pdu)
1143{
1144	struct ctl_iscsi req;
1145
1146	bzero(&req, sizeof(req));
1147
1148	req.type = CTL_ISCSI_SEND;
1149	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1150	req.data.send.bhs = pdu->pdu_bhs;
1151	req.data.send.data_segment_len = pdu->pdu_data_len;
1152	req.data.send.data_segment = pdu->pdu_data;
1153
1154	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1155		log_err(1, "error issuing CTL_ISCSI ioctl; "
1156		    "dropping connection");
1157	}
1158
1159	if (req.status != CTL_ISCSI_OK) {
1160		log_errx(1, "error returned from CTL iSCSI send: "
1161		    "%s; dropping connection", req.error_str);
1162	}
1163}
1164
1165void
1166kernel_receive(struct pdu *pdu)
1167{
1168	struct ctl_iscsi req;
1169
1170	pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1171	if (pdu->pdu_data == NULL)
1172		log_err(1, "malloc");
1173
1174	bzero(&req, sizeof(req));
1175
1176	req.type = CTL_ISCSI_RECEIVE;
1177	req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1178	req.data.receive.bhs = pdu->pdu_bhs;
1179	req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1180	req.data.receive.data_segment = pdu->pdu_data;
1181
1182	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1183		log_err(1, "error issuing CTL_ISCSI ioctl; "
1184		    "dropping connection");
1185	}
1186
1187	if (req.status != CTL_ISCSI_OK) {
1188		log_errx(1, "error returned from CTL iSCSI receive: "
1189		    "%s; dropping connection", req.error_str);
1190	}
1191
1192}
1193
1194#endif /* ICL_KERNEL_PROXY */
1195
1196/*
1197 * XXX: I CANT INTO LATIN
1198 */
1199void
1200kernel_capsicate(void)
1201{
1202	int error;
1203	cap_rights_t rights;
1204	const unsigned long cmds[] = { CTL_ISCSI };
1205
1206	cap_rights_init(&rights, CAP_IOCTL);
1207	error = cap_rights_limit(ctl_fd, &rights);
1208	if (error != 0 && errno != ENOSYS)
1209		log_err(1, "cap_rights_limit");
1210
1211	error = cap_ioctls_limit(ctl_fd, cmds,
1212	    sizeof(cmds) / sizeof(cmds[0]));
1213	if (error != 0 && errno != ENOSYS)
1214		log_err(1, "cap_ioctls_limit");
1215
1216	error = cap_enter();
1217	if (error != 0 && errno != ENOSYS)
1218		log_err(1, "cap_enter");
1219
1220	if (cap_sandboxed())
1221		log_debugx("Capsicum capability mode enabled");
1222	else
1223		log_warnx("Capsicum capability mode not supported");
1224}
1225
1226