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;
440 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
441 if (strchr(flagstr, 'S'))
442 val |= ND_RA_FLAG_IPV6_ONLY;
445 MAYHAVE(val, "raflags", 0);
447 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED;
448 rai->rai_otherflg = val & ND_RA_FLAG_OTHER;
449 #ifndef ND_RA_FLAG_RTPREF_MASK
450 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
451 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
453 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
454 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) {
455 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
456 __func__, rai->rai_rtpref, ifi->ifi_ifname);
457 goto getconfig_free_rai;
459 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
460 rai->rai_ipv6onlyflg = val & ND_RA_FLAG_IPV6_ONLY;
463 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3);
464 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval ||
465 (uint16_t)val > MAXROUTERLIFETIME)) {
467 "<%s> router lifetime (%" PRIu32 ") on %s is invalid "
468 "(must be 0 or between %d and %d)",
469 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval,
471 goto getconfig_free_rai;
473 rai->rai_lifetime = val & 0xffff;
475 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
476 if (val < 0 || val > MAXREACHABLETIME) {
478 "<%s> reachable time (%" PRIu32 ") on %s is invalid "
479 "(must be no greater than %d)",
480 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME);
481 goto getconfig_free_rai;
483 rai->rai_reachabletime = (uint32_t)val;
485 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
486 if (val64 < 0 || val64 > 0xffffffff) {
487 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range",
488 __func__, val64, ifi->ifi_ifname);
489 goto getconfig_free_rai;
491 rai->rai_retranstimer = (uint32_t)val64;
493 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
495 "<%s> mobile-ip6 configuration not supported",
497 goto getconfig_free_rai;
499 /* prefix information */
502 * This is an implementation specific parameter to consider
503 * link propagation delays and poorly synchronized clocks when
504 * checking consistency of advertised lifetimes.
506 MAYHAVE(val, "clockskew", 0);
507 rai->rai_clockskew = val;
510 for (i = -1; i < MAXPREFIX; i++) {
513 makeentry(entbuf, sizeof(entbuf), i, "addr");
514 addr = (char *)agetstr(entbuf, &bp);
518 /* allocate memory to store prefix information */
519 ELM_MALLOC(pfx, exit(1));
520 pfx->pfx_rainfo = rai;
521 pfx->pfx_origin = PREFIX_FROM_CONFIG;
523 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) {
525 "<%s> inet_pton failed for %s",
527 goto getconfig_free_pfx;
529 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) {
531 "<%s> multicast prefix (%s) must "
532 "not be advertised on %s",
533 __func__, addr, ifi->ifi_ifname);
534 goto getconfig_free_pfx;
536 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix))
538 "<%s> link-local prefix (%s) will be"
540 __func__, addr, ifi->ifi_ifname);
542 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
543 MAYHAVE(val, entbuf, 64);
544 if (val < 0 || val > 128) {
545 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s "
546 "on %s out of range",
547 __func__, val, addr, ifi->ifi_ifname);
548 goto getconfig_free_pfx;
550 pfx->pfx_prefixlen = (int)val;
552 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
553 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
555 if (strchr(flagstr, 'l'))
556 val |= ND_OPT_PI_FLAG_ONLINK;
557 if (strchr(flagstr, 'a'))
558 val |= ND_OPT_PI_FLAG_AUTO;
561 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
563 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
564 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
566 makeentry(entbuf, sizeof(entbuf), i, "vltime");
567 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
568 if (val64 < 0 || val64 > 0xffffffff) {
569 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for "
570 "%s/%d on %s is out of range",
572 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
573 goto getconfig_free_pfx;
575 pfx->pfx_validlifetime = (uint32_t)val64;
577 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
578 if (agetflag(entbuf)) {
581 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
582 pfx->pfx_vltimeexpire =
583 now.tv_sec + pfx->pfx_validlifetime;
586 makeentry(entbuf, sizeof(entbuf), i, "pltime");
587 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
588 if (val64 < 0 || val64 > 0xffffffff) {
590 "<%s> pltime (%" PRIu64 ") for %s/%d on %s "
593 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
594 goto getconfig_free_pfx;
596 pfx->pfx_preflifetime = (uint32_t)val64;
598 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
599 if (agetflag(entbuf)) {
602 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
603 pfx->pfx_pltimeexpire =
604 now.tv_sec + pfx->pfx_preflifetime;
606 /* link into chain */
607 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
613 if (rai->rai_advifprefix && rai->rai_pfxs == 0)
616 MAYHAVE(val64, "mtu", 0);
617 if (val < 0 || val64 > 0xffffffff) {
619 "<%s> mtu (%" PRIu64 ") on %s out of range",
620 __func__, val64, ifi->ifi_ifname);
621 goto getconfig_free_rai;
623 rai->rai_linkmtu = (uint32_t)val64;
624 if (rai->rai_linkmtu == 0) {
627 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
628 strcmp(mtustr, "auto") == 0)
629 rai->rai_linkmtu = ifi->ifi_phymtu;
631 else if (rai->rai_linkmtu < IPV6_MMTU ||
632 rai->rai_linkmtu > ifi->ifi_phymtu) {
634 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must "
635 "be between least MTU (%d) and physical link MTU (%d)",
636 __func__, rai->rai_linkmtu, ifi->ifi_ifname,
637 IPV6_MMTU, ifi->ifi_phymtu);
638 goto getconfig_free_rai;
641 #ifdef SIOCSIFINFO_IN6
643 struct in6_ndireq ndi;
646 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
647 syslog(LOG_ERR, "<%s> socket: %s", __func__,
651 memset(&ndi, 0, sizeof(ndi));
652 strlcpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname));
653 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0)
654 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
655 __func__, ifi->ifi_ifname, strerror(errno));
657 /* reflect the RA info to the host variables in kernel */
658 ndi.ndi.chlim = rai->rai_hoplimit;
659 ndi.ndi.retrans = rai->rai_retranstimer;
660 ndi.ndi.basereachable = rai->rai_reachabletime;
661 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0)
662 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
663 __func__, ifi->ifi_ifname, strerror(errno));
669 /* route information */
671 for (i = -1; i < MAXROUTE; i++) {
674 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
675 addr = (char *)agetstr(entbuf, &bp);
677 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
678 addr = (char *)agetstr(oentbuf, &bp);
680 fprintf(stderr, "%s was obsoleted. Use %s.\n",
686 /* allocate memory to store prefix information */
687 ELM_MALLOC(rti, exit(1));
689 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) {
690 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
692 goto getconfig_free_rti;
696 * XXX: currently there's no restriction in route information
697 * prefix according to
698 * draft-ietf-ipngwg-router-selection-00.txt.
699 * However, I think the similar restriction be necessary.
701 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
702 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
704 "<%s> multicast route (%s) must "
705 "not be advertised on %s",
706 __func__, addr, ifi->ifi_ifname);
707 goto getconfig_free_rti;
709 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
711 "<%s> link-local route (%s) will "
712 "be advertised on %s",
713 __func__, addr, ifi->ifi_ifname);
714 goto getconfig_free_rti;
718 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
719 /* XXX: 256 is a magic number for compatibility check. */
720 MAYHAVE(val, entbuf, 256);
722 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
723 MAYHAVE(val, oentbuf, 256);
725 fprintf(stderr, "%s was obsoleted. Use %s.\n",
730 if (val < 0 || val > 128) {
731 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s "
733 __func__, val, addr, ifi->ifi_ifname);
734 goto getconfig_free_rti;
736 rti->rti_prefixlen = (int)val;
738 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
739 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
741 if (strchr(flagstr, 'h'))
742 val |= ND_RA_FLAG_RTPREF_HIGH;
743 if (strchr(flagstr, 'l')) {
744 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
746 "<%s> the \'h\' and \'l\' route"
747 " preferences are exclusive",
749 goto getconfig_free_rti;
751 val |= ND_RA_FLAG_RTPREF_LOW;
754 MAYHAVE(val, entbuf, 256); /* XXX */
756 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
757 MAYHAVE(val, oentbuf, 256);
759 fprintf(stderr, "%s was obsoleted. Use %s.\n",
764 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
765 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) {
766 syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
768 __func__, rti->rti_rtpref, addr,
769 rti->rti_prefixlen, ifi->ifi_ifname);
770 goto getconfig_free_rti;
774 * Since the spec does not a default value, we should make
775 * this entry mandatory. However, FreeBSD 4.4 has shipped
776 * with this field being optional, we use the router lifetime
777 * as an ad-hoc default value with a warning message.
779 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
780 MAYHAVE(val64, entbuf, -1);
782 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
783 MAYHAVE(val64, oentbuf, -1);
785 fprintf(stderr, "%s was obsoleted. Use %s.\n",
788 fprintf(stderr, "%s should be specified "
789 "for interface %s.\n", entbuf,
791 val64 = rai->rai_lifetime;
794 if (val64 < 0 || val64 > 0xffffffff) {
795 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for "
796 "%s/%d on %s out of range", __func__,
797 val64, addr, rti->rti_prefixlen,
799 goto getconfig_free_rti;
801 rti->rti_ltime = (uint32_t)val64;
803 /* link into chain */
804 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next);
811 /* DNS server and DNS search list information */
812 for (i = -1; i < MAXRDNSSENT ; i++) {
814 struct rdnss_addr *rdna;
818 makeentry(entbuf, sizeof(entbuf), i, "rdnss");
819 addr = (char *)agetstr(entbuf, &bp);
822 ELM_MALLOC(rdn, exit(1));
824 TAILQ_INIT(&rdn->rd_list);
826 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
827 c = strcspn(ap, ",");
828 strncpy(abuf, ap, c);
830 ELM_MALLOC(rdna, goto getconfig_free_rdn);
831 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) {
832 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
835 goto getconfig_free_rdn;
837 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next);
840 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
841 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
842 if ((uint16_t)val < rai->rai_maxinterval ||
843 (uint16_t)val > rai->rai_maxinterval * 2) {
844 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
845 "(must be between %d and %d)",
846 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
847 rai->rai_maxinterval * 2);
848 goto getconfig_free_rdn;
852 /* link into chain */
853 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next);
856 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
857 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
863 for (i = -1; i < MAXDNSSLENT ; i++) {
865 struct dnssl_addr *dnsa;
869 makeentry(entbuf, sizeof(entbuf), i, "dnssl");
870 addr = (char *)agetstr(entbuf, &bp);
874 ELM_MALLOC(dns, exit(1));
876 TAILQ_INIT(&dns->dn_list);
878 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
879 c = strcspn(ap, ",");
880 strncpy(abuf, ap, c);
882 ELM_MALLOC(dnsa, goto getconfig_free_dns);
883 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf);
884 if (dnsa->da_len < 0) {
885 syslog(LOG_ERR, "Invalid dnssl entry: %s",
887 goto getconfig_free_dns;
889 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__,
891 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next);
894 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
895 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
896 if ((uint16_t)val < rai->rai_maxinterval ||
897 (uint16_t)val > rai->rai_maxinterval * 2) {
898 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
899 "(must be between %d and %d)",
900 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
901 rai->rai_maxinterval * 2);
902 goto getconfig_free_dns;
906 /* link into chain */
907 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next);
910 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) {
911 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next);
916 /* construct the sending packet */
920 * If an entry with the same ifindex exists, remove it first.
921 * Before the removal, RDNSS and DNSSL options with
922 * zero-lifetime will be sent.
924 switch (ifi->ifi_state) {
925 case IFI_STATE_UNCONFIGURED:
926 /* UNCONFIGURED -> TRANSITIVE */
928 error = sock_mc_join(&sock, ifi->ifi_ifindex);
932 ifi->ifi_state = IFI_STATE_TRANSITIVE;
933 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS;
934 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
936 /* The same two rai mean initial burst */
937 ifi->ifi_rainfo = rai;
938 ifi->ifi_rainfo_trans = rai;
939 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
941 if (ifi->ifi_ra_timer == NULL)
942 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
943 ra_timer_update, ifi, ifi);
944 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
945 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
949 "<%s> ifname=%s marked as TRANSITIVE (initial burst).",
950 __func__, ifi->ifi_ifname);
952 case IFI_STATE_CONFIGURED:
953 /* CONFIGURED -> TRANSITIVE */
954 rai_old = ifi->ifi_rainfo;
955 if (rai_old == NULL) {
957 "<%s> ifi_rainfo is NULL"
958 " in IFI_STATE_CONFIGURED.", __func__);
965 rai_old->rai_lifetime = 0;
966 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next)
968 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next)
971 ifi->ifi_rainfo_trans = rai_old;
972 ifi->ifi_state = IFI_STATE_TRANSITIVE;
973 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
974 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
976 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
977 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
981 "<%s> ifname=%s marked as TRANSITIVE"
982 " (transitional burst)",
983 __func__, ifi->ifi_ifname);
985 ifi->ifi_rainfo = rai;
986 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
988 case IFI_STATE_TRANSITIVE:
989 if (ifi->ifi_rainfo != NULL) {
990 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
991 /* Reinitialize initial burst */
992 rm_rainfo(ifi->ifi_rainfo);
993 ifi->ifi_rainfo = rai;
994 ifi->ifi_rainfo_trans = rai;
995 ifi->ifi_burstcount =
996 MAX_INITIAL_RTR_ADVERTISEMENTS;
997 ifi->ifi_burstinterval =
998 MAX_INITIAL_RTR_ADVERT_INTERVAL;
1000 /* Replace ifi_rainfo with the new one */
1001 rm_rainfo(ifi->ifi_rainfo);
1002 ifi->ifi_rainfo = rai;
1004 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
1006 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
1007 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
1010 /* XXX: NOTREACHED. Being shut down. */
1012 "<%s> %s is shutting down. Skipped.",
1013 __func__, ifi->ifi_ifname);
1029 get_prefix(struct rainfo *rai)
1031 struct ifaddrs *ifap, *ifa;
1035 char *p, *ep, *m, *lim;
1036 char ntopbuf[INET6_ADDRSTRLEN];
1038 if (getifaddrs(&ifap) < 0) {
1040 "<%s> can't get interface addresses",
1044 ifi = rai->rai_ifinfo;
1046 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1049 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0)
1051 if (ifa->ifa_addr->sa_family != AF_INET6)
1053 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1054 if (IN6_IS_ADDR_LINKLOCAL(a))
1057 /* get prefix length */
1058 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1059 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
1060 plen = prefixlen(m, lim);
1061 if (plen <= 0 || plen > 128) {
1062 syslog(LOG_ERR, "<%s> failed to get prefixlen "
1063 "or prefix is invalid",
1067 if (plen == 128) /* XXX */
1069 if (find_prefix(rai, a, plen)) {
1070 /* ignore a duplicated prefix. */
1074 /* allocate memory to store prefix info. */
1075 ELM_MALLOC(pfx, exit(1));
1077 /* set prefix, sweep bits outside of prefixlen */
1078 pfx->pfx_prefixlen = plen;
1079 memcpy(&pfx->pfx_prefix, a, sizeof(*a));
1080 p = (char *)&pfx->pfx_prefix;
1081 ep = (char *)(&pfx->pfx_prefix + 1);
1082 while (m < lim && p < ep)
1086 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1088 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
1092 "<%s> add %s/%d to prefix list on %s",
1093 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname);
1095 /* set other fields with protocol defaults */
1096 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME;
1097 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME;
1098 pfx->pfx_onlinkflg = 1;
1099 pfx->pfx_autoconfflg = 1;
1100 pfx->pfx_origin = PREFIX_FROM_KERNEL;
1101 pfx->pfx_rainfo = rai;
1103 /* link into chain */
1104 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1106 /* counter increment */
1114 makeentry(char *buf, size_t len, int id, const char *string)
1118 strlcpy(buf, string, len);
1120 snprintf(buf, len, "%s%d", string, id);
1124 * Add a prefix to the list of specified interface and reconstruct
1125 * the outgoing packet.
1126 * The prefix must not be in the list.
1127 * XXX: other parameters of the prefix (e.g. lifetime) should be
1128 * able to be specified.
1131 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
1135 char ntopbuf[INET6_ADDRSTRLEN];
1137 ifi = rai->rai_ifinfo;
1138 ELM_MALLOC(pfx, return);
1139 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr;
1140 pfx->pfx_prefixlen = ipr->ipr_plen;
1141 pfx->pfx_validlifetime = ipr->ipr_vltime;
1142 pfx->pfx_preflifetime = ipr->ipr_pltime;
1143 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink;
1144 pfx->pfx_autoconfflg = ipr->ipr_raf_auto;
1145 pfx->pfx_origin = PREFIX_FROM_DYNAMIC;
1146 pfx->pfx_rainfo = rai;
1148 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1150 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
1152 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1153 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname);
1159 * Delete a prefix to the list of specified interface and reconstruct
1160 * the outgoing packet.
1161 * The prefix must be in the list.
1164 delete_prefix(struct prefix *pfx)
1168 char ntopbuf[INET6_ADDRSTRLEN];
1170 rai = pfx->pfx_rainfo;
1171 ifi = rai->rai_ifinfo;
1172 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next);
1173 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
1175 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1176 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1178 rtadvd_remove_timer(pfx->pfx_timer);
1185 invalidate_prefix(struct prefix *pfx)
1187 struct timespec timo;
1190 char ntopbuf[INET6_ADDRSTRLEN];
1192 rai = pfx->pfx_rainfo;
1193 ifi = rai->rai_ifinfo;
1194 if (pfx->pfx_timer) { /* sanity check */
1196 "<%s> assumption failure: timer already exists",
1201 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
1202 "will expire in %ld seconds", __func__,
1203 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)),
1204 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo);
1206 /* set the expiration timer */
1207 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL);
1208 if (pfx->pfx_timer == NULL) {
1209 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
1210 "remove the prefix", __func__);
1213 timo.tv_sec = prefix_timo;
1215 rtadvd_set_timer(&timo, pfx->pfx_timer);
1218 static struct rtadvd_timer *
1219 prefix_timeout(void *arg)
1222 delete_prefix((struct prefix *)arg);
1228 update_prefix(struct prefix *pfx)
1232 char ntopbuf[INET6_ADDRSTRLEN];
1234 rai = pfx->pfx_rainfo;
1235 ifi = rai->rai_ifinfo;
1236 if (pfx->pfx_timer == NULL) { /* sanity check */
1238 "<%s> assumption failure: timer does not exist",
1243 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1244 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1245 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1247 /* stop the expiration timer */
1248 rtadvd_remove_timer(pfx->pfx_timer);
1249 pfx->pfx_timer = NULL;
1253 * Try to get an in6_prefixreq contents for a prefix which matches
1254 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1255 * the interface whose name is ipr->ipr_name[].
1258 init_prefix(struct in6_prefixreq *ipr)
1263 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1264 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1269 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
1270 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
1273 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1274 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1275 ipr->ipr_raf_onlink = 1;
1276 ipr->ipr_raf_auto = 1;
1277 /* omit other field initialization */
1279 else if (ipr->ipr_origin < PR_ORIG_RR) {
1280 char ntopbuf[INET6_ADDRSTRLEN];
1282 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1283 "lower than PR_ORIG_RR(router renumbering)."
1284 "This should not happen if I am router", __func__,
1285 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1286 sizeof(ntopbuf)), ipr->ipr_origin);
1294 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1295 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1296 ipr->ipr_raf_onlink = 1;
1297 ipr->ipr_raf_auto = 1;
1303 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1305 struct in6_prefixreq ipr;
1307 memset(&ipr, 0, sizeof(ipr));
1308 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1309 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't "
1310 "exist. This should not happen! %s", __func__,
1311 ifindex, strerror(errno));
1314 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1315 ipr.ipr_prefix.sin6_family = AF_INET6;
1316 ipr.ipr_prefix.sin6_addr = *addr;
1317 ipr.ipr_plen = plen;
1319 if (init_prefix(&ipr))
1320 return; /* init failed by some error */
1321 add_prefix(rai, &ipr);
1325 make_packet(struct rainfo *rai)
1327 size_t packlen, lladdroptlen = 0;
1329 struct nd_router_advert *ra;
1330 struct nd_opt_prefix_info *ndopt_pi;
1331 struct nd_opt_mtu *ndopt_mtu;
1332 struct nd_opt_route_info *ndopt_rti;
1334 struct nd_opt_rdnss *ndopt_rdnss;
1336 struct nd_opt_dnssl *ndopt_dnssl;
1342 ifi = rai->rai_ifinfo;
1343 /* calculate total length */
1344 packlen = sizeof(struct nd_router_advert);
1345 if (rai->rai_advlinkopt) {
1346 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) {
1348 "<%s> link-layer address option has"
1349 " null length on %s. Treat as not included.",
1350 __func__, ifi->ifi_ifname);
1351 rai->rai_advlinkopt = 0;
1353 packlen += lladdroptlen;
1356 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs;
1357 if (rai->rai_linkmtu)
1358 packlen += sizeof(struct nd_opt_mtu);
1360 TAILQ_FOREACH(rti, &rai->rai_route, rti_next)
1361 packlen += sizeof(struct nd_opt_route_info) +
1362 ((rti->rti_prefixlen + 0x3f) >> 6) * 8;
1364 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1365 struct rdnss_addr *rdna;
1367 packlen += sizeof(struct nd_opt_rdnss);
1368 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next)
1369 packlen += sizeof(rdna->ra_dns);
1371 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1372 struct dnssl_addr *dnsa;
1374 packlen += sizeof(struct nd_opt_dnssl);
1376 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next)
1377 len += dnsa->da_len;
1379 /* A zero octet and 8 octet boundary */
1381 len += (len % 8) ? 8 - len % 8 : 0;
1385 /* allocate memory for the packet */
1386 if ((buf = malloc(packlen)) == NULL) {
1388 "<%s> can't get enough memory for an RA packet",
1392 memset(buf, 0, packlen);
1393 if (rai->rai_ra_data) /* Free old data if any. */
1394 free(rai->rai_ra_data);
1395 rai->rai_ra_data = buf;
1396 /* XXX: what if packlen > 576? */
1397 rai->rai_ra_datalen = packlen;
1400 * construct the packet
1402 ra = (struct nd_router_advert *)buf;
1403 ra->nd_ra_type = ND_ROUTER_ADVERT;
1405 ra->nd_ra_cksum = 0;
1406 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit);
1408 * XXX: the router preference field, which is a 2-bit field, should be
1409 * initialized before other fields.
1411 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref;
1412 ra->nd_ra_flags_reserved |=
1413 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0;
1414 ra->nd_ra_flags_reserved |=
1415 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0;
1416 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
1417 ra->nd_ra_flags_reserved |=
1418 rai->rai_ipv6onlyflg ? ND_RA_FLAG_IPV6_ONLY : 0;
1420 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime);
1421 ra->nd_ra_reachable = htonl(rai->rai_reachabletime);
1422 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer);
1425 if (rai->rai_advlinkopt) {
1426 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf);
1427 buf += lladdroptlen;
1430 if (rai->rai_linkmtu) {
1431 ndopt_mtu = (struct nd_opt_mtu *)buf;
1432 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1433 ndopt_mtu->nd_opt_mtu_len = 1;
1434 ndopt_mtu->nd_opt_mtu_reserved = 0;
1435 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu);
1436 buf += sizeof(struct nd_opt_mtu);
1439 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1440 uint32_t vltime, pltime;
1441 struct timespec now;
1443 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1444 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1445 ndopt_pi->nd_opt_pi_len = 4;
1446 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen;
1447 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1448 if (pfx->pfx_onlinkflg)
1449 ndopt_pi->nd_opt_pi_flags_reserved |=
1450 ND_OPT_PI_FLAG_ONLINK;
1451 if (pfx->pfx_autoconfflg)
1452 ndopt_pi->nd_opt_pi_flags_reserved |=
1453 ND_OPT_PI_FLAG_AUTO;
1457 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire)
1458 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1459 if (pfx->pfx_vltimeexpire == 0)
1460 vltime = pfx->pfx_validlifetime;
1462 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ?
1463 pfx->pfx_vltimeexpire - now.tv_sec : 0;
1468 if (pfx->pfx_pltimeexpire == 0)
1469 pltime = pfx->pfx_preflifetime;
1471 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ?
1472 pfx->pfx_pltimeexpire - now.tv_sec : 0;
1474 if (vltime < pltime) {
1476 * this can happen if vltime is decrement but pltime
1481 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1482 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1483 ndopt_pi->nd_opt_pi_reserved2 = 0;
1484 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix;
1486 buf += sizeof(struct nd_opt_prefix_info);
1489 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) {
1490 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6;
1492 ndopt_rti = (struct nd_opt_route_info *)buf;
1493 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1494 ndopt_rti->nd_opt_rti_len = 1 + psize;
1495 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen;
1496 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref;
1497 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime);
1498 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8);
1499 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1502 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1503 struct rdnss_addr *rdna;
1505 ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1506 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1507 ndopt_rdnss->nd_opt_rdnss_len = 0;
1508 ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1509 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime);
1510 buf += sizeof(struct nd_opt_rdnss);
1512 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) {
1513 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns));
1514 buf += sizeof(rdna->ra_dns);
1516 /* Length field should be in 8 octets */
1517 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8;
1519 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__,
1520 ndopt_rdnss->nd_opt_rdnss_len);
1523 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1524 struct dnssl_addr *dnsa;
1526 ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1527 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1528 ndopt_dnssl->nd_opt_dnssl_len = 0;
1529 ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1530 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime);
1531 buf += sizeof(*ndopt_dnssl);
1533 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) {
1534 memcpy(buf, dnsa->da_dom, dnsa->da_len);
1535 buf += dnsa->da_len;
1538 /* A zero octet after encoded DNS server list. */
1541 /* Padding to next 8 octets boundary */
1542 len = buf - (char *)ndopt_dnssl;
1543 len += (len % 8) ? 8 - len % 8 : 0;
1544 buf = (char *)ndopt_dnssl + len;
1546 /* Length field must be in 8 octets */
1547 ndopt_dnssl->nd_opt_dnssl_len = len / 8;
1549 syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__,
1550 ndopt_dnssl->nd_opt_dnssl_len);