xref: /freebsd/usr.sbin/ctld/kernel.cc (revision d15f2551b25f79ddcbe289faa95e655100b952da)
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.add_port(name, port.port_id)) {
535 		log_warnx("Ignoring duplicate kernel port \"%s\"",
536 		    name.c_str());
537 	}
538 }
539 
540 conf_up
541 conf_new_from_kernel(struct kports &kports)
542 {
543 	struct cctl_devlist_data devlist;
544 
545 	log_debugx("obtaining previously configured CTL luns from the kernel");
546 
547 	if (!parse_kernel_config(devlist))
548 		return {};
549 
550 	conf_up conf = std::make_unique<struct conf>();
551 
552 	for (const auto &port : devlist.port_list) {
553 		if (port.port_frontend == "ha")
554 			continue;
555 
556 		std::string name = port.port_name;
557 		if (port.pp != 0 || port.vp != 0) {
558 			name += "/" + std::to_string(port.pp);
559 			if (port.vp != 0)
560 				name += "/" + std::to_string(port.vp);
561 		}
562 
563 		if (port.port_frontend == "iscsi") {
564 			add_iscsi_port(conf.get(), port, name);
565 		} else if (port.port_frontend == "nvmf") {
566 			add_nvmf_port(conf.get(), port, name);
567 		} else {
568 			add_kernel_port(kports, port, name);
569 		}
570 	}
571 
572 	for (const auto &lun : devlist.lun_list) {
573 		if (lun.ctld_name.empty()) {
574 			log_debugx("CTL lun %ju is not managed by ctld; "
575 			    "ignoring", (uintmax_t)lun.lun_id);
576 			continue;
577 		}
578 
579 		const char *l_name = lun.ctld_name.c_str();
580 		struct lun *cl = conf->find_lun(l_name);
581 		if (cl != NULL) {
582 			log_warnx("found CTL lun %ju \"%s\", "
583 			    "also backed by CTL lun %d; ignoring",
584 			    (uintmax_t)lun.lun_id, l_name,
585 			    cl->ctl_lun());
586 			continue;
587 		}
588 
589 		log_debugx("found CTL lun %ju \"%s\"",
590 		    (uintmax_t)lun.lun_id, l_name);
591 
592 		cl = conf->add_lun(l_name);
593 		if (cl == NULL) {
594 			log_warnx("lun_new failed");
595 			continue;
596 		}
597 		cl->set_backend(lun.backend_type.c_str());
598 		cl->set_device_type(lun.device_type);
599 		cl->set_blocksize(lun.blocksize);
600 		cl->set_device_id(lun.device_id.c_str());
601 		cl->set_serial(lun.serial_number.c_str());
602 		cl->set_size(lun.size_blocks * lun.blocksize);
603 		cl->set_ctl_lun(lun.lun_id);
604 
605 		for (const auto &pair : lun.attr_list) {
606 			const char *key = pair.first.c_str();
607 			const char *value = pair.second.c_str();
608 			if (pair.first == "file" || pair.first == "dev") {
609 				cl->set_path(value);
610 				continue;
611 			}
612 			if (!cl->add_option(key, value))
613 				log_warnx("unable to add CTL lun option "
614 				    "%s for CTL lun %ju \"%s\"",
615 				    key, (uintmax_t)lun.lun_id,
616 				    cl->name());
617 		}
618 	}
619 
620 	return (conf);
621 }
622 
623 bool
624 lun::kernel_add()
625 {
626 	struct ctl_lun_req req;
627 	int error;
628 
629 	bzero(&req, sizeof(req));
630 
631 	strlcpy(req.backend, l_backend.c_str(), sizeof(req.backend));
632 	req.reqtype = CTL_LUNREQ_CREATE;
633 
634 	req.reqdata.create.blocksize_bytes = l_blocksize;
635 
636 	if (l_size != 0)
637 		req.reqdata.create.lun_size_bytes = l_size;
638 
639 	if (l_ctl_lun >= 0) {
640 		req.reqdata.create.req_lun_id = l_ctl_lun;
641 		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
642 	}
643 
644 	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
645 	req.reqdata.create.device_type = l_device_type;
646 
647 	if (!l_serial.empty()) {
648 		strncpy((char *)req.reqdata.create.serial_num, l_serial.c_str(),
649 			sizeof(req.reqdata.create.serial_num));
650 		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
651 	}
652 
653 	if (!l_device_id.empty()) {
654 		strncpy((char *)req.reqdata.create.device_id,
655 		    l_device_id.c_str(), sizeof(req.reqdata.create.device_id));
656 		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
657 	}
658 
659 	freebsd::nvlist_up nvl = options();
660 	req.args = nvlist_pack(nvl.get(), &req.args_len);
661 	if (req.args == NULL) {
662 		log_warn("error packing nvlist");
663 		return (false);
664 	}
665 
666 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
667 	free(req.args);
668 
669 	if (error != 0) {
670 		log_warn("error issuing CTL_LUN_REQ ioctl");
671 		return (false);
672 	}
673 
674 	switch (req.status) {
675 	case CTL_LUN_ERROR:
676 		log_warnx("LUN creation error: %s", req.error_str);
677 		return (false);
678 	case CTL_LUN_WARNING:
679 		log_warnx("LUN creation warning: %s", req.error_str);
680 		break;
681 	case CTL_LUN_OK:
682 		break;
683 	default:
684 		log_warnx("unknown LUN creation status: %d",
685 		    req.status);
686 		return (false);
687 	}
688 
689 	l_ctl_lun = req.reqdata.create.req_lun_id;
690 	return (true);
691 }
692 
693 bool
694 lun::kernel_modify() const
695 {
696 	struct ctl_lun_req req;
697 	int error;
698 
699 	bzero(&req, sizeof(req));
700 
701 	strlcpy(req.backend, l_backend.c_str(), sizeof(req.backend));
702 	req.reqtype = CTL_LUNREQ_MODIFY;
703 
704 	req.reqdata.modify.lun_id = l_ctl_lun;
705 	req.reqdata.modify.lun_size_bytes = l_size;
706 
707 	freebsd::nvlist_up nvl = options();
708 	req.args = nvlist_pack(nvl.get(), &req.args_len);
709 	if (req.args == NULL) {
710 		log_warn("error packing nvlist");
711 		return (false);
712 	}
713 
714 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
715 	free(req.args);
716 
717 	if (error != 0) {
718 		log_warn("error issuing CTL_LUN_REQ ioctl");
719 		return (false);
720 	}
721 
722 	switch (req.status) {
723 	case CTL_LUN_ERROR:
724 		log_warnx("LUN modification error: %s", req.error_str);
725 		return (false);
726 	case CTL_LUN_WARNING:
727 		log_warnx("LUN modification warning: %s", req.error_str);
728 		break;
729 	case CTL_LUN_OK:
730 		break;
731 	default:
732 		log_warnx("unknown LUN modification status: %d",
733 		    req.status);
734 		return (false);
735 	}
736 
737 	return (true);
738 }
739 
740 bool
741 lun::kernel_remove() const
742 {
743 	struct ctl_lun_req req;
744 
745 	bzero(&req, sizeof(req));
746 
747 	strlcpy(req.backend, l_backend.c_str(), sizeof(req.backend));
748 	req.reqtype = CTL_LUNREQ_RM;
749 
750 	req.reqdata.rm.lun_id = l_ctl_lun;
751 
752 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
753 		log_warn("error issuing CTL_LUN_REQ ioctl");
754 		return (false);
755 	}
756 
757 	switch (req.status) {
758 	case CTL_LUN_ERROR:
759 		log_warnx("LUN removal error: %s", req.error_str);
760 		return (false);
761 	case CTL_LUN_WARNING:
762 		log_warnx("LUN removal warning: %s", req.error_str);
763 		break;
764 	case CTL_LUN_OK:
765 		break;
766 	default:
767 		log_warnx("unknown LUN removal status: %d", req.status);
768 		return (false);
769 	}
770 
771 	return (true);
772 }
773 
774 bool
775 ctl_create_port(const char *driver, const nvlist_t *nvl, uint32_t *ctl_port)
776 {
777 	struct ctl_req req;
778 	char result_buf[NVLIST_BUFSIZE];
779 	int error;
780 
781 	bzero(&req, sizeof(req));
782 	req.reqtype = CTL_REQ_CREATE;
783 
784 	strlcpy(req.driver, driver, sizeof(req.driver));
785 	req.args = nvlist_pack(nvl, &req.args_len);
786 	if (req.args == NULL) {
787 		log_warn("error packing nvlist");
788 		return (false);
789 	}
790 
791 	req.result = result_buf;
792 	req.result_len = sizeof(result_buf);
793 	error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
794 	free(req.args);
795 
796 	if (error != 0) {
797 		log_warn("error issuing CTL_PORT_REQ ioctl");
798 		return (false);
799 	}
800 	if (req.status == CTL_LUN_ERROR) {
801 		log_warnx("error returned from port creation request: %s",
802 		    req.error_str);
803 		return (false);
804 	}
805 	if (req.status != CTL_LUN_OK) {
806 		log_warnx("unknown port creation request status %d",
807 		    req.status);
808 		return (false);
809 	}
810 
811 	freebsd::nvlist_up result_nvl(nvlist_unpack(result_buf, req.result_len,
812 	    0));
813 	if (result_nvl == NULL) {
814 		log_warnx("error unpacking result nvlist");
815 		return (false);
816 	}
817 
818 	*ctl_port = nvlist_get_number(result_nvl.get(), "port_id");
819 	return (true);
820 }
821 
822 bool
823 ioctl_port::kernel_create_port()
824 {
825 	freebsd::nvlist_up nvl(nvlist_create(0));
826 	nvlist_add_stringf(nvl.get(), "pp", "%d", p_ioctl_pp);
827 	nvlist_add_stringf(nvl.get(), "vp", "%d", p_ioctl_vp);
828 
829 	return (ctl_create_port("ioctl", nvl.get(), &p_ctl_port));
830 }
831 
832 bool
833 kernel_port::kernel_create_port()
834 {
835 	struct ctl_port_entry entry;
836 	struct target *targ = p_target;
837 
838 	p_ctl_port = p_pport->ctl_port();
839 
840 	if (strncmp(targ->name(), "naa.", 4) == 0 &&
841 	    strlen(targ->name()) == 20) {
842 		bzero(&entry, sizeof(entry));
843 		entry.port_type = CTL_PORT_NONE;
844 		entry.targ_port = p_ctl_port;
845 		entry.flags |= CTL_PORT_WWNN_VALID;
846 		entry.wwnn = strtoull(targ->name() + 4, NULL, 16);
847 		if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
848 			log_warn("CTL_SET_PORT_WWNS ioctl failed");
849 	}
850 	return (true);
851 }
852 
853 bool
854 port::kernel_add()
855 {
856 	struct ctl_port_entry entry;
857 	struct ctl_lun_map lm;
858 	struct target *targ = p_target;
859 	int error, i;
860 
861 	if (!kernel_create_port())
862 		return (false);
863 
864 	/* Explicitly enable mapping to block any access except allowed. */
865 	lm.port = p_ctl_port;
866 	lm.plun = UINT32_MAX;
867 	lm.lun = 0;
868 	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
869 	if (error != 0)
870 		log_warn("CTL_LUN_MAP ioctl failed");
871 
872 	/* Map configured LUNs */
873 	for (i = 0; i < MAX_LUNS; i++) {
874 		if (targ->lun(i) == nullptr)
875 			continue;
876 		lm.port = p_ctl_port;
877 		lm.plun = i;
878 		lm.lun = targ->lun(i)->ctl_lun();
879 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
880 		if (error != 0)
881 			log_warn("CTL_LUN_MAP ioctl failed");
882 	}
883 
884 	/* Enable port */
885 	bzero(&entry, sizeof(entry));
886 	entry.targ_port = p_ctl_port;
887 	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
888 	if (error != 0) {
889 		log_warn("CTL_ENABLE_PORT ioctl failed");
890 		return (false);
891 	}
892 
893 	return (true);
894 }
895 
896 bool
897 port::kernel_update(const struct port *oport)
898 {
899 	struct ctl_lun_map lm;
900 	struct target *targ = p_target;
901 	struct target *otarg = oport->p_target;
902 	int error, i;
903 	uint32_t olun;
904 
905 	p_ctl_port = oport->p_ctl_port;
906 
907 	/* Map configured LUNs and unmap others */
908 	for (i = 0; i < MAX_LUNS; i++) {
909 		lm.port = p_ctl_port;
910 		lm.plun = i;
911 		if (targ->lun(i) == nullptr)
912 			lm.lun = UINT32_MAX;
913 		else
914 			lm.lun = targ->lun(i)->ctl_lun();
915 		if (otarg->lun(i) == nullptr)
916 			olun = UINT32_MAX;
917 		else
918 			olun = otarg->lun(i)->ctl_lun();
919 		if (lm.lun == olun)
920 			continue;
921 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
922 		if (error != 0)
923 			log_warn("CTL_LUN_MAP ioctl failed");
924 	}
925 	return (true);
926 }
927 
928 bool
929 ctl_remove_port(const char *driver, nvlist_t *nvl)
930 {
931 	struct ctl_req req;
932 	int error;
933 
934 	strlcpy(req.driver, driver, sizeof(req.driver));
935 	req.reqtype = CTL_REQ_REMOVE;
936 	req.args = nvlist_pack(nvl, &req.args_len);
937 	if (req.args == NULL) {
938 		log_warn("error packing nvlist");
939 		return (false);
940 	}
941 
942 	error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
943 	free(req.args);
944 
945 	if (error != 0) {
946 		log_warn("error issuing CTL_PORT_REQ ioctl");
947 		return (false);
948 	}
949 	if (req.status == CTL_LUN_ERROR) {
950 		log_warnx("error returned from port removal request: %s",
951 		    req.error_str);
952 		return (false);
953 	}
954 	if (req.status != CTL_LUN_OK) {
955 		log_warnx("unknown port removal request status %d", req.status);
956 		return (false);
957 	}
958 	return (true);
959 }
960 
961 bool
962 ioctl_port::kernel_remove_port()
963 {
964 	freebsd::nvlist_up nvl(nvlist_create(0));
965 	nvlist_add_stringf(nvl.get(), "port_id", "%d", p_ctl_port);
966 
967 	return (ctl_remove_port("ioctl", nvl.get()));
968 }
969 
970 bool
971 kernel_port::kernel_remove_port()
972 {
973 	struct ctl_lun_map lm;
974 	int error;
975 
976 	/* Disable LUN mapping. */
977 	lm.port = p_ctl_port;
978 	lm.plun = UINT32_MAX;
979 	lm.lun = UINT32_MAX;
980 	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
981 	if (error != 0)
982 		log_warn("CTL_LUN_MAP ioctl failed");
983 	return (true);
984 }
985 
986 bool
987 port::kernel_remove()
988 {
989 	struct ctl_port_entry entry;
990 	int error;
991 
992 	/* Disable port */
993 	bzero(&entry, sizeof(entry));
994 	entry.targ_port = p_ctl_port;
995 	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
996 	if (error != 0) {
997 		log_warn("CTL_DISABLE_PORT ioctl failed");
998 		return (false);
999 	}
1000 
1001 	return (kernel_remove_port());
1002 }
1003 
1004 #ifdef ICL_KERNEL_PROXY
1005 void
1006 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1007 {
1008 	struct ctl_iscsi req;
1009 
1010 	bzero(&req, sizeof(req));
1011 
1012 	req.type = CTL_ISCSI_LISTEN;
1013 	req.data.listen.iser = iser;
1014 	req.data.listen.domain = ai->ai_family;
1015 	req.data.listen.socktype = ai->ai_socktype;
1016 	req.data.listen.protocol = ai->ai_protocol;
1017 	req.data.listen.addr = ai->ai_addr;
1018 	req.data.listen.addrlen = ai->ai_addrlen;
1019 	req.data.listen.portal_id = portal_id;
1020 
1021 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1022 		log_err(1, "error issuing CTL_ISCSI ioctl");
1023 
1024 	if (req.status != CTL_ISCSI_OK) {
1025 		log_errx(1, "error returned from CTL iSCSI listen: %s",
1026 		    req.error_str);
1027 	}
1028 }
1029 
1030 void
1031 kernel_accept(int *connection_id, int *portal_id,
1032     struct sockaddr *client_sa, socklen_t *client_salen)
1033 {
1034 	struct ctl_iscsi req;
1035 	struct sockaddr_storage ss;
1036 
1037 	bzero(&req, sizeof(req));
1038 
1039 	req.type = CTL_ISCSI_ACCEPT;
1040 	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1041 
1042 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1043 		log_err(1, "error issuing CTL_ISCSI ioctl");
1044 
1045 	if (req.status != CTL_ISCSI_OK) {
1046 		log_errx(1, "error returned from CTL iSCSI accept: %s",
1047 		    req.error_str);
1048 	}
1049 
1050 	*connection_id = req.data.accept.connection_id;
1051 	*portal_id = req.data.accept.portal_id;
1052 	*client_salen = req.data.accept.initiator_addrlen;
1053 	memcpy(client_sa, &ss, *client_salen);
1054 }
1055 
1056 void
1057 kernel_send(struct pdu *pdu)
1058 {
1059 	struct ctl_iscsi req;
1060 
1061 	bzero(&req, sizeof(req));
1062 
1063 	req.type = CTL_ISCSI_SEND;
1064 	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1065 	req.data.send.bhs = pdu->pdu_bhs;
1066 	req.data.send.data_segment_len = pdu->pdu_data_len;
1067 	req.data.send.data_segment = pdu->pdu_data;
1068 
1069 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1070 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1071 		    "dropping connection");
1072 	}
1073 
1074 	if (req.status != CTL_ISCSI_OK) {
1075 		log_errx(1, "error returned from CTL iSCSI send: "
1076 		    "%s; dropping connection", req.error_str);
1077 	}
1078 }
1079 
1080 void
1081 kernel_receive(struct pdu *pdu)
1082 {
1083 	struct connection *conn;
1084 	struct ctl_iscsi req;
1085 
1086 	conn = pdu->pdu_connection;
1087 	pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1088 	if (pdu->pdu_data == NULL)
1089 		log_err(1, "malloc");
1090 
1091 	bzero(&req, sizeof(req));
1092 
1093 	req.type = CTL_ISCSI_RECEIVE;
1094 	req.data.receive.connection_id = conn->conn_socket;
1095 	req.data.receive.bhs = pdu->pdu_bhs;
1096 	req.data.receive.data_segment_len =
1097 	    conn->conn_max_recv_data_segment_length;
1098 	req.data.receive.data_segment = pdu->pdu_data;
1099 
1100 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1101 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1102 		    "dropping connection");
1103 	}
1104 
1105 	if (req.status != CTL_ISCSI_OK) {
1106 		log_errx(1, "error returned from CTL iSCSI receive: "
1107 		    "%s; dropping connection", req.error_str);
1108 	}
1109 
1110 }
1111 
1112 #endif /* ICL_KERNEL_PROXY */
1113 
1114 /*
1115  * XXX: I CANT INTO LATIN
1116  */
1117 void
1118 kernel_capsicate(void)
1119 {
1120 	cap_rights_t rights;
1121 	const unsigned long cmds[] = { CTL_ISCSI, CTL_NVMF };
1122 
1123 	cap_rights_init(&rights, CAP_IOCTL);
1124 	if (caph_rights_limit(ctl_fd, &rights) < 0)
1125 		log_err(1, "cap_rights_limit");
1126 
1127 	if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0)
1128 		log_err(1, "cap_ioctls_limit");
1129 
1130 	if (caph_enter() < 0)
1131 		log_err(1, "cap_enter");
1132 
1133 	if (cap_sandboxed())
1134 		log_debugx("Capsicum capability mode enabled");
1135 	else
1136 		log_warnx("Capsicum capability mode not supported");
1137 }
1138 
1139