xref: /freebsd-14.2/usr.sbin/ctld/kernel.c (revision 0062ba53)
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 <[email protected]>
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/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/capsicum.h>
43 #include <sys/callout.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/nv.h>
50 #include <sys/stat.h>
51 #include <assert.h>
52 #include <bsdxml.h>
53 #include <capsicum_helpers.h>
54 #include <ctype.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <strings.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/ctl/ctl.h>
65 #include <cam/ctl/ctl_io.h>
66 #include <cam/ctl/ctl_backend.h>
67 #include <cam/ctl/ctl_ioctl.h>
68 #include <cam/ctl/ctl_util.h>
69 #include <cam/ctl/ctl_scsi_all.h>
70 
71 #include "ctld.h"
72 
73 #ifdef ICL_KERNEL_PROXY
74 #include <netdb.h>
75 #endif
76 
77 #define	NVLIST_BUFSIZE	1024
78 
79 extern bool proxy_mode;
80 
81 static int	ctl_fd = 0;
82 
83 void
kernel_init(void)84 kernel_init(void)
85 {
86 	int retval, saved_errno;
87 
88 	ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
89 	if (ctl_fd < 0 && errno == ENOENT) {
90 		saved_errno = errno;
91 		retval = kldload("ctl");
92 		if (retval != -1)
93 			ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
94 		else
95 			errno = saved_errno;
96 	}
97 	if (ctl_fd < 0)
98 		log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
99 #ifdef	WANT_ISCSI
100 	else {
101 		saved_errno = errno;
102 		if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
103 			log_warn("couldn't load cfiscsi");
104 		errno = saved_errno;
105 	}
106 #endif
107 }
108 
109 /*
110  * Name/value pair used for per-LUN attributes.
111  */
112 struct cctl_lun_nv {
113 	char *name;
114 	char *value;
115 	STAILQ_ENTRY(cctl_lun_nv) links;
116 };
117 
118 /*
119  * Backend LUN information.
120  */
121 struct cctl_lun {
122 	uint64_t lun_id;
123 	char *backend_type;
124 	uint8_t device_type;
125 	uint64_t size_blocks;
126 	uint32_t blocksize;
127 	char *serial_number;
128 	char *device_id;
129 	char *ctld_name;
130 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
131 	STAILQ_ENTRY(cctl_lun) links;
132 };
133 
134 struct cctl_port {
135 	uint32_t port_id;
136 	char *port_frontend;
137 	char *port_name;
138 	int pp;
139 	int vp;
140 	int cfiscsi_state;
141 	char *cfiscsi_target;
142 	uint16_t cfiscsi_portal_group_tag;
143 	char *ctld_portal_group_name;
144 	STAILQ_HEAD(,cctl_lun_nv) attr_list;
145 	STAILQ_ENTRY(cctl_port) links;
146 };
147 
148 struct cctl_devlist_data {
149 	int num_luns;
150 	STAILQ_HEAD(,cctl_lun) lun_list;
151 	struct cctl_lun *cur_lun;
152 	int num_ports;
153 	STAILQ_HEAD(,cctl_port) port_list;
154 	struct cctl_port *cur_port;
155 	int level;
156 	struct sbuf *cur_sb[32];
157 };
158 
159 static void
cctl_start_element(void * user_data,const char * name,const char ** attr)160 cctl_start_element(void *user_data, const char *name, const char **attr)
161 {
162 	int i;
163 	struct cctl_devlist_data *devlist;
164 	struct cctl_lun *cur_lun;
165 
166 	devlist = (struct cctl_devlist_data *)user_data;
167 	cur_lun = devlist->cur_lun;
168 	devlist->level++;
169 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
170 	    sizeof(devlist->cur_sb[0])))
171 		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
172 		     nitems(devlist->cur_sb));
173 
174 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
175 	if (devlist->cur_sb[devlist->level] == NULL)
176 		log_err(1, "%s: unable to allocate sbuf", __func__);
177 
178 	if (strcmp(name, "lun") == 0) {
179 		if (cur_lun != NULL)
180 			log_errx(1, "%s: improper lun element nesting",
181 			    __func__);
182 
183 		cur_lun = calloc(1, sizeof(*cur_lun));
184 		if (cur_lun == NULL)
185 			log_err(1, "%s: cannot allocate %zd bytes", __func__,
186 			    sizeof(*cur_lun));
187 
188 		devlist->num_luns++;
189 		devlist->cur_lun = cur_lun;
190 
191 		STAILQ_INIT(&cur_lun->attr_list);
192 		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
193 
194 		for (i = 0; attr[i] != NULL; i += 2) {
195 			if (strcmp(attr[i], "id") == 0) {
196 				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
197 			} else {
198 				log_errx(1, "%s: invalid LUN attribute %s = %s",
199 				     __func__, attr[i], attr[i+1]);
200 			}
201 		}
202 	}
203 }
204 
205 static void
cctl_end_element(void * user_data,const char * name)206 cctl_end_element(void *user_data, const char *name)
207 {
208 	struct cctl_devlist_data *devlist;
209 	struct cctl_lun *cur_lun;
210 	char *str;
211 
212 	devlist = (struct cctl_devlist_data *)user_data;
213 	cur_lun = devlist->cur_lun;
214 
215 	if ((cur_lun == NULL)
216 	 && (strcmp(name, "ctllunlist") != 0))
217 		log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
218 
219 	if (devlist->cur_sb[devlist->level] == NULL)
220 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
221 		     devlist->level, name);
222 
223 	sbuf_finish(devlist->cur_sb[devlist->level]);
224 	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
225 
226 	if (strlen(str) == 0) {
227 		free(str);
228 		str = NULL;
229 	}
230 
231 	sbuf_delete(devlist->cur_sb[devlist->level]);
232 	devlist->cur_sb[devlist->level] = NULL;
233 	devlist->level--;
234 
235 	if (strcmp(name, "backend_type") == 0) {
236 		cur_lun->backend_type = str;
237 		str = NULL;
238 	} else if (strcmp(name, "lun_type") == 0) {
239 		if (str == NULL)
240 			log_errx(1, "%s: %s missing its argument", __func__, name);
241 		cur_lun->device_type = strtoull(str, NULL, 0);
242 	} else if (strcmp(name, "size") == 0) {
243 		if (str == NULL)
244 			log_errx(1, "%s: %s missing its argument", __func__, name);
245 		cur_lun->size_blocks = strtoull(str, NULL, 0);
246 	} else if (strcmp(name, "blocksize") == 0) {
247 		if (str == NULL)
248 			log_errx(1, "%s: %s missing its argument", __func__, name);
249 		cur_lun->blocksize = strtoul(str, NULL, 0);
250 	} else if (strcmp(name, "serial_number") == 0) {
251 		cur_lun->serial_number = str;
252 		str = NULL;
253 	} else if (strcmp(name, "device_id") == 0) {
254 		cur_lun->device_id = str;
255 		str = NULL;
256 	} else if (strcmp(name, "ctld_name") == 0) {
257 		cur_lun->ctld_name = str;
258 		str = NULL;
259 	} else if (strcmp(name, "lun") == 0) {
260 		devlist->cur_lun = NULL;
261 	} else if (strcmp(name, "ctllunlist") == 0) {
262 		/* Nothing. */
263 	} else {
264 		struct cctl_lun_nv *nv;
265 
266 		nv = calloc(1, sizeof(*nv));
267 		if (nv == NULL)
268 			log_err(1, "%s: can't allocate %zd bytes for nv pair",
269 			    __func__, sizeof(*nv));
270 
271 		nv->name = checked_strdup(name);
272 
273 		nv->value = str;
274 		str = NULL;
275 		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
276 	}
277 
278 	free(str);
279 }
280 
281 static void
cctl_start_pelement(void * user_data,const char * name,const char ** attr)282 cctl_start_pelement(void *user_data, const char *name, const char **attr)
283 {
284 	int i;
285 	struct cctl_devlist_data *devlist;
286 	struct cctl_port *cur_port;
287 
288 	devlist = (struct cctl_devlist_data *)user_data;
289 	cur_port = devlist->cur_port;
290 	devlist->level++;
291 	if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
292 	    sizeof(devlist->cur_sb[0])))
293 		log_errx(1, "%s: too many nesting levels, %zd max", __func__,
294 		     nitems(devlist->cur_sb));
295 
296 	devlist->cur_sb[devlist->level] = sbuf_new_auto();
297 	if (devlist->cur_sb[devlist->level] == NULL)
298 		log_err(1, "%s: unable to allocate sbuf", __func__);
299 
300 	if (strcmp(name, "targ_port") == 0) {
301 		if (cur_port != NULL)
302 			log_errx(1, "%s: improper port element nesting (%s)",
303 			    __func__, name);
304 
305 		cur_port = calloc(1, sizeof(*cur_port));
306 		if (cur_port == NULL)
307 			log_err(1, "%s: cannot allocate %zd bytes", __func__,
308 			    sizeof(*cur_port));
309 
310 		devlist->num_ports++;
311 		devlist->cur_port = cur_port;
312 
313 		STAILQ_INIT(&cur_port->attr_list);
314 		STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
315 
316 		for (i = 0; attr[i] != NULL; i += 2) {
317 			if (strcmp(attr[i], "id") == 0) {
318 				cur_port->port_id = strtoul(attr[i+1], NULL, 0);
319 			} else {
320 				log_errx(1, "%s: invalid LUN attribute %s = %s",
321 				     __func__, attr[i], attr[i+1]);
322 			}
323 		}
324 	}
325 }
326 
327 static void
cctl_end_pelement(void * user_data,const char * name)328 cctl_end_pelement(void *user_data, const char *name)
329 {
330 	struct cctl_devlist_data *devlist;
331 	struct cctl_port *cur_port;
332 	char *str;
333 
334 	devlist = (struct cctl_devlist_data *)user_data;
335 	cur_port = devlist->cur_port;
336 
337 	if ((cur_port == NULL)
338 	 && (strcmp(name, "ctlportlist") != 0))
339 		log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
340 
341 	if (devlist->cur_sb[devlist->level] == NULL)
342 		log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
343 		     devlist->level, name);
344 
345 	sbuf_finish(devlist->cur_sb[devlist->level]);
346 	str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
347 
348 	if (strlen(str) == 0) {
349 		free(str);
350 		str = NULL;
351 	}
352 
353 	sbuf_delete(devlist->cur_sb[devlist->level]);
354 	devlist->cur_sb[devlist->level] = NULL;
355 	devlist->level--;
356 
357 	if (strcmp(name, "frontend_type") == 0) {
358 		cur_port->port_frontend = str;
359 		str = NULL;
360 	} else if (strcmp(name, "port_name") == 0) {
361 		cur_port->port_name = str;
362 		str = NULL;
363 	} else if (strcmp(name, "physical_port") == 0) {
364 		if (str == NULL)
365 			log_errx(1, "%s: %s missing its argument", __func__, name);
366 		cur_port->pp = strtoul(str, NULL, 0);
367 	} else if (strcmp(name, "virtual_port") == 0) {
368 		if (str == NULL)
369 			log_errx(1, "%s: %s missing its argument", __func__, name);
370 		cur_port->vp = strtoul(str, NULL, 0);
371 	} else if (strcmp(name, "cfiscsi_target") == 0) {
372 		cur_port->cfiscsi_target = str;
373 		str = NULL;
374 	} else if (strcmp(name, "cfiscsi_state") == 0) {
375 		if (str == NULL)
376 			log_errx(1, "%s: %s missing its argument", __func__, name);
377 		cur_port->cfiscsi_state = strtoul(str, NULL, 0);
378 	} else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
379 		if (str == NULL)
380 			log_errx(1, "%s: %s missing its argument", __func__, name);
381 		cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
382 	} else if (strcmp(name, "ctld_portal_group_name") == 0) {
383 		cur_port->ctld_portal_group_name = str;
384 		str = NULL;
385 	} else if (strcmp(name, "targ_port") == 0) {
386 		devlist->cur_port = NULL;
387 	} else if (strcmp(name, "ctlportlist") == 0) {
388 		/* Nothing. */
389 	} else {
390 		struct cctl_lun_nv *nv;
391 
392 		nv = calloc(1, sizeof(*nv));
393 		if (nv == NULL)
394 			log_err(1, "%s: can't allocate %zd bytes for nv pair",
395 			    __func__, sizeof(*nv));
396 
397 		nv->name = checked_strdup(name);
398 
399 		nv->value = str;
400 		str = NULL;
401 		STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
402 	}
403 
404 	free(str);
405 }
406 
407 static void
cctl_char_handler(void * user_data,const XML_Char * str,int len)408 cctl_char_handler(void *user_data, const XML_Char *str, int len)
409 {
410 	struct cctl_devlist_data *devlist;
411 
412 	devlist = (struct cctl_devlist_data *)user_data;
413 
414 	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
415 }
416 
417 struct conf *
conf_new_from_kernel(struct kports * kports)418 conf_new_from_kernel(struct kports *kports)
419 {
420 	struct conf *conf = NULL;
421 	struct target *targ;
422 	struct portal_group *pg;
423 	struct pport *pp;
424 	struct port *cp;
425 	struct lun *cl;
426 	struct option *o;
427 	struct ctl_lun_list list;
428 	struct cctl_devlist_data devlist;
429 	struct cctl_lun *lun;
430 	struct cctl_port *port;
431 	XML_Parser parser;
432 	char *str, *name;
433 	int len, retval;
434 
435 	bzero(&devlist, sizeof(devlist));
436 	STAILQ_INIT(&devlist.lun_list);
437 	STAILQ_INIT(&devlist.port_list);
438 
439 	log_debugx("obtaining previously configured CTL luns from the kernel");
440 
441 	str = NULL;
442 	len = 4096;
443 retry:
444 	str = realloc(str, len);
445 	if (str == NULL)
446 		log_err(1, "realloc");
447 
448 	bzero(&list, sizeof(list));
449 	list.alloc_len = len;
450 	list.status = CTL_LUN_LIST_NONE;
451 	list.lun_xml = str;
452 
453 	if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
454 		log_warn("error issuing CTL_LUN_LIST ioctl");
455 		free(str);
456 		return (NULL);
457 	}
458 
459 	if (list.status == CTL_LUN_LIST_ERROR) {
460 		log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
461 		    list.error_str);
462 		free(str);
463 		return (NULL);
464 	}
465 
466 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
467 		len = len << 1;
468 		goto retry;
469 	}
470 
471 	parser = XML_ParserCreate(NULL);
472 	if (parser == NULL) {
473 		log_warnx("unable to create XML parser");
474 		free(str);
475 		return (NULL);
476 	}
477 
478 	XML_SetUserData(parser, &devlist);
479 	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
480 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
481 
482 	retval = XML_Parse(parser, str, strlen(str), 1);
483 	XML_ParserFree(parser);
484 	free(str);
485 	if (retval != 1) {
486 		log_warnx("XML_Parse failed");
487 		return (NULL);
488 	}
489 
490 	str = NULL;
491 	len = 4096;
492 retry_port:
493 	str = realloc(str, len);
494 	if (str == NULL)
495 		log_err(1, "realloc");
496 
497 	bzero(&list, sizeof(list));
498 	list.alloc_len = len;
499 	list.status = CTL_LUN_LIST_NONE;
500 	list.lun_xml = str;
501 
502 	if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
503 		log_warn("error issuing CTL_PORT_LIST ioctl");
504 		free(str);
505 		return (NULL);
506 	}
507 
508 	if (list.status == CTL_LUN_LIST_ERROR) {
509 		log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
510 		    list.error_str);
511 		free(str);
512 		return (NULL);
513 	}
514 
515 	if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
516 		len = len << 1;
517 		goto retry_port;
518 	}
519 
520 	parser = XML_ParserCreate(NULL);
521 	if (parser == NULL) {
522 		log_warnx("unable to create XML parser");
523 		free(str);
524 		return (NULL);
525 	}
526 
527 	XML_SetUserData(parser, &devlist);
528 	XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
529 	XML_SetCharacterDataHandler(parser, cctl_char_handler);
530 
531 	retval = XML_Parse(parser, str, strlen(str), 1);
532 	XML_ParserFree(parser);
533 	free(str);
534 	if (retval != 1) {
535 		log_warnx("XML_Parse failed");
536 		return (NULL);
537 	}
538 
539 	conf = conf_new();
540 
541 	name = NULL;
542 	STAILQ_FOREACH(port, &devlist.port_list, links) {
543 		if (strcmp(port->port_frontend, "ha") == 0)
544 			continue;
545 		free(name);
546 		if (port->pp == 0 && port->vp == 0) {
547 			name = checked_strdup(port->port_name);
548 		} else if (port->vp == 0) {
549 			retval = asprintf(&name, "%s/%d",
550 			    port->port_name, port->pp);
551 			if (retval <= 0)
552 				log_err(1, "asprintf");
553 		} else {
554 			retval = asprintf(&name, "%s/%d/%d",
555 			    port->port_name, port->pp, port->vp);
556 			if (retval <= 0)
557 				log_err(1, "asprintf");
558 		}
559 
560 		if (port->cfiscsi_target == NULL) {
561 			log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
562 			    port->port_id, name);
563 			pp = pport_find(kports, name);
564 			if (pp == NULL) {
565 #if 0
566 				log_debugx("found new kernel port %u \"%s\"",
567 				    port->port_id, name);
568 #endif
569 				pp = pport_new(kports, name, port->port_id);
570 				if (pp == NULL) {
571 					log_warnx("pport_new failed");
572 					continue;
573 				}
574 			}
575 			continue;
576 		}
577 		if (port->cfiscsi_state != 1) {
578 			log_debugx("CTL port %ju is not active (%d); ignoring",
579 			    (uintmax_t)port->port_id, port->cfiscsi_state);
580 			continue;
581 		}
582 
583 		targ = target_find(conf, port->cfiscsi_target);
584 		if (targ == NULL) {
585 #if 0
586 			log_debugx("found new kernel target %s for CTL port %ld",
587 			    port->cfiscsi_target, port->port_id);
588 #endif
589 			targ = target_new(conf, port->cfiscsi_target);
590 			if (targ == NULL) {
591 				log_warnx("target_new failed");
592 				continue;
593 			}
594 		}
595 
596 		if (port->ctld_portal_group_name == NULL)
597 			continue;
598 		pg = portal_group_find(conf, port->ctld_portal_group_name);
599 		if (pg == NULL) {
600 #if 0
601 			log_debugx("found new kernel portal group %s for CTL port %ld",
602 			    port->ctld_portal_group_name, port->port_id);
603 #endif
604 			pg = portal_group_new(conf, port->ctld_portal_group_name);
605 			if (pg == NULL) {
606 				log_warnx("portal_group_new failed");
607 				continue;
608 			}
609 		}
610 		pg->pg_tag = port->cfiscsi_portal_group_tag;
611 		cp = port_new(conf, targ, pg);
612 		if (cp == NULL) {
613 			log_warnx("port_new failed");
614 			continue;
615 		}
616 		cp->p_ctl_port = port->port_id;
617 	}
618 	while ((port = STAILQ_FIRST(&devlist.port_list))) {
619 		struct cctl_lun_nv *nv;
620 
621 		STAILQ_REMOVE_HEAD(&devlist.port_list, links);
622 		free(port->port_frontend);
623 		free(port->port_name);
624 		free(port->cfiscsi_target);
625 		free(port->ctld_portal_group_name);
626 		while ((nv = STAILQ_FIRST(&port->attr_list))) {
627 			STAILQ_REMOVE_HEAD(&port->attr_list, links);
628 			free(nv->value);
629 			free(nv->name);
630 			free(nv);
631 		}
632 		free(port);
633 	}
634 	free(name);
635 
636 	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
637 		struct cctl_lun_nv *nv;
638 
639 		if (lun->ctld_name == NULL) {
640 			log_debugx("CTL lun %ju wasn't managed by ctld; "
641 			    "ignoring", (uintmax_t)lun->lun_id);
642 			continue;
643 		}
644 
645 		cl = lun_find(conf, lun->ctld_name);
646 		if (cl != NULL) {
647 			log_warnx("found CTL lun %ju \"%s\", "
648 			    "also backed by CTL lun %d; ignoring",
649 			    (uintmax_t)lun->lun_id, lun->ctld_name,
650 			    cl->l_ctl_lun);
651 			continue;
652 		}
653 
654 		log_debugx("found CTL lun %ju \"%s\"",
655 		    (uintmax_t)lun->lun_id, lun->ctld_name);
656 
657 		cl = lun_new(conf, lun->ctld_name);
658 		if (cl == NULL) {
659 			log_warnx("lun_new failed");
660 			continue;
661 		}
662 		lun_set_backend(cl, lun->backend_type);
663 		lun_set_device_type(cl, lun->device_type);
664 		lun_set_blocksize(cl, lun->blocksize);
665 		lun_set_device_id(cl, lun->device_id);
666 		lun_set_serial(cl, lun->serial_number);
667 		lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
668 		lun_set_ctl_lun(cl, lun->lun_id);
669 
670 		STAILQ_FOREACH(nv, &lun->attr_list, links) {
671 			if (strcmp(nv->name, "file") == 0 ||
672 			    strcmp(nv->name, "dev") == 0) {
673 				lun_set_path(cl, nv->value);
674 				continue;
675 			}
676 			o = option_new(&cl->l_options, nv->name, nv->value);
677 			if (o == NULL)
678 				log_warnx("unable to add CTL lun option %s "
679 				    "for CTL lun %ju \"%s\"",
680 				    nv->name, (uintmax_t) lun->lun_id,
681 				    cl->l_name);
682 		}
683 	}
684 	while ((lun = STAILQ_FIRST(&devlist.lun_list))) {
685 		struct cctl_lun_nv *nv;
686 
687 		STAILQ_REMOVE_HEAD(&devlist.lun_list, links);
688 		while ((nv = STAILQ_FIRST(&lun->attr_list))) {
689 			STAILQ_REMOVE_HEAD(&lun->attr_list, links);
690 			free(nv->value);
691 			free(nv->name);
692 			free(nv);
693 		}
694 		free(lun);
695 	}
696 
697 	return (conf);
698 }
699 
700 int
kernel_lun_add(struct lun * lun)701 kernel_lun_add(struct lun *lun)
702 {
703 	struct option *o;
704 	struct ctl_lun_req req;
705 	int error;
706 
707 	bzero(&req, sizeof(req));
708 
709 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
710 	req.reqtype = CTL_LUNREQ_CREATE;
711 
712 	req.reqdata.create.blocksize_bytes = lun->l_blocksize;
713 
714 	if (lun->l_size != 0)
715 		req.reqdata.create.lun_size_bytes = lun->l_size;
716 
717 	if (lun->l_ctl_lun >= 0) {
718 		req.reqdata.create.req_lun_id = lun->l_ctl_lun;
719 		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
720 	}
721 
722 	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
723 	req.reqdata.create.device_type = lun->l_device_type;
724 
725 	if (lun->l_serial != NULL) {
726 		strncpy(req.reqdata.create.serial_num, lun->l_serial,
727 			sizeof(req.reqdata.create.serial_num));
728 		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
729 	}
730 
731 	if (lun->l_device_id != NULL) {
732 		strncpy(req.reqdata.create.device_id, lun->l_device_id,
733 			sizeof(req.reqdata.create.device_id));
734 		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
735 	}
736 
737 	if (lun->l_path != NULL) {
738 		o = option_find(&lun->l_options, "file");
739 		if (o != NULL) {
740 			option_set(o, lun->l_path);
741 		} else {
742 			o = option_new(&lun->l_options, "file", lun->l_path);
743 			assert(o != NULL);
744 		}
745 	}
746 
747 	o = option_find(&lun->l_options, "ctld_name");
748 	if (o != NULL) {
749 		option_set(o, lun->l_name);
750 	} else {
751 		o = option_new(&lun->l_options, "ctld_name", lun->l_name);
752 		assert(o != NULL);
753 	}
754 
755 	o = option_find(&lun->l_options, "scsiname");
756 	if (o == NULL && lun->l_scsiname != NULL) {
757 		o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
758 		assert(o != NULL);
759 	}
760 
761 	if (!TAILQ_EMPTY(&lun->l_options)) {
762 		req.args_nvl = nvlist_create(0);
763 		if (req.args_nvl == NULL) {
764 			log_warn("error allocating nvlist");
765 			return (1);
766 		}
767 
768 		TAILQ_FOREACH(o, &lun->l_options, o_next)
769 			nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
770 
771 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
772 		if (req.args == NULL) {
773 			nvlist_destroy(req.args_nvl);
774 			log_warn("error packing nvlist");
775 			return (1);
776 		}
777 	}
778 
779 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
780 	free(req.args);
781 	nvlist_destroy(req.args_nvl);
782 
783 	if (error != 0) {
784 		log_warn("error issuing CTL_LUN_REQ ioctl");
785 		return (1);
786 	}
787 
788 	switch (req.status) {
789 	case CTL_LUN_ERROR:
790 		log_warnx("LUN creation error: %s", req.error_str);
791 		return (1);
792 	case CTL_LUN_WARNING:
793 		log_warnx("LUN creation warning: %s", req.error_str);
794 		break;
795 	case CTL_LUN_OK:
796 		break;
797 	default:
798 		log_warnx("unknown LUN creation status: %d",
799 		    req.status);
800 		return (1);
801 	}
802 
803 	lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
804 	return (0);
805 }
806 
807 int
kernel_lun_modify(struct lun * lun)808 kernel_lun_modify(struct lun *lun)
809 {
810 	struct option *o;
811 	struct ctl_lun_req req;
812 	int error;
813 
814 	bzero(&req, sizeof(req));
815 
816 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
817 	req.reqtype = CTL_LUNREQ_MODIFY;
818 
819 	req.reqdata.modify.lun_id = lun->l_ctl_lun;
820 	req.reqdata.modify.lun_size_bytes = lun->l_size;
821 
822 	if (lun->l_path != NULL) {
823 		o = option_find(&lun->l_options, "file");
824 		if (o != NULL) {
825 			option_set(o, lun->l_path);
826 		} else {
827 			o = option_new(&lun->l_options, "file", lun->l_path);
828 			assert(o != NULL);
829 		}
830 	}
831 
832 	o = option_find(&lun->l_options, "ctld_name");
833 	if (o != NULL) {
834 		option_set(o, lun->l_name);
835 	} else {
836 		o = option_new(&lun->l_options, "ctld_name", lun->l_name);
837 		assert(o != NULL);
838 	}
839 
840 	o = option_find(&lun->l_options, "scsiname");
841 	if (o == NULL && lun->l_scsiname != NULL) {
842 		o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
843 		assert(o != NULL);
844 	}
845 
846 	if (!TAILQ_EMPTY(&lun->l_options)) {
847 		req.args_nvl = nvlist_create(0);
848 		if (req.args_nvl == NULL) {
849 			log_warn("error allocating nvlist");
850 			return (1);
851 		}
852 
853 		TAILQ_FOREACH(o, &lun->l_options, o_next)
854 			nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
855 
856 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
857 		if (req.args == NULL) {
858 			nvlist_destroy(req.args_nvl);
859 			log_warn("error packing nvlist");
860 			return (1);
861 		}
862 	}
863 
864 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
865 	free(req.args);
866 	nvlist_destroy(req.args_nvl);
867 
868 	if (error != 0) {
869 		log_warn("error issuing CTL_LUN_REQ ioctl");
870 		return (1);
871 	}
872 
873 	switch (req.status) {
874 	case CTL_LUN_ERROR:
875 		log_warnx("LUN modification error: %s", req.error_str);
876 		return (1);
877 	case CTL_LUN_WARNING:
878 		log_warnx("LUN modification warning: %s", req.error_str);
879 		break;
880 	case CTL_LUN_OK:
881 		break;
882 	default:
883 		log_warnx("unknown LUN modification status: %d",
884 		    req.status);
885 		return (1);
886 	}
887 
888 	return (0);
889 }
890 
891 int
kernel_lun_remove(struct lun * lun)892 kernel_lun_remove(struct lun *lun)
893 {
894 	struct ctl_lun_req req;
895 
896 	bzero(&req, sizeof(req));
897 
898 	strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
899 	req.reqtype = CTL_LUNREQ_RM;
900 
901 	req.reqdata.rm.lun_id = lun->l_ctl_lun;
902 
903 	if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
904 		log_warn("error issuing CTL_LUN_REQ ioctl");
905 		return (1);
906 	}
907 
908 	switch (req.status) {
909 	case CTL_LUN_ERROR:
910 		log_warnx("LUN removal error: %s", req.error_str);
911 		return (1);
912 	case CTL_LUN_WARNING:
913 		log_warnx("LUN removal warning: %s", req.error_str);
914 		break;
915 	case CTL_LUN_OK:
916 		break;
917 	default:
918 		log_warnx("unknown LUN removal status: %d", req.status);
919 		return (1);
920 	}
921 
922 	return (0);
923 }
924 
925 void
kernel_handoff(struct ctld_connection * conn)926 kernel_handoff(struct ctld_connection *conn)
927 {
928 	struct ctl_iscsi req;
929 
930 	bzero(&req, sizeof(req));
931 
932 	req.type = CTL_ISCSI_HANDOFF;
933 	strlcpy(req.data.handoff.initiator_name,
934 	    conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
935 	strlcpy(req.data.handoff.initiator_addr,
936 	    conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
937 	if (conn->conn_initiator_alias != NULL) {
938 		strlcpy(req.data.handoff.initiator_alias,
939 		    conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
940 	}
941 	memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
942 	    sizeof(req.data.handoff.initiator_isid));
943 	strlcpy(req.data.handoff.target_name,
944 	    conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
945 	if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
946 		strlcpy(req.data.handoff.offload,
947 		    conn->conn_portal->p_portal_group->pg_offload,
948 		    sizeof(req.data.handoff.offload));
949 	}
950 #ifdef ICL_KERNEL_PROXY
951 	if (proxy_mode)
952 		req.data.handoff.connection_id = conn->conn.conn_socket;
953 	else
954 		req.data.handoff.socket = conn->conn.conn_socket;
955 #else
956 	req.data.handoff.socket = conn->conn.conn_socket;
957 #endif
958 	req.data.handoff.portal_group_tag =
959 	    conn->conn_portal->p_portal_group->pg_tag;
960 	if (conn->conn.conn_header_digest == CONN_DIGEST_CRC32C)
961 		req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
962 	if (conn->conn.conn_data_digest == CONN_DIGEST_CRC32C)
963 		req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
964 	req.data.handoff.cmdsn = conn->conn.conn_cmdsn;
965 	req.data.handoff.statsn = conn->conn.conn_statsn;
966 	req.data.handoff.max_recv_data_segment_length =
967 	    conn->conn.conn_max_recv_data_segment_length;
968 	req.data.handoff.max_send_data_segment_length =
969 	    conn->conn.conn_max_send_data_segment_length;
970 	req.data.handoff.max_burst_length = conn->conn.conn_max_burst_length;
971 	req.data.handoff.first_burst_length =
972 	    conn->conn.conn_first_burst_length;
973 	req.data.handoff.immediate_data = conn->conn.conn_immediate_data;
974 
975 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
976 		log_err(1, "error issuing CTL_ISCSI ioctl; "
977 		    "dropping connection");
978 	}
979 
980 	if (req.status != CTL_ISCSI_OK) {
981 		log_errx(1, "error returned from CTL iSCSI handoff request: "
982 		    "%s; dropping connection", req.error_str);
983 	}
984 }
985 
986 void
kernel_limits(const char * offload,int s,int * max_recv_dsl,int * max_send_dsl,int * max_burst_length,int * first_burst_length)987 kernel_limits(const char *offload, int s, int *max_recv_dsl, int *max_send_dsl,
988     int *max_burst_length, int *first_burst_length)
989 {
990 	struct ctl_iscsi req;
991 	struct ctl_iscsi_limits_params *cilp;
992 
993 	bzero(&req, sizeof(req));
994 
995 	req.type = CTL_ISCSI_LIMITS;
996 	cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
997 	if (offload != NULL) {
998 		strlcpy(cilp->offload, offload, sizeof(cilp->offload));
999 	}
1000 	cilp->socket = s;
1001 
1002 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1003 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1004 		    "dropping connection");
1005 	}
1006 
1007 	if (req.status != CTL_ISCSI_OK) {
1008 		log_errx(1, "error returned from CTL iSCSI limits request: "
1009 		    "%s; dropping connection", req.error_str);
1010 	}
1011 
1012 	if (cilp->max_recv_data_segment_length != 0) {
1013 		*max_recv_dsl = cilp->max_recv_data_segment_length;
1014 		*max_send_dsl = cilp->max_recv_data_segment_length;
1015 	}
1016 	if (cilp->max_send_data_segment_length != 0)
1017 		*max_send_dsl = cilp->max_send_data_segment_length;
1018 	if (cilp->max_burst_length != 0)
1019 		*max_burst_length = cilp->max_burst_length;
1020 	if (cilp->first_burst_length != 0)
1021 		*first_burst_length = cilp->first_burst_length;
1022 	if (*max_burst_length < *first_burst_length)
1023 		*first_burst_length = *max_burst_length;
1024 
1025 	if (offload != NULL) {
1026 		log_debugx("Kernel limits for offload \"%s\" are "
1027 		    "MaxRecvDataSegment=%d, max_send_dsl=%d, "
1028 		    "MaxBurstLength=%d, FirstBurstLength=%d",
1029 		    offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
1030 		    *first_burst_length);
1031 	} else {
1032 		log_debugx("Kernel limits are "
1033 		    "MaxRecvDataSegment=%d, max_send_dsl=%d, "
1034 		    "MaxBurstLength=%d, FirstBurstLength=%d",
1035 		    *max_recv_dsl, *max_send_dsl, *max_burst_length,
1036 		    *first_burst_length);
1037 	}
1038 }
1039 
1040 int
kernel_port_add(struct port * port)1041 kernel_port_add(struct port *port)
1042 {
1043 	struct option *o;
1044 	struct ctl_port_entry entry;
1045 	struct ctl_req req;
1046 	struct ctl_lun_map lm;
1047 	struct target *targ = port->p_target;
1048 	struct portal_group *pg = port->p_portal_group;
1049 	char result_buf[NVLIST_BUFSIZE];
1050 	int error, i;
1051 
1052 	/* Create iSCSI port. */
1053 	if (port->p_portal_group || port->p_ioctl_port) {
1054 		bzero(&req, sizeof(req));
1055 		req.reqtype = CTL_REQ_CREATE;
1056 
1057 		if (port->p_portal_group) {
1058 			strlcpy(req.driver, "iscsi", sizeof(req.driver));
1059 			req.args_nvl = nvlist_create(0);
1060 			nvlist_add_string(req.args_nvl, "cfiscsi_target",
1061 			    targ->t_name);
1062 			nvlist_add_string(req.args_nvl,
1063 			    "ctld_portal_group_name", pg->pg_name);
1064 			nvlist_add_stringf(req.args_nvl,
1065 			    "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1066 
1067 			if (targ->t_alias) {
1068 				nvlist_add_string(req.args_nvl,
1069 				    "cfiscsi_target_alias", targ->t_alias);
1070 			}
1071 
1072 			TAILQ_FOREACH(o, &pg->pg_options, o_next)
1073 				nvlist_add_string(req.args_nvl, o->o_name,
1074 				    o->o_value);
1075 		}
1076 
1077 		if (port->p_ioctl_port) {
1078 			strlcpy(req.driver, "ioctl", sizeof(req.driver));
1079 			req.args_nvl = nvlist_create(0);
1080 			nvlist_add_stringf(req.args_nvl, "pp", "%d",
1081 			    port->p_ioctl_pp);
1082 			nvlist_add_stringf(req.args_nvl, "vp", "%d",
1083 			    port->p_ioctl_vp);
1084 		}
1085 
1086 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
1087 		if (req.args == NULL) {
1088 			nvlist_destroy(req.args_nvl);
1089 			log_warn("error packing nvlist");
1090 			return (1);
1091 		}
1092 
1093 		req.result = result_buf;
1094 		req.result_len = sizeof(result_buf);
1095 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1096 		free(req.args);
1097 		nvlist_destroy(req.args_nvl);
1098 
1099 		if (error != 0) {
1100 			log_warn("error issuing CTL_PORT_REQ ioctl");
1101 			return (1);
1102 		}
1103 		if (req.status == CTL_LUN_ERROR) {
1104 			log_warnx("error returned from port creation request: %s",
1105 			    req.error_str);
1106 			return (1);
1107 		}
1108 		if (req.status != CTL_LUN_OK) {
1109 			log_warnx("unknown port creation request status %d",
1110 			    req.status);
1111 			return (1);
1112 		}
1113 
1114 		req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0);
1115 		if (req.result_nvl == NULL) {
1116 			log_warnx("error unpacking result nvlist");
1117 			return (1);
1118 		}
1119 
1120 		port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id");
1121 		nvlist_destroy(req.result_nvl);
1122 	} else if (port->p_pport) {
1123 		port->p_ctl_port = port->p_pport->pp_ctl_port;
1124 
1125 		if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1126 		    strlen(targ->t_name) == 20) {
1127 			bzero(&entry, sizeof(entry));
1128 			entry.port_type = CTL_PORT_NONE;
1129 			entry.targ_port = port->p_ctl_port;
1130 			entry.flags |= CTL_PORT_WWNN_VALID;
1131 			entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1132 			if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1133 				log_warn("CTL_SET_PORT_WWNS ioctl failed");
1134 		}
1135 	}
1136 
1137 	/* Explicitly enable mapping to block any access except allowed. */
1138 	lm.port = port->p_ctl_port;
1139 	lm.plun = UINT32_MAX;
1140 	lm.lun = 0;
1141 	error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1142 	if (error != 0)
1143 		log_warn("CTL_LUN_MAP ioctl failed");
1144 
1145 	/* Map configured LUNs */
1146 	for (i = 0; i < MAX_LUNS; i++) {
1147 		if (targ->t_luns[i] == NULL)
1148 			continue;
1149 		lm.port = port->p_ctl_port;
1150 		lm.plun = i;
1151 		lm.lun = targ->t_luns[i]->l_ctl_lun;
1152 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1153 		if (error != 0)
1154 			log_warn("CTL_LUN_MAP ioctl failed");
1155 	}
1156 
1157 	/* Enable port */
1158 	bzero(&entry, sizeof(entry));
1159 	entry.targ_port = port->p_ctl_port;
1160 	error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1161 	if (error != 0) {
1162 		log_warn("CTL_ENABLE_PORT ioctl failed");
1163 		return (-1);
1164 	}
1165 
1166 	return (0);
1167 }
1168 
1169 int
kernel_port_update(struct port * port,struct port * oport)1170 kernel_port_update(struct port *port, struct port *oport)
1171 {
1172 	struct ctl_lun_map lm;
1173 	struct target *targ = port->p_target;
1174 	struct target *otarg = oport->p_target;
1175 	int error, i;
1176 	uint32_t olun;
1177 
1178 	/* Map configured LUNs and unmap others */
1179 	for (i = 0; i < MAX_LUNS; i++) {
1180 		lm.port = port->p_ctl_port;
1181 		lm.plun = i;
1182 		if (targ->t_luns[i] == NULL)
1183 			lm.lun = UINT32_MAX;
1184 		else
1185 			lm.lun = targ->t_luns[i]->l_ctl_lun;
1186 		if (otarg->t_luns[i] == NULL)
1187 			olun = UINT32_MAX;
1188 		else
1189 			olun = otarg->t_luns[i]->l_ctl_lun;
1190 		if (lm.lun == olun)
1191 			continue;
1192 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1193 		if (error != 0)
1194 			log_warn("CTL_LUN_MAP ioctl failed");
1195 	}
1196 	return (0);
1197 }
1198 
1199 int
kernel_port_remove(struct port * port)1200 kernel_port_remove(struct port *port)
1201 {
1202 	struct ctl_port_entry entry;
1203 	struct ctl_lun_map lm;
1204 	struct ctl_req req;
1205 	struct target *targ = port->p_target;
1206 	struct portal_group *pg = port->p_portal_group;
1207 	int error;
1208 
1209 	/* Disable port */
1210 	bzero(&entry, sizeof(entry));
1211 	entry.targ_port = port->p_ctl_port;
1212 	error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1213 	if (error != 0) {
1214 		log_warn("CTL_DISABLE_PORT ioctl failed");
1215 		return (-1);
1216 	}
1217 
1218 	/* Remove iSCSI or ioctl port. */
1219 	if (port->p_portal_group || port->p_ioctl_port) {
1220 		bzero(&req, sizeof(req));
1221 		strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi",
1222 		    sizeof(req.driver));
1223 		req.reqtype = CTL_REQ_REMOVE;
1224 		req.args_nvl = nvlist_create(0);
1225 		if (req.args_nvl == NULL)
1226 			log_err(1, "nvlist_create");
1227 
1228 		if (port->p_ioctl_port)
1229 			nvlist_add_stringf(req.args_nvl, "port_id", "%d",
1230 			    port->p_ctl_port);
1231 		else {
1232 			nvlist_add_string(req.args_nvl, "cfiscsi_target",
1233 			    targ->t_name);
1234 			nvlist_add_stringf(req.args_nvl,
1235 			    "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1236 		}
1237 
1238 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
1239 		if (req.args == NULL) {
1240 			nvlist_destroy(req.args_nvl);
1241 			log_warn("error packing nvlist");
1242 			return (1);
1243 		}
1244 
1245 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1246 		free(req.args);
1247 		nvlist_destroy(req.args_nvl);
1248 
1249 		if (error != 0) {
1250 			log_warn("error issuing CTL_PORT_REQ ioctl");
1251 			return (1);
1252 		}
1253 		if (req.status == CTL_LUN_ERROR) {
1254 			log_warnx("error returned from port removal request: %s",
1255 			    req.error_str);
1256 			return (1);
1257 		}
1258 		if (req.status != CTL_LUN_OK) {
1259 			log_warnx("unknown port removal request status %d",
1260 			    req.status);
1261 			return (1);
1262 		}
1263 	} else {
1264 		/* Disable LUN mapping. */
1265 		lm.port = port->p_ctl_port;
1266 		lm.plun = UINT32_MAX;
1267 		lm.lun = UINT32_MAX;
1268 		error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1269 		if (error != 0)
1270 			log_warn("CTL_LUN_MAP ioctl failed");
1271 	}
1272 	return (0);
1273 }
1274 
1275 #ifdef ICL_KERNEL_PROXY
1276 void
kernel_listen(struct addrinfo * ai,bool iser,int portal_id)1277 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1278 {
1279 	struct ctl_iscsi req;
1280 
1281 	bzero(&req, sizeof(req));
1282 
1283 	req.type = CTL_ISCSI_LISTEN;
1284 	req.data.listen.iser = iser;
1285 	req.data.listen.domain = ai->ai_family;
1286 	req.data.listen.socktype = ai->ai_socktype;
1287 	req.data.listen.protocol = ai->ai_protocol;
1288 	req.data.listen.addr = ai->ai_addr;
1289 	req.data.listen.addrlen = ai->ai_addrlen;
1290 	req.data.listen.portal_id = portal_id;
1291 
1292 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1293 		log_err(1, "error issuing CTL_ISCSI ioctl");
1294 
1295 	if (req.status != CTL_ISCSI_OK) {
1296 		log_errx(1, "error returned from CTL iSCSI listen: %s",
1297 		    req.error_str);
1298 	}
1299 }
1300 
1301 void
kernel_accept(int * connection_id,int * portal_id,struct sockaddr * client_sa,socklen_t * client_salen)1302 kernel_accept(int *connection_id, int *portal_id,
1303     struct sockaddr *client_sa, socklen_t *client_salen)
1304 {
1305 	struct ctl_iscsi req;
1306 	struct sockaddr_storage ss;
1307 
1308 	bzero(&req, sizeof(req));
1309 
1310 	req.type = CTL_ISCSI_ACCEPT;
1311 	req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1312 
1313 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1314 		log_err(1, "error issuing CTL_ISCSI ioctl");
1315 
1316 	if (req.status != CTL_ISCSI_OK) {
1317 		log_errx(1, "error returned from CTL iSCSI accept: %s",
1318 		    req.error_str);
1319 	}
1320 
1321 	*connection_id = req.data.accept.connection_id;
1322 	*portal_id = req.data.accept.portal_id;
1323 	*client_salen = req.data.accept.initiator_addrlen;
1324 	memcpy(client_sa, &ss, *client_salen);
1325 }
1326 
1327 void
kernel_send(struct pdu * pdu)1328 kernel_send(struct pdu *pdu)
1329 {
1330 	struct ctl_iscsi req;
1331 
1332 	bzero(&req, sizeof(req));
1333 
1334 	req.type = CTL_ISCSI_SEND;
1335 	req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1336 	req.data.send.bhs = pdu->pdu_bhs;
1337 	req.data.send.data_segment_len = pdu->pdu_data_len;
1338 	req.data.send.data_segment = pdu->pdu_data;
1339 
1340 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1341 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1342 		    "dropping connection");
1343 	}
1344 
1345 	if (req.status != CTL_ISCSI_OK) {
1346 		log_errx(1, "error returned from CTL iSCSI send: "
1347 		    "%s; dropping connection", req.error_str);
1348 	}
1349 }
1350 
1351 void
kernel_receive(struct pdu * pdu)1352 kernel_receive(struct pdu *pdu)
1353 {
1354 	struct connection *conn;
1355 	struct ctl_iscsi req;
1356 
1357 	conn = pdu->pdu_connection;
1358 	pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1359 	if (pdu->pdu_data == NULL)
1360 		log_err(1, "malloc");
1361 
1362 	bzero(&req, sizeof(req));
1363 
1364 	req.type = CTL_ISCSI_RECEIVE;
1365 	req.data.receive.connection_id = conn->conn_socket;
1366 	req.data.receive.bhs = pdu->pdu_bhs;
1367 	req.data.receive.data_segment_len =
1368 	    conn->conn_max_recv_data_segment_length;
1369 	req.data.receive.data_segment = pdu->pdu_data;
1370 
1371 	if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1372 		log_err(1, "error issuing CTL_ISCSI ioctl; "
1373 		    "dropping connection");
1374 	}
1375 
1376 	if (req.status != CTL_ISCSI_OK) {
1377 		log_errx(1, "error returned from CTL iSCSI receive: "
1378 		    "%s; dropping connection", req.error_str);
1379 	}
1380 
1381 }
1382 
1383 #endif /* ICL_KERNEL_PROXY */
1384 
1385 /*
1386  * XXX: I CANT INTO LATIN
1387  */
1388 void
kernel_capsicate(void)1389 kernel_capsicate(void)
1390 {
1391 	cap_rights_t rights;
1392 	const unsigned long cmds[] = { CTL_ISCSI };
1393 
1394 	cap_rights_init(&rights, CAP_IOCTL);
1395 	if (caph_rights_limit(ctl_fd, &rights) < 0)
1396 		log_err(1, "cap_rights_limit");
1397 
1398 	if (caph_ioctls_limit(ctl_fd, cmds, nitems(cmds)) < 0)
1399 		log_err(1, "cap_ioctls_limit");
1400 
1401 	if (caph_enter() < 0)
1402 		log_err(1, "cap_enter");
1403 
1404 	if (cap_sandboxed())
1405 		log_debugx("Capsicum capability mode enabled");
1406 	else
1407 		log_warnx("Capsicum capability mode not supported");
1408 }
1409 
1410