2 /* $KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (C) 1998 WIDE Project.
8 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.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>
71 /* label of tcapcode + number + domain name + zero octet */
72 static char entbuf[10 + 3 + NI_MAXHOST + 1];
73 static char oentbuf[10 + 3 + NI_MAXHOST + 1];
74 static char abuf[DNAME_LABELENC_MAXLEN];
76 static time_t prefix_timo = (60 * 120); /* 2 hours.
77 * XXX: should be configurable. */
79 static struct rtadvd_timer *prefix_timeout(void *);
80 static void makeentry(char *, size_t, int, const char *);
81 static ssize_t dname_labelenc(char *, const char *);
83 /* Encode domain name label encoding in RFC 1035 Section 3.1 */
85 dname_labelenc(char *dst, const char *src)
94 if (len + len / 64 + 1 + 1 > DNAME_LABELENC_MAXLEN)
96 /* Length fields per 63 octets + '\0' (<= DNAME_LABELENC_MAXLEN) */
97 memset(dst, 0, len + len / 64 + 1 + 1);
99 syslog(LOG_DEBUG, "<%s> labelenc = %s", __func__, src);
100 while (src && (len = strlen(src)) != 0) {
101 /* Put a length field with 63 octet limitation first. */
102 p = strchr(src, '.');
104 *dst = len = MIN(63, len);
106 *dst = len = MIN(63, p - src);
107 if (dst + 1 + len < dst_origin + DNAME_LABELENC_MAXLEN)
111 /* Copy 63 octets at most. */
112 memcpy(dst, src, len);
114 if (p == NULL) /* the last label */
118 /* Always need a 0-length label at the tail. */
121 syslog(LOG_DEBUG, "<%s> labellen = %td", __func__, dst - dst_origin);
122 return (dst - dst_origin);
125 #define MUSTHAVE(var, cap) \
128 if ((t = agetnum(cap)) < 0) { \
129 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
136 #define MAYHAVE(var, cap, def) \
138 if ((var = agetnum(cap)) < 0) \
143 loadconfig_index(int idx)
145 char ifname[IFNAMSIZ];
147 syslog(LOG_DEBUG, "<%s> enter", __func__);
149 if (if_indextoname(idx, ifname) != NULL)
150 return (loadconfig_ifname(ifname));
156 loadconfig_ifname(char *ifname)
160 syslog(LOG_DEBUG, "<%s> enter", __func__);
162 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
163 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
164 /* NULL means all IFs will be processed. */
165 if (ifname != NULL &&
166 strcmp(ifi->ifi_ifname, ifname) != 0)
169 if (!ifi->ifi_persist) {
171 "<%s> %s is not a target interface. "
172 "Ignored at this moment.", __func__,
177 if (ifi->ifi_ifindex == 0) {
179 "<%s> %s not found. "
180 "Ignored at this moment.", __func__,
184 if (getconfig(ifi) == NULL) {
186 "<%s> invalid configuration for %s. "
187 "Ignored at this moment.", __func__,
196 rm_ifinfo_index(int idx)
200 ifi = if_indextoifinfo(idx);
202 syslog(LOG_ERR, "<%s>: ifinfo not found (idx=%d)",
207 return (rm_ifinfo(ifi));
211 rm_ifinfo(struct ifinfo *ifi)
215 syslog(LOG_DEBUG, "<%s> enter (%s).", __func__, ifi->ifi_ifname);
216 switch (ifi->ifi_state) {
217 case IFI_STATE_UNCONFIGURED:
221 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
223 "<%s> ifname=%s marked as UNCONFIGURED.",
224 __func__, ifi->ifi_ifname);
226 /* XXX: No MC leaving here because index is disappeared */
228 /* Inactivate timer */
229 rtadvd_remove_timer(ifi->ifi_ra_timer);
230 ifi->ifi_ra_timer = NULL;
235 if (!ifi->ifi_persist) {
236 TAILQ_REMOVE(&ifilist, ifi, ifi_next);
237 syslog(LOG_DEBUG, "<%s>: ifinfo (idx=%d) removed.",
238 __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);
282 if (!ifi->ifi_persist)
288 rm_rainfo(struct rainfo *rai)
291 struct soliciter *sol;
293 struct rdnss_addr *rdna;
297 syslog(LOG_DEBUG, "<%s>: enter", __func__);
299 TAILQ_REMOVE(&railist, rai, rai_next);
300 if (rai->rai_ifinfo != NULL)
301 syslog(LOG_DEBUG, "<%s>: rainfo (idx=%d) removed.",
302 __func__, rai->rai_ifinfo->ifi_ifindex);
304 if (rai->rai_ra_data != NULL)
305 free(rai->rai_ra_data);
307 while ((pfx = TAILQ_FIRST(&rai->rai_prefix)) != NULL)
309 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
310 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
313 while ((rdn = TAILQ_FIRST(&rai->rai_rdnss)) != NULL) {
314 TAILQ_REMOVE(&rai->rai_rdnss, rdn, rd_next);
315 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
316 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
321 while ((dns = TAILQ_FIRST(&rai->rai_dnssl)) != NULL) {
322 TAILQ_REMOVE(&rai->rai_dnssl, dns, dn_next);
325 while ((rti = TAILQ_FIRST(&rai->rai_route)) != NULL) {
326 TAILQ_REMOVE(&rai->rai_route, rti, rti_next);
330 syslog(LOG_DEBUG, "<%s>: leave", __func__);
336 getconfig(struct ifinfo *ifi)
342 struct rainfo *rai_old;
347 char *addr, *flagstr;
349 if (ifi == NULL) /* if does not exist */
352 if (ifi->ifi_state == IFI_STATE_TRANSITIVE &&
353 ifi->ifi_rainfo == NULL) {
354 syslog(LOG_INFO, "<%s> %s is shutting down. Skipped.",
355 __func__, ifi->ifi_ifname);
359 if ((stat = agetent(tbuf, ifi->ifi_ifname)) <= 0) {
360 memset(tbuf, 0, sizeof(tbuf));
362 "<%s> %s isn't defined in the configuration file"
363 " or the configuration file doesn't exist."
364 " Treat it as default",
365 __func__, ifi->ifi_ifname);
368 ELM_MALLOC(rai, exit(1));
369 TAILQ_INIT(&rai->rai_prefix);
370 TAILQ_INIT(&rai->rai_route);
371 TAILQ_INIT(&rai->rai_rdnss);
372 TAILQ_INIT(&rai->rai_dnssl);
373 TAILQ_INIT(&rai->rai_soliciter);
374 rai->rai_ifinfo = ifi;
376 /* gather on-link prefixes from the network interfaces. */
377 if (agetflag("noifprefix"))
378 rai->rai_advifprefix = 0;
380 rai->rai_advifprefix = 1;
382 /* get interface information */
383 if (agetflag("nolladdr"))
384 rai->rai_advlinkopt = 0;
386 rai->rai_advlinkopt = 1;
387 if (rai->rai_advlinkopt) {
388 if (ifi->ifi_sdl.sdl_type == 0) {
390 "<%s> can't get information of %s",
391 __func__, ifi->ifi_ifname);
392 goto getconfig_free_rai;
397 * set router configuration variables.
399 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
400 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
402 "<%s> maxinterval (%" PRIu32 ") on %s is invalid "
403 "(must be between %u and %u)", __func__, val,
404 ifi->ifi_ifname, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
405 goto getconfig_free_rai;
407 rai->rai_maxinterval = (uint16_t)val;
409 MAYHAVE(val, "mininterval", rai->rai_maxinterval/3);
410 if ((uint16_t)val < MIN_MININTERVAL ||
411 (uint16_t)val > (rai->rai_maxinterval * 3) / 4) {
413 "<%s> mininterval (%" PRIu32 ") on %s is invalid "
414 "(must be between %d and %d)",
415 __func__, val, ifi->ifi_ifname, MIN_MININTERVAL,
416 (rai->rai_maxinterval * 3) / 4);
417 goto getconfig_free_rai;
419 rai->rai_mininterval = (uint16_t)val;
421 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
422 rai->rai_hoplimit = val & 0xff;
424 if ((flagstr = (char *)agetstr("raflags", &bp))) {
426 if (strchr(flagstr, 'm'))
427 val |= ND_RA_FLAG_MANAGED;
428 if (strchr(flagstr, 'o'))
429 val |= ND_RA_FLAG_OTHER;
430 if (strchr(flagstr, 'h'))
431 val |= ND_RA_FLAG_RTPREF_HIGH;
432 if (strchr(flagstr, 'l')) {
433 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
434 syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
435 " router flags are exclusive", __func__);
436 goto getconfig_free_rai;
438 val |= ND_RA_FLAG_RTPREF_LOW;
441 MAYHAVE(val, "raflags", 0);
443 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED;
444 rai->rai_otherflg = val & ND_RA_FLAG_OTHER;
445 #ifndef ND_RA_FLAG_RTPREF_MASK
446 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
447 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
449 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
450 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) {
451 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
452 __func__, rai->rai_rtpref, ifi->ifi_ifname);
453 goto getconfig_free_rai;
456 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3);
457 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval ||
458 (uint16_t)val > MAXROUTERLIFETIME)) {
460 "<%s> router lifetime (%" PRIu32 ") on %s is invalid "
461 "(must be 0 or between %d and %d)",
462 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval,
464 goto getconfig_free_rai;
466 rai->rai_lifetime = val & 0xffff;
468 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
469 if (val < 0 || val > MAXREACHABLETIME) {
471 "<%s> reachable time (%" PRIu32 ") on %s is invalid "
472 "(must be no greater than %d)",
473 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME);
474 goto getconfig_free_rai;
476 rai->rai_reachabletime = (uint32_t)val;
478 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
479 if (val64 < 0 || val64 > 0xffffffff) {
480 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range",
481 __func__, val64, ifi->ifi_ifname);
482 goto getconfig_free_rai;
484 rai->rai_retranstimer = (uint32_t)val64;
486 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
488 "<%s> mobile-ip6 configuration not supported",
490 goto getconfig_free_rai;
492 /* prefix information */
495 * This is an implementation specific parameter to consider
496 * link propagation delays and poorly synchronized clocks when
497 * checking consistency of advertised lifetimes.
499 MAYHAVE(val, "clockskew", 0);
500 rai->rai_clockskew = val;
503 for (i = -1; i < MAXPREFIX; i++) {
506 makeentry(entbuf, sizeof(entbuf), i, "addr");
507 addr = (char *)agetstr(entbuf, &bp);
511 /* allocate memory to store prefix information */
512 ELM_MALLOC(pfx, exit(1));
513 pfx->pfx_rainfo = rai;
514 pfx->pfx_origin = PREFIX_FROM_CONFIG;
516 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) {
518 "<%s> inet_pton failed for %s",
520 goto getconfig_free_pfx;
522 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) {
524 "<%s> multicast prefix (%s) must "
525 "not be advertised on %s",
526 __func__, addr, ifi->ifi_ifname);
527 goto getconfig_free_pfx;
529 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix))
531 "<%s> link-local prefix (%s) will be"
533 __func__, addr, ifi->ifi_ifname);
535 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
536 MAYHAVE(val, entbuf, 64);
537 if (val < 0 || val > 128) {
538 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s "
539 "on %s out of range",
540 __func__, val, addr, ifi->ifi_ifname);
541 goto getconfig_free_pfx;
543 pfx->pfx_prefixlen = (int)val;
545 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
546 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
548 if (strchr(flagstr, 'l'))
549 val |= ND_OPT_PI_FLAG_ONLINK;
550 if (strchr(flagstr, 'a'))
551 val |= ND_OPT_PI_FLAG_AUTO;
554 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
556 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
557 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
559 makeentry(entbuf, sizeof(entbuf), i, "vltime");
560 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
561 if (val64 < 0 || val64 > 0xffffffff) {
562 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for "
563 "%s/%d on %s is out of range",
565 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
566 goto getconfig_free_pfx;
568 pfx->pfx_validlifetime = (uint32_t)val64;
570 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
571 if (agetflag(entbuf)) {
574 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
575 pfx->pfx_vltimeexpire =
576 now.tv_sec + pfx->pfx_validlifetime;
579 makeentry(entbuf, sizeof(entbuf), i, "pltime");
580 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
581 if (val64 < 0 || val64 > 0xffffffff) {
583 "<%s> pltime (%" PRIu64 ") for %s/%d on %s "
586 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
587 goto getconfig_free_pfx;
589 pfx->pfx_preflifetime = (uint32_t)val64;
591 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
592 if (agetflag(entbuf)) {
595 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
596 pfx->pfx_pltimeexpire =
597 now.tv_sec + pfx->pfx_preflifetime;
599 /* link into chain */
600 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
606 if (rai->rai_advifprefix && rai->rai_pfxs == 0)
609 MAYHAVE(val64, "mtu", 0);
610 if (val < 0 || val64 > 0xffffffff) {
612 "<%s> mtu (%" PRIu64 ") on %s out of range",
613 __func__, val64, ifi->ifi_ifname);
614 goto getconfig_free_rai;
616 rai->rai_linkmtu = (uint32_t)val64;
617 if (rai->rai_linkmtu == 0) {
620 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
621 strcmp(mtustr, "auto") == 0)
622 rai->rai_linkmtu = ifi->ifi_phymtu;
624 else if (rai->rai_linkmtu < IPV6_MMTU ||
625 rai->rai_linkmtu > ifi->ifi_phymtu) {
627 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must "
628 "be between least MTU (%d) and physical link MTU (%d)",
629 __func__, rai->rai_linkmtu, ifi->ifi_ifname,
630 IPV6_MMTU, ifi->ifi_phymtu);
631 goto getconfig_free_rai;
634 #ifdef SIOCSIFINFO_IN6
636 struct in6_ndireq ndi;
639 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
640 syslog(LOG_ERR, "<%s> socket: %s", __func__,
644 memset(&ndi, 0, sizeof(ndi));
645 strlcpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname));
646 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0)
647 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
648 __func__, ifi->ifi_ifname, strerror(errno));
650 /* reflect the RA info to the host variables in kernel */
651 ndi.ndi.chlim = rai->rai_hoplimit;
652 ndi.ndi.retrans = rai->rai_retranstimer;
653 ndi.ndi.basereachable = rai->rai_reachabletime;
654 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0)
655 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
656 __func__, ifi->ifi_ifname, strerror(errno));
662 /* route information */
664 for (i = -1; i < MAXROUTE; i++) {
667 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
668 addr = (char *)agetstr(entbuf, &bp);
670 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
671 addr = (char *)agetstr(oentbuf, &bp);
673 fprintf(stderr, "%s was obsoleted. Use %s.\n",
679 /* allocate memory to store prefix information */
680 ELM_MALLOC(rti, exit(1));
682 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) {
683 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
685 goto getconfig_free_rti;
689 * XXX: currently there's no restriction in route information
690 * prefix according to
691 * draft-ietf-ipngwg-router-selection-00.txt.
692 * However, I think the similar restriction be necessary.
694 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
695 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
697 "<%s> multicast route (%s) must "
698 "not be advertised on %s",
699 __func__, addr, ifi->ifi_ifname);
700 goto getconfig_free_rti;
702 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
704 "<%s> link-local route (%s) will "
705 "be advertised on %s",
706 __func__, addr, ifi->ifi_ifname);
707 goto getconfig_free_rti;
711 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
712 /* XXX: 256 is a magic number for compatibility check. */
713 MAYHAVE(val, entbuf, 256);
715 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
716 MAYHAVE(val, oentbuf, 256);
718 fprintf(stderr, "%s was obsoleted. Use %s.\n",
723 if (val < 0 || val > 128) {
724 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s "
726 __func__, val, addr, ifi->ifi_ifname);
727 goto getconfig_free_rti;
729 rti->rti_prefixlen = (int)val;
731 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
732 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
734 if (strchr(flagstr, 'h'))
735 val |= ND_RA_FLAG_RTPREF_HIGH;
736 if (strchr(flagstr, 'l')) {
737 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
739 "<%s> the \'h\' and \'l\' route"
740 " preferences are exclusive",
742 goto getconfig_free_rti;
744 val |= ND_RA_FLAG_RTPREF_LOW;
747 MAYHAVE(val, entbuf, 256); /* XXX */
749 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
750 MAYHAVE(val, oentbuf, 256);
752 fprintf(stderr, "%s was obsoleted. Use %s.\n",
757 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
758 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) {
759 syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
761 __func__, rti->rti_rtpref, addr,
762 rti->rti_prefixlen, ifi->ifi_ifname);
763 goto getconfig_free_rti;
767 * Since the spec does not a default value, we should make
768 * this entry mandatory. However, FreeBSD 4.4 has shipped
769 * with this field being optional, we use the router lifetime
770 * as an ad-hoc default value with a warning message.
772 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
773 MAYHAVE(val64, entbuf, -1);
775 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
776 MAYHAVE(val64, oentbuf, -1);
778 fprintf(stderr, "%s was obsoleted. Use %s.\n",
781 fprintf(stderr, "%s should be specified "
782 "for interface %s.\n", entbuf,
784 val64 = rai->rai_lifetime;
787 if (val64 < 0 || val64 > 0xffffffff) {
788 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for "
789 "%s/%d on %s out of range", __func__,
790 val64, addr, rti->rti_prefixlen,
792 goto getconfig_free_rti;
794 rti->rti_ltime = (uint32_t)val64;
796 /* link into chain */
797 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next);
804 /* DNS server and DNS search list information */
805 for (i = -1; i < MAXRDNSSENT ; i++) {
807 struct rdnss_addr *rdna;
811 makeentry(entbuf, sizeof(entbuf), i, "rdnss");
812 addr = (char *)agetstr(entbuf, &bp);
815 ELM_MALLOC(rdn, exit(1));
817 TAILQ_INIT(&rdn->rd_list);
819 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
820 c = strcspn(ap, ",");
821 strncpy(abuf, ap, c);
823 ELM_MALLOC(rdna, goto getconfig_free_rdn);
824 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) {
825 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
828 goto getconfig_free_rdn;
830 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next);
833 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
834 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
835 if ((uint16_t)val < rai->rai_maxinterval ||
836 (uint16_t)val > rai->rai_maxinterval * 2) {
837 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
838 "(must be between %d and %d)",
839 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
840 rai->rai_maxinterval * 2);
841 goto getconfig_free_rdn;
845 /* link into chain */
846 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next);
849 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
850 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
856 for (i = -1; i < MAXDNSSLENT ; i++) {
858 struct dnssl_addr *dnsa;
862 makeentry(entbuf, sizeof(entbuf), i, "dnssl");
863 addr = (char *)agetstr(entbuf, &bp);
867 ELM_MALLOC(dns, exit(1));
869 TAILQ_INIT(&dns->dn_list);
871 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
872 c = strcspn(ap, ",");
873 strncpy(abuf, ap, c);
875 ELM_MALLOC(dnsa, goto getconfig_free_dns);
876 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf);
877 if (dnsa->da_len < 0) {
878 syslog(LOG_ERR, "Invalid dnssl entry: %s",
880 goto getconfig_free_dns;
882 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__,
884 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next);
887 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
888 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
889 if ((uint16_t)val < rai->rai_maxinterval ||
890 (uint16_t)val > rai->rai_maxinterval * 2) {
891 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
892 "(must be between %d and %d)",
893 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
894 rai->rai_maxinterval * 2);
895 goto getconfig_free_dns;
899 /* link into chain */
900 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next);
903 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) {
904 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next);
909 /* construct the sending packet */
913 * If an entry with the same ifindex exists, remove it first.
914 * Before the removal, RDNSS and DNSSL options with
915 * zero-lifetime will be sent.
917 switch (ifi->ifi_state) {
918 case IFI_STATE_UNCONFIGURED:
919 /* UNCONFIGURED -> TRANSITIVE */
921 error = sock_mc_join(&sock, ifi->ifi_ifindex);
925 ifi->ifi_state = IFI_STATE_TRANSITIVE;
926 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS;
927 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
929 /* The same two rai mean initial burst */
930 ifi->ifi_rainfo = rai;
931 ifi->ifi_rainfo_trans = rai;
932 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
934 if (ifi->ifi_ra_timer == NULL)
935 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
936 ra_timer_update, ifi, ifi);
937 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
938 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
942 "<%s> ifname=%s marked as TRANSITIVE (initial burst).",
943 __func__, ifi->ifi_ifname);
945 case IFI_STATE_CONFIGURED:
946 /* CONFIGURED -> TRANSITIVE */
947 rai_old = ifi->ifi_rainfo;
948 if (rai_old == NULL) {
950 "<%s> ifi_rainfo is NULL"
951 " in IFI_STATE_CONFIGURED.", __func__);
958 rai_old->rai_lifetime = 0;
959 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next)
961 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next)
964 ifi->ifi_rainfo_trans = rai_old;
965 ifi->ifi_state = IFI_STATE_TRANSITIVE;
966 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
967 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
969 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
970 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
974 "<%s> ifname=%s marked as TRANSITIVE"
975 " (transitional burst)",
976 __func__, ifi->ifi_ifname);
978 ifi->ifi_rainfo = rai;
979 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
981 case IFI_STATE_TRANSITIVE:
982 if (ifi->ifi_rainfo != NULL) {
983 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
984 /* Reinitialize initial burst */
985 rm_rainfo(ifi->ifi_rainfo);
986 ifi->ifi_rainfo = rai;
987 ifi->ifi_rainfo_trans = rai;
988 ifi->ifi_burstcount =
989 MAX_INITIAL_RTR_ADVERTISEMENTS;
990 ifi->ifi_burstinterval =
991 MAX_INITIAL_RTR_ADVERT_INTERVAL;
993 /* Replace ifi_rainfo with the new one */
994 rm_rainfo(ifi->ifi_rainfo);
995 ifi->ifi_rainfo = rai;
997 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
999 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
1000 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
1003 /* XXX: NOTREACHED. Being shut down. */
1005 "<%s> %s is shutting down. Skipped.",
1006 __func__, ifi->ifi_ifname);
1022 get_prefix(struct rainfo *rai)
1024 struct ifaddrs *ifap, *ifa;
1028 char *p, *ep, *m, *lim;
1029 char ntopbuf[INET6_ADDRSTRLEN];
1031 if (getifaddrs(&ifap) < 0) {
1033 "<%s> can't get interface addresses",
1037 ifi = rai->rai_ifinfo;
1039 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1042 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0)
1044 if (ifa->ifa_addr->sa_family != AF_INET6)
1046 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1047 if (IN6_IS_ADDR_LINKLOCAL(a))
1050 /* get prefix length */
1051 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1052 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
1053 plen = prefixlen(m, lim);
1054 if (plen <= 0 || plen > 128) {
1055 syslog(LOG_ERR, "<%s> failed to get prefixlen "
1056 "or prefix is invalid",
1060 if (plen == 128) /* XXX */
1062 if (find_prefix(rai, a, plen)) {
1063 /* ignore a duplicated prefix. */
1067 /* allocate memory to store prefix info. */
1068 ELM_MALLOC(pfx, exit(1));
1070 /* set prefix, sweep bits outside of prefixlen */
1071 pfx->pfx_prefixlen = plen;
1072 memcpy(&pfx->pfx_prefix, a, sizeof(*a));
1073 p = (char *)&pfx->pfx_prefix;
1074 ep = (char *)(&pfx->pfx_prefix + 1);
1075 while (m < lim && p < ep)
1079 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1081 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
1085 "<%s> add %s/%d to prefix list on %s",
1086 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname);
1088 /* set other fields with protocol defaults */
1089 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME;
1090 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME;
1091 pfx->pfx_onlinkflg = 1;
1092 pfx->pfx_autoconfflg = 1;
1093 pfx->pfx_origin = PREFIX_FROM_KERNEL;
1094 pfx->pfx_rainfo = rai;
1096 /* link into chain */
1097 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1099 /* counter increment */
1107 makeentry(char *buf, size_t len, int id, const char *string)
1111 strlcpy(buf, string, len);
1113 snprintf(buf, len, "%s%d", string, id);
1117 * Add a prefix to the list of specified interface and reconstruct
1118 * the outgoing packet.
1119 * The prefix must not be in the list.
1120 * XXX: other parameters of the prefix (e.g. lifetime) should be
1121 * able to be specified.
1124 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
1128 char ntopbuf[INET6_ADDRSTRLEN];
1130 ifi = rai->rai_ifinfo;
1131 ELM_MALLOC(pfx, return);
1132 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr;
1133 pfx->pfx_prefixlen = ipr->ipr_plen;
1134 pfx->pfx_validlifetime = ipr->ipr_vltime;
1135 pfx->pfx_preflifetime = ipr->ipr_pltime;
1136 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink;
1137 pfx->pfx_autoconfflg = ipr->ipr_raf_auto;
1138 pfx->pfx_origin = PREFIX_FROM_DYNAMIC;
1139 pfx->pfx_rainfo = rai;
1141 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1143 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
1145 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1146 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname);
1152 * Delete a prefix to the list of specified interface and reconstruct
1153 * the outgoing packet.
1154 * The prefix must be in the list.
1157 delete_prefix(struct prefix *pfx)
1161 char ntopbuf[INET6_ADDRSTRLEN];
1163 rai = pfx->pfx_rainfo;
1164 ifi = rai->rai_ifinfo;
1165 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next);
1166 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
1168 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1169 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1171 rtadvd_remove_timer(pfx->pfx_timer);
1178 invalidate_prefix(struct prefix *pfx)
1180 struct timespec timo;
1183 char ntopbuf[INET6_ADDRSTRLEN];
1185 rai = pfx->pfx_rainfo;
1186 ifi = rai->rai_ifinfo;
1187 if (pfx->pfx_timer) { /* sanity check */
1189 "<%s> assumption failure: timer already exists",
1194 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
1195 "will expire in %ld seconds", __func__,
1196 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)),
1197 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo);
1199 /* set the expiration timer */
1200 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL);
1201 if (pfx->pfx_timer == NULL) {
1202 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
1203 "remove the prefix", __func__);
1206 timo.tv_sec = prefix_timo;
1208 rtadvd_set_timer(&timo, pfx->pfx_timer);
1211 static struct rtadvd_timer *
1212 prefix_timeout(void *arg)
1215 delete_prefix((struct prefix *)arg);
1221 update_prefix(struct prefix *pfx)
1225 char ntopbuf[INET6_ADDRSTRLEN];
1227 rai = pfx->pfx_rainfo;
1228 ifi = rai->rai_ifinfo;
1229 if (pfx->pfx_timer == NULL) { /* sanity check */
1231 "<%s> assumption failure: timer does not exist",
1236 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1237 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1238 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1240 /* stop the expiration timer */
1241 rtadvd_remove_timer(pfx->pfx_timer);
1242 pfx->pfx_timer = NULL;
1246 * Try to get an in6_prefixreq contents for a prefix which matches
1247 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1248 * the interface whose name is ipr->ipr_name[].
1251 init_prefix(struct in6_prefixreq *ipr)
1256 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1257 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1262 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
1263 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
1266 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1267 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1268 ipr->ipr_raf_onlink = 1;
1269 ipr->ipr_raf_auto = 1;
1270 /* omit other field initialization */
1272 else if (ipr->ipr_origin < PR_ORIG_RR) {
1273 char ntopbuf[INET6_ADDRSTRLEN];
1275 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1276 "lower than PR_ORIG_RR(router renumbering)."
1277 "This should not happen if I am router", __func__,
1278 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1279 sizeof(ntopbuf)), ipr->ipr_origin);
1287 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1288 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1289 ipr->ipr_raf_onlink = 1;
1290 ipr->ipr_raf_auto = 1;
1296 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1298 struct in6_prefixreq ipr;
1300 memset(&ipr, 0, sizeof(ipr));
1301 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1302 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't "
1303 "exist. This should not happen! %s", __func__,
1304 ifindex, strerror(errno));
1307 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1308 ipr.ipr_prefix.sin6_family = AF_INET6;
1309 ipr.ipr_prefix.sin6_addr = *addr;
1310 ipr.ipr_plen = plen;
1312 if (init_prefix(&ipr))
1313 return; /* init failed by some error */
1314 add_prefix(rai, &ipr);
1318 make_packet(struct rainfo *rai)
1320 size_t packlen, lladdroptlen = 0;
1322 struct nd_router_advert *ra;
1323 struct nd_opt_prefix_info *ndopt_pi;
1324 struct nd_opt_mtu *ndopt_mtu;
1325 struct nd_opt_route_info *ndopt_rti;
1327 struct nd_opt_rdnss *ndopt_rdnss;
1329 struct nd_opt_dnssl *ndopt_dnssl;
1335 ifi = rai->rai_ifinfo;
1336 /* calculate total length */
1337 packlen = sizeof(struct nd_router_advert);
1338 if (rai->rai_advlinkopt) {
1339 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) {
1341 "<%s> link-layer address option has"
1342 " null length on %s. Treat as not included.",
1343 __func__, ifi->ifi_ifname);
1344 rai->rai_advlinkopt = 0;
1346 packlen += lladdroptlen;
1349 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs;
1350 if (rai->rai_linkmtu)
1351 packlen += sizeof(struct nd_opt_mtu);
1353 TAILQ_FOREACH(rti, &rai->rai_route, rti_next)
1354 packlen += sizeof(struct nd_opt_route_info) +
1355 ((rti->rti_prefixlen + 0x3f) >> 6) * 8;
1357 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1358 struct rdnss_addr *rdna;
1360 packlen += sizeof(struct nd_opt_rdnss);
1361 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next)
1362 packlen += sizeof(rdna->ra_dns);
1364 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1365 struct dnssl_addr *dnsa;
1367 packlen += sizeof(struct nd_opt_dnssl);
1369 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next)
1370 len += dnsa->da_len;
1372 /* A zero octet and 8 octet boundary */
1374 len += (len % 8) ? 8 - len % 8 : 0;
1378 /* allocate memory for the packet */
1379 if ((buf = malloc(packlen)) == NULL) {
1381 "<%s> can't get enough memory for an RA packet",
1385 memset(buf, 0, packlen);
1386 if (rai->rai_ra_data) /* Free old data if any. */
1387 free(rai->rai_ra_data);
1388 rai->rai_ra_data = buf;
1389 /* XXX: what if packlen > 576? */
1390 rai->rai_ra_datalen = packlen;
1393 * construct the packet
1395 ra = (struct nd_router_advert *)buf;
1396 ra->nd_ra_type = ND_ROUTER_ADVERT;
1398 ra->nd_ra_cksum = 0;
1399 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit);
1401 * XXX: the router preference field, which is a 2-bit field, should be
1402 * initialized before other fields.
1404 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref;
1405 ra->nd_ra_flags_reserved |=
1406 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0;
1407 ra->nd_ra_flags_reserved |=
1408 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0;
1409 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime);
1410 ra->nd_ra_reachable = htonl(rai->rai_reachabletime);
1411 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer);
1414 if (rai->rai_advlinkopt) {
1415 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf);
1416 buf += lladdroptlen;
1419 if (rai->rai_linkmtu) {
1420 ndopt_mtu = (struct nd_opt_mtu *)buf;
1421 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1422 ndopt_mtu->nd_opt_mtu_len = 1;
1423 ndopt_mtu->nd_opt_mtu_reserved = 0;
1424 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu);
1425 buf += sizeof(struct nd_opt_mtu);
1428 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1429 uint32_t vltime, pltime;
1430 struct timespec now;
1432 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1433 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1434 ndopt_pi->nd_opt_pi_len = 4;
1435 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen;
1436 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1437 if (pfx->pfx_onlinkflg)
1438 ndopt_pi->nd_opt_pi_flags_reserved |=
1439 ND_OPT_PI_FLAG_ONLINK;
1440 if (pfx->pfx_autoconfflg)
1441 ndopt_pi->nd_opt_pi_flags_reserved |=
1442 ND_OPT_PI_FLAG_AUTO;
1446 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire)
1447 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1448 if (pfx->pfx_vltimeexpire == 0)
1449 vltime = pfx->pfx_validlifetime;
1451 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ?
1452 pfx->pfx_vltimeexpire - now.tv_sec : 0;
1457 if (pfx->pfx_pltimeexpire == 0)
1458 pltime = pfx->pfx_preflifetime;
1460 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ?
1461 pfx->pfx_pltimeexpire - now.tv_sec : 0;
1463 if (vltime < pltime) {
1465 * this can happen if vltime is decrement but pltime
1470 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1471 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1472 ndopt_pi->nd_opt_pi_reserved2 = 0;
1473 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix;
1475 buf += sizeof(struct nd_opt_prefix_info);
1478 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) {
1479 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6;
1481 ndopt_rti = (struct nd_opt_route_info *)buf;
1482 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1483 ndopt_rti->nd_opt_rti_len = 1 + psize;
1484 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen;
1485 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref;
1486 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime);
1487 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8);
1488 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1491 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1492 struct rdnss_addr *rdna;
1494 ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1495 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1496 ndopt_rdnss->nd_opt_rdnss_len = 0;
1497 ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1498 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime);
1499 buf += sizeof(struct nd_opt_rdnss);
1501 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) {
1502 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns));
1503 buf += sizeof(rdna->ra_dns);
1505 /* Length field should be in 8 octets */
1506 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8;
1508 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__,
1509 ndopt_rdnss->nd_opt_rdnss_len);
1512 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1513 struct dnssl_addr *dnsa;
1515 ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1516 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1517 ndopt_dnssl->nd_opt_dnssl_len = 0;
1518 ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1519 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime);
1520 buf += sizeof(*ndopt_dnssl);
1522 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) {
1523 memcpy(buf, dnsa->da_dom, dnsa->da_len);
1524 buf += dnsa->da_len;
1527 /* A zero octet after encoded DNS server list. */
1530 /* Padding to next 8 octets boundary */
1531 len = buf - (char *)ndopt_dnssl;
1532 len += (len % 8) ? 8 - len % 8 : 0;
1533 buf = (char *)ndopt_dnssl + len;
1535 /* Length field must be in 8 octets */
1536 ndopt_dnssl->nd_opt_dnssl_len = len / 8;
1538 syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__,
1539 ndopt_dnssl->nd_opt_dnssl_len);