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