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
32 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
51 static volatile bool sighup_received = false;
52 static volatile bool sigterm_received = false;
53 static volatile bool sigalrm_received = false;
55 static int nchildren = 0;
61 fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
66 checked_strdup(const char *s)
81 conf = calloc(1, sizeof(*conf));
84 TAILQ_INIT(&conf->conf_targets);
85 TAILQ_INIT(&conf->conf_auth_groups);
86 TAILQ_INIT(&conf->conf_portal_groups);
89 conf->conf_timeout = 60;
90 conf->conf_maxproc = 30;
96 conf_delete(struct conf *conf)
98 struct target *targ, *tmp;
99 struct auth_group *ag, *cagtmp;
100 struct portal_group *pg, *cpgtmp;
102 assert(conf->conf_pidfh == NULL);
104 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
106 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
107 auth_group_delete(ag);
108 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
109 portal_group_delete(pg);
110 free(conf->conf_pidfile_path);
115 auth_new(struct auth_group *ag)
119 auth = calloc(1, sizeof(*auth));
121 log_err(1, "calloc");
122 auth->a_auth_group = ag;
123 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
128 auth_delete(struct auth *auth)
130 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
133 free(auth->a_secret);
134 free(auth->a_mutual_user);
135 free(auth->a_mutual_secret);
140 auth_find(struct auth_group *ag, const char *user)
142 const struct auth *auth;
144 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
145 if (strcmp(auth->a_user, user) == 0)
153 auth_check_secret_length(struct auth *auth)
157 len = strlen(auth->a_secret);
159 if (auth->a_auth_group->ag_name != NULL)
160 log_warnx("secret for user \"%s\", auth-group \"%s\", "
161 "is too long; it should be at most 16 characters "
162 "long", auth->a_user, auth->a_auth_group->ag_name);
164 log_warnx("secret for user \"%s\", target \"%s\", "
165 "is too long; it should be at most 16 characters "
166 "long", auth->a_user,
167 auth->a_auth_group->ag_target->t_name);
170 if (auth->a_auth_group->ag_name != NULL)
171 log_warnx("secret for user \"%s\", auth-group \"%s\", "
172 "is too short; it should be at least 12 characters "
173 "long", auth->a_user,
174 auth->a_auth_group->ag_name);
176 log_warnx("secret for user \"%s\", target \"%s\", "
177 "is too short; it should be at least 16 characters "
178 "long", auth->a_user,
179 auth->a_auth_group->ag_target->t_name);
182 if (auth->a_mutual_secret != NULL) {
183 len = strlen(auth->a_secret);
185 if (auth->a_auth_group->ag_name != NULL)
186 log_warnx("mutual secret for user \"%s\", "
187 "auth-group \"%s\", is too long; it should "
188 "be at most 16 characters long",
189 auth->a_user, auth->a_auth_group->ag_name);
191 log_warnx("mutual secret for user \"%s\", "
192 "target \"%s\", is too long; it should "
193 "be at most 16 characters long",
195 auth->a_auth_group->ag_target->t_name);
198 if (auth->a_auth_group->ag_name != NULL)
199 log_warnx("mutual secret for user \"%s\", "
200 "auth-group \"%s\", is too short; it "
201 "should be at least 12 characters long",
202 auth->a_user, auth->a_auth_group->ag_name);
204 log_warnx("mutual secret for user \"%s\", "
205 "target \"%s\", is too short; it should be "
206 "at least 16 characters long",
208 auth->a_auth_group->ag_target->t_name);
214 auth_new_chap(struct auth_group *ag, const char *user,
219 if (ag->ag_type == AG_TYPE_UNKNOWN)
220 ag->ag_type = AG_TYPE_CHAP;
221 if (ag->ag_type != AG_TYPE_CHAP) {
222 if (ag->ag_name != NULL)
223 log_warnx("cannot mix \"chap\" authentication with "
224 "other types for auth-group \"%s\"", ag->ag_name);
226 log_warnx("cannot mix \"chap\" authentication with "
227 "other types for target \"%s\"",
228 ag->ag_target->t_name);
233 auth->a_user = checked_strdup(user);
234 auth->a_secret = checked_strdup(secret);
236 auth_check_secret_length(auth);
242 auth_new_chap_mutual(struct auth_group *ag, const char *user,
243 const char *secret, const char *user2, const char *secret2)
247 if (ag->ag_type == AG_TYPE_UNKNOWN)
248 ag->ag_type = AG_TYPE_CHAP_MUTUAL;
249 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
250 if (ag->ag_name != NULL)
251 log_warnx("cannot mix \"chap-mutual\" authentication "
252 "with other types for auth-group \"%s\"",
255 log_warnx("cannot mix \"chap-mutual\" authentication "
256 "with other types for target \"%s\"",
257 ag->ag_target->t_name);
262 auth->a_user = checked_strdup(user);
263 auth->a_secret = checked_strdup(secret);
264 auth->a_mutual_user = checked_strdup(user2);
265 auth->a_mutual_secret = checked_strdup(secret2);
267 auth_check_secret_length(auth);
272 const struct auth_name *
273 auth_name_new(struct auth_group *ag, const char *name)
275 struct auth_name *an;
277 an = calloc(1, sizeof(*an));
279 log_err(1, "calloc");
280 an->an_auth_group = ag;
281 an->an_initator_name = checked_strdup(name);
282 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
287 auth_name_delete(struct auth_name *an)
289 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
291 free(an->an_initator_name);
296 auth_name_defined(const struct auth_group *ag)
298 if (TAILQ_EMPTY(&ag->ag_names))
303 const struct auth_name *
304 auth_name_find(const struct auth_group *ag, const char *name)
306 const struct auth_name *auth_name;
308 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
309 if (strcmp(auth_name->an_initator_name, name) == 0)
316 const struct auth_portal *
317 auth_portal_new(struct auth_group *ag, const char *portal)
319 struct auth_portal *ap;
321 ap = calloc(1, sizeof(*ap));
323 log_err(1, "calloc");
324 ap->ap_auth_group = ag;
325 ap->ap_initator_portal = checked_strdup(portal);
326 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
331 auth_portal_delete(struct auth_portal *ap)
333 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
335 free(ap->ap_initator_portal);
340 auth_portal_defined(const struct auth_group *ag)
342 if (TAILQ_EMPTY(&ag->ag_portals))
347 const struct auth_portal *
348 auth_portal_find(const struct auth_group *ag, const char *portal)
350 const struct auth_portal *auth_portal;
352 TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next) {
353 if (strcmp(auth_portal->ap_initator_portal, portal) == 0)
354 return (auth_portal);
361 auth_group_new(struct conf *conf, const char *name)
363 struct auth_group *ag;
366 ag = auth_group_find(conf, name);
368 log_warnx("duplicated auth-group \"%s\"", name);
373 ag = calloc(1, sizeof(*ag));
375 log_err(1, "calloc");
377 ag->ag_name = checked_strdup(name);
378 TAILQ_INIT(&ag->ag_auths);
379 TAILQ_INIT(&ag->ag_names);
380 TAILQ_INIT(&ag->ag_portals);
382 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
388 auth_group_delete(struct auth_group *ag)
390 struct auth *auth, *auth_tmp;
391 struct auth_name *auth_name, *auth_name_tmp;
392 struct auth_portal *auth_portal, *auth_portal_tmp;
394 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
396 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
398 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
399 auth_name_delete(auth_name);
400 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
402 auth_portal_delete(auth_portal);
408 auth_group_find(struct conf *conf, const char *name)
410 struct auth_group *ag;
412 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
413 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
421 auth_group_set_type(struct auth_group *ag, int type)
424 if (ag->ag_type == AG_TYPE_UNKNOWN) {
429 if (ag->ag_type == type)
436 auth_group_set_type_str(struct auth_group *ag, const char *str)
440 if (strcmp(str, "none") == 0) {
441 type = AG_TYPE_NO_AUTHENTICATION;
442 } else if (strcmp(str, "deny") == 0) {
444 } else if (strcmp(str, "chap") == 0) {
446 } else if (strcmp(str, "chap-mutual") == 0) {
447 type = AG_TYPE_CHAP_MUTUAL;
449 if (ag->ag_name != NULL)
450 log_warnx("invalid auth-type \"%s\" for auth-group "
451 "\"%s\"", str, ag->ag_name);
453 log_warnx("invalid auth-type \"%s\" for target "
454 "\"%s\"", str, ag->ag_target->t_name);
458 error = auth_group_set_type(ag, type);
460 if (ag->ag_name != NULL)
461 log_warnx("cannot set auth-type to \"%s\" for "
462 "auth-group \"%s\"; already has a different "
463 "type", str, ag->ag_name);
465 log_warnx("cannot set auth-type to \"%s\" for target "
466 "\"%s\"; already has a different type",
467 str, ag->ag_target->t_name);
474 static struct portal *
475 portal_new(struct portal_group *pg)
477 struct portal *portal;
479 portal = calloc(1, sizeof(*portal));
481 log_err(1, "calloc");
482 TAILQ_INIT(&portal->p_targets);
483 portal->p_portal_group = pg;
484 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
489 portal_delete(struct portal *portal)
491 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
492 freeaddrinfo(portal->p_ai);
493 free(portal->p_listen);
497 struct portal_group *
498 portal_group_new(struct conf *conf, const char *name)
500 struct portal_group *pg;
502 pg = portal_group_find(conf, name);
504 log_warnx("duplicated portal-group \"%s\"", name);
508 pg = calloc(1, sizeof(*pg));
510 log_err(1, "calloc");
511 pg->pg_name = checked_strdup(name);
512 TAILQ_INIT(&pg->pg_portals);
514 conf->conf_last_portal_group_tag++;
515 pg->pg_tag = conf->conf_last_portal_group_tag;
516 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
522 portal_group_delete(struct portal_group *pg)
524 struct portal *portal, *tmp;
526 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
528 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
529 portal_delete(portal);
534 struct portal_group *
535 portal_group_find(struct conf *conf, const char *name)
537 struct portal_group *pg;
539 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
540 if (strcmp(pg->pg_name, name) == 0)
548 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
550 struct addrinfo hints;
551 struct portal *portal;
552 char *addr, *ch, *arg;
554 int error, colons = 0;
556 #ifndef ICL_KERNEL_PROXY
558 log_warnx("ctld(8) compiled without ICL_KERNEL_PROXY "
559 "does not support iSER protocol");
564 portal = portal_new(pg);
565 portal->p_listen = checked_strdup(value);
566 portal->p_iser = iser;
568 arg = portal->p_listen;
569 if (arg[0] == '\0') {
570 log_warnx("empty listen address");
571 free(portal->p_listen);
577 * IPv6 address in square brackets, perhaps with port.
580 addr = strsep(&arg, "]");
582 log_warnx("invalid listen address %s",
584 free(portal->p_listen);
588 if (arg[0] == '\0') {
590 } else if (arg[0] == ':') {
593 log_warnx("invalid listen address %s",
595 free(portal->p_listen);
601 * Either IPv6 address without brackets - and without
602 * a port - or IPv4 address. Just count the colons.
604 for (ch = arg; *ch != '\0'; ch++) {
612 addr = strsep(&arg, ":");
620 memset(&hints, 0, sizeof(hints));
621 hints.ai_family = PF_UNSPEC;
622 hints.ai_socktype = SOCK_STREAM;
623 hints.ai_flags = AI_PASSIVE;
625 error = getaddrinfo(addr, port, &hints, &portal->p_ai);
627 log_warnx("getaddrinfo for %s failed: %s",
628 portal->p_listen, gai_strerror(error));
629 free(portal->p_listen);
635 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
636 * those into multiple portals.
643 valid_hex(const char ch)
675 valid_iscsi_name(const char *name)
679 if (strlen(name) >= MAX_NAME_LEN) {
680 log_warnx("overlong name for target \"%s\"; max length allowed "
681 "by iSCSI specification is %d characters",
687 * In the cases below, we don't return an error, just in case the admin
688 * was right, and we're wrong.
690 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
691 for (i = strlen("iqn."); name[i] != '\0'; i++) {
693 * XXX: We should verify UTF-8 normalisation, as defined
694 * by 3.2.6.2: iSCSI Name Encoding.
696 if (isalnum(name[i]))
698 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
700 log_warnx("invalid character \"%c\" in target name "
701 "\"%s\"; allowed characters are letters, digits, "
702 "'-', '.', and ':'", name[i], name);
706 * XXX: Check more stuff: valid date and a valid reversed domain.
708 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
709 if (strlen(name) != strlen("eui.") + 16)
710 log_warnx("invalid target name \"%s\"; the \"eui.\" "
711 "should be followed by exactly 16 hexadecimal "
713 for (i = strlen("eui."); name[i] != '\0'; i++) {
714 if (!valid_hex(name[i])) {
715 log_warnx("invalid character \"%c\" in target "
716 "name \"%s\"; allowed characters are 1-9 "
717 "and A-F", name[i], name);
721 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
722 if (strlen(name) > strlen("naa.") + 32)
723 log_warnx("invalid target name \"%s\"; the \"naa.\" "
724 "should be followed by at most 32 hexadecimal "
726 for (i = strlen("naa."); name[i] != '\0'; i++) {
727 if (!valid_hex(name[i])) {
728 log_warnx("invalid character \"%c\" in target "
729 "name \"%s\"; allowed characters are 1-9 "
730 "and A-F", name[i], name);
735 log_warnx("invalid target name \"%s\"; should start with "
736 "either \".iqn\", \"eui.\", or \"naa.\"",
743 target_new(struct conf *conf, const char *name)
748 targ = target_find(conf, name);
750 log_warnx("duplicated target \"%s\"", name);
753 if (valid_iscsi_name(name) == false) {
754 log_warnx("target name \"%s\" is invalid", name);
757 targ = calloc(1, sizeof(*targ));
759 log_err(1, "calloc");
760 targ->t_name = checked_strdup(name);
763 * RFC 3722 requires us to normalize the name to lowercase.
766 for (i = 0; i < len; i++)
767 targ->t_name[i] = tolower(targ->t_name[i]);
769 TAILQ_INIT(&targ->t_luns);
771 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
777 target_delete(struct target *targ)
779 struct lun *lun, *tmp;
781 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
783 TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
790 target_find(struct conf *conf, const char *name)
794 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
795 if (strcasecmp(targ->t_name, name) == 0)
803 lun_new(struct target *targ, int lun_id)
807 lun = lun_find(targ, lun_id);
809 log_warnx("duplicated lun %d for target \"%s\"",
810 lun_id, targ->t_name);
814 lun = calloc(1, sizeof(*lun));
816 log_err(1, "calloc");
818 TAILQ_INIT(&lun->l_options);
819 lun->l_target = targ;
820 TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
826 lun_delete(struct lun *lun)
828 struct lun_option *lo, *tmp;
830 TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
832 TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
833 lun_option_delete(lo);
834 free(lun->l_backend);
835 free(lun->l_device_id);
842 lun_find(struct target *targ, int lun_id)
846 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
847 if (lun->l_lun == lun_id)
855 lun_set_backend(struct lun *lun, const char *value)
857 free(lun->l_backend);
858 lun->l_backend = checked_strdup(value);
862 lun_set_blocksize(struct lun *lun, size_t value)
865 lun->l_blocksize = value;
869 lun_set_device_id(struct lun *lun, const char *value)
871 free(lun->l_device_id);
872 lun->l_device_id = checked_strdup(value);
876 lun_set_path(struct lun *lun, const char *value)
879 lun->l_path = checked_strdup(value);
883 lun_set_serial(struct lun *lun, const char *value)
886 lun->l_serial = checked_strdup(value);
890 lun_set_size(struct lun *lun, size_t value)
897 lun_set_ctl_lun(struct lun *lun, uint32_t value)
900 lun->l_ctl_lun = value;
904 lun_option_new(struct lun *lun, const char *name, const char *value)
906 struct lun_option *lo;
908 lo = lun_option_find(lun, name);
910 log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
911 name, lun->l_lun, lun->l_target->t_name);
915 lo = calloc(1, sizeof(*lo));
917 log_err(1, "calloc");
918 lo->lo_name = checked_strdup(name);
919 lo->lo_value = checked_strdup(value);
921 TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
927 lun_option_delete(struct lun_option *lo)
930 TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
938 lun_option_find(struct lun *lun, const char *name)
940 struct lun_option *lo;
942 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
943 if (strcmp(lo->lo_name, name) == 0)
951 lun_option_set(struct lun_option *lo, const char *value)
955 lo->lo_value = checked_strdup(value);
958 static struct connection *
959 connection_new(struct portal *portal, int fd, const char *host)
961 struct connection *conn;
963 conn = calloc(1, sizeof(*conn));
965 log_err(1, "calloc");
966 conn->conn_portal = portal;
967 conn->conn_socket = fd;
968 conn->conn_initiator_addr = checked_strdup(host);
971 * Default values, from RFC 3720, section 12.
973 conn->conn_max_data_segment_length = 8192;
974 conn->conn_max_burst_length = 262144;
975 conn->conn_immediate_data = true;
982 conf_print(struct conf *conf)
984 struct auth_group *ag;
986 struct auth_name *auth_name;
987 struct auth_portal *auth_portal;
988 struct portal_group *pg;
989 struct portal *portal;
992 struct lun_option *lo;
994 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
995 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
996 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
997 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
998 auth->a_user, auth->a_secret,
999 auth->a_mutual_user, auth->a_mutual_secret);
1000 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1001 fprintf(stderr, "\t initiator-name %s\n",
1002 auth_name->an_initator_name);
1003 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1004 fprintf(stderr, "\t initiator-portal %s\n",
1005 auth_portal->an_initator_portal);
1006 fprintf(stderr, "}\n");
1008 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1009 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1010 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1011 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1012 fprintf(stderr, "}\n");
1014 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1015 fprintf(stderr, "target %s {\n", targ->t_name);
1016 if (targ->t_alias != NULL)
1017 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1018 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1019 fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1020 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1021 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1022 fprintf(stderr, "\t\toption %s %s\n",
1023 lo->lo_name, lo->lo_value);
1024 fprintf(stderr, "\t}\n");
1026 fprintf(stderr, "}\n");
1032 conf_verify_lun(struct lun *lun)
1034 const struct lun *lun2;
1035 const struct target *targ2;
1037 if (lun->l_backend == NULL)
1038 lun_set_backend(lun, "block");
1039 if (strcmp(lun->l_backend, "block") == 0) {
1040 if (lun->l_path == NULL) {
1041 log_warnx("missing path for lun %d, target \"%s\"",
1042 lun->l_lun, lun->l_target->t_name);
1045 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1046 if (lun->l_size == 0) {
1047 log_warnx("missing size for ramdisk-backed lun %d, "
1048 "target \"%s\"", lun->l_lun, lun->l_target->t_name);
1051 if (lun->l_path != NULL) {
1052 log_warnx("path must not be specified "
1053 "for ramdisk-backed lun %d, target \"%s\"",
1054 lun->l_lun, lun->l_target->t_name);
1058 if (lun->l_lun < 0 || lun->l_lun > 255) {
1059 log_warnx("invalid lun number for lun %d, target \"%s\"; "
1060 "must be between 0 and 255", lun->l_lun,
1061 lun->l_target->t_name);
1064 if (lun->l_blocksize == 0) {
1065 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1066 } else if (lun->l_blocksize < 0) {
1067 log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1068 "must be larger than 0", lun->l_lun, lun->l_target->t_name);
1071 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1072 log_warnx("invalid size for lun %d, target \"%s\"; "
1073 "must be multiple of blocksize", lun->l_lun,
1074 lun->l_target->t_name);
1077 TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1078 TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1081 if (lun->l_path != NULL && lun2->l_path != NULL &&
1082 strcmp(lun->l_path, lun2->l_path) == 0) {
1083 log_debugx("WARNING: path \"%s\" duplicated "
1084 "between lun %d, target \"%s\", and "
1085 "lun %d, target \"%s\"", lun->l_path,
1086 lun->l_lun, lun->l_target->t_name,
1087 lun2->l_lun, lun2->l_target->t_name);
1096 conf_verify(struct conf *conf)
1098 struct auth_group *ag;
1099 struct portal_group *pg;
1100 struct target *targ;
1105 if (conf->conf_pidfile_path == NULL)
1106 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1108 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1109 if (targ->t_auth_group == NULL) {
1110 targ->t_auth_group = auth_group_find(conf,
1112 assert(targ->t_auth_group != NULL);
1114 if (targ->t_portal_group == NULL) {
1115 targ->t_portal_group = portal_group_find(conf,
1117 assert(targ->t_portal_group != NULL);
1120 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1121 error = conf_verify_lun(lun);
1124 if (lun->l_lun == 0)
1128 log_warnx("mandatory LUN 0 not configured "
1129 "for target \"%s\"", targ->t_name);
1133 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1134 assert(pg->pg_name != NULL);
1135 if (pg->pg_discovery_auth_group == NULL) {
1136 pg->pg_discovery_auth_group =
1137 auth_group_find(conf, "default");
1138 assert(pg->pg_discovery_auth_group != NULL);
1141 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1142 if (targ->t_portal_group == pg)
1146 if (strcmp(pg->pg_name, "default") != 0)
1147 log_warnx("portal-group \"%s\" not assigned "
1148 "to any target", pg->pg_name);
1149 pg->pg_unassigned = true;
1151 pg->pg_unassigned = false;
1153 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1154 if (ag->ag_name == NULL)
1155 assert(ag->ag_target != NULL);
1157 assert(ag->ag_target == NULL);
1159 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1160 if (targ->t_auth_group == ag)
1163 if (targ == NULL && ag->ag_name != NULL &&
1164 strcmp(ag->ag_name, "default") != 0 &&
1165 strcmp(ag->ag_name, "no-authentication") != 0 &&
1166 strcmp(ag->ag_name, "no-access") != 0) {
1167 log_warnx("auth-group \"%s\" not assigned "
1168 "to any target", ag->ag_name);
1176 conf_apply(struct conf *oldconf, struct conf *newconf)
1178 struct target *oldtarg, *newtarg, *tmptarg;
1179 struct lun *oldlun, *newlun, *tmplun;
1180 struct portal_group *oldpg, *newpg;
1181 struct portal *oldp, *newp;
1183 int changed, cumulated_error = 0, error;
1184 #ifndef ICL_KERNEL_PROXY
1188 if (oldconf->conf_debug != newconf->conf_debug) {
1189 log_debugx("changing debug level to %d", newconf->conf_debug);
1190 log_init(newconf->conf_debug);
1193 if (oldconf->conf_pidfh != NULL) {
1194 assert(oldconf->conf_pidfile_path != NULL);
1195 if (newconf->conf_pidfile_path != NULL &&
1196 strcmp(oldconf->conf_pidfile_path,
1197 newconf->conf_pidfile_path) == 0) {
1198 newconf->conf_pidfh = oldconf->conf_pidfh;
1199 oldconf->conf_pidfh = NULL;
1201 log_debugx("removing pidfile %s",
1202 oldconf->conf_pidfile_path);
1203 pidfile_remove(oldconf->conf_pidfh);
1204 oldconf->conf_pidfh = NULL;
1208 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1209 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1210 newconf->conf_pidfh =
1211 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1212 if (newconf->conf_pidfh == NULL) {
1213 if (errno == EEXIST)
1214 log_errx(1, "daemon already running, pid: %jd.",
1215 (intmax_t)otherpid);
1216 log_err(1, "cannot open or create pidfile \"%s\"",
1217 newconf->conf_pidfile_path);
1221 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1223 * First, remove any targets present in the old configuration
1224 * and missing in the new one.
1226 newtarg = target_find(newconf, oldtarg->t_name);
1227 if (newtarg == NULL) {
1228 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1230 log_debugx("target %s not found in new "
1231 "configuration; removing its lun %d, "
1232 "backed by CTL lun %d",
1233 oldtarg->t_name, oldlun->l_lun,
1235 error = kernel_lun_remove(oldlun);
1237 log_warnx("failed to remove lun %d, "
1238 "target %s, CTL lun %d",
1239 oldlun->l_lun, oldtarg->t_name,
1245 target_delete(oldtarg);
1250 * Second, remove any LUNs present in the old target
1251 * and missing in the new one.
1253 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1254 newlun = lun_find(newtarg, oldlun->l_lun);
1255 if (newlun == NULL) {
1256 log_debugx("lun %d, target %s, CTL lun %d "
1257 "not found in new configuration; "
1258 "removing", oldlun->l_lun, oldtarg->t_name,
1260 error = kernel_lun_remove(oldlun);
1262 log_warnx("failed to remove lun %d, "
1263 "target %s, CTL lun %d",
1264 oldlun->l_lun, oldtarg->t_name,
1273 * Also remove the LUNs changed by more than size.
1276 assert(oldlun->l_backend != NULL);
1277 assert(newlun->l_backend != NULL);
1278 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1279 log_debugx("backend for lun %d, target %s, "
1280 "CTL lun %d changed; removing",
1281 oldlun->l_lun, oldtarg->t_name,
1285 if (oldlun->l_blocksize != newlun->l_blocksize) {
1286 log_debugx("blocksize for lun %d, target %s, "
1287 "CTL lun %d changed; removing",
1288 oldlun->l_lun, oldtarg->t_name,
1292 if (newlun->l_device_id != NULL &&
1293 (oldlun->l_device_id == NULL ||
1294 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1296 log_debugx("device-id for lun %d, target %s, "
1297 "CTL lun %d changed; removing",
1298 oldlun->l_lun, oldtarg->t_name,
1302 if (newlun->l_path != NULL &&
1303 (oldlun->l_path == NULL ||
1304 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1305 log_debugx("path for lun %d, target %s, "
1306 "CTL lun %d, changed; removing",
1307 oldlun->l_lun, oldtarg->t_name,
1311 if (newlun->l_serial != NULL &&
1312 (oldlun->l_serial == NULL ||
1313 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1314 log_debugx("serial for lun %d, target %s, "
1315 "CTL lun %d changed; removing",
1316 oldlun->l_lun, oldtarg->t_name,
1321 error = kernel_lun_remove(oldlun);
1323 log_warnx("failed to remove lun %d, "
1324 "target %s, CTL lun %d",
1325 oldlun->l_lun, oldtarg->t_name,
1333 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1338 * Now add new targets or modify existing ones.
1340 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1341 oldtarg = target_find(oldconf, newtarg->t_name);
1343 TAILQ_FOREACH(newlun, &newtarg->t_luns, l_next) {
1344 if (oldtarg != NULL) {
1345 oldlun = lun_find(oldtarg, newlun->l_lun);
1346 if (oldlun != NULL) {
1347 if (newlun->l_size != oldlun->l_size) {
1348 log_debugx("resizing lun %d, "
1349 "target %s, CTL lun %d",
1354 kernel_lun_resize(newlun);
1356 log_warnx("failed to "
1369 log_debugx("adding lun %d, target %s",
1370 newlun->l_lun, newtarg->t_name);
1371 error = kernel_lun_add(newlun);
1373 log_warnx("failed to add lun %d, target %s",
1374 newlun->l_lun, newtarg->t_name);
1381 * Go through the new portals, opening the sockets as neccessary.
1383 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1384 if (newpg->pg_unassigned) {
1385 log_debugx("not listening on portal-group \"%s\", "
1386 "not assigned to any target",
1390 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1392 * Try to find already open portal and reuse
1393 * the listening socket. We don't care about
1394 * what portal or portal group that was, what
1395 * matters is the listening address.
1397 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1399 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1401 if (strcmp(newp->p_listen,
1402 oldp->p_listen) == 0 &&
1403 oldp->p_socket > 0) {
1411 if (newp->p_socket > 0) {
1413 * We're done with this portal.
1418 #ifdef ICL_KERNEL_PROXY
1419 log_debugx("listening on %s, portal-group \"%s\" using ICL proxy",
1420 newp->p_listen, newpg->pg_name);
1421 kernel_listen(newp->p_ai, newp->p_iser);
1423 assert(newp->p_iser == false);
1425 log_debugx("listening on %s, portal-group \"%s\"",
1426 newp->p_listen, newpg->pg_name);
1427 newp->p_socket = socket(newp->p_ai->ai_family,
1428 newp->p_ai->ai_socktype,
1429 newp->p_ai->ai_protocol);
1430 if (newp->p_socket < 0) {
1431 log_warn("socket(2) failed for %s",
1436 error = setsockopt(newp->p_socket, SOL_SOCKET,
1437 SO_REUSEADDR, &one, sizeof(one));
1439 log_warn("setsockopt(SO_REUSEADDR) failed "
1440 "for %s", newp->p_listen);
1441 close(newp->p_socket);
1446 error = bind(newp->p_socket, newp->p_ai->ai_addr,
1447 newp->p_ai->ai_addrlen);
1449 log_warn("bind(2) failed for %s",
1451 close(newp->p_socket);
1456 error = listen(newp->p_socket, -1);
1458 log_warn("listen(2) failed for %s",
1460 close(newp->p_socket);
1465 #endif /* !ICL_KERNEL_PROXY */
1470 * Go through the no longer used sockets, closing them.
1472 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1473 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1474 if (oldp->p_socket <= 0)
1476 log_debugx("closing socket for %s, portal-group \"%s\"",
1477 oldp->p_listen, oldpg->pg_name);
1478 close(oldp->p_socket);
1483 return (cumulated_error);
1490 return (sigalrm_received);
1494 sigalrm_handler(int dummy __unused)
1497 * It would be easiest to just log an error and exit. We can't
1498 * do this, though, because log_errx() is not signal safe, since
1499 * it calls syslog(3). Instead, set a flag checked by pdu_send()
1500 * and pdu_receive(), to call log_errx() there. Should they fail
1501 * to notice, we'll exit here one second later.
1503 if (sigalrm_received) {
1505 * Oh well. Just give up and quit.
1510 sigalrm_received = true;
1514 set_timeout(const struct conf *conf)
1516 struct sigaction sa;
1517 struct itimerval itv;
1520 if (conf->conf_timeout <= 0) {
1521 log_debugx("session timeout disabled");
1525 bzero(&sa, sizeof(sa));
1526 sa.sa_handler = sigalrm_handler;
1527 sigfillset(&sa.sa_mask);
1528 error = sigaction(SIGALRM, &sa, NULL);
1530 log_err(1, "sigaction");
1533 * First SIGALRM will arive after conf_timeout seconds.
1534 * If we do nothing, another one will arrive a second later.
1536 bzero(&itv, sizeof(itv));
1537 itv.it_interval.tv_sec = 1;
1538 itv.it_value.tv_sec = conf->conf_timeout;
1540 log_debugx("setting session timeout to %d seconds",
1541 conf->conf_timeout);
1542 error = setitimer(ITIMER_REAL, &itv, NULL);
1544 log_err(1, "setitimer");
1548 wait_for_children(bool block)
1556 * If "block" is true, wait for at least one process.
1558 if (block && num == 0)
1559 pid = wait4(-1, &status, 0, NULL);
1561 pid = wait4(-1, &status, WNOHANG, NULL);
1564 if (WIFSIGNALED(status)) {
1565 log_warnx("child process %d terminated with signal %d",
1566 pid, WTERMSIG(status));
1567 } else if (WEXITSTATUS(status) != 0) {
1568 log_warnx("child process %d terminated with exit status %d",
1569 pid, WEXITSTATUS(status));
1571 log_debugx("child process %d terminated gracefully", pid);
1580 handle_connection(struct portal *portal, int fd, bool dont_fork)
1582 struct connection *conn;
1583 #ifndef ICL_KERNEL_PROXY
1584 struct sockaddr_storage ss;
1585 socklen_t sslen = sizeof(ss);
1589 char host[NI_MAXHOST + 1];
1592 conf = portal->p_portal_group->pg_conf;
1595 log_debugx("incoming connection; not forking due to -d flag");
1597 nchildren -= wait_for_children(false);
1598 assert(nchildren >= 0);
1600 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
1601 log_debugx("maxproc limit of %d child processes hit; "
1602 "waiting for child process to exit", conf->conf_maxproc);
1603 nchildren -= wait_for_children(true);
1604 assert(nchildren >= 0);
1606 log_debugx("incoming connection; forking child process #%d",
1617 pidfile_close(conf->conf_pidfh);
1619 #ifdef ICL_KERNEL_PROXY
1623 log_set_peer_addr("XXX");
1625 error = getpeername(fd, (struct sockaddr *)&ss, &sslen);
1627 log_err(1, "getpeername");
1628 error = getnameinfo((struct sockaddr *)&ss, sslen,
1629 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
1631 log_errx(1, "getaddrinfo: %s", gai_strerror(error));
1633 log_debugx("accepted connection from %s; portal group \"%s\"",
1634 host, portal->p_portal_group->pg_name);
1635 log_set_peer_addr(host);
1636 setproctitle("%s", host);
1639 conn = connection_new(portal, fd, host);
1643 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1644 kernel_handoff(conn);
1645 log_debugx("connection handed off to the kernel");
1647 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
1650 log_debugx("nothing more to do; exiting");
1654 #ifndef ICL_KERNEL_PROXY
1656 fd_add(int fd, fd_set *fdset, int nfds)
1660 * Skip sockets which we failed to bind.
1673 main_loop(struct conf *conf, bool dont_fork)
1675 struct portal_group *pg;
1676 struct portal *portal;
1677 #ifdef ICL_KERNEL_PROXY
1681 int error, nfds, client_fd;
1684 pidfile_write(conf->conf_pidfh);
1687 if (sighup_received || sigterm_received)
1690 #ifdef ICL_KERNEL_PROXY
1691 connection_id = kernel_accept();
1692 if (connection_id == 0)
1696 * XXX: This is obviously temporary.
1698 pg = TAILQ_FIRST(&conf->conf_portal_groups);
1699 portal = TAILQ_FIRST(&pg->pg_portals);
1701 handle_connection(portal, connection_id, dont_fork);
1705 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1706 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1707 nfds = fd_add(portal->p_socket, &fdset, nfds);
1709 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
1713 log_err(1, "select");
1715 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1716 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
1717 if (!FD_ISSET(portal->p_socket, &fdset))
1719 client_fd = accept(portal->p_socket, NULL, 0);
1721 log_err(1, "accept");
1722 handle_connection(portal, client_fd, dont_fork);
1726 #endif /* !ICL_KERNEL_PROXY */
1731 sighup_handler(int dummy __unused)
1734 sighup_received = true;
1738 sigterm_handler(int dummy __unused)
1741 sigterm_received = true;
1745 sigchld_handler(int dummy __unused)
1749 * The only purpose of this handler is to make SIGCHLD
1750 * interrupt the ISCSIDWAIT ioctl(2), so we can call
1751 * wait_for_children().
1756 register_signals(void)
1758 struct sigaction sa;
1761 bzero(&sa, sizeof(sa));
1762 sa.sa_handler = sighup_handler;
1763 sigfillset(&sa.sa_mask);
1764 error = sigaction(SIGHUP, &sa, NULL);
1766 log_err(1, "sigaction");
1768 sa.sa_handler = sigterm_handler;
1769 error = sigaction(SIGTERM, &sa, NULL);
1771 log_err(1, "sigaction");
1773 sa.sa_handler = sigterm_handler;
1774 error = sigaction(SIGINT, &sa, NULL);
1776 log_err(1, "sigaction");
1778 sa.sa_handler = sigchld_handler;
1779 error = sigaction(SIGCHLD, &sa, NULL);
1781 log_err(1, "sigaction");
1785 main(int argc, char **argv)
1787 struct conf *oldconf, *newconf, *tmpconf;
1788 const char *config_path = DEFAULT_CONFIG_PATH;
1789 int debug = 0, ch, error;
1790 bool dont_daemonize = false;
1792 while ((ch = getopt(argc, argv, "df:")) != -1) {
1795 dont_daemonize = true;
1799 config_path = optarg;
1813 oldconf = conf_new_from_kernel();
1814 newconf = conf_new_from_file(config_path);
1815 if (newconf == NULL)
1816 log_errx(1, "configuration error, exiting");
1818 oldconf->conf_debug = debug;
1819 newconf->conf_debug = debug;
1822 #ifdef ICL_KERNEL_PROXY
1823 log_debugx("enabling CTL iSCSI port");
1824 error = kernel_port_on();
1826 log_errx(1, "failed to enable CTL iSCSI port, exiting");
1829 error = conf_apply(oldconf, newconf);
1831 log_errx(1, "failed to apply configuration, exiting");
1832 conf_delete(oldconf);
1837 #ifndef ICL_KERNEL_PROXY
1838 log_debugx("enabling CTL iSCSI port");
1839 error = kernel_port_on();
1841 log_errx(1, "failed to enable CTL iSCSI port, exiting");
1844 if (dont_daemonize == false) {
1845 log_debugx("daemonizing");
1846 if (daemon(0, 0) == -1) {
1847 log_warn("cannot daemonize");
1848 pidfile_remove(newconf->conf_pidfh);
1854 main_loop(newconf, dont_daemonize);
1855 if (sighup_received) {
1856 sighup_received = false;
1857 log_debugx("received SIGHUP, reloading configuration");
1858 tmpconf = conf_new_from_file(config_path);
1859 if (tmpconf == NULL) {
1860 log_warnx("configuration error, "
1861 "continuing with old configuration");
1864 tmpconf->conf_debug = debug;
1867 error = conf_apply(oldconf, newconf);
1869 log_warnx("failed to reload "
1871 conf_delete(oldconf);
1874 } else if (sigterm_received) {
1875 log_debugx("exiting on signal; "
1876 "reloading empty configuration");
1878 log_debugx("disabling CTL iSCSI port "
1879 "and terminating all connections");
1880 error = kernel_port_off();
1882 log_warnx("failed to disable CTL iSCSI port");
1885 newconf = conf_new();
1887 newconf->conf_debug = debug;
1888 error = conf_apply(oldconf, newconf);
1890 log_warnx("failed to apply configuration");
1892 log_warnx("exiting on signal");
1895 nchildren -= wait_for_children(false);
1896 assert(nchildren >= 0);