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, "chap") == 0) {
444 } else if (strcmp(str, "chap-mutual") == 0) {
445 type = AG_TYPE_CHAP_MUTUAL;
447 if (ag->ag_name != NULL)
448 log_warnx("invalid auth-type \"%s\" for auth-group "
449 "\"%s\"", str, ag->ag_name);
451 log_warnx("invalid auth-type \"%s\" for target "
452 "\"%s\"", str, ag->ag_target->t_name);
456 error = auth_group_set_type(ag, type);
458 if (ag->ag_name != NULL)
459 log_warnx("cannot set auth-type to \"%s\" for "
460 "auth-group \"%s\"; already has a different "
461 "type", str, ag->ag_name);
463 log_warnx("cannot set auth-type to \"%s\" for target "
464 "\"%s\"; already has a different type",
465 str, ag->ag_target->t_name);
472 static struct portal *
473 portal_new(struct portal_group *pg)
475 struct portal *portal;
477 portal = calloc(1, sizeof(*portal));
479 log_err(1, "calloc");
480 TAILQ_INIT(&portal->p_targets);
481 portal->p_portal_group = pg;
482 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
487 portal_delete(struct portal *portal)
489 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
490 freeaddrinfo(portal->p_ai);
491 free(portal->p_listen);
495 struct portal_group *
496 portal_group_new(struct conf *conf, const char *name)
498 struct portal_group *pg;
500 pg = portal_group_find(conf, name);
502 log_warnx("duplicated portal-group \"%s\"", name);
506 pg = calloc(1, sizeof(*pg));
508 log_err(1, "calloc");
509 pg->pg_name = checked_strdup(name);
510 TAILQ_INIT(&pg->pg_portals);
512 conf->conf_last_portal_group_tag++;
513 pg->pg_tag = conf->conf_last_portal_group_tag;
514 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
520 portal_group_delete(struct portal_group *pg)
522 struct portal *portal, *tmp;
524 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
526 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
527 portal_delete(portal);
532 struct portal_group *
533 portal_group_find(struct conf *conf, const char *name)
535 struct portal_group *pg;
537 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
538 if (strcmp(pg->pg_name, name) == 0)
546 portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
548 struct addrinfo hints;
549 struct portal *portal;
550 char *addr, *ch, *arg;
552 int error, colons = 0;
554 #ifndef ICL_KERNEL_PROXY
556 log_warnx("ctld(8) compiled without ICL_KERNEL_PROXY "
557 "does not support iSER protocol");
562 portal = portal_new(pg);
563 portal->p_listen = checked_strdup(value);
564 portal->p_iser = iser;
566 arg = portal->p_listen;
567 if (arg[0] == '\0') {
568 log_warnx("empty listen address");
569 free(portal->p_listen);
575 * IPv6 address in square brackets, perhaps with port.
578 addr = strsep(&arg, "]");
580 log_warnx("invalid listen address %s",
582 free(portal->p_listen);
586 if (arg[0] == '\0') {
588 } else if (arg[0] == ':') {
591 log_warnx("invalid listen address %s",
593 free(portal->p_listen);
599 * Either IPv6 address without brackets - and without
600 * a port - or IPv4 address. Just count the colons.
602 for (ch = arg; *ch != '\0'; ch++) {
610 addr = strsep(&arg, ":");
618 memset(&hints, 0, sizeof(hints));
619 hints.ai_family = PF_UNSPEC;
620 hints.ai_socktype = SOCK_STREAM;
621 hints.ai_flags = AI_PASSIVE;
623 error = getaddrinfo(addr, port, &hints, &portal->p_ai);
625 log_warnx("getaddrinfo for %s failed: %s",
626 portal->p_listen, gai_strerror(error));
627 free(portal->p_listen);
633 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
634 * those into multiple portals.
641 valid_hex(const char ch)
673 valid_iscsi_name(const char *name)
677 if (strlen(name) >= MAX_NAME_LEN) {
678 log_warnx("overlong name for target \"%s\"; max length allowed "
679 "by iSCSI specification is %d characters",
685 * In the cases below, we don't return an error, just in case the admin
686 * was right, and we're wrong.
688 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
689 for (i = strlen("iqn."); name[i] != '\0'; i++) {
691 * XXX: We should verify UTF-8 normalisation, as defined
692 * by 3.2.6.2: iSCSI Name Encoding.
694 if (isalnum(name[i]))
696 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
698 log_warnx("invalid character \"%c\" in target name "
699 "\"%s\"; allowed characters are letters, digits, "
700 "'-', '.', and ':'", name[i], name);
704 * XXX: Check more stuff: valid date and a valid reversed domain.
706 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
707 if (strlen(name) != strlen("eui.") + 16)
708 log_warnx("invalid target name \"%s\"; the \"eui.\" "
709 "should be followed by exactly 16 hexadecimal "
711 for (i = strlen("eui."); name[i] != '\0'; i++) {
712 if (!valid_hex(name[i])) {
713 log_warnx("invalid character \"%c\" in target "
714 "name \"%s\"; allowed characters are 1-9 "
715 "and A-F", name[i], name);
719 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
720 if (strlen(name) > strlen("naa.") + 32)
721 log_warnx("invalid target name \"%s\"; the \"naa.\" "
722 "should be followed by at most 32 hexadecimal "
724 for (i = strlen("naa."); name[i] != '\0'; i++) {
725 if (!valid_hex(name[i])) {
726 log_warnx("invalid character \"%c\" in target "
727 "name \"%s\"; allowed characters are 1-9 "
728 "and A-F", name[i], name);
733 log_warnx("invalid target name \"%s\"; should start with "
734 "either \".iqn\", \"eui.\", or \"naa.\"",
741 target_new(struct conf *conf, const char *name)
746 targ = target_find(conf, name);
748 log_warnx("duplicated target \"%s\"", name);
751 if (valid_iscsi_name(name) == false) {
752 log_warnx("target name \"%s\" is invalid", name);
755 targ = calloc(1, sizeof(*targ));
757 log_err(1, "calloc");
758 targ->t_name = checked_strdup(name);
761 * RFC 3722 requires us to normalize the name to lowercase.
764 for (i = 0; i < len; i++)
765 targ->t_name[i] = tolower(targ->t_name[i]);
767 TAILQ_INIT(&targ->t_luns);
769 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
775 target_delete(struct target *targ)
777 struct lun *lun, *tmp;
779 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
781 TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
788 target_find(struct conf *conf, const char *name)
792 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
793 if (strcasecmp(targ->t_name, name) == 0)
801 lun_new(struct target *targ, int lun_id)
805 lun = lun_find(targ, lun_id);
807 log_warnx("duplicated lun %d for target \"%s\"",
808 lun_id, targ->t_name);
812 lun = calloc(1, sizeof(*lun));
814 log_err(1, "calloc");
816 TAILQ_INIT(&lun->l_options);
817 lun->l_target = targ;
818 TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
824 lun_delete(struct lun *lun)
826 struct lun_option *lo, *tmp;
828 TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
830 TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
831 lun_option_delete(lo);
832 free(lun->l_backend);
833 free(lun->l_device_id);
840 lun_find(struct target *targ, int lun_id)
844 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
845 if (lun->l_lun == lun_id)
853 lun_set_backend(struct lun *lun, const char *value)
855 free(lun->l_backend);
856 lun->l_backend = checked_strdup(value);
860 lun_set_blocksize(struct lun *lun, size_t value)
863 lun->l_blocksize = value;
867 lun_set_device_id(struct lun *lun, const char *value)
869 free(lun->l_device_id);
870 lun->l_device_id = checked_strdup(value);
874 lun_set_path(struct lun *lun, const char *value)
877 lun->l_path = checked_strdup(value);
881 lun_set_serial(struct lun *lun, const char *value)
884 lun->l_serial = checked_strdup(value);
888 lun_set_size(struct lun *lun, size_t value)
895 lun_set_ctl_lun(struct lun *lun, uint32_t value)
898 lun->l_ctl_lun = value;
902 lun_option_new(struct lun *lun, const char *name, const char *value)
904 struct lun_option *lo;
906 lo = lun_option_find(lun, name);
908 log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
909 name, lun->l_lun, lun->l_target->t_name);
913 lo = calloc(1, sizeof(*lo));
915 log_err(1, "calloc");
916 lo->lo_name = checked_strdup(name);
917 lo->lo_value = checked_strdup(value);
919 TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
925 lun_option_delete(struct lun_option *lo)
928 TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
936 lun_option_find(struct lun *lun, const char *name)
938 struct lun_option *lo;
940 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
941 if (strcmp(lo->lo_name, name) == 0)
949 lun_option_set(struct lun_option *lo, const char *value)
953 lo->lo_value = checked_strdup(value);
956 static struct connection *
957 connection_new(struct portal *portal, int fd, const char *host)
959 struct connection *conn;
961 conn = calloc(1, sizeof(*conn));
963 log_err(1, "calloc");
964 conn->conn_portal = portal;
965 conn->conn_socket = fd;
966 conn->conn_initiator_addr = checked_strdup(host);
969 * Default values, from RFC 3720, section 12.
971 conn->conn_max_data_segment_length = 8192;
972 conn->conn_max_burst_length = 262144;
973 conn->conn_immediate_data = true;
980 conf_print(struct conf *conf)
982 struct auth_group *ag;
984 struct auth_name *auth_name;
985 struct auth_portal *auth_portal;
986 struct portal_group *pg;
987 struct portal *portal;
990 struct lun_option *lo;
992 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
993 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
994 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
995 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
996 auth->a_user, auth->a_secret,
997 auth->a_mutual_user, auth->a_mutual_secret);
998 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
999 fprintf(stderr, "\t initiator-name %s\n",
1000 auth_name->an_initator_name);
1001 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1002 fprintf(stderr, "\t initiator-portal %s\n",
1003 auth_portal->an_initator_portal);
1004 fprintf(stderr, "}\n");
1006 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1007 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1008 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1009 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1010 fprintf(stderr, "}\n");
1012 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1013 fprintf(stderr, "target %s {\n", targ->t_name);
1014 if (targ->t_alias != NULL)
1015 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1016 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1017 fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1018 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1019 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1020 fprintf(stderr, "\t\toption %s %s\n",
1021 lo->lo_name, lo->lo_value);
1022 fprintf(stderr, "\t}\n");
1024 fprintf(stderr, "}\n");
1030 conf_verify_lun(struct lun *lun)
1032 const struct lun *lun2;
1033 const struct target *targ2;
1035 if (lun->l_backend == NULL)
1036 lun_set_backend(lun, "block");
1037 if (strcmp(lun->l_backend, "block") == 0) {
1038 if (lun->l_path == NULL) {
1039 log_warnx("missing path for lun %d, target \"%s\"",
1040 lun->l_lun, lun->l_target->t_name);
1043 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1044 if (lun->l_size == 0) {
1045 log_warnx("missing size for ramdisk-backed lun %d, "
1046 "target \"%s\"", lun->l_lun, lun->l_target->t_name);
1049 if (lun->l_path != NULL) {
1050 log_warnx("path must not be specified "
1051 "for ramdisk-backed lun %d, target \"%s\"",
1052 lun->l_lun, lun->l_target->t_name);
1056 if (lun->l_lun < 0 || lun->l_lun > 255) {
1057 log_warnx("invalid lun number for lun %d, target \"%s\"; "
1058 "must be between 0 and 255", lun->l_lun,
1059 lun->l_target->t_name);
1062 if (lun->l_blocksize == 0) {
1063 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1064 } else if (lun->l_blocksize < 0) {
1065 log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1066 "must be larger than 0", lun->l_lun, lun->l_target->t_name);
1069 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1070 log_warnx("invalid size for lun %d, target \"%s\"; "
1071 "must be multiple of blocksize", lun->l_lun,
1072 lun->l_target->t_name);
1075 TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1076 TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1079 if (lun->l_path != NULL && lun2->l_path != NULL &&
1080 strcmp(lun->l_path, lun2->l_path) == 0) {
1081 log_debugx("WARNING: path \"%s\" duplicated "
1082 "between lun %d, target \"%s\", and "
1083 "lun %d, target \"%s\"", lun->l_path,
1084 lun->l_lun, lun->l_target->t_name,
1085 lun2->l_lun, lun2->l_target->t_name);
1094 conf_verify(struct conf *conf)
1096 struct auth_group *ag;
1097 struct portal_group *pg;
1098 struct target *targ;
1103 if (conf->conf_pidfile_path == NULL)
1104 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1106 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1107 if (targ->t_auth_group == NULL) {
1108 log_warnx("missing authentication for target \"%s\"; "
1109 "must specify either \"auth-group\", \"chap\", "
1110 "or \"chap-mutual\"", targ->t_name);
1113 if (targ->t_portal_group == NULL) {
1114 targ->t_portal_group = portal_group_find(conf,
1116 assert(targ->t_portal_group != NULL);
1119 TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1120 error = conf_verify_lun(lun);
1123 if (lun->l_lun == 0)
1127 log_warnx("mandatory LUN 0 not configured "
1128 "for target \"%s\"", targ->t_name);
1132 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1133 assert(pg->pg_name != NULL);
1134 if (pg->pg_discovery_auth_group == NULL) {
1135 pg->pg_discovery_auth_group =
1136 auth_group_find(conf, "no-access");
1137 assert(pg->pg_discovery_auth_group != NULL);
1140 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1141 if (targ->t_portal_group == pg)
1145 if (strcmp(pg->pg_name, "default") != 0)
1146 log_warnx("portal-group \"%s\" not assigned "
1147 "to any target", pg->pg_name);
1148 pg->pg_unassigned = true;
1150 pg->pg_unassigned = false;
1152 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1153 if (ag->ag_name == NULL)
1154 assert(ag->ag_target != NULL);
1156 assert(ag->ag_target == NULL);
1158 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1159 if (targ->t_auth_group == ag)
1162 if (targ == NULL && ag->ag_name != NULL &&
1163 strcmp(ag->ag_name, "no-authentication") != 0 &&
1164 strcmp(ag->ag_name, "no-access") != 0) {
1165 log_warnx("auth-group \"%s\" not assigned "
1166 "to any target", ag->ag_name);
1174 conf_apply(struct conf *oldconf, struct conf *newconf)
1176 struct target *oldtarg, *newtarg, *tmptarg;
1177 struct lun *oldlun, *newlun, *tmplun;
1178 struct portal_group *oldpg, *newpg;
1179 struct portal *oldp, *newp;
1181 int changed, cumulated_error = 0, error;
1182 #ifndef ICL_KERNEL_PROXY
1186 if (oldconf->conf_debug != newconf->conf_debug) {
1187 log_debugx("changing debug level to %d", newconf->conf_debug);
1188 log_init(newconf->conf_debug);
1191 if (oldconf->conf_pidfh != NULL) {
1192 assert(oldconf->conf_pidfile_path != NULL);
1193 if (newconf->conf_pidfile_path != NULL &&
1194 strcmp(oldconf->conf_pidfile_path,
1195 newconf->conf_pidfile_path) == 0) {
1196 newconf->conf_pidfh = oldconf->conf_pidfh;
1197 oldconf->conf_pidfh = NULL;
1199 log_debugx("removing pidfile %s",
1200 oldconf->conf_pidfile_path);
1201 pidfile_remove(oldconf->conf_pidfh);
1202 oldconf->conf_pidfh = NULL;
1206 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1207 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1208 newconf->conf_pidfh =
1209 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1210 if (newconf->conf_pidfh == NULL) {
1211 if (errno == EEXIST)
1212 log_errx(1, "daemon already running, pid: %jd.",
1213 (intmax_t)otherpid);
1214 log_err(1, "cannot open or create pidfile \"%s\"",
1215 newconf->conf_pidfile_path);
1219 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1221 * First, remove any targets present in the old configuration
1222 * and missing in the new one.
1224 newtarg = target_find(newconf, oldtarg->t_name);
1225 if (newtarg == NULL) {
1226 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1228 log_debugx("target %s not found in new "
1229 "configuration; removing its lun %d, "
1230 "backed by CTL lun %d",
1231 oldtarg->t_name, oldlun->l_lun,
1233 error = kernel_lun_remove(oldlun);
1235 log_warnx("failed to remove lun %d, "
1236 "target %s, CTL lun %d",
1237 oldlun->l_lun, oldtarg->t_name,
1243 target_delete(oldtarg);
1248 * Second, remove any LUNs present in the old target
1249 * and missing in the new one.
1251 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1252 newlun = lun_find(newtarg, oldlun->l_lun);
1253 if (newlun == NULL) {
1254 log_debugx("lun %d, target %s, CTL lun %d "
1255 "not found in new configuration; "
1256 "removing", oldlun->l_lun, oldtarg->t_name,
1258 error = kernel_lun_remove(oldlun);
1260 log_warnx("failed to remove lun %d, "
1261 "target %s, CTL lun %d",
1262 oldlun->l_lun, oldtarg->t_name,
1271 * Also remove the LUNs changed by more than size.
1274 assert(oldlun->l_backend != NULL);
1275 assert(newlun->l_backend != NULL);
1276 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1277 log_debugx("backend for lun %d, target %s, "
1278 "CTL lun %d changed; removing",
1279 oldlun->l_lun, oldtarg->t_name,
1283 if (oldlun->l_blocksize != newlun->l_blocksize) {
1284 log_debugx("blocksize for lun %d, target %s, "
1285 "CTL lun %d changed; removing",
1286 oldlun->l_lun, oldtarg->t_name,
1290 if (newlun->l_device_id != NULL &&
1291 (oldlun->l_device_id == NULL ||
1292 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1294 log_debugx("device-id for lun %d, target %s, "
1295 "CTL lun %d changed; removing",
1296 oldlun->l_lun, oldtarg->t_name,
1300 if (newlun->l_path != NULL &&
1301 (oldlun->l_path == NULL ||
1302 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1303 log_debugx("path for lun %d, target %s, "
1304 "CTL lun %d, changed; removing",
1305 oldlun->l_lun, oldtarg->t_name,
1309 if (newlun->l_serial != NULL &&
1310 (oldlun->l_serial == NULL ||
1311 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1312 log_debugx("serial for lun %d, target %s, "
1313 "CTL lun %d changed; removing",
1314 oldlun->l_lun, oldtarg->t_name,
1319 error = kernel_lun_remove(oldlun);
1321 log_warnx("failed to remove lun %d, "
1322 "target %s, CTL lun %d",
1323 oldlun->l_lun, oldtarg->t_name,
1331 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1336 * Now add new targets or modify existing ones.
1338 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1339 oldtarg = target_find(oldconf, newtarg->t_name);
1341 TAILQ_FOREACH(newlun, &newtarg->t_luns, l_next) {
1342 if (oldtarg != NULL) {
1343 oldlun = lun_find(oldtarg, newlun->l_lun);
1344 if (oldlun != NULL) {
1345 if (newlun->l_size != oldlun->l_size) {
1346 log_debugx("resizing lun %d, "
1347 "target %s, CTL lun %d",
1352 kernel_lun_resize(newlun);
1354 log_warnx("failed to "
1367 log_debugx("adding lun %d, target %s",
1368 newlun->l_lun, newtarg->t_name);
1369 error = kernel_lun_add(newlun);
1371 log_warnx("failed to add lun %d, target %s",
1372 newlun->l_lun, newtarg->t_name);
1379 * Go through the new portals, opening the sockets as neccessary.
1381 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1382 if (newpg->pg_unassigned) {
1383 log_debugx("not listening on portal-group \"%s\", "
1384 "not assigned to any target",
1388 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1390 * Try to find already open portal and reuse
1391 * the listening socket. We don't care about
1392 * what portal or portal group that was, what
1393 * matters is the listening address.
1395 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1397 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1399 if (strcmp(newp->p_listen,
1400 oldp->p_listen) == 0 &&
1401 oldp->p_socket > 0) {
1409 if (newp->p_socket > 0) {
1411 * We're done with this portal.
1416 #ifdef ICL_KERNEL_PROXY
1417 log_debugx("listening on %s, portal-group \"%s\" using ICL proxy",
1418 newp->p_listen, newpg->pg_name);
1419 kernel_listen(newp->p_ai, newp->p_iser);
1421 assert(newp->p_iser == false);
1423 log_debugx("listening on %s, portal-group \"%s\"",
1424 newp->p_listen, newpg->pg_name);
1425 newp->p_socket = socket(newp->p_ai->ai_family,
1426 newp->p_ai->ai_socktype,
1427 newp->p_ai->ai_protocol);
1428 if (newp->p_socket < 0) {
1429 log_warn("socket(2) failed for %s",
1434 error = setsockopt(newp->p_socket, SOL_SOCKET,
1435 SO_REUSEADDR, &one, sizeof(one));
1437 log_warn("setsockopt(SO_REUSEADDR) failed "
1438 "for %s", newp->p_listen);
1439 close(newp->p_socket);
1444 error = bind(newp->p_socket, newp->p_ai->ai_addr,
1445 newp->p_ai->ai_addrlen);
1447 log_warn("bind(2) failed for %s",
1449 close(newp->p_socket);
1454 error = listen(newp->p_socket, -1);
1456 log_warn("listen(2) failed for %s",
1458 close(newp->p_socket);
1463 #endif /* !ICL_KERNEL_PROXY */
1468 * Go through the no longer used sockets, closing them.
1470 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1471 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1472 if (oldp->p_socket <= 0)
1474 log_debugx("closing socket for %s, portal-group \"%s\"",
1475 oldp->p_listen, oldpg->pg_name);
1476 close(oldp->p_socket);
1481 return (cumulated_error);
1488 return (sigalrm_received);
1492 sigalrm_handler(int dummy __unused)
1495 * It would be easiest to just log an error and exit. We can't
1496 * do this, though, because log_errx() is not signal safe, since
1497 * it calls syslog(3). Instead, set a flag checked by pdu_send()
1498 * and pdu_receive(), to call log_errx() there. Should they fail
1499 * to notice, we'll exit here one second later.
1501 if (sigalrm_received) {
1503 * Oh well. Just give up and quit.
1508 sigalrm_received = true;
1512 set_timeout(const struct conf *conf)
1514 struct sigaction sa;
1515 struct itimerval itv;
1518 if (conf->conf_timeout <= 0) {
1519 log_debugx("session timeout disabled");
1523 bzero(&sa, sizeof(sa));
1524 sa.sa_handler = sigalrm_handler;
1525 sigfillset(&sa.sa_mask);
1526 error = sigaction(SIGALRM, &sa, NULL);
1528 log_err(1, "sigaction");
1531 * First SIGALRM will arive after conf_timeout seconds.
1532 * If we do nothing, another one will arrive a second later.
1534 bzero(&itv, sizeof(itv));
1535 itv.it_interval.tv_sec = 1;
1536 itv.it_value.tv_sec = conf->conf_timeout;
1538 log_debugx("setting session timeout to %d seconds",
1539 conf->conf_timeout);
1540 error = setitimer(ITIMER_REAL, &itv, NULL);
1542 log_err(1, "setitimer");
1546 wait_for_children(bool block)
1554 * If "block" is true, wait for at least one process.
1556 if (block && num == 0)
1557 pid = wait4(-1, &status, 0, NULL);
1559 pid = wait4(-1, &status, WNOHANG, NULL);
1562 if (WIFSIGNALED(status)) {
1563 log_warnx("child process %d terminated with signal %d",
1564 pid, WTERMSIG(status));
1565 } else if (WEXITSTATUS(status) != 0) {
1566 log_warnx("child process %d terminated with exit status %d",
1567 pid, WEXITSTATUS(status));
1569 log_debugx("child process %d terminated gracefully", pid);
1578 handle_connection(struct portal *portal, int fd, bool dont_fork)
1580 struct connection *conn;
1581 #ifndef ICL_KERNEL_PROXY
1582 struct sockaddr_storage ss;
1583 socklen_t sslen = sizeof(ss);
1587 char host[NI_MAXHOST + 1];
1590 conf = portal->p_portal_group->pg_conf;
1593 log_debugx("incoming connection; not forking due to -d flag");
1595 nchildren -= wait_for_children(false);
1596 assert(nchildren >= 0);
1598 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
1599 log_debugx("maxproc limit of %d child processes hit; "
1600 "waiting for child process to exit", conf->conf_maxproc);
1601 nchildren -= wait_for_children(true);
1602 assert(nchildren >= 0);
1604 log_debugx("incoming connection; forking child process #%d",
1615 pidfile_close(conf->conf_pidfh);
1617 #ifdef ICL_KERNEL_PROXY
1621 log_set_peer_addr("XXX");
1623 error = getpeername(fd, (struct sockaddr *)&ss, &sslen);
1625 log_err(1, "getpeername");
1626 error = getnameinfo((struct sockaddr *)&ss, sslen,
1627 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
1629 log_errx(1, "getaddrinfo: %s", gai_strerror(error));
1631 log_debugx("accepted connection from %s; portal group \"%s\"",
1632 host, portal->p_portal_group->pg_name);
1633 log_set_peer_addr(host);
1634 setproctitle("%s", host);
1637 conn = connection_new(portal, fd, host);
1641 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1642 kernel_handoff(conn);
1643 log_debugx("connection handed off to the kernel");
1645 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
1648 log_debugx("nothing more to do; exiting");
1652 #ifndef ICL_KERNEL_PROXY
1654 fd_add(int fd, fd_set *fdset, int nfds)
1658 * Skip sockets which we failed to bind.
1671 main_loop(struct conf *conf, bool dont_fork)
1673 struct portal_group *pg;
1674 struct portal *portal;
1675 #ifdef ICL_KERNEL_PROXY
1679 int error, nfds, client_fd;
1682 pidfile_write(conf->conf_pidfh);
1685 if (sighup_received || sigterm_received)
1688 #ifdef ICL_KERNEL_PROXY
1689 connection_id = kernel_accept();
1690 if (connection_id == 0)
1694 * XXX: This is obviously temporary.
1696 pg = TAILQ_FIRST(&conf->conf_portal_groups);
1697 portal = TAILQ_FIRST(&pg->pg_portals);
1699 handle_connection(portal, connection_id, dont_fork);
1703 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1704 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1705 nfds = fd_add(portal->p_socket, &fdset, nfds);
1707 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
1711 log_err(1, "select");
1713 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1714 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
1715 if (!FD_ISSET(portal->p_socket, &fdset))
1717 client_fd = accept(portal->p_socket, NULL, 0);
1719 log_err(1, "accept");
1720 handle_connection(portal, client_fd, dont_fork);
1724 #endif /* !ICL_KERNEL_PROXY */
1729 sighup_handler(int dummy __unused)
1732 sighup_received = true;
1736 sigterm_handler(int dummy __unused)
1739 sigterm_received = true;
1743 register_signals(void)
1745 struct sigaction sa;
1748 bzero(&sa, sizeof(sa));
1749 sa.sa_handler = sighup_handler;
1750 sigfillset(&sa.sa_mask);
1751 error = sigaction(SIGHUP, &sa, NULL);
1753 log_err(1, "sigaction");
1755 sa.sa_handler = sigterm_handler;
1756 error = sigaction(SIGTERM, &sa, NULL);
1758 log_err(1, "sigaction");
1760 sa.sa_handler = sigterm_handler;
1761 error = sigaction(SIGINT, &sa, NULL);
1763 log_err(1, "sigaction");
1767 main(int argc, char **argv)
1769 struct conf *oldconf, *newconf, *tmpconf;
1770 const char *config_path = DEFAULT_CONFIG_PATH;
1771 int debug = 0, ch, error;
1772 bool dont_daemonize = false;
1774 while ((ch = getopt(argc, argv, "df:")) != -1) {
1777 dont_daemonize = true;
1781 config_path = optarg;
1795 oldconf = conf_new_from_kernel();
1796 newconf = conf_new_from_file(config_path);
1797 if (newconf == NULL)
1798 log_errx(1, "configuration error, exiting");
1800 oldconf->conf_debug = debug;
1801 newconf->conf_debug = debug;
1804 #ifdef ICL_KERNEL_PROXY
1805 log_debugx("enabling CTL iSCSI port");
1806 error = kernel_port_on();
1808 log_errx(1, "failed to enable CTL iSCSI port, exiting");
1811 error = conf_apply(oldconf, newconf);
1813 log_errx(1, "failed to apply configuration, exiting");
1814 conf_delete(oldconf);
1819 #ifndef ICL_KERNEL_PROXY
1820 log_debugx("enabling CTL iSCSI port");
1821 error = kernel_port_on();
1823 log_errx(1, "failed to enable CTL iSCSI port, exiting");
1826 if (dont_daemonize == false) {
1827 log_debugx("daemonizing");
1828 if (daemon(0, 0) == -1) {
1829 log_warn("cannot daemonize");
1830 pidfile_remove(newconf->conf_pidfh);
1836 main_loop(newconf, dont_daemonize);
1837 if (sighup_received) {
1838 sighup_received = false;
1839 log_debugx("received SIGHUP, reloading configuration");
1840 tmpconf = conf_new_from_file(config_path);
1841 if (tmpconf == NULL) {
1842 log_warnx("configuration error, "
1843 "continuing with old configuration");
1846 tmpconf->conf_debug = debug;
1849 error = conf_apply(oldconf, newconf);
1851 log_warnx("failed to reload "
1853 conf_delete(oldconf);
1856 } else if (sigterm_received) {
1857 log_debugx("exiting on signal; "
1858 "reloading empty configuration");
1860 log_debugx("disabling CTL iSCSI port "
1861 "and terminating all connections");
1862 error = kernel_port_off();
1864 log_warnx("failed to disable CTL iSCSI port");
1867 newconf = conf_new();
1869 newconf->conf_debug = debug;
1870 error = conf_apply(oldconf, newconf);
1872 log_warnx("failed to apply configuration");
1874 log_warnx("exiting on signal");
1877 nchildren -= wait_for_children(false);
1878 assert(nchildren >= 0);