2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/rwlock.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
53 #include <net/if_types.h>
54 #include <net/if_dl.h>
55 #include <net/route.h>
56 #include <net/radix.h>
59 #include <netinet/in.h>
60 #include <net/if_llatbl.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet6/in6_ifattach.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/nd6.h>
66 #include <netinet/icmp6.h>
67 #include <netinet6/scope6_var.h>
69 static int rtpref(struct nd_defrouter *);
70 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
71 static int prelist_update __P((struct nd_prefixctl *, struct nd_defrouter *,
73 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
74 static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *,
75 struct nd_defrouter *));
76 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
77 static void pfxrtr_del(struct nd_pfxrouter *);
78 static struct nd_pfxrouter *find_pfxlist_reachable_router
80 static void defrouter_delreq(struct nd_defrouter *);
81 static void nd6_rtmsg(int, struct rtentry *);
83 static int in6_init_prefix_ltimes(struct nd_prefix *);
84 static void in6_init_address_ltimes __P((struct nd_prefix *,
85 struct in6_addrlifetime *));
87 static int rt6_deleteroute(struct radix_node *, void *);
89 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
90 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
92 static VNET_DEFINE(struct ifnet *, nd6_defifp);
93 VNET_DEFINE(int, nd6_defifindex);
94 #define V_nd6_defifp VNET(nd6_defifp)
96 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
98 VNET_DEFINE(int, ip6_desync_factor);
99 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
100 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
102 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
104 /* RTPREF_MEDIUM has to be 0! */
105 #define RTPREF_HIGH 1
106 #define RTPREF_MEDIUM 0
107 #define RTPREF_LOW (-1)
108 #define RTPREF_RESERVED (-2)
109 #define RTPREF_INVALID (-3) /* internal */
112 * Receive Router Solicitation Message - just for routers.
113 * Router solicitation/advertisement is mostly managed by userland program
114 * (rtadvd) so here we have no function like nd6_ra_output().
119 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
121 struct ifnet *ifp = m->m_pkthdr.rcvif;
122 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
123 struct nd_router_solicit *nd_rs;
124 struct in6_addr saddr6 = ip6->ip6_src;
127 union nd_opts ndopts;
128 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
131 * Accept RS only when V_ip6_forwarding=1 and the interface has
132 * no ND6_IFF_ACCEPT_RTADV.
134 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
138 if (ip6->ip6_hlim != 255) {
140 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
141 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
142 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
147 * Don't update the neighbor cache, if src = ::.
148 * This indicates that the src has no IP address assigned yet.
150 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
153 #ifndef PULLDOWN_TEST
154 IP6_EXTHDR_CHECK(m, off, icmp6len,);
155 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
157 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
159 ICMP6STAT_INC(icp6s_tooshort);
164 icmp6len -= sizeof(*nd_rs);
165 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
166 if (nd6_options(&ndopts) < 0) {
168 "nd6_rs_input: invalid ND option, ignored\n"));
169 /* nd6_options have incremented stats */
173 if (ndopts.nd_opts_src_lladdr) {
174 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
175 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
178 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
180 "nd6_rs_input: lladdrlen mismatch for %s "
181 "(if %d, RS packet %d)\n",
182 ip6_sprintf(ip6bufs, &saddr6),
183 ifp->if_addrlen, lladdrlen - 2));
187 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
194 ICMP6STAT_INC(icp6s_badrs);
199 * Receive Router Advertisement Message.
202 * TODO: on-link bit on prefix information
203 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
206 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
208 struct ifnet *ifp = m->m_pkthdr.rcvif;
209 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
210 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
211 struct nd_router_advert *nd_ra;
212 struct in6_addr saddr6 = ip6->ip6_src;
214 union nd_opts ndopts;
215 struct nd_defrouter *dr;
216 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
219 * We only accept RAs only when the per-interface flag
220 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
222 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
225 if (ip6->ip6_hlim != 255) {
227 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
228 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
229 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
233 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
235 "nd6_ra_input: src %s is not link-local\n",
236 ip6_sprintf(ip6bufs, &saddr6)));
240 #ifndef PULLDOWN_TEST
241 IP6_EXTHDR_CHECK(m, off, icmp6len,);
242 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
244 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
246 ICMP6STAT_INC(icp6s_tooshort);
251 icmp6len -= sizeof(*nd_ra);
252 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
253 if (nd6_options(&ndopts) < 0) {
255 "nd6_ra_input: invalid ND option, ignored\n"));
256 /* nd6_options have incremented stats */
261 struct nd_defrouter dr0;
262 u_int32_t advreachable = nd_ra->nd_ra_reachable;
264 /* remember if this is a multicasted advertisement */
265 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
268 bzero(&dr0, sizeof(dr0));
270 dr0.flags = nd_ra->nd_ra_flags_reserved;
272 * Effectively-disable routes from RA messages when
273 * ND6_IFF_NO_RADR enabled on the receiving interface or
274 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
276 if (ndi->flags & ND6_IFF_NO_RADR)
278 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
281 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
282 dr0.expire = time_second + dr0.rtlifetime;
284 /* unspecified or not? (RFC 2461 6.3.4) */
286 advreachable = ntohl(advreachable);
287 if (advreachable <= MAX_REACHABLE_TIME &&
288 ndi->basereachable != advreachable) {
289 ndi->basereachable = advreachable;
290 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
291 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
294 if (nd_ra->nd_ra_retransmit)
295 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
296 if (nd_ra->nd_ra_curhoplimit)
297 ndi->chlim = nd_ra->nd_ra_curhoplimit;
298 dr = defrtrlist_update(&dr0);
304 if (ndopts.nd_opts_pi) {
305 struct nd_opt_hdr *pt;
306 struct nd_opt_prefix_info *pi = NULL;
307 struct nd_prefixctl pr;
309 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
310 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
311 pt = (struct nd_opt_hdr *)((caddr_t)pt +
312 (pt->nd_opt_len << 3))) {
313 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
315 pi = (struct nd_opt_prefix_info *)pt;
317 if (pi->nd_opt_pi_len != 4) {
319 "nd6_ra_input: invalid option "
320 "len %d for prefix information option, "
321 "ignored\n", pi->nd_opt_pi_len));
325 if (128 < pi->nd_opt_pi_prefix_len) {
327 "nd6_ra_input: invalid prefix "
328 "len %d for prefix information option, "
329 "ignored\n", pi->nd_opt_pi_prefix_len));
333 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
334 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
336 "nd6_ra_input: invalid prefix "
339 &pi->nd_opt_pi_prefix)));
343 bzero(&pr, sizeof(pr));
344 pr.ndpr_prefix.sin6_family = AF_INET6;
345 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
346 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
347 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
349 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
350 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
351 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
352 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
353 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
354 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
355 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
356 (void)prelist_update(&pr, dr, m, mcast);
363 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
367 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
370 if (mtu < IPV6_MMTU) {
371 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
372 "mtu=%lu sent from %s, ignoring\n",
373 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
378 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
379 ? ndi->maxmtu : ifp->if_mtu;
381 int change = (ndi->linkmtu != mtu);
384 if (change) /* in6_maxmtu may change */
387 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
388 "mtu=%lu sent from %s; "
389 "exceeds maxmtu %lu, ignoring\n",
390 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
397 * Source link layer address
403 if (ndopts.nd_opts_src_lladdr) {
404 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
405 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
408 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
410 "nd6_ra_input: lladdrlen mismatch for %s "
411 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
412 ifp->if_addrlen, lladdrlen - 2));
416 nd6_cache_lladdr(ifp, &saddr6, lladdr,
417 lladdrlen, ND_ROUTER_ADVERT, 0);
420 * Installing a link-layer address might change the state of the
421 * router's neighbor cache, which might also affect our on-link
422 * detection of adveritsed prefixes.
424 pfxlist_onlink_check();
432 ICMP6STAT_INC(icp6s_badra);
437 * default router list proccessing sub routines
440 /* tell the change to user processes watching the routing socket. */
442 nd6_rtmsg(int cmd, struct rtentry *rt)
444 struct rt_addrinfo info;
448 bzero((caddr_t)&info, sizeof(info));
449 info.rti_info[RTAX_DST] = rt_key(rt);
450 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
451 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
455 ifa = TAILQ_FIRST(&ifp->if_addrhead);
456 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
459 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
463 rt_missmsg(cmd, &info, rt->rt_flags, 0);
469 defrouter_addreq(struct nd_defrouter *new)
471 struct sockaddr_in6 def, mask, gate;
472 struct rtentry *newrt = NULL;
476 bzero(&def, sizeof(def));
477 bzero(&mask, sizeof(mask));
478 bzero(&gate, sizeof(gate));
480 def.sin6_len = mask.sin6_len = gate.sin6_len =
481 sizeof(struct sockaddr_in6);
482 def.sin6_family = gate.sin6_family = AF_INET6;
483 gate.sin6_addr = new->rtaddr;
486 error = rtrequest(RTM_ADD, (struct sockaddr *)&def,
487 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
488 RTF_GATEWAY, &newrt);
490 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
499 struct nd_defrouter *
500 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
502 struct nd_defrouter *dr;
504 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
505 dr = TAILQ_NEXT(dr, dr_entry)) {
506 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
510 return (NULL); /* search failed */
514 * Remove the default route for a given router.
515 * This is just a subroutine function for defrouter_select(), and should
516 * not be called from anywhere else.
519 defrouter_delreq(struct nd_defrouter *dr)
521 struct sockaddr_in6 def, mask, gate;
522 struct rtentry *oldrt = NULL;
524 bzero(&def, sizeof(def));
525 bzero(&mask, sizeof(mask));
526 bzero(&gate, sizeof(gate));
528 def.sin6_len = mask.sin6_len = gate.sin6_len =
529 sizeof(struct sockaddr_in6);
530 def.sin6_family = gate.sin6_family = AF_INET6;
531 gate.sin6_addr = dr->rtaddr;
533 rtrequest(RTM_DELETE, (struct sockaddr *)&def,
534 (struct sockaddr *)&gate,
535 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt);
537 nd6_rtmsg(RTM_DELETE, oldrt);
545 * remove all default routes from default router list
548 defrouter_reset(void)
550 struct nd_defrouter *dr;
552 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
553 dr = TAILQ_NEXT(dr, dr_entry))
554 defrouter_delreq(dr);
557 * XXX should we also nuke any default routers in the kernel, by
558 * going through them by rtalloc1()?
563 defrtrlist_del(struct nd_defrouter *dr)
565 struct nd_defrouter *deldr = NULL;
566 struct nd_prefix *pr;
569 * Flush all the routing table entries that use the router
572 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
573 rt6_flush(&dr->rtaddr, dr->ifp);
577 defrouter_delreq(dr);
579 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
582 * Also delete all the pointers to the router in each prefix lists.
584 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
585 struct nd_pfxrouter *pfxrtr;
586 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
589 pfxlist_onlink_check();
592 * If the router is the primary one, choose a new one.
593 * Note that defrouter_select() will remove the current gateway
594 * from the routing table.
603 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
604 * draft-ietf-ipngwg-router-selection:
605 * 1) Routers that are reachable or probably reachable should be preferred.
606 * If we have more than one (probably) reachable router, prefer ones
607 * with the highest router preference.
608 * 2) When no routers on the list are known to be reachable or
609 * probably reachable, routers SHOULD be selected in a round-robin
610 * fashion, regardless of router preference values.
611 * 3) If the Default Router List is empty, assume that all
612 * destinations are on-link.
614 * We assume nd_defrouter is sorted by router preference value.
615 * Since the code below covers both with and without router preference cases,
616 * we do not need to classify the cases by ifdef.
618 * At this moment, we do not try to install more than one default router,
619 * even when the multipath routing is available, because we're not sure about
620 * the benefits for stub hosts comparing to the risk of making the code
621 * complicated and the possibility of introducing bugs.
624 defrouter_select(void)
627 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL;
628 struct llentry *ln = NULL;
631 * Let's handle easy case (3) first:
632 * If default router list is empty, there's nothing to be done.
634 if (!TAILQ_FIRST(&V_nd_defrouter)) {
640 * Search for a (probably) reachable router from the list.
641 * We just pick up the first reachable one (if any), assuming that
642 * the ordering rule of the list described in defrtrlist_update().
644 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
645 dr = TAILQ_NEXT(dr, dr_entry)) {
646 IF_AFDATA_LOCK(dr->ifp);
647 if (selected_dr == NULL &&
648 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
649 ND6_IS_LLINFO_PROBREACH(ln)) {
652 IF_AFDATA_UNLOCK(dr->ifp);
658 if (dr->installed && installed_dr == NULL)
660 else if (dr->installed && installed_dr) {
661 /* this should not happen. warn for diagnosis. */
662 log(LOG_ERR, "defrouter_select: more than one router"
667 * If none of the default routers was found to be reachable,
668 * round-robin the list regardless of preference.
669 * Otherwise, if we have an installed router, check if the selected
670 * (reachable) router should really be preferred to the installed one.
671 * We only prefer the new router when the old one is not reachable
672 * or when the new one has a really higher preference value.
674 if (selected_dr == NULL) {
675 if (installed_dr == NULL || !TAILQ_NEXT(installed_dr, dr_entry))
676 selected_dr = TAILQ_FIRST(&V_nd_defrouter);
678 selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
679 } else if (installed_dr) {
680 IF_AFDATA_LOCK(installed_dr->ifp);
681 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
682 ND6_IS_LLINFO_PROBREACH(ln) &&
683 rtpref(selected_dr) <= rtpref(installed_dr)) {
684 selected_dr = installed_dr;
686 IF_AFDATA_UNLOCK(installed_dr->ifp);
692 * If the selected router is different than the installed one,
693 * remove the installed router and install the selected one.
694 * Note that the selected router is never NULL here.
696 if (installed_dr != selected_dr) {
698 defrouter_delreq(installed_dr);
699 defrouter_addreq(selected_dr);
707 * for default router selection
708 * regards router-preference field as a 2-bit signed integer
711 rtpref(struct nd_defrouter *dr)
713 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
714 case ND_RA_FLAG_RTPREF_HIGH:
715 return (RTPREF_HIGH);
716 case ND_RA_FLAG_RTPREF_MEDIUM:
717 case ND_RA_FLAG_RTPREF_RSV:
718 return (RTPREF_MEDIUM);
719 case ND_RA_FLAG_RTPREF_LOW:
723 * This case should never happen. If it did, it would mean a
724 * serious bug of kernel internal. We thus always bark here.
725 * Or, can we even panic?
727 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags);
728 return (RTPREF_INVALID);
733 static struct nd_defrouter *
734 defrtrlist_update(struct nd_defrouter *new)
736 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.
756 if (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);
778 /* entry does not exist */
779 if (new->rtlifetime == 0) {
784 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
789 bzero(n, sizeof(*n));
794 * Insert the new router in the Default Router List;
795 * The Default Router List should be in the descending order
796 * of router-preferece. Routers with the same preference are
797 * sorted in the arriving time order.
800 /* insert at the end of the group */
801 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
802 dr = TAILQ_NEXT(dr, dr_entry)) {
803 if (rtpref(n) > rtpref(dr))
807 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
809 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
818 static struct nd_pfxrouter *
819 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
821 struct nd_pfxrouter *search;
823 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
824 if (search->router == dr)
832 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
834 struct nd_pfxrouter *new;
836 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
839 bzero(new, sizeof(*new));
842 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
844 pfxlist_onlink_check();
848 pfxrtr_del(struct nd_pfxrouter *pfr)
850 LIST_REMOVE(pfr, pfr_entry);
855 nd6_prefix_lookup(struct nd_prefixctl *key)
857 struct nd_prefix *search;
859 for (search = V_nd_prefix.lh_first;
860 search; search = search->ndpr_next) {
861 if (key->ndpr_ifp == search->ndpr_ifp &&
862 key->ndpr_plen == search->ndpr_plen &&
863 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
864 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
873 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
874 struct nd_prefix **newp)
876 struct nd_prefix *new = NULL;
879 char ip6buf[INET6_ADDRSTRLEN];
881 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
884 bzero(new, sizeof(*new));
885 new->ndpr_ifp = pr->ndpr_ifp;
886 new->ndpr_prefix = pr->ndpr_prefix;
887 new->ndpr_plen = pr->ndpr_plen;
888 new->ndpr_vltime = pr->ndpr_vltime;
889 new->ndpr_pltime = pr->ndpr_pltime;
890 new->ndpr_flags = pr->ndpr_flags;
891 if ((error = in6_init_prefix_ltimes(new)) != 0) {
895 new->ndpr_lastupdate = time_second;
900 LIST_INIT(&new->ndpr_advrtrs);
901 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
902 /* make prefix in the canonical form */
903 for (i = 0; i < 4; i++)
904 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
905 new->ndpr_mask.s6_addr32[i];
908 /* link ndpr_entry to nd_prefix list */
909 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
912 /* ND_OPT_PI_FLAG_ONLINK processing */
913 if (new->ndpr_raf_onlink) {
916 if ((e = nd6_prefix_onlink(new)) != 0) {
917 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
918 "the prefix %s/%d on-link on %s (errno=%d)\n",
919 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
920 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
921 /* proceed anyway. XXX: is it correct? */
932 prelist_remove(struct nd_prefix *pr)
934 struct nd_pfxrouter *pfr, *next;
936 char ip6buf[INET6_ADDRSTRLEN];
938 /* make sure to invalidate the prefix until it is really freed. */
943 * Though these flags are now meaningless, we'd rather keep the value
944 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
945 * when executing "ndp -p".
948 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
949 (e = nd6_prefix_offlink(pr)) != 0) {
950 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
952 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
953 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
954 /* what should we do? */
957 if (pr->ndpr_refcnt > 0)
958 return; /* notice here? */
962 /* unlink ndpr_entry from nd_prefix list */
963 LIST_REMOVE(pr, ndpr_entry);
965 /* free list of routers that adversed the prefix */
966 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
967 next = pfr->pfr_next;
975 pfxlist_onlink_check();
983 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
984 struct mbuf *m, int mcast)
986 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
988 struct ifnet *ifp = new->ndpr_ifp;
989 struct nd_prefix *pr;
994 struct in6_addrlifetime lt6_tmp;
995 char ip6buf[INET6_ADDRSTRLEN];
1000 * Authenticity for NA consists authentication for
1001 * both IP header and IP datagrams, doesn't it ?
1003 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1004 auth = ((m->m_flags & M_AUTHIPHDR) &&
1005 (m->m_flags & M_AUTHIPDGM));
1009 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1011 * nd6_prefix_lookup() ensures that pr and new have the same
1012 * prefix on a same interface.
1016 * Update prefix information. Note that the on-link (L) bit
1017 * and the autonomous (A) bit should NOT be changed from 1
1020 if (new->ndpr_raf_onlink == 1)
1021 pr->ndpr_raf_onlink = 1;
1022 if (new->ndpr_raf_auto == 1)
1023 pr->ndpr_raf_auto = 1;
1024 if (new->ndpr_raf_onlink) {
1025 pr->ndpr_vltime = new->ndpr_vltime;
1026 pr->ndpr_pltime = new->ndpr_pltime;
1027 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1028 pr->ndpr_lastupdate = time_second;
1031 if (new->ndpr_raf_onlink &&
1032 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1035 if ((e = nd6_prefix_onlink(pr)) != 0) {
1037 "prelist_update: failed to make "
1038 "the prefix %s/%d on-link on %s "
1041 &pr->ndpr_prefix.sin6_addr),
1042 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1043 /* proceed anyway. XXX: is it correct? */
1047 if (dr && pfxrtr_lookup(pr, dr) == NULL)
1050 struct nd_prefix *newpr = NULL;
1054 if (new->ndpr_vltime == 0)
1056 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1059 error = nd6_prelist_add(new, dr, &newpr);
1060 if (error != 0 || newpr == NULL) {
1061 nd6log((LOG_NOTICE, "prelist_update: "
1062 "nd6_prelist_add failed for %s/%d on %s "
1063 "errno=%d, returnpr=%p\n",
1064 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1065 new->ndpr_plen, if_name(new->ndpr_ifp),
1067 goto end; /* we should just give up in this case. */
1071 * XXX: from the ND point of view, we can ignore a prefix
1072 * with the on-link bit being zero. However, we need a
1073 * prefix structure for references from autoconfigured
1074 * addresses. Thus, we explicitly make sure that the prefix
1075 * itself expires now.
1077 if (newpr->ndpr_raf_onlink == 0) {
1078 newpr->ndpr_vltime = 0;
1079 newpr->ndpr_pltime = 0;
1080 in6_init_prefix_ltimes(newpr);
1087 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1088 * Note that pr must be non NULL at this point.
1091 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1092 if (!new->ndpr_raf_auto)
1096 * 5.5.3 (b). the link-local prefix should have been ignored in
1100 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1101 if (new->ndpr_pltime > new->ndpr_vltime) {
1102 error = EINVAL; /* XXX: won't be used */
1107 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1108 * an address configured by stateless autoconfiguration already in the
1109 * list of addresses associated with the interface, and the Valid
1110 * Lifetime is not 0, form an address. We first check if we have
1111 * a matching prefix.
1112 * Note: we apply a clarification in rfc2462bis-02 here. We only
1113 * consider autoconfigured addresses while RFC2462 simply said
1117 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1118 struct in6_ifaddr *ifa6;
1119 u_int32_t remaininglifetime;
1121 if (ifa->ifa_addr->sa_family != AF_INET6)
1124 ifa6 = (struct in6_ifaddr *)ifa;
1127 * We only consider autoconfigured addresses as per rfc2462bis.
1129 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1133 * Spec is not clear here, but I believe we should concentrate
1134 * on unicast (i.e. not anycast) addresses.
1135 * XXX: other ia6_flags? detached or duplicated?
1137 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1141 * Ignore the address if it is not associated with a prefix
1142 * or is associated with a prefix that is different from this
1143 * one. (pr is never NULL here)
1145 if (ifa6->ia6_ndpr != pr)
1148 if (ia6_match == NULL) /* remember the first one */
1152 * An already autoconfigured address matched. Now that we
1153 * are sure there is at least one matched address, we can
1154 * proceed to 5.5.3. (e): update the lifetimes according to the
1155 * "two hours" rule and the privacy extension.
1156 * We apply some clarifications in rfc2462bis:
1157 * - use remaininglifetime instead of storedlifetime as a
1159 * - remove the dead code in the "two-hour" rule
1161 #define TWOHOUR (120*60)
1162 lt6_tmp = ifa6->ia6_lifetime;
1164 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1165 remaininglifetime = ND6_INFINITE_LIFETIME;
1166 else if (time_second - ifa6->ia6_updatetime >
1167 lt6_tmp.ia6t_vltime) {
1169 * The case of "invalid" address. We should usually
1170 * not see this case.
1172 remaininglifetime = 0;
1174 remaininglifetime = lt6_tmp.ia6t_vltime -
1175 (time_second - ifa6->ia6_updatetime);
1177 /* when not updating, keep the current stored lifetime. */
1178 lt6_tmp.ia6t_vltime = remaininglifetime;
1180 if (TWOHOUR < new->ndpr_vltime ||
1181 remaininglifetime < new->ndpr_vltime) {
1182 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1183 } else if (remaininglifetime <= TWOHOUR) {
1185 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1189 * new->ndpr_vltime <= TWOHOUR &&
1190 * TWOHOUR < remaininglifetime
1192 lt6_tmp.ia6t_vltime = TWOHOUR;
1195 /* The 2 hour rule is not imposed for preferred lifetime. */
1196 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1198 in6_init_address_ltimes(pr, <6_tmp);
1201 * We need to treat lifetimes for temporary addresses
1202 * differently, according to
1203 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1204 * we only update the lifetimes when they are in the maximum
1207 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1208 u_int32_t maxvltime, maxpltime;
1210 if (V_ip6_temp_valid_lifetime >
1211 (u_int32_t)((time_second - ifa6->ia6_createtime) +
1212 V_ip6_desync_factor)) {
1213 maxvltime = V_ip6_temp_valid_lifetime -
1214 (time_second - ifa6->ia6_createtime) -
1215 V_ip6_desync_factor;
1218 if (V_ip6_temp_preferred_lifetime >
1219 (u_int32_t)((time_second - ifa6->ia6_createtime) +
1220 V_ip6_desync_factor)) {
1221 maxpltime = V_ip6_temp_preferred_lifetime -
1222 (time_second - ifa6->ia6_createtime) -
1223 V_ip6_desync_factor;
1227 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1228 lt6_tmp.ia6t_vltime > maxvltime) {
1229 lt6_tmp.ia6t_vltime = maxvltime;
1231 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1232 lt6_tmp.ia6t_pltime > maxpltime) {
1233 lt6_tmp.ia6t_pltime = maxpltime;
1236 ifa6->ia6_lifetime = lt6_tmp;
1237 ifa6->ia6_updatetime = time_second;
1239 IF_ADDR_UNLOCK(ifp);
1240 if (ia6_match == NULL && new->ndpr_vltime) {
1244 * 5.5.3 (d) (continued)
1245 * No address matched and the valid lifetime is non-zero.
1246 * Create a new address.
1250 * Prefix Length check:
1251 * If the sum of the prefix length and interface identifier
1252 * length does not equal 128 bits, the Prefix Information
1253 * option MUST be ignored. The length of the interface
1254 * identifier is defined in a separate link-type specific
1257 ifidlen = in6_if2idlen(ifp);
1259 /* this should not happen, so we always log it. */
1260 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1264 if (ifidlen + pr->ndpr_plen != 128) {
1266 "prelist_update: invalid prefixlen "
1267 "%d for %s, ignored\n",
1268 pr->ndpr_plen, if_name(ifp)));
1272 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1274 * note that we should use pr (not new) for reference.
1281 * When a new public address is created as described
1282 * in RFC2462, also create a new temporary address.
1285 * When an interface connects to a new link, a new
1286 * randomized interface identifier should be generated
1287 * immediately together with a new set of temporary
1288 * addresses. Thus, we specifiy 1 as the 2nd arg of
1291 if (V_ip6_use_tempaddr) {
1293 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1294 nd6log((LOG_NOTICE, "prelist_update: "
1295 "failed to create a temporary "
1296 "address, errno=%d\n",
1300 ifa_free(&ia6->ia_ifa);
1303 * A newly added address might affect the status
1304 * of other addresses, so we check and update it.
1305 * XXX: what if address duplication happens?
1307 pfxlist_onlink_check();
1309 /* just set an error. do not bark here. */
1310 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1320 * A supplement function used in the on-link detection below;
1321 * detect if a given prefix has a (probably) reachable advertising router.
1322 * XXX: lengthy function name...
1324 static struct nd_pfxrouter *
1325 find_pfxlist_reachable_router(struct nd_prefix *pr)
1327 struct nd_pfxrouter *pfxrtr;
1331 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr != NULL;
1332 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1333 IF_AFDATA_LOCK(pfxrtr->router->ifp);
1334 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1335 IF_AFDATA_UNLOCK(pfxrtr->router->ifp);
1338 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1347 * Check if each prefix in the prefix list has at least one available router
1348 * that advertised the prefix (a router is "available" if its neighbor cache
1349 * entry is reachable or probably reachable).
1350 * If the check fails, the prefix may be off-link, because, for example,
1351 * we have moved from the network but the lifetime of the prefix has not
1352 * expired yet. So we should not use the prefix if there is another prefix
1353 * that has an available router.
1354 * But, if there is no prefix that has an available router, we still regards
1355 * all the prefixes as on-link. This is because we can't tell if all the
1356 * routers are simply dead or if we really moved from the network and there
1357 * is no router around us.
1360 pfxlist_onlink_check()
1362 struct nd_prefix *pr;
1363 struct in6_ifaddr *ifa;
1364 struct nd_defrouter *dr;
1365 struct nd_pfxrouter *pfxrtr = NULL;
1368 * Check if there is a prefix that has a reachable advertising
1371 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1372 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1377 * If we have no such prefix, check whether we still have a router
1378 * that does not advertise any prefixes.
1381 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
1382 dr = TAILQ_NEXT(dr, dr_entry)) {
1383 struct nd_prefix *pr0;
1385 for (pr0 = V_nd_prefix.lh_first; pr0;
1386 pr0 = pr0->ndpr_next) {
1387 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1394 if (pr != NULL || (TAILQ_FIRST(&V_nd_defrouter) && pfxrtr == NULL)) {
1396 * There is at least one prefix that has a reachable router,
1397 * or at least a router which probably does not advertise
1398 * any prefixes. The latter would be the case when we move
1399 * to a new link where we have a router that does not provide
1400 * prefixes and we configure an address by hand.
1401 * Detach prefixes which have no reachable advertising
1402 * router, and attach other prefixes.
1404 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1405 /* XXX: a link-local prefix should never be detached */
1406 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1410 * we aren't interested in prefixes without the L bit
1413 if (pr->ndpr_raf_onlink == 0)
1416 if (pr->ndpr_raf_auto == 0)
1419 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1420 find_pfxlist_reachable_router(pr) == NULL)
1421 pr->ndpr_stateflags |= NDPRF_DETACHED;
1422 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1423 find_pfxlist_reachable_router(pr) != 0)
1424 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1427 /* there is no prefix that has a reachable router */
1428 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1429 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1432 if (pr->ndpr_raf_onlink == 0)
1435 if (pr->ndpr_raf_auto == 0)
1438 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1439 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1444 * Remove each interface route associated with a (just) detached
1445 * prefix, and reinstall the interface route for a (just) attached
1446 * prefix. Note that all attempt of reinstallation does not
1447 * necessarily success, when a same prefix is shared among multiple
1448 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1449 * so we don't have to care about them.
1451 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1453 char ip6buf[INET6_ADDRSTRLEN];
1455 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1458 if (pr->ndpr_raf_onlink == 0)
1461 if (pr->ndpr_raf_auto == 0)
1464 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1465 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1466 if ((e = nd6_prefix_offlink(pr)) != 0) {
1468 "pfxlist_onlink_check: failed to "
1469 "make %s/%d offlink, errno=%d\n",
1471 &pr->ndpr_prefix.sin6_addr),
1475 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1476 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1477 pr->ndpr_raf_onlink) {
1478 if ((e = nd6_prefix_onlink(pr)) != 0) {
1480 "pfxlist_onlink_check: failed to "
1481 "make %s/%d onlink, errno=%d\n",
1483 &pr->ndpr_prefix.sin6_addr),
1490 * Changes on the prefix status might affect address status as well.
1491 * Make sure that all addresses derived from an attached prefix are
1492 * attached, and that all addresses derived from a detached prefix are
1493 * detached. Note, however, that a manually configured address should
1494 * always be attached.
1495 * The precise detection logic is same as the one for prefixes.
1497 * XXXRW: in6_ifaddrhead locking.
1499 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1500 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1503 if (ifa->ia6_ndpr == NULL) {
1505 * This can happen when we first configure the address
1506 * (i.e. the address exists, but the prefix does not).
1507 * XXX: complicated relationships...
1512 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1516 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1517 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1520 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1523 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1524 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1525 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1526 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1527 nd6_dad_start((struct ifaddr *)ifa, 0);
1530 ifa->ia6_flags |= IN6_IFF_DETACHED;
1535 TAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1536 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1539 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1540 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1541 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1542 /* Do we need a delay in this case? */
1543 nd6_dad_start((struct ifaddr *)ifa, 0);
1550 nd6_prefix_onlink(struct nd_prefix *pr)
1553 struct ifnet *ifp = pr->ndpr_ifp;
1554 struct sockaddr_in6 mask6;
1555 struct nd_prefix *opr;
1558 struct radix_node_head *rnh;
1559 struct rtentry *rt = NULL;
1560 char ip6buf[INET6_ADDRSTRLEN];
1561 struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1564 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1566 "nd6_prefix_onlink: %s/%d is already on-link\n",
1567 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1573 * Add the interface route associated with the prefix. Before
1574 * installing the route, check if there's the same prefix on another
1575 * interface, and the prefix has already installed the interface route.
1576 * Although such a configuration is expected to be rare, we explicitly
1579 for (opr = V_nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1583 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1586 if (opr->ndpr_plen == pr->ndpr_plen &&
1587 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1588 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1593 * We prefer link-local addresses as the associated interface address.
1595 /* search for a link-local addr */
1596 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1597 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1599 /* XXX: freebsd does not have ifa_ifwithaf */
1601 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1602 if (ifa->ifa_addr->sa_family == AF_INET6)
1607 IF_ADDR_UNLOCK(ifp);
1608 /* should we care about ia6_flags? */
1612 * This can still happen, when, for example, we receive an RA
1613 * containing a prefix with the L bit set and the A bit clear,
1614 * after removing all IPv6 addresses on the receiving
1615 * interface. This should, of course, be rare though.
1618 "nd6_prefix_onlink: failed to find any ifaddr"
1619 " to add route for a prefix(%s/%d) on %s\n",
1620 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1621 pr->ndpr_plen, if_name(ifp)));
1626 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1627 * ifa->ifa_rtrequest = nd6_rtrequest;
1629 bzero(&mask6, sizeof(mask6));
1630 mask6.sin6_len = sizeof(mask6);
1631 mask6.sin6_addr = pr->ndpr_mask;
1632 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1633 error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1634 ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags, &rt);
1636 if (rt != NULL) /* this should be non NULL, though */ {
1637 rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
1638 /* XXX what if rhn == NULL? */
1639 RADIX_NODE_HEAD_LOCK(rnh);
1641 if (!rt_setgate(rt, rt_key(rt), (struct sockaddr *)&null_sdl)) {
1642 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1643 rt->rt_ifp->if_type;
1644 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1645 rt->rt_ifp->if_index;
1647 RADIX_NODE_HEAD_UNLOCK(rnh);
1648 nd6_rtmsg(RTM_ADD, rt);
1651 pr->ndpr_stateflags |= NDPRF_ONLINK;
1653 char ip6bufg[INET6_ADDRSTRLEN], ip6bufm[INET6_ADDRSTRLEN];
1654 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a"
1655 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1657 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1658 pr->ndpr_plen, if_name(ifp),
1659 ip6_sprintf(ip6bufg, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
1660 ip6_sprintf(ip6bufm, &mask6.sin6_addr), rtflags, error));
1675 nd6_prefix_offlink(struct nd_prefix *pr)
1678 struct ifnet *ifp = pr->ndpr_ifp;
1679 struct nd_prefix *opr;
1680 struct sockaddr_in6 sa6, mask6;
1681 struct rtentry *rt = NULL;
1682 char ip6buf[INET6_ADDRSTRLEN];
1685 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1687 "nd6_prefix_offlink: %s/%d is already off-link\n",
1688 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1693 bzero(&sa6, sizeof(sa6));
1694 sa6.sin6_family = AF_INET6;
1695 sa6.sin6_len = sizeof(sa6);
1696 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1697 sizeof(struct in6_addr));
1698 bzero(&mask6, sizeof(mask6));
1699 mask6.sin6_family = AF_INET6;
1700 mask6.sin6_len = sizeof(sa6);
1701 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1702 error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1703 (struct sockaddr *)&mask6, 0, &rt);
1705 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1707 /* report the route deletion to the routing socket. */
1709 nd6_rtmsg(RTM_DELETE, rt);
1712 * There might be the same prefix on another interface,
1713 * the prefix which could not be on-link just because we have
1714 * the interface route (see comments in nd6_prefix_onlink).
1715 * If there's one, try to make the prefix on-link on the
1718 for (opr = V_nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1722 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1726 * KAME specific: detached prefixes should not be
1729 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1732 if (opr->ndpr_plen == pr->ndpr_plen &&
1733 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1734 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1737 if ((e = nd6_prefix_onlink(opr)) != 0) {
1739 "nd6_prefix_offlink: failed to "
1740 "recover a prefix %s/%d from %s "
1741 "to %s (errno = %d)\n",
1743 &opr->ndpr_prefix.sin6_addr),
1744 opr->ndpr_plen, if_name(ifp),
1745 if_name(opr->ndpr_ifp), e));
1750 /* XXX: can we still set the NDPRF_ONLINK flag? */
1752 "nd6_prefix_offlink: failed to delete route: "
1753 "%s/%d on %s (errno = %d)\n",
1754 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
1755 if_name(ifp), error));
1765 static struct in6_ifaddr *
1766 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1768 struct ifnet *ifp = pr->ndpr_ifp;
1770 struct in6_aliasreq ifra;
1771 struct in6_ifaddr *ia, *ib;
1773 struct in6_addr mask;
1774 int prefixlen = pr->ndpr_plen;
1776 char ip6buf[INET6_ADDRSTRLEN];
1778 in6_prefixlen2mask(&mask, prefixlen);
1781 * find a link-local address (will be interface ID).
1782 * Is it really mandatory? Theoretically, a global or a site-local
1783 * address can be configured without a link-local address, if we
1784 * have a unique interface identifier...
1786 * it is not mandatory to have a link-local address, we can generate
1787 * interface identifier on the fly. we do this because:
1788 * (1) it should be the easiest way to find interface identifier.
1789 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1790 * for multiple addresses on a single interface, and possible shortcut
1791 * of DAD. we omitted DAD for this reason in the past.
1792 * (3) a user can prevent autoconfiguration of global address
1793 * by removing link-local address by hand (this is partly because we
1794 * don't have other way to control the use of IPv6 on an interface.
1795 * this has been our design choice - cf. NRL's "ifconfig auto").
1796 * (4) it is easier to manage when an interface has addresses
1797 * with the same interface identifier, than to have multiple addresses
1798 * with different interface identifiers.
1800 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1802 ib = (struct in6_ifaddr *)ifa;
1806 /* prefixlen + ifidlen must be equal to 128 */
1807 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1808 if (prefixlen != plen0) {
1810 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1811 "(prefix=%d ifid=%d)\n",
1812 if_name(ifp), prefixlen, 128 - plen0));
1818 bzero(&ifra, sizeof(ifra));
1820 * in6_update_ifa() does not use ifra_name, but we accurately set it
1823 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1824 ifra.ifra_addr.sin6_family = AF_INET6;
1825 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1827 ifra.ifra_addr.sin6_addr = pr->ndpr_prefix.sin6_addr;
1828 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1829 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1830 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1831 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1834 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1835 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1836 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1837 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1838 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1839 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1840 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1841 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1844 /* new prefix mask. */
1845 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1846 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1847 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1848 sizeof(ifra.ifra_prefixmask.sin6_addr));
1851 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1852 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1854 /* XXX: scope zone ID? */
1856 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1859 * Make sure that we do not have this address already. This should
1860 * usually not happen, but we can still see this case, e.g., if we
1861 * have manually configured the exact address to be configured.
1863 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1864 &ifra.ifra_addr.sin6_addr);
1867 /* this should be rare enough to make an explicit log */
1868 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1869 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1874 * Allocate ifaddr structure, link into chain, etc.
1875 * If we are going to create a new address upon receiving a multicasted
1876 * RA, we need to impose a random delay before starting DAD.
1877 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1881 updateflags |= IN6_IFAUPDATE_DADDELAY;
1882 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1884 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1885 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1886 if_name(ifp), error));
1887 return (NULL); /* ifaddr must not have been allocated. */
1890 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1892 * XXXRW: Assumption of non-NULLness here might not be true with
1893 * fine-grained locking -- should we validate it? Or just return
1894 * earlier ifa rather than looking it up again?
1896 return (ia); /* this is always non-NULL and referenced. */
1900 * ia0 - corresponding public address
1903 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
1905 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1906 struct in6_ifaddr *newia, *ia;
1907 struct in6_aliasreq ifra;
1909 int trylimit = 3; /* XXX: adhoc value */
1911 u_int32_t randid[2];
1912 time_t vltime0, pltime0;
1914 bzero(&ifra, sizeof(ifra));
1915 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1916 ifra.ifra_addr = ia0->ia_addr;
1917 /* copy prefix mask */
1918 ifra.ifra_prefixmask = ia0->ia_prefixmask;
1919 /* clear the old IFID */
1920 for (i = 0; i < 4; i++) {
1921 ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
1922 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1926 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
1927 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
1928 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
1932 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1933 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1934 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1935 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1938 * in6_get_tmpifid() quite likely provided a unique interface ID.
1939 * However, we may still have a chance to see collision, because
1940 * there may be a time lag between generation of the ID and generation
1941 * of the address. So, we'll do one more sanity check.
1944 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1945 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1946 &ifra.ifra_addr.sin6_addr)) {
1947 if (trylimit-- == 0) {
1948 IN6_IFADDR_RUNLOCK();
1950 * Give up. Something strange should have
1953 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
1954 "find a unique random IFID\n"));
1957 IN6_IFADDR_RUNLOCK();
1962 IN6_IFADDR_RUNLOCK();
1965 * The Valid Lifetime is the lower of the Valid Lifetime of the
1966 * public address or TEMP_VALID_LIFETIME.
1967 * The Preferred Lifetime is the lower of the Preferred Lifetime
1968 * of the public address or TEMP_PREFERRED_LIFETIME -
1971 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1972 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
1973 (ia0->ia6_lifetime.ia6t_vltime -
1974 (time_second - ia0->ia6_updatetime));
1975 if (vltime0 > V_ip6_temp_valid_lifetime)
1976 vltime0 = V_ip6_temp_valid_lifetime;
1978 vltime0 = V_ip6_temp_valid_lifetime;
1979 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1980 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
1981 (ia0->ia6_lifetime.ia6t_pltime -
1982 (time_second - ia0->ia6_updatetime));
1983 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
1984 pltime0 = V_ip6_temp_preferred_lifetime -
1985 V_ip6_desync_factor;
1988 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
1989 ifra.ifra_lifetime.ia6t_vltime = vltime0;
1990 ifra.ifra_lifetime.ia6t_pltime = pltime0;
1993 * A temporary address is created only if this calculated Preferred
1994 * Lifetime is greater than REGEN_ADVANCE time units.
1996 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
1999 /* XXX: scope zone ID? */
2001 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2003 /* allocate ifaddr structure, link into chain, etc. */
2006 updateflags |= IN6_IFAUPDATE_DADDELAY;
2007 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2010 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2011 if (newia == NULL) { /* XXX: can it happen? */
2013 "in6_tmpifadd: ifa update succeeded, but we got "
2015 return (EINVAL); /* XXX */
2017 newia->ia6_ndpr = ia0->ia6_ndpr;
2018 newia->ia6_ndpr->ndpr_refcnt++;
2019 ifa_free(&newia->ia_ifa);
2022 * A newly added address might affect the status of other addresses.
2023 * XXX: when the temporary address is generated with a new public
2024 * address, the onlink check is redundant. However, it would be safe
2025 * to do the check explicitly everywhere a new address is generated,
2026 * and, in fact, we surely need the check when we create a new
2027 * temporary address due to deprecation of an old temporary address.
2029 pfxlist_onlink_check();
2035 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2037 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2038 ndpr->ndpr_preferred = 0;
2040 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
2041 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2042 ndpr->ndpr_expire = 0;
2044 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
2050 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
2052 /* init ia6t_expire */
2053 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2054 lt6->ia6t_expire = 0;
2056 lt6->ia6t_expire = time_second;
2057 lt6->ia6t_expire += lt6->ia6t_vltime;
2060 /* init ia6t_preferred */
2061 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2062 lt6->ia6t_preferred = 0;
2064 lt6->ia6t_preferred = time_second;
2065 lt6->ia6t_preferred += lt6->ia6t_pltime;
2070 * Delete all the routing table entries that use the specified gateway.
2071 * XXX: this function causes search through all entries of routing table, so
2072 * it shouldn't be called when acting as a router.
2075 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2077 struct radix_node_head *rnh;
2080 /* We'll care only link-local addresses */
2081 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
2086 rnh = rt_tables_get_rnh(0, AF_INET6);
2090 RADIX_NODE_HEAD_LOCK(rnh);
2091 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
2092 RADIX_NODE_HEAD_UNLOCK(rnh);
2097 rt6_deleteroute(struct radix_node *rn, void *arg)
2099 #define SIN6(s) ((struct sockaddr_in6 *)s)
2100 struct rtentry *rt = (struct rtentry *)rn;
2101 struct in6_addr *gate = (struct in6_addr *)arg;
2103 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2106 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2111 * Do not delete a static route.
2112 * XXX: this seems to be a bit ad-hoc. Should we consider the
2113 * 'cloned' bit instead?
2115 if ((rt->rt_flags & RTF_STATIC) != 0)
2119 * We delete only host route. This means, in particular, we don't
2120 * delete default route.
2122 if ((rt->rt_flags & RTF_HOST) == 0)
2125 return (rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
2126 rt_mask(rt), rt->rt_flags, 0));
2131 nd6_setdefaultiface(int ifindex)
2135 if (ifindex < 0 || V_if_index < ifindex)
2137 if (ifindex != 0 && !ifnet_byindex(ifindex))
2140 if (V_nd6_defifindex != ifindex) {
2141 V_nd6_defifindex = ifindex;
2142 if (V_nd6_defifindex > 0)
2143 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2145 V_nd6_defifp = NULL;
2148 * Our current implementation assumes one-to-one maping between
2149 * interfaces and links, so it would be natural to use the
2150 * default interface as the default link.
2152 scope6_setdefault(V_nd6_defifp);