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/rwlock.h>
50 #include <sys/syslog.h>
51 #include <sys/queue.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <net/radix.h>
60 #include <netinet/in.h>
61 #include <net/if_llatbl.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet6/in6_ifattach.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet/icmp6.h>
68 #include <netinet6/scope6_var.h>
70 static int rtpref(struct nd_defrouter *);
71 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
72 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
74 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
75 static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
76 struct nd_defrouter *);
77 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
78 static void pfxrtr_del(struct nd_pfxrouter *);
79 static struct nd_pfxrouter *find_pfxlist_reachable_router
81 static void defrouter_delreq(struct nd_defrouter *);
82 static void nd6_rtmsg(int, struct rtentry *);
84 static int in6_init_prefix_ltimes(struct nd_prefix *);
85 static void in6_init_address_ltimes(struct nd_prefix *,
86 struct in6_addrlifetime *);
88 static int nd6_prefix_onlink(struct nd_prefix *);
89 static int nd6_prefix_offlink(struct nd_prefix *);
91 static int rt6_deleteroute(struct radix_node *, void *);
93 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
94 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
96 static VNET_DEFINE(struct ifnet *, nd6_defifp);
97 VNET_DEFINE(int, nd6_defifindex);
98 #define V_nd6_defifp VNET(nd6_defifp)
100 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
102 VNET_DEFINE(int, ip6_desync_factor);
103 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
104 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
106 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
108 /* RTPREF_MEDIUM has to be 0! */
109 #define RTPREF_HIGH 1
110 #define RTPREF_MEDIUM 0
111 #define RTPREF_LOW (-1)
112 #define RTPREF_RESERVED (-2)
113 #define RTPREF_INVALID (-3) /* internal */
116 * Receive Router Solicitation Message - just for routers.
117 * Router solicitation/advertisement is mostly managed by userland program
118 * (rtadvd) so here we have no function like nd6_ra_output().
123 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
125 struct ifnet *ifp = m->m_pkthdr.rcvif;
126 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
127 struct nd_router_solicit *nd_rs;
128 struct in6_addr saddr6 = ip6->ip6_src;
131 union nd_opts ndopts;
132 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
135 * Accept RS only when V_ip6_forwarding=1 and the interface has
136 * no ND6_IFF_ACCEPT_RTADV.
138 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
142 if (ip6->ip6_hlim != 255) {
144 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
145 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
146 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
151 * Don't update the neighbor cache, if src = ::.
152 * This indicates that the src has no IP address assigned yet.
154 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
157 #ifndef PULLDOWN_TEST
158 IP6_EXTHDR_CHECK(m, off, icmp6len,);
159 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
161 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
163 ICMP6STAT_INC(icp6s_tooshort);
168 icmp6len -= sizeof(*nd_rs);
169 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
170 if (nd6_options(&ndopts) < 0) {
172 "nd6_rs_input: invalid ND option, ignored\n"));
173 /* nd6_options have incremented stats */
177 if (ndopts.nd_opts_src_lladdr) {
178 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
179 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
182 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
184 "nd6_rs_input: lladdrlen mismatch for %s "
185 "(if %d, RS packet %d)\n",
186 ip6_sprintf(ip6bufs, &saddr6),
187 ifp->if_addrlen, lladdrlen - 2));
191 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
198 ICMP6STAT_INC(icp6s_badrs);
203 * Receive Router Advertisement Message.
206 * TODO: on-link bit on prefix information
207 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
210 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
212 struct ifnet *ifp = m->m_pkthdr.rcvif;
213 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
214 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
215 struct nd_router_advert *nd_ra;
216 struct in6_addr saddr6 = ip6->ip6_src;
218 union nd_opts ndopts;
219 struct nd_defrouter *dr;
220 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
225 * We only accept RAs only when the per-interface flag
226 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
228 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
231 if (ip6->ip6_hlim != 255) {
233 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
234 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
235 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
239 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
241 "nd6_ra_input: src %s is not link-local\n",
242 ip6_sprintf(ip6bufs, &saddr6)));
246 #ifndef PULLDOWN_TEST
247 IP6_EXTHDR_CHECK(m, off, icmp6len,);
248 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
250 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
252 ICMP6STAT_INC(icp6s_tooshort);
257 icmp6len -= sizeof(*nd_ra);
258 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
259 if (nd6_options(&ndopts) < 0) {
261 "nd6_ra_input: invalid ND option, ignored\n"));
262 /* nd6_options have incremented stats */
267 struct nd_defrouter dr0;
268 u_int32_t advreachable = nd_ra->nd_ra_reachable;
270 /* remember if this is a multicasted advertisement */
271 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
274 bzero(&dr0, sizeof(dr0));
276 dr0.raflags = nd_ra->nd_ra_flags_reserved;
278 * Effectively-disable routes from RA messages when
279 * ND6_IFF_NO_RADR enabled on the receiving interface or
280 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
282 if (ndi->flags & ND6_IFF_NO_RADR)
284 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
287 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
288 dr0.expire = time_uptime + dr0.rtlifetime;
290 /* unspecified or not? (RFC 2461 6.3.4) */
292 advreachable = ntohl(advreachable);
293 if (advreachable <= MAX_REACHABLE_TIME &&
294 ndi->basereachable != advreachable) {
295 ndi->basereachable = advreachable;
296 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
297 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
300 if (nd_ra->nd_ra_retransmit)
301 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
302 if (nd_ra->nd_ra_curhoplimit) {
303 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
304 ndi->chlim = nd_ra->nd_ra_curhoplimit;
305 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
306 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
307 "%s on %s (current = %d, received = %d). "
308 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
309 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
312 dr = defrtrlist_update(&dr0);
318 if (ndopts.nd_opts_pi) {
319 struct nd_opt_hdr *pt;
320 struct nd_opt_prefix_info *pi = NULL;
321 struct nd_prefixctl pr;
323 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
324 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
325 pt = (struct nd_opt_hdr *)((caddr_t)pt +
326 (pt->nd_opt_len << 3))) {
327 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
329 pi = (struct nd_opt_prefix_info *)pt;
331 if (pi->nd_opt_pi_len != 4) {
333 "nd6_ra_input: invalid option "
334 "len %d for prefix information option, "
335 "ignored\n", pi->nd_opt_pi_len));
339 if (128 < pi->nd_opt_pi_prefix_len) {
341 "nd6_ra_input: invalid prefix "
342 "len %d for prefix information option, "
343 "ignored\n", pi->nd_opt_pi_prefix_len));
347 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
348 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
350 "nd6_ra_input: invalid prefix "
353 &pi->nd_opt_pi_prefix)));
357 bzero(&pr, sizeof(pr));
358 pr.ndpr_prefix.sin6_family = AF_INET6;
359 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
360 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
361 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
363 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
364 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
365 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
366 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
367 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
368 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
369 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
370 (void)prelist_update(&pr, dr, m, mcast);
381 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
385 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
388 if (mtu < IPV6_MMTU) {
389 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
390 "mtu=%lu sent from %s, ignoring\n",
391 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
396 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
397 ? ndi->maxmtu : ifp->if_mtu;
399 int change = (ndi->linkmtu != mtu);
402 if (change) /* in6_maxmtu may change */
405 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
406 "mtu=%lu sent from %s; "
407 "exceeds maxmtu %lu, ignoring\n",
408 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
415 * Source link layer address
421 if (ndopts.nd_opts_src_lladdr) {
422 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
423 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
426 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
428 "nd6_ra_input: lladdrlen mismatch for %s "
429 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
430 ifp->if_addrlen, lladdrlen - 2));
434 nd6_cache_lladdr(ifp, &saddr6, lladdr,
435 lladdrlen, ND_ROUTER_ADVERT, 0);
438 * Installing a link-layer address might change the state of the
439 * router's neighbor cache, which might also affect our on-link
440 * detection of adveritsed prefixes.
442 pfxlist_onlink_check();
450 ICMP6STAT_INC(icp6s_badra);
454 /* tell the change to user processes watching the routing socket. */
456 nd6_rtmsg(int cmd, struct rtentry *rt)
458 struct rt_addrinfo info;
462 bzero((caddr_t)&info, sizeof(info));
463 info.rti_info[RTAX_DST] = rt_key(rt);
464 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
465 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
469 ifa = TAILQ_FIRST(&ifp->if_addrhead);
470 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
472 IF_ADDR_RUNLOCK(ifp);
473 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
477 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
483 * default router list proccessing sub routines
487 defrouter_addreq(struct nd_defrouter *new)
489 struct sockaddr_in6 def, mask, gate;
490 struct rtentry *newrt = NULL;
493 bzero(&def, sizeof(def));
494 bzero(&mask, sizeof(mask));
495 bzero(&gate, sizeof(gate));
497 def.sin6_len = mask.sin6_len = gate.sin6_len =
498 sizeof(struct sockaddr_in6);
499 def.sin6_family = gate.sin6_family = AF_INET6;
500 gate.sin6_addr = new->rtaddr;
502 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
503 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
504 RTF_GATEWAY, &newrt, RT_DEFAULT_FIB);
506 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
513 struct nd_defrouter *
514 defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
516 struct nd_defrouter *dr;
519 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
520 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
527 struct nd_defrouter *
528 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
530 struct nd_defrouter *dr;
533 dr = defrouter_lookup_locked(addr, ifp);
539 defrouter_ref(struct nd_defrouter *dr)
542 refcount_acquire(&dr->refcnt);
546 defrouter_rele(struct nd_defrouter *dr)
549 if (refcount_release(&dr->refcnt))
554 * Remove the default route for a given router.
555 * This is just a subroutine function for defrouter_select(), and should
556 * not be called from anywhere else.
559 defrouter_delreq(struct nd_defrouter *dr)
561 struct sockaddr_in6 def, mask, gate;
562 struct rtentry *oldrt = NULL;
564 bzero(&def, sizeof(def));
565 bzero(&mask, sizeof(mask));
566 bzero(&gate, sizeof(gate));
568 def.sin6_len = mask.sin6_len = gate.sin6_len =
569 sizeof(struct sockaddr_in6);
570 def.sin6_family = gate.sin6_family = AF_INET6;
571 gate.sin6_addr = dr->rtaddr;
573 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
574 (struct sockaddr *)&gate,
575 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, RT_DEFAULT_FIB);
577 nd6_rtmsg(RTM_DELETE, oldrt);
585 * Remove all default routes from default router list.
588 defrouter_reset(void)
590 struct nd_defrouter *dr, **dra;
596 * We can't delete routes with the ND lock held, so make a copy of the
597 * current default router list and use that when deleting routes.
600 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
604 dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
607 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
615 for (i = 0; i < count && dra[i] != NULL; i++) {
616 defrouter_delreq(dra[i]);
617 defrouter_rele(dra[i]);
622 * XXX should we also nuke any default routers in the kernel, by
623 * going through them by rtalloc1()?
628 * Look up a matching default router list entry and remove it. Returns true if a
629 * matching entry was found, false otherwise.
632 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
634 struct nd_defrouter *dr;
637 dr = defrouter_lookup_locked(addr, ifp);
643 defrouter_unlink(dr, NULL);
651 * Remove a router from the global list and optionally stash it in a
652 * caller-supplied queue.
654 * The ND lock must be held.
657 defrouter_unlink(struct nd_defrouter *dr, struct nd_drhead *drq)
661 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;
675 * Flush all the routing table entries that use the router
678 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
679 rt6_flush(&dr->rtaddr, dr->ifp);
683 defrouter_delreq(dr);
687 * Also delete all the pointers to the router in each prefix lists.
689 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
690 struct nd_pfxrouter *pfxrtr;
691 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
694 pfxlist_onlink_check();
697 * If the router is the primary one, choose a new one.
698 * Note that defrouter_select() will remove the current gateway
699 * from the routing table.
705 * Release the list reference.
711 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
712 * draft-ietf-ipngwg-router-selection:
713 * 1) Routers that are reachable or probably reachable should be preferred.
714 * If we have more than one (probably) reachable router, prefer ones
715 * with the highest router preference.
716 * 2) When no routers on the list are known to be reachable or
717 * probably reachable, routers SHOULD be selected in a round-robin
718 * fashion, regardless of router preference values.
719 * 3) If the Default Router List is empty, assume that all
720 * destinations are on-link.
722 * We assume nd_defrouter is sorted by router preference value.
723 * Since the code below covers both with and without router preference cases,
724 * we do not need to classify the cases by ifdef.
726 * At this moment, we do not try to install more than one default router,
727 * even when the multipath routing is available, because we're not sure about
728 * the benefits for stub hosts comparing to the risk of making the code
729 * complicated and the possibility of introducing bugs.
732 defrouter_select(void)
734 struct nd_defrouter *dr, *selected_dr, *installed_dr;
735 struct llentry *ln = NULL;
739 * Let's handle easy case (3) first:
740 * If default router list is empty, there's nothing to be done.
742 if (TAILQ_EMPTY(&V_nd_defrouter)) {
748 * Search for a (probably) reachable router from the list.
749 * We just pick up the first reachable one (if any), assuming that
750 * the ordering rule of the list described in defrtrlist_update().
752 selected_dr = installed_dr = NULL;
753 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
754 IF_AFDATA_RLOCK(dr->ifp);
755 if (selected_dr == NULL &&
756 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
757 ND6_IS_LLINFO_PROBREACH(ln)) {
759 defrouter_ref(selected_dr);
761 IF_AFDATA_RUNLOCK(dr->ifp);
768 if (installed_dr == NULL) {
770 defrouter_ref(installed_dr);
772 /* this should not happen. warn for diagnosis. */
774 "defrouter_select: more than one router is installed\n");
779 * If none of the default routers was found to be reachable,
780 * round-robin the list regardless of preference.
781 * Otherwise, if we have an installed router, check if the selected
782 * (reachable) router should really be preferred to the installed one.
783 * We only prefer the new router when the old one is not reachable
784 * or when the new one has a really higher preference value.
786 if (selected_dr == NULL) {
787 if (installed_dr == NULL ||
788 TAILQ_NEXT(installed_dr, dr_entry) == NULL)
789 selected_dr = TAILQ_FIRST(&V_nd_defrouter);
791 selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
792 defrouter_ref(selected_dr);
793 } else if (installed_dr != NULL) {
794 IF_AFDATA_RLOCK(installed_dr->ifp);
795 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
796 ND6_IS_LLINFO_PROBREACH(ln) &&
797 rtpref(selected_dr) <= rtpref(installed_dr)) {
798 defrouter_rele(selected_dr);
799 selected_dr = installed_dr;
801 IF_AFDATA_RUNLOCK(installed_dr->ifp);
808 * If the selected router is different than the installed one,
809 * remove the installed router and install the selected one.
810 * Note that the selected router is never NULL here.
812 if (installed_dr != selected_dr) {
813 if (installed_dr != NULL) {
814 defrouter_delreq(installed_dr);
815 defrouter_rele(installed_dr);
817 defrouter_addreq(selected_dr);
819 defrouter_rele(selected_dr);
823 * for default router selection
824 * regards router-preference field as a 2-bit signed integer
827 rtpref(struct nd_defrouter *dr)
829 switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
830 case ND_RA_FLAG_RTPREF_HIGH:
831 return (RTPREF_HIGH);
832 case ND_RA_FLAG_RTPREF_MEDIUM:
833 case ND_RA_FLAG_RTPREF_RSV:
834 return (RTPREF_MEDIUM);
835 case ND_RA_FLAG_RTPREF_LOW:
839 * This case should never happen. If it did, it would mean a
840 * serious bug of kernel internal. We thus always bark here.
841 * Or, can we even panic?
843 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
844 return (RTPREF_INVALID);
849 static struct nd_defrouter *
850 defrtrlist_update(struct nd_defrouter *new)
852 struct nd_defrouter *dr, *n;
855 if (new->rtlifetime == 0) {
856 defrouter_remove(&new->rtaddr, new->ifp);
861 dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
863 oldpref = rtpref(dr);
866 dr->raflags = new->raflags; /* XXX flag check */
867 dr->rtlifetime = new->rtlifetime;
868 dr->expire = new->expire;
871 * If the preference does not change, there's no need
872 * to sort the entries. Also make sure the selected
873 * router is still installed in the kernel.
875 if (dr->installed && rtpref(new) == oldpref) {
881 * The preferred router may have changed, so relocate this
884 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
887 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
892 memcpy(n, new, sizeof(*n));
893 /* Initialize with an extra reference for the caller. */
894 refcount_init(&n->refcnt, 2);
898 * Insert the new router in the Default Router List;
899 * The Default Router List should be in the descending order
900 * of router-preferece. Routers with the same preference are
901 * sorted in the arriving time order.
904 /* insert at the end of the group */
905 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
906 if (rtpref(n) > rtpref(dr))
910 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
912 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
920 static struct nd_pfxrouter *
921 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
923 struct nd_pfxrouter *search;
925 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
926 if (search->router == dr)
934 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
936 struct nd_pfxrouter *new;
938 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
944 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
946 pfxlist_onlink_check();
950 pfxrtr_del(struct nd_pfxrouter *pfr)
953 LIST_REMOVE(pfr, pfr_entry);
954 defrouter_rele(pfr->router);
959 nd6_prefix_lookup(struct nd_prefixctl *key)
961 struct nd_prefix *search;
963 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
964 if (key->ndpr_ifp == search->ndpr_ifp &&
965 key->ndpr_plen == search->ndpr_plen &&
966 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
967 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
976 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
977 struct nd_prefix **newp)
979 struct nd_prefix *new = NULL;
982 char ip6buf[INET6_ADDRSTRLEN];
984 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
987 new->ndpr_ifp = pr->ndpr_ifp;
988 new->ndpr_prefix = pr->ndpr_prefix;
989 new->ndpr_plen = pr->ndpr_plen;
990 new->ndpr_vltime = pr->ndpr_vltime;
991 new->ndpr_pltime = pr->ndpr_pltime;
992 new->ndpr_flags = pr->ndpr_flags;
993 if ((error = in6_init_prefix_ltimes(new)) != 0) {
997 new->ndpr_lastupdate = time_uptime;
1000 LIST_INIT(&new->ndpr_advrtrs);
1001 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
1002 /* make prefix in the canonical form */
1003 for (i = 0; i < 4; i++)
1004 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
1005 new->ndpr_mask.s6_addr32[i];
1007 /* link ndpr_entry to nd_prefix list */
1008 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1010 /* ND_OPT_PI_FLAG_ONLINK processing */
1011 if (new->ndpr_raf_onlink) {
1014 if ((e = nd6_prefix_onlink(new)) != 0) {
1015 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
1016 "the prefix %s/%d on-link on %s (errno=%d)\n",
1017 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1018 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1019 /* proceed anyway. XXX: is it correct? */
1024 pfxrtr_add(new, dr);
1031 prelist_remove(struct nd_prefix *pr)
1033 struct nd_pfxrouter *pfr, *next;
1035 char ip6buf[INET6_ADDRSTRLEN];
1037 /* make sure to invalidate the prefix until it is really freed. */
1038 pr->ndpr_vltime = 0;
1039 pr->ndpr_pltime = 0;
1042 * Though these flags are now meaningless, we'd rather keep the value
1043 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
1044 * when executing "ndp -p".
1047 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
1048 (e = nd6_prefix_offlink(pr)) != 0) {
1049 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
1050 "on %s, errno=%d\n",
1051 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1052 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1053 /* what should we do? */
1056 if (pr->ndpr_refcnt > 0)
1057 return; /* notice here? */
1059 /* unlink ndpr_entry from nd_prefix list */
1060 LIST_REMOVE(pr, ndpr_entry);
1062 /* free list of routers that advertised the prefix */
1063 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next) {
1068 pfxlist_onlink_check();
1076 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
1077 struct mbuf *m, int mcast)
1079 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1081 struct ifnet *ifp = new->ndpr_ifp;
1082 struct nd_prefix *pr;
1085 struct in6_addrlifetime lt6_tmp;
1086 char ip6buf[INET6_ADDRSTRLEN];
1091 * Authenticity for NA consists authentication for
1092 * both IP header and IP datagrams, doesn't it ?
1094 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1095 auth = ((m->m_flags & M_AUTHIPHDR) &&
1096 (m->m_flags & M_AUTHIPDGM));
1100 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1102 * nd6_prefix_lookup() ensures that pr and new have the same
1103 * prefix on a same interface.
1107 * Update prefix information. Note that the on-link (L) bit
1108 * and the autonomous (A) bit should NOT be changed from 1
1111 if (new->ndpr_raf_onlink == 1)
1112 pr->ndpr_raf_onlink = 1;
1113 if (new->ndpr_raf_auto == 1)
1114 pr->ndpr_raf_auto = 1;
1115 if (new->ndpr_raf_onlink) {
1116 pr->ndpr_vltime = new->ndpr_vltime;
1117 pr->ndpr_pltime = new->ndpr_pltime;
1118 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1119 pr->ndpr_lastupdate = time_uptime;
1122 if (new->ndpr_raf_onlink &&
1123 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1126 if ((e = nd6_prefix_onlink(pr)) != 0) {
1128 "prelist_update: failed to make "
1129 "the prefix %s/%d on-link on %s "
1132 &pr->ndpr_prefix.sin6_addr),
1133 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1134 /* proceed anyway. XXX: is it correct? */
1138 if (dr && pfxrtr_lookup(pr, dr) == NULL)
1141 if (new->ndpr_vltime == 0)
1143 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1146 error = nd6_prelist_add(new, dr, &pr);
1148 nd6log((LOG_NOTICE, "prelist_update: "
1149 "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
1150 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1151 new->ndpr_plen, if_name(new->ndpr_ifp), error));
1152 goto end; /* we should just give up in this case. */
1156 * XXX: from the ND point of view, we can ignore a prefix
1157 * with the on-link bit being zero. However, we need a
1158 * prefix structure for references from autoconfigured
1159 * addresses. Thus, we explicitly make sure that the prefix
1160 * itself expires now.
1162 if (pr->ndpr_raf_onlink == 0) {
1163 pr->ndpr_vltime = 0;
1164 pr->ndpr_pltime = 0;
1165 in6_init_prefix_ltimes(pr);
1170 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1171 * Note that pr must be non NULL at this point.
1174 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1175 if (!new->ndpr_raf_auto)
1179 * 5.5.3 (b). the link-local prefix should have been ignored in
1183 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1184 if (new->ndpr_pltime > new->ndpr_vltime) {
1185 error = EINVAL; /* XXX: won't be used */
1190 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1191 * an address configured by stateless autoconfiguration already in the
1192 * list of addresses associated with the interface, and the Valid
1193 * Lifetime is not 0, form an address. We first check if we have
1194 * a matching prefix.
1195 * Note: we apply a clarification in rfc2462bis-02 here. We only
1196 * consider autoconfigured addresses while RFC2462 simply said
1200 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1201 struct in6_ifaddr *ifa6;
1202 u_int32_t remaininglifetime;
1204 if (ifa->ifa_addr->sa_family != AF_INET6)
1207 ifa6 = (struct in6_ifaddr *)ifa;
1210 * We only consider autoconfigured addresses as per rfc2462bis.
1212 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1216 * Spec is not clear here, but I believe we should concentrate
1217 * on unicast (i.e. not anycast) addresses.
1218 * XXX: other ia6_flags? detached or duplicated?
1220 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1224 * Ignore the address if it is not associated with a prefix
1225 * or is associated with a prefix that is different from this
1226 * one. (pr is never NULL here)
1228 if (ifa6->ia6_ndpr != pr)
1231 if (ia6_match == NULL) /* remember the first one */
1235 * An already autoconfigured address matched. Now that we
1236 * are sure there is at least one matched address, we can
1237 * proceed to 5.5.3. (e): update the lifetimes according to the
1238 * "two hours" rule and the privacy extension.
1239 * We apply some clarifications in rfc2462bis:
1240 * - use remaininglifetime instead of storedlifetime as a
1242 * - remove the dead code in the "two-hour" rule
1244 #define TWOHOUR (120*60)
1245 lt6_tmp = ifa6->ia6_lifetime;
1247 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1248 remaininglifetime = ND6_INFINITE_LIFETIME;
1249 else if (time_uptime - ifa6->ia6_updatetime >
1250 lt6_tmp.ia6t_vltime) {
1252 * The case of "invalid" address. We should usually
1253 * not see this case.
1255 remaininglifetime = 0;
1257 remaininglifetime = lt6_tmp.ia6t_vltime -
1258 (time_uptime - ifa6->ia6_updatetime);
1260 /* when not updating, keep the current stored lifetime. */
1261 lt6_tmp.ia6t_vltime = remaininglifetime;
1263 if (TWOHOUR < new->ndpr_vltime ||
1264 remaininglifetime < new->ndpr_vltime) {
1265 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1266 } else if (remaininglifetime <= TWOHOUR) {
1268 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1272 * new->ndpr_vltime <= TWOHOUR &&
1273 * TWOHOUR < remaininglifetime
1275 lt6_tmp.ia6t_vltime = TWOHOUR;
1278 /* The 2 hour rule is not imposed for preferred lifetime. */
1279 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1281 in6_init_address_ltimes(pr, <6_tmp);
1284 * We need to treat lifetimes for temporary addresses
1285 * differently, according to
1286 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1287 * we only update the lifetimes when they are in the maximum
1290 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1291 u_int32_t maxvltime, maxpltime;
1293 if (V_ip6_temp_valid_lifetime >
1294 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1295 V_ip6_desync_factor)) {
1296 maxvltime = V_ip6_temp_valid_lifetime -
1297 (time_uptime - ifa6->ia6_createtime) -
1298 V_ip6_desync_factor;
1301 if (V_ip6_temp_preferred_lifetime >
1302 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1303 V_ip6_desync_factor)) {
1304 maxpltime = V_ip6_temp_preferred_lifetime -
1305 (time_uptime - ifa6->ia6_createtime) -
1306 V_ip6_desync_factor;
1310 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1311 lt6_tmp.ia6t_vltime > maxvltime) {
1312 lt6_tmp.ia6t_vltime = maxvltime;
1314 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1315 lt6_tmp.ia6t_pltime > maxpltime) {
1316 lt6_tmp.ia6t_pltime = maxpltime;
1319 ifa6->ia6_lifetime = lt6_tmp;
1320 ifa6->ia6_updatetime = time_uptime;
1322 IF_ADDR_RUNLOCK(ifp);
1323 if (ia6_match == NULL && new->ndpr_vltime) {
1327 * 5.5.3 (d) (continued)
1328 * No address matched and the valid lifetime is non-zero.
1329 * Create a new address.
1333 * Prefix Length check:
1334 * If the sum of the prefix length and interface identifier
1335 * length does not equal 128 bits, the Prefix Information
1336 * option MUST be ignored. The length of the interface
1337 * identifier is defined in a separate link-type specific
1340 ifidlen = in6_if2idlen(ifp);
1342 /* this should not happen, so we always log it. */
1343 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1347 if (ifidlen + pr->ndpr_plen != 128) {
1349 "prelist_update: invalid prefixlen "
1350 "%d for %s, ignored\n",
1351 pr->ndpr_plen, if_name(ifp)));
1355 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1357 * note that we should use pr (not new) for reference.
1364 * When a new public address is created as described
1365 * in RFC2462, also create a new temporary address.
1368 * When an interface connects to a new link, a new
1369 * randomized interface identifier should be generated
1370 * immediately together with a new set of temporary
1371 * addresses. Thus, we specifiy 1 as the 2nd arg of
1374 if (V_ip6_use_tempaddr) {
1376 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1377 nd6log((LOG_NOTICE, "prelist_update: "
1378 "failed to create a temporary "
1379 "address, errno=%d\n",
1383 ifa_free(&ia6->ia_ifa);
1386 * A newly added address might affect the status
1387 * of other addresses, so we check and update it.
1388 * XXX: what if address duplication happens?
1390 pfxlist_onlink_check();
1392 /* just set an error. do not bark here. */
1393 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1402 * A supplement function used in the on-link detection below;
1403 * detect if a given prefix has a (probably) reachable advertising router.
1404 * XXX: lengthy function name...
1406 static struct nd_pfxrouter *
1407 find_pfxlist_reachable_router(struct nd_prefix *pr)
1409 struct nd_pfxrouter *pfxrtr;
1413 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1414 IF_AFDATA_RLOCK(pfxrtr->router->ifp);
1415 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1416 IF_AFDATA_RUNLOCK(pfxrtr->router->ifp);
1419 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1428 * Check if each prefix in the prefix list has at least one available router
1429 * that advertised the prefix (a router is "available" if its neighbor cache
1430 * entry is reachable or probably reachable).
1431 * If the check fails, the prefix may be off-link, because, for example,
1432 * we have moved from the network but the lifetime of the prefix has not
1433 * expired yet. So we should not use the prefix if there is another prefix
1434 * that has an available router.
1435 * But, if there is no prefix that has an available router, we still regards
1436 * all the prefixes as on-link. This is because we can't tell if all the
1437 * routers are simply dead or if we really moved from the network and there
1438 * is no router around us.
1441 pfxlist_onlink_check()
1443 struct nd_prefix *pr;
1444 struct in6_ifaddr *ifa;
1445 struct nd_defrouter *dr;
1446 struct nd_pfxrouter *pfxrtr = NULL;
1449 * Check if there is a prefix that has a reachable advertising
1452 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1453 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1458 * If we have no such prefix, check whether we still have a router
1459 * that does not advertise any prefixes.
1463 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1464 struct nd_prefix *pr0;
1466 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1467 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1475 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
1477 * There is at least one prefix that has a reachable router,
1478 * or at least a router which probably does not advertise
1479 * any prefixes. The latter would be the case when we move
1480 * to a new link where we have a router that does not provide
1481 * prefixes and we configure an address by hand.
1482 * Detach prefixes which have no reachable advertising
1483 * router, and attach other prefixes.
1485 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1486 /* XXX: a link-local prefix should never be detached */
1487 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1491 * we aren't interested in prefixes without the L bit
1494 if (pr->ndpr_raf_onlink == 0)
1497 if (pr->ndpr_raf_auto == 0)
1500 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1501 find_pfxlist_reachable_router(pr) == NULL)
1502 pr->ndpr_stateflags |= NDPRF_DETACHED;
1503 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1504 find_pfxlist_reachable_router(pr) != 0)
1505 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1508 /* there is no prefix that has a reachable router */
1509 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1510 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1513 if (pr->ndpr_raf_onlink == 0)
1516 if (pr->ndpr_raf_auto == 0)
1519 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1520 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1525 * Remove each interface route associated with a (just) detached
1526 * prefix, and reinstall the interface route for a (just) attached
1527 * prefix. Note that all attempt of reinstallation does not
1528 * necessarily success, when a same prefix is shared among multiple
1529 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1530 * so we don't have to care about them.
1532 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1534 char ip6buf[INET6_ADDRSTRLEN];
1536 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1539 if (pr->ndpr_raf_onlink == 0)
1542 if (pr->ndpr_raf_auto == 0)
1545 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1546 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1547 if ((e = nd6_prefix_offlink(pr)) != 0) {
1549 "pfxlist_onlink_check: failed to "
1550 "make %s/%d offlink, errno=%d\n",
1552 &pr->ndpr_prefix.sin6_addr),
1556 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1557 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1558 pr->ndpr_raf_onlink) {
1559 if ((e = nd6_prefix_onlink(pr)) != 0) {
1561 "pfxlist_onlink_check: failed to "
1562 "make %s/%d onlink, errno=%d\n",
1564 &pr->ndpr_prefix.sin6_addr),
1571 * Changes on the prefix status might affect address status as well.
1572 * Make sure that all addresses derived from an attached prefix are
1573 * attached, and that all addresses derived from a detached prefix are
1574 * detached. Note, however, that a manually configured address should
1575 * always be attached.
1576 * The precise detection logic is same as the one for prefixes.
1578 * XXXRW: in6_ifaddrhead locking.
1580 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1581 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1584 if (ifa->ia6_ndpr == NULL) {
1586 * This can happen when we first configure the address
1587 * (i.e. the address exists, but the prefix does not).
1588 * XXX: complicated relationships...
1593 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1597 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1598 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1601 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1604 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1605 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1606 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1607 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1608 nd6_dad_start((struct ifaddr *)ifa, 0);
1611 ifa->ia6_flags |= IN6_IFF_DETACHED;
1616 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1617 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1620 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1621 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1622 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1623 /* Do we need a delay in this case? */
1624 nd6_dad_start((struct ifaddr *)ifa, 0);
1631 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
1633 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1634 struct radix_node_head *rnh;
1636 struct sockaddr_in6 mask6;
1638 int error, a_failure, fibnum;
1641 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1642 * ifa->ifa_rtrequest = nd6_rtrequest;
1644 bzero(&mask6, sizeof(mask6));
1645 mask6.sin6_len = sizeof(mask6);
1646 mask6.sin6_addr = pr->ndpr_mask;
1647 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1650 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1653 error = in6_rtrequest(RTM_ADD,
1654 (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr,
1655 (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
1657 KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
1658 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
1661 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
1662 /* XXX what if rhn == NULL? */
1663 RADIX_NODE_HEAD_LOCK(rnh);
1665 if (rt_setgate(rt, rt_key(rt),
1666 (struct sockaddr *)&null_sdl) == 0) {
1667 struct sockaddr_dl *dl;
1669 dl = (struct sockaddr_dl *)rt->rt_gateway;
1670 dl->sdl_type = rt->rt_ifp->if_type;
1671 dl->sdl_index = rt->rt_ifp->if_index;
1673 RADIX_NODE_HEAD_UNLOCK(rnh);
1674 nd6_rtmsg(RTM_ADD, rt);
1676 pr->ndpr_stateflags |= NDPRF_ONLINK;
1678 char ip6buf[INET6_ADDRSTRLEN];
1679 char ip6bufg[INET6_ADDRSTRLEN];
1680 char ip6bufm[INET6_ADDRSTRLEN];
1681 struct sockaddr_in6 *sin6;
1683 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1684 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
1685 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
1686 "flags=%lx errno = %d\n",
1687 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1688 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1689 ip6_sprintf(ip6bufg, &sin6->sin6_addr),
1690 ip6_sprintf(ip6bufm, &mask6.sin6_addr),
1693 /* Save last error to return, see rtinit(). */
1704 /* Return the last error we got. */
1709 nd6_prefix_onlink(struct nd_prefix *pr)
1712 struct ifnet *ifp = pr->ndpr_ifp;
1713 struct nd_prefix *opr;
1715 char ip6buf[INET6_ADDRSTRLEN];
1718 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1720 "nd6_prefix_onlink: %s/%d is already on-link\n",
1721 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1727 * Add the interface route associated with the prefix. Before
1728 * installing the route, check if there's the same prefix on another
1729 * interface, and the prefix has already installed the interface route.
1730 * Although such a configuration is expected to be rare, we explicitly
1733 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1737 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1740 if (opr->ndpr_plen == pr->ndpr_plen &&
1741 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1742 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1747 * We prefer link-local addresses as the associated interface address.
1749 /* search for a link-local addr */
1750 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1751 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1753 /* XXX: freebsd does not have ifa_ifwithaf */
1755 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1756 if (ifa->ifa_addr->sa_family == AF_INET6)
1761 IF_ADDR_RUNLOCK(ifp);
1762 /* should we care about ia6_flags? */
1766 * This can still happen, when, for example, we receive an RA
1767 * containing a prefix with the L bit set and the A bit clear,
1768 * after removing all IPv6 addresses on the receiving
1769 * interface. This should, of course, be rare though.
1772 "nd6_prefix_onlink: failed to find any ifaddr"
1773 " to add route for a prefix(%s/%d) on %s\n",
1774 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1775 pr->ndpr_plen, if_name(ifp)));
1779 error = nd6_prefix_onlink_rtrequest(pr, ifa);
1788 nd6_prefix_offlink(struct nd_prefix *pr)
1791 struct ifnet *ifp = pr->ndpr_ifp;
1792 struct nd_prefix *opr;
1793 struct sockaddr_in6 sa6, mask6;
1795 char ip6buf[INET6_ADDRSTRLEN];
1796 int fibnum, a_failure;
1799 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1801 "nd6_prefix_offlink: %s/%d is already off-link\n",
1802 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1807 bzero(&sa6, sizeof(sa6));
1808 sa6.sin6_family = AF_INET6;
1809 sa6.sin6_len = sizeof(sa6);
1810 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1811 sizeof(struct in6_addr));
1812 bzero(&mask6, sizeof(mask6));
1813 mask6.sin6_family = AF_INET6;
1814 mask6.sin6_len = sizeof(sa6);
1815 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1818 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1820 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1821 (struct sockaddr *)&mask6, 0, &rt, fibnum);
1823 /* report the route deletion to the routing socket. */
1825 nd6_rtmsg(RTM_DELETE, rt);
1827 /* Save last error to return, see rtinit(). */
1837 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1840 * There might be the same prefix on another interface,
1841 * the prefix which could not be on-link just because we have
1842 * the interface route (see comments in nd6_prefix_onlink).
1843 * If there's one, try to make the prefix on-link on the
1846 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1850 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1854 * KAME specific: detached prefixes should not be
1857 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1860 if (opr->ndpr_plen == pr->ndpr_plen &&
1861 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1862 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1865 if ((e = nd6_prefix_onlink(opr)) != 0) {
1867 "nd6_prefix_offlink: failed to "
1868 "recover a prefix %s/%d from %s "
1869 "to %s (errno = %d)\n",
1871 &opr->ndpr_prefix.sin6_addr),
1872 opr->ndpr_plen, if_name(ifp),
1873 if_name(opr->ndpr_ifp), e));
1879 /* XXX: can we still set the NDPRF_ONLINK flag? */
1881 "nd6_prefix_offlink: failed to delete route: "
1882 "%s/%d on %s (errno = %d)\n",
1883 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
1884 if_name(ifp), error));
1888 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
1889 (struct sockaddr *)&mask6, LLE_STATIC);
1894 static struct in6_ifaddr *
1895 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1897 struct ifnet *ifp = pr->ndpr_ifp;
1899 struct in6_aliasreq ifra;
1900 struct in6_ifaddr *ia, *ib;
1902 struct in6_addr mask;
1903 int prefixlen = pr->ndpr_plen;
1905 char ip6buf[INET6_ADDRSTRLEN];
1907 in6_prefixlen2mask(&mask, prefixlen);
1910 * find a link-local address (will be interface ID).
1911 * Is it really mandatory? Theoretically, a global or a site-local
1912 * address can be configured without a link-local address, if we
1913 * have a unique interface identifier...
1915 * it is not mandatory to have a link-local address, we can generate
1916 * interface identifier on the fly. we do this because:
1917 * (1) it should be the easiest way to find interface identifier.
1918 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1919 * for multiple addresses on a single interface, and possible shortcut
1920 * of DAD. we omitted DAD for this reason in the past.
1921 * (3) a user can prevent autoconfiguration of global address
1922 * by removing link-local address by hand (this is partly because we
1923 * don't have other way to control the use of IPv6 on an interface.
1924 * this has been our design choice - cf. NRL's "ifconfig auto").
1925 * (4) it is easier to manage when an interface has addresses
1926 * with the same interface identifier, than to have multiple addresses
1927 * with different interface identifiers.
1929 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1931 ib = (struct in6_ifaddr *)ifa;
1935 /* prefixlen + ifidlen must be equal to 128 */
1936 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1937 if (prefixlen != plen0) {
1939 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1940 "(prefix=%d ifid=%d)\n",
1941 if_name(ifp), prefixlen, 128 - plen0));
1947 bzero(&ifra, sizeof(ifra));
1949 * in6_update_ifa() does not use ifra_name, but we accurately set it
1952 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1953 ifra.ifra_addr.sin6_family = AF_INET6;
1954 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1956 ifra.ifra_addr.sin6_addr = pr->ndpr_prefix.sin6_addr;
1957 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1958 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1959 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1960 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1963 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1964 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1965 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1966 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1967 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1968 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1969 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1970 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1973 /* new prefix mask. */
1974 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1975 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1976 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1977 sizeof(ifra.ifra_prefixmask.sin6_addr));
1980 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1981 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1983 /* XXX: scope zone ID? */
1985 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1988 * Make sure that we do not have this address already. This should
1989 * usually not happen, but we can still see this case, e.g., if we
1990 * have manually configured the exact address to be configured.
1992 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1993 &ifra.ifra_addr.sin6_addr);
1996 /* this should be rare enough to make an explicit log */
1997 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1998 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
2003 * Allocate ifaddr structure, link into chain, etc.
2004 * If we are going to create a new address upon receiving a multicasted
2005 * RA, we need to impose a random delay before starting DAD.
2006 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
2010 updateflags |= IN6_IFAUPDATE_DADDELAY;
2011 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
2013 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
2014 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
2015 if_name(ifp), error));
2016 return (NULL); /* ifaddr must not have been allocated. */
2019 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2021 * XXXRW: Assumption of non-NULLness here might not be true with
2022 * fine-grained locking -- should we validate it? Or just return
2023 * earlier ifa rather than looking it up again?
2025 return (ia); /* this is always non-NULL and referenced. */
2029 * ia0 - corresponding public address
2032 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
2034 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
2035 struct in6_ifaddr *newia, *ia;
2036 struct in6_aliasreq ifra;
2038 int trylimit = 3; /* XXX: adhoc value */
2040 u_int32_t randid[2];
2041 time_t vltime0, pltime0;
2043 bzero(&ifra, sizeof(ifra));
2044 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
2045 ifra.ifra_addr = ia0->ia_addr;
2046 /* copy prefix mask */
2047 ifra.ifra_prefixmask = ia0->ia_prefixmask;
2048 /* clear the old IFID */
2049 for (i = 0; i < 4; i++) {
2050 ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
2051 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
2055 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2056 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2057 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
2061 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2062 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
2063 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2064 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
2067 * in6_get_tmpifid() quite likely provided a unique interface ID.
2068 * However, we may still have a chance to see collision, because
2069 * there may be a time lag between generation of the ID and generation
2070 * of the address. So, we'll do one more sanity check.
2073 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2074 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
2075 &ifra.ifra_addr.sin6_addr)) {
2076 if (trylimit-- == 0) {
2077 IN6_IFADDR_RUNLOCK();
2079 * Give up. Something strange should have
2082 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2083 "find a unique random IFID\n"));
2086 IN6_IFADDR_RUNLOCK();
2091 IN6_IFADDR_RUNLOCK();
2094 * The Valid Lifetime is the lower of the Valid Lifetime of the
2095 * public address or TEMP_VALID_LIFETIME.
2096 * The Preferred Lifetime is the lower of the Preferred Lifetime
2097 * of the public address or TEMP_PREFERRED_LIFETIME -
2100 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2101 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2102 (ia0->ia6_lifetime.ia6t_vltime -
2103 (time_uptime - ia0->ia6_updatetime));
2104 if (vltime0 > V_ip6_temp_valid_lifetime)
2105 vltime0 = V_ip6_temp_valid_lifetime;
2107 vltime0 = V_ip6_temp_valid_lifetime;
2108 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2109 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2110 (ia0->ia6_lifetime.ia6t_pltime -
2111 (time_uptime - ia0->ia6_updatetime));
2112 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2113 pltime0 = V_ip6_temp_preferred_lifetime -
2114 V_ip6_desync_factor;
2117 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2118 ifra.ifra_lifetime.ia6t_vltime = vltime0;
2119 ifra.ifra_lifetime.ia6t_pltime = pltime0;
2122 * A temporary address is created only if this calculated Preferred
2123 * Lifetime is greater than REGEN_ADVANCE time units.
2125 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2128 /* XXX: scope zone ID? */
2130 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2132 /* allocate ifaddr structure, link into chain, etc. */
2135 updateflags |= IN6_IFAUPDATE_DADDELAY;
2136 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2139 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2140 if (newia == NULL) { /* XXX: can it happen? */
2142 "in6_tmpifadd: ifa update succeeded, but we got "
2144 return (EINVAL); /* XXX */
2146 newia->ia6_ndpr = ia0->ia6_ndpr;
2147 newia->ia6_ndpr->ndpr_refcnt++;
2148 ifa_free(&newia->ia_ifa);
2151 * A newly added address might affect the status of other addresses.
2152 * XXX: when the temporary address is generated with a new public
2153 * address, the onlink check is redundant. However, it would be safe
2154 * to do the check explicitly everywhere a new address is generated,
2155 * and, in fact, we surely need the check when we create a new
2156 * temporary address due to deprecation of an old temporary address.
2158 pfxlist_onlink_check();
2164 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2166 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2167 ndpr->ndpr_preferred = 0;
2169 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
2170 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2171 ndpr->ndpr_expire = 0;
2173 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
2179 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
2181 /* init ia6t_expire */
2182 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2183 lt6->ia6t_expire = 0;
2185 lt6->ia6t_expire = time_uptime;
2186 lt6->ia6t_expire += lt6->ia6t_vltime;
2189 /* init ia6t_preferred */
2190 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2191 lt6->ia6t_preferred = 0;
2193 lt6->ia6t_preferred = time_uptime;
2194 lt6->ia6t_preferred += lt6->ia6t_pltime;
2199 * Delete all the routing table entries that use the specified gateway.
2200 * XXX: this function causes search through all entries of routing table, so
2201 * it shouldn't be called when acting as a router.
2204 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2206 struct radix_node_head *rnh;
2209 /* We'll care only link-local addresses */
2210 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2213 /* XXX Do we really need to walk any but the default FIB? */
2214 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
2215 rnh = rt_tables_get_rnh(fibnum, AF_INET6);
2219 RADIX_NODE_HEAD_LOCK(rnh);
2220 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
2221 RADIX_NODE_HEAD_UNLOCK(rnh);
2226 rt6_deleteroute(struct radix_node *rn, void *arg)
2228 #define SIN6(s) ((struct sockaddr_in6 *)s)
2229 struct rtentry *rt = (struct rtentry *)rn;
2230 struct in6_addr *gate = (struct in6_addr *)arg;
2232 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2235 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2240 * Do not delete a static route.
2241 * XXX: this seems to be a bit ad-hoc. Should we consider the
2242 * 'cloned' bit instead?
2244 if ((rt->rt_flags & RTF_STATIC) != 0)
2248 * We delete only host route. This means, in particular, we don't
2249 * delete default route.
2251 if ((rt->rt_flags & RTF_HOST) == 0)
2254 return (in6_rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
2255 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum));
2260 nd6_setdefaultiface(int ifindex)
2264 if (ifindex < 0 || V_if_index < ifindex)
2266 if (ifindex != 0 && !ifnet_byindex(ifindex))
2269 if (V_nd6_defifindex != ifindex) {
2270 V_nd6_defifindex = ifindex;
2271 if (V_nd6_defifindex > 0)
2272 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2274 V_nd6_defifp = NULL;
2277 * Our current implementation assumes one-to-one maping between
2278 * interfaces and links, so it would be natural to use the
2279 * default interface as the default link.
2281 scope6_setdefault(V_nd6_defifp);