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