ctld.c revision 293290
1255570Strasz/*-
2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation
3255570Strasz * All rights reserved.
4255570Strasz *
5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6255570Strasz * from the FreeBSD Foundation.
7255570Strasz *
8255570Strasz * Redistribution and use in source and binary forms, with or without
9255570Strasz * modification, are permitted provided that the following conditions
10255570Strasz * are met:
11255570Strasz * 1. Redistributions of source code must retain the above copyright
12255570Strasz *    notice, this list of conditions and the following disclaimer.
13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright
14255570Strasz *    notice, this list of conditions and the following disclaimer in the
15255570Strasz *    documentation and/or other materials provided with the distribution.
16255570Strasz *
17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20255570Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27255570Strasz * SUCH DAMAGE.
28255570Strasz *
29255570Strasz */
30255570Strasz
31270888Strasz#include <sys/cdefs.h>
32270888Strasz__FBSDID("$FreeBSD: stable/10/usr.sbin/ctld/ctld.c 293290 2016-01-07 00:40:51Z bdrewery $");
33270888Strasz
34255570Strasz#include <sys/types.h>
35255570Strasz#include <sys/time.h>
36255570Strasz#include <sys/socket.h>
37255570Strasz#include <sys/wait.h>
38255570Strasz#include <netinet/in.h>
39270137Smav#include <arpa/inet.h>
40255570Strasz#include <assert.h>
41255570Strasz#include <ctype.h>
42255570Strasz#include <errno.h>
43255570Strasz#include <netdb.h>
44255570Strasz#include <signal.h>
45255570Strasz#include <stdbool.h>
46255570Strasz#include <stdio.h>
47255570Strasz#include <stdint.h>
48255570Strasz#include <stdlib.h>
49255570Strasz#include <string.h>
50255570Strasz#include <unistd.h>
51255570Strasz
52255570Strasz#include "ctld.h"
53274939Smav#include "isns.h"
54255570Strasz
55265507Straszbool proxy_mode = false;
56265507Strasz
57255570Straszstatic volatile bool sighup_received = false;
58255570Straszstatic volatile bool sigterm_received = false;
59255570Straszstatic volatile bool sigalrm_received = false;
60255570Strasz
61255570Straszstatic int nchildren = 0;
62288729Smavstatic uint16_t last_portal_group_tag = 0xff;
63255570Strasz
64255570Straszstatic void
65255570Straszusage(void)
66255570Strasz{
67255570Strasz
68255570Strasz	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
69255570Strasz	exit(1);
70255570Strasz}
71255570Strasz
72255570Straszchar *
73255570Straszchecked_strdup(const char *s)
74255570Strasz{
75255570Strasz	char *c;
76255570Strasz
77255570Strasz	c = strdup(s);
78255570Strasz	if (c == NULL)
79255570Strasz		log_err(1, "strdup");
80255570Strasz	return (c);
81255570Strasz}
82255570Strasz
83255570Straszstruct conf *
84255570Straszconf_new(void)
85255570Strasz{
86255570Strasz	struct conf *conf;
87255570Strasz
88255570Strasz	conf = calloc(1, sizeof(*conf));
89255570Strasz	if (conf == NULL)
90255570Strasz		log_err(1, "calloc");
91279002Smav	TAILQ_INIT(&conf->conf_luns);
92255570Strasz	TAILQ_INIT(&conf->conf_targets);
93255570Strasz	TAILQ_INIT(&conf->conf_auth_groups);
94279006Smav	TAILQ_INIT(&conf->conf_ports);
95255570Strasz	TAILQ_INIT(&conf->conf_portal_groups);
96279055Smav	TAILQ_INIT(&conf->conf_pports);
97274939Smav	TAILQ_INIT(&conf->conf_isns);
98255570Strasz
99274939Smav	conf->conf_isns_period = 900;
100274939Smav	conf->conf_isns_timeout = 5;
101255570Strasz	conf->conf_debug = 0;
102255570Strasz	conf->conf_timeout = 60;
103255570Strasz	conf->conf_maxproc = 30;
104255570Strasz
105255570Strasz	return (conf);
106255570Strasz}
107255570Strasz
108255570Straszvoid
109255570Straszconf_delete(struct conf *conf)
110255570Strasz{
111279002Smav	struct lun *lun, *ltmp;
112255570Strasz	struct target *targ, *tmp;
113255570Strasz	struct auth_group *ag, *cagtmp;
114255570Strasz	struct portal_group *pg, *cpgtmp;
115279055Smav	struct pport *pp, *pptmp;
116274939Smav	struct isns *is, *istmp;
117255570Strasz
118255570Strasz	assert(conf->conf_pidfh == NULL);
119255570Strasz
120279002Smav	TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
121279002Smav		lun_delete(lun);
122255570Strasz	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
123255570Strasz		target_delete(targ);
124255570Strasz	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
125255570Strasz		auth_group_delete(ag);
126255570Strasz	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
127255570Strasz		portal_group_delete(pg);
128279055Smav	TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
129279055Smav		pport_delete(pp);
130274939Smav	TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
131274939Smav		isns_delete(is);
132279006Smav	assert(TAILQ_EMPTY(&conf->conf_ports));
133255570Strasz	free(conf->conf_pidfile_path);
134255570Strasz	free(conf);
135255570Strasz}
136255570Strasz
137255570Straszstatic struct auth *
138255570Straszauth_new(struct auth_group *ag)
139255570Strasz{
140255570Strasz	struct auth *auth;
141255570Strasz
142255570Strasz	auth = calloc(1, sizeof(*auth));
143255570Strasz	if (auth == NULL)
144255570Strasz		log_err(1, "calloc");
145255570Strasz	auth->a_auth_group = ag;
146255570Strasz	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
147255570Strasz	return (auth);
148255570Strasz}
149255570Strasz
150255570Straszstatic void
151255570Straszauth_delete(struct auth *auth)
152255570Strasz{
153255570Strasz	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
154255570Strasz
155255570Strasz	free(auth->a_user);
156255570Strasz	free(auth->a_secret);
157255570Strasz	free(auth->a_mutual_user);
158255570Strasz	free(auth->a_mutual_secret);
159255570Strasz	free(auth);
160255570Strasz}
161255570Strasz
162255570Straszconst struct auth *
163265514Straszauth_find(const struct auth_group *ag, const char *user)
164255570Strasz{
165255570Strasz	const struct auth *auth;
166255570Strasz
167255570Strasz	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
168255570Strasz		if (strcmp(auth->a_user, user) == 0)
169255570Strasz			return (auth);
170255570Strasz	}
171255570Strasz
172255570Strasz	return (NULL);
173255570Strasz}
174255570Strasz
175263721Straszstatic void
176263721Straszauth_check_secret_length(struct auth *auth)
177263721Strasz{
178263721Strasz	size_t len;
179263721Strasz
180263721Strasz	len = strlen(auth->a_secret);
181263721Strasz	if (len > 16) {
182263721Strasz		if (auth->a_auth_group->ag_name != NULL)
183263721Strasz			log_warnx("secret for user \"%s\", auth-group \"%s\", "
184263721Strasz			    "is too long; it should be at most 16 characters "
185263721Strasz			    "long", auth->a_user, auth->a_auth_group->ag_name);
186263721Strasz		else
187263721Strasz			log_warnx("secret for user \"%s\", target \"%s\", "
188263721Strasz			    "is too long; it should be at most 16 characters "
189263721Strasz			    "long", auth->a_user,
190263723Strasz			    auth->a_auth_group->ag_target->t_name);
191263721Strasz	}
192263721Strasz	if (len < 12) {
193263721Strasz		if (auth->a_auth_group->ag_name != NULL)
194263721Strasz			log_warnx("secret for user \"%s\", auth-group \"%s\", "
195263721Strasz			    "is too short; it should be at least 12 characters "
196263721Strasz			    "long", auth->a_user,
197263721Strasz			    auth->a_auth_group->ag_name);
198263721Strasz		else
199263721Strasz			log_warnx("secret for user \"%s\", target \"%s\", "
200263721Strasz			    "is too short; it should be at least 16 characters "
201263721Strasz			    "long", auth->a_user,
202263723Strasz			    auth->a_auth_group->ag_target->t_name);
203263721Strasz	}
204263721Strasz
205263721Strasz	if (auth->a_mutual_secret != NULL) {
206281187Sjpaetzel		len = strlen(auth->a_mutual_secret);
207263721Strasz		if (len > 16) {
208263721Strasz			if (auth->a_auth_group->ag_name != NULL)
209263721Strasz				log_warnx("mutual secret for user \"%s\", "
210263721Strasz				    "auth-group \"%s\", is too long; it should "
211263721Strasz				    "be at most 16 characters long",
212263721Strasz				    auth->a_user, auth->a_auth_group->ag_name);
213263721Strasz			else
214263721Strasz				log_warnx("mutual secret for user \"%s\", "
215263721Strasz				    "target \"%s\", is too long; it should "
216263721Strasz				    "be at most 16 characters long",
217263721Strasz				    auth->a_user,
218263723Strasz				    auth->a_auth_group->ag_target->t_name);
219263721Strasz		}
220263721Strasz		if (len < 12) {
221263721Strasz			if (auth->a_auth_group->ag_name != NULL)
222263721Strasz				log_warnx("mutual secret for user \"%s\", "
223263721Strasz				    "auth-group \"%s\", is too short; it "
224263721Strasz				    "should be at least 12 characters long",
225263721Strasz				    auth->a_user, auth->a_auth_group->ag_name);
226263721Strasz			else
227263721Strasz				log_warnx("mutual secret for user \"%s\", "
228263721Strasz				    "target \"%s\", is too short; it should be "
229263721Strasz				    "at least 16 characters long",
230263721Strasz				    auth->a_user,
231263723Strasz				    auth->a_auth_group->ag_target->t_name);
232263721Strasz		}
233263721Strasz	}
234263721Strasz}
235263721Strasz
236263721Straszconst struct auth *
237263721Straszauth_new_chap(struct auth_group *ag, const char *user,
238263721Strasz    const char *secret)
239263721Strasz{
240263721Strasz	struct auth *auth;
241263721Strasz
242263721Strasz	if (ag->ag_type == AG_TYPE_UNKNOWN)
243263721Strasz		ag->ag_type = AG_TYPE_CHAP;
244263721Strasz	if (ag->ag_type != AG_TYPE_CHAP) {
245263721Strasz		if (ag->ag_name != NULL)
246263721Strasz			log_warnx("cannot mix \"chap\" authentication with "
247263721Strasz			    "other types for auth-group \"%s\"", ag->ag_name);
248263721Strasz		else
249263721Strasz			log_warnx("cannot mix \"chap\" authentication with "
250263721Strasz			    "other types for target \"%s\"",
251263723Strasz			    ag->ag_target->t_name);
252263721Strasz		return (NULL);
253263721Strasz	}
254263721Strasz
255263721Strasz	auth = auth_new(ag);
256263721Strasz	auth->a_user = checked_strdup(user);
257263721Strasz	auth->a_secret = checked_strdup(secret);
258263721Strasz
259263721Strasz	auth_check_secret_length(auth);
260263721Strasz
261263721Strasz	return (auth);
262263721Strasz}
263263721Strasz
264263721Straszconst struct auth *
265263721Straszauth_new_chap_mutual(struct auth_group *ag, const char *user,
266263721Strasz    const char *secret, const char *user2, const char *secret2)
267263721Strasz{
268263721Strasz	struct auth *auth;
269263721Strasz
270263721Strasz	if (ag->ag_type == AG_TYPE_UNKNOWN)
271263721Strasz		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
272263721Strasz	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
273263721Strasz		if (ag->ag_name != NULL)
274263721Strasz			log_warnx("cannot mix \"chap-mutual\" authentication "
275263721Strasz			    "with other types for auth-group \"%s\"",
276274870Strasz			    ag->ag_name);
277263721Strasz		else
278263721Strasz			log_warnx("cannot mix \"chap-mutual\" authentication "
279263721Strasz			    "with other types for target \"%s\"",
280263723Strasz			    ag->ag_target->t_name);
281263721Strasz		return (NULL);
282263721Strasz	}
283263721Strasz
284263721Strasz	auth = auth_new(ag);
285263721Strasz	auth->a_user = checked_strdup(user);
286263721Strasz	auth->a_secret = checked_strdup(secret);
287263721Strasz	auth->a_mutual_user = checked_strdup(user2);
288263721Strasz	auth->a_mutual_secret = checked_strdup(secret2);
289263721Strasz
290263721Strasz	auth_check_secret_length(auth);
291263721Strasz
292263721Strasz	return (auth);
293263721Strasz}
294263721Strasz
295263720Straszconst struct auth_name *
296263720Straszauth_name_new(struct auth_group *ag, const char *name)
297263720Strasz{
298263720Strasz	struct auth_name *an;
299263720Strasz
300263720Strasz	an = calloc(1, sizeof(*an));
301263720Strasz	if (an == NULL)
302263720Strasz		log_err(1, "calloc");
303263720Strasz	an->an_auth_group = ag;
304263720Strasz	an->an_initator_name = checked_strdup(name);
305263720Strasz	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
306263720Strasz	return (an);
307263720Strasz}
308263720Strasz
309263720Straszstatic void
310263720Straszauth_name_delete(struct auth_name *an)
311263720Strasz{
312263720Strasz	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
313263720Strasz
314263720Strasz	free(an->an_initator_name);
315263720Strasz	free(an);
316263720Strasz}
317263720Strasz
318263720Straszbool
319263720Straszauth_name_defined(const struct auth_group *ag)
320263720Strasz{
321263720Strasz	if (TAILQ_EMPTY(&ag->ag_names))
322263720Strasz		return (false);
323263720Strasz	return (true);
324263720Strasz}
325263720Strasz
326263720Straszconst struct auth_name *
327263720Straszauth_name_find(const struct auth_group *ag, const char *name)
328263720Strasz{
329263720Strasz	const struct auth_name *auth_name;
330263720Strasz
331263720Strasz	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
332263720Strasz		if (strcmp(auth_name->an_initator_name, name) == 0)
333263720Strasz			return (auth_name);
334263720Strasz	}
335263720Strasz
336263720Strasz	return (NULL);
337263720Strasz}
338263720Strasz
339274949Straszint
340274949Straszauth_name_check(const struct auth_group *ag, const char *initiator_name)
341274949Strasz{
342274949Strasz	if (!auth_name_defined(ag))
343274949Strasz		return (0);
344274949Strasz
345274949Strasz	if (auth_name_find(ag, initiator_name) == NULL)
346274949Strasz		return (1);
347274949Strasz
348274949Strasz	return (0);
349274949Strasz}
350274949Strasz
351263720Straszconst struct auth_portal *
352263720Straszauth_portal_new(struct auth_group *ag, const char *portal)
353263720Strasz{
354263720Strasz	struct auth_portal *ap;
355270137Smav	char *net, *mask, *str, *tmp;
356270137Smav	int len, dm, m;
357263720Strasz
358263720Strasz	ap = calloc(1, sizeof(*ap));
359263720Strasz	if (ap == NULL)
360263720Strasz		log_err(1, "calloc");
361263720Strasz	ap->ap_auth_group = ag;
362263720Strasz	ap->ap_initator_portal = checked_strdup(portal);
363270137Smav	mask = str = checked_strdup(portal);
364270137Smav	net = strsep(&mask, "/");
365270137Smav	if (net[0] == '[')
366270137Smav		net++;
367270137Smav	len = strlen(net);
368270137Smav	if (len == 0)
369270137Smav		goto error;
370270137Smav	if (net[len - 1] == ']')
371270137Smav		net[len - 1] = 0;
372270137Smav	if (strchr(net, ':') != NULL) {
373270137Smav		struct sockaddr_in6 *sin6 =
374270137Smav		    (struct sockaddr_in6 *)&ap->ap_sa;
375270137Smav
376270137Smav		sin6->sin6_len = sizeof(*sin6);
377270137Smav		sin6->sin6_family = AF_INET6;
378270137Smav		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
379270137Smav			goto error;
380270137Smav		dm = 128;
381270137Smav	} else {
382270137Smav		struct sockaddr_in *sin =
383270137Smav		    (struct sockaddr_in *)&ap->ap_sa;
384270137Smav
385270137Smav		sin->sin_len = sizeof(*sin);
386270137Smav		sin->sin_family = AF_INET;
387270137Smav		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
388270137Smav			goto error;
389270137Smav		dm = 32;
390270137Smav	}
391270137Smav	if (mask != NULL) {
392270137Smav		m = strtol(mask, &tmp, 0);
393270137Smav		if (m < 0 || m > dm || tmp[0] != 0)
394270137Smav			goto error;
395270137Smav	} else
396270137Smav		m = dm;
397270137Smav	ap->ap_mask = m;
398270137Smav	free(str);
399263720Strasz	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
400263720Strasz	return (ap);
401270137Smav
402270137Smaverror:
403279481Smav	free(ap);
404270137Smav	log_errx(1, "Incorrect initiator portal '%s'", portal);
405270137Smav	return (NULL);
406263720Strasz}
407263720Strasz
408263720Straszstatic void
409263720Straszauth_portal_delete(struct auth_portal *ap)
410263720Strasz{
411263720Strasz	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
412263720Strasz
413263720Strasz	free(ap->ap_initator_portal);
414263720Strasz	free(ap);
415263720Strasz}
416263720Strasz
417263720Straszbool
418263720Straszauth_portal_defined(const struct auth_group *ag)
419263720Strasz{
420263720Strasz	if (TAILQ_EMPTY(&ag->ag_portals))
421263720Strasz		return (false);
422263720Strasz	return (true);
423263720Strasz}
424263720Strasz
425263720Straszconst struct auth_portal *
426270137Smavauth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
427263720Strasz{
428270137Smav	const struct auth_portal *ap;
429270137Smav	const uint8_t *a, *b;
430270137Smav	int i;
431270137Smav	uint8_t bmask;
432263720Strasz
433270137Smav	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
434270137Smav		if (ap->ap_sa.ss_family != ss->ss_family)
435270137Smav			continue;
436270137Smav		if (ss->ss_family == AF_INET) {
437270137Smav			a = (const uint8_t *)
438270137Smav			    &((const struct sockaddr_in *)ss)->sin_addr;
439270137Smav			b = (const uint8_t *)
440270137Smav			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
441270137Smav		} else {
442270137Smav			a = (const uint8_t *)
443270137Smav			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
444270137Smav			b = (const uint8_t *)
445270137Smav			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
446270137Smav		}
447270137Smav		for (i = 0; i < ap->ap_mask / 8; i++) {
448270137Smav			if (a[i] != b[i])
449270137Smav				goto next;
450270137Smav		}
451270137Smav		if (ap->ap_mask % 8) {
452270137Smav			bmask = 0xff << (8 - (ap->ap_mask % 8));
453270137Smav			if ((a[i] & bmask) != (b[i] & bmask))
454270137Smav				goto next;
455270137Smav		}
456270137Smav		return (ap);
457270137Smavnext:
458270137Smav		;
459263720Strasz	}
460263720Strasz
461263720Strasz	return (NULL);
462263720Strasz}
463263720Strasz
464274949Straszint
465274949Straszauth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
466274949Strasz{
467274949Strasz
468274949Strasz	if (!auth_portal_defined(ag))
469274949Strasz		return (0);
470274949Strasz
471274949Strasz	if (auth_portal_find(ag, sa) == NULL)
472274949Strasz		return (1);
473274949Strasz
474274949Strasz	return (0);
475274949Strasz}
476274949Strasz
477255570Straszstruct auth_group *
478255570Straszauth_group_new(struct conf *conf, const char *name)
479255570Strasz{
480255570Strasz	struct auth_group *ag;
481255570Strasz
482255570Strasz	if (name != NULL) {
483255570Strasz		ag = auth_group_find(conf, name);
484255570Strasz		if (ag != NULL) {
485255570Strasz			log_warnx("duplicated auth-group \"%s\"", name);
486255570Strasz			return (NULL);
487255570Strasz		}
488255570Strasz	}
489255570Strasz
490255570Strasz	ag = calloc(1, sizeof(*ag));
491255570Strasz	if (ag == NULL)
492255570Strasz		log_err(1, "calloc");
493255570Strasz	if (name != NULL)
494255570Strasz		ag->ag_name = checked_strdup(name);
495255570Strasz	TAILQ_INIT(&ag->ag_auths);
496263720Strasz	TAILQ_INIT(&ag->ag_names);
497263720Strasz	TAILQ_INIT(&ag->ag_portals);
498255570Strasz	ag->ag_conf = conf;
499255570Strasz	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
500255570Strasz
501255570Strasz	return (ag);
502255570Strasz}
503255570Strasz
504255570Straszvoid
505255570Straszauth_group_delete(struct auth_group *ag)
506255570Strasz{
507263720Strasz	struct auth *auth, *auth_tmp;
508263720Strasz	struct auth_name *auth_name, *auth_name_tmp;
509263720Strasz	struct auth_portal *auth_portal, *auth_portal_tmp;
510255570Strasz
511255570Strasz	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
512255570Strasz
513263720Strasz	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
514255570Strasz		auth_delete(auth);
515263720Strasz	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
516263720Strasz		auth_name_delete(auth_name);
517263720Strasz	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
518263720Strasz	    auth_portal_tmp)
519263720Strasz		auth_portal_delete(auth_portal);
520255570Strasz	free(ag->ag_name);
521255570Strasz	free(ag);
522255570Strasz}
523255570Strasz
524255570Straszstruct auth_group *
525265514Straszauth_group_find(const struct conf *conf, const char *name)
526255570Strasz{
527255570Strasz	struct auth_group *ag;
528255570Strasz
529255570Strasz	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
530255570Strasz		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
531255570Strasz			return (ag);
532255570Strasz	}
533255570Strasz
534255570Strasz	return (NULL);
535255570Strasz}
536255570Strasz
537263724Straszint
538275245Straszauth_group_set_type(struct auth_group *ag, const char *str)
539263724Strasz{
540275245Strasz	int type;
541263724Strasz
542263724Strasz	if (strcmp(str, "none") == 0) {
543263724Strasz		type = AG_TYPE_NO_AUTHENTICATION;
544263729Strasz	} else if (strcmp(str, "deny") == 0) {
545263729Strasz		type = AG_TYPE_DENY;
546263724Strasz	} else if (strcmp(str, "chap") == 0) {
547263724Strasz		type = AG_TYPE_CHAP;
548263724Strasz	} else if (strcmp(str, "chap-mutual") == 0) {
549263724Strasz		type = AG_TYPE_CHAP_MUTUAL;
550263724Strasz	} else {
551263724Strasz		if (ag->ag_name != NULL)
552263724Strasz			log_warnx("invalid auth-type \"%s\" for auth-group "
553263724Strasz			    "\"%s\"", str, ag->ag_name);
554263724Strasz		else
555263724Strasz			log_warnx("invalid auth-type \"%s\" for target "
556263724Strasz			    "\"%s\"", str, ag->ag_target->t_name);
557263724Strasz		return (1);
558263724Strasz	}
559263724Strasz
560275245Strasz	if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
561275245Strasz		if (ag->ag_name != NULL) {
562263724Strasz			log_warnx("cannot set auth-type to \"%s\" for "
563263724Strasz			    "auth-group \"%s\"; already has a different "
564263724Strasz			    "type", str, ag->ag_name);
565275245Strasz		} else {
566263724Strasz			log_warnx("cannot set auth-type to \"%s\" for target "
567263724Strasz			    "\"%s\"; already has a different type",
568263724Strasz			    str, ag->ag_target->t_name);
569275245Strasz		}
570263724Strasz		return (1);
571263724Strasz	}
572263724Strasz
573275245Strasz	ag->ag_type = type;
574275245Strasz
575275245Strasz	return (0);
576263724Strasz}
577263724Strasz
578255570Straszstatic struct portal *
579255570Straszportal_new(struct portal_group *pg)
580255570Strasz{
581255570Strasz	struct portal *portal;
582255570Strasz
583255570Strasz	portal = calloc(1, sizeof(*portal));
584255570Strasz	if (portal == NULL)
585255570Strasz		log_err(1, "calloc");
586255570Strasz	TAILQ_INIT(&portal->p_targets);
587255570Strasz	portal->p_portal_group = pg;
588255570Strasz	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
589255570Strasz	return (portal);
590255570Strasz}
591255570Strasz
592255570Straszstatic void
593255570Straszportal_delete(struct portal *portal)
594255570Strasz{
595271632Strasz
596255570Strasz	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
597271632Strasz	if (portal->p_ai != NULL)
598271632Strasz		freeaddrinfo(portal->p_ai);
599255570Strasz	free(portal->p_listen);
600255570Strasz	free(portal);
601255570Strasz}
602255570Strasz
603255570Straszstruct portal_group *
604255570Straszportal_group_new(struct conf *conf, const char *name)
605255570Strasz{
606255570Strasz	struct portal_group *pg;
607255570Strasz
608255678Strasz	pg = portal_group_find(conf, name);
609255678Strasz	if (pg != NULL) {
610255678Strasz		log_warnx("duplicated portal-group \"%s\"", name);
611255678Strasz		return (NULL);
612255570Strasz	}
613255570Strasz
614255570Strasz	pg = calloc(1, sizeof(*pg));
615255570Strasz	if (pg == NULL)
616255570Strasz		log_err(1, "calloc");
617255570Strasz	pg->pg_name = checked_strdup(name);
618291387Smav	TAILQ_INIT(&pg->pg_options);
619255570Strasz	TAILQ_INIT(&pg->pg_portals);
620279006Smav	TAILQ_INIT(&pg->pg_ports);
621255570Strasz	pg->pg_conf = conf;
622279003Smav	pg->pg_tag = 0;		/* Assigned later in conf_apply(). */
623255570Strasz	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
624255570Strasz
625255570Strasz	return (pg);
626255570Strasz}
627255570Strasz
628255570Straszvoid
629255570Straszportal_group_delete(struct portal_group *pg)
630255570Strasz{
631255570Strasz	struct portal *portal, *tmp;
632279006Smav	struct port *port, *tport;
633291387Smav	struct option *o, *otmp;
634255570Strasz
635279006Smav	TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
636279006Smav		port_delete(port);
637255570Strasz	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
638255570Strasz
639255570Strasz	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
640255570Strasz		portal_delete(portal);
641291387Smav	TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
642291387Smav		option_delete(&pg->pg_options, o);
643255570Strasz	free(pg->pg_name);
644275642Strasz	free(pg->pg_redirection);
645255570Strasz	free(pg);
646255570Strasz}
647255570Strasz
648255570Straszstruct portal_group *
649265514Straszportal_group_find(const struct conf *conf, const char *name)
650255570Strasz{
651255570Strasz	struct portal_group *pg;
652255570Strasz
653255570Strasz	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
654255570Strasz		if (strcmp(pg->pg_name, name) == 0)
655255570Strasz			return (pg);
656255570Strasz	}
657255570Strasz
658255570Strasz	return (NULL);
659255570Strasz}
660255570Strasz
661274939Smavstatic int
662274939Smavparse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
663255570Strasz{
664255570Strasz	struct addrinfo hints;
665275674Smav	char *str, *addr, *ch;
666255570Strasz	const char *port;
667255570Strasz	int error, colons = 0;
668255570Strasz
669275674Smav	str = arg = strdup(arg);
670255570Strasz	if (arg[0] == '[') {
671255570Strasz		/*
672255570Strasz		 * IPv6 address in square brackets, perhaps with port.
673255570Strasz		 */
674255570Strasz		arg++;
675255570Strasz		addr = strsep(&arg, "]");
676274939Smav		if (arg == NULL)
677255570Strasz			return (1);
678255570Strasz		if (arg[0] == '\0') {
679274939Smav			port = def_port;
680255570Strasz		} else if (arg[0] == ':') {
681255570Strasz			port = arg + 1;
682275674Smav		} else {
683275674Smav			free(str);
684255570Strasz			return (1);
685275674Smav		}
686255570Strasz	} else {
687255570Strasz		/*
688255570Strasz		 * Either IPv6 address without brackets - and without
689255570Strasz		 * a port - or IPv4 address.  Just count the colons.
690255570Strasz		 */
691255570Strasz		for (ch = arg; *ch != '\0'; ch++) {
692255570Strasz			if (*ch == ':')
693255570Strasz				colons++;
694255570Strasz		}
695255570Strasz		if (colons > 1) {
696255570Strasz			addr = arg;
697274939Smav			port = def_port;
698255570Strasz		} else {
699255570Strasz			addr = strsep(&arg, ":");
700255570Strasz			if (arg == NULL)
701274939Smav				port = def_port;
702255570Strasz			else
703255570Strasz				port = arg;
704255570Strasz		}
705255570Strasz	}
706255570Strasz
707255570Strasz	memset(&hints, 0, sizeof(hints));
708255570Strasz	hints.ai_family = PF_UNSPEC;
709255570Strasz	hints.ai_socktype = SOCK_STREAM;
710255570Strasz	hints.ai_flags = AI_PASSIVE;
711274939Smav	error = getaddrinfo(addr, port, &hints, ai);
712275674Smav	free(str);
713275674Smav	return ((error != 0) ? 1 : 0);
714274939Smav}
715255570Strasz
716274939Smavint
717274939Smavportal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
718274939Smav{
719274939Smav	struct portal *portal;
720274939Smav
721274939Smav	portal = portal_new(pg);
722274939Smav	portal->p_listen = checked_strdup(value);
723274939Smav	portal->p_iser = iser;
724274939Smav
725274939Smav	if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
726274939Smav		log_warnx("invalid listen address %s", portal->p_listen);
727271632Strasz		portal_delete(portal);
728255570Strasz		return (1);
729255570Strasz	}
730255570Strasz
731255570Strasz	/*
732255570Strasz	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
733255570Strasz	 *	those into multiple portals.
734255570Strasz	 */
735255570Strasz
736255570Strasz	return (0);
737255570Strasz}
738255570Strasz
739274939Smavint
740274939Smavisns_new(struct conf *conf, const char *addr)
741274939Smav{
742274939Smav	struct isns *isns;
743274939Smav
744274939Smav	isns = calloc(1, sizeof(*isns));
745274939Smav	if (isns == NULL)
746274939Smav		log_err(1, "calloc");
747274939Smav	isns->i_conf = conf;
748274939Smav	TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
749274939Smav	isns->i_addr = checked_strdup(addr);
750274939Smav
751274939Smav	if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
752274939Smav		log_warnx("invalid iSNS address %s", isns->i_addr);
753274939Smav		isns_delete(isns);
754274939Smav		return (1);
755274939Smav	}
756274939Smav
757274939Smav	/*
758274939Smav	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
759274939Smav	 *	those into multiple servers.
760274939Smav	 */
761274939Smav
762274939Smav	return (0);
763274939Smav}
764274939Smav
765274939Smavvoid
766274939Smavisns_delete(struct isns *isns)
767274939Smav{
768274939Smav
769274939Smav	TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
770274939Smav	free(isns->i_addr);
771274939Smav	if (isns->i_ai != NULL)
772274939Smav		freeaddrinfo(isns->i_ai);
773274939Smav	free(isns);
774274939Smav}
775274939Smav
776274939Smavstatic int
777274939Smavisns_do_connect(struct isns *isns)
778274939Smav{
779274939Smav	int s;
780274939Smav
781274939Smav	s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
782274939Smav	    isns->i_ai->ai_protocol);
783274939Smav	if (s < 0) {
784274939Smav		log_warn("socket(2) failed for %s", isns->i_addr);
785274939Smav		return (-1);
786274939Smav	}
787274939Smav	if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
788274939Smav		log_warn("connect(2) failed for %s", isns->i_addr);
789274939Smav		close(s);
790274939Smav		return (-1);
791274939Smav	}
792274939Smav	return(s);
793274939Smav}
794274939Smav
795274939Smavstatic int
796274939Smavisns_do_register(struct isns *isns, int s, const char *hostname)
797274939Smav{
798274939Smav	struct conf *conf = isns->i_conf;
799274939Smav	struct target *target;
800274939Smav	struct portal *portal;
801274939Smav	struct portal_group *pg;
802279006Smav	struct port *port;
803274939Smav	struct isns_req *req;
804274939Smav	int res = 0;
805274939Smav	uint32_t error;
806274939Smav
807274939Smav	req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
808274939Smav	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
809274939Smav	isns_req_add_delim(req);
810274939Smav	isns_req_add_str(req, 1, hostname);
811274939Smav	isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
812274939Smav	isns_req_add_32(req, 6, conf->conf_isns_period);
813274939Smav	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
814274939Smav		if (pg->pg_unassigned)
815274939Smav			continue;
816274939Smav		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
817274939Smav			isns_req_add_addr(req, 16, portal->p_ai);
818274939Smav			isns_req_add_port(req, 17, portal->p_ai);
819274939Smav		}
820274939Smav	}
821274939Smav	TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
822274939Smav		isns_req_add_str(req, 32, target->t_name);
823274939Smav		isns_req_add_32(req, 33, 1); /* 1 -- Target*/
824274939Smav		if (target->t_alias != NULL)
825274939Smav			isns_req_add_str(req, 34, target->t_alias);
826279006Smav		TAILQ_FOREACH(port, &target->t_ports, p_ts) {
827279006Smav			if ((pg = port->p_portal_group) == NULL)
828279006Smav				continue;
829279006Smav			isns_req_add_32(req, 51, pg->pg_tag);
830279006Smav			TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
831279006Smav				isns_req_add_addr(req, 49, portal->p_ai);
832279006Smav				isns_req_add_port(req, 50, portal->p_ai);
833279006Smav			}
834274939Smav		}
835274939Smav	}
836274939Smav	res = isns_req_send(s, req);
837274939Smav	if (res < 0) {
838274939Smav		log_warn("send(2) failed for %s", isns->i_addr);
839274939Smav		goto quit;
840274939Smav	}
841274939Smav	res = isns_req_receive(s, req);
842274939Smav	if (res < 0) {
843274939Smav		log_warn("receive(2) failed for %s", isns->i_addr);
844274939Smav		goto quit;
845274939Smav	}
846274939Smav	error = isns_req_get_status(req);
847274939Smav	if (error != 0) {
848274939Smav		log_warnx("iSNS register error %d for %s", error, isns->i_addr);
849274939Smav		res = -1;
850274939Smav	}
851274939Smavquit:
852274939Smav	isns_req_free(req);
853274939Smav	return (res);
854274939Smav}
855274939Smav
856274939Smavstatic int
857274939Smavisns_do_check(struct isns *isns, int s, const char *hostname)
858274939Smav{
859274939Smav	struct conf *conf = isns->i_conf;
860274939Smav	struct isns_req *req;
861274939Smav	int res = 0;
862274939Smav	uint32_t error;
863274939Smav
864274939Smav	req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
865274939Smav	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
866274939Smav	isns_req_add_str(req, 1, hostname);
867274939Smav	isns_req_add_delim(req);
868274939Smav	isns_req_add(req, 2, 0, NULL);
869274939Smav	res = isns_req_send(s, req);
870274939Smav	if (res < 0) {
871274939Smav		log_warn("send(2) failed for %s", isns->i_addr);
872274939Smav		goto quit;
873274939Smav	}
874274939Smav	res = isns_req_receive(s, req);
875274939Smav	if (res < 0) {
876274939Smav		log_warn("receive(2) failed for %s", isns->i_addr);
877274939Smav		goto quit;
878274939Smav	}
879274939Smav	error = isns_req_get_status(req);
880274939Smav	if (error != 0) {
881274939Smav		log_warnx("iSNS check error %d for %s", error, isns->i_addr);
882274939Smav		res = -1;
883274939Smav	}
884274939Smavquit:
885274939Smav	isns_req_free(req);
886274939Smav	return (res);
887274939Smav}
888274939Smav
889274939Smavstatic int
890274939Smavisns_do_deregister(struct isns *isns, int s, const char *hostname)
891274939Smav{
892274939Smav	struct conf *conf = isns->i_conf;
893274939Smav	struct isns_req *req;
894274939Smav	int res = 0;
895274939Smav	uint32_t error;
896274939Smav
897274939Smav	req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
898274939Smav	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
899274939Smav	isns_req_add_delim(req);
900274939Smav	isns_req_add_str(req, 1, hostname);
901274939Smav	res = isns_req_send(s, req);
902274939Smav	if (res < 0) {
903274939Smav		log_warn("send(2) failed for %s", isns->i_addr);
904274939Smav		goto quit;
905274939Smav	}
906274939Smav	res = isns_req_receive(s, req);
907274939Smav	if (res < 0) {
908274939Smav		log_warn("receive(2) failed for %s", isns->i_addr);
909274939Smav		goto quit;
910274939Smav	}
911274939Smav	error = isns_req_get_status(req);
912274939Smav	if (error != 0) {
913274939Smav		log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
914274939Smav		res = -1;
915274939Smav	}
916274939Smavquit:
917274939Smav	isns_req_free(req);
918274939Smav	return (res);
919274939Smav}
920274939Smav
921274939Smavvoid
922274939Smavisns_register(struct isns *isns, struct isns *oldisns)
923274939Smav{
924274939Smav	struct conf *conf = isns->i_conf;
925275496Smav	int s;
926274939Smav	char hostname[256];
927274939Smav
928274939Smav	if (TAILQ_EMPTY(&conf->conf_targets) ||
929274939Smav	    TAILQ_EMPTY(&conf->conf_portal_groups))
930274939Smav		return;
931274939Smav	set_timeout(conf->conf_isns_timeout, false);
932274939Smav	s = isns_do_connect(isns);
933274939Smav	if (s < 0) {
934274939Smav		set_timeout(0, false);
935274939Smav		return;
936274939Smav	}
937274939Smav	gethostname(hostname, sizeof(hostname));
938274939Smav
939274939Smav	if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
940274939Smav		oldisns = isns;
941275496Smav	isns_do_deregister(oldisns, s, hostname);
942275496Smav	isns_do_register(isns, s, hostname);
943274939Smav	close(s);
944274939Smav	set_timeout(0, false);
945274939Smav}
946274939Smav
947274939Smavvoid
948274939Smavisns_check(struct isns *isns)
949274939Smav{
950274939Smav	struct conf *conf = isns->i_conf;
951274939Smav	int s, res;
952274939Smav	char hostname[256];
953274939Smav
954274939Smav	if (TAILQ_EMPTY(&conf->conf_targets) ||
955274939Smav	    TAILQ_EMPTY(&conf->conf_portal_groups))
956274939Smav		return;
957274939Smav	set_timeout(conf->conf_isns_timeout, false);
958274939Smav	s = isns_do_connect(isns);
959274939Smav	if (s < 0) {
960274939Smav		set_timeout(0, false);
961274939Smav		return;
962274939Smav	}
963274939Smav	gethostname(hostname, sizeof(hostname));
964274939Smav
965274939Smav	res = isns_do_check(isns, s, hostname);
966274939Smav	if (res < 0) {
967275496Smav		isns_do_deregister(isns, s, hostname);
968275496Smav		isns_do_register(isns, s, hostname);
969274939Smav	}
970274939Smav	close(s);
971274939Smav	set_timeout(0, false);
972274939Smav}
973274939Smav
974274939Smavvoid
975274939Smavisns_deregister(struct isns *isns)
976274939Smav{
977274939Smav	struct conf *conf = isns->i_conf;
978275496Smav	int s;
979274939Smav	char hostname[256];
980274939Smav
981274939Smav	if (TAILQ_EMPTY(&conf->conf_targets) ||
982274939Smav	    TAILQ_EMPTY(&conf->conf_portal_groups))
983274939Smav		return;
984274939Smav	set_timeout(conf->conf_isns_timeout, false);
985274939Smav	s = isns_do_connect(isns);
986274939Smav	if (s < 0)
987274939Smav		return;
988274939Smav	gethostname(hostname, sizeof(hostname));
989274939Smav
990275496Smav	isns_do_deregister(isns, s, hostname);
991274939Smav	close(s);
992274939Smav	set_timeout(0, false);
993274939Smav}
994274939Smav
995275244Straszint
996275245Straszportal_group_set_filter(struct portal_group *pg, const char *str)
997275244Strasz{
998275245Strasz	int filter;
999275244Strasz
1000275244Strasz	if (strcmp(str, "none") == 0) {
1001275244Strasz		filter = PG_FILTER_NONE;
1002275244Strasz	} else if (strcmp(str, "portal") == 0) {
1003275244Strasz		filter = PG_FILTER_PORTAL;
1004275244Strasz	} else if (strcmp(str, "portal-name") == 0) {
1005275244Strasz		filter = PG_FILTER_PORTAL_NAME;
1006275244Strasz	} else if (strcmp(str, "portal-name-auth") == 0) {
1007275244Strasz		filter = PG_FILTER_PORTAL_NAME_AUTH;
1008275244Strasz	} else {
1009275244Strasz		log_warnx("invalid discovery-filter \"%s\" for portal-group "
1010275244Strasz		    "\"%s\"; valid values are \"none\", \"portal\", "
1011275244Strasz		    "\"portal-name\", and \"portal-name-auth\"",
1012275244Strasz		    str, pg->pg_name);
1013275244Strasz		return (1);
1014275244Strasz	}
1015275244Strasz
1016275245Strasz	if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1017275245Strasz	    pg->pg_discovery_filter != filter) {
1018275244Strasz		log_warnx("cannot set discovery-filter to \"%s\" for "
1019275244Strasz		    "portal-group \"%s\"; already has a different "
1020275244Strasz		    "value", str, pg->pg_name);
1021275244Strasz		return (1);
1022275244Strasz	}
1023275244Strasz
1024275245Strasz	pg->pg_discovery_filter = filter;
1025275245Strasz
1026275245Strasz	return (0);
1027275244Strasz}
1028275244Strasz
1029275642Straszint
1030275642Straszportal_group_set_redirection(struct portal_group *pg, const char *addr)
1031275642Strasz{
1032275642Strasz
1033275642Strasz	if (pg->pg_redirection != NULL) {
1034275642Strasz		log_warnx("cannot set redirection to \"%s\" for "
1035275642Strasz		    "portal-group \"%s\"; already defined",
1036275642Strasz		    addr, pg->pg_name);
1037275642Strasz		return (1);
1038275642Strasz	}
1039275642Strasz
1040275642Strasz	pg->pg_redirection = checked_strdup(addr);
1041275642Strasz
1042275642Strasz	return (0);
1043275642Strasz}
1044275642Strasz
1045255570Straszstatic bool
1046255570Straszvalid_hex(const char ch)
1047255570Strasz{
1048255570Strasz	switch (ch) {
1049255570Strasz	case '0':
1050255570Strasz	case '1':
1051255570Strasz	case '2':
1052255570Strasz	case '3':
1053255570Strasz	case '4':
1054255570Strasz	case '5':
1055255570Strasz	case '6':
1056255570Strasz	case '7':
1057255570Strasz	case '8':
1058255570Strasz	case '9':
1059255570Strasz	case 'a':
1060255570Strasz	case 'A':
1061255570Strasz	case 'b':
1062255570Strasz	case 'B':
1063255570Strasz	case 'c':
1064255570Strasz	case 'C':
1065255570Strasz	case 'd':
1066255570Strasz	case 'D':
1067255570Strasz	case 'e':
1068255570Strasz	case 'E':
1069255570Strasz	case 'f':
1070255570Strasz	case 'F':
1071255570Strasz		return (true);
1072255570Strasz	default:
1073255570Strasz		return (false);
1074255570Strasz	}
1075255570Strasz}
1076255570Strasz
1077255570Straszbool
1078255570Straszvalid_iscsi_name(const char *name)
1079255570Strasz{
1080255570Strasz	int i;
1081255570Strasz
1082255570Strasz	if (strlen(name) >= MAX_NAME_LEN) {
1083255570Strasz		log_warnx("overlong name for target \"%s\"; max length allowed "
1084255570Strasz		    "by iSCSI specification is %d characters",
1085255570Strasz		    name, MAX_NAME_LEN);
1086255570Strasz		return (false);
1087255570Strasz	}
1088255570Strasz
1089255570Strasz	/*
1090255570Strasz	 * In the cases below, we don't return an error, just in case the admin
1091255570Strasz	 * was right, and we're wrong.
1092255570Strasz	 */
1093255570Strasz	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1094255570Strasz		for (i = strlen("iqn."); name[i] != '\0'; i++) {
1095255570Strasz			/*
1096255570Strasz			 * XXX: We should verify UTF-8 normalisation, as defined
1097274870Strasz			 *      by 3.2.6.2: iSCSI Name Encoding.
1098255570Strasz			 */
1099255570Strasz			if (isalnum(name[i]))
1100255570Strasz				continue;
1101255570Strasz			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1102255570Strasz				continue;
1103255570Strasz			log_warnx("invalid character \"%c\" in target name "
1104255570Strasz			    "\"%s\"; allowed characters are letters, digits, "
1105255570Strasz			    "'-', '.', and ':'", name[i], name);
1106255570Strasz			break;
1107255570Strasz		}
1108255570Strasz		/*
1109255570Strasz		 * XXX: Check more stuff: valid date and a valid reversed domain.
1110255570Strasz		 */
1111255570Strasz	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1112255570Strasz		if (strlen(name) != strlen("eui.") + 16)
1113255570Strasz			log_warnx("invalid target name \"%s\"; the \"eui.\" "
1114255570Strasz			    "should be followed by exactly 16 hexadecimal "
1115255570Strasz			    "digits", name);
1116255570Strasz		for (i = strlen("eui."); name[i] != '\0'; i++) {
1117255570Strasz			if (!valid_hex(name[i])) {
1118255570Strasz				log_warnx("invalid character \"%c\" in target "
1119255570Strasz				    "name \"%s\"; allowed characters are 1-9 "
1120255570Strasz				    "and A-F", name[i], name);
1121255570Strasz				break;
1122255570Strasz			}
1123255570Strasz		}
1124255570Strasz	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1125255570Strasz		if (strlen(name) > strlen("naa.") + 32)
1126255570Strasz			log_warnx("invalid target name \"%s\"; the \"naa.\" "
1127255570Strasz			    "should be followed by at most 32 hexadecimal "
1128255570Strasz			    "digits", name);
1129255570Strasz		for (i = strlen("naa."); name[i] != '\0'; i++) {
1130255570Strasz			if (!valid_hex(name[i])) {
1131255570Strasz				log_warnx("invalid character \"%c\" in target "
1132255570Strasz				    "name \"%s\"; allowed characters are 1-9 "
1133255570Strasz				    "and A-F", name[i], name);
1134255570Strasz				break;
1135255570Strasz			}
1136255570Strasz		}
1137255570Strasz	} else {
1138255570Strasz		log_warnx("invalid target name \"%s\"; should start with "
1139288209Sjpaetzel		    "either \"iqn.\", \"eui.\", or \"naa.\"",
1140255570Strasz		    name);
1141255570Strasz	}
1142255570Strasz	return (true);
1143255570Strasz}
1144255570Strasz
1145279055Smavstruct pport *
1146279055Smavpport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1147279055Smav{
1148279055Smav	struct pport *pp;
1149279055Smav
1150279055Smav	pp = calloc(1, sizeof(*pp));
1151279055Smav	if (pp == NULL)
1152279055Smav		log_err(1, "calloc");
1153279055Smav	pp->pp_conf = conf;
1154279055Smav	pp->pp_name = checked_strdup(name);
1155279055Smav	pp->pp_ctl_port = ctl_port;
1156279055Smav	TAILQ_INIT(&pp->pp_ports);
1157279055Smav	TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1158279055Smav	return (pp);
1159279055Smav}
1160279055Smav
1161279055Smavstruct pport *
1162279055Smavpport_find(const struct conf *conf, const char *name)
1163279055Smav{
1164279055Smav	struct pport *pp;
1165279055Smav
1166279055Smav	TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1167279055Smav		if (strcasecmp(pp->pp_name, name) == 0)
1168279055Smav			return (pp);
1169279055Smav	}
1170279055Smav	return (NULL);
1171279055Smav}
1172279055Smav
1173279055Smavstruct pport *
1174279055Smavpport_copy(struct pport *pp, struct conf *conf)
1175279055Smav{
1176279055Smav	struct pport *ppnew;
1177279055Smav
1178279055Smav	ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1179279055Smav	return (ppnew);
1180279055Smav}
1181279055Smav
1182279055Smavvoid
1183279055Smavpport_delete(struct pport *pp)
1184279055Smav{
1185279055Smav	struct port *port, *tport;
1186279055Smav
1187279055Smav	TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1188279055Smav		port_delete(port);
1189279055Smav	TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1190279055Smav	free(pp->pp_name);
1191279055Smav	free(pp);
1192279055Smav}
1193279055Smav
1194279006Smavstruct port *
1195279006Smavport_new(struct conf *conf, struct target *target, struct portal_group *pg)
1196279006Smav{
1197279006Smav	struct port *port;
1198279055Smav	char *name;
1199279056Smav	int ret;
1200279006Smav
1201279056Smav	ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1202279056Smav	if (ret <= 0)
1203279056Smav		log_err(1, "asprintf");
1204279055Smav	if (port_find(conf, name) != NULL) {
1205279055Smav		log_warnx("duplicate port \"%s\"", name);
1206279055Smav		free(name);
1207279055Smav		return (NULL);
1208279055Smav	}
1209279006Smav	port = calloc(1, sizeof(*port));
1210279006Smav	if (port == NULL)
1211279006Smav		log_err(1, "calloc");
1212279006Smav	port->p_conf = conf;
1213279055Smav	port->p_name = name;
1214279006Smav	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1215279006Smav	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1216279006Smav	port->p_target = target;
1217279006Smav	TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1218279006Smav	port->p_portal_group = pg;
1219288729Smav	port->p_foreign = pg->pg_foreign;
1220279006Smav	return (port);
1221279006Smav}
1222279006Smav
1223279006Smavstruct port *
1224279055Smavport_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1225279055Smav{
1226279055Smav	struct port *port;
1227279055Smav	char *name;
1228279056Smav	int ret;
1229279055Smav
1230279056Smav	ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1231279056Smav	if (ret <= 0)
1232279056Smav		log_err(1, "asprintf");
1233279055Smav	if (port_find(conf, name) != NULL) {
1234279055Smav		log_warnx("duplicate port \"%s\"", name);
1235279055Smav		free(name);
1236279055Smav		return (NULL);
1237279055Smav	}
1238279055Smav	port = calloc(1, sizeof(*port));
1239279055Smav	if (port == NULL)
1240279055Smav		log_err(1, "calloc");
1241279055Smav	port->p_conf = conf;
1242279055Smav	port->p_name = name;
1243279055Smav	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1244279055Smav	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1245279055Smav	port->p_target = target;
1246279055Smav	TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1247279055Smav	port->p_pport = pp;
1248279055Smav	return (port);
1249279055Smav}
1250279055Smav
1251279055Smavstruct port *
1252279006Smavport_find(const struct conf *conf, const char *name)
1253279006Smav{
1254279006Smav	struct port *port;
1255279006Smav
1256279006Smav	TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1257279006Smav		if (strcasecmp(port->p_name, name) == 0)
1258279006Smav			return (port);
1259279006Smav	}
1260279006Smav
1261279006Smav	return (NULL);
1262279006Smav}
1263279006Smav
1264279006Smavstruct port *
1265279006Smavport_find_in_pg(const struct portal_group *pg, const char *target)
1266279006Smav{
1267279006Smav	struct port *port;
1268279006Smav
1269279006Smav	TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1270279006Smav		if (strcasecmp(port->p_target->t_name, target) == 0)
1271279006Smav			return (port);
1272279006Smav	}
1273279006Smav
1274279006Smav	return (NULL);
1275279006Smav}
1276279006Smav
1277279006Smavvoid
1278279006Smavport_delete(struct port *port)
1279279006Smav{
1280279006Smav
1281279006Smav	if (port->p_portal_group)
1282279006Smav		TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1283279055Smav	if (port->p_pport)
1284279055Smav		TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1285279006Smav	if (port->p_target)
1286279006Smav		TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1287279006Smav	TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1288279006Smav	free(port->p_name);
1289279006Smav	free(port);
1290279006Smav}
1291279006Smav
1292255570Straszstruct target *
1293263723Strasztarget_new(struct conf *conf, const char *name)
1294255570Strasz{
1295255570Strasz	struct target *targ;
1296255570Strasz	int i, len;
1297255570Strasz
1298263723Strasz	targ = target_find(conf, name);
1299255570Strasz	if (targ != NULL) {
1300263723Strasz		log_warnx("duplicated target \"%s\"", name);
1301255570Strasz		return (NULL);
1302255570Strasz	}
1303263723Strasz	if (valid_iscsi_name(name) == false) {
1304263723Strasz		log_warnx("target name \"%s\" is invalid", name);
1305255570Strasz		return (NULL);
1306255570Strasz	}
1307255570Strasz	targ = calloc(1, sizeof(*targ));
1308255570Strasz	if (targ == NULL)
1309255570Strasz		log_err(1, "calloc");
1310263723Strasz	targ->t_name = checked_strdup(name);
1311255570Strasz
1312255570Strasz	/*
1313255570Strasz	 * RFC 3722 requires us to normalize the name to lowercase.
1314255570Strasz	 */
1315263723Strasz	len = strlen(name);
1316255570Strasz	for (i = 0; i < len; i++)
1317263723Strasz		targ->t_name[i] = tolower(targ->t_name[i]);
1318255570Strasz
1319255570Strasz	targ->t_conf = conf;
1320279006Smav	TAILQ_INIT(&targ->t_ports);
1321255570Strasz	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1322255570Strasz
1323255570Strasz	return (targ);
1324255570Strasz}
1325255570Strasz
1326255570Straszvoid
1327255570Strasztarget_delete(struct target *targ)
1328255570Strasz{
1329279006Smav	struct port *port, *tport;
1330255570Strasz
1331279006Smav	TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1332279006Smav		port_delete(port);
1333255570Strasz	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1334255570Strasz
1335263723Strasz	free(targ->t_name);
1336275642Strasz	free(targ->t_redirection);
1337255570Strasz	free(targ);
1338255570Strasz}
1339255570Strasz
1340255570Straszstruct target *
1341263723Strasztarget_find(struct conf *conf, const char *name)
1342255570Strasz{
1343255570Strasz	struct target *targ;
1344255570Strasz
1345255570Strasz	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1346263723Strasz		if (strcasecmp(targ->t_name, name) == 0)
1347255570Strasz			return (targ);
1348255570Strasz	}
1349255570Strasz
1350255570Strasz	return (NULL);
1351255570Strasz}
1352255570Strasz
1353275642Straszint
1354275642Strasztarget_set_redirection(struct target *target, const char *addr)
1355275642Strasz{
1356275642Strasz
1357275642Strasz	if (target->t_redirection != NULL) {
1358275642Strasz		log_warnx("cannot set redirection to \"%s\" for "
1359275642Strasz		    "target \"%s\"; already defined",
1360275642Strasz		    addr, target->t_name);
1361275642Strasz		return (1);
1362275642Strasz	}
1363275642Strasz
1364275642Strasz	target->t_redirection = checked_strdup(addr);
1365275642Strasz
1366275642Strasz	return (0);
1367275642Strasz}
1368275642Strasz
1369255570Straszstruct lun *
1370279002Smavlun_new(struct conf *conf, const char *name)
1371255570Strasz{
1372255570Strasz	struct lun *lun;
1373255570Strasz
1374279002Smav	lun = lun_find(conf, name);
1375255570Strasz	if (lun != NULL) {
1376279002Smav		log_warnx("duplicated lun \"%s\"", name);
1377255570Strasz		return (NULL);
1378255570Strasz	}
1379255570Strasz
1380255570Strasz	lun = calloc(1, sizeof(*lun));
1381255570Strasz	if (lun == NULL)
1382255570Strasz		log_err(1, "calloc");
1383279002Smav	lun->l_conf = conf;
1384279002Smav	lun->l_name = checked_strdup(name);
1385255570Strasz	TAILQ_INIT(&lun->l_options);
1386279002Smav	TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1387288760Smav	lun->l_ctl_lun = -1;
1388255570Strasz
1389255570Strasz	return (lun);
1390255570Strasz}
1391255570Strasz
1392255570Straszvoid
1393255570Straszlun_delete(struct lun *lun)
1394255570Strasz{
1395279002Smav	struct target *targ;
1396291387Smav	struct option *o, *tmp;
1397279002Smav	int i;
1398255570Strasz
1399279002Smav	TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1400279002Smav		for (i = 0; i < MAX_LUNS; i++) {
1401279002Smav			if (targ->t_luns[i] == lun)
1402279002Smav				targ->t_luns[i] = NULL;
1403279002Smav		}
1404279002Smav	}
1405279002Smav	TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1406255570Strasz
1407291387Smav	TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1408291387Smav		option_delete(&lun->l_options, o);
1409279002Smav	free(lun->l_name);
1410255570Strasz	free(lun->l_backend);
1411255570Strasz	free(lun->l_device_id);
1412255570Strasz	free(lun->l_path);
1413279002Smav	free(lun->l_scsiname);
1414255570Strasz	free(lun->l_serial);
1415255570Strasz	free(lun);
1416255570Strasz}
1417255570Strasz
1418255570Straszstruct lun *
1419279002Smavlun_find(const struct conf *conf, const char *name)
1420255570Strasz{
1421255570Strasz	struct lun *lun;
1422255570Strasz
1423279002Smav	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1424279002Smav		if (strcmp(lun->l_name, name) == 0)
1425255570Strasz			return (lun);
1426255570Strasz	}
1427255570Strasz
1428255570Strasz	return (NULL);
1429255570Strasz}
1430255570Strasz
1431255570Straszvoid
1432255570Straszlun_set_backend(struct lun *lun, const char *value)
1433255570Strasz{
1434255570Strasz	free(lun->l_backend);
1435255570Strasz	lun->l_backend = checked_strdup(value);
1436255570Strasz}
1437255570Strasz
1438255570Straszvoid
1439255570Straszlun_set_blocksize(struct lun *lun, size_t value)
1440255570Strasz{
1441255570Strasz
1442255570Strasz	lun->l_blocksize = value;
1443255570Strasz}
1444255570Strasz
1445255570Straszvoid
1446288810Smavlun_set_device_type(struct lun *lun, uint8_t value)
1447288810Smav{
1448288810Smav
1449288810Smav	lun->l_device_type = value;
1450288810Smav}
1451288810Smav
1452288810Smavvoid
1453255570Straszlun_set_device_id(struct lun *lun, const char *value)
1454255570Strasz{
1455255570Strasz	free(lun->l_device_id);
1456255570Strasz	lun->l_device_id = checked_strdup(value);
1457255570Strasz}
1458255570Strasz
1459255570Straszvoid
1460255570Straszlun_set_path(struct lun *lun, const char *value)
1461255570Strasz{
1462255570Strasz	free(lun->l_path);
1463255570Strasz	lun->l_path = checked_strdup(value);
1464255570Strasz}
1465255570Strasz
1466255570Straszvoid
1467279002Smavlun_set_scsiname(struct lun *lun, const char *value)
1468279002Smav{
1469279002Smav	free(lun->l_scsiname);
1470279002Smav	lun->l_scsiname = checked_strdup(value);
1471279002Smav}
1472279002Smav
1473279002Smavvoid
1474255570Straszlun_set_serial(struct lun *lun, const char *value)
1475255570Strasz{
1476255570Strasz	free(lun->l_serial);
1477255570Strasz	lun->l_serial = checked_strdup(value);
1478255570Strasz}
1479255570Strasz
1480255570Straszvoid
1481255570Straszlun_set_size(struct lun *lun, size_t value)
1482255570Strasz{
1483255570Strasz
1484255570Strasz	lun->l_size = value;
1485255570Strasz}
1486255570Strasz
1487255570Straszvoid
1488255570Straszlun_set_ctl_lun(struct lun *lun, uint32_t value)
1489255570Strasz{
1490255570Strasz
1491255570Strasz	lun->l_ctl_lun = value;
1492255570Strasz}
1493255570Strasz
1494291387Smavstruct option *
1495291387Smavoption_new(struct options *options, const char *name, const char *value)
1496255570Strasz{
1497291387Smav	struct option *o;
1498255570Strasz
1499291387Smav	o = option_find(options, name);
1500291387Smav	if (o != NULL) {
1501291387Smav		log_warnx("duplicated option \"%s\"", name);
1502255570Strasz		return (NULL);
1503255570Strasz	}
1504255570Strasz
1505291387Smav	o = calloc(1, sizeof(*o));
1506291387Smav	if (o == NULL)
1507255570Strasz		log_err(1, "calloc");
1508291387Smav	o->o_name = checked_strdup(name);
1509291387Smav	o->o_value = checked_strdup(value);
1510291387Smav	TAILQ_INSERT_TAIL(options, o, o_next);
1511255570Strasz
1512291387Smav	return (o);
1513255570Strasz}
1514255570Strasz
1515255570Straszvoid
1516291387Smavoption_delete(struct options *options, struct option *o)
1517255570Strasz{
1518255570Strasz
1519291387Smav	TAILQ_REMOVE(options, o, o_next);
1520291387Smav	free(o->o_name);
1521291387Smav	free(o->o_value);
1522291387Smav	free(o);
1523255570Strasz}
1524255570Strasz
1525291387Smavstruct option *
1526291387Smavoption_find(const struct options *options, const char *name)
1527255570Strasz{
1528291387Smav	struct option *o;
1529255570Strasz
1530291387Smav	TAILQ_FOREACH(o, options, o_next) {
1531291387Smav		if (strcmp(o->o_name, name) == 0)
1532291387Smav			return (o);
1533255570Strasz	}
1534255570Strasz
1535255570Strasz	return (NULL);
1536255570Strasz}
1537255570Strasz
1538255570Straszvoid
1539291387Smavoption_set(struct option *o, const char *value)
1540255570Strasz{
1541255570Strasz
1542291387Smav	free(o->o_value);
1543291387Smav	o->o_value = checked_strdup(value);
1544255570Strasz}
1545255570Strasz
1546255570Straszstatic struct connection *
1547270137Smavconnection_new(struct portal *portal, int fd, const char *host,
1548270137Smav    const struct sockaddr *client_sa)
1549255570Strasz{
1550255570Strasz	struct connection *conn;
1551255570Strasz
1552255570Strasz	conn = calloc(1, sizeof(*conn));
1553255570Strasz	if (conn == NULL)
1554255570Strasz		log_err(1, "calloc");
1555255570Strasz	conn->conn_portal = portal;
1556255570Strasz	conn->conn_socket = fd;
1557255570Strasz	conn->conn_initiator_addr = checked_strdup(host);
1558270137Smav	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1559255570Strasz
1560255570Strasz	/*
1561255570Strasz	 * Default values, from RFC 3720, section 12.
1562255570Strasz	 */
1563255570Strasz	conn->conn_max_data_segment_length = 8192;
1564255570Strasz	conn->conn_max_burst_length = 262144;
1565255570Strasz	conn->conn_immediate_data = true;
1566255570Strasz
1567255570Strasz	return (conn);
1568255570Strasz}
1569255570Strasz
1570255570Strasz#if 0
1571255570Straszstatic void
1572255570Straszconf_print(struct conf *conf)
1573255570Strasz{
1574255570Strasz	struct auth_group *ag;
1575255570Strasz	struct auth *auth;
1576263720Strasz	struct auth_name *auth_name;
1577263720Strasz	struct auth_portal *auth_portal;
1578255570Strasz	struct portal_group *pg;
1579255570Strasz	struct portal *portal;
1580255570Strasz	struct target *targ;
1581255570Strasz	struct lun *lun;
1582291387Smav	struct option *o;
1583255570Strasz
1584255570Strasz	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1585255570Strasz		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1586255570Strasz		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1587255570Strasz			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1588255570Strasz			    auth->a_user, auth->a_secret,
1589255570Strasz			    auth->a_mutual_user, auth->a_mutual_secret);
1590263720Strasz		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1591263720Strasz			fprintf(stderr, "\t initiator-name %s\n",
1592263720Strasz			    auth_name->an_initator_name);
1593263720Strasz		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1594263720Strasz			fprintf(stderr, "\t initiator-portal %s\n",
1595263720Strasz			    auth_portal->an_initator_portal);
1596255570Strasz		fprintf(stderr, "}\n");
1597255570Strasz	}
1598255570Strasz	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1599255570Strasz		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1600255570Strasz		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1601255570Strasz			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1602255570Strasz		fprintf(stderr, "}\n");
1603255570Strasz	}
1604279002Smav	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1605279002Smav		fprintf(stderr, "\tlun %s {\n", lun->l_name);
1606279002Smav		fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1607291387Smav		TAILQ_FOREACH(o, &lun->l_options, o_next)
1608279002Smav			fprintf(stderr, "\t\toption %s %s\n",
1609291387Smav			    lo->o_name, lo->o_value);
1610279002Smav		fprintf(stderr, "\t}\n");
1611279002Smav	}
1612255570Strasz	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1613263723Strasz		fprintf(stderr, "target %s {\n", targ->t_name);
1614255570Strasz		if (targ->t_alias != NULL)
1615255570Strasz			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1616255570Strasz		fprintf(stderr, "}\n");
1617255570Strasz	}
1618255570Strasz}
1619255570Strasz#endif
1620255570Strasz
1621263717Straszstatic int
1622263717Straszconf_verify_lun(struct lun *lun)
1623263717Strasz{
1624263717Strasz	const struct lun *lun2;
1625263717Strasz
1626263717Strasz	if (lun->l_backend == NULL)
1627263717Strasz		lun_set_backend(lun, "block");
1628263717Strasz	if (strcmp(lun->l_backend, "block") == 0) {
1629263717Strasz		if (lun->l_path == NULL) {
1630279002Smav			log_warnx("missing path for lun \"%s\"",
1631279002Smav			    lun->l_name);
1632263717Strasz			return (1);
1633263717Strasz		}
1634263717Strasz	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1635263717Strasz		if (lun->l_size == 0) {
1636279002Smav			log_warnx("missing size for ramdisk-backed lun \"%s\"",
1637279002Smav			    lun->l_name);
1638263717Strasz			return (1);
1639263717Strasz		}
1640263717Strasz		if (lun->l_path != NULL) {
1641263717Strasz			log_warnx("path must not be specified "
1642279002Smav			    "for ramdisk-backed lun \"%s\"",
1643279002Smav			    lun->l_name);
1644263717Strasz			return (1);
1645263717Strasz		}
1646263717Strasz	}
1647263717Strasz	if (lun->l_blocksize == 0) {
1648288823Smav		if (lun->l_device_type == 5)
1649288823Smav			lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1650288823Smav		else
1651288823Smav			lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1652263717Strasz	} else if (lun->l_blocksize < 0) {
1653279002Smav		log_warnx("invalid blocksize for lun \"%s\"; "
1654279002Smav		    "must be larger than 0", lun->l_name);
1655263717Strasz		return (1);
1656263717Strasz	}
1657263717Strasz	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1658279002Smav		log_warnx("invalid size for lun \"%s\"; "
1659279002Smav		    "must be multiple of blocksize", lun->l_name);
1660263717Strasz		return (1);
1661263717Strasz	}
1662279002Smav	TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1663279002Smav		if (lun == lun2)
1664279002Smav			continue;
1665279002Smav		if (lun->l_path != NULL && lun2->l_path != NULL &&
1666279002Smav		    strcmp(lun->l_path, lun2->l_path) == 0) {
1667279002Smav			log_debugx("WARNING: path \"%s\" duplicated "
1668279002Smav			    "between lun \"%s\", and "
1669279002Smav			    "lun \"%s\"", lun->l_path,
1670279002Smav			    lun->l_name, lun2->l_name);
1671263718Strasz		}
1672263718Strasz	}
1673263717Strasz
1674263717Strasz	return (0);
1675263717Strasz}
1676263717Strasz
1677255570Straszint
1678255570Straszconf_verify(struct conf *conf)
1679255570Strasz{
1680255570Strasz	struct auth_group *ag;
1681255570Strasz	struct portal_group *pg;
1682279006Smav	struct port *port;
1683255570Strasz	struct target *targ;
1684263717Strasz	struct lun *lun;
1685274871Strasz	bool found;
1686279002Smav	int error, i;
1687255570Strasz
1688255570Strasz	if (conf->conf_pidfile_path == NULL)
1689255570Strasz		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1690255570Strasz
1691279002Smav	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1692279002Smav		error = conf_verify_lun(lun);
1693279002Smav		if (error != 0)
1694279002Smav			return (error);
1695279002Smav	}
1696255570Strasz	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1697255570Strasz		if (targ->t_auth_group == NULL) {
1698263726Strasz			targ->t_auth_group = auth_group_find(conf,
1699263726Strasz			    "default");
1700263726Strasz			assert(targ->t_auth_group != NULL);
1701255570Strasz		}
1702279006Smav		if (TAILQ_EMPTY(&targ->t_ports)) {
1703279006Smav			pg = portal_group_find(conf, "default");
1704279006Smav			assert(pg != NULL);
1705279006Smav			port_new(conf, targ, pg);
1706255570Strasz		}
1707274871Strasz		found = false;
1708279002Smav		for (i = 0; i < MAX_LUNS; i++) {
1709279002Smav			if (targ->t_luns[i] != NULL)
1710279002Smav				found = true;
1711255570Strasz		}
1712275642Strasz		if (!found && targ->t_redirection == NULL) {
1713265506Strasz			log_warnx("no LUNs defined for target \"%s\"",
1714265506Strasz			    targ->t_name);
1715255570Strasz		}
1716275642Strasz		if (found && targ->t_redirection != NULL) {
1717275642Strasz			log_debugx("target \"%s\" contains luns, "
1718275642Strasz			    " but configured for redirection",
1719275642Strasz			    targ->t_name);
1720275642Strasz		}
1721255570Strasz	}
1722255570Strasz	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1723255570Strasz		assert(pg->pg_name != NULL);
1724255570Strasz		if (pg->pg_discovery_auth_group == NULL) {
1725255570Strasz			pg->pg_discovery_auth_group =
1726263728Strasz			    auth_group_find(conf, "default");
1727255570Strasz			assert(pg->pg_discovery_auth_group != NULL);
1728255570Strasz		}
1729255570Strasz
1730275244Strasz		if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1731275244Strasz			pg->pg_discovery_filter = PG_FILTER_NONE;
1732275244Strasz
1733288729Smav		if (pg->pg_redirection != NULL) {
1734288729Smav			if (!TAILQ_EMPTY(&pg->pg_ports)) {
1735275642Strasz				log_debugx("portal-group \"%s\" assigned "
1736279006Smav				    "to target, but configured "
1737275642Strasz				    "for redirection",
1738279006Smav				    pg->pg_name);
1739275642Strasz			}
1740275642Strasz			pg->pg_unassigned = false;
1741288729Smav		} else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1742288729Smav			pg->pg_unassigned = false;
1743275642Strasz		} else {
1744255570Strasz			if (strcmp(pg->pg_name, "default") != 0)
1745255570Strasz				log_warnx("portal-group \"%s\" not assigned "
1746255570Strasz				    "to any target", pg->pg_name);
1747255570Strasz			pg->pg_unassigned = true;
1748275642Strasz		}
1749255570Strasz	}
1750255570Strasz	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1751255570Strasz		if (ag->ag_name == NULL)
1752255570Strasz			assert(ag->ag_target != NULL);
1753255570Strasz		else
1754255570Strasz			assert(ag->ag_target == NULL);
1755255570Strasz
1756274871Strasz		found = false;
1757255570Strasz		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1758274871Strasz			if (targ->t_auth_group == ag) {
1759274871Strasz				found = true;
1760255570Strasz				break;
1761274871Strasz			}
1762255570Strasz		}
1763279006Smav		TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1764279006Smav			if (port->p_auth_group == ag) {
1765279006Smav				found = true;
1766279006Smav				break;
1767279006Smav			}
1768279006Smav		}
1769274871Strasz		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1770274871Strasz			if (pg->pg_discovery_auth_group == ag) {
1771274871Strasz				found = true;
1772274871Strasz				break;
1773274871Strasz			}
1774274871Strasz		}
1775274871Strasz		if (!found && ag->ag_name != NULL &&
1776263728Strasz		    strcmp(ag->ag_name, "default") != 0 &&
1777255570Strasz		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1778255570Strasz		    strcmp(ag->ag_name, "no-access") != 0) {
1779255570Strasz			log_warnx("auth-group \"%s\" not assigned "
1780255570Strasz			    "to any target", ag->ag_name);
1781255570Strasz		}
1782255570Strasz	}
1783255570Strasz
1784255570Strasz	return (0);
1785255570Strasz}
1786255570Strasz
1787255570Straszstatic int
1788255570Straszconf_apply(struct conf *oldconf, struct conf *newconf)
1789255570Strasz{
1790255570Strasz	struct lun *oldlun, *newlun, *tmplun;
1791255570Strasz	struct portal_group *oldpg, *newpg;
1792255570Strasz	struct portal *oldp, *newp;
1793279006Smav	struct port *oldport, *newport, *tmpport;
1794274939Smav	struct isns *oldns, *newns;
1795255570Strasz	pid_t otherpid;
1796279001Smav	int changed, cumulated_error = 0, error, sockbuf;
1797255570Strasz	int one = 1;
1798255570Strasz
1799255570Strasz	if (oldconf->conf_debug != newconf->conf_debug) {
1800255570Strasz		log_debugx("changing debug level to %d", newconf->conf_debug);
1801255570Strasz		log_init(newconf->conf_debug);
1802255570Strasz	}
1803255570Strasz
1804255570Strasz	if (oldconf->conf_pidfh != NULL) {
1805255570Strasz		assert(oldconf->conf_pidfile_path != NULL);
1806255570Strasz		if (newconf->conf_pidfile_path != NULL &&
1807255570Strasz		    strcmp(oldconf->conf_pidfile_path,
1808255570Strasz		    newconf->conf_pidfile_path) == 0) {
1809255570Strasz			newconf->conf_pidfh = oldconf->conf_pidfh;
1810255570Strasz			oldconf->conf_pidfh = NULL;
1811255570Strasz		} else {
1812255570Strasz			log_debugx("removing pidfile %s",
1813255570Strasz			    oldconf->conf_pidfile_path);
1814255570Strasz			pidfile_remove(oldconf->conf_pidfh);
1815255570Strasz			oldconf->conf_pidfh = NULL;
1816255570Strasz		}
1817255570Strasz	}
1818255570Strasz
1819255570Strasz	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1820255570Strasz		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1821255570Strasz		newconf->conf_pidfh =
1822255570Strasz		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1823255570Strasz		if (newconf->conf_pidfh == NULL) {
1824255570Strasz			if (errno == EEXIST)
1825255570Strasz				log_errx(1, "daemon already running, pid: %jd.",
1826255570Strasz				    (intmax_t)otherpid);
1827255570Strasz			log_err(1, "cannot open or create pidfile \"%s\"",
1828255570Strasz			    newconf->conf_pidfile_path);
1829255570Strasz		}
1830255570Strasz	}
1831255570Strasz
1832279003Smav	/*
1833279003Smav	 * Go through the new portal groups, assigning tags or preserving old.
1834279003Smav	 */
1835279003Smav	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1836288729Smav		if (newpg->pg_tag != 0)
1837288729Smav			continue;
1838279003Smav		oldpg = portal_group_find(oldconf, newpg->pg_name);
1839279003Smav		if (oldpg != NULL)
1840279003Smav			newpg->pg_tag = oldpg->pg_tag;
1841279003Smav		else
1842279003Smav			newpg->pg_tag = ++last_portal_group_tag;
1843279003Smav	}
1844279003Smav
1845274939Smav	/* Deregister on removed iSNS servers. */
1846274939Smav	TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1847274939Smav		TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1848274939Smav			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1849274939Smav				break;
1850274939Smav		}
1851274939Smav		if (newns == NULL)
1852274939Smav			isns_deregister(oldns);
1853274939Smav	}
1854274939Smav
1855265518Strasz	/*
1856265518Strasz	 * XXX: If target or lun removal fails, we should somehow "move"
1857274870Strasz	 *      the old lun or target into newconf, so that subsequent
1858274870Strasz	 *      conf_apply() would try to remove them again.  That would
1859274870Strasz	 *      be somewhat hairy, though, and lun deletion failures don't
1860274870Strasz	 *      really happen, so leave it as it is for now.
1861265518Strasz	 */
1862279002Smav	/*
1863279006Smav	 * First, remove any ports present in the old configuration
1864279002Smav	 * and missing in the new one.
1865279002Smav	 */
1866279006Smav	TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1867288729Smav		if (oldport->p_foreign)
1868288729Smav			continue;
1869279006Smav		newport = port_find(newconf, oldport->p_name);
1870288729Smav		if (newport != NULL && !newport->p_foreign)
1871279002Smav			continue;
1872279055Smav		log_debugx("removing port \"%s\"", oldport->p_name);
1873279006Smav		error = kernel_port_remove(oldport);
1874279002Smav		if (error != 0) {
1875279006Smav			log_warnx("failed to remove port %s",
1876279006Smav			    oldport->p_name);
1877279002Smav			/*
1878279002Smav			 * XXX: Uncomment after fixing the root cause.
1879279002Smav			 *
1880279002Smav			 * cumulated_error++;
1881279002Smav			 */
1882279002Smav		}
1883279002Smav	}
1884279002Smav
1885279002Smav	/*
1886279002Smav	 * Second, remove any LUNs present in the old configuration
1887279002Smav	 * and missing in the new one.
1888279002Smav	 */
1889279002Smav	TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1890279002Smav		newlun = lun_find(newconf, oldlun->l_name);
1891279002Smav		if (newlun == NULL) {
1892279002Smav			log_debugx("lun \"%s\", CTL lun %d "
1893279002Smav			    "not found in new configuration; "
1894279002Smav			    "removing", oldlun->l_name, oldlun->l_ctl_lun);
1895279002Smav			error = kernel_lun_remove(oldlun);
1896279000Smav			if (error != 0) {
1897279002Smav				log_warnx("failed to remove lun \"%s\", "
1898279002Smav				    "CTL lun %d",
1899279002Smav				    oldlun->l_name, oldlun->l_ctl_lun);
1900279002Smav				cumulated_error++;
1901279000Smav			}
1902255570Strasz			continue;
1903255570Strasz		}
1904255570Strasz
1905255570Strasz		/*
1906279002Smav		 * Also remove the LUNs changed by more than size.
1907255570Strasz		 */
1908279002Smav		changed = 0;
1909279002Smav		assert(oldlun->l_backend != NULL);
1910279002Smav		assert(newlun->l_backend != NULL);
1911279002Smav		if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1912279002Smav			log_debugx("backend for lun \"%s\", "
1913279002Smav			    "CTL lun %d changed; removing",
1914279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1915279002Smav			changed = 1;
1916279002Smav		}
1917279002Smav		if (oldlun->l_blocksize != newlun->l_blocksize) {
1918279002Smav			log_debugx("blocksize for lun \"%s\", "
1919279002Smav			    "CTL lun %d changed; removing",
1920279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1921279002Smav			changed = 1;
1922279002Smav		}
1923279002Smav		if (newlun->l_device_id != NULL &&
1924279002Smav		    (oldlun->l_device_id == NULL ||
1925279002Smav		     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1926279002Smav		     0)) {
1927279002Smav			log_debugx("device-id for lun \"%s\", "
1928279002Smav			    "CTL lun %d changed; removing",
1929279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1930279002Smav			changed = 1;
1931279002Smav		}
1932279002Smav		if (newlun->l_path != NULL &&
1933279002Smav		    (oldlun->l_path == NULL ||
1934279002Smav		     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1935279002Smav			log_debugx("path for lun \"%s\", "
1936279002Smav			    "CTL lun %d, changed; removing",
1937279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1938279002Smav			changed = 1;
1939279002Smav		}
1940279002Smav		if (newlun->l_serial != NULL &&
1941279002Smav		    (oldlun->l_serial == NULL ||
1942279002Smav		     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1943279002Smav			log_debugx("serial for lun \"%s\", "
1944279002Smav			    "CTL lun %d changed; removing",
1945279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1946279002Smav			changed = 1;
1947279002Smav		}
1948279002Smav		if (changed) {
1949279002Smav			error = kernel_lun_remove(oldlun);
1950279002Smav			if (error != 0) {
1951279002Smav				log_warnx("failed to remove lun \"%s\", "
1952279002Smav				    "CTL lun %d",
1953279002Smav				    oldlun->l_name, oldlun->l_ctl_lun);
1954279002Smav				cumulated_error++;
1955255570Strasz			}
1956279002Smav			lun_delete(oldlun);
1957279002Smav			continue;
1958279002Smav		}
1959255570Strasz
1960279002Smav		lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1961279002Smav	}
1962279002Smav
1963279002Smav	TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1964279002Smav		oldlun = lun_find(oldconf, newlun->l_name);
1965279002Smav		if (oldlun != NULL) {
1966288728Smav			log_debugx("modifying lun \"%s\", CTL lun %d",
1967288728Smav			    newlun->l_name, newlun->l_ctl_lun);
1968288728Smav			error = kernel_lun_modify(newlun);
1969288728Smav			if (error != 0) {
1970288728Smav				log_warnx("failed to "
1971288728Smav				    "modify lun \"%s\", CTL lun %d",
1972279002Smav				    newlun->l_name, newlun->l_ctl_lun);
1973288728Smav				cumulated_error++;
1974255570Strasz			}
1975279002Smav			continue;
1976255570Strasz		}
1977279002Smav		log_debugx("adding lun \"%s\"", newlun->l_name);
1978279002Smav		error = kernel_lun_add(newlun);
1979279002Smav		if (error != 0) {
1980279002Smav			log_warnx("failed to add lun \"%s\"", newlun->l_name);
1981279002Smav			lun_delete(newlun);
1982279002Smav			cumulated_error++;
1983279002Smav		}
1984255570Strasz	}
1985255570Strasz
1986255570Strasz	/*
1987279006Smav	 * Now add new ports or modify existing ones.
1988255570Strasz	 */
1989279006Smav	TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
1990288729Smav		if (newport->p_foreign)
1991288729Smav			continue;
1992279006Smav		oldport = port_find(oldconf, newport->p_name);
1993255570Strasz
1994288729Smav		if (oldport == NULL || oldport->p_foreign) {
1995279055Smav			log_debugx("adding port \"%s\"", newport->p_name);
1996279006Smav			error = kernel_port_add(newport);
1997279006Smav		} else {
1998279055Smav			log_debugx("updating port \"%s\"", newport->p_name);
1999279006Smav			newport->p_ctl_port = oldport->p_ctl_port;
2000288748Smav			error = kernel_port_update(newport, oldport);
2001277749Strasz		}
2002279002Smav		if (error != 0) {
2003279006Smav			log_warnx("failed to %s port %s",
2004279006Smav			    (oldport == NULL) ? "add" : "update",
2005279006Smav			    newport->p_name);
2006279002Smav			/*
2007279002Smav			 * XXX: Uncomment after fixing the root cause.
2008279002Smav			 *
2009279002Smav			 * cumulated_error++;
2010279002Smav			 */
2011279002Smav		}
2012255570Strasz	}
2013255570Strasz
2014255570Strasz	/*
2015293290Sbdrewery	 * Go through the new portals, opening the sockets as necessary.
2016255570Strasz	 */
2017255570Strasz	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2018288729Smav		if (newpg->pg_foreign)
2019288729Smav			continue;
2020255570Strasz		if (newpg->pg_unassigned) {
2021255570Strasz			log_debugx("not listening on portal-group \"%s\", "
2022255570Strasz			    "not assigned to any target",
2023255570Strasz			    newpg->pg_name);
2024255570Strasz			continue;
2025255570Strasz		}
2026255570Strasz		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2027255570Strasz			/*
2028255570Strasz			 * Try to find already open portal and reuse
2029255570Strasz			 * the listening socket.  We don't care about
2030255570Strasz			 * what portal or portal group that was, what
2031255570Strasz			 * matters is the listening address.
2032255570Strasz			 */
2033255570Strasz			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2034255570Strasz			    pg_next) {
2035255570Strasz				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2036255570Strasz				    p_next) {
2037255570Strasz					if (strcmp(newp->p_listen,
2038255570Strasz					    oldp->p_listen) == 0 &&
2039255570Strasz					    oldp->p_socket > 0) {
2040255570Strasz						newp->p_socket =
2041255570Strasz						    oldp->p_socket;
2042255570Strasz						oldp->p_socket = 0;
2043255570Strasz						break;
2044255570Strasz					}
2045255570Strasz				}
2046255570Strasz			}
2047255570Strasz			if (newp->p_socket > 0) {
2048255570Strasz				/*
2049255570Strasz				 * We're done with this portal.
2050255570Strasz				 */
2051255570Strasz				continue;
2052255570Strasz			}
2053255570Strasz
2054255570Strasz#ifdef ICL_KERNEL_PROXY
2055265507Strasz			if (proxy_mode) {
2056265509Strasz				newpg->pg_conf->conf_portal_id++;
2057265509Strasz				newp->p_id = newpg->pg_conf->conf_portal_id;
2058265509Strasz				log_debugx("listening on %s, portal-group "
2059265509Strasz				    "\"%s\", portal id %d, using ICL proxy",
2060265509Strasz				    newp->p_listen, newpg->pg_name, newp->p_id);
2061265509Strasz				kernel_listen(newp->p_ai, newp->p_iser,
2062265509Strasz				    newp->p_id);
2063265507Strasz				continue;
2064265507Strasz			}
2065265507Strasz#endif
2066265507Strasz			assert(proxy_mode == false);
2067255570Strasz			assert(newp->p_iser == false);
2068255570Strasz
2069255570Strasz			log_debugx("listening on %s, portal-group \"%s\"",
2070255570Strasz			    newp->p_listen, newpg->pg_name);
2071255570Strasz			newp->p_socket = socket(newp->p_ai->ai_family,
2072255570Strasz			    newp->p_ai->ai_socktype,
2073255570Strasz			    newp->p_ai->ai_protocol);
2074255570Strasz			if (newp->p_socket < 0) {
2075255570Strasz				log_warn("socket(2) failed for %s",
2076255570Strasz				    newp->p_listen);
2077255570Strasz				cumulated_error++;
2078255570Strasz				continue;
2079255570Strasz			}
2080279001Smav			sockbuf = SOCKBUF_SIZE;
2081279001Smav			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2082279001Smav			    &sockbuf, sizeof(sockbuf)) == -1)
2083279001Smav				log_warn("setsockopt(SO_RCVBUF) failed "
2084279001Smav				    "for %s", newp->p_listen);
2085279001Smav			sockbuf = SOCKBUF_SIZE;
2086279001Smav			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2087279001Smav			    &sockbuf, sizeof(sockbuf)) == -1)
2088279001Smav				log_warn("setsockopt(SO_SNDBUF) failed "
2089279001Smav				    "for %s", newp->p_listen);
2090255570Strasz			error = setsockopt(newp->p_socket, SOL_SOCKET,
2091255570Strasz			    SO_REUSEADDR, &one, sizeof(one));
2092255570Strasz			if (error != 0) {
2093255570Strasz				log_warn("setsockopt(SO_REUSEADDR) failed "
2094255570Strasz				    "for %s", newp->p_listen);
2095255570Strasz				close(newp->p_socket);
2096255570Strasz				newp->p_socket = 0;
2097255570Strasz				cumulated_error++;
2098255570Strasz				continue;
2099255570Strasz			}
2100255570Strasz			error = bind(newp->p_socket, newp->p_ai->ai_addr,
2101255570Strasz			    newp->p_ai->ai_addrlen);
2102255570Strasz			if (error != 0) {
2103255570Strasz				log_warn("bind(2) failed for %s",
2104255570Strasz				    newp->p_listen);
2105255570Strasz				close(newp->p_socket);
2106255570Strasz				newp->p_socket = 0;
2107255570Strasz				cumulated_error++;
2108255570Strasz				continue;
2109255570Strasz			}
2110255570Strasz			error = listen(newp->p_socket, -1);
2111255570Strasz			if (error != 0) {
2112255570Strasz				log_warn("listen(2) failed for %s",
2113255570Strasz				    newp->p_listen);
2114255570Strasz				close(newp->p_socket);
2115255570Strasz				newp->p_socket = 0;
2116255570Strasz				cumulated_error++;
2117255570Strasz				continue;
2118255570Strasz			}
2119255570Strasz		}
2120255570Strasz	}
2121255570Strasz
2122255570Strasz	/*
2123255570Strasz	 * Go through the no longer used sockets, closing them.
2124255570Strasz	 */
2125255570Strasz	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2126255570Strasz		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2127255570Strasz			if (oldp->p_socket <= 0)
2128255570Strasz				continue;
2129255570Strasz			log_debugx("closing socket for %s, portal-group \"%s\"",
2130255570Strasz			    oldp->p_listen, oldpg->pg_name);
2131255570Strasz			close(oldp->p_socket);
2132255570Strasz			oldp->p_socket = 0;
2133255570Strasz		}
2134255570Strasz	}
2135255570Strasz
2136274939Smav	/* (Re-)Register on remaining/new iSNS servers. */
2137274939Smav	TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2138274939Smav		TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2139274939Smav			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2140274939Smav				break;
2141274939Smav		}
2142274939Smav		isns_register(newns, oldns);
2143274939Smav	}
2144274939Smav
2145274939Smav	/* Schedule iSNS update */
2146274939Smav	if (!TAILQ_EMPTY(&newconf->conf_isns))
2147274939Smav		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2148274939Smav
2149255570Strasz	return (cumulated_error);
2150255570Strasz}
2151255570Strasz
2152255570Straszbool
2153255570Strasztimed_out(void)
2154255570Strasz{
2155255570Strasz
2156255570Strasz	return (sigalrm_received);
2157255570Strasz}
2158255570Strasz
2159255570Straszstatic void
2160274939Smavsigalrm_handler_fatal(int dummy __unused)
2161255570Strasz{
2162255570Strasz	/*
2163255570Strasz	 * It would be easiest to just log an error and exit.  We can't
2164255570Strasz	 * do this, though, because log_errx() is not signal safe, since
2165255570Strasz	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
2166255570Strasz	 * and pdu_receive(), to call log_errx() there.  Should they fail
2167255570Strasz	 * to notice, we'll exit here one second later.
2168255570Strasz	 */
2169255570Strasz	if (sigalrm_received) {
2170255570Strasz		/*
2171255570Strasz		 * Oh well.  Just give up and quit.
2172255570Strasz		 */
2173255570Strasz		_exit(2);
2174255570Strasz	}
2175255570Strasz
2176255570Strasz	sigalrm_received = true;
2177255570Strasz}
2178255570Strasz
2179255570Straszstatic void
2180274939Smavsigalrm_handler(int dummy __unused)
2181255570Strasz{
2182274939Smav
2183274939Smav	sigalrm_received = true;
2184274939Smav}
2185274939Smav
2186274939Smavvoid
2187274939Smavset_timeout(int timeout, int fatal)
2188274939Smav{
2189255570Strasz	struct sigaction sa;
2190255570Strasz	struct itimerval itv;
2191255570Strasz	int error;
2192255570Strasz
2193274939Smav	if (timeout <= 0) {
2194255570Strasz		log_debugx("session timeout disabled");
2195274939Smav		bzero(&itv, sizeof(itv));
2196274939Smav		error = setitimer(ITIMER_REAL, &itv, NULL);
2197274939Smav		if (error != 0)
2198274939Smav			log_err(1, "setitimer");
2199274939Smav		sigalrm_received = false;
2200255570Strasz		return;
2201255570Strasz	}
2202255570Strasz
2203274939Smav	sigalrm_received = false;
2204255570Strasz	bzero(&sa, sizeof(sa));
2205274939Smav	if (fatal)
2206274939Smav		sa.sa_handler = sigalrm_handler_fatal;
2207274939Smav	else
2208274939Smav		sa.sa_handler = sigalrm_handler;
2209255570Strasz	sigfillset(&sa.sa_mask);
2210255570Strasz	error = sigaction(SIGALRM, &sa, NULL);
2211255570Strasz	if (error != 0)
2212255570Strasz		log_err(1, "sigaction");
2213255570Strasz
2214255570Strasz	/*
2215255570Strasz	 * First SIGALRM will arive after conf_timeout seconds.
2216255570Strasz	 * If we do nothing, another one will arrive a second later.
2217255570Strasz	 */
2218274939Smav	log_debugx("setting session timeout to %d seconds", timeout);
2219255570Strasz	bzero(&itv, sizeof(itv));
2220255570Strasz	itv.it_interval.tv_sec = 1;
2221274939Smav	itv.it_value.tv_sec = timeout;
2222255570Strasz	error = setitimer(ITIMER_REAL, &itv, NULL);
2223255570Strasz	if (error != 0)
2224255570Strasz		log_err(1, "setitimer");
2225255570Strasz}
2226255570Strasz
2227255570Straszstatic int
2228255570Straszwait_for_children(bool block)
2229255570Strasz{
2230255570Strasz	pid_t pid;
2231255570Strasz	int status;
2232255570Strasz	int num = 0;
2233255570Strasz
2234255570Strasz	for (;;) {
2235255570Strasz		/*
2236255570Strasz		 * If "block" is true, wait for at least one process.
2237255570Strasz		 */
2238255570Strasz		if (block && num == 0)
2239255570Strasz			pid = wait4(-1, &status, 0, NULL);
2240255570Strasz		else
2241255570Strasz			pid = wait4(-1, &status, WNOHANG, NULL);
2242255570Strasz		if (pid <= 0)
2243255570Strasz			break;
2244255570Strasz		if (WIFSIGNALED(status)) {
2245255570Strasz			log_warnx("child process %d terminated with signal %d",
2246255570Strasz			    pid, WTERMSIG(status));
2247255570Strasz		} else if (WEXITSTATUS(status) != 0) {
2248255570Strasz			log_warnx("child process %d terminated with exit status %d",
2249255570Strasz			    pid, WEXITSTATUS(status));
2250255570Strasz		} else {
2251255570Strasz			log_debugx("child process %d terminated gracefully", pid);
2252255570Strasz		}
2253255570Strasz		num++;
2254255570Strasz	}
2255255570Strasz
2256255570Strasz	return (num);
2257255570Strasz}
2258255570Strasz
2259255570Straszstatic void
2260265513Straszhandle_connection(struct portal *portal, int fd,
2261270137Smav    const struct sockaddr *client_sa, bool dont_fork)
2262255570Strasz{
2263255570Strasz	struct connection *conn;
2264255570Strasz	int error;
2265255570Strasz	pid_t pid;
2266255570Strasz	char host[NI_MAXHOST + 1];
2267255570Strasz	struct conf *conf;
2268255570Strasz
2269255570Strasz	conf = portal->p_portal_group->pg_conf;
2270255570Strasz
2271255570Strasz	if (dont_fork) {
2272255570Strasz		log_debugx("incoming connection; not forking due to -d flag");
2273255570Strasz	} else {
2274255570Strasz		nchildren -= wait_for_children(false);
2275255570Strasz		assert(nchildren >= 0);
2276255570Strasz
2277255570Strasz		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2278255570Strasz			log_debugx("maxproc limit of %d child processes hit; "
2279255570Strasz			    "waiting for child process to exit", conf->conf_maxproc);
2280255570Strasz			nchildren -= wait_for_children(true);
2281255570Strasz			assert(nchildren >= 0);
2282255570Strasz		}
2283255570Strasz		log_debugx("incoming connection; forking child process #%d",
2284255570Strasz		    nchildren);
2285255570Strasz		nchildren++;
2286255570Strasz		pid = fork();
2287255570Strasz		if (pid < 0)
2288255570Strasz			log_err(1, "fork");
2289255570Strasz		if (pid > 0) {
2290255570Strasz			close(fd);
2291255570Strasz			return;
2292255570Strasz		}
2293255570Strasz	}
2294255570Strasz	pidfile_close(conf->conf_pidfh);
2295255570Strasz
2296270137Smav	error = getnameinfo(client_sa, client_sa->sa_len,
2297265513Strasz	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2298265513Strasz	if (error != 0)
2299265513Strasz		log_errx(1, "getnameinfo: %s", gai_strerror(error));
2300255570Strasz
2301265513Strasz	log_debugx("accepted connection from %s; portal group \"%s\"",
2302265513Strasz	    host, portal->p_portal_group->pg_name);
2303265513Strasz	log_set_peer_addr(host);
2304265513Strasz	setproctitle("%s", host);
2305255570Strasz
2306270137Smav	conn = connection_new(portal, fd, host, client_sa);
2307274939Smav	set_timeout(conf->conf_timeout, true);
2308255570Strasz	kernel_capsicate();
2309255570Strasz	login(conn);
2310255570Strasz	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2311255570Strasz		kernel_handoff(conn);
2312255570Strasz		log_debugx("connection handed off to the kernel");
2313255570Strasz	} else {
2314255570Strasz		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2315255570Strasz		discovery(conn);
2316255570Strasz	}
2317255570Strasz	log_debugx("nothing more to do; exiting");
2318255570Strasz	exit(0);
2319255570Strasz}
2320255570Strasz
2321255570Straszstatic int
2322255570Straszfd_add(int fd, fd_set *fdset, int nfds)
2323255570Strasz{
2324255570Strasz
2325255570Strasz	/*
2326255570Strasz	 * Skip sockets which we failed to bind.
2327255570Strasz	 */
2328255570Strasz	if (fd <= 0)
2329255570Strasz		return (nfds);
2330255570Strasz
2331255570Strasz	FD_SET(fd, fdset);
2332255570Strasz	if (fd > nfds)
2333255570Strasz		nfds = fd;
2334255570Strasz	return (nfds);
2335255570Strasz}
2336255570Strasz
2337255570Straszstatic void
2338255570Straszmain_loop(struct conf *conf, bool dont_fork)
2339255570Strasz{
2340255570Strasz	struct portal_group *pg;
2341255570Strasz	struct portal *portal;
2342265512Strasz	struct sockaddr_storage client_sa;
2343265512Strasz	socklen_t client_salen;
2344255570Strasz#ifdef ICL_KERNEL_PROXY
2345255570Strasz	int connection_id;
2346265509Strasz	int portal_id;
2347265507Strasz#endif
2348255570Strasz	fd_set fdset;
2349255570Strasz	int error, nfds, client_fd;
2350255570Strasz
2351255570Strasz	pidfile_write(conf->conf_pidfh);
2352255570Strasz
2353255570Strasz	for (;;) {
2354274939Smav		if (sighup_received || sigterm_received || timed_out())
2355255570Strasz			return;
2356255570Strasz
2357255570Strasz#ifdef ICL_KERNEL_PROXY
2358265507Strasz		if (proxy_mode) {
2359265513Strasz			client_salen = sizeof(client_sa);
2360265513Strasz			kernel_accept(&connection_id, &portal_id,
2361265513Strasz			    (struct sockaddr *)&client_sa, &client_salen);
2362271627Strasz			assert(client_salen >= client_sa.ss_len);
2363255570Strasz
2364265509Strasz			log_debugx("incoming connection, id %d, portal id %d",
2365265509Strasz			    connection_id, portal_id);
2366265509Strasz			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2367265509Strasz				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2368265509Strasz					if (portal->p_id == portal_id) {
2369265509Strasz						goto found;
2370265509Strasz					}
2371265509Strasz				}
2372265509Strasz			}
2373255570Strasz
2374265509Strasz			log_errx(1, "kernel returned invalid portal_id %d",
2375265509Strasz			    portal_id);
2376265509Strasz
2377265509Straszfound:
2378265513Strasz			handle_connection(portal, connection_id,
2379270137Smav			    (struct sockaddr *)&client_sa, dont_fork);
2380265507Strasz		} else {
2381265507Strasz#endif
2382265507Strasz			assert(proxy_mode == false);
2383265507Strasz
2384265507Strasz			FD_ZERO(&fdset);
2385265507Strasz			nfds = 0;
2386265507Strasz			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2387265507Strasz				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2388265507Strasz					nfds = fd_add(portal->p_socket, &fdset, nfds);
2389255570Strasz			}
2390265507Strasz			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2391265507Strasz			if (error <= 0) {
2392265507Strasz				if (errno == EINTR)
2393265507Strasz					return;
2394265507Strasz				log_err(1, "select");
2395265507Strasz			}
2396265507Strasz			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2397265507Strasz				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2398265507Strasz					if (!FD_ISSET(portal->p_socket, &fdset))
2399265507Strasz						continue;
2400265512Strasz					client_salen = sizeof(client_sa);
2401265512Strasz					client_fd = accept(portal->p_socket,
2402265512Strasz					    (struct sockaddr *)&client_sa,
2403265512Strasz					    &client_salen);
2404281488Smav					if (client_fd < 0) {
2405281488Smav						if (errno == ECONNABORTED)
2406281488Smav							continue;
2407265507Strasz						log_err(1, "accept");
2408281488Smav					}
2409271627Strasz					assert(client_salen >= client_sa.ss_len);
2410271627Strasz
2411265512Strasz					handle_connection(portal, client_fd,
2412265513Strasz					    (struct sockaddr *)&client_sa,
2413270137Smav					    dont_fork);
2414265507Strasz					break;
2415265507Strasz				}
2416265507Strasz			}
2417265507Strasz#ifdef ICL_KERNEL_PROXY
2418255570Strasz		}
2419265507Strasz#endif
2420255570Strasz	}
2421255570Strasz}
2422255570Strasz
2423255570Straszstatic void
2424255570Straszsighup_handler(int dummy __unused)
2425255570Strasz{
2426255570Strasz
2427255570Strasz	sighup_received = true;
2428255570Strasz}
2429255570Strasz
2430255570Straszstatic void
2431255570Straszsigterm_handler(int dummy __unused)
2432255570Strasz{
2433255570Strasz
2434255570Strasz	sigterm_received = true;
2435255570Strasz}
2436255570Strasz
2437255570Straszstatic void
2438263730Straszsigchld_handler(int dummy __unused)
2439263730Strasz{
2440263730Strasz
2441263730Strasz	/*
2442263730Strasz	 * The only purpose of this handler is to make SIGCHLD
2443263730Strasz	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2444263730Strasz	 * wait_for_children().
2445263730Strasz	 */
2446263730Strasz}
2447263730Strasz
2448263730Straszstatic void
2449255570Straszregister_signals(void)
2450255570Strasz{
2451255570Strasz	struct sigaction sa;
2452255570Strasz	int error;
2453255570Strasz
2454255570Strasz	bzero(&sa, sizeof(sa));
2455255570Strasz	sa.sa_handler = sighup_handler;
2456255570Strasz	sigfillset(&sa.sa_mask);
2457255570Strasz	error = sigaction(SIGHUP, &sa, NULL);
2458255570Strasz	if (error != 0)
2459255570Strasz		log_err(1, "sigaction");
2460255570Strasz
2461255570Strasz	sa.sa_handler = sigterm_handler;
2462255570Strasz	error = sigaction(SIGTERM, &sa, NULL);
2463255570Strasz	if (error != 0)
2464255570Strasz		log_err(1, "sigaction");
2465255570Strasz
2466255570Strasz	sa.sa_handler = sigterm_handler;
2467255570Strasz	error = sigaction(SIGINT, &sa, NULL);
2468255570Strasz	if (error != 0)
2469255570Strasz		log_err(1, "sigaction");
2470263730Strasz
2471263730Strasz	sa.sa_handler = sigchld_handler;
2472263730Strasz	error = sigaction(SIGCHLD, &sa, NULL);
2473263730Strasz	if (error != 0)
2474263730Strasz		log_err(1, "sigaction");
2475255570Strasz}
2476255570Strasz
2477255570Straszint
2478255570Straszmain(int argc, char **argv)
2479255570Strasz{
2480255570Strasz	struct conf *oldconf, *newconf, *tmpconf;
2481274939Smav	struct isns *newns;
2482255570Strasz	const char *config_path = DEFAULT_CONFIG_PATH;
2483255570Strasz	int debug = 0, ch, error;
2484255570Strasz	bool dont_daemonize = false;
2485255570Strasz
2486265507Strasz	while ((ch = getopt(argc, argv, "df:R")) != -1) {
2487255570Strasz		switch (ch) {
2488255570Strasz		case 'd':
2489255570Strasz			dont_daemonize = true;
2490255570Strasz			debug++;
2491255570Strasz			break;
2492255570Strasz		case 'f':
2493255570Strasz			config_path = optarg;
2494255570Strasz			break;
2495265507Strasz		case 'R':
2496265507Strasz#ifndef ICL_KERNEL_PROXY
2497265507Strasz			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2498265507Strasz			    "does not support iSER protocol");
2499265507Strasz#endif
2500265507Strasz			proxy_mode = true;
2501265507Strasz			break;
2502255570Strasz		case '?':
2503255570Strasz		default:
2504255570Strasz			usage();
2505255570Strasz		}
2506255570Strasz	}
2507255570Strasz	argc -= optind;
2508255570Strasz	if (argc != 0)
2509255570Strasz		usage();
2510255570Strasz
2511255570Strasz	log_init(debug);
2512255570Strasz	kernel_init();
2513255570Strasz
2514255570Strasz	oldconf = conf_new_from_kernel();
2515279055Smav	newconf = conf_new_from_file(config_path, oldconf);
2516255570Strasz	if (newconf == NULL)
2517265516Strasz		log_errx(1, "configuration error; exiting");
2518255570Strasz	if (debug > 0) {
2519255570Strasz		oldconf->conf_debug = debug;
2520255570Strasz		newconf->conf_debug = debug;
2521255570Strasz	}
2522255570Strasz
2523255570Strasz	error = conf_apply(oldconf, newconf);
2524255570Strasz	if (error != 0)
2525265516Strasz		log_errx(1, "failed to apply configuration; exiting");
2526265516Strasz
2527255570Strasz	conf_delete(oldconf);
2528255570Strasz	oldconf = NULL;
2529255570Strasz
2530255570Strasz	register_signals();
2531255570Strasz
2532263719Strasz	if (dont_daemonize == false) {
2533263719Strasz		log_debugx("daemonizing");
2534263719Strasz		if (daemon(0, 0) == -1) {
2535263719Strasz			log_warn("cannot daemonize");
2536263719Strasz			pidfile_remove(newconf->conf_pidfh);
2537263719Strasz			exit(1);
2538263719Strasz		}
2539263719Strasz	}
2540263719Strasz
2541274939Smav	/* Schedule iSNS update */
2542274939Smav	if (!TAILQ_EMPTY(&newconf->conf_isns))
2543274939Smav		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2544274939Smav
2545255570Strasz	for (;;) {
2546255570Strasz		main_loop(newconf, dont_daemonize);
2547255570Strasz		if (sighup_received) {
2548255570Strasz			sighup_received = false;
2549255570Strasz			log_debugx("received SIGHUP, reloading configuration");
2550279055Smav			tmpconf = conf_new_from_file(config_path, newconf);
2551255570Strasz			if (tmpconf == NULL) {
2552255570Strasz				log_warnx("configuration error, "
2553255570Strasz				    "continuing with old configuration");
2554255570Strasz			} else {
2555255570Strasz				if (debug > 0)
2556255570Strasz					tmpconf->conf_debug = debug;
2557255570Strasz				oldconf = newconf;
2558255570Strasz				newconf = tmpconf;
2559255570Strasz				error = conf_apply(oldconf, newconf);
2560255570Strasz				if (error != 0)
2561255570Strasz					log_warnx("failed to reload "
2562255570Strasz					    "configuration");
2563255570Strasz				conf_delete(oldconf);
2564255570Strasz				oldconf = NULL;
2565255570Strasz			}
2566255570Strasz		} else if (sigterm_received) {
2567255570Strasz			log_debugx("exiting on signal; "
2568255570Strasz			    "reloading empty configuration");
2569255570Strasz
2570279003Smav			log_debugx("removing CTL iSCSI ports "
2571255570Strasz			    "and terminating all connections");
2572255570Strasz
2573255570Strasz			oldconf = newconf;
2574255570Strasz			newconf = conf_new();
2575255570Strasz			if (debug > 0)
2576255570Strasz				newconf->conf_debug = debug;
2577255570Strasz			error = conf_apply(oldconf, newconf);
2578255570Strasz			if (error != 0)
2579255570Strasz				log_warnx("failed to apply configuration");
2580274939Smav			conf_delete(oldconf);
2581274939Smav			oldconf = NULL;
2582255570Strasz
2583255570Strasz			log_warnx("exiting on signal");
2584255570Strasz			exit(0);
2585255570Strasz		} else {
2586255570Strasz			nchildren -= wait_for_children(false);
2587255570Strasz			assert(nchildren >= 0);
2588274939Smav			if (timed_out()) {
2589274939Smav				set_timeout(0, false);
2590274939Smav				TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2591274939Smav					isns_check(newns);
2592274939Smav				/* Schedule iSNS update */
2593274939Smav				if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2594274939Smav					set_timeout((newconf->conf_isns_period
2595274939Smav					    + 2) / 3,
2596274939Smav					    false);
2597274939Smav				}
2598274939Smav			}
2599255570Strasz		}
2600255570Strasz	}
2601255570Strasz	/* NOTREACHED */
2602255570Strasz}
2603