xref: /freebsd/usr.sbin/ctld/kernel.c (revision cc3f4b99653c34ae64f8a1fddea370abefef680e)
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  * $FreeBSD$
36  */
37 
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/param.h>
42 #include <sys/linker.h>
43 #include <sys/queue.h>
44 #include <sys/callout.h>
45 #include <sys/sbuf.h>
46 #include <sys/capsicum.h>
47 #include <assert.h>
48 #include <bsdxml.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <strings.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/ctl/ctl.h>
60 #include <cam/ctl/ctl_io.h>
61 #include <cam/ctl/ctl_frontend_internal.h>
62 #include <cam/ctl/ctl_backend.h>
63 #include <cam/ctl/ctl_ioctl.h>
64 #include <cam/ctl/ctl_backend_block.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_scsi_all.h>
67 
68 #include "ctld.h"
69 
70 #ifdef ICL_KERNEL_PROXY
71 #include <netdb.h>
72 #endif
73 
74 extern bool proxy_mode;
75 
76 static int	ctl_fd = 0;
77 
78 void
79 kernel_init(void)
80 {
81 	int retval, saved_errno;
82 
83 	ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84 	if (ctl_fd < 0 && errno == ENOENT) {
85 		saved_errno = errno;
86 		retval = kldload("ctl");
87 		if (retval != -1)
88 			ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
89 		else
90 			errno = saved_errno;
91 	}
92 	if (ctl_fd < 0)
93 		log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
94 }
95 
96 /*
97  * Name/value pair used for per-LUN attributes.
98  */
99 struct cctl_lun_nv {
100 	char *name;
101 	char *value;
102 	STAILQ_ENTRY(cctl_lun_nv) links;
103 };
104 
105 /*
106  * Backend LUN information.
107  */
108 struct cctl_lun {
109 	uint64_t lun_id;
110 	char *backend_type;
111 	uint64_t size_blocks;
112 	uint32_t blocksize;
113 	char *serial_number;
114 	char *device_id;
115 	char *cfiscsi_target;
116 	char *cfiscsi_target_alias;
117 	int cfiscsi_lun;
118 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
119 	STAILQ_ENTRY(cctl_lun) links;
120 };
121 
122 struct cctl_devlist_data {
123 	int num_luns;
124 	STAILQ_HEAD(,cctl_lun) lun_list;
125 	struct cctl_lun *cur_lun;
126 	int level;
127 	struct sbuf *cur_sb[32];
128 };
129 
130 static void
131 cctl_start_element(void *user_data, const char *name, const char **attr)
132 {
133 	int i;
134 	struct cctl_devlist_data *devlist;
135 	struct cctl_lun *cur_lun;
136 
137 	devlist = (struct cctl_devlist_data *)user_data;
138 	cur_lun = devlist->cur_lun;
139 	devlist->level++;
140 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
141 	    sizeof(devlist->cur_sb[0])))
142 		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
143 		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
144 
145 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
146 	if (devlist->cur_sb[devlist->level] == NULL)
147 		log_err(1, "%s: unable to allocate sbuf", __func__);
148 
149 	if (strcmp(name, "lun") == 0) {
150 		if (cur_lun != NULL)
151 			log_errx(1, "%s: improper lun element nesting",
152 			    __func__);
153 
154 		cur_lun = calloc(1, sizeof(*cur_lun));
155 		if (cur_lun == NULL)
156 			log_err(1, "%s: cannot allocate %zd bytes", __func__,
157 			    sizeof(*cur_lun));
158 
159 		devlist->num_luns++;
160 		devlist->cur_lun = cur_lun;
161 
162 		STAILQ_INIT(&cur_lun->attr_list);
163 		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
164 
165 		for (i = 0; attr[i] != NULL; i += 2) {
166 			if (strcmp(attr[i], "id") == 0) {
167 				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
168 			} else {
169 				log_errx(1, "%s: invalid LUN attribute %s = %s",
170 				     __func__, attr[i], attr[i+1]);
171 			}
172 		}
173 	}
174 }
175 
176 static void
177 cctl_end_element(void *user_data, const char *name)
178 {
179 	struct cctl_devlist_data *devlist;
180 	struct cctl_lun *cur_lun;
181 	char *str;
182 
183 	devlist = (struct cctl_devlist_data *)user_data;
184 	cur_lun = devlist->cur_lun;
185 
186 	if ((cur_lun == NULL)
187 	 && (strcmp(name, "ctllunlist") != 0))
188 		log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
189 
190 	if (devlist->cur_sb[devlist->level] == NULL)
191 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
192 		     devlist->level, name);
193 
194 	sbuf_finish(devlist->cur_sb[devlist->level]);
195 	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
196 
197 	if (strlen(str) == 0) {
198 		free(str);
199 		str = NULL;
200 	}
201 
202 	sbuf_delete(devlist->cur_sb[devlist->level]);
203 	devlist->cur_sb[devlist->level] = NULL;
204 	devlist->level--;
205 
206 	if (strcmp(name, "backend_type") == 0) {
207 		cur_lun->backend_type = str;
208 		str = NULL;
209 	} else if (strcmp(name, "size") == 0) {
210 		cur_lun->size_blocks = strtoull(str, NULL, 0);
211 	} else if (strcmp(name, "blocksize") == 0) {
212 		cur_lun->blocksize = strtoul(str, NULL, 0);
213 	} else if (strcmp(name, "serial_number") == 0) {
214 		cur_lun->serial_number = str;
215 		str = NULL;
216 	} else if (strcmp(name, "device_id") == 0) {
217 		cur_lun->device_id = str;
218 		str = NULL;
219 	} else if (strcmp(name, "cfiscsi_target") == 0) {
220 		cur_lun->cfiscsi_target = str;
221 		str = NULL;
222 	} else if (strcmp(name, "cfiscsi_target_alias") == 0) {
223 		cur_lun->cfiscsi_target_alias = str;
224 		str = NULL;
225 	} else if (strcmp(name, "cfiscsi_lun") == 0) {
226 		cur_lun->cfiscsi_lun = strtoul(str, NULL, 0);
227 	} else if (strcmp(name, "lun") == 0) {
228 		devlist->cur_lun = NULL;
229 	} else if (strcmp(name, "ctllunlist") == 0) {
230 
231 	} else {
232 		struct cctl_lun_nv *nv;
233 
234 		nv = calloc(1, sizeof(*nv));
235 		if (nv == NULL)
236 			log_err(1, "%s: can't allocate %zd bytes for nv pair",
237 			    __func__, sizeof(*nv));
238 
239 		nv->name = checked_strdup(name);
240 
241 		nv->value = str;
242 		str = NULL;
243 		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
244 	}
245 
246 	free(str);
247 }
248 
249 static void
250 cctl_char_handler(void *user_data, const XML_Char *str, int len)
251 {
252 	struct cctl_devlist_data *devlist;
253 
254 	devlist = (struct cctl_devlist_data *)user_data;
255 
256 	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
257 }
258 
259 struct conf *
260 conf_new_from_kernel(void)
261 {
262 	struct conf *conf = NULL;
263 	struct target *targ;
264 	struct lun *cl;
265 	struct lun_option *lo;
266 	struct ctl_lun_list list;
267 	struct cctl_devlist_data devlist;
268 	struct cctl_lun *lun;
269 	XML_Parser parser;
270 	char *lun_str = NULL;
271 	int lun_len;
272 	int retval;
273 
274 	lun_len = 4096;
275 
276 	bzero(&devlist, sizeof(devlist));
277 	STAILQ_INIT(&devlist.lun_list);
278 
279 	log_debugx("obtaining previously configured CTL luns from the kernel");
280 
281 retry:
282 	lun_str = realloc(lun_str, lun_len);
283 	if (lun_str == NULL)
284 		log_err(1, "realloc");
285 
286 	bzero(&list, sizeof(list));
287 	list.alloc_len = lun_len;
288 	list.status = CTL_LUN_LIST_NONE;
289 	list.lun_xml = lun_str;
290 
291 	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
292 		log_warn("error issuing CTL_LUN_LIST ioctl");
293 		free(lun_str);
294 		return (NULL);
295 	}
296 
297 	if (list.status == CTL_LUN_LIST_ERROR) {
298 		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
299 		    list.error_str);
300 		free(lun_str);
301 		return (NULL);
302 	}
303 
304 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
305 		lun_len = lun_len << 1;
306 		goto retry;
307 	}
308 
309 	parser = XML_ParserCreate(NULL);
310 	if (parser == NULL) {
311 		log_warnx("unable to create XML parser");
312 		free(lun_str);
313 		return (NULL);
314 	}
315 
316 	XML_SetUserData(parser, &devlist);
317 	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
318 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
319 
320 	retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
321 	XML_ParserFree(parser);
322 	free(lun_str);
323 	if (retval != 1) {
324 		log_warnx("XML_Parse failed");
325 		return (NULL);
326 	}
327 
328 	conf = conf_new();
329 
330 	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
331 		struct cctl_lun_nv *nv;
332 
333 		if (lun->cfiscsi_target == NULL) {
334 			log_debugx("CTL lun %ju wasn't managed by ctld; "
335 			    "ignoring", (uintmax_t)lun->lun_id);
336 			continue;
337 		}
338 
339 		targ = target_find(conf, lun->cfiscsi_target);
340 		if (targ == NULL) {
341 #if 0
342 			log_debugx("found new kernel target %s for CTL lun %ld",
343 			    lun->cfiscsi_target, lun->lun_id);
344 #endif
345 			targ = target_new(conf, lun->cfiscsi_target);
346 			if (targ == NULL) {
347 				log_warnx("target_new failed");
348 				continue;
349 			}
350 		}
351 
352 		cl = lun_find(targ, lun->cfiscsi_lun);
353 		if (cl != NULL) {
354 			log_warnx("found CTL lun %ju, backing lun %d, target "
355 			    "%s, also backed by CTL lun %d; ignoring",
356 			    (uintmax_t) lun->lun_id, cl->l_lun,
357 			    cl->l_target->t_name, cl->l_ctl_lun);
358 			continue;
359 		}
360 
361 		log_debugx("found CTL lun %ju, backing lun %d, target %s",
362 		    (uintmax_t)lun->lun_id, lun->cfiscsi_lun, lun->cfiscsi_target);
363 
364 		cl = lun_new(targ, lun->cfiscsi_lun);
365 		if (cl == NULL) {
366 			log_warnx("lun_new failed");
367 			continue;
368 		}
369 		lun_set_backend(cl, lun->backend_type);
370 		lun_set_blocksize(cl, lun->blocksize);
371 		lun_set_device_id(cl, lun->device_id);
372 		lun_set_serial(cl, lun->serial_number);
373 		lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
374 		lun_set_ctl_lun(cl, lun->lun_id);
375 
376 		STAILQ_FOREACH(nv, &lun->attr_list, links) {
377 			if (strcmp(nv->name, "file") == 0 ||
378 			    strcmp(nv->name, "dev") == 0) {
379 				lun_set_path(cl, nv->value);
380 				continue;
381 			}
382 			lo = lun_option_new(cl, nv->name, nv->value);
383 			if (lo == NULL)
384 				log_warnx("unable to add CTL lun option %s "
385 				    "for CTL lun %ju for lun %d, target %s",
386 				    nv->name, (uintmax_t) lun->lun_id,
387 				    cl->l_lun, cl->l_target->t_name);
388 		}
389 	}
390 
391 	return (conf);
392 }
393 
394 int
395 kernel_lun_add(struct lun *lun)
396 {
397 	struct lun_option *lo;
398 	struct ctl_lun_req req;
399 	char *tmp;
400 	int error, i, num_options;
401 
402 	bzero(&req, sizeof(req));
403 
404 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
405 	req.reqtype = CTL_LUNREQ_CREATE;
406 
407 	req.reqdata.create.blocksize_bytes = lun->l_blocksize;
408 
409 	if (lun->l_size != 0)
410 		req.reqdata.create.lun_size_bytes = lun->l_size;
411 
412 	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
413 	req.reqdata.create.device_type = T_DIRECT;
414 
415 	if (lun->l_serial != NULL) {
416 		strlcpy(req.reqdata.create.serial_num, lun->l_serial,
417 			sizeof(req.reqdata.create.serial_num));
418 		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
419 	}
420 
421 	if (lun->l_device_id != NULL) {
422 		strlcpy(req.reqdata.create.device_id, lun->l_device_id,
423 			sizeof(req.reqdata.create.device_id));
424 		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
425 	}
426 
427 	if (lun->l_path != NULL) {
428 		lo = lun_option_find(lun, "file");
429 		if (lo != NULL) {
430 			lun_option_set(lo, lun->l_path);
431 		} else {
432 			lo = lun_option_new(lun, "file", lun->l_path);
433 			assert(lo != NULL);
434 		}
435 	}
436 
437 	lo = lun_option_find(lun, "cfiscsi_target");
438 	if (lo != NULL) {
439 		lun_option_set(lo, lun->l_target->t_name);
440 	} else {
441 		lo = lun_option_new(lun, "cfiscsi_target",
442 		    lun->l_target->t_name);
443 		assert(lo != NULL);
444 	}
445 
446 	if (lun->l_target->t_alias != NULL) {
447 		lo = lun_option_find(lun, "cfiscsi_target_alias");
448 		if (lo != NULL) {
449 			lun_option_set(lo, lun->l_target->t_alias);
450 		} else {
451 			lo = lun_option_new(lun, "cfiscsi_target_alias",
452 			    lun->l_target->t_alias);
453 			assert(lo != NULL);
454 		}
455 	}
456 
457 	asprintf(&tmp, "%d", lun->l_lun);
458 	if (tmp == NULL)
459 		log_errx(1, "asprintf");
460 	lo = lun_option_find(lun, "cfiscsi_lun");
461 	if (lo != NULL) {
462 		lun_option_set(lo, tmp);
463 		free(tmp);
464 	} else {
465 		lo = lun_option_new(lun, "cfiscsi_lun", tmp);
466 		free(tmp);
467 		assert(lo != NULL);
468 	}
469 
470 	num_options = 0;
471 	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
472 		num_options++;
473 
474 	req.num_be_args = num_options;
475 	if (num_options > 0) {
476 		req.be_args = malloc(num_options * sizeof(*req.be_args));
477 		if (req.be_args == NULL) {
478 			log_warn("error allocating %zd bytes",
479 			    num_options * sizeof(*req.be_args));
480 			return (1);
481 		}
482 
483 		i = 0;
484 		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
485 			 /*
486 			  * +1 for the terminating '\0'
487 			  */
488 			req.be_args[i].namelen = strlen(lo->lo_name) + 1;
489 			req.be_args[i].name = lo->lo_name;
490 			req.be_args[i].vallen = strlen(lo->lo_value) + 1;
491 			req.be_args[i].value = lo->lo_value;
492 			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
493 			i++;
494 		}
495 		assert(i == num_options);
496 	}
497 
498 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
499 	free(req.be_args);
500 	if (error != 0) {
501 		log_warn("error issuing CTL_LUN_REQ ioctl");
502 		return (1);
503 	}
504 
505 	if (req.status == CTL_LUN_ERROR) {
506 		log_warnx("error returned from LUN creation request: %s",
507 		    req.error_str);
508 		return (1);
509 	}
510 
511 	if (req.status != CTL_LUN_OK) {
512 		log_warnx("unknown LUN creation request status %d",
513 		    req.status);
514 		return (1);
515 	}
516 
517 	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
518 
519 	return (0);
520 }
521 
522 int
523 kernel_lun_resize(struct lun *lun)
524 {
525 	struct ctl_lun_req req;
526 
527 	bzero(&req, sizeof(req));
528 
529 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
530 	req.reqtype = CTL_LUNREQ_MODIFY;
531 
532 	req.reqdata.modify.lun_id = lun->l_ctl_lun;
533 	req.reqdata.modify.lun_size_bytes = lun->l_size;
534 
535 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
536 		log_warn("error issuing CTL_LUN_REQ ioctl");
537 		return (1);
538 	}
539 
540 	if (req.status == CTL_LUN_ERROR) {
541 		log_warnx("error returned from LUN modification request: %s",
542 		    req.error_str);
543 		return (1);
544 	}
545 
546 	if (req.status != CTL_LUN_OK) {
547 		log_warnx("unknown LUN modification request status %d",
548 		    req.status);
549 		return (1);
550 	}
551 
552 	return (0);
553 }
554 
555 int
556 kernel_lun_remove(struct lun *lun)
557 {
558 	struct ctl_lun_req req;
559 
560 	bzero(&req, sizeof(req));
561 
562 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
563 	req.reqtype = CTL_LUNREQ_RM;
564 
565 	req.reqdata.rm.lun_id = lun->l_ctl_lun;
566 
567 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
568 		log_warn("error issuing CTL_LUN_REQ ioctl");
569 		return (1);
570 	}
571 
572 	if (req.status == CTL_LUN_ERROR) {
573 		log_warnx("error returned from LUN removal request: %s",
574 		    req.error_str);
575 		return (1);
576 	}
577 
578 	if (req.status != CTL_LUN_OK) {
579 		log_warnx("unknown LUN removal request status %d", req.status);
580 		return (1);
581 	}
582 
583 	return (0);
584 }
585 
586 void
587 kernel_handoff(struct connection *conn)
588 {
589 	struct ctl_iscsi req;
590 
591 	bzero(&req, sizeof(req));
592 
593 	req.type = CTL_ISCSI_HANDOFF;
594 	strlcpy(req.data.handoff.initiator_name,
595 	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
596 	strlcpy(req.data.handoff.initiator_addr,
597 	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
598 	if (conn->conn_initiator_alias != NULL) {
599 		strlcpy(req.data.handoff.initiator_alias,
600 		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
601 	}
602 	strlcpy(req.data.handoff.target_name,
603 	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
604 #ifdef ICL_KERNEL_PROXY
605 	if (proxy_mode)
606 		req.data.handoff.connection_id = conn->conn_socket;
607 	else
608 		req.data.handoff.socket = conn->conn_socket;
609 #else
610 	req.data.handoff.socket = conn->conn_socket;
611 #endif
612 	req.data.handoff.portal_group_tag =
613 	    conn->conn_portal->p_portal_group->pg_tag;
614 	if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
615 		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
616 	if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
617 		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
618 	req.data.handoff.cmdsn = conn->conn_cmdsn;
619 	req.data.handoff.statsn = conn->conn_statsn;
620 	req.data.handoff.max_recv_data_segment_length =
621 	    conn->conn_max_data_segment_length;
622 	req.data.handoff.max_burst_length = conn->conn_max_burst_length;
623 	req.data.handoff.immediate_data = conn->conn_immediate_data;
624 
625 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
626 		log_err(1, "error issuing CTL_ISCSI ioctl; "
627 		    "dropping connection");
628 	}
629 
630 	if (req.status != CTL_ISCSI_OK) {
631 		log_errx(1, "error returned from CTL iSCSI handoff request: "
632 		    "%s; dropping connection", req.error_str);
633 	}
634 }
635 
636 int
637 kernel_port_on(void)
638 {
639 	struct ctl_port_entry entry;
640 	int error;
641 
642 	bzero(&entry, sizeof(entry));
643 
644 	entry.port_type = CTL_PORT_ISCSI;
645 	entry.targ_port = -1;
646 
647 	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
648 	if (error != 0) {
649 		log_warn("CTL_ENABLE_PORT ioctl failed");
650 		return (-1);
651 	}
652 
653 	return (0);
654 }
655 
656 int
657 kernel_port_off(void)
658 {
659 	struct ctl_port_entry entry;
660 	int error;
661 
662 	bzero(&entry, sizeof(entry));
663 
664 	entry.port_type = CTL_PORT_ISCSI;
665 	entry.targ_port = -1;
666 
667 	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
668 	if (error != 0) {
669 		log_warn("CTL_DISABLE_PORT ioctl failed");
670 		return (-1);
671 	}
672 
673 	return (0);
674 }
675 
676 #ifdef ICL_KERNEL_PROXY
677 void
678 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
679 {
680 	struct ctl_iscsi req;
681 
682 	bzero(&req, sizeof(req));
683 
684 	req.type = CTL_ISCSI_LISTEN;
685 	req.data.listen.iser = iser;
686 	req.data.listen.domain = ai->ai_family;
687 	req.data.listen.socktype = ai->ai_socktype;
688 	req.data.listen.protocol = ai->ai_protocol;
689 	req.data.listen.addr = ai->ai_addr;
690 	req.data.listen.addrlen = ai->ai_addrlen;
691 	req.data.listen.portal_id = portal_id;
692 
693 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
694 		log_err(1, "error issuing CTL_ISCSI ioctl");
695 
696 	if (req.status != CTL_ISCSI_OK) {
697 		log_errx(1, "error returned from CTL iSCSI listen: %s",
698 		    req.error_str);
699 	}
700 }
701 
702 void
703 kernel_accept(int *connection_id, int *portal_id,
704     struct sockaddr *client_sa, socklen_t *client_salen)
705 {
706 	struct ctl_iscsi req;
707 	struct sockaddr_storage ss;
708 
709 	bzero(&req, sizeof(req));
710 
711 	req.type = CTL_ISCSI_ACCEPT;
712 	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
713 
714 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
715 		log_err(1, "error issuing CTL_ISCSI ioctl");
716 
717 	if (req.status != CTL_ISCSI_OK) {
718 		log_errx(1, "error returned from CTL iSCSI accept: %s",
719 		    req.error_str);
720 	}
721 
722 	*connection_id = req.data.accept.connection_id;
723 	*portal_id = req.data.accept.portal_id;
724 	*client_salen = req.data.accept.initiator_addrlen;
725 	memcpy(client_sa, &ss, *client_salen);
726 }
727 
728 void
729 kernel_send(struct pdu *pdu)
730 {
731 	struct ctl_iscsi req;
732 
733 	bzero(&req, sizeof(req));
734 
735 	req.type = CTL_ISCSI_SEND;
736 	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
737 	req.data.send.bhs = pdu->pdu_bhs;
738 	req.data.send.data_segment_len = pdu->pdu_data_len;
739 	req.data.send.data_segment = pdu->pdu_data;
740 
741 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
742 		log_err(1, "error issuing CTL_ISCSI ioctl; "
743 		    "dropping connection");
744 	}
745 
746 	if (req.status != CTL_ISCSI_OK) {
747 		log_errx(1, "error returned from CTL iSCSI send: "
748 		    "%s; dropping connection", req.error_str);
749 	}
750 }
751 
752 void
753 kernel_receive(struct pdu *pdu)
754 {
755 	struct ctl_iscsi req;
756 
757 	pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
758 	if (pdu->pdu_data == NULL)
759 		log_err(1, "malloc");
760 
761 	bzero(&req, sizeof(req));
762 
763 	req.type = CTL_ISCSI_RECEIVE;
764 	req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
765 	req.data.receive.bhs = pdu->pdu_bhs;
766 	req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
767 	req.data.receive.data_segment = pdu->pdu_data;
768 
769 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
770 		log_err(1, "error issuing CTL_ISCSI ioctl; "
771 		    "dropping connection");
772 	}
773 
774 	if (req.status != CTL_ISCSI_OK) {
775 		log_errx(1, "error returned from CTL iSCSI receive: "
776 		    "%s; dropping connection", req.error_str);
777 	}
778 
779 }
780 
781 #endif /* ICL_KERNEL_PROXY */
782 
783 /*
784  * XXX: I CANT INTO LATIN
785  */
786 void
787 kernel_capsicate(void)
788 {
789 	int error;
790 	cap_rights_t rights;
791 	const unsigned long cmds[] = { CTL_ISCSI };
792 
793 	cap_rights_init(&rights, CAP_IOCTL);
794 	error = cap_rights_limit(ctl_fd, &rights);
795 	if (error != 0 && errno != ENOSYS)
796 		log_err(1, "cap_rights_limit");
797 
798 	error = cap_ioctls_limit(ctl_fd, cmds,
799 	    sizeof(cmds) / sizeof(cmds[0]));
800 	if (error != 0 && errno != ENOSYS)
801 		log_err(1, "cap_ioctls_limit");
802 
803 	error = cap_enter();
804 	if (error != 0 && errno != ENOSYS)
805 		log_err(1, "cap_enter");
806 
807 	if (cap_sandboxed())
808 		log_debugx("Capsicum capability mode enabled");
809 	else
810 		log_warnx("Capsicum capability mode not supported");
811 }
812 
813