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