2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/rwlock.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
53 #include <net/if_types.h>
54 #include <net/if_dl.h>
55 #include <net/route.h>
56 #include <net/radix.h>
59 #include <netinet/in.h>
60 #include <net/if_llatbl.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet6/in6_ifattach.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/nd6.h>
66 #include <netinet/icmp6.h>
67 #include <netinet6/scope6_var.h>
69 static int rtpref(struct nd_defrouter *);
70 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
71 static int prelist_update __P((struct nd_prefixctl *, struct nd_defrouter *,
73 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
74 static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *,
75 struct nd_defrouter *));
76 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
77 static void pfxrtr_del(struct nd_pfxrouter *);
78 static struct nd_pfxrouter *find_pfxlist_reachable_router
80 static void defrouter_delreq(struct nd_defrouter *);
81 static void nd6_rtmsg(int, struct rtentry *);
83 static int in6_init_prefix_ltimes(struct nd_prefix *);
84 static void in6_init_address_ltimes __P((struct nd_prefix *,
85 struct in6_addrlifetime *));
87 static int nd6_prefix_onlink(struct nd_prefix *);
88 static int nd6_prefix_offlink(struct nd_prefix *);
90 static int rt6_deleteroute(struct radix_node *, void *);
92 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
93 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
95 static VNET_DEFINE(struct ifnet *, nd6_defifp);
96 VNET_DEFINE(int, nd6_defifindex);
97 #define V_nd6_defifp VNET(nd6_defifp)
99 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
101 VNET_DEFINE(int, ip6_desync_factor);
102 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
103 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
105 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
107 /* RTPREF_MEDIUM has to be 0! */
108 #define RTPREF_HIGH 1
109 #define RTPREF_MEDIUM 0
110 #define RTPREF_LOW (-1)
111 #define RTPREF_RESERVED (-2)
112 #define RTPREF_INVALID (-3) /* internal */
115 * Receive Router Solicitation Message - just for routers.
116 * Router solicitation/advertisement is mostly managed by userland program
117 * (rtadvd) so here we have no function like nd6_ra_output().
122 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
124 struct ifnet *ifp = m->m_pkthdr.rcvif;
125 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
126 struct nd_router_solicit *nd_rs;
127 struct in6_addr saddr6 = ip6->ip6_src;
130 union nd_opts ndopts;
131 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
133 /* If I'm not a router, ignore it. */
134 if (V_ip6_accept_rtadv != 0 || V_ip6_forwarding != 1)
138 if (ip6->ip6_hlim != 255) {
140 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
141 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
142 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
147 * Don't update the neighbor cache, if src = ::.
148 * This indicates that the src has no IP address assigned yet.
150 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
153 #ifndef PULLDOWN_TEST
154 IP6_EXTHDR_CHECK(m, off, icmp6len,);
155 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
157 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
159 ICMP6STAT_INC(icp6s_tooshort);
164 icmp6len -= sizeof(*nd_rs);
165 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
166 if (nd6_options(&ndopts) < 0) {
168 "nd6_rs_input: invalid ND option, ignored\n"));
169 /* nd6_options have incremented stats */
173 if (ndopts.nd_opts_src_lladdr) {
174 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
175 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
178 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
180 "nd6_rs_input: lladdrlen mismatch for %s "
181 "(if %d, RS packet %d)\n",
182 ip6_sprintf(ip6bufs, &saddr6),
183 ifp->if_addrlen, lladdrlen - 2));
187 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
194 ICMP6STAT_INC(icp6s_badrs);
199 * Receive Router Advertisement Message.
202 * TODO: on-link bit on prefix information
203 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
206 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
208 struct ifnet *ifp = m->m_pkthdr.rcvif;
209 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
210 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
211 struct nd_router_advert *nd_ra;
212 struct in6_addr saddr6 = ip6->ip6_src;
214 union nd_opts ndopts;
215 struct nd_defrouter *dr;
216 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
219 * We only accept RAs only when
220 * the system-wide variable allows the acceptance, and
221 * per-interface variable allows RAs on the receiving interface.
223 if (V_ip6_accept_rtadv == 0)
225 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
228 if (ip6->ip6_hlim != 255) {
230 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
231 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
232 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
236 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
238 "nd6_ra_input: src %s is not link-local\n",
239 ip6_sprintf(ip6bufs, &saddr6)));
243 #ifndef PULLDOWN_TEST
244 IP6_EXTHDR_CHECK(m, off, icmp6len,);
245 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
247 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
249 ICMP6STAT_INC(icp6s_tooshort);
254 icmp6len -= sizeof(*nd_ra);
255 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
256 if (nd6_options(&ndopts) < 0) {
258 "nd6_ra_input: invalid ND option, ignored\n"));
259 /* nd6_options have incremented stats */
264 struct nd_defrouter dr0;
265 u_int32_t advreachable = nd_ra->nd_ra_reachable;
267 /* remember if this is a multicasted advertisement */
268 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
271 bzero(&dr0, sizeof(dr0));
273 dr0.flags = nd_ra->nd_ra_flags_reserved;
274 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
275 dr0.expire = time_second + dr0.rtlifetime;
277 /* unspecified or not? (RFC 2461 6.3.4) */
279 advreachable = ntohl(advreachable);
280 if (advreachable <= MAX_REACHABLE_TIME &&
281 ndi->basereachable != advreachable) {
282 ndi->basereachable = advreachable;
283 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
284 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
287 if (nd_ra->nd_ra_retransmit)
288 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
289 if (nd_ra->nd_ra_curhoplimit) {
290 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
291 ndi->chlim = nd_ra->nd_ra_curhoplimit;
292 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
293 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
294 "%s on %s (current = %d, received = %d). "
295 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
296 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
299 dr = defrtrlist_update(&dr0);
305 if (ndopts.nd_opts_pi) {
306 struct nd_opt_hdr *pt;
307 struct nd_opt_prefix_info *pi = NULL;
308 struct nd_prefixctl pr;
310 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
311 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
312 pt = (struct nd_opt_hdr *)((caddr_t)pt +
313 (pt->nd_opt_len << 3))) {
314 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
316 pi = (struct nd_opt_prefix_info *)pt;
318 if (pi->nd_opt_pi_len != 4) {
320 "nd6_ra_input: invalid option "
321 "len %d for prefix information option, "
322 "ignored\n", pi->nd_opt_pi_len));
326 if (128 < pi->nd_opt_pi_prefix_len) {
328 "nd6_ra_input: invalid prefix "
329 "len %d for prefix information option, "
330 "ignored\n", pi->nd_opt_pi_prefix_len));
334 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
335 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
337 "nd6_ra_input: invalid prefix "
340 &pi->nd_opt_pi_prefix)));
344 bzero(&pr, sizeof(pr));
345 pr.ndpr_prefix.sin6_family = AF_INET6;
346 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
347 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
348 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
350 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
351 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
352 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
353 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
354 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
355 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
356 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
357 (void)prelist_update(&pr, dr, m, mcast);
364 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
368 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
371 if (mtu < IPV6_MMTU) {
372 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
373 "mtu=%lu sent from %s, ignoring\n",
374 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
379 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
380 ? ndi->maxmtu : ifp->if_mtu;
382 int change = (ndi->linkmtu != mtu);
385 if (change) /* in6_maxmtu may change */
388 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
389 "mtu=%lu sent from %s; "
390 "exceeds maxmtu %lu, ignoring\n",
391 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
398 * Source link layer address
404 if (ndopts.nd_opts_src_lladdr) {
405 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
406 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
409 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
411 "nd6_ra_input: lladdrlen mismatch for %s "
412 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
413 ifp->if_addrlen, lladdrlen - 2));
417 nd6_cache_lladdr(ifp, &saddr6, lladdr,
418 lladdrlen, ND_ROUTER_ADVERT, 0);
421 * Installing a link-layer address might change the state of the
422 * router's neighbor cache, which might also affect our on-link
423 * detection of adveritsed prefixes.
425 pfxlist_onlink_check();
433 ICMP6STAT_INC(icp6s_badra);
438 * default router list proccessing sub routines
441 /* tell the change to user processes watching the routing socket. */
443 nd6_rtmsg(int cmd, struct rtentry *rt)
445 struct rt_addrinfo info;
449 bzero((caddr_t)&info, sizeof(info));
450 info.rti_info[RTAX_DST] = rt_key(rt);
451 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
452 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
456 ifa = TAILQ_FIRST(&ifp->if_addrhead);
457 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
459 IF_ADDR_RUNLOCK(ifp);
460 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
464 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
470 defrouter_addreq(struct nd_defrouter *new)
472 struct sockaddr_in6 def, mask, gate;
473 struct rtentry *newrt = NULL;
477 bzero(&def, sizeof(def));
478 bzero(&mask, sizeof(mask));
479 bzero(&gate, sizeof(gate));
481 def.sin6_len = mask.sin6_len = gate.sin6_len =
482 sizeof(struct sockaddr_in6);
483 def.sin6_family = gate.sin6_family = AF_INET6;
484 gate.sin6_addr = new->rtaddr;
487 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
488 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
489 RTF_GATEWAY, &newrt, RT_DEFAULT_FIB);
491 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
500 struct nd_defrouter *
501 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
503 struct nd_defrouter *dr;
505 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
506 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
510 return (NULL); /* search failed */
514 * Remove the default route for a given router.
515 * This is just a subroutine function for defrouter_select(), and should
516 * not be called from anywhere else.
519 defrouter_delreq(struct nd_defrouter *dr)
521 struct sockaddr_in6 def, mask, gate;
522 struct rtentry *oldrt = NULL;
524 bzero(&def, sizeof(def));
525 bzero(&mask, sizeof(mask));
526 bzero(&gate, sizeof(gate));
528 def.sin6_len = mask.sin6_len = gate.sin6_len =
529 sizeof(struct sockaddr_in6);
530 def.sin6_family = gate.sin6_family = AF_INET6;
531 gate.sin6_addr = dr->rtaddr;
533 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
534 (struct sockaddr *)&gate,
535 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, RT_DEFAULT_FIB);
537 nd6_rtmsg(RTM_DELETE, oldrt);
545 * remove all default routes from default router list
548 defrouter_reset(void)
550 struct nd_defrouter *dr;
552 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
553 defrouter_delreq(dr);
556 * XXX should we also nuke any default routers in the kernel, by
557 * going through them by rtalloc1()?
562 defrtrlist_del(struct nd_defrouter *dr)
564 struct nd_defrouter *deldr = NULL;
565 struct nd_prefix *pr;
568 * Flush all the routing table entries that use the router
571 if (!V_ip6_forwarding && V_ip6_accept_rtadv) /* XXX: better condition? */
572 rt6_flush(&dr->rtaddr, dr->ifp);
576 defrouter_delreq(dr);
578 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
581 * Also delete all the pointers to the router in each prefix lists.
583 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
584 struct nd_pfxrouter *pfxrtr;
585 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
588 pfxlist_onlink_check();
591 * If the router is the primary one, choose a new one.
592 * Note that defrouter_select() will remove the current gateway
593 * from the routing table.
602 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
603 * draft-ietf-ipngwg-router-selection:
604 * 1) Routers that are reachable or probably reachable should be preferred.
605 * If we have more than one (probably) reachable router, prefer ones
606 * with the highest router preference.
607 * 2) When no routers on the list are known to be reachable or
608 * probably reachable, routers SHOULD be selected in a round-robin
609 * fashion, regardless of router preference values.
610 * 3) If the Default Router List is empty, assume that all
611 * destinations are on-link.
613 * We assume nd_defrouter is sorted by router preference value.
614 * Since the code below covers both with and without router preference cases,
615 * we do not need to classify the cases by ifdef.
617 * At this moment, we do not try to install more than one default router,
618 * even when the multipath routing is available, because we're not sure about
619 * the benefits for stub hosts comparing to the risk of making the code
620 * complicated and the possibility of introducing bugs.
623 defrouter_select(void)
626 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL;
627 struct llentry *ln = NULL;
630 * This function should be called only when acting as an autoconfigured
631 * host. Although the remaining part of this function is not effective
632 * if the node is not an autoconfigured host, we explicitly exclude
633 * such cases here for safety.
635 if (V_ip6_forwarding || !V_ip6_accept_rtadv) {
637 "defrouter_select: called unexpectedly (forwarding=%d, "
638 "accept_rtadv=%d)\n", V_ip6_forwarding, V_ip6_accept_rtadv));
644 * Let's handle easy case (3) first:
645 * If default router list is empty, there's nothing to be done.
647 if (TAILQ_EMPTY(&V_nd_defrouter)) {
653 * Search for a (probably) reachable router from the list.
654 * We just pick up the first reachable one (if any), assuming that
655 * the ordering rule of the list described in defrtrlist_update().
657 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
658 IF_AFDATA_RLOCK(dr->ifp);
659 if (selected_dr == NULL &&
660 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
661 ND6_IS_LLINFO_PROBREACH(ln)) {
664 IF_AFDATA_RUNLOCK(dr->ifp);
670 if (dr->installed && installed_dr == NULL)
672 else if (dr->installed && installed_dr) {
673 /* this should not happen. warn for diagnosis. */
674 log(LOG_ERR, "defrouter_select: more than one router"
679 * If none of the default routers was found to be reachable,
680 * round-robin the list regardless of preference.
681 * Otherwise, if we have an installed router, check if the selected
682 * (reachable) router should really be preferred to the installed one.
683 * We only prefer the new router when the old one is not reachable
684 * or when the new one has a really higher preference value.
686 if (selected_dr == NULL) {
687 if (installed_dr == NULL || !TAILQ_NEXT(installed_dr, dr_entry))
688 selected_dr = TAILQ_FIRST(&V_nd_defrouter);
690 selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
691 } else if (installed_dr) {
692 IF_AFDATA_RLOCK(installed_dr->ifp);
693 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
694 ND6_IS_LLINFO_PROBREACH(ln) &&
695 rtpref(selected_dr) <= rtpref(installed_dr)) {
696 selected_dr = installed_dr;
698 IF_AFDATA_RUNLOCK(installed_dr->ifp);
704 * If the selected router is different than the installed one,
705 * remove the installed router and install the selected one.
706 * Note that the selected router is never NULL here.
708 if (installed_dr != selected_dr) {
710 defrouter_delreq(installed_dr);
711 defrouter_addreq(selected_dr);
719 * for default router selection
720 * regards router-preference field as a 2-bit signed integer
723 rtpref(struct nd_defrouter *dr)
725 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
726 case ND_RA_FLAG_RTPREF_HIGH:
727 return (RTPREF_HIGH);
728 case ND_RA_FLAG_RTPREF_MEDIUM:
729 case ND_RA_FLAG_RTPREF_RSV:
730 return (RTPREF_MEDIUM);
731 case ND_RA_FLAG_RTPREF_LOW:
735 * This case should never happen. If it did, it would mean a
736 * serious bug of kernel internal. We thus always bark here.
737 * Or, can we even panic?
739 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags);
740 return (RTPREF_INVALID);
745 static struct nd_defrouter *
746 defrtrlist_update(struct nd_defrouter *new)
748 struct nd_defrouter *dr, *n;
751 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
753 if (new->rtlifetime == 0) {
757 int oldpref = rtpref(dr);
760 dr->flags = new->flags; /* xxx flag check */
761 dr->rtlifetime = new->rtlifetime;
762 dr->expire = new->expire;
765 * If the preference does not change, there's no need
766 * to sort the entries. Also make sure the selected
767 * router is still installed in the kernel.
769 if (dr->installed && rtpref(new) == oldpref) {
775 * preferred router may be changed, so relocate
777 * XXX: calling TAILQ_REMOVE directly is a bad manner.
778 * However, since defrtrlist_del() has many side
779 * effects, we intentionally do so here.
780 * defrouter_select() below will handle routing
783 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
791 /* entry does not exist */
792 if (new->rtlifetime == 0) {
797 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
802 bzero(n, sizeof(*n));
807 * Insert the new router in the Default Router List;
808 * The Default Router List should be in the descending order
809 * of router-preferece. Routers with the same preference are
810 * sorted in the arriving time order.
813 /* insert at the end of the group */
814 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
815 if (rtpref(n) > rtpref(dr))
819 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
821 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
830 static struct nd_pfxrouter *
831 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
833 struct nd_pfxrouter *search;
835 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
836 if (search->router == dr)
844 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
846 struct nd_pfxrouter *new;
848 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
851 bzero(new, sizeof(*new));
854 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
856 pfxlist_onlink_check();
860 pfxrtr_del(struct nd_pfxrouter *pfr)
862 LIST_REMOVE(pfr, pfr_entry);
867 nd6_prefix_lookup(struct nd_prefixctl *key)
869 struct nd_prefix *search;
871 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
872 if (key->ndpr_ifp == search->ndpr_ifp &&
873 key->ndpr_plen == search->ndpr_plen &&
874 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
875 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
884 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
885 struct nd_prefix **newp)
887 struct nd_prefix *new = NULL;
890 char ip6buf[INET6_ADDRSTRLEN];
892 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
895 bzero(new, sizeof(*new));
896 new->ndpr_ifp = pr->ndpr_ifp;
897 new->ndpr_prefix = pr->ndpr_prefix;
898 new->ndpr_plen = pr->ndpr_plen;
899 new->ndpr_vltime = pr->ndpr_vltime;
900 new->ndpr_pltime = pr->ndpr_pltime;
901 new->ndpr_flags = pr->ndpr_flags;
902 if ((error = in6_init_prefix_ltimes(new)) != 0) {
906 new->ndpr_lastupdate = time_second;
911 LIST_INIT(&new->ndpr_advrtrs);
912 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
913 /* make prefix in the canonical form */
914 for (i = 0; i < 4; i++)
915 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
916 new->ndpr_mask.s6_addr32[i];
919 /* link ndpr_entry to nd_prefix list */
920 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
923 /* ND_OPT_PI_FLAG_ONLINK processing */
924 if (new->ndpr_raf_onlink) {
927 if ((e = nd6_prefix_onlink(new)) != 0) {
928 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
929 "the prefix %s/%d on-link on %s (errno=%d)\n",
930 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
931 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
932 /* proceed anyway. XXX: is it correct? */
943 prelist_remove(struct nd_prefix *pr)
945 struct nd_pfxrouter *pfr, *next;
947 char ip6buf[INET6_ADDRSTRLEN];
949 /* make sure to invalidate the prefix until it is really freed. */
954 * Though these flags are now meaningless, we'd rather keep the value
955 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
956 * when executing "ndp -p".
959 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
960 (e = nd6_prefix_offlink(pr)) != 0) {
961 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
963 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
964 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
965 /* what should we do? */
968 if (pr->ndpr_refcnt > 0)
969 return; /* notice here? */
973 /* unlink ndpr_entry from nd_prefix list */
974 LIST_REMOVE(pr, ndpr_entry);
976 /* free list of routers that adversed the prefix */
977 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next) {
984 pfxlist_onlink_check();
992 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
993 struct mbuf *m, int mcast)
995 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
997 struct ifnet *ifp = new->ndpr_ifp;
998 struct nd_prefix *pr;
1003 struct in6_addrlifetime lt6_tmp;
1004 char ip6buf[INET6_ADDRSTRLEN];
1009 * Authenticity for NA consists authentication for
1010 * both IP header and IP datagrams, doesn't it ?
1012 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1013 auth = ((m->m_flags & M_AUTHIPHDR) &&
1014 (m->m_flags & M_AUTHIPDGM));
1018 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1020 * nd6_prefix_lookup() ensures that pr and new have the same
1021 * prefix on a same interface.
1025 * Update prefix information. Note that the on-link (L) bit
1026 * and the autonomous (A) bit should NOT be changed from 1
1029 if (new->ndpr_raf_onlink == 1)
1030 pr->ndpr_raf_onlink = 1;
1031 if (new->ndpr_raf_auto == 1)
1032 pr->ndpr_raf_auto = 1;
1033 if (new->ndpr_raf_onlink) {
1034 pr->ndpr_vltime = new->ndpr_vltime;
1035 pr->ndpr_pltime = new->ndpr_pltime;
1036 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1037 pr->ndpr_lastupdate = time_second;
1040 if (new->ndpr_raf_onlink &&
1041 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1044 if ((e = nd6_prefix_onlink(pr)) != 0) {
1046 "prelist_update: failed to make "
1047 "the prefix %s/%d on-link on %s "
1050 &pr->ndpr_prefix.sin6_addr),
1051 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1052 /* proceed anyway. XXX: is it correct? */
1056 if (dr && pfxrtr_lookup(pr, dr) == NULL)
1059 struct nd_prefix *newpr = NULL;
1063 if (new->ndpr_vltime == 0)
1065 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1068 error = nd6_prelist_add(new, dr, &newpr);
1069 if (error != 0 || newpr == NULL) {
1070 nd6log((LOG_NOTICE, "prelist_update: "
1071 "nd6_prelist_add failed for %s/%d on %s "
1072 "errno=%d, returnpr=%p\n",
1073 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1074 new->ndpr_plen, if_name(new->ndpr_ifp),
1076 goto end; /* we should just give up in this case. */
1080 * XXX: from the ND point of view, we can ignore a prefix
1081 * with the on-link bit being zero. However, we need a
1082 * prefix structure for references from autoconfigured
1083 * addresses. Thus, we explicitly make sure that the prefix
1084 * itself expires now.
1086 if (newpr->ndpr_raf_onlink == 0) {
1087 newpr->ndpr_vltime = 0;
1088 newpr->ndpr_pltime = 0;
1089 in6_init_prefix_ltimes(newpr);
1096 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1097 * Note that pr must be non NULL at this point.
1100 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1101 if (!new->ndpr_raf_auto)
1105 * 5.5.3 (b). the link-local prefix should have been ignored in
1109 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1110 if (new->ndpr_pltime > new->ndpr_vltime) {
1111 error = EINVAL; /* XXX: won't be used */
1116 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1117 * an address configured by stateless autoconfiguration already in the
1118 * list of addresses associated with the interface, and the Valid
1119 * Lifetime is not 0, form an address. We first check if we have
1120 * a matching prefix.
1121 * Note: we apply a clarification in rfc2462bis-02 here. We only
1122 * consider autoconfigured addresses while RFC2462 simply said
1126 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1127 struct in6_ifaddr *ifa6;
1128 u_int32_t remaininglifetime;
1130 if (ifa->ifa_addr->sa_family != AF_INET6)
1133 ifa6 = (struct in6_ifaddr *)ifa;
1136 * We only consider autoconfigured addresses as per rfc2462bis.
1138 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1142 * Spec is not clear here, but I believe we should concentrate
1143 * on unicast (i.e. not anycast) addresses.
1144 * XXX: other ia6_flags? detached or duplicated?
1146 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1150 * Ignore the address if it is not associated with a prefix
1151 * or is associated with a prefix that is different from this
1152 * one. (pr is never NULL here)
1154 if (ifa6->ia6_ndpr != pr)
1157 if (ia6_match == NULL) /* remember the first one */
1161 * An already autoconfigured address matched. Now that we
1162 * are sure there is at least one matched address, we can
1163 * proceed to 5.5.3. (e): update the lifetimes according to the
1164 * "two hours" rule and the privacy extension.
1165 * We apply some clarifications in rfc2462bis:
1166 * - use remaininglifetime instead of storedlifetime as a
1168 * - remove the dead code in the "two-hour" rule
1170 #define TWOHOUR (120*60)
1171 lt6_tmp = ifa6->ia6_lifetime;
1173 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1174 remaininglifetime = ND6_INFINITE_LIFETIME;
1175 else if (time_second - ifa6->ia6_updatetime >
1176 lt6_tmp.ia6t_vltime) {
1178 * The case of "invalid" address. We should usually
1179 * not see this case.
1181 remaininglifetime = 0;
1183 remaininglifetime = lt6_tmp.ia6t_vltime -
1184 (time_second - ifa6->ia6_updatetime);
1186 /* when not updating, keep the current stored lifetime. */
1187 lt6_tmp.ia6t_vltime = remaininglifetime;
1189 if (TWOHOUR < new->ndpr_vltime ||
1190 remaininglifetime < new->ndpr_vltime) {
1191 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1192 } else if (remaininglifetime <= TWOHOUR) {
1194 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1198 * new->ndpr_vltime <= TWOHOUR &&
1199 * TWOHOUR < remaininglifetime
1201 lt6_tmp.ia6t_vltime = TWOHOUR;
1204 /* The 2 hour rule is not imposed for preferred lifetime. */
1205 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1207 in6_init_address_ltimes(pr, <6_tmp);
1210 * We need to treat lifetimes for temporary addresses
1211 * differently, according to
1212 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1213 * we only update the lifetimes when they are in the maximum
1216 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1217 u_int32_t maxvltime, maxpltime;
1219 if (V_ip6_temp_valid_lifetime >
1220 (u_int32_t)((time_second - ifa6->ia6_createtime) +
1221 V_ip6_desync_factor)) {
1222 maxvltime = V_ip6_temp_valid_lifetime -
1223 (time_second - ifa6->ia6_createtime) -
1224 V_ip6_desync_factor;
1227 if (V_ip6_temp_preferred_lifetime >
1228 (u_int32_t)((time_second - ifa6->ia6_createtime) +
1229 V_ip6_desync_factor)) {
1230 maxpltime = V_ip6_temp_preferred_lifetime -
1231 (time_second - ifa6->ia6_createtime) -
1232 V_ip6_desync_factor;
1236 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1237 lt6_tmp.ia6t_vltime > maxvltime) {
1238 lt6_tmp.ia6t_vltime = maxvltime;
1240 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1241 lt6_tmp.ia6t_pltime > maxpltime) {
1242 lt6_tmp.ia6t_pltime = maxpltime;
1245 ifa6->ia6_lifetime = lt6_tmp;
1246 ifa6->ia6_updatetime = time_second;
1248 IF_ADDR_RUNLOCK(ifp);
1249 if (ia6_match == NULL && new->ndpr_vltime) {
1253 * 5.5.3 (d) (continued)
1254 * No address matched and the valid lifetime is non-zero.
1255 * Create a new address.
1259 * Prefix Length check:
1260 * If the sum of the prefix length and interface identifier
1261 * length does not equal 128 bits, the Prefix Information
1262 * option MUST be ignored. The length of the interface
1263 * identifier is defined in a separate link-type specific
1266 ifidlen = in6_if2idlen(ifp);
1268 /* this should not happen, so we always log it. */
1269 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1273 if (ifidlen + pr->ndpr_plen != 128) {
1275 "prelist_update: invalid prefixlen "
1276 "%d for %s, ignored\n",
1277 pr->ndpr_plen, if_name(ifp)));
1281 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1283 * note that we should use pr (not new) for reference.
1290 * When a new public address is created as described
1291 * in RFC2462, also create a new temporary address.
1294 * When an interface connects to a new link, a new
1295 * randomized interface identifier should be generated
1296 * immediately together with a new set of temporary
1297 * addresses. Thus, we specifiy 1 as the 2nd arg of
1300 if (V_ip6_use_tempaddr) {
1302 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1303 nd6log((LOG_NOTICE, "prelist_update: "
1304 "failed to create a temporary "
1305 "address, errno=%d\n",
1309 ifa_free(&ia6->ia_ifa);
1312 * A newly added address might affect the status
1313 * of other addresses, so we check and update it.
1314 * XXX: what if address duplication happens?
1316 pfxlist_onlink_check();
1318 /* just set an error. do not bark here. */
1319 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1329 * A supplement function used in the on-link detection below;
1330 * detect if a given prefix has a (probably) reachable advertising router.
1331 * XXX: lengthy function name...
1333 static struct nd_pfxrouter *
1334 find_pfxlist_reachable_router(struct nd_prefix *pr)
1336 struct nd_pfxrouter *pfxrtr;
1340 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1341 IF_AFDATA_RLOCK(pfxrtr->router->ifp);
1342 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1343 IF_AFDATA_RUNLOCK(pfxrtr->router->ifp);
1346 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1355 * Check if each prefix in the prefix list has at least one available router
1356 * that advertised the prefix (a router is "available" if its neighbor cache
1357 * entry is reachable or probably reachable).
1358 * If the check fails, the prefix may be off-link, because, for example,
1359 * we have moved from the network but the lifetime of the prefix has not
1360 * expired yet. So we should not use the prefix if there is another prefix
1361 * that has an available router.
1362 * But, if there is no prefix that has an available router, we still regards
1363 * all the prefixes as on-link. This is because we can't tell if all the
1364 * routers are simply dead or if we really moved from the network and there
1365 * is no router around us.
1368 pfxlist_onlink_check()
1370 struct nd_prefix *pr;
1371 struct in6_ifaddr *ifa;
1372 struct nd_defrouter *dr;
1373 struct nd_pfxrouter *pfxrtr = NULL;
1376 * Check if there is a prefix that has a reachable advertising
1379 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1380 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1385 * If we have no such prefix, check whether we still have a router
1386 * that does not advertise any prefixes.
1389 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1390 struct nd_prefix *pr0;
1392 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1393 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1400 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
1402 * There is at least one prefix that has a reachable router,
1403 * or at least a router which probably does not advertise
1404 * any prefixes. The latter would be the case when we move
1405 * to a new link where we have a router that does not provide
1406 * prefixes and we configure an address by hand.
1407 * Detach prefixes which have no reachable advertising
1408 * router, and attach other prefixes.
1410 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1411 /* XXX: a link-local prefix should never be detached */
1412 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1416 * we aren't interested in prefixes without the L bit
1419 if (pr->ndpr_raf_onlink == 0)
1422 if (pr->ndpr_raf_auto == 0)
1425 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1426 find_pfxlist_reachable_router(pr) == NULL)
1427 pr->ndpr_stateflags |= NDPRF_DETACHED;
1428 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1429 find_pfxlist_reachable_router(pr) != 0)
1430 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1433 /* there is no prefix that has a reachable router */
1434 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1435 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1438 if (pr->ndpr_raf_onlink == 0)
1441 if (pr->ndpr_raf_auto == 0)
1444 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1445 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1450 * Remove each interface route associated with a (just) detached
1451 * prefix, and reinstall the interface route for a (just) attached
1452 * prefix. Note that all attempt of reinstallation does not
1453 * necessarily success, when a same prefix is shared among multiple
1454 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1455 * so we don't have to care about them.
1457 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1459 char ip6buf[INET6_ADDRSTRLEN];
1461 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1464 if (pr->ndpr_raf_onlink == 0)
1467 if (pr->ndpr_raf_auto == 0)
1470 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1471 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1472 if ((e = nd6_prefix_offlink(pr)) != 0) {
1474 "pfxlist_onlink_check: failed to "
1475 "make %s/%d offlink, errno=%d\n",
1477 &pr->ndpr_prefix.sin6_addr),
1481 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1482 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1483 pr->ndpr_raf_onlink) {
1484 if ((e = nd6_prefix_onlink(pr)) != 0) {
1486 "pfxlist_onlink_check: failed to "
1487 "make %s/%d onlink, errno=%d\n",
1489 &pr->ndpr_prefix.sin6_addr),
1496 * Changes on the prefix status might affect address status as well.
1497 * Make sure that all addresses derived from an attached prefix are
1498 * attached, and that all addresses derived from a detached prefix are
1499 * detached. Note, however, that a manually configured address should
1500 * always be attached.
1501 * The precise detection logic is same as the one for prefixes.
1503 * XXXRW: in6_ifaddrhead locking.
1505 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1506 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1509 if (ifa->ia6_ndpr == NULL) {
1511 * This can happen when we first configure the address
1512 * (i.e. the address exists, but the prefix does not).
1513 * XXX: complicated relationships...
1518 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1522 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1523 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1526 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1529 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1530 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1531 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1532 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1533 nd6_dad_start((struct ifaddr *)ifa, 0);
1536 ifa->ia6_flags |= IN6_IFF_DETACHED;
1541 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1542 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1545 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1546 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1547 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1548 /* Do we need a delay in this case? */
1549 nd6_dad_start((struct ifaddr *)ifa, 0);
1556 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
1558 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1559 struct radix_node_head *rnh;
1561 struct sockaddr_in6 mask6;
1563 int error, a_failure, fibnum;
1566 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1567 * ifa->ifa_rtrequest = nd6_rtrequest;
1569 bzero(&mask6, sizeof(mask6));
1570 mask6.sin6_len = sizeof(mask6);
1571 mask6.sin6_addr = pr->ndpr_mask;
1572 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1575 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1578 error = in6_rtrequest(RTM_ADD,
1579 (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr,
1580 (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
1582 KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
1583 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
1586 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
1587 /* XXX what if rhn == NULL? */
1588 RADIX_NODE_HEAD_LOCK(rnh);
1590 if (rt_setgate(rt, rt_key(rt),
1591 (struct sockaddr *)&null_sdl) == 0) {
1592 struct sockaddr_dl *dl;
1594 dl = (struct sockaddr_dl *)rt->rt_gateway;
1595 dl->sdl_type = rt->rt_ifp->if_type;
1596 dl->sdl_index = rt->rt_ifp->if_index;
1598 RADIX_NODE_HEAD_UNLOCK(rnh);
1599 nd6_rtmsg(RTM_ADD, rt);
1601 pr->ndpr_stateflags |= NDPRF_ONLINK;
1603 char ip6buf[INET6_ADDRSTRLEN];
1604 char ip6bufg[INET6_ADDRSTRLEN];
1605 char ip6bufm[INET6_ADDRSTRLEN];
1606 struct sockaddr_in6 *sin6;
1608 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1609 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
1610 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
1611 "flags=%lx errno = %d\n",
1612 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1613 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1614 ip6_sprintf(ip6bufg, &sin6->sin6_addr),
1615 ip6_sprintf(ip6bufm, &mask6.sin6_addr),
1618 /* Save last error to return, see rtinit(). */
1629 /* Return the last error we got. */
1634 nd6_prefix_onlink(struct nd_prefix *pr)
1637 struct ifnet *ifp = pr->ndpr_ifp;
1638 struct nd_prefix *opr;
1640 char ip6buf[INET6_ADDRSTRLEN];
1643 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1645 "nd6_prefix_onlink: %s/%d is already on-link\n",
1646 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1652 * Add the interface route associated with the prefix. Before
1653 * installing the route, check if there's the same prefix on another
1654 * interface, and the prefix has already installed the interface route.
1655 * Although such a configuration is expected to be rare, we explicitly
1658 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1662 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1665 if (opr->ndpr_plen == pr->ndpr_plen &&
1666 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1667 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1672 * We prefer link-local addresses as the associated interface address.
1674 /* search for a link-local addr */
1675 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1676 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1678 /* XXX: freebsd does not have ifa_ifwithaf */
1680 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1681 if (ifa->ifa_addr->sa_family == AF_INET6)
1686 IF_ADDR_RUNLOCK(ifp);
1687 /* should we care about ia6_flags? */
1691 * This can still happen, when, for example, we receive an RA
1692 * containing a prefix with the L bit set and the A bit clear,
1693 * after removing all IPv6 addresses on the receiving
1694 * interface. This should, of course, be rare though.
1697 "nd6_prefix_onlink: failed to find any ifaddr"
1698 " to add route for a prefix(%s/%d) on %s\n",
1699 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1700 pr->ndpr_plen, if_name(ifp)));
1704 error = nd6_prefix_onlink_rtrequest(pr, ifa);
1713 nd6_prefix_offlink(struct nd_prefix *pr)
1716 struct ifnet *ifp = pr->ndpr_ifp;
1717 struct nd_prefix *opr;
1718 struct sockaddr_in6 sa6, mask6;
1720 char ip6buf[INET6_ADDRSTRLEN];
1721 int fibnum, a_failure;
1724 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1726 "nd6_prefix_offlink: %s/%d is already off-link\n",
1727 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1732 bzero(&sa6, sizeof(sa6));
1733 sa6.sin6_family = AF_INET6;
1734 sa6.sin6_len = sizeof(sa6);
1735 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1736 sizeof(struct in6_addr));
1737 bzero(&mask6, sizeof(mask6));
1738 mask6.sin6_family = AF_INET6;
1739 mask6.sin6_len = sizeof(sa6);
1740 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1743 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1745 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1746 (struct sockaddr *)&mask6, 0, &rt, fibnum);
1748 /* report the route deletion to the routing socket. */
1750 nd6_rtmsg(RTM_DELETE, rt);
1752 /* Save last error to return, see rtinit(). */
1761 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1764 * There might be the same prefix on another interface,
1765 * the prefix which could not be on-link just because we have
1766 * the interface route (see comments in nd6_prefix_onlink).
1767 * If there's one, try to make the prefix on-link on the
1770 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1774 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1778 * KAME specific: detached prefixes should not be
1781 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1784 if (opr->ndpr_plen == pr->ndpr_plen &&
1785 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1786 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1789 if ((e = nd6_prefix_onlink(opr)) != 0) {
1791 "nd6_prefix_offlink: failed to "
1792 "recover a prefix %s/%d from %s "
1793 "to %s (errno = %d)\n",
1795 &opr->ndpr_prefix.sin6_addr),
1796 opr->ndpr_plen, if_name(ifp),
1797 if_name(opr->ndpr_ifp), e));
1802 /* XXX: can we still set the NDPRF_ONLINK flag? */
1804 "nd6_prefix_offlink: failed to delete route: "
1805 "%s/%d on %s (errno = %d)\n",
1806 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
1807 if_name(ifp), error));
1813 static struct in6_ifaddr *
1814 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1816 struct ifnet *ifp = pr->ndpr_ifp;
1818 struct in6_aliasreq ifra;
1819 struct in6_ifaddr *ia, *ib;
1821 struct in6_addr mask;
1822 int prefixlen = pr->ndpr_plen;
1824 char ip6buf[INET6_ADDRSTRLEN];
1826 in6_prefixlen2mask(&mask, prefixlen);
1829 * find a link-local address (will be interface ID).
1830 * Is it really mandatory? Theoretically, a global or a site-local
1831 * address can be configured without a link-local address, if we
1832 * have a unique interface identifier...
1834 * it is not mandatory to have a link-local address, we can generate
1835 * interface identifier on the fly. we do this because:
1836 * (1) it should be the easiest way to find interface identifier.
1837 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1838 * for multiple addresses on a single interface, and possible shortcut
1839 * of DAD. we omitted DAD for this reason in the past.
1840 * (3) a user can prevent autoconfiguration of global address
1841 * by removing link-local address by hand (this is partly because we
1842 * don't have other way to control the use of IPv6 on an interface.
1843 * this has been our design choice - cf. NRL's "ifconfig auto").
1844 * (4) it is easier to manage when an interface has addresses
1845 * with the same interface identifier, than to have multiple addresses
1846 * with different interface identifiers.
1848 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1850 ib = (struct in6_ifaddr *)ifa;
1854 /* prefixlen + ifidlen must be equal to 128 */
1855 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1856 if (prefixlen != plen0) {
1858 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1859 "(prefix=%d ifid=%d)\n",
1860 if_name(ifp), prefixlen, 128 - plen0));
1866 bzero(&ifra, sizeof(ifra));
1868 * in6_update_ifa() does not use ifra_name, but we accurately set it
1871 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1872 ifra.ifra_addr.sin6_family = AF_INET6;
1873 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1875 ifra.ifra_addr.sin6_addr = pr->ndpr_prefix.sin6_addr;
1876 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1877 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1878 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1879 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1882 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1883 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1884 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1885 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1886 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1887 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1888 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1889 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1892 /* new prefix mask. */
1893 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1894 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1895 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1896 sizeof(ifra.ifra_prefixmask.sin6_addr));
1899 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1900 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1902 /* XXX: scope zone ID? */
1904 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1907 * Make sure that we do not have this address already. This should
1908 * usually not happen, but we can still see this case, e.g., if we
1909 * have manually configured the exact address to be configured.
1911 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1912 &ifra.ifra_addr.sin6_addr);
1915 /* this should be rare enough to make an explicit log */
1916 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1917 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1922 * Allocate ifaddr structure, link into chain, etc.
1923 * If we are going to create a new address upon receiving a multicasted
1924 * RA, we need to impose a random delay before starting DAD.
1925 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1929 updateflags |= IN6_IFAUPDATE_DADDELAY;
1930 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1932 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1933 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1934 if_name(ifp), error));
1935 return (NULL); /* ifaddr must not have been allocated. */
1938 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1940 * XXXRW: Assumption of non-NULLness here might not be true with
1941 * fine-grained locking -- should we validate it? Or just return
1942 * earlier ifa rather than looking it up again?
1944 return (ia); /* this is always non-NULL and referenced. */
1948 * ia0 - corresponding public address
1951 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
1953 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1954 struct in6_ifaddr *newia, *ia;
1955 struct in6_aliasreq ifra;
1957 int trylimit = 3; /* XXX: adhoc value */
1959 u_int32_t randid[2];
1960 time_t vltime0, pltime0;
1962 bzero(&ifra, sizeof(ifra));
1963 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1964 ifra.ifra_addr = ia0->ia_addr;
1965 /* copy prefix mask */
1966 ifra.ifra_prefixmask = ia0->ia_prefixmask;
1967 /* clear the old IFID */
1968 for (i = 0; i < 4; i++) {
1969 ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
1970 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1974 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
1975 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
1976 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
1980 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1981 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1982 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1983 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1986 * in6_get_tmpifid() quite likely provided a unique interface ID.
1987 * However, we may still have a chance to see collision, because
1988 * there may be a time lag between generation of the ID and generation
1989 * of the address. So, we'll do one more sanity check.
1992 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1993 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1994 &ifra.ifra_addr.sin6_addr)) {
1995 if (trylimit-- == 0) {
1996 IN6_IFADDR_RUNLOCK();
1998 * Give up. Something strange should have
2001 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2002 "find a unique random IFID\n"));
2005 IN6_IFADDR_RUNLOCK();
2010 IN6_IFADDR_RUNLOCK();
2013 * The Valid Lifetime is the lower of the Valid Lifetime of the
2014 * public address or TEMP_VALID_LIFETIME.
2015 * The Preferred Lifetime is the lower of the Preferred Lifetime
2016 * of the public address or TEMP_PREFERRED_LIFETIME -
2019 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2020 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2021 (ia0->ia6_lifetime.ia6t_vltime -
2022 (time_second - ia0->ia6_updatetime));
2023 if (vltime0 > V_ip6_temp_valid_lifetime)
2024 vltime0 = V_ip6_temp_valid_lifetime;
2026 vltime0 = V_ip6_temp_valid_lifetime;
2027 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2028 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2029 (ia0->ia6_lifetime.ia6t_pltime -
2030 (time_second - ia0->ia6_updatetime));
2031 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2032 pltime0 = V_ip6_temp_preferred_lifetime -
2033 V_ip6_desync_factor;
2036 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2037 ifra.ifra_lifetime.ia6t_vltime = vltime0;
2038 ifra.ifra_lifetime.ia6t_pltime = pltime0;
2041 * A temporary address is created only if this calculated Preferred
2042 * Lifetime is greater than REGEN_ADVANCE time units.
2044 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2047 /* XXX: scope zone ID? */
2049 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2051 /* allocate ifaddr structure, link into chain, etc. */
2054 updateflags |= IN6_IFAUPDATE_DADDELAY;
2055 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2058 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2059 if (newia == NULL) { /* XXX: can it happen? */
2061 "in6_tmpifadd: ifa update succeeded, but we got "
2063 return (EINVAL); /* XXX */
2065 newia->ia6_ndpr = ia0->ia6_ndpr;
2066 newia->ia6_ndpr->ndpr_refcnt++;
2067 ifa_free(&newia->ia_ifa);
2070 * A newly added address might affect the status of other addresses.
2071 * XXX: when the temporary address is generated with a new public
2072 * address, the onlink check is redundant. However, it would be safe
2073 * to do the check explicitly everywhere a new address is generated,
2074 * and, in fact, we surely need the check when we create a new
2075 * temporary address due to deprecation of an old temporary address.
2077 pfxlist_onlink_check();
2083 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2085 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2086 ndpr->ndpr_preferred = 0;
2088 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
2089 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2090 ndpr->ndpr_expire = 0;
2092 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
2098 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
2100 /* init ia6t_expire */
2101 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2102 lt6->ia6t_expire = 0;
2104 lt6->ia6t_expire = time_second;
2105 lt6->ia6t_expire += lt6->ia6t_vltime;
2108 /* init ia6t_preferred */
2109 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2110 lt6->ia6t_preferred = 0;
2112 lt6->ia6t_preferred = time_second;
2113 lt6->ia6t_preferred += lt6->ia6t_pltime;
2118 * Delete all the routing table entries that use the specified gateway.
2119 * XXX: this function causes search through all entries of routing table, so
2120 * it shouldn't be called when acting as a router.
2123 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2125 struct radix_node_head *rnh;
2129 /* We'll care only link-local addresses */
2130 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
2135 /* XXX Do we really need to walk any but the default FIB? */
2136 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
2137 rnh = rt_tables_get_rnh(fibnum, AF_INET6);
2141 RADIX_NODE_HEAD_LOCK(rnh);
2142 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
2143 RADIX_NODE_HEAD_UNLOCK(rnh);
2149 rt6_deleteroute(struct radix_node *rn, void *arg)
2151 #define SIN6(s) ((struct sockaddr_in6 *)s)
2152 struct rtentry *rt = (struct rtentry *)rn;
2153 struct in6_addr *gate = (struct in6_addr *)arg;
2155 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2158 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2163 * Do not delete a static route.
2164 * XXX: this seems to be a bit ad-hoc. Should we consider the
2165 * 'cloned' bit instead?
2167 if ((rt->rt_flags & RTF_STATIC) != 0)
2171 * We delete only host route. This means, in particular, we don't
2172 * delete default route.
2174 if ((rt->rt_flags & RTF_HOST) == 0)
2177 return (in6_rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
2178 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum));
2183 nd6_setdefaultiface(int ifindex)
2187 if (ifindex < 0 || V_if_index < ifindex)
2189 if (ifindex != 0 && !ifnet_byindex(ifindex))
2192 if (V_nd6_defifindex != ifindex) {
2193 V_nd6_defifindex = ifindex;
2194 if (V_nd6_defifindex > 0)
2195 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2197 V_nd6_defifp = NULL;
2200 * Our current implementation assumes one-to-one maping between
2201 * interfaces and links, so it would be natural to use the
2202 * default interface as the default link.
2204 scope6_setdefault(V_nd6_defifp);