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