xref: /freebsd/usr.sbin/ctld/ctld.c (revision 5f89aea7b74aa4605b25af62e31303097a4a48cc)
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
usage(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 *
conf_new(void)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);
101829603e2SAlexander Motin 	TAILQ_INIT(&conf->conf_isns);
102009ea47eSEdward Tomasz Napierala 
103829603e2SAlexander Motin 	conf->conf_isns_period = 900;
104829603e2SAlexander Motin 	conf->conf_isns_timeout = 5;
105009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
106009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
107009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
108009ea47eSEdward Tomasz Napierala 
109009ea47eSEdward Tomasz Napierala 	return (conf);
110009ea47eSEdward Tomasz Napierala }
111009ea47eSEdward Tomasz Napierala 
112009ea47eSEdward Tomasz Napierala void
conf_delete(struct conf * conf)113009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
114009ea47eSEdward Tomasz Napierala {
115920c6cbaSAlexander Motin 	struct lun *lun, *ltmp;
116009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
117009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
118009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
119829603e2SAlexander Motin 	struct isns *is, *istmp;
120009ea47eSEdward Tomasz Napierala 
121009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
122009ea47eSEdward Tomasz Napierala 
123920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
124920c6cbaSAlexander Motin 		lun_delete(lun);
125009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
126009ea47eSEdward Tomasz Napierala 		target_delete(targ);
127009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
128009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
129009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
130009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
131829603e2SAlexander Motin 	TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
132829603e2SAlexander Motin 		isns_delete(is);
13392847ee1SAlexander Motin 	assert(TAILQ_EMPTY(&conf->conf_ports));
134009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
135009ea47eSEdward Tomasz Napierala 	free(conf);
136009ea47eSEdward Tomasz Napierala }
137009ea47eSEdward Tomasz Napierala 
138009ea47eSEdward Tomasz Napierala static struct auth *
auth_new(struct auth_group * ag)139009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
140009ea47eSEdward Tomasz Napierala {
141009ea47eSEdward Tomasz Napierala 	struct auth *auth;
142009ea47eSEdward Tomasz Napierala 
143009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
144009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
145009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
146009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
147009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
148009ea47eSEdward Tomasz Napierala 	return (auth);
149009ea47eSEdward Tomasz Napierala }
150009ea47eSEdward Tomasz Napierala 
151009ea47eSEdward Tomasz Napierala static void
auth_delete(struct auth * auth)152009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
153009ea47eSEdward Tomasz Napierala {
154009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
155009ea47eSEdward Tomasz Napierala 
156009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
157009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
158009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
159009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
160009ea47eSEdward Tomasz Napierala 	free(auth);
161009ea47eSEdward Tomasz Napierala }
162009ea47eSEdward Tomasz Napierala 
163009ea47eSEdward Tomasz Napierala const struct auth *
auth_find(const struct auth_group * ag,const char * user)164d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user)
165009ea47eSEdward Tomasz Napierala {
166009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
167009ea47eSEdward Tomasz Napierala 
168009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
169009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
170009ea47eSEdward Tomasz Napierala 			return (auth);
171009ea47eSEdward Tomasz Napierala 	}
172009ea47eSEdward Tomasz Napierala 
173009ea47eSEdward Tomasz Napierala 	return (NULL);
174009ea47eSEdward Tomasz Napierala }
175009ea47eSEdward Tomasz Napierala 
1761658628dSEdward Tomasz Napierala static void
auth_check_secret_length(struct auth * auth)1771658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1781658628dSEdward Tomasz Napierala {
1791658628dSEdward Tomasz Napierala 	size_t len;
1801658628dSEdward Tomasz Napierala 
1811658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1821658628dSEdward Tomasz Napierala 	if (len > 16) {
1831658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1841658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1851658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1861658628dSEdward Tomasz Napierala 			    "long", auth->a_user, auth->a_auth_group->ag_name);
1871658628dSEdward Tomasz Napierala 		else
1881658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1891658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1901658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
191f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1921658628dSEdward Tomasz Napierala 	}
1931658628dSEdward Tomasz Napierala 	if (len < 12) {
1941658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1951658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1961658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
1971658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
1981658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
1991658628dSEdward Tomasz Napierala 		else
2001658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
20142f97facSAlexander Motin 			    "is too short; it should be at least 12 characters "
2021658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
203f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
2041658628dSEdward Tomasz Napierala 	}
2051658628dSEdward Tomasz Napierala 
2061658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
2077c924aaeSJosh Paetzel 		len = strlen(auth->a_mutual_secret);
2081658628dSEdward Tomasz Napierala 		if (len > 16) {
2091658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2101658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2111658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
2121658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2131658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2141658628dSEdward Tomasz Napierala 			else
2151658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2161658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
2171658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2181658628dSEdward Tomasz Napierala 				    auth->a_user,
219f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2201658628dSEdward Tomasz Napierala 		}
2211658628dSEdward Tomasz Napierala 		if (len < 12) {
2221658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2231658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2241658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2251658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2261658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2271658628dSEdward Tomasz Napierala 			else
2281658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2291658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2302668ec07SAlexander Motin 				    "at least 12 characters long",
2311658628dSEdward Tomasz Napierala 				    auth->a_user,
232f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2331658628dSEdward Tomasz Napierala 		}
2341658628dSEdward Tomasz Napierala 	}
2351658628dSEdward Tomasz Napierala }
2361658628dSEdward Tomasz Napierala 
2371658628dSEdward Tomasz Napierala const struct auth *
auth_new_chap(struct auth_group * ag,const char * user,const char * secret)2381658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2391658628dSEdward Tomasz Napierala     const char *secret)
2401658628dSEdward Tomasz Napierala {
2411658628dSEdward Tomasz Napierala 	struct auth *auth;
2421658628dSEdward Tomasz Napierala 
2431658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2441658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2451658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2461658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2471658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2481658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2491658628dSEdward Tomasz Napierala 		else
2501658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2511658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
252f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2531658628dSEdward Tomasz Napierala 		return (NULL);
2541658628dSEdward Tomasz Napierala 	}
2551658628dSEdward Tomasz Napierala 
2561658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2571658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2581658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2591658628dSEdward Tomasz Napierala 
2601658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2611658628dSEdward Tomasz Napierala 
2621658628dSEdward Tomasz Napierala 	return (auth);
2631658628dSEdward Tomasz Napierala }
2641658628dSEdward Tomasz Napierala 
2651658628dSEdward Tomasz Napierala const struct auth *
auth_new_chap_mutual(struct auth_group * ag,const char * user,const char * secret,const char * user2,const char * secret2)2661658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2671658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2681658628dSEdward Tomasz Napierala {
2691658628dSEdward Tomasz Napierala 	struct auth *auth;
2701658628dSEdward Tomasz Napierala 
2711658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2721658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2731658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2741658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2751658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2761658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2771658628dSEdward Tomasz Napierala 			    ag->ag_name);
2781658628dSEdward Tomasz Napierala 		else
2791658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2801658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
281f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2821658628dSEdward Tomasz Napierala 		return (NULL);
2831658628dSEdward Tomasz Napierala 	}
2841658628dSEdward Tomasz Napierala 
2851658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2861658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2871658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2881658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2891658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2901658628dSEdward Tomasz Napierala 
2911658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2921658628dSEdward Tomasz Napierala 
2931658628dSEdward Tomasz Napierala 	return (auth);
2941658628dSEdward Tomasz Napierala }
2951658628dSEdward Tomasz Napierala 
2968cb2e958SEdward Tomasz Napierala const struct auth_name *
auth_name_new(struct auth_group * ag,const char * name)2978cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
2988cb2e958SEdward Tomasz Napierala {
2998cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
3008cb2e958SEdward Tomasz Napierala 
3018cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
3028cb2e958SEdward Tomasz Napierala 	if (an == NULL)
3038cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3048cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
305bdbc2a6fSAlexander Motin 	an->an_initiator_name = checked_strdup(name);
3068cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
3078cb2e958SEdward Tomasz Napierala 	return (an);
3088cb2e958SEdward Tomasz Napierala }
3098cb2e958SEdward Tomasz Napierala 
3108cb2e958SEdward Tomasz Napierala static void
auth_name_delete(struct auth_name * an)3118cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
3128cb2e958SEdward Tomasz Napierala {
3138cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
3148cb2e958SEdward Tomasz Napierala 
315bdbc2a6fSAlexander Motin 	free(an->an_initiator_name);
3168cb2e958SEdward Tomasz Napierala 	free(an);
3178cb2e958SEdward Tomasz Napierala }
3188cb2e958SEdward Tomasz Napierala 
3198cb2e958SEdward Tomasz Napierala bool
auth_name_defined(const struct auth_group * ag)3208cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
3218cb2e958SEdward Tomasz Napierala {
3228cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
3238cb2e958SEdward Tomasz Napierala 		return (false);
3248cb2e958SEdward Tomasz Napierala 	return (true);
3258cb2e958SEdward Tomasz Napierala }
3268cb2e958SEdward Tomasz Napierala 
3278cb2e958SEdward Tomasz Napierala const struct auth_name *
auth_name_find(const struct auth_group * ag,const char * name)3288cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3298cb2e958SEdward Tomasz Napierala {
3308cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3318cb2e958SEdward Tomasz Napierala 
3328cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
333bdbc2a6fSAlexander Motin 		if (strcmp(auth_name->an_initiator_name, name) == 0)
3348cb2e958SEdward Tomasz Napierala 			return (auth_name);
3358cb2e958SEdward Tomasz Napierala 	}
3368cb2e958SEdward Tomasz Napierala 
3378cb2e958SEdward Tomasz Napierala 	return (NULL);
3388cb2e958SEdward Tomasz Napierala }
3398cb2e958SEdward Tomasz Napierala 
340e867e162SEdward Tomasz Napierala int
auth_name_check(const struct auth_group * ag,const char * initiator_name)341e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name)
342e867e162SEdward Tomasz Napierala {
343e867e162SEdward Tomasz Napierala 	if (!auth_name_defined(ag))
344e867e162SEdward Tomasz Napierala 		return (0);
345e867e162SEdward Tomasz Napierala 
346e867e162SEdward Tomasz Napierala 	if (auth_name_find(ag, initiator_name) == NULL)
347e867e162SEdward Tomasz Napierala 		return (1);
348e867e162SEdward Tomasz Napierala 
349e867e162SEdward Tomasz Napierala 	return (0);
350e867e162SEdward Tomasz Napierala }
351e867e162SEdward Tomasz Napierala 
3528cb2e958SEdward Tomasz Napierala const struct auth_portal *
auth_portal_new(struct auth_group * ag,const char * portal)3538cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3548cb2e958SEdward Tomasz Napierala {
3558cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
356073edb1cSAlexander Motin 	char *net, *mask, *str, *tmp;
357073edb1cSAlexander Motin 	int len, dm, m;
3588cb2e958SEdward Tomasz Napierala 
3598cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3608cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3618cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3628cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
363a3434cdcSAlexander Motin 	ap->ap_initiator_portal = checked_strdup(portal);
364073edb1cSAlexander Motin 	mask = str = checked_strdup(portal);
365073edb1cSAlexander Motin 	net = strsep(&mask, "/");
366073edb1cSAlexander Motin 	if (net[0] == '[')
367073edb1cSAlexander Motin 		net++;
368073edb1cSAlexander Motin 	len = strlen(net);
369073edb1cSAlexander Motin 	if (len == 0)
370073edb1cSAlexander Motin 		goto error;
371073edb1cSAlexander Motin 	if (net[len - 1] == ']')
372073edb1cSAlexander Motin 		net[len - 1] = 0;
373073edb1cSAlexander Motin 	if (strchr(net, ':') != NULL) {
374073edb1cSAlexander Motin 		struct sockaddr_in6 *sin6 =
375073edb1cSAlexander Motin 		    (struct sockaddr_in6 *)&ap->ap_sa;
376073edb1cSAlexander Motin 
377073edb1cSAlexander Motin 		sin6->sin6_len = sizeof(*sin6);
378073edb1cSAlexander Motin 		sin6->sin6_family = AF_INET6;
379073edb1cSAlexander Motin 		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
380073edb1cSAlexander Motin 			goto error;
381073edb1cSAlexander Motin 		dm = 128;
382073edb1cSAlexander Motin 	} else {
383073edb1cSAlexander Motin 		struct sockaddr_in *sin =
384073edb1cSAlexander Motin 		    (struct sockaddr_in *)&ap->ap_sa;
385073edb1cSAlexander Motin 
386073edb1cSAlexander Motin 		sin->sin_len = sizeof(*sin);
387073edb1cSAlexander Motin 		sin->sin_family = AF_INET;
388073edb1cSAlexander Motin 		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
389073edb1cSAlexander Motin 			goto error;
390073edb1cSAlexander Motin 		dm = 32;
391073edb1cSAlexander Motin 	}
392073edb1cSAlexander Motin 	if (mask != NULL) {
393073edb1cSAlexander Motin 		m = strtol(mask, &tmp, 0);
394073edb1cSAlexander Motin 		if (m < 0 || m > dm || tmp[0] != 0)
395073edb1cSAlexander Motin 			goto error;
396073edb1cSAlexander Motin 	} else
397073edb1cSAlexander Motin 		m = dm;
398073edb1cSAlexander Motin 	ap->ap_mask = m;
399073edb1cSAlexander Motin 	free(str);
4008cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
4018cb2e958SEdward Tomasz Napierala 	return (ap);
402073edb1cSAlexander Motin 
403073edb1cSAlexander Motin error:
4049a913ec2SXin LI 	free(str);
405678b6879SAlexander Motin 	free(ap);
406f9a20518SEdward Tomasz Napierala 	log_warnx("incorrect initiator portal \"%s\"", portal);
407073edb1cSAlexander Motin 	return (NULL);
4088cb2e958SEdward Tomasz Napierala }
4098cb2e958SEdward Tomasz Napierala 
4108cb2e958SEdward Tomasz Napierala static void
auth_portal_delete(struct auth_portal * ap)4118cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
4128cb2e958SEdward Tomasz Napierala {
4138cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
4148cb2e958SEdward Tomasz Napierala 
415a3434cdcSAlexander Motin 	free(ap->ap_initiator_portal);
4168cb2e958SEdward Tomasz Napierala 	free(ap);
4178cb2e958SEdward Tomasz Napierala }
4188cb2e958SEdward Tomasz Napierala 
4198cb2e958SEdward Tomasz Napierala bool
auth_portal_defined(const struct auth_group * ag)4208cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
4218cb2e958SEdward Tomasz Napierala {
4228cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
4238cb2e958SEdward Tomasz Napierala 		return (false);
4248cb2e958SEdward Tomasz Napierala 	return (true);
4258cb2e958SEdward Tomasz Napierala }
4268cb2e958SEdward Tomasz Napierala 
4278cb2e958SEdward Tomasz Napierala const struct auth_portal *
auth_portal_find(const struct auth_group * ag,const struct sockaddr_storage * ss)428073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
4298cb2e958SEdward Tomasz Napierala {
430073edb1cSAlexander Motin 	const struct auth_portal *ap;
4315e46a066SAlexander Motin 	const uint8_t *a, *b;
432073edb1cSAlexander Motin 	int i;
4335e46a066SAlexander Motin 	uint8_t bmask;
4348cb2e958SEdward Tomasz Napierala 
435073edb1cSAlexander Motin 	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
436073edb1cSAlexander Motin 		if (ap->ap_sa.ss_family != ss->ss_family)
437073edb1cSAlexander Motin 			continue;
438073edb1cSAlexander Motin 		if (ss->ss_family == AF_INET) {
4395e46a066SAlexander Motin 			a = (const uint8_t *)
4405e46a066SAlexander Motin 			    &((const struct sockaddr_in *)ss)->sin_addr;
4415e46a066SAlexander Motin 			b = (const uint8_t *)
4425e46a066SAlexander Motin 			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
443073edb1cSAlexander Motin 		} else {
4445e46a066SAlexander Motin 			a = (const uint8_t *)
4455e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
4465e46a066SAlexander Motin 			b = (const uint8_t *)
4475e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
448073edb1cSAlexander Motin 		}
449073edb1cSAlexander Motin 		for (i = 0; i < ap->ap_mask / 8; i++) {
450073edb1cSAlexander Motin 			if (a[i] != b[i])
451073edb1cSAlexander Motin 				goto next;
452073edb1cSAlexander Motin 		}
453073edb1cSAlexander Motin 		if (ap->ap_mask % 8) {
454073edb1cSAlexander Motin 			bmask = 0xff << (8 - (ap->ap_mask % 8));
455073edb1cSAlexander Motin 			if ((a[i] & bmask) != (b[i] & bmask))
456073edb1cSAlexander Motin 				goto next;
457073edb1cSAlexander Motin 		}
458073edb1cSAlexander Motin 		return (ap);
459073edb1cSAlexander Motin next:
460073edb1cSAlexander Motin 		;
4618cb2e958SEdward Tomasz Napierala 	}
4628cb2e958SEdward Tomasz Napierala 
4638cb2e958SEdward Tomasz Napierala 	return (NULL);
4648cb2e958SEdward Tomasz Napierala }
4658cb2e958SEdward Tomasz Napierala 
466e867e162SEdward Tomasz Napierala int
auth_portal_check(const struct auth_group * ag,const struct sockaddr_storage * sa)467e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
468e867e162SEdward Tomasz Napierala {
469e867e162SEdward Tomasz Napierala 
470e867e162SEdward Tomasz Napierala 	if (!auth_portal_defined(ag))
471e867e162SEdward Tomasz Napierala 		return (0);
472e867e162SEdward Tomasz Napierala 
473e867e162SEdward Tomasz Napierala 	if (auth_portal_find(ag, sa) == NULL)
474e867e162SEdward Tomasz Napierala 		return (1);
475e867e162SEdward Tomasz Napierala 
476e867e162SEdward Tomasz Napierala 	return (0);
477e867e162SEdward Tomasz Napierala }
478e867e162SEdward Tomasz Napierala 
479009ea47eSEdward Tomasz Napierala struct auth_group *
auth_group_new(struct conf * conf,const char * name)480009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
481009ea47eSEdward Tomasz Napierala {
482009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
483009ea47eSEdward Tomasz Napierala 
484009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
485009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
486009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
487009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
488009ea47eSEdward Tomasz Napierala 			return (NULL);
489009ea47eSEdward Tomasz Napierala 		}
490009ea47eSEdward Tomasz Napierala 	}
491009ea47eSEdward Tomasz Napierala 
492009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
493009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
494009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
495009ea47eSEdward Tomasz Napierala 	if (name != NULL)
496009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
497009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
4988cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
4998cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
500009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
501009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
502009ea47eSEdward Tomasz Napierala 
503009ea47eSEdward Tomasz Napierala 	return (ag);
504009ea47eSEdward Tomasz Napierala }
505009ea47eSEdward Tomasz Napierala 
506009ea47eSEdward Tomasz Napierala void
auth_group_delete(struct auth_group * ag)507009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
508009ea47eSEdward Tomasz Napierala {
5098cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
5108cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
5118cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
512009ea47eSEdward Tomasz Napierala 
513009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
514009ea47eSEdward Tomasz Napierala 
5158cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
516009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
5178cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
5188cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
5198cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
5208cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
5218cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
522009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
523009ea47eSEdward Tomasz Napierala 	free(ag);
524009ea47eSEdward Tomasz Napierala }
525009ea47eSEdward Tomasz Napierala 
526009ea47eSEdward Tomasz Napierala struct auth_group *
auth_group_find(const struct conf * conf,const char * name)527d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name)
528009ea47eSEdward Tomasz Napierala {
529009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
530009ea47eSEdward Tomasz Napierala 
5312391e536SAlan Somers 	assert(name != NULL);
532009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
533009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
534009ea47eSEdward Tomasz Napierala 			return (ag);
535009ea47eSEdward Tomasz Napierala 	}
536009ea47eSEdward Tomasz Napierala 
537009ea47eSEdward Tomasz Napierala 	return (NULL);
538009ea47eSEdward Tomasz Napierala }
539009ea47eSEdward Tomasz Napierala 
540df9900fbSEdward Tomasz Napierala int
auth_group_set_type(struct auth_group * ag,const char * str)541bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str)
542df9900fbSEdward Tomasz Napierala {
543bd833b11SEdward Tomasz Napierala 	int type;
544df9900fbSEdward Tomasz Napierala 
545df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
546df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
547e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
548e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
549df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
550df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
551df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
552df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
553df9900fbSEdward Tomasz Napierala 	} else {
554df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
555df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
556df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
557df9900fbSEdward Tomasz Napierala 		else
558df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
559df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
560df9900fbSEdward Tomasz Napierala 		return (1);
561df9900fbSEdward Tomasz Napierala 	}
562df9900fbSEdward Tomasz Napierala 
563bd833b11SEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
564bd833b11SEdward Tomasz Napierala 		if (ag->ag_name != NULL) {
565df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
566df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
567df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
568bd833b11SEdward Tomasz Napierala 		} else {
569df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
570df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
571df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
572bd833b11SEdward Tomasz Napierala 		}
573df9900fbSEdward Tomasz Napierala 		return (1);
574df9900fbSEdward Tomasz Napierala 	}
575df9900fbSEdward Tomasz Napierala 
576bd833b11SEdward Tomasz Napierala 	ag->ag_type = type;
577bd833b11SEdward Tomasz Napierala 
578bd833b11SEdward Tomasz Napierala 	return (0);
579df9900fbSEdward Tomasz Napierala }
580df9900fbSEdward Tomasz Napierala 
581009ea47eSEdward Tomasz Napierala static struct portal *
portal_new(struct portal_group * pg)582009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
583009ea47eSEdward Tomasz Napierala {
584009ea47eSEdward Tomasz Napierala 	struct portal *portal;
585009ea47eSEdward Tomasz Napierala 
586009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
587009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
588009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
589009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
590009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
591009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
592009ea47eSEdward Tomasz Napierala 	return (portal);
593009ea47eSEdward Tomasz Napierala }
594009ea47eSEdward Tomasz Napierala 
595009ea47eSEdward Tomasz Napierala static void
portal_delete(struct portal * portal)596009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
597009ea47eSEdward Tomasz Napierala {
59844a5953aSEdward Tomasz Napierala 
599009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
60044a5953aSEdward Tomasz Napierala 	if (portal->p_ai != NULL)
601009ea47eSEdward Tomasz Napierala 		freeaddrinfo(portal->p_ai);
602009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
603009ea47eSEdward Tomasz Napierala 	free(portal);
604009ea47eSEdward Tomasz Napierala }
605009ea47eSEdward Tomasz Napierala 
606009ea47eSEdward Tomasz Napierala struct portal_group *
portal_group_new(struct conf * conf,const char * name)607009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
608009ea47eSEdward Tomasz Napierala {
609009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
610009ea47eSEdward Tomasz Napierala 
611009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
612009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
613009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
614009ea47eSEdward Tomasz Napierala 		return (NULL);
615009ea47eSEdward Tomasz Napierala 	}
616009ea47eSEdward Tomasz Napierala 
617009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
618009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
619009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
620009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
621398290f2SAlexander Motin 	TAILQ_INIT(&pg->pg_options);
622009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
62392847ee1SAlexander Motin 	TAILQ_INIT(&pg->pg_ports);
624009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
6256b31e130SAlexander Motin 	pg->pg_tag = 0;		/* Assigned later in conf_apply(). */
62664ffe6d4SRichard Scheffenegger 	pg->pg_dscp = -1;
627ea8f1280SRichard Scheffenegger 	pg->pg_pcp = -1;
628009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
629009ea47eSEdward Tomasz Napierala 
630009ea47eSEdward Tomasz Napierala 	return (pg);
631009ea47eSEdward Tomasz Napierala }
632009ea47eSEdward Tomasz Napierala 
633009ea47eSEdward Tomasz Napierala void
portal_group_delete(struct portal_group * pg)634009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
635009ea47eSEdward Tomasz Napierala {
636009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
63792847ee1SAlexander Motin 	struct port *port, *tport;
638398290f2SAlexander Motin 	struct option *o, *otmp;
639009ea47eSEdward Tomasz Napierala 
64092847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
64192847ee1SAlexander Motin 		port_delete(port);
642009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
643009ea47eSEdward Tomasz Napierala 
644009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
645009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
646398290f2SAlexander Motin 	TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
647398290f2SAlexander Motin 		option_delete(&pg->pg_options, o);
648009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
649a9d78210SEdward Tomasz Napierala 	free(pg->pg_offload);
6506dbdbf82SEdward Tomasz Napierala 	free(pg->pg_redirection);
651009ea47eSEdward Tomasz Napierala 	free(pg);
652009ea47eSEdward Tomasz Napierala }
653009ea47eSEdward Tomasz Napierala 
654009ea47eSEdward Tomasz Napierala struct portal_group *
portal_group_find(const struct conf * conf,const char * name)655d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name)
656009ea47eSEdward Tomasz Napierala {
657009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
658009ea47eSEdward Tomasz Napierala 
659009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
660009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
661009ea47eSEdward Tomasz Napierala 			return (pg);
662009ea47eSEdward Tomasz Napierala 	}
663009ea47eSEdward Tomasz Napierala 
664009ea47eSEdward Tomasz Napierala 	return (NULL);
665009ea47eSEdward Tomasz Napierala }
666009ea47eSEdward Tomasz Napierala 
667829603e2SAlexander Motin static int
parse_addr_port(char * arg,const char * def_port,struct addrinfo ** ai)668829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
669009ea47eSEdward Tomasz Napierala {
670009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
6711606ad8cSAlexander Motin 	char *str, *addr, *ch;
672009ea47eSEdward Tomasz Napierala 	const char *port;
673009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
674009ea47eSEdward Tomasz Napierala 
6751606ad8cSAlexander Motin 	str = arg = strdup(arg);
676009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
677009ea47eSEdward Tomasz Napierala 		/*
678009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
679009ea47eSEdward Tomasz Napierala 		 */
680009ea47eSEdward Tomasz Napierala 		arg++;
681009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
682203632f2SXin LI 		if (arg == NULL) {
683203632f2SXin LI 			free(str);
684009ea47eSEdward Tomasz Napierala 			return (1);
685203632f2SXin LI 		}
686009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
687829603e2SAlexander Motin 			port = def_port;
688009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
689009ea47eSEdward Tomasz Napierala 			port = arg + 1;
6901606ad8cSAlexander Motin 		} else {
6911606ad8cSAlexander Motin 			free(str);
692009ea47eSEdward Tomasz Napierala 			return (1);
6931606ad8cSAlexander Motin 		}
694009ea47eSEdward Tomasz Napierala 	} else {
695009ea47eSEdward Tomasz Napierala 		/*
696009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
697009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
698009ea47eSEdward Tomasz Napierala 		 */
699009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
700009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
701009ea47eSEdward Tomasz Napierala 				colons++;
702009ea47eSEdward Tomasz Napierala 		}
703009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
704009ea47eSEdward Tomasz Napierala 			addr = arg;
705829603e2SAlexander Motin 			port = def_port;
706009ea47eSEdward Tomasz Napierala 		} else {
707009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
708009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
709829603e2SAlexander Motin 				port = def_port;
710009ea47eSEdward Tomasz Napierala 			else
711009ea47eSEdward Tomasz Napierala 				port = arg;
712009ea47eSEdward Tomasz Napierala 		}
713009ea47eSEdward Tomasz Napierala 	}
714009ea47eSEdward Tomasz Napierala 
715009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
716009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
717009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
718009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
719829603e2SAlexander Motin 	error = getaddrinfo(addr, port, &hints, ai);
7201606ad8cSAlexander Motin 	free(str);
7211606ad8cSAlexander Motin 	return ((error != 0) ? 1 : 0);
722829603e2SAlexander Motin }
723009ea47eSEdward Tomasz Napierala 
724829603e2SAlexander Motin int
portal_group_add_listen(struct portal_group * pg,const char * value,bool iser)725829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
726829603e2SAlexander Motin {
727829603e2SAlexander Motin 	struct portal *portal;
728829603e2SAlexander Motin 
729829603e2SAlexander Motin 	portal = portal_new(pg);
730829603e2SAlexander Motin 	portal->p_listen = checked_strdup(value);
731829603e2SAlexander Motin 	portal->p_iser = iser;
732829603e2SAlexander Motin 
733829603e2SAlexander Motin 	if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
734829603e2SAlexander Motin 		log_warnx("invalid listen address %s", portal->p_listen);
73544a5953aSEdward Tomasz Napierala 		portal_delete(portal);
736009ea47eSEdward Tomasz Napierala 		return (1);
737009ea47eSEdward Tomasz Napierala 	}
738009ea47eSEdward Tomasz Napierala 
739009ea47eSEdward Tomasz Napierala 	/*
740009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
741009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
742009ea47eSEdward Tomasz Napierala 	 */
743009ea47eSEdward Tomasz Napierala 
744009ea47eSEdward Tomasz Napierala 	return (0);
745009ea47eSEdward Tomasz Napierala }
746009ea47eSEdward Tomasz Napierala 
747829603e2SAlexander Motin int
isns_new(struct conf * conf,const char * addr)748829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr)
749829603e2SAlexander Motin {
750829603e2SAlexander Motin 	struct isns *isns;
751829603e2SAlexander Motin 
752829603e2SAlexander Motin 	isns = calloc(1, sizeof(*isns));
753829603e2SAlexander Motin 	if (isns == NULL)
754829603e2SAlexander Motin 		log_err(1, "calloc");
755829603e2SAlexander Motin 	isns->i_conf = conf;
756829603e2SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
757829603e2SAlexander Motin 	isns->i_addr = checked_strdup(addr);
758829603e2SAlexander Motin 
759829603e2SAlexander Motin 	if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
760829603e2SAlexander Motin 		log_warnx("invalid iSNS address %s", isns->i_addr);
761829603e2SAlexander Motin 		isns_delete(isns);
762829603e2SAlexander Motin 		return (1);
763829603e2SAlexander Motin 	}
764829603e2SAlexander Motin 
765829603e2SAlexander Motin 	/*
766829603e2SAlexander Motin 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
767829603e2SAlexander Motin 	 *	those into multiple servers.
768829603e2SAlexander Motin 	 */
769829603e2SAlexander Motin 
770829603e2SAlexander Motin 	return (0);
771829603e2SAlexander Motin }
772829603e2SAlexander Motin 
773829603e2SAlexander Motin void
isns_delete(struct isns * isns)774829603e2SAlexander Motin isns_delete(struct isns *isns)
775829603e2SAlexander Motin {
776829603e2SAlexander Motin 
777829603e2SAlexander Motin 	TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
778829603e2SAlexander Motin 	free(isns->i_addr);
779829603e2SAlexander Motin 	if (isns->i_ai != NULL)
780829603e2SAlexander Motin 		freeaddrinfo(isns->i_ai);
781829603e2SAlexander Motin 	free(isns);
782829603e2SAlexander Motin }
783829603e2SAlexander Motin 
784829603e2SAlexander Motin static int
isns_do_connect(struct isns * isns)785829603e2SAlexander Motin isns_do_connect(struct isns *isns)
786829603e2SAlexander Motin {
787829603e2SAlexander Motin 	int s;
788829603e2SAlexander Motin 
789829603e2SAlexander Motin 	s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
790829603e2SAlexander Motin 	    isns->i_ai->ai_protocol);
791829603e2SAlexander Motin 	if (s < 0) {
792829603e2SAlexander Motin 		log_warn("socket(2) failed for %s", isns->i_addr);
793829603e2SAlexander Motin 		return (-1);
794829603e2SAlexander Motin 	}
795829603e2SAlexander Motin 	if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
796829603e2SAlexander Motin 		log_warn("connect(2) failed for %s", isns->i_addr);
797829603e2SAlexander Motin 		close(s);
798829603e2SAlexander Motin 		return (-1);
799829603e2SAlexander Motin 	}
800829603e2SAlexander Motin 	return(s);
801829603e2SAlexander Motin }
802829603e2SAlexander Motin 
803829603e2SAlexander Motin static int
isns_do_register(struct isns * isns,int s,const char * hostname)804829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname)
805829603e2SAlexander Motin {
806829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
807829603e2SAlexander Motin 	struct target *target;
808829603e2SAlexander Motin 	struct portal *portal;
809829603e2SAlexander Motin 	struct portal_group *pg;
81092847ee1SAlexander Motin 	struct port *port;
811829603e2SAlexander Motin 	struct isns_req *req;
812829603e2SAlexander Motin 	int res = 0;
813829603e2SAlexander Motin 	uint32_t error;
814829603e2SAlexander Motin 
815829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
816829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
817829603e2SAlexander Motin 	isns_req_add_delim(req);
818829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
819829603e2SAlexander Motin 	isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
820829603e2SAlexander Motin 	isns_req_add_32(req, 6, conf->conf_isns_period);
821829603e2SAlexander Motin 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
822829603e2SAlexander Motin 		if (pg->pg_unassigned)
823829603e2SAlexander Motin 			continue;
824829603e2SAlexander Motin 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
825829603e2SAlexander Motin 			isns_req_add_addr(req, 16, portal->p_ai);
826829603e2SAlexander Motin 			isns_req_add_port(req, 17, portal->p_ai);
827829603e2SAlexander Motin 		}
828829603e2SAlexander Motin 	}
829829603e2SAlexander Motin 	TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
830829603e2SAlexander Motin 		isns_req_add_str(req, 32, target->t_name);
831829603e2SAlexander Motin 		isns_req_add_32(req, 33, 1); /* 1 -- Target*/
832829603e2SAlexander Motin 		if (target->t_alias != NULL)
833829603e2SAlexander Motin 			isns_req_add_str(req, 34, target->t_alias);
83492847ee1SAlexander Motin 		TAILQ_FOREACH(port, &target->t_ports, p_ts) {
83592847ee1SAlexander Motin 			if ((pg = port->p_portal_group) == NULL)
83692847ee1SAlexander Motin 				continue;
837829603e2SAlexander Motin 			isns_req_add_32(req, 51, pg->pg_tag);
838829603e2SAlexander Motin 			TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
839829603e2SAlexander Motin 				isns_req_add_addr(req, 49, portal->p_ai);
840829603e2SAlexander Motin 				isns_req_add_port(req, 50, portal->p_ai);
841829603e2SAlexander Motin 			}
842829603e2SAlexander Motin 		}
84392847ee1SAlexander Motin 	}
844829603e2SAlexander Motin 	res = isns_req_send(s, req);
845829603e2SAlexander Motin 	if (res < 0) {
846829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
847829603e2SAlexander Motin 		goto quit;
848829603e2SAlexander Motin 	}
849829603e2SAlexander Motin 	res = isns_req_receive(s, req);
850829603e2SAlexander Motin 	if (res < 0) {
851829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
852829603e2SAlexander Motin 		goto quit;
853829603e2SAlexander Motin 	}
854829603e2SAlexander Motin 	error = isns_req_get_status(req);
855829603e2SAlexander Motin 	if (error != 0) {
856829603e2SAlexander Motin 		log_warnx("iSNS register error %d for %s", error, isns->i_addr);
857829603e2SAlexander Motin 		res = -1;
858829603e2SAlexander Motin 	}
859829603e2SAlexander Motin quit:
860829603e2SAlexander Motin 	isns_req_free(req);
861829603e2SAlexander Motin 	return (res);
862829603e2SAlexander Motin }
863829603e2SAlexander Motin 
864829603e2SAlexander Motin static int
isns_do_check(struct isns * isns,int s,const char * hostname)865829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname)
866829603e2SAlexander Motin {
867829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
868829603e2SAlexander Motin 	struct isns_req *req;
869829603e2SAlexander Motin 	int res = 0;
870829603e2SAlexander Motin 	uint32_t error;
871829603e2SAlexander Motin 
872829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
873829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
874829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
875829603e2SAlexander Motin 	isns_req_add_delim(req);
876829603e2SAlexander Motin 	isns_req_add(req, 2, 0, NULL);
877829603e2SAlexander Motin 	res = isns_req_send(s, req);
878829603e2SAlexander Motin 	if (res < 0) {
879829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
880829603e2SAlexander Motin 		goto quit;
881829603e2SAlexander Motin 	}
882829603e2SAlexander Motin 	res = isns_req_receive(s, req);
883829603e2SAlexander Motin 	if (res < 0) {
884829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
885829603e2SAlexander Motin 		goto quit;
886829603e2SAlexander Motin 	}
887829603e2SAlexander Motin 	error = isns_req_get_status(req);
888829603e2SAlexander Motin 	if (error != 0) {
889829603e2SAlexander Motin 		log_warnx("iSNS check error %d for %s", error, isns->i_addr);
890829603e2SAlexander Motin 		res = -1;
891829603e2SAlexander Motin 	}
892829603e2SAlexander Motin quit:
893829603e2SAlexander Motin 	isns_req_free(req);
894829603e2SAlexander Motin 	return (res);
895829603e2SAlexander Motin }
896829603e2SAlexander Motin 
897829603e2SAlexander Motin static int
isns_do_deregister(struct isns * isns,int s,const char * hostname)898829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname)
899829603e2SAlexander Motin {
900829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
901829603e2SAlexander Motin 	struct isns_req *req;
902829603e2SAlexander Motin 	int res = 0;
903829603e2SAlexander Motin 	uint32_t error;
904829603e2SAlexander Motin 
905829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
906829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
907829603e2SAlexander Motin 	isns_req_add_delim(req);
908829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
909829603e2SAlexander Motin 	res = isns_req_send(s, req);
910829603e2SAlexander Motin 	if (res < 0) {
911829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
912829603e2SAlexander Motin 		goto quit;
913829603e2SAlexander Motin 	}
914829603e2SAlexander Motin 	res = isns_req_receive(s, req);
915829603e2SAlexander Motin 	if (res < 0) {
916829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
917829603e2SAlexander Motin 		goto quit;
918829603e2SAlexander Motin 	}
919829603e2SAlexander Motin 	error = isns_req_get_status(req);
920829603e2SAlexander Motin 	if (error != 0) {
921829603e2SAlexander Motin 		log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
922829603e2SAlexander Motin 		res = -1;
923829603e2SAlexander Motin 	}
924829603e2SAlexander Motin quit:
925829603e2SAlexander Motin 	isns_req_free(req);
926829603e2SAlexander Motin 	return (res);
927829603e2SAlexander Motin }
928829603e2SAlexander Motin 
929829603e2SAlexander Motin void
isns_register(struct isns * isns,struct isns * oldisns)930829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns)
931829603e2SAlexander Motin {
932829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
933ba2548b7SEdward Tomasz Napierala 	int error, s;
934829603e2SAlexander Motin 	char hostname[256];
935829603e2SAlexander Motin 
936829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
937829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
938829603e2SAlexander Motin 		return;
939829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
940829603e2SAlexander Motin 	s = isns_do_connect(isns);
941829603e2SAlexander Motin 	if (s < 0) {
942829603e2SAlexander Motin 		set_timeout(0, false);
943829603e2SAlexander Motin 		return;
944829603e2SAlexander Motin 	}
945ba2548b7SEdward Tomasz Napierala 	error = gethostname(hostname, sizeof(hostname));
946ba2548b7SEdward Tomasz Napierala 	if (error != 0)
947ba2548b7SEdward Tomasz Napierala 		log_err(1, "gethostname");
948829603e2SAlexander Motin 
949829603e2SAlexander Motin 	if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
950829603e2SAlexander Motin 		oldisns = isns;
951d5cc4a4aSAlexander Motin 	isns_do_deregister(oldisns, s, hostname);
952d5cc4a4aSAlexander Motin 	isns_do_register(isns, s, hostname);
953829603e2SAlexander Motin 	close(s);
954829603e2SAlexander Motin 	set_timeout(0, false);
955829603e2SAlexander Motin }
956829603e2SAlexander Motin 
957829603e2SAlexander Motin void
isns_check(struct isns * isns)958829603e2SAlexander Motin isns_check(struct isns *isns)
959829603e2SAlexander Motin {
960829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
961ba2548b7SEdward Tomasz Napierala 	int error, s, res;
962829603e2SAlexander Motin 	char hostname[256];
963829603e2SAlexander Motin 
964829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
965829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
966829603e2SAlexander Motin 		return;
967829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
968829603e2SAlexander Motin 	s = isns_do_connect(isns);
969829603e2SAlexander Motin 	if (s < 0) {
970829603e2SAlexander Motin 		set_timeout(0, false);
971829603e2SAlexander Motin 		return;
972829603e2SAlexander Motin 	}
973ba2548b7SEdward Tomasz Napierala 	error = gethostname(hostname, sizeof(hostname));
974ba2548b7SEdward Tomasz Napierala 	if (error != 0)
975ba2548b7SEdward Tomasz Napierala 		log_err(1, "gethostname");
976829603e2SAlexander Motin 
977829603e2SAlexander Motin 	res = isns_do_check(isns, s, hostname);
978829603e2SAlexander Motin 	if (res < 0) {
979d5cc4a4aSAlexander Motin 		isns_do_deregister(isns, s, hostname);
980d5cc4a4aSAlexander Motin 		isns_do_register(isns, s, hostname);
981829603e2SAlexander Motin 	}
982829603e2SAlexander Motin 	close(s);
983829603e2SAlexander Motin 	set_timeout(0, false);
984829603e2SAlexander Motin }
985829603e2SAlexander Motin 
986829603e2SAlexander Motin void
isns_deregister(struct isns * isns)987829603e2SAlexander Motin isns_deregister(struct isns *isns)
988829603e2SAlexander Motin {
989829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
990ba2548b7SEdward Tomasz Napierala 	int error, s;
991829603e2SAlexander Motin 	char hostname[256];
992829603e2SAlexander Motin 
993829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
994829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
995829603e2SAlexander Motin 		return;
996829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
997829603e2SAlexander Motin 	s = isns_do_connect(isns);
998829603e2SAlexander Motin 	if (s < 0)
999829603e2SAlexander Motin 		return;
1000ba2548b7SEdward Tomasz Napierala 	error = gethostname(hostname, sizeof(hostname));
1001ba2548b7SEdward Tomasz Napierala 	if (error != 0)
1002ba2548b7SEdward Tomasz Napierala 		log_err(1, "gethostname");
1003829603e2SAlexander Motin 
1004d5cc4a4aSAlexander Motin 	isns_do_deregister(isns, s, hostname);
1005829603e2SAlexander Motin 	close(s);
1006829603e2SAlexander Motin 	set_timeout(0, false);
1007829603e2SAlexander Motin }
1008829603e2SAlexander Motin 
100905374883SEdward Tomasz Napierala int
portal_group_set_filter(struct portal_group * pg,const char * str)1010bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str)
101105374883SEdward Tomasz Napierala {
1012bd833b11SEdward Tomasz Napierala 	int filter;
101305374883SEdward Tomasz Napierala 
101405374883SEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
101505374883SEdward Tomasz Napierala 		filter = PG_FILTER_NONE;
101605374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal") == 0) {
101705374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL;
101805374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name") == 0) {
101905374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME;
102005374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name-auth") == 0) {
102105374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME_AUTH;
102205374883SEdward Tomasz Napierala 	} else {
102305374883SEdward Tomasz Napierala 		log_warnx("invalid discovery-filter \"%s\" for portal-group "
102405374883SEdward Tomasz Napierala 		    "\"%s\"; valid values are \"none\", \"portal\", "
102505374883SEdward Tomasz Napierala 		    "\"portal-name\", and \"portal-name-auth\"",
102605374883SEdward Tomasz Napierala 		    str, pg->pg_name);
102705374883SEdward Tomasz Napierala 		return (1);
102805374883SEdward Tomasz Napierala 	}
102905374883SEdward Tomasz Napierala 
1030bd833b11SEdward Tomasz Napierala 	if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1031bd833b11SEdward Tomasz Napierala 	    pg->pg_discovery_filter != filter) {
103205374883SEdward Tomasz Napierala 		log_warnx("cannot set discovery-filter to \"%s\" for "
103305374883SEdward Tomasz Napierala 		    "portal-group \"%s\"; already has a different "
103405374883SEdward Tomasz Napierala 		    "value", str, pg->pg_name);
103505374883SEdward Tomasz Napierala 		return (1);
103605374883SEdward Tomasz Napierala 	}
103705374883SEdward Tomasz Napierala 
1038bd833b11SEdward Tomasz Napierala 	pg->pg_discovery_filter = filter;
1039bd833b11SEdward Tomasz Napierala 
1040bd833b11SEdward Tomasz Napierala 	return (0);
104105374883SEdward Tomasz Napierala }
104205374883SEdward Tomasz Napierala 
10436dbdbf82SEdward Tomasz Napierala int
portal_group_set_offload(struct portal_group * pg,const char * offload)1044a9d78210SEdward Tomasz Napierala portal_group_set_offload(struct portal_group *pg, const char *offload)
1045a9d78210SEdward Tomasz Napierala {
1046a9d78210SEdward Tomasz Napierala 
1047a9d78210SEdward Tomasz Napierala 	if (pg->pg_offload != NULL) {
1048a9d78210SEdward Tomasz Napierala 		log_warnx("cannot set offload to \"%s\" for "
1049a9d78210SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
1050a9d78210SEdward Tomasz Napierala 		    offload, pg->pg_name);
1051a9d78210SEdward Tomasz Napierala 		return (1);
1052a9d78210SEdward Tomasz Napierala 	}
1053a9d78210SEdward Tomasz Napierala 
1054a9d78210SEdward Tomasz Napierala 	pg->pg_offload = checked_strdup(offload);
1055a9d78210SEdward Tomasz Napierala 
1056a9d78210SEdward Tomasz Napierala 	return (0);
1057a9d78210SEdward Tomasz Napierala }
1058a9d78210SEdward Tomasz Napierala 
1059a9d78210SEdward Tomasz Napierala int
portal_group_set_redirection(struct portal_group * pg,const char * addr)10606dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr)
10616dbdbf82SEdward Tomasz Napierala {
10626dbdbf82SEdward Tomasz Napierala 
10636dbdbf82SEdward Tomasz Napierala 	if (pg->pg_redirection != NULL) {
10646dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
10656dbdbf82SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
10666dbdbf82SEdward Tomasz Napierala 		    addr, pg->pg_name);
10676dbdbf82SEdward Tomasz Napierala 		return (1);
10686dbdbf82SEdward Tomasz Napierala 	}
10696dbdbf82SEdward Tomasz Napierala 
10706dbdbf82SEdward Tomasz Napierala 	pg->pg_redirection = checked_strdup(addr);
10716dbdbf82SEdward Tomasz Napierala 
10726dbdbf82SEdward Tomasz Napierala 	return (0);
10736dbdbf82SEdward Tomasz Napierala }
10746dbdbf82SEdward Tomasz Napierala 
1075009ea47eSEdward Tomasz Napierala static bool
valid_hex(const char ch)1076009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
1077009ea47eSEdward Tomasz Napierala {
1078009ea47eSEdward Tomasz Napierala 	switch (ch) {
1079009ea47eSEdward Tomasz Napierala 	case '0':
1080009ea47eSEdward Tomasz Napierala 	case '1':
1081009ea47eSEdward Tomasz Napierala 	case '2':
1082009ea47eSEdward Tomasz Napierala 	case '3':
1083009ea47eSEdward Tomasz Napierala 	case '4':
1084009ea47eSEdward Tomasz Napierala 	case '5':
1085009ea47eSEdward Tomasz Napierala 	case '6':
1086009ea47eSEdward Tomasz Napierala 	case '7':
1087009ea47eSEdward Tomasz Napierala 	case '8':
1088009ea47eSEdward Tomasz Napierala 	case '9':
1089009ea47eSEdward Tomasz Napierala 	case 'a':
1090009ea47eSEdward Tomasz Napierala 	case 'A':
1091009ea47eSEdward Tomasz Napierala 	case 'b':
1092009ea47eSEdward Tomasz Napierala 	case 'B':
1093009ea47eSEdward Tomasz Napierala 	case 'c':
1094009ea47eSEdward Tomasz Napierala 	case 'C':
1095009ea47eSEdward Tomasz Napierala 	case 'd':
1096009ea47eSEdward Tomasz Napierala 	case 'D':
1097009ea47eSEdward Tomasz Napierala 	case 'e':
1098009ea47eSEdward Tomasz Napierala 	case 'E':
1099009ea47eSEdward Tomasz Napierala 	case 'f':
1100009ea47eSEdward Tomasz Napierala 	case 'F':
1101009ea47eSEdward Tomasz Napierala 		return (true);
1102009ea47eSEdward Tomasz Napierala 	default:
1103009ea47eSEdward Tomasz Napierala 		return (false);
1104009ea47eSEdward Tomasz Napierala 	}
1105009ea47eSEdward Tomasz Napierala }
1106009ea47eSEdward Tomasz Napierala 
1107009ea47eSEdward Tomasz Napierala bool
valid_iscsi_name(const char * name)1108009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
1109009ea47eSEdward Tomasz Napierala {
1110009ea47eSEdward Tomasz Napierala 	int i;
1111009ea47eSEdward Tomasz Napierala 
1112009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
1113009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
1114009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
1115009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
1116009ea47eSEdward Tomasz Napierala 		return (false);
1117009ea47eSEdward Tomasz Napierala 	}
1118009ea47eSEdward Tomasz Napierala 
1119009ea47eSEdward Tomasz Napierala 	/*
1120009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
1121009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
1122009ea47eSEdward Tomasz Napierala 	 */
1123009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1124009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
1125009ea47eSEdward Tomasz Napierala 			/*
1126009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
1127009ea47eSEdward Tomasz Napierala 			 *      by 3.2.6.2: iSCSI Name Encoding.
1128009ea47eSEdward Tomasz Napierala 			 */
1129009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
1130009ea47eSEdward Tomasz Napierala 				continue;
1131009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1132009ea47eSEdward Tomasz Napierala 				continue;
1133009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
1134009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
1135009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
1136009ea47eSEdward Tomasz Napierala 			break;
1137009ea47eSEdward Tomasz Napierala 		}
1138009ea47eSEdward Tomasz Napierala 		/*
1139009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
1140009ea47eSEdward Tomasz Napierala 		 */
1141009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1142009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
1143009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
1144009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
1145009ea47eSEdward Tomasz Napierala 			    "digits", name);
1146009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
1147009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1148009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1149009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1150009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1151009ea47eSEdward Tomasz Napierala 				break;
1152009ea47eSEdward Tomasz Napierala 			}
1153009ea47eSEdward Tomasz Napierala 		}
1154009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1155009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
1156009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
1157009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
1158009ea47eSEdward Tomasz Napierala 			    "digits", name);
1159009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
1160009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1161009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1162009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1163009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1164009ea47eSEdward Tomasz Napierala 				break;
1165009ea47eSEdward Tomasz Napierala 			}
1166009ea47eSEdward Tomasz Napierala 		}
1167009ea47eSEdward Tomasz Napierala 	} else {
1168009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
1169d7b87b89SJosh Paetzel 		    "either \"iqn.\", \"eui.\", or \"naa.\"",
1170009ea47eSEdward Tomasz Napierala 		    name);
1171009ea47eSEdward Tomasz Napierala 	}
1172009ea47eSEdward Tomasz Napierala 	return (true);
1173009ea47eSEdward Tomasz Napierala }
1174009ea47eSEdward Tomasz Napierala 
1175057abcb0SAlexander Motin struct pport *
pport_new(struct kports * kports,const char * name,uint32_t ctl_port)1176969876fcSAlan Somers pport_new(struct kports *kports, const char *name, uint32_t ctl_port)
1177057abcb0SAlexander Motin {
1178057abcb0SAlexander Motin 	struct pport *pp;
1179057abcb0SAlexander Motin 
1180057abcb0SAlexander Motin 	pp = calloc(1, sizeof(*pp));
1181057abcb0SAlexander Motin 	if (pp == NULL)
1182057abcb0SAlexander Motin 		log_err(1, "calloc");
1183969876fcSAlan Somers 	pp->pp_kports = kports;
1184057abcb0SAlexander Motin 	pp->pp_name = checked_strdup(name);
1185057abcb0SAlexander Motin 	pp->pp_ctl_port = ctl_port;
1186057abcb0SAlexander Motin 	TAILQ_INIT(&pp->pp_ports);
1187969876fcSAlan Somers 	TAILQ_INSERT_TAIL(&kports->pports, pp, pp_next);
1188057abcb0SAlexander Motin 	return (pp);
1189057abcb0SAlexander Motin }
1190057abcb0SAlexander Motin 
1191057abcb0SAlexander Motin struct pport *
pport_find(const struct kports * kports,const char * name)1192969876fcSAlan Somers pport_find(const struct kports *kports, const char *name)
1193057abcb0SAlexander Motin {
1194057abcb0SAlexander Motin 	struct pport *pp;
1195057abcb0SAlexander Motin 
1196969876fcSAlan Somers 	TAILQ_FOREACH(pp, &kports->pports, pp_next) {
1197057abcb0SAlexander Motin 		if (strcasecmp(pp->pp_name, name) == 0)
1198057abcb0SAlexander Motin 			return (pp);
1199057abcb0SAlexander Motin 	}
1200057abcb0SAlexander Motin 	return (NULL);
1201057abcb0SAlexander Motin }
1202057abcb0SAlexander Motin 
1203057abcb0SAlexander Motin struct pport *
pport_copy(struct pport * pp,struct kports * kports)1204969876fcSAlan Somers pport_copy(struct pport *pp, struct kports *kports)
1205057abcb0SAlexander Motin {
1206057abcb0SAlexander Motin 	struct pport *ppnew;
1207057abcb0SAlexander Motin 
1208969876fcSAlan Somers 	ppnew = pport_new(kports, pp->pp_name, pp->pp_ctl_port);
1209057abcb0SAlexander Motin 	return (ppnew);
1210057abcb0SAlexander Motin }
1211057abcb0SAlexander Motin 
1212057abcb0SAlexander Motin void
pport_delete(struct pport * pp)1213057abcb0SAlexander Motin pport_delete(struct pport *pp)
1214057abcb0SAlexander Motin {
1215057abcb0SAlexander Motin 	struct port *port, *tport;
1216057abcb0SAlexander Motin 
1217057abcb0SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1218057abcb0SAlexander Motin 		port_delete(port);
1219969876fcSAlan Somers 	TAILQ_REMOVE(&pp->pp_kports->pports, pp, pp_next);
1220057abcb0SAlexander Motin 	free(pp->pp_name);
1221057abcb0SAlexander Motin 	free(pp);
1222057abcb0SAlexander Motin }
1223057abcb0SAlexander Motin 
122492847ee1SAlexander Motin struct port *
port_new(struct conf * conf,struct target * target,struct portal_group * pg)122592847ee1SAlexander Motin port_new(struct conf *conf, struct target *target, struct portal_group *pg)
122692847ee1SAlexander Motin {
122792847ee1SAlexander Motin 	struct port *port;
1228057abcb0SAlexander Motin 	char *name;
122955e4579eSAlexander Motin 	int ret;
123092847ee1SAlexander Motin 
123155e4579eSAlexander Motin 	ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
123255e4579eSAlexander Motin 	if (ret <= 0)
123355e4579eSAlexander Motin 		log_err(1, "asprintf");
1234057abcb0SAlexander Motin 	if (port_find(conf, name) != NULL) {
1235057abcb0SAlexander Motin 		log_warnx("duplicate port \"%s\"", name);
1236057abcb0SAlexander Motin 		free(name);
1237057abcb0SAlexander Motin 		return (NULL);
1238057abcb0SAlexander Motin 	}
123992847ee1SAlexander Motin 	port = calloc(1, sizeof(*port));
124092847ee1SAlexander Motin 	if (port == NULL)
124192847ee1SAlexander Motin 		log_err(1, "calloc");
124292847ee1SAlexander Motin 	port->p_conf = conf;
1243057abcb0SAlexander Motin 	port->p_name = name;
12448951f055SMarcelo Araujo 	port->p_ioctl_port = 0;
124592847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
124692847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
124792847ee1SAlexander Motin 	port->p_target = target;
124892847ee1SAlexander Motin 	TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
124992847ee1SAlexander Motin 	port->p_portal_group = pg;
125092847ee1SAlexander Motin 	return (port);
125192847ee1SAlexander Motin }
125292847ee1SAlexander Motin 
125392847ee1SAlexander Motin struct port *
port_new_ioctl(struct conf * conf,struct kports * kports,struct target * target,int pp,int vp)1254969876fcSAlan Somers port_new_ioctl(struct conf *conf, struct kports *kports, struct target *target,
1255969876fcSAlan Somers     int pp, int vp)
12568951f055SMarcelo Araujo {
12578951f055SMarcelo Araujo 	struct pport *pport;
12588951f055SMarcelo Araujo 	struct port *port;
12598951f055SMarcelo Araujo 	char *pname;
12608951f055SMarcelo Araujo 	char *name;
12618951f055SMarcelo Araujo 	int ret;
12628951f055SMarcelo Araujo 
12638951f055SMarcelo Araujo 	ret = asprintf(&pname, "ioctl/%d/%d", pp, vp);
12648951f055SMarcelo Araujo 	if (ret <= 0) {
12658951f055SMarcelo Araujo 		log_err(1, "asprintf");
12668951f055SMarcelo Araujo 		return (NULL);
12678951f055SMarcelo Araujo 	}
12688951f055SMarcelo Araujo 
1269969876fcSAlan Somers 	pport = pport_find(kports, pname);
12708951f055SMarcelo Araujo 	if (pport != NULL) {
12718951f055SMarcelo Araujo 		free(pname);
12728951f055SMarcelo Araujo 		return (port_new_pp(conf, target, pport));
12738951f055SMarcelo Araujo 	}
12748951f055SMarcelo Araujo 
12758951f055SMarcelo Araujo 	ret = asprintf(&name, "%s-%s", pname, target->t_name);
12768951f055SMarcelo Araujo 	free(pname);
12778951f055SMarcelo Araujo 
12788951f055SMarcelo Araujo 	if (ret <= 0)
12798951f055SMarcelo Araujo 		log_err(1, "asprintf");
12808951f055SMarcelo Araujo 	if (port_find(conf, name) != NULL) {
12818951f055SMarcelo Araujo 		log_warnx("duplicate port \"%s\"", name);
12828951f055SMarcelo Araujo 		free(name);
12838951f055SMarcelo Araujo 		return (NULL);
12848951f055SMarcelo Araujo 	}
12858951f055SMarcelo Araujo 	port = calloc(1, sizeof(*port));
12868951f055SMarcelo Araujo 	if (port == NULL)
12878951f055SMarcelo Araujo 		log_err(1, "calloc");
12888951f055SMarcelo Araujo 	port->p_conf = conf;
12898951f055SMarcelo Araujo 	port->p_name = name;
12908951f055SMarcelo Araujo 	port->p_ioctl_port = 1;
12918951f055SMarcelo Araujo 	port->p_ioctl_pp = pp;
12928951f055SMarcelo Araujo 	port->p_ioctl_vp = vp;
12938951f055SMarcelo Araujo 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
12948951f055SMarcelo Araujo 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
12958951f055SMarcelo Araujo 	port->p_target = target;
12968951f055SMarcelo Araujo 	return (port);
12978951f055SMarcelo Araujo }
12988951f055SMarcelo Araujo 
12998951f055SMarcelo Araujo struct port *
port_new_pp(struct conf * conf,struct target * target,struct pport * pp)1300057abcb0SAlexander Motin port_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1301057abcb0SAlexander Motin {
1302057abcb0SAlexander Motin 	struct port *port;
1303057abcb0SAlexander Motin 	char *name;
130455e4579eSAlexander Motin 	int ret;
1305057abcb0SAlexander Motin 
130655e4579eSAlexander Motin 	ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
130755e4579eSAlexander Motin 	if (ret <= 0)
130855e4579eSAlexander Motin 		log_err(1, "asprintf");
1309057abcb0SAlexander Motin 	if (port_find(conf, name) != NULL) {
1310057abcb0SAlexander Motin 		log_warnx("duplicate port \"%s\"", name);
1311057abcb0SAlexander Motin 		free(name);
1312057abcb0SAlexander Motin 		return (NULL);
1313057abcb0SAlexander Motin 	}
1314057abcb0SAlexander Motin 	port = calloc(1, sizeof(*port));
1315057abcb0SAlexander Motin 	if (port == NULL)
1316057abcb0SAlexander Motin 		log_err(1, "calloc");
1317057abcb0SAlexander Motin 	port->p_conf = conf;
1318057abcb0SAlexander Motin 	port->p_name = name;
1319057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1320057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1321057abcb0SAlexander Motin 	port->p_target = target;
1322057abcb0SAlexander Motin 	TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1323057abcb0SAlexander Motin 	port->p_pport = pp;
1324057abcb0SAlexander Motin 	return (port);
1325057abcb0SAlexander Motin }
1326057abcb0SAlexander Motin 
1327057abcb0SAlexander Motin struct port *
port_find(const struct conf * conf,const char * name)132892847ee1SAlexander Motin port_find(const struct conf *conf, const char *name)
132992847ee1SAlexander Motin {
133092847ee1SAlexander Motin 	struct port *port;
133192847ee1SAlexander Motin 
133292847ee1SAlexander Motin 	TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
133392847ee1SAlexander Motin 		if (strcasecmp(port->p_name, name) == 0)
133492847ee1SAlexander Motin 			return (port);
133592847ee1SAlexander Motin 	}
133692847ee1SAlexander Motin 
133792847ee1SAlexander Motin 	return (NULL);
133892847ee1SAlexander Motin }
133992847ee1SAlexander Motin 
134092847ee1SAlexander Motin struct port *
port_find_in_pg(const struct portal_group * pg,const char * target)134192847ee1SAlexander Motin port_find_in_pg(const struct portal_group *pg, const char *target)
134292847ee1SAlexander Motin {
134392847ee1SAlexander Motin 	struct port *port;
134492847ee1SAlexander Motin 
134592847ee1SAlexander Motin 	TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
134692847ee1SAlexander Motin 		if (strcasecmp(port->p_target->t_name, target) == 0)
134792847ee1SAlexander Motin 			return (port);
134892847ee1SAlexander Motin 	}
134992847ee1SAlexander Motin 
135092847ee1SAlexander Motin 	return (NULL);
135192847ee1SAlexander Motin }
135292847ee1SAlexander Motin 
135392847ee1SAlexander Motin void
port_delete(struct port * port)135492847ee1SAlexander Motin port_delete(struct port *port)
135592847ee1SAlexander Motin {
135692847ee1SAlexander Motin 
135792847ee1SAlexander Motin 	if (port->p_portal_group)
135892847ee1SAlexander Motin 		TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1359057abcb0SAlexander Motin 	if (port->p_pport)
1360057abcb0SAlexander Motin 		TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
136192847ee1SAlexander Motin 	if (port->p_target)
136292847ee1SAlexander Motin 		TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
136392847ee1SAlexander Motin 	TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
136492847ee1SAlexander Motin 	free(port->p_name);
136592847ee1SAlexander Motin 	free(port);
136692847ee1SAlexander Motin }
136792847ee1SAlexander Motin 
13689215e501SAlexander Motin int
port_is_dummy(struct port * port)13699215e501SAlexander Motin port_is_dummy(struct port *port)
13709215e501SAlexander Motin {
13719215e501SAlexander Motin 
13729215e501SAlexander Motin 	if (port->p_portal_group) {
13739215e501SAlexander Motin 		if (port->p_portal_group->pg_foreign)
13749215e501SAlexander Motin 			return (1);
13759215e501SAlexander Motin 		if (TAILQ_EMPTY(&port->p_portal_group->pg_portals))
13769215e501SAlexander Motin 			return (1);
13779215e501SAlexander Motin 	}
13789215e501SAlexander Motin 	return (0);
13799215e501SAlexander Motin }
13809215e501SAlexander Motin 
1381009ea47eSEdward Tomasz Napierala struct target *
target_new(struct conf * conf,const char * name)1382f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
1383009ea47eSEdward Tomasz Napierala {
1384009ea47eSEdward Tomasz Napierala 	struct target *targ;
1385009ea47eSEdward Tomasz Napierala 	int i, len;
1386009ea47eSEdward Tomasz Napierala 
1387f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
1388009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
1389f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
1390009ea47eSEdward Tomasz Napierala 		return (NULL);
1391009ea47eSEdward Tomasz Napierala 	}
1392f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
1393f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
1394009ea47eSEdward Tomasz Napierala 		return (NULL);
1395009ea47eSEdward Tomasz Napierala 	}
1396009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
1397009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
1398009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1399f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
1400009ea47eSEdward Tomasz Napierala 
1401009ea47eSEdward Tomasz Napierala 	/*
1402009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
1403009ea47eSEdward Tomasz Napierala 	 */
1404f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
1405009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
1406f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
1407009ea47eSEdward Tomasz Napierala 
1408009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
140992847ee1SAlexander Motin 	TAILQ_INIT(&targ->t_ports);
1410009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1411009ea47eSEdward Tomasz Napierala 
1412009ea47eSEdward Tomasz Napierala 	return (targ);
1413009ea47eSEdward Tomasz Napierala }
1414009ea47eSEdward Tomasz Napierala 
1415009ea47eSEdward Tomasz Napierala void
target_delete(struct target * targ)1416009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
1417009ea47eSEdward Tomasz Napierala {
141892847ee1SAlexander Motin 	struct port *port, *tport;
1419009ea47eSEdward Tomasz Napierala 
142092847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
142192847ee1SAlexander Motin 		port_delete(port);
1422009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1423009ea47eSEdward Tomasz Napierala 
1424969876fcSAlan Somers 	free(targ->t_pport);
1425f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
14266dbdbf82SEdward Tomasz Napierala 	free(targ->t_redirection);
1427009ea47eSEdward Tomasz Napierala 	free(targ);
1428009ea47eSEdward Tomasz Napierala }
1429009ea47eSEdward Tomasz Napierala 
1430009ea47eSEdward Tomasz Napierala struct target *
target_find(struct conf * conf,const char * name)1431f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
1432009ea47eSEdward Tomasz Napierala {
1433009ea47eSEdward Tomasz Napierala 	struct target *targ;
1434009ea47eSEdward Tomasz Napierala 
1435009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1436f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
1437009ea47eSEdward Tomasz Napierala 			return (targ);
1438009ea47eSEdward Tomasz Napierala 	}
1439009ea47eSEdward Tomasz Napierala 
1440009ea47eSEdward Tomasz Napierala 	return (NULL);
1441009ea47eSEdward Tomasz Napierala }
1442009ea47eSEdward Tomasz Napierala 
14436dbdbf82SEdward Tomasz Napierala int
target_set_redirection(struct target * target,const char * addr)14446dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr)
14456dbdbf82SEdward Tomasz Napierala {
14466dbdbf82SEdward Tomasz Napierala 
14476dbdbf82SEdward Tomasz Napierala 	if (target->t_redirection != NULL) {
14486dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
14496dbdbf82SEdward Tomasz Napierala 		    "target \"%s\"; already defined",
14506dbdbf82SEdward Tomasz Napierala 		    addr, target->t_name);
14516dbdbf82SEdward Tomasz Napierala 		return (1);
14526dbdbf82SEdward Tomasz Napierala 	}
14536dbdbf82SEdward Tomasz Napierala 
14546dbdbf82SEdward Tomasz Napierala 	target->t_redirection = checked_strdup(addr);
14556dbdbf82SEdward Tomasz Napierala 
14566dbdbf82SEdward Tomasz Napierala 	return (0);
14576dbdbf82SEdward Tomasz Napierala }
14586dbdbf82SEdward Tomasz Napierala 
1459009ea47eSEdward Tomasz Napierala struct lun *
lun_new(struct conf * conf,const char * name)1460920c6cbaSAlexander Motin lun_new(struct conf *conf, const char *name)
1461009ea47eSEdward Tomasz Napierala {
1462009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1463009ea47eSEdward Tomasz Napierala 
1464920c6cbaSAlexander Motin 	lun = lun_find(conf, name);
1465009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
1466920c6cbaSAlexander Motin 		log_warnx("duplicated lun \"%s\"", name);
1467009ea47eSEdward Tomasz Napierala 		return (NULL);
1468009ea47eSEdward Tomasz Napierala 	}
1469009ea47eSEdward Tomasz Napierala 
1470009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
1471009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
1472009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1473920c6cbaSAlexander Motin 	lun->l_conf = conf;
1474920c6cbaSAlexander Motin 	lun->l_name = checked_strdup(name);
1475009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
1476920c6cbaSAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
14773933f7b1SAlexander Motin 	lun->l_ctl_lun = -1;
1478009ea47eSEdward Tomasz Napierala 
1479009ea47eSEdward Tomasz Napierala 	return (lun);
1480009ea47eSEdward Tomasz Napierala }
1481009ea47eSEdward Tomasz Napierala 
1482009ea47eSEdward Tomasz Napierala void
lun_delete(struct lun * lun)1483009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
1484009ea47eSEdward Tomasz Napierala {
1485920c6cbaSAlexander Motin 	struct target *targ;
1486398290f2SAlexander Motin 	struct option *o, *tmp;
1487920c6cbaSAlexander Motin 	int i;
1488009ea47eSEdward Tomasz Napierala 
1489920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1490920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1491920c6cbaSAlexander Motin 			if (targ->t_luns[i] == lun)
1492920c6cbaSAlexander Motin 				targ->t_luns[i] = NULL;
1493920c6cbaSAlexander Motin 		}
1494920c6cbaSAlexander Motin 	}
1495920c6cbaSAlexander Motin 	TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1496009ea47eSEdward Tomasz Napierala 
1497398290f2SAlexander Motin 	TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1498398290f2SAlexander Motin 		option_delete(&lun->l_options, o);
1499920c6cbaSAlexander Motin 	free(lun->l_name);
1500009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1501009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1502009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1503920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1504009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1505009ea47eSEdward Tomasz Napierala 	free(lun);
1506009ea47eSEdward Tomasz Napierala }
1507009ea47eSEdward Tomasz Napierala 
1508009ea47eSEdward Tomasz Napierala struct lun *
lun_find(const struct conf * conf,const char * name)1509920c6cbaSAlexander Motin lun_find(const struct conf *conf, const char *name)
1510009ea47eSEdward Tomasz Napierala {
1511009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1512009ea47eSEdward Tomasz Napierala 
1513920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1514920c6cbaSAlexander Motin 		if (strcmp(lun->l_name, name) == 0)
1515009ea47eSEdward Tomasz Napierala 			return (lun);
1516009ea47eSEdward Tomasz Napierala 	}
1517009ea47eSEdward Tomasz Napierala 
1518009ea47eSEdward Tomasz Napierala 	return (NULL);
1519009ea47eSEdward Tomasz Napierala }
1520009ea47eSEdward Tomasz Napierala 
1521009ea47eSEdward Tomasz Napierala void
lun_set_backend(struct lun * lun,const char * value)1522009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
1523009ea47eSEdward Tomasz Napierala {
1524009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1525009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
1526009ea47eSEdward Tomasz Napierala }
1527009ea47eSEdward Tomasz Napierala 
1528009ea47eSEdward Tomasz Napierala void
lun_set_blocksize(struct lun * lun,size_t value)1529009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
1530009ea47eSEdward Tomasz Napierala {
1531009ea47eSEdward Tomasz Napierala 
1532009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
1533009ea47eSEdward Tomasz Napierala }
1534009ea47eSEdward Tomasz Napierala 
1535009ea47eSEdward Tomasz Napierala void
lun_set_device_type(struct lun * lun,uint8_t value)153691be33dcSAlexander Motin lun_set_device_type(struct lun *lun, uint8_t value)
153791be33dcSAlexander Motin {
153891be33dcSAlexander Motin 
153991be33dcSAlexander Motin 	lun->l_device_type = value;
154091be33dcSAlexander Motin }
154191be33dcSAlexander Motin 
154291be33dcSAlexander Motin void
lun_set_device_id(struct lun * lun,const char * value)1543009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
1544009ea47eSEdward Tomasz Napierala {
1545009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1546009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
1547009ea47eSEdward Tomasz Napierala }
1548009ea47eSEdward Tomasz Napierala 
1549009ea47eSEdward Tomasz Napierala void
lun_set_path(struct lun * lun,const char * value)1550009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
1551009ea47eSEdward Tomasz Napierala {
1552009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1553009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
1554009ea47eSEdward Tomasz Napierala }
1555009ea47eSEdward Tomasz Napierala 
1556009ea47eSEdward Tomasz Napierala void
lun_set_scsiname(struct lun * lun,const char * value)1557920c6cbaSAlexander Motin lun_set_scsiname(struct lun *lun, const char *value)
1558920c6cbaSAlexander Motin {
1559920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1560920c6cbaSAlexander Motin 	lun->l_scsiname = checked_strdup(value);
1561920c6cbaSAlexander Motin }
1562920c6cbaSAlexander Motin 
1563920c6cbaSAlexander Motin void
lun_set_serial(struct lun * lun,const char * value)1564009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
1565009ea47eSEdward Tomasz Napierala {
1566009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1567009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
1568009ea47eSEdward Tomasz Napierala }
1569009ea47eSEdward Tomasz Napierala 
1570009ea47eSEdward Tomasz Napierala void
lun_set_size(struct lun * lun,size_t value)1571009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
1572009ea47eSEdward Tomasz Napierala {
1573009ea47eSEdward Tomasz Napierala 
1574009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
1575009ea47eSEdward Tomasz Napierala }
1576009ea47eSEdward Tomasz Napierala 
1577009ea47eSEdward Tomasz Napierala void
lun_set_ctl_lun(struct lun * lun,uint32_t value)1578009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
1579009ea47eSEdward Tomasz Napierala {
1580009ea47eSEdward Tomasz Napierala 
1581009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
1582009ea47eSEdward Tomasz Napierala }
1583009ea47eSEdward Tomasz Napierala 
1584398290f2SAlexander Motin struct option *
option_new(struct options * options,const char * name,const char * value)1585398290f2SAlexander Motin option_new(struct options *options, const char *name, const char *value)
1586009ea47eSEdward Tomasz Napierala {
1587398290f2SAlexander Motin 	struct option *o;
1588009ea47eSEdward Tomasz Napierala 
1589398290f2SAlexander Motin 	o = option_find(options, name);
1590398290f2SAlexander Motin 	if (o != NULL) {
1591398290f2SAlexander Motin 		log_warnx("duplicated option \"%s\"", name);
1592009ea47eSEdward Tomasz Napierala 		return (NULL);
1593009ea47eSEdward Tomasz Napierala 	}
1594009ea47eSEdward Tomasz Napierala 
1595398290f2SAlexander Motin 	o = calloc(1, sizeof(*o));
1596398290f2SAlexander Motin 	if (o == NULL)
1597009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1598398290f2SAlexander Motin 	o->o_name = checked_strdup(name);
1599398290f2SAlexander Motin 	o->o_value = checked_strdup(value);
1600398290f2SAlexander Motin 	TAILQ_INSERT_TAIL(options, o, o_next);
1601009ea47eSEdward Tomasz Napierala 
1602398290f2SAlexander Motin 	return (o);
1603009ea47eSEdward Tomasz Napierala }
1604009ea47eSEdward Tomasz Napierala 
1605009ea47eSEdward Tomasz Napierala void
option_delete(struct options * options,struct option * o)1606398290f2SAlexander Motin option_delete(struct options *options, struct option *o)
1607009ea47eSEdward Tomasz Napierala {
1608009ea47eSEdward Tomasz Napierala 
1609398290f2SAlexander Motin 	TAILQ_REMOVE(options, o, o_next);
1610398290f2SAlexander Motin 	free(o->o_name);
1611398290f2SAlexander Motin 	free(o->o_value);
1612398290f2SAlexander Motin 	free(o);
1613009ea47eSEdward Tomasz Napierala }
1614009ea47eSEdward Tomasz Napierala 
1615398290f2SAlexander Motin struct option *
option_find(const struct options * options,const char * name)1616398290f2SAlexander Motin option_find(const struct options *options, const char *name)
1617009ea47eSEdward Tomasz Napierala {
1618398290f2SAlexander Motin 	struct option *o;
1619009ea47eSEdward Tomasz Napierala 
1620398290f2SAlexander Motin 	TAILQ_FOREACH(o, options, o_next) {
1621398290f2SAlexander Motin 		if (strcmp(o->o_name, name) == 0)
1622398290f2SAlexander Motin 			return (o);
1623009ea47eSEdward Tomasz Napierala 	}
1624009ea47eSEdward Tomasz Napierala 
1625009ea47eSEdward Tomasz Napierala 	return (NULL);
1626009ea47eSEdward Tomasz Napierala }
1627009ea47eSEdward Tomasz Napierala 
1628009ea47eSEdward Tomasz Napierala void
option_set(struct option * o,const char * value)1629398290f2SAlexander Motin option_set(struct option *o, const char *value)
1630009ea47eSEdward Tomasz Napierala {
1631009ea47eSEdward Tomasz Napierala 
1632398290f2SAlexander Motin 	free(o->o_value);
1633398290f2SAlexander Motin 	o->o_value = checked_strdup(value);
1634009ea47eSEdward Tomasz Napierala }
1635009ea47eSEdward Tomasz Napierala 
163663783933SJohn Baldwin #ifdef ICL_KERNEL_PROXY
163763783933SJohn Baldwin 
163863783933SJohn Baldwin static void
pdu_receive_proxy(struct pdu * pdu)163963783933SJohn Baldwin pdu_receive_proxy(struct pdu *pdu)
164063783933SJohn Baldwin {
164163783933SJohn Baldwin 	struct connection *conn;
164263783933SJohn Baldwin 	size_t len;
164363783933SJohn Baldwin 
164463783933SJohn Baldwin 	assert(proxy_mode);
164563783933SJohn Baldwin 	conn = pdu->pdu_connection;
164663783933SJohn Baldwin 
164763783933SJohn Baldwin 	kernel_receive(pdu);
164863783933SJohn Baldwin 
164963783933SJohn Baldwin 	len = pdu_ahs_length(pdu);
165063783933SJohn Baldwin 	if (len > 0)
165163783933SJohn Baldwin 		log_errx(1, "protocol error: non-empty AHS");
165263783933SJohn Baldwin 
165363783933SJohn Baldwin 	len = pdu_data_segment_length(pdu);
165463783933SJohn Baldwin 	assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
165563783933SJohn Baldwin 	pdu->pdu_data_len = len;
165663783933SJohn Baldwin }
165763783933SJohn Baldwin 
165863783933SJohn Baldwin static void
pdu_send_proxy(struct pdu * pdu)165963783933SJohn Baldwin pdu_send_proxy(struct pdu *pdu)
166063783933SJohn Baldwin {
166163783933SJohn Baldwin 
166263783933SJohn Baldwin 	assert(proxy_mode);
166363783933SJohn Baldwin 
166463783933SJohn Baldwin 	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
166563783933SJohn Baldwin 	kernel_send(pdu);
166663783933SJohn Baldwin }
166763783933SJohn Baldwin 
166863783933SJohn Baldwin #endif /* ICL_KERNEL_PROXY */
166963783933SJohn Baldwin 
167063783933SJohn Baldwin static void
pdu_fail(const struct connection * conn __unused,const char * reason __unused)167163783933SJohn Baldwin pdu_fail(const struct connection *conn __unused, const char *reason __unused)
167263783933SJohn Baldwin {
167363783933SJohn Baldwin }
167463783933SJohn Baldwin 
167563783933SJohn Baldwin static struct ctld_connection *
connection_new(struct portal * portal,int fd,const char * host,const struct sockaddr * client_sa)1676073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host,
1677073edb1cSAlexander Motin     const struct sockaddr *client_sa)
1678009ea47eSEdward Tomasz Napierala {
167963783933SJohn Baldwin 	struct ctld_connection *conn;
1680009ea47eSEdward Tomasz Napierala 
1681009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
1682009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
1683009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
168463783933SJohn Baldwin 	connection_init(&conn->conn, &conn_ops, proxy_mode);
168563783933SJohn Baldwin 	conn->conn.conn_socket = fd;
1686009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
1687009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
1688073edb1cSAlexander Motin 	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1689009ea47eSEdward Tomasz Napierala 
1690009ea47eSEdward Tomasz Napierala 	return (conn);
1691009ea47eSEdward Tomasz Napierala }
1692009ea47eSEdward Tomasz Napierala 
1693009ea47eSEdward Tomasz Napierala #if 0
1694009ea47eSEdward Tomasz Napierala static void
1695009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
1696009ea47eSEdward Tomasz Napierala {
1697009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1698009ea47eSEdward Tomasz Napierala 	struct auth *auth;
16998cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
17008cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
1701009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1702009ea47eSEdward Tomasz Napierala 	struct portal *portal;
1703009ea47eSEdward Tomasz Napierala 	struct target *targ;
1704009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1705398290f2SAlexander Motin 	struct option *o;
1706009ea47eSEdward Tomasz Napierala 
1707009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1708009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1709009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1710009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1711009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
1712009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
17138cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
17148cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
1715bdbc2a6fSAlexander Motin 			    auth_name->an_initiator_name);
17168951f055SMarcelo Araujo 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next)
17178cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
1718a3434cdcSAlexander Motin 			    auth_portal->ap_initiator_portal);
1719009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1720009ea47eSEdward Tomasz Napierala 	}
1721009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1722009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1723009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1724009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1725009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1726009ea47eSEdward Tomasz Napierala 	}
1727920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1728920c6cbaSAlexander Motin 		fprintf(stderr, "\tlun %s {\n", lun->l_name);
1729009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1730398290f2SAlexander Motin 		TAILQ_FOREACH(o, &lun->l_options, o_next)
1731009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\toption %s %s\n",
17328951f055SMarcelo Araujo 			    o->o_name, o->o_value);
1733009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t}\n");
1734009ea47eSEdward Tomasz Napierala 	}
1735920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1736920c6cbaSAlexander Motin 		fprintf(stderr, "target %s {\n", targ->t_name);
1737920c6cbaSAlexander Motin 		if (targ->t_alias != NULL)
1738920c6cbaSAlexander Motin 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1739009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1740009ea47eSEdward Tomasz Napierala 	}
1741009ea47eSEdward Tomasz Napierala }
1742009ea47eSEdward Tomasz Napierala #endif
1743009ea47eSEdward Tomasz Napierala 
17442fabfaa5SEdward Tomasz Napierala static int
conf_verify_lun(struct lun * lun)17452fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
17462fabfaa5SEdward Tomasz Napierala {
17472fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
17482fabfaa5SEdward Tomasz Napierala 
17492fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
17502fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
17512fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
17522fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
1753920c6cbaSAlexander Motin 			log_warnx("missing path for lun \"%s\"",
1754920c6cbaSAlexander Motin 			    lun->l_name);
17552fabfaa5SEdward Tomasz Napierala 			return (1);
17562fabfaa5SEdward Tomasz Napierala 		}
17572fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
17582fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
1759920c6cbaSAlexander Motin 			log_warnx("missing size for ramdisk-backed lun \"%s\"",
1760920c6cbaSAlexander Motin 			    lun->l_name);
17612fabfaa5SEdward Tomasz Napierala 			return (1);
17622fabfaa5SEdward Tomasz Napierala 		}
17632fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
17642fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
1765920c6cbaSAlexander Motin 			    "for ramdisk-backed lun \"%s\"",
1766920c6cbaSAlexander Motin 			    lun->l_name);
17672fabfaa5SEdward Tomasz Napierala 			return (1);
17682fabfaa5SEdward Tomasz Napierala 		}
17692fabfaa5SEdward Tomasz Napierala 	}
17702fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
17716f2f92a5SAlexander Motin 		if (lun->l_device_type == 5)
17726f2f92a5SAlexander Motin 			lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
17736f2f92a5SAlexander Motin 		else
17742fabfaa5SEdward Tomasz Napierala 			lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
17752fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
1776920c6cbaSAlexander Motin 		log_warnx("invalid blocksize for lun \"%s\"; "
1777920c6cbaSAlexander Motin 		    "must be larger than 0", lun->l_name);
17782fabfaa5SEdward Tomasz Napierala 		return (1);
17792fabfaa5SEdward Tomasz Napierala 	}
17802fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1781920c6cbaSAlexander Motin 		log_warnx("invalid size for lun \"%s\"; "
1782920c6cbaSAlexander Motin 		    "must be multiple of blocksize", lun->l_name);
17832fabfaa5SEdward Tomasz Napierala 		return (1);
17842fabfaa5SEdward Tomasz Napierala 	}
1785920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1786a19eebb1SEdward Tomasz Napierala 		if (lun == lun2)
1787a19eebb1SEdward Tomasz Napierala 			continue;
1788a19eebb1SEdward Tomasz Napierala 		if (lun->l_path != NULL && lun2->l_path != NULL &&
1789a19eebb1SEdward Tomasz Napierala 		    strcmp(lun->l_path, lun2->l_path) == 0) {
1790a19eebb1SEdward Tomasz Napierala 			log_debugx("WARNING: path \"%s\" duplicated "
1791920c6cbaSAlexander Motin 			    "between lun \"%s\", and "
1792920c6cbaSAlexander Motin 			    "lun \"%s\"", lun->l_path,
1793920c6cbaSAlexander Motin 			    lun->l_name, lun2->l_name);
1794a19eebb1SEdward Tomasz Napierala 		}
1795a19eebb1SEdward Tomasz Napierala 	}
17962fabfaa5SEdward Tomasz Napierala 
17972fabfaa5SEdward Tomasz Napierala 	return (0);
17982fabfaa5SEdward Tomasz Napierala }
17992fabfaa5SEdward Tomasz Napierala 
1800009ea47eSEdward Tomasz Napierala int
conf_verify(struct conf * conf)1801009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1802009ea47eSEdward Tomasz Napierala {
1803009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1804009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
180592847ee1SAlexander Motin 	struct port *port;
1806009ea47eSEdward Tomasz Napierala 	struct target *targ;
18072fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
18084e5c38f8SEdward Tomasz Napierala 	bool found;
1809920c6cbaSAlexander Motin 	int error, i;
1810009ea47eSEdward Tomasz Napierala 
1811009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1812009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1813009ea47eSEdward Tomasz Napierala 
1814920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1815920c6cbaSAlexander Motin 		error = conf_verify_lun(lun);
1816920c6cbaSAlexander Motin 		if (error != 0)
1817920c6cbaSAlexander Motin 			return (error);
1818920c6cbaSAlexander Motin 	}
1819009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1820009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1821e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1822e7977549SEdward Tomasz Napierala 			    "default");
1823e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1824009ea47eSEdward Tomasz Napierala 		}
182592847ee1SAlexander Motin 		if (TAILQ_EMPTY(&targ->t_ports)) {
182692847ee1SAlexander Motin 			pg = portal_group_find(conf, "default");
182792847ee1SAlexander Motin 			assert(pg != NULL);
182892847ee1SAlexander Motin 			port_new(conf, targ, pg);
1829009ea47eSEdward Tomasz Napierala 		}
18304e5c38f8SEdward Tomasz Napierala 		found = false;
1831920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1832920c6cbaSAlexander Motin 			if (targ->t_luns[i] != NULL)
18334e5c38f8SEdward Tomasz Napierala 				found = true;
1834009ea47eSEdward Tomasz Napierala 		}
18356dbdbf82SEdward Tomasz Napierala 		if (!found && targ->t_redirection == NULL) {
183619b59f13SEdward Tomasz Napierala 			log_warnx("no LUNs defined for target \"%s\"",
183719b59f13SEdward Tomasz Napierala 			    targ->t_name);
1838009ea47eSEdward Tomasz Napierala 		}
18396dbdbf82SEdward Tomasz Napierala 		if (found && targ->t_redirection != NULL) {
18406dbdbf82SEdward Tomasz Napierala 			log_debugx("target \"%s\" contains luns, "
18416dbdbf82SEdward Tomasz Napierala 			    " but configured for redirection",
18426dbdbf82SEdward Tomasz Napierala 			    targ->t_name);
18436dbdbf82SEdward Tomasz Napierala 		}
1844009ea47eSEdward Tomasz Napierala 	}
1845009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1846009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1847009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1848009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1849ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1850009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1851009ea47eSEdward Tomasz Napierala 		}
1852009ea47eSEdward Tomasz Napierala 
185305374883SEdward Tomasz Napierala 		if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
185405374883SEdward Tomasz Napierala 			pg->pg_discovery_filter = PG_FILTER_NONE;
185505374883SEdward Tomasz Napierala 
18566dbdbf82SEdward Tomasz Napierala 		if (pg->pg_redirection != NULL) {
1857db7bf2baSAlexander Motin 			if (!TAILQ_EMPTY(&pg->pg_ports)) {
18586dbdbf82SEdward Tomasz Napierala 				log_debugx("portal-group \"%s\" assigned "
185992847ee1SAlexander Motin 				    "to target, but configured "
18606dbdbf82SEdward Tomasz Napierala 				    "for redirection",
186192847ee1SAlexander Motin 				    pg->pg_name);
18626dbdbf82SEdward Tomasz Napierala 			}
18636dbdbf82SEdward Tomasz Napierala 			pg->pg_unassigned = false;
1864db7bf2baSAlexander Motin 		} else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1865db7bf2baSAlexander Motin 			pg->pg_unassigned = false;
18666dbdbf82SEdward Tomasz Napierala 		} else {
1867009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1868009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1869009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1870009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
18716dbdbf82SEdward Tomasz Napierala 		}
1872009ea47eSEdward Tomasz Napierala 	}
1873009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1874009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1875009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1876009ea47eSEdward Tomasz Napierala 		else
1877009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1878009ea47eSEdward Tomasz Napierala 
18794e5c38f8SEdward Tomasz Napierala 		found = false;
1880009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
18814e5c38f8SEdward Tomasz Napierala 			if (targ->t_auth_group == ag) {
18824e5c38f8SEdward Tomasz Napierala 				found = true;
1883009ea47eSEdward Tomasz Napierala 				break;
1884009ea47eSEdward Tomasz Napierala 			}
18854e5c38f8SEdward Tomasz Napierala 		}
188692847ee1SAlexander Motin 		TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
188792847ee1SAlexander Motin 			if (port->p_auth_group == ag) {
188892847ee1SAlexander Motin 				found = true;
188992847ee1SAlexander Motin 				break;
189092847ee1SAlexander Motin 			}
189192847ee1SAlexander Motin 		}
18924e5c38f8SEdward Tomasz Napierala 		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
18934e5c38f8SEdward Tomasz Napierala 			if (pg->pg_discovery_auth_group == ag) {
18944e5c38f8SEdward Tomasz Napierala 				found = true;
18954e5c38f8SEdward Tomasz Napierala 				break;
18964e5c38f8SEdward Tomasz Napierala 			}
18974e5c38f8SEdward Tomasz Napierala 		}
18984e5c38f8SEdward Tomasz Napierala 		if (!found && ag->ag_name != NULL &&
1899ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1900009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1901009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1902009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1903009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1904009ea47eSEdward Tomasz Napierala 		}
1905009ea47eSEdward Tomasz Napierala 	}
1906009ea47eSEdward Tomasz Napierala 
1907009ea47eSEdward Tomasz Napierala 	return (0);
1908009ea47eSEdward Tomasz Napierala }
1909009ea47eSEdward Tomasz Napierala 
1910009ea47eSEdward Tomasz Napierala static int
conf_apply(struct conf * oldconf,struct conf * newconf)1911009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1912009ea47eSEdward Tomasz Napierala {
1913009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1914009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1915009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
191692847ee1SAlexander Motin 	struct port *oldport, *newport, *tmpport;
1917829603e2SAlexander Motin 	struct isns *oldns, *newns;
19188b94b583SAlexander Motin 	int changed, cumulated_error = 0, error, sockbuf;
1919009ea47eSEdward Tomasz Napierala 	int one = 1;
1920009ea47eSEdward Tomasz Napierala 
1921009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1922009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1923009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1924009ea47eSEdward Tomasz Napierala 	}
1925009ea47eSEdward Tomasz Napierala 
1926*5f89aea7SAlan Somers 	if (oldconf->conf_pidfile_path != NULL &&
1927*5f89aea7SAlan Somers 	    newconf->conf_pidfile_path != NULL)
1928*5f89aea7SAlan Somers 	{
1929*5f89aea7SAlan Somers 		if (strcmp(oldconf->conf_pidfile_path,
1930*5f89aea7SAlan Somers 		           newconf->conf_pidfile_path) != 0)
1931*5f89aea7SAlan Somers 		{
1932*5f89aea7SAlan Somers 			/* pidfile has changed.  rename it */
1933*5f89aea7SAlan Somers 			log_debugx("moving pidfile to %s",
1934*5f89aea7SAlan Somers 				newconf->conf_pidfile_path);
1935*5f89aea7SAlan Somers 			if (rename(oldconf->conf_pidfile_path,
1936*5f89aea7SAlan Somers 				   newconf->conf_pidfile_path))
1937*5f89aea7SAlan Somers 			{
1938*5f89aea7SAlan Somers 				log_err(1, "renaming pidfile %s -> %s",
1939*5f89aea7SAlan Somers 					oldconf->conf_pidfile_path,
1940009ea47eSEdward Tomasz Napierala 					newconf->conf_pidfile_path);
1941009ea47eSEdward Tomasz Napierala 			}
1942009ea47eSEdward Tomasz Napierala 		}
1943*5f89aea7SAlan Somers 		newconf->conf_pidfh = oldconf->conf_pidfh;
1944*5f89aea7SAlan Somers 		oldconf->conf_pidfh = NULL;
1945*5f89aea7SAlan Somers 	}
1946009ea47eSEdward Tomasz Napierala 
19476b31e130SAlexander Motin 	/*
19486b31e130SAlexander Motin 	 * Go through the new portal groups, assigning tags or preserving old.
19496b31e130SAlexander Motin 	 */
19506b31e130SAlexander Motin 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1951db7bf2baSAlexander Motin 		if (newpg->pg_tag != 0)
1952db7bf2baSAlexander Motin 			continue;
19536b31e130SAlexander Motin 		oldpg = portal_group_find(oldconf, newpg->pg_name);
19546b31e130SAlexander Motin 		if (oldpg != NULL)
19556b31e130SAlexander Motin 			newpg->pg_tag = oldpg->pg_tag;
19566b31e130SAlexander Motin 		else
19576b31e130SAlexander Motin 			newpg->pg_tag = ++last_portal_group_tag;
19586b31e130SAlexander Motin 	}
19596b31e130SAlexander Motin 
1960829603e2SAlexander Motin 	/* Deregister on removed iSNS servers. */
1961829603e2SAlexander Motin 	TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1962829603e2SAlexander Motin 		TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1963829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1964829603e2SAlexander Motin 				break;
1965829603e2SAlexander Motin 		}
1966829603e2SAlexander Motin 		if (newns == NULL)
1967829603e2SAlexander Motin 			isns_deregister(oldns);
1968829603e2SAlexander Motin 	}
1969829603e2SAlexander Motin 
1970d5e316e5SEdward Tomasz Napierala 	/*
1971d5e316e5SEdward Tomasz Napierala 	 * XXX: If target or lun removal fails, we should somehow "move"
1972d5e316e5SEdward Tomasz Napierala 	 *      the old lun or target into newconf, so that subsequent
197361a2a354SEdward Tomasz Napierala 	 *      conf_apply() would try to remove them again.  That would
197461a2a354SEdward Tomasz Napierala 	 *      be somewhat hairy, though, and lun deletion failures don't
197561a2a354SEdward Tomasz Napierala 	 *      really happen, so leave it as it is for now.
1976d5e316e5SEdward Tomasz Napierala 	 */
1977009ea47eSEdward Tomasz Napierala 	/*
197892847ee1SAlexander Motin 	 * First, remove any ports present in the old configuration
1979009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
1980009ea47eSEdward Tomasz Napierala 	 */
198192847ee1SAlexander Motin 	TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
19829215e501SAlexander Motin 		if (port_is_dummy(oldport))
1983db7bf2baSAlexander Motin 			continue;
198492847ee1SAlexander Motin 		newport = port_find(newconf, oldport->p_name);
19859215e501SAlexander Motin 		if (newport != NULL && !port_is_dummy(newport))
1986920c6cbaSAlexander Motin 			continue;
1987057abcb0SAlexander Motin 		log_debugx("removing port \"%s\"", oldport->p_name);
198892847ee1SAlexander Motin 		error = kernel_port_remove(oldport);
19896de78f9fSAlexander Motin 		if (error != 0) {
199092847ee1SAlexander Motin 			log_warnx("failed to remove port %s",
199192847ee1SAlexander Motin 			    oldport->p_name);
19926de78f9fSAlexander Motin 			/*
19936de78f9fSAlexander Motin 			 * XXX: Uncomment after fixing the root cause.
19946de78f9fSAlexander Motin 			 *
19956de78f9fSAlexander Motin 			 * cumulated_error++;
19966de78f9fSAlexander Motin 			 */
19976de78f9fSAlexander Motin 		}
1998009ea47eSEdward Tomasz Napierala 	}
1999009ea47eSEdward Tomasz Napierala 
2000009ea47eSEdward Tomasz Napierala 	/*
2001920c6cbaSAlexander Motin 	 * Second, remove any LUNs present in the old configuration
2002009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
2003009ea47eSEdward Tomasz Napierala 	 */
2004920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
2005920c6cbaSAlexander Motin 		newlun = lun_find(newconf, oldlun->l_name);
2006009ea47eSEdward Tomasz Napierala 		if (newlun == NULL) {
2007920c6cbaSAlexander Motin 			log_debugx("lun \"%s\", CTL lun %d "
2008cadf3831SEdward Tomasz Napierala 			    "not found in new configuration; "
2009920c6cbaSAlexander Motin 			    "removing", oldlun->l_name, oldlun->l_ctl_lun);
2010009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
2011009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2012920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
2013920c6cbaSAlexander Motin 				    "CTL lun %d",
2014920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
2015009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2016009ea47eSEdward Tomasz Napierala 			}
2017009ea47eSEdward Tomasz Napierala 			continue;
2018009ea47eSEdward Tomasz Napierala 		}
2019009ea47eSEdward Tomasz Napierala 
2020009ea47eSEdward Tomasz Napierala 		/*
2021009ea47eSEdward Tomasz Napierala 		 * Also remove the LUNs changed by more than size.
2022009ea47eSEdward Tomasz Napierala 		 */
2023009ea47eSEdward Tomasz Napierala 		changed = 0;
2024009ea47eSEdward Tomasz Napierala 		assert(oldlun->l_backend != NULL);
2025009ea47eSEdward Tomasz Napierala 		assert(newlun->l_backend != NULL);
2026009ea47eSEdward Tomasz Napierala 		if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
2027920c6cbaSAlexander Motin 			log_debugx("backend for lun \"%s\", "
2028009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2029920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2030009ea47eSEdward Tomasz Napierala 			changed = 1;
2031009ea47eSEdward Tomasz Napierala 		}
2032009ea47eSEdward Tomasz Napierala 		if (oldlun->l_blocksize != newlun->l_blocksize) {
2033920c6cbaSAlexander Motin 			log_debugx("blocksize for lun \"%s\", "
2034009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2035920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2036009ea47eSEdward Tomasz Napierala 			changed = 1;
2037009ea47eSEdward Tomasz Napierala 		}
2038009ea47eSEdward Tomasz Napierala 		if (newlun->l_device_id != NULL &&
2039009ea47eSEdward Tomasz Napierala 		    (oldlun->l_device_id == NULL ||
2040009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
2041009ea47eSEdward Tomasz Napierala 		     0)) {
2042920c6cbaSAlexander Motin 			log_debugx("device-id for lun \"%s\", "
2043009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2044920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2045009ea47eSEdward Tomasz Napierala 			changed = 1;
2046009ea47eSEdward Tomasz Napierala 		}
2047009ea47eSEdward Tomasz Napierala 		if (newlun->l_path != NULL &&
2048009ea47eSEdward Tomasz Napierala 		    (oldlun->l_path == NULL ||
2049009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
2050920c6cbaSAlexander Motin 			log_debugx("path for lun \"%s\", "
2051009ea47eSEdward Tomasz Napierala 			    "CTL lun %d, changed; removing",
2052920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2053009ea47eSEdward Tomasz Napierala 			changed = 1;
2054009ea47eSEdward Tomasz Napierala 		}
2055009ea47eSEdward Tomasz Napierala 		if (newlun->l_serial != NULL &&
2056009ea47eSEdward Tomasz Napierala 		    (oldlun->l_serial == NULL ||
2057009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
2058920c6cbaSAlexander Motin 			log_debugx("serial for lun \"%s\", "
2059009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
2060920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
2061009ea47eSEdward Tomasz Napierala 			changed = 1;
2062009ea47eSEdward Tomasz Napierala 		}
2063009ea47eSEdward Tomasz Napierala 		if (changed) {
2064009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
2065009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2066920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
2067920c6cbaSAlexander Motin 				    "CTL lun %d",
2068920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
2069009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2070009ea47eSEdward Tomasz Napierala 			}
2071009ea47eSEdward Tomasz Napierala 			lun_delete(oldlun);
2072009ea47eSEdward Tomasz Napierala 			continue;
2073009ea47eSEdward Tomasz Napierala 		}
2074009ea47eSEdward Tomasz Napierala 
2075009ea47eSEdward Tomasz Napierala 		lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
2076009ea47eSEdward Tomasz Napierala 	}
2077920c6cbaSAlexander Motin 
2078920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
2079920c6cbaSAlexander Motin 		oldlun = lun_find(oldconf, newlun->l_name);
2080920c6cbaSAlexander Motin 		if (oldlun != NULL) {
2081a3977beaSAlexander Motin 			log_debugx("modifying lun \"%s\", CTL lun %d",
2082920c6cbaSAlexander Motin 			    newlun->l_name, newlun->l_ctl_lun);
2083a3977beaSAlexander Motin 			error = kernel_lun_modify(newlun);
2084920c6cbaSAlexander Motin 			if (error != 0) {
2085920c6cbaSAlexander Motin 				log_warnx("failed to "
2086a3977beaSAlexander Motin 				    "modify lun \"%s\", CTL lun %d",
2087a3977beaSAlexander Motin 				    newlun->l_name, newlun->l_ctl_lun);
2088920c6cbaSAlexander Motin 				cumulated_error++;
2089920c6cbaSAlexander Motin 			}
2090920c6cbaSAlexander Motin 			continue;
2091920c6cbaSAlexander Motin 		}
2092920c6cbaSAlexander Motin 		log_debugx("adding lun \"%s\"", newlun->l_name);
2093920c6cbaSAlexander Motin 		error = kernel_lun_add(newlun);
2094920c6cbaSAlexander Motin 		if (error != 0) {
2095920c6cbaSAlexander Motin 			log_warnx("failed to add lun \"%s\"", newlun->l_name);
2096920c6cbaSAlexander Motin 			lun_delete(newlun);
2097920c6cbaSAlexander Motin 			cumulated_error++;
2098920c6cbaSAlexander Motin 		}
2099009ea47eSEdward Tomasz Napierala 	}
2100009ea47eSEdward Tomasz Napierala 
2101009ea47eSEdward Tomasz Napierala 	/*
210292847ee1SAlexander Motin 	 * Now add new ports or modify existing ones.
2103009ea47eSEdward Tomasz Napierala 	 */
210415b3e3bbSAlan Somers 	TAILQ_FOREACH_SAFE(newport, &newconf->conf_ports, p_next, tmpport) {
21059215e501SAlexander Motin 		if (port_is_dummy(newport))
2106db7bf2baSAlexander Motin 			continue;
210792847ee1SAlexander Motin 		oldport = port_find(oldconf, newport->p_name);
2108009ea47eSEdward Tomasz Napierala 
21099215e501SAlexander Motin 		if (oldport == NULL || port_is_dummy(oldport)) {
2110057abcb0SAlexander Motin 			log_debugx("adding port \"%s\"", newport->p_name);
211192847ee1SAlexander Motin 			error = kernel_port_add(newport);
211292847ee1SAlexander Motin 		} else {
2113057abcb0SAlexander Motin 			log_debugx("updating port \"%s\"", newport->p_name);
211492847ee1SAlexander Motin 			newport->p_ctl_port = oldport->p_ctl_port;
2115828524c1SAlexander Motin 			error = kernel_port_update(newport, oldport);
2116920c6cbaSAlexander Motin 		}
2117e48bb73aSEdward Tomasz Napierala 		if (error != 0) {
211892847ee1SAlexander Motin 			log_warnx("failed to %s port %s",
211992847ee1SAlexander Motin 			    (oldport == NULL) ? "add" : "update",
212092847ee1SAlexander Motin 			    newport->p_name);
212115b3e3bbSAlan Somers 			if (oldport == NULL || port_is_dummy(oldport))
212215b3e3bbSAlan Somers 				port_delete(newport);
2123e48bb73aSEdward Tomasz Napierala 			/*
2124e48bb73aSEdward Tomasz Napierala 			 * XXX: Uncomment after fixing the root cause.
2125e48bb73aSEdward Tomasz Napierala 			 *
2126e48bb73aSEdward Tomasz Napierala 			 * cumulated_error++;
2127e48bb73aSEdward Tomasz Napierala 			 */
2128e48bb73aSEdward Tomasz Napierala 		}
2129e48bb73aSEdward Tomasz Napierala 	}
2130009ea47eSEdward Tomasz Napierala 
2131009ea47eSEdward Tomasz Napierala 	/*
2132463a577bSEitan Adler 	 * Go through the new portals, opening the sockets as necessary.
2133009ea47eSEdward Tomasz Napierala 	 */
2134009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2135db7bf2baSAlexander Motin 		if (newpg->pg_foreign)
2136db7bf2baSAlexander Motin 			continue;
2137009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
2138009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
2139009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
2140009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
2141009ea47eSEdward Tomasz Napierala 			continue;
2142009ea47eSEdward Tomasz Napierala 		}
2143009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2144009ea47eSEdward Tomasz Napierala 			/*
2145009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
2146009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
2147009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
2148009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
2149009ea47eSEdward Tomasz Napierala 			 */
2150009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2151009ea47eSEdward Tomasz Napierala 			    pg_next) {
2152009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2153009ea47eSEdward Tomasz Napierala 				    p_next) {
2154009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
2155009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
2156009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
2157009ea47eSEdward Tomasz Napierala 						newp->p_socket =
2158009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
2159009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
2160009ea47eSEdward Tomasz Napierala 						break;
2161009ea47eSEdward Tomasz Napierala 					}
2162009ea47eSEdward Tomasz Napierala 				}
2163009ea47eSEdward Tomasz Napierala 			}
2164009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
2165009ea47eSEdward Tomasz Napierala 				/*
2166009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
2167009ea47eSEdward Tomasz Napierala 				 */
2168009ea47eSEdward Tomasz Napierala 				continue;
2169009ea47eSEdward Tomasz Napierala 			}
2170009ea47eSEdward Tomasz Napierala 
2171009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2172ba3a2d31SEdward Tomasz Napierala 			if (proxy_mode) {
21738cab2ed4SEdward Tomasz Napierala 				newpg->pg_conf->conf_portal_id++;
21748cab2ed4SEdward Tomasz Napierala 				newp->p_id = newpg->pg_conf->conf_portal_id;
21758cab2ed4SEdward Tomasz Napierala 				log_debugx("listening on %s, portal-group "
21768cab2ed4SEdward Tomasz Napierala 				    "\"%s\", portal id %d, using ICL proxy",
21778cab2ed4SEdward Tomasz Napierala 				    newp->p_listen, newpg->pg_name, newp->p_id);
21788cab2ed4SEdward Tomasz Napierala 				kernel_listen(newp->p_ai, newp->p_iser,
21798cab2ed4SEdward Tomasz Napierala 				    newp->p_id);
2180ba3a2d31SEdward Tomasz Napierala 				continue;
2181ba3a2d31SEdward Tomasz Napierala 			}
2182ba3a2d31SEdward Tomasz Napierala #endif
2183ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2184009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
2185009ea47eSEdward Tomasz Napierala 
2186009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
2187009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
2188009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
2189009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
2190009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
2191009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
2192009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
2193009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2194009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2195009ea47eSEdward Tomasz Napierala 				continue;
2196009ea47eSEdward Tomasz Napierala 			}
21978b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
21988b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
21998b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
22008b94b583SAlexander Motin 				log_warn("setsockopt(SO_RCVBUF) failed "
22018b94b583SAlexander Motin 				    "for %s", newp->p_listen);
22028b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
22038b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
22048b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
22058b94b583SAlexander Motin 				log_warn("setsockopt(SO_SNDBUF) failed "
22068b94b583SAlexander Motin 				    "for %s", newp->p_listen);
22073b5f95d7SJohn Baldwin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_NO_DDP,
22083b5f95d7SJohn Baldwin 			    &one, sizeof(one)) == -1)
22093b5f95d7SJohn Baldwin 				log_warn("setsockopt(SO_NO_DDP) failed "
22103b5f95d7SJohn Baldwin 				    "for %s", newp->p_listen);
2211009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
2212009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
2213009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2214009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
2215009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
2216009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2217009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2218009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2219009ea47eSEdward Tomasz Napierala 				continue;
2220009ea47eSEdward Tomasz Napierala 			}
222164ffe6d4SRichard Scheffenegger 			if (newpg->pg_dscp != -1) {
222264ffe6d4SRichard Scheffenegger 				struct sockaddr sa;
222364ffe6d4SRichard Scheffenegger 				int len = sizeof(sa);
222464ffe6d4SRichard Scheffenegger 				getsockname(newp->p_socket, &sa, &len);
222564ffe6d4SRichard Scheffenegger 				/*
222664ffe6d4SRichard Scheffenegger 				 * Only allow the 6-bit DSCP
222764ffe6d4SRichard Scheffenegger 				 * field to be modified
222864ffe6d4SRichard Scheffenegger 				 */
222964ffe6d4SRichard Scheffenegger 				int tos = newpg->pg_dscp << 2;
223064ffe6d4SRichard Scheffenegger 				if (sa.sa_family == AF_INET) {
223164ffe6d4SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
223264ffe6d4SRichard Scheffenegger 					    IPPROTO_IP, IP_TOS,
223364ffe6d4SRichard Scheffenegger 					    &tos, sizeof(tos)) == -1)
223464ffe6d4SRichard Scheffenegger 						log_warn("setsockopt(IP_TOS) "
223564ffe6d4SRichard Scheffenegger 						    "failed for %s",
223664ffe6d4SRichard Scheffenegger 						    newp->p_listen);
223764ffe6d4SRichard Scheffenegger 				} else
223864ffe6d4SRichard Scheffenegger 				if (sa.sa_family == AF_INET6) {
223964ffe6d4SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
224064ffe6d4SRichard Scheffenegger 					    IPPROTO_IPV6, IPV6_TCLASS,
224164ffe6d4SRichard Scheffenegger 					    &tos, sizeof(tos)) == -1)
224264ffe6d4SRichard Scheffenegger 						log_warn("setsockopt(IPV6_TCLASS) "
224364ffe6d4SRichard Scheffenegger 						    "failed for %s",
224464ffe6d4SRichard Scheffenegger 						    newp->p_listen);
224564ffe6d4SRichard Scheffenegger 				}
224664ffe6d4SRichard Scheffenegger 			}
2247ea8f1280SRichard Scheffenegger 			if (newpg->pg_pcp != -1) {
2248ea8f1280SRichard Scheffenegger 				struct sockaddr sa;
2249ea8f1280SRichard Scheffenegger 				int len = sizeof(sa);
2250ea8f1280SRichard Scheffenegger 				getsockname(newp->p_socket, &sa, &len);
2251ea8f1280SRichard Scheffenegger 				/*
2252ea8f1280SRichard Scheffenegger 				 * Only allow the 6-bit DSCP
2253ea8f1280SRichard Scheffenegger 				 * field to be modified
2254ea8f1280SRichard Scheffenegger 				 */
2255ea8f1280SRichard Scheffenegger 				int pcp = newpg->pg_pcp;
2256ea8f1280SRichard Scheffenegger 				if (sa.sa_family == AF_INET) {
2257ea8f1280SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
2258ea8f1280SRichard Scheffenegger 					    IPPROTO_IP, IP_VLAN_PCP,
2259ea8f1280SRichard Scheffenegger 					    &pcp, sizeof(pcp)) == -1)
2260ea8f1280SRichard Scheffenegger 						log_warn("setsockopt(IP_VLAN_PCP) "
2261ea8f1280SRichard Scheffenegger 						    "failed for %s",
2262ea8f1280SRichard Scheffenegger 						    newp->p_listen);
2263ea8f1280SRichard Scheffenegger 				} else
2264ea8f1280SRichard Scheffenegger 				if (sa.sa_family == AF_INET6) {
2265ea8f1280SRichard Scheffenegger 					if (setsockopt(newp->p_socket,
2266ea8f1280SRichard Scheffenegger 					    IPPROTO_IPV6, IPV6_VLAN_PCP,
2267ea8f1280SRichard Scheffenegger 					    &pcp, sizeof(pcp)) == -1)
2268ea8f1280SRichard Scheffenegger 						log_warn("setsockopt(IPV6_VLAN_PCP) "
2269ea8f1280SRichard Scheffenegger 						    "failed for %s",
2270ea8f1280SRichard Scheffenegger 						    newp->p_listen);
2271ea8f1280SRichard Scheffenegger 				}
2272ea8f1280SRichard Scheffenegger 			}
2273009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
2274009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
2275009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2276009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
2277009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2278009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2279009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2280009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2281009ea47eSEdward Tomasz Napierala 				continue;
2282009ea47eSEdward Tomasz Napierala 			}
2283009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
2284009ea47eSEdward Tomasz Napierala 			if (error != 0) {
2285009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
2286009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
2287009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
2288009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
2289009ea47eSEdward Tomasz Napierala 				cumulated_error++;
2290009ea47eSEdward Tomasz Napierala 				continue;
2291009ea47eSEdward Tomasz Napierala 			}
2292009ea47eSEdward Tomasz Napierala 		}
2293009ea47eSEdward Tomasz Napierala 	}
2294009ea47eSEdward Tomasz Napierala 
2295009ea47eSEdward Tomasz Napierala 	/*
2296009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
2297009ea47eSEdward Tomasz Napierala 	 */
2298009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2299009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2300009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
2301009ea47eSEdward Tomasz Napierala 				continue;
2302009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
2303009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
2304009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
2305009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
2306009ea47eSEdward Tomasz Napierala 		}
2307009ea47eSEdward Tomasz Napierala 	}
2308009ea47eSEdward Tomasz Napierala 
2309829603e2SAlexander Motin 	/* (Re-)Register on remaining/new iSNS servers. */
2310829603e2SAlexander Motin 	TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2311829603e2SAlexander Motin 		TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2312829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2313829603e2SAlexander Motin 				break;
2314829603e2SAlexander Motin 		}
2315829603e2SAlexander Motin 		isns_register(newns, oldns);
2316829603e2SAlexander Motin 	}
2317829603e2SAlexander Motin 
2318829603e2SAlexander Motin 	/* Schedule iSNS update */
2319829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2320829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2321829603e2SAlexander Motin 
2322009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
2323009ea47eSEdward Tomasz Napierala }
2324009ea47eSEdward Tomasz Napierala 
232563783933SJohn Baldwin static bool
timed_out(void)2326009ea47eSEdward Tomasz Napierala timed_out(void)
2327009ea47eSEdward Tomasz Napierala {
2328009ea47eSEdward Tomasz Napierala 
2329009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
2330009ea47eSEdward Tomasz Napierala }
2331009ea47eSEdward Tomasz Napierala 
2332009ea47eSEdward Tomasz Napierala static void
sigalrm_handler_fatal(int dummy __unused)2333829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused)
2334009ea47eSEdward Tomasz Napierala {
2335009ea47eSEdward Tomasz Napierala 	/*
2336009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
2337009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
2338009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
2339009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
2340009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
2341009ea47eSEdward Tomasz Napierala 	 */
2342009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
2343009ea47eSEdward Tomasz Napierala 		/*
2344009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
2345009ea47eSEdward Tomasz Napierala 		 */
2346009ea47eSEdward Tomasz Napierala 		_exit(2);
2347009ea47eSEdward Tomasz Napierala 	}
2348009ea47eSEdward Tomasz Napierala 
2349009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
2350009ea47eSEdward Tomasz Napierala }
2351009ea47eSEdward Tomasz Napierala 
2352009ea47eSEdward Tomasz Napierala static void
sigalrm_handler(int dummy __unused)2353829603e2SAlexander Motin sigalrm_handler(int dummy __unused)
2354829603e2SAlexander Motin {
2355829603e2SAlexander Motin 
2356829603e2SAlexander Motin 	sigalrm_received = true;
2357829603e2SAlexander Motin }
2358829603e2SAlexander Motin 
2359829603e2SAlexander Motin void
set_timeout(int timeout,int fatal)2360829603e2SAlexander Motin set_timeout(int timeout, int fatal)
2361009ea47eSEdward Tomasz Napierala {
2362009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2363009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
2364009ea47eSEdward Tomasz Napierala 	int error;
2365009ea47eSEdward Tomasz Napierala 
2366829603e2SAlexander Motin 	if (timeout <= 0) {
2367009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
2368829603e2SAlexander Motin 		bzero(&itv, sizeof(itv));
2369829603e2SAlexander Motin 		error = setitimer(ITIMER_REAL, &itv, NULL);
2370829603e2SAlexander Motin 		if (error != 0)
2371829603e2SAlexander Motin 			log_err(1, "setitimer");
2372829603e2SAlexander Motin 		sigalrm_received = false;
2373009ea47eSEdward Tomasz Napierala 		return;
2374009ea47eSEdward Tomasz Napierala 	}
2375009ea47eSEdward Tomasz Napierala 
2376829603e2SAlexander Motin 	sigalrm_received = false;
2377009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2378829603e2SAlexander Motin 	if (fatal)
2379829603e2SAlexander Motin 		sa.sa_handler = sigalrm_handler_fatal;
2380829603e2SAlexander Motin 	else
2381009ea47eSEdward Tomasz Napierala 		sa.sa_handler = sigalrm_handler;
2382009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2383009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
2384009ea47eSEdward Tomasz Napierala 	if (error != 0)
2385009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2386009ea47eSEdward Tomasz Napierala 
2387009ea47eSEdward Tomasz Napierala 	/*
2388009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
2389009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
2390009ea47eSEdward Tomasz Napierala 	 */
2391829603e2SAlexander Motin 	log_debugx("setting session timeout to %d seconds", timeout);
2392009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
2393009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
2394829603e2SAlexander Motin 	itv.it_value.tv_sec = timeout;
2395009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
2396009ea47eSEdward Tomasz Napierala 	if (error != 0)
2397009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
2398009ea47eSEdward Tomasz Napierala }
2399009ea47eSEdward Tomasz Napierala 
2400009ea47eSEdward Tomasz Napierala static int
wait_for_children(bool block)2401009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
2402009ea47eSEdward Tomasz Napierala {
2403009ea47eSEdward Tomasz Napierala 	pid_t pid;
2404009ea47eSEdward Tomasz Napierala 	int status;
2405009ea47eSEdward Tomasz Napierala 	int num = 0;
2406009ea47eSEdward Tomasz Napierala 
2407009ea47eSEdward Tomasz Napierala 	for (;;) {
2408009ea47eSEdward Tomasz Napierala 		/*
2409009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
2410009ea47eSEdward Tomasz Napierala 		 */
2411009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
2412009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
2413009ea47eSEdward Tomasz Napierala 		else
2414009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
2415009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
2416009ea47eSEdward Tomasz Napierala 			break;
2417009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
2418009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
2419009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
2420009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
2421009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
2422009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
2423009ea47eSEdward Tomasz Napierala 		} else {
2424009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
2425009ea47eSEdward Tomasz Napierala 		}
2426009ea47eSEdward Tomasz Napierala 		num++;
2427009ea47eSEdward Tomasz Napierala 	}
2428009ea47eSEdward Tomasz Napierala 
2429009ea47eSEdward Tomasz Napierala 	return (num);
2430009ea47eSEdward Tomasz Napierala }
2431009ea47eSEdward Tomasz Napierala 
2432009ea47eSEdward Tomasz Napierala static void
handle_connection(struct portal * portal,int fd,const struct sockaddr * client_sa,bool dont_fork)24338eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd,
2434073edb1cSAlexander Motin     const struct sockaddr *client_sa, bool dont_fork)
2435009ea47eSEdward Tomasz Napierala {
243663783933SJohn Baldwin 	struct ctld_connection *conn;
2437009ea47eSEdward Tomasz Napierala 	int error;
2438009ea47eSEdward Tomasz Napierala 	pid_t pid;
2439009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
2440009ea47eSEdward Tomasz Napierala 	struct conf *conf;
2441009ea47eSEdward Tomasz Napierala 
2442009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
2443009ea47eSEdward Tomasz Napierala 
2444009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
2445009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
2446009ea47eSEdward Tomasz Napierala 	} else {
2447009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
2448009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
2449009ea47eSEdward Tomasz Napierala 
2450009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2451009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
2452009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
2453009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
2454009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2455009ea47eSEdward Tomasz Napierala 		}
2456009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
2457009ea47eSEdward Tomasz Napierala 		    nchildren);
2458009ea47eSEdward Tomasz Napierala 		nchildren++;
2459009ea47eSEdward Tomasz Napierala 		pid = fork();
2460009ea47eSEdward Tomasz Napierala 		if (pid < 0)
2461009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
2462009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
2463009ea47eSEdward Tomasz Napierala 			close(fd);
2464009ea47eSEdward Tomasz Napierala 			return;
2465009ea47eSEdward Tomasz Napierala 		}
2466009ea47eSEdward Tomasz Napierala 		pidfile_close(conf->conf_pidfh);
2467*5f89aea7SAlan Somers 	}
2468009ea47eSEdward Tomasz Napierala 
2469073edb1cSAlexander Motin 	error = getnameinfo(client_sa, client_sa->sa_len,
2470009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2471009ea47eSEdward Tomasz Napierala 	if (error != 0)
24729a95cfffSEdward Tomasz Napierala 		log_errx(1, "getnameinfo: %s", gai_strerror(error));
2473009ea47eSEdward Tomasz Napierala 
2474009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
2475009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
2476009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
2477009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
2478009ea47eSEdward Tomasz Napierala 
2479073edb1cSAlexander Motin 	conn = connection_new(portal, fd, host, client_sa);
2480829603e2SAlexander Motin 	set_timeout(conf->conf_timeout, true);
2481009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
2482009ea47eSEdward Tomasz Napierala 	login(conn);
2483009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2484009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
2485009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
2486009ea47eSEdward Tomasz Napierala 	} else {
2487009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2488009ea47eSEdward Tomasz Napierala 		discovery(conn);
2489009ea47eSEdward Tomasz Napierala 	}
2490009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
2491009ea47eSEdward Tomasz Napierala 	exit(0);
2492009ea47eSEdward Tomasz Napierala }
2493009ea47eSEdward Tomasz Napierala 
2494009ea47eSEdward Tomasz Napierala static int
fd_add(int fd,fd_set * fdset,int nfds)2495009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
2496009ea47eSEdward Tomasz Napierala {
2497009ea47eSEdward Tomasz Napierala 
2498009ea47eSEdward Tomasz Napierala 	/*
2499009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
2500009ea47eSEdward Tomasz Napierala 	 */
2501009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
2502009ea47eSEdward Tomasz Napierala 		return (nfds);
2503009ea47eSEdward Tomasz Napierala 
2504009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
2505009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
2506009ea47eSEdward Tomasz Napierala 		nfds = fd;
2507009ea47eSEdward Tomasz Napierala 	return (nfds);
2508009ea47eSEdward Tomasz Napierala }
2509009ea47eSEdward Tomasz Napierala 
2510009ea47eSEdward Tomasz Napierala static void
main_loop(struct conf * conf,bool dont_fork)2511009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
2512009ea47eSEdward Tomasz Napierala {
2513009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
2514009ea47eSEdward Tomasz Napierala 	struct portal *portal;
25159a95cfffSEdward Tomasz Napierala 	struct sockaddr_storage client_sa;
25169a95cfffSEdward Tomasz Napierala 	socklen_t client_salen;
2517009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2518009ea47eSEdward Tomasz Napierala 	int connection_id;
25198cab2ed4SEdward Tomasz Napierala 	int portal_id;
2520ba3a2d31SEdward Tomasz Napierala #endif
2521009ea47eSEdward Tomasz Napierala 	fd_set fdset;
2522009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
2523009ea47eSEdward Tomasz Napierala 
2524009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
2525009ea47eSEdward Tomasz Napierala 
2526009ea47eSEdward Tomasz Napierala 	for (;;) {
2527829603e2SAlexander Motin 		if (sighup_received || sigterm_received || timed_out())
2528009ea47eSEdward Tomasz Napierala 			return;
2529009ea47eSEdward Tomasz Napierala 
2530009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2531ba3a2d31SEdward Tomasz Napierala 		if (proxy_mode) {
25328eab95d6SEdward Tomasz Napierala 			client_salen = sizeof(client_sa);
25338eab95d6SEdward Tomasz Napierala 			kernel_accept(&connection_id, &portal_id,
25348eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, &client_salen);
25350488e848SEdward Tomasz Napierala 			assert(client_salen >= client_sa.ss_len);
2536009ea47eSEdward Tomasz Napierala 
25378cab2ed4SEdward Tomasz Napierala 			log_debugx("incoming connection, id %d, portal id %d",
25388cab2ed4SEdward Tomasz Napierala 			    connection_id, portal_id);
25398cab2ed4SEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
25408cab2ed4SEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
25418cab2ed4SEdward Tomasz Napierala 					if (portal->p_id == portal_id) {
25428cab2ed4SEdward Tomasz Napierala 						goto found;
25438cab2ed4SEdward Tomasz Napierala 					}
25448cab2ed4SEdward Tomasz Napierala 				}
25458cab2ed4SEdward Tomasz Napierala 			}
2546009ea47eSEdward Tomasz Napierala 
25478cab2ed4SEdward Tomasz Napierala 			log_errx(1, "kernel returned invalid portal_id %d",
25488cab2ed4SEdward Tomasz Napierala 			    portal_id);
25498cab2ed4SEdward Tomasz Napierala 
25508cab2ed4SEdward Tomasz Napierala found:
25518eab95d6SEdward Tomasz Napierala 			handle_connection(portal, connection_id,
2552073edb1cSAlexander Motin 			    (struct sockaddr *)&client_sa, dont_fork);
2553ba3a2d31SEdward Tomasz Napierala 		} else {
2554ba3a2d31SEdward Tomasz Napierala #endif
2555ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2556ba3a2d31SEdward Tomasz Napierala 
2557009ea47eSEdward Tomasz Napierala 			FD_ZERO(&fdset);
2558009ea47eSEdward Tomasz Napierala 			nfds = 0;
2559009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2560009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2561009ea47eSEdward Tomasz Napierala 					nfds = fd_add(portal->p_socket, &fdset, nfds);
2562009ea47eSEdward Tomasz Napierala 			}
2563009ea47eSEdward Tomasz Napierala 			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2564009ea47eSEdward Tomasz Napierala 			if (error <= 0) {
2565009ea47eSEdward Tomasz Napierala 				if (errno == EINTR)
2566009ea47eSEdward Tomasz Napierala 					return;
2567009ea47eSEdward Tomasz Napierala 				log_err(1, "select");
2568009ea47eSEdward Tomasz Napierala 			}
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 					if (!FD_ISSET(portal->p_socket, &fdset))
2572009ea47eSEdward Tomasz Napierala 						continue;
25739a95cfffSEdward Tomasz Napierala 					client_salen = sizeof(client_sa);
25749a95cfffSEdward Tomasz Napierala 					client_fd = accept(portal->p_socket,
25759a95cfffSEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
25769a95cfffSEdward Tomasz Napierala 					    &client_salen);
257783dd77c3SAlexander Motin 					if (client_fd < 0) {
257883dd77c3SAlexander Motin 						if (errno == ECONNABORTED)
257983dd77c3SAlexander Motin 							continue;
2580009ea47eSEdward Tomasz Napierala 						log_err(1, "accept");
258183dd77c3SAlexander Motin 					}
25820488e848SEdward Tomasz Napierala 					assert(client_salen >= client_sa.ss_len);
25830488e848SEdward Tomasz Napierala 
25849a95cfffSEdward Tomasz Napierala 					handle_connection(portal, client_fd,
25858eab95d6SEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
2586073edb1cSAlexander Motin 					    dont_fork);
2587009ea47eSEdward Tomasz Napierala 					break;
2588009ea47eSEdward Tomasz Napierala 				}
2589009ea47eSEdward Tomasz Napierala 			}
2590ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2591ba3a2d31SEdward Tomasz Napierala 		}
2592ba3a2d31SEdward Tomasz Napierala #endif
2593009ea47eSEdward Tomasz Napierala 	}
2594009ea47eSEdward Tomasz Napierala }
2595009ea47eSEdward Tomasz Napierala 
2596009ea47eSEdward Tomasz Napierala static void
sighup_handler(int dummy __unused)2597009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
2598009ea47eSEdward Tomasz Napierala {
2599009ea47eSEdward Tomasz Napierala 
2600009ea47eSEdward Tomasz Napierala 	sighup_received = true;
2601009ea47eSEdward Tomasz Napierala }
2602009ea47eSEdward Tomasz Napierala 
2603009ea47eSEdward Tomasz Napierala static void
sigterm_handler(int dummy __unused)2604009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
2605009ea47eSEdward Tomasz Napierala {
2606009ea47eSEdward Tomasz Napierala 
2607009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
2608009ea47eSEdward Tomasz Napierala }
2609009ea47eSEdward Tomasz Napierala 
2610009ea47eSEdward Tomasz Napierala static void
sigchld_handler(int dummy __unused)261170b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused)
261270b939ecSEdward Tomasz Napierala {
261370b939ecSEdward Tomasz Napierala 
261470b939ecSEdward Tomasz Napierala 	/*
261570b939ecSEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
261670b939ecSEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
261770b939ecSEdward Tomasz Napierala 	 * wait_for_children().
261870b939ecSEdward Tomasz Napierala 	 */
261970b939ecSEdward Tomasz Napierala }
262070b939ecSEdward Tomasz Napierala 
262170b939ecSEdward Tomasz Napierala static void
register_signals(void)2622009ea47eSEdward Tomasz Napierala register_signals(void)
2623009ea47eSEdward Tomasz Napierala {
2624009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2625009ea47eSEdward Tomasz Napierala 	int error;
2626009ea47eSEdward Tomasz Napierala 
2627009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2628009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
2629009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2630009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
2631009ea47eSEdward Tomasz Napierala 	if (error != 0)
2632009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2633009ea47eSEdward Tomasz Napierala 
2634009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2635009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
2636009ea47eSEdward Tomasz Napierala 	if (error != 0)
2637009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2638009ea47eSEdward Tomasz Napierala 
2639009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2640009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
2641009ea47eSEdward Tomasz Napierala 	if (error != 0)
2642009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
264370b939ecSEdward Tomasz Napierala 
264470b939ecSEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
264570b939ecSEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
264670b939ecSEdward Tomasz Napierala 	if (error != 0)
264770b939ecSEdward Tomasz Napierala 		log_err(1, "sigaction");
2648009ea47eSEdward Tomasz Napierala }
2649009ea47eSEdward Tomasz Napierala 
26505c734b04SJakub Wojciech Klama static void
check_perms(const char * path)26515c734b04SJakub Wojciech Klama check_perms(const char *path)
26525c734b04SJakub Wojciech Klama {
26535c734b04SJakub Wojciech Klama 	struct stat sb;
26545c734b04SJakub Wojciech Klama 	int error;
26555c734b04SJakub Wojciech Klama 
26565c734b04SJakub Wojciech Klama 	error = stat(path, &sb);
26575c734b04SJakub Wojciech Klama 	if (error != 0) {
26585c734b04SJakub Wojciech Klama 		log_warn("stat");
26595c734b04SJakub Wojciech Klama 		return;
26605c734b04SJakub Wojciech Klama 	}
26615c734b04SJakub Wojciech Klama 	if (sb.st_mode & S_IWOTH) {
26625c734b04SJakub Wojciech Klama 		log_warnx("%s is world-writable", path);
26635c734b04SJakub Wojciech Klama 	} else if (sb.st_mode & S_IROTH) {
26645c734b04SJakub Wojciech Klama 		log_warnx("%s is world-readable", path);
26655c734b04SJakub Wojciech Klama 	} else if (sb.st_mode & S_IXOTH) {
26665c734b04SJakub Wojciech Klama 		/*
26675c734b04SJakub Wojciech Klama 		 * Ok, this one doesn't matter, but still do it,
26685c734b04SJakub Wojciech Klama 		 * just for consistency.
26695c734b04SJakub Wojciech Klama 		 */
26705c734b04SJakub Wojciech Klama 		log_warnx("%s is world-executable", path);
26715c734b04SJakub Wojciech Klama 	}
26725c734b04SJakub Wojciech Klama 
26735c734b04SJakub Wojciech Klama 	/*
26745c734b04SJakub Wojciech Klama 	 * XXX: Should we also check for owner != 0?
26755c734b04SJakub Wojciech Klama 	 */
26765c734b04SJakub Wojciech Klama }
26775c734b04SJakub Wojciech Klama 
26785c734b04SJakub Wojciech Klama static struct conf *
conf_new_from_file(const char * path,bool ucl)2679969876fcSAlan Somers conf_new_from_file(const char *path, bool ucl)
26805c734b04SJakub Wojciech Klama {
26815c734b04SJakub Wojciech Klama 	struct conf *conf;
26825c734b04SJakub Wojciech Klama 	struct auth_group *ag;
26835c734b04SJakub Wojciech Klama 	struct portal_group *pg;
26845c734b04SJakub Wojciech Klama 	int error;
26855c734b04SJakub Wojciech Klama 
26865c734b04SJakub Wojciech Klama 	log_debugx("obtaining configuration from %s", path);
26875c734b04SJakub Wojciech Klama 
26885c734b04SJakub Wojciech Klama 	conf = conf_new();
26895c734b04SJakub Wojciech Klama 
26905c734b04SJakub Wojciech Klama 	ag = auth_group_new(conf, "default");
26915c734b04SJakub Wojciech Klama 	assert(ag != NULL);
26925c734b04SJakub Wojciech Klama 
26935c734b04SJakub Wojciech Klama 	ag = auth_group_new(conf, "no-authentication");
26945c734b04SJakub Wojciech Klama 	assert(ag != NULL);
26955c734b04SJakub Wojciech Klama 	ag->ag_type = AG_TYPE_NO_AUTHENTICATION;
26965c734b04SJakub Wojciech Klama 
26975c734b04SJakub Wojciech Klama 	ag = auth_group_new(conf, "no-access");
26985c734b04SJakub Wojciech Klama 	assert(ag != NULL);
26995c734b04SJakub Wojciech Klama 	ag->ag_type = AG_TYPE_DENY;
27005c734b04SJakub Wojciech Klama 
27015c734b04SJakub Wojciech Klama 	pg = portal_group_new(conf, "default");
27025c734b04SJakub Wojciech Klama 	assert(pg != NULL);
27035c734b04SJakub Wojciech Klama 
27045c734b04SJakub Wojciech Klama 	if (ucl)
27055c734b04SJakub Wojciech Klama 		error = uclparse_conf(conf, path);
27065c734b04SJakub Wojciech Klama 	else
27075c734b04SJakub Wojciech Klama 		error = parse_conf(conf, path);
27085c734b04SJakub Wojciech Klama 
27095c734b04SJakub Wojciech Klama 	if (error != 0) {
27105c734b04SJakub Wojciech Klama 		conf_delete(conf);
27115c734b04SJakub Wojciech Klama 		return (NULL);
27125c734b04SJakub Wojciech Klama 	}
27135c734b04SJakub Wojciech Klama 
27145c734b04SJakub Wojciech Klama 	check_perms(path);
27155c734b04SJakub Wojciech Klama 
27165c734b04SJakub Wojciech Klama 	if (conf->conf_default_ag_defined == false) {
27175c734b04SJakub Wojciech Klama 		log_debugx("auth-group \"default\" not defined; "
27185c734b04SJakub Wojciech Klama 		    "going with defaults");
27195c734b04SJakub Wojciech Klama 		ag = auth_group_find(conf, "default");
27205c734b04SJakub Wojciech Klama 		assert(ag != NULL);
27215c734b04SJakub Wojciech Klama 		ag->ag_type = AG_TYPE_DENY;
27225c734b04SJakub Wojciech Klama 	}
27235c734b04SJakub Wojciech Klama 
27245c734b04SJakub Wojciech Klama 	if (conf->conf_default_pg_defined == false) {
27255c734b04SJakub Wojciech Klama 		log_debugx("portal-group \"default\" not defined; "
27265c734b04SJakub Wojciech Klama 		    "going with defaults");
27275c734b04SJakub Wojciech Klama 		pg = portal_group_find(conf, "default");
27285c734b04SJakub Wojciech Klama 		assert(pg != NULL);
27295c734b04SJakub Wojciech Klama 		portal_group_add_listen(pg, "0.0.0.0:3260", false);
27305c734b04SJakub Wojciech Klama 		portal_group_add_listen(pg, "[::]:3260", false);
27315c734b04SJakub Wojciech Klama 	}
27325c734b04SJakub Wojciech Klama 
27335c734b04SJakub Wojciech Klama 	conf->conf_kernel_port_on = true;
27345c734b04SJakub Wojciech Klama 
27355c734b04SJakub Wojciech Klama 	error = conf_verify(conf);
27365c734b04SJakub Wojciech Klama 	if (error != 0) {
27375c734b04SJakub Wojciech Klama 		conf_delete(conf);
27385c734b04SJakub Wojciech Klama 		return (NULL);
27395c734b04SJakub Wojciech Klama 	}
27405c734b04SJakub Wojciech Klama 
27415c734b04SJakub Wojciech Klama 	return (conf);
27425c734b04SJakub Wojciech Klama }
27435c734b04SJakub Wojciech Klama 
2744969876fcSAlan Somers /*
2745969876fcSAlan Somers  * If the config file specifies physical ports for any target, associate them
2746969876fcSAlan Somers  * with the config file.  If necessary, create them.
2747969876fcSAlan Somers  */
2748969876fcSAlan Somers static int
new_pports_from_conf(struct conf * conf,struct kports * kports)2749969876fcSAlan Somers new_pports_from_conf(struct conf *conf, struct kports *kports)
2750969876fcSAlan Somers {
2751969876fcSAlan Somers 	struct target *targ;
2752969876fcSAlan Somers 	struct pport *pp;
2753969876fcSAlan Somers 	struct port *tp;
2754969876fcSAlan Somers 	int ret, i_pp, i_vp;
2755969876fcSAlan Somers 
2756969876fcSAlan Somers 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
2757969876fcSAlan Somers 		if (!targ->t_pport)
2758969876fcSAlan Somers 			continue;
2759969876fcSAlan Somers 
2760969876fcSAlan Somers 		ret = sscanf(targ->t_pport, "ioctl/%d/%d", &i_pp, &i_vp);
2761969876fcSAlan Somers 		if (ret > 0) {
2762969876fcSAlan Somers 			tp = port_new_ioctl(conf, kports, targ, i_pp, i_vp);
2763969876fcSAlan Somers 			if (tp == NULL) {
2764969876fcSAlan Somers 				log_warnx("can't create new ioctl port "
2765969876fcSAlan Somers 				    "for target \"%s\"", targ->t_name);
2766969876fcSAlan Somers 				return (1);
2767969876fcSAlan Somers 			}
2768969876fcSAlan Somers 
2769969876fcSAlan Somers 			continue;
2770969876fcSAlan Somers 		}
2771969876fcSAlan Somers 
2772969876fcSAlan Somers 		pp = pport_find(kports, targ->t_pport);
2773969876fcSAlan Somers 		if (pp == NULL) {
2774969876fcSAlan Somers 			log_warnx("unknown port \"%s\" for target \"%s\"",
2775969876fcSAlan Somers 			    targ->t_pport, targ->t_name);
2776969876fcSAlan Somers 			return (1);
2777969876fcSAlan Somers 		}
2778969876fcSAlan Somers 		if (!TAILQ_EMPTY(&pp->pp_ports)) {
2779969876fcSAlan Somers 			log_warnx("can't link port \"%s\" to target \"%s\", "
2780969876fcSAlan Somers 			    "port already linked to some target",
2781969876fcSAlan Somers 			    targ->t_pport, targ->t_name);
2782969876fcSAlan Somers 			return (1);
2783969876fcSAlan Somers 		}
2784969876fcSAlan Somers 		tp = port_new_pp(conf, targ, pp);
2785969876fcSAlan Somers 		if (tp == NULL) {
2786969876fcSAlan Somers 			log_warnx("can't link port \"%s\" to target \"%s\"",
2787969876fcSAlan Somers 			    targ->t_pport, targ->t_name);
2788969876fcSAlan Somers 			return (1);
2789969876fcSAlan Somers 		}
2790969876fcSAlan Somers 	}
2791969876fcSAlan Somers 	return (0);
2792969876fcSAlan Somers }
2793969876fcSAlan Somers 
2794009ea47eSEdward Tomasz Napierala int
main(int argc,char ** argv)2795009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
2796009ea47eSEdward Tomasz Napierala {
2797969876fcSAlan Somers 	struct kports kports;
2798009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
2799829603e2SAlexander Motin 	struct isns *newns;
2800009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
2801009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
2802*5f89aea7SAlan Somers 	pid_t otherpid;
2803009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
28047fcbecd0SEdward Tomasz Napierala 	bool test_config = false;
28055c734b04SJakub Wojciech Klama 	bool use_ucl = false;
2806009ea47eSEdward Tomasz Napierala 
28077fcbecd0SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "dtuf:R")) != -1) {
2808009ea47eSEdward Tomasz Napierala 		switch (ch) {
2809009ea47eSEdward Tomasz Napierala 		case 'd':
2810009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
2811009ea47eSEdward Tomasz Napierala 			debug++;
2812009ea47eSEdward Tomasz Napierala 			break;
28137fcbecd0SEdward Tomasz Napierala 		case 't':
28147fcbecd0SEdward Tomasz Napierala 			test_config = true;
28157fcbecd0SEdward Tomasz Napierala 			break;
28165c734b04SJakub Wojciech Klama 		case 'u':
28175c734b04SJakub Wojciech Klama 			use_ucl = true;
28185c734b04SJakub Wojciech Klama 			break;
2819009ea47eSEdward Tomasz Napierala 		case 'f':
2820009ea47eSEdward Tomasz Napierala 			config_path = optarg;
2821009ea47eSEdward Tomasz Napierala 			break;
2822ba3a2d31SEdward Tomasz Napierala 		case 'R':
2823ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
2824ba3a2d31SEdward Tomasz Napierala 			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2825ba3a2d31SEdward Tomasz Napierala 			    "does not support iSER protocol");
2826ba3a2d31SEdward Tomasz Napierala #endif
2827ba3a2d31SEdward Tomasz Napierala 			proxy_mode = true;
2828ba3a2d31SEdward Tomasz Napierala 			break;
2829009ea47eSEdward Tomasz Napierala 		case '?':
2830009ea47eSEdward Tomasz Napierala 		default:
2831009ea47eSEdward Tomasz Napierala 			usage();
2832009ea47eSEdward Tomasz Napierala 		}
2833009ea47eSEdward Tomasz Napierala 	}
2834009ea47eSEdward Tomasz Napierala 	argc -= optind;
2835009ea47eSEdward Tomasz Napierala 	if (argc != 0)
2836009ea47eSEdward Tomasz Napierala 		usage();
2837009ea47eSEdward Tomasz Napierala 
2838009ea47eSEdward Tomasz Napierala 	log_init(debug);
2839009ea47eSEdward Tomasz Napierala 	kernel_init();
2840009ea47eSEdward Tomasz Napierala 
2841969876fcSAlan Somers 	TAILQ_INIT(&kports.pports);
2842969876fcSAlan Somers 	newconf = conf_new_from_file(config_path, use_ucl);
28435c734b04SJakub Wojciech Klama 
2844009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
2845fe845cdbSEdward Tomasz Napierala 		log_errx(1, "configuration error; exiting");
28467fcbecd0SEdward Tomasz Napierala 
28477fcbecd0SEdward Tomasz Napierala 	if (test_config)
28487fcbecd0SEdward Tomasz Napierala 		return (0);
28497fcbecd0SEdward Tomasz Napierala 
2850*5f89aea7SAlan Somers 	assert(newconf->conf_pidfile_path != NULL);
2851*5f89aea7SAlan Somers 	log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
2852*5f89aea7SAlan Somers 	newconf->conf_pidfh = pidfile_open(newconf->conf_pidfile_path, 0600,
2853*5f89aea7SAlan Somers 		&otherpid);
2854*5f89aea7SAlan Somers 	if (newconf->conf_pidfh == NULL) {
2855*5f89aea7SAlan Somers 		if (errno == EEXIST)
2856*5f89aea7SAlan Somers 			log_errx(1, "daemon already running, pid: %jd.",
2857*5f89aea7SAlan Somers 			    (intmax_t)otherpid);
2858*5f89aea7SAlan Somers 		log_err(1, "cannot open or create pidfile \"%s\"",
2859*5f89aea7SAlan Somers 		    newconf->conf_pidfile_path);
2860*5f89aea7SAlan Somers 	}
2861*5f89aea7SAlan Somers 
2862*5f89aea7SAlan Somers 	register_signals();
2863*5f89aea7SAlan Somers 
2864*5f89aea7SAlan Somers 	oldconf = conf_new_from_kernel(&kports);
2865*5f89aea7SAlan Somers 
2866009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
2867009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
2868009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
2869009ea47eSEdward Tomasz Napierala 	}
2870009ea47eSEdward Tomasz Napierala 
2871969876fcSAlan Somers 	if (new_pports_from_conf(newconf, &kports))
2872969876fcSAlan Somers 		log_errx(1, "Error associating physical ports; exiting");
2873969876fcSAlan Somers 
2874009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
2875009ea47eSEdward Tomasz Napierala 	if (error != 0)
2876fe845cdbSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration; exiting");
2877fe845cdbSEdward Tomasz Napierala 
2878009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
2879009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
2880009ea47eSEdward Tomasz Napierala 
28815d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
28825d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
28835d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
28845d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
28855d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
28865d5a95f1SEdward Tomasz Napierala 			exit(1);
28875d5a95f1SEdward Tomasz Napierala 		}
28885d5a95f1SEdward Tomasz Napierala 	}
28895d5a95f1SEdward Tomasz Napierala 
2890829603e2SAlexander Motin 	/* Schedule iSNS update */
2891829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2892829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2893829603e2SAlexander Motin 
2894009ea47eSEdward Tomasz Napierala 	for (;;) {
2895009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
2896009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
2897009ea47eSEdward Tomasz Napierala 			sighup_received = false;
2898009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
2899969876fcSAlan Somers 			tmpconf = conf_new_from_file(config_path, use_ucl);
29005c734b04SJakub Wojciech Klama 
2901009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
2902009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
2903009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
2904969876fcSAlan Somers 			} else if (new_pports_from_conf(tmpconf, &kports)) {
2905969876fcSAlan Somers 				log_warnx("Error associating physical ports, "
2906969876fcSAlan Somers 				    "continuing with old configuration");
2907969876fcSAlan Somers 				conf_delete(tmpconf);
2908009ea47eSEdward Tomasz Napierala 			} else {
2909009ea47eSEdward Tomasz Napierala 				if (debug > 0)
2910009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
2911009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
2912009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
2913969876fcSAlan Somers 
2914009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
2915009ea47eSEdward Tomasz Napierala 				if (error != 0)
2916009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
2917009ea47eSEdward Tomasz Napierala 					    "configuration");
2918009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
2919009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
2920009ea47eSEdward Tomasz Napierala 			}
2921009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
2922009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
2923009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
2924009ea47eSEdward Tomasz Napierala 
29256b31e130SAlexander Motin 			log_debugx("removing CTL iSCSI ports "
2926009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
2927009ea47eSEdward Tomasz Napierala 
2928009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
2929009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
2930009ea47eSEdward Tomasz Napierala 			if (debug > 0)
2931009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
2932009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
2933009ea47eSEdward Tomasz Napierala 			if (error != 0)
2934009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
2935*5f89aea7SAlan Somers 			if (oldconf->conf_pidfh) {
2936*5f89aea7SAlan Somers 				pidfile_remove(oldconf->conf_pidfh);
2937*5f89aea7SAlan Somers 				oldconf->conf_pidfh = NULL;
2938*5f89aea7SAlan Somers 			}
29392909ddd1SAlan Somers 			conf_delete(newconf);
2940829603e2SAlexander Motin 			conf_delete(oldconf);
2941829603e2SAlexander Motin 			oldconf = NULL;
2942009ea47eSEdward Tomasz Napierala 
2943009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
2944009ea47eSEdward Tomasz Napierala 			exit(0);
2945009ea47eSEdward Tomasz Napierala 		} else {
2946009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
2947009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2948829603e2SAlexander Motin 			if (timed_out()) {
2949829603e2SAlexander Motin 				set_timeout(0, false);
2950829603e2SAlexander Motin 				TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2951829603e2SAlexander Motin 					isns_check(newns);
2952829603e2SAlexander Motin 				/* Schedule iSNS update */
2953829603e2SAlexander Motin 				if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2954829603e2SAlexander Motin 					set_timeout((newconf->conf_isns_period
2955829603e2SAlexander Motin 					    + 2) / 3,
2956829603e2SAlexander Motin 					    false);
2957829603e2SAlexander Motin 				}
2958829603e2SAlexander Motin 			}
2959009ea47eSEdward Tomasz Napierala 		}
2960009ea47eSEdward Tomasz Napierala 	}
2961009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
2962009ea47eSEdward Tomasz Napierala }
2963