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