1 /* $NetBSD: config.c,v 1.47 2021/11/27 22:30:26 rillig 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
encode_domain(char * dst,const char * src)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
free_rainfo(struct rainfo * rai)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
getconfig(const char * intface,int exithard)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 logit(LOG_ERR, "%s: need %s for interface %s", \
177 __func__, cap, intface); \
178 goto errexit; \
179 } \
180 var = t; \
181 } while (0)
182
183 #define MAYHAVE(var, cap, def) \
184 do { \
185 if ((var = agetnum(cap)) < 0) \
186 var = def; \
187 } while (0)
188
189 #define ELM_MALLOC(p) \
190 do { \
191 p = calloc(1, sizeof(*p)); \
192 if (p == NULL) { \
193 logit(LOG_ERR, "%s: calloc failed: %m", \
194 __func__); \
195 goto errexit; \
196 } \
197 } while(/*CONSTCOND*/0)
198
199 if (if_nametoindex(intface) == 0) {
200 logit(LOG_INFO, "%s: interface %s not found, ignoring",
201 __func__, intface);
202 return;
203 }
204
205 logit(LOG_DEBUG, "%s: loading configuration for interface %s",
206 __func__, intface);
207
208 if ((stat = agetent(tbuf, intface)) <= 0) {
209 memset(tbuf, 0, sizeof(tbuf));
210 logit(LOG_INFO,
211 "%s: %s isn't defined in the configuration file"
212 " or the configuration file doesn't exist."
213 " Treat it as default",
214 __func__, intface);
215 }
216
217 ELM_MALLOC(tmp);
218 TAILQ_INIT(&tmp->soliciter);
219 TAILQ_INIT(&tmp->prefix);
220 TAILQ_INIT(&tmp->route);
221 TAILQ_INIT(&tmp->rdnss);
222 TAILQ_INIT(&tmp->dnssl);
223
224 /* check if we are allowed to forward packets (if not determined) */
225 if (forwarding < 0) {
226 if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
227 exit(EXIT_FAILURE);
228 }
229
230 /* get interface information */
231 if (agetflag("nolladdr"))
232 tmp->advlinkopt = 0;
233 else
234 tmp->advlinkopt = 1;
235 if (tmp->advlinkopt) {
236 if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
237 logit(LOG_ERR,
238 "%s: can't get information of %s",
239 __func__, intface);
240 goto errexit;
241 }
242 tmp->ifindex = tmp->sdl->sdl_index;
243 } else {
244 tmp->ifindex = if_nametoindex(intface);
245 if (tmp->ifindex == 0) {
246 logit(LOG_ERR,
247 "%s: can't get information of %s",
248 __func__, intface);
249 goto errexit;
250 }
251 }
252 tmp->ifflags = if_getflags(tmp->ifindex, 0);
253 strlcpy(tmp->ifname, intface, sizeof(tmp->ifname));
254 if ((tmp->phymtu = if_getmtu(intface)) == 0) {
255 tmp->phymtu = IPV6_MMTU;
256 logit(LOG_WARNING,
257 "%s: can't get interface mtu of %s. Treat as %d",
258 __func__, intface, IPV6_MMTU);
259 }
260
261 /*
262 * set router configuration variables.
263 */
264 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
265 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
266 logit(LOG_ERR,
267 "%s: maxinterval (%d) on %s is invalid "
268 "(must be between %u and %u)", __func__, val,
269 intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
270 goto errexit;
271 }
272 tmp->maxinterval = val;
273 MAYHAVE(val, "mininterval", tmp->maxinterval/3);
274 if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
275 logit(LOG_ERR,
276 "%s: mininterval (%d) on %s is invalid "
277 "(must be between %u and %d)",
278 __func__, val, intface, MIN_MININTERVAL,
279 (tmp->maxinterval * 3) / 4);
280 goto errexit;
281 }
282 tmp->mininterval = val;
283
284 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
285 tmp->hoplimit = val & 0xff;
286
287 if ((flagstr = agetstr("raflags", &bp))) {
288 val = 0;
289 if (strchr(flagstr, 'm'))
290 val |= ND_RA_FLAG_MANAGED;
291 if (strchr(flagstr, 'o'))
292 val |= ND_RA_FLAG_OTHER;
293 if (strchr(flagstr, 'h'))
294 val |= ND_RA_FLAG_RTPREF_HIGH;
295 if (strchr(flagstr, 'l')) {
296 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
297 logit(LOG_ERR, "%s: the \'h\' and \'l\'"
298 " router flags are exclusive", __func__);
299 goto errexit;
300 }
301 val |= ND_RA_FLAG_RTPREF_LOW;
302 }
303 } else {
304 MAYHAVE(val, "raflags", 0);
305 }
306 tmp->managedflg = val & ND_RA_FLAG_MANAGED;
307 tmp->otherflg = val & ND_RA_FLAG_OTHER;
308 #ifndef ND_RA_FLAG_RTPREF_MASK
309 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
310 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
311 #endif
312 tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
313 if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
314 logit(LOG_ERR, "%s: invalid router preference (%02x) on %s",
315 __func__, tmp->rtpref, intface);
316 goto errexit;
317 }
318
319 MAYHAVE(val, "rltime", DEF_ADVROUTERLIFETIME);
320 if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
321 logit(LOG_ERR, "%s: router lifetime (%d) on %s is invalid "
322 "(must be 0 or between %d and %d)",
323 __func__, val, intface,
324 tmp->maxinterval, MAXROUTERLIFETIME);
325 goto errexit;
326 }
327 /*
328 * Basically, hosts MUST NOT send Router Advertisement messages at any
329 * time (RFC 2461, Section 6.2.3). However, it would sometimes be
330 * useful to allow hosts to advertise some parameters such as prefix
331 * information and link MTU. Thus, we allow hosts to invoke rtadvd
332 * only when router lifetime (on every advertising interface) is
333 * explicitly set zero. (see also the above section)
334 */
335 if (val && forwarding == 0) {
336 logit(LOG_ERR,
337 "%s: non zero router lifetime is specified for %s, "
338 "which must not be allowed for hosts. you must "
339 "change router lifetime or enable IPv6 forwarding.",
340 __func__, intface);
341 goto errexit;
342 }
343 tmp->lifetime = val & 0xffff;
344
345 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
346 if (val < 0 || val > MAXREACHABLETIME) {
347 logit(LOG_ERR,
348 "%s: reachable time (%d) on %s is invalid "
349 "(must be no greater than %d)",
350 __func__, val, intface, MAXREACHABLETIME);
351 goto errexit;
352 }
353 tmp->reachabletime = (uint32_t)val;
354
355 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
356 if (val64 < 0 || val64 > 0xffffffff) {
357 logit(LOG_ERR, "%s: retrans time (%lld) on %s out of range",
358 __func__, (long long)val64, intface);
359 goto errexit;
360 }
361 tmp->retranstimer = (uint32_t)val64;
362
363 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
364 logit(LOG_ERR,
365 "%s: mobile-ip6 configuration not supported",
366 __func__);
367 goto errexit;
368 }
369 /* prefix information */
370
371 /*
372 * This is an implementation specific parameter to consider
373 * link propagation delays and poorly synchronized clocks when
374 * checking consistency of advertised lifetimes.
375 */
376 MAYHAVE(val, "clockskew", 0);
377 tmp->clockskew = val;
378
379 tmp->pfxs = 0;
380 for (i = -1; i < MAXPREFIX; i++) {
381 struct prefix *pfx;
382
383 makeentry(entbuf, sizeof(entbuf), i, "addr");
384 addr = (char *)agetstr(entbuf, &bp);
385 if (addr == NULL)
386 continue;
387
388 /* allocate memory to store prefix information */
389 if ((pfx = calloc(1, sizeof(*pfx))) == NULL) {
390 logit(LOG_ERR,
391 "%s: can't allocate memory: %m",
392 __func__);
393 goto errexit;
394 }
395
396 TAILQ_INSERT_TAIL(&tmp->prefix, pfx, next);
397 tmp->pfxs++;
398 pfx->rainfo = tmp;
399
400 pfx->origin = PREFIX_FROM_CONFIG;
401
402 if (inet_pton(AF_INET6, addr, &pfx->prefix) != 1) {
403 logit(LOG_ERR,
404 "%s: inet_pton failed for %s",
405 __func__, addr);
406 goto errexit;
407 }
408 if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
409 logit(LOG_ERR,
410 "%s: multicast prefix (%s) must "
411 "not be advertised on %s",
412 __func__, addr, intface);
413 goto errexit;
414 }
415 if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
416 logit(LOG_NOTICE,
417 "%s: link-local prefix (%s) will be"
418 " advertised on %s",
419 __func__, addr, intface);
420
421 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
422 MAYHAVE(val, entbuf, 64);
423 if (val < 0 || val > 128) {
424 logit(LOG_ERR, "%s: prefixlen (%d) for %s "
425 "on %s out of range",
426 __func__, val, addr, intface);
427 goto errexit;
428 }
429 pfx->prefixlen = (int)val;
430
431 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
432 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
433 val = 0;
434 if (strchr(flagstr, 'l'))
435 val |= ND_OPT_PI_FLAG_ONLINK;
436 if (strchr(flagstr, 'a'))
437 val |= ND_OPT_PI_FLAG_AUTO;
438 } else {
439 MAYHAVE(val, entbuf,
440 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
441 }
442 pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
443 pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
444
445 makeentry(entbuf, sizeof(entbuf), i, "vltime");
446 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
447 if (val64 < 0 || val64 > 0xffffffff) {
448 logit(LOG_ERR, "%s: vltime (%lld) for "
449 "%s/%d on %s is out of range",
450 __func__, (long long)val64,
451 addr, pfx->prefixlen, intface);
452 goto errexit;
453 }
454 pfx->validlifetime = (uint32_t)val64;
455
456 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
457 if (agetflag(entbuf)) {
458 struct timespec now;
459 prog_clock_gettime(CLOCK_MONOTONIC, &now);
460 pfx->vltimeexpire =
461 now.tv_sec + pfx->validlifetime;
462 }
463
464 makeentry(entbuf, sizeof(entbuf), i, "pltime");
465 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
466 if (val64 < 0 || val64 > 0xffffffff) {
467 logit(LOG_ERR,
468 "%s: pltime (%lld) for %s/%d on %s "
469 "is out of range",
470 __func__, (long long)val64,
471 addr, pfx->prefixlen, intface);
472 goto errexit;
473 }
474 pfx->preflifetime = (uint32_t)val64;
475
476 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
477 if (agetflag(entbuf)) {
478 struct timespec now;
479 prog_clock_gettime(CLOCK_MONOTONIC, &now);
480 pfx->pltimeexpire =
481 now.tv_sec + pfx->preflifetime;
482 }
483 }
484 if (TAILQ_FIRST(&tmp->prefix) == NULL && !agetflag("noifprefix"))
485 get_prefix(tmp);
486
487 MAYHAVE(val64, "mtu", 0);
488 if (val64 < 0 || val64 > 0xffffffff) {
489 logit(LOG_ERR,
490 "%s: mtu (%" PRIi64 ") on %s out of range",
491 __func__, val64, intface);
492 goto errexit;
493 }
494 tmp->linkmtu = (uint32_t)val64;
495 if (tmp->linkmtu == 0) {
496 char *mtustr;
497
498 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
499 strcmp(mtustr, "auto") == 0)
500 tmp->linkmtu = tmp->phymtu;
501 }
502 else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
503 logit(LOG_ERR,
504 "%s: advertised link mtu (%d) on %s is invalid (must "
505 "be between least MTU (%d) and physical link MTU (%d)",
506 __func__, tmp->linkmtu, intface,
507 IPV6_MMTU, tmp->phymtu);
508 goto errexit;
509 }
510
511 /* route information */
512 for (i = -1; i < MAXROUTE; i++) {
513 struct rtinfo *rti;
514 char oentbuf[256];
515
516 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
517 addr = (char *)agetstr(entbuf, &bp);
518 if (addr == NULL) {
519 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
520 addr = (char *)agetstr(oentbuf, &bp);
521 if (addr) {
522 fprintf(stderr, "%s was obsoleted. Use %s.\n",
523 oentbuf, entbuf);
524 }
525 }
526 if (addr == NULL)
527 continue;
528
529 ELM_MALLOC(rti);
530 memset(rti, 0, sizeof(*rti));
531
532 /* link into chain */
533 TAILQ_INSERT_TAIL(&tmp->route, rti, next);
534
535 if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) {
536 logit(LOG_ERR, "%s: inet_pton failed for %s",
537 __func__, addr);
538 goto errexit;
539 }
540 #if 0
541 /*
542 * XXX: currently there's no restriction in route information
543 * prefix according to
544 * draft-ietf-ipngwg-router-selection-00.txt.
545 * However, I think the similar restriction be necessary.
546 */
547 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
548 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
549 logit(LOG_ERR,
550 "%s: multicast route (%s) must "
551 "not be advertised on %s",
552 __func__, addr, intface);
553 goto errexit;
554 }
555 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
556 logit(LOG_NOTICE,
557 "%s: link-local route (%s) will "
558 "be advertised on %s",
559 __func__, addr, intface);
560 goto errexit;
561 }
562 #endif
563
564 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
565 /* XXX: 256 is a magic number for compatibility check. */
566 MAYHAVE(val, entbuf, 256);
567 if (val == 256) {
568 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
569 MAYHAVE(val, oentbuf, 256);
570 if (val != 256) {
571 fprintf(stderr, "%s was obsoleted. Use %s.\n",
572 oentbuf, entbuf);
573 } else
574 val = 64;
575 }
576 if (val < 0 || val > 128) {
577 logit(LOG_ERR, "%s: prefixlen (%d) for %s on %s "
578 "out of range",
579 __func__, val, addr, intface);
580 goto errexit;
581 }
582 rti->prefixlen = (int)val;
583
584 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
585 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
586 val = 0;
587 if (strchr(flagstr, 'h'))
588 val |= ND_RA_FLAG_RTPREF_HIGH;
589 if (strchr(flagstr, 'l')) {
590 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
591 logit(LOG_ERR,
592 "%s: the \'h\' and \'l\' route"
593 " preferences are exclusive",
594 __func__);
595 goto errexit;
596 }
597 val |= ND_RA_FLAG_RTPREF_LOW;
598 }
599 } else
600 MAYHAVE(val, entbuf, 256); /* XXX */
601 if (val == 256) {
602 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
603 MAYHAVE(val, oentbuf, 256);
604 if (val != 256) {
605 fprintf(stderr, "%s was obsoleted. Use %s.\n",
606 oentbuf, entbuf);
607 } else
608 val = 0;
609 }
610 rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
611 if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) {
612 logit(LOG_ERR, "%s: invalid route preference (%02x) "
613 "for %s/%d on %s",
614 __func__, rti->rtpref, addr,
615 rti->prefixlen, intface);
616 goto errexit;
617 }
618
619 /*
620 * Since the spec does not a default value, we should make
621 * this entry mandatory. However, FreeBSD 4.4 has shipped
622 * with this field being optional, we use the router lifetime
623 * as an ad-hoc default value with a warning message.
624 */
625 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
626 MAYHAVE(val64, entbuf, -1);
627 if (val64 == -1) {
628 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
629 MAYHAVE(val64, oentbuf, -1);
630 if (val64 != -1) {
631 fprintf(stderr, "%s was obsoleted. Use %s.\n",
632 oentbuf, entbuf);
633 } else {
634 fprintf(stderr, "%s should be specified "
635 "for interface %s.\n",
636 entbuf, intface);
637 val64 = tmp->lifetime;
638 }
639 }
640 if (val64 < 0 || val64 > 0xffffffff) {
641 logit(LOG_ERR, "%s: route lifetime (%lld) for "
642 "%s/%d on %s out of range", __func__,
643 (long long)val64, addr, rti->prefixlen, intface);
644 goto errexit;
645 }
646 rti->ltime = (uint32_t)val64;
647 }
648
649 /* RDNSS */
650 for (i = -1; i < MAXRDNSS; i++) {
651 struct rdnss_addr *rdnsa;
652
653 makeentry(entbuf, sizeof(entbuf), i, "rdnss");
654 addr = (char *)agetstr(entbuf, &bp);
655 if (addr == NULL)
656 continue;
657
658 ELM_MALLOC(rdnss);
659 TAILQ_INSERT_TAIL(&tmp->rdnss, rdnss, next);
660 TAILQ_INIT(&rdnss->list);
661
662 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
663 c = strcspn(ap, ",");
664 strncpy(abuf, ap, c);
665 abuf[c] = '\0';
666 ELM_MALLOC(rdnsa);
667 TAILQ_INSERT_TAIL(&rdnss->list, rdnsa, next);
668 if (inet_pton(AF_INET6, abuf, &rdnsa->addr) != 1) {
669 logit(LOG_ERR, "%s: inet_pton failed for %s",
670 __func__, addr);
671 goto errexit;
672 }
673 }
674
675 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
676 MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2);
677 if (val64 < 0 || val64 > 0xffffffff) {
678 logit(LOG_ERR, "%s: %s (%lld) on %s is invalid",
679 __func__, entbuf, (long long)val64, intface);
680 goto errexit;
681 }
682 rdnss->lifetime = (uint32_t)val64;
683
684 }
685
686 /* DNSSL */
687 TAILQ_INIT(&tmp->dnssl);
688 for (i = -1; i < MAXDNSSL; i++) {
689 struct dnssl_domain *dnsd;
690
691 makeentry(entbuf, sizeof(entbuf), i, "dnssl");
692 addr = (char *)agetstr(entbuf, &bp);
693 if (addr == NULL)
694 continue;
695
696 ELM_MALLOC(dnssl);
697 TAILQ_INSERT_TAIL(&tmp->dnssl, dnssl, next);
698 TAILQ_INIT(&dnssl->list);
699
700 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
701 c = strcspn(ap, ",");
702 strncpy(abuf, ap, c);
703 abuf[c] = '\0';
704 ELM_MALLOC(dnsd);
705 TAILQ_INSERT_TAIL(&dnssl->list, dnsd, next);
706 dnsd->len = encode_domain(dnsd->domain, abuf);
707 }
708
709 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
710 MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2);
711 if (val64 < 0 || val64 > 0xffffffff) {
712 logit(LOG_ERR, "%s: %s (%lld) on %s is invalid",
713 __func__, entbuf, (long long)val64, intface);
714 goto errexit;
715 }
716 dnssl->lifetime = (uint32_t)val64;
717
718 }
719
720 TAILQ_FOREACH(rai, &ralist, next) {
721 if (rai->ifindex == tmp->ifindex) {
722 TAILQ_REMOVE(&ralist, rai, next);
723 if (Cflag) {
724 free_rainfo(rai);
725 rai = NULL;
726 break;
727 }
728 /* If we already have a leaving RA use that
729 * as this config hasn't been advertised */
730 if (rai->leaving) {
731 tmp->leaving = rai->leaving;
732 free_rainfo(rai);
733 rai = tmp->leaving;
734 rai->leaving_for = tmp;
735 break;
736 }
737 rai->lifetime = 0;
738 TAILQ_FOREACH(rdnss, &rai->rdnss, next)
739 rdnss->lifetime = 0;
740 TAILQ_FOREACH(dnssl, &rai->dnssl, next)
741 dnssl->lifetime = 0;
742 rai->leaving_for = tmp;
743 tmp->leaving = rai;
744 rai->initcounter = MAX_INITIAL_RTR_ADVERTISEMENTS;
745 rai->mininterval = MIN_DELAY_BETWEEN_RAS;
746 rai->maxinterval = MIN_DELAY_BETWEEN_RAS;
747 rai->leaving_adv = MAX_FINAL_RTR_ADVERTISEMENTS;
748 if (rai->timer == NULL)
749 rai->timer = rtadvd_add_timer(ra_timeout,
750 ra_timer_update,
751 rai, rai);
752 ra_timer_update((void *)rai, &rai->timer->tm);
753 rtadvd_set_timer(&rai->timer->tm, rai->timer);
754 break;
755 }
756 }
757
758 /* okey */
759 TAILQ_INSERT_TAIL(&ralist, tmp, next);
760
761 /* construct the sending packet */
762 make_packet(tmp);
763
764 /* set timer */
765 if (rai)
766 return;
767 tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
768 tmp, tmp);
769 ra_timer_set_short_delay(tmp, tmp->timer);
770 tmp->timer_sol = rtadvd_add_timer(ra_timeout_sol, NULL, tmp, NULL);
771
772 return;
773
774 errexit:
775 if (exithard)
776 exit(EXIT_FAILURE);
777 free_rainfo(tmp);
778 }
779
780 void
get_prefix(struct rainfo * rai)781 get_prefix(struct rainfo *rai)
782 {
783 struct ifaddrs *ifap, *ifa;
784 struct prefix *pp;
785 struct in6_addr *a;
786 unsigned char *p, *ep, *m, *lim;
787 char ntopbuf[INET6_ADDRSTRLEN];
788
789 if (getifaddrs(&ifap) < 0) {
790 logit(LOG_ERR,
791 "%s: can't get interface addresses",
792 __func__);
793 exit(EXIT_FAILURE);
794 }
795
796 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
797 int plen;
798
799 if (strcmp(ifa->ifa_name, rai->ifname) != 0)
800 continue;
801 if (ifa->ifa_addr->sa_family != AF_INET6)
802 continue;
803 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
804 if (IN6_IS_ADDR_LINKLOCAL(a))
805 continue;
806 /* get prefix length */
807 m = (unsigned char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
808 lim = (unsigned char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
809 plen = prefixlen(m, lim);
810 if (plen <= 0 || plen > 128) {
811 logit(LOG_ERR, "%s: failed to get prefixlen "
812 "or prefix is invalid",
813 __func__);
814 exit(EXIT_FAILURE);
815 }
816 if (plen == 128) /* XXX */
817 continue;
818 if (find_prefix(rai, a, plen)) {
819 /* ignore a duplicated prefix. */
820 continue;
821 }
822
823 /* allocate memory to store prefix info. */
824 if ((pp = calloc(1, sizeof(*pp))) == NULL) {
825 logit(LOG_ERR,
826 "%s: can't get allocate buffer for prefix",
827 __func__);
828 exit(EXIT_FAILURE);
829 }
830
831 /* set prefix, sweep bits outside of prefixlen */
832 pp->prefixlen = plen;
833 memcpy(&pp->prefix, a, sizeof(*a));
834 if (1)
835 {
836 p = (unsigned char *)&pp->prefix;
837 ep = (unsigned char *)(&pp->prefix + 1);
838 while (m < lim && p < ep)
839 *p++ &= *m++;
840 while (p < ep)
841 *p++ = 0x00;
842 }
843 if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
844 sizeof(ntopbuf))) {
845 logit(LOG_ERR, "%s: inet_ntop failed", __func__);
846 exit(EXIT_FAILURE);
847 }
848 logit(LOG_DEBUG,
849 "%s: add %s/%d to prefix list on %s",
850 __func__, ntopbuf, pp->prefixlen, rai->ifname);
851
852 /* set other fields with protocol defaults */
853 pp->validlifetime = DEF_ADVVALIDLIFETIME;
854 pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
855 pp->onlinkflg = 1;
856 pp->autoconfflg = 1;
857 pp->origin = PREFIX_FROM_KERNEL;
858 pp->rainfo = rai;
859
860 /* link into chain */
861 TAILQ_INSERT_TAIL(&rai->prefix, pp, next);
862 rai->pfxs++;
863 }
864
865 freeifaddrs(ifap);
866 }
867
868 static void
makeentry(char * buf,size_t len,int id,const char * string)869 makeentry(char *buf, size_t len, int id, const char *string)
870 {
871
872 if (id < 0)
873 strlcpy(buf, string, len);
874 else
875 snprintf(buf, len, "%s%d", string, id);
876 }
877
878 /*
879 * Delete a prefix to the list of specified interface and reconstruct
880 * the outgoing packet.
881 * The prefix must be in the list.
882 */
883 void
delete_prefix(struct prefix * prefix)884 delete_prefix(struct prefix *prefix)
885 {
886 char ntopbuf[INET6_ADDRSTRLEN];
887 struct rainfo *rai = prefix->rainfo;
888
889 TAILQ_REMOVE(&rai->prefix, prefix, next);
890 rai->pfxs--;
891 logit(LOG_DEBUG, "%s: prefix %s/%d was deleted on %s",
892 __func__, inet_ntop(AF_INET6, &prefix->prefix,
893 ntopbuf, INET6_ADDRSTRLEN),
894 prefix->prefixlen, rai->ifname);
895 rtadvd_remove_timer(&prefix->timer);
896 free(prefix);
897 }
898
899 void
invalidate_prefix(struct prefix * prefix)900 invalidate_prefix(struct prefix *prefix)
901 {
902 char ntopbuf[INET6_ADDRSTRLEN];
903 struct timespec timo;
904 struct rainfo *rai = prefix->rainfo;
905
906 if (prefix->timer) { /* sanity check */
907 logit(LOG_ERR,
908 "%s: assumption failure: timer already exists",
909 __func__);
910 exit(EXIT_FAILURE);
911 }
912
913 logit(LOG_DEBUG, "%s: prefix %s/%d was invalidated on %s, "
914 "will expire in %ld seconds", __func__,
915 inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
916 prefix->prefixlen, rai->ifname, (long)prefix_timo);
917
918 /* set the expiration timer */
919 prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
920 if (prefix->timer == NULL) {
921 logit(LOG_ERR, "%s: failed to add a timer for a prefix. "
922 "remove the prefix", __func__);
923 delete_prefix(prefix);
924 }
925 timo.tv_sec = prefix_timo;
926 timo.tv_nsec = 0;
927 rtadvd_set_timer(&timo, prefix->timer);
928 }
929
930 static struct rtadvd_timer *
prefix_timeout(void * arg)931 prefix_timeout(void *arg)
932 {
933 struct prefix *prefix = (struct prefix *)arg;
934
935 delete_prefix(prefix);
936
937 return(NULL);
938 }
939
940 void
update_prefix(struct prefix * prefix)941 update_prefix(struct prefix * prefix)
942 {
943 char ntopbuf[INET6_ADDRSTRLEN];
944 struct rainfo *rai = prefix->rainfo;
945
946 if (prefix->timer == NULL) { /* sanity check */
947 logit(LOG_ERR,
948 "%s: assumption failure: timer does not exist",
949 __func__);
950 exit(EXIT_FAILURE);
951 }
952
953 logit(LOG_DEBUG, "%s: prefix %s/%d was re-enabled on %s",
954 __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
955 INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
956
957 /* stop the expiration timer */
958 rtadvd_remove_timer(&prefix->timer);
959 }
960
961 /*
962 * Add a prefix to the list of specified interface and reconstruct
963 * the outgoing packet.
964 * The prefix must not be in the list.
965 * XXX: other parameters of the prefix(e.g. lifetime) should be
966 * able to be specified.
967 */
968 void
add_prefix(struct rainfo * rai,int ifindex,const struct in6_addr * addr,int plen)969 add_prefix(struct rainfo *rai, int ifindex, const struct in6_addr *addr,
970 int plen)
971 {
972 struct prefix *prefix;
973 char ntopbuf[INET6_ADDRSTRLEN];
974
975 if ((prefix = calloc(1, sizeof(*prefix))) == NULL) {
976 logit(LOG_ERR, "%s: memory allocation failed",
977 __func__);
978 return; /* XXX: error or exit? */
979 }
980 prefix->prefix = *addr;
981 prefix->prefixlen = plen;
982 prefix->validlifetime = DEF_ADVVALIDLIFETIME;
983 prefix->preflifetime = DEF_ADVPREFERREDLIFETIME;
984 prefix->onlinkflg = 1;
985 prefix->autoconfflg = 0;
986 prefix->origin = PREFIX_FROM_DYNAMIC;
987
988 prefix->rainfo = rai;
989 TAILQ_INSERT_TAIL(&rai->prefix, prefix, next);
990 rai->pfxs++;
991
992 logit(LOG_DEBUG, "%s: new prefix %s/%d was added on %s",
993 __func__, inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
994 plen, rai->ifname);
995
996 /* free the previous packet */
997 free(rai->ra_data);
998 rai->ra_data = NULL;
999
1000 /* reconstruct the packet */
1001 make_packet(rai);
1002 }
1003
1004 void
make_packet(struct rainfo * rainfo)1005 make_packet(struct rainfo *rainfo)
1006 {
1007 size_t packlen, lladdroptlen = 0;
1008 char *buf;
1009 struct nd_router_advert *ra;
1010 struct nd_opt_prefix_info *ndopt_pi;
1011 struct nd_opt_mtu *ndopt_mtu;
1012 struct prefix *pfx;
1013 struct nd_opt_route_info *ndopt_rti;
1014 struct rtinfo *rti;
1015 struct nd_opt_rdnss *ndopt_rdnss;
1016 struct rdnss *rdns;
1017 struct rdnss_addr *rdnsa;
1018 struct nd_opt_dnssl *ndopt_dnssl;
1019 struct dnssl *dnsl;
1020 struct dnssl_domain *dnsd;
1021 size_t len, plen;
1022
1023 /* calculate total length */
1024 packlen = sizeof(struct nd_router_advert);
1025 if (rainfo->advlinkopt) {
1026 if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
1027 logit(LOG_INFO,
1028 "%s: link-layer address option has"
1029 " null length on %s. Treat as not included.",
1030 __func__, rainfo->ifname);
1031 rainfo->advlinkopt = 0;
1032 }
1033 packlen += lladdroptlen;
1034 }
1035 if (TAILQ_FIRST(&rainfo->prefix) != NULL)
1036 packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
1037 if (rainfo->linkmtu)
1038 packlen += sizeof(struct nd_opt_mtu);
1039 TAILQ_FOREACH(rti, &rainfo->route, next)
1040 packlen += sizeof(struct nd_opt_route_info) +
1041 ((rti->prefixlen + 0x3f) >> 6) * 8;
1042
1043 TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1044 packlen += sizeof(struct nd_opt_rdnss);
1045 TAILQ_FOREACH(rdnsa, &rdns->list, next)
1046 packlen += sizeof(rdnsa->addr);
1047 }
1048 TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1049 packlen += sizeof(struct nd_opt_dnssl);
1050 len = 0;
1051 TAILQ_FOREACH(dnsd, &dnsl->list, next)
1052 len += dnsd->len;
1053 len += len % 8 ? 8 - len % 8 : 0;
1054 packlen += len;
1055 }
1056
1057 /* allocate memory for the packet */
1058 if ((buf = realloc(rainfo->ra_data, packlen)) == NULL) {
1059 logit(LOG_ERR,
1060 "%s: can't get enough memory for an RA packet %m",
1061 __func__);
1062 exit(EXIT_FAILURE);
1063 }
1064 rainfo->ra_data = buf;
1065 /* XXX: what if packlen > 576? */
1066 rainfo->ra_datalen = packlen;
1067 #define CHECKLEN(size) \
1068 do { \
1069 if (buf + size > rainfo->ra_data + packlen) { \
1070 logit(LOG_ERR, \
1071 "%s: @%d RA packet does not fit in %zu",\
1072 __func__, __LINE__, packlen); \
1073 exit(EXIT_FAILURE); \
1074 } \
1075 } while (0)
1076 /*
1077 * construct the packet
1078 */
1079 CHECKLEN(sizeof(*ra));
1080 ra = (struct nd_router_advert *)buf;
1081 ra->nd_ra_type = ND_ROUTER_ADVERT;
1082 ra->nd_ra_code = 0;
1083 ra->nd_ra_cksum = 0;
1084 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rainfo->hoplimit);
1085 ra->nd_ra_flags_reserved = 0; /* just in case */
1086 /*
1087 * XXX: the router preference field, which is a 2-bit field, should be
1088 * initialized before other fields.
1089 */
1090 ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
1091 ra->nd_ra_flags_reserved |=
1092 rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
1093 ra->nd_ra_flags_reserved |=
1094 rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
1095 ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
1096 ra->nd_ra_reachable = htonl(rainfo->reachabletime);
1097 ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
1098 buf += sizeof(*ra);
1099
1100 if (rainfo->advlinkopt) {
1101 CHECKLEN(sizeof(struct nd_opt_hdr));
1102 lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
1103 buf += lladdroptlen;
1104 }
1105
1106 if (rainfo->linkmtu) {
1107 CHECKLEN(sizeof(*ndopt_mtu));
1108 ndopt_mtu = (struct nd_opt_mtu *)buf;
1109 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1110 ndopt_mtu->nd_opt_mtu_len = 1;
1111 ndopt_mtu->nd_opt_mtu_reserved = 0;
1112 ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
1113 buf += sizeof(struct nd_opt_mtu);
1114 }
1115
1116 TAILQ_FOREACH(pfx, &rainfo->prefix, next) {
1117 uint32_t vltime, pltime;
1118 struct timespec now;
1119
1120 CHECKLEN(sizeof(*ndopt_pi));
1121 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1122 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1123 ndopt_pi->nd_opt_pi_len = 4;
1124 ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
1125 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1126 if (pfx->onlinkflg)
1127 ndopt_pi->nd_opt_pi_flags_reserved |=
1128 ND_OPT_PI_FLAG_ONLINK;
1129 if (pfx->autoconfflg)
1130 ndopt_pi->nd_opt_pi_flags_reserved |=
1131 ND_OPT_PI_FLAG_AUTO;
1132 if (pfx->timer)
1133 vltime = 0;
1134 else {
1135 if (pfx->vltimeexpire || pfx->pltimeexpire)
1136 prog_clock_gettime(CLOCK_MONOTONIC, &now);
1137 if (pfx->vltimeexpire == 0)
1138 vltime = pfx->validlifetime;
1139 else
1140 vltime = (pfx->vltimeexpire > now.tv_sec) ?
1141 pfx->vltimeexpire - now.tv_sec : 0;
1142 }
1143 if (pfx->timer)
1144 pltime = 0;
1145 else {
1146 if (pfx->pltimeexpire == 0)
1147 pltime = pfx->preflifetime;
1148 else
1149 pltime = (pfx->pltimeexpire > now.tv_sec) ?
1150 pfx->pltimeexpire - now.tv_sec : 0;
1151 }
1152 if (vltime < pltime) {
1153 /*
1154 * this can happen if vltime is decrement but pltime
1155 * is not.
1156 */
1157 pltime = vltime;
1158 }
1159 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1160 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1161 ndopt_pi->nd_opt_pi_reserved2 = 0;
1162 ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1163
1164 buf += sizeof(struct nd_opt_prefix_info);
1165 }
1166
1167 TAILQ_FOREACH(rti, &rainfo->route, next) {
1168 uint8_t psize = (rti->prefixlen + 0x3f) >> 6;
1169
1170 CHECKLEN(sizeof(*ndopt_rti));
1171 ndopt_rti = (struct nd_opt_route_info *)buf;
1172 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1173 ndopt_rti->nd_opt_rti_len = 1 + psize;
1174 ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1175 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1176 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1177 memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1178 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1179 }
1180
1181 TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1182 CHECKLEN(sizeof(*ndopt_rdnss));
1183 ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1184 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1185 ndopt_rdnss->nd_opt_rdnss_len = 1;
1186 ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1187 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdns->lifetime);
1188 buf += sizeof(*ndopt_rdnss);
1189
1190 TAILQ_FOREACH(rdnsa, &rdns->list, next) {
1191 CHECKLEN(sizeof(rdnsa->addr));
1192 memcpy(buf, &rdnsa->addr, sizeof(rdnsa->addr));
1193 ndopt_rdnss->nd_opt_rdnss_len += 2;
1194 buf += sizeof(rdnsa->addr);
1195 }
1196 }
1197
1198 TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1199 CHECKLEN(sizeof(*ndopt_dnssl));
1200 ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1201 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1202 ndopt_dnssl->nd_opt_dnssl_len = 0;
1203 ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1204 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dnsl->lifetime);
1205 buf += sizeof(*ndopt_dnssl);
1206
1207 TAILQ_FOREACH(dnsd, &dnsl->list, next) {
1208 CHECKLEN(dnsd->len);
1209 memcpy(buf, dnsd->domain, dnsd->len);
1210 buf += dnsd->len;
1211 }
1212 /* Ensure our length is padded correctly */
1213 len = buf - (char *)ndopt_dnssl;
1214 plen = len % 8 ? 8 - len % 8 : 0;
1215 CHECKLEN(plen);
1216 memset(buf, 0, plen);
1217 buf += plen;
1218 ndopt_dnssl->nd_opt_dnssl_len = (len + plen) / 8;
1219 }
1220 memset(buf, 0, packlen - (buf - rainfo->ra_data));
1221 }
1222
1223 static int
getinet6sysctl(int code)1224 getinet6sysctl(int code)
1225 {
1226 const int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, code };
1227 int value;
1228 size_t size;
1229
1230 size = sizeof(value);
1231 if (prog_sysctl(mib, __arraycount(mib), &value, &size, NULL, 0) == -1) {
1232 logit(LOG_ERR, "%s: failed to get ip6 sysctl(%d): %m",
1233 __func__, code);
1234 return -1;
1235 }
1236 return value;
1237 }
1238