xref: /freebsd/usr.sbin/ctld/kernel.c (revision b78ee15e9f04ae15c3e1200df974473167524d17)
1 /*-
2  * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3  * Copyright (c) 1997-2007 Kenneth D. Merry
4  * Copyright (c) 2012 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Edward Tomasz Napierala
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    substantially similar to the "NO WARRANTY" disclaimer below
18  *    ("Disclaimer") and any redistribution must be conditioned upon
19  *    including a substantially similar Disclaimer requirement for further
20  *    binary redistribution.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/ioctl.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <sys/linker.h>
45 #include <sys/queue.h>
46 #include <sys/callout.h>
47 #include <sys/sbuf.h>
48 #include <sys/capsicum.h>
49 #include <assert.h>
50 #include <bsdxml.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <strings.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_io.h>
63 #include <cam/ctl/ctl_frontend_internal.h>
64 #include <cam/ctl/ctl_backend.h>
65 #include <cam/ctl/ctl_ioctl.h>
66 #include <cam/ctl/ctl_backend_block.h>
67 #include <cam/ctl/ctl_util.h>
68 #include <cam/ctl/ctl_scsi_all.h>
69 
70 #include "ctld.h"
71 
72 #ifdef ICL_KERNEL_PROXY
73 #include <netdb.h>
74 #endif
75 
76 extern bool proxy_mode;
77 
78 static 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 }
97 
98 /*
99  * Name/value pair used for per-LUN attributes.
100  */
101 struct cctl_lun_nv {
102 	char *name;
103 	char *value;
104 	STAILQ_ENTRY(cctl_lun_nv) links;
105 };
106 
107 /*
108  * Backend LUN information.
109  */
110 struct cctl_lun {
111 	uint64_t lun_id;
112 	char *backend_type;
113 	uint64_t size_blocks;
114 	uint32_t blocksize;
115 	char *serial_number;
116 	char *device_id;
117 	char *ctld_name;
118 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
119 	STAILQ_ENTRY(cctl_lun) links;
120 };
121 
122 struct cctl_port {
123 	uint32_t port_id;
124 	char *port_name;
125 	int pp;
126 	int vp;
127 	int cfiscsi_state;
128 	char *cfiscsi_target;
129 	uint16_t cfiscsi_portal_group_tag;
130 	char *ctld_portal_group_name;
131 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
132 	STAILQ_ENTRY(cctl_port) links;
133 };
134 
135 struct cctl_devlist_data {
136 	int num_luns;
137 	STAILQ_HEAD(,cctl_lun) lun_list;
138 	struct cctl_lun *cur_lun;
139 	int num_ports;
140 	STAILQ_HEAD(,cctl_port) port_list;
141 	struct cctl_port *cur_port;
142 	int level;
143 	struct sbuf *cur_sb[32];
144 };
145 
146 static void
147 cctl_start_element(void *user_data, const char *name, const char **attr)
148 {
149 	int i;
150 	struct cctl_devlist_data *devlist;
151 	struct cctl_lun *cur_lun;
152 
153 	devlist = (struct cctl_devlist_data *)user_data;
154 	cur_lun = devlist->cur_lun;
155 	devlist->level++;
156 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
157 	    sizeof(devlist->cur_sb[0])))
158 		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
159 		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
160 
161 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
162 	if (devlist->cur_sb[devlist->level] == NULL)
163 		log_err(1, "%s: unable to allocate sbuf", __func__);
164 
165 	if (strcmp(name, "lun") == 0) {
166 		if (cur_lun != NULL)
167 			log_errx(1, "%s: improper lun element nesting",
168 			    __func__);
169 
170 		cur_lun = calloc(1, sizeof(*cur_lun));
171 		if (cur_lun == NULL)
172 			log_err(1, "%s: cannot allocate %zd bytes", __func__,
173 			    sizeof(*cur_lun));
174 
175 		devlist->num_luns++;
176 		devlist->cur_lun = cur_lun;
177 
178 		STAILQ_INIT(&cur_lun->attr_list);
179 		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
180 
181 		for (i = 0; attr[i] != NULL; i += 2) {
182 			if (strcmp(attr[i], "id") == 0) {
183 				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
184 			} else {
185 				log_errx(1, "%s: invalid LUN attribute %s = %s",
186 				     __func__, attr[i], attr[i+1]);
187 			}
188 		}
189 	}
190 }
191 
192 static void
193 cctl_end_element(void *user_data, const char *name)
194 {
195 	struct cctl_devlist_data *devlist;
196 	struct cctl_lun *cur_lun;
197 	char *str;
198 
199 	devlist = (struct cctl_devlist_data *)user_data;
200 	cur_lun = devlist->cur_lun;
201 
202 	if ((cur_lun == NULL)
203 	 && (strcmp(name, "ctllunlist") != 0))
204 		log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
205 
206 	if (devlist->cur_sb[devlist->level] == NULL)
207 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
208 		     devlist->level, name);
209 
210 	sbuf_finish(devlist->cur_sb[devlist->level]);
211 	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
212 
213 	if (strlen(str) == 0) {
214 		free(str);
215 		str = NULL;
216 	}
217 
218 	sbuf_delete(devlist->cur_sb[devlist->level]);
219 	devlist->cur_sb[devlist->level] = NULL;
220 	devlist->level--;
221 
222 	if (strcmp(name, "backend_type") == 0) {
223 		cur_lun->backend_type = str;
224 		str = NULL;
225 	} else if (strcmp(name, "size") == 0) {
226 		cur_lun->size_blocks = strtoull(str, NULL, 0);
227 	} else if (strcmp(name, "blocksize") == 0) {
228 		cur_lun->blocksize = strtoul(str, NULL, 0);
229 	} else if (strcmp(name, "serial_number") == 0) {
230 		cur_lun->serial_number = str;
231 		str = NULL;
232 	} else if (strcmp(name, "device_id") == 0) {
233 		cur_lun->device_id = str;
234 		str = NULL;
235 	} else if (strcmp(name, "ctld_name") == 0) {
236 		cur_lun->ctld_name = str;
237 		str = NULL;
238 	} else if (strcmp(name, "lun") == 0) {
239 		devlist->cur_lun = NULL;
240 	} else if (strcmp(name, "ctllunlist") == 0) {
241 		/* Nothing. */
242 	} else {
243 		struct cctl_lun_nv *nv;
244 
245 		nv = calloc(1, sizeof(*nv));
246 		if (nv == NULL)
247 			log_err(1, "%s: can't allocate %zd bytes for nv pair",
248 			    __func__, sizeof(*nv));
249 
250 		nv->name = checked_strdup(name);
251 
252 		nv->value = str;
253 		str = NULL;
254 		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
255 	}
256 
257 	free(str);
258 }
259 
260 static void
261 cctl_start_pelement(void *user_data, const char *name, const char **attr)
262 {
263 	int i;
264 	struct cctl_devlist_data *devlist;
265 	struct cctl_port *cur_port;
266 
267 	devlist = (struct cctl_devlist_data *)user_data;
268 	cur_port = devlist->cur_port;
269 	devlist->level++;
270 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
271 	    sizeof(devlist->cur_sb[0])))
272 		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
273 		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
274 
275 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
276 	if (devlist->cur_sb[devlist->level] == NULL)
277 		log_err(1, "%s: unable to allocate sbuf", __func__);
278 
279 	if (strcmp(name, "targ_port") == 0) {
280 		if (cur_port != NULL)
281 			log_errx(1, "%s: improper port element nesting (%s)",
282 			    __func__, name);
283 
284 		cur_port = calloc(1, sizeof(*cur_port));
285 		if (cur_port == NULL)
286 			log_err(1, "%s: cannot allocate %zd bytes", __func__,
287 			    sizeof(*cur_port));
288 
289 		devlist->num_ports++;
290 		devlist->cur_port = cur_port;
291 
292 		STAILQ_INIT(&cur_port->attr_list);
293 		STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
294 
295 		for (i = 0; attr[i] != NULL; i += 2) {
296 			if (strcmp(attr[i], "id") == 0) {
297 				cur_port->port_id = strtoul(attr[i+1], NULL, 0);
298 			} else {
299 				log_errx(1, "%s: invalid LUN attribute %s = %s",
300 				     __func__, attr[i], attr[i+1]);
301 			}
302 		}
303 	}
304 }
305 
306 static void
307 cctl_end_pelement(void *user_data, const char *name)
308 {
309 	struct cctl_devlist_data *devlist;
310 	struct cctl_port *cur_port;
311 	char *str;
312 
313 	devlist = (struct cctl_devlist_data *)user_data;
314 	cur_port = devlist->cur_port;
315 
316 	if ((cur_port == NULL)
317 	 && (strcmp(name, "ctlportlist") != 0))
318 		log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
319 
320 	if (devlist->cur_sb[devlist->level] == NULL)
321 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
322 		     devlist->level, name);
323 
324 	sbuf_finish(devlist->cur_sb[devlist->level]);
325 	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
326 
327 	if (strlen(str) == 0) {
328 		free(str);
329 		str = NULL;
330 	}
331 
332 	sbuf_delete(devlist->cur_sb[devlist->level]);
333 	devlist->cur_sb[devlist->level] = NULL;
334 	devlist->level--;
335 
336 	if (strcmp(name, "port_name") == 0) {
337 		cur_port->port_name = str;
338 		str = NULL;
339 	} else if (strcmp(name, "physical_port") == 0) {
340 		cur_port->pp = strtoul(str, NULL, 0);
341 	} else if (strcmp(name, "virtual_port") == 0) {
342 		cur_port->vp = strtoul(str, NULL, 0);
343 	} else if (strcmp(name, "cfiscsi_target") == 0) {
344 		cur_port->cfiscsi_target = str;
345 		str = NULL;
346 	} else if (strcmp(name, "cfiscsi_state") == 0) {
347 		cur_port->cfiscsi_state = strtoul(str, NULL, 0);
348 	} else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
349 		cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
350 	} else if (strcmp(name, "ctld_portal_group_name") == 0) {
351 		cur_port->ctld_portal_group_name = str;
352 		str = NULL;
353 	} else if (strcmp(name, "targ_port") == 0) {
354 		devlist->cur_port = NULL;
355 	} else if (strcmp(name, "ctlportlist") == 0) {
356 		/* Nothing. */
357 	} else {
358 		struct cctl_lun_nv *nv;
359 
360 		nv = calloc(1, sizeof(*nv));
361 		if (nv == NULL)
362 			log_err(1, "%s: can't allocate %zd bytes for nv pair",
363 			    __func__, sizeof(*nv));
364 
365 		nv->name = checked_strdup(name);
366 
367 		nv->value = str;
368 		str = NULL;
369 		STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
370 	}
371 
372 	free(str);
373 }
374 
375 static void
376 cctl_char_handler(void *user_data, const XML_Char *str, int len)
377 {
378 	struct cctl_devlist_data *devlist;
379 
380 	devlist = (struct cctl_devlist_data *)user_data;
381 
382 	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
383 }
384 
385 struct conf *
386 conf_new_from_kernel(void)
387 {
388 	struct conf *conf = NULL;
389 	struct target *targ;
390 	struct portal_group *pg;
391 	struct pport *pp;
392 	struct port *cp;
393 	struct lun *cl;
394 	struct lun_option *lo;
395 	struct ctl_lun_list list;
396 	struct cctl_devlist_data devlist;
397 	struct cctl_lun *lun;
398 	struct cctl_port *port;
399 	XML_Parser parser;
400 	char *str, *name;
401 	int len, retval;
402 
403 	bzero(&devlist, sizeof(devlist));
404 	STAILQ_INIT(&devlist.lun_list);
405 	STAILQ_INIT(&devlist.port_list);
406 
407 	log_debugx("obtaining previously configured CTL luns from the kernel");
408 
409 	str = NULL;
410 	len = 4096;
411 retry:
412 	str = realloc(str, len);
413 	if (str == NULL)
414 		log_err(1, "realloc");
415 
416 	bzero(&list, sizeof(list));
417 	list.alloc_len = len;
418 	list.status = CTL_LUN_LIST_NONE;
419 	list.lun_xml = str;
420 
421 	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
422 		log_warn("error issuing CTL_LUN_LIST ioctl");
423 		free(str);
424 		return (NULL);
425 	}
426 
427 	if (list.status == CTL_LUN_LIST_ERROR) {
428 		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
429 		    list.error_str);
430 		free(str);
431 		return (NULL);
432 	}
433 
434 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
435 		len = len << 1;
436 		goto retry;
437 	}
438 
439 	parser = XML_ParserCreate(NULL);
440 	if (parser == NULL) {
441 		log_warnx("unable to create XML parser");
442 		free(str);
443 		return (NULL);
444 	}
445 
446 	XML_SetUserData(parser, &devlist);
447 	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
448 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
449 
450 	retval = XML_Parse(parser, str, strlen(str), 1);
451 	XML_ParserFree(parser);
452 	free(str);
453 	if (retval != 1) {
454 		log_warnx("XML_Parse failed");
455 		return (NULL);
456 	}
457 
458 	str = NULL;
459 	len = 4096;
460 retry_port:
461 	str = realloc(str, len);
462 	if (str == NULL)
463 		log_err(1, "realloc");
464 
465 	bzero(&list, sizeof(list));
466 	list.alloc_len = len;
467 	list.status = CTL_LUN_LIST_NONE;
468 	list.lun_xml = str;
469 
470 	if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
471 		log_warn("error issuing CTL_PORT_LIST ioctl");
472 		free(str);
473 		return (NULL);
474 	}
475 
476 	if (list.status == CTL_PORT_LIST_ERROR) {
477 		log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
478 		    list.error_str);
479 		free(str);
480 		return (NULL);
481 	}
482 
483 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
484 		len = len << 1;
485 		goto retry_port;
486 	}
487 
488 	parser = XML_ParserCreate(NULL);
489 	if (parser == NULL) {
490 		log_warnx("unable to create XML parser");
491 		free(str);
492 		return (NULL);
493 	}
494 
495 	XML_SetUserData(parser, &devlist);
496 	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
497 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
498 
499 	retval = XML_Parse(parser, str, strlen(str), 1);
500 	XML_ParserFree(parser);
501 	free(str);
502 	if (retval != 1) {
503 		log_warnx("XML_Parse failed");
504 		return (NULL);
505 	}
506 
507 	conf = conf_new();
508 
509 	name = NULL;
510 	STAILQ_FOREACH(port, &devlist.port_list, links) {
511 		if (name)
512 			free(name);
513 		if (port->pp == 0 && port->vp == 0)
514 			name = checked_strdup(port->port_name);
515 		else if (port->vp == 0)
516 			asprintf(&name, "%s/%d", port->port_name, port->pp);
517 		else
518 			asprintf(&name, "%s/%d/%d", port->port_name, port->pp,
519 			    port->vp);
520 
521 		if (port->cfiscsi_target == NULL) {
522 			log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
523 			    port->port_id, name);
524 			pp = pport_find(conf, name);
525 			if (pp == NULL) {
526 #if 0
527 				log_debugx("found new kernel port %u \"%s\"",
528 				    port->port_id, name);
529 #endif
530 				pp = pport_new(conf, name, port->port_id);
531 				if (pp == NULL) {
532 					log_warnx("pport_new failed");
533 					continue;
534 				}
535 			}
536 			continue;
537 		}
538 		if (port->cfiscsi_state != 1) {
539 			log_debugx("CTL port %ju is not active (%d); ignoring",
540 			    (uintmax_t)port->port_id, port->cfiscsi_state);
541 			continue;
542 		}
543 
544 		targ = target_find(conf, port->cfiscsi_target);
545 		if (targ == NULL) {
546 #if 0
547 			log_debugx("found new kernel target %s for CTL port %ld",
548 			    port->cfiscsi_target, port->port_id);
549 #endif
550 			targ = target_new(conf, port->cfiscsi_target);
551 			if (targ == NULL) {
552 				log_warnx("target_new failed");
553 				continue;
554 			}
555 		}
556 
557 		if (port->ctld_portal_group_name == NULL)
558 			continue;
559 		pg = portal_group_find(conf, port->ctld_portal_group_name);
560 		if (pg == NULL) {
561 #if 0
562 			log_debugx("found new kernel portal group %s for CTL port %ld",
563 			    port->ctld_portal_group_name, port->port_id);
564 #endif
565 			pg = portal_group_new(conf, port->ctld_portal_group_name);
566 			if (pg == NULL) {
567 				log_warnx("portal_group_new failed");
568 				continue;
569 			}
570 		}
571 		pg->pg_tag = port->cfiscsi_portal_group_tag;
572 		cp = port_new(conf, targ, pg);
573 		if (cp == NULL) {
574 			log_warnx("port_new failed");
575 			continue;
576 		}
577 		cp->p_ctl_port = port->port_id;
578 	}
579 	if (name)
580 		free(name);
581 
582 	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
583 		struct cctl_lun_nv *nv;
584 
585 		if (lun->ctld_name == NULL) {
586 			log_debugx("CTL lun %ju wasn't managed by ctld; "
587 			    "ignoring", (uintmax_t)lun->lun_id);
588 			continue;
589 		}
590 
591 		cl = lun_find(conf, lun->ctld_name);
592 		if (cl != NULL) {
593 			log_warnx("found CTL lun %ju \"%s\", "
594 			    "also backed by CTL lun %d; ignoring",
595 			    (uintmax_t)lun->lun_id, lun->ctld_name,
596 			    cl->l_ctl_lun);
597 			continue;
598 		}
599 
600 		log_debugx("found CTL lun %ju \"%s\"",
601 		    (uintmax_t)lun->lun_id, lun->ctld_name);
602 
603 		cl = lun_new(conf, lun->ctld_name);
604 		if (cl == NULL) {
605 			log_warnx("lun_new failed");
606 			continue;
607 		}
608 		lun_set_backend(cl, lun->backend_type);
609 		lun_set_blocksize(cl, lun->blocksize);
610 		lun_set_device_id(cl, lun->device_id);
611 		lun_set_serial(cl, lun->serial_number);
612 		lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
613 		lun_set_ctl_lun(cl, lun->lun_id);
614 
615 		STAILQ_FOREACH(nv, &lun->attr_list, links) {
616 			if (strcmp(nv->name, "file") == 0 ||
617 			    strcmp(nv->name, "dev") == 0) {
618 				lun_set_path(cl, nv->value);
619 				continue;
620 			}
621 			lo = lun_option_new(cl, nv->name, nv->value);
622 			if (lo == NULL)
623 				log_warnx("unable to add CTL lun option %s "
624 				    "for CTL lun %ju \"%s\"",
625 				    nv->name, (uintmax_t) lun->lun_id,
626 				    cl->l_name);
627 		}
628 	}
629 
630 	return (conf);
631 }
632 
633 static void
634 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
635 {
636 
637 	arg->namelen = strlen(name) + 1;
638 	arg->name = __DECONST(char *, name);
639 	arg->vallen = strlen(value) + 1;
640 	arg->value = __DECONST(char *, value);
641 	arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
642 }
643 
644 int
645 kernel_lun_add(struct lun *lun)
646 {
647 	struct lun_option *lo;
648 	struct ctl_lun_req req;
649 	int error, i, num_options;
650 
651 	bzero(&req, sizeof(req));
652 
653 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
654 	req.reqtype = CTL_LUNREQ_CREATE;
655 
656 	req.reqdata.create.blocksize_bytes = lun->l_blocksize;
657 
658 	if (lun->l_size != 0)
659 		req.reqdata.create.lun_size_bytes = lun->l_size;
660 
661 	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
662 	req.reqdata.create.device_type = T_DIRECT;
663 
664 	if (lun->l_serial != NULL) {
665 		strncpy(req.reqdata.create.serial_num, lun->l_serial,
666 			sizeof(req.reqdata.create.serial_num));
667 		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
668 	}
669 
670 	if (lun->l_device_id != NULL) {
671 		strncpy(req.reqdata.create.device_id, lun->l_device_id,
672 			sizeof(req.reqdata.create.device_id));
673 		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
674 	}
675 
676 	if (lun->l_path != NULL) {
677 		lo = lun_option_find(lun, "file");
678 		if (lo != NULL) {
679 			lun_option_set(lo, lun->l_path);
680 		} else {
681 			lo = lun_option_new(lun, "file", lun->l_path);
682 			assert(lo != NULL);
683 		}
684 	}
685 
686 	lo = lun_option_find(lun, "ctld_name");
687 	if (lo != NULL) {
688 		lun_option_set(lo, lun->l_name);
689 	} else {
690 		lo = lun_option_new(lun, "ctld_name", lun->l_name);
691 		assert(lo != NULL);
692 	}
693 
694 	lo = lun_option_find(lun, "scsiname");
695 	if (lo == NULL && lun->l_scsiname != NULL) {
696 		lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
697 		assert(lo != NULL);
698 	}
699 
700 	num_options = 0;
701 	TAILQ_FOREACH(lo, &lun->l_options, lo_next)
702 		num_options++;
703 
704 	req.num_be_args = num_options;
705 	if (num_options > 0) {
706 		req.be_args = malloc(num_options * sizeof(*req.be_args));
707 		if (req.be_args == NULL) {
708 			log_warn("error allocating %zd bytes",
709 			    num_options * sizeof(*req.be_args));
710 			return (1);
711 		}
712 
713 		i = 0;
714 		TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
715 			str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
716 			i++;
717 		}
718 		assert(i == num_options);
719 	}
720 
721 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
722 	free(req.be_args);
723 	if (error != 0) {
724 		log_warn("error issuing CTL_LUN_REQ ioctl");
725 		return (1);
726 	}
727 
728 	switch (req.status) {
729 	case CTL_LUN_ERROR:
730 		log_warnx("LUN creation error: %s", req.error_str);
731 		return (1);
732 	case CTL_LUN_WARNING:
733 		log_warnx("LUN creation warning: %s", req.error_str);
734 		break;
735 	case CTL_LUN_OK:
736 		break;
737 	default:
738 		log_warnx("unknown LUN creation status: %d",
739 		    req.status);
740 		return (1);
741 	}
742 
743 	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
744 	return (0);
745 }
746 
747 int
748 kernel_lun_resize(struct lun *lun)
749 {
750 	struct ctl_lun_req req;
751 
752 	bzero(&req, sizeof(req));
753 
754 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
755 	req.reqtype = CTL_LUNREQ_MODIFY;
756 
757 	req.reqdata.modify.lun_id = lun->l_ctl_lun;
758 	req.reqdata.modify.lun_size_bytes = lun->l_size;
759 
760 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
761 		log_warn("error issuing CTL_LUN_REQ ioctl");
762 		return (1);
763 	}
764 
765 	switch (req.status) {
766 	case CTL_LUN_ERROR:
767 		log_warnx("LUN modification error: %s", req.error_str);
768 		return (1);
769 	case CTL_LUN_WARNING:
770 		log_warnx("LUN modification warning: %s", req.error_str);
771 		break;
772 	case CTL_LUN_OK:
773 		break;
774 	default:
775 		log_warnx("unknown LUN modification status: %d",
776 		    req.status);
777 		return (1);
778 	}
779 
780 	return (0);
781 }
782 
783 int
784 kernel_lun_remove(struct lun *lun)
785 {
786 	struct ctl_lun_req req;
787 
788 	bzero(&req, sizeof(req));
789 
790 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
791 	req.reqtype = CTL_LUNREQ_RM;
792 
793 	req.reqdata.rm.lun_id = lun->l_ctl_lun;
794 
795 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
796 		log_warn("error issuing CTL_LUN_REQ ioctl");
797 		return (1);
798 	}
799 
800 	switch (req.status) {
801 	case CTL_LUN_ERROR:
802 		log_warnx("LUN removal error: %s", req.error_str);
803 		return (1);
804 	case CTL_LUN_WARNING:
805 		log_warnx("LUN removal warning: %s", req.error_str);
806 		break;
807 	case CTL_LUN_OK:
808 		break;
809 	default:
810 		log_warnx("unknown LUN removal status: %d", req.status);
811 		return (1);
812 	}
813 
814 	return (0);
815 }
816 
817 void
818 kernel_handoff(struct connection *conn)
819 {
820 	struct ctl_iscsi req;
821 
822 	bzero(&req, sizeof(req));
823 
824 	req.type = CTL_ISCSI_HANDOFF;
825 	strlcpy(req.data.handoff.initiator_name,
826 	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
827 	strlcpy(req.data.handoff.initiator_addr,
828 	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
829 	if (conn->conn_initiator_alias != NULL) {
830 		strlcpy(req.data.handoff.initiator_alias,
831 		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
832 	}
833 	memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
834 	    sizeof(req.data.handoff.initiator_isid));
835 	strlcpy(req.data.handoff.target_name,
836 	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
837 	if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
838 		strlcpy(req.data.handoff.offload,
839 		    conn->conn_portal->p_portal_group->pg_offload,
840 		    sizeof(req.data.handoff.offload));
841 	}
842 #ifdef ICL_KERNEL_PROXY
843 	if (proxy_mode)
844 		req.data.handoff.connection_id = conn->conn_socket;
845 	else
846 		req.data.handoff.socket = conn->conn_socket;
847 #else
848 	req.data.handoff.socket = conn->conn_socket;
849 #endif
850 	req.data.handoff.portal_group_tag =
851 	    conn->conn_portal->p_portal_group->pg_tag;
852 	if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
853 		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
854 	if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
855 		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
856 	req.data.handoff.cmdsn = conn->conn_cmdsn;
857 	req.data.handoff.statsn = conn->conn_statsn;
858 	req.data.handoff.max_recv_data_segment_length =
859 	    conn->conn_max_data_segment_length;
860 	req.data.handoff.max_burst_length = conn->conn_max_burst_length;
861 	req.data.handoff.immediate_data = conn->conn_immediate_data;
862 
863 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
864 		log_err(1, "error issuing CTL_ISCSI ioctl; "
865 		    "dropping connection");
866 	}
867 
868 	if (req.status != CTL_ISCSI_OK) {
869 		log_errx(1, "error returned from CTL iSCSI handoff request: "
870 		    "%s; dropping connection", req.error_str);
871 	}
872 }
873 
874 void
875 kernel_limits(const char *offload, size_t *max_data_segment_length)
876 {
877 	struct ctl_iscsi req;
878 
879 	bzero(&req, sizeof(req));
880 
881 	req.type = CTL_ISCSI_LIMITS;
882 	if (offload != NULL) {
883 		strlcpy(req.data.limits.offload, offload,
884 		    sizeof(req.data.limits.offload));
885 	}
886 
887 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
888 		log_err(1, "error issuing CTL_ISCSI ioctl; "
889 		    "dropping connection");
890 	}
891 
892 	if (req.status != CTL_ISCSI_OK) {
893 		log_errx(1, "error returned from CTL iSCSI limits request: "
894 		    "%s; dropping connection", req.error_str);
895 	}
896 
897 	*max_data_segment_length = req.data.limits.data_segment_limit;
898 	if (offload != NULL) {
899 		log_debugx("MaxRecvDataSegment kernel limit for offload "
900 		    "\"%s\" is %zd", offload, *max_data_segment_length);
901 	} else {
902 		log_debugx("MaxRecvDataSegment kernel limit is %zd",
903 		    *max_data_segment_length);
904 	}
905 }
906 
907 int
908 kernel_port_add(struct port *port)
909 {
910 	struct ctl_port_entry entry;
911 	struct ctl_req req;
912 	struct ctl_lun_map lm;
913 	struct target *targ = port->p_target;
914 	struct portal_group *pg = port->p_portal_group;
915 	char tagstr[16];
916 	int error, i, n;
917 
918 	/* Create iSCSI port. */
919 	if (port->p_portal_group) {
920 		bzero(&req, sizeof(req));
921 		strlcpy(req.driver, "iscsi", sizeof(req.driver));
922 		req.reqtype = CTL_REQ_CREATE;
923 		req.num_args = 5;
924 		req.args = malloc(req.num_args * sizeof(*req.args));
925 		if (req.args == NULL)
926 			log_err(1, "malloc");
927 		n = 0;
928 		req.args[n].namelen = sizeof("port_id");
929 		req.args[n].name = __DECONST(char *, "port_id");
930 		req.args[n].vallen = sizeof(port->p_ctl_port);
931 		req.args[n].value = &port->p_ctl_port;
932 		req.args[n++].flags = CTL_BEARG_WR;
933 		str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
934 		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
935 		str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
936 		if (targ->t_alias)
937 			str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
938 		str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
939 		req.num_args = n;
940 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
941 		free(req.args);
942 		if (error != 0) {
943 			log_warn("error issuing CTL_PORT_REQ ioctl");
944 			return (1);
945 		}
946 		if (req.status == CTL_LUN_ERROR) {
947 			log_warnx("error returned from port creation request: %s",
948 			    req.error_str);
949 			return (1);
950 		}
951 		if (req.status != CTL_LUN_OK) {
952 			log_warnx("unknown port creation request status %d",
953 			    req.status);
954 			return (1);
955 		}
956 	} else if (port->p_pport) {
957 		port->p_ctl_port = port->p_pport->pp_ctl_port;
958 
959 		if (strncmp(targ->t_name, "naa.", 4) == 0 &&
960 		    strlen(targ->t_name) == 20) {
961 			bzero(&entry, sizeof(entry));
962 			entry.port_type = CTL_PORT_NONE;
963 			entry.targ_port = port->p_ctl_port;
964 			entry.flags |= CTL_PORT_WWNN_VALID;
965 			entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
966 			if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
967 				log_warn("CTL_SET_PORT_WWNS ioctl failed");
968 		}
969 	}
970 
971 	/* Explicitly enable mapping to block any access except allowed. */
972 	lm.port = port->p_ctl_port;
973 	lm.plun = UINT32_MAX;
974 	lm.lun = 0;
975 	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
976 	if (error != 0)
977 		log_warn("CTL_LUN_MAP ioctl failed");
978 
979 	/* Map configured LUNs */
980 	for (i = 0; i < MAX_LUNS; i++) {
981 		if (targ->t_luns[i] == NULL)
982 			continue;
983 		lm.port = port->p_ctl_port;
984 		lm.plun = i;
985 		lm.lun = targ->t_luns[i]->l_ctl_lun;
986 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
987 		if (error != 0)
988 			log_warn("CTL_LUN_MAP ioctl failed");
989 	}
990 
991 	/* Enable port */
992 	bzero(&entry, sizeof(entry));
993 	entry.targ_port = port->p_ctl_port;
994 	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
995 	if (error != 0) {
996 		log_warn("CTL_ENABLE_PORT ioctl failed");
997 		return (-1);
998 	}
999 
1000 	return (0);
1001 }
1002 
1003 int
1004 kernel_port_update(struct port *port)
1005 {
1006 	struct ctl_lun_map lm;
1007 	struct target *targ = port->p_target;
1008 	int error, i;
1009 
1010 	/* Map configured LUNs and unmap others */
1011 	for (i = 0; i < MAX_LUNS; i++) {
1012 		lm.port = port->p_ctl_port;
1013 		lm.plun = i;
1014 		if (targ->t_luns[i] == NULL)
1015 			lm.lun = UINT32_MAX;
1016 		else
1017 			lm.lun = targ->t_luns[i]->l_ctl_lun;
1018 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1019 		if (error != 0)
1020 			log_warn("CTL_LUN_MAP ioctl failed");
1021 	}
1022 	return (0);
1023 }
1024 
1025 int
1026 kernel_port_remove(struct port *port)
1027 {
1028 	struct ctl_port_entry entry;
1029 	struct ctl_lun_map lm;
1030 	struct ctl_req req;
1031 	char tagstr[16];
1032 	struct target *targ = port->p_target;
1033 	struct portal_group *pg = port->p_portal_group;
1034 	int error;
1035 
1036 	/* Disable port */
1037 	bzero(&entry, sizeof(entry));
1038 	entry.targ_port = port->p_ctl_port;
1039 	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1040 	if (error != 0) {
1041 		log_warn("CTL_DISABLE_PORT ioctl failed");
1042 		return (-1);
1043 	}
1044 
1045 	/* Remove iSCSI port. */
1046 	if (port->p_portal_group) {
1047 		bzero(&req, sizeof(req));
1048 		strlcpy(req.driver, "iscsi", sizeof(req.driver));
1049 		req.reqtype = CTL_REQ_REMOVE;
1050 		req.num_args = 2;
1051 		req.args = malloc(req.num_args * sizeof(*req.args));
1052 		if (req.args == NULL)
1053 			log_err(1, "malloc");
1054 		str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1055 		snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1056 		str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1057 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1058 		free(req.args);
1059 		if (error != 0) {
1060 			log_warn("error issuing CTL_PORT_REQ ioctl");
1061 			return (1);
1062 		}
1063 		if (req.status == CTL_LUN_ERROR) {
1064 			log_warnx("error returned from port removal request: %s",
1065 			    req.error_str);
1066 			return (1);
1067 		}
1068 		if (req.status != CTL_LUN_OK) {
1069 			log_warnx("unknown port removal request status %d",
1070 			    req.status);
1071 			return (1);
1072 		}
1073 	} else {
1074 		/* Disable LUN mapping. */
1075 		lm.port = port->p_ctl_port;
1076 		lm.plun = UINT32_MAX;
1077 		lm.lun = UINT32_MAX;
1078 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1079 		if (error != 0)
1080 			log_warn("CTL_LUN_MAP ioctl failed");
1081 	}
1082 	return (0);
1083 }
1084 
1085 #ifdef ICL_KERNEL_PROXY
1086 void
1087 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1088 {
1089 	struct ctl_iscsi req;
1090 
1091 	bzero(&req, sizeof(req));
1092 
1093 	req.type = CTL_ISCSI_LISTEN;
1094 	req.data.listen.iser = iser;
1095 	req.data.listen.domain = ai->ai_family;
1096 	req.data.listen.socktype = ai->ai_socktype;
1097 	req.data.listen.protocol = ai->ai_protocol;
1098 	req.data.listen.addr = ai->ai_addr;
1099 	req.data.listen.addrlen = ai->ai_addrlen;
1100 	req.data.listen.portal_id = portal_id;
1101 
1102 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1103 		log_err(1, "error issuing CTL_ISCSI ioctl");
1104 
1105 	if (req.status != CTL_ISCSI_OK) {
1106 		log_errx(1, "error returned from CTL iSCSI listen: %s",
1107 		    req.error_str);
1108 	}
1109 }
1110 
1111 void
1112 kernel_accept(int *connection_id, int *portal_id,
1113     struct sockaddr *client_sa, socklen_t *client_salen)
1114 {
1115 	struct ctl_iscsi req;
1116 	struct sockaddr_storage ss;
1117 
1118 	bzero(&req, sizeof(req));
1119 
1120 	req.type = CTL_ISCSI_ACCEPT;
1121 	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1122 
1123 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1124 		log_err(1, "error issuing CTL_ISCSI ioctl");
1125 
1126 	if (req.status != CTL_ISCSI_OK) {
1127 		log_errx(1, "error returned from CTL iSCSI accept: %s",
1128 		    req.error_str);
1129 	}
1130 
1131 	*connection_id = req.data.accept.connection_id;
1132 	*portal_id = req.data.accept.portal_id;
1133 	*client_salen = req.data.accept.initiator_addrlen;
1134 	memcpy(client_sa, &ss, *client_salen);
1135 }
1136 
1137 void
1138 kernel_send(struct pdu *pdu)
1139 {
1140 	struct ctl_iscsi req;
1141 
1142 	bzero(&req, sizeof(req));
1143 
1144 	req.type = CTL_ISCSI_SEND;
1145 	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1146 	req.data.send.bhs = pdu->pdu_bhs;
1147 	req.data.send.data_segment_len = pdu->pdu_data_len;
1148 	req.data.send.data_segment = pdu->pdu_data;
1149 
1150 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1151 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1152 		    "dropping connection");
1153 	}
1154 
1155 	if (req.status != CTL_ISCSI_OK) {
1156 		log_errx(1, "error returned from CTL iSCSI send: "
1157 		    "%s; dropping connection", req.error_str);
1158 	}
1159 }
1160 
1161 void
1162 kernel_receive(struct pdu *pdu)
1163 {
1164 	struct ctl_iscsi req;
1165 
1166 	pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1167 	if (pdu->pdu_data == NULL)
1168 		log_err(1, "malloc");
1169 
1170 	bzero(&req, sizeof(req));
1171 
1172 	req.type = CTL_ISCSI_RECEIVE;
1173 	req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1174 	req.data.receive.bhs = pdu->pdu_bhs;
1175 	req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1176 	req.data.receive.data_segment = pdu->pdu_data;
1177 
1178 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1179 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1180 		    "dropping connection");
1181 	}
1182 
1183 	if (req.status != CTL_ISCSI_OK) {
1184 		log_errx(1, "error returned from CTL iSCSI receive: "
1185 		    "%s; dropping connection", req.error_str);
1186 	}
1187 
1188 }
1189 
1190 #endif /* ICL_KERNEL_PROXY */
1191 
1192 /*
1193  * XXX: I CANT INTO LATIN
1194  */
1195 void
1196 kernel_capsicate(void)
1197 {
1198 	int error;
1199 	cap_rights_t rights;
1200 	const unsigned long cmds[] = { CTL_ISCSI };
1201 
1202 	cap_rights_init(&rights, CAP_IOCTL);
1203 	error = cap_rights_limit(ctl_fd, &rights);
1204 	if (error != 0 && errno != ENOSYS)
1205 		log_err(1, "cap_rights_limit");
1206 
1207 	error = cap_ioctls_limit(ctl_fd, cmds,
1208 	    sizeof(cmds) / sizeof(cmds[0]));
1209 	if (error != 0 && errno != ENOSYS)
1210 		log_err(1, "cap_ioctls_limit");
1211 
1212 	error = cap_enter();
1213 	if (error != 0 && errno != ENOSYS)
1214 		log_err(1, "cap_enter");
1215 
1216 	if (cap_sandboxed())
1217 		log_debugx("Capsicum capability mode enabled");
1218 	else
1219 		log_warnx("Capsicum capability mode not supported");
1220 }
1221 
1222