2 * Copyright (c) 2003-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/syslog.h>
69 #include <sys/queue.h>
70 #include <sys/mcache.h>
71 #include <sys/protosw.h>
73 #include <dev/random/randomdev.h>
75 #include <kern/locks.h>
76 #include <kern/zalloc.h>
77 #include <machine/machine_routines.h>
80 #include <net/if_var.h>
81 #include <net/if_types.h>
82 #include <net/if_dl.h>
83 #include <net/route.h>
84 #include <net/radix.h>
86 #include <netinet/in.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet6/in6_ifattach.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/nd6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/scope6_var.h>
95 #include <net/net_osdep.h>
97 static void defrouter_addreq(struct nd_defrouter
*, boolean_t
);
98 static void defrouter_delreq(struct nd_defrouter
*);
99 static struct nd_defrouter
*defrtrlist_update_common(struct nd_defrouter
*,
101 static struct nd_defrouter
*defrtrlist_update(struct nd_defrouter
*);
103 static struct in6_ifaddr
*in6_pfx_newpersistaddr(struct nd_prefix
*, int,
106 static struct nd_pfxrouter
*pfxrtr_lookup(struct nd_prefix
*,
107 struct nd_defrouter
*);
108 static void pfxrtr_add(struct nd_prefix
*, struct nd_defrouter
*);
109 static void pfxrtr_del(struct nd_pfxrouter
*, struct nd_prefix
*);
110 static struct nd_pfxrouter
*find_pfxlist_reachable_router(struct nd_prefix
*);
111 static void nd6_rtmsg(int, struct rtentry
*);
113 static int nd6_prefix_onlink_common(struct nd_prefix
*, boolean_t
,
115 static struct nd_prefix
*nd6_prefix_equal_lookup(struct nd_prefix
*, boolean_t
);
116 static void nd6_prefix_sync(struct ifnet
*);
118 static void in6_init_address_ltimes(struct nd_prefix
*,
119 struct in6_addrlifetime
*);
121 static int rt6_deleteroute(struct radix_node
*, void *);
123 static struct nd_defrouter
*nddr_alloc(int);
124 static void nddr_free(struct nd_defrouter
*);
125 static void nddr_trace(struct nd_defrouter
*, int);
127 static struct nd_prefix
*ndpr_alloc(int);
128 static void ndpr_free(struct nd_prefix
*);
129 static void ndpr_trace(struct nd_prefix
*, int);
131 extern int nd6_recalc_reachtm_interval
;
133 static struct ifnet
*nd6_defifp
= NULL
;
134 int nd6_defifindex
= 0;
135 static unsigned int nd6_defrouter_genid
;
137 int ip6_use_tempaddr
= 1; /* use temp addr by default for testing now */
139 int nd6_accept_6to4
= 1;
141 int ip6_desync_factor
;
142 u_int32_t ip6_temp_preferred_lifetime
= DEF_TEMP_PREFERRED_LIFETIME
;
143 u_int32_t ip6_temp_valid_lifetime
= DEF_TEMP_VALID_LIFETIME
;
145 * shorter lifetimes for debugging purposes.
146 * u_int32_t ip6_temp_preferred_lifetime = 800;
147 * static u_int32_t ip6_temp_valid_lifetime = 1800;
149 int ip6_temp_regen_advance
= TEMPADDR_REGEN_ADVANCE
;
151 extern lck_mtx_t
*nd6_mutex
;
153 /* Serialization variables for single thread access to nd_prefix */
154 static boolean_t nd_prefix_busy
;
155 static void *nd_prefix_waitchan
= &nd_prefix_busy
;
156 static int nd_prefix_waiters
= 0;
158 /* Serialization variables for single thread access to nd_defrouter */
159 static boolean_t nd_defrouter_busy
;
160 static void *nd_defrouter_waitchan
= &nd_defrouter_busy
;
161 static int nd_defrouter_waiters
= 0;
163 /* RTPREF_MEDIUM has to be 0! */
164 #define RTPREF_HIGH 1
165 #define RTPREF_MEDIUM 0
166 #define RTPREF_LOW (-1)
167 #define RTPREF_RESERVED (-2)
168 #define RTPREF_INVALID (-3) /* internal */
170 #define NDPR_TRACE_HIST_SIZE 32 /* size of trace history */
173 __private_extern__
unsigned int ndpr_trace_hist_size
= NDPR_TRACE_HIST_SIZE
;
175 struct nd_prefix_dbg
{
176 struct nd_prefix ndpr_pr
; /* nd_prefix */
177 u_int16_t ndpr_refhold_cnt
; /* # of ref */
178 u_int16_t ndpr_refrele_cnt
; /* # of rele */
180 * Circular lists of ndpr_addref and ndpr_remref callers.
182 ctrace_t ndpr_refhold
[NDPR_TRACE_HIST_SIZE
];
183 ctrace_t ndpr_refrele
[NDPR_TRACE_HIST_SIZE
];
186 static unsigned int ndpr_debug
; /* debug flags */
187 static unsigned int ndpr_size
; /* size of zone element */
188 static struct zone
*ndpr_zone
; /* zone for nd_prefix */
190 #define NDPR_ZONE_MAX 64 /* maximum elements in zone */
191 #define NDPR_ZONE_NAME "nd6_prefix" /* zone name */
193 #define NDDR_TRACE_HIST_SIZE 32 /* size of trace history */
196 __private_extern__
unsigned int nddr_trace_hist_size
= NDDR_TRACE_HIST_SIZE
;
198 struct nd_defrouter_dbg
{
199 struct nd_defrouter nddr_dr
; /* nd_defrouter */
200 uint16_t nddr_refhold_cnt
; /* # of ref */
201 uint16_t nddr_refrele_cnt
; /* # of rele */
203 * Circular lists of ndpr_addref and ndpr_remref callers.
205 ctrace_t nddr_refhold
[NDDR_TRACE_HIST_SIZE
];
206 ctrace_t nddr_refrele
[NDDR_TRACE_HIST_SIZE
];
209 static unsigned int nddr_debug
; /* debug flags */
210 static unsigned int nddr_size
; /* size of zone element */
211 static struct zone
*nddr_zone
; /* zone for nd_defrouter */
213 #define NDDR_ZONE_MAX 64 /* maximum elements in zone */
214 #define NDDR_ZONE_NAME "nd6_defrouter" /* zone name */
216 static unsigned int ndprtr_size
; /* size of zone element */
217 static struct zone
*ndprtr_zone
; /* zone for nd_pfxrouter */
219 #define NDPRTR_ZONE_MAX 64 /* maximum elements in zone */
220 #define NDPRTR_ZONE_NAME "nd6_pfxrouter" /* zone name */
225 PE_parse_boot_argn("ifa_debug", &ndpr_debug
, sizeof (ndpr_debug
));
226 PE_parse_boot_argn("ifa_debug", &nddr_debug
, sizeof (nddr_debug
));
228 ndpr_size
= (ndpr_debug
== 0) ? sizeof (struct nd_prefix
) :
229 sizeof (struct nd_prefix_dbg
);
230 ndpr_zone
= zinit(ndpr_size
, NDPR_ZONE_MAX
* ndpr_size
, 0,
232 if (ndpr_zone
== NULL
) {
233 panic("%s: failed allocating %s", __func__
, NDPR_ZONE_NAME
);
236 zone_change(ndpr_zone
, Z_EXPAND
, TRUE
);
237 zone_change(ndpr_zone
, Z_CALLERACCT
, FALSE
);
239 nddr_size
= (nddr_debug
== 0) ? sizeof (struct nd_defrouter
) :
240 sizeof (struct nd_defrouter_dbg
);
241 nddr_zone
= zinit(nddr_size
, NDDR_ZONE_MAX
* nddr_size
, 0,
243 if (nddr_zone
== NULL
) {
244 panic("%s: failed allocating %s", __func__
, NDDR_ZONE_NAME
);
247 zone_change(nddr_zone
, Z_EXPAND
, TRUE
);
248 zone_change(nddr_zone
, Z_CALLERACCT
, FALSE
);
250 ndprtr_size
= sizeof (struct nd_pfxrouter
);
251 ndprtr_zone
= zinit(ndprtr_size
, NDPRTR_ZONE_MAX
* ndprtr_size
, 0,
253 if (ndprtr_zone
== NULL
) {
254 panic("%s: failed allocating %s", __func__
, NDPRTR_ZONE_NAME
);
257 zone_change(ndprtr_zone
, Z_EXPAND
, TRUE
);
258 zone_change(ndprtr_zone
, Z_CALLERACCT
, FALSE
);
262 * Receive Router Solicitation Message - just for routers.
263 * Router solicitation/advertisement is mostly managed by userland program
264 * (rtadvd) so here we have no function like nd6_ra_output().
274 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
275 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
276 struct nd_router_solicit
*nd_rs
;
277 struct in6_addr saddr6
= ip6
->ip6_src
;
280 union nd_opts ndopts
;
282 /* Expect 32-bit aligned data pointer on strict-align platforms */
283 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
285 /* If I'm not a router, ignore it. */
286 if (!ip6_forwarding
|| !(ifp
->if_eflags
& IFEF_IPV6_ROUTER
))
290 if (ip6
->ip6_hlim
!= 255) {
292 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
293 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
294 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
299 * Don't update the neighbor cache, if src = :: or a non-neighbor.
300 * The former case indicates that the src has no IP address assigned
301 * yet. See nd6_ns_input() for the latter case.
303 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
306 struct sockaddr_in6 src_sa6
;
308 bzero(&src_sa6
, sizeof (src_sa6
));
309 src_sa6
.sin6_family
= AF_INET6
;
310 src_sa6
.sin6_len
= sizeof (src_sa6
);
311 src_sa6
.sin6_addr
= ip6
->ip6_src
;
312 if (!nd6_is_addr_neighbor(&src_sa6
, ifp
, 0)) {
313 nd6log((LOG_INFO
, "nd6_rs_input: "
314 "RS packet from non-neighbor\n"));
319 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
320 nd_rs
= (struct nd_router_solicit
*)((caddr_t
)ip6
+ off
);
321 icmp6len
-= sizeof (*nd_rs
);
322 nd6_option_init(nd_rs
+ 1, icmp6len
, &ndopts
);
323 if (nd6_options(&ndopts
) < 0) {
325 "nd6_rs_input: invalid ND option, ignored\n"));
326 /* nd6_options have incremented stats */
330 if (ndopts
.nd_opts_src_lladdr
) {
331 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
332 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
335 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
337 "nd6_rs_input: lladdrlen mismatch for %s "
338 "(if %d, RS packet %d)\n",
339 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
343 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_SOLICIT
, 0);
350 icmp6stat
.icp6s_badrs
++;
355 * Receive Router Advertisement Message.
358 * TODO: on-link bit on prefix information
359 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
367 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
368 struct nd_ifinfo
*ndi
= NULL
;
369 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
370 struct nd_router_advert
*nd_ra
;
371 struct in6_addr saddr6
= ip6
->ip6_src
;
373 union nd_opts ndopts
;
374 struct nd_defrouter
*dr
= NULL
;
377 u_int32_t lladdrlen
= 0;
378 struct nd_prefix_list
*nd_prefix_list_head
= NULL
;
379 u_int32_t nd_prefix_list_length
= 0;
380 struct in6_ifaddr
*ia6
= NULL
;
381 struct nd_prefix_list
*prfl
;
382 struct nd_defrouter dr0
;
383 u_int32_t advreachable
;
386 /* Expect 32-bit aligned data pointer on strict-align platforms */
387 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
390 * Discard RA unless IFEF_ACCEPT_RTADV is set (as host), or when
391 * IFEF_IPV6_ROUTER is set (as router) but the RA is not locally
392 * generated. For convenience, we allow locally generated (rtadvd)
393 * RAs to be processed on the advertising interface, as a router.
395 * Note that we don't test against ip6_forwarding as we could be
396 * both a host and a router on different interfaces, hence the
397 * check against the per-interface flags.
399 if (!(ifp
->if_eflags
& (IFEF_ACCEPT_RTADV
| IFEF_IPV6_ROUTER
)) ||
400 ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) &&
401 (ia6
= ifa_foraddr6(&saddr6
)) == NULL
))
405 IFA_REMREF(&ia6
->ia_ifa
);
409 if (ip6
->ip6_hlim
!= 255) {
411 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
412 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
413 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
417 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) {
419 "nd6_ra_input: src %s is not link-local\n",
420 ip6_sprintf(&saddr6
)));
424 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
425 nd_ra
= (struct nd_router_advert
*)((caddr_t
)ip6
+ off
);
427 icmp6len
-= sizeof (*nd_ra
);
428 nd6_option_init(nd_ra
+ 1, icmp6len
, &ndopts
);
429 if (nd6_options(&ndopts
) < 0) {
431 "nd6_ra_input: invalid ND option, ignored\n"));
432 /* nd6_options have incremented stats */
436 advreachable
= nd_ra
->nd_ra_reachable
;
438 /* remember if this is a multicasted advertisement */
439 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
442 ndi
= ND_IFINFO(ifp
);
443 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
444 lck_mtx_lock(&ndi
->lock
);
445 bzero(&dr0
, sizeof (dr0
));
447 dr0
.flags
= nd_ra
->nd_ra_flags_reserved
;
448 dr0
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
449 dr0
.expire
= net_uptime() + dr0
.rtlifetime
;
451 /* unspecified or not? (RFC 2461 6.3.4) */
453 advreachable
= ntohl(advreachable
);
454 if (advreachable
<= MAX_REACHABLE_TIME
&&
455 ndi
->basereachable
!= advreachable
) {
456 ndi
->basereachable
= advreachable
;
457 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
458 ndi
->recalctm
= nd6_recalc_reachtm_interval
; /* reset */
461 if (nd_ra
->nd_ra_retransmit
)
462 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
463 if (nd_ra
->nd_ra_curhoplimit
) {
464 if (ndi
->chlim
< nd_ra
->nd_ra_curhoplimit
) {
465 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
466 } else if (ndi
->chlim
!= nd_ra
->nd_ra_curhoplimit
) {
468 "RA with a lower CurHopLimit sent from "
469 "%s on %s (current = %d, received = %d). "
470 "Ignored.\n", ip6_sprintf(&ip6
->ip6_src
),
471 if_name(ifp
), ndi
->chlim
,
472 nd_ra
->nd_ra_curhoplimit
));
475 lck_mtx_unlock(&ndi
->lock
);
476 lck_mtx_lock(nd6_mutex
);
477 dr
= defrtrlist_update(&dr0
);
478 lck_mtx_unlock(nd6_mutex
);
483 if (ndopts
.nd_opts_pi
) {
484 struct nd_opt_hdr
*pt
;
485 struct nd_opt_prefix_info
*pi
= NULL
;
488 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
489 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
490 pt
= (struct nd_opt_hdr
*)((caddr_t
)pt
+
491 (pt
->nd_opt_len
<< 3))) {
492 struct in6_addr pi_mask
;
493 bzero(&pi_mask
, sizeof(pi_mask
));
495 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
)
497 pi
= (struct nd_opt_prefix_info
*)pt
;
499 if (pi
->nd_opt_pi_len
!= 4) {
501 "nd6_ra_input: invalid option "
502 "len %d for prefix information option, "
503 "ignored\n", pi
->nd_opt_pi_len
));
507 if (128 < pi
->nd_opt_pi_prefix_len
) {
509 "nd6_ra_input: invalid prefix "
510 "len %d for prefix information option, "
511 "ignored\n", pi
->nd_opt_pi_prefix_len
));
516 * To ignore ::/64 make sure bits beyond prefixlen
519 in6_prefixlen2mask(&pi_mask
, pi
->nd_opt_pi_prefix_len
);
520 pi
->nd_opt_pi_prefix
.s6_addr32
[0] &= pi_mask
.s6_addr32
[0];
521 pi
->nd_opt_pi_prefix
.s6_addr32
[1] &= pi_mask
.s6_addr32
[1];
522 pi
->nd_opt_pi_prefix
.s6_addr32
[2] &= pi_mask
.s6_addr32
[2];
523 pi
->nd_opt_pi_prefix
.s6_addr32
[3] &= pi_mask
.s6_addr32
[3];
525 if (IN6_IS_ADDR_UNSPECIFIED(&pi
->nd_opt_pi_prefix
) ||
526 IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
) ||
527 IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
529 "%s: invalid prefix %s, ignored\n",
531 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
535 bzero(&pr
, sizeof (pr
));
536 lck_mtx_init(&pr
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
538 pr
.ndpr_prefix
.sin6_family
= AF_INET6
;
539 pr
.ndpr_prefix
.sin6_len
= sizeof (pr
.ndpr_prefix
);
540 pr
.ndpr_prefix
.sin6_addr
= pi
->nd_opt_pi_prefix
;
541 pr
.ndpr_ifp
= m
->m_pkthdr
.rcvif
;
543 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
544 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
545 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
546 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
547 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
548 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
550 ntohl(pi
->nd_opt_pi_preferred_time
);
553 * Exceptions to stateless autoconfiguration processing:
554 * + nd6_accept_6to4 == 0 && address has 6to4 prefix
555 * + ip6_only_allow_rfc4193_prefix != 0 &&
556 * address not RFC 4193
558 if (ip6_only_allow_rfc4193_prefix
&&
559 !IN6_IS_ADDR_UNIQUE_LOCAL(&pi
->nd_opt_pi_prefix
)) {
561 "nd6_ra_input: no SLAAC on prefix %s "
563 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
564 pr
.ndpr_raf_auto
= 0;
565 } else if (!nd6_accept_6to4
&&
566 IN6_IS_ADDR_6TO4(&pi
->nd_opt_pi_prefix
)) {
568 "%s: no SLAAC on prefix %s "
569 "[6to4]\n", __func__
,
570 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
571 pr
.ndpr_raf_auto
= 0;
574 if (in6_init_prefix_ltimes(&pr
)) {
576 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
577 continue; /* prefix lifetime init failed */
581 (void) prelist_update(&pr
, dr
, m
, mcast
);
582 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
585 * We have to copy the values out after the
586 * prelist_update call since some of these values won't
587 * be properly set until after the router advertisement
588 * updating can vet the values.
591 MALLOC(prfl
, struct nd_prefix_list
*, sizeof (*prfl
),
592 M_TEMP
, M_WAITOK
| M_ZERO
);
595 log(LOG_DEBUG
, "%s: unable to MALLOC RA prefix "
596 "structure\n", __func__
);
600 /* this is only for nd6_post_msg(), otherwise unused */
601 bcopy(&pr
.ndpr_prefix
, &prfl
->pr
.ndpr_prefix
,
602 sizeof (prfl
->pr
.ndpr_prefix
));
603 prfl
->pr
.ndpr_raf
= pr
.ndpr_raf
;
604 prfl
->pr
.ndpr_plen
= pr
.ndpr_plen
;
605 prfl
->pr
.ndpr_vltime
= pr
.ndpr_vltime
;
606 prfl
->pr
.ndpr_pltime
= pr
.ndpr_pltime
;
607 prfl
->pr
.ndpr_expire
= pr
.ndpr_expire
;
608 prfl
->pr
.ndpr_base_calendartime
=
609 pr
.ndpr_base_calendartime
;
610 prfl
->pr
.ndpr_base_uptime
= pr
.ndpr_base_uptime
;
611 prfl
->pr
.ndpr_stateflags
= pr
.ndpr_stateflags
;
612 prfl
->pr
.ndpr_addrcnt
= pr
.ndpr_addrcnt
;
613 prfl
->pr
.ndpr_ifp
= pr
.ndpr_ifp
;
615 prfl
->next
= nd_prefix_list_head
;
616 nd_prefix_list_head
= prfl
;
617 nd_prefix_list_length
++;
624 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
625 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
628 if (mtu
< IPV6_MMTU
) {
629 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu option "
630 "mtu=%d sent from %s, ignoring\n",
631 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
635 lck_mtx_lock(&ndi
->lock
);
638 if (mtu
<= ndi
->maxmtu
) {
639 int change
= (ndi
->linkmtu
!= mtu
);
642 lck_mtx_unlock(&ndi
->lock
);
643 if (change
) /* in6_maxmtu may change */
646 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu "
647 "mtu=%d sent from %s; "
648 "exceeds maxmtu %d, ignoring\n",
649 mtu
, ip6_sprintf(&ip6
->ip6_src
),
651 lck_mtx_unlock(&ndi
->lock
);
654 lck_mtx_unlock(&ndi
->lock
);
655 nd6log((LOG_INFO
, "nd6_ra_input: mtu option "
656 "mtu=%d sent from %s; maxmtu unknown, "
658 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
665 * Source link layer address
667 if (ndopts
.nd_opts_src_lladdr
) {
668 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
669 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
672 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
674 "nd6_ra_input: lladdrlen mismatch for %s "
675 "(if %d, RA packet %d)\n",
676 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
680 if (dr
&& dr
->stateflags
& NDDRF_MAPPED
)
681 saddr6
= dr
->rtaddr_mapped
;
683 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, (int)lladdrlen
,
684 ND_ROUTER_ADVERT
, 0);
687 nd6_post_msg(KEV_ND6_RA
, nd_prefix_list_head
, nd_prefix_list_length
,
688 mtu
, lladdr
, lladdrlen
);
691 * Installing a link-layer address might change the state of the
692 * router's neighbor cache, which might also affect our on-link
693 * detection of adveritsed prefixes.
695 lck_mtx_lock(nd6_mutex
);
696 pfxlist_onlink_check();
697 lck_mtx_unlock(nd6_mutex
);
705 while ((prfl
= nd_prefix_list_head
) != NULL
) {
706 nd_prefix_list_head
= prfl
->next
;
713 icmp6stat
.icp6s_badra
++;
718 * default router list proccessing sub routines
721 /* tell the change to user processes watching the routing socket. */
723 nd6_rtmsg(int cmd
, struct rtentry
*rt
)
725 struct rt_addrinfo info
;
726 struct ifnet
*ifp
= rt
->rt_ifp
;
728 RT_LOCK_ASSERT_HELD(rt
);
730 bzero((caddr_t
)&info
, sizeof (info
));
731 /* It's not necessary to lock ifp for if_lladdr */
732 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
733 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
734 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
736 * ifa_addr pointers for both should always be valid
737 * in this context; no need to hold locks.
739 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
740 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
742 rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0);
746 defrouter_addreq(struct nd_defrouter
*new, boolean_t scoped
)
748 struct sockaddr_in6 def
, mask
, gate
;
749 struct rtentry
*newrt
= NULL
;
750 unsigned int ifscope
;
752 struct nd_ifinfo
*ndi
= ND_IFINFO(new->ifp
);
754 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
755 NDDR_LOCK_ASSERT_NOTHELD(new);
757 * We're free to lock and unlock NDDR because our callers
758 * are holding an extra reference for us.
762 if (new->stateflags
& NDDRF_INSTALLED
)
765 if (new->ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
766 nd6log2((LOG_INFO
, "%s: ignoring router %s, scoped=%d, "
767 "static=%d on advertising interface\n", if_name(new->ifp
),
768 ip6_sprintf(&new->rtaddr
), scoped
,
769 (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
773 nd6log2((LOG_INFO
, "%s: adding default router %s, scoped=%d, "
774 "static=%d\n", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
),
775 scoped
, (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
777 Bzero(&def
, sizeof (def
));
778 Bzero(&mask
, sizeof (mask
));
779 Bzero(&gate
, sizeof (gate
));
781 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
782 = sizeof (struct sockaddr_in6
);
783 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
785 if (new->stateflags
& NDDRF_MAPPED
)
786 gate
.sin6_addr
= new->rtaddr_mapped
;
788 gate
.sin6_addr
= new->rtaddr
;
790 ifscope
= scoped
? new->ifp
->if_index
: IFSCOPE_NONE
;
794 * Cellular networks may have buggy deployments
795 * with gateway IPv6 link local address with same
796 * interface identifier as the one that has been
797 * assigned for the cellular context.
798 * If gateway is same as locally configured link local
799 * interface on cellular interface, generated a different one
800 * and store it in the nd_defrouter entry and use it to work
803 if (new->ifp
->if_type
== IFT_CELLULAR
&&
804 !(new->stateflags
& NDDRF_STATIC
) &&
805 !(new->stateflags
& NDDRF_MAPPED
) &&
806 IN6_IS_ADDR_LINKLOCAL(&gate
.sin6_addr
) &&
807 ndi
&& !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
808 struct in6_ifaddr
*tmp_ia6
= in6ifa_ifpforlinklocal(new->ifp
, 0);
810 if (tmp_ia6
!= NULL
&&
811 !(tmp_ia6
->ia6_flags
& IN6_IFF_NOTMANUAL
) &&
812 IN6_ARE_ADDR_EQUAL(&tmp_ia6
->ia_addr
.sin6_addr
,
814 gate
.sin6_addr
.s6_addr8
[15] += 1;
815 new->rtaddr_mapped
= gate
.sin6_addr
;
816 new->stateflags
|= NDDRF_MAPPED
;
818 nd6log((LOG_INFO
, "%s: Default router %s mapped "
819 "to ", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
)));
820 nd6log((LOG_INFO
, "%s\n", ip6_sprintf(&new->rtaddr_mapped
)));
824 err
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&def
,
825 (struct sockaddr
*)&gate
, (struct sockaddr
*)&mask
,
826 RTF_GATEWAY
, &newrt
, ifscope
);
830 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */
831 RT_REMREF_LOCKED(newrt
);
834 new->stateflags
|= NDDRF_INSTALLED
;
835 if (ifscope
!= IFSCOPE_NONE
)
836 new->stateflags
|= NDDRF_IFSCOPE
;
838 nd6log((LOG_ERR
, "%s: failed to add default router "
839 "%s on %s scoped %d (errno = %d)\n", __func__
,
840 ip6_sprintf(&gate
.sin6_addr
), if_name(new->ifp
),
841 (ifscope
!= IFSCOPE_NONE
), err
));
850 struct nd_defrouter
*
852 struct in6_addr
*addr
,
855 struct nd_defrouter
*dr
;
857 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
859 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
860 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
862 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) {
863 NDDR_ADDREF_LOCKED(dr
);
870 return (NULL
); /* search failed */
874 * Remove the default route for a given router.
875 * This is just a subroutine function for defrouter_select(), and should
876 * not be called from anywhere else.
879 defrouter_delreq(struct nd_defrouter
*dr
)
881 struct sockaddr_in6 def
, mask
, gate
;
882 struct rtentry
*oldrt
= NULL
;
883 unsigned int ifscope
;
886 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
887 NDDR_LOCK_ASSERT_NOTHELD(dr
);
889 * We're free to lock and unlock NDDR because our callers
890 * are holding an extra reference for us.
893 /* ifp would be NULL for the "drany" case */
894 if (dr
->ifp
!= NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
))
897 nd6log2((LOG_INFO
, "%s: removing default router %s, scoped=%d, "
898 "static=%d\n", dr
->ifp
!= NULL
? if_name(dr
->ifp
) : "ANY",
899 ip6_sprintf(&dr
->rtaddr
), (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
900 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
902 Bzero(&def
, sizeof (def
));
903 Bzero(&mask
, sizeof (mask
));
904 Bzero(&gate
, sizeof (gate
));
906 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
907 = sizeof (struct sockaddr_in6
);
908 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
911 * The router entry may be mapped to a different address.
912 * If that is the case, use the mapped address as gateway
913 * to do operation on the routing table.
914 * To get more context, read the related comment in
917 if (dr
->stateflags
& NDDRF_MAPPED
)
918 gate
.sin6_addr
= dr
->rtaddr_mapped
;
920 gate
.sin6_addr
= dr
->rtaddr
;
922 if (dr
->ifp
!= NULL
) {
923 ifscope
= (dr
->stateflags
& NDDRF_IFSCOPE
) ?
924 dr
->ifp
->if_index
: IFSCOPE_NONE
;
926 ifscope
= IFSCOPE_NONE
;
930 err
= rtrequest_scoped(RTM_DELETE
,
931 (struct sockaddr
*)&def
, (struct sockaddr
*)&gate
,
932 (struct sockaddr
*)&mask
, RTF_GATEWAY
, &oldrt
, ifscope
);
936 nd6_rtmsg(RTM_DELETE
, oldrt
);
939 } else if (err
!= ESRCH
) {
940 nd6log((LOG_ERR
, "%s: failed to delete default router "
941 "%s on %s scoped %d (errno = %d)\n", __func__
,
942 ip6_sprintf(&gate
.sin6_addr
), dr
->ifp
!= NULL
?
943 if_name(dr
->ifp
) : "ANY", (ifscope
!= IFSCOPE_NONE
), err
));
946 /* ESRCH means it's no longer in the routing table; ignore it */
947 if (oldrt
!= NULL
|| err
== ESRCH
) {
948 dr
->stateflags
&= ~NDDRF_INSTALLED
;
949 if (ifscope
!= IFSCOPE_NONE
)
950 dr
->stateflags
&= ~NDDRF_IFSCOPE
;
959 * remove all default routes from default router list
962 defrouter_reset(void)
964 struct nd_defrouter
*dr
, drany
;
966 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
968 dr
= TAILQ_FIRST(&nd_defrouter
);
971 if (dr
->stateflags
& NDDRF_INSTALLED
) {
972 NDDR_ADDREF_LOCKED(dr
);
974 lck_mtx_unlock(nd6_mutex
);
975 defrouter_delreq(dr
);
976 lck_mtx_lock(nd6_mutex
);
978 dr
= TAILQ_FIRST(&nd_defrouter
);
981 dr
= TAILQ_NEXT(dr
, dr_entry
);
985 /* Nuke primary (non-scoped) default router */
986 bzero(&drany
, sizeof (drany
));
987 lck_mtx_init(&drany
.nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
988 lck_mtx_unlock(nd6_mutex
);
989 defrouter_delreq(&drany
);
990 lck_mtx_destroy(&drany
.nddr_lock
, ifa_mtx_grp
);
991 lck_mtx_lock(nd6_mutex
);
995 defrtrlist_ioctl(u_long cmd
, caddr_t data
)
997 struct nd_defrouter dr0
;
998 unsigned int ifindex
;
999 struct ifnet
*dr_ifp
;
1000 int error
= 0, add
= 0;
1002 /* XXX Handle mapped default router entries */
1004 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1005 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1008 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1009 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1010 bzero(&dr0
, sizeof (dr0
));
1011 if (cmd
== SIOCDRADD_IN6_64
|| cmd
== SIOCDRDEL_IN6_64
) {
1012 struct in6_defrouter_64
*r_64
=
1013 (struct in6_defrouter_64
*)(void *)data
;
1016 bcopy(&r_64
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1017 sizeof (dr0
.rtaddr
));
1018 dr0
.flags
= r_64
->flags
;
1019 bcopy(&r_64
->if_index
, &i
, sizeof (i
));
1022 struct in6_defrouter_32
*r_32
=
1023 (struct in6_defrouter_32
*)(void *)data
;
1026 bcopy(&r_32
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1027 sizeof (dr0
.rtaddr
));
1028 dr0
.flags
= r_32
->flags
;
1029 bcopy(&r_32
->if_index
, &i
, sizeof (i
));
1032 ifnet_head_lock_shared();
1033 /* Don't need to check is ifindex is < 0 since it's unsigned */
1034 if (if_index
< ifindex
||
1035 (dr_ifp
= ifindex2ifnet
[ifindex
]) == NULL
) {
1043 if (IN6_IS_SCOPE_EMBED(&dr0
.rtaddr
)) {
1044 uint16_t *scope
= &dr0
.rtaddr
.s6_addr16
[1];
1047 *scope
= htons(dr_ifp
->if_index
);
1048 } else if (*scope
!= htons(dr_ifp
->if_index
)) {
1055 error
= defrtrlist_add_static(&dr0
);
1056 if (!add
|| error
!= 0) {
1057 int err
= defrtrlist_del_static(&dr0
);
1064 error
= EOPNOTSUPP
; /* check for safety */
1072 * XXX Please make sure to remove dr from the
1073 * global default router tailq list before this
1075 * Also ensure that you release the list reference
1076 * only after calling this routine.
1079 defrtrlist_del(struct nd_defrouter
*dr
)
1081 #if (DEVELOPMENT || DEBUG)
1082 struct nd_defrouter
*dr_itr
= NULL
;
1084 struct nd_prefix
*pr
;
1085 struct ifnet
*ifp
= dr
->ifp
;
1086 struct nd_ifinfo
*ndi
= NULL
;
1089 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1091 #if (DEVELOPMENT || DEBUG)
1093 * Verify that the router is not in the global default
1095 * Can't use defrouter_lookup here because that just works
1096 * with address and ifp pointer.
1097 * We have to compare the memory here.
1098 * Also we can't use ASSERT here as that is not defined
1099 * for development builds.
1101 TAILQ_FOREACH(dr_itr
, &nd_defrouter
, dr_entry
)
1102 VERIFY(dr
!= dr_itr
);
1104 ++nd6_defrouter_genid
;
1106 * Flush all the routing table entries that use the router
1109 /* above is a good condition? */
1111 lck_mtx_unlock(nd6_mutex
);
1112 if (dr
->stateflags
& NDDRF_MAPPED
)
1113 rt6_flush(&dr
->rtaddr_mapped
, ifp
);
1115 rt6_flush(&dr
->rtaddr
, ifp
);
1117 lck_mtx_lock(nd6_mutex
);
1119 nd6log2((LOG_INFO
, "%s: freeing defrouter %s\n", if_name(dr
->ifp
),
1120 ip6_sprintf(&dr
->rtaddr
)));
1122 * Delete it from the routing table.
1125 lck_mtx_unlock(nd6_mutex
);
1126 defrouter_delreq(dr
);
1127 lck_mtx_lock(nd6_mutex
);
1131 * Also delete all the pointers to the router in each prefix lists.
1133 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1134 struct nd_pfxrouter
*pfxrtr
;
1137 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
1138 pfxrtr_del(pfxrtr
, pr
);
1142 pfxlist_onlink_check();
1145 ndi
= ND_IFINFO(ifp
);
1146 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1147 lck_mtx_lock(&ndi
->lock
);
1148 VERIFY(ndi
->ndefrouters
>= 0);
1149 if (ndi
->ndefrouters
> 0 && --ndi
->ndefrouters
== 0) {
1153 lck_mtx_unlock(&ndi
->lock
);
1156 * If the router is the primary one, choose a new one.
1157 * We always try to pick another eligible router
1158 * on this interface as we do scoped routing
1160 defrouter_select(ifp
);
1167 defrtrlist_add_static(struct nd_defrouter
*new)
1169 struct nd_defrouter
*dr
;
1172 new->rtlifetime
= -1;
1173 new->stateflags
|= NDDRF_STATIC
;
1175 /* we only want the preference level */
1176 new->flags
&= ND_RA_FLAG_RTPREF_MASK
;
1178 lck_mtx_lock(nd6_mutex
);
1179 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1180 if (dr
!= NULL
&& !(dr
->stateflags
& NDDRF_STATIC
)) {
1185 dr
= defrtrlist_update(new);
1193 lck_mtx_unlock(nd6_mutex
);
1199 defrtrlist_del_static(struct nd_defrouter
*new)
1201 struct nd_defrouter
*dr
;
1203 lck_mtx_lock(nd6_mutex
);
1204 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1205 if (dr
== NULL
|| !(dr
->stateflags
& NDDRF_STATIC
)) {
1210 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1212 NDDR_REMREF(dr
); /* remove list reference */
1215 lck_mtx_unlock(nd6_mutex
);
1217 return (dr
!= NULL
? 0 : EINVAL
);
1221 * for default router selection
1222 * regards router-preference field as a 2-bit signed integer
1225 rtpref(struct nd_defrouter
*dr
)
1227 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
1228 case ND_RA_FLAG_RTPREF_HIGH
:
1229 return (RTPREF_HIGH
);
1230 case ND_RA_FLAG_RTPREF_MEDIUM
:
1231 case ND_RA_FLAG_RTPREF_RSV
:
1232 return (RTPREF_MEDIUM
);
1233 case ND_RA_FLAG_RTPREF_LOW
:
1234 return (RTPREF_LOW
);
1237 * This case should never happen. If it did, it would mean a
1238 * serious bug of kernel internal. We thus always bark here.
1239 * Or, can we even panic?
1241 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
1242 return (RTPREF_INVALID
);
1248 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
1250 * 1) Routers that are reachable or probably reachable should be preferred.
1251 * If we have more than one (probably) reachable router, prefer ones
1252 * with the highest router preference.
1253 * 2) When no routers on the list are known to be reachable or
1254 * probably reachable, routers SHOULD be selected in a round-robin
1255 * fashion, regardless of router preference values.
1256 * 3) If the Default Router List is empty, assume that all
1257 * destinations are on-link.
1259 * When Scoped Routing is enabled, the selection logic is amended as follows:
1261 * a) When a default interface is specified, the primary/non-scoped default
1262 * router will be set to the reachable router on that link (if any) with
1263 * the highest router preference.
1264 * b) When there are more than one routers on the same link, the one with
1265 * the highest router preference will be installed, either as scoped or
1266 * non-scoped route entry. If they all share the same preference value,
1267 * the one installed will be the static or the first encountered reachable
1268 * router, i.e. static one wins over dynamic.
1269 * c) When no routers on the list are known to be reachable, or probably
1270 * reachable, no round-robin selection will take place when the default
1273 * We assume nd_defrouter is sorted by router preference value.
1274 * Since the code below covers both with and without router preference cases,
1275 * we do not need to classify the cases by ifdef.
1278 defrouter_select(struct ifnet
*ifp
)
1280 struct nd_defrouter
*dr
= NULL
;
1281 struct nd_defrouter
*selected_dr
= NULL
;
1282 struct nd_defrouter
*installed_dr
= NULL
;
1283 struct llinfo_nd6
*ln
= NULL
;
1284 struct rtentry
*rt
= NULL
;
1285 struct nd_ifinfo
*ndi
= NULL
;
1286 unsigned int genid
= 0;
1287 boolean_t is_installed_reachable
= FALSE
;
1289 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1293 "%s:%d: Return early. NULL interface",
1294 __func__
, __LINE__
));
1298 if (ifp
== lo_ifp
) {
1300 "%s:%d: Return early. "
1301 "Default router select called for loopback.\n",
1302 __func__
, __LINE__
));
1306 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1308 "%s:%d: Return early. "
1309 "Default router select called for interface"
1310 " %s with IFEF_IPV6_ROUTER flag set\n",
1311 __func__
, __LINE__
, if_name(ifp
)));
1316 * Let's handle easy case (3) first:
1317 * If default router list is empty, there's nothing to be done.
1319 if (!TAILQ_FIRST(&nd_defrouter
)) {
1321 "%s:%d: Return early. "
1322 "Default router is empty.\n", __func__
, __LINE__
));
1327 * Take an early exit if number of routers in nd_ifinfo is
1328 * 0 for the interface.
1330 ndi
= ND_IFINFO(ifp
);
1331 if (!ndi
|| !ndi
->initialized
) {
1333 "%s:%d: Return early. "
1334 "Interface %s's nd_ifinfo not initialized.\n",
1335 __func__
, __LINE__
, if_name(ifp
)));
1339 if (ndi
->ndefrouters
== 0) {
1341 "%s:%d: Return early. "
1342 "%s does not have any default routers.\n",
1343 __func__
, __LINE__
, if_name(ifp
)));
1348 * Due to the number of times we drop nd6_mutex, we need to
1349 * serialize this function.
1351 while (nd_defrouter_busy
) {
1352 nd_defrouter_waiters
++;
1353 msleep(nd_defrouter_waitchan
, nd6_mutex
, (PZERO
-1),
1355 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1357 nd_defrouter_busy
= TRUE
;
1360 * Search for a (probably) reachable router from the list.
1361 * We just pick up the first reachable one (if any), assuming that
1362 * the ordering rule of the list described in defrtrlist_update().
1364 * For all intents and purposes of Scoped Routing:
1365 * selected_dr = candidate for primary router
1366 * installed_dr = currently installed primary router
1368 genid
= nd6_defrouter_genid
;
1369 dr
= TAILQ_FIRST(&nd_defrouter
);
1371 while (dr
!= NULL
) {
1372 struct in6_addr rtaddr
;
1373 struct ifnet
*drifp
= NULL
;
1374 struct nd_defrouter
*drrele
= NULL
;
1380 dr
= TAILQ_NEXT(dr
, dr_entry
);
1385 * Optimize for the common case.
1386 * When the interface has only one default router
1387 * there's no point checking for reachability as
1388 * there's nothing else to choose from.
1390 if (ndi
->ndefrouters
== 1) {
1392 "%s:%d: Fast forward default router selection "
1393 "as interface %s has learned only one default "
1394 "router and there's nothing else to choose from.\n",
1395 __func__
, __LINE__
, if_name(ifp
)));
1396 VERIFY(selected_dr
== NULL
&& installed_dr
== NULL
);
1398 if (dr
->stateflags
& NDDRF_INSTALLED
)
1400 NDDR_ADDREF_LOCKED(selected_dr
);
1405 if (dr
->stateflags
& NDDRF_MAPPED
)
1406 rtaddr
= dr
->rtaddr_mapped
;
1408 rtaddr
= dr
->rtaddr
;
1410 NDDR_ADDREF_LOCKED(dr
); /* for this for loop */
1413 /* Callee returns a locked route upon success */
1414 if (selected_dr
== NULL
) {
1415 lck_mtx_unlock(nd6_mutex
);
1416 if ((rt
= nd6_lookup(&rtaddr
, 0, drifp
, 0)) != NULL
&&
1417 (ln
= rt
->rt_llinfo
) != NULL
&&
1418 ND6_IS_LLINFO_PROBREACH(ln
)) {
1419 RT_LOCK_ASSERT_HELD(rt
);
1421 NDDR_ADDREF(selected_dr
);
1423 lck_mtx_lock(nd6_mutex
);
1427 RT_REMREF_LOCKED(rt
);
1434 * When there are more than one routers on the same link, the one with
1435 * the highest router preference will be installed.
1436 * Since the list is in decreasing order of preference:
1437 * 1) If selected_dr is not NULL, only use dr if it is static and has
1438 * equal preference and selected_dr is not static.
1439 * 2) Else if selected_dr is NULL, and dr is static make selected_dr = dr
1442 if (((selected_dr
&& (rtpref(dr
) >= rtpref(selected_dr
)) &&
1443 !(selected_dr
->stateflags
& NDDRF_STATIC
)) ||
1444 (selected_dr
== NULL
)) &&
1445 (dr
->stateflags
& NDDRF_STATIC
)) {
1447 /* Release it later on */
1448 VERIFY(drrele
== NULL
);
1449 drrele
= selected_dr
;
1452 NDDR_ADDREF_LOCKED(selected_dr
);
1455 /* Record the currently installed router */
1456 if (dr
->stateflags
& NDDRF_INSTALLED
) {
1457 if (installed_dr
== NULL
) {
1459 NDDR_ADDREF_LOCKED(installed_dr
);
1460 if (dr
->stateflags
& NDDRF_MAPPED
)
1461 rtaddr
= installed_dr
->rtaddr_mapped
;
1463 rtaddr
= installed_dr
->rtaddr
;
1464 lck_mtx_unlock(nd6_mutex
);
1465 /* Callee returns a locked route upon success */
1466 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
1467 RT_LOCK_ASSERT_HELD(rt
);
1468 if ((ln
= rt
->rt_llinfo
) != NULL
&&
1469 ND6_IS_LLINFO_PROBREACH(ln
))
1470 is_installed_reachable
= TRUE
;
1472 RT_REMREF_LOCKED(rt
);
1476 lck_mtx_lock(nd6_mutex
);
1478 /* this should not happen; warn for diagnosis */
1479 nd6log((LOG_ERR
, "defrouter_select: more than one "
1480 "default router is installed for interface :%s.\n",
1485 NDDR_REMREF(dr
); /* for this for loop */
1487 NDDR_REMREF(drrele
);
1490 * Check if the list changed when we gave up
1491 * the nd6_mutex lock
1493 if(genid
!= nd6_defrouter_genid
) {
1495 NDDR_REMREF(selected_dr
);
1500 NDDR_REMREF(selected_dr
);
1501 installed_dr
= NULL
;
1504 if (ndi
->ndefrouters
== 0) {
1506 "%s:%d: Interface %s no longer "
1507 "has any default routers. Abort.\n",
1508 __func__
, __LINE__
, if_name(ifp
)));
1512 "%s:%d: Iterate default router list again "
1513 "for interface %s, as the list seems to have "
1514 "changed during release-reaquire of global "
1515 "nd6_mutex lock.\n",
1516 __func__
, __LINE__
, if_name(ifp
)));
1518 is_installed_reachable
= FALSE
;
1519 genid
= nd6_defrouter_genid
;
1520 dr
= TAILQ_FIRST(&nd_defrouter
);
1522 dr
= TAILQ_NEXT(dr
, dr_entry
);
1527 * If none of the default routers was found to be reachable,
1528 * round-robin the list regardless of preference.
1529 * Please note selected_dr equal to NULL implies that even
1530 * installed default router is not reachable
1532 if (selected_dr
== NULL
) {
1534 for (dr
= TAILQ_NEXT(installed_dr
, dr_entry
); dr
;
1535 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1536 if (installed_dr
->ifp
!= dr
->ifp
)
1544 * If none was installed or the installed one if the last
1545 * one on the list, select the first one from the list
1547 if ((installed_dr
== NULL
) || (selected_dr
== NULL
)) {
1548 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1549 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1550 if (dr
->ifp
== ifp
) {
1557 if ((selected_dr
== NULL
) && (installed_dr
== NULL
)) {
1559 "%s:%d: Between release and reaquire of global "
1560 "nd6_mutex lock, the list seems to have changed "
1561 "and it does not have any default routers for "
1563 __func__
, __LINE__
, if_name(ifp
)));
1567 if (selected_dr
!= installed_dr
)
1568 NDDR_ADDREF(selected_dr
);
1569 } else if (installed_dr
!= NULL
) {
1570 if (installed_dr
!= selected_dr
) {
1572 * This means that selected default router is reachable
1573 * while installed one may or may not be.
1574 * Static router should always be considered as reachable
1575 * for router selection process.
1577 if ((installed_dr
->stateflags
& NDDRF_STATIC
) &&
1578 rtpref(installed_dr
) >= rtpref(selected_dr
)) {
1579 NDDR_REMREF(selected_dr
);
1580 selected_dr
= installed_dr
;
1581 } else if (is_installed_reachable
) {
1582 if (rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1583 NDDR_REMREF(selected_dr
);
1584 selected_dr
= installed_dr
;
1588 NDDR_REMREF(selected_dr
);
1594 * If the selected router is different than the installed one,
1595 * remove the installed router and install the selected one.
1596 * Note that the selected router is never NULL here.
1597 * Else check if the route entry scope has to be changed.
1599 lck_mtx_unlock(nd6_mutex
);
1600 if (installed_dr
!= selected_dr
) {
1602 "%s:%d: Found a better router for interface "
1603 "%s. Installing new default route.\n",
1604 __func__
, __LINE__
, if_name(ifp
)));
1605 if (installed_dr
!= NULL
) {
1606 defrouter_delreq(installed_dr
);
1609 * Install scoped route if the interface is not
1610 * the default nd6 interface.
1612 defrouter_addreq(selected_dr
,
1613 (selected_dr
->ifp
!= nd6_defifp
));
1614 } else if (((installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1615 (installed_dr
->ifp
== nd6_defifp
)) ||
1616 (!(installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1617 (installed_dr
->ifp
!= nd6_defifp
))) {
1619 "%s:%d: Need to reinstall default route for interface "
1620 "%s as its scope has changed.\n",
1621 __func__
, __LINE__
, if_name(ifp
)));
1622 defrouter_delreq(installed_dr
);
1623 defrouter_addreq(installed_dr
,
1624 (installed_dr
->ifp
!= nd6_defifp
));
1627 "%s:%d: No need to change the default "
1628 "route for interface %s.\n",
1629 __func__
, __LINE__
, if_name(ifp
)));
1631 lck_mtx_lock(nd6_mutex
);
1633 if (selected_dr
&& (selected_dr
!= installed_dr
))
1634 NDDR_REMREF(selected_dr
);
1636 NDDR_REMREF(installed_dr
);
1637 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1638 VERIFY(nd_defrouter_busy
);
1639 nd_defrouter_busy
= FALSE
;
1640 if (nd_defrouter_waiters
> 0) {
1641 nd_defrouter_waiters
= 0;
1642 wakeup(nd_defrouter_waitchan
);
1646 static struct nd_defrouter
*
1647 defrtrlist_update_common(struct nd_defrouter
*new, boolean_t scoped
)
1649 struct nd_defrouter
*dr
, *n
;
1650 struct ifnet
*ifp
= new->ifp
;
1651 struct nd_ifinfo
*ndi
= NULL
;
1652 struct timeval caltime
;
1654 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1656 if ((dr
= defrouter_lookup(&new->rtaddr
, ifp
)) != NULL
) {
1658 if (new->rtlifetime
== 0) {
1659 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1661 NDDR_REMREF(dr
); /* remove list reference */
1665 int oldpref
= rtpref(dr
);
1666 struct nd_defrouter
*p
= NULL
;
1668 dr
->flags
= new->flags
; /* xxx flag check */
1669 dr
->rtlifetime
= new->rtlifetime
;
1670 dr
->expire
= new->expire
;
1673 * If the preference does not change, there's no need
1674 * to sort the entries. If Scoped Routing is enabled,
1675 * put the primary/non-scoped router at the top of the
1676 * list of routers in the same preference band, unless
1677 * it's already at that position.
1679 /* same preference and scoped; just return */
1680 if (rtpref(new) == oldpref
&& scoped
)
1683 n
= TAILQ_FIRST(&nd_defrouter
);
1685 /* preference changed; sort it */
1686 if (rtpref(new) != oldpref
)
1689 /* not at the top of band; sort it */
1690 if (n
!= dr
&& rtpref(n
) == oldpref
&&
1691 (!p
|| rtpref(p
) > rtpref(n
)))
1695 n
= TAILQ_NEXT(n
, dr_entry
);
1698 /* nothing has changed, just return */
1699 if (n
== NULL
&& (scoped
||
1700 !(dr
->stateflags
& NDDRF_IFSCOPE
)))
1704 * preferred router may be changed, so relocate
1706 * XXX: calling TAILQ_REMOVE directly is a bad manner.
1707 * However, since defrtrlist_del() has many side
1708 * effects, we intentionally do so here.
1709 * defrouter_select() below will handle routing
1712 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1713 new->stateflags
= dr
->stateflags
;
1723 /* entry does not exist */
1724 if (new->rtlifetime
== 0) {
1728 n
= nddr_alloc(M_WAITOK
);
1733 ndi
= ND_IFINFO(ifp
);
1734 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1735 lck_mtx_lock(&ndi
->lock
);
1736 if (ip6_maxifdefrouters
>= 0 &&
1737 ndi
->ndefrouters
>= ip6_maxifdefrouters
) {
1738 lck_mtx_unlock(&ndi
->lock
);
1743 NDDR_ADDREF(n
); /* for the nd_defrouter list */
1744 NDDR_ADDREF(n
); /* for the caller */
1746 ++nd6_defrouter_genid
;
1748 VERIFY(ndi
->ndefrouters
!= 0);
1749 lck_mtx_unlock(&ndi
->lock
);
1751 nd6log2((LOG_INFO
, "%s: allocating defrouter %s\n", if_name(ifp
),
1752 ip6_sprintf(&new->rtaddr
)));
1754 getmicrotime(&caltime
);
1756 memcpy(&n
->rtaddr
, &new->rtaddr
, sizeof (n
->rtaddr
));
1757 n
->flags
= new->flags
;
1758 n
->stateflags
= new->stateflags
;
1759 n
->rtlifetime
= new->rtlifetime
;
1760 n
->expire
= new->expire
;
1761 n
->base_calendartime
= caltime
.tv_sec
;
1762 n
->base_uptime
= net_uptime();
1767 /* get nd6_service() to be scheduled as soon as it's convenient */
1768 ++nd6_sched_timeout_want
;
1771 * Insert the new router in the Default Router List;
1772 * The Default Router List should be in the descending order
1773 * of router-preferece. When Scoped Routing is disabled, routers
1774 * with the same preference are sorted in the arriving time order;
1775 * otherwise, the first entry in the list of routers having the same
1776 * preference is the primary default router, when the interface used
1777 * by the entry is the default interface.
1780 /* insert at the end of the group */
1781 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1782 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1783 if (rtpref(n
) > rtpref(dr
) ||
1784 (!scoped
&& rtpref(n
) == rtpref(dr
)))
1788 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1790 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
1792 defrouter_select(ifp
);
1797 static struct nd_defrouter
*
1798 defrtrlist_update(struct nd_defrouter
*new)
1800 struct nd_defrouter
*dr
;
1802 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1803 dr
= defrtrlist_update_common(new,
1804 (nd6_defifp
!= NULL
&& new->ifp
!= nd6_defifp
));
1809 static struct nd_pfxrouter
*
1810 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1812 struct nd_pfxrouter
*search
;
1814 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1815 NDPR_LOCK_ASSERT_HELD(pr
);
1817 for (search
= pr
->ndpr_advrtrs
.lh_first
; search
;
1818 search
= search
->pfr_next
) {
1819 if (search
->router
== dr
)
1827 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1829 struct nd_pfxrouter
*new;
1831 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1832 NDPR_LOCK_ASSERT_NOTHELD(pr
);
1834 new = zalloc(ndprtr_zone
);
1837 bzero(new, sizeof (*new));
1841 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
1845 pfxlist_onlink_check();
1849 pfxrtr_del(struct nd_pfxrouter
*pfr
, struct nd_prefix
*pr
)
1851 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1852 NDPR_LOCK_ASSERT_HELD(pr
);
1854 LIST_REMOVE(pfr
, pfr_entry
);
1855 zfree(ndprtr_zone
, pfr
);
1859 * The routine has been modified to atomically refresh expiry
1860 * time for nd6 prefix as the part of lookup.
1861 * There's a corner case where a system going
1862 * in sleep gets rid of manual addresses configured in the system
1863 * and then schedules the prefix for deletion.
1864 * However before the prefix gets deleted, if system comes out
1865 * from sleep and configures same address before prefix deletion
1866 * , the later prefix deletion will remove the prefix route and
1867 * the system will not be able to communicate with other IPv6
1868 * neighbor nodes in the same subnet.
1871 nd6_prefix_lookup(struct nd_prefix
*pr
, int nd6_prefix_expiry
)
1873 struct nd_prefix
*search
;
1875 lck_mtx_lock(nd6_mutex
);
1876 for (search
= nd_prefix
.lh_first
; search
; search
= search
->ndpr_next
) {
1878 if (pr
->ndpr_ifp
== search
->ndpr_ifp
&&
1879 pr
->ndpr_plen
== search
->ndpr_plen
&&
1880 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1881 &search
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1882 if (nd6_prefix_expiry
!= ND6_PREFIX_EXPIRY_UNSPEC
) {
1883 search
->ndpr_expire
= nd6_prefix_expiry
;
1885 NDPR_ADDREF_LOCKED(search
);
1886 NDPR_UNLOCK(search
);
1889 NDPR_UNLOCK(search
);
1891 lck_mtx_unlock(nd6_mutex
);
1897 nd6_prelist_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
,
1898 struct nd_prefix
**newp
, boolean_t force_scoped
)
1900 struct nd_prefix
*new = NULL
;
1901 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1902 struct nd_ifinfo
*ndi
= NULL
;
1905 if (ip6_maxifprefixes
>= 0) {
1906 ndi
= ND_IFINFO(ifp
);
1907 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1908 lck_mtx_lock(&ndi
->lock
);
1909 if (ndi
->nprefixes
>= ip6_maxifprefixes
) {
1910 lck_mtx_unlock(&ndi
->lock
);
1913 lck_mtx_unlock(&ndi
->lock
);
1916 new = ndpr_alloc(M_WAITOK
);
1922 new->ndpr_ifp
= pr
->ndpr_ifp
;
1923 new->ndpr_prefix
= pr
->ndpr_prefix
;
1924 new->ndpr_plen
= pr
->ndpr_plen
;
1925 new->ndpr_vltime
= pr
->ndpr_vltime
;
1926 new->ndpr_pltime
= pr
->ndpr_pltime
;
1927 new->ndpr_flags
= pr
->ndpr_flags
;
1928 if (pr
->ndpr_stateflags
& NDPRF_STATIC
)
1929 new->ndpr_stateflags
|= NDPRF_STATIC
;
1931 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
1936 new->ndpr_lastupdate
= net_uptime();
1939 NDPR_ADDREF_LOCKED(new); /* for caller */
1941 /* initialization */
1942 LIST_INIT(&new->ndpr_advrtrs
);
1943 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
1944 /* make prefix in the canonical form */
1945 for (i
= 0; i
< 4; i
++)
1946 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1947 new->ndpr_mask
.s6_addr32
[i
];
1951 /* get nd6_service() to be scheduled as soon as it's convenient */
1952 ++nd6_sched_timeout_want
;
1954 lck_mtx_lock(nd6_mutex
);
1955 /* link ndpr_entry to nd_prefix list */
1956 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
1957 new->ndpr_debug
|= IFD_ATTACHED
;
1958 NDPR_ADDREF(new); /* for nd_prefix list */
1960 lck_mtx_lock(&ndi
->lock
);
1962 VERIFY(ndi
->nprefixes
!= 0);
1963 lck_mtx_unlock(&ndi
->lock
);
1965 /* ND_OPT_PI_FLAG_ONLINK processing */
1966 if (new->ndpr_raf_onlink
) {
1969 if ((e
= nd6_prefix_onlink_common(new, force_scoped
,
1970 new->ndpr_ifp
->if_index
)) != 0) {
1971 nd6log((LOG_ERR
, "nd6_prelist_add: failed to make "
1972 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
1973 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
1974 new->ndpr_plen
, force_scoped
? "scoped" :
1975 "non-scoped", if_name(ifp
), e
));
1976 /* proceed anyway. XXX: is it correct? */
1981 pfxrtr_add(new, dr
);
1984 lck_mtx_unlock(nd6_mutex
);
1990 * Caller must have held an extra reference on nd_prefix.
1993 prelist_remove(struct nd_prefix
*pr
)
1995 struct nd_pfxrouter
*pfr
, *next
;
1996 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1998 struct nd_ifinfo
*ndi
= NULL
;
2000 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2001 NDPR_LOCK_ASSERT_HELD(pr
);
2003 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
2007 * If there are no more addresses, defunct the prefix. This is needed
2008 * because we don't want multiple threads calling prelist_remove() for
2009 * the same prefix and this might happen because we unlock nd6_mutex
2012 if (pr
->ndpr_addrcnt
== 0)
2013 pr
->ndpr_stateflags
|= NDPRF_DEFUNCT
;
2015 /* make sure to invalidate the prefix until it is really freed. */
2016 pr
->ndpr_vltime
= 0;
2017 pr
->ndpr_pltime
= 0;
2020 * Though these flags are now meaningless, we'd rather keep the value
2021 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2022 * when executing "ndp -p".
2024 if (pr
->ndpr_stateflags
& NDPRF_ONLINK
) {
2025 NDPR_ADDREF_LOCKED(pr
);
2027 lck_mtx_unlock(nd6_mutex
);
2028 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2029 nd6log((LOG_ERR
, "prelist_remove: failed to make "
2030 "%s/%d offlink on %s, errno=%d\n",
2031 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2032 pr
->ndpr_plen
, if_name(ifp
), e
));
2033 /* what should we do? */
2035 lck_mtx_lock(nd6_mutex
);
2037 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2041 if (pr
->ndpr_addrcnt
> 0) {
2043 * The state might have changed if we called
2044 * nd6_prefix_offlink().
2046 pr
->ndpr_stateflags
&= ~NDPRF_DEFUNCT
;
2047 return; /* notice here? */
2050 /* unlink ndpr_entry from nd_prefix list */
2051 LIST_REMOVE(pr
, ndpr_entry
);
2052 pr
->ndpr_debug
&= ~IFD_ATTACHED
;
2054 /* free list of routers that adversed the prefix */
2055 for (pfr
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr
= next
) {
2056 next
= pfr
->pfr_next
;
2057 pfxrtr_del(pfr
, pr
);
2060 ndi
= ND_IFINFO(ifp
);
2061 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2062 lck_mtx_lock(&ndi
->lock
);
2063 VERIFY(ndi
->nprefixes
> 0);
2065 lck_mtx_unlock(&ndi
->lock
);
2067 /* This must not be the last reference to the nd_prefix */
2068 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2069 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__
, pr
);
2074 * Don't call pfxlist_onlink_check() here because we are
2075 * holding the NDPR lock and this could cause a deadlock when
2076 * there are multiple threads executing pfxlist_onlink_check().
2082 struct nd_prefix
*new,
2083 struct nd_defrouter
*dr
, /* may be NULL */
2087 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
2089 struct ifnet
*ifp
= new->ndpr_ifp
;
2090 struct nd_prefix
*pr
;
2094 struct in6_addrlifetime lt6_tmp
;
2095 uint64_t timenow
= net_uptime();
2097 /* no need to lock "new" here, as it is local to the caller */
2098 NDPR_LOCK_ASSERT_NOTHELD(new);
2103 * Authenticity for NA consists authentication for
2104 * both IP header and IP datagrams, doesn't it ?
2106 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
2107 auth
= (m
->m_flags
& M_AUTHIPHDR
) && (m
->m_flags
& M_AUTHIPDGM
);
2111 if ((pr
= nd6_prefix_lookup(new, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
2113 * nd6_prefix_lookup() ensures that pr and new have the same
2114 * prefix on a same interface.
2118 * Update prefix information. Note that the on-link (L) bit
2119 * and the autonomous (A) bit should NOT be changed from 1
2122 lck_mtx_lock(nd6_mutex
);
2124 if (new->ndpr_raf_onlink
== 1)
2125 pr
->ndpr_raf_onlink
= 1;
2126 if (new->ndpr_raf_auto
== 1)
2127 pr
->ndpr_raf_auto
= 1;
2128 if (new->ndpr_raf_onlink
) {
2129 pr
->ndpr_vltime
= new->ndpr_vltime
;
2130 pr
->ndpr_pltime
= new->ndpr_pltime
;
2131 (void) in6_init_prefix_ltimes(pr
); /* XXX error case? */
2132 pr
->ndpr_lastupdate
= net_uptime();
2135 NDPR_ADDREF_LOCKED(pr
);
2136 if (new->ndpr_raf_onlink
&&
2137 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
2141 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2143 "prelist_update: failed to make "
2144 "the prefix %s/%d on-link on %s "
2146 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2147 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
2148 /* proceed anyway. XXX: is it correct? */
2153 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
) {
2160 lck_mtx_unlock(nd6_mutex
);
2162 struct nd_prefix
*newpr
= NULL
;
2166 if (new->ndpr_vltime
== 0)
2168 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
2171 bzero(&new->ndpr_addr
, sizeof (struct in6_addr
));
2173 error
= nd6_prelist_add(new, dr
, &newpr
, FALSE
);
2174 if (error
!= 0 || newpr
== NULL
) {
2175 nd6log((LOG_NOTICE
, "prelist_update: "
2176 "nd6_prelist_add failed for %s/%d on %s "
2177 "errno=%d, returnpr=0x%llx\n",
2178 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2179 new->ndpr_plen
, if_name(new->ndpr_ifp
),
2180 error
, (uint64_t)VM_KERNEL_ADDRPERM(newpr
)));
2181 goto end
; /* we should just give up in this case. */
2185 * XXX: from the ND point of view, we can ignore a prefix
2186 * with the on-link bit being zero. However, we need a
2187 * prefix structure for references from autoconfigured
2188 * addresses. Thus, we explicitly make sure that the prefix
2189 * itself expires now.
2192 if (newpr
->ndpr_raf_onlink
== 0) {
2193 newpr
->ndpr_vltime
= 0;
2194 newpr
->ndpr_pltime
= 0;
2195 in6_init_prefix_ltimes(newpr
);
2203 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
2204 * Note that pr must be non NULL at this point.
2207 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
2208 if (!new->ndpr_raf_auto
)
2212 * 5.5.3 (b). the link-local prefix should have been ignored in
2216 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2217 if (new->ndpr_pltime
> new->ndpr_vltime
) {
2218 error
= EINVAL
; /* XXX: won't be used */
2223 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2224 * an address configured by stateless autoconfiguration already in the
2225 * list of addresses associated with the interface, and the Valid
2226 * Lifetime is not 0, form an address. We first check if we have
2227 * a matching prefix.
2229 ifnet_lock_shared(ifp
);
2230 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2231 struct in6_ifaddr
*ifa6
;
2232 u_int32_t remaininglifetime
;
2235 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2239 ifa6
= (struct in6_ifaddr
*)ifa
;
2242 * We only consider autoconfigured addresses as per RFC 4862.
2244 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
2249 * Spec is not clear here, but I believe we should concentrate
2250 * on unicast (i.e. not anycast) addresses.
2251 * XXX: other ia6_flags? detached or duplicated?
2253 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0) {
2258 * Ignore the address if it is not associated with a prefix
2259 * or is associated with a prefix that is different from this
2260 * one. (pr is never NULL here)
2262 if (ifa6
->ia6_ndpr
!= pr
) {
2267 if (ia6_match
== NULL
) { /* remember the first one */
2269 IFA_ADDREF_LOCKED(ifa
); /* for ia6_match */
2273 * An already autoconfigured address matched. Now that we
2274 * are sure there is at least one matched address, we can
2275 * proceed to 5.5.3. (e): update the lifetimes according to the
2276 * "two hours" rule and the privacy extension.
2278 #define TWOHOUR (120*60)
2280 /* retrieve time as uptime (last arg is 0) */
2281 in6ifa_getlifetime(ifa6
, <6_tmp
, 0);
2283 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
2284 remaininglifetime
= ND6_INFINITE_LIFETIME
;
2285 else if (timenow
- ifa6
->ia6_updatetime
> lt6_tmp
.ia6t_vltime
) {
2287 * The case of "invalid" address. We should usually
2288 * not see this case.
2290 remaininglifetime
= 0;
2292 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
2293 (timenow
- ifa6
->ia6_updatetime
);
2295 /* when not updating, keep the current stored lifetime. */
2296 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
2298 if (TWOHOUR
< new->ndpr_vltime
||
2299 remaininglifetime
< new->ndpr_vltime
) {
2300 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2301 } else if (remaininglifetime
<= TWOHOUR
) {
2303 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2307 * new->ndpr_vltime <= TWOHOUR &&
2308 * TWOHOUR < remaininglifetime
2310 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
2313 /* The 2 hour rule is not imposed for preferred lifetime. */
2314 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
2316 /* Special handling for lifetimes of temporary addresses. */
2317 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
2318 u_int32_t maxvltime
, maxpltime
;
2320 /* Constrain lifetimes to system limits. */
2321 if (lt6_tmp
.ia6t_vltime
> ip6_temp_valid_lifetime
)
2322 lt6_tmp
.ia6t_vltime
= ip6_temp_valid_lifetime
;
2323 if (lt6_tmp
.ia6t_pltime
> ip6_temp_preferred_lifetime
)
2324 lt6_tmp
.ia6t_pltime
=
2325 ip6_temp_preferred_lifetime
-
2329 * According to RFC 4941, section 3.3 (1), we only
2330 * update the lifetimes when they are in the maximum
2333 if (ip6_temp_valid_lifetime
>
2334 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2335 ip6_desync_factor
)) {
2336 maxvltime
= ip6_temp_valid_lifetime
-
2337 (timenow
- ifa6
->ia6_createtime
) -
2341 if (ip6_temp_preferred_lifetime
>
2342 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2343 ip6_desync_factor
)) {
2344 maxpltime
= ip6_temp_preferred_lifetime
-
2345 (timenow
- ifa6
->ia6_createtime
) -
2350 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
2351 lt6_tmp
.ia6t_vltime
> maxvltime
)
2352 lt6_tmp
.ia6t_vltime
= maxvltime
;
2354 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
2355 lt6_tmp
.ia6t_pltime
> maxpltime
)
2356 lt6_tmp
.ia6t_pltime
= maxpltime
;
2359 in6_init_address_ltimes(pr
, <6_tmp
);
2361 in6ifa_setlifetime(ifa6
, <6_tmp
);
2362 ifa6
->ia6_updatetime
= timenow
;
2365 ifnet_lock_done(ifp
);
2366 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
2368 * 5.5.3 (d) (continued)
2369 * No address matched and the valid lifetime is non-zero.
2370 * Create a new address.
2373 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
))
2376 * note that we should use pr (not new) for reference.
2378 IFA_LOCK(&ia6
->ia_ifa
);
2381 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2383 VERIFY(pr
->ndpr_addrcnt
!= 0);
2385 IFA_UNLOCK(&ia6
->ia_ifa
);
2389 * When a new public address is created as described
2390 * in RFC 4862, also create a new temporary address.
2393 * When an interface connects to a new link, a new
2394 * randomized interface identifier should be generated
2395 * immediately together with a new set of temporary
2396 * addresses. Thus, we specifiy 1 as the 2nd arg of
2399 if (ip6_use_tempaddr
) {
2401 if ((e
= in6_tmpifadd(ia6
, 1)) != 0) {
2402 nd6log((LOG_NOTICE
, "prelist_update: "
2403 "failed to create a temporary "
2404 "address, errno=%d\n",
2408 IFA_REMREF(&ia6
->ia_ifa
);
2412 * A newly added address might affect the status
2413 * of other addresses, so we check and update it.
2414 * XXX: what if address duplication happens?
2416 lck_mtx_lock(nd6_mutex
);
2417 pfxlist_onlink_check();
2418 lck_mtx_unlock(nd6_mutex
);
2427 if (ia6_match
!= NULL
)
2428 IFA_REMREF(&ia6_match
->ia_ifa
);
2433 * Neighbor Discover Default Router structure reference counting routines.
2435 static struct nd_defrouter
*
2438 struct nd_defrouter
*dr
;
2440 dr
= (how
== M_WAITOK
) ? zalloc(nddr_zone
) : zalloc_noblock(nddr_zone
);
2442 bzero(dr
, nddr_size
);
2443 lck_mtx_init(&dr
->nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2444 dr
->nddr_debug
|= IFD_ALLOC
;
2445 if (nddr_debug
!= 0) {
2446 dr
->nddr_debug
|= IFD_DEBUG
;
2447 dr
->nddr_trace
= nddr_trace
;
2454 nddr_free(struct nd_defrouter
*dr
)
2457 if (dr
->nddr_debug
& IFD_ATTACHED
) {
2458 panic("%s: attached nddr %p is being freed", __func__
, dr
);
2460 } else if (!(dr
->nddr_debug
& IFD_ALLOC
)) {
2461 panic("%s: nddr %p cannot be freed", __func__
, dr
);
2464 dr
->nddr_debug
&= ~IFD_ALLOC
;
2467 lck_mtx_destroy(&dr
->nddr_lock
, ifa_mtx_grp
);
2468 zfree(nddr_zone
, dr
);
2472 nddr_trace(struct nd_defrouter
*dr
, int refhold
)
2474 struct nd_defrouter_dbg
*dr_dbg
= (struct nd_defrouter_dbg
*)dr
;
2479 if (!(dr
->nddr_debug
& IFD_DEBUG
)) {
2480 panic("%s: nddr %p has no debug structure", __func__
, dr
);
2484 cnt
= &dr_dbg
->nddr_refhold_cnt
;
2485 tr
= dr_dbg
->nddr_refhold
;
2487 cnt
= &dr_dbg
->nddr_refrele_cnt
;
2488 tr
= dr_dbg
->nddr_refrele
;
2491 idx
= atomic_add_16_ov(cnt
, 1) % NDDR_TRACE_HIST_SIZE
;
2492 ctrace_record(&tr
[idx
]);
2496 nddr_addref(struct nd_defrouter
*nddr
, int locked
)
2500 NDDR_LOCK_SPIN(nddr
);
2502 NDDR_LOCK_ASSERT_HELD(nddr
);
2504 if (++nddr
->nddr_refcount
== 0) {
2505 panic("%s: nddr %p wraparound refcnt\n", __func__
, nddr
);
2507 } else if (nddr
->nddr_trace
!= NULL
) {
2508 (*nddr
->nddr_trace
)(nddr
, TRUE
);
2515 struct nd_defrouter
*
2516 nddr_remref(struct nd_defrouter
*nddr
, int locked
)
2520 NDDR_LOCK_SPIN(nddr
);
2522 NDDR_LOCK_ASSERT_HELD(nddr
);
2524 if (nddr
->nddr_refcount
== 0) {
2525 panic("%s: nddr %p negative refcnt\n", __func__
, nddr
);
2527 } else if (nddr
->nddr_trace
!= NULL
) {
2528 (*nddr
->nddr_trace
)(nddr
, FALSE
);
2531 if (--nddr
->nddr_refcount
== 0) {
2537 if (!locked
&& nddr
!= NULL
)
2544 nddr_getexpire(struct nd_defrouter
*dr
)
2546 struct timeval caltime
;
2549 if (dr
->expire
!= 0) {
2550 /* account for system time change */
2551 getmicrotime(&caltime
);
2553 dr
->base_calendartime
+=
2554 NET_CALCULATE_CLOCKSKEW(caltime
,
2555 dr
->base_calendartime
, net_uptime(), dr
->base_uptime
);
2557 expiry
= dr
->base_calendartime
+
2558 dr
->expire
- dr
->base_uptime
;
2566 * Neighbor Discover Prefix structure reference counting routines.
2568 static struct nd_prefix
*
2571 struct nd_prefix
*pr
;
2573 pr
= (how
== M_WAITOK
) ? zalloc(ndpr_zone
) : zalloc_noblock(ndpr_zone
);
2575 bzero(pr
, ndpr_size
);
2576 lck_mtx_init(&pr
->ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2577 RB_INIT(&pr
->ndpr_prproxy_sols
);
2578 pr
->ndpr_debug
|= IFD_ALLOC
;
2579 if (ndpr_debug
!= 0) {
2580 pr
->ndpr_debug
|= IFD_DEBUG
;
2581 pr
->ndpr_trace
= ndpr_trace
;
2588 ndpr_free(struct nd_prefix
*pr
)
2591 if (pr
->ndpr_debug
& IFD_ATTACHED
) {
2592 panic("%s: attached ndpr %p is being freed", __func__
, pr
);
2594 } else if (!(pr
->ndpr_debug
& IFD_ALLOC
)) {
2595 panic("%s: ndpr %p cannot be freed", __func__
, pr
);
2597 } else if (pr
->ndpr_rt
!= NULL
) {
2598 panic("%s: ndpr %p route %p not freed", __func__
, pr
,
2601 } else if (pr
->ndpr_prproxy_sols_cnt
!= 0) {
2602 panic("%s: ndpr %p non-zero solicitors count (%d)",
2603 __func__
, pr
, pr
->ndpr_prproxy_sols_cnt
);
2605 } else if (!RB_EMPTY(&pr
->ndpr_prproxy_sols
)) {
2606 panic("%s: ndpr %p non-empty solicitors tree", __func__
, pr
);
2609 pr
->ndpr_debug
&= ~IFD_ALLOC
;
2612 lck_mtx_destroy(&pr
->ndpr_lock
, ifa_mtx_grp
);
2613 zfree(ndpr_zone
, pr
);
2617 ndpr_trace(struct nd_prefix
*pr
, int refhold
)
2619 struct nd_prefix_dbg
*pr_dbg
= (struct nd_prefix_dbg
*)pr
;
2624 if (!(pr
->ndpr_debug
& IFD_DEBUG
)) {
2625 panic("%s: ndpr %p has no debug structure", __func__
, pr
);
2629 cnt
= &pr_dbg
->ndpr_refhold_cnt
;
2630 tr
= pr_dbg
->ndpr_refhold
;
2632 cnt
= &pr_dbg
->ndpr_refrele_cnt
;
2633 tr
= pr_dbg
->ndpr_refrele
;
2636 idx
= atomic_add_16_ov(cnt
, 1) % NDPR_TRACE_HIST_SIZE
;
2637 ctrace_record(&tr
[idx
]);
2641 ndpr_addref(struct nd_prefix
*ndpr
, int locked
)
2644 NDPR_LOCK_SPIN(ndpr
);
2646 NDPR_LOCK_ASSERT_HELD(ndpr
);
2648 if (++ndpr
->ndpr_refcount
== 0) {
2649 panic("%s: ndpr %p wraparound refcnt\n", __func__
, ndpr
);
2651 } else if (ndpr
->ndpr_trace
!= NULL
) {
2652 (*ndpr
->ndpr_trace
)(ndpr
, TRUE
);
2660 ndpr_remref(struct nd_prefix
*ndpr
, int locked
)
2663 NDPR_LOCK_SPIN(ndpr
);
2665 NDPR_LOCK_ASSERT_HELD(ndpr
);
2667 if (ndpr
->ndpr_refcount
== 0) {
2668 panic("%s: ndpr %p negative refcnt\n", __func__
, ndpr
);
2670 } else if (ndpr
->ndpr_trace
!= NULL
) {
2671 (*ndpr
->ndpr_trace
)(ndpr
, FALSE
);
2674 if (--ndpr
->ndpr_refcount
== 0) {
2675 if (ndpr
->ndpr_addrcnt
!= 0) {
2676 panic("%s: freeing ndpr %p with outstanding address "
2677 "reference (%d)", __func__
, ndpr
,
2678 ndpr
->ndpr_addrcnt
);
2686 if (!locked
&& ndpr
!= NULL
)
2693 ndpr_getexpire(struct nd_prefix
*pr
)
2695 struct timeval caltime
;
2698 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_vltime
!= ND6_INFINITE_LIFETIME
) {
2699 /* account for system time change */
2700 getmicrotime(&caltime
);
2702 pr
->ndpr_base_calendartime
+=
2703 NET_CALCULATE_CLOCKSKEW(caltime
,
2704 pr
->ndpr_base_calendartime
, net_uptime(),
2705 pr
->ndpr_base_uptime
);
2707 expiry
= pr
->ndpr_base_calendartime
+
2708 pr
->ndpr_expire
- pr
->ndpr_base_uptime
;
2716 * A supplement function used in the on-link detection below;
2717 * detect if a given prefix has a (probably) reachable advertising router.
2718 * XXX: lengthy function name...
2720 * Callers *must* increase the reference count of nd_prefix.
2722 static struct nd_pfxrouter
*
2723 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
2725 struct nd_pfxrouter
*pfxrtr
;
2727 struct llinfo_nd6
*ln
;
2729 struct in6_addr rtaddr
;
2732 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2733 NDPR_LOCK_ASSERT_HELD(pr
);
2735 genid
= pr
->ndpr_genid
;
2736 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2738 ifp
= pfxrtr
->router
->ifp
;
2739 if (pfxrtr
->router
->stateflags
& NDDRF_MAPPED
)
2740 rtaddr
= pfxrtr
->router
->rtaddr_mapped
;
2742 rtaddr
= pfxrtr
->router
->rtaddr
;
2745 lck_mtx_unlock(nd6_mutex
);
2746 /* Callee returns a locked route upon success */
2747 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
2748 RT_LOCK_ASSERT_HELD(rt
);
2749 if ((ln
= rt
->rt_llinfo
) != NULL
&&
2750 ND6_IS_LLINFO_PROBREACH(ln
)) {
2751 RT_REMREF_LOCKED(rt
);
2753 lck_mtx_lock(nd6_mutex
);
2757 RT_REMREF_LOCKED(rt
);
2760 lck_mtx_lock(nd6_mutex
);
2762 if (pr
->ndpr_genid
!= genid
) {
2763 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2764 genid
= pr
->ndpr_genid
;
2766 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
);
2768 NDPR_LOCK_ASSERT_HELD(pr
);
2775 * Check if each prefix in the prefix list has at least one available router
2776 * that advertised the prefix (a router is "available" if its neighbor cache
2777 * entry is reachable or probably reachable).
2778 * If the check fails, the prefix may be off-link, because, for example,
2779 * we have moved from the network but the lifetime of the prefix has not
2780 * expired yet. So we should not use the prefix if there is another prefix
2781 * that has an available router.
2782 * But, if there is no prefix that has an available router, we still regards
2783 * all the prefixes as on-link. This is because we can't tell if all the
2784 * routers are simply dead or if we really moved from the network and there
2785 * is no router around us.
2788 pfxlist_onlink_check(void)
2790 struct nd_prefix
*pr
, *prclear
;
2791 struct in6_ifaddr
*ifa
;
2792 struct nd_defrouter
*dr
;
2793 struct nd_pfxrouter
*pfxrtr
= NULL
;
2794 int err
, i
, found
= 0;
2795 struct ifaddr
**ifap
= NULL
;
2796 struct nd_prefix
*ndpr
;
2798 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2800 while (nd_prefix_busy
) {
2801 nd_prefix_waiters
++;
2802 msleep(nd_prefix_waitchan
, nd6_mutex
, (PZERO
-1),
2804 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2806 nd_prefix_busy
= TRUE
;
2809 * Check if there is a prefix that has a reachable advertising
2812 pr
= nd_prefix
.lh_first
;
2815 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
) {
2820 NDPR_ADDREF_LOCKED(pr
);
2821 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
) &&
2822 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
2823 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2829 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2833 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
2834 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
2835 * stop us from checking the same prefix twice.
2837 pr
= nd_prefix
.lh_first
;
2839 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2841 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2842 NDPR_UNLOCK(prclear
);
2845 * If we have no such prefix, check whether we still have a router
2846 * that does not advertise any prefixes.
2849 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
2850 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2851 struct nd_prefix
*pr0
;
2853 for (pr0
= nd_prefix
.lh_first
; pr0
;
2854 pr0
= pr0
->ndpr_next
) {
2856 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
) {
2866 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && pfxrtr
== NULL
)) {
2868 * There is at least one prefix that has a reachable router,
2869 * or at least a router which probably does not advertise
2870 * any prefixes. The latter would be the case when we move
2871 * to a new link where we have a router that does not provide
2872 * prefixes and we configure an address by hand.
2873 * Detach prefixes which have no reachable advertising
2874 * router, and attach other prefixes.
2876 pr
= nd_prefix
.lh_first
;
2880 * We aren't interested prefixes already processed,
2881 * nor in prefixes without the L bit
2882 * set nor in static prefixes
2884 if (pr
->ndpr_raf_onlink
== 0 ||
2885 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2886 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2891 NDPR_ADDREF_LOCKED(pr
);
2892 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2893 find_pfxlist_reachable_router(pr
) == NULL
&&
2894 (pr
->ndpr_debug
& IFD_ATTACHED
))
2895 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
2896 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2897 find_pfxlist_reachable_router(pr
) != NULL
&&
2898 (pr
->ndpr_debug
& IFD_ATTACHED
))
2899 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2900 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2904 * Since find_pfxlist_reachable_router() drops the
2905 * nd6_mutex, we have to start over, but the
2906 * NDPRF_PROCESSED_ONLINK flag will stop us from
2907 * checking the same prefix twice.
2909 pr
= nd_prefix
.lh_first
;
2912 /* there is no prefix that has a reachable router */
2913 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2915 if (pr
->ndpr_raf_onlink
== 0 ||
2916 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2920 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
2921 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2925 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2927 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2928 NDPR_UNLOCK(prclear
);
2931 * Remove each interface route associated with a (just) detached
2932 * prefix, and reinstall the interface route for a (just) attached
2933 * prefix. Note that all attempt of reinstallation does not
2934 * necessarily success, when a same prefix is shared among multiple
2935 * interfaces. Such cases will be handled in nd6_prefix_onlink,
2936 * so we don't have to care about them.
2938 pr
= nd_prefix
.lh_first
;
2943 if (pr
->ndpr_raf_onlink
== 0 ||
2944 pr
->ndpr_stateflags
& NDPRF_STATIC
||
2945 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2946 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
2951 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2952 NDPR_ADDREF_LOCKED(pr
);
2953 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2954 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
2956 lck_mtx_unlock(nd6_mutex
);
2957 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2959 "pfxlist_onlink_check: failed to "
2960 "make %s/%d offlink, errno=%d\n",
2961 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2964 lck_mtx_lock(nd6_mutex
);
2966 pr
= nd_prefix
.lh_first
;
2969 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2970 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
2971 pr
->ndpr_raf_onlink
) {
2973 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2975 "pfxlist_onlink_check: failed to "
2976 "make %s/%d offlink, errno=%d\n",
2977 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2981 pr
= nd_prefix
.lh_first
;
2989 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2991 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2992 NDPR_UNLOCK(prclear
);
2994 VERIFY(nd_prefix_busy
);
2995 nd_prefix_busy
= FALSE
;
2996 if (nd_prefix_waiters
> 0) {
2997 nd_prefix_waiters
= 0;
2998 wakeup(nd_prefix_waitchan
);
3002 * Changes on the prefix status might affect address status as well.
3003 * Make sure that all addresses derived from an attached prefix are
3004 * attached, and that all addresses derived from a detached prefix are
3005 * detached. Note, however, that a manually configured address should
3006 * always be attached.
3007 * The precise detection logic is same as the one for prefixes.
3009 * ifnet_get_address_list_family_internal() may fail due to memory
3010 * pressure, but we will eventually be called again when we receive
3011 * another NA, RA, or when the link status changes.
3013 err
= ifnet_get_address_list_family_internal(NULL
, &ifap
, AF_INET6
, 0,
3015 if (err
!= 0 || ifap
== NULL
) {
3016 nd6log((LOG_ERR
, "%s: ifnet_get_address_list_family_internal "
3017 "failed", __func__
));
3020 for (i
= 0; ifap
[i
]; i
++) {
3021 ifa
= ifatoia6(ifap
[i
]);
3022 IFA_LOCK(&ifa
->ia_ifa
);
3023 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3024 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3025 IFA_UNLOCK(&ifa
->ia_ifa
);
3028 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3030 * This can happen when we first configure the address
3031 * (i.e. the address exists, but the prefix does not).
3032 * XXX: complicated relationships...
3034 IFA_UNLOCK(&ifa
->ia_ifa
);
3037 IFA_UNLOCK(&ifa
->ia_ifa
);
3040 NDPR_ADDREF_LOCKED(ndpr
);
3041 if (find_pfxlist_reachable_router(ndpr
)) {
3042 if (NDPR_REMREF_LOCKED(ndpr
) == NULL
) {
3054 for (i
= 0; ifap
[i
]; i
++) {
3055 ifa
= ifatoia6(ifap
[i
]);
3056 IFA_LOCK(&ifa
->ia_ifa
);
3057 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3058 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3059 IFA_UNLOCK(&ifa
->ia_ifa
);
3062 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3063 /* XXX: see above. */
3064 IFA_UNLOCK(&ifa
->ia_ifa
);
3067 IFA_UNLOCK(&ifa
->ia_ifa
);
3069 NDPR_ADDREF_LOCKED(ndpr
);
3070 if (find_pfxlist_reachable_router(ndpr
)) {
3072 IFA_LOCK(&ifa
->ia_ifa
);
3073 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3074 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3075 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3076 IFA_UNLOCK(&ifa
->ia_ifa
);
3077 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3079 IFA_UNLOCK(&ifa
->ia_ifa
);
3083 IFA_LOCK(&ifa
->ia_ifa
);
3084 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
3085 IFA_UNLOCK(&ifa
->ia_ifa
);
3090 for (i
= 0; ifap
[i
]; i
++) {
3091 ifa
= ifatoia6(ifap
[i
]);
3092 IFA_LOCK(&ifa
->ia_ifa
);
3093 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3094 IFA_UNLOCK(&ifa
->ia_ifa
);
3097 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3098 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3099 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3100 IFA_UNLOCK(&ifa
->ia_ifa
);
3101 /* Do we need a delay in this case? */
3102 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3104 IFA_UNLOCK(&ifa
->ia_ifa
);
3108 ifnet_free_address_list(ifap
);
3111 static struct nd_prefix
*
3112 nd6_prefix_equal_lookup(struct nd_prefix
*pr
, boolean_t primary_only
)
3114 struct nd_prefix
*opr
;
3116 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3118 for (opr
= nd_prefix
.lh_first
; opr
; opr
= opr
->ndpr_next
) {
3123 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3127 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
3128 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
3129 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
) &&
3131 !(opr
->ndpr_stateflags
& NDPRF_IFSCOPE
))) {
3132 NDPR_ADDREF_LOCKED(opr
);
3142 * Synchronize the interface routes of similar prefixes on different
3143 * interfaces; the one using the default interface would be (re)installed
3144 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3147 nd6_prefix_sync(struct ifnet
*ifp
)
3149 struct nd_prefix
*pr
, *opr
;
3152 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3157 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3159 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
3163 if (pr
->ndpr_ifp
== ifp
&&
3164 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) &&
3165 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3175 /* Remove conflicting entries */
3176 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3178 lck_mtx_unlock(nd6_mutex
);
3179 err
= nd6_prefix_offlink(opr
);
3180 lck_mtx_lock(nd6_mutex
);
3183 "%s: failed to make %s/%d offlink on %s, "
3184 "errno=%d\n", __func__
,
3185 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3186 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3190 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3191 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3192 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
)));
3195 lck_mtx_unlock(nd6_mutex
);
3196 err
= nd6_prefix_offlink(pr
);
3197 lck_mtx_lock(nd6_mutex
);
3200 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3201 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3202 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3205 /* Add the entries back */
3207 err
= nd6_prefix_onlink_scoped(opr
, opr
->ndpr_ifp
->if_index
);
3210 "%s: failed to make %s/%d scoped onlink on %s, "
3211 "errno=%d\n", __func__
,
3212 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3213 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3217 err
= nd6_prefix_onlink_scoped(pr
, IFSCOPE_NONE
);
3220 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3221 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3222 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3227 "%s: error promoting %s/%d to %s from %s\n",
3228 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3229 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3230 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3233 "%s: %s/%d promoted, previously on %s\n",
3234 if_name(pr
->ndpr_ifp
),
3235 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
,
3236 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3244 nd6_prefix_onlink_common(struct nd_prefix
*pr
, boolean_t force_scoped
,
3245 unsigned int ifscope
)
3248 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3249 struct sockaddr_in6 mask6
, prefix
;
3250 struct nd_prefix
*opr
;
3252 int error
= 0, prproxy
= 0;
3253 struct rtentry
*rt
= NULL
;
3255 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3259 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3261 "%s: %s/%d on %s scoped=%d is already on-link\n",
3262 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3263 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3264 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3271 * Add the interface route associated with the prefix. Before
3272 * installing the route, check if there's the same prefix on another
3273 * interface, and the prefix has already installed the interface route.
3275 opr
= nd6_prefix_equal_lookup(pr
, FALSE
);
3279 if (!force_scoped
) {
3281 * If a primary/non-scoped interface route already exists,
3282 * install the new one as a scoped entry. If the existing
3283 * interface route is scoped, install new as non-scoped.
3285 ifscope
= (opr
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
3286 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3289 else if (ifscope
!= IFSCOPE_NONE
)
3290 ifscope
= IFSCOPE_NONE
;
3294 * We prefer link-local addresses as the associated interface address.
3296 /* search for a link-local addr */
3297 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
3298 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3300 struct in6_ifaddr
*ia6
;
3301 ifnet_lock_shared(ifp
);
3302 IFP_TO_IA6(ifp
, ia6
);
3303 ifnet_lock_done(ifp
);
3306 /* should we care about ia6_flags? */
3311 * This can still happen, when, for example, we receive an RA
3312 * containing a prefix with the L bit set and the A bit clear,
3313 * after removing all IPv6 addresses on the receiving
3314 * interface. This should, of course, be rare though.
3317 "nd6_prefix_onlink: failed to find any ifaddr"
3318 " to add route for a prefix(%s/%d) on %s\n",
3319 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3320 pr
->ndpr_plen
, if_name(ifp
)));
3326 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3327 * ifa->ifa_rtrequest = nd6_rtrequest;
3329 bzero(&mask6
, sizeof (mask6
));
3330 mask6
.sin6_len
= sizeof (mask6
);
3331 mask6
.sin6_addr
= pr
->ndpr_mask
;
3332 prefix
= pr
->ndpr_prefix
;
3333 if ((rt
= pr
->ndpr_rt
) != NULL
)
3335 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3339 rtflags
= ifa
->ifa_flags
| RTF_CLONING
| RTF_UP
;
3341 if (nd6_need_cache(ifp
)) {
3342 /* explicitly set in case ifa_flags does not set the flag. */
3343 rtflags
|= RTF_CLONING
;
3346 * explicitly clear the cloning bit in case ifa_flags sets it.
3348 rtflags
&= ~RTF_CLONING
;
3351 lck_mtx_unlock(nd6_mutex
);
3358 error
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&prefix
,
3359 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
,
3363 * Serialize the setting of NDPRF_PRPROXY.
3365 lck_mtx_lock(&proxy6_lock
);
3369 nd6_rtmsg(RTM_ADD
, rt
);
3374 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a"
3375 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx,"
3376 " scoped=%d, errno = %d\n",
3377 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3378 pr
->ndpr_plen
, if_name(ifp
),
3379 ip6_sprintf(&((struct sockaddr_in6
*)
3380 (void *)ifa
->ifa_addr
)->sin6_addr
),
3381 ip6_sprintf(&mask6
.sin6_addr
), rtflags
,
3382 (ifscope
!= IFSCOPE_NONE
), error
));
3384 NDPR_LOCK_ASSERT_HELD(pr
);
3386 pr
->ndpr_stateflags
&= ~(NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3389 * TODO: If the prefix route exists, we should really find it and
3390 * refer the prefix to it; otherwise ndpr_rt is NULL.
3392 if (!(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) &&
3393 (rt
!= NULL
|| error
== EEXIST
)) {
3394 struct nd_ifinfo
*ndi
= NULL
;
3396 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3397 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3399 ndi
= ND_IFINFO(ifp
);
3400 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3401 lck_mtx_lock(&ndi
->lock
);
3403 pr
->ndpr_rt
= rt
; /* keep reference from rtrequest */
3404 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
3405 if (ifscope
!= IFSCOPE_NONE
) {
3406 pr
->ndpr_stateflags
|= NDPRF_IFSCOPE
;
3407 } else if ((rtflags
& RTF_CLONING
) &&
3408 (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) &&
3409 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3411 * At present, in order for the prefix to be eligible
3412 * as a proxying/proxied prefix, we require that the
3413 * prefix route entry be marked as a cloning route with
3414 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3415 * true for the interface type, hence the test for
3416 * RTF_CLONING above.
3418 pr
->ndpr_stateflags
|= NDPRF_PRPROXY
;
3421 lck_mtx_unlock(&ndi
->lock
);
3422 } else if (rt
!= NULL
&& pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
3425 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3426 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3432 * If this is an upstream prefix, find the downstream ones (if any)
3433 * and re-configure their prefix routes accordingly. Otherwise,
3434 * this could be potentially be a downstream prefix, and so find the
3435 * upstream prefix, if any.
3437 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3439 NDPR_REMREF(pr
); /* release reference for this routine */
3440 lck_mtx_unlock(&proxy6_lock
);
3442 lck_mtx_lock(nd6_mutex
);
3448 nd6_prefix_onlink(struct nd_prefix
*pr
)
3450 return (nd6_prefix_onlink_common(pr
, FALSE
, IFSCOPE_NONE
));
3454 nd6_prefix_onlink_scoped(struct nd_prefix
*pr
, unsigned int ifscope
)
3456 return (nd6_prefix_onlink_common(pr
, TRUE
, ifscope
));
3460 nd6_prefix_offlink(struct nd_prefix
*pr
)
3462 int plen
, error
= 0, prproxy
;
3463 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3464 struct sockaddr_in6 sa6
, mask6
, prefix
;
3465 struct rtentry
*rt
= NULL
, *ndpr_rt
= NULL
;
3466 unsigned int ifscope
;
3468 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3472 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3474 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3475 "off-link\n", ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3476 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3477 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0));
3482 bzero(&sa6
, sizeof (sa6
));
3483 sa6
.sin6_family
= AF_INET6
;
3484 sa6
.sin6_len
= sizeof (sa6
);
3485 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
,
3486 sizeof (struct in6_addr
));
3487 bzero(&mask6
, sizeof (mask6
));
3488 mask6
.sin6_family
= AF_INET6
;
3489 mask6
.sin6_len
= sizeof (sa6
);
3490 bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof (struct in6_addr
));
3491 prefix
= pr
->ndpr_prefix
;
3492 plen
= pr
->ndpr_plen
;
3493 if ((ndpr_rt
= pr
->ndpr_rt
) != NULL
)
3495 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3498 ifscope
= (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ?
3499 ifp
->if_index
: IFSCOPE_NONE
;
3501 error
= rtrequest_scoped(RTM_DELETE
, (struct sockaddr
*)&sa6
,
3502 NULL
, (struct sockaddr
*)&mask6
, 0, &rt
, ifscope
);
3505 /* report the route deletion to the routing socket. */
3507 nd6_rtmsg(RTM_DELETE
, rt
);
3513 "nd6_prefix_offlink: failed to delete route: "
3514 "%s/%d on %s, scoped %d, (errno = %d)\n",
3515 ip6_sprintf(&sa6
.sin6_addr
), plen
, if_name(ifp
),
3516 (ifscope
!= IFSCOPE_NONE
), error
));
3519 if (ndpr_rt
!= NULL
)
3522 lck_mtx_lock(&proxy6_lock
);
3525 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3526 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3527 pr
->ndpr_stateflags
&= ~(NDPRF_ONLINK
| NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3528 if (pr
->ndpr_prproxy_sols_cnt
> 0) {
3530 nd6_prproxy_sols_reap(pr
);
3531 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3532 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3537 * If this was an upstream prefix, find the downstream ones and do
3538 * some cleanups. If this was a downstream prefix, the prefix route
3539 * has been removed from the routing table above, but there may be
3540 * other tasks to perform.
3542 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3544 NDPR_REMREF(pr
); /* release reference for this routine */
3545 lck_mtx_unlock(&proxy6_lock
);
3550 static struct in6_ifaddr
*
3551 in6_pfx_newpersistaddr(struct nd_prefix
*pr
, int mcast
, int *errorp
)
3553 struct in6_ifaddr
*ia6
= NULL
;
3554 struct ifnet
*ifp
= NULL
;
3555 struct nd_ifinfo
*ndi
= NULL
;
3556 struct in6_addr mask
;
3557 struct in6_aliasreq ifra
;
3558 int error
, ifaupdate
, iidlen
, notcga
;
3561 VERIFY(errorp
!= NULL
);
3569 * Prefix Length check:
3570 * If the sum of the prefix length and interface identifier
3571 * length does not equal 128 bits, the Prefix Information
3572 * option MUST be ignored. The length of the interface
3573 * identifier is defined in a separate link-type specific
3576 iidlen
= in6_if2idlen(ifp
);
3578 error
= EADDRNOTAVAIL
;
3579 /* this should not happen, so we always log it. */
3580 log(LOG_ERR
, "%s: IID length undefined (%s)\n",
3581 __func__
, if_name(ifp
));
3583 } else if (iidlen
!= 64) {
3584 error
= EADDRNOTAVAIL
;
3586 * stateless autoconfiguration not yet well-defined for IID
3587 * lengths other than 64 octets. Just give up for now.
3589 nd6log((LOG_INFO
, "%s: IID length not 64 octets (%s)\n",
3590 __func__
, if_name(ifp
)));
3594 if (iidlen
+ pr
->ndpr_plen
!= 128) {
3595 error
= EADDRNOTAVAIL
;
3597 "%s: invalid prefix length %d for %s, ignored\n",
3598 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
3602 bzero(&ifra
, sizeof (ifra
));
3603 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3604 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
3605 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
3608 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
,
3609 sizeof (ifra
.ifra_addr
.sin6_addr
));
3610 in6_len2mask(&mask
, pr
->ndpr_plen
);
3611 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
3612 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
3613 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
3614 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
3616 ndi
= ND_IFINFO(ifp
);
3617 VERIFY(ndi
->initialized
);
3618 lck_mtx_lock(&ndi
->lock
);
3620 notcga
= nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
||
3621 (ndi
->flags
& ND6_IFF_INSECURE
) != 0;
3623 lck_mtx_unlock(&ndi
->lock
);
3627 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
3629 error
= EADDRNOTAVAIL
;
3630 nd6log((LOG_INFO
, "%s: no link-local address (%s)\n",
3631 __func__
, if_name(ifp
)));
3635 IFA_LOCK(&ia6
->ia_ifa
);
3636 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
3637 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
3638 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
3639 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
3640 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3641 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
3642 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3643 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
3644 IFA_UNLOCK(&ia6
->ia_ifa
);
3645 IFA_REMREF(&ia6
->ia_ifa
);
3648 in6_cga_node_lock();
3649 struct in6_cga_prepare local_cga_prepare
;
3651 if (ndi
->cga_initialized
) {
3652 bcopy(&(ndi
->local_cga_modifier
),
3653 &(local_cga_prepare
.cga_modifier
),
3654 sizeof(local_cga_prepare
.cga_modifier
));
3655 error
= in6_cga_generate(&local_cga_prepare
, 0,
3656 &ifra
.ifra_addr
.sin6_addr
);
3658 error
= in6_cga_generate(NULL
, 0,
3659 &ifra
.ifra_addr
.sin6_addr
);
3661 in6_cga_node_unlock();
3663 ifra
.ifra_flags
|= IN6_IFF_SECURED
;
3665 nd6log((LOG_ERR
, "%s: no CGA available (%s)\n",
3666 __func__
, if_name(ifp
)));
3671 VERIFY(ia6
== NULL
);
3673 /* new prefix mask. */
3674 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
3675 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
3676 bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
,
3677 sizeof (ifra
.ifra_prefixmask
.sin6_addr
));
3680 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
3681 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
3684 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
3687 * Make sure that we do not have this address already. This should
3688 * usually not happen, but we can still see this case, e.g., if we
3689 * have manually configured the exact address to be configured.
3691 if ((ia6
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
))
3693 error
= EADDRNOTAVAIL
;
3694 IFA_REMREF(&ia6
->ia_ifa
);
3697 /* this should be rare enough to make an explicit log */
3698 log(LOG_INFO
, "%s: %s is already configured!\n",
3699 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
3704 * Allocate ifaddr structure, link into chain, etc.
3705 * If we are going to create a new address upon receiving a multicasted
3706 * RA, we need to impose a random delay before starting DAD.
3707 * [RFC 4862, Section 5.4.2]
3709 ifaupdate
= IN6_IFAUPDATE_NOWAIT
;
3711 ifaupdate
|= IN6_IFAUPDATE_DADDELAY
;
3712 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &ia6
);
3715 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
3716 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
),
3717 if_name(ifp
), error
));
3718 error
= EADDRNOTAVAIL
;
3722 VERIFY(ia6
!= NULL
);
3723 in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia6
, NULL
);
3734 #define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
3737 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
)
3739 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
3740 struct in6_ifaddr
*ia
, *newia
;
3741 struct in6_aliasreq ifra
;
3742 int i
, error
, ifaupdate
;
3743 int trylimit
= 3; /* XXX: adhoc value */
3744 u_int32_t randid
[2];
3745 time_t vltime0
, pltime0
;
3746 uint64_t timenow
= net_uptime();
3747 struct in6_addr addr
;
3748 struct nd_prefix
*ndpr
;
3750 bzero(&ifra
, sizeof (ifra
));
3751 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3752 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3753 ifra
.ifra_addr
= ia0
->ia_addr
;
3754 /* copy prefix mask */
3755 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
3756 /* clear the old IFID */
3757 for (i
= 0; i
< 4; i
++) {
3758 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
]
3759 &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
3761 addr
= ia0
->ia_addr
.sin6_addr
;
3762 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3765 in6_iid_mktmp(ifp
, (u_int8_t
*)randid
,
3766 (const u_int8_t
*)&addr
.s6_addr
[8], forcegen
);
3768 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3769 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
3770 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3771 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
3774 * in6_iid_mktmp() quite likely provided a unique interface ID.
3775 * However, we may still have a chance to see collision, because
3776 * there may be a time lag between generation of the ID and generation
3777 * of the address. So, we'll do one more sanity check.
3779 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
)) != NULL
) {
3780 IFA_REMREF(&ia
->ia_ifa
);
3781 if (trylimit
-- == 0) {
3782 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find "
3783 "a unique random IFID\n"));
3791 * The Valid Lifetime is the lower of the Valid Lifetime of the
3792 * public address or TEMP_VALID_LIFETIME.
3793 * The Preferred Lifetime is the lower of the Preferred Lifetime
3794 * of the public address or TEMP_PREFERRED_LIFETIME -
3797 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3798 if (ia0
->ia6_lifetime
.ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3799 vltime0
= IFA6_IS_INVALID(ia0
, timenow
) ? 0 :
3800 (ia0
->ia6_lifetime
.ia6ti_vltime
-
3801 (timenow
- ia0
->ia6_updatetime
));
3802 if (vltime0
> ip6_temp_valid_lifetime
)
3803 vltime0
= ip6_temp_valid_lifetime
;
3805 vltime0
= ip6_temp_valid_lifetime
;
3807 if (ia0
->ia6_lifetime
.ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3808 pltime0
= IFA6_IS_DEPRECATED(ia0
, timenow
) ? 0 :
3809 (ia0
->ia6_lifetime
.ia6ti_pltime
-
3810 (timenow
- ia0
->ia6_updatetime
));
3811 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
)
3812 pltime0
= ip6_temp_preferred_lifetime
-
3815 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
3817 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
3818 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
3819 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3821 * A temporary address is created only if this calculated Preferred
3822 * Lifetime is greater than REGEN_ADVANCE time units.
3824 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
)
3827 /* XXX: scope zone ID? */
3829 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
3831 /* allocate ifaddr structure, link into chain, etc. */
3832 ifaupdate
= IN6_IFAUPDATE_NOWAIT
| IN6_IFAUPDATE_DADDELAY
;
3833 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &newia
);
3835 nd6log((LOG_ERR
, "in6_tmpifadd: failed to add address.\n"));
3838 VERIFY(newia
!= NULL
);
3840 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3841 ndpr
= ia0
->ia6_ndpr
;
3844 * We lost the race with another thread that has purged
3845 * ia0 address; in this case, purge the tmp addr as well.
3847 nd6log((LOG_ERR
, "in6_tmpifadd: no public address\n"));
3848 VERIFY(!(ia0
->ia6_flags
& IN6_IFF_AUTOCONF
));
3849 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3850 in6_purgeaddr(&newia
->ia_ifa
);
3851 IFA_REMREF(&newia
->ia_ifa
);
3852 return (EADDRNOTAVAIL
);
3854 NDPR_ADDREF(ndpr
); /* for us */
3855 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3856 IFA_LOCK(&newia
->ia_ifa
);
3857 if (newia
->ia6_ndpr
!= NULL
) {
3858 NDPR_LOCK(newia
->ia6_ndpr
);
3859 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3860 newia
->ia6_ndpr
->ndpr_addrcnt
--;
3861 NDPR_UNLOCK(newia
->ia6_ndpr
);
3862 NDPR_REMREF(newia
->ia6_ndpr
); /* release addr reference */
3864 newia
->ia6_ndpr
= ndpr
;
3865 NDPR_LOCK(newia
->ia6_ndpr
);
3866 newia
->ia6_ndpr
->ndpr_addrcnt
++;
3867 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3868 NDPR_ADDREF_LOCKED(newia
->ia6_ndpr
); /* for addr reference */
3869 NDPR_UNLOCK(newia
->ia6_ndpr
);
3870 IFA_UNLOCK(&newia
->ia_ifa
);
3872 * A newly added address might affect the status of other addresses.
3873 * XXX: when the temporary address is generated with a new public
3874 * address, the onlink check is redundant. However, it would be safe
3875 * to do the check explicitly everywhere a new address is generated,
3876 * and, in fact, we surely need the check when we create a new
3877 * temporary address due to deprecation of an old temporary address.
3879 lck_mtx_lock(nd6_mutex
);
3880 pfxlist_onlink_check();
3881 lck_mtx_unlock(nd6_mutex
);
3882 IFA_REMREF(&newia
->ia_ifa
);
3884 /* remove our reference */
3892 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
3894 struct timeval caltime
;
3895 u_int64_t timenow
= net_uptime();
3897 NDPR_LOCK_ASSERT_HELD(ndpr
);
3899 getmicrotime(&caltime
);
3900 ndpr
->ndpr_base_calendartime
= caltime
.tv_sec
;
3901 ndpr
->ndpr_base_uptime
= timenow
;
3903 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
3904 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
3905 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime"
3906 "(%d) is greater than valid lifetime(%d)\n",
3907 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
));
3910 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
3911 ndpr
->ndpr_preferred
= 0;
3913 ndpr
->ndpr_preferred
= timenow
+ ndpr
->ndpr_pltime
;
3914 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
3915 ndpr
->ndpr_expire
= 0;
3917 ndpr
->ndpr_expire
= timenow
+ ndpr
->ndpr_vltime
;
3923 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
3926 uint64_t timenow
= net_uptime();
3928 /* Valid lifetime must not be updated unless explicitly specified. */
3929 /* init ia6t_expire */
3930 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
3931 lt6
->ia6t_expire
= 0;
3933 lt6
->ia6t_expire
= timenow
;
3934 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
3937 /* init ia6t_preferred */
3938 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
) {
3939 lt6
->ia6t_preferred
= 0;
3941 lt6
->ia6t_preferred
= timenow
;
3942 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
3947 * Delete all the routing table entries that use the specified gateway.
3948 * XXX: this function causes search through all entries of routing table, so
3949 * it shouldn't be called when acting as a router.
3953 struct in6_addr
*gateway
,
3956 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
3958 /* We'll care only link-local addresses */
3959 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
3962 lck_mtx_lock(rnh_lock
);
3963 /* XXX: hack for KAME's link-local address kludge */
3964 gateway
->s6_addr16
[1] = htons(ifp
->if_index
);
3966 rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
);
3967 lck_mtx_unlock(rnh_lock
);
3972 struct radix_node
*rn
,
3975 struct rtentry
*rt
= (struct rtentry
*)rn
;
3976 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
3978 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
3981 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
) {
3986 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) {
3991 * Do not delete a static route.
3992 * XXX: this seems to be a bit ad-hoc. Should we consider the
3993 * 'cloned' bit instead?
3995 if ((rt
->rt_flags
& RTF_STATIC
) != 0) {
4000 * We delete only host route. This means, in particular, we don't
4001 * delete default route.
4003 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
4009 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
4010 * rnh_lock here prevents another thread from calling rt_setgate()
4014 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4015 rt_mask(rt
), rt
->rt_flags
, 0));
4019 nd6_setdefaultiface(
4023 ifnet_t def_ifp
= NULL
;
4025 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4027 ifnet_head_lock_shared();
4028 if (ifindex
< 0 || if_index
< ifindex
) {
4032 def_ifp
= ifindex2ifnet
[ifindex
];
4035 lck_mtx_lock(nd6_mutex
);
4036 if (nd6_defifindex
!= ifindex
) {
4037 struct ifnet
*odef_ifp
= nd6_defifp
;
4039 nd6_defifindex
= ifindex
;
4040 if (nd6_defifindex
> 0)
4041 nd6_defifp
= def_ifp
;
4045 if (nd6_defifp
!= NULL
)
4046 nd6log((LOG_INFO
, "%s: is now the default "
4047 "interface (was %s)\n", if_name(nd6_defifp
),
4048 odef_ifp
!= NULL
? if_name(odef_ifp
) : "NONE"));
4050 nd6log((LOG_INFO
, "No default interface set\n"));
4053 * If the Default Router List is empty, install a route
4054 * to the specified interface as default or remove the default
4055 * route when the default interface becomes canceled.
4056 * The check for the queue is actually redundant, but
4057 * we do this here to avoid re-install the default route
4058 * if the list is NOT empty.
4060 if (odef_ifp
!= NULL
) {
4061 defrouter_select(odef_ifp
);
4064 if (nd6_defifp
!= NULL
) {
4065 defrouter_select(nd6_defifp
);
4066 nd6_prefix_sync(nd6_defifp
);
4070 * Our current implementation assumes one-to-one mapping between
4071 * interfaces and links, so it would be natural to use the
4072 * default interface as the default link.
4074 scope6_setdefault(nd6_defifp
);
4076 lck_mtx_unlock(nd6_mutex
);