2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
34 #include <sys/cdefs.h>
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/refcount.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
46 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/rmlock.h>
50 #include <sys/rwlock.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
53 #include <sys/queue.h>
56 #include <net/if_var.h>
57 #include <net/if_private.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <net/route/nhop.h>
62 #include <net/route/route_ctl.h>
63 #include <net/radix.h>
66 #include <netinet/in.h>
67 #include <net/if_llatbl.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/in6_ifattach.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/nd6.h>
73 #include <netinet/icmp6.h>
74 #include <netinet6/scope6_var.h>
76 static struct nd_defrouter
*defrtrlist_update(struct nd_defrouter
*);
77 static int prelist_update(struct nd_prefixctl
*, struct nd_defrouter
*,
79 static int nd6_prefix_onlink(struct nd_prefix
*);
81 TAILQ_HEAD(nd6_drhead
, nd_defrouter
);
82 VNET_DEFINE_STATIC(struct nd6_drhead
, nd6_defrouter
);
83 #define V_nd6_defrouter VNET(nd6_defrouter)
85 VNET_DECLARE(int, nd6_recalc_reachtm_interval
);
86 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
88 VNET_DEFINE_STATIC(struct ifnet
*, nd6_defifp
);
89 VNET_DEFINE(int, nd6_defifindex
);
90 #define V_nd6_defifp VNET(nd6_defifp)
92 VNET_DEFINE(int, ip6_use_tempaddr
) = 0;
94 VNET_DEFINE(int, ip6_desync_factor
);
95 VNET_DEFINE(u_int32_t
, ip6_temp_preferred_lifetime
) = DEF_TEMP_PREFERRED_LIFETIME
;
96 VNET_DEFINE(u_int32_t
, ip6_temp_valid_lifetime
) = DEF_TEMP_VALID_LIFETIME
;
98 VNET_DEFINE(int, ip6_temp_regen_advance
) = TEMPADDR_REGEN_ADVANCE
;
101 VNET_DEFINE_STATIC(int, nd6_ignore_ipv6_only_ra
) = 1;
102 #define V_nd6_ignore_ipv6_only_ra VNET(nd6_ignore_ipv6_only_ra)
103 SYSCTL_INT(_net_inet6_icmp6
, OID_AUTO
,
104 nd6_ignore_ipv6_only_ra
, CTLFLAG_VNET
| CTLFLAG_RW
,
105 &VNET_NAME(nd6_ignore_ipv6_only_ra
), 0,
106 "Ignore the 'IPv6-Only flag' in RA messages in compliance with "
107 "draft-ietf-6man-ipv6only-flag");
110 /* RTPREF_MEDIUM has to be 0! */
111 #define RTPREF_HIGH 1
112 #define RTPREF_MEDIUM 0
113 #define RTPREF_LOW (-1)
114 #define RTPREF_RESERVED (-2)
115 #define RTPREF_INVALID (-3) /* internal */
118 defrouter_ref(struct nd_defrouter
*dr
)
121 refcount_acquire(&dr
->refcnt
);
125 defrouter_rele(struct nd_defrouter
*dr
)
128 if (refcount_release(&dr
->refcnt
))
133 * Remove a router from the global list and optionally stash it in a
134 * caller-supplied queue.
137 defrouter_unlink(struct nd_defrouter
*dr
, struct nd6_drhead
*drq
)
142 TAILQ_REMOVE(&V_nd6_defrouter
, dr
, dr_entry
);
145 TAILQ_INSERT_TAIL(drq
, dr
, dr_entry
);
149 * Receive Router Solicitation Message - just for routers.
150 * Router solicitation/advertisement is mostly managed by userland program
151 * (rtadvd) so here we have no function like nd6_ra_output().
156 nd6_rs_input(struct mbuf
*m
, int off
, int icmp6len
)
160 struct nd_router_solicit
*nd_rs
;
161 struct in6_addr saddr6
;
162 union nd_opts ndopts
;
163 char ip6bufs
[INET6_ADDRSTRLEN
], ip6bufd
[INET6_ADDRSTRLEN
];
167 ifp
= m
->m_pkthdr
.rcvif
;
170 * Accept RS only when V_ip6_forwarding=1 and the interface has
171 * no ND6_IFF_ACCEPT_RTADV.
173 if (!V_ip6_forwarding
|| ND_IFINFO(ifp
)->flags
& ND6_IFF_ACCEPT_RTADV
)
176 /* RFC 6980: Nodes MUST silently ignore fragments */
177 if(m
->m_flags
& M_FRAGMENTED
)
181 ip6
= mtod(m
, struct ip6_hdr
*);
182 if (__predict_false(ip6
->ip6_hlim
!= 255)) {
183 ICMP6STAT_INC(icp6s_invlhlim
);
185 "%s: invalid hlim (%d) from %s to %s on %s\n", __func__
,
186 ip6
->ip6_hlim
, ip6_sprintf(ip6bufs
, &ip6
->ip6_src
),
187 ip6_sprintf(ip6bufd
, &ip6
->ip6_dst
), if_name(ifp
)));
192 * Don't update the neighbor cache, if src = ::.
193 * This indicates that the src has no IP address assigned yet.
195 saddr6
= ip6
->ip6_src
;
196 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
))
199 if (m
->m_len
< off
+ icmp6len
) {
200 m
= m_pullup(m
, off
+ icmp6len
);
202 IP6STAT_INC(ip6s_exthdrtoolong
);
206 ip6
= mtod(m
, struct ip6_hdr
*);
207 nd_rs
= (struct nd_router_solicit
*)((caddr_t
)ip6
+ off
);
209 icmp6len
-= sizeof(*nd_rs
);
210 nd6_option_init(nd_rs
+ 1, icmp6len
, &ndopts
);
211 if (nd6_options(&ndopts
) < 0) {
213 "%s: invalid ND option, ignored\n", __func__
));
214 /* nd6_options have incremented stats */
220 if (ndopts
.nd_opts_src_lladdr
) {
221 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
222 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
225 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
227 "%s: lladdrlen mismatch for %s (if %d, RS packet %d)\n",
228 __func__
, ip6_sprintf(ip6bufs
, &saddr6
),
229 ifp
->if_addrlen
, lladdrlen
- 2));
233 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_SOLICIT
, 0);
240 ICMP6STAT_INC(icp6s_badrs
);
246 * An initial update routine for draft-ietf-6man-ipv6only-flag.
247 * We need to iterate over all default routers for the given
248 * interface to see whether they are all advertising the "S"
249 * (IPv6-Only) flag. If they do set, otherwise unset, the
250 * interface flag we later use to filter on.
253 defrtr_ipv6_only_ifp(struct ifnet
*ifp
)
255 struct nd_defrouter
*dr
;
256 bool ipv6_only
, ipv6_only_old
;
258 struct epoch_tracker et
;
263 if (V_nd6_ignore_ipv6_only_ra
!= 0)
268 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
)
269 if (dr
->ifp
== ifp
&&
270 (dr
->raflags
& ND_RA_FLAG_IPV6_ONLY
) == 0)
274 IF_AFDATA_WLOCK(ifp
);
275 ipv6_only_old
= ND_IFINFO(ifp
)->flags
& ND6_IFF_IPV6_ONLY
;
276 IF_AFDATA_WUNLOCK(ifp
);
278 /* If nothing changed, we have an early exit. */
279 if (ipv6_only
== ipv6_only_old
)
284 * Should we want to set the IPV6-ONLY flag, check if the
285 * interface has a non-0/0 and non-link-local IPv4 address
286 * configured on it. If it has we will assume working
287 * IPv4 operations and will clear the interface flag.
289 has_ipv4_addr
= false;
292 CK_STAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
293 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
296 satosin(ifa
->ifa_addr
)->sin_addr
)) {
297 has_ipv4_addr
= true;
303 if (ipv6_only
&& has_ipv4_addr
) {
304 log(LOG_NOTICE
, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
305 "configured, ignoring IPv6-Only flag.\n", ifp
->if_xname
);
310 IF_AFDATA_WLOCK(ifp
);
312 ND_IFINFO(ifp
)->flags
|= ND6_IFF_IPV6_ONLY
;
314 ND_IFINFO(ifp
)->flags
&= ~ND6_IFF_IPV6_ONLY
;
315 IF_AFDATA_WUNLOCK(ifp
);
318 /* Send notification of flag change. */
323 defrtr_ipv6_only_ipf_down(struct ifnet
*ifp
)
326 IF_AFDATA_WLOCK(ifp
);
327 ND_IFINFO(ifp
)->flags
&= ~ND6_IFF_IPV6_ONLY
;
328 IF_AFDATA_WUNLOCK(ifp
);
330 #endif /* EXPERIMENTAL */
333 nd6_ifnet_link_event(void *arg __unused
, struct ifnet
*ifp
, int linkstate
)
337 * XXX-BZ we might want to trigger re-evaluation of our default router
338 * availability. E.g., on link down the default router might be
339 * unreachable but a different interface might still have connectivity.
343 if (linkstate
== LINK_STATE_DOWN
)
344 defrtr_ipv6_only_ipf_down(ifp
);
349 * Receive Router Advertisement Message.
352 * TODO: on-link bit on prefix information
353 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
356 nd6_ra_input(struct mbuf
*m
, int off
, int icmp6len
)
359 struct nd_ifinfo
*ndi
;
361 struct nd_router_advert
*nd_ra
;
362 struct in6_addr saddr6
;
363 struct nd_defrouter
*dr
;
364 union nd_opts ndopts
;
365 char ip6bufs
[INET6_ADDRSTRLEN
], ip6bufd
[INET6_ADDRSTRLEN
];
369 * We only accept RAs only when the per-interface flag
370 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
372 ifp
= m
->m_pkthdr
.rcvif
;
373 ndi
= ND_IFINFO(ifp
);
374 if (!(ndi
->flags
& ND6_IFF_ACCEPT_RTADV
))
377 /* RFC 6980: Nodes MUST silently ignore fragments */
378 if(m
->m_flags
& M_FRAGMENTED
)
381 ip6
= mtod(m
, struct ip6_hdr
*);
382 if (__predict_false(ip6
->ip6_hlim
!= 255)) {
383 ICMP6STAT_INC(icp6s_invlhlim
);
385 "%s: invalid hlim (%d) from %s to %s on %s\n", __func__
,
386 ip6
->ip6_hlim
, ip6_sprintf(ip6bufs
, &ip6
->ip6_src
),
387 ip6_sprintf(ip6bufd
, &ip6
->ip6_dst
), if_name(ifp
)));
391 saddr6
= ip6
->ip6_src
;
392 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) {
394 "%s: src %s is not link-local\n", __func__
,
395 ip6_sprintf(ip6bufs
, &saddr6
)));
399 if (m
->m_len
< off
+ icmp6len
) {
400 m
= m_pullup(m
, off
+ icmp6len
);
402 IP6STAT_INC(ip6s_exthdrtoolong
);
406 ip6
= mtod(m
, struct ip6_hdr
*);
407 nd_ra
= (struct nd_router_advert
*)((caddr_t
)ip6
+ off
);
409 icmp6len
-= sizeof(*nd_ra
);
410 nd6_option_init(nd_ra
+ 1, icmp6len
, &ndopts
);
411 if (nd6_options(&ndopts
) < 0) {
413 "%s: invalid ND option, ignored\n", __func__
));
414 /* nd6_options have incremented stats */
421 struct nd_defrouter dr0
;
422 u_int32_t advreachable
= nd_ra
->nd_ra_reachable
;
424 /* remember if this is a multicasted advertisement */
425 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
428 bzero(&dr0
, sizeof(dr0
));
430 dr0
.raflags
= nd_ra
->nd_ra_flags_reserved
;
432 * Effectively-disable routes from RA messages when
433 * ND6_IFF_NO_RADR enabled on the receiving interface or
434 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
436 if (ndi
->flags
& ND6_IFF_NO_RADR
)
438 else if (V_ip6_forwarding
&& !V_ip6_rfc6204w3
)
441 dr0
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
442 dr0
.expire
= time_uptime
+ dr0
.rtlifetime
;
444 /* unspecified or not? (RFC 2461 6.3.4) */
446 advreachable
= ntohl(advreachable
);
447 if (advreachable
<= MAX_REACHABLE_TIME
&&
448 ndi
->basereachable
!= advreachable
) {
449 ndi
->basereachable
= advreachable
;
450 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
451 ndi
->recalctm
= V_nd6_recalc_reachtm_interval
; /* reset */
454 if (nd_ra
->nd_ra_retransmit
)
455 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
456 if (nd_ra
->nd_ra_curhoplimit
) {
457 if (ndi
->chlim
< nd_ra
->nd_ra_curhoplimit
)
458 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
459 else if (ndi
->chlim
!= nd_ra
->nd_ra_curhoplimit
) {
460 log(LOG_ERR
, "RA with a lower CurHopLimit sent from "
461 "%s on %s (current = %d, received = %d). "
462 "Ignored.\n", ip6_sprintf(ip6bufs
, &ip6
->ip6_src
),
463 if_name(ifp
), ndi
->chlim
, nd_ra
->nd_ra_curhoplimit
);
466 dr
= defrtrlist_update(&dr0
);
468 defrtr_ipv6_only_ifp(ifp
);
475 if (ndopts
.nd_opts_pi
) {
476 struct nd_opt_hdr
*pt
;
477 struct nd_opt_prefix_info
*pi
= NULL
;
478 struct nd_prefixctl pr
;
480 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
481 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
482 pt
= (struct nd_opt_hdr
*)((caddr_t
)pt
+
483 (pt
->nd_opt_len
<< 3))) {
484 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
)
486 pi
= (struct nd_opt_prefix_info
*)pt
;
488 if (pi
->nd_opt_pi_len
!= 4) {
490 "%s: invalid option len %d for prefix "
491 "information option, ignored\n", __func__
,
496 if (128 < pi
->nd_opt_pi_prefix_len
) {
498 "%s: invalid prefix len %d for prefix "
499 "information option, ignored\n", __func__
,
500 pi
->nd_opt_pi_prefix_len
));
504 if (IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
)
505 || IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
507 "%s: invalid prefix %s, ignored\n",
508 __func__
, ip6_sprintf(ip6bufs
,
509 &pi
->nd_opt_pi_prefix
)));
513 bzero(&pr
, sizeof(pr
));
514 pr
.ndpr_prefix
.sin6_family
= AF_INET6
;
515 pr
.ndpr_prefix
.sin6_len
= sizeof(pr
.ndpr_prefix
);
516 pr
.ndpr_prefix
.sin6_addr
= pi
->nd_opt_pi_prefix
;
517 pr
.ndpr_ifp
= (struct ifnet
*)m
->m_pkthdr
.rcvif
;
519 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
520 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
521 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
522 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
523 pr
.ndpr_raf_ra_derived
= 1;
524 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
525 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
526 pr
.ndpr_pltime
= ntohl(pi
->nd_opt_pi_preferred_time
);
527 (void)prelist_update(&pr
, dr
, m
, mcast
);
538 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
542 mtu
= (u_long
)ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
545 if (mtu
< IPV6_MMTU
) {
546 nd6log((LOG_INFO
, "%s: bogus mtu option mtu=%lu sent "
547 "from %s, ignoring\n", __func__
,
548 mtu
, ip6_sprintf(ip6bufs
, &ip6
->ip6_src
)));
553 maxmtu
= (ndi
->maxmtu
&& ndi
->maxmtu
< ifp
->if_mtu
)
554 ? ndi
->maxmtu
: ifp
->if_mtu
;
556 int change
= (ndi
->linkmtu
!= mtu
);
560 /* in6_maxmtu may change */
565 nd6log((LOG_INFO
, "%s: bogus mtu=%lu sent from %s; "
566 "exceeds maxmtu %lu, ignoring\n", __func__
,
567 mtu
, ip6_sprintf(ip6bufs
, &ip6
->ip6_src
), maxmtu
));
574 * Source link layer address
580 if (ndopts
.nd_opts_src_lladdr
) {
581 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
582 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
585 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
587 "%s: lladdrlen mismatch for %s (if %d, RA packet %d)\n",
588 __func__
, ip6_sprintf(ip6bufs
, &saddr6
),
589 ifp
->if_addrlen
, lladdrlen
- 2));
593 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
,
594 lladdrlen
, ND_ROUTER_ADVERT
, 0);
597 * Installing a link-layer address might change the state of the
598 * router's neighbor cache, which might also affect our on-link
599 * detection of adveritsed prefixes.
601 pfxlist_onlink_check();
609 ICMP6STAT_INC(icp6s_badra
);
614 static struct nd_pfxrouter
*
615 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
617 struct nd_pfxrouter
*search
;
621 LIST_FOREACH(search
, &pr
->ndpr_advrtrs
, pfr_entry
) {
622 if (search
->router
== dr
)
629 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
631 struct nd_pfxrouter
*new;
637 if (pfxrtr_lookup(pr
, dr
) != NULL
) {
643 new = malloc(sizeof(*new), M_IP6NDP
, M_NOWAIT
| M_ZERO
);
650 if (pfxrtr_lookup(pr
, dr
) == NULL
) {
651 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
654 /* We lost a race to add the reference. */
662 pfxlist_onlink_check();
666 pfxrtr_del(struct nd_pfxrouter
*pfr
)
671 LIST_REMOVE(pfr
, pfr_entry
);
672 defrouter_rele(pfr
->router
);
676 /* Default router list processing sub routines. */
678 defrouter_addreq(struct nd_defrouter
*new)
680 uint32_t fibnum
= new->ifp
->if_fib
;
681 struct rib_cmd_info rc
= {};
686 struct sockaddr_in6 gw
= {
687 .sin6_family
= AF_INET6
,
688 .sin6_len
= sizeof(struct sockaddr_in6
),
689 .sin6_addr
= new->rtaddr
,
692 error
= rib_add_default_route(fibnum
, AF_INET6
, new->ifp
,
693 (struct sockaddr
*)&gw
, &rc
);
696 struct nhop_object
*nh
= nhop_select_func(rc
.rc_nh_new
, 0);
697 rt_routemsg(RTM_ADD
, rc
.rc_rt
, nh
, fibnum
);
703 * Remove the default route for a given router.
704 * This is just a subroutine function for defrouter_select_fib(), and
705 * should not be called from anywhere else.
708 defrouter_delreq(struct nd_defrouter
*dr
)
710 uint32_t fibnum
= dr
->ifp
->if_fib
;
711 struct epoch_tracker et
;
712 struct rib_cmd_info rc
;
715 struct sockaddr_in6 dst
= {
716 .sin6_family
= AF_INET6
,
717 .sin6_len
= sizeof(struct sockaddr_in6
),
720 struct sockaddr_in6 gw
= {
721 .sin6_family
= AF_INET6
,
722 .sin6_len
= sizeof(struct sockaddr_in6
),
723 .sin6_addr
= dr
->rtaddr
,
727 error
= rib_del_route_px(fibnum
, (struct sockaddr
*)&dst
, 0,
728 rib_match_gw
, (struct sockaddr
*)&gw
, 0, &rc
);
730 struct nhop_object
*nh
= nhop_select_func(rc
.rc_nh_old
, 0);
731 rt_routemsg(RTM_DELETE
, rc
.rc_rt
, nh
, fibnum
);
739 defrouter_del(struct nd_defrouter
*dr
)
741 struct nd_defrouter
*deldr
= NULL
;
742 struct nd_prefix
*pr
;
743 struct nd_pfxrouter
*pfxrtr
;
748 * Flush all the routing table entries that use the router
751 if (ND_IFINFO(dr
->ifp
)->flags
& ND6_IFF_ACCEPT_RTADV
)
752 rt6_flush(&dr
->rtaddr
, dr
->ifp
);
755 defrtr_ipv6_only_ifp(dr
->ifp
);
760 defrouter_delreq(dr
);
764 * Also delete all the pointers to the router in each prefix lists.
767 LIST_FOREACH(pr
, &V_nd_prefix
, ndpr_entry
) {
768 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
773 pfxlist_onlink_check();
776 * If the router is the primary one, choose a new one.
777 * Note that defrouter_select_fib() will remove the current
778 * gateway from the routing table.
781 defrouter_select_fib(deldr
->ifp
->if_fib
);
784 * Release the list reference.
789 struct nd_defrouter
*
790 defrouter_lookup_locked(const struct in6_addr
*addr
, struct ifnet
*ifp
)
792 struct nd_defrouter
*dr
;
795 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
)
796 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) {
803 struct nd_defrouter
*
804 defrouter_lookup(const struct in6_addr
*addr
, struct ifnet
*ifp
)
806 struct nd_defrouter
*dr
;
809 dr
= defrouter_lookup_locked(addr
, ifp
);
815 * Remove all default routes from default router list.
818 defrouter_reset(void)
820 struct nd_defrouter
*dr
, **dra
;
826 * We can't delete routes with the ND lock held, so make a copy of the
827 * current default router list and use that when deleting routes.
830 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
)
834 dra
= malloc(count
* sizeof(*dra
), M_TEMP
, M_WAITOK
| M_ZERO
);
837 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
) {
845 for (i
= 0; i
< count
&& dra
[i
] != NULL
; i
++) {
846 defrouter_delreq(dra
[i
]);
847 defrouter_rele(dra
[i
]);
852 * XXX should we also nuke any default routers in the kernel, by
853 * going through them by rtalloc1()?
858 * Look up a matching default router list entry and remove it. Returns true if a
859 * matching entry was found, false otherwise.
862 defrouter_remove(struct in6_addr
*addr
, struct ifnet
*ifp
)
864 struct nd_defrouter
*dr
;
867 dr
= defrouter_lookup_locked(addr
, ifp
);
873 defrouter_unlink(dr
, NULL
);
881 * for default router selection
882 * regards router-preference field as a 2-bit signed integer
885 rtpref(struct nd_defrouter
*dr
)
887 switch (dr
->raflags
& ND_RA_FLAG_RTPREF_MASK
) {
888 case ND_RA_FLAG_RTPREF_HIGH
:
889 return (RTPREF_HIGH
);
890 case ND_RA_FLAG_RTPREF_MEDIUM
:
891 case ND_RA_FLAG_RTPREF_RSV
:
892 return (RTPREF_MEDIUM
);
893 case ND_RA_FLAG_RTPREF_LOW
:
897 * This case should never happen. If it did, it would mean a
898 * serious bug of kernel internal. We thus always bark here.
899 * Or, can we even panic?
901 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->raflags
);
902 return (RTPREF_INVALID
);
908 is_dr_reachable(const struct nd_defrouter
*dr
) {
909 struct llentry
*ln
= NULL
;
911 ln
= nd6_lookup(&dr
->rtaddr
, LLE_SF(AF_INET6
, 0), dr
->ifp
);
914 bool reachable
= ND6_IS_LLINFO_PROBREACH(ln
);
920 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
921 * draft-ietf-ipngwg-router-selection:
922 * 1) Routers that are reachable or probably reachable should be preferred.
923 * If we have more than one (probably) reachable router, prefer ones
924 * with the highest router preference.
925 * 2) When no routers on the list are known to be reachable or
926 * probably reachable, routers SHOULD be selected in a round-robin
927 * fashion, regardless of router preference values.
928 * 3) If the Default Router List is empty, assume that all
929 * destinations are on-link.
931 * We assume nd_defrouter is sorted by router preference value.
932 * Since the code below covers both with and without router preference cases,
933 * we do not need to classify the cases by ifdef.
935 * At this moment, we do not try to install more than one default router,
936 * even when the multipath routing is available, because we're not sure about
937 * the benefits for stub hosts comparing to the risk of making the code
938 * complicated and the possibility of introducing bugs.
940 * We maintain a single list of routers for multiple FIBs, only considering one
941 * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
942 * we do the whole thing multiple times.
945 defrouter_select_fib(int fibnum
)
947 struct epoch_tracker et
;
948 struct nd_defrouter
*dr
, *selected_dr
, *installed_dr
;
950 if (fibnum
== RT_ALL_FIBS
) {
951 for (fibnum
= 0; fibnum
< rt_numfibs
; fibnum
++) {
952 defrouter_select_fib(fibnum
);
959 * Let's handle easy case (3) first:
960 * If default router list is empty, there's nothing to be done.
962 if (TAILQ_EMPTY(&V_nd6_defrouter
)) {
968 * Search for a (probably) reachable router from the list.
969 * We just pick up the first reachable one (if any), assuming that
970 * the ordering rule of the list described in defrtrlist_update().
972 selected_dr
= installed_dr
= NULL
;
974 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
) {
975 if (dr
->ifp
->if_fib
!= fibnum
)
978 if (selected_dr
== NULL
&& is_dr_reachable(dr
)) {
980 defrouter_ref(selected_dr
);
984 if (installed_dr
== NULL
) {
986 defrouter_ref(installed_dr
);
989 * this should not happen.
990 * warn for diagnosis.
992 log(LOG_ERR
, "defrouter_select_fib: more than "
993 "one router is installed\n");
999 * If none of the default routers was found to be reachable,
1000 * round-robin the list regardless of preference.
1001 * Otherwise, if we have an installed router, check if the selected
1002 * (reachable) router should really be preferred to the installed one.
1003 * We only prefer the new router when the old one is not reachable
1004 * or when the new one has a really higher preference value.
1006 if (selected_dr
== NULL
) {
1007 if (installed_dr
== NULL
||
1008 TAILQ_NEXT(installed_dr
, dr_entry
) == NULL
)
1009 dr
= TAILQ_FIRST(&V_nd6_defrouter
);
1011 dr
= TAILQ_NEXT(installed_dr
, dr_entry
);
1013 /* Ensure we select a router for this FIB. */
1014 TAILQ_FOREACH_FROM(dr
, &V_nd6_defrouter
, dr_entry
) {
1015 if (dr
->ifp
->if_fib
== fibnum
) {
1017 defrouter_ref(selected_dr
);
1021 } else if (installed_dr
!= NULL
) {
1022 if (is_dr_reachable(installed_dr
) &&
1023 rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1024 defrouter_rele(selected_dr
);
1025 selected_dr
= installed_dr
;
1031 * If we selected a router for this FIB and it's different
1032 * than the installed one, remove the installed router and
1033 * install the selected one in its place.
1035 if (installed_dr
!= selected_dr
) {
1036 if (installed_dr
!= NULL
) {
1037 defrouter_delreq(installed_dr
);
1038 defrouter_rele(installed_dr
);
1040 if (selected_dr
!= NULL
)
1041 defrouter_addreq(selected_dr
);
1043 if (selected_dr
!= NULL
)
1044 defrouter_rele(selected_dr
);
1048 static struct nd_defrouter
*
1049 defrtrlist_update(struct nd_defrouter
*new)
1051 struct nd_defrouter
*dr
, *n
;
1056 if (new->rtlifetime
== 0) {
1057 defrouter_remove(&new->rtaddr
, new->ifp
);
1062 writelocked
= false;
1064 dr
= defrouter_lookup_locked(&new->rtaddr
, new->ifp
);
1066 oldpref
= rtpref(dr
);
1069 dr
->raflags
= new->raflags
; /* XXX flag check */
1070 dr
->rtlifetime
= new->rtlifetime
;
1071 dr
->expire
= new->expire
;
1074 * If the preference does not change, there's no need
1075 * to sort the entries. Also make sure the selected
1076 * router is still installed in the kernel.
1078 if (dr
->installed
&& rtpref(new) == oldpref
) {
1088 * The router needs to be reinserted into the default router
1089 * list, so upgrade to a write lock. If that fails and the list
1090 * has potentially changed while the lock was dropped, we'll
1091 * redo the lookup with the write lock held.
1095 if (!ND6_TRY_UPGRADE()) {
1096 genid
= V_nd6_list_genid
;
1099 if (genid
!= V_nd6_list_genid
)
1106 * The preferred router may have changed, so relocate this
1109 TAILQ_REMOVE(&V_nd6_defrouter
, dr
, dr_entry
);
1112 n
= malloc(sizeof(*n
), M_IP6NDP
, M_NOWAIT
| M_ZERO
);
1117 memcpy(n
, new, sizeof(*n
));
1118 /* Initialize with an extra reference for the caller. */
1119 refcount_init(&n
->refcnt
, 2);
1123 * Insert the new router in the Default Router List;
1124 * The Default Router List should be in the descending order
1125 * of router-preferece. Routers with the same preference are
1126 * sorted in the arriving time order.
1129 /* insert at the end of the group */
1130 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
) {
1131 if (rtpref(n
) > rtpref(dr
))
1135 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1137 TAILQ_INSERT_TAIL(&V_nd6_defrouter
, n
, dr_entry
);
1141 defrouter_select_fib(new->ifp
->if_fib
);
1147 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
1149 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
1150 ndpr
->ndpr_preferred
= 0;
1152 ndpr
->ndpr_preferred
= time_uptime
+ ndpr
->ndpr_pltime
;
1153 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
1154 ndpr
->ndpr_expire
= 0;
1156 ndpr
->ndpr_expire
= time_uptime
+ ndpr
->ndpr_vltime
;
1162 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
1164 /* init ia6t_expire */
1165 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
)
1166 lt6
->ia6t_expire
= 0;
1168 lt6
->ia6t_expire
= time_uptime
;
1169 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
1172 /* init ia6t_preferred */
1173 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
)
1174 lt6
->ia6t_preferred
= 0;
1176 lt6
->ia6t_preferred
= time_uptime
;
1177 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
1181 static struct in6_ifaddr
*
1182 in6_ifadd(struct nd_prefixctl
*pr
, int mcast
)
1184 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1186 struct in6_aliasreq ifra
;
1187 struct in6_ifaddr
*ia
, *ib
;
1189 struct in6_addr mask
;
1190 int prefixlen
= pr
->ndpr_plen
;
1192 char ip6buf
[INET6_ADDRSTRLEN
];
1194 in6_prefixlen2mask(&mask
, prefixlen
);
1197 * find a link-local address (will be interface ID).
1198 * Is it really mandatory? Theoretically, a global or a site-local
1199 * address can be configured without a link-local address, if we
1200 * have a unique interface identifier...
1202 * it is not mandatory to have a link-local address, we can generate
1203 * interface identifier on the fly. we do this because:
1204 * (1) it should be the easiest way to find interface identifier.
1205 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1206 * for multiple addresses on a single interface, and possible shortcut
1207 * of DAD. we omitted DAD for this reason in the past.
1208 * (3) a user can prevent autoconfiguration of global address
1209 * by removing link-local address by hand (this is partly because we
1210 * don't have other way to control the use of IPv6 on an interface.
1211 * this has been our design choice - cf. NRL's "ifconfig auto").
1212 * (4) it is easier to manage when an interface has addresses
1213 * with the same interface identifier, than to have multiple addresses
1214 * with different interface identifiers.
1216 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0); /* 0 is OK? */
1218 ib
= (struct in6_ifaddr
*)ifa
;
1222 /* prefixlen + ifidlen must be equal to 128 */
1223 plen0
= in6_mask2len(&ib
->ia_prefixmask
.sin6_addr
, NULL
);
1224 if (prefixlen
!= plen0
) {
1227 "%s: wrong prefixlen for %s (prefix=%d ifid=%d)\n",
1228 __func__
, if_name(ifp
), prefixlen
, 128 - plen0
));
1233 in6_prepare_ifra(&ifra
, &pr
->ndpr_prefix
.sin6_addr
, &mask
);
1235 IN6_MASK_ADDR(&ifra
.ifra_addr
.sin6_addr
, &mask
);
1237 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
1238 (ib
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
1239 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
1240 (ib
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
1241 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
1242 (ib
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
1243 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
1244 (ib
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
1248 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
1249 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
1251 /* XXX: scope zone ID? */
1253 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
1256 * Make sure that we do not have this address already. This should
1257 * usually not happen, but we can still see this case, e.g., if we
1258 * have manually configured the exact address to be configured.
1260 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
,
1261 &ifra
.ifra_addr
.sin6_addr
);
1264 /* this should be rare enough to make an explicit log */
1265 log(LOG_INFO
, "in6_ifadd: %s is already configured\n",
1266 ip6_sprintf(ip6buf
, &ifra
.ifra_addr
.sin6_addr
));
1271 * Allocate ifaddr structure, link into chain, etc.
1272 * If we are going to create a new address upon receiving a multicasted
1273 * RA, we need to impose a random delay before starting DAD.
1274 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1278 updateflags
|= IN6_IFAUPDATE_DADDELAY
;
1279 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
, updateflags
)) != 0) {
1281 "%s: failed to make ifaddr %s on %s (errno=%d)\n", __func__
,
1282 ip6_sprintf(ip6buf
, &ifra
.ifra_addr
.sin6_addr
),
1283 if_name(ifp
), error
));
1284 return (NULL
); /* ifaddr must not have been allocated. */
1287 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
);
1289 * XXXRW: Assumption of non-NULLness here might not be true with
1290 * fine-grained locking -- should we validate it? Or just return
1291 * earlier ifa rather than looking it up again?
1293 return (ia
); /* this is always non-NULL and referenced. */
1296 static struct nd_prefix
*
1297 nd6_prefix_lookup_locked(struct nd_prefixctl
*key
)
1299 struct nd_prefix
*search
;
1303 LIST_FOREACH(search
, &V_nd_prefix
, ndpr_entry
) {
1304 if (key
->ndpr_ifp
== search
->ndpr_ifp
&&
1305 key
->ndpr_plen
== search
->ndpr_plen
&&
1306 in6_are_prefix_equal(&key
->ndpr_prefix
.sin6_addr
,
1307 &search
->ndpr_prefix
.sin6_addr
, key
->ndpr_plen
)) {
1308 nd6_prefix_ref(search
);
1316 nd6_prefix_lookup(struct nd_prefixctl
*key
)
1318 struct nd_prefix
*search
;
1321 search
= nd6_prefix_lookup_locked(key
);
1327 nd6_prefix_ref(struct nd_prefix
*pr
)
1330 refcount_acquire(&pr
->ndpr_refcnt
);
1334 nd6_prefix_rele(struct nd_prefix
*pr
)
1337 if (refcount_release(&pr
->ndpr_refcnt
)) {
1338 KASSERT(LIST_EMPTY(&pr
->ndpr_advrtrs
),
1339 ("prefix %p has advertising routers", pr
));
1345 nd6_prelist_add(struct nd_prefixctl
*pr
, struct nd_defrouter
*dr
,
1346 struct nd_prefix
**newp
)
1348 struct nd_prefix
*new;
1349 char ip6buf
[INET6_ADDRSTRLEN
];
1352 new = malloc(sizeof(*new), M_IP6NDP
, M_NOWAIT
| M_ZERO
);
1355 refcount_init(&new->ndpr_refcnt
, newp
!= NULL
? 2 : 1);
1356 new->ndpr_ifp
= pr
->ndpr_ifp
;
1357 new->ndpr_prefix
= pr
->ndpr_prefix
;
1358 new->ndpr_plen
= pr
->ndpr_plen
;
1359 new->ndpr_vltime
= pr
->ndpr_vltime
;
1360 new->ndpr_pltime
= pr
->ndpr_pltime
;
1361 new->ndpr_flags
= pr
->ndpr_flags
;
1362 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
1363 free(new, M_IP6NDP
);
1366 new->ndpr_lastupdate
= time_uptime
;
1368 /* initialization */
1369 LIST_INIT(&new->ndpr_advrtrs
);
1370 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
1371 /* make prefix in the canonical form */
1372 IN6_MASK_ADDR(&new->ndpr_prefix
.sin6_addr
, &new->ndpr_mask
);
1375 LIST_INSERT_HEAD(&V_nd_prefix
, new, ndpr_entry
);
1379 /* ND_OPT_PI_FLAG_ONLINK processing */
1380 if (new->ndpr_raf_onlink
) {
1381 struct epoch_tracker et
;
1384 NET_EPOCH_ENTER(et
);
1385 if ((error
= nd6_prefix_onlink(new)) != 0) {
1386 nd6log((LOG_ERR
, "%s: failed to make the prefix %s/%d "
1387 "on-link on %s (errno=%d)\n", __func__
,
1388 ip6_sprintf(ip6buf
, &pr
->ndpr_prefix
.sin6_addr
),
1389 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), error
));
1390 /* proceed anyway. XXX: is it correct? */
1393 ND6_ONLINK_UNLOCK();
1397 pfxrtr_add(new, dr
);
1404 * Remove a prefix from the prefix list and optionally stash it in a
1405 * caller-provided list.
1407 * The ND6 lock must be held.
1410 nd6_prefix_unlink(struct nd_prefix
*pr
, struct nd_prhead
*list
)
1415 LIST_REMOVE(pr
, ndpr_entry
);
1418 LIST_INSERT_HEAD(list
, pr
, ndpr_entry
);
1422 * Free an unlinked prefix, first marking it off-link if necessary.
1425 nd6_prefix_del(struct nd_prefix
*pr
)
1427 struct nd_pfxrouter
*pfr
, *next
;
1429 char ip6buf
[INET6_ADDRSTRLEN
];
1431 KASSERT(pr
->ndpr_addrcnt
== 0,
1432 ("prefix %p has referencing addresses", pr
));
1433 ND6_UNLOCK_ASSERT();
1436 * Though these flags are now meaningless, we'd rather keep the value
1437 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
1438 * when executing "ndp -p".
1440 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
1442 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
1444 "%s: failed to make the prefix %s/%d offlink on %s "
1445 "(errno=%d)\n", __func__
,
1446 ip6_sprintf(ip6buf
, &pr
->ndpr_prefix
.sin6_addr
),
1447 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
1448 /* what should we do? */
1450 ND6_ONLINK_UNLOCK();
1453 /* Release references to routers that have advertised this prefix. */
1455 LIST_FOREACH_SAFE(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
, next
)
1459 nd6_prefix_rele(pr
);
1461 pfxlist_onlink_check();
1465 prelist_update(struct nd_prefixctl
*new, struct nd_defrouter
*dr
,
1466 struct mbuf
*m
, int mcast
)
1468 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
1470 struct ifnet
*ifp
= new->ndpr_ifp
;
1471 struct nd_prefix
*pr
;
1474 struct in6_addrlifetime lt6_tmp
;
1475 char ip6buf
[INET6_ADDRSTRLEN
];
1482 * Authenticity for NA consists authentication for
1483 * both IP header and IP datagrams, doesn't it ?
1485 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1486 auth
= ((m
->m_flags
& M_AUTHIPHDR
) &&
1487 (m
->m_flags
& M_AUTHIPDGM
));
1491 if ((pr
= nd6_prefix_lookup(new)) != NULL
) {
1493 * nd6_prefix_lookup() ensures that pr and new have the same
1494 * prefix on a same interface.
1498 * Update prefix information. Note that the on-link (L) bit
1499 * and the autonomous (A) bit should NOT be changed from 1
1502 if (new->ndpr_raf_onlink
== 1)
1503 pr
->ndpr_raf_onlink
= 1;
1504 if (new->ndpr_raf_auto
== 1)
1505 pr
->ndpr_raf_auto
= 1;
1506 if (new->ndpr_raf_onlink
) {
1507 pr
->ndpr_vltime
= new->ndpr_vltime
;
1508 pr
->ndpr_pltime
= new->ndpr_pltime
;
1509 (void)in6_init_prefix_ltimes(pr
); /* XXX error case? */
1510 pr
->ndpr_lastupdate
= time_uptime
;
1513 if (new->ndpr_raf_onlink
&&
1514 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
1516 if ((error
= nd6_prefix_onlink(pr
)) != 0) {
1518 "%s: failed to make the prefix %s/%d "
1519 "on-link on %s (errno=%d)\n", __func__
,
1521 &pr
->ndpr_prefix
.sin6_addr
),
1522 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
1524 /* proceed anyway. XXX: is it correct? */
1526 ND6_ONLINK_UNLOCK();
1532 if (new->ndpr_vltime
== 0)
1534 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
1537 error
= nd6_prelist_add(new, dr
, &pr
);
1539 nd6log((LOG_NOTICE
, "%s: nd6_prelist_add() failed for "
1540 "the prefix %s/%d on %s (errno=%d)\n", __func__
,
1541 ip6_sprintf(ip6buf
, &new->ndpr_prefix
.sin6_addr
),
1542 new->ndpr_plen
, if_name(new->ndpr_ifp
), error
));
1543 goto end
; /* we should just give up in this case. */
1547 * XXX: from the ND point of view, we can ignore a prefix
1548 * with the on-link bit being zero. However, we need a
1549 * prefix structure for references from autoconfigured
1550 * addresses. Thus, we explicitly make sure that the prefix
1551 * itself expires now.
1553 if (pr
->ndpr_raf_onlink
== 0) {
1554 pr
->ndpr_vltime
= 0;
1555 pr
->ndpr_pltime
= 0;
1556 in6_init_prefix_ltimes(pr
);
1561 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1562 * Note that pr must be non NULL at this point.
1565 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1566 if (!new->ndpr_raf_auto
)
1570 * 5.5.3 (b). the link-local prefix should have been ignored in
1574 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1575 if (new->ndpr_pltime
> new->ndpr_vltime
) {
1576 error
= EINVAL
; /* XXX: won't be used */
1581 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1582 * an address configured by stateless autoconfiguration already in the
1583 * list of addresses associated with the interface, and the Valid
1584 * Lifetime is not 0, form an address. We first check if we have
1585 * a matching prefix.
1586 * Note: we apply a clarification in rfc2462bis-02 here. We only
1587 * consider autoconfigured addresses while RFC2462 simply said
1590 CK_STAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1591 struct in6_ifaddr
*ifa6
;
1592 u_int32_t remaininglifetime
;
1594 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1597 ifa6
= (struct in6_ifaddr
*)ifa
;
1600 * We only consider autoconfigured addresses as per rfc2462bis.
1602 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
))
1606 * Spec is not clear here, but I believe we should concentrate
1607 * on unicast (i.e. not anycast) addresses.
1608 * XXX: other ia6_flags? detached or duplicated?
1610 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0)
1614 * Ignore the address if it is not associated with a prefix
1615 * or is associated with a prefix that is different from this
1616 * one. (pr is never NULL here)
1618 if (ifa6
->ia6_ndpr
!= pr
)
1621 if (ia6_match
== NULL
) /* remember the first one */
1625 * An already autoconfigured address matched. Now that we
1626 * are sure there is at least one matched address, we can
1627 * proceed to 5.5.3. (e): update the lifetimes according to the
1628 * "two hours" rule and the privacy extension.
1629 * We apply some clarifications in rfc2462bis:
1630 * - use remaininglifetime instead of storedlifetime as a
1632 * - remove the dead code in the "two-hour" rule
1634 #define TWOHOUR (120*60)
1635 lt6_tmp
= ifa6
->ia6_lifetime
;
1637 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
1638 remaininglifetime
= ND6_INFINITE_LIFETIME
;
1639 else if (time_uptime
- ifa6
->ia6_updatetime
>
1640 lt6_tmp
.ia6t_vltime
) {
1642 * The case of "invalid" address. We should usually
1643 * not see this case.
1645 remaininglifetime
= 0;
1647 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
1648 (time_uptime
- ifa6
->ia6_updatetime
);
1650 /* when not updating, keep the current stored lifetime. */
1651 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
1653 if (TWOHOUR
< new->ndpr_vltime
||
1654 remaininglifetime
< new->ndpr_vltime
) {
1655 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
1656 } else if (remaininglifetime
<= TWOHOUR
) {
1658 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
1662 * new->ndpr_vltime <= TWOHOUR &&
1663 * TWOHOUR < remaininglifetime
1665 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
1668 /* The 2 hour rule is not imposed for preferred lifetime. */
1669 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
1671 in6_init_address_ltimes(pr
, <6_tmp
);
1674 * We need to treat lifetimes for temporary addresses
1675 * differently, according to
1676 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1677 * we only update the lifetimes when they are in the maximum
1680 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1681 u_int32_t maxvltime
, maxpltime
;
1683 if (V_ip6_temp_valid_lifetime
>
1684 (u_int32_t
)((time_uptime
- ifa6
->ia6_createtime
) +
1685 V_ip6_desync_factor
)) {
1686 maxvltime
= V_ip6_temp_valid_lifetime
-
1687 (time_uptime
- ifa6
->ia6_createtime
) -
1688 V_ip6_desync_factor
;
1691 if (V_ip6_temp_preferred_lifetime
>
1692 (u_int32_t
)((time_uptime
- ifa6
->ia6_createtime
) +
1693 V_ip6_desync_factor
)) {
1694 maxpltime
= V_ip6_temp_preferred_lifetime
-
1695 (time_uptime
- ifa6
->ia6_createtime
) -
1696 V_ip6_desync_factor
;
1700 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
1701 lt6_tmp
.ia6t_vltime
> maxvltime
) {
1702 lt6_tmp
.ia6t_vltime
= maxvltime
;
1704 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
1705 lt6_tmp
.ia6t_pltime
> maxpltime
) {
1706 lt6_tmp
.ia6t_pltime
= maxpltime
;
1709 ifa6
->ia6_lifetime
= lt6_tmp
;
1710 ifa6
->ia6_updatetime
= time_uptime
;
1712 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
1716 * 5.5.3 (d) (continued)
1717 * No address matched and the valid lifetime is non-zero.
1718 * Create a new address.
1722 * Prefix Length check:
1723 * If the sum of the prefix length and interface identifier
1724 * length does not equal 128 bits, the Prefix Information
1725 * option MUST be ignored. The length of the interface
1726 * identifier is defined in a separate link-type specific
1729 ifidlen
= in6_if2idlen(ifp
);
1731 /* this should not happen, so we always log it. */
1732 log(LOG_ERR
, "prelist_update: IFID undefined (%s)\n",
1736 if (ifidlen
+ pr
->ndpr_plen
!= 128) {
1738 "%s: invalid prefixlen %d for %s, ignored\n",
1739 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
1743 if ((ia6
= in6_ifadd(new, mcast
)) != NULL
) {
1745 * note that we should use pr (not new) for reference.
1752 * When a new public address is created as described
1753 * in RFC2462, also create a new temporary address.
1756 * When an interface connects to a new link, a new
1757 * randomized interface identifier should be generated
1758 * immediately together with a new set of temporary
1759 * addresses. Thus, we specifiy 1 as the 2nd arg of
1762 if (V_ip6_use_tempaddr
) {
1764 if ((e
= in6_tmpifadd(ia6
, 1, 1)) != 0) {
1765 nd6log((LOG_NOTICE
, "%s: failed to "
1766 "create a temporary address "
1767 "(errno=%d)\n", __func__
, e
));
1770 ifa_free(&ia6
->ia_ifa
);
1773 * A newly added address might affect the status
1774 * of other addresses, so we check and update it.
1775 * XXX: what if address duplication happens?
1777 pfxlist_onlink_check();
1779 /* just set an error. do not bark here. */
1780 error
= EADDRNOTAVAIL
; /* XXX: might be unused. */
1786 nd6_prefix_rele(pr
);
1791 * A supplement function used in the on-link detection below;
1792 * detect if a given prefix has a (probably) reachable advertising router.
1793 * XXX: lengthy function name...
1795 static struct nd_pfxrouter
*
1796 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
1798 struct epoch_tracker et
;
1799 struct nd_pfxrouter
*pfxrtr
;
1803 NET_EPOCH_ENTER(et
);
1804 LIST_FOREACH(pfxrtr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
1805 if (is_dr_reachable(pfxrtr
->router
))
1813 * Check if each prefix in the prefix list has at least one available router
1814 * that advertised the prefix (a router is "available" if its neighbor cache
1815 * entry is reachable or probably reachable).
1816 * If the check fails, the prefix may be off-link, because, for example,
1817 * we have moved from the network but the lifetime of the prefix has not
1818 * expired yet. So we should not use the prefix if there is another prefix
1819 * that has an available router.
1820 * But, if there is no prefix that has an available router, we still regard
1821 * all the prefixes as on-link. This is because we can't tell if all the
1822 * routers are simply dead or if we really moved from the network and there
1823 * is no router around us.
1826 pfxlist_onlink_check(void)
1828 struct nd_prefix
*pr
;
1829 struct in6_ifaddr
*ifa
;
1830 struct nd_defrouter
*dr
;
1831 struct nd_pfxrouter
*pfxrtr
= NULL
;
1832 struct rm_priotracker in6_ifa_tracker
;
1840 * Check if there is a prefix that has a reachable advertising
1843 LIST_FOREACH(pr
, &V_nd_prefix
, ndpr_entry
) {
1844 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
))
1849 * If we have no such prefix, check whether we still have a router
1850 * that does not advertise any prefixes.
1853 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
) {
1854 struct nd_prefix
*pr0
;
1856 LIST_FOREACH(pr0
, &V_nd_prefix
, ndpr_entry
) {
1857 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
)
1864 if (pr
!= NULL
|| (!TAILQ_EMPTY(&V_nd6_defrouter
) && pfxrtr
== NULL
)) {
1866 * There is at least one prefix that has a reachable router,
1867 * or at least a router which probably does not advertise
1868 * any prefixes. The latter would be the case when we move
1869 * to a new link where we have a router that does not provide
1870 * prefixes and we configure an address by hand.
1871 * Detach prefixes which have no reachable advertising
1872 * router, and attach other prefixes.
1874 LIST_FOREACH(pr
, &V_nd_prefix
, ndpr_entry
) {
1875 /* XXX: a link-local prefix should never be detached */
1876 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
) ||
1877 pr
->ndpr_raf_onlink
== 0 ||
1878 pr
->ndpr_raf_auto
== 0)
1881 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
1882 find_pfxlist_reachable_router(pr
) == NULL
)
1883 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
1884 else if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
1885 find_pfxlist_reachable_router(pr
) != NULL
)
1886 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
1889 /* there is no prefix that has a reachable router */
1890 LIST_FOREACH(pr
, &V_nd_prefix
, ndpr_entry
) {
1891 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
) ||
1892 pr
->ndpr_raf_onlink
== 0 ||
1893 pr
->ndpr_raf_auto
== 0)
1895 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
1900 * Remove each interface route associated with a (just) detached
1901 * prefix, and reinstall the interface route for a (just) attached
1902 * prefix. Note that all attempt of reinstallation does not
1903 * necessarily success, when a same prefix is shared among multiple
1904 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1905 * so we don't have to care about them.
1908 LIST_FOREACH(pr
, &V_nd_prefix
, ndpr_entry
) {
1909 char ip6buf
[INET6_ADDRSTRLEN
];
1912 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
) ||
1913 pr
->ndpr_raf_onlink
== 0 ||
1914 pr
->ndpr_raf_auto
== 0)
1917 flags
= pr
->ndpr_stateflags
& (NDPRF_DETACHED
| NDPRF_ONLINK
);
1918 if (flags
== 0 || flags
== (NDPRF_DETACHED
| NDPRF_ONLINK
)) {
1919 genid
= V_nd6_list_genid
;
1921 if ((flags
& NDPRF_ONLINK
) != 0 &&
1922 (e
= nd6_prefix_offlink(pr
)) != 0) {
1924 "%s: failed to make %s/%d offlink "
1925 "(errno=%d)\n", __func__
,
1927 &pr
->ndpr_prefix
.sin6_addr
),
1929 } else if ((flags
& NDPRF_ONLINK
) == 0 &&
1930 (e
= nd6_prefix_onlink(pr
)) != 0) {
1932 "%s: failed to make %s/%d onlink "
1933 "(errno=%d)\n", __func__
,
1935 &pr
->ndpr_prefix
.sin6_addr
),
1939 if (genid
!= V_nd6_list_genid
)
1945 * Changes on the prefix status might affect address status as well.
1946 * Make sure that all addresses derived from an attached prefix are
1947 * attached, and that all addresses derived from a detached prefix are
1948 * detached. Note, however, that a manually configured address should
1949 * always be attached.
1950 * The precise detection logic is same as the one for prefixes.
1952 IN6_IFADDR_RLOCK(&in6_ifa_tracker
);
1953 CK_STAILQ_FOREACH(ifa
, &V_in6_ifaddrhead
, ia_link
) {
1954 if (!(ifa
->ia6_flags
& IN6_IFF_AUTOCONF
))
1957 if (ifa
->ia6_ndpr
== NULL
) {
1959 * This can happen when we first configure the address
1960 * (i.e. the address exists, but the prefix does not).
1961 * XXX: complicated relationships...
1966 if (find_pfxlist_reachable_router(ifa
->ia6_ndpr
))
1970 CK_STAILQ_FOREACH(ifa
, &V_in6_ifaddrhead
, ia_link
) {
1971 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1974 if (ifa
->ia6_ndpr
== NULL
) /* XXX: see above. */
1977 if (find_pfxlist_reachable_router(ifa
->ia6_ndpr
)) {
1978 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
1979 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
1980 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1981 nd6_dad_start((struct ifaddr
*)ifa
, 0);
1984 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
1988 CK_STAILQ_FOREACH(ifa
, &V_in6_ifaddrhead
, ia_link
) {
1989 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0)
1992 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
1993 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
1994 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1995 /* Do we need a delay in this case? */
1996 nd6_dad_start((struct ifaddr
*)ifa
, 0);
2000 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker
);
2002 ND6_ONLINK_UNLOCK();
2006 * Add or remove interface route specified by @dst, @netmask and @ifp.
2008 * Returns 0 on success
2011 nd6_prefix_rtrequest(uint32_t fibnum
, int cmd
, struct sockaddr_in6
*dst
,
2012 struct sockaddr_in6
*netmask
, struct ifnet
*ifp
, struct ifaddr
*ifa
)
2014 struct epoch_tracker et
;
2017 /* Prepare gateway */
2018 struct sockaddr_dl_short sdl
= {
2019 .sdl_family
= AF_LINK
,
2020 .sdl_len
= sizeof(struct sockaddr_dl_short
),
2021 .sdl_type
= ifp
->if_type
,
2022 .sdl_index
= ifp
->if_index
,
2025 struct rt_addrinfo info
= {
2028 .rti_flags
= RTF_PINNED
| ((netmask
!= NULL
) ? 0 : RTF_HOST
),
2030 [RTAX_DST
] = (struct sockaddr
*)dst
,
2031 [RTAX_NETMASK
] = (struct sockaddr
*)netmask
,
2032 [RTAX_GATEWAY
] = (struct sockaddr
*)&sdl
,
2035 /* Don't set additional per-gw filters on removal */
2037 NET_EPOCH_ENTER(et
);
2038 error
= rib_handle_ifaddr_info(fibnum
, cmd
, &info
);
2044 nd6_prefix_onlink_rtrequest(struct nd_prefix
*pr
, struct ifaddr
*ifa
)
2048 struct sockaddr_in6 mask6
= {
2049 .sin6_family
= AF_INET6
,
2050 .sin6_len
= sizeof(struct sockaddr_in6
),
2051 .sin6_addr
= pr
->ndpr_mask
,
2053 struct sockaddr_in6
*pmask6
= (pr
->ndpr_plen
!= 128) ? &mask6
: NULL
;
2055 error
= nd6_prefix_rtrequest(pr
->ndpr_ifp
->if_fib
, RTM_ADD
,
2056 &pr
->ndpr_prefix
, pmask6
, pr
->ndpr_ifp
, ifa
);
2058 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
2064 nd6_prefix_onlink(struct nd_prefix
*pr
)
2066 struct epoch_tracker et
;
2068 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2069 struct nd_prefix
*opr
;
2070 char ip6buf
[INET6_ADDRSTRLEN
];
2073 ND6_ONLINK_LOCK_ASSERT();
2074 ND6_UNLOCK_ASSERT();
2076 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0)
2080 * Add the interface route associated with the prefix. Before
2081 * installing the route, check if there's the same prefix on another
2082 * interface, and the prefix has already installed the interface route.
2083 * Although such a configuration is expected to be rare, we explicitly
2087 LIST_FOREACH(opr
, &V_nd_prefix
, ndpr_entry
) {
2091 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0)
2094 if (!V_rt_add_addr_allfibs
&&
2095 opr
->ndpr_ifp
->if_fib
!= pr
->ndpr_ifp
->if_fib
)
2098 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
2099 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
2100 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
2108 * We prefer link-local addresses as the associated interface address.
2110 /* search for a link-local addr */
2111 NET_EPOCH_ENTER(et
);
2112 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
2113 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
2115 /* XXX: freebsd does not have ifa_ifwithaf */
2116 CK_STAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
2117 if (ifa
->ifa_addr
->sa_family
== AF_INET6
) {
2122 /* should we care about ia6_flags? */
2126 * This can still happen, when, for example, we receive an RA
2127 * containing a prefix with the L bit set and the A bit clear,
2128 * after removing all IPv6 addresses on the receiving
2129 * interface. This should, of course, be rare though.
2132 "%s: failed to find any ifaddr to add route for a "
2133 "prefix(%s/%d) on %s\n", __func__
,
2134 ip6_sprintf(ip6buf
, &pr
->ndpr_prefix
.sin6_addr
),
2135 pr
->ndpr_plen
, if_name(ifp
)));
2138 error
= nd6_prefix_onlink_rtrequest(pr
, ifa
);
2147 nd6_prefix_offlink(struct nd_prefix
*pr
)
2150 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2151 struct nd_prefix
*opr
;
2152 char ip6buf
[INET6_ADDRSTRLEN
];
2156 ND6_ONLINK_LOCK_ASSERT();
2157 ND6_UNLOCK_ASSERT();
2159 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0)
2162 struct sockaddr_in6 mask6
= {
2163 .sin6_family
= AF_INET6
,
2164 .sin6_len
= sizeof(struct sockaddr_in6
),
2165 .sin6_addr
= pr
->ndpr_mask
,
2167 struct sockaddr_in6
*pmask6
= (pr
->ndpr_plen
!= 128) ? &mask6
: NULL
;
2169 error
= nd6_prefix_rtrequest(ifp
->if_fib
, RTM_DELETE
,
2170 &pr
->ndpr_prefix
, pmask6
, ifp
, NULL
);
2174 pr
->ndpr_stateflags
&= ~NDPRF_ONLINK
;
2177 * There might be the same prefix on another interface,
2178 * the prefix which could not be on-link just because we have
2179 * the interface route (see comments in nd6_prefix_onlink).
2180 * If there's one, try to make the prefix on-link on the
2185 LIST_FOREACH(opr
, &V_nd_prefix
, ndpr_entry
) {
2187 * KAME specific: detached prefixes should not be
2190 if (opr
== pr
|| (opr
->ndpr_stateflags
&
2191 (NDPRF_ONLINK
| NDPRF_DETACHED
)) != 0)
2194 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
2195 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
2196 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
2199 genid
= V_nd6_list_genid
;
2201 if ((e
= nd6_prefix_onlink(opr
)) != 0) {
2203 "%s: failed to recover a prefix "
2204 "%s/%d from %s to %s (errno=%d)\n",
2205 __func__
, ip6_sprintf(ip6buf
,
2206 &opr
->ndpr_prefix
.sin6_addr
),
2207 opr
->ndpr_plen
, if_name(ifp
),
2208 if_name(opr
->ndpr_ifp
), e
));
2212 if (genid
!= V_nd6_list_genid
)
2218 /* XXX: can we still set the NDPRF_ONLINK flag? */
2220 "%s: failed to delete route: %s/%d on %s (errno=%d)\n",
2221 __func__
, ip6_sprintf(ip6buf
, &pr
->ndpr_prefix
.sin6_addr
),
2222 pr
->ndpr_plen
, if_name(ifp
), error
));
2226 lltable_prefix_free(AF_INET6
,
2227 (struct sockaddr
*)&pr
->ndpr_prefix
,
2228 (struct sockaddr
*)&mask6
, LLE_STATIC
);
2234 * ia0 - corresponding public address
2237 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
, int delay
)
2239 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
2240 struct in6_ifaddr
*newia
;
2241 struct in6_aliasreq ifra
;
2243 int trylimit
= 3; /* XXX: adhoc value */
2245 u_int32_t randid
[2];
2246 time_t vltime0
, pltime0
;
2248 in6_prepare_ifra(&ifra
, &ia0
->ia_addr
.sin6_addr
,
2249 &ia0
->ia_prefixmask
.sin6_addr
);
2251 ifra
.ifra_addr
= ia0
->ia_addr
; /* XXX: do we need this ? */
2252 /* clear the old IFID */
2253 IN6_MASK_ADDR(&ifra
.ifra_addr
.sin6_addr
,
2254 &ifra
.ifra_prefixmask
.sin6_addr
);
2257 if (in6_get_tmpifid(ifp
, (u_int8_t
*)randid
,
2258 (const u_int8_t
*)&ia0
->ia_addr
.sin6_addr
.s6_addr
[8], forcegen
)) {
2259 nd6log((LOG_NOTICE
, "%s: failed to find a good random IFID\n",
2263 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
2264 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
2265 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
2266 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
2269 * in6_get_tmpifid() quite likely provided a unique interface ID.
2270 * However, we may still have a chance to see collision, because
2271 * there may be a time lag between generation of the ID and generation
2272 * of the address. So, we'll do one more sanity check.
2275 if (in6_localip(&ifra
.ifra_addr
.sin6_addr
) != 0) {
2276 if (trylimit
-- > 0) {
2281 /* Give up. Something strange should have happened. */
2282 nd6log((LOG_NOTICE
, "%s: failed to find a unique random IFID\n",
2288 * The Valid Lifetime is the lower of the Valid Lifetime of the
2289 * public address or TEMP_VALID_LIFETIME.
2290 * The Preferred Lifetime is the lower of the Preferred Lifetime
2291 * of the public address or TEMP_PREFERRED_LIFETIME -
2294 if (ia0
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
2295 vltime0
= IFA6_IS_INVALID(ia0
) ? 0 :
2296 (ia0
->ia6_lifetime
.ia6t_vltime
-
2297 (time_uptime
- ia0
->ia6_updatetime
));
2298 if (vltime0
> V_ip6_temp_valid_lifetime
)
2299 vltime0
= V_ip6_temp_valid_lifetime
;
2301 vltime0
= V_ip6_temp_valid_lifetime
;
2302 if (ia0
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
2303 pltime0
= IFA6_IS_DEPRECATED(ia0
) ? 0 :
2304 (ia0
->ia6_lifetime
.ia6t_pltime
-
2305 (time_uptime
- ia0
->ia6_updatetime
));
2306 if (pltime0
> V_ip6_temp_preferred_lifetime
- V_ip6_desync_factor
){
2307 pltime0
= V_ip6_temp_preferred_lifetime
-
2308 V_ip6_desync_factor
;
2311 pltime0
= V_ip6_temp_preferred_lifetime
- V_ip6_desync_factor
;
2312 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
2313 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
2316 * A temporary address is created only if this calculated Preferred
2317 * Lifetime is greater than REGEN_ADVANCE time units.
2319 if (ifra
.ifra_lifetime
.ia6t_pltime
<= V_ip6_temp_regen_advance
)
2322 /* XXX: scope zone ID? */
2324 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
2326 /* allocate ifaddr structure, link into chain, etc. */
2329 updateflags
|= IN6_IFAUPDATE_DADDELAY
;
2330 if ((error
= in6_update_ifa(ifp
, &ifra
, NULL
, updateflags
)) != 0)
2333 newia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
);
2334 if (newia
== NULL
) { /* XXX: can it happen? */
2336 "%s: ifa update succeeded, but we got no ifaddr\n",
2338 return (EINVAL
); /* XXX */
2340 newia
->ia6_ndpr
= ia0
->ia6_ndpr
;
2341 newia
->ia6_ndpr
->ndpr_addrcnt
++;
2342 ifa_free(&newia
->ia_ifa
);
2345 * A newly added address might affect the status of other addresses.
2346 * XXX: when the temporary address is generated with a new public
2347 * address, the onlink check is redundant. However, it would be safe
2348 * to do the check explicitly everywhere a new address is generated,
2349 * and, in fact, we surely need the check when we create a new
2350 * temporary address due to deprecation of an old temporary address.
2352 pfxlist_onlink_check();
2358 rt6_deleteroute(const struct rtentry
*rt
, const struct nhop_object
*nh
,
2361 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
2364 if (nh
->gw_sa
.sa_family
!= AF_INET6
)
2367 if (!IN6_ARE_ADDR_EQUAL(gate
, &nh
->gw6_sa
.sin6_addr
)) {
2372 * Do not delete a static route.
2373 * XXX: this seems to be a bit ad-hoc. Should we consider the
2374 * 'cloned' bit instead?
2376 nh_rt_flags
= nhop_get_rtflags(nh
);
2377 if ((nh_rt_flags
& RTF_STATIC
) != 0)
2381 * We delete only host route. This means, in particular, we don't
2382 * delete default route.
2384 if ((nh_rt_flags
& RTF_HOST
) == 0)
2392 * Delete all the routing table entries that use the specified gateway.
2393 * XXX: this function causes search through all entries of routing table, so
2394 * it shouldn't be called when acting as a router.
2397 rt6_flush(struct in6_addr
*gateway
, struct ifnet
*ifp
)
2400 /* We'll care only link-local addresses */
2401 if (!IN6_IS_ADDR_LINKLOCAL(gateway
))
2404 /* XXX Do we really need to walk any but the default FIB? */
2405 rib_foreach_table_walk_del(AF_INET6
, rt6_deleteroute
, (void *)gateway
);
2409 nd6_setdefaultiface(int ifindex
)
2412 if (V_nd6_defifindex
!= ifindex
) {
2413 V_nd6_defifindex
= ifindex
;
2414 if (V_nd6_defifindex
!= 0) {
2415 struct epoch_tracker et
;
2418 * XXXGL: this function should use ifnet_byindex_ref!
2420 NET_EPOCH_ENTER(et
);
2421 V_nd6_defifp
= ifnet_byindex(V_nd6_defifindex
);
2423 if (V_nd6_defifp
== NULL
)
2426 V_nd6_defifp
= NULL
;
2429 * Our current implementation assumes one-to-one mapping between
2430 * interfaces and links, so it would be natural to use the
2431 * default interface as the default link.
2433 scope6_setdefault(V_nd6_defifp
);
2440 nd6_defrouter_list_empty(void)
2443 return (TAILQ_EMPTY(&V_nd6_defrouter
));
2447 nd6_defrouter_timer(void)
2449 struct nd_defrouter
*dr
, *ndr
;
2450 struct nd6_drhead drq
;
2455 TAILQ_FOREACH_SAFE(dr
, &V_nd6_defrouter
, dr_entry
, ndr
)
2456 if (dr
->expire
&& dr
->expire
< time_uptime
)
2457 defrouter_unlink(dr
, &drq
);
2460 while ((dr
= TAILQ_FIRST(&drq
)) != NULL
) {
2461 TAILQ_REMOVE(&drq
, dr
, dr_entry
);
2467 * Nuke default router list entries toward ifp.
2468 * We defer removal of default router list entries that is installed in the
2469 * routing table, in order to keep additional side effects as small as possible.
2472 nd6_defrouter_purge(struct ifnet
*ifp
)
2474 struct nd_defrouter
*dr
, *ndr
;
2475 struct nd6_drhead drq
;
2480 TAILQ_FOREACH_SAFE(dr
, &V_nd6_defrouter
, dr_entry
, ndr
) {
2484 defrouter_unlink(dr
, &drq
);
2486 TAILQ_FOREACH_SAFE(dr
, &V_nd6_defrouter
, dr_entry
, ndr
) {
2490 defrouter_unlink(dr
, &drq
);
2494 /* Delete the unlinked router objects. */
2495 while ((dr
= TAILQ_FIRST(&drq
)) != NULL
) {
2496 TAILQ_REMOVE(&drq
, dr
, dr_entry
);
2502 nd6_defrouter_flush_all(void)
2504 struct nd_defrouter
*dr
;
2505 struct nd6_drhead drq
;
2510 while ((dr
= TAILQ_FIRST(&V_nd6_defrouter
)) != NULL
)
2511 defrouter_unlink(dr
, &drq
);
2514 while ((dr
= TAILQ_FIRST(&drq
)) != NULL
) {
2515 TAILQ_REMOVE(&drq
, dr
, dr_entry
);
2521 nd6_defrouter_init(void)
2524 TAILQ_INIT(&V_nd6_defrouter
);
2528 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS
)
2530 struct in6_defrouter d
;
2531 struct nd_defrouter
*dr
;
2534 if (req
->newptr
!= NULL
)
2537 error
= sysctl_wire_old_buffer(req
, 0);
2541 bzero(&d
, sizeof(d
));
2542 d
.rtaddr
.sin6_family
= AF_INET6
;
2543 d
.rtaddr
.sin6_len
= sizeof(d
.rtaddr
);
2546 TAILQ_FOREACH(dr
, &V_nd6_defrouter
, dr_entry
) {
2547 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
2548 error
= sa6_recoverscope(&d
.rtaddr
);
2551 d
.flags
= dr
->raflags
;
2552 d
.rtlifetime
= dr
->rtlifetime
;
2553 d
.expire
= dr
->expire
+ (time_second
- time_uptime
);
2554 d
.if_index
= dr
->ifp
->if_index
;
2555 error
= SYSCTL_OUT(req
, &d
, sizeof(d
));
2562 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
2563 CTLTYPE_OPAQUE
| CTLFLAG_RD
| CTLFLAG_MPSAFE
,
2564 NULL
, 0, nd6_sysctl_drlist
, "S,in6_defrouter",
2565 "NDP default router list");