2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/rwlock.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
53 #include <net/if_types.h>
54 #include <net/if_dl.h>
55 #include <net/route.h>
56 #include <net/radix.h>
59 #include <netinet/in.h>
60 #include <net/if_llatbl.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet6/in6_ifattach.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/nd6.h>
66 #include <netinet/icmp6.h>
67 #include <netinet6/scope6_var.h>
69 static int rtpref(struct nd_defrouter *);
70 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
71 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
73 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
74 static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
75 struct nd_defrouter *);
76 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
77 static void pfxrtr_del(struct nd_pfxrouter *);
78 static struct nd_pfxrouter *find_pfxlist_reachable_router
80 static void defrouter_delreq(struct nd_defrouter *);
81 static void nd6_rtmsg(int, struct rtentry *);
83 static int in6_init_prefix_ltimes(struct nd_prefix *);
84 static void in6_init_address_ltimes(struct nd_prefix *,
85 struct in6_addrlifetime *);
87 static int nd6_prefix_onlink(struct nd_prefix *);
88 static int nd6_prefix_offlink(struct nd_prefix *);
90 static int rt6_deleteroute(struct radix_node *, void *);
92 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
93 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
95 static VNET_DEFINE(struct ifnet *, nd6_defifp);
96 VNET_DEFINE(int, nd6_defifindex);
97 #define V_nd6_defifp VNET(nd6_defifp)
99 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
101 VNET_DEFINE(int, ip6_desync_factor);
102 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
103 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
105 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
107 /* RTPREF_MEDIUM has to be 0! */
108 #define RTPREF_HIGH 1
109 #define RTPREF_MEDIUM 0
110 #define RTPREF_LOW (-1)
111 #define RTPREF_RESERVED (-2)
112 #define RTPREF_INVALID (-3) /* internal */
115 * Receive Router Solicitation Message - just for routers.
116 * Router solicitation/advertisement is mostly managed by userland program
117 * (rtadvd) so here we have no function like nd6_ra_output().
122 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
124 struct ifnet *ifp = m->m_pkthdr.rcvif;
125 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
126 struct nd_router_solicit *nd_rs;
127 struct in6_addr saddr6 = ip6->ip6_src;
130 union nd_opts ndopts;
131 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
134 * Accept RS only when V_ip6_forwarding=1 and the interface has
135 * no ND6_IFF_ACCEPT_RTADV.
137 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
141 if (ip6->ip6_hlim != 255) {
143 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
144 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
145 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
150 * Don't update the neighbor cache, if src = ::.
151 * This indicates that the src has no IP address assigned yet.
153 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
156 #ifndef PULLDOWN_TEST
157 IP6_EXTHDR_CHECK(m, off, icmp6len,);
158 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
160 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
162 ICMP6STAT_INC(icp6s_tooshort);
167 icmp6len -= sizeof(*nd_rs);
168 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
169 if (nd6_options(&ndopts) < 0) {
171 "nd6_rs_input: invalid ND option, ignored\n"));
172 /* nd6_options have incremented stats */
176 if (ndopts.nd_opts_src_lladdr) {
177 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
178 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
181 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
183 "nd6_rs_input: lladdrlen mismatch for %s "
184 "(if %d, RS packet %d)\n",
185 ip6_sprintf(ip6bufs, &saddr6),
186 ifp->if_addrlen, lladdrlen - 2));
190 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
197 ICMP6STAT_INC(icp6s_badrs);
202 * Receive Router Advertisement Message.
205 * TODO: on-link bit on prefix information
206 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
209 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
211 struct ifnet *ifp = m->m_pkthdr.rcvif;
212 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
213 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
214 struct nd_router_advert *nd_ra;
215 struct in6_addr saddr6 = ip6->ip6_src;
217 union nd_opts ndopts;
218 struct nd_defrouter *dr;
219 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
222 * We only accept RAs only when the per-interface flag
223 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
225 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
228 if (ip6->ip6_hlim != 255) {
230 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
231 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
232 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
236 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
238 "nd6_ra_input: src %s is not link-local\n",
239 ip6_sprintf(ip6bufs, &saddr6)));
243 #ifndef PULLDOWN_TEST
244 IP6_EXTHDR_CHECK(m, off, icmp6len,);
245 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
247 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
249 ICMP6STAT_INC(icp6s_tooshort);
254 icmp6len -= sizeof(*nd_ra);
255 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
256 if (nd6_options(&ndopts) < 0) {
258 "nd6_ra_input: invalid ND option, ignored\n"));
259 /* nd6_options have incremented stats */
264 struct nd_defrouter dr0;
265 u_int32_t advreachable = nd_ra->nd_ra_reachable;
267 /* remember if this is a multicasted advertisement */
268 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
271 bzero(&dr0, sizeof(dr0));
273 dr0.flags = nd_ra->nd_ra_flags_reserved;
275 * Effectively-disable routes from RA messages when
276 * ND6_IFF_NO_RADR enabled on the receiving interface or
277 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
279 if (ndi->flags & ND6_IFF_NO_RADR)
281 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
284 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
285 dr0.expire = time_uptime + dr0.rtlifetime;
287 /* unspecified or not? (RFC 2461 6.3.4) */
289 advreachable = ntohl(advreachable);
290 if (advreachable <= MAX_REACHABLE_TIME &&
291 ndi->basereachable != advreachable) {
292 ndi->basereachable = advreachable;
293 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
294 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
297 if (nd_ra->nd_ra_retransmit)
298 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
299 if (nd_ra->nd_ra_curhoplimit) {
300 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
301 ndi->chlim = nd_ra->nd_ra_curhoplimit;
302 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
303 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
304 "%s on %s (current = %d, received = %d). "
305 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
306 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
309 dr = defrtrlist_update(&dr0);
315 if (ndopts.nd_opts_pi) {
316 struct nd_opt_hdr *pt;
317 struct nd_opt_prefix_info *pi = NULL;
318 struct nd_prefixctl pr;
320 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
321 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
322 pt = (struct nd_opt_hdr *)((caddr_t)pt +
323 (pt->nd_opt_len << 3))) {
324 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
326 pi = (struct nd_opt_prefix_info *)pt;
328 if (pi->nd_opt_pi_len != 4) {
330 "nd6_ra_input: invalid option "
331 "len %d for prefix information option, "
332 "ignored\n", pi->nd_opt_pi_len));
336 if (128 < pi->nd_opt_pi_prefix_len) {
338 "nd6_ra_input: invalid prefix "
339 "len %d for prefix information option, "
340 "ignored\n", pi->nd_opt_pi_prefix_len));
344 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
345 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
347 "nd6_ra_input: invalid prefix "
350 &pi->nd_opt_pi_prefix)));
354 bzero(&pr, sizeof(pr));
355 pr.ndpr_prefix.sin6_family = AF_INET6;
356 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
357 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
358 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
360 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
361 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
362 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
363 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
364 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
365 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
366 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
367 (void)prelist_update(&pr, dr, m, mcast);
374 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
378 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
381 if (mtu < IPV6_MMTU) {
382 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
383 "mtu=%lu sent from %s, ignoring\n",
384 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
389 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
390 ? ndi->maxmtu : ifp->if_mtu;
392 int change = (ndi->linkmtu != mtu);
395 if (change) /* in6_maxmtu may change */
398 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
399 "mtu=%lu sent from %s; "
400 "exceeds maxmtu %lu, ignoring\n",
401 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
408 * Source link layer address
414 if (ndopts.nd_opts_src_lladdr) {
415 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
416 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
419 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
421 "nd6_ra_input: lladdrlen mismatch for %s "
422 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
423 ifp->if_addrlen, lladdrlen - 2));
427 nd6_cache_lladdr(ifp, &saddr6, lladdr,
428 lladdrlen, ND_ROUTER_ADVERT, 0);
431 * Installing a link-layer address might change the state of the
432 * router's neighbor cache, which might also affect our on-link
433 * detection of adveritsed prefixes.
435 pfxlist_onlink_check();
443 ICMP6STAT_INC(icp6s_badra);
448 * default router list proccessing sub routines
451 /* tell the change to user processes watching the routing socket. */
453 nd6_rtmsg(int cmd, struct rtentry *rt)
455 struct rt_addrinfo info;
459 bzero((caddr_t)&info, sizeof(info));
460 info.rti_info[RTAX_DST] = rt_key(rt);
461 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
462 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
466 ifa = TAILQ_FIRST(&ifp->if_addrhead);
467 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
469 IF_ADDR_RUNLOCK(ifp);
470 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
474 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
480 defrouter_addreq(struct nd_defrouter *new)
482 struct sockaddr_in6 def, mask, gate;
483 struct rtentry *newrt = NULL;
486 bzero(&def, sizeof(def));
487 bzero(&mask, sizeof(mask));
488 bzero(&gate, sizeof(gate));
490 def.sin6_len = mask.sin6_len = gate.sin6_len =
491 sizeof(struct sockaddr_in6);
492 def.sin6_family = gate.sin6_family = AF_INET6;
493 gate.sin6_addr = new->rtaddr;
495 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
496 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
497 RTF_GATEWAY, &newrt, RT_DEFAULT_FIB);
499 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
507 struct nd_defrouter *
508 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
510 struct nd_defrouter *dr;
512 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
513 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
517 return (NULL); /* search failed */
521 * Remove the default route for a given router.
522 * This is just a subroutine function for defrouter_select(), and should
523 * not be called from anywhere else.
526 defrouter_delreq(struct nd_defrouter *dr)
528 struct sockaddr_in6 def, mask, gate;
529 struct rtentry *oldrt = NULL;
531 bzero(&def, sizeof(def));
532 bzero(&mask, sizeof(mask));
533 bzero(&gate, sizeof(gate));
535 def.sin6_len = mask.sin6_len = gate.sin6_len =
536 sizeof(struct sockaddr_in6);
537 def.sin6_family = gate.sin6_family = AF_INET6;
538 gate.sin6_addr = dr->rtaddr;
540 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
541 (struct sockaddr *)&gate,
542 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, RT_DEFAULT_FIB);
544 nd6_rtmsg(RTM_DELETE, oldrt);
552 * remove all default routes from default router list
555 defrouter_reset(void)
557 struct nd_defrouter *dr;
559 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
560 defrouter_delreq(dr);
563 * XXX should we also nuke any default routers in the kernel, by
564 * going through them by rtalloc1()?
569 defrtrlist_del(struct nd_defrouter *dr)
571 struct nd_defrouter *deldr = NULL;
572 struct nd_prefix *pr;
575 * Flush all the routing table entries that use the router
578 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
579 rt6_flush(&dr->rtaddr, dr->ifp);
583 defrouter_delreq(dr);
585 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
588 * Also delete all the pointers to the router in each prefix lists.
590 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
591 struct nd_pfxrouter *pfxrtr;
592 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
595 pfxlist_onlink_check();
598 * If the router is the primary one, choose a new one.
599 * Note that defrouter_select() will remove the current gateway
600 * from the routing table.
609 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
610 * draft-ietf-ipngwg-router-selection:
611 * 1) Routers that are reachable or probably reachable should be preferred.
612 * If we have more than one (probably) reachable router, prefer ones
613 * with the highest router preference.
614 * 2) When no routers on the list are known to be reachable or
615 * probably reachable, routers SHOULD be selected in a round-robin
616 * fashion, regardless of router preference values.
617 * 3) If the Default Router List is empty, assume that all
618 * destinations are on-link.
620 * We assume nd_defrouter is sorted by router preference value.
621 * Since the code below covers both with and without router preference cases,
622 * we do not need to classify the cases by ifdef.
624 * At this moment, we do not try to install more than one default router,
625 * even when the multipath routing is available, because we're not sure about
626 * the benefits for stub hosts comparing to the risk of making the code
627 * complicated and the possibility of introducing bugs.
630 defrouter_select(void)
632 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL;
633 struct llentry *ln = NULL;
636 * Let's handle easy case (3) first:
637 * If default router list is empty, there's nothing to be done.
639 if (TAILQ_EMPTY(&V_nd_defrouter))
643 * Search for a (probably) reachable router from the list.
644 * We just pick up the first reachable one (if any), assuming that
645 * the ordering rule of the list described in defrtrlist_update().
647 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
648 IF_AFDATA_RLOCK(dr->ifp);
649 if (selected_dr == NULL &&
650 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
651 ND6_IS_LLINFO_PROBREACH(ln)) {
654 IF_AFDATA_RUNLOCK(dr->ifp);
660 if (dr->installed && installed_dr == NULL)
662 else if (dr->installed && installed_dr) {
663 /* this should not happen. warn for diagnosis. */
664 log(LOG_ERR, "defrouter_select: more than one router"
669 * If none of the default routers was found to be reachable,
670 * round-robin the list regardless of preference.
671 * Otherwise, if we have an installed router, check if the selected
672 * (reachable) router should really be preferred to the installed one.
673 * We only prefer the new router when the old one is not reachable
674 * or when the new one has a really higher preference value.
676 if (selected_dr == NULL) {
677 if (installed_dr == NULL || !TAILQ_NEXT(installed_dr, dr_entry))
678 selected_dr = TAILQ_FIRST(&V_nd_defrouter);
680 selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
681 } else if (installed_dr) {
682 IF_AFDATA_RLOCK(installed_dr->ifp);
683 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
684 ND6_IS_LLINFO_PROBREACH(ln) &&
685 rtpref(selected_dr) <= rtpref(installed_dr)) {
686 selected_dr = installed_dr;
688 IF_AFDATA_RUNLOCK(installed_dr->ifp);
694 * If the selected router is different than the installed one,
695 * remove the installed router and install the selected one.
696 * Note that the selected router is never NULL here.
698 if (installed_dr != selected_dr) {
700 defrouter_delreq(installed_dr);
701 defrouter_addreq(selected_dr);
708 * for default router selection
709 * regards router-preference field as a 2-bit signed integer
712 rtpref(struct nd_defrouter *dr)
714 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
715 case ND_RA_FLAG_RTPREF_HIGH:
716 return (RTPREF_HIGH);
717 case ND_RA_FLAG_RTPREF_MEDIUM:
718 case ND_RA_FLAG_RTPREF_RSV:
719 return (RTPREF_MEDIUM);
720 case ND_RA_FLAG_RTPREF_LOW:
724 * This case should never happen. If it did, it would mean a
725 * serious bug of kernel internal. We thus always bark here.
726 * Or, can we even panic?
728 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags);
729 return (RTPREF_INVALID);
734 static struct nd_defrouter *
735 defrtrlist_update(struct nd_defrouter *new)
737 struct nd_defrouter *dr, *n;
739 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
741 if (new->rtlifetime == 0) {
745 int oldpref = rtpref(dr);
748 dr->flags = new->flags; /* xxx flag check */
749 dr->rtlifetime = new->rtlifetime;
750 dr->expire = new->expire;
753 * If the preference does not change, there's no need
754 * to sort the entries. Also make sure the selected
755 * router is still installed in the kernel.
757 if (dr->installed && rtpref(new) == oldpref)
761 * preferred router may be changed, so relocate
763 * XXX: calling TAILQ_REMOVE directly is a bad manner.
764 * However, since defrtrlist_del() has many side
765 * effects, we intentionally do so here.
766 * defrouter_select() below will handle routing
769 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
776 /* entry does not exist */
777 if (new->rtlifetime == 0)
780 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
783 bzero(n, sizeof(*n));
788 * Insert the new router in the Default Router List;
789 * The Default Router List should be in the descending order
790 * of router-preferece. Routers with the same preference are
791 * sorted in the arriving time order.
794 /* insert at the end of the group */
795 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
796 if (rtpref(n) > rtpref(dr))
800 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
802 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
809 static struct nd_pfxrouter *
810 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
812 struct nd_pfxrouter *search;
814 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
815 if (search->router == dr)
823 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
825 struct nd_pfxrouter *new;
827 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
830 bzero(new, sizeof(*new));
833 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
835 pfxlist_onlink_check();
839 pfxrtr_del(struct nd_pfxrouter *pfr)
841 LIST_REMOVE(pfr, pfr_entry);
846 nd6_prefix_lookup(struct nd_prefixctl *key)
848 struct nd_prefix *search;
850 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
851 if (key->ndpr_ifp == search->ndpr_ifp &&
852 key->ndpr_plen == search->ndpr_plen &&
853 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
854 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
863 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
864 struct nd_prefix **newp)
866 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 for (i = 0; i < 4; i++)
894 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
895 new->ndpr_mask.s6_addr32[i];
897 /* link ndpr_entry to nd_prefix list */
898 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
900 /* ND_OPT_PI_FLAG_ONLINK processing */
901 if (new->ndpr_raf_onlink) {
904 if ((e = nd6_prefix_onlink(new)) != 0) {
905 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
906 "the prefix %s/%d on-link on %s (errno=%d)\n",
907 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
908 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
909 /* proceed anyway. XXX: is it correct? */
920 prelist_remove(struct nd_prefix *pr)
922 struct nd_pfxrouter *pfr, *next;
924 char ip6buf[INET6_ADDRSTRLEN];
926 /* make sure to invalidate the prefix until it is really freed. */
931 * Though these flags are now meaningless, we'd rather keep the value
932 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
933 * when executing "ndp -p".
936 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
937 (e = nd6_prefix_offlink(pr)) != 0) {
938 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
940 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
941 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
942 /* what should we do? */
945 if (pr->ndpr_refcnt > 0)
946 return; /* notice here? */
948 /* unlink ndpr_entry from nd_prefix list */
949 LIST_REMOVE(pr, ndpr_entry);
951 /* free list of routers that adversed the prefix */
952 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next) {
957 pfxlist_onlink_check();
965 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
966 struct mbuf *m, int mcast)
968 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
970 struct ifnet *ifp = new->ndpr_ifp;
971 struct nd_prefix *pr;
975 struct in6_addrlifetime lt6_tmp;
976 char ip6buf[INET6_ADDRSTRLEN];
981 * Authenticity for NA consists authentication for
982 * both IP header and IP datagrams, doesn't it ?
984 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
985 auth = ((m->m_flags & M_AUTHIPHDR) &&
986 (m->m_flags & M_AUTHIPDGM));
990 if ((pr = nd6_prefix_lookup(new)) != NULL) {
992 * nd6_prefix_lookup() ensures that pr and new have the same
993 * prefix on a same interface.
997 * Update prefix information. Note that the on-link (L) bit
998 * and the autonomous (A) bit should NOT be changed from 1
1001 if (new->ndpr_raf_onlink == 1)
1002 pr->ndpr_raf_onlink = 1;
1003 if (new->ndpr_raf_auto == 1)
1004 pr->ndpr_raf_auto = 1;
1005 if (new->ndpr_raf_onlink) {
1006 pr->ndpr_vltime = new->ndpr_vltime;
1007 pr->ndpr_pltime = new->ndpr_pltime;
1008 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1009 pr->ndpr_lastupdate = time_uptime;
1012 if (new->ndpr_raf_onlink &&
1013 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1016 if ((e = nd6_prefix_onlink(pr)) != 0) {
1018 "prelist_update: failed to make "
1019 "the prefix %s/%d on-link on %s "
1022 &pr->ndpr_prefix.sin6_addr),
1023 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1024 /* proceed anyway. XXX: is it correct? */
1028 if (dr && pfxrtr_lookup(pr, dr) == NULL)
1031 struct nd_prefix *newpr = NULL;
1035 if (new->ndpr_vltime == 0)
1037 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1040 error = nd6_prelist_add(new, dr, &newpr);
1041 if (error != 0 || newpr == NULL) {
1042 nd6log((LOG_NOTICE, "prelist_update: "
1043 "nd6_prelist_add failed for %s/%d on %s "
1044 "errno=%d, returnpr=%p\n",
1045 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1046 new->ndpr_plen, if_name(new->ndpr_ifp),
1048 goto end; /* we should just give up in this case. */
1052 * XXX: from the ND point of view, we can ignore a prefix
1053 * with the on-link bit being zero. However, we need a
1054 * prefix structure for references from autoconfigured
1055 * addresses. Thus, we explicitly make sure that the prefix
1056 * itself expires now.
1058 if (newpr->ndpr_raf_onlink == 0) {
1059 newpr->ndpr_vltime = 0;
1060 newpr->ndpr_pltime = 0;
1061 in6_init_prefix_ltimes(newpr);
1068 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1069 * Note that pr must be non NULL at this point.
1072 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1073 if (!new->ndpr_raf_auto)
1077 * 5.5.3 (b). the link-local prefix should have been ignored in
1081 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1082 if (new->ndpr_pltime > new->ndpr_vltime) {
1083 error = EINVAL; /* XXX: won't be used */
1088 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1089 * an address configured by stateless autoconfiguration already in the
1090 * list of addresses associated with the interface, and the Valid
1091 * Lifetime is not 0, form an address. We first check if we have
1092 * a matching prefix.
1093 * Note: we apply a clarification in rfc2462bis-02 here. We only
1094 * consider autoconfigured addresses while RFC2462 simply said
1098 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1099 struct in6_ifaddr *ifa6;
1100 u_int32_t remaininglifetime;
1102 if (ifa->ifa_addr->sa_family != AF_INET6)
1105 ifa6 = (struct in6_ifaddr *)ifa;
1108 * We only consider autoconfigured addresses as per rfc2462bis.
1110 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1114 * Spec is not clear here, but I believe we should concentrate
1115 * on unicast (i.e. not anycast) addresses.
1116 * XXX: other ia6_flags? detached or duplicated?
1118 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1122 * Ignore the address if it is not associated with a prefix
1123 * or is associated with a prefix that is different from this
1124 * one. (pr is never NULL here)
1126 if (ifa6->ia6_ndpr != pr)
1129 if (ia6_match == NULL) /* remember the first one */
1133 * An already autoconfigured address matched. Now that we
1134 * are sure there is at least one matched address, we can
1135 * proceed to 5.5.3. (e): update the lifetimes according to the
1136 * "two hours" rule and the privacy extension.
1137 * We apply some clarifications in rfc2462bis:
1138 * - use remaininglifetime instead of storedlifetime as a
1140 * - remove the dead code in the "two-hour" rule
1142 #define TWOHOUR (120*60)
1143 lt6_tmp = ifa6->ia6_lifetime;
1145 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1146 remaininglifetime = ND6_INFINITE_LIFETIME;
1147 else if (time_uptime - ifa6->ia6_updatetime >
1148 lt6_tmp.ia6t_vltime) {
1150 * The case of "invalid" address. We should usually
1151 * not see this case.
1153 remaininglifetime = 0;
1155 remaininglifetime = lt6_tmp.ia6t_vltime -
1156 (time_uptime - ifa6->ia6_updatetime);
1158 /* when not updating, keep the current stored lifetime. */
1159 lt6_tmp.ia6t_vltime = remaininglifetime;
1161 if (TWOHOUR < new->ndpr_vltime ||
1162 remaininglifetime < new->ndpr_vltime) {
1163 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1164 } else if (remaininglifetime <= TWOHOUR) {
1166 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1170 * new->ndpr_vltime <= TWOHOUR &&
1171 * TWOHOUR < remaininglifetime
1173 lt6_tmp.ia6t_vltime = TWOHOUR;
1176 /* The 2 hour rule is not imposed for preferred lifetime. */
1177 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1179 in6_init_address_ltimes(pr, <6_tmp);
1182 * We need to treat lifetimes for temporary addresses
1183 * differently, according to
1184 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1185 * we only update the lifetimes when they are in the maximum
1188 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1189 u_int32_t maxvltime, maxpltime;
1191 if (V_ip6_temp_valid_lifetime >
1192 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1193 V_ip6_desync_factor)) {
1194 maxvltime = V_ip6_temp_valid_lifetime -
1195 (time_uptime - ifa6->ia6_createtime) -
1196 V_ip6_desync_factor;
1199 if (V_ip6_temp_preferred_lifetime >
1200 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1201 V_ip6_desync_factor)) {
1202 maxpltime = V_ip6_temp_preferred_lifetime -
1203 (time_uptime - ifa6->ia6_createtime) -
1204 V_ip6_desync_factor;
1208 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1209 lt6_tmp.ia6t_vltime > maxvltime) {
1210 lt6_tmp.ia6t_vltime = maxvltime;
1212 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1213 lt6_tmp.ia6t_pltime > maxpltime) {
1214 lt6_tmp.ia6t_pltime = maxpltime;
1217 ifa6->ia6_lifetime = lt6_tmp;
1218 ifa6->ia6_updatetime = time_uptime;
1220 IF_ADDR_RUNLOCK(ifp);
1221 if (ia6_match == NULL && new->ndpr_vltime) {
1225 * 5.5.3 (d) (continued)
1226 * No address matched and the valid lifetime is non-zero.
1227 * Create a new address.
1231 * Prefix Length check:
1232 * If the sum of the prefix length and interface identifier
1233 * length does not equal 128 bits, the Prefix Information
1234 * option MUST be ignored. The length of the interface
1235 * identifier is defined in a separate link-type specific
1238 ifidlen = in6_if2idlen(ifp);
1240 /* this should not happen, so we always log it. */
1241 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1245 if (ifidlen + pr->ndpr_plen != 128) {
1247 "prelist_update: invalid prefixlen "
1248 "%d for %s, ignored\n",
1249 pr->ndpr_plen, if_name(ifp)));
1253 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1255 * note that we should use pr (not new) for reference.
1262 * When a new public address is created as described
1263 * in RFC2462, also create a new temporary address.
1266 * When an interface connects to a new link, a new
1267 * randomized interface identifier should be generated
1268 * immediately together with a new set of temporary
1269 * addresses. Thus, we specifiy 1 as the 2nd arg of
1272 if (V_ip6_use_tempaddr) {
1274 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1275 nd6log((LOG_NOTICE, "prelist_update: "
1276 "failed to create a temporary "
1277 "address, errno=%d\n",
1281 ifa_free(&ia6->ia_ifa);
1284 * A newly added address might affect the status
1285 * of other addresses, so we check and update it.
1286 * XXX: what if address duplication happens?
1288 pfxlist_onlink_check();
1290 /* just set an error. do not bark here. */
1291 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1300 * A supplement function used in the on-link detection below;
1301 * detect if a given prefix has a (probably) reachable advertising router.
1302 * XXX: lengthy function name...
1304 static struct nd_pfxrouter *
1305 find_pfxlist_reachable_router(struct nd_prefix *pr)
1307 struct nd_pfxrouter *pfxrtr;
1311 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1312 IF_AFDATA_RLOCK(pfxrtr->router->ifp);
1313 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1314 IF_AFDATA_RUNLOCK(pfxrtr->router->ifp);
1317 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1326 * Check if each prefix in the prefix list has at least one available router
1327 * that advertised the prefix (a router is "available" if its neighbor cache
1328 * entry is reachable or probably reachable).
1329 * If the check fails, the prefix may be off-link, because, for example,
1330 * we have moved from the network but the lifetime of the prefix has not
1331 * expired yet. So we should not use the prefix if there is another prefix
1332 * that has an available router.
1333 * But, if there is no prefix that has an available router, we still regards
1334 * all the prefixes as on-link. This is because we can't tell if all the
1335 * routers are simply dead or if we really moved from the network and there
1336 * is no router around us.
1339 pfxlist_onlink_check()
1341 struct nd_prefix *pr;
1342 struct in6_ifaddr *ifa;
1343 struct nd_defrouter *dr;
1344 struct nd_pfxrouter *pfxrtr = NULL;
1347 * Check if there is a prefix that has a reachable advertising
1350 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1351 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1356 * If we have no such prefix, check whether we still have a router
1357 * that does not advertise any prefixes.
1360 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1361 struct nd_prefix *pr0;
1363 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1364 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1371 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
1373 * There is at least one prefix that has a reachable router,
1374 * or at least a router which probably does not advertise
1375 * any prefixes. The latter would be the case when we move
1376 * to a new link where we have a router that does not provide
1377 * prefixes and we configure an address by hand.
1378 * Detach prefixes which have no reachable advertising
1379 * router, and attach other prefixes.
1381 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1382 /* XXX: a link-local prefix should never be detached */
1383 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1387 * we aren't interested in prefixes without the L bit
1390 if (pr->ndpr_raf_onlink == 0)
1393 if (pr->ndpr_raf_auto == 0)
1396 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1397 find_pfxlist_reachable_router(pr) == NULL)
1398 pr->ndpr_stateflags |= NDPRF_DETACHED;
1399 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1400 find_pfxlist_reachable_router(pr) != 0)
1401 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1404 /* there is no prefix that has a reachable router */
1405 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1406 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1409 if (pr->ndpr_raf_onlink == 0)
1412 if (pr->ndpr_raf_auto == 0)
1415 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1416 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1421 * Remove each interface route associated with a (just) detached
1422 * prefix, and reinstall the interface route for a (just) attached
1423 * prefix. Note that all attempt of reinstallation does not
1424 * necessarily success, when a same prefix is shared among multiple
1425 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1426 * so we don't have to care about them.
1428 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1430 char ip6buf[INET6_ADDRSTRLEN];
1432 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1435 if (pr->ndpr_raf_onlink == 0)
1438 if (pr->ndpr_raf_auto == 0)
1441 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1442 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1443 if ((e = nd6_prefix_offlink(pr)) != 0) {
1445 "pfxlist_onlink_check: failed to "
1446 "make %s/%d offlink, errno=%d\n",
1448 &pr->ndpr_prefix.sin6_addr),
1452 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1453 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1454 pr->ndpr_raf_onlink) {
1455 if ((e = nd6_prefix_onlink(pr)) != 0) {
1457 "pfxlist_onlink_check: failed to "
1458 "make %s/%d onlink, errno=%d\n",
1460 &pr->ndpr_prefix.sin6_addr),
1467 * Changes on the prefix status might affect address status as well.
1468 * Make sure that all addresses derived from an attached prefix are
1469 * attached, and that all addresses derived from a detached prefix are
1470 * detached. Note, however, that a manually configured address should
1471 * always be attached.
1472 * The precise detection logic is same as the one for prefixes.
1474 * XXXRW: in6_ifaddrhead locking.
1476 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1477 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1480 if (ifa->ia6_ndpr == NULL) {
1482 * This can happen when we first configure the address
1483 * (i.e. the address exists, but the prefix does not).
1484 * XXX: complicated relationships...
1489 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1493 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1494 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1497 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1500 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1501 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1502 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1503 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1504 nd6_dad_start((struct ifaddr *)ifa, 0);
1507 ifa->ia6_flags |= IN6_IFF_DETACHED;
1512 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1513 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1516 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1517 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1518 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1519 /* Do we need a delay in this case? */
1520 nd6_dad_start((struct ifaddr *)ifa, 0);
1527 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
1529 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1530 struct radix_node_head *rnh;
1532 struct sockaddr_in6 mask6;
1534 int error, a_failure, fibnum;
1537 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1538 * ifa->ifa_rtrequest = nd6_rtrequest;
1540 bzero(&mask6, sizeof(mask6));
1541 mask6.sin6_len = sizeof(mask6);
1542 mask6.sin6_addr = pr->ndpr_mask;
1543 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1546 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1549 error = in6_rtrequest(RTM_ADD,
1550 (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr,
1551 (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
1553 KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
1554 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
1557 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
1558 /* XXX what if rhn == NULL? */
1559 RADIX_NODE_HEAD_LOCK(rnh);
1561 if (rt_setgate(rt, rt_key(rt),
1562 (struct sockaddr *)&null_sdl) == 0) {
1563 struct sockaddr_dl *dl;
1565 dl = (struct sockaddr_dl *)rt->rt_gateway;
1566 dl->sdl_type = rt->rt_ifp->if_type;
1567 dl->sdl_index = rt->rt_ifp->if_index;
1569 RADIX_NODE_HEAD_UNLOCK(rnh);
1570 nd6_rtmsg(RTM_ADD, rt);
1572 pr->ndpr_stateflags |= NDPRF_ONLINK;
1574 char ip6buf[INET6_ADDRSTRLEN];
1575 char ip6bufg[INET6_ADDRSTRLEN];
1576 char ip6bufm[INET6_ADDRSTRLEN];
1577 struct sockaddr_in6 *sin6;
1579 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1580 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
1581 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
1582 "flags=%lx errno = %d\n",
1583 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1584 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1585 ip6_sprintf(ip6bufg, &sin6->sin6_addr),
1586 ip6_sprintf(ip6bufm, &mask6.sin6_addr),
1589 /* Save last error to return, see rtinit(). */
1600 /* Return the last error we got. */
1605 nd6_prefix_onlink(struct nd_prefix *pr)
1608 struct ifnet *ifp = pr->ndpr_ifp;
1609 struct nd_prefix *opr;
1611 char ip6buf[INET6_ADDRSTRLEN];
1614 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1616 "nd6_prefix_onlink: %s/%d is already on-link\n",
1617 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1623 * Add the interface route associated with the prefix. Before
1624 * installing the route, check if there's the same prefix on another
1625 * interface, and the prefix has already installed the interface route.
1626 * Although such a configuration is expected to be rare, we explicitly
1629 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1633 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1636 if (opr->ndpr_plen == pr->ndpr_plen &&
1637 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1638 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1643 * We prefer link-local addresses as the associated interface address.
1645 /* search for a link-local addr */
1646 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1647 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1649 /* XXX: freebsd does not have ifa_ifwithaf */
1651 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1652 if (ifa->ifa_addr->sa_family == AF_INET6)
1657 IF_ADDR_RUNLOCK(ifp);
1658 /* should we care about ia6_flags? */
1662 * This can still happen, when, for example, we receive an RA
1663 * containing a prefix with the L bit set and the A bit clear,
1664 * after removing all IPv6 addresses on the receiving
1665 * interface. This should, of course, be rare though.
1668 "nd6_prefix_onlink: failed to find any ifaddr"
1669 " to add route for a prefix(%s/%d) on %s\n",
1670 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1671 pr->ndpr_plen, if_name(ifp)));
1675 error = nd6_prefix_onlink_rtrequest(pr, ifa);
1684 nd6_prefix_offlink(struct nd_prefix *pr)
1687 struct ifnet *ifp = pr->ndpr_ifp;
1688 struct nd_prefix *opr;
1689 struct sockaddr_in6 sa6, mask6;
1691 char ip6buf[INET6_ADDRSTRLEN];
1692 int fibnum, a_failure;
1695 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1697 "nd6_prefix_offlink: %s/%d is already off-link\n",
1698 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1703 bzero(&sa6, sizeof(sa6));
1704 sa6.sin6_family = AF_INET6;
1705 sa6.sin6_len = sizeof(sa6);
1706 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1707 sizeof(struct in6_addr));
1708 bzero(&mask6, sizeof(mask6));
1709 mask6.sin6_family = AF_INET6;
1710 mask6.sin6_len = sizeof(sa6);
1711 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1714 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1716 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1717 (struct sockaddr *)&mask6, 0, &rt, fibnum);
1719 /* report the route deletion to the routing socket. */
1721 nd6_rtmsg(RTM_DELETE, rt);
1723 /* Save last error to return, see rtinit(). */
1733 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1736 * There might be the same prefix on another interface,
1737 * the prefix which could not be on-link just because we have
1738 * the interface route (see comments in nd6_prefix_onlink).
1739 * If there's one, try to make the prefix on-link on the
1742 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1746 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1750 * KAME specific: detached prefixes should not be
1753 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1756 if (opr->ndpr_plen == pr->ndpr_plen &&
1757 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1758 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1761 if ((e = nd6_prefix_onlink(opr)) != 0) {
1763 "nd6_prefix_offlink: failed to "
1764 "recover a prefix %s/%d from %s "
1765 "to %s (errno = %d)\n",
1767 &opr->ndpr_prefix.sin6_addr),
1768 opr->ndpr_plen, if_name(ifp),
1769 if_name(opr->ndpr_ifp), e));
1775 /* XXX: can we still set the NDPRF_ONLINK flag? */
1777 "nd6_prefix_offlink: failed to delete route: "
1778 "%s/%d on %s (errno = %d)\n",
1779 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
1780 if_name(ifp), error));
1784 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
1785 (struct sockaddr *)&mask6, LLE_STATIC);
1790 static struct in6_ifaddr *
1791 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1793 struct ifnet *ifp = pr->ndpr_ifp;
1795 struct in6_aliasreq ifra;
1796 struct in6_ifaddr *ia, *ib;
1798 struct in6_addr mask;
1799 int prefixlen = pr->ndpr_plen;
1801 char ip6buf[INET6_ADDRSTRLEN];
1803 in6_prefixlen2mask(&mask, prefixlen);
1806 * find a link-local address (will be interface ID).
1807 * Is it really mandatory? Theoretically, a global or a site-local
1808 * address can be configured without a link-local address, if we
1809 * have a unique interface identifier...
1811 * it is not mandatory to have a link-local address, we can generate
1812 * interface identifier on the fly. we do this because:
1813 * (1) it should be the easiest way to find interface identifier.
1814 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1815 * for multiple addresses on a single interface, and possible shortcut
1816 * of DAD. we omitted DAD for this reason in the past.
1817 * (3) a user can prevent autoconfiguration of global address
1818 * by removing link-local address by hand (this is partly because we
1819 * don't have other way to control the use of IPv6 on an interface.
1820 * this has been our design choice - cf. NRL's "ifconfig auto").
1821 * (4) it is easier to manage when an interface has addresses
1822 * with the same interface identifier, than to have multiple addresses
1823 * with different interface identifiers.
1825 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1827 ib = (struct in6_ifaddr *)ifa;
1831 /* prefixlen + ifidlen must be equal to 128 */
1832 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1833 if (prefixlen != plen0) {
1835 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1836 "(prefix=%d ifid=%d)\n",
1837 if_name(ifp), prefixlen, 128 - plen0));
1843 bzero(&ifra, sizeof(ifra));
1845 * in6_update_ifa() does not use ifra_name, but we accurately set it
1848 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1849 ifra.ifra_addr.sin6_family = AF_INET6;
1850 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1852 ifra.ifra_addr.sin6_addr = pr->ndpr_prefix.sin6_addr;
1853 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1854 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1855 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1856 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1859 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1860 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1861 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1862 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1863 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1864 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1865 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1866 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1869 /* new prefix mask. */
1870 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1871 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1872 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1873 sizeof(ifra.ifra_prefixmask.sin6_addr));
1876 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1877 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1879 /* XXX: scope zone ID? */
1881 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1884 * Make sure that we do not have this address already. This should
1885 * usually not happen, but we can still see this case, e.g., if we
1886 * have manually configured the exact address to be configured.
1888 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1889 &ifra.ifra_addr.sin6_addr);
1892 /* this should be rare enough to make an explicit log */
1893 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1894 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1899 * Allocate ifaddr structure, link into chain, etc.
1900 * If we are going to create a new address upon receiving a multicasted
1901 * RA, we need to impose a random delay before starting DAD.
1902 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1906 updateflags |= IN6_IFAUPDATE_DADDELAY;
1907 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1909 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1910 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1911 if_name(ifp), error));
1912 return (NULL); /* ifaddr must not have been allocated. */
1915 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1917 * XXXRW: Assumption of non-NULLness here might not be true with
1918 * fine-grained locking -- should we validate it? Or just return
1919 * earlier ifa rather than looking it up again?
1921 return (ia); /* this is always non-NULL and referenced. */
1925 * ia0 - corresponding public address
1928 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
1930 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1931 struct in6_ifaddr *newia, *ia;
1932 struct in6_aliasreq ifra;
1934 int trylimit = 3; /* XXX: adhoc value */
1936 u_int32_t randid[2];
1937 time_t vltime0, pltime0;
1939 bzero(&ifra, sizeof(ifra));
1940 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1941 ifra.ifra_addr = ia0->ia_addr;
1942 /* copy prefix mask */
1943 ifra.ifra_prefixmask = ia0->ia_prefixmask;
1944 /* clear the old IFID */
1945 for (i = 0; i < 4; i++) {
1946 ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
1947 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1951 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
1952 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
1953 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
1957 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1958 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1959 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1960 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1963 * in6_get_tmpifid() quite likely provided a unique interface ID.
1964 * However, we may still have a chance to see collision, because
1965 * there may be a time lag between generation of the ID and generation
1966 * of the address. So, we'll do one more sanity check.
1969 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1970 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1971 &ifra.ifra_addr.sin6_addr)) {
1972 if (trylimit-- == 0) {
1973 IN6_IFADDR_RUNLOCK();
1975 * Give up. Something strange should have
1978 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
1979 "find a unique random IFID\n"));
1982 IN6_IFADDR_RUNLOCK();
1987 IN6_IFADDR_RUNLOCK();
1990 * The Valid Lifetime is the lower of the Valid Lifetime of the
1991 * public address or TEMP_VALID_LIFETIME.
1992 * The Preferred Lifetime is the lower of the Preferred Lifetime
1993 * of the public address or TEMP_PREFERRED_LIFETIME -
1996 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1997 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
1998 (ia0->ia6_lifetime.ia6t_vltime -
1999 (time_uptime - ia0->ia6_updatetime));
2000 if (vltime0 > V_ip6_temp_valid_lifetime)
2001 vltime0 = V_ip6_temp_valid_lifetime;
2003 vltime0 = V_ip6_temp_valid_lifetime;
2004 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2005 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2006 (ia0->ia6_lifetime.ia6t_pltime -
2007 (time_uptime - ia0->ia6_updatetime));
2008 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2009 pltime0 = V_ip6_temp_preferred_lifetime -
2010 V_ip6_desync_factor;
2013 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2014 ifra.ifra_lifetime.ia6t_vltime = vltime0;
2015 ifra.ifra_lifetime.ia6t_pltime = pltime0;
2018 * A temporary address is created only if this calculated Preferred
2019 * Lifetime is greater than REGEN_ADVANCE time units.
2021 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2024 /* XXX: scope zone ID? */
2026 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2028 /* allocate ifaddr structure, link into chain, etc. */
2031 updateflags |= IN6_IFAUPDATE_DADDELAY;
2032 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2035 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2036 if (newia == NULL) { /* XXX: can it happen? */
2038 "in6_tmpifadd: ifa update succeeded, but we got "
2040 return (EINVAL); /* XXX */
2042 newia->ia6_ndpr = ia0->ia6_ndpr;
2043 newia->ia6_ndpr->ndpr_refcnt++;
2044 ifa_free(&newia->ia_ifa);
2047 * A newly added address might affect the status of other addresses.
2048 * XXX: when the temporary address is generated with a new public
2049 * address, the onlink check is redundant. However, it would be safe
2050 * to do the check explicitly everywhere a new address is generated,
2051 * and, in fact, we surely need the check when we create a new
2052 * temporary address due to deprecation of an old temporary address.
2054 pfxlist_onlink_check();
2060 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2062 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2063 ndpr->ndpr_preferred = 0;
2065 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
2066 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2067 ndpr->ndpr_expire = 0;
2069 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
2075 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
2077 /* init ia6t_expire */
2078 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2079 lt6->ia6t_expire = 0;
2081 lt6->ia6t_expire = time_uptime;
2082 lt6->ia6t_expire += lt6->ia6t_vltime;
2085 /* init ia6t_preferred */
2086 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2087 lt6->ia6t_preferred = 0;
2089 lt6->ia6t_preferred = time_uptime;
2090 lt6->ia6t_preferred += lt6->ia6t_pltime;
2095 * Delete all the routing table entries that use the specified gateway.
2096 * XXX: this function causes search through all entries of routing table, so
2097 * it shouldn't be called when acting as a router.
2100 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2102 struct radix_node_head *rnh;
2105 /* We'll care only link-local addresses */
2106 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2109 /* XXX Do we really need to walk any but the default FIB? */
2110 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
2111 rnh = rt_tables_get_rnh(fibnum, AF_INET6);
2115 RADIX_NODE_HEAD_LOCK(rnh);
2116 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
2117 RADIX_NODE_HEAD_UNLOCK(rnh);
2122 rt6_deleteroute(struct radix_node *rn, void *arg)
2124 #define SIN6(s) ((struct sockaddr_in6 *)s)
2125 struct rtentry *rt = (struct rtentry *)rn;
2126 struct in6_addr *gate = (struct in6_addr *)arg;
2128 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2131 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2136 * Do not delete a static route.
2137 * XXX: this seems to be a bit ad-hoc. Should we consider the
2138 * 'cloned' bit instead?
2140 if ((rt->rt_flags & RTF_STATIC) != 0)
2144 * We delete only host route. This means, in particular, we don't
2145 * delete default route.
2147 if ((rt->rt_flags & RTF_HOST) == 0)
2150 return (in6_rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
2151 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum));
2156 nd6_setdefaultiface(int ifindex)
2160 if (ifindex < 0 || V_if_index < ifindex)
2162 if (ifindex != 0 && !ifnet_byindex(ifindex))
2165 if (V_nd6_defifindex != ifindex) {
2166 V_nd6_defifindex = ifindex;
2167 if (V_nd6_defifindex > 0)
2168 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2170 V_nd6_defifp = NULL;
2173 * Our current implementation assumes one-to-one maping between
2174 * interfaces and links, so it would be natural to use the
2175 * default interface as the default link.
2177 scope6_setdefault(V_nd6_defifp);