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: stable/10/usr.sbin/ctld/kernel.c 279003 2015-02-19 14:33:46Z mav $");
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/capability.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
76extern bool proxy_mode;
77
78static int ctl_fd = 0;
79
80void
81kernel_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 */
101struct cctl_lun_nv {
102 char *name;
103 char *value;
104 STAILQ_ENTRY(cctl_lun_nv) links;
105};
106
107/*
108 * Backend LUN information.
109 */
110struct 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
122struct cctl_port {
123 uint32_t port_id;
124 int cfiscsi_status;
125 char *cfiscsi_target;
126 uint16_t cfiscsi_portal_group_tag;
127 STAILQ_HEAD(,cctl_lun_nv) attr_list;
128 STAILQ_ENTRY(cctl_port) links;
129};
130
131struct cctl_devlist_data {
132 int num_luns;
133 STAILQ_HEAD(,cctl_lun) lun_list;
134 struct cctl_lun *cur_lun;
135 int num_ports;
136 STAILQ_HEAD(,cctl_port) port_list;
137 struct cctl_port *cur_port;
138 int level;
139 struct sbuf *cur_sb[32];
140};
141
142static void
143cctl_start_element(void *user_data, const char *name, const char **attr)
144{
145 int i;
146 struct cctl_devlist_data *devlist;
147 struct cctl_lun *cur_lun;
148
149 devlist = (struct cctl_devlist_data *)user_data;
150 cur_lun = devlist->cur_lun;
151 devlist->level++;
152 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
153 sizeof(devlist->cur_sb[0])))
154 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
155 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
156
157 devlist->cur_sb[devlist->level] = sbuf_new_auto();
158 if (devlist->cur_sb[devlist->level] == NULL)
159 log_err(1, "%s: unable to allocate sbuf", __func__);
160
161 if (strcmp(name, "lun") == 0) {
162 if (cur_lun != NULL)
163 log_errx(1, "%s: improper lun element nesting",
164 __func__);
165
166 cur_lun = calloc(1, sizeof(*cur_lun));
167 if (cur_lun == NULL)
168 log_err(1, "%s: cannot allocate %zd bytes", __func__,
169 sizeof(*cur_lun));
170
171 devlist->num_luns++;
172 devlist->cur_lun = cur_lun;
173
174 STAILQ_INIT(&cur_lun->attr_list);
175 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
176
177 for (i = 0; attr[i] != NULL; i += 2) {
178 if (strcmp(attr[i], "id") == 0) {
179 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
180 } else {
181 log_errx(1, "%s: invalid LUN attribute %s = %s",
182 __func__, attr[i], attr[i+1]);
183 }
184 }
185 }
186}
187
188static void
189cctl_end_element(void *user_data, const char *name)
190{
191 struct cctl_devlist_data *devlist;
192 struct cctl_lun *cur_lun;
193 char *str;
194
195 devlist = (struct cctl_devlist_data *)user_data;
196 cur_lun = devlist->cur_lun;
197
198 if ((cur_lun == NULL)
199 && (strcmp(name, "ctllunlist") != 0))
200 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
201
202 if (devlist->cur_sb[devlist->level] == NULL)
203 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
204 devlist->level, name);
205
206 sbuf_finish(devlist->cur_sb[devlist->level]);
207 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
208
209 if (strlen(str) == 0) {
210 free(str);
211 str = NULL;
212 }
213
214 sbuf_delete(devlist->cur_sb[devlist->level]);
215 devlist->cur_sb[devlist->level] = NULL;
216 devlist->level--;
217
218 if (strcmp(name, "backend_type") == 0) {
219 cur_lun->backend_type = str;
220 str = NULL;
221 } else if (strcmp(name, "size") == 0) {
222 cur_lun->size_blocks = strtoull(str, NULL, 0);
223 } else if (strcmp(name, "blocksize") == 0) {
224 cur_lun->blocksize = strtoul(str, NULL, 0);
225 } else if (strcmp(name, "serial_number") == 0) {
226 cur_lun->serial_number = str;
227 str = NULL;
228 } else if (strcmp(name, "device_id") == 0) {
229 cur_lun->device_id = str;
230 str = NULL;
231 } else if (strcmp(name, "ctld_name") == 0) {
232 cur_lun->ctld_name = str;
233 str = NULL;
234 } else if (strcmp(name, "lun") == 0) {
235 devlist->cur_lun = NULL;
236 } else if (strcmp(name, "ctllunlist") == 0) {
237 /* Nothing. */
238 } else {
239 struct cctl_lun_nv *nv;
240
241 nv = calloc(1, sizeof(*nv));
242 if (nv == NULL)
243 log_err(1, "%s: can't allocate %zd bytes for nv pair",
244 __func__, sizeof(*nv));
245
246 nv->name = checked_strdup(name);
247
248 nv->value = str;
249 str = NULL;
250 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
251 }
252
253 free(str);
254}
255
256static void
257cctl_start_pelement(void *user_data, const char *name, const char **attr)
258{
259 int i;
260 struct cctl_devlist_data *devlist;
261 struct cctl_port *cur_port;
262
263 devlist = (struct cctl_devlist_data *)user_data;
264 cur_port = devlist->cur_port;
265 devlist->level++;
266 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
267 sizeof(devlist->cur_sb[0])))
268 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
269 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
270
271 devlist->cur_sb[devlist->level] = sbuf_new_auto();
272 if (devlist->cur_sb[devlist->level] == NULL)
273 log_err(1, "%s: unable to allocate sbuf", __func__);
274
275 if (strcmp(name, "targ_port") == 0) {
276 if (cur_port != NULL)
277 log_errx(1, "%s: improper port element nesting (%s)",
278 __func__, name);
279
280 cur_port = calloc(1, sizeof(*cur_port));
281 if (cur_port == NULL)
282 log_err(1, "%s: cannot allocate %zd bytes", __func__,
283 sizeof(*cur_port));
284
285 devlist->num_ports++;
286 devlist->cur_port = cur_port;
287
288 STAILQ_INIT(&cur_port->attr_list);
289 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
290
291 for (i = 0; attr[i] != NULL; i += 2) {
292 if (strcmp(attr[i], "id") == 0) {
293 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
294 } else {
295 log_errx(1, "%s: invalid LUN attribute %s = %s",
296 __func__, attr[i], attr[i+1]);
297 }
298 }
299 }
300}
301
302static void
303cctl_end_pelement(void *user_data, const char *name)
304{
305 struct cctl_devlist_data *devlist;
306 struct cctl_port *cur_port;
307 char *str;
308
309 devlist = (struct cctl_devlist_data *)user_data;
310 cur_port = devlist->cur_port;
311
312 if ((cur_port == NULL)
313 && (strcmp(name, "ctlportlist") != 0))
314 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
315
316 if (devlist->cur_sb[devlist->level] == NULL)
317 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
318 devlist->level, name);
319
320 sbuf_finish(devlist->cur_sb[devlist->level]);
321 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
322
323 if (strlen(str) == 0) {
324 free(str);
325 str = NULL;
326 }
327
328 sbuf_delete(devlist->cur_sb[devlist->level]);
329 devlist->cur_sb[devlist->level] = NULL;
330 devlist->level--;
331
332 if (strcmp(name, "cfiscsi_target") == 0) {
333 cur_port->cfiscsi_target = str;
334 str = NULL;
335 } else if (strcmp(name, "cfiscsi_status") == 0) {
336 cur_port->cfiscsi_status = strtoul(str, NULL, 0);
337 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
338 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
339 } else if (strcmp(name, "targ_port") == 0) {
340 devlist->cur_port = NULL;
341 } else if (strcmp(name, "ctlportlist") == 0) {
342 /* Nothing. */
343 } else {
344 struct cctl_lun_nv *nv;
345
346 nv = calloc(1, sizeof(*nv));
347 if (nv == NULL)
348 log_err(1, "%s: can't allocate %zd bytes for nv pair",
349 __func__, sizeof(*nv));
350
351 nv->name = checked_strdup(name);
352
353 nv->value = str;
354 str = NULL;
355 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
356 }
357
358 free(str);
359}
360
361static void
362cctl_char_handler(void *user_data, const XML_Char *str, int len)
363{
364 struct cctl_devlist_data *devlist;
365
366 devlist = (struct cctl_devlist_data *)user_data;
367
368 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
369}
370
371struct conf *
372conf_new_from_kernel(void)
373{
374 struct conf *conf = NULL;
375 struct target *targ;
376 struct lun *cl;
377 struct lun_option *lo;
378 struct ctl_lun_list list;
379 struct cctl_devlist_data devlist;
380 struct cctl_lun *lun;
381 struct cctl_port *port;
382 XML_Parser parser;
383 char *str;
384 int len, retval;
385
386 bzero(&devlist, sizeof(devlist));
387 STAILQ_INIT(&devlist.lun_list);
388 STAILQ_INIT(&devlist.port_list);
389
390 log_debugx("obtaining previously configured CTL luns from the kernel");
391
392 str = NULL;
393 len = 4096;
394retry:
395 str = realloc(str, len);
396 if (str == NULL)
397 log_err(1, "realloc");
398
399 bzero(&list, sizeof(list));
400 list.alloc_len = len;
401 list.status = CTL_LUN_LIST_NONE;
402 list.lun_xml = str;
403
404 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
405 log_warn("error issuing CTL_LUN_LIST ioctl");
406 free(str);
407 return (NULL);
408 }
409
410 if (list.status == CTL_LUN_LIST_ERROR) {
411 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
412 list.error_str);
413 free(str);
414 return (NULL);
415 }
416
417 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
418 len = len << 1;
419 goto retry;
420 }
421
422 parser = XML_ParserCreate(NULL);
423 if (parser == NULL) {
424 log_warnx("unable to create XML parser");
425 free(str);
426 return (NULL);
427 }
428
429 XML_SetUserData(parser, &devlist);
430 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
431 XML_SetCharacterDataHandler(parser, cctl_char_handler);
432
433 retval = XML_Parse(parser, str, strlen(str), 1);
434 XML_ParserFree(parser);
435 free(str);
436 if (retval != 1) {
437 log_warnx("XML_Parse failed");
438 return (NULL);
439 }
440
441 str = NULL;
442 len = 4096;
443retry_port:
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_PORT_LIST, &list) == -1) {
454 log_warn("error issuing CTL_PORT_LIST ioctl");
455 free(str);
456 return (NULL);
457 }
458
459 if (list.status == CTL_PORT_LIST_ERROR) {
460 log_warnx("error returned from CTL_PORT_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_port;
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_pelement, cctl_end_pelement);
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 conf = conf_new();
491
492 STAILQ_FOREACH(port, &devlist.port_list, links) {
493
494 if (port->cfiscsi_target == NULL) {
495 log_debugx("CTL port %ju wasn't managed by ctld; "
496 "ignoring", (uintmax_t)port->port_id);
497 continue;
498 }
499 if (port->cfiscsi_status != 1) {
500 log_debugx("CTL port %ju is not active (%d); ignoring",
501 (uintmax_t)port->port_id, port->cfiscsi_status);
502 continue;
503 }
504
505 targ = target_find(conf, port->cfiscsi_target);
506 if (targ == NULL) {
507#if 0
508 log_debugx("found new kernel target %s for CTL port %ld",
509 port->cfiscsi_target, port->port_id);
510#endif
511 targ = target_new(conf, port->cfiscsi_target);
512 if (targ == NULL) {
513 log_warnx("target_new failed");
514 continue;
515 }
516 }
517 }
518
519 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
520 struct cctl_lun_nv *nv;
521
522 if (lun->ctld_name == NULL) {
523 log_debugx("CTL lun %ju wasn't managed by ctld; "
524 "ignoring", (uintmax_t)lun->lun_id);
525 continue;
526 }
527
528 cl = lun_find(conf, lun->ctld_name);
529 if (cl != NULL) {
530 log_warnx("found CTL lun %ju \"%s\", "
531 "also backed by CTL lun %d; ignoring",
532 (uintmax_t)lun->lun_id, lun->ctld_name,
533 cl->l_ctl_lun);
534 continue;
535 }
536
537 log_debugx("found CTL lun %ju \"%s\"",
538 (uintmax_t)lun->lun_id, lun->ctld_name);
539
540 cl = lun_new(conf, lun->ctld_name);
541 if (cl == NULL) {
542 log_warnx("lun_new failed");
543 continue;
544 }
545 lun_set_backend(cl, lun->backend_type);
546 lun_set_blocksize(cl, lun->blocksize);
547 lun_set_device_id(cl, lun->device_id);
548 lun_set_serial(cl, lun->serial_number);
549 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
550 lun_set_ctl_lun(cl, lun->lun_id);
551
552 STAILQ_FOREACH(nv, &lun->attr_list, links) {
553 if (strcmp(nv->name, "file") == 0 ||
554 strcmp(nv->name, "dev") == 0) {
555 lun_set_path(cl, nv->value);
556 continue;
557 }
558 lo = lun_option_new(cl, nv->name, nv->value);
559 if (lo == NULL)
560 log_warnx("unable to add CTL lun option %s "
561 "for CTL lun %ju \"%s\"",
562 nv->name, (uintmax_t) lun->lun_id,
563 cl->l_name);
564 }
565 }
566
567 return (conf);
568}
569
570static void
571str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
572{
573
574 arg->namelen = strlen(name) + 1;
575 arg->name = __DECONST(char *, name);
576 arg->vallen = strlen(value) + 1;
577 arg->value = __DECONST(char *, value);
578 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
579}
580
581int
582kernel_lun_add(struct lun *lun)
583{
584 struct lun_option *lo;
585 struct ctl_lun_req req;
586 int error, i, num_options;
587
588 bzero(&req, sizeof(req));
589
590 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
591 req.reqtype = CTL_LUNREQ_CREATE;
592
593 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
594
595 if (lun->l_size != 0)
596 req.reqdata.create.lun_size_bytes = lun->l_size;
597
598 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
599 req.reqdata.create.device_type = T_DIRECT;
600
601 if (lun->l_serial != NULL) {
602 strncpy(req.reqdata.create.serial_num, lun->l_serial,
603 sizeof(req.reqdata.create.serial_num));
604 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
605 }
606
607 if (lun->l_device_id != NULL) {
608 strncpy(req.reqdata.create.device_id, lun->l_device_id,
609 sizeof(req.reqdata.create.device_id));
610 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
611 }
612
613 if (lun->l_path != NULL) {
614 lo = lun_option_find(lun, "file");
615 if (lo != NULL) {
616 lun_option_set(lo, lun->l_path);
617 } else {
618 lo = lun_option_new(lun, "file", lun->l_path);
619 assert(lo != NULL);
620 }
621 }
622
623 lo = lun_option_find(lun, "ctld_name");
624 if (lo != NULL) {
625 lun_option_set(lo, lun->l_name);
626 } else {
627 lo = lun_option_new(lun, "ctld_name", lun->l_name);
628 assert(lo != NULL);
629 }
630
631 lo = lun_option_find(lun, "scsiname");
632 if (lo == NULL && lun->l_scsiname != NULL) {
633 lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
634 assert(lo != NULL);
635 }
636
637 num_options = 0;
638 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
639 num_options++;
640
641 req.num_be_args = num_options;
642 if (num_options > 0) {
643 req.be_args = malloc(num_options * sizeof(*req.be_args));
644 if (req.be_args == NULL) {
645 log_warn("error allocating %zd bytes",
646 num_options * sizeof(*req.be_args));
647 return (1);
648 }
649
650 i = 0;
651 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
652 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
653 i++;
654 }
655 assert(i == num_options);
656 }
657
658 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
659 free(req.be_args);
660 if (error != 0) {
661 log_warn("error issuing CTL_LUN_REQ ioctl");
662 return (1);
663 }
664
665 switch (req.status) {
666 case CTL_LUN_ERROR:
667 log_warnx("LUN creation error: %s", req.error_str);
668 return (1);
669 case CTL_LUN_WARNING:
670 log_warnx("LUN creation warning: %s", req.error_str);
671 break;
672 case CTL_LUN_OK:
673 break;
674 default:
675 log_warnx("unknown LUN creation status: %d",
676 req.status);
677 return (1);
678 }
679
680 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
681 return (0);
682}
683
684int
685kernel_lun_resize(struct lun *lun)
686{
687 struct ctl_lun_req req;
688
689 bzero(&req, sizeof(req));
690
691 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
692 req.reqtype = CTL_LUNREQ_MODIFY;
693
694 req.reqdata.modify.lun_id = lun->l_ctl_lun;
695 req.reqdata.modify.lun_size_bytes = lun->l_size;
696
697 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
698 log_warn("error issuing CTL_LUN_REQ ioctl");
699 return (1);
700 }
701
702 switch (req.status) {
703 case CTL_LUN_ERROR:
704 log_warnx("LUN modification error: %s", req.error_str);
705 return (1);
706 case CTL_LUN_WARNING:
707 log_warnx("LUN modification warning: %s", req.error_str);
708 break;
709 case CTL_LUN_OK:
710 break;
711 default:
712 log_warnx("unknown LUN modification status: %d",
713 req.status);
714 return (1);
715 }
716
717 return (0);
718}
719
720int
721kernel_lun_remove(struct lun *lun)
722{
723 struct ctl_lun_req req;
724
725 bzero(&req, sizeof(req));
726
727 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
728 req.reqtype = CTL_LUNREQ_RM;
729
730 req.reqdata.rm.lun_id = lun->l_ctl_lun;
731
732 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
733 log_warn("error issuing CTL_LUN_REQ ioctl");
734 return (1);
735 }
736
737 switch (req.status) {
738 case CTL_LUN_ERROR:
739 log_warnx("LUN removal error: %s", req.error_str);
740 return (1);
741 case CTL_LUN_WARNING:
742 log_warnx("LUN removal warning: %s", req.error_str);
743 break;
744 case CTL_LUN_OK:
745 break;
746 default:
747 log_warnx("unknown LUN removal status: %d", req.status);
748 return (1);
749 }
750
751 return (0);
752}
753
754void
755kernel_handoff(struct connection *conn)
756{
757 struct ctl_iscsi req;
758
759 bzero(&req, sizeof(req));
760
761 req.type = CTL_ISCSI_HANDOFF;
762 strlcpy(req.data.handoff.initiator_name,
763 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
764 strlcpy(req.data.handoff.initiator_addr,
765 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
766 if (conn->conn_initiator_alias != NULL) {
767 strlcpy(req.data.handoff.initiator_alias,
768 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
769 }
770 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
771 sizeof(req.data.handoff.initiator_isid));
772 strlcpy(req.data.handoff.target_name,
773 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
774#ifdef ICL_KERNEL_PROXY
775 if (proxy_mode)
776 req.data.handoff.connection_id = conn->conn_socket;
777 else
778 req.data.handoff.socket = conn->conn_socket;
779#else
780 req.data.handoff.socket = conn->conn_socket;
781#endif
782 req.data.handoff.portal_group_tag =
783 conn->conn_portal->p_portal_group->pg_tag;
784 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
785 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
786 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
787 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
788 req.data.handoff.cmdsn = conn->conn_cmdsn;
789 req.data.handoff.statsn = conn->conn_statsn;
790 req.data.handoff.max_recv_data_segment_length =
791 conn->conn_max_data_segment_length;
792 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
793 req.data.handoff.immediate_data = conn->conn_immediate_data;
794
795 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
796 log_err(1, "error issuing CTL_ISCSI ioctl; "
797 "dropping connection");
798 }
799
800 if (req.status != CTL_ISCSI_OK) {
801 log_errx(1, "error returned from CTL iSCSI handoff request: "
802 "%s; dropping connection", req.error_str);
803 }
804}
805
806int
807kernel_port_add(struct target *targ)
808{
809 struct ctl_port_entry entry;
810 struct ctl_req req;
811 struct ctl_lun_map lm;
812 char tagstr[16];
813 int error, i;
814
815 /* Create iSCSI port. */
816 bzero(&req, sizeof(req));
817 strlcpy(req.driver, "iscsi", sizeof(req.driver));
818 req.reqtype = CTL_REQ_CREATE;
819 req.num_args = 4;
820 req.args = malloc(req.num_args * sizeof(*req.args));
821 req.args[0].namelen = sizeof("port_id");
822 req.args[0].name = __DECONST(char *, "port_id");
823 req.args[0].vallen = sizeof(targ->t_ctl_port);
824 req.args[0].value = &targ->t_ctl_port;
825 req.args[0].flags = CTL_BEARG_WR;
826 str_arg(&req.args[1], "cfiscsi_target", targ->t_name);
827 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
828 str_arg(&req.args[2], "cfiscsi_portal_group_tag", tagstr);
829 if (targ->t_alias)
830 str_arg(&req.args[3], "cfiscsi_target_alias", targ->t_alias);
831 else
832 req.num_args--;
833 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
834 free(req.args);
835 if (error != 0) {
836 log_warn("error issuing CTL_PORT_REQ ioctl");
837 return (1);
838 }
839 if (req.status == CTL_LUN_ERROR) {
840 log_warnx("error returned from port creation request: %s",
841 req.error_str);
842 return (1);
843 }
844 if (req.status != CTL_LUN_OK) {
845 log_warnx("unknown port creation request status %d",
846 req.status);
847 return (1);
848 }
849
850 /* Explicitly enable mapping to block any access except allowed. */
851 lm.port = targ->t_ctl_port;
852 lm.plun = UINT32_MAX;
853 lm.lun = 0;
854 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
855 if (error != 0)
856 log_warn("CTL_LUN_MAP ioctl failed");
857
858 /* Map configured LUNs */
859 for (i = 0; i < MAX_LUNS; i++) {
860 if (targ->t_luns[i] == NULL)
861 continue;
862 lm.port = targ->t_ctl_port;
863 lm.plun = i;
864 lm.lun = targ->t_luns[i]->l_ctl_lun;
865 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
866 if (error != 0)
867 log_warn("CTL_LUN_MAP ioctl failed");
868 }
869
870 /* Enable port */
871 bzero(&entry, sizeof(entry));
872 entry.targ_port = targ->t_ctl_port;
873 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
874 if (error != 0) {
875 log_warn("CTL_ENABLE_PORT ioctl failed");
876 return (-1);
877 }
878
879 return (0);
880}
881
882int
883kernel_port_update(struct target *targ)
884{
885 struct ctl_lun_map lm;
886 int error, i;
887
888 /* Map configured LUNs and unmap others */
889 for (i = 0; i < MAX_LUNS; i++) {
890 lm.port = targ->t_ctl_port;
891 lm.plun = i;
892 if (targ->t_luns[i] == NULL)
893 lm.lun = UINT32_MAX;
894 else
895 lm.lun = targ->t_luns[i]->l_ctl_lun;
896 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
897 if (error != 0)
898 log_warn("CTL_LUN_MAP ioctl failed");
899 }
900 return (0);
901}
902
903int
904kernel_port_remove(struct target *targ)
905{
906 struct ctl_req req;
907 char tagstr[16];
908 int error;
909
910 bzero(&req, sizeof(req));
911 strlcpy(req.driver, "iscsi", sizeof(req.driver));
912 req.reqtype = CTL_REQ_REMOVE;
913 req.num_args = 2;
914 req.args = malloc(req.num_args * sizeof(*req.args));
915 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
916 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
917 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
918
919 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
920 free(req.args);
921 if (error != 0) {
922 log_warn("error issuing CTL_PORT_REQ ioctl");
923 return (1);
924 }
925
926 if (req.status == CTL_LUN_ERROR) {
927 log_warnx("error returned from port removal request: %s",
928 req.error_str);
929 return (1);
930 }
931
932 if (req.status != CTL_LUN_OK) {
933 log_warnx("unknown port removal request status %d",
934 req.status);
935 return (1);
936 }
937
938 return (0);
939}
940
941#ifdef ICL_KERNEL_PROXY
942void
943kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
944{
945 struct ctl_iscsi req;
946
947 bzero(&req, sizeof(req));
948
949 req.type = CTL_ISCSI_LISTEN;
950 req.data.listen.iser = iser;
951 req.data.listen.domain = ai->ai_family;
952 req.data.listen.socktype = ai->ai_socktype;
953 req.data.listen.protocol = ai->ai_protocol;
954 req.data.listen.addr = ai->ai_addr;
955 req.data.listen.addrlen = ai->ai_addrlen;
956 req.data.listen.portal_id = portal_id;
957
958 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
959 log_err(1, "error issuing CTL_ISCSI ioctl");
960
961 if (req.status != CTL_ISCSI_OK) {
962 log_errx(1, "error returned from CTL iSCSI listen: %s",
963 req.error_str);
964 }
965}
966
967void
968kernel_accept(int *connection_id, int *portal_id,
969 struct sockaddr *client_sa, socklen_t *client_salen)
970{
971 struct ctl_iscsi req;
972 struct sockaddr_storage ss;
973
974 bzero(&req, sizeof(req));
975
976 req.type = CTL_ISCSI_ACCEPT;
977 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
978
979 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
980 log_err(1, "error issuing CTL_ISCSI ioctl");
981
982 if (req.status != CTL_ISCSI_OK) {
983 log_errx(1, "error returned from CTL iSCSI accept: %s",
984 req.error_str);
985 }
986
987 *connection_id = req.data.accept.connection_id;
988 *portal_id = req.data.accept.portal_id;
989 *client_salen = req.data.accept.initiator_addrlen;
990 memcpy(client_sa, &ss, *client_salen);
991}
992
993void
994kernel_send(struct pdu *pdu)
995{
996 struct ctl_iscsi req;
997
998 bzero(&req, sizeof(req));
999
1000 req.type = CTL_ISCSI_SEND;
1001 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1002 req.data.send.bhs = pdu->pdu_bhs;
1003 req.data.send.data_segment_len = pdu->pdu_data_len;
1004 req.data.send.data_segment = pdu->pdu_data;
1005
1006 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1007 log_err(1, "error issuing CTL_ISCSI ioctl; "
1008 "dropping connection");
1009 }
1010
1011 if (req.status != CTL_ISCSI_OK) {
1012 log_errx(1, "error returned from CTL iSCSI send: "
1013 "%s; dropping connection", req.error_str);
1014 }
1015}
1016
1017void
1018kernel_receive(struct pdu *pdu)
1019{
1020 struct ctl_iscsi req;
1021
1022 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1023 if (pdu->pdu_data == NULL)
1024 log_err(1, "malloc");
1025
1026 bzero(&req, sizeof(req));
1027
1028 req.type = CTL_ISCSI_RECEIVE;
1029 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1030 req.data.receive.bhs = pdu->pdu_bhs;
1031 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1032 req.data.receive.data_segment = pdu->pdu_data;
1033
1034 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1035 log_err(1, "error issuing CTL_ISCSI ioctl; "
1036 "dropping connection");
1037 }
1038
1039 if (req.status != CTL_ISCSI_OK) {
1040 log_errx(1, "error returned from CTL iSCSI receive: "
1041 "%s; dropping connection", req.error_str);
1042 }
1043
1044}
1045
1046#endif /* ICL_KERNEL_PROXY */
1047
1048/*
1049 * XXX: I CANT INTO LATIN
1050 */
1051void
1052kernel_capsicate(void)
1053{
1054 int error;
1055 cap_rights_t rights;
1056 const unsigned long cmds[] = { CTL_ISCSI };
1057
1058 cap_rights_init(&rights, CAP_IOCTL);
1059 error = cap_rights_limit(ctl_fd, &rights);
1060 if (error != 0 && errno != ENOSYS)
1061 log_err(1, "cap_rights_limit");
1062
1063 error = cap_ioctls_limit(ctl_fd, cmds,
1064 sizeof(cmds) / sizeof(cmds[0]));
1065 if (error != 0 && errno != ENOSYS)
1066 log_err(1, "cap_ioctls_limit");
1067
1068 error = cap_enter();
1069 if (error != 0 && errno != ENOSYS)
1070 log_err(1, "cap_enter");
1071
1072 if (cap_sandboxed())
1073 log_debugx("Capsicum capability mode enabled");
1074 else
1075 log_warnx("Capsicum capability mode not supported");
1076}
1077
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: stable/10/usr.sbin/ctld/kernel.c 279003 2015-02-19 14:33:46Z mav $");
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/capability.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
76extern bool proxy_mode;
77
78static int ctl_fd = 0;
79
80void
81kernel_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 */
101struct cctl_lun_nv {
102 char *name;
103 char *value;
104 STAILQ_ENTRY(cctl_lun_nv) links;
105};
106
107/*
108 * Backend LUN information.
109 */
110struct 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
122struct cctl_port {
123 uint32_t port_id;
124 int cfiscsi_status;
125 char *cfiscsi_target;
126 uint16_t cfiscsi_portal_group_tag;
127 STAILQ_HEAD(,cctl_lun_nv) attr_list;
128 STAILQ_ENTRY(cctl_port) links;
129};
130
131struct cctl_devlist_data {
132 int num_luns;
133 STAILQ_HEAD(,cctl_lun) lun_list;
134 struct cctl_lun *cur_lun;
135 int num_ports;
136 STAILQ_HEAD(,cctl_port) port_list;
137 struct cctl_port *cur_port;
138 int level;
139 struct sbuf *cur_sb[32];
140};
141
142static void
143cctl_start_element(void *user_data, const char *name, const char **attr)
144{
145 int i;
146 struct cctl_devlist_data *devlist;
147 struct cctl_lun *cur_lun;
148
149 devlist = (struct cctl_devlist_data *)user_data;
150 cur_lun = devlist->cur_lun;
151 devlist->level++;
152 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
153 sizeof(devlist->cur_sb[0])))
154 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
155 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
156
157 devlist->cur_sb[devlist->level] = sbuf_new_auto();
158 if (devlist->cur_sb[devlist->level] == NULL)
159 log_err(1, "%s: unable to allocate sbuf", __func__);
160
161 if (strcmp(name, "lun") == 0) {
162 if (cur_lun != NULL)
163 log_errx(1, "%s: improper lun element nesting",
164 __func__);
165
166 cur_lun = calloc(1, sizeof(*cur_lun));
167 if (cur_lun == NULL)
168 log_err(1, "%s: cannot allocate %zd bytes", __func__,
169 sizeof(*cur_lun));
170
171 devlist->num_luns++;
172 devlist->cur_lun = cur_lun;
173
174 STAILQ_INIT(&cur_lun->attr_list);
175 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
176
177 for (i = 0; attr[i] != NULL; i += 2) {
178 if (strcmp(attr[i], "id") == 0) {
179 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
180 } else {
181 log_errx(1, "%s: invalid LUN attribute %s = %s",
182 __func__, attr[i], attr[i+1]);
183 }
184 }
185 }
186}
187
188static void
189cctl_end_element(void *user_data, const char *name)
190{
191 struct cctl_devlist_data *devlist;
192 struct cctl_lun *cur_lun;
193 char *str;
194
195 devlist = (struct cctl_devlist_data *)user_data;
196 cur_lun = devlist->cur_lun;
197
198 if ((cur_lun == NULL)
199 && (strcmp(name, "ctllunlist") != 0))
200 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
201
202 if (devlist->cur_sb[devlist->level] == NULL)
203 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
204 devlist->level, name);
205
206 sbuf_finish(devlist->cur_sb[devlist->level]);
207 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
208
209 if (strlen(str) == 0) {
210 free(str);
211 str = NULL;
212 }
213
214 sbuf_delete(devlist->cur_sb[devlist->level]);
215 devlist->cur_sb[devlist->level] = NULL;
216 devlist->level--;
217
218 if (strcmp(name, "backend_type") == 0) {
219 cur_lun->backend_type = str;
220 str = NULL;
221 } else if (strcmp(name, "size") == 0) {
222 cur_lun->size_blocks = strtoull(str, NULL, 0);
223 } else if (strcmp(name, "blocksize") == 0) {
224 cur_lun->blocksize = strtoul(str, NULL, 0);
225 } else if (strcmp(name, "serial_number") == 0) {
226 cur_lun->serial_number = str;
227 str = NULL;
228 } else if (strcmp(name, "device_id") == 0) {
229 cur_lun->device_id = str;
230 str = NULL;
231 } else if (strcmp(name, "ctld_name") == 0) {
232 cur_lun->ctld_name = str;
233 str = NULL;
234 } else if (strcmp(name, "lun") == 0) {
235 devlist->cur_lun = NULL;
236 } else if (strcmp(name, "ctllunlist") == 0) {
237 /* Nothing. */
238 } else {
239 struct cctl_lun_nv *nv;
240
241 nv = calloc(1, sizeof(*nv));
242 if (nv == NULL)
243 log_err(1, "%s: can't allocate %zd bytes for nv pair",
244 __func__, sizeof(*nv));
245
246 nv->name = checked_strdup(name);
247
248 nv->value = str;
249 str = NULL;
250 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
251 }
252
253 free(str);
254}
255
256static void
257cctl_start_pelement(void *user_data, const char *name, const char **attr)
258{
259 int i;
260 struct cctl_devlist_data *devlist;
261 struct cctl_port *cur_port;
262
263 devlist = (struct cctl_devlist_data *)user_data;
264 cur_port = devlist->cur_port;
265 devlist->level++;
266 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
267 sizeof(devlist->cur_sb[0])))
268 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
269 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
270
271 devlist->cur_sb[devlist->level] = sbuf_new_auto();
272 if (devlist->cur_sb[devlist->level] == NULL)
273 log_err(1, "%s: unable to allocate sbuf", __func__);
274
275 if (strcmp(name, "targ_port") == 0) {
276 if (cur_port != NULL)
277 log_errx(1, "%s: improper port element nesting (%s)",
278 __func__, name);
279
280 cur_port = calloc(1, sizeof(*cur_port));
281 if (cur_port == NULL)
282 log_err(1, "%s: cannot allocate %zd bytes", __func__,
283 sizeof(*cur_port));
284
285 devlist->num_ports++;
286 devlist->cur_port = cur_port;
287
288 STAILQ_INIT(&cur_port->attr_list);
289 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
290
291 for (i = 0; attr[i] != NULL; i += 2) {
292 if (strcmp(attr[i], "id") == 0) {
293 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
294 } else {
295 log_errx(1, "%s: invalid LUN attribute %s = %s",
296 __func__, attr[i], attr[i+1]);
297 }
298 }
299 }
300}
301
302static void
303cctl_end_pelement(void *user_data, const char *name)
304{
305 struct cctl_devlist_data *devlist;
306 struct cctl_port *cur_port;
307 char *str;
308
309 devlist = (struct cctl_devlist_data *)user_data;
310 cur_port = devlist->cur_port;
311
312 if ((cur_port == NULL)
313 && (strcmp(name, "ctlportlist") != 0))
314 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
315
316 if (devlist->cur_sb[devlist->level] == NULL)
317 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
318 devlist->level, name);
319
320 sbuf_finish(devlist->cur_sb[devlist->level]);
321 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
322
323 if (strlen(str) == 0) {
324 free(str);
325 str = NULL;
326 }
327
328 sbuf_delete(devlist->cur_sb[devlist->level]);
329 devlist->cur_sb[devlist->level] = NULL;
330 devlist->level--;
331
332 if (strcmp(name, "cfiscsi_target") == 0) {
333 cur_port->cfiscsi_target = str;
334 str = NULL;
335 } else if (strcmp(name, "cfiscsi_status") == 0) {
336 cur_port->cfiscsi_status = strtoul(str, NULL, 0);
337 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
338 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
339 } else if (strcmp(name, "targ_port") == 0) {
340 devlist->cur_port = NULL;
341 } else if (strcmp(name, "ctlportlist") == 0) {
342 /* Nothing. */
343 } else {
344 struct cctl_lun_nv *nv;
345
346 nv = calloc(1, sizeof(*nv));
347 if (nv == NULL)
348 log_err(1, "%s: can't allocate %zd bytes for nv pair",
349 __func__, sizeof(*nv));
350
351 nv->name = checked_strdup(name);
352
353 nv->value = str;
354 str = NULL;
355 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
356 }
357
358 free(str);
359}
360
361static void
362cctl_char_handler(void *user_data, const XML_Char *str, int len)
363{
364 struct cctl_devlist_data *devlist;
365
366 devlist = (struct cctl_devlist_data *)user_data;
367
368 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
369}
370
371struct conf *
372conf_new_from_kernel(void)
373{
374 struct conf *conf = NULL;
375 struct target *targ;
376 struct lun *cl;
377 struct lun_option *lo;
378 struct ctl_lun_list list;
379 struct cctl_devlist_data devlist;
380 struct cctl_lun *lun;
381 struct cctl_port *port;
382 XML_Parser parser;
383 char *str;
384 int len, retval;
385
386 bzero(&devlist, sizeof(devlist));
387 STAILQ_INIT(&devlist.lun_list);
388 STAILQ_INIT(&devlist.port_list);
389
390 log_debugx("obtaining previously configured CTL luns from the kernel");
391
392 str = NULL;
393 len = 4096;
394retry:
395 str = realloc(str, len);
396 if (str == NULL)
397 log_err(1, "realloc");
398
399 bzero(&list, sizeof(list));
400 list.alloc_len = len;
401 list.status = CTL_LUN_LIST_NONE;
402 list.lun_xml = str;
403
404 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
405 log_warn("error issuing CTL_LUN_LIST ioctl");
406 free(str);
407 return (NULL);
408 }
409
410 if (list.status == CTL_LUN_LIST_ERROR) {
411 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
412 list.error_str);
413 free(str);
414 return (NULL);
415 }
416
417 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
418 len = len << 1;
419 goto retry;
420 }
421
422 parser = XML_ParserCreate(NULL);
423 if (parser == NULL) {
424 log_warnx("unable to create XML parser");
425 free(str);
426 return (NULL);
427 }
428
429 XML_SetUserData(parser, &devlist);
430 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
431 XML_SetCharacterDataHandler(parser, cctl_char_handler);
432
433 retval = XML_Parse(parser, str, strlen(str), 1);
434 XML_ParserFree(parser);
435 free(str);
436 if (retval != 1) {
437 log_warnx("XML_Parse failed");
438 return (NULL);
439 }
440
441 str = NULL;
442 len = 4096;
443retry_port:
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_PORT_LIST, &list) == -1) {
454 log_warn("error issuing CTL_PORT_LIST ioctl");
455 free(str);
456 return (NULL);
457 }
458
459 if (list.status == CTL_PORT_LIST_ERROR) {
460 log_warnx("error returned from CTL_PORT_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_port;
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_pelement, cctl_end_pelement);
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 conf = conf_new();
491
492 STAILQ_FOREACH(port, &devlist.port_list, links) {
493
494 if (port->cfiscsi_target == NULL) {
495 log_debugx("CTL port %ju wasn't managed by ctld; "
496 "ignoring", (uintmax_t)port->port_id);
497 continue;
498 }
499 if (port->cfiscsi_status != 1) {
500 log_debugx("CTL port %ju is not active (%d); ignoring",
501 (uintmax_t)port->port_id, port->cfiscsi_status);
502 continue;
503 }
504
505 targ = target_find(conf, port->cfiscsi_target);
506 if (targ == NULL) {
507#if 0
508 log_debugx("found new kernel target %s for CTL port %ld",
509 port->cfiscsi_target, port->port_id);
510#endif
511 targ = target_new(conf, port->cfiscsi_target);
512 if (targ == NULL) {
513 log_warnx("target_new failed");
514 continue;
515 }
516 }
517 }
518
519 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
520 struct cctl_lun_nv *nv;
521
522 if (lun->ctld_name == NULL) {
523 log_debugx("CTL lun %ju wasn't managed by ctld; "
524 "ignoring", (uintmax_t)lun->lun_id);
525 continue;
526 }
527
528 cl = lun_find(conf, lun->ctld_name);
529 if (cl != NULL) {
530 log_warnx("found CTL lun %ju \"%s\", "
531 "also backed by CTL lun %d; ignoring",
532 (uintmax_t)lun->lun_id, lun->ctld_name,
533 cl->l_ctl_lun);
534 continue;
535 }
536
537 log_debugx("found CTL lun %ju \"%s\"",
538 (uintmax_t)lun->lun_id, lun->ctld_name);
539
540 cl = lun_new(conf, lun->ctld_name);
541 if (cl == NULL) {
542 log_warnx("lun_new failed");
543 continue;
544 }
545 lun_set_backend(cl, lun->backend_type);
546 lun_set_blocksize(cl, lun->blocksize);
547 lun_set_device_id(cl, lun->device_id);
548 lun_set_serial(cl, lun->serial_number);
549 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
550 lun_set_ctl_lun(cl, lun->lun_id);
551
552 STAILQ_FOREACH(nv, &lun->attr_list, links) {
553 if (strcmp(nv->name, "file") == 0 ||
554 strcmp(nv->name, "dev") == 0) {
555 lun_set_path(cl, nv->value);
556 continue;
557 }
558 lo = lun_option_new(cl, nv->name, nv->value);
559 if (lo == NULL)
560 log_warnx("unable to add CTL lun option %s "
561 "for CTL lun %ju \"%s\"",
562 nv->name, (uintmax_t) lun->lun_id,
563 cl->l_name);
564 }
565 }
566
567 return (conf);
568}
569
570static void
571str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
572{
573
574 arg->namelen = strlen(name) + 1;
575 arg->name = __DECONST(char *, name);
576 arg->vallen = strlen(value) + 1;
577 arg->value = __DECONST(char *, value);
578 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
579}
580
581int
582kernel_lun_add(struct lun *lun)
583{
584 struct lun_option *lo;
585 struct ctl_lun_req req;
586 int error, i, num_options;
587
588 bzero(&req, sizeof(req));
589
590 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
591 req.reqtype = CTL_LUNREQ_CREATE;
592
593 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
594
595 if (lun->l_size != 0)
596 req.reqdata.create.lun_size_bytes = lun->l_size;
597
598 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
599 req.reqdata.create.device_type = T_DIRECT;
600
601 if (lun->l_serial != NULL) {
602 strncpy(req.reqdata.create.serial_num, lun->l_serial,
603 sizeof(req.reqdata.create.serial_num));
604 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
605 }
606
607 if (lun->l_device_id != NULL) {
608 strncpy(req.reqdata.create.device_id, lun->l_device_id,
609 sizeof(req.reqdata.create.device_id));
610 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
611 }
612
613 if (lun->l_path != NULL) {
614 lo = lun_option_find(lun, "file");
615 if (lo != NULL) {
616 lun_option_set(lo, lun->l_path);
617 } else {
618 lo = lun_option_new(lun, "file", lun->l_path);
619 assert(lo != NULL);
620 }
621 }
622
623 lo = lun_option_find(lun, "ctld_name");
624 if (lo != NULL) {
625 lun_option_set(lo, lun->l_name);
626 } else {
627 lo = lun_option_new(lun, "ctld_name", lun->l_name);
628 assert(lo != NULL);
629 }
630
631 lo = lun_option_find(lun, "scsiname");
632 if (lo == NULL && lun->l_scsiname != NULL) {
633 lo = lun_option_new(lun, "scsiname", lun->l_scsiname);
634 assert(lo != NULL);
635 }
636
637 num_options = 0;
638 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
639 num_options++;
640
641 req.num_be_args = num_options;
642 if (num_options > 0) {
643 req.be_args = malloc(num_options * sizeof(*req.be_args));
644 if (req.be_args == NULL) {
645 log_warn("error allocating %zd bytes",
646 num_options * sizeof(*req.be_args));
647 return (1);
648 }
649
650 i = 0;
651 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
652 str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
653 i++;
654 }
655 assert(i == num_options);
656 }
657
658 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
659 free(req.be_args);
660 if (error != 0) {
661 log_warn("error issuing CTL_LUN_REQ ioctl");
662 return (1);
663 }
664
665 switch (req.status) {
666 case CTL_LUN_ERROR:
667 log_warnx("LUN creation error: %s", req.error_str);
668 return (1);
669 case CTL_LUN_WARNING:
670 log_warnx("LUN creation warning: %s", req.error_str);
671 break;
672 case CTL_LUN_OK:
673 break;
674 default:
675 log_warnx("unknown LUN creation status: %d",
676 req.status);
677 return (1);
678 }
679
680 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
681 return (0);
682}
683
684int
685kernel_lun_resize(struct lun *lun)
686{
687 struct ctl_lun_req req;
688
689 bzero(&req, sizeof(req));
690
691 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
692 req.reqtype = CTL_LUNREQ_MODIFY;
693
694 req.reqdata.modify.lun_id = lun->l_ctl_lun;
695 req.reqdata.modify.lun_size_bytes = lun->l_size;
696
697 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
698 log_warn("error issuing CTL_LUN_REQ ioctl");
699 return (1);
700 }
701
702 switch (req.status) {
703 case CTL_LUN_ERROR:
704 log_warnx("LUN modification error: %s", req.error_str);
705 return (1);
706 case CTL_LUN_WARNING:
707 log_warnx("LUN modification warning: %s", req.error_str);
708 break;
709 case CTL_LUN_OK:
710 break;
711 default:
712 log_warnx("unknown LUN modification status: %d",
713 req.status);
714 return (1);
715 }
716
717 return (0);
718}
719
720int
721kernel_lun_remove(struct lun *lun)
722{
723 struct ctl_lun_req req;
724
725 bzero(&req, sizeof(req));
726
727 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
728 req.reqtype = CTL_LUNREQ_RM;
729
730 req.reqdata.rm.lun_id = lun->l_ctl_lun;
731
732 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
733 log_warn("error issuing CTL_LUN_REQ ioctl");
734 return (1);
735 }
736
737 switch (req.status) {
738 case CTL_LUN_ERROR:
739 log_warnx("LUN removal error: %s", req.error_str);
740 return (1);
741 case CTL_LUN_WARNING:
742 log_warnx("LUN removal warning: %s", req.error_str);
743 break;
744 case CTL_LUN_OK:
745 break;
746 default:
747 log_warnx("unknown LUN removal status: %d", req.status);
748 return (1);
749 }
750
751 return (0);
752}
753
754void
755kernel_handoff(struct connection *conn)
756{
757 struct ctl_iscsi req;
758
759 bzero(&req, sizeof(req));
760
761 req.type = CTL_ISCSI_HANDOFF;
762 strlcpy(req.data.handoff.initiator_name,
763 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
764 strlcpy(req.data.handoff.initiator_addr,
765 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
766 if (conn->conn_initiator_alias != NULL) {
767 strlcpy(req.data.handoff.initiator_alias,
768 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
769 }
770 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
771 sizeof(req.data.handoff.initiator_isid));
772 strlcpy(req.data.handoff.target_name,
773 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
774#ifdef ICL_KERNEL_PROXY
775 if (proxy_mode)
776 req.data.handoff.connection_id = conn->conn_socket;
777 else
778 req.data.handoff.socket = conn->conn_socket;
779#else
780 req.data.handoff.socket = conn->conn_socket;
781#endif
782 req.data.handoff.portal_group_tag =
783 conn->conn_portal->p_portal_group->pg_tag;
784 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
785 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
786 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
787 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
788 req.data.handoff.cmdsn = conn->conn_cmdsn;
789 req.data.handoff.statsn = conn->conn_statsn;
790 req.data.handoff.max_recv_data_segment_length =
791 conn->conn_max_data_segment_length;
792 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
793 req.data.handoff.immediate_data = conn->conn_immediate_data;
794
795 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
796 log_err(1, "error issuing CTL_ISCSI ioctl; "
797 "dropping connection");
798 }
799
800 if (req.status != CTL_ISCSI_OK) {
801 log_errx(1, "error returned from CTL iSCSI handoff request: "
802 "%s; dropping connection", req.error_str);
803 }
804}
805
806int
807kernel_port_add(struct target *targ)
808{
809 struct ctl_port_entry entry;
810 struct ctl_req req;
811 struct ctl_lun_map lm;
812 char tagstr[16];
813 int error, i;
814
815 /* Create iSCSI port. */
816 bzero(&req, sizeof(req));
817 strlcpy(req.driver, "iscsi", sizeof(req.driver));
818 req.reqtype = CTL_REQ_CREATE;
819 req.num_args = 4;
820 req.args = malloc(req.num_args * sizeof(*req.args));
821 req.args[0].namelen = sizeof("port_id");
822 req.args[0].name = __DECONST(char *, "port_id");
823 req.args[0].vallen = sizeof(targ->t_ctl_port);
824 req.args[0].value = &targ->t_ctl_port;
825 req.args[0].flags = CTL_BEARG_WR;
826 str_arg(&req.args[1], "cfiscsi_target", targ->t_name);
827 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
828 str_arg(&req.args[2], "cfiscsi_portal_group_tag", tagstr);
829 if (targ->t_alias)
830 str_arg(&req.args[3], "cfiscsi_target_alias", targ->t_alias);
831 else
832 req.num_args--;
833 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
834 free(req.args);
835 if (error != 0) {
836 log_warn("error issuing CTL_PORT_REQ ioctl");
837 return (1);
838 }
839 if (req.status == CTL_LUN_ERROR) {
840 log_warnx("error returned from port creation request: %s",
841 req.error_str);
842 return (1);
843 }
844 if (req.status != CTL_LUN_OK) {
845 log_warnx("unknown port creation request status %d",
846 req.status);
847 return (1);
848 }
849
850 /* Explicitly enable mapping to block any access except allowed. */
851 lm.port = targ->t_ctl_port;
852 lm.plun = UINT32_MAX;
853 lm.lun = 0;
854 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
855 if (error != 0)
856 log_warn("CTL_LUN_MAP ioctl failed");
857
858 /* Map configured LUNs */
859 for (i = 0; i < MAX_LUNS; i++) {
860 if (targ->t_luns[i] == NULL)
861 continue;
862 lm.port = targ->t_ctl_port;
863 lm.plun = i;
864 lm.lun = targ->t_luns[i]->l_ctl_lun;
865 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
866 if (error != 0)
867 log_warn("CTL_LUN_MAP ioctl failed");
868 }
869
870 /* Enable port */
871 bzero(&entry, sizeof(entry));
872 entry.targ_port = targ->t_ctl_port;
873 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
874 if (error != 0) {
875 log_warn("CTL_ENABLE_PORT ioctl failed");
876 return (-1);
877 }
878
879 return (0);
880}
881
882int
883kernel_port_update(struct target *targ)
884{
885 struct ctl_lun_map lm;
886 int error, i;
887
888 /* Map configured LUNs and unmap others */
889 for (i = 0; i < MAX_LUNS; i++) {
890 lm.port = targ->t_ctl_port;
891 lm.plun = i;
892 if (targ->t_luns[i] == NULL)
893 lm.lun = UINT32_MAX;
894 else
895 lm.lun = targ->t_luns[i]->l_ctl_lun;
896 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
897 if (error != 0)
898 log_warn("CTL_LUN_MAP ioctl failed");
899 }
900 return (0);
901}
902
903int
904kernel_port_remove(struct target *targ)
905{
906 struct ctl_req req;
907 char tagstr[16];
908 int error;
909
910 bzero(&req, sizeof(req));
911 strlcpy(req.driver, "iscsi", sizeof(req.driver));
912 req.reqtype = CTL_REQ_REMOVE;
913 req.num_args = 2;
914 req.args = malloc(req.num_args * sizeof(*req.args));
915 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
916 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
917 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
918
919 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
920 free(req.args);
921 if (error != 0) {
922 log_warn("error issuing CTL_PORT_REQ ioctl");
923 return (1);
924 }
925
926 if (req.status == CTL_LUN_ERROR) {
927 log_warnx("error returned from port removal request: %s",
928 req.error_str);
929 return (1);
930 }
931
932 if (req.status != CTL_LUN_OK) {
933 log_warnx("unknown port removal request status %d",
934 req.status);
935 return (1);
936 }
937
938 return (0);
939}
940
941#ifdef ICL_KERNEL_PROXY
942void
943kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
944{
945 struct ctl_iscsi req;
946
947 bzero(&req, sizeof(req));
948
949 req.type = CTL_ISCSI_LISTEN;
950 req.data.listen.iser = iser;
951 req.data.listen.domain = ai->ai_family;
952 req.data.listen.socktype = ai->ai_socktype;
953 req.data.listen.protocol = ai->ai_protocol;
954 req.data.listen.addr = ai->ai_addr;
955 req.data.listen.addrlen = ai->ai_addrlen;
956 req.data.listen.portal_id = portal_id;
957
958 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
959 log_err(1, "error issuing CTL_ISCSI ioctl");
960
961 if (req.status != CTL_ISCSI_OK) {
962 log_errx(1, "error returned from CTL iSCSI listen: %s",
963 req.error_str);
964 }
965}
966
967void
968kernel_accept(int *connection_id, int *portal_id,
969 struct sockaddr *client_sa, socklen_t *client_salen)
970{
971 struct ctl_iscsi req;
972 struct sockaddr_storage ss;
973
974 bzero(&req, sizeof(req));
975
976 req.type = CTL_ISCSI_ACCEPT;
977 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
978
979 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
980 log_err(1, "error issuing CTL_ISCSI ioctl");
981
982 if (req.status != CTL_ISCSI_OK) {
983 log_errx(1, "error returned from CTL iSCSI accept: %s",
984 req.error_str);
985 }
986
987 *connection_id = req.data.accept.connection_id;
988 *portal_id = req.data.accept.portal_id;
989 *client_salen = req.data.accept.initiator_addrlen;
990 memcpy(client_sa, &ss, *client_salen);
991}
992
993void
994kernel_send(struct pdu *pdu)
995{
996 struct ctl_iscsi req;
997
998 bzero(&req, sizeof(req));
999
1000 req.type = CTL_ISCSI_SEND;
1001 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1002 req.data.send.bhs = pdu->pdu_bhs;
1003 req.data.send.data_segment_len = pdu->pdu_data_len;
1004 req.data.send.data_segment = pdu->pdu_data;
1005
1006 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1007 log_err(1, "error issuing CTL_ISCSI ioctl; "
1008 "dropping connection");
1009 }
1010
1011 if (req.status != CTL_ISCSI_OK) {
1012 log_errx(1, "error returned from CTL iSCSI send: "
1013 "%s; dropping connection", req.error_str);
1014 }
1015}
1016
1017void
1018kernel_receive(struct pdu *pdu)
1019{
1020 struct ctl_iscsi req;
1021
1022 pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1023 if (pdu->pdu_data == NULL)
1024 log_err(1, "malloc");
1025
1026 bzero(&req, sizeof(req));
1027
1028 req.type = CTL_ISCSI_RECEIVE;
1029 req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1030 req.data.receive.bhs = pdu->pdu_bhs;
1031 req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1032 req.data.receive.data_segment = pdu->pdu_data;
1033
1034 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1035 log_err(1, "error issuing CTL_ISCSI ioctl; "
1036 "dropping connection");
1037 }
1038
1039 if (req.status != CTL_ISCSI_OK) {
1040 log_errx(1, "error returned from CTL iSCSI receive: "
1041 "%s; dropping connection", req.error_str);
1042 }
1043
1044}
1045
1046#endif /* ICL_KERNEL_PROXY */
1047
1048/*
1049 * XXX: I CANT INTO LATIN
1050 */
1051void
1052kernel_capsicate(void)
1053{
1054 int error;
1055 cap_rights_t rights;
1056 const unsigned long cmds[] = { CTL_ISCSI };
1057
1058 cap_rights_init(&rights, CAP_IOCTL);
1059 error = cap_rights_limit(ctl_fd, &rights);
1060 if (error != 0 && errno != ENOSYS)
1061 log_err(1, "cap_rights_limit");
1062
1063 error = cap_ioctls_limit(ctl_fd, cmds,
1064 sizeof(cmds) / sizeof(cmds[0]));
1065 if (error != 0 && errno != ENOSYS)
1066 log_err(1, "cap_ioctls_limit");
1067
1068 error = cap_enter();
1069 if (error != 0 && errno != ENOSYS)
1070 log_err(1, "cap_enter");
1071
1072 if (cap_sandboxed())
1073 log_debugx("Capsicum capability mode enabled");
1074 else
1075 log_warnx("Capsicum capability mode not supported");
1076}
1077