2 /* $KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $ */
5 * Copyright (C) 1998 WIDE Project.
6 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
40 #include <net/if_var.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_var.h>
46 #include <netinet/ip6.h>
47 #include <netinet6/ip6_var.h>
48 #include <netinet/icmp6.h>
49 #include <netinet6/nd6.h>
51 #include <arpa/inet.h>
70 /* label of tcapcode + number + domain name + zero octet */
71 static char entbuf[10 + 3 + NI_MAXHOST + 1];
72 static char oentbuf[10 + 3 + NI_MAXHOST + 1];
73 static char abuf[DNAME_LABELENC_MAXLEN];
75 static time_t prefix_timo = (60 * 120); /* 2 hours.
76 * XXX: should be configurable. */
78 static struct rtadvd_timer *prefix_timeout(void *);
79 static void makeentry(char *, size_t, int, const char *);
80 static size_t dname_labelenc(char *, const char *);
82 /* Encode domain name label encoding in RFC 1035 Section 3.1 */
84 dname_labelenc(char *dst, const char *src)
93 /* Length fields per 63 octets + '\0' (<= DNAME_LABELENC_MAXLEN) */
94 memset(dst, 0, len + len / 64 + 1 + 1);
96 syslog(LOG_DEBUG, "<%s> labelenc = %s", __func__, src);
97 while (src && (len = strlen(src)) != 0) {
98 /* Put a length field with 63 octet limitation first. */
101 *dst++ = len = MIN(63, len);
103 *dst++ = len = MIN(63, p - src);
104 /* Copy 63 octets at most. */
105 memcpy(dst, src, len);
107 if (p == NULL) /* the last label */
111 /* Always need a 0-length label at the tail. */
114 syslog(LOG_DEBUG, "<%s> labellen = %td", __func__, dst - dst_origin);
115 return (dst - dst_origin);
118 #define MUSTHAVE(var, cap) \
121 if ((t = agetnum(cap)) < 0) { \
122 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
129 #define MAYHAVE(var, cap, def) \
131 if ((var = agetnum(cap)) < 0) \
136 loadconfig_index(int idx)
138 char ifname[IFNAMSIZ];
140 syslog(LOG_DEBUG, "<%s> enter", __func__);
142 if (if_indextoname(idx, ifname) != NULL)
143 return (loadconfig_ifname(ifname));
149 loadconfig_ifname(char *ifname)
153 syslog(LOG_DEBUG, "<%s> enter", __func__);
155 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
156 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
157 /* NULL means all IFs will be processed. */
158 if (ifname != NULL &&
159 strcmp(ifi->ifi_ifname, ifname) != 0)
162 if (!ifi->ifi_persist) {
164 "<%s> %s is not a target interface. "
165 "Ignored at this moment.", __func__,
170 if (ifi->ifi_ifindex == 0) {
172 "<%s> %s not found. "
173 "Ignored at this moment.", __func__,
177 if (getconfig(ifi) == NULL) {
179 "<%s> invalid configuration for %s. "
180 "Ignored at this moment.", __func__,
189 rm_ifinfo_index(int idx)
193 ifi = if_indextoifinfo(idx);
195 syslog(LOG_ERR, "<%s>: ifinfo not found (idx=%d)",
200 return (rm_ifinfo(ifi));
204 rm_ifinfo(struct ifinfo *ifi)
208 syslog(LOG_DEBUG, "<%s> enter (%s).", __func__, ifi->ifi_ifname);
209 switch (ifi->ifi_state) {
210 case IFI_STATE_UNCONFIGURED:
214 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
216 "<%s> ifname=%s marked as UNCONFIGURED.",
217 __func__, ifi->ifi_ifname);
219 /* XXX: No MC leaving here becasue index is disappeared */
221 /* Inactivate timer */
222 rtadvd_remove_timer(ifi->ifi_ra_timer);
223 ifi->ifi_ra_timer = NULL;
228 if (!ifi->ifi_persist) {
229 TAILQ_REMOVE(&ifilist, ifi, ifi_next);
230 syslog(LOG_DEBUG, "<%s>: ifinfo (idx=%d) removed.",
231 __func__, ifi->ifi_ifindex);
234 /* recreate an empty entry */
235 update_persist_ifinfo(&ifilist, ifi->ifi_ifname);
236 syslog(LOG_DEBUG, "<%s>: ifname=%s is persistent.",
237 __func__, ifi->ifi_ifname);
240 /* clean up rai if any */
241 switch (ifi->ifi_state) {
242 case IFI_STATE_CONFIGURED:
243 if (ifi->ifi_rainfo != NULL) {
244 error = rm_rainfo(ifi->ifi_rainfo);
247 ifi->ifi_rainfo = NULL;
250 case IFI_STATE_TRANSITIVE:
251 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
252 if (ifi->ifi_rainfo != NULL) {
253 error = rm_rainfo(ifi->ifi_rainfo);
256 ifi->ifi_rainfo = NULL;
257 ifi->ifi_rainfo_trans = NULL;
260 if (ifi->ifi_rainfo != NULL) {
261 error = rm_rainfo(ifi->ifi_rainfo);
264 ifi->ifi_rainfo = NULL;
266 if (ifi->ifi_rainfo_trans != NULL) {
267 error = rm_rainfo(ifi->ifi_rainfo_trans);
270 ifi->ifi_rainfo_trans = NULL;
275 syslog(LOG_DEBUG, "<%s> leave (%s).", __func__, ifi->ifi_ifname);
280 rm_rainfo(struct rainfo *rai)
283 struct soliciter *sol;
285 struct rdnss_addr *rdna;
289 syslog(LOG_DEBUG, "<%s>: enter", __func__);
291 TAILQ_REMOVE(&railist, rai, rai_next);
292 if (rai->rai_ifinfo != NULL)
293 syslog(LOG_DEBUG, "<%s>: rainfo (idx=%d) removed.",
294 __func__, rai->rai_ifinfo->ifi_ifindex);
296 if (rai->rai_ra_data != NULL)
297 free(rai->rai_ra_data);
299 while ((pfx = TAILQ_FIRST(&rai->rai_prefix)) != NULL)
301 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
302 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
305 while ((rdn = TAILQ_FIRST(&rai->rai_rdnss)) != NULL) {
306 TAILQ_REMOVE(&rai->rai_rdnss, rdn, rd_next);
307 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
308 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
313 while ((dns = TAILQ_FIRST(&rai->rai_dnssl)) != NULL) {
314 TAILQ_REMOVE(&rai->rai_dnssl, dns, dn_next);
317 while ((rti = TAILQ_FIRST(&rai->rai_route)) != NULL) {
318 TAILQ_REMOVE(&rai->rai_route, rti, rti_next);
322 syslog(LOG_DEBUG, "<%s>: leave", __func__);
328 getconfig(struct ifinfo *ifi)
334 struct rainfo *rai_old;
339 char *addr, *flagstr;
341 if (ifi == NULL) /* if does not exist */
344 if (ifi->ifi_state == IFI_STATE_TRANSITIVE &&
345 ifi->ifi_rainfo == NULL) {
346 syslog(LOG_INFO, "<%s> %s is shutting down. Skipped.",
347 __func__, ifi->ifi_ifname);
351 if ((stat = agetent(tbuf, ifi->ifi_ifname)) <= 0) {
352 memset(tbuf, 0, sizeof(tbuf));
354 "<%s> %s isn't defined in the configuration file"
355 " or the configuration file doesn't exist."
356 " Treat it as default",
357 __func__, ifi->ifi_ifname);
360 ELM_MALLOC(rai, exit(1));
361 TAILQ_INIT(&rai->rai_prefix);
362 TAILQ_INIT(&rai->rai_route);
363 TAILQ_INIT(&rai->rai_rdnss);
364 TAILQ_INIT(&rai->rai_dnssl);
365 TAILQ_INIT(&rai->rai_soliciter);
366 rai->rai_ifinfo = ifi;
368 /* gather on-link prefixes from the network interfaces. */
369 if (agetflag("noifprefix"))
370 rai->rai_advifprefix = 0;
372 rai->rai_advifprefix = 1;
374 /* get interface information */
375 if (agetflag("nolladdr"))
376 rai->rai_advlinkopt = 0;
378 rai->rai_advlinkopt = 1;
379 if (rai->rai_advlinkopt) {
380 if (ifi->ifi_sdl.sdl_type == 0) {
382 "<%s> can't get information of %s",
383 __func__, ifi->ifi_ifname);
384 goto getconfig_free_rai;
389 * set router configuration variables.
391 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
392 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
394 "<%s> maxinterval (%" PRIu32 ") on %s is invalid "
395 "(must be between %u and %u)", __func__, val,
396 ifi->ifi_ifname, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
397 goto getconfig_free_rai;
399 rai->rai_maxinterval = (uint16_t)val;
401 MAYHAVE(val, "mininterval", rai->rai_maxinterval/3);
402 if ((uint16_t)val < MIN_MININTERVAL ||
403 (uint16_t)val > (rai->rai_maxinterval * 3) / 4) {
405 "<%s> mininterval (%" PRIu32 ") on %s is invalid "
406 "(must be between %d and %d)",
407 __func__, val, ifi->ifi_ifname, MIN_MININTERVAL,
408 (rai->rai_maxinterval * 3) / 4);
409 goto getconfig_free_rai;
411 rai->rai_mininterval = (uint16_t)val;
413 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
414 rai->rai_hoplimit = val & 0xff;
416 if ((flagstr = (char *)agetstr("raflags", &bp))) {
418 if (strchr(flagstr, 'm'))
419 val |= ND_RA_FLAG_MANAGED;
420 if (strchr(flagstr, 'o'))
421 val |= ND_RA_FLAG_OTHER;
422 if (strchr(flagstr, 'h'))
423 val |= ND_RA_FLAG_RTPREF_HIGH;
424 if (strchr(flagstr, 'l')) {
425 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
426 syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
427 " router flags are exclusive", __func__);
428 goto getconfig_free_rai;
430 val |= ND_RA_FLAG_RTPREF_LOW;
433 MAYHAVE(val, "raflags", 0);
435 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED;
436 rai->rai_otherflg = val & ND_RA_FLAG_OTHER;
437 #ifndef ND_RA_FLAG_RTPREF_MASK
438 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
439 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
441 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
442 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) {
443 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
444 __func__, rai->rai_rtpref, ifi->ifi_ifname);
445 goto getconfig_free_rai;
448 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3);
449 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval ||
450 (uint16_t)val > MAXROUTERLIFETIME)) {
452 "<%s> router lifetime (%" PRIu32 ") on %s is invalid "
453 "(must be 0 or between %d and %d)",
454 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval,
456 goto getconfig_free_rai;
458 rai->rai_lifetime = val & 0xffff;
460 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
461 if (val < 0 || val > MAXREACHABLETIME) {
463 "<%s> reachable time (%" PRIu32 ") on %s is invalid "
464 "(must be no greater than %d)",
465 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME);
466 goto getconfig_free_rai;
468 rai->rai_reachabletime = (uint32_t)val;
470 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
471 if (val64 < 0 || val64 > 0xffffffff) {
472 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range",
473 __func__, val64, ifi->ifi_ifname);
474 goto getconfig_free_rai;
476 rai->rai_retranstimer = (uint32_t)val64;
478 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
480 "<%s> mobile-ip6 configuration not supported",
482 goto getconfig_free_rai;
484 /* prefix information */
487 * This is an implementation specific parameter to consider
488 * link propagation delays and poorly synchronized clocks when
489 * checking consistency of advertised lifetimes.
491 MAYHAVE(val, "clockskew", 0);
492 rai->rai_clockskew = val;
495 for (i = -1; i < MAXPREFIX; i++) {
498 makeentry(entbuf, sizeof(entbuf), i, "addr");
499 addr = (char *)agetstr(entbuf, &bp);
503 /* allocate memory to store prefix information */
504 ELM_MALLOC(pfx, exit(1));
505 pfx->pfx_rainfo = rai;
506 pfx->pfx_origin = PREFIX_FROM_CONFIG;
508 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) {
510 "<%s> inet_pton failed for %s",
512 goto getconfig_free_pfx;
514 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) {
516 "<%s> multicast prefix (%s) must "
517 "not be advertised on %s",
518 __func__, addr, ifi->ifi_ifname);
519 goto getconfig_free_pfx;
521 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix))
523 "<%s> link-local prefix (%s) will be"
525 __func__, addr, ifi->ifi_ifname);
527 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
528 MAYHAVE(val, entbuf, 64);
529 if (val < 0 || val > 128) {
530 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s "
531 "on %s out of range",
532 __func__, val, addr, ifi->ifi_ifname);
533 goto getconfig_free_pfx;
535 pfx->pfx_prefixlen = (int)val;
537 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
538 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
540 if (strchr(flagstr, 'l'))
541 val |= ND_OPT_PI_FLAG_ONLINK;
542 if (strchr(flagstr, 'a'))
543 val |= ND_OPT_PI_FLAG_AUTO;
546 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
548 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
549 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
551 makeentry(entbuf, sizeof(entbuf), i, "vltime");
552 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
553 if (val64 < 0 || val64 > 0xffffffff) {
554 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for "
555 "%s/%d on %s is out of range",
557 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
558 goto getconfig_free_pfx;
560 pfx->pfx_validlifetime = (uint32_t)val64;
562 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
563 if (agetflag(entbuf)) {
565 gettimeofday(&now, 0);
566 pfx->pfx_vltimeexpire =
567 now.tv_sec + pfx->pfx_validlifetime;
570 makeentry(entbuf, sizeof(entbuf), i, "pltime");
571 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
572 if (val64 < 0 || val64 > 0xffffffff) {
574 "<%s> pltime (%" PRIu64 ") for %s/%d on %s "
577 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
578 goto getconfig_free_pfx;
580 pfx->pfx_preflifetime = (uint32_t)val64;
582 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
583 if (agetflag(entbuf)) {
585 gettimeofday(&now, 0);
586 pfx->pfx_pltimeexpire =
587 now.tv_sec + pfx->pfx_preflifetime;
589 /* link into chain */
590 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
596 if (rai->rai_advifprefix && rai->rai_pfxs == 0)
599 MAYHAVE(val64, "mtu", 0);
600 if (val < 0 || val64 > 0xffffffff) {
602 "<%s> mtu (%" PRIu64 ") on %s out of range",
603 __func__, val64, ifi->ifi_ifname);
604 goto getconfig_free_rai;
606 rai->rai_linkmtu = (uint32_t)val64;
607 if (rai->rai_linkmtu == 0) {
610 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
611 strcmp(mtustr, "auto") == 0)
612 rai->rai_linkmtu = ifi->ifi_phymtu;
614 else if (rai->rai_linkmtu < IPV6_MMTU ||
615 rai->rai_linkmtu > ifi->ifi_phymtu) {
617 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must "
618 "be between least MTU (%d) and physical link MTU (%d)",
619 __func__, rai->rai_linkmtu, ifi->ifi_ifname,
620 IPV6_MMTU, ifi->ifi_phymtu);
621 goto getconfig_free_rai;
624 #ifdef SIOCSIFINFO_IN6
626 struct in6_ndireq ndi;
629 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
630 syslog(LOG_ERR, "<%s> socket: %s", __func__,
634 memset(&ndi, 0, sizeof(ndi));
635 strncpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname));
636 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0)
637 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
638 __func__, ifi->ifi_ifname, strerror(errno));
640 /* reflect the RA info to the host variables in kernel */
641 ndi.ndi.chlim = rai->rai_hoplimit;
642 ndi.ndi.retrans = rai->rai_retranstimer;
643 ndi.ndi.basereachable = rai->rai_reachabletime;
644 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0)
645 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
646 __func__, ifi->ifi_ifname, strerror(errno));
652 /* route information */
654 for (i = -1; i < MAXROUTE; i++) {
657 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
658 addr = (char *)agetstr(entbuf, &bp);
660 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
661 addr = (char *)agetstr(oentbuf, &bp);
663 fprintf(stderr, "%s was obsoleted. Use %s.\n",
669 /* allocate memory to store prefix information */
670 ELM_MALLOC(rti, exit(1));
672 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) {
673 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
675 goto getconfig_free_rti;
679 * XXX: currently there's no restriction in route information
680 * prefix according to
681 * draft-ietf-ipngwg-router-selection-00.txt.
682 * However, I think the similar restriction be necessary.
684 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
685 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
687 "<%s> multicast route (%s) must "
688 "not be advertised on %s",
689 __func__, addr, ifi->ifi_ifname);
690 goto getconfig_free_rti;
692 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
694 "<%s> link-local route (%s) will "
695 "be advertised on %s",
696 __func__, addr, ifi->ifi_ifname);
697 goto getconfig_free_rti;
701 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
702 /* XXX: 256 is a magic number for compatibility check. */
703 MAYHAVE(val, entbuf, 256);
705 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
706 MAYHAVE(val, oentbuf, 256);
708 fprintf(stderr, "%s was obsoleted. Use %s.\n",
713 if (val < 0 || val > 128) {
714 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s "
716 __func__, val, addr, ifi->ifi_ifname);
717 goto getconfig_free_rti;
719 rti->rti_prefixlen = (int)val;
721 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
722 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
724 if (strchr(flagstr, 'h'))
725 val |= ND_RA_FLAG_RTPREF_HIGH;
726 if (strchr(flagstr, 'l')) {
727 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
729 "<%s> the \'h\' and \'l\' route"
730 " preferences are exclusive",
732 goto getconfig_free_rti;
734 val |= ND_RA_FLAG_RTPREF_LOW;
737 MAYHAVE(val, entbuf, 256); /* XXX */
739 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
740 MAYHAVE(val, oentbuf, 256);
742 fprintf(stderr, "%s was obsoleted. Use %s.\n",
747 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
748 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) {
749 syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
751 __func__, rti->rti_rtpref, addr,
752 rti->rti_prefixlen, ifi->ifi_ifname);
753 goto getconfig_free_rti;
757 * Since the spec does not a default value, we should make
758 * this entry mandatory. However, FreeBSD 4.4 has shipped
759 * with this field being optional, we use the router lifetime
760 * as an ad-hoc default value with a warning message.
762 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
763 MAYHAVE(val64, entbuf, -1);
765 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
766 MAYHAVE(val64, oentbuf, -1);
768 fprintf(stderr, "%s was obsoleted. Use %s.\n",
771 fprintf(stderr, "%s should be specified "
772 "for interface %s.\n", entbuf,
774 val64 = rai->rai_lifetime;
777 if (val64 < 0 || val64 > 0xffffffff) {
778 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for "
779 "%s/%d on %s out of range", __func__,
780 val64, addr, rti->rti_prefixlen,
782 goto getconfig_free_rti;
784 rti->rti_ltime = (uint32_t)val64;
786 /* link into chain */
787 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next);
794 /* DNS server and DNS search list information */
795 for (i = -1; i < MAXRDNSSENT ; i++) {
797 struct rdnss_addr *rdna;
801 makeentry(entbuf, sizeof(entbuf), i, "rdnss");
802 addr = (char *)agetstr(entbuf, &bp);
805 ELM_MALLOC(rdn, exit(1));
807 TAILQ_INIT(&rdn->rd_list);
809 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
810 c = strcspn(ap, ",");
811 strncpy(abuf, ap, c);
813 ELM_MALLOC(rdna, goto getconfig_free_rdn);
814 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) {
815 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
818 goto getconfig_free_rdn;
820 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next);
823 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
824 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
825 if ((uint16_t)val < rai->rai_maxinterval ||
826 (uint16_t)val > rai->rai_maxinterval * 2) {
827 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
828 "(must be between %d and %d)",
829 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
830 rai->rai_maxinterval * 2);
831 goto getconfig_free_rdn;
835 /* link into chain */
836 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next);
839 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
840 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
846 for (i = -1; i < MAXDNSSLENT ; i++) {
848 struct dnssl_addr *dnsa;
852 makeentry(entbuf, sizeof(entbuf), i, "dnssl");
853 addr = (char *)agetstr(entbuf, &bp);
857 ELM_MALLOC(dns, exit(1));
859 TAILQ_INIT(&dns->dn_list);
861 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
862 c = strcspn(ap, ",");
863 strncpy(abuf, ap, c);
865 ELM_MALLOC(dnsa, goto getconfig_free_dns);
866 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf);
867 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__,
869 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next);
872 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
873 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
874 if ((uint16_t)val < rai->rai_maxinterval ||
875 (uint16_t)val > rai->rai_maxinterval * 2) {
876 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
877 "(must be between %d and %d)",
878 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
879 rai->rai_maxinterval * 2);
880 goto getconfig_free_dns;
884 /* link into chain */
885 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next);
888 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) {
889 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next);
894 /* construct the sending packet */
898 * If an entry with the same ifindex exists, remove it first.
899 * Before the removal, RDNSS and DNSSL options with
900 * zero-lifetime will be sent.
902 switch (ifi->ifi_state) {
903 case IFI_STATE_UNCONFIGURED:
904 /* UNCONFIGURED -> TRANSITIVE */
906 error = sock_mc_join(&sock, ifi->ifi_ifindex);
910 ifi->ifi_state = IFI_STATE_TRANSITIVE;
911 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS;
912 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
914 /* The same two rai mean initial burst */
915 ifi->ifi_rainfo = rai;
916 ifi->ifi_rainfo_trans = rai;
917 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
919 if (ifi->ifi_ra_timer == NULL)
920 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
921 ra_timer_update, ifi, ifi);
922 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
923 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
927 "<%s> ifname=%s marked as TRANSITIVE (initial burst).",
928 __func__, ifi->ifi_ifname);
930 case IFI_STATE_CONFIGURED:
931 /* CONFIGURED -> TRANSITIVE */
932 rai_old = ifi->ifi_rainfo;
933 if (rai_old == NULL) {
935 "<%s> ifi_rainfo is NULL"
936 " in IFI_STATE_CONFIGURED.", __func__);
943 rai_old->rai_lifetime = 0;
944 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next)
946 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next)
949 ifi->ifi_rainfo_trans = rai_old;
950 ifi->ifi_state = IFI_STATE_TRANSITIVE;
951 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
952 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
954 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
955 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
959 "<%s> ifname=%s marked as TRANSITIVE"
960 " (transitional burst)",
961 __func__, ifi->ifi_ifname);
963 ifi->ifi_rainfo = rai;
964 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
966 case IFI_STATE_TRANSITIVE:
967 if (ifi->ifi_rainfo != NULL) {
968 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
969 /* Reinitialize initial burst */
970 rm_rainfo(ifi->ifi_rainfo);
971 ifi->ifi_rainfo = rai;
972 ifi->ifi_rainfo_trans = rai;
973 ifi->ifi_burstcount =
974 MAX_INITIAL_RTR_ADVERTISEMENTS;
975 ifi->ifi_burstinterval =
976 MAX_INITIAL_RTR_ADVERT_INTERVAL;
978 /* Replace ifi_rainfo with the new one */
979 rm_rainfo(ifi->ifi_rainfo);
980 ifi->ifi_rainfo = rai;
982 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
984 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
985 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
988 /* XXX: NOTREACHED. Being shut down. */
990 "<%s> %s is shutting down. Skipped.",
991 __func__, ifi->ifi_ifname);
1007 get_prefix(struct rainfo *rai)
1009 struct ifaddrs *ifap, *ifa;
1013 char *p, *ep, *m, *lim;
1014 char ntopbuf[INET6_ADDRSTRLEN];
1016 if (getifaddrs(&ifap) < 0) {
1018 "<%s> can't get interface addresses",
1022 ifi = rai->rai_ifinfo;
1024 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1027 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0)
1029 if (ifa->ifa_addr->sa_family != AF_INET6)
1031 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1032 if (IN6_IS_ADDR_LINKLOCAL(a))
1035 /* get prefix length */
1036 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1037 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
1038 plen = prefixlen(m, lim);
1039 if (plen <= 0 || plen > 128) {
1040 syslog(LOG_ERR, "<%s> failed to get prefixlen "
1041 "or prefix is invalid",
1045 if (plen == 128) /* XXX */
1047 if (find_prefix(rai, a, plen)) {
1048 /* ignore a duplicated prefix. */
1052 /* allocate memory to store prefix info. */
1053 ELM_MALLOC(pfx, exit(1));
1055 /* set prefix, sweep bits outside of prefixlen */
1056 pfx->pfx_prefixlen = plen;
1057 memcpy(&pfx->pfx_prefix, a, sizeof(*a));
1058 p = (char *)&pfx->pfx_prefix;
1059 ep = (char *)(&pfx->pfx_prefix + 1);
1060 while (m < lim && p < ep)
1064 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1066 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
1070 "<%s> add %s/%d to prefix list on %s",
1071 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname);
1073 /* set other fields with protocol defaults */
1074 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME;
1075 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME;
1076 pfx->pfx_onlinkflg = 1;
1077 pfx->pfx_autoconfflg = 1;
1078 pfx->pfx_origin = PREFIX_FROM_KERNEL;
1079 pfx->pfx_rainfo = rai;
1081 /* link into chain */
1082 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1084 /* counter increment */
1092 makeentry(char *buf, size_t len, int id, const char *string)
1096 strlcpy(buf, string, len);
1098 snprintf(buf, len, "%s%d", string, id);
1102 * Add a prefix to the list of specified interface and reconstruct
1103 * the outgoing packet.
1104 * The prefix must not be in the list.
1105 * XXX: other parameters of the prefix (e.g. lifetime) should be
1106 * able to be specified.
1109 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
1113 char ntopbuf[INET6_ADDRSTRLEN];
1115 ifi = rai->rai_ifinfo;
1116 ELM_MALLOC(pfx, return);
1117 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr;
1118 pfx->pfx_prefixlen = ipr->ipr_plen;
1119 pfx->pfx_validlifetime = ipr->ipr_vltime;
1120 pfx->pfx_preflifetime = ipr->ipr_pltime;
1121 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink;
1122 pfx->pfx_autoconfflg = ipr->ipr_raf_auto;
1123 pfx->pfx_origin = PREFIX_FROM_DYNAMIC;
1124 pfx->pfx_rainfo = rai;
1126 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1128 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
1130 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1131 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname);
1137 * Delete a prefix to the list of specified interface and reconstruct
1138 * the outgoing packet.
1139 * The prefix must be in the list.
1142 delete_prefix(struct prefix *pfx)
1146 char ntopbuf[INET6_ADDRSTRLEN];
1148 rai = pfx->pfx_rainfo;
1149 ifi = rai->rai_ifinfo;
1150 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next);
1151 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
1153 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1154 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1156 rtadvd_remove_timer(pfx->pfx_timer);
1163 invalidate_prefix(struct prefix *pfx)
1165 struct timeval timo;
1168 char ntopbuf[INET6_ADDRSTRLEN];
1170 rai = pfx->pfx_rainfo;
1171 ifi = rai->rai_ifinfo;
1172 if (pfx->pfx_timer) { /* sanity check */
1174 "<%s> assumption failure: timer already exists",
1179 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
1180 "will expire in %ld seconds", __func__,
1181 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)),
1182 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo);
1184 /* set the expiration timer */
1185 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL);
1186 if (pfx->pfx_timer == NULL) {
1187 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
1188 "remove the prefix", __func__);
1191 timo.tv_sec = prefix_timo;
1193 rtadvd_set_timer(&timo, pfx->pfx_timer);
1196 static struct rtadvd_timer *
1197 prefix_timeout(void *arg)
1200 delete_prefix((struct prefix *)arg);
1206 update_prefix(struct prefix *pfx)
1210 char ntopbuf[INET6_ADDRSTRLEN];
1212 rai = pfx->pfx_rainfo;
1213 ifi = rai->rai_ifinfo;
1214 if (pfx->pfx_timer == NULL) { /* sanity check */
1216 "<%s> assumption failure: timer does not exist",
1221 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1222 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1223 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1225 /* stop the expiration timer */
1226 rtadvd_remove_timer(pfx->pfx_timer);
1227 pfx->pfx_timer = NULL;
1231 * Try to get an in6_prefixreq contents for a prefix which matches
1232 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1233 * the interface whose name is ipr->ipr_name[].
1236 init_prefix(struct in6_prefixreq *ipr)
1241 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1242 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1247 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
1248 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
1251 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1252 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1253 ipr->ipr_raf_onlink = 1;
1254 ipr->ipr_raf_auto = 1;
1255 /* omit other field initialization */
1257 else if (ipr->ipr_origin < PR_ORIG_RR) {
1258 char ntopbuf[INET6_ADDRSTRLEN];
1260 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1261 "lower than PR_ORIG_RR(router renumbering)."
1262 "This should not happen if I am router", __func__,
1263 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1264 sizeof(ntopbuf)), ipr->ipr_origin);
1272 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1273 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1274 ipr->ipr_raf_onlink = 1;
1275 ipr->ipr_raf_auto = 1;
1281 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1283 struct in6_prefixreq ipr;
1285 memset(&ipr, 0, sizeof(ipr));
1286 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1287 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
1288 "exist. This should not happen! %s", __func__,
1289 ifindex, strerror(errno));
1292 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1293 ipr.ipr_prefix.sin6_family = AF_INET6;
1294 ipr.ipr_prefix.sin6_addr = *addr;
1295 ipr.ipr_plen = plen;
1297 if (init_prefix(&ipr))
1298 return; /* init failed by some error */
1299 add_prefix(rai, &ipr);
1303 make_packet(struct rainfo *rai)
1305 size_t packlen, lladdroptlen = 0;
1307 struct nd_router_advert *ra;
1308 struct nd_opt_prefix_info *ndopt_pi;
1309 struct nd_opt_mtu *ndopt_mtu;
1310 struct nd_opt_route_info *ndopt_rti;
1312 struct nd_opt_rdnss *ndopt_rdnss;
1314 struct nd_opt_dnssl *ndopt_dnssl;
1320 ifi = rai->rai_ifinfo;
1321 /* calculate total length */
1322 packlen = sizeof(struct nd_router_advert);
1323 if (rai->rai_advlinkopt) {
1324 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) {
1326 "<%s> link-layer address option has"
1327 " null length on %s. Treat as not included.",
1328 __func__, ifi->ifi_ifname);
1329 rai->rai_advlinkopt = 0;
1331 packlen += lladdroptlen;
1334 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs;
1335 if (rai->rai_linkmtu)
1336 packlen += sizeof(struct nd_opt_mtu);
1338 TAILQ_FOREACH(rti, &rai->rai_route, rti_next)
1339 packlen += sizeof(struct nd_opt_route_info) +
1340 ((rti->rti_prefixlen + 0x3f) >> 6) * 8;
1342 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1343 struct rdnss_addr *rdna;
1345 packlen += sizeof(struct nd_opt_rdnss);
1346 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next)
1347 packlen += sizeof(rdna->ra_dns);
1349 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1350 struct dnssl_addr *dnsa;
1352 packlen += sizeof(struct nd_opt_dnssl);
1354 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next)
1355 len += dnsa->da_len;
1357 /* A zero octet and 8 octet boundary */
1359 len += (len % 8) ? 8 - len % 8 : 0;
1363 /* allocate memory for the packet */
1364 if ((buf = malloc(packlen)) == NULL) {
1366 "<%s> can't get enough memory for an RA packet",
1370 memset(buf, 0, packlen);
1371 if (rai->rai_ra_data) /* Free old data if any. */
1372 free(rai->rai_ra_data);
1373 rai->rai_ra_data = buf;
1374 /* XXX: what if packlen > 576? */
1375 rai->rai_ra_datalen = packlen;
1378 * construct the packet
1380 ra = (struct nd_router_advert *)buf;
1381 ra->nd_ra_type = ND_ROUTER_ADVERT;
1383 ra->nd_ra_cksum = 0;
1384 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit);
1385 ra->nd_ra_flags_reserved = 0; /* just in case */
1387 * XXX: the router preference field, which is a 2-bit field, should be
1388 * initialized before other fields.
1390 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref;
1391 ra->nd_ra_flags_reserved |=
1392 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0;
1393 ra->nd_ra_flags_reserved |=
1394 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0;
1395 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime);
1396 ra->nd_ra_reachable = htonl(rai->rai_reachabletime);
1397 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer);
1400 if (rai->rai_advlinkopt) {
1401 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf);
1402 buf += lladdroptlen;
1405 if (rai->rai_linkmtu) {
1406 ndopt_mtu = (struct nd_opt_mtu *)buf;
1407 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1408 ndopt_mtu->nd_opt_mtu_len = 1;
1409 ndopt_mtu->nd_opt_mtu_reserved = 0;
1410 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu);
1411 buf += sizeof(struct nd_opt_mtu);
1414 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1415 uint32_t vltime, pltime;
1418 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1419 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1420 ndopt_pi->nd_opt_pi_len = 4;
1421 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen;
1422 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1423 if (pfx->pfx_onlinkflg)
1424 ndopt_pi->nd_opt_pi_flags_reserved |=
1425 ND_OPT_PI_FLAG_ONLINK;
1426 if (pfx->pfx_autoconfflg)
1427 ndopt_pi->nd_opt_pi_flags_reserved |=
1428 ND_OPT_PI_FLAG_AUTO;
1432 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire)
1433 gettimeofday(&now, NULL);
1434 if (pfx->pfx_vltimeexpire == 0)
1435 vltime = pfx->pfx_validlifetime;
1437 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ?
1438 pfx->pfx_vltimeexpire - now.tv_sec : 0;
1443 if (pfx->pfx_pltimeexpire == 0)
1444 pltime = pfx->pfx_preflifetime;
1446 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ?
1447 pfx->pfx_pltimeexpire - now.tv_sec : 0;
1449 if (vltime < pltime) {
1451 * this can happen if vltime is decrement but pltime
1456 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1457 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1458 ndopt_pi->nd_opt_pi_reserved2 = 0;
1459 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix;
1461 buf += sizeof(struct nd_opt_prefix_info);
1464 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) {
1465 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6;
1467 ndopt_rti = (struct nd_opt_route_info *)buf;
1468 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1469 ndopt_rti->nd_opt_rti_len = 1 + psize;
1470 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen;
1471 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref;
1472 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime);
1473 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8);
1474 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1477 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1478 struct rdnss_addr *rdna;
1480 ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1481 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1482 ndopt_rdnss->nd_opt_rdnss_len = 0;
1483 ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1484 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime);
1485 buf += sizeof(struct nd_opt_rdnss);
1487 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) {
1488 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns));
1489 buf += sizeof(rdna->ra_dns);
1491 /* Length field should be in 8 octets */
1492 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8;
1494 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__,
1495 ndopt_rdnss->nd_opt_rdnss_len);
1498 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1499 struct dnssl_addr *dnsa;
1501 ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1502 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1503 ndopt_dnssl->nd_opt_dnssl_len = 0;
1504 ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1505 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime);
1506 buf += sizeof(*ndopt_dnssl);
1508 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) {
1509 memcpy(buf, dnsa->da_dom, dnsa->da_len);
1510 buf += dnsa->da_len;
1513 /* A zero octet after encoded DNS server list. */
1516 /* Padding to next 8 octets boundary */
1517 len = buf - (char *)ndopt_dnssl;
1518 len += (len % 8) ? 8 - len % 8 : 0;
1520 /* Length field must be in 8 octets */
1521 ndopt_dnssl->nd_opt_dnssl_len = len / 8;
1523 syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__,
1524 ndopt_dnssl->nd_opt_dnssl_len);