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)
329 auth_name_check(const struct auth_group *ag, const char *initiator_name)
331 if (!auth_name_defined(ag))
334 if (auth_name_find(ag, initiator_name) == NULL)
340 const struct auth_portal *
341 auth_portal_new(struct auth_group *ag, const char *portal)
343 struct auth_portal *ap;
344 char *net, *mask, *str, *tmp;
347 ap = calloc(1, sizeof(*ap));
349 log_err(1, "calloc");
350 ap->ap_auth_group = ag;
351 ap->ap_initator_portal = checked_strdup(portal);
352 mask = str = checked_strdup(portal);
353 net = strsep(&mask, "/");
359 if (net[len - 1] == ']')
361 if (strchr(net, ':') != NULL) {
362 struct sockaddr_in6 *sin6 =
363 (struct sockaddr_in6 *)&ap->ap_sa;
365 sin6->sin6_len = sizeof(*sin6);
366 sin6->sin6_family = AF_INET6;
367 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
371 struct sockaddr_in *sin =
372 (struct sockaddr_in *)&ap->ap_sa;
374 sin->sin_len = sizeof(*sin);
375 sin->sin_family = AF_INET;
376 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
381 m = strtol(mask, &tmp, 0);
382 if (m < 0 || m > dm || tmp[0] != 0)
388 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
392 log_errx(1, "Incorrect initiator portal '%s'", portal);
397 auth_portal_delete(struct auth_portal *ap)
399 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
401 free(ap->ap_initator_portal);
406 auth_portal_defined(const struct auth_group *ag)
408 if (TAILQ_EMPTY(&ag->ag_portals))
413 const struct auth_portal *
414 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
416 const struct auth_portal *ap;
417 const uint8_t *a, *b;
421 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
422 if (ap->ap_sa.ss_family != ss->ss_family)
424 if (ss->ss_family == AF_INET) {
425 a = (const uint8_t *)
426 &((const struct sockaddr_in *)ss)->sin_addr;
427 b = (const uint8_t *)
428 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
430 a = (const uint8_t *)
431 &((const struct sockaddr_in6 *)ss)->sin6_addr;
432 b = (const uint8_t *)
433 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
435 for (i = 0; i < ap->ap_mask / 8; i++) {
439 if (ap->ap_mask % 8) {
440 bmask = 0xff << (8 - (ap->ap_mask % 8));
441 if ((a[i] & bmask) != (b[i] & bmask))
453 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
456 if (!auth_portal_defined(ag))
459 if (auth_portal_find(ag, sa) == NULL)
466 auth_group_new(struct conf *conf, const char *name)
468 struct auth_group *ag;
471 ag = auth_group_find(conf, name);
473 log_warnx("duplicated auth-group \"%s\"", name);
478 ag = calloc(1, sizeof(*ag));
480 log_err(1, "calloc");
482 ag->ag_name = checked_strdup(name);
483 TAILQ_INIT(&ag->ag_auths);
484 TAILQ_INIT(&ag->ag_names);
485 TAILQ_INIT(&ag->ag_portals);
487 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
493 auth_group_delete(struct auth_group *ag)
495 struct auth *auth, *auth_tmp;
496 struct auth_name *auth_name, *auth_name_tmp;
497 struct auth_portal *auth_portal, *auth_portal_tmp;
499 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
501 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
503 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
504 auth_name_delete(auth_name);
505 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
507 auth_portal_delete(auth_portal);
513 auth_group_find(const struct conf *conf, const char *name)
515 struct auth_group *ag;
517 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
518 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
526 auth_group_set_type(struct auth_group *ag, int type)
529 if (ag->ag_type == AG_TYPE_UNKNOWN) {
534 if (ag->ag_type == type)
541 auth_group_set_type_str(struct auth_group *ag, const char *str)
545 if (strcmp(str, "none") == 0) {
546 type = AG_TYPE_NO_AUTHENTICATION;
547 } else if (strcmp(str, "deny") == 0) {
549 } else if (strcmp(str, "chap") == 0) {
551 } else if (strcmp(str, "chap-mutual") == 0) {
552 type = AG_TYPE_CHAP_MUTUAL;
554 if (ag->ag_name != NULL)
555 log_warnx("invalid auth-type \"%s\" for auth-group "
556 "\"%s\"", str, ag->ag_name);
558 log_warnx("invalid auth-type \"%s\" for target "
559 "\"%s\"", str, ag->ag_target->t_name);
563 error = auth_group_set_type(ag, type);
565 if (ag->ag_name != NULL)
566 log_warnx("cannot set auth-type to \"%s\" for "
567 "auth-group \"%s\"; already has a different "
568 "type", str, ag->ag_name);
570 log_warnx("cannot set auth-type to \"%s\" for target "
571 "\"%s\"; already has a different type",
572 str, ag->ag_target->t_name);
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_portals);
621 conf->conf_last_portal_group_tag++;
622 pg->pg_tag = conf->conf_last_portal_group_tag;
623 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
629 portal_group_delete(struct portal_group *pg)
631 struct portal *portal, *tmp;
633 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
635 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
636 portal_delete(portal);
641 struct portal_group *
642 portal_group_find(const struct conf *conf, const char *name)
644 struct portal_group *pg;
646 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
647 if (strcmp(pg->pg_name, name) == 0)
655 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
657 struct addrinfo hints;
660 int error, colons = 0;
664 * IPv6 address in square brackets, perhaps with port.
667 addr = strsep(&arg, "]");
670 if (arg[0] == '\0') {
672 } else if (arg[0] == ':') {
678 * Either IPv6 address without brackets - and without
679 * a port - or IPv4 address. Just count the colons.
681 for (ch = arg; *ch != '\0'; ch++) {
689 addr = strsep(&arg, ":");
697 memset(&hints, 0, sizeof(hints));
698 hints.ai_family = PF_UNSPEC;
699 hints.ai_socktype = SOCK_STREAM;
700 hints.ai_flags = AI_PASSIVE;
701 error = getaddrinfo(addr, port, &hints, ai);
708 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
710 struct portal *portal;
712 portal = portal_new(pg);
713 portal->p_listen = checked_strdup(value);
714 portal->p_iser = iser;
716 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
717 log_warnx("invalid listen address %s", portal->p_listen);
718 portal_delete(portal);
723 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
724 * those into multiple portals.
731 isns_new(struct conf *conf, const char *addr)
735 isns = calloc(1, sizeof(*isns));
737 log_err(1, "calloc");
739 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
740 isns->i_addr = checked_strdup(addr);
742 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
743 log_warnx("invalid iSNS address %s", isns->i_addr);
749 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
750 * those into multiple servers.
757 isns_delete(struct isns *isns)
760 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
762 if (isns->i_ai != NULL)
763 freeaddrinfo(isns->i_ai);
768 isns_do_connect(struct isns *isns)
772 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
773 isns->i_ai->ai_protocol);
775 log_warn("socket(2) failed for %s", isns->i_addr);
778 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
779 log_warn("connect(2) failed for %s", isns->i_addr);
787 isns_do_register(struct isns *isns, int s, const char *hostname)
789 struct conf *conf = isns->i_conf;
790 struct target *target;
791 struct portal *portal;
792 struct portal_group *pg;
793 struct isns_req *req;
797 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
798 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
799 isns_req_add_delim(req);
800 isns_req_add_str(req, 1, hostname);
801 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
802 isns_req_add_32(req, 6, conf->conf_isns_period);
803 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
804 if (pg->pg_unassigned)
806 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
807 isns_req_add_addr(req, 16, portal->p_ai);
808 isns_req_add_port(req, 17, portal->p_ai);
811 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
812 isns_req_add_str(req, 32, target->t_name);
813 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
814 if (target->t_alias != NULL)
815 isns_req_add_str(req, 34, target->t_alias);
816 pg = target->t_portal_group;
817 isns_req_add_32(req, 51, pg->pg_tag);
818 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
819 isns_req_add_addr(req, 49, portal->p_ai);
820 isns_req_add_port(req, 50, portal->p_ai);
823 res = isns_req_send(s, req);
825 log_warn("send(2) failed for %s", isns->i_addr);
828 res = isns_req_receive(s, req);
830 log_warn("receive(2) failed for %s", isns->i_addr);
833 error = isns_req_get_status(req);
835 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
844 isns_do_check(struct isns *isns, int s, const char *hostname)
846 struct conf *conf = isns->i_conf;
847 struct isns_req *req;
851 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
852 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
853 isns_req_add_str(req, 1, hostname);
854 isns_req_add_delim(req);
855 isns_req_add(req, 2, 0, NULL);
856 res = isns_req_send(s, req);
858 log_warn("send(2) failed for %s", isns->i_addr);
861 res = isns_req_receive(s, req);
863 log_warn("receive(2) failed for %s", isns->i_addr);
866 error = isns_req_get_status(req);
868 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
877 isns_do_deregister(struct isns *isns, int s, const char *hostname)
879 struct conf *conf = isns->i_conf;
880 struct isns_req *req;
884 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
885 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
886 isns_req_add_delim(req);
887 isns_req_add_str(req, 1, hostname);
888 res = isns_req_send(s, req);
890 log_warn("send(2) failed for %s", isns->i_addr);
893 res = isns_req_receive(s, req);
895 log_warn("receive(2) failed for %s", isns->i_addr);
898 error = isns_req_get_status(req);
900 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
909 isns_register(struct isns *isns, struct isns *oldisns)
911 struct conf *conf = isns->i_conf;
915 if (TAILQ_EMPTY(&conf->conf_targets) ||
916 TAILQ_EMPTY(&conf->conf_portal_groups))
918 set_timeout(conf->conf_isns_timeout, false);
919 s = isns_do_connect(isns);
921 set_timeout(0, false);
924 gethostname(hostname, sizeof(hostname));
926 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
928 res = isns_do_deregister(oldisns, s, hostname);
929 res = isns_do_register(isns, s, hostname);
931 set_timeout(0, false);
935 isns_check(struct isns *isns)
937 struct conf *conf = isns->i_conf;
941 if (TAILQ_EMPTY(&conf->conf_targets) ||
942 TAILQ_EMPTY(&conf->conf_portal_groups))
944 set_timeout(conf->conf_isns_timeout, false);
945 s = isns_do_connect(isns);
947 set_timeout(0, false);
950 gethostname(hostname, sizeof(hostname));
952 res = isns_do_check(isns, s, hostname);
954 res = isns_do_deregister(isns, s, hostname);
955 res = isns_do_register(isns, s, hostname);
958 set_timeout(0, false);
962 isns_deregister(struct isns *isns)
964 struct conf *conf = isns->i_conf;
968 if (TAILQ_EMPTY(&conf->conf_targets) ||
969 TAILQ_EMPTY(&conf->conf_portal_groups))
971 set_timeout(conf->conf_isns_timeout, false);
972 s = isns_do_connect(isns);
975 gethostname(hostname, sizeof(hostname));
977 res = isns_do_deregister(isns, s, hostname);
979 set_timeout(0, false);
983 valid_hex(const char ch)
1015 valid_iscsi_name(const char *name)
1019 if (strlen(name) >= MAX_NAME_LEN) {
1020 log_warnx("overlong name for target \"%s\"; max length allowed "
1021 "by iSCSI specification is %d characters",
1022 name, MAX_NAME_LEN);
1027 * In the cases below, we don't return an error, just in case the admin
1028 * was right, and we're wrong.
1030 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1031 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1033 * XXX: We should verify UTF-8 normalisation, as defined
1034 * by 3.2.6.2: iSCSI Name Encoding.
1036 if (isalnum(name[i]))
1038 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1040 log_warnx("invalid character \"%c\" in target name "
1041 "\"%s\"; allowed characters are letters, digits, "
1042 "'-', '.', and ':'", name[i], name);
1046 * XXX: Check more stuff: valid date and a valid reversed domain.
1048 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1049 if (strlen(name) != strlen("eui.") + 16)
1050 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1051 "should be followed by exactly 16 hexadecimal "
1053 for (i = strlen("eui."); name[i] != '\0'; i++) {
1054 if (!valid_hex(name[i])) {
1055 log_warnx("invalid character \"%c\" in target "
1056 "name \"%s\"; allowed characters are 1-9 "
1057 "and A-F", name[i], name);
1061 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1062 if (strlen(name) > strlen("naa.") + 32)
1063 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1064 "should be followed by at most 32 hexadecimal "
1066 for (i = strlen("naa."); name[i] != '\0'; i++) {
1067 if (!valid_hex(name[i])) {
1068 log_warnx("invalid character \"%c\" in target "
1069 "name \"%s\"; allowed characters are 1-9 "
1070 "and A-F", name[i], name);
1075 log_warnx("invalid target name \"%s\"; should start with "
1076 "either \".iqn\", \"eui.\", or \"naa.\"",
1083 target_new(struct conf *conf, const char *name)
1085 struct target *targ;
1088 targ = target_find(conf, name);
1090 log_warnx("duplicated target \"%s\"", name);
1093 if (valid_iscsi_name(name) == false) {
1094 log_warnx("target name \"%s\" is invalid", name);
1097 targ = calloc(1, sizeof(*targ));
1099 log_err(1, "calloc");
1100 targ->t_name = checked_strdup(name);
1103 * RFC 3722 requires us to normalize the name to lowercase.
1106 for (i = 0; i < len; i++)
1107 targ->t_name[i] = tolower(targ->t_name[i]);
1109 TAILQ_INIT(&targ->t_luns);
1110 targ->t_conf = conf;
1111 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1117 target_delete(struct target *targ)
1119 struct lun *lun, *tmp;
1121 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1123 TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
1130 target_find(struct conf *conf, const char *name)
1132 struct target *targ;
1134 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1135 if (strcasecmp(targ->t_name, name) == 0)
1143 lun_new(struct target *targ, int lun_id)
1147 lun = lun_find(targ, lun_id);
1149 log_warnx("duplicated lun %d for target \"%s\"",
1150 lun_id, targ->t_name);
1154 lun = calloc(1, sizeof(*lun));
1156 log_err(1, "calloc");
1157 lun->l_lun = lun_id;
1158 TAILQ_INIT(&lun->l_options);
1159 lun->l_target = targ;
1160 TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
1166 lun_delete(struct lun *lun)
1168 struct lun_option *lo, *tmp;
1170 TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
1172 TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
1173 lun_option_delete(lo);
1174 free(lun->l_backend);
1175 free(lun->l_device_id);
1177 free(lun->l_serial);
1182 lun_find(const struct target *targ, int lun_id)
1186 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1187 if (lun->l_lun == lun_id)
1195 lun_set_backend(struct lun *lun, const char *value)
1197 free(lun->l_backend);
1198 lun->l_backend = checked_strdup(value);
1202 lun_set_blocksize(struct lun *lun, size_t value)
1205 lun->l_blocksize = value;
1209 lun_set_device_id(struct lun *lun, const char *value)
1211 free(lun->l_device_id);
1212 lun->l_device_id = checked_strdup(value);
1216 lun_set_path(struct lun *lun, const char *value)
1219 lun->l_path = checked_strdup(value);
1223 lun_set_serial(struct lun *lun, const char *value)
1225 free(lun->l_serial);
1226 lun->l_serial = checked_strdup(value);
1230 lun_set_size(struct lun *lun, size_t value)
1233 lun->l_size = value;
1237 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1240 lun->l_ctl_lun = value;
1244 lun_option_new(struct lun *lun, const char *name, const char *value)
1246 struct lun_option *lo;
1248 lo = lun_option_find(lun, name);
1250 log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
1251 name, lun->l_lun, lun->l_target->t_name);
1255 lo = calloc(1, sizeof(*lo));
1257 log_err(1, "calloc");
1258 lo->lo_name = checked_strdup(name);
1259 lo->lo_value = checked_strdup(value);
1261 TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
1267 lun_option_delete(struct lun_option *lo)
1270 TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
1278 lun_option_find(const struct lun *lun, const char *name)
1280 struct lun_option *lo;
1282 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
1283 if (strcmp(lo->lo_name, name) == 0)
1291 lun_option_set(struct lun_option *lo, const char *value)
1295 lo->lo_value = checked_strdup(value);
1298 static struct connection *
1299 connection_new(struct portal *portal, int fd, const char *host,
1300 const struct sockaddr *client_sa)
1302 struct connection *conn;
1304 conn = calloc(1, sizeof(*conn));
1306 log_err(1, "calloc");
1307 conn->conn_portal = portal;
1308 conn->conn_socket = fd;
1309 conn->conn_initiator_addr = checked_strdup(host);
1310 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1313 * Default values, from RFC 3720, section 12.
1315 conn->conn_max_data_segment_length = 8192;
1316 conn->conn_max_burst_length = 262144;
1317 conn->conn_immediate_data = true;
1324 conf_print(struct conf *conf)
1326 struct auth_group *ag;
1328 struct auth_name *auth_name;
1329 struct auth_portal *auth_portal;
1330 struct portal_group *pg;
1331 struct portal *portal;
1332 struct target *targ;
1334 struct lun_option *lo;
1336 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1337 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1338 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1339 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1340 auth->a_user, auth->a_secret,
1341 auth->a_mutual_user, auth->a_mutual_secret);
1342 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1343 fprintf(stderr, "\t initiator-name %s\n",
1344 auth_name->an_initator_name);
1345 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1346 fprintf(stderr, "\t initiator-portal %s\n",
1347 auth_portal->an_initator_portal);
1348 fprintf(stderr, "}\n");
1350 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1351 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1352 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1353 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1354 fprintf(stderr, "}\n");
1356 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1357 fprintf(stderr, "target %s {\n", targ->t_name);
1358 if (targ->t_alias != NULL)
1359 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1360 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1361 fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1362 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1363 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1364 fprintf(stderr, "\t\toption %s %s\n",
1365 lo->lo_name, lo->lo_value);
1366 fprintf(stderr, "\t}\n");
1368 fprintf(stderr, "}\n");
1374 conf_verify_lun(struct lun *lun)
1376 const struct lun *lun2;
1377 const struct target *targ2;
1379 if (lun->l_backend == NULL)
1380 lun_set_backend(lun, "block");
1381 if (strcmp(lun->l_backend, "block") == 0) {
1382 if (lun->l_path == NULL) {
1383 log_warnx("missing path for lun %d, target \"%s\"",
1384 lun->l_lun, lun->l_target->t_name);
1387 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1388 if (lun->l_size == 0) {
1389 log_warnx("missing size for ramdisk-backed lun %d, "
1390 "target \"%s\"", lun->l_lun, lun->l_target->t_name);
1393 if (lun->l_path != NULL) {
1394 log_warnx("path must not be specified "
1395 "for ramdisk-backed lun %d, target \"%s\"",
1396 lun->l_lun, lun->l_target->t_name);
1400 if (lun->l_lun < 0 || lun->l_lun > 255) {
1401 log_warnx("invalid lun number for lun %d, target \"%s\"; "
1402 "must be between 0 and 255", lun->l_lun,
1403 lun->l_target->t_name);
1406 if (lun->l_blocksize == 0) {
1407 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1408 } else if (lun->l_blocksize < 0) {
1409 log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1410 "must be larger than 0", lun->l_lun, lun->l_target->t_name);
1413 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1414 log_warnx("invalid size for lun %d, target \"%s\"; "
1415 "must be multiple of blocksize", lun->l_lun,
1416 lun->l_target->t_name);
1419 TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1420 TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1423 if (lun->l_path != NULL && lun2->l_path != NULL &&
1424 strcmp(lun->l_path, lun2->l_path) == 0) {
1425 log_debugx("WARNING: path \"%s\" duplicated "
1426 "between lun %d, target \"%s\", and "
1427 "lun %d, target \"%s\"", lun->l_path,
1428 lun->l_lun, lun->l_target->t_name,
1429 lun2->l_lun, lun2->l_target->t_name);
1438 conf_verify(struct conf *conf)
1440 struct auth_group *ag;
1441 struct portal_group *pg;
1442 struct target *targ;
1447 if (conf->conf_pidfile_path == NULL)
1448 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1450 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1451 if (targ->t_auth_group == NULL) {
1452 targ->t_auth_group = auth_group_find(conf,
1454 assert(targ->t_auth_group != NULL);
1456 if (targ->t_portal_group == NULL) {
1457 targ->t_portal_group = portal_group_find(conf,
1459 assert(targ->t_portal_group != NULL);
1462 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1463 error = conf_verify_lun(lun);
1469 log_warnx("no LUNs defined for target \"%s\"",
1473 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1474 assert(pg->pg_name != NULL);
1475 if (pg->pg_discovery_auth_group == NULL) {
1476 pg->pg_discovery_auth_group =
1477 auth_group_find(conf, "default");
1478 assert(pg->pg_discovery_auth_group != NULL);
1481 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1482 if (targ->t_portal_group == pg)
1486 if (strcmp(pg->pg_name, "default") != 0)
1487 log_warnx("portal-group \"%s\" not assigned "
1488 "to any target", pg->pg_name);
1489 pg->pg_unassigned = true;
1491 pg->pg_unassigned = false;
1493 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1494 if (ag->ag_name == NULL)
1495 assert(ag->ag_target != NULL);
1497 assert(ag->ag_target == NULL);
1500 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1501 if (targ->t_auth_group == ag) {
1506 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1507 if (pg->pg_discovery_auth_group == ag) {
1512 if (!found && ag->ag_name != NULL &&
1513 strcmp(ag->ag_name, "default") != 0 &&
1514 strcmp(ag->ag_name, "no-authentication") != 0 &&
1515 strcmp(ag->ag_name, "no-access") != 0) {
1516 log_warnx("auth-group \"%s\" not assigned "
1517 "to any target", ag->ag_name);
1525 conf_apply(struct conf *oldconf, struct conf *newconf)
1527 struct target *oldtarg, *newtarg, *tmptarg;
1528 struct lun *oldlun, *newlun, *tmplun;
1529 struct portal_group *oldpg, *newpg;
1530 struct portal *oldp, *newp;
1531 struct isns *oldns, *newns;
1533 int changed, cumulated_error = 0, error;
1536 if (oldconf->conf_debug != newconf->conf_debug) {
1537 log_debugx("changing debug level to %d", newconf->conf_debug);
1538 log_init(newconf->conf_debug);
1541 if (oldconf->conf_pidfh != NULL) {
1542 assert(oldconf->conf_pidfile_path != NULL);
1543 if (newconf->conf_pidfile_path != NULL &&
1544 strcmp(oldconf->conf_pidfile_path,
1545 newconf->conf_pidfile_path) == 0) {
1546 newconf->conf_pidfh = oldconf->conf_pidfh;
1547 oldconf->conf_pidfh = NULL;
1549 log_debugx("removing pidfile %s",
1550 oldconf->conf_pidfile_path);
1551 pidfile_remove(oldconf->conf_pidfh);
1552 oldconf->conf_pidfh = NULL;
1556 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1557 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1558 newconf->conf_pidfh =
1559 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1560 if (newconf->conf_pidfh == NULL) {
1561 if (errno == EEXIST)
1562 log_errx(1, "daemon already running, pid: %jd.",
1563 (intmax_t)otherpid);
1564 log_err(1, "cannot open or create pidfile \"%s\"",
1565 newconf->conf_pidfile_path);
1569 /* Deregister on removed iSNS servers. */
1570 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1571 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1572 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1576 isns_deregister(oldns);
1580 * XXX: If target or lun removal fails, we should somehow "move"
1581 * the old lun or target into newconf, so that subsequent
1582 * conf_apply() would try to remove them again. That would
1583 * be somewhat hairy, though, and lun deletion failures don't
1584 * really happen, so leave it as it is for now.
1586 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1588 * First, remove any targets present in the old configuration
1589 * and missing in the new one.
1591 newtarg = target_find(newconf, oldtarg->t_name);
1592 if (newtarg == NULL) {
1593 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1595 log_debugx("target %s not found in new "
1596 "configuration; removing its lun %d, "
1597 "backed by CTL lun %d",
1598 oldtarg->t_name, oldlun->l_lun,
1600 error = kernel_lun_remove(oldlun);
1602 log_warnx("failed to remove lun %d, "
1603 "target %s, CTL lun %d",
1604 oldlun->l_lun, oldtarg->t_name,
1609 kernel_port_remove(oldtarg);
1614 * Second, remove any LUNs present in the old target
1615 * and missing in the new one.
1617 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1618 newlun = lun_find(newtarg, oldlun->l_lun);
1619 if (newlun == NULL) {
1620 log_debugx("lun %d, target %s, CTL lun %d "
1621 "not found in new configuration; "
1622 "removing", oldlun->l_lun, oldtarg->t_name,
1624 error = kernel_lun_remove(oldlun);
1626 log_warnx("failed to remove lun %d, "
1627 "target %s, CTL lun %d",
1628 oldlun->l_lun, oldtarg->t_name,
1636 * Also remove the LUNs changed by more than size.
1639 assert(oldlun->l_backend != NULL);
1640 assert(newlun->l_backend != NULL);
1641 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1642 log_debugx("backend for lun %d, target %s, "
1643 "CTL lun %d changed; removing",
1644 oldlun->l_lun, oldtarg->t_name,
1648 if (oldlun->l_blocksize != newlun->l_blocksize) {
1649 log_debugx("blocksize for lun %d, target %s, "
1650 "CTL lun %d changed; removing",
1651 oldlun->l_lun, oldtarg->t_name,
1655 if (newlun->l_device_id != NULL &&
1656 (oldlun->l_device_id == NULL ||
1657 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1659 log_debugx("device-id for lun %d, target %s, "
1660 "CTL lun %d changed; removing",
1661 oldlun->l_lun, oldtarg->t_name,
1665 if (newlun->l_path != NULL &&
1666 (oldlun->l_path == NULL ||
1667 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1668 log_debugx("path for lun %d, target %s, "
1669 "CTL lun %d, changed; removing",
1670 oldlun->l_lun, oldtarg->t_name,
1674 if (newlun->l_serial != NULL &&
1675 (oldlun->l_serial == NULL ||
1676 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1677 log_debugx("serial for lun %d, target %s, "
1678 "CTL lun %d changed; removing",
1679 oldlun->l_lun, oldtarg->t_name,
1684 error = kernel_lun_remove(oldlun);
1686 log_warnx("failed to remove lun %d, "
1687 "target %s, CTL lun %d",
1688 oldlun->l_lun, oldtarg->t_name,
1696 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1701 * Now add new targets or modify existing ones.
1703 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1704 oldtarg = target_find(oldconf, newtarg->t_name);
1706 TAILQ_FOREACH_SAFE(newlun, &newtarg->t_luns, l_next, tmplun) {
1707 if (oldtarg != NULL) {
1708 oldlun = lun_find(oldtarg, newlun->l_lun);
1709 if (oldlun != NULL) {
1710 if (newlun->l_size != oldlun->l_size ||
1711 newlun->l_size == 0) {
1712 log_debugx("resizing lun %d, "
1713 "target %s, CTL lun %d",
1718 kernel_lun_resize(newlun);
1720 log_warnx("failed to "
1733 log_debugx("adding lun %d, target %s",
1734 newlun->l_lun, newtarg->t_name);
1735 error = kernel_lun_add(newlun);
1737 log_warnx("failed to add lun %d, target %s",
1738 newlun->l_lun, newtarg->t_name);
1743 if (oldtarg == NULL)
1744 kernel_port_add(newtarg);
1748 * Go through the new portals, opening the sockets as neccessary.
1750 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1751 if (newpg->pg_unassigned) {
1752 log_debugx("not listening on portal-group \"%s\", "
1753 "not assigned to any target",
1757 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1759 * Try to find already open portal and reuse
1760 * the listening socket. We don't care about
1761 * what portal or portal group that was, what
1762 * matters is the listening address.
1764 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1766 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1768 if (strcmp(newp->p_listen,
1769 oldp->p_listen) == 0 &&
1770 oldp->p_socket > 0) {
1778 if (newp->p_socket > 0) {
1780 * We're done with this portal.
1785 #ifdef ICL_KERNEL_PROXY
1787 newpg->pg_conf->conf_portal_id++;
1788 newp->p_id = newpg->pg_conf->conf_portal_id;
1789 log_debugx("listening on %s, portal-group "
1790 "\"%s\", portal id %d, using ICL proxy",
1791 newp->p_listen, newpg->pg_name, newp->p_id);
1792 kernel_listen(newp->p_ai, newp->p_iser,
1797 assert(proxy_mode == false);
1798 assert(newp->p_iser == false);
1800 log_debugx("listening on %s, portal-group \"%s\"",
1801 newp->p_listen, newpg->pg_name);
1802 newp->p_socket = socket(newp->p_ai->ai_family,
1803 newp->p_ai->ai_socktype,
1804 newp->p_ai->ai_protocol);
1805 if (newp->p_socket < 0) {
1806 log_warn("socket(2) failed for %s",
1811 error = setsockopt(newp->p_socket, SOL_SOCKET,
1812 SO_REUSEADDR, &one, sizeof(one));
1814 log_warn("setsockopt(SO_REUSEADDR) failed "
1815 "for %s", newp->p_listen);
1816 close(newp->p_socket);
1821 error = bind(newp->p_socket, newp->p_ai->ai_addr,
1822 newp->p_ai->ai_addrlen);
1824 log_warn("bind(2) failed for %s",
1826 close(newp->p_socket);
1831 error = listen(newp->p_socket, -1);
1833 log_warn("listen(2) failed for %s",
1835 close(newp->p_socket);
1844 * Go through the no longer used sockets, closing them.
1846 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1847 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1848 if (oldp->p_socket <= 0)
1850 log_debugx("closing socket for %s, portal-group \"%s\"",
1851 oldp->p_listen, oldpg->pg_name);
1852 close(oldp->p_socket);
1857 /* (Re-)Register on remaining/new iSNS servers. */
1858 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1859 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1860 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1863 isns_register(newns, oldns);
1866 /* Schedule iSNS update */
1867 if (!TAILQ_EMPTY(&newconf->conf_isns))
1868 set_timeout((newconf->conf_isns_period + 2) / 3, false);
1870 return (cumulated_error);
1877 return (sigalrm_received);
1881 sigalrm_handler_fatal(int dummy __unused)
1884 * It would be easiest to just log an error and exit. We can't
1885 * do this, though, because log_errx() is not signal safe, since
1886 * it calls syslog(3). Instead, set a flag checked by pdu_send()
1887 * and pdu_receive(), to call log_errx() there. Should they fail
1888 * to notice, we'll exit here one second later.
1890 if (sigalrm_received) {
1892 * Oh well. Just give up and quit.
1897 sigalrm_received = true;
1901 sigalrm_handler(int dummy __unused)
1904 sigalrm_received = true;
1908 set_timeout(int timeout, int fatal)
1910 struct sigaction sa;
1911 struct itimerval itv;
1915 log_debugx("session timeout disabled");
1916 bzero(&itv, sizeof(itv));
1917 error = setitimer(ITIMER_REAL, &itv, NULL);
1919 log_err(1, "setitimer");
1920 sigalrm_received = false;
1924 sigalrm_received = false;
1925 bzero(&sa, sizeof(sa));
1927 sa.sa_handler = sigalrm_handler_fatal;
1929 sa.sa_handler = sigalrm_handler;
1930 sigfillset(&sa.sa_mask);
1931 error = sigaction(SIGALRM, &sa, NULL);
1933 log_err(1, "sigaction");
1936 * First SIGALRM will arive after conf_timeout seconds.
1937 * If we do nothing, another one will arrive a second later.
1939 log_debugx("setting session timeout to %d seconds", timeout);
1940 bzero(&itv, sizeof(itv));
1941 itv.it_interval.tv_sec = 1;
1942 itv.it_value.tv_sec = timeout;
1943 error = setitimer(ITIMER_REAL, &itv, NULL);
1945 log_err(1, "setitimer");
1949 wait_for_children(bool block)
1957 * If "block" is true, wait for at least one process.
1959 if (block && num == 0)
1960 pid = wait4(-1, &status, 0, NULL);
1962 pid = wait4(-1, &status, WNOHANG, NULL);
1965 if (WIFSIGNALED(status)) {
1966 log_warnx("child process %d terminated with signal %d",
1967 pid, WTERMSIG(status));
1968 } else if (WEXITSTATUS(status) != 0) {
1969 log_warnx("child process %d terminated with exit status %d",
1970 pid, WEXITSTATUS(status));
1972 log_debugx("child process %d terminated gracefully", pid);
1981 handle_connection(struct portal *portal, int fd,
1982 const struct sockaddr *client_sa, bool dont_fork)
1984 struct connection *conn;
1987 char host[NI_MAXHOST + 1];
1990 conf = portal->p_portal_group->pg_conf;
1993 log_debugx("incoming connection; not forking due to -d flag");
1995 nchildren -= wait_for_children(false);
1996 assert(nchildren >= 0);
1998 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
1999 log_debugx("maxproc limit of %d child processes hit; "
2000 "waiting for child process to exit", conf->conf_maxproc);
2001 nchildren -= wait_for_children(true);
2002 assert(nchildren >= 0);
2004 log_debugx("incoming connection; forking child process #%d",
2015 pidfile_close(conf->conf_pidfh);
2017 error = getnameinfo(client_sa, client_sa->sa_len,
2018 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2020 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2022 log_debugx("accepted connection from %s; portal group \"%s\"",
2023 host, portal->p_portal_group->pg_name);
2024 log_set_peer_addr(host);
2025 setproctitle("%s", host);
2027 conn = connection_new(portal, fd, host, client_sa);
2028 set_timeout(conf->conf_timeout, true);
2031 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2032 kernel_handoff(conn);
2033 log_debugx("connection handed off to the kernel");
2035 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2038 log_debugx("nothing more to do; exiting");
2043 fd_add(int fd, fd_set *fdset, int nfds)
2047 * Skip sockets which we failed to bind.
2059 main_loop(struct conf *conf, bool dont_fork)
2061 struct portal_group *pg;
2062 struct portal *portal;
2063 struct sockaddr_storage client_sa;
2064 socklen_t client_salen;
2065 #ifdef ICL_KERNEL_PROXY
2070 int error, nfds, client_fd;
2072 pidfile_write(conf->conf_pidfh);
2075 if (sighup_received || sigterm_received || timed_out())
2078 #ifdef ICL_KERNEL_PROXY
2080 client_salen = sizeof(client_sa);
2081 kernel_accept(&connection_id, &portal_id,
2082 (struct sockaddr *)&client_sa, &client_salen);
2083 assert(client_salen >= client_sa.ss_len);
2085 log_debugx("incoming connection, id %d, portal id %d",
2086 connection_id, portal_id);
2087 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2088 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2089 if (portal->p_id == portal_id) {
2095 log_errx(1, "kernel returned invalid portal_id %d",
2099 handle_connection(portal, connection_id,
2100 (struct sockaddr *)&client_sa, dont_fork);
2103 assert(proxy_mode == false);
2107 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2108 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2109 nfds = fd_add(portal->p_socket, &fdset, nfds);
2111 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2115 log_err(1, "select");
2117 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2118 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2119 if (!FD_ISSET(portal->p_socket, &fdset))
2121 client_salen = sizeof(client_sa);
2122 client_fd = accept(portal->p_socket,
2123 (struct sockaddr *)&client_sa,
2126 log_err(1, "accept");
2127 assert(client_salen >= client_sa.ss_len);
2129 handle_connection(portal, client_fd,
2130 (struct sockaddr *)&client_sa,
2135 #ifdef ICL_KERNEL_PROXY
2142 sighup_handler(int dummy __unused)
2145 sighup_received = true;
2149 sigterm_handler(int dummy __unused)
2152 sigterm_received = true;
2156 sigchld_handler(int dummy __unused)
2160 * The only purpose of this handler is to make SIGCHLD
2161 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2162 * wait_for_children().
2167 register_signals(void)
2169 struct sigaction sa;
2172 bzero(&sa, sizeof(sa));
2173 sa.sa_handler = sighup_handler;
2174 sigfillset(&sa.sa_mask);
2175 error = sigaction(SIGHUP, &sa, NULL);
2177 log_err(1, "sigaction");
2179 sa.sa_handler = sigterm_handler;
2180 error = sigaction(SIGTERM, &sa, NULL);
2182 log_err(1, "sigaction");
2184 sa.sa_handler = sigterm_handler;
2185 error = sigaction(SIGINT, &sa, NULL);
2187 log_err(1, "sigaction");
2189 sa.sa_handler = sigchld_handler;
2190 error = sigaction(SIGCHLD, &sa, NULL);
2192 log_err(1, "sigaction");
2196 main(int argc, char **argv)
2198 struct conf *oldconf, *newconf, *tmpconf;
2200 const char *config_path = DEFAULT_CONFIG_PATH;
2201 int debug = 0, ch, error;
2202 bool dont_daemonize = false;
2204 while ((ch = getopt(argc, argv, "df:R")) != -1) {
2207 dont_daemonize = true;
2211 config_path = optarg;
2214 #ifndef ICL_KERNEL_PROXY
2215 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2216 "does not support iSER protocol");
2232 oldconf = conf_new_from_kernel();
2233 newconf = conf_new_from_file(config_path);
2234 if (newconf == NULL)
2235 log_errx(1, "configuration error; exiting");
2237 oldconf->conf_debug = debug;
2238 newconf->conf_debug = debug;
2241 error = conf_apply(oldconf, newconf);
2243 log_errx(1, "failed to apply configuration; exiting");
2245 conf_delete(oldconf);
2250 if (dont_daemonize == false) {
2251 log_debugx("daemonizing");
2252 if (daemon(0, 0) == -1) {
2253 log_warn("cannot daemonize");
2254 pidfile_remove(newconf->conf_pidfh);
2259 /* Schedule iSNS update */
2260 if (!TAILQ_EMPTY(&newconf->conf_isns))
2261 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2264 main_loop(newconf, dont_daemonize);
2265 if (sighup_received) {
2266 sighup_received = false;
2267 log_debugx("received SIGHUP, reloading configuration");
2268 tmpconf = conf_new_from_file(config_path);
2269 if (tmpconf == NULL) {
2270 log_warnx("configuration error, "
2271 "continuing with old configuration");
2274 tmpconf->conf_debug = debug;
2277 error = conf_apply(oldconf, newconf);
2279 log_warnx("failed to reload "
2281 conf_delete(oldconf);
2284 } else if (sigterm_received) {
2285 log_debugx("exiting on signal; "
2286 "reloading empty configuration");
2288 log_debugx("disabling CTL iSCSI port "
2289 "and terminating all connections");
2292 newconf = conf_new();
2294 newconf->conf_debug = debug;
2295 error = conf_apply(oldconf, newconf);
2297 log_warnx("failed to apply configuration");
2298 conf_delete(oldconf);
2301 log_warnx("exiting on signal");
2304 nchildren -= wait_for_children(false);
2305 assert(nchildren >= 0);
2307 set_timeout(0, false);
2308 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2310 /* Schedule iSNS update */
2311 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2312 set_timeout((newconf->conf_isns_period