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