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 (ND_IFINFO(dr_ifp
) == NULL
||
1044 !ND_IFINFO(dr_ifp
)->initialized
) {
1049 if (IN6_IS_SCOPE_EMBED(&dr0
.rtaddr
)) {
1050 uint16_t *scope
= &dr0
.rtaddr
.s6_addr16
[1];
1053 *scope
= htons(dr_ifp
->if_index
);
1054 } else if (*scope
!= htons(dr_ifp
->if_index
)) {
1061 error
= defrtrlist_add_static(&dr0
);
1062 if (!add
|| error
!= 0) {
1063 int err
= defrtrlist_del_static(&dr0
);
1070 error
= EOPNOTSUPP
; /* check for safety */
1078 * XXX Please make sure to remove dr from the
1079 * global default router tailq list before this
1081 * Also ensure that you release the list reference
1082 * only after calling this routine.
1085 defrtrlist_del(struct nd_defrouter
*dr
)
1087 #if (DEVELOPMENT || DEBUG)
1088 struct nd_defrouter
*dr_itr
= NULL
;
1090 struct nd_prefix
*pr
;
1091 struct ifnet
*ifp
= dr
->ifp
;
1092 struct nd_ifinfo
*ndi
= NULL
;
1095 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1097 #if (DEVELOPMENT || DEBUG)
1099 * Verify that the router is not in the global default
1101 * Can't use defrouter_lookup here because that just works
1102 * with address and ifp pointer.
1103 * We have to compare the memory here.
1104 * Also we can't use ASSERT here as that is not defined
1105 * for development builds.
1107 TAILQ_FOREACH(dr_itr
, &nd_defrouter
, dr_entry
)
1108 VERIFY(dr
!= dr_itr
);
1110 ++nd6_defrouter_genid
;
1112 * Flush all the routing table entries that use the router
1115 /* above is a good condition? */
1117 lck_mtx_unlock(nd6_mutex
);
1118 if (dr
->stateflags
& NDDRF_MAPPED
)
1119 rt6_flush(&dr
->rtaddr_mapped
, ifp
);
1121 rt6_flush(&dr
->rtaddr
, ifp
);
1123 lck_mtx_lock(nd6_mutex
);
1125 nd6log2((LOG_INFO
, "%s: freeing defrouter %s\n", if_name(dr
->ifp
),
1126 ip6_sprintf(&dr
->rtaddr
)));
1128 * Delete it from the routing table.
1131 lck_mtx_unlock(nd6_mutex
);
1132 defrouter_delreq(dr
);
1133 lck_mtx_lock(nd6_mutex
);
1137 * Also delete all the pointers to the router in each prefix lists.
1139 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1140 struct nd_pfxrouter
*pfxrtr
;
1143 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
1144 pfxrtr_del(pfxrtr
, pr
);
1148 pfxlist_onlink_check();
1151 ndi
= ND_IFINFO(ifp
);
1152 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1153 lck_mtx_lock(&ndi
->lock
);
1154 VERIFY(ndi
->ndefrouters
>= 0);
1155 if (ndi
->ndefrouters
> 0 && --ndi
->ndefrouters
== 0) {
1159 lck_mtx_unlock(&ndi
->lock
);
1162 * If the router is the primary one, choose a new one.
1163 * We always try to pick another eligible router
1164 * on this interface as we do scoped routing
1166 defrouter_select(ifp
);
1173 defrtrlist_add_static(struct nd_defrouter
*new)
1175 struct nd_defrouter
*dr
;
1178 new->rtlifetime
= -1;
1179 new->stateflags
|= NDDRF_STATIC
;
1181 /* we only want the preference level */
1182 new->flags
&= ND_RA_FLAG_RTPREF_MASK
;
1184 lck_mtx_lock(nd6_mutex
);
1185 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1186 if (dr
!= NULL
&& !(dr
->stateflags
& NDDRF_STATIC
)) {
1191 dr
= defrtrlist_update(new);
1199 lck_mtx_unlock(nd6_mutex
);
1205 defrtrlist_del_static(struct nd_defrouter
*new)
1207 struct nd_defrouter
*dr
;
1209 lck_mtx_lock(nd6_mutex
);
1210 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1211 if (dr
== NULL
|| !(dr
->stateflags
& NDDRF_STATIC
)) {
1216 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1218 NDDR_REMREF(dr
); /* remove list reference */
1221 lck_mtx_unlock(nd6_mutex
);
1223 return (dr
!= NULL
? 0 : EINVAL
);
1227 * for default router selection
1228 * regards router-preference field as a 2-bit signed integer
1231 rtpref(struct nd_defrouter
*dr
)
1233 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
1234 case ND_RA_FLAG_RTPREF_HIGH
:
1235 return (RTPREF_HIGH
);
1236 case ND_RA_FLAG_RTPREF_MEDIUM
:
1237 case ND_RA_FLAG_RTPREF_RSV
:
1238 return (RTPREF_MEDIUM
);
1239 case ND_RA_FLAG_RTPREF_LOW
:
1240 return (RTPREF_LOW
);
1243 * This case should never happen. If it did, it would mean a
1244 * serious bug of kernel internal. We thus always bark here.
1245 * Or, can we even panic?
1247 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
1248 return (RTPREF_INVALID
);
1254 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
1256 * 1) Routers that are reachable or probably reachable should be preferred.
1257 * If we have more than one (probably) reachable router, prefer ones
1258 * with the highest router preference.
1259 * 2) When no routers on the list are known to be reachable or
1260 * probably reachable, routers SHOULD be selected in a round-robin
1261 * fashion, regardless of router preference values.
1262 * 3) If the Default Router List is empty, assume that all
1263 * destinations are on-link.
1265 * When Scoped Routing is enabled, the selection logic is amended as follows:
1267 * a) When a default interface is specified, the primary/non-scoped default
1268 * router will be set to the reachable router on that link (if any) with
1269 * the highest router preference.
1270 * b) When there are more than one routers on the same link, the one with
1271 * the highest router preference will be installed, either as scoped or
1272 * non-scoped route entry. If they all share the same preference value,
1273 * the one installed will be the static or the first encountered reachable
1274 * router, i.e. static one wins over dynamic.
1275 * c) When no routers on the list are known to be reachable, or probably
1276 * reachable, no round-robin selection will take place when the default
1279 * We assume nd_defrouter is sorted by router preference value.
1280 * Since the code below covers both with and without router preference cases,
1281 * we do not need to classify the cases by ifdef.
1284 defrouter_select(struct ifnet
*ifp
)
1286 struct nd_defrouter
*dr
= NULL
;
1287 struct nd_defrouter
*selected_dr
= NULL
;
1288 struct nd_defrouter
*installed_dr
= NULL
;
1289 struct llinfo_nd6
*ln
= NULL
;
1290 struct rtentry
*rt
= NULL
;
1291 struct nd_ifinfo
*ndi
= NULL
;
1292 unsigned int genid
= 0;
1293 boolean_t is_installed_reachable
= FALSE
;
1295 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1299 "%s:%d: Return early. NULL interface",
1300 __func__
, __LINE__
));
1304 if (ifp
== lo_ifp
) {
1306 "%s:%d: Return early. "
1307 "Default router select called for loopback.\n",
1308 __func__
, __LINE__
));
1312 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1314 "%s:%d: Return early. "
1315 "Default router select called for interface"
1316 " %s with IFEF_IPV6_ROUTER flag set\n",
1317 __func__
, __LINE__
, if_name(ifp
)));
1322 * Let's handle easy case (3) first:
1323 * If default router list is empty, there's nothing to be done.
1325 if (!TAILQ_FIRST(&nd_defrouter
)) {
1327 "%s:%d: Return early. "
1328 "Default router is empty.\n", __func__
, __LINE__
));
1333 * Take an early exit if number of routers in nd_ifinfo is
1334 * 0 for the interface.
1336 ndi
= ND_IFINFO(ifp
);
1337 if (!ndi
|| !ndi
->initialized
) {
1339 "%s:%d: Return early. "
1340 "Interface %s's nd_ifinfo not initialized.\n",
1341 __func__
, __LINE__
, if_name(ifp
)));
1345 if (ndi
->ndefrouters
== 0) {
1347 "%s:%d: Return early. "
1348 "%s does not have any default routers.\n",
1349 __func__
, __LINE__
, if_name(ifp
)));
1354 * Due to the number of times we drop nd6_mutex, we need to
1355 * serialize this function.
1357 while (nd_defrouter_busy
) {
1358 nd_defrouter_waiters
++;
1359 msleep(nd_defrouter_waitchan
, nd6_mutex
, (PZERO
-1),
1361 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1363 nd_defrouter_busy
= TRUE
;
1366 * Search for a (probably) reachable router from the list.
1367 * We just pick up the first reachable one (if any), assuming that
1368 * the ordering rule of the list described in defrtrlist_update().
1370 * For all intents and purposes of Scoped Routing:
1371 * selected_dr = candidate for primary router
1372 * installed_dr = currently installed primary router
1374 genid
= nd6_defrouter_genid
;
1375 dr
= TAILQ_FIRST(&nd_defrouter
);
1377 while (dr
!= NULL
) {
1378 struct in6_addr rtaddr
;
1379 struct ifnet
*drifp
= NULL
;
1380 struct nd_defrouter
*drrele
= NULL
;
1386 dr
= TAILQ_NEXT(dr
, dr_entry
);
1391 * Optimize for the common case.
1392 * When the interface has only one default router
1393 * there's no point checking for reachability as
1394 * there's nothing else to choose from.
1396 if (ndi
->ndefrouters
== 1) {
1398 "%s:%d: Fast forward default router selection "
1399 "as interface %s has learned only one default "
1400 "router and there's nothing else to choose from.\n",
1401 __func__
, __LINE__
, if_name(ifp
)));
1402 VERIFY(selected_dr
== NULL
&& installed_dr
== NULL
);
1404 if (dr
->stateflags
& NDDRF_INSTALLED
)
1406 NDDR_ADDREF_LOCKED(selected_dr
);
1411 if (dr
->stateflags
& NDDRF_MAPPED
)
1412 rtaddr
= dr
->rtaddr_mapped
;
1414 rtaddr
= dr
->rtaddr
;
1416 NDDR_ADDREF_LOCKED(dr
); /* for this for loop */
1419 /* Callee returns a locked route upon success */
1420 if (selected_dr
== NULL
) {
1421 lck_mtx_unlock(nd6_mutex
);
1422 if ((rt
= nd6_lookup(&rtaddr
, 0, drifp
, 0)) != NULL
&&
1423 (ln
= rt
->rt_llinfo
) != NULL
&&
1424 ND6_IS_LLINFO_PROBREACH(ln
)) {
1425 RT_LOCK_ASSERT_HELD(rt
);
1427 NDDR_ADDREF(selected_dr
);
1429 lck_mtx_lock(nd6_mutex
);
1433 RT_REMREF_LOCKED(rt
);
1440 * When there are more than one routers on the same link, the one with
1441 * the highest router preference will be installed.
1442 * Since the list is in decreasing order of preference:
1443 * 1) If selected_dr is not NULL, only use dr if it is static and has
1444 * equal preference and selected_dr is not static.
1445 * 2) Else if selected_dr is NULL, and dr is static make selected_dr = dr
1448 if (((selected_dr
&& (rtpref(dr
) >= rtpref(selected_dr
)) &&
1449 !(selected_dr
->stateflags
& NDDRF_STATIC
)) ||
1450 (selected_dr
== NULL
)) &&
1451 (dr
->stateflags
& NDDRF_STATIC
)) {
1453 /* Release it later on */
1454 VERIFY(drrele
== NULL
);
1455 drrele
= selected_dr
;
1458 NDDR_ADDREF_LOCKED(selected_dr
);
1461 /* Record the currently installed router */
1462 if (dr
->stateflags
& NDDRF_INSTALLED
) {
1463 if (installed_dr
== NULL
) {
1465 NDDR_ADDREF_LOCKED(installed_dr
);
1466 if (dr
->stateflags
& NDDRF_MAPPED
)
1467 rtaddr
= installed_dr
->rtaddr_mapped
;
1469 rtaddr
= installed_dr
->rtaddr
;
1471 lck_mtx_unlock(nd6_mutex
);
1472 /* Callee returns a locked route upon success */
1473 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
1474 RT_LOCK_ASSERT_HELD(rt
);
1475 if ((ln
= rt
->rt_llinfo
) != NULL
&&
1476 ND6_IS_LLINFO_PROBREACH(ln
))
1477 is_installed_reachable
= TRUE
;
1479 RT_REMREF_LOCKED(rt
);
1483 lck_mtx_lock(nd6_mutex
);
1485 /* this should not happen; warn for diagnosis */
1486 nd6log((LOG_ERR
, "defrouter_select: more than one "
1487 "default router is installed for interface :%s.\n",
1494 NDDR_REMREF(dr
); /* for this for loop */
1496 NDDR_REMREF(drrele
);
1499 * Check if the list changed when we gave up
1500 * the nd6_mutex lock
1502 if(genid
!= nd6_defrouter_genid
) {
1504 NDDR_REMREF(selected_dr
);
1509 NDDR_REMREF(installed_dr
);
1510 installed_dr
= NULL
;
1513 if (ndi
->ndefrouters
== 0) {
1515 "%s:%d: Interface %s no longer "
1516 "has any default routers. Abort.\n",
1517 __func__
, __LINE__
, if_name(ifp
)));
1521 "%s:%d: Iterate default router list again "
1522 "for interface %s, as the list seems to have "
1523 "changed during release-reaquire of global "
1524 "nd6_mutex lock.\n",
1525 __func__
, __LINE__
, if_name(ifp
)));
1527 is_installed_reachable
= FALSE
;
1528 genid
= nd6_defrouter_genid
;
1529 dr
= TAILQ_FIRST(&nd_defrouter
);
1531 dr
= TAILQ_NEXT(dr
, dr_entry
);
1536 * If none of the default routers was found to be reachable,
1537 * round-robin the list regardless of preference.
1538 * Please note selected_dr equal to NULL implies that even
1539 * installed default router is not reachable
1541 if (selected_dr
== NULL
) {
1543 for (dr
= TAILQ_NEXT(installed_dr
, dr_entry
); dr
;
1544 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1545 if (installed_dr
->ifp
!= dr
->ifp
)
1553 * If none was installed or the installed one if the last
1554 * one on the list, select the first one from the list
1556 if ((installed_dr
== NULL
) || (selected_dr
== NULL
)) {
1557 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1558 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1559 if (dr
->ifp
== ifp
) {
1566 if ((selected_dr
== NULL
) && (installed_dr
== NULL
)) {
1568 "%s:%d: Between release and reaquire of global "
1569 "nd6_mutex lock, the list seems to have changed "
1570 "and it does not have any default routers for "
1572 __func__
, __LINE__
, if_name(ifp
)));
1576 if (selected_dr
!= installed_dr
)
1577 NDDR_ADDREF(selected_dr
);
1578 } else if (installed_dr
!= NULL
) {
1579 if (installed_dr
!= selected_dr
) {
1581 * This means that selected default router is reachable
1582 * while installed one may or may not be.
1583 * Static router should always be considered as reachable
1584 * for router selection process.
1586 if ((installed_dr
->stateflags
& NDDRF_STATIC
) &&
1587 rtpref(installed_dr
) >= rtpref(selected_dr
)) {
1588 NDDR_REMREF(selected_dr
);
1589 selected_dr
= installed_dr
;
1590 } else if (is_installed_reachable
) {
1591 if (rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1592 NDDR_REMREF(selected_dr
);
1593 selected_dr
= installed_dr
;
1597 NDDR_REMREF(selected_dr
);
1603 * If the selected router is different than the installed one,
1604 * remove the installed router and install the selected one.
1605 * Note that the selected router is never NULL here.
1606 * Else check if the route entry scope has to be changed.
1608 lck_mtx_unlock(nd6_mutex
);
1609 if (installed_dr
!= selected_dr
) {
1611 "%s:%d: Found a better router for interface "
1612 "%s. Installing new default route.\n",
1613 __func__
, __LINE__
, if_name(ifp
)));
1614 if (installed_dr
!= NULL
) {
1615 defrouter_delreq(installed_dr
);
1618 * Install scoped route if the interface is not
1619 * the default nd6 interface.
1621 defrouter_addreq(selected_dr
,
1622 (selected_dr
->ifp
!= nd6_defifp
));
1623 } else if (((installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1624 (installed_dr
->ifp
== nd6_defifp
)) ||
1625 (!(installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1626 (installed_dr
->ifp
!= nd6_defifp
))) {
1628 "%s:%d: Need to reinstall default route for interface "
1629 "%s as its scope has changed.\n",
1630 __func__
, __LINE__
, if_name(ifp
)));
1631 defrouter_delreq(installed_dr
);
1632 defrouter_addreq(installed_dr
,
1633 (installed_dr
->ifp
!= nd6_defifp
));
1636 "%s:%d: No need to change the default "
1637 "route for interface %s.\n",
1638 __func__
, __LINE__
, if_name(ifp
)));
1640 lck_mtx_lock(nd6_mutex
);
1642 if (selected_dr
&& (selected_dr
!= installed_dr
))
1643 NDDR_REMREF(selected_dr
);
1645 NDDR_REMREF(installed_dr
);
1646 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1647 VERIFY(nd_defrouter_busy
);
1648 nd_defrouter_busy
= FALSE
;
1649 if (nd_defrouter_waiters
> 0) {
1650 nd_defrouter_waiters
= 0;
1651 wakeup(nd_defrouter_waitchan
);
1655 static struct nd_defrouter
*
1656 defrtrlist_update_common(struct nd_defrouter
*new, boolean_t scoped
)
1658 struct nd_defrouter
*dr
, *n
;
1659 struct ifnet
*ifp
= new->ifp
;
1660 struct nd_ifinfo
*ndi
= NULL
;
1661 struct timeval caltime
;
1663 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1665 if ((dr
= defrouter_lookup(&new->rtaddr
, ifp
)) != NULL
) {
1667 if (new->rtlifetime
== 0) {
1668 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1670 NDDR_REMREF(dr
); /* remove list reference */
1674 int oldpref
= rtpref(dr
);
1675 struct nd_defrouter
*p
= NULL
;
1677 dr
->flags
= new->flags
; /* xxx flag check */
1678 dr
->rtlifetime
= new->rtlifetime
;
1679 dr
->expire
= new->expire
;
1682 * If the preference does not change, there's no need
1683 * to sort the entries. If Scoped Routing is enabled,
1684 * put the primary/non-scoped router at the top of the
1685 * list of routers in the same preference band, unless
1686 * it's already at that position.
1688 /* same preference and scoped; just return */
1689 if (rtpref(new) == oldpref
&& scoped
)
1692 n
= TAILQ_FIRST(&nd_defrouter
);
1694 /* preference changed; sort it */
1695 if (rtpref(new) != oldpref
)
1698 /* not at the top of band; sort it */
1699 if (n
!= dr
&& rtpref(n
) == oldpref
&&
1700 (!p
|| rtpref(p
) > rtpref(n
)))
1704 n
= TAILQ_NEXT(n
, dr_entry
);
1707 /* nothing has changed, just return */
1708 if (n
== NULL
&& (scoped
||
1709 !(dr
->stateflags
& NDDRF_IFSCOPE
)))
1713 * preferred router may be changed, so relocate
1715 * XXX: calling TAILQ_REMOVE directly is a bad manner.
1716 * However, since defrtrlist_del() has many side
1717 * effects, we intentionally do so here.
1718 * defrouter_select() below will handle routing
1721 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1722 new->stateflags
= dr
->stateflags
;
1732 /* entry does not exist */
1733 if (new->rtlifetime
== 0) {
1737 n
= nddr_alloc(M_WAITOK
);
1742 ndi
= ND_IFINFO(ifp
);
1743 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1744 lck_mtx_lock(&ndi
->lock
);
1745 if (ip6_maxifdefrouters
>= 0 &&
1746 ndi
->ndefrouters
>= ip6_maxifdefrouters
) {
1747 lck_mtx_unlock(&ndi
->lock
);
1752 NDDR_ADDREF(n
); /* for the nd_defrouter list */
1753 NDDR_ADDREF(n
); /* for the caller */
1755 ++nd6_defrouter_genid
;
1757 VERIFY(ndi
->ndefrouters
!= 0);
1758 lck_mtx_unlock(&ndi
->lock
);
1760 nd6log2((LOG_INFO
, "%s: allocating defrouter %s\n", if_name(ifp
),
1761 ip6_sprintf(&new->rtaddr
)));
1763 getmicrotime(&caltime
);
1765 memcpy(&n
->rtaddr
, &new->rtaddr
, sizeof (n
->rtaddr
));
1766 n
->flags
= new->flags
;
1767 n
->stateflags
= new->stateflags
;
1768 n
->rtlifetime
= new->rtlifetime
;
1769 n
->expire
= new->expire
;
1770 n
->base_calendartime
= caltime
.tv_sec
;
1771 n
->base_uptime
= net_uptime();
1776 /* get nd6_service() to be scheduled as soon as it's convenient */
1777 ++nd6_sched_timeout_want
;
1780 * Insert the new router in the Default Router List;
1781 * The Default Router List should be in the descending order
1782 * of router-preferece. When Scoped Routing is disabled, routers
1783 * with the same preference are sorted in the arriving time order;
1784 * otherwise, the first entry in the list of routers having the same
1785 * preference is the primary default router, when the interface used
1786 * by the entry is the default interface.
1789 /* insert at the end of the group */
1790 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1791 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1792 if (rtpref(n
) > rtpref(dr
) ||
1793 (!scoped
&& rtpref(n
) == rtpref(dr
)))
1797 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1799 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
1801 defrouter_select(ifp
);
1806 static struct nd_defrouter
*
1807 defrtrlist_update(struct nd_defrouter
*new)
1809 struct nd_defrouter
*dr
;
1811 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1812 dr
= defrtrlist_update_common(new,
1813 (nd6_defifp
!= NULL
&& new->ifp
!= nd6_defifp
));
1818 static struct nd_pfxrouter
*
1819 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1821 struct nd_pfxrouter
*search
;
1823 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1824 NDPR_LOCK_ASSERT_HELD(pr
);
1826 for (search
= pr
->ndpr_advrtrs
.lh_first
; search
;
1827 search
= search
->pfr_next
) {
1828 if (search
->router
== dr
)
1836 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1838 struct nd_pfxrouter
*new;
1840 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1841 NDPR_LOCK_ASSERT_NOTHELD(pr
);
1843 new = zalloc(ndprtr_zone
);
1846 bzero(new, sizeof (*new));
1850 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
1854 pfxlist_onlink_check();
1858 pfxrtr_del(struct nd_pfxrouter
*pfr
, struct nd_prefix
*pr
)
1860 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1861 NDPR_LOCK_ASSERT_HELD(pr
);
1863 LIST_REMOVE(pfr
, pfr_entry
);
1864 zfree(ndprtr_zone
, pfr
);
1868 * The routine has been modified to atomically refresh expiry
1869 * time for nd6 prefix as the part of lookup.
1870 * There's a corner case where a system going
1871 * in sleep gets rid of manual addresses configured in the system
1872 * and then schedules the prefix for deletion.
1873 * However before the prefix gets deleted, if system comes out
1874 * from sleep and configures same address before prefix deletion
1875 * , the later prefix deletion will remove the prefix route and
1876 * the system will not be able to communicate with other IPv6
1877 * neighbor nodes in the same subnet.
1880 nd6_prefix_lookup(struct nd_prefix
*pr
, int nd6_prefix_expiry
)
1882 struct nd_prefix
*search
;
1884 lck_mtx_lock(nd6_mutex
);
1885 for (search
= nd_prefix
.lh_first
; search
; search
= search
->ndpr_next
) {
1887 if (pr
->ndpr_ifp
== search
->ndpr_ifp
&&
1888 pr
->ndpr_plen
== search
->ndpr_plen
&&
1889 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1890 &search
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1891 if (nd6_prefix_expiry
!= ND6_PREFIX_EXPIRY_UNSPEC
) {
1892 search
->ndpr_expire
= nd6_prefix_expiry
;
1894 NDPR_ADDREF_LOCKED(search
);
1895 NDPR_UNLOCK(search
);
1898 NDPR_UNLOCK(search
);
1900 lck_mtx_unlock(nd6_mutex
);
1906 nd6_prelist_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
,
1907 struct nd_prefix
**newp
, boolean_t force_scoped
)
1909 struct nd_prefix
*new = NULL
;
1910 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1911 struct nd_ifinfo
*ndi
= NULL
;
1914 if (ip6_maxifprefixes
>= 0) {
1915 ndi
= ND_IFINFO(ifp
);
1916 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1917 lck_mtx_lock(&ndi
->lock
);
1918 if (ndi
->nprefixes
>= ip6_maxifprefixes
) {
1919 lck_mtx_unlock(&ndi
->lock
);
1922 lck_mtx_unlock(&ndi
->lock
);
1925 new = ndpr_alloc(M_WAITOK
);
1931 new->ndpr_ifp
= pr
->ndpr_ifp
;
1932 new->ndpr_prefix
= pr
->ndpr_prefix
;
1933 new->ndpr_plen
= pr
->ndpr_plen
;
1934 new->ndpr_vltime
= pr
->ndpr_vltime
;
1935 new->ndpr_pltime
= pr
->ndpr_pltime
;
1936 new->ndpr_flags
= pr
->ndpr_flags
;
1937 if (pr
->ndpr_stateflags
& NDPRF_STATIC
)
1938 new->ndpr_stateflags
|= NDPRF_STATIC
;
1940 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
1945 new->ndpr_lastupdate
= net_uptime();
1948 NDPR_ADDREF_LOCKED(new); /* for caller */
1950 /* initialization */
1951 LIST_INIT(&new->ndpr_advrtrs
);
1952 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
1953 /* make prefix in the canonical form */
1954 for (i
= 0; i
< 4; i
++)
1955 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1956 new->ndpr_mask
.s6_addr32
[i
];
1960 /* get nd6_service() to be scheduled as soon as it's convenient */
1961 ++nd6_sched_timeout_want
;
1963 lck_mtx_lock(nd6_mutex
);
1964 /* link ndpr_entry to nd_prefix list */
1965 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
1966 new->ndpr_debug
|= IFD_ATTACHED
;
1967 NDPR_ADDREF(new); /* for nd_prefix list */
1969 lck_mtx_lock(&ndi
->lock
);
1971 VERIFY(ndi
->nprefixes
!= 0);
1972 lck_mtx_unlock(&ndi
->lock
);
1974 /* ND_OPT_PI_FLAG_ONLINK processing */
1975 if (new->ndpr_raf_onlink
) {
1978 if ((e
= nd6_prefix_onlink_common(new, force_scoped
,
1979 new->ndpr_ifp
->if_index
)) != 0) {
1980 nd6log((LOG_ERR
, "nd6_prelist_add: failed to make "
1981 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
1982 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
1983 new->ndpr_plen
, force_scoped
? "scoped" :
1984 "non-scoped", if_name(ifp
), e
));
1985 /* proceed anyway. XXX: is it correct? */
1990 pfxrtr_add(new, dr
);
1993 lck_mtx_unlock(nd6_mutex
);
1999 * Caller must have held an extra reference on nd_prefix.
2002 prelist_remove(struct nd_prefix
*pr
)
2004 struct nd_pfxrouter
*pfr
, *next
;
2005 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2007 struct nd_ifinfo
*ndi
= NULL
;
2009 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2010 NDPR_LOCK_ASSERT_HELD(pr
);
2012 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
2016 * If there are no more addresses, defunct the prefix. This is needed
2017 * because we don't want multiple threads calling prelist_remove() for
2018 * the same prefix and this might happen because we unlock nd6_mutex
2021 if (pr
->ndpr_addrcnt
== 0)
2022 pr
->ndpr_stateflags
|= NDPRF_DEFUNCT
;
2024 /* make sure to invalidate the prefix until it is really freed. */
2025 pr
->ndpr_vltime
= 0;
2026 pr
->ndpr_pltime
= 0;
2029 * Though these flags are now meaningless, we'd rather keep the value
2030 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2031 * when executing "ndp -p".
2033 if (pr
->ndpr_stateflags
& NDPRF_ONLINK
) {
2034 NDPR_ADDREF_LOCKED(pr
);
2036 lck_mtx_unlock(nd6_mutex
);
2037 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2038 nd6log((LOG_ERR
, "prelist_remove: failed to make "
2039 "%s/%d offlink on %s, errno=%d\n",
2040 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2041 pr
->ndpr_plen
, if_name(ifp
), e
));
2042 /* what should we do? */
2044 lck_mtx_lock(nd6_mutex
);
2046 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2050 if (pr
->ndpr_addrcnt
> 0) {
2052 * The state might have changed if we called
2053 * nd6_prefix_offlink().
2055 pr
->ndpr_stateflags
&= ~NDPRF_DEFUNCT
;
2056 return; /* notice here? */
2059 /* unlink ndpr_entry from nd_prefix list */
2060 LIST_REMOVE(pr
, ndpr_entry
);
2061 pr
->ndpr_debug
&= ~IFD_ATTACHED
;
2063 /* free list of routers that adversed the prefix */
2064 for (pfr
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr
= next
) {
2065 next
= pfr
->pfr_next
;
2066 pfxrtr_del(pfr
, pr
);
2069 ndi
= ND_IFINFO(ifp
);
2070 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2071 lck_mtx_lock(&ndi
->lock
);
2072 VERIFY(ndi
->nprefixes
> 0);
2074 lck_mtx_unlock(&ndi
->lock
);
2076 /* This must not be the last reference to the nd_prefix */
2077 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2078 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__
, pr
);
2083 * Don't call pfxlist_onlink_check() here because we are
2084 * holding the NDPR lock and this could cause a deadlock when
2085 * there are multiple threads executing pfxlist_onlink_check().
2091 struct nd_prefix
*new,
2092 struct nd_defrouter
*dr
, /* may be NULL */
2096 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
2098 struct ifnet
*ifp
= new->ndpr_ifp
;
2099 struct nd_prefix
*pr
;
2103 struct in6_addrlifetime lt6_tmp
;
2104 uint64_t timenow
= net_uptime();
2106 /* no need to lock "new" here, as it is local to the caller */
2107 NDPR_LOCK_ASSERT_NOTHELD(new);
2112 * Authenticity for NA consists authentication for
2113 * both IP header and IP datagrams, doesn't it ?
2115 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
2116 auth
= (m
->m_flags
& M_AUTHIPHDR
) && (m
->m_flags
& M_AUTHIPDGM
);
2120 if ((pr
= nd6_prefix_lookup(new, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
2122 * nd6_prefix_lookup() ensures that pr and new have the same
2123 * prefix on a same interface.
2127 * Update prefix information. Note that the on-link (L) bit
2128 * and the autonomous (A) bit should NOT be changed from 1
2131 lck_mtx_lock(nd6_mutex
);
2133 if (new->ndpr_raf_onlink
== 1)
2134 pr
->ndpr_raf_onlink
= 1;
2135 if (new->ndpr_raf_auto
== 1)
2136 pr
->ndpr_raf_auto
= 1;
2137 if (new->ndpr_raf_onlink
) {
2138 pr
->ndpr_vltime
= new->ndpr_vltime
;
2139 pr
->ndpr_pltime
= new->ndpr_pltime
;
2140 (void) in6_init_prefix_ltimes(pr
); /* XXX error case? */
2141 pr
->ndpr_lastupdate
= net_uptime();
2144 NDPR_ADDREF_LOCKED(pr
);
2145 if (new->ndpr_raf_onlink
&&
2146 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
2150 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2152 "prelist_update: failed to make "
2153 "the prefix %s/%d on-link on %s "
2155 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2156 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
2157 /* proceed anyway. XXX: is it correct? */
2162 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
) {
2169 lck_mtx_unlock(nd6_mutex
);
2173 if (new->ndpr_vltime
== 0)
2175 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
2178 bzero(&new->ndpr_addr
, sizeof (struct in6_addr
));
2180 error
= nd6_prelist_add(new, dr
, &pr
, FALSE
);
2181 if (error
!= 0 || pr
== NULL
) {
2182 nd6log((LOG_NOTICE
, "prelist_update: "
2183 "nd6_prelist_add failed for %s/%d on %s "
2184 "errno=%d, returnpr=0x%llx\n",
2185 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2186 new->ndpr_plen
, if_name(new->ndpr_ifp
),
2187 error
, (uint64_t)VM_KERNEL_ADDRPERM(pr
)));
2188 goto end
; /* we should just give up in this case. */
2193 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
2194 * Note that pr must be non NULL at this point.
2197 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
2198 if (!new->ndpr_raf_auto
)
2202 * 5.5.3 (b). the link-local prefix should have been ignored in
2206 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2207 if (new->ndpr_pltime
> new->ndpr_vltime
) {
2208 error
= EINVAL
; /* XXX: won't be used */
2213 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2214 * an address configured by stateless autoconfiguration already in the
2215 * list of addresses associated with the interface, and the Valid
2216 * Lifetime is not 0, form an address. We first check if we have
2217 * a matching prefix.
2219 ifnet_lock_shared(ifp
);
2220 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2221 struct in6_ifaddr
*ifa6
;
2222 u_int32_t remaininglifetime
;
2225 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2229 ifa6
= (struct in6_ifaddr
*)ifa
;
2232 * We only consider autoconfigured addresses as per RFC 4862.
2234 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
2239 * Spec is not clear here, but I believe we should concentrate
2240 * on unicast (i.e. not anycast) addresses.
2241 * XXX: other ia6_flags? detached or duplicated?
2243 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0) {
2248 * Ignore the address if it is not associated with a prefix
2249 * or is associated with a prefix that is different from this
2250 * one. (pr is never NULL here)
2252 if (ifa6
->ia6_ndpr
!= pr
) {
2257 if (ia6_match
== NULL
) { /* remember the first one */
2259 IFA_ADDREF_LOCKED(ifa
); /* for ia6_match */
2263 * An already autoconfigured address matched. Now that we
2264 * are sure there is at least one matched address, we can
2265 * proceed to 5.5.3. (e): update the lifetimes according to the
2266 * "two hours" rule and the privacy extension.
2268 #define TWOHOUR (120*60)
2270 /* retrieve time as uptime (last arg is 0) */
2271 in6ifa_getlifetime(ifa6
, <6_tmp
, 0);
2273 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
2274 remaininglifetime
= ND6_INFINITE_LIFETIME
;
2275 else if (timenow
- ifa6
->ia6_updatetime
> lt6_tmp
.ia6t_vltime
) {
2277 * The case of "invalid" address. We should usually
2278 * not see this case.
2280 remaininglifetime
= 0;
2282 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
2283 (timenow
- ifa6
->ia6_updatetime
);
2285 /* when not updating, keep the current stored lifetime. */
2286 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
2288 if (TWOHOUR
< new->ndpr_vltime
||
2289 remaininglifetime
< new->ndpr_vltime
) {
2290 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2291 } else if (remaininglifetime
<= TWOHOUR
) {
2293 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2297 * new->ndpr_vltime <= TWOHOUR &&
2298 * TWOHOUR < remaininglifetime
2300 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
2303 /* The 2 hour rule is not imposed for preferred lifetime. */
2304 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
2306 /* Special handling for lifetimes of temporary addresses. */
2307 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
2308 u_int32_t maxvltime
, maxpltime
;
2310 /* Constrain lifetimes to system limits. */
2311 if (lt6_tmp
.ia6t_vltime
> ip6_temp_valid_lifetime
)
2312 lt6_tmp
.ia6t_vltime
= ip6_temp_valid_lifetime
;
2313 if (lt6_tmp
.ia6t_pltime
> ip6_temp_preferred_lifetime
)
2314 lt6_tmp
.ia6t_pltime
=
2315 ip6_temp_preferred_lifetime
-
2319 * According to RFC 4941, section 3.3 (1), we only
2320 * update the lifetimes when they are in the maximum
2323 if (ip6_temp_valid_lifetime
>
2324 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2325 ip6_desync_factor
)) {
2326 maxvltime
= ip6_temp_valid_lifetime
-
2327 (timenow
- ifa6
->ia6_createtime
) -
2331 if (ip6_temp_preferred_lifetime
>
2332 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2333 ip6_desync_factor
)) {
2334 maxpltime
= ip6_temp_preferred_lifetime
-
2335 (timenow
- ifa6
->ia6_createtime
) -
2340 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
2341 lt6_tmp
.ia6t_vltime
> maxvltime
)
2342 lt6_tmp
.ia6t_vltime
= maxvltime
;
2344 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
2345 lt6_tmp
.ia6t_pltime
> maxpltime
)
2346 lt6_tmp
.ia6t_pltime
= maxpltime
;
2349 in6_init_address_ltimes(pr
, <6_tmp
);
2351 in6ifa_setlifetime(ifa6
, <6_tmp
);
2352 ifa6
->ia6_updatetime
= timenow
;
2355 ifnet_lock_done(ifp
);
2356 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
2358 * 5.5.3 (d) (continued)
2359 * No address matched and the valid lifetime is non-zero.
2360 * Create a new address.
2363 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
))
2366 * note that we should use pr (not new) for reference.
2368 IFA_LOCK(&ia6
->ia_ifa
);
2371 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2373 VERIFY(pr
->ndpr_addrcnt
!= 0);
2375 IFA_UNLOCK(&ia6
->ia_ifa
);
2379 * When a new public address is created as described
2380 * in RFC 4862, also create a new temporary address.
2383 * When an interface connects to a new link, a new
2384 * randomized interface identifier should be generated
2385 * immediately together with a new set of temporary
2386 * addresses. Thus, we specifiy 1 as the 2nd arg of
2389 if (ip6_use_tempaddr
) {
2391 if ((e
= in6_tmpifadd(ia6
, 1)) != 0) {
2392 nd6log((LOG_NOTICE
, "prelist_update: "
2393 "failed to create a temporary "
2394 "address, errno=%d\n",
2398 IFA_REMREF(&ia6
->ia_ifa
);
2402 * A newly added address might affect the status
2403 * of other addresses, so we check and update it.
2404 * XXX: what if address duplication happens?
2406 lck_mtx_lock(nd6_mutex
);
2407 pfxlist_onlink_check();
2408 lck_mtx_unlock(nd6_mutex
);
2415 if (ia6_match
!= NULL
)
2416 IFA_REMREF(&ia6_match
->ia_ifa
);
2421 * Neighbor Discover Default Router structure reference counting routines.
2423 static struct nd_defrouter
*
2426 struct nd_defrouter
*dr
;
2428 dr
= (how
== M_WAITOK
) ? zalloc(nddr_zone
) : zalloc_noblock(nddr_zone
);
2430 bzero(dr
, nddr_size
);
2431 lck_mtx_init(&dr
->nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2432 dr
->nddr_debug
|= IFD_ALLOC
;
2433 if (nddr_debug
!= 0) {
2434 dr
->nddr_debug
|= IFD_DEBUG
;
2435 dr
->nddr_trace
= nddr_trace
;
2442 nddr_free(struct nd_defrouter
*dr
)
2445 if (dr
->nddr_debug
& IFD_ATTACHED
) {
2446 panic("%s: attached nddr %p is being freed", __func__
, dr
);
2448 } else if (!(dr
->nddr_debug
& IFD_ALLOC
)) {
2449 panic("%s: nddr %p cannot be freed", __func__
, dr
);
2452 dr
->nddr_debug
&= ~IFD_ALLOC
;
2455 lck_mtx_destroy(&dr
->nddr_lock
, ifa_mtx_grp
);
2456 zfree(nddr_zone
, dr
);
2460 nddr_trace(struct nd_defrouter
*dr
, int refhold
)
2462 struct nd_defrouter_dbg
*dr_dbg
= (struct nd_defrouter_dbg
*)dr
;
2467 if (!(dr
->nddr_debug
& IFD_DEBUG
)) {
2468 panic("%s: nddr %p has no debug structure", __func__
, dr
);
2472 cnt
= &dr_dbg
->nddr_refhold_cnt
;
2473 tr
= dr_dbg
->nddr_refhold
;
2475 cnt
= &dr_dbg
->nddr_refrele_cnt
;
2476 tr
= dr_dbg
->nddr_refrele
;
2479 idx
= atomic_add_16_ov(cnt
, 1) % NDDR_TRACE_HIST_SIZE
;
2480 ctrace_record(&tr
[idx
]);
2484 nddr_addref(struct nd_defrouter
*nddr
, int locked
)
2488 NDDR_LOCK_SPIN(nddr
);
2490 NDDR_LOCK_ASSERT_HELD(nddr
);
2492 if (++nddr
->nddr_refcount
== 0) {
2493 panic("%s: nddr %p wraparound refcnt\n", __func__
, nddr
);
2495 } else if (nddr
->nddr_trace
!= NULL
) {
2496 (*nddr
->nddr_trace
)(nddr
, TRUE
);
2503 struct nd_defrouter
*
2504 nddr_remref(struct nd_defrouter
*nddr
, int locked
)
2508 NDDR_LOCK_SPIN(nddr
);
2510 NDDR_LOCK_ASSERT_HELD(nddr
);
2512 if (nddr
->nddr_refcount
== 0) {
2513 panic("%s: nddr %p negative refcnt\n", __func__
, nddr
);
2515 } else if (nddr
->nddr_trace
!= NULL
) {
2516 (*nddr
->nddr_trace
)(nddr
, FALSE
);
2519 if (--nddr
->nddr_refcount
== 0) {
2525 if (!locked
&& nddr
!= NULL
)
2532 nddr_getexpire(struct nd_defrouter
*dr
)
2534 struct timeval caltime
;
2537 if (dr
->expire
!= 0) {
2538 /* account for system time change */
2539 getmicrotime(&caltime
);
2541 dr
->base_calendartime
+=
2542 NET_CALCULATE_CLOCKSKEW(caltime
,
2543 dr
->base_calendartime
, net_uptime(), dr
->base_uptime
);
2545 expiry
= dr
->base_calendartime
+
2546 dr
->expire
- dr
->base_uptime
;
2554 * Neighbor Discover Prefix structure reference counting routines.
2556 static struct nd_prefix
*
2559 struct nd_prefix
*pr
;
2561 pr
= (how
== M_WAITOK
) ? zalloc(ndpr_zone
) : zalloc_noblock(ndpr_zone
);
2563 bzero(pr
, ndpr_size
);
2564 lck_mtx_init(&pr
->ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2565 RB_INIT(&pr
->ndpr_prproxy_sols
);
2566 pr
->ndpr_debug
|= IFD_ALLOC
;
2567 if (ndpr_debug
!= 0) {
2568 pr
->ndpr_debug
|= IFD_DEBUG
;
2569 pr
->ndpr_trace
= ndpr_trace
;
2576 ndpr_free(struct nd_prefix
*pr
)
2579 if (pr
->ndpr_debug
& IFD_ATTACHED
) {
2580 panic("%s: attached ndpr %p is being freed", __func__
, pr
);
2582 } else if (!(pr
->ndpr_debug
& IFD_ALLOC
)) {
2583 panic("%s: ndpr %p cannot be freed", __func__
, pr
);
2585 } else if (pr
->ndpr_rt
!= NULL
) {
2586 panic("%s: ndpr %p route %p not freed", __func__
, pr
,
2589 } else if (pr
->ndpr_prproxy_sols_cnt
!= 0) {
2590 panic("%s: ndpr %p non-zero solicitors count (%d)",
2591 __func__
, pr
, pr
->ndpr_prproxy_sols_cnt
);
2593 } else if (!RB_EMPTY(&pr
->ndpr_prproxy_sols
)) {
2594 panic("%s: ndpr %p non-empty solicitors tree", __func__
, pr
);
2597 pr
->ndpr_debug
&= ~IFD_ALLOC
;
2600 lck_mtx_destroy(&pr
->ndpr_lock
, ifa_mtx_grp
);
2601 zfree(ndpr_zone
, pr
);
2605 ndpr_trace(struct nd_prefix
*pr
, int refhold
)
2607 struct nd_prefix_dbg
*pr_dbg
= (struct nd_prefix_dbg
*)pr
;
2612 if (!(pr
->ndpr_debug
& IFD_DEBUG
)) {
2613 panic("%s: ndpr %p has no debug structure", __func__
, pr
);
2617 cnt
= &pr_dbg
->ndpr_refhold_cnt
;
2618 tr
= pr_dbg
->ndpr_refhold
;
2620 cnt
= &pr_dbg
->ndpr_refrele_cnt
;
2621 tr
= pr_dbg
->ndpr_refrele
;
2624 idx
= atomic_add_16_ov(cnt
, 1) % NDPR_TRACE_HIST_SIZE
;
2625 ctrace_record(&tr
[idx
]);
2629 ndpr_addref(struct nd_prefix
*ndpr
, int locked
)
2632 NDPR_LOCK_SPIN(ndpr
);
2634 NDPR_LOCK_ASSERT_HELD(ndpr
);
2636 if (++ndpr
->ndpr_refcount
== 0) {
2637 panic("%s: ndpr %p wraparound refcnt\n", __func__
, ndpr
);
2639 } else if (ndpr
->ndpr_trace
!= NULL
) {
2640 (*ndpr
->ndpr_trace
)(ndpr
, TRUE
);
2648 ndpr_remref(struct nd_prefix
*ndpr
, int locked
)
2651 NDPR_LOCK_SPIN(ndpr
);
2653 NDPR_LOCK_ASSERT_HELD(ndpr
);
2655 if (ndpr
->ndpr_refcount
== 0) {
2656 panic("%s: ndpr %p negative refcnt\n", __func__
, ndpr
);
2658 } else if (ndpr
->ndpr_trace
!= NULL
) {
2659 (*ndpr
->ndpr_trace
)(ndpr
, FALSE
);
2662 if (--ndpr
->ndpr_refcount
== 0) {
2663 if (ndpr
->ndpr_addrcnt
!= 0) {
2664 panic("%s: freeing ndpr %p with outstanding address "
2665 "reference (%d)", __func__
, ndpr
,
2666 ndpr
->ndpr_addrcnt
);
2674 if (!locked
&& ndpr
!= NULL
)
2681 ndpr_getexpire(struct nd_prefix
*pr
)
2683 struct timeval caltime
;
2686 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_vltime
!= ND6_INFINITE_LIFETIME
) {
2687 /* account for system time change */
2688 getmicrotime(&caltime
);
2690 pr
->ndpr_base_calendartime
+=
2691 NET_CALCULATE_CLOCKSKEW(caltime
,
2692 pr
->ndpr_base_calendartime
, net_uptime(),
2693 pr
->ndpr_base_uptime
);
2695 expiry
= pr
->ndpr_base_calendartime
+
2696 pr
->ndpr_expire
- pr
->ndpr_base_uptime
;
2704 * A supplement function used in the on-link detection below;
2705 * detect if a given prefix has a (probably) reachable advertising router.
2706 * XXX: lengthy function name...
2708 * Callers *must* increase the reference count of nd_prefix.
2710 static struct nd_pfxrouter
*
2711 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
2713 struct nd_pfxrouter
*pfxrtr
;
2715 struct llinfo_nd6
*ln
;
2717 struct in6_addr rtaddr
;
2720 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2721 NDPR_LOCK_ASSERT_HELD(pr
);
2723 genid
= pr
->ndpr_genid
;
2724 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2726 ifp
= pfxrtr
->router
->ifp
;
2727 if (pfxrtr
->router
->stateflags
& NDDRF_MAPPED
)
2728 rtaddr
= pfxrtr
->router
->rtaddr_mapped
;
2730 rtaddr
= pfxrtr
->router
->rtaddr
;
2733 lck_mtx_unlock(nd6_mutex
);
2734 /* Callee returns a locked route upon success */
2735 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
2736 RT_LOCK_ASSERT_HELD(rt
);
2737 if ((ln
= rt
->rt_llinfo
) != NULL
&&
2738 ND6_IS_LLINFO_PROBREACH(ln
)) {
2739 RT_REMREF_LOCKED(rt
);
2741 lck_mtx_lock(nd6_mutex
);
2745 RT_REMREF_LOCKED(rt
);
2748 lck_mtx_lock(nd6_mutex
);
2750 if (pr
->ndpr_genid
!= genid
) {
2751 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2752 genid
= pr
->ndpr_genid
;
2754 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
);
2756 NDPR_LOCK_ASSERT_HELD(pr
);
2763 * Check if each prefix in the prefix list has at least one available router
2764 * that advertised the prefix (a router is "available" if its neighbor cache
2765 * entry is reachable or probably reachable).
2766 * If the check fails, the prefix may be off-link, because, for example,
2767 * we have moved from the network but the lifetime of the prefix has not
2768 * expired yet. So we should not use the prefix if there is another prefix
2769 * that has an available router.
2770 * But, if there is no prefix that has an available router, we still regards
2771 * all the prefixes as on-link. This is because we can't tell if all the
2772 * routers are simply dead or if we really moved from the network and there
2773 * is no router around us.
2776 pfxlist_onlink_check(void)
2778 struct nd_prefix
*pr
, *prclear
;
2779 struct in6_ifaddr
*ifa
;
2780 struct nd_defrouter
*dr
;
2781 struct nd_pfxrouter
*pfxrtr
= NULL
;
2782 int err
, i
, found
= 0;
2783 struct ifaddr
**ifap
= NULL
;
2784 struct nd_prefix
*ndpr
;
2786 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2788 while (nd_prefix_busy
) {
2789 nd_prefix_waiters
++;
2790 msleep(nd_prefix_waitchan
, nd6_mutex
, (PZERO
-1),
2792 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2794 nd_prefix_busy
= TRUE
;
2797 * Check if there is a prefix that has a reachable advertising
2800 pr
= nd_prefix
.lh_first
;
2803 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
) {
2808 NDPR_ADDREF_LOCKED(pr
);
2809 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
) &&
2810 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
2811 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2817 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2821 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
2822 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
2823 * stop us from checking the same prefix twice.
2825 pr
= nd_prefix
.lh_first
;
2827 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2829 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2830 NDPR_UNLOCK(prclear
);
2833 * If we have no such prefix, check whether we still have a router
2834 * that does not advertise any prefixes.
2837 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
2838 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2839 struct nd_prefix
*pr0
;
2841 for (pr0
= nd_prefix
.lh_first
; pr0
;
2842 pr0
= pr0
->ndpr_next
) {
2844 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
) {
2854 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && pfxrtr
== NULL
)) {
2856 * There is at least one prefix that has a reachable router,
2857 * or at least a router which probably does not advertise
2858 * any prefixes. The latter would be the case when we move
2859 * to a new link where we have a router that does not provide
2860 * prefixes and we configure an address by hand.
2861 * Detach prefixes which have no reachable advertising
2862 * router, and attach other prefixes.
2864 pr
= nd_prefix
.lh_first
;
2868 * We aren't interested prefixes already processed,
2869 * nor in prefixes without the L bit
2870 * set nor in static prefixes
2872 if (pr
->ndpr_raf_onlink
== 0 ||
2873 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2874 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2879 NDPR_ADDREF_LOCKED(pr
);
2880 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2881 find_pfxlist_reachable_router(pr
) == NULL
&&
2882 (pr
->ndpr_debug
& IFD_ATTACHED
))
2883 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
2884 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2885 find_pfxlist_reachable_router(pr
) != NULL
&&
2886 (pr
->ndpr_debug
& IFD_ATTACHED
))
2887 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2888 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2892 * Since find_pfxlist_reachable_router() drops the
2893 * nd6_mutex, we have to start over, but the
2894 * NDPRF_PROCESSED_ONLINK flag will stop us from
2895 * checking the same prefix twice.
2897 pr
= nd_prefix
.lh_first
;
2900 /* there is no prefix that has a reachable router */
2901 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2903 if (pr
->ndpr_raf_onlink
== 0 ||
2904 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2908 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
2909 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2913 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2915 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2916 NDPR_UNLOCK(prclear
);
2919 * Remove each interface route associated with a (just) detached
2920 * prefix, and reinstall the interface route for a (just) attached
2921 * prefix. Note that all attempt of reinstallation does not
2922 * necessarily success, when a same prefix is shared among multiple
2923 * interfaces. Such cases will be handled in nd6_prefix_onlink,
2924 * so we don't have to care about them.
2926 pr
= nd_prefix
.lh_first
;
2931 if (pr
->ndpr_raf_onlink
== 0 ||
2932 pr
->ndpr_stateflags
& NDPRF_STATIC
||
2933 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2934 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
2939 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2940 NDPR_ADDREF_LOCKED(pr
);
2941 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2942 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
2944 lck_mtx_unlock(nd6_mutex
);
2945 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2947 "pfxlist_onlink_check: failed to "
2948 "make %s/%d offlink, errno=%d\n",
2949 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2952 lck_mtx_lock(nd6_mutex
);
2954 pr
= nd_prefix
.lh_first
;
2957 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2958 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
2959 pr
->ndpr_raf_onlink
) {
2961 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2963 "pfxlist_onlink_check: failed to "
2964 "make %s/%d offlink, errno=%d\n",
2965 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2969 pr
= nd_prefix
.lh_first
;
2977 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2979 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2980 NDPR_UNLOCK(prclear
);
2982 VERIFY(nd_prefix_busy
);
2983 nd_prefix_busy
= FALSE
;
2984 if (nd_prefix_waiters
> 0) {
2985 nd_prefix_waiters
= 0;
2986 wakeup(nd_prefix_waitchan
);
2990 * Changes on the prefix status might affect address status as well.
2991 * Make sure that all addresses derived from an attached prefix are
2992 * attached, and that all addresses derived from a detached prefix are
2993 * detached. Note, however, that a manually configured address should
2994 * always be attached.
2995 * The precise detection logic is same as the one for prefixes.
2997 * ifnet_get_address_list_family_internal() may fail due to memory
2998 * pressure, but we will eventually be called again when we receive
2999 * another NA, RA, or when the link status changes.
3001 err
= ifnet_get_address_list_family_internal(NULL
, &ifap
, AF_INET6
, 0,
3003 if (err
!= 0 || ifap
== NULL
) {
3004 nd6log((LOG_ERR
, "%s: ifnet_get_address_list_family_internal "
3005 "failed", __func__
));
3008 for (i
= 0; ifap
[i
]; i
++) {
3009 ifa
= ifatoia6(ifap
[i
]);
3010 IFA_LOCK(&ifa
->ia_ifa
);
3011 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3012 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3013 IFA_UNLOCK(&ifa
->ia_ifa
);
3016 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3018 * This can happen when we first configure the address
3019 * (i.e. the address exists, but the prefix does not).
3020 * XXX: complicated relationships...
3022 IFA_UNLOCK(&ifa
->ia_ifa
);
3025 IFA_UNLOCK(&ifa
->ia_ifa
);
3028 NDPR_ADDREF_LOCKED(ndpr
);
3029 if (find_pfxlist_reachable_router(ndpr
)) {
3030 if (NDPR_REMREF_LOCKED(ndpr
) == NULL
) {
3042 for (i
= 0; ifap
[i
]; i
++) {
3043 ifa
= ifatoia6(ifap
[i
]);
3044 IFA_LOCK(&ifa
->ia_ifa
);
3045 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3046 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3047 IFA_UNLOCK(&ifa
->ia_ifa
);
3050 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3051 /* XXX: see above. */
3052 IFA_UNLOCK(&ifa
->ia_ifa
);
3055 IFA_UNLOCK(&ifa
->ia_ifa
);
3057 NDPR_ADDREF_LOCKED(ndpr
);
3058 if (find_pfxlist_reachable_router(ndpr
)) {
3060 IFA_LOCK(&ifa
->ia_ifa
);
3061 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3062 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3063 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3064 IFA_UNLOCK(&ifa
->ia_ifa
);
3065 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3067 IFA_UNLOCK(&ifa
->ia_ifa
);
3071 IFA_LOCK(&ifa
->ia_ifa
);
3072 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
3073 IFA_UNLOCK(&ifa
->ia_ifa
);
3078 for (i
= 0; ifap
[i
]; i
++) {
3079 ifa
= ifatoia6(ifap
[i
]);
3080 IFA_LOCK(&ifa
->ia_ifa
);
3081 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3082 IFA_UNLOCK(&ifa
->ia_ifa
);
3085 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3086 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3087 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3088 IFA_UNLOCK(&ifa
->ia_ifa
);
3089 /* Do we need a delay in this case? */
3090 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3092 IFA_UNLOCK(&ifa
->ia_ifa
);
3096 ifnet_free_address_list(ifap
);
3099 static struct nd_prefix
*
3100 nd6_prefix_equal_lookup(struct nd_prefix
*pr
, boolean_t primary_only
)
3102 struct nd_prefix
*opr
;
3104 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3106 for (opr
= nd_prefix
.lh_first
; opr
; opr
= opr
->ndpr_next
) {
3111 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3115 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
3116 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
3117 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
) &&
3119 !(opr
->ndpr_stateflags
& NDPRF_IFSCOPE
))) {
3120 NDPR_ADDREF_LOCKED(opr
);
3130 * Synchronize the interface routes of similar prefixes on different
3131 * interfaces; the one using the default interface would be (re)installed
3132 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3135 nd6_prefix_sync(struct ifnet
*ifp
)
3137 struct nd_prefix
*pr
, *opr
;
3140 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3145 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3147 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
3151 if (pr
->ndpr_ifp
== ifp
&&
3152 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) &&
3153 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3163 /* Remove conflicting entries */
3164 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3166 lck_mtx_unlock(nd6_mutex
);
3167 err
= nd6_prefix_offlink(opr
);
3168 lck_mtx_lock(nd6_mutex
);
3171 "%s: failed to make %s/%d offlink on %s, "
3172 "errno=%d\n", __func__
,
3173 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3174 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3178 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3179 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3180 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
)));
3183 lck_mtx_unlock(nd6_mutex
);
3184 err
= nd6_prefix_offlink(pr
);
3185 lck_mtx_lock(nd6_mutex
);
3188 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3189 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3190 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3193 /* Add the entries back */
3195 err
= nd6_prefix_onlink_scoped(opr
, opr
->ndpr_ifp
->if_index
);
3198 "%s: failed to make %s/%d scoped onlink on %s, "
3199 "errno=%d\n", __func__
,
3200 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3201 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3205 err
= nd6_prefix_onlink_scoped(pr
, IFSCOPE_NONE
);
3208 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3209 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3210 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3215 "%s: error promoting %s/%d to %s from %s\n",
3216 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3217 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3218 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3221 "%s: %s/%d promoted, previously on %s\n",
3222 if_name(pr
->ndpr_ifp
),
3223 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
,
3224 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3232 nd6_prefix_onlink_common(struct nd_prefix
*pr
, boolean_t force_scoped
,
3233 unsigned int ifscope
)
3236 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3237 struct sockaddr_in6 mask6
, prefix
;
3238 struct nd_prefix
*opr
;
3240 int error
= 0, prproxy
= 0;
3241 struct rtentry
*rt
= NULL
;
3243 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3247 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3249 "%s: %s/%d on %s scoped=%d is already on-link\n",
3250 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3251 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3252 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3259 * Add the interface route associated with the prefix. Before
3260 * installing the route, check if there's the same prefix on another
3261 * interface, and the prefix has already installed the interface route.
3263 opr
= nd6_prefix_equal_lookup(pr
, FALSE
);
3267 if (!force_scoped
) {
3269 * If a primary/non-scoped interface route already exists,
3270 * install the new one as a scoped entry. If the existing
3271 * interface route is scoped, install new as non-scoped.
3273 ifscope
= (opr
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
3274 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3277 else if (ifscope
!= IFSCOPE_NONE
)
3278 ifscope
= IFSCOPE_NONE
;
3282 * We prefer link-local addresses as the associated interface address.
3284 /* search for a link-local addr */
3285 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
3286 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3288 struct in6_ifaddr
*ia6
;
3289 ifnet_lock_shared(ifp
);
3290 IFP_TO_IA6(ifp
, ia6
);
3291 ifnet_lock_done(ifp
);
3294 /* should we care about ia6_flags? */
3299 * This can still happen, when, for example, we receive an RA
3300 * containing a prefix with the L bit set and the A bit clear,
3301 * after removing all IPv6 addresses on the receiving
3302 * interface. This should, of course, be rare though.
3305 "nd6_prefix_onlink: failed to find any ifaddr"
3306 " to add route for a prefix(%s/%d) on %s\n",
3307 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3308 pr
->ndpr_plen
, if_name(ifp
)));
3314 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3315 * ifa->ifa_rtrequest = nd6_rtrequest;
3317 bzero(&mask6
, sizeof (mask6
));
3318 mask6
.sin6_len
= sizeof (mask6
);
3319 mask6
.sin6_addr
= pr
->ndpr_mask
;
3320 prefix
= pr
->ndpr_prefix
;
3321 if ((rt
= pr
->ndpr_rt
) != NULL
)
3323 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3327 rtflags
= ifa
->ifa_flags
| RTF_CLONING
| RTF_UP
;
3329 if (nd6_need_cache(ifp
)) {
3330 /* explicitly set in case ifa_flags does not set the flag. */
3331 rtflags
|= RTF_CLONING
;
3334 * explicitly clear the cloning bit in case ifa_flags sets it.
3336 rtflags
&= ~RTF_CLONING
;
3339 lck_mtx_unlock(nd6_mutex
);
3346 error
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&prefix
,
3347 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
,
3351 * Serialize the setting of NDPRF_PRPROXY.
3353 lck_mtx_lock(&proxy6_lock
);
3357 nd6_rtmsg(RTM_ADD
, rt
);
3362 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a"
3363 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx,"
3364 " scoped=%d, errno = %d\n",
3365 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3366 pr
->ndpr_plen
, if_name(ifp
),
3367 ip6_sprintf(&((struct sockaddr_in6
*)
3368 (void *)ifa
->ifa_addr
)->sin6_addr
),
3369 ip6_sprintf(&mask6
.sin6_addr
), rtflags
,
3370 (ifscope
!= IFSCOPE_NONE
), error
));
3372 NDPR_LOCK_ASSERT_HELD(pr
);
3374 pr
->ndpr_stateflags
&= ~(NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3377 * TODO: If the prefix route exists, we should really find it and
3378 * refer the prefix to it; otherwise ndpr_rt is NULL.
3380 if (!(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) &&
3381 (rt
!= NULL
|| error
== EEXIST
)) {
3382 struct nd_ifinfo
*ndi
= NULL
;
3384 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3385 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3387 ndi
= ND_IFINFO(ifp
);
3388 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3389 lck_mtx_lock(&ndi
->lock
);
3391 pr
->ndpr_rt
= rt
; /* keep reference from rtrequest */
3392 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
3393 if (ifscope
!= IFSCOPE_NONE
) {
3394 pr
->ndpr_stateflags
|= NDPRF_IFSCOPE
;
3395 } else if ((rtflags
& RTF_CLONING
) &&
3396 (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) &&
3397 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3399 * At present, in order for the prefix to be eligible
3400 * as a proxying/proxied prefix, we require that the
3401 * prefix route entry be marked as a cloning route with
3402 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3403 * true for the interface type, hence the test for
3404 * RTF_CLONING above.
3406 pr
->ndpr_stateflags
|= NDPRF_PRPROXY
;
3409 lck_mtx_unlock(&ndi
->lock
);
3410 } else if (rt
!= NULL
&& pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
3413 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3414 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3420 * If this is an upstream prefix, find the downstream ones (if any)
3421 * and re-configure their prefix routes accordingly. Otherwise,
3422 * this could be potentially be a downstream prefix, and so find the
3423 * upstream prefix, if any.
3425 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3427 NDPR_REMREF(pr
); /* release reference for this routine */
3428 lck_mtx_unlock(&proxy6_lock
);
3430 lck_mtx_lock(nd6_mutex
);
3436 nd6_prefix_onlink(struct nd_prefix
*pr
)
3438 return (nd6_prefix_onlink_common(pr
, FALSE
, IFSCOPE_NONE
));
3442 nd6_prefix_onlink_scoped(struct nd_prefix
*pr
, unsigned int ifscope
)
3444 return (nd6_prefix_onlink_common(pr
, TRUE
, ifscope
));
3448 nd6_prefix_offlink(struct nd_prefix
*pr
)
3450 int plen
, error
= 0, prproxy
;
3451 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3452 struct sockaddr_in6 sa6
, mask6
, prefix
;
3453 struct rtentry
*rt
= NULL
, *ndpr_rt
= NULL
;
3454 unsigned int ifscope
;
3456 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3460 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3462 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3463 "off-link\n", ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3464 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3465 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0));
3470 bzero(&sa6
, sizeof (sa6
));
3471 sa6
.sin6_family
= AF_INET6
;
3472 sa6
.sin6_len
= sizeof (sa6
);
3473 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
,
3474 sizeof (struct in6_addr
));
3475 bzero(&mask6
, sizeof (mask6
));
3476 mask6
.sin6_family
= AF_INET6
;
3477 mask6
.sin6_len
= sizeof (sa6
);
3478 bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof (struct in6_addr
));
3479 prefix
= pr
->ndpr_prefix
;
3480 plen
= pr
->ndpr_plen
;
3481 if ((ndpr_rt
= pr
->ndpr_rt
) != NULL
)
3483 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3486 ifscope
= (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ?
3487 ifp
->if_index
: IFSCOPE_NONE
;
3489 error
= rtrequest_scoped(RTM_DELETE
, (struct sockaddr
*)&sa6
,
3490 NULL
, (struct sockaddr
*)&mask6
, 0, &rt
, ifscope
);
3493 /* report the route deletion to the routing socket. */
3495 nd6_rtmsg(RTM_DELETE
, rt
);
3501 "nd6_prefix_offlink: failed to delete route: "
3502 "%s/%d on %s, scoped %d, (errno = %d)\n",
3503 ip6_sprintf(&sa6
.sin6_addr
), plen
, if_name(ifp
),
3504 (ifscope
!= IFSCOPE_NONE
), error
));
3507 if (ndpr_rt
!= NULL
)
3510 lck_mtx_lock(&proxy6_lock
);
3513 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3514 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3515 pr
->ndpr_stateflags
&= ~(NDPRF_ONLINK
| NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3516 if (pr
->ndpr_prproxy_sols_cnt
> 0) {
3518 nd6_prproxy_sols_reap(pr
);
3519 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3520 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3525 * If this was an upstream prefix, find the downstream ones and do
3526 * some cleanups. If this was a downstream prefix, the prefix route
3527 * has been removed from the routing table above, but there may be
3528 * other tasks to perform.
3530 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3532 NDPR_REMREF(pr
); /* release reference for this routine */
3533 lck_mtx_unlock(&proxy6_lock
);
3538 static struct in6_ifaddr
*
3539 in6_pfx_newpersistaddr(struct nd_prefix
*pr
, int mcast
, int *errorp
)
3541 struct in6_ifaddr
*ia6
= NULL
;
3542 struct ifnet
*ifp
= NULL
;
3543 struct nd_ifinfo
*ndi
= NULL
;
3544 struct in6_addr mask
;
3545 struct in6_aliasreq ifra
;
3546 int error
, ifaupdate
, iidlen
, notcga
;
3549 VERIFY(errorp
!= NULL
);
3557 * Prefix Length check:
3558 * If the sum of the prefix length and interface identifier
3559 * length does not equal 128 bits, the Prefix Information
3560 * option MUST be ignored. The length of the interface
3561 * identifier is defined in a separate link-type specific
3564 iidlen
= in6_if2idlen(ifp
);
3566 error
= EADDRNOTAVAIL
;
3567 /* this should not happen, so we always log it. */
3568 log(LOG_ERR
, "%s: IID length undefined (%s)\n",
3569 __func__
, if_name(ifp
));
3571 } else if (iidlen
!= 64) {
3572 error
= EADDRNOTAVAIL
;
3574 * stateless autoconfiguration not yet well-defined for IID
3575 * lengths other than 64 octets. Just give up for now.
3577 nd6log((LOG_INFO
, "%s: IID length not 64 octets (%s)\n",
3578 __func__
, if_name(ifp
)));
3582 if (iidlen
+ pr
->ndpr_plen
!= 128) {
3583 error
= EADDRNOTAVAIL
;
3585 "%s: invalid prefix length %d for %s, ignored\n",
3586 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
3590 bzero(&ifra
, sizeof (ifra
));
3591 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3592 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
3593 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
3596 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
,
3597 sizeof (ifra
.ifra_addr
.sin6_addr
));
3598 in6_len2mask(&mask
, pr
->ndpr_plen
);
3599 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
3600 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
3601 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
3602 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
3604 ndi
= ND_IFINFO(ifp
);
3605 VERIFY(ndi
->initialized
);
3606 lck_mtx_lock(&ndi
->lock
);
3608 notcga
= nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
||
3609 (ndi
->flags
& ND6_IFF_INSECURE
) != 0;
3611 lck_mtx_unlock(&ndi
->lock
);
3615 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
3617 error
= EADDRNOTAVAIL
;
3618 nd6log((LOG_INFO
, "%s: no link-local address (%s)\n",
3619 __func__
, if_name(ifp
)));
3623 IFA_LOCK(&ia6
->ia_ifa
);
3624 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
3625 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
3626 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
3627 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
3628 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3629 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
3630 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3631 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
3632 IFA_UNLOCK(&ia6
->ia_ifa
);
3633 IFA_REMREF(&ia6
->ia_ifa
);
3636 in6_cga_node_lock();
3637 struct in6_cga_prepare local_cga_prepare
;
3639 if (ndi
->cga_initialized
) {
3640 bcopy(&(ndi
->local_cga_modifier
),
3641 &(local_cga_prepare
.cga_modifier
),
3642 sizeof(local_cga_prepare
.cga_modifier
));
3643 error
= in6_cga_generate(&local_cga_prepare
, 0,
3644 &ifra
.ifra_addr
.sin6_addr
);
3646 error
= in6_cga_generate(NULL
, 0,
3647 &ifra
.ifra_addr
.sin6_addr
);
3649 in6_cga_node_unlock();
3651 ifra
.ifra_flags
|= IN6_IFF_SECURED
;
3653 nd6log((LOG_ERR
, "%s: no CGA available (%s)\n",
3654 __func__
, if_name(ifp
)));
3659 VERIFY(ia6
== NULL
);
3661 /* new prefix mask. */
3662 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
3663 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
3664 bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
,
3665 sizeof (ifra
.ifra_prefixmask
.sin6_addr
));
3668 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
3669 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
3672 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
3675 * Make sure that we do not have this address already. This should
3676 * usually not happen, but we can still see this case, e.g., if we
3677 * have manually configured the exact address to be configured.
3679 if ((ia6
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
))
3681 error
= EADDRNOTAVAIL
;
3682 IFA_REMREF(&ia6
->ia_ifa
);
3685 /* this should be rare enough to make an explicit log */
3686 log(LOG_INFO
, "%s: %s is already configured!\n",
3687 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
3692 * Allocate ifaddr structure, link into chain, etc.
3693 * If we are going to create a new address upon receiving a multicasted
3694 * RA, we need to impose a random delay before starting DAD.
3695 * [RFC 4862, Section 5.4.2]
3697 ifaupdate
= IN6_IFAUPDATE_NOWAIT
;
3699 ifaupdate
|= IN6_IFAUPDATE_DADDELAY
;
3700 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &ia6
);
3703 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
3704 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
),
3705 if_name(ifp
), error
));
3706 error
= EADDRNOTAVAIL
;
3710 VERIFY(ia6
!= NULL
);
3711 in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia6
, NULL
);
3722 #define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
3725 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
)
3727 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
3728 struct in6_ifaddr
*ia
, *newia
;
3729 struct in6_aliasreq ifra
;
3730 int i
, error
, ifaupdate
;
3731 int trylimit
= 3; /* XXX: adhoc value */
3732 u_int32_t randid
[2];
3733 time_t vltime0
, pltime0
;
3734 uint64_t timenow
= net_uptime();
3735 struct in6_addr addr
;
3736 struct nd_prefix
*ndpr
;
3738 bzero(&ifra
, sizeof (ifra
));
3739 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3740 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3741 ifra
.ifra_addr
= ia0
->ia_addr
;
3742 /* copy prefix mask */
3743 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
3744 /* clear the old IFID */
3745 for (i
= 0; i
< 4; i
++) {
3746 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
]
3747 &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
3749 addr
= ia0
->ia_addr
.sin6_addr
;
3750 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3753 in6_iid_mktmp(ifp
, (u_int8_t
*)randid
,
3754 (const u_int8_t
*)&addr
.s6_addr
[8], forcegen
);
3756 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3757 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
3758 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3759 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
3762 * in6_iid_mktmp() quite likely provided a unique interface ID.
3763 * However, we may still have a chance to see collision, because
3764 * there may be a time lag between generation of the ID and generation
3765 * of the address. So, we'll do one more sanity check.
3767 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
)) != NULL
) {
3768 IFA_REMREF(&ia
->ia_ifa
);
3769 if (trylimit
-- == 0) {
3770 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find "
3771 "a unique random IFID\n"));
3779 * The Valid Lifetime is the lower of the Valid Lifetime of the
3780 * public address or TEMP_VALID_LIFETIME.
3781 * The Preferred Lifetime is the lower of the Preferred Lifetime
3782 * of the public address or TEMP_PREFERRED_LIFETIME -
3785 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3786 if (ia0
->ia6_lifetime
.ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3787 vltime0
= IFA6_IS_INVALID(ia0
, timenow
) ? 0 :
3788 (ia0
->ia6_lifetime
.ia6ti_vltime
-
3789 (timenow
- ia0
->ia6_updatetime
));
3790 if (vltime0
> ip6_temp_valid_lifetime
)
3791 vltime0
= ip6_temp_valid_lifetime
;
3793 vltime0
= ip6_temp_valid_lifetime
;
3795 if (ia0
->ia6_lifetime
.ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3796 pltime0
= IFA6_IS_DEPRECATED(ia0
, timenow
) ? 0 :
3797 (ia0
->ia6_lifetime
.ia6ti_pltime
-
3798 (timenow
- ia0
->ia6_updatetime
));
3799 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
)
3800 pltime0
= ip6_temp_preferred_lifetime
-
3803 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
3805 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
3806 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
3807 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3809 * A temporary address is created only if this calculated Preferred
3810 * Lifetime is greater than REGEN_ADVANCE time units.
3812 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
)
3815 /* XXX: scope zone ID? */
3817 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
3819 /* allocate ifaddr structure, link into chain, etc. */
3820 ifaupdate
= IN6_IFAUPDATE_NOWAIT
| IN6_IFAUPDATE_DADDELAY
;
3821 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &newia
);
3823 nd6log((LOG_ERR
, "in6_tmpifadd: failed to add address.\n"));
3826 VERIFY(newia
!= NULL
);
3828 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3829 ndpr
= ia0
->ia6_ndpr
;
3832 * We lost the race with another thread that has purged
3833 * ia0 address; in this case, purge the tmp addr as well.
3835 nd6log((LOG_ERR
, "in6_tmpifadd: no public address\n"));
3836 VERIFY(!(ia0
->ia6_flags
& IN6_IFF_AUTOCONF
));
3837 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3838 in6_purgeaddr(&newia
->ia_ifa
);
3839 IFA_REMREF(&newia
->ia_ifa
);
3840 return (EADDRNOTAVAIL
);
3842 NDPR_ADDREF(ndpr
); /* for us */
3843 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3844 IFA_LOCK(&newia
->ia_ifa
);
3845 if (newia
->ia6_ndpr
!= NULL
) {
3846 NDPR_LOCK(newia
->ia6_ndpr
);
3847 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3848 newia
->ia6_ndpr
->ndpr_addrcnt
--;
3849 NDPR_UNLOCK(newia
->ia6_ndpr
);
3850 NDPR_REMREF(newia
->ia6_ndpr
); /* release addr reference */
3852 newia
->ia6_ndpr
= ndpr
;
3853 NDPR_LOCK(newia
->ia6_ndpr
);
3854 newia
->ia6_ndpr
->ndpr_addrcnt
++;
3855 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3856 NDPR_ADDREF_LOCKED(newia
->ia6_ndpr
); /* for addr reference */
3857 NDPR_UNLOCK(newia
->ia6_ndpr
);
3858 IFA_UNLOCK(&newia
->ia_ifa
);
3860 * A newly added address might affect the status of other addresses.
3861 * XXX: when the temporary address is generated with a new public
3862 * address, the onlink check is redundant. However, it would be safe
3863 * to do the check explicitly everywhere a new address is generated,
3864 * and, in fact, we surely need the check when we create a new
3865 * temporary address due to deprecation of an old temporary address.
3867 lck_mtx_lock(nd6_mutex
);
3868 pfxlist_onlink_check();
3869 lck_mtx_unlock(nd6_mutex
);
3870 IFA_REMREF(&newia
->ia_ifa
);
3872 /* remove our reference */
3880 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
3882 struct timeval caltime
;
3883 u_int64_t timenow
= net_uptime();
3885 NDPR_LOCK_ASSERT_HELD(ndpr
);
3887 getmicrotime(&caltime
);
3888 ndpr
->ndpr_base_calendartime
= caltime
.tv_sec
;
3889 ndpr
->ndpr_base_uptime
= timenow
;
3891 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
3892 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
3893 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime"
3894 "(%d) is greater than valid lifetime(%d)\n",
3895 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
));
3898 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
3899 ndpr
->ndpr_preferred
= 0;
3901 ndpr
->ndpr_preferred
= timenow
+ ndpr
->ndpr_pltime
;
3902 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
3903 ndpr
->ndpr_expire
= 0;
3905 ndpr
->ndpr_expire
= timenow
+ ndpr
->ndpr_vltime
;
3911 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
3914 uint64_t timenow
= net_uptime();
3916 /* Valid lifetime must not be updated unless explicitly specified. */
3917 /* init ia6t_expire */
3918 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
3919 lt6
->ia6t_expire
= 0;
3921 lt6
->ia6t_expire
= timenow
;
3922 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
3925 /* init ia6t_preferred */
3926 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
) {
3927 lt6
->ia6t_preferred
= 0;
3929 lt6
->ia6t_preferred
= timenow
;
3930 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
3935 * Delete all the routing table entries that use the specified gateway.
3936 * XXX: this function causes search through all entries of routing table, so
3937 * it shouldn't be called when acting as a router.
3941 struct in6_addr
*gateway
,
3944 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
3946 /* We'll care only link-local addresses */
3947 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
3950 lck_mtx_lock(rnh_lock
);
3951 /* XXX: hack for KAME's link-local address kludge */
3952 gateway
->s6_addr16
[1] = htons(ifp
->if_index
);
3954 rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
);
3955 lck_mtx_unlock(rnh_lock
);
3960 struct radix_node
*rn
,
3963 struct rtentry
*rt
= (struct rtentry
*)rn
;
3964 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
3966 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
3969 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
) {
3974 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) {
3979 * Do not delete a static route.
3980 * XXX: this seems to be a bit ad-hoc. Should we consider the
3981 * 'cloned' bit instead?
3983 if ((rt
->rt_flags
& RTF_STATIC
) != 0) {
3988 * We delete only host route. This means, in particular, we don't
3989 * delete default route.
3991 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
3997 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
3998 * rnh_lock here prevents another thread from calling rt_setgate()
4002 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4003 rt_mask(rt
), rt
->rt_flags
, 0));
4007 nd6_setdefaultiface(
4011 ifnet_t def_ifp
= NULL
;
4013 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4015 ifnet_head_lock_shared();
4016 if (ifindex
< 0 || if_index
< ifindex
) {
4020 def_ifp
= ifindex2ifnet
[ifindex
];
4023 lck_mtx_lock(nd6_mutex
);
4024 if (nd6_defifindex
!= ifindex
) {
4025 struct ifnet
*odef_ifp
= nd6_defifp
;
4027 nd6_defifindex
= ifindex
;
4028 if (nd6_defifindex
> 0)
4029 nd6_defifp
= def_ifp
;
4033 if (nd6_defifp
!= NULL
)
4034 nd6log((LOG_INFO
, "%s: is now the default "
4035 "interface (was %s)\n", if_name(nd6_defifp
),
4036 odef_ifp
!= NULL
? if_name(odef_ifp
) : "NONE"));
4038 nd6log((LOG_INFO
, "No default interface set\n"));
4041 * If the Default Router List is empty, install a route
4042 * to the specified interface as default or remove the default
4043 * route when the default interface becomes canceled.
4044 * The check for the queue is actually redundant, but
4045 * we do this here to avoid re-install the default route
4046 * if the list is NOT empty.
4048 if (odef_ifp
!= NULL
) {
4049 defrouter_select(odef_ifp
);
4052 if (nd6_defifp
!= NULL
) {
4053 defrouter_select(nd6_defifp
);
4054 nd6_prefix_sync(nd6_defifp
);
4058 * Our current implementation assumes one-to-one mapping between
4059 * interfaces and links, so it would be natural to use the
4060 * default interface as the default link.
4062 scope6_setdefault(nd6_defifp
);
4064 lck_mtx_unlock(nd6_mutex
);