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>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
55 bool proxy_mode = false;
57 static volatile bool sighup_received = false;
58 static volatile bool sigterm_received = false;
59 static volatile bool sigalrm_received = false;
61 static int nchildren = 0;
67 fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
72 checked_strdup(const char *s)
87 conf = calloc(1, sizeof(*conf));
90 TAILQ_INIT(&conf->conf_targets);
91 TAILQ_INIT(&conf->conf_auth_groups);
92 TAILQ_INIT(&conf->conf_portal_groups);
93 TAILQ_INIT(&conf->conf_isns);
95 conf->conf_isns_period = 900;
96 conf->conf_isns_timeout = 5;
98 conf->conf_timeout = 60;
99 conf->conf_maxproc = 30;
105 conf_delete(struct conf *conf)
107 struct target *targ, *tmp;
108 struct auth_group *ag, *cagtmp;
109 struct portal_group *pg, *cpgtmp;
110 struct isns *is, *istmp;
112 assert(conf->conf_pidfh == NULL);
114 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
116 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
117 auth_group_delete(ag);
118 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
119 portal_group_delete(pg);
120 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
122 free(conf->conf_pidfile_path);
127 auth_new(struct auth_group *ag)
131 auth = calloc(1, sizeof(*auth));
133 log_err(1, "calloc");
134 auth->a_auth_group = ag;
135 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
140 auth_delete(struct auth *auth)
142 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
145 free(auth->a_secret);
146 free(auth->a_mutual_user);
147 free(auth->a_mutual_secret);
152 auth_find(const struct auth_group *ag, const char *user)
154 const struct auth *auth;
156 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
157 if (strcmp(auth->a_user, user) == 0)
165 auth_check_secret_length(struct auth *auth)
169 len = strlen(auth->a_secret);
171 if (auth->a_auth_group->ag_name != NULL)
172 log_warnx("secret for user \"%s\", auth-group \"%s\", "
173 "is too long; it should be at most 16 characters "
174 "long", auth->a_user, auth->a_auth_group->ag_name);
176 log_warnx("secret for user \"%s\", target \"%s\", "
177 "is too long; it should be at most 16 characters "
178 "long", auth->a_user,
179 auth->a_auth_group->ag_target->t_name);
182 if (auth->a_auth_group->ag_name != NULL)
183 log_warnx("secret for user \"%s\", auth-group \"%s\", "
184 "is too short; it should be at least 12 characters "
185 "long", auth->a_user,
186 auth->a_auth_group->ag_name);
188 log_warnx("secret for user \"%s\", target \"%s\", "
189 "is too short; it should be at least 16 characters "
190 "long", auth->a_user,
191 auth->a_auth_group->ag_target->t_name);
194 if (auth->a_mutual_secret != NULL) {
195 len = strlen(auth->a_secret);
197 if (auth->a_auth_group->ag_name != NULL)
198 log_warnx("mutual secret for user \"%s\", "
199 "auth-group \"%s\", is too long; it should "
200 "be at most 16 characters long",
201 auth->a_user, auth->a_auth_group->ag_name);
203 log_warnx("mutual secret for user \"%s\", "
204 "target \"%s\", is too long; it should "
205 "be at most 16 characters long",
207 auth->a_auth_group->ag_target->t_name);
210 if (auth->a_auth_group->ag_name != NULL)
211 log_warnx("mutual secret for user \"%s\", "
212 "auth-group \"%s\", is too short; it "
213 "should be at least 12 characters long",
214 auth->a_user, auth->a_auth_group->ag_name);
216 log_warnx("mutual secret for user \"%s\", "
217 "target \"%s\", is too short; it should be "
218 "at least 16 characters long",
220 auth->a_auth_group->ag_target->t_name);
226 auth_new_chap(struct auth_group *ag, const char *user,
231 if (ag->ag_type == AG_TYPE_UNKNOWN)
232 ag->ag_type = AG_TYPE_CHAP;
233 if (ag->ag_type != AG_TYPE_CHAP) {
234 if (ag->ag_name != NULL)
235 log_warnx("cannot mix \"chap\" authentication with "
236 "other types for auth-group \"%s\"", ag->ag_name);
238 log_warnx("cannot mix \"chap\" authentication with "
239 "other types for target \"%s\"",
240 ag->ag_target->t_name);
245 auth->a_user = checked_strdup(user);
246 auth->a_secret = checked_strdup(secret);
248 auth_check_secret_length(auth);
254 auth_new_chap_mutual(struct auth_group *ag, const char *user,
255 const char *secret, const char *user2, const char *secret2)
259 if (ag->ag_type == AG_TYPE_UNKNOWN)
260 ag->ag_type = AG_TYPE_CHAP_MUTUAL;
261 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
262 if (ag->ag_name != NULL)
263 log_warnx("cannot mix \"chap-mutual\" authentication "
264 "with other types for auth-group \"%s\"",
267 log_warnx("cannot mix \"chap-mutual\" authentication "
268 "with other types for target \"%s\"",
269 ag->ag_target->t_name);
274 auth->a_user = checked_strdup(user);
275 auth->a_secret = checked_strdup(secret);
276 auth->a_mutual_user = checked_strdup(user2);
277 auth->a_mutual_secret = checked_strdup(secret2);
279 auth_check_secret_length(auth);
284 const struct auth_name *
285 auth_name_new(struct auth_group *ag, const char *name)
287 struct auth_name *an;
289 an = calloc(1, sizeof(*an));
291 log_err(1, "calloc");
292 an->an_auth_group = ag;
293 an->an_initator_name = checked_strdup(name);
294 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
299 auth_name_delete(struct auth_name *an)
301 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
303 free(an->an_initator_name);
308 auth_name_defined(const struct auth_group *ag)
310 if (TAILQ_EMPTY(&ag->ag_names))
315 const struct auth_name *
316 auth_name_find(const struct auth_group *ag, const char *name)
318 const struct auth_name *auth_name;
320 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
321 if (strcmp(auth_name->an_initator_name, name) == 0)
328 const struct auth_portal *
329 auth_portal_new(struct auth_group *ag, const char *portal)
331 struct auth_portal *ap;
332 char *net, *mask, *str, *tmp;
335 ap = calloc(1, sizeof(*ap));
337 log_err(1, "calloc");
338 ap->ap_auth_group = ag;
339 ap->ap_initator_portal = checked_strdup(portal);
340 mask = str = checked_strdup(portal);
341 net = strsep(&mask, "/");
347 if (net[len - 1] == ']')
349 if (strchr(net, ':') != NULL) {
350 struct sockaddr_in6 *sin6 =
351 (struct sockaddr_in6 *)&ap->ap_sa;
353 sin6->sin6_len = sizeof(*sin6);
354 sin6->sin6_family = AF_INET6;
355 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
359 struct sockaddr_in *sin =
360 (struct sockaddr_in *)&ap->ap_sa;
362 sin->sin_len = sizeof(*sin);
363 sin->sin_family = AF_INET;
364 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
369 m = strtol(mask, &tmp, 0);
370 if (m < 0 || m > dm || tmp[0] != 0)
376 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
380 log_errx(1, "Incorrect initiator portal '%s'", portal);
385 auth_portal_delete(struct auth_portal *ap)
387 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
389 free(ap->ap_initator_portal);
394 auth_portal_defined(const struct auth_group *ag)
396 if (TAILQ_EMPTY(&ag->ag_portals))
401 const struct auth_portal *
402 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
404 const struct auth_portal *ap;
405 const uint8_t *a, *b;
409 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
410 if (ap->ap_sa.ss_family != ss->ss_family)
412 if (ss->ss_family == AF_INET) {
413 a = (const uint8_t *)
414 &((const struct sockaddr_in *)ss)->sin_addr;
415 b = (const uint8_t *)
416 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
418 a = (const uint8_t *)
419 &((const struct sockaddr_in6 *)ss)->sin6_addr;
420 b = (const uint8_t *)
421 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
423 for (i = 0; i < ap->ap_mask / 8; i++) {
427 if (ap->ap_mask % 8) {
428 bmask = 0xff << (8 - (ap->ap_mask % 8));
429 if ((a[i] & bmask) != (b[i] & bmask))
441 auth_group_new(struct conf *conf, const char *name)
443 struct auth_group *ag;
446 ag = auth_group_find(conf, name);
448 log_warnx("duplicated auth-group \"%s\"", name);
453 ag = calloc(1, sizeof(*ag));
455 log_err(1, "calloc");
457 ag->ag_name = checked_strdup(name);
458 TAILQ_INIT(&ag->ag_auths);
459 TAILQ_INIT(&ag->ag_names);
460 TAILQ_INIT(&ag->ag_portals);
462 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
468 auth_group_delete(struct auth_group *ag)
470 struct auth *auth, *auth_tmp;
471 struct auth_name *auth_name, *auth_name_tmp;
472 struct auth_portal *auth_portal, *auth_portal_tmp;
474 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
476 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
478 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
479 auth_name_delete(auth_name);
480 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
482 auth_portal_delete(auth_portal);
488 auth_group_find(const struct conf *conf, const char *name)
490 struct auth_group *ag;
492 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
493 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
501 auth_group_set_type(struct auth_group *ag, int type)
504 if (ag->ag_type == AG_TYPE_UNKNOWN) {
509 if (ag->ag_type == type)
516 auth_group_set_type_str(struct auth_group *ag, const char *str)
520 if (strcmp(str, "none") == 0) {
521 type = AG_TYPE_NO_AUTHENTICATION;
522 } else if (strcmp(str, "deny") == 0) {
524 } else if (strcmp(str, "chap") == 0) {
526 } else if (strcmp(str, "chap-mutual") == 0) {
527 type = AG_TYPE_CHAP_MUTUAL;
529 if (ag->ag_name != NULL)
530 log_warnx("invalid auth-type \"%s\" for auth-group "
531 "\"%s\"", str, ag->ag_name);
533 log_warnx("invalid auth-type \"%s\" for target "
534 "\"%s\"", str, ag->ag_target->t_name);
538 error = auth_group_set_type(ag, type);
540 if (ag->ag_name != NULL)
541 log_warnx("cannot set auth-type to \"%s\" for "
542 "auth-group \"%s\"; already has a different "
543 "type", str, ag->ag_name);
545 log_warnx("cannot set auth-type to \"%s\" for target "
546 "\"%s\"; already has a different type",
547 str, ag->ag_target->t_name);
554 static struct portal *
555 portal_new(struct portal_group *pg)
557 struct portal *portal;
559 portal = calloc(1, sizeof(*portal));
561 log_err(1, "calloc");
562 TAILQ_INIT(&portal->p_targets);
563 portal->p_portal_group = pg;
564 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
569 portal_delete(struct portal *portal)
572 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
573 if (portal->p_ai != NULL)
574 freeaddrinfo(portal->p_ai);
575 free(portal->p_listen);
579 struct portal_group *
580 portal_group_new(struct conf *conf, const char *name)
582 struct portal_group *pg;
584 pg = portal_group_find(conf, name);
586 log_warnx("duplicated portal-group \"%s\"", name);
590 pg = calloc(1, sizeof(*pg));
592 log_err(1, "calloc");
593 pg->pg_name = checked_strdup(name);
594 TAILQ_INIT(&pg->pg_portals);
596 conf->conf_last_portal_group_tag++;
597 pg->pg_tag = conf->conf_last_portal_group_tag;
598 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
604 portal_group_delete(struct portal_group *pg)
606 struct portal *portal, *tmp;
608 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
610 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
611 portal_delete(portal);
616 struct portal_group *
617 portal_group_find(const struct conf *conf, const char *name)
619 struct portal_group *pg;
621 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
622 if (strcmp(pg->pg_name, name) == 0)
630 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
632 struct addrinfo hints;
635 int error, colons = 0;
639 * IPv6 address in square brackets, perhaps with port.
642 addr = strsep(&arg, "]");
645 if (arg[0] == '\0') {
647 } else if (arg[0] == ':') {
653 * Either IPv6 address without brackets - and without
654 * a port - or IPv4 address. Just count the colons.
656 for (ch = arg; *ch != '\0'; ch++) {
664 addr = strsep(&arg, ":");
672 memset(&hints, 0, sizeof(hints));
673 hints.ai_family = PF_UNSPEC;
674 hints.ai_socktype = SOCK_STREAM;
675 hints.ai_flags = AI_PASSIVE;
676 error = getaddrinfo(addr, port, &hints, ai);
683 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
685 struct portal *portal;
687 portal = portal_new(pg);
688 portal->p_listen = checked_strdup(value);
689 portal->p_iser = iser;
691 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
692 log_warnx("invalid listen address %s", portal->p_listen);
693 portal_delete(portal);
698 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
699 * those into multiple portals.
706 isns_new(struct conf *conf, const char *addr)
710 isns = calloc(1, sizeof(*isns));
712 log_err(1, "calloc");
714 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
715 isns->i_addr = checked_strdup(addr);
717 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
718 log_warnx("invalid iSNS address %s", isns->i_addr);
724 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
725 * those into multiple servers.
732 isns_delete(struct isns *isns)
735 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
737 if (isns->i_ai != NULL)
738 freeaddrinfo(isns->i_ai);
743 isns_do_connect(struct isns *isns)
747 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
748 isns->i_ai->ai_protocol);
750 log_warn("socket(2) failed for %s", isns->i_addr);
753 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
754 log_warn("connect(2) failed for %s", isns->i_addr);
762 isns_do_register(struct isns *isns, int s, const char *hostname)
764 struct conf *conf = isns->i_conf;
765 struct target *target;
766 struct portal *portal;
767 struct portal_group *pg;
768 struct isns_req *req;
772 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
773 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
774 isns_req_add_delim(req);
775 isns_req_add_str(req, 1, hostname);
776 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
777 isns_req_add_32(req, 6, conf->conf_isns_period);
778 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
779 if (pg->pg_unassigned)
781 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
782 isns_req_add_addr(req, 16, portal->p_ai);
783 isns_req_add_port(req, 17, portal->p_ai);
786 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
787 isns_req_add_str(req, 32, target->t_name);
788 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
789 if (target->t_alias != NULL)
790 isns_req_add_str(req, 34, target->t_alias);
791 pg = target->t_portal_group;
792 isns_req_add_32(req, 51, pg->pg_tag);
793 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
794 isns_req_add_addr(req, 49, portal->p_ai);
795 isns_req_add_port(req, 50, portal->p_ai);
798 res = isns_req_send(s, req);
800 log_warn("send(2) failed for %s", isns->i_addr);
803 res = isns_req_receive(s, req);
805 log_warn("receive(2) failed for %s", isns->i_addr);
808 error = isns_req_get_status(req);
810 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
819 isns_do_check(struct isns *isns, int s, const char *hostname)
821 struct conf *conf = isns->i_conf;
822 struct isns_req *req;
826 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
827 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
828 isns_req_add_str(req, 1, hostname);
829 isns_req_add_delim(req);
830 isns_req_add(req, 2, 0, NULL);
831 res = isns_req_send(s, req);
833 log_warn("send(2) failed for %s", isns->i_addr);
836 res = isns_req_receive(s, req);
838 log_warn("receive(2) failed for %s", isns->i_addr);
841 error = isns_req_get_status(req);
843 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
852 isns_do_deregister(struct isns *isns, int s, const char *hostname)
854 struct conf *conf = isns->i_conf;
855 struct isns_req *req;
859 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
860 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
861 isns_req_add_delim(req);
862 isns_req_add_str(req, 1, hostname);
863 res = isns_req_send(s, req);
865 log_warn("send(2) failed for %s", isns->i_addr);
868 res = isns_req_receive(s, req);
870 log_warn("receive(2) failed for %s", isns->i_addr);
873 error = isns_req_get_status(req);
875 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
884 isns_register(struct isns *isns, struct isns *oldisns)
886 struct conf *conf = isns->i_conf;
890 if (TAILQ_EMPTY(&conf->conf_targets) ||
891 TAILQ_EMPTY(&conf->conf_portal_groups))
893 set_timeout(conf->conf_isns_timeout, false);
894 s = isns_do_connect(isns);
896 set_timeout(0, false);
899 gethostname(hostname, sizeof(hostname));
901 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
903 res = isns_do_deregister(oldisns, s, hostname);
904 res = isns_do_register(isns, s, hostname);
906 set_timeout(0, false);
910 isns_check(struct isns *isns)
912 struct conf *conf = isns->i_conf;
916 if (TAILQ_EMPTY(&conf->conf_targets) ||
917 TAILQ_EMPTY(&conf->conf_portal_groups))
919 set_timeout(conf->conf_isns_timeout, false);
920 s = isns_do_connect(isns);
922 set_timeout(0, false);
925 gethostname(hostname, sizeof(hostname));
927 res = isns_do_check(isns, s, hostname);
929 res = isns_do_deregister(isns, s, hostname);
930 res = isns_do_register(isns, s, hostname);
933 set_timeout(0, false);
937 isns_deregister(struct isns *isns)
939 struct conf *conf = isns->i_conf;
943 if (TAILQ_EMPTY(&conf->conf_targets) ||
944 TAILQ_EMPTY(&conf->conf_portal_groups))
946 set_timeout(conf->conf_isns_timeout, false);
947 s = isns_do_connect(isns);
950 gethostname(hostname, sizeof(hostname));
952 res = isns_do_deregister(isns, s, hostname);
954 set_timeout(0, false);
958 valid_hex(const char ch)
990 valid_iscsi_name(const char *name)
994 if (strlen(name) >= MAX_NAME_LEN) {
995 log_warnx("overlong name for target \"%s\"; max length allowed "
996 "by iSCSI specification is %d characters",
1002 * In the cases below, we don't return an error, just in case the admin
1003 * was right, and we're wrong.
1005 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1006 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1008 * XXX: We should verify UTF-8 normalisation, as defined
1009 * by 3.2.6.2: iSCSI Name Encoding.
1011 if (isalnum(name[i]))
1013 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1015 log_warnx("invalid character \"%c\" in target name "
1016 "\"%s\"; allowed characters are letters, digits, "
1017 "'-', '.', and ':'", name[i], name);
1021 * XXX: Check more stuff: valid date and a valid reversed domain.
1023 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1024 if (strlen(name) != strlen("eui.") + 16)
1025 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1026 "should be followed by exactly 16 hexadecimal "
1028 for (i = strlen("eui."); name[i] != '\0'; i++) {
1029 if (!valid_hex(name[i])) {
1030 log_warnx("invalid character \"%c\" in target "
1031 "name \"%s\"; allowed characters are 1-9 "
1032 "and A-F", name[i], name);
1036 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1037 if (strlen(name) > strlen("naa.") + 32)
1038 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1039 "should be followed by at most 32 hexadecimal "
1041 for (i = strlen("naa."); name[i] != '\0'; i++) {
1042 if (!valid_hex(name[i])) {
1043 log_warnx("invalid character \"%c\" in target "
1044 "name \"%s\"; allowed characters are 1-9 "
1045 "and A-F", name[i], name);
1050 log_warnx("invalid target name \"%s\"; should start with "
1051 "either \".iqn\", \"eui.\", or \"naa.\"",
1058 target_new(struct conf *conf, const char *name)
1060 struct target *targ;
1063 targ = target_find(conf, name);
1065 log_warnx("duplicated target \"%s\"", name);
1068 if (valid_iscsi_name(name) == false) {
1069 log_warnx("target name \"%s\" is invalid", name);
1072 targ = calloc(1, sizeof(*targ));
1074 log_err(1, "calloc");
1075 targ->t_name = checked_strdup(name);
1078 * RFC 3722 requires us to normalize the name to lowercase.
1081 for (i = 0; i < len; i++)
1082 targ->t_name[i] = tolower(targ->t_name[i]);
1084 TAILQ_INIT(&targ->t_luns);
1085 targ->t_conf = conf;
1086 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1092 target_delete(struct target *targ)
1094 struct lun *lun, *tmp;
1096 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1098 TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
1105 target_find(struct conf *conf, const char *name)
1107 struct target *targ;
1109 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1110 if (strcasecmp(targ->t_name, name) == 0)
1118 lun_new(struct target *targ, int lun_id)
1122 lun = lun_find(targ, lun_id);
1124 log_warnx("duplicated lun %d for target \"%s\"",
1125 lun_id, targ->t_name);
1129 lun = calloc(1, sizeof(*lun));
1131 log_err(1, "calloc");
1132 lun->l_lun = lun_id;
1133 TAILQ_INIT(&lun->l_options);
1134 lun->l_target = targ;
1135 TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
1141 lun_delete(struct lun *lun)
1143 struct lun_option *lo, *tmp;
1145 TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
1147 TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
1148 lun_option_delete(lo);
1149 free(lun->l_backend);
1150 free(lun->l_device_id);
1152 free(lun->l_serial);
1157 lun_find(const struct target *targ, int lun_id)
1161 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1162 if (lun->l_lun == lun_id)
1170 lun_set_backend(struct lun *lun, const char *value)
1172 free(lun->l_backend);
1173 lun->l_backend = checked_strdup(value);
1177 lun_set_blocksize(struct lun *lun, size_t value)
1180 lun->l_blocksize = value;
1184 lun_set_device_id(struct lun *lun, const char *value)
1186 free(lun->l_device_id);
1187 lun->l_device_id = checked_strdup(value);
1191 lun_set_path(struct lun *lun, const char *value)
1194 lun->l_path = checked_strdup(value);
1198 lun_set_serial(struct lun *lun, const char *value)
1200 free(lun->l_serial);
1201 lun->l_serial = checked_strdup(value);
1205 lun_set_size(struct lun *lun, size_t value)
1208 lun->l_size = value;
1212 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1215 lun->l_ctl_lun = value;
1219 lun_option_new(struct lun *lun, const char *name, const char *value)
1221 struct lun_option *lo;
1223 lo = lun_option_find(lun, name);
1225 log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
1226 name, lun->l_lun, lun->l_target->t_name);
1230 lo = calloc(1, sizeof(*lo));
1232 log_err(1, "calloc");
1233 lo->lo_name = checked_strdup(name);
1234 lo->lo_value = checked_strdup(value);
1236 TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
1242 lun_option_delete(struct lun_option *lo)
1245 TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
1253 lun_option_find(const struct lun *lun, const char *name)
1255 struct lun_option *lo;
1257 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
1258 if (strcmp(lo->lo_name, name) == 0)
1266 lun_option_set(struct lun_option *lo, const char *value)
1270 lo->lo_value = checked_strdup(value);
1273 static struct connection *
1274 connection_new(struct portal *portal, int fd, const char *host,
1275 const struct sockaddr *client_sa)
1277 struct connection *conn;
1279 conn = calloc(1, sizeof(*conn));
1281 log_err(1, "calloc");
1282 conn->conn_portal = portal;
1283 conn->conn_socket = fd;
1284 conn->conn_initiator_addr = checked_strdup(host);
1285 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1288 * Default values, from RFC 3720, section 12.
1290 conn->conn_max_data_segment_length = 8192;
1291 conn->conn_max_burst_length = 262144;
1292 conn->conn_immediate_data = true;
1299 conf_print(struct conf *conf)
1301 struct auth_group *ag;
1303 struct auth_name *auth_name;
1304 struct auth_portal *auth_portal;
1305 struct portal_group *pg;
1306 struct portal *portal;
1307 struct target *targ;
1309 struct lun_option *lo;
1311 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1312 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1313 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1314 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1315 auth->a_user, auth->a_secret,
1316 auth->a_mutual_user, auth->a_mutual_secret);
1317 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1318 fprintf(stderr, "\t initiator-name %s\n",
1319 auth_name->an_initator_name);
1320 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1321 fprintf(stderr, "\t initiator-portal %s\n",
1322 auth_portal->an_initator_portal);
1323 fprintf(stderr, "}\n");
1325 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1326 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1327 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1328 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1329 fprintf(stderr, "}\n");
1331 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1332 fprintf(stderr, "target %s {\n", targ->t_name);
1333 if (targ->t_alias != NULL)
1334 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1335 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1336 fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1337 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1338 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1339 fprintf(stderr, "\t\toption %s %s\n",
1340 lo->lo_name, lo->lo_value);
1341 fprintf(stderr, "\t}\n");
1343 fprintf(stderr, "}\n");
1349 conf_verify_lun(struct lun *lun)
1351 const struct lun *lun2;
1352 const struct target *targ2;
1354 if (lun->l_backend == NULL)
1355 lun_set_backend(lun, "block");
1356 if (strcmp(lun->l_backend, "block") == 0) {
1357 if (lun->l_path == NULL) {
1358 log_warnx("missing path for lun %d, target \"%s\"",
1359 lun->l_lun, lun->l_target->t_name);
1362 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1363 if (lun->l_size == 0) {
1364 log_warnx("missing size for ramdisk-backed lun %d, "
1365 "target \"%s\"", lun->l_lun, lun->l_target->t_name);
1368 if (lun->l_path != NULL) {
1369 log_warnx("path must not be specified "
1370 "for ramdisk-backed lun %d, target \"%s\"",
1371 lun->l_lun, lun->l_target->t_name);
1375 if (lun->l_lun < 0 || lun->l_lun > 255) {
1376 log_warnx("invalid lun number for lun %d, target \"%s\"; "
1377 "must be between 0 and 255", lun->l_lun,
1378 lun->l_target->t_name);
1381 if (lun->l_blocksize == 0) {
1382 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1383 } else if (lun->l_blocksize < 0) {
1384 log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1385 "must be larger than 0", lun->l_lun, lun->l_target->t_name);
1388 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1389 log_warnx("invalid size for lun %d, target \"%s\"; "
1390 "must be multiple of blocksize", lun->l_lun,
1391 lun->l_target->t_name);
1394 TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1395 TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1398 if (lun->l_path != NULL && lun2->l_path != NULL &&
1399 strcmp(lun->l_path, lun2->l_path) == 0) {
1400 log_debugx("WARNING: path \"%s\" duplicated "
1401 "between lun %d, target \"%s\", and "
1402 "lun %d, target \"%s\"", lun->l_path,
1403 lun->l_lun, lun->l_target->t_name,
1404 lun2->l_lun, lun2->l_target->t_name);
1413 conf_verify(struct conf *conf)
1415 struct auth_group *ag;
1416 struct portal_group *pg;
1417 struct target *targ;
1422 if (conf->conf_pidfile_path == NULL)
1423 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1425 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1426 if (targ->t_auth_group == NULL) {
1427 targ->t_auth_group = auth_group_find(conf,
1429 assert(targ->t_auth_group != NULL);
1431 if (targ->t_portal_group == NULL) {
1432 targ->t_portal_group = portal_group_find(conf,
1434 assert(targ->t_portal_group != NULL);
1437 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1438 error = conf_verify_lun(lun);
1444 log_warnx("no LUNs defined for target \"%s\"",
1448 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1449 assert(pg->pg_name != NULL);
1450 if (pg->pg_discovery_auth_group == NULL) {
1451 pg->pg_discovery_auth_group =
1452 auth_group_find(conf, "default");
1453 assert(pg->pg_discovery_auth_group != NULL);
1456 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1457 if (targ->t_portal_group == pg)
1461 if (strcmp(pg->pg_name, "default") != 0)
1462 log_warnx("portal-group \"%s\" not assigned "
1463 "to any target", pg->pg_name);
1464 pg->pg_unassigned = true;
1466 pg->pg_unassigned = false;
1468 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1469 if (ag->ag_name == NULL)
1470 assert(ag->ag_target != NULL);
1472 assert(ag->ag_target == NULL);
1475 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1476 if (targ->t_auth_group == ag) {
1481 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1482 if (pg->pg_discovery_auth_group == ag) {
1487 if (!found && ag->ag_name != NULL &&
1488 strcmp(ag->ag_name, "default") != 0 &&
1489 strcmp(ag->ag_name, "no-authentication") != 0 &&
1490 strcmp(ag->ag_name, "no-access") != 0) {
1491 log_warnx("auth-group \"%s\" not assigned "
1492 "to any target", ag->ag_name);
1500 conf_apply(struct conf *oldconf, struct conf *newconf)
1502 struct target *oldtarg, *newtarg, *tmptarg;
1503 struct lun *oldlun, *newlun, *tmplun;
1504 struct portal_group *oldpg, *newpg;
1505 struct portal *oldp, *newp;
1506 struct isns *oldns, *newns;
1508 int changed, cumulated_error = 0, error;
1511 if (oldconf->conf_debug != newconf->conf_debug) {
1512 log_debugx("changing debug level to %d", newconf->conf_debug);
1513 log_init(newconf->conf_debug);
1516 if (oldconf->conf_pidfh != NULL) {
1517 assert(oldconf->conf_pidfile_path != NULL);
1518 if (newconf->conf_pidfile_path != NULL &&
1519 strcmp(oldconf->conf_pidfile_path,
1520 newconf->conf_pidfile_path) == 0) {
1521 newconf->conf_pidfh = oldconf->conf_pidfh;
1522 oldconf->conf_pidfh = NULL;
1524 log_debugx("removing pidfile %s",
1525 oldconf->conf_pidfile_path);
1526 pidfile_remove(oldconf->conf_pidfh);
1527 oldconf->conf_pidfh = NULL;
1531 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1532 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1533 newconf->conf_pidfh =
1534 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1535 if (newconf->conf_pidfh == NULL) {
1536 if (errno == EEXIST)
1537 log_errx(1, "daemon already running, pid: %jd.",
1538 (intmax_t)otherpid);
1539 log_err(1, "cannot open or create pidfile \"%s\"",
1540 newconf->conf_pidfile_path);
1544 /* Deregister on removed iSNS servers. */
1545 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1546 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1547 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1551 isns_deregister(oldns);
1555 * XXX: If target or lun removal fails, we should somehow "move"
1556 * the old lun or target into newconf, so that subsequent
1557 * conf_apply() would try to remove them again. That would
1558 * be somewhat hairy, though, and lun deletion failures don't
1559 * really happen, so leave it as it is for now.
1561 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1563 * First, remove any targets present in the old configuration
1564 * and missing in the new one.
1566 newtarg = target_find(newconf, oldtarg->t_name);
1567 if (newtarg == NULL) {
1568 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1570 log_debugx("target %s not found in new "
1571 "configuration; removing its lun %d, "
1572 "backed by CTL lun %d",
1573 oldtarg->t_name, oldlun->l_lun,
1575 error = kernel_lun_remove(oldlun);
1577 log_warnx("failed to remove lun %d, "
1578 "target %s, CTL lun %d",
1579 oldlun->l_lun, oldtarg->t_name,
1584 kernel_port_remove(oldtarg);
1589 * Second, remove any LUNs present in the old target
1590 * and missing in the new one.
1592 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1593 newlun = lun_find(newtarg, oldlun->l_lun);
1594 if (newlun == NULL) {
1595 log_debugx("lun %d, target %s, CTL lun %d "
1596 "not found in new configuration; "
1597 "removing", oldlun->l_lun, oldtarg->t_name,
1599 error = kernel_lun_remove(oldlun);
1601 log_warnx("failed to remove lun %d, "
1602 "target %s, CTL lun %d",
1603 oldlun->l_lun, oldtarg->t_name,
1611 * Also remove the LUNs changed by more than size.
1614 assert(oldlun->l_backend != NULL);
1615 assert(newlun->l_backend != NULL);
1616 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1617 log_debugx("backend for lun %d, target %s, "
1618 "CTL lun %d changed; removing",
1619 oldlun->l_lun, oldtarg->t_name,
1623 if (oldlun->l_blocksize != newlun->l_blocksize) {
1624 log_debugx("blocksize for lun %d, target %s, "
1625 "CTL lun %d changed; removing",
1626 oldlun->l_lun, oldtarg->t_name,
1630 if (newlun->l_device_id != NULL &&
1631 (oldlun->l_device_id == NULL ||
1632 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1634 log_debugx("device-id for lun %d, target %s, "
1635 "CTL lun %d changed; removing",
1636 oldlun->l_lun, oldtarg->t_name,
1640 if (newlun->l_path != NULL &&
1641 (oldlun->l_path == NULL ||
1642 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1643 log_debugx("path for lun %d, target %s, "
1644 "CTL lun %d, changed; removing",
1645 oldlun->l_lun, oldtarg->t_name,
1649 if (newlun->l_serial != NULL &&
1650 (oldlun->l_serial == NULL ||
1651 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1652 log_debugx("serial for lun %d, target %s, "
1653 "CTL lun %d changed; removing",
1654 oldlun->l_lun, oldtarg->t_name,
1659 error = kernel_lun_remove(oldlun);
1661 log_warnx("failed to remove lun %d, "
1662 "target %s, CTL lun %d",
1663 oldlun->l_lun, oldtarg->t_name,
1671 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1676 * Now add new targets or modify existing ones.
1678 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1679 oldtarg = target_find(oldconf, newtarg->t_name);
1681 TAILQ_FOREACH_SAFE(newlun, &newtarg->t_luns, l_next, tmplun) {
1682 if (oldtarg != NULL) {
1683 oldlun = lun_find(oldtarg, newlun->l_lun);
1684 if (oldlun != NULL) {
1685 if (newlun->l_size != oldlun->l_size ||
1686 newlun->l_size == 0) {
1687 log_debugx("resizing lun %d, "
1688 "target %s, CTL lun %d",
1693 kernel_lun_resize(newlun);
1695 log_warnx("failed to "
1708 log_debugx("adding lun %d, target %s",
1709 newlun->l_lun, newtarg->t_name);
1710 error = kernel_lun_add(newlun);
1712 log_warnx("failed to add lun %d, target %s",
1713 newlun->l_lun, newtarg->t_name);
1718 if (oldtarg == NULL)
1719 kernel_port_add(newtarg);
1723 * Go through the new portals, opening the sockets as neccessary.
1725 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1726 if (newpg->pg_unassigned) {
1727 log_debugx("not listening on portal-group \"%s\", "
1728 "not assigned to any target",
1732 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1734 * Try to find already open portal and reuse
1735 * the listening socket. We don't care about
1736 * what portal or portal group that was, what
1737 * matters is the listening address.
1739 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1741 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1743 if (strcmp(newp->p_listen,
1744 oldp->p_listen) == 0 &&
1745 oldp->p_socket > 0) {
1753 if (newp->p_socket > 0) {
1755 * We're done with this portal.
1760 #ifdef ICL_KERNEL_PROXY
1762 newpg->pg_conf->conf_portal_id++;
1763 newp->p_id = newpg->pg_conf->conf_portal_id;
1764 log_debugx("listening on %s, portal-group "
1765 "\"%s\", portal id %d, using ICL proxy",
1766 newp->p_listen, newpg->pg_name, newp->p_id);
1767 kernel_listen(newp->p_ai, newp->p_iser,
1772 assert(proxy_mode == false);
1773 assert(newp->p_iser == false);
1775 log_debugx("listening on %s, portal-group \"%s\"",
1776 newp->p_listen, newpg->pg_name);
1777 newp->p_socket = socket(newp->p_ai->ai_family,
1778 newp->p_ai->ai_socktype,
1779 newp->p_ai->ai_protocol);
1780 if (newp->p_socket < 0) {
1781 log_warn("socket(2) failed for %s",
1786 error = setsockopt(newp->p_socket, SOL_SOCKET,
1787 SO_REUSEADDR, &one, sizeof(one));
1789 log_warn("setsockopt(SO_REUSEADDR) failed "
1790 "for %s", newp->p_listen);
1791 close(newp->p_socket);
1796 error = bind(newp->p_socket, newp->p_ai->ai_addr,
1797 newp->p_ai->ai_addrlen);
1799 log_warn("bind(2) failed for %s",
1801 close(newp->p_socket);
1806 error = listen(newp->p_socket, -1);
1808 log_warn("listen(2) failed for %s",
1810 close(newp->p_socket);
1819 * Go through the no longer used sockets, closing them.
1821 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1822 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1823 if (oldp->p_socket <= 0)
1825 log_debugx("closing socket for %s, portal-group \"%s\"",
1826 oldp->p_listen, oldpg->pg_name);
1827 close(oldp->p_socket);
1832 /* (Re-)Register on remaining/new iSNS servers. */
1833 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1834 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1835 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1838 isns_register(newns, oldns);
1841 /* Schedule iSNS update */
1842 if (!TAILQ_EMPTY(&newconf->conf_isns))
1843 set_timeout((newconf->conf_isns_period + 2) / 3, false);
1845 return (cumulated_error);
1852 return (sigalrm_received);
1856 sigalrm_handler_fatal(int dummy __unused)
1859 * It would be easiest to just log an error and exit. We can't
1860 * do this, though, because log_errx() is not signal safe, since
1861 * it calls syslog(3). Instead, set a flag checked by pdu_send()
1862 * and pdu_receive(), to call log_errx() there. Should they fail
1863 * to notice, we'll exit here one second later.
1865 if (sigalrm_received) {
1867 * Oh well. Just give up and quit.
1872 sigalrm_received = true;
1876 sigalrm_handler(int dummy __unused)
1879 sigalrm_received = true;
1883 set_timeout(int timeout, int fatal)
1885 struct sigaction sa;
1886 struct itimerval itv;
1890 log_debugx("session timeout disabled");
1891 bzero(&itv, sizeof(itv));
1892 error = setitimer(ITIMER_REAL, &itv, NULL);
1894 log_err(1, "setitimer");
1895 sigalrm_received = false;
1899 sigalrm_received = false;
1900 bzero(&sa, sizeof(sa));
1902 sa.sa_handler = sigalrm_handler_fatal;
1904 sa.sa_handler = sigalrm_handler;
1905 sigfillset(&sa.sa_mask);
1906 error = sigaction(SIGALRM, &sa, NULL);
1908 log_err(1, "sigaction");
1911 * First SIGALRM will arive after conf_timeout seconds.
1912 * If we do nothing, another one will arrive a second later.
1914 log_debugx("setting session timeout to %d seconds", timeout);
1915 bzero(&itv, sizeof(itv));
1916 itv.it_interval.tv_sec = 1;
1917 itv.it_value.tv_sec = timeout;
1918 error = setitimer(ITIMER_REAL, &itv, NULL);
1920 log_err(1, "setitimer");
1924 wait_for_children(bool block)
1932 * If "block" is true, wait for at least one process.
1934 if (block && num == 0)
1935 pid = wait4(-1, &status, 0, NULL);
1937 pid = wait4(-1, &status, WNOHANG, NULL);
1940 if (WIFSIGNALED(status)) {
1941 log_warnx("child process %d terminated with signal %d",
1942 pid, WTERMSIG(status));
1943 } else if (WEXITSTATUS(status) != 0) {
1944 log_warnx("child process %d terminated with exit status %d",
1945 pid, WEXITSTATUS(status));
1947 log_debugx("child process %d terminated gracefully", pid);
1956 handle_connection(struct portal *portal, int fd,
1957 const struct sockaddr *client_sa, bool dont_fork)
1959 struct connection *conn;
1962 char host[NI_MAXHOST + 1];
1965 conf = portal->p_portal_group->pg_conf;
1968 log_debugx("incoming connection; not forking due to -d flag");
1970 nchildren -= wait_for_children(false);
1971 assert(nchildren >= 0);
1973 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
1974 log_debugx("maxproc limit of %d child processes hit; "
1975 "waiting for child process to exit", conf->conf_maxproc);
1976 nchildren -= wait_for_children(true);
1977 assert(nchildren >= 0);
1979 log_debugx("incoming connection; forking child process #%d",
1990 pidfile_close(conf->conf_pidfh);
1992 error = getnameinfo(client_sa, client_sa->sa_len,
1993 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
1995 log_errx(1, "getnameinfo: %s", gai_strerror(error));
1997 log_debugx("accepted connection from %s; portal group \"%s\"",
1998 host, portal->p_portal_group->pg_name);
1999 log_set_peer_addr(host);
2000 setproctitle("%s", host);
2002 conn = connection_new(portal, fd, host, client_sa);
2003 set_timeout(conf->conf_timeout, true);
2006 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2007 kernel_handoff(conn);
2008 log_debugx("connection handed off to the kernel");
2010 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2013 log_debugx("nothing more to do; exiting");
2018 fd_add(int fd, fd_set *fdset, int nfds)
2022 * Skip sockets which we failed to bind.
2034 main_loop(struct conf *conf, bool dont_fork)
2036 struct portal_group *pg;
2037 struct portal *portal;
2038 struct sockaddr_storage client_sa;
2039 socklen_t client_salen;
2040 #ifdef ICL_KERNEL_PROXY
2045 int error, nfds, client_fd;
2047 pidfile_write(conf->conf_pidfh);
2050 if (sighup_received || sigterm_received || timed_out())
2053 #ifdef ICL_KERNEL_PROXY
2055 client_salen = sizeof(client_sa);
2056 kernel_accept(&connection_id, &portal_id,
2057 (struct sockaddr *)&client_sa, &client_salen);
2058 assert(client_salen >= client_sa.ss_len);
2060 log_debugx("incoming connection, id %d, portal id %d",
2061 connection_id, portal_id);
2062 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2063 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2064 if (portal->p_id == portal_id) {
2070 log_errx(1, "kernel returned invalid portal_id %d",
2074 handle_connection(portal, connection_id,
2075 (struct sockaddr *)&client_sa, dont_fork);
2078 assert(proxy_mode == false);
2082 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2083 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2084 nfds = fd_add(portal->p_socket, &fdset, nfds);
2086 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2090 log_err(1, "select");
2092 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2093 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2094 if (!FD_ISSET(portal->p_socket, &fdset))
2096 client_salen = sizeof(client_sa);
2097 client_fd = accept(portal->p_socket,
2098 (struct sockaddr *)&client_sa,
2101 log_err(1, "accept");
2102 assert(client_salen >= client_sa.ss_len);
2104 handle_connection(portal, client_fd,
2105 (struct sockaddr *)&client_sa,
2110 #ifdef ICL_KERNEL_PROXY
2117 sighup_handler(int dummy __unused)
2120 sighup_received = true;
2124 sigterm_handler(int dummy __unused)
2127 sigterm_received = true;
2131 sigchld_handler(int dummy __unused)
2135 * The only purpose of this handler is to make SIGCHLD
2136 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2137 * wait_for_children().
2142 register_signals(void)
2144 struct sigaction sa;
2147 bzero(&sa, sizeof(sa));
2148 sa.sa_handler = sighup_handler;
2149 sigfillset(&sa.sa_mask);
2150 error = sigaction(SIGHUP, &sa, NULL);
2152 log_err(1, "sigaction");
2154 sa.sa_handler = sigterm_handler;
2155 error = sigaction(SIGTERM, &sa, NULL);
2157 log_err(1, "sigaction");
2159 sa.sa_handler = sigterm_handler;
2160 error = sigaction(SIGINT, &sa, NULL);
2162 log_err(1, "sigaction");
2164 sa.sa_handler = sigchld_handler;
2165 error = sigaction(SIGCHLD, &sa, NULL);
2167 log_err(1, "sigaction");
2171 main(int argc, char **argv)
2173 struct conf *oldconf, *newconf, *tmpconf;
2175 const char *config_path = DEFAULT_CONFIG_PATH;
2176 int debug = 0, ch, error;
2177 bool dont_daemonize = false;
2179 while ((ch = getopt(argc, argv, "df:R")) != -1) {
2182 dont_daemonize = true;
2186 config_path = optarg;
2189 #ifndef ICL_KERNEL_PROXY
2190 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2191 "does not support iSER protocol");
2207 oldconf = conf_new_from_kernel();
2208 newconf = conf_new_from_file(config_path);
2209 if (newconf == NULL)
2210 log_errx(1, "configuration error; exiting");
2212 oldconf->conf_debug = debug;
2213 newconf->conf_debug = debug;
2216 error = conf_apply(oldconf, newconf);
2218 log_errx(1, "failed to apply configuration; exiting");
2220 conf_delete(oldconf);
2225 if (dont_daemonize == false) {
2226 log_debugx("daemonizing");
2227 if (daemon(0, 0) == -1) {
2228 log_warn("cannot daemonize");
2229 pidfile_remove(newconf->conf_pidfh);
2234 /* Schedule iSNS update */
2235 if (!TAILQ_EMPTY(&newconf->conf_isns))
2236 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2239 main_loop(newconf, dont_daemonize);
2240 if (sighup_received) {
2241 sighup_received = false;
2242 log_debugx("received SIGHUP, reloading configuration");
2243 tmpconf = conf_new_from_file(config_path);
2244 if (tmpconf == NULL) {
2245 log_warnx("configuration error, "
2246 "continuing with old configuration");
2249 tmpconf->conf_debug = debug;
2252 error = conf_apply(oldconf, newconf);
2254 log_warnx("failed to reload "
2256 conf_delete(oldconf);
2259 } else if (sigterm_received) {
2260 log_debugx("exiting on signal; "
2261 "reloading empty configuration");
2263 log_debugx("disabling CTL iSCSI port "
2264 "and terminating all connections");
2267 newconf = conf_new();
2269 newconf->conf_debug = debug;
2270 error = conf_apply(oldconf, newconf);
2272 log_warnx("failed to apply configuration");
2273 conf_delete(oldconf);
2276 log_warnx("exiting on signal");
2279 nchildren -= wait_for_children(false);
2280 assert(nchildren >= 0);
2282 set_timeout(0, false);
2283 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2285 /* Schedule iSNS update */
2286 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2287 set_timeout((newconf->conf_isns_period