2 /* $KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $ */
5 * Copyright (C) 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
37 #include <sys/sysctl.h>
40 #include <net/if_var.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_var.h>
46 #include <netinet/ip6.h>
47 #include <netinet6/ip6_var.h>
48 #include <netinet/icmp6.h>
50 #include <arpa/inet.h>
67 static time_t prefix_timo = (60 * 120); /* 2 hours.
68 * XXX: should be configurable. */
69 extern struct rainfo *ralist;
71 static struct rtadvd_timer *prefix_timeout __P((void *));
72 static void makeentry __P((char *, size_t, int, char *));
73 static int getinet6sysctl __P((int));
87 static int forwarding = -1;
89 #define MUSTHAVE(var, cap) \
92 if ((t = agetnum(cap)) < 0) { \
93 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
99 #define MAYHAVE(var, cap, def) \
101 if ((var = agetnum(cap)) < 0) \
105 if ((stat = agetent(tbuf, intface)) <= 0) {
106 memset(tbuf, 0, sizeof(tbuf));
108 "<%s> %s isn't defined in the configuration file"
109 " or the configuration file doesn't exist."
110 " Treat it as default",
114 tmp = (struct rainfo *)malloc(sizeof(*ralist));
116 syslog(LOG_INFO, "<%s> %s: can't allocate enough memory",
120 memset(tmp, 0, sizeof(*tmp));
121 tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
123 tmp->route.next = tmp->route.prev = &tmp->route;
126 /* check if we are allowed to forward packets (if not determined) */
127 if (forwarding < 0) {
128 if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
132 /* get interface information */
133 if (agetflag("nolladdr"))
137 if (tmp->advlinkopt) {
138 if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
140 "<%s> can't get information of %s",
144 tmp->ifindex = tmp->sdl->sdl_index;
146 tmp->ifindex = if_nametoindex(intface);
147 strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
148 if ((tmp->phymtu = if_getmtu(intface)) == 0) {
149 tmp->phymtu = IPV6_MMTU;
151 "<%s> can't get interface mtu of %s. Treat as %d",
152 __func__, intface, IPV6_MMTU);
156 * set router configuration variables.
158 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
159 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
161 "<%s> maxinterval (%ld) on %s is invalid "
162 "(must be between %u and %u)", __func__, val,
163 intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
166 tmp->maxinterval = (u_int)val;
167 MAYHAVE(val, "mininterval", tmp->maxinterval/3);
168 if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
170 "<%s> mininterval (%ld) on %s is invalid "
171 "(must be between %d and %d)",
172 __func__, val, intface, MIN_MININTERVAL,
173 (tmp->maxinterval * 3) / 4);
176 tmp->mininterval = (u_int)val;
178 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
179 tmp->hoplimit = val & 0xff;
181 if ((flagstr = (char *)agetstr("raflags", &bp))) {
183 if (strchr(flagstr, 'm'))
184 val |= ND_RA_FLAG_MANAGED;
185 if (strchr(flagstr, 'o'))
186 val |= ND_RA_FLAG_OTHER;
187 if (strchr(flagstr, 'h'))
188 val |= ND_RA_FLAG_RTPREF_HIGH;
189 if (strchr(flagstr, 'l')) {
190 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
191 syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
192 " router flags are exclusive", __func__);
195 val |= ND_RA_FLAG_RTPREF_LOW;
198 MAYHAVE(val, "raflags", 0);
200 tmp->managedflg = val & ND_RA_FLAG_MANAGED;
201 tmp->otherflg = val & ND_RA_FLAG_OTHER;
202 #ifndef ND_RA_FLAG_RTPREF_MASK
203 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
204 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
206 tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
207 if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
208 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
209 __func__, tmp->rtpref, intface);
213 MAYHAVE(val, "rltime", tmp->maxinterval * 3);
214 if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
216 "<%s> router lifetime (%ld) on %s is invalid "
217 "(must be 0 or between %d and %d)",
218 __func__, val, intface,
224 * Basically, hosts MUST NOT send Router Advertisement messages at any
225 * time (RFC 2461, Section 6.2.3). However, it would sometimes be
226 * useful to allow hosts to advertise some parameters such as prefix
227 * information and link MTU. Thus, we allow hosts to invoke rtadvd
228 * only when router lifetime (on every advertising interface) is
229 * explicitly set zero. (see also the above section)
231 if (val && forwarding == 0) {
233 "<%s> non zero router lifetime is specified for %s, "
234 "which must not be allowed for hosts. you must "
235 "change router lifetime or enable IPv6 forwarding.",
239 tmp->lifetime = val & 0xffff;
241 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
242 if (val < 0 || val > MAXREACHABLETIME) {
244 "<%s> reachable time (%ld) on %s is invalid "
245 "(must be no greater than %d)",
246 __func__, val, intface, MAXREACHABLETIME);
249 tmp->reachabletime = (u_int32_t)val;
251 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
252 if (val64 < 0 || val64 > 0xffffffff) {
253 syslog(LOG_ERR, "<%s> retrans time (%lld) on %s out of range",
254 __func__, (long long)val64, intface);
257 tmp->retranstimer = (u_int32_t)val64;
259 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
261 "<%s> mobile-ip6 configuration not supported",
265 /* prefix information */
268 * This is an implementation specific parameter to consider
269 * link propagation delays and poorly synchronized clocks when
270 * checking consistency of advertised lifetimes.
272 MAYHAVE(val, "clockskew", 0);
273 tmp->clockskew = val;
276 for (i = -1; i < MAXPREFIX; i++) {
280 makeentry(entbuf, sizeof(entbuf), i, "addr");
281 addr = (char *)agetstr(entbuf, &bp);
285 /* allocate memory to store prefix information */
286 if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
288 "<%s> can't allocate enough memory",
292 memset(pfx, 0, sizeof(*pfx));
294 /* link into chain */
295 insque(pfx, &tmp->prefix);
299 pfx->origin = PREFIX_FROM_CONFIG;
301 if (inet_pton(AF_INET6, addr, &pfx->prefix) != 1) {
303 "<%s> inet_pton failed for %s",
307 if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
309 "<%s> multicast prefix (%s) must "
310 "not be advertised on %s",
311 __func__, addr, intface);
314 if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
316 "<%s> link-local prefix (%s) will be"
318 __func__, addr, intface);
320 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
321 MAYHAVE(val, entbuf, 64);
322 if (val < 0 || val > 128) {
323 syslog(LOG_ERR, "<%s> prefixlen (%ld) for %s "
324 "on %s out of range",
325 __func__, val, addr, intface);
328 pfx->prefixlen = (int)val;
330 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
331 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
333 if (strchr(flagstr, 'l'))
334 val |= ND_OPT_PI_FLAG_ONLINK;
335 if (strchr(flagstr, 'a'))
336 val |= ND_OPT_PI_FLAG_AUTO;
339 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
341 pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
342 pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
344 makeentry(entbuf, sizeof(entbuf), i, "vltime");
345 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
346 if (val64 < 0 || val64 > 0xffffffff) {
347 syslog(LOG_ERR, "<%s> vltime (%lld) for "
348 "%s/%d on %s is out of range",
349 __func__, (long long)val64,
350 addr, pfx->prefixlen, intface);
353 pfx->validlifetime = (u_int32_t)val64;
355 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
356 if (agetflag(entbuf)) {
358 gettimeofday(&now, 0);
360 now.tv_sec + pfx->validlifetime;
363 makeentry(entbuf, sizeof(entbuf), i, "pltime");
364 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
365 if (val64 < 0 || val64 > 0xffffffff) {
367 "<%s> pltime (%lld) for %s/%d on %s "
369 __func__, (long long)val64,
370 addr, pfx->prefixlen, intface);
373 pfx->preflifetime = (u_int32_t)val64;
375 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
376 if (agetflag(entbuf)) {
378 gettimeofday(&now, 0);
380 now.tv_sec + pfx->preflifetime;
386 MAYHAVE(val, "mtu", 0);
387 if (val < 0 || val > 0xffffffff) {
389 "<%s> mtu (%ld) on %s out of range",
390 __func__, val, intface);
393 tmp->linkmtu = (u_int32_t)val;
394 if (tmp->linkmtu == 0) {
397 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
398 strcmp(mtustr, "auto") == 0)
399 tmp->linkmtu = tmp->phymtu;
401 else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
403 "<%s> advertised link mtu (%lu) on %s is invalid (must "
404 "be between least MTU (%d) and physical link MTU (%d)",
405 __func__, (unsigned long)tmp->linkmtu, intface,
406 IPV6_MMTU, tmp->phymtu);
410 /* route information */
413 for (i = -1; i < MAXROUTE; i++) {
415 char entbuf[256], oentbuf[256];
417 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
418 addr = (char *)agetstr(entbuf, &bp);
420 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
421 addr = (char *)agetstr(oentbuf, &bp);
423 fprintf(stderr, "%s was obsoleted. Use %s.\n",
430 /* allocate memory to store prefix information */
431 if ((rti = malloc(sizeof(struct rtinfo))) == NULL) {
433 "<%s> can't allocate enough memory",
437 memset(rti, 0, sizeof(*rti));
439 /* link into chain */
440 insque(rti, &tmp->route);
443 if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) {
444 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
450 * XXX: currently there's no restriction in route information
451 * prefix according to
452 * draft-ietf-ipngwg-router-selection-00.txt.
453 * However, I think the similar restriction be necessary.
455 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
456 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
458 "<%s> multicast route (%s) must "
459 "not be advertised on %s",
460 __func__, addr, intface);
463 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
465 "<%s> link-local route (%s) will "
466 "be advertised on %s",
467 __func__, addr, intface);
472 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
473 /* XXX: 256 is a magic number for compatibility check. */
474 MAYHAVE(val, entbuf, 256);
476 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
477 MAYHAVE(val, oentbuf, 256);
479 fprintf(stderr, "%s was obsoleted. Use %s.\n",
484 if (val < 0 || val > 128) {
485 syslog(LOG_ERR, "<%s> prefixlen (%ld) for %s on %s "
487 __func__, val, addr, intface);
490 rti->prefixlen = (int)val;
492 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
493 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
495 if (strchr(flagstr, 'h'))
496 val |= ND_RA_FLAG_RTPREF_HIGH;
497 if (strchr(flagstr, 'l')) {
498 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
500 "<%s> the \'h\' and \'l\' route"
501 " preferences are exclusive",
505 val |= ND_RA_FLAG_RTPREF_LOW;
508 MAYHAVE(val, entbuf, 256); /* XXX */
510 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
511 MAYHAVE(val, oentbuf, 256);
513 fprintf(stderr, "%s was obsoleted. Use %s.\n",
518 rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
519 if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) {
520 syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
522 __func__, rti->rtpref, addr,
523 rti->prefixlen, intface);
528 * Since the spec does not a default value, we should make
529 * this entry mandatory. However, FreeBSD 4.4 has shipped
530 * with this field being optional, we use the router lifetime
531 * as an ad-hoc default value with a warning message.
533 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
534 MAYHAVE(val64, entbuf, -1);
536 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
537 MAYHAVE(val64, oentbuf, -1);
539 fprintf(stderr, "%s was obsoleted. Use %s.\n",
542 fprintf(stderr, "%s should be specified "
543 "for interface %s.\n",
545 val64 = tmp->lifetime;
548 if (val64 < 0 || val64 > 0xffffffff) {
549 syslog(LOG_ERR, "<%s> route lifetime (%lld) for "
550 "%s/%d on %s out of range", __func__,
551 (long long)val64, addr, rti->prefixlen, intface);
554 rti->ltime = (u_int32_t)val64;
562 /* construct the sending packet */
566 tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
568 ra_timer_update((void *)tmp, &tmp->timer->tm);
569 rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
573 get_prefix(struct rainfo *rai)
575 struct ifaddrs *ifap, *ifa;
578 u_char *p, *ep, *m, *lim;
579 u_char ntopbuf[INET6_ADDRSTRLEN];
581 if (getifaddrs(&ifap) < 0) {
583 "<%s> can't get interface addresses",
588 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
591 if (strcmp(ifa->ifa_name, rai->ifname) != 0)
593 if (ifa->ifa_addr->sa_family != AF_INET6)
595 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
596 if (IN6_IS_ADDR_LINKLOCAL(a))
598 /* get prefix length */
599 m = (u_char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
600 lim = (u_char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
601 plen = prefixlen(m, lim);
602 if (plen <= 0 || plen > 128) {
603 syslog(LOG_ERR, "<%s> failed to get prefixlen "
604 "or prefix is invalid",
608 if (plen == 128) /* XXX */
610 if (find_prefix(rai, a, plen)) {
611 /* ignore a duplicated prefix. */
615 /* allocate memory to store prefix info. */
616 if ((pp = malloc(sizeof(*pp))) == NULL) {
618 "<%s> can't get allocate buffer for prefix",
622 memset(pp, 0, sizeof(*pp));
624 /* set prefix, sweep bits outside of prefixlen */
625 pp->prefixlen = plen;
626 memcpy(&pp->prefix, a, sizeof(*a));
627 p = (u_char *)&pp->prefix;
628 ep = (u_char *)(&pp->prefix + 1);
633 if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
635 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
639 "<%s> add %s/%d to prefix list on %s",
640 __func__, ntopbuf, pp->prefixlen, rai->ifname);
642 /* set other fields with protocol defaults */
643 pp->validlifetime = DEF_ADVVALIDLIFETIME;
644 pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
647 pp->origin = PREFIX_FROM_KERNEL;
650 /* link into chain */
651 insque(pp, &rai->prefix);
653 /* counter increment */
661 makeentry(buf, len, id, string)
669 strlcpy(buf, string, len);
671 snprintf(buf, len, "%s%d", string, id);
675 * Add a prefix to the list of specified interface and reconstruct
676 * the outgoing packet.
677 * The prefix must not be in the list.
678 * XXX: other parameters of the prefix (e.g. lifetime) shoule be
679 * able to be specified.
682 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
684 struct prefix *prefix;
685 u_char ntopbuf[INET6_ADDRSTRLEN];
687 if ((prefix = malloc(sizeof(*prefix))) == NULL) {
688 syslog(LOG_ERR, "<%s> memory allocation failed",
690 return; /* XXX: error or exit? */
692 memset(prefix, 0, sizeof(*prefix));
693 prefix->prefix = ipr->ipr_prefix.sin6_addr;
694 prefix->prefixlen = ipr->ipr_plen;
695 prefix->validlifetime = ipr->ipr_vltime;
696 prefix->preflifetime = ipr->ipr_pltime;
697 prefix->onlinkflg = ipr->ipr_raf_onlink;
698 prefix->autoconfflg = ipr->ipr_raf_auto;
699 prefix->origin = PREFIX_FROM_DYNAMIC;
701 insque(prefix, &rai->prefix);
702 prefix->rainfo = rai;
704 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
705 __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
706 ntopbuf, INET6_ADDRSTRLEN),
707 ipr->ipr_plen, rai->ifname);
709 /* free the previous packet */
713 /* reconstruct the packet */
719 * Delete a prefix to the list of specified interface and reconstruct
720 * the outgoing packet.
721 * The prefix must be in the list.
724 delete_prefix(struct prefix *prefix)
726 u_char ntopbuf[INET6_ADDRSTRLEN];
727 struct rainfo *rai = prefix->rainfo;
730 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
731 __func__, inet_ntop(AF_INET6, &prefix->prefix,
732 ntopbuf, INET6_ADDRSTRLEN),
733 prefix->prefixlen, rai->ifname);
735 rtadvd_remove_timer(&prefix->timer);
741 invalidate_prefix(struct prefix *prefix)
743 u_char ntopbuf[INET6_ADDRSTRLEN];
745 struct rainfo *rai = prefix->rainfo;
747 if (prefix->timer) { /* sanity check */
749 "<%s> assumption failure: timer already exists",
754 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
755 "will expire in %ld seconds", __func__,
756 inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
757 prefix->prefixlen, rai->ifname, (long)prefix_timo);
759 /* set the expiration timer */
760 prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
761 if (prefix->timer == NULL) {
762 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
763 "remove the prefix", __func__);
764 delete_prefix(prefix);
766 timo.tv_sec = prefix_timo;
768 rtadvd_set_timer(&timo, prefix->timer);
771 static struct rtadvd_timer *
772 prefix_timeout(void *arg)
774 struct prefix *prefix = (struct prefix *)arg;
776 delete_prefix(prefix);
782 update_prefix(struct prefix * prefix)
784 u_char ntopbuf[INET6_ADDRSTRLEN];
785 struct rainfo *rai = prefix->rainfo;
787 if (prefix->timer == NULL) { /* sanity check */
789 "<%s> assumption failure: timer does not exist",
794 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
795 __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
796 INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
798 /* stop the expiration timer */
799 rtadvd_remove_timer(&prefix->timer);
803 * Try to get an in6_prefixreq contents for a prefix which matches
804 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
805 * the interface whose name is ipr->ipr_name[].
808 init_prefix(struct in6_prefixreq *ipr)
813 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
814 syslog(LOG_ERR, "<%s> socket: %s", __func__,
819 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
820 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
823 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
824 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
825 ipr->ipr_raf_onlink = 1;
826 ipr->ipr_raf_auto = 1;
827 /* omit other field initialization */
829 else if (ipr->ipr_origin < PR_ORIG_RR) {
830 u_char ntopbuf[INET6_ADDRSTRLEN];
832 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
833 "lower than PR_ORIG_RR(router renumbering)."
834 "This should not happen if I am router", __func__,
835 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
836 sizeof(ntopbuf)), ipr->ipr_origin);
844 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
845 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
846 ipr->ipr_raf_onlink = 1;
847 ipr->ipr_raf_auto = 1;
853 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
855 struct in6_prefixreq ipr;
857 memset(&ipr, 0, sizeof(ipr));
858 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
859 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
860 "exist. This should not happen! %s", __func__,
861 ifindex, strerror(errno));
864 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
865 ipr.ipr_prefix.sin6_family = AF_INET6;
866 ipr.ipr_prefix.sin6_addr = *addr;
869 if (init_prefix(&ipr))
870 return; /* init failed by some error */
871 add_prefix(rai, &ipr);
875 make_packet(struct rainfo *rainfo)
877 size_t packlen, lladdroptlen = 0;
879 struct nd_router_advert *ra;
880 struct nd_opt_prefix_info *ndopt_pi;
881 struct nd_opt_mtu *ndopt_mtu;
883 struct nd_opt_route_info *ndopt_rti;
888 /* calculate total length */
889 packlen = sizeof(struct nd_router_advert);
890 if (rainfo->advlinkopt) {
891 if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
893 "<%s> link-layer address option has"
894 " null length on %s. Treat as not included.",
895 __func__, rainfo->ifname);
896 rainfo->advlinkopt = 0;
898 packlen += lladdroptlen;
901 packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
903 packlen += sizeof(struct nd_opt_mtu);
905 for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next)
906 packlen += sizeof(struct nd_opt_route_info) +
907 ((rti->prefixlen + 0x3f) >> 6) * 8;
910 /* allocate memory for the packet */
911 if ((buf = malloc(packlen)) == NULL) {
913 "<%s> can't get enough memory for an RA packet",
917 if (rainfo->ra_data) {
918 /* free the previous packet */
919 free(rainfo->ra_data);
920 rainfo->ra_data = NULL;
922 rainfo->ra_data = buf;
923 /* XXX: what if packlen > 576? */
924 rainfo->ra_datalen = packlen;
927 * construct the packet
929 ra = (struct nd_router_advert *)buf;
930 ra->nd_ra_type = ND_ROUTER_ADVERT;
933 ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
934 ra->nd_ra_flags_reserved = 0; /* just in case */
936 * XXX: the router preference field, which is a 2-bit field, should be
937 * initialized before other fields.
939 ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
940 ra->nd_ra_flags_reserved |=
941 rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
942 ra->nd_ra_flags_reserved |=
943 rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
944 ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
945 ra->nd_ra_reachable = htonl(rainfo->reachabletime);
946 ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
949 if (rainfo->advlinkopt) {
950 lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
954 if (rainfo->linkmtu) {
955 ndopt_mtu = (struct nd_opt_mtu *)buf;
956 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
957 ndopt_mtu->nd_opt_mtu_len = 1;
958 ndopt_mtu->nd_opt_mtu_reserved = 0;
959 ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
960 buf += sizeof(struct nd_opt_mtu);
963 for (pfx = rainfo->prefix.next;
964 pfx != &rainfo->prefix; pfx = pfx->next) {
965 u_int32_t vltime, pltime;
968 ndopt_pi = (struct nd_opt_prefix_info *)buf;
969 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
970 ndopt_pi->nd_opt_pi_len = 4;
971 ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
972 ndopt_pi->nd_opt_pi_flags_reserved = 0;
974 ndopt_pi->nd_opt_pi_flags_reserved |=
975 ND_OPT_PI_FLAG_ONLINK;
976 if (pfx->autoconfflg)
977 ndopt_pi->nd_opt_pi_flags_reserved |=
982 if (pfx->vltimeexpire || pfx->pltimeexpire)
983 gettimeofday(&now, NULL);
984 if (pfx->vltimeexpire == 0)
985 vltime = pfx->validlifetime;
987 vltime = (pfx->vltimeexpire > now.tv_sec) ?
988 pfx->vltimeexpire - now.tv_sec : 0;
993 if (pfx->pltimeexpire == 0)
994 pltime = pfx->preflifetime;
996 pltime = (pfx->pltimeexpire > now.tv_sec) ?
997 pfx->pltimeexpire - now.tv_sec : 0;
999 if (vltime < pltime) {
1001 * this can happen if vltime is decrement but pltime
1006 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1007 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1008 ndopt_pi->nd_opt_pi_reserved2 = 0;
1009 ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1011 buf += sizeof(struct nd_opt_prefix_info);
1015 for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next) {
1016 u_int8_t psize = (rti->prefixlen + 0x3f) >> 6;
1018 ndopt_rti = (struct nd_opt_route_info *)buf;
1019 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1020 ndopt_rti->nd_opt_rti_len = 1 + psize;
1021 ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1022 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1023 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1024 memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1025 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1033 getinet6sysctl(int code)
1035 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
1040 size = sizeof(value);
1041 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
1043 syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",