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