1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2012 The FreeBSD Foundation
5 *
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <assert.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <netdb.h>
44 #include <signal.h>
45 #include <stdbool.h>
46 #include <stdio.h>
47 #include <stdint.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "ctld.h"
53 #include "isns.h"
54
55 static bool timed_out(void);
56 #ifdef ICL_KERNEL_PROXY
57 static void pdu_receive_proxy(struct pdu *pdu);
58 static void pdu_send_proxy(struct pdu *pdu);
59 #endif /* ICL_KERNEL_PROXY */
60 static void pdu_fail(const struct connection *conn, const char *reason);
61
62 bool proxy_mode = false;
63
64 static volatile bool sighup_received = false;
65 static volatile bool sigterm_received = false;
66 static volatile bool sigalrm_received = false;
67
68 static int nchildren = 0;
69 static uint16_t last_portal_group_tag = 0xff;
70
71 static struct connection_ops conn_ops = {
72 .timed_out = timed_out,
73 #ifdef ICL_KERNEL_PROXY
74 .pdu_receive_proxy = pdu_receive_proxy,
75 .pdu_send_proxy = pdu_send_proxy,
76 #endif
77 .fail = pdu_fail,
78 };
79
80 static void
usage(void)81 usage(void)
82 {
83
84 fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n");
85 fprintf(stderr, " ctld -t [-u][-f config-file]\n");
86 exit(1);
87 }
88
89 struct conf *
conf_new(void)90 conf_new(void)
91 {
92 struct conf *conf;
93
94 conf = calloc(1, sizeof(*conf));
95 if (conf == NULL)
96 log_err(1, "calloc");
97 TAILQ_INIT(&conf->conf_luns);
98 TAILQ_INIT(&conf->conf_targets);
99 TAILQ_INIT(&conf->conf_auth_groups);
100 TAILQ_INIT(&conf->conf_ports);
101 TAILQ_INIT(&conf->conf_portal_groups);
102 TAILQ_INIT(&conf->conf_isns);
103
104 conf->conf_isns_period = 900;
105 conf->conf_isns_timeout = 5;
106 conf->conf_debug = 0;
107 conf->conf_timeout = 60;
108 conf->conf_maxproc = 30;
109
110 return (conf);
111 }
112
113 void
conf_delete(struct conf * conf)114 conf_delete(struct conf *conf)
115 {
116 struct lun *lun, *ltmp;
117 struct target *targ, *tmp;
118 struct auth_group *ag, *cagtmp;
119 struct portal_group *pg, *cpgtmp;
120 struct isns *is, *istmp;
121
122 assert(conf->conf_pidfh == NULL);
123
124 TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
125 lun_delete(lun);
126 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
127 target_delete(targ);
128 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
129 auth_group_delete(ag);
130 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
131 portal_group_delete(pg);
132 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
133 isns_delete(is);
134 assert(TAILQ_EMPTY(&conf->conf_ports));
135 free(conf->conf_pidfile_path);
136 free(conf);
137 }
138
139 static struct auth *
auth_new(struct auth_group * ag)140 auth_new(struct auth_group *ag)
141 {
142 struct auth *auth;
143
144 auth = calloc(1, sizeof(*auth));
145 if (auth == NULL)
146 log_err(1, "calloc");
147 auth->a_auth_group = ag;
148 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
149 return (auth);
150 }
151
152 static void
auth_delete(struct auth * auth)153 auth_delete(struct auth *auth)
154 {
155 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
156
157 free(auth->a_user);
158 free(auth->a_secret);
159 free(auth->a_mutual_user);
160 free(auth->a_mutual_secret);
161 free(auth);
162 }
163
164 const struct auth *
auth_find(const struct auth_group * ag,const char * user)165 auth_find(const struct auth_group *ag, const char *user)
166 {
167 const struct auth *auth;
168
169 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
170 if (strcmp(auth->a_user, user) == 0)
171 return (auth);
172 }
173
174 return (NULL);
175 }
176
177 static void
auth_check_secret_length(struct auth * auth)178 auth_check_secret_length(struct auth *auth)
179 {
180 size_t len;
181
182 len = strlen(auth->a_secret);
183 if (len > 16) {
184 if (auth->a_auth_group->ag_name != NULL)
185 log_warnx("secret for user \"%s\", auth-group \"%s\", "
186 "is too long; it should be at most 16 characters "
187 "long", auth->a_user, auth->a_auth_group->ag_name);
188 else
189 log_warnx("secret for user \"%s\", target \"%s\", "
190 "is too long; it should be at most 16 characters "
191 "long", auth->a_user,
192 auth->a_auth_group->ag_target->t_name);
193 }
194 if (len < 12) {
195 if (auth->a_auth_group->ag_name != NULL)
196 log_warnx("secret for user \"%s\", auth-group \"%s\", "
197 "is too short; it should be at least 12 characters "
198 "long", auth->a_user,
199 auth->a_auth_group->ag_name);
200 else
201 log_warnx("secret for user \"%s\", target \"%s\", "
202 "is too short; it should be at least 12 characters "
203 "long", auth->a_user,
204 auth->a_auth_group->ag_target->t_name);
205 }
206
207 if (auth->a_mutual_secret != NULL) {
208 len = strlen(auth->a_mutual_secret);
209 if (len > 16) {
210 if (auth->a_auth_group->ag_name != NULL)
211 log_warnx("mutual secret for user \"%s\", "
212 "auth-group \"%s\", is too long; it should "
213 "be at most 16 characters long",
214 auth->a_user, auth->a_auth_group->ag_name);
215 else
216 log_warnx("mutual secret for user \"%s\", "
217 "target \"%s\", is too long; it should "
218 "be at most 16 characters long",
219 auth->a_user,
220 auth->a_auth_group->ag_target->t_name);
221 }
222 if (len < 12) {
223 if (auth->a_auth_group->ag_name != NULL)
224 log_warnx("mutual secret for user \"%s\", "
225 "auth-group \"%s\", is too short; it "
226 "should be at least 12 characters long",
227 auth->a_user, auth->a_auth_group->ag_name);
228 else
229 log_warnx("mutual secret for user \"%s\", "
230 "target \"%s\", is too short; it should be "
231 "at least 12 characters long",
232 auth->a_user,
233 auth->a_auth_group->ag_target->t_name);
234 }
235 }
236 }
237
238 const struct auth *
auth_new_chap(struct auth_group * ag,const char * user,const char * secret)239 auth_new_chap(struct auth_group *ag, const char *user,
240 const char *secret)
241 {
242 struct auth *auth;
243
244 if (ag->ag_type == AG_TYPE_UNKNOWN)
245 ag->ag_type = AG_TYPE_CHAP;
246 if (ag->ag_type != AG_TYPE_CHAP) {
247 if (ag->ag_name != NULL)
248 log_warnx("cannot mix \"chap\" authentication with "
249 "other types for auth-group \"%s\"", ag->ag_name);
250 else
251 log_warnx("cannot mix \"chap\" authentication with "
252 "other types for target \"%s\"",
253 ag->ag_target->t_name);
254 return (NULL);
255 }
256
257 auth = auth_new(ag);
258 auth->a_user = checked_strdup(user);
259 auth->a_secret = checked_strdup(secret);
260
261 auth_check_secret_length(auth);
262
263 return (auth);
264 }
265
266 const struct auth *
auth_new_chap_mutual(struct auth_group * ag,const char * user,const char * secret,const char * user2,const char * secret2)267 auth_new_chap_mutual(struct auth_group *ag, const char *user,
268 const char *secret, const char *user2, const char *secret2)
269 {
270 struct auth *auth;
271
272 if (ag->ag_type == AG_TYPE_UNKNOWN)
273 ag->ag_type = AG_TYPE_CHAP_MUTUAL;
274 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
275 if (ag->ag_name != NULL)
276 log_warnx("cannot mix \"chap-mutual\" authentication "
277 "with other types for auth-group \"%s\"",
278 ag->ag_name);
279 else
280 log_warnx("cannot mix \"chap-mutual\" authentication "
281 "with other types for target \"%s\"",
282 ag->ag_target->t_name);
283 return (NULL);
284 }
285
286 auth = auth_new(ag);
287 auth->a_user = checked_strdup(user);
288 auth->a_secret = checked_strdup(secret);
289 auth->a_mutual_user = checked_strdup(user2);
290 auth->a_mutual_secret = checked_strdup(secret2);
291
292 auth_check_secret_length(auth);
293
294 return (auth);
295 }
296
297 const struct auth_name *
auth_name_new(struct auth_group * ag,const char * name)298 auth_name_new(struct auth_group *ag, const char *name)
299 {
300 struct auth_name *an;
301
302 an = calloc(1, sizeof(*an));
303 if (an == NULL)
304 log_err(1, "calloc");
305 an->an_auth_group = ag;
306 an->an_initiator_name = checked_strdup(name);
307 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
308 return (an);
309 }
310
311 static void
auth_name_delete(struct auth_name * an)312 auth_name_delete(struct auth_name *an)
313 {
314 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
315
316 free(an->an_initiator_name);
317 free(an);
318 }
319
320 bool
auth_name_defined(const struct auth_group * ag)321 auth_name_defined(const struct auth_group *ag)
322 {
323 if (TAILQ_EMPTY(&ag->ag_names))
324 return (false);
325 return (true);
326 }
327
328 const struct auth_name *
auth_name_find(const struct auth_group * ag,const char * name)329 auth_name_find(const struct auth_group *ag, const char *name)
330 {
331 const struct auth_name *auth_name;
332
333 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
334 if (strcmp(auth_name->an_initiator_name, name) == 0)
335 return (auth_name);
336 }
337
338 return (NULL);
339 }
340
341 int
auth_name_check(const struct auth_group * ag,const char * initiator_name)342 auth_name_check(const struct auth_group *ag, const char *initiator_name)
343 {
344 if (!auth_name_defined(ag))
345 return (0);
346
347 if (auth_name_find(ag, initiator_name) == NULL)
348 return (1);
349
350 return (0);
351 }
352
353 const struct auth_portal *
auth_portal_new(struct auth_group * ag,const char * portal)354 auth_portal_new(struct auth_group *ag, const char *portal)
355 {
356 struct auth_portal *ap;
357 char *net, *mask, *str, *tmp;
358 int len, dm, m;
359
360 ap = calloc(1, sizeof(*ap));
361 if (ap == NULL)
362 log_err(1, "calloc");
363 ap->ap_auth_group = ag;
364 ap->ap_initiator_portal = checked_strdup(portal);
365 mask = str = checked_strdup(portal);
366 net = strsep(&mask, "/");
367 if (net[0] == '[')
368 net++;
369 len = strlen(net);
370 if (len == 0)
371 goto error;
372 if (net[len - 1] == ']')
373 net[len - 1] = 0;
374 if (strchr(net, ':') != NULL) {
375 struct sockaddr_in6 *sin6 =
376 (struct sockaddr_in6 *)&ap->ap_sa;
377
378 sin6->sin6_len = sizeof(*sin6);
379 sin6->sin6_family = AF_INET6;
380 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
381 goto error;
382 dm = 128;
383 } else {
384 struct sockaddr_in *sin =
385 (struct sockaddr_in *)&ap->ap_sa;
386
387 sin->sin_len = sizeof(*sin);
388 sin->sin_family = AF_INET;
389 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
390 goto error;
391 dm = 32;
392 }
393 if (mask != NULL) {
394 m = strtol(mask, &tmp, 0);
395 if (m < 0 || m > dm || tmp[0] != 0)
396 goto error;
397 } else
398 m = dm;
399 ap->ap_mask = m;
400 free(str);
401 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
402 return (ap);
403
404 error:
405 free(str);
406 free(ap);
407 log_warnx("incorrect initiator portal \"%s\"", portal);
408 return (NULL);
409 }
410
411 static void
auth_portal_delete(struct auth_portal * ap)412 auth_portal_delete(struct auth_portal *ap)
413 {
414 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
415
416 free(ap->ap_initiator_portal);
417 free(ap);
418 }
419
420 bool
auth_portal_defined(const struct auth_group * ag)421 auth_portal_defined(const struct auth_group *ag)
422 {
423 if (TAILQ_EMPTY(&ag->ag_portals))
424 return (false);
425 return (true);
426 }
427
428 const struct auth_portal *
auth_portal_find(const struct auth_group * ag,const struct sockaddr_storage * ss)429 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
430 {
431 const struct auth_portal *ap;
432 const uint8_t *a, *b;
433 int i;
434 uint8_t bmask;
435
436 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
437 if (ap->ap_sa.ss_family != ss->ss_family)
438 continue;
439 if (ss->ss_family == AF_INET) {
440 a = (const uint8_t *)
441 &((const struct sockaddr_in *)ss)->sin_addr;
442 b = (const uint8_t *)
443 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
444 } else {
445 a = (const uint8_t *)
446 &((const struct sockaddr_in6 *)ss)->sin6_addr;
447 b = (const uint8_t *)
448 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
449 }
450 for (i = 0; i < ap->ap_mask / 8; i++) {
451 if (a[i] != b[i])
452 goto next;
453 }
454 if (ap->ap_mask % 8) {
455 bmask = 0xff << (8 - (ap->ap_mask % 8));
456 if ((a[i] & bmask) != (b[i] & bmask))
457 goto next;
458 }
459 return (ap);
460 next:
461 ;
462 }
463
464 return (NULL);
465 }
466
467 int
auth_portal_check(const struct auth_group * ag,const struct sockaddr_storage * sa)468 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
469 {
470
471 if (!auth_portal_defined(ag))
472 return (0);
473
474 if (auth_portal_find(ag, sa) == NULL)
475 return (1);
476
477 return (0);
478 }
479
480 struct auth_group *
auth_group_new(struct conf * conf,const char * name)481 auth_group_new(struct conf *conf, const char *name)
482 {
483 struct auth_group *ag;
484
485 if (name != NULL) {
486 ag = auth_group_find(conf, name);
487 if (ag != NULL) {
488 log_warnx("duplicated auth-group \"%s\"", name);
489 return (NULL);
490 }
491 }
492
493 ag = calloc(1, sizeof(*ag));
494 if (ag == NULL)
495 log_err(1, "calloc");
496 if (name != NULL)
497 ag->ag_name = checked_strdup(name);
498 TAILQ_INIT(&ag->ag_auths);
499 TAILQ_INIT(&ag->ag_names);
500 TAILQ_INIT(&ag->ag_portals);
501 ag->ag_conf = conf;
502 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
503
504 return (ag);
505 }
506
507 void
auth_group_delete(struct auth_group * ag)508 auth_group_delete(struct auth_group *ag)
509 {
510 struct auth *auth, *auth_tmp;
511 struct auth_name *auth_name, *auth_name_tmp;
512 struct auth_portal *auth_portal, *auth_portal_tmp;
513
514 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
515
516 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
517 auth_delete(auth);
518 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
519 auth_name_delete(auth_name);
520 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
521 auth_portal_tmp)
522 auth_portal_delete(auth_portal);
523 free(ag->ag_name);
524 free(ag);
525 }
526
527 struct auth_group *
auth_group_find(const struct conf * conf,const char * name)528 auth_group_find(const struct conf *conf, const char *name)
529 {
530 struct auth_group *ag;
531
532 assert(name != NULL);
533 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
534 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
535 return (ag);
536 }
537
538 return (NULL);
539 }
540
541 int
auth_group_set_type(struct auth_group * ag,const char * str)542 auth_group_set_type(struct auth_group *ag, const char *str)
543 {
544 int type;
545
546 if (strcmp(str, "none") == 0) {
547 type = AG_TYPE_NO_AUTHENTICATION;
548 } else if (strcmp(str, "deny") == 0) {
549 type = AG_TYPE_DENY;
550 } else if (strcmp(str, "chap") == 0) {
551 type = AG_TYPE_CHAP;
552 } else if (strcmp(str, "chap-mutual") == 0) {
553 type = AG_TYPE_CHAP_MUTUAL;
554 } else {
555 if (ag->ag_name != NULL)
556 log_warnx("invalid auth-type \"%s\" for auth-group "
557 "\"%s\"", str, ag->ag_name);
558 else
559 log_warnx("invalid auth-type \"%s\" for target "
560 "\"%s\"", str, ag->ag_target->t_name);
561 return (1);
562 }
563
564 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
565 if (ag->ag_name != NULL) {
566 log_warnx("cannot set auth-type to \"%s\" for "
567 "auth-group \"%s\"; already has a different "
568 "type", str, ag->ag_name);
569 } else {
570 log_warnx("cannot set auth-type to \"%s\" for target "
571 "\"%s\"; already has a different type",
572 str, ag->ag_target->t_name);
573 }
574 return (1);
575 }
576
577 ag->ag_type = type;
578
579 return (0);
580 }
581
582 static struct portal *
portal_new(struct portal_group * pg)583 portal_new(struct portal_group *pg)
584 {
585 struct portal *portal;
586
587 portal = calloc(1, sizeof(*portal));
588 if (portal == NULL)
589 log_err(1, "calloc");
590 TAILQ_INIT(&portal->p_targets);
591 portal->p_portal_group = pg;
592 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
593 return (portal);
594 }
595
596 static void
portal_delete(struct portal * portal)597 portal_delete(struct portal *portal)
598 {
599
600 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
601 if (portal->p_ai != NULL)
602 freeaddrinfo(portal->p_ai);
603 free(portal->p_listen);
604 free(portal);
605 }
606
607 struct portal_group *
portal_group_new(struct conf * conf,const char * name)608 portal_group_new(struct conf *conf, const char *name)
609 {
610 struct portal_group *pg;
611
612 pg = portal_group_find(conf, name);
613 if (pg != NULL) {
614 log_warnx("duplicated portal-group \"%s\"", name);
615 return (NULL);
616 }
617
618 pg = calloc(1, sizeof(*pg));
619 if (pg == NULL)
620 log_err(1, "calloc");
621 pg->pg_name = checked_strdup(name);
622 TAILQ_INIT(&pg->pg_options);
623 TAILQ_INIT(&pg->pg_portals);
624 TAILQ_INIT(&pg->pg_ports);
625 pg->pg_conf = conf;
626 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
627 pg->pg_dscp = -1;
628 pg->pg_pcp = -1;
629 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
630
631 return (pg);
632 }
633
634 void
portal_group_delete(struct portal_group * pg)635 portal_group_delete(struct portal_group *pg)
636 {
637 struct portal *portal, *tmp;
638 struct port *port, *tport;
639 struct option *o, *otmp;
640
641 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
642 port_delete(port);
643 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
644
645 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
646 portal_delete(portal);
647 TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
648 option_delete(&pg->pg_options, o);
649 free(pg->pg_name);
650 free(pg->pg_offload);
651 free(pg->pg_redirection);
652 free(pg);
653 }
654
655 struct portal_group *
portal_group_find(const struct conf * conf,const char * name)656 portal_group_find(const struct conf *conf, const char *name)
657 {
658 struct portal_group *pg;
659
660 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
661 if (strcmp(pg->pg_name, name) == 0)
662 return (pg);
663 }
664
665 return (NULL);
666 }
667
668 static int
parse_addr_port(char * arg,const char * def_port,struct addrinfo ** ai)669 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
670 {
671 struct addrinfo hints;
672 char *str, *addr, *ch;
673 const char *port;
674 int error, colons = 0;
675
676 str = arg = strdup(arg);
677 if (arg[0] == '[') {
678 /*
679 * IPv6 address in square brackets, perhaps with port.
680 */
681 arg++;
682 addr = strsep(&arg, "]");
683 if (arg == NULL) {
684 free(str);
685 return (1);
686 }
687 if (arg[0] == '\0') {
688 port = def_port;
689 } else if (arg[0] == ':') {
690 port = arg + 1;
691 } else {
692 free(str);
693 return (1);
694 }
695 } else {
696 /*
697 * Either IPv6 address without brackets - and without
698 * a port - or IPv4 address. Just count the colons.
699 */
700 for (ch = arg; *ch != '\0'; ch++) {
701 if (*ch == ':')
702 colons++;
703 }
704 if (colons > 1) {
705 addr = arg;
706 port = def_port;
707 } else {
708 addr = strsep(&arg, ":");
709 if (arg == NULL)
710 port = def_port;
711 else
712 port = arg;
713 }
714 }
715
716 memset(&hints, 0, sizeof(hints));
717 hints.ai_family = PF_UNSPEC;
718 hints.ai_socktype = SOCK_STREAM;
719 hints.ai_flags = AI_PASSIVE;
720 error = getaddrinfo(addr, port, &hints, ai);
721 free(str);
722 return ((error != 0) ? 1 : 0);
723 }
724
725 int
portal_group_add_listen(struct portal_group * pg,const char * value,bool iser)726 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
727 {
728 struct portal *portal;
729
730 portal = portal_new(pg);
731 portal->p_listen = checked_strdup(value);
732 portal->p_iser = iser;
733
734 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
735 log_warnx("invalid listen address %s", portal->p_listen);
736 portal_delete(portal);
737 return (1);
738 }
739
740 /*
741 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
742 * those into multiple portals.
743 */
744
745 return (0);
746 }
747
748 int
isns_new(struct conf * conf,const char * addr)749 isns_new(struct conf *conf, const char *addr)
750 {
751 struct isns *isns;
752
753 isns = calloc(1, sizeof(*isns));
754 if (isns == NULL)
755 log_err(1, "calloc");
756 isns->i_conf = conf;
757 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
758 isns->i_addr = checked_strdup(addr);
759
760 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
761 log_warnx("invalid iSNS address %s", isns->i_addr);
762 isns_delete(isns);
763 return (1);
764 }
765
766 /*
767 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
768 * those into multiple servers.
769 */
770
771 return (0);
772 }
773
774 void
isns_delete(struct isns * isns)775 isns_delete(struct isns *isns)
776 {
777
778 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
779 free(isns->i_addr);
780 if (isns->i_ai != NULL)
781 freeaddrinfo(isns->i_ai);
782 free(isns);
783 }
784
785 static int
isns_do_connect(struct isns * isns)786 isns_do_connect(struct isns *isns)
787 {
788 int s;
789
790 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
791 isns->i_ai->ai_protocol);
792 if (s < 0) {
793 log_warn("socket(2) failed for %s", isns->i_addr);
794 return (-1);
795 }
796 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
797 log_warn("connect(2) failed for %s", isns->i_addr);
798 close(s);
799 return (-1);
800 }
801 return(s);
802 }
803
804 static int
isns_do_register(struct isns * isns,int s,const char * hostname)805 isns_do_register(struct isns *isns, int s, const char *hostname)
806 {
807 struct conf *conf = isns->i_conf;
808 struct target *target;
809 struct portal *portal;
810 struct portal_group *pg;
811 struct port *port;
812 struct isns_req *req;
813 int res = 0;
814 uint32_t error;
815
816 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
817 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
818 isns_req_add_delim(req);
819 isns_req_add_str(req, 1, hostname);
820 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
821 isns_req_add_32(req, 6, conf->conf_isns_period);
822 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
823 if (pg->pg_unassigned)
824 continue;
825 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
826 isns_req_add_addr(req, 16, portal->p_ai);
827 isns_req_add_port(req, 17, portal->p_ai);
828 }
829 }
830 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
831 isns_req_add_str(req, 32, target->t_name);
832 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
833 if (target->t_alias != NULL)
834 isns_req_add_str(req, 34, target->t_alias);
835 TAILQ_FOREACH(port, &target->t_ports, p_ts) {
836 if ((pg = port->p_portal_group) == NULL)
837 continue;
838 isns_req_add_32(req, 51, pg->pg_tag);
839 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
840 isns_req_add_addr(req, 49, portal->p_ai);
841 isns_req_add_port(req, 50, portal->p_ai);
842 }
843 }
844 }
845 res = isns_req_send(s, req);
846 if (res < 0) {
847 log_warn("send(2) failed for %s", isns->i_addr);
848 goto quit;
849 }
850 res = isns_req_receive(s, req);
851 if (res < 0) {
852 log_warn("receive(2) failed for %s", isns->i_addr);
853 goto quit;
854 }
855 error = isns_req_get_status(req);
856 if (error != 0) {
857 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
858 res = -1;
859 }
860 quit:
861 isns_req_free(req);
862 return (res);
863 }
864
865 static int
isns_do_check(struct isns * isns,int s,const char * hostname)866 isns_do_check(struct isns *isns, int s, const char *hostname)
867 {
868 struct conf *conf = isns->i_conf;
869 struct isns_req *req;
870 int res = 0;
871 uint32_t error;
872
873 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
874 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
875 isns_req_add_str(req, 1, hostname);
876 isns_req_add_delim(req);
877 isns_req_add(req, 2, 0, NULL);
878 res = isns_req_send(s, req);
879 if (res < 0) {
880 log_warn("send(2) failed for %s", isns->i_addr);
881 goto quit;
882 }
883 res = isns_req_receive(s, req);
884 if (res < 0) {
885 log_warn("receive(2) failed for %s", isns->i_addr);
886 goto quit;
887 }
888 error = isns_req_get_status(req);
889 if (error != 0) {
890 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
891 res = -1;
892 }
893 quit:
894 isns_req_free(req);
895 return (res);
896 }
897
898 static int
isns_do_deregister(struct isns * isns,int s,const char * hostname)899 isns_do_deregister(struct isns *isns, int s, const char *hostname)
900 {
901 struct conf *conf = isns->i_conf;
902 struct isns_req *req;
903 int res = 0;
904 uint32_t error;
905
906 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
907 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
908 isns_req_add_delim(req);
909 isns_req_add_str(req, 1, hostname);
910 res = isns_req_send(s, req);
911 if (res < 0) {
912 log_warn("send(2) failed for %s", isns->i_addr);
913 goto quit;
914 }
915 res = isns_req_receive(s, req);
916 if (res < 0) {
917 log_warn("receive(2) failed for %s", isns->i_addr);
918 goto quit;
919 }
920 error = isns_req_get_status(req);
921 if (error != 0) {
922 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
923 res = -1;
924 }
925 quit:
926 isns_req_free(req);
927 return (res);
928 }
929
930 void
isns_register(struct isns * isns,struct isns * oldisns)931 isns_register(struct isns *isns, struct isns *oldisns)
932 {
933 struct conf *conf = isns->i_conf;
934 int error, s;
935 char hostname[256];
936
937 if (TAILQ_EMPTY(&conf->conf_targets) ||
938 TAILQ_EMPTY(&conf->conf_portal_groups))
939 return;
940 set_timeout(conf->conf_isns_timeout, false);
941 s = isns_do_connect(isns);
942 if (s < 0) {
943 set_timeout(0, false);
944 return;
945 }
946 error = gethostname(hostname, sizeof(hostname));
947 if (error != 0)
948 log_err(1, "gethostname");
949
950 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
951 oldisns = isns;
952 isns_do_deregister(oldisns, s, hostname);
953 isns_do_register(isns, s, hostname);
954 close(s);
955 set_timeout(0, false);
956 }
957
958 void
isns_check(struct isns * isns)959 isns_check(struct isns *isns)
960 {
961 struct conf *conf = isns->i_conf;
962 int error, s, res;
963 char hostname[256];
964
965 if (TAILQ_EMPTY(&conf->conf_targets) ||
966 TAILQ_EMPTY(&conf->conf_portal_groups))
967 return;
968 set_timeout(conf->conf_isns_timeout, false);
969 s = isns_do_connect(isns);
970 if (s < 0) {
971 set_timeout(0, false);
972 return;
973 }
974 error = gethostname(hostname, sizeof(hostname));
975 if (error != 0)
976 log_err(1, "gethostname");
977
978 res = isns_do_check(isns, s, hostname);
979 if (res < 0) {
980 isns_do_deregister(isns, s, hostname);
981 isns_do_register(isns, s, hostname);
982 }
983 close(s);
984 set_timeout(0, false);
985 }
986
987 void
isns_deregister(struct isns * isns)988 isns_deregister(struct isns *isns)
989 {
990 struct conf *conf = isns->i_conf;
991 int error, s;
992 char hostname[256];
993
994 if (TAILQ_EMPTY(&conf->conf_targets) ||
995 TAILQ_EMPTY(&conf->conf_portal_groups))
996 return;
997 set_timeout(conf->conf_isns_timeout, false);
998 s = isns_do_connect(isns);
999 if (s < 0)
1000 return;
1001 error = gethostname(hostname, sizeof(hostname));
1002 if (error != 0)
1003 log_err(1, "gethostname");
1004
1005 isns_do_deregister(isns, s, hostname);
1006 close(s);
1007 set_timeout(0, false);
1008 }
1009
1010 int
portal_group_set_filter(struct portal_group * pg,const char * str)1011 portal_group_set_filter(struct portal_group *pg, const char *str)
1012 {
1013 int filter;
1014
1015 if (strcmp(str, "none") == 0) {
1016 filter = PG_FILTER_NONE;
1017 } else if (strcmp(str, "portal") == 0) {
1018 filter = PG_FILTER_PORTAL;
1019 } else if (strcmp(str, "portal-name") == 0) {
1020 filter = PG_FILTER_PORTAL_NAME;
1021 } else if (strcmp(str, "portal-name-auth") == 0) {
1022 filter = PG_FILTER_PORTAL_NAME_AUTH;
1023 } else {
1024 log_warnx("invalid discovery-filter \"%s\" for portal-group "
1025 "\"%s\"; valid values are \"none\", \"portal\", "
1026 "\"portal-name\", and \"portal-name-auth\"",
1027 str, pg->pg_name);
1028 return (1);
1029 }
1030
1031 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1032 pg->pg_discovery_filter != filter) {
1033 log_warnx("cannot set discovery-filter to \"%s\" for "
1034 "portal-group \"%s\"; already has a different "
1035 "value", str, pg->pg_name);
1036 return (1);
1037 }
1038
1039 pg->pg_discovery_filter = filter;
1040
1041 return (0);
1042 }
1043
1044 int
portal_group_set_offload(struct portal_group * pg,const char * offload)1045 portal_group_set_offload(struct portal_group *pg, const char *offload)
1046 {
1047
1048 if (pg->pg_offload != NULL) {
1049 log_warnx("cannot set offload to \"%s\" for "
1050 "portal-group \"%s\"; already defined",
1051 offload, pg->pg_name);
1052 return (1);
1053 }
1054
1055 pg->pg_offload = checked_strdup(offload);
1056
1057 return (0);
1058 }
1059
1060 int
portal_group_set_redirection(struct portal_group * pg,const char * addr)1061 portal_group_set_redirection(struct portal_group *pg, const char *addr)
1062 {
1063
1064 if (pg->pg_redirection != NULL) {
1065 log_warnx("cannot set redirection to \"%s\" for "
1066 "portal-group \"%s\"; already defined",
1067 addr, pg->pg_name);
1068 return (1);
1069 }
1070
1071 pg->pg_redirection = checked_strdup(addr);
1072
1073 return (0);
1074 }
1075
1076 static bool
valid_hex(const char ch)1077 valid_hex(const char ch)
1078 {
1079 switch (ch) {
1080 case '0':
1081 case '1':
1082 case '2':
1083 case '3':
1084 case '4':
1085 case '5':
1086 case '6':
1087 case '7':
1088 case '8':
1089 case '9':
1090 case 'a':
1091 case 'A':
1092 case 'b':
1093 case 'B':
1094 case 'c':
1095 case 'C':
1096 case 'd':
1097 case 'D':
1098 case 'e':
1099 case 'E':
1100 case 'f':
1101 case 'F':
1102 return (true);
1103 default:
1104 return (false);
1105 }
1106 }
1107
1108 bool
valid_iscsi_name(const char * name)1109 valid_iscsi_name(const char *name)
1110 {
1111 int i;
1112
1113 if (strlen(name) >= MAX_NAME_LEN) {
1114 log_warnx("overlong name for target \"%s\"; max length allowed "
1115 "by iSCSI specification is %d characters",
1116 name, MAX_NAME_LEN);
1117 return (false);
1118 }
1119
1120 /*
1121 * In the cases below, we don't return an error, just in case the admin
1122 * was right, and we're wrong.
1123 */
1124 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1125 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1126 /*
1127 * XXX: We should verify UTF-8 normalisation, as defined
1128 * by 3.2.6.2: iSCSI Name Encoding.
1129 */
1130 if (isalnum(name[i]))
1131 continue;
1132 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1133 continue;
1134 log_warnx("invalid character \"%c\" in target name "
1135 "\"%s\"; allowed characters are letters, digits, "
1136 "'-', '.', and ':'", name[i], name);
1137 break;
1138 }
1139 /*
1140 * XXX: Check more stuff: valid date and a valid reversed domain.
1141 */
1142 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1143 if (strlen(name) != strlen("eui.") + 16)
1144 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1145 "should be followed by exactly 16 hexadecimal "
1146 "digits", name);
1147 for (i = strlen("eui."); name[i] != '\0'; i++) {
1148 if (!valid_hex(name[i])) {
1149 log_warnx("invalid character \"%c\" in target "
1150 "name \"%s\"; allowed characters are 1-9 "
1151 "and A-F", name[i], name);
1152 break;
1153 }
1154 }
1155 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1156 if (strlen(name) > strlen("naa.") + 32)
1157 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1158 "should be followed by at most 32 hexadecimal "
1159 "digits", name);
1160 for (i = strlen("naa."); name[i] != '\0'; i++) {
1161 if (!valid_hex(name[i])) {
1162 log_warnx("invalid character \"%c\" in target "
1163 "name \"%s\"; allowed characters are 1-9 "
1164 "and A-F", name[i], name);
1165 break;
1166 }
1167 }
1168 } else {
1169 log_warnx("invalid target name \"%s\"; should start with "
1170 "either \"iqn.\", \"eui.\", or \"naa.\"",
1171 name);
1172 }
1173 return (true);
1174 }
1175
1176 struct pport *
pport_new(struct kports * kports,const char * name,uint32_t ctl_port)1177 pport_new(struct kports *kports, const char *name, uint32_t ctl_port)
1178 {
1179 struct pport *pp;
1180
1181 pp = calloc(1, sizeof(*pp));
1182 if (pp == NULL)
1183 log_err(1, "calloc");
1184 pp->pp_kports = kports;
1185 pp->pp_name = checked_strdup(name);
1186 pp->pp_ctl_port = ctl_port;
1187 TAILQ_INIT(&pp->pp_ports);
1188 TAILQ_INSERT_TAIL(&kports->pports, pp, pp_next);
1189 return (pp);
1190 }
1191
1192 struct pport *
pport_find(const struct kports * kports,const char * name)1193 pport_find(const struct kports *kports, const char *name)
1194 {
1195 struct pport *pp;
1196
1197 TAILQ_FOREACH(pp, &kports->pports, pp_next) {
1198 if (strcasecmp(pp->pp_name, name) == 0)
1199 return (pp);
1200 }
1201 return (NULL);
1202 }
1203
1204 struct pport *
pport_copy(struct pport * pp,struct kports * kports)1205 pport_copy(struct pport *pp, struct kports *kports)
1206 {
1207 struct pport *ppnew;
1208
1209 ppnew = pport_new(kports, pp->pp_name, pp->pp_ctl_port);
1210 return (ppnew);
1211 }
1212
1213 void
pport_delete(struct pport * pp)1214 pport_delete(struct pport *pp)
1215 {
1216 struct port *port, *tport;
1217
1218 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1219 port_delete(port);
1220 TAILQ_REMOVE(&pp->pp_kports->pports, pp, pp_next);
1221 free(pp->pp_name);
1222 free(pp);
1223 }
1224
1225 struct port *
port_new(struct conf * conf,struct target * target,struct portal_group * pg)1226 port_new(struct conf *conf, struct target *target, struct portal_group *pg)
1227 {
1228 struct port *port;
1229 char *name;
1230 int ret;
1231
1232 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1233 if (ret <= 0)
1234 log_err(1, "asprintf");
1235 if (port_find(conf, name) != NULL) {
1236 log_warnx("duplicate port \"%s\"", name);
1237 free(name);
1238 return (NULL);
1239 }
1240 port = calloc(1, sizeof(*port));
1241 if (port == NULL)
1242 log_err(1, "calloc");
1243 port->p_conf = conf;
1244 port->p_name = name;
1245 port->p_ioctl_port = 0;
1246 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1247 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1248 port->p_target = target;
1249 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1250 port->p_portal_group = pg;
1251 return (port);
1252 }
1253
1254 struct port *
port_new_ioctl(struct conf * conf,struct kports * kports,struct target * target,int pp,int vp)1255 port_new_ioctl(struct conf *conf, struct kports *kports, struct target *target,
1256 int pp, int vp)
1257 {
1258 struct pport *pport;
1259 struct port *port;
1260 char *pname;
1261 char *name;
1262 int ret;
1263
1264 ret = asprintf(&pname, "ioctl/%d/%d", pp, vp);
1265 if (ret <= 0) {
1266 log_err(1, "asprintf");
1267 return (NULL);
1268 }
1269
1270 pport = pport_find(kports, pname);
1271 if (pport != NULL) {
1272 free(pname);
1273 return (port_new_pp(conf, target, pport));
1274 }
1275
1276 ret = asprintf(&name, "%s-%s", pname, target->t_name);
1277 free(pname);
1278
1279 if (ret <= 0)
1280 log_err(1, "asprintf");
1281 if (port_find(conf, name) != NULL) {
1282 log_warnx("duplicate port \"%s\"", name);
1283 free(name);
1284 return (NULL);
1285 }
1286 port = calloc(1, sizeof(*port));
1287 if (port == NULL)
1288 log_err(1, "calloc");
1289 port->p_conf = conf;
1290 port->p_name = name;
1291 port->p_ioctl_port = 1;
1292 port->p_ioctl_pp = pp;
1293 port->p_ioctl_vp = vp;
1294 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1295 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1296 port->p_target = target;
1297 return (port);
1298 }
1299
1300 struct port *
port_new_pp(struct conf * conf,struct target * target,struct pport * pp)1301 port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1302 {
1303 struct port *port;
1304 char *name;
1305 int ret;
1306
1307 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1308 if (ret <= 0)
1309 log_err(1, "asprintf");
1310 if (port_find(conf, name) != NULL) {
1311 log_warnx("duplicate port \"%s\"", name);
1312 free(name);
1313 return (NULL);
1314 }
1315 port = calloc(1, sizeof(*port));
1316 if (port == NULL)
1317 log_err(1, "calloc");
1318 port->p_conf = conf;
1319 port->p_name = name;
1320 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1321 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1322 port->p_target = target;
1323 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1324 port->p_pport = pp;
1325 return (port);
1326 }
1327
1328 struct port *
port_find(const struct conf * conf,const char * name)1329 port_find(const struct conf *conf, const char *name)
1330 {
1331 struct port *port;
1332
1333 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1334 if (strcasecmp(port->p_name, name) == 0)
1335 return (port);
1336 }
1337
1338 return (NULL);
1339 }
1340
1341 struct port *
port_find_in_pg(const struct portal_group * pg,const char * target)1342 port_find_in_pg(const struct portal_group *pg, const char *target)
1343 {
1344 struct port *port;
1345
1346 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1347 if (strcasecmp(port->p_target->t_name, target) == 0)
1348 return (port);
1349 }
1350
1351 return (NULL);
1352 }
1353
1354 void
port_delete(struct port * port)1355 port_delete(struct port *port)
1356 {
1357
1358 if (port->p_portal_group)
1359 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1360 if (port->p_pport)
1361 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1362 if (port->p_target)
1363 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1364 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1365 free(port->p_name);
1366 free(port);
1367 }
1368
1369 int
port_is_dummy(struct port * port)1370 port_is_dummy(struct port *port)
1371 {
1372
1373 if (port->p_portal_group) {
1374 if (port->p_portal_group->pg_foreign)
1375 return (1);
1376 if (TAILQ_EMPTY(&port->p_portal_group->pg_portals))
1377 return (1);
1378 }
1379 return (0);
1380 }
1381
1382 struct target *
target_new(struct conf * conf,const char * name)1383 target_new(struct conf *conf, const char *name)
1384 {
1385 struct target *targ;
1386 int i, len;
1387
1388 targ = target_find(conf, name);
1389 if (targ != NULL) {
1390 log_warnx("duplicated target \"%s\"", name);
1391 return (NULL);
1392 }
1393 if (valid_iscsi_name(name) == false) {
1394 log_warnx("target name \"%s\" is invalid", name);
1395 return (NULL);
1396 }
1397 targ = calloc(1, sizeof(*targ));
1398 if (targ == NULL)
1399 log_err(1, "calloc");
1400 targ->t_name = checked_strdup(name);
1401
1402 /*
1403 * RFC 3722 requires us to normalize the name to lowercase.
1404 */
1405 len = strlen(name);
1406 for (i = 0; i < len; i++)
1407 targ->t_name[i] = tolower(targ->t_name[i]);
1408
1409 targ->t_conf = conf;
1410 TAILQ_INIT(&targ->t_ports);
1411 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1412
1413 return (targ);
1414 }
1415
1416 void
target_delete(struct target * targ)1417 target_delete(struct target *targ)
1418 {
1419 struct port *port, *tport;
1420
1421 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1422 port_delete(port);
1423 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1424
1425 free(targ->t_pport);
1426 free(targ->t_name);
1427 free(targ->t_redirection);
1428 free(targ);
1429 }
1430
1431 struct target *
target_find(struct conf * conf,const char * name)1432 target_find(struct conf *conf, const char *name)
1433 {
1434 struct target *targ;
1435
1436 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1437 if (strcasecmp(targ->t_name, name) == 0)
1438 return (targ);
1439 }
1440
1441 return (NULL);
1442 }
1443
1444 int
target_set_redirection(struct target * target,const char * addr)1445 target_set_redirection(struct target *target, const char *addr)
1446 {
1447
1448 if (target->t_redirection != NULL) {
1449 log_warnx("cannot set redirection to \"%s\" for "
1450 "target \"%s\"; already defined",
1451 addr, target->t_name);
1452 return (1);
1453 }
1454
1455 target->t_redirection = checked_strdup(addr);
1456
1457 return (0);
1458 }
1459
1460 struct lun *
lun_new(struct conf * conf,const char * name)1461 lun_new(struct conf *conf, const char *name)
1462 {
1463 struct lun *lun;
1464
1465 lun = lun_find(conf, name);
1466 if (lun != NULL) {
1467 log_warnx("duplicated lun \"%s\"", name);
1468 return (NULL);
1469 }
1470
1471 lun = calloc(1, sizeof(*lun));
1472 if (lun == NULL)
1473 log_err(1, "calloc");
1474 lun->l_conf = conf;
1475 lun->l_name = checked_strdup(name);
1476 TAILQ_INIT(&lun->l_options);
1477 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1478 lun->l_ctl_lun = -1;
1479
1480 return (lun);
1481 }
1482
1483 void
lun_delete(struct lun * lun)1484 lun_delete(struct lun *lun)
1485 {
1486 struct target *targ;
1487 struct option *o, *tmp;
1488 int i;
1489
1490 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1491 for (i = 0; i < MAX_LUNS; i++) {
1492 if (targ->t_luns[i] == lun)
1493 targ->t_luns[i] = NULL;
1494 }
1495 }
1496 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1497
1498 TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1499 option_delete(&lun->l_options, o);
1500 free(lun->l_name);
1501 free(lun->l_backend);
1502 free(lun->l_device_id);
1503 free(lun->l_path);
1504 free(lun->l_scsiname);
1505 free(lun->l_serial);
1506 free(lun);
1507 }
1508
1509 struct lun *
lun_find(const struct conf * conf,const char * name)1510 lun_find(const struct conf *conf, const char *name)
1511 {
1512 struct lun *lun;
1513
1514 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1515 if (strcmp(lun->l_name, name) == 0)
1516 return (lun);
1517 }
1518
1519 return (NULL);
1520 }
1521
1522 void
lun_set_backend(struct lun * lun,const char * value)1523 lun_set_backend(struct lun *lun, const char *value)
1524 {
1525 free(lun->l_backend);
1526 lun->l_backend = checked_strdup(value);
1527 }
1528
1529 void
lun_set_blocksize(struct lun * lun,size_t value)1530 lun_set_blocksize(struct lun *lun, size_t value)
1531 {
1532
1533 lun->l_blocksize = value;
1534 }
1535
1536 void
lun_set_device_type(struct lun * lun,uint8_t value)1537 lun_set_device_type(struct lun *lun, uint8_t value)
1538 {
1539
1540 lun->l_device_type = value;
1541 }
1542
1543 void
lun_set_device_id(struct lun * lun,const char * value)1544 lun_set_device_id(struct lun *lun, const char *value)
1545 {
1546 free(lun->l_device_id);
1547 lun->l_device_id = checked_strdup(value);
1548 }
1549
1550 void
lun_set_path(struct lun * lun,const char * value)1551 lun_set_path(struct lun *lun, const char *value)
1552 {
1553 free(lun->l_path);
1554 lun->l_path = checked_strdup(value);
1555 }
1556
1557 void
lun_set_scsiname(struct lun * lun,const char * value)1558 lun_set_scsiname(struct lun *lun, const char *value)
1559 {
1560 free(lun->l_scsiname);
1561 lun->l_scsiname = checked_strdup(value);
1562 }
1563
1564 void
lun_set_serial(struct lun * lun,const char * value)1565 lun_set_serial(struct lun *lun, const char *value)
1566 {
1567 free(lun->l_serial);
1568 lun->l_serial = checked_strdup(value);
1569 }
1570
1571 void
lun_set_size(struct lun * lun,size_t value)1572 lun_set_size(struct lun *lun, size_t value)
1573 {
1574
1575 lun->l_size = value;
1576 }
1577
1578 void
lun_set_ctl_lun(struct lun * lun,uint32_t value)1579 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1580 {
1581
1582 lun->l_ctl_lun = value;
1583 }
1584
1585 struct option *
option_new(struct options * options,const char * name,const char * value)1586 option_new(struct options *options, const char *name, const char *value)
1587 {
1588 struct option *o;
1589
1590 o = option_find(options, name);
1591 if (o != NULL) {
1592 log_warnx("duplicated option \"%s\"", name);
1593 return (NULL);
1594 }
1595
1596 o = calloc(1, sizeof(*o));
1597 if (o == NULL)
1598 log_err(1, "calloc");
1599 o->o_name = checked_strdup(name);
1600 o->o_value = checked_strdup(value);
1601 TAILQ_INSERT_TAIL(options, o, o_next);
1602
1603 return (o);
1604 }
1605
1606 void
option_delete(struct options * options,struct option * o)1607 option_delete(struct options *options, struct option *o)
1608 {
1609
1610 TAILQ_REMOVE(options, o, o_next);
1611 free(o->o_name);
1612 free(o->o_value);
1613 free(o);
1614 }
1615
1616 struct option *
option_find(const struct options * options,const char * name)1617 option_find(const struct options *options, const char *name)
1618 {
1619 struct option *o;
1620
1621 TAILQ_FOREACH(o, options, o_next) {
1622 if (strcmp(o->o_name, name) == 0)
1623 return (o);
1624 }
1625
1626 return (NULL);
1627 }
1628
1629 void
option_set(struct option * o,const char * value)1630 option_set(struct option *o, const char *value)
1631 {
1632
1633 free(o->o_value);
1634 o->o_value = checked_strdup(value);
1635 }
1636
1637 #ifdef ICL_KERNEL_PROXY
1638
1639 static void
pdu_receive_proxy(struct pdu * pdu)1640 pdu_receive_proxy(struct pdu *pdu)
1641 {
1642 struct connection *conn;
1643 size_t len;
1644
1645 assert(proxy_mode);
1646 conn = pdu->pdu_connection;
1647
1648 kernel_receive(pdu);
1649
1650 len = pdu_ahs_length(pdu);
1651 if (len > 0)
1652 log_errx(1, "protocol error: non-empty AHS");
1653
1654 len = pdu_data_segment_length(pdu);
1655 assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
1656 pdu->pdu_data_len = len;
1657 }
1658
1659 static void
pdu_send_proxy(struct pdu * pdu)1660 pdu_send_proxy(struct pdu *pdu)
1661 {
1662
1663 assert(proxy_mode);
1664
1665 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
1666 kernel_send(pdu);
1667 }
1668
1669 #endif /* ICL_KERNEL_PROXY */
1670
1671 static void
pdu_fail(const struct connection * conn __unused,const char * reason __unused)1672 pdu_fail(const struct connection *conn __unused, const char *reason __unused)
1673 {
1674 }
1675
1676 static struct ctld_connection *
connection_new(struct portal * portal,int fd,const char * host,const struct sockaddr * client_sa)1677 connection_new(struct portal *portal, int fd, const char *host,
1678 const struct sockaddr *client_sa)
1679 {
1680 struct ctld_connection *conn;
1681
1682 conn = calloc(1, sizeof(*conn));
1683 if (conn == NULL)
1684 log_err(1, "calloc");
1685 connection_init(&conn->conn, &conn_ops, proxy_mode);
1686 conn->conn.conn_socket = fd;
1687 conn->conn_portal = portal;
1688 conn->conn_initiator_addr = checked_strdup(host);
1689 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1690
1691 return (conn);
1692 }
1693
1694 #if 0
1695 static void
1696 conf_print(struct conf *conf)
1697 {
1698 struct auth_group *ag;
1699 struct auth *auth;
1700 struct auth_name *auth_name;
1701 struct auth_portal *auth_portal;
1702 struct portal_group *pg;
1703 struct portal *portal;
1704 struct target *targ;
1705 struct lun *lun;
1706 struct option *o;
1707
1708 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1709 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1710 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1711 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1712 auth->a_user, auth->a_secret,
1713 auth->a_mutual_user, auth->a_mutual_secret);
1714 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1715 fprintf(stderr, "\t initiator-name %s\n",
1716 auth_name->an_initiator_name);
1717 TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next)
1718 fprintf(stderr, "\t initiator-portal %s\n",
1719 auth_portal->ap_initiator_portal);
1720 fprintf(stderr, "}\n");
1721 }
1722 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1723 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1724 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1725 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1726 fprintf(stderr, "}\n");
1727 }
1728 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1729 fprintf(stderr, "\tlun %s {\n", lun->l_name);
1730 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1731 TAILQ_FOREACH(o, &lun->l_options, o_next)
1732 fprintf(stderr, "\t\toption %s %s\n",
1733 o->o_name, o->o_value);
1734 fprintf(stderr, "\t}\n");
1735 }
1736 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1737 fprintf(stderr, "target %s {\n", targ->t_name);
1738 if (targ->t_alias != NULL)
1739 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1740 fprintf(stderr, "}\n");
1741 }
1742 }
1743 #endif
1744
1745 static int
conf_verify_lun(struct lun * lun)1746 conf_verify_lun(struct lun *lun)
1747 {
1748 const struct lun *lun2;
1749
1750 if (lun->l_backend == NULL)
1751 lun_set_backend(lun, "block");
1752 if (strcmp(lun->l_backend, "block") == 0) {
1753 if (lun->l_path == NULL) {
1754 log_warnx("missing path for lun \"%s\"",
1755 lun->l_name);
1756 return (1);
1757 }
1758 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1759 if (lun->l_size == 0) {
1760 log_warnx("missing size for ramdisk-backed lun \"%s\"",
1761 lun->l_name);
1762 return (1);
1763 }
1764 if (lun->l_path != NULL) {
1765 log_warnx("path must not be specified "
1766 "for ramdisk-backed lun \"%s\"",
1767 lun->l_name);
1768 return (1);
1769 }
1770 }
1771 if (lun->l_blocksize == 0) {
1772 if (lun->l_device_type == 5)
1773 lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1774 else
1775 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1776 } else if (lun->l_blocksize < 0) {
1777 log_warnx("invalid blocksize for lun \"%s\"; "
1778 "must be larger than 0", lun->l_name);
1779 return (1);
1780 }
1781 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1782 log_warnx("invalid size for lun \"%s\"; "
1783 "must be multiple of blocksize", lun->l_name);
1784 return (1);
1785 }
1786 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1787 if (lun == lun2)
1788 continue;
1789 if (lun->l_path != NULL && lun2->l_path != NULL &&
1790 strcmp(lun->l_path, lun2->l_path) == 0) {
1791 log_debugx("WARNING: path \"%s\" duplicated "
1792 "between lun \"%s\", and "
1793 "lun \"%s\"", lun->l_path,
1794 lun->l_name, lun2->l_name);
1795 }
1796 }
1797
1798 return (0);
1799 }
1800
1801 int
conf_verify(struct conf * conf)1802 conf_verify(struct conf *conf)
1803 {
1804 struct auth_group *ag;
1805 struct portal_group *pg;
1806 struct port *port;
1807 struct target *targ;
1808 struct lun *lun;
1809 bool found;
1810 int error, i;
1811
1812 if (conf->conf_pidfile_path == NULL)
1813 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1814
1815 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1816 error = conf_verify_lun(lun);
1817 if (error != 0)
1818 return (error);
1819 }
1820 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1821 if (targ->t_auth_group == NULL) {
1822 targ->t_auth_group = auth_group_find(conf,
1823 "default");
1824 assert(targ->t_auth_group != NULL);
1825 }
1826 if (TAILQ_EMPTY(&targ->t_ports)) {
1827 pg = portal_group_find(conf, "default");
1828 assert(pg != NULL);
1829 port_new(conf, targ, pg);
1830 }
1831 found = false;
1832 for (i = 0; i < MAX_LUNS; i++) {
1833 if (targ->t_luns[i] != NULL)
1834 found = true;
1835 }
1836 if (!found && targ->t_redirection == NULL) {
1837 log_warnx("no LUNs defined for target \"%s\"",
1838 targ->t_name);
1839 }
1840 if (found && targ->t_redirection != NULL) {
1841 log_debugx("target \"%s\" contains luns, "
1842 " but configured for redirection",
1843 targ->t_name);
1844 }
1845 }
1846 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1847 assert(pg->pg_name != NULL);
1848 if (pg->pg_discovery_auth_group == NULL) {
1849 pg->pg_discovery_auth_group =
1850 auth_group_find(conf, "default");
1851 assert(pg->pg_discovery_auth_group != NULL);
1852 }
1853
1854 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1855 pg->pg_discovery_filter = PG_FILTER_NONE;
1856
1857 if (pg->pg_redirection != NULL) {
1858 if (!TAILQ_EMPTY(&pg->pg_ports)) {
1859 log_debugx("portal-group \"%s\" assigned "
1860 "to target, but configured "
1861 "for redirection",
1862 pg->pg_name);
1863 }
1864 pg->pg_unassigned = false;
1865 } else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1866 pg->pg_unassigned = false;
1867 } else {
1868 if (strcmp(pg->pg_name, "default") != 0)
1869 log_warnx("portal-group \"%s\" not assigned "
1870 "to any target", pg->pg_name);
1871 pg->pg_unassigned = true;
1872 }
1873 }
1874 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1875 if (ag->ag_name == NULL)
1876 assert(ag->ag_target != NULL);
1877 else
1878 assert(ag->ag_target == NULL);
1879
1880 found = false;
1881 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1882 if (targ->t_auth_group == ag) {
1883 found = true;
1884 break;
1885 }
1886 }
1887 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1888 if (port->p_auth_group == ag) {
1889 found = true;
1890 break;
1891 }
1892 }
1893 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1894 if (pg->pg_discovery_auth_group == ag) {
1895 found = true;
1896 break;
1897 }
1898 }
1899 if (!found && ag->ag_name != NULL &&
1900 strcmp(ag->ag_name, "default") != 0 &&
1901 strcmp(ag->ag_name, "no-authentication") != 0 &&
1902 strcmp(ag->ag_name, "no-access") != 0) {
1903 log_warnx("auth-group \"%s\" not assigned "
1904 "to any target", ag->ag_name);
1905 }
1906 }
1907
1908 return (0);
1909 }
1910
1911 static int
conf_apply(struct conf * oldconf,struct conf * newconf)1912 conf_apply(struct conf *oldconf, struct conf *newconf)
1913 {
1914 struct lun *oldlun, *newlun, *tmplun;
1915 struct portal_group *oldpg, *newpg;
1916 struct portal *oldp, *newp;
1917 struct port *oldport, *newport, *tmpport;
1918 struct isns *oldns, *newns;
1919 int changed, cumulated_error = 0, error, sockbuf;
1920 int one = 1;
1921
1922 if (oldconf->conf_debug != newconf->conf_debug) {
1923 log_debugx("changing debug level to %d", newconf->conf_debug);
1924 log_init(newconf->conf_debug);
1925 }
1926
1927 if (oldconf->conf_pidfile_path != NULL &&
1928 newconf->conf_pidfile_path != NULL)
1929 {
1930 if (strcmp(oldconf->conf_pidfile_path,
1931 newconf->conf_pidfile_path) != 0)
1932 {
1933 /* pidfile has changed. rename it */
1934 log_debugx("moving pidfile to %s",
1935 newconf->conf_pidfile_path);
1936 if (rename(oldconf->conf_pidfile_path,
1937 newconf->conf_pidfile_path))
1938 {
1939 log_err(1, "renaming pidfile %s -> %s",
1940 oldconf->conf_pidfile_path,
1941 newconf->conf_pidfile_path);
1942 }
1943 }
1944 newconf->conf_pidfh = oldconf->conf_pidfh;
1945 oldconf->conf_pidfh = NULL;
1946 }
1947
1948 /*
1949 * Go through the new portal groups, assigning tags or preserving old.
1950 */
1951 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1952 if (newpg->pg_tag != 0)
1953 continue;
1954 oldpg = portal_group_find(oldconf, newpg->pg_name);
1955 if (oldpg != NULL)
1956 newpg->pg_tag = oldpg->pg_tag;
1957 else
1958 newpg->pg_tag = ++last_portal_group_tag;
1959 }
1960
1961 /* Deregister on removed iSNS servers. */
1962 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1963 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1964 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1965 break;
1966 }
1967 if (newns == NULL)
1968 isns_deregister(oldns);
1969 }
1970
1971 /*
1972 * XXX: If target or lun removal fails, we should somehow "move"
1973 * the old lun or target into newconf, so that subsequent
1974 * conf_apply() would try to remove them again. That would
1975 * be somewhat hairy, though, and lun deletion failures don't
1976 * really happen, so leave it as it is for now.
1977 */
1978 /*
1979 * First, remove any ports present in the old configuration
1980 * and missing in the new one.
1981 */
1982 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1983 if (port_is_dummy(oldport))
1984 continue;
1985 newport = port_find(newconf, oldport->p_name);
1986 if (newport != NULL && !port_is_dummy(newport))
1987 continue;
1988 log_debugx("removing port \"%s\"", oldport->p_name);
1989 error = kernel_port_remove(oldport);
1990 if (error != 0) {
1991 log_warnx("failed to remove port %s",
1992 oldport->p_name);
1993 /*
1994 * XXX: Uncomment after fixing the root cause.
1995 *
1996 * cumulated_error++;
1997 */
1998 }
1999 }
2000
2001 /*
2002 * Second, remove any LUNs present in the old configuration
2003 * and missing in the new one.
2004 */
2005 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
2006 newlun = lun_find(newconf, oldlun->l_name);
2007 if (newlun == NULL) {
2008 log_debugx("lun \"%s\", CTL lun %d "
2009 "not found in new configuration; "
2010 "removing", oldlun->l_name, oldlun->l_ctl_lun);
2011 error = kernel_lun_remove(oldlun);
2012 if (error != 0) {
2013 log_warnx("failed to remove lun \"%s\", "
2014 "CTL lun %d",
2015 oldlun->l_name, oldlun->l_ctl_lun);
2016 cumulated_error++;
2017 }
2018 continue;
2019 }
2020
2021 /*
2022 * Also remove the LUNs changed by more than size.
2023 */
2024 changed = 0;
2025 assert(oldlun->l_backend != NULL);
2026 assert(newlun->l_backend != NULL);
2027 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
2028 log_debugx("backend for lun \"%s\", "
2029 "CTL lun %d changed; removing",
2030 oldlun->l_name, oldlun->l_ctl_lun);
2031 changed = 1;
2032 }
2033 if (oldlun->l_blocksize != newlun->l_blocksize) {
2034 log_debugx("blocksize for lun \"%s\", "
2035 "CTL lun %d changed; removing",
2036 oldlun->l_name, oldlun->l_ctl_lun);
2037 changed = 1;
2038 }
2039 if (newlun->l_device_id != NULL &&
2040 (oldlun->l_device_id == NULL ||
2041 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
2042 0)) {
2043 log_debugx("device-id for lun \"%s\", "
2044 "CTL lun %d changed; removing",
2045 oldlun->l_name, oldlun->l_ctl_lun);
2046 changed = 1;
2047 }
2048 if (newlun->l_path != NULL &&
2049 (oldlun->l_path == NULL ||
2050 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
2051 log_debugx("path for lun \"%s\", "
2052 "CTL lun %d, changed; removing",
2053 oldlun->l_name, oldlun->l_ctl_lun);
2054 changed = 1;
2055 }
2056 if (newlun->l_serial != NULL &&
2057 (oldlun->l_serial == NULL ||
2058 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
2059 log_debugx("serial for lun \"%s\", "
2060 "CTL lun %d changed; removing",
2061 oldlun->l_name, oldlun->l_ctl_lun);
2062 changed = 1;
2063 }
2064 if (changed) {
2065 error = kernel_lun_remove(oldlun);
2066 if (error != 0) {
2067 log_warnx("failed to remove lun \"%s\", "
2068 "CTL lun %d",
2069 oldlun->l_name, oldlun->l_ctl_lun);
2070 cumulated_error++;
2071 }
2072 lun_delete(oldlun);
2073 continue;
2074 }
2075
2076 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
2077 }
2078
2079 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
2080 oldlun = lun_find(oldconf, newlun->l_name);
2081 if (oldlun != NULL) {
2082 log_debugx("modifying lun \"%s\", CTL lun %d",
2083 newlun->l_name, newlun->l_ctl_lun);
2084 error = kernel_lun_modify(newlun);
2085 if (error != 0) {
2086 log_warnx("failed to "
2087 "modify lun \"%s\", CTL lun %d",
2088 newlun->l_name, newlun->l_ctl_lun);
2089 cumulated_error++;
2090 }
2091 continue;
2092 }
2093 log_debugx("adding lun \"%s\"", newlun->l_name);
2094 error = kernel_lun_add(newlun);
2095 if (error != 0) {
2096 log_warnx("failed to add lun \"%s\"", newlun->l_name);
2097 lun_delete(newlun);
2098 cumulated_error++;
2099 }
2100 }
2101
2102 /*
2103 * Now add new ports or modify existing ones.
2104 */
2105 TAILQ_FOREACH_SAFE(newport, &newconf->conf_ports, p_next, tmpport) {
2106 if (port_is_dummy(newport))
2107 continue;
2108 oldport = port_find(oldconf, newport->p_name);
2109
2110 if (oldport == NULL || port_is_dummy(oldport)) {
2111 log_debugx("adding port \"%s\"", newport->p_name);
2112 error = kernel_port_add(newport);
2113 } else {
2114 log_debugx("updating port \"%s\"", newport->p_name);
2115 newport->p_ctl_port = oldport->p_ctl_port;
2116 error = kernel_port_update(newport, oldport);
2117 }
2118 if (error != 0) {
2119 log_warnx("failed to %s port %s",
2120 (oldport == NULL) ? "add" : "update",
2121 newport->p_name);
2122 if (oldport == NULL || port_is_dummy(oldport))
2123 port_delete(newport);
2124 /*
2125 * XXX: Uncomment after fixing the root cause.
2126 *
2127 * cumulated_error++;
2128 */
2129 }
2130 }
2131
2132 /*
2133 * Go through the new portals, opening the sockets as necessary.
2134 */
2135 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2136 if (newpg->pg_foreign)
2137 continue;
2138 if (newpg->pg_unassigned) {
2139 log_debugx("not listening on portal-group \"%s\", "
2140 "not assigned to any target",
2141 newpg->pg_name);
2142 continue;
2143 }
2144 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2145 /*
2146 * Try to find already open portal and reuse
2147 * the listening socket. We don't care about
2148 * what portal or portal group that was, what
2149 * matters is the listening address.
2150 */
2151 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2152 pg_next) {
2153 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2154 p_next) {
2155 if (strcmp(newp->p_listen,
2156 oldp->p_listen) == 0 &&
2157 oldp->p_socket > 0) {
2158 newp->p_socket =
2159 oldp->p_socket;
2160 oldp->p_socket = 0;
2161 break;
2162 }
2163 }
2164 }
2165 if (newp->p_socket > 0) {
2166 /*
2167 * We're done with this portal.
2168 */
2169 continue;
2170 }
2171
2172 #ifdef ICL_KERNEL_PROXY
2173 if (proxy_mode) {
2174 newpg->pg_conf->conf_portal_id++;
2175 newp->p_id = newpg->pg_conf->conf_portal_id;
2176 log_debugx("listening on %s, portal-group "
2177 "\"%s\", portal id %d, using ICL proxy",
2178 newp->p_listen, newpg->pg_name, newp->p_id);
2179 kernel_listen(newp->p_ai, newp->p_iser,
2180 newp->p_id);
2181 continue;
2182 }
2183 #endif
2184 assert(proxy_mode == false);
2185 assert(newp->p_iser == false);
2186
2187 log_debugx("listening on %s, portal-group \"%s\"",
2188 newp->p_listen, newpg->pg_name);
2189 newp->p_socket = socket(newp->p_ai->ai_family,
2190 newp->p_ai->ai_socktype,
2191 newp->p_ai->ai_protocol);
2192 if (newp->p_socket < 0) {
2193 log_warn("socket(2) failed for %s",
2194 newp->p_listen);
2195 cumulated_error++;
2196 continue;
2197 }
2198 sockbuf = SOCKBUF_SIZE;
2199 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2200 &sockbuf, sizeof(sockbuf)) == -1)
2201 log_warn("setsockopt(SO_RCVBUF) failed "
2202 "for %s", newp->p_listen);
2203 sockbuf = SOCKBUF_SIZE;
2204 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2205 &sockbuf, sizeof(sockbuf)) == -1)
2206 log_warn("setsockopt(SO_SNDBUF) failed "
2207 "for %s", newp->p_listen);
2208 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_NO_DDP,
2209 &one, sizeof(one)) == -1)
2210 log_warn("setsockopt(SO_NO_DDP) failed "
2211 "for %s", newp->p_listen);
2212 error = setsockopt(newp->p_socket, SOL_SOCKET,
2213 SO_REUSEADDR, &one, sizeof(one));
2214 if (error != 0) {
2215 log_warn("setsockopt(SO_REUSEADDR) failed "
2216 "for %s", newp->p_listen);
2217 close(newp->p_socket);
2218 newp->p_socket = 0;
2219 cumulated_error++;
2220 continue;
2221 }
2222 if (newpg->pg_dscp != -1) {
2223 struct sockaddr sa;
2224 int len = sizeof(sa);
2225 getsockname(newp->p_socket, &sa, &len);
2226 /*
2227 * Only allow the 6-bit DSCP
2228 * field to be modified
2229 */
2230 int tos = newpg->pg_dscp << 2;
2231 if (sa.sa_family == AF_INET) {
2232 if (setsockopt(newp->p_socket,
2233 IPPROTO_IP, IP_TOS,
2234 &tos, sizeof(tos)) == -1)
2235 log_warn("setsockopt(IP_TOS) "
2236 "failed for %s",
2237 newp->p_listen);
2238 } else
2239 if (sa.sa_family == AF_INET6) {
2240 if (setsockopt(newp->p_socket,
2241 IPPROTO_IPV6, IPV6_TCLASS,
2242 &tos, sizeof(tos)) == -1)
2243 log_warn("setsockopt(IPV6_TCLASS) "
2244 "failed for %s",
2245 newp->p_listen);
2246 }
2247 }
2248 if (newpg->pg_pcp != -1) {
2249 struct sockaddr sa;
2250 int len = sizeof(sa);
2251 getsockname(newp->p_socket, &sa, &len);
2252 /*
2253 * Only allow the 6-bit DSCP
2254 * field to be modified
2255 */
2256 int pcp = newpg->pg_pcp;
2257 if (sa.sa_family == AF_INET) {
2258 if (setsockopt(newp->p_socket,
2259 IPPROTO_IP, IP_VLAN_PCP,
2260 &pcp, sizeof(pcp)) == -1)
2261 log_warn("setsockopt(IP_VLAN_PCP) "
2262 "failed for %s",
2263 newp->p_listen);
2264 } else
2265 if (sa.sa_family == AF_INET6) {
2266 if (setsockopt(newp->p_socket,
2267 IPPROTO_IPV6, IPV6_VLAN_PCP,
2268 &pcp, sizeof(pcp)) == -1)
2269 log_warn("setsockopt(IPV6_VLAN_PCP) "
2270 "failed for %s",
2271 newp->p_listen);
2272 }
2273 }
2274 error = bind(newp->p_socket, newp->p_ai->ai_addr,
2275 newp->p_ai->ai_addrlen);
2276 if (error != 0) {
2277 log_warn("bind(2) failed for %s",
2278 newp->p_listen);
2279 close(newp->p_socket);
2280 newp->p_socket = 0;
2281 cumulated_error++;
2282 continue;
2283 }
2284 error = listen(newp->p_socket, -1);
2285 if (error != 0) {
2286 log_warn("listen(2) failed for %s",
2287 newp->p_listen);
2288 close(newp->p_socket);
2289 newp->p_socket = 0;
2290 cumulated_error++;
2291 continue;
2292 }
2293 }
2294 }
2295
2296 /*
2297 * Go through the no longer used sockets, closing them.
2298 */
2299 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2300 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2301 if (oldp->p_socket <= 0)
2302 continue;
2303 log_debugx("closing socket for %s, portal-group \"%s\"",
2304 oldp->p_listen, oldpg->pg_name);
2305 close(oldp->p_socket);
2306 oldp->p_socket = 0;
2307 }
2308 }
2309
2310 /* (Re-)Register on remaining/new iSNS servers. */
2311 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2312 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2313 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2314 break;
2315 }
2316 isns_register(newns, oldns);
2317 }
2318
2319 /* Schedule iSNS update */
2320 if (!TAILQ_EMPTY(&newconf->conf_isns))
2321 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2322
2323 return (cumulated_error);
2324 }
2325
2326 static bool
timed_out(void)2327 timed_out(void)
2328 {
2329
2330 return (sigalrm_received);
2331 }
2332
2333 static void
sigalrm_handler_fatal(int dummy __unused)2334 sigalrm_handler_fatal(int dummy __unused)
2335 {
2336 /*
2337 * It would be easiest to just log an error and exit. We can't
2338 * do this, though, because log_errx() is not signal safe, since
2339 * it calls syslog(3). Instead, set a flag checked by pdu_send()
2340 * and pdu_receive(), to call log_errx() there. Should they fail
2341 * to notice, we'll exit here one second later.
2342 */
2343 if (sigalrm_received) {
2344 /*
2345 * Oh well. Just give up and quit.
2346 */
2347 _exit(2);
2348 }
2349
2350 sigalrm_received = true;
2351 }
2352
2353 static void
sigalrm_handler(int dummy __unused)2354 sigalrm_handler(int dummy __unused)
2355 {
2356
2357 sigalrm_received = true;
2358 }
2359
2360 void
set_timeout(int timeout,int fatal)2361 set_timeout(int timeout, int fatal)
2362 {
2363 struct sigaction sa;
2364 struct itimerval itv;
2365 int error;
2366
2367 if (timeout <= 0) {
2368 log_debugx("session timeout disabled");
2369 bzero(&itv, sizeof(itv));
2370 error = setitimer(ITIMER_REAL, &itv, NULL);
2371 if (error != 0)
2372 log_err(1, "setitimer");
2373 sigalrm_received = false;
2374 return;
2375 }
2376
2377 sigalrm_received = false;
2378 bzero(&sa, sizeof(sa));
2379 if (fatal)
2380 sa.sa_handler = sigalrm_handler_fatal;
2381 else
2382 sa.sa_handler = sigalrm_handler;
2383 sigfillset(&sa.sa_mask);
2384 error = sigaction(SIGALRM, &sa, NULL);
2385 if (error != 0)
2386 log_err(1, "sigaction");
2387
2388 /*
2389 * First SIGALRM will arive after conf_timeout seconds.
2390 * If we do nothing, another one will arrive a second later.
2391 */
2392 log_debugx("setting session timeout to %d seconds", timeout);
2393 bzero(&itv, sizeof(itv));
2394 itv.it_interval.tv_sec = 1;
2395 itv.it_value.tv_sec = timeout;
2396 error = setitimer(ITIMER_REAL, &itv, NULL);
2397 if (error != 0)
2398 log_err(1, "setitimer");
2399 }
2400
2401 static int
wait_for_children(bool block)2402 wait_for_children(bool block)
2403 {
2404 pid_t pid;
2405 int status;
2406 int num = 0;
2407
2408 for (;;) {
2409 /*
2410 * If "block" is true, wait for at least one process.
2411 */
2412 if (block && num == 0)
2413 pid = wait4(-1, &status, 0, NULL);
2414 else
2415 pid = wait4(-1, &status, WNOHANG, NULL);
2416 if (pid <= 0)
2417 break;
2418 if (WIFSIGNALED(status)) {
2419 log_warnx("child process %d terminated with signal %d",
2420 pid, WTERMSIG(status));
2421 } else if (WEXITSTATUS(status) != 0) {
2422 log_warnx("child process %d terminated with exit status %d",
2423 pid, WEXITSTATUS(status));
2424 } else {
2425 log_debugx("child process %d terminated gracefully", pid);
2426 }
2427 num++;
2428 }
2429
2430 return (num);
2431 }
2432
2433 static void
handle_connection(struct portal * portal,int fd,const struct sockaddr * client_sa,bool dont_fork)2434 handle_connection(struct portal *portal, int fd,
2435 const struct sockaddr *client_sa, bool dont_fork)
2436 {
2437 struct ctld_connection *conn;
2438 int error;
2439 pid_t pid;
2440 char host[NI_MAXHOST + 1];
2441 struct conf *conf;
2442
2443 conf = portal->p_portal_group->pg_conf;
2444
2445 if (dont_fork) {
2446 log_debugx("incoming connection; not forking due to -d flag");
2447 } else {
2448 nchildren -= wait_for_children(false);
2449 assert(nchildren >= 0);
2450
2451 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2452 log_debugx("maxproc limit of %d child processes hit; "
2453 "waiting for child process to exit", conf->conf_maxproc);
2454 nchildren -= wait_for_children(true);
2455 assert(nchildren >= 0);
2456 }
2457 log_debugx("incoming connection; forking child process #%d",
2458 nchildren);
2459 nchildren++;
2460 pid = fork();
2461 if (pid < 0)
2462 log_err(1, "fork");
2463 if (pid > 0) {
2464 close(fd);
2465 return;
2466 }
2467 pidfile_close(conf->conf_pidfh);
2468 }
2469
2470 error = getnameinfo(client_sa, client_sa->sa_len,
2471 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2472 if (error != 0)
2473 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2474
2475 log_debugx("accepted connection from %s; portal group \"%s\"",
2476 host, portal->p_portal_group->pg_name);
2477 log_set_peer_addr(host);
2478 setproctitle("%s", host);
2479
2480 conn = connection_new(portal, fd, host, client_sa);
2481 set_timeout(conf->conf_timeout, true);
2482 kernel_capsicate();
2483 login(conn);
2484 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2485 kernel_handoff(conn);
2486 log_debugx("connection handed off to the kernel");
2487 } else {
2488 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2489 discovery(conn);
2490 }
2491 log_debugx("nothing more to do; exiting");
2492 exit(0);
2493 }
2494
2495 static int
fd_add(int fd,fd_set * fdset,int nfds)2496 fd_add(int fd, fd_set *fdset, int nfds)
2497 {
2498
2499 /*
2500 * Skip sockets which we failed to bind.
2501 */
2502 if (fd <= 0)
2503 return (nfds);
2504
2505 FD_SET(fd, fdset);
2506 if (fd > nfds)
2507 nfds = fd;
2508 return (nfds);
2509 }
2510
2511 static void
main_loop(struct conf * conf,bool dont_fork)2512 main_loop(struct conf *conf, bool dont_fork)
2513 {
2514 struct portal_group *pg;
2515 struct portal *portal;
2516 struct sockaddr_storage client_sa;
2517 socklen_t client_salen;
2518 #ifdef ICL_KERNEL_PROXY
2519 int connection_id;
2520 int portal_id;
2521 #endif
2522 fd_set fdset;
2523 int error, nfds, client_fd;
2524
2525 pidfile_write(conf->conf_pidfh);
2526
2527 for (;;) {
2528 if (sighup_received || sigterm_received || timed_out())
2529 return;
2530
2531 #ifdef ICL_KERNEL_PROXY
2532 if (proxy_mode) {
2533 client_salen = sizeof(client_sa);
2534 kernel_accept(&connection_id, &portal_id,
2535 (struct sockaddr *)&client_sa, &client_salen);
2536 assert(client_salen >= client_sa.ss_len);
2537
2538 log_debugx("incoming connection, id %d, portal id %d",
2539 connection_id, portal_id);
2540 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2541 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2542 if (portal->p_id == portal_id) {
2543 goto found;
2544 }
2545 }
2546 }
2547
2548 log_errx(1, "kernel returned invalid portal_id %d",
2549 portal_id);
2550
2551 found:
2552 handle_connection(portal, connection_id,
2553 (struct sockaddr *)&client_sa, dont_fork);
2554 } else {
2555 #endif
2556 assert(proxy_mode == false);
2557
2558 FD_ZERO(&fdset);
2559 nfds = 0;
2560 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2561 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2562 nfds = fd_add(portal->p_socket, &fdset, nfds);
2563 }
2564 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2565 if (error <= 0) {
2566 if (errno == EINTR)
2567 return;
2568 log_err(1, "select");
2569 }
2570 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2571 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2572 if (!FD_ISSET(portal->p_socket, &fdset))
2573 continue;
2574 client_salen = sizeof(client_sa);
2575 client_fd = accept(portal->p_socket,
2576 (struct sockaddr *)&client_sa,
2577 &client_salen);
2578 if (client_fd < 0) {
2579 if (errno == ECONNABORTED)
2580 continue;
2581 log_err(1, "accept");
2582 }
2583 assert(client_salen >= client_sa.ss_len);
2584
2585 handle_connection(portal, client_fd,
2586 (struct sockaddr *)&client_sa,
2587 dont_fork);
2588 break;
2589 }
2590 }
2591 #ifdef ICL_KERNEL_PROXY
2592 }
2593 #endif
2594 }
2595 }
2596
2597 static void
sighup_handler(int dummy __unused)2598 sighup_handler(int dummy __unused)
2599 {
2600
2601 sighup_received = true;
2602 }
2603
2604 static void
sigterm_handler(int dummy __unused)2605 sigterm_handler(int dummy __unused)
2606 {
2607
2608 sigterm_received = true;
2609 }
2610
2611 static void
sigchld_handler(int dummy __unused)2612 sigchld_handler(int dummy __unused)
2613 {
2614
2615 /*
2616 * The only purpose of this handler is to make SIGCHLD
2617 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2618 * wait_for_children().
2619 */
2620 }
2621
2622 static void
register_signals(void)2623 register_signals(void)
2624 {
2625 struct sigaction sa;
2626 int error;
2627
2628 bzero(&sa, sizeof(sa));
2629 sa.sa_handler = sighup_handler;
2630 sigfillset(&sa.sa_mask);
2631 error = sigaction(SIGHUP, &sa, NULL);
2632 if (error != 0)
2633 log_err(1, "sigaction");
2634
2635 sa.sa_handler = sigterm_handler;
2636 error = sigaction(SIGTERM, &sa, NULL);
2637 if (error != 0)
2638 log_err(1, "sigaction");
2639
2640 sa.sa_handler = sigterm_handler;
2641 error = sigaction(SIGINT, &sa, NULL);
2642 if (error != 0)
2643 log_err(1, "sigaction");
2644
2645 sa.sa_handler = sigchld_handler;
2646 error = sigaction(SIGCHLD, &sa, NULL);
2647 if (error != 0)
2648 log_err(1, "sigaction");
2649 }
2650
2651 static void
check_perms(const char * path)2652 check_perms(const char *path)
2653 {
2654 struct stat sb;
2655 int error;
2656
2657 error = stat(path, &sb);
2658 if (error != 0) {
2659 log_warn("stat");
2660 return;
2661 }
2662 if (sb.st_mode & S_IWOTH) {
2663 log_warnx("%s is world-writable", path);
2664 } else if (sb.st_mode & S_IROTH) {
2665 log_warnx("%s is world-readable", path);
2666 } else if (sb.st_mode & S_IXOTH) {
2667 /*
2668 * Ok, this one doesn't matter, but still do it,
2669 * just for consistency.
2670 */
2671 log_warnx("%s is world-executable", path);
2672 }
2673
2674 /*
2675 * XXX: Should we also check for owner != 0?
2676 */
2677 }
2678
2679 static struct conf *
conf_new_from_file(const char * path,bool ucl)2680 conf_new_from_file(const char *path, bool ucl)
2681 {
2682 struct conf *conf;
2683 struct auth_group *ag;
2684 struct portal_group *pg;
2685 int error;
2686
2687 log_debugx("obtaining configuration from %s", path);
2688
2689 conf = conf_new();
2690
2691 ag = auth_group_new(conf, "default");
2692 assert(ag != NULL);
2693
2694 ag = auth_group_new(conf, "no-authentication");
2695 assert(ag != NULL);
2696 ag->ag_type = AG_TYPE_NO_AUTHENTICATION;
2697
2698 ag = auth_group_new(conf, "no-access");
2699 assert(ag != NULL);
2700 ag->ag_type = AG_TYPE_DENY;
2701
2702 pg = portal_group_new(conf, "default");
2703 assert(pg != NULL);
2704
2705 if (ucl)
2706 error = uclparse_conf(conf, path);
2707 else
2708 error = parse_conf(conf, path);
2709
2710 if (error != 0) {
2711 conf_delete(conf);
2712 return (NULL);
2713 }
2714
2715 check_perms(path);
2716
2717 if (conf->conf_default_ag_defined == false) {
2718 log_debugx("auth-group \"default\" not defined; "
2719 "going with defaults");
2720 ag = auth_group_find(conf, "default");
2721 assert(ag != NULL);
2722 ag->ag_type = AG_TYPE_DENY;
2723 }
2724
2725 if (conf->conf_default_pg_defined == false) {
2726 log_debugx("portal-group \"default\" not defined; "
2727 "going with defaults");
2728 pg = portal_group_find(conf, "default");
2729 assert(pg != NULL);
2730 portal_group_add_listen(pg, "0.0.0.0:3260", false);
2731 portal_group_add_listen(pg, "[::]:3260", false);
2732 }
2733
2734 conf->conf_kernel_port_on = true;
2735
2736 error = conf_verify(conf);
2737 if (error != 0) {
2738 conf_delete(conf);
2739 return (NULL);
2740 }
2741
2742 return (conf);
2743 }
2744
2745 /*
2746 * If the config file specifies physical ports for any target, associate them
2747 * with the config file. If necessary, create them.
2748 */
2749 static int
new_pports_from_conf(struct conf * conf,struct kports * kports)2750 new_pports_from_conf(struct conf *conf, struct kports *kports)
2751 {
2752 struct target *targ;
2753 struct pport *pp;
2754 struct port *tp;
2755 int ret, i_pp, i_vp;
2756
2757 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
2758 if (!targ->t_pport)
2759 continue;
2760
2761 ret = sscanf(targ->t_pport, "ioctl/%d/%d", &i_pp, &i_vp);
2762 if (ret > 0) {
2763 tp = port_new_ioctl(conf, kports, targ, i_pp, i_vp);
2764 if (tp == NULL) {
2765 log_warnx("can't create new ioctl port "
2766 "for target \"%s\"", targ->t_name);
2767 return (1);
2768 }
2769
2770 continue;
2771 }
2772
2773 pp = pport_find(kports, targ->t_pport);
2774 if (pp == NULL) {
2775 log_warnx("unknown port \"%s\" for target \"%s\"",
2776 targ->t_pport, targ->t_name);
2777 return (1);
2778 }
2779 if (!TAILQ_EMPTY(&pp->pp_ports)) {
2780 log_warnx("can't link port \"%s\" to target \"%s\", "
2781 "port already linked to some target",
2782 targ->t_pport, targ->t_name);
2783 return (1);
2784 }
2785 tp = port_new_pp(conf, targ, pp);
2786 if (tp == NULL) {
2787 log_warnx("can't link port \"%s\" to target \"%s\"",
2788 targ->t_pport, targ->t_name);
2789 return (1);
2790 }
2791 }
2792 return (0);
2793 }
2794
2795 int
main(int argc,char ** argv)2796 main(int argc, char **argv)
2797 {
2798 struct kports kports;
2799 struct conf *oldconf, *newconf, *tmpconf;
2800 struct isns *newns;
2801 const char *config_path = DEFAULT_CONFIG_PATH;
2802 int debug = 0, ch, error;
2803 pid_t otherpid;
2804 bool dont_daemonize = false;
2805 bool test_config = false;
2806 bool use_ucl = false;
2807
2808 while ((ch = getopt(argc, argv, "dtuf:R")) != -1) {
2809 switch (ch) {
2810 case 'd':
2811 dont_daemonize = true;
2812 debug++;
2813 break;
2814 case 't':
2815 test_config = true;
2816 break;
2817 case 'u':
2818 use_ucl = true;
2819 break;
2820 case 'f':
2821 config_path = optarg;
2822 break;
2823 case 'R':
2824 #ifndef ICL_KERNEL_PROXY
2825 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2826 "does not support iSER protocol");
2827 #endif
2828 proxy_mode = true;
2829 break;
2830 case '?':
2831 default:
2832 usage();
2833 }
2834 }
2835 argc -= optind;
2836 if (argc != 0)
2837 usage();
2838
2839 log_init(debug);
2840 kernel_init();
2841
2842 TAILQ_INIT(&kports.pports);
2843 newconf = conf_new_from_file(config_path, use_ucl);
2844
2845 if (newconf == NULL)
2846 log_errx(1, "configuration error; exiting");
2847
2848 if (test_config)
2849 return (0);
2850
2851 assert(newconf->conf_pidfile_path != NULL);
2852 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
2853 newconf->conf_pidfh = pidfile_open(newconf->conf_pidfile_path, 0600,
2854 &otherpid);
2855 if (newconf->conf_pidfh == NULL) {
2856 if (errno == EEXIST)
2857 log_errx(1, "daemon already running, pid: %jd.",
2858 (intmax_t)otherpid);
2859 log_err(1, "cannot open or create pidfile \"%s\"",
2860 newconf->conf_pidfile_path);
2861 }
2862
2863 register_signals();
2864
2865 oldconf = conf_new_from_kernel(&kports);
2866
2867 if (debug > 0) {
2868 oldconf->conf_debug = debug;
2869 newconf->conf_debug = debug;
2870 }
2871
2872 if (new_pports_from_conf(newconf, &kports))
2873 log_errx(1, "Error associating physical ports; exiting");
2874
2875 error = conf_apply(oldconf, newconf);
2876 if (error != 0)
2877 log_errx(1, "failed to apply configuration; exiting");
2878
2879 conf_delete(oldconf);
2880 oldconf = NULL;
2881
2882 if (dont_daemonize == false) {
2883 log_debugx("daemonizing");
2884 if (daemon(0, 0) == -1) {
2885 log_warn("cannot daemonize");
2886 pidfile_remove(newconf->conf_pidfh);
2887 exit(1);
2888 }
2889 }
2890
2891 /* Schedule iSNS update */
2892 if (!TAILQ_EMPTY(&newconf->conf_isns))
2893 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2894
2895 for (;;) {
2896 main_loop(newconf, dont_daemonize);
2897 if (sighup_received) {
2898 sighup_received = false;
2899 log_debugx("received SIGHUP, reloading configuration");
2900 tmpconf = conf_new_from_file(config_path, use_ucl);
2901
2902 if (tmpconf == NULL) {
2903 log_warnx("configuration error, "
2904 "continuing with old configuration");
2905 } else if (new_pports_from_conf(tmpconf, &kports)) {
2906 log_warnx("Error associating physical ports, "
2907 "continuing with old configuration");
2908 conf_delete(tmpconf);
2909 } else {
2910 if (debug > 0)
2911 tmpconf->conf_debug = debug;
2912 oldconf = newconf;
2913 newconf = tmpconf;
2914
2915 error = conf_apply(oldconf, newconf);
2916 if (error != 0)
2917 log_warnx("failed to reload "
2918 "configuration");
2919 conf_delete(oldconf);
2920 oldconf = NULL;
2921 }
2922 } else if (sigterm_received) {
2923 log_debugx("exiting on signal; "
2924 "reloading empty configuration");
2925
2926 log_debugx("removing CTL iSCSI ports "
2927 "and terminating all connections");
2928
2929 oldconf = newconf;
2930 newconf = conf_new();
2931 if (debug > 0)
2932 newconf->conf_debug = debug;
2933 error = conf_apply(oldconf, newconf);
2934 if (error != 0)
2935 log_warnx("failed to apply configuration");
2936 if (oldconf->conf_pidfh) {
2937 pidfile_remove(oldconf->conf_pidfh);
2938 oldconf->conf_pidfh = NULL;
2939 }
2940 conf_delete(newconf);
2941 conf_delete(oldconf);
2942 oldconf = NULL;
2943
2944 log_warnx("exiting on signal");
2945 exit(0);
2946 } else {
2947 nchildren -= wait_for_children(false);
2948 assert(nchildren >= 0);
2949 if (timed_out()) {
2950 set_timeout(0, false);
2951 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2952 isns_check(newns);
2953 /* Schedule iSNS update */
2954 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2955 set_timeout((newconf->conf_isns_period
2956 + 2) / 3,
2957 false);
2958 }
2959 }
2960 }
2961 }
2962 /* NOTREACHED */
2963 }
2964