2 * Copyright (c) 2003-2018 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 nd_pfxrouter
*pfxrtr_lookup(struct nd_prefix
*,
104 struct nd_defrouter
*);
105 static void pfxrtr_add(struct nd_prefix
*, struct nd_defrouter
*);
106 static void pfxrtr_del(struct nd_pfxrouter
*, struct nd_prefix
*);
107 static struct nd_pfxrouter
*find_pfxlist_reachable_router(struct nd_prefix
*);
108 static void nd6_rtmsg(int, struct rtentry
*);
110 static int nd6_prefix_onlink_common(struct nd_prefix
*, boolean_t
,
112 static struct nd_prefix
*nd6_prefix_equal_lookup(struct nd_prefix
*, boolean_t
);
113 static void nd6_prefix_sync(struct ifnet
*);
115 static void in6_init_address_ltimes(struct nd_prefix
*,
116 struct in6_addrlifetime
*);
118 static int rt6_deleteroute(struct radix_node
*, void *);
120 static struct nd_defrouter
*nddr_alloc(int);
121 static void nddr_free(struct nd_defrouter
*);
122 static void nddr_trace(struct nd_defrouter
*, int);
124 static struct nd_prefix
*ndpr_alloc(int);
125 static void ndpr_free(struct nd_prefix
*);
126 static void ndpr_trace(struct nd_prefix
*, int);
128 extern int nd6_recalc_reachtm_interval
;
130 static struct ifnet
*nd6_defifp
= NULL
;
131 int nd6_defifindex
= 0;
132 static unsigned int nd6_defrouter_genid
;
134 int ip6_use_tempaddr
= 1; /* use temp addr by default for testing now */
136 int nd6_accept_6to4
= 1;
138 int ip6_desync_factor
;
139 u_int32_t ip6_temp_preferred_lifetime
= DEF_TEMP_PREFERRED_LIFETIME
;
140 u_int32_t ip6_temp_valid_lifetime
= DEF_TEMP_VALID_LIFETIME
;
142 * shorter lifetimes for debugging purposes.
143 * u_int32_t ip6_temp_preferred_lifetime = 800;
144 * static u_int32_t ip6_temp_valid_lifetime = 1800;
146 int ip6_temp_regen_advance
= TEMPADDR_REGEN_ADVANCE
;
148 extern lck_mtx_t
*nd6_mutex
;
150 /* Serialization variables for single thread access to nd_prefix */
151 static boolean_t nd_prefix_busy
;
152 static void *nd_prefix_waitchan
= &nd_prefix_busy
;
153 static int nd_prefix_waiters
= 0;
155 /* Serialization variables for single thread access to nd_defrouter */
156 static boolean_t nd_defrouter_busy
;
157 static void *nd_defrouter_waitchan
= &nd_defrouter_busy
;
158 static int nd_defrouter_waiters
= 0;
160 /* RTPREF_MEDIUM has to be 0! */
161 #define RTPREF_HIGH 1
162 #define RTPREF_MEDIUM 0
163 #define RTPREF_LOW (-1)
164 #define RTPREF_RESERVED (-2)
165 #define RTPREF_INVALID (-3) /* internal */
167 #define NDPR_TRACE_HIST_SIZE 32 /* size of trace history */
170 __private_extern__
unsigned int ndpr_trace_hist_size
= NDPR_TRACE_HIST_SIZE
;
172 struct nd_prefix_dbg
{
173 struct nd_prefix ndpr_pr
; /* nd_prefix */
174 u_int16_t ndpr_refhold_cnt
; /* # of ref */
175 u_int16_t ndpr_refrele_cnt
; /* # of rele */
177 * Circular lists of ndpr_addref and ndpr_remref callers.
179 ctrace_t ndpr_refhold
[NDPR_TRACE_HIST_SIZE
];
180 ctrace_t ndpr_refrele
[NDPR_TRACE_HIST_SIZE
];
183 static unsigned int ndpr_debug
; /* debug flags */
184 static unsigned int ndpr_size
; /* size of zone element */
185 static struct zone
*ndpr_zone
; /* zone for nd_prefix */
187 #define NDPR_ZONE_MAX 64 /* maximum elements in zone */
188 #define NDPR_ZONE_NAME "nd6_prefix" /* zone name */
190 #define NDDR_TRACE_HIST_SIZE 32 /* size of trace history */
193 __private_extern__
unsigned int nddr_trace_hist_size
= NDDR_TRACE_HIST_SIZE
;
195 struct nd_defrouter_dbg
{
196 struct nd_defrouter nddr_dr
; /* nd_defrouter */
197 uint16_t nddr_refhold_cnt
; /* # of ref */
198 uint16_t nddr_refrele_cnt
; /* # of rele */
200 * Circular lists of ndpr_addref and ndpr_remref callers.
202 ctrace_t nddr_refhold
[NDDR_TRACE_HIST_SIZE
];
203 ctrace_t nddr_refrele
[NDDR_TRACE_HIST_SIZE
];
206 static unsigned int nddr_debug
; /* debug flags */
207 static unsigned int nddr_size
; /* size of zone element */
208 static struct zone
*nddr_zone
; /* zone for nd_defrouter */
210 #define NDDR_ZONE_MAX 64 /* maximum elements in zone */
211 #define NDDR_ZONE_NAME "nd6_defrouter" /* zone name */
213 static unsigned int ndprtr_size
; /* size of zone element */
214 static struct zone
*ndprtr_zone
; /* zone for nd_pfxrouter */
216 #define NDPRTR_ZONE_MAX 64 /* maximum elements in zone */
217 #define NDPRTR_ZONE_NAME "nd6_pfxrouter" /* zone name */
222 PE_parse_boot_argn("ifa_debug", &ndpr_debug
, sizeof(ndpr_debug
));
223 PE_parse_boot_argn("ifa_debug", &nddr_debug
, sizeof(nddr_debug
));
225 ndpr_size
= (ndpr_debug
== 0) ? sizeof(struct nd_prefix
) :
226 sizeof(struct nd_prefix_dbg
);
227 ndpr_zone
= zinit(ndpr_size
, NDPR_ZONE_MAX
* ndpr_size
, 0,
229 if (ndpr_zone
== NULL
) {
230 panic("%s: failed allocating %s", __func__
, NDPR_ZONE_NAME
);
233 zone_change(ndpr_zone
, Z_EXPAND
, TRUE
);
234 zone_change(ndpr_zone
, Z_CALLERACCT
, FALSE
);
236 nddr_size
= (nddr_debug
== 0) ? sizeof(struct nd_defrouter
) :
237 sizeof(struct nd_defrouter_dbg
);
238 nddr_zone
= zinit(nddr_size
, NDDR_ZONE_MAX
* nddr_size
, 0,
240 if (nddr_zone
== NULL
) {
241 panic("%s: failed allocating %s", __func__
, NDDR_ZONE_NAME
);
244 zone_change(nddr_zone
, Z_EXPAND
, TRUE
);
245 zone_change(nddr_zone
, Z_CALLERACCT
, FALSE
);
247 ndprtr_size
= sizeof(struct nd_pfxrouter
);
248 ndprtr_zone
= zinit(ndprtr_size
, NDPRTR_ZONE_MAX
* ndprtr_size
, 0,
250 if (ndprtr_zone
== NULL
) {
251 panic("%s: failed allocating %s", __func__
, NDPRTR_ZONE_NAME
);
254 zone_change(ndprtr_zone
, Z_EXPAND
, TRUE
);
255 zone_change(ndprtr_zone
, Z_CALLERACCT
, FALSE
);
259 * Receive Router Solicitation Message - just for routers.
260 * Router solicitation/advertisement is mostly managed by userland program
261 * (rtadvd) so here we have no function like nd6_ra_output().
271 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
272 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
273 struct nd_router_solicit
*nd_rs
;
274 struct in6_addr saddr6
= ip6
->ip6_src
;
277 union nd_opts ndopts
;
279 /* Expect 32-bit aligned data pointer on strict-align platforms */
280 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
282 /* If I'm not a router, ignore it. */
283 if (!ip6_forwarding
|| !(ifp
->if_eflags
& IFEF_IPV6_ROUTER
)) {
288 if (ip6
->ip6_hlim
!= IPV6_MAXHLIM
) {
290 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
291 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
292 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
));
297 * Don't update the neighbor cache, if src = :: or a non-neighbor.
298 * The former case indicates that the src has no IP address assigned
299 * yet. See nd6_ns_input() for the latter case.
301 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
304 struct sockaddr_in6 src_sa6
;
306 bzero(&src_sa6
, sizeof(src_sa6
));
307 src_sa6
.sin6_family
= AF_INET6
;
308 src_sa6
.sin6_len
= sizeof(src_sa6
);
309 src_sa6
.sin6_addr
= ip6
->ip6_src
;
310 if (!nd6_is_addr_neighbor(&src_sa6
, ifp
, 0)) {
311 nd6log(info
, "nd6_rs_input: "
312 "RS packet from non-neighbor\n");
317 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return );
318 nd_rs
= (struct nd_router_solicit
*)((caddr_t
)ip6
+ off
);
319 icmp6len
-= sizeof(*nd_rs
);
320 nd6_option_init(nd_rs
+ 1, icmp6len
, &ndopts
);
321 if (nd6_options(&ndopts
) < 0) {
323 "nd6_rs_input: invalid ND option, ignored\n");
324 /* nd6_options have incremented stats */
328 if (ndopts
.nd_opts_src_lladdr
) {
329 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
330 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
333 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
335 "nd6_rs_input: lladdrlen mismatch for %s "
336 "(if %d, RS packet %d)\n",
337 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2);
341 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_SOLICIT
, 0);
348 icmp6stat
.icp6s_badrs
++;
353 * Receive Router Advertisement Message.
356 * TODO: on-link bit on prefix information
357 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
365 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
366 struct nd_ifinfo
*ndi
= NULL
;
367 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
368 struct nd_router_advert
*nd_ra
;
369 struct in6_addr saddr6
= ip6
->ip6_src
;
371 union nd_opts ndopts
;
372 struct nd_defrouter
*dr
= NULL
;
375 u_int32_t lladdrlen
= 0;
376 struct nd_prefix_list
*nd_prefix_list_head
= NULL
;
377 u_int32_t nd_prefix_list_length
= 0;
378 struct in6_ifaddr
*ia6
= NULL
;
379 struct nd_prefix_list
*prfl
;
380 struct nd_defrouter dr0
;
381 u_int32_t advreachable
;
383 #if (DEVELOPMENT || DEBUG)
384 if (ip6_accept_rtadv
== 0) {
387 #endif /* (DEVELOPMENT || DEBUG) */
388 /* Expect 32-bit aligned data pointer on strict-align platforms */
389 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
392 * Discard RA unless IFEF_ACCEPT_RTADV is set (as host), or when
393 * IFEF_IPV6_ROUTER is set (as router) but the RA is not locally
394 * generated. For convenience, we allow locally generated (rtadvd)
395 * RAs to be processed on the advertising interface, as a router.
397 * Note that we don't test against ip6_forwarding as we could be
398 * both a host and a router on different interfaces, hence the
399 * check against the per-interface flags.
401 if (!(ifp
->if_eflags
& (IFEF_ACCEPT_RTADV
| IFEF_IPV6_ROUTER
)) ||
402 ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) &&
403 (ia6
= ifa_foraddr6(&saddr6
)) == NULL
)) {
408 IFA_REMREF(&ia6
->ia_ifa
);
412 if (ip6
->ip6_hlim
!= IPV6_MAXHLIM
) {
414 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
415 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
416 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
));
420 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) {
422 "nd6_ra_input: src %s is not link-local\n",
423 ip6_sprintf(&saddr6
));
427 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return );
428 nd_ra
= (struct nd_router_advert
*)((caddr_t
)ip6
+ off
);
430 icmp6len
-= sizeof(*nd_ra
);
431 nd6_option_init(nd_ra
+ 1, icmp6len
, &ndopts
);
432 if (nd6_options(&ndopts
) < 0) {
434 "nd6_ra_input: invalid ND option, ignored\n");
435 /* nd6_options have incremented stats */
439 advreachable
= nd_ra
->nd_ra_reachable
;
441 /* remember if this is a multicasted advertisement */
442 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
446 ndi
= ND_IFINFO(ifp
);
447 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
448 lck_mtx_lock(&ndi
->lock
);
449 bzero(&dr0
, sizeof(dr0
));
451 dr0
.flags
= nd_ra
->nd_ra_flags_reserved
;
452 dr0
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
453 dr0
.expire
= net_uptime() + dr0
.rtlifetime
;
455 /* unspecified or not? (RFC 2461 6.3.4) */
457 advreachable
= ntohl(advreachable
);
458 if (advreachable
<= MAX_REACHABLE_TIME
&&
459 ndi
->basereachable
!= advreachable
) {
460 ndi
->basereachable
= advreachable
;
461 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
462 ndi
->recalctm
= nd6_recalc_reachtm_interval
; /* reset */
465 if (nd_ra
->nd_ra_retransmit
) {
466 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
468 if (nd_ra
->nd_ra_curhoplimit
) {
469 if (ndi
->chlim
< nd_ra
->nd_ra_curhoplimit
) {
470 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
471 } else if (ndi
->chlim
!= nd_ra
->nd_ra_curhoplimit
) {
473 "RA with a lower CurHopLimit sent from "
474 "%s on %s (current = %d, received = %d). "
475 "Ignored.\n", ip6_sprintf(&ip6
->ip6_src
),
476 if_name(ifp
), ndi
->chlim
,
477 nd_ra
->nd_ra_curhoplimit
);
480 lck_mtx_unlock(&ndi
->lock
);
481 lck_mtx_lock(nd6_mutex
);
482 dr
= defrtrlist_update(&dr0
);
483 lck_mtx_unlock(nd6_mutex
);
488 if (ndopts
.nd_opts_pi
) {
489 struct nd_opt_hdr
*pt
;
490 struct nd_opt_prefix_info
*pi
= NULL
;
493 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
494 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
495 pt
= (struct nd_opt_hdr
*)((caddr_t
)pt
+
496 (pt
->nd_opt_len
<< 3))) {
497 struct in6_addr pi_mask
;
498 bzero(&pi_mask
, sizeof(pi_mask
));
500 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
) {
503 pi
= (struct nd_opt_prefix_info
*)pt
;
505 if (pi
->nd_opt_pi_len
!= 4) {
507 "nd6_ra_input: invalid option "
508 "len %d for prefix information option, "
509 "ignored\n", pi
->nd_opt_pi_len
);
513 if (128 < pi
->nd_opt_pi_prefix_len
) {
515 "nd6_ra_input: invalid prefix "
516 "len %d for prefix information option, "
517 "ignored\n", pi
->nd_opt_pi_prefix_len
);
522 * To ignore ::/64 make sure bits beyond prefixlen
525 in6_prefixlen2mask(&pi_mask
, pi
->nd_opt_pi_prefix_len
);
526 pi
->nd_opt_pi_prefix
.s6_addr32
[0] &= pi_mask
.s6_addr32
[0];
527 pi
->nd_opt_pi_prefix
.s6_addr32
[1] &= pi_mask
.s6_addr32
[1];
528 pi
->nd_opt_pi_prefix
.s6_addr32
[2] &= pi_mask
.s6_addr32
[2];
529 pi
->nd_opt_pi_prefix
.s6_addr32
[3] &= pi_mask
.s6_addr32
[3];
531 if (IN6_IS_ADDR_UNSPECIFIED(&pi
->nd_opt_pi_prefix
) ||
532 IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
) ||
533 IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
535 "%s: invalid prefix %s, ignored\n",
537 ip6_sprintf(&pi
->nd_opt_pi_prefix
));
541 bzero(&pr
, sizeof(pr
));
542 lck_mtx_init(&pr
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
544 pr
.ndpr_prefix
.sin6_family
= AF_INET6
;
545 pr
.ndpr_prefix
.sin6_len
= sizeof(pr
.ndpr_prefix
);
546 pr
.ndpr_prefix
.sin6_addr
= pi
->nd_opt_pi_prefix
;
547 pr
.ndpr_ifp
= m
->m_pkthdr
.rcvif
;
549 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
550 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
551 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
552 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
553 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
554 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
556 ntohl(pi
->nd_opt_pi_preferred_time
);
559 * Exceptions to stateless autoconfiguration processing:
560 * + nd6_accept_6to4 == 0 && address has 6to4 prefix
561 * + ip6_only_allow_rfc4193_prefix != 0 &&
562 * address not RFC 4193
564 if (ip6_only_allow_rfc4193_prefix
&&
565 !IN6_IS_ADDR_UNIQUE_LOCAL(&pi
->nd_opt_pi_prefix
)) {
567 "nd6_ra_input: no SLAAC on prefix %s "
569 ip6_sprintf(&pi
->nd_opt_pi_prefix
));
570 pr
.ndpr_raf_auto
= 0;
571 } else if (!nd6_accept_6to4
&&
572 IN6_IS_ADDR_6TO4(&pi
->nd_opt_pi_prefix
)) {
574 "%s: no SLAAC on prefix %s "
575 "[6to4]\n", __func__
,
576 ip6_sprintf(&pi
->nd_opt_pi_prefix
));
577 pr
.ndpr_raf_auto
= 0;
580 if (in6_init_prefix_ltimes(&pr
)) {
582 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
583 continue; /* prefix lifetime init failed */
587 (void) prelist_update(&pr
, dr
, m
, mcast
);
588 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
591 * We have to copy the values out after the
592 * prelist_update call since some of these values won't
593 * be properly set until after the router advertisement
594 * updating can vet the values.
597 MALLOC(prfl
, struct nd_prefix_list
*, sizeof(*prfl
),
598 M_TEMP
, M_WAITOK
| M_ZERO
);
601 log(LOG_DEBUG
, "%s: unable to MALLOC RA prefix "
602 "structure\n", __func__
);
606 /* this is only for nd6_post_msg(), otherwise unused */
607 bcopy(&pr
.ndpr_prefix
, &prfl
->pr
.ndpr_prefix
,
608 sizeof(prfl
->pr
.ndpr_prefix
));
609 prfl
->pr
.ndpr_raf
= pr
.ndpr_raf
;
610 prfl
->pr
.ndpr_plen
= pr
.ndpr_plen
;
611 prfl
->pr
.ndpr_vltime
= pr
.ndpr_vltime
;
612 prfl
->pr
.ndpr_pltime
= pr
.ndpr_pltime
;
613 prfl
->pr
.ndpr_expire
= pr
.ndpr_expire
;
614 prfl
->pr
.ndpr_base_calendartime
=
615 pr
.ndpr_base_calendartime
;
616 prfl
->pr
.ndpr_base_uptime
= pr
.ndpr_base_uptime
;
617 prfl
->pr
.ndpr_stateflags
= pr
.ndpr_stateflags
;
618 prfl
->pr
.ndpr_addrcnt
= pr
.ndpr_addrcnt
;
619 prfl
->pr
.ndpr_ifp
= pr
.ndpr_ifp
;
621 prfl
->next
= nd_prefix_list_head
;
622 nd_prefix_list_head
= prfl
;
623 nd_prefix_list_length
++;
630 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
631 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
634 if (mtu
< IPV6_MMTU
) {
635 nd6log(info
, "nd6_ra_input: bogus mtu option "
636 "mtu=%d sent from %s, ignoring\n",
637 mtu
, ip6_sprintf(&ip6
->ip6_src
));
641 lck_mtx_lock(&ndi
->lock
);
644 if (mtu
<= ndi
->maxmtu
) {
645 int change
= (ndi
->linkmtu
!= mtu
);
648 lck_mtx_unlock(&ndi
->lock
);
649 if (change
) { /* in6_maxmtu may change */
653 nd6log(info
, "nd6_ra_input: bogus mtu "
654 "mtu=%d sent from %s; "
655 "exceeds maxmtu %d, ignoring\n",
656 mtu
, ip6_sprintf(&ip6
->ip6_src
),
658 lck_mtx_unlock(&ndi
->lock
);
661 lck_mtx_unlock(&ndi
->lock
);
662 nd6log(info
, "nd6_ra_input: mtu option "
663 "mtu=%d sent from %s; maxmtu unknown, "
665 mtu
, ip6_sprintf(&ip6
->ip6_src
));
672 * Source link layer address
674 if (ndopts
.nd_opts_src_lladdr
) {
675 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
676 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
679 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
681 "nd6_ra_input: lladdrlen mismatch for %s "
682 "(if %d, RA packet %d)\n",
683 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2);
687 if (dr
&& dr
->stateflags
& NDDRF_MAPPED
) {
688 saddr6
= dr
->rtaddr_mapped
;
691 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, (int)lladdrlen
,
692 ND_ROUTER_ADVERT
, 0);
695 nd6_post_msg(KEV_ND6_RA
, nd_prefix_list_head
, nd_prefix_list_length
,
699 * Installing a link-layer address might change the state of the
700 * router's neighbor cache, which might also affect our on-link
701 * detection of adveritsed prefixes.
703 lck_mtx_lock(nd6_mutex
);
704 pfxlist_onlink_check();
705 lck_mtx_unlock(nd6_mutex
);
714 while ((prfl
= nd_prefix_list_head
) != NULL
) {
715 nd_prefix_list_head
= prfl
->next
;
722 icmp6stat
.icp6s_badra
++;
727 * default router list proccessing sub routines
730 /* tell the change to user processes watching the routing socket. */
732 nd6_rtmsg(int cmd
, struct rtentry
*rt
)
734 struct rt_addrinfo info
;
735 struct ifnet
*ifp
= rt
->rt_ifp
;
737 RT_LOCK_ASSERT_HELD(rt
);
739 bzero((caddr_t
)&info
, sizeof(info
));
740 /* It's not necessary to lock ifp for if_lladdr */
741 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
742 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
743 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
745 * ifa_addr pointers for both should always be valid
746 * in this context; no need to hold locks.
748 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
749 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
751 rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0);
755 defrouter_addreq(struct nd_defrouter
*new, boolean_t scoped
)
757 struct sockaddr_in6 def
, mask
, gate
;
758 struct rtentry
*newrt
= NULL
;
759 unsigned int ifscope
;
761 struct nd_ifinfo
*ndi
= ND_IFINFO(new->ifp
);
763 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
764 NDDR_LOCK_ASSERT_NOTHELD(new);
766 * We're free to lock and unlock NDDR because our callers
767 * are holding an extra reference for us.
771 if (new->stateflags
& NDDRF_INSTALLED
) {
775 if (new->ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
776 nd6log2(info
, "%s: ignoring router %s, scoped=%d, "
777 "static=%d on advertising interface\n", if_name(new->ifp
),
778 ip6_sprintf(&new->rtaddr
), scoped
,
779 (new->stateflags
& NDDRF_STATIC
) ? 1 : 0);
783 nd6log2(info
, "%s: adding default router %s, scoped=%d, "
784 "static=%d\n", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
),
785 scoped
, (new->stateflags
& NDDRF_STATIC
) ? 1 : 0);
787 Bzero(&def
, sizeof(def
));
788 Bzero(&mask
, sizeof(mask
));
789 Bzero(&gate
, sizeof(gate
));
791 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
792 = sizeof(struct sockaddr_in6
);
793 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
795 if (new->stateflags
& NDDRF_MAPPED
) {
796 gate
.sin6_addr
= new->rtaddr_mapped
;
798 gate
.sin6_addr
= new->rtaddr
;
801 ifscope
= scoped
? new->ifp
->if_index
: IFSCOPE_NONE
;
805 * Cellular networks may have buggy deployments
806 * with gateway IPv6 link local address with same
807 * interface identifier as the one that has been
808 * assigned for the cellular context.
809 * If gateway is same as locally configured link local
810 * interface on cellular interface, generated a different one
811 * and store it in the nd_defrouter entry and use it to work
814 if (new->ifp
->if_type
== IFT_CELLULAR
&&
815 !(new->stateflags
& NDDRF_STATIC
) &&
816 !(new->stateflags
& NDDRF_MAPPED
) &&
817 IN6_IS_ADDR_LINKLOCAL(&gate
.sin6_addr
) &&
818 ndi
&& !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
819 struct in6_ifaddr
*tmp_ia6
= in6ifa_ifpforlinklocal(new->ifp
, 0);
821 if (tmp_ia6
!= NULL
&&
822 !(tmp_ia6
->ia6_flags
& IN6_IFF_NOTMANUAL
) &&
823 IN6_ARE_ADDR_EQUAL(&tmp_ia6
->ia_addr
.sin6_addr
,
825 gate
.sin6_addr
.s6_addr8
[15] += 1;
826 new->rtaddr_mapped
= gate
.sin6_addr
;
827 new->stateflags
|= NDDRF_MAPPED
;
829 nd6log(info
, "%s: Default router %s mapped "
830 "to ", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
));
831 nd6log(info
, "%s\n", ip6_sprintf(&new->rtaddr_mapped
));
835 err
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&def
,
836 (struct sockaddr
*)&gate
, (struct sockaddr
*)&mask
,
837 RTF_GATEWAY
, &newrt
, ifscope
);
841 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */
842 RT_REMREF_LOCKED(newrt
);
845 new->stateflags
|= NDDRF_INSTALLED
;
846 if (ifscope
!= IFSCOPE_NONE
) {
847 new->stateflags
|= NDDRF_IFSCOPE
;
850 nd6log(error
, "%s: failed to add default router "
851 "%s on %s scoped %d (errno = %d)\n", __func__
,
852 ip6_sprintf(&gate
.sin6_addr
), if_name(new->ifp
),
853 (ifscope
!= IFSCOPE_NONE
), err
);
862 struct nd_defrouter
*
864 struct in6_addr
*addr
,
867 struct nd_defrouter
*dr
;
869 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
871 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
872 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
874 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) {
875 NDDR_ADDREF_LOCKED(dr
);
882 return NULL
; /* search failed */
886 * Remove the default route for a given router.
887 * This is just a subroutine function for defrouter_select(), and should
888 * not be called from anywhere else.
891 defrouter_delreq(struct nd_defrouter
*dr
)
893 struct sockaddr_in6 def
, mask
, gate
;
894 struct rtentry
*oldrt
= NULL
;
895 unsigned int ifscope
;
898 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
899 NDDR_LOCK_ASSERT_NOTHELD(dr
);
901 * We're free to lock and unlock NDDR because our callers
902 * are holding an extra reference for us.
905 /* ifp would be NULL for the "drany" case */
906 if (dr
->ifp
!= NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
)) {
910 nd6log2(info
, "%s: removing default router %s, scoped=%d, "
911 "static=%d\n", dr
->ifp
!= NULL
? if_name(dr
->ifp
) : "ANY",
912 ip6_sprintf(&dr
->rtaddr
), (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
913 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0);
915 Bzero(&def
, sizeof(def
));
916 Bzero(&mask
, sizeof(mask
));
917 Bzero(&gate
, sizeof(gate
));
919 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
920 = sizeof(struct sockaddr_in6
);
921 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
924 * The router entry may be mapped to a different address.
925 * If that is the case, use the mapped address as gateway
926 * to do operation on the routing table.
927 * To get more context, read the related comment in
930 if (dr
->stateflags
& NDDRF_MAPPED
) {
931 gate
.sin6_addr
= dr
->rtaddr_mapped
;
933 gate
.sin6_addr
= dr
->rtaddr
;
936 if (dr
->ifp
!= NULL
) {
937 ifscope
= (dr
->stateflags
& NDDRF_IFSCOPE
) ?
938 dr
->ifp
->if_index
: IFSCOPE_NONE
;
940 ifscope
= IFSCOPE_NONE
;
944 err
= rtrequest_scoped(RTM_DELETE
,
945 (struct sockaddr
*)&def
, (struct sockaddr
*)&gate
,
946 (struct sockaddr
*)&mask
, RTF_GATEWAY
, &oldrt
, ifscope
);
950 nd6_rtmsg(RTM_DELETE
, oldrt
);
953 } else if (err
!= ESRCH
) {
954 nd6log(error
, "%s: failed to delete default router "
955 "%s on %s scoped %d (errno = %d)\n", __func__
,
956 ip6_sprintf(&gate
.sin6_addr
), dr
->ifp
!= NULL
?
957 if_name(dr
->ifp
) : "ANY", (ifscope
!= IFSCOPE_NONE
), err
);
960 /* ESRCH means it's no longer in the routing table; ignore it */
961 if (oldrt
!= NULL
|| err
== ESRCH
) {
962 dr
->stateflags
&= ~NDDRF_INSTALLED
;
963 if (ifscope
!= IFSCOPE_NONE
) {
964 dr
->stateflags
&= ~NDDRF_IFSCOPE
;
974 * remove all default routes from default router list
977 defrouter_reset(void)
979 struct nd_defrouter
*dr
, drany
;
981 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
983 dr
= TAILQ_FIRST(&nd_defrouter
);
986 if (dr
->stateflags
& NDDRF_INSTALLED
) {
987 NDDR_ADDREF_LOCKED(dr
);
989 lck_mtx_unlock(nd6_mutex
);
990 defrouter_delreq(dr
);
991 lck_mtx_lock(nd6_mutex
);
993 dr
= TAILQ_FIRST(&nd_defrouter
);
996 dr
= TAILQ_NEXT(dr
, dr_entry
);
1000 /* Nuke primary (non-scoped) default router */
1001 bzero(&drany
, sizeof(drany
));
1002 lck_mtx_init(&drany
.nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1003 lck_mtx_unlock(nd6_mutex
);
1004 defrouter_delreq(&drany
);
1005 lck_mtx_destroy(&drany
.nddr_lock
, ifa_mtx_grp
);
1006 lck_mtx_lock(nd6_mutex
);
1010 defrtrlist_ioctl(u_long cmd
, caddr_t data
)
1012 struct nd_defrouter dr0
;
1013 unsigned int ifindex
;
1014 struct ifnet
*dr_ifp
;
1015 int error
= 0, add
= 0;
1017 /* XXX Handle mapped default router entries */
1019 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1020 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1023 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1024 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1025 bzero(&dr0
, sizeof(dr0
));
1026 if (cmd
== SIOCDRADD_IN6_64
|| cmd
== SIOCDRDEL_IN6_64
) {
1027 struct in6_defrouter_64
*r_64
=
1028 (struct in6_defrouter_64
*)(void *)data
;
1031 bcopy(&r_64
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1032 sizeof(dr0
.rtaddr
));
1033 dr0
.flags
= r_64
->flags
;
1034 bcopy(&r_64
->if_index
, &i
, sizeof(i
));
1037 struct in6_defrouter_32
*r_32
=
1038 (struct in6_defrouter_32
*)(void *)data
;
1041 bcopy(&r_32
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1042 sizeof(dr0
.rtaddr
));
1043 dr0
.flags
= r_32
->flags
;
1044 bcopy(&r_32
->if_index
, &i
, sizeof(i
));
1047 ifnet_head_lock_shared();
1048 /* Don't need to check is ifindex is < 0 since it's unsigned */
1049 if (if_index
< ifindex
||
1050 (dr_ifp
= ifindex2ifnet
[ifindex
]) == NULL
) {
1058 if (ND_IFINFO(dr_ifp
) == NULL
||
1059 !ND_IFINFO(dr_ifp
)->initialized
) {
1064 if (IN6_IS_SCOPE_EMBED(&dr0
.rtaddr
)) {
1065 uint16_t *scope
= &dr0
.rtaddr
.s6_addr16
[1];
1068 *scope
= htons(dr_ifp
->if_index
);
1069 } else if (*scope
!= htons(dr_ifp
->if_index
)) {
1076 error
= defrtrlist_add_static(&dr0
);
1078 if (!add
|| error
!= 0) {
1079 int err
= defrtrlist_del_static(&dr0
);
1087 error
= EOPNOTSUPP
; /* check for safety */
1095 * XXX Please make sure to remove dr from the
1096 * global default router tailq list before this
1098 * Also ensure that you release the list reference
1099 * only after calling this routine.
1102 defrtrlist_del(struct nd_defrouter
*dr
)
1104 #if (DEVELOPMENT || DEBUG)
1105 struct nd_defrouter
*dr_itr
= NULL
;
1107 struct nd_prefix
*pr
;
1108 struct ifnet
*ifp
= dr
->ifp
;
1109 struct nd_ifinfo
*ndi
= NULL
;
1112 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1114 #if (DEVELOPMENT || DEBUG)
1116 * Verify that the router is not in the global default
1118 * Can't use defrouter_lookup here because that just works
1119 * with address and ifp pointer.
1120 * We have to compare the memory here.
1121 * Also we can't use ASSERT here as that is not defined
1122 * for development builds.
1124 TAILQ_FOREACH(dr_itr
, &nd_defrouter
, dr_entry
)
1125 VERIFY(dr
!= dr_itr
);
1127 ++nd6_defrouter_genid
;
1129 * Flush all the routing table entries that use the router
1132 /* above is a good condition? */
1134 lck_mtx_unlock(nd6_mutex
);
1135 if (dr
->stateflags
& NDDRF_MAPPED
) {
1136 rt6_flush(&dr
->rtaddr_mapped
, ifp
);
1138 rt6_flush(&dr
->rtaddr
, ifp
);
1141 lck_mtx_lock(nd6_mutex
);
1143 nd6log2(info
, "%s: freeing defrouter %s\n", if_name(dr
->ifp
),
1144 ip6_sprintf(&dr
->rtaddr
));
1146 * Delete it from the routing table.
1149 lck_mtx_unlock(nd6_mutex
);
1150 defrouter_delreq(dr
);
1151 lck_mtx_lock(nd6_mutex
);
1155 * Also delete all the pointers to the router in each prefix lists.
1157 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1158 struct nd_pfxrouter
*pfxrtr
;
1161 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
) {
1162 pfxrtr_del(pfxrtr
, pr
);
1167 pfxlist_onlink_check();
1170 ndi
= ND_IFINFO(ifp
);
1171 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1172 lck_mtx_lock(&ndi
->lock
);
1173 VERIFY(ndi
->ndefrouters
>= 0);
1174 if (ndi
->ndefrouters
> 0 && --ndi
->ndefrouters
== 0) {
1178 lck_mtx_unlock(&ndi
->lock
);
1181 * If the router is the primary one, choose a new one.
1182 * We always try to pick another eligible router
1183 * on this interface as we do scoped routing
1185 defrouter_select(ifp
);
1193 defrtrlist_add_static(struct nd_defrouter
*new)
1195 struct nd_defrouter
*dr
;
1198 new->rtlifetime
= -1;
1199 new->stateflags
|= NDDRF_STATIC
;
1201 /* we only want the preference level */
1202 new->flags
&= ND_RA_FLAG_RTPREF_MASK
;
1204 lck_mtx_lock(nd6_mutex
);
1205 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1206 if (dr
!= NULL
&& !(dr
->stateflags
& NDDRF_STATIC
)) {
1212 dr
= defrtrlist_update(new);
1222 lck_mtx_unlock(nd6_mutex
);
1228 defrtrlist_del_static(struct nd_defrouter
*new)
1230 struct nd_defrouter
*dr
;
1232 lck_mtx_lock(nd6_mutex
);
1233 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1234 if (dr
== NULL
|| !(dr
->stateflags
& NDDRF_STATIC
)) {
1240 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1242 NDDR_REMREF(dr
); /* remove list reference */
1245 lck_mtx_unlock(nd6_mutex
);
1247 return dr
!= NULL
? 0 : EINVAL
;
1251 * for default router selection
1252 * regards router-preference field as a 2-bit signed integer
1255 rtpref(struct nd_defrouter
*dr
)
1257 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
1258 case ND_RA_FLAG_RTPREF_HIGH
:
1260 case ND_RA_FLAG_RTPREF_MEDIUM
:
1261 case ND_RA_FLAG_RTPREF_RSV
:
1262 return RTPREF_MEDIUM
;
1263 case ND_RA_FLAG_RTPREF_LOW
:
1267 * This case should never happen. If it did, it would mean a
1268 * serious bug of kernel internal. We thus always bark here.
1269 * Or, can we even panic?
1271 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
1272 return RTPREF_INVALID
;
1278 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
1280 * 1) Routers that are reachable or probably reachable should be preferred.
1281 * If we have more than one (probably) reachable router, prefer ones
1282 * with the highest router preference.
1283 * 2) When no routers on the list are known to be reachable or
1284 * probably reachable, routers SHOULD be selected in a round-robin
1285 * fashion, regardless of router preference values.
1286 * 3) If the Default Router List is empty, assume that all
1287 * destinations are on-link.
1289 * When Scoped Routing is enabled, the selection logic is amended as follows:
1291 * a) When a default interface is specified, the primary/non-scoped default
1292 * router will be set to the reachable router on that link (if any) with
1293 * the highest router preference.
1294 * b) When there are more than one routers on the same link, the one with
1295 * the highest router preference will be installed, either as scoped or
1296 * non-scoped route entry. If they all share the same preference value,
1297 * the one installed will be the static or the first encountered reachable
1298 * router, i.e. static one wins over dynamic.
1299 * c) When no routers on the list are known to be reachable, or probably
1300 * reachable, no round-robin selection will take place when the default
1303 * We assume nd_defrouter is sorted by router preference value.
1304 * Since the code below covers both with and without router preference cases,
1305 * we do not need to classify the cases by ifdef.
1308 defrouter_select(struct ifnet
*ifp
)
1310 struct nd_defrouter
*dr
= NULL
;
1311 struct nd_defrouter
*selected_dr
= NULL
;
1312 struct nd_defrouter
*installed_dr
= NULL
;
1313 struct llinfo_nd6
*ln
= NULL
;
1314 struct rtentry
*rt
= NULL
;
1315 struct nd_ifinfo
*ndi
= NULL
;
1316 unsigned int genid
= 0;
1317 boolean_t is_installed_reachable
= FALSE
;
1319 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1325 "%s:%d: Return early. NULL interface",
1326 __func__
, __LINE__
);
1330 "%s:%d: NULL interface passed. Setting to default interface %s.\n",
1331 __func__
, __LINE__
, if_name(ifp
));
1334 if (ifp
== lo_ifp
) {
1336 "%s:%d: Return early. "
1337 "Default router select called for loopback.\n",
1338 __func__
, __LINE__
);
1342 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1344 "%s:%d: Return early. "
1345 "Default router select called for interface"
1346 " %s with IFEF_IPV6_ROUTER flag set\n",
1347 __func__
, __LINE__
, if_name(ifp
));
1352 * Let's handle easy case (3) first:
1353 * If default router list is empty, there's nothing to be done.
1355 if (!TAILQ_FIRST(&nd_defrouter
)) {
1357 "%s:%d: Return early. "
1358 "Default router is empty.\n", __func__
, __LINE__
);
1363 * Take an early exit if number of routers in nd_ifinfo is
1364 * 0 for the interface.
1366 ndi
= ND_IFINFO(ifp
);
1367 if (!ndi
|| !ndi
->initialized
) {
1369 "%s:%d: Return early. "
1370 "Interface %s's nd_ifinfo not initialized.\n",
1371 __func__
, __LINE__
, if_name(ifp
));
1375 if (ndi
->ndefrouters
== 0) {
1377 "%s:%d: Return early. "
1378 "%s does not have any default routers.\n",
1379 __func__
, __LINE__
, if_name(ifp
));
1384 * Due to the number of times we drop nd6_mutex, we need to
1385 * serialize this function.
1387 while (nd_defrouter_busy
) {
1388 nd_defrouter_waiters
++;
1389 msleep(nd_defrouter_waitchan
, nd6_mutex
, (PZERO
- 1),
1391 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1393 nd_defrouter_busy
= TRUE
;
1396 * Search for a (probably) reachable router from the list.
1397 * We just pick up the first reachable one (if any), assuming that
1398 * the ordering rule of the list described in defrtrlist_update().
1400 * For all intents and purposes of Scoped Routing:
1401 * selected_dr = candidate for primary router
1402 * installed_dr = currently installed primary router
1404 genid
= nd6_defrouter_genid
;
1405 dr
= TAILQ_FIRST(&nd_defrouter
);
1407 while (dr
!= NULL
) {
1408 struct in6_addr rtaddr
;
1409 struct ifnet
*drifp
= NULL
;
1410 struct nd_defrouter
*drrele
= NULL
;
1416 dr
= TAILQ_NEXT(dr
, dr_entry
);
1421 * Optimize for the common case.
1422 * When the interface has only one default router
1423 * there's no point checking for reachability as
1424 * there's nothing else to choose from.
1426 if (ndi
->ndefrouters
== 1) {
1428 "%s:%d: Fast forward default router selection "
1429 "as interface %s has learned only one default "
1430 "router and there's nothing else to choose from.\n",
1431 __func__
, __LINE__
, if_name(ifp
));
1432 VERIFY(selected_dr
== NULL
&& installed_dr
== NULL
);
1434 if (dr
->stateflags
& NDDRF_INSTALLED
) {
1437 NDDR_ADDREF_LOCKED(selected_dr
);
1442 if (dr
->stateflags
& NDDRF_MAPPED
) {
1443 rtaddr
= dr
->rtaddr_mapped
;
1445 rtaddr
= dr
->rtaddr
;
1448 NDDR_ADDREF_LOCKED(dr
); /* for this for loop */
1451 /* Callee returns a locked route upon success */
1452 if (selected_dr
== NULL
) {
1453 lck_mtx_unlock(nd6_mutex
);
1454 if ((rt
= nd6_lookup(&rtaddr
, 0, drifp
, 0)) != NULL
&&
1455 (ln
= rt
->rt_llinfo
) != NULL
&&
1456 ND6_IS_LLINFO_PROBREACH(ln
)) {
1457 RT_LOCK_ASSERT_HELD(rt
);
1459 NDDR_ADDREF(selected_dr
);
1461 lck_mtx_lock(nd6_mutex
);
1465 RT_REMREF_LOCKED(rt
);
1472 * When there are more than one routers on the same link, the one with
1473 * the highest router preference will be installed.
1474 * Since the list is in decreasing order of preference:
1475 * 1) If selected_dr is not NULL, only use dr if it is static and has
1476 * equal preference and selected_dr is not static.
1477 * 2) Else if selected_dr is NULL, and dr is static make selected_dr = dr
1480 if (((selected_dr
&& (rtpref(dr
) >= rtpref(selected_dr
)) &&
1481 !(selected_dr
->stateflags
& NDDRF_STATIC
)) ||
1482 (selected_dr
== NULL
)) &&
1483 (dr
->stateflags
& NDDRF_STATIC
)) {
1485 /* Release it later on */
1486 VERIFY(drrele
== NULL
);
1487 drrele
= selected_dr
;
1490 NDDR_ADDREF_LOCKED(selected_dr
);
1493 /* Record the currently installed router */
1494 if (dr
->stateflags
& NDDRF_INSTALLED
) {
1495 if (installed_dr
== NULL
) {
1497 NDDR_ADDREF_LOCKED(installed_dr
);
1498 if (dr
->stateflags
& NDDRF_MAPPED
) {
1499 rtaddr
= installed_dr
->rtaddr_mapped
;
1501 rtaddr
= installed_dr
->rtaddr
;
1504 lck_mtx_unlock(nd6_mutex
);
1505 /* Callee returns a locked route upon success */
1506 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
1507 RT_LOCK_ASSERT_HELD(rt
);
1508 if ((ln
= rt
->rt_llinfo
) != NULL
&&
1509 ND6_IS_LLINFO_PROBREACH(ln
)) {
1510 is_installed_reachable
= TRUE
;
1513 RT_REMREF_LOCKED(rt
);
1517 lck_mtx_lock(nd6_mutex
);
1519 /* this should not happen; warn for diagnosis */
1520 nd6log(error
, "defrouter_select: more than one "
1521 "default router is installed for interface :%s.\n",
1529 NDDR_REMREF(dr
); /* for this for loop */
1530 if (drrele
!= NULL
) {
1531 NDDR_REMREF(drrele
);
1535 * Check if the list changed when we gave up
1536 * the nd6_mutex lock
1538 if (genid
!= nd6_defrouter_genid
) {
1540 NDDR_REMREF(selected_dr
);
1545 NDDR_REMREF(installed_dr
);
1546 installed_dr
= NULL
;
1549 if (ndi
->ndefrouters
== 0) {
1551 "%s:%d: Interface %s no longer "
1552 "has any default routers. Abort.\n",
1553 __func__
, __LINE__
, if_name(ifp
));
1557 "%s:%d: Iterate default router list again "
1558 "for interface %s, as the list seems to have "
1559 "changed during release-reaquire of global "
1560 "nd6_mutex lock.\n",
1561 __func__
, __LINE__
, if_name(ifp
));
1563 is_installed_reachable
= FALSE
;
1564 genid
= nd6_defrouter_genid
;
1565 dr
= TAILQ_FIRST(&nd_defrouter
);
1567 dr
= TAILQ_NEXT(dr
, dr_entry
);
1572 * If none of the default routers was found to be reachable,
1573 * round-robin the list regardless of preference.
1574 * Please note selected_dr equal to NULL implies that even
1575 * installed default router is not reachable
1577 if (selected_dr
== NULL
) {
1579 for (dr
= TAILQ_NEXT(installed_dr
, dr_entry
); dr
;
1580 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1581 if (installed_dr
->ifp
!= dr
->ifp
) {
1590 * If none was installed or the installed one if the last
1591 * one on the list, select the first one from the list
1593 if ((installed_dr
== NULL
) || (selected_dr
== NULL
)) {
1594 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1595 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1596 if (dr
->ifp
== ifp
) {
1603 if ((selected_dr
== NULL
) && (installed_dr
== NULL
)) {
1605 "%s:%d: Between release and reaquire of global "
1606 "nd6_mutex lock, the list seems to have changed "
1607 "and it does not have any default routers for "
1609 __func__
, __LINE__
, if_name(ifp
));
1613 if (selected_dr
!= installed_dr
) {
1614 NDDR_ADDREF(selected_dr
);
1616 } else if (installed_dr
!= NULL
) {
1617 if (installed_dr
!= selected_dr
) {
1619 * This means that selected default router is reachable
1620 * while installed one may or may not be.
1621 * Static router should always be considered as reachable
1622 * for router selection process.
1624 if ((installed_dr
->stateflags
& NDDRF_STATIC
) &&
1625 rtpref(installed_dr
) >= rtpref(selected_dr
)) {
1626 NDDR_REMREF(selected_dr
);
1627 selected_dr
= installed_dr
;
1628 } else if (is_installed_reachable
) {
1629 if (rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1630 NDDR_REMREF(selected_dr
);
1631 selected_dr
= installed_dr
;
1635 NDDR_REMREF(selected_dr
);
1641 * If the selected router is different than the installed one,
1642 * remove the installed router and install the selected one.
1643 * Note that the selected router is never NULL here.
1644 * Else check if the route entry scope has to be changed.
1646 lck_mtx_unlock(nd6_mutex
);
1647 if (installed_dr
!= selected_dr
) {
1649 "%s:%d: Found a better router for interface "
1650 "%s. Installing new default route.\n",
1651 __func__
, __LINE__
, if_name(ifp
));
1652 if (installed_dr
!= NULL
) {
1653 defrouter_delreq(installed_dr
);
1656 * Install scoped route if the interface is not
1657 * the default nd6 interface.
1659 defrouter_addreq(selected_dr
,
1660 (selected_dr
->ifp
!= nd6_defifp
));
1661 } else if (((installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1662 (installed_dr
->ifp
== nd6_defifp
)) ||
1663 (!(installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1664 (installed_dr
->ifp
!= nd6_defifp
))) {
1666 "%s:%d: Need to reinstall default route for interface "
1667 "%s as its scope has changed.\n",
1668 __func__
, __LINE__
, if_name(ifp
));
1669 defrouter_delreq(installed_dr
);
1670 defrouter_addreq(installed_dr
,
1671 (installed_dr
->ifp
!= nd6_defifp
));
1674 "%s:%d: No need to change the default "
1675 "route for interface %s.\n",
1676 __func__
, __LINE__
, if_name(ifp
));
1678 lck_mtx_lock(nd6_mutex
);
1680 if (selected_dr
&& (selected_dr
!= installed_dr
)) {
1681 NDDR_REMREF(selected_dr
);
1684 NDDR_REMREF(installed_dr
);
1686 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1687 VERIFY(nd_defrouter_busy
);
1688 nd_defrouter_busy
= FALSE
;
1689 if (nd_defrouter_waiters
> 0) {
1690 nd_defrouter_waiters
= 0;
1691 wakeup(nd_defrouter_waitchan
);
1695 static struct nd_defrouter
*
1696 defrtrlist_update_common(struct nd_defrouter
*new, boolean_t scoped
)
1698 struct nd_defrouter
*dr
, *n
;
1699 struct ifnet
*ifp
= new->ifp
;
1700 struct nd_ifinfo
*ndi
= NULL
;
1701 struct timeval caltime
;
1703 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1705 if ((dr
= defrouter_lookup(&new->rtaddr
, ifp
)) != NULL
) {
1707 if (new->rtlifetime
== 0) {
1708 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1710 NDDR_REMREF(dr
); /* remove list reference */
1714 int oldpref
= rtpref(dr
);
1715 struct nd_defrouter
*p
= NULL
;
1717 dr
->flags
= new->flags
; /* xxx flag check */
1718 dr
->rtlifetime
= new->rtlifetime
;
1719 dr
->expire
= new->expire
;
1722 * If the preference does not change, there's no need
1723 * to sort the entries. If Scoped Routing is enabled,
1724 * put the primary/non-scoped router at the top of the
1725 * list of routers in the same preference band, unless
1726 * it's already at that position.
1728 /* same preference and scoped; just return */
1729 if (rtpref(new) == oldpref
&& scoped
) {
1733 n
= TAILQ_FIRST(&nd_defrouter
);
1735 /* preference changed; sort it */
1736 if (rtpref(new) != oldpref
) {
1740 /* not at the top of band; sort it */
1741 if (n
!= dr
&& rtpref(n
) == oldpref
&&
1742 (!p
|| rtpref(p
) > rtpref(n
))) {
1747 n
= TAILQ_NEXT(n
, dr_entry
);
1750 /* nothing has changed, just return */
1751 if (n
== NULL
&& (scoped
||
1752 !(dr
->stateflags
& NDDRF_IFSCOPE
))) {
1757 * preferred router may be changed, so relocate
1759 * XXX: calling TAILQ_REMOVE directly is a bad manner.
1760 * However, since defrtrlist_del() has many side
1761 * effects, we intentionally do so here.
1762 * defrouter_select() below will handle routing
1765 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1766 new->stateflags
= dr
->stateflags
;
1776 /* entry does not exist */
1777 if (new->rtlifetime
== 0) {
1781 n
= nddr_alloc(M_WAITOK
);
1786 ndi
= ND_IFINFO(ifp
);
1787 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1788 lck_mtx_lock(&ndi
->lock
);
1789 if (ip6_maxifdefrouters
>= 0 &&
1790 ndi
->ndefrouters
>= ip6_maxifdefrouters
) {
1791 lck_mtx_unlock(&ndi
->lock
);
1796 NDDR_ADDREF(n
); /* for the nd_defrouter list */
1797 NDDR_ADDREF(n
); /* for the caller */
1799 ++nd6_defrouter_genid
;
1801 VERIFY(ndi
->ndefrouters
!= 0);
1802 lck_mtx_unlock(&ndi
->lock
);
1804 nd6log2(info
, "%s: allocating defrouter %s\n", if_name(ifp
),
1805 ip6_sprintf(&new->rtaddr
));
1807 getmicrotime(&caltime
);
1809 memcpy(&n
->rtaddr
, &new->rtaddr
, sizeof(n
->rtaddr
));
1810 n
->flags
= new->flags
;
1811 n
->stateflags
= new->stateflags
;
1812 n
->rtlifetime
= new->rtlifetime
;
1813 n
->expire
= new->expire
;
1814 n
->base_calendartime
= caltime
.tv_sec
;
1815 n
->base_uptime
= net_uptime();
1820 /* get nd6_service() to be scheduled as soon as it's convenient */
1821 ++nd6_sched_timeout_want
;
1824 * Insert the new router in the Default Router List;
1825 * The Default Router List should be in the descending order
1826 * of router-preferece. When Scoped Routing is disabled, routers
1827 * with the same preference are sorted in the arriving time order;
1828 * otherwise, the first entry in the list of routers having the same
1829 * preference is the primary default router, when the interface used
1830 * by the entry is the default interface.
1833 /* insert at the end of the group */
1834 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1835 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1836 if (rtpref(n
) > rtpref(dr
) ||
1837 (!scoped
&& rtpref(n
) == rtpref(dr
))) {
1842 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1844 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
1847 defrouter_select(ifp
);
1852 static struct nd_defrouter
*
1853 defrtrlist_update(struct nd_defrouter
*new)
1855 struct nd_defrouter
*dr
;
1857 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1858 dr
= defrtrlist_update_common(new,
1859 (nd6_defifp
!= NULL
&& new->ifp
!= nd6_defifp
));
1864 static struct nd_pfxrouter
*
1865 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1867 struct nd_pfxrouter
*search
;
1869 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1870 NDPR_LOCK_ASSERT_HELD(pr
);
1872 for (search
= pr
->ndpr_advrtrs
.lh_first
; search
;
1873 search
= search
->pfr_next
) {
1874 if (search
->router
== dr
) {
1883 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1885 struct nd_pfxrouter
*new;
1887 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1888 NDPR_LOCK_ASSERT_NOTHELD(pr
);
1890 new = zalloc(ndprtr_zone
);
1894 bzero(new, sizeof(*new));
1898 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
1902 pfxlist_onlink_check();
1906 pfxrtr_del(struct nd_pfxrouter
*pfr
, struct nd_prefix
*pr
)
1908 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1909 NDPR_LOCK_ASSERT_HELD(pr
);
1911 LIST_REMOVE(pfr
, pfr_entry
);
1912 zfree(ndprtr_zone
, pfr
);
1916 * The routine has been modified to atomically refresh expiry
1917 * time for nd6 prefix as the part of lookup.
1918 * There's a corner case where a system going
1919 * in sleep gets rid of manual addresses configured in the system
1920 * and then schedules the prefix for deletion.
1921 * However before the prefix gets deleted, if system comes out
1922 * from sleep and configures same address before prefix deletion
1923 * , the later prefix deletion will remove the prefix route and
1924 * the system will not be able to communicate with other IPv6
1925 * neighbor nodes in the same subnet.
1928 nd6_prefix_lookup(struct nd_prefix
*pr
, int nd6_prefix_expiry
)
1930 struct nd_prefix
*search
;
1932 lck_mtx_lock(nd6_mutex
);
1933 for (search
= nd_prefix
.lh_first
; search
; search
= search
->ndpr_next
) {
1935 if (pr
->ndpr_ifp
== search
->ndpr_ifp
&&
1936 pr
->ndpr_plen
== search
->ndpr_plen
&&
1937 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1938 &search
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1939 if (nd6_prefix_expiry
!= ND6_PREFIX_EXPIRY_UNSPEC
) {
1940 search
->ndpr_expire
= nd6_prefix_expiry
;
1942 NDPR_ADDREF_LOCKED(search
);
1943 NDPR_UNLOCK(search
);
1946 NDPR_UNLOCK(search
);
1948 lck_mtx_unlock(nd6_mutex
);
1954 nd6_prelist_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
,
1955 struct nd_prefix
**newp
, boolean_t force_scoped
)
1957 struct nd_prefix
*new = NULL
;
1958 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1959 struct nd_ifinfo
*ndi
= NULL
;
1962 if (ip6_maxifprefixes
>= 0) {
1963 ndi
= ND_IFINFO(ifp
);
1964 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1965 lck_mtx_lock(&ndi
->lock
);
1966 if (ndi
->nprefixes
>= ip6_maxifprefixes
) {
1967 lck_mtx_unlock(&ndi
->lock
);
1970 lck_mtx_unlock(&ndi
->lock
);
1973 new = ndpr_alloc(M_WAITOK
);
1980 new->ndpr_ifp
= pr
->ndpr_ifp
;
1981 new->ndpr_prefix
= pr
->ndpr_prefix
;
1982 new->ndpr_plen
= pr
->ndpr_plen
;
1983 new->ndpr_vltime
= pr
->ndpr_vltime
;
1984 new->ndpr_pltime
= pr
->ndpr_pltime
;
1985 new->ndpr_flags
= pr
->ndpr_flags
;
1986 if (pr
->ndpr_stateflags
& NDPRF_STATIC
) {
1987 new->ndpr_stateflags
|= NDPRF_STATIC
;
1990 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
1995 new->ndpr_lastupdate
= net_uptime();
1998 NDPR_ADDREF_LOCKED(new); /* for caller */
2000 /* initialization */
2001 LIST_INIT(&new->ndpr_advrtrs
);
2002 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
2003 /* make prefix in the canonical form */
2004 for (i
= 0; i
< 4; i
++) {
2005 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
2006 new->ndpr_mask
.s6_addr32
[i
];
2011 /* get nd6_service() to be scheduled as soon as it's convenient */
2012 ++nd6_sched_timeout_want
;
2014 lck_mtx_lock(nd6_mutex
);
2015 /* link ndpr_entry to nd_prefix list */
2016 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
2017 new->ndpr_debug
|= IFD_ATTACHED
;
2018 NDPR_ADDREF(new); /* for nd_prefix list */
2020 lck_mtx_lock(&ndi
->lock
);
2022 VERIFY(ndi
->nprefixes
!= 0);
2023 lck_mtx_unlock(&ndi
->lock
);
2025 /* ND_OPT_PI_FLAG_ONLINK processing */
2026 if (new->ndpr_raf_onlink
) {
2029 if ((e
= nd6_prefix_onlink_common(new, force_scoped
,
2030 new->ndpr_ifp
->if_index
)) != 0) {
2031 nd6log(error
, "nd6_prelist_add: failed to make "
2032 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
2033 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2034 new->ndpr_plen
, force_scoped
? "scoped" :
2035 "non-scoped", if_name(ifp
), e
);
2036 /* proceed anyway. XXX: is it correct? */
2041 pfxrtr_add(new, dr
);
2044 lck_mtx_unlock(nd6_mutex
);
2050 * Caller must have held an extra reference on nd_prefix.
2053 prelist_remove(struct nd_prefix
*pr
)
2055 struct nd_pfxrouter
*pfr
, *next
;
2056 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2058 struct nd_ifinfo
*ndi
= NULL
;
2060 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2061 NDPR_LOCK_ASSERT_HELD(pr
);
2063 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
2068 * If there are no more addresses, defunct the prefix. This is needed
2069 * because we don't want multiple threads calling prelist_remove() for
2070 * the same prefix and this might happen because we unlock nd6_mutex
2073 if (pr
->ndpr_addrcnt
== 0) {
2074 pr
->ndpr_stateflags
|= NDPRF_DEFUNCT
;
2077 /* make sure to invalidate the prefix until it is really freed. */
2078 pr
->ndpr_vltime
= 0;
2079 pr
->ndpr_pltime
= 0;
2082 * Though these flags are now meaningless, we'd rather keep the value
2083 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2084 * when executing "ndp -p".
2086 if (pr
->ndpr_stateflags
& NDPRF_ONLINK
) {
2087 NDPR_ADDREF_LOCKED(pr
);
2089 lck_mtx_unlock(nd6_mutex
);
2090 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2091 nd6log(error
, "prelist_remove: failed to make "
2092 "%s/%d offlink on %s, errno=%d\n",
2093 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2094 pr
->ndpr_plen
, if_name(ifp
), e
);
2095 /* what should we do? */
2097 lck_mtx_lock(nd6_mutex
);
2099 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2104 if (pr
->ndpr_addrcnt
> 0) {
2106 * The state might have changed if we called
2107 * nd6_prefix_offlink().
2109 pr
->ndpr_stateflags
&= ~NDPRF_DEFUNCT
;
2110 return; /* notice here? */
2113 /* unlink ndpr_entry from nd_prefix list */
2114 LIST_REMOVE(pr
, ndpr_entry
);
2115 pr
->ndpr_debug
&= ~IFD_ATTACHED
;
2117 /* free list of routers that adversed the prefix */
2118 for (pfr
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr
= next
) {
2119 next
= pfr
->pfr_next
;
2120 pfxrtr_del(pfr
, pr
);
2123 ndi
= ND_IFINFO(ifp
);
2124 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2125 lck_mtx_lock(&ndi
->lock
);
2126 VERIFY(ndi
->nprefixes
> 0);
2128 lck_mtx_unlock(&ndi
->lock
);
2130 /* This must not be the last reference to the nd_prefix */
2131 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2132 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__
, pr
);
2137 * Don't call pfxlist_onlink_check() here because we are
2138 * holding the NDPR lock and this could cause a deadlock when
2139 * there are multiple threads executing pfxlist_onlink_check().
2145 struct nd_prefix
*new,
2146 struct nd_defrouter
*dr
, /* may be NULL */
2150 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
2152 struct ifnet
*ifp
= new->ndpr_ifp
;
2153 struct nd_prefix
*pr
;
2157 struct in6_addrlifetime lt6_tmp
;
2158 uint64_t timenow
= net_uptime();
2160 /* no need to lock "new" here, as it is local to the caller */
2161 NDPR_LOCK_ASSERT_NOTHELD(new);
2166 * Authenticity for NA consists authentication for
2167 * both IP header and IP datagrams, doesn't it ?
2169 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
2170 auth
= (m
->m_flags
& M_AUTHIPHDR
) && (m
->m_flags
& M_AUTHIPDGM
);
2174 if ((pr
= nd6_prefix_lookup(new, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
2176 * nd6_prefix_lookup() ensures that pr and new have the same
2177 * prefix on a same interface.
2181 * Update prefix information. Note that the on-link (L) bit
2182 * and the autonomous (A) bit should NOT be changed from 1
2185 lck_mtx_lock(nd6_mutex
);
2187 if (new->ndpr_raf_onlink
== 1) {
2188 pr
->ndpr_raf_onlink
= 1;
2190 if (new->ndpr_raf_auto
== 1) {
2191 pr
->ndpr_raf_auto
= 1;
2193 if (new->ndpr_raf_onlink
) {
2194 pr
->ndpr_vltime
= new->ndpr_vltime
;
2195 pr
->ndpr_pltime
= new->ndpr_pltime
;
2196 (void) in6_init_prefix_ltimes(pr
); /* XXX error case? */
2197 pr
->ndpr_lastupdate
= net_uptime();
2200 NDPR_ADDREF_LOCKED(pr
);
2201 if (new->ndpr_raf_onlink
&&
2202 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
2206 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2208 "prelist_update: failed to make "
2209 "the prefix %s/%d on-link on %s "
2211 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2212 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
);
2213 /* proceed anyway. XXX: is it correct? */
2218 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
) {
2225 lck_mtx_unlock(nd6_mutex
);
2229 if (new->ndpr_vltime
== 0) {
2232 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0) {
2236 bzero(&new->ndpr_addr
, sizeof(struct in6_addr
));
2238 error
= nd6_prelist_add(new, dr
, &pr
, FALSE
);
2239 if (error
!= 0 || pr
== NULL
) {
2240 nd6log(info
, "prelist_update: "
2241 "nd6_prelist_add failed for %s/%d on %s "
2242 "errno=%d, returnpr=0x%llx\n",
2243 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2244 new->ndpr_plen
, if_name(new->ndpr_ifp
),
2245 error
, (uint64_t)VM_KERNEL_ADDRPERM(pr
));
2246 goto end
; /* we should just give up in this case. */
2251 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
2252 * Note that pr must be non NULL at this point.
2255 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
2256 if (!new->ndpr_raf_auto
) {
2261 * 5.5.3 (b). the link-local prefix should have been ignored in
2265 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2266 if (new->ndpr_pltime
> new->ndpr_vltime
) {
2267 error
= EINVAL
; /* XXX: won't be used */
2272 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2273 * an address configured by stateless autoconfiguration already in the
2274 * list of addresses associated with the interface, and the Valid
2275 * Lifetime is not 0, form an address. We first check if we have
2276 * a matching prefix.
2278 ifnet_lock_shared(ifp
);
2279 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2280 struct in6_ifaddr
*ifa6
;
2281 u_int32_t remaininglifetime
;
2284 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2288 ifa6
= (struct in6_ifaddr
*)ifa
;
2291 * We only consider autoconfigured addresses as per RFC 4862.
2293 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
2298 * Spec is not clear here, but I believe we should concentrate
2299 * on unicast (i.e. not anycast) addresses.
2300 * XXX: other ia6_flags? detached or duplicated?
2302 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0) {
2307 * Ignore the address if it is not associated with a prefix
2308 * or is associated with a prefix that is different from this
2309 * one. (pr is never NULL here)
2311 if (ifa6
->ia6_ndpr
!= pr
) {
2316 if (ia6_match
== NULL
) { /* remember the first one */
2318 IFA_ADDREF_LOCKED(ifa
); /* for ia6_match */
2322 * An already autoconfigured address matched. Now that we
2323 * are sure there is at least one matched address, we can
2324 * proceed to 5.5.3. (e): update the lifetimes according to the
2325 * "two hours" rule and the privacy extension.
2327 #define TWOHOUR (120*60)
2329 /* retrieve time as uptime (last arg is 0) */
2330 in6ifa_getlifetime(ifa6
, <6_tmp
, 0);
2332 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
2333 remaininglifetime
= ND6_INFINITE_LIFETIME
;
2334 } else if (timenow
- ifa6
->ia6_updatetime
> lt6_tmp
.ia6t_vltime
) {
2336 * The case of "invalid" address. We should usually
2337 * not see this case.
2339 remaininglifetime
= 0;
2341 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
2342 (timenow
- ifa6
->ia6_updatetime
);
2344 /* when not updating, keep the current stored lifetime. */
2345 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
2347 if (TWOHOUR
< new->ndpr_vltime
||
2348 remaininglifetime
< new->ndpr_vltime
) {
2349 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2350 } else if (remaininglifetime
<= TWOHOUR
) {
2352 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2356 * new->ndpr_vltime <= TWOHOUR &&
2357 * TWOHOUR < remaininglifetime
2359 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
2362 /* The 2 hour rule is not imposed for preferred lifetime. */
2363 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
2365 /* Special handling for lifetimes of temporary addresses. */
2366 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
2367 u_int32_t maxvltime
, maxpltime
;
2369 /* Constrain lifetimes to system limits. */
2370 if (lt6_tmp
.ia6t_vltime
> ip6_temp_valid_lifetime
) {
2371 lt6_tmp
.ia6t_vltime
= ip6_temp_valid_lifetime
;
2373 if (lt6_tmp
.ia6t_pltime
> ip6_temp_preferred_lifetime
) {
2374 lt6_tmp
.ia6t_pltime
=
2375 ip6_temp_preferred_lifetime
-
2380 * According to RFC 4941, section 3.3 (1), we only
2381 * update the lifetimes when they are in the maximum
2384 if (ip6_temp_valid_lifetime
>
2385 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2386 ip6_desync_factor
)) {
2387 maxvltime
= ip6_temp_valid_lifetime
-
2388 (timenow
- ifa6
->ia6_createtime
) -
2393 if (ip6_temp_preferred_lifetime
>
2394 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2395 ip6_desync_factor
)) {
2396 maxpltime
= ip6_temp_preferred_lifetime
-
2397 (timenow
- ifa6
->ia6_createtime
) -
2403 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
2404 lt6_tmp
.ia6t_vltime
> maxvltime
) {
2405 lt6_tmp
.ia6t_vltime
= maxvltime
;
2408 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
2409 lt6_tmp
.ia6t_pltime
> maxpltime
) {
2410 lt6_tmp
.ia6t_pltime
= maxpltime
;
2414 in6_init_address_ltimes(pr
, <6_tmp
);
2416 in6ifa_setlifetime(ifa6
, <6_tmp
);
2417 ifa6
->ia6_updatetime
= timenow
;
2420 ifnet_lock_done(ifp
);
2421 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
2423 * 5.5.3 (d) (continued)
2424 * No address matched and the valid lifetime is non-zero.
2425 * Create a new address.
2427 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
, FALSE
))
2430 * note that we should use pr (not new) for reference.
2432 IFA_LOCK(&ia6
->ia_ifa
);
2435 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2437 VERIFY(pr
->ndpr_addrcnt
!= 0);
2439 IFA_UNLOCK(&ia6
->ia_ifa
);
2443 * When a new public address is created as described
2444 * in RFC 4862, also create a new temporary address.
2447 * When an interface connects to a new link, a new
2448 * randomized interface identifier should be generated
2449 * immediately together with a new set of temporary
2450 * addresses. Thus, we specifiy 1 as the 2nd arg of
2453 if (ip6_use_tempaddr
) {
2455 if ((e
= in6_tmpifadd(ia6
, 1)) != 0) {
2456 nd6log(info
, "prelist_update: "
2457 "failed to create a temporary "
2458 "address, errno=%d\n",
2462 IFA_REMREF(&ia6
->ia_ifa
);
2466 * If the interface is marked for CLAT46 configuration
2467 * try and configure the reserved IPv6 address for
2468 * stateless translation.
2470 if (IS_INTF_CLAT46(ifp
)) {
2471 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
, TRUE
)) != NULL
) {
2472 IFA_LOCK(&ia6
->ia_ifa
);
2475 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2477 VERIFY(pr
->ndpr_addrcnt
!= 0);
2478 pr
->ndpr_stateflags
|= NDPRF_CLAT46
;
2480 IFA_UNLOCK(&ia6
->ia_ifa
);
2481 IFA_REMREF(&ia6
->ia_ifa
);
2483 } else if (error
!= EEXIST
) {
2484 uuid_t tmp_uuid
= {};
2486 * Only report the error if it is not
2489 ip6stat
.ip6s_clat464_v6addr_conffail
++;
2490 in6_clat46_event_enqueue_nwk_wq_entry(
2491 IN6_CLAT46_EVENT_V6_ADDR_CONFFAIL
,
2494 nd6log0(error
, "Could not configure CLAT46 address on interface %s.\n", ifp
->if_xname
);
2497 * Reset the error as we do not want to
2498 * treat failure of CLAT46 address configuration
2499 * as complete failure in prelist update path.
2505 * A newly added address might affect the status
2506 * of other addresses, so we check and update it.
2507 * XXX: what if address duplication happens?
2509 lck_mtx_lock(nd6_mutex
);
2510 pfxlist_onlink_check();
2511 lck_mtx_unlock(nd6_mutex
);
2518 if (ia6_match
!= NULL
) {
2519 IFA_REMREF(&ia6_match
->ia_ifa
);
2525 * Neighbor Discover Default Router structure reference counting routines.
2527 static struct nd_defrouter
*
2530 struct nd_defrouter
*dr
;
2532 dr
= (how
== M_WAITOK
) ? zalloc(nddr_zone
) : zalloc_noblock(nddr_zone
);
2534 bzero(dr
, nddr_size
);
2535 lck_mtx_init(&dr
->nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2536 dr
->nddr_debug
|= IFD_ALLOC
;
2537 if (nddr_debug
!= 0) {
2538 dr
->nddr_debug
|= IFD_DEBUG
;
2539 dr
->nddr_trace
= nddr_trace
;
2546 nddr_free(struct nd_defrouter
*dr
)
2549 if (dr
->nddr_debug
& IFD_ATTACHED
) {
2550 panic("%s: attached nddr %p is being freed", __func__
, dr
);
2552 } else if (!(dr
->nddr_debug
& IFD_ALLOC
)) {
2553 panic("%s: nddr %p cannot be freed", __func__
, dr
);
2556 dr
->nddr_debug
&= ~IFD_ALLOC
;
2559 lck_mtx_destroy(&dr
->nddr_lock
, ifa_mtx_grp
);
2560 zfree(nddr_zone
, dr
);
2564 nddr_trace(struct nd_defrouter
*dr
, int refhold
)
2566 struct nd_defrouter_dbg
*dr_dbg
= (struct nd_defrouter_dbg
*)dr
;
2571 if (!(dr
->nddr_debug
& IFD_DEBUG
)) {
2572 panic("%s: nddr %p has no debug structure", __func__
, dr
);
2576 cnt
= &dr_dbg
->nddr_refhold_cnt
;
2577 tr
= dr_dbg
->nddr_refhold
;
2579 cnt
= &dr_dbg
->nddr_refrele_cnt
;
2580 tr
= dr_dbg
->nddr_refrele
;
2583 idx
= atomic_add_16_ov(cnt
, 1) % NDDR_TRACE_HIST_SIZE
;
2584 ctrace_record(&tr
[idx
]);
2588 nddr_addref(struct nd_defrouter
*nddr
, int locked
)
2591 NDDR_LOCK_SPIN(nddr
);
2593 NDDR_LOCK_ASSERT_HELD(nddr
);
2596 if (++nddr
->nddr_refcount
== 0) {
2597 panic("%s: nddr %p wraparound refcnt\n", __func__
, nddr
);
2599 } else if (nddr
->nddr_trace
!= NULL
) {
2600 (*nddr
->nddr_trace
)(nddr
, TRUE
);
2608 struct nd_defrouter
*
2609 nddr_remref(struct nd_defrouter
*nddr
, int locked
)
2612 NDDR_LOCK_SPIN(nddr
);
2614 NDDR_LOCK_ASSERT_HELD(nddr
);
2617 if (nddr
->nddr_refcount
== 0) {
2618 panic("%s: nddr %p negative refcnt\n", __func__
, nddr
);
2620 } else if (nddr
->nddr_trace
!= NULL
) {
2621 (*nddr
->nddr_trace
)(nddr
, FALSE
);
2624 if (--nddr
->nddr_refcount
== 0) {
2630 if (!locked
&& nddr
!= NULL
) {
2638 nddr_getexpire(struct nd_defrouter
*dr
)
2640 struct timeval caltime
;
2643 if (dr
->expire
!= 0) {
2644 /* account for system time change */
2645 getmicrotime(&caltime
);
2647 dr
->base_calendartime
+=
2648 NET_CALCULATE_CLOCKSKEW(caltime
,
2649 dr
->base_calendartime
, net_uptime(), dr
->base_uptime
);
2651 expiry
= dr
->base_calendartime
+
2652 dr
->expire
- dr
->base_uptime
;
2660 * Neighbor Discover Prefix structure reference counting routines.
2662 static struct nd_prefix
*
2665 struct nd_prefix
*pr
;
2667 pr
= (how
== M_WAITOK
) ? zalloc(ndpr_zone
) : zalloc_noblock(ndpr_zone
);
2669 bzero(pr
, ndpr_size
);
2670 lck_mtx_init(&pr
->ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2671 RB_INIT(&pr
->ndpr_prproxy_sols
);
2672 pr
->ndpr_debug
|= IFD_ALLOC
;
2673 if (ndpr_debug
!= 0) {
2674 pr
->ndpr_debug
|= IFD_DEBUG
;
2675 pr
->ndpr_trace
= ndpr_trace
;
2682 ndpr_free(struct nd_prefix
*pr
)
2685 if (pr
->ndpr_debug
& IFD_ATTACHED
) {
2686 panic("%s: attached ndpr %p is being freed", __func__
, pr
);
2688 } else if (!(pr
->ndpr_debug
& IFD_ALLOC
)) {
2689 panic("%s: ndpr %p cannot be freed", __func__
, pr
);
2691 } else if (pr
->ndpr_rt
!= NULL
) {
2692 panic("%s: ndpr %p route %p not freed", __func__
, pr
,
2695 } else if (pr
->ndpr_prproxy_sols_cnt
!= 0) {
2696 panic("%s: ndpr %p non-zero solicitors count (%d)",
2697 __func__
, pr
, pr
->ndpr_prproxy_sols_cnt
);
2699 } else if (!RB_EMPTY(&pr
->ndpr_prproxy_sols
)) {
2700 panic("%s: ndpr %p non-empty solicitors tree", __func__
, pr
);
2703 pr
->ndpr_debug
&= ~IFD_ALLOC
;
2706 lck_mtx_destroy(&pr
->ndpr_lock
, ifa_mtx_grp
);
2707 zfree(ndpr_zone
, pr
);
2711 ndpr_trace(struct nd_prefix
*pr
, int refhold
)
2713 struct nd_prefix_dbg
*pr_dbg
= (struct nd_prefix_dbg
*)pr
;
2718 if (!(pr
->ndpr_debug
& IFD_DEBUG
)) {
2719 panic("%s: ndpr %p has no debug structure", __func__
, pr
);
2723 cnt
= &pr_dbg
->ndpr_refhold_cnt
;
2724 tr
= pr_dbg
->ndpr_refhold
;
2726 cnt
= &pr_dbg
->ndpr_refrele_cnt
;
2727 tr
= pr_dbg
->ndpr_refrele
;
2730 idx
= atomic_add_16_ov(cnt
, 1) % NDPR_TRACE_HIST_SIZE
;
2731 ctrace_record(&tr
[idx
]);
2735 ndpr_addref(struct nd_prefix
*ndpr
, int locked
)
2738 NDPR_LOCK_SPIN(ndpr
);
2740 NDPR_LOCK_ASSERT_HELD(ndpr
);
2743 if (++ndpr
->ndpr_refcount
== 0) {
2744 panic("%s: ndpr %p wraparound refcnt\n", __func__
, ndpr
);
2746 } else if (ndpr
->ndpr_trace
!= NULL
) {
2747 (*ndpr
->ndpr_trace
)(ndpr
, TRUE
);
2756 ndpr_remref(struct nd_prefix
*ndpr
, int locked
)
2759 NDPR_LOCK_SPIN(ndpr
);
2761 NDPR_LOCK_ASSERT_HELD(ndpr
);
2764 if (ndpr
->ndpr_refcount
== 0) {
2765 panic("%s: ndpr %p negative refcnt\n", __func__
, ndpr
);
2767 } else if (ndpr
->ndpr_trace
!= NULL
) {
2768 (*ndpr
->ndpr_trace
)(ndpr
, FALSE
);
2771 if (--ndpr
->ndpr_refcount
== 0) {
2772 if (ndpr
->ndpr_addrcnt
!= 0) {
2773 panic("%s: freeing ndpr %p with outstanding address "
2774 "reference (%d)", __func__
, ndpr
,
2775 ndpr
->ndpr_addrcnt
);
2783 if (!locked
&& ndpr
!= NULL
) {
2791 ndpr_getexpire(struct nd_prefix
*pr
)
2793 struct timeval caltime
;
2796 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_vltime
!= ND6_INFINITE_LIFETIME
) {
2797 /* account for system time change */
2798 getmicrotime(&caltime
);
2800 pr
->ndpr_base_calendartime
+=
2801 NET_CALCULATE_CLOCKSKEW(caltime
,
2802 pr
->ndpr_base_calendartime
, net_uptime(),
2803 pr
->ndpr_base_uptime
);
2805 expiry
= pr
->ndpr_base_calendartime
+
2806 pr
->ndpr_expire
- pr
->ndpr_base_uptime
;
2814 * A supplement function used in the on-link detection below;
2815 * detect if a given prefix has a (probably) reachable advertising router.
2816 * XXX: lengthy function name...
2818 * Callers *must* increase the reference count of nd_prefix.
2820 static struct nd_pfxrouter
*
2821 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
2823 struct nd_pfxrouter
*pfxrtr
;
2825 struct llinfo_nd6
*ln
;
2827 struct in6_addr rtaddr
;
2830 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2831 NDPR_LOCK_ASSERT_HELD(pr
);
2833 genid
= pr
->ndpr_genid
;
2834 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2836 ifp
= pfxrtr
->router
->ifp
;
2837 if (pfxrtr
->router
->stateflags
& NDDRF_MAPPED
) {
2838 rtaddr
= pfxrtr
->router
->rtaddr_mapped
;
2840 rtaddr
= pfxrtr
->router
->rtaddr
;
2844 lck_mtx_unlock(nd6_mutex
);
2845 /* Callee returns a locked route upon success */
2846 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
2847 RT_LOCK_ASSERT_HELD(rt
);
2848 if ((ln
= rt
->rt_llinfo
) != NULL
&&
2849 ND6_IS_LLINFO_PROBREACH(ln
)) {
2850 RT_REMREF_LOCKED(rt
);
2852 lck_mtx_lock(nd6_mutex
);
2856 RT_REMREF_LOCKED(rt
);
2859 lck_mtx_lock(nd6_mutex
);
2861 if (pr
->ndpr_genid
!= genid
) {
2862 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2863 genid
= pr
->ndpr_genid
;
2865 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
);
2868 NDPR_LOCK_ASSERT_HELD(pr
);
2874 * Check if each prefix in the prefix list has at least one available router
2875 * that advertised the prefix (a router is "available" if its neighbor cache
2876 * entry is reachable or probably reachable).
2877 * If the check fails, the prefix may be off-link, because, for example,
2878 * we have moved from the network but the lifetime of the prefix has not
2879 * expired yet. So we should not use the prefix if there is another prefix
2880 * that has an available router.
2881 * But, if there is no prefix that has an available router, we still regards
2882 * all the prefixes as on-link. This is because we can't tell if all the
2883 * routers are simply dead or if we really moved from the network and there
2884 * is no router around us.
2887 pfxlist_onlink_check(void)
2889 struct nd_prefix
*pr
, *prclear
;
2890 struct in6_ifaddr
*ifa
;
2891 struct nd_defrouter
*dr
;
2892 struct nd_pfxrouter
*pfxrtr
= NULL
;
2893 int err
, i
, found
= 0;
2894 struct ifaddr
**ifap
= NULL
;
2895 struct nd_prefix
*ndpr
;
2897 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2899 while (nd_prefix_busy
) {
2900 nd_prefix_waiters
++;
2901 msleep(nd_prefix_waitchan
, nd6_mutex
, (PZERO
- 1),
2903 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2905 nd_prefix_busy
= TRUE
;
2908 * Check if there is a prefix that has a reachable advertising
2911 pr
= nd_prefix
.lh_first
;
2914 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
) {
2919 NDPR_ADDREF_LOCKED(pr
);
2920 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
) &&
2921 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
2922 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2929 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2933 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
2934 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
2935 * stop us from checking the same prefix twice.
2937 pr
= nd_prefix
.lh_first
;
2939 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2941 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2942 NDPR_UNLOCK(prclear
);
2945 * If we have no such prefix, check whether we still have a router
2946 * that does not advertise any prefixes.
2949 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
2950 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2951 struct nd_prefix
*pr0
;
2953 for (pr0
= nd_prefix
.lh_first
; pr0
;
2954 pr0
= pr0
->ndpr_next
) {
2956 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
) {
2962 if (pfxrtr
!= NULL
) {
2967 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && pfxrtr
== NULL
)) {
2969 * There is at least one prefix that has a reachable router,
2970 * or at least a router which probably does not advertise
2971 * any prefixes. The latter would be the case when we move
2972 * to a new link where we have a router that does not provide
2973 * prefixes and we configure an address by hand.
2974 * Detach prefixes which have no reachable advertising
2975 * router, and attach other prefixes.
2977 pr
= nd_prefix
.lh_first
;
2981 * We aren't interested prefixes already processed,
2982 * nor in prefixes without the L bit
2983 * set nor in static prefixes
2985 if (pr
->ndpr_raf_onlink
== 0 ||
2986 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2987 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2992 NDPR_ADDREF_LOCKED(pr
);
2993 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2994 find_pfxlist_reachable_router(pr
) == NULL
&&
2995 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
2996 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
2998 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2999 find_pfxlist_reachable_router(pr
) != NULL
&&
3000 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
3001 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
3003 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
3007 * Since find_pfxlist_reachable_router() drops the
3008 * nd6_mutex, we have to start over, but the
3009 * NDPRF_PROCESSED_ONLINK flag will stop us from
3010 * checking the same prefix twice.
3012 pr
= nd_prefix
.lh_first
;
3015 /* there is no prefix that has a reachable router */
3016 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3018 if (pr
->ndpr_raf_onlink
== 0 ||
3019 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
3023 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0) {
3024 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
3029 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
3031 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
3032 NDPR_UNLOCK(prclear
);
3035 * Remove each interface route associated with a (just) detached
3036 * prefix, and reinstall the interface route for a (just) attached
3037 * prefix. Note that all attempt of reinstallation does not
3038 * necessarily success, when a same prefix is shared among multiple
3039 * interfaces. Such cases will be handled in nd6_prefix_onlink,
3040 * so we don't have to care about them.
3042 pr
= nd_prefix
.lh_first
;
3047 if (pr
->ndpr_raf_onlink
== 0 ||
3048 pr
->ndpr_stateflags
& NDPRF_STATIC
||
3049 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
3050 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
3055 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
3056 NDPR_ADDREF_LOCKED(pr
);
3057 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
3058 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3060 lck_mtx_unlock(nd6_mutex
);
3061 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
3063 "pfxlist_onlink_check: failed to "
3064 "make %s/%d offlink, errno=%d\n",
3065 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3068 lck_mtx_lock(nd6_mutex
);
3070 pr
= nd_prefix
.lh_first
;
3073 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
3074 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
3075 pr
->ndpr_raf_onlink
) {
3077 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
3079 "pfxlist_onlink_check: failed to "
3080 "make %s/%d offlink, errno=%d\n",
3081 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3085 pr
= nd_prefix
.lh_first
;
3093 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
3095 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
3096 NDPR_UNLOCK(prclear
);
3098 VERIFY(nd_prefix_busy
);
3099 nd_prefix_busy
= FALSE
;
3100 if (nd_prefix_waiters
> 0) {
3101 nd_prefix_waiters
= 0;
3102 wakeup(nd_prefix_waitchan
);
3106 * Changes on the prefix status might affect address status as well.
3107 * Make sure that all addresses derived from an attached prefix are
3108 * attached, and that all addresses derived from a detached prefix are
3109 * detached. Note, however, that a manually configured address should
3110 * always be attached.
3111 * The precise detection logic is same as the one for prefixes.
3113 * ifnet_get_address_list_family_internal() may fail due to memory
3114 * pressure, but we will eventually be called again when we receive
3115 * another NA, RA, or when the link status changes.
3117 err
= ifnet_get_address_list_family_internal(NULL
, &ifap
, AF_INET6
, 0,
3119 if (err
!= 0 || ifap
== NULL
) {
3120 nd6log(error
, "%s: ifnet_get_address_list_family_internal "
3121 "failed", __func__
);
3124 for (i
= 0; ifap
[i
]; i
++) {
3125 ifa
= ifatoia6(ifap
[i
]);
3126 IFA_LOCK(&ifa
->ia_ifa
);
3127 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3128 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3129 IFA_UNLOCK(&ifa
->ia_ifa
);
3132 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3134 * This can happen when we first configure the address
3135 * (i.e. the address exists, but the prefix does not).
3136 * XXX: complicated relationships...
3138 IFA_UNLOCK(&ifa
->ia_ifa
);
3141 IFA_UNLOCK(&ifa
->ia_ifa
);
3144 NDPR_ADDREF_LOCKED(ndpr
);
3145 if (find_pfxlist_reachable_router(ndpr
)) {
3146 if (NDPR_REMREF_LOCKED(ndpr
) == NULL
) {
3158 for (i
= 0; ifap
[i
]; i
++) {
3159 ifa
= ifatoia6(ifap
[i
]);
3160 IFA_LOCK(&ifa
->ia_ifa
);
3161 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3162 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3163 IFA_UNLOCK(&ifa
->ia_ifa
);
3166 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3167 /* XXX: see above. */
3168 IFA_UNLOCK(&ifa
->ia_ifa
);
3171 IFA_UNLOCK(&ifa
->ia_ifa
);
3173 NDPR_ADDREF_LOCKED(ndpr
);
3174 if (find_pfxlist_reachable_router(ndpr
)) {
3176 IFA_LOCK(&ifa
->ia_ifa
);
3177 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3178 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3179 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3180 IFA_UNLOCK(&ifa
->ia_ifa
);
3181 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3183 IFA_UNLOCK(&ifa
->ia_ifa
);
3187 IFA_LOCK(&ifa
->ia_ifa
);
3188 if ((ifa
->ia6_flags
& IN6_IFF_DETACHED
) == 0) {
3189 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
3190 in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DETACHED
,
3191 ifa
->ia_ifa
.ifa_ifp
, &ifa
->ia_addr
.sin6_addr
,
3194 IFA_UNLOCK(&ifa
->ia_ifa
);
3199 for (i
= 0; ifap
[i
]; i
++) {
3200 ifa
= ifatoia6(ifap
[i
]);
3201 IFA_LOCK(&ifa
->ia_ifa
);
3202 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3203 IFA_UNLOCK(&ifa
->ia_ifa
);
3206 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3207 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3208 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3209 IFA_UNLOCK(&ifa
->ia_ifa
);
3210 /* Do we need a delay in this case? */
3211 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3213 IFA_UNLOCK(&ifa
->ia_ifa
);
3217 ifnet_free_address_list(ifap
);
3220 static struct nd_prefix
*
3221 nd6_prefix_equal_lookup(struct nd_prefix
*pr
, boolean_t primary_only
)
3223 struct nd_prefix
*opr
;
3225 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3227 for (opr
= nd_prefix
.lh_first
; opr
; opr
= opr
->ndpr_next
) {
3233 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3237 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
3238 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
3239 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
) &&
3241 !(opr
->ndpr_stateflags
& NDPRF_IFSCOPE
))) {
3242 NDPR_ADDREF_LOCKED(opr
);
3252 * Synchronize the interface routes of similar prefixes on different
3253 * interfaces; the one using the default interface would be (re)installed
3254 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3257 nd6_prefix_sync(struct ifnet
*ifp
)
3259 struct nd_prefix
*pr
, *opr
;
3262 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3268 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3270 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
3274 if (pr
->ndpr_ifp
== ifp
&&
3275 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) &&
3276 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3287 /* Remove conflicting entries */
3288 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3290 lck_mtx_unlock(nd6_mutex
);
3291 err
= nd6_prefix_offlink(opr
);
3292 lck_mtx_lock(nd6_mutex
);
3295 "%s: failed to make %s/%d offlink on %s, "
3296 "errno=%d\n", __func__
,
3297 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3298 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
);
3302 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3303 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3304 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
));
3307 lck_mtx_unlock(nd6_mutex
);
3308 err
= nd6_prefix_offlink(pr
);
3309 lck_mtx_lock(nd6_mutex
);
3312 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3313 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3314 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
);
3317 /* Add the entries back */
3319 err
= nd6_prefix_onlink_scoped(opr
, opr
->ndpr_ifp
->if_index
);
3322 "%s: failed to make %s/%d scoped onlink on %s, "
3323 "errno=%d\n", __func__
,
3324 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3325 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
);
3329 err
= nd6_prefix_onlink_scoped(pr
, IFSCOPE_NONE
);
3332 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3333 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3334 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
);
3339 "%s: error promoting %s/%d to %s from %s\n",
3340 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3341 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3342 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE");
3345 "%s: %s/%d promoted, previously on %s\n",
3346 if_name(pr
->ndpr_ifp
),
3347 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
,
3348 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE");
3357 nd6_prefix_onlink_common(struct nd_prefix
*pr
, boolean_t force_scoped
,
3358 unsigned int ifscope
)
3361 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3362 struct sockaddr_in6 mask6
, prefix
;
3363 struct nd_prefix
*opr
;
3365 int error
= 0, prproxy
= 0;
3366 struct rtentry
*rt
= NULL
;
3368 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3372 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3374 "%s: %s/%d on %s scoped=%d is already on-link\n",
3375 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3376 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3377 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3384 * Add the interface route associated with the prefix. Before
3385 * installing the route, check if there's the same prefix on another
3386 * interface, and the prefix has already installed the interface route.
3388 opr
= nd6_prefix_equal_lookup(pr
, FALSE
);
3393 if (!force_scoped
) {
3395 * If a primary/non-scoped interface route already exists,
3396 * install the new one as a scoped entry. If the existing
3397 * interface route is scoped, install new as non-scoped.
3399 ifscope
= (opr
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
3400 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3403 } else if (ifscope
!= IFSCOPE_NONE
) {
3404 ifscope
= IFSCOPE_NONE
;
3409 * We prefer link-local addresses as the associated interface address.
3411 /* search for a link-local addr */
3412 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
3413 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3415 struct in6_ifaddr
*ia6
;
3416 ifnet_lock_shared(ifp
);
3417 IFP_TO_IA6(ifp
, ia6
);
3418 ifnet_lock_done(ifp
);
3422 /* should we care about ia6_flags? */
3427 * This can still happen, when, for example, we receive an RA
3428 * containing a prefix with the L bit set and the A bit clear,
3429 * after removing all IPv6 addresses on the receiving
3430 * interface. This should, of course, be rare though.
3433 "nd6_prefix_onlink: failed to find any ifaddr"
3434 " to add route for a prefix(%s/%d) on %s\n",
3435 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3436 pr
->ndpr_plen
, if_name(ifp
));
3442 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3443 * ifa->ifa_rtrequest = nd6_rtrequest;
3445 bzero(&mask6
, sizeof(mask6
));
3446 mask6
.sin6_len
= sizeof(mask6
);
3447 mask6
.sin6_addr
= pr
->ndpr_mask
;
3448 prefix
= pr
->ndpr_prefix
;
3449 if ((rt
= pr
->ndpr_rt
) != NULL
) {
3452 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3456 rtflags
= ifa
->ifa_flags
| RTF_CLONING
| RTF_UP
;
3458 if (nd6_need_cache(ifp
)) {
3459 /* explicitly set in case ifa_flags does not set the flag. */
3460 rtflags
|= RTF_CLONING
;
3463 * explicitly clear the cloning bit in case ifa_flags sets it.
3465 rtflags
&= ~RTF_CLONING
;
3468 lck_mtx_unlock(nd6_mutex
);
3475 error
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&prefix
,
3476 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
,
3480 * Serialize the setting of NDPRF_PRPROXY.
3482 lck_mtx_lock(&proxy6_lock
);
3486 nd6_rtmsg(RTM_ADD
, rt
);
3491 nd6log(error
, "nd6_prefix_onlink: failed to add route for a"
3492 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%x,"
3493 " scoped=%d, errno = %d\n",
3494 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3495 pr
->ndpr_plen
, if_name(ifp
),
3496 ip6_sprintf(&((struct sockaddr_in6
*)
3497 (void *)ifa
->ifa_addr
)->sin6_addr
),
3498 ip6_sprintf(&mask6
.sin6_addr
), rtflags
,
3499 (ifscope
!= IFSCOPE_NONE
), error
);
3501 NDPR_LOCK_ASSERT_HELD(pr
);
3503 pr
->ndpr_stateflags
&= ~(NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3506 * TODO: If the prefix route exists, we should really find it and
3507 * refer the prefix to it; otherwise ndpr_rt is NULL.
3509 if (!(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) &&
3510 (rt
!= NULL
|| error
== EEXIST
)) {
3511 struct nd_ifinfo
*ndi
= NULL
;
3513 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3514 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3516 ndi
= ND_IFINFO(ifp
);
3517 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3518 lck_mtx_lock(&ndi
->lock
);
3520 pr
->ndpr_rt
= rt
; /* keep reference from rtrequest */
3521 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
3522 if (ifscope
!= IFSCOPE_NONE
) {
3523 pr
->ndpr_stateflags
|= NDPRF_IFSCOPE
;
3524 } else if ((rtflags
& RTF_CLONING
) &&
3525 (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) &&
3526 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3528 * At present, in order for the prefix to be eligible
3529 * as a proxying/proxied prefix, we require that the
3530 * prefix route entry be marked as a cloning route with
3531 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3532 * true for the interface type, hence the test for
3533 * RTF_CLONING above.
3535 pr
->ndpr_stateflags
|= NDPRF_PRPROXY
;
3538 lck_mtx_unlock(&ndi
->lock
);
3539 } else if (rt
!= NULL
&& pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
3543 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3544 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3550 * If this is an upstream prefix, find the downstream ones (if any)
3551 * and re-configure their prefix routes accordingly. Otherwise,
3552 * this could be potentially be a downstream prefix, and so find the
3553 * upstream prefix, if any.
3555 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3557 NDPR_REMREF(pr
); /* release reference for this routine */
3558 lck_mtx_unlock(&proxy6_lock
);
3560 lck_mtx_lock(nd6_mutex
);
3566 nd6_prefix_onlink(struct nd_prefix
*pr
)
3568 return nd6_prefix_onlink_common(pr
, FALSE
, IFSCOPE_NONE
);
3572 nd6_prefix_onlink_scoped(struct nd_prefix
*pr
, unsigned int ifscope
)
3574 return nd6_prefix_onlink_common(pr
, TRUE
, ifscope
);
3578 nd6_prefix_offlink(struct nd_prefix
*pr
)
3580 int plen
, error
= 0, prproxy
;
3581 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3582 struct sockaddr_in6 sa6
, mask6
, prefix
;
3583 struct rtentry
*rt
= NULL
, *ndpr_rt
= NULL
;
3584 unsigned int ifscope
;
3586 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3590 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3592 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3593 "off-link\n", ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3594 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3595 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3600 bzero(&sa6
, sizeof(sa6
));
3601 sa6
.sin6_family
= AF_INET6
;
3602 sa6
.sin6_len
= sizeof(sa6
);
3603 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
,
3604 sizeof(struct in6_addr
));
3605 bzero(&mask6
, sizeof(mask6
));
3606 mask6
.sin6_family
= AF_INET6
;
3607 mask6
.sin6_len
= sizeof(sa6
);
3608 bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof(struct in6_addr
));
3609 prefix
= pr
->ndpr_prefix
;
3610 plen
= pr
->ndpr_plen
;
3611 if ((ndpr_rt
= pr
->ndpr_rt
) != NULL
) {
3614 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3617 ifscope
= (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ?
3618 ifp
->if_index
: IFSCOPE_NONE
;
3620 error
= rtrequest_scoped(RTM_DELETE
, (struct sockaddr
*)&sa6
,
3621 NULL
, (struct sockaddr
*)&mask6
, 0, &rt
, ifscope
);
3624 /* report the route deletion to the routing socket. */
3626 nd6_rtmsg(RTM_DELETE
, rt
);
3631 "nd6_prefix_offlink: failed to delete route: "
3632 "%s/%d on %s, scoped %d, (errno = %d)\n",
3633 ip6_sprintf(&sa6
.sin6_addr
), plen
, if_name(ifp
),
3634 (ifscope
!= IFSCOPE_NONE
), error
);
3637 if (ndpr_rt
!= NULL
) {
3641 lck_mtx_lock(&proxy6_lock
);
3644 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3645 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3646 pr
->ndpr_stateflags
&= ~(NDPRF_ONLINK
| NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3647 if (pr
->ndpr_prproxy_sols_cnt
> 0) {
3649 nd6_prproxy_sols_reap(pr
);
3650 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3651 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3656 * If this was an upstream prefix, find the downstream ones and do
3657 * some cleanups. If this was a downstream prefix, the prefix route
3658 * has been removed from the routing table above, but there may be
3659 * other tasks to perform.
3661 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3663 NDPR_REMREF(pr
); /* release reference for this routine */
3664 lck_mtx_unlock(&proxy6_lock
);
3670 in6_pfx_newpersistaddr(struct nd_prefix
*pr
, int mcast
, int *errorp
, boolean_t is_clat46
)
3672 struct in6_ifaddr
*ia6
= NULL
;
3673 struct ifnet
*ifp
= NULL
;
3674 struct nd_ifinfo
*ndi
= NULL
;
3675 struct in6_addr mask
;
3676 struct in6_aliasreq ifra
;
3677 int error
, ifaupdate
, iidlen
, notcga
;
3680 VERIFY(errorp
!= NULL
);
3688 * Prefix Length check:
3689 * If the sum of the prefix length and interface identifier
3690 * length does not equal 128 bits, the Prefix Information
3691 * option MUST be ignored. The length of the interface
3692 * identifier is defined in a separate link-type specific
3695 iidlen
= in6_if2idlen(ifp
);
3697 error
= EADDRNOTAVAIL
;
3698 /* this should not happen, so we always log it. */
3699 log(LOG_ERR
, "%s: IID length undefined (%s)\n",
3700 __func__
, if_name(ifp
));
3702 } else if (iidlen
!= 64) {
3703 error
= EADDRNOTAVAIL
;
3705 * stateless autoconfiguration not yet well-defined for IID
3706 * lengths other than 64 octets. Just give up for now.
3708 nd6log(info
, "%s: IID length not 64 octets (%s)\n",
3709 __func__
, if_name(ifp
));
3713 if (iidlen
+ pr
->ndpr_plen
!= 128) {
3714 error
= EADDRNOTAVAIL
;
3716 "%s: invalid prefix length %d for %s, ignored\n",
3717 __func__
, pr
->ndpr_plen
, if_name(ifp
));
3721 bzero(&ifra
, sizeof(ifra
));
3722 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
3723 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
3724 ifra
.ifra_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
3727 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
,
3728 sizeof(ifra
.ifra_addr
.sin6_addr
));
3729 in6_len2mask(&mask
, pr
->ndpr_plen
);
3730 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
3731 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
3732 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
3733 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
3735 ndi
= ND_IFINFO(ifp
);
3736 VERIFY(ndi
->initialized
);
3737 lck_mtx_lock(&ndi
->lock
);
3739 notcga
= nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
||
3740 (ndi
->flags
& ND6_IFF_INSECURE
) != 0;
3742 lck_mtx_unlock(&ndi
->lock
);
3745 if (notcga
&& !is_clat46
) {
3746 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
3748 error
= EADDRNOTAVAIL
;
3749 nd6log(info
, "%s: no link-local address (%s)\n",
3750 __func__
, if_name(ifp
));
3754 IFA_LOCK(&ia6
->ia_ifa
);
3755 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
3756 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
3757 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
3758 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
3759 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3760 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
3761 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3762 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
3763 IFA_UNLOCK(&ia6
->ia_ifa
);
3764 IFA_REMREF(&ia6
->ia_ifa
);
3767 in6_cga_node_lock();
3768 struct in6_cga_prepare local_cga_prepare
;
3771 * XXX For now the collision count is not used in the classical
3772 * way for secure addresses.
3773 * Use a different collision count value to generate reserved
3774 * address for stateless CLAT46
3776 if (ndi
->cga_initialized
) {
3777 bcopy(&(ndi
->local_cga_modifier
),
3778 &(local_cga_prepare
.cga_modifier
),
3779 sizeof(local_cga_prepare
.cga_modifier
));
3781 error
= in6_cga_generate(&local_cga_prepare
, 0,
3782 &ifra
.ifra_addr
.sin6_addr
);
3784 error
= in6_cga_generate(&local_cga_prepare
, 1,
3785 &ifra
.ifra_addr
.sin6_addr
);
3789 error
= in6_cga_generate(NULL
, 0,
3790 &ifra
.ifra_addr
.sin6_addr
);
3792 error
= in6_cga_generate(NULL
, 1,
3793 &ifra
.ifra_addr
.sin6_addr
);
3796 in6_cga_node_unlock();
3798 ifra
.ifra_flags
|= IN6_IFF_SECURED
;
3800 ifra
.ifra_flags
|= IN6_IFF_CLAT46
;
3804 nd6log(error
, "%s: no CGA available (%s)\n",
3805 __func__
, if_name(ifp
));
3807 nd6log(error
, "%s: no CLAT46 available (%s)\n",
3808 __func__
, if_name(ifp
));
3814 VERIFY(ia6
== NULL
);
3816 /* new prefix mask. */
3817 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
3818 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
3819 bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
,
3820 sizeof(ifra
.ifra_prefixmask
.sin6_addr
));
3823 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
3824 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
3827 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
3830 * Make sure that we do not have this address already. This should
3831 * usually not happen, but we can still see this case, e.g., if we
3832 * have manually configured the exact address to be configured.
3834 if ((ia6
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
))
3837 IFA_REMREF(&ia6
->ia_ifa
);
3840 /* this should be rare enough to make an explicit log */
3841 log(LOG_INFO
, "%s: %s is already configured!\n",
3842 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
3847 * Allocate ifaddr structure, link into chain, etc.
3848 * If we are going to create a new address upon receiving a multicasted
3849 * RA, we need to impose a random delay before starting DAD.
3850 * [RFC 4862, Section 5.4.2]
3852 ifaupdate
= IN6_IFAUPDATE_NOWAIT
;
3854 ifaupdate
|= IN6_IFAUPDATE_DADDELAY
;
3856 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &ia6
);
3859 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
3860 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
),
3861 if_name(ifp
), error
);
3862 error
= EADDRNOTAVAIL
;
3866 VERIFY(ia6
!= NULL
);
3867 in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia6
, NULL
);
3878 #define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
3881 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
)
3883 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
3884 struct in6_ifaddr
*ia
, *newia
;
3885 struct in6_aliasreq ifra
;
3886 int i
, error
, ifaupdate
;
3887 int trylimit
= 3; /* XXX: adhoc value */
3888 u_int32_t randid
[2];
3889 time_t vltime0
, pltime0
;
3890 uint64_t timenow
= net_uptime();
3891 struct in6_addr addr
;
3892 struct nd_prefix
*ndpr
;
3894 bzero(&ifra
, sizeof(ifra
));
3895 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof(ifra
.ifra_name
));
3896 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3897 ifra
.ifra_addr
= ia0
->ia_addr
;
3898 /* copy prefix mask */
3899 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
3900 /* clear the old IFID */
3901 for (i
= 0; i
< 4; i
++) {
3902 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
]
3903 &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
3905 addr
= ia0
->ia_addr
.sin6_addr
;
3906 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3909 in6_iid_mktmp(ifp
, (u_int8_t
*)randid
,
3910 (const u_int8_t
*)&addr
.s6_addr
[8], forcegen
);
3912 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3913 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
3914 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3915 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
3918 * in6_iid_mktmp() quite likely provided a unique interface ID.
3919 * However, we may still have a chance to see collision, because
3920 * there may be a time lag between generation of the ID and generation
3921 * of the address. So, we'll do one more sanity check.
3923 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
)) != NULL
) {
3924 IFA_REMREF(&ia
->ia_ifa
);
3925 if (trylimit
-- == 0) {
3926 nd6log(info
, "in6_tmpifadd: failed to find "
3927 "a unique random IFID\n");
3935 * The Valid Lifetime is the lower of the Valid Lifetime of the
3936 * public address or TEMP_VALID_LIFETIME.
3937 * The Preferred Lifetime is the lower of the Preferred Lifetime
3938 * of the public address or TEMP_PREFERRED_LIFETIME -
3941 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3942 if (ia0
->ia6_lifetime
.ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3943 vltime0
= IFA6_IS_INVALID(ia0
, timenow
) ? 0 :
3944 (ia0
->ia6_lifetime
.ia6ti_vltime
-
3945 (timenow
- ia0
->ia6_updatetime
));
3946 if (vltime0
> ip6_temp_valid_lifetime
) {
3947 vltime0
= ip6_temp_valid_lifetime
;
3950 vltime0
= ip6_temp_valid_lifetime
;
3952 if (ia0
->ia6_lifetime
.ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3953 pltime0
= IFA6_IS_DEPRECATED(ia0
, timenow
) ? 0 :
3954 (ia0
->ia6_lifetime
.ia6ti_pltime
-
3955 (timenow
- ia0
->ia6_updatetime
));
3956 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
) {
3957 pltime0
= ip6_temp_preferred_lifetime
-
3961 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
3963 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
3964 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
3965 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3967 * A temporary address is created only if this calculated Preferred
3968 * Lifetime is greater than REGEN_ADVANCE time units.
3970 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
) {
3974 /* XXX: scope zone ID? */
3976 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
);
3978 /* allocate ifaddr structure, link into chain, etc. */
3979 ifaupdate
= IN6_IFAUPDATE_NOWAIT
| IN6_IFAUPDATE_DADDELAY
;
3980 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &newia
);
3982 nd6log(error
, "in6_tmpifadd: failed to add address.\n");
3985 VERIFY(newia
!= NULL
);
3987 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3988 ndpr
= ia0
->ia6_ndpr
;
3991 * We lost the race with another thread that has purged
3992 * ia0 address; in this case, purge the tmp addr as well.
3994 nd6log(error
, "in6_tmpifadd: no public address\n");
3995 VERIFY(!(ia0
->ia6_flags
& IN6_IFF_AUTOCONF
));
3996 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3997 in6_purgeaddr(&newia
->ia_ifa
);
3998 IFA_REMREF(&newia
->ia_ifa
);
3999 return EADDRNOTAVAIL
;
4001 NDPR_ADDREF(ndpr
); /* for us */
4002 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4003 IFA_LOCK(&newia
->ia_ifa
);
4004 if (newia
->ia6_ndpr
!= NULL
) {
4005 NDPR_LOCK(newia
->ia6_ndpr
);
4006 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
4007 newia
->ia6_ndpr
->ndpr_addrcnt
--;
4008 NDPR_UNLOCK(newia
->ia6_ndpr
);
4009 NDPR_REMREF(newia
->ia6_ndpr
); /* release addr reference */
4011 newia
->ia6_ndpr
= ndpr
;
4012 NDPR_LOCK(newia
->ia6_ndpr
);
4013 newia
->ia6_ndpr
->ndpr_addrcnt
++;
4014 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
4015 NDPR_ADDREF_LOCKED(newia
->ia6_ndpr
); /* for addr reference */
4016 NDPR_UNLOCK(newia
->ia6_ndpr
);
4017 IFA_UNLOCK(&newia
->ia_ifa
);
4019 * A newly added address might affect the status of other addresses.
4020 * XXX: when the temporary address is generated with a new public
4021 * address, the onlink check is redundant. However, it would be safe
4022 * to do the check explicitly everywhere a new address is generated,
4023 * and, in fact, we surely need the check when we create a new
4024 * temporary address due to deprecation of an old temporary address.
4026 lck_mtx_lock(nd6_mutex
);
4027 pfxlist_onlink_check();
4028 lck_mtx_unlock(nd6_mutex
);
4029 IFA_REMREF(&newia
->ia_ifa
);
4031 /* remove our reference */
4039 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
4041 struct timeval caltime
;
4042 u_int64_t timenow
= net_uptime();
4044 NDPR_LOCK_ASSERT_HELD(ndpr
);
4046 getmicrotime(&caltime
);
4047 ndpr
->ndpr_base_calendartime
= caltime
.tv_sec
;
4048 ndpr
->ndpr_base_uptime
= timenow
;
4050 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
4051 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
4052 nd6log(info
, "in6_init_prefix_ltimes: preferred lifetime"
4053 "(%d) is greater than valid lifetime(%d)\n",
4054 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
);
4057 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
) {
4058 ndpr
->ndpr_preferred
= 0;
4060 ndpr
->ndpr_preferred
= timenow
+ ndpr
->ndpr_pltime
;
4062 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
) {
4063 ndpr
->ndpr_expire
= 0;
4065 ndpr
->ndpr_expire
= timenow
+ ndpr
->ndpr_vltime
;
4072 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
4075 uint64_t timenow
= net_uptime();
4077 /* Valid lifetime must not be updated unless explicitly specified. */
4078 /* init ia6t_expire */
4079 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
4080 lt6
->ia6t_expire
= 0;
4082 lt6
->ia6t_expire
= timenow
;
4083 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
4086 /* init ia6t_preferred */
4087 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
) {
4088 lt6
->ia6t_preferred
= 0;
4090 lt6
->ia6t_preferred
= timenow
;
4091 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
4096 * Delete all the routing table entries that use the specified gateway.
4097 * XXX: this function causes search through all entries of routing table, so
4098 * it shouldn't be called when acting as a router.
4102 struct in6_addr
*gateway
,
4105 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
4107 /* We'll care only link-local addresses */
4108 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
4111 lck_mtx_lock(rnh_lock
);
4112 /* XXX: hack for KAME's link-local address kludge */
4113 gateway
->s6_addr16
[1] = htons(ifp
->if_index
);
4115 rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
);
4116 lck_mtx_unlock(rnh_lock
);
4121 struct radix_node
*rn
,
4124 struct rtentry
*rt
= (struct rtentry
*)rn
;
4125 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
4127 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
4130 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
) {
4135 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) {
4140 * Do not delete a static route.
4141 * XXX: this seems to be a bit ad-hoc. Should we consider the
4142 * 'cloned' bit instead?
4144 if ((rt
->rt_flags
& RTF_STATIC
) != 0) {
4149 * We delete only host route. This means, in particular, we don't
4150 * delete default route.
4152 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
4158 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
4159 * rnh_lock here prevents another thread from calling rt_setgate()
4163 return rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4164 rt_mask(rt
), rt
->rt_flags
, 0);
4168 nd6_setdefaultiface(
4172 ifnet_t def_ifp
= NULL
;
4174 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4176 ifnet_head_lock_shared();
4177 if (ifindex
< 0 || if_index
< ifindex
) {
4181 def_ifp
= ifindex2ifnet
[ifindex
];
4184 lck_mtx_lock(nd6_mutex
);
4185 if (nd6_defifindex
!= ifindex
) {
4186 struct ifnet
*odef_ifp
= nd6_defifp
;
4188 nd6_defifindex
= ifindex
;
4189 if (nd6_defifindex
> 0) {
4190 nd6_defifp
= def_ifp
;
4195 if (nd6_defifp
!= NULL
) {
4196 nd6log(info
, "%s: is now the default "
4197 "interface (was %s)\n", if_name(nd6_defifp
),
4198 odef_ifp
!= NULL
? if_name(odef_ifp
) : "NONE");
4200 nd6log(info
, "No default interface set\n");
4204 * If the Default Router List is empty, install a route
4205 * to the specified interface as default or remove the default
4206 * route when the default interface becomes canceled.
4207 * The check for the queue is actually redundant, but
4208 * we do this here to avoid re-install the default route
4209 * if the list is NOT empty.
4211 if (odef_ifp
!= NULL
) {
4212 defrouter_select(odef_ifp
);
4215 if (nd6_defifp
!= NULL
) {
4216 defrouter_select(nd6_defifp
);
4217 nd6_prefix_sync(nd6_defifp
);
4221 * Our current implementation assumes one-to-one mapping between
4222 * interfaces and links, so it would be natural to use the
4223 * default interface as the default link.
4225 scope6_setdefault(nd6_defifp
);
4227 lck_mtx_unlock(nd6_mutex
);