config.c revision 331722
1/*	$FreeBSD: stable/11/usr.sbin/rtadvd/config.c 331722 2018-03-29 02:50:57Z eadler $	*/
2/*	$KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $	*/
3
4/*
5 * Copyright (C) 1998 WIDE Project.
6 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/param.h>
35#include <sys/ioctl.h>
36#include <sys/socket.h>
37
38#include <net/if.h>
39#include <net/route.h>
40#include <net/if_dl.h>
41
42#include <netinet/in.h>
43#include <netinet/in_var.h>
44#include <netinet/ip6.h>
45#include <netinet6/ip6_var.h>
46#include <netinet/icmp6.h>
47#include <netinet6/nd6.h>
48
49#include <arpa/inet.h>
50
51#include <stdio.h>
52#include <syslog.h>
53#include <errno.h>
54#include <inttypes.h>
55#include <netdb.h>
56#include <string.h>
57#include <search.h>
58#include <stdlib.h>
59#include <time.h>
60#include <unistd.h>
61#include <ifaddrs.h>
62
63#include "rtadvd.h"
64#include "advcap.h"
65#include "timer.h"
66#include "if.h"
67#include "config.h"
68
69/* label of tcapcode + number + domain name + zero octet */
70static char entbuf[10 + 3 + NI_MAXHOST + 1];
71static char oentbuf[10 + 3 + NI_MAXHOST + 1];
72static char abuf[DNAME_LABELENC_MAXLEN];
73
74static time_t prefix_timo = (60 * 120);	/* 2 hours.
75					 * XXX: should be configurable. */
76
77static struct rtadvd_timer *prefix_timeout(void *);
78static void makeentry(char *, size_t, int, const char *);
79static ssize_t dname_labelenc(char *, const char *);
80
81/* Encode domain name label encoding in RFC 1035 Section 3.1 */
82static ssize_t
83dname_labelenc(char *dst, const char *src)
84{
85	char *dst_origin;
86	char *p;
87	size_t len;
88
89	dst_origin = dst;
90	len = strlen(src);
91
92	if (len + len / 64 + 1 + 1 > DNAME_LABELENC_MAXLEN)
93		return (-1);
94	/* Length fields per 63 octets + '\0' (<= DNAME_LABELENC_MAXLEN) */
95	memset(dst, 0, len + len / 64 + 1 + 1);
96
97	syslog(LOG_DEBUG, "<%s> labelenc = %s", __func__, src);
98	while (src && (len = strlen(src)) != 0) {
99		/* Put a length field with 63 octet limitation first. */
100		p = strchr(src, '.');
101		if (p == NULL)
102			*dst = len = MIN(63, len);
103		else
104			*dst = len = MIN(63, p - src);
105		if (dst + 1 + len < dst_origin + DNAME_LABELENC_MAXLEN)
106			dst++;
107		else
108			return (-1);
109		/* Copy 63 octets at most. */
110		memcpy(dst, src, len);
111		dst += len;
112		if (p == NULL) /* the last label */
113			break;
114		src = p + 1;
115	}
116	/* Always need a 0-length label at the tail. */
117	*dst++ = '\0';
118
119	syslog(LOG_DEBUG, "<%s> labellen = %td", __func__, dst - dst_origin);
120	return (dst - dst_origin);
121}
122
123#define	MUSTHAVE(var, cap)						\
124    do {								\
125	int64_t t;							\
126	if ((t = agetnum(cap)) < 0) {					\
127		fprintf(stderr, "rtadvd: need %s for interface %s\n",	\
128			cap, intface);					\
129		exit(1);						\
130	}								\
131	var = t;							\
132     } while (0)
133
134#define	MAYHAVE(var, cap, def)						\
135     do {								\
136	if ((var = agetnum(cap)) < 0)					\
137		var = def;						\
138     } while (0)
139
140int
141loadconfig_index(int idx)
142{
143	char ifname[IFNAMSIZ];
144
145	syslog(LOG_DEBUG, "<%s> enter", __func__);
146
147	if (if_indextoname(idx, ifname) != NULL)
148		return (loadconfig_ifname(ifname));
149	else
150		return (1);
151}
152
153int
154loadconfig_ifname(char *ifname)
155{
156	struct ifinfo *ifi;
157
158	syslog(LOG_DEBUG, "<%s> enter", __func__);
159
160	update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
161	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
162		/* NULL means all IFs will be processed. */
163		if (ifname != NULL &&
164		    strcmp(ifi->ifi_ifname, ifname) != 0)
165			continue;
166
167		if (!ifi->ifi_persist) {
168			syslog(LOG_INFO,
169			    "<%s> %s is not a target interface.  "
170			    "Ignored at this moment.", __func__,
171			    ifi->ifi_ifname);
172			continue;
173
174		}
175		if (ifi->ifi_ifindex == 0) {
176			syslog(LOG_ERR,
177			    "<%s> %s not found.  "
178			    "Ignored at this moment.", __func__,
179			    ifi->ifi_ifname);
180			continue;
181		}
182		if (getconfig(ifi) == NULL) {
183			syslog(LOG_ERR,
184			    "<%s> invalid configuration for %s.  "
185			    "Ignored at this moment.", __func__,
186			    ifi->ifi_ifname);
187			continue;
188		}
189	}
190	return (0);
191}
192
193int
194rm_ifinfo_index(int idx)
195{
196	struct ifinfo *ifi;
197
198	ifi = if_indextoifinfo(idx);
199	if (ifi == NULL) {
200		syslog(LOG_ERR, "<%s>: ifinfo not found (idx=%d)",
201		    __func__, idx);
202		return (-1);
203	}
204
205	return (rm_ifinfo(ifi));
206}
207
208int
209rm_ifinfo(struct ifinfo *ifi)
210{
211	int error;
212
213	syslog(LOG_DEBUG, "<%s> enter (%s).", __func__, ifi->ifi_ifname);
214	switch (ifi->ifi_state) {
215	case IFI_STATE_UNCONFIGURED:
216		return (0);
217		break;
218	default:
219		ifi->ifi_state = IFI_STATE_UNCONFIGURED;
220		syslog(LOG_DEBUG,
221		    "<%s> ifname=%s marked as UNCONFIGURED.",
222		    __func__, ifi->ifi_ifname);
223
224		/* XXX: No MC leaving here because index is disappeared */
225
226		/* Inactivate timer */
227		rtadvd_remove_timer(ifi->ifi_ra_timer);
228		ifi->ifi_ra_timer = NULL;
229		break;
230	}
231
232	/* clean up ifi */
233	if (!ifi->ifi_persist) {
234		TAILQ_REMOVE(&ifilist, ifi, ifi_next);
235		syslog(LOG_DEBUG, "<%s>: ifinfo (idx=%d) removed.",
236		    __func__, ifi->ifi_ifindex);
237	} else {
238		/* recreate an empty entry */
239		update_persist_ifinfo(&ifilist, ifi->ifi_ifname);
240		syslog(LOG_DEBUG, "<%s>: ifname=%s is persistent.",
241		    __func__, ifi->ifi_ifname);
242	}
243
244	/* clean up rai if any */
245	switch (ifi->ifi_state) {
246	case IFI_STATE_CONFIGURED:
247		if (ifi->ifi_rainfo != NULL) {
248			error = rm_rainfo(ifi->ifi_rainfo);
249			if (error)
250				return (error);
251			ifi->ifi_rainfo = NULL;
252		}
253		break;
254	case IFI_STATE_TRANSITIVE:
255		if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
256			if (ifi->ifi_rainfo != NULL) {
257				error = rm_rainfo(ifi->ifi_rainfo);
258				if (error)
259					return (error);
260				ifi->ifi_rainfo = NULL;
261				ifi->ifi_rainfo_trans = NULL;
262			}
263		} else {
264			if (ifi->ifi_rainfo != NULL) {
265				error = rm_rainfo(ifi->ifi_rainfo);
266				if (error)
267					return (error);
268				ifi->ifi_rainfo = NULL;
269			}
270			if (ifi->ifi_rainfo_trans != NULL) {
271				error = rm_rainfo(ifi->ifi_rainfo_trans);
272				if (error)
273					return (error);
274				ifi->ifi_rainfo_trans = NULL;
275			}
276		}
277	}
278
279	syslog(LOG_DEBUG, "<%s> leave (%s).", __func__, ifi->ifi_ifname);
280	if (!ifi->ifi_persist)
281		free(ifi);
282	return (0);
283}
284
285int
286rm_rainfo(struct rainfo *rai)
287{
288	struct prefix *pfx;
289	struct soliciter *sol;
290	struct rdnss *rdn;
291	struct rdnss_addr *rdna;
292	struct dnssl *dns;
293	struct rtinfo *rti;
294
295	syslog(LOG_DEBUG, "<%s>: enter",  __func__);
296
297	TAILQ_REMOVE(&railist, rai, rai_next);
298	if (rai->rai_ifinfo != NULL)
299		syslog(LOG_DEBUG, "<%s>: rainfo (idx=%d) removed.",
300		    __func__, rai->rai_ifinfo->ifi_ifindex);
301
302	if (rai->rai_ra_data != NULL)
303		free(rai->rai_ra_data);
304
305	while ((pfx = TAILQ_FIRST(&rai->rai_prefix)) != NULL)
306		delete_prefix(pfx);
307	while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
308		TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
309		free(sol);
310	}
311	while ((rdn = TAILQ_FIRST(&rai->rai_rdnss)) != NULL) {
312		TAILQ_REMOVE(&rai->rai_rdnss, rdn, rd_next);
313		while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
314			TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
315			free(rdna);
316		}
317		free(rdn);
318	}
319	while ((dns = TAILQ_FIRST(&rai->rai_dnssl)) != NULL) {
320		TAILQ_REMOVE(&rai->rai_dnssl, dns, dn_next);
321		free(dns);
322	}
323	while ((rti = TAILQ_FIRST(&rai->rai_route)) != NULL) {
324		TAILQ_REMOVE(&rai->rai_route, rti, rti_next);
325		free(rti);
326	}
327	free(rai);
328	syslog(LOG_DEBUG, "<%s>: leave",  __func__);
329
330	return (0);
331}
332
333struct ifinfo *
334getconfig(struct ifinfo *ifi)
335{
336	int stat, i;
337	int error;
338	char tbuf[BUFSIZ];
339	struct rainfo *rai;
340	struct rainfo *rai_old;
341	int32_t val;
342	int64_t val64;
343	char buf[BUFSIZ];
344	char *bp = buf;
345	char *addr, *flagstr;
346
347	if (ifi == NULL)	/* if does not exist */
348		return (NULL);
349
350	if (ifi->ifi_state == IFI_STATE_TRANSITIVE &&
351	    ifi->ifi_rainfo == NULL) {
352		syslog(LOG_INFO, "<%s> %s is shutting down.  Skipped.",
353		    __func__, ifi->ifi_ifname);
354		return (NULL);
355	}
356
357	if ((stat = agetent(tbuf, ifi->ifi_ifname)) <= 0) {
358		memset(tbuf, 0, sizeof(tbuf));
359		syslog(LOG_INFO,
360		    "<%s> %s isn't defined in the configuration file"
361		    " or the configuration file doesn't exist."
362		    " Treat it as default",
363		     __func__, ifi->ifi_ifname);
364	}
365
366	ELM_MALLOC(rai, exit(1));
367	TAILQ_INIT(&rai->rai_prefix);
368	TAILQ_INIT(&rai->rai_route);
369	TAILQ_INIT(&rai->rai_rdnss);
370	TAILQ_INIT(&rai->rai_dnssl);
371	TAILQ_INIT(&rai->rai_soliciter);
372	rai->rai_ifinfo = ifi;
373
374	/* gather on-link prefixes from the network interfaces. */
375	if (agetflag("noifprefix"))
376		rai->rai_advifprefix = 0;
377	else
378		rai->rai_advifprefix = 1;
379
380	/* get interface information */
381	if (agetflag("nolladdr"))
382		rai->rai_advlinkopt = 0;
383	else
384		rai->rai_advlinkopt = 1;
385	if (rai->rai_advlinkopt) {
386		if (ifi->ifi_sdl.sdl_type == 0) {
387			syslog(LOG_ERR,
388			    "<%s> can't get information of %s",
389			    __func__, ifi->ifi_ifname);
390			goto getconfig_free_rai;
391		}
392	}
393
394	/*
395	 * set router configuration variables.
396	 */
397	MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
398	if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
399		syslog(LOG_ERR,
400		    "<%s> maxinterval (%" PRIu32 ") on %s is invalid "
401		    "(must be between %u and %u)", __func__, val,
402		    ifi->ifi_ifname, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
403		goto getconfig_free_rai;
404	}
405	rai->rai_maxinterval = (uint16_t)val;
406
407	MAYHAVE(val, "mininterval", rai->rai_maxinterval/3);
408	if ((uint16_t)val < MIN_MININTERVAL ||
409	    (uint16_t)val > (rai->rai_maxinterval * 3) / 4) {
410		syslog(LOG_ERR,
411		    "<%s> mininterval (%" PRIu32 ") on %s is invalid "
412		    "(must be between %d and %d)",
413		    __func__, val, ifi->ifi_ifname, MIN_MININTERVAL,
414		    (rai->rai_maxinterval * 3) / 4);
415		goto getconfig_free_rai;
416	}
417	rai->rai_mininterval = (uint16_t)val;
418
419	MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
420	rai->rai_hoplimit = val & 0xff;
421
422	if ((flagstr = (char *)agetstr("raflags", &bp))) {
423		val = 0;
424		if (strchr(flagstr, 'm'))
425			val |= ND_RA_FLAG_MANAGED;
426		if (strchr(flagstr, 'o'))
427			val |= ND_RA_FLAG_OTHER;
428		if (strchr(flagstr, 'h'))
429			val |= ND_RA_FLAG_RTPREF_HIGH;
430		if (strchr(flagstr, 'l')) {
431			if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
432				syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
433				    " router flags are exclusive", __func__);
434				goto getconfig_free_rai;
435			}
436			val |= ND_RA_FLAG_RTPREF_LOW;
437		}
438	} else
439		MAYHAVE(val, "raflags", 0);
440
441	rai->rai_managedflg = val & ND_RA_FLAG_MANAGED;
442	rai->rai_otherflg = val & ND_RA_FLAG_OTHER;
443#ifndef ND_RA_FLAG_RTPREF_MASK
444#define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
445#define ND_RA_FLAG_RTPREF_RSV	0x10 /* 00010000 */
446#endif
447	rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
448	if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) {
449		syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
450		    __func__, rai->rai_rtpref, ifi->ifi_ifname);
451		goto getconfig_free_rai;
452	}
453
454	MAYHAVE(val, "rltime", rai->rai_maxinterval * 3);
455	if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval ||
456	    (uint16_t)val > MAXROUTERLIFETIME)) {
457		syslog(LOG_ERR,
458		    "<%s> router lifetime (%" PRIu32 ") on %s is invalid "
459		    "(must be 0 or between %d and %d)",
460		    __func__, val, ifi->ifi_ifname, rai->rai_maxinterval,
461		    MAXROUTERLIFETIME);
462		goto getconfig_free_rai;
463	}
464	rai->rai_lifetime = val & 0xffff;
465
466	MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
467	if (val < 0 || val > MAXREACHABLETIME) {
468		syslog(LOG_ERR,
469		    "<%s> reachable time (%" PRIu32 ") on %s is invalid "
470		    "(must be no greater than %d)",
471		    __func__, val, ifi->ifi_ifname, MAXREACHABLETIME);
472		goto getconfig_free_rai;
473	}
474	rai->rai_reachabletime = (uint32_t)val;
475
476	MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
477	if (val64 < 0 || val64 > 0xffffffff) {
478		syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range",
479		    __func__, val64, ifi->ifi_ifname);
480		goto getconfig_free_rai;
481	}
482	rai->rai_retranstimer = (uint32_t)val64;
483
484	if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
485		syslog(LOG_ERR,
486		    "<%s> mobile-ip6 configuration not supported",
487		    __func__);
488		goto getconfig_free_rai;
489	}
490	/* prefix information */
491
492	/*
493	 * This is an implementation specific parameter to consider
494	 * link propagation delays and poorly synchronized clocks when
495	 * checking consistency of advertised lifetimes.
496	 */
497	MAYHAVE(val, "clockskew", 0);
498	rai->rai_clockskew = val;
499
500	rai->rai_pfxs = 0;
501	for (i = -1; i < MAXPREFIX; i++) {
502		struct prefix *pfx;
503
504		makeentry(entbuf, sizeof(entbuf), i, "addr");
505		addr = (char *)agetstr(entbuf, &bp);
506		if (addr == NULL)
507			continue;
508
509		/* allocate memory to store prefix information */
510		ELM_MALLOC(pfx, exit(1));
511		pfx->pfx_rainfo = rai;
512		pfx->pfx_origin = PREFIX_FROM_CONFIG;
513
514		if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) {
515			syslog(LOG_ERR,
516			    "<%s> inet_pton failed for %s",
517			    __func__, addr);
518			goto getconfig_free_pfx;
519		}
520		if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) {
521			syslog(LOG_ERR,
522			    "<%s> multicast prefix (%s) must "
523			    "not be advertised on %s",
524			    __func__, addr, ifi->ifi_ifname);
525			goto getconfig_free_pfx;
526		}
527		if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix))
528			syslog(LOG_NOTICE,
529			    "<%s> link-local prefix (%s) will be"
530			    " advertised on %s",
531			    __func__, addr, ifi->ifi_ifname);
532
533		makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
534		MAYHAVE(val, entbuf, 64);
535		if (val < 0 || val > 128) {
536			syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s "
537			    "on %s out of range",
538			    __func__, val, addr, ifi->ifi_ifname);
539			goto getconfig_free_pfx;
540		}
541		pfx->pfx_prefixlen = (int)val;
542
543		makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
544		if ((flagstr = (char *)agetstr(entbuf, &bp))) {
545			val = 0;
546			if (strchr(flagstr, 'l'))
547				val |= ND_OPT_PI_FLAG_ONLINK;
548			if (strchr(flagstr, 'a'))
549				val |= ND_OPT_PI_FLAG_AUTO;
550		} else {
551			MAYHAVE(val, entbuf,
552			    (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
553		}
554		pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
555		pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
556
557		makeentry(entbuf, sizeof(entbuf), i, "vltime");
558		MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
559		if (val64 < 0 || val64 > 0xffffffff) {
560			syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for "
561			    "%s/%d on %s is out of range",
562			    __func__, val64,
563			    addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
564			goto getconfig_free_pfx;
565		}
566		pfx->pfx_validlifetime = (uint32_t)val64;
567
568		makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
569		if (agetflag(entbuf)) {
570			struct timespec now;
571
572			clock_gettime(CLOCK_MONOTONIC_FAST, &now);
573			pfx->pfx_vltimeexpire =
574				now.tv_sec + pfx->pfx_validlifetime;
575		}
576
577		makeentry(entbuf, sizeof(entbuf), i, "pltime");
578		MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
579		if (val64 < 0 || val64 > 0xffffffff) {
580			syslog(LOG_ERR,
581			    "<%s> pltime (%" PRIu64 ") for %s/%d on %s "
582			    "is out of range",
583			    __func__, val64,
584			    addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
585			goto getconfig_free_pfx;
586		}
587		pfx->pfx_preflifetime = (uint32_t)val64;
588
589		makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
590		if (agetflag(entbuf)) {
591			struct timespec now;
592
593			clock_gettime(CLOCK_MONOTONIC_FAST, &now);
594			pfx->pfx_pltimeexpire =
595			    now.tv_sec + pfx->pfx_preflifetime;
596		}
597		/* link into chain */
598		TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
599		rai->rai_pfxs++;
600		continue;
601getconfig_free_pfx:
602		free(pfx);
603	}
604	if (rai->rai_advifprefix && rai->rai_pfxs == 0)
605		get_prefix(rai);
606
607	MAYHAVE(val64, "mtu", 0);
608	if (val < 0 || val64 > 0xffffffff) {
609		syslog(LOG_ERR,
610		    "<%s> mtu (%" PRIu64 ") on %s out of range",
611		    __func__, val64, ifi->ifi_ifname);
612		goto getconfig_free_rai;
613	}
614	rai->rai_linkmtu = (uint32_t)val64;
615	if (rai->rai_linkmtu == 0) {
616		char *mtustr;
617
618		if ((mtustr = (char *)agetstr("mtu", &bp)) &&
619		    strcmp(mtustr, "auto") == 0)
620			rai->rai_linkmtu = ifi->ifi_phymtu;
621	}
622	else if (rai->rai_linkmtu < IPV6_MMTU ||
623	    rai->rai_linkmtu > ifi->ifi_phymtu) {
624		syslog(LOG_ERR,
625		    "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must "
626		    "be between least MTU (%d) and physical link MTU (%d)",
627		    __func__, rai->rai_linkmtu, ifi->ifi_ifname,
628		    IPV6_MMTU, ifi->ifi_phymtu);
629		goto getconfig_free_rai;
630	}
631
632#ifdef SIOCSIFINFO_IN6
633	{
634		struct in6_ndireq ndi;
635		int s;
636
637		if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
638			syslog(LOG_ERR, "<%s> socket: %s", __func__,
639			    strerror(errno));
640			exit(1);
641		}
642		memset(&ndi, 0, sizeof(ndi));
643		strlcpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname));
644		if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0)
645			syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
646			    __func__, ifi->ifi_ifname, strerror(errno));
647
648		/* reflect the RA info to the host variables in kernel */
649		ndi.ndi.chlim = rai->rai_hoplimit;
650		ndi.ndi.retrans = rai->rai_retranstimer;
651		ndi.ndi.basereachable = rai->rai_reachabletime;
652		if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0)
653			syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
654			    __func__, ifi->ifi_ifname, strerror(errno));
655
656		close(s);
657	}
658#endif
659
660	/* route information */
661	rai->rai_routes = 0;
662	for (i = -1; i < MAXROUTE; i++) {
663		struct rtinfo *rti;
664
665		makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
666		addr = (char *)agetstr(entbuf, &bp);
667		if (addr == NULL) {
668			makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
669			addr = (char *)agetstr(oentbuf, &bp);
670			if (addr)
671				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
672				    oentbuf, entbuf);
673		}
674		if (addr == NULL)
675			continue;
676
677		/* allocate memory to store prefix information */
678		ELM_MALLOC(rti, exit(1));
679
680		if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) {
681			syslog(LOG_ERR, "<%s> inet_pton failed for %s",
682			    __func__, addr);
683			goto getconfig_free_rti;
684		}
685#if 0
686		/*
687		 * XXX: currently there's no restriction in route information
688		 * prefix according to
689		 * draft-ietf-ipngwg-router-selection-00.txt.
690		 * However, I think the similar restriction be necessary.
691		 */
692		MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
693		if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
694			syslog(LOG_ERR,
695			    "<%s> multicast route (%s) must "
696			    "not be advertised on %s",
697			    __func__, addr, ifi->ifi_ifname);
698			goto getconfig_free_rti;
699		}
700		if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
701			syslog(LOG_NOTICE,
702			    "<%s> link-local route (%s) will "
703			    "be advertised on %s",
704			    __func__, addr, ifi->ifi_ifname);
705			goto getconfig_free_rti;
706		}
707#endif
708
709		makeentry(entbuf, sizeof(entbuf), i, "rtplen");
710		/* XXX: 256 is a magic number for compatibility check. */
711		MAYHAVE(val, entbuf, 256);
712		if (val == 256) {
713			makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
714			MAYHAVE(val, oentbuf, 256);
715			if (val != 256)
716				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
717				    oentbuf, entbuf);
718			else
719				val = 64;
720		}
721		if (val < 0 || val > 128) {
722			syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s "
723			    "out of range",
724			    __func__, val, addr, ifi->ifi_ifname);
725			goto getconfig_free_rti;
726		}
727		rti->rti_prefixlen = (int)val;
728
729		makeentry(entbuf, sizeof(entbuf), i, "rtflags");
730		if ((flagstr = (char *)agetstr(entbuf, &bp))) {
731			val = 0;
732			if (strchr(flagstr, 'h'))
733				val |= ND_RA_FLAG_RTPREF_HIGH;
734			if (strchr(flagstr, 'l')) {
735				if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
736					syslog(LOG_ERR,
737					    "<%s> the \'h\' and \'l\' route"
738					    " preferences are exclusive",
739					    __func__);
740					goto getconfig_free_rti;
741				}
742				val |= ND_RA_FLAG_RTPREF_LOW;
743			}
744		} else
745			MAYHAVE(val, entbuf, 256); /* XXX */
746		if (val == 256) {
747			makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
748			MAYHAVE(val, oentbuf, 256);
749			if (val != 256) {
750				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
751				    oentbuf, entbuf);
752			} else
753				val = 0;
754		}
755		rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
756		if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) {
757			syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
758			    "for %s/%d on %s",
759			    __func__, rti->rti_rtpref, addr,
760			    rti->rti_prefixlen, ifi->ifi_ifname);
761			goto getconfig_free_rti;
762		}
763
764		/*
765		 * Since the spec does not a default value, we should make
766		 * this entry mandatory.  However, FreeBSD 4.4 has shipped
767		 * with this field being optional, we use the router lifetime
768		 * as an ad-hoc default value with a warning message.
769		 */
770		makeentry(entbuf, sizeof(entbuf), i, "rtltime");
771		MAYHAVE(val64, entbuf, -1);
772		if (val64 == -1) {
773			makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
774			MAYHAVE(val64, oentbuf, -1);
775			if (val64 != -1)
776				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
777				    oentbuf, entbuf);
778			else {
779				fprintf(stderr, "%s should be specified "
780				    "for interface %s.\n", entbuf,
781				    ifi->ifi_ifname);
782				val64 = rai->rai_lifetime;
783			}
784		}
785		if (val64 < 0 || val64 > 0xffffffff) {
786			syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for "
787			    "%s/%d on %s out of range", __func__,
788			    val64, addr, rti->rti_prefixlen,
789			    ifi->ifi_ifname);
790			goto getconfig_free_rti;
791		}
792		rti->rti_ltime = (uint32_t)val64;
793
794		/* link into chain */
795		TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next);
796		rai->rai_routes++;
797		continue;
798getconfig_free_rti:
799		free(rti);
800	}
801
802	/* DNS server and DNS search list information */
803	for (i = -1; i < MAXRDNSSENT ; i++) {
804		struct rdnss *rdn;
805		struct rdnss_addr *rdna;
806		char *ap;
807		int c;
808
809		makeentry(entbuf, sizeof(entbuf), i, "rdnss");
810		addr = (char *)agetstr(entbuf, &bp);
811		if (addr == NULL)
812			continue;
813		ELM_MALLOC(rdn, exit(1));
814
815		TAILQ_INIT(&rdn->rd_list);
816
817		for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
818			c = strcspn(ap, ",");
819			strncpy(abuf, ap, c);
820			abuf[c] = '\0';
821			ELM_MALLOC(rdna, goto getconfig_free_rdn);
822			if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) {
823				syslog(LOG_ERR, "<%s> inet_pton failed for %s",
824				    __func__, abuf);
825				free(rdna);
826				goto getconfig_free_rdn;
827			}
828			TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next);
829		}
830
831		makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
832		MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
833		if ((uint16_t)val < rai->rai_maxinterval ||
834		    (uint16_t)val > rai->rai_maxinterval * 2) {
835			syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
836			    "(must be between %d and %d)",
837			    entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
838			    rai->rai_maxinterval * 2);
839			goto getconfig_free_rdn;
840		}
841		rdn->rd_ltime = val;
842
843		/* link into chain */
844		TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next);
845		continue;
846getconfig_free_rdn:
847		while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
848			TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
849			free(rdna);
850		}
851		free(rdn);
852	}
853
854	for (i = -1; i < MAXDNSSLENT ; i++) {
855		struct dnssl *dns;
856		struct dnssl_addr *dnsa;
857		char *ap;
858		int c;
859
860		makeentry(entbuf, sizeof(entbuf), i, "dnssl");
861		addr = (char *)agetstr(entbuf, &bp);
862		if (addr == NULL)
863			continue;
864
865		ELM_MALLOC(dns, exit(1));
866
867		TAILQ_INIT(&dns->dn_list);
868
869		for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
870			c = strcspn(ap, ",");
871			strncpy(abuf, ap, c);
872			abuf[c] = '\0';
873			ELM_MALLOC(dnsa, goto getconfig_free_dns);
874			dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf);
875			if (dnsa->da_len < 0) {
876				syslog(LOG_ERR, "Invalid dnssl entry: %s",
877				    abuf);
878				goto getconfig_free_dns;
879			}
880			syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__,
881			    dnsa->da_len);
882			TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next);
883		}
884
885		makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
886		MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
887		if ((uint16_t)val < rai->rai_maxinterval ||
888		    (uint16_t)val > rai->rai_maxinterval * 2) {
889			syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
890			    "(must be between %d and %d)",
891			    entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
892			    rai->rai_maxinterval * 2);
893			goto getconfig_free_dns;
894		}
895		dns->dn_ltime = val;
896
897		/* link into chain */
898		TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next);
899		continue;
900getconfig_free_dns:
901		while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) {
902			TAILQ_REMOVE(&dns->dn_list, dnsa, da_next);
903			free(dnsa);
904		}
905		free(dns);
906	}
907	/* construct the sending packet */
908	make_packet(rai);
909
910	/*
911	 * If an entry with the same ifindex exists, remove it first.
912	 * Before the removal, RDNSS and DNSSL options with
913	 * zero-lifetime will be sent.
914	 */
915	switch (ifi->ifi_state) {
916	case IFI_STATE_UNCONFIGURED:
917		/* UNCONFIGURED -> TRANSITIVE */
918
919		error = sock_mc_join(&sock, ifi->ifi_ifindex);
920		if (error)
921			exit(1);
922
923		ifi->ifi_state = IFI_STATE_TRANSITIVE;
924		ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS;
925		ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
926
927		/* The same two rai mean initial burst */
928		ifi->ifi_rainfo = rai;
929		ifi->ifi_rainfo_trans = rai;
930		TAILQ_INSERT_TAIL(&railist, rai, rai_next);
931
932		if (ifi->ifi_ra_timer == NULL)
933			ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
934			    ra_timer_update, ifi, ifi);
935		ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
936		rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
937		    ifi->ifi_ra_timer);
938
939		syslog(LOG_DEBUG,
940		    "<%s> ifname=%s marked as TRANSITIVE (initial burst).",
941		    __func__, ifi->ifi_ifname);
942		break;
943	case IFI_STATE_CONFIGURED:
944		/* CONFIGURED -> TRANSITIVE */
945		rai_old = ifi->ifi_rainfo;
946		if (rai_old == NULL) {
947			syslog(LOG_ERR,
948			    "<%s> ifi_rainfo is NULL"
949			    " in IFI_STATE_CONFIGURED.", __func__);
950			ifi = NULL;
951			break;
952		} else {
953			struct rdnss *rdn;
954			struct dnssl *dns;
955
956			rai_old->rai_lifetime = 0;
957			TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next)
958			    rdn->rd_ltime = 0;
959			TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next)
960			    dns->dn_ltime = 0;
961
962			ifi->ifi_rainfo_trans = rai_old;
963			ifi->ifi_state = IFI_STATE_TRANSITIVE;
964			ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
965			ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
966
967			ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
968			rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
969			    ifi->ifi_ra_timer);
970
971			syslog(LOG_DEBUG,
972			    "<%s> ifname=%s marked as TRANSITIVE"
973			    " (transitional burst)",
974			    __func__, ifi->ifi_ifname);
975		}
976		ifi->ifi_rainfo = rai;
977		TAILQ_INSERT_TAIL(&railist, rai, rai_next);
978		break;
979	case IFI_STATE_TRANSITIVE:
980		if (ifi->ifi_rainfo != NULL) {
981			if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
982				/* Reinitialize initial burst */
983				rm_rainfo(ifi->ifi_rainfo);
984				ifi->ifi_rainfo = rai;
985				ifi->ifi_rainfo_trans = rai;
986				ifi->ifi_burstcount =
987				    MAX_INITIAL_RTR_ADVERTISEMENTS;
988				ifi->ifi_burstinterval =
989				    MAX_INITIAL_RTR_ADVERT_INTERVAL;
990			} else {
991				/* Replace ifi_rainfo with the new one */
992				rm_rainfo(ifi->ifi_rainfo);
993				ifi->ifi_rainfo = rai;
994			}
995			TAILQ_INSERT_TAIL(&railist, rai, rai_next);
996
997			ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
998			rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
999			    ifi->ifi_ra_timer);
1000		} else {
1001			/* XXX: NOTREACHED.  Being shut down. */
1002			syslog(LOG_ERR,
1003			    "<%s> %s is shutting down.  Skipped.",
1004			    __func__, ifi->ifi_ifname);
1005			rm_rainfo(rai);
1006
1007			return (NULL);
1008		}
1009		break;
1010	}
1011
1012	return (ifi);
1013
1014getconfig_free_rai:
1015	free(rai);
1016	return (NULL);
1017}
1018
1019void
1020get_prefix(struct rainfo *rai)
1021{
1022	struct ifaddrs *ifap, *ifa;
1023	struct prefix *pfx;
1024	struct in6_addr *a;
1025	struct ifinfo *ifi;
1026	char *p, *ep, *m, *lim;
1027	char ntopbuf[INET6_ADDRSTRLEN];
1028
1029	if (getifaddrs(&ifap) < 0) {
1030		syslog(LOG_ERR,
1031		    "<%s> can't get interface addresses",
1032		    __func__);
1033		exit(1);
1034	}
1035	ifi = rai->rai_ifinfo;
1036
1037	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1038		int plen;
1039
1040		if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0)
1041			continue;
1042		if (ifa->ifa_addr->sa_family != AF_INET6)
1043			continue;
1044		a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1045		if (IN6_IS_ADDR_LINKLOCAL(a))
1046			continue;
1047
1048		/* get prefix length */
1049		m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1050		lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
1051		plen = prefixlen(m, lim);
1052		if (plen <= 0 || plen > 128) {
1053			syslog(LOG_ERR, "<%s> failed to get prefixlen "
1054			    "or prefix is invalid",
1055			    __func__);
1056			exit(1);
1057		}
1058		if (plen == 128)	/* XXX */
1059			continue;
1060		if (find_prefix(rai, a, plen)) {
1061			/* ignore a duplicated prefix. */
1062			continue;
1063		}
1064
1065		/* allocate memory to store prefix info. */
1066		ELM_MALLOC(pfx, exit(1));
1067
1068		/* set prefix, sweep bits outside of prefixlen */
1069		pfx->pfx_prefixlen = plen;
1070		memcpy(&pfx->pfx_prefix, a, sizeof(*a));
1071		p = (char *)&pfx->pfx_prefix;
1072		ep = (char *)(&pfx->pfx_prefix + 1);
1073		while (m < lim && p < ep)
1074			*p++ &= *m++;
1075		while (p < ep)
1076			*p++ = 0x00;
1077	        if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1078	            sizeof(ntopbuf))) {
1079			syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
1080			exit(1);
1081		}
1082		syslog(LOG_DEBUG,
1083		    "<%s> add %s/%d to prefix list on %s",
1084		    __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname);
1085
1086		/* set other fields with protocol defaults */
1087		pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME;
1088		pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME;
1089		pfx->pfx_onlinkflg = 1;
1090		pfx->pfx_autoconfflg = 1;
1091		pfx->pfx_origin = PREFIX_FROM_KERNEL;
1092		pfx->pfx_rainfo = rai;
1093
1094		/* link into chain */
1095		TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1096
1097		/* counter increment */
1098		rai->rai_pfxs++;
1099	}
1100
1101	freeifaddrs(ifap);
1102}
1103
1104static void
1105makeentry(char *buf, size_t len, int id, const char *string)
1106{
1107
1108	if (id < 0)
1109		strlcpy(buf, string, len);
1110	else
1111		snprintf(buf, len, "%s%d", string, id);
1112}
1113
1114/*
1115 * Add a prefix to the list of specified interface and reconstruct
1116 * the outgoing packet.
1117 * The prefix must not be in the list.
1118 * XXX: other parameters of the prefix (e.g. lifetime) should be
1119 * able to be specified.
1120 */
1121static void
1122add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
1123{
1124	struct prefix *pfx;
1125	struct ifinfo *ifi;
1126	char ntopbuf[INET6_ADDRSTRLEN];
1127
1128	ifi = rai->rai_ifinfo;
1129	ELM_MALLOC(pfx, return);
1130	pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr;
1131	pfx->pfx_prefixlen = ipr->ipr_plen;
1132	pfx->pfx_validlifetime = ipr->ipr_vltime;
1133	pfx->pfx_preflifetime = ipr->ipr_pltime;
1134	pfx->pfx_onlinkflg = ipr->ipr_raf_onlink;
1135	pfx->pfx_autoconfflg = ipr->ipr_raf_auto;
1136	pfx->pfx_origin = PREFIX_FROM_DYNAMIC;
1137	pfx->pfx_rainfo = rai;
1138
1139	TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1140
1141	syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
1142	    __func__,
1143	    inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1144		sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname);
1145
1146	rai->rai_pfxs++;
1147}
1148
1149/*
1150 * Delete a prefix to the list of specified interface and reconstruct
1151 * the outgoing packet.
1152 * The prefix must be in the list.
1153 */
1154void
1155delete_prefix(struct prefix *pfx)
1156{
1157	struct rainfo *rai;
1158	struct ifinfo *ifi;
1159	char ntopbuf[INET6_ADDRSTRLEN];
1160
1161	rai = pfx->pfx_rainfo;
1162	ifi = rai->rai_ifinfo;
1163	TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next);
1164	syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
1165	    __func__,
1166	    inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1167		sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1168	if (pfx->pfx_timer)
1169		rtadvd_remove_timer(pfx->pfx_timer);
1170	free(pfx);
1171
1172	rai->rai_pfxs--;
1173}
1174
1175void
1176invalidate_prefix(struct prefix *pfx)
1177{
1178	struct timespec timo;
1179	struct rainfo *rai;
1180	struct ifinfo *ifi;
1181	char ntopbuf[INET6_ADDRSTRLEN];
1182
1183	rai = pfx->pfx_rainfo;
1184	ifi = rai->rai_ifinfo;
1185	if (pfx->pfx_timer) {	/* sanity check */
1186		syslog(LOG_ERR,
1187		    "<%s> assumption failure: timer already exists",
1188		    __func__);
1189		exit(1);
1190	}
1191
1192	syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
1193	    "will expire in %ld seconds", __func__,
1194	    inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)),
1195	    pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo);
1196
1197	/* set the expiration timer */
1198	pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL);
1199	if (pfx->pfx_timer == NULL) {
1200		syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
1201		    "remove the prefix", __func__);
1202		delete_prefix(pfx);
1203	}
1204	timo.tv_sec = prefix_timo;
1205	timo.tv_nsec = 0;
1206	rtadvd_set_timer(&timo, pfx->pfx_timer);
1207}
1208
1209static struct rtadvd_timer *
1210prefix_timeout(void *arg)
1211{
1212
1213	delete_prefix((struct prefix *)arg);
1214
1215	return (NULL);
1216}
1217
1218void
1219update_prefix(struct prefix *pfx)
1220{
1221	struct rainfo *rai;
1222	struct ifinfo *ifi;
1223	char ntopbuf[INET6_ADDRSTRLEN];
1224
1225	rai = pfx->pfx_rainfo;
1226	ifi = rai->rai_ifinfo;
1227	if (pfx->pfx_timer == NULL) { /* sanity check */
1228		syslog(LOG_ERR,
1229		    "<%s> assumption failure: timer does not exist",
1230		    __func__);
1231		exit(1);
1232	}
1233
1234	syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1235	    __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1236		sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1237
1238	/* stop the expiration timer */
1239	rtadvd_remove_timer(pfx->pfx_timer);
1240	pfx->pfx_timer = NULL;
1241}
1242
1243/*
1244 * Try to get an in6_prefixreq contents for a prefix which matches
1245 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1246 * the interface whose name is ipr->ipr_name[].
1247 */
1248static int
1249init_prefix(struct in6_prefixreq *ipr)
1250{
1251#if 0
1252	int s;
1253
1254	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1255		syslog(LOG_ERR, "<%s> socket: %s", __func__,
1256		    strerror(errno));
1257		exit(1);
1258	}
1259
1260	if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
1261		syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
1262		    strerror(errno));
1263
1264		ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1265		ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1266		ipr->ipr_raf_onlink = 1;
1267		ipr->ipr_raf_auto = 1;
1268		/* omit other field initialization */
1269	}
1270	else if (ipr->ipr_origin < PR_ORIG_RR) {
1271		char ntopbuf[INET6_ADDRSTRLEN];
1272
1273		syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1274		    "lower than PR_ORIG_RR(router renumbering)."
1275		    "This should not happen if I am router", __func__,
1276		    inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1277			sizeof(ntopbuf)), ipr->ipr_origin);
1278		close(s);
1279		return (1);
1280	}
1281
1282	close(s);
1283	return (0);
1284#else
1285	ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1286	ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1287	ipr->ipr_raf_onlink = 1;
1288	ipr->ipr_raf_auto = 1;
1289	return (0);
1290#endif
1291}
1292
1293void
1294make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1295{
1296	struct in6_prefixreq ipr;
1297
1298	memset(&ipr, 0, sizeof(ipr));
1299	if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1300		syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't "
1301		    "exist. This should not happen! %s", __func__,
1302		    ifindex, strerror(errno));
1303		exit(1);
1304	}
1305	ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1306	ipr.ipr_prefix.sin6_family = AF_INET6;
1307	ipr.ipr_prefix.sin6_addr = *addr;
1308	ipr.ipr_plen = plen;
1309
1310	if (init_prefix(&ipr))
1311		return; /* init failed by some error */
1312	add_prefix(rai, &ipr);
1313}
1314
1315void
1316make_packet(struct rainfo *rai)
1317{
1318	size_t packlen, lladdroptlen = 0;
1319	char *buf;
1320	struct nd_router_advert *ra;
1321	struct nd_opt_prefix_info *ndopt_pi;
1322	struct nd_opt_mtu *ndopt_mtu;
1323	struct nd_opt_route_info *ndopt_rti;
1324	struct rtinfo *rti;
1325	struct nd_opt_rdnss *ndopt_rdnss;
1326	struct rdnss *rdn;
1327	struct nd_opt_dnssl *ndopt_dnssl;
1328	struct dnssl *dns;
1329	size_t len;
1330	struct prefix *pfx;
1331	struct ifinfo *ifi;
1332
1333	ifi = rai->rai_ifinfo;
1334	/* calculate total length */
1335	packlen = sizeof(struct nd_router_advert);
1336	if (rai->rai_advlinkopt) {
1337		if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) {
1338			syslog(LOG_INFO,
1339			    "<%s> link-layer address option has"
1340			    " null length on %s.  Treat as not included.",
1341			    __func__, ifi->ifi_ifname);
1342			rai->rai_advlinkopt = 0;
1343		}
1344		packlen += lladdroptlen;
1345	}
1346	if (rai->rai_pfxs)
1347		packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs;
1348	if (rai->rai_linkmtu)
1349		packlen += sizeof(struct nd_opt_mtu);
1350
1351	TAILQ_FOREACH(rti, &rai->rai_route, rti_next)
1352		packlen += sizeof(struct nd_opt_route_info) +
1353			   ((rti->rti_prefixlen + 0x3f) >> 6) * 8;
1354
1355	TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1356		struct rdnss_addr *rdna;
1357
1358		packlen += sizeof(struct nd_opt_rdnss);
1359		TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next)
1360			packlen += sizeof(rdna->ra_dns);
1361	}
1362	TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1363		struct dnssl_addr *dnsa;
1364
1365		packlen += sizeof(struct nd_opt_dnssl);
1366		len = 0;
1367		TAILQ_FOREACH(dnsa, &dns->dn_list, da_next)
1368			len += dnsa->da_len;
1369
1370		/* A zero octet and 8 octet boundary */
1371		len++;
1372		len += (len % 8) ? 8 - len % 8 : 0;
1373
1374		packlen += len;
1375	}
1376	/* allocate memory for the packet */
1377	if ((buf = malloc(packlen)) == NULL) {
1378		syslog(LOG_ERR,
1379		    "<%s> can't get enough memory for an RA packet",
1380		    __func__);
1381		exit(1);
1382	}
1383	memset(buf, 0, packlen);
1384	if (rai->rai_ra_data)	/* Free old data if any. */
1385		free(rai->rai_ra_data);
1386	rai->rai_ra_data = buf;
1387	/* XXX: what if packlen > 576? */
1388	rai->rai_ra_datalen = packlen;
1389
1390	/*
1391	 * construct the packet
1392	 */
1393	ra = (struct nd_router_advert *)buf;
1394	ra->nd_ra_type = ND_ROUTER_ADVERT;
1395	ra->nd_ra_code = 0;
1396	ra->nd_ra_cksum = 0;
1397	ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit);
1398	ra->nd_ra_flags_reserved = 0; /* just in case */
1399	/*
1400	 * XXX: the router preference field, which is a 2-bit field, should be
1401	 * initialized before other fields.
1402	 */
1403	ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref;
1404	ra->nd_ra_flags_reserved |=
1405		rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0;
1406	ra->nd_ra_flags_reserved |=
1407		rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0;
1408	ra->nd_ra_router_lifetime = htons(rai->rai_lifetime);
1409	ra->nd_ra_reachable = htonl(rai->rai_reachabletime);
1410	ra->nd_ra_retransmit = htonl(rai->rai_retranstimer);
1411	buf += sizeof(*ra);
1412
1413	if (rai->rai_advlinkopt) {
1414		lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf);
1415		buf += lladdroptlen;
1416	}
1417
1418	if (rai->rai_linkmtu) {
1419		ndopt_mtu = (struct nd_opt_mtu *)buf;
1420		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1421		ndopt_mtu->nd_opt_mtu_len = 1;
1422		ndopt_mtu->nd_opt_mtu_reserved = 0;
1423		ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu);
1424		buf += sizeof(struct nd_opt_mtu);
1425	}
1426
1427	TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1428		uint32_t vltime, pltime;
1429		struct timespec now;
1430
1431		ndopt_pi = (struct nd_opt_prefix_info *)buf;
1432		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1433		ndopt_pi->nd_opt_pi_len = 4;
1434		ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen;
1435		ndopt_pi->nd_opt_pi_flags_reserved = 0;
1436		if (pfx->pfx_onlinkflg)
1437			ndopt_pi->nd_opt_pi_flags_reserved |=
1438				ND_OPT_PI_FLAG_ONLINK;
1439		if (pfx->pfx_autoconfflg)
1440			ndopt_pi->nd_opt_pi_flags_reserved |=
1441				ND_OPT_PI_FLAG_AUTO;
1442		if (pfx->pfx_timer)
1443			vltime = 0;
1444		else {
1445			if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire)
1446				clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1447			if (pfx->pfx_vltimeexpire == 0)
1448				vltime = pfx->pfx_validlifetime;
1449			else
1450				vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ?
1451				    pfx->pfx_vltimeexpire - now.tv_sec : 0;
1452		}
1453		if (pfx->pfx_timer)
1454			pltime = 0;
1455		else {
1456			if (pfx->pfx_pltimeexpire == 0)
1457				pltime = pfx->pfx_preflifetime;
1458			else
1459				pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ?
1460				    pfx->pfx_pltimeexpire - now.tv_sec : 0;
1461		}
1462		if (vltime < pltime) {
1463			/*
1464			 * this can happen if vltime is decrement but pltime
1465			 * is not.
1466			 */
1467			pltime = vltime;
1468		}
1469		ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1470		ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1471		ndopt_pi->nd_opt_pi_reserved2 = 0;
1472		ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix;
1473
1474		buf += sizeof(struct nd_opt_prefix_info);
1475	}
1476
1477	TAILQ_FOREACH(rti, &rai->rai_route, rti_next) {
1478		uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6;
1479
1480		ndopt_rti = (struct nd_opt_route_info *)buf;
1481		ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1482		ndopt_rti->nd_opt_rti_len = 1 + psize;
1483		ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen;
1484		ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref;
1485		ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime);
1486		memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8);
1487		buf += sizeof(struct nd_opt_route_info) + psize * 8;
1488	}
1489
1490	TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1491		struct rdnss_addr *rdna;
1492
1493		ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1494		ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1495		ndopt_rdnss->nd_opt_rdnss_len = 0;
1496		ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1497		ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime);
1498		buf += sizeof(struct nd_opt_rdnss);
1499
1500		TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) {
1501			memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns));
1502			buf += sizeof(rdna->ra_dns);
1503		}
1504		/* Length field should be in 8 octets */
1505		ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8;
1506
1507		syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__,
1508		    ndopt_rdnss->nd_opt_rdnss_len);
1509	}
1510
1511	TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1512		struct dnssl_addr *dnsa;
1513
1514		ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1515		ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1516		ndopt_dnssl->nd_opt_dnssl_len = 0;
1517		ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1518		ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime);
1519		buf += sizeof(*ndopt_dnssl);
1520
1521		TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) {
1522			memcpy(buf, dnsa->da_dom, dnsa->da_len);
1523			buf += dnsa->da_len;
1524		}
1525
1526		/* A zero octet after encoded DNS server list. */
1527		*buf++ = '\0';
1528
1529		/* Padding to next 8 octets boundary */
1530		len = buf - (char *)ndopt_dnssl;
1531		len += (len % 8) ? 8 - len % 8 : 0;
1532		buf = (char *)ndopt_dnssl + len;
1533
1534		/* Length field must be in 8 octets */
1535		ndopt_dnssl->nd_opt_dnssl_len = len / 8;
1536
1537		syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__,
1538		    ndopt_dnssl->nd_opt_dnssl_len);
1539	}
1540	return;
1541}
1542