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