]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ctld/kernel.c
Add UPDATING entries and bump version.
[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 (lun->l_path != NULL) {
780                 o = option_find(&lun->l_options, "file");
781                 if (o != NULL) {
782                         option_set(o, lun->l_path);
783                 } else {
784                         o = option_new(&lun->l_options, "file", lun->l_path);
785                         assert(o != NULL);
786                 }
787         }
788
789         o = option_find(&lun->l_options, "ctld_name");
790         if (o != NULL) {
791                 option_set(o, lun->l_name);
792         } else {
793                 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
794                 assert(o != NULL);
795         }
796
797         o = option_find(&lun->l_options, "scsiname");
798         if (o == NULL && lun->l_scsiname != NULL) {
799                 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
800                 assert(o != NULL);
801         }
802
803         if (!TAILQ_EMPTY(&lun->l_options)) {
804                 req.args_nvl = nvlist_create(0);
805                 if (req.args_nvl == NULL) {
806                         log_warn("error allocating nvlist");
807                         return (1);
808                 }
809
810                 TAILQ_FOREACH(o, &lun->l_options, o_next)
811                         nvlist_add_string(req.args_nvl, o->o_name, o->o_value);
812
813                 req.args = nvlist_pack(req.args_nvl, &req.args_len);
814                 if (req.args == NULL) {
815                         log_warn("error packing nvlist");
816                         return (1);
817                 }
818         }
819
820         error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
821         nvlist_destroy(req.args_nvl);
822
823         if (error != 0) {
824                 log_warn("error issuing CTL_LUN_REQ ioctl");
825                 return (1);
826         }
827
828         switch (req.status) {
829         case CTL_LUN_ERROR:
830                 log_warnx("LUN modification error: %s", req.error_str);
831                 return (1);
832         case CTL_LUN_WARNING:
833                 log_warnx("LUN modification warning: %s", req.error_str);
834                 break;
835         case CTL_LUN_OK:
836                 break;
837         default:
838                 log_warnx("unknown LUN modification status: %d",
839                     req.status);
840                 return (1);
841         }
842
843         return (0);
844 }
845
846 int
847 kernel_lun_remove(struct lun *lun)
848 {
849         struct ctl_lun_req req;
850
851         bzero(&req, sizeof(req));
852
853         strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
854         req.reqtype = CTL_LUNREQ_RM;
855
856         req.reqdata.rm.lun_id = lun->l_ctl_lun;
857
858         if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
859                 log_warn("error issuing CTL_LUN_REQ ioctl");
860                 return (1);
861         }
862
863         switch (req.status) {
864         case CTL_LUN_ERROR:
865                 log_warnx("LUN removal error: %s", req.error_str);
866                 return (1);
867         case CTL_LUN_WARNING:
868                 log_warnx("LUN removal warning: %s", req.error_str);
869                 break;
870         case CTL_LUN_OK:
871                 break;
872         default:
873                 log_warnx("unknown LUN removal status: %d", req.status);
874                 return (1);
875         }
876
877         return (0);
878 }
879
880 void
881 kernel_handoff(struct connection *conn)
882 {
883         struct ctl_iscsi req;
884
885         bzero(&req, sizeof(req));
886
887         req.type = CTL_ISCSI_HANDOFF;
888         strlcpy(req.data.handoff.initiator_name,
889             conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
890         strlcpy(req.data.handoff.initiator_addr,
891             conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
892         if (conn->conn_initiator_alias != NULL) {
893                 strlcpy(req.data.handoff.initiator_alias,
894                     conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
895         }
896         memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
897             sizeof(req.data.handoff.initiator_isid));
898         strlcpy(req.data.handoff.target_name,
899             conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
900         if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
901                 strlcpy(req.data.handoff.offload,
902                     conn->conn_portal->p_portal_group->pg_offload,
903                     sizeof(req.data.handoff.offload));
904         }
905 #ifdef ICL_KERNEL_PROXY
906         if (proxy_mode)
907                 req.data.handoff.connection_id = conn->conn_socket;
908         else
909                 req.data.handoff.socket = conn->conn_socket;
910 #else
911         req.data.handoff.socket = conn->conn_socket;
912 #endif
913         req.data.handoff.portal_group_tag =
914             conn->conn_portal->p_portal_group->pg_tag;
915         if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
916                 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
917         if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
918                 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
919         req.data.handoff.cmdsn = conn->conn_cmdsn;
920         req.data.handoff.statsn = conn->conn_statsn;
921         req.data.handoff.max_recv_data_segment_length =
922             conn->conn_max_recv_data_segment_length;
923         req.data.handoff.max_send_data_segment_length =
924             conn->conn_max_send_data_segment_length;
925         req.data.handoff.max_burst_length = conn->conn_max_burst_length;
926         req.data.handoff.first_burst_length = conn->conn_first_burst_length;
927         req.data.handoff.immediate_data = conn->conn_immediate_data;
928
929         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
930                 log_err(1, "error issuing CTL_ISCSI ioctl; "
931                     "dropping connection");
932         }
933
934         if (req.status != CTL_ISCSI_OK) {
935                 log_errx(1, "error returned from CTL iSCSI handoff request: "
936                     "%s; dropping connection", req.error_str);
937         }
938 }
939
940 void
941 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
942     int *max_burst_length, int *first_burst_length)
943 {
944         struct ctl_iscsi req;
945         struct ctl_iscsi_limits_params *cilp;
946
947         bzero(&req, sizeof(req));
948
949         req.type = CTL_ISCSI_LIMITS;
950         cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
951         if (offload != NULL) {
952                 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
953         }
954
955         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
956                 log_err(1, "error issuing CTL_ISCSI ioctl; "
957                     "dropping connection");
958         }
959
960         if (req.status != CTL_ISCSI_OK) {
961                 log_errx(1, "error returned from CTL iSCSI limits request: "
962                     "%s; dropping connection", req.error_str);
963         }
964
965         if (cilp->max_recv_data_segment_length != 0) {
966                 *max_recv_dsl = cilp->max_recv_data_segment_length;
967                 *max_send_dsl = cilp->max_recv_data_segment_length;
968         }
969         if (cilp->max_send_data_segment_length != 0)
970                 *max_send_dsl = cilp->max_send_data_segment_length;
971         if (cilp->max_burst_length != 0)
972                 *max_burst_length = cilp->max_burst_length;
973         if (cilp->first_burst_length != 0)
974                 *first_burst_length = cilp->first_burst_length;
975         if (*max_burst_length < *first_burst_length)
976                 *first_burst_length = *max_burst_length;
977
978         if (offload != NULL) {
979                 log_debugx("Kernel limits for offload \"%s\" are "
980                     "MaxRecvDataSegment=%d, max_send_dsl=%d, "
981                     "MaxBurstLength=%d, FirstBurstLength=%d",
982                     offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
983                     *first_burst_length);
984         } else {
985                 log_debugx("Kernel limits are "
986                     "MaxRecvDataSegment=%d, max_send_dsl=%d, "
987                     "MaxBurstLength=%d, FirstBurstLength=%d",
988                     *max_recv_dsl, *max_send_dsl, *max_burst_length,
989                     *first_burst_length);
990         }
991 }
992
993 int
994 kernel_port_add(struct port *port)
995 {
996         struct option *o;
997         struct ctl_port_entry entry;
998         struct ctl_req req;
999         struct ctl_lun_map lm;
1000         struct target *targ = port->p_target;
1001         struct portal_group *pg = port->p_portal_group;
1002         char result_buf[NVLIST_BUFSIZE];
1003         int error, i;
1004
1005         /* Create iSCSI port. */
1006         if (port->p_portal_group || port->p_ioctl_port) {
1007                 bzero(&req, sizeof(req));
1008                 req.reqtype = CTL_REQ_CREATE;
1009
1010                 if (port->p_portal_group) {
1011                         strlcpy(req.driver, "iscsi", sizeof(req.driver));
1012                         req.args_nvl = nvlist_create(0);
1013                         nvlist_add_string(req.args_nvl, "cfiscsi_target",
1014                             targ->t_name);
1015                         nvlist_add_string(req.args_nvl,
1016                             "ctld_portal_group_name", pg->pg_name);
1017                         nvlist_add_stringf(req.args_nvl,
1018                             "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1019
1020                         if (targ->t_alias) {
1021                                 nvlist_add_string(req.args_nvl,
1022                                     "cfiscsi_target_alias", targ->t_alias);
1023                         }
1024
1025                         TAILQ_FOREACH(o, &pg->pg_options, o_next)
1026                                 nvlist_add_string(req.args_nvl, o->o_name,
1027                                     o->o_value);
1028                 }
1029
1030                 if (port->p_ioctl_port) {
1031                         strlcpy(req.driver, "ioctl", sizeof(req.driver));
1032                         req.args_nvl = nvlist_create(0);
1033                         nvlist_add_stringf(req.args_nvl, "pp", "%d",
1034                             port->p_ioctl_pp);
1035                         nvlist_add_stringf(req.args_nvl, "vp", "%d",
1036                             port->p_ioctl_vp);
1037                 }
1038
1039                 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1040                 if (req.args == NULL) {
1041                         log_warn("error packing nvlist");
1042                         return (1);
1043                 }
1044
1045                 req.result = result_buf;
1046                 req.result_len = sizeof(result_buf);
1047                 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1048                 nvlist_destroy(req.args_nvl);
1049
1050                 if (error != 0) {
1051                         log_warn("error issuing CTL_PORT_REQ ioctl");
1052                         return (1);
1053                 }
1054                 if (req.status == CTL_LUN_ERROR) {
1055                         log_warnx("error returned from port creation request: %s",
1056                             req.error_str);
1057                         return (1);
1058                 }
1059                 if (req.status != CTL_LUN_OK) {
1060                         log_warnx("unknown port creation request status %d",
1061                             req.status);
1062                         return (1);
1063                 }
1064
1065                 req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0);
1066                 if (req.result_nvl == NULL) {
1067                         log_warnx("error unpacking result nvlist");
1068                         return (1);
1069                 }
1070
1071                 port->p_ctl_port = nvlist_get_number(req.result_nvl, "port_id");
1072                 nvlist_destroy(req.result_nvl);
1073         } else if (port->p_pport) {
1074                 port->p_ctl_port = port->p_pport->pp_ctl_port;
1075
1076                 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1077                     strlen(targ->t_name) == 20) {
1078                         bzero(&entry, sizeof(entry));
1079                         entry.port_type = CTL_PORT_NONE;
1080                         entry.targ_port = port->p_ctl_port;
1081                         entry.flags |= CTL_PORT_WWNN_VALID;
1082                         entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1083                         if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1084                                 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1085                 }
1086         }
1087
1088         /* Explicitly enable mapping to block any access except allowed. */
1089         lm.port = port->p_ctl_port;
1090         lm.plun = UINT32_MAX;
1091         lm.lun = 0;
1092         error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1093         if (error != 0)
1094                 log_warn("CTL_LUN_MAP ioctl failed");
1095
1096         /* Map configured LUNs */
1097         for (i = 0; i < MAX_LUNS; i++) {
1098                 if (targ->t_luns[i] == NULL)
1099                         continue;
1100                 lm.port = port->p_ctl_port;
1101                 lm.plun = i;
1102                 lm.lun = targ->t_luns[i]->l_ctl_lun;
1103                 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1104                 if (error != 0)
1105                         log_warn("CTL_LUN_MAP ioctl failed");
1106         }
1107
1108         /* Enable port */
1109         bzero(&entry, sizeof(entry));
1110         entry.targ_port = port->p_ctl_port;
1111         error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1112         if (error != 0) {
1113                 log_warn("CTL_ENABLE_PORT ioctl failed");
1114                 return (-1);
1115         }
1116
1117         return (0);
1118 }
1119
1120 int
1121 kernel_port_update(struct port *port, struct port *oport)
1122 {
1123         struct ctl_lun_map lm;
1124         struct target *targ = port->p_target;
1125         struct target *otarg = oport->p_target;
1126         int error, i;
1127         uint32_t olun;
1128
1129         /* Map configured LUNs and unmap others */
1130         for (i = 0; i < MAX_LUNS; i++) {
1131                 lm.port = port->p_ctl_port;
1132                 lm.plun = i;
1133                 if (targ->t_luns[i] == NULL)
1134                         lm.lun = UINT32_MAX;
1135                 else
1136                         lm.lun = targ->t_luns[i]->l_ctl_lun;
1137                 if (otarg->t_luns[i] == NULL)
1138                         olun = UINT32_MAX;
1139                 else
1140                         olun = otarg->t_luns[i]->l_ctl_lun;
1141                 if (lm.lun == olun)
1142                         continue;
1143                 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1144                 if (error != 0)
1145                         log_warn("CTL_LUN_MAP ioctl failed");
1146         }
1147         return (0);
1148 }
1149
1150 int
1151 kernel_port_remove(struct port *port)
1152 {
1153         struct ctl_port_entry entry;
1154         struct ctl_lun_map lm;
1155         struct ctl_req req;
1156         struct target *targ = port->p_target;
1157         struct portal_group *pg = port->p_portal_group;
1158         int error;
1159
1160         /* Disable port */
1161         bzero(&entry, sizeof(entry));
1162         entry.targ_port = port->p_ctl_port;
1163         error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1164         if (error != 0) {
1165                 log_warn("CTL_DISABLE_PORT ioctl failed");
1166                 return (-1);
1167         }
1168
1169         /* Remove iSCSI or ioctl port. */
1170         if (port->p_portal_group || port->p_ioctl_port) {
1171                 bzero(&req, sizeof(req));
1172                 strlcpy(req.driver, port->p_ioctl_port ? "ioctl" : "iscsi",
1173                     sizeof(req.driver));
1174                 req.reqtype = CTL_REQ_REMOVE;
1175                 req.args_nvl = nvlist_create(0);
1176                 if (req.args_nvl == NULL)
1177                         log_err(1, "nvlist_create");
1178
1179                 if (port->p_ioctl_port)
1180                         nvlist_add_stringf(req.args_nvl, "port_id", "%d",
1181                             port->p_ctl_port);
1182                 else {
1183                         nvlist_add_string(req.args_nvl, "cfiscsi_target",
1184                             targ->t_name);
1185                         nvlist_add_stringf(req.args_nvl,
1186                             "cfiscsi_portal_group_tag", "%u", pg->pg_tag);
1187                 }
1188
1189                 req.args = nvlist_pack(req.args_nvl, &req.args_len);
1190                 if (req.args == NULL) {
1191                         log_warn("error packing nvlist");
1192                         return (1);
1193                 }
1194
1195                 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1196                 nvlist_destroy(req.args_nvl);
1197
1198                 if (error != 0) {
1199                         log_warn("error issuing CTL_PORT_REQ ioctl");
1200                         return (1);
1201                 }
1202                 if (req.status == CTL_LUN_ERROR) {
1203                         log_warnx("error returned from port removal request: %s",
1204                             req.error_str);
1205                         return (1);
1206                 }
1207                 if (req.status != CTL_LUN_OK) {
1208                         log_warnx("unknown port removal request status %d",
1209                             req.status);
1210                         return (1);
1211                 }
1212         } else {
1213                 /* Disable LUN mapping. */
1214                 lm.port = port->p_ctl_port;
1215                 lm.plun = UINT32_MAX;
1216                 lm.lun = UINT32_MAX;
1217                 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1218                 if (error != 0)
1219                         log_warn("CTL_LUN_MAP ioctl failed");
1220         }
1221         return (0);
1222 }
1223
1224 #ifdef ICL_KERNEL_PROXY
1225 void
1226 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1227 {
1228         struct ctl_iscsi req;
1229
1230         bzero(&req, sizeof(req));
1231
1232         req.type = CTL_ISCSI_LISTEN;
1233         req.data.listen.iser = iser;
1234         req.data.listen.domain = ai->ai_family;
1235         req.data.listen.socktype = ai->ai_socktype;
1236         req.data.listen.protocol = ai->ai_protocol;
1237         req.data.listen.addr = ai->ai_addr;
1238         req.data.listen.addrlen = ai->ai_addrlen;
1239         req.data.listen.portal_id = portal_id;
1240
1241         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1242                 log_err(1, "error issuing CTL_ISCSI ioctl");
1243
1244         if (req.status != CTL_ISCSI_OK) {
1245                 log_errx(1, "error returned from CTL iSCSI listen: %s",
1246                     req.error_str);
1247         }
1248 }
1249
1250 void
1251 kernel_accept(int *connection_id, int *portal_id,
1252     struct sockaddr *client_sa, socklen_t *client_salen)
1253 {
1254         struct ctl_iscsi req;
1255         struct sockaddr_storage ss;
1256
1257         bzero(&req, sizeof(req));
1258
1259         req.type = CTL_ISCSI_ACCEPT;
1260         req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1261
1262         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1263                 log_err(1, "error issuing CTL_ISCSI ioctl");
1264
1265         if (req.status != CTL_ISCSI_OK) {
1266                 log_errx(1, "error returned from CTL iSCSI accept: %s",
1267                     req.error_str);
1268         }
1269
1270         *connection_id = req.data.accept.connection_id;
1271         *portal_id = req.data.accept.portal_id;
1272         *client_salen = req.data.accept.initiator_addrlen;
1273         memcpy(client_sa, &ss, *client_salen);
1274 }
1275
1276 void
1277 kernel_send(struct pdu *pdu)
1278 {
1279         struct ctl_iscsi req;
1280
1281         bzero(&req, sizeof(req));
1282
1283         req.type = CTL_ISCSI_SEND;
1284         req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1285         req.data.send.bhs = pdu->pdu_bhs;
1286         req.data.send.data_segment_len = pdu->pdu_data_len;
1287         req.data.send.data_segment = pdu->pdu_data;
1288
1289         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1290                 log_err(1, "error issuing CTL_ISCSI ioctl; "
1291                     "dropping connection");
1292         }
1293
1294         if (req.status != CTL_ISCSI_OK) {
1295                 log_errx(1, "error returned from CTL iSCSI send: "
1296                     "%s; dropping connection", req.error_str);
1297         }
1298 }
1299
1300 void
1301 kernel_receive(struct pdu *pdu)
1302 {
1303         struct connection *conn;
1304         struct ctl_iscsi req;
1305
1306         conn = pdu->pdu_connection;
1307         pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1308         if (pdu->pdu_data == NULL)
1309                 log_err(1, "malloc");
1310
1311         bzero(&req, sizeof(req));
1312
1313         req.type = CTL_ISCSI_RECEIVE;
1314         req.data.receive.connection_id = conn->conn_socket;
1315         req.data.receive.bhs = pdu->pdu_bhs;
1316         req.data.receive.data_segment_len =
1317             conn->conn_max_recv_data_segment_length;
1318         req.data.receive.data_segment = pdu->pdu_data;
1319
1320         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1321                 log_err(1, "error issuing CTL_ISCSI ioctl; "
1322                     "dropping connection");
1323         }
1324
1325         if (req.status != CTL_ISCSI_OK) {
1326                 log_errx(1, "error returned from CTL iSCSI receive: "
1327                     "%s; dropping connection", req.error_str);
1328         }
1329
1330 }
1331
1332 #endif /* ICL_KERNEL_PROXY */
1333
1334 /*
1335  * XXX: I CANT INTO LATIN
1336  */
1337 void
1338 kernel_capsicate(void)
1339 {
1340         int error;
1341         cap_rights_t rights;
1342         const unsigned long cmds[] = { CTL_ISCSI };
1343
1344         cap_rights_init(&rights, CAP_IOCTL);
1345         error = cap_rights_limit(ctl_fd, &rights);
1346         if (error != 0 && errno != ENOSYS)
1347                 log_err(1, "cap_rights_limit");
1348
1349         error = cap_ioctls_limit(ctl_fd, cmds, nitems(cmds));
1350
1351         if (error != 0 && errno != ENOSYS)
1352                 log_err(1, "cap_ioctls_limit");
1353
1354         error = cap_enter();
1355         if (error != 0 && errno != ENOSYS)
1356                 log_err(1, "cap_enter");
1357
1358         if (cap_sandboxed())
1359                 log_debugx("Capsicum capability mode enabled");
1360         else
1361                 log_warnx("Capsicum capability mode not supported");
1362 }
1363