2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/rwlock.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
53 #include <net/if_var.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 rtentry *, 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];
223 * We only accept RAs only when the per-interface flag
224 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
226 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
229 if (ip6->ip6_hlim != 255) {
231 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
232 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
233 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
237 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
239 "nd6_ra_input: src %s is not link-local\n",
240 ip6_sprintf(ip6bufs, &saddr6)));
244 #ifndef PULLDOWN_TEST
245 IP6_EXTHDR_CHECK(m, off, icmp6len,);
246 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
248 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
250 ICMP6STAT_INC(icp6s_tooshort);
255 icmp6len -= sizeof(*nd_ra);
256 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
257 if (nd6_options(&ndopts) < 0) {
259 "nd6_ra_input: invalid ND option, ignored\n"));
260 /* nd6_options have incremented stats */
265 struct nd_defrouter dr0;
266 u_int32_t advreachable = nd_ra->nd_ra_reachable;
268 /* remember if this is a multicasted advertisement */
269 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
272 bzero(&dr0, sizeof(dr0));
274 dr0.flags = nd_ra->nd_ra_flags_reserved;
276 * Effectively-disable routes from RA messages when
277 * ND6_IFF_NO_RADR enabled on the receiving interface or
278 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
280 if (ndi->flags & ND6_IFF_NO_RADR)
282 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
285 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
286 dr0.expire = time_uptime + dr0.rtlifetime;
288 /* unspecified or not? (RFC 2461 6.3.4) */
290 advreachable = ntohl(advreachable);
291 if (advreachable <= MAX_REACHABLE_TIME &&
292 ndi->basereachable != advreachable) {
293 ndi->basereachable = advreachable;
294 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
295 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
298 if (nd_ra->nd_ra_retransmit)
299 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
300 if (nd_ra->nd_ra_curhoplimit) {
301 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
302 ndi->chlim = nd_ra->nd_ra_curhoplimit;
303 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
304 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
305 "%s on %s (current = %d, received = %d). "
306 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
307 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
310 dr = defrtrlist_update(&dr0);
316 if (ndopts.nd_opts_pi) {
317 struct nd_opt_hdr *pt;
318 struct nd_opt_prefix_info *pi = NULL;
319 struct nd_prefixctl pr;
321 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
322 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
323 pt = (struct nd_opt_hdr *)((caddr_t)pt +
324 (pt->nd_opt_len << 3))) {
325 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
327 pi = (struct nd_opt_prefix_info *)pt;
329 if (pi->nd_opt_pi_len != 4) {
331 "nd6_ra_input: invalid option "
332 "len %d for prefix information option, "
333 "ignored\n", pi->nd_opt_pi_len));
337 if (128 < pi->nd_opt_pi_prefix_len) {
339 "nd6_ra_input: invalid prefix "
340 "len %d for prefix information option, "
341 "ignored\n", pi->nd_opt_pi_prefix_len));
345 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
346 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
348 "nd6_ra_input: invalid prefix "
351 &pi->nd_opt_pi_prefix)));
355 bzero(&pr, sizeof(pr));
356 pr.ndpr_prefix.sin6_family = AF_INET6;
357 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
358 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
359 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
361 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
362 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
363 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
364 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
365 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
366 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
367 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
368 (void)prelist_update(&pr, dr, m, mcast);
375 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
379 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
382 if (mtu < IPV6_MMTU) {
383 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
384 "mtu=%lu sent from %s, ignoring\n",
385 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
390 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
391 ? ndi->maxmtu : ifp->if_mtu;
393 int change = (ndi->linkmtu != mtu);
396 if (change) /* in6_maxmtu may change */
399 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
400 "mtu=%lu sent from %s; "
401 "exceeds maxmtu %lu, ignoring\n",
402 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
409 * Source link layer address
415 if (ndopts.nd_opts_src_lladdr) {
416 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
417 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
420 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
422 "nd6_ra_input: lladdrlen mismatch for %s "
423 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
424 ifp->if_addrlen, lladdrlen - 2));
428 nd6_cache_lladdr(ifp, &saddr6, lladdr,
429 lladdrlen, ND_ROUTER_ADVERT, 0);
432 * Installing a link-layer address might change the state of the
433 * router's neighbor cache, which might also affect our on-link
434 * detection of adveritsed prefixes.
436 pfxlist_onlink_check();
444 ICMP6STAT_INC(icp6s_badra);
449 * default router list proccessing sub routines
452 /* tell the change to user processes watching the routing socket. */
454 nd6_rtmsg(int cmd, struct rtentry *rt)
456 struct rt_addrinfo info;
460 bzero((caddr_t)&info, sizeof(info));
461 info.rti_info[RTAX_DST] = rt_key(rt);
462 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
463 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
467 ifa = TAILQ_FIRST(&ifp->if_addrhead);
468 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
470 IF_ADDR_RUNLOCK(ifp);
471 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
475 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
481 defrouter_addreq(struct nd_defrouter *new)
483 struct sockaddr_in6 def, mask, gate;
484 struct rtentry *newrt = NULL;
487 bzero(&def, sizeof(def));
488 bzero(&mask, sizeof(mask));
489 bzero(&gate, sizeof(gate));
491 def.sin6_len = mask.sin6_len = gate.sin6_len =
492 sizeof(struct sockaddr_in6);
493 def.sin6_family = gate.sin6_family = AF_INET6;
494 gate.sin6_addr = new->rtaddr;
496 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
497 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
498 RTF_GATEWAY, &newrt, RT_DEFAULT_FIB);
500 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
508 struct nd_defrouter *
509 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
511 struct nd_defrouter *dr;
513 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
514 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
518 return (NULL); /* search failed */
522 * Remove the default route for a given router.
523 * This is just a subroutine function for defrouter_select(), and should
524 * not be called from anywhere else.
527 defrouter_delreq(struct nd_defrouter *dr)
529 struct sockaddr_in6 def, mask, gate;
530 struct rtentry *oldrt = NULL;
532 bzero(&def, sizeof(def));
533 bzero(&mask, sizeof(mask));
534 bzero(&gate, sizeof(gate));
536 def.sin6_len = mask.sin6_len = gate.sin6_len =
537 sizeof(struct sockaddr_in6);
538 def.sin6_family = gate.sin6_family = AF_INET6;
539 gate.sin6_addr = dr->rtaddr;
541 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
542 (struct sockaddr *)&gate,
543 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, RT_DEFAULT_FIB);
545 nd6_rtmsg(RTM_DELETE, oldrt);
553 * remove all default routes from default router list
556 defrouter_reset(void)
558 struct nd_defrouter *dr;
560 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
561 defrouter_delreq(dr);
564 * XXX should we also nuke any default routers in the kernel, by
565 * going through them by rtalloc1()?
570 defrtrlist_del(struct nd_defrouter *dr)
572 struct nd_defrouter *deldr = NULL;
573 struct nd_prefix *pr;
576 * Flush all the routing table entries that use the router
579 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
580 rt6_flush(&dr->rtaddr, dr->ifp);
584 defrouter_delreq(dr);
586 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
589 * Also delete all the pointers to the router in each prefix lists.
591 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
592 struct nd_pfxrouter *pfxrtr;
593 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
596 pfxlist_onlink_check();
599 * If the router is the primary one, choose a new one.
600 * Note that defrouter_select() will remove the current gateway
601 * from the routing table.
610 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
611 * draft-ietf-ipngwg-router-selection:
612 * 1) Routers that are reachable or probably reachable should be preferred.
613 * If we have more than one (probably) reachable router, prefer ones
614 * with the highest router preference.
615 * 2) When no routers on the list are known to be reachable or
616 * probably reachable, routers SHOULD be selected in a round-robin
617 * fashion, regardless of router preference values.
618 * 3) If the Default Router List is empty, assume that all
619 * destinations are on-link.
621 * We assume nd_defrouter is sorted by router preference value.
622 * Since the code below covers both with and without router preference cases,
623 * we do not need to classify the cases by ifdef.
625 * At this moment, we do not try to install more than one default router,
626 * even when the multipath routing is available, because we're not sure about
627 * the benefits for stub hosts comparing to the risk of making the code
628 * complicated and the possibility of introducing bugs.
631 defrouter_select(void)
633 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL;
634 struct llentry *ln = NULL;
637 * Let's handle easy case (3) first:
638 * If default router list is empty, there's nothing to be done.
640 if (TAILQ_EMPTY(&V_nd_defrouter))
644 * Search for a (probably) reachable router from the list.
645 * We just pick up the first reachable one (if any), assuming that
646 * the ordering rule of the list described in defrtrlist_update().
648 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
649 IF_AFDATA_RLOCK(dr->ifp);
650 if (selected_dr == NULL &&
651 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
652 ND6_IS_LLINFO_PROBREACH(ln)) {
655 IF_AFDATA_RUNLOCK(dr->ifp);
661 if (dr->installed && installed_dr == NULL)
663 else if (dr->installed && installed_dr) {
664 /* this should not happen. warn for diagnosis. */
665 log(LOG_ERR, "defrouter_select: more than one router"
670 * If none of the default routers was found to be reachable,
671 * round-robin the list regardless of preference.
672 * Otherwise, if we have an installed router, check if the selected
673 * (reachable) router should really be preferred to the installed one.
674 * We only prefer the new router when the old one is not reachable
675 * or when the new one has a really higher preference value.
677 if (selected_dr == NULL) {
678 if (installed_dr == NULL || !TAILQ_NEXT(installed_dr, dr_entry))
679 selected_dr = TAILQ_FIRST(&V_nd_defrouter);
681 selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
682 } else if (installed_dr) {
683 IF_AFDATA_RLOCK(installed_dr->ifp);
684 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
685 ND6_IS_LLINFO_PROBREACH(ln) &&
686 rtpref(selected_dr) <= rtpref(installed_dr)) {
687 selected_dr = installed_dr;
689 IF_AFDATA_RUNLOCK(installed_dr->ifp);
695 * If the selected router is different than the installed one,
696 * remove the installed router and install the selected one.
697 * Note that the selected router is never NULL here.
699 if (installed_dr != selected_dr) {
701 defrouter_delreq(installed_dr);
702 defrouter_addreq(selected_dr);
709 * for default router selection
710 * regards router-preference field as a 2-bit signed integer
713 rtpref(struct nd_defrouter *dr)
715 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
716 case ND_RA_FLAG_RTPREF_HIGH:
717 return (RTPREF_HIGH);
718 case ND_RA_FLAG_RTPREF_MEDIUM:
719 case ND_RA_FLAG_RTPREF_RSV:
720 return (RTPREF_MEDIUM);
721 case ND_RA_FLAG_RTPREF_LOW:
725 * This case should never happen. If it did, it would mean a
726 * serious bug of kernel internal. We thus always bark here.
727 * Or, can we even panic?
729 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags);
730 return (RTPREF_INVALID);
735 static struct nd_defrouter *
736 defrtrlist_update(struct nd_defrouter *new)
738 struct nd_defrouter *dr, *n;
740 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
742 if (new->rtlifetime == 0) {
746 int oldpref = rtpref(dr);
749 dr->flags = new->flags; /* xxx flag check */
750 dr->rtlifetime = new->rtlifetime;
751 dr->expire = new->expire;
754 * If the preference does not change, there's no need
755 * to sort the entries. Also make sure the selected
756 * router is still installed in the kernel.
758 if (dr->installed && rtpref(new) == oldpref)
762 * preferred router may be changed, so relocate
764 * XXX: calling TAILQ_REMOVE directly is a bad manner.
765 * However, since defrtrlist_del() has many side
766 * effects, we intentionally do so here.
767 * defrouter_select() below will handle routing
770 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
777 /* entry does not exist */
778 if (new->rtlifetime == 0)
781 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
784 bzero(n, sizeof(*n));
789 * Insert the new router in the Default Router List;
790 * The Default Router List should be in the descending order
791 * of router-preferece. Routers with the same preference are
792 * sorted in the arriving time order.
795 /* insert at the end of the group */
796 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
797 if (rtpref(n) > rtpref(dr))
801 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
803 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
810 static struct nd_pfxrouter *
811 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
813 struct nd_pfxrouter *search;
815 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
816 if (search->router == dr)
824 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
826 struct nd_pfxrouter *new;
828 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
831 bzero(new, sizeof(*new));
834 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
836 pfxlist_onlink_check();
840 pfxrtr_del(struct nd_pfxrouter *pfr)
842 LIST_REMOVE(pfr, pfr_entry);
847 nd6_prefix_lookup(struct nd_prefixctl *key)
849 struct nd_prefix *search;
851 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
852 if (key->ndpr_ifp == search->ndpr_ifp &&
853 key->ndpr_plen == search->ndpr_plen &&
854 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
855 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
864 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
865 struct nd_prefix **newp)
867 struct nd_prefix *new = NULL;
869 char ip6buf[INET6_ADDRSTRLEN];
871 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
874 bzero(new, sizeof(*new));
875 new->ndpr_ifp = pr->ndpr_ifp;
876 new->ndpr_prefix = pr->ndpr_prefix;
877 new->ndpr_plen = pr->ndpr_plen;
878 new->ndpr_vltime = pr->ndpr_vltime;
879 new->ndpr_pltime = pr->ndpr_pltime;
880 new->ndpr_flags = pr->ndpr_flags;
881 if ((error = in6_init_prefix_ltimes(new)) != 0) {
885 new->ndpr_lastupdate = time_uptime;
890 LIST_INIT(&new->ndpr_advrtrs);
891 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
892 /* make prefix in the canonical form */
893 IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
895 /* link ndpr_entry to nd_prefix list */
896 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
898 /* ND_OPT_PI_FLAG_ONLINK processing */
899 if (new->ndpr_raf_onlink) {
902 if ((e = nd6_prefix_onlink(new)) != 0) {
903 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
904 "the prefix %s/%d on-link on %s (errno=%d)\n",
905 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
906 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
907 /* proceed anyway. XXX: is it correct? */
918 prelist_remove(struct nd_prefix *pr)
920 struct nd_pfxrouter *pfr, *next;
922 char ip6buf[INET6_ADDRSTRLEN];
924 /* make sure to invalidate the prefix until it is really freed. */
929 * Though these flags are now meaningless, we'd rather keep the value
930 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
931 * when executing "ndp -p".
934 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
935 (e = nd6_prefix_offlink(pr)) != 0) {
936 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
938 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
939 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
940 /* what should we do? */
943 if (pr->ndpr_refcnt > 0)
944 return; /* notice here? */
946 /* unlink ndpr_entry from nd_prefix list */
947 LIST_REMOVE(pr, ndpr_entry);
949 /* free list of routers that adversed the prefix */
950 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next) {
955 pfxlist_onlink_check();
963 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
964 struct mbuf *m, int mcast)
966 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
968 struct ifnet *ifp = new->ndpr_ifp;
969 struct nd_prefix *pr;
973 struct in6_addrlifetime lt6_tmp;
974 char ip6buf[INET6_ADDRSTRLEN];
979 * Authenticity for NA consists authentication for
980 * both IP header and IP datagrams, doesn't it ?
982 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
983 auth = ((m->m_flags & M_AUTHIPHDR) &&
984 (m->m_flags & M_AUTHIPDGM));
988 if ((pr = nd6_prefix_lookup(new)) != NULL) {
990 * nd6_prefix_lookup() ensures that pr and new have the same
991 * prefix on a same interface.
995 * Update prefix information. Note that the on-link (L) bit
996 * and the autonomous (A) bit should NOT be changed from 1
999 if (new->ndpr_raf_onlink == 1)
1000 pr->ndpr_raf_onlink = 1;
1001 if (new->ndpr_raf_auto == 1)
1002 pr->ndpr_raf_auto = 1;
1003 if (new->ndpr_raf_onlink) {
1004 pr->ndpr_vltime = new->ndpr_vltime;
1005 pr->ndpr_pltime = new->ndpr_pltime;
1006 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1007 pr->ndpr_lastupdate = time_uptime;
1010 if (new->ndpr_raf_onlink &&
1011 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1014 if ((e = nd6_prefix_onlink(pr)) != 0) {
1016 "prelist_update: failed to make "
1017 "the prefix %s/%d on-link on %s "
1020 &pr->ndpr_prefix.sin6_addr),
1021 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1022 /* proceed anyway. XXX: is it correct? */
1026 if (dr && pfxrtr_lookup(pr, dr) == NULL)
1029 struct nd_prefix *newpr = NULL;
1033 if (new->ndpr_vltime == 0)
1035 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1038 error = nd6_prelist_add(new, dr, &newpr);
1039 if (error != 0 || newpr == NULL) {
1040 nd6log((LOG_NOTICE, "prelist_update: "
1041 "nd6_prelist_add failed for %s/%d on %s "
1042 "errno=%d, returnpr=%p\n",
1043 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1044 new->ndpr_plen, if_name(new->ndpr_ifp),
1046 goto end; /* we should just give up in this case. */
1050 * XXX: from the ND point of view, we can ignore a prefix
1051 * with the on-link bit being zero. However, we need a
1052 * prefix structure for references from autoconfigured
1053 * addresses. Thus, we explicitly make sure that the prefix
1054 * itself expires now.
1056 if (newpr->ndpr_raf_onlink == 0) {
1057 newpr->ndpr_vltime = 0;
1058 newpr->ndpr_pltime = 0;
1059 in6_init_prefix_ltimes(newpr);
1066 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1067 * Note that pr must be non NULL at this point.
1070 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1071 if (!new->ndpr_raf_auto)
1075 * 5.5.3 (b). the link-local prefix should have been ignored in
1079 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1080 if (new->ndpr_pltime > new->ndpr_vltime) {
1081 error = EINVAL; /* XXX: won't be used */
1086 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1087 * an address configured by stateless autoconfiguration already in the
1088 * list of addresses associated with the interface, and the Valid
1089 * Lifetime is not 0, form an address. We first check if we have
1090 * a matching prefix.
1091 * Note: we apply a clarification in rfc2462bis-02 here. We only
1092 * consider autoconfigured addresses while RFC2462 simply said
1096 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1097 struct in6_ifaddr *ifa6;
1098 u_int32_t remaininglifetime;
1100 if (ifa->ifa_addr->sa_family != AF_INET6)
1103 ifa6 = (struct in6_ifaddr *)ifa;
1106 * We only consider autoconfigured addresses as per rfc2462bis.
1108 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1112 * Spec is not clear here, but I believe we should concentrate
1113 * on unicast (i.e. not anycast) addresses.
1114 * XXX: other ia6_flags? detached or duplicated?
1116 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1120 * Ignore the address if it is not associated with a prefix
1121 * or is associated with a prefix that is different from this
1122 * one. (pr is never NULL here)
1124 if (ifa6->ia6_ndpr != pr)
1127 if (ia6_match == NULL) /* remember the first one */
1131 * An already autoconfigured address matched. Now that we
1132 * are sure there is at least one matched address, we can
1133 * proceed to 5.5.3. (e): update the lifetimes according to the
1134 * "two hours" rule and the privacy extension.
1135 * We apply some clarifications in rfc2462bis:
1136 * - use remaininglifetime instead of storedlifetime as a
1138 * - remove the dead code in the "two-hour" rule
1140 #define TWOHOUR (120*60)
1141 lt6_tmp = ifa6->ia6_lifetime;
1143 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1144 remaininglifetime = ND6_INFINITE_LIFETIME;
1145 else if (time_uptime - ifa6->ia6_updatetime >
1146 lt6_tmp.ia6t_vltime) {
1148 * The case of "invalid" address. We should usually
1149 * not see this case.
1151 remaininglifetime = 0;
1153 remaininglifetime = lt6_tmp.ia6t_vltime -
1154 (time_uptime - ifa6->ia6_updatetime);
1156 /* when not updating, keep the current stored lifetime. */
1157 lt6_tmp.ia6t_vltime = remaininglifetime;
1159 if (TWOHOUR < new->ndpr_vltime ||
1160 remaininglifetime < new->ndpr_vltime) {
1161 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1162 } else if (remaininglifetime <= TWOHOUR) {
1164 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1168 * new->ndpr_vltime <= TWOHOUR &&
1169 * TWOHOUR < remaininglifetime
1171 lt6_tmp.ia6t_vltime = TWOHOUR;
1174 /* The 2 hour rule is not imposed for preferred lifetime. */
1175 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1177 in6_init_address_ltimes(pr, <6_tmp);
1180 * We need to treat lifetimes for temporary addresses
1181 * differently, according to
1182 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1183 * we only update the lifetimes when they are in the maximum
1186 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1187 u_int32_t maxvltime, maxpltime;
1189 if (V_ip6_temp_valid_lifetime >
1190 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1191 V_ip6_desync_factor)) {
1192 maxvltime = V_ip6_temp_valid_lifetime -
1193 (time_uptime - ifa6->ia6_createtime) -
1194 V_ip6_desync_factor;
1197 if (V_ip6_temp_preferred_lifetime >
1198 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1199 V_ip6_desync_factor)) {
1200 maxpltime = V_ip6_temp_preferred_lifetime -
1201 (time_uptime - ifa6->ia6_createtime) -
1202 V_ip6_desync_factor;
1206 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1207 lt6_tmp.ia6t_vltime > maxvltime) {
1208 lt6_tmp.ia6t_vltime = maxvltime;
1210 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1211 lt6_tmp.ia6t_pltime > maxpltime) {
1212 lt6_tmp.ia6t_pltime = maxpltime;
1215 ifa6->ia6_lifetime = lt6_tmp;
1216 ifa6->ia6_updatetime = time_uptime;
1218 IF_ADDR_RUNLOCK(ifp);
1219 if (ia6_match == NULL && new->ndpr_vltime) {
1223 * 5.5.3 (d) (continued)
1224 * No address matched and the valid lifetime is non-zero.
1225 * Create a new address.
1229 * Prefix Length check:
1230 * If the sum of the prefix length and interface identifier
1231 * length does not equal 128 bits, the Prefix Information
1232 * option MUST be ignored. The length of the interface
1233 * identifier is defined in a separate link-type specific
1236 ifidlen = in6_if2idlen(ifp);
1238 /* this should not happen, so we always log it. */
1239 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1243 if (ifidlen + pr->ndpr_plen != 128) {
1245 "prelist_update: invalid prefixlen "
1246 "%d for %s, ignored\n",
1247 pr->ndpr_plen, if_name(ifp)));
1251 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1253 * note that we should use pr (not new) for reference.
1260 * When a new public address is created as described
1261 * in RFC2462, also create a new temporary address.
1264 * When an interface connects to a new link, a new
1265 * randomized interface identifier should be generated
1266 * immediately together with a new set of temporary
1267 * addresses. Thus, we specifiy 1 as the 2nd arg of
1270 if (V_ip6_use_tempaddr) {
1272 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1273 nd6log((LOG_NOTICE, "prelist_update: "
1274 "failed to create a temporary "
1275 "address, errno=%d\n",
1279 ifa_free(&ia6->ia_ifa);
1282 * A newly added address might affect the status
1283 * of other addresses, so we check and update it.
1284 * XXX: what if address duplication happens?
1286 pfxlist_onlink_check();
1288 /* just set an error. do not bark here. */
1289 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1298 * A supplement function used in the on-link detection below;
1299 * detect if a given prefix has a (probably) reachable advertising router.
1300 * XXX: lengthy function name...
1302 static struct nd_pfxrouter *
1303 find_pfxlist_reachable_router(struct nd_prefix *pr)
1305 struct nd_pfxrouter *pfxrtr;
1309 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1310 IF_AFDATA_RLOCK(pfxrtr->router->ifp);
1311 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1312 IF_AFDATA_RUNLOCK(pfxrtr->router->ifp);
1315 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1324 * Check if each prefix in the prefix list has at least one available router
1325 * that advertised the prefix (a router is "available" if its neighbor cache
1326 * entry is reachable or probably reachable).
1327 * If the check fails, the prefix may be off-link, because, for example,
1328 * we have moved from the network but the lifetime of the prefix has not
1329 * expired yet. So we should not use the prefix if there is another prefix
1330 * that has an available router.
1331 * But, if there is no prefix that has an available router, we still regards
1332 * all the prefixes as on-link. This is because we can't tell if all the
1333 * routers are simply dead or if we really moved from the network and there
1334 * is no router around us.
1337 pfxlist_onlink_check()
1339 struct nd_prefix *pr;
1340 struct in6_ifaddr *ifa;
1341 struct nd_defrouter *dr;
1342 struct nd_pfxrouter *pfxrtr = NULL;
1345 * Check if there is a prefix that has a reachable advertising
1348 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1349 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1354 * If we have no such prefix, check whether we still have a router
1355 * that does not advertise any prefixes.
1358 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1359 struct nd_prefix *pr0;
1361 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1362 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1369 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
1371 * There is at least one prefix that has a reachable router,
1372 * or at least a router which probably does not advertise
1373 * any prefixes. The latter would be the case when we move
1374 * to a new link where we have a router that does not provide
1375 * prefixes and we configure an address by hand.
1376 * Detach prefixes which have no reachable advertising
1377 * router, and attach other prefixes.
1379 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1380 /* XXX: a link-local prefix should never be detached */
1381 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1385 * we aren't interested in prefixes without the L bit
1388 if (pr->ndpr_raf_onlink == 0)
1391 if (pr->ndpr_raf_auto == 0)
1394 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1395 find_pfxlist_reachable_router(pr) == NULL)
1396 pr->ndpr_stateflags |= NDPRF_DETACHED;
1397 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1398 find_pfxlist_reachable_router(pr) != 0)
1399 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1402 /* there is no prefix that has a reachable router */
1403 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1404 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1407 if (pr->ndpr_raf_onlink == 0)
1410 if (pr->ndpr_raf_auto == 0)
1413 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1414 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1419 * Remove each interface route associated with a (just) detached
1420 * prefix, and reinstall the interface route for a (just) attached
1421 * prefix. Note that all attempt of reinstallation does not
1422 * necessarily success, when a same prefix is shared among multiple
1423 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1424 * so we don't have to care about them.
1426 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1428 char ip6buf[INET6_ADDRSTRLEN];
1430 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1433 if (pr->ndpr_raf_onlink == 0)
1436 if (pr->ndpr_raf_auto == 0)
1439 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1440 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1441 if ((e = nd6_prefix_offlink(pr)) != 0) {
1443 "pfxlist_onlink_check: failed to "
1444 "make %s/%d offlink, errno=%d\n",
1446 &pr->ndpr_prefix.sin6_addr),
1450 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1451 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1452 pr->ndpr_raf_onlink) {
1453 if ((e = nd6_prefix_onlink(pr)) != 0) {
1455 "pfxlist_onlink_check: failed to "
1456 "make %s/%d onlink, errno=%d\n",
1458 &pr->ndpr_prefix.sin6_addr),
1465 * Changes on the prefix status might affect address status as well.
1466 * Make sure that all addresses derived from an attached prefix are
1467 * attached, and that all addresses derived from a detached prefix are
1468 * detached. Note, however, that a manually configured address should
1469 * always be attached.
1470 * The precise detection logic is same as the one for prefixes.
1472 * XXXRW: in6_ifaddrhead locking.
1474 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1475 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1478 if (ifa->ia6_ndpr == NULL) {
1480 * This can happen when we first configure the address
1481 * (i.e. the address exists, but the prefix does not).
1482 * XXX: complicated relationships...
1487 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1491 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1492 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1495 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1498 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1499 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1500 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1501 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1502 nd6_dad_start((struct ifaddr *)ifa, 0);
1505 ifa->ia6_flags |= IN6_IFF_DETACHED;
1510 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1511 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1514 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1515 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1516 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1517 /* Do we need a delay in this case? */
1518 nd6_dad_start((struct ifaddr *)ifa, 0);
1525 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
1527 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1528 struct radix_node_head *rnh;
1530 struct sockaddr_in6 mask6;
1532 int error, a_failure, fibnum;
1535 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1536 * ifa->ifa_rtrequest = nd6_rtrequest;
1538 bzero(&mask6, sizeof(mask6));
1539 mask6.sin6_len = sizeof(mask6);
1540 mask6.sin6_addr = pr->ndpr_mask;
1541 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1544 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1547 error = in6_rtrequest(RTM_ADD,
1548 (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr,
1549 (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
1551 KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
1552 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
1555 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
1556 /* XXX what if rhn == NULL? */
1557 RADIX_NODE_HEAD_LOCK(rnh);
1559 if (rt_setgate(rt, rt_key(rt),
1560 (struct sockaddr *)&null_sdl) == 0) {
1561 struct sockaddr_dl *dl;
1563 dl = (struct sockaddr_dl *)rt->rt_gateway;
1564 dl->sdl_type = rt->rt_ifp->if_type;
1565 dl->sdl_index = rt->rt_ifp->if_index;
1567 RADIX_NODE_HEAD_UNLOCK(rnh);
1568 nd6_rtmsg(RTM_ADD, rt);
1570 pr->ndpr_stateflags |= NDPRF_ONLINK;
1572 char ip6buf[INET6_ADDRSTRLEN];
1573 char ip6bufg[INET6_ADDRSTRLEN];
1574 char ip6bufm[INET6_ADDRSTRLEN];
1575 struct sockaddr_in6 *sin6;
1577 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1578 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
1579 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
1580 "flags=%lx errno = %d\n",
1581 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1582 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1583 ip6_sprintf(ip6bufg, &sin6->sin6_addr),
1584 ip6_sprintf(ip6bufm, &mask6.sin6_addr),
1587 /* Save last error to return, see rtinit(). */
1598 /* Return the last error we got. */
1603 nd6_prefix_onlink(struct nd_prefix *pr)
1606 struct ifnet *ifp = pr->ndpr_ifp;
1607 struct nd_prefix *opr;
1609 char ip6buf[INET6_ADDRSTRLEN];
1612 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1614 "nd6_prefix_onlink: %s/%d is already on-link\n",
1615 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1621 * Add the interface route associated with the prefix. Before
1622 * installing the route, check if there's the same prefix on another
1623 * interface, and the prefix has already installed the interface route.
1624 * Although such a configuration is expected to be rare, we explicitly
1627 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1631 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1634 if (opr->ndpr_plen == pr->ndpr_plen &&
1635 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1636 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1641 * We prefer link-local addresses as the associated interface address.
1643 /* search for a link-local addr */
1644 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1645 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1647 /* XXX: freebsd does not have ifa_ifwithaf */
1649 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1650 if (ifa->ifa_addr->sa_family == AF_INET6)
1655 IF_ADDR_RUNLOCK(ifp);
1656 /* should we care about ia6_flags? */
1660 * This can still happen, when, for example, we receive an RA
1661 * containing a prefix with the L bit set and the A bit clear,
1662 * after removing all IPv6 addresses on the receiving
1663 * interface. This should, of course, be rare though.
1666 "nd6_prefix_onlink: failed to find any ifaddr"
1667 " to add route for a prefix(%s/%d) on %s\n",
1668 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1669 pr->ndpr_plen, if_name(ifp)));
1673 error = nd6_prefix_onlink_rtrequest(pr, ifa);
1682 nd6_prefix_offlink(struct nd_prefix *pr)
1685 struct ifnet *ifp = pr->ndpr_ifp;
1686 struct nd_prefix *opr;
1687 struct sockaddr_in6 sa6, mask6;
1689 char ip6buf[INET6_ADDRSTRLEN];
1690 int fibnum, a_failure;
1693 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1695 "nd6_prefix_offlink: %s/%d is already off-link\n",
1696 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1701 bzero(&sa6, sizeof(sa6));
1702 sa6.sin6_family = AF_INET6;
1703 sa6.sin6_len = sizeof(sa6);
1704 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1705 sizeof(struct in6_addr));
1706 bzero(&mask6, sizeof(mask6));
1707 mask6.sin6_family = AF_INET6;
1708 mask6.sin6_len = sizeof(sa6);
1709 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1712 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1714 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1715 (struct sockaddr *)&mask6, 0, &rt, fibnum);
1717 /* report the route deletion to the routing socket. */
1719 nd6_rtmsg(RTM_DELETE, rt);
1721 /* Save last error to return, see rtinit(). */
1731 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1734 * There might be the same prefix on another interface,
1735 * the prefix which could not be on-link just because we have
1736 * the interface route (see comments in nd6_prefix_onlink).
1737 * If there's one, try to make the prefix on-link on the
1740 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1744 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1748 * KAME specific: detached prefixes should not be
1751 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1754 if (opr->ndpr_plen == pr->ndpr_plen &&
1755 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1756 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1759 if ((e = nd6_prefix_onlink(opr)) != 0) {
1761 "nd6_prefix_offlink: failed to "
1762 "recover a prefix %s/%d from %s "
1763 "to %s (errno = %d)\n",
1765 &opr->ndpr_prefix.sin6_addr),
1766 opr->ndpr_plen, if_name(ifp),
1767 if_name(opr->ndpr_ifp), e));
1773 /* XXX: can we still set the NDPRF_ONLINK flag? */
1775 "nd6_prefix_offlink: failed to delete route: "
1776 "%s/%d on %s (errno = %d)\n",
1777 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
1778 if_name(ifp), error));
1782 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
1783 (struct sockaddr *)&mask6, LLE_STATIC);
1788 static struct in6_ifaddr *
1789 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1791 struct ifnet *ifp = pr->ndpr_ifp;
1793 struct in6_aliasreq ifra;
1794 struct in6_ifaddr *ia, *ib;
1796 struct in6_addr mask;
1797 int prefixlen = pr->ndpr_plen;
1799 char ip6buf[INET6_ADDRSTRLEN];
1801 in6_prefixlen2mask(&mask, prefixlen);
1804 * find a link-local address (will be interface ID).
1805 * Is it really mandatory? Theoretically, a global or a site-local
1806 * address can be configured without a link-local address, if we
1807 * have a unique interface identifier...
1809 * it is not mandatory to have a link-local address, we can generate
1810 * interface identifier on the fly. we do this because:
1811 * (1) it should be the easiest way to find interface identifier.
1812 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1813 * for multiple addresses on a single interface, and possible shortcut
1814 * of DAD. we omitted DAD for this reason in the past.
1815 * (3) a user can prevent autoconfiguration of global address
1816 * by removing link-local address by hand (this is partly because we
1817 * don't have other way to control the use of IPv6 on an interface.
1818 * this has been our design choice - cf. NRL's "ifconfig auto").
1819 * (4) it is easier to manage when an interface has addresses
1820 * with the same interface identifier, than to have multiple addresses
1821 * with different interface identifiers.
1823 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1825 ib = (struct in6_ifaddr *)ifa;
1829 /* prefixlen + ifidlen must be equal to 128 */
1830 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1831 if (prefixlen != plen0) {
1833 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1834 "(prefix=%d ifid=%d)\n",
1835 if_name(ifp), prefixlen, 128 - plen0));
1840 in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1842 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1844 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1845 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1846 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1847 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1848 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1849 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1850 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1851 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1855 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1856 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1858 /* XXX: scope zone ID? */
1860 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1863 * Make sure that we do not have this address already. This should
1864 * usually not happen, but we can still see this case, e.g., if we
1865 * have manually configured the exact address to be configured.
1867 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1868 &ifra.ifra_addr.sin6_addr);
1871 /* this should be rare enough to make an explicit log */
1872 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1873 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1878 * Allocate ifaddr structure, link into chain, etc.
1879 * If we are going to create a new address upon receiving a multicasted
1880 * RA, we need to impose a random delay before starting DAD.
1881 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1885 updateflags |= IN6_IFAUPDATE_DADDELAY;
1886 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1888 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1889 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1890 if_name(ifp), error));
1891 return (NULL); /* ifaddr must not have been allocated. */
1894 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1896 * XXXRW: Assumption of non-NULLness here might not be true with
1897 * fine-grained locking -- should we validate it? Or just return
1898 * earlier ifa rather than looking it up again?
1900 return (ia); /* this is always non-NULL and referenced. */
1904 * ia0 - corresponding public address
1907 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
1909 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1910 struct in6_ifaddr *newia;
1911 struct in6_aliasreq ifra;
1913 int trylimit = 3; /* XXX: adhoc value */
1915 u_int32_t randid[2];
1916 time_t vltime0, pltime0;
1918 in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
1919 &ia0->ia_prefixmask.sin6_addr);
1921 ifra.ifra_addr = ia0->ia_addr; /* XXX: do we need this ? */
1922 /* clear the old IFID */
1923 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
1924 &ifra.ifra_prefixmask.sin6_addr);
1927 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
1928 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
1929 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
1933 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1934 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1935 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1936 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1939 * in6_get_tmpifid() quite likely provided a unique interface ID.
1940 * However, we may still have a chance to see collision, because
1941 * there may be a time lag between generation of the ID and generation
1942 * of the address. So, we'll do one more sanity check.
1945 if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
1946 if (trylimit-- > 0) {
1951 /* Give up. Something strange should have happened. */
1952 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
1953 "find a unique random IFID\n"));
1958 * The Valid Lifetime is the lower of the Valid Lifetime of the
1959 * public address or TEMP_VALID_LIFETIME.
1960 * The Preferred Lifetime is the lower of the Preferred Lifetime
1961 * of the public address or TEMP_PREFERRED_LIFETIME -
1964 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1965 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
1966 (ia0->ia6_lifetime.ia6t_vltime -
1967 (time_uptime - ia0->ia6_updatetime));
1968 if (vltime0 > V_ip6_temp_valid_lifetime)
1969 vltime0 = V_ip6_temp_valid_lifetime;
1971 vltime0 = V_ip6_temp_valid_lifetime;
1972 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1973 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
1974 (ia0->ia6_lifetime.ia6t_pltime -
1975 (time_uptime - ia0->ia6_updatetime));
1976 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
1977 pltime0 = V_ip6_temp_preferred_lifetime -
1978 V_ip6_desync_factor;
1981 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
1982 ifra.ifra_lifetime.ia6t_vltime = vltime0;
1983 ifra.ifra_lifetime.ia6t_pltime = pltime0;
1986 * A temporary address is created only if this calculated Preferred
1987 * Lifetime is greater than REGEN_ADVANCE time units.
1989 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
1992 /* XXX: scope zone ID? */
1994 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
1996 /* allocate ifaddr structure, link into chain, etc. */
1999 updateflags |= IN6_IFAUPDATE_DADDELAY;
2000 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2003 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2004 if (newia == NULL) { /* XXX: can it happen? */
2006 "in6_tmpifadd: ifa update succeeded, but we got "
2008 return (EINVAL); /* XXX */
2010 newia->ia6_ndpr = ia0->ia6_ndpr;
2011 newia->ia6_ndpr->ndpr_refcnt++;
2012 ifa_free(&newia->ia_ifa);
2015 * A newly added address might affect the status of other addresses.
2016 * XXX: when the temporary address is generated with a new public
2017 * address, the onlink check is redundant. However, it would be safe
2018 * to do the check explicitly everywhere a new address is generated,
2019 * and, in fact, we surely need the check when we create a new
2020 * temporary address due to deprecation of an old temporary address.
2022 pfxlist_onlink_check();
2028 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2030 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2031 ndpr->ndpr_preferred = 0;
2033 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
2034 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2035 ndpr->ndpr_expire = 0;
2037 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
2043 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
2045 /* init ia6t_expire */
2046 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2047 lt6->ia6t_expire = 0;
2049 lt6->ia6t_expire = time_uptime;
2050 lt6->ia6t_expire += lt6->ia6t_vltime;
2053 /* init ia6t_preferred */
2054 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2055 lt6->ia6t_preferred = 0;
2057 lt6->ia6t_preferred = time_uptime;
2058 lt6->ia6t_preferred += lt6->ia6t_pltime;
2063 * Delete all the routing table entries that use the specified gateway.
2064 * XXX: this function causes search through all entries of routing table, so
2065 * it shouldn't be called when acting as a router.
2068 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2071 /* We'll care only link-local addresses */
2072 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2075 /* XXX Do we really need to walk any but the default FIB? */
2076 rt_foreach_fib_walk(AF_INET6, NULL, rt6_deleteroute, (void *)gateway);
2080 rt6_deleteroute(struct rtentry *rt, void *arg)
2082 #define SIN6(s) ((struct sockaddr_in6 *)s)
2083 struct in6_addr *gate = (struct in6_addr *)arg;
2085 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2088 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2093 * Do not delete a static route.
2094 * XXX: this seems to be a bit ad-hoc. Should we consider the
2095 * 'cloned' bit instead?
2097 if ((rt->rt_flags & RTF_STATIC) != 0)
2101 * We delete only host route. This means, in particular, we don't
2102 * delete default route.
2104 if ((rt->rt_flags & RTF_HOST) == 0)
2107 return (in6_rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
2108 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum));
2113 nd6_setdefaultiface(int ifindex)
2117 if (ifindex < 0 || V_if_index < ifindex)
2119 if (ifindex != 0 && !ifnet_byindex(ifindex))
2122 if (V_nd6_defifindex != ifindex) {
2123 V_nd6_defifindex = ifindex;
2124 if (V_nd6_defifindex > 0)
2125 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2127 V_nd6_defifp = NULL;
2130 * Our current implementation assumes one-to-one maping between
2131 * interfaces and links, so it would be natural to use the
2132 * default interface as the default link.
2134 scope6_setdefault(V_nd6_defifp);