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