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