xref: /freebsd/usr.sbin/ctld/ctld.c (revision 2909ddd17cb4d750852dc04128e584f93f8c5058)
1009ea47eSEdward Tomasz Napierala /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
5009ea47eSEdward Tomasz Napierala  *
6009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
7009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
8009ea47eSEdward Tomasz Napierala  *
9009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
10009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
11009ea47eSEdward Tomasz Napierala  * are met:
12009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
13009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
14009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
15009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
16009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
17009ea47eSEdward Tomasz Napierala  *
18009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
29009ea47eSEdward Tomasz Napierala  *
30009ea47eSEdward Tomasz Napierala  */
31009ea47eSEdward Tomasz Napierala 
32009ea47eSEdward Tomasz Napierala #include <sys/types.h>
33009ea47eSEdward Tomasz Napierala #include <sys/time.h>
34009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
355c734b04SJakub Wojciech Klama #include <sys/stat.h>
36009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
37009ea47eSEdward Tomasz Napierala #include <netinet/in.h>
38073edb1cSAlexander Motin #include <arpa/inet.h>
39009ea47eSEdward Tomasz Napierala #include <assert.h>
40009ea47eSEdward Tomasz Napierala #include <ctype.h>
41009ea47eSEdward Tomasz Napierala #include <errno.h>
42009ea47eSEdward Tomasz Napierala #include <netdb.h>
43009ea47eSEdward Tomasz Napierala #include <signal.h>
44009ea47eSEdward Tomasz Napierala #include <stdbool.h>
45009ea47eSEdward Tomasz Napierala #include <stdio.h>
46009ea47eSEdward Tomasz Napierala #include <stdint.h>
47009ea47eSEdward Tomasz Napierala #include <stdlib.h>
48009ea47eSEdward Tomasz Napierala #include <string.h>
49009ea47eSEdward Tomasz Napierala #include <unistd.h>
50009ea47eSEdward Tomasz Napierala 
51009ea47eSEdward Tomasz Napierala #include "ctld.h"
52829603e2SAlexander Motin #include "isns.h"
53009ea47eSEdward Tomasz Napierala 
5463783933SJohn Baldwin static bool	timed_out(void);
5563783933SJohn Baldwin #ifdef ICL_KERNEL_PROXY
5663783933SJohn Baldwin static void	pdu_receive_proxy(struct pdu *pdu);
5763783933SJohn Baldwin static void	pdu_send_proxy(struct pdu *pdu);
5863783933SJohn Baldwin #endif /* ICL_KERNEL_PROXY */
5963783933SJohn Baldwin static void	pdu_fail(const struct connection *conn, const char *reason);
6063783933SJohn Baldwin 
61ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false;
62ba3a2d31SEdward Tomasz Napierala 
63009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false;
64009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false;
65009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
66009ea47eSEdward Tomasz Napierala 
67009ea47eSEdward Tomasz Napierala static int nchildren = 0;
68db7bf2baSAlexander Motin static uint16_t last_portal_group_tag = 0xff;
69009ea47eSEdward Tomasz Napierala 
7063783933SJohn Baldwin static struct connection_ops conn_ops = {
7163783933SJohn Baldwin 	.timed_out = timed_out,
7263783933SJohn Baldwin #ifdef ICL_KERNEL_PROXY
7363783933SJohn Baldwin 	.pdu_receive_proxy = pdu_receive_proxy,
7463783933SJohn Baldwin 	.pdu_send_proxy = pdu_send_proxy,
7563783933SJohn Baldwin #endif
7663783933SJohn Baldwin 	.fail = pdu_fail,
7763783933SJohn Baldwin };
7863783933SJohn Baldwin 
79009ea47eSEdward Tomasz Napierala static void
80009ea47eSEdward Tomasz Napierala usage(void)
81009ea47eSEdward Tomasz Napierala {
82009ea47eSEdward Tomasz Napierala 
83e321146fSEdward Tomasz Napierala 	fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n");
847fcbecd0SEdward Tomasz Napierala 	fprintf(stderr, "       ctld -t [-u][-f config-file]\n");
85009ea47eSEdward Tomasz Napierala 	exit(1);
86009ea47eSEdward Tomasz Napierala }
87009ea47eSEdward Tomasz Napierala 
88009ea47eSEdward Tomasz Napierala struct conf *
89009ea47eSEdward Tomasz Napierala conf_new(void)
90009ea47eSEdward Tomasz Napierala {
91009ea47eSEdward Tomasz Napierala 	struct conf *conf;
92009ea47eSEdward Tomasz Napierala 
93009ea47eSEdward Tomasz Napierala 	conf = calloc(1, sizeof(*conf));
94009ea47eSEdward Tomasz Napierala 	if (conf == NULL)
95009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
96920c6cbaSAlexander Motin 	TAILQ_INIT(&conf->conf_luns);
97009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_targets);
98009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_auth_groups);
9992847ee1SAlexander Motin 	TAILQ_INIT(&conf->conf_ports);
100009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_portal_groups);
101057abcb0SAlexander Motin 	TAILQ_INIT(&conf->conf_pports);
102829603e2SAlexander Motin 	TAILQ_INIT(&conf->conf_isns);
103009ea47eSEdward Tomasz Napierala 
104829603e2SAlexander Motin 	conf->conf_isns_period = 900;
105829603e2SAlexander Motin 	conf->conf_isns_timeout = 5;
106009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
107009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
108009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
109009ea47eSEdward Tomasz Napierala 
110009ea47eSEdward Tomasz Napierala 	return (conf);
111009ea47eSEdward Tomasz Napierala }
112009ea47eSEdward Tomasz Napierala 
113009ea47eSEdward Tomasz Napierala void
114009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
115009ea47eSEdward Tomasz Napierala {
116920c6cbaSAlexander Motin 	struct lun *lun, *ltmp;
117009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
118009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
119009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
120057abcb0SAlexander Motin 	struct pport *pp, *pptmp;
121829603e2SAlexander Motin 	struct isns *is, *istmp;
122009ea47eSEdward Tomasz Napierala 
123009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
124009ea47eSEdward Tomasz Napierala 
125920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
126920c6cbaSAlexander Motin 		lun_delete(lun);
127009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
128009ea47eSEdward Tomasz Napierala 		target_delete(targ);
129009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
130009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
131009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
132009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
133057abcb0SAlexander Motin 	TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
134057abcb0SAlexander Motin 		pport_delete(pp);
135829603e2SAlexander Motin 	TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
136829603e2SAlexander Motin 		isns_delete(is);
13792847ee1SAlexander Motin 	assert(TAILQ_EMPTY(&conf->conf_ports));
138009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
139009ea47eSEdward Tomasz Napierala 	free(conf);
140009ea47eSEdward Tomasz Napierala }
141009ea47eSEdward Tomasz Napierala 
142009ea47eSEdward Tomasz Napierala static struct auth *
143009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
144009ea47eSEdward Tomasz Napierala {
145009ea47eSEdward Tomasz Napierala 	struct auth *auth;
146009ea47eSEdward Tomasz Napierala 
147009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
148009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
149009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
150009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
151009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
152009ea47eSEdward Tomasz Napierala 	return (auth);
153009ea47eSEdward Tomasz Napierala }
154009ea47eSEdward Tomasz Napierala 
155009ea47eSEdward Tomasz Napierala static void
156009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
157009ea47eSEdward Tomasz Napierala {
158009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
159009ea47eSEdward Tomasz Napierala 
160009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
161009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
162009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
163009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
164009ea47eSEdward Tomasz Napierala 	free(auth);
165009ea47eSEdward Tomasz Napierala }
166009ea47eSEdward Tomasz Napierala 
167009ea47eSEdward Tomasz Napierala const struct auth *
168d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user)
169009ea47eSEdward Tomasz Napierala {
170009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
171009ea47eSEdward Tomasz Napierala 
172009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
173009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
174009ea47eSEdward Tomasz Napierala 			return (auth);
175009ea47eSEdward Tomasz Napierala 	}
176009ea47eSEdward Tomasz Napierala 
177009ea47eSEdward Tomasz Napierala 	return (NULL);
178009ea47eSEdward Tomasz Napierala }
179009ea47eSEdward Tomasz Napierala 
1801658628dSEdward Tomasz Napierala static void
1811658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1821658628dSEdward Tomasz Napierala {
1831658628dSEdward Tomasz Napierala 	size_t len;
1841658628dSEdward Tomasz Napierala 
1851658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1861658628dSEdward Tomasz Napierala 	if (len > 16) {
1871658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1881658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1891658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1901658628dSEdward Tomasz Napierala 			    "long", auth->a_user, auth->a_auth_group->ag_name);
1911658628dSEdward Tomasz Napierala 		else
1921658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1931658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1941658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
195f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1961658628dSEdward Tomasz Napierala 	}
1971658628dSEdward Tomasz Napierala 	if (len < 12) {
1981658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1991658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
2001658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
2011658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
2021658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
2031658628dSEdward Tomasz Napierala 		else
2041658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
20542f97facSAlexander Motin 			    "is too short; it should be at least 12 characters "
2061658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
207f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
2081658628dSEdward Tomasz Napierala 	}
2091658628dSEdward Tomasz Napierala 
2101658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
2117c924aaeSJosh Paetzel 		len = strlen(auth->a_mutual_secret);
2121658628dSEdward Tomasz Napierala 		if (len > 16) {
2131658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2141658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2151658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
2161658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2171658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2181658628dSEdward Tomasz Napierala 			else
2191658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2201658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
2211658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2221658628dSEdward Tomasz Napierala 				    auth->a_user,
223f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2241658628dSEdward Tomasz Napierala 		}
2251658628dSEdward Tomasz Napierala 		if (len < 12) {
2261658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2271658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2281658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2291658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2301658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2311658628dSEdward Tomasz Napierala 			else
2321658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2331658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2342668ec07SAlexander Motin 				    "at least 12 characters long",
2351658628dSEdward Tomasz Napierala 				    auth->a_user,
236f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2371658628dSEdward Tomasz Napierala 		}
2381658628dSEdward Tomasz Napierala 	}
2391658628dSEdward Tomasz Napierala }
2401658628dSEdward Tomasz Napierala 
2411658628dSEdward Tomasz Napierala const struct auth *
2421658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2431658628dSEdward Tomasz Napierala     const char *secret)
2441658628dSEdward Tomasz Napierala {
2451658628dSEdward Tomasz Napierala 	struct auth *auth;
2461658628dSEdward Tomasz Napierala 
2471658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2481658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2491658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2501658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2511658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2521658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2531658628dSEdward Tomasz Napierala 		else
2541658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2551658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
256f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2571658628dSEdward Tomasz Napierala 		return (NULL);
2581658628dSEdward Tomasz Napierala 	}
2591658628dSEdward Tomasz Napierala 
2601658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2611658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2621658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2631658628dSEdward Tomasz Napierala 
2641658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2651658628dSEdward Tomasz Napierala 
2661658628dSEdward Tomasz Napierala 	return (auth);
2671658628dSEdward Tomasz Napierala }
2681658628dSEdward Tomasz Napierala 
2691658628dSEdward Tomasz Napierala const struct auth *
2701658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2711658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2721658628dSEdward Tomasz Napierala {
2731658628dSEdward Tomasz Napierala 	struct auth *auth;
2741658628dSEdward Tomasz Napierala 
2751658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2761658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2771658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2781658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2791658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2801658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2811658628dSEdward Tomasz Napierala 			    ag->ag_name);
2821658628dSEdward Tomasz Napierala 		else
2831658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2841658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
285f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2861658628dSEdward Tomasz Napierala 		return (NULL);
2871658628dSEdward Tomasz Napierala 	}
2881658628dSEdward Tomasz Napierala 
2891658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2901658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2911658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2921658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2931658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2941658628dSEdward Tomasz Napierala 
2951658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2961658628dSEdward Tomasz Napierala 
2971658628dSEdward Tomasz Napierala 	return (auth);
2981658628dSEdward Tomasz Napierala }
2991658628dSEdward Tomasz Napierala 
3008cb2e958SEdward Tomasz Napierala const struct auth_name *
3018cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
3028cb2e958SEdward Tomasz Napierala {
3038cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
3048cb2e958SEdward Tomasz Napierala 
3058cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
3068cb2e958SEdward Tomasz Napierala 	if (an == NULL)
3078cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3088cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
309bdbc2a6fSAlexander Motin 	an->an_initiator_name = checked_strdup(name);
3108cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
3118cb2e958SEdward Tomasz Napierala 	return (an);
3128cb2e958SEdward Tomasz Napierala }
3138cb2e958SEdward Tomasz Napierala 
3148cb2e958SEdward Tomasz Napierala static void
3158cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
3168cb2e958SEdward Tomasz Napierala {
3178cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
3188cb2e958SEdward Tomasz Napierala 
319bdbc2a6fSAlexander Motin 	free(an->an_initiator_name);
3208cb2e958SEdward Tomasz Napierala 	free(an);
3218cb2e958SEdward Tomasz Napierala }
3228cb2e958SEdward Tomasz Napierala 
3238cb2e958SEdward Tomasz Napierala bool
3248cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
3258cb2e958SEdward Tomasz Napierala {
3268cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
3278cb2e958SEdward Tomasz Napierala 		return (false);
3288cb2e958SEdward Tomasz Napierala 	return (true);
3298cb2e958SEdward Tomasz Napierala }
3308cb2e958SEdward Tomasz Napierala 
3318cb2e958SEdward Tomasz Napierala const struct auth_name *
3328cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3338cb2e958SEdward Tomasz Napierala {
3348cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3358cb2e958SEdward Tomasz Napierala 
3368cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
337bdbc2a6fSAlexander Motin 		if (strcmp(auth_name->an_initiator_name, name) == 0)
3388cb2e958SEdward Tomasz Napierala 			return (auth_name);
3398cb2e958SEdward Tomasz Napierala 	}
3408cb2e958SEdward Tomasz Napierala 
3418cb2e958SEdward Tomasz Napierala 	return (NULL);
3428cb2e958SEdward Tomasz Napierala }
3438cb2e958SEdward Tomasz Napierala 
344e867e162SEdward Tomasz Napierala int
345e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name)
346e867e162SEdward Tomasz Napierala {
347e867e162SEdward Tomasz Napierala 	if (!auth_name_defined(ag))
348e867e162SEdward Tomasz Napierala 		return (0);
349e867e162SEdward Tomasz Napierala 
350e867e162SEdward Tomasz Napierala 	if (auth_name_find(ag, initiator_name) == NULL)
351e867e162SEdward Tomasz Napierala 		return (1);
352e867e162SEdward Tomasz Napierala 
353e867e162SEdward Tomasz Napierala 	return (0);
354e867e162SEdward Tomasz Napierala }
355e867e162SEdward Tomasz Napierala 
3568cb2e958SEdward Tomasz Napierala const struct auth_portal *
3578cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3588cb2e958SEdward Tomasz Napierala {
3598cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
360073edb1cSAlexander Motin 	char *net, *mask, *str, *tmp;
361073edb1cSAlexander Motin 	int len, dm, m;
3628cb2e958SEdward Tomasz Napierala 
3638cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3648cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3658cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3668cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
367a3434cdcSAlexander Motin 	ap->ap_initiator_portal = checked_strdup(portal);
368073edb1cSAlexander Motin 	mask = str = checked_strdup(portal);
369073edb1cSAlexander Motin 	net = strsep(&mask, "/");
370073edb1cSAlexander Motin 	if (net[0] == '[')
371073edb1cSAlexander Motin 		net++;
372073edb1cSAlexander Motin 	len = strlen(net);
373073edb1cSAlexander Motin 	if (len == 0)
374073edb1cSAlexander Motin 		goto error;
375073edb1cSAlexander Motin 	if (net[len - 1] == ']')
376073edb1cSAlexander Motin 		net[len - 1] = 0;
377073edb1cSAlexander Motin 	if (strchr(net, ':') != NULL) {
378073edb1cSAlexander Motin 		struct sockaddr_in6 *sin6 =
379073edb1cSAlexander Motin 		    (struct sockaddr_in6 *)&ap->ap_sa;
380073edb1cSAlexander Motin 
381073edb1cSAlexander Motin 		sin6->sin6_len = sizeof(*sin6);
382073edb1cSAlexander Motin 		sin6->sin6_family = AF_INET6;
383073edb1cSAlexander Motin 		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
384073edb1cSAlexander Motin 			goto error;
385073edb1cSAlexander Motin 		dm = 128;
386073edb1cSAlexander Motin 	} else {
387073edb1cSAlexander Motin 		struct sockaddr_in *sin =
388073edb1cSAlexander Motin 		    (struct sockaddr_in *)&ap->ap_sa;
389073edb1cSAlexander Motin 
390073edb1cSAlexander Motin 		sin->sin_len = sizeof(*sin);
391073edb1cSAlexander Motin 		sin->sin_family = AF_INET;
392073edb1cSAlexander Motin 		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
393073edb1cSAlexander Motin 			goto error;
394073edb1cSAlexander Motin 		dm = 32;
395073edb1cSAlexander Motin 	}
396073edb1cSAlexander Motin 	if (mask != NULL) {
397073edb1cSAlexander Motin 		m = strtol(mask, &tmp, 0);
398073edb1cSAlexander Motin 		if (m < 0 || m > dm || tmp[0] != 0)
399073edb1cSAlexander Motin 			goto error;
400073edb1cSAlexander Motin 	} else
401073edb1cSAlexander Motin 		m = dm;
402073edb1cSAlexander Motin 	ap->ap_mask = m;
403073edb1cSAlexander Motin 	free(str);
4048cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
4058cb2e958SEdward Tomasz Napierala 	return (ap);
406073edb1cSAlexander Motin 
407073edb1cSAlexander Motin error:
4089a913ec2SXin LI 	free(str);
409678b6879SAlexander Motin 	free(ap);
410f9a20518SEdward Tomasz Napierala 	log_warnx("incorrect initiator portal \"%s\"", portal);
411073edb1cSAlexander Motin 	return (NULL);
4128cb2e958SEdward Tomasz Napierala }
4138cb2e958SEdward Tomasz Napierala 
4148cb2e958SEdward Tomasz Napierala static void
4158cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
4168cb2e958SEdward Tomasz Napierala {
4178cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
4188cb2e958SEdward Tomasz Napierala 
419a3434cdcSAlexander Motin 	free(ap->ap_initiator_portal);
4208cb2e958SEdward Tomasz Napierala 	free(ap);
4218cb2e958SEdward Tomasz Napierala }
4228cb2e958SEdward Tomasz Napierala 
4238cb2e958SEdward Tomasz Napierala bool
4248cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
4258cb2e958SEdward Tomasz Napierala {
4268cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
4278cb2e958SEdward Tomasz Napierala 		return (false);
4288cb2e958SEdward Tomasz Napierala 	return (true);
4298cb2e958SEdward Tomasz Napierala }
4308cb2e958SEdward Tomasz Napierala 
4318cb2e958SEdward Tomasz Napierala const struct auth_portal *
432073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
4338cb2e958SEdward Tomasz Napierala {
434073edb1cSAlexander Motin 	const struct auth_portal *ap;
4355e46a066SAlexander Motin 	const uint8_t *a, *b;
436073edb1cSAlexander Motin 	int i;
4375e46a066SAlexander Motin 	uint8_t bmask;
4388cb2e958SEdward Tomasz Napierala 
439073edb1cSAlexander Motin 	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
440073edb1cSAlexander Motin 		if (ap->ap_sa.ss_family != ss->ss_family)
441073edb1cSAlexander Motin 			continue;
442073edb1cSAlexander Motin 		if (ss->ss_family == AF_INET) {
4435e46a066SAlexander Motin 			a = (const uint8_t *)
4445e46a066SAlexander Motin 			    &((const struct sockaddr_in *)ss)->sin_addr;
4455e46a066SAlexander Motin 			b = (const uint8_t *)
4465e46a066SAlexander Motin 			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
447073edb1cSAlexander Motin 		} else {
4485e46a066SAlexander Motin 			a = (const uint8_t *)
4495e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
4505e46a066SAlexander Motin 			b = (const uint8_t *)
4515e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
452073edb1cSAlexander Motin 		}
453073edb1cSAlexander Motin 		for (i = 0; i < ap->ap_mask / 8; i++) {
454073edb1cSAlexander Motin 			if (a[i] != b[i])
455073edb1cSAlexander Motin 				goto next;
456073edb1cSAlexander Motin 		}
457073edb1cSAlexander Motin 		if (ap->ap_mask % 8) {
458073edb1cSAlexander Motin 			bmask = 0xff << (8 - (ap->ap_mask % 8));
459073edb1cSAlexander Motin 			if ((a[i] & bmask) != (b[i] & bmask))
460073edb1cSAlexander Motin 				goto next;
461073edb1cSAlexander Motin 		}
462073edb1cSAlexander Motin 		return (ap);
463073edb1cSAlexander Motin next:
464073edb1cSAlexander Motin 		;
4658cb2e958SEdward Tomasz Napierala 	}
4668cb2e958SEdward Tomasz Napierala 
4678cb2e958SEdward Tomasz Napierala 	return (NULL);
4688cb2e958SEdward Tomasz Napierala }
4698cb2e958SEdward Tomasz Napierala 
470e867e162SEdward Tomasz Napierala int
471e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
472e867e162SEdward Tomasz Napierala {
473e867e162SEdward Tomasz Napierala 
474e867e162SEdward Tomasz Napierala 	if (!auth_portal_defined(ag))
475e867e162SEdward Tomasz Napierala 		return (0);
476e867e162SEdward Tomasz Napierala 
477e867e162SEdward Tomasz Napierala 	if (auth_portal_find(ag, sa) == NULL)
478e867e162SEdward Tomasz Napierala 		return (1);
479e867e162SEdward Tomasz Napierala 
480e867e162SEdward Tomasz Napierala 	return (0);
481e867e162SEdward Tomasz Napierala }
482e867e162SEdward Tomasz Napierala 
483009ea47eSEdward Tomasz Napierala struct auth_group *
484009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
485009ea47eSEdward Tomasz Napierala {
486009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
487009ea47eSEdward Tomasz Napierala 
488009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
489009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
490009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
491009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
492009ea47eSEdward Tomasz Napierala 			return (NULL);
493009ea47eSEdward Tomasz Napierala 		}
494009ea47eSEdward Tomasz Napierala 	}
495009ea47eSEdward Tomasz Napierala 
496009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
497009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
498009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
499009ea47eSEdward Tomasz Napierala 	if (name != NULL)
500009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
501009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
5028cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
5038cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
504009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
505009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
506009ea47eSEdward Tomasz Napierala 
507009ea47eSEdward Tomasz Napierala 	return (ag);
508009ea47eSEdward Tomasz Napierala }
509009ea47eSEdward Tomasz Napierala 
510009ea47eSEdward Tomasz Napierala void
511009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
512009ea47eSEdward Tomasz Napierala {
5138cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
5148cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
5158cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
516009ea47eSEdward Tomasz Napierala 
517009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
518009ea47eSEdward Tomasz Napierala 
5198cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
520009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
5218cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
5228cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
5238cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
5248cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
5258cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
526009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
527009ea47eSEdward Tomasz Napierala 	free(ag);
528009ea47eSEdward Tomasz Napierala }
529009ea47eSEdward Tomasz Napierala 
530009ea47eSEdward Tomasz Napierala struct auth_group *
531d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name)
532009ea47eSEdward Tomasz Napierala {
533009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
534009ea47eSEdward Tomasz Napierala 
5352391e536SAlan Somers 	assert(name != NULL);
536009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
537009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
538009ea47eSEdward Tomasz Napierala 			return (ag);
539009ea47eSEdward Tomasz Napierala 	}
540009ea47eSEdward Tomasz Napierala 
541009ea47eSEdward Tomasz Napierala 	return (NULL);
542009ea47eSEdward Tomasz Napierala }
543009ea47eSEdward Tomasz Napierala 
544df9900fbSEdward Tomasz Napierala int
545bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str)
546df9900fbSEdward Tomasz Napierala {
547bd833b11SEdward Tomasz Napierala 	int type;
548df9900fbSEdward Tomasz Napierala 
549df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
550df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
551e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
552e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
553df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
554df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
555df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
556df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
557df9900fbSEdward Tomasz Napierala 	} else {
558df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
559df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
560df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
561df9900fbSEdward Tomasz Napierala 		else
562df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
563df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
564df9900fbSEdward Tomasz Napierala 		return (1);
565df9900fbSEdward Tomasz Napierala 	}
566df9900fbSEdward Tomasz Napierala 
567bd833b11SEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
568bd833b11SEdward Tomasz Napierala 		if (ag->ag_name != NULL) {
569df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
570df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
571df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
572bd833b11SEdward Tomasz Napierala 		} else {
573df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
574df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
575df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
576bd833b11SEdward Tomasz Napierala 		}
577df9900fbSEdward Tomasz Napierala 		return (1);
578df9900fbSEdward Tomasz Napierala 	}
579df9900fbSEdward Tomasz Napierala 
580bd833b11SEdward Tomasz Napierala 	ag->ag_type = type;
581bd833b11SEdward Tomasz Napierala 
582bd833b11SEdward Tomasz Napierala 	return (0);
583df9900fbSEdward Tomasz Napierala }
584df9900fbSEdward Tomasz Napierala 
585009ea47eSEdward Tomasz Napierala static struct portal *
586009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
587009ea47eSEdward Tomasz Napierala {
588009ea47eSEdward Tomasz Napierala 	struct portal *portal;
589009ea47eSEdward Tomasz Napierala 
590009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
591009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
592009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
593009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
594009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
595009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
596009ea47eSEdward Tomasz Napierala 	return (portal);
597009ea47eSEdward Tomasz Napierala }
598009ea47eSEdward Tomasz Napierala 
599009ea47eSEdward Tomasz Napierala static void
600009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
601009ea47eSEdward Tomasz Napierala {
60244a5953aSEdward Tomasz Napierala 
603009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
60444a5953aSEdward Tomasz Napierala 	if (portal->p_ai != NULL)
605009ea47eSEdward Tomasz Napierala 		freeaddrinfo(portal->p_ai);
606009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
607009ea47eSEdward Tomasz Napierala 	free(portal);
608009ea47eSEdward Tomasz Napierala }
609009ea47eSEdward Tomasz Napierala 
610009ea47eSEdward Tomasz Napierala struct portal_group *
611009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
612009ea47eSEdward Tomasz Napierala {
613009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
614009ea47eSEdward Tomasz Napierala 
615009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
616009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
617009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
618009ea47eSEdward Tomasz Napierala 		return (NULL);
619009ea47eSEdward Tomasz Napierala 	}
620009ea47eSEdward Tomasz Napierala 
621009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
622009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
623009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
624009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
625398290f2SAlexander Motin 	TAILQ_INIT(&pg->pg_options);
626009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
62792847ee1SAlexander Motin 	TAILQ_INIT(&pg->pg_ports);
628009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
6296b31e130SAlexander Motin 	pg->pg_tag = 0;		/* Assigned later in conf_apply(). */
63064ffe6d4SRichard Scheffenegger 	pg->pg_dscp = -1;
631ea8f1280SRichard Scheffenegger 	pg->pg_pcp = -1;
632009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
633009ea47eSEdward Tomasz Napierala 
634009ea47eSEdward Tomasz Napierala 	return (pg);
635009ea47eSEdward Tomasz Napierala }
636009ea47eSEdward Tomasz Napierala 
637009ea47eSEdward Tomasz Napierala void
638009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
639009ea47eSEdward Tomasz Napierala {
640009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
64192847ee1SAlexander Motin 	struct port *port, *tport;
642398290f2SAlexander Motin 	struct option *o, *otmp;
643009ea47eSEdward Tomasz Napierala 
64492847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
64592847ee1SAlexander Motin 		port_delete(port);
646009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
647009ea47eSEdward Tomasz Napierala 
648009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
649009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
650398290f2SAlexander Motin 	TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
651398290f2SAlexander Motin 		option_delete(&pg->pg_options, o);
652009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
653a9d78210SEdward Tomasz Napierala 	free(pg->pg_offload);
6546dbdbf82SEdward Tomasz Napierala 	free(pg->pg_redirection);
655009ea47eSEdward Tomasz Napierala 	free(pg);
656009ea47eSEdward Tomasz Napierala }
657009ea47eSEdward Tomasz Napierala 
658009ea47eSEdward Tomasz Napierala struct portal_group *
659d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name)
660009ea47eSEdward Tomasz Napierala {
661009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
662009ea47eSEdward Tomasz Napierala 
663009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
664009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
665009ea47eSEdward Tomasz Napierala 			return (pg);
666009ea47eSEdward Tomasz Napierala 	}
667009ea47eSEdward Tomasz Napierala 
668009ea47eSEdward Tomasz Napierala 	return (NULL);
669009ea47eSEdward Tomasz Napierala }
670009ea47eSEdward Tomasz Napierala 
671829603e2SAlexander Motin static int
672829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
673009ea47eSEdward Tomasz Napierala {
674009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
6751606ad8cSAlexander Motin 	char *str, *addr, *ch;
676009ea47eSEdward Tomasz Napierala 	const char *port;
677009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
678009ea47eSEdward Tomasz Napierala 
6791606ad8cSAlexander Motin 	str = arg = strdup(arg);
680009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
681009ea47eSEdward Tomasz Napierala 		/*
682009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
683009ea47eSEdward Tomasz Napierala 		 */
684009ea47eSEdward Tomasz Napierala 		arg++;
685009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
686203632f2SXin LI 		if (arg == NULL) {
687203632f2SXin LI 			free(str);
688009ea47eSEdward Tomasz Napierala 			return (1);
689203632f2SXin LI 		}
690009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
691829603e2SAlexander Motin 			port = def_port;
692009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
693009ea47eSEdward Tomasz Napierala 			port = arg + 1;
6941606ad8cSAlexander Motin 		} else {
6951606ad8cSAlexander Motin 			free(str);
696009ea47eSEdward Tomasz Napierala 			return (1);
6971606ad8cSAlexander Motin 		}
698009ea47eSEdward Tomasz Napierala 	} else {
699009ea47eSEdward Tomasz Napierala 		/*
700009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
701009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
702009ea47eSEdward Tomasz Napierala 		 */
703009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
704009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
705009ea47eSEdward Tomasz Napierala 				colons++;
706009ea47eSEdward Tomasz Napierala 		}
707009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
708009ea47eSEdward Tomasz Napierala 			addr = arg;
709829603e2SAlexander Motin 			port = def_port;
710009ea47eSEdward Tomasz Napierala 		} else {
711009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
712009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
713829603e2SAlexander Motin 				port = def_port;
714009ea47eSEdward Tomasz Napierala 			else
715009ea47eSEdward Tomasz Napierala 				port = arg;
716009ea47eSEdward Tomasz Napierala 		}
717009ea47eSEdward Tomasz Napierala 	}
718009ea47eSEdward Tomasz Napierala 
719009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
720009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
721009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
722009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
723829603e2SAlexander Motin 	error = getaddrinfo(addr, port, &hints, ai);
7241606ad8cSAlexander Motin 	free(str);
7251606ad8cSAlexander Motin 	return ((error != 0) ? 1 : 0);
726829603e2SAlexander Motin }
727009ea47eSEdward Tomasz Napierala 
728829603e2SAlexander Motin int
729829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
730829603e2SAlexander Motin {
731829603e2SAlexander Motin 	struct portal *portal;
732829603e2SAlexander Motin 
733829603e2SAlexander Motin 	portal = portal_new(pg);
734829603e2SAlexander Motin 	portal->p_listen = checked_strdup(value);
735829603e2SAlexander Motin 	portal->p_iser = iser;
736829603e2SAlexander Motin 
737829603e2SAlexander Motin 	if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
738829603e2SAlexander Motin 		log_warnx("invalid listen address %s", portal->p_listen);
73944a5953aSEdward Tomasz Napierala 		portal_delete(portal);
740009ea47eSEdward Tomasz Napierala 		return (1);
741009ea47eSEdward Tomasz Napierala 	}
742009ea47eSEdward Tomasz Napierala 
743009ea47eSEdward Tomasz Napierala 	/*
744009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
745009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
746009ea47eSEdward Tomasz Napierala 	 */
747009ea47eSEdward Tomasz Napierala 
748009ea47eSEdward Tomasz Napierala 	return (0);
749009ea47eSEdward Tomasz Napierala }
750009ea47eSEdward Tomasz Napierala 
751829603e2SAlexander Motin int
752829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr)
753829603e2SAlexander Motin {
754829603e2SAlexander Motin 	struct isns *isns;
755829603e2SAlexander Motin 
756829603e2SAlexander Motin 	isns = calloc(1, sizeof(*isns));
757829603e2SAlexander Motin 	if (isns == NULL)
758829603e2SAlexander Motin 		log_err(1, "calloc");
759829603e2SAlexander Motin 	isns->i_conf = conf;
760829603e2SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
761829603e2SAlexander Motin 	isns->i_addr = checked_strdup(addr);
762829603e2SAlexander Motin 
763829603e2SAlexander Motin 	if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
764829603e2SAlexander Motin 		log_warnx("invalid iSNS address %s", isns->i_addr);
765829603e2SAlexander Motin 		isns_delete(isns);
766829603e2SAlexander Motin 		return (1);
767829603e2SAlexander Motin 	}
768829603e2SAlexander Motin 
769829603e2SAlexander Motin 	/*
770829603e2SAlexander Motin 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
771829603e2SAlexander Motin 	 *	those into multiple servers.
772829603e2SAlexander Motin 	 */
773829603e2SAlexander Motin 
774829603e2SAlexander Motin 	return (0);
775829603e2SAlexander Motin }
776829603e2SAlexander Motin 
777829603e2SAlexander Motin void
778829603e2SAlexander Motin isns_delete(struct isns *isns)
779829603e2SAlexander Motin {
780829603e2SAlexander Motin 
781829603e2SAlexander Motin 	TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
782829603e2SAlexander Motin 	free(isns->i_addr);
783829603e2SAlexander Motin 	if (isns->i_ai != NULL)
784829603e2SAlexander Motin 		freeaddrinfo(isns->i_ai);
785829603e2SAlexander Motin 	free(isns);
786829603e2SAlexander Motin }
787829603e2SAlexander Motin 
788829603e2SAlexander Motin static int
789829603e2SAlexander Motin isns_do_connect(struct isns *isns)
790829603e2SAlexander Motin {
791829603e2SAlexander Motin 	int s;
792829603e2SAlexander Motin 
793829603e2SAlexander Motin 	s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
794829603e2SAlexander Motin 	    isns->i_ai->ai_protocol);
795829603e2SAlexander Motin 	if (s < 0) {
796829603e2SAlexander Motin 		log_warn("socket(2) failed for %s", isns->i_addr);
797829603e2SAlexander Motin 		return (-1);
798829603e2SAlexander Motin 	}
799829603e2SAlexander Motin 	if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
800829603e2SAlexander Motin 		log_warn("connect(2) failed for %s", isns->i_addr);
801829603e2SAlexander Motin 		close(s);
802829603e2SAlexander Motin 		return (-1);
803829603e2SAlexander Motin 	}
804829603e2SAlexander Motin 	return(s);
805829603e2SAlexander Motin }
806829603e2SAlexander Motin 
807829603e2SAlexander Motin static int
808829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname)
809829603e2SAlexander Motin {
810829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
811829603e2SAlexander Motin 	struct target *target;
812829603e2SAlexander Motin 	struct portal *portal;
813829603e2SAlexander Motin 	struct portal_group *pg;
81492847ee1SAlexander Motin 	struct port *port;
815829603e2SAlexander Motin 	struct isns_req *req;
816829603e2SAlexander Motin 	int res = 0;
817829603e2SAlexander Motin 	uint32_t error;
818829603e2SAlexander Motin 
819829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
820829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
821829603e2SAlexander Motin 	isns_req_add_delim(req);
822829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
823829603e2SAlexander Motin 	isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
824829603e2SAlexander Motin 	isns_req_add_32(req, 6, conf->conf_isns_period);
825829603e2SAlexander Motin 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
826829603e2SAlexander Motin 		if (pg->pg_unassigned)
827829603e2SAlexander Motin 			continue;
828829603e2SAlexander Motin 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
829829603e2SAlexander Motin 			isns_req_add_addr(req, 16, portal->p_ai);
830829603e2SAlexander Motin 			isns_req_add_port(req, 17, portal->p_ai);
831829603e2SAlexander Motin 		}
832829603e2SAlexander Motin 	}
833829603e2SAlexander Motin 	TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
834829603e2SAlexander Motin 		isns_req_add_str(req, 32, target->t_name);
835829603e2SAlexander Motin 		isns_req_add_32(req, 33, 1); /* 1 -- Target*/
836829603e2SAlexander Motin 		if (target->t_alias != NULL)
837829603e2SAlexander Motin 			isns_req_add_str(req, 34, target->t_alias);
83892847ee1SAlexander Motin 		TAILQ_FOREACH(port, &target->t_ports, p_ts) {
83992847ee1SAlexander Motin 			if ((pg = port->p_portal_group) == NULL)
84092847ee1SAlexander Motin 				continue;
841829603e2SAlexander Motin 			isns_req_add_32(req, 51, pg->pg_tag);
842829603e2SAlexander Motin 			TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
843829603e2SAlexander Motin 				isns_req_add_addr(req, 49, portal->p_ai);
844829603e2SAlexander Motin 				isns_req_add_port(req, 50, portal->p_ai);
845829603e2SAlexander Motin 			}
846829603e2SAlexander Motin 		}
84792847ee1SAlexander Motin 	}
848829603e2SAlexander Motin 	res = isns_req_send(s, req);
849829603e2SAlexander Motin 	if (res < 0) {
850829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
851829603e2SAlexander Motin 		goto quit;
852829603e2SAlexander Motin 	}
853829603e2SAlexander Motin 	res = isns_req_receive(s, req);
854829603e2SAlexander Motin 	if (res < 0) {
855829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
856829603e2SAlexander Motin 		goto quit;
857829603e2SAlexander Motin 	}
858829603e2SAlexander Motin 	error = isns_req_get_status(req);
859829603e2SAlexander Motin 	if (error != 0) {
860829603e2SAlexander Motin 		log_warnx("iSNS register error %d for %s", error, isns->i_addr);
861829603e2SAlexander Motin 		res = -1;
862829603e2SAlexander Motin 	}
863829603e2SAlexander Motin quit:
864829603e2SAlexander Motin 	isns_req_free(req);
865829603e2SAlexander Motin 	return (res);
866829603e2SAlexander Motin }
867829603e2SAlexander Motin 
868829603e2SAlexander Motin static int
869829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname)
870829603e2SAlexander Motin {
871829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
872829603e2SAlexander Motin 	struct isns_req *req;
873829603e2SAlexander Motin 	int res = 0;
874829603e2SAlexander Motin 	uint32_t error;
875829603e2SAlexander Motin 
876829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
877829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
878829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
879829603e2SAlexander Motin 	isns_req_add_delim(req);
880829603e2SAlexander Motin 	isns_req_add(req, 2, 0, NULL);
881829603e2SAlexander Motin 	res = isns_req_send(s, req);
882829603e2SAlexander Motin 	if (res < 0) {
883829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
884829603e2SAlexander Motin 		goto quit;
885829603e2SAlexander Motin 	}
886829603e2SAlexander Motin 	res = isns_req_receive(s, req);
887829603e2SAlexander Motin 	if (res < 0) {
888829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
889829603e2SAlexander Motin 		goto quit;
890829603e2SAlexander Motin 	}
891829603e2SAlexander Motin 	error = isns_req_get_status(req);
892829603e2SAlexander Motin 	if (error != 0) {
893829603e2SAlexander Motin 		log_warnx("iSNS check error %d for %s", error, isns->i_addr);
894829603e2SAlexander Motin 		res = -1;
895829603e2SAlexander Motin 	}
896829603e2SAlexander Motin quit:
897829603e2SAlexander Motin 	isns_req_free(req);
898829603e2SAlexander Motin 	return (res);
899829603e2SAlexander Motin }
900829603e2SAlexander Motin 
901829603e2SAlexander Motin static int
902829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname)
903829603e2SAlexander Motin {
904829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
905829603e2SAlexander Motin 	struct isns_req *req;
906829603e2SAlexander Motin 	int res = 0;
907829603e2SAlexander Motin 	uint32_t error;
908829603e2SAlexander Motin 
909829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
910829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
911829603e2SAlexander Motin 	isns_req_add_delim(req);
912829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
913829603e2SAlexander Motin 	res = isns_req_send(s, req);
914829603e2SAlexander Motin 	if (res < 0) {
915829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
916829603e2SAlexander Motin 		goto quit;
917829603e2SAlexander Motin 	}
918829603e2SAlexander Motin 	res = isns_req_receive(s, req);
919829603e2SAlexander Motin 	if (res < 0) {
920829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
921829603e2SAlexander Motin 		goto quit;
922829603e2SAlexander Motin 	}
923829603e2SAlexander Motin 	error = isns_req_get_status(req);
924829603e2SAlexander Motin 	if (error != 0) {
925829603e2SAlexander Motin 		log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
926829603e2SAlexander Motin 		res = -1;
927829603e2SAlexander Motin 	}
928829603e2SAlexander Motin quit:
929829603e2SAlexander Motin 	isns_req_free(req);
930829603e2SAlexander Motin 	return (res);
931829603e2SAlexander Motin }
932829603e2SAlexander Motin 
933829603e2SAlexander Motin void
934829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns)
935829603e2SAlexander Motin {
936829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
937ba2548b7SEdward Tomasz Napierala 	int error, s;
938829603e2SAlexander Motin 	char hostname[256];
939829603e2SAlexander Motin 
940829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
941829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
942829603e2SAlexander Motin 		return;
943829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
944829603e2SAlexander Motin 	s = isns_do_connect(isns);
945829603e2SAlexander Motin 	if (s < 0) {
946829603e2SAlexander Motin 		set_timeout(0, false);
947829603e2SAlexander Motin 		return;
948829603e2SAlexander Motin 	}
949ba2548b7SEdward Tomasz Napierala 	error = gethostname(hostname, sizeof(hostname));
950ba2548b7SEdward Tomasz Napierala 	if (error != 0)
951ba2548b7SEdward Tomasz Napierala 		log_err(1, "gethostname");
952829603e2SAlexander Motin 
953829603e2SAlexander Motin 	if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
954829603e2SAlexander Motin 		oldisns = isns;
955d5cc4a4aSAlexander Motin 	isns_do_deregister(oldisns, s, hostname);
956d5cc4a4aSAlexander Motin 	isns_do_register(isns, s, hostname);
957829603e2SAlexander Motin 	close(s);
958829603e2SAlexander Motin 	set_timeout(0, false);
959829603e2SAlexander Motin }
960829603e2SAlexander Motin 
961829603e2SAlexander Motin void
962829603e2SAlexander Motin isns_check(struct isns *isns)
963829603e2SAlexander Motin {
964829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
965ba2548b7SEdward Tomasz Napierala 	int error, s, res;
966829603e2SAlexander Motin 	char hostname[256];
967829603e2SAlexander Motin 
968829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
969829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
970829603e2SAlexander Motin 		return;
971829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
972829603e2SAlexander Motin 	s = isns_do_connect(isns);
973829603e2SAlexander Motin 	if (s < 0) {
974829603e2SAlexander Motin 		set_timeout(0, false);
975829603e2SAlexander Motin 		return;
976829603e2SAlexander Motin 	}
977ba2548b7SEdward Tomasz Napierala 	error = gethostname(hostname, sizeof(hostname));
978ba2548b7SEdward Tomasz Napierala 	if (error != 0)
979ba2548b7SEdward Tomasz Napierala 		log_err(1, "gethostname");
980829603e2SAlexander Motin 
981829603e2SAlexander Motin 	res = isns_do_check(isns, s, hostname);
982829603e2SAlexander Motin 	if (res < 0) {
983d5cc4a4aSAlexander Motin 		isns_do_deregister(isns, s, hostname);
984d5cc4a4aSAlexander Motin 		isns_do_register(isns, s, hostname);
985829603e2SAlexander Motin 	}
986829603e2SAlexander Motin 	close(s);
987829603e2SAlexander Motin 	set_timeout(0, false);
988829603e2SAlexander Motin }
989829603e2SAlexander Motin 
990829603e2SAlexander Motin void
991829603e2SAlexander Motin isns_deregister(struct isns *isns)
992829603e2SAlexander Motin {
993829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
994ba2548b7SEdward Tomasz Napierala 	int error, s;
995829603e2SAlexander Motin 	char hostname[256];
996829603e2SAlexander Motin 
997829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
998829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
999829603e2SAlexander Motin 		return;
1000829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
1001829603e2SAlexander Motin 	s = isns_do_connect(isns);
1002829603e2SAlexander Motin 	if (s < 0)
1003829603e2SAlexander Motin 		return;
1004ba2548b7SEdward Tomasz Napierala 	error = gethostname(hostname, sizeof(hostname));
1005ba2548b7SEdward Tomasz Napierala 	if (error != 0)
1006ba2548b7SEdward Tomasz Napierala 		log_err(1, "gethostname");
1007829603e2SAlexander Motin 
1008d5cc4a4aSAlexander Motin 	isns_do_deregister(isns, s, hostname);
1009829603e2SAlexander Motin 	close(s);
1010829603e2SAlexander Motin 	set_timeout(0, false);
1011829603e2SAlexander Motin }
1012829603e2SAlexander Motin 
101305374883SEdward Tomasz Napierala int
1014bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str)
101505374883SEdward Tomasz Napierala {
1016bd833b11SEdward Tomasz Napierala 	int filter;
101705374883SEdward Tomasz Napierala 
101805374883SEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
101905374883SEdward Tomasz Napierala 		filter = PG_FILTER_NONE;
102005374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal") == 0) {
102105374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL;
102205374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name") == 0) {
102305374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME;
102405374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name-auth") == 0) {
102505374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME_AUTH;
102605374883SEdward Tomasz Napierala 	} else {
102705374883SEdward Tomasz Napierala 		log_warnx("invalid discovery-filter \"%s\" for portal-group "
102805374883SEdward Tomasz Napierala 		    "\"%s\"; valid values are \"none\", \"portal\", "
102905374883SEdward Tomasz Napierala 		    "\"portal-name\", and \"portal-name-auth\"",
103005374883SEdward Tomasz Napierala 		    str, pg->pg_name);
103105374883SEdward Tomasz Napierala 		return (1);
103205374883SEdward Tomasz Napierala 	}
103305374883SEdward Tomasz Napierala 
1034bd833b11SEdward Tomasz Napierala 	if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1035bd833b11SEdward Tomasz Napierala 	    pg->pg_discovery_filter != filter) {
103605374883SEdward Tomasz Napierala 		log_warnx("cannot set discovery-filter to \"%s\" for "
103705374883SEdward Tomasz Napierala 		    "portal-group \"%s\"; already has a different "
103805374883SEdward Tomasz Napierala 		    "value", str, pg->pg_name);
103905374883SEdward Tomasz Napierala 		return (1);
104005374883SEdward Tomasz Napierala 	}
104105374883SEdward Tomasz Napierala 
1042bd833b11SEdward Tomasz Napierala 	pg->pg_discovery_filter = filter;
1043bd833b11SEdward Tomasz Napierala 
1044bd833b11SEdward Tomasz Napierala 	return (0);
104505374883SEdward Tomasz Napierala }
104605374883SEdward Tomasz Napierala 
10476dbdbf82SEdward Tomasz Napierala int
1048a9d78210SEdward Tomasz Napierala portal_group_set_offload(struct portal_group *pg, const char *offload)
1049a9d78210SEdward Tomasz Napierala {
1050a9d78210SEdward Tomasz Napierala 
1051a9d78210SEdward Tomasz Napierala 	if (pg->pg_offload != NULL) {
1052a9d78210SEdward Tomasz Napierala 		log_warnx("cannot set offload to \"%s\" for "
1053a9d78210SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
1054a9d78210SEdward Tomasz Napierala 		    offload, pg->pg_name);
1055a9d78210SEdward Tomasz Napierala 		return (1);
1056a9d78210SEdward Tomasz Napierala 	}
1057a9d78210SEdward Tomasz Napierala 
1058a9d78210SEdward Tomasz Napierala 	pg->pg_offload = checked_strdup(offload);
1059a9d78210SEdward Tomasz Napierala 
1060a9d78210SEdward Tomasz Napierala 	return (0);
1061a9d78210SEdward Tomasz Napierala }
1062a9d78210SEdward Tomasz Napierala 
1063a9d78210SEdward Tomasz Napierala int
10646dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr)
10656dbdbf82SEdward Tomasz Napierala {
10666dbdbf82SEdward Tomasz Napierala 
10676dbdbf82SEdward Tomasz Napierala 	if (pg->pg_redirection != NULL) {
10686dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
10696dbdbf82SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
10706dbdbf82SEdward Tomasz Napierala 		    addr, pg->pg_name);
10716dbdbf82SEdward Tomasz Napierala 		return (1);
10726dbdbf82SEdward Tomasz Napierala 	}
10736dbdbf82SEdward Tomasz Napierala 
10746dbdbf82SEdward Tomasz Napierala 	pg->pg_redirection = checked_strdup(addr);
10756dbdbf82SEdward Tomasz Napierala 
10766dbdbf82SEdward Tomasz Napierala 	return (0);
10776dbdbf82SEdward Tomasz Napierala }
10786dbdbf82SEdward Tomasz Napierala 
1079009ea47eSEdward Tomasz Napierala static bool
1080009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
1081009ea47eSEdward Tomasz Napierala {
1082009ea47eSEdward Tomasz Napierala 	switch (ch) {
1083009ea47eSEdward Tomasz Napierala 	case '0':
1084009ea47eSEdward Tomasz Napierala 	case '1':
1085009ea47eSEdward Tomasz Napierala 	case '2':
1086009ea47eSEdward Tomasz Napierala 	case '3':
1087009ea47eSEdward Tomasz Napierala 	case '4':
1088009ea47eSEdward Tomasz Napierala 	case '5':
1089009ea47eSEdward Tomasz Napierala 	case '6':
1090009ea47eSEdward Tomasz Napierala 	case '7':
1091009ea47eSEdward Tomasz Napierala 	case '8':
1092009ea47eSEdward Tomasz Napierala 	case '9':
1093009ea47eSEdward Tomasz Napierala 	case 'a':
1094009ea47eSEdward Tomasz Napierala 	case 'A':
1095009ea47eSEdward Tomasz Napierala 	case 'b':
1096009ea47eSEdward Tomasz Napierala 	case 'B':
1097009ea47eSEdward Tomasz Napierala 	case 'c':
1098009ea47eSEdward Tomasz Napierala 	case 'C':
1099009ea47eSEdward Tomasz Napierala 	case 'd':
1100009ea47eSEdward Tomasz Napierala 	case 'D':
1101009ea47eSEdward Tomasz Napierala 	case 'e':
1102009ea47eSEdward Tomasz Napierala 	case 'E':
1103009ea47eSEdward Tomasz Napierala 	case 'f':
1104009ea47eSEdward Tomasz Napierala 	case 'F':
1105009ea47eSEdward Tomasz Napierala 		return (true);
1106009ea47eSEdward Tomasz Napierala 	default:
1107009ea47eSEdward Tomasz Napierala 		return (false);
1108009ea47eSEdward Tomasz Napierala 	}
1109009ea47eSEdward Tomasz Napierala }
1110009ea47eSEdward Tomasz Napierala 
1111009ea47eSEdward Tomasz Napierala bool
1112009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
1113009ea47eSEdward Tomasz Napierala {
1114009ea47eSEdward Tomasz Napierala 	int i;
1115009ea47eSEdward Tomasz Napierala 
1116009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
1117009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
1118009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
1119009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
1120009ea47eSEdward Tomasz Napierala 		return (false);
1121009ea47eSEdward Tomasz Napierala 	}
1122009ea47eSEdward Tomasz Napierala 
1123009ea47eSEdward Tomasz Napierala 	/*
1124009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
1125009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
1126009ea47eSEdward Tomasz Napierala 	 */
1127009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1128009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
1129009ea47eSEdward Tomasz Napierala 			/*
1130009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
1131009ea47eSEdward Tomasz Napierala 			 *      by 3.2.6.2: iSCSI Name Encoding.
1132009ea47eSEdward Tomasz Napierala 			 */
1133009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
1134009ea47eSEdward Tomasz Napierala 				continue;
1135009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1136009ea47eSEdward Tomasz Napierala 				continue;
1137009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
1138009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
1139009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
1140009ea47eSEdward Tomasz Napierala 			break;
1141009ea47eSEdward Tomasz Napierala 		}
1142009ea47eSEdward Tomasz Napierala 		/*
1143009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
1144009ea47eSEdward Tomasz Napierala 		 */
1145009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1146009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
1147009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
1148009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
1149009ea47eSEdward Tomasz Napierala 			    "digits", name);
1150009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
1151009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1152009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1153009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1154009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1155009ea47eSEdward Tomasz Napierala 				break;
1156009ea47eSEdward Tomasz Napierala 			}
1157009ea47eSEdward Tomasz Napierala 		}
1158009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1159009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
1160009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
1161009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
1162009ea47eSEdward Tomasz Napierala 			    "digits", name);
1163009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
1164009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1165009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1166009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1167009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1168009ea47eSEdward Tomasz Napierala 				break;
1169009ea47eSEdward Tomasz Napierala 			}
1170009ea47eSEdward Tomasz Napierala 		}
1171009ea47eSEdward Tomasz Napierala 	} else {
1172009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
1173d7b87b89SJosh Paetzel 		    "either \"iqn.\", \"eui.\", or \"naa.\"",
1174009ea47eSEdward Tomasz Napierala 		    name);
1175009ea47eSEdward Tomasz Napierala 	}
1176009ea47eSEdward Tomasz Napierala 	return (true);
1177009ea47eSEdward Tomasz Napierala }
1178009ea47eSEdward Tomasz Napierala 
1179057abcb0SAlexander Motin struct pport *
1180057abcb0SAlexander Motin pport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1181057abcb0SAlexander Motin {
1182057abcb0SAlexander Motin 	struct pport *pp;
1183057abcb0SAlexander Motin 
1184057abcb0SAlexander Motin 	pp = calloc(1, sizeof(*pp));
1185057abcb0SAlexander Motin 	if (pp == NULL)
1186057abcb0SAlexander Motin 		log_err(1, "calloc");
1187057abcb0SAlexander Motin 	pp->pp_conf = conf;
1188057abcb0SAlexander Motin 	pp->pp_name = checked_strdup(name);
1189057abcb0SAlexander Motin 	pp->pp_ctl_port = ctl_port;
1190057abcb0SAlexander Motin 	TAILQ_INIT(&pp->pp_ports);
1191057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1192057abcb0SAlexander Motin 	return (pp);
1193057abcb0SAlexander Motin }
1194057abcb0SAlexander Motin 
1195057abcb0SAlexander Motin struct pport *
1196057abcb0SAlexander Motin pport_find(const struct conf *conf, const char *name)
1197057abcb0SAlexander Motin {
1198057abcb0SAlexander Motin 	struct pport *pp;
1199057abcb0SAlexander Motin 
1200057abcb0SAlexander Motin 	TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1201057abcb0SAlexander Motin 		if (strcasecmp(pp->pp_name, name) == 0)
1202057abcb0SAlexander Motin 			return (pp);
1203057abcb0SAlexander Motin 	}
1204057abcb0SAlexander Motin 	return (NULL);
1205057abcb0SAlexander Motin }
1206057abcb0SAlexander Motin 
1207057abcb0SAlexander Motin struct pport *
1208057abcb0SAlexander Motin pport_copy(struct pport *pp, struct conf *conf)
1209057abcb0SAlexander Motin {
1210057abcb0SAlexander Motin 	struct pport *ppnew;
1211057abcb0SAlexander Motin 
1212057abcb0SAlexander Motin 	ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1213057abcb0SAlexander Motin 	return (ppnew);
1214057abcb0SAlexander Motin }
1215057abcb0SAlexander Motin 
1216057abcb0SAlexander Motin void
1217057abcb0SAlexander Motin pport_delete(struct pport *pp)
1218057abcb0SAlexander Motin {
1219057abcb0SAlexander Motin 	struct port *port, *tport;
1220057abcb0SAlexander Motin 
1221057abcb0SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1222057abcb0SAlexander Motin 		port_delete(port);
1223057abcb0SAlexander Motin 	TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1224057abcb0SAlexander Motin 	free(pp->pp_name);
1225057abcb0SAlexander Motin 	free(pp);
1226057abcb0SAlexander Motin }
1227057abcb0SAlexander Motin 
122892847ee1SAlexander Motin struct port *
122992847ee1SAlexander Motin port_new(struct conf *conf, struct target *target, struct portal_group *pg)
123092847ee1SAlexander Motin {
123192847ee1SAlexander Motin 	struct port *port;
1232057abcb0SAlexander Motin 	char *name;
123355e4579eSAlexander Motin 	int ret;
123492847ee1SAlexander Motin 
123555e4579eSAlexander Motin 	ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
123655e4579eSAlexander Motin 	if (ret <= 0)
123755e4579eSAlexander Motin 		log_err(1, "asprintf");
1238057abcb0SAlexander Motin 	if (port_find(conf, name) != NULL) {
1239057abcb0SAlexander Motin 		log_warnx("duplicate port \"%s\"", name);
1240057abcb0SAlexander Motin 		free(name);
1241057abcb0SAlexander Motin 		return (NULL);
1242057abcb0SAlexander Motin 	}
124392847ee1SAlexander Motin 	port = calloc(1, sizeof(*port));
124492847ee1SAlexander Motin 	if (port == NULL)
124592847ee1SAlexander Motin 		log_err(1, "calloc");
124692847ee1SAlexander Motin 	port->p_conf = conf;
1247057abcb0SAlexander Motin 	port->p_name = name;
12488951f055SMarcelo Araujo 	port->p_ioctl_port = 0;
124992847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
125092847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
125192847ee1SAlexander Motin 	port->p_target = target;
125292847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
125392847ee1SAlexander Motin 	port->p_portal_group = pg;
125492847ee1SAlexander Motin 	return (port);
125592847ee1SAlexander Motin }
125692847ee1SAlexander Motin 
125792847ee1SAlexander Motin struct port *
12588951f055SMarcelo Araujo port_new_ioctl(struct conf *conf, struct target *target, int pp, int vp)
12598951f055SMarcelo Araujo {
12608951f055SMarcelo Araujo 	struct pport *pport;
12618951f055SMarcelo Araujo 	struct port *port;
12628951f055SMarcelo Araujo 	char *pname;
12638951f055SMarcelo Araujo 	char *name;
12648951f055SMarcelo Araujo 	int ret;
12658951f055SMarcelo Araujo 
12668951f055SMarcelo Araujo 	ret = asprintf(&pname, "ioctl/%d/%d", pp, vp);
12678951f055SMarcelo Araujo 	if (ret <= 0) {
12688951f055SMarcelo Araujo 		log_err(1, "asprintf");
12698951f055SMarcelo Araujo 		return (NULL);
12708951f055SMarcelo Araujo 	}
12718951f055SMarcelo Araujo 
12728951f055SMarcelo Araujo 	pport = pport_find(conf, pname);
12738951f055SMarcelo Araujo 	if (pport != NULL) {
12748951f055SMarcelo Araujo 		free(pname);
12758951f055SMarcelo Araujo 		return (port_new_pp(conf, target, pport));
12768951f055SMarcelo Araujo 	}
12778951f055SMarcelo Araujo 
12788951f055SMarcelo Araujo 	ret = asprintf(&name, "%s-%s", pname, target->t_name);
12798951f055SMarcelo Araujo 	free(pname);
12808951f055SMarcelo Araujo 
12818951f055SMarcelo Araujo 	if (ret <= 0)
12828951f055SMarcelo Araujo 		log_err(1, "asprintf");
12838951f055SMarcelo Araujo 	if (port_find(conf, name) != NULL) {
12848951f055SMarcelo Araujo 		log_warnx("duplicate port \"%s\"", name);
12858951f055SMarcelo Araujo 		free(name);
12868951f055SMarcelo Araujo 		return (NULL);
12878951f055SMarcelo Araujo 	}
12888951f055SMarcelo Araujo 	port = calloc(1, sizeof(*port));
12898951f055SMarcelo Araujo 	if (port == NULL)
12908951f055SMarcelo Araujo 		log_err(1, "calloc");
12918951f055SMarcelo Araujo 	port->p_conf = conf;
12928951f055SMarcelo Araujo 	port->p_name = name;
12938951f055SMarcelo Araujo 	port->p_ioctl_port = 1;
12948951f055SMarcelo Araujo 	port->p_ioctl_pp = pp;
12958951f055SMarcelo Araujo 	port->p_ioctl_vp = vp;
12968951f055SMarcelo Araujo 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
12978951f055SMarcelo Araujo 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
12988951f055SMarcelo Araujo 	port->p_target = target;
12998951f055SMarcelo Araujo 	return (port);
13008951f055SMarcelo Araujo }
13018951f055SMarcelo Araujo 
13028951f055SMarcelo Araujo struct port *
1303057abcb0SAlexander Motin port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1304057abcb0SAlexander Motin {
1305057abcb0SAlexander Motin 	struct port *port;
1306057abcb0SAlexander Motin 	char *name;
130755e4579eSAlexander Motin 	int ret;
1308057abcb0SAlexander Motin 
130955e4579eSAlexander Motin 	ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
131055e4579eSAlexander Motin 	if (ret <= 0)
131155e4579eSAlexander Motin 		log_err(1, "asprintf");
1312057abcb0SAlexander Motin 	if (port_find(conf, name) != NULL) {
1313057abcb0SAlexander Motin 		log_warnx("duplicate port \"%s\"", name);
1314057abcb0SAlexander Motin 		free(name);
1315057abcb0SAlexander Motin 		return (NULL);
1316057abcb0SAlexander Motin 	}
1317057abcb0SAlexander Motin 	port = calloc(1, sizeof(*port));
1318057abcb0SAlexander Motin 	if (port == NULL)
1319057abcb0SAlexander Motin 		log_err(1, "calloc");
1320057abcb0SAlexander Motin 	port->p_conf = conf;
1321057abcb0SAlexander Motin 	port->p_name = name;
1322057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1323057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1324057abcb0SAlexander Motin 	port->p_target = target;
1325057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1326057abcb0SAlexander Motin 	port->p_pport = pp;
1327057abcb0SAlexander Motin 	return (port);
1328057abcb0SAlexander Motin }
1329057abcb0SAlexander Motin 
1330057abcb0SAlexander Motin struct port *
133192847ee1SAlexander Motin port_find(const struct conf *conf, const char *name)
133292847ee1SAlexander Motin {
133392847ee1SAlexander Motin 	struct port *port;
133492847ee1SAlexander Motin 
133592847ee1SAlexander Motin 	TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
133692847ee1SAlexander Motin 		if (strcasecmp(port->p_name, name) == 0)
133792847ee1SAlexander Motin 			return (port);
133892847ee1SAlexander Motin 	}
133992847ee1SAlexander Motin 
134092847ee1SAlexander Motin 	return (NULL);
134192847ee1SAlexander Motin }
134292847ee1SAlexander Motin 
134392847ee1SAlexander Motin struct port *
134492847ee1SAlexander Motin port_find_in_pg(const struct portal_group *pg, const char *target)
134592847ee1SAlexander Motin {
134692847ee1SAlexander Motin 	struct port *port;
134792847ee1SAlexander Motin 
134892847ee1SAlexander Motin 	TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
134992847ee1SAlexander Motin 		if (strcasecmp(port->p_target->t_name, target) == 0)
135092847ee1SAlexander Motin 			return (port);
135192847ee1SAlexander Motin 	}
135292847ee1SAlexander Motin 
135392847ee1SAlexander Motin 	return (NULL);
135492847ee1SAlexander Motin }
135592847ee1SAlexander Motin 
135692847ee1SAlexander Motin void
135792847ee1SAlexander Motin port_delete(struct port *port)
135892847ee1SAlexander Motin {
135992847ee1SAlexander Motin 
136092847ee1SAlexander Motin 	if (port->p_portal_group)
136192847ee1SAlexander Motin 		TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1362057abcb0SAlexander Motin 	if (port->p_pport)
1363057abcb0SAlexander Motin 		TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
136492847ee1SAlexander Motin 	if (port->p_target)
136592847ee1SAlexander Motin 		TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
136692847ee1SAlexander Motin 	TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
136792847ee1SAlexander Motin 	free(port->p_name);
136892847ee1SAlexander Motin 	free(port);
136992847ee1SAlexander Motin }
137092847ee1SAlexander Motin 
13719215e501SAlexander Motin int
13729215e501SAlexander Motin port_is_dummy(struct port *port)
13739215e501SAlexander Motin {
13749215e501SAlexander Motin 
13759215e501SAlexander Motin 	if (port->p_portal_group) {
13769215e501SAlexander Motin 		if (port->p_portal_group->pg_foreign)
13779215e501SAlexander Motin 			return (1);
13789215e501SAlexander Motin 		if (TAILQ_EMPTY(&port->p_portal_group->pg_portals))
13799215e501SAlexander Motin 			return (1);
13809215e501SAlexander Motin 	}
13819215e501SAlexander Motin 	return (0);
13829215e501SAlexander Motin }
13839215e501SAlexander Motin 
1384009ea47eSEdward Tomasz Napierala struct target *
1385f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
1386009ea47eSEdward Tomasz Napierala {
1387009ea47eSEdward Tomasz Napierala 	struct target *targ;
1388009ea47eSEdward Tomasz Napierala 	int i, len;
1389009ea47eSEdward Tomasz Napierala 
1390f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
1391009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
1392f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
1393009ea47eSEdward Tomasz Napierala 		return (NULL);
1394009ea47eSEdward Tomasz Napierala 	}
1395f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
1396f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
1397009ea47eSEdward Tomasz Napierala 		return (NULL);
1398009ea47eSEdward Tomasz Napierala 	}
1399009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
1400009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
1401009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1402f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
1403009ea47eSEdward Tomasz Napierala 
1404009ea47eSEdward Tomasz Napierala 	/*
1405009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
1406009ea47eSEdward Tomasz Napierala 	 */
1407f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
1408009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
1409f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
1410009ea47eSEdward Tomasz Napierala 
1411009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
141292847ee1SAlexander Motin 	TAILQ_INIT(&targ->t_ports);
1413009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1414009ea47eSEdward Tomasz Napierala 
1415009ea47eSEdward Tomasz Napierala 	return (targ);
1416009ea47eSEdward Tomasz Napierala }
1417009ea47eSEdward Tomasz Napierala 
1418009ea47eSEdward Tomasz Napierala void
1419009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
1420009ea47eSEdward Tomasz Napierala {
142192847ee1SAlexander Motin 	struct port *port, *tport;
1422009ea47eSEdward Tomasz Napierala 
142392847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
142492847ee1SAlexander Motin 		port_delete(port);
1425009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1426009ea47eSEdward Tomasz Napierala 
1427f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
14286dbdbf82SEdward Tomasz Napierala 	free(targ->t_redirection);
1429009ea47eSEdward Tomasz Napierala 	free(targ);
1430009ea47eSEdward Tomasz Napierala }
1431009ea47eSEdward Tomasz Napierala 
1432009ea47eSEdward Tomasz Napierala struct target *
1433f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
1434009ea47eSEdward Tomasz Napierala {
1435009ea47eSEdward Tomasz Napierala 	struct target *targ;
1436009ea47eSEdward Tomasz Napierala 
1437009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1438f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
1439009ea47eSEdward Tomasz Napierala 			return (targ);
1440009ea47eSEdward Tomasz Napierala 	}
1441009ea47eSEdward Tomasz Napierala 
1442009ea47eSEdward Tomasz Napierala 	return (NULL);
1443009ea47eSEdward Tomasz Napierala }
1444009ea47eSEdward Tomasz Napierala 
14456dbdbf82SEdward Tomasz Napierala int
14466dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr)
14476dbdbf82SEdward Tomasz Napierala {
14486dbdbf82SEdward Tomasz Napierala 
14496dbdbf82SEdward Tomasz Napierala 	if (target->t_redirection != NULL) {
14506dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
14516dbdbf82SEdward Tomasz Napierala 		    "target \"%s\"; already defined",
14526dbdbf82SEdward Tomasz Napierala 		    addr, target->t_name);
14536dbdbf82SEdward Tomasz Napierala 		return (1);
14546dbdbf82SEdward Tomasz Napierala 	}
14556dbdbf82SEdward Tomasz Napierala 
14566dbdbf82SEdward Tomasz Napierala 	target->t_redirection = checked_strdup(addr);
14576dbdbf82SEdward Tomasz Napierala 
14586dbdbf82SEdward Tomasz Napierala 	return (0);
14596dbdbf82SEdward Tomasz Napierala }
14606dbdbf82SEdward Tomasz Napierala 
1461009ea47eSEdward Tomasz Napierala struct lun *
1462920c6cbaSAlexander Motin lun_new(struct conf *conf, const char *name)
1463009ea47eSEdward Tomasz Napierala {
1464009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1465009ea47eSEdward Tomasz Napierala 
1466920c6cbaSAlexander Motin 	lun = lun_find(conf, name);
1467009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
1468920c6cbaSAlexander Motin 		log_warnx("duplicated lun \"%s\"", name);
1469009ea47eSEdward Tomasz Napierala 		return (NULL);
1470009ea47eSEdward Tomasz Napierala 	}
1471009ea47eSEdward Tomasz Napierala 
1472009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
1473009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
1474009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1475920c6cbaSAlexander Motin 	lun->l_conf = conf;
1476920c6cbaSAlexander Motin 	lun->l_name = checked_strdup(name);
1477009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
1478920c6cbaSAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
14793933f7b1SAlexander Motin 	lun->l_ctl_lun = -1;
1480009ea47eSEdward Tomasz Napierala 
1481009ea47eSEdward Tomasz Napierala 	return (lun);
1482009ea47eSEdward Tomasz Napierala }
1483009ea47eSEdward Tomasz Napierala 
1484009ea47eSEdward Tomasz Napierala void
1485009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
1486009ea47eSEdward Tomasz Napierala {
1487920c6cbaSAlexander Motin 	struct target *targ;
1488398290f2SAlexander Motin 	struct option *o, *tmp;
1489920c6cbaSAlexander Motin 	int i;
1490009ea47eSEdward Tomasz Napierala 
1491920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1492920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1493920c6cbaSAlexander Motin 			if (targ->t_luns[i] == lun)
1494920c6cbaSAlexander Motin 				targ->t_luns[i] = NULL;
1495920c6cbaSAlexander Motin 		}
1496920c6cbaSAlexander Motin 	}
1497920c6cbaSAlexander Motin 	TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1498009ea47eSEdward Tomasz Napierala 
1499398290f2SAlexander Motin 	TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1500398290f2SAlexander Motin 		option_delete(&lun->l_options, o);
1501920c6cbaSAlexander Motin 	free(lun->l_name);
1502009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1503009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1504009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1505920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1506009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1507009ea47eSEdward Tomasz Napierala 	free(lun);
1508009ea47eSEdward Tomasz Napierala }
1509009ea47eSEdward Tomasz Napierala 
1510009ea47eSEdward Tomasz Napierala struct lun *
1511920c6cbaSAlexander Motin lun_find(const struct conf *conf, const char *name)
1512009ea47eSEdward Tomasz Napierala {
1513009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1514009ea47eSEdward Tomasz Napierala 
1515920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1516920c6cbaSAlexander Motin 		if (strcmp(lun->l_name, name) == 0)
1517009ea47eSEdward Tomasz Napierala 			return (lun);
1518009ea47eSEdward Tomasz Napierala 	}
1519009ea47eSEdward Tomasz Napierala 
1520009ea47eSEdward Tomasz Napierala 	return (NULL);
1521009ea47eSEdward Tomasz Napierala }
1522009ea47eSEdward Tomasz Napierala 
1523009ea47eSEdward Tomasz Napierala void
1524009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
1525009ea47eSEdward Tomasz Napierala {
1526009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1527009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
1528009ea47eSEdward Tomasz Napierala }
1529009ea47eSEdward Tomasz Napierala 
1530009ea47eSEdward Tomasz Napierala void
1531009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
1532009ea47eSEdward Tomasz Napierala {
1533009ea47eSEdward Tomasz Napierala 
1534009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
1535009ea47eSEdward Tomasz Napierala }
1536009ea47eSEdward Tomasz Napierala 
1537009ea47eSEdward Tomasz Napierala void
153891be33dcSAlexander Motin lun_set_device_type(struct lun *lun, uint8_t value)
153991be33dcSAlexander Motin {
154091be33dcSAlexander Motin 
154191be33dcSAlexander Motin 	lun->l_device_type = value;
154291be33dcSAlexander Motin }
154391be33dcSAlexander Motin 
154491be33dcSAlexander Motin void
1545009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
1546009ea47eSEdward Tomasz Napierala {
1547009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1548009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
1549009ea47eSEdward Tomasz Napierala }
1550009ea47eSEdward Tomasz Napierala 
1551009ea47eSEdward Tomasz Napierala void
1552009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
1553009ea47eSEdward Tomasz Napierala {
1554009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1555009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
1556009ea47eSEdward Tomasz Napierala }
1557009ea47eSEdward Tomasz Napierala 
1558009ea47eSEdward Tomasz Napierala void
1559920c6cbaSAlexander Motin lun_set_scsiname(struct lun *lun, const char *value)
1560920c6cbaSAlexander Motin {
1561920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1562920c6cbaSAlexander Motin 	lun->l_scsiname = checked_strdup(value);
1563920c6cbaSAlexander Motin }
1564920c6cbaSAlexander Motin 
1565920c6cbaSAlexander Motin void
1566009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
1567009ea47eSEdward Tomasz Napierala {
1568009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1569009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
1570009ea47eSEdward Tomasz Napierala }
1571009ea47eSEdward Tomasz Napierala 
1572009ea47eSEdward Tomasz Napierala void
1573009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
1574009ea47eSEdward Tomasz Napierala {
1575009ea47eSEdward Tomasz Napierala 
1576009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
1577009ea47eSEdward Tomasz Napierala }
1578009ea47eSEdward Tomasz Napierala 
1579009ea47eSEdward Tomasz Napierala void
1580009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
1581009ea47eSEdward Tomasz Napierala {
1582009ea47eSEdward Tomasz Napierala 
1583009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
1584009ea47eSEdward Tomasz Napierala }
1585009ea47eSEdward Tomasz Napierala 
1586398290f2SAlexander Motin struct option *
1587398290f2SAlexander Motin option_new(struct options *options, const char *name, const char *value)
1588009ea47eSEdward Tomasz Napierala {
1589398290f2SAlexander Motin 	struct option *o;
1590009ea47eSEdward Tomasz Napierala 
1591398290f2SAlexander Motin 	o = option_find(options, name);
1592398290f2SAlexander Motin 	if (o != NULL) {
1593398290f2SAlexander Motin 		log_warnx("duplicated option \"%s\"", name);
1594009ea47eSEdward Tomasz Napierala 		return (NULL);
1595009ea47eSEdward Tomasz Napierala 	}
1596009ea47eSEdward Tomasz Napierala 
1597398290f2SAlexander Motin 	o = calloc(1, sizeof(*o));
1598398290f2SAlexander Motin 	if (o == NULL)
1599009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1600398290f2SAlexander Motin 	o->o_name = checked_strdup(name);
1601398290f2SAlexander Motin 	o->o_value = checked_strdup(value);
1602398290f2SAlexander Motin 	TAILQ_INSERT_TAIL(options, o, o_next);
1603009ea47eSEdward Tomasz Napierala 
1604398290f2SAlexander Motin 	return (o);
1605009ea47eSEdward Tomasz Napierala }
1606009ea47eSEdward Tomasz Napierala 
1607009ea47eSEdward Tomasz Napierala void
1608398290f2SAlexander Motin option_delete(struct options *options, struct option *o)
1609009ea47eSEdward Tomasz Napierala {
1610009ea47eSEdward Tomasz Napierala 
1611398290f2SAlexander Motin 	TAILQ_REMOVE(options, o, o_next);
1612398290f2SAlexander Motin 	free(o->o_name);
1613398290f2SAlexander Motin 	free(o->o_value);
1614398290f2SAlexander Motin 	free(o);
1615009ea47eSEdward Tomasz Napierala }
1616009ea47eSEdward Tomasz Napierala 
1617398290f2SAlexander Motin struct option *
1618398290f2SAlexander Motin option_find(const struct options *options, const char *name)
1619009ea47eSEdward Tomasz Napierala {
1620398290f2SAlexander Motin 	struct option *o;
1621009ea47eSEdward Tomasz Napierala 
1622398290f2SAlexander Motin 	TAILQ_FOREACH(o, options, o_next) {
1623398290f2SAlexander Motin 		if (strcmp(o->o_name, name) == 0)
1624398290f2SAlexander Motin 			return (o);
1625009ea47eSEdward Tomasz Napierala 	}
1626009ea47eSEdward Tomasz Napierala 
1627009ea47eSEdward Tomasz Napierala 	return (NULL);
1628009ea47eSEdward Tomasz Napierala }
1629009ea47eSEdward Tomasz Napierala 
1630009ea47eSEdward Tomasz Napierala void
1631398290f2SAlexander Motin option_set(struct option *o, const char *value)
1632009ea47eSEdward Tomasz Napierala {
1633009ea47eSEdward Tomasz Napierala 
1634398290f2SAlexander Motin 	free(o->o_value);
1635398290f2SAlexander Motin 	o->o_value = checked_strdup(value);
1636009ea47eSEdward Tomasz Napierala }
1637009ea47eSEdward Tomasz Napierala 
163863783933SJohn Baldwin #ifdef ICL_KERNEL_PROXY
163963783933SJohn Baldwin 
164063783933SJohn Baldwin static void
164163783933SJohn Baldwin pdu_receive_proxy(struct pdu *pdu)
164263783933SJohn Baldwin {
164363783933SJohn Baldwin 	struct connection *conn;
164463783933SJohn Baldwin 	size_t len;
164563783933SJohn Baldwin 
164663783933SJohn Baldwin 	assert(proxy_mode);
164763783933SJohn Baldwin 	conn = pdu->pdu_connection;
164863783933SJohn Baldwin 
164963783933SJohn Baldwin 	kernel_receive(pdu);
165063783933SJohn Baldwin 
165163783933SJohn Baldwin 	len = pdu_ahs_length(pdu);
165263783933SJohn Baldwin 	if (len > 0)
165363783933SJohn Baldwin 		log_errx(1, "protocol error: non-empty AHS");
165463783933SJohn Baldwin 
165563783933SJohn Baldwin 	len = pdu_data_segment_length(pdu);
165663783933SJohn Baldwin 	assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
165763783933SJohn Baldwin 	pdu->pdu_data_len = len;
165863783933SJohn Baldwin }
165963783933SJohn Baldwin 
166063783933SJohn Baldwin static void
166163783933SJohn Baldwin pdu_send_proxy(struct pdu *pdu)
166263783933SJohn Baldwin {
166363783933SJohn Baldwin 
166463783933SJohn Baldwin 	assert(proxy_mode);
166563783933SJohn Baldwin 
166663783933SJohn Baldwin 	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
166763783933SJohn Baldwin 	kernel_send(pdu);
166863783933SJohn Baldwin }
166963783933SJohn Baldwin 
167063783933SJohn Baldwin #endif /* ICL_KERNEL_PROXY */
167163783933SJohn Baldwin 
167263783933SJohn Baldwin static void
167363783933SJohn Baldwin pdu_fail(const struct connection *conn __unused, const char *reason __unused)
167463783933SJohn Baldwin {
167563783933SJohn Baldwin }
167663783933SJohn Baldwin 
167763783933SJohn Baldwin static struct ctld_connection *
1678073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host,
1679073edb1cSAlexander Motin     const struct sockaddr *client_sa)
1680009ea47eSEdward Tomasz Napierala {
168163783933SJohn Baldwin 	struct ctld_connection *conn;
1682009ea47eSEdward Tomasz Napierala 
1683009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
1684009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
1685009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
168663783933SJohn Baldwin 	connection_init(&conn->conn, &conn_ops, proxy_mode);
168763783933SJohn Baldwin 	conn->conn.conn_socket = fd;
1688009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
1689009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
1690073edb1cSAlexander Motin 	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1691009ea47eSEdward Tomasz Napierala 
1692009ea47eSEdward Tomasz Napierala 	return (conn);
1693009ea47eSEdward Tomasz Napierala }
1694009ea47eSEdward Tomasz Napierala 
1695009ea47eSEdward Tomasz Napierala #if 0
1696009ea47eSEdward Tomasz Napierala static void
1697009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
1698009ea47eSEdward Tomasz Napierala {
1699009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1700009ea47eSEdward Tomasz Napierala 	struct auth *auth;
17018cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
17028cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
1703009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1704009ea47eSEdward Tomasz Napierala 	struct portal *portal;
1705009ea47eSEdward Tomasz Napierala 	struct target *targ;
1706009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1707398290f2SAlexander Motin 	struct option *o;
1708009ea47eSEdward Tomasz Napierala 
1709009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1710009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1711009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1712009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1713009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
1714009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
17158cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
17168cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
1717bdbc2a6fSAlexander Motin 			    auth_name->an_initiator_name);
17188951f055SMarcelo Araujo 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next)
17198cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
1720a3434cdcSAlexander Motin 			    auth_portal->ap_initiator_portal);
1721009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1722009ea47eSEdward Tomasz Napierala 	}
1723009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1724009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1725009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1726009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1727009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1728009ea47eSEdward Tomasz Napierala 	}
1729920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1730920c6cbaSAlexander Motin 		fprintf(stderr, "\tlun %s {\n", lun->l_name);
1731009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1732398290f2SAlexander Motin 		TAILQ_FOREACH(o, &lun->l_options, o_next)
1733009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\toption %s %s\n",
17348951f055SMarcelo Araujo 			    o->o_name, o->o_value);
1735009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t}\n");
1736009ea47eSEdward Tomasz Napierala 	}
1737920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1738920c6cbaSAlexander Motin 		fprintf(stderr, "target %s {\n", targ->t_name);
1739920c6cbaSAlexander Motin 		if (targ->t_alias != NULL)
1740920c6cbaSAlexander Motin 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1741009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1742009ea47eSEdward Tomasz Napierala 	}
1743009ea47eSEdward Tomasz Napierala }
1744009ea47eSEdward Tomasz Napierala #endif
1745009ea47eSEdward Tomasz Napierala 
17462fabfaa5SEdward Tomasz Napierala static int
17472fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
17482fabfaa5SEdward Tomasz Napierala {
17492fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
17502fabfaa5SEdward Tomasz Napierala 
17512fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
17522fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
17532fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
17542fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
1755920c6cbaSAlexander Motin 			log_warnx("missing path for lun \"%s\"",
1756920c6cbaSAlexander Motin 			    lun->l_name);
17572fabfaa5SEdward Tomasz Napierala 			return (1);
17582fabfaa5SEdward Tomasz Napierala 		}
17592fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
17602fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
1761920c6cbaSAlexander Motin 			log_warnx("missing size for ramdisk-backed lun \"%s\"",
1762920c6cbaSAlexander Motin 			    lun->l_name);
17632fabfaa5SEdward Tomasz Napierala 			return (1);
17642fabfaa5SEdward Tomasz Napierala 		}
17652fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
17662fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
1767920c6cbaSAlexander Motin 			    "for ramdisk-backed lun \"%s\"",
1768920c6cbaSAlexander Motin 			    lun->l_name);
17692fabfaa5SEdward Tomasz Napierala 			return (1);
17702fabfaa5SEdward Tomasz Napierala 		}
17712fabfaa5SEdward Tomasz Napierala 	}
17722fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
17736f2f92a5SAlexander Motin 		if (lun->l_device_type == 5)
17746f2f92a5SAlexander Motin 			lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
17756f2f92a5SAlexander Motin 		else
17762fabfaa5SEdward Tomasz Napierala 			lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
17772fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
1778920c6cbaSAlexander Motin 		log_warnx("invalid blocksize for lun \"%s\"; "
1779920c6cbaSAlexander Motin 		    "must be larger than 0", lun->l_name);
17802fabfaa5SEdward Tomasz Napierala 		return (1);
17812fabfaa5SEdward Tomasz Napierala 	}
17822fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1783920c6cbaSAlexander Motin 		log_warnx("invalid size for lun \"%s\"; "
1784920c6cbaSAlexander Motin 		    "must be multiple of blocksize", lun->l_name);
17852fabfaa5SEdward Tomasz Napierala 		return (1);
17862fabfaa5SEdward Tomasz Napierala 	}
1787920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1788a19eebb1SEdward Tomasz Napierala 		if (lun == lun2)
1789a19eebb1SEdward Tomasz Napierala 			continue;
1790a19eebb1SEdward Tomasz Napierala 		if (lun->l_path != NULL && lun2->l_path != NULL &&
1791a19eebb1SEdward Tomasz Napierala 		    strcmp(lun->l_path, lun2->l_path) == 0) {
1792a19eebb1SEdward Tomasz Napierala 			log_debugx("WARNING: path \"%s\" duplicated "
1793920c6cbaSAlexander Motin 			    "between lun \"%s\", and "
1794920c6cbaSAlexander Motin 			    "lun \"%s\"", lun->l_path,
1795920c6cbaSAlexander Motin 			    lun->l_name, lun2->l_name);
1796a19eebb1SEdward Tomasz Napierala 		}
1797a19eebb1SEdward Tomasz Napierala 	}
17982fabfaa5SEdward Tomasz Napierala 
17992fabfaa5SEdward Tomasz Napierala 	return (0);
18002fabfaa5SEdward Tomasz Napierala }
18012fabfaa5SEdward Tomasz Napierala 
1802009ea47eSEdward Tomasz Napierala int
1803009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1804009ea47eSEdward Tomasz Napierala {
1805009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1806009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
180792847ee1SAlexander Motin 	struct port *port;
1808009ea47eSEdward Tomasz Napierala 	struct target *targ;
18092fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
18104e5c38f8SEdward Tomasz Napierala 	bool found;
1811920c6cbaSAlexander Motin 	int error, i;
1812009ea47eSEdward Tomasz Napierala 
1813009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1814009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1815009ea47eSEdward Tomasz Napierala 
1816920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1817920c6cbaSAlexander Motin 		error = conf_verify_lun(lun);
1818920c6cbaSAlexander Motin 		if (error != 0)
1819920c6cbaSAlexander Motin 			return (error);
1820920c6cbaSAlexander Motin 	}
1821009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1822009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1823e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1824e7977549SEdward Tomasz Napierala 			    "default");
1825e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1826009ea47eSEdward Tomasz Napierala 		}
182792847ee1SAlexander Motin 		if (TAILQ_EMPTY(&targ->t_ports)) {
182892847ee1SAlexander Motin 			pg = portal_group_find(conf, "default");
182992847ee1SAlexander Motin 			assert(pg != NULL);
183092847ee1SAlexander Motin 			port_new(conf, targ, pg);
1831009ea47eSEdward Tomasz Napierala 		}
18324e5c38f8SEdward Tomasz Napierala 		found = false;
1833920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1834920c6cbaSAlexander Motin 			if (targ->t_luns[i] != NULL)
18354e5c38f8SEdward Tomasz Napierala 				found = true;
1836009ea47eSEdward Tomasz Napierala 		}
18376dbdbf82SEdward Tomasz Napierala 		if (!found && targ->t_redirection == NULL) {
183819b59f13SEdward Tomasz Napierala 			log_warnx("no LUNs defined for target \"%s\"",
183919b59f13SEdward Tomasz Napierala 			    targ->t_name);
1840009ea47eSEdward Tomasz Napierala 		}
18416dbdbf82SEdward Tomasz Napierala 		if (found && targ->t_redirection != NULL) {
18426dbdbf82SEdward Tomasz Napierala 			log_debugx("target \"%s\" contains luns, "
18436dbdbf82SEdward Tomasz Napierala 			    " but configured for redirection",
18446dbdbf82SEdward Tomasz Napierala 			    targ->t_name);
18456dbdbf82SEdward Tomasz Napierala 		}
1846009ea47eSEdward Tomasz Napierala 	}
1847009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1848009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1849009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1850009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1851ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1852009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1853009ea47eSEdward Tomasz Napierala 		}
1854009ea47eSEdward Tomasz Napierala 
185505374883SEdward Tomasz Napierala 		if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
185605374883SEdward Tomasz Napierala 			pg->pg_discovery_filter = PG_FILTER_NONE;
185705374883SEdward Tomasz Napierala 
18586dbdbf82SEdward Tomasz Napierala 		if (pg->pg_redirection != NULL) {
1859db7bf2baSAlexander Motin 			if (!TAILQ_EMPTY(&pg->pg_ports)) {
18606dbdbf82SEdward Tomasz Napierala 				log_debugx("portal-group \"%s\" assigned "
186192847ee1SAlexander Motin 				    "to target, but configured "
18626dbdbf82SEdward Tomasz Napierala 				    "for redirection",
186392847ee1SAlexander Motin 				    pg->pg_name);
18646dbdbf82SEdward Tomasz Napierala 			}
18656dbdbf82SEdward Tomasz Napierala 			pg->pg_unassigned = false;
1866db7bf2baSAlexander Motin 		} else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1867db7bf2baSAlexander Motin 			pg->pg_unassigned = false;
18686dbdbf82SEdward Tomasz Napierala 		} else {
1869009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1870009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1871009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1872009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
18736dbdbf82SEdward Tomasz Napierala 		}
1874009ea47eSEdward Tomasz Napierala 	}
1875009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1876009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1877009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1878009ea47eSEdward Tomasz Napierala 		else
1879009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1880009ea47eSEdward Tomasz Napierala 
18814e5c38f8SEdward Tomasz Napierala 		found = false;
1882009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
18834e5c38f8SEdward Tomasz Napierala 			if (targ->t_auth_group == ag) {
18844e5c38f8SEdward Tomasz Napierala 				found = true;
1885009ea47eSEdward Tomasz Napierala 				break;
1886009ea47eSEdward Tomasz Napierala 			}
18874e5c38f8SEdward Tomasz Napierala 		}
188892847ee1SAlexander Motin 		TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
188992847ee1SAlexander Motin 			if (port->p_auth_group == ag) {
189092847ee1SAlexander Motin 				found = true;
189192847ee1SAlexander Motin 				break;
189292847ee1SAlexander Motin 			}
189392847ee1SAlexander Motin 		}
18944e5c38f8SEdward Tomasz Napierala 		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
18954e5c38f8SEdward Tomasz Napierala 			if (pg->pg_discovery_auth_group == ag) {
18964e5c38f8SEdward Tomasz Napierala 				found = true;
18974e5c38f8SEdward Tomasz Napierala 				break;
18984e5c38f8SEdward Tomasz Napierala 			}
18994e5c38f8SEdward Tomasz Napierala 		}
19004e5c38f8SEdward Tomasz Napierala 		if (!found && ag->ag_name != NULL &&
1901ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1902009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1903009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1904009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1905009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1906009ea47eSEdward Tomasz Napierala 		}
1907009ea47eSEdward Tomasz Napierala 	}
1908009ea47eSEdward Tomasz Napierala 
1909009ea47eSEdward Tomasz Napierala 	return (0);
1910009ea47eSEdward Tomasz Napierala }
1911009ea47eSEdward Tomasz Napierala 
1912009ea47eSEdward Tomasz Napierala static int
1913009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1914009ea47eSEdward Tomasz Napierala {
1915009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1916009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1917009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
191892847ee1SAlexander Motin 	struct port *oldport, *newport, *tmpport;
1919829603e2SAlexander Motin 	struct isns *oldns, *newns;
1920009ea47eSEdward Tomasz Napierala 	pid_t otherpid;
19218b94b583SAlexander Motin 	int changed, cumulated_error = 0, error, sockbuf;
1922009ea47eSEdward Tomasz Napierala 	int one = 1;
1923009ea47eSEdward Tomasz Napierala 
1924009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1925009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1926009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1927009ea47eSEdward Tomasz Napierala 	}
1928009ea47eSEdward Tomasz Napierala 
1929009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_pidfh != NULL) {
1930009ea47eSEdward Tomasz Napierala 		assert(oldconf->conf_pidfile_path != NULL);
1931009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfile_path != NULL &&
1932009ea47eSEdward Tomasz Napierala 		    strcmp(oldconf->conf_pidfile_path,
1933009ea47eSEdward Tomasz Napierala 		    newconf->conf_pidfile_path) == 0) {
1934009ea47eSEdward Tomasz Napierala 			newconf->conf_pidfh = oldconf->conf_pidfh;
1935009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1936009ea47eSEdward Tomasz Napierala 		} else {
1937009ea47eSEdward Tomasz Napierala 			log_debugx("removing pidfile %s",
1938009ea47eSEdward Tomasz Napierala 			    oldconf->conf_pidfile_path);
1939009ea47eSEdward Tomasz Napierala 			pidfile_remove(oldconf->conf_pidfh);
1940009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1941009ea47eSEdward Tomasz Napierala 		}
1942009ea47eSEdward Tomasz Napierala 	}
1943009ea47eSEdward Tomasz Napierala 
1944009ea47eSEdward Tomasz Napierala 	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1945009ea47eSEdward Tomasz Napierala 		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1946009ea47eSEdward Tomasz Napierala 		newconf->conf_pidfh =
1947009ea47eSEdward Tomasz Napierala 		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1948009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfh == NULL) {
1949009ea47eSEdward Tomasz Napierala 			if (errno == EEXIST)
1950009ea47eSEdward Tomasz Napierala 				log_errx(1, "daemon already running, pid: %jd.",
1951009ea47eSEdward Tomasz Napierala 				    (intmax_t)otherpid);
1952009ea47eSEdward Tomasz Napierala 			log_err(1, "cannot open or create pidfile \"%s\"",
1953009ea47eSEdward Tomasz Napierala 			    newconf->conf_pidfile_path);
1954009ea47eSEdward Tomasz Napierala 		}
1955009ea47eSEdward Tomasz Napierala 	}
1956009ea47eSEdward Tomasz Napierala 
19576b31e130SAlexander Motin 	/*
19586b31e130SAlexander Motin 	 * Go through the new portal groups, assigning tags or preserving old.
19596b31e130SAlexander Motin 	 */
19606b31e130SAlexander Motin 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1961db7bf2baSAlexander Motin 		if (newpg->pg_tag != 0)
1962db7bf2baSAlexander Motin 			continue;
19636b31e130SAlexander Motin 		oldpg = portal_group_find(oldconf, newpg->pg_name);
19646b31e130SAlexander Motin 		if (oldpg != NULL)
19656b31e130SAlexander Motin 			newpg->pg_tag = oldpg->pg_tag;
19666b31e130SAlexander Motin 		else
19676b31e130SAlexander Motin 			newpg->pg_tag = ++last_portal_group_tag;
19686b31e130SAlexander Motin 	}
19696b31e130SAlexander Motin 
1970829603e2SAlexander Motin 	/* Deregister on removed iSNS servers. */
1971829603e2SAlexander Motin 	TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1972829603e2SAlexander Motin 		TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1973829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1974829603e2SAlexander Motin 				break;
1975829603e2SAlexander Motin 		}
1976829603e2SAlexander Motin 		if (newns == NULL)
1977829603e2SAlexander Motin 			isns_deregister(oldns);
1978829603e2SAlexander Motin 	}
1979829603e2SAlexander Motin 
1980d5e316e5SEdward Tomasz Napierala 	/*
1981d5e316e5SEdward Tomasz Napierala 	 * XXX: If target or lun removal fails, we should somehow "move"
1982d5e316e5SEdward Tomasz Napierala 	 *      the old lun or target into newconf, so that subsequent
198361a2a354SEdward Tomasz Napierala 	 *      conf_apply() would try to remove them again.  That would
198461a2a354SEdward Tomasz Napierala 	 *      be somewhat hairy, though, and lun deletion failures don't
198561a2a354SEdward Tomasz Napierala 	 *      really happen, so leave it as it is for now.
1986d5e316e5SEdward Tomasz Napierala 	 */
1987009ea47eSEdward Tomasz Napierala 	/*
198892847ee1SAlexander Motin 	 * First, remove any ports present in the old configuration
1989009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
1990009ea47eSEdward Tomasz Napierala 	 */
199192847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
19929215e501SAlexander Motin 		if (port_is_dummy(oldport))
1993db7bf2baSAlexander Motin 			continue;
199492847ee1SAlexander Motin 		newport = port_find(newconf, oldport->p_name);
19959215e501SAlexander Motin 		if (newport != NULL && !port_is_dummy(newport))
1996920c6cbaSAlexander Motin 			continue;
1997057abcb0SAlexander Motin 		log_debugx("removing port \"%s\"", oldport->p_name);
199892847ee1SAlexander Motin 		error = kernel_port_remove(oldport);
19996de78f9fSAlexander Motin 		if (error != 0) {
200092847ee1SAlexander Motin 			log_warnx("failed to remove port %s",
200192847ee1SAlexander Motin 			    oldport->p_name);
20026de78f9fSAlexander Motin 			/*
20036de78f9fSAlexander Motin 			 * XXX: Uncomment after fixing the root cause.
20046de78f9fSAlexander Motin 			 *
20056de78f9fSAlexander Motin 			 * cumulated_error++;
20066de78f9fSAlexander Motin 			 */
20076de78f9fSAlexander Motin 		}
2008009ea47eSEdward Tomasz Napierala 	}
2009009ea47eSEdward Tomasz Napierala 
2010009ea47eSEdward Tomasz Napierala 	/*
2011920c6cbaSAlexander Motin 	 * Second, remove any LUNs present in the old configuration
2012009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
2013009ea47eSEdward Tomasz Napierala 	 */
2014920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
2015920c6cbaSAlexander Motin 		newlun = lun_find(newconf, oldlun->l_name);
2016009ea47eSEdward Tomasz Napierala 		if (newlun == NULL) {
2017920c6cbaSAlexander Motin 			log_debugx("lun \"%s\", CTL lun %d "
2018cadf3831SEdward Tomasz Napierala 			    "not found in new configuration; "
2019920c6cbaSAlexander Motin 			    "removing", oldlun->l_name, oldlun->l_ctl_lun);
2020009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
2021009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2022920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
2023920c6cbaSAlexander Motin 				    "CTL lun %d",
2024920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
2025009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2026009ea47eSEdward Tomasz Napierala 			}
2027009ea47eSEdward Tomasz Napierala 			continue;
2028009ea47eSEdward Tomasz Napierala 		}
2029009ea47eSEdward Tomasz Napierala 
2030009ea47eSEdward Tomasz Napierala 		/*
2031009ea47eSEdward Tomasz Napierala 		 * Also remove the LUNs changed by more than size.
2032009ea47eSEdward Tomasz Napierala 		 */
2033009ea47eSEdward Tomasz Napierala 		changed = 0;
2034009ea47eSEdward Tomasz Napierala 		assert(oldlun->l_backend != NULL);
2035009ea47eSEdward Tomasz Napierala 		assert(newlun->l_backend != NULL);
2036009ea47eSEdward Tomasz Napierala 		if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
2037920c6cbaSAlexander Motin 			log_debugx("backend for lun \"%s\", "
2038009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2039920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2040009ea47eSEdward Tomasz Napierala 			changed = 1;
2041009ea47eSEdward Tomasz Napierala 		}
2042009ea47eSEdward Tomasz Napierala 		if (oldlun->l_blocksize != newlun->l_blocksize) {
2043920c6cbaSAlexander Motin 			log_debugx("blocksize for lun \"%s\", "
2044009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2045920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2046009ea47eSEdward Tomasz Napierala 			changed = 1;
2047009ea47eSEdward Tomasz Napierala 		}
2048009ea47eSEdward Tomasz Napierala 		if (newlun->l_device_id != NULL &&
2049009ea47eSEdward Tomasz Napierala 		    (oldlun->l_device_id == NULL ||
2050009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
2051009ea47eSEdward Tomasz Napierala 		     0)) {
2052920c6cbaSAlexander Motin 			log_debugx("device-id for lun \"%s\", "
2053009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2054920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2055009ea47eSEdward Tomasz Napierala 			changed = 1;
2056009ea47eSEdward Tomasz Napierala 		}
2057009ea47eSEdward Tomasz Napierala 		if (newlun->l_path != NULL &&
2058009ea47eSEdward Tomasz Napierala 		    (oldlun->l_path == NULL ||
2059009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
2060920c6cbaSAlexander Motin 			log_debugx("path for lun \"%s\", "
2061009ea47eSEdward Tomasz Napierala 			    "CTL lun %d, changed; removing",
2062920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2063009ea47eSEdward Tomasz Napierala 			changed = 1;
2064009ea47eSEdward Tomasz Napierala 		}
2065009ea47eSEdward Tomasz Napierala 		if (newlun->l_serial != NULL &&
2066009ea47eSEdward Tomasz Napierala 		    (oldlun->l_serial == NULL ||
2067009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
2068920c6cbaSAlexander Motin 			log_debugx("serial for lun \"%s\", "
2069009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2070920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2071009ea47eSEdward Tomasz Napierala 			changed = 1;
2072009ea47eSEdward Tomasz Napierala 		}
2073009ea47eSEdward Tomasz Napierala 		if (changed) {
2074009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
2075009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2076920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
2077920c6cbaSAlexander Motin 				    "CTL lun %d",
2078920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
2079009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2080009ea47eSEdward Tomasz Napierala 			}
2081009ea47eSEdward Tomasz Napierala 			lun_delete(oldlun);
2082009ea47eSEdward Tomasz Napierala 			continue;
2083009ea47eSEdward Tomasz Napierala 		}
2084009ea47eSEdward Tomasz Napierala 
2085009ea47eSEdward Tomasz Napierala 		lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
2086009ea47eSEdward Tomasz Napierala 	}
2087920c6cbaSAlexander Motin 
2088920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
2089920c6cbaSAlexander Motin 		oldlun = lun_find(oldconf, newlun->l_name);
2090920c6cbaSAlexander Motin 		if (oldlun != NULL) {
2091a3977beaSAlexander Motin 			log_debugx("modifying lun \"%s\", CTL lun %d",
2092920c6cbaSAlexander Motin 			    newlun->l_name, newlun->l_ctl_lun);
2093a3977beaSAlexander Motin 			error = kernel_lun_modify(newlun);
2094920c6cbaSAlexander Motin 			if (error != 0) {
2095920c6cbaSAlexander Motin 				log_warnx("failed to "
2096a3977beaSAlexander Motin 				    "modify lun \"%s\", CTL lun %d",
2097a3977beaSAlexander Motin 				    newlun->l_name, newlun->l_ctl_lun);
2098920c6cbaSAlexander Motin 				cumulated_error++;
2099920c6cbaSAlexander Motin 			}
2100920c6cbaSAlexander Motin 			continue;
2101920c6cbaSAlexander Motin 		}
2102920c6cbaSAlexander Motin 		log_debugx("adding lun \"%s\"", newlun->l_name);
2103920c6cbaSAlexander Motin 		error = kernel_lun_add(newlun);
2104920c6cbaSAlexander Motin 		if (error != 0) {
2105920c6cbaSAlexander Motin 			log_warnx("failed to add lun \"%s\"", newlun->l_name);
2106920c6cbaSAlexander Motin 			lun_delete(newlun);
2107920c6cbaSAlexander Motin 			cumulated_error++;
2108920c6cbaSAlexander Motin 		}
2109009ea47eSEdward Tomasz Napierala 	}
2110009ea47eSEdward Tomasz Napierala 
2111009ea47eSEdward Tomasz Napierala 	/*
211292847ee1SAlexander Motin 	 * Now add new ports or modify existing ones.
2113009ea47eSEdward Tomasz Napierala 	 */
211415b3e3bbSAlan Somers 	TAILQ_FOREACH_SAFE(newport, &newconf->conf_ports, p_next, tmpport) {
21159215e501SAlexander Motin 		if (port_is_dummy(newport))
2116db7bf2baSAlexander Motin 			continue;
211792847ee1SAlexander Motin 		oldport = port_find(oldconf, newport->p_name);
2118009ea47eSEdward Tomasz Napierala 
21199215e501SAlexander Motin 		if (oldport == NULL || port_is_dummy(oldport)) {
2120057abcb0SAlexander Motin 			log_debugx("adding port \"%s\"", newport->p_name);
212192847ee1SAlexander Motin 			error = kernel_port_add(newport);
212292847ee1SAlexander Motin 		} else {
2123057abcb0SAlexander Motin 			log_debugx("updating port \"%s\"", newport->p_name);
212492847ee1SAlexander Motin 			newport->p_ctl_port = oldport->p_ctl_port;
2125828524c1SAlexander Motin 			error = kernel_port_update(newport, oldport);
2126920c6cbaSAlexander Motin 		}
2127e48bb73aSEdward Tomasz Napierala 		if (error != 0) {
212892847ee1SAlexander Motin 			log_warnx("failed to %s port %s",
212992847ee1SAlexander Motin 			    (oldport == NULL) ? "add" : "update",
213092847ee1SAlexander Motin 			    newport->p_name);
213115b3e3bbSAlan Somers 			if (oldport == NULL || port_is_dummy(oldport))
213215b3e3bbSAlan Somers 				port_delete(newport);
2133e48bb73aSEdward Tomasz Napierala 			/*
2134e48bb73aSEdward Tomasz Napierala 			 * XXX: Uncomment after fixing the root cause.
2135e48bb73aSEdward Tomasz Napierala 			 *
2136e48bb73aSEdward Tomasz Napierala 			 * cumulated_error++;
2137e48bb73aSEdward Tomasz Napierala 			 */
2138e48bb73aSEdward Tomasz Napierala 		}
2139e48bb73aSEdward Tomasz Napierala 	}
2140009ea47eSEdward Tomasz Napierala 
2141009ea47eSEdward Tomasz Napierala 	/*
2142463a577bSEitan Adler 	 * Go through the new portals, opening the sockets as necessary.
2143009ea47eSEdward Tomasz Napierala 	 */
2144009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2145db7bf2baSAlexander Motin 		if (newpg->pg_foreign)
2146db7bf2baSAlexander Motin 			continue;
2147009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
2148009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
2149009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
2150009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
2151009ea47eSEdward Tomasz Napierala 			continue;
2152009ea47eSEdward Tomasz Napierala 		}
2153009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2154009ea47eSEdward Tomasz Napierala 			/*
2155009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
2156009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
2157009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
2158009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
2159009ea47eSEdward Tomasz Napierala 			 */
2160009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2161009ea47eSEdward Tomasz Napierala 			    pg_next) {
2162009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2163009ea47eSEdward Tomasz Napierala 				    p_next) {
2164009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
2165009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
2166009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
2167009ea47eSEdward Tomasz Napierala 						newp->p_socket =
2168009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
2169009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
2170009ea47eSEdward Tomasz Napierala 						break;
2171009ea47eSEdward Tomasz Napierala 					}
2172009ea47eSEdward Tomasz Napierala 				}
2173009ea47eSEdward Tomasz Napierala 			}
2174009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
2175009ea47eSEdward Tomasz Napierala 				/*
2176009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
2177009ea47eSEdward Tomasz Napierala 				 */
2178009ea47eSEdward Tomasz Napierala 				continue;
2179009ea47eSEdward Tomasz Napierala 			}
2180009ea47eSEdward Tomasz Napierala 
2181009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2182ba3a2d31SEdward Tomasz Napierala 			if (proxy_mode) {
21838cab2ed4SEdward Tomasz Napierala 				newpg->pg_conf->conf_portal_id++;
21848cab2ed4SEdward Tomasz Napierala 				newp->p_id = newpg->pg_conf->conf_portal_id;
21858cab2ed4SEdward Tomasz Napierala 				log_debugx("listening on %s, portal-group "
21868cab2ed4SEdward Tomasz Napierala 				    "\"%s\", portal id %d, using ICL proxy",
21878cab2ed4SEdward Tomasz Napierala 				    newp->p_listen, newpg->pg_name, newp->p_id);
21888cab2ed4SEdward Tomasz Napierala 				kernel_listen(newp->p_ai, newp->p_iser,
21898cab2ed4SEdward Tomasz Napierala 				    newp->p_id);
2190ba3a2d31SEdward Tomasz Napierala 				continue;
2191ba3a2d31SEdward Tomasz Napierala 			}
2192ba3a2d31SEdward Tomasz Napierala #endif
2193ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2194009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
2195009ea47eSEdward Tomasz Napierala 
2196009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
2197009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
2198009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
2199009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
2200009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
2201009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
2202009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
2203009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2204009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2205009ea47eSEdward Tomasz Napierala 				continue;
2206009ea47eSEdward Tomasz Napierala 			}
22078b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
22088b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
22098b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
22108b94b583SAlexander Motin 				log_warn("setsockopt(SO_RCVBUF) failed "
22118b94b583SAlexander Motin 				    "for %s", newp->p_listen);
22128b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
22138b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
22148b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
22158b94b583SAlexander Motin 				log_warn("setsockopt(SO_SNDBUF) failed "
22168b94b583SAlexander Motin 				    "for %s", newp->p_listen);
22173b5f95d7SJohn Baldwin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_NO_DDP,
22183b5f95d7SJohn Baldwin 			    &one, sizeof(one)) == -1)
22193b5f95d7SJohn Baldwin 				log_warn("setsockopt(SO_NO_DDP) failed "
22203b5f95d7SJohn Baldwin 				    "for %s", newp->p_listen);
2221009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
2222009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
2223009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2224009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
2225009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
2226009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2227009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2228009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2229009ea47eSEdward Tomasz Napierala 				continue;
2230009ea47eSEdward Tomasz Napierala 			}
223164ffe6d4SRichard Scheffenegger 			if (newpg->pg_dscp != -1) {
223264ffe6d4SRichard Scheffenegger 				struct sockaddr sa;
223364ffe6d4SRichard Scheffenegger 				int len = sizeof(sa);
223464ffe6d4SRichard Scheffenegger 				getsockname(newp->p_socket, &sa, &len);
223564ffe6d4SRichard Scheffenegger 				/*
223664ffe6d4SRichard Scheffenegger 				 * Only allow the 6-bit DSCP
223764ffe6d4SRichard Scheffenegger 				 * field to be modified
223864ffe6d4SRichard Scheffenegger 				 */
223964ffe6d4SRichard Scheffenegger 				int tos = newpg->pg_dscp << 2;
224064ffe6d4SRichard Scheffenegger 				if (sa.sa_family == AF_INET) {
224164ffe6d4SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
224264ffe6d4SRichard Scheffenegger 					    IPPROTO_IP, IP_TOS,
224364ffe6d4SRichard Scheffenegger 					    &tos, sizeof(tos)) == -1)
224464ffe6d4SRichard Scheffenegger 						log_warn("setsockopt(IP_TOS) "
224564ffe6d4SRichard Scheffenegger 						    "failed for %s",
224664ffe6d4SRichard Scheffenegger 						    newp->p_listen);
224764ffe6d4SRichard Scheffenegger 				} else
224864ffe6d4SRichard Scheffenegger 				if (sa.sa_family == AF_INET6) {
224964ffe6d4SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
225064ffe6d4SRichard Scheffenegger 					    IPPROTO_IPV6, IPV6_TCLASS,
225164ffe6d4SRichard Scheffenegger 					    &tos, sizeof(tos)) == -1)
225264ffe6d4SRichard Scheffenegger 						log_warn("setsockopt(IPV6_TCLASS) "
225364ffe6d4SRichard Scheffenegger 						    "failed for %s",
225464ffe6d4SRichard Scheffenegger 						    newp->p_listen);
225564ffe6d4SRichard Scheffenegger 				}
225664ffe6d4SRichard Scheffenegger 			}
2257ea8f1280SRichard Scheffenegger 			if (newpg->pg_pcp != -1) {
2258ea8f1280SRichard Scheffenegger 				struct sockaddr sa;
2259ea8f1280SRichard Scheffenegger 				int len = sizeof(sa);
2260ea8f1280SRichard Scheffenegger 				getsockname(newp->p_socket, &sa, &len);
2261ea8f1280SRichard Scheffenegger 				/*
2262ea8f1280SRichard Scheffenegger 				 * Only allow the 6-bit DSCP
2263ea8f1280SRichard Scheffenegger 				 * field to be modified
2264ea8f1280SRichard Scheffenegger 				 */
2265ea8f1280SRichard Scheffenegger 				int pcp = newpg->pg_pcp;
2266ea8f1280SRichard Scheffenegger 				if (sa.sa_family == AF_INET) {
2267ea8f1280SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
2268ea8f1280SRichard Scheffenegger 					    IPPROTO_IP, IP_VLAN_PCP,
2269ea8f1280SRichard Scheffenegger 					    &pcp, sizeof(pcp)) == -1)
2270ea8f1280SRichard Scheffenegger 						log_warn("setsockopt(IP_VLAN_PCP) "
2271ea8f1280SRichard Scheffenegger 						    "failed for %s",
2272ea8f1280SRichard Scheffenegger 						    newp->p_listen);
2273ea8f1280SRichard Scheffenegger 				} else
2274ea8f1280SRichard Scheffenegger 				if (sa.sa_family == AF_INET6) {
2275ea8f1280SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
2276ea8f1280SRichard Scheffenegger 					    IPPROTO_IPV6, IPV6_VLAN_PCP,
2277ea8f1280SRichard Scheffenegger 					    &pcp, sizeof(pcp)) == -1)
2278ea8f1280SRichard Scheffenegger 						log_warn("setsockopt(IPV6_VLAN_PCP) "
2279ea8f1280SRichard Scheffenegger 						    "failed for %s",
2280ea8f1280SRichard Scheffenegger 						    newp->p_listen);
2281ea8f1280SRichard Scheffenegger 				}
2282ea8f1280SRichard Scheffenegger 			}
2283009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
2284009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
2285009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2286009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
2287009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2288009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2289009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2290009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2291009ea47eSEdward Tomasz Napierala 				continue;
2292009ea47eSEdward Tomasz Napierala 			}
2293009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
2294009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2295009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
2296009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2297009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2298009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2299009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2300009ea47eSEdward Tomasz Napierala 				continue;
2301009ea47eSEdward Tomasz Napierala 			}
2302009ea47eSEdward Tomasz Napierala 		}
2303009ea47eSEdward Tomasz Napierala 	}
2304009ea47eSEdward Tomasz Napierala 
2305009ea47eSEdward Tomasz Napierala 	/*
2306009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
2307009ea47eSEdward Tomasz Napierala 	 */
2308009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2309009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2310009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
2311009ea47eSEdward Tomasz Napierala 				continue;
2312009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
2313009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
2314009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
2315009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
2316009ea47eSEdward Tomasz Napierala 		}
2317009ea47eSEdward Tomasz Napierala 	}
2318009ea47eSEdward Tomasz Napierala 
2319829603e2SAlexander Motin 	/* (Re-)Register on remaining/new iSNS servers. */
2320829603e2SAlexander Motin 	TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2321829603e2SAlexander Motin 		TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2322829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2323829603e2SAlexander Motin 				break;
2324829603e2SAlexander Motin 		}
2325829603e2SAlexander Motin 		isns_register(newns, oldns);
2326829603e2SAlexander Motin 	}
2327829603e2SAlexander Motin 
2328829603e2SAlexander Motin 	/* Schedule iSNS update */
2329829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2330829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2331829603e2SAlexander Motin 
2332009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
2333009ea47eSEdward Tomasz Napierala }
2334009ea47eSEdward Tomasz Napierala 
233563783933SJohn Baldwin static bool
2336009ea47eSEdward Tomasz Napierala timed_out(void)
2337009ea47eSEdward Tomasz Napierala {
2338009ea47eSEdward Tomasz Napierala 
2339009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
2340009ea47eSEdward Tomasz Napierala }
2341009ea47eSEdward Tomasz Napierala 
2342009ea47eSEdward Tomasz Napierala static void
2343829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused)
2344009ea47eSEdward Tomasz Napierala {
2345009ea47eSEdward Tomasz Napierala 	/*
2346009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
2347009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
2348009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
2349009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
2350009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
2351009ea47eSEdward Tomasz Napierala 	 */
2352009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
2353009ea47eSEdward Tomasz Napierala 		/*
2354009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
2355009ea47eSEdward Tomasz Napierala 		 */
2356009ea47eSEdward Tomasz Napierala 		_exit(2);
2357009ea47eSEdward Tomasz Napierala 	}
2358009ea47eSEdward Tomasz Napierala 
2359009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
2360009ea47eSEdward Tomasz Napierala }
2361009ea47eSEdward Tomasz Napierala 
2362009ea47eSEdward Tomasz Napierala static void
2363829603e2SAlexander Motin sigalrm_handler(int dummy __unused)
2364829603e2SAlexander Motin {
2365829603e2SAlexander Motin 
2366829603e2SAlexander Motin 	sigalrm_received = true;
2367829603e2SAlexander Motin }
2368829603e2SAlexander Motin 
2369829603e2SAlexander Motin void
2370829603e2SAlexander Motin set_timeout(int timeout, int fatal)
2371009ea47eSEdward Tomasz Napierala {
2372009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2373009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
2374009ea47eSEdward Tomasz Napierala 	int error;
2375009ea47eSEdward Tomasz Napierala 
2376829603e2SAlexander Motin 	if (timeout <= 0) {
2377009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
2378829603e2SAlexander Motin 		bzero(&itv, sizeof(itv));
2379829603e2SAlexander Motin 		error = setitimer(ITIMER_REAL, &itv, NULL);
2380829603e2SAlexander Motin 		if (error != 0)
2381829603e2SAlexander Motin 			log_err(1, "setitimer");
2382829603e2SAlexander Motin 		sigalrm_received = false;
2383009ea47eSEdward Tomasz Napierala 		return;
2384009ea47eSEdward Tomasz Napierala 	}
2385009ea47eSEdward Tomasz Napierala 
2386829603e2SAlexander Motin 	sigalrm_received = false;
2387009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2388829603e2SAlexander Motin 	if (fatal)
2389829603e2SAlexander Motin 		sa.sa_handler = sigalrm_handler_fatal;
2390829603e2SAlexander Motin 	else
2391009ea47eSEdward Tomasz Napierala 		sa.sa_handler = sigalrm_handler;
2392009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2393009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
2394009ea47eSEdward Tomasz Napierala 	if (error != 0)
2395009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2396009ea47eSEdward Tomasz Napierala 
2397009ea47eSEdward Tomasz Napierala 	/*
2398009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
2399009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
2400009ea47eSEdward Tomasz Napierala 	 */
2401829603e2SAlexander Motin 	log_debugx("setting session timeout to %d seconds", timeout);
2402009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
2403009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
2404829603e2SAlexander Motin 	itv.it_value.tv_sec = timeout;
2405009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
2406009ea47eSEdward Tomasz Napierala 	if (error != 0)
2407009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
2408009ea47eSEdward Tomasz Napierala }
2409009ea47eSEdward Tomasz Napierala 
2410009ea47eSEdward Tomasz Napierala static int
2411009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
2412009ea47eSEdward Tomasz Napierala {
2413009ea47eSEdward Tomasz Napierala 	pid_t pid;
2414009ea47eSEdward Tomasz Napierala 	int status;
2415009ea47eSEdward Tomasz Napierala 	int num = 0;
2416009ea47eSEdward Tomasz Napierala 
2417009ea47eSEdward Tomasz Napierala 	for (;;) {
2418009ea47eSEdward Tomasz Napierala 		/*
2419009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
2420009ea47eSEdward Tomasz Napierala 		 */
2421009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
2422009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
2423009ea47eSEdward Tomasz Napierala 		else
2424009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
2425009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
2426009ea47eSEdward Tomasz Napierala 			break;
2427009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
2428009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
2429009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
2430009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
2431009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
2432009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
2433009ea47eSEdward Tomasz Napierala 		} else {
2434009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
2435009ea47eSEdward Tomasz Napierala 		}
2436009ea47eSEdward Tomasz Napierala 		num++;
2437009ea47eSEdward Tomasz Napierala 	}
2438009ea47eSEdward Tomasz Napierala 
2439009ea47eSEdward Tomasz Napierala 	return (num);
2440009ea47eSEdward Tomasz Napierala }
2441009ea47eSEdward Tomasz Napierala 
2442009ea47eSEdward Tomasz Napierala static void
24438eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd,
2444073edb1cSAlexander Motin     const struct sockaddr *client_sa, bool dont_fork)
2445009ea47eSEdward Tomasz Napierala {
244663783933SJohn Baldwin 	struct ctld_connection *conn;
2447009ea47eSEdward Tomasz Napierala 	int error;
2448009ea47eSEdward Tomasz Napierala 	pid_t pid;
2449009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
2450009ea47eSEdward Tomasz Napierala 	struct conf *conf;
2451009ea47eSEdward Tomasz Napierala 
2452009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
2453009ea47eSEdward Tomasz Napierala 
2454009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
2455009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
2456009ea47eSEdward Tomasz Napierala 	} else {
2457009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
2458009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
2459009ea47eSEdward Tomasz Napierala 
2460009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2461009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
2462009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
2463009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
2464009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2465009ea47eSEdward Tomasz Napierala 		}
2466009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
2467009ea47eSEdward Tomasz Napierala 		    nchildren);
2468009ea47eSEdward Tomasz Napierala 		nchildren++;
2469009ea47eSEdward Tomasz Napierala 		pid = fork();
2470009ea47eSEdward Tomasz Napierala 		if (pid < 0)
2471009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
2472009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
2473009ea47eSEdward Tomasz Napierala 			close(fd);
2474009ea47eSEdward Tomasz Napierala 			return;
2475009ea47eSEdward Tomasz Napierala 		}
2476009ea47eSEdward Tomasz Napierala 	}
2477009ea47eSEdward Tomasz Napierala 	pidfile_close(conf->conf_pidfh);
2478009ea47eSEdward Tomasz Napierala 
2479073edb1cSAlexander Motin 	error = getnameinfo(client_sa, client_sa->sa_len,
2480009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2481009ea47eSEdward Tomasz Napierala 	if (error != 0)
24829a95cfffSEdward Tomasz Napierala 		log_errx(1, "getnameinfo: %s", gai_strerror(error));
2483009ea47eSEdward Tomasz Napierala 
2484009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
2485009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
2486009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
2487009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
2488009ea47eSEdward Tomasz Napierala 
2489073edb1cSAlexander Motin 	conn = connection_new(portal, fd, host, client_sa);
2490829603e2SAlexander Motin 	set_timeout(conf->conf_timeout, true);
2491009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
2492009ea47eSEdward Tomasz Napierala 	login(conn);
2493009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2494009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
2495009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
2496009ea47eSEdward Tomasz Napierala 	} else {
2497009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2498009ea47eSEdward Tomasz Napierala 		discovery(conn);
2499009ea47eSEdward Tomasz Napierala 	}
2500009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
2501009ea47eSEdward Tomasz Napierala 	exit(0);
2502009ea47eSEdward Tomasz Napierala }
2503009ea47eSEdward Tomasz Napierala 
2504009ea47eSEdward Tomasz Napierala static int
2505009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
2506009ea47eSEdward Tomasz Napierala {
2507009ea47eSEdward Tomasz Napierala 
2508009ea47eSEdward Tomasz Napierala 	/*
2509009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
2510009ea47eSEdward Tomasz Napierala 	 */
2511009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
2512009ea47eSEdward Tomasz Napierala 		return (nfds);
2513009ea47eSEdward Tomasz Napierala 
2514009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
2515009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
2516009ea47eSEdward Tomasz Napierala 		nfds = fd;
2517009ea47eSEdward Tomasz Napierala 	return (nfds);
2518009ea47eSEdward Tomasz Napierala }
2519009ea47eSEdward Tomasz Napierala 
2520009ea47eSEdward Tomasz Napierala static void
2521009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
2522009ea47eSEdward Tomasz Napierala {
2523009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
2524009ea47eSEdward Tomasz Napierala 	struct portal *portal;
25259a95cfffSEdward Tomasz Napierala 	struct sockaddr_storage client_sa;
25269a95cfffSEdward Tomasz Napierala 	socklen_t client_salen;
2527009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2528009ea47eSEdward Tomasz Napierala 	int connection_id;
25298cab2ed4SEdward Tomasz Napierala 	int portal_id;
2530ba3a2d31SEdward Tomasz Napierala #endif
2531009ea47eSEdward Tomasz Napierala 	fd_set fdset;
2532009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
2533009ea47eSEdward Tomasz Napierala 
2534009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
2535009ea47eSEdward Tomasz Napierala 
2536009ea47eSEdward Tomasz Napierala 	for (;;) {
2537829603e2SAlexander Motin 		if (sighup_received || sigterm_received || timed_out())
2538009ea47eSEdward Tomasz Napierala 			return;
2539009ea47eSEdward Tomasz Napierala 
2540009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2541ba3a2d31SEdward Tomasz Napierala 		if (proxy_mode) {
25428eab95d6SEdward Tomasz Napierala 			client_salen = sizeof(client_sa);
25438eab95d6SEdward Tomasz Napierala 			kernel_accept(&connection_id, &portal_id,
25448eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, &client_salen);
25450488e848SEdward Tomasz Napierala 			assert(client_salen >= client_sa.ss_len);
2546009ea47eSEdward Tomasz Napierala 
25478cab2ed4SEdward Tomasz Napierala 			log_debugx("incoming connection, id %d, portal id %d",
25488cab2ed4SEdward Tomasz Napierala 			    connection_id, portal_id);
25498cab2ed4SEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
25508cab2ed4SEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
25518cab2ed4SEdward Tomasz Napierala 					if (portal->p_id == portal_id) {
25528cab2ed4SEdward Tomasz Napierala 						goto found;
25538cab2ed4SEdward Tomasz Napierala 					}
25548cab2ed4SEdward Tomasz Napierala 				}
25558cab2ed4SEdward Tomasz Napierala 			}
2556009ea47eSEdward Tomasz Napierala 
25578cab2ed4SEdward Tomasz Napierala 			log_errx(1, "kernel returned invalid portal_id %d",
25588cab2ed4SEdward Tomasz Napierala 			    portal_id);
25598cab2ed4SEdward Tomasz Napierala 
25608cab2ed4SEdward Tomasz Napierala found:
25618eab95d6SEdward Tomasz Napierala 			handle_connection(portal, connection_id,
2562073edb1cSAlexander Motin 			    (struct sockaddr *)&client_sa, dont_fork);
2563ba3a2d31SEdward Tomasz Napierala 		} else {
2564ba3a2d31SEdward Tomasz Napierala #endif
2565ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2566ba3a2d31SEdward Tomasz Napierala 
2567009ea47eSEdward Tomasz Napierala 			FD_ZERO(&fdset);
2568009ea47eSEdward Tomasz Napierala 			nfds = 0;
2569009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2570009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2571009ea47eSEdward Tomasz Napierala 					nfds = fd_add(portal->p_socket, &fdset, nfds);
2572009ea47eSEdward Tomasz Napierala 			}
2573009ea47eSEdward Tomasz Napierala 			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2574009ea47eSEdward Tomasz Napierala 			if (error <= 0) {
2575009ea47eSEdward Tomasz Napierala 				if (errno == EINTR)
2576009ea47eSEdward Tomasz Napierala 					return;
2577009ea47eSEdward Tomasz Napierala 				log_err(1, "select");
2578009ea47eSEdward Tomasz Napierala 			}
2579009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2580009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2581009ea47eSEdward Tomasz Napierala 					if (!FD_ISSET(portal->p_socket, &fdset))
2582009ea47eSEdward Tomasz Napierala 						continue;
25839a95cfffSEdward Tomasz Napierala 					client_salen = sizeof(client_sa);
25849a95cfffSEdward Tomasz Napierala 					client_fd = accept(portal->p_socket,
25859a95cfffSEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
25869a95cfffSEdward Tomasz Napierala 					    &client_salen);
258783dd77c3SAlexander Motin 					if (client_fd < 0) {
258883dd77c3SAlexander Motin 						if (errno == ECONNABORTED)
258983dd77c3SAlexander Motin 							continue;
2590009ea47eSEdward Tomasz Napierala 						log_err(1, "accept");
259183dd77c3SAlexander Motin 					}
25920488e848SEdward Tomasz Napierala 					assert(client_salen >= client_sa.ss_len);
25930488e848SEdward Tomasz Napierala 
25949a95cfffSEdward Tomasz Napierala 					handle_connection(portal, client_fd,
25958eab95d6SEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
2596073edb1cSAlexander Motin 					    dont_fork);
2597009ea47eSEdward Tomasz Napierala 					break;
2598009ea47eSEdward Tomasz Napierala 				}
2599009ea47eSEdward Tomasz Napierala 			}
2600ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2601ba3a2d31SEdward Tomasz Napierala 		}
2602ba3a2d31SEdward Tomasz Napierala #endif
2603009ea47eSEdward Tomasz Napierala 	}
2604009ea47eSEdward Tomasz Napierala }
2605009ea47eSEdward Tomasz Napierala 
2606009ea47eSEdward Tomasz Napierala static void
2607009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
2608009ea47eSEdward Tomasz Napierala {
2609009ea47eSEdward Tomasz Napierala 
2610009ea47eSEdward Tomasz Napierala 	sighup_received = true;
2611009ea47eSEdward Tomasz Napierala }
2612009ea47eSEdward Tomasz Napierala 
2613009ea47eSEdward Tomasz Napierala static void
2614009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
2615009ea47eSEdward Tomasz Napierala {
2616009ea47eSEdward Tomasz Napierala 
2617009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
2618009ea47eSEdward Tomasz Napierala }
2619009ea47eSEdward Tomasz Napierala 
2620009ea47eSEdward Tomasz Napierala static void
262170b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused)
262270b939ecSEdward Tomasz Napierala {
262370b939ecSEdward Tomasz Napierala 
262470b939ecSEdward Tomasz Napierala 	/*
262570b939ecSEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
262670b939ecSEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
262770b939ecSEdward Tomasz Napierala 	 * wait_for_children().
262870b939ecSEdward Tomasz Napierala 	 */
262970b939ecSEdward Tomasz Napierala }
263070b939ecSEdward Tomasz Napierala 
263170b939ecSEdward Tomasz Napierala static void
2632009ea47eSEdward Tomasz Napierala register_signals(void)
2633009ea47eSEdward Tomasz Napierala {
2634009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2635009ea47eSEdward Tomasz Napierala 	int error;
2636009ea47eSEdward Tomasz Napierala 
2637009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2638009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
2639009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2640009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
2641009ea47eSEdward Tomasz Napierala 	if (error != 0)
2642009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2643009ea47eSEdward Tomasz Napierala 
2644009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2645009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
2646009ea47eSEdward Tomasz Napierala 	if (error != 0)
2647009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2648009ea47eSEdward Tomasz Napierala 
2649009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2650009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
2651009ea47eSEdward Tomasz Napierala 	if (error != 0)
2652009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
265370b939ecSEdward Tomasz Napierala 
265470b939ecSEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
265570b939ecSEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
265670b939ecSEdward Tomasz Napierala 	if (error != 0)
265770b939ecSEdward Tomasz Napierala 		log_err(1, "sigaction");
2658009ea47eSEdward Tomasz Napierala }
2659009ea47eSEdward Tomasz Napierala 
26605c734b04SJakub Wojciech Klama static void
26615c734b04SJakub Wojciech Klama check_perms(const char *path)
26625c734b04SJakub Wojciech Klama {
26635c734b04SJakub Wojciech Klama 	struct stat sb;
26645c734b04SJakub Wojciech Klama 	int error;
26655c734b04SJakub Wojciech Klama 
26665c734b04SJakub Wojciech Klama 	error = stat(path, &sb);
26675c734b04SJakub Wojciech Klama 	if (error != 0) {
26685c734b04SJakub Wojciech Klama 		log_warn("stat");
26695c734b04SJakub Wojciech Klama 		return;
26705c734b04SJakub Wojciech Klama 	}
26715c734b04SJakub Wojciech Klama 	if (sb.st_mode & S_IWOTH) {
26725c734b04SJakub Wojciech Klama 		log_warnx("%s is world-writable", path);
26735c734b04SJakub Wojciech Klama 	} else if (sb.st_mode & S_IROTH) {
26745c734b04SJakub Wojciech Klama 		log_warnx("%s is world-readable", path);
26755c734b04SJakub Wojciech Klama 	} else if (sb.st_mode & S_IXOTH) {
26765c734b04SJakub Wojciech Klama 		/*
26775c734b04SJakub Wojciech Klama 		 * Ok, this one doesn't matter, but still do it,
26785c734b04SJakub Wojciech Klama 		 * just for consistency.
26795c734b04SJakub Wojciech Klama 		 */
26805c734b04SJakub Wojciech Klama 		log_warnx("%s is world-executable", path);
26815c734b04SJakub Wojciech Klama 	}
26825c734b04SJakub Wojciech Klama 
26835c734b04SJakub Wojciech Klama 	/*
26845c734b04SJakub Wojciech Klama 	 * XXX: Should we also check for owner != 0?
26855c734b04SJakub Wojciech Klama 	 */
26865c734b04SJakub Wojciech Klama }
26875c734b04SJakub Wojciech Klama 
26885c734b04SJakub Wojciech Klama static struct conf *
26895c734b04SJakub Wojciech Klama conf_new_from_file(const char *path, struct conf *oldconf, bool ucl)
26905c734b04SJakub Wojciech Klama {
26915c734b04SJakub Wojciech Klama 	struct conf *conf;
26925c734b04SJakub Wojciech Klama 	struct auth_group *ag;
26935c734b04SJakub Wojciech Klama 	struct portal_group *pg;
26945c734b04SJakub Wojciech Klama 	struct pport *pp;
26955c734b04SJakub Wojciech Klama 	int error;
26965c734b04SJakub Wojciech Klama 
26975c734b04SJakub Wojciech Klama 	log_debugx("obtaining configuration from %s", path);
26985c734b04SJakub Wojciech Klama 
26995c734b04SJakub Wojciech Klama 	conf = conf_new();
27005c734b04SJakub Wojciech Klama 
27015c734b04SJakub Wojciech Klama 	TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next)
27025c734b04SJakub Wojciech Klama 		pport_copy(pp, conf);
27035c734b04SJakub Wojciech Klama 
27045c734b04SJakub Wojciech Klama 	ag = auth_group_new(conf, "default");
27055c734b04SJakub Wojciech Klama 	assert(ag != NULL);
27065c734b04SJakub Wojciech Klama 
27075c734b04SJakub Wojciech Klama 	ag = auth_group_new(conf, "no-authentication");
27085c734b04SJakub Wojciech Klama 	assert(ag != NULL);
27095c734b04SJakub Wojciech Klama 	ag->ag_type = AG_TYPE_NO_AUTHENTICATION;
27105c734b04SJakub Wojciech Klama 
27115c734b04SJakub Wojciech Klama 	ag = auth_group_new(conf, "no-access");
27125c734b04SJakub Wojciech Klama 	assert(ag != NULL);
27135c734b04SJakub Wojciech Klama 	ag->ag_type = AG_TYPE_DENY;
27145c734b04SJakub Wojciech Klama 
27155c734b04SJakub Wojciech Klama 	pg = portal_group_new(conf, "default");
27165c734b04SJakub Wojciech Klama 	assert(pg != NULL);
27175c734b04SJakub Wojciech Klama 
27185c734b04SJakub Wojciech Klama 	if (ucl)
27195c734b04SJakub Wojciech Klama 		error = uclparse_conf(conf, path);
27205c734b04SJakub Wojciech Klama 	else
27215c734b04SJakub Wojciech Klama 		error = parse_conf(conf, path);
27225c734b04SJakub Wojciech Klama 
27235c734b04SJakub Wojciech Klama 	if (error != 0) {
27245c734b04SJakub Wojciech Klama 		conf_delete(conf);
27255c734b04SJakub Wojciech Klama 		return (NULL);
27265c734b04SJakub Wojciech Klama 	}
27275c734b04SJakub Wojciech Klama 
27285c734b04SJakub Wojciech Klama 	check_perms(path);
27295c734b04SJakub Wojciech Klama 
27305c734b04SJakub Wojciech Klama 	if (conf->conf_default_ag_defined == false) {
27315c734b04SJakub Wojciech Klama 		log_debugx("auth-group \"default\" not defined; "
27325c734b04SJakub Wojciech Klama 		    "going with defaults");
27335c734b04SJakub Wojciech Klama 		ag = auth_group_find(conf, "default");
27345c734b04SJakub Wojciech Klama 		assert(ag != NULL);
27355c734b04SJakub Wojciech Klama 		ag->ag_type = AG_TYPE_DENY;
27365c734b04SJakub Wojciech Klama 	}
27375c734b04SJakub Wojciech Klama 
27385c734b04SJakub Wojciech Klama 	if (conf->conf_default_pg_defined == false) {
27395c734b04SJakub Wojciech Klama 		log_debugx("portal-group \"default\" not defined; "
27405c734b04SJakub Wojciech Klama 		    "going with defaults");
27415c734b04SJakub Wojciech Klama 		pg = portal_group_find(conf, "default");
27425c734b04SJakub Wojciech Klama 		assert(pg != NULL);
27435c734b04SJakub Wojciech Klama 		portal_group_add_listen(pg, "0.0.0.0:3260", false);
27445c734b04SJakub Wojciech Klama 		portal_group_add_listen(pg, "[::]:3260", false);
27455c734b04SJakub Wojciech Klama 	}
27465c734b04SJakub Wojciech Klama 
27475c734b04SJakub Wojciech Klama 	conf->conf_kernel_port_on = true;
27485c734b04SJakub Wojciech Klama 
27495c734b04SJakub Wojciech Klama 	error = conf_verify(conf);
27505c734b04SJakub Wojciech Klama 	if (error != 0) {
27515c734b04SJakub Wojciech Klama 		conf_delete(conf);
27525c734b04SJakub Wojciech Klama 		return (NULL);
27535c734b04SJakub Wojciech Klama 	}
27545c734b04SJakub Wojciech Klama 
27555c734b04SJakub Wojciech Klama 	return (conf);
27565c734b04SJakub Wojciech Klama }
27575c734b04SJakub Wojciech Klama 
2758009ea47eSEdward Tomasz Napierala int
2759009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
2760009ea47eSEdward Tomasz Napierala {
2761009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
2762829603e2SAlexander Motin 	struct isns *newns;
2763009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
2764009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
2765009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
27667fcbecd0SEdward Tomasz Napierala 	bool test_config = false;
27675c734b04SJakub Wojciech Klama 	bool use_ucl = false;
2768009ea47eSEdward Tomasz Napierala 
27697fcbecd0SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "dtuf:R")) != -1) {
2770009ea47eSEdward Tomasz Napierala 		switch (ch) {
2771009ea47eSEdward Tomasz Napierala 		case 'd':
2772009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
2773009ea47eSEdward Tomasz Napierala 			debug++;
2774009ea47eSEdward Tomasz Napierala 			break;
27757fcbecd0SEdward Tomasz Napierala 		case 't':
27767fcbecd0SEdward Tomasz Napierala 			test_config = true;
27777fcbecd0SEdward Tomasz Napierala 			break;
27785c734b04SJakub Wojciech Klama 		case 'u':
27795c734b04SJakub Wojciech Klama 			use_ucl = true;
27805c734b04SJakub Wojciech Klama 			break;
2781009ea47eSEdward Tomasz Napierala 		case 'f':
2782009ea47eSEdward Tomasz Napierala 			config_path = optarg;
2783009ea47eSEdward Tomasz Napierala 			break;
2784ba3a2d31SEdward Tomasz Napierala 		case 'R':
2785ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
2786ba3a2d31SEdward Tomasz Napierala 			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2787ba3a2d31SEdward Tomasz Napierala 			    "does not support iSER protocol");
2788ba3a2d31SEdward Tomasz Napierala #endif
2789ba3a2d31SEdward Tomasz Napierala 			proxy_mode = true;
2790ba3a2d31SEdward Tomasz Napierala 			break;
2791009ea47eSEdward Tomasz Napierala 		case '?':
2792009ea47eSEdward Tomasz Napierala 		default:
2793009ea47eSEdward Tomasz Napierala 			usage();
2794009ea47eSEdward Tomasz Napierala 		}
2795009ea47eSEdward Tomasz Napierala 	}
2796009ea47eSEdward Tomasz Napierala 	argc -= optind;
2797009ea47eSEdward Tomasz Napierala 	if (argc != 0)
2798009ea47eSEdward Tomasz Napierala 		usage();
2799009ea47eSEdward Tomasz Napierala 
2800009ea47eSEdward Tomasz Napierala 	log_init(debug);
2801009ea47eSEdward Tomasz Napierala 	kernel_init();
2802009ea47eSEdward Tomasz Napierala 
2803009ea47eSEdward Tomasz Napierala 	oldconf = conf_new_from_kernel();
28045c734b04SJakub Wojciech Klama 	newconf = conf_new_from_file(config_path, oldconf, use_ucl);
28055c734b04SJakub Wojciech Klama 
2806009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
2807fe845cdbSEdward Tomasz Napierala 		log_errx(1, "configuration error; exiting");
28087fcbecd0SEdward Tomasz Napierala 
28097fcbecd0SEdward Tomasz Napierala 	if (test_config)
28107fcbecd0SEdward Tomasz Napierala 		return (0);
28117fcbecd0SEdward Tomasz Napierala 
2812009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
2813009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
2814009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
2815009ea47eSEdward Tomasz Napierala 	}
2816009ea47eSEdward Tomasz Napierala 
2817009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
2818009ea47eSEdward Tomasz Napierala 	if (error != 0)
2819fe845cdbSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration; exiting");
2820fe845cdbSEdward Tomasz Napierala 
2821009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
2822009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
2823009ea47eSEdward Tomasz Napierala 
2824009ea47eSEdward Tomasz Napierala 	register_signals();
2825009ea47eSEdward Tomasz Napierala 
28265d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
28275d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
28285d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
28295d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
28305d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
28315d5a95f1SEdward Tomasz Napierala 			exit(1);
28325d5a95f1SEdward Tomasz Napierala 		}
28335d5a95f1SEdward Tomasz Napierala 	}
28345d5a95f1SEdward Tomasz Napierala 
2835829603e2SAlexander Motin 	/* Schedule iSNS update */
2836829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2837829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2838829603e2SAlexander Motin 
2839009ea47eSEdward Tomasz Napierala 	for (;;) {
2840009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
2841009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
2842009ea47eSEdward Tomasz Napierala 			sighup_received = false;
2843009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
28445c734b04SJakub Wojciech Klama 			tmpconf = conf_new_from_file(config_path, newconf,
28455c734b04SJakub Wojciech Klama 			    use_ucl);
28465c734b04SJakub Wojciech Klama 
2847009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
2848009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
2849009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
2850009ea47eSEdward Tomasz Napierala 			} else {
2851009ea47eSEdward Tomasz Napierala 				if (debug > 0)
2852009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
2853009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
2854009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
2855009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
2856009ea47eSEdward Tomasz Napierala 				if (error != 0)
2857009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
2858009ea47eSEdward Tomasz Napierala 					    "configuration");
2859009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
2860009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
2861009ea47eSEdward Tomasz Napierala 			}
2862009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
2863009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
2864009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
2865009ea47eSEdward Tomasz Napierala 
28666b31e130SAlexander Motin 			log_debugx("removing CTL iSCSI ports "
2867009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
2868009ea47eSEdward Tomasz Napierala 
2869009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
2870009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
2871009ea47eSEdward Tomasz Napierala 			if (debug > 0)
2872009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
2873009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
2874009ea47eSEdward Tomasz Napierala 			if (error != 0)
2875009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
2876*2909ddd1SAlan Somers 			conf_delete(newconf);
2877829603e2SAlexander Motin 			conf_delete(oldconf);
2878829603e2SAlexander Motin 			oldconf = NULL;
2879009ea47eSEdward Tomasz Napierala 
2880009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
2881009ea47eSEdward Tomasz Napierala 			exit(0);
2882009ea47eSEdward Tomasz Napierala 		} else {
2883009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
2884009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2885829603e2SAlexander Motin 			if (timed_out()) {
2886829603e2SAlexander Motin 				set_timeout(0, false);
2887829603e2SAlexander Motin 				TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2888829603e2SAlexander Motin 					isns_check(newns);
2889829603e2SAlexander Motin 				/* Schedule iSNS update */
2890829603e2SAlexander Motin 				if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2891829603e2SAlexander Motin 					set_timeout((newconf->conf_isns_period
2892829603e2SAlexander Motin 					    + 2) / 3,
2893829603e2SAlexander Motin 					    false);
2894829603e2SAlexander Motin 				}
2895829603e2SAlexander Motin 			}
2896009ea47eSEdward Tomasz Napierala 		}
2897009ea47eSEdward Tomasz Napierala 	}
2898009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
2899009ea47eSEdward Tomasz Napierala }
2900