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/if_var.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
43 #include <netinet/in.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip6.h>
46 #include <netinet6/ip6_var.h>
47 #include <netinet/icmp6.h>
48 #include <netinet6/nd6.h>
50 #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 ssize_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 if (len + len / 64 + 1 + 1 > DNAME_LABELENC_MAXLEN)
95 /* Length fields per 63 octets + '\0' (<= DNAME_LABELENC_MAXLEN) */
96 memset(dst, 0, len + len / 64 + 1 + 1);
98 syslog(LOG_DEBUG, "<%s> labelenc = %s", __func__, src);
99 while (src && (len = strlen(src)) != 0) {
100 /* Put a length field with 63 octet limitation first. */
101 p = strchr(src, '.');
103 *dst = len = MIN(63, len);
105 *dst = len = MIN(63, p - src);
106 if (dst + 1 + len < dst_origin + DNAME_LABELENC_MAXLEN)
110 /* Copy 63 octets at most. */
111 memcpy(dst, src, len);
113 if (p == NULL) /* the last label */
117 /* Always need a 0-length label at the tail. */
120 syslog(LOG_DEBUG, "<%s> labellen = %td", __func__, dst - dst_origin);
121 return (dst - dst_origin);
124 #define MUSTHAVE(var, cap) \
127 if ((t = agetnum(cap)) < 0) { \
128 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
135 #define MAYHAVE(var, cap, def) \
137 if ((var = agetnum(cap)) < 0) \
142 loadconfig_index(int idx)
144 char ifname[IFNAMSIZ];
146 syslog(LOG_DEBUG, "<%s> enter", __func__);
148 if (if_indextoname(idx, ifname) != NULL)
149 return (loadconfig_ifname(ifname));
155 loadconfig_ifname(char *ifname)
159 syslog(LOG_DEBUG, "<%s> enter", __func__);
161 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
162 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
163 /* NULL means all IFs will be processed. */
164 if (ifname != NULL &&
165 strcmp(ifi->ifi_ifname, ifname) != 0)
168 if (!ifi->ifi_persist) {
170 "<%s> %s is not a target interface. "
171 "Ignored at this moment.", __func__,
176 if (ifi->ifi_ifindex == 0) {
178 "<%s> %s not found. "
179 "Ignored at this moment.", __func__,
183 if (getconfig(ifi) == NULL) {
185 "<%s> invalid configuration for %s. "
186 "Ignored at this moment.", __func__,
195 rm_ifinfo_index(int idx)
199 ifi = if_indextoifinfo(idx);
201 syslog(LOG_ERR, "<%s>: ifinfo not found (idx=%d)",
206 return (rm_ifinfo(ifi));
210 rm_ifinfo(struct ifinfo *ifi)
214 syslog(LOG_DEBUG, "<%s> enter (%s).", __func__, ifi->ifi_ifname);
215 switch (ifi->ifi_state) {
216 case IFI_STATE_UNCONFIGURED:
220 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
222 "<%s> ifname=%s marked as UNCONFIGURED.",
223 __func__, ifi->ifi_ifname);
225 /* XXX: No MC leaving here because index is disappeared */
227 /* Inactivate timer */
228 rtadvd_remove_timer(ifi->ifi_ra_timer);
229 ifi->ifi_ra_timer = NULL;
234 if (!ifi->ifi_persist) {
235 TAILQ_REMOVE(&ifilist, ifi, ifi_next);
236 syslog(LOG_DEBUG, "<%s>: ifinfo (idx=%d) removed.",
237 __func__, ifi->ifi_ifindex);
240 /* recreate an empty entry */
241 update_persist_ifinfo(&ifilist, ifi->ifi_ifname);
242 syslog(LOG_DEBUG, "<%s>: ifname=%s is persistent.",
243 __func__, ifi->ifi_ifname);
246 /* clean up rai if any */
247 switch (ifi->ifi_state) {
248 case IFI_STATE_CONFIGURED:
249 if (ifi->ifi_rainfo != NULL) {
250 error = rm_rainfo(ifi->ifi_rainfo);
253 ifi->ifi_rainfo = NULL;
256 case IFI_STATE_TRANSITIVE:
257 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
258 if (ifi->ifi_rainfo != NULL) {
259 error = rm_rainfo(ifi->ifi_rainfo);
262 ifi->ifi_rainfo = NULL;
263 ifi->ifi_rainfo_trans = NULL;
266 if (ifi->ifi_rainfo != NULL) {
267 error = rm_rainfo(ifi->ifi_rainfo);
270 ifi->ifi_rainfo = NULL;
272 if (ifi->ifi_rainfo_trans != NULL) {
273 error = rm_rainfo(ifi->ifi_rainfo_trans);
276 ifi->ifi_rainfo_trans = NULL;
281 syslog(LOG_DEBUG, "<%s> leave (%s).", __func__, ifi->ifi_ifname);
286 rm_rainfo(struct rainfo *rai)
289 struct soliciter *sol;
291 struct rdnss_addr *rdna;
295 syslog(LOG_DEBUG, "<%s>: enter", __func__);
297 TAILQ_REMOVE(&railist, rai, rai_next);
298 if (rai->rai_ifinfo != NULL)
299 syslog(LOG_DEBUG, "<%s>: rainfo (idx=%d) removed.",
300 __func__, rai->rai_ifinfo->ifi_ifindex);
302 if (rai->rai_ra_data != NULL)
303 free(rai->rai_ra_data);
305 while ((pfx = TAILQ_FIRST(&rai->rai_prefix)) != NULL)
307 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
308 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
311 while ((rdn = TAILQ_FIRST(&rai->rai_rdnss)) != NULL) {
312 TAILQ_REMOVE(&rai->rai_rdnss, rdn, rd_next);
313 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
314 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
319 while ((dns = TAILQ_FIRST(&rai->rai_dnssl)) != NULL) {
320 TAILQ_REMOVE(&rai->rai_dnssl, dns, dn_next);
323 while ((rti = TAILQ_FIRST(&rai->rai_route)) != NULL) {
324 TAILQ_REMOVE(&rai->rai_route, rti, rti_next);
328 syslog(LOG_DEBUG, "<%s>: leave", __func__);
334 getconfig(struct ifinfo *ifi)
340 struct rainfo *rai_old;
345 char *addr, *flagstr;
347 if (ifi == NULL) /* if does not exist */
350 if (ifi->ifi_state == IFI_STATE_TRANSITIVE &&
351 ifi->ifi_rainfo == NULL) {
352 syslog(LOG_INFO, "<%s> %s is shutting down. Skipped.",
353 __func__, ifi->ifi_ifname);
357 if ((stat = agetent(tbuf, ifi->ifi_ifname)) <= 0) {
358 memset(tbuf, 0, sizeof(tbuf));
360 "<%s> %s isn't defined in the configuration file"
361 " or the configuration file doesn't exist."
362 " Treat it as default",
363 __func__, ifi->ifi_ifname);
366 ELM_MALLOC(rai, exit(1));
367 TAILQ_INIT(&rai->rai_prefix);
368 TAILQ_INIT(&rai->rai_route);
369 TAILQ_INIT(&rai->rai_rdnss);
370 TAILQ_INIT(&rai->rai_dnssl);
371 TAILQ_INIT(&rai->rai_soliciter);
372 rai->rai_ifinfo = ifi;
374 /* gather on-link prefixes from the network interfaces. */
375 if (agetflag("noifprefix"))
376 rai->rai_advifprefix = 0;
378 rai->rai_advifprefix = 1;
380 /* get interface information */
381 if (agetflag("nolladdr"))
382 rai->rai_advlinkopt = 0;
384 rai->rai_advlinkopt = 1;
385 if (rai->rai_advlinkopt) {
386 if (ifi->ifi_sdl.sdl_type == 0) {
388 "<%s> can't get information of %s",
389 __func__, ifi->ifi_ifname);
390 goto getconfig_free_rai;
395 * set router configuration variables.
397 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
398 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
400 "<%s> maxinterval (%" PRIu32 ") on %s is invalid "
401 "(must be between %u and %u)", __func__, val,
402 ifi->ifi_ifname, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
403 goto getconfig_free_rai;
405 rai->rai_maxinterval = (uint16_t)val;
407 MAYHAVE(val, "mininterval", rai->rai_maxinterval/3);
408 if ((uint16_t)val < MIN_MININTERVAL ||
409 (uint16_t)val > (rai->rai_maxinterval * 3) / 4) {
411 "<%s> mininterval (%" PRIu32 ") on %s is invalid "
412 "(must be between %d and %d)",
413 __func__, val, ifi->ifi_ifname, MIN_MININTERVAL,
414 (rai->rai_maxinterval * 3) / 4);
415 goto getconfig_free_rai;
417 rai->rai_mininterval = (uint16_t)val;
419 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
420 rai->rai_hoplimit = val & 0xff;
422 if ((flagstr = (char *)agetstr("raflags", &bp))) {
424 if (strchr(flagstr, 'm'))
425 val |= ND_RA_FLAG_MANAGED;
426 if (strchr(flagstr, 'o'))
427 val |= ND_RA_FLAG_OTHER;
428 if (strchr(flagstr, 'h'))
429 val |= ND_RA_FLAG_RTPREF_HIGH;
430 if (strchr(flagstr, 'l')) {
431 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
432 syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
433 " router flags are exclusive", __func__);
434 goto getconfig_free_rai;
436 val |= ND_RA_FLAG_RTPREF_LOW;
439 MAYHAVE(val, "raflags", 0);
441 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED;
442 rai->rai_otherflg = val & ND_RA_FLAG_OTHER;
443 #ifndef ND_RA_FLAG_RTPREF_MASK
444 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
445 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
447 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
448 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) {
449 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
450 __func__, rai->rai_rtpref, ifi->ifi_ifname);
451 goto getconfig_free_rai;
454 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3);
455 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval ||
456 (uint16_t)val > MAXROUTERLIFETIME)) {
458 "<%s> router lifetime (%" PRIu32 ") on %s is invalid "
459 "(must be 0 or between %d and %d)",
460 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval,
462 goto getconfig_free_rai;
464 rai->rai_lifetime = val & 0xffff;
466 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
467 if (val < 0 || val > MAXREACHABLETIME) {
469 "<%s> reachable time (%" PRIu32 ") on %s is invalid "
470 "(must be no greater than %d)",
471 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME);
472 goto getconfig_free_rai;
474 rai->rai_reachabletime = (uint32_t)val;
476 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
477 if (val64 < 0 || val64 > 0xffffffff) {
478 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range",
479 __func__, val64, ifi->ifi_ifname);
480 goto getconfig_free_rai;
482 rai->rai_retranstimer = (uint32_t)val64;
484 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
486 "<%s> mobile-ip6 configuration not supported",
488 goto getconfig_free_rai;
490 /* prefix information */
493 * This is an implementation specific parameter to consider
494 * link propagation delays and poorly synchronized clocks when
495 * checking consistency of advertised lifetimes.
497 MAYHAVE(val, "clockskew", 0);
498 rai->rai_clockskew = val;
501 for (i = -1; i < MAXPREFIX; i++) {
504 makeentry(entbuf, sizeof(entbuf), i, "addr");
505 addr = (char *)agetstr(entbuf, &bp);
509 /* allocate memory to store prefix information */
510 ELM_MALLOC(pfx, exit(1));
511 pfx->pfx_rainfo = rai;
512 pfx->pfx_origin = PREFIX_FROM_CONFIG;
514 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) {
516 "<%s> inet_pton failed for %s",
518 goto getconfig_free_pfx;
520 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) {
522 "<%s> multicast prefix (%s) must "
523 "not be advertised on %s",
524 __func__, addr, ifi->ifi_ifname);
525 goto getconfig_free_pfx;
527 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix))
529 "<%s> link-local prefix (%s) will be"
531 __func__, addr, ifi->ifi_ifname);
533 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
534 MAYHAVE(val, entbuf, 64);
535 if (val < 0 || val > 128) {
536 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s "
537 "on %s out of range",
538 __func__, val, addr, ifi->ifi_ifname);
539 goto getconfig_free_pfx;
541 pfx->pfx_prefixlen = (int)val;
543 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
544 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
546 if (strchr(flagstr, 'l'))
547 val |= ND_OPT_PI_FLAG_ONLINK;
548 if (strchr(flagstr, 'a'))
549 val |= ND_OPT_PI_FLAG_AUTO;
552 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
554 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
555 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
557 makeentry(entbuf, sizeof(entbuf), i, "vltime");
558 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
559 if (val64 < 0 || val64 > 0xffffffff) {
560 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for "
561 "%s/%d on %s is out of range",
563 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
564 goto getconfig_free_pfx;
566 pfx->pfx_validlifetime = (uint32_t)val64;
568 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
569 if (agetflag(entbuf)) {
572 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
573 pfx->pfx_vltimeexpire =
574 now.tv_sec + pfx->pfx_validlifetime;
577 makeentry(entbuf, sizeof(entbuf), i, "pltime");
578 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
579 if (val64 < 0 || val64 > 0xffffffff) {
581 "<%s> pltime (%" PRIu64 ") for %s/%d on %s "
584 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
585 goto getconfig_free_pfx;
587 pfx->pfx_preflifetime = (uint32_t)val64;
589 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
590 if (agetflag(entbuf)) {
593 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
594 pfx->pfx_pltimeexpire =
595 now.tv_sec + pfx->pfx_preflifetime;
597 /* link into chain */
598 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
604 if (rai->rai_advifprefix && rai->rai_pfxs == 0)
607 MAYHAVE(val64, "mtu", 0);
608 if (val < 0 || val64 > 0xffffffff) {
610 "<%s> mtu (%" PRIu64 ") on %s out of range",
611 __func__, val64, ifi->ifi_ifname);
612 goto getconfig_free_rai;
614 rai->rai_linkmtu = (uint32_t)val64;
615 if (rai->rai_linkmtu == 0) {
618 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
619 strcmp(mtustr, "auto") == 0)
620 rai->rai_linkmtu = ifi->ifi_phymtu;
622 else if (rai->rai_linkmtu < IPV6_MMTU ||
623 rai->rai_linkmtu > ifi->ifi_phymtu) {
625 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must "
626 "be between least MTU (%d) and physical link MTU (%d)",
627 __func__, rai->rai_linkmtu, ifi->ifi_ifname,
628 IPV6_MMTU, ifi->ifi_phymtu);
629 goto getconfig_free_rai;
632 #ifdef SIOCSIFINFO_IN6
634 struct in6_ndireq ndi;
637 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
638 syslog(LOG_ERR, "<%s> socket: %s", __func__,
642 memset(&ndi, 0, sizeof(ndi));
643 strncpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname));
644 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0)
645 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
646 __func__, ifi->ifi_ifname, strerror(errno));
648 /* reflect the RA info to the host variables in kernel */
649 ndi.ndi.chlim = rai->rai_hoplimit;
650 ndi.ndi.retrans = rai->rai_retranstimer;
651 ndi.ndi.basereachable = rai->rai_reachabletime;
652 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0)
653 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
654 __func__, ifi->ifi_ifname, strerror(errno));
660 /* route information */
662 for (i = -1; i < MAXROUTE; i++) {
665 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
666 addr = (char *)agetstr(entbuf, &bp);
668 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
669 addr = (char *)agetstr(oentbuf, &bp);
671 fprintf(stderr, "%s was obsoleted. Use %s.\n",
677 /* allocate memory to store prefix information */
678 ELM_MALLOC(rti, exit(1));
680 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) {
681 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
683 goto getconfig_free_rti;
687 * XXX: currently there's no restriction in route information
688 * prefix according to
689 * draft-ietf-ipngwg-router-selection-00.txt.
690 * However, I think the similar restriction be necessary.
692 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
693 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
695 "<%s> multicast route (%s) must "
696 "not be advertised on %s",
697 __func__, addr, ifi->ifi_ifname);
698 goto getconfig_free_rti;
700 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
702 "<%s> link-local route (%s) will "
703 "be advertised on %s",
704 __func__, addr, ifi->ifi_ifname);
705 goto getconfig_free_rti;
709 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
710 /* XXX: 256 is a magic number for compatibility check. */
711 MAYHAVE(val, entbuf, 256);
713 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
714 MAYHAVE(val, oentbuf, 256);
716 fprintf(stderr, "%s was obsoleted. Use %s.\n",
721 if (val < 0 || val > 128) {
722 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s "
724 __func__, val, addr, ifi->ifi_ifname);
725 goto getconfig_free_rti;
727 rti->rti_prefixlen = (int)val;
729 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
730 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
732 if (strchr(flagstr, 'h'))
733 val |= ND_RA_FLAG_RTPREF_HIGH;
734 if (strchr(flagstr, 'l')) {
735 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
737 "<%s> the \'h\' and \'l\' route"
738 " preferences are exclusive",
740 goto getconfig_free_rti;
742 val |= ND_RA_FLAG_RTPREF_LOW;
745 MAYHAVE(val, entbuf, 256); /* XXX */
747 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
748 MAYHAVE(val, oentbuf, 256);
750 fprintf(stderr, "%s was obsoleted. Use %s.\n",
755 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
756 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) {
757 syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
759 __func__, rti->rti_rtpref, addr,
760 rti->rti_prefixlen, ifi->ifi_ifname);
761 goto getconfig_free_rti;
765 * Since the spec does not a default value, we should make
766 * this entry mandatory. However, FreeBSD 4.4 has shipped
767 * with this field being optional, we use the router lifetime
768 * as an ad-hoc default value with a warning message.
770 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
771 MAYHAVE(val64, entbuf, -1);
773 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
774 MAYHAVE(val64, oentbuf, -1);
776 fprintf(stderr, "%s was obsoleted. Use %s.\n",
779 fprintf(stderr, "%s should be specified "
780 "for interface %s.\n", entbuf,
782 val64 = rai->rai_lifetime;
785 if (val64 < 0 || val64 > 0xffffffff) {
786 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for "
787 "%s/%d on %s out of range", __func__,
788 val64, addr, rti->rti_prefixlen,
790 goto getconfig_free_rti;
792 rti->rti_ltime = (uint32_t)val64;
794 /* link into chain */
795 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next);
802 /* DNS server and DNS search list information */
803 for (i = -1; i < MAXRDNSSENT ; i++) {
805 struct rdnss_addr *rdna;
809 makeentry(entbuf, sizeof(entbuf), i, "rdnss");
810 addr = (char *)agetstr(entbuf, &bp);
813 ELM_MALLOC(rdn, exit(1));
815 TAILQ_INIT(&rdn->rd_list);
817 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
818 c = strcspn(ap, ",");
819 strncpy(abuf, ap, c);
821 ELM_MALLOC(rdna, goto getconfig_free_rdn);
822 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) {
823 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
826 goto getconfig_free_rdn;
828 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next);
831 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
832 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
833 if ((uint16_t)val < rai->rai_maxinterval ||
834 (uint16_t)val > rai->rai_maxinterval * 2) {
835 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
836 "(must be between %d and %d)",
837 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
838 rai->rai_maxinterval * 2);
839 goto getconfig_free_rdn;
843 /* link into chain */
844 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next);
847 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
848 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
854 for (i = -1; i < MAXDNSSLENT ; i++) {
856 struct dnssl_addr *dnsa;
860 makeentry(entbuf, sizeof(entbuf), i, "dnssl");
861 addr = (char *)agetstr(entbuf, &bp);
865 ELM_MALLOC(dns, exit(1));
867 TAILQ_INIT(&dns->dn_list);
869 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
870 c = strcspn(ap, ",");
871 strncpy(abuf, ap, c);
873 ELM_MALLOC(dnsa, goto getconfig_free_dns);
874 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf);
875 if (dnsa->da_len < 0) {
876 syslog(LOG_ERR, "Invalid dnssl entry: %s",
878 goto getconfig_free_dns;
880 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__,
882 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next);
885 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
886 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
887 if ((uint16_t)val < rai->rai_maxinterval ||
888 (uint16_t)val > rai->rai_maxinterval * 2) {
889 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
890 "(must be between %d and %d)",
891 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
892 rai->rai_maxinterval * 2);
893 goto getconfig_free_dns;
897 /* link into chain */
898 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next);
901 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) {
902 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next);
907 /* construct the sending packet */
911 * If an entry with the same ifindex exists, remove it first.
912 * Before the removal, RDNSS and DNSSL options with
913 * zero-lifetime will be sent.
915 switch (ifi->ifi_state) {
916 case IFI_STATE_UNCONFIGURED:
917 /* UNCONFIGURED -> TRANSITIVE */
919 error = sock_mc_join(&sock, ifi->ifi_ifindex);
923 ifi->ifi_state = IFI_STATE_TRANSITIVE;
924 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS;
925 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
927 /* The same two rai mean initial burst */
928 ifi->ifi_rainfo = rai;
929 ifi->ifi_rainfo_trans = rai;
930 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
932 if (ifi->ifi_ra_timer == NULL)
933 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
934 ra_timer_update, ifi, ifi);
935 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
936 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
940 "<%s> ifname=%s marked as TRANSITIVE (initial burst).",
941 __func__, ifi->ifi_ifname);
943 case IFI_STATE_CONFIGURED:
944 /* CONFIGURED -> TRANSITIVE */
945 rai_old = ifi->ifi_rainfo;
946 if (rai_old == NULL) {
948 "<%s> ifi_rainfo is NULL"
949 " in IFI_STATE_CONFIGURED.", __func__);
956 rai_old->rai_lifetime = 0;
957 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next)
959 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next)
962 ifi->ifi_rainfo_trans = rai_old;
963 ifi->ifi_state = IFI_STATE_TRANSITIVE;
964 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
965 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
967 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
968 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
972 "<%s> ifname=%s marked as TRANSITIVE"
973 " (transitional burst)",
974 __func__, ifi->ifi_ifname);
976 ifi->ifi_rainfo = rai;
977 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
979 case IFI_STATE_TRANSITIVE:
980 if (ifi->ifi_rainfo != NULL) {
981 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
982 /* Reinitialize initial burst */
983 rm_rainfo(ifi->ifi_rainfo);
984 ifi->ifi_rainfo = rai;
985 ifi->ifi_rainfo_trans = rai;
986 ifi->ifi_burstcount =
987 MAX_INITIAL_RTR_ADVERTISEMENTS;
988 ifi->ifi_burstinterval =
989 MAX_INITIAL_RTR_ADVERT_INTERVAL;
991 /* Replace ifi_rainfo with the new one */
992 rm_rainfo(ifi->ifi_rainfo);
993 ifi->ifi_rainfo = rai;
995 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
997 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
998 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
1001 /* XXX: NOTREACHED. Being shut down. */
1003 "<%s> %s is shutting down. Skipped.",
1004 __func__, ifi->ifi_ifname);
1020 get_prefix(struct rainfo *rai)
1022 struct ifaddrs *ifap, *ifa;
1026 char *p, *ep, *m, *lim;
1027 char ntopbuf[INET6_ADDRSTRLEN];
1029 if (getifaddrs(&ifap) < 0) {
1031 "<%s> can't get interface addresses",
1035 ifi = rai->rai_ifinfo;
1037 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1040 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0)
1042 if (ifa->ifa_addr->sa_family != AF_INET6)
1044 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1045 if (IN6_IS_ADDR_LINKLOCAL(a))
1048 /* get prefix length */
1049 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1050 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
1051 plen = prefixlen(m, lim);
1052 if (plen <= 0 || plen > 128) {
1053 syslog(LOG_ERR, "<%s> failed to get prefixlen "
1054 "or prefix is invalid",
1058 if (plen == 128) /* XXX */
1060 if (find_prefix(rai, a, plen)) {
1061 /* ignore a duplicated prefix. */
1065 /* allocate memory to store prefix info. */
1066 ELM_MALLOC(pfx, exit(1));
1068 /* set prefix, sweep bits outside of prefixlen */
1069 pfx->pfx_prefixlen = plen;
1070 memcpy(&pfx->pfx_prefix, a, sizeof(*a));
1071 p = (char *)&pfx->pfx_prefix;
1072 ep = (char *)(&pfx->pfx_prefix + 1);
1073 while (m < lim && p < ep)
1077 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1079 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
1083 "<%s> add %s/%d to prefix list on %s",
1084 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname);
1086 /* set other fields with protocol defaults */
1087 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME;
1088 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME;
1089 pfx->pfx_onlinkflg = 1;
1090 pfx->pfx_autoconfflg = 1;
1091 pfx->pfx_origin = PREFIX_FROM_KERNEL;
1092 pfx->pfx_rainfo = rai;
1094 /* link into chain */
1095 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1097 /* counter increment */
1105 makeentry(char *buf, size_t len, int id, const char *string)
1109 strlcpy(buf, string, len);
1111 snprintf(buf, len, "%s%d", string, id);
1115 * Add a prefix to the list of specified interface and reconstruct
1116 * the outgoing packet.
1117 * The prefix must not be in the list.
1118 * XXX: other parameters of the prefix (e.g. lifetime) should be
1119 * able to be specified.
1122 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
1126 char ntopbuf[INET6_ADDRSTRLEN];
1128 ifi = rai->rai_ifinfo;
1129 ELM_MALLOC(pfx, return);
1130 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr;
1131 pfx->pfx_prefixlen = ipr->ipr_plen;
1132 pfx->pfx_validlifetime = ipr->ipr_vltime;
1133 pfx->pfx_preflifetime = ipr->ipr_pltime;
1134 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink;
1135 pfx->pfx_autoconfflg = ipr->ipr_raf_auto;
1136 pfx->pfx_origin = PREFIX_FROM_DYNAMIC;
1137 pfx->pfx_rainfo = rai;
1139 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1141 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
1143 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1144 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname);
1150 * Delete a prefix to the list of specified interface and reconstruct
1151 * the outgoing packet.
1152 * The prefix must be in the list.
1155 delete_prefix(struct prefix *pfx)
1159 char ntopbuf[INET6_ADDRSTRLEN];
1161 rai = pfx->pfx_rainfo;
1162 ifi = rai->rai_ifinfo;
1163 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next);
1164 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
1166 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1167 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1169 rtadvd_remove_timer(pfx->pfx_timer);
1176 invalidate_prefix(struct prefix *pfx)
1178 struct timespec timo;
1181 char ntopbuf[INET6_ADDRSTRLEN];
1183 rai = pfx->pfx_rainfo;
1184 ifi = rai->rai_ifinfo;
1185 if (pfx->pfx_timer) { /* sanity check */
1187 "<%s> assumption failure: timer already exists",
1192 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
1193 "will expire in %ld seconds", __func__,
1194 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)),
1195 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo);
1197 /* set the expiration timer */
1198 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL);
1199 if (pfx->pfx_timer == NULL) {
1200 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
1201 "remove the prefix", __func__);
1204 timo.tv_sec = prefix_timo;
1206 rtadvd_set_timer(&timo, pfx->pfx_timer);
1209 static struct rtadvd_timer *
1210 prefix_timeout(void *arg)
1213 delete_prefix((struct prefix *)arg);
1219 update_prefix(struct prefix *pfx)
1223 char ntopbuf[INET6_ADDRSTRLEN];
1225 rai = pfx->pfx_rainfo;
1226 ifi = rai->rai_ifinfo;
1227 if (pfx->pfx_timer == NULL) { /* sanity check */
1229 "<%s> assumption failure: timer does not exist",
1234 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1235 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1236 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1238 /* stop the expiration timer */
1239 rtadvd_remove_timer(pfx->pfx_timer);
1240 pfx->pfx_timer = NULL;
1244 * Try to get an in6_prefixreq contents for a prefix which matches
1245 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1246 * the interface whose name is ipr->ipr_name[].
1249 init_prefix(struct in6_prefixreq *ipr)
1254 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1255 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1260 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
1261 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
1264 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1265 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1266 ipr->ipr_raf_onlink = 1;
1267 ipr->ipr_raf_auto = 1;
1268 /* omit other field initialization */
1270 else if (ipr->ipr_origin < PR_ORIG_RR) {
1271 char ntopbuf[INET6_ADDRSTRLEN];
1273 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1274 "lower than PR_ORIG_RR(router renumbering)."
1275 "This should not happen if I am router", __func__,
1276 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1277 sizeof(ntopbuf)), ipr->ipr_origin);
1285 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1286 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1287 ipr->ipr_raf_onlink = 1;
1288 ipr->ipr_raf_auto = 1;
1294 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1296 struct in6_prefixreq ipr;
1298 memset(&ipr, 0, sizeof(ipr));
1299 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1300 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't "
1301 "exist. This should not happen! %s", __func__,
1302 ifindex, strerror(errno));
1305 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1306 ipr.ipr_prefix.sin6_family = AF_INET6;
1307 ipr.ipr_prefix.sin6_addr = *addr;
1308 ipr.ipr_plen = plen;
1310 if (init_prefix(&ipr))
1311 return; /* init failed by some error */
1312 add_prefix(rai, &ipr);
1316 make_packet(struct rainfo *rai)
1318 size_t packlen, lladdroptlen = 0;
1320 struct nd_router_advert *ra;
1321 struct nd_opt_prefix_info *ndopt_pi;
1322 struct nd_opt_mtu *ndopt_mtu;
1323 struct nd_opt_route_info *ndopt_rti;
1325 struct nd_opt_rdnss *ndopt_rdnss;
1327 struct nd_opt_dnssl *ndopt_dnssl;
1333 ifi = rai->rai_ifinfo;
1334 /* calculate total length */
1335 packlen = sizeof(struct nd_router_advert);
1336 if (rai->rai_advlinkopt) {
1337 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) {
1339 "<%s> link-layer address option has"
1340 " null length on %s. Treat as not included.",
1341 __func__, ifi->ifi_ifname);
1342 rai->rai_advlinkopt = 0;
1344 packlen += lladdroptlen;
1347 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs;
1348 if (rai->rai_linkmtu)
1349 packlen += sizeof(struct nd_opt_mtu);
1351 TAILQ_FOREACH(rti, &rai->rai_route, rti_next)
1352 packlen += sizeof(struct nd_opt_route_info) +
1353 ((rti->rti_prefixlen + 0x3f) >> 6) * 8;
1355 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1356 struct rdnss_addr *rdna;
1358 packlen += sizeof(struct nd_opt_rdnss);
1359 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next)
1360 packlen += sizeof(rdna->ra_dns);
1362 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1363 struct dnssl_addr *dnsa;
1365 packlen += sizeof(struct nd_opt_dnssl);
1367 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next)
1368 len += dnsa->da_len;
1370 /* A zero octet and 8 octet boundary */
1372 len += (len % 8) ? 8 - len % 8 : 0;
1376 /* allocate memory for the packet */
1377 if ((buf = malloc(packlen)) == NULL) {
1379 "<%s> can't get enough memory for an RA packet",
1383 memset(buf, 0, packlen);
1384 if (rai->rai_ra_data) /* Free old data if any. */
1385 free(rai->rai_ra_data);
1386 rai->rai_ra_data = buf;
1387 /* XXX: what if packlen > 576? */
1388 rai->rai_ra_datalen = packlen;
1391 * construct the packet
1393 ra = (struct nd_router_advert *)buf;
1394 ra->nd_ra_type = ND_ROUTER_ADVERT;
1396 ra->nd_ra_cksum = 0;
1397 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit);
1398 ra->nd_ra_flags_reserved = 0; /* just in case */
1400 * XXX: the router preference field, which is a 2-bit field, should be
1401 * initialized before other fields.
1403 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref;
1404 ra->nd_ra_flags_reserved |=
1405 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0;
1406 ra->nd_ra_flags_reserved |=
1407 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0;
1408 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime);
1409 ra->nd_ra_reachable = htonl(rai->rai_reachabletime);
1410 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer);
1413 if (rai->rai_advlinkopt) {
1414 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf);
1415 buf += lladdroptlen;
1418 if (rai->rai_linkmtu) {
1419 ndopt_mtu = (struct nd_opt_mtu *)buf;
1420 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1421 ndopt_mtu->nd_opt_mtu_len = 1;
1422 ndopt_mtu->nd_opt_mtu_reserved = 0;
1423 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu);
1424 buf += sizeof(struct nd_opt_mtu);
1427 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1428 uint32_t vltime, pltime;
1429 struct timespec now;
1431 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1432 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1433 ndopt_pi->nd_opt_pi_len = 4;
1434 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen;
1435 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1436 if (pfx->pfx_onlinkflg)
1437 ndopt_pi->nd_opt_pi_flags_reserved |=
1438 ND_OPT_PI_FLAG_ONLINK;
1439 if (pfx->pfx_autoconfflg)
1440 ndopt_pi->nd_opt_pi_flags_reserved |=
1441 ND_OPT_PI_FLAG_AUTO;
1445 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire)
1446 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1447 if (pfx->pfx_vltimeexpire == 0)
1448 vltime = pfx->pfx_validlifetime;
1450 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ?
1451 pfx->pfx_vltimeexpire - now.tv_sec : 0;
1456 if (pfx->pfx_pltimeexpire == 0)
1457 pltime = pfx->pfx_preflifetime;
1459 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ?
1460 pfx->pfx_pltimeexpire - now.tv_sec : 0;
1462 if (vltime < pltime) {
1464 * this can happen if vltime is decrement but pltime
1469 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1470 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1471 ndopt_pi->nd_opt_pi_reserved2 = 0;
1472 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix;
1474 buf += sizeof(struct nd_opt_prefix_info);
1477 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) {
1478 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6;
1480 ndopt_rti = (struct nd_opt_route_info *)buf;
1481 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1482 ndopt_rti->nd_opt_rti_len = 1 + psize;
1483 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen;
1484 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref;
1485 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime);
1486 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8);
1487 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1490 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1491 struct rdnss_addr *rdna;
1493 ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1494 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1495 ndopt_rdnss->nd_opt_rdnss_len = 0;
1496 ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1497 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime);
1498 buf += sizeof(struct nd_opt_rdnss);
1500 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) {
1501 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns));
1502 buf += sizeof(rdna->ra_dns);
1504 /* Length field should be in 8 octets */
1505 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8;
1507 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__,
1508 ndopt_rdnss->nd_opt_rdnss_len);
1511 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1512 struct dnssl_addr *dnsa;
1514 ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1515 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1516 ndopt_dnssl->nd_opt_dnssl_len = 0;
1517 ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1518 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime);
1519 buf += sizeof(*ndopt_dnssl);
1521 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) {
1522 memcpy(buf, dnsa->da_dom, dnsa->da_len);
1523 buf += dnsa->da_len;
1526 /* A zero octet after encoded DNS server list. */
1529 /* Padding to next 8 octets boundary */
1530 len = buf - (char *)ndopt_dnssl;
1531 len += (len % 8) ? 8 - len % 8 : 0;
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);