xref: /freebsd/usr.sbin/ctld/kernel.c (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
5  * Copyright (c) 1997-2007 Kenneth D. Merry
6  * Copyright (c) 2012 The FreeBSD Foundation
7  * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Edward Tomasz Napierala
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions, and the following disclaimer,
18  *    without modification.
19  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20  *    substantially similar to the "NO WARRANTY" disclaimer below
21  *    ("Disclaimer") and any redistribution must be conditioned upon
22  *    including a substantially similar Disclaimer requirement for further
23  *    binary redistribution.
24  *
25  * NO WARRANTY
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGES.
37  *
38  */
39 
40 #include <sys/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/callout.h>
43 #include <sys/ioctl.h>
44 #include <sys/linker.h>
45 #include <sys/module.h>
46 #include <sys/queue.h>
47 #include <sys/sbuf.h>
48 #include <sys/nv.h>
49 #include <sys/stat.h>
50 #include <assert.h>
51 #include <bsdxml.h>
52 #include <capsicum_helpers.h>
53 #include <ctype.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <strings.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/ctl/ctl.h>
64 #include <cam/ctl/ctl_io.h>
65 #include <cam/ctl/ctl_backend.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_util.h>
68 #include <cam/ctl/ctl_scsi_all.h>
69 
70 #include "ctld.h"
71 
72 #ifdef ICL_KERNEL_PROXY
73 #include <netdb.h>
74 #endif
75 
76 #define	NVLIST_BUFSIZE	1024
77 
78 extern bool proxy_mode;
79 
80 static int	ctl_fd = 0;
81 
82 void
83 kernel_init(void)
84 {
85 	int retval, saved_errno;
86 
87 	ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
88 	if (ctl_fd < 0 && errno == ENOENT) {
89 		saved_errno = errno;
90 		retval = kldload("ctl");
91 		if (retval != -1)
92 			ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
93 		else
94 			errno = saved_errno;
95 	}
96 	if (ctl_fd < 0)
97 		log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
98 #ifdef	WANT_ISCSI
99 	else {
100 		saved_errno = errno;
101 		if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
102 			log_warn("couldn't load cfiscsi");
103 		errno = saved_errno;
104 	}
105 #endif
106 }
107 
108 /*
109  * Name/value pair used for per-LUN attributes.
110  */
111 struct cctl_lun_nv {
112 	char *name;
113 	char *value;
114 	STAILQ_ENTRY(cctl_lun_nv) links;
115 };
116 
117 /*
118  * Backend LUN information.
119  */
120 struct cctl_lun {
121 	uint64_t lun_id;
122 	char *backend_type;
123 	uint8_t device_type;
124 	uint64_t size_blocks;
125 	uint32_t blocksize;
126 	char *serial_number;
127 	char *device_id;
128 	char *ctld_name;
129 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
130 	STAILQ_ENTRY(cctl_lun) links;
131 };
132 
133 struct cctl_port {
134 	uint32_t port_id;
135 	char *port_frontend;
136 	char *port_name;
137 	int pp;
138 	int vp;
139 	int cfiscsi_state;
140 	char *cfiscsi_target;
141 	uint16_t cfiscsi_portal_group_tag;
142 	char *ctld_portal_group_name;
143 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
144 	STAILQ_ENTRY(cctl_port) links;
145 };
146 
147 struct cctl_devlist_data {
148 	int num_luns;
149 	STAILQ_HEAD(,cctl_lun) lun_list;
150 	struct cctl_lun *cur_lun;
151 	int num_ports;
152 	STAILQ_HEAD(,cctl_port) port_list;
153 	struct cctl_port *cur_port;
154 	int level;
155 	struct sbuf *cur_sb[32];
156 };
157 
158 static void
159 cctl_start_element(void *user_data, const char *name, const char **attr)
160 {
161 	int i;
162 	struct cctl_devlist_data *devlist;
163 	struct cctl_lun *cur_lun;
164 
165 	devlist = (struct cctl_devlist_data *)user_data;
166 	cur_lun = devlist->cur_lun;
167 	devlist->level++;
168 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
169 	    sizeof(devlist->cur_sb[0])))
170 		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
171 		     nitems(devlist->cur_sb));
172 
173 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
174 	if (devlist->cur_sb[devlist->level] == NULL)
175 		log_err(1, "%s: unable to allocate sbuf", __func__);
176 
177 	if (strcmp(name, "lun") == 0) {
178 		if (cur_lun != NULL)
179 			log_errx(1, "%s: improper lun element nesting",
180 			    __func__);
181 
182 		cur_lun = calloc(1, sizeof(*cur_lun));
183 		if (cur_lun == NULL)
184 			log_err(1, "%s: cannot allocate %zd bytes", __func__,
185 			    sizeof(*cur_lun));
186 
187 		devlist->num_luns++;
188 		devlist->cur_lun = cur_lun;
189 
190 		STAILQ_INIT(&cur_lun->attr_list);
191 		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
192 
193 		for (i = 0; attr[i] != NULL; i += 2) {
194 			if (strcmp(attr[i], "id") == 0) {
195 				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
196 			} else {
197 				log_errx(1, "%s: invalid LUN attribute %s = %s",
198 				     __func__, attr[i], attr[i+1]);
199 			}
200 		}
201 	}
202 }
203 
204 static void
205 cctl_end_element(void *user_data, const char *name)
206 {
207 	struct cctl_devlist_data *devlist;
208 	struct cctl_lun *cur_lun;
209 	char *str;
210 
211 	devlist = (struct cctl_devlist_data *)user_data;
212 	cur_lun = devlist->cur_lun;
213 
214 	if ((cur_lun == NULL)
215 	 && (strcmp(name, "ctllunlist") != 0))
216 		log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
217 
218 	if (devlist->cur_sb[devlist->level] == NULL)
219 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
220 		     devlist->level, name);
221 
222 	sbuf_finish(devlist->cur_sb[devlist->level]);
223 	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
224 
225 	if (strlen(str) == 0) {
226 		free(str);
227 		str = NULL;
228 	}
229 
230 	sbuf_delete(devlist->cur_sb[devlist->level]);
231 	devlist->cur_sb[devlist->level] = NULL;
232 	devlist->level--;
233 
234 	if (strcmp(name, "backend_type") == 0) {
235 		cur_lun->backend_type = str;
236 		str = NULL;
237 	} else if (strcmp(name, "lun_type") == 0) {
238 		if (str == NULL)
239 			log_errx(1, "%s: %s missing its argument", __func__, name);
240 		cur_lun->device_type = strtoull(str, NULL, 0);
241 	} else if (strcmp(name, "size") == 0) {
242 		if (str == NULL)
243 			log_errx(1, "%s: %s missing its argument", __func__, name);
244 		cur_lun->size_blocks = strtoull(str, NULL, 0);
245 	} else if (strcmp(name, "blocksize") == 0) {
246 		if (str == NULL)
247 			log_errx(1, "%s: %s missing its argument", __func__, name);
248 		cur_lun->blocksize = strtoul(str, NULL, 0);
249 	} else if (strcmp(name, "serial_number") == 0) {
250 		cur_lun->serial_number = str;
251 		str = NULL;
252 	} else if (strcmp(name, "device_id") == 0) {
253 		cur_lun->device_id = str;
254 		str = NULL;
255 	} else if (strcmp(name, "ctld_name") == 0) {
256 		cur_lun->ctld_name = str;
257 		str = NULL;
258 	} else if (strcmp(name, "lun") == 0) {
259 		devlist->cur_lun = NULL;
260 	} else if (strcmp(name, "ctllunlist") == 0) {
261 		/* Nothing. */
262 	} else {
263 		struct cctl_lun_nv *nv;
264 
265 		nv = calloc(1, sizeof(*nv));
266 		if (nv == NULL)
267 			log_err(1, "%s: can't allocate %zd bytes for nv pair",
268 			    __func__, sizeof(*nv));
269 
270 		nv->name = checked_strdup(name);
271 
272 		nv->value = str;
273 		str = NULL;
274 		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
275 	}
276 
277 	free(str);
278 }
279 
280 static void
281 cctl_start_pelement(void *user_data, const char *name, const char **attr)
282 {
283 	int i;
284 	struct cctl_devlist_data *devlist;
285 	struct cctl_port *cur_port;
286 
287 	devlist = (struct cctl_devlist_data *)user_data;
288 	cur_port = devlist->cur_port;
289 	devlist->level++;
290 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
291 	    sizeof(devlist->cur_sb[0])))
292 		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
293 		     nitems(devlist->cur_sb));
294 
295 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
296 	if (devlist->cur_sb[devlist->level] == NULL)
297 		log_err(1, "%s: unable to allocate sbuf", __func__);
298 
299 	if (strcmp(name, "targ_port") == 0) {
300 		if (cur_port != NULL)
301 			log_errx(1, "%s: improper port element nesting (%s)",
302 			    __func__, name);
303 
304 		cur_port = calloc(1, sizeof(*cur_port));
305 		if (cur_port == NULL)
306 			log_err(1, "%s: cannot allocate %zd bytes", __func__,
307 			    sizeof(*cur_port));
308 
309 		devlist->num_ports++;
310 		devlist->cur_port = cur_port;
311 
312 		STAILQ_INIT(&cur_port->attr_list);
313 		STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
314 
315 		for (i = 0; attr[i] != NULL; i += 2) {
316 			if (strcmp(attr[i], "id") == 0) {
317 				cur_port->port_id = strtoul(attr[i+1], NULL, 0);
318 			} else {
319 				log_errx(1, "%s: invalid LUN attribute %s = %s",
320 				     __func__, attr[i], attr[i+1]);
321 			}
322 		}
323 	}
324 }
325 
326 static void
327 cctl_end_pelement(void *user_data, const char *name)
328 {
329 	struct cctl_devlist_data *devlist;
330 	struct cctl_port *cur_port;
331 	char *str;
332 
333 	devlist = (struct cctl_devlist_data *)user_data;
334 	cur_port = devlist->cur_port;
335 
336 	if ((cur_port == NULL)
337 	 && (strcmp(name, "ctlportlist") != 0))
338 		log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
339 
340 	if (devlist->cur_sb[devlist->level] == NULL)
341 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
342 		     devlist->level, name);
343 
344 	sbuf_finish(devlist->cur_sb[devlist->level]);
345 	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
346 
347 	if (strlen(str) == 0) {
348 		free(str);
349 		str = NULL;
350 	}
351 
352 	sbuf_delete(devlist->cur_sb[devlist->level]);
353 	devlist->cur_sb[devlist->level] = NULL;
354 	devlist->level--;
355 
356 	if (strcmp(name, "frontend_type") == 0) {
357 		cur_port->port_frontend = str;
358 		str = NULL;
359 	} else if (strcmp(name, "port_name") == 0) {
360 		cur_port->port_name = str;
361 		str = NULL;
362 	} else if (strcmp(name, "physical_port") == 0) {
363 		if (str == NULL)
364 			log_errx(1, "%s: %s missing its argument", __func__, name);
365 		cur_port->pp = strtoul(str, NULL, 0);
366 	} else if (strcmp(name, "virtual_port") == 0) {
367 		if (str == NULL)
368 			log_errx(1, "%s: %s missing its argument", __func__, name);
369 		cur_port->vp = strtoul(str, NULL, 0);
370 	} else if (strcmp(name, "cfiscsi_target") == 0) {
371 		cur_port->cfiscsi_target = str;
372 		str = NULL;
373 	} else if (strcmp(name, "cfiscsi_state") == 0) {
374 		if (str == NULL)
375 			log_errx(1, "%s: %s missing its argument", __func__, name);
376 		cur_port->cfiscsi_state = strtoul(str, NULL, 0);
377 	} else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
378 		if (str == NULL)
379 			log_errx(1, "%s: %s missing its argument", __func__, name);
380 		cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
381 	} else if (strcmp(name, "ctld_portal_group_name") == 0) {
382 		cur_port->ctld_portal_group_name = str;
383 		str = NULL;
384 	} else if (strcmp(name, "targ_port") == 0) {
385 		devlist->cur_port = NULL;
386 	} else if (strcmp(name, "ctlportlist") == 0) {
387 		/* Nothing. */
388 	} else {
389 		struct cctl_lun_nv *nv;
390 
391 		nv = calloc(1, sizeof(*nv));
392 		if (nv == NULL)
393 			log_err(1, "%s: can't allocate %zd bytes for nv pair",
394 			    __func__, sizeof(*nv));
395 
396 		nv->name = checked_strdup(name);
397 
398 		nv->value = str;
399 		str = NULL;
400 		STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
401 	}
402 
403 	free(str);
404 }
405 
406 static void
407 cctl_char_handler(void *user_data, const XML_Char *str, int len)
408 {
409 	struct cctl_devlist_data *devlist;
410 
411 	devlist = (struct cctl_devlist_data *)user_data;
412 
413 	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
414 }
415 
416 struct conf *
417 conf_new_from_kernel(void)
418 {
419 	struct conf *conf = NULL;
420 	struct target *targ;
421 	struct portal_group *pg;
422 	struct pport *pp;
423 	struct port *cp;
424 	struct lun *cl;
425 	struct option *o;
426 	struct ctl_lun_list list;
427 	struct cctl_devlist_data devlist;
428 	struct cctl_lun *lun;
429 	struct cctl_port *port;
430 	XML_Parser parser;
431 	char *str, *name;
432 	int len, retval;
433 
434 	bzero(&devlist, sizeof(devlist));
435 	STAILQ_INIT(&devlist.lun_list);
436 	STAILQ_INIT(&devlist.port_list);
437 
438 	log_debugx("obtaining previously configured CTL luns from the kernel");
439 
440 	str = NULL;
441 	len = 4096;
442 retry:
443 	str = realloc(str, len);
444 	if (str == NULL)
445 		log_err(1, "realloc");
446 
447 	bzero(&list, sizeof(list));
448 	list.alloc_len = len;
449 	list.status = CTL_LUN_LIST_NONE;
450 	list.lun_xml = str;
451 
452 	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
453 		log_warn("error issuing CTL_LUN_LIST ioctl");
454 		free(str);
455 		return (NULL);
456 	}
457 
458 	if (list.status == CTL_LUN_LIST_ERROR) {
459 		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
460 		    list.error_str);
461 		free(str);
462 		return (NULL);
463 	}
464 
465 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
466 		len = len << 1;
467 		goto retry;
468 	}
469 
470 	parser = XML_ParserCreate(NULL);
471 	if (parser == NULL) {
472 		log_warnx("unable to create XML parser");
473 		free(str);
474 		return (NULL);
475 	}
476 
477 	XML_SetUserData(parser, &devlist);
478 	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
479 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
480 
481 	retval = XML_Parse(parser, str, strlen(str), 1);
482 	XML_ParserFree(parser);
483 	free(str);
484 	if (retval != 1) {
485 		log_warnx("XML_Parse failed");
486 		return (NULL);
487 	}
488 
489 	str = NULL;
490 	len = 4096;
491 retry_port:
492 	str = realloc(str, len);
493 	if (str == NULL)
494 		log_err(1, "realloc");
495 
496 	bzero(&list, sizeof(list));
497 	list.alloc_len = len;
498 	list.status = CTL_LUN_LIST_NONE;
499 	list.lun_xml = str;
500 
501 	if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
502 		log_warn("error issuing CTL_PORT_LIST ioctl");
503 		free(str);
504 		return (NULL);
505 	}
506 
507 	if (list.status == CTL_LUN_LIST_ERROR) {
508 		log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
509 		    list.error_str);
510 		free(str);
511 		return (NULL);
512 	}
513 
514 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
515 		len = len << 1;
516 		goto retry_port;
517 	}
518 
519 	parser = XML_ParserCreate(NULL);
520 	if (parser == NULL) {
521 		log_warnx("unable to create XML parser");
522 		free(str);
523 		return (NULL);
524 	}
525 
526 	XML_SetUserData(parser, &devlist);
527 	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
528 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
529 
530 	retval = XML_Parse(parser, str, strlen(str), 1);
531 	XML_ParserFree(parser);
532 	free(str);
533 	if (retval != 1) {
534 		log_warnx("XML_Parse failed");
535 		return (NULL);
536 	}
537 
538 	conf = conf_new();
539 
540 	name = NULL;
541 	STAILQ_FOREACH(port, &devlist.port_list, links) {
542 		if (strcmp(port->port_frontend, "ha") == 0)
543 			continue;
544 		free(name);
545 		if (port->pp == 0 && port->vp == 0) {
546 			name = checked_strdup(port->port_name);
547 		} else if (port->vp == 0) {
548 			retval = asprintf(&name, "%s/%d",
549 			    port->port_name, port->pp);
550 			if (retval <= 0)
551 				log_err(1, "asprintf");
552 		} else {
553 			retval = asprintf(&name, "%s/%d/%d",
554 			    port->port_name, port->pp, port->vp);
555 			if (retval <= 0)
556 				log_err(1, "asprintf");
557 		}
558 
559 		if (port->cfiscsi_target == NULL) {
560 			log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
561 			    port->port_id, name);
562 			pp = pport_find(conf, name);
563 			if (pp == NULL) {
564 #if 0
565 				log_debugx("found new kernel port %u \"%s\"",
566 				    port->port_id, name);
567 #endif
568 				pp = pport_new(conf, name, port->port_id);
569 				if (pp == NULL) {
570 					log_warnx("pport_new failed");
571 					continue;
572 				}
573 			}
574 			continue;
575 		}
576 		if (port->cfiscsi_state != 1) {
577 			log_debugx("CTL port %ju is not active (%d); ignoring",
578 			    (uintmax_t)port->port_id, port->cfiscsi_state);
579 			continue;
580 		}
581 
582 		targ = target_find(conf, port->cfiscsi_target);
583 		if (targ == NULL) {
584 #if 0
585 			log_debugx("found new kernel target %s for CTL port %ld",
586 			    port->cfiscsi_target, port->port_id);
587 #endif
588 			targ = target_new(conf, port->cfiscsi_target);
589 			if (targ == NULL) {
590 				log_warnx("target_new failed");
591 				continue;
592 			}
593 		}
594 
595 		if (port->ctld_portal_group_name == NULL)
596 			continue;
597 		pg = portal_group_find(conf, port->ctld_portal_group_name);
598 		if (pg == NULL) {
599 #if 0
600 			log_debugx("found new kernel portal group %s for CTL port %ld",
601 			    port->ctld_portal_group_name, port->port_id);
602 #endif
603 			pg = portal_group_new(conf, port->ctld_portal_group_name);
604 			if (pg == NULL) {
605 				log_warnx("portal_group_new failed");
606 				continue;
607 			}
608 		}
609 		pg->pg_tag = port->cfiscsi_portal_group_tag;
610 		cp = port_new(conf, targ, pg);
611 		if (cp == NULL) {
612 			log_warnx("port_new failed");
613 			continue;
614 		}
615 		cp->p_ctl_port = port->port_id;
616 	}
617 	free(name);
618 
619 	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
620 		struct cctl_lun_nv *nv;
621 
622 		if (lun->ctld_name == NULL) {
623 			log_debugx("CTL lun %ju wasn't managed by ctld; "
624 			    "ignoring", (uintmax_t)lun->lun_id);
625 			continue;
626 		}
627 
628 		cl = lun_find(conf, lun->ctld_name);
629 		if (cl != NULL) {
630 			log_warnx("found CTL lun %ju \"%s\", "
631 			    "also backed by CTL lun %d; ignoring",
632 			    (uintmax_t)lun->lun_id, lun->ctld_name,
633 			    cl->l_ctl_lun);
634 			continue;
635 		}
636 
637 		log_debugx("found CTL lun %ju \"%s\"",
638 		    (uintmax_t)lun->lun_id, lun->ctld_name);
639 
640 		cl = lun_new(conf, lun->ctld_name);
641 		if (cl == NULL) {
642 			log_warnx("lun_new failed");
643 			continue;
644 		}
645 		lun_set_backend(cl, lun->backend_type);
646 		lun_set_device_type(cl, lun->device_type);
647 		lun_set_blocksize(cl, lun->blocksize);
648 		lun_set_device_id(cl, lun->device_id);
649 		lun_set_serial(cl, lun->serial_number);
650 		lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
651 		lun_set_ctl_lun(cl, lun->lun_id);
652 
653 		STAILQ_FOREACH(nv, &lun->attr_list, links) {
654 			if (strcmp(nv->name, "file") == 0 ||
655 			    strcmp(nv->name, "dev") == 0) {
656 				lun_set_path(cl, nv->value);
657 				continue;
658 			}
659 			o = option_new(&cl->l_options, nv->name, nv->value);
660 			if (o == NULL)
661 				log_warnx("unable to add CTL lun option %s "
662 				    "for CTL lun %ju \"%s\"",
663 				    nv->name, (uintmax_t) lun->lun_id,
664 				    cl->l_name);
665 		}
666 	}
667 
668 	return (conf);
669 }
670 
671 int
672 kernel_lun_add(struct lun *lun)
673 {
674 	struct option *o;
675 	struct ctl_lun_req req;
676 	int error;
677 
678 	bzero(&req, sizeof(req));
679 
680 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
681 	req.reqtype = CTL_LUNREQ_CREATE;
682 
683 	req.reqdata.create.blocksize_bytes = lun->l_blocksize;
684 
685 	if (lun->l_size != 0)
686 		req.reqdata.create.lun_size_bytes = lun->l_size;
687 
688 	if (lun->l_ctl_lun >= 0) {
689 		req.reqdata.create.req_lun_id = lun->l_ctl_lun;
690 		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
691 	}
692 
693 	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
694 	req.reqdata.create.device_type = lun->l_device_type;
695 
696 	if (lun->l_serial != NULL) {
697 		strncpy(req.reqdata.create.serial_num, lun->l_serial,
698 			sizeof(req.reqdata.create.serial_num));
699 		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
700 	}
701 
702 	if (lun->l_device_id != NULL) {
703 		strncpy(req.reqdata.create.device_id, lun->l_device_id,
704 			sizeof(req.reqdata.create.device_id));
705 		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
706 	}
707 
708 	if (lun->l_path != NULL) {
709 		o = option_find(&lun->l_options, "file");
710 		if (o != NULL) {
711 			option_set(o, lun->l_path);
712 		} else {
713 			o = option_new(&lun->l_options, "file", lun->l_path);
714 			assert(o != NULL);
715 		}
716 	}
717 
718 	o = option_find(&lun->l_options, "ctld_name");
719 	if (o != NULL) {
720 		option_set(o, lun->l_name);
721 	} else {
722 		o = option_new(&lun->l_options, "ctld_name", lun->l_name);
723 		assert(o != NULL);
724 	}
725 
726 	o = option_find(&lun->l_options, "scsiname");
727 	if (o == NULL && lun->l_scsiname != NULL) {
728 		o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
729 		assert(o != NULL);
730 	}
731 
732 	if (!TAILQ_EMPTY(&lun->l_options)) {
733 		req.args_nvl = nvlist_create(0);
734 		if (req.args_nvl == NULL) {
735 			log_warn("error allocating nvlist");
736 			return (1);
737 		}
738 
739 		TAILQ_FOREACH(o, &lun->l_options, o_next)
740 			nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
741 
742 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
743 		if (req.args == NULL) {
744 			log_warn("error packing nvlist");
745 			return (1);
746 		}
747 	}
748 
749 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
750 	nvlist_destroy(req.args_nvl);
751 
752 	if (error != 0) {
753 		log_warn("error issuing CTL_LUN_REQ ioctl");
754 		return (1);
755 	}
756 
757 	switch (req.status) {
758 	case CTL_LUN_ERROR:
759 		log_warnx("LUN creation error: %s", req.error_str);
760 		return (1);
761 	case CTL_LUN_WARNING:
762 		log_warnx("LUN creation warning: %s", req.error_str);
763 		break;
764 	case CTL_LUN_OK:
765 		break;
766 	default:
767 		log_warnx("unknown LUN creation status: %d",
768 		    req.status);
769 		return (1);
770 	}
771 
772 	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
773 	return (0);
774 }
775 
776 int
777 kernel_lun_modify(struct lun *lun)
778 {
779 	struct option *o;
780 	struct ctl_lun_req req;
781 	int error;
782 
783 	bzero(&req, sizeof(req));
784 
785 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
786 	req.reqtype = CTL_LUNREQ_MODIFY;
787 
788 	req.reqdata.modify.lun_id = lun->l_ctl_lun;
789 	req.reqdata.modify.lun_size_bytes = lun->l_size;
790 
791 	if (lun->l_path != NULL) {
792 		o = option_find(&lun->l_options, "file");
793 		if (o != NULL) {
794 			option_set(o, lun->l_path);
795 		} else {
796 			o = option_new(&lun->l_options, "file", lun->l_path);
797 			assert(o != NULL);
798 		}
799 	}
800 
801 	o = option_find(&lun->l_options, "ctld_name");
802 	if (o != NULL) {
803 		option_set(o, lun->l_name);
804 	} else {
805 		o = option_new(&lun->l_options, "ctld_name", lun->l_name);
806 		assert(o != NULL);
807 	}
808 
809 	o = option_find(&lun->l_options, "scsiname");
810 	if (o == NULL && lun->l_scsiname != NULL) {
811 		o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
812 		assert(o != NULL);
813 	}
814 
815 	if (!TAILQ_EMPTY(&lun->l_options)) {
816 		req.args_nvl = nvlist_create(0);
817 		if (req.args_nvl == NULL) {
818 			log_warn("error allocating nvlist");
819 			return (1);
820 		}
821 
822 		TAILQ_FOREACH(o, &lun->l_options, o_next)
823 			nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
824 
825 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
826 		if (req.args == NULL) {
827 			log_warn("error packing nvlist");
828 			return (1);
829 		}
830 	}
831 
832 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
833 	nvlist_destroy(req.args_nvl);
834 
835 	if (error != 0) {
836 		log_warn("error issuing CTL_LUN_REQ ioctl");
837 		return (1);
838 	}
839 
840 	switch (req.status) {
841 	case CTL_LUN_ERROR:
842 		log_warnx("LUN modification error: %s", req.error_str);
843 		return (1);
844 	case CTL_LUN_WARNING:
845 		log_warnx("LUN modification warning: %s", req.error_str);
846 		break;
847 	case CTL_LUN_OK:
848 		break;
849 	default:
850 		log_warnx("unknown LUN modification status: %d",
851 		    req.status);
852 		return (1);
853 	}
854 
855 	return (0);
856 }
857 
858 int
859 kernel_lun_remove(struct lun *lun)
860 {
861 	struct ctl_lun_req req;
862 
863 	bzero(&req, sizeof(req));
864 
865 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
866 	req.reqtype = CTL_LUNREQ_RM;
867 
868 	req.reqdata.rm.lun_id = lun->l_ctl_lun;
869 
870 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
871 		log_warn("error issuing CTL_LUN_REQ ioctl");
872 		return (1);
873 	}
874 
875 	switch (req.status) {
876 	case CTL_LUN_ERROR:
877 		log_warnx("LUN removal error: %s", req.error_str);
878 		return (1);
879 	case CTL_LUN_WARNING:
880 		log_warnx("LUN removal warning: %s", req.error_str);
881 		break;
882 	case CTL_LUN_OK:
883 		break;
884 	default:
885 		log_warnx("unknown LUN removal status: %d", req.status);
886 		return (1);
887 	}
888 
889 	return (0);
890 }
891 
892 void
893 kernel_handoff(struct ctld_connection *conn)
894 {
895 	struct ctl_iscsi req;
896 
897 	bzero(&req, sizeof(req));
898 
899 	req.type = CTL_ISCSI_HANDOFF;
900 	strlcpy(req.data.handoff.initiator_name,
901 	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
902 	strlcpy(req.data.handoff.initiator_addr,
903 	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
904 	if (conn->conn_initiator_alias != NULL) {
905 		strlcpy(req.data.handoff.initiator_alias,
906 		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
907 	}
908 	memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
909 	    sizeof(req.data.handoff.initiator_isid));
910 	strlcpy(req.data.handoff.target_name,
911 	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
912 	if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
913 		strlcpy(req.data.handoff.offload,
914 		    conn->conn_portal->p_portal_group->pg_offload,
915 		    sizeof(req.data.handoff.offload));
916 	}
917 #ifdef ICL_KERNEL_PROXY
918 	if (proxy_mode)
919 		req.data.handoff.connection_id = conn->conn.conn_socket;
920 	else
921 		req.data.handoff.socket = conn->conn.conn_socket;
922 #else
923 	req.data.handoff.socket = conn->conn.conn_socket;
924 #endif
925 	req.data.handoff.portal_group_tag =
926 	    conn->conn_portal->p_portal_group->pg_tag;
927 	if (conn->conn.conn_header_digest == CONN_DIGEST_CRC32C)
928 		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
929 	if (conn->conn.conn_data_digest == CONN_DIGEST_CRC32C)
930 		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
931 	req.data.handoff.cmdsn = conn->conn.conn_cmdsn;
932 	req.data.handoff.statsn = conn->conn.conn_statsn;
933 	req.data.handoff.max_recv_data_segment_length =
934 	    conn->conn.conn_max_recv_data_segment_length;
935 	req.data.handoff.max_send_data_segment_length =
936 	    conn->conn.conn_max_send_data_segment_length;
937 	req.data.handoff.max_burst_length = conn->conn.conn_max_burst_length;
938 	req.data.handoff.first_burst_length =
939 	    conn->conn.conn_first_burst_length;
940 	req.data.handoff.immediate_data = conn->conn.conn_immediate_data;
941 
942 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
943 		log_err(1, "error issuing CTL_ISCSI ioctl; "
944 		    "dropping connection");
945 	}
946 
947 	if (req.status != CTL_ISCSI_OK) {
948 		log_errx(1, "error returned from CTL iSCSI handoff request: "
949 		    "%s; dropping connection", req.error_str);
950 	}
951 }
952 
953 void
954 kernel_limits(const char *offload, int s, int *max_recv_dsl, int *max_send_dsl,
955     int *max_burst_length, int *first_burst_length)
956 {
957 	struct ctl_iscsi req;
958 	struct ctl_iscsi_limits_params *cilp;
959 
960 	bzero(&req, sizeof(req));
961 
962 	req.type = CTL_ISCSI_LIMITS;
963 	cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
964 	if (offload != NULL) {
965 		strlcpy(cilp->offload, offload, sizeof(cilp->offload));
966 	}
967 	cilp->socket = s;
968 
969 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
970 		log_err(1, "error issuing CTL_ISCSI ioctl; "
971 		    "dropping connection");
972 	}
973 
974 	if (req.status != CTL_ISCSI_OK) {
975 		log_errx(1, "error returned from CTL iSCSI limits request: "
976 		    "%s; dropping connection", req.error_str);
977 	}
978 
979 	if (cilp->max_recv_data_segment_length != 0) {
980 		*max_recv_dsl = cilp->max_recv_data_segment_length;
981 		*max_send_dsl = cilp->max_recv_data_segment_length;
982 	}
983 	if (cilp->max_send_data_segment_length != 0)
984 		*max_send_dsl = cilp->max_send_data_segment_length;
985 	if (cilp->max_burst_length != 0)
986 		*max_burst_length = cilp->max_burst_length;
987 	if (cilp->first_burst_length != 0)
988 		*first_burst_length = cilp->first_burst_length;
989 	if (*max_burst_length < *first_burst_length)
990 		*first_burst_length = *max_burst_length;
991 
992 	if (offload != NULL) {
993 		log_debugx("Kernel limits for offload \"%s\" are "
994 		    "MaxRecvDataSegment=%d, max_send_dsl=%d, "
995 		    "MaxBurstLength=%d, FirstBurstLength=%d",
996 		    offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
997 		    *first_burst_length);
998 	} else {
999 		log_debugx("Kernel limits are "
1000 		    "MaxRecvDataSegment=%d, max_send_dsl=%d, "
1001 		    "MaxBurstLength=%d, FirstBurstLength=%d",
1002 		    *max_recv_dsl, *max_send_dsl, *max_burst_length,
1003 		    *first_burst_length);
1004 	}
1005 }
1006 
1007 int
1008 kernel_port_add(struct port *port)
1009 {
1010 	struct option *o;
1011 	struct ctl_port_entry entry;
1012 	struct ctl_req req;
1013 	struct ctl_lun_map lm;
1014 	struct target *targ = port->p_target;
1015 	struct portal_group *pg = port->p_portal_group;
1016 	char result_buf[NVLIST_BUFSIZE];
1017 	int error, i;
1018 
1019 	/* Create iSCSI port. */
1020 	if (port->p_portal_group || port->p_ioctl_port) {
1021 		bzero(&req, sizeof(req));
1022 		req.reqtype = CTL_REQ_CREATE;
1023 
1024 		if (port->p_portal_group) {
1025 			strlcpy(req.driver, "iscsi", sizeof(req.driver));
1026 			req.args_nvl = nvlist_create(0);
1027 			nvlist_add_string(req.args_nvl, "cfiscsi_target",
1028 			    targ->t_name);
1029 			nvlist_add_string(req.args_nvl,
1030 			    "ctld_portal_group_name", pg->pg_name);
1031 			nvlist_add_stringf(req.args_nvl,
1032 			    "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1033 
1034 			if (targ->t_alias) {
1035 				nvlist_add_string(req.args_nvl,
1036 				    "cfiscsi_target_alias", targ->t_alias);
1037 			}
1038 
1039 			TAILQ_FOREACH(o, &pg->pg_options, o_next)
1040 				nvlist_add_string(req.args_nvl, o->o_name,
1041 				    o->o_value);
1042 		}
1043 
1044 		if (port->p_ioctl_port) {
1045 			strlcpy(req.driver, "ioctl", sizeof(req.driver));
1046 			req.args_nvl = nvlist_create(0);
1047 			nvlist_add_stringf(req.args_nvl, "pp", "%d",
1048 			    port->p_ioctl_pp);
1049 			nvlist_add_stringf(req.args_nvl, "vp", "%d",
1050 			    port->p_ioctl_vp);
1051 		}
1052 
1053 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
1054 		if (req.args == NULL) {
1055 			log_warn("error packing nvlist");
1056 			return (1);
1057 		}
1058 
1059 		req.result = result_buf;
1060 		req.result_len = sizeof(result_buf);
1061 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1062 		nvlist_destroy(req.args_nvl);
1063 
1064 		if (error != 0) {
1065 			log_warn("error issuing CTL_PORT_REQ ioctl");
1066 			return (1);
1067 		}
1068 		if (req.status == CTL_LUN_ERROR) {
1069 			log_warnx("error returned from port creation request: %s",
1070 			    req.error_str);
1071 			return (1);
1072 		}
1073 		if (req.status != CTL_LUN_OK) {
1074 			log_warnx("unknown port creation request status %d",
1075 			    req.status);
1076 			return (1);
1077 		}
1078 
1079 		req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0);
1080 		if (req.result_nvl == NULL) {
1081 			log_warnx("error unpacking result nvlist");
1082 			return (1);
1083 		}
1084 
1085 		port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id");
1086 		nvlist_destroy(req.result_nvl);
1087 	} else if (port->p_pport) {
1088 		port->p_ctl_port = port->p_pport->pp_ctl_port;
1089 
1090 		if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1091 		    strlen(targ->t_name) == 20) {
1092 			bzero(&entry, sizeof(entry));
1093 			entry.port_type = CTL_PORT_NONE;
1094 			entry.targ_port = port->p_ctl_port;
1095 			entry.flags |= CTL_PORT_WWNN_VALID;
1096 			entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1097 			if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1098 				log_warn("CTL_SET_PORT_WWNS ioctl failed");
1099 		}
1100 	}
1101 
1102 	/* Explicitly enable mapping to block any access except allowed. */
1103 	lm.port = port->p_ctl_port;
1104 	lm.plun = UINT32_MAX;
1105 	lm.lun = 0;
1106 	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1107 	if (error != 0)
1108 		log_warn("CTL_LUN_MAP ioctl failed");
1109 
1110 	/* Map configured LUNs */
1111 	for (i = 0; i < MAX_LUNS; i++) {
1112 		if (targ->t_luns[i] == NULL)
1113 			continue;
1114 		lm.port = port->p_ctl_port;
1115 		lm.plun = i;
1116 		lm.lun = targ->t_luns[i]->l_ctl_lun;
1117 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1118 		if (error != 0)
1119 			log_warn("CTL_LUN_MAP ioctl failed");
1120 	}
1121 
1122 	/* Enable port */
1123 	bzero(&entry, sizeof(entry));
1124 	entry.targ_port = port->p_ctl_port;
1125 	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1126 	if (error != 0) {
1127 		log_warn("CTL_ENABLE_PORT ioctl failed");
1128 		return (-1);
1129 	}
1130 
1131 	return (0);
1132 }
1133 
1134 int
1135 kernel_port_update(struct port *port, struct port *oport)
1136 {
1137 	struct ctl_lun_map lm;
1138 	struct target *targ = port->p_target;
1139 	struct target *otarg = oport->p_target;
1140 	int error, i;
1141 	uint32_t olun;
1142 
1143 	/* Map configured LUNs and unmap others */
1144 	for (i = 0; i < MAX_LUNS; i++) {
1145 		lm.port = port->p_ctl_port;
1146 		lm.plun = i;
1147 		if (targ->t_luns[i] == NULL)
1148 			lm.lun = UINT32_MAX;
1149 		else
1150 			lm.lun = targ->t_luns[i]->l_ctl_lun;
1151 		if (otarg->t_luns[i] == NULL)
1152 			olun = UINT32_MAX;
1153 		else
1154 			olun = otarg->t_luns[i]->l_ctl_lun;
1155 		if (lm.lun == olun)
1156 			continue;
1157 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1158 		if (error != 0)
1159 			log_warn("CTL_LUN_MAP ioctl failed");
1160 	}
1161 	return (0);
1162 }
1163 
1164 int
1165 kernel_port_remove(struct port *port)
1166 {
1167 	struct ctl_port_entry entry;
1168 	struct ctl_lun_map lm;
1169 	struct ctl_req req;
1170 	struct target *targ = port->p_target;
1171 	struct portal_group *pg = port->p_portal_group;
1172 	int error;
1173 
1174 	/* Disable port */
1175 	bzero(&entry, sizeof(entry));
1176 	entry.targ_port = port->p_ctl_port;
1177 	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1178 	if (error != 0) {
1179 		log_warn("CTL_DISABLE_PORT ioctl failed");
1180 		return (-1);
1181 	}
1182 
1183 	/* Remove iSCSI or ioctl port. */
1184 	if (port->p_portal_group || port->p_ioctl_port) {
1185 		bzero(&req, sizeof(req));
1186 		strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi",
1187 		    sizeof(req.driver));
1188 		req.reqtype = CTL_REQ_REMOVE;
1189 		req.args_nvl = nvlist_create(0);
1190 		if (req.args_nvl == NULL)
1191 			log_err(1, "nvlist_create");
1192 
1193 		if (port->p_ioctl_port)
1194 			nvlist_add_stringf(req.args_nvl, "port_id", "%d",
1195 			    port->p_ctl_port);
1196 		else {
1197 			nvlist_add_string(req.args_nvl, "cfiscsi_target",
1198 			    targ->t_name);
1199 			nvlist_add_stringf(req.args_nvl,
1200 			    "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1201 		}
1202 
1203 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
1204 		if (req.args == NULL) {
1205 			log_warn("error packing nvlist");
1206 			return (1);
1207 		}
1208 
1209 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1210 		nvlist_destroy(req.args_nvl);
1211 
1212 		if (error != 0) {
1213 			log_warn("error issuing CTL_PORT_REQ ioctl");
1214 			return (1);
1215 		}
1216 		if (req.status == CTL_LUN_ERROR) {
1217 			log_warnx("error returned from port removal request: %s",
1218 			    req.error_str);
1219 			return (1);
1220 		}
1221 		if (req.status != CTL_LUN_OK) {
1222 			log_warnx("unknown port removal request status %d",
1223 			    req.status);
1224 			return (1);
1225 		}
1226 	} else {
1227 		/* Disable LUN mapping. */
1228 		lm.port = port->p_ctl_port;
1229 		lm.plun = UINT32_MAX;
1230 		lm.lun = UINT32_MAX;
1231 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1232 		if (error != 0)
1233 			log_warn("CTL_LUN_MAP ioctl failed");
1234 	}
1235 	return (0);
1236 }
1237 
1238 #ifdef ICL_KERNEL_PROXY
1239 void
1240 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1241 {
1242 	struct ctl_iscsi req;
1243 
1244 	bzero(&req, sizeof(req));
1245 
1246 	req.type = CTL_ISCSI_LISTEN;
1247 	req.data.listen.iser = iser;
1248 	req.data.listen.domain = ai->ai_family;
1249 	req.data.listen.socktype = ai->ai_socktype;
1250 	req.data.listen.protocol = ai->ai_protocol;
1251 	req.data.listen.addr = ai->ai_addr;
1252 	req.data.listen.addrlen = ai->ai_addrlen;
1253 	req.data.listen.portal_id = portal_id;
1254 
1255 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1256 		log_err(1, "error issuing CTL_ISCSI ioctl");
1257 
1258 	if (req.status != CTL_ISCSI_OK) {
1259 		log_errx(1, "error returned from CTL iSCSI listen: %s",
1260 		    req.error_str);
1261 	}
1262 }
1263 
1264 void
1265 kernel_accept(int *connection_id, int *portal_id,
1266     struct sockaddr *client_sa, socklen_t *client_salen)
1267 {
1268 	struct ctl_iscsi req;
1269 	struct sockaddr_storage ss;
1270 
1271 	bzero(&req, sizeof(req));
1272 
1273 	req.type = CTL_ISCSI_ACCEPT;
1274 	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1275 
1276 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1277 		log_err(1, "error issuing CTL_ISCSI ioctl");
1278 
1279 	if (req.status != CTL_ISCSI_OK) {
1280 		log_errx(1, "error returned from CTL iSCSI accept: %s",
1281 		    req.error_str);
1282 	}
1283 
1284 	*connection_id = req.data.accept.connection_id;
1285 	*portal_id = req.data.accept.portal_id;
1286 	*client_salen = req.data.accept.initiator_addrlen;
1287 	memcpy(client_sa, &ss, *client_salen);
1288 }
1289 
1290 void
1291 kernel_send(struct pdu *pdu)
1292 {
1293 	struct ctl_iscsi req;
1294 
1295 	bzero(&req, sizeof(req));
1296 
1297 	req.type = CTL_ISCSI_SEND;
1298 	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1299 	req.data.send.bhs = pdu->pdu_bhs;
1300 	req.data.send.data_segment_len = pdu->pdu_data_len;
1301 	req.data.send.data_segment = pdu->pdu_data;
1302 
1303 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1304 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1305 		    "dropping connection");
1306 	}
1307 
1308 	if (req.status != CTL_ISCSI_OK) {
1309 		log_errx(1, "error returned from CTL iSCSI send: "
1310 		    "%s; dropping connection", req.error_str);
1311 	}
1312 }
1313 
1314 void
1315 kernel_receive(struct pdu *pdu)
1316 {
1317 	struct connection *conn;
1318 	struct ctl_iscsi req;
1319 
1320 	conn = pdu->pdu_connection;
1321 	pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1322 	if (pdu->pdu_data == NULL)
1323 		log_err(1, "malloc");
1324 
1325 	bzero(&req, sizeof(req));
1326 
1327 	req.type = CTL_ISCSI_RECEIVE;
1328 	req.data.receive.connection_id = conn->conn_socket;
1329 	req.data.receive.bhs = pdu->pdu_bhs;
1330 	req.data.receive.data_segment_len =
1331 	    conn->conn_max_recv_data_segment_length;
1332 	req.data.receive.data_segment = pdu->pdu_data;
1333 
1334 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1335 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1336 		    "dropping connection");
1337 	}
1338 
1339 	if (req.status != CTL_ISCSI_OK) {
1340 		log_errx(1, "error returned from CTL iSCSI receive: "
1341 		    "%s; dropping connection", req.error_str);
1342 	}
1343 
1344 }
1345 
1346 #endif /* ICL_KERNEL_PROXY */
1347 
1348 /*
1349  * XXX: I CANT INTO LATIN
1350  */
1351 void
1352 kernel_capsicate(void)
1353 {
1354 	cap_rights_t rights;
1355 	const unsigned long cmds[] = { CTL_ISCSI };
1356 
1357 	cap_rights_init(&rights, CAP_IOCTL);
1358 	if (caph_rights_limit(ctl_fd, &rights) < 0)
1359 		log_err(1, "cap_rights_limit");
1360 
1361 	if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0)
1362 		log_err(1, "cap_ioctls_limit");
1363 
1364 	if (caph_enter() < 0)
1365 		log_err(1, "cap_enter");
1366 
1367 	if (cap_sandboxed())
1368 		log_debugx("Capsicum capability mode enabled");
1369 	else
1370 		log_warnx("Capsicum capability mode not supported");
1371 }
1372 
1373