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 16 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 16 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);
405 log_warnx("incorrect initiator portal \"%s\"", portal);
410 auth_portal_delete(struct auth_portal *ap)
412 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
414 free(ap->ap_initator_portal);
419 auth_portal_defined(const struct auth_group *ag)
421 if (TAILQ_EMPTY(&ag->ag_portals))
426 const struct auth_portal *
427 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
429 const struct auth_portal *ap;
430 const uint8_t *a, *b;
434 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
435 if (ap->ap_sa.ss_family != ss->ss_family)
437 if (ss->ss_family == AF_INET) {
438 a = (const uint8_t *)
439 &((const struct sockaddr_in *)ss)->sin_addr;
440 b = (const uint8_t *)
441 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
443 a = (const uint8_t *)
444 &((const struct sockaddr_in6 *)ss)->sin6_addr;
445 b = (const uint8_t *)
446 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
448 for (i = 0; i < ap->ap_mask / 8; i++) {
452 if (ap->ap_mask % 8) {
453 bmask = 0xff << (8 - (ap->ap_mask % 8));
454 if ((a[i] & bmask) != (b[i] & bmask))
466 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
469 if (!auth_portal_defined(ag))
472 if (auth_portal_find(ag, sa) == NULL)
479 auth_group_new(struct conf *conf, const char *name)
481 struct auth_group *ag;
484 ag = auth_group_find(conf, name);
486 log_warnx("duplicated auth-group \"%s\"", name);
491 ag = calloc(1, sizeof(*ag));
493 log_err(1, "calloc");
495 ag->ag_name = checked_strdup(name);
496 TAILQ_INIT(&ag->ag_auths);
497 TAILQ_INIT(&ag->ag_names);
498 TAILQ_INIT(&ag->ag_portals);
500 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
506 auth_group_delete(struct auth_group *ag)
508 struct auth *auth, *auth_tmp;
509 struct auth_name *auth_name, *auth_name_tmp;
510 struct auth_portal *auth_portal, *auth_portal_tmp;
512 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
514 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
516 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
517 auth_name_delete(auth_name);
518 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
520 auth_portal_delete(auth_portal);
526 auth_group_find(const struct conf *conf, const char *name)
528 struct auth_group *ag;
530 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
531 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
539 auth_group_set_type(struct auth_group *ag, const char *str)
543 if (strcmp(str, "none") == 0) {
544 type = AG_TYPE_NO_AUTHENTICATION;
545 } else if (strcmp(str, "deny") == 0) {
547 } else if (strcmp(str, "chap") == 0) {
549 } else if (strcmp(str, "chap-mutual") == 0) {
550 type = AG_TYPE_CHAP_MUTUAL;
552 if (ag->ag_name != NULL)
553 log_warnx("invalid auth-type \"%s\" for auth-group "
554 "\"%s\"", str, ag->ag_name);
556 log_warnx("invalid auth-type \"%s\" for target "
557 "\"%s\"", str, ag->ag_target->t_name);
561 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
562 if (ag->ag_name != NULL) {
563 log_warnx("cannot set auth-type to \"%s\" for "
564 "auth-group \"%s\"; already has a different "
565 "type", str, ag->ag_name);
567 log_warnx("cannot set auth-type to \"%s\" for target "
568 "\"%s\"; already has a different type",
569 str, ag->ag_target->t_name);
579 static struct portal *
580 portal_new(struct portal_group *pg)
582 struct portal *portal;
584 portal = calloc(1, sizeof(*portal));
586 log_err(1, "calloc");
587 TAILQ_INIT(&portal->p_targets);
588 portal->p_portal_group = pg;
589 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
594 portal_delete(struct portal *portal)
597 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
598 if (portal->p_ai != NULL)
599 freeaddrinfo(portal->p_ai);
600 free(portal->p_listen);
604 struct portal_group *
605 portal_group_new(struct conf *conf, const char *name)
607 struct portal_group *pg;
609 pg = portal_group_find(conf, name);
611 log_warnx("duplicated portal-group \"%s\"", name);
615 pg = calloc(1, sizeof(*pg));
617 log_err(1, "calloc");
618 pg->pg_name = checked_strdup(name);
619 TAILQ_INIT(&pg->pg_options);
620 TAILQ_INIT(&pg->pg_portals);
621 TAILQ_INIT(&pg->pg_ports);
623 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
624 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
630 portal_group_delete(struct portal_group *pg)
632 struct portal *portal, *tmp;
633 struct port *port, *tport;
634 struct option *o, *otmp;
636 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
638 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
640 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
641 portal_delete(portal);
642 TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
643 option_delete(&pg->pg_options, o);
645 free(pg->pg_offload);
646 free(pg->pg_redirection);
650 struct portal_group *
651 portal_group_find(const struct conf *conf, const char *name)
653 struct portal_group *pg;
655 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
656 if (strcmp(pg->pg_name, name) == 0)
664 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
666 struct addrinfo hints;
667 char *str, *addr, *ch;
669 int error, colons = 0;
671 str = arg = strdup(arg);
674 * IPv6 address in square brackets, perhaps with port.
677 addr = strsep(&arg, "]");
680 if (arg[0] == '\0') {
682 } else if (arg[0] == ':') {
690 * Either IPv6 address without brackets - and without
691 * a port - or IPv4 address. Just count the colons.
693 for (ch = arg; *ch != '\0'; ch++) {
701 addr = strsep(&arg, ":");
709 memset(&hints, 0, sizeof(hints));
710 hints.ai_family = PF_UNSPEC;
711 hints.ai_socktype = SOCK_STREAM;
712 hints.ai_flags = AI_PASSIVE;
713 error = getaddrinfo(addr, port, &hints, ai);
715 return ((error != 0) ? 1 : 0);
719 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
721 struct portal *portal;
723 portal = portal_new(pg);
724 portal->p_listen = checked_strdup(value);
725 portal->p_iser = iser;
727 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
728 log_warnx("invalid listen address %s", portal->p_listen);
729 portal_delete(portal);
734 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
735 * those into multiple portals.
742 isns_new(struct conf *conf, const char *addr)
746 isns = calloc(1, sizeof(*isns));
748 log_err(1, "calloc");
750 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
751 isns->i_addr = checked_strdup(addr);
753 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
754 log_warnx("invalid iSNS address %s", isns->i_addr);
760 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
761 * those into multiple servers.
768 isns_delete(struct isns *isns)
771 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
773 if (isns->i_ai != NULL)
774 freeaddrinfo(isns->i_ai);
779 isns_do_connect(struct isns *isns)
783 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
784 isns->i_ai->ai_protocol);
786 log_warn("socket(2) failed for %s", isns->i_addr);
789 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
790 log_warn("connect(2) failed for %s", isns->i_addr);
798 isns_do_register(struct isns *isns, int s, const char *hostname)
800 struct conf *conf = isns->i_conf;
801 struct target *target;
802 struct portal *portal;
803 struct portal_group *pg;
805 struct isns_req *req;
809 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
810 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
811 isns_req_add_delim(req);
812 isns_req_add_str(req, 1, hostname);
813 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
814 isns_req_add_32(req, 6, conf->conf_isns_period);
815 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
816 if (pg->pg_unassigned)
818 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
819 isns_req_add_addr(req, 16, portal->p_ai);
820 isns_req_add_port(req, 17, portal->p_ai);
823 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
824 isns_req_add_str(req, 32, target->t_name);
825 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
826 if (target->t_alias != NULL)
827 isns_req_add_str(req, 34, target->t_alias);
828 TAILQ_FOREACH(port, &target->t_ports, p_ts) {
829 if ((pg = port->p_portal_group) == NULL)
831 isns_req_add_32(req, 51, pg->pg_tag);
832 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
833 isns_req_add_addr(req, 49, portal->p_ai);
834 isns_req_add_port(req, 50, portal->p_ai);
838 res = isns_req_send(s, req);
840 log_warn("send(2) failed for %s", isns->i_addr);
843 res = isns_req_receive(s, req);
845 log_warn("receive(2) failed for %s", isns->i_addr);
848 error = isns_req_get_status(req);
850 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
859 isns_do_check(struct isns *isns, int s, const char *hostname)
861 struct conf *conf = isns->i_conf;
862 struct isns_req *req;
866 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
867 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
868 isns_req_add_str(req, 1, hostname);
869 isns_req_add_delim(req);
870 isns_req_add(req, 2, 0, NULL);
871 res = isns_req_send(s, req);
873 log_warn("send(2) failed for %s", isns->i_addr);
876 res = isns_req_receive(s, req);
878 log_warn("receive(2) failed for %s", isns->i_addr);
881 error = isns_req_get_status(req);
883 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
892 isns_do_deregister(struct isns *isns, int s, const char *hostname)
894 struct conf *conf = isns->i_conf;
895 struct isns_req *req;
899 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
900 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
901 isns_req_add_delim(req);
902 isns_req_add_str(req, 1, hostname);
903 res = isns_req_send(s, req);
905 log_warn("send(2) failed for %s", isns->i_addr);
908 res = isns_req_receive(s, req);
910 log_warn("receive(2) failed for %s", isns->i_addr);
913 error = isns_req_get_status(req);
915 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
924 isns_register(struct isns *isns, struct isns *oldisns)
926 struct conf *conf = isns->i_conf;
930 if (TAILQ_EMPTY(&conf->conf_targets) ||
931 TAILQ_EMPTY(&conf->conf_portal_groups))
933 set_timeout(conf->conf_isns_timeout, false);
934 s = isns_do_connect(isns);
936 set_timeout(0, false);
939 gethostname(hostname, sizeof(hostname));
941 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
943 isns_do_deregister(oldisns, s, hostname);
944 isns_do_register(isns, s, hostname);
946 set_timeout(0, false);
950 isns_check(struct isns *isns)
952 struct conf *conf = isns->i_conf;
956 if (TAILQ_EMPTY(&conf->conf_targets) ||
957 TAILQ_EMPTY(&conf->conf_portal_groups))
959 set_timeout(conf->conf_isns_timeout, false);
960 s = isns_do_connect(isns);
962 set_timeout(0, false);
965 gethostname(hostname, sizeof(hostname));
967 res = isns_do_check(isns, s, hostname);
969 isns_do_deregister(isns, s, hostname);
970 isns_do_register(isns, s, hostname);
973 set_timeout(0, false);
977 isns_deregister(struct isns *isns)
979 struct conf *conf = isns->i_conf;
983 if (TAILQ_EMPTY(&conf->conf_targets) ||
984 TAILQ_EMPTY(&conf->conf_portal_groups))
986 set_timeout(conf->conf_isns_timeout, false);
987 s = isns_do_connect(isns);
990 gethostname(hostname, sizeof(hostname));
992 isns_do_deregister(isns, s, hostname);
994 set_timeout(0, false);
998 portal_group_set_filter(struct portal_group *pg, const char *str)
1002 if (strcmp(str, "none") == 0) {
1003 filter = PG_FILTER_NONE;
1004 } else if (strcmp(str, "portal") == 0) {
1005 filter = PG_FILTER_PORTAL;
1006 } else if (strcmp(str, "portal-name") == 0) {
1007 filter = PG_FILTER_PORTAL_NAME;
1008 } else if (strcmp(str, "portal-name-auth") == 0) {
1009 filter = PG_FILTER_PORTAL_NAME_AUTH;
1011 log_warnx("invalid discovery-filter \"%s\" for portal-group "
1012 "\"%s\"; valid values are \"none\", \"portal\", "
1013 "\"portal-name\", and \"portal-name-auth\"",
1018 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1019 pg->pg_discovery_filter != filter) {
1020 log_warnx("cannot set discovery-filter to \"%s\" for "
1021 "portal-group \"%s\"; already has a different "
1022 "value", str, pg->pg_name);
1026 pg->pg_discovery_filter = filter;
1032 portal_group_set_offload(struct portal_group *pg, const char *offload)
1035 if (pg->pg_offload != NULL) {
1036 log_warnx("cannot set offload to \"%s\" for "
1037 "portal-group \"%s\"; already defined",
1038 offload, pg->pg_name);
1042 pg->pg_offload = checked_strdup(offload);
1048 portal_group_set_redirection(struct portal_group *pg, const char *addr)
1051 if (pg->pg_redirection != NULL) {
1052 log_warnx("cannot set redirection to \"%s\" for "
1053 "portal-group \"%s\"; already defined",
1058 pg->pg_redirection = checked_strdup(addr);
1064 valid_hex(const char ch)
1096 valid_iscsi_name(const char *name)
1100 if (strlen(name) >= MAX_NAME_LEN) {
1101 log_warnx("overlong name for target \"%s\"; max length allowed "
1102 "by iSCSI specification is %d characters",
1103 name, MAX_NAME_LEN);
1108 * In the cases below, we don't return an error, just in case the admin
1109 * was right, and we're wrong.
1111 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1112 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1114 * XXX: We should verify UTF-8 normalisation, as defined
1115 * by 3.2.6.2: iSCSI Name Encoding.
1117 if (isalnum(name[i]))
1119 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1121 log_warnx("invalid character \"%c\" in target name "
1122 "\"%s\"; allowed characters are letters, digits, "
1123 "'-', '.', and ':'", name[i], name);
1127 * XXX: Check more stuff: valid date and a valid reversed domain.
1129 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1130 if (strlen(name) != strlen("eui.") + 16)
1131 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1132 "should be followed by exactly 16 hexadecimal "
1134 for (i = strlen("eui."); name[i] != '\0'; i++) {
1135 if (!valid_hex(name[i])) {
1136 log_warnx("invalid character \"%c\" in target "
1137 "name \"%s\"; allowed characters are 1-9 "
1138 "and A-F", name[i], name);
1142 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1143 if (strlen(name) > strlen("naa.") + 32)
1144 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1145 "should be followed by at most 32 hexadecimal "
1147 for (i = strlen("naa."); 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);
1156 log_warnx("invalid target name \"%s\"; should start with "
1157 "either \"iqn.\", \"eui.\", or \"naa.\"",
1164 pport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1168 pp = calloc(1, sizeof(*pp));
1170 log_err(1, "calloc");
1172 pp->pp_name = checked_strdup(name);
1173 pp->pp_ctl_port = ctl_port;
1174 TAILQ_INIT(&pp->pp_ports);
1175 TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1180 pport_find(const struct conf *conf, const char *name)
1184 TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1185 if (strcasecmp(pp->pp_name, name) == 0)
1192 pport_copy(struct pport *pp, struct conf *conf)
1194 struct pport *ppnew;
1196 ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1201 pport_delete(struct pport *pp)
1203 struct port *port, *tport;
1205 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1207 TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1213 port_new(struct conf *conf, struct target *target, struct portal_group *pg)
1219 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1221 log_err(1, "asprintf");
1222 if (port_find(conf, name) != NULL) {
1223 log_warnx("duplicate port \"%s\"", name);
1227 port = calloc(1, sizeof(*port));
1229 log_err(1, "calloc");
1230 port->p_conf = conf;
1231 port->p_name = name;
1232 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1233 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1234 port->p_target = target;
1235 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1236 port->p_portal_group = pg;
1237 port->p_foreign = pg->pg_foreign;
1242 port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1248 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1250 log_err(1, "asprintf");
1251 if (port_find(conf, name) != NULL) {
1252 log_warnx("duplicate port \"%s\"", name);
1256 port = calloc(1, sizeof(*port));
1258 log_err(1, "calloc");
1259 port->p_conf = conf;
1260 port->p_name = name;
1261 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1262 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1263 port->p_target = target;
1264 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1270 port_find(const struct conf *conf, const char *name)
1274 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1275 if (strcasecmp(port->p_name, name) == 0)
1283 port_find_in_pg(const struct portal_group *pg, const char *target)
1287 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1288 if (strcasecmp(port->p_target->t_name, target) == 0)
1296 port_delete(struct port *port)
1299 if (port->p_portal_group)
1300 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1302 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1304 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1305 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1311 target_new(struct conf *conf, const char *name)
1313 struct target *targ;
1316 targ = target_find(conf, name);
1318 log_warnx("duplicated target \"%s\"", name);
1321 if (valid_iscsi_name(name) == false) {
1322 log_warnx("target name \"%s\" is invalid", name);
1325 targ = calloc(1, sizeof(*targ));
1327 log_err(1, "calloc");
1328 targ->t_name = checked_strdup(name);
1331 * RFC 3722 requires us to normalize the name to lowercase.
1334 for (i = 0; i < len; i++)
1335 targ->t_name[i] = tolower(targ->t_name[i]);
1337 targ->t_conf = conf;
1338 TAILQ_INIT(&targ->t_ports);
1339 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1345 target_delete(struct target *targ)
1347 struct port *port, *tport;
1349 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1351 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1354 free(targ->t_redirection);
1359 target_find(struct conf *conf, const char *name)
1361 struct target *targ;
1363 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1364 if (strcasecmp(targ->t_name, name) == 0)
1372 target_set_redirection(struct target *target, const char *addr)
1375 if (target->t_redirection != NULL) {
1376 log_warnx("cannot set redirection to \"%s\" for "
1377 "target \"%s\"; already defined",
1378 addr, target->t_name);
1382 target->t_redirection = checked_strdup(addr);
1388 lun_new(struct conf *conf, const char *name)
1392 lun = lun_find(conf, name);
1394 log_warnx("duplicated lun \"%s\"", name);
1398 lun = calloc(1, sizeof(*lun));
1400 log_err(1, "calloc");
1402 lun->l_name = checked_strdup(name);
1403 TAILQ_INIT(&lun->l_options);
1404 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1405 lun->l_ctl_lun = -1;
1411 lun_delete(struct lun *lun)
1413 struct target *targ;
1414 struct option *o, *tmp;
1417 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1418 for (i = 0; i < MAX_LUNS; i++) {
1419 if (targ->t_luns[i] == lun)
1420 targ->t_luns[i] = NULL;
1423 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1425 TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1426 option_delete(&lun->l_options, o);
1428 free(lun->l_backend);
1429 free(lun->l_device_id);
1431 free(lun->l_scsiname);
1432 free(lun->l_serial);
1437 lun_find(const struct conf *conf, const char *name)
1441 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1442 if (strcmp(lun->l_name, name) == 0)
1450 lun_set_backend(struct lun *lun, const char *value)
1452 free(lun->l_backend);
1453 lun->l_backend = checked_strdup(value);
1457 lun_set_blocksize(struct lun *lun, size_t value)
1460 lun->l_blocksize = value;
1464 lun_set_device_type(struct lun *lun, uint8_t value)
1467 lun->l_device_type = value;
1471 lun_set_device_id(struct lun *lun, const char *value)
1473 free(lun->l_device_id);
1474 lun->l_device_id = checked_strdup(value);
1478 lun_set_path(struct lun *lun, const char *value)
1481 lun->l_path = checked_strdup(value);
1485 lun_set_scsiname(struct lun *lun, const char *value)
1487 free(lun->l_scsiname);
1488 lun->l_scsiname = checked_strdup(value);
1492 lun_set_serial(struct lun *lun, const char *value)
1494 free(lun->l_serial);
1495 lun->l_serial = checked_strdup(value);
1499 lun_set_size(struct lun *lun, size_t value)
1502 lun->l_size = value;
1506 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1509 lun->l_ctl_lun = value;
1513 option_new(struct options *options, const char *name, const char *value)
1517 o = option_find(options, name);
1519 log_warnx("duplicated option \"%s\"", name);
1523 o = calloc(1, sizeof(*o));
1525 log_err(1, "calloc");
1526 o->o_name = checked_strdup(name);
1527 o->o_value = checked_strdup(value);
1528 TAILQ_INSERT_TAIL(options, o, o_next);
1534 option_delete(struct options *options, struct option *o)
1537 TAILQ_REMOVE(options, o, o_next);
1544 option_find(const struct options *options, const char *name)
1548 TAILQ_FOREACH(o, options, o_next) {
1549 if (strcmp(o->o_name, name) == 0)
1557 option_set(struct option *o, const char *value)
1561 o->o_value = checked_strdup(value);
1564 static struct connection *
1565 connection_new(struct portal *portal, int fd, const char *host,
1566 const struct sockaddr *client_sa)
1568 struct connection *conn;
1570 conn = calloc(1, sizeof(*conn));
1572 log_err(1, "calloc");
1573 conn->conn_portal = portal;
1574 conn->conn_socket = fd;
1575 conn->conn_initiator_addr = checked_strdup(host);
1576 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1579 * Default values, from RFC 3720, section 12.
1581 conn->conn_max_recv_data_segment_length = 8192;
1582 conn->conn_max_burst_length = 262144;
1583 conn->conn_first_burst_length = 65536;
1584 conn->conn_immediate_data = true;
1591 conf_print(struct conf *conf)
1593 struct auth_group *ag;
1595 struct auth_name *auth_name;
1596 struct auth_portal *auth_portal;
1597 struct portal_group *pg;
1598 struct portal *portal;
1599 struct target *targ;
1603 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1604 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1605 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1606 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1607 auth->a_user, auth->a_secret,
1608 auth->a_mutual_user, auth->a_mutual_secret);
1609 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1610 fprintf(stderr, "\t initiator-name %s\n",
1611 auth_name->an_initator_name);
1612 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1613 fprintf(stderr, "\t initiator-portal %s\n",
1614 auth_portal->an_initator_portal);
1615 fprintf(stderr, "}\n");
1617 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1618 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1619 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1620 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1621 fprintf(stderr, "}\n");
1623 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1624 fprintf(stderr, "\tlun %s {\n", lun->l_name);
1625 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1626 TAILQ_FOREACH(o, &lun->l_options, o_next)
1627 fprintf(stderr, "\t\toption %s %s\n",
1628 lo->o_name, lo->o_value);
1629 fprintf(stderr, "\t}\n");
1631 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1632 fprintf(stderr, "target %s {\n", targ->t_name);
1633 if (targ->t_alias != NULL)
1634 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1635 fprintf(stderr, "}\n");
1641 conf_verify_lun(struct lun *lun)
1643 const struct lun *lun2;
1645 if (lun->l_backend == NULL)
1646 lun_set_backend(lun, "block");
1647 if (strcmp(lun->l_backend, "block") == 0) {
1648 if (lun->l_path == NULL) {
1649 log_warnx("missing path for lun \"%s\"",
1653 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1654 if (lun->l_size == 0) {
1655 log_warnx("missing size for ramdisk-backed lun \"%s\"",
1659 if (lun->l_path != NULL) {
1660 log_warnx("path must not be specified "
1661 "for ramdisk-backed lun \"%s\"",
1666 if (lun->l_blocksize == 0) {
1667 if (lun->l_device_type == 5)
1668 lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1670 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1671 } else if (lun->l_blocksize < 0) {
1672 log_warnx("invalid blocksize for lun \"%s\"; "
1673 "must be larger than 0", lun->l_name);
1676 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1677 log_warnx("invalid size for lun \"%s\"; "
1678 "must be multiple of blocksize", lun->l_name);
1681 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1684 if (lun->l_path != NULL && lun2->l_path != NULL &&
1685 strcmp(lun->l_path, lun2->l_path) == 0) {
1686 log_debugx("WARNING: path \"%s\" duplicated "
1687 "between lun \"%s\", and "
1688 "lun \"%s\"", lun->l_path,
1689 lun->l_name, lun2->l_name);
1697 conf_verify(struct conf *conf)
1699 struct auth_group *ag;
1700 struct portal_group *pg;
1702 struct target *targ;
1707 if (conf->conf_pidfile_path == NULL)
1708 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1710 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1711 error = conf_verify_lun(lun);
1715 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1716 if (targ->t_auth_group == NULL) {
1717 targ->t_auth_group = auth_group_find(conf,
1719 assert(targ->t_auth_group != NULL);
1721 if (TAILQ_EMPTY(&targ->t_ports)) {
1722 pg = portal_group_find(conf, "default");
1724 port_new(conf, targ, pg);
1727 for (i = 0; i < MAX_LUNS; i++) {
1728 if (targ->t_luns[i] != NULL)
1731 if (!found && targ->t_redirection == NULL) {
1732 log_warnx("no LUNs defined for target \"%s\"",
1735 if (found && targ->t_redirection != NULL) {
1736 log_debugx("target \"%s\" contains luns, "
1737 " but configured for redirection",
1741 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1742 assert(pg->pg_name != NULL);
1743 if (pg->pg_discovery_auth_group == NULL) {
1744 pg->pg_discovery_auth_group =
1745 auth_group_find(conf, "default");
1746 assert(pg->pg_discovery_auth_group != NULL);
1749 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1750 pg->pg_discovery_filter = PG_FILTER_NONE;
1752 if (pg->pg_redirection != NULL) {
1753 if (!TAILQ_EMPTY(&pg->pg_ports)) {
1754 log_debugx("portal-group \"%s\" assigned "
1755 "to target, but configured "
1759 pg->pg_unassigned = false;
1760 } else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1761 pg->pg_unassigned = false;
1763 if (strcmp(pg->pg_name, "default") != 0)
1764 log_warnx("portal-group \"%s\" not assigned "
1765 "to any target", pg->pg_name);
1766 pg->pg_unassigned = true;
1769 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1770 if (ag->ag_name == NULL)
1771 assert(ag->ag_target != NULL);
1773 assert(ag->ag_target == NULL);
1776 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1777 if (targ->t_auth_group == ag) {
1782 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1783 if (port->p_auth_group == ag) {
1788 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1789 if (pg->pg_discovery_auth_group == ag) {
1794 if (!found && ag->ag_name != NULL &&
1795 strcmp(ag->ag_name, "default") != 0 &&
1796 strcmp(ag->ag_name, "no-authentication") != 0 &&
1797 strcmp(ag->ag_name, "no-access") != 0) {
1798 log_warnx("auth-group \"%s\" not assigned "
1799 "to any target", ag->ag_name);
1807 conf_apply(struct conf *oldconf, struct conf *newconf)
1809 struct lun *oldlun, *newlun, *tmplun;
1810 struct portal_group *oldpg, *newpg;
1811 struct portal *oldp, *newp;
1812 struct port *oldport, *newport, *tmpport;
1813 struct isns *oldns, *newns;
1815 int changed, cumulated_error = 0, error, sockbuf;
1818 if (oldconf->conf_debug != newconf->conf_debug) {
1819 log_debugx("changing debug level to %d", newconf->conf_debug);
1820 log_init(newconf->conf_debug);
1823 if (oldconf->conf_pidfh != NULL) {
1824 assert(oldconf->conf_pidfile_path != NULL);
1825 if (newconf->conf_pidfile_path != NULL &&
1826 strcmp(oldconf->conf_pidfile_path,
1827 newconf->conf_pidfile_path) == 0) {
1828 newconf->conf_pidfh = oldconf->conf_pidfh;
1829 oldconf->conf_pidfh = NULL;
1831 log_debugx("removing pidfile %s",
1832 oldconf->conf_pidfile_path);
1833 pidfile_remove(oldconf->conf_pidfh);
1834 oldconf->conf_pidfh = NULL;
1838 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1839 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1840 newconf->conf_pidfh =
1841 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1842 if (newconf->conf_pidfh == NULL) {
1843 if (errno == EEXIST)
1844 log_errx(1, "daemon already running, pid: %jd.",
1845 (intmax_t)otherpid);
1846 log_err(1, "cannot open or create pidfile \"%s\"",
1847 newconf->conf_pidfile_path);
1852 * Go through the new portal groups, assigning tags or preserving old.
1854 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1855 if (newpg->pg_tag != 0)
1857 oldpg = portal_group_find(oldconf, newpg->pg_name);
1859 newpg->pg_tag = oldpg->pg_tag;
1861 newpg->pg_tag = ++last_portal_group_tag;
1864 /* Deregister on removed iSNS servers. */
1865 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1866 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1867 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1871 isns_deregister(oldns);
1875 * XXX: If target or lun removal fails, we should somehow "move"
1876 * the old lun or target into newconf, so that subsequent
1877 * conf_apply() would try to remove them again. That would
1878 * be somewhat hairy, though, and lun deletion failures don't
1879 * really happen, so leave it as it is for now.
1882 * First, remove any ports present in the old configuration
1883 * and missing in the new one.
1885 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1886 if (oldport->p_foreign)
1888 newport = port_find(newconf, oldport->p_name);
1889 if (newport != NULL && !newport->p_foreign)
1891 log_debugx("removing port \"%s\"", oldport->p_name);
1892 error = kernel_port_remove(oldport);
1894 log_warnx("failed to remove port %s",
1897 * XXX: Uncomment after fixing the root cause.
1899 * cumulated_error++;
1905 * Second, remove any LUNs present in the old configuration
1906 * and missing in the new one.
1908 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1909 newlun = lun_find(newconf, oldlun->l_name);
1910 if (newlun == NULL) {
1911 log_debugx("lun \"%s\", CTL lun %d "
1912 "not found in new configuration; "
1913 "removing", oldlun->l_name, oldlun->l_ctl_lun);
1914 error = kernel_lun_remove(oldlun);
1916 log_warnx("failed to remove lun \"%s\", "
1918 oldlun->l_name, oldlun->l_ctl_lun);
1925 * Also remove the LUNs changed by more than size.
1928 assert(oldlun->l_backend != NULL);
1929 assert(newlun->l_backend != NULL);
1930 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1931 log_debugx("backend for lun \"%s\", "
1932 "CTL lun %d changed; removing",
1933 oldlun->l_name, oldlun->l_ctl_lun);
1936 if (oldlun->l_blocksize != newlun->l_blocksize) {
1937 log_debugx("blocksize for lun \"%s\", "
1938 "CTL lun %d changed; removing",
1939 oldlun->l_name, oldlun->l_ctl_lun);
1942 if (newlun->l_device_id != NULL &&
1943 (oldlun->l_device_id == NULL ||
1944 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1946 log_debugx("device-id for lun \"%s\", "
1947 "CTL lun %d changed; removing",
1948 oldlun->l_name, oldlun->l_ctl_lun);
1951 if (newlun->l_path != NULL &&
1952 (oldlun->l_path == NULL ||
1953 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1954 log_debugx("path for lun \"%s\", "
1955 "CTL lun %d, changed; removing",
1956 oldlun->l_name, oldlun->l_ctl_lun);
1959 if (newlun->l_serial != NULL &&
1960 (oldlun->l_serial == NULL ||
1961 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1962 log_debugx("serial for lun \"%s\", "
1963 "CTL lun %d changed; removing",
1964 oldlun->l_name, oldlun->l_ctl_lun);
1968 error = kernel_lun_remove(oldlun);
1970 log_warnx("failed to remove lun \"%s\", "
1972 oldlun->l_name, oldlun->l_ctl_lun);
1979 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1982 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1983 oldlun = lun_find(oldconf, newlun->l_name);
1984 if (oldlun != NULL) {
1985 log_debugx("modifying lun \"%s\", CTL lun %d",
1986 newlun->l_name, newlun->l_ctl_lun);
1987 error = kernel_lun_modify(newlun);
1989 log_warnx("failed to "
1990 "modify lun \"%s\", CTL lun %d",
1991 newlun->l_name, newlun->l_ctl_lun);
1996 log_debugx("adding lun \"%s\"", newlun->l_name);
1997 error = kernel_lun_add(newlun);
1999 log_warnx("failed to add lun \"%s\"", newlun->l_name);
2006 * Now add new ports or modify existing ones.
2008 TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
2009 if (newport->p_foreign)
2011 oldport = port_find(oldconf, newport->p_name);
2013 if (oldport == NULL || oldport->p_foreign) {
2014 log_debugx("adding port \"%s\"", newport->p_name);
2015 error = kernel_port_add(newport);
2017 log_debugx("updating port \"%s\"", newport->p_name);
2018 newport->p_ctl_port = oldport->p_ctl_port;
2019 error = kernel_port_update(newport, oldport);
2022 log_warnx("failed to %s port %s",
2023 (oldport == NULL) ? "add" : "update",
2026 * XXX: Uncomment after fixing the root cause.
2028 * cumulated_error++;
2034 * Go through the new portals, opening the sockets as necessary.
2036 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2037 if (newpg->pg_foreign)
2039 if (newpg->pg_unassigned) {
2040 log_debugx("not listening on portal-group \"%s\", "
2041 "not assigned to any target",
2045 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2047 * Try to find already open portal and reuse
2048 * the listening socket. We don't care about
2049 * what portal or portal group that was, what
2050 * matters is the listening address.
2052 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2054 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2056 if (strcmp(newp->p_listen,
2057 oldp->p_listen) == 0 &&
2058 oldp->p_socket > 0) {
2066 if (newp->p_socket > 0) {
2068 * We're done with this portal.
2073 #ifdef ICL_KERNEL_PROXY
2075 newpg->pg_conf->conf_portal_id++;
2076 newp->p_id = newpg->pg_conf->conf_portal_id;
2077 log_debugx("listening on %s, portal-group "
2078 "\"%s\", portal id %d, using ICL proxy",
2079 newp->p_listen, newpg->pg_name, newp->p_id);
2080 kernel_listen(newp->p_ai, newp->p_iser,
2085 assert(proxy_mode == false);
2086 assert(newp->p_iser == false);
2088 log_debugx("listening on %s, portal-group \"%s\"",
2089 newp->p_listen, newpg->pg_name);
2090 newp->p_socket = socket(newp->p_ai->ai_family,
2091 newp->p_ai->ai_socktype,
2092 newp->p_ai->ai_protocol);
2093 if (newp->p_socket < 0) {
2094 log_warn("socket(2) failed for %s",
2099 sockbuf = SOCKBUF_SIZE;
2100 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2101 &sockbuf, sizeof(sockbuf)) == -1)
2102 log_warn("setsockopt(SO_RCVBUF) failed "
2103 "for %s", newp->p_listen);
2104 sockbuf = SOCKBUF_SIZE;
2105 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2106 &sockbuf, sizeof(sockbuf)) == -1)
2107 log_warn("setsockopt(SO_SNDBUF) failed "
2108 "for %s", newp->p_listen);
2109 error = setsockopt(newp->p_socket, SOL_SOCKET,
2110 SO_REUSEADDR, &one, sizeof(one));
2112 log_warn("setsockopt(SO_REUSEADDR) failed "
2113 "for %s", newp->p_listen);
2114 close(newp->p_socket);
2119 error = bind(newp->p_socket, newp->p_ai->ai_addr,
2120 newp->p_ai->ai_addrlen);
2122 log_warn("bind(2) failed for %s",
2124 close(newp->p_socket);
2129 error = listen(newp->p_socket, -1);
2131 log_warn("listen(2) failed for %s",
2133 close(newp->p_socket);
2142 * Go through the no longer used sockets, closing them.
2144 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2145 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2146 if (oldp->p_socket <= 0)
2148 log_debugx("closing socket for %s, portal-group \"%s\"",
2149 oldp->p_listen, oldpg->pg_name);
2150 close(oldp->p_socket);
2155 /* (Re-)Register on remaining/new iSNS servers. */
2156 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2157 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2158 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2161 isns_register(newns, oldns);
2164 /* Schedule iSNS update */
2165 if (!TAILQ_EMPTY(&newconf->conf_isns))
2166 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2168 return (cumulated_error);
2175 return (sigalrm_received);
2179 sigalrm_handler_fatal(int dummy __unused)
2182 * It would be easiest to just log an error and exit. We can't
2183 * do this, though, because log_errx() is not signal safe, since
2184 * it calls syslog(3). Instead, set a flag checked by pdu_send()
2185 * and pdu_receive(), to call log_errx() there. Should they fail
2186 * to notice, we'll exit here one second later.
2188 if (sigalrm_received) {
2190 * Oh well. Just give up and quit.
2195 sigalrm_received = true;
2199 sigalrm_handler(int dummy __unused)
2202 sigalrm_received = true;
2206 set_timeout(int timeout, int fatal)
2208 struct sigaction sa;
2209 struct itimerval itv;
2213 log_debugx("session timeout disabled");
2214 bzero(&itv, sizeof(itv));
2215 error = setitimer(ITIMER_REAL, &itv, NULL);
2217 log_err(1, "setitimer");
2218 sigalrm_received = false;
2222 sigalrm_received = false;
2223 bzero(&sa, sizeof(sa));
2225 sa.sa_handler = sigalrm_handler_fatal;
2227 sa.sa_handler = sigalrm_handler;
2228 sigfillset(&sa.sa_mask);
2229 error = sigaction(SIGALRM, &sa, NULL);
2231 log_err(1, "sigaction");
2234 * First SIGALRM will arive after conf_timeout seconds.
2235 * If we do nothing, another one will arrive a second later.
2237 log_debugx("setting session timeout to %d seconds", timeout);
2238 bzero(&itv, sizeof(itv));
2239 itv.it_interval.tv_sec = 1;
2240 itv.it_value.tv_sec = timeout;
2241 error = setitimer(ITIMER_REAL, &itv, NULL);
2243 log_err(1, "setitimer");
2247 wait_for_children(bool block)
2255 * If "block" is true, wait for at least one process.
2257 if (block && num == 0)
2258 pid = wait4(-1, &status, 0, NULL);
2260 pid = wait4(-1, &status, WNOHANG, NULL);
2263 if (WIFSIGNALED(status)) {
2264 log_warnx("child process %d terminated with signal %d",
2265 pid, WTERMSIG(status));
2266 } else if (WEXITSTATUS(status) != 0) {
2267 log_warnx("child process %d terminated with exit status %d",
2268 pid, WEXITSTATUS(status));
2270 log_debugx("child process %d terminated gracefully", pid);
2279 handle_connection(struct portal *portal, int fd,
2280 const struct sockaddr *client_sa, bool dont_fork)
2282 struct connection *conn;
2285 char host[NI_MAXHOST + 1];
2288 conf = portal->p_portal_group->pg_conf;
2291 log_debugx("incoming connection; not forking due to -d flag");
2293 nchildren -= wait_for_children(false);
2294 assert(nchildren >= 0);
2296 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2297 log_debugx("maxproc limit of %d child processes hit; "
2298 "waiting for child process to exit", conf->conf_maxproc);
2299 nchildren -= wait_for_children(true);
2300 assert(nchildren >= 0);
2302 log_debugx("incoming connection; forking child process #%d",
2313 pidfile_close(conf->conf_pidfh);
2315 error = getnameinfo(client_sa, client_sa->sa_len,
2316 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2318 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2320 log_debugx("accepted connection from %s; portal group \"%s\"",
2321 host, portal->p_portal_group->pg_name);
2322 log_set_peer_addr(host);
2323 setproctitle("%s", host);
2325 conn = connection_new(portal, fd, host, client_sa);
2326 set_timeout(conf->conf_timeout, true);
2329 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2330 kernel_handoff(conn);
2331 log_debugx("connection handed off to the kernel");
2333 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2336 log_debugx("nothing more to do; exiting");
2341 fd_add(int fd, fd_set *fdset, int nfds)
2345 * Skip sockets which we failed to bind.
2357 main_loop(struct conf *conf, bool dont_fork)
2359 struct portal_group *pg;
2360 struct portal *portal;
2361 struct sockaddr_storage client_sa;
2362 socklen_t client_salen;
2363 #ifdef ICL_KERNEL_PROXY
2368 int error, nfds, client_fd;
2370 pidfile_write(conf->conf_pidfh);
2373 if (sighup_received || sigterm_received || timed_out())
2376 #ifdef ICL_KERNEL_PROXY
2378 client_salen = sizeof(client_sa);
2379 kernel_accept(&connection_id, &portal_id,
2380 (struct sockaddr *)&client_sa, &client_salen);
2381 assert(client_salen >= client_sa.ss_len);
2383 log_debugx("incoming connection, id %d, portal id %d",
2384 connection_id, portal_id);
2385 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2386 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2387 if (portal->p_id == portal_id) {
2393 log_errx(1, "kernel returned invalid portal_id %d",
2397 handle_connection(portal, connection_id,
2398 (struct sockaddr *)&client_sa, dont_fork);
2401 assert(proxy_mode == false);
2405 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2406 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2407 nfds = fd_add(portal->p_socket, &fdset, nfds);
2409 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2413 log_err(1, "select");
2415 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2416 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2417 if (!FD_ISSET(portal->p_socket, &fdset))
2419 client_salen = sizeof(client_sa);
2420 client_fd = accept(portal->p_socket,
2421 (struct sockaddr *)&client_sa,
2423 if (client_fd < 0) {
2424 if (errno == ECONNABORTED)
2426 log_err(1, "accept");
2428 assert(client_salen >= client_sa.ss_len);
2430 handle_connection(portal, client_fd,
2431 (struct sockaddr *)&client_sa,
2436 #ifdef ICL_KERNEL_PROXY
2443 sighup_handler(int dummy __unused)
2446 sighup_received = true;
2450 sigterm_handler(int dummy __unused)
2453 sigterm_received = true;
2457 sigchld_handler(int dummy __unused)
2461 * The only purpose of this handler is to make SIGCHLD
2462 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2463 * wait_for_children().
2468 register_signals(void)
2470 struct sigaction sa;
2473 bzero(&sa, sizeof(sa));
2474 sa.sa_handler = sighup_handler;
2475 sigfillset(&sa.sa_mask);
2476 error = sigaction(SIGHUP, &sa, NULL);
2478 log_err(1, "sigaction");
2480 sa.sa_handler = sigterm_handler;
2481 error = sigaction(SIGTERM, &sa, NULL);
2483 log_err(1, "sigaction");
2485 sa.sa_handler = sigterm_handler;
2486 error = sigaction(SIGINT, &sa, NULL);
2488 log_err(1, "sigaction");
2490 sa.sa_handler = sigchld_handler;
2491 error = sigaction(SIGCHLD, &sa, NULL);
2493 log_err(1, "sigaction");
2497 check_perms(const char *path)
2502 error = stat(path, &sb);
2507 if (sb.st_mode & S_IWOTH) {
2508 log_warnx("%s is world-writable", path);
2509 } else if (sb.st_mode & S_IROTH) {
2510 log_warnx("%s is world-readable", path);
2511 } else if (sb.st_mode & S_IXOTH) {
2513 * Ok, this one doesn't matter, but still do it,
2514 * just for consistency.
2516 log_warnx("%s is world-executable", path);
2520 * XXX: Should we also check for owner != 0?
2524 static struct conf *
2525 conf_new_from_file(const char *path, struct conf *oldconf, bool ucl)
2528 struct auth_group *ag;
2529 struct portal_group *pg;
2533 log_debugx("obtaining configuration from %s", path);
2537 TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next)
2538 pport_copy(pp, conf);
2540 ag = auth_group_new(conf, "default");
2543 ag = auth_group_new(conf, "no-authentication");
2545 ag->ag_type = AG_TYPE_NO_AUTHENTICATION;
2547 ag = auth_group_new(conf, "no-access");
2549 ag->ag_type = AG_TYPE_DENY;
2551 pg = portal_group_new(conf, "default");
2555 error = uclparse_conf(conf, path);
2557 error = parse_conf(conf, path);
2566 if (conf->conf_default_ag_defined == false) {
2567 log_debugx("auth-group \"default\" not defined; "
2568 "going with defaults");
2569 ag = auth_group_find(conf, "default");
2571 ag->ag_type = AG_TYPE_DENY;
2574 if (conf->conf_default_pg_defined == false) {
2575 log_debugx("portal-group \"default\" not defined; "
2576 "going with defaults");
2577 pg = portal_group_find(conf, "default");
2579 portal_group_add_listen(pg, "0.0.0.0:3260", false);
2580 portal_group_add_listen(pg, "[::]:3260", false);
2583 conf->conf_kernel_port_on = true;
2585 error = conf_verify(conf);
2595 main(int argc, char **argv)
2597 struct conf *oldconf, *newconf, *tmpconf;
2599 const char *config_path = DEFAULT_CONFIG_PATH;
2600 int debug = 0, ch, error;
2601 bool dont_daemonize = false;
2602 bool use_ucl = false;
2604 while ((ch = getopt(argc, argv, "duf:R")) != -1) {
2607 dont_daemonize = true;
2614 config_path = optarg;
2617 #ifndef ICL_KERNEL_PROXY
2618 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2619 "does not support iSER protocol");
2635 oldconf = conf_new_from_kernel();
2636 newconf = conf_new_from_file(config_path, oldconf, use_ucl);
2638 if (newconf == NULL)
2639 log_errx(1, "configuration error; exiting");
2641 oldconf->conf_debug = debug;
2642 newconf->conf_debug = debug;
2645 error = conf_apply(oldconf, newconf);
2647 log_errx(1, "failed to apply configuration; exiting");
2649 conf_delete(oldconf);
2654 if (dont_daemonize == false) {
2655 log_debugx("daemonizing");
2656 if (daemon(0, 0) == -1) {
2657 log_warn("cannot daemonize");
2658 pidfile_remove(newconf->conf_pidfh);
2663 /* Schedule iSNS update */
2664 if (!TAILQ_EMPTY(&newconf->conf_isns))
2665 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2668 main_loop(newconf, dont_daemonize);
2669 if (sighup_received) {
2670 sighup_received = false;
2671 log_debugx("received SIGHUP, reloading configuration");
2672 tmpconf = conf_new_from_file(config_path, newconf,
2675 if (tmpconf == NULL) {
2676 log_warnx("configuration error, "
2677 "continuing with old configuration");
2680 tmpconf->conf_debug = debug;
2683 error = conf_apply(oldconf, newconf);
2685 log_warnx("failed to reload "
2687 conf_delete(oldconf);
2690 } else if (sigterm_received) {
2691 log_debugx("exiting on signal; "
2692 "reloading empty configuration");
2694 log_debugx("removing CTL iSCSI ports "
2695 "and terminating all connections");
2698 newconf = conf_new();
2700 newconf->conf_debug = debug;
2701 error = conf_apply(oldconf, newconf);
2703 log_warnx("failed to apply configuration");
2704 conf_delete(oldconf);
2707 log_warnx("exiting on signal");
2710 nchildren -= wait_for_children(false);
2711 assert(nchildren >= 0);
2713 set_timeout(0, false);
2714 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2716 /* Schedule iSNS update */
2717 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2718 set_timeout((newconf->conf_isns_period