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/refcount.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
46 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/rmlock.h>
50 #include <sys/rwlock.h>
51 #include <sys/syslog.h>
52 #include <sys/queue.h>
55 #include <net/if_var.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <net/route_var.h>
60 #include <net/radix.h>
63 #include <netinet/in.h>
64 #include <net/if_llatbl.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/in6_ifattach.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet/icmp6.h>
71 #include <netinet6/scope6_var.h>
73 static int rtpref(struct nd_defrouter *);
74 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
75 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
77 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
78 static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
79 struct nd_defrouter *);
80 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
81 static void pfxrtr_del(struct nd_pfxrouter *);
82 static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *);
83 static void defrouter_delreq(struct nd_defrouter *);
84 static void nd6_rtmsg(int, struct rtentry *);
86 static int in6_init_prefix_ltimes(struct nd_prefix *);
87 static void in6_init_address_ltimes(struct nd_prefix *,
88 struct in6_addrlifetime *);
90 static int rt6_deleteroute(const struct rtentry *, 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];
134 * Accept RS only when V_ip6_forwarding=1 and the interface has
135 * no ND6_IFF_ACCEPT_RTADV.
137 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
141 if (ip6->ip6_hlim != 255) {
143 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
144 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
145 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
150 * Don't update the neighbor cache, if src = ::.
151 * This indicates that the src has no IP address assigned yet.
153 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
156 #ifndef PULLDOWN_TEST
157 IP6_EXTHDR_CHECK(m, off, icmp6len,);
158 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
160 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
162 ICMP6STAT_INC(icp6s_tooshort);
167 icmp6len -= sizeof(*nd_rs);
168 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
169 if (nd6_options(&ndopts) < 0) {
171 "nd6_rs_input: invalid ND option, ignored\n"));
172 /* nd6_options have incremented stats */
176 if (ndopts.nd_opts_src_lladdr) {
177 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
178 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
181 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
183 "nd6_rs_input: lladdrlen mismatch for %s "
184 "(if %d, RS packet %d)\n",
185 ip6_sprintf(ip6bufs, &saddr6),
186 ifp->if_addrlen, lladdrlen - 2));
190 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
197 ICMP6STAT_INC(icp6s_badrs);
202 * Receive Router Advertisement Message.
205 * TODO: on-link bit on prefix information
206 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
209 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
211 struct ifnet *ifp = m->m_pkthdr.rcvif;
212 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
213 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
214 struct nd_router_advert *nd_ra;
215 struct in6_addr saddr6 = ip6->ip6_src;
217 union nd_opts ndopts;
218 struct nd_defrouter *dr;
219 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
224 * We only accept RAs only when the per-interface flag
225 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
227 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
230 if (ip6->ip6_hlim != 255) {
232 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
233 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
234 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
238 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
240 "nd6_ra_input: src %s is not link-local\n",
241 ip6_sprintf(ip6bufs, &saddr6)));
245 #ifndef PULLDOWN_TEST
246 IP6_EXTHDR_CHECK(m, off, icmp6len,);
247 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
249 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
251 ICMP6STAT_INC(icp6s_tooshort);
256 icmp6len -= sizeof(*nd_ra);
257 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
258 if (nd6_options(&ndopts) < 0) {
260 "nd6_ra_input: invalid ND option, ignored\n"));
261 /* nd6_options have incremented stats */
266 struct nd_defrouter dr0;
267 u_int32_t advreachable = nd_ra->nd_ra_reachable;
269 /* remember if this is a multicasted advertisement */
270 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
273 bzero(&dr0, sizeof(dr0));
275 dr0.raflags = nd_ra->nd_ra_flags_reserved;
277 * Effectively-disable routes from RA messages when
278 * ND6_IFF_NO_RADR enabled on the receiving interface or
279 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
281 if (ndi->flags & ND6_IFF_NO_RADR)
283 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
286 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
287 dr0.expire = time_uptime + dr0.rtlifetime;
289 /* unspecified or not? (RFC 2461 6.3.4) */
291 advreachable = ntohl(advreachable);
292 if (advreachable <= MAX_REACHABLE_TIME &&
293 ndi->basereachable != advreachable) {
294 ndi->basereachable = advreachable;
295 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
296 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
299 if (nd_ra->nd_ra_retransmit)
300 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
301 if (nd_ra->nd_ra_curhoplimit) {
302 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
303 ndi->chlim = nd_ra->nd_ra_curhoplimit;
304 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
305 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
306 "%s on %s (current = %d, received = %d). "
307 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
308 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
311 dr = defrtrlist_update(&dr0);
317 if (ndopts.nd_opts_pi) {
318 struct nd_opt_hdr *pt;
319 struct nd_opt_prefix_info *pi = NULL;
320 struct nd_prefixctl pr;
322 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
323 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
324 pt = (struct nd_opt_hdr *)((caddr_t)pt +
325 (pt->nd_opt_len << 3))) {
326 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
328 pi = (struct nd_opt_prefix_info *)pt;
330 if (pi->nd_opt_pi_len != 4) {
332 "nd6_ra_input: invalid option "
333 "len %d for prefix information option, "
334 "ignored\n", pi->nd_opt_pi_len));
338 if (128 < pi->nd_opt_pi_prefix_len) {
340 "nd6_ra_input: invalid prefix "
341 "len %d for prefix information option, "
342 "ignored\n", pi->nd_opt_pi_prefix_len));
346 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
347 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
349 "nd6_ra_input: invalid prefix "
352 &pi->nd_opt_pi_prefix)));
356 bzero(&pr, sizeof(pr));
357 pr.ndpr_prefix.sin6_family = AF_INET6;
358 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
359 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
360 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
362 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
363 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
364 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
365 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
366 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
367 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
368 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
369 (void)prelist_update(&pr, dr, m, mcast);
380 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
384 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
387 if (mtu < IPV6_MMTU) {
388 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
389 "mtu=%lu sent from %s, ignoring\n",
390 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
395 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
396 ? ndi->maxmtu : ifp->if_mtu;
398 int change = (ndi->linkmtu != mtu);
401 if (change) /* in6_maxmtu may change */
404 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
405 "mtu=%lu sent from %s; "
406 "exceeds maxmtu %lu, ignoring\n",
407 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
414 * Source link layer address
420 if (ndopts.nd_opts_src_lladdr) {
421 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
422 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
425 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
427 "nd6_ra_input: lladdrlen mismatch for %s "
428 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
429 ifp->if_addrlen, lladdrlen - 2));
433 nd6_cache_lladdr(ifp, &saddr6, lladdr,
434 lladdrlen, ND_ROUTER_ADVERT, 0);
437 * Installing a link-layer address might change the state of the
438 * router's neighbor cache, which might also affect our on-link
439 * detection of adveritsed prefixes.
441 pfxlist_onlink_check();
449 ICMP6STAT_INC(icp6s_badra);
453 /* tell the change to user processes watching the routing socket. */
455 nd6_rtmsg(int cmd, struct rtentry *rt)
457 struct rt_addrinfo info;
461 bzero((caddr_t)&info, sizeof(info));
462 info.rti_info[RTAX_DST] = rt_key(rt);
463 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
464 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
468 ifa = TAILQ_FIRST(&ifp->if_addrhead);
469 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
471 IF_ADDR_RUNLOCK(ifp);
472 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
476 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
482 * default router list processing sub routines
486 defrouter_addreq(struct nd_defrouter *new)
488 struct sockaddr_in6 def, mask, gate;
489 struct rtentry *newrt = NULL;
492 bzero(&def, sizeof(def));
493 bzero(&mask, sizeof(mask));
494 bzero(&gate, sizeof(gate));
496 def.sin6_len = mask.sin6_len = gate.sin6_len =
497 sizeof(struct sockaddr_in6);
498 def.sin6_family = gate.sin6_family = AF_INET6;
499 gate.sin6_addr = new->rtaddr;
501 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
502 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
503 RTF_GATEWAY, &newrt, RT_DEFAULT_FIB);
505 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
512 struct nd_defrouter *
513 defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
515 struct nd_defrouter *dr;
518 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
519 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
526 struct nd_defrouter *
527 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
529 struct nd_defrouter *dr;
532 dr = defrouter_lookup_locked(addr, ifp);
538 defrouter_ref(struct nd_defrouter *dr)
541 refcount_acquire(&dr->refcnt);
545 defrouter_rele(struct nd_defrouter *dr)
548 if (refcount_release(&dr->refcnt))
553 * Remove the default route for a given router.
554 * This is just a subroutine function for defrouter_select(), and should
555 * not be called from anywhere else.
558 defrouter_delreq(struct nd_defrouter *dr)
560 struct sockaddr_in6 def, mask, gate;
561 struct rtentry *oldrt = NULL;
563 bzero(&def, sizeof(def));
564 bzero(&mask, sizeof(mask));
565 bzero(&gate, sizeof(gate));
567 def.sin6_len = mask.sin6_len = gate.sin6_len =
568 sizeof(struct sockaddr_in6);
569 def.sin6_family = gate.sin6_family = AF_INET6;
570 gate.sin6_addr = dr->rtaddr;
572 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
573 (struct sockaddr *)&gate,
574 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, RT_DEFAULT_FIB);
576 nd6_rtmsg(RTM_DELETE, oldrt);
584 * Remove all default routes from default router list.
587 defrouter_reset(void)
589 struct nd_defrouter *dr, **dra;
595 * We can't delete routes with the ND lock held, so make a copy of the
596 * current default router list and use that when deleting routes.
599 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
603 dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
606 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
614 for (i = 0; i < count && dra[i] != NULL; i++) {
615 defrouter_delreq(dra[i]);
616 defrouter_rele(dra[i]);
621 * XXX should we also nuke any default routers in the kernel, by
622 * going through them by rtalloc1()?
627 * Look up a matching default router list entry and remove it. Returns true if a
628 * matching entry was found, false otherwise.
631 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
633 struct nd_defrouter *dr;
636 dr = defrouter_lookup_locked(addr, ifp);
642 defrouter_unlink(dr, NULL);
650 * Remove a router from the global list and optionally stash it in a
651 * caller-supplied queue.
653 * The ND lock must be held.
656 defrouter_unlink(struct nd_defrouter *dr, struct nd_drhead *drq)
660 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
663 TAILQ_INSERT_TAIL(drq, dr, dr_entry);
667 defrouter_del(struct nd_defrouter *dr)
669 struct nd_defrouter *deldr = NULL;
670 struct nd_prefix *pr;
671 struct nd_pfxrouter *pfxrtr;
676 * Flush all the routing table entries that use the router
679 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
680 rt6_flush(&dr->rtaddr, dr->ifp);
684 defrouter_delreq(dr);
688 * Also delete all the pointers to the router in each prefix lists.
691 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
692 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
697 pfxlist_onlink_check();
700 * If the router is the primary one, choose a new one.
701 * Note that defrouter_select() will remove the current gateway
702 * from the routing table.
708 * Release the list reference.
714 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
715 * draft-ietf-ipngwg-router-selection:
716 * 1) Routers that are reachable or probably reachable should be preferred.
717 * If we have more than one (probably) reachable router, prefer ones
718 * with the highest router preference.
719 * 2) When no routers on the list are known to be reachable or
720 * probably reachable, routers SHOULD be selected in a round-robin
721 * fashion, regardless of router preference values.
722 * 3) If the Default Router List is empty, assume that all
723 * destinations are on-link.
725 * We assume nd_defrouter is sorted by router preference value.
726 * Since the code below covers both with and without router preference cases,
727 * we do not need to classify the cases by ifdef.
729 * At this moment, we do not try to install more than one default router,
730 * even when the multipath routing is available, because we're not sure about
731 * the benefits for stub hosts comparing to the risk of making the code
732 * complicated and the possibility of introducing bugs.
735 defrouter_select(void)
737 struct nd_defrouter *dr, *selected_dr, *installed_dr;
738 struct llentry *ln = NULL;
742 * Let's handle easy case (3) first:
743 * If default router list is empty, there's nothing to be done.
745 if (TAILQ_EMPTY(&V_nd_defrouter)) {
751 * Search for a (probably) reachable router from the list.
752 * We just pick up the first reachable one (if any), assuming that
753 * the ordering rule of the list described in defrtrlist_update().
755 selected_dr = installed_dr = NULL;
756 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
757 IF_AFDATA_RLOCK(dr->ifp);
758 if (selected_dr == NULL &&
759 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
760 ND6_IS_LLINFO_PROBREACH(ln)) {
762 defrouter_ref(selected_dr);
764 IF_AFDATA_RUNLOCK(dr->ifp);
771 if (installed_dr == NULL) {
773 defrouter_ref(installed_dr);
775 /* this should not happen. warn for diagnosis. */
777 "defrouter_select: more than one router is installed\n");
782 * If none of the default routers was found to be reachable,
783 * round-robin the list regardless of preference.
784 * Otherwise, if we have an installed router, check if the selected
785 * (reachable) router should really be preferred to the installed one.
786 * We only prefer the new router when the old one is not reachable
787 * or when the new one has a really higher preference value.
789 if (selected_dr == NULL) {
790 if (installed_dr == NULL ||
791 TAILQ_NEXT(installed_dr, dr_entry) == NULL)
792 selected_dr = TAILQ_FIRST(&V_nd_defrouter);
794 selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
795 defrouter_ref(selected_dr);
796 } else if (installed_dr != NULL) {
797 IF_AFDATA_RLOCK(installed_dr->ifp);
798 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
799 ND6_IS_LLINFO_PROBREACH(ln) &&
800 rtpref(selected_dr) <= rtpref(installed_dr)) {
801 defrouter_rele(selected_dr);
802 selected_dr = installed_dr;
804 IF_AFDATA_RUNLOCK(installed_dr->ifp);
811 * If the selected router is different than the installed one,
812 * remove the installed router and install the selected one.
813 * Note that the selected router is never NULL here.
815 if (installed_dr != selected_dr) {
816 if (installed_dr != NULL) {
817 defrouter_delreq(installed_dr);
818 defrouter_rele(installed_dr);
820 defrouter_addreq(selected_dr);
822 defrouter_rele(selected_dr);
826 * for default router selection
827 * regards router-preference field as a 2-bit signed integer
830 rtpref(struct nd_defrouter *dr)
832 switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
833 case ND_RA_FLAG_RTPREF_HIGH:
834 return (RTPREF_HIGH);
835 case ND_RA_FLAG_RTPREF_MEDIUM:
836 case ND_RA_FLAG_RTPREF_RSV:
837 return (RTPREF_MEDIUM);
838 case ND_RA_FLAG_RTPREF_LOW:
842 * This case should never happen. If it did, it would mean a
843 * serious bug of kernel internal. We thus always bark here.
844 * Or, can we even panic?
846 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
847 return (RTPREF_INVALID);
852 static struct nd_defrouter *
853 defrtrlist_update(struct nd_defrouter *new)
855 struct nd_defrouter *dr, *n;
860 if (new->rtlifetime == 0) {
861 defrouter_remove(&new->rtaddr, new->ifp);
868 dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
870 oldpref = rtpref(dr);
873 dr->raflags = new->raflags; /* XXX flag check */
874 dr->rtlifetime = new->rtlifetime;
875 dr->expire = new->expire;
878 * If the preference does not change, there's no need
879 * to sort the entries. Also make sure the selected
880 * router is still installed in the kernel.
882 if (dr->installed && rtpref(new) == oldpref) {
892 * The router needs to be reinserted into the default router
893 * list, so upgrade to a write lock. If that fails and the list
894 * has potentially changed while the lock was dropped, we'll
895 * redo the lookup with the write lock held.
899 if (!ND6_TRY_UPGRADE()) {
900 genid = V_nd6_list_genid;
903 if (genid != V_nd6_list_genid)
910 * The preferred router may have changed, so relocate this
913 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
916 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
921 memcpy(n, new, sizeof(*n));
922 /* Initialize with an extra reference for the caller. */
923 refcount_init(&n->refcnt, 2);
927 * Insert the new router in the Default Router List;
928 * The Default Router List should be in the descending order
929 * of router-preferece. Routers with the same preference are
930 * sorted in the arriving time order.
933 /* insert at the end of the group */
934 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
935 if (rtpref(n) > rtpref(dr))
939 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
941 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
950 static struct nd_pfxrouter *
951 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
953 struct nd_pfxrouter *search;
957 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
958 if (search->router == dr)
965 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
967 struct nd_pfxrouter *new;
973 if (pfxrtr_lookup(pr, dr) != NULL) {
979 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
986 if (pfxrtr_lookup(pr, dr) == NULL) {
987 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
990 /* We lost a race to add the reference. */
998 pfxlist_onlink_check();
1002 pfxrtr_del(struct nd_pfxrouter *pfr)
1007 LIST_REMOVE(pfr, pfr_entry);
1008 defrouter_rele(pfr->router);
1009 free(pfr, M_IP6NDP);
1012 static struct nd_prefix *
1013 nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1015 struct nd_prefix *search;
1019 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1020 if (key->ndpr_ifp == search->ndpr_ifp &&
1021 key->ndpr_plen == search->ndpr_plen &&
1022 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1023 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1024 nd6_prefix_ref(search);
1032 nd6_prefix_lookup(struct nd_prefixctl *key)
1034 struct nd_prefix *search;
1037 search = nd6_prefix_lookup_locked(key);
1043 nd6_prefix_ref(struct nd_prefix *pr)
1046 refcount_acquire(&pr->ndpr_refcnt);
1050 nd6_prefix_rele(struct nd_prefix *pr)
1053 if (refcount_release(&pr->ndpr_refcnt)) {
1054 KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1055 ("prefix %p has advertising routers", pr));
1061 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
1062 struct nd_prefix **newp)
1064 struct nd_prefix *new;
1065 char ip6buf[INET6_ADDRSTRLEN];
1068 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
1071 refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1072 new->ndpr_ifp = pr->ndpr_ifp;
1073 new->ndpr_prefix = pr->ndpr_prefix;
1074 new->ndpr_plen = pr->ndpr_plen;
1075 new->ndpr_vltime = pr->ndpr_vltime;
1076 new->ndpr_pltime = pr->ndpr_pltime;
1077 new->ndpr_flags = pr->ndpr_flags;
1078 if ((error = in6_init_prefix_ltimes(new)) != 0) {
1079 free(new, M_IP6NDP);
1082 new->ndpr_lastupdate = time_uptime;
1084 /* initialization */
1085 LIST_INIT(&new->ndpr_advrtrs);
1086 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
1087 /* make prefix in the canonical form */
1088 IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
1091 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1095 /* ND_OPT_PI_FLAG_ONLINK processing */
1096 if (new->ndpr_raf_onlink) {
1098 if ((error = nd6_prefix_onlink(new)) != 0) {
1099 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
1100 "the prefix %s/%d on-link on %s (errno=%d)\n",
1101 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1102 pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
1103 /* proceed anyway. XXX: is it correct? */
1105 ND6_ONLINK_UNLOCK();
1109 pfxrtr_add(new, dr);
1116 * Remove a prefix from the prefix list and optionally stash it in a
1117 * caller-provided list.
1119 * The ND6 lock must be held.
1122 nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1127 LIST_REMOVE(pr, ndpr_entry);
1130 LIST_INSERT_HEAD(list, pr, ndpr_entry);
1134 * Free an unlinked prefix, first marking it off-link if necessary.
1137 nd6_prefix_del(struct nd_prefix *pr)
1139 struct nd_pfxrouter *pfr, *next;
1141 char ip6buf[INET6_ADDRSTRLEN];
1143 KASSERT(pr->ndpr_addrcnt == 0,
1144 ("prefix %p has referencing addresses", pr));
1145 ND6_UNLOCK_ASSERT();
1148 * Though these flags are now meaningless, we'd rather keep the value
1149 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
1150 * when executing "ndp -p".
1152 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1154 if ((e = nd6_prefix_offlink(pr)) != 0) {
1156 "nd6_prefix_del: failed to make %s/%d offlink "
1157 "on %s, errno=%d\n",
1158 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1159 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1160 /* what should we do? */
1162 ND6_ONLINK_UNLOCK();
1165 /* Release references to routers that have advertised this prefix. */
1167 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1171 nd6_prefix_rele(pr);
1173 pfxlist_onlink_check();
1177 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
1178 struct mbuf *m, int mcast)
1180 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1182 struct ifnet *ifp = new->ndpr_ifp;
1183 struct nd_prefix *pr;
1186 struct in6_addrlifetime lt6_tmp;
1187 char ip6buf[INET6_ADDRSTRLEN];
1192 * Authenticity for NA consists authentication for
1193 * both IP header and IP datagrams, doesn't it ?
1195 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1196 auth = ((m->m_flags & M_AUTHIPHDR) &&
1197 (m->m_flags & M_AUTHIPDGM));
1201 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1203 * nd6_prefix_lookup() ensures that pr and new have the same
1204 * prefix on a same interface.
1208 * Update prefix information. Note that the on-link (L) bit
1209 * and the autonomous (A) bit should NOT be changed from 1
1212 if (new->ndpr_raf_onlink == 1)
1213 pr->ndpr_raf_onlink = 1;
1214 if (new->ndpr_raf_auto == 1)
1215 pr->ndpr_raf_auto = 1;
1216 if (new->ndpr_raf_onlink) {
1217 pr->ndpr_vltime = new->ndpr_vltime;
1218 pr->ndpr_pltime = new->ndpr_pltime;
1219 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1220 pr->ndpr_lastupdate = time_uptime;
1223 if (new->ndpr_raf_onlink &&
1224 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1226 if ((error = nd6_prefix_onlink(pr)) != 0) {
1228 "prelist_update: failed to make "
1229 "the prefix %s/%d on-link on %s "
1232 &pr->ndpr_prefix.sin6_addr),
1233 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1235 /* proceed anyway. XXX: is it correct? */
1237 ND6_ONLINK_UNLOCK();
1243 if (new->ndpr_vltime == 0)
1245 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1248 error = nd6_prelist_add(new, dr, &pr);
1250 nd6log((LOG_NOTICE, "prelist_update: "
1251 "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
1252 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1253 new->ndpr_plen, if_name(new->ndpr_ifp), error));
1254 goto end; /* we should just give up in this case. */
1258 * XXX: from the ND point of view, we can ignore a prefix
1259 * with the on-link bit being zero. However, we need a
1260 * prefix structure for references from autoconfigured
1261 * addresses. Thus, we explicitly make sure that the prefix
1262 * itself expires now.
1264 if (pr->ndpr_raf_onlink == 0) {
1265 pr->ndpr_vltime = 0;
1266 pr->ndpr_pltime = 0;
1267 in6_init_prefix_ltimes(pr);
1272 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1273 * Note that pr must be non NULL at this point.
1276 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1277 if (!new->ndpr_raf_auto)
1281 * 5.5.3 (b). the link-local prefix should have been ignored in
1285 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1286 if (new->ndpr_pltime > new->ndpr_vltime) {
1287 error = EINVAL; /* XXX: won't be used */
1292 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1293 * an address configured by stateless autoconfiguration already in the
1294 * list of addresses associated with the interface, and the Valid
1295 * Lifetime is not 0, form an address. We first check if we have
1296 * a matching prefix.
1297 * Note: we apply a clarification in rfc2462bis-02 here. We only
1298 * consider autoconfigured addresses while RFC2462 simply said
1302 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1303 struct in6_ifaddr *ifa6;
1304 u_int32_t remaininglifetime;
1306 if (ifa->ifa_addr->sa_family != AF_INET6)
1309 ifa6 = (struct in6_ifaddr *)ifa;
1312 * We only consider autoconfigured addresses as per rfc2462bis.
1314 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1318 * Spec is not clear here, but I believe we should concentrate
1319 * on unicast (i.e. not anycast) addresses.
1320 * XXX: other ia6_flags? detached or duplicated?
1322 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1326 * Ignore the address if it is not associated with a prefix
1327 * or is associated with a prefix that is different from this
1328 * one. (pr is never NULL here)
1330 if (ifa6->ia6_ndpr != pr)
1333 if (ia6_match == NULL) /* remember the first one */
1337 * An already autoconfigured address matched. Now that we
1338 * are sure there is at least one matched address, we can
1339 * proceed to 5.5.3. (e): update the lifetimes according to the
1340 * "two hours" rule and the privacy extension.
1341 * We apply some clarifications in rfc2462bis:
1342 * - use remaininglifetime instead of storedlifetime as a
1344 * - remove the dead code in the "two-hour" rule
1346 #define TWOHOUR (120*60)
1347 lt6_tmp = ifa6->ia6_lifetime;
1349 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1350 remaininglifetime = ND6_INFINITE_LIFETIME;
1351 else if (time_uptime - ifa6->ia6_updatetime >
1352 lt6_tmp.ia6t_vltime) {
1354 * The case of "invalid" address. We should usually
1355 * not see this case.
1357 remaininglifetime = 0;
1359 remaininglifetime = lt6_tmp.ia6t_vltime -
1360 (time_uptime - ifa6->ia6_updatetime);
1362 /* when not updating, keep the current stored lifetime. */
1363 lt6_tmp.ia6t_vltime = remaininglifetime;
1365 if (TWOHOUR < new->ndpr_vltime ||
1366 remaininglifetime < new->ndpr_vltime) {
1367 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1368 } else if (remaininglifetime <= TWOHOUR) {
1370 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1374 * new->ndpr_vltime <= TWOHOUR &&
1375 * TWOHOUR < remaininglifetime
1377 lt6_tmp.ia6t_vltime = TWOHOUR;
1380 /* The 2 hour rule is not imposed for preferred lifetime. */
1381 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1383 in6_init_address_ltimes(pr, <6_tmp);
1386 * We need to treat lifetimes for temporary addresses
1387 * differently, according to
1388 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1389 * we only update the lifetimes when they are in the maximum
1392 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1393 u_int32_t maxvltime, maxpltime;
1395 if (V_ip6_temp_valid_lifetime >
1396 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1397 V_ip6_desync_factor)) {
1398 maxvltime = V_ip6_temp_valid_lifetime -
1399 (time_uptime - ifa6->ia6_createtime) -
1400 V_ip6_desync_factor;
1403 if (V_ip6_temp_preferred_lifetime >
1404 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1405 V_ip6_desync_factor)) {
1406 maxpltime = V_ip6_temp_preferred_lifetime -
1407 (time_uptime - ifa6->ia6_createtime) -
1408 V_ip6_desync_factor;
1412 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1413 lt6_tmp.ia6t_vltime > maxvltime) {
1414 lt6_tmp.ia6t_vltime = maxvltime;
1416 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1417 lt6_tmp.ia6t_pltime > maxpltime) {
1418 lt6_tmp.ia6t_pltime = maxpltime;
1421 ifa6->ia6_lifetime = lt6_tmp;
1422 ifa6->ia6_updatetime = time_uptime;
1424 IF_ADDR_RUNLOCK(ifp);
1425 if (ia6_match == NULL && new->ndpr_vltime) {
1429 * 5.5.3 (d) (continued)
1430 * No address matched and the valid lifetime is non-zero.
1431 * Create a new address.
1435 * Prefix Length check:
1436 * If the sum of the prefix length and interface identifier
1437 * length does not equal 128 bits, the Prefix Information
1438 * option MUST be ignored. The length of the interface
1439 * identifier is defined in a separate link-type specific
1442 ifidlen = in6_if2idlen(ifp);
1444 /* this should not happen, so we always log it. */
1445 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1449 if (ifidlen + pr->ndpr_plen != 128) {
1451 "prelist_update: invalid prefixlen "
1452 "%d for %s, ignored\n",
1453 pr->ndpr_plen, if_name(ifp)));
1457 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1459 * note that we should use pr (not new) for reference.
1466 * When a new public address is created as described
1467 * in RFC2462, also create a new temporary address.
1470 * When an interface connects to a new link, a new
1471 * randomized interface identifier should be generated
1472 * immediately together with a new set of temporary
1473 * addresses. Thus, we specifiy 1 as the 2nd arg of
1476 if (V_ip6_use_tempaddr) {
1478 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1479 nd6log((LOG_NOTICE, "prelist_update: "
1480 "failed to create a temporary "
1481 "address, errno=%d\n",
1485 ifa_free(&ia6->ia_ifa);
1488 * A newly added address might affect the status
1489 * of other addresses, so we check and update it.
1490 * XXX: what if address duplication happens?
1492 pfxlist_onlink_check();
1494 /* just set an error. do not bark here. */
1495 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1501 nd6_prefix_rele(pr);
1506 * A supplement function used in the on-link detection below;
1507 * detect if a given prefix has a (probably) reachable advertising router.
1508 * XXX: lengthy function name...
1510 static struct nd_pfxrouter *
1511 find_pfxlist_reachable_router(struct nd_prefix *pr)
1513 struct nd_pfxrouter *pfxrtr;
1519 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1520 IF_AFDATA_RLOCK(pfxrtr->router->ifp);
1521 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1522 IF_AFDATA_RUNLOCK(pfxrtr->router->ifp);
1525 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1534 * Check if each prefix in the prefix list has at least one available router
1535 * that advertised the prefix (a router is "available" if its neighbor cache
1536 * entry is reachable or probably reachable).
1537 * If the check fails, the prefix may be off-link, because, for example,
1538 * we have moved from the network but the lifetime of the prefix has not
1539 * expired yet. So we should not use the prefix if there is another prefix
1540 * that has an available router.
1541 * But, if there is no prefix that has an available router, we still regard
1542 * all the prefixes as on-link. This is because we can't tell if all the
1543 * routers are simply dead or if we really moved from the network and there
1544 * is no router around us.
1547 pfxlist_onlink_check(void)
1549 struct nd_prefix *pr;
1550 struct in6_ifaddr *ifa;
1551 struct nd_defrouter *dr;
1552 struct nd_pfxrouter *pfxrtr = NULL;
1553 struct rm_priotracker in6_ifa_tracker;
1561 * Check if there is a prefix that has a reachable advertising
1564 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1565 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1570 * If we have no such prefix, check whether we still have a router
1571 * that does not advertise any prefixes.
1574 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1575 struct nd_prefix *pr0;
1577 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1578 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1585 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
1587 * There is at least one prefix that has a reachable router,
1588 * or at least a router which probably does not advertise
1589 * any prefixes. The latter would be the case when we move
1590 * to a new link where we have a router that does not provide
1591 * prefixes and we configure an address by hand.
1592 * Detach prefixes which have no reachable advertising
1593 * router, and attach other prefixes.
1595 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1596 /* XXX: a link-local prefix should never be detached */
1597 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1598 pr->ndpr_raf_onlink == 0 ||
1599 pr->ndpr_raf_auto == 0)
1602 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1603 find_pfxlist_reachable_router(pr) == NULL)
1604 pr->ndpr_stateflags |= NDPRF_DETACHED;
1605 else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1606 find_pfxlist_reachable_router(pr) != NULL)
1607 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1610 /* there is no prefix that has a reachable router */
1611 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1612 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1613 pr->ndpr_raf_onlink == 0 ||
1614 pr->ndpr_raf_auto == 0)
1616 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1621 * Remove each interface route associated with a (just) detached
1622 * prefix, and reinstall the interface route for a (just) attached
1623 * prefix. Note that all attempt of reinstallation does not
1624 * necessarily success, when a same prefix is shared among multiple
1625 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1626 * so we don't have to care about them.
1629 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1630 char ip6buf[INET6_ADDRSTRLEN];
1633 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1634 pr->ndpr_raf_onlink == 0 ||
1635 pr->ndpr_raf_auto == 0)
1638 flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1639 if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1640 genid = V_nd6_list_genid;
1642 if ((flags & NDPRF_ONLINK) != 0 &&
1643 (e = nd6_prefix_offlink(pr)) != 0) {
1645 "pfxlist_onlink_check: failed to "
1646 "make %s/%d offlink, errno=%d\n",
1648 &pr->ndpr_prefix.sin6_addr),
1650 } else if ((flags & NDPRF_ONLINK) == 0 &&
1651 (e = nd6_prefix_onlink(pr)) != 0) {
1653 "pfxlist_onlink_check: failed to "
1654 "make %s/%d onlink, errno=%d\n",
1656 &pr->ndpr_prefix.sin6_addr),
1660 if (genid != V_nd6_list_genid)
1666 * Changes on the prefix status might affect address status as well.
1667 * Make sure that all addresses derived from an attached prefix are
1668 * attached, and that all addresses derived from a detached prefix are
1669 * detached. Note, however, that a manually configured address should
1670 * always be attached.
1671 * The precise detection logic is same as the one for prefixes.
1673 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1674 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1675 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1678 if (ifa->ia6_ndpr == NULL) {
1680 * This can happen when we first configure the address
1681 * (i.e. the address exists, but the prefix does not).
1682 * XXX: complicated relationships...
1687 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1691 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1692 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1695 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1698 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1699 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1700 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1701 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1702 nd6_dad_start((struct ifaddr *)ifa, 0);
1705 ifa->ia6_flags |= IN6_IFF_DETACHED;
1709 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1710 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1713 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1714 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1715 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1716 /* Do we need a delay in this case? */
1717 nd6_dad_start((struct ifaddr *)ifa, 0);
1721 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1723 ND6_ONLINK_UNLOCK();
1727 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
1729 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1730 struct rib_head *rnh;
1732 struct sockaddr_in6 mask6;
1734 int error, a_failure, fibnum;
1737 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1738 * ifa->ifa_rtrequest = nd6_rtrequest;
1740 bzero(&mask6, sizeof(mask6));
1741 mask6.sin6_len = sizeof(mask6);
1742 mask6.sin6_addr = pr->ndpr_mask;
1743 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1746 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1749 error = in6_rtrequest(RTM_ADD,
1750 (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr,
1751 (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
1753 KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
1754 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
1757 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
1758 /* XXX what if rhn == NULL? */
1761 if (rt_setgate(rt, rt_key(rt),
1762 (struct sockaddr *)&null_sdl) == 0) {
1763 struct sockaddr_dl *dl;
1765 dl = (struct sockaddr_dl *)rt->rt_gateway;
1766 dl->sdl_type = rt->rt_ifp->if_type;
1767 dl->sdl_index = rt->rt_ifp->if_index;
1770 nd6_rtmsg(RTM_ADD, rt);
1772 pr->ndpr_stateflags |= NDPRF_ONLINK;
1774 char ip6buf[INET6_ADDRSTRLEN];
1775 char ip6bufg[INET6_ADDRSTRLEN];
1776 char ip6bufm[INET6_ADDRSTRLEN];
1777 struct sockaddr_in6 *sin6;
1779 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1780 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
1781 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
1782 "flags=%lx errno = %d\n",
1783 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1784 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1785 ip6_sprintf(ip6bufg, &sin6->sin6_addr),
1786 ip6_sprintf(ip6bufm, &mask6.sin6_addr),
1789 /* Save last error to return, see rtinit(). */
1800 /* Return the last error we got. */
1805 nd6_prefix_onlink(struct nd_prefix *pr)
1808 struct ifnet *ifp = pr->ndpr_ifp;
1809 struct nd_prefix *opr;
1810 char ip6buf[INET6_ADDRSTRLEN];
1813 ND6_ONLINK_LOCK_ASSERT();
1814 ND6_UNLOCK_ASSERT();
1816 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1820 * Add the interface route associated with the prefix. Before
1821 * installing the route, check if there's the same prefix on another
1822 * interface, and the prefix has already installed the interface route.
1823 * Although such a configuration is expected to be rare, we explicitly
1827 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1831 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1834 if (opr->ndpr_plen == pr->ndpr_plen &&
1835 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1836 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1844 * We prefer link-local addresses as the associated interface address.
1846 /* search for a link-local addr */
1847 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1848 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1850 /* XXX: freebsd does not have ifa_ifwithaf */
1852 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1853 if (ifa->ifa_addr->sa_family == AF_INET6) {
1858 IF_ADDR_RUNLOCK(ifp);
1859 /* should we care about ia6_flags? */
1863 * This can still happen, when, for example, we receive an RA
1864 * containing a prefix with the L bit set and the A bit clear,
1865 * after removing all IPv6 addresses on the receiving
1866 * interface. This should, of course, be rare though.
1869 "nd6_prefix_onlink: failed to find any ifaddr"
1870 " to add route for a prefix(%s/%d) on %s\n",
1871 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1872 pr->ndpr_plen, if_name(ifp)));
1876 error = nd6_prefix_onlink_rtrequest(pr, ifa);
1885 nd6_prefix_offlink(struct nd_prefix *pr)
1888 struct ifnet *ifp = pr->ndpr_ifp;
1889 struct nd_prefix *opr;
1890 struct sockaddr_in6 sa6, mask6;
1892 char ip6buf[INET6_ADDRSTRLEN];
1894 int fibnum, a_failure;
1896 ND6_ONLINK_LOCK_ASSERT();
1897 ND6_UNLOCK_ASSERT();
1899 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1902 bzero(&sa6, sizeof(sa6));
1903 sa6.sin6_family = AF_INET6;
1904 sa6.sin6_len = sizeof(sa6);
1905 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1906 sizeof(struct in6_addr));
1907 bzero(&mask6, sizeof(mask6));
1908 mask6.sin6_family = AF_INET6;
1909 mask6.sin6_len = sizeof(sa6);
1910 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1913 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1915 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1916 (struct sockaddr *)&mask6, 0, &rt, fibnum);
1918 /* report the route deletion to the routing socket. */
1920 nd6_rtmsg(RTM_DELETE, rt);
1922 /* Save last error to return, see rtinit(). */
1932 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1935 * There might be the same prefix on another interface,
1936 * the prefix which could not be on-link just because we have
1937 * the interface route (see comments in nd6_prefix_onlink).
1938 * If there's one, try to make the prefix on-link on the
1943 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1945 * KAME specific: detached prefixes should not be
1948 if (opr == pr || (opr->ndpr_stateflags &
1949 (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
1952 if (opr->ndpr_plen == pr->ndpr_plen &&
1953 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1954 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1957 genid = V_nd6_list_genid;
1959 if ((e = nd6_prefix_onlink(opr)) != 0) {
1961 "nd6_prefix_offlink: failed to "
1962 "recover a prefix %s/%d from %s "
1963 "to %s (errno = %d)\n",
1965 &opr->ndpr_prefix.sin6_addr),
1966 opr->ndpr_plen, if_name(ifp),
1967 if_name(opr->ndpr_ifp), e));
1971 if (genid != V_nd6_list_genid)
1977 /* XXX: can we still set the NDPRF_ONLINK flag? */
1979 "nd6_prefix_offlink: failed to delete route: "
1980 "%s/%d on %s (errno = %d)\n",
1981 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
1982 if_name(ifp), error));
1986 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
1987 (struct sockaddr *)&mask6, LLE_STATIC);
1992 static struct in6_ifaddr *
1993 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1995 struct ifnet *ifp = pr->ndpr_ifp;
1997 struct in6_aliasreq ifra;
1998 struct in6_ifaddr *ia, *ib;
2000 struct in6_addr mask;
2001 int prefixlen = pr->ndpr_plen;
2003 char ip6buf[INET6_ADDRSTRLEN];
2005 in6_prefixlen2mask(&mask, prefixlen);
2008 * find a link-local address (will be interface ID).
2009 * Is it really mandatory? Theoretically, a global or a site-local
2010 * address can be configured without a link-local address, if we
2011 * have a unique interface identifier...
2013 * it is not mandatory to have a link-local address, we can generate
2014 * interface identifier on the fly. we do this because:
2015 * (1) it should be the easiest way to find interface identifier.
2016 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
2017 * for multiple addresses on a single interface, and possible shortcut
2018 * of DAD. we omitted DAD for this reason in the past.
2019 * (3) a user can prevent autoconfiguration of global address
2020 * by removing link-local address by hand (this is partly because we
2021 * don't have other way to control the use of IPv6 on an interface.
2022 * this has been our design choice - cf. NRL's "ifconfig auto").
2023 * (4) it is easier to manage when an interface has addresses
2024 * with the same interface identifier, than to have multiple addresses
2025 * with different interface identifiers.
2027 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
2029 ib = (struct in6_ifaddr *)ifa;
2033 /* prefixlen + ifidlen must be equal to 128 */
2034 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
2035 if (prefixlen != plen0) {
2037 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
2038 "(prefix=%d ifid=%d)\n",
2039 if_name(ifp), prefixlen, 128 - plen0));
2044 in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
2046 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
2048 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
2049 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
2050 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
2051 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
2052 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2053 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
2054 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2055 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
2059 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
2060 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
2062 /* XXX: scope zone ID? */
2064 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
2067 * Make sure that we do not have this address already. This should
2068 * usually not happen, but we can still see this case, e.g., if we
2069 * have manually configured the exact address to be configured.
2071 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
2072 &ifra.ifra_addr.sin6_addr);
2075 /* this should be rare enough to make an explicit log */
2076 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
2077 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
2082 * Allocate ifaddr structure, link into chain, etc.
2083 * If we are going to create a new address upon receiving a multicasted
2084 * RA, we need to impose a random delay before starting DAD.
2085 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
2089 updateflags |= IN6_IFAUPDATE_DADDELAY;
2090 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
2092 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
2093 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
2094 if_name(ifp), error));
2095 return (NULL); /* ifaddr must not have been allocated. */
2098 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2100 * XXXRW: Assumption of non-NULLness here might not be true with
2101 * fine-grained locking -- should we validate it? Or just return
2102 * earlier ifa rather than looking it up again?
2104 return (ia); /* this is always non-NULL and referenced. */
2108 * ia0 - corresponding public address
2111 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
2113 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
2114 struct in6_ifaddr *newia;
2115 struct in6_aliasreq ifra;
2117 int trylimit = 3; /* XXX: adhoc value */
2119 u_int32_t randid[2];
2120 time_t vltime0, pltime0;
2122 in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
2123 &ia0->ia_prefixmask.sin6_addr);
2125 ifra.ifra_addr = ia0->ia_addr; /* XXX: do we need this ? */
2126 /* clear the old IFID */
2127 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
2128 &ifra.ifra_prefixmask.sin6_addr);
2131 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2132 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2133 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
2137 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2138 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
2139 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2140 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
2143 * in6_get_tmpifid() quite likely provided a unique interface ID.
2144 * However, we may still have a chance to see collision, because
2145 * there may be a time lag between generation of the ID and generation
2146 * of the address. So, we'll do one more sanity check.
2149 if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
2150 if (trylimit-- > 0) {
2155 /* Give up. Something strange should have happened. */
2156 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2157 "find a unique random IFID\n"));
2162 * The Valid Lifetime is the lower of the Valid Lifetime of the
2163 * public address or TEMP_VALID_LIFETIME.
2164 * The Preferred Lifetime is the lower of the Preferred Lifetime
2165 * of the public address or TEMP_PREFERRED_LIFETIME -
2168 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2169 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2170 (ia0->ia6_lifetime.ia6t_vltime -
2171 (time_uptime - ia0->ia6_updatetime));
2172 if (vltime0 > V_ip6_temp_valid_lifetime)
2173 vltime0 = V_ip6_temp_valid_lifetime;
2175 vltime0 = V_ip6_temp_valid_lifetime;
2176 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2177 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2178 (ia0->ia6_lifetime.ia6t_pltime -
2179 (time_uptime - ia0->ia6_updatetime));
2180 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2181 pltime0 = V_ip6_temp_preferred_lifetime -
2182 V_ip6_desync_factor;
2185 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2186 ifra.ifra_lifetime.ia6t_vltime = vltime0;
2187 ifra.ifra_lifetime.ia6t_pltime = pltime0;
2190 * A temporary address is created only if this calculated Preferred
2191 * Lifetime is greater than REGEN_ADVANCE time units.
2193 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2196 /* XXX: scope zone ID? */
2198 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2200 /* allocate ifaddr structure, link into chain, etc. */
2203 updateflags |= IN6_IFAUPDATE_DADDELAY;
2204 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2207 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2208 if (newia == NULL) { /* XXX: can it happen? */
2210 "in6_tmpifadd: ifa update succeeded, but we got "
2212 return (EINVAL); /* XXX */
2214 newia->ia6_ndpr = ia0->ia6_ndpr;
2215 newia->ia6_ndpr->ndpr_addrcnt++;
2216 ifa_free(&newia->ia_ifa);
2219 * A newly added address might affect the status of other addresses.
2220 * XXX: when the temporary address is generated with a new public
2221 * address, the onlink check is redundant. However, it would be safe
2222 * to do the check explicitly everywhere a new address is generated,
2223 * and, in fact, we surely need the check when we create a new
2224 * temporary address due to deprecation of an old temporary address.
2226 pfxlist_onlink_check();
2232 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2234 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2235 ndpr->ndpr_preferred = 0;
2237 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
2238 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2239 ndpr->ndpr_expire = 0;
2241 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
2247 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
2249 /* init ia6t_expire */
2250 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2251 lt6->ia6t_expire = 0;
2253 lt6->ia6t_expire = time_uptime;
2254 lt6->ia6t_expire += lt6->ia6t_vltime;
2257 /* init ia6t_preferred */
2258 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2259 lt6->ia6t_preferred = 0;
2261 lt6->ia6t_preferred = time_uptime;
2262 lt6->ia6t_preferred += lt6->ia6t_pltime;
2267 * Delete all the routing table entries that use the specified gateway.
2268 * XXX: this function causes search through all entries of routing table, so
2269 * it shouldn't be called when acting as a router.
2272 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2275 /* We'll care only link-local addresses */
2276 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2279 /* XXX Do we really need to walk any but the default FIB? */
2280 rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2284 rt6_deleteroute(const struct rtentry *rt, void *arg)
2286 #define SIN6(s) ((struct sockaddr_in6 *)s)
2287 struct in6_addr *gate = (struct in6_addr *)arg;
2289 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2292 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2297 * Do not delete a static route.
2298 * XXX: this seems to be a bit ad-hoc. Should we consider the
2299 * 'cloned' bit instead?
2301 if ((rt->rt_flags & RTF_STATIC) != 0)
2305 * We delete only host route. This means, in particular, we don't
2306 * delete default route.
2308 if ((rt->rt_flags & RTF_HOST) == 0)
2316 nd6_setdefaultiface(int ifindex)
2320 if (ifindex < 0 || V_if_index < ifindex)
2322 if (ifindex != 0 && !ifnet_byindex(ifindex))
2325 if (V_nd6_defifindex != ifindex) {
2326 V_nd6_defifindex = ifindex;
2327 if (V_nd6_defifindex > 0)
2328 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2330 V_nd6_defifp = NULL;
2333 * Our current implementation assumes one-to-one maping between
2334 * interfaces and links, so it would be natural to use the
2335 * default interface as the default link.
2337 scope6_setdefault(V_nd6_defifp);