1 /* $NetBSD: nd6_rtr.c,v 1.79 2009/07/25 23:12:09 tonnerre Exp $ */
2 /* $KAME: nd6_rtr.c,v 1.95 2001/02/07 08:09:47 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nd6_rtr.c,v 1.79 2009/07/25 23:12:09 tonnerre Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
43 #include <sys/kernel.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/syslog.h>
49 #include <net/if_types.h>
50 #include <net/if_dl.h>
51 #include <net/route.h>
52 #include <net/radix.h>
54 #include <netinet/in.h>
55 #include <netinet6/in6_var.h>
56 #include <netinet6/in6_ifattach.h>
57 #include <netinet/ip6.h>
58 #include <netinet6/ip6_var.h>
59 #include <netinet6/nd6.h>
60 #include <netinet/icmp6.h>
61 #include <netinet6/icmp6_private.h>
62 #include <netinet6/scope6_var.h>
64 #include <net/net_osdep.h>
66 static int rtpref(struct nd_defrouter
*);
67 static struct nd_defrouter
*defrtrlist_update(struct nd_defrouter
*);
68 static int prelist_update(struct nd_prefixctl
*, struct nd_defrouter
*,
70 static struct in6_ifaddr
*in6_ifadd(struct nd_prefixctl
*, int);
71 static struct nd_pfxrouter
*pfxrtr_lookup(struct nd_prefix
*,
72 struct nd_defrouter
*);
73 static void pfxrtr_add(struct nd_prefix
*, struct nd_defrouter
*);
74 static void pfxrtr_del(struct nd_pfxrouter
*);
75 static struct nd_pfxrouter
*find_pfxlist_reachable_router
77 static void defrouter_delreq(struct nd_defrouter
*);
78 static void nd6_rtmsg(int, struct rtentry
*);
80 static int in6_init_prefix_ltimes(struct nd_prefix
*);
81 static void in6_init_address_ltimes(struct nd_prefix
*ndpr
,
82 struct in6_addrlifetime
*lt6
);
84 static int rt6_deleteroute(struct rtentry
*, void *);
86 extern int nd6_recalc_reachtm_interval
;
88 static struct ifnet
*nd6_defifp
;
91 int ip6_use_tempaddr
= 0;
93 int ip6_desync_factor
;
94 u_int32_t ip6_temp_preferred_lifetime
= DEF_TEMP_PREFERRED_LIFETIME
;
95 u_int32_t ip6_temp_valid_lifetime
= DEF_TEMP_VALID_LIFETIME
;
96 int ip6_temp_regen_advance
= TEMPADDR_REGEN_ADVANCE
;
98 /* RTPREF_MEDIUM has to be 0! */
100 #define RTPREF_MEDIUM 0
101 #define RTPREF_LOW (-1)
102 #define RTPREF_RESERVED (-2)
103 #define RTPREF_INVALID (-3) /* internal */
106 * Receive Router Solicitation Message - just for routers.
107 * Router solicitation/advertisement is mostly managed by userland program
108 * (rtadvd) so here we have no function like nd6_ra_output().
113 nd6_rs_input(struct mbuf
*m
, int off
, int icmp6len
)
115 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
116 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
117 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
118 struct nd_router_solicit
*nd_rs
;
119 struct in6_addr saddr6
= ip6
->ip6_src
;
122 union nd_opts ndopts
;
124 /* If I'm not a router, ignore it. */
125 if (nd6_accepts_rtadv(ndi
) || !ip6_forwarding
)
129 if (ip6
->ip6_hlim
!= 255) {
131 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
132 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
133 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
138 * Don't update the neighbor cache, if src = ::.
139 * This indicates that the src has no IP address assigned yet.
141 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
))
144 IP6_EXTHDR_GET(nd_rs
, struct nd_router_solicit
*, m
, off
, icmp6len
);
146 ICMP6_STATINC(ICMP6_STAT_TOOSHORT
);
150 icmp6len
-= sizeof(*nd_rs
);
151 nd6_option_init(nd_rs
+ 1, icmp6len
, &ndopts
);
152 if (nd6_options(&ndopts
) < 0) {
154 "nd6_rs_input: invalid ND option, ignored\n"));
155 /* nd6_options have incremented stats */
159 if (ndopts
.nd_opts_src_lladdr
) {
160 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
161 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
164 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
166 "nd6_rs_input: lladdrlen mismatch for %s "
167 "(if %d, RS packet %d)\n",
168 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
172 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_SOLICIT
, 0);
179 ICMP6_STATINC(ICMP6_STAT_BADRS
);
184 * Receive Router Advertisement Message.
187 * TODO: on-link bit on prefix information
188 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
191 nd6_ra_input(struct mbuf
*m
, int off
, int icmp6len
)
193 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
194 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
195 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
196 struct nd_router_advert
*nd_ra
;
197 struct in6_addr saddr6
= ip6
->ip6_src
;
199 struct in6_addr daddr6
= ip6
->ip6_dst
;
200 int flags
; /* = nd_ra->nd_ra_flags_reserved; */
201 int is_managed
= ((flags
& ND_RA_FLAG_MANAGED
) != 0);
202 int is_other
= ((flags
& ND_RA_FLAG_OTHER
) != 0);
205 union nd_opts ndopts
;
206 struct nd_defrouter
*dr
;
209 * We only accept RAs only when
210 * the system-wide variable allows the acceptance, and
211 * per-interface variable allows RAs on the receiving interface.
213 if (!nd6_accepts_rtadv(ndi
))
216 if (ip6
->ip6_hlim
!= 255) {
218 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
219 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
220 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
224 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) {
226 "nd6_ra_input: src %s is not link-local\n",
227 ip6_sprintf(&saddr6
)));
231 IP6_EXTHDR_GET(nd_ra
, struct nd_router_advert
*, m
, off
, icmp6len
);
233 ICMP6_STATINC(ICMP6_STAT_TOOSHORT
);
237 icmp6len
-= sizeof(*nd_ra
);
238 nd6_option_init(nd_ra
+ 1, icmp6len
, &ndopts
);
239 if (nd6_options(&ndopts
) < 0) {
241 "nd6_ra_input: invalid ND option, ignored\n"));
242 /* nd6_options have incremented stats */
247 struct nd_defrouter drtr
;
248 u_int32_t advreachable
= nd_ra
->nd_ra_reachable
;
250 /* remember if this is a multicasted advertisement */
251 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
254 memset(&drtr
, 0, sizeof(drtr
));
255 drtr
.rtaddr
= saddr6
;
256 drtr
.flags
= nd_ra
->nd_ra_flags_reserved
;
257 drtr
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
258 drtr
.expire
= time_second
+ drtr
.rtlifetime
;
260 /* unspecified or not? (RFC 2461 6.3.4) */
263 if (advreachable
<= MAX_REACHABLE_TIME
&&
264 ndi
->basereachable
!= advreachable
) {
265 ndi
->basereachable
= advreachable
;
266 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
267 ndi
->recalctm
= nd6_recalc_reachtm_interval
; /* reset */
270 if (nd_ra
->nd_ra_retransmit
)
271 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
272 if (nd_ra
->nd_ra_curhoplimit
)
273 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
274 dr
= defrtrlist_update(&drtr
);
280 if (ndopts
.nd_opts_pi
) {
281 struct nd_opt_hdr
*pt
;
282 struct nd_opt_prefix_info
*pi
= NULL
;
283 struct nd_prefixctl pr
;
285 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
286 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
287 pt
= (struct nd_opt_hdr
*)((char *)pt
+
288 (pt
->nd_opt_len
<< 3))) {
289 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
)
291 pi
= (struct nd_opt_prefix_info
*)pt
;
293 if (pi
->nd_opt_pi_len
!= 4) {
295 "nd6_ra_input: invalid option "
296 "len %d for prefix information option, "
297 "ignored\n", pi
->nd_opt_pi_len
));
301 if (128 < pi
->nd_opt_pi_prefix_len
) {
303 "nd6_ra_input: invalid prefix "
304 "len %d for prefix information option, "
305 "ignored\n", pi
->nd_opt_pi_prefix_len
));
309 if (IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
)
310 || IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
312 "nd6_ra_input: invalid prefix "
314 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
318 memset(&pr
, 0, sizeof(pr
));
319 sockaddr_in6_init(&pr
.ndpr_prefix
,
320 &pi
->nd_opt_pi_prefix
, 0, 0, 0);
321 pr
.ndpr_ifp
= (struct ifnet
*)m
->m_pkthdr
.rcvif
;
323 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
324 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
325 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
326 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
327 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
328 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
329 pr
.ndpr_pltime
= ntohl(pi
->nd_opt_pi_preferred_time
);
331 (void)prelist_update(&pr
, dr
, m
, mcast
);
338 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
342 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
345 if (mtu
< IPV6_MMTU
) {
346 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu option "
347 "mtu=%lu sent from %s, ignoring\n",
348 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
353 maxmtu
= (ndi
->maxmtu
&& ndi
->maxmtu
< ifp
->if_mtu
)
354 ? ndi
->maxmtu
: ifp
->if_mtu
;
356 int change
= (ndi
->linkmtu
!= mtu
);
359 if (change
) /* in6_maxmtu may change */
362 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu "
363 "mtu=%lu sent from %s; "
364 "exceeds maxmtu %lu, ignoring\n",
365 mtu
, ip6_sprintf(&ip6
->ip6_src
), maxmtu
));
372 * Source link layer address
378 if (ndopts
.nd_opts_src_lladdr
) {
379 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
380 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
383 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
385 "nd6_ra_input: lladdrlen mismatch for %s "
386 "(if %d, RA packet %d)\n", ip6_sprintf(&saddr6
),
387 ifp
->if_addrlen
, lladdrlen
- 2));
391 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_ADVERT
, 0);
394 * Installing a link-layer address might change the state of the
395 * router's neighbor cache, which might also affect our on-link
396 * detection of adveritsed prefixes.
398 pfxlist_onlink_check();
406 ICMP6_STATINC(ICMP6_STAT_BADRA
);
411 * default router list processing sub routines
414 /* tell the change to user processes watching the routing socket. */
416 nd6_rtmsg(int cmd
, struct rtentry
*rt
)
418 struct rt_addrinfo info
;
420 memset((void *)&info
, 0, sizeof(info
));
421 info
.rti_info
[RTAX_DST
] = rt_getkey(rt
);
422 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
423 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
425 info
.rti_info
[RTAX_IFP
] = rt
->rt_ifp
->if_dl
->ifa_addr
;
426 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
429 rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0);
433 defrouter_addreq(struct nd_defrouter
*new)
436 struct sockaddr_in6 sin6
;
439 struct rtentry
*newrt
= NULL
;
443 memset(&def
, 0, sizeof(def
));
444 memset(&mask
, 0, sizeof(mask
));
445 memset(&gate
, 0,sizeof(gate
)); /* for safety */
447 def
.sin6
.sin6_len
= mask
.sin6
.sin6_len
= gate
.sin6
.sin6_len
=
448 sizeof(struct sockaddr_in6
);
449 def
.sin6
.sin6_family
= mask
.sin6
.sin6_family
= gate
.sin6
.sin6_family
= AF_INET6
;
450 gate
.sin6
.sin6_addr
= new->rtaddr
;
451 #ifndef SCOPEDROUTING
452 gate
.sin6
.sin6_scope_id
= 0; /* XXX */
456 error
= rtrequest(RTM_ADD
, &def
.sa
, &gate
.sa
, &mask
.sa
,
457 RTF_GATEWAY
, &newrt
);
459 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */
468 struct nd_defrouter
*
469 defrouter_lookup(const struct in6_addr
*addr
, struct ifnet
*ifp
)
471 struct nd_defrouter
*dr
;
473 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
474 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
))
478 return dr
; /* search failed */
482 defrtrlist_del(struct nd_defrouter
*dr
)
484 struct nd_ifinfo
*ndi
= ND_IFINFO(dr
->ifp
);
485 struct nd_defrouter
*deldr
= NULL
;
486 struct nd_prefix
*pr
;
489 * Flush all the routing table entries that use the router
492 /* XXX: better condition? */
493 if (!ip6_forwarding
&& nd6_accepts_rtadv(ndi
))
494 rt6_flush(&dr
->rtaddr
, dr
->ifp
);
498 defrouter_delreq(dr
);
500 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
503 * Also delete all the pointers to the router in each prefix lists.
505 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
506 struct nd_pfxrouter
*pfxrtr
;
507 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
510 pfxlist_onlink_check();
513 * If the router is the primary one, choose a new one.
514 * Note that defrouter_select() will remove the current gateway
515 * from the routing table.
524 * Remove the default route for a given router.
525 * This is just a subroutine function for defrouter_select(), and should
526 * not be called from anywhere else.
529 defrouter_delreq(struct nd_defrouter
*dr
)
532 struct sockaddr_in6 sin6
;
535 struct rtentry
*oldrt
= NULL
;
539 panic("dr == NULL in defrouter_delreq");
542 memset(&def
, 0, sizeof(def
));
543 memset(&mask
, 0, sizeof(mask
));
544 memset(&gw
, 0, sizeof(gw
)); /* for safety */
546 def
.sin6
.sin6_len
= mask
.sin6
.sin6_len
= gw
.sin6
.sin6_len
=
547 sizeof(struct sockaddr_in6
);
548 def
.sin6
.sin6_family
= mask
.sin6
.sin6_family
= gw
.sin6
.sin6_family
= AF_INET6
;
549 gw
.sin6
.sin6_addr
= dr
->rtaddr
;
550 #ifndef SCOPEDROUTING
551 gw
.sin6
.sin6_scope_id
= 0; /* XXX */
554 rtrequest(RTM_DELETE
, &def
.sa
, &gw
.sa
, &mask
.sa
, RTF_GATEWAY
, &oldrt
);
556 nd6_rtmsg(RTM_DELETE
, oldrt
);
557 if (oldrt
->rt_refcnt
<= 0) {
559 * XXX: borrowed from the RTM_DELETE case of
571 * remove all default routes from default router list
574 defrouter_reset(void)
576 struct nd_defrouter
*dr
;
578 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
579 dr
= TAILQ_NEXT(dr
, dr_entry
))
580 defrouter_delreq(dr
);
583 * XXX should we also nuke any default routers in the kernel, by
584 * going through them by rtalloc1()?
589 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
590 * draft-ietf-ipngwg-router-selection:
591 * 1) Routers that are reachable or probably reachable should be preferred.
592 * If we have more than one (probably) reachable router, prefer ones
593 * with the highest router preference.
594 * 2) When no routers on the list are known to be reachable or
595 * probably reachable, routers SHOULD be selected in a round-robin
596 * fashion, regardless of router preference values.
597 * 3) If the Default Router List is empty, assume that all
598 * destinations are on-link.
600 * We assume nd_defrouter is sorted by router preference value.
601 * Since the code below covers both with and without router preference cases,
602 * we do not need to classify the cases by ifdef.
604 * At this moment, we do not try to install more than one default router,
605 * even when the multipath routing is available, because we're not sure about
606 * the benefits for stub hosts comparing to the risk of making the code
607 * complicated and the possibility of introducing bugs.
610 defrouter_select(void)
612 struct nd_ifinfo
*ndi
;
613 int s
= splsoftnet();
614 struct nd_defrouter
*dr
, *selected_dr
= NULL
, *installed_dr
= NULL
;
615 struct rtentry
*rt
= NULL
;
616 struct llinfo_nd6
*ln
= NULL
;
619 * This function should be called only when acting as an autoconfigured
620 * host. Although the remaining part of this function is not effective
621 * if the node is not an autoconfigured host, we explicitly exclude
622 * such cases here for safety.
624 if (ip6_forwarding
) {
626 "defrouter_select: called unexpectedly (forwarding=%d, "
627 "accept_rtadv=%d)\n", ip6_forwarding
, ip6_accept_rtadv
));
633 * Let's handle easy case (3) first:
634 * If default router list is empty, there's nothing to be done.
636 if (!TAILQ_FIRST(&nd_defrouter
)) {
642 * Search for a (probably) reachable router from the list.
643 * We just pick up the first reachable one (if any), assuming that
644 * the ordering rule of the list described in defrtrlist_update().
646 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
647 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
648 ndi
= ND_IFINFO(dr
->ifp
);
649 if (nd6_accepts_rtadv(ndi
))
652 if (selected_dr
== NULL
&&
653 (rt
= nd6_lookup(&dr
->rtaddr
, 0, dr
->ifp
)) != NULL
&&
654 (ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) != NULL
&&
655 ND6_IS_LLINFO_PROBREACH(ln
)) {
659 if (dr
->installed
&& !installed_dr
)
661 else if (dr
->installed
&& installed_dr
) {
662 /* this should not happen. warn for diagnosis. */
663 log(LOG_ERR
, "defrouter_select: more than one router"
668 * If none of the default routers was found to be reachable,
669 * round-robin the list regardless of preference.
670 * Otherwise, if we have an installed router, check if the selected
671 * (reachable) router should really be preferred to the installed one.
672 * We only prefer the new router when the old one is not reachable
673 * or when the new one has a really higher preference value.
675 if (selected_dr
== NULL
) {
676 if (installed_dr
== NULL
|| !TAILQ_NEXT(installed_dr
, dr_entry
))
677 selected_dr
= TAILQ_FIRST(&nd_defrouter
);
679 selected_dr
= TAILQ_NEXT(installed_dr
, dr_entry
);
680 } else if (installed_dr
&&
681 (rt
= nd6_lookup(&installed_dr
->rtaddr
, 0, installed_dr
->ifp
)) &&
682 (ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) &&
683 ND6_IS_LLINFO_PROBREACH(ln
) &&
684 rtpref(selected_dr
) <= rtpref(installed_dr
)) {
685 selected_dr
= installed_dr
;
689 * If the selected router is different than the installed one,
690 * remove the installed router and install the selected one.
691 * Note that the selected router is never NULL here.
693 if (installed_dr
!= selected_dr
) {
695 defrouter_delreq(installed_dr
);
696 defrouter_addreq(selected_dr
);
704 * for default router selection
705 * regards router-preference field as a 2-bit signed integer
708 rtpref(struct nd_defrouter
*dr
)
710 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
711 case ND_RA_FLAG_RTPREF_HIGH
:
712 return (RTPREF_HIGH
);
713 case ND_RA_FLAG_RTPREF_MEDIUM
:
714 case ND_RA_FLAG_RTPREF_RSV
:
715 return (RTPREF_MEDIUM
);
716 case ND_RA_FLAG_RTPREF_LOW
:
720 * This case should never happen. If it did, it would mean a
721 * serious bug of kernel internal. We thus always bark here.
722 * Or, can we even panic?
724 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
725 return (RTPREF_INVALID
);
730 static struct nd_defrouter
*
731 defrtrlist_update(struct nd_defrouter
*new)
733 struct nd_defrouter
*dr
, *n
;
734 int s
= splsoftnet();
736 if ((dr
= defrouter_lookup(&new->rtaddr
, new->ifp
)) != NULL
) {
738 if (new->rtlifetime
== 0) {
742 int oldpref
= rtpref(dr
);
745 dr
->flags
= new->flags
; /* xxx flag check */
746 dr
->rtlifetime
= new->rtlifetime
;
747 dr
->expire
= new->expire
;
750 * If the preference does not change, there's no need
751 * to sort the entries.
753 if (rtpref(new) == oldpref
) {
759 * preferred router may be changed, so relocate
761 * XXX: calling TAILQ_REMOVE directly is a bad manner.
762 * However, since defrtrlist_del() has many side
763 * effects, we intentionally do so here.
764 * defrouter_select() below will handle routing
767 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
775 /* entry does not exist */
776 if (new->rtlifetime
== 0) {
781 n
= (struct nd_defrouter
*)malloc(sizeof(*n
), M_IP6NDP
, M_NOWAIT
);
786 memset(n
, 0, sizeof(*n
));
791 * Insert the new router in the Default Router List;
792 * The Default Router List should be in the descending order
793 * of router-preferece. Routers with the same preference are
794 * sorted in the arriving time order.
797 /* insert at the end of the group */
798 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
799 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
800 if (rtpref(n
) > rtpref(dr
))
804 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
806 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
815 static struct nd_pfxrouter
*
816 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
818 struct nd_pfxrouter
*search
;
820 LIST_FOREACH(search
, &pr
->ndpr_advrtrs
, pfr_entry
) {
821 if (search
->router
== dr
)
829 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
831 struct nd_pfxrouter
*new;
833 new = malloc(sizeof(*new), M_IP6NDP
, M_NOWAIT
|M_ZERO
);
838 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
840 pfxlist_onlink_check();
844 pfxrtr_del(struct nd_pfxrouter
*pfr
)
846 LIST_REMOVE(pfr
, pfr_entry
);
851 nd6_prefix_lookup(struct nd_prefixctl
*key
)
853 struct nd_prefix
*search
;
855 LIST_FOREACH(search
, &nd_prefix
, ndpr_entry
) {
856 if (key
->ndpr_ifp
== search
->ndpr_ifp
&&
857 key
->ndpr_plen
== search
->ndpr_plen
&&
858 in6_are_prefix_equal(&key
->ndpr_prefix
.sin6_addr
,
859 &search
->ndpr_prefix
.sin6_addr
, key
->ndpr_plen
)) {
868 nd6_prelist_add(struct nd_prefixctl
*pr
, struct nd_defrouter
*dr
,
869 struct nd_prefix
**newp
)
871 struct nd_prefix
*new = NULL
;
876 new = malloc(sizeof(*new), M_IP6NDP
, M_NOWAIT
|M_ZERO
);
879 new->ndpr_ifp
= pr
->ndpr_ifp
;
880 new->ndpr_prefix
= pr
->ndpr_prefix
;
881 new->ndpr_plen
= pr
->ndpr_plen
;
882 new->ndpr_vltime
= pr
->ndpr_vltime
;
883 new->ndpr_pltime
= pr
->ndpr_pltime
;
884 new->ndpr_flags
= pr
->ndpr_flags
;
885 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
889 new->ndpr_lastupdate
= time_second
;
894 LIST_INIT(&new->ndpr_advrtrs
);
895 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
896 /* make prefix in the canonical form */
897 for (i
= 0; i
< 4; i
++)
898 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
899 new->ndpr_mask
.s6_addr32
[i
];
902 /* link ndpr_entry to nd_prefix list */
903 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
906 /* ND_OPT_PI_FLAG_ONLINK processing */
907 if (new->ndpr_raf_onlink
) {
910 if ((e
= nd6_prefix_onlink(new)) != 0) {
911 nd6log((LOG_ERR
, "nd6_prelist_add: failed to make "
912 "the prefix %s/%d on-link on %s (errno=%d)\n",
913 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
914 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
915 /* proceed anyway. XXX: is it correct? */
926 prelist_remove(struct nd_prefix
*pr
)
928 struct nd_pfxrouter
*pfr
, *next
;
931 /* make sure to invalidate the prefix until it is really freed. */
936 * Though these flags are now meaningless, we'd rather keep the value
937 * not to confuse users when executing "ndp -p".
939 pr
->ndpr_raf_onlink
= 0;
940 pr
->ndpr_raf_auto
= 0;
942 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0 &&
943 (e
= nd6_prefix_offlink(pr
)) != 0) {
944 nd6log((LOG_ERR
, "prelist_remove: failed to make %s/%d offlink "
946 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
947 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
948 /* what should we do? */
951 if (pr
->ndpr_refcnt
> 0)
952 return; /* notice here? */
955 /* unlink ndpr_entry from nd_prefix list */
956 LIST_REMOVE(pr
, ndpr_entry
);
958 /* free list of routers that adversed the prefix */
959 for (pfr
= LIST_FIRST(&pr
->ndpr_advrtrs
); pfr
!= NULL
; pfr
= next
) {
960 next
= LIST_NEXT(pfr
, pfr_entry
);
968 pfxlist_onlink_check();
972 prelist_update(struct nd_prefixctl
*new,
973 struct nd_defrouter
*dr
, /* may be NULL */
977 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
979 struct ifnet
*ifp
= new->ndpr_ifp
;
980 struct nd_prefix
*pr
;
981 int s
= splsoftnet();
985 struct in6_addrlifetime lt6_tmp
;
990 * Authenticity for NA consists authentication for
991 * both IP header and IP datagrams, doesn't it ?
993 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
994 auth
= (m
->m_flags
& M_AUTHIPHDR
995 && m
->m_flags
& M_AUTHIPDGM
) ? 1 : 0;
999 if ((pr
= nd6_prefix_lookup(new)) != NULL
) {
1001 * nd6_prefix_lookup() ensures that pr and new have the same
1002 * prefix on a same interface.
1006 * Update prefix information. Note that the on-link (L) bit
1007 * and the autonomous (A) bit should NOT be changed from 1
1010 if (new->ndpr_raf_onlink
== 1)
1011 pr
->ndpr_raf_onlink
= 1;
1012 if (new->ndpr_raf_auto
== 1)
1013 pr
->ndpr_raf_auto
= 1;
1014 if (new->ndpr_raf_onlink
) {
1015 pr
->ndpr_vltime
= new->ndpr_vltime
;
1016 pr
->ndpr_pltime
= new->ndpr_pltime
;
1017 (void)in6_init_prefix_ltimes(pr
); /* XXX error case? */
1018 pr
->ndpr_lastupdate
= time_second
;
1021 if (new->ndpr_raf_onlink
&&
1022 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
1025 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
1027 "prelist_update: failed to make "
1028 "the prefix %s/%d on-link on %s "
1030 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
1031 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
1032 /* proceed anyway. XXX: is it correct? */
1036 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
)
1039 struct nd_prefix
*newpr
= NULL
;
1043 if (new->ndpr_vltime
== 0)
1045 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
1048 error
= nd6_prelist_add(new, dr
, &newpr
);
1049 if (error
!= 0 || newpr
== NULL
) {
1050 nd6log((LOG_NOTICE
, "prelist_update: "
1051 "nd6_prelist_add failed for %s/%d on %s "
1052 "errno=%d, returnpr=%p\n",
1053 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
1054 new->ndpr_plen
, if_name(new->ndpr_ifp
),
1056 goto end
; /* we should just give up in this case. */
1060 * XXX: from the ND point of view, we can ignore a prefix
1061 * with the on-link bit being zero. However, we need a
1062 * prefix structure for references from autoconfigured
1063 * addresses. Thus, we explicitly make sure that the prefix
1064 * itself expires now.
1066 if (newpr
->ndpr_raf_onlink
== 0) {
1067 newpr
->ndpr_vltime
= 0;
1068 newpr
->ndpr_pltime
= 0;
1069 in6_init_prefix_ltimes(newpr
);
1076 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1077 * Note that pr must be non NULL at this point.
1080 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1081 if (!new->ndpr_raf_auto
)
1085 * 5.5.3 (b). the link-local prefix should have been ignored in
1089 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1090 if (new->ndpr_pltime
> new->ndpr_vltime
) {
1091 error
= EINVAL
; /* XXX: won't be used */
1096 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1097 * an address configured by stateless autoconfiguration already in the
1098 * list of addresses associated with the interface, and the Valid
1099 * Lifetime is not 0, form an address. We first check if we have
1100 * a matching prefix.
1101 * Note: we apply a clarification in rfc2462bis-02 here. We only
1102 * consider autoconfigured addresses while RFC2462 simply said
1105 IFADDR_FOREACH(ifa
, ifp
) {
1106 struct in6_ifaddr
*ifa6
;
1107 u_int32_t remaininglifetime
;
1109 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1112 ifa6
= (struct in6_ifaddr
*)ifa
;
1115 * We only consider autoconfigured addresses as per rfc2462bis.
1117 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
))
1121 * Spec is not clear here, but I believe we should concentrate
1122 * on unicast (i.e. not anycast) addresses.
1123 * XXX: other ia6_flags? detached or duplicated?
1125 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0)
1129 * Ignore the address if it is not associated with a prefix
1130 * or is associated with a prefix that is different from this
1131 * one. (pr is never NULL here)
1133 if (ifa6
->ia6_ndpr
!= pr
)
1136 if (ia6_match
== NULL
) /* remember the first one */
1140 * An already autoconfigured address matched. Now that we
1141 * are sure there is at least one matched address, we can
1142 * proceed to 5.5.3. (e): update the lifetimes according to the
1143 * "two hours" rule and the privacy extension.
1144 * We apply some clarifications in rfc2462bis:
1145 * - use remaininglifetime instead of storedlifetime as a
1147 * - remove the dead code in the "two-hour" rule
1149 #define TWOHOUR (120*60)
1150 lt6_tmp
= ifa6
->ia6_lifetime
;
1151 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
1152 remaininglifetime
= ND6_INFINITE_LIFETIME
;
1153 else if (time_second
- ifa6
->ia6_updatetime
>
1154 lt6_tmp
.ia6t_vltime
) {
1156 * The case of "invalid" address. We should usually
1157 * not see this case.
1159 remaininglifetime
= 0;
1161 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
1162 (time_second
- ifa6
->ia6_updatetime
);
1164 /* when not updating, keep the current stored lifetime. */
1165 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
1167 if (TWOHOUR
< new->ndpr_vltime
||
1168 remaininglifetime
< new->ndpr_vltime
) {
1169 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
1170 } else if (remaininglifetime
<= TWOHOUR
) {
1172 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
1175 * new->ndpr_vltime <= TWOHOUR &&
1176 * TWOHOUR < remaininglifetime
1178 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
1181 /* The 2 hour rule is not imposed for preferred lifetime. */
1182 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
1184 in6_init_address_ltimes(pr
, <6_tmp
);
1187 * We need to treat lifetimes for temporary addresses
1188 * differently, according to
1189 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1190 * we only update the lifetimes when they are in the maximum
1193 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1194 u_int32_t maxvltime
, maxpltime
;
1196 if (ip6_temp_valid_lifetime
>
1197 (u_int32_t
)((time_second
- ifa6
->ia6_createtime
) +
1198 ip6_desync_factor
)) {
1199 maxvltime
= ip6_temp_valid_lifetime
-
1200 (time_second
- ifa6
->ia6_createtime
) -
1204 if (ip6_temp_preferred_lifetime
>
1205 (u_int32_t
)((time_second
- ifa6
->ia6_createtime
) +
1206 ip6_desync_factor
)) {
1207 maxpltime
= ip6_temp_preferred_lifetime
-
1208 (time_second
- ifa6
->ia6_createtime
) -
1213 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
1214 lt6_tmp
.ia6t_vltime
> maxvltime
) {
1215 lt6_tmp
.ia6t_vltime
= maxvltime
;
1217 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
1218 lt6_tmp
.ia6t_pltime
> maxpltime
) {
1219 lt6_tmp
.ia6t_pltime
= maxpltime
;
1223 ifa6
->ia6_lifetime
= lt6_tmp
;
1224 ifa6
->ia6_updatetime
= time_second
;
1226 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
1230 * 5.5.3 (d) (continued)
1231 * No address matched and the valid lifetime is non-zero.
1232 * Create a new address.
1236 * Prefix Length check:
1237 * If the sum of the prefix length and interface identifier
1238 * length does not equal 128 bits, the Prefix Information
1239 * option MUST be ignored. The length of the interface
1240 * identifier is defined in a separate link-type specific
1243 ifidlen
= in6_if2idlen(ifp
);
1245 /* this should not happen, so we always log it. */
1246 log(LOG_ERR
, "prelist_update: IFID undefined (%s)\n",
1250 if (ifidlen
+ pr
->ndpr_plen
!= 128) {
1252 "prelist_update: invalid prefixlen "
1253 "%d for %s, ignored\n",
1254 pr
->ndpr_plen
, if_name(ifp
)));
1258 if ((ia6
= in6_ifadd(new, mcast
)) != NULL
) {
1260 * note that we should use pr (not new) for reference.
1266 * draft-ietf-ipngwg-temp-addresses-v2-00 3.3 (2).
1267 * When a new public address is created as described
1268 * in RFC2462, also create a new temporary address.
1270 * draft-ietf-ipngwg-temp-addresses-v2-00 3.5.
1271 * When an interface connects to a new link, a new
1272 * randomized interface identifier should be generated
1273 * immediately together with a new set of temporary
1274 * addresses. Thus, we specifiy 1 as the 2nd arg of
1277 if (ip6_use_tempaddr
) {
1279 if ((e
= in6_tmpifadd(ia6
, 1, 1)) != 0) {
1280 nd6log((LOG_NOTICE
, "prelist_update: "
1281 "failed to create a temporary "
1282 "address, errno=%d\n",
1288 * A newly added address might affect the status
1289 * of other addresses, so we check and update it.
1290 * XXX: what if address duplication happens?
1292 pfxlist_onlink_check();
1294 /* just set an error. do not bark here. */
1295 error
= EADDRNOTAVAIL
; /* XXX: might be unused. */
1305 * A supplement function used in the on-link detection below;
1306 * detect if a given prefix has a (probably) reachable advertising router.
1307 * XXX: lengthy function name...
1309 static struct nd_pfxrouter
*
1310 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
1312 struct nd_pfxrouter
*pfxrtr
;
1314 struct llinfo_nd6
*ln
;
1316 for (pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
); pfxrtr
;
1317 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
)) {
1318 if ((rt
= nd6_lookup(&pfxrtr
->router
->rtaddr
, 0,
1319 pfxrtr
->router
->ifp
)) &&
1320 (ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) &&
1321 ND6_IS_LLINFO_PROBREACH(ln
))
1329 * Check if each prefix in the prefix list has at least one available router
1330 * that advertised the prefix (a router is "available" if its neighbor cache
1331 * entry is reachable or probably reachable).
1332 * If the check fails, the prefix may be off-link, because, for example,
1333 * we have moved from the network but the lifetime of the prefix has not
1334 * expired yet. So we should not use the prefix if there is another prefix
1335 * that has an available router.
1336 * But, if there is no prefix that has an available router, we still regards
1337 * all the prefixes as on-link. This is because we can't tell if all the
1338 * routers are simply dead or if we really moved from the network and there
1339 * is no router around us.
1342 pfxlist_onlink_check(void)
1344 struct nd_prefix
*pr
;
1345 struct in6_ifaddr
*ifa
;
1346 struct nd_defrouter
*dr
;
1347 struct nd_pfxrouter
*pfxrtr
= NULL
;
1350 * Check if there is a prefix that has a reachable advertising
1353 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1354 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
))
1358 * If we have no such prefix, check whether we still have a router
1359 * that does not advertise any prefixes.
1362 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
1363 struct nd_prefix
*pr0
;
1365 LIST_FOREACH(pr0
, &nd_prefix
, ndpr_entry
) {
1366 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
)
1373 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && !pfxrtr
)) {
1375 * There is at least one prefix that has a reachable router,
1376 * or at least a router which probably does not advertise
1377 * any prefixes. The latter would be the case when we move
1378 * to a new link where we have a router that does not provide
1379 * prefixes and we configure an address by hand.
1380 * Detach prefixes which have no reachable advertising
1381 * router, and attach other prefixes.
1383 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1384 /* XXX: a link-local prefix should never be detached */
1385 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1389 * we aren't interested in prefixes without the L bit
1392 if (pr
->ndpr_raf_onlink
== 0)
1395 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
1396 find_pfxlist_reachable_router(pr
) == NULL
)
1397 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
1398 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
1399 find_pfxlist_reachable_router(pr
) != 0)
1400 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
1403 /* there is no prefix that has a reachable router */
1404 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1405 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1408 if (pr
->ndpr_raf_onlink
== 0)
1411 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
1412 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
1417 * Remove each interface route associated with a (just) detached
1418 * prefix, and reinstall the interface route for a (just) attached
1419 * prefix. Note that all attempt of reinstallation does not
1420 * necessarily success, when a same prefix is shared among multiple
1421 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1422 * so we don't have to care about them.
1424 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1427 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
))
1430 if (pr
->ndpr_raf_onlink
== 0)
1433 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
1434 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
1435 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
1437 "pfxlist_onlink_check: failed to "
1438 "make %s/%d offlink, errno=%d\n",
1439 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
1443 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
1444 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
1445 pr
->ndpr_raf_onlink
) {
1446 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
1448 "pfxlist_onlink_check: failed to "
1449 "make %s/%d onlink, errno=%d\n",
1450 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
1457 * Changes on the prefix status might affect address status as well.
1458 * Make sure that all addresses derived from an attached prefix are
1459 * attached, and that all addresses derived from a detached prefix are
1460 * detached. Note, however, that a manually configured address should
1461 * always be attached.
1462 * The precise detection logic is same as the one for prefixes.
1464 for (ifa
= in6_ifaddr
; ifa
; ifa
= ifa
->ia_next
) {
1465 if (!(ifa
->ia6_flags
& IN6_IFF_AUTOCONF
))
1468 if (ifa
->ia6_ndpr
== NULL
) {
1470 * This can happen when we first configure the address
1471 * (i.e. the address exists, but the prefix does not).
1472 * XXX: complicated relationships...
1477 if (find_pfxlist_reachable_router(ifa
->ia6_ndpr
))
1481 for (ifa
= in6_ifaddr
; ifa
; ifa
= ifa
->ia_next
) {
1482 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1485 if (ifa
->ia6_ndpr
== NULL
) /* XXX: see above. */
1488 if (find_pfxlist_reachable_router(ifa
->ia6_ndpr
)) {
1489 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
1490 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
1491 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1492 nd6_dad_start((struct ifaddr
*)ifa
,
1496 if ((ifa
->ia6_flags
& IN6_IFF_DETACHED
) == 0) {
1497 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
1503 for (ifa
= in6_ifaddr
; ifa
; ifa
= ifa
->ia_next
) {
1504 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1507 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
1508 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
1509 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1510 /* Do we need a delay in this case? */
1511 nd6_dad_start((struct ifaddr
*)ifa
, 0);
1518 nd6_prefix_onlink(struct nd_prefix
*pr
)
1521 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1522 struct sockaddr_in6 mask6
;
1523 struct nd_prefix
*opr
;
1526 struct rtentry
*rt
= NULL
;
1529 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
1531 "nd6_prefix_onlink: %s/%d is already on-link\n",
1532 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
));
1537 * Add the interface route associated with the prefix. Before
1538 * installing the route, check if there's the same prefix on another
1539 * interface, and the prefix has already installed the interface route.
1540 * Although such a configuration is expected to be rare, we explicitly
1543 LIST_FOREACH(opr
, &nd_prefix
, ndpr_entry
) {
1547 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0)
1550 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
1551 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1552 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
))
1557 * We prefer link-local addresses as the associated interface address.
1559 /* search for a link-local addr */
1560 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
1561 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
1563 /* XXX: freebsd does not have ifa_ifwithaf */
1564 IFADDR_FOREACH(ifa
, ifp
) {
1565 if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
1568 /* should we care about ia6_flags? */
1572 * This can still happen, when, for example, we receive an RA
1573 * containing a prefix with the L bit set and the A bit clear,
1574 * after removing all IPv6 addresses on the receiving
1575 * interface. This should, of course, be rare though.
1578 "nd6_prefix_onlink: failed to find any ifaddr"
1579 " to add route for a prefix(%s/%d) on %s\n",
1580 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
1581 pr
->ndpr_plen
, if_name(ifp
)));
1586 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1587 * ifa->ifa_rtrequest = nd6_rtrequest;
1589 memset(&mask6
, 0, sizeof(mask6
));
1590 mask6
.sin6_len
= sizeof(mask6
);
1591 mask6
.sin6_addr
= pr
->ndpr_mask
;
1592 /* rtrequest() will probably set RTF_UP, but we're not sure. */
1593 rtflags
= ifa
->ifa_flags
| RTF_UP
;
1594 if (nd6_need_cache(ifp
)) {
1595 /* explicitly set in case ifa_flags does not set the flag. */
1596 rtflags
|= RTF_CLONING
;
1599 * explicitly clear the cloning bit in case ifa_flags sets it.
1601 rtflags
&= ~RTF_CLONING
;
1603 error
= rtrequest(RTM_ADD
, (struct sockaddr
*)&pr
->ndpr_prefix
,
1604 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
);
1606 if (rt
!= NULL
) /* this should be non NULL, though */
1607 nd6_rtmsg(RTM_ADD
, rt
);
1608 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
1610 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a"
1611 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1613 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
1614 pr
->ndpr_plen
, if_name(ifp
),
1615 ip6_sprintf(&((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
),
1616 ip6_sprintf(&mask6
.sin6_addr
), rtflags
, error
));
1626 nd6_prefix_offlink(struct nd_prefix
*pr
)
1629 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1630 struct nd_prefix
*opr
;
1631 struct sockaddr_in6 sa6
, mask6
;
1632 struct rtentry
*rt
= NULL
;
1635 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
1637 "nd6_prefix_offlink: %s/%d is already off-link\n",
1638 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
));
1642 sockaddr_in6_init(&sa6
, &pr
->ndpr_prefix
.sin6_addr
, 0, 0, 0);
1643 sockaddr_in6_init(&mask6
, &pr
->ndpr_mask
, 0, 0, 0);
1644 error
= rtrequest(RTM_DELETE
, (struct sockaddr
*)&sa6
, NULL
,
1645 (struct sockaddr
*)&mask6
, 0, &rt
);
1647 pr
->ndpr_stateflags
&= ~NDPRF_ONLINK
;
1649 /* report the route deletion to the routing socket. */
1651 nd6_rtmsg(RTM_DELETE
, rt
);
1654 * There might be the same prefix on another interface,
1655 * the prefix which could not be on-link just because we have
1656 * the interface route (see comments in nd6_prefix_onlink).
1657 * If there's one, try to make the prefix on-link on the
1660 LIST_FOREACH(opr
, &nd_prefix
, ndpr_entry
) {
1664 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0)
1668 * KAME specific: detached prefixes should not be
1671 if ((opr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
1674 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
1675 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1676 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1679 if ((e
= nd6_prefix_onlink(opr
)) != 0) {
1681 "nd6_prefix_offlink: failed to "
1682 "recover a prefix %s/%d from %s "
1683 "to %s (errno = %d)\n",
1684 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
1685 opr
->ndpr_plen
, if_name(ifp
),
1686 if_name(opr
->ndpr_ifp
), e
));
1691 /* XXX: can we still set the NDPRF_ONLINK flag? */
1693 "nd6_prefix_offlink: failed to delete route: "
1694 "%s/%d on %s (errno = %d)\n",
1695 ip6_sprintf(&sa6
.sin6_addr
), pr
->ndpr_plen
, if_name(ifp
),
1700 if (rt
->rt_refcnt
<= 0) {
1701 /* XXX: we should free the entry ourselves. */
1710 static struct in6_ifaddr
*
1711 in6_ifadd(struct nd_prefixctl
*pr
, int mcast
)
1713 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1715 struct in6_aliasreq ifra
;
1716 struct in6_ifaddr
*ia
, *ib
;
1718 struct in6_addr mask
;
1719 int prefixlen
= pr
->ndpr_plen
;
1722 in6_prefixlen2mask(&mask
, prefixlen
);
1725 * find a link-local address (will be interface ID).
1726 * Is it really mandatory? Theoretically, a global or a site-local
1727 * address can be configured without a link-local address, if we
1728 * have a unique interface identifier...
1730 * it is not mandatory to have a link-local address, we can generate
1731 * interface identifier on the fly. we do this because:
1732 * (1) it should be the easiest way to find interface identifier.
1733 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1734 * for multiple addresses on a single interface, and possible shortcut
1735 * of DAD. we omitted DAD for this reason in the past.
1736 * (3) a user can prevent autoconfiguration of global address
1737 * by removing link-local address by hand (this is partly because we
1738 * don't have other way to control the use of IPv6 on an interface.
1739 * this has been our design choice - cf. NRL's "ifconfig auto").
1740 * (4) it is easier to manage when an interface has addresses
1741 * with the same interface identifier, than to have multiple addresses
1742 * with different interface identifiers.
1744 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0); /* 0 is OK? */
1746 ib
= (struct in6_ifaddr
*)ifa
;
1750 #if 0 /* don't care link local addr state, and always do DAD */
1751 /* if link-local address is not eligible, do not autoconfigure. */
1752 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
1753 printf("in6_ifadd: link-local address not ready\n");
1758 /* prefixlen + ifidlen must be equal to 128 */
1759 plen0
= in6_mask2len(&ib
->ia_prefixmask
.sin6_addr
, NULL
);
1760 if (prefixlen
!= plen0
) {
1761 nd6log((LOG_INFO
, "in6_ifadd: wrong prefixlen for %s "
1762 "(prefix=%d ifid=%d)\n",
1763 if_name(ifp
), prefixlen
, 128 - plen0
));
1769 memset(&ifra
, 0, sizeof(ifra
));
1771 * in6_update_ifa() does not use ifra_name, but we accurately set it
1774 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
1775 sockaddr_in6_init(&ifra
.ifra_addr
, &pr
->ndpr_prefix
.sin6_addr
, 0, 0, 0);
1777 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1778 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1779 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1780 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1783 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
1784 (ib
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
1785 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
1786 (ib
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
1787 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
1788 (ib
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
1789 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
1790 (ib
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
1792 /* new prefix mask. */
1793 sockaddr_in6_init(&ifra
.ifra_prefixmask
, &mask
, 0, 0, 0);
1796 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
1797 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
1799 /* XXX: scope zone ID? */
1801 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
1804 * Make sure that we do not have this address already. This should
1805 * usually not happen, but we can still see this case, e.g., if we
1806 * have manually configured the exact address to be configured.
1808 if (in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
) != NULL
) {
1809 /* this should be rare enough to make an explicit log */
1810 log(LOG_INFO
, "in6_ifadd: %s is already configured\n",
1811 ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
1816 * Allocate ifaddr structure, link into chain, etc.
1817 * If we are going to create a new address upon receiving a multicasted
1818 * RA, we need to impose a random delay before starting DAD.
1819 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1823 updateflags
|= IN6_IFAUPDATE_DADDELAY
;
1824 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
, updateflags
)) != 0) {
1826 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1827 ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
), if_name(ifp
),
1829 return (NULL
); /* ifaddr must not have been allocated. */
1832 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
);
1834 return (ia
); /* this is always non-NULL */
1839 const struct in6_ifaddr
*ia0
, /* corresponding public address */
1843 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
1844 struct in6_ifaddr
*newia
, *ia
;
1845 struct in6_aliasreq ifra
;
1847 int trylimit
= 3; /* XXX: adhoc value */
1849 u_int32_t randid
[2];
1850 u_int32_t vltime0
, pltime0
;
1852 memset(&ifra
, 0, sizeof(ifra
));
1853 strncpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
1854 ifra
.ifra_addr
= ia0
->ia_addr
;
1855 /* copy prefix mask */
1856 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
1857 /* clear the old IFID */
1858 for (i
= 0; i
< 4; i
++) {
1859 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
] &=
1860 ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1864 if (in6_get_tmpifid(ifp
, (u_int8_t
*)randid
,
1865 (const u_int8_t
*)&ia0
->ia_addr
.sin6_addr
.s6_addr
[8], forcegen
)) {
1866 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find a good "
1870 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
1871 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
1872 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
1873 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
1876 * in6_get_tmpifid() quite likely provided a unique interface ID.
1877 * However, we may still have a chance to see collision, because
1878 * there may be a time lag between generation of the ID and generation
1879 * of the address. So, we'll do one more sanity check.
1881 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
1882 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
1883 &ifra
.ifra_addr
.sin6_addr
)) {
1884 if (trylimit
-- == 0) {
1886 * Give up. Something strange should have
1889 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to "
1890 "find a unique random IFID\n"));
1899 * The Valid Lifetime is the lower of the Valid Lifetime of the
1900 * public address or TEMP_VALID_LIFETIME.
1901 * The Preferred Lifetime is the lower of the Preferred Lifetime
1902 * of the public address or TEMP_PREFERRED_LIFETIME -
1905 if (ia0
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1906 vltime0
= IFA6_IS_INVALID(ia0
) ? 0 :
1907 (ia0
->ia6_lifetime
.ia6t_vltime
-
1908 (time_second
- ia0
->ia6_updatetime
));
1909 if (vltime0
> ip6_temp_valid_lifetime
)
1910 vltime0
= ip6_temp_valid_lifetime
;
1912 vltime0
= ip6_temp_valid_lifetime
;
1913 if (ia0
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1914 pltime0
= IFA6_IS_DEPRECATED(ia0
) ? 0 :
1915 (ia0
->ia6_lifetime
.ia6t_pltime
-
1916 (time_second
- ia0
->ia6_updatetime
));
1917 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
){
1918 pltime0
= ip6_temp_preferred_lifetime
-
1922 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
1923 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
1924 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
1927 * A temporary address is created only if this calculated Preferred
1928 * Lifetime is greater than REGEN_ADVANCE time units.
1930 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
)
1933 /* XXX: scope zone ID? */
1935 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
1937 /* allocate ifaddr structure, link into chain, etc. */
1940 updateflags
|= IN6_IFAUPDATE_DADDELAY
;
1941 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
, updateflags
)) != 0)
1944 newia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
);
1945 if (newia
== NULL
) { /* XXX: can it happen? */
1947 "in6_tmpifadd: ifa update succeeded, but we got "
1949 return (EINVAL
); /* XXX */
1951 newia
->ia6_ndpr
= ia0
->ia6_ndpr
;
1952 newia
->ia6_ndpr
->ndpr_refcnt
++;
1955 * A newly added address might affect the status of other addresses.
1956 * XXX: when the temporary address is generated with a new public
1957 * address, the onlink check is redundant. However, it would be safe
1958 * to do the check explicitly everywhere a new address is generated,
1959 * and, in fact, we surely need the check when we create a new
1960 * temporary address due to deprecation of an old temporary address.
1962 pfxlist_onlink_check();
1968 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
1971 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */
1972 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
1973 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime"
1974 "(%d) is greater than valid lifetime(%d)\n",
1975 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
));
1978 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
1979 ndpr
->ndpr_preferred
= 0;
1981 ndpr
->ndpr_preferred
= time_second
+ ndpr
->ndpr_pltime
;
1982 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
1983 ndpr
->ndpr_expire
= 0;
1985 ndpr
->ndpr_expire
= time_second
+ ndpr
->ndpr_vltime
;
1991 in6_init_address_ltimes(struct nd_prefix
*new,
1992 struct in6_addrlifetime
*lt6
)
1995 /* Valid lifetime must not be updated unless explicitly specified. */
1996 /* init ia6t_expire */
1997 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
)
1998 lt6
->ia6t_expire
= 0;
2000 lt6
->ia6t_expire
= time_second
;
2001 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
2004 /* init ia6t_preferred */
2005 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
)
2006 lt6
->ia6t_preferred
= 0;
2008 lt6
->ia6t_preferred
= time_second
;
2009 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
2014 * Delete all the routing table entries that use the specified gateway.
2015 * XXX: this function causes search through all entries of routing table, so
2016 * it shouldn't be called when acting as a router.
2019 rt6_flush(struct in6_addr
*gateway
, struct ifnet
*ifp
)
2021 int s
= splsoftnet();
2023 /* We'll care only link-local addresses */
2024 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
2029 rt_walktree(AF_INET6
, rt6_deleteroute
, (void *)gateway
);
2034 rt6_deleteroute(struct rtentry
*rt
, void *arg
)
2036 #define SIN6(s) ((struct sockaddr_in6 *)s)
2037 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
2039 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
)
2042 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
))
2046 * Do not delete a static route.
2047 * XXX: this seems to be a bit ad-hoc. Should we consider the
2048 * 'cloned' bit instead?
2050 if ((rt
->rt_flags
& RTF_STATIC
) != 0)
2054 * We delete only host route. This means, in particular, we don't
2055 * delete default route.
2057 if ((rt
->rt_flags
& RTF_HOST
) == 0)
2060 return (rtrequest(RTM_DELETE
, rt_getkey(rt
), rt
->rt_gateway
,
2061 rt_mask(rt
), rt
->rt_flags
, 0));
2066 nd6_setdefaultiface(int ifindex
)
2070 if (ifindex
< 0 || if_indexlim
<= ifindex
)
2072 if (ifindex
!= 0 && !ifindex2ifnet
[ifindex
])
2075 if (nd6_defifindex
!= ifindex
) {
2076 nd6_defifindex
= ifindex
;
2077 if (nd6_defifindex
> 0) {
2078 nd6_defifp
= ifindex2ifnet
[nd6_defifindex
];
2083 * Our current implementation assumes one-to-one maping between
2084 * interfaces and links, so it would be natural to use the
2085 * default interface as the default link.
2087 scope6_setdefault(nd6_defifp
);