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