xref: /netbsd-src/usr.sbin/rtadvd/config.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: config.c,v 1.43 2019/11/11 13:42:49 roy Exp $	*/
2 /*	$KAME: config.c,v 1.93 2005/10/17 14:40:02 suz Exp $	*/
3 
4 /*
5  * Copyright (C) 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <sys/sysctl.h>
38 
39 #include <net/if.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
42 #ifdef __FreeBSD__
43 #include <net/if_var.h>
44 #endif
45 
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 #include <netinet/ip6.h>
49 #include <netinet6/ip6_var.h>
50 #include <netinet/icmp6.h>
51 #include <netinet6/nd6.h>
52 
53 #include <arpa/inet.h>
54 
55 #include <stdio.h>
56 #include <syslog.h>
57 #include <errno.h>
58 #include <string.h>
59 #include <stdlib.h>
60 #include <search.h>
61 #include <unistd.h>
62 #include <ifaddrs.h>
63 #include <inttypes.h>
64 
65 #include "rtadvd.h"
66 #include "advcap.h"
67 #include "timer.h"
68 #include "if.h"
69 #include "config.h"
70 #include "logit.h"
71 #include "prog_ops.h"
72 
73 #ifndef __arraycount
74 #define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
75 #endif
76 
77 static time_t prefix_timo = (60 * 120);	/* 2 hours.
78 					 * XXX: should be configurable. */
79 static struct rtadvd_timer *prefix_timeout(void *);
80 static void makeentry(char *, size_t, int, const char *);
81 static int getinet6sysctl(int);
82 
83 static size_t
84 encode_domain(char *dst, const char *src)
85 {
86 	ssize_t len;
87 	char *odst, *p;
88 
89 	odst = dst;
90 	while (src && (len = strlen(src)) != 0) {
91 		p = strchr(src, '.');
92 		*dst++ = len = MIN(63, p == NULL ? len : p - src);
93 		memcpy(dst, src, len);
94 		dst += len;
95 		if (p == NULL)
96 			break;
97 		src = p + 1;
98 	}
99 	*dst++ = '\0';
100 
101 	return dst - odst;
102 }
103 
104 void
105 free_rainfo(struct rainfo *rai)
106 {
107 	struct soliciter *sol;
108 	struct prefix *pfx;
109 	struct rtinfo *rti;
110 	struct rdnss *rdnss;
111 	struct rdnss_addr *rdnsa;
112 	struct dnssl *dnssl;
113 	struct dnssl_domain *dnsd;
114 
115 	rtadvd_remove_timer(&rai->timer);
116 	rtadvd_remove_timer(&rai->timer_sol);
117 
118 	while ((sol = TAILQ_FIRST(&rai->soliciter))) {
119 		TAILQ_REMOVE(&rai->soliciter, sol, next);
120 		free(sol);
121 	}
122 
123 	while ((pfx = TAILQ_FIRST(&rai->prefix))) {
124 		TAILQ_REMOVE(&rai->prefix, pfx, next);
125 		free(pfx);
126 	}
127 
128 	while ((rti = TAILQ_FIRST(&rai->route))) {
129 		TAILQ_REMOVE(&rai->route, rti, next);
130 		free(rti);
131 	}
132 
133 	while ((rdnss = TAILQ_FIRST(&rai->rdnss))) {
134 		TAILQ_REMOVE(&rai->rdnss, rdnss, next);
135 		while ((rdnsa = TAILQ_FIRST(&rdnss->list))) {
136 			TAILQ_REMOVE(&rdnss->list, rdnsa, next);
137 			free(rdnsa);
138 		}
139 		free(rdnss);
140 	}
141 
142 	while ((dnssl = TAILQ_FIRST(&rai->dnssl))) {
143 		TAILQ_REMOVE(&rai->dnssl, dnssl, next);
144 		while ((dnsd = TAILQ_FIRST(&dnssl->list))) {
145 			TAILQ_REMOVE(&dnssl->list, dnsd, next);
146 			free(dnsd);
147 		}
148 		free(dnssl);
149 	}
150 
151 	free(rai->sdl);
152 	free(rai->ra_data);
153 	free(rai);
154 }
155 
156 void
157 getconfig(const char *intface, int exithard)
158 {
159 	int stat, c, i;
160 	char tbuf[BUFSIZ];
161 	struct rainfo *tmp, *rai;
162 	int32_t val;
163 	int64_t val64;
164 	char buf[BUFSIZ];
165 	char *bp = buf;
166 	char *addr, *flagstr, *ap;
167 	static int forwarding = -1;
168 	char entbuf[256], abuf[256];
169 	struct rdnss *rdnss;
170 	struct dnssl *dnssl;
171 
172 #define MUSTHAVE(var, cap)	\
173     do {								\
174 	int64_t t;							\
175 	if ((t = agetnum(cap)) < 0) {					\
176 		fprintf(stderr, "rtadvd: need %s for interface %s\n",	\
177 			cap, intface);					\
178 		goto errexit;						\
179 	}								\
180 	var = t;							\
181      } while (0)
182 #define MAYHAVE(var, cap, def)	\
183      do {								\
184 	if ((var = agetnum(cap)) < 0)					\
185 		var = def;						\
186      } while (0)
187 #define	ELM_MALLOC(p)					\
188 	do {								\
189 		p = calloc(1, sizeof(*p));				\
190 		if (p == NULL) {					\
191 			logit(LOG_ERR, "<%s> calloc failed: %m",	\
192 			    __func__);					\
193 			goto errexit;					\
194 		}							\
195 	} while(/*CONSTCOND*/0)
196 
197 	if (if_nametoindex(intface) == 0) {
198 		logit(LOG_INFO, "<%s> interface %s not found, ignoring",
199 		       __func__, intface);
200 		return;
201 	}
202 
203 	logit(LOG_DEBUG, "<%s> loading configuration for interface %s",
204 	       __func__, intface);
205 
206 	if ((stat = agetent(tbuf, intface)) <= 0) {
207 		memset(tbuf, 0, sizeof(tbuf));
208 		logit(LOG_INFO,
209 		       "<%s> %s isn't defined in the configuration file"
210 		       " or the configuration file doesn't exist."
211 		       " Treat it as default",
212 		        __func__, intface);
213 	}
214 
215 	ELM_MALLOC(tmp);
216 	TAILQ_INIT(&tmp->soliciter);
217 	TAILQ_INIT(&tmp->prefix);
218 	TAILQ_INIT(&tmp->route);
219 	TAILQ_INIT(&tmp->rdnss);
220 	TAILQ_INIT(&tmp->dnssl);
221 
222 	/* check if we are allowed to forward packets (if not determined) */
223 	if (forwarding < 0) {
224 		if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
225 			exit(1);
226 	}
227 
228 	/* get interface information */
229 	if (agetflag("nolladdr"))
230 		tmp->advlinkopt = 0;
231 	else
232 		tmp->advlinkopt = 1;
233 	if (tmp->advlinkopt) {
234 		if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
235 			logit(LOG_ERR,
236 			       "<%s> can't get information of %s",
237 			       __func__, intface);
238 			goto errexit;
239 		}
240 		tmp->ifindex = tmp->sdl->sdl_index;
241 	} else {
242 		tmp->ifindex = if_nametoindex(intface);
243 		if (tmp->ifindex == 0) {
244 			logit(LOG_ERR,
245 			       "<%s> can't get information of %s",
246 			       __func__, intface);
247 			goto errexit;
248 		}
249 	}
250 	tmp->ifflags = if_getflags(tmp->ifindex, 0);
251 	strlcpy(tmp->ifname, intface, sizeof(tmp->ifname));
252 	if ((tmp->phymtu = if_getmtu(intface)) == 0) {
253 		tmp->phymtu = IPV6_MMTU;
254 		logit(LOG_WARNING,
255 		       "<%s> can't get interface mtu of %s. Treat as %d",
256 		       __func__, intface, IPV6_MMTU);
257 	}
258 
259 	/*
260 	 * set router configuration variables.
261 	 */
262 	MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
263 	if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
264 		logit(LOG_ERR,
265 		       "<%s> maxinterval (%d) on %s is invalid "
266 		       "(must be between %u and %u)", __func__, val,
267 		       intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
268 		goto errexit;
269 	}
270 	tmp->maxinterval = val;
271 	MAYHAVE(val, "mininterval", tmp->maxinterval/3);
272 	if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
273 		logit(LOG_ERR,
274 		       "<%s> mininterval (%d) on %s is invalid "
275 		       "(must be between %u and %d)",
276 		       __func__, val, intface, MIN_MININTERVAL,
277 		       (tmp->maxinterval * 3) / 4);
278 		goto errexit;
279 	}
280 	tmp->mininterval = val;
281 
282 	MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
283 	tmp->hoplimit = val & 0xff;
284 
285 	if ((flagstr = (char *)agetstr("raflags", &bp))) {
286 		val = 0;
287 		if (strchr(flagstr, 'm'))
288 			val |= ND_RA_FLAG_MANAGED;
289 		if (strchr(flagstr, 'o'))
290 			val |= ND_RA_FLAG_OTHER;
291 		if (strchr(flagstr, 'h'))
292 			val |= ND_RA_FLAG_RTPREF_HIGH;
293 		if (strchr(flagstr, 'l')) {
294 			if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
295 				logit(LOG_ERR, "<%s> the \'h\' and \'l\'"
296 				    " router flags are exclusive", __func__);
297 				goto errexit;
298 			}
299 			val |= ND_RA_FLAG_RTPREF_LOW;
300 		}
301 	} else {
302 		MAYHAVE(val, "raflags", 0);
303 	}
304 	tmp->managedflg = val & ND_RA_FLAG_MANAGED;
305 	tmp->otherflg = val & ND_RA_FLAG_OTHER;
306 #ifndef ND_RA_FLAG_RTPREF_MASK
307 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
308 #define ND_RA_FLAG_RTPREF_RSV	0x10 /* 00010000 */
309 #endif
310 	tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
311 	if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
312 		logit(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
313 		       __func__, tmp->rtpref, intface);
314 		goto errexit;
315 	}
316 
317 	MAYHAVE(val, "rltime", DEF_ADVROUTERLIFETIME);
318 	if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
319 		logit(LOG_ERR,
320 		       "<%s> router lifetime (%d) on %s is invalid "
321 		       "(must be 0 or between %d and %d)",
322 		       __func__, val, intface,
323 		       tmp->maxinterval, MAXROUTERLIFETIME);
324 		goto errexit;
325 	}
326 	/*
327 	 * Basically, hosts MUST NOT send Router Advertisement messages at any
328 	 * time (RFC 2461, Section 6.2.3). However, it would sometimes be
329 	 * useful to allow hosts to advertise some parameters such as prefix
330 	 * information and link MTU. Thus, we allow hosts to invoke rtadvd
331 	 * only when router lifetime (on every advertising interface) is
332 	 * explicitly set zero. (see also the above section)
333 	 */
334 	if (val && forwarding == 0) {
335 		logit(LOG_ERR,
336 		       "<%s> non zero router lifetime is specified for %s, "
337 		       "which must not be allowed for hosts.  you must "
338 		       "change router lifetime or enable IPv6 forwarding.",
339 		       __func__, intface);
340 		goto errexit;
341 	}
342 	tmp->lifetime = val & 0xffff;
343 
344 	MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
345 	if (val < 0 || val > MAXREACHABLETIME) {
346 		logit(LOG_ERR,
347 		       "<%s> reachable time (%d) on %s is invalid "
348 		       "(must be no greater than %d)",
349 		       __func__, val, intface, MAXREACHABLETIME);
350 		goto errexit;
351 	}
352 	tmp->reachabletime = (uint32_t)val;
353 
354 	MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
355 	if (val64 < 0 || val64 > 0xffffffff) {
356 		logit(LOG_ERR, "<%s> retrans time (%lld) on %s out of range",
357 		       __func__, (long long)val64, intface);
358 		goto errexit;
359 	}
360 	tmp->retranstimer = (uint32_t)val64;
361 
362 	if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
363 		logit(LOG_ERR,
364 		       "<%s> mobile-ip6 configuration not supported",
365 		       __func__);
366 		goto errexit;
367 	}
368 	/* prefix information */
369 
370 	/*
371 	 * This is an implementation specific parameter to consider
372 	 * link propagation delays and poorly synchronized clocks when
373 	 * checking consistency of advertised lifetimes.
374 	 */
375 	MAYHAVE(val, "clockskew", 0);
376 	tmp->clockskew = val;
377 
378 	tmp->pfxs = 0;
379 	for (i = -1; i < MAXPREFIX; i++) {
380 		struct prefix *pfx;
381 
382 		makeentry(entbuf, sizeof(entbuf), i, "addr");
383 		addr = (char *)agetstr(entbuf, &bp);
384 		if (addr == NULL)
385 			continue;
386 
387 		/* allocate memory to store prefix information */
388 		if ((pfx = calloc(1, sizeof(*pfx))) == NULL) {
389 			logit(LOG_ERR,
390 			       "<%s> can't allocate memory: %m",
391 			       __func__);
392 			goto errexit;
393 		}
394 
395 		TAILQ_INSERT_TAIL(&tmp->prefix, pfx, next);
396 		tmp->pfxs++;
397 		pfx->rainfo = tmp;
398 
399 		pfx->origin = PREFIX_FROM_CONFIG;
400 
401 		if (inet_pton(AF_INET6, addr, &pfx->prefix) != 1) {
402 			logit(LOG_ERR,
403 			       "<%s> inet_pton failed for %s",
404 			       __func__, addr);
405 			goto errexit;
406 		}
407 		if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
408 			logit(LOG_ERR,
409 			       "<%s> multicast prefix (%s) must "
410 			       "not be advertised on %s",
411 			       __func__, addr, intface);
412 			goto errexit;
413 		}
414 		if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
415 			logit(LOG_NOTICE,
416 			       "<%s> link-local prefix (%s) will be"
417 			       " advertised on %s",
418 			       __func__, addr, intface);
419 
420 		makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
421 		MAYHAVE(val, entbuf, 64);
422 		if (val < 0 || val > 128) {
423 			logit(LOG_ERR, "<%s> prefixlen (%d) for %s "
424 			       "on %s out of range",
425 			       __func__, val, addr, intface);
426 			goto errexit;
427 		}
428 		pfx->prefixlen = (int)val;
429 
430 		makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
431 		if ((flagstr = (char *)agetstr(entbuf, &bp))) {
432 			val = 0;
433 			if (strchr(flagstr, 'l'))
434 				val |= ND_OPT_PI_FLAG_ONLINK;
435 			if (strchr(flagstr, 'a'))
436 				val |= ND_OPT_PI_FLAG_AUTO;
437 		} else {
438 			MAYHAVE(val, entbuf,
439 			    (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
440 		}
441 		pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
442 		pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
443 
444 		makeentry(entbuf, sizeof(entbuf), i, "vltime");
445 		MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
446 		if (val64 < 0 || val64 > 0xffffffff) {
447 			logit(LOG_ERR, "<%s> vltime (%lld) for "
448 			    "%s/%d on %s is out of range",
449 			    __func__, (long long)val64,
450 			    addr, pfx->prefixlen, intface);
451 			goto errexit;
452 		}
453 		pfx->validlifetime = (uint32_t)val64;
454 
455 		makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
456 		if (agetflag(entbuf)) {
457 			struct timespec now;
458 			prog_clock_gettime(CLOCK_MONOTONIC, &now);
459 			pfx->vltimeexpire =
460 				now.tv_sec + pfx->validlifetime;
461 		}
462 
463 		makeentry(entbuf, sizeof(entbuf), i, "pltime");
464 		MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
465 		if (val64 < 0 || val64 > 0xffffffff) {
466 			logit(LOG_ERR,
467 			    "<%s> pltime (%lld) for %s/%d on %s "
468 			    "is out of range",
469 			    __func__, (long long)val64,
470 			    addr, pfx->prefixlen, intface);
471 			goto errexit;
472 		}
473 		pfx->preflifetime = (uint32_t)val64;
474 
475 		makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
476 		if (agetflag(entbuf)) {
477 			struct timespec now;
478 			prog_clock_gettime(CLOCK_MONOTONIC, &now);
479 			pfx->pltimeexpire =
480 				now.tv_sec + pfx->preflifetime;
481 		}
482 	}
483 	if (TAILQ_FIRST(&tmp->prefix) == NULL && !agetflag("noifprefix"))
484 		get_prefix(tmp);
485 
486 	MAYHAVE(val64, "mtu", 0);
487 	if (val64 < 0 || val64 > 0xffffffff) {
488 		logit(LOG_ERR,
489 		       "<%s> mtu (%" PRIi64 ") on %s out of range",
490 		       __func__, val64, intface);
491 		goto errexit;
492 	}
493 	tmp->linkmtu = (uint32_t)val64;
494 	if (tmp->linkmtu == 0) {
495 		char *mtustr;
496 
497 		if ((mtustr = (char *)agetstr("mtu", &bp)) &&
498 		    strcmp(mtustr, "auto") == 0)
499 			tmp->linkmtu = tmp->phymtu;
500 	}
501 	else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
502 		logit(LOG_ERR,
503 		       "<%s> advertised link mtu (%d) on %s is invalid (must "
504 		       "be between least MTU (%d) and physical link MTU (%d)",
505 		       __func__, tmp->linkmtu, intface,
506 		       IPV6_MMTU, tmp->phymtu);
507 		goto errexit;
508 	}
509 
510 	/* route information */
511 	for (i = -1; i < MAXROUTE; i++) {
512 		struct rtinfo *rti;
513 		char oentbuf[256];
514 
515 		makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
516 		addr = (char *)agetstr(entbuf, &bp);
517 		if (addr == NULL) {
518 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
519 			addr = (char *)agetstr(oentbuf, &bp);
520 			if (addr) {
521 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
522 					oentbuf, entbuf);
523 			}
524 		}
525 		if (addr == NULL)
526 			continue;
527 
528 		ELM_MALLOC(rti);
529 		memset(rti, 0, sizeof(*rti));
530 
531 		/* link into chain */
532 		TAILQ_INSERT_TAIL(&tmp->route, rti, next);
533 
534 		if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) {
535 			logit(LOG_ERR, "<%s> inet_pton failed for %s",
536 			       __func__, addr);
537 			goto errexit;
538 		}
539 #if 0
540 		/*
541 		 * XXX: currently there's no restriction in route information
542 		 * prefix according to
543 		 * draft-ietf-ipngwg-router-selection-00.txt.
544 		 * However, I think the similar restriction be necessary.
545 		 */
546 		MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
547 		if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
548 			logit(LOG_ERR,
549 			       "<%s> multicast route (%s) must "
550 			       "not be advertised on %s",
551 			       __func__, addr, intface);
552 			goto errexit;
553 		}
554 		if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
555 			logit(LOG_NOTICE,
556 			       "<%s> link-local route (%s) will "
557 			       "be advertised on %s",
558 			       __func__, addr, intface);
559 			goto errexit;
560 		}
561 #endif
562 
563 		makeentry(entbuf, sizeof(entbuf), i, "rtplen");
564 		/* XXX: 256 is a magic number for compatibility check. */
565 		MAYHAVE(val, entbuf, 256);
566 		if (val == 256) {
567 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
568 			MAYHAVE(val, oentbuf, 256);
569 			if (val != 256) {
570 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
571 					oentbuf, entbuf);
572 			} else
573 				val = 64;
574 		}
575 		if (val < 0 || val > 128) {
576 			logit(LOG_ERR, "<%s> prefixlen (%d) for %s on %s "
577 			       "out of range",
578 			       __func__, val, addr, intface);
579 			goto errexit;
580 		}
581 		rti->prefixlen = (int)val;
582 
583 		makeentry(entbuf, sizeof(entbuf), i, "rtflags");
584 		if ((flagstr = (char *)agetstr(entbuf, &bp))) {
585 			val = 0;
586 			if (strchr(flagstr, 'h'))
587 				val |= ND_RA_FLAG_RTPREF_HIGH;
588 			if (strchr(flagstr, 'l')) {
589 				if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
590 					logit(LOG_ERR,
591 					    "<%s> the \'h\' and \'l\' route"
592 					    " preferences are exclusive",
593 					    __func__);
594 					goto errexit;
595 				}
596 				val |= ND_RA_FLAG_RTPREF_LOW;
597 			}
598 		} else
599 			MAYHAVE(val, entbuf, 256); /* XXX */
600 		if (val == 256) {
601 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
602 			MAYHAVE(val, oentbuf, 256);
603 			if (val != 256) {
604 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
605 					oentbuf, entbuf);
606 			} else
607 				val = 0;
608 		}
609 		rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
610 		if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) {
611 			logit(LOG_ERR, "<%s> invalid route preference (%02x) "
612 			       "for %s/%d on %s",
613 			       __func__, rti->rtpref, addr,
614 			       rti->prefixlen, intface);
615 			goto errexit;
616 		}
617 
618 		/*
619 		 * Since the spec does not a default value, we should make
620 		 * this entry mandatory.  However, FreeBSD 4.4 has shipped
621 		 * with this field being optional, we use the router lifetime
622 		 * as an ad-hoc default value with a warning message.
623 		 */
624 		makeentry(entbuf, sizeof(entbuf), i, "rtltime");
625 		MAYHAVE(val64, entbuf, -1);
626 		if (val64 == -1) {
627 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
628 			MAYHAVE(val64, oentbuf, -1);
629 			if (val64 != -1) {
630 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
631 					oentbuf, entbuf);
632 			} else {
633 				fprintf(stderr, "%s should be specified "
634 					"for interface %s.\n",
635 					entbuf, intface);
636 				val64 = tmp->lifetime;
637 			}
638 		}
639 		if (val64 < 0 || val64 > 0xffffffff) {
640 			logit(LOG_ERR, "<%s> route lifetime (%lld) for "
641 			    "%s/%d on %s out of range", __func__,
642 			    (long long)val64, addr, rti->prefixlen, intface);
643 			goto errexit;
644 		}
645 		rti->ltime = (uint32_t)val64;
646 	}
647 
648 	/* RDNSS */
649 	for (i = -1; i < MAXRDNSS; i++) {
650 		struct rdnss_addr *rdnsa;
651 
652 		makeentry(entbuf, sizeof(entbuf), i, "rdnss");
653 		addr = (char *)agetstr(entbuf, &bp);
654 		if (addr == NULL)
655 			continue;
656 
657 		ELM_MALLOC(rdnss);
658 		TAILQ_INSERT_TAIL(&tmp->rdnss, rdnss, next);
659 		TAILQ_INIT(&rdnss->list);
660 
661 		for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
662 			c = strcspn(ap, ",");
663 			strncpy(abuf, ap, c);
664 			abuf[c] = '\0';
665 			ELM_MALLOC(rdnsa);
666 			TAILQ_INSERT_TAIL(&rdnss->list, rdnsa, next);
667 			if (inet_pton(AF_INET6, abuf, &rdnsa->addr) != 1) {
668 				logit(LOG_ERR, "<%s> inet_pton failed for %s",
669 			           __func__, addr);
670 				goto errexit;
671 			}
672 		}
673 
674 		makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
675 		MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2);
676 		if (val64 < 0 || val64 > 0xffffffff) {
677 			logit(LOG_ERR, "<%s> %s (%lld) on %s is invalid",
678 			     __func__, entbuf, (long long)val64, intface);
679 			goto errexit;
680 		}
681 		rdnss->lifetime = (uint32_t)val64;
682 
683 	}
684 
685 	/* DNSSL */
686 	TAILQ_INIT(&tmp->dnssl);
687 	for (i = -1; i < MAXDNSSL; i++) {
688 		struct dnssl_domain *dnsd;
689 
690 		makeentry(entbuf, sizeof(entbuf), i, "dnssl");
691 		addr = (char *)agetstr(entbuf, &bp);
692 		if (addr == NULL)
693 			continue;
694 
695 		ELM_MALLOC(dnssl);
696 		TAILQ_INSERT_TAIL(&tmp->dnssl, dnssl, next);
697 		TAILQ_INIT(&dnssl->list);
698 
699 		for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
700 			c = strcspn(ap, ",");
701 			strncpy(abuf, ap, c);
702 			abuf[c] = '\0';
703 			ELM_MALLOC(dnsd);
704 			TAILQ_INSERT_TAIL(&dnssl->list, dnsd, next);
705 			dnsd->len = encode_domain(dnsd->domain, abuf);
706 		}
707 
708 		makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
709 		MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2);
710 		if (val64 < 0 || val64 > 0xffffffff) {
711 			logit(LOG_ERR, "<%s> %s (%lld) on %s is invalid",
712 			     __func__, entbuf, (long long)val64, intface);
713 			goto errexit;
714 		}
715 		dnssl->lifetime = (uint32_t)val64;
716 
717 	}
718 
719 	TAILQ_FOREACH(rai, &ralist, next) {
720 		if (rai->ifindex == tmp->ifindex) {
721 			TAILQ_REMOVE(&ralist, rai, next);
722 			if (Cflag) {
723 				free_rainfo(rai);
724 				rai = NULL;
725 				break;
726 			}
727 			/* If we already have a leaving RA use that
728 			 * as this config hasn't been advertised */
729 			if (rai->leaving) {
730 				tmp->leaving = rai->leaving;
731 				free_rainfo(rai);
732 				rai = tmp->leaving;
733 				rai->leaving_for = tmp;
734 				break;
735 			}
736 			rai->lifetime = 0;
737 			TAILQ_FOREACH(rdnss, &rai->rdnss, next)
738 				rdnss->lifetime = 0;
739 			TAILQ_FOREACH(dnssl, &rai->dnssl, next)
740 				dnssl->lifetime = 0;
741 			rai->leaving_for = tmp;
742 			tmp->leaving = rai;
743 			rai->initcounter = MAX_INITIAL_RTR_ADVERTISEMENTS;
744 			rai->mininterval = MIN_DELAY_BETWEEN_RAS;
745 			rai->maxinterval = MIN_DELAY_BETWEEN_RAS;
746 			rai->leaving_adv = MAX_FINAL_RTR_ADVERTISEMENTS;
747 			if (rai->timer == NULL)
748 				rai->timer = rtadvd_add_timer(ra_timeout,
749 							      ra_timer_update,
750 							      rai, rai);
751 			ra_timer_update((void *)rai, &rai->timer->tm);
752 			rtadvd_set_timer(&rai->timer->tm, rai->timer);
753 			break;
754 		}
755 	}
756 
757 	/* okey */
758 	TAILQ_INSERT_TAIL(&ralist, tmp, next);
759 
760 	/* construct the sending packet */
761 	make_packet(tmp);
762 
763 	/* set timer */
764 	if (rai)
765 		return;
766 	tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
767 				      tmp, tmp);
768 	ra_timer_set_short_delay(tmp, tmp->timer);
769 	tmp->timer_sol = rtadvd_add_timer(ra_timeout_sol, NULL, tmp, NULL);
770 
771 	return;
772 
773 errexit:
774 	if (exithard)
775 		exit(1);
776 	free_rainfo(tmp);
777 }
778 
779 void
780 get_prefix(struct rainfo *rai)
781 {
782 	struct ifaddrs *ifap, *ifa;
783 	struct prefix *pp;
784 	struct in6_addr *a;
785 	unsigned char *p, *ep, *m, *lim;
786 	char ntopbuf[INET6_ADDRSTRLEN];
787 
788 	if (getifaddrs(&ifap) < 0) {
789 		logit(LOG_ERR,
790 		       "<%s> can't get interface addresses",
791 		       __func__);
792 		exit(1);
793 	}
794 
795 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
796 		int plen;
797 
798 		if (strcmp(ifa->ifa_name, rai->ifname) != 0)
799 			continue;
800 		if (ifa->ifa_addr->sa_family != AF_INET6)
801 			continue;
802 		a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
803 		if (IN6_IS_ADDR_LINKLOCAL(a))
804 			continue;
805 		/* get prefix length */
806 		m = (unsigned char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
807 		lim = (unsigned char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
808 		plen = prefixlen(m, lim);
809 		if (plen <= 0 || plen > 128) {
810 			logit(LOG_ERR, "<%s> failed to get prefixlen "
811 			       "or prefix is invalid",
812 			       __func__);
813 			exit(1);
814 		}
815 		if (plen == 128)	/* XXX */
816 			continue;
817 		if (find_prefix(rai, a, plen)) {
818 			/* ignore a duplicated prefix. */
819 			continue;
820 		}
821 
822 		/* allocate memory to store prefix info. */
823 		if ((pp = calloc(1, sizeof(*pp))) == NULL) {
824 			logit(LOG_ERR,
825 			       "<%s> can't get allocate buffer for prefix",
826 			       __func__);
827 			exit(1);
828 		}
829 
830 		/* set prefix, sweep bits outside of prefixlen */
831 		pp->prefixlen = plen;
832 		memcpy(&pp->prefix, a, sizeof(*a));
833 		if (1)
834 		{
835 			p = (unsigned char *)&pp->prefix;
836 			ep = (unsigned char *)(&pp->prefix + 1);
837 			while (m < lim && p < ep)
838 				*p++ &= *m++;
839 			while (p < ep)
840 				*p++ = 0x00;
841 		}
842 	        if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
843 	            sizeof(ntopbuf))) {
844 			logit(LOG_ERR, "<%s> inet_ntop failed", __func__);
845 			exit(1);
846 		}
847 		logit(LOG_DEBUG,
848 		       "<%s> add %s/%d to prefix list on %s",
849 		       __func__, ntopbuf, pp->prefixlen, rai->ifname);
850 
851 		/* set other fields with protocol defaults */
852 		pp->validlifetime = DEF_ADVVALIDLIFETIME;
853 		pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
854 		pp->onlinkflg = 1;
855 		pp->autoconfflg = 1;
856 		pp->origin = PREFIX_FROM_KERNEL;
857 		pp->rainfo = rai;
858 
859 		/* link into chain */
860 		TAILQ_INSERT_TAIL(&rai->prefix, pp, next);
861 		rai->pfxs++;
862 	}
863 
864 	freeifaddrs(ifap);
865 }
866 
867 static void
868 makeentry(char *buf, size_t len, int id, const char *string)
869 {
870 
871 	if (id < 0)
872 		strlcpy(buf, string, len);
873 	else
874 		snprintf(buf, len, "%s%d", string, id);
875 }
876 
877 /*
878  * Add a prefix to the list of specified interface and reconstruct
879  * the outgoing packet.
880  * The prefix must not be in the list.
881  * XXX: other parameters of the prefix(e.g. lifetime) should be
882  * able to be specified.
883  */
884 static void
885 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
886 {
887 	struct prefix *prefix;
888 	char ntopbuf[INET6_ADDRSTRLEN];
889 
890 	if ((prefix = calloc(1, sizeof(*prefix))) == NULL) {
891 		logit(LOG_ERR, "<%s> memory allocation failed",
892 		       __func__);
893 		return;		/* XXX: error or exit? */
894 	}
895 	prefix->prefix = ipr->ipr_prefix.sin6_addr;
896 	prefix->prefixlen = ipr->ipr_plen;
897 	prefix->validlifetime = ipr->ipr_vltime;
898 	prefix->preflifetime = ipr->ipr_pltime;
899 	prefix->onlinkflg = ipr->ipr_raf_onlink;
900 	prefix->autoconfflg = ipr->ipr_raf_auto;
901 	prefix->origin = PREFIX_FROM_DYNAMIC;
902 
903 	prefix->rainfo = rai;
904 	TAILQ_INSERT_TAIL(&rai->prefix, prefix, next);
905 	rai->pfxs++;
906 
907 	logit(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
908 	       __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
909 				       ntopbuf, INET6_ADDRSTRLEN),
910 	       ipr->ipr_plen, rai->ifname);
911 
912 	/* free the previous packet */
913 	free(rai->ra_data);
914 	rai->ra_data = NULL;
915 
916 	/* reconstruct the packet */
917 	make_packet(rai);
918 }
919 
920 /*
921  * Delete a prefix to the list of specified interface and reconstruct
922  * the outgoing packet.
923  * The prefix must be in the list.
924  */
925 void
926 delete_prefix(struct prefix *prefix)
927 {
928 	char ntopbuf[INET6_ADDRSTRLEN];
929 	struct rainfo *rai = prefix->rainfo;
930 
931 	TAILQ_REMOVE(&rai->prefix, prefix, next);
932 	rai->pfxs--;
933 	logit(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
934 	       __func__, inet_ntop(AF_INET6, &prefix->prefix,
935 				       ntopbuf, INET6_ADDRSTRLEN),
936 	       prefix->prefixlen, rai->ifname);
937 	rtadvd_remove_timer(&prefix->timer);
938 	free(prefix);
939 }
940 
941 void
942 invalidate_prefix(struct prefix *prefix)
943 {
944 	char ntopbuf[INET6_ADDRSTRLEN];
945 	struct timespec timo;
946 	struct rainfo *rai = prefix->rainfo;
947 
948 	if (prefix->timer) {	/* sanity check */
949 		logit(LOG_ERR,
950 		    "<%s> assumption failure: timer already exists",
951 		    __func__);
952 		exit(1);
953 	}
954 
955 	logit(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
956 	    "will expire in %ld seconds", __func__,
957 	    inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
958 	    prefix->prefixlen, rai->ifname, (long)prefix_timo);
959 
960 	/* set the expiration timer */
961 	prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
962 	if (prefix->timer == NULL) {
963 		logit(LOG_ERR, "<%s> failed to add a timer for a prefix. "
964 		    "remove the prefix", __func__);
965 		delete_prefix(prefix);
966 	}
967 	timo.tv_sec = prefix_timo;
968 	timo.tv_nsec = 0;
969 	rtadvd_set_timer(&timo, prefix->timer);
970 }
971 
972 static struct rtadvd_timer *
973 prefix_timeout(void *arg)
974 {
975 	struct prefix *prefix = (struct prefix *)arg;
976 
977 	delete_prefix(prefix);
978 
979 	return(NULL);
980 }
981 
982 void
983 update_prefix(struct prefix * prefix)
984 {
985 	char ntopbuf[INET6_ADDRSTRLEN];
986 	struct rainfo *rai = prefix->rainfo;
987 
988 	if (prefix->timer == NULL) { /* sanity check */
989 		logit(LOG_ERR,
990 		    "<%s> assumption failure: timer does not exist",
991 		    __func__);
992 		exit(1);
993 	}
994 
995 	logit(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
996 	    __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
997 	    INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
998 
999 	/* stop the expiration timer */
1000 	rtadvd_remove_timer(&prefix->timer);
1001 }
1002 
1003 /*
1004  * Try to get an in6_prefixreq contents for a prefix which matches
1005  * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1006  * the interface whose name is ipr->ipr_name[].
1007  */
1008 static int
1009 init_prefix(struct in6_prefixreq *ipr)
1010 {
1011 #if 0
1012 	int s;
1013 
1014 	if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1015 		logit(LOG_ERR, "<%s> socket: %m", __func__);
1016 		exit(1);
1017 	}
1018 
1019 	if (prog_ioctl(s, SIOCGIFPREFIX_IN6, ipr) < 0) {
1020 		logit(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX: %m", __func__);
1021 
1022 		ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1023 		ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1024 		ipr->ipr_raf_onlink = 1;
1025 		ipr->ipr_raf_auto = 1;
1026 		/* omit other field initialization */
1027 	}
1028 	else if (ipr->ipr_origin < PR_ORIG_RR) {
1029 		char ntopbuf[INET6_ADDRSTRLEN];
1030 
1031 		logit(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1032 		       "lower than PR_ORIG_RR(router renumbering)."
1033 		       "This should not happen if I am router", __func__,
1034 		       inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1035 				 sizeof(ntopbuf)), ipr->ipr_origin);
1036 		prog_close(s);
1037 		return 1;
1038 	}
1039 
1040 	prog_close(s);
1041 	return 0;
1042 #else
1043 	ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1044 	ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1045 	ipr->ipr_raf_onlink = 1;
1046 	ipr->ipr_raf_auto = 1;
1047 	return 0;
1048 #endif
1049 }
1050 
1051 void
1052 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1053 {
1054 	struct in6_prefixreq ipr;
1055 
1056 	memset(&ipr, 0, sizeof(ipr));
1057 	if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1058 		logit(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
1059 		       "exist. This should not happen: %m", __func__,
1060 		       ifindex);
1061 		exit(1);
1062 	}
1063 	ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1064 	ipr.ipr_prefix.sin6_family = AF_INET6;
1065 	ipr.ipr_prefix.sin6_addr = *addr;
1066 	ipr.ipr_plen = plen;
1067 
1068 	if (init_prefix(&ipr))
1069 		return; /* init failed by some error */
1070 	add_prefix(rai, &ipr);
1071 }
1072 
1073 void
1074 make_packet(struct rainfo *rainfo)
1075 {
1076 	size_t packlen, lladdroptlen = 0;
1077 	char *buf;
1078 	struct nd_router_advert *ra;
1079 	struct nd_opt_prefix_info *ndopt_pi;
1080 	struct nd_opt_mtu *ndopt_mtu;
1081 	struct prefix *pfx;
1082 	struct nd_opt_route_info *ndopt_rti;
1083 	struct rtinfo *rti;
1084 	struct nd_opt_rdnss *ndopt_rdnss;
1085 	struct rdnss *rdns;
1086 	struct rdnss_addr *rdnsa;
1087 	struct nd_opt_dnssl *ndopt_dnssl;
1088 	struct dnssl *dnsl;
1089 	struct dnssl_domain *dnsd;
1090 	size_t len, plen;
1091 
1092 	/* calculate total length */
1093 	packlen = sizeof(struct nd_router_advert);
1094 	if (rainfo->advlinkopt) {
1095 		if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
1096 			logit(LOG_INFO,
1097 			       "<%s> link-layer address option has"
1098 			       " null length on %s.  Treat as not included.",
1099 			       __func__, rainfo->ifname);
1100 			rainfo->advlinkopt = 0;
1101 		}
1102 		packlen += lladdroptlen;
1103 	}
1104 	if (TAILQ_FIRST(&rainfo->prefix) != NULL)
1105 		packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
1106 	if (rainfo->linkmtu)
1107 		packlen += sizeof(struct nd_opt_mtu);
1108 	TAILQ_FOREACH(rti, &rainfo->route, next)
1109 		packlen += sizeof(struct nd_opt_route_info) +
1110 			   ((rti->prefixlen + 0x3f) >> 6) * 8;
1111 
1112 	TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1113 		packlen += sizeof(struct nd_opt_rdnss);
1114 		TAILQ_FOREACH(rdnsa, &rdns->list, next)
1115 			packlen += sizeof(rdnsa->addr);
1116 	}
1117 	TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1118 		packlen += sizeof(struct nd_opt_dnssl);
1119 		len = 0;
1120 		TAILQ_FOREACH(dnsd, &dnsl->list, next)
1121 			len += dnsd->len;
1122 		len += len % 8 ? 8 - len % 8 : 0;
1123 		packlen += len;
1124 	}
1125 
1126 	/* allocate memory for the packet */
1127 	if ((buf = realloc(rainfo->ra_data, packlen)) == NULL) {
1128 		logit(LOG_ERR,
1129 		       "<%s> can't get enough memory for an RA packet %m",
1130 		       __func__);
1131 		exit(1);
1132 	}
1133 	rainfo->ra_data = buf;
1134 	/* XXX: what if packlen > 576? */
1135 	rainfo->ra_datalen = packlen;
1136 #define CHECKLEN(size) \
1137 	do { \
1138 		if (buf + size > rainfo->ra_data + packlen) { \
1139 			logit(LOG_ERR, \
1140 			    "<%s, %d> RA packet does not fit in %zu",\
1141 			    __func__, __LINE__, packlen); \
1142 			exit(1); \
1143 		} \
1144 	} while (/*CONSTCOND*/0)
1145 	/*
1146 	 * construct the packet
1147 	 */
1148 	CHECKLEN(sizeof(*ra));
1149 	ra = (struct nd_router_advert *)buf;
1150 	ra->nd_ra_type = ND_ROUTER_ADVERT;
1151 	ra->nd_ra_code = 0;
1152 	ra->nd_ra_cksum = 0;
1153 	ra->nd_ra_curhoplimit = (uint8_t)(0xff & rainfo->hoplimit);
1154 	ra->nd_ra_flags_reserved = 0; /* just in case */
1155 	/*
1156 	 * XXX: the router preference field, which is a 2-bit field, should be
1157 	 * initialized before other fields.
1158 	 */
1159 	ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
1160 	ra->nd_ra_flags_reserved |=
1161 		rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
1162 	ra->nd_ra_flags_reserved |=
1163 		rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
1164 	ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
1165 	ra->nd_ra_reachable = htonl(rainfo->reachabletime);
1166 	ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
1167 	buf += sizeof(*ra);
1168 
1169 	if (rainfo->advlinkopt) {
1170 		CHECKLEN(sizeof(struct nd_opt_hdr));
1171 		lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
1172 		buf += lladdroptlen;
1173 	}
1174 
1175 	if (rainfo->linkmtu) {
1176 		CHECKLEN(sizeof(*ndopt_mtu));
1177 		ndopt_mtu = (struct nd_opt_mtu *)buf;
1178 		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1179 		ndopt_mtu->nd_opt_mtu_len = 1;
1180 		ndopt_mtu->nd_opt_mtu_reserved = 0;
1181 		ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
1182 		buf += sizeof(struct nd_opt_mtu);
1183 	}
1184 
1185 	TAILQ_FOREACH(pfx, &rainfo->prefix, next) {
1186 		uint32_t vltime, pltime;
1187 		struct timespec now;
1188 
1189 		CHECKLEN(sizeof(*ndopt_pi));
1190 		ndopt_pi = (struct nd_opt_prefix_info *)buf;
1191 		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1192 		ndopt_pi->nd_opt_pi_len = 4;
1193 		ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
1194 		ndopt_pi->nd_opt_pi_flags_reserved = 0;
1195 		if (pfx->onlinkflg)
1196 			ndopt_pi->nd_opt_pi_flags_reserved |=
1197 				ND_OPT_PI_FLAG_ONLINK;
1198 		if (pfx->autoconfflg)
1199 			ndopt_pi->nd_opt_pi_flags_reserved |=
1200 				ND_OPT_PI_FLAG_AUTO;
1201 		if (pfx->timer)
1202 			vltime = 0;
1203 		else {
1204 			if (pfx->vltimeexpire || pfx->pltimeexpire)
1205 				prog_clock_gettime(CLOCK_MONOTONIC, &now);
1206 			if (pfx->vltimeexpire == 0)
1207 				vltime = pfx->validlifetime;
1208 			else
1209 				vltime = (pfx->vltimeexpire > now.tv_sec) ?
1210 				    pfx->vltimeexpire - now.tv_sec : 0;
1211 		}
1212 		if (pfx->timer)
1213 			pltime = 0;
1214 		else {
1215 			if (pfx->pltimeexpire == 0)
1216 				pltime = pfx->preflifetime;
1217 			else
1218 				pltime = (pfx->pltimeexpire > now.tv_sec) ?
1219 				    pfx->pltimeexpire - now.tv_sec : 0;
1220 		}
1221 		if (vltime < pltime) {
1222 			/*
1223 			 * this can happen if vltime is decrement but pltime
1224 			 * is not.
1225 			 */
1226 			pltime = vltime;
1227 		}
1228 		ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1229 		ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1230 		ndopt_pi->nd_opt_pi_reserved2 = 0;
1231 		ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1232 
1233 		buf += sizeof(struct nd_opt_prefix_info);
1234 	}
1235 
1236 	TAILQ_FOREACH(rti, &rainfo->route, next) {
1237 		uint8_t psize = (rti->prefixlen + 0x3f) >> 6;
1238 
1239 		CHECKLEN(sizeof(*ndopt_rti));
1240 		ndopt_rti = (struct nd_opt_route_info *)buf;
1241 		ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1242 		ndopt_rti->nd_opt_rti_len = 1 + psize;
1243 		ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1244 		ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1245 		ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1246 		memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1247 		buf += sizeof(struct nd_opt_route_info) + psize * 8;
1248 	}
1249 
1250 	TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1251 		CHECKLEN(sizeof(*ndopt_rdnss));
1252 		ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1253 		ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1254 		ndopt_rdnss->nd_opt_rdnss_len = 1;
1255 		ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1256 		ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdns->lifetime);
1257 		buf += sizeof(*ndopt_rdnss);
1258 
1259 		TAILQ_FOREACH(rdnsa, &rdns->list, next) {
1260 			CHECKLEN(sizeof(rdnsa->addr));
1261 			memcpy(buf, &rdnsa->addr, sizeof(rdnsa->addr));
1262 			ndopt_rdnss->nd_opt_rdnss_len += 2;
1263 			buf += sizeof(rdnsa->addr);
1264 		}
1265 	}
1266 
1267 	TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1268 		CHECKLEN(sizeof(*ndopt_dnssl));
1269 		ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1270 		ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1271 		ndopt_dnssl->nd_opt_dnssl_len = 0;
1272 		ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1273 		ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dnsl->lifetime);
1274 		buf += sizeof(*ndopt_dnssl);
1275 
1276 		TAILQ_FOREACH(dnsd, &dnsl->list, next) {
1277 			CHECKLEN(dnsd->len);
1278 			memcpy(buf, dnsd->domain, dnsd->len);
1279 			buf += dnsd->len;
1280 		}
1281 		/* Ensure our length is padded correctly */
1282 		len = buf - (char *)ndopt_dnssl;
1283 		plen = len % 8 ? 8 - len % 8 : 0;
1284 		CHECKLEN(plen);
1285 		memset(buf, 0, plen);
1286 		buf += plen;
1287 		ndopt_dnssl->nd_opt_dnssl_len = (len + plen) / 8;
1288 	}
1289 	memset(buf, 0, packlen - (buf - rainfo->ra_data));
1290 }
1291 
1292 static int
1293 getinet6sysctl(int code)
1294 {
1295 	const int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, code };
1296 	int value;
1297 	size_t size;
1298 
1299 	size = sizeof(value);
1300 	if (prog_sysctl(mib, __arraycount(mib), &value, &size, NULL, 0)
1301 	    < 0) {
1302 		logit(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %m",
1303 		       __func__, code);
1304 		return -1;
1305 	}
1306 	else
1307 		return value;
1308 }
1309