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