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;
62 static uint16_t last_portal_group_tag = 0xff;
68 fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
73 checked_strdup(const char *s)
88 conf = calloc(1, sizeof(*conf));
91 TAILQ_INIT(&conf->conf_luns);
92 TAILQ_INIT(&conf->conf_targets);
93 TAILQ_INIT(&conf->conf_auth_groups);
94 TAILQ_INIT(&conf->conf_ports);
95 TAILQ_INIT(&conf->conf_portal_groups);
96 TAILQ_INIT(&conf->conf_pports);
97 TAILQ_INIT(&conf->conf_isns);
99 conf->conf_isns_period = 900;
100 conf->conf_isns_timeout = 5;
101 conf->conf_debug = 0;
102 conf->conf_timeout = 60;
103 conf->conf_maxproc = 30;
109 conf_delete(struct conf *conf)
111 struct lun *lun, *ltmp;
112 struct target *targ, *tmp;
113 struct auth_group *ag, *cagtmp;
114 struct portal_group *pg, *cpgtmp;
115 struct pport *pp, *pptmp;
116 struct isns *is, *istmp;
118 assert(conf->conf_pidfh == NULL);
120 TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
122 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
124 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
125 auth_group_delete(ag);
126 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
127 portal_group_delete(pg);
128 TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
130 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
132 assert(TAILQ_EMPTY(&conf->conf_ports));
133 free(conf->conf_pidfile_path);
138 auth_new(struct auth_group *ag)
142 auth = calloc(1, sizeof(*auth));
144 log_err(1, "calloc");
145 auth->a_auth_group = ag;
146 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
151 auth_delete(struct auth *auth)
153 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
156 free(auth->a_secret);
157 free(auth->a_mutual_user);
158 free(auth->a_mutual_secret);
163 auth_find(const struct auth_group *ag, const char *user)
165 const struct auth *auth;
167 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
168 if (strcmp(auth->a_user, user) == 0)
176 auth_check_secret_length(struct auth *auth)
180 len = strlen(auth->a_secret);
182 if (auth->a_auth_group->ag_name != NULL)
183 log_warnx("secret for user \"%s\", auth-group \"%s\", "
184 "is too long; it should be at most 16 characters "
185 "long", auth->a_user, auth->a_auth_group->ag_name);
187 log_warnx("secret for user \"%s\", target \"%s\", "
188 "is too long; it should be at most 16 characters "
189 "long", auth->a_user,
190 auth->a_auth_group->ag_target->t_name);
193 if (auth->a_auth_group->ag_name != NULL)
194 log_warnx("secret for user \"%s\", auth-group \"%s\", "
195 "is too short; it should be at least 12 characters "
196 "long", auth->a_user,
197 auth->a_auth_group->ag_name);
199 log_warnx("secret for user \"%s\", target \"%s\", "
200 "is too short; it should be at least 12 characters "
201 "long", auth->a_user,
202 auth->a_auth_group->ag_target->t_name);
205 if (auth->a_mutual_secret != NULL) {
206 len = strlen(auth->a_mutual_secret);
208 if (auth->a_auth_group->ag_name != NULL)
209 log_warnx("mutual secret for user \"%s\", "
210 "auth-group \"%s\", is too long; it should "
211 "be at most 16 characters long",
212 auth->a_user, auth->a_auth_group->ag_name);
214 log_warnx("mutual secret for user \"%s\", "
215 "target \"%s\", is too long; it should "
216 "be at most 16 characters long",
218 auth->a_auth_group->ag_target->t_name);
221 if (auth->a_auth_group->ag_name != NULL)
222 log_warnx("mutual secret for user \"%s\", "
223 "auth-group \"%s\", is too short; it "
224 "should be at least 12 characters long",
225 auth->a_user, auth->a_auth_group->ag_name);
227 log_warnx("mutual secret for user \"%s\", "
228 "target \"%s\", is too short; it should be "
229 "at least 12 characters long",
231 auth->a_auth_group->ag_target->t_name);
237 auth_new_chap(struct auth_group *ag, const char *user,
242 if (ag->ag_type == AG_TYPE_UNKNOWN)
243 ag->ag_type = AG_TYPE_CHAP;
244 if (ag->ag_type != AG_TYPE_CHAP) {
245 if (ag->ag_name != NULL)
246 log_warnx("cannot mix \"chap\" authentication with "
247 "other types for auth-group \"%s\"", ag->ag_name);
249 log_warnx("cannot mix \"chap\" authentication with "
250 "other types for target \"%s\"",
251 ag->ag_target->t_name);
256 auth->a_user = checked_strdup(user);
257 auth->a_secret = checked_strdup(secret);
259 auth_check_secret_length(auth);
265 auth_new_chap_mutual(struct auth_group *ag, const char *user,
266 const char *secret, const char *user2, const char *secret2)
270 if (ag->ag_type == AG_TYPE_UNKNOWN)
271 ag->ag_type = AG_TYPE_CHAP_MUTUAL;
272 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
273 if (ag->ag_name != NULL)
274 log_warnx("cannot mix \"chap-mutual\" authentication "
275 "with other types for auth-group \"%s\"",
278 log_warnx("cannot mix \"chap-mutual\" authentication "
279 "with other types for target \"%s\"",
280 ag->ag_target->t_name);
285 auth->a_user = checked_strdup(user);
286 auth->a_secret = checked_strdup(secret);
287 auth->a_mutual_user = checked_strdup(user2);
288 auth->a_mutual_secret = checked_strdup(secret2);
290 auth_check_secret_length(auth);
295 const struct auth_name *
296 auth_name_new(struct auth_group *ag, const char *name)
298 struct auth_name *an;
300 an = calloc(1, sizeof(*an));
302 log_err(1, "calloc");
303 an->an_auth_group = ag;
304 an->an_initator_name = checked_strdup(name);
305 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
310 auth_name_delete(struct auth_name *an)
312 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
314 free(an->an_initator_name);
319 auth_name_defined(const struct auth_group *ag)
321 if (TAILQ_EMPTY(&ag->ag_names))
326 const struct auth_name *
327 auth_name_find(const struct auth_group *ag, const char *name)
329 const struct auth_name *auth_name;
331 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
332 if (strcmp(auth_name->an_initator_name, name) == 0)
340 auth_name_check(const struct auth_group *ag, const char *initiator_name)
342 if (!auth_name_defined(ag))
345 if (auth_name_find(ag, initiator_name) == NULL)
351 const struct auth_portal *
352 auth_portal_new(struct auth_group *ag, const char *portal)
354 struct auth_portal *ap;
355 char *net, *mask, *str, *tmp;
358 ap = calloc(1, sizeof(*ap));
360 log_err(1, "calloc");
361 ap->ap_auth_group = ag;
362 ap->ap_initator_portal = checked_strdup(portal);
363 mask = str = checked_strdup(portal);
364 net = strsep(&mask, "/");
370 if (net[len - 1] == ']')
372 if (strchr(net, ':') != NULL) {
373 struct sockaddr_in6 *sin6 =
374 (struct sockaddr_in6 *)&ap->ap_sa;
376 sin6->sin6_len = sizeof(*sin6);
377 sin6->sin6_family = AF_INET6;
378 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
382 struct sockaddr_in *sin =
383 (struct sockaddr_in *)&ap->ap_sa;
385 sin->sin_len = sizeof(*sin);
386 sin->sin_family = AF_INET;
387 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
392 m = strtol(mask, &tmp, 0);
393 if (m < 0 || m > dm || tmp[0] != 0)
399 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
405 log_errx(1, "Incorrect initiator portal '%s'", portal);
410 auth_portal_delete(struct auth_portal *ap)
412 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
414 free(ap->ap_initator_portal);
419 auth_portal_defined(const struct auth_group *ag)
421 if (TAILQ_EMPTY(&ag->ag_portals))
426 const struct auth_portal *
427 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
429 const struct auth_portal *ap;
430 const uint8_t *a, *b;
434 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
435 if (ap->ap_sa.ss_family != ss->ss_family)
437 if (ss->ss_family == AF_INET) {
438 a = (const uint8_t *)
439 &((const struct sockaddr_in *)ss)->sin_addr;
440 b = (const uint8_t *)
441 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
443 a = (const uint8_t *)
444 &((const struct sockaddr_in6 *)ss)->sin6_addr;
445 b = (const uint8_t *)
446 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
448 for (i = 0; i < ap->ap_mask / 8; i++) {
452 if (ap->ap_mask % 8) {
453 bmask = 0xff << (8 - (ap->ap_mask % 8));
454 if ((a[i] & bmask) != (b[i] & bmask))
466 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
469 if (!auth_portal_defined(ag))
472 if (auth_portal_find(ag, sa) == NULL)
479 auth_group_new(struct conf *conf, const char *name)
481 struct auth_group *ag;
484 ag = auth_group_find(conf, name);
486 log_warnx("duplicated auth-group \"%s\"", name);
491 ag = calloc(1, sizeof(*ag));
493 log_err(1, "calloc");
495 ag->ag_name = checked_strdup(name);
496 TAILQ_INIT(&ag->ag_auths);
497 TAILQ_INIT(&ag->ag_names);
498 TAILQ_INIT(&ag->ag_portals);
500 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
506 auth_group_delete(struct auth_group *ag)
508 struct auth *auth, *auth_tmp;
509 struct auth_name *auth_name, *auth_name_tmp;
510 struct auth_portal *auth_portal, *auth_portal_tmp;
512 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
514 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
516 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
517 auth_name_delete(auth_name);
518 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
520 auth_portal_delete(auth_portal);
526 auth_group_find(const struct conf *conf, const char *name)
528 struct auth_group *ag;
530 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
531 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
539 auth_group_set_type(struct auth_group *ag, const char *str)
543 if (strcmp(str, "none") == 0) {
544 type = AG_TYPE_NO_AUTHENTICATION;
545 } else if (strcmp(str, "deny") == 0) {
547 } else if (strcmp(str, "chap") == 0) {
549 } else if (strcmp(str, "chap-mutual") == 0) {
550 type = AG_TYPE_CHAP_MUTUAL;
552 if (ag->ag_name != NULL)
553 log_warnx("invalid auth-type \"%s\" for auth-group "
554 "\"%s\"", str, ag->ag_name);
556 log_warnx("invalid auth-type \"%s\" for target "
557 "\"%s\"", str, ag->ag_target->t_name);
561 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
562 if (ag->ag_name != NULL) {
563 log_warnx("cannot set auth-type to \"%s\" for "
564 "auth-group \"%s\"; already has a different "
565 "type", str, ag->ag_name);
567 log_warnx("cannot set auth-type to \"%s\" for target "
568 "\"%s\"; already has a different type",
569 str, ag->ag_target->t_name);
579 static struct portal *
580 portal_new(struct portal_group *pg)
582 struct portal *portal;
584 portal = calloc(1, sizeof(*portal));
586 log_err(1, "calloc");
587 TAILQ_INIT(&portal->p_targets);
588 portal->p_portal_group = pg;
589 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
594 portal_delete(struct portal *portal)
597 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
598 if (portal->p_ai != NULL)
599 freeaddrinfo(portal->p_ai);
600 free(portal->p_listen);
604 struct portal_group *
605 portal_group_new(struct conf *conf, const char *name)
607 struct portal_group *pg;
609 pg = portal_group_find(conf, name);
611 log_warnx("duplicated portal-group \"%s\"", name);
615 pg = calloc(1, sizeof(*pg));
617 log_err(1, "calloc");
618 pg->pg_name = checked_strdup(name);
619 TAILQ_INIT(&pg->pg_options);
620 TAILQ_INIT(&pg->pg_portals);
621 TAILQ_INIT(&pg->pg_ports);
623 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
624 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
630 portal_group_delete(struct portal_group *pg)
632 struct portal *portal, *tmp;
633 struct port *port, *tport;
634 struct option *o, *otmp;
636 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
638 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
640 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
641 portal_delete(portal);
642 TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
643 option_delete(&pg->pg_options, o);
645 free(pg->pg_redirection);
649 struct portal_group *
650 portal_group_find(const struct conf *conf, const char *name)
652 struct portal_group *pg;
654 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
655 if (strcmp(pg->pg_name, name) == 0)
663 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
665 struct addrinfo hints;
666 char *str, *addr, *ch;
668 int error, colons = 0;
670 str = arg = strdup(arg);
673 * IPv6 address in square brackets, perhaps with port.
676 addr = strsep(&arg, "]");
681 if (arg[0] == '\0') {
683 } else if (arg[0] == ':') {
691 * Either IPv6 address without brackets - and without
692 * a port - or IPv4 address. Just count the colons.
694 for (ch = arg; *ch != '\0'; ch++) {
702 addr = strsep(&arg, ":");
710 memset(&hints, 0, sizeof(hints));
711 hints.ai_family = PF_UNSPEC;
712 hints.ai_socktype = SOCK_STREAM;
713 hints.ai_flags = AI_PASSIVE;
714 error = getaddrinfo(addr, port, &hints, ai);
716 return ((error != 0) ? 1 : 0);
720 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
722 struct portal *portal;
724 portal = portal_new(pg);
725 portal->p_listen = checked_strdup(value);
726 portal->p_iser = iser;
728 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
729 log_warnx("invalid listen address %s", portal->p_listen);
730 portal_delete(portal);
735 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
736 * those into multiple portals.
743 isns_new(struct conf *conf, const char *addr)
747 isns = calloc(1, sizeof(*isns));
749 log_err(1, "calloc");
751 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
752 isns->i_addr = checked_strdup(addr);
754 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
755 log_warnx("invalid iSNS address %s", isns->i_addr);
761 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
762 * those into multiple servers.
769 isns_delete(struct isns *isns)
772 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
774 if (isns->i_ai != NULL)
775 freeaddrinfo(isns->i_ai);
780 isns_do_connect(struct isns *isns)
784 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
785 isns->i_ai->ai_protocol);
787 log_warn("socket(2) failed for %s", isns->i_addr);
790 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
791 log_warn("connect(2) failed for %s", isns->i_addr);
799 isns_do_register(struct isns *isns, int s, const char *hostname)
801 struct conf *conf = isns->i_conf;
802 struct target *target;
803 struct portal *portal;
804 struct portal_group *pg;
806 struct isns_req *req;
810 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
811 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
812 isns_req_add_delim(req);
813 isns_req_add_str(req, 1, hostname);
814 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
815 isns_req_add_32(req, 6, conf->conf_isns_period);
816 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
817 if (pg->pg_unassigned)
819 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
820 isns_req_add_addr(req, 16, portal->p_ai);
821 isns_req_add_port(req, 17, portal->p_ai);
824 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
825 isns_req_add_str(req, 32, target->t_name);
826 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
827 if (target->t_alias != NULL)
828 isns_req_add_str(req, 34, target->t_alias);
829 TAILQ_FOREACH(port, &target->t_ports, p_ts) {
830 if ((pg = port->p_portal_group) == NULL)
832 isns_req_add_32(req, 51, pg->pg_tag);
833 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
834 isns_req_add_addr(req, 49, portal->p_ai);
835 isns_req_add_port(req, 50, portal->p_ai);
839 res = isns_req_send(s, req);
841 log_warn("send(2) failed for %s", isns->i_addr);
844 res = isns_req_receive(s, req);
846 log_warn("receive(2) failed for %s", isns->i_addr);
849 error = isns_req_get_status(req);
851 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
860 isns_do_check(struct isns *isns, int s, const char *hostname)
862 struct conf *conf = isns->i_conf;
863 struct isns_req *req;
867 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
868 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
869 isns_req_add_str(req, 1, hostname);
870 isns_req_add_delim(req);
871 isns_req_add(req, 2, 0, NULL);
872 res = isns_req_send(s, req);
874 log_warn("send(2) failed for %s", isns->i_addr);
877 res = isns_req_receive(s, req);
879 log_warn("receive(2) failed for %s", isns->i_addr);
882 error = isns_req_get_status(req);
884 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
893 isns_do_deregister(struct isns *isns, int s, const char *hostname)
895 struct conf *conf = isns->i_conf;
896 struct isns_req *req;
900 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
901 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
902 isns_req_add_delim(req);
903 isns_req_add_str(req, 1, hostname);
904 res = isns_req_send(s, req);
906 log_warn("send(2) failed for %s", isns->i_addr);
909 res = isns_req_receive(s, req);
911 log_warn("receive(2) failed for %s", isns->i_addr);
914 error = isns_req_get_status(req);
916 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
925 isns_register(struct isns *isns, struct isns *oldisns)
927 struct conf *conf = isns->i_conf;
931 if (TAILQ_EMPTY(&conf->conf_targets) ||
932 TAILQ_EMPTY(&conf->conf_portal_groups))
934 set_timeout(conf->conf_isns_timeout, false);
935 s = isns_do_connect(isns);
937 set_timeout(0, false);
940 gethostname(hostname, sizeof(hostname));
942 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
944 isns_do_deregister(oldisns, s, hostname);
945 isns_do_register(isns, s, hostname);
947 set_timeout(0, false);
951 isns_check(struct isns *isns)
953 struct conf *conf = isns->i_conf;
957 if (TAILQ_EMPTY(&conf->conf_targets) ||
958 TAILQ_EMPTY(&conf->conf_portal_groups))
960 set_timeout(conf->conf_isns_timeout, false);
961 s = isns_do_connect(isns);
963 set_timeout(0, false);
966 gethostname(hostname, sizeof(hostname));
968 res = isns_do_check(isns, s, hostname);
970 isns_do_deregister(isns, s, hostname);
971 isns_do_register(isns, s, hostname);
974 set_timeout(0, false);
978 isns_deregister(struct isns *isns)
980 struct conf *conf = isns->i_conf;
984 if (TAILQ_EMPTY(&conf->conf_targets) ||
985 TAILQ_EMPTY(&conf->conf_portal_groups))
987 set_timeout(conf->conf_isns_timeout, false);
988 s = isns_do_connect(isns);
991 gethostname(hostname, sizeof(hostname));
993 isns_do_deregister(isns, s, hostname);
995 set_timeout(0, false);
999 portal_group_set_filter(struct portal_group *pg, const char *str)
1003 if (strcmp(str, "none") == 0) {
1004 filter = PG_FILTER_NONE;
1005 } else if (strcmp(str, "portal") == 0) {
1006 filter = PG_FILTER_PORTAL;
1007 } else if (strcmp(str, "portal-name") == 0) {
1008 filter = PG_FILTER_PORTAL_NAME;
1009 } else if (strcmp(str, "portal-name-auth") == 0) {
1010 filter = PG_FILTER_PORTAL_NAME_AUTH;
1012 log_warnx("invalid discovery-filter \"%s\" for portal-group "
1013 "\"%s\"; valid values are \"none\", \"portal\", "
1014 "\"portal-name\", and \"portal-name-auth\"",
1019 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1020 pg->pg_discovery_filter != filter) {
1021 log_warnx("cannot set discovery-filter to \"%s\" for "
1022 "portal-group \"%s\"; already has a different "
1023 "value", str, pg->pg_name);
1027 pg->pg_discovery_filter = filter;
1033 portal_group_set_redirection(struct portal_group *pg, const char *addr)
1036 if (pg->pg_redirection != NULL) {
1037 log_warnx("cannot set redirection to \"%s\" for "
1038 "portal-group \"%s\"; already defined",
1043 pg->pg_redirection = checked_strdup(addr);
1049 valid_hex(const char ch)
1081 valid_iscsi_name(const char *name)
1085 if (strlen(name) >= MAX_NAME_LEN) {
1086 log_warnx("overlong name for target \"%s\"; max length allowed "
1087 "by iSCSI specification is %d characters",
1088 name, MAX_NAME_LEN);
1093 * In the cases below, we don't return an error, just in case the admin
1094 * was right, and we're wrong.
1096 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1097 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1099 * XXX: We should verify UTF-8 normalisation, as defined
1100 * by 3.2.6.2: iSCSI Name Encoding.
1102 if (isalnum(name[i]))
1104 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1106 log_warnx("invalid character \"%c\" in target name "
1107 "\"%s\"; allowed characters are letters, digits, "
1108 "'-', '.', and ':'", name[i], name);
1112 * XXX: Check more stuff: valid date and a valid reversed domain.
1114 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1115 if (strlen(name) != strlen("eui.") + 16)
1116 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1117 "should be followed by exactly 16 hexadecimal "
1119 for (i = strlen("eui."); name[i] != '\0'; i++) {
1120 if (!valid_hex(name[i])) {
1121 log_warnx("invalid character \"%c\" in target "
1122 "name \"%s\"; allowed characters are 1-9 "
1123 "and A-F", name[i], name);
1127 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1128 if (strlen(name) > strlen("naa.") + 32)
1129 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1130 "should be followed by at most 32 hexadecimal "
1132 for (i = strlen("naa."); name[i] != '\0'; i++) {
1133 if (!valid_hex(name[i])) {
1134 log_warnx("invalid character \"%c\" in target "
1135 "name \"%s\"; allowed characters are 1-9 "
1136 "and A-F", name[i], name);
1141 log_warnx("invalid target name \"%s\"; should start with "
1142 "either \"iqn.\", \"eui.\", or \"naa.\"",
1149 pport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1153 pp = calloc(1, sizeof(*pp));
1155 log_err(1, "calloc");
1157 pp->pp_name = checked_strdup(name);
1158 pp->pp_ctl_port = ctl_port;
1159 TAILQ_INIT(&pp->pp_ports);
1160 TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1165 pport_find(const struct conf *conf, const char *name)
1169 TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1170 if (strcasecmp(pp->pp_name, name) == 0)
1177 pport_copy(struct pport *pp, struct conf *conf)
1179 struct pport *ppnew;
1181 ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1186 pport_delete(struct pport *pp)
1188 struct port *port, *tport;
1190 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1192 TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1198 port_new(struct conf *conf, struct target *target, struct portal_group *pg)
1204 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1206 log_err(1, "asprintf");
1207 if (port_find(conf, name) != NULL) {
1208 log_warnx("duplicate port \"%s\"", name);
1212 port = calloc(1, sizeof(*port));
1214 log_err(1, "calloc");
1215 port->p_conf = conf;
1216 port->p_name = name;
1217 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1218 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1219 port->p_target = target;
1220 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1221 port->p_portal_group = pg;
1222 port->p_foreign = pg->pg_foreign;
1227 port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1233 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1235 log_err(1, "asprintf");
1236 if (port_find(conf, name) != NULL) {
1237 log_warnx("duplicate port \"%s\"", name);
1241 port = calloc(1, sizeof(*port));
1243 log_err(1, "calloc");
1244 port->p_conf = conf;
1245 port->p_name = name;
1246 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1247 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1248 port->p_target = target;
1249 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1255 port_find(const struct conf *conf, const char *name)
1259 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1260 if (strcasecmp(port->p_name, name) == 0)
1268 port_find_in_pg(const struct portal_group *pg, const char *target)
1272 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1273 if (strcasecmp(port->p_target->t_name, target) == 0)
1281 port_delete(struct port *port)
1284 if (port->p_portal_group)
1285 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1287 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1289 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1290 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1296 target_new(struct conf *conf, const char *name)
1298 struct target *targ;
1301 targ = target_find(conf, name);
1303 log_warnx("duplicated target \"%s\"", name);
1306 if (valid_iscsi_name(name) == false) {
1307 log_warnx("target name \"%s\" is invalid", name);
1310 targ = calloc(1, sizeof(*targ));
1312 log_err(1, "calloc");
1313 targ->t_name = checked_strdup(name);
1316 * RFC 3722 requires us to normalize the name to lowercase.
1319 for (i = 0; i < len; i++)
1320 targ->t_name[i] = tolower(targ->t_name[i]);
1322 targ->t_conf = conf;
1323 TAILQ_INIT(&targ->t_ports);
1324 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1330 target_delete(struct target *targ)
1332 struct port *port, *tport;
1334 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1336 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1339 free(targ->t_redirection);
1344 target_find(struct conf *conf, const char *name)
1346 struct target *targ;
1348 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1349 if (strcasecmp(targ->t_name, name) == 0)
1357 target_set_redirection(struct target *target, const char *addr)
1360 if (target->t_redirection != NULL) {
1361 log_warnx("cannot set redirection to \"%s\" for "
1362 "target \"%s\"; already defined",
1363 addr, target->t_name);
1367 target->t_redirection = checked_strdup(addr);
1373 lun_new(struct conf *conf, const char *name)
1377 lun = lun_find(conf, name);
1379 log_warnx("duplicated lun \"%s\"", name);
1383 lun = calloc(1, sizeof(*lun));
1385 log_err(1, "calloc");
1387 lun->l_name = checked_strdup(name);
1388 TAILQ_INIT(&lun->l_options);
1389 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1390 lun->l_ctl_lun = -1;
1396 lun_delete(struct lun *lun)
1398 struct target *targ;
1399 struct option *o, *tmp;
1402 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1403 for (i = 0; i < MAX_LUNS; i++) {
1404 if (targ->t_luns[i] == lun)
1405 targ->t_luns[i] = NULL;
1408 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1410 TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1411 option_delete(&lun->l_options, o);
1413 free(lun->l_backend);
1414 free(lun->l_device_id);
1416 free(lun->l_scsiname);
1417 free(lun->l_serial);
1422 lun_find(const struct conf *conf, const char *name)
1426 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1427 if (strcmp(lun->l_name, name) == 0)
1435 lun_set_backend(struct lun *lun, const char *value)
1437 free(lun->l_backend);
1438 lun->l_backend = checked_strdup(value);
1442 lun_set_blocksize(struct lun *lun, size_t value)
1445 lun->l_blocksize = value;
1449 lun_set_device_type(struct lun *lun, uint8_t value)
1452 lun->l_device_type = value;
1456 lun_set_device_id(struct lun *lun, const char *value)
1458 free(lun->l_device_id);
1459 lun->l_device_id = checked_strdup(value);
1463 lun_set_path(struct lun *lun, const char *value)
1466 lun->l_path = checked_strdup(value);
1470 lun_set_scsiname(struct lun *lun, const char *value)
1472 free(lun->l_scsiname);
1473 lun->l_scsiname = checked_strdup(value);
1477 lun_set_serial(struct lun *lun, const char *value)
1479 free(lun->l_serial);
1480 lun->l_serial = checked_strdup(value);
1484 lun_set_size(struct lun *lun, size_t value)
1487 lun->l_size = value;
1491 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1494 lun->l_ctl_lun = value;
1498 option_new(struct options *options, const char *name, const char *value)
1502 o = option_find(options, name);
1504 log_warnx("duplicated option \"%s\"", name);
1508 o = calloc(1, sizeof(*o));
1510 log_err(1, "calloc");
1511 o->o_name = checked_strdup(name);
1512 o->o_value = checked_strdup(value);
1513 TAILQ_INSERT_TAIL(options, o, o_next);
1519 option_delete(struct options *options, struct option *o)
1522 TAILQ_REMOVE(options, o, o_next);
1529 option_find(const struct options *options, const char *name)
1533 TAILQ_FOREACH(o, options, o_next) {
1534 if (strcmp(o->o_name, name) == 0)
1542 option_set(struct option *o, const char *value)
1546 o->o_value = checked_strdup(value);
1549 static struct connection *
1550 connection_new(struct portal *portal, int fd, const char *host,
1551 const struct sockaddr *client_sa)
1553 struct connection *conn;
1555 conn = calloc(1, sizeof(*conn));
1557 log_err(1, "calloc");
1558 conn->conn_portal = portal;
1559 conn->conn_socket = fd;
1560 conn->conn_initiator_addr = checked_strdup(host);
1561 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1564 * Default values, from RFC 3720, section 12.
1566 conn->conn_max_data_segment_length = 8192;
1567 conn->conn_max_burst_length = 262144;
1568 conn->conn_immediate_data = true;
1575 conf_print(struct conf *conf)
1577 struct auth_group *ag;
1579 struct auth_name *auth_name;
1580 struct auth_portal *auth_portal;
1581 struct portal_group *pg;
1582 struct portal *portal;
1583 struct target *targ;
1587 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1588 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1589 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1590 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1591 auth->a_user, auth->a_secret,
1592 auth->a_mutual_user, auth->a_mutual_secret);
1593 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1594 fprintf(stderr, "\t initiator-name %s\n",
1595 auth_name->an_initator_name);
1596 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1597 fprintf(stderr, "\t initiator-portal %s\n",
1598 auth_portal->an_initator_portal);
1599 fprintf(stderr, "}\n");
1601 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1602 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1603 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1604 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1605 fprintf(stderr, "}\n");
1607 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1608 fprintf(stderr, "\tlun %s {\n", lun->l_name);
1609 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1610 TAILQ_FOREACH(o, &lun->l_options, o_next)
1611 fprintf(stderr, "\t\toption %s %s\n",
1612 lo->o_name, lo->o_value);
1613 fprintf(stderr, "\t}\n");
1615 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1616 fprintf(stderr, "target %s {\n", targ->t_name);
1617 if (targ->t_alias != NULL)
1618 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1619 fprintf(stderr, "}\n");
1625 conf_verify_lun(struct lun *lun)
1627 const struct lun *lun2;
1629 if (lun->l_backend == NULL)
1630 lun_set_backend(lun, "block");
1631 if (strcmp(lun->l_backend, "block") == 0) {
1632 if (lun->l_path == NULL) {
1633 log_warnx("missing path for lun \"%s\"",
1637 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1638 if (lun->l_size == 0) {
1639 log_warnx("missing size for ramdisk-backed lun \"%s\"",
1643 if (lun->l_path != NULL) {
1644 log_warnx("path must not be specified "
1645 "for ramdisk-backed lun \"%s\"",
1650 if (lun->l_blocksize == 0) {
1651 if (lun->l_device_type == 5)
1652 lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1654 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1655 } else if (lun->l_blocksize < 0) {
1656 log_warnx("invalid blocksize for lun \"%s\"; "
1657 "must be larger than 0", lun->l_name);
1660 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1661 log_warnx("invalid size for lun \"%s\"; "
1662 "must be multiple of blocksize", lun->l_name);
1665 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1668 if (lun->l_path != NULL && lun2->l_path != NULL &&
1669 strcmp(lun->l_path, lun2->l_path) == 0) {
1670 log_debugx("WARNING: path \"%s\" duplicated "
1671 "between lun \"%s\", and "
1672 "lun \"%s\"", lun->l_path,
1673 lun->l_name, lun2->l_name);
1681 conf_verify(struct conf *conf)
1683 struct auth_group *ag;
1684 struct portal_group *pg;
1686 struct target *targ;
1691 if (conf->conf_pidfile_path == NULL)
1692 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1694 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1695 error = conf_verify_lun(lun);
1699 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1700 if (targ->t_auth_group == NULL) {
1701 targ->t_auth_group = auth_group_find(conf,
1703 assert(targ->t_auth_group != NULL);
1705 if (TAILQ_EMPTY(&targ->t_ports)) {
1706 pg = portal_group_find(conf, "default");
1708 port_new(conf, targ, pg);
1711 for (i = 0; i < MAX_LUNS; i++) {
1712 if (targ->t_luns[i] != NULL)
1715 if (!found && targ->t_redirection == NULL) {
1716 log_warnx("no LUNs defined for target \"%s\"",
1719 if (found && targ->t_redirection != NULL) {
1720 log_debugx("target \"%s\" contains luns, "
1721 " but configured for redirection",
1725 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1726 assert(pg->pg_name != NULL);
1727 if (pg->pg_discovery_auth_group == NULL) {
1728 pg->pg_discovery_auth_group =
1729 auth_group_find(conf, "default");
1730 assert(pg->pg_discovery_auth_group != NULL);
1733 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1734 pg->pg_discovery_filter = PG_FILTER_NONE;
1736 if (pg->pg_redirection != NULL) {
1737 if (!TAILQ_EMPTY(&pg->pg_ports)) {
1738 log_debugx("portal-group \"%s\" assigned "
1739 "to target, but configured "
1743 pg->pg_unassigned = false;
1744 } else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1745 pg->pg_unassigned = false;
1747 if (strcmp(pg->pg_name, "default") != 0)
1748 log_warnx("portal-group \"%s\" not assigned "
1749 "to any target", pg->pg_name);
1750 pg->pg_unassigned = true;
1753 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1754 if (ag->ag_name == NULL)
1755 assert(ag->ag_target != NULL);
1757 assert(ag->ag_target == NULL);
1760 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1761 if (targ->t_auth_group == ag) {
1766 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1767 if (port->p_auth_group == ag) {
1772 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1773 if (pg->pg_discovery_auth_group == ag) {
1778 if (!found && ag->ag_name != NULL &&
1779 strcmp(ag->ag_name, "default") != 0 &&
1780 strcmp(ag->ag_name, "no-authentication") != 0 &&
1781 strcmp(ag->ag_name, "no-access") != 0) {
1782 log_warnx("auth-group \"%s\" not assigned "
1783 "to any target", ag->ag_name);
1791 conf_apply(struct conf *oldconf, struct conf *newconf)
1793 struct lun *oldlun, *newlun, *tmplun;
1794 struct portal_group *oldpg, *newpg;
1795 struct portal *oldp, *newp;
1796 struct port *oldport, *newport, *tmpport;
1797 struct isns *oldns, *newns;
1799 int changed, cumulated_error = 0, error, sockbuf;
1802 if (oldconf->conf_debug != newconf->conf_debug) {
1803 log_debugx("changing debug level to %d", newconf->conf_debug);
1804 log_init(newconf->conf_debug);
1807 if (oldconf->conf_pidfh != NULL) {
1808 assert(oldconf->conf_pidfile_path != NULL);
1809 if (newconf->conf_pidfile_path != NULL &&
1810 strcmp(oldconf->conf_pidfile_path,
1811 newconf->conf_pidfile_path) == 0) {
1812 newconf->conf_pidfh = oldconf->conf_pidfh;
1813 oldconf->conf_pidfh = NULL;
1815 log_debugx("removing pidfile %s",
1816 oldconf->conf_pidfile_path);
1817 pidfile_remove(oldconf->conf_pidfh);
1818 oldconf->conf_pidfh = NULL;
1822 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1823 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1824 newconf->conf_pidfh =
1825 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1826 if (newconf->conf_pidfh == NULL) {
1827 if (errno == EEXIST)
1828 log_errx(1, "daemon already running, pid: %jd.",
1829 (intmax_t)otherpid);
1830 log_err(1, "cannot open or create pidfile \"%s\"",
1831 newconf->conf_pidfile_path);
1836 * Go through the new portal groups, assigning tags or preserving old.
1838 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1839 if (newpg->pg_tag != 0)
1841 oldpg = portal_group_find(oldconf, newpg->pg_name);
1843 newpg->pg_tag = oldpg->pg_tag;
1845 newpg->pg_tag = ++last_portal_group_tag;
1848 /* Deregister on removed iSNS servers. */
1849 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1850 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1851 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1855 isns_deregister(oldns);
1859 * XXX: If target or lun removal fails, we should somehow "move"
1860 * the old lun or target into newconf, so that subsequent
1861 * conf_apply() would try to remove them again. That would
1862 * be somewhat hairy, though, and lun deletion failures don't
1863 * really happen, so leave it as it is for now.
1866 * First, remove any ports present in the old configuration
1867 * and missing in the new one.
1869 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1870 if (oldport->p_foreign)
1872 newport = port_find(newconf, oldport->p_name);
1873 if (newport != NULL && !newport->p_foreign)
1875 log_debugx("removing port \"%s\"", oldport->p_name);
1876 error = kernel_port_remove(oldport);
1878 log_warnx("failed to remove port %s",
1881 * XXX: Uncomment after fixing the root cause.
1883 * cumulated_error++;
1889 * Second, remove any LUNs present in the old configuration
1890 * and missing in the new one.
1892 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1893 newlun = lun_find(newconf, oldlun->l_name);
1894 if (newlun == NULL) {
1895 log_debugx("lun \"%s\", CTL lun %d "
1896 "not found in new configuration; "
1897 "removing", oldlun->l_name, oldlun->l_ctl_lun);
1898 error = kernel_lun_remove(oldlun);
1900 log_warnx("failed to remove lun \"%s\", "
1902 oldlun->l_name, oldlun->l_ctl_lun);
1909 * Also remove the LUNs changed by more than size.
1912 assert(oldlun->l_backend != NULL);
1913 assert(newlun->l_backend != NULL);
1914 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1915 log_debugx("backend for lun \"%s\", "
1916 "CTL lun %d changed; removing",
1917 oldlun->l_name, oldlun->l_ctl_lun);
1920 if (oldlun->l_blocksize != newlun->l_blocksize) {
1921 log_debugx("blocksize for lun \"%s\", "
1922 "CTL lun %d changed; removing",
1923 oldlun->l_name, oldlun->l_ctl_lun);
1926 if (newlun->l_device_id != NULL &&
1927 (oldlun->l_device_id == NULL ||
1928 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1930 log_debugx("device-id for lun \"%s\", "
1931 "CTL lun %d changed; removing",
1932 oldlun->l_name, oldlun->l_ctl_lun);
1935 if (newlun->l_path != NULL &&
1936 (oldlun->l_path == NULL ||
1937 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1938 log_debugx("path for lun \"%s\", "
1939 "CTL lun %d, changed; removing",
1940 oldlun->l_name, oldlun->l_ctl_lun);
1943 if (newlun->l_serial != NULL &&
1944 (oldlun->l_serial == NULL ||
1945 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1946 log_debugx("serial for lun \"%s\", "
1947 "CTL lun %d changed; removing",
1948 oldlun->l_name, oldlun->l_ctl_lun);
1952 error = kernel_lun_remove(oldlun);
1954 log_warnx("failed to remove lun \"%s\", "
1956 oldlun->l_name, oldlun->l_ctl_lun);
1963 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1966 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1967 oldlun = lun_find(oldconf, newlun->l_name);
1968 if (oldlun != NULL) {
1969 log_debugx("modifying lun \"%s\", CTL lun %d",
1970 newlun->l_name, newlun->l_ctl_lun);
1971 error = kernel_lun_modify(newlun);
1973 log_warnx("failed to "
1974 "modify lun \"%s\", CTL lun %d",
1975 newlun->l_name, newlun->l_ctl_lun);
1980 log_debugx("adding lun \"%s\"", newlun->l_name);
1981 error = kernel_lun_add(newlun);
1983 log_warnx("failed to add lun \"%s\"", newlun->l_name);
1990 * Now add new ports or modify existing ones.
1992 TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
1993 if (newport->p_foreign)
1995 oldport = port_find(oldconf, newport->p_name);
1997 if (oldport == NULL || oldport->p_foreign) {
1998 log_debugx("adding port \"%s\"", newport->p_name);
1999 error = kernel_port_add(newport);
2001 log_debugx("updating port \"%s\"", newport->p_name);
2002 newport->p_ctl_port = oldport->p_ctl_port;
2003 error = kernel_port_update(newport, oldport);
2006 log_warnx("failed to %s port %s",
2007 (oldport == NULL) ? "add" : "update",
2010 * XXX: Uncomment after fixing the root cause.
2012 * cumulated_error++;
2018 * Go through the new portals, opening the sockets as necessary.
2020 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2021 if (newpg->pg_foreign)
2023 if (newpg->pg_unassigned) {
2024 log_debugx("not listening on portal-group \"%s\", "
2025 "not assigned to any target",
2029 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2031 * Try to find already open portal and reuse
2032 * the listening socket. We don't care about
2033 * what portal or portal group that was, what
2034 * matters is the listening address.
2036 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2038 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2040 if (strcmp(newp->p_listen,
2041 oldp->p_listen) == 0 &&
2042 oldp->p_socket > 0) {
2050 if (newp->p_socket > 0) {
2052 * We're done with this portal.
2057 #ifdef ICL_KERNEL_PROXY
2059 newpg->pg_conf->conf_portal_id++;
2060 newp->p_id = newpg->pg_conf->conf_portal_id;
2061 log_debugx("listening on %s, portal-group "
2062 "\"%s\", portal id %d, using ICL proxy",
2063 newp->p_listen, newpg->pg_name, newp->p_id);
2064 kernel_listen(newp->p_ai, newp->p_iser,
2069 assert(proxy_mode == false);
2070 assert(newp->p_iser == false);
2072 log_debugx("listening on %s, portal-group \"%s\"",
2073 newp->p_listen, newpg->pg_name);
2074 newp->p_socket = socket(newp->p_ai->ai_family,
2075 newp->p_ai->ai_socktype,
2076 newp->p_ai->ai_protocol);
2077 if (newp->p_socket < 0) {
2078 log_warn("socket(2) failed for %s",
2083 sockbuf = SOCKBUF_SIZE;
2084 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2085 &sockbuf, sizeof(sockbuf)) == -1)
2086 log_warn("setsockopt(SO_RCVBUF) failed "
2087 "for %s", newp->p_listen);
2088 sockbuf = SOCKBUF_SIZE;
2089 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2090 &sockbuf, sizeof(sockbuf)) == -1)
2091 log_warn("setsockopt(SO_SNDBUF) failed "
2092 "for %s", newp->p_listen);
2093 error = setsockopt(newp->p_socket, SOL_SOCKET,
2094 SO_REUSEADDR, &one, sizeof(one));
2096 log_warn("setsockopt(SO_REUSEADDR) failed "
2097 "for %s", newp->p_listen);
2098 close(newp->p_socket);
2103 error = bind(newp->p_socket, newp->p_ai->ai_addr,
2104 newp->p_ai->ai_addrlen);
2106 log_warn("bind(2) failed for %s",
2108 close(newp->p_socket);
2113 error = listen(newp->p_socket, -1);
2115 log_warn("listen(2) failed for %s",
2117 close(newp->p_socket);
2126 * Go through the no longer used sockets, closing them.
2128 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2129 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2130 if (oldp->p_socket <= 0)
2132 log_debugx("closing socket for %s, portal-group \"%s\"",
2133 oldp->p_listen, oldpg->pg_name);
2134 close(oldp->p_socket);
2139 /* (Re-)Register on remaining/new iSNS servers. */
2140 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2141 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2142 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2145 isns_register(newns, oldns);
2148 /* Schedule iSNS update */
2149 if (!TAILQ_EMPTY(&newconf->conf_isns))
2150 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2152 return (cumulated_error);
2159 return (sigalrm_received);
2163 sigalrm_handler_fatal(int dummy __unused)
2166 * It would be easiest to just log an error and exit. We can't
2167 * do this, though, because log_errx() is not signal safe, since
2168 * it calls syslog(3). Instead, set a flag checked by pdu_send()
2169 * and pdu_receive(), to call log_errx() there. Should they fail
2170 * to notice, we'll exit here one second later.
2172 if (sigalrm_received) {
2174 * Oh well. Just give up and quit.
2179 sigalrm_received = true;
2183 sigalrm_handler(int dummy __unused)
2186 sigalrm_received = true;
2190 set_timeout(int timeout, int fatal)
2192 struct sigaction sa;
2193 struct itimerval itv;
2197 log_debugx("session timeout disabled");
2198 bzero(&itv, sizeof(itv));
2199 error = setitimer(ITIMER_REAL, &itv, NULL);
2201 log_err(1, "setitimer");
2202 sigalrm_received = false;
2206 sigalrm_received = false;
2207 bzero(&sa, sizeof(sa));
2209 sa.sa_handler = sigalrm_handler_fatal;
2211 sa.sa_handler = sigalrm_handler;
2212 sigfillset(&sa.sa_mask);
2213 error = sigaction(SIGALRM, &sa, NULL);
2215 log_err(1, "sigaction");
2218 * First SIGALRM will arive after conf_timeout seconds.
2219 * If we do nothing, another one will arrive a second later.
2221 log_debugx("setting session timeout to %d seconds", timeout);
2222 bzero(&itv, sizeof(itv));
2223 itv.it_interval.tv_sec = 1;
2224 itv.it_value.tv_sec = timeout;
2225 error = setitimer(ITIMER_REAL, &itv, NULL);
2227 log_err(1, "setitimer");
2231 wait_for_children(bool block)
2239 * If "block" is true, wait for at least one process.
2241 if (block && num == 0)
2242 pid = wait4(-1, &status, 0, NULL);
2244 pid = wait4(-1, &status, WNOHANG, NULL);
2247 if (WIFSIGNALED(status)) {
2248 log_warnx("child process %d terminated with signal %d",
2249 pid, WTERMSIG(status));
2250 } else if (WEXITSTATUS(status) != 0) {
2251 log_warnx("child process %d terminated with exit status %d",
2252 pid, WEXITSTATUS(status));
2254 log_debugx("child process %d terminated gracefully", pid);
2263 handle_connection(struct portal *portal, int fd,
2264 const struct sockaddr *client_sa, bool dont_fork)
2266 struct connection *conn;
2269 char host[NI_MAXHOST + 1];
2272 conf = portal->p_portal_group->pg_conf;
2275 log_debugx("incoming connection; not forking due to -d flag");
2277 nchildren -= wait_for_children(false);
2278 assert(nchildren >= 0);
2280 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2281 log_debugx("maxproc limit of %d child processes hit; "
2282 "waiting for child process to exit", conf->conf_maxproc);
2283 nchildren -= wait_for_children(true);
2284 assert(nchildren >= 0);
2286 log_debugx("incoming connection; forking child process #%d",
2297 pidfile_close(conf->conf_pidfh);
2299 error = getnameinfo(client_sa, client_sa->sa_len,
2300 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2302 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2304 log_debugx("accepted connection from %s; portal group \"%s\"",
2305 host, portal->p_portal_group->pg_name);
2306 log_set_peer_addr(host);
2307 setproctitle("%s", host);
2309 conn = connection_new(portal, fd, host, client_sa);
2310 set_timeout(conf->conf_timeout, true);
2313 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2314 kernel_handoff(conn);
2315 log_debugx("connection handed off to the kernel");
2317 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2320 log_debugx("nothing more to do; exiting");
2325 fd_add(int fd, fd_set *fdset, int nfds)
2329 * Skip sockets which we failed to bind.
2341 main_loop(struct conf *conf, bool dont_fork)
2343 struct portal_group *pg;
2344 struct portal *portal;
2345 struct sockaddr_storage client_sa;
2346 socklen_t client_salen;
2347 #ifdef ICL_KERNEL_PROXY
2352 int error, nfds, client_fd;
2354 pidfile_write(conf->conf_pidfh);
2357 if (sighup_received || sigterm_received || timed_out())
2360 #ifdef ICL_KERNEL_PROXY
2362 client_salen = sizeof(client_sa);
2363 kernel_accept(&connection_id, &portal_id,
2364 (struct sockaddr *)&client_sa, &client_salen);
2365 assert(client_salen >= client_sa.ss_len);
2367 log_debugx("incoming connection, id %d, portal id %d",
2368 connection_id, portal_id);
2369 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2370 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2371 if (portal->p_id == portal_id) {
2377 log_errx(1, "kernel returned invalid portal_id %d",
2381 handle_connection(portal, connection_id,
2382 (struct sockaddr *)&client_sa, dont_fork);
2385 assert(proxy_mode == false);
2389 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2390 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2391 nfds = fd_add(portal->p_socket, &fdset, nfds);
2393 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2397 log_err(1, "select");
2399 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2400 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2401 if (!FD_ISSET(portal->p_socket, &fdset))
2403 client_salen = sizeof(client_sa);
2404 client_fd = accept(portal->p_socket,
2405 (struct sockaddr *)&client_sa,
2407 if (client_fd < 0) {
2408 if (errno == ECONNABORTED)
2410 log_err(1, "accept");
2412 assert(client_salen >= client_sa.ss_len);
2414 handle_connection(portal, client_fd,
2415 (struct sockaddr *)&client_sa,
2420 #ifdef ICL_KERNEL_PROXY
2427 sighup_handler(int dummy __unused)
2430 sighup_received = true;
2434 sigterm_handler(int dummy __unused)
2437 sigterm_received = true;
2441 sigchld_handler(int dummy __unused)
2445 * The only purpose of this handler is to make SIGCHLD
2446 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2447 * wait_for_children().
2452 register_signals(void)
2454 struct sigaction sa;
2457 bzero(&sa, sizeof(sa));
2458 sa.sa_handler = sighup_handler;
2459 sigfillset(&sa.sa_mask);
2460 error = sigaction(SIGHUP, &sa, NULL);
2462 log_err(1, "sigaction");
2464 sa.sa_handler = sigterm_handler;
2465 error = sigaction(SIGTERM, &sa, NULL);
2467 log_err(1, "sigaction");
2469 sa.sa_handler = sigterm_handler;
2470 error = sigaction(SIGINT, &sa, NULL);
2472 log_err(1, "sigaction");
2474 sa.sa_handler = sigchld_handler;
2475 error = sigaction(SIGCHLD, &sa, NULL);
2477 log_err(1, "sigaction");
2481 main(int argc, char **argv)
2483 struct conf *oldconf, *newconf, *tmpconf;
2485 const char *config_path = DEFAULT_CONFIG_PATH;
2486 int debug = 0, ch, error;
2487 bool dont_daemonize = false;
2489 while ((ch = getopt(argc, argv, "df:R")) != -1) {
2492 dont_daemonize = true;
2496 config_path = optarg;
2499 #ifndef ICL_KERNEL_PROXY
2500 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2501 "does not support iSER protocol");
2517 oldconf = conf_new_from_kernel();
2518 newconf = conf_new_from_file(config_path, oldconf);
2519 if (newconf == NULL)
2520 log_errx(1, "configuration error; exiting");
2522 oldconf->conf_debug = debug;
2523 newconf->conf_debug = debug;
2526 error = conf_apply(oldconf, newconf);
2528 log_errx(1, "failed to apply configuration; exiting");
2530 conf_delete(oldconf);
2535 if (dont_daemonize == false) {
2536 log_debugx("daemonizing");
2537 if (daemon(0, 0) == -1) {
2538 log_warn("cannot daemonize");
2539 pidfile_remove(newconf->conf_pidfh);
2544 /* Schedule iSNS update */
2545 if (!TAILQ_EMPTY(&newconf->conf_isns))
2546 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2549 main_loop(newconf, dont_daemonize);
2550 if (sighup_received) {
2551 sighup_received = false;
2552 log_debugx("received SIGHUP, reloading configuration");
2553 tmpconf = conf_new_from_file(config_path, newconf);
2554 if (tmpconf == NULL) {
2555 log_warnx("configuration error, "
2556 "continuing with old configuration");
2559 tmpconf->conf_debug = debug;
2562 error = conf_apply(oldconf, newconf);
2564 log_warnx("failed to reload "
2566 conf_delete(oldconf);
2569 } else if (sigterm_received) {
2570 log_debugx("exiting on signal; "
2571 "reloading empty configuration");
2573 log_debugx("removing CTL iSCSI ports "
2574 "and terminating all connections");
2577 newconf = conf_new();
2579 newconf->conf_debug = debug;
2580 error = conf_apply(oldconf, newconf);
2582 log_warnx("failed to apply configuration");
2583 conf_delete(oldconf);
2586 log_warnx("exiting on signal");
2589 nchildren -= wait_for_children(false);
2590 assert(nchildren >= 0);
2592 set_timeout(0, false);
2593 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2595 /* Schedule iSNS update */
2596 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2597 set_timeout((newconf->conf_isns_period