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