2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
58 bool proxy_mode = false;
60 static volatile bool sighup_received = false;
61 static volatile bool sigterm_received = false;
62 static volatile bool sigalrm_received = false;
64 static int nchildren = 0;
65 static uint16_t last_portal_group_tag = 0xff;
71 fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n");
72 fprintf(stderr, " ctld -t [-u][-f config-file]\n");
77 checked_strdup(const char *s)
92 conf = calloc(1, sizeof(*conf));
95 TAILQ_INIT(&conf->conf_luns);
96 TAILQ_INIT(&conf->conf_targets);
97 TAILQ_INIT(&conf->conf_auth_groups);
98 TAILQ_INIT(&conf->conf_ports);
99 TAILQ_INIT(&conf->conf_portal_groups);
100 TAILQ_INIT(&conf->conf_pports);
101 TAILQ_INIT(&conf->conf_isns);
103 conf->conf_isns_period = 900;
104 conf->conf_isns_timeout = 5;
105 conf->conf_debug = 0;
106 conf->conf_timeout = 60;
107 conf->conf_maxproc = 30;
113 conf_delete(struct conf *conf)
115 struct lun *lun, *ltmp;
116 struct target *targ, *tmp;
117 struct auth_group *ag, *cagtmp;
118 struct portal_group *pg, *cpgtmp;
119 struct pport *pp, *pptmp;
120 struct isns *is, *istmp;
122 assert(conf->conf_pidfh == NULL);
124 TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
126 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
128 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
129 auth_group_delete(ag);
130 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
131 portal_group_delete(pg);
132 TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
134 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
136 assert(TAILQ_EMPTY(&conf->conf_ports));
137 free(conf->conf_pidfile_path);
142 auth_new(struct auth_group *ag)
146 auth = calloc(1, sizeof(*auth));
148 log_err(1, "calloc");
149 auth->a_auth_group = ag;
150 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
155 auth_delete(struct auth *auth)
157 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
160 free(auth->a_secret);
161 free(auth->a_mutual_user);
162 free(auth->a_mutual_secret);
167 auth_find(const struct auth_group *ag, const char *user)
169 const struct auth *auth;
171 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
172 if (strcmp(auth->a_user, user) == 0)
180 auth_check_secret_length(struct auth *auth)
184 len = strlen(auth->a_secret);
186 if (auth->a_auth_group->ag_name != NULL)
187 log_warnx("secret for user \"%s\", auth-group \"%s\", "
188 "is too long; it should be at most 16 characters "
189 "long", auth->a_user, auth->a_auth_group->ag_name);
191 log_warnx("secret for user \"%s\", target \"%s\", "
192 "is too long; it should be at most 16 characters "
193 "long", auth->a_user,
194 auth->a_auth_group->ag_target->t_name);
197 if (auth->a_auth_group->ag_name != NULL)
198 log_warnx("secret for user \"%s\", auth-group \"%s\", "
199 "is too short; it should be at least 12 characters "
200 "long", auth->a_user,
201 auth->a_auth_group->ag_name);
203 log_warnx("secret for user \"%s\", target \"%s\", "
204 "is too short; it should be at least 12 characters "
205 "long", auth->a_user,
206 auth->a_auth_group->ag_target->t_name);
209 if (auth->a_mutual_secret != NULL) {
210 len = strlen(auth->a_mutual_secret);
212 if (auth->a_auth_group->ag_name != NULL)
213 log_warnx("mutual secret for user \"%s\", "
214 "auth-group \"%s\", is too long; it should "
215 "be at most 16 characters long",
216 auth->a_user, auth->a_auth_group->ag_name);
218 log_warnx("mutual secret for user \"%s\", "
219 "target \"%s\", is too long; it should "
220 "be at most 16 characters long",
222 auth->a_auth_group->ag_target->t_name);
225 if (auth->a_auth_group->ag_name != NULL)
226 log_warnx("mutual secret for user \"%s\", "
227 "auth-group \"%s\", is too short; it "
228 "should be at least 12 characters long",
229 auth->a_user, auth->a_auth_group->ag_name);
231 log_warnx("mutual secret for user \"%s\", "
232 "target \"%s\", is too short; it should be "
233 "at least 12 characters long",
235 auth->a_auth_group->ag_target->t_name);
241 auth_new_chap(struct auth_group *ag, const char *user,
246 if (ag->ag_type == AG_TYPE_UNKNOWN)
247 ag->ag_type = AG_TYPE_CHAP;
248 if (ag->ag_type != AG_TYPE_CHAP) {
249 if (ag->ag_name != NULL)
250 log_warnx("cannot mix \"chap\" authentication with "
251 "other types for auth-group \"%s\"", ag->ag_name);
253 log_warnx("cannot mix \"chap\" authentication with "
254 "other types for target \"%s\"",
255 ag->ag_target->t_name);
260 auth->a_user = checked_strdup(user);
261 auth->a_secret = checked_strdup(secret);
263 auth_check_secret_length(auth);
269 auth_new_chap_mutual(struct auth_group *ag, const char *user,
270 const char *secret, const char *user2, const char *secret2)
274 if (ag->ag_type == AG_TYPE_UNKNOWN)
275 ag->ag_type = AG_TYPE_CHAP_MUTUAL;
276 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
277 if (ag->ag_name != NULL)
278 log_warnx("cannot mix \"chap-mutual\" authentication "
279 "with other types for auth-group \"%s\"",
282 log_warnx("cannot mix \"chap-mutual\" authentication "
283 "with other types for target \"%s\"",
284 ag->ag_target->t_name);
289 auth->a_user = checked_strdup(user);
290 auth->a_secret = checked_strdup(secret);
291 auth->a_mutual_user = checked_strdup(user2);
292 auth->a_mutual_secret = checked_strdup(secret2);
294 auth_check_secret_length(auth);
299 const struct auth_name *
300 auth_name_new(struct auth_group *ag, const char *name)
302 struct auth_name *an;
304 an = calloc(1, sizeof(*an));
306 log_err(1, "calloc");
307 an->an_auth_group = ag;
308 an->an_initator_name = checked_strdup(name);
309 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
314 auth_name_delete(struct auth_name *an)
316 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
318 free(an->an_initator_name);
323 auth_name_defined(const struct auth_group *ag)
325 if (TAILQ_EMPTY(&ag->ag_names))
330 const struct auth_name *
331 auth_name_find(const struct auth_group *ag, const char *name)
333 const struct auth_name *auth_name;
335 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
336 if (strcmp(auth_name->an_initator_name, name) == 0)
344 auth_name_check(const struct auth_group *ag, const char *initiator_name)
346 if (!auth_name_defined(ag))
349 if (auth_name_find(ag, initiator_name) == NULL)
355 const struct auth_portal *
356 auth_portal_new(struct auth_group *ag, const char *portal)
358 struct auth_portal *ap;
359 char *net, *mask, *str, *tmp;
362 ap = calloc(1, sizeof(*ap));
364 log_err(1, "calloc");
365 ap->ap_auth_group = ag;
366 ap->ap_initator_portal = checked_strdup(portal);
367 mask = str = checked_strdup(portal);
368 net = strsep(&mask, "/");
374 if (net[len - 1] == ']')
376 if (strchr(net, ':') != NULL) {
377 struct sockaddr_in6 *sin6 =
378 (struct sockaddr_in6 *)&ap->ap_sa;
380 sin6->sin6_len = sizeof(*sin6);
381 sin6->sin6_family = AF_INET6;
382 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
386 struct sockaddr_in *sin =
387 (struct sockaddr_in *)&ap->ap_sa;
389 sin->sin_len = sizeof(*sin);
390 sin->sin_family = AF_INET;
391 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
396 m = strtol(mask, &tmp, 0);
397 if (m < 0 || m > dm || tmp[0] != 0)
403 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
409 log_warnx("incorrect initiator portal \"%s\"", portal);
414 auth_portal_delete(struct auth_portal *ap)
416 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
418 free(ap->ap_initator_portal);
423 auth_portal_defined(const struct auth_group *ag)
425 if (TAILQ_EMPTY(&ag->ag_portals))
430 const struct auth_portal *
431 auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
433 const struct auth_portal *ap;
434 const uint8_t *a, *b;
438 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
439 if (ap->ap_sa.ss_family != ss->ss_family)
441 if (ss->ss_family == AF_INET) {
442 a = (const uint8_t *)
443 &((const struct sockaddr_in *)ss)->sin_addr;
444 b = (const uint8_t *)
445 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
447 a = (const uint8_t *)
448 &((const struct sockaddr_in6 *)ss)->sin6_addr;
449 b = (const uint8_t *)
450 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
452 for (i = 0; i < ap->ap_mask / 8; i++) {
456 if (ap->ap_mask % 8) {
457 bmask = 0xff << (8 - (ap->ap_mask % 8));
458 if ((a[i] & bmask) != (b[i] & bmask))
470 auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
473 if (!auth_portal_defined(ag))
476 if (auth_portal_find(ag, sa) == NULL)
483 auth_group_new(struct conf *conf, const char *name)
485 struct auth_group *ag;
488 ag = auth_group_find(conf, name);
490 log_warnx("duplicated auth-group \"%s\"", name);
495 ag = calloc(1, sizeof(*ag));
497 log_err(1, "calloc");
499 ag->ag_name = checked_strdup(name);
500 TAILQ_INIT(&ag->ag_auths);
501 TAILQ_INIT(&ag->ag_names);
502 TAILQ_INIT(&ag->ag_portals);
504 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
510 auth_group_delete(struct auth_group *ag)
512 struct auth *auth, *auth_tmp;
513 struct auth_name *auth_name, *auth_name_tmp;
514 struct auth_portal *auth_portal, *auth_portal_tmp;
516 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
518 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
520 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
521 auth_name_delete(auth_name);
522 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
524 auth_portal_delete(auth_portal);
530 auth_group_find(const struct conf *conf, const char *name)
532 struct auth_group *ag;
534 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
535 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
543 auth_group_set_type(struct auth_group *ag, const char *str)
547 if (strcmp(str, "none") == 0) {
548 type = AG_TYPE_NO_AUTHENTICATION;
549 } else if (strcmp(str, "deny") == 0) {
551 } else if (strcmp(str, "chap") == 0) {
553 } else if (strcmp(str, "chap-mutual") == 0) {
554 type = AG_TYPE_CHAP_MUTUAL;
556 if (ag->ag_name != NULL)
557 log_warnx("invalid auth-type \"%s\" for auth-group "
558 "\"%s\"", str, ag->ag_name);
560 log_warnx("invalid auth-type \"%s\" for target "
561 "\"%s\"", str, ag->ag_target->t_name);
565 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
566 if (ag->ag_name != NULL) {
567 log_warnx("cannot set auth-type to \"%s\" for "
568 "auth-group \"%s\"; already has a different "
569 "type", str, ag->ag_name);
571 log_warnx("cannot set auth-type to \"%s\" for target "
572 "\"%s\"; already has a different type",
573 str, ag->ag_target->t_name);
583 static struct portal *
584 portal_new(struct portal_group *pg)
586 struct portal *portal;
588 portal = calloc(1, sizeof(*portal));
590 log_err(1, "calloc");
591 TAILQ_INIT(&portal->p_targets);
592 portal->p_portal_group = pg;
593 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
598 portal_delete(struct portal *portal)
601 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
602 if (portal->p_ai != NULL)
603 freeaddrinfo(portal->p_ai);
604 free(portal->p_listen);
608 struct portal_group *
609 portal_group_new(struct conf *conf, const char *name)
611 struct portal_group *pg;
613 pg = portal_group_find(conf, name);
615 log_warnx("duplicated portal-group \"%s\"", name);
619 pg = calloc(1, sizeof(*pg));
621 log_err(1, "calloc");
622 pg->pg_name = checked_strdup(name);
623 TAILQ_INIT(&pg->pg_options);
624 TAILQ_INIT(&pg->pg_portals);
625 TAILQ_INIT(&pg->pg_ports);
627 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
628 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
634 portal_group_delete(struct portal_group *pg)
636 struct portal *portal, *tmp;
637 struct port *port, *tport;
638 struct option *o, *otmp;
640 TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
642 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
644 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
645 portal_delete(portal);
646 TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
647 option_delete(&pg->pg_options, o);
649 free(pg->pg_offload);
650 free(pg->pg_redirection);
654 struct portal_group *
655 portal_group_find(const struct conf *conf, const char *name)
657 struct portal_group *pg;
659 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
660 if (strcmp(pg->pg_name, name) == 0)
668 parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
670 struct addrinfo hints;
671 char *str, *addr, *ch;
673 int error, colons = 0;
675 str = arg = strdup(arg);
678 * IPv6 address in square brackets, perhaps with port.
681 addr = strsep(&arg, "]");
686 if (arg[0] == '\0') {
688 } else if (arg[0] == ':') {
696 * Either IPv6 address without brackets - and without
697 * a port - or IPv4 address. Just count the colons.
699 for (ch = arg; *ch != '\0'; ch++) {
707 addr = strsep(&arg, ":");
715 memset(&hints, 0, sizeof(hints));
716 hints.ai_family = PF_UNSPEC;
717 hints.ai_socktype = SOCK_STREAM;
718 hints.ai_flags = AI_PASSIVE;
719 error = getaddrinfo(addr, port, &hints, ai);
721 return ((error != 0) ? 1 : 0);
725 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
727 struct portal *portal;
729 portal = portal_new(pg);
730 portal->p_listen = checked_strdup(value);
731 portal->p_iser = iser;
733 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
734 log_warnx("invalid listen address %s", portal->p_listen);
735 portal_delete(portal);
740 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
741 * those into multiple portals.
748 isns_new(struct conf *conf, const char *addr)
752 isns = calloc(1, sizeof(*isns));
754 log_err(1, "calloc");
756 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
757 isns->i_addr = checked_strdup(addr);
759 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
760 log_warnx("invalid iSNS address %s", isns->i_addr);
766 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
767 * those into multiple servers.
774 isns_delete(struct isns *isns)
777 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
779 if (isns->i_ai != NULL)
780 freeaddrinfo(isns->i_ai);
785 isns_do_connect(struct isns *isns)
789 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
790 isns->i_ai->ai_protocol);
792 log_warn("socket(2) failed for %s", isns->i_addr);
795 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
796 log_warn("connect(2) failed for %s", isns->i_addr);
804 isns_do_register(struct isns *isns, int s, const char *hostname)
806 struct conf *conf = isns->i_conf;
807 struct target *target;
808 struct portal *portal;
809 struct portal_group *pg;
811 struct isns_req *req;
815 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
816 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
817 isns_req_add_delim(req);
818 isns_req_add_str(req, 1, hostname);
819 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
820 isns_req_add_32(req, 6, conf->conf_isns_period);
821 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
822 if (pg->pg_unassigned)
824 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
825 isns_req_add_addr(req, 16, portal->p_ai);
826 isns_req_add_port(req, 17, portal->p_ai);
829 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
830 isns_req_add_str(req, 32, target->t_name);
831 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
832 if (target->t_alias != NULL)
833 isns_req_add_str(req, 34, target->t_alias);
834 TAILQ_FOREACH(port, &target->t_ports, p_ts) {
835 if ((pg = port->p_portal_group) == NULL)
837 isns_req_add_32(req, 51, pg->pg_tag);
838 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
839 isns_req_add_addr(req, 49, portal->p_ai);
840 isns_req_add_port(req, 50, portal->p_ai);
844 res = isns_req_send(s, req);
846 log_warn("send(2) failed for %s", isns->i_addr);
849 res = isns_req_receive(s, req);
851 log_warn("receive(2) failed for %s", isns->i_addr);
854 error = isns_req_get_status(req);
856 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
865 isns_do_check(struct isns *isns, int s, const char *hostname)
867 struct conf *conf = isns->i_conf;
868 struct isns_req *req;
872 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
873 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
874 isns_req_add_str(req, 1, hostname);
875 isns_req_add_delim(req);
876 isns_req_add(req, 2, 0, NULL);
877 res = isns_req_send(s, req);
879 log_warn("send(2) failed for %s", isns->i_addr);
882 res = isns_req_receive(s, req);
884 log_warn("receive(2) failed for %s", isns->i_addr);
887 error = isns_req_get_status(req);
889 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
898 isns_do_deregister(struct isns *isns, int s, const char *hostname)
900 struct conf *conf = isns->i_conf;
901 struct isns_req *req;
905 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
906 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
907 isns_req_add_delim(req);
908 isns_req_add_str(req, 1, hostname);
909 res = isns_req_send(s, req);
911 log_warn("send(2) failed for %s", isns->i_addr);
914 res = isns_req_receive(s, req);
916 log_warn("receive(2) failed for %s", isns->i_addr);
919 error = isns_req_get_status(req);
921 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
930 isns_register(struct isns *isns, struct isns *oldisns)
932 struct conf *conf = isns->i_conf;
936 if (TAILQ_EMPTY(&conf->conf_targets) ||
937 TAILQ_EMPTY(&conf->conf_portal_groups))
939 set_timeout(conf->conf_isns_timeout, false);
940 s = isns_do_connect(isns);
942 set_timeout(0, false);
945 gethostname(hostname, sizeof(hostname));
947 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
949 isns_do_deregister(oldisns, s, hostname);
950 isns_do_register(isns, s, hostname);
952 set_timeout(0, false);
956 isns_check(struct isns *isns)
958 struct conf *conf = isns->i_conf;
962 if (TAILQ_EMPTY(&conf->conf_targets) ||
963 TAILQ_EMPTY(&conf->conf_portal_groups))
965 set_timeout(conf->conf_isns_timeout, false);
966 s = isns_do_connect(isns);
968 set_timeout(0, false);
971 gethostname(hostname, sizeof(hostname));
973 res = isns_do_check(isns, s, hostname);
975 isns_do_deregister(isns, s, hostname);
976 isns_do_register(isns, s, hostname);
979 set_timeout(0, false);
983 isns_deregister(struct isns *isns)
985 struct conf *conf = isns->i_conf;
989 if (TAILQ_EMPTY(&conf->conf_targets) ||
990 TAILQ_EMPTY(&conf->conf_portal_groups))
992 set_timeout(conf->conf_isns_timeout, false);
993 s = isns_do_connect(isns);
996 gethostname(hostname, sizeof(hostname));
998 isns_do_deregister(isns, s, hostname);
1000 set_timeout(0, false);
1004 portal_group_set_filter(struct portal_group *pg, const char *str)
1008 if (strcmp(str, "none") == 0) {
1009 filter = PG_FILTER_NONE;
1010 } else if (strcmp(str, "portal") == 0) {
1011 filter = PG_FILTER_PORTAL;
1012 } else if (strcmp(str, "portal-name") == 0) {
1013 filter = PG_FILTER_PORTAL_NAME;
1014 } else if (strcmp(str, "portal-name-auth") == 0) {
1015 filter = PG_FILTER_PORTAL_NAME_AUTH;
1017 log_warnx("invalid discovery-filter \"%s\" for portal-group "
1018 "\"%s\"; valid values are \"none\", \"portal\", "
1019 "\"portal-name\", and \"portal-name-auth\"",
1024 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1025 pg->pg_discovery_filter != filter) {
1026 log_warnx("cannot set discovery-filter to \"%s\" for "
1027 "portal-group \"%s\"; already has a different "
1028 "value", str, pg->pg_name);
1032 pg->pg_discovery_filter = filter;
1038 portal_group_set_offload(struct portal_group *pg, const char *offload)
1041 if (pg->pg_offload != NULL) {
1042 log_warnx("cannot set offload to \"%s\" for "
1043 "portal-group \"%s\"; already defined",
1044 offload, pg->pg_name);
1048 pg->pg_offload = checked_strdup(offload);
1054 portal_group_set_redirection(struct portal_group *pg, const char *addr)
1057 if (pg->pg_redirection != NULL) {
1058 log_warnx("cannot set redirection to \"%s\" for "
1059 "portal-group \"%s\"; already defined",
1064 pg->pg_redirection = checked_strdup(addr);
1070 valid_hex(const char ch)
1102 valid_iscsi_name(const char *name)
1106 if (strlen(name) >= MAX_NAME_LEN) {
1107 log_warnx("overlong name for target \"%s\"; max length allowed "
1108 "by iSCSI specification is %d characters",
1109 name, MAX_NAME_LEN);
1114 * In the cases below, we don't return an error, just in case the admin
1115 * was right, and we're wrong.
1117 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1118 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1120 * XXX: We should verify UTF-8 normalisation, as defined
1121 * by 3.2.6.2: iSCSI Name Encoding.
1123 if (isalnum(name[i]))
1125 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1127 log_warnx("invalid character \"%c\" in target name "
1128 "\"%s\"; allowed characters are letters, digits, "
1129 "'-', '.', and ':'", name[i], name);
1133 * XXX: Check more stuff: valid date and a valid reversed domain.
1135 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1136 if (strlen(name) != strlen("eui.") + 16)
1137 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1138 "should be followed by exactly 16 hexadecimal "
1140 for (i = strlen("eui."); name[i] != '\0'; i++) {
1141 if (!valid_hex(name[i])) {
1142 log_warnx("invalid character \"%c\" in target "
1143 "name \"%s\"; allowed characters are 1-9 "
1144 "and A-F", name[i], name);
1148 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1149 if (strlen(name) > strlen("naa.") + 32)
1150 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1151 "should be followed by at most 32 hexadecimal "
1153 for (i = strlen("naa."); name[i] != '\0'; i++) {
1154 if (!valid_hex(name[i])) {
1155 log_warnx("invalid character \"%c\" in target "
1156 "name \"%s\"; allowed characters are 1-9 "
1157 "and A-F", name[i], name);
1162 log_warnx("invalid target name \"%s\"; should start with "
1163 "either \"iqn.\", \"eui.\", or \"naa.\"",
1170 pport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1174 pp = calloc(1, sizeof(*pp));
1176 log_err(1, "calloc");
1178 pp->pp_name = checked_strdup(name);
1179 pp->pp_ctl_port = ctl_port;
1180 TAILQ_INIT(&pp->pp_ports);
1181 TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1186 pport_find(const struct conf *conf, const char *name)
1190 TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1191 if (strcasecmp(pp->pp_name, name) == 0)
1198 pport_copy(struct pport *pp, struct conf *conf)
1200 struct pport *ppnew;
1202 ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1207 pport_delete(struct pport *pp)
1209 struct port *port, *tport;
1211 TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1213 TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1219 port_new(struct conf *conf, struct target *target, struct portal_group *pg)
1225 ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1227 log_err(1, "asprintf");
1228 if (port_find(conf, name) != NULL) {
1229 log_warnx("duplicate port \"%s\"", name);
1233 port = calloc(1, sizeof(*port));
1235 log_err(1, "calloc");
1236 port->p_conf = conf;
1237 port->p_name = name;
1238 port->p_ioctl_port = 0;
1239 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1240 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1241 port->p_target = target;
1242 TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1243 port->p_portal_group = pg;
1248 port_new_ioctl(struct conf *conf, struct target *target, int pp, int vp)
1250 struct pport *pport;
1256 ret = asprintf(&pname, "ioctl/%d/%d", pp, vp);
1258 log_err(1, "asprintf");
1262 pport = pport_find(conf, pname);
1263 if (pport != NULL) {
1265 return (port_new_pp(conf, target, pport));
1268 ret = asprintf(&name, "%s-%s", pname, target->t_name);
1272 log_err(1, "asprintf");
1273 if (port_find(conf, name) != NULL) {
1274 log_warnx("duplicate port \"%s\"", name);
1278 port = calloc(1, sizeof(*port));
1280 log_err(1, "calloc");
1281 port->p_conf = conf;
1282 port->p_name = name;
1283 port->p_ioctl_port = 1;
1284 port->p_ioctl_pp = pp;
1285 port->p_ioctl_vp = vp;
1286 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1287 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1288 port->p_target = target;
1293 port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1299 ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1301 log_err(1, "asprintf");
1302 if (port_find(conf, name) != NULL) {
1303 log_warnx("duplicate port \"%s\"", name);
1307 port = calloc(1, sizeof(*port));
1309 log_err(1, "calloc");
1310 port->p_conf = conf;
1311 port->p_name = name;
1312 TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1313 TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1314 port->p_target = target;
1315 TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1321 port_find(const struct conf *conf, const char *name)
1325 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1326 if (strcasecmp(port->p_name, name) == 0)
1334 port_find_in_pg(const struct portal_group *pg, const char *target)
1338 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1339 if (strcasecmp(port->p_target->t_name, target) == 0)
1347 port_delete(struct port *port)
1350 if (port->p_portal_group)
1351 TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1353 TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1355 TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1356 TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1362 port_is_dummy(struct port *port)
1365 if (port->p_portal_group) {
1366 if (port->p_portal_group->pg_foreign)
1368 if (TAILQ_EMPTY(&port->p_portal_group->pg_portals))
1375 target_new(struct conf *conf, const char *name)
1377 struct target *targ;
1380 targ = target_find(conf, name);
1382 log_warnx("duplicated target \"%s\"", name);
1385 if (valid_iscsi_name(name) == false) {
1386 log_warnx("target name \"%s\" is invalid", name);
1389 targ = calloc(1, sizeof(*targ));
1391 log_err(1, "calloc");
1392 targ->t_name = checked_strdup(name);
1395 * RFC 3722 requires us to normalize the name to lowercase.
1398 for (i = 0; i < len; i++)
1399 targ->t_name[i] = tolower(targ->t_name[i]);
1401 targ->t_conf = conf;
1402 TAILQ_INIT(&targ->t_ports);
1403 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1409 target_delete(struct target *targ)
1411 struct port *port, *tport;
1413 TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1415 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1418 free(targ->t_redirection);
1423 target_find(struct conf *conf, const char *name)
1425 struct target *targ;
1427 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1428 if (strcasecmp(targ->t_name, name) == 0)
1436 target_set_redirection(struct target *target, const char *addr)
1439 if (target->t_redirection != NULL) {
1440 log_warnx("cannot set redirection to \"%s\" for "
1441 "target \"%s\"; already defined",
1442 addr, target->t_name);
1446 target->t_redirection = checked_strdup(addr);
1452 lun_new(struct conf *conf, const char *name)
1456 lun = lun_find(conf, name);
1458 log_warnx("duplicated lun \"%s\"", name);
1462 lun = calloc(1, sizeof(*lun));
1464 log_err(1, "calloc");
1466 lun->l_name = checked_strdup(name);
1467 TAILQ_INIT(&lun->l_options);
1468 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1469 lun->l_ctl_lun = -1;
1475 lun_delete(struct lun *lun)
1477 struct target *targ;
1478 struct option *o, *tmp;
1481 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1482 for (i = 0; i < MAX_LUNS; i++) {
1483 if (targ->t_luns[i] == lun)
1484 targ->t_luns[i] = NULL;
1487 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1489 TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1490 option_delete(&lun->l_options, o);
1492 free(lun->l_backend);
1493 free(lun->l_device_id);
1495 free(lun->l_scsiname);
1496 free(lun->l_serial);
1501 lun_find(const struct conf *conf, const char *name)
1505 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1506 if (strcmp(lun->l_name, name) == 0)
1514 lun_set_backend(struct lun *lun, const char *value)
1516 free(lun->l_backend);
1517 lun->l_backend = checked_strdup(value);
1521 lun_set_blocksize(struct lun *lun, size_t value)
1524 lun->l_blocksize = value;
1528 lun_set_device_type(struct lun *lun, uint8_t value)
1531 lun->l_device_type = value;
1535 lun_set_device_id(struct lun *lun, const char *value)
1537 free(lun->l_device_id);
1538 lun->l_device_id = checked_strdup(value);
1542 lun_set_path(struct lun *lun, const char *value)
1545 lun->l_path = checked_strdup(value);
1549 lun_set_scsiname(struct lun *lun, const char *value)
1551 free(lun->l_scsiname);
1552 lun->l_scsiname = checked_strdup(value);
1556 lun_set_serial(struct lun *lun, const char *value)
1558 free(lun->l_serial);
1559 lun->l_serial = checked_strdup(value);
1563 lun_set_size(struct lun *lun, size_t value)
1566 lun->l_size = value;
1570 lun_set_ctl_lun(struct lun *lun, uint32_t value)
1573 lun->l_ctl_lun = value;
1577 option_new(struct options *options, const char *name, const char *value)
1581 o = option_find(options, name);
1583 log_warnx("duplicated option \"%s\"", name);
1587 o = calloc(1, sizeof(*o));
1589 log_err(1, "calloc");
1590 o->o_name = checked_strdup(name);
1591 o->o_value = checked_strdup(value);
1592 TAILQ_INSERT_TAIL(options, o, o_next);
1598 option_delete(struct options *options, struct option *o)
1601 TAILQ_REMOVE(options, o, o_next);
1608 option_find(const struct options *options, const char *name)
1612 TAILQ_FOREACH(o, options, o_next) {
1613 if (strcmp(o->o_name, name) == 0)
1621 option_set(struct option *o, const char *value)
1625 o->o_value = checked_strdup(value);
1628 static struct connection *
1629 connection_new(struct portal *portal, int fd, const char *host,
1630 const struct sockaddr *client_sa)
1632 struct connection *conn;
1634 conn = calloc(1, sizeof(*conn));
1636 log_err(1, "calloc");
1637 conn->conn_portal = portal;
1638 conn->conn_socket = fd;
1639 conn->conn_initiator_addr = checked_strdup(host);
1640 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1643 * Default values, from RFC 3720, section 12.
1645 conn->conn_max_recv_data_segment_length = 8192;
1646 conn->conn_max_send_data_segment_length = 8192;
1647 conn->conn_max_burst_length = 262144;
1648 conn->conn_first_burst_length = 65536;
1649 conn->conn_immediate_data = true;
1656 conf_print(struct conf *conf)
1658 struct auth_group *ag;
1660 struct auth_name *auth_name;
1661 struct auth_portal *auth_portal;
1662 struct portal_group *pg;
1663 struct portal *portal;
1664 struct target *targ;
1668 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1669 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1670 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1671 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1672 auth->a_user, auth->a_secret,
1673 auth->a_mutual_user, auth->a_mutual_secret);
1674 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1675 fprintf(stderr, "\t initiator-name %s\n",
1676 auth_name->an_initator_name);
1677 TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next)
1678 fprintf(stderr, "\t initiator-portal %s\n",
1679 auth_portal->ap_initator_portal);
1680 fprintf(stderr, "}\n");
1682 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1683 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1684 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1685 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1686 fprintf(stderr, "}\n");
1688 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1689 fprintf(stderr, "\tlun %s {\n", lun->l_name);
1690 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1691 TAILQ_FOREACH(o, &lun->l_options, o_next)
1692 fprintf(stderr, "\t\toption %s %s\n",
1693 o->o_name, o->o_value);
1694 fprintf(stderr, "\t}\n");
1696 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1697 fprintf(stderr, "target %s {\n", targ->t_name);
1698 if (targ->t_alias != NULL)
1699 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1700 fprintf(stderr, "}\n");
1706 conf_verify_lun(struct lun *lun)
1708 const struct lun *lun2;
1710 if (lun->l_backend == NULL)
1711 lun_set_backend(lun, "block");
1712 if (strcmp(lun->l_backend, "block") == 0) {
1713 if (lun->l_path == NULL) {
1714 log_warnx("missing path for lun \"%s\"",
1718 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1719 if (lun->l_size == 0) {
1720 log_warnx("missing size for ramdisk-backed lun \"%s\"",
1724 if (lun->l_path != NULL) {
1725 log_warnx("path must not be specified "
1726 "for ramdisk-backed lun \"%s\"",
1731 if (lun->l_blocksize == 0) {
1732 if (lun->l_device_type == 5)
1733 lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1735 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1736 } else if (lun->l_blocksize < 0) {
1737 log_warnx("invalid blocksize for lun \"%s\"; "
1738 "must be larger than 0", lun->l_name);
1741 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1742 log_warnx("invalid size for lun \"%s\"; "
1743 "must be multiple of blocksize", lun->l_name);
1746 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1749 if (lun->l_path != NULL && lun2->l_path != NULL &&
1750 strcmp(lun->l_path, lun2->l_path) == 0) {
1751 log_debugx("WARNING: path \"%s\" duplicated "
1752 "between lun \"%s\", and "
1753 "lun \"%s\"", lun->l_path,
1754 lun->l_name, lun2->l_name);
1762 conf_verify(struct conf *conf)
1764 struct auth_group *ag;
1765 struct portal_group *pg;
1767 struct target *targ;
1772 if (conf->conf_pidfile_path == NULL)
1773 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1775 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1776 error = conf_verify_lun(lun);
1780 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1781 if (targ->t_auth_group == NULL) {
1782 targ->t_auth_group = auth_group_find(conf,
1784 assert(targ->t_auth_group != NULL);
1786 if (TAILQ_EMPTY(&targ->t_ports)) {
1787 pg = portal_group_find(conf, "default");
1789 port_new(conf, targ, pg);
1792 for (i = 0; i < MAX_LUNS; i++) {
1793 if (targ->t_luns[i] != NULL)
1796 if (!found && targ->t_redirection == NULL) {
1797 log_warnx("no LUNs defined for target \"%s\"",
1800 if (found && targ->t_redirection != NULL) {
1801 log_debugx("target \"%s\" contains luns, "
1802 " but configured for redirection",
1806 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1807 assert(pg->pg_name != NULL);
1808 if (pg->pg_discovery_auth_group == NULL) {
1809 pg->pg_discovery_auth_group =
1810 auth_group_find(conf, "default");
1811 assert(pg->pg_discovery_auth_group != NULL);
1814 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1815 pg->pg_discovery_filter = PG_FILTER_NONE;
1817 if (pg->pg_redirection != NULL) {
1818 if (!TAILQ_EMPTY(&pg->pg_ports)) {
1819 log_debugx("portal-group \"%s\" assigned "
1820 "to target, but configured "
1824 pg->pg_unassigned = false;
1825 } else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1826 pg->pg_unassigned = false;
1828 if (strcmp(pg->pg_name, "default") != 0)
1829 log_warnx("portal-group \"%s\" not assigned "
1830 "to any target", pg->pg_name);
1831 pg->pg_unassigned = true;
1834 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1835 if (ag->ag_name == NULL)
1836 assert(ag->ag_target != NULL);
1838 assert(ag->ag_target == NULL);
1841 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1842 if (targ->t_auth_group == ag) {
1847 TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1848 if (port->p_auth_group == ag) {
1853 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1854 if (pg->pg_discovery_auth_group == ag) {
1859 if (!found && ag->ag_name != NULL &&
1860 strcmp(ag->ag_name, "default") != 0 &&
1861 strcmp(ag->ag_name, "no-authentication") != 0 &&
1862 strcmp(ag->ag_name, "no-access") != 0) {
1863 log_warnx("auth-group \"%s\" not assigned "
1864 "to any target", ag->ag_name);
1872 conf_apply(struct conf *oldconf, struct conf *newconf)
1874 struct lun *oldlun, *newlun, *tmplun;
1875 struct portal_group *oldpg, *newpg;
1876 struct portal *oldp, *newp;
1877 struct port *oldport, *newport, *tmpport;
1878 struct isns *oldns, *newns;
1880 int changed, cumulated_error = 0, error, sockbuf;
1883 if (oldconf->conf_debug != newconf->conf_debug) {
1884 log_debugx("changing debug level to %d", newconf->conf_debug);
1885 log_init(newconf->conf_debug);
1888 if (oldconf->conf_pidfh != NULL) {
1889 assert(oldconf->conf_pidfile_path != NULL);
1890 if (newconf->conf_pidfile_path != NULL &&
1891 strcmp(oldconf->conf_pidfile_path,
1892 newconf->conf_pidfile_path) == 0) {
1893 newconf->conf_pidfh = oldconf->conf_pidfh;
1894 oldconf->conf_pidfh = NULL;
1896 log_debugx("removing pidfile %s",
1897 oldconf->conf_pidfile_path);
1898 pidfile_remove(oldconf->conf_pidfh);
1899 oldconf->conf_pidfh = NULL;
1903 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1904 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1905 newconf->conf_pidfh =
1906 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1907 if (newconf->conf_pidfh == NULL) {
1908 if (errno == EEXIST)
1909 log_errx(1, "daemon already running, pid: %jd.",
1910 (intmax_t)otherpid);
1911 log_err(1, "cannot open or create pidfile \"%s\"",
1912 newconf->conf_pidfile_path);
1917 * Go through the new portal groups, assigning tags or preserving old.
1919 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1920 if (newpg->pg_tag != 0)
1922 oldpg = portal_group_find(oldconf, newpg->pg_name);
1924 newpg->pg_tag = oldpg->pg_tag;
1926 newpg->pg_tag = ++last_portal_group_tag;
1929 /* Deregister on removed iSNS servers. */
1930 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1931 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1932 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1936 isns_deregister(oldns);
1940 * XXX: If target or lun removal fails, we should somehow "move"
1941 * the old lun or target into newconf, so that subsequent
1942 * conf_apply() would try to remove them again. That would
1943 * be somewhat hairy, though, and lun deletion failures don't
1944 * really happen, so leave it as it is for now.
1947 * First, remove any ports present in the old configuration
1948 * and missing in the new one.
1950 TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1951 if (port_is_dummy(oldport))
1953 newport = port_find(newconf, oldport->p_name);
1954 if (newport != NULL && !port_is_dummy(newport))
1956 log_debugx("removing port \"%s\"", oldport->p_name);
1957 error = kernel_port_remove(oldport);
1959 log_warnx("failed to remove port %s",
1962 * XXX: Uncomment after fixing the root cause.
1964 * cumulated_error++;
1970 * Second, remove any LUNs present in the old configuration
1971 * and missing in the new one.
1973 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1974 newlun = lun_find(newconf, oldlun->l_name);
1975 if (newlun == NULL) {
1976 log_debugx("lun \"%s\", CTL lun %d "
1977 "not found in new configuration; "
1978 "removing", oldlun->l_name, oldlun->l_ctl_lun);
1979 error = kernel_lun_remove(oldlun);
1981 log_warnx("failed to remove lun \"%s\", "
1983 oldlun->l_name, oldlun->l_ctl_lun);
1990 * Also remove the LUNs changed by more than size.
1993 assert(oldlun->l_backend != NULL);
1994 assert(newlun->l_backend != NULL);
1995 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1996 log_debugx("backend for lun \"%s\", "
1997 "CTL lun %d changed; removing",
1998 oldlun->l_name, oldlun->l_ctl_lun);
2001 if (oldlun->l_blocksize != newlun->l_blocksize) {
2002 log_debugx("blocksize for lun \"%s\", "
2003 "CTL lun %d changed; removing",
2004 oldlun->l_name, oldlun->l_ctl_lun);
2007 if (newlun->l_device_id != NULL &&
2008 (oldlun->l_device_id == NULL ||
2009 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
2011 log_debugx("device-id for lun \"%s\", "
2012 "CTL lun %d changed; removing",
2013 oldlun->l_name, oldlun->l_ctl_lun);
2016 if (newlun->l_path != NULL &&
2017 (oldlun->l_path == NULL ||
2018 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
2019 log_debugx("path for lun \"%s\", "
2020 "CTL lun %d, changed; removing",
2021 oldlun->l_name, oldlun->l_ctl_lun);
2024 if (newlun->l_serial != NULL &&
2025 (oldlun->l_serial == NULL ||
2026 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
2027 log_debugx("serial for lun \"%s\", "
2028 "CTL lun %d changed; removing",
2029 oldlun->l_name, oldlun->l_ctl_lun);
2033 error = kernel_lun_remove(oldlun);
2035 log_warnx("failed to remove lun \"%s\", "
2037 oldlun->l_name, oldlun->l_ctl_lun);
2044 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
2047 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
2048 oldlun = lun_find(oldconf, newlun->l_name);
2049 if (oldlun != NULL) {
2050 log_debugx("modifying lun \"%s\", CTL lun %d",
2051 newlun->l_name, newlun->l_ctl_lun);
2052 error = kernel_lun_modify(newlun);
2054 log_warnx("failed to "
2055 "modify lun \"%s\", CTL lun %d",
2056 newlun->l_name, newlun->l_ctl_lun);
2061 log_debugx("adding lun \"%s\"", newlun->l_name);
2062 error = kernel_lun_add(newlun);
2064 log_warnx("failed to add lun \"%s\"", newlun->l_name);
2071 * Now add new ports or modify existing ones.
2073 TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
2074 if (port_is_dummy(newport))
2076 oldport = port_find(oldconf, newport->p_name);
2078 if (oldport == NULL || port_is_dummy(oldport)) {
2079 log_debugx("adding port \"%s\"", newport->p_name);
2080 error = kernel_port_add(newport);
2082 log_debugx("updating port \"%s\"", newport->p_name);
2083 newport->p_ctl_port = oldport->p_ctl_port;
2084 error = kernel_port_update(newport, oldport);
2087 log_warnx("failed to %s port %s",
2088 (oldport == NULL) ? "add" : "update",
2091 * XXX: Uncomment after fixing the root cause.
2093 * cumulated_error++;
2099 * Go through the new portals, opening the sockets as necessary.
2101 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2102 if (newpg->pg_foreign)
2104 if (newpg->pg_unassigned) {
2105 log_debugx("not listening on portal-group \"%s\", "
2106 "not assigned to any target",
2110 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2112 * Try to find already open portal and reuse
2113 * the listening socket. We don't care about
2114 * what portal or portal group that was, what
2115 * matters is the listening address.
2117 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2119 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2121 if (strcmp(newp->p_listen,
2122 oldp->p_listen) == 0 &&
2123 oldp->p_socket > 0) {
2131 if (newp->p_socket > 0) {
2133 * We're done with this portal.
2138 #ifdef ICL_KERNEL_PROXY
2140 newpg->pg_conf->conf_portal_id++;
2141 newp->p_id = newpg->pg_conf->conf_portal_id;
2142 log_debugx("listening on %s, portal-group "
2143 "\"%s\", portal id %d, using ICL proxy",
2144 newp->p_listen, newpg->pg_name, newp->p_id);
2145 kernel_listen(newp->p_ai, newp->p_iser,
2150 assert(proxy_mode == false);
2151 assert(newp->p_iser == false);
2153 log_debugx("listening on %s, portal-group \"%s\"",
2154 newp->p_listen, newpg->pg_name);
2155 newp->p_socket = socket(newp->p_ai->ai_family,
2156 newp->p_ai->ai_socktype,
2157 newp->p_ai->ai_protocol);
2158 if (newp->p_socket < 0) {
2159 log_warn("socket(2) failed for %s",
2164 sockbuf = SOCKBUF_SIZE;
2165 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2166 &sockbuf, sizeof(sockbuf)) == -1)
2167 log_warn("setsockopt(SO_RCVBUF) failed "
2168 "for %s", newp->p_listen);
2169 sockbuf = SOCKBUF_SIZE;
2170 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2171 &sockbuf, sizeof(sockbuf)) == -1)
2172 log_warn("setsockopt(SO_SNDBUF) failed "
2173 "for %s", newp->p_listen);
2174 error = setsockopt(newp->p_socket, SOL_SOCKET,
2175 SO_REUSEADDR, &one, sizeof(one));
2177 log_warn("setsockopt(SO_REUSEADDR) failed "
2178 "for %s", newp->p_listen);
2179 close(newp->p_socket);
2184 error = bind(newp->p_socket, newp->p_ai->ai_addr,
2185 newp->p_ai->ai_addrlen);
2187 log_warn("bind(2) failed for %s",
2189 close(newp->p_socket);
2194 error = listen(newp->p_socket, -1);
2196 log_warn("listen(2) failed for %s",
2198 close(newp->p_socket);
2207 * Go through the no longer used sockets, closing them.
2209 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2210 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2211 if (oldp->p_socket <= 0)
2213 log_debugx("closing socket for %s, portal-group \"%s\"",
2214 oldp->p_listen, oldpg->pg_name);
2215 close(oldp->p_socket);
2220 /* (Re-)Register on remaining/new iSNS servers. */
2221 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2222 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2223 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2226 isns_register(newns, oldns);
2229 /* Schedule iSNS update */
2230 if (!TAILQ_EMPTY(&newconf->conf_isns))
2231 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2233 return (cumulated_error);
2240 return (sigalrm_received);
2244 sigalrm_handler_fatal(int dummy __unused)
2247 * It would be easiest to just log an error and exit. We can't
2248 * do this, though, because log_errx() is not signal safe, since
2249 * it calls syslog(3). Instead, set a flag checked by pdu_send()
2250 * and pdu_receive(), to call log_errx() there. Should they fail
2251 * to notice, we'll exit here one second later.
2253 if (sigalrm_received) {
2255 * Oh well. Just give up and quit.
2260 sigalrm_received = true;
2264 sigalrm_handler(int dummy __unused)
2267 sigalrm_received = true;
2271 set_timeout(int timeout, int fatal)
2273 struct sigaction sa;
2274 struct itimerval itv;
2278 log_debugx("session timeout disabled");
2279 bzero(&itv, sizeof(itv));
2280 error = setitimer(ITIMER_REAL, &itv, NULL);
2282 log_err(1, "setitimer");
2283 sigalrm_received = false;
2287 sigalrm_received = false;
2288 bzero(&sa, sizeof(sa));
2290 sa.sa_handler = sigalrm_handler_fatal;
2292 sa.sa_handler = sigalrm_handler;
2293 sigfillset(&sa.sa_mask);
2294 error = sigaction(SIGALRM, &sa, NULL);
2296 log_err(1, "sigaction");
2299 * First SIGALRM will arive after conf_timeout seconds.
2300 * If we do nothing, another one will arrive a second later.
2302 log_debugx("setting session timeout to %d seconds", timeout);
2303 bzero(&itv, sizeof(itv));
2304 itv.it_interval.tv_sec = 1;
2305 itv.it_value.tv_sec = timeout;
2306 error = setitimer(ITIMER_REAL, &itv, NULL);
2308 log_err(1, "setitimer");
2312 wait_for_children(bool block)
2320 * If "block" is true, wait for at least one process.
2322 if (block && num == 0)
2323 pid = wait4(-1, &status, 0, NULL);
2325 pid = wait4(-1, &status, WNOHANG, NULL);
2328 if (WIFSIGNALED(status)) {
2329 log_warnx("child process %d terminated with signal %d",
2330 pid, WTERMSIG(status));
2331 } else if (WEXITSTATUS(status) != 0) {
2332 log_warnx("child process %d terminated with exit status %d",
2333 pid, WEXITSTATUS(status));
2335 log_debugx("child process %d terminated gracefully", pid);
2344 handle_connection(struct portal *portal, int fd,
2345 const struct sockaddr *client_sa, bool dont_fork)
2347 struct connection *conn;
2350 char host[NI_MAXHOST + 1];
2353 conf = portal->p_portal_group->pg_conf;
2356 log_debugx("incoming connection; not forking due to -d flag");
2358 nchildren -= wait_for_children(false);
2359 assert(nchildren >= 0);
2361 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2362 log_debugx("maxproc limit of %d child processes hit; "
2363 "waiting for child process to exit", conf->conf_maxproc);
2364 nchildren -= wait_for_children(true);
2365 assert(nchildren >= 0);
2367 log_debugx("incoming connection; forking child process #%d",
2378 pidfile_close(conf->conf_pidfh);
2380 error = getnameinfo(client_sa, client_sa->sa_len,
2381 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2383 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2385 log_debugx("accepted connection from %s; portal group \"%s\"",
2386 host, portal->p_portal_group->pg_name);
2387 log_set_peer_addr(host);
2388 setproctitle("%s", host);
2390 conn = connection_new(portal, fd, host, client_sa);
2391 set_timeout(conf->conf_timeout, true);
2394 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2395 kernel_handoff(conn);
2396 log_debugx("connection handed off to the kernel");
2398 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2401 log_debugx("nothing more to do; exiting");
2406 fd_add(int fd, fd_set *fdset, int nfds)
2410 * Skip sockets which we failed to bind.
2422 main_loop(struct conf *conf, bool dont_fork)
2424 struct portal_group *pg;
2425 struct portal *portal;
2426 struct sockaddr_storage client_sa;
2427 socklen_t client_salen;
2428 #ifdef ICL_KERNEL_PROXY
2433 int error, nfds, client_fd;
2435 pidfile_write(conf->conf_pidfh);
2438 if (sighup_received || sigterm_received || timed_out())
2441 #ifdef ICL_KERNEL_PROXY
2443 client_salen = sizeof(client_sa);
2444 kernel_accept(&connection_id, &portal_id,
2445 (struct sockaddr *)&client_sa, &client_salen);
2446 assert(client_salen >= client_sa.ss_len);
2448 log_debugx("incoming connection, id %d, portal id %d",
2449 connection_id, portal_id);
2450 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2451 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2452 if (portal->p_id == portal_id) {
2458 log_errx(1, "kernel returned invalid portal_id %d",
2462 handle_connection(portal, connection_id,
2463 (struct sockaddr *)&client_sa, dont_fork);
2466 assert(proxy_mode == false);
2470 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2471 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2472 nfds = fd_add(portal->p_socket, &fdset, nfds);
2474 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2478 log_err(1, "select");
2480 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2481 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2482 if (!FD_ISSET(portal->p_socket, &fdset))
2484 client_salen = sizeof(client_sa);
2485 client_fd = accept(portal->p_socket,
2486 (struct sockaddr *)&client_sa,
2488 if (client_fd < 0) {
2489 if (errno == ECONNABORTED)
2491 log_err(1, "accept");
2493 assert(client_salen >= client_sa.ss_len);
2495 handle_connection(portal, client_fd,
2496 (struct sockaddr *)&client_sa,
2501 #ifdef ICL_KERNEL_PROXY
2508 sighup_handler(int dummy __unused)
2511 sighup_received = true;
2515 sigterm_handler(int dummy __unused)
2518 sigterm_received = true;
2522 sigchld_handler(int dummy __unused)
2526 * The only purpose of this handler is to make SIGCHLD
2527 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2528 * wait_for_children().
2533 register_signals(void)
2535 struct sigaction sa;
2538 bzero(&sa, sizeof(sa));
2539 sa.sa_handler = sighup_handler;
2540 sigfillset(&sa.sa_mask);
2541 error = sigaction(SIGHUP, &sa, NULL);
2543 log_err(1, "sigaction");
2545 sa.sa_handler = sigterm_handler;
2546 error = sigaction(SIGTERM, &sa, NULL);
2548 log_err(1, "sigaction");
2550 sa.sa_handler = sigterm_handler;
2551 error = sigaction(SIGINT, &sa, NULL);
2553 log_err(1, "sigaction");
2555 sa.sa_handler = sigchld_handler;
2556 error = sigaction(SIGCHLD, &sa, NULL);
2558 log_err(1, "sigaction");
2562 check_perms(const char *path)
2567 error = stat(path, &sb);
2572 if (sb.st_mode & S_IWOTH) {
2573 log_warnx("%s is world-writable", path);
2574 } else if (sb.st_mode & S_IROTH) {
2575 log_warnx("%s is world-readable", path);
2576 } else if (sb.st_mode & S_IXOTH) {
2578 * Ok, this one doesn't matter, but still do it,
2579 * just for consistency.
2581 log_warnx("%s is world-executable", path);
2585 * XXX: Should we also check for owner != 0?
2589 static struct conf *
2590 conf_new_from_file(const char *path, struct conf *oldconf, bool ucl)
2593 struct auth_group *ag;
2594 struct portal_group *pg;
2598 log_debugx("obtaining configuration from %s", path);
2602 TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next)
2603 pport_copy(pp, conf);
2605 ag = auth_group_new(conf, "default");
2608 ag = auth_group_new(conf, "no-authentication");
2610 ag->ag_type = AG_TYPE_NO_AUTHENTICATION;
2612 ag = auth_group_new(conf, "no-access");
2614 ag->ag_type = AG_TYPE_DENY;
2616 pg = portal_group_new(conf, "default");
2620 error = uclparse_conf(conf, path);
2622 error = parse_conf(conf, path);
2631 if (conf->conf_default_ag_defined == false) {
2632 log_debugx("auth-group \"default\" not defined; "
2633 "going with defaults");
2634 ag = auth_group_find(conf, "default");
2636 ag->ag_type = AG_TYPE_DENY;
2639 if (conf->conf_default_pg_defined == false) {
2640 log_debugx("portal-group \"default\" not defined; "
2641 "going with defaults");
2642 pg = portal_group_find(conf, "default");
2644 portal_group_add_listen(pg, "0.0.0.0:3260", false);
2645 portal_group_add_listen(pg, "[::]:3260", false);
2648 conf->conf_kernel_port_on = true;
2650 error = conf_verify(conf);
2660 main(int argc, char **argv)
2662 struct conf *oldconf, *newconf, *tmpconf;
2664 const char *config_path = DEFAULT_CONFIG_PATH;
2665 int debug = 0, ch, error;
2666 bool dont_daemonize = false;
2667 bool test_config = false;
2668 bool use_ucl = false;
2670 while ((ch = getopt(argc, argv, "dtuf:R")) != -1) {
2673 dont_daemonize = true;
2683 config_path = optarg;
2686 #ifndef ICL_KERNEL_PROXY
2687 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2688 "does not support iSER protocol");
2704 oldconf = conf_new_from_kernel();
2705 newconf = conf_new_from_file(config_path, oldconf, use_ucl);
2707 if (newconf == NULL)
2708 log_errx(1, "configuration error; exiting");
2714 oldconf->conf_debug = debug;
2715 newconf->conf_debug = debug;
2718 error = conf_apply(oldconf, newconf);
2720 log_errx(1, "failed to apply configuration; exiting");
2722 conf_delete(oldconf);
2727 if (dont_daemonize == false) {
2728 log_debugx("daemonizing");
2729 if (daemon(0, 0) == -1) {
2730 log_warn("cannot daemonize");
2731 pidfile_remove(newconf->conf_pidfh);
2736 /* Schedule iSNS update */
2737 if (!TAILQ_EMPTY(&newconf->conf_isns))
2738 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2741 main_loop(newconf, dont_daemonize);
2742 if (sighup_received) {
2743 sighup_received = false;
2744 log_debugx("received SIGHUP, reloading configuration");
2745 tmpconf = conf_new_from_file(config_path, newconf,
2748 if (tmpconf == NULL) {
2749 log_warnx("configuration error, "
2750 "continuing with old configuration");
2753 tmpconf->conf_debug = debug;
2756 error = conf_apply(oldconf, newconf);
2758 log_warnx("failed to reload "
2760 conf_delete(oldconf);
2763 } else if (sigterm_received) {
2764 log_debugx("exiting on signal; "
2765 "reloading empty configuration");
2767 log_debugx("removing CTL iSCSI ports "
2768 "and terminating all connections");
2771 newconf = conf_new();
2773 newconf->conf_debug = debug;
2774 error = conf_apply(oldconf, newconf);
2776 log_warnx("failed to apply configuration");
2777 conf_delete(oldconf);
2780 log_warnx("exiting on signal");
2783 nchildren -= wait_for_children(false);
2784 assert(nchildren >= 0);
2786 set_timeout(0, false);
2787 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2789 /* Schedule iSNS update */
2790 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2791 set_timeout((newconf->conf_isns_period