xref: /netbsd-src/usr.sbin/rtadvd/config.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: config.c,v 1.44 2020/04/21 12:16:47 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  * Delete a prefix to the list of specified interface and reconstruct
879  * the outgoing packet.
880  * The prefix must be in the list.
881  */
882 void
883 delete_prefix(struct prefix *prefix)
884 {
885 	char ntopbuf[INET6_ADDRSTRLEN];
886 	struct rainfo *rai = prefix->rainfo;
887 
888 	TAILQ_REMOVE(&rai->prefix, prefix, next);
889 	rai->pfxs--;
890 	logit(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
891 	       __func__, inet_ntop(AF_INET6, &prefix->prefix,
892 				       ntopbuf, INET6_ADDRSTRLEN),
893 	       prefix->prefixlen, rai->ifname);
894 	rtadvd_remove_timer(&prefix->timer);
895 	free(prefix);
896 }
897 
898 void
899 invalidate_prefix(struct prefix *prefix)
900 {
901 	char ntopbuf[INET6_ADDRSTRLEN];
902 	struct timespec timo;
903 	struct rainfo *rai = prefix->rainfo;
904 
905 	if (prefix->timer) {	/* sanity check */
906 		logit(LOG_ERR,
907 		    "<%s> assumption failure: timer already exists",
908 		    __func__);
909 		exit(1);
910 	}
911 
912 	logit(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
913 	    "will expire in %ld seconds", __func__,
914 	    inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
915 	    prefix->prefixlen, rai->ifname, (long)prefix_timo);
916 
917 	/* set the expiration timer */
918 	prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
919 	if (prefix->timer == NULL) {
920 		logit(LOG_ERR, "<%s> failed to add a timer for a prefix. "
921 		    "remove the prefix", __func__);
922 		delete_prefix(prefix);
923 	}
924 	timo.tv_sec = prefix_timo;
925 	timo.tv_nsec = 0;
926 	rtadvd_set_timer(&timo, prefix->timer);
927 }
928 
929 static struct rtadvd_timer *
930 prefix_timeout(void *arg)
931 {
932 	struct prefix *prefix = (struct prefix *)arg;
933 
934 	delete_prefix(prefix);
935 
936 	return(NULL);
937 }
938 
939 void
940 update_prefix(struct prefix * prefix)
941 {
942 	char ntopbuf[INET6_ADDRSTRLEN];
943 	struct rainfo *rai = prefix->rainfo;
944 
945 	if (prefix->timer == NULL) { /* sanity check */
946 		logit(LOG_ERR,
947 		    "<%s> assumption failure: timer does not exist",
948 		    __func__);
949 		exit(1);
950 	}
951 
952 	logit(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
953 	    __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
954 	    INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
955 
956 	/* stop the expiration timer */
957 	rtadvd_remove_timer(&prefix->timer);
958 }
959 
960 /*
961  * Add a prefix to the list of specified interface and reconstruct
962  * the outgoing packet.
963  * The prefix must not be in the list.
964  * XXX: other parameters of the prefix(e.g. lifetime) should be
965  * able to be specified.
966  */
967 void
968 add_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
969 {
970 	struct prefix *prefix;
971 	char ntopbuf[INET6_ADDRSTRLEN];
972 
973 	if ((prefix = calloc(1, sizeof(*prefix))) == NULL) {
974 		logit(LOG_ERR, "<%s> memory allocation failed",
975 		       __func__);
976 		return;		/* XXX: error or exit? */
977 	}
978 	prefix->prefix = *addr;
979 	prefix->prefixlen = plen;
980 	prefix->validlifetime = DEF_ADVVALIDLIFETIME;
981 	prefix->preflifetime = DEF_ADVPREFERREDLIFETIME;
982 	prefix->onlinkflg = 1;
983 	prefix->autoconfflg = 0;
984 	prefix->origin = PREFIX_FROM_DYNAMIC;
985 
986 	prefix->rainfo = rai;
987 	TAILQ_INSERT_TAIL(&rai->prefix, prefix, next);
988 	rai->pfxs++;
989 
990 	logit(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
991 	       __func__, inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
992 	       plen, rai->ifname);
993 
994 	/* free the previous packet */
995 	free(rai->ra_data);
996 	rai->ra_data = NULL;
997 
998 	/* reconstruct the packet */
999 	make_packet(rai);
1000 }
1001 
1002 void
1003 make_packet(struct rainfo *rainfo)
1004 {
1005 	size_t packlen, lladdroptlen = 0;
1006 	char *buf;
1007 	struct nd_router_advert *ra;
1008 	struct nd_opt_prefix_info *ndopt_pi;
1009 	struct nd_opt_mtu *ndopt_mtu;
1010 	struct prefix *pfx;
1011 	struct nd_opt_route_info *ndopt_rti;
1012 	struct rtinfo *rti;
1013 	struct nd_opt_rdnss *ndopt_rdnss;
1014 	struct rdnss *rdns;
1015 	struct rdnss_addr *rdnsa;
1016 	struct nd_opt_dnssl *ndopt_dnssl;
1017 	struct dnssl *dnsl;
1018 	struct dnssl_domain *dnsd;
1019 	size_t len, plen;
1020 
1021 	/* calculate total length */
1022 	packlen = sizeof(struct nd_router_advert);
1023 	if (rainfo->advlinkopt) {
1024 		if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
1025 			logit(LOG_INFO,
1026 			       "<%s> link-layer address option has"
1027 			       " null length on %s.  Treat as not included.",
1028 			       __func__, rainfo->ifname);
1029 			rainfo->advlinkopt = 0;
1030 		}
1031 		packlen += lladdroptlen;
1032 	}
1033 	if (TAILQ_FIRST(&rainfo->prefix) != NULL)
1034 		packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
1035 	if (rainfo->linkmtu)
1036 		packlen += sizeof(struct nd_opt_mtu);
1037 	TAILQ_FOREACH(rti, &rainfo->route, next)
1038 		packlen += sizeof(struct nd_opt_route_info) +
1039 			   ((rti->prefixlen + 0x3f) >> 6) * 8;
1040 
1041 	TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1042 		packlen += sizeof(struct nd_opt_rdnss);
1043 		TAILQ_FOREACH(rdnsa, &rdns->list, next)
1044 			packlen += sizeof(rdnsa->addr);
1045 	}
1046 	TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1047 		packlen += sizeof(struct nd_opt_dnssl);
1048 		len = 0;
1049 		TAILQ_FOREACH(dnsd, &dnsl->list, next)
1050 			len += dnsd->len;
1051 		len += len % 8 ? 8 - len % 8 : 0;
1052 		packlen += len;
1053 	}
1054 
1055 	/* allocate memory for the packet */
1056 	if ((buf = realloc(rainfo->ra_data, packlen)) == NULL) {
1057 		logit(LOG_ERR,
1058 		       "<%s> can't get enough memory for an RA packet %m",
1059 		       __func__);
1060 		exit(1);
1061 	}
1062 	rainfo->ra_data = buf;
1063 	/* XXX: what if packlen > 576? */
1064 	rainfo->ra_datalen = packlen;
1065 #define CHECKLEN(size) \
1066 	do { \
1067 		if (buf + size > rainfo->ra_data + packlen) { \
1068 			logit(LOG_ERR, \
1069 			    "<%s, %d> RA packet does not fit in %zu",\
1070 			    __func__, __LINE__, packlen); \
1071 			exit(1); \
1072 		} \
1073 	} while (/*CONSTCOND*/0)
1074 	/*
1075 	 * construct the packet
1076 	 */
1077 	CHECKLEN(sizeof(*ra));
1078 	ra = (struct nd_router_advert *)buf;
1079 	ra->nd_ra_type = ND_ROUTER_ADVERT;
1080 	ra->nd_ra_code = 0;
1081 	ra->nd_ra_cksum = 0;
1082 	ra->nd_ra_curhoplimit = (uint8_t)(0xff & rainfo->hoplimit);
1083 	ra->nd_ra_flags_reserved = 0; /* just in case */
1084 	/*
1085 	 * XXX: the router preference field, which is a 2-bit field, should be
1086 	 * initialized before other fields.
1087 	 */
1088 	ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
1089 	ra->nd_ra_flags_reserved |=
1090 		rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
1091 	ra->nd_ra_flags_reserved |=
1092 		rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
1093 	ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
1094 	ra->nd_ra_reachable = htonl(rainfo->reachabletime);
1095 	ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
1096 	buf += sizeof(*ra);
1097 
1098 	if (rainfo->advlinkopt) {
1099 		CHECKLEN(sizeof(struct nd_opt_hdr));
1100 		lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
1101 		buf += lladdroptlen;
1102 	}
1103 
1104 	if (rainfo->linkmtu) {
1105 		CHECKLEN(sizeof(*ndopt_mtu));
1106 		ndopt_mtu = (struct nd_opt_mtu *)buf;
1107 		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1108 		ndopt_mtu->nd_opt_mtu_len = 1;
1109 		ndopt_mtu->nd_opt_mtu_reserved = 0;
1110 		ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
1111 		buf += sizeof(struct nd_opt_mtu);
1112 	}
1113 
1114 	TAILQ_FOREACH(pfx, &rainfo->prefix, next) {
1115 		uint32_t vltime, pltime;
1116 		struct timespec now;
1117 
1118 		CHECKLEN(sizeof(*ndopt_pi));
1119 		ndopt_pi = (struct nd_opt_prefix_info *)buf;
1120 		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1121 		ndopt_pi->nd_opt_pi_len = 4;
1122 		ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
1123 		ndopt_pi->nd_opt_pi_flags_reserved = 0;
1124 		if (pfx->onlinkflg)
1125 			ndopt_pi->nd_opt_pi_flags_reserved |=
1126 				ND_OPT_PI_FLAG_ONLINK;
1127 		if (pfx->autoconfflg)
1128 			ndopt_pi->nd_opt_pi_flags_reserved |=
1129 				ND_OPT_PI_FLAG_AUTO;
1130 		if (pfx->timer)
1131 			vltime = 0;
1132 		else {
1133 			if (pfx->vltimeexpire || pfx->pltimeexpire)
1134 				prog_clock_gettime(CLOCK_MONOTONIC, &now);
1135 			if (pfx->vltimeexpire == 0)
1136 				vltime = pfx->validlifetime;
1137 			else
1138 				vltime = (pfx->vltimeexpire > now.tv_sec) ?
1139 				    pfx->vltimeexpire - now.tv_sec : 0;
1140 		}
1141 		if (pfx->timer)
1142 			pltime = 0;
1143 		else {
1144 			if (pfx->pltimeexpire == 0)
1145 				pltime = pfx->preflifetime;
1146 			else
1147 				pltime = (pfx->pltimeexpire > now.tv_sec) ?
1148 				    pfx->pltimeexpire - now.tv_sec : 0;
1149 		}
1150 		if (vltime < pltime) {
1151 			/*
1152 			 * this can happen if vltime is decrement but pltime
1153 			 * is not.
1154 			 */
1155 			pltime = vltime;
1156 		}
1157 		ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1158 		ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1159 		ndopt_pi->nd_opt_pi_reserved2 = 0;
1160 		ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1161 
1162 		buf += sizeof(struct nd_opt_prefix_info);
1163 	}
1164 
1165 	TAILQ_FOREACH(rti, &rainfo->route, next) {
1166 		uint8_t psize = (rti->prefixlen + 0x3f) >> 6;
1167 
1168 		CHECKLEN(sizeof(*ndopt_rti));
1169 		ndopt_rti = (struct nd_opt_route_info *)buf;
1170 		ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1171 		ndopt_rti->nd_opt_rti_len = 1 + psize;
1172 		ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1173 		ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1174 		ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1175 		memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1176 		buf += sizeof(struct nd_opt_route_info) + psize * 8;
1177 	}
1178 
1179 	TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1180 		CHECKLEN(sizeof(*ndopt_rdnss));
1181 		ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1182 		ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1183 		ndopt_rdnss->nd_opt_rdnss_len = 1;
1184 		ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1185 		ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdns->lifetime);
1186 		buf += sizeof(*ndopt_rdnss);
1187 
1188 		TAILQ_FOREACH(rdnsa, &rdns->list, next) {
1189 			CHECKLEN(sizeof(rdnsa->addr));
1190 			memcpy(buf, &rdnsa->addr, sizeof(rdnsa->addr));
1191 			ndopt_rdnss->nd_opt_rdnss_len += 2;
1192 			buf += sizeof(rdnsa->addr);
1193 		}
1194 	}
1195 
1196 	TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1197 		CHECKLEN(sizeof(*ndopt_dnssl));
1198 		ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1199 		ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1200 		ndopt_dnssl->nd_opt_dnssl_len = 0;
1201 		ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1202 		ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dnsl->lifetime);
1203 		buf += sizeof(*ndopt_dnssl);
1204 
1205 		TAILQ_FOREACH(dnsd, &dnsl->list, next) {
1206 			CHECKLEN(dnsd->len);
1207 			memcpy(buf, dnsd->domain, dnsd->len);
1208 			buf += dnsd->len;
1209 		}
1210 		/* Ensure our length is padded correctly */
1211 		len = buf - (char *)ndopt_dnssl;
1212 		plen = len % 8 ? 8 - len % 8 : 0;
1213 		CHECKLEN(plen);
1214 		memset(buf, 0, plen);
1215 		buf += plen;
1216 		ndopt_dnssl->nd_opt_dnssl_len = (len + plen) / 8;
1217 	}
1218 	memset(buf, 0, packlen - (buf - rainfo->ra_data));
1219 }
1220 
1221 static int
1222 getinet6sysctl(int code)
1223 {
1224 	const int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, code };
1225 	int value;
1226 	size_t size;
1227 
1228 	size = sizeof(value);
1229 	if (prog_sysctl(mib, __arraycount(mib), &value, &size, NULL, 0)
1230 	    < 0) {
1231 		logit(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %m",
1232 		       __func__, code);
1233 		return -1;
1234 	}
1235 	else
1236 		return value;
1237 }
1238