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);
404 log_errx(1, "Incorrect initiator portal '%s'", portal);
409 auth_portal_delete(struct auth_portal *ap)
411 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
413 free(ap->ap_initator_portal);
418 auth_portal_defined(const struct auth_group *ag)
420 if (TAILQ_EMPTY(&ag->ag_portals))
425 const struct auth_portal *
426 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
428 const struct auth_portal *ap;
429 const uint8_t *a, *b;
433 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
434 if (ap->ap_sa.ss_family != ss->ss_family)
436 if (ss->ss_family == AF_INET) {
437 a = (const uint8_t *)
438 &((const struct sockaddr_in *)ss)->sin_addr;
439 b = (const uint8_t *)
440 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
442 a = (const uint8_t *)
443 &((const struct sockaddr_in6 *)ss)->sin6_addr;
444 b = (const uint8_t *)
445 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
447 for (i = 0; i < ap->ap_mask / 8; i++) {
451 if (ap->ap_mask % 8) {
452 bmask = 0xff << (8 - (ap->ap_mask % 8));
453 if ((a[i] & bmask) != (b[i] & bmask))
465 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
468 if (!auth_portal_defined(ag))
471 if (auth_portal_find(ag, sa) == NULL)
478 auth_group_new(struct conf *conf, const char *name)
480 struct auth_group *ag;
483 ag = auth_group_find(conf, name);
485 log_warnx("duplicated auth-group \"%s\"", name);
490 ag = calloc(1, sizeof(*ag));
492 log_err(1, "calloc");
494 ag->ag_name = checked_strdup(name);
495 TAILQ_INIT(&ag->ag_auths);
496 TAILQ_INIT(&ag->ag_names);
497 TAILQ_INIT(&ag->ag_portals);
499 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
505 auth_group_delete(struct auth_group *ag)
507 struct auth *auth, *auth_tmp;
508 struct auth_name *auth_name, *auth_name_tmp;
509 struct auth_portal *auth_portal, *auth_portal_tmp;
511 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
513 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
515 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
516 auth_name_delete(auth_name);
517 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
519 auth_portal_delete(auth_portal);
525 auth_group_find(const struct conf *conf, const char *name)
527 struct auth_group *ag;
529 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
530 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
538 auth_group_set_type(struct auth_group *ag, const char *str)
542 if (strcmp(str, "none") == 0) {
543 type = AG_TYPE_NO_AUTHENTICATION;
544 } else if (strcmp(str, "deny") == 0) {
546 } else if (strcmp(str, "chap") == 0) {
548 } else if (strcmp(str, "chap-mutual") == 0) {
549 type = AG_TYPE_CHAP_MUTUAL;
551 if (ag->ag_name != NULL)
552 log_warnx("invalid auth-type \"%s\" for auth-group "
553 "\"%s\"", str, ag->ag_name);
555 log_warnx("invalid auth-type \"%s\" for target "
556 "\"%s\"", str, ag->ag_target->t_name);
560 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
561 if (ag->ag_name != NULL) {
562 log_warnx("cannot set auth-type to \"%s\" for "
563 "auth-group \"%s\"; already has a different "
564 "type", str, ag->ag_name);
566 log_warnx("cannot set auth-type to \"%s\" for target "
567 "\"%s\"; already has a different type",
568 str, ag->ag_target->t_name);
578 static struct portal *
579 portal_new(struct portal_group *pg)
581 struct portal *portal;
583 portal = calloc(1, sizeof(*portal));
585 log_err(1, "calloc");
586 TAILQ_INIT(&portal->p_targets);
587 portal->p_portal_group = pg;
588 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
593 portal_delete(struct portal *portal)
596 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
597 if (portal->p_ai != NULL)
598 freeaddrinfo(portal->p_ai);
599 free(portal->p_listen);
603 struct portal_group *
604 portal_group_new(struct conf *conf, const char *name)
606 struct portal_group *pg;
608 pg = portal_group_find(conf, name);
610 log_warnx("duplicated portal-group \"%s\"", name);
614 pg = calloc(1, sizeof(*pg));
616 log_err(1, "calloc");
617 pg->pg_name = checked_strdup(name);
618 TAILQ_INIT(&pg->pg_options);
619 TAILQ_INIT(&pg->pg_portals);
620 TAILQ_INIT(&pg->pg_ports);
622 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
623 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
629 portal_group_delete(struct portal_group *pg)
631 struct portal *portal, *tmp;
632 struct port *port, *tport;
633 struct option *o, *otmp;
635 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
637 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
639 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
640 portal_delete(portal);
641 TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
642 option_delete(&pg->pg_options, o);
644 free(pg->pg_redirection);
648 struct portal_group *
649 portal_group_find(const struct conf *conf, const char *name)
651 struct portal_group *pg;
653 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
654 if (strcmp(pg->pg_name, name) == 0)
662 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
664 struct addrinfo hints;
665 char *str, *addr, *ch;
667 int error, colons = 0;
669 str = arg = strdup(arg);
672 * IPv6 address in square brackets, perhaps with port.
675 addr = strsep(&arg, "]");
678 if (arg[0] == '\0') {
680 } else if (arg[0] == ':') {
688 * Either IPv6 address without brackets - and without
689 * a port - or IPv4 address. Just count the colons.
691 for (ch = arg; *ch != '\0'; ch++) {
699 addr = strsep(&arg, ":");
707 memset(&hints, 0, sizeof(hints));
708 hints.ai_family = PF_UNSPEC;
709 hints.ai_socktype = SOCK_STREAM;
710 hints.ai_flags = AI_PASSIVE;
711 error = getaddrinfo(addr, port, &hints, ai);
713 return ((error != 0) ? 1 : 0);
717 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
719 struct portal *portal;
721 portal = portal_new(pg);
722 portal->p_listen = checked_strdup(value);
723 portal->p_iser = iser;
725 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
726 log_warnx("invalid listen address %s", portal->p_listen);
727 portal_delete(portal);
732 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
733 * those into multiple portals.
740 isns_new(struct conf *conf, const char *addr)
744 isns = calloc(1, sizeof(*isns));
746 log_err(1, "calloc");
748 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
749 isns->i_addr = checked_strdup(addr);
751 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
752 log_warnx("invalid iSNS address %s", isns->i_addr);
758 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
759 * those into multiple servers.
766 isns_delete(struct isns *isns)
769 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
771 if (isns->i_ai != NULL)
772 freeaddrinfo(isns->i_ai);
777 isns_do_connect(struct isns *isns)
781 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
782 isns->i_ai->ai_protocol);
784 log_warn("socket(2) failed for %s", isns->i_addr);
787 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
788 log_warn("connect(2) failed for %s", isns->i_addr);
796 isns_do_register(struct isns *isns, int s, const char *hostname)
798 struct conf *conf = isns->i_conf;
799 struct target *target;
800 struct portal *portal;
801 struct portal_group *pg;
803 struct isns_req *req;
807 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
808 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
809 isns_req_add_delim(req);
810 isns_req_add_str(req, 1, hostname);
811 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
812 isns_req_add_32(req, 6, conf->conf_isns_period);
813 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
814 if (pg->pg_unassigned)
816 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
817 isns_req_add_addr(req, 16, portal->p_ai);
818 isns_req_add_port(req, 17, portal->p_ai);
821 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
822 isns_req_add_str(req, 32, target->t_name);
823 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
824 if (target->t_alias != NULL)
825 isns_req_add_str(req, 34, target->t_alias);
826 TAILQ_FOREACH(port, &target->t_ports, p_ts) {
827 if ((pg = port->p_portal_group) == NULL)
829 isns_req_add_32(req, 51, pg->pg_tag);
830 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
831 isns_req_add_addr(req, 49, portal->p_ai);
832 isns_req_add_port(req, 50, portal->p_ai);
836 res = isns_req_send(s, req);
838 log_warn("send(2) failed for %s", isns->i_addr);
841 res = isns_req_receive(s, req);
843 log_warn("receive(2) failed for %s", isns->i_addr);
846 error = isns_req_get_status(req);
848 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
857 isns_do_check(struct isns *isns, int s, const char *hostname)
859 struct conf *conf = isns->i_conf;
860 struct isns_req *req;
864 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
865 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
866 isns_req_add_str(req, 1, hostname);
867 isns_req_add_delim(req);
868 isns_req_add(req, 2, 0, NULL);
869 res = isns_req_send(s, req);
871 log_warn("send(2) failed for %s", isns->i_addr);
874 res = isns_req_receive(s, req);
876 log_warn("receive(2) failed for %s", isns->i_addr);
879 error = isns_req_get_status(req);
881 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
890 isns_do_deregister(struct isns *isns, int s, const char *hostname)
892 struct conf *conf = isns->i_conf;
893 struct isns_req *req;
897 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
898 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
899 isns_req_add_delim(req);
900 isns_req_add_str(req, 1, hostname);
901 res = isns_req_send(s, req);
903 log_warn("send(2) failed for %s", isns->i_addr);
906 res = isns_req_receive(s, req);
908 log_warn("receive(2) failed for %s", isns->i_addr);
911 error = isns_req_get_status(req);
913 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
922 isns_register(struct isns *isns, struct isns *oldisns)
924 struct conf *conf = isns->i_conf;
928 if (TAILQ_EMPTY(&conf->conf_targets) ||
929 TAILQ_EMPTY(&conf->conf_portal_groups))
931 set_timeout(conf->conf_isns_timeout, false);
932 s = isns_do_connect(isns);
934 set_timeout(0, false);
937 gethostname(hostname, sizeof(hostname));
939 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
941 isns_do_deregister(oldisns, s, hostname);
942 isns_do_register(isns, s, hostname);
944 set_timeout(0, false);
948 isns_check(struct isns *isns)
950 struct conf *conf = isns->i_conf;
954 if (TAILQ_EMPTY(&conf->conf_targets) ||
955 TAILQ_EMPTY(&conf->conf_portal_groups))
957 set_timeout(conf->conf_isns_timeout, false);
958 s = isns_do_connect(isns);
960 set_timeout(0, false);
963 gethostname(hostname, sizeof(hostname));
965 res = isns_do_check(isns, s, hostname);
967 isns_do_deregister(isns, s, hostname);
968 isns_do_register(isns, s, hostname);
971 set_timeout(0, false);
975 isns_deregister(struct isns *isns)
977 struct conf *conf = isns->i_conf;
981 if (TAILQ_EMPTY(&conf->conf_targets) ||
982 TAILQ_EMPTY(&conf->conf_portal_groups))
984 set_timeout(conf->conf_isns_timeout, false);
985 s = isns_do_connect(isns);
988 gethostname(hostname, sizeof(hostname));
990 isns_do_deregister(isns, s, hostname);
992 set_timeout(0, false);
996 portal_group_set_filter(struct portal_group *pg, const char *str)
1000 if (strcmp(str, "none") == 0) {
1001 filter = PG_FILTER_NONE;
1002 } else if (strcmp(str, "portal") == 0) {
1003 filter = PG_FILTER_PORTAL;
1004 } else if (strcmp(str, "portal-name") == 0) {
1005 filter = PG_FILTER_PORTAL_NAME;
1006 } else if (strcmp(str, "portal-name-auth") == 0) {
1007 filter = PG_FILTER_PORTAL_NAME_AUTH;
1009 log_warnx("invalid discovery-filter \"%s\" for portal-group "
1010 "\"%s\"; valid values are \"none\", \"portal\", "
1011 "\"portal-name\", and \"portal-name-auth\"",
1016 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1017 pg->pg_discovery_filter != filter) {
1018 log_warnx("cannot set discovery-filter to \"%s\" for "
1019 "portal-group \"%s\"; already has a different "
1020 "value", str, pg->pg_name);
1024 pg->pg_discovery_filter = filter;
1030 portal_group_set_redirection(struct portal_group *pg, const char *addr)
1033 if (pg->pg_redirection != NULL) {
1034 log_warnx("cannot set redirection to \"%s\" for "
1035 "portal-group \"%s\"; already defined",
1040 pg->pg_redirection = checked_strdup(addr);
1046 valid_hex(const char ch)
1078 valid_iscsi_name(const char *name)
1082 if (strlen(name) >= MAX_NAME_LEN) {
1083 log_warnx("overlong name for target \"%s\"; max length allowed "
1084 "by iSCSI specification is %d characters",
1085 name, MAX_NAME_LEN);
1090 * In the cases below, we don't return an error, just in case the admin
1091 * was right, and we're wrong.
1093 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1094 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1096 * XXX: We should verify UTF-8 normalisation, as defined
1097 * by 3.2.6.2: iSCSI Name Encoding.
1099 if (isalnum(name[i]))
1101 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1103 log_warnx("invalid character \"%c\" in target name "
1104 "\"%s\"; allowed characters are letters, digits, "
1105 "'-', '.', and ':'", name[i], name);
1109 * XXX: Check more stuff: valid date and a valid reversed domain.
1111 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1112 if (strlen(name) != strlen("eui.") + 16)
1113 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1114 "should be followed by exactly 16 hexadecimal "
1116 for (i = strlen("eui."); name[i] != '\0'; i++) {
1117 if (!valid_hex(name[i])) {
1118 log_warnx("invalid character \"%c\" in target "
1119 "name \"%s\"; allowed characters are 1-9 "
1120 "and A-F", name[i], name);
1124 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1125 if (strlen(name) > strlen("naa.") + 32)
1126 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1127 "should be followed by at most 32 hexadecimal "
1129 for (i = strlen("naa."); name[i] != '\0'; i++) {
1130 if (!valid_hex(name[i])) {
1131 log_warnx("invalid character \"%c\" in target "
1132 "name \"%s\"; allowed characters are 1-9 "
1133 "and A-F", name[i], name);
1138 log_warnx("invalid target name \"%s\"; should start with "
1139 "either \"iqn.\", \"eui.\", or \"naa.\"",
1146 pport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1150 pp = calloc(1, sizeof(*pp));
1152 log_err(1, "calloc");
1154 pp->pp_name = checked_strdup(name);
1155 pp->pp_ctl_port = ctl_port;
1156 TAILQ_INIT(&pp->pp_ports);
1157 TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1162 pport_find(const struct conf *conf, const char *name)
1166 TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1167 if (strcasecmp(pp->pp_name, name) == 0)
1174 pport_copy(struct pport *pp, struct conf *conf)
1176 struct pport *ppnew;
1178 ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1183 pport_delete(struct pport *pp)
1185 struct port *port, *tport;
1187 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1189 TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1195 port_new(struct conf *conf, struct target *target, struct portal_group *pg)
1201 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1203 log_err(1, "asprintf");
1204 if (port_find(conf, name) != NULL) {
1205 log_warnx("duplicate port \"%s\"", name);
1209 port = calloc(1, sizeof(*port));
1211 log_err(1, "calloc");
1212 port->p_conf = conf;
1213 port->p_name = name;
1214 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1215 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1216 port->p_target = target;
1217 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1218 port->p_portal_group = pg;
1219 port->p_foreign = pg->pg_foreign;
1224 port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1230 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1232 log_err(1, "asprintf");
1233 if (port_find(conf, name) != NULL) {
1234 log_warnx("duplicate port \"%s\"", name);
1238 port = calloc(1, sizeof(*port));
1240 log_err(1, "calloc");
1241 port->p_conf = conf;
1242 port->p_name = name;
1243 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1244 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1245 port->p_target = target;
1246 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1252 port_find(const struct conf *conf, const char *name)
1256 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1257 if (strcasecmp(port->p_name, name) == 0)
1265 port_find_in_pg(const struct portal_group *pg, const char *target)
1269 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1270 if (strcasecmp(port->p_target->t_name, target) == 0)
1278 port_delete(struct port *port)
1281 if (port->p_portal_group)
1282 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1284 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1286 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1287 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1293 target_new(struct conf *conf, const char *name)
1295 struct target *targ;
1298 targ = target_find(conf, name);
1300 log_warnx("duplicated target \"%s\"", name);
1303 if (valid_iscsi_name(name) == false) {
1304 log_warnx("target name \"%s\" is invalid", name);
1307 targ = calloc(1, sizeof(*targ));
1309 log_err(1, "calloc");
1310 targ->t_name = checked_strdup(name);
1313 * RFC 3722 requires us to normalize the name to lowercase.
1316 for (i = 0; i < len; i++)
1317 targ->t_name[i] = tolower(targ->t_name[i]);
1319 targ->t_conf = conf;
1320 TAILQ_INIT(&targ->t_ports);
1321 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1327 target_delete(struct target *targ)
1329 struct port *port, *tport;
1331 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1333 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1336 free(targ->t_redirection);
1341 target_find(struct conf *conf, const char *name)
1343 struct target *targ;
1345 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1346 if (strcasecmp(targ->t_name, name) == 0)
1354 target_set_redirection(struct target *target, const char *addr)
1357 if (target->t_redirection != NULL) {
1358 log_warnx("cannot set redirection to \"%s\" for "
1359 "target \"%s\"; already defined",
1360 addr, target->t_name);
1364 target->t_redirection = checked_strdup(addr);
1370 lun_new(struct conf *conf, const char *name)
1374 lun = lun_find(conf, name);
1376 log_warnx("duplicated lun \"%s\"", name);
1380 lun = calloc(1, sizeof(*lun));
1382 log_err(1, "calloc");
1384 lun->l_name = checked_strdup(name);
1385 TAILQ_INIT(&lun->l_options);
1386 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1387 lun->l_ctl_lun = -1;
1393 lun_delete(struct lun *lun)
1395 struct target *targ;
1396 struct option *o, *tmp;
1399 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1400 for (i = 0; i < MAX_LUNS; i++) {
1401 if (targ->t_luns[i] == lun)
1402 targ->t_luns[i] = NULL;
1405 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1407 TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1408 option_delete(&lun->l_options, o);
1410 free(lun->l_backend);
1411 free(lun->l_device_id);
1413 free(lun->l_scsiname);
1414 free(lun->l_serial);
1419 lun_find(const struct conf *conf, const char *name)
1423 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1424 if (strcmp(lun->l_name, name) == 0)
1432 lun_set_backend(struct lun *lun, const char *value)
1434 free(lun->l_backend);
1435 lun->l_backend = checked_strdup(value);
1439 lun_set_blocksize(struct lun *lun, size_t value)
1442 lun->l_blocksize = value;
1446 lun_set_device_type(struct lun *lun, uint8_t value)
1449 lun->l_device_type = value;
1453 lun_set_device_id(struct lun *lun, const char *value)
1455 free(lun->l_device_id);
1456 lun->l_device_id = checked_strdup(value);
1460 lun_set_path(struct lun *lun, const char *value)
1463 lun->l_path = checked_strdup(value);
1467 lun_set_scsiname(struct lun *lun, const char *value)
1469 free(lun->l_scsiname);
1470 lun->l_scsiname = checked_strdup(value);
1474 lun_set_serial(struct lun *lun, const char *value)
1476 free(lun->l_serial);
1477 lun->l_serial = checked_strdup(value);
1481 lun_set_size(struct lun *lun, size_t value)
1484 lun->l_size = value;
1488 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1491 lun->l_ctl_lun = value;
1495 option_new(struct options *options, const char *name, const char *value)
1499 o = option_find(options, name);
1501 log_warnx("duplicated option \"%s\"", name);
1505 o = calloc(1, sizeof(*o));
1507 log_err(1, "calloc");
1508 o->o_name = checked_strdup(name);
1509 o->o_value = checked_strdup(value);
1510 TAILQ_INSERT_TAIL(options, o, o_next);
1516 option_delete(struct options *options, struct option *o)
1519 TAILQ_REMOVE(options, o, o_next);
1526 option_find(const struct options *options, const char *name)
1530 TAILQ_FOREACH(o, options, o_next) {
1531 if (strcmp(o->o_name, name) == 0)
1539 option_set(struct option *o, const char *value)
1543 o->o_value = checked_strdup(value);
1546 static struct connection *
1547 connection_new(struct portal *portal, int fd, const char *host,
1548 const struct sockaddr *client_sa)
1550 struct connection *conn;
1552 conn = calloc(1, sizeof(*conn));
1554 log_err(1, "calloc");
1555 conn->conn_portal = portal;
1556 conn->conn_socket = fd;
1557 conn->conn_initiator_addr = checked_strdup(host);
1558 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1561 * Default values, from RFC 3720, section 12.
1563 conn->conn_max_data_segment_length = 8192;
1564 conn->conn_max_burst_length = 262144;
1565 conn->conn_immediate_data = true;
1572 conf_print(struct conf *conf)
1574 struct auth_group *ag;
1576 struct auth_name *auth_name;
1577 struct auth_portal *auth_portal;
1578 struct portal_group *pg;
1579 struct portal *portal;
1580 struct target *targ;
1584 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1585 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1586 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1587 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1588 auth->a_user, auth->a_secret,
1589 auth->a_mutual_user, auth->a_mutual_secret);
1590 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1591 fprintf(stderr, "\t initiator-name %s\n",
1592 auth_name->an_initator_name);
1593 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1594 fprintf(stderr, "\t initiator-portal %s\n",
1595 auth_portal->an_initator_portal);
1596 fprintf(stderr, "}\n");
1598 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1599 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1600 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1601 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1602 fprintf(stderr, "}\n");
1604 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1605 fprintf(stderr, "\tlun %s {\n", lun->l_name);
1606 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1607 TAILQ_FOREACH(o, &lun->l_options, o_next)
1608 fprintf(stderr, "\t\toption %s %s\n",
1609 lo->o_name, lo->o_value);
1610 fprintf(stderr, "\t}\n");
1612 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1613 fprintf(stderr, "target %s {\n", targ->t_name);
1614 if (targ->t_alias != NULL)
1615 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1616 fprintf(stderr, "}\n");
1622 conf_verify_lun(struct lun *lun)
1624 const struct lun *lun2;
1626 if (lun->l_backend == NULL)
1627 lun_set_backend(lun, "block");
1628 if (strcmp(lun->l_backend, "block") == 0) {
1629 if (lun->l_path == NULL) {
1630 log_warnx("missing path for lun \"%s\"",
1634 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1635 if (lun->l_size == 0) {
1636 log_warnx("missing size for ramdisk-backed lun \"%s\"",
1640 if (lun->l_path != NULL) {
1641 log_warnx("path must not be specified "
1642 "for ramdisk-backed lun \"%s\"",
1647 if (lun->l_blocksize == 0) {
1648 if (lun->l_device_type == 5)
1649 lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1651 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1652 } else if (lun->l_blocksize < 0) {
1653 log_warnx("invalid blocksize for lun \"%s\"; "
1654 "must be larger than 0", lun->l_name);
1657 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1658 log_warnx("invalid size for lun \"%s\"; "
1659 "must be multiple of blocksize", lun->l_name);
1662 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1665 if (lun->l_path != NULL && lun2->l_path != NULL &&
1666 strcmp(lun->l_path, lun2->l_path) == 0) {
1667 log_debugx("WARNING: path \"%s\" duplicated "
1668 "between lun \"%s\", and "
1669 "lun \"%s\"", lun->l_path,
1670 lun->l_name, lun2->l_name);
1678 conf_verify(struct conf *conf)
1680 struct auth_group *ag;
1681 struct portal_group *pg;
1683 struct target *targ;
1688 if (conf->conf_pidfile_path == NULL)
1689 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1691 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1692 error = conf_verify_lun(lun);
1696 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1697 if (targ->t_auth_group == NULL) {
1698 targ->t_auth_group = auth_group_find(conf,
1700 assert(targ->t_auth_group != NULL);
1702 if (TAILQ_EMPTY(&targ->t_ports)) {
1703 pg = portal_group_find(conf, "default");
1705 port_new(conf, targ, pg);
1708 for (i = 0; i < MAX_LUNS; i++) {
1709 if (targ->t_luns[i] != NULL)
1712 if (!found && targ->t_redirection == NULL) {
1713 log_warnx("no LUNs defined for target \"%s\"",
1716 if (found && targ->t_redirection != NULL) {
1717 log_debugx("target \"%s\" contains luns, "
1718 " but configured for redirection",
1722 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1723 assert(pg->pg_name != NULL);
1724 if (pg->pg_discovery_auth_group == NULL) {
1725 pg->pg_discovery_auth_group =
1726 auth_group_find(conf, "default");
1727 assert(pg->pg_discovery_auth_group != NULL);
1730 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1731 pg->pg_discovery_filter = PG_FILTER_NONE;
1733 if (pg->pg_redirection != NULL) {
1734 if (!TAILQ_EMPTY(&pg->pg_ports)) {
1735 log_debugx("portal-group \"%s\" assigned "
1736 "to target, but configured "
1740 pg->pg_unassigned = false;
1741 } else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1742 pg->pg_unassigned = false;
1744 if (strcmp(pg->pg_name, "default") != 0)
1745 log_warnx("portal-group \"%s\" not assigned "
1746 "to any target", pg->pg_name);
1747 pg->pg_unassigned = true;
1750 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1751 if (ag->ag_name == NULL)
1752 assert(ag->ag_target != NULL);
1754 assert(ag->ag_target == NULL);
1757 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1758 if (targ->t_auth_group == ag) {
1763 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1764 if (port->p_auth_group == ag) {
1769 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1770 if (pg->pg_discovery_auth_group == ag) {
1775 if (!found && ag->ag_name != NULL &&
1776 strcmp(ag->ag_name, "default") != 0 &&
1777 strcmp(ag->ag_name, "no-authentication") != 0 &&
1778 strcmp(ag->ag_name, "no-access") != 0) {
1779 log_warnx("auth-group \"%s\" not assigned "
1780 "to any target", ag->ag_name);
1788 conf_apply(struct conf *oldconf, struct conf *newconf)
1790 struct lun *oldlun, *newlun, *tmplun;
1791 struct portal_group *oldpg, *newpg;
1792 struct portal *oldp, *newp;
1793 struct port *oldport, *newport, *tmpport;
1794 struct isns *oldns, *newns;
1796 int changed, cumulated_error = 0, error, sockbuf;
1799 if (oldconf->conf_debug != newconf->conf_debug) {
1800 log_debugx("changing debug level to %d", newconf->conf_debug);
1801 log_init(newconf->conf_debug);
1804 if (oldconf->conf_pidfh != NULL) {
1805 assert(oldconf->conf_pidfile_path != NULL);
1806 if (newconf->conf_pidfile_path != NULL &&
1807 strcmp(oldconf->conf_pidfile_path,
1808 newconf->conf_pidfile_path) == 0) {
1809 newconf->conf_pidfh = oldconf->conf_pidfh;
1810 oldconf->conf_pidfh = NULL;
1812 log_debugx("removing pidfile %s",
1813 oldconf->conf_pidfile_path);
1814 pidfile_remove(oldconf->conf_pidfh);
1815 oldconf->conf_pidfh = NULL;
1819 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1820 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1821 newconf->conf_pidfh =
1822 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1823 if (newconf->conf_pidfh == NULL) {
1824 if (errno == EEXIST)
1825 log_errx(1, "daemon already running, pid: %jd.",
1826 (intmax_t)otherpid);
1827 log_err(1, "cannot open or create pidfile \"%s\"",
1828 newconf->conf_pidfile_path);
1833 * Go through the new portal groups, assigning tags or preserving old.
1835 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1836 if (newpg->pg_tag != 0)
1838 oldpg = portal_group_find(oldconf, newpg->pg_name);
1840 newpg->pg_tag = oldpg->pg_tag;
1842 newpg->pg_tag = ++last_portal_group_tag;
1845 /* Deregister on removed iSNS servers. */
1846 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1847 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1848 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1852 isns_deregister(oldns);
1856 * XXX: If target or lun removal fails, we should somehow "move"
1857 * the old lun or target into newconf, so that subsequent
1858 * conf_apply() would try to remove them again. That would
1859 * be somewhat hairy, though, and lun deletion failures don't
1860 * really happen, so leave it as it is for now.
1863 * First, remove any ports present in the old configuration
1864 * and missing in the new one.
1866 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1867 if (oldport->p_foreign)
1869 newport = port_find(newconf, oldport->p_name);
1870 if (newport != NULL && !newport->p_foreign)
1872 log_debugx("removing port \"%s\"", oldport->p_name);
1873 error = kernel_port_remove(oldport);
1875 log_warnx("failed to remove port %s",
1878 * XXX: Uncomment after fixing the root cause.
1880 * cumulated_error++;
1886 * Second, remove any LUNs present in the old configuration
1887 * and missing in the new one.
1889 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1890 newlun = lun_find(newconf, oldlun->l_name);
1891 if (newlun == NULL) {
1892 log_debugx("lun \"%s\", CTL lun %d "
1893 "not found in new configuration; "
1894 "removing", oldlun->l_name, oldlun->l_ctl_lun);
1895 error = kernel_lun_remove(oldlun);
1897 log_warnx("failed to remove lun \"%s\", "
1899 oldlun->l_name, oldlun->l_ctl_lun);
1906 * Also remove the LUNs changed by more than size.
1909 assert(oldlun->l_backend != NULL);
1910 assert(newlun->l_backend != NULL);
1911 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1912 log_debugx("backend for lun \"%s\", "
1913 "CTL lun %d changed; removing",
1914 oldlun->l_name, oldlun->l_ctl_lun);
1917 if (oldlun->l_blocksize != newlun->l_blocksize) {
1918 log_debugx("blocksize for lun \"%s\", "
1919 "CTL lun %d changed; removing",
1920 oldlun->l_name, oldlun->l_ctl_lun);
1923 if (newlun->l_device_id != NULL &&
1924 (oldlun->l_device_id == NULL ||
1925 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1927 log_debugx("device-id for lun \"%s\", "
1928 "CTL lun %d changed; removing",
1929 oldlun->l_name, oldlun->l_ctl_lun);
1932 if (newlun->l_path != NULL &&
1933 (oldlun->l_path == NULL ||
1934 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1935 log_debugx("path for lun \"%s\", "
1936 "CTL lun %d, changed; removing",
1937 oldlun->l_name, oldlun->l_ctl_lun);
1940 if (newlun->l_serial != NULL &&
1941 (oldlun->l_serial == NULL ||
1942 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1943 log_debugx("serial for lun \"%s\", "
1944 "CTL lun %d changed; removing",
1945 oldlun->l_name, oldlun->l_ctl_lun);
1949 error = kernel_lun_remove(oldlun);
1951 log_warnx("failed to remove lun \"%s\", "
1953 oldlun->l_name, oldlun->l_ctl_lun);
1960 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1963 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1964 oldlun = lun_find(oldconf, newlun->l_name);
1965 if (oldlun != NULL) {
1966 log_debugx("modifying lun \"%s\", CTL lun %d",
1967 newlun->l_name, newlun->l_ctl_lun);
1968 error = kernel_lun_modify(newlun);
1970 log_warnx("failed to "
1971 "modify lun \"%s\", CTL lun %d",
1972 newlun->l_name, newlun->l_ctl_lun);
1977 log_debugx("adding lun \"%s\"", newlun->l_name);
1978 error = kernel_lun_add(newlun);
1980 log_warnx("failed to add lun \"%s\"", newlun->l_name);
1987 * Now add new ports or modify existing ones.
1989 TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
1990 if (newport->p_foreign)
1992 oldport = port_find(oldconf, newport->p_name);
1994 if (oldport == NULL || oldport->p_foreign) {
1995 log_debugx("adding port \"%s\"", newport->p_name);
1996 error = kernel_port_add(newport);
1998 log_debugx("updating port \"%s\"", newport->p_name);
1999 newport->p_ctl_port = oldport->p_ctl_port;
2000 error = kernel_port_update(newport, oldport);
2003 log_warnx("failed to %s port %s",
2004 (oldport == NULL) ? "add" : "update",
2007 * XXX: Uncomment after fixing the root cause.
2009 * cumulated_error++;
2015 * Go through the new portals, opening the sockets as necessary.
2017 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2018 if (newpg->pg_foreign)
2020 if (newpg->pg_unassigned) {
2021 log_debugx("not listening on portal-group \"%s\", "
2022 "not assigned to any target",
2026 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2028 * Try to find already open portal and reuse
2029 * the listening socket. We don't care about
2030 * what portal or portal group that was, what
2031 * matters is the listening address.
2033 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2035 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2037 if (strcmp(newp->p_listen,
2038 oldp->p_listen) == 0 &&
2039 oldp->p_socket > 0) {
2047 if (newp->p_socket > 0) {
2049 * We're done with this portal.
2054 #ifdef ICL_KERNEL_PROXY
2056 newpg->pg_conf->conf_portal_id++;
2057 newp->p_id = newpg->pg_conf->conf_portal_id;
2058 log_debugx("listening on %s, portal-group "
2059 "\"%s\", portal id %d, using ICL proxy",
2060 newp->p_listen, newpg->pg_name, newp->p_id);
2061 kernel_listen(newp->p_ai, newp->p_iser,
2066 assert(proxy_mode == false);
2067 assert(newp->p_iser == false);
2069 log_debugx("listening on %s, portal-group \"%s\"",
2070 newp->p_listen, newpg->pg_name);
2071 newp->p_socket = socket(newp->p_ai->ai_family,
2072 newp->p_ai->ai_socktype,
2073 newp->p_ai->ai_protocol);
2074 if (newp->p_socket < 0) {
2075 log_warn("socket(2) failed for %s",
2080 sockbuf = SOCKBUF_SIZE;
2081 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2082 &sockbuf, sizeof(sockbuf)) == -1)
2083 log_warn("setsockopt(SO_RCVBUF) failed "
2084 "for %s", newp->p_listen);
2085 sockbuf = SOCKBUF_SIZE;
2086 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2087 &sockbuf, sizeof(sockbuf)) == -1)
2088 log_warn("setsockopt(SO_SNDBUF) failed "
2089 "for %s", newp->p_listen);
2090 error = setsockopt(newp->p_socket, SOL_SOCKET,
2091 SO_REUSEADDR, &one, sizeof(one));
2093 log_warn("setsockopt(SO_REUSEADDR) failed "
2094 "for %s", newp->p_listen);
2095 close(newp->p_socket);
2100 error = bind(newp->p_socket, newp->p_ai->ai_addr,
2101 newp->p_ai->ai_addrlen);
2103 log_warn("bind(2) failed for %s",
2105 close(newp->p_socket);
2110 error = listen(newp->p_socket, -1);
2112 log_warn("listen(2) failed for %s",
2114 close(newp->p_socket);
2123 * Go through the no longer used sockets, closing them.
2125 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2126 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2127 if (oldp->p_socket <= 0)
2129 log_debugx("closing socket for %s, portal-group \"%s\"",
2130 oldp->p_listen, oldpg->pg_name);
2131 close(oldp->p_socket);
2136 /* (Re-)Register on remaining/new iSNS servers. */
2137 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2138 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2139 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2142 isns_register(newns, oldns);
2145 /* Schedule iSNS update */
2146 if (!TAILQ_EMPTY(&newconf->conf_isns))
2147 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2149 return (cumulated_error);
2156 return (sigalrm_received);
2160 sigalrm_handler_fatal(int dummy __unused)
2163 * It would be easiest to just log an error and exit. We can't
2164 * do this, though, because log_errx() is not signal safe, since
2165 * it calls syslog(3). Instead, set a flag checked by pdu_send()
2166 * and pdu_receive(), to call log_errx() there. Should they fail
2167 * to notice, we'll exit here one second later.
2169 if (sigalrm_received) {
2171 * Oh well. Just give up and quit.
2176 sigalrm_received = true;
2180 sigalrm_handler(int dummy __unused)
2183 sigalrm_received = true;
2187 set_timeout(int timeout, int fatal)
2189 struct sigaction sa;
2190 struct itimerval itv;
2194 log_debugx("session timeout disabled");
2195 bzero(&itv, sizeof(itv));
2196 error = setitimer(ITIMER_REAL, &itv, NULL);
2198 log_err(1, "setitimer");
2199 sigalrm_received = false;
2203 sigalrm_received = false;
2204 bzero(&sa, sizeof(sa));
2206 sa.sa_handler = sigalrm_handler_fatal;
2208 sa.sa_handler = sigalrm_handler;
2209 sigfillset(&sa.sa_mask);
2210 error = sigaction(SIGALRM, &sa, NULL);
2212 log_err(1, "sigaction");
2215 * First SIGALRM will arive after conf_timeout seconds.
2216 * If we do nothing, another one will arrive a second later.
2218 log_debugx("setting session timeout to %d seconds", timeout);
2219 bzero(&itv, sizeof(itv));
2220 itv.it_interval.tv_sec = 1;
2221 itv.it_value.tv_sec = timeout;
2222 error = setitimer(ITIMER_REAL, &itv, NULL);
2224 log_err(1, "setitimer");
2228 wait_for_children(bool block)
2236 * If "block" is true, wait for at least one process.
2238 if (block && num == 0)
2239 pid = wait4(-1, &status, 0, NULL);
2241 pid = wait4(-1, &status, WNOHANG, NULL);
2244 if (WIFSIGNALED(status)) {
2245 log_warnx("child process %d terminated with signal %d",
2246 pid, WTERMSIG(status));
2247 } else if (WEXITSTATUS(status) != 0) {
2248 log_warnx("child process %d terminated with exit status %d",
2249 pid, WEXITSTATUS(status));
2251 log_debugx("child process %d terminated gracefully", pid);
2260 handle_connection(struct portal *portal, int fd,
2261 const struct sockaddr *client_sa, bool dont_fork)
2263 struct connection *conn;
2266 char host[NI_MAXHOST + 1];
2269 conf = portal->p_portal_group->pg_conf;
2272 log_debugx("incoming connection; not forking due to -d flag");
2274 nchildren -= wait_for_children(false);
2275 assert(nchildren >= 0);
2277 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2278 log_debugx("maxproc limit of %d child processes hit; "
2279 "waiting for child process to exit", conf->conf_maxproc);
2280 nchildren -= wait_for_children(true);
2281 assert(nchildren >= 0);
2283 log_debugx("incoming connection; forking child process #%d",
2294 pidfile_close(conf->conf_pidfh);
2296 error = getnameinfo(client_sa, client_sa->sa_len,
2297 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2299 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2301 log_debugx("accepted connection from %s; portal group \"%s\"",
2302 host, portal->p_portal_group->pg_name);
2303 log_set_peer_addr(host);
2304 setproctitle("%s", host);
2306 conn = connection_new(portal, fd, host, client_sa);
2307 set_timeout(conf->conf_timeout, true);
2310 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2311 kernel_handoff(conn);
2312 log_debugx("connection handed off to the kernel");
2314 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2317 log_debugx("nothing more to do; exiting");
2322 fd_add(int fd, fd_set *fdset, int nfds)
2326 * Skip sockets which we failed to bind.
2338 main_loop(struct conf *conf, bool dont_fork)
2340 struct portal_group *pg;
2341 struct portal *portal;
2342 struct sockaddr_storage client_sa;
2343 socklen_t client_salen;
2344 #ifdef ICL_KERNEL_PROXY
2349 int error, nfds, client_fd;
2351 pidfile_write(conf->conf_pidfh);
2354 if (sighup_received || sigterm_received || timed_out())
2357 #ifdef ICL_KERNEL_PROXY
2359 client_salen = sizeof(client_sa);
2360 kernel_accept(&connection_id, &portal_id,
2361 (struct sockaddr *)&client_sa, &client_salen);
2362 assert(client_salen >= client_sa.ss_len);
2364 log_debugx("incoming connection, id %d, portal id %d",
2365 connection_id, portal_id);
2366 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2367 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2368 if (portal->p_id == portal_id) {
2374 log_errx(1, "kernel returned invalid portal_id %d",
2378 handle_connection(portal, connection_id,
2379 (struct sockaddr *)&client_sa, dont_fork);
2382 assert(proxy_mode == false);
2386 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2387 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2388 nfds = fd_add(portal->p_socket, &fdset, nfds);
2390 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2394 log_err(1, "select");
2396 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2397 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2398 if (!FD_ISSET(portal->p_socket, &fdset))
2400 client_salen = sizeof(client_sa);
2401 client_fd = accept(portal->p_socket,
2402 (struct sockaddr *)&client_sa,
2404 if (client_fd < 0) {
2405 if (errno == ECONNABORTED)
2407 log_err(1, "accept");
2409 assert(client_salen >= client_sa.ss_len);
2411 handle_connection(portal, client_fd,
2412 (struct sockaddr *)&client_sa,
2417 #ifdef ICL_KERNEL_PROXY
2424 sighup_handler(int dummy __unused)
2427 sighup_received = true;
2431 sigterm_handler(int dummy __unused)
2434 sigterm_received = true;
2438 sigchld_handler(int dummy __unused)
2442 * The only purpose of this handler is to make SIGCHLD
2443 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2444 * wait_for_children().
2449 register_signals(void)
2451 struct sigaction sa;
2454 bzero(&sa, sizeof(sa));
2455 sa.sa_handler = sighup_handler;
2456 sigfillset(&sa.sa_mask);
2457 error = sigaction(SIGHUP, &sa, NULL);
2459 log_err(1, "sigaction");
2461 sa.sa_handler = sigterm_handler;
2462 error = sigaction(SIGTERM, &sa, NULL);
2464 log_err(1, "sigaction");
2466 sa.sa_handler = sigterm_handler;
2467 error = sigaction(SIGINT, &sa, NULL);
2469 log_err(1, "sigaction");
2471 sa.sa_handler = sigchld_handler;
2472 error = sigaction(SIGCHLD, &sa, NULL);
2474 log_err(1, "sigaction");
2478 main(int argc, char **argv)
2480 struct conf *oldconf, *newconf, *tmpconf;
2482 const char *config_path = DEFAULT_CONFIG_PATH;
2483 int debug = 0, ch, error;
2484 bool dont_daemonize = false;
2486 while ((ch = getopt(argc, argv, "df:R")) != -1) {
2489 dont_daemonize = true;
2493 config_path = optarg;
2496 #ifndef ICL_KERNEL_PROXY
2497 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2498 "does not support iSER protocol");
2514 oldconf = conf_new_from_kernel();
2515 newconf = conf_new_from_file(config_path, oldconf);
2516 if (newconf == NULL)
2517 log_errx(1, "configuration error; exiting");
2519 oldconf->conf_debug = debug;
2520 newconf->conf_debug = debug;
2523 error = conf_apply(oldconf, newconf);
2525 log_errx(1, "failed to apply configuration; exiting");
2527 conf_delete(oldconf);
2532 if (dont_daemonize == false) {
2533 log_debugx("daemonizing");
2534 if (daemon(0, 0) == -1) {
2535 log_warn("cannot daemonize");
2536 pidfile_remove(newconf->conf_pidfh);
2541 /* Schedule iSNS update */
2542 if (!TAILQ_EMPTY(&newconf->conf_isns))
2543 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2546 main_loop(newconf, dont_daemonize);
2547 if (sighup_received) {
2548 sighup_received = false;
2549 log_debugx("received SIGHUP, reloading configuration");
2550 tmpconf = conf_new_from_file(config_path, newconf);
2551 if (tmpconf == NULL) {
2552 log_warnx("configuration error, "
2553 "continuing with old configuration");
2556 tmpconf->conf_debug = debug;
2559 error = conf_apply(oldconf, newconf);
2561 log_warnx("failed to reload "
2563 conf_delete(oldconf);
2566 } else if (sigterm_received) {
2567 log_debugx("exiting on signal; "
2568 "reloading empty configuration");
2570 log_debugx("removing CTL iSCSI ports "
2571 "and terminating all connections");
2574 newconf = conf_new();
2576 newconf->conf_debug = debug;
2577 error = conf_apply(oldconf, newconf);
2579 log_warnx("failed to apply configuration");
2580 conf_delete(oldconf);
2583 log_warnx("exiting on signal");
2586 nchildren -= wait_for_children(false);
2587 assert(nchildren >= 0);
2589 set_timeout(0, false);
2590 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2592 /* Schedule iSNS update */
2593 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2594 set_timeout((newconf->conf_isns_period