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