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