]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/ctld/kernel.c
MFC r268293:
[FreeBSD/stable/10.git] / usr.sbin / ctld / kernel.c
1 /*-
2  * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3  * Copyright (c) 1997-2007 Kenneth D. Merry
4  * Copyright (c) 2012 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Edward Tomasz Napierala
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    substantially similar to the "NO WARRANTY" disclaimer below
18  *    ("Disclaimer") and any redistribution must be conditioned upon
19  *    including a substantially similar Disclaimer requirement for further
20  *    binary redistribution.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * $FreeBSD$
36  */
37
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/param.h>
42 #include <sys/linker.h>
43 #include <sys/queue.h>
44 #include <sys/callout.h>
45 #include <sys/sbuf.h>
46 #include <sys/capability.h>
47 #include <assert.h>
48 #include <bsdxml.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <strings.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/ctl/ctl.h>
60 #include <cam/ctl/ctl_io.h>
61 #include <cam/ctl/ctl_frontend_internal.h>
62 #include <cam/ctl/ctl_backend.h>
63 #include <cam/ctl/ctl_ioctl.h>
64 #include <cam/ctl/ctl_backend_block.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_scsi_all.h>
67
68 #include "ctld.h"
69
70 #ifdef ICL_KERNEL_PROXY
71 #include <netdb.h>
72 #endif
73
74 extern bool proxy_mode;
75
76 static int      ctl_fd = 0;
77
78 void
79 kernel_init(void)
80 {
81         int retval, saved_errno;
82
83         ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84         if (ctl_fd < 0 && errno == ENOENT) {
85                 saved_errno = errno;
86                 retval = kldload("ctl");
87                 if (retval != -1)
88                         ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
89                 else
90                         errno = saved_errno;
91         }
92         if (ctl_fd < 0)
93                 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
94 }
95
96 /*
97  * Name/value pair used for per-LUN attributes.
98  */
99 struct cctl_lun_nv {
100         char *name;
101         char *value;
102         STAILQ_ENTRY(cctl_lun_nv) links;
103 };
104
105 /*
106  * Backend LUN information.  
107  */
108 struct cctl_lun {
109         uint64_t lun_id;
110         char *backend_type;
111         uint64_t size_blocks;
112         uint32_t blocksize;
113         char *serial_number;
114         char *device_id;
115         char *cfiscsi_target;
116         int cfiscsi_lun;
117         STAILQ_HEAD(,cctl_lun_nv) attr_list;
118         STAILQ_ENTRY(cctl_lun) links;
119 };
120
121 struct cctl_port {
122         uint32_t port_id;
123         char *cfiscsi_target;
124         uint16_t cfiscsi_portal_group_tag;
125         STAILQ_HEAD(,cctl_lun_nv) attr_list;
126         STAILQ_ENTRY(cctl_port) links;
127 };
128
129 struct cctl_devlist_data {
130         int num_luns;
131         STAILQ_HEAD(,cctl_lun) lun_list;
132         struct cctl_lun *cur_lun;
133         int num_ports;
134         STAILQ_HEAD(,cctl_port) port_list;
135         struct cctl_port *cur_port;
136         int level;
137         struct sbuf *cur_sb[32];
138 };
139
140 static void
141 cctl_start_element(void *user_data, const char *name, const char **attr)
142 {
143         int i;
144         struct cctl_devlist_data *devlist;
145         struct cctl_lun *cur_lun;
146
147         devlist = (struct cctl_devlist_data *)user_data;
148         cur_lun = devlist->cur_lun;
149         devlist->level++;
150         if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
151             sizeof(devlist->cur_sb[0])))
152                 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
153                      sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
154
155         devlist->cur_sb[devlist->level] = sbuf_new_auto();
156         if (devlist->cur_sb[devlist->level] == NULL)
157                 log_err(1, "%s: unable to allocate sbuf", __func__);
158
159         if (strcmp(name, "lun") == 0) {
160                 if (cur_lun != NULL)
161                         log_errx(1, "%s: improper lun element nesting",
162                             __func__);
163
164                 cur_lun = calloc(1, sizeof(*cur_lun));
165                 if (cur_lun == NULL)
166                         log_err(1, "%s: cannot allocate %zd bytes", __func__,
167                             sizeof(*cur_lun));
168
169                 devlist->num_luns++;
170                 devlist->cur_lun = cur_lun;
171
172                 STAILQ_INIT(&cur_lun->attr_list);
173                 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
174
175                 for (i = 0; attr[i] != NULL; i += 2) {
176                         if (strcmp(attr[i], "id") == 0) {
177                                 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
178                         } else {
179                                 log_errx(1, "%s: invalid LUN attribute %s = %s",
180                                      __func__, attr[i], attr[i+1]);
181                         }
182                 }
183         }
184 }
185
186 static void
187 cctl_end_element(void *user_data, const char *name)
188 {
189         struct cctl_devlist_data *devlist;
190         struct cctl_lun *cur_lun;
191         char *str;
192
193         devlist = (struct cctl_devlist_data *)user_data;
194         cur_lun = devlist->cur_lun;
195
196         if ((cur_lun == NULL)
197          && (strcmp(name, "ctllunlist") != 0))
198                 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
199
200         if (devlist->cur_sb[devlist->level] == NULL)
201                 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
202                      devlist->level, name);
203
204         sbuf_finish(devlist->cur_sb[devlist->level]);
205         str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
206
207         if (strlen(str) == 0) {
208                 free(str);
209                 str = NULL;
210         }
211
212         sbuf_delete(devlist->cur_sb[devlist->level]);
213         devlist->cur_sb[devlist->level] = NULL;
214         devlist->level--;
215
216         if (strcmp(name, "backend_type") == 0) {
217                 cur_lun->backend_type = str;
218                 str = NULL;
219         } else if (strcmp(name, "size") == 0) {
220                 cur_lun->size_blocks = strtoull(str, NULL, 0);
221         } else if (strcmp(name, "blocksize") == 0) {
222                 cur_lun->blocksize = strtoul(str, NULL, 0);
223         } else if (strcmp(name, "serial_number") == 0) {
224                 cur_lun->serial_number = str;
225                 str = NULL;
226         } else if (strcmp(name, "device_id") == 0) {
227                 cur_lun->device_id = str;
228                 str = NULL;
229         } else if (strcmp(name, "cfiscsi_target") == 0) {
230                 cur_lun->cfiscsi_target = str;
231                 str = NULL;
232         } else if (strcmp(name, "cfiscsi_lun") == 0) {
233                 cur_lun->cfiscsi_lun = strtoul(str, NULL, 0);
234         } else if (strcmp(name, "lun") == 0) {
235                 devlist->cur_lun = NULL;
236         } else if (strcmp(name, "ctllunlist") == 0) {
237                 
238         } else {
239                 struct cctl_lun_nv *nv;
240
241                 nv = calloc(1, sizeof(*nv));
242                 if (nv == NULL)
243                         log_err(1, "%s: can't allocate %zd bytes for nv pair",
244                             __func__, sizeof(*nv));
245
246                 nv->name = checked_strdup(name);
247
248                 nv->value = str;
249                 str = NULL;
250                 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
251         }
252
253         free(str);
254 }
255
256 static void
257 cctl_start_pelement(void *user_data, const char *name, const char **attr)
258 {
259         int i;
260         struct cctl_devlist_data *devlist;
261         struct cctl_port *cur_port;
262
263         devlist = (struct cctl_devlist_data *)user_data;
264         cur_port = devlist->cur_port;
265         devlist->level++;
266         if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
267             sizeof(devlist->cur_sb[0])))
268                 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
269                      sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
270
271         devlist->cur_sb[devlist->level] = sbuf_new_auto();
272         if (devlist->cur_sb[devlist->level] == NULL)
273                 log_err(1, "%s: unable to allocate sbuf", __func__);
274
275         if (strcmp(name, "targ_port") == 0) {
276                 if (cur_port != NULL)
277                         log_errx(1, "%s: improper port element nesting (%s)",
278                             __func__, name);
279
280                 cur_port = calloc(1, sizeof(*cur_port));
281                 if (cur_port == NULL)
282                         log_err(1, "%s: cannot allocate %zd bytes", __func__,
283                             sizeof(*cur_port));
284
285                 devlist->num_ports++;
286                 devlist->cur_port = cur_port;
287
288                 STAILQ_INIT(&cur_port->attr_list);
289                 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
290
291                 for (i = 0; attr[i] != NULL; i += 2) {
292                         if (strcmp(attr[i], "id") == 0) {
293                                 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
294                         } else {
295                                 log_errx(1, "%s: invalid LUN attribute %s = %s",
296                                      __func__, attr[i], attr[i+1]);
297                         }
298                 }
299         }
300 }
301
302 static void
303 cctl_end_pelement(void *user_data, const char *name)
304 {
305         struct cctl_devlist_data *devlist;
306         struct cctl_port *cur_port;
307         char *str;
308
309         devlist = (struct cctl_devlist_data *)user_data;
310         cur_port = devlist->cur_port;
311
312         if ((cur_port == NULL)
313          && (strcmp(name, "ctlportlist") != 0))
314                 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
315
316         if (devlist->cur_sb[devlist->level] == NULL)
317                 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
318                      devlist->level, name);
319
320         sbuf_finish(devlist->cur_sb[devlist->level]);
321         str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
322
323         if (strlen(str) == 0) {
324                 free(str);
325                 str = NULL;
326         }
327
328         sbuf_delete(devlist->cur_sb[devlist->level]);
329         devlist->cur_sb[devlist->level] = NULL;
330         devlist->level--;
331
332         if (strcmp(name, "cfiscsi_target") == 0) {
333                 cur_port->cfiscsi_target = str;
334                 str = NULL;
335         } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
336                 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
337         } else if (strcmp(name, "targ_port") == 0) {
338                 devlist->cur_port = NULL;
339         } else if (strcmp(name, "ctlportlist") == 0) {
340                 
341         } else {
342                 struct cctl_lun_nv *nv;
343
344                 nv = calloc(1, sizeof(*nv));
345                 if (nv == NULL)
346                         log_err(1, "%s: can't allocate %zd bytes for nv pair",
347                             __func__, sizeof(*nv));
348
349                 nv->name = checked_strdup(name);
350
351                 nv->value = str;
352                 str = NULL;
353                 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
354         }
355
356         free(str);
357 }
358
359 static void
360 cctl_char_handler(void *user_data, const XML_Char *str, int len)
361 {
362         struct cctl_devlist_data *devlist;
363
364         devlist = (struct cctl_devlist_data *)user_data;
365
366         sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
367 }
368
369 struct conf *
370 conf_new_from_kernel(void)
371 {
372         struct conf *conf = NULL;
373         struct target *targ;
374         struct lun *cl;
375         struct lun_option *lo;
376         struct ctl_lun_list list;
377         struct cctl_devlist_data devlist;
378         struct cctl_lun *lun;
379         struct cctl_port *port;
380         XML_Parser parser;
381         char *str;
382         int len, retval;
383
384         bzero(&devlist, sizeof(devlist));
385         STAILQ_INIT(&devlist.lun_list);
386         STAILQ_INIT(&devlist.port_list);
387
388         log_debugx("obtaining previously configured CTL luns from the kernel");
389
390         str = NULL;
391         len = 4096;
392 retry:
393         str = realloc(str, len);
394         if (str == NULL)
395                 log_err(1, "realloc");
396
397         bzero(&list, sizeof(list));
398         list.alloc_len = len;
399         list.status = CTL_LUN_LIST_NONE;
400         list.lun_xml = str;
401
402         if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
403                 log_warn("error issuing CTL_LUN_LIST ioctl");
404                 free(str);
405                 return (NULL);
406         }
407
408         if (list.status == CTL_LUN_LIST_ERROR) {
409                 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
410                     list.error_str);
411                 free(str);
412                 return (NULL);
413         }
414
415         if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
416                 len = len << 1;
417                 goto retry;
418         }
419
420         parser = XML_ParserCreate(NULL);
421         if (parser == NULL) {
422                 log_warnx("unable to create XML parser");
423                 free(str);
424                 return (NULL);
425         }
426
427         XML_SetUserData(parser, &devlist);
428         XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
429         XML_SetCharacterDataHandler(parser, cctl_char_handler);
430
431         retval = XML_Parse(parser, str, strlen(str), 1);
432         XML_ParserFree(parser);
433         free(str);
434         if (retval != 1) {
435                 log_warnx("XML_Parse failed");
436                 return (NULL);
437         }
438
439         str = NULL;
440         len = 4096;
441 retry_port:
442         str = realloc(str, len);
443         if (str == NULL)
444                 log_err(1, "realloc");
445
446         bzero(&list, sizeof(list));
447         list.alloc_len = len;
448         list.status = CTL_LUN_LIST_NONE;
449         list.lun_xml = str;
450
451         if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
452                 log_warn("error issuing CTL_PORT_LIST ioctl");
453                 free(str);
454                 return (NULL);
455         }
456
457         if (list.status == CTL_PORT_LIST_ERROR) {
458                 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
459                     list.error_str);
460                 free(str);
461                 return (NULL);
462         }
463
464         if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
465                 len = len << 1;
466                 goto retry_port;
467         }
468
469         parser = XML_ParserCreate(NULL);
470         if (parser == NULL) {
471                 log_warnx("unable to create XML parser");
472                 free(str);
473                 return (NULL);
474         }
475
476         XML_SetUserData(parser, &devlist);
477         XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
478         XML_SetCharacterDataHandler(parser, cctl_char_handler);
479
480         retval = XML_Parse(parser, str, strlen(str), 1);
481         XML_ParserFree(parser);
482         free(str);
483         if (retval != 1) {
484                 log_warnx("XML_Parse failed");
485                 return (NULL);
486         }
487
488         conf = conf_new();
489
490         STAILQ_FOREACH(port, &devlist.port_list, links) {
491
492                 if (port->cfiscsi_target == NULL) {
493                         log_debugx("CTL port %ju wasn't managed by ctld; "
494                             "ignoring", (uintmax_t)port->port_id);
495                         continue;
496                 }
497
498                 targ = target_find(conf, port->cfiscsi_target);
499                 if (targ == NULL) {
500 #if 0
501                         log_debugx("found new kernel target %s for CTL port %ld",
502                             port->cfiscsi_target, port->port_id);
503 #endif
504                         targ = target_new(conf, port->cfiscsi_target);
505                         if (targ == NULL) {
506                                 log_warnx("target_new failed");
507                                 continue;
508                         }
509                 }
510         }
511
512         STAILQ_FOREACH(lun, &devlist.lun_list, links) {
513                 struct cctl_lun_nv *nv;
514
515                 if (lun->cfiscsi_target == NULL) {
516                         log_debugx("CTL lun %ju wasn't managed by ctld; "
517                             "ignoring", (uintmax_t)lun->lun_id);
518                         continue;
519                 }
520
521                 targ = target_find(conf, lun->cfiscsi_target);
522                 if (targ == NULL) {
523 #if 0
524                         log_debugx("found new kernel target %s for CTL lun %ld",
525                             lun->cfiscsi_target, lun->lun_id);
526 #endif
527                         targ = target_new(conf, lun->cfiscsi_target);
528                         if (targ == NULL) {
529                                 log_warnx("target_new failed");
530                                 continue;
531                         }
532                 }
533
534                 cl = lun_find(targ, lun->cfiscsi_lun);
535                 if (cl != NULL) {
536                         log_warnx("found CTL lun %ju, backing lun %d, target "
537                             "%s, also backed by CTL lun %d; ignoring",
538                             (uintmax_t) lun->lun_id, cl->l_lun,
539                             cl->l_target->t_name, cl->l_ctl_lun);
540                         continue;
541                 }
542
543                 log_debugx("found CTL lun %ju, backing lun %d, target %s",
544                     (uintmax_t)lun->lun_id, lun->cfiscsi_lun, lun->cfiscsi_target);
545
546                 cl = lun_new(targ, lun->cfiscsi_lun);
547                 if (cl == NULL) {
548                         log_warnx("lun_new failed");
549                         continue;
550                 }
551                 lun_set_backend(cl, lun->backend_type);
552                 lun_set_blocksize(cl, lun->blocksize);
553                 lun_set_device_id(cl, lun->device_id);
554                 lun_set_serial(cl, lun->serial_number);
555                 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
556                 lun_set_ctl_lun(cl, lun->lun_id);
557
558                 STAILQ_FOREACH(nv, &lun->attr_list, links) {
559                         if (strcmp(nv->name, "file") == 0 ||
560                             strcmp(nv->name, "dev") == 0) {
561                                 lun_set_path(cl, nv->value);
562                                 continue;
563                         }
564                         lo = lun_option_new(cl, nv->name, nv->value);
565                         if (lo == NULL)
566                                 log_warnx("unable to add CTL lun option %s "
567                                     "for CTL lun %ju for lun %d, target %s",
568                                     nv->name, (uintmax_t) lun->lun_id,
569                                     cl->l_lun, cl->l_target->t_name);
570                 }
571         }
572
573         return (conf);
574 }
575
576 static void
577 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
578 {
579
580         arg->namelen = strlen(name) + 1;
581         arg->name = __DECONST(char *, name);
582         arg->vallen = strlen(value) + 1;
583         arg->value = __DECONST(char *, value);
584         arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
585 }
586
587 int
588 kernel_lun_add(struct lun *lun)
589 {
590         struct lun_option *lo;
591         struct ctl_lun_req req;
592         char *tmp;
593         int error, i, num_options;
594
595         bzero(&req, sizeof(req));
596
597         strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
598         req.reqtype = CTL_LUNREQ_CREATE;
599
600         req.reqdata.create.blocksize_bytes = lun->l_blocksize;
601
602         if (lun->l_size != 0)
603                 req.reqdata.create.lun_size_bytes = lun->l_size;
604
605         req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
606         req.reqdata.create.device_type = T_DIRECT;
607
608         if (lun->l_serial != NULL) {
609                 strncpy(req.reqdata.create.serial_num, lun->l_serial,
610                         sizeof(req.reqdata.create.serial_num));
611                 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
612         }
613
614         if (lun->l_device_id != NULL) {
615                 strncpy(req.reqdata.create.device_id, lun->l_device_id,
616                         sizeof(req.reqdata.create.device_id));
617                 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
618         }
619
620         if (lun->l_path != NULL) {
621                 lo = lun_option_find(lun, "file");
622                 if (lo != NULL) {
623                         lun_option_set(lo, lun->l_path);
624                 } else {
625                         lo = lun_option_new(lun, "file", lun->l_path);
626                         assert(lo != NULL);
627                 }
628         }
629
630         lo = lun_option_find(lun, "cfiscsi_target");
631         if (lo != NULL) {
632                 lun_option_set(lo, lun->l_target->t_name);
633         } else {
634                 lo = lun_option_new(lun, "cfiscsi_target",
635                     lun->l_target->t_name);
636                 assert(lo != NULL);
637         }
638
639         asprintf(&tmp, "%d", lun->l_lun);
640         if (tmp == NULL)
641                 log_errx(1, "asprintf");
642         lo = lun_option_find(lun, "cfiscsi_lun");
643         if (lo != NULL) {
644                 lun_option_set(lo, tmp);
645                 free(tmp);
646         } else {
647                 lo = lun_option_new(lun, "cfiscsi_lun", tmp);
648                 free(tmp);
649                 assert(lo != NULL);
650         }
651
652         asprintf(&tmp, "%s,lun,%d", lun->l_target->t_name, lun->l_lun);
653         if (tmp == NULL)
654                 log_errx(1, "asprintf");
655         lo = lun_option_find(lun, "scsiname");
656         if (lo != NULL) {
657                 lun_option_set(lo, tmp);
658                 free(tmp);
659         } else {
660                 lo = lun_option_new(lun, "scsiname", tmp);
661                 free(tmp);
662                 assert(lo != NULL);
663         }
664
665         num_options = 0;
666         TAILQ_FOREACH(lo, &lun->l_options, lo_next)
667                 num_options++;
668
669         req.num_be_args = num_options;
670         if (num_options > 0) {
671                 req.be_args = malloc(num_options * sizeof(*req.be_args));
672                 if (req.be_args == NULL) {
673                         log_warn("error allocating %zd bytes",
674                             num_options * sizeof(*req.be_args));
675                         return (1);
676                 }
677
678                 i = 0;
679                 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
680                         str_arg(&req.be_args[i], lo->lo_name, lo->lo_value);
681                         i++;
682                 }
683                 assert(i == num_options);
684         }
685
686         error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
687         free(req.be_args);
688         if (error != 0) {
689                 log_warn("error issuing CTL_LUN_REQ ioctl");
690                 return (1);
691         }
692
693         if (req.status == CTL_LUN_ERROR) {
694                 log_warnx("error returned from LUN creation request: %s",
695                     req.error_str);
696                 return (1);
697         }
698
699         if (req.status != CTL_LUN_OK) {
700                 log_warnx("unknown LUN creation request status %d",
701                     req.status);
702                 return (1);
703         }
704
705         lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
706
707         return (0);
708 }
709
710 int
711 kernel_lun_resize(struct lun *lun)
712 {
713         struct ctl_lun_req req;
714
715         bzero(&req, sizeof(req));
716
717         strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
718         req.reqtype = CTL_LUNREQ_MODIFY;
719
720         req.reqdata.modify.lun_id = lun->l_ctl_lun;
721         req.reqdata.modify.lun_size_bytes = lun->l_size;
722
723         if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
724                 log_warn("error issuing CTL_LUN_REQ ioctl");
725                 return (1);
726         }
727
728         if (req.status == CTL_LUN_ERROR) {
729                 log_warnx("error returned from LUN modification request: %s",
730                     req.error_str);
731                 return (1);
732         }
733
734         if (req.status != CTL_LUN_OK) {
735                 log_warnx("unknown LUN modification request status %d",
736                     req.status);
737                 return (1);
738         }
739
740         return (0);
741 }
742
743 int
744 kernel_lun_remove(struct lun *lun)
745 {
746         struct ctl_lun_req req;
747
748         bzero(&req, sizeof(req));
749
750         strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
751         req.reqtype = CTL_LUNREQ_RM;
752
753         req.reqdata.rm.lun_id = lun->l_ctl_lun;
754
755         if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
756                 log_warn("error issuing CTL_LUN_REQ ioctl");
757                 return (1);
758         }
759
760         if (req.status == CTL_LUN_ERROR) {
761                 log_warnx("error returned from LUN removal request: %s",
762                     req.error_str);
763                 return (1);
764         }
765         
766         if (req.status != CTL_LUN_OK) {
767                 log_warnx("unknown LUN removal request status %d", req.status);
768                 return (1);
769         }
770
771         return (0);
772 }
773
774 void
775 kernel_handoff(struct connection *conn)
776 {
777         struct ctl_iscsi req;
778
779         bzero(&req, sizeof(req));
780
781         req.type = CTL_ISCSI_HANDOFF;
782         strlcpy(req.data.handoff.initiator_name,
783             conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
784         strlcpy(req.data.handoff.initiator_addr,
785             conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
786         if (conn->conn_initiator_alias != NULL) {
787                 strlcpy(req.data.handoff.initiator_alias,
788                     conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
789         }
790         strlcpy(req.data.handoff.target_name,
791             conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
792 #ifdef ICL_KERNEL_PROXY
793         if (proxy_mode)
794                 req.data.handoff.connection_id = conn->conn_socket;
795         else
796                 req.data.handoff.socket = conn->conn_socket;
797 #else
798         req.data.handoff.socket = conn->conn_socket;
799 #endif
800         req.data.handoff.portal_group_tag =
801             conn->conn_portal->p_portal_group->pg_tag;
802         if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
803                 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
804         if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
805                 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
806         req.data.handoff.cmdsn = conn->conn_cmdsn;
807         req.data.handoff.statsn = conn->conn_statsn;
808         req.data.handoff.max_recv_data_segment_length =
809             conn->conn_max_data_segment_length;
810         req.data.handoff.max_burst_length = conn->conn_max_burst_length;
811         req.data.handoff.immediate_data = conn->conn_immediate_data;
812
813         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
814                 log_err(1, "error issuing CTL_ISCSI ioctl; "
815                     "dropping connection");
816         }
817
818         if (req.status != CTL_ISCSI_OK) {
819                 log_errx(1, "error returned from CTL iSCSI handoff request: "
820                     "%s; dropping connection", req.error_str);
821         }
822 }
823
824 int
825 kernel_port_add(struct target *targ)
826 {
827         struct ctl_port_entry entry;
828         struct ctl_req req;
829         char tagstr[16];
830         int error;
831         uint32_t port_id = -1;
832
833         bzero(&req, sizeof(req));
834         strlcpy(req.driver, "iscsi", sizeof(req.driver));
835         req.reqtype = CTL_REQ_CREATE;
836         req.num_args = 4;
837         req.args = malloc(req.num_args * sizeof(*req.args));
838         req.args[0].namelen = sizeof("port_id");
839         req.args[0].name = __DECONST(char *, "port_id");
840         req.args[0].vallen = sizeof(port_id);
841         req.args[0].value = &port_id;
842         req.args[0].flags = CTL_BEARG_WR;
843         str_arg(&req.args[1], "cfiscsi_target", targ->t_name);
844         str_arg(&req.args[2], "cfiscsi_target_alias", targ->t_alias);
845         snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
846         str_arg(&req.args[3], "cfiscsi_portal_group_tag", tagstr);
847
848         error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
849         free(req.args);
850         if (error != 0) {
851                 log_warn("error issuing CTL_PORT_REQ ioctl");
852                 return (1);
853         }
854
855         if (req.status == CTL_LUN_ERROR) {
856                 log_warnx("error returned from port creation request: %s",
857                     req.error_str);
858                 return (1);
859         }
860
861         if (req.status != CTL_LUN_OK) {
862                 log_warnx("unknown port creation request status %d",
863                     req.status);
864                 return (1);
865         }
866
867         bzero(&entry, sizeof(entry));
868         entry.targ_port = port_id;
869
870         error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
871         if (error != 0) {
872                 log_warn("CTL_ENABLE_PORT ioctl failed");
873                 return (-1);
874         }
875
876         return (0);
877 }
878
879 int
880 kernel_port_remove(struct target *targ)
881 {
882         struct ctl_req req;
883         char tagstr[16];
884         int error;
885
886         bzero(&req, sizeof(req));
887         strlcpy(req.driver, "iscsi", sizeof(req.driver));
888         req.reqtype = CTL_REQ_REMOVE;
889         req.num_args = 2;
890         req.args = malloc(req.num_args * sizeof(*req.args));
891         str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
892         if (targ->t_portal_group) {
893                 snprintf(tagstr, sizeof(tagstr), "%d",
894                     targ->t_portal_group->pg_tag);
895                 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
896         } else
897                 req.num_args--;
898
899         error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
900         free(req.args);
901         if (error != 0) {
902                 log_warn("error issuing CTL_PORT_REQ ioctl");
903                 return (1);
904         }
905
906         if (req.status == CTL_LUN_ERROR) {
907                 log_warnx("error returned from port removal request: %s",
908                     req.error_str);
909                 return (1);
910         }
911
912         if (req.status != CTL_LUN_OK) {
913                 log_warnx("unknown port removal request status %d",
914                     req.status);
915                 return (1);
916         }
917
918         return (0);
919 }
920
921 #ifdef ICL_KERNEL_PROXY
922 void
923 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
924 {
925         struct ctl_iscsi req;
926
927         bzero(&req, sizeof(req));
928
929         req.type = CTL_ISCSI_LISTEN;
930         req.data.listen.iser = iser;
931         req.data.listen.domain = ai->ai_family;
932         req.data.listen.socktype = ai->ai_socktype;
933         req.data.listen.protocol = ai->ai_protocol;
934         req.data.listen.addr = ai->ai_addr;
935         req.data.listen.addrlen = ai->ai_addrlen;
936         req.data.listen.portal_id = portal_id;
937
938         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
939                 log_err(1, "error issuing CTL_ISCSI ioctl");
940
941         if (req.status != CTL_ISCSI_OK) {
942                 log_errx(1, "error returned from CTL iSCSI listen: %s",
943                     req.error_str);
944         }
945 }
946
947 void
948 kernel_accept(int *connection_id, int *portal_id,
949     struct sockaddr *client_sa, socklen_t *client_salen)
950 {
951         struct ctl_iscsi req;
952         struct sockaddr_storage ss;
953
954         bzero(&req, sizeof(req));
955
956         req.type = CTL_ISCSI_ACCEPT;
957         req.data.accept.initiator_addr = (struct sockaddr *)&ss;
958
959         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
960                 log_err(1, "error issuing CTL_ISCSI ioctl");
961
962         if (req.status != CTL_ISCSI_OK) {
963                 log_errx(1, "error returned from CTL iSCSI accept: %s",
964                     req.error_str);
965         }
966
967         *connection_id = req.data.accept.connection_id;
968         *portal_id = req.data.accept.portal_id;
969         *client_salen = req.data.accept.initiator_addrlen;
970         memcpy(client_sa, &ss, *client_salen);
971 }
972
973 void
974 kernel_send(struct pdu *pdu)
975 {
976         struct ctl_iscsi req;
977
978         bzero(&req, sizeof(req));
979
980         req.type = CTL_ISCSI_SEND;
981         req.data.send.connection_id = pdu->pdu_connection->conn_socket;
982         req.data.send.bhs = pdu->pdu_bhs;
983         req.data.send.data_segment_len = pdu->pdu_data_len;
984         req.data.send.data_segment = pdu->pdu_data;
985
986         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
987                 log_err(1, "error issuing CTL_ISCSI ioctl; "
988                     "dropping connection");
989         }
990
991         if (req.status != CTL_ISCSI_OK) {
992                 log_errx(1, "error returned from CTL iSCSI send: "
993                     "%s; dropping connection", req.error_str);
994         }
995 }
996
997 void
998 kernel_receive(struct pdu *pdu)
999 {
1000         struct ctl_iscsi req;
1001
1002         pdu->pdu_data = malloc(MAX_DATA_SEGMENT_LENGTH);
1003         if (pdu->pdu_data == NULL)
1004                 log_err(1, "malloc");
1005
1006         bzero(&req, sizeof(req));
1007
1008         req.type = CTL_ISCSI_RECEIVE;
1009         req.data.receive.connection_id = pdu->pdu_connection->conn_socket;
1010         req.data.receive.bhs = pdu->pdu_bhs;
1011         req.data.receive.data_segment_len = MAX_DATA_SEGMENT_LENGTH;
1012         req.data.receive.data_segment = pdu->pdu_data;
1013
1014         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1015                 log_err(1, "error issuing CTL_ISCSI ioctl; "
1016                     "dropping connection");
1017         }
1018
1019         if (req.status != CTL_ISCSI_OK) {
1020                 log_errx(1, "error returned from CTL iSCSI receive: "
1021                     "%s; dropping connection", req.error_str);
1022         }
1023
1024 }
1025
1026 #endif /* ICL_KERNEL_PROXY */
1027
1028 /*
1029  * XXX: I CANT INTO LATIN
1030  */
1031 void
1032 kernel_capsicate(void)
1033 {
1034         int error;
1035         cap_rights_t rights;
1036         const unsigned long cmds[] = { CTL_ISCSI };
1037
1038         cap_rights_init(&rights, CAP_IOCTL);
1039         error = cap_rights_limit(ctl_fd, &rights);
1040         if (error != 0 && errno != ENOSYS)
1041                 log_err(1, "cap_rights_limit");
1042
1043         error = cap_ioctls_limit(ctl_fd, cmds,
1044             sizeof(cmds) / sizeof(cmds[0]));
1045         if (error != 0 && errno != ENOSYS)
1046                 log_err(1, "cap_ioctls_limit");
1047
1048         error = cap_enter();
1049         if (error != 0 && errno != ENOSYS)
1050                 log_err(1, "cap_enter");
1051
1052         if (cap_sandboxed())
1053                 log_debugx("Capsicum capability mode enabled");
1054         else
1055                 log_warnx("Capsicum capability mode not supported");
1056 }
1057