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>
39 #include <net/route.h>
40 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet/ip6.h>
45 #include <netinet6/ip6_var.h>
46 #include <netinet/icmp6.h>
47 #include <netinet6/nd6.h>
49 #include <arpa/inet.h>
69 /* label of tcapcode + number + domain name + zero octet */
70 static char entbuf[10 + 3 + NI_MAXHOST + 1];
71 static char oentbuf[10 + 3 + NI_MAXHOST + 1];
72 static char abuf[DNAME_LABELENC_MAXLEN];
74 static time_t prefix_timo = (60 * 120); /* 2 hours.
75 * XXX: should be configurable. */
77 static struct rtadvd_timer *prefix_timeout(void *);
78 static void makeentry(char *, size_t, int, const char *);
79 static ssize_t dname_labelenc(char *, const char *);
81 /* Encode domain name label encoding in RFC 1035 Section 3.1 */
83 dname_labelenc(char *dst, const char *src)
92 if (len + len / 64 + 1 + 1 > DNAME_LABELENC_MAXLEN)
94 /* Length fields per 63 octets + '\0' (<= DNAME_LABELENC_MAXLEN) */
95 memset(dst, 0, len + len / 64 + 1 + 1);
97 syslog(LOG_DEBUG, "<%s> labelenc = %s", __func__, src);
98 while (src && (len = strlen(src)) != 0) {
99 /* Put a length field with 63 octet limitation first. */
100 p = strchr(src, '.');
102 *dst = len = MIN(63, len);
104 *dst = len = MIN(63, p - src);
105 if (dst + 1 + len < dst_origin + DNAME_LABELENC_MAXLEN)
109 /* Copy 63 octets at most. */
110 memcpy(dst, src, len);
112 if (p == NULL) /* the last label */
116 /* Always need a 0-length label at the tail. */
119 syslog(LOG_DEBUG, "<%s> labellen = %td", __func__, dst - dst_origin);
120 return (dst - dst_origin);
123 #define MUSTHAVE(var, cap) \
126 if ((t = agetnum(cap)) < 0) { \
127 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
134 #define MAYHAVE(var, cap, def) \
136 if ((var = agetnum(cap)) < 0) \
141 loadconfig_index(int idx)
143 char ifname[IFNAMSIZ];
145 syslog(LOG_DEBUG, "<%s> enter", __func__);
147 if (if_indextoname(idx, ifname) != NULL)
148 return (loadconfig_ifname(ifname));
154 loadconfig_ifname(char *ifname)
158 syslog(LOG_DEBUG, "<%s> enter", __func__);
160 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
161 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
162 /* NULL means all IFs will be processed. */
163 if (ifname != NULL &&
164 strcmp(ifi->ifi_ifname, ifname) != 0)
167 if (!ifi->ifi_persist) {
169 "<%s> %s is not a target interface. "
170 "Ignored at this moment.", __func__,
175 if (ifi->ifi_ifindex == 0) {
177 "<%s> %s not found. "
178 "Ignored at this moment.", __func__,
182 if (getconfig(ifi) == NULL) {
184 "<%s> invalid configuration for %s. "
185 "Ignored at this moment.", __func__,
194 rm_ifinfo_index(int idx)
198 ifi = if_indextoifinfo(idx);
200 syslog(LOG_ERR, "<%s>: ifinfo not found (idx=%d)",
205 return (rm_ifinfo(ifi));
209 rm_ifinfo(struct ifinfo *ifi)
213 syslog(LOG_DEBUG, "<%s> enter (%s).", __func__, ifi->ifi_ifname);
214 switch (ifi->ifi_state) {
215 case IFI_STATE_UNCONFIGURED:
219 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
221 "<%s> ifname=%s marked as UNCONFIGURED.",
222 __func__, ifi->ifi_ifname);
224 /* XXX: No MC leaving here because index is disappeared */
226 /* Inactivate timer */
227 rtadvd_remove_timer(ifi->ifi_ra_timer);
228 ifi->ifi_ra_timer = NULL;
233 if (!ifi->ifi_persist) {
234 TAILQ_REMOVE(&ifilist, ifi, ifi_next);
235 syslog(LOG_DEBUG, "<%s>: ifinfo (idx=%d) removed.",
236 __func__, ifi->ifi_ifindex);
239 /* recreate an empty entry */
240 update_persist_ifinfo(&ifilist, ifi->ifi_ifname);
241 syslog(LOG_DEBUG, "<%s>: ifname=%s is persistent.",
242 __func__, ifi->ifi_ifname);
245 /* clean up rai if any */
246 switch (ifi->ifi_state) {
247 case IFI_STATE_CONFIGURED:
248 if (ifi->ifi_rainfo != NULL) {
249 error = rm_rainfo(ifi->ifi_rainfo);
252 ifi->ifi_rainfo = NULL;
255 case IFI_STATE_TRANSITIVE:
256 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
257 if (ifi->ifi_rainfo != NULL) {
258 error = rm_rainfo(ifi->ifi_rainfo);
261 ifi->ifi_rainfo = NULL;
262 ifi->ifi_rainfo_trans = NULL;
265 if (ifi->ifi_rainfo != NULL) {
266 error = rm_rainfo(ifi->ifi_rainfo);
269 ifi->ifi_rainfo = NULL;
271 if (ifi->ifi_rainfo_trans != NULL) {
272 error = rm_rainfo(ifi->ifi_rainfo_trans);
275 ifi->ifi_rainfo_trans = NULL;
280 syslog(LOG_DEBUG, "<%s> leave (%s).", __func__, ifi->ifi_ifname);
285 rm_rainfo(struct rainfo *rai)
288 struct soliciter *sol;
290 struct rdnss_addr *rdna;
294 syslog(LOG_DEBUG, "<%s>: enter", __func__);
296 TAILQ_REMOVE(&railist, rai, rai_next);
297 if (rai->rai_ifinfo != NULL)
298 syslog(LOG_DEBUG, "<%s>: rainfo (idx=%d) removed.",
299 __func__, rai->rai_ifinfo->ifi_ifindex);
301 if (rai->rai_ra_data != NULL)
302 free(rai->rai_ra_data);
304 while ((pfx = TAILQ_FIRST(&rai->rai_prefix)) != NULL)
306 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
307 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
310 while ((rdn = TAILQ_FIRST(&rai->rai_rdnss)) != NULL) {
311 TAILQ_REMOVE(&rai->rai_rdnss, rdn, rd_next);
312 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
313 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
318 while ((dns = TAILQ_FIRST(&rai->rai_dnssl)) != NULL) {
319 TAILQ_REMOVE(&rai->rai_dnssl, dns, dn_next);
322 while ((rti = TAILQ_FIRST(&rai->rai_route)) != NULL) {
323 TAILQ_REMOVE(&rai->rai_route, rti, rti_next);
327 syslog(LOG_DEBUG, "<%s>: leave", __func__);
333 getconfig(struct ifinfo *ifi)
339 struct rainfo *rai_old;
344 char *addr, *flagstr;
346 if (ifi == NULL) /* if does not exist */
349 if (ifi->ifi_state == IFI_STATE_TRANSITIVE &&
350 ifi->ifi_rainfo == NULL) {
351 syslog(LOG_INFO, "<%s> %s is shutting down. Skipped.",
352 __func__, ifi->ifi_ifname);
356 if ((stat = agetent(tbuf, ifi->ifi_ifname)) <= 0) {
357 memset(tbuf, 0, sizeof(tbuf));
359 "<%s> %s isn't defined in the configuration file"
360 " or the configuration file doesn't exist."
361 " Treat it as default",
362 __func__, ifi->ifi_ifname);
365 ELM_MALLOC(rai, exit(1));
366 TAILQ_INIT(&rai->rai_prefix);
367 TAILQ_INIT(&rai->rai_route);
368 TAILQ_INIT(&rai->rai_rdnss);
369 TAILQ_INIT(&rai->rai_dnssl);
370 TAILQ_INIT(&rai->rai_soliciter);
371 rai->rai_ifinfo = ifi;
373 /* gather on-link prefixes from the network interfaces. */
374 if (agetflag("noifprefix"))
375 rai->rai_advifprefix = 0;
377 rai->rai_advifprefix = 1;
379 /* get interface information */
380 if (agetflag("nolladdr"))
381 rai->rai_advlinkopt = 0;
383 rai->rai_advlinkopt = 1;
384 if (rai->rai_advlinkopt) {
385 if (ifi->ifi_sdl.sdl_type == 0) {
387 "<%s> can't get information of %s",
388 __func__, ifi->ifi_ifname);
389 goto getconfig_free_rai;
394 * set router configuration variables.
396 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
397 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
399 "<%s> maxinterval (%" PRIu32 ") on %s is invalid "
400 "(must be between %u and %u)", __func__, val,
401 ifi->ifi_ifname, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
402 goto getconfig_free_rai;
404 rai->rai_maxinterval = (uint16_t)val;
406 MAYHAVE(val, "mininterval", rai->rai_maxinterval/3);
407 if ((uint16_t)val < MIN_MININTERVAL ||
408 (uint16_t)val > (rai->rai_maxinterval * 3) / 4) {
410 "<%s> mininterval (%" PRIu32 ") on %s is invalid "
411 "(must be between %d and %d)",
412 __func__, val, ifi->ifi_ifname, MIN_MININTERVAL,
413 (rai->rai_maxinterval * 3) / 4);
414 goto getconfig_free_rai;
416 rai->rai_mininterval = (uint16_t)val;
418 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
419 rai->rai_hoplimit = val & 0xff;
421 if ((flagstr = (char *)agetstr("raflags", &bp))) {
423 if (strchr(flagstr, 'm'))
424 val |= ND_RA_FLAG_MANAGED;
425 if (strchr(flagstr, 'o'))
426 val |= ND_RA_FLAG_OTHER;
427 if (strchr(flagstr, 'h'))
428 val |= ND_RA_FLAG_RTPREF_HIGH;
429 if (strchr(flagstr, 'l')) {
430 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
431 syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
432 " router flags are exclusive", __func__);
433 goto getconfig_free_rai;
435 val |= ND_RA_FLAG_RTPREF_LOW;
438 MAYHAVE(val, "raflags", 0);
440 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED;
441 rai->rai_otherflg = val & ND_RA_FLAG_OTHER;
442 #ifndef ND_RA_FLAG_RTPREF_MASK
443 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
444 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
446 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
447 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) {
448 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
449 __func__, rai->rai_rtpref, ifi->ifi_ifname);
450 goto getconfig_free_rai;
453 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3);
454 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval ||
455 (uint16_t)val > MAXROUTERLIFETIME)) {
457 "<%s> router lifetime (%" PRIu32 ") on %s is invalid "
458 "(must be 0 or between %d and %d)",
459 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval,
461 goto getconfig_free_rai;
463 rai->rai_lifetime = val & 0xffff;
465 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
466 if (val < 0 || val > MAXREACHABLETIME) {
468 "<%s> reachable time (%" PRIu32 ") on %s is invalid "
469 "(must be no greater than %d)",
470 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME);
471 goto getconfig_free_rai;
473 rai->rai_reachabletime = (uint32_t)val;
475 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
476 if (val64 < 0 || val64 > 0xffffffff) {
477 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range",
478 __func__, val64, ifi->ifi_ifname);
479 goto getconfig_free_rai;
481 rai->rai_retranstimer = (uint32_t)val64;
483 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
485 "<%s> mobile-ip6 configuration not supported",
487 goto getconfig_free_rai;
489 /* prefix information */
492 * This is an implementation specific parameter to consider
493 * link propagation delays and poorly synchronized clocks when
494 * checking consistency of advertised lifetimes.
496 MAYHAVE(val, "clockskew", 0);
497 rai->rai_clockskew = val;
500 for (i = -1; i < MAXPREFIX; i++) {
503 makeentry(entbuf, sizeof(entbuf), i, "addr");
504 addr = (char *)agetstr(entbuf, &bp);
508 /* allocate memory to store prefix information */
509 ELM_MALLOC(pfx, exit(1));
510 pfx->pfx_rainfo = rai;
511 pfx->pfx_origin = PREFIX_FROM_CONFIG;
513 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) {
515 "<%s> inet_pton failed for %s",
517 goto getconfig_free_pfx;
519 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) {
521 "<%s> multicast prefix (%s) must "
522 "not be advertised on %s",
523 __func__, addr, ifi->ifi_ifname);
524 goto getconfig_free_pfx;
526 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix))
528 "<%s> link-local prefix (%s) will be"
530 __func__, addr, ifi->ifi_ifname);
532 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
533 MAYHAVE(val, entbuf, 64);
534 if (val < 0 || val > 128) {
535 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s "
536 "on %s out of range",
537 __func__, val, addr, ifi->ifi_ifname);
538 goto getconfig_free_pfx;
540 pfx->pfx_prefixlen = (int)val;
542 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
543 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
545 if (strchr(flagstr, 'l'))
546 val |= ND_OPT_PI_FLAG_ONLINK;
547 if (strchr(flagstr, 'a'))
548 val |= ND_OPT_PI_FLAG_AUTO;
551 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
553 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
554 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
556 makeentry(entbuf, sizeof(entbuf), i, "vltime");
557 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
558 if (val64 < 0 || val64 > 0xffffffff) {
559 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for "
560 "%s/%d on %s is out of range",
562 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
563 goto getconfig_free_pfx;
565 pfx->pfx_validlifetime = (uint32_t)val64;
567 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
568 if (agetflag(entbuf)) {
571 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
572 pfx->pfx_vltimeexpire =
573 now.tv_sec + pfx->pfx_validlifetime;
576 makeentry(entbuf, sizeof(entbuf), i, "pltime");
577 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
578 if (val64 < 0 || val64 > 0xffffffff) {
580 "<%s> pltime (%" PRIu64 ") for %s/%d on %s "
583 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
584 goto getconfig_free_pfx;
586 pfx->pfx_preflifetime = (uint32_t)val64;
588 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
589 if (agetflag(entbuf)) {
592 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
593 pfx->pfx_pltimeexpire =
594 now.tv_sec + pfx->pfx_preflifetime;
596 /* link into chain */
597 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
603 if (rai->rai_advifprefix && rai->rai_pfxs == 0)
606 MAYHAVE(val64, "mtu", 0);
607 if (val < 0 || val64 > 0xffffffff) {
609 "<%s> mtu (%" PRIu64 ") on %s out of range",
610 __func__, val64, ifi->ifi_ifname);
611 goto getconfig_free_rai;
613 rai->rai_linkmtu = (uint32_t)val64;
614 if (rai->rai_linkmtu == 0) {
617 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
618 strcmp(mtustr, "auto") == 0)
619 rai->rai_linkmtu = ifi->ifi_phymtu;
621 else if (rai->rai_linkmtu < IPV6_MMTU ||
622 rai->rai_linkmtu > ifi->ifi_phymtu) {
624 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must "
625 "be between least MTU (%d) and physical link MTU (%d)",
626 __func__, rai->rai_linkmtu, ifi->ifi_ifname,
627 IPV6_MMTU, ifi->ifi_phymtu);
628 goto getconfig_free_rai;
631 #ifdef SIOCSIFINFO_IN6
633 struct in6_ndireq ndi;
636 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
637 syslog(LOG_ERR, "<%s> socket: %s", __func__,
641 memset(&ndi, 0, sizeof(ndi));
642 strncpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname));
643 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0)
644 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
645 __func__, ifi->ifi_ifname, strerror(errno));
647 /* reflect the RA info to the host variables in kernel */
648 ndi.ndi.chlim = rai->rai_hoplimit;
649 ndi.ndi.retrans = rai->rai_retranstimer;
650 ndi.ndi.basereachable = rai->rai_reachabletime;
651 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0)
652 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
653 __func__, ifi->ifi_ifname, strerror(errno));
659 /* route information */
661 for (i = -1; i < MAXROUTE; i++) {
664 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
665 addr = (char *)agetstr(entbuf, &bp);
667 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
668 addr = (char *)agetstr(oentbuf, &bp);
670 fprintf(stderr, "%s was obsoleted. Use %s.\n",
676 /* allocate memory to store prefix information */
677 ELM_MALLOC(rti, exit(1));
679 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) {
680 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
682 goto getconfig_free_rti;
686 * XXX: currently there's no restriction in route information
687 * prefix according to
688 * draft-ietf-ipngwg-router-selection-00.txt.
689 * However, I think the similar restriction be necessary.
691 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
692 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
694 "<%s> multicast route (%s) must "
695 "not be advertised on %s",
696 __func__, addr, ifi->ifi_ifname);
697 goto getconfig_free_rti;
699 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
701 "<%s> link-local route (%s) will "
702 "be advertised on %s",
703 __func__, addr, ifi->ifi_ifname);
704 goto getconfig_free_rti;
708 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
709 /* XXX: 256 is a magic number for compatibility check. */
710 MAYHAVE(val, entbuf, 256);
712 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
713 MAYHAVE(val, oentbuf, 256);
715 fprintf(stderr, "%s was obsoleted. Use %s.\n",
720 if (val < 0 || val > 128) {
721 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s "
723 __func__, val, addr, ifi->ifi_ifname);
724 goto getconfig_free_rti;
726 rti->rti_prefixlen = (int)val;
728 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
729 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
731 if (strchr(flagstr, 'h'))
732 val |= ND_RA_FLAG_RTPREF_HIGH;
733 if (strchr(flagstr, 'l')) {
734 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
736 "<%s> the \'h\' and \'l\' route"
737 " preferences are exclusive",
739 goto getconfig_free_rti;
741 val |= ND_RA_FLAG_RTPREF_LOW;
744 MAYHAVE(val, entbuf, 256); /* XXX */
746 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
747 MAYHAVE(val, oentbuf, 256);
749 fprintf(stderr, "%s was obsoleted. Use %s.\n",
754 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
755 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) {
756 syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
758 __func__, rti->rti_rtpref, addr,
759 rti->rti_prefixlen, ifi->ifi_ifname);
760 goto getconfig_free_rti;
764 * Since the spec does not a default value, we should make
765 * this entry mandatory. However, FreeBSD 4.4 has shipped
766 * with this field being optional, we use the router lifetime
767 * as an ad-hoc default value with a warning message.
769 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
770 MAYHAVE(val64, entbuf, -1);
772 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
773 MAYHAVE(val64, oentbuf, -1);
775 fprintf(stderr, "%s was obsoleted. Use %s.\n",
778 fprintf(stderr, "%s should be specified "
779 "for interface %s.\n", entbuf,
781 val64 = rai->rai_lifetime;
784 if (val64 < 0 || val64 > 0xffffffff) {
785 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for "
786 "%s/%d on %s out of range", __func__,
787 val64, addr, rti->rti_prefixlen,
789 goto getconfig_free_rti;
791 rti->rti_ltime = (uint32_t)val64;
793 /* link into chain */
794 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next);
801 /* DNS server and DNS search list information */
802 for (i = -1; i < MAXRDNSSENT ; i++) {
804 struct rdnss_addr *rdna;
808 makeentry(entbuf, sizeof(entbuf), i, "rdnss");
809 addr = (char *)agetstr(entbuf, &bp);
812 ELM_MALLOC(rdn, exit(1));
814 TAILQ_INIT(&rdn->rd_list);
816 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
817 c = strcspn(ap, ",");
818 strncpy(abuf, ap, c);
820 ELM_MALLOC(rdna, goto getconfig_free_rdn);
821 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) {
822 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
825 goto getconfig_free_rdn;
827 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next);
830 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
831 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
832 if ((uint16_t)val < rai->rai_maxinterval ||
833 (uint16_t)val > rai->rai_maxinterval * 2) {
834 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
835 "(must be between %d and %d)",
836 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
837 rai->rai_maxinterval * 2);
838 goto getconfig_free_rdn;
842 /* link into chain */
843 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next);
846 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
847 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
853 for (i = -1; i < MAXDNSSLENT ; i++) {
855 struct dnssl_addr *dnsa;
859 makeentry(entbuf, sizeof(entbuf), i, "dnssl");
860 addr = (char *)agetstr(entbuf, &bp);
864 ELM_MALLOC(dns, exit(1));
866 TAILQ_INIT(&dns->dn_list);
868 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
869 c = strcspn(ap, ",");
870 strncpy(abuf, ap, c);
872 ELM_MALLOC(dnsa, goto getconfig_free_dns);
873 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf);
874 if (dnsa->da_len < 0) {
875 syslog(LOG_ERR, "Invalid dnssl entry: %s",
877 goto getconfig_free_dns;
879 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__,
881 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next);
884 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
885 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
886 if ((uint16_t)val < rai->rai_maxinterval ||
887 (uint16_t)val > rai->rai_maxinterval * 2) {
888 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
889 "(must be between %d and %d)",
890 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
891 rai->rai_maxinterval * 2);
892 goto getconfig_free_dns;
896 /* link into chain */
897 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next);
900 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) {
901 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next);
906 /* construct the sending packet */
910 * If an entry with the same ifindex exists, remove it first.
911 * Before the removal, RDNSS and DNSSL options with
912 * zero-lifetime will be sent.
914 switch (ifi->ifi_state) {
915 case IFI_STATE_UNCONFIGURED:
916 /* UNCONFIGURED -> TRANSITIVE */
918 error = sock_mc_join(&sock, ifi->ifi_ifindex);
922 ifi->ifi_state = IFI_STATE_TRANSITIVE;
923 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS;
924 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
926 /* The same two rai mean initial burst */
927 ifi->ifi_rainfo = rai;
928 ifi->ifi_rainfo_trans = rai;
929 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
931 if (ifi->ifi_ra_timer == NULL)
932 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
933 ra_timer_update, ifi, ifi);
934 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
935 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
939 "<%s> ifname=%s marked as TRANSITIVE (initial burst).",
940 __func__, ifi->ifi_ifname);
942 case IFI_STATE_CONFIGURED:
943 /* CONFIGURED -> TRANSITIVE */
944 rai_old = ifi->ifi_rainfo;
945 if (rai_old == NULL) {
947 "<%s> ifi_rainfo is NULL"
948 " in IFI_STATE_CONFIGURED.", __func__);
955 rai_old->rai_lifetime = 0;
956 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next)
958 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next)
961 ifi->ifi_rainfo_trans = rai_old;
962 ifi->ifi_state = IFI_STATE_TRANSITIVE;
963 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
964 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
966 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
967 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
971 "<%s> ifname=%s marked as TRANSITIVE"
972 " (transitional burst)",
973 __func__, ifi->ifi_ifname);
975 ifi->ifi_rainfo = rai;
976 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
978 case IFI_STATE_TRANSITIVE:
979 if (ifi->ifi_rainfo != NULL) {
980 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
981 /* Reinitialize initial burst */
982 rm_rainfo(ifi->ifi_rainfo);
983 ifi->ifi_rainfo = rai;
984 ifi->ifi_rainfo_trans = rai;
985 ifi->ifi_burstcount =
986 MAX_INITIAL_RTR_ADVERTISEMENTS;
987 ifi->ifi_burstinterval =
988 MAX_INITIAL_RTR_ADVERT_INTERVAL;
990 /* Replace ifi_rainfo with the new one */
991 rm_rainfo(ifi->ifi_rainfo);
992 ifi->ifi_rainfo = rai;
994 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
996 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
997 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
1000 /* XXX: NOTREACHED. Being shut down. */
1002 "<%s> %s is shutting down. Skipped.",
1003 __func__, ifi->ifi_ifname);
1019 get_prefix(struct rainfo *rai)
1021 struct ifaddrs *ifap, *ifa;
1025 char *p, *ep, *m, *lim;
1026 char ntopbuf[INET6_ADDRSTRLEN];
1028 if (getifaddrs(&ifap) < 0) {
1030 "<%s> can't get interface addresses",
1034 ifi = rai->rai_ifinfo;
1036 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1039 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0)
1041 if (ifa->ifa_addr->sa_family != AF_INET6)
1043 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1044 if (IN6_IS_ADDR_LINKLOCAL(a))
1047 /* get prefix length */
1048 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1049 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
1050 plen = prefixlen(m, lim);
1051 if (plen <= 0 || plen > 128) {
1052 syslog(LOG_ERR, "<%s> failed to get prefixlen "
1053 "or prefix is invalid",
1057 if (plen == 128) /* XXX */
1059 if (find_prefix(rai, a, plen)) {
1060 /* ignore a duplicated prefix. */
1064 /* allocate memory to store prefix info. */
1065 ELM_MALLOC(pfx, exit(1));
1067 /* set prefix, sweep bits outside of prefixlen */
1068 pfx->pfx_prefixlen = plen;
1069 memcpy(&pfx->pfx_prefix, a, sizeof(*a));
1070 p = (char *)&pfx->pfx_prefix;
1071 ep = (char *)(&pfx->pfx_prefix + 1);
1072 while (m < lim && p < ep)
1076 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1078 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
1082 "<%s> add %s/%d to prefix list on %s",
1083 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname);
1085 /* set other fields with protocol defaults */
1086 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME;
1087 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME;
1088 pfx->pfx_onlinkflg = 1;
1089 pfx->pfx_autoconfflg = 1;
1090 pfx->pfx_origin = PREFIX_FROM_KERNEL;
1091 pfx->pfx_rainfo = rai;
1093 /* link into chain */
1094 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1096 /* counter increment */
1104 makeentry(char *buf, size_t len, int id, const char *string)
1108 strlcpy(buf, string, len);
1110 snprintf(buf, len, "%s%d", string, id);
1114 * Add a prefix to the list of specified interface and reconstruct
1115 * the outgoing packet.
1116 * The prefix must not be in the list.
1117 * XXX: other parameters of the prefix (e.g. lifetime) should be
1118 * able to be specified.
1121 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
1125 char ntopbuf[INET6_ADDRSTRLEN];
1127 ifi = rai->rai_ifinfo;
1128 ELM_MALLOC(pfx, return);
1129 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr;
1130 pfx->pfx_prefixlen = ipr->ipr_plen;
1131 pfx->pfx_validlifetime = ipr->ipr_vltime;
1132 pfx->pfx_preflifetime = ipr->ipr_pltime;
1133 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink;
1134 pfx->pfx_autoconfflg = ipr->ipr_raf_auto;
1135 pfx->pfx_origin = PREFIX_FROM_DYNAMIC;
1136 pfx->pfx_rainfo = rai;
1138 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1140 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
1142 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1143 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname);
1149 * Delete a prefix to the list of specified interface and reconstruct
1150 * the outgoing packet.
1151 * The prefix must be in the list.
1154 delete_prefix(struct prefix *pfx)
1158 char ntopbuf[INET6_ADDRSTRLEN];
1160 rai = pfx->pfx_rainfo;
1161 ifi = rai->rai_ifinfo;
1162 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next);
1163 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
1165 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1166 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1168 rtadvd_remove_timer(pfx->pfx_timer);
1175 invalidate_prefix(struct prefix *pfx)
1177 struct timespec timo;
1180 char ntopbuf[INET6_ADDRSTRLEN];
1182 rai = pfx->pfx_rainfo;
1183 ifi = rai->rai_ifinfo;
1184 if (pfx->pfx_timer) { /* sanity check */
1186 "<%s> assumption failure: timer already exists",
1191 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
1192 "will expire in %ld seconds", __func__,
1193 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)),
1194 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo);
1196 /* set the expiration timer */
1197 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL);
1198 if (pfx->pfx_timer == NULL) {
1199 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
1200 "remove the prefix", __func__);
1203 timo.tv_sec = prefix_timo;
1205 rtadvd_set_timer(&timo, pfx->pfx_timer);
1208 static struct rtadvd_timer *
1209 prefix_timeout(void *arg)
1212 delete_prefix((struct prefix *)arg);
1218 update_prefix(struct prefix *pfx)
1222 char ntopbuf[INET6_ADDRSTRLEN];
1224 rai = pfx->pfx_rainfo;
1225 ifi = rai->rai_ifinfo;
1226 if (pfx->pfx_timer == NULL) { /* sanity check */
1228 "<%s> assumption failure: timer does not exist",
1233 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1234 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1235 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1237 /* stop the expiration timer */
1238 rtadvd_remove_timer(pfx->pfx_timer);
1239 pfx->pfx_timer = NULL;
1243 * Try to get an in6_prefixreq contents for a prefix which matches
1244 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1245 * the interface whose name is ipr->ipr_name[].
1248 init_prefix(struct in6_prefixreq *ipr)
1253 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1254 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1259 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
1260 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
1263 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1264 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1265 ipr->ipr_raf_onlink = 1;
1266 ipr->ipr_raf_auto = 1;
1267 /* omit other field initialization */
1269 else if (ipr->ipr_origin < PR_ORIG_RR) {
1270 char ntopbuf[INET6_ADDRSTRLEN];
1272 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1273 "lower than PR_ORIG_RR(router renumbering)."
1274 "This should not happen if I am router", __func__,
1275 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1276 sizeof(ntopbuf)), ipr->ipr_origin);
1284 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1285 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1286 ipr->ipr_raf_onlink = 1;
1287 ipr->ipr_raf_auto = 1;
1293 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1295 struct in6_prefixreq ipr;
1297 memset(&ipr, 0, sizeof(ipr));
1298 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1299 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't "
1300 "exist. This should not happen! %s", __func__,
1301 ifindex, strerror(errno));
1304 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1305 ipr.ipr_prefix.sin6_family = AF_INET6;
1306 ipr.ipr_prefix.sin6_addr = *addr;
1307 ipr.ipr_plen = plen;
1309 if (init_prefix(&ipr))
1310 return; /* init failed by some error */
1311 add_prefix(rai, &ipr);
1315 make_packet(struct rainfo *rai)
1317 size_t packlen, lladdroptlen = 0;
1319 struct nd_router_advert *ra;
1320 struct nd_opt_prefix_info *ndopt_pi;
1321 struct nd_opt_mtu *ndopt_mtu;
1322 struct nd_opt_route_info *ndopt_rti;
1324 struct nd_opt_rdnss *ndopt_rdnss;
1326 struct nd_opt_dnssl *ndopt_dnssl;
1332 ifi = rai->rai_ifinfo;
1333 /* calculate total length */
1334 packlen = sizeof(struct nd_router_advert);
1335 if (rai->rai_advlinkopt) {
1336 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) {
1338 "<%s> link-layer address option has"
1339 " null length on %s. Treat as not included.",
1340 __func__, ifi->ifi_ifname);
1341 rai->rai_advlinkopt = 0;
1343 packlen += lladdroptlen;
1346 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs;
1347 if (rai->rai_linkmtu)
1348 packlen += sizeof(struct nd_opt_mtu);
1350 TAILQ_FOREACH(rti, &rai->rai_route, rti_next)
1351 packlen += sizeof(struct nd_opt_route_info) +
1352 ((rti->rti_prefixlen + 0x3f) >> 6) * 8;
1354 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1355 struct rdnss_addr *rdna;
1357 packlen += sizeof(struct nd_opt_rdnss);
1358 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next)
1359 packlen += sizeof(rdna->ra_dns);
1361 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1362 struct dnssl_addr *dnsa;
1364 packlen += sizeof(struct nd_opt_dnssl);
1366 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next)
1367 len += dnsa->da_len;
1369 /* A zero octet and 8 octet boundary */
1371 len += (len % 8) ? 8 - len % 8 : 0;
1375 /* allocate memory for the packet */
1376 if ((buf = malloc(packlen)) == NULL) {
1378 "<%s> can't get enough memory for an RA packet",
1382 memset(buf, 0, packlen);
1383 if (rai->rai_ra_data) /* Free old data if any. */
1384 free(rai->rai_ra_data);
1385 rai->rai_ra_data = buf;
1386 /* XXX: what if packlen > 576? */
1387 rai->rai_ra_datalen = packlen;
1390 * construct the packet
1392 ra = (struct nd_router_advert *)buf;
1393 ra->nd_ra_type = ND_ROUTER_ADVERT;
1395 ra->nd_ra_cksum = 0;
1396 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit);
1397 ra->nd_ra_flags_reserved = 0; /* just in case */
1399 * XXX: the router preference field, which is a 2-bit field, should be
1400 * initialized before other fields.
1402 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref;
1403 ra->nd_ra_flags_reserved |=
1404 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0;
1405 ra->nd_ra_flags_reserved |=
1406 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0;
1407 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime);
1408 ra->nd_ra_reachable = htonl(rai->rai_reachabletime);
1409 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer);
1412 if (rai->rai_advlinkopt) {
1413 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf);
1414 buf += lladdroptlen;
1417 if (rai->rai_linkmtu) {
1418 ndopt_mtu = (struct nd_opt_mtu *)buf;
1419 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1420 ndopt_mtu->nd_opt_mtu_len = 1;
1421 ndopt_mtu->nd_opt_mtu_reserved = 0;
1422 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu);
1423 buf += sizeof(struct nd_opt_mtu);
1426 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1427 uint32_t vltime, pltime;
1428 struct timespec now;
1430 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1431 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1432 ndopt_pi->nd_opt_pi_len = 4;
1433 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen;
1434 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1435 if (pfx->pfx_onlinkflg)
1436 ndopt_pi->nd_opt_pi_flags_reserved |=
1437 ND_OPT_PI_FLAG_ONLINK;
1438 if (pfx->pfx_autoconfflg)
1439 ndopt_pi->nd_opt_pi_flags_reserved |=
1440 ND_OPT_PI_FLAG_AUTO;
1444 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire)
1445 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1446 if (pfx->pfx_vltimeexpire == 0)
1447 vltime = pfx->pfx_validlifetime;
1449 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ?
1450 pfx->pfx_vltimeexpire - now.tv_sec : 0;
1455 if (pfx->pfx_pltimeexpire == 0)
1456 pltime = pfx->pfx_preflifetime;
1458 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ?
1459 pfx->pfx_pltimeexpire - now.tv_sec : 0;
1461 if (vltime < pltime) {
1463 * this can happen if vltime is decrement but pltime
1468 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1469 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1470 ndopt_pi->nd_opt_pi_reserved2 = 0;
1471 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix;
1473 buf += sizeof(struct nd_opt_prefix_info);
1476 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) {
1477 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6;
1479 ndopt_rti = (struct nd_opt_route_info *)buf;
1480 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1481 ndopt_rti->nd_opt_rti_len = 1 + psize;
1482 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen;
1483 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref;
1484 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime);
1485 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8);
1486 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1489 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1490 struct rdnss_addr *rdna;
1492 ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1493 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1494 ndopt_rdnss->nd_opt_rdnss_len = 0;
1495 ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1496 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime);
1497 buf += sizeof(struct nd_opt_rdnss);
1499 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) {
1500 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns));
1501 buf += sizeof(rdna->ra_dns);
1503 /* Length field should be in 8 octets */
1504 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8;
1506 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__,
1507 ndopt_rdnss->nd_opt_rdnss_len);
1510 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1511 struct dnssl_addr *dnsa;
1513 ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1514 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1515 ndopt_dnssl->nd_opt_dnssl_len = 0;
1516 ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1517 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime);
1518 buf += sizeof(*ndopt_dnssl);
1520 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) {
1521 memcpy(buf, dnsa->da_dom, dnsa->da_len);
1522 buf += dnsa->da_len;
1525 /* A zero octet after encoded DNS server list. */
1528 /* Padding to next 8 octets boundary */
1529 len = buf - (char *)ndopt_dnssl;
1530 len += (len % 8) ? 8 - len % 8 : 0;
1531 buf = (char *)ndopt_dnssl + len;
1533 /* Length field must be in 8 octets */
1534 ndopt_dnssl->nd_opt_dnssl_len = len / 8;
1536 syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__,
1537 ndopt_dnssl->nd_opt_dnssl_len);