xref: /freebsd/usr.sbin/ctld/kernel.cc (revision 03ac6fe4f6d2bce06a672024152eb894a423dfcb)
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/stat.h>
50 #include <assert.h>
51 #include <bsdxml.h>
52 #include <capsicum_helpers.h>
53 #include <ctype.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <strings.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/ctl/ctl.h>
64 #include <cam/ctl/ctl_io.h>
65 #include <cam/ctl/ctl_backend.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_util.h>
68 #include <cam/ctl/ctl_scsi_all.h>
69 
70 #include "ctld.hh"
71 
72 #ifdef ICL_KERNEL_PROXY
73 #include <netdb.h>
74 #endif
75 
76 #define	NVLIST_BUFSIZE	1024
77 
78 int	ctl_fd = 0;
79 
80 void
81 kernel_init(void)
82 {
83 	int retval, saved_errno;
84 
85 	ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
86 	if (ctl_fd < 0 && errno == ENOENT) {
87 		saved_errno = errno;
88 		retval = kldload("ctl");
89 		if (retval != -1)
90 			ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
91 		else
92 			errno = saved_errno;
93 	}
94 	if (ctl_fd < 0)
95 		log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
96 #ifdef	WANT_ISCSI
97 	else {
98 		saved_errno = errno;
99 		if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
100 			log_warn("couldn't load cfiscsi");
101 		errno = saved_errno;
102 	}
103 #endif
104 }
105 
106 /*
107  * Backend LUN information.
108  */
109 using attr_list_t = std::list<std::pair<std::string, std::string>>;
110 
111 struct cctl_lun {
112 	uint64_t lun_id;
113 	std::string backend_type;
114 	uint8_t device_type;
115 	uint64_t size_blocks;
116 	uint32_t blocksize;
117 	std::string serial_number;
118 	std::string device_id;
119 	std::string ctld_name;
120 	attr_list_t attr_list;
121 };
122 
123 struct cctl_port {
124 	uint32_t port_id;
125 	std::string port_frontend;
126 	std::string port_name;
127 	int pp;
128 	int vp;
129 	uint16_t portid;
130 	int cfiscsi_state;
131 	std::string cfiscsi_target;
132 	std::string nqn;
133 	uint16_t cfiscsi_portal_group_tag;
134 	std::string ctld_portal_group_name;
135 	std::string ctld_transport_group_name;
136 	attr_list_t attr_list;
137 };
138 
139 struct cctl_devlist_data {
140 	std::list<cctl_lun> lun_list;
141 	struct cctl_lun *cur_lun = nullptr;
142 	std::list<cctl_port> port_list;
143 	struct cctl_port *cur_port = nullptr;
144 	u_int level = 0;
145 	struct sbuf *cur_sb[32] = {};
146 };
147 
148 static void
149 cctl_start_element(void *user_data, const char *name, const char **attr)
150 {
151 	int i;
152 	struct cctl_devlist_data *devlist;
153 	struct cctl_lun *cur_lun;
154 
155 	devlist = (struct cctl_devlist_data *)user_data;
156 	cur_lun = devlist->cur_lun;
157 	devlist->level++;
158 	if (devlist->level >= nitems(devlist->cur_sb))
159 		log_errx(1, "%s: too many nesting levels, %zu max", __func__,
160 		     nitems(devlist->cur_sb));
161 
162 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
163 	if (devlist->cur_sb[devlist->level] == NULL)
164 		log_err(1, "%s: unable to allocate sbuf", __func__);
165 
166 	if (strcmp(name, "lun") == 0) {
167 		if (cur_lun != NULL)
168 			log_errx(1, "%s: improper lun element nesting",
169 			    __func__);
170 
171 		devlist->lun_list.emplace_back();
172 		cur_lun = &devlist->lun_list.back();
173 
174 		devlist->cur_lun = cur_lun;
175 
176 		for (i = 0; attr[i] != NULL; i += 2) {
177 			if (strcmp(attr[i], "id") == 0) {
178 				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
179 			} else {
180 				log_errx(1, "%s: invalid LUN attribute %s = %s",
181 				     __func__, attr[i], attr[i+1]);
182 			}
183 		}
184 	}
185 }
186 
187 static void
188 cctl_end_element(void *user_data, const char *name)
189 {
190 	struct cctl_devlist_data *devlist;
191 	struct cctl_lun *cur_lun;
192 	std::string str;
193 
194 	devlist = (struct cctl_devlist_data *)user_data;
195 	cur_lun = devlist->cur_lun;
196 
197 	if ((cur_lun == NULL)
198 	 && (strcmp(name, "ctllunlist") != 0))
199 		log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
200 
201 	if (devlist->cur_sb[devlist->level] == NULL)
202 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
203 		     devlist->level, name);
204 
205 	sbuf_finish(devlist->cur_sb[devlist->level]);
206 	str = sbuf_data(devlist->cur_sb[devlist->level]);
207 
208 	sbuf_delete(devlist->cur_sb[devlist->level]);
209 	devlist->cur_sb[devlist->level] = NULL;
210 	devlist->level--;
211 
212 	if (strcmp(name, "backend_type") == 0) {
213 		cur_lun->backend_type = std::move(str);
214 	} else if (strcmp(name, "lun_type") == 0) {
215 		if (str.empty())
216 			log_errx(1, "%s: %s missing its argument", __func__, name);
217 		cur_lun->device_type = strtoull(str.c_str(), NULL, 0);
218 	} else if (strcmp(name, "size") == 0) {
219 		if (str.empty())
220 			log_errx(1, "%s: %s missing its argument", __func__, name);
221 		cur_lun->size_blocks = strtoull(str.c_str(), NULL, 0);
222 	} else if (strcmp(name, "blocksize") == 0) {
223 		if (str.empty())
224 			log_errx(1, "%s: %s missing its argument", __func__, name);
225 		cur_lun->blocksize = strtoul(str.c_str(), NULL, 0);
226 	} else if (strcmp(name, "serial_number") == 0) {
227 		cur_lun->serial_number = std::move(str);
228 	} else if (strcmp(name, "device_id") == 0) {
229 		cur_lun->device_id = std::move(str);
230 	} else if (strcmp(name, "ctld_name") == 0) {
231 		cur_lun->ctld_name = std::move(str);
232 	} else if (strcmp(name, "lun") == 0) {
233 		devlist->cur_lun = NULL;
234 	} else if (strcmp(name, "ctllunlist") == 0) {
235 		/* Nothing. */
236 	} else {
237 		cur_lun->attr_list.emplace_back(name, std::move(str));
238 	}
239 }
240 
241 static void
242 cctl_start_pelement(void *user_data, const char *name, const char **attr)
243 {
244 	int i;
245 	struct cctl_devlist_data *devlist;
246 	struct cctl_port *cur_port;
247 
248 	devlist = (struct cctl_devlist_data *)user_data;
249 	cur_port = devlist->cur_port;
250 	devlist->level++;
251 	if (devlist->level >= nitems(devlist->cur_sb))
252 		log_errx(1, "%s: too many nesting levels, %zu max", __func__,
253 		     nitems(devlist->cur_sb));
254 
255 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
256 	if (devlist->cur_sb[devlist->level] == NULL)
257 		log_err(1, "%s: unable to allocate sbuf", __func__);
258 
259 	if (strcmp(name, "targ_port") == 0) {
260 		if (cur_port != NULL)
261 			log_errx(1, "%s: improper port element nesting (%s)",
262 			    __func__, name);
263 
264 		devlist->port_list.emplace_back();
265 		cur_port = &devlist->port_list.back();
266 		devlist->cur_port = cur_port;
267 
268 		for (i = 0; attr[i] != NULL; i += 2) {
269 			if (strcmp(attr[i], "id") == 0) {
270 				cur_port->port_id = strtoul(attr[i+1], NULL, 0);
271 			} else {
272 				log_errx(1, "%s: invalid LUN attribute %s = %s",
273 				     __func__, attr[i], attr[i+1]);
274 			}
275 		}
276 	}
277 }
278 
279 static void
280 cctl_end_pelement(void *user_data, const char *name)
281 {
282 	struct cctl_devlist_data *devlist;
283 	struct cctl_port *cur_port;
284 	std::string str;
285 
286 	devlist = (struct cctl_devlist_data *)user_data;
287 	cur_port = devlist->cur_port;
288 
289 	if ((cur_port == NULL)
290 	 && (strcmp(name, "ctlportlist") != 0))
291 		log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
292 
293 	if (devlist->cur_sb[devlist->level] == NULL)
294 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
295 		     devlist->level, name);
296 
297 	sbuf_finish(devlist->cur_sb[devlist->level]);
298 	str = sbuf_data(devlist->cur_sb[devlist->level]);
299 
300 	sbuf_delete(devlist->cur_sb[devlist->level]);
301 	devlist->cur_sb[devlist->level] = NULL;
302 	devlist->level--;
303 
304 	if (strcmp(name, "frontend_type") == 0) {
305 		cur_port->port_frontend = std::move(str);
306 	} else if (strcmp(name, "port_name") == 0) {
307 		cur_port->port_name = std::move(str);
308 	} else if (strcmp(name, "physical_port") == 0) {
309 		if (str.empty())
310 			log_errx(1, "%s: %s missing its argument", __func__, name);
311 		cur_port->pp = strtoul(str.c_str(), NULL, 0);
312 	} else if (strcmp(name, "virtual_port") == 0) {
313 		if (str.empty())
314 			log_errx(1, "%s: %s missing its argument", __func__, name);
315 		cur_port->vp = strtoul(str.c_str(), NULL, 0);
316 	} else if (strcmp(name, "cfiscsi_target") == 0) {
317 		cur_port->cfiscsi_target = std::move(str);
318 	} else if (strcmp(name, "cfiscsi_state") == 0) {
319 		if (str.empty())
320 			log_errx(1, "%s: %s missing its argument", __func__, name);
321 		cur_port->cfiscsi_state = strtoul(str.c_str(), NULL, 0);
322 	} else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
323 		if (str.empty())
324 			log_errx(1, "%s: %s missing its argument", __func__, name);
325 		cur_port->cfiscsi_portal_group_tag = strtoul(str.c_str(), NULL, 0);
326 	} else if (strcmp(name, "ctld_portal_group_name") == 0) {
327 		cur_port->ctld_portal_group_name = std::move(str);
328 	} else if (strcmp(name, "ctld_transport_group_name") == 0) {
329 		cur_port->ctld_transport_group_name = std::move(str);
330 	} else if (strcmp(name, "nqn") == 0) {
331 		cur_port->nqn = std::move(str);
332 	} else if (strcmp(name, "portid") == 0) {
333 		if (str.empty())
334 			log_errx(1, "%s: %s missing its argument", __func__, name);
335 		cur_port->portid = strtoul(str.c_str(), NULL, 0);
336 	} else if (strcmp(name, "targ_port") == 0) {
337 		devlist->cur_port = NULL;
338 	} else if (strcmp(name, "ctlportlist") == 0) {
339 		/* Nothing. */
340 	} else {
341 		cur_port->attr_list.emplace_back(name, std::move(str));
342 	}
343 }
344 
345 static void
346 cctl_char_handler(void *user_data, const XML_Char *str, int len)
347 {
348 	struct cctl_devlist_data *devlist;
349 
350 	devlist = (struct cctl_devlist_data *)user_data;
351 
352 	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
353 }
354 
355 static bool
356 parse_kernel_config(struct cctl_devlist_data &devlist)
357 {
358 	struct ctl_lun_list list;
359 	XML_Parser parser;
360 	int retval;
361 
362 	std::vector<char> buf(4096);
363 retry:
364 	bzero(&list, sizeof(list));
365 	list.alloc_len = buf.size();
366 	list.status = CTL_LUN_LIST_NONE;
367 	list.lun_xml = buf.data();
368 
369 	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
370 		log_warn("error issuing CTL_LUN_LIST ioctl");
371 		return (false);
372 	}
373 
374 	if (list.status == CTL_LUN_LIST_ERROR) {
375 		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
376 		    list.error_str);
377 		return (false);
378 	}
379 
380 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
381 		buf.resize(buf.size() << 1);
382 		goto retry;
383 	}
384 
385 	parser = XML_ParserCreate(NULL);
386 	if (parser == NULL) {
387 		log_warnx("unable to create XML parser");
388 		return (false);
389 	}
390 
391 	XML_SetUserData(parser, &devlist);
392 	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
393 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
394 
395 	retval = XML_Parse(parser, buf.data(), strlen(buf.data()), 1);
396 	XML_ParserFree(parser);
397 	if (retval != 1) {
398 		log_warnx("XML_Parse failed");
399 		return (false);
400 	}
401 
402 retry_port:
403 	bzero(&list, sizeof(list));
404 	list.alloc_len = buf.size();
405 	list.status = CTL_LUN_LIST_NONE;
406 	list.lun_xml = buf.data();
407 
408 	if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
409 		log_warn("error issuing CTL_PORT_LIST ioctl");
410 		return (false);
411 	}
412 
413 	if (list.status == CTL_LUN_LIST_ERROR) {
414 		log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
415 		    list.error_str);
416 		return (false);
417 	}
418 
419 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
420 		buf.resize(buf.size() << 1);
421 		goto retry_port;
422 	}
423 
424 	parser = XML_ParserCreate(NULL);
425 	if (parser == NULL) {
426 		log_warnx("unable to create XML parser");
427 		return (false);
428 	}
429 
430 	XML_SetUserData(parser, &devlist);
431 	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
432 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
433 
434 	retval = XML_Parse(parser, buf.data(), strlen(buf.data()), 1);
435 	XML_ParserFree(parser);
436 	if (retval != 1) {
437 		log_warnx("XML_Parse failed");
438 		return (false);
439 	}
440 
441 	return (true);
442 }
443 
444 static void
445 add_iscsi_port(struct conf *conf, const struct cctl_port &port,
446     std::string &name)
447 {
448 	if (port.cfiscsi_target.empty()) {
449 		log_debugx("CTL iSCSI port %u \"%s\" is not managed by ctld; "
450 		    "ignoring", port.port_id, name.c_str());
451 		return;
452 	}
453 	if (port.cfiscsi_state != 1) {
454 		log_debugx("CTL port %ju is not active (%d); ignoring",
455 		    (uintmax_t)port.port_id, port.cfiscsi_state);
456 		return;
457 	}
458 
459 	const char *t_name = port.cfiscsi_target.c_str();
460 	struct target *targ = conf->find_target(t_name);
461 	if (targ == nullptr) {
462 		targ = conf->add_target(t_name);
463 		if (targ == nullptr) {
464 			log_warnx("Failed to add target \"%s\"", t_name);
465 			return;
466 		}
467 	}
468 
469 	if (port.ctld_portal_group_name.empty())
470 		return;
471 
472 	const char *pg_name = port.ctld_portal_group_name.c_str();
473 	struct portal_group *pg = conf->find_portal_group(pg_name);
474 	if (pg == nullptr) {
475 		pg = conf->add_portal_group(pg_name);
476 		if (pg == nullptr) {
477 			log_warnx("Failed to add portal-group \"%s\"", pg_name);
478 			return;
479 		}
480 
481 		pg->set_kernel();
482 	}
483 	pg->set_tag(port.cfiscsi_portal_group_tag);
484 	if (!conf->add_port(targ, pg, port.port_id)) {
485 		log_warnx("Failed to add port for target \"%s\" and portal-group \"%s\"",
486 		    t_name, pg_name);
487 	}
488 }
489 
490 static void
491 add_nvmf_port(struct conf *conf, const struct cctl_port &port,
492     std::string &name)
493 {
494 	if (port.nqn.empty() || port.ctld_transport_group_name.empty()) {
495 		log_debugx("CTL NVMeoF port %u \"%s\" is not managed by ctld; "
496 		    "ignoring", port.port_id, name.c_str());
497 		return;
498 	}
499 
500 	const char *nqn = port.nqn.c_str();
501 	struct target *targ = conf->find_controller(nqn);
502 	if (targ == nullptr) {
503 		targ = conf->add_controller(nqn);
504 		if (targ == nullptr) {
505 			log_warnx("Failed to add controller \"%s\"", nqn);
506 			return;
507 		}
508 	}
509 
510 	const char *tg_name = port.ctld_transport_group_name.c_str();
511 	struct portal_group *pg = conf->find_transport_group(tg_name);
512 	if (pg == nullptr) {
513 		pg = conf->add_transport_group(tg_name);
514 		if (pg == nullptr) {
515 			log_warnx("Failed to add transport-group \"%s\"",
516 			    tg_name);
517 			return;
518 		}
519 
520 		pg->set_kernel();
521 	}
522 	pg->set_tag(port.portid);
523 	if (!conf->add_port(targ, pg, port.port_id)) {
524 		log_warnx("Failed to add port for controller \"%s\" and transport-group \"%s\"",
525 		    nqn, tg_name);
526 	}
527 }
528 
529 static void
530 add_kernel_port(struct kports &kports, const struct cctl_port &port,
531     std::string &name)
532 {
533 	log_debugx("CTL kernel port %u \"%s\"", port.port_id, name.c_str());
534 	if (kports.has_port(name)) {
535 		log_warnx("Ignoring duplicate kernel port \"%s\"",
536 		    name.c_str());
537 		return;
538 	}
539 
540 	if (!kports.add_port(name, port.port_id)) {
541 		log_warnx("kports::add_port failed");
542 	}
543 }
544 
545 conf_up
546 conf_new_from_kernel(struct kports &kports)
547 {
548 	struct cctl_devlist_data devlist;
549 
550 	log_debugx("obtaining previously configured CTL luns from the kernel");
551 
552 	if (!parse_kernel_config(devlist))
553 		return {};
554 
555 	conf_up conf = std::make_unique<struct conf>();
556 
557 	for (const auto &port : devlist.port_list) {
558 		if (port.port_frontend == "ha")
559 			continue;
560 
561 		std::string name = port.port_name;
562 		if (port.port_frontend == "ioctl")
563 			name += "/" + std::to_string(port.pp) + "/" +
564 			    std::to_string(port.vp);
565 		else if (port.pp != 0 || port.vp != 0) {
566 			name += "/" + std::to_string(port.pp);
567 			if (port.vp != 0)
568 				name += "/" + std::to_string(port.vp);
569 		}
570 
571 		if (port.port_frontend == "iscsi") {
572 			add_iscsi_port(conf.get(), port, name);
573 		} else if (port.port_frontend == "nvmf") {
574 			add_nvmf_port(conf.get(), port, name);
575 		} else {
576 			add_kernel_port(kports, port, name);
577 		}
578 	}
579 
580 	for (const auto &lun : devlist.lun_list) {
581 		if (lun.ctld_name.empty()) {
582 			log_debugx("CTL lun %ju is not managed by ctld; "
583 			    "ignoring", (uintmax_t)lun.lun_id);
584 			continue;
585 		}
586 
587 		const char *l_name = lun.ctld_name.c_str();
588 		struct lun *cl = conf->find_lun(l_name);
589 		if (cl != NULL) {
590 			log_warnx("found CTL lun %ju \"%s\", "
591 			    "also backed by CTL lun %d; ignoring",
592 			    (uintmax_t)lun.lun_id, l_name,
593 			    cl->ctl_lun());
594 			continue;
595 		}
596 
597 		log_debugx("found CTL lun %ju \"%s\"",
598 		    (uintmax_t)lun.lun_id, l_name);
599 
600 		cl = conf->add_lun(l_name);
601 		if (cl == NULL) {
602 			log_warnx("lun_new failed");
603 			continue;
604 		}
605 		cl->set_backend(lun.backend_type.c_str());
606 		cl->set_device_type(lun.device_type);
607 		cl->set_blocksize(lun.blocksize);
608 		cl->set_device_id(lun.device_id.c_str());
609 		cl->set_serial(lun.serial_number.c_str());
610 		cl->set_size(lun.size_blocks * lun.blocksize);
611 		cl->set_ctl_lun(lun.lun_id);
612 
613 		for (const auto &pair : lun.attr_list) {
614 			const char *key = pair.first.c_str();
615 			const char *value = pair.second.c_str();
616 			if (pair.first == "file" || pair.first == "dev") {
617 				cl->set_path(value);
618 				continue;
619 			}
620 			if (!cl->add_option(key, value))
621 				log_warnx("unable to add CTL lun option "
622 				    "%s for CTL lun %ju \"%s\"",
623 				    key, (uintmax_t)lun.lun_id,
624 				    cl->name());
625 		}
626 	}
627 
628 	return (conf);
629 }
630 
631 bool
632 lun::kernel_add()
633 {
634 	struct ctl_lun_req req;
635 	int error;
636 
637 	bzero(&req, sizeof(req));
638 
639 	strlcpy(req.backend, l_backend.c_str(), sizeof(req.backend));
640 	req.reqtype = CTL_LUNREQ_CREATE;
641 
642 	req.reqdata.create.blocksize_bytes = l_blocksize;
643 
644 	if (l_size != 0)
645 		req.reqdata.create.lun_size_bytes = l_size;
646 
647 	if (l_ctl_lun >= 0) {
648 		req.reqdata.create.req_lun_id = l_ctl_lun;
649 		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
650 	}
651 
652 	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
653 	req.reqdata.create.device_type = l_device_type;
654 
655 	if (!l_serial.empty()) {
656 		strncpy((char *)req.reqdata.create.serial_num, l_serial.c_str(),
657 			sizeof(req.reqdata.create.serial_num));
658 		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
659 	}
660 
661 	if (!l_device_id.empty()) {
662 		strncpy((char *)req.reqdata.create.device_id,
663 		    l_device_id.c_str(), sizeof(req.reqdata.create.device_id));
664 		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
665 	}
666 
667 	freebsd::nvlist_up nvl = options();
668 	req.args = nvlist_pack(nvl.get(), &req.args_len);
669 	if (req.args == NULL) {
670 		log_warn("error packing nvlist");
671 		return (false);
672 	}
673 
674 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
675 	free(req.args);
676 
677 	if (error != 0) {
678 		log_warn("error issuing CTL_LUN_REQ ioctl");
679 		return (false);
680 	}
681 
682 	switch (req.status) {
683 	case CTL_LUN_ERROR:
684 		log_warnx("LUN creation error: %s", req.error_str);
685 		return (false);
686 	case CTL_LUN_WARNING:
687 		log_warnx("LUN creation warning: %s", req.error_str);
688 		break;
689 	case CTL_LUN_OK:
690 		break;
691 	default:
692 		log_warnx("unknown LUN creation status: %d",
693 		    req.status);
694 		return (false);
695 	}
696 
697 	l_ctl_lun = req.reqdata.create.req_lun_id;
698 	return (true);
699 }
700 
701 bool
702 lun::kernel_modify() const
703 {
704 	struct ctl_lun_req req;
705 	int error;
706 
707 	bzero(&req, sizeof(req));
708 
709 	strlcpy(req.backend, l_backend.c_str(), sizeof(req.backend));
710 	req.reqtype = CTL_LUNREQ_MODIFY;
711 
712 	req.reqdata.modify.lun_id = l_ctl_lun;
713 	req.reqdata.modify.lun_size_bytes = l_size;
714 
715 	freebsd::nvlist_up nvl = options();
716 	req.args = nvlist_pack(nvl.get(), &req.args_len);
717 	if (req.args == NULL) {
718 		log_warn("error packing nvlist");
719 		return (false);
720 	}
721 
722 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
723 	free(req.args);
724 
725 	if (error != 0) {
726 		log_warn("error issuing CTL_LUN_REQ ioctl");
727 		return (false);
728 	}
729 
730 	switch (req.status) {
731 	case CTL_LUN_ERROR:
732 		log_warnx("LUN modification error: %s", req.error_str);
733 		return (false);
734 	case CTL_LUN_WARNING:
735 		log_warnx("LUN modification warning: %s", req.error_str);
736 		break;
737 	case CTL_LUN_OK:
738 		break;
739 	default:
740 		log_warnx("unknown LUN modification status: %d",
741 		    req.status);
742 		return (false);
743 	}
744 
745 	return (true);
746 }
747 
748 bool
749 lun::kernel_remove() const
750 {
751 	struct ctl_lun_req req;
752 
753 	bzero(&req, sizeof(req));
754 
755 	strlcpy(req.backend, l_backend.c_str(), sizeof(req.backend));
756 	req.reqtype = CTL_LUNREQ_RM;
757 
758 	req.reqdata.rm.lun_id = l_ctl_lun;
759 
760 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
761 		log_warn("error issuing CTL_LUN_REQ ioctl");
762 		return (false);
763 	}
764 
765 	switch (req.status) {
766 	case CTL_LUN_ERROR:
767 		log_warnx("LUN removal error: %s", req.error_str);
768 		return (false);
769 	case CTL_LUN_WARNING:
770 		log_warnx("LUN removal warning: %s", req.error_str);
771 		break;
772 	case CTL_LUN_OK:
773 		break;
774 	default:
775 		log_warnx("unknown LUN removal status: %d", req.status);
776 		return (false);
777 	}
778 
779 	return (true);
780 }
781 
782 bool
783 ctl_create_port(const char *driver, const nvlist_t *nvl, uint32_t *ctl_port)
784 {
785 	struct ctl_req req;
786 	char result_buf[NVLIST_BUFSIZE];
787 	int error;
788 
789 	bzero(&req, sizeof(req));
790 	req.reqtype = CTL_REQ_CREATE;
791 
792 	strlcpy(req.driver, driver, sizeof(req.driver));
793 	req.args = nvlist_pack(nvl, &req.args_len);
794 	if (req.args == NULL) {
795 		log_warn("error packing nvlist");
796 		return (false);
797 	}
798 
799 	req.result = result_buf;
800 	req.result_len = sizeof(result_buf);
801 	error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
802 	free(req.args);
803 
804 	if (error != 0) {
805 		log_warn("error issuing CTL_PORT_REQ ioctl");
806 		return (false);
807 	}
808 	if (req.status == CTL_LUN_ERROR) {
809 		log_warnx("error returned from port creation request: %s",
810 		    req.error_str);
811 		return (false);
812 	}
813 	if (req.status != CTL_LUN_OK) {
814 		log_warnx("unknown port creation request status %d",
815 		    req.status);
816 		return (false);
817 	}
818 
819 	freebsd::nvlist_up result_nvl(nvlist_unpack(result_buf, req.result_len,
820 	    0));
821 	if (result_nvl == NULL) {
822 		log_warnx("error unpacking result nvlist");
823 		return (false);
824 	}
825 
826 	*ctl_port = nvlist_get_number(result_nvl.get(), "port_id");
827 	return (true);
828 }
829 
830 bool
831 ioctl_port::kernel_create_port()
832 {
833 	freebsd::nvlist_up nvl(nvlist_create(0));
834 	nvlist_add_stringf(nvl.get(), "pp", "%d", p_ioctl_pp);
835 	nvlist_add_stringf(nvl.get(), "vp", "%d", p_ioctl_vp);
836 
837 	return (ctl_create_port("ioctl", nvl.get(), &p_ctl_port));
838 }
839 
840 bool
841 kernel_port::kernel_create_port()
842 {
843 	struct ctl_port_entry entry;
844 	struct target *targ = p_target;
845 
846 	p_ctl_port = p_pport->ctl_port();
847 
848 	if (strncmp(targ->name(), "naa.", 4) == 0 &&
849 	    strlen(targ->name()) == 20) {
850 		bzero(&entry, sizeof(entry));
851 		entry.port_type = CTL_PORT_NONE;
852 		entry.targ_port = p_ctl_port;
853 		entry.flags |= CTL_PORT_WWNN_VALID;
854 		entry.wwnn = strtoull(targ->name() + 4, NULL, 16);
855 		if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
856 			log_warn("CTL_SET_PORT_WWNS ioctl failed");
857 	}
858 	return (true);
859 }
860 
861 bool
862 port::kernel_add()
863 {
864 	struct ctl_port_entry entry;
865 	struct ctl_lun_map lm;
866 	struct target *targ = p_target;
867 	int error, i;
868 
869 	if (!kernel_create_port())
870 		return (false);
871 
872 	/* Explicitly enable mapping to block any access except allowed. */
873 	lm.port = p_ctl_port;
874 	lm.plun = UINT32_MAX;
875 	lm.lun = 0;
876 	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
877 	if (error != 0)
878 		log_warn("CTL_LUN_MAP ioctl failed");
879 
880 	/* Map configured LUNs */
881 	for (i = 0; i < MAX_LUNS; i++) {
882 		if (targ->lun(i) == nullptr)
883 			continue;
884 		lm.port = p_ctl_port;
885 		lm.plun = i;
886 		lm.lun = targ->lun(i)->ctl_lun();
887 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
888 		if (error != 0)
889 			log_warn("CTL_LUN_MAP ioctl failed");
890 	}
891 
892 	/* Enable port */
893 	bzero(&entry, sizeof(entry));
894 	entry.targ_port = p_ctl_port;
895 	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
896 	if (error != 0) {
897 		log_warn("CTL_ENABLE_PORT ioctl failed");
898 		return (false);
899 	}
900 
901 	return (true);
902 }
903 
904 bool
905 port::kernel_update(const struct port *oport)
906 {
907 	struct ctl_lun_map lm;
908 	struct target *targ = p_target;
909 	struct target *otarg = oport->p_target;
910 	int error, i;
911 	uint32_t olun;
912 
913 	p_ctl_port = oport->p_ctl_port;
914 
915 	/* Map configured LUNs and unmap others */
916 	for (i = 0; i < MAX_LUNS; i++) {
917 		lm.port = p_ctl_port;
918 		lm.plun = i;
919 		if (targ->lun(i) == nullptr)
920 			lm.lun = UINT32_MAX;
921 		else
922 			lm.lun = targ->lun(i)->ctl_lun();
923 		if (otarg->lun(i) == nullptr)
924 			olun = UINT32_MAX;
925 		else
926 			olun = otarg->lun(i)->ctl_lun();
927 		if (lm.lun == olun)
928 			continue;
929 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
930 		if (error != 0)
931 			log_warn("CTL_LUN_MAP ioctl failed");
932 	}
933 	return (true);
934 }
935 
936 bool
937 ctl_remove_port(const char *driver, nvlist_t *nvl)
938 {
939 	struct ctl_req req;
940 	int error;
941 
942 	strlcpy(req.driver, driver, sizeof(req.driver));
943 	req.reqtype = CTL_REQ_REMOVE;
944 	req.args = nvlist_pack(nvl, &req.args_len);
945 	if (req.args == NULL) {
946 		log_warn("error packing nvlist");
947 		return (false);
948 	}
949 
950 	error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
951 	free(req.args);
952 
953 	if (error != 0) {
954 		log_warn("error issuing CTL_PORT_REQ ioctl");
955 		return (false);
956 	}
957 	if (req.status == CTL_LUN_ERROR) {
958 		log_warnx("error returned from port removal request: %s",
959 		    req.error_str);
960 		return (false);
961 	}
962 	if (req.status != CTL_LUN_OK) {
963 		log_warnx("unknown port removal request status %d", req.status);
964 		return (false);
965 	}
966 	return (true);
967 }
968 
969 bool
970 ioctl_port::kernel_remove_port()
971 {
972 	freebsd::nvlist_up nvl(nvlist_create(0));
973 	nvlist_add_stringf(nvl.get(), "port_id", "%d", p_ctl_port);
974 
975 	return (ctl_remove_port("ioctl", nvl.get()));
976 }
977 
978 bool
979 kernel_port::kernel_remove_port()
980 {
981 	struct ctl_lun_map lm;
982 	int error;
983 
984 	/* Disable LUN mapping. */
985 	lm.port = p_ctl_port;
986 	lm.plun = UINT32_MAX;
987 	lm.lun = UINT32_MAX;
988 	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
989 	if (error != 0)
990 		log_warn("CTL_LUN_MAP ioctl failed");
991 	return (true);
992 }
993 
994 bool
995 port::kernel_remove()
996 {
997 	struct ctl_port_entry entry;
998 	int error;
999 
1000 	/* Disable port */
1001 	bzero(&entry, sizeof(entry));
1002 	entry.targ_port = p_ctl_port;
1003 	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1004 	if (error != 0) {
1005 		log_warn("CTL_DISABLE_PORT ioctl failed");
1006 		return (false);
1007 	}
1008 
1009 	return (kernel_remove_port());
1010 }
1011 
1012 #ifdef ICL_KERNEL_PROXY
1013 void
1014 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1015 {
1016 	struct ctl_iscsi req;
1017 
1018 	bzero(&req, sizeof(req));
1019 
1020 	req.type = CTL_ISCSI_LISTEN;
1021 	req.data.listen.iser = iser;
1022 	req.data.listen.domain = ai->ai_family;
1023 	req.data.listen.socktype = ai->ai_socktype;
1024 	req.data.listen.protocol = ai->ai_protocol;
1025 	req.data.listen.addr = ai->ai_addr;
1026 	req.data.listen.addrlen = ai->ai_addrlen;
1027 	req.data.listen.portal_id = portal_id;
1028 
1029 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1030 		log_err(1, "error issuing CTL_ISCSI ioctl");
1031 
1032 	if (req.status != CTL_ISCSI_OK) {
1033 		log_errx(1, "error returned from CTL iSCSI listen: %s",
1034 		    req.error_str);
1035 	}
1036 }
1037 
1038 void
1039 kernel_accept(int *connection_id, int *portal_id,
1040     struct sockaddr *client_sa, socklen_t *client_salen)
1041 {
1042 	struct ctl_iscsi req;
1043 	struct sockaddr_storage ss;
1044 
1045 	bzero(&req, sizeof(req));
1046 
1047 	req.type = CTL_ISCSI_ACCEPT;
1048 	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1049 
1050 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1051 		log_err(1, "error issuing CTL_ISCSI ioctl");
1052 
1053 	if (req.status != CTL_ISCSI_OK) {
1054 		log_errx(1, "error returned from CTL iSCSI accept: %s",
1055 		    req.error_str);
1056 	}
1057 
1058 	*connection_id = req.data.accept.connection_id;
1059 	*portal_id = req.data.accept.portal_id;
1060 	*client_salen = req.data.accept.initiator_addrlen;
1061 	memcpy(client_sa, &ss, *client_salen);
1062 }
1063 
1064 void
1065 kernel_send(struct pdu *pdu)
1066 {
1067 	struct ctl_iscsi req;
1068 
1069 	bzero(&req, sizeof(req));
1070 
1071 	req.type = CTL_ISCSI_SEND;
1072 	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1073 	req.data.send.bhs = pdu->pdu_bhs;
1074 	req.data.send.data_segment_len = pdu->pdu_data_len;
1075 	req.data.send.data_segment = pdu->pdu_data;
1076 
1077 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1078 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1079 		    "dropping connection");
1080 	}
1081 
1082 	if (req.status != CTL_ISCSI_OK) {
1083 		log_errx(1, "error returned from CTL iSCSI send: "
1084 		    "%s; dropping connection", req.error_str);
1085 	}
1086 }
1087 
1088 void
1089 kernel_receive(struct pdu *pdu)
1090 {
1091 	struct connection *conn;
1092 	struct ctl_iscsi req;
1093 
1094 	conn = pdu->pdu_connection;
1095 	pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1096 	if (pdu->pdu_data == NULL)
1097 		log_err(1, "malloc");
1098 
1099 	bzero(&req, sizeof(req));
1100 
1101 	req.type = CTL_ISCSI_RECEIVE;
1102 	req.data.receive.connection_id = conn->conn_socket;
1103 	req.data.receive.bhs = pdu->pdu_bhs;
1104 	req.data.receive.data_segment_len =
1105 	    conn->conn_max_recv_data_segment_length;
1106 	req.data.receive.data_segment = pdu->pdu_data;
1107 
1108 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1109 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1110 		    "dropping connection");
1111 	}
1112 
1113 	if (req.status != CTL_ISCSI_OK) {
1114 		log_errx(1, "error returned from CTL iSCSI receive: "
1115 		    "%s; dropping connection", req.error_str);
1116 	}
1117 
1118 }
1119 
1120 #endif /* ICL_KERNEL_PROXY */
1121 
1122 /*
1123  * XXX: I CANT INTO LATIN
1124  */
1125 void
1126 kernel_capsicate(void)
1127 {
1128 	cap_rights_t rights;
1129 	const unsigned long cmds[] = { CTL_ISCSI, CTL_NVMF };
1130 
1131 	cap_rights_init(&rights, CAP_IOCTL);
1132 	if (caph_rights_limit(ctl_fd, &rights) < 0)
1133 		log_err(1, "cap_rights_limit");
1134 
1135 	if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0)
1136 		log_err(1, "cap_ioctls_limit");
1137 
1138 	if (caph_enter() < 0)
1139 		log_err(1, "cap_enter");
1140 
1141 	if (cap_sandboxed())
1142 		log_debugx("Capsicum capability mode enabled");
1143 	else
1144 		log_warnx("Capsicum capability mode not supported");
1145 }
1146 
1147