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