2 * Copyright (c) 2003-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/syslog.h>
69 #include <sys/queue.h>
70 #include <sys/mcache.h>
71 #include <sys/protosw.h>
73 #include <dev/random/randomdev.h>
75 #include <kern/locks.h>
76 #include <kern/zalloc.h>
77 #include <machine/machine_routines.h>
80 #include <net/if_var.h>
81 #include <net/if_types.h>
82 #include <net/if_dl.h>
83 #include <net/route.h>
84 #include <net/radix.h>
86 #include <netinet/in.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet6/in6_ifattach.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/nd6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/scope6_var.h>
95 #include <net/net_osdep.h>
97 static void defrouter_addreq(struct nd_defrouter
*, boolean_t
);
98 static void defrouter_delreq(struct nd_defrouter
*);
99 static struct nd_defrouter
*defrtrlist_update_common(struct nd_defrouter
*,
101 static struct nd_defrouter
*defrtrlist_update(struct nd_defrouter
*);
103 static struct in6_ifaddr
*in6_pfx_newpersistaddr(struct nd_prefix
*, int,
106 static struct nd_pfxrouter
*pfxrtr_lookup(struct nd_prefix
*,
107 struct nd_defrouter
*);
108 static void pfxrtr_add(struct nd_prefix
*, struct nd_defrouter
*);
109 static void pfxrtr_del(struct nd_pfxrouter
*, struct nd_prefix
*);
110 static struct nd_pfxrouter
*find_pfxlist_reachable_router(struct nd_prefix
*);
111 static void nd6_rtmsg(int, struct rtentry
*);
113 static int nd6_prefix_onlink_common(struct nd_prefix
*, boolean_t
,
115 static struct nd_prefix
*nd6_prefix_equal_lookup(struct nd_prefix
*, boolean_t
);
116 static void nd6_prefix_sync(struct ifnet
*);
118 static void in6_init_address_ltimes(struct nd_prefix
*,
119 struct in6_addrlifetime
*);
121 static int rt6_deleteroute(struct radix_node
*, void *);
123 static struct nd_defrouter
*nddr_alloc(int);
124 static void nddr_free(struct nd_defrouter
*);
125 static void nddr_trace(struct nd_defrouter
*, int);
127 static struct nd_prefix
*ndpr_alloc(int);
128 static void ndpr_free(struct nd_prefix
*);
129 static void ndpr_trace(struct nd_prefix
*, int);
131 extern int nd6_recalc_reachtm_interval
;
133 static struct ifnet
*nd6_defifp
= NULL
;
134 int nd6_defifindex
= 0;
135 static unsigned int nd6_defrouter_genid
;
137 int ip6_use_tempaddr
= 1; /* use temp addr by default for testing now */
139 int nd6_accept_6to4
= 1;
141 int ip6_desync_factor
;
142 u_int32_t ip6_temp_preferred_lifetime
= DEF_TEMP_PREFERRED_LIFETIME
;
143 u_int32_t ip6_temp_valid_lifetime
= DEF_TEMP_VALID_LIFETIME
;
145 * shorter lifetimes for debugging purposes.
146 * u_int32_t ip6_temp_preferred_lifetime = 800;
147 * static u_int32_t ip6_temp_valid_lifetime = 1800;
149 int ip6_temp_regen_advance
= TEMPADDR_REGEN_ADVANCE
;
151 extern lck_mtx_t
*nd6_mutex
;
153 /* Serialization variables for single thread access to nd_prefix */
154 static boolean_t nd_prefix_busy
;
155 static void *nd_prefix_waitchan
= &nd_prefix_busy
;
156 static int nd_prefix_waiters
= 0;
158 /* Serialization variables for single thread access to nd_defrouter */
159 static boolean_t nd_defrouter_busy
;
160 static void *nd_defrouter_waitchan
= &nd_defrouter_busy
;
161 static int nd_defrouter_waiters
= 0;
163 /* RTPREF_MEDIUM has to be 0! */
164 #define RTPREF_HIGH 1
165 #define RTPREF_MEDIUM 0
166 #define RTPREF_LOW (-1)
167 #define RTPREF_RESERVED (-2)
168 #define RTPREF_INVALID (-3) /* internal */
170 #define NDPR_TRACE_HIST_SIZE 32 /* size of trace history */
173 __private_extern__
unsigned int ndpr_trace_hist_size
= NDPR_TRACE_HIST_SIZE
;
175 struct nd_prefix_dbg
{
176 struct nd_prefix ndpr_pr
; /* nd_prefix */
177 u_int16_t ndpr_refhold_cnt
; /* # of ref */
178 u_int16_t ndpr_refrele_cnt
; /* # of rele */
180 * Circular lists of ndpr_addref and ndpr_remref callers.
182 ctrace_t ndpr_refhold
[NDPR_TRACE_HIST_SIZE
];
183 ctrace_t ndpr_refrele
[NDPR_TRACE_HIST_SIZE
];
186 static unsigned int ndpr_debug
; /* debug flags */
187 static unsigned int ndpr_size
; /* size of zone element */
188 static struct zone
*ndpr_zone
; /* zone for nd_prefix */
190 #define NDPR_ZONE_MAX 64 /* maximum elements in zone */
191 #define NDPR_ZONE_NAME "nd6_prefix" /* zone name */
193 #define NDDR_TRACE_HIST_SIZE 32 /* size of trace history */
196 __private_extern__
unsigned int nddr_trace_hist_size
= NDDR_TRACE_HIST_SIZE
;
198 struct nd_defrouter_dbg
{
199 struct nd_defrouter nddr_dr
; /* nd_defrouter */
200 uint16_t nddr_refhold_cnt
; /* # of ref */
201 uint16_t nddr_refrele_cnt
; /* # of rele */
203 * Circular lists of ndpr_addref and ndpr_remref callers.
205 ctrace_t nddr_refhold
[NDDR_TRACE_HIST_SIZE
];
206 ctrace_t nddr_refrele
[NDDR_TRACE_HIST_SIZE
];
209 static unsigned int nddr_debug
; /* debug flags */
210 static unsigned int nddr_size
; /* size of zone element */
211 static struct zone
*nddr_zone
; /* zone for nd_defrouter */
213 #define NDDR_ZONE_MAX 64 /* maximum elements in zone */
214 #define NDDR_ZONE_NAME "nd6_defrouter" /* zone name */
216 static unsigned int ndprtr_size
; /* size of zone element */
217 static struct zone
*ndprtr_zone
; /* zone for nd_pfxrouter */
219 #define NDPRTR_ZONE_MAX 64 /* maximum elements in zone */
220 #define NDPRTR_ZONE_NAME "nd6_pfxrouter" /* zone name */
225 PE_parse_boot_argn("ifa_debug", &ndpr_debug
, sizeof (ndpr_debug
));
226 PE_parse_boot_argn("ifa_debug", &nddr_debug
, sizeof (nddr_debug
));
228 ndpr_size
= (ndpr_debug
== 0) ? sizeof (struct nd_prefix
) :
229 sizeof (struct nd_prefix_dbg
);
230 ndpr_zone
= zinit(ndpr_size
, NDPR_ZONE_MAX
* ndpr_size
, 0,
232 if (ndpr_zone
== NULL
) {
233 panic("%s: failed allocating %s", __func__
, NDPR_ZONE_NAME
);
236 zone_change(ndpr_zone
, Z_EXPAND
, TRUE
);
237 zone_change(ndpr_zone
, Z_CALLERACCT
, FALSE
);
239 nddr_size
= (nddr_debug
== 0) ? sizeof (struct nd_defrouter
) :
240 sizeof (struct nd_defrouter_dbg
);
241 nddr_zone
= zinit(nddr_size
, NDDR_ZONE_MAX
* nddr_size
, 0,
243 if (nddr_zone
== NULL
) {
244 panic("%s: failed allocating %s", __func__
, NDDR_ZONE_NAME
);
247 zone_change(nddr_zone
, Z_EXPAND
, TRUE
);
248 zone_change(nddr_zone
, Z_CALLERACCT
, FALSE
);
250 ndprtr_size
= sizeof (struct nd_pfxrouter
);
251 ndprtr_zone
= zinit(ndprtr_size
, NDPRTR_ZONE_MAX
* ndprtr_size
, 0,
253 if (ndprtr_zone
== NULL
) {
254 panic("%s: failed allocating %s", __func__
, NDPRTR_ZONE_NAME
);
257 zone_change(ndprtr_zone
, Z_EXPAND
, TRUE
);
258 zone_change(ndprtr_zone
, Z_CALLERACCT
, FALSE
);
262 * Receive Router Solicitation Message - just for routers.
263 * Router solicitation/advertisement is mostly managed by userland program
264 * (rtadvd) so here we have no function like nd6_ra_output().
274 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
275 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
276 struct nd_router_solicit
*nd_rs
;
277 struct in6_addr saddr6
= ip6
->ip6_src
;
280 union nd_opts ndopts
;
282 /* Expect 32-bit aligned data pointer on strict-align platforms */
283 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
285 /* If I'm not a router, ignore it. */
286 if (!ip6_forwarding
|| !(ifp
->if_eflags
& IFEF_IPV6_ROUTER
))
290 if (ip6
->ip6_hlim
!= 255) {
292 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
293 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
294 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
299 * Don't update the neighbor cache, if src = :: or a non-neighbor.
300 * The former case indicates that the src has no IP address assigned
301 * yet. See nd6_ns_input() for the latter case.
303 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
306 struct sockaddr_in6 src_sa6
;
308 bzero(&src_sa6
, sizeof (src_sa6
));
309 src_sa6
.sin6_family
= AF_INET6
;
310 src_sa6
.sin6_len
= sizeof (src_sa6
);
311 src_sa6
.sin6_addr
= ip6
->ip6_src
;
312 if (!nd6_is_addr_neighbor(&src_sa6
, ifp
, 0)) {
313 nd6log((LOG_INFO
, "nd6_rs_input: "
314 "RS packet from non-neighbor\n"));
319 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
320 nd_rs
= (struct nd_router_solicit
*)((caddr_t
)ip6
+ off
);
321 icmp6len
-= sizeof (*nd_rs
);
322 nd6_option_init(nd_rs
+ 1, icmp6len
, &ndopts
);
323 if (nd6_options(&ndopts
) < 0) {
325 "nd6_rs_input: invalid ND option, ignored\n"));
326 /* nd6_options have incremented stats */
330 if (ndopts
.nd_opts_src_lladdr
) {
331 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
332 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
335 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
337 "nd6_rs_input: lladdrlen mismatch for %s "
338 "(if %d, RS packet %d)\n",
339 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
343 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_SOLICIT
, 0);
350 icmp6stat
.icp6s_badrs
++;
355 * Receive Router Advertisement Message.
358 * TODO: on-link bit on prefix information
359 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
367 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
368 struct nd_ifinfo
*ndi
= NULL
;
369 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
370 struct nd_router_advert
*nd_ra
;
371 struct in6_addr saddr6
= ip6
->ip6_src
;
373 union nd_opts ndopts
;
374 struct nd_defrouter
*dr
= NULL
;
377 u_int32_t lladdrlen
= 0;
378 struct nd_prefix_list
*nd_prefix_list_head
= NULL
;
379 u_int32_t nd_prefix_list_length
= 0;
380 struct in6_ifaddr
*ia6
= NULL
;
381 struct nd_prefix_list
*prfl
;
382 struct nd_defrouter dr0
;
383 u_int32_t advreachable
;
386 /* Expect 32-bit aligned data pointer on strict-align platforms */
387 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
390 * Discard RA unless IFEF_ACCEPT_RTADV is set (as host), or when
391 * IFEF_IPV6_ROUTER is set (as router) but the RA is not locally
392 * generated. For convenience, we allow locally generated (rtadvd)
393 * RAs to be processed on the advertising interface, as a router.
395 * Note that we don't test against ip6_forwarding as we could be
396 * both a host and a router on different interfaces, hence the
397 * check against the per-interface flags.
399 if (!(ifp
->if_eflags
& (IFEF_ACCEPT_RTADV
| IFEF_IPV6_ROUTER
)) ||
400 ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) &&
401 (ia6
= ifa_foraddr6(&saddr6
)) == NULL
))
405 IFA_REMREF(&ia6
->ia_ifa
);
409 if (ip6
->ip6_hlim
!= 255) {
411 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
412 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
413 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
417 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) {
419 "nd6_ra_input: src %s is not link-local\n",
420 ip6_sprintf(&saddr6
)));
424 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
425 nd_ra
= (struct nd_router_advert
*)((caddr_t
)ip6
+ off
);
427 icmp6len
-= sizeof (*nd_ra
);
428 nd6_option_init(nd_ra
+ 1, icmp6len
, &ndopts
);
429 if (nd6_options(&ndopts
) < 0) {
431 "nd6_ra_input: invalid ND option, ignored\n"));
432 /* nd6_options have incremented stats */
436 advreachable
= nd_ra
->nd_ra_reachable
;
438 /* remember if this is a multicasted advertisement */
439 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
442 ndi
= ND_IFINFO(ifp
);
443 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
444 lck_mtx_lock(&ndi
->lock
);
445 bzero(&dr0
, sizeof (dr0
));
447 dr0
.flags
= nd_ra
->nd_ra_flags_reserved
;
448 dr0
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
449 dr0
.expire
= net_uptime() + dr0
.rtlifetime
;
451 /* unspecified or not? (RFC 2461 6.3.4) */
453 advreachable
= ntohl(advreachable
);
454 if (advreachable
<= MAX_REACHABLE_TIME
&&
455 ndi
->basereachable
!= advreachable
) {
456 ndi
->basereachable
= advreachable
;
457 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
458 ndi
->recalctm
= nd6_recalc_reachtm_interval
; /* reset */
461 if (nd_ra
->nd_ra_retransmit
)
462 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
463 if (nd_ra
->nd_ra_curhoplimit
) {
464 if (ndi
->chlim
< nd_ra
->nd_ra_curhoplimit
) {
465 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
466 } else if (ndi
->chlim
!= nd_ra
->nd_ra_curhoplimit
) {
468 "RA with a lower CurHopLimit sent from "
469 "%s on %s (current = %d, received = %d). "
470 "Ignored.\n", ip6_sprintf(&ip6
->ip6_src
),
471 if_name(ifp
), ndi
->chlim
,
472 nd_ra
->nd_ra_curhoplimit
));
475 lck_mtx_unlock(&ndi
->lock
);
476 lck_mtx_lock(nd6_mutex
);
477 dr
= defrtrlist_update(&dr0
);
478 lck_mtx_unlock(nd6_mutex
);
483 if (ndopts
.nd_opts_pi
) {
484 struct nd_opt_hdr
*pt
;
485 struct nd_opt_prefix_info
*pi
= NULL
;
488 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
489 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
490 pt
= (struct nd_opt_hdr
*)((caddr_t
)pt
+
491 (pt
->nd_opt_len
<< 3))) {
492 struct in6_addr pi_mask
;
493 bzero(&pi_mask
, sizeof(pi_mask
));
495 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
)
497 pi
= (struct nd_opt_prefix_info
*)pt
;
499 if (pi
->nd_opt_pi_len
!= 4) {
501 "nd6_ra_input: invalid option "
502 "len %d for prefix information option, "
503 "ignored\n", pi
->nd_opt_pi_len
));
507 if (128 < pi
->nd_opt_pi_prefix_len
) {
509 "nd6_ra_input: invalid prefix "
510 "len %d for prefix information option, "
511 "ignored\n", pi
->nd_opt_pi_prefix_len
));
516 * To ignore ::/64 make sure bits beyond prefixlen
519 in6_prefixlen2mask(&pi_mask
, pi
->nd_opt_pi_prefix_len
);
520 pi
->nd_opt_pi_prefix
.s6_addr32
[0] &= pi_mask
.s6_addr32
[0];
521 pi
->nd_opt_pi_prefix
.s6_addr32
[1] &= pi_mask
.s6_addr32
[1];
522 pi
->nd_opt_pi_prefix
.s6_addr32
[2] &= pi_mask
.s6_addr32
[2];
523 pi
->nd_opt_pi_prefix
.s6_addr32
[3] &= pi_mask
.s6_addr32
[3];
525 if (IN6_IS_ADDR_UNSPECIFIED(&pi
->nd_opt_pi_prefix
) ||
526 IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
) ||
527 IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
529 "%s: invalid prefix %s, ignored\n",
531 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
535 bzero(&pr
, sizeof (pr
));
536 lck_mtx_init(&pr
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
538 pr
.ndpr_prefix
.sin6_family
= AF_INET6
;
539 pr
.ndpr_prefix
.sin6_len
= sizeof (pr
.ndpr_prefix
);
540 pr
.ndpr_prefix
.sin6_addr
= pi
->nd_opt_pi_prefix
;
541 pr
.ndpr_ifp
= m
->m_pkthdr
.rcvif
;
543 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
544 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
545 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
546 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
547 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
548 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
550 ntohl(pi
->nd_opt_pi_preferred_time
);
553 * Exceptions to stateless autoconfiguration processing:
554 * + nd6_accept_6to4 == 0 && address has 6to4 prefix
555 * + ip6_only_allow_rfc4193_prefix != 0 &&
556 * address not RFC 4193
558 if (ip6_only_allow_rfc4193_prefix
&&
559 !IN6_IS_ADDR_UNIQUE_LOCAL(&pi
->nd_opt_pi_prefix
)) {
561 "nd6_ra_input: no SLAAC on prefix %s "
563 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
564 pr
.ndpr_raf_auto
= 0;
565 } else if (!nd6_accept_6to4
&&
566 IN6_IS_ADDR_6TO4(&pi
->nd_opt_pi_prefix
)) {
568 "%s: no SLAAC on prefix %s "
569 "[6to4]\n", __func__
,
570 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
571 pr
.ndpr_raf_auto
= 0;
574 if (in6_init_prefix_ltimes(&pr
)) {
576 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
577 continue; /* prefix lifetime init failed */
581 (void) prelist_update(&pr
, dr
, m
, mcast
);
582 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
585 * We have to copy the values out after the
586 * prelist_update call since some of these values won't
587 * be properly set until after the router advertisement
588 * updating can vet the values.
591 MALLOC(prfl
, struct nd_prefix_list
*, sizeof (*prfl
),
592 M_TEMP
, M_WAITOK
| M_ZERO
);
595 log(LOG_DEBUG
, "%s: unable to MALLOC RA prefix "
596 "structure\n", __func__
);
600 /* this is only for nd6_post_msg(), otherwise unused */
601 bcopy(&pr
.ndpr_prefix
, &prfl
->pr
.ndpr_prefix
,
602 sizeof (prfl
->pr
.ndpr_prefix
));
603 prfl
->pr
.ndpr_raf
= pr
.ndpr_raf
;
604 prfl
->pr
.ndpr_plen
= pr
.ndpr_plen
;
605 prfl
->pr
.ndpr_vltime
= pr
.ndpr_vltime
;
606 prfl
->pr
.ndpr_pltime
= pr
.ndpr_pltime
;
607 prfl
->pr
.ndpr_expire
= pr
.ndpr_expire
;
608 prfl
->pr
.ndpr_base_calendartime
=
609 pr
.ndpr_base_calendartime
;
610 prfl
->pr
.ndpr_base_uptime
= pr
.ndpr_base_uptime
;
611 prfl
->pr
.ndpr_stateflags
= pr
.ndpr_stateflags
;
612 prfl
->pr
.ndpr_addrcnt
= pr
.ndpr_addrcnt
;
613 prfl
->pr
.ndpr_ifp
= pr
.ndpr_ifp
;
615 prfl
->next
= nd_prefix_list_head
;
616 nd_prefix_list_head
= prfl
;
617 nd_prefix_list_length
++;
624 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
625 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
628 if (mtu
< IPV6_MMTU
) {
629 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu option "
630 "mtu=%d sent from %s, ignoring\n",
631 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
635 lck_mtx_lock(&ndi
->lock
);
638 if (mtu
<= ndi
->maxmtu
) {
639 int change
= (ndi
->linkmtu
!= mtu
);
642 lck_mtx_unlock(&ndi
->lock
);
643 if (change
) /* in6_maxmtu may change */
646 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu "
647 "mtu=%d sent from %s; "
648 "exceeds maxmtu %d, ignoring\n",
649 mtu
, ip6_sprintf(&ip6
->ip6_src
),
651 lck_mtx_unlock(&ndi
->lock
);
654 lck_mtx_unlock(&ndi
->lock
);
655 nd6log((LOG_INFO
, "nd6_ra_input: mtu option "
656 "mtu=%d sent from %s; maxmtu unknown, "
658 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
665 * Source link layer address
667 if (ndopts
.nd_opts_src_lladdr
) {
668 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
669 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
672 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
674 "nd6_ra_input: lladdrlen mismatch for %s "
675 "(if %d, RA packet %d)\n",
676 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
680 if (dr
&& dr
->stateflags
& NDDRF_MAPPED
)
681 saddr6
= dr
->rtaddr_mapped
;
683 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, (int)lladdrlen
,
684 ND_ROUTER_ADVERT
, 0);
687 nd6_post_msg(KEV_ND6_RA
, nd_prefix_list_head
, nd_prefix_list_length
,
688 mtu
, lladdr
, lladdrlen
);
691 * Installing a link-layer address might change the state of the
692 * router's neighbor cache, which might also affect our on-link
693 * detection of adveritsed prefixes.
695 lck_mtx_lock(nd6_mutex
);
696 pfxlist_onlink_check();
697 lck_mtx_unlock(nd6_mutex
);
705 while ((prfl
= nd_prefix_list_head
) != NULL
) {
706 nd_prefix_list_head
= prfl
->next
;
713 icmp6stat
.icp6s_badra
++;
718 * default router list proccessing sub routines
721 /* tell the change to user processes watching the routing socket. */
723 nd6_rtmsg(int cmd
, struct rtentry
*rt
)
725 struct rt_addrinfo info
;
726 struct ifnet
*ifp
= rt
->rt_ifp
;
728 RT_LOCK_ASSERT_HELD(rt
);
730 bzero((caddr_t
)&info
, sizeof (info
));
731 /* It's not necessary to lock ifp for if_lladdr */
732 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
733 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
734 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
736 * ifa_addr pointers for both should always be valid
737 * in this context; no need to hold locks.
739 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
740 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
742 rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0);
746 defrouter_addreq(struct nd_defrouter
*new, boolean_t scoped
)
748 struct sockaddr_in6 def
, mask
, gate
;
749 struct rtentry
*newrt
= NULL
;
750 unsigned int ifscope
;
752 struct nd_ifinfo
*ndi
= ND_IFINFO(new->ifp
);
754 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
755 NDDR_LOCK_ASSERT_NOTHELD(new);
757 * We're free to lock and unlock NDDR because our callers
758 * are holding an extra reference for us.
762 if (new->stateflags
& NDDRF_INSTALLED
)
765 if (new->ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
766 nd6log2((LOG_INFO
, "%s: ignoring router %s, scoped=%d, "
767 "static=%d on advertising interface\n", if_name(new->ifp
),
768 ip6_sprintf(&new->rtaddr
), scoped
,
769 (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
773 nd6log2((LOG_INFO
, "%s: adding default router %s, scoped=%d, "
774 "static=%d\n", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
),
775 scoped
, (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
777 Bzero(&def
, sizeof (def
));
778 Bzero(&mask
, sizeof (mask
));
779 Bzero(&gate
, sizeof (gate
));
781 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
782 = sizeof (struct sockaddr_in6
);
783 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
785 if (new->stateflags
& NDDRF_MAPPED
)
786 gate
.sin6_addr
= new->rtaddr_mapped
;
788 gate
.sin6_addr
= new->rtaddr
;
790 ifscope
= scoped
? new->ifp
->if_index
: IFSCOPE_NONE
;
794 * Cellular networks may have buggy deployments
795 * with gateway IPv6 link local address with same
796 * interface identifier as the one that has been
797 * assigned for the cellular context.
798 * If gateway is same as locally configured link local
799 * interface on cellular interface, generated a different one
800 * and store it in the nd_defrouter entry and use it to work
803 if (new->ifp
->if_type
== IFT_CELLULAR
&&
804 !(new->stateflags
& NDDRF_STATIC
) &&
805 !(new->stateflags
& NDDRF_MAPPED
) &&
806 IN6_IS_ADDR_LINKLOCAL(&gate
.sin6_addr
) &&
807 ndi
&& !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
808 struct in6_ifaddr
*tmp_ia6
= in6ifa_ifpforlinklocal(new->ifp
, 0);
810 if (tmp_ia6
!= NULL
&&
811 !(tmp_ia6
->ia6_flags
& IN6_IFF_NOTMANUAL
) &&
812 IN6_ARE_ADDR_EQUAL(&tmp_ia6
->ia_addr
.sin6_addr
,
814 gate
.sin6_addr
.s6_addr8
[15] += 1;
815 new->rtaddr_mapped
= gate
.sin6_addr
;
816 new->stateflags
|= NDDRF_MAPPED
;
818 nd6log((LOG_INFO
, "%s: Default router %s mapped "
819 "to ", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
)));
820 nd6log((LOG_INFO
, "%s\n", ip6_sprintf(&new->rtaddr_mapped
)));
824 err
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&def
,
825 (struct sockaddr
*)&gate
, (struct sockaddr
*)&mask
,
826 RTF_GATEWAY
, &newrt
, ifscope
);
830 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */
831 RT_REMREF_LOCKED(newrt
);
834 new->stateflags
|= NDDRF_INSTALLED
;
835 if (ifscope
!= IFSCOPE_NONE
)
836 new->stateflags
|= NDDRF_IFSCOPE
;
838 nd6log((LOG_ERR
, "%s: failed to add default router "
839 "%s on %s scoped %d (errno = %d)\n", __func__
,
840 ip6_sprintf(&gate
.sin6_addr
), if_name(new->ifp
),
841 (ifscope
!= IFSCOPE_NONE
), err
));
850 struct nd_defrouter
*
852 struct in6_addr
*addr
,
855 struct nd_defrouter
*dr
;
857 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
859 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
860 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
862 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) {
863 NDDR_ADDREF_LOCKED(dr
);
870 return (NULL
); /* search failed */
874 * Remove the default route for a given router.
875 * This is just a subroutine function for defrouter_select(), and should
876 * not be called from anywhere else.
879 defrouter_delreq(struct nd_defrouter
*dr
)
881 struct sockaddr_in6 def
, mask
, gate
;
882 struct rtentry
*oldrt
= NULL
;
883 unsigned int ifscope
;
886 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
887 NDDR_LOCK_ASSERT_NOTHELD(dr
);
889 * We're free to lock and unlock NDDR because our callers
890 * are holding an extra reference for us.
893 /* ifp would be NULL for the "drany" case */
894 if (dr
->ifp
!= NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
))
897 nd6log2((LOG_INFO
, "%s: removing default router %s, scoped=%d, "
898 "static=%d\n", dr
->ifp
!= NULL
? if_name(dr
->ifp
) : "ANY",
899 ip6_sprintf(&dr
->rtaddr
), (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
900 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
902 Bzero(&def
, sizeof (def
));
903 Bzero(&mask
, sizeof (mask
));
904 Bzero(&gate
, sizeof (gate
));
906 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
907 = sizeof (struct sockaddr_in6
);
908 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
911 * The router entry may be mapped to a different address.
912 * If that is the case, use the mapped address as gateway
913 * to do operation on the routing table.
914 * To get more context, read the related comment in
917 if (dr
->stateflags
& NDDRF_MAPPED
)
918 gate
.sin6_addr
= dr
->rtaddr_mapped
;
920 gate
.sin6_addr
= dr
->rtaddr
;
922 if (dr
->ifp
!= NULL
) {
923 ifscope
= (dr
->stateflags
& NDDRF_IFSCOPE
) ?
924 dr
->ifp
->if_index
: IFSCOPE_NONE
;
926 ifscope
= IFSCOPE_NONE
;
930 err
= rtrequest_scoped(RTM_DELETE
,
931 (struct sockaddr
*)&def
, (struct sockaddr
*)&gate
,
932 (struct sockaddr
*)&mask
, RTF_GATEWAY
, &oldrt
, ifscope
);
936 nd6_rtmsg(RTM_DELETE
, oldrt
);
939 } else if (err
!= ESRCH
) {
940 nd6log((LOG_ERR
, "%s: failed to delete default router "
941 "%s on %s scoped %d (errno = %d)\n", __func__
,
942 ip6_sprintf(&gate
.sin6_addr
), dr
->ifp
!= NULL
?
943 if_name(dr
->ifp
) : "ANY", (ifscope
!= IFSCOPE_NONE
), err
));
946 /* ESRCH means it's no longer in the routing table; ignore it */
947 if (oldrt
!= NULL
|| err
== ESRCH
) {
948 dr
->stateflags
&= ~NDDRF_INSTALLED
;
949 if (ifscope
!= IFSCOPE_NONE
)
950 dr
->stateflags
&= ~NDDRF_IFSCOPE
;
959 * remove all default routes from default router list
962 defrouter_reset(void)
964 struct nd_defrouter
*dr
, drany
;
966 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
968 dr
= TAILQ_FIRST(&nd_defrouter
);
971 if (dr
->stateflags
& NDDRF_INSTALLED
) {
972 NDDR_ADDREF_LOCKED(dr
);
974 lck_mtx_unlock(nd6_mutex
);
975 defrouter_delreq(dr
);
976 lck_mtx_lock(nd6_mutex
);
978 dr
= TAILQ_FIRST(&nd_defrouter
);
981 dr
= TAILQ_NEXT(dr
, dr_entry
);
985 /* Nuke primary (non-scoped) default router */
986 bzero(&drany
, sizeof (drany
));
987 lck_mtx_init(&drany
.nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
988 lck_mtx_unlock(nd6_mutex
);
989 defrouter_delreq(&drany
);
990 lck_mtx_destroy(&drany
.nddr_lock
, ifa_mtx_grp
);
991 lck_mtx_lock(nd6_mutex
);
995 defrtrlist_ioctl(u_long cmd
, caddr_t data
)
997 struct nd_defrouter dr0
;
998 unsigned int ifindex
;
999 struct ifnet
*dr_ifp
;
1000 int error
= 0, add
= 0;
1002 /* XXX Handle mapped default router entries */
1004 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1005 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1008 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1009 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1010 bzero(&dr0
, sizeof (dr0
));
1011 if (cmd
== SIOCDRADD_IN6_64
|| cmd
== SIOCDRDEL_IN6_64
) {
1012 struct in6_defrouter_64
*r_64
=
1013 (struct in6_defrouter_64
*)(void *)data
;
1016 bcopy(&r_64
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1017 sizeof (dr0
.rtaddr
));
1018 dr0
.flags
= r_64
->flags
;
1019 bcopy(&r_64
->if_index
, &i
, sizeof (i
));
1022 struct in6_defrouter_32
*r_32
=
1023 (struct in6_defrouter_32
*)(void *)data
;
1026 bcopy(&r_32
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1027 sizeof (dr0
.rtaddr
));
1028 dr0
.flags
= r_32
->flags
;
1029 bcopy(&r_32
->if_index
, &i
, sizeof (i
));
1032 ifnet_head_lock_shared();
1033 /* Don't need to check is ifindex is < 0 since it's unsigned */
1034 if (if_index
< ifindex
||
1035 (dr_ifp
= ifindex2ifnet
[ifindex
]) == NULL
) {
1043 if (IN6_IS_SCOPE_EMBED(&dr0
.rtaddr
)) {
1044 uint16_t *scope
= &dr0
.rtaddr
.s6_addr16
[1];
1047 *scope
= htons(dr_ifp
->if_index
);
1048 } else if (*scope
!= htons(dr_ifp
->if_index
)) {
1055 error
= defrtrlist_add_static(&dr0
);
1056 if (!add
|| error
!= 0) {
1057 int err
= defrtrlist_del_static(&dr0
);
1064 error
= EOPNOTSUPP
; /* check for safety */
1072 * XXX Please make sure to remove dr from the
1073 * global default router tailq list before this
1075 * Also ensure that you release the list reference
1076 * only after calling this routine.
1079 defrtrlist_del(struct nd_defrouter
*dr
)
1081 #if (DEVELOPMENT || DEBUG)
1082 struct nd_defrouter
*dr_itr
= NULL
;
1084 struct nd_prefix
*pr
;
1085 struct ifnet
*ifp
= dr
->ifp
;
1086 struct nd_ifinfo
*ndi
= NULL
;
1089 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1091 #if (DEVELOPMENT || DEBUG)
1093 * Verify that the router is not in the global default
1095 * Can't use defrouter_lookup here because that just works
1096 * with address and ifp pointer.
1097 * We have to compare the memory here.
1098 * Also we can't use ASSERT here as that is not defined
1099 * for development builds.
1101 TAILQ_FOREACH(dr_itr
, &nd_defrouter
, dr_entry
)
1102 VERIFY(dr
!= dr_itr
);
1104 ++nd6_defrouter_genid
;
1106 * Flush all the routing table entries that use the router
1109 /* above is a good condition? */
1111 lck_mtx_unlock(nd6_mutex
);
1112 if (dr
->stateflags
& NDDRF_MAPPED
)
1113 rt6_flush(&dr
->rtaddr_mapped
, ifp
);
1115 rt6_flush(&dr
->rtaddr
, ifp
);
1117 lck_mtx_lock(nd6_mutex
);
1119 nd6log2((LOG_INFO
, "%s: freeing defrouter %s\n", if_name(dr
->ifp
),
1120 ip6_sprintf(&dr
->rtaddr
)));
1122 * Delete it from the routing table.
1125 lck_mtx_unlock(nd6_mutex
);
1126 defrouter_delreq(dr
);
1127 lck_mtx_lock(nd6_mutex
);
1131 * Also delete all the pointers to the router in each prefix lists.
1133 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1134 struct nd_pfxrouter
*pfxrtr
;
1137 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
1138 pfxrtr_del(pfxrtr
, pr
);
1142 pfxlist_onlink_check();
1145 ndi
= ND_IFINFO(ifp
);
1146 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1147 lck_mtx_lock(&ndi
->lock
);
1148 VERIFY(ndi
->ndefrouters
>= 0);
1149 if (ndi
->ndefrouters
> 0 && --ndi
->ndefrouters
== 0) {
1153 lck_mtx_unlock(&ndi
->lock
);
1156 * If the router is the primary one, choose a new one.
1157 * We always try to pick another eligible router
1158 * on this interface as we do scoped routing
1160 defrouter_select(ifp
);
1167 defrtrlist_add_static(struct nd_defrouter
*new)
1169 struct nd_defrouter
*dr
;
1172 new->rtlifetime
= -1;
1173 new->stateflags
|= NDDRF_STATIC
;
1175 /* we only want the preference level */
1176 new->flags
&= ND_RA_FLAG_RTPREF_MASK
;
1178 lck_mtx_lock(nd6_mutex
);
1179 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1180 if (dr
!= NULL
&& !(dr
->stateflags
& NDDRF_STATIC
)) {
1185 dr
= defrtrlist_update(new);
1193 lck_mtx_unlock(nd6_mutex
);
1199 defrtrlist_del_static(struct nd_defrouter
*new)
1201 struct nd_defrouter
*dr
;
1203 lck_mtx_lock(nd6_mutex
);
1204 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1205 if (dr
== NULL
|| !(dr
->stateflags
& NDDRF_STATIC
)) {
1210 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1212 NDDR_REMREF(dr
); /* remove list reference */
1215 lck_mtx_unlock(nd6_mutex
);
1217 return (dr
!= NULL
? 0 : EINVAL
);
1221 * for default router selection
1222 * regards router-preference field as a 2-bit signed integer
1225 rtpref(struct nd_defrouter
*dr
)
1227 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
1228 case ND_RA_FLAG_RTPREF_HIGH
:
1229 return (RTPREF_HIGH
);
1230 case ND_RA_FLAG_RTPREF_MEDIUM
:
1231 case ND_RA_FLAG_RTPREF_RSV
:
1232 return (RTPREF_MEDIUM
);
1233 case ND_RA_FLAG_RTPREF_LOW
:
1234 return (RTPREF_LOW
);
1237 * This case should never happen. If it did, it would mean a
1238 * serious bug of kernel internal. We thus always bark here.
1239 * Or, can we even panic?
1241 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
1242 return (RTPREF_INVALID
);
1248 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
1250 * 1) Routers that are reachable or probably reachable should be preferred.
1251 * If we have more than one (probably) reachable router, prefer ones
1252 * with the highest router preference.
1253 * 2) When no routers on the list are known to be reachable or
1254 * probably reachable, routers SHOULD be selected in a round-robin
1255 * fashion, regardless of router preference values.
1256 * 3) If the Default Router List is empty, assume that all
1257 * destinations are on-link.
1259 * When Scoped Routing is enabled, the selection logic is amended as follows:
1261 * a) When a default interface is specified, the primary/non-scoped default
1262 * router will be set to the reachable router on that link (if any) with
1263 * the highest router preference.
1264 * b) When there are more than one routers on the same link, the one with
1265 * the highest router preference will be installed, either as scoped or
1266 * non-scoped route entry. If they all share the same preference value,
1267 * the one installed will be the static or the first encountered reachable
1268 * router, i.e. static one wins over dynamic.
1269 * c) When no routers on the list are known to be reachable, or probably
1270 * reachable, no round-robin selection will take place when the default
1273 * We assume nd_defrouter is sorted by router preference value.
1274 * Since the code below covers both with and without router preference cases,
1275 * we do not need to classify the cases by ifdef.
1278 defrouter_select(struct ifnet
*ifp
)
1280 struct nd_defrouter
*dr
= NULL
;
1281 struct nd_defrouter
*selected_dr
= NULL
;
1282 struct nd_defrouter
*installed_dr
= NULL
;
1283 struct llinfo_nd6
*ln
= NULL
;
1284 struct rtentry
*rt
= NULL
;
1285 struct nd_ifinfo
*ndi
= NULL
;
1286 unsigned int genid
= 0;
1287 boolean_t is_installed_reachable
= FALSE
;
1289 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1293 "%s:%d: Return early. NULL interface",
1294 __func__
, __LINE__
));
1298 if (ifp
== lo_ifp
) {
1300 "%s:%d: Return early. "
1301 "Default router select called for loopback.\n",
1302 __func__
, __LINE__
));
1306 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1308 "%s:%d: Return early. "
1309 "Default router select called for interface"
1310 " %s with IFEF_IPV6_ROUTER flag set\n",
1311 __func__
, __LINE__
, if_name(ifp
)));
1316 * Let's handle easy case (3) first:
1317 * If default router list is empty, there's nothing to be done.
1319 if (!TAILQ_FIRST(&nd_defrouter
)) {
1321 "%s:%d: Return early. "
1322 "Default router is empty.\n", __func__
, __LINE__
));
1327 * Take an early exit if number of routers in nd_ifinfo is
1328 * 0 for the interface.
1330 ndi
= ND_IFINFO(ifp
);
1331 if (!ndi
|| !ndi
->initialized
) {
1333 "%s:%d: Return early. "
1334 "Interface %s's nd_ifinfo not initialized.\n",
1335 __func__
, __LINE__
, if_name(ifp
)));
1339 if (ndi
->ndefrouters
== 0) {
1341 "%s:%d: Return early. "
1342 "%s does not have any default routers.\n",
1343 __func__
, __LINE__
, if_name(ifp
)));
1348 * Due to the number of times we drop nd6_mutex, we need to
1349 * serialize this function.
1351 while (nd_defrouter_busy
) {
1352 nd_defrouter_waiters
++;
1353 msleep(nd_defrouter_waitchan
, nd6_mutex
, (PZERO
-1),
1355 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1357 nd_defrouter_busy
= TRUE
;
1360 * Search for a (probably) reachable router from the list.
1361 * We just pick up the first reachable one (if any), assuming that
1362 * the ordering rule of the list described in defrtrlist_update().
1364 * For all intents and purposes of Scoped Routing:
1365 * selected_dr = candidate for primary router
1366 * installed_dr = currently installed primary router
1368 genid
= nd6_defrouter_genid
;
1369 dr
= TAILQ_FIRST(&nd_defrouter
);
1371 while (dr
!= NULL
) {
1372 struct in6_addr rtaddr
;
1373 struct ifnet
*drifp
= NULL
;
1374 struct nd_defrouter
*drrele
= NULL
;
1380 dr
= TAILQ_NEXT(dr
, dr_entry
);
1385 * Optimize for the common case.
1386 * When the interface has only one default router
1387 * there's no point checking for reachability as
1388 * there's nothing else to choose from.
1390 if (ndi
->ndefrouters
== 1) {
1392 "%s:%d: Fast forward default router selection "
1393 "as interface %s has learned only one default "
1394 "router and there's nothing else to choose from.\n",
1395 __func__
, __LINE__
, if_name(ifp
)));
1396 VERIFY(selected_dr
== NULL
&& installed_dr
== NULL
);
1398 if (dr
->stateflags
& NDDRF_INSTALLED
)
1400 NDDR_ADDREF_LOCKED(selected_dr
);
1405 if (dr
->stateflags
& NDDRF_MAPPED
)
1406 rtaddr
= dr
->rtaddr_mapped
;
1408 rtaddr
= dr
->rtaddr
;
1410 NDDR_ADDREF_LOCKED(dr
); /* for this for loop */
1413 /* Callee returns a locked route upon success */
1414 if (selected_dr
== NULL
) {
1415 lck_mtx_unlock(nd6_mutex
);
1416 if ((rt
= nd6_lookup(&rtaddr
, 0, drifp
, 0)) != NULL
&&
1417 (ln
= rt
->rt_llinfo
) != NULL
&&
1418 ND6_IS_LLINFO_PROBREACH(ln
)) {
1419 RT_LOCK_ASSERT_HELD(rt
);
1421 NDDR_ADDREF(selected_dr
);
1423 lck_mtx_lock(nd6_mutex
);
1427 RT_REMREF_LOCKED(rt
);
1434 * When there are more than one routers on the same link, the one with
1435 * the highest router preference will be installed.
1436 * Since the list is in decreasing order of preference:
1437 * 1) If selected_dr is not NULL, only use dr if it is static and has
1438 * equal preference and selected_dr is not static.
1439 * 2) Else if selected_dr is NULL, and dr is static make selected_dr = dr
1442 if (((selected_dr
&& (rtpref(dr
) >= rtpref(selected_dr
)) &&
1443 !(selected_dr
->stateflags
& NDDRF_STATIC
)) ||
1444 (selected_dr
== NULL
)) &&
1445 (dr
->stateflags
& NDDRF_STATIC
)) {
1447 /* Release it later on */
1448 VERIFY(drrele
== NULL
);
1449 drrele
= selected_dr
;
1452 NDDR_ADDREF_LOCKED(selected_dr
);
1455 /* Record the currently installed router */
1456 if (dr
->stateflags
& NDDRF_INSTALLED
) {
1457 if (installed_dr
== NULL
) {
1459 NDDR_ADDREF_LOCKED(installed_dr
);
1460 if (dr
->stateflags
& NDDRF_MAPPED
)
1461 rtaddr
= installed_dr
->rtaddr_mapped
;
1463 rtaddr
= installed_dr
->rtaddr
;
1465 lck_mtx_unlock(nd6_mutex
);
1466 /* Callee returns a locked route upon success */
1467 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
1468 RT_LOCK_ASSERT_HELD(rt
);
1469 if ((ln
= rt
->rt_llinfo
) != NULL
&&
1470 ND6_IS_LLINFO_PROBREACH(ln
))
1471 is_installed_reachable
= TRUE
;
1473 RT_REMREF_LOCKED(rt
);
1477 lck_mtx_lock(nd6_mutex
);
1479 /* this should not happen; warn for diagnosis */
1480 nd6log((LOG_ERR
, "defrouter_select: more than one "
1481 "default router is installed for interface :%s.\n",
1488 NDDR_REMREF(dr
); /* for this for loop */
1490 NDDR_REMREF(drrele
);
1493 * Check if the list changed when we gave up
1494 * the nd6_mutex lock
1496 if(genid
!= nd6_defrouter_genid
) {
1498 NDDR_REMREF(selected_dr
);
1503 NDDR_REMREF(installed_dr
);
1504 installed_dr
= NULL
;
1507 if (ndi
->ndefrouters
== 0) {
1509 "%s:%d: Interface %s no longer "
1510 "has any default routers. Abort.\n",
1511 __func__
, __LINE__
, if_name(ifp
)));
1515 "%s:%d: Iterate default router list again "
1516 "for interface %s, as the list seems to have "
1517 "changed during release-reaquire of global "
1518 "nd6_mutex lock.\n",
1519 __func__
, __LINE__
, if_name(ifp
)));
1521 is_installed_reachable
= FALSE
;
1522 genid
= nd6_defrouter_genid
;
1523 dr
= TAILQ_FIRST(&nd_defrouter
);
1525 dr
= TAILQ_NEXT(dr
, dr_entry
);
1530 * If none of the default routers was found to be reachable,
1531 * round-robin the list regardless of preference.
1532 * Please note selected_dr equal to NULL implies that even
1533 * installed default router is not reachable
1535 if (selected_dr
== NULL
) {
1537 for (dr
= TAILQ_NEXT(installed_dr
, dr_entry
); dr
;
1538 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1539 if (installed_dr
->ifp
!= dr
->ifp
)
1547 * If none was installed or the installed one if the last
1548 * one on the list, select the first one from the list
1550 if ((installed_dr
== NULL
) || (selected_dr
== NULL
)) {
1551 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1552 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1553 if (dr
->ifp
== ifp
) {
1560 if ((selected_dr
== NULL
) && (installed_dr
== NULL
)) {
1562 "%s:%d: Between release and reaquire of global "
1563 "nd6_mutex lock, the list seems to have changed "
1564 "and it does not have any default routers for "
1566 __func__
, __LINE__
, if_name(ifp
)));
1570 if (selected_dr
!= installed_dr
)
1571 NDDR_ADDREF(selected_dr
);
1572 } else if (installed_dr
!= NULL
) {
1573 if (installed_dr
!= selected_dr
) {
1575 * This means that selected default router is reachable
1576 * while installed one may or may not be.
1577 * Static router should always be considered as reachable
1578 * for router selection process.
1580 if ((installed_dr
->stateflags
& NDDRF_STATIC
) &&
1581 rtpref(installed_dr
) >= rtpref(selected_dr
)) {
1582 NDDR_REMREF(selected_dr
);
1583 selected_dr
= installed_dr
;
1584 } else if (is_installed_reachable
) {
1585 if (rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1586 NDDR_REMREF(selected_dr
);
1587 selected_dr
= installed_dr
;
1591 NDDR_REMREF(selected_dr
);
1597 * If the selected router is different than the installed one,
1598 * remove the installed router and install the selected one.
1599 * Note that the selected router is never NULL here.
1600 * Else check if the route entry scope has to be changed.
1602 lck_mtx_unlock(nd6_mutex
);
1603 if (installed_dr
!= selected_dr
) {
1605 "%s:%d: Found a better router for interface "
1606 "%s. Installing new default route.\n",
1607 __func__
, __LINE__
, if_name(ifp
)));
1608 if (installed_dr
!= NULL
) {
1609 defrouter_delreq(installed_dr
);
1612 * Install scoped route if the interface is not
1613 * the default nd6 interface.
1615 defrouter_addreq(selected_dr
,
1616 (selected_dr
->ifp
!= nd6_defifp
));
1617 } else if (((installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1618 (installed_dr
->ifp
== nd6_defifp
)) ||
1619 (!(installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1620 (installed_dr
->ifp
!= nd6_defifp
))) {
1622 "%s:%d: Need to reinstall default route for interface "
1623 "%s as its scope has changed.\n",
1624 __func__
, __LINE__
, if_name(ifp
)));
1625 defrouter_delreq(installed_dr
);
1626 defrouter_addreq(installed_dr
,
1627 (installed_dr
->ifp
!= nd6_defifp
));
1630 "%s:%d: No need to change the default "
1631 "route for interface %s.\n",
1632 __func__
, __LINE__
, if_name(ifp
)));
1634 lck_mtx_lock(nd6_mutex
);
1636 if (selected_dr
&& (selected_dr
!= installed_dr
))
1637 NDDR_REMREF(selected_dr
);
1639 NDDR_REMREF(installed_dr
);
1640 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1641 VERIFY(nd_defrouter_busy
);
1642 nd_defrouter_busy
= FALSE
;
1643 if (nd_defrouter_waiters
> 0) {
1644 nd_defrouter_waiters
= 0;
1645 wakeup(nd_defrouter_waitchan
);
1649 static struct nd_defrouter
*
1650 defrtrlist_update_common(struct nd_defrouter
*new, boolean_t scoped
)
1652 struct nd_defrouter
*dr
, *n
;
1653 struct ifnet
*ifp
= new->ifp
;
1654 struct nd_ifinfo
*ndi
= NULL
;
1655 struct timeval caltime
;
1657 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1659 if ((dr
= defrouter_lookup(&new->rtaddr
, ifp
)) != NULL
) {
1661 if (new->rtlifetime
== 0) {
1662 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1664 NDDR_REMREF(dr
); /* remove list reference */
1668 int oldpref
= rtpref(dr
);
1669 struct nd_defrouter
*p
= NULL
;
1671 dr
->flags
= new->flags
; /* xxx flag check */
1672 dr
->rtlifetime
= new->rtlifetime
;
1673 dr
->expire
= new->expire
;
1676 * If the preference does not change, there's no need
1677 * to sort the entries. If Scoped Routing is enabled,
1678 * put the primary/non-scoped router at the top of the
1679 * list of routers in the same preference band, unless
1680 * it's already at that position.
1682 /* same preference and scoped; just return */
1683 if (rtpref(new) == oldpref
&& scoped
)
1686 n
= TAILQ_FIRST(&nd_defrouter
);
1688 /* preference changed; sort it */
1689 if (rtpref(new) != oldpref
)
1692 /* not at the top of band; sort it */
1693 if (n
!= dr
&& rtpref(n
) == oldpref
&&
1694 (!p
|| rtpref(p
) > rtpref(n
)))
1698 n
= TAILQ_NEXT(n
, dr_entry
);
1701 /* nothing has changed, just return */
1702 if (n
== NULL
&& (scoped
||
1703 !(dr
->stateflags
& NDDRF_IFSCOPE
)))
1707 * preferred router may be changed, so relocate
1709 * XXX: calling TAILQ_REMOVE directly is a bad manner.
1710 * However, since defrtrlist_del() has many side
1711 * effects, we intentionally do so here.
1712 * defrouter_select() below will handle routing
1715 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1716 new->stateflags
= dr
->stateflags
;
1726 /* entry does not exist */
1727 if (new->rtlifetime
== 0) {
1731 n
= nddr_alloc(M_WAITOK
);
1736 ndi
= ND_IFINFO(ifp
);
1737 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1738 lck_mtx_lock(&ndi
->lock
);
1739 if (ip6_maxifdefrouters
>= 0 &&
1740 ndi
->ndefrouters
>= ip6_maxifdefrouters
) {
1741 lck_mtx_unlock(&ndi
->lock
);
1746 NDDR_ADDREF(n
); /* for the nd_defrouter list */
1747 NDDR_ADDREF(n
); /* for the caller */
1749 ++nd6_defrouter_genid
;
1751 VERIFY(ndi
->ndefrouters
!= 0);
1752 lck_mtx_unlock(&ndi
->lock
);
1754 nd6log2((LOG_INFO
, "%s: allocating defrouter %s\n", if_name(ifp
),
1755 ip6_sprintf(&new->rtaddr
)));
1757 getmicrotime(&caltime
);
1759 memcpy(&n
->rtaddr
, &new->rtaddr
, sizeof (n
->rtaddr
));
1760 n
->flags
= new->flags
;
1761 n
->stateflags
= new->stateflags
;
1762 n
->rtlifetime
= new->rtlifetime
;
1763 n
->expire
= new->expire
;
1764 n
->base_calendartime
= caltime
.tv_sec
;
1765 n
->base_uptime
= net_uptime();
1770 /* get nd6_service() to be scheduled as soon as it's convenient */
1771 ++nd6_sched_timeout_want
;
1774 * Insert the new router in the Default Router List;
1775 * The Default Router List should be in the descending order
1776 * of router-preferece. When Scoped Routing is disabled, routers
1777 * with the same preference are sorted in the arriving time order;
1778 * otherwise, the first entry in the list of routers having the same
1779 * preference is the primary default router, when the interface used
1780 * by the entry is the default interface.
1783 /* insert at the end of the group */
1784 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1785 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1786 if (rtpref(n
) > rtpref(dr
) ||
1787 (!scoped
&& rtpref(n
) == rtpref(dr
)))
1791 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1793 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
1795 defrouter_select(ifp
);
1800 static struct nd_defrouter
*
1801 defrtrlist_update(struct nd_defrouter
*new)
1803 struct nd_defrouter
*dr
;
1805 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1806 dr
= defrtrlist_update_common(new,
1807 (nd6_defifp
!= NULL
&& new->ifp
!= nd6_defifp
));
1812 static struct nd_pfxrouter
*
1813 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1815 struct nd_pfxrouter
*search
;
1817 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1818 NDPR_LOCK_ASSERT_HELD(pr
);
1820 for (search
= pr
->ndpr_advrtrs
.lh_first
; search
;
1821 search
= search
->pfr_next
) {
1822 if (search
->router
== dr
)
1830 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1832 struct nd_pfxrouter
*new;
1834 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1835 NDPR_LOCK_ASSERT_NOTHELD(pr
);
1837 new = zalloc(ndprtr_zone
);
1840 bzero(new, sizeof (*new));
1844 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
1848 pfxlist_onlink_check();
1852 pfxrtr_del(struct nd_pfxrouter
*pfr
, struct nd_prefix
*pr
)
1854 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1855 NDPR_LOCK_ASSERT_HELD(pr
);
1857 LIST_REMOVE(pfr
, pfr_entry
);
1858 zfree(ndprtr_zone
, pfr
);
1862 * The routine has been modified to atomically refresh expiry
1863 * time for nd6 prefix as the part of lookup.
1864 * There's a corner case where a system going
1865 * in sleep gets rid of manual addresses configured in the system
1866 * and then schedules the prefix for deletion.
1867 * However before the prefix gets deleted, if system comes out
1868 * from sleep and configures same address before prefix deletion
1869 * , the later prefix deletion will remove the prefix route and
1870 * the system will not be able to communicate with other IPv6
1871 * neighbor nodes in the same subnet.
1874 nd6_prefix_lookup(struct nd_prefix
*pr
, int nd6_prefix_expiry
)
1876 struct nd_prefix
*search
;
1878 lck_mtx_lock(nd6_mutex
);
1879 for (search
= nd_prefix
.lh_first
; search
; search
= search
->ndpr_next
) {
1881 if (pr
->ndpr_ifp
== search
->ndpr_ifp
&&
1882 pr
->ndpr_plen
== search
->ndpr_plen
&&
1883 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1884 &search
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1885 if (nd6_prefix_expiry
!= ND6_PREFIX_EXPIRY_UNSPEC
) {
1886 search
->ndpr_expire
= nd6_prefix_expiry
;
1888 NDPR_ADDREF_LOCKED(search
);
1889 NDPR_UNLOCK(search
);
1892 NDPR_UNLOCK(search
);
1894 lck_mtx_unlock(nd6_mutex
);
1900 nd6_prelist_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
,
1901 struct nd_prefix
**newp
, boolean_t force_scoped
)
1903 struct nd_prefix
*new = NULL
;
1904 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1905 struct nd_ifinfo
*ndi
= NULL
;
1908 if (ip6_maxifprefixes
>= 0) {
1909 ndi
= ND_IFINFO(ifp
);
1910 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1911 lck_mtx_lock(&ndi
->lock
);
1912 if (ndi
->nprefixes
>= ip6_maxifprefixes
) {
1913 lck_mtx_unlock(&ndi
->lock
);
1916 lck_mtx_unlock(&ndi
->lock
);
1919 new = ndpr_alloc(M_WAITOK
);
1925 new->ndpr_ifp
= pr
->ndpr_ifp
;
1926 new->ndpr_prefix
= pr
->ndpr_prefix
;
1927 new->ndpr_plen
= pr
->ndpr_plen
;
1928 new->ndpr_vltime
= pr
->ndpr_vltime
;
1929 new->ndpr_pltime
= pr
->ndpr_pltime
;
1930 new->ndpr_flags
= pr
->ndpr_flags
;
1931 if (pr
->ndpr_stateflags
& NDPRF_STATIC
)
1932 new->ndpr_stateflags
|= NDPRF_STATIC
;
1934 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
1939 new->ndpr_lastupdate
= net_uptime();
1942 NDPR_ADDREF_LOCKED(new); /* for caller */
1944 /* initialization */
1945 LIST_INIT(&new->ndpr_advrtrs
);
1946 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
1947 /* make prefix in the canonical form */
1948 for (i
= 0; i
< 4; i
++)
1949 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1950 new->ndpr_mask
.s6_addr32
[i
];
1954 /* get nd6_service() to be scheduled as soon as it's convenient */
1955 ++nd6_sched_timeout_want
;
1957 lck_mtx_lock(nd6_mutex
);
1958 /* link ndpr_entry to nd_prefix list */
1959 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
1960 new->ndpr_debug
|= IFD_ATTACHED
;
1961 NDPR_ADDREF(new); /* for nd_prefix list */
1963 lck_mtx_lock(&ndi
->lock
);
1965 VERIFY(ndi
->nprefixes
!= 0);
1966 lck_mtx_unlock(&ndi
->lock
);
1968 /* ND_OPT_PI_FLAG_ONLINK processing */
1969 if (new->ndpr_raf_onlink
) {
1972 if ((e
= nd6_prefix_onlink_common(new, force_scoped
,
1973 new->ndpr_ifp
->if_index
)) != 0) {
1974 nd6log((LOG_ERR
, "nd6_prelist_add: failed to make "
1975 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
1976 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
1977 new->ndpr_plen
, force_scoped
? "scoped" :
1978 "non-scoped", if_name(ifp
), e
));
1979 /* proceed anyway. XXX: is it correct? */
1984 pfxrtr_add(new, dr
);
1987 lck_mtx_unlock(nd6_mutex
);
1993 * Caller must have held an extra reference on nd_prefix.
1996 prelist_remove(struct nd_prefix
*pr
)
1998 struct nd_pfxrouter
*pfr
, *next
;
1999 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2001 struct nd_ifinfo
*ndi
= NULL
;
2003 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2004 NDPR_LOCK_ASSERT_HELD(pr
);
2006 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
2010 * If there are no more addresses, defunct the prefix. This is needed
2011 * because we don't want multiple threads calling prelist_remove() for
2012 * the same prefix and this might happen because we unlock nd6_mutex
2015 if (pr
->ndpr_addrcnt
== 0)
2016 pr
->ndpr_stateflags
|= NDPRF_DEFUNCT
;
2018 /* make sure to invalidate the prefix until it is really freed. */
2019 pr
->ndpr_vltime
= 0;
2020 pr
->ndpr_pltime
= 0;
2023 * Though these flags are now meaningless, we'd rather keep the value
2024 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2025 * when executing "ndp -p".
2027 if (pr
->ndpr_stateflags
& NDPRF_ONLINK
) {
2028 NDPR_ADDREF_LOCKED(pr
);
2030 lck_mtx_unlock(nd6_mutex
);
2031 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2032 nd6log((LOG_ERR
, "prelist_remove: failed to make "
2033 "%s/%d offlink on %s, errno=%d\n",
2034 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2035 pr
->ndpr_plen
, if_name(ifp
), e
));
2036 /* what should we do? */
2038 lck_mtx_lock(nd6_mutex
);
2040 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2044 if (pr
->ndpr_addrcnt
> 0) {
2046 * The state might have changed if we called
2047 * nd6_prefix_offlink().
2049 pr
->ndpr_stateflags
&= ~NDPRF_DEFUNCT
;
2050 return; /* notice here? */
2053 /* unlink ndpr_entry from nd_prefix list */
2054 LIST_REMOVE(pr
, ndpr_entry
);
2055 pr
->ndpr_debug
&= ~IFD_ATTACHED
;
2057 /* free list of routers that adversed the prefix */
2058 for (pfr
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr
= next
) {
2059 next
= pfr
->pfr_next
;
2060 pfxrtr_del(pfr
, pr
);
2063 ndi
= ND_IFINFO(ifp
);
2064 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2065 lck_mtx_lock(&ndi
->lock
);
2066 VERIFY(ndi
->nprefixes
> 0);
2068 lck_mtx_unlock(&ndi
->lock
);
2070 /* This must not be the last reference to the nd_prefix */
2071 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2072 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__
, pr
);
2077 * Don't call pfxlist_onlink_check() here because we are
2078 * holding the NDPR lock and this could cause a deadlock when
2079 * there are multiple threads executing pfxlist_onlink_check().
2085 struct nd_prefix
*new,
2086 struct nd_defrouter
*dr
, /* may be NULL */
2090 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
2092 struct ifnet
*ifp
= new->ndpr_ifp
;
2093 struct nd_prefix
*pr
;
2097 struct in6_addrlifetime lt6_tmp
;
2098 uint64_t timenow
= net_uptime();
2100 /* no need to lock "new" here, as it is local to the caller */
2101 NDPR_LOCK_ASSERT_NOTHELD(new);
2106 * Authenticity for NA consists authentication for
2107 * both IP header and IP datagrams, doesn't it ?
2109 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
2110 auth
= (m
->m_flags
& M_AUTHIPHDR
) && (m
->m_flags
& M_AUTHIPDGM
);
2114 if ((pr
= nd6_prefix_lookup(new, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
2116 * nd6_prefix_lookup() ensures that pr and new have the same
2117 * prefix on a same interface.
2121 * Update prefix information. Note that the on-link (L) bit
2122 * and the autonomous (A) bit should NOT be changed from 1
2125 lck_mtx_lock(nd6_mutex
);
2127 if (new->ndpr_raf_onlink
== 1)
2128 pr
->ndpr_raf_onlink
= 1;
2129 if (new->ndpr_raf_auto
== 1)
2130 pr
->ndpr_raf_auto
= 1;
2131 if (new->ndpr_raf_onlink
) {
2132 pr
->ndpr_vltime
= new->ndpr_vltime
;
2133 pr
->ndpr_pltime
= new->ndpr_pltime
;
2134 (void) in6_init_prefix_ltimes(pr
); /* XXX error case? */
2135 pr
->ndpr_lastupdate
= net_uptime();
2138 NDPR_ADDREF_LOCKED(pr
);
2139 if (new->ndpr_raf_onlink
&&
2140 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
2144 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2146 "prelist_update: failed to make "
2147 "the prefix %s/%d on-link on %s "
2149 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2150 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
2151 /* proceed anyway. XXX: is it correct? */
2156 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
) {
2163 lck_mtx_unlock(nd6_mutex
);
2167 if (new->ndpr_vltime
== 0)
2169 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
2172 bzero(&new->ndpr_addr
, sizeof (struct in6_addr
));
2174 error
= nd6_prelist_add(new, dr
, &pr
, FALSE
);
2175 if (error
!= 0 || pr
== NULL
) {
2176 nd6log((LOG_NOTICE
, "prelist_update: "
2177 "nd6_prelist_add failed for %s/%d on %s "
2178 "errno=%d, returnpr=0x%llx\n",
2179 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2180 new->ndpr_plen
, if_name(new->ndpr_ifp
),
2181 error
, (uint64_t)VM_KERNEL_ADDRPERM(pr
)));
2182 goto end
; /* we should just give up in this case. */
2187 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
2188 * Note that pr must be non NULL at this point.
2191 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
2192 if (!new->ndpr_raf_auto
)
2196 * 5.5.3 (b). the link-local prefix should have been ignored in
2200 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2201 if (new->ndpr_pltime
> new->ndpr_vltime
) {
2202 error
= EINVAL
; /* XXX: won't be used */
2207 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2208 * an address configured by stateless autoconfiguration already in the
2209 * list of addresses associated with the interface, and the Valid
2210 * Lifetime is not 0, form an address. We first check if we have
2211 * a matching prefix.
2213 ifnet_lock_shared(ifp
);
2214 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2215 struct in6_ifaddr
*ifa6
;
2216 u_int32_t remaininglifetime
;
2219 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2223 ifa6
= (struct in6_ifaddr
*)ifa
;
2226 * We only consider autoconfigured addresses as per RFC 4862.
2228 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
2233 * Spec is not clear here, but I believe we should concentrate
2234 * on unicast (i.e. not anycast) addresses.
2235 * XXX: other ia6_flags? detached or duplicated?
2237 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0) {
2242 * Ignore the address if it is not associated with a prefix
2243 * or is associated with a prefix that is different from this
2244 * one. (pr is never NULL here)
2246 if (ifa6
->ia6_ndpr
!= pr
) {
2251 if (ia6_match
== NULL
) { /* remember the first one */
2253 IFA_ADDREF_LOCKED(ifa
); /* for ia6_match */
2257 * An already autoconfigured address matched. Now that we
2258 * are sure there is at least one matched address, we can
2259 * proceed to 5.5.3. (e): update the lifetimes according to the
2260 * "two hours" rule and the privacy extension.
2262 #define TWOHOUR (120*60)
2264 /* retrieve time as uptime (last arg is 0) */
2265 in6ifa_getlifetime(ifa6
, <6_tmp
, 0);
2267 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
2268 remaininglifetime
= ND6_INFINITE_LIFETIME
;
2269 else if (timenow
- ifa6
->ia6_updatetime
> lt6_tmp
.ia6t_vltime
) {
2271 * The case of "invalid" address. We should usually
2272 * not see this case.
2274 remaininglifetime
= 0;
2276 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
2277 (timenow
- ifa6
->ia6_updatetime
);
2279 /* when not updating, keep the current stored lifetime. */
2280 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
2282 if (TWOHOUR
< new->ndpr_vltime
||
2283 remaininglifetime
< new->ndpr_vltime
) {
2284 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2285 } else if (remaininglifetime
<= TWOHOUR
) {
2287 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2291 * new->ndpr_vltime <= TWOHOUR &&
2292 * TWOHOUR < remaininglifetime
2294 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
2297 /* The 2 hour rule is not imposed for preferred lifetime. */
2298 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
2300 /* Special handling for lifetimes of temporary addresses. */
2301 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
2302 u_int32_t maxvltime
, maxpltime
;
2304 /* Constrain lifetimes to system limits. */
2305 if (lt6_tmp
.ia6t_vltime
> ip6_temp_valid_lifetime
)
2306 lt6_tmp
.ia6t_vltime
= ip6_temp_valid_lifetime
;
2307 if (lt6_tmp
.ia6t_pltime
> ip6_temp_preferred_lifetime
)
2308 lt6_tmp
.ia6t_pltime
=
2309 ip6_temp_preferred_lifetime
-
2313 * According to RFC 4941, section 3.3 (1), we only
2314 * update the lifetimes when they are in the maximum
2317 if (ip6_temp_valid_lifetime
>
2318 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2319 ip6_desync_factor
)) {
2320 maxvltime
= ip6_temp_valid_lifetime
-
2321 (timenow
- ifa6
->ia6_createtime
) -
2325 if (ip6_temp_preferred_lifetime
>
2326 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2327 ip6_desync_factor
)) {
2328 maxpltime
= ip6_temp_preferred_lifetime
-
2329 (timenow
- ifa6
->ia6_createtime
) -
2334 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
2335 lt6_tmp
.ia6t_vltime
> maxvltime
)
2336 lt6_tmp
.ia6t_vltime
= maxvltime
;
2338 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
2339 lt6_tmp
.ia6t_pltime
> maxpltime
)
2340 lt6_tmp
.ia6t_pltime
= maxpltime
;
2343 in6_init_address_ltimes(pr
, <6_tmp
);
2345 in6ifa_setlifetime(ifa6
, <6_tmp
);
2346 ifa6
->ia6_updatetime
= timenow
;
2349 ifnet_lock_done(ifp
);
2350 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
2352 * 5.5.3 (d) (continued)
2353 * No address matched and the valid lifetime is non-zero.
2354 * Create a new address.
2357 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
))
2360 * note that we should use pr (not new) for reference.
2362 IFA_LOCK(&ia6
->ia_ifa
);
2365 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2367 VERIFY(pr
->ndpr_addrcnt
!= 0);
2369 IFA_UNLOCK(&ia6
->ia_ifa
);
2373 * When a new public address is created as described
2374 * in RFC 4862, also create a new temporary address.
2377 * When an interface connects to a new link, a new
2378 * randomized interface identifier should be generated
2379 * immediately together with a new set of temporary
2380 * addresses. Thus, we specifiy 1 as the 2nd arg of
2383 if (ip6_use_tempaddr
) {
2385 if ((e
= in6_tmpifadd(ia6
, 1)) != 0) {
2386 nd6log((LOG_NOTICE
, "prelist_update: "
2387 "failed to create a temporary "
2388 "address, errno=%d\n",
2392 IFA_REMREF(&ia6
->ia_ifa
);
2396 * A newly added address might affect the status
2397 * of other addresses, so we check and update it.
2398 * XXX: what if address duplication happens?
2400 lck_mtx_lock(nd6_mutex
);
2401 pfxlist_onlink_check();
2402 lck_mtx_unlock(nd6_mutex
);
2409 if (ia6_match
!= NULL
)
2410 IFA_REMREF(&ia6_match
->ia_ifa
);
2415 * Neighbor Discover Default Router structure reference counting routines.
2417 static struct nd_defrouter
*
2420 struct nd_defrouter
*dr
;
2422 dr
= (how
== M_WAITOK
) ? zalloc(nddr_zone
) : zalloc_noblock(nddr_zone
);
2424 bzero(dr
, nddr_size
);
2425 lck_mtx_init(&dr
->nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2426 dr
->nddr_debug
|= IFD_ALLOC
;
2427 if (nddr_debug
!= 0) {
2428 dr
->nddr_debug
|= IFD_DEBUG
;
2429 dr
->nddr_trace
= nddr_trace
;
2436 nddr_free(struct nd_defrouter
*dr
)
2439 if (dr
->nddr_debug
& IFD_ATTACHED
) {
2440 panic("%s: attached nddr %p is being freed", __func__
, dr
);
2442 } else if (!(dr
->nddr_debug
& IFD_ALLOC
)) {
2443 panic("%s: nddr %p cannot be freed", __func__
, dr
);
2446 dr
->nddr_debug
&= ~IFD_ALLOC
;
2449 lck_mtx_destroy(&dr
->nddr_lock
, ifa_mtx_grp
);
2450 zfree(nddr_zone
, dr
);
2454 nddr_trace(struct nd_defrouter
*dr
, int refhold
)
2456 struct nd_defrouter_dbg
*dr_dbg
= (struct nd_defrouter_dbg
*)dr
;
2461 if (!(dr
->nddr_debug
& IFD_DEBUG
)) {
2462 panic("%s: nddr %p has no debug structure", __func__
, dr
);
2466 cnt
= &dr_dbg
->nddr_refhold_cnt
;
2467 tr
= dr_dbg
->nddr_refhold
;
2469 cnt
= &dr_dbg
->nddr_refrele_cnt
;
2470 tr
= dr_dbg
->nddr_refrele
;
2473 idx
= atomic_add_16_ov(cnt
, 1) % NDDR_TRACE_HIST_SIZE
;
2474 ctrace_record(&tr
[idx
]);
2478 nddr_addref(struct nd_defrouter
*nddr
, int locked
)
2482 NDDR_LOCK_SPIN(nddr
);
2484 NDDR_LOCK_ASSERT_HELD(nddr
);
2486 if (++nddr
->nddr_refcount
== 0) {
2487 panic("%s: nddr %p wraparound refcnt\n", __func__
, nddr
);
2489 } else if (nddr
->nddr_trace
!= NULL
) {
2490 (*nddr
->nddr_trace
)(nddr
, TRUE
);
2497 struct nd_defrouter
*
2498 nddr_remref(struct nd_defrouter
*nddr
, int locked
)
2502 NDDR_LOCK_SPIN(nddr
);
2504 NDDR_LOCK_ASSERT_HELD(nddr
);
2506 if (nddr
->nddr_refcount
== 0) {
2507 panic("%s: nddr %p negative refcnt\n", __func__
, nddr
);
2509 } else if (nddr
->nddr_trace
!= NULL
) {
2510 (*nddr
->nddr_trace
)(nddr
, FALSE
);
2513 if (--nddr
->nddr_refcount
== 0) {
2519 if (!locked
&& nddr
!= NULL
)
2526 nddr_getexpire(struct nd_defrouter
*dr
)
2528 struct timeval caltime
;
2531 if (dr
->expire
!= 0) {
2532 /* account for system time change */
2533 getmicrotime(&caltime
);
2535 dr
->base_calendartime
+=
2536 NET_CALCULATE_CLOCKSKEW(caltime
,
2537 dr
->base_calendartime
, net_uptime(), dr
->base_uptime
);
2539 expiry
= dr
->base_calendartime
+
2540 dr
->expire
- dr
->base_uptime
;
2548 * Neighbor Discover Prefix structure reference counting routines.
2550 static struct nd_prefix
*
2553 struct nd_prefix
*pr
;
2555 pr
= (how
== M_WAITOK
) ? zalloc(ndpr_zone
) : zalloc_noblock(ndpr_zone
);
2557 bzero(pr
, ndpr_size
);
2558 lck_mtx_init(&pr
->ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2559 RB_INIT(&pr
->ndpr_prproxy_sols
);
2560 pr
->ndpr_debug
|= IFD_ALLOC
;
2561 if (ndpr_debug
!= 0) {
2562 pr
->ndpr_debug
|= IFD_DEBUG
;
2563 pr
->ndpr_trace
= ndpr_trace
;
2570 ndpr_free(struct nd_prefix
*pr
)
2573 if (pr
->ndpr_debug
& IFD_ATTACHED
) {
2574 panic("%s: attached ndpr %p is being freed", __func__
, pr
);
2576 } else if (!(pr
->ndpr_debug
& IFD_ALLOC
)) {
2577 panic("%s: ndpr %p cannot be freed", __func__
, pr
);
2579 } else if (pr
->ndpr_rt
!= NULL
) {
2580 panic("%s: ndpr %p route %p not freed", __func__
, pr
,
2583 } else if (pr
->ndpr_prproxy_sols_cnt
!= 0) {
2584 panic("%s: ndpr %p non-zero solicitors count (%d)",
2585 __func__
, pr
, pr
->ndpr_prproxy_sols_cnt
);
2587 } else if (!RB_EMPTY(&pr
->ndpr_prproxy_sols
)) {
2588 panic("%s: ndpr %p non-empty solicitors tree", __func__
, pr
);
2591 pr
->ndpr_debug
&= ~IFD_ALLOC
;
2594 lck_mtx_destroy(&pr
->ndpr_lock
, ifa_mtx_grp
);
2595 zfree(ndpr_zone
, pr
);
2599 ndpr_trace(struct nd_prefix
*pr
, int refhold
)
2601 struct nd_prefix_dbg
*pr_dbg
= (struct nd_prefix_dbg
*)pr
;
2606 if (!(pr
->ndpr_debug
& IFD_DEBUG
)) {
2607 panic("%s: ndpr %p has no debug structure", __func__
, pr
);
2611 cnt
= &pr_dbg
->ndpr_refhold_cnt
;
2612 tr
= pr_dbg
->ndpr_refhold
;
2614 cnt
= &pr_dbg
->ndpr_refrele_cnt
;
2615 tr
= pr_dbg
->ndpr_refrele
;
2618 idx
= atomic_add_16_ov(cnt
, 1) % NDPR_TRACE_HIST_SIZE
;
2619 ctrace_record(&tr
[idx
]);
2623 ndpr_addref(struct nd_prefix
*ndpr
, int locked
)
2626 NDPR_LOCK_SPIN(ndpr
);
2628 NDPR_LOCK_ASSERT_HELD(ndpr
);
2630 if (++ndpr
->ndpr_refcount
== 0) {
2631 panic("%s: ndpr %p wraparound refcnt\n", __func__
, ndpr
);
2633 } else if (ndpr
->ndpr_trace
!= NULL
) {
2634 (*ndpr
->ndpr_trace
)(ndpr
, TRUE
);
2642 ndpr_remref(struct nd_prefix
*ndpr
, int locked
)
2645 NDPR_LOCK_SPIN(ndpr
);
2647 NDPR_LOCK_ASSERT_HELD(ndpr
);
2649 if (ndpr
->ndpr_refcount
== 0) {
2650 panic("%s: ndpr %p negative refcnt\n", __func__
, ndpr
);
2652 } else if (ndpr
->ndpr_trace
!= NULL
) {
2653 (*ndpr
->ndpr_trace
)(ndpr
, FALSE
);
2656 if (--ndpr
->ndpr_refcount
== 0) {
2657 if (ndpr
->ndpr_addrcnt
!= 0) {
2658 panic("%s: freeing ndpr %p with outstanding address "
2659 "reference (%d)", __func__
, ndpr
,
2660 ndpr
->ndpr_addrcnt
);
2668 if (!locked
&& ndpr
!= NULL
)
2675 ndpr_getexpire(struct nd_prefix
*pr
)
2677 struct timeval caltime
;
2680 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_vltime
!= ND6_INFINITE_LIFETIME
) {
2681 /* account for system time change */
2682 getmicrotime(&caltime
);
2684 pr
->ndpr_base_calendartime
+=
2685 NET_CALCULATE_CLOCKSKEW(caltime
,
2686 pr
->ndpr_base_calendartime
, net_uptime(),
2687 pr
->ndpr_base_uptime
);
2689 expiry
= pr
->ndpr_base_calendartime
+
2690 pr
->ndpr_expire
- pr
->ndpr_base_uptime
;
2698 * A supplement function used in the on-link detection below;
2699 * detect if a given prefix has a (probably) reachable advertising router.
2700 * XXX: lengthy function name...
2702 * Callers *must* increase the reference count of nd_prefix.
2704 static struct nd_pfxrouter
*
2705 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
2707 struct nd_pfxrouter
*pfxrtr
;
2709 struct llinfo_nd6
*ln
;
2711 struct in6_addr rtaddr
;
2714 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2715 NDPR_LOCK_ASSERT_HELD(pr
);
2717 genid
= pr
->ndpr_genid
;
2718 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2720 ifp
= pfxrtr
->router
->ifp
;
2721 if (pfxrtr
->router
->stateflags
& NDDRF_MAPPED
)
2722 rtaddr
= pfxrtr
->router
->rtaddr_mapped
;
2724 rtaddr
= pfxrtr
->router
->rtaddr
;
2727 lck_mtx_unlock(nd6_mutex
);
2728 /* Callee returns a locked route upon success */
2729 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
2730 RT_LOCK_ASSERT_HELD(rt
);
2731 if ((ln
= rt
->rt_llinfo
) != NULL
&&
2732 ND6_IS_LLINFO_PROBREACH(ln
)) {
2733 RT_REMREF_LOCKED(rt
);
2735 lck_mtx_lock(nd6_mutex
);
2739 RT_REMREF_LOCKED(rt
);
2742 lck_mtx_lock(nd6_mutex
);
2744 if (pr
->ndpr_genid
!= genid
) {
2745 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2746 genid
= pr
->ndpr_genid
;
2748 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
);
2750 NDPR_LOCK_ASSERT_HELD(pr
);
2757 * Check if each prefix in the prefix list has at least one available router
2758 * that advertised the prefix (a router is "available" if its neighbor cache
2759 * entry is reachable or probably reachable).
2760 * If the check fails, the prefix may be off-link, because, for example,
2761 * we have moved from the network but the lifetime of the prefix has not
2762 * expired yet. So we should not use the prefix if there is another prefix
2763 * that has an available router.
2764 * But, if there is no prefix that has an available router, we still regards
2765 * all the prefixes as on-link. This is because we can't tell if all the
2766 * routers are simply dead or if we really moved from the network and there
2767 * is no router around us.
2770 pfxlist_onlink_check(void)
2772 struct nd_prefix
*pr
, *prclear
;
2773 struct in6_ifaddr
*ifa
;
2774 struct nd_defrouter
*dr
;
2775 struct nd_pfxrouter
*pfxrtr
= NULL
;
2776 int err
, i
, found
= 0;
2777 struct ifaddr
**ifap
= NULL
;
2778 struct nd_prefix
*ndpr
;
2780 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2782 while (nd_prefix_busy
) {
2783 nd_prefix_waiters
++;
2784 msleep(nd_prefix_waitchan
, nd6_mutex
, (PZERO
-1),
2786 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2788 nd_prefix_busy
= TRUE
;
2791 * Check if there is a prefix that has a reachable advertising
2794 pr
= nd_prefix
.lh_first
;
2797 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
) {
2802 NDPR_ADDREF_LOCKED(pr
);
2803 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
) &&
2804 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
2805 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2811 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2815 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
2816 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
2817 * stop us from checking the same prefix twice.
2819 pr
= nd_prefix
.lh_first
;
2821 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2823 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2824 NDPR_UNLOCK(prclear
);
2827 * If we have no such prefix, check whether we still have a router
2828 * that does not advertise any prefixes.
2831 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
2832 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2833 struct nd_prefix
*pr0
;
2835 for (pr0
= nd_prefix
.lh_first
; pr0
;
2836 pr0
= pr0
->ndpr_next
) {
2838 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
) {
2848 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && pfxrtr
== NULL
)) {
2850 * There is at least one prefix that has a reachable router,
2851 * or at least a router which probably does not advertise
2852 * any prefixes. The latter would be the case when we move
2853 * to a new link where we have a router that does not provide
2854 * prefixes and we configure an address by hand.
2855 * Detach prefixes which have no reachable advertising
2856 * router, and attach other prefixes.
2858 pr
= nd_prefix
.lh_first
;
2862 * We aren't interested prefixes already processed,
2863 * nor in prefixes without the L bit
2864 * set nor in static prefixes
2866 if (pr
->ndpr_raf_onlink
== 0 ||
2867 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2868 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2873 NDPR_ADDREF_LOCKED(pr
);
2874 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2875 find_pfxlist_reachable_router(pr
) == NULL
&&
2876 (pr
->ndpr_debug
& IFD_ATTACHED
))
2877 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
2878 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2879 find_pfxlist_reachable_router(pr
) != NULL
&&
2880 (pr
->ndpr_debug
& IFD_ATTACHED
))
2881 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2882 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2886 * Since find_pfxlist_reachable_router() drops the
2887 * nd6_mutex, we have to start over, but the
2888 * NDPRF_PROCESSED_ONLINK flag will stop us from
2889 * checking the same prefix twice.
2891 pr
= nd_prefix
.lh_first
;
2894 /* there is no prefix that has a reachable router */
2895 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2897 if (pr
->ndpr_raf_onlink
== 0 ||
2898 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2902 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
2903 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2907 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2909 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2910 NDPR_UNLOCK(prclear
);
2913 * Remove each interface route associated with a (just) detached
2914 * prefix, and reinstall the interface route for a (just) attached
2915 * prefix. Note that all attempt of reinstallation does not
2916 * necessarily success, when a same prefix is shared among multiple
2917 * interfaces. Such cases will be handled in nd6_prefix_onlink,
2918 * so we don't have to care about them.
2920 pr
= nd_prefix
.lh_first
;
2925 if (pr
->ndpr_raf_onlink
== 0 ||
2926 pr
->ndpr_stateflags
& NDPRF_STATIC
||
2927 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2928 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
2933 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2934 NDPR_ADDREF_LOCKED(pr
);
2935 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2936 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
2938 lck_mtx_unlock(nd6_mutex
);
2939 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2941 "pfxlist_onlink_check: failed to "
2942 "make %s/%d offlink, errno=%d\n",
2943 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2946 lck_mtx_lock(nd6_mutex
);
2948 pr
= nd_prefix
.lh_first
;
2951 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2952 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
2953 pr
->ndpr_raf_onlink
) {
2955 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2957 "pfxlist_onlink_check: failed to "
2958 "make %s/%d offlink, errno=%d\n",
2959 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2963 pr
= nd_prefix
.lh_first
;
2971 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2973 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2974 NDPR_UNLOCK(prclear
);
2976 VERIFY(nd_prefix_busy
);
2977 nd_prefix_busy
= FALSE
;
2978 if (nd_prefix_waiters
> 0) {
2979 nd_prefix_waiters
= 0;
2980 wakeup(nd_prefix_waitchan
);
2984 * Changes on the prefix status might affect address status as well.
2985 * Make sure that all addresses derived from an attached prefix are
2986 * attached, and that all addresses derived from a detached prefix are
2987 * detached. Note, however, that a manually configured address should
2988 * always be attached.
2989 * The precise detection logic is same as the one for prefixes.
2991 * ifnet_get_address_list_family_internal() may fail due to memory
2992 * pressure, but we will eventually be called again when we receive
2993 * another NA, RA, or when the link status changes.
2995 err
= ifnet_get_address_list_family_internal(NULL
, &ifap
, AF_INET6
, 0,
2997 if (err
!= 0 || ifap
== NULL
) {
2998 nd6log((LOG_ERR
, "%s: ifnet_get_address_list_family_internal "
2999 "failed", __func__
));
3002 for (i
= 0; ifap
[i
]; i
++) {
3003 ifa
= ifatoia6(ifap
[i
]);
3004 IFA_LOCK(&ifa
->ia_ifa
);
3005 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3006 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3007 IFA_UNLOCK(&ifa
->ia_ifa
);
3010 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3012 * This can happen when we first configure the address
3013 * (i.e. the address exists, but the prefix does not).
3014 * XXX: complicated relationships...
3016 IFA_UNLOCK(&ifa
->ia_ifa
);
3019 IFA_UNLOCK(&ifa
->ia_ifa
);
3022 NDPR_ADDREF_LOCKED(ndpr
);
3023 if (find_pfxlist_reachable_router(ndpr
)) {
3024 if (NDPR_REMREF_LOCKED(ndpr
) == NULL
) {
3036 for (i
= 0; ifap
[i
]; i
++) {
3037 ifa
= ifatoia6(ifap
[i
]);
3038 IFA_LOCK(&ifa
->ia_ifa
);
3039 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3040 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3041 IFA_UNLOCK(&ifa
->ia_ifa
);
3044 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3045 /* XXX: see above. */
3046 IFA_UNLOCK(&ifa
->ia_ifa
);
3049 IFA_UNLOCK(&ifa
->ia_ifa
);
3051 NDPR_ADDREF_LOCKED(ndpr
);
3052 if (find_pfxlist_reachable_router(ndpr
)) {
3054 IFA_LOCK(&ifa
->ia_ifa
);
3055 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3056 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3057 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3058 IFA_UNLOCK(&ifa
->ia_ifa
);
3059 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3061 IFA_UNLOCK(&ifa
->ia_ifa
);
3065 IFA_LOCK(&ifa
->ia_ifa
);
3066 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
3067 IFA_UNLOCK(&ifa
->ia_ifa
);
3072 for (i
= 0; ifap
[i
]; i
++) {
3073 ifa
= ifatoia6(ifap
[i
]);
3074 IFA_LOCK(&ifa
->ia_ifa
);
3075 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3076 IFA_UNLOCK(&ifa
->ia_ifa
);
3079 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3080 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3081 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3082 IFA_UNLOCK(&ifa
->ia_ifa
);
3083 /* Do we need a delay in this case? */
3084 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3086 IFA_UNLOCK(&ifa
->ia_ifa
);
3090 ifnet_free_address_list(ifap
);
3093 static struct nd_prefix
*
3094 nd6_prefix_equal_lookup(struct nd_prefix
*pr
, boolean_t primary_only
)
3096 struct nd_prefix
*opr
;
3098 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3100 for (opr
= nd_prefix
.lh_first
; opr
; opr
= opr
->ndpr_next
) {
3105 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3109 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
3110 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
3111 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
) &&
3113 !(opr
->ndpr_stateflags
& NDPRF_IFSCOPE
))) {
3114 NDPR_ADDREF_LOCKED(opr
);
3124 * Synchronize the interface routes of similar prefixes on different
3125 * interfaces; the one using the default interface would be (re)installed
3126 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3129 nd6_prefix_sync(struct ifnet
*ifp
)
3131 struct nd_prefix
*pr
, *opr
;
3134 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3139 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3141 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
3145 if (pr
->ndpr_ifp
== ifp
&&
3146 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) &&
3147 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3157 /* Remove conflicting entries */
3158 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3160 lck_mtx_unlock(nd6_mutex
);
3161 err
= nd6_prefix_offlink(opr
);
3162 lck_mtx_lock(nd6_mutex
);
3165 "%s: failed to make %s/%d offlink on %s, "
3166 "errno=%d\n", __func__
,
3167 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3168 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3172 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3173 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3174 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
)));
3177 lck_mtx_unlock(nd6_mutex
);
3178 err
= nd6_prefix_offlink(pr
);
3179 lck_mtx_lock(nd6_mutex
);
3182 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3183 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3184 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3187 /* Add the entries back */
3189 err
= nd6_prefix_onlink_scoped(opr
, opr
->ndpr_ifp
->if_index
);
3192 "%s: failed to make %s/%d scoped onlink on %s, "
3193 "errno=%d\n", __func__
,
3194 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3195 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3199 err
= nd6_prefix_onlink_scoped(pr
, IFSCOPE_NONE
);
3202 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3203 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3204 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3209 "%s: error promoting %s/%d to %s from %s\n",
3210 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3211 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3212 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3215 "%s: %s/%d promoted, previously on %s\n",
3216 if_name(pr
->ndpr_ifp
),
3217 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
,
3218 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3226 nd6_prefix_onlink_common(struct nd_prefix
*pr
, boolean_t force_scoped
,
3227 unsigned int ifscope
)
3230 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3231 struct sockaddr_in6 mask6
, prefix
;
3232 struct nd_prefix
*opr
;
3234 int error
= 0, prproxy
= 0;
3235 struct rtentry
*rt
= NULL
;
3237 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3241 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3243 "%s: %s/%d on %s scoped=%d is already on-link\n",
3244 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3245 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3246 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3253 * Add the interface route associated with the prefix. Before
3254 * installing the route, check if there's the same prefix on another
3255 * interface, and the prefix has already installed the interface route.
3257 opr
= nd6_prefix_equal_lookup(pr
, FALSE
);
3261 if (!force_scoped
) {
3263 * If a primary/non-scoped interface route already exists,
3264 * install the new one as a scoped entry. If the existing
3265 * interface route is scoped, install new as non-scoped.
3267 ifscope
= (opr
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
3268 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3271 else if (ifscope
!= IFSCOPE_NONE
)
3272 ifscope
= IFSCOPE_NONE
;
3276 * We prefer link-local addresses as the associated interface address.
3278 /* search for a link-local addr */
3279 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
3280 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3282 struct in6_ifaddr
*ia6
;
3283 ifnet_lock_shared(ifp
);
3284 IFP_TO_IA6(ifp
, ia6
);
3285 ifnet_lock_done(ifp
);
3288 /* should we care about ia6_flags? */
3293 * This can still happen, when, for example, we receive an RA
3294 * containing a prefix with the L bit set and the A bit clear,
3295 * after removing all IPv6 addresses on the receiving
3296 * interface. This should, of course, be rare though.
3299 "nd6_prefix_onlink: failed to find any ifaddr"
3300 " to add route for a prefix(%s/%d) on %s\n",
3301 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3302 pr
->ndpr_plen
, if_name(ifp
)));
3308 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3309 * ifa->ifa_rtrequest = nd6_rtrequest;
3311 bzero(&mask6
, sizeof (mask6
));
3312 mask6
.sin6_len
= sizeof (mask6
);
3313 mask6
.sin6_addr
= pr
->ndpr_mask
;
3314 prefix
= pr
->ndpr_prefix
;
3315 if ((rt
= pr
->ndpr_rt
) != NULL
)
3317 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3321 rtflags
= ifa
->ifa_flags
| RTF_CLONING
| RTF_UP
;
3323 if (nd6_need_cache(ifp
)) {
3324 /* explicitly set in case ifa_flags does not set the flag. */
3325 rtflags
|= RTF_CLONING
;
3328 * explicitly clear the cloning bit in case ifa_flags sets it.
3330 rtflags
&= ~RTF_CLONING
;
3333 lck_mtx_unlock(nd6_mutex
);
3340 error
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&prefix
,
3341 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
,
3345 * Serialize the setting of NDPRF_PRPROXY.
3347 lck_mtx_lock(&proxy6_lock
);
3351 nd6_rtmsg(RTM_ADD
, rt
);
3356 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a"
3357 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx,"
3358 " scoped=%d, errno = %d\n",
3359 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3360 pr
->ndpr_plen
, if_name(ifp
),
3361 ip6_sprintf(&((struct sockaddr_in6
*)
3362 (void *)ifa
->ifa_addr
)->sin6_addr
),
3363 ip6_sprintf(&mask6
.sin6_addr
), rtflags
,
3364 (ifscope
!= IFSCOPE_NONE
), error
));
3366 NDPR_LOCK_ASSERT_HELD(pr
);
3368 pr
->ndpr_stateflags
&= ~(NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3371 * TODO: If the prefix route exists, we should really find it and
3372 * refer the prefix to it; otherwise ndpr_rt is NULL.
3374 if (!(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) &&
3375 (rt
!= NULL
|| error
== EEXIST
)) {
3376 struct nd_ifinfo
*ndi
= NULL
;
3378 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3379 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3381 ndi
= ND_IFINFO(ifp
);
3382 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3383 lck_mtx_lock(&ndi
->lock
);
3385 pr
->ndpr_rt
= rt
; /* keep reference from rtrequest */
3386 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
3387 if (ifscope
!= IFSCOPE_NONE
) {
3388 pr
->ndpr_stateflags
|= NDPRF_IFSCOPE
;
3389 } else if ((rtflags
& RTF_CLONING
) &&
3390 (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) &&
3391 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3393 * At present, in order for the prefix to be eligible
3394 * as a proxying/proxied prefix, we require that the
3395 * prefix route entry be marked as a cloning route with
3396 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3397 * true for the interface type, hence the test for
3398 * RTF_CLONING above.
3400 pr
->ndpr_stateflags
|= NDPRF_PRPROXY
;
3403 lck_mtx_unlock(&ndi
->lock
);
3404 } else if (rt
!= NULL
&& pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
3407 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3408 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3414 * If this is an upstream prefix, find the downstream ones (if any)
3415 * and re-configure their prefix routes accordingly. Otherwise,
3416 * this could be potentially be a downstream prefix, and so find the
3417 * upstream prefix, if any.
3419 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3421 NDPR_REMREF(pr
); /* release reference for this routine */
3422 lck_mtx_unlock(&proxy6_lock
);
3424 lck_mtx_lock(nd6_mutex
);
3430 nd6_prefix_onlink(struct nd_prefix
*pr
)
3432 return (nd6_prefix_onlink_common(pr
, FALSE
, IFSCOPE_NONE
));
3436 nd6_prefix_onlink_scoped(struct nd_prefix
*pr
, unsigned int ifscope
)
3438 return (nd6_prefix_onlink_common(pr
, TRUE
, ifscope
));
3442 nd6_prefix_offlink(struct nd_prefix
*pr
)
3444 int plen
, error
= 0, prproxy
;
3445 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3446 struct sockaddr_in6 sa6
, mask6
, prefix
;
3447 struct rtentry
*rt
= NULL
, *ndpr_rt
= NULL
;
3448 unsigned int ifscope
;
3450 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3454 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3456 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3457 "off-link\n", ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3458 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3459 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0));
3464 bzero(&sa6
, sizeof (sa6
));
3465 sa6
.sin6_family
= AF_INET6
;
3466 sa6
.sin6_len
= sizeof (sa6
);
3467 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
,
3468 sizeof (struct in6_addr
));
3469 bzero(&mask6
, sizeof (mask6
));
3470 mask6
.sin6_family
= AF_INET6
;
3471 mask6
.sin6_len
= sizeof (sa6
);
3472 bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof (struct in6_addr
));
3473 prefix
= pr
->ndpr_prefix
;
3474 plen
= pr
->ndpr_plen
;
3475 if ((ndpr_rt
= pr
->ndpr_rt
) != NULL
)
3477 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3480 ifscope
= (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ?
3481 ifp
->if_index
: IFSCOPE_NONE
;
3483 error
= rtrequest_scoped(RTM_DELETE
, (struct sockaddr
*)&sa6
,
3484 NULL
, (struct sockaddr
*)&mask6
, 0, &rt
, ifscope
);
3487 /* report the route deletion to the routing socket. */
3489 nd6_rtmsg(RTM_DELETE
, rt
);
3495 "nd6_prefix_offlink: failed to delete route: "
3496 "%s/%d on %s, scoped %d, (errno = %d)\n",
3497 ip6_sprintf(&sa6
.sin6_addr
), plen
, if_name(ifp
),
3498 (ifscope
!= IFSCOPE_NONE
), error
));
3501 if (ndpr_rt
!= NULL
)
3504 lck_mtx_lock(&proxy6_lock
);
3507 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3508 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3509 pr
->ndpr_stateflags
&= ~(NDPRF_ONLINK
| NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3510 if (pr
->ndpr_prproxy_sols_cnt
> 0) {
3512 nd6_prproxy_sols_reap(pr
);
3513 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3514 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3519 * If this was an upstream prefix, find the downstream ones and do
3520 * some cleanups. If this was a downstream prefix, the prefix route
3521 * has been removed from the routing table above, but there may be
3522 * other tasks to perform.
3524 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3526 NDPR_REMREF(pr
); /* release reference for this routine */
3527 lck_mtx_unlock(&proxy6_lock
);
3532 static struct in6_ifaddr
*
3533 in6_pfx_newpersistaddr(struct nd_prefix
*pr
, int mcast
, int *errorp
)
3535 struct in6_ifaddr
*ia6
= NULL
;
3536 struct ifnet
*ifp
= NULL
;
3537 struct nd_ifinfo
*ndi
= NULL
;
3538 struct in6_addr mask
;
3539 struct in6_aliasreq ifra
;
3540 int error
, ifaupdate
, iidlen
, notcga
;
3543 VERIFY(errorp
!= NULL
);
3551 * Prefix Length check:
3552 * If the sum of the prefix length and interface identifier
3553 * length does not equal 128 bits, the Prefix Information
3554 * option MUST be ignored. The length of the interface
3555 * identifier is defined in a separate link-type specific
3558 iidlen
= in6_if2idlen(ifp
);
3560 error
= EADDRNOTAVAIL
;
3561 /* this should not happen, so we always log it. */
3562 log(LOG_ERR
, "%s: IID length undefined (%s)\n",
3563 __func__
, if_name(ifp
));
3565 } else if (iidlen
!= 64) {
3566 error
= EADDRNOTAVAIL
;
3568 * stateless autoconfiguration not yet well-defined for IID
3569 * lengths other than 64 octets. Just give up for now.
3571 nd6log((LOG_INFO
, "%s: IID length not 64 octets (%s)\n",
3572 __func__
, if_name(ifp
)));
3576 if (iidlen
+ pr
->ndpr_plen
!= 128) {
3577 error
= EADDRNOTAVAIL
;
3579 "%s: invalid prefix length %d for %s, ignored\n",
3580 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
3584 bzero(&ifra
, sizeof (ifra
));
3585 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3586 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
3587 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
3590 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
,
3591 sizeof (ifra
.ifra_addr
.sin6_addr
));
3592 in6_len2mask(&mask
, pr
->ndpr_plen
);
3593 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
3594 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
3595 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
3596 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
3598 ndi
= ND_IFINFO(ifp
);
3599 VERIFY(ndi
->initialized
);
3600 lck_mtx_lock(&ndi
->lock
);
3602 notcga
= nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
||
3603 (ndi
->flags
& ND6_IFF_INSECURE
) != 0;
3605 lck_mtx_unlock(&ndi
->lock
);
3609 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
3611 error
= EADDRNOTAVAIL
;
3612 nd6log((LOG_INFO
, "%s: no link-local address (%s)\n",
3613 __func__
, if_name(ifp
)));
3617 IFA_LOCK(&ia6
->ia_ifa
);
3618 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
3619 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
3620 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
3621 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
3622 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3623 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
3624 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3625 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
3626 IFA_UNLOCK(&ia6
->ia_ifa
);
3627 IFA_REMREF(&ia6
->ia_ifa
);
3630 in6_cga_node_lock();
3631 struct in6_cga_prepare local_cga_prepare
;
3633 if (ndi
->cga_initialized
) {
3634 bcopy(&(ndi
->local_cga_modifier
),
3635 &(local_cga_prepare
.cga_modifier
),
3636 sizeof(local_cga_prepare
.cga_modifier
));
3637 error
= in6_cga_generate(&local_cga_prepare
, 0,
3638 &ifra
.ifra_addr
.sin6_addr
);
3640 error
= in6_cga_generate(NULL
, 0,
3641 &ifra
.ifra_addr
.sin6_addr
);
3643 in6_cga_node_unlock();
3645 ifra
.ifra_flags
|= IN6_IFF_SECURED
;
3647 nd6log((LOG_ERR
, "%s: no CGA available (%s)\n",
3648 __func__
, if_name(ifp
)));
3653 VERIFY(ia6
== NULL
);
3655 /* new prefix mask. */
3656 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
3657 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
3658 bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
,
3659 sizeof (ifra
.ifra_prefixmask
.sin6_addr
));
3662 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
3663 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
3666 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
3669 * Make sure that we do not have this address already. This should
3670 * usually not happen, but we can still see this case, e.g., if we
3671 * have manually configured the exact address to be configured.
3673 if ((ia6
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
))
3675 error
= EADDRNOTAVAIL
;
3676 IFA_REMREF(&ia6
->ia_ifa
);
3679 /* this should be rare enough to make an explicit log */
3680 log(LOG_INFO
, "%s: %s is already configured!\n",
3681 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
3686 * Allocate ifaddr structure, link into chain, etc.
3687 * If we are going to create a new address upon receiving a multicasted
3688 * RA, we need to impose a random delay before starting DAD.
3689 * [RFC 4862, Section 5.4.2]
3691 ifaupdate
= IN6_IFAUPDATE_NOWAIT
;
3693 ifaupdate
|= IN6_IFAUPDATE_DADDELAY
;
3694 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &ia6
);
3697 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
3698 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
),
3699 if_name(ifp
), error
));
3700 error
= EADDRNOTAVAIL
;
3704 VERIFY(ia6
!= NULL
);
3705 in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia6
, NULL
);
3716 #define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
3719 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
)
3721 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
3722 struct in6_ifaddr
*ia
, *newia
;
3723 struct in6_aliasreq ifra
;
3724 int i
, error
, ifaupdate
;
3725 int trylimit
= 3; /* XXX: adhoc value */
3726 u_int32_t randid
[2];
3727 time_t vltime0
, pltime0
;
3728 uint64_t timenow
= net_uptime();
3729 struct in6_addr addr
;
3730 struct nd_prefix
*ndpr
;
3732 bzero(&ifra
, sizeof (ifra
));
3733 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3734 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3735 ifra
.ifra_addr
= ia0
->ia_addr
;
3736 /* copy prefix mask */
3737 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
3738 /* clear the old IFID */
3739 for (i
= 0; i
< 4; i
++) {
3740 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
]
3741 &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
3743 addr
= ia0
->ia_addr
.sin6_addr
;
3744 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3747 in6_iid_mktmp(ifp
, (u_int8_t
*)randid
,
3748 (const u_int8_t
*)&addr
.s6_addr
[8], forcegen
);
3750 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3751 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
3752 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3753 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
3756 * in6_iid_mktmp() quite likely provided a unique interface ID.
3757 * However, we may still have a chance to see collision, because
3758 * there may be a time lag between generation of the ID and generation
3759 * of the address. So, we'll do one more sanity check.
3761 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
)) != NULL
) {
3762 IFA_REMREF(&ia
->ia_ifa
);
3763 if (trylimit
-- == 0) {
3764 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find "
3765 "a unique random IFID\n"));
3773 * The Valid Lifetime is the lower of the Valid Lifetime of the
3774 * public address or TEMP_VALID_LIFETIME.
3775 * The Preferred Lifetime is the lower of the Preferred Lifetime
3776 * of the public address or TEMP_PREFERRED_LIFETIME -
3779 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3780 if (ia0
->ia6_lifetime
.ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3781 vltime0
= IFA6_IS_INVALID(ia0
, timenow
) ? 0 :
3782 (ia0
->ia6_lifetime
.ia6ti_vltime
-
3783 (timenow
- ia0
->ia6_updatetime
));
3784 if (vltime0
> ip6_temp_valid_lifetime
)
3785 vltime0
= ip6_temp_valid_lifetime
;
3787 vltime0
= ip6_temp_valid_lifetime
;
3789 if (ia0
->ia6_lifetime
.ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3790 pltime0
= IFA6_IS_DEPRECATED(ia0
, timenow
) ? 0 :
3791 (ia0
->ia6_lifetime
.ia6ti_pltime
-
3792 (timenow
- ia0
->ia6_updatetime
));
3793 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
)
3794 pltime0
= ip6_temp_preferred_lifetime
-
3797 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
3799 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
3800 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
3801 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3803 * A temporary address is created only if this calculated Preferred
3804 * Lifetime is greater than REGEN_ADVANCE time units.
3806 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
)
3809 /* XXX: scope zone ID? */
3811 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
3813 /* allocate ifaddr structure, link into chain, etc. */
3814 ifaupdate
= IN6_IFAUPDATE_NOWAIT
| IN6_IFAUPDATE_DADDELAY
;
3815 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &newia
);
3817 nd6log((LOG_ERR
, "in6_tmpifadd: failed to add address.\n"));
3820 VERIFY(newia
!= NULL
);
3822 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3823 ndpr
= ia0
->ia6_ndpr
;
3826 * We lost the race with another thread that has purged
3827 * ia0 address; in this case, purge the tmp addr as well.
3829 nd6log((LOG_ERR
, "in6_tmpifadd: no public address\n"));
3830 VERIFY(!(ia0
->ia6_flags
& IN6_IFF_AUTOCONF
));
3831 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3832 in6_purgeaddr(&newia
->ia_ifa
);
3833 IFA_REMREF(&newia
->ia_ifa
);
3834 return (EADDRNOTAVAIL
);
3836 NDPR_ADDREF(ndpr
); /* for us */
3837 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3838 IFA_LOCK(&newia
->ia_ifa
);
3839 if (newia
->ia6_ndpr
!= NULL
) {
3840 NDPR_LOCK(newia
->ia6_ndpr
);
3841 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3842 newia
->ia6_ndpr
->ndpr_addrcnt
--;
3843 NDPR_UNLOCK(newia
->ia6_ndpr
);
3844 NDPR_REMREF(newia
->ia6_ndpr
); /* release addr reference */
3846 newia
->ia6_ndpr
= ndpr
;
3847 NDPR_LOCK(newia
->ia6_ndpr
);
3848 newia
->ia6_ndpr
->ndpr_addrcnt
++;
3849 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3850 NDPR_ADDREF_LOCKED(newia
->ia6_ndpr
); /* for addr reference */
3851 NDPR_UNLOCK(newia
->ia6_ndpr
);
3852 IFA_UNLOCK(&newia
->ia_ifa
);
3854 * A newly added address might affect the status of other addresses.
3855 * XXX: when the temporary address is generated with a new public
3856 * address, the onlink check is redundant. However, it would be safe
3857 * to do the check explicitly everywhere a new address is generated,
3858 * and, in fact, we surely need the check when we create a new
3859 * temporary address due to deprecation of an old temporary address.
3861 lck_mtx_lock(nd6_mutex
);
3862 pfxlist_onlink_check();
3863 lck_mtx_unlock(nd6_mutex
);
3864 IFA_REMREF(&newia
->ia_ifa
);
3866 /* remove our reference */
3874 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
3876 struct timeval caltime
;
3877 u_int64_t timenow
= net_uptime();
3879 NDPR_LOCK_ASSERT_HELD(ndpr
);
3881 getmicrotime(&caltime
);
3882 ndpr
->ndpr_base_calendartime
= caltime
.tv_sec
;
3883 ndpr
->ndpr_base_uptime
= timenow
;
3885 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
3886 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
3887 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime"
3888 "(%d) is greater than valid lifetime(%d)\n",
3889 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
));
3892 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
3893 ndpr
->ndpr_preferred
= 0;
3895 ndpr
->ndpr_preferred
= timenow
+ ndpr
->ndpr_pltime
;
3896 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
3897 ndpr
->ndpr_expire
= 0;
3899 ndpr
->ndpr_expire
= timenow
+ ndpr
->ndpr_vltime
;
3905 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
3908 uint64_t timenow
= net_uptime();
3910 /* Valid lifetime must not be updated unless explicitly specified. */
3911 /* init ia6t_expire */
3912 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
3913 lt6
->ia6t_expire
= 0;
3915 lt6
->ia6t_expire
= timenow
;
3916 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
3919 /* init ia6t_preferred */
3920 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
) {
3921 lt6
->ia6t_preferred
= 0;
3923 lt6
->ia6t_preferred
= timenow
;
3924 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
3929 * Delete all the routing table entries that use the specified gateway.
3930 * XXX: this function causes search through all entries of routing table, so
3931 * it shouldn't be called when acting as a router.
3935 struct in6_addr
*gateway
,
3938 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
3940 /* We'll care only link-local addresses */
3941 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
3944 lck_mtx_lock(rnh_lock
);
3945 /* XXX: hack for KAME's link-local address kludge */
3946 gateway
->s6_addr16
[1] = htons(ifp
->if_index
);
3948 rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
);
3949 lck_mtx_unlock(rnh_lock
);
3954 struct radix_node
*rn
,
3957 struct rtentry
*rt
= (struct rtentry
*)rn
;
3958 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
3960 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
3963 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
) {
3968 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) {
3973 * Do not delete a static route.
3974 * XXX: this seems to be a bit ad-hoc. Should we consider the
3975 * 'cloned' bit instead?
3977 if ((rt
->rt_flags
& RTF_STATIC
) != 0) {
3982 * We delete only host route. This means, in particular, we don't
3983 * delete default route.
3985 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
3991 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
3992 * rnh_lock here prevents another thread from calling rt_setgate()
3996 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
3997 rt_mask(rt
), rt
->rt_flags
, 0));
4001 nd6_setdefaultiface(
4005 ifnet_t def_ifp
= NULL
;
4007 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4009 ifnet_head_lock_shared();
4010 if (ifindex
< 0 || if_index
< ifindex
) {
4014 def_ifp
= ifindex2ifnet
[ifindex
];
4017 lck_mtx_lock(nd6_mutex
);
4018 if (nd6_defifindex
!= ifindex
) {
4019 struct ifnet
*odef_ifp
= nd6_defifp
;
4021 nd6_defifindex
= ifindex
;
4022 if (nd6_defifindex
> 0)
4023 nd6_defifp
= def_ifp
;
4027 if (nd6_defifp
!= NULL
)
4028 nd6log((LOG_INFO
, "%s: is now the default "
4029 "interface (was %s)\n", if_name(nd6_defifp
),
4030 odef_ifp
!= NULL
? if_name(odef_ifp
) : "NONE"));
4032 nd6log((LOG_INFO
, "No default interface set\n"));
4035 * If the Default Router List is empty, install a route
4036 * to the specified interface as default or remove the default
4037 * route when the default interface becomes canceled.
4038 * The check for the queue is actually redundant, but
4039 * we do this here to avoid re-install the default route
4040 * if the list is NOT empty.
4042 if (odef_ifp
!= NULL
) {
4043 defrouter_select(odef_ifp
);
4046 if (nd6_defifp
!= NULL
) {
4047 defrouter_select(nd6_defifp
);
4048 nd6_prefix_sync(nd6_defifp
);
4052 * Our current implementation assumes one-to-one mapping between
4053 * interfaces and links, so it would be natural to use the
4054 * default interface as the default link.
4056 scope6_setdefault(nd6_defifp
);
4058 lck_mtx_unlock(nd6_mutex
);