xref: /freebsd/usr.sbin/ctld/ctld.c (revision 398290f29ab69fdbc2da1a55905b303a2cb118ed)
1009ea47eSEdward Tomasz Napierala /*-
2009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
3009ea47eSEdward Tomasz Napierala  * All rights reserved.
4009ea47eSEdward Tomasz Napierala  *
5009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
6009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
7009ea47eSEdward Tomasz Napierala  *
8009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
9009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
10009ea47eSEdward Tomasz Napierala  * are met:
11009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
12009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
13009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
14009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
15009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
16009ea47eSEdward Tomasz Napierala  *
17009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
28009ea47eSEdward Tomasz Napierala  *
29009ea47eSEdward Tomasz Napierala  */
30009ea47eSEdward Tomasz Napierala 
31b7a65e39SEdward Tomasz Napierala #include <sys/cdefs.h>
32b7a65e39SEdward Tomasz Napierala __FBSDID("$FreeBSD$");
33b7a65e39SEdward Tomasz Napierala 
34009ea47eSEdward Tomasz Napierala #include <sys/types.h>
35009ea47eSEdward Tomasz Napierala #include <sys/time.h>
36009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
37009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
38009ea47eSEdward Tomasz Napierala #include <netinet/in.h>
39073edb1cSAlexander Motin #include <arpa/inet.h>
40009ea47eSEdward Tomasz Napierala #include <assert.h>
41009ea47eSEdward Tomasz Napierala #include <ctype.h>
42009ea47eSEdward Tomasz Napierala #include <errno.h>
43009ea47eSEdward Tomasz Napierala #include <netdb.h>
44009ea47eSEdward Tomasz Napierala #include <signal.h>
45009ea47eSEdward Tomasz Napierala #include <stdbool.h>
46009ea47eSEdward Tomasz Napierala #include <stdio.h>
47009ea47eSEdward Tomasz Napierala #include <stdint.h>
48009ea47eSEdward Tomasz Napierala #include <stdlib.h>
49009ea47eSEdward Tomasz Napierala #include <string.h>
50009ea47eSEdward Tomasz Napierala #include <unistd.h>
51009ea47eSEdward Tomasz Napierala 
52009ea47eSEdward Tomasz Napierala #include "ctld.h"
53829603e2SAlexander Motin #include "isns.h"
54009ea47eSEdward Tomasz Napierala 
55ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false;
56ba3a2d31SEdward Tomasz Napierala 
57009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false;
58009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false;
59009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
60009ea47eSEdward Tomasz Napierala 
61009ea47eSEdward Tomasz Napierala static int nchildren = 0;
62db7bf2baSAlexander Motin static uint16_t last_portal_group_tag = 0xff;
63009ea47eSEdward Tomasz Napierala 
64009ea47eSEdward Tomasz Napierala static void
65009ea47eSEdward Tomasz Napierala usage(void)
66009ea47eSEdward Tomasz Napierala {
67009ea47eSEdward Tomasz Napierala 
68009ea47eSEdward Tomasz Napierala 	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
69009ea47eSEdward Tomasz Napierala 	exit(1);
70009ea47eSEdward Tomasz Napierala }
71009ea47eSEdward Tomasz Napierala 
72009ea47eSEdward Tomasz Napierala char *
73009ea47eSEdward Tomasz Napierala checked_strdup(const char *s)
74009ea47eSEdward Tomasz Napierala {
75009ea47eSEdward Tomasz Napierala 	char *c;
76009ea47eSEdward Tomasz Napierala 
77009ea47eSEdward Tomasz Napierala 	c = strdup(s);
78009ea47eSEdward Tomasz Napierala 	if (c == NULL)
79009ea47eSEdward Tomasz Napierala 		log_err(1, "strdup");
80009ea47eSEdward Tomasz Napierala 	return (c);
81009ea47eSEdward Tomasz Napierala }
82009ea47eSEdward Tomasz Napierala 
83009ea47eSEdward Tomasz Napierala struct conf *
84009ea47eSEdward Tomasz Napierala conf_new(void)
85009ea47eSEdward Tomasz Napierala {
86009ea47eSEdward Tomasz Napierala 	struct conf *conf;
87009ea47eSEdward Tomasz Napierala 
88009ea47eSEdward Tomasz Napierala 	conf = calloc(1, sizeof(*conf));
89009ea47eSEdward Tomasz Napierala 	if (conf == NULL)
90009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
91920c6cbaSAlexander Motin 	TAILQ_INIT(&conf->conf_luns);
92009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_targets);
93009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_auth_groups);
9492847ee1SAlexander Motin 	TAILQ_INIT(&conf->conf_ports);
95009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_portal_groups);
96057abcb0SAlexander Motin 	TAILQ_INIT(&conf->conf_pports);
97829603e2SAlexander Motin 	TAILQ_INIT(&conf->conf_isns);
98009ea47eSEdward Tomasz Napierala 
99829603e2SAlexander Motin 	conf->conf_isns_period = 900;
100829603e2SAlexander Motin 	conf->conf_isns_timeout = 5;
101009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
102009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
103009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
104009ea47eSEdward Tomasz Napierala 
105009ea47eSEdward Tomasz Napierala 	return (conf);
106009ea47eSEdward Tomasz Napierala }
107009ea47eSEdward Tomasz Napierala 
108009ea47eSEdward Tomasz Napierala void
109009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
110009ea47eSEdward Tomasz Napierala {
111920c6cbaSAlexander Motin 	struct lun *lun, *ltmp;
112009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
113009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
114009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
115057abcb0SAlexander Motin 	struct pport *pp, *pptmp;
116829603e2SAlexander Motin 	struct isns *is, *istmp;
117009ea47eSEdward Tomasz Napierala 
118009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
119009ea47eSEdward Tomasz Napierala 
120920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
121920c6cbaSAlexander Motin 		lun_delete(lun);
122009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
123009ea47eSEdward Tomasz Napierala 		target_delete(targ);
124009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
125009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
126009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
127009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
128057abcb0SAlexander Motin 	TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
129057abcb0SAlexander Motin 		pport_delete(pp);
130829603e2SAlexander Motin 	TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
131829603e2SAlexander Motin 		isns_delete(is);
13292847ee1SAlexander Motin 	assert(TAILQ_EMPTY(&conf->conf_ports));
133009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
134009ea47eSEdward Tomasz Napierala 	free(conf);
135009ea47eSEdward Tomasz Napierala }
136009ea47eSEdward Tomasz Napierala 
137009ea47eSEdward Tomasz Napierala static struct auth *
138009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
139009ea47eSEdward Tomasz Napierala {
140009ea47eSEdward Tomasz Napierala 	struct auth *auth;
141009ea47eSEdward Tomasz Napierala 
142009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
143009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
144009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
145009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
146009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
147009ea47eSEdward Tomasz Napierala 	return (auth);
148009ea47eSEdward Tomasz Napierala }
149009ea47eSEdward Tomasz Napierala 
150009ea47eSEdward Tomasz Napierala static void
151009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
152009ea47eSEdward Tomasz Napierala {
153009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
154009ea47eSEdward Tomasz Napierala 
155009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
156009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
157009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
158009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
159009ea47eSEdward Tomasz Napierala 	free(auth);
160009ea47eSEdward Tomasz Napierala }
161009ea47eSEdward Tomasz Napierala 
162009ea47eSEdward Tomasz Napierala const struct auth *
163d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user)
164009ea47eSEdward Tomasz Napierala {
165009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
166009ea47eSEdward Tomasz Napierala 
167009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
168009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
169009ea47eSEdward Tomasz Napierala 			return (auth);
170009ea47eSEdward Tomasz Napierala 	}
171009ea47eSEdward Tomasz Napierala 
172009ea47eSEdward Tomasz Napierala 	return (NULL);
173009ea47eSEdward Tomasz Napierala }
174009ea47eSEdward Tomasz Napierala 
1751658628dSEdward Tomasz Napierala static void
1761658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1771658628dSEdward Tomasz Napierala {
1781658628dSEdward Tomasz Napierala 	size_t len;
1791658628dSEdward Tomasz Napierala 
1801658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1811658628dSEdward Tomasz Napierala 	if (len > 16) {
1821658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1831658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1841658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1851658628dSEdward Tomasz Napierala 			    "long", auth->a_user, auth->a_auth_group->ag_name);
1861658628dSEdward Tomasz Napierala 		else
1871658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1881658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1891658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
190f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1911658628dSEdward Tomasz Napierala 	}
1921658628dSEdward Tomasz Napierala 	if (len < 12) {
1931658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1941658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1951658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
1961658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
1971658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
1981658628dSEdward Tomasz Napierala 		else
1991658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
2001658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 16 characters "
2011658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
202f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
2031658628dSEdward Tomasz Napierala 	}
2041658628dSEdward Tomasz Napierala 
2051658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
2067c924aaeSJosh Paetzel 		len = strlen(auth->a_mutual_secret);
2071658628dSEdward Tomasz Napierala 		if (len > 16) {
2081658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2091658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2101658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
2111658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2121658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2131658628dSEdward Tomasz Napierala 			else
2141658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2151658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
2161658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2171658628dSEdward Tomasz Napierala 				    auth->a_user,
218f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2191658628dSEdward Tomasz Napierala 		}
2201658628dSEdward Tomasz Napierala 		if (len < 12) {
2211658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2221658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2231658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2241658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2251658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2261658628dSEdward Tomasz Napierala 			else
2271658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2281658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2291658628dSEdward Tomasz Napierala 				    "at least 16 characters long",
2301658628dSEdward Tomasz Napierala 				    auth->a_user,
231f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2321658628dSEdward Tomasz Napierala 		}
2331658628dSEdward Tomasz Napierala 	}
2341658628dSEdward Tomasz Napierala }
2351658628dSEdward Tomasz Napierala 
2361658628dSEdward Tomasz Napierala const struct auth *
2371658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2381658628dSEdward Tomasz Napierala     const char *secret)
2391658628dSEdward Tomasz Napierala {
2401658628dSEdward Tomasz Napierala 	struct auth *auth;
2411658628dSEdward Tomasz Napierala 
2421658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2431658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2441658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2451658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2461658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2471658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2481658628dSEdward Tomasz Napierala 		else
2491658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2501658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
251f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2521658628dSEdward Tomasz Napierala 		return (NULL);
2531658628dSEdward Tomasz Napierala 	}
2541658628dSEdward Tomasz Napierala 
2551658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2561658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2571658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2581658628dSEdward Tomasz Napierala 
2591658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2601658628dSEdward Tomasz Napierala 
2611658628dSEdward Tomasz Napierala 	return (auth);
2621658628dSEdward Tomasz Napierala }
2631658628dSEdward Tomasz Napierala 
2641658628dSEdward Tomasz Napierala const struct auth *
2651658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2661658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2671658628dSEdward Tomasz Napierala {
2681658628dSEdward Tomasz Napierala 	struct auth *auth;
2691658628dSEdward Tomasz Napierala 
2701658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2711658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2721658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2731658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2741658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2751658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2761658628dSEdward Tomasz Napierala 			    ag->ag_name);
2771658628dSEdward Tomasz Napierala 		else
2781658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2791658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
280f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2811658628dSEdward Tomasz Napierala 		return (NULL);
2821658628dSEdward Tomasz Napierala 	}
2831658628dSEdward Tomasz Napierala 
2841658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2851658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2861658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2871658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2881658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2891658628dSEdward Tomasz Napierala 
2901658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2911658628dSEdward Tomasz Napierala 
2921658628dSEdward Tomasz Napierala 	return (auth);
2931658628dSEdward Tomasz Napierala }
2941658628dSEdward Tomasz Napierala 
2958cb2e958SEdward Tomasz Napierala const struct auth_name *
2968cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
2978cb2e958SEdward Tomasz Napierala {
2988cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
2998cb2e958SEdward Tomasz Napierala 
3008cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
3018cb2e958SEdward Tomasz Napierala 	if (an == NULL)
3028cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3038cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
3048cb2e958SEdward Tomasz Napierala 	an->an_initator_name = checked_strdup(name);
3058cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
3068cb2e958SEdward Tomasz Napierala 	return (an);
3078cb2e958SEdward Tomasz Napierala }
3088cb2e958SEdward Tomasz Napierala 
3098cb2e958SEdward Tomasz Napierala static void
3108cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
3118cb2e958SEdward Tomasz Napierala {
3128cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
3138cb2e958SEdward Tomasz Napierala 
3148cb2e958SEdward Tomasz Napierala 	free(an->an_initator_name);
3158cb2e958SEdward Tomasz Napierala 	free(an);
3168cb2e958SEdward Tomasz Napierala }
3178cb2e958SEdward Tomasz Napierala 
3188cb2e958SEdward Tomasz Napierala bool
3198cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
3208cb2e958SEdward Tomasz Napierala {
3218cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
3228cb2e958SEdward Tomasz Napierala 		return (false);
3238cb2e958SEdward Tomasz Napierala 	return (true);
3248cb2e958SEdward Tomasz Napierala }
3258cb2e958SEdward Tomasz Napierala 
3268cb2e958SEdward Tomasz Napierala const struct auth_name *
3278cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3288cb2e958SEdward Tomasz Napierala {
3298cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3308cb2e958SEdward Tomasz Napierala 
3318cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
3328cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_name->an_initator_name, name) == 0)
3338cb2e958SEdward Tomasz Napierala 			return (auth_name);
3348cb2e958SEdward Tomasz Napierala 	}
3358cb2e958SEdward Tomasz Napierala 
3368cb2e958SEdward Tomasz Napierala 	return (NULL);
3378cb2e958SEdward Tomasz Napierala }
3388cb2e958SEdward Tomasz Napierala 
339e867e162SEdward Tomasz Napierala int
340e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name)
341e867e162SEdward Tomasz Napierala {
342e867e162SEdward Tomasz Napierala 	if (!auth_name_defined(ag))
343e867e162SEdward Tomasz Napierala 		return (0);
344e867e162SEdward Tomasz Napierala 
345e867e162SEdward Tomasz Napierala 	if (auth_name_find(ag, initiator_name) == NULL)
346e867e162SEdward Tomasz Napierala 		return (1);
347e867e162SEdward Tomasz Napierala 
348e867e162SEdward Tomasz Napierala 	return (0);
349e867e162SEdward Tomasz Napierala }
350e867e162SEdward Tomasz Napierala 
3518cb2e958SEdward Tomasz Napierala const struct auth_portal *
3528cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3538cb2e958SEdward Tomasz Napierala {
3548cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
355073edb1cSAlexander Motin 	char *net, *mask, *str, *tmp;
356073edb1cSAlexander Motin 	int len, dm, m;
3578cb2e958SEdward Tomasz Napierala 
3588cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3598cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3608cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3618cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
3628cb2e958SEdward Tomasz Napierala 	ap->ap_initator_portal = checked_strdup(portal);
363073edb1cSAlexander Motin 	mask = str = checked_strdup(portal);
364073edb1cSAlexander Motin 	net = strsep(&mask, "/");
365073edb1cSAlexander Motin 	if (net[0] == '[')
366073edb1cSAlexander Motin 		net++;
367073edb1cSAlexander Motin 	len = strlen(net);
368073edb1cSAlexander Motin 	if (len == 0)
369073edb1cSAlexander Motin 		goto error;
370073edb1cSAlexander Motin 	if (net[len - 1] == ']')
371073edb1cSAlexander Motin 		net[len - 1] = 0;
372073edb1cSAlexander Motin 	if (strchr(net, ':') != NULL) {
373073edb1cSAlexander Motin 		struct sockaddr_in6 *sin6 =
374073edb1cSAlexander Motin 		    (struct sockaddr_in6 *)&ap->ap_sa;
375073edb1cSAlexander Motin 
376073edb1cSAlexander Motin 		sin6->sin6_len = sizeof(*sin6);
377073edb1cSAlexander Motin 		sin6->sin6_family = AF_INET6;
378073edb1cSAlexander Motin 		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
379073edb1cSAlexander Motin 			goto error;
380073edb1cSAlexander Motin 		dm = 128;
381073edb1cSAlexander Motin 	} else {
382073edb1cSAlexander Motin 		struct sockaddr_in *sin =
383073edb1cSAlexander Motin 		    (struct sockaddr_in *)&ap->ap_sa;
384073edb1cSAlexander Motin 
385073edb1cSAlexander Motin 		sin->sin_len = sizeof(*sin);
386073edb1cSAlexander Motin 		sin->sin_family = AF_INET;
387073edb1cSAlexander Motin 		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
388073edb1cSAlexander Motin 			goto error;
389073edb1cSAlexander Motin 		dm = 32;
390073edb1cSAlexander Motin 	}
391073edb1cSAlexander Motin 	if (mask != NULL) {
392073edb1cSAlexander Motin 		m = strtol(mask, &tmp, 0);
393073edb1cSAlexander Motin 		if (m < 0 || m > dm || tmp[0] != 0)
394073edb1cSAlexander Motin 			goto error;
395073edb1cSAlexander Motin 	} else
396073edb1cSAlexander Motin 		m = dm;
397073edb1cSAlexander Motin 	ap->ap_mask = m;
398073edb1cSAlexander Motin 	free(str);
3998cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
4008cb2e958SEdward Tomasz Napierala 	return (ap);
401073edb1cSAlexander Motin 
402073edb1cSAlexander Motin error:
403678b6879SAlexander Motin 	free(ap);
404073edb1cSAlexander Motin 	log_errx(1, "Incorrect initiator portal '%s'", portal);
405073edb1cSAlexander Motin 	return (NULL);
4068cb2e958SEdward Tomasz Napierala }
4078cb2e958SEdward Tomasz Napierala 
4088cb2e958SEdward Tomasz Napierala static void
4098cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
4108cb2e958SEdward Tomasz Napierala {
4118cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
4128cb2e958SEdward Tomasz Napierala 
4138cb2e958SEdward Tomasz Napierala 	free(ap->ap_initator_portal);
4148cb2e958SEdward Tomasz Napierala 	free(ap);
4158cb2e958SEdward Tomasz Napierala }
4168cb2e958SEdward Tomasz Napierala 
4178cb2e958SEdward Tomasz Napierala bool
4188cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
4198cb2e958SEdward Tomasz Napierala {
4208cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
4218cb2e958SEdward Tomasz Napierala 		return (false);
4228cb2e958SEdward Tomasz Napierala 	return (true);
4238cb2e958SEdward Tomasz Napierala }
4248cb2e958SEdward Tomasz Napierala 
4258cb2e958SEdward Tomasz Napierala const struct auth_portal *
426073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
4278cb2e958SEdward Tomasz Napierala {
428073edb1cSAlexander Motin 	const struct auth_portal *ap;
4295e46a066SAlexander Motin 	const uint8_t *a, *b;
430073edb1cSAlexander Motin 	int i;
4315e46a066SAlexander Motin 	uint8_t bmask;
4328cb2e958SEdward Tomasz Napierala 
433073edb1cSAlexander Motin 	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
434073edb1cSAlexander Motin 		if (ap->ap_sa.ss_family != ss->ss_family)
435073edb1cSAlexander Motin 			continue;
436073edb1cSAlexander Motin 		if (ss->ss_family == AF_INET) {
4375e46a066SAlexander Motin 			a = (const uint8_t *)
4385e46a066SAlexander Motin 			    &((const struct sockaddr_in *)ss)->sin_addr;
4395e46a066SAlexander Motin 			b = (const uint8_t *)
4405e46a066SAlexander Motin 			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
441073edb1cSAlexander Motin 		} else {
4425e46a066SAlexander Motin 			a = (const uint8_t *)
4435e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
4445e46a066SAlexander Motin 			b = (const uint8_t *)
4455e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
446073edb1cSAlexander Motin 		}
447073edb1cSAlexander Motin 		for (i = 0; i < ap->ap_mask / 8; i++) {
448073edb1cSAlexander Motin 			if (a[i] != b[i])
449073edb1cSAlexander Motin 				goto next;
450073edb1cSAlexander Motin 		}
451073edb1cSAlexander Motin 		if (ap->ap_mask % 8) {
452073edb1cSAlexander Motin 			bmask = 0xff << (8 - (ap->ap_mask % 8));
453073edb1cSAlexander Motin 			if ((a[i] & bmask) != (b[i] & bmask))
454073edb1cSAlexander Motin 				goto next;
455073edb1cSAlexander Motin 		}
456073edb1cSAlexander Motin 		return (ap);
457073edb1cSAlexander Motin next:
458073edb1cSAlexander Motin 		;
4598cb2e958SEdward Tomasz Napierala 	}
4608cb2e958SEdward Tomasz Napierala 
4618cb2e958SEdward Tomasz Napierala 	return (NULL);
4628cb2e958SEdward Tomasz Napierala }
4638cb2e958SEdward Tomasz Napierala 
464e867e162SEdward Tomasz Napierala int
465e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
466e867e162SEdward Tomasz Napierala {
467e867e162SEdward Tomasz Napierala 
468e867e162SEdward Tomasz Napierala 	if (!auth_portal_defined(ag))
469e867e162SEdward Tomasz Napierala 		return (0);
470e867e162SEdward Tomasz Napierala 
471e867e162SEdward Tomasz Napierala 	if (auth_portal_find(ag, sa) == NULL)
472e867e162SEdward Tomasz Napierala 		return (1);
473e867e162SEdward Tomasz Napierala 
474e867e162SEdward Tomasz Napierala 	return (0);
475e867e162SEdward Tomasz Napierala }
476e867e162SEdward Tomasz Napierala 
477009ea47eSEdward Tomasz Napierala struct auth_group *
478009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
479009ea47eSEdward Tomasz Napierala {
480009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
481009ea47eSEdward Tomasz Napierala 
482009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
483009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
484009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
485009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
486009ea47eSEdward Tomasz Napierala 			return (NULL);
487009ea47eSEdward Tomasz Napierala 		}
488009ea47eSEdward Tomasz Napierala 	}
489009ea47eSEdward Tomasz Napierala 
490009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
491009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
492009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
493009ea47eSEdward Tomasz Napierala 	if (name != NULL)
494009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
495009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
4968cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
4978cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
498009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
499009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
500009ea47eSEdward Tomasz Napierala 
501009ea47eSEdward Tomasz Napierala 	return (ag);
502009ea47eSEdward Tomasz Napierala }
503009ea47eSEdward Tomasz Napierala 
504009ea47eSEdward Tomasz Napierala void
505009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
506009ea47eSEdward Tomasz Napierala {
5078cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
5088cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
5098cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
510009ea47eSEdward Tomasz Napierala 
511009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
512009ea47eSEdward Tomasz Napierala 
5138cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
514009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
5158cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
5168cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
5178cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
5188cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
5198cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
520009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
521009ea47eSEdward Tomasz Napierala 	free(ag);
522009ea47eSEdward Tomasz Napierala }
523009ea47eSEdward Tomasz Napierala 
524009ea47eSEdward Tomasz Napierala struct auth_group *
525d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name)
526009ea47eSEdward Tomasz Napierala {
527009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
528009ea47eSEdward Tomasz Napierala 
529009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
530009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
531009ea47eSEdward Tomasz Napierala 			return (ag);
532009ea47eSEdward Tomasz Napierala 	}
533009ea47eSEdward Tomasz Napierala 
534009ea47eSEdward Tomasz Napierala 	return (NULL);
535009ea47eSEdward Tomasz Napierala }
536009ea47eSEdward Tomasz Napierala 
537df9900fbSEdward Tomasz Napierala int
538bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str)
539df9900fbSEdward Tomasz Napierala {
540bd833b11SEdward Tomasz Napierala 	int type;
541df9900fbSEdward Tomasz Napierala 
542df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
543df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
544e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
545e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
546df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
547df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
548df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
549df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
550df9900fbSEdward Tomasz Napierala 	} else {
551df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
552df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
553df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
554df9900fbSEdward Tomasz Napierala 		else
555df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
556df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
557df9900fbSEdward Tomasz Napierala 		return (1);
558df9900fbSEdward Tomasz Napierala 	}
559df9900fbSEdward Tomasz Napierala 
560bd833b11SEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
561bd833b11SEdward Tomasz Napierala 		if (ag->ag_name != NULL) {
562df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
563df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
564df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
565bd833b11SEdward Tomasz Napierala 		} else {
566df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
567df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
568df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
569bd833b11SEdward Tomasz Napierala 		}
570df9900fbSEdward Tomasz Napierala 		return (1);
571df9900fbSEdward Tomasz Napierala 	}
572df9900fbSEdward Tomasz Napierala 
573bd833b11SEdward Tomasz Napierala 	ag->ag_type = type;
574bd833b11SEdward Tomasz Napierala 
575bd833b11SEdward Tomasz Napierala 	return (0);
576df9900fbSEdward Tomasz Napierala }
577df9900fbSEdward Tomasz Napierala 
578009ea47eSEdward Tomasz Napierala static struct portal *
579009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
580009ea47eSEdward Tomasz Napierala {
581009ea47eSEdward Tomasz Napierala 	struct portal *portal;
582009ea47eSEdward Tomasz Napierala 
583009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
584009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
585009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
586009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
587009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
588009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
589009ea47eSEdward Tomasz Napierala 	return (portal);
590009ea47eSEdward Tomasz Napierala }
591009ea47eSEdward Tomasz Napierala 
592009ea47eSEdward Tomasz Napierala static void
593009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
594009ea47eSEdward Tomasz Napierala {
59544a5953aSEdward Tomasz Napierala 
596009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
59744a5953aSEdward Tomasz Napierala 	if (portal->p_ai != NULL)
598009ea47eSEdward Tomasz Napierala 		freeaddrinfo(portal->p_ai);
599009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
600009ea47eSEdward Tomasz Napierala 	free(portal);
601009ea47eSEdward Tomasz Napierala }
602009ea47eSEdward Tomasz Napierala 
603009ea47eSEdward Tomasz Napierala struct portal_group *
604009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
605009ea47eSEdward Tomasz Napierala {
606009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
607009ea47eSEdward Tomasz Napierala 
608009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
609009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
610009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
611009ea47eSEdward Tomasz Napierala 		return (NULL);
612009ea47eSEdward Tomasz Napierala 	}
613009ea47eSEdward Tomasz Napierala 
614009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
615009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
616009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
617009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
618*398290f2SAlexander Motin 	TAILQ_INIT(&pg->pg_options);
619009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
62092847ee1SAlexander Motin 	TAILQ_INIT(&pg->pg_ports);
621009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
6226b31e130SAlexander Motin 	pg->pg_tag = 0;		/* Assigned later in conf_apply(). */
623009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
624009ea47eSEdward Tomasz Napierala 
625009ea47eSEdward Tomasz Napierala 	return (pg);
626009ea47eSEdward Tomasz Napierala }
627009ea47eSEdward Tomasz Napierala 
628009ea47eSEdward Tomasz Napierala void
629009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
630009ea47eSEdward Tomasz Napierala {
631009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
63292847ee1SAlexander Motin 	struct port *port, *tport;
633*398290f2SAlexander Motin 	struct option *o, *otmp;
634009ea47eSEdward Tomasz Napierala 
63592847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
63692847ee1SAlexander Motin 		port_delete(port);
637009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
638009ea47eSEdward Tomasz Napierala 
639009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
640009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
641*398290f2SAlexander Motin 	TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
642*398290f2SAlexander Motin 		option_delete(&pg->pg_options, o);
643009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
644a9d78210SEdward Tomasz Napierala 	free(pg->pg_offload);
6456dbdbf82SEdward Tomasz Napierala 	free(pg->pg_redirection);
646009ea47eSEdward Tomasz Napierala 	free(pg);
647009ea47eSEdward Tomasz Napierala }
648009ea47eSEdward Tomasz Napierala 
649009ea47eSEdward Tomasz Napierala struct portal_group *
650d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name)
651009ea47eSEdward Tomasz Napierala {
652009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
653009ea47eSEdward Tomasz Napierala 
654009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
655009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
656009ea47eSEdward Tomasz Napierala 			return (pg);
657009ea47eSEdward Tomasz Napierala 	}
658009ea47eSEdward Tomasz Napierala 
659009ea47eSEdward Tomasz Napierala 	return (NULL);
660009ea47eSEdward Tomasz Napierala }
661009ea47eSEdward Tomasz Napierala 
662829603e2SAlexander Motin static int
663829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
664009ea47eSEdward Tomasz Napierala {
665009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
6661606ad8cSAlexander Motin 	char *str, *addr, *ch;
667009ea47eSEdward Tomasz Napierala 	const char *port;
668009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
669009ea47eSEdward Tomasz Napierala 
6701606ad8cSAlexander Motin 	str = arg = strdup(arg);
671009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
672009ea47eSEdward Tomasz Napierala 		/*
673009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
674009ea47eSEdward Tomasz Napierala 		 */
675009ea47eSEdward Tomasz Napierala 		arg++;
676009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
677829603e2SAlexander Motin 		if (arg == NULL)
678009ea47eSEdward Tomasz Napierala 			return (1);
679009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
680829603e2SAlexander Motin 			port = def_port;
681009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
682009ea47eSEdward Tomasz Napierala 			port = arg + 1;
6831606ad8cSAlexander Motin 		} else {
6841606ad8cSAlexander Motin 			free(str);
685009ea47eSEdward Tomasz Napierala 			return (1);
6861606ad8cSAlexander Motin 		}
687009ea47eSEdward Tomasz Napierala 	} else {
688009ea47eSEdward Tomasz Napierala 		/*
689009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
690009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
691009ea47eSEdward Tomasz Napierala 		 */
692009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
693009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
694009ea47eSEdward Tomasz Napierala 				colons++;
695009ea47eSEdward Tomasz Napierala 		}
696009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
697009ea47eSEdward Tomasz Napierala 			addr = arg;
698829603e2SAlexander Motin 			port = def_port;
699009ea47eSEdward Tomasz Napierala 		} else {
700009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
701009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
702829603e2SAlexander Motin 				port = def_port;
703009ea47eSEdward Tomasz Napierala 			else
704009ea47eSEdward Tomasz Napierala 				port = arg;
705009ea47eSEdward Tomasz Napierala 		}
706009ea47eSEdward Tomasz Napierala 	}
707009ea47eSEdward Tomasz Napierala 
708009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
709009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
710009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
711009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
712829603e2SAlexander Motin 	error = getaddrinfo(addr, port, &hints, ai);
7131606ad8cSAlexander Motin 	free(str);
7141606ad8cSAlexander Motin 	return ((error != 0) ? 1 : 0);
715829603e2SAlexander Motin }
716009ea47eSEdward Tomasz Napierala 
717829603e2SAlexander Motin int
718829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
719829603e2SAlexander Motin {
720829603e2SAlexander Motin 	struct portal *portal;
721829603e2SAlexander Motin 
722829603e2SAlexander Motin 	portal = portal_new(pg);
723829603e2SAlexander Motin 	portal->p_listen = checked_strdup(value);
724829603e2SAlexander Motin 	portal->p_iser = iser;
725829603e2SAlexander Motin 
726829603e2SAlexander Motin 	if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
727829603e2SAlexander Motin 		log_warnx("invalid listen address %s", portal->p_listen);
72844a5953aSEdward Tomasz Napierala 		portal_delete(portal);
729009ea47eSEdward Tomasz Napierala 		return (1);
730009ea47eSEdward Tomasz Napierala 	}
731009ea47eSEdward Tomasz Napierala 
732009ea47eSEdward Tomasz Napierala 	/*
733009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
734009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
735009ea47eSEdward Tomasz Napierala 	 */
736009ea47eSEdward Tomasz Napierala 
737009ea47eSEdward Tomasz Napierala 	return (0);
738009ea47eSEdward Tomasz Napierala }
739009ea47eSEdward Tomasz Napierala 
740829603e2SAlexander Motin int
741829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr)
742829603e2SAlexander Motin {
743829603e2SAlexander Motin 	struct isns *isns;
744829603e2SAlexander Motin 
745829603e2SAlexander Motin 	isns = calloc(1, sizeof(*isns));
746829603e2SAlexander Motin 	if (isns == NULL)
747829603e2SAlexander Motin 		log_err(1, "calloc");
748829603e2SAlexander Motin 	isns->i_conf = conf;
749829603e2SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
750829603e2SAlexander Motin 	isns->i_addr = checked_strdup(addr);
751829603e2SAlexander Motin 
752829603e2SAlexander Motin 	if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
753829603e2SAlexander Motin 		log_warnx("invalid iSNS address %s", isns->i_addr);
754829603e2SAlexander Motin 		isns_delete(isns);
755829603e2SAlexander Motin 		return (1);
756829603e2SAlexander Motin 	}
757829603e2SAlexander Motin 
758829603e2SAlexander Motin 	/*
759829603e2SAlexander Motin 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
760829603e2SAlexander Motin 	 *	those into multiple servers.
761829603e2SAlexander Motin 	 */
762829603e2SAlexander Motin 
763829603e2SAlexander Motin 	return (0);
764829603e2SAlexander Motin }
765829603e2SAlexander Motin 
766829603e2SAlexander Motin void
767829603e2SAlexander Motin isns_delete(struct isns *isns)
768829603e2SAlexander Motin {
769829603e2SAlexander Motin 
770829603e2SAlexander Motin 	TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
771829603e2SAlexander Motin 	free(isns->i_addr);
772829603e2SAlexander Motin 	if (isns->i_ai != NULL)
773829603e2SAlexander Motin 		freeaddrinfo(isns->i_ai);
774829603e2SAlexander Motin 	free(isns);
775829603e2SAlexander Motin }
776829603e2SAlexander Motin 
777829603e2SAlexander Motin static int
778829603e2SAlexander Motin isns_do_connect(struct isns *isns)
779829603e2SAlexander Motin {
780829603e2SAlexander Motin 	int s;
781829603e2SAlexander Motin 
782829603e2SAlexander Motin 	s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
783829603e2SAlexander Motin 	    isns->i_ai->ai_protocol);
784829603e2SAlexander Motin 	if (s < 0) {
785829603e2SAlexander Motin 		log_warn("socket(2) failed for %s", isns->i_addr);
786829603e2SAlexander Motin 		return (-1);
787829603e2SAlexander Motin 	}
788829603e2SAlexander Motin 	if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
789829603e2SAlexander Motin 		log_warn("connect(2) failed for %s", isns->i_addr);
790829603e2SAlexander Motin 		close(s);
791829603e2SAlexander Motin 		return (-1);
792829603e2SAlexander Motin 	}
793829603e2SAlexander Motin 	return(s);
794829603e2SAlexander Motin }
795829603e2SAlexander Motin 
796829603e2SAlexander Motin static int
797829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname)
798829603e2SAlexander Motin {
799829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
800829603e2SAlexander Motin 	struct target *target;
801829603e2SAlexander Motin 	struct portal *portal;
802829603e2SAlexander Motin 	struct portal_group *pg;
80392847ee1SAlexander Motin 	struct port *port;
804829603e2SAlexander Motin 	struct isns_req *req;
805829603e2SAlexander Motin 	int res = 0;
806829603e2SAlexander Motin 	uint32_t error;
807829603e2SAlexander Motin 
808829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
809829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
810829603e2SAlexander Motin 	isns_req_add_delim(req);
811829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
812829603e2SAlexander Motin 	isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
813829603e2SAlexander Motin 	isns_req_add_32(req, 6, conf->conf_isns_period);
814829603e2SAlexander Motin 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
815829603e2SAlexander Motin 		if (pg->pg_unassigned)
816829603e2SAlexander Motin 			continue;
817829603e2SAlexander Motin 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
818829603e2SAlexander Motin 			isns_req_add_addr(req, 16, portal->p_ai);
819829603e2SAlexander Motin 			isns_req_add_port(req, 17, portal->p_ai);
820829603e2SAlexander Motin 		}
821829603e2SAlexander Motin 	}
822829603e2SAlexander Motin 	TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
823829603e2SAlexander Motin 		isns_req_add_str(req, 32, target->t_name);
824829603e2SAlexander Motin 		isns_req_add_32(req, 33, 1); /* 1 -- Target*/
825829603e2SAlexander Motin 		if (target->t_alias != NULL)
826829603e2SAlexander Motin 			isns_req_add_str(req, 34, target->t_alias);
82792847ee1SAlexander Motin 		TAILQ_FOREACH(port, &target->t_ports, p_ts) {
82892847ee1SAlexander Motin 			if ((pg = port->p_portal_group) == NULL)
82992847ee1SAlexander Motin 				continue;
830829603e2SAlexander Motin 			isns_req_add_32(req, 51, pg->pg_tag);
831829603e2SAlexander Motin 			TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
832829603e2SAlexander Motin 				isns_req_add_addr(req, 49, portal->p_ai);
833829603e2SAlexander Motin 				isns_req_add_port(req, 50, portal->p_ai);
834829603e2SAlexander Motin 			}
835829603e2SAlexander Motin 		}
83692847ee1SAlexander Motin 	}
837829603e2SAlexander Motin 	res = isns_req_send(s, req);
838829603e2SAlexander Motin 	if (res < 0) {
839829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
840829603e2SAlexander Motin 		goto quit;
841829603e2SAlexander Motin 	}
842829603e2SAlexander Motin 	res = isns_req_receive(s, req);
843829603e2SAlexander Motin 	if (res < 0) {
844829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
845829603e2SAlexander Motin 		goto quit;
846829603e2SAlexander Motin 	}
847829603e2SAlexander Motin 	error = isns_req_get_status(req);
848829603e2SAlexander Motin 	if (error != 0) {
849829603e2SAlexander Motin 		log_warnx("iSNS register error %d for %s", error, isns->i_addr);
850829603e2SAlexander Motin 		res = -1;
851829603e2SAlexander Motin 	}
852829603e2SAlexander Motin quit:
853829603e2SAlexander Motin 	isns_req_free(req);
854829603e2SAlexander Motin 	return (res);
855829603e2SAlexander Motin }
856829603e2SAlexander Motin 
857829603e2SAlexander Motin static int
858829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname)
859829603e2SAlexander Motin {
860829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
861829603e2SAlexander Motin 	struct isns_req *req;
862829603e2SAlexander Motin 	int res = 0;
863829603e2SAlexander Motin 	uint32_t error;
864829603e2SAlexander Motin 
865829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
866829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
867829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
868829603e2SAlexander Motin 	isns_req_add_delim(req);
869829603e2SAlexander Motin 	isns_req_add(req, 2, 0, NULL);
870829603e2SAlexander Motin 	res = isns_req_send(s, req);
871829603e2SAlexander Motin 	if (res < 0) {
872829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
873829603e2SAlexander Motin 		goto quit;
874829603e2SAlexander Motin 	}
875829603e2SAlexander Motin 	res = isns_req_receive(s, req);
876829603e2SAlexander Motin 	if (res < 0) {
877829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
878829603e2SAlexander Motin 		goto quit;
879829603e2SAlexander Motin 	}
880829603e2SAlexander Motin 	error = isns_req_get_status(req);
881829603e2SAlexander Motin 	if (error != 0) {
882829603e2SAlexander Motin 		log_warnx("iSNS check error %d for %s", error, isns->i_addr);
883829603e2SAlexander Motin 		res = -1;
884829603e2SAlexander Motin 	}
885829603e2SAlexander Motin quit:
886829603e2SAlexander Motin 	isns_req_free(req);
887829603e2SAlexander Motin 	return (res);
888829603e2SAlexander Motin }
889829603e2SAlexander Motin 
890829603e2SAlexander Motin static int
891829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname)
892829603e2SAlexander Motin {
893829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
894829603e2SAlexander Motin 	struct isns_req *req;
895829603e2SAlexander Motin 	int res = 0;
896829603e2SAlexander Motin 	uint32_t error;
897829603e2SAlexander Motin 
898829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
899829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
900829603e2SAlexander Motin 	isns_req_add_delim(req);
901829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
902829603e2SAlexander Motin 	res = isns_req_send(s, req);
903829603e2SAlexander Motin 	if (res < 0) {
904829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
905829603e2SAlexander Motin 		goto quit;
906829603e2SAlexander Motin 	}
907829603e2SAlexander Motin 	res = isns_req_receive(s, req);
908829603e2SAlexander Motin 	if (res < 0) {
909829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
910829603e2SAlexander Motin 		goto quit;
911829603e2SAlexander Motin 	}
912829603e2SAlexander Motin 	error = isns_req_get_status(req);
913829603e2SAlexander Motin 	if (error != 0) {
914829603e2SAlexander Motin 		log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
915829603e2SAlexander Motin 		res = -1;
916829603e2SAlexander Motin 	}
917829603e2SAlexander Motin quit:
918829603e2SAlexander Motin 	isns_req_free(req);
919829603e2SAlexander Motin 	return (res);
920829603e2SAlexander Motin }
921829603e2SAlexander Motin 
922829603e2SAlexander Motin void
923829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns)
924829603e2SAlexander Motin {
925829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
926d5cc4a4aSAlexander Motin 	int s;
927829603e2SAlexander Motin 	char hostname[256];
928829603e2SAlexander Motin 
929829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
930829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
931829603e2SAlexander Motin 		return;
932829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
933829603e2SAlexander Motin 	s = isns_do_connect(isns);
934829603e2SAlexander Motin 	if (s < 0) {
935829603e2SAlexander Motin 		set_timeout(0, false);
936829603e2SAlexander Motin 		return;
937829603e2SAlexander Motin 	}
938829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
939829603e2SAlexander Motin 
940829603e2SAlexander Motin 	if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
941829603e2SAlexander Motin 		oldisns = isns;
942d5cc4a4aSAlexander Motin 	isns_do_deregister(oldisns, s, hostname);
943d5cc4a4aSAlexander Motin 	isns_do_register(isns, s, hostname);
944829603e2SAlexander Motin 	close(s);
945829603e2SAlexander Motin 	set_timeout(0, false);
946829603e2SAlexander Motin }
947829603e2SAlexander Motin 
948829603e2SAlexander Motin void
949829603e2SAlexander Motin isns_check(struct isns *isns)
950829603e2SAlexander Motin {
951829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
952829603e2SAlexander Motin 	int s, res;
953829603e2SAlexander Motin 	char hostname[256];
954829603e2SAlexander Motin 
955829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
956829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
957829603e2SAlexander Motin 		return;
958829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
959829603e2SAlexander Motin 	s = isns_do_connect(isns);
960829603e2SAlexander Motin 	if (s < 0) {
961829603e2SAlexander Motin 		set_timeout(0, false);
962829603e2SAlexander Motin 		return;
963829603e2SAlexander Motin 	}
964829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
965829603e2SAlexander Motin 
966829603e2SAlexander Motin 	res = isns_do_check(isns, s, hostname);
967829603e2SAlexander Motin 	if (res < 0) {
968d5cc4a4aSAlexander Motin 		isns_do_deregister(isns, s, hostname);
969d5cc4a4aSAlexander Motin 		isns_do_register(isns, s, hostname);
970829603e2SAlexander Motin 	}
971829603e2SAlexander Motin 	close(s);
972829603e2SAlexander Motin 	set_timeout(0, false);
973829603e2SAlexander Motin }
974829603e2SAlexander Motin 
975829603e2SAlexander Motin void
976829603e2SAlexander Motin isns_deregister(struct isns *isns)
977829603e2SAlexander Motin {
978829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
979d5cc4a4aSAlexander Motin 	int s;
980829603e2SAlexander Motin 	char hostname[256];
981829603e2SAlexander Motin 
982829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
983829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
984829603e2SAlexander Motin 		return;
985829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
986829603e2SAlexander Motin 	s = isns_do_connect(isns);
987829603e2SAlexander Motin 	if (s < 0)
988829603e2SAlexander Motin 		return;
989829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
990829603e2SAlexander Motin 
991d5cc4a4aSAlexander Motin 	isns_do_deregister(isns, s, hostname);
992829603e2SAlexander Motin 	close(s);
993829603e2SAlexander Motin 	set_timeout(0, false);
994829603e2SAlexander Motin }
995829603e2SAlexander Motin 
99605374883SEdward Tomasz Napierala int
997bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str)
99805374883SEdward Tomasz Napierala {
999bd833b11SEdward Tomasz Napierala 	int filter;
100005374883SEdward Tomasz Napierala 
100105374883SEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
100205374883SEdward Tomasz Napierala 		filter = PG_FILTER_NONE;
100305374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal") == 0) {
100405374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL;
100505374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name") == 0) {
100605374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME;
100705374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name-auth") == 0) {
100805374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME_AUTH;
100905374883SEdward Tomasz Napierala 	} else {
101005374883SEdward Tomasz Napierala 		log_warnx("invalid discovery-filter \"%s\" for portal-group "
101105374883SEdward Tomasz Napierala 		    "\"%s\"; valid values are \"none\", \"portal\", "
101205374883SEdward Tomasz Napierala 		    "\"portal-name\", and \"portal-name-auth\"",
101305374883SEdward Tomasz Napierala 		    str, pg->pg_name);
101405374883SEdward Tomasz Napierala 		return (1);
101505374883SEdward Tomasz Napierala 	}
101605374883SEdward Tomasz Napierala 
1017bd833b11SEdward Tomasz Napierala 	if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1018bd833b11SEdward Tomasz Napierala 	    pg->pg_discovery_filter != filter) {
101905374883SEdward Tomasz Napierala 		log_warnx("cannot set discovery-filter to \"%s\" for "
102005374883SEdward Tomasz Napierala 		    "portal-group \"%s\"; already has a different "
102105374883SEdward Tomasz Napierala 		    "value", str, pg->pg_name);
102205374883SEdward Tomasz Napierala 		return (1);
102305374883SEdward Tomasz Napierala 	}
102405374883SEdward Tomasz Napierala 
1025bd833b11SEdward Tomasz Napierala 	pg->pg_discovery_filter = filter;
1026bd833b11SEdward Tomasz Napierala 
1027bd833b11SEdward Tomasz Napierala 	return (0);
102805374883SEdward Tomasz Napierala }
102905374883SEdward Tomasz Napierala 
10306dbdbf82SEdward Tomasz Napierala int
1031a9d78210SEdward Tomasz Napierala portal_group_set_offload(struct portal_group *pg, const char *offload)
1032a9d78210SEdward Tomasz Napierala {
1033a9d78210SEdward Tomasz Napierala 
1034a9d78210SEdward Tomasz Napierala 	if (pg->pg_offload != NULL) {
1035a9d78210SEdward Tomasz Napierala 		log_warnx("cannot set offload to \"%s\" for "
1036a9d78210SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
1037a9d78210SEdward Tomasz Napierala 		    offload, pg->pg_name);
1038a9d78210SEdward Tomasz Napierala 		return (1);
1039a9d78210SEdward Tomasz Napierala 	}
1040a9d78210SEdward Tomasz Napierala 
1041a9d78210SEdward Tomasz Napierala 	pg->pg_offload = checked_strdup(offload);
1042a9d78210SEdward Tomasz Napierala 
1043a9d78210SEdward Tomasz Napierala 	return (0);
1044a9d78210SEdward Tomasz Napierala }
1045a9d78210SEdward Tomasz Napierala 
1046a9d78210SEdward Tomasz Napierala int
10476dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr)
10486dbdbf82SEdward Tomasz Napierala {
10496dbdbf82SEdward Tomasz Napierala 
10506dbdbf82SEdward Tomasz Napierala 	if (pg->pg_redirection != NULL) {
10516dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
10526dbdbf82SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
10536dbdbf82SEdward Tomasz Napierala 		    addr, pg->pg_name);
10546dbdbf82SEdward Tomasz Napierala 		return (1);
10556dbdbf82SEdward Tomasz Napierala 	}
10566dbdbf82SEdward Tomasz Napierala 
10576dbdbf82SEdward Tomasz Napierala 	pg->pg_redirection = checked_strdup(addr);
10586dbdbf82SEdward Tomasz Napierala 
10596dbdbf82SEdward Tomasz Napierala 	return (0);
10606dbdbf82SEdward Tomasz Napierala }
10616dbdbf82SEdward Tomasz Napierala 
1062009ea47eSEdward Tomasz Napierala static bool
1063009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
1064009ea47eSEdward Tomasz Napierala {
1065009ea47eSEdward Tomasz Napierala 	switch (ch) {
1066009ea47eSEdward Tomasz Napierala 	case '0':
1067009ea47eSEdward Tomasz Napierala 	case '1':
1068009ea47eSEdward Tomasz Napierala 	case '2':
1069009ea47eSEdward Tomasz Napierala 	case '3':
1070009ea47eSEdward Tomasz Napierala 	case '4':
1071009ea47eSEdward Tomasz Napierala 	case '5':
1072009ea47eSEdward Tomasz Napierala 	case '6':
1073009ea47eSEdward Tomasz Napierala 	case '7':
1074009ea47eSEdward Tomasz Napierala 	case '8':
1075009ea47eSEdward Tomasz Napierala 	case '9':
1076009ea47eSEdward Tomasz Napierala 	case 'a':
1077009ea47eSEdward Tomasz Napierala 	case 'A':
1078009ea47eSEdward Tomasz Napierala 	case 'b':
1079009ea47eSEdward Tomasz Napierala 	case 'B':
1080009ea47eSEdward Tomasz Napierala 	case 'c':
1081009ea47eSEdward Tomasz Napierala 	case 'C':
1082009ea47eSEdward Tomasz Napierala 	case 'd':
1083009ea47eSEdward Tomasz Napierala 	case 'D':
1084009ea47eSEdward Tomasz Napierala 	case 'e':
1085009ea47eSEdward Tomasz Napierala 	case 'E':
1086009ea47eSEdward Tomasz Napierala 	case 'f':
1087009ea47eSEdward Tomasz Napierala 	case 'F':
1088009ea47eSEdward Tomasz Napierala 		return (true);
1089009ea47eSEdward Tomasz Napierala 	default:
1090009ea47eSEdward Tomasz Napierala 		return (false);
1091009ea47eSEdward Tomasz Napierala 	}
1092009ea47eSEdward Tomasz Napierala }
1093009ea47eSEdward Tomasz Napierala 
1094009ea47eSEdward Tomasz Napierala bool
1095009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
1096009ea47eSEdward Tomasz Napierala {
1097009ea47eSEdward Tomasz Napierala 	int i;
1098009ea47eSEdward Tomasz Napierala 
1099009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
1100009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
1101009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
1102009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
1103009ea47eSEdward Tomasz Napierala 		return (false);
1104009ea47eSEdward Tomasz Napierala 	}
1105009ea47eSEdward Tomasz Napierala 
1106009ea47eSEdward Tomasz Napierala 	/*
1107009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
1108009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
1109009ea47eSEdward Tomasz Napierala 	 */
1110009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1111009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
1112009ea47eSEdward Tomasz Napierala 			/*
1113009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
1114009ea47eSEdward Tomasz Napierala 			 *      by 3.2.6.2: iSCSI Name Encoding.
1115009ea47eSEdward Tomasz Napierala 			 */
1116009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
1117009ea47eSEdward Tomasz Napierala 				continue;
1118009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1119009ea47eSEdward Tomasz Napierala 				continue;
1120009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
1121009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
1122009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
1123009ea47eSEdward Tomasz Napierala 			break;
1124009ea47eSEdward Tomasz Napierala 		}
1125009ea47eSEdward Tomasz Napierala 		/*
1126009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
1127009ea47eSEdward Tomasz Napierala 		 */
1128009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1129009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
1130009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
1131009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
1132009ea47eSEdward Tomasz Napierala 			    "digits", name);
1133009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
1134009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1135009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1136009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1137009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1138009ea47eSEdward Tomasz Napierala 				break;
1139009ea47eSEdward Tomasz Napierala 			}
1140009ea47eSEdward Tomasz Napierala 		}
1141009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1142009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
1143009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
1144009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
1145009ea47eSEdward Tomasz Napierala 			    "digits", name);
1146009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
1147009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1148009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1149009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1150009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1151009ea47eSEdward Tomasz Napierala 				break;
1152009ea47eSEdward Tomasz Napierala 			}
1153009ea47eSEdward Tomasz Napierala 		}
1154009ea47eSEdward Tomasz Napierala 	} else {
1155009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
1156d7b87b89SJosh Paetzel 		    "either \"iqn.\", \"eui.\", or \"naa.\"",
1157009ea47eSEdward Tomasz Napierala 		    name);
1158009ea47eSEdward Tomasz Napierala 	}
1159009ea47eSEdward Tomasz Napierala 	return (true);
1160009ea47eSEdward Tomasz Napierala }
1161009ea47eSEdward Tomasz Napierala 
1162057abcb0SAlexander Motin struct pport *
1163057abcb0SAlexander Motin pport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1164057abcb0SAlexander Motin {
1165057abcb0SAlexander Motin 	struct pport *pp;
1166057abcb0SAlexander Motin 
1167057abcb0SAlexander Motin 	pp = calloc(1, sizeof(*pp));
1168057abcb0SAlexander Motin 	if (pp == NULL)
1169057abcb0SAlexander Motin 		log_err(1, "calloc");
1170057abcb0SAlexander Motin 	pp->pp_conf = conf;
1171057abcb0SAlexander Motin 	pp->pp_name = checked_strdup(name);
1172057abcb0SAlexander Motin 	pp->pp_ctl_port = ctl_port;
1173057abcb0SAlexander Motin 	TAILQ_INIT(&pp->pp_ports);
1174057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1175057abcb0SAlexander Motin 	return (pp);
1176057abcb0SAlexander Motin }
1177057abcb0SAlexander Motin 
1178057abcb0SAlexander Motin struct pport *
1179057abcb0SAlexander Motin pport_find(const struct conf *conf, const char *name)
1180057abcb0SAlexander Motin {
1181057abcb0SAlexander Motin 	struct pport *pp;
1182057abcb0SAlexander Motin 
1183057abcb0SAlexander Motin 	TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1184057abcb0SAlexander Motin 		if (strcasecmp(pp->pp_name, name) == 0)
1185057abcb0SAlexander Motin 			return (pp);
1186057abcb0SAlexander Motin 	}
1187057abcb0SAlexander Motin 	return (NULL);
1188057abcb0SAlexander Motin }
1189057abcb0SAlexander Motin 
1190057abcb0SAlexander Motin struct pport *
1191057abcb0SAlexander Motin pport_copy(struct pport *pp, struct conf *conf)
1192057abcb0SAlexander Motin {
1193057abcb0SAlexander Motin 	struct pport *ppnew;
1194057abcb0SAlexander Motin 
1195057abcb0SAlexander Motin 	ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1196057abcb0SAlexander Motin 	return (ppnew);
1197057abcb0SAlexander Motin }
1198057abcb0SAlexander Motin 
1199057abcb0SAlexander Motin void
1200057abcb0SAlexander Motin pport_delete(struct pport *pp)
1201057abcb0SAlexander Motin {
1202057abcb0SAlexander Motin 	struct port *port, *tport;
1203057abcb0SAlexander Motin 
1204057abcb0SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1205057abcb0SAlexander Motin 		port_delete(port);
1206057abcb0SAlexander Motin 	TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1207057abcb0SAlexander Motin 	free(pp->pp_name);
1208057abcb0SAlexander Motin 	free(pp);
1209057abcb0SAlexander Motin }
1210057abcb0SAlexander Motin 
121192847ee1SAlexander Motin struct port *
121292847ee1SAlexander Motin port_new(struct conf *conf, struct target *target, struct portal_group *pg)
121392847ee1SAlexander Motin {
121492847ee1SAlexander Motin 	struct port *port;
1215057abcb0SAlexander Motin 	char *name;
121655e4579eSAlexander Motin 	int ret;
121792847ee1SAlexander Motin 
121855e4579eSAlexander Motin 	ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
121955e4579eSAlexander Motin 	if (ret <= 0)
122055e4579eSAlexander Motin 		log_err(1, "asprintf");
1221057abcb0SAlexander Motin 	if (port_find(conf, name) != NULL) {
1222057abcb0SAlexander Motin 		log_warnx("duplicate port \"%s\"", name);
1223057abcb0SAlexander Motin 		free(name);
1224057abcb0SAlexander Motin 		return (NULL);
1225057abcb0SAlexander Motin 	}
122692847ee1SAlexander Motin 	port = calloc(1, sizeof(*port));
122792847ee1SAlexander Motin 	if (port == NULL)
122892847ee1SAlexander Motin 		log_err(1, "calloc");
122992847ee1SAlexander Motin 	port->p_conf = conf;
1230057abcb0SAlexander Motin 	port->p_name = name;
123192847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
123292847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
123392847ee1SAlexander Motin 	port->p_target = target;
123492847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
123592847ee1SAlexander Motin 	port->p_portal_group = pg;
1236db7bf2baSAlexander Motin 	port->p_foreign = pg->pg_foreign;
123792847ee1SAlexander Motin 	return (port);
123892847ee1SAlexander Motin }
123992847ee1SAlexander Motin 
124092847ee1SAlexander Motin struct port *
1241057abcb0SAlexander Motin port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1242057abcb0SAlexander Motin {
1243057abcb0SAlexander Motin 	struct port *port;
1244057abcb0SAlexander Motin 	char *name;
124555e4579eSAlexander Motin 	int ret;
1246057abcb0SAlexander Motin 
124755e4579eSAlexander Motin 	ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
124855e4579eSAlexander Motin 	if (ret <= 0)
124955e4579eSAlexander Motin 		log_err(1, "asprintf");
1250057abcb0SAlexander Motin 	if (port_find(conf, name) != NULL) {
1251057abcb0SAlexander Motin 		log_warnx("duplicate port \"%s\"", name);
1252057abcb0SAlexander Motin 		free(name);
1253057abcb0SAlexander Motin 		return (NULL);
1254057abcb0SAlexander Motin 	}
1255057abcb0SAlexander Motin 	port = calloc(1, sizeof(*port));
1256057abcb0SAlexander Motin 	if (port == NULL)
1257057abcb0SAlexander Motin 		log_err(1, "calloc");
1258057abcb0SAlexander Motin 	port->p_conf = conf;
1259057abcb0SAlexander Motin 	port->p_name = name;
1260057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1261057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1262057abcb0SAlexander Motin 	port->p_target = target;
1263057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1264057abcb0SAlexander Motin 	port->p_pport = pp;
1265057abcb0SAlexander Motin 	return (port);
1266057abcb0SAlexander Motin }
1267057abcb0SAlexander Motin 
1268057abcb0SAlexander Motin struct port *
126992847ee1SAlexander Motin port_find(const struct conf *conf, const char *name)
127092847ee1SAlexander Motin {
127192847ee1SAlexander Motin 	struct port *port;
127292847ee1SAlexander Motin 
127392847ee1SAlexander Motin 	TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
127492847ee1SAlexander Motin 		if (strcasecmp(port->p_name, name) == 0)
127592847ee1SAlexander Motin 			return (port);
127692847ee1SAlexander Motin 	}
127792847ee1SAlexander Motin 
127892847ee1SAlexander Motin 	return (NULL);
127992847ee1SAlexander Motin }
128092847ee1SAlexander Motin 
128192847ee1SAlexander Motin struct port *
128292847ee1SAlexander Motin port_find_in_pg(const struct portal_group *pg, const char *target)
128392847ee1SAlexander Motin {
128492847ee1SAlexander Motin 	struct port *port;
128592847ee1SAlexander Motin 
128692847ee1SAlexander Motin 	TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
128792847ee1SAlexander Motin 		if (strcasecmp(port->p_target->t_name, target) == 0)
128892847ee1SAlexander Motin 			return (port);
128992847ee1SAlexander Motin 	}
129092847ee1SAlexander Motin 
129192847ee1SAlexander Motin 	return (NULL);
129292847ee1SAlexander Motin }
129392847ee1SAlexander Motin 
129492847ee1SAlexander Motin void
129592847ee1SAlexander Motin port_delete(struct port *port)
129692847ee1SAlexander Motin {
129792847ee1SAlexander Motin 
129892847ee1SAlexander Motin 	if (port->p_portal_group)
129992847ee1SAlexander Motin 		TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1300057abcb0SAlexander Motin 	if (port->p_pport)
1301057abcb0SAlexander Motin 		TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
130292847ee1SAlexander Motin 	if (port->p_target)
130392847ee1SAlexander Motin 		TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
130492847ee1SAlexander Motin 	TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
130592847ee1SAlexander Motin 	free(port->p_name);
130692847ee1SAlexander Motin 	free(port);
130792847ee1SAlexander Motin }
130892847ee1SAlexander Motin 
1309009ea47eSEdward Tomasz Napierala struct target *
1310f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
1311009ea47eSEdward Tomasz Napierala {
1312009ea47eSEdward Tomasz Napierala 	struct target *targ;
1313009ea47eSEdward Tomasz Napierala 	int i, len;
1314009ea47eSEdward Tomasz Napierala 
1315f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
1316009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
1317f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
1318009ea47eSEdward Tomasz Napierala 		return (NULL);
1319009ea47eSEdward Tomasz Napierala 	}
1320f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
1321f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
1322009ea47eSEdward Tomasz Napierala 		return (NULL);
1323009ea47eSEdward Tomasz Napierala 	}
1324009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
1325009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
1326009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1327f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
1328009ea47eSEdward Tomasz Napierala 
1329009ea47eSEdward Tomasz Napierala 	/*
1330009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
1331009ea47eSEdward Tomasz Napierala 	 */
1332f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
1333009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
1334f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
1335009ea47eSEdward Tomasz Napierala 
1336009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
133792847ee1SAlexander Motin 	TAILQ_INIT(&targ->t_ports);
1338009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1339009ea47eSEdward Tomasz Napierala 
1340009ea47eSEdward Tomasz Napierala 	return (targ);
1341009ea47eSEdward Tomasz Napierala }
1342009ea47eSEdward Tomasz Napierala 
1343009ea47eSEdward Tomasz Napierala void
1344009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
1345009ea47eSEdward Tomasz Napierala {
134692847ee1SAlexander Motin 	struct port *port, *tport;
1347009ea47eSEdward Tomasz Napierala 
134892847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
134992847ee1SAlexander Motin 		port_delete(port);
1350009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1351009ea47eSEdward Tomasz Napierala 
1352f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
13536dbdbf82SEdward Tomasz Napierala 	free(targ->t_redirection);
1354009ea47eSEdward Tomasz Napierala 	free(targ);
1355009ea47eSEdward Tomasz Napierala }
1356009ea47eSEdward Tomasz Napierala 
1357009ea47eSEdward Tomasz Napierala struct target *
1358f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
1359009ea47eSEdward Tomasz Napierala {
1360009ea47eSEdward Tomasz Napierala 	struct target *targ;
1361009ea47eSEdward Tomasz Napierala 
1362009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1363f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
1364009ea47eSEdward Tomasz Napierala 			return (targ);
1365009ea47eSEdward Tomasz Napierala 	}
1366009ea47eSEdward Tomasz Napierala 
1367009ea47eSEdward Tomasz Napierala 	return (NULL);
1368009ea47eSEdward Tomasz Napierala }
1369009ea47eSEdward Tomasz Napierala 
13706dbdbf82SEdward Tomasz Napierala int
13716dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr)
13726dbdbf82SEdward Tomasz Napierala {
13736dbdbf82SEdward Tomasz Napierala 
13746dbdbf82SEdward Tomasz Napierala 	if (target->t_redirection != NULL) {
13756dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
13766dbdbf82SEdward Tomasz Napierala 		    "target \"%s\"; already defined",
13776dbdbf82SEdward Tomasz Napierala 		    addr, target->t_name);
13786dbdbf82SEdward Tomasz Napierala 		return (1);
13796dbdbf82SEdward Tomasz Napierala 	}
13806dbdbf82SEdward Tomasz Napierala 
13816dbdbf82SEdward Tomasz Napierala 	target->t_redirection = checked_strdup(addr);
13826dbdbf82SEdward Tomasz Napierala 
13836dbdbf82SEdward Tomasz Napierala 	return (0);
13846dbdbf82SEdward Tomasz Napierala }
13856dbdbf82SEdward Tomasz Napierala 
1386009ea47eSEdward Tomasz Napierala struct lun *
1387920c6cbaSAlexander Motin lun_new(struct conf *conf, const char *name)
1388009ea47eSEdward Tomasz Napierala {
1389009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1390009ea47eSEdward Tomasz Napierala 
1391920c6cbaSAlexander Motin 	lun = lun_find(conf, name);
1392009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
1393920c6cbaSAlexander Motin 		log_warnx("duplicated lun \"%s\"", name);
1394009ea47eSEdward Tomasz Napierala 		return (NULL);
1395009ea47eSEdward Tomasz Napierala 	}
1396009ea47eSEdward Tomasz Napierala 
1397009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
1398009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
1399009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1400920c6cbaSAlexander Motin 	lun->l_conf = conf;
1401920c6cbaSAlexander Motin 	lun->l_name = checked_strdup(name);
1402009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
1403920c6cbaSAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
14043933f7b1SAlexander Motin 	lun->l_ctl_lun = -1;
1405009ea47eSEdward Tomasz Napierala 
1406009ea47eSEdward Tomasz Napierala 	return (lun);
1407009ea47eSEdward Tomasz Napierala }
1408009ea47eSEdward Tomasz Napierala 
1409009ea47eSEdward Tomasz Napierala void
1410009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
1411009ea47eSEdward Tomasz Napierala {
1412920c6cbaSAlexander Motin 	struct target *targ;
1413*398290f2SAlexander Motin 	struct option *o, *tmp;
1414920c6cbaSAlexander Motin 	int i;
1415009ea47eSEdward Tomasz Napierala 
1416920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1417920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1418920c6cbaSAlexander Motin 			if (targ->t_luns[i] == lun)
1419920c6cbaSAlexander Motin 				targ->t_luns[i] = NULL;
1420920c6cbaSAlexander Motin 		}
1421920c6cbaSAlexander Motin 	}
1422920c6cbaSAlexander Motin 	TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1423009ea47eSEdward Tomasz Napierala 
1424*398290f2SAlexander Motin 	TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1425*398290f2SAlexander Motin 		option_delete(&lun->l_options, o);
1426920c6cbaSAlexander Motin 	free(lun->l_name);
1427009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1428009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1429009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1430920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1431009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1432009ea47eSEdward Tomasz Napierala 	free(lun);
1433009ea47eSEdward Tomasz Napierala }
1434009ea47eSEdward Tomasz Napierala 
1435009ea47eSEdward Tomasz Napierala struct lun *
1436920c6cbaSAlexander Motin lun_find(const struct conf *conf, const char *name)
1437009ea47eSEdward Tomasz Napierala {
1438009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1439009ea47eSEdward Tomasz Napierala 
1440920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1441920c6cbaSAlexander Motin 		if (strcmp(lun->l_name, name) == 0)
1442009ea47eSEdward Tomasz Napierala 			return (lun);
1443009ea47eSEdward Tomasz Napierala 	}
1444009ea47eSEdward Tomasz Napierala 
1445009ea47eSEdward Tomasz Napierala 	return (NULL);
1446009ea47eSEdward Tomasz Napierala }
1447009ea47eSEdward Tomasz Napierala 
1448009ea47eSEdward Tomasz Napierala void
1449009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
1450009ea47eSEdward Tomasz Napierala {
1451009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1452009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
1453009ea47eSEdward Tomasz Napierala }
1454009ea47eSEdward Tomasz Napierala 
1455009ea47eSEdward Tomasz Napierala void
1456009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
1457009ea47eSEdward Tomasz Napierala {
1458009ea47eSEdward Tomasz Napierala 
1459009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
1460009ea47eSEdward Tomasz Napierala }
1461009ea47eSEdward Tomasz Napierala 
1462009ea47eSEdward Tomasz Napierala void
146391be33dcSAlexander Motin lun_set_device_type(struct lun *lun, uint8_t value)
146491be33dcSAlexander Motin {
146591be33dcSAlexander Motin 
146691be33dcSAlexander Motin 	lun->l_device_type = value;
146791be33dcSAlexander Motin }
146891be33dcSAlexander Motin 
146991be33dcSAlexander Motin void
1470009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
1471009ea47eSEdward Tomasz Napierala {
1472009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1473009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
1474009ea47eSEdward Tomasz Napierala }
1475009ea47eSEdward Tomasz Napierala 
1476009ea47eSEdward Tomasz Napierala void
1477009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
1478009ea47eSEdward Tomasz Napierala {
1479009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1480009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
1481009ea47eSEdward Tomasz Napierala }
1482009ea47eSEdward Tomasz Napierala 
1483009ea47eSEdward Tomasz Napierala void
1484920c6cbaSAlexander Motin lun_set_scsiname(struct lun *lun, const char *value)
1485920c6cbaSAlexander Motin {
1486920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1487920c6cbaSAlexander Motin 	lun->l_scsiname = checked_strdup(value);
1488920c6cbaSAlexander Motin }
1489920c6cbaSAlexander Motin 
1490920c6cbaSAlexander Motin void
1491009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
1492009ea47eSEdward Tomasz Napierala {
1493009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1494009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
1495009ea47eSEdward Tomasz Napierala }
1496009ea47eSEdward Tomasz Napierala 
1497009ea47eSEdward Tomasz Napierala void
1498009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
1499009ea47eSEdward Tomasz Napierala {
1500009ea47eSEdward Tomasz Napierala 
1501009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
1502009ea47eSEdward Tomasz Napierala }
1503009ea47eSEdward Tomasz Napierala 
1504009ea47eSEdward Tomasz Napierala void
1505009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
1506009ea47eSEdward Tomasz Napierala {
1507009ea47eSEdward Tomasz Napierala 
1508009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
1509009ea47eSEdward Tomasz Napierala }
1510009ea47eSEdward Tomasz Napierala 
1511*398290f2SAlexander Motin struct option *
1512*398290f2SAlexander Motin option_new(struct options *options, const char *name, const char *value)
1513009ea47eSEdward Tomasz Napierala {
1514*398290f2SAlexander Motin 	struct option *o;
1515009ea47eSEdward Tomasz Napierala 
1516*398290f2SAlexander Motin 	o = option_find(options, name);
1517*398290f2SAlexander Motin 	if (o != NULL) {
1518*398290f2SAlexander Motin 		log_warnx("duplicated option \"%s\"", name);
1519009ea47eSEdward Tomasz Napierala 		return (NULL);
1520009ea47eSEdward Tomasz Napierala 	}
1521009ea47eSEdward Tomasz Napierala 
1522*398290f2SAlexander Motin 	o = calloc(1, sizeof(*o));
1523*398290f2SAlexander Motin 	if (o == NULL)
1524009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1525*398290f2SAlexander Motin 	o->o_name = checked_strdup(name);
1526*398290f2SAlexander Motin 	o->o_value = checked_strdup(value);
1527*398290f2SAlexander Motin 	TAILQ_INSERT_TAIL(options, o, o_next);
1528009ea47eSEdward Tomasz Napierala 
1529*398290f2SAlexander Motin 	return (o);
1530009ea47eSEdward Tomasz Napierala }
1531009ea47eSEdward Tomasz Napierala 
1532009ea47eSEdward Tomasz Napierala void
1533*398290f2SAlexander Motin option_delete(struct options *options, struct option *o)
1534009ea47eSEdward Tomasz Napierala {
1535009ea47eSEdward Tomasz Napierala 
1536*398290f2SAlexander Motin 	TAILQ_REMOVE(options, o, o_next);
1537*398290f2SAlexander Motin 	free(o->o_name);
1538*398290f2SAlexander Motin 	free(o->o_value);
1539*398290f2SAlexander Motin 	free(o);
1540009ea47eSEdward Tomasz Napierala }
1541009ea47eSEdward Tomasz Napierala 
1542*398290f2SAlexander Motin struct option *
1543*398290f2SAlexander Motin option_find(const struct options *options, const char *name)
1544009ea47eSEdward Tomasz Napierala {
1545*398290f2SAlexander Motin 	struct option *o;
1546009ea47eSEdward Tomasz Napierala 
1547*398290f2SAlexander Motin 	TAILQ_FOREACH(o, options, o_next) {
1548*398290f2SAlexander Motin 		if (strcmp(o->o_name, name) == 0)
1549*398290f2SAlexander Motin 			return (o);
1550009ea47eSEdward Tomasz Napierala 	}
1551009ea47eSEdward Tomasz Napierala 
1552009ea47eSEdward Tomasz Napierala 	return (NULL);
1553009ea47eSEdward Tomasz Napierala }
1554009ea47eSEdward Tomasz Napierala 
1555009ea47eSEdward Tomasz Napierala void
1556*398290f2SAlexander Motin option_set(struct option *o, const char *value)
1557009ea47eSEdward Tomasz Napierala {
1558009ea47eSEdward Tomasz Napierala 
1559*398290f2SAlexander Motin 	free(o->o_value);
1560*398290f2SAlexander Motin 	o->o_value = checked_strdup(value);
1561009ea47eSEdward Tomasz Napierala }
1562009ea47eSEdward Tomasz Napierala 
1563009ea47eSEdward Tomasz Napierala static struct connection *
1564073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host,
1565073edb1cSAlexander Motin     const struct sockaddr *client_sa)
1566009ea47eSEdward Tomasz Napierala {
1567009ea47eSEdward Tomasz Napierala 	struct connection *conn;
1568009ea47eSEdward Tomasz Napierala 
1569009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
1570009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
1571009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1572009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
1573009ea47eSEdward Tomasz Napierala 	conn->conn_socket = fd;
1574009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
1575073edb1cSAlexander Motin 	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1576009ea47eSEdward Tomasz Napierala 
1577009ea47eSEdward Tomasz Napierala 	/*
1578009ea47eSEdward Tomasz Napierala 	 * Default values, from RFC 3720, section 12.
1579009ea47eSEdward Tomasz Napierala 	 */
1580009ea47eSEdward Tomasz Napierala 	conn->conn_max_data_segment_length = 8192;
1581009ea47eSEdward Tomasz Napierala 	conn->conn_max_burst_length = 262144;
1582009ea47eSEdward Tomasz Napierala 	conn->conn_immediate_data = true;
1583009ea47eSEdward Tomasz Napierala 
1584009ea47eSEdward Tomasz Napierala 	return (conn);
1585009ea47eSEdward Tomasz Napierala }
1586009ea47eSEdward Tomasz Napierala 
1587009ea47eSEdward Tomasz Napierala #if 0
1588009ea47eSEdward Tomasz Napierala static void
1589009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
1590009ea47eSEdward Tomasz Napierala {
1591009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1592009ea47eSEdward Tomasz Napierala 	struct auth *auth;
15938cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
15948cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
1595009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1596009ea47eSEdward Tomasz Napierala 	struct portal *portal;
1597009ea47eSEdward Tomasz Napierala 	struct target *targ;
1598009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1599*398290f2SAlexander Motin 	struct option *o;
1600009ea47eSEdward Tomasz Napierala 
1601009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1602009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1603009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1604009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1605009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
1606009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
16078cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
16088cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
16098cb2e958SEdward Tomasz Napierala 			    auth_name->an_initator_name);
16108cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
16118cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
16128cb2e958SEdward Tomasz Napierala 			    auth_portal->an_initator_portal);
1613009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1614009ea47eSEdward Tomasz Napierala 	}
1615009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1616009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1617009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1618009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1619009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1620009ea47eSEdward Tomasz Napierala 	}
1621920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1622920c6cbaSAlexander Motin 		fprintf(stderr, "\tlun %s {\n", lun->l_name);
1623009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1624*398290f2SAlexander Motin 		TAILQ_FOREACH(o, &lun->l_options, o_next)
1625009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\toption %s %s\n",
1626*398290f2SAlexander Motin 			    lo->o_name, lo->o_value);
1627009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t}\n");
1628009ea47eSEdward Tomasz Napierala 	}
1629920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1630920c6cbaSAlexander Motin 		fprintf(stderr, "target %s {\n", targ->t_name);
1631920c6cbaSAlexander Motin 		if (targ->t_alias != NULL)
1632920c6cbaSAlexander Motin 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1633009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1634009ea47eSEdward Tomasz Napierala 	}
1635009ea47eSEdward Tomasz Napierala }
1636009ea47eSEdward Tomasz Napierala #endif
1637009ea47eSEdward Tomasz Napierala 
16382fabfaa5SEdward Tomasz Napierala static int
16392fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
16402fabfaa5SEdward Tomasz Napierala {
16412fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
16422fabfaa5SEdward Tomasz Napierala 
16432fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
16442fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
16452fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
16462fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
1647920c6cbaSAlexander Motin 			log_warnx("missing path for lun \"%s\"",
1648920c6cbaSAlexander Motin 			    lun->l_name);
16492fabfaa5SEdward Tomasz Napierala 			return (1);
16502fabfaa5SEdward Tomasz Napierala 		}
16512fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
16522fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
1653920c6cbaSAlexander Motin 			log_warnx("missing size for ramdisk-backed lun \"%s\"",
1654920c6cbaSAlexander Motin 			    lun->l_name);
16552fabfaa5SEdward Tomasz Napierala 			return (1);
16562fabfaa5SEdward Tomasz Napierala 		}
16572fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
16582fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
1659920c6cbaSAlexander Motin 			    "for ramdisk-backed lun \"%s\"",
1660920c6cbaSAlexander Motin 			    lun->l_name);
16612fabfaa5SEdward Tomasz Napierala 			return (1);
16622fabfaa5SEdward Tomasz Napierala 		}
16632fabfaa5SEdward Tomasz Napierala 	}
16642fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
16656f2f92a5SAlexander Motin 		if (lun->l_device_type == 5)
16666f2f92a5SAlexander Motin 			lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
16676f2f92a5SAlexander Motin 		else
16682fabfaa5SEdward Tomasz Napierala 			lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
16692fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
1670920c6cbaSAlexander Motin 		log_warnx("invalid blocksize for lun \"%s\"; "
1671920c6cbaSAlexander Motin 		    "must be larger than 0", lun->l_name);
16722fabfaa5SEdward Tomasz Napierala 		return (1);
16732fabfaa5SEdward Tomasz Napierala 	}
16742fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1675920c6cbaSAlexander Motin 		log_warnx("invalid size for lun \"%s\"; "
1676920c6cbaSAlexander Motin 		    "must be multiple of blocksize", lun->l_name);
16772fabfaa5SEdward Tomasz Napierala 		return (1);
16782fabfaa5SEdward Tomasz Napierala 	}
1679920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1680a19eebb1SEdward Tomasz Napierala 		if (lun == lun2)
1681a19eebb1SEdward Tomasz Napierala 			continue;
1682a19eebb1SEdward Tomasz Napierala 		if (lun->l_path != NULL && lun2->l_path != NULL &&
1683a19eebb1SEdward Tomasz Napierala 		    strcmp(lun->l_path, lun2->l_path) == 0) {
1684a19eebb1SEdward Tomasz Napierala 			log_debugx("WARNING: path \"%s\" duplicated "
1685920c6cbaSAlexander Motin 			    "between lun \"%s\", and "
1686920c6cbaSAlexander Motin 			    "lun \"%s\"", lun->l_path,
1687920c6cbaSAlexander Motin 			    lun->l_name, lun2->l_name);
1688a19eebb1SEdward Tomasz Napierala 		}
1689a19eebb1SEdward Tomasz Napierala 	}
16902fabfaa5SEdward Tomasz Napierala 
16912fabfaa5SEdward Tomasz Napierala 	return (0);
16922fabfaa5SEdward Tomasz Napierala }
16932fabfaa5SEdward Tomasz Napierala 
1694009ea47eSEdward Tomasz Napierala int
1695009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1696009ea47eSEdward Tomasz Napierala {
1697009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1698009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
169992847ee1SAlexander Motin 	struct port *port;
1700009ea47eSEdward Tomasz Napierala 	struct target *targ;
17012fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
17024e5c38f8SEdward Tomasz Napierala 	bool found;
1703920c6cbaSAlexander Motin 	int error, i;
1704009ea47eSEdward Tomasz Napierala 
1705009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1706009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1707009ea47eSEdward Tomasz Napierala 
1708920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1709920c6cbaSAlexander Motin 		error = conf_verify_lun(lun);
1710920c6cbaSAlexander Motin 		if (error != 0)
1711920c6cbaSAlexander Motin 			return (error);
1712920c6cbaSAlexander Motin 	}
1713009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1714009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1715e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1716e7977549SEdward Tomasz Napierala 			    "default");
1717e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1718009ea47eSEdward Tomasz Napierala 		}
171992847ee1SAlexander Motin 		if (TAILQ_EMPTY(&targ->t_ports)) {
172092847ee1SAlexander Motin 			pg = portal_group_find(conf, "default");
172192847ee1SAlexander Motin 			assert(pg != NULL);
172292847ee1SAlexander Motin 			port_new(conf, targ, pg);
1723009ea47eSEdward Tomasz Napierala 		}
17244e5c38f8SEdward Tomasz Napierala 		found = false;
1725920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1726920c6cbaSAlexander Motin 			if (targ->t_luns[i] != NULL)
17274e5c38f8SEdward Tomasz Napierala 				found = true;
1728009ea47eSEdward Tomasz Napierala 		}
17296dbdbf82SEdward Tomasz Napierala 		if (!found && targ->t_redirection == NULL) {
173019b59f13SEdward Tomasz Napierala 			log_warnx("no LUNs defined for target \"%s\"",
173119b59f13SEdward Tomasz Napierala 			    targ->t_name);
1732009ea47eSEdward Tomasz Napierala 		}
17336dbdbf82SEdward Tomasz Napierala 		if (found && targ->t_redirection != NULL) {
17346dbdbf82SEdward Tomasz Napierala 			log_debugx("target \"%s\" contains luns, "
17356dbdbf82SEdward Tomasz Napierala 			    " but configured for redirection",
17366dbdbf82SEdward Tomasz Napierala 			    targ->t_name);
17376dbdbf82SEdward Tomasz Napierala 		}
1738009ea47eSEdward Tomasz Napierala 	}
1739009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1740009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1741009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1742009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1743ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1744009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1745009ea47eSEdward Tomasz Napierala 		}
1746009ea47eSEdward Tomasz Napierala 
174705374883SEdward Tomasz Napierala 		if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
174805374883SEdward Tomasz Napierala 			pg->pg_discovery_filter = PG_FILTER_NONE;
174905374883SEdward Tomasz Napierala 
17506dbdbf82SEdward Tomasz Napierala 		if (pg->pg_redirection != NULL) {
1751db7bf2baSAlexander Motin 			if (!TAILQ_EMPTY(&pg->pg_ports)) {
17526dbdbf82SEdward Tomasz Napierala 				log_debugx("portal-group \"%s\" assigned "
175392847ee1SAlexander Motin 				    "to target, but configured "
17546dbdbf82SEdward Tomasz Napierala 				    "for redirection",
175592847ee1SAlexander Motin 				    pg->pg_name);
17566dbdbf82SEdward Tomasz Napierala 			}
17576dbdbf82SEdward Tomasz Napierala 			pg->pg_unassigned = false;
1758db7bf2baSAlexander Motin 		} else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1759db7bf2baSAlexander Motin 			pg->pg_unassigned = false;
17606dbdbf82SEdward Tomasz Napierala 		} else {
1761009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1762009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1763009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1764009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
17656dbdbf82SEdward Tomasz Napierala 		}
1766009ea47eSEdward Tomasz Napierala 	}
1767009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1768009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1769009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1770009ea47eSEdward Tomasz Napierala 		else
1771009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1772009ea47eSEdward Tomasz Napierala 
17734e5c38f8SEdward Tomasz Napierala 		found = false;
1774009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
17754e5c38f8SEdward Tomasz Napierala 			if (targ->t_auth_group == ag) {
17764e5c38f8SEdward Tomasz Napierala 				found = true;
1777009ea47eSEdward Tomasz Napierala 				break;
1778009ea47eSEdward Tomasz Napierala 			}
17794e5c38f8SEdward Tomasz Napierala 		}
178092847ee1SAlexander Motin 		TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
178192847ee1SAlexander Motin 			if (port->p_auth_group == ag) {
178292847ee1SAlexander Motin 				found = true;
178392847ee1SAlexander Motin 				break;
178492847ee1SAlexander Motin 			}
178592847ee1SAlexander Motin 		}
17864e5c38f8SEdward Tomasz Napierala 		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
17874e5c38f8SEdward Tomasz Napierala 			if (pg->pg_discovery_auth_group == ag) {
17884e5c38f8SEdward Tomasz Napierala 				found = true;
17894e5c38f8SEdward Tomasz Napierala 				break;
17904e5c38f8SEdward Tomasz Napierala 			}
17914e5c38f8SEdward Tomasz Napierala 		}
17924e5c38f8SEdward Tomasz Napierala 		if (!found && ag->ag_name != NULL &&
1793ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1794009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1795009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1796009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1797009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1798009ea47eSEdward Tomasz Napierala 		}
1799009ea47eSEdward Tomasz Napierala 	}
1800009ea47eSEdward Tomasz Napierala 
1801009ea47eSEdward Tomasz Napierala 	return (0);
1802009ea47eSEdward Tomasz Napierala }
1803009ea47eSEdward Tomasz Napierala 
1804009ea47eSEdward Tomasz Napierala static int
1805009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1806009ea47eSEdward Tomasz Napierala {
1807009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1808009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1809009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
181092847ee1SAlexander Motin 	struct port *oldport, *newport, *tmpport;
1811829603e2SAlexander Motin 	struct isns *oldns, *newns;
1812009ea47eSEdward Tomasz Napierala 	pid_t otherpid;
18138b94b583SAlexander Motin 	int changed, cumulated_error = 0, error, sockbuf;
1814009ea47eSEdward Tomasz Napierala 	int one = 1;
1815009ea47eSEdward Tomasz Napierala 
1816009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1817009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1818009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1819009ea47eSEdward Tomasz Napierala 	}
1820009ea47eSEdward Tomasz Napierala 
1821009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_pidfh != NULL) {
1822009ea47eSEdward Tomasz Napierala 		assert(oldconf->conf_pidfile_path != NULL);
1823009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfile_path != NULL &&
1824009ea47eSEdward Tomasz Napierala 		    strcmp(oldconf->conf_pidfile_path,
1825009ea47eSEdward Tomasz Napierala 		    newconf->conf_pidfile_path) == 0) {
1826009ea47eSEdward Tomasz Napierala 			newconf->conf_pidfh = oldconf->conf_pidfh;
1827009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1828009ea47eSEdward Tomasz Napierala 		} else {
1829009ea47eSEdward Tomasz Napierala 			log_debugx("removing pidfile %s",
1830009ea47eSEdward Tomasz Napierala 			    oldconf->conf_pidfile_path);
1831009ea47eSEdward Tomasz Napierala 			pidfile_remove(oldconf->conf_pidfh);
1832009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1833009ea47eSEdward Tomasz Napierala 		}
1834009ea47eSEdward Tomasz Napierala 	}
1835009ea47eSEdward Tomasz Napierala 
1836009ea47eSEdward Tomasz Napierala 	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1837009ea47eSEdward Tomasz Napierala 		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1838009ea47eSEdward Tomasz Napierala 		newconf->conf_pidfh =
1839009ea47eSEdward Tomasz Napierala 		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1840009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfh == NULL) {
1841009ea47eSEdward Tomasz Napierala 			if (errno == EEXIST)
1842009ea47eSEdward Tomasz Napierala 				log_errx(1, "daemon already running, pid: %jd.",
1843009ea47eSEdward Tomasz Napierala 				    (intmax_t)otherpid);
1844009ea47eSEdward Tomasz Napierala 			log_err(1, "cannot open or create pidfile \"%s\"",
1845009ea47eSEdward Tomasz Napierala 			    newconf->conf_pidfile_path);
1846009ea47eSEdward Tomasz Napierala 		}
1847009ea47eSEdward Tomasz Napierala 	}
1848009ea47eSEdward Tomasz Napierala 
18496b31e130SAlexander Motin 	/*
18506b31e130SAlexander Motin 	 * Go through the new portal groups, assigning tags or preserving old.
18516b31e130SAlexander Motin 	 */
18526b31e130SAlexander Motin 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1853db7bf2baSAlexander Motin 		if (newpg->pg_tag != 0)
1854db7bf2baSAlexander Motin 			continue;
18556b31e130SAlexander Motin 		oldpg = portal_group_find(oldconf, newpg->pg_name);
18566b31e130SAlexander Motin 		if (oldpg != NULL)
18576b31e130SAlexander Motin 			newpg->pg_tag = oldpg->pg_tag;
18586b31e130SAlexander Motin 		else
18596b31e130SAlexander Motin 			newpg->pg_tag = ++last_portal_group_tag;
18606b31e130SAlexander Motin 	}
18616b31e130SAlexander Motin 
1862829603e2SAlexander Motin 	/* Deregister on removed iSNS servers. */
1863829603e2SAlexander Motin 	TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1864829603e2SAlexander Motin 		TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1865829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1866829603e2SAlexander Motin 				break;
1867829603e2SAlexander Motin 		}
1868829603e2SAlexander Motin 		if (newns == NULL)
1869829603e2SAlexander Motin 			isns_deregister(oldns);
1870829603e2SAlexander Motin 	}
1871829603e2SAlexander Motin 
1872d5e316e5SEdward Tomasz Napierala 	/*
1873d5e316e5SEdward Tomasz Napierala 	 * XXX: If target or lun removal fails, we should somehow "move"
1874d5e316e5SEdward Tomasz Napierala 	 *      the old lun or target into newconf, so that subsequent
187561a2a354SEdward Tomasz Napierala 	 *      conf_apply() would try to remove them again.  That would
187661a2a354SEdward Tomasz Napierala 	 *      be somewhat hairy, though, and lun deletion failures don't
187761a2a354SEdward Tomasz Napierala 	 *      really happen, so leave it as it is for now.
1878d5e316e5SEdward Tomasz Napierala 	 */
1879009ea47eSEdward Tomasz Napierala 	/*
188092847ee1SAlexander Motin 	 * First, remove any ports present in the old configuration
1881009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
1882009ea47eSEdward Tomasz Napierala 	 */
188392847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1884db7bf2baSAlexander Motin 		if (oldport->p_foreign)
1885db7bf2baSAlexander Motin 			continue;
188692847ee1SAlexander Motin 		newport = port_find(newconf, oldport->p_name);
1887db7bf2baSAlexander Motin 		if (newport != NULL && !newport->p_foreign)
1888920c6cbaSAlexander Motin 			continue;
1889057abcb0SAlexander Motin 		log_debugx("removing port \"%s\"", oldport->p_name);
189092847ee1SAlexander Motin 		error = kernel_port_remove(oldport);
18916de78f9fSAlexander Motin 		if (error != 0) {
189292847ee1SAlexander Motin 			log_warnx("failed to remove port %s",
189392847ee1SAlexander Motin 			    oldport->p_name);
18946de78f9fSAlexander Motin 			/*
18956de78f9fSAlexander Motin 			 * XXX: Uncomment after fixing the root cause.
18966de78f9fSAlexander Motin 			 *
18976de78f9fSAlexander Motin 			 * cumulated_error++;
18986de78f9fSAlexander Motin 			 */
18996de78f9fSAlexander Motin 		}
1900009ea47eSEdward Tomasz Napierala 	}
1901009ea47eSEdward Tomasz Napierala 
1902009ea47eSEdward Tomasz Napierala 	/*
1903920c6cbaSAlexander Motin 	 * Second, remove any LUNs present in the old configuration
1904009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
1905009ea47eSEdward Tomasz Napierala 	 */
1906920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1907920c6cbaSAlexander Motin 		newlun = lun_find(newconf, oldlun->l_name);
1908009ea47eSEdward Tomasz Napierala 		if (newlun == NULL) {
1909920c6cbaSAlexander Motin 			log_debugx("lun \"%s\", CTL lun %d "
1910cadf3831SEdward Tomasz Napierala 			    "not found in new configuration; "
1911920c6cbaSAlexander Motin 			    "removing", oldlun->l_name, oldlun->l_ctl_lun);
1912009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
1913009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1914920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
1915920c6cbaSAlexander Motin 				    "CTL lun %d",
1916920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
1917009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1918009ea47eSEdward Tomasz Napierala 			}
1919009ea47eSEdward Tomasz Napierala 			continue;
1920009ea47eSEdward Tomasz Napierala 		}
1921009ea47eSEdward Tomasz Napierala 
1922009ea47eSEdward Tomasz Napierala 		/*
1923009ea47eSEdward Tomasz Napierala 		 * Also remove the LUNs changed by more than size.
1924009ea47eSEdward Tomasz Napierala 		 */
1925009ea47eSEdward Tomasz Napierala 		changed = 0;
1926009ea47eSEdward Tomasz Napierala 		assert(oldlun->l_backend != NULL);
1927009ea47eSEdward Tomasz Napierala 		assert(newlun->l_backend != NULL);
1928009ea47eSEdward Tomasz Napierala 		if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1929920c6cbaSAlexander Motin 			log_debugx("backend for lun \"%s\", "
1930009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1931920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1932009ea47eSEdward Tomasz Napierala 			changed = 1;
1933009ea47eSEdward Tomasz Napierala 		}
1934009ea47eSEdward Tomasz Napierala 		if (oldlun->l_blocksize != newlun->l_blocksize) {
1935920c6cbaSAlexander Motin 			log_debugx("blocksize for lun \"%s\", "
1936009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1937920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1938009ea47eSEdward Tomasz Napierala 			changed = 1;
1939009ea47eSEdward Tomasz Napierala 		}
1940009ea47eSEdward Tomasz Napierala 		if (newlun->l_device_id != NULL &&
1941009ea47eSEdward Tomasz Napierala 		    (oldlun->l_device_id == NULL ||
1942009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1943009ea47eSEdward Tomasz Napierala 		     0)) {
1944920c6cbaSAlexander Motin 			log_debugx("device-id for lun \"%s\", "
1945009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1946920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1947009ea47eSEdward Tomasz Napierala 			changed = 1;
1948009ea47eSEdward Tomasz Napierala 		}
1949009ea47eSEdward Tomasz Napierala 		if (newlun->l_path != NULL &&
1950009ea47eSEdward Tomasz Napierala 		    (oldlun->l_path == NULL ||
1951009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1952920c6cbaSAlexander Motin 			log_debugx("path for lun \"%s\", "
1953009ea47eSEdward Tomasz Napierala 			    "CTL lun %d, changed; removing",
1954920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1955009ea47eSEdward Tomasz Napierala 			changed = 1;
1956009ea47eSEdward Tomasz Napierala 		}
1957009ea47eSEdward Tomasz Napierala 		if (newlun->l_serial != NULL &&
1958009ea47eSEdward Tomasz Napierala 		    (oldlun->l_serial == NULL ||
1959009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1960920c6cbaSAlexander Motin 			log_debugx("serial for lun \"%s\", "
1961009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1962920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1963009ea47eSEdward Tomasz Napierala 			changed = 1;
1964009ea47eSEdward Tomasz Napierala 		}
1965009ea47eSEdward Tomasz Napierala 		if (changed) {
1966009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
1967009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1968920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
1969920c6cbaSAlexander Motin 				    "CTL lun %d",
1970920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
1971009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1972009ea47eSEdward Tomasz Napierala 			}
1973009ea47eSEdward Tomasz Napierala 			lun_delete(oldlun);
1974009ea47eSEdward Tomasz Napierala 			continue;
1975009ea47eSEdward Tomasz Napierala 		}
1976009ea47eSEdward Tomasz Napierala 
1977009ea47eSEdward Tomasz Napierala 		lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1978009ea47eSEdward Tomasz Napierala 	}
1979920c6cbaSAlexander Motin 
1980920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1981920c6cbaSAlexander Motin 		oldlun = lun_find(oldconf, newlun->l_name);
1982920c6cbaSAlexander Motin 		if (oldlun != NULL) {
1983a3977beaSAlexander Motin 			log_debugx("modifying lun \"%s\", CTL lun %d",
1984920c6cbaSAlexander Motin 			    newlun->l_name, newlun->l_ctl_lun);
1985a3977beaSAlexander Motin 			error = kernel_lun_modify(newlun);
1986920c6cbaSAlexander Motin 			if (error != 0) {
1987920c6cbaSAlexander Motin 				log_warnx("failed to "
1988a3977beaSAlexander Motin 				    "modify lun \"%s\", CTL lun %d",
1989a3977beaSAlexander Motin 				    newlun->l_name, newlun->l_ctl_lun);
1990920c6cbaSAlexander Motin 				cumulated_error++;
1991920c6cbaSAlexander Motin 			}
1992920c6cbaSAlexander Motin 			continue;
1993920c6cbaSAlexander Motin 		}
1994920c6cbaSAlexander Motin 		log_debugx("adding lun \"%s\"", newlun->l_name);
1995920c6cbaSAlexander Motin 		error = kernel_lun_add(newlun);
1996920c6cbaSAlexander Motin 		if (error != 0) {
1997920c6cbaSAlexander Motin 			log_warnx("failed to add lun \"%s\"", newlun->l_name);
1998920c6cbaSAlexander Motin 			lun_delete(newlun);
1999920c6cbaSAlexander Motin 			cumulated_error++;
2000920c6cbaSAlexander Motin 		}
2001009ea47eSEdward Tomasz Napierala 	}
2002009ea47eSEdward Tomasz Napierala 
2003009ea47eSEdward Tomasz Napierala 	/*
200492847ee1SAlexander Motin 	 * Now add new ports or modify existing ones.
2005009ea47eSEdward Tomasz Napierala 	 */
200692847ee1SAlexander Motin 	TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
2007db7bf2baSAlexander Motin 		if (newport->p_foreign)
2008db7bf2baSAlexander Motin 			continue;
200992847ee1SAlexander Motin 		oldport = port_find(oldconf, newport->p_name);
2010009ea47eSEdward Tomasz Napierala 
2011db7bf2baSAlexander Motin 		if (oldport == NULL || oldport->p_foreign) {
2012057abcb0SAlexander Motin 			log_debugx("adding port \"%s\"", newport->p_name);
201392847ee1SAlexander Motin 			error = kernel_port_add(newport);
201492847ee1SAlexander Motin 		} else {
2015057abcb0SAlexander Motin 			log_debugx("updating port \"%s\"", newport->p_name);
201692847ee1SAlexander Motin 			newport->p_ctl_port = oldport->p_ctl_port;
2017828524c1SAlexander Motin 			error = kernel_port_update(newport, oldport);
2018920c6cbaSAlexander Motin 		}
2019e48bb73aSEdward Tomasz Napierala 		if (error != 0) {
202092847ee1SAlexander Motin 			log_warnx("failed to %s port %s",
202192847ee1SAlexander Motin 			    (oldport == NULL) ? "add" : "update",
202292847ee1SAlexander Motin 			    newport->p_name);
2023e48bb73aSEdward Tomasz Napierala 			/*
2024e48bb73aSEdward Tomasz Napierala 			 * XXX: Uncomment after fixing the root cause.
2025e48bb73aSEdward Tomasz Napierala 			 *
2026e48bb73aSEdward Tomasz Napierala 			 * cumulated_error++;
2027e48bb73aSEdward Tomasz Napierala 			 */
2028e48bb73aSEdward Tomasz Napierala 		}
2029e48bb73aSEdward Tomasz Napierala 	}
2030009ea47eSEdward Tomasz Napierala 
2031009ea47eSEdward Tomasz Napierala 	/*
2032463a577bSEitan Adler 	 * Go through the new portals, opening the sockets as necessary.
2033009ea47eSEdward Tomasz Napierala 	 */
2034009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2035db7bf2baSAlexander Motin 		if (newpg->pg_foreign)
2036db7bf2baSAlexander Motin 			continue;
2037009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
2038009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
2039009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
2040009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
2041009ea47eSEdward Tomasz Napierala 			continue;
2042009ea47eSEdward Tomasz Napierala 		}
2043009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2044009ea47eSEdward Tomasz Napierala 			/*
2045009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
2046009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
2047009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
2048009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
2049009ea47eSEdward Tomasz Napierala 			 */
2050009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2051009ea47eSEdward Tomasz Napierala 			    pg_next) {
2052009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2053009ea47eSEdward Tomasz Napierala 				    p_next) {
2054009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
2055009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
2056009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
2057009ea47eSEdward Tomasz Napierala 						newp->p_socket =
2058009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
2059009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
2060009ea47eSEdward Tomasz Napierala 						break;
2061009ea47eSEdward Tomasz Napierala 					}
2062009ea47eSEdward Tomasz Napierala 				}
2063009ea47eSEdward Tomasz Napierala 			}
2064009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
2065009ea47eSEdward Tomasz Napierala 				/*
2066009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
2067009ea47eSEdward Tomasz Napierala 				 */
2068009ea47eSEdward Tomasz Napierala 				continue;
2069009ea47eSEdward Tomasz Napierala 			}
2070009ea47eSEdward Tomasz Napierala 
2071009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2072ba3a2d31SEdward Tomasz Napierala 			if (proxy_mode) {
20738cab2ed4SEdward Tomasz Napierala 				newpg->pg_conf->conf_portal_id++;
20748cab2ed4SEdward Tomasz Napierala 				newp->p_id = newpg->pg_conf->conf_portal_id;
20758cab2ed4SEdward Tomasz Napierala 				log_debugx("listening on %s, portal-group "
20768cab2ed4SEdward Tomasz Napierala 				    "\"%s\", portal id %d, using ICL proxy",
20778cab2ed4SEdward Tomasz Napierala 				    newp->p_listen, newpg->pg_name, newp->p_id);
20788cab2ed4SEdward Tomasz Napierala 				kernel_listen(newp->p_ai, newp->p_iser,
20798cab2ed4SEdward Tomasz Napierala 				    newp->p_id);
2080ba3a2d31SEdward Tomasz Napierala 				continue;
2081ba3a2d31SEdward Tomasz Napierala 			}
2082ba3a2d31SEdward Tomasz Napierala #endif
2083ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2084009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
2085009ea47eSEdward Tomasz Napierala 
2086009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
2087009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
2088009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
2089009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
2090009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
2091009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
2092009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
2093009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2094009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2095009ea47eSEdward Tomasz Napierala 				continue;
2096009ea47eSEdward Tomasz Napierala 			}
20978b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
20988b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
20998b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
21008b94b583SAlexander Motin 				log_warn("setsockopt(SO_RCVBUF) failed "
21018b94b583SAlexander Motin 				    "for %s", newp->p_listen);
21028b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
21038b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
21048b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
21058b94b583SAlexander Motin 				log_warn("setsockopt(SO_SNDBUF) failed "
21068b94b583SAlexander Motin 				    "for %s", newp->p_listen);
2107009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
2108009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
2109009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2110009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
2111009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
2112009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2113009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2114009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2115009ea47eSEdward Tomasz Napierala 				continue;
2116009ea47eSEdward Tomasz Napierala 			}
2117009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
2118009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
2119009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2120009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
2121009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2122009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2123009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2124009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2125009ea47eSEdward Tomasz Napierala 				continue;
2126009ea47eSEdward Tomasz Napierala 			}
2127009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
2128009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2129009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
2130009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2131009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2132009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2133009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2134009ea47eSEdward Tomasz Napierala 				continue;
2135009ea47eSEdward Tomasz Napierala 			}
2136009ea47eSEdward Tomasz Napierala 		}
2137009ea47eSEdward Tomasz Napierala 	}
2138009ea47eSEdward Tomasz Napierala 
2139009ea47eSEdward Tomasz Napierala 	/*
2140009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
2141009ea47eSEdward Tomasz Napierala 	 */
2142009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2143009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2144009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
2145009ea47eSEdward Tomasz Napierala 				continue;
2146009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
2147009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
2148009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
2149009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
2150009ea47eSEdward Tomasz Napierala 		}
2151009ea47eSEdward Tomasz Napierala 	}
2152009ea47eSEdward Tomasz Napierala 
2153829603e2SAlexander Motin 	/* (Re-)Register on remaining/new iSNS servers. */
2154829603e2SAlexander Motin 	TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2155829603e2SAlexander Motin 		TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2156829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2157829603e2SAlexander Motin 				break;
2158829603e2SAlexander Motin 		}
2159829603e2SAlexander Motin 		isns_register(newns, oldns);
2160829603e2SAlexander Motin 	}
2161829603e2SAlexander Motin 
2162829603e2SAlexander Motin 	/* Schedule iSNS update */
2163829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2164829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2165829603e2SAlexander Motin 
2166009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
2167009ea47eSEdward Tomasz Napierala }
2168009ea47eSEdward Tomasz Napierala 
2169009ea47eSEdward Tomasz Napierala bool
2170009ea47eSEdward Tomasz Napierala timed_out(void)
2171009ea47eSEdward Tomasz Napierala {
2172009ea47eSEdward Tomasz Napierala 
2173009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
2174009ea47eSEdward Tomasz Napierala }
2175009ea47eSEdward Tomasz Napierala 
2176009ea47eSEdward Tomasz Napierala static void
2177829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused)
2178009ea47eSEdward Tomasz Napierala {
2179009ea47eSEdward Tomasz Napierala 	/*
2180009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
2181009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
2182009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
2183009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
2184009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
2185009ea47eSEdward Tomasz Napierala 	 */
2186009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
2187009ea47eSEdward Tomasz Napierala 		/*
2188009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
2189009ea47eSEdward Tomasz Napierala 		 */
2190009ea47eSEdward Tomasz Napierala 		_exit(2);
2191009ea47eSEdward Tomasz Napierala 	}
2192009ea47eSEdward Tomasz Napierala 
2193009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
2194009ea47eSEdward Tomasz Napierala }
2195009ea47eSEdward Tomasz Napierala 
2196009ea47eSEdward Tomasz Napierala static void
2197829603e2SAlexander Motin sigalrm_handler(int dummy __unused)
2198829603e2SAlexander Motin {
2199829603e2SAlexander Motin 
2200829603e2SAlexander Motin 	sigalrm_received = true;
2201829603e2SAlexander Motin }
2202829603e2SAlexander Motin 
2203829603e2SAlexander Motin void
2204829603e2SAlexander Motin set_timeout(int timeout, int fatal)
2205009ea47eSEdward Tomasz Napierala {
2206009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2207009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
2208009ea47eSEdward Tomasz Napierala 	int error;
2209009ea47eSEdward Tomasz Napierala 
2210829603e2SAlexander Motin 	if (timeout <= 0) {
2211009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
2212829603e2SAlexander Motin 		bzero(&itv, sizeof(itv));
2213829603e2SAlexander Motin 		error = setitimer(ITIMER_REAL, &itv, NULL);
2214829603e2SAlexander Motin 		if (error != 0)
2215829603e2SAlexander Motin 			log_err(1, "setitimer");
2216829603e2SAlexander Motin 		sigalrm_received = false;
2217009ea47eSEdward Tomasz Napierala 		return;
2218009ea47eSEdward Tomasz Napierala 	}
2219009ea47eSEdward Tomasz Napierala 
2220829603e2SAlexander Motin 	sigalrm_received = false;
2221009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2222829603e2SAlexander Motin 	if (fatal)
2223829603e2SAlexander Motin 		sa.sa_handler = sigalrm_handler_fatal;
2224829603e2SAlexander Motin 	else
2225009ea47eSEdward Tomasz Napierala 		sa.sa_handler = sigalrm_handler;
2226009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2227009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
2228009ea47eSEdward Tomasz Napierala 	if (error != 0)
2229009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2230009ea47eSEdward Tomasz Napierala 
2231009ea47eSEdward Tomasz Napierala 	/*
2232009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
2233009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
2234009ea47eSEdward Tomasz Napierala 	 */
2235829603e2SAlexander Motin 	log_debugx("setting session timeout to %d seconds", timeout);
2236009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
2237009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
2238829603e2SAlexander Motin 	itv.it_value.tv_sec = timeout;
2239009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
2240009ea47eSEdward Tomasz Napierala 	if (error != 0)
2241009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
2242009ea47eSEdward Tomasz Napierala }
2243009ea47eSEdward Tomasz Napierala 
2244009ea47eSEdward Tomasz Napierala static int
2245009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
2246009ea47eSEdward Tomasz Napierala {
2247009ea47eSEdward Tomasz Napierala 	pid_t pid;
2248009ea47eSEdward Tomasz Napierala 	int status;
2249009ea47eSEdward Tomasz Napierala 	int num = 0;
2250009ea47eSEdward Tomasz Napierala 
2251009ea47eSEdward Tomasz Napierala 	for (;;) {
2252009ea47eSEdward Tomasz Napierala 		/*
2253009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
2254009ea47eSEdward Tomasz Napierala 		 */
2255009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
2256009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
2257009ea47eSEdward Tomasz Napierala 		else
2258009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
2259009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
2260009ea47eSEdward Tomasz Napierala 			break;
2261009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
2262009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
2263009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
2264009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
2265009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
2266009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
2267009ea47eSEdward Tomasz Napierala 		} else {
2268009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
2269009ea47eSEdward Tomasz Napierala 		}
2270009ea47eSEdward Tomasz Napierala 		num++;
2271009ea47eSEdward Tomasz Napierala 	}
2272009ea47eSEdward Tomasz Napierala 
2273009ea47eSEdward Tomasz Napierala 	return (num);
2274009ea47eSEdward Tomasz Napierala }
2275009ea47eSEdward Tomasz Napierala 
2276009ea47eSEdward Tomasz Napierala static void
22778eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd,
2278073edb1cSAlexander Motin     const struct sockaddr *client_sa, bool dont_fork)
2279009ea47eSEdward Tomasz Napierala {
2280009ea47eSEdward Tomasz Napierala 	struct connection *conn;
2281009ea47eSEdward Tomasz Napierala 	int error;
2282009ea47eSEdward Tomasz Napierala 	pid_t pid;
2283009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
2284009ea47eSEdward Tomasz Napierala 	struct conf *conf;
2285009ea47eSEdward Tomasz Napierala 
2286009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
2287009ea47eSEdward Tomasz Napierala 
2288009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
2289009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
2290009ea47eSEdward Tomasz Napierala 	} else {
2291009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
2292009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
2293009ea47eSEdward Tomasz Napierala 
2294009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2295009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
2296009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
2297009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
2298009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2299009ea47eSEdward Tomasz Napierala 		}
2300009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
2301009ea47eSEdward Tomasz Napierala 		    nchildren);
2302009ea47eSEdward Tomasz Napierala 		nchildren++;
2303009ea47eSEdward Tomasz Napierala 		pid = fork();
2304009ea47eSEdward Tomasz Napierala 		if (pid < 0)
2305009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
2306009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
2307009ea47eSEdward Tomasz Napierala 			close(fd);
2308009ea47eSEdward Tomasz Napierala 			return;
2309009ea47eSEdward Tomasz Napierala 		}
2310009ea47eSEdward Tomasz Napierala 	}
2311009ea47eSEdward Tomasz Napierala 	pidfile_close(conf->conf_pidfh);
2312009ea47eSEdward Tomasz Napierala 
2313073edb1cSAlexander Motin 	error = getnameinfo(client_sa, client_sa->sa_len,
2314009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2315009ea47eSEdward Tomasz Napierala 	if (error != 0)
23169a95cfffSEdward Tomasz Napierala 		log_errx(1, "getnameinfo: %s", gai_strerror(error));
2317009ea47eSEdward Tomasz Napierala 
2318009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
2319009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
2320009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
2321009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
2322009ea47eSEdward Tomasz Napierala 
2323073edb1cSAlexander Motin 	conn = connection_new(portal, fd, host, client_sa);
2324829603e2SAlexander Motin 	set_timeout(conf->conf_timeout, true);
2325009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
2326009ea47eSEdward Tomasz Napierala 	login(conn);
2327009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2328009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
2329009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
2330009ea47eSEdward Tomasz Napierala 	} else {
2331009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2332009ea47eSEdward Tomasz Napierala 		discovery(conn);
2333009ea47eSEdward Tomasz Napierala 	}
2334009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
2335009ea47eSEdward Tomasz Napierala 	exit(0);
2336009ea47eSEdward Tomasz Napierala }
2337009ea47eSEdward Tomasz Napierala 
2338009ea47eSEdward Tomasz Napierala static int
2339009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
2340009ea47eSEdward Tomasz Napierala {
2341009ea47eSEdward Tomasz Napierala 
2342009ea47eSEdward Tomasz Napierala 	/*
2343009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
2344009ea47eSEdward Tomasz Napierala 	 */
2345009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
2346009ea47eSEdward Tomasz Napierala 		return (nfds);
2347009ea47eSEdward Tomasz Napierala 
2348009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
2349009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
2350009ea47eSEdward Tomasz Napierala 		nfds = fd;
2351009ea47eSEdward Tomasz Napierala 	return (nfds);
2352009ea47eSEdward Tomasz Napierala }
2353009ea47eSEdward Tomasz Napierala 
2354009ea47eSEdward Tomasz Napierala static void
2355009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
2356009ea47eSEdward Tomasz Napierala {
2357009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
2358009ea47eSEdward Tomasz Napierala 	struct portal *portal;
23599a95cfffSEdward Tomasz Napierala 	struct sockaddr_storage client_sa;
23609a95cfffSEdward Tomasz Napierala 	socklen_t client_salen;
2361009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2362009ea47eSEdward Tomasz Napierala 	int connection_id;
23638cab2ed4SEdward Tomasz Napierala 	int portal_id;
2364ba3a2d31SEdward Tomasz Napierala #endif
2365009ea47eSEdward Tomasz Napierala 	fd_set fdset;
2366009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
2367009ea47eSEdward Tomasz Napierala 
2368009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
2369009ea47eSEdward Tomasz Napierala 
2370009ea47eSEdward Tomasz Napierala 	for (;;) {
2371829603e2SAlexander Motin 		if (sighup_received || sigterm_received || timed_out())
2372009ea47eSEdward Tomasz Napierala 			return;
2373009ea47eSEdward Tomasz Napierala 
2374009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2375ba3a2d31SEdward Tomasz Napierala 		if (proxy_mode) {
23768eab95d6SEdward Tomasz Napierala 			client_salen = sizeof(client_sa);
23778eab95d6SEdward Tomasz Napierala 			kernel_accept(&connection_id, &portal_id,
23788eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, &client_salen);
23790488e848SEdward Tomasz Napierala 			assert(client_salen >= client_sa.ss_len);
2380009ea47eSEdward Tomasz Napierala 
23818cab2ed4SEdward Tomasz Napierala 			log_debugx("incoming connection, id %d, portal id %d",
23828cab2ed4SEdward Tomasz Napierala 			    connection_id, portal_id);
23838cab2ed4SEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
23848cab2ed4SEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
23858cab2ed4SEdward Tomasz Napierala 					if (portal->p_id == portal_id) {
23868cab2ed4SEdward Tomasz Napierala 						goto found;
23878cab2ed4SEdward Tomasz Napierala 					}
23888cab2ed4SEdward Tomasz Napierala 				}
23898cab2ed4SEdward Tomasz Napierala 			}
2390009ea47eSEdward Tomasz Napierala 
23918cab2ed4SEdward Tomasz Napierala 			log_errx(1, "kernel returned invalid portal_id %d",
23928cab2ed4SEdward Tomasz Napierala 			    portal_id);
23938cab2ed4SEdward Tomasz Napierala 
23948cab2ed4SEdward Tomasz Napierala found:
23958eab95d6SEdward Tomasz Napierala 			handle_connection(portal, connection_id,
2396073edb1cSAlexander Motin 			    (struct sockaddr *)&client_sa, dont_fork);
2397ba3a2d31SEdward Tomasz Napierala 		} else {
2398ba3a2d31SEdward Tomasz Napierala #endif
2399ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2400ba3a2d31SEdward Tomasz Napierala 
2401009ea47eSEdward Tomasz Napierala 			FD_ZERO(&fdset);
2402009ea47eSEdward Tomasz Napierala 			nfds = 0;
2403009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2404009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2405009ea47eSEdward Tomasz Napierala 					nfds = fd_add(portal->p_socket, &fdset, nfds);
2406009ea47eSEdward Tomasz Napierala 			}
2407009ea47eSEdward Tomasz Napierala 			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2408009ea47eSEdward Tomasz Napierala 			if (error <= 0) {
2409009ea47eSEdward Tomasz Napierala 				if (errno == EINTR)
2410009ea47eSEdward Tomasz Napierala 					return;
2411009ea47eSEdward Tomasz Napierala 				log_err(1, "select");
2412009ea47eSEdward Tomasz Napierala 			}
2413009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2414009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2415009ea47eSEdward Tomasz Napierala 					if (!FD_ISSET(portal->p_socket, &fdset))
2416009ea47eSEdward Tomasz Napierala 						continue;
24179a95cfffSEdward Tomasz Napierala 					client_salen = sizeof(client_sa);
24189a95cfffSEdward Tomasz Napierala 					client_fd = accept(portal->p_socket,
24199a95cfffSEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
24209a95cfffSEdward Tomasz Napierala 					    &client_salen);
242183dd77c3SAlexander Motin 					if (client_fd < 0) {
242283dd77c3SAlexander Motin 						if (errno == ECONNABORTED)
242383dd77c3SAlexander Motin 							continue;
2424009ea47eSEdward Tomasz Napierala 						log_err(1, "accept");
242583dd77c3SAlexander Motin 					}
24260488e848SEdward Tomasz Napierala 					assert(client_salen >= client_sa.ss_len);
24270488e848SEdward Tomasz Napierala 
24289a95cfffSEdward Tomasz Napierala 					handle_connection(portal, client_fd,
24298eab95d6SEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
2430073edb1cSAlexander Motin 					    dont_fork);
2431009ea47eSEdward Tomasz Napierala 					break;
2432009ea47eSEdward Tomasz Napierala 				}
2433009ea47eSEdward Tomasz Napierala 			}
2434ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2435ba3a2d31SEdward Tomasz Napierala 		}
2436ba3a2d31SEdward Tomasz Napierala #endif
2437009ea47eSEdward Tomasz Napierala 	}
2438009ea47eSEdward Tomasz Napierala }
2439009ea47eSEdward Tomasz Napierala 
2440009ea47eSEdward Tomasz Napierala static void
2441009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
2442009ea47eSEdward Tomasz Napierala {
2443009ea47eSEdward Tomasz Napierala 
2444009ea47eSEdward Tomasz Napierala 	sighup_received = true;
2445009ea47eSEdward Tomasz Napierala }
2446009ea47eSEdward Tomasz Napierala 
2447009ea47eSEdward Tomasz Napierala static void
2448009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
2449009ea47eSEdward Tomasz Napierala {
2450009ea47eSEdward Tomasz Napierala 
2451009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
2452009ea47eSEdward Tomasz Napierala }
2453009ea47eSEdward Tomasz Napierala 
2454009ea47eSEdward Tomasz Napierala static void
245570b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused)
245670b939ecSEdward Tomasz Napierala {
245770b939ecSEdward Tomasz Napierala 
245870b939ecSEdward Tomasz Napierala 	/*
245970b939ecSEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
246070b939ecSEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
246170b939ecSEdward Tomasz Napierala 	 * wait_for_children().
246270b939ecSEdward Tomasz Napierala 	 */
246370b939ecSEdward Tomasz Napierala }
246470b939ecSEdward Tomasz Napierala 
246570b939ecSEdward Tomasz Napierala static void
2466009ea47eSEdward Tomasz Napierala register_signals(void)
2467009ea47eSEdward Tomasz Napierala {
2468009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2469009ea47eSEdward Tomasz Napierala 	int error;
2470009ea47eSEdward Tomasz Napierala 
2471009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2472009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
2473009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2474009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
2475009ea47eSEdward Tomasz Napierala 	if (error != 0)
2476009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2477009ea47eSEdward Tomasz Napierala 
2478009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2479009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
2480009ea47eSEdward Tomasz Napierala 	if (error != 0)
2481009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2482009ea47eSEdward Tomasz Napierala 
2483009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2484009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
2485009ea47eSEdward Tomasz Napierala 	if (error != 0)
2486009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
248770b939ecSEdward Tomasz Napierala 
248870b939ecSEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
248970b939ecSEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
249070b939ecSEdward Tomasz Napierala 	if (error != 0)
249170b939ecSEdward Tomasz Napierala 		log_err(1, "sigaction");
2492009ea47eSEdward Tomasz Napierala }
2493009ea47eSEdward Tomasz Napierala 
2494009ea47eSEdward Tomasz Napierala int
2495009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
2496009ea47eSEdward Tomasz Napierala {
2497009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
2498829603e2SAlexander Motin 	struct isns *newns;
2499009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
2500009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
2501009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
2502009ea47eSEdward Tomasz Napierala 
2503ba3a2d31SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "df:R")) != -1) {
2504009ea47eSEdward Tomasz Napierala 		switch (ch) {
2505009ea47eSEdward Tomasz Napierala 		case 'd':
2506009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
2507009ea47eSEdward Tomasz Napierala 			debug++;
2508009ea47eSEdward Tomasz Napierala 			break;
2509009ea47eSEdward Tomasz Napierala 		case 'f':
2510009ea47eSEdward Tomasz Napierala 			config_path = optarg;
2511009ea47eSEdward Tomasz Napierala 			break;
2512ba3a2d31SEdward Tomasz Napierala 		case 'R':
2513ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
2514ba3a2d31SEdward Tomasz Napierala 			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2515ba3a2d31SEdward Tomasz Napierala 			    "does not support iSER protocol");
2516ba3a2d31SEdward Tomasz Napierala #endif
2517ba3a2d31SEdward Tomasz Napierala 			proxy_mode = true;
2518ba3a2d31SEdward Tomasz Napierala 			break;
2519009ea47eSEdward Tomasz Napierala 		case '?':
2520009ea47eSEdward Tomasz Napierala 		default:
2521009ea47eSEdward Tomasz Napierala 			usage();
2522009ea47eSEdward Tomasz Napierala 		}
2523009ea47eSEdward Tomasz Napierala 	}
2524009ea47eSEdward Tomasz Napierala 	argc -= optind;
2525009ea47eSEdward Tomasz Napierala 	if (argc != 0)
2526009ea47eSEdward Tomasz Napierala 		usage();
2527009ea47eSEdward Tomasz Napierala 
2528009ea47eSEdward Tomasz Napierala 	log_init(debug);
2529009ea47eSEdward Tomasz Napierala 	kernel_init();
2530009ea47eSEdward Tomasz Napierala 
2531009ea47eSEdward Tomasz Napierala 	oldconf = conf_new_from_kernel();
2532057abcb0SAlexander Motin 	newconf = conf_new_from_file(config_path, oldconf);
2533009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
2534fe845cdbSEdward Tomasz Napierala 		log_errx(1, "configuration error; exiting");
2535009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
2536009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
2537009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
2538009ea47eSEdward Tomasz Napierala 	}
2539009ea47eSEdward Tomasz Napierala 
2540009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
2541009ea47eSEdward Tomasz Napierala 	if (error != 0)
2542fe845cdbSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration; exiting");
2543fe845cdbSEdward Tomasz Napierala 
2544009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
2545009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
2546009ea47eSEdward Tomasz Napierala 
2547009ea47eSEdward Tomasz Napierala 	register_signals();
2548009ea47eSEdward Tomasz Napierala 
25495d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
25505d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
25515d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
25525d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
25535d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
25545d5a95f1SEdward Tomasz Napierala 			exit(1);
25555d5a95f1SEdward Tomasz Napierala 		}
25565d5a95f1SEdward Tomasz Napierala 	}
25575d5a95f1SEdward Tomasz Napierala 
2558829603e2SAlexander Motin 	/* Schedule iSNS update */
2559829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2560829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2561829603e2SAlexander Motin 
2562009ea47eSEdward Tomasz Napierala 	for (;;) {
2563009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
2564009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
2565009ea47eSEdward Tomasz Napierala 			sighup_received = false;
2566009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
2567057abcb0SAlexander Motin 			tmpconf = conf_new_from_file(config_path, newconf);
2568009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
2569009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
2570009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
2571009ea47eSEdward Tomasz Napierala 			} else {
2572009ea47eSEdward Tomasz Napierala 				if (debug > 0)
2573009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
2574009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
2575009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
2576009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
2577009ea47eSEdward Tomasz Napierala 				if (error != 0)
2578009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
2579009ea47eSEdward Tomasz Napierala 					    "configuration");
2580009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
2581009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
2582009ea47eSEdward Tomasz Napierala 			}
2583009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
2584009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
2585009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
2586009ea47eSEdward Tomasz Napierala 
25876b31e130SAlexander Motin 			log_debugx("removing CTL iSCSI ports "
2588009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
2589009ea47eSEdward Tomasz Napierala 
2590009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
2591009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
2592009ea47eSEdward Tomasz Napierala 			if (debug > 0)
2593009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
2594009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
2595009ea47eSEdward Tomasz Napierala 			if (error != 0)
2596009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
2597829603e2SAlexander Motin 			conf_delete(oldconf);
2598829603e2SAlexander Motin 			oldconf = NULL;
2599009ea47eSEdward Tomasz Napierala 
2600009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
2601009ea47eSEdward Tomasz Napierala 			exit(0);
2602009ea47eSEdward Tomasz Napierala 		} else {
2603009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
2604009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2605829603e2SAlexander Motin 			if (timed_out()) {
2606829603e2SAlexander Motin 				set_timeout(0, false);
2607829603e2SAlexander Motin 				TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2608829603e2SAlexander Motin 					isns_check(newns);
2609829603e2SAlexander Motin 				/* Schedule iSNS update */
2610829603e2SAlexander Motin 				if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2611829603e2SAlexander Motin 					set_timeout((newconf->conf_isns_period
2612829603e2SAlexander Motin 					    + 2) / 3,
2613829603e2SAlexander Motin 					    false);
2614829603e2SAlexander Motin 				}
2615829603e2SAlexander Motin 			}
2616009ea47eSEdward Tomasz Napierala 		}
2617009ea47eSEdward Tomasz Napierala 	}
2618009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
2619009ea47eSEdward Tomasz Napierala }
2620