2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
56 bool proxy_mode = false;
58 static volatile bool sighup_received = false;
59 static volatile bool sigterm_received = false;
60 static volatile bool sigalrm_received = false;
62 static int nchildren = 0;
63 static uint16_t last_portal_group_tag = 0xff;
69 fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n");
74 checked_strdup(const char *s)
89 conf = calloc(1, sizeof(*conf));
92 TAILQ_INIT(&conf->conf_luns);
93 TAILQ_INIT(&conf->conf_targets);
94 TAILQ_INIT(&conf->conf_auth_groups);
95 TAILQ_INIT(&conf->conf_ports);
96 TAILQ_INIT(&conf->conf_portal_groups);
97 TAILQ_INIT(&conf->conf_pports);
98 TAILQ_INIT(&conf->conf_isns);
100 conf->conf_isns_period = 900;
101 conf->conf_isns_timeout = 5;
102 conf->conf_debug = 0;
103 conf->conf_timeout = 60;
104 conf->conf_maxproc = 30;
110 conf_delete(struct conf *conf)
112 struct lun *lun, *ltmp;
113 struct target *targ, *tmp;
114 struct auth_group *ag, *cagtmp;
115 struct portal_group *pg, *cpgtmp;
116 struct pport *pp, *pptmp;
117 struct isns *is, *istmp;
119 assert(conf->conf_pidfh == NULL);
121 TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
123 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
125 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
126 auth_group_delete(ag);
127 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
128 portal_group_delete(pg);
129 TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
131 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
133 assert(TAILQ_EMPTY(&conf->conf_ports));
134 free(conf->conf_pidfile_path);
139 auth_new(struct auth_group *ag)
143 auth = calloc(1, sizeof(*auth));
145 log_err(1, "calloc");
146 auth->a_auth_group = ag;
147 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
152 auth_delete(struct auth *auth)
154 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
157 free(auth->a_secret);
158 free(auth->a_mutual_user);
159 free(auth->a_mutual_secret);
164 auth_find(const struct auth_group *ag, const char *user)
166 const struct auth *auth;
168 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
169 if (strcmp(auth->a_user, user) == 0)
177 auth_check_secret_length(struct auth *auth)
181 len = strlen(auth->a_secret);
183 if (auth->a_auth_group->ag_name != NULL)
184 log_warnx("secret for user \"%s\", auth-group \"%s\", "
185 "is too long; it should be at most 16 characters "
186 "long", auth->a_user, auth->a_auth_group->ag_name);
188 log_warnx("secret for user \"%s\", target \"%s\", "
189 "is too long; it should be at most 16 characters "
190 "long", auth->a_user,
191 auth->a_auth_group->ag_target->t_name);
194 if (auth->a_auth_group->ag_name != NULL)
195 log_warnx("secret for user \"%s\", auth-group \"%s\", "
196 "is too short; it should be at least 12 characters "
197 "long", auth->a_user,
198 auth->a_auth_group->ag_name);
200 log_warnx("secret for user \"%s\", target \"%s\", "
201 "is too short; it should be at least 12 characters "
202 "long", auth->a_user,
203 auth->a_auth_group->ag_target->t_name);
206 if (auth->a_mutual_secret != NULL) {
207 len = strlen(auth->a_mutual_secret);
209 if (auth->a_auth_group->ag_name != NULL)
210 log_warnx("mutual secret for user \"%s\", "
211 "auth-group \"%s\", is too long; it should "
212 "be at most 16 characters long",
213 auth->a_user, auth->a_auth_group->ag_name);
215 log_warnx("mutual secret for user \"%s\", "
216 "target \"%s\", is too long; it should "
217 "be at most 16 characters long",
219 auth->a_auth_group->ag_target->t_name);
222 if (auth->a_auth_group->ag_name != NULL)
223 log_warnx("mutual secret for user \"%s\", "
224 "auth-group \"%s\", is too short; it "
225 "should be at least 12 characters long",
226 auth->a_user, auth->a_auth_group->ag_name);
228 log_warnx("mutual secret for user \"%s\", "
229 "target \"%s\", is too short; it should be "
230 "at least 12 characters long",
232 auth->a_auth_group->ag_target->t_name);
238 auth_new_chap(struct auth_group *ag, const char *user,
243 if (ag->ag_type == AG_TYPE_UNKNOWN)
244 ag->ag_type = AG_TYPE_CHAP;
245 if (ag->ag_type != AG_TYPE_CHAP) {
246 if (ag->ag_name != NULL)
247 log_warnx("cannot mix \"chap\" authentication with "
248 "other types for auth-group \"%s\"", ag->ag_name);
250 log_warnx("cannot mix \"chap\" authentication with "
251 "other types for target \"%s\"",
252 ag->ag_target->t_name);
257 auth->a_user = checked_strdup(user);
258 auth->a_secret = checked_strdup(secret);
260 auth_check_secret_length(auth);
266 auth_new_chap_mutual(struct auth_group *ag, const char *user,
267 const char *secret, const char *user2, const char *secret2)
271 if (ag->ag_type == AG_TYPE_UNKNOWN)
272 ag->ag_type = AG_TYPE_CHAP_MUTUAL;
273 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
274 if (ag->ag_name != NULL)
275 log_warnx("cannot mix \"chap-mutual\" authentication "
276 "with other types for auth-group \"%s\"",
279 log_warnx("cannot mix \"chap-mutual\" authentication "
280 "with other types for target \"%s\"",
281 ag->ag_target->t_name);
286 auth->a_user = checked_strdup(user);
287 auth->a_secret = checked_strdup(secret);
288 auth->a_mutual_user = checked_strdup(user2);
289 auth->a_mutual_secret = checked_strdup(secret2);
291 auth_check_secret_length(auth);
296 const struct auth_name *
297 auth_name_new(struct auth_group *ag, const char *name)
299 struct auth_name *an;
301 an = calloc(1, sizeof(*an));
303 log_err(1, "calloc");
304 an->an_auth_group = ag;
305 an->an_initator_name = checked_strdup(name);
306 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
311 auth_name_delete(struct auth_name *an)
313 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
315 free(an->an_initator_name);
320 auth_name_defined(const struct auth_group *ag)
322 if (TAILQ_EMPTY(&ag->ag_names))
327 const struct auth_name *
328 auth_name_find(const struct auth_group *ag, const char *name)
330 const struct auth_name *auth_name;
332 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
333 if (strcmp(auth_name->an_initator_name, name) == 0)
341 auth_name_check(const struct auth_group *ag, const char *initiator_name)
343 if (!auth_name_defined(ag))
346 if (auth_name_find(ag, initiator_name) == NULL)
352 const struct auth_portal *
353 auth_portal_new(struct auth_group *ag, const char *portal)
355 struct auth_portal *ap;
356 char *net, *mask, *str, *tmp;
359 ap = calloc(1, sizeof(*ap));
361 log_err(1, "calloc");
362 ap->ap_auth_group = ag;
363 ap->ap_initator_portal = checked_strdup(portal);
364 mask = str = checked_strdup(portal);
365 net = strsep(&mask, "/");
371 if (net[len - 1] == ']')
373 if (strchr(net, ':') != NULL) {
374 struct sockaddr_in6 *sin6 =
375 (struct sockaddr_in6 *)&ap->ap_sa;
377 sin6->sin6_len = sizeof(*sin6);
378 sin6->sin6_family = AF_INET6;
379 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
383 struct sockaddr_in *sin =
384 (struct sockaddr_in *)&ap->ap_sa;
386 sin->sin_len = sizeof(*sin);
387 sin->sin_family = AF_INET;
388 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
393 m = strtol(mask, &tmp, 0);
394 if (m < 0 || m > dm || tmp[0] != 0)
400 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
406 log_warnx("incorrect initiator portal \"%s\"", portal);
411 auth_portal_delete(struct auth_portal *ap)
413 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
415 free(ap->ap_initator_portal);
420 auth_portal_defined(const struct auth_group *ag)
422 if (TAILQ_EMPTY(&ag->ag_portals))
427 const struct auth_portal *
428 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
430 const struct auth_portal *ap;
431 const uint8_t *a, *b;
435 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
436 if (ap->ap_sa.ss_family != ss->ss_family)
438 if (ss->ss_family == AF_INET) {
439 a = (const uint8_t *)
440 &((const struct sockaddr_in *)ss)->sin_addr;
441 b = (const uint8_t *)
442 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
444 a = (const uint8_t *)
445 &((const struct sockaddr_in6 *)ss)->sin6_addr;
446 b = (const uint8_t *)
447 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
449 for (i = 0; i < ap->ap_mask / 8; i++) {
453 if (ap->ap_mask % 8) {
454 bmask = 0xff << (8 - (ap->ap_mask % 8));
455 if ((a[i] & bmask) != (b[i] & bmask))
467 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
470 if (!auth_portal_defined(ag))
473 if (auth_portal_find(ag, sa) == NULL)
480 auth_group_new(struct conf *conf, const char *name)
482 struct auth_group *ag;
485 ag = auth_group_find(conf, name);
487 log_warnx("duplicated auth-group \"%s\"", name);
492 ag = calloc(1, sizeof(*ag));
494 log_err(1, "calloc");
496 ag->ag_name = checked_strdup(name);
497 TAILQ_INIT(&ag->ag_auths);
498 TAILQ_INIT(&ag->ag_names);
499 TAILQ_INIT(&ag->ag_portals);
501 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
507 auth_group_delete(struct auth_group *ag)
509 struct auth *auth, *auth_tmp;
510 struct auth_name *auth_name, *auth_name_tmp;
511 struct auth_portal *auth_portal, *auth_portal_tmp;
513 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
515 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
517 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
518 auth_name_delete(auth_name);
519 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
521 auth_portal_delete(auth_portal);
527 auth_group_find(const struct conf *conf, const char *name)
529 struct auth_group *ag;
531 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
532 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
540 auth_group_set_type(struct auth_group *ag, const char *str)
544 if (strcmp(str, "none") == 0) {
545 type = AG_TYPE_NO_AUTHENTICATION;
546 } else if (strcmp(str, "deny") == 0) {
548 } else if (strcmp(str, "chap") == 0) {
550 } else if (strcmp(str, "chap-mutual") == 0) {
551 type = AG_TYPE_CHAP_MUTUAL;
553 if (ag->ag_name != NULL)
554 log_warnx("invalid auth-type \"%s\" for auth-group "
555 "\"%s\"", str, ag->ag_name);
557 log_warnx("invalid auth-type \"%s\" for target "
558 "\"%s\"", str, ag->ag_target->t_name);
562 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
563 if (ag->ag_name != NULL) {
564 log_warnx("cannot set auth-type to \"%s\" for "
565 "auth-group \"%s\"; already has a different "
566 "type", str, ag->ag_name);
568 log_warnx("cannot set auth-type to \"%s\" for target "
569 "\"%s\"; already has a different type",
570 str, ag->ag_target->t_name);
580 static struct portal *
581 portal_new(struct portal_group *pg)
583 struct portal *portal;
585 portal = calloc(1, sizeof(*portal));
587 log_err(1, "calloc");
588 TAILQ_INIT(&portal->p_targets);
589 portal->p_portal_group = pg;
590 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
595 portal_delete(struct portal *portal)
598 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
599 if (portal->p_ai != NULL)
600 freeaddrinfo(portal->p_ai);
601 free(portal->p_listen);
605 struct portal_group *
606 portal_group_new(struct conf *conf, const char *name)
608 struct portal_group *pg;
610 pg = portal_group_find(conf, name);
612 log_warnx("duplicated portal-group \"%s\"", name);
616 pg = calloc(1, sizeof(*pg));
618 log_err(1, "calloc");
619 pg->pg_name = checked_strdup(name);
620 TAILQ_INIT(&pg->pg_options);
621 TAILQ_INIT(&pg->pg_portals);
622 TAILQ_INIT(&pg->pg_ports);
624 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
625 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
631 portal_group_delete(struct portal_group *pg)
633 struct portal *portal, *tmp;
634 struct port *port, *tport;
635 struct option *o, *otmp;
637 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
639 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
641 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
642 portal_delete(portal);
643 TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
644 option_delete(&pg->pg_options, o);
646 free(pg->pg_offload);
647 free(pg->pg_redirection);
651 struct portal_group *
652 portal_group_find(const struct conf *conf, const char *name)
654 struct portal_group *pg;
656 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
657 if (strcmp(pg->pg_name, name) == 0)
665 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
667 struct addrinfo hints;
668 char *str, *addr, *ch;
670 int error, colons = 0;
672 str = arg = strdup(arg);
675 * IPv6 address in square brackets, perhaps with port.
678 addr = strsep(&arg, "]");
683 if (arg[0] == '\0') {
685 } else if (arg[0] == ':') {
693 * Either IPv6 address without brackets - and without
694 * a port - or IPv4 address. Just count the colons.
696 for (ch = arg; *ch != '\0'; ch++) {
704 addr = strsep(&arg, ":");
712 memset(&hints, 0, sizeof(hints));
713 hints.ai_family = PF_UNSPEC;
714 hints.ai_socktype = SOCK_STREAM;
715 hints.ai_flags = AI_PASSIVE;
716 error = getaddrinfo(addr, port, &hints, ai);
718 return ((error != 0) ? 1 : 0);
722 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
724 struct portal *portal;
726 portal = portal_new(pg);
727 portal->p_listen = checked_strdup(value);
728 portal->p_iser = iser;
730 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
731 log_warnx("invalid listen address %s", portal->p_listen);
732 portal_delete(portal);
737 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
738 * those into multiple portals.
745 isns_new(struct conf *conf, const char *addr)
749 isns = calloc(1, sizeof(*isns));
751 log_err(1, "calloc");
753 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
754 isns->i_addr = checked_strdup(addr);
756 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
757 log_warnx("invalid iSNS address %s", isns->i_addr);
763 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
764 * those into multiple servers.
771 isns_delete(struct isns *isns)
774 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
776 if (isns->i_ai != NULL)
777 freeaddrinfo(isns->i_ai);
782 isns_do_connect(struct isns *isns)
786 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
787 isns->i_ai->ai_protocol);
789 log_warn("socket(2) failed for %s", isns->i_addr);
792 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
793 log_warn("connect(2) failed for %s", isns->i_addr);
801 isns_do_register(struct isns *isns, int s, const char *hostname)
803 struct conf *conf = isns->i_conf;
804 struct target *target;
805 struct portal *portal;
806 struct portal_group *pg;
808 struct isns_req *req;
812 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
813 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
814 isns_req_add_delim(req);
815 isns_req_add_str(req, 1, hostname);
816 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
817 isns_req_add_32(req, 6, conf->conf_isns_period);
818 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
819 if (pg->pg_unassigned)
821 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
822 isns_req_add_addr(req, 16, portal->p_ai);
823 isns_req_add_port(req, 17, portal->p_ai);
826 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
827 isns_req_add_str(req, 32, target->t_name);
828 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
829 if (target->t_alias != NULL)
830 isns_req_add_str(req, 34, target->t_alias);
831 TAILQ_FOREACH(port, &target->t_ports, p_ts) {
832 if ((pg = port->p_portal_group) == NULL)
834 isns_req_add_32(req, 51, pg->pg_tag);
835 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
836 isns_req_add_addr(req, 49, portal->p_ai);
837 isns_req_add_port(req, 50, portal->p_ai);
841 res = isns_req_send(s, req);
843 log_warn("send(2) failed for %s", isns->i_addr);
846 res = isns_req_receive(s, req);
848 log_warn("receive(2) failed for %s", isns->i_addr);
851 error = isns_req_get_status(req);
853 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
862 isns_do_check(struct isns *isns, int s, const char *hostname)
864 struct conf *conf = isns->i_conf;
865 struct isns_req *req;
869 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
870 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
871 isns_req_add_str(req, 1, hostname);
872 isns_req_add_delim(req);
873 isns_req_add(req, 2, 0, NULL);
874 res = isns_req_send(s, req);
876 log_warn("send(2) failed for %s", isns->i_addr);
879 res = isns_req_receive(s, req);
881 log_warn("receive(2) failed for %s", isns->i_addr);
884 error = isns_req_get_status(req);
886 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
895 isns_do_deregister(struct isns *isns, int s, const char *hostname)
897 struct conf *conf = isns->i_conf;
898 struct isns_req *req;
902 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
903 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
904 isns_req_add_delim(req);
905 isns_req_add_str(req, 1, hostname);
906 res = isns_req_send(s, req);
908 log_warn("send(2) failed for %s", isns->i_addr);
911 res = isns_req_receive(s, req);
913 log_warn("receive(2) failed for %s", isns->i_addr);
916 error = isns_req_get_status(req);
918 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
927 isns_register(struct isns *isns, struct isns *oldisns)
929 struct conf *conf = isns->i_conf;
933 if (TAILQ_EMPTY(&conf->conf_targets) ||
934 TAILQ_EMPTY(&conf->conf_portal_groups))
936 set_timeout(conf->conf_isns_timeout, false);
937 s = isns_do_connect(isns);
939 set_timeout(0, false);
942 gethostname(hostname, sizeof(hostname));
944 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
946 isns_do_deregister(oldisns, s, hostname);
947 isns_do_register(isns, s, hostname);
949 set_timeout(0, false);
953 isns_check(struct isns *isns)
955 struct conf *conf = isns->i_conf;
959 if (TAILQ_EMPTY(&conf->conf_targets) ||
960 TAILQ_EMPTY(&conf->conf_portal_groups))
962 set_timeout(conf->conf_isns_timeout, false);
963 s = isns_do_connect(isns);
965 set_timeout(0, false);
968 gethostname(hostname, sizeof(hostname));
970 res = isns_do_check(isns, s, hostname);
972 isns_do_deregister(isns, s, hostname);
973 isns_do_register(isns, s, hostname);
976 set_timeout(0, false);
980 isns_deregister(struct isns *isns)
982 struct conf *conf = isns->i_conf;
986 if (TAILQ_EMPTY(&conf->conf_targets) ||
987 TAILQ_EMPTY(&conf->conf_portal_groups))
989 set_timeout(conf->conf_isns_timeout, false);
990 s = isns_do_connect(isns);
993 gethostname(hostname, sizeof(hostname));
995 isns_do_deregister(isns, s, hostname);
997 set_timeout(0, false);
1001 portal_group_set_filter(struct portal_group *pg, const char *str)
1005 if (strcmp(str, "none") == 0) {
1006 filter = PG_FILTER_NONE;
1007 } else if (strcmp(str, "portal") == 0) {
1008 filter = PG_FILTER_PORTAL;
1009 } else if (strcmp(str, "portal-name") == 0) {
1010 filter = PG_FILTER_PORTAL_NAME;
1011 } else if (strcmp(str, "portal-name-auth") == 0) {
1012 filter = PG_FILTER_PORTAL_NAME_AUTH;
1014 log_warnx("invalid discovery-filter \"%s\" for portal-group "
1015 "\"%s\"; valid values are \"none\", \"portal\", "
1016 "\"portal-name\", and \"portal-name-auth\"",
1021 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1022 pg->pg_discovery_filter != filter) {
1023 log_warnx("cannot set discovery-filter to \"%s\" for "
1024 "portal-group \"%s\"; already has a different "
1025 "value", str, pg->pg_name);
1029 pg->pg_discovery_filter = filter;
1035 portal_group_set_offload(struct portal_group *pg, const char *offload)
1038 if (pg->pg_offload != NULL) {
1039 log_warnx("cannot set offload to \"%s\" for "
1040 "portal-group \"%s\"; already defined",
1041 offload, pg->pg_name);
1045 pg->pg_offload = checked_strdup(offload);
1051 portal_group_set_redirection(struct portal_group *pg, const char *addr)
1054 if (pg->pg_redirection != NULL) {
1055 log_warnx("cannot set redirection to \"%s\" for "
1056 "portal-group \"%s\"; already defined",
1061 pg->pg_redirection = checked_strdup(addr);
1067 valid_hex(const char ch)
1099 valid_iscsi_name(const char *name)
1103 if (strlen(name) >= MAX_NAME_LEN) {
1104 log_warnx("overlong name for target \"%s\"; max length allowed "
1105 "by iSCSI specification is %d characters",
1106 name, MAX_NAME_LEN);
1111 * In the cases below, we don't return an error, just in case the admin
1112 * was right, and we're wrong.
1114 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1115 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1117 * XXX: We should verify UTF-8 normalisation, as defined
1118 * by 3.2.6.2: iSCSI Name Encoding.
1120 if (isalnum(name[i]))
1122 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1124 log_warnx("invalid character \"%c\" in target name "
1125 "\"%s\"; allowed characters are letters, digits, "
1126 "'-', '.', and ':'", name[i], name);
1130 * XXX: Check more stuff: valid date and a valid reversed domain.
1132 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1133 if (strlen(name) != strlen("eui.") + 16)
1134 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1135 "should be followed by exactly 16 hexadecimal "
1137 for (i = strlen("eui."); name[i] != '\0'; i++) {
1138 if (!valid_hex(name[i])) {
1139 log_warnx("invalid character \"%c\" in target "
1140 "name \"%s\"; allowed characters are 1-9 "
1141 "and A-F", name[i], name);
1145 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1146 if (strlen(name) > strlen("naa.") + 32)
1147 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1148 "should be followed by at most 32 hexadecimal "
1150 for (i = strlen("naa."); name[i] != '\0'; i++) {
1151 if (!valid_hex(name[i])) {
1152 log_warnx("invalid character \"%c\" in target "
1153 "name \"%s\"; allowed characters are 1-9 "
1154 "and A-F", name[i], name);
1159 log_warnx("invalid target name \"%s\"; should start with "
1160 "either \"iqn.\", \"eui.\", or \"naa.\"",
1167 pport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1171 pp = calloc(1, sizeof(*pp));
1173 log_err(1, "calloc");
1175 pp->pp_name = checked_strdup(name);
1176 pp->pp_ctl_port = ctl_port;
1177 TAILQ_INIT(&pp->pp_ports);
1178 TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1183 pport_find(const struct conf *conf, const char *name)
1187 TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1188 if (strcasecmp(pp->pp_name, name) == 0)
1195 pport_copy(struct pport *pp, struct conf *conf)
1197 struct pport *ppnew;
1199 ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1204 pport_delete(struct pport *pp)
1206 struct port *port, *tport;
1208 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1210 TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1216 port_new(struct conf *conf, struct target *target, struct portal_group *pg)
1222 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1224 log_err(1, "asprintf");
1225 if (port_find(conf, name) != NULL) {
1226 log_warnx("duplicate port \"%s\"", name);
1230 port = calloc(1, sizeof(*port));
1232 log_err(1, "calloc");
1233 port->p_conf = conf;
1234 port->p_name = name;
1235 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1236 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1237 port->p_target = target;
1238 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1239 port->p_portal_group = pg;
1244 port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1250 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1252 log_err(1, "asprintf");
1253 if (port_find(conf, name) != NULL) {
1254 log_warnx("duplicate port \"%s\"", name);
1258 port = calloc(1, sizeof(*port));
1260 log_err(1, "calloc");
1261 port->p_conf = conf;
1262 port->p_name = name;
1263 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1264 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1265 port->p_target = target;
1266 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1272 port_find(const struct conf *conf, const char *name)
1276 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1277 if (strcasecmp(port->p_name, name) == 0)
1285 port_find_in_pg(const struct portal_group *pg, const char *target)
1289 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1290 if (strcasecmp(port->p_target->t_name, target) == 0)
1298 port_delete(struct port *port)
1301 if (port->p_portal_group)
1302 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1304 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1306 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1307 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1313 port_is_dummy(struct port *port)
1316 if (port->p_portal_group) {
1317 if (port->p_portal_group->pg_foreign)
1319 if (TAILQ_EMPTY(&port->p_portal_group->pg_portals))
1326 target_new(struct conf *conf, const char *name)
1328 struct target *targ;
1331 targ = target_find(conf, name);
1333 log_warnx("duplicated target \"%s\"", name);
1336 if (valid_iscsi_name(name) == false) {
1337 log_warnx("target name \"%s\" is invalid", name);
1340 targ = calloc(1, sizeof(*targ));
1342 log_err(1, "calloc");
1343 targ->t_name = checked_strdup(name);
1346 * RFC 3722 requires us to normalize the name to lowercase.
1349 for (i = 0; i < len; i++)
1350 targ->t_name[i] = tolower(targ->t_name[i]);
1352 targ->t_conf = conf;
1353 TAILQ_INIT(&targ->t_ports);
1354 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1360 target_delete(struct target *targ)
1362 struct port *port, *tport;
1364 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1366 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1369 free(targ->t_redirection);
1374 target_find(struct conf *conf, const char *name)
1376 struct target *targ;
1378 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1379 if (strcasecmp(targ->t_name, name) == 0)
1387 target_set_redirection(struct target *target, const char *addr)
1390 if (target->t_redirection != NULL) {
1391 log_warnx("cannot set redirection to \"%s\" for "
1392 "target \"%s\"; already defined",
1393 addr, target->t_name);
1397 target->t_redirection = checked_strdup(addr);
1403 lun_new(struct conf *conf, const char *name)
1407 lun = lun_find(conf, name);
1409 log_warnx("duplicated lun \"%s\"", name);
1413 lun = calloc(1, sizeof(*lun));
1415 log_err(1, "calloc");
1417 lun->l_name = checked_strdup(name);
1418 TAILQ_INIT(&lun->l_options);
1419 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1420 lun->l_ctl_lun = -1;
1426 lun_delete(struct lun *lun)
1428 struct target *targ;
1429 struct option *o, *tmp;
1432 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1433 for (i = 0; i < MAX_LUNS; i++) {
1434 if (targ->t_luns[i] == lun)
1435 targ->t_luns[i] = NULL;
1438 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1440 TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1441 option_delete(&lun->l_options, o);
1443 free(lun->l_backend);
1444 free(lun->l_device_id);
1446 free(lun->l_scsiname);
1447 free(lun->l_serial);
1452 lun_find(const struct conf *conf, const char *name)
1456 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1457 if (strcmp(lun->l_name, name) == 0)
1465 lun_set_backend(struct lun *lun, const char *value)
1467 free(lun->l_backend);
1468 lun->l_backend = checked_strdup(value);
1472 lun_set_blocksize(struct lun *lun, size_t value)
1475 lun->l_blocksize = value;
1479 lun_set_device_type(struct lun *lun, uint8_t value)
1482 lun->l_device_type = value;
1486 lun_set_device_id(struct lun *lun, const char *value)
1488 free(lun->l_device_id);
1489 lun->l_device_id = checked_strdup(value);
1493 lun_set_path(struct lun *lun, const char *value)
1496 lun->l_path = checked_strdup(value);
1500 lun_set_scsiname(struct lun *lun, const char *value)
1502 free(lun->l_scsiname);
1503 lun->l_scsiname = checked_strdup(value);
1507 lun_set_serial(struct lun *lun, const char *value)
1509 free(lun->l_serial);
1510 lun->l_serial = checked_strdup(value);
1514 lun_set_size(struct lun *lun, size_t value)
1517 lun->l_size = value;
1521 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1524 lun->l_ctl_lun = value;
1528 option_new(struct options *options, const char *name, const char *value)
1532 o = option_find(options, name);
1534 log_warnx("duplicated option \"%s\"", name);
1538 o = calloc(1, sizeof(*o));
1540 log_err(1, "calloc");
1541 o->o_name = checked_strdup(name);
1542 o->o_value = checked_strdup(value);
1543 TAILQ_INSERT_TAIL(options, o, o_next);
1549 option_delete(struct options *options, struct option *o)
1552 TAILQ_REMOVE(options, o, o_next);
1559 option_find(const struct options *options, const char *name)
1563 TAILQ_FOREACH(o, options, o_next) {
1564 if (strcmp(o->o_name, name) == 0)
1572 option_set(struct option *o, const char *value)
1576 o->o_value = checked_strdup(value);
1579 static struct connection *
1580 connection_new(struct portal *portal, int fd, const char *host,
1581 const struct sockaddr *client_sa)
1583 struct connection *conn;
1585 conn = calloc(1, sizeof(*conn));
1587 log_err(1, "calloc");
1588 conn->conn_portal = portal;
1589 conn->conn_socket = fd;
1590 conn->conn_initiator_addr = checked_strdup(host);
1591 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1594 * Default values, from RFC 3720, section 12.
1596 conn->conn_max_recv_data_segment_length = 8192;
1597 conn->conn_max_send_data_segment_length = 8192;
1598 conn->conn_max_burst_length = 262144;
1599 conn->conn_first_burst_length = 65536;
1600 conn->conn_immediate_data = true;
1607 conf_print(struct conf *conf)
1609 struct auth_group *ag;
1611 struct auth_name *auth_name;
1612 struct auth_portal *auth_portal;
1613 struct portal_group *pg;
1614 struct portal *portal;
1615 struct target *targ;
1619 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1620 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1621 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1622 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1623 auth->a_user, auth->a_secret,
1624 auth->a_mutual_user, auth->a_mutual_secret);
1625 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1626 fprintf(stderr, "\t initiator-name %s\n",
1627 auth_name->an_initator_name);
1628 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1629 fprintf(stderr, "\t initiator-portal %s\n",
1630 auth_portal->an_initator_portal);
1631 fprintf(stderr, "}\n");
1633 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1634 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1635 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1636 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1637 fprintf(stderr, "}\n");
1639 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1640 fprintf(stderr, "\tlun %s {\n", lun->l_name);
1641 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1642 TAILQ_FOREACH(o, &lun->l_options, o_next)
1643 fprintf(stderr, "\t\toption %s %s\n",
1644 lo->o_name, lo->o_value);
1645 fprintf(stderr, "\t}\n");
1647 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1648 fprintf(stderr, "target %s {\n", targ->t_name);
1649 if (targ->t_alias != NULL)
1650 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1651 fprintf(stderr, "}\n");
1657 conf_verify_lun(struct lun *lun)
1659 const struct lun *lun2;
1661 if (lun->l_backend == NULL)
1662 lun_set_backend(lun, "block");
1663 if (strcmp(lun->l_backend, "block") == 0) {
1664 if (lun->l_path == NULL) {
1665 log_warnx("missing path for lun \"%s\"",
1669 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1670 if (lun->l_size == 0) {
1671 log_warnx("missing size for ramdisk-backed lun \"%s\"",
1675 if (lun->l_path != NULL) {
1676 log_warnx("path must not be specified "
1677 "for ramdisk-backed lun \"%s\"",
1682 if (lun->l_blocksize == 0) {
1683 if (lun->l_device_type == 5)
1684 lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1686 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1687 } else if (lun->l_blocksize < 0) {
1688 log_warnx("invalid blocksize for lun \"%s\"; "
1689 "must be larger than 0", lun->l_name);
1692 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1693 log_warnx("invalid size for lun \"%s\"; "
1694 "must be multiple of blocksize", lun->l_name);
1697 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1700 if (lun->l_path != NULL && lun2->l_path != NULL &&
1701 strcmp(lun->l_path, lun2->l_path) == 0) {
1702 log_debugx("WARNING: path \"%s\" duplicated "
1703 "between lun \"%s\", and "
1704 "lun \"%s\"", lun->l_path,
1705 lun->l_name, lun2->l_name);
1713 conf_verify(struct conf *conf)
1715 struct auth_group *ag;
1716 struct portal_group *pg;
1718 struct target *targ;
1723 if (conf->conf_pidfile_path == NULL)
1724 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1726 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1727 error = conf_verify_lun(lun);
1731 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1732 if (targ->t_auth_group == NULL) {
1733 targ->t_auth_group = auth_group_find(conf,
1735 assert(targ->t_auth_group != NULL);
1737 if (TAILQ_EMPTY(&targ->t_ports)) {
1738 pg = portal_group_find(conf, "default");
1740 port_new(conf, targ, pg);
1743 for (i = 0; i < MAX_LUNS; i++) {
1744 if (targ->t_luns[i] != NULL)
1747 if (!found && targ->t_redirection == NULL) {
1748 log_warnx("no LUNs defined for target \"%s\"",
1751 if (found && targ->t_redirection != NULL) {
1752 log_debugx("target \"%s\" contains luns, "
1753 " but configured for redirection",
1757 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1758 assert(pg->pg_name != NULL);
1759 if (pg->pg_discovery_auth_group == NULL) {
1760 pg->pg_discovery_auth_group =
1761 auth_group_find(conf, "default");
1762 assert(pg->pg_discovery_auth_group != NULL);
1765 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1766 pg->pg_discovery_filter = PG_FILTER_NONE;
1768 if (pg->pg_redirection != NULL) {
1769 if (!TAILQ_EMPTY(&pg->pg_ports)) {
1770 log_debugx("portal-group \"%s\" assigned "
1771 "to target, but configured "
1775 pg->pg_unassigned = false;
1776 } else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1777 pg->pg_unassigned = false;
1779 if (strcmp(pg->pg_name, "default") != 0)
1780 log_warnx("portal-group \"%s\" not assigned "
1781 "to any target", pg->pg_name);
1782 pg->pg_unassigned = true;
1785 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1786 if (ag->ag_name == NULL)
1787 assert(ag->ag_target != NULL);
1789 assert(ag->ag_target == NULL);
1792 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1793 if (targ->t_auth_group == ag) {
1798 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1799 if (port->p_auth_group == ag) {
1804 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1805 if (pg->pg_discovery_auth_group == ag) {
1810 if (!found && ag->ag_name != NULL &&
1811 strcmp(ag->ag_name, "default") != 0 &&
1812 strcmp(ag->ag_name, "no-authentication") != 0 &&
1813 strcmp(ag->ag_name, "no-access") != 0) {
1814 log_warnx("auth-group \"%s\" not assigned "
1815 "to any target", ag->ag_name);
1823 conf_apply(struct conf *oldconf, struct conf *newconf)
1825 struct lun *oldlun, *newlun, *tmplun;
1826 struct portal_group *oldpg, *newpg;
1827 struct portal *oldp, *newp;
1828 struct port *oldport, *newport, *tmpport;
1829 struct isns *oldns, *newns;
1831 int changed, cumulated_error = 0, error, sockbuf;
1834 if (oldconf->conf_debug != newconf->conf_debug) {
1835 log_debugx("changing debug level to %d", newconf->conf_debug);
1836 log_init(newconf->conf_debug);
1839 if (oldconf->conf_pidfh != NULL) {
1840 assert(oldconf->conf_pidfile_path != NULL);
1841 if (newconf->conf_pidfile_path != NULL &&
1842 strcmp(oldconf->conf_pidfile_path,
1843 newconf->conf_pidfile_path) == 0) {
1844 newconf->conf_pidfh = oldconf->conf_pidfh;
1845 oldconf->conf_pidfh = NULL;
1847 log_debugx("removing pidfile %s",
1848 oldconf->conf_pidfile_path);
1849 pidfile_remove(oldconf->conf_pidfh);
1850 oldconf->conf_pidfh = NULL;
1854 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1855 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1856 newconf->conf_pidfh =
1857 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1858 if (newconf->conf_pidfh == NULL) {
1859 if (errno == EEXIST)
1860 log_errx(1, "daemon already running, pid: %jd.",
1861 (intmax_t)otherpid);
1862 log_err(1, "cannot open or create pidfile \"%s\"",
1863 newconf->conf_pidfile_path);
1868 * Go through the new portal groups, assigning tags or preserving old.
1870 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1871 if (newpg->pg_tag != 0)
1873 oldpg = portal_group_find(oldconf, newpg->pg_name);
1875 newpg->pg_tag = oldpg->pg_tag;
1877 newpg->pg_tag = ++last_portal_group_tag;
1880 /* Deregister on removed iSNS servers. */
1881 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1882 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1883 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1887 isns_deregister(oldns);
1891 * XXX: If target or lun removal fails, we should somehow "move"
1892 * the old lun or target into newconf, so that subsequent
1893 * conf_apply() would try to remove them again. That would
1894 * be somewhat hairy, though, and lun deletion failures don't
1895 * really happen, so leave it as it is for now.
1898 * First, remove any ports present in the old configuration
1899 * and missing in the new one.
1901 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1902 if (port_is_dummy(oldport))
1904 newport = port_find(newconf, oldport->p_name);
1905 if (newport != NULL && !port_is_dummy(newport))
1907 log_debugx("removing port \"%s\"", oldport->p_name);
1908 error = kernel_port_remove(oldport);
1910 log_warnx("failed to remove port %s",
1913 * XXX: Uncomment after fixing the root cause.
1915 * cumulated_error++;
1921 * Second, remove any LUNs present in the old configuration
1922 * and missing in the new one.
1924 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1925 newlun = lun_find(newconf, oldlun->l_name);
1926 if (newlun == NULL) {
1927 log_debugx("lun \"%s\", CTL lun %d "
1928 "not found in new configuration; "
1929 "removing", oldlun->l_name, oldlun->l_ctl_lun);
1930 error = kernel_lun_remove(oldlun);
1932 log_warnx("failed to remove lun \"%s\", "
1934 oldlun->l_name, oldlun->l_ctl_lun);
1941 * Also remove the LUNs changed by more than size.
1944 assert(oldlun->l_backend != NULL);
1945 assert(newlun->l_backend != NULL);
1946 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1947 log_debugx("backend for lun \"%s\", "
1948 "CTL lun %d changed; removing",
1949 oldlun->l_name, oldlun->l_ctl_lun);
1952 if (oldlun->l_blocksize != newlun->l_blocksize) {
1953 log_debugx("blocksize for lun \"%s\", "
1954 "CTL lun %d changed; removing",
1955 oldlun->l_name, oldlun->l_ctl_lun);
1958 if (newlun->l_device_id != NULL &&
1959 (oldlun->l_device_id == NULL ||
1960 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1962 log_debugx("device-id for lun \"%s\", "
1963 "CTL lun %d changed; removing",
1964 oldlun->l_name, oldlun->l_ctl_lun);
1967 if (newlun->l_path != NULL &&
1968 (oldlun->l_path == NULL ||
1969 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1970 log_debugx("path for lun \"%s\", "
1971 "CTL lun %d, changed; removing",
1972 oldlun->l_name, oldlun->l_ctl_lun);
1975 if (newlun->l_serial != NULL &&
1976 (oldlun->l_serial == NULL ||
1977 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1978 log_debugx("serial for lun \"%s\", "
1979 "CTL lun %d changed; removing",
1980 oldlun->l_name, oldlun->l_ctl_lun);
1984 error = kernel_lun_remove(oldlun);
1986 log_warnx("failed to remove lun \"%s\", "
1988 oldlun->l_name, oldlun->l_ctl_lun);
1995 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1998 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1999 oldlun = lun_find(oldconf, newlun->l_name);
2000 if (oldlun != NULL) {
2001 log_debugx("modifying lun \"%s\", CTL lun %d",
2002 newlun->l_name, newlun->l_ctl_lun);
2003 error = kernel_lun_modify(newlun);
2005 log_warnx("failed to "
2006 "modify lun \"%s\", CTL lun %d",
2007 newlun->l_name, newlun->l_ctl_lun);
2012 log_debugx("adding lun \"%s\"", newlun->l_name);
2013 error = kernel_lun_add(newlun);
2015 log_warnx("failed to add lun \"%s\"", newlun->l_name);
2022 * Now add new ports or modify existing ones.
2024 TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
2025 if (port_is_dummy(newport))
2027 oldport = port_find(oldconf, newport->p_name);
2029 if (oldport == NULL || port_is_dummy(oldport)) {
2030 log_debugx("adding port \"%s\"", newport->p_name);
2031 error = kernel_port_add(newport);
2033 log_debugx("updating port \"%s\"", newport->p_name);
2034 newport->p_ctl_port = oldport->p_ctl_port;
2035 error = kernel_port_update(newport, oldport);
2038 log_warnx("failed to %s port %s",
2039 (oldport == NULL) ? "add" : "update",
2042 * XXX: Uncomment after fixing the root cause.
2044 * cumulated_error++;
2050 * Go through the new portals, opening the sockets as necessary.
2052 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2053 if (newpg->pg_foreign)
2055 if (newpg->pg_unassigned) {
2056 log_debugx("not listening on portal-group \"%s\", "
2057 "not assigned to any target",
2061 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2063 * Try to find already open portal and reuse
2064 * the listening socket. We don't care about
2065 * what portal or portal group that was, what
2066 * matters is the listening address.
2068 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2070 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2072 if (strcmp(newp->p_listen,
2073 oldp->p_listen) == 0 &&
2074 oldp->p_socket > 0) {
2082 if (newp->p_socket > 0) {
2084 * We're done with this portal.
2089 #ifdef ICL_KERNEL_PROXY
2091 newpg->pg_conf->conf_portal_id++;
2092 newp->p_id = newpg->pg_conf->conf_portal_id;
2093 log_debugx("listening on %s, portal-group "
2094 "\"%s\", portal id %d, using ICL proxy",
2095 newp->p_listen, newpg->pg_name, newp->p_id);
2096 kernel_listen(newp->p_ai, newp->p_iser,
2101 assert(proxy_mode == false);
2102 assert(newp->p_iser == false);
2104 log_debugx("listening on %s, portal-group \"%s\"",
2105 newp->p_listen, newpg->pg_name);
2106 newp->p_socket = socket(newp->p_ai->ai_family,
2107 newp->p_ai->ai_socktype,
2108 newp->p_ai->ai_protocol);
2109 if (newp->p_socket < 0) {
2110 log_warn("socket(2) failed for %s",
2115 sockbuf = SOCKBUF_SIZE;
2116 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2117 &sockbuf, sizeof(sockbuf)) == -1)
2118 log_warn("setsockopt(SO_RCVBUF) failed "
2119 "for %s", newp->p_listen);
2120 sockbuf = SOCKBUF_SIZE;
2121 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2122 &sockbuf, sizeof(sockbuf)) == -1)
2123 log_warn("setsockopt(SO_SNDBUF) failed "
2124 "for %s", newp->p_listen);
2125 error = setsockopt(newp->p_socket, SOL_SOCKET,
2126 SO_REUSEADDR, &one, sizeof(one));
2128 log_warn("setsockopt(SO_REUSEADDR) failed "
2129 "for %s", newp->p_listen);
2130 close(newp->p_socket);
2135 error = bind(newp->p_socket, newp->p_ai->ai_addr,
2136 newp->p_ai->ai_addrlen);
2138 log_warn("bind(2) failed for %s",
2140 close(newp->p_socket);
2145 error = listen(newp->p_socket, -1);
2147 log_warn("listen(2) failed for %s",
2149 close(newp->p_socket);
2158 * Go through the no longer used sockets, closing them.
2160 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2161 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2162 if (oldp->p_socket <= 0)
2164 log_debugx("closing socket for %s, portal-group \"%s\"",
2165 oldp->p_listen, oldpg->pg_name);
2166 close(oldp->p_socket);
2171 /* (Re-)Register on remaining/new iSNS servers. */
2172 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2173 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2174 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2177 isns_register(newns, oldns);
2180 /* Schedule iSNS update */
2181 if (!TAILQ_EMPTY(&newconf->conf_isns))
2182 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2184 return (cumulated_error);
2191 return (sigalrm_received);
2195 sigalrm_handler_fatal(int dummy __unused)
2198 * It would be easiest to just log an error and exit. We can't
2199 * do this, though, because log_errx() is not signal safe, since
2200 * it calls syslog(3). Instead, set a flag checked by pdu_send()
2201 * and pdu_receive(), to call log_errx() there. Should they fail
2202 * to notice, we'll exit here one second later.
2204 if (sigalrm_received) {
2206 * Oh well. Just give up and quit.
2211 sigalrm_received = true;
2215 sigalrm_handler(int dummy __unused)
2218 sigalrm_received = true;
2222 set_timeout(int timeout, int fatal)
2224 struct sigaction sa;
2225 struct itimerval itv;
2229 log_debugx("session timeout disabled");
2230 bzero(&itv, sizeof(itv));
2231 error = setitimer(ITIMER_REAL, &itv, NULL);
2233 log_err(1, "setitimer");
2234 sigalrm_received = false;
2238 sigalrm_received = false;
2239 bzero(&sa, sizeof(sa));
2241 sa.sa_handler = sigalrm_handler_fatal;
2243 sa.sa_handler = sigalrm_handler;
2244 sigfillset(&sa.sa_mask);
2245 error = sigaction(SIGALRM, &sa, NULL);
2247 log_err(1, "sigaction");
2250 * First SIGALRM will arive after conf_timeout seconds.
2251 * If we do nothing, another one will arrive a second later.
2253 log_debugx("setting session timeout to %d seconds", timeout);
2254 bzero(&itv, sizeof(itv));
2255 itv.it_interval.tv_sec = 1;
2256 itv.it_value.tv_sec = timeout;
2257 error = setitimer(ITIMER_REAL, &itv, NULL);
2259 log_err(1, "setitimer");
2263 wait_for_children(bool block)
2271 * If "block" is true, wait for at least one process.
2273 if (block && num == 0)
2274 pid = wait4(-1, &status, 0, NULL);
2276 pid = wait4(-1, &status, WNOHANG, NULL);
2279 if (WIFSIGNALED(status)) {
2280 log_warnx("child process %d terminated with signal %d",
2281 pid, WTERMSIG(status));
2282 } else if (WEXITSTATUS(status) != 0) {
2283 log_warnx("child process %d terminated with exit status %d",
2284 pid, WEXITSTATUS(status));
2286 log_debugx("child process %d terminated gracefully", pid);
2295 handle_connection(struct portal *portal, int fd,
2296 const struct sockaddr *client_sa, bool dont_fork)
2298 struct connection *conn;
2301 char host[NI_MAXHOST + 1];
2304 conf = portal->p_portal_group->pg_conf;
2307 log_debugx("incoming connection; not forking due to -d flag");
2309 nchildren -= wait_for_children(false);
2310 assert(nchildren >= 0);
2312 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2313 log_debugx("maxproc limit of %d child processes hit; "
2314 "waiting for child process to exit", conf->conf_maxproc);
2315 nchildren -= wait_for_children(true);
2316 assert(nchildren >= 0);
2318 log_debugx("incoming connection; forking child process #%d",
2329 pidfile_close(conf->conf_pidfh);
2331 error = getnameinfo(client_sa, client_sa->sa_len,
2332 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2334 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2336 log_debugx("accepted connection from %s; portal group \"%s\"",
2337 host, portal->p_portal_group->pg_name);
2338 log_set_peer_addr(host);
2339 setproctitle("%s", host);
2341 conn = connection_new(portal, fd, host, client_sa);
2342 set_timeout(conf->conf_timeout, true);
2345 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2346 kernel_handoff(conn);
2347 log_debugx("connection handed off to the kernel");
2349 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2352 log_debugx("nothing more to do; exiting");
2357 fd_add(int fd, fd_set *fdset, int nfds)
2361 * Skip sockets which we failed to bind.
2373 main_loop(struct conf *conf, bool dont_fork)
2375 struct portal_group *pg;
2376 struct portal *portal;
2377 struct sockaddr_storage client_sa;
2378 socklen_t client_salen;
2379 #ifdef ICL_KERNEL_PROXY
2384 int error, nfds, client_fd;
2386 pidfile_write(conf->conf_pidfh);
2389 if (sighup_received || sigterm_received || timed_out())
2392 #ifdef ICL_KERNEL_PROXY
2394 client_salen = sizeof(client_sa);
2395 kernel_accept(&connection_id, &portal_id,
2396 (struct sockaddr *)&client_sa, &client_salen);
2397 assert(client_salen >= client_sa.ss_len);
2399 log_debugx("incoming connection, id %d, portal id %d",
2400 connection_id, portal_id);
2401 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2402 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2403 if (portal->p_id == portal_id) {
2409 log_errx(1, "kernel returned invalid portal_id %d",
2413 handle_connection(portal, connection_id,
2414 (struct sockaddr *)&client_sa, dont_fork);
2417 assert(proxy_mode == false);
2421 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2422 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2423 nfds = fd_add(portal->p_socket, &fdset, nfds);
2425 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2429 log_err(1, "select");
2431 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2432 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2433 if (!FD_ISSET(portal->p_socket, &fdset))
2435 client_salen = sizeof(client_sa);
2436 client_fd = accept(portal->p_socket,
2437 (struct sockaddr *)&client_sa,
2439 if (client_fd < 0) {
2440 if (errno == ECONNABORTED)
2442 log_err(1, "accept");
2444 assert(client_salen >= client_sa.ss_len);
2446 handle_connection(portal, client_fd,
2447 (struct sockaddr *)&client_sa,
2452 #ifdef ICL_KERNEL_PROXY
2459 sighup_handler(int dummy __unused)
2462 sighup_received = true;
2466 sigterm_handler(int dummy __unused)
2469 sigterm_received = true;
2473 sigchld_handler(int dummy __unused)
2477 * The only purpose of this handler is to make SIGCHLD
2478 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2479 * wait_for_children().
2484 register_signals(void)
2486 struct sigaction sa;
2489 bzero(&sa, sizeof(sa));
2490 sa.sa_handler = sighup_handler;
2491 sigfillset(&sa.sa_mask);
2492 error = sigaction(SIGHUP, &sa, NULL);
2494 log_err(1, "sigaction");
2496 sa.sa_handler = sigterm_handler;
2497 error = sigaction(SIGTERM, &sa, NULL);
2499 log_err(1, "sigaction");
2501 sa.sa_handler = sigterm_handler;
2502 error = sigaction(SIGINT, &sa, NULL);
2504 log_err(1, "sigaction");
2506 sa.sa_handler = sigchld_handler;
2507 error = sigaction(SIGCHLD, &sa, NULL);
2509 log_err(1, "sigaction");
2513 check_perms(const char *path)
2518 error = stat(path, &sb);
2523 if (sb.st_mode & S_IWOTH) {
2524 log_warnx("%s is world-writable", path);
2525 } else if (sb.st_mode & S_IROTH) {
2526 log_warnx("%s is world-readable", path);
2527 } else if (sb.st_mode & S_IXOTH) {
2529 * Ok, this one doesn't matter, but still do it,
2530 * just for consistency.
2532 log_warnx("%s is world-executable", path);
2536 * XXX: Should we also check for owner != 0?
2540 static struct conf *
2541 conf_new_from_file(const char *path, struct conf *oldconf, bool ucl)
2544 struct auth_group *ag;
2545 struct portal_group *pg;
2549 log_debugx("obtaining configuration from %s", path);
2553 TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next)
2554 pport_copy(pp, conf);
2556 ag = auth_group_new(conf, "default");
2559 ag = auth_group_new(conf, "no-authentication");
2561 ag->ag_type = AG_TYPE_NO_AUTHENTICATION;
2563 ag = auth_group_new(conf, "no-access");
2565 ag->ag_type = AG_TYPE_DENY;
2567 pg = portal_group_new(conf, "default");
2571 error = uclparse_conf(conf, path);
2573 error = parse_conf(conf, path);
2582 if (conf->conf_default_ag_defined == false) {
2583 log_debugx("auth-group \"default\" not defined; "
2584 "going with defaults");
2585 ag = auth_group_find(conf, "default");
2587 ag->ag_type = AG_TYPE_DENY;
2590 if (conf->conf_default_pg_defined == false) {
2591 log_debugx("portal-group \"default\" not defined; "
2592 "going with defaults");
2593 pg = portal_group_find(conf, "default");
2595 portal_group_add_listen(pg, "0.0.0.0:3260", false);
2596 portal_group_add_listen(pg, "[::]:3260", false);
2599 conf->conf_kernel_port_on = true;
2601 error = conf_verify(conf);
2611 main(int argc, char **argv)
2613 struct conf *oldconf, *newconf, *tmpconf;
2615 const char *config_path = DEFAULT_CONFIG_PATH;
2616 int debug = 0, ch, error;
2617 bool dont_daemonize = false;
2618 bool use_ucl = false;
2620 while ((ch = getopt(argc, argv, "duf:R")) != -1) {
2623 dont_daemonize = true;
2630 config_path = optarg;
2633 #ifndef ICL_KERNEL_PROXY
2634 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2635 "does not support iSER protocol");
2651 oldconf = conf_new_from_kernel();
2652 newconf = conf_new_from_file(config_path, oldconf, use_ucl);
2654 if (newconf == NULL)
2655 log_errx(1, "configuration error; exiting");
2657 oldconf->conf_debug = debug;
2658 newconf->conf_debug = debug;
2661 error = conf_apply(oldconf, newconf);
2663 log_errx(1, "failed to apply configuration; exiting");
2665 conf_delete(oldconf);
2670 if (dont_daemonize == false) {
2671 log_debugx("daemonizing");
2672 if (daemon(0, 0) == -1) {
2673 log_warn("cannot daemonize");
2674 pidfile_remove(newconf->conf_pidfh);
2679 /* Schedule iSNS update */
2680 if (!TAILQ_EMPTY(&newconf->conf_isns))
2681 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2684 main_loop(newconf, dont_daemonize);
2685 if (sighup_received) {
2686 sighup_received = false;
2687 log_debugx("received SIGHUP, reloading configuration");
2688 tmpconf = conf_new_from_file(config_path, newconf,
2691 if (tmpconf == NULL) {
2692 log_warnx("configuration error, "
2693 "continuing with old configuration");
2696 tmpconf->conf_debug = debug;
2699 error = conf_apply(oldconf, newconf);
2701 log_warnx("failed to reload "
2703 conf_delete(oldconf);
2706 } else if (sigterm_received) {
2707 log_debugx("exiting on signal; "
2708 "reloading empty configuration");
2710 log_debugx("removing CTL iSCSI ports "
2711 "and terminating all connections");
2714 newconf = conf_new();
2716 newconf->conf_debug = debug;
2717 error = conf_apply(oldconf, newconf);
2719 log_warnx("failed to apply configuration");
2720 conf_delete(oldconf);
2723 log_warnx("exiting on signal");
2726 nchildren -= wait_for_children(false);
2727 assert(nchildren >= 0);
2729 set_timeout(0, false);
2730 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2732 /* Schedule iSNS update */
2733 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2734 set_timeout((newconf->conf_isns_period