2 * Copyright (c) 2003-2014 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_types.h>
81 #include <net/if_dl.h>
82 #include <net/route.h>
83 #include <net/radix.h>
85 #include <netinet/in.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/nd6.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/scope6_var.h>
94 #include <net/net_osdep.h>
96 static void defrouter_addreq(struct nd_defrouter
*, boolean_t
);
97 static void defrouter_delreq(struct nd_defrouter
*);
98 static struct nd_defrouter
*defrtrlist_update_common(struct nd_defrouter
*,
100 static struct nd_defrouter
*defrtrlist_update(struct nd_defrouter
*);
102 static struct in6_ifaddr
*in6_pfx_newpersistaddr(struct nd_prefix
*, int,
104 static void defrtrlist_sync(struct ifnet
*);
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
;
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 lck_rw_lock_shared(nd_if_rwlock
);
443 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
444 lck_rw_done(nd_if_rwlock
);
447 ndi
= &nd_ifinfo
[ifp
->if_index
];
448 VERIFY(ndi
->initialized
);
449 lck_mtx_lock(&ndi
->lock
);
450 bzero(&dr0
, sizeof (dr0
));
452 dr0
.flags
= nd_ra
->nd_ra_flags_reserved
;
453 dr0
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
454 dr0
.expire
= net_uptime() + dr0
.rtlifetime
;
456 /* unspecified or not? (RFC 2461 6.3.4) */
458 advreachable
= ntohl(advreachable
);
459 if (advreachable
<= MAX_REACHABLE_TIME
&&
460 ndi
->basereachable
!= advreachable
) {
461 ndi
->basereachable
= advreachable
;
462 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
463 ndi
->recalctm
= nd6_recalc_reachtm_interval
; /* reset */
466 if (nd_ra
->nd_ra_retransmit
)
467 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
468 if (nd_ra
->nd_ra_curhoplimit
)
469 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
470 lck_mtx_unlock(&ndi
->lock
);
471 lck_rw_done(nd_if_rwlock
);
473 lck_mtx_lock(nd6_mutex
);
474 dr
= defrtrlist_update(&dr0
);
475 lck_mtx_unlock(nd6_mutex
);
480 if (ndopts
.nd_opts_pi
) {
481 struct nd_opt_hdr
*pt
;
482 struct nd_opt_prefix_info
*pi
= NULL
;
485 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
486 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
487 pt
= (struct nd_opt_hdr
*)((caddr_t
)pt
+
488 (pt
->nd_opt_len
<< 3))) {
489 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
)
491 pi
= (struct nd_opt_prefix_info
*)pt
;
493 if (pi
->nd_opt_pi_len
!= 4) {
495 "nd6_ra_input: invalid option "
496 "len %d for prefix information option, "
497 "ignored\n", pi
->nd_opt_pi_len
));
501 if (128 < pi
->nd_opt_pi_prefix_len
) {
503 "nd6_ra_input: invalid prefix "
504 "len %d for prefix information option, "
505 "ignored\n", pi
->nd_opt_pi_prefix_len
));
509 if (IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
) ||
510 IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
512 "%s: invalid prefix %s, ignored\n",
514 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
518 bzero(&pr
, sizeof (pr
));
519 lck_mtx_init(&pr
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
521 pr
.ndpr_prefix
.sin6_family
= AF_INET6
;
522 pr
.ndpr_prefix
.sin6_len
= sizeof (pr
.ndpr_prefix
);
523 pr
.ndpr_prefix
.sin6_addr
= pi
->nd_opt_pi_prefix
;
524 pr
.ndpr_ifp
= m
->m_pkthdr
.rcvif
;
526 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
527 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
528 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
529 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
530 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
531 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
533 ntohl(pi
->nd_opt_pi_preferred_time
);
536 * Exceptions to stateless autoconfiguration processing:
537 * + nd6_accept_6to4 == 0 && address has 6to4 prefix
538 * + ip6_only_allow_rfc4193_prefix != 0 &&
539 * address not RFC 4193
541 if (ip6_only_allow_rfc4193_prefix
&&
542 !IN6_IS_ADDR_UNIQUE_LOCAL(&pi
->nd_opt_pi_prefix
)) {
544 "nd6_ra_input: no SLAAC on prefix %s "
546 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
547 pr
.ndpr_raf_auto
= 0;
548 } else if (!nd6_accept_6to4
&&
549 IN6_IS_ADDR_6TO4(&pi
->nd_opt_pi_prefix
)) {
551 "%s: no SLAAC on prefix %s "
552 "[6to4]\n", __func__
,
553 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
554 pr
.ndpr_raf_auto
= 0;
557 if (in6_init_prefix_ltimes(&pr
)) {
559 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
560 continue; /* prefix lifetime init failed */
564 (void) prelist_update(&pr
, dr
, m
, mcast
);
565 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
568 * We have to copy the values out after the
569 * prelist_update call since some of these values won't
570 * be properly set until after the router advertisement
571 * updating can vet the values.
574 MALLOC(prfl
, struct nd_prefix_list
*, sizeof (*prfl
),
575 M_TEMP
, M_WAITOK
| M_ZERO
);
578 log(LOG_DEBUG
, "%s: unable to MALLOC RA prefix "
579 "structure\n", __func__
);
583 /* this is only for nd6_post_msg(), otherwise unused */
584 bcopy(&pr
.ndpr_prefix
, &prfl
->pr
.ndpr_prefix
,
585 sizeof (prfl
->pr
.ndpr_prefix
));
586 prfl
->pr
.ndpr_raf
= pr
.ndpr_raf
;
587 prfl
->pr
.ndpr_plen
= pr
.ndpr_plen
;
588 prfl
->pr
.ndpr_vltime
= pr
.ndpr_vltime
;
589 prfl
->pr
.ndpr_pltime
= pr
.ndpr_pltime
;
590 prfl
->pr
.ndpr_expire
= pr
.ndpr_expire
;
591 prfl
->pr
.ndpr_base_calendartime
=
592 pr
.ndpr_base_calendartime
;
593 prfl
->pr
.ndpr_base_uptime
= pr
.ndpr_base_uptime
;
594 prfl
->pr
.ndpr_stateflags
= pr
.ndpr_stateflags
;
595 prfl
->pr
.ndpr_addrcnt
= pr
.ndpr_addrcnt
;
596 prfl
->pr
.ndpr_ifp
= pr
.ndpr_ifp
;
598 prfl
->next
= nd_prefix_list_head
;
599 nd_prefix_list_head
= prfl
;
600 nd_prefix_list_length
++;
607 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
608 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
611 if (mtu
< IPV6_MMTU
) {
612 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu option "
613 "mtu=%d sent from %s, ignoring\n",
614 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
618 lck_rw_lock_shared(nd_if_rwlock
);
619 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
620 lck_rw_done(nd_if_rwlock
);
623 ndi
= &nd_ifinfo
[ifp
->if_index
];
624 VERIFY(ndi
->initialized
);
625 lck_mtx_lock(&ndi
->lock
);
628 if (mtu
<= ndi
->maxmtu
) {
629 int change
= (ndi
->linkmtu
!= mtu
);
632 lck_mtx_unlock(&ndi
->lock
);
633 lck_rw_done(nd_if_rwlock
);
634 if (change
) /* in6_maxmtu may change */
637 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu "
638 "mtu=%d sent from %s; "
639 "exceeds maxmtu %d, ignoring\n",
640 mtu
, ip6_sprintf(&ip6
->ip6_src
),
642 lck_mtx_unlock(&ndi
->lock
);
643 lck_rw_done(nd_if_rwlock
);
646 lck_mtx_unlock(&ndi
->lock
);
647 lck_rw_done(nd_if_rwlock
);
648 nd6log((LOG_INFO
, "nd6_ra_input: mtu option "
649 "mtu=%d sent from %s; maxmtu unknown, "
651 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
659 * Source link layer address
661 if (ndopts
.nd_opts_src_lladdr
) {
662 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
663 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
666 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
668 "nd6_ra_input: lladdrlen mismatch for %s "
669 "(if %d, RA packet %d)\n",
670 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
674 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, (int)lladdrlen
,
675 ND_ROUTER_ADVERT
, 0);
678 nd6_post_msg(KEV_ND6_RA
, nd_prefix_list_head
, nd_prefix_list_length
,
679 mtu
, lladdr
, lladdrlen
);
682 * Installing a link-layer address might change the state of the
683 * router's neighbor cache, which might also affect our on-link
684 * detection of adveritsed prefixes.
686 lck_mtx_lock(nd6_mutex
);
687 pfxlist_onlink_check();
688 lck_mtx_unlock(nd6_mutex
);
696 while ((prfl
= nd_prefix_list_head
) != NULL
) {
697 nd_prefix_list_head
= prfl
->next
;
704 icmp6stat
.icp6s_badra
++;
709 * default router list proccessing sub routines
712 /* tell the change to user processes watching the routing socket. */
718 struct rt_addrinfo info
;
719 struct ifnet
*ifp
= rt
->rt_ifp
;
721 RT_LOCK_ASSERT_HELD(rt
);
723 bzero((caddr_t
)&info
, sizeof (info
));
724 /* It's not necessary to lock ifp for if_lladdr */
725 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
726 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
727 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
729 * ifa_addr pointers for both should always be valid
730 * in this context; no need to hold locks.
732 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
733 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
735 rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0);
739 defrouter_addreq(struct nd_defrouter
*new, boolean_t scoped
)
741 struct sockaddr_in6 def
, mask
, gate
;
742 struct rtentry
*newrt
= NULL
;
743 unsigned int ifscope
;
746 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
747 NDDR_LOCK_ASSERT_NOTHELD(new);
749 * We're free to lock and unlock NDDR because our callers
750 * are holding an extra reference for us.
754 if (new->stateflags
& NDDRF_INSTALLED
)
757 if (new->ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
758 nd6log2((LOG_INFO
, "%s: ignoring router %s, scoped=%d, "
759 "static=%d on advertising interface\n", if_name(new->ifp
),
760 ip6_sprintf(&new->rtaddr
), scoped
,
761 (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
765 nd6log2((LOG_INFO
, "%s: adding default router %s, scoped=%d, "
766 "static=%d\n", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
),
767 scoped
, (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
769 Bzero(&def
, sizeof (def
));
770 Bzero(&mask
, sizeof (mask
));
771 Bzero(&gate
, sizeof (gate
));
773 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
774 = sizeof (struct sockaddr_in6
);
775 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
776 gate
.sin6_addr
= new->rtaddr
;
778 ifscope
= scoped
? new->ifp
->if_index
: IFSCOPE_NONE
;
781 err
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&def
,
782 (struct sockaddr
*)&gate
, (struct sockaddr
*)&mask
,
783 RTF_GATEWAY
, &newrt
, ifscope
);
787 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */
788 RT_REMREF_LOCKED(newrt
);
791 new->stateflags
|= NDDRF_INSTALLED
;
792 if (ifscope
!= IFSCOPE_NONE
)
793 new->stateflags
|= NDDRF_IFSCOPE
;
794 new->genid
= nd6_defrouter_genid
;
796 nd6log((LOG_ERR
, "%s: failed to add default router "
797 "%s on %s scoped %d (errno = %d)\n", __func__
,
798 ip6_sprintf(&gate
.sin6_addr
), if_name(new->ifp
),
799 (ifscope
!= IFSCOPE_NONE
), err
));
808 struct nd_defrouter
*
810 struct in6_addr
*addr
,
813 struct nd_defrouter
*dr
;
815 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
817 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
818 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
820 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) {
821 NDDR_ADDREF_LOCKED(dr
);
828 return (NULL
); /* search failed */
832 * Remove the default route for a given router.
833 * This is just a subroutine function for defrouter_select(), and should
834 * not be called from anywhere else.
837 defrouter_delreq(struct nd_defrouter
*dr
)
839 struct sockaddr_in6 def
, mask
, gate
;
840 struct rtentry
*oldrt
= NULL
;
841 unsigned int ifscope
;
844 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
845 NDDR_LOCK_ASSERT_NOTHELD(dr
);
847 * We're free to lock and unlock NDDR because our callers
848 * are holding an extra reference for us.
851 /* ifp would be NULL for the "drany" case */
852 if (dr
->ifp
!= NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
))
855 nd6log2((LOG_INFO
, "%s: removing default router %s, scoped=%d, "
856 "static=%d\n", dr
->ifp
!= NULL
? if_name(dr
->ifp
) : "ANY",
857 ip6_sprintf(&dr
->rtaddr
), (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
858 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
860 Bzero(&def
, sizeof (def
));
861 Bzero(&mask
, sizeof (mask
));
862 Bzero(&gate
, sizeof (gate
));
864 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
865 = sizeof (struct sockaddr_in6
);
866 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
867 gate
.sin6_addr
= dr
->rtaddr
;
869 if (dr
->ifp
!= NULL
) {
870 ifscope
= (dr
->stateflags
& NDDRF_IFSCOPE
) ?
871 dr
->ifp
->if_index
: IFSCOPE_NONE
;
873 ifscope
= IFSCOPE_NONE
;
876 err
= rtrequest_scoped(RTM_DELETE
,
877 (struct sockaddr
*)&def
, (struct sockaddr
*)&gate
,
878 (struct sockaddr
*)&mask
, RTF_GATEWAY
, &oldrt
, ifscope
);
882 nd6_rtmsg(RTM_DELETE
, oldrt
);
885 } else if (err
!= ESRCH
) {
886 nd6log((LOG_ERR
, "%s: failed to delete default router "
887 "%s on %s scoped %d (errno = %d)\n", __func__
,
888 ip6_sprintf(&gate
.sin6_addr
), dr
->ifp
!= NULL
?
889 if_name(dr
->ifp
) : "ANY", (ifscope
!= IFSCOPE_NONE
), err
));
892 /* ESRCH means it's no longer in the routing table; ignore it */
893 if (oldrt
!= NULL
|| err
== ESRCH
) {
894 dr
->stateflags
&= ~NDDRF_INSTALLED
;
895 if (ifscope
!= IFSCOPE_NONE
)
896 dr
->stateflags
&= ~NDDRF_IFSCOPE
;
905 * remove all default routes from default router list
908 defrouter_reset(void)
910 struct nd_defrouter
*dr
, drany
;
912 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
914 dr
= TAILQ_FIRST(&nd_defrouter
);
917 if (dr
->stateflags
& NDDRF_INSTALLED
) {
918 NDDR_ADDREF_LOCKED(dr
);
920 lck_mtx_unlock(nd6_mutex
);
921 defrouter_delreq(dr
);
922 lck_mtx_lock(nd6_mutex
);
924 dr
= TAILQ_FIRST(&nd_defrouter
);
927 dr
= TAILQ_NEXT(dr
, dr_entry
);
931 /* Nuke primary (non-scoped) default router */
932 if (ip6_doscopedroute
) {
933 bzero(&drany
, sizeof (drany
));
934 lck_mtx_init(&drany
.nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
935 lck_mtx_unlock(nd6_mutex
);
936 defrouter_delreq(&drany
);
937 lck_mtx_destroy(&drany
.nddr_lock
, ifa_mtx_grp
);
938 lck_mtx_lock(nd6_mutex
);
944 defrtrlist_ioctl(u_long cmd
, caddr_t data
)
946 struct nd_defrouter dr0
;
947 unsigned int ifindex
;
948 struct ifnet
*dr_ifp
;
949 int error
= 0, add
= 0;
952 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
953 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
956 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
957 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
958 bzero(&dr0
, sizeof (dr0
));
959 if (cmd
== SIOCDRADD_IN6_64
|| cmd
== SIOCDRDEL_IN6_64
) {
960 struct in6_defrouter_64
*r_64
=
961 (struct in6_defrouter_64
*)(void *)data
;
964 bcopy(&r_64
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
965 sizeof (dr0
.rtaddr
));
966 dr0
.flags
= r_64
->flags
;
967 bcopy(&r_64
->if_index
, &i
, sizeof (i
));
970 struct in6_defrouter_32
*r_32
=
971 (struct in6_defrouter_32
*)(void *)data
;
974 bcopy(&r_32
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
975 sizeof (dr0
.rtaddr
));
976 dr0
.flags
= r_32
->flags
;
977 bcopy(&r_32
->if_index
, &i
, sizeof (i
));
980 ifnet_head_lock_shared();
981 /* Don't need to check is ifindex is < 0 since it's unsigned */
982 if (if_index
< ifindex
||
983 (dr_ifp
= ifindex2ifnet
[ifindex
]) == NULL
) {
991 if (IN6_IS_SCOPE_EMBED(&dr0
.rtaddr
)) {
992 uint16_t *scope
= &dr0
.rtaddr
.s6_addr16
[1];
995 *scope
= htons(dr_ifp
->if_index
);
996 } else if (*scope
!= htons(dr_ifp
->if_index
)) {
1003 error
= defrtrlist_add_static(&dr0
);
1004 if (!add
|| error
!= 0) {
1005 int err
= defrtrlist_del_static(&dr0
);
1012 error
= EOPNOTSUPP
; /* check for safety */
1020 defrtrlist_del(struct nd_defrouter
*dr
)
1022 struct nd_defrouter
*deldr
= NULL
;
1023 struct nd_prefix
*pr
;
1024 struct ifnet
*ifp
= dr
->ifp
;
1027 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1029 if (!ip6_doscopedroute
&& dr
== TAILQ_FIRST(&nd_defrouter
))
1030 deldr
= dr
; /* The router is primary. */
1032 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1033 ++nd6_defrouter_genid
;
1035 * Flush all the routing table entries that use the router
1038 if (ip6_doscopedroute
|| !ip6_forwarding
) {
1039 /* above is a good condition? */
1041 lck_mtx_unlock(nd6_mutex
);
1042 rt6_flush(&dr
->rtaddr
, ifp
);
1043 lck_mtx_lock(nd6_mutex
);
1046 nd6log2((LOG_INFO
, "%s: freeing defrouter %s\n", if_name(dr
->ifp
),
1047 ip6_sprintf(&dr
->rtaddr
)));
1049 * Delete it from the routing table.
1052 lck_mtx_unlock(nd6_mutex
);
1053 defrouter_delreq(dr
);
1054 lck_mtx_lock(nd6_mutex
);
1058 * Also delete all the pointers to the router in each prefix lists.
1060 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1061 struct nd_pfxrouter
*pfxrtr
;
1064 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
1065 pfxrtr_del(pfxrtr
, pr
);
1069 pfxlist_onlink_check();
1072 * If the router is the primary one, choose a new one. If Scoped
1073 * Routing is enabled, always try to pick another eligible router
1074 * on this interface.
1076 if (deldr
|| ip6_doscopedroute
)
1077 defrouter_select(ifp
);
1080 lck_rw_lock_shared(nd_if_rwlock
);
1081 if (ifp
->if_index
< nd_ifinfo_indexlim
) {
1082 struct nd_ifinfo
*ndi
= &nd_ifinfo
[ifp
->if_index
];
1083 VERIFY(ndi
->initialized
);
1084 lck_mtx_lock(&ndi
->lock
);
1085 VERIFY(ndi
->ndefrouters
>= 0);
1086 if (ndi
->ndefrouters
> 0 && --ndi
->ndefrouters
== 0) {
1090 lck_mtx_unlock(&ndi
->lock
);
1092 lck_rw_done(nd_if_rwlock
);
1097 NDDR_REMREF(dr
); /* remove list reference */
1101 defrtrlist_add_static(struct nd_defrouter
*new)
1103 struct nd_defrouter
*dr
;
1106 new->rtlifetime
= -1;
1107 new->stateflags
|= NDDRF_STATIC
;
1109 /* we only want the preference level */
1110 new->flags
&= ND_RA_FLAG_RTPREF_MASK
;
1112 lck_mtx_lock(nd6_mutex
);
1113 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1114 if (dr
!= NULL
&& !(dr
->stateflags
& NDDRF_STATIC
)) {
1119 dr
= defrtrlist_update(new);
1127 lck_mtx_unlock(nd6_mutex
);
1133 defrtrlist_del_static(struct nd_defrouter
*new)
1135 struct nd_defrouter
*dr
;
1137 lck_mtx_lock(nd6_mutex
);
1138 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1139 if (dr
== NULL
|| !(dr
->stateflags
& NDDRF_STATIC
)) {
1147 lck_mtx_unlock(nd6_mutex
);
1149 return (dr
!= NULL
? 0 : EINVAL
);
1153 * for default router selection
1154 * regards router-preference field as a 2-bit signed integer
1157 rtpref(struct nd_defrouter
*dr
)
1159 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
1160 case ND_RA_FLAG_RTPREF_HIGH
:
1161 return (RTPREF_HIGH
);
1162 case ND_RA_FLAG_RTPREF_MEDIUM
:
1163 case ND_RA_FLAG_RTPREF_RSV
:
1164 return (RTPREF_MEDIUM
);
1165 case ND_RA_FLAG_RTPREF_LOW
:
1166 return (RTPREF_LOW
);
1169 * This case should never happen. If it did, it would mean a
1170 * serious bug of kernel internal. We thus always bark here.
1171 * Or, can we even panic?
1173 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
1174 return (RTPREF_INVALID
);
1180 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
1182 * 1) Routers that are reachable or probably reachable should be preferred.
1183 * If we have more than one (probably) reachable router, prefer ones
1184 * with the highest router preference.
1185 * 2) When no routers on the list are known to be reachable or
1186 * probably reachable, routers SHOULD be selected in a round-robin
1187 * fashion, regardless of router preference values.
1188 * 3) If the Default Router List is empty, assume that all
1189 * destinations are on-link.
1191 * When Scoped Routing is enabled, the selection logic is amended as follows:
1193 * a) When a default interface is specified, the primary/non-scoped default
1194 * router will be set to the reachable router on that link (if any) with
1195 * the highest router preference.
1196 * b) When there are more than one routers on the same link, the one with
1197 * the highest router preference will be installed, either as scoped or
1198 * non-scoped route entry. If they all share the same preference value,
1199 * the one installed will be the static or the first encountered reachable
1200 * router, i.e. static one wins over dynamic.
1201 * c) When no routers on the list are known to be reachable, or probably
1202 * reachable, no round-robin selection will take place when the default
1205 * We assume nd_defrouter is sorted by router preference value.
1206 * Since the code below covers both with and without router preference cases,
1207 * we do not need to classify the cases by ifdef.
1210 defrouter_select(struct ifnet
*ifp
)
1213 struct nd_defrouter
*dr
, *selected_dr
= NULL
, *installed_dr
= NULL
;
1214 struct nd_defrouter
*installed_dr0
= NULL
;
1215 struct rtentry
*rt
= NULL
;
1216 struct llinfo_nd6
*ln
= NULL
;
1218 boolean_t found_installedrt
= FALSE
;
1220 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1223 * We no longer install (default) interface route; only prefix routes
1224 * are installed as interface routes. Therefore, there is no harm in
1225 * going through this routine even if a default interface is specified,
1226 * which happens when Scoped Routing is enabled. But for efficiency,
1227 * we fall back to the original KAME logic when Scoped Routing is
1230 if (ip6_forwarding
&& !ip6_doscopedroute
) {
1231 nd6log((LOG_WARNING
,
1232 "defrouter_select: called unexpectedly (forwarding=%d)\n",
1238 * Let's handle easy case (3) first:
1239 * If default router list is empty, there's nothing to be done.
1241 if (!TAILQ_FIRST(&nd_defrouter
))
1245 * Due to the number of times we drop nd6_mutex, we need to
1246 * serialize this function.
1248 while (nd_defrouter_busy
) {
1249 nd_defrouter_waiters
++;
1250 msleep(nd_defrouter_waitchan
, nd6_mutex
, (PZERO
-1),
1252 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1254 nd_defrouter_busy
= TRUE
;
1257 * Search for a (probably) reachable router from the list.
1258 * We just pick up the first reachable one (if any), assuming that
1259 * the ordering rule of the list described in defrtrlist_update().
1261 * For all intents and purposes of Scoped Routing:
1262 * selected_dr = candidate for primary router
1263 * installed_dr = currently installed primary router
1265 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1266 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1267 boolean_t reachable
, advrouter
;
1268 struct in6_addr rtaddr
;
1269 struct ifnet
*drifp
;
1270 struct nd_defrouter
*drrele
;
1275 rtaddr
= *(&dr
->rtaddr
);
1277 advrouter
= (drifp
!= NULL
&&
1278 (drifp
->if_eflags
& IFEF_IPV6_ROUTER
));
1279 NDDR_ADDREF_LOCKED(dr
); /* for this for loop */
1282 lck_mtx_unlock(nd6_mutex
);
1283 /* Callee returns a locked route upon success */
1284 if ((rt
= nd6_lookup(&rtaddr
, 0, drifp
, 0)) != NULL
) {
1285 RT_LOCK_ASSERT_HELD(rt
);
1286 if ((ln
= rt
->rt_llinfo
) != NULL
&&
1287 ND6_IS_LLINFO_PROBREACH(ln
)) {
1289 if (selected_dr
== NULL
&&
1290 (!ip6_doscopedroute
||
1291 (drifp
== nd6_defifp
&& !advrouter
))) {
1293 NDDR_ADDREF(selected_dr
);
1296 RT_REMREF_LOCKED(rt
);
1300 lck_mtx_lock(nd6_mutex
);
1302 /* Handle case (b) */
1304 if (ip6_doscopedroute
&& drifp
== nd6_defifp
&& !advrouter
&&
1305 (selected_dr
== NULL
|| rtpref(dr
) > rtpref(selected_dr
) ||
1306 (rtpref(dr
) == rtpref(selected_dr
) &&
1307 (dr
->stateflags
& NDDRF_STATIC
) &&
1308 !(selected_dr
->stateflags
& NDDRF_STATIC
)))) {
1310 /* Release it later on */
1311 VERIFY(drrele
== NULL
);
1312 drrele
= selected_dr
;
1315 NDDR_ADDREF_LOCKED(selected_dr
);
1318 if (!(dr
->stateflags
& NDDRF_INSTALLED
)) {
1320 * If the router hasn't been installed and it is
1321 * reachable, try to install it later on below.
1322 * If it's static, try to install it anyway.
1324 if (!advrouter
&& (reachable
||
1325 (dr
->stateflags
& NDDRF_STATIC
))) {
1328 nd6log2((LOG_INFO
, "%s: possible router %s, "
1329 "scoped=%d, static=%d\n", if_name(drifp
),
1330 ip6_sprintf(&rtaddr
),
1331 (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
1332 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1335 NDDR_REMREF(dr
); /* for this for loop */
1337 NDDR_REMREF(drrele
);
1341 /* Record the currently installed primary/non-scoped router */
1342 if (!ip6_doscopedroute
|| !(dr
->stateflags
& NDDRF_IFSCOPE
)) {
1343 if (installed_dr
== NULL
) {
1345 NDDR_ADDREF_LOCKED(installed_dr
);
1347 /* this should not happen; warn for diagnosis */
1348 log(LOG_ERR
, "defrouter_select: more than one "
1349 "%s default router is installed\n",
1350 ip6_doscopedroute
? "non-scoped" : "");
1354 NDDR_REMREF(dr
); /* for this for loop */
1356 NDDR_REMREF(drrele
);
1359 /* If none was selected, use the currently installed one */
1360 if (ip6_doscopedroute
&& selected_dr
== NULL
&& installed_dr
!= NULL
) {
1361 selected_dr
= installed_dr
;
1362 NDDR_ADDREF(selected_dr
);
1366 * Install the unreachable one(s) if necesssary.
1368 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1369 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1370 struct nd_defrouter
*_dr
;
1372 if (!ip6_doscopedroute
)
1377 /* If already (or will be) installed, skip */
1378 if ((dr
->stateflags
& NDDRF_INSTALLED
) || dr
->genid
== -1) {
1383 /* See if there is already a default router for the link */
1384 for (_dr
= TAILQ_FIRST(&nd_defrouter
); _dr
;
1385 _dr
= TAILQ_NEXT(_dr
, dr_entry
)) {
1388 if (_dr
== dr
|| _dr
->ifp
!= dr
->ifp
) {
1394 if ((_dr
->stateflags
& NDDRF_INSTALLED
) ||
1404 /* If none so far, schedule it to be installed below */
1405 if (_dr
== NULL
&& dr
->ifp
!= NULL
&&
1406 !(dr
->ifp
->if_eflags
& IFEF_IPV6_ROUTER
)) {
1409 nd6log2((LOG_INFO
, "%s: possible router %s, "
1410 "static=%d (unreachable)\n", if_name(dr
->ifp
),
1411 ip6_sprintf(&dr
->rtaddr
),
1412 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1419 nd6log2((LOG_INFO
, "%s: considering primary default router %s, "
1420 "static=%d [round 1]\n", if_name(dr
->ifp
),
1421 ip6_sprintf(&dr
->rtaddr
),
1422 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1426 * If none of the default routers was found to be reachable,
1427 * round-robin the list regardless of preference, except when
1428 * Scoped Routing is enabled per case (c).
1430 * Otherwise, if we have an installed router, check if the selected
1431 * (reachable) router should really be preferred to the installed one.
1432 * We only prefer the new router when the old one is not reachable
1433 * or when the new one has a really higher preference value.
1435 if (!ip6_doscopedroute
&& selected_dr
== NULL
) {
1436 if (installed_dr
== NULL
||
1437 !TAILQ_NEXT(installed_dr
, dr_entry
)) {
1438 selected_dr
= TAILQ_FIRST(&nd_defrouter
);
1440 NDDR_ADDREF(selected_dr
);
1442 selected_dr
= TAILQ_NEXT(installed_dr
, dr_entry
);
1444 NDDR_ADDREF(selected_dr
);
1446 } else if (selected_dr
!= NULL
&& installed_dr
!= NULL
) {
1447 lck_mtx_unlock(nd6_mutex
);
1448 rt
= nd6_lookup(&installed_dr
->rtaddr
, 0, installed_dr
->ifp
, 0);
1450 RT_LOCK_ASSERT_HELD(rt
);
1451 if ((ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) &&
1452 ND6_IS_LLINFO_PROBREACH(ln
) &&
1453 (!ip6_doscopedroute
||
1454 installed_dr
->ifp
== nd6_defifp
) &&
1455 rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1456 NDDR_REMREF(selected_dr
);
1457 selected_dr
= installed_dr
;
1458 NDDR_ADDREF(selected_dr
);
1460 RT_REMREF_LOCKED(rt
);
1463 found_installedrt
= TRUE
;
1465 lck_mtx_lock(nd6_mutex
);
1468 if (ip6_doscopedroute
) {
1470 * If the installed primary router is not on the current
1471 * IPv6 default interface, demote it to a scoped entry.
1473 if (installed_dr
!= NULL
&& installed_dr
->ifp
!= nd6_defifp
&&
1474 !(installed_dr
->stateflags
& NDDRF_IFSCOPE
)) {
1475 if (selected_dr
!= NULL
&&
1476 selected_dr
->ifp
!= nd6_defifp
) {
1477 NDDR_REMREF(selected_dr
);
1484 * If the selected router is currently scoped, make sure
1485 * we update (it needs to be promoted to primary.)
1487 if (selected_dr
!= NULL
&&
1488 (selected_dr
->stateflags
& NDDRF_IFSCOPE
))
1492 * If the installed router is no longer reachable, remove
1493 * it and install the selected router instead.
1495 if (installed_dr
!= NULL
1496 && selected_dr
!= NULL
1497 && installed_dr
!= selected_dr
1498 && found_installedrt
== FALSE
1499 && installed_dr
->ifp
== selected_dr
->ifp
) {
1501 installed_dr0
= installed_dr
;
1502 /* NB: we previousled referenced installed_dr */
1503 installed_dr
= NULL
;
1504 selected_dr
->genid
= -1;
1510 * If Scoped Routing is enabled and there's nothing to update,
1511 * just return. Otherwise, if Scoped Routing is disabled and if
1512 * the selected router is different than the installed one,
1513 * remove the installed router and install the selected one.
1516 VERIFY(dr
!= NULL
|| ip6_doscopedroute
);
1517 if (!ip6_doscopedroute
|| !update
) {
1521 if (dr
!= installed_dr
) {
1522 nd6log2((LOG_INFO
, "%s: no update, selected router %s, "
1523 "installed router %s\n", if_name(dr
->ifp
),
1524 ip6_sprintf(&dr
->rtaddr
), installed_dr
!= NULL
?
1525 ip6_sprintf(&installed_dr
->rtaddr
) : "NONE"));
1527 nd6log2((LOG_INFO
, "%s: no update, router is %s\n",
1528 if_name(dr
->ifp
), ip6_sprintf(&dr
->rtaddr
)));
1530 if (!ip6_doscopedroute
&& installed_dr
!= dr
) {
1532 * No need to ADDREF dr because at this point
1533 * dr points to selected_dr, which already holds
1536 lck_mtx_unlock(nd6_mutex
);
1538 defrouter_delreq(installed_dr
);
1540 defrouter_addreq(dr
, FALSE
);
1541 lck_mtx_lock(nd6_mutex
);
1547 * Scoped Routing is enabled and we need to update. The selected
1548 * router needs to be installed as primary/non-scoped entry. If
1549 * there is any existing entry that is non-scoped, remove it from
1550 * the routing table and reinstall it as scoped entry.
1553 nd6log2((LOG_INFO
, "%s: considering primary default router %s, "
1554 "static=%d [round 2]\n", if_name(dr
->ifp
),
1555 ip6_sprintf(&dr
->rtaddr
),
1556 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1560 * On the following while loops we use two flags:
1564 * genid is used to skip entries that are not to be added/removed on the
1565 * second while loop.
1566 * NDDRF_PROCESSED is used to skip entries that were already
1568 * This is necessary because we drop the nd6_mutex and start the while
1571 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
1573 VERIFY((dr
->stateflags
& NDDRF_PROCESSED
) == 0);
1576 /* Remove conflicting entries */
1577 dr
= TAILQ_FIRST(&nd_defrouter
);
1580 if (!(dr
->stateflags
& NDDRF_INSTALLED
) ||
1581 dr
->stateflags
& NDDRF_PROCESSED
) {
1583 dr
= TAILQ_NEXT(dr
, dr_entry
);
1586 dr
->stateflags
|= NDDRF_PROCESSED
;
1588 /* A NULL selected_dr will remove primary default route */
1589 if ((dr
== selected_dr
&& (dr
->stateflags
& NDDRF_IFSCOPE
)) ||
1590 (dr
!= selected_dr
&& !(dr
->stateflags
& NDDRF_IFSCOPE
))) {
1591 NDDR_ADDREF_LOCKED(dr
);
1593 lck_mtx_unlock(nd6_mutex
);
1594 defrouter_delreq(dr
);
1595 lck_mtx_lock(nd6_mutex
);
1597 if (dr
&& dr
!= installed_dr0
)
1602 * Since we lost nd6_mutex, we have to start over.
1604 dr
= TAILQ_FIRST(&nd_defrouter
);
1608 dr
= TAILQ_NEXT(dr
, dr_entry
);
1611 /* -1 is a special number, make sure we don't use it for genid */
1612 if (++nd6_defrouter_genid
== -1)
1613 nd6_defrouter_genid
= 1;
1615 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
1617 dr
->stateflags
&= ~NDDRF_PROCESSED
;
1620 /* Add the entries back */
1621 dr
= TAILQ_FIRST(&nd_defrouter
);
1623 struct nd_defrouter
*_dr
;
1626 if (dr
->stateflags
& NDDRF_PROCESSED
||
1629 dr
= TAILQ_NEXT(dr
, dr_entry
);
1632 dr
->stateflags
|= NDDRF_PROCESSED
;
1634 /* Handle case (b) */
1635 for (_dr
= TAILQ_FIRST(&nd_defrouter
); _dr
;
1636 _dr
= TAILQ_NEXT(_dr
, dr_entry
)) {
1640 * This is safe because we previously checked if
1644 if (_dr
->ifp
== dr
->ifp
&& rtpref(_dr
) >= rtpref(dr
) &&
1645 (_dr
->stateflags
& NDDRF_INSTALLED
)) {
1646 NDDR_ADDREF_LOCKED(_dr
);
1653 /* If same preference and i/f, static entry takes precedence */
1654 if (_dr
!= NULL
&& rtpref(_dr
) == rtpref(dr
) &&
1655 !(_dr
->stateflags
& NDDRF_STATIC
) &&
1656 (dr
->stateflags
& NDDRF_STATIC
)) {
1657 lck_mtx_unlock(nd6_mutex
);
1658 defrouter_delreq(_dr
);
1659 lck_mtx_lock(nd6_mutex
);
1664 if (_dr
== NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
)) {
1665 NDDR_ADDREF_LOCKED(dr
);
1667 lck_mtx_unlock(nd6_mutex
);
1668 defrouter_addreq(dr
, (selected_dr
== NULL
||
1669 dr
->ifp
!= selected_dr
->ifp
));
1670 dr
->genid
= nd6_defrouter_genid
;
1671 lck_mtx_lock(nd6_mutex
);
1674 * Since we lost nd6_mutex, we have to start over.
1676 dr
= TAILQ_FIRST(&nd_defrouter
);
1680 dr
= TAILQ_NEXT(dr
, dr_entry
);
1683 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
1685 dr
->stateflags
&= ~NDDRF_PROCESSED
;
1689 NDDR_REMREF(selected_dr
);
1691 NDDR_REMREF(installed_dr
);
1693 NDDR_REMREF(installed_dr0
);
1694 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1695 VERIFY(nd_defrouter_busy
);
1696 nd_defrouter_busy
= FALSE
;
1697 if (nd_defrouter_waiters
> 0) {
1698 nd_defrouter_waiters
= 0;
1699 wakeup(nd_defrouter_waitchan
);
1703 static struct nd_defrouter
*
1704 defrtrlist_update_common(struct nd_defrouter
*new, boolean_t scoped
)
1706 struct nd_defrouter
*dr
, *n
;
1707 struct ifnet
*ifp
= new->ifp
;
1708 struct nd_ifinfo
*ndi
= NULL
;
1709 struct timeval caltime
;
1711 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1713 if ((dr
= defrouter_lookup(&new->rtaddr
, ifp
)) != NULL
) {
1715 if (new->rtlifetime
== 0) {
1720 int oldpref
= rtpref(dr
);
1723 dr
->flags
= new->flags
; /* xxx flag check */
1724 dr
->rtlifetime
= new->rtlifetime
;
1725 dr
->expire
= new->expire
;
1728 * If the preference does not change, there's no need
1729 * to sort the entries. If Scoped Routing is enabled,
1730 * put the primary/non-scoped router at the top of the
1731 * list of routers in the same preference band, unless
1732 * it's already at that position.
1734 if (ip6_doscopedroute
) {
1735 struct nd_defrouter
*p
= NULL
;
1737 /* same preference and scoped; just return */
1738 if (rtpref(new) == oldpref
&& scoped
)
1741 n
= TAILQ_FIRST(&nd_defrouter
);
1743 /* preference changed; sort it */
1744 if (rtpref(new) != oldpref
)
1747 /* not at the top of band; sort it */
1748 if (n
!= dr
&& rtpref(n
) == oldpref
&&
1749 (!p
|| rtpref(p
) > rtpref(n
)))
1753 n
= TAILQ_NEXT(n
, dr_entry
);
1756 /* nothing has changed, just return */
1757 if (n
== NULL
&& (scoped
||
1758 !(dr
->stateflags
& NDDRF_IFSCOPE
)))
1760 } else if (rtpref(new) == oldpref
) {
1765 * preferred router may be changed, so relocate
1767 * XXX: calling TAILQ_REMOVE directly is a bad manner.
1768 * However, since defrtrlist_del() has many side
1769 * effects, we intentionally do so here.
1770 * defrouter_select() below will handle routing
1773 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1774 new->stateflags
= dr
->stateflags
;
1775 new->stateflags
&= ~NDDRF_PROCESSED
;
1785 /* entry does not exist */
1786 if (new->rtlifetime
== 0) {
1790 n
= nddr_alloc(M_WAITOK
);
1795 lck_rw_lock_shared(nd_if_rwlock
);
1796 if (ifp
->if_index
>= nd_ifinfo_indexlim
)
1798 ndi
= &nd_ifinfo
[ifp
->if_index
];
1799 VERIFY(ndi
->initialized
);
1800 lck_mtx_lock(&ndi
->lock
);
1801 if (ip6_maxifdefrouters
>= 0 &&
1802 ndi
->ndefrouters
>= ip6_maxifdefrouters
) {
1803 lck_mtx_unlock(&ndi
->lock
);
1805 lck_rw_done(nd_if_rwlock
);
1810 NDDR_ADDREF(n
); /* for the nd_defrouter list */
1811 NDDR_ADDREF(n
); /* for the caller */
1813 ++nd6_defrouter_genid
;
1815 VERIFY(ndi
->ndefrouters
!= 0);
1816 lck_mtx_unlock(&ndi
->lock
);
1817 lck_rw_done(nd_if_rwlock
);
1819 nd6log2((LOG_INFO
, "%s: allocating defrouter %s\n", if_name(ifp
),
1820 ip6_sprintf(&new->rtaddr
)));
1822 getmicrotime(&caltime
);
1824 memcpy(&n
->rtaddr
, &new->rtaddr
, sizeof (n
->rtaddr
));
1825 n
->flags
= new->flags
;
1826 n
->stateflags
= new->stateflags
;
1827 n
->stateflags
&= ~NDDRF_PROCESSED
;
1828 n
->rtlifetime
= new->rtlifetime
;
1829 n
->expire
= new->expire
;
1830 n
->base_calendartime
= caltime
.tv_sec
;
1831 n
->base_uptime
= net_uptime();
1833 n
->genid
= new->genid
;
1837 /* get nd6_service() to be scheduled as soon as it's convenient */
1838 ++nd6_sched_timeout_want
;
1841 * Insert the new router in the Default Router List;
1842 * The Default Router List should be in the descending order
1843 * of router-preferece. When Scoped Routing is disabled, routers
1844 * with the same preference are sorted in the arriving time order;
1845 * otherwise, the first entry in the list of routers having the same
1846 * preference is the primary default router, when the interface used
1847 * by the entry is the default interface.
1850 /* insert at the end of the group */
1851 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1852 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1853 if (rtpref(n
) > rtpref(dr
) ||
1854 (ip6_doscopedroute
&& !scoped
&& rtpref(n
) == rtpref(dr
)))
1858 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1860 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
1862 defrouter_select(ifp
);
1867 static struct nd_defrouter
*
1868 defrtrlist_update(struct nd_defrouter
*new)
1870 struct nd_defrouter
*dr
;
1872 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1873 dr
= defrtrlist_update_common(new,
1874 (nd6_defifp
!= NULL
&& new->ifp
!= nd6_defifp
));
1880 defrtrlist_sync(struct ifnet
*ifp
)
1882 struct nd_defrouter
*dr
, new;
1884 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1886 if (!ip6_doscopedroute
) {
1887 defrouter_select(ifp
);
1891 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1892 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1894 if (dr
->ifp
== ifp
&& (dr
->stateflags
& NDDRF_INSTALLED
))
1900 defrouter_select(ifp
);
1902 memcpy(&new.rtaddr
, &dr
->rtaddr
, sizeof (new.rtaddr
));
1903 new.flags
= dr
->flags
;
1904 new.stateflags
= dr
->stateflags
;
1905 new.stateflags
&= ~NDDRF_PROCESSED
;
1906 new.rtlifetime
= dr
->rtlifetime
;
1907 new.expire
= dr
->expire
;
1909 new.genid
= dr
->genid
;
1912 dr
= defrtrlist_update_common(&new, FALSE
);
1918 static struct nd_pfxrouter
*
1919 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1921 struct nd_pfxrouter
*search
;
1923 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1924 NDPR_LOCK_ASSERT_HELD(pr
);
1926 for (search
= pr
->ndpr_advrtrs
.lh_first
; search
;
1927 search
= search
->pfr_next
) {
1928 if (search
->router
== dr
)
1936 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1938 struct nd_pfxrouter
*new;
1940 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1941 NDPR_LOCK_ASSERT_NOTHELD(pr
);
1943 new = zalloc(ndprtr_zone
);
1946 bzero(new, sizeof (*new));
1950 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
1954 pfxlist_onlink_check();
1958 pfxrtr_del(struct nd_pfxrouter
*pfr
, struct nd_prefix
*pr
)
1960 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1961 NDPR_LOCK_ASSERT_HELD(pr
);
1963 LIST_REMOVE(pfr
, pfr_entry
);
1964 zfree(ndprtr_zone
, pfr
);
1968 nd6_prefix_lookup(struct nd_prefix
*pr
)
1970 struct nd_prefix
*search
;
1972 lck_mtx_lock(nd6_mutex
);
1973 for (search
= nd_prefix
.lh_first
; search
; search
= search
->ndpr_next
) {
1975 if (pr
->ndpr_ifp
== search
->ndpr_ifp
&&
1976 pr
->ndpr_plen
== search
->ndpr_plen
&&
1977 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1978 &search
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1979 NDPR_ADDREF_LOCKED(search
);
1980 NDPR_UNLOCK(search
);
1983 NDPR_UNLOCK(search
);
1985 lck_mtx_unlock(nd6_mutex
);
1991 purge_detached(struct ifnet
*ifp
)
1993 struct nd_prefix
*pr
, *pr_next
;
1994 struct in6_ifaddr
*ia
;
1995 struct ifaddr
*ifa
, *ifa_next
;
1996 boolean_t removed
= FALSE
;
1998 lck_mtx_lock(nd6_mutex
);
2000 pr
= nd_prefix
.lh_first
;
2004 pr_next
= pr
->ndpr_next
;
2005 if (pr
->ndpr_ifp
!= ifp
||
2006 IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
) ||
2007 ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2008 !LIST_EMPTY(&pr
->ndpr_advrtrs
))) {
2013 NDPR_ADDREF_LOCKED(pr
);
2015 ifnet_lock_shared(ifp
);
2016 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
; ifa
= ifa_next
) {
2018 ifa_next
= ifa
->ifa_list
.tqe_next
;
2019 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2023 ia
= (struct in6_ifaddr
*)ifa
;
2024 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) ==
2025 IN6_IFF_AUTOCONF
&& ia
->ia6_ndpr
== pr
) {
2026 IFA_ADDREF_LOCKED(ifa
); /* for us */
2029 * Purging the address requires writer access
2030 * to the address list, so drop the ifnet lock
2031 * now and repeat from beginning.
2033 ifnet_lock_done(ifp
);
2034 lck_mtx_unlock(nd6_mutex
);
2036 IFA_REMREF(ifa
); /* drop ours */
2037 lck_mtx_lock(nd6_mutex
);
2039 pr
= nd_prefix
.lh_first
;
2044 ifnet_lock_done(ifp
);
2046 if (pr
->ndpr_addrcnt
== 0 &&
2047 !(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)) {
2052 * Reset the search from the beginning because
2053 * nd6_mutex may have been dropped in
2056 pr_next
= nd_prefix
.lh_first
;
2064 pfxlist_onlink_check();
2065 lck_mtx_unlock(nd6_mutex
);
2069 nd6_prelist_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
,
2070 struct nd_prefix
**newp
, boolean_t force_scoped
)
2072 struct nd_prefix
*new = NULL
;
2073 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2074 struct nd_ifinfo
*ndi
= NULL
;
2077 if (ip6_maxifprefixes
>= 0) {
2078 lck_rw_lock_shared(nd_if_rwlock
);
2079 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
2080 lck_rw_done(nd_if_rwlock
);
2083 ndi
= &nd_ifinfo
[ifp
->if_index
];
2084 VERIFY(ndi
->initialized
);
2085 lck_mtx_lock(&ndi
->lock
);
2086 if (ndi
->nprefixes
>= ip6_maxifprefixes
/ 2) {
2087 lck_mtx_unlock(&ndi
->lock
);
2088 lck_rw_done(nd_if_rwlock
);
2089 purge_detached(ifp
);
2090 lck_rw_lock_shared(nd_if_rwlock
);
2092 * Refresh pointer since nd_ifinfo[] may have grown;
2093 * repeating the bounds check against nd_ifinfo_indexlim
2094 * isn't necessary since the array never shrinks.
2096 ndi
= &nd_ifinfo
[ifp
->if_index
];
2097 lck_mtx_lock(&ndi
->lock
);
2099 if (ndi
->nprefixes
>= ip6_maxifprefixes
) {
2100 lck_mtx_unlock(&ndi
->lock
);
2101 lck_rw_done(nd_if_rwlock
);
2104 lck_mtx_unlock(&ndi
->lock
);
2105 lck_rw_done(nd_if_rwlock
);
2108 new = ndpr_alloc(M_WAITOK
);
2114 new->ndpr_ifp
= pr
->ndpr_ifp
;
2115 new->ndpr_prefix
= pr
->ndpr_prefix
;
2116 new->ndpr_plen
= pr
->ndpr_plen
;
2117 new->ndpr_vltime
= pr
->ndpr_vltime
;
2118 new->ndpr_pltime
= pr
->ndpr_pltime
;
2119 new->ndpr_flags
= pr
->ndpr_flags
;
2120 if (pr
->ndpr_stateflags
& NDPRF_STATIC
)
2121 new->ndpr_stateflags
|= NDPRF_STATIC
;
2123 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
2128 new->ndpr_lastupdate
= net_uptime();
2131 NDPR_ADDREF_LOCKED(new); /* for caller */
2133 /* initialization */
2134 LIST_INIT(&new->ndpr_advrtrs
);
2135 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
2136 /* make prefix in the canonical form */
2137 for (i
= 0; i
< 4; i
++)
2138 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
2139 new->ndpr_mask
.s6_addr32
[i
];
2143 /* get nd6_service() to be scheduled as soon as it's convenient */
2144 ++nd6_sched_timeout_want
;
2146 lck_mtx_lock(nd6_mutex
);
2147 /* link ndpr_entry to nd_prefix list */
2148 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
2149 new->ndpr_debug
|= IFD_ATTACHED
;
2150 NDPR_ADDREF(new); /* for nd_prefix list */
2152 /* ND_OPT_PI_FLAG_ONLINK processing */
2153 if (new->ndpr_raf_onlink
) {
2156 if ((e
= nd6_prefix_onlink_common(new, force_scoped
,
2157 new->ndpr_ifp
->if_index
)) != 0) {
2158 nd6log((LOG_ERR
, "nd6_prelist_add: failed to make "
2159 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
2160 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2161 new->ndpr_plen
, force_scoped
? "scoped" :
2162 "non-scoped", if_name(ifp
), e
));
2163 /* proceed anyway. XXX: is it correct? */
2168 pfxrtr_add(new, dr
);
2171 lck_rw_lock_shared(nd_if_rwlock
);
2173 * Refresh pointer since nd_ifinfo[] may have grown;
2174 * repeating the bounds check against nd_ifinfo_indexlim
2175 * isn't necessary since the array never shrinks.
2177 ndi
= &nd_ifinfo
[ifp
->if_index
];
2178 VERIFY(ndi
->initialized
);
2179 lck_mtx_lock(&ndi
->lock
);
2181 VERIFY(ndi
->nprefixes
!= 0);
2182 lck_mtx_unlock(&ndi
->lock
);
2183 lck_rw_done(nd_if_rwlock
);
2185 lck_mtx_unlock(nd6_mutex
);
2191 * Caller must have held an extra reference on nd_prefix.
2194 prelist_remove(struct nd_prefix
*pr
)
2196 struct nd_pfxrouter
*pfr
, *next
;
2197 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2200 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2201 NDPR_LOCK_ASSERT_HELD(pr
);
2203 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
2207 * If there are no more addresses, defunct the prefix. This is needed
2208 * because we don't want multiple threads calling prelist_remove() for
2209 * the same prefix and this might happen because we unlock nd6_mutex
2212 if (pr
->ndpr_addrcnt
== 0)
2213 pr
->ndpr_stateflags
|= NDPRF_DEFUNCT
;
2215 /* make sure to invalidate the prefix until it is really freed. */
2216 pr
->ndpr_vltime
= 0;
2217 pr
->ndpr_pltime
= 0;
2220 * Though these flags are now meaningless, we'd rather keep the value
2221 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2222 * when executing "ndp -p".
2224 if (pr
->ndpr_stateflags
& NDPRF_ONLINK
) {
2225 NDPR_ADDREF_LOCKED(pr
);
2227 lck_mtx_unlock(nd6_mutex
);
2228 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2229 nd6log((LOG_ERR
, "prelist_remove: failed to make "
2230 "%s/%d offlink on %s, errno=%d\n",
2231 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2232 pr
->ndpr_plen
, if_name(ifp
), e
));
2233 /* what should we do? */
2235 lck_mtx_lock(nd6_mutex
);
2237 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2241 if (pr
->ndpr_addrcnt
> 0) {
2243 * The state might have changed if we called
2244 * nd6_prefix_offlink().
2246 pr
->ndpr_stateflags
&= ~NDPRF_DEFUNCT
;
2247 return; /* notice here? */
2250 /* unlink ndpr_entry from nd_prefix list */
2251 LIST_REMOVE(pr
, ndpr_entry
);
2252 pr
->ndpr_debug
&= ~IFD_ATTACHED
;
2254 /* free list of routers that adversed the prefix */
2255 for (pfr
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr
= next
) {
2256 next
= pfr
->pfr_next
;
2257 pfxrtr_del(pfr
, pr
);
2260 lck_rw_lock_shared(nd_if_rwlock
);
2261 if (ifp
->if_index
< nd_ifinfo_indexlim
) {
2262 struct nd_ifinfo
*ndi
= &nd_ifinfo
[ifp
->if_index
];
2263 VERIFY(ndi
->initialized
);
2264 lck_mtx_lock(&ndi
->lock
);
2265 VERIFY(ndi
->nprefixes
> 0);
2267 lck_mtx_unlock(&ndi
->lock
);
2269 lck_rw_done(nd_if_rwlock
);
2271 /* This must not be the last reference to the nd_prefix */
2272 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2273 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__
, pr
);
2278 * Don't call pfxlist_onlink_check() here because we are
2279 * holding the NDPR lock and this could cause a deadlock when
2280 * there are multiple threads executing pfxlist_onlink_check().
2286 struct nd_prefix
*new,
2287 struct nd_defrouter
*dr
, /* may be NULL */
2291 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
2293 struct ifnet
*ifp
= new->ndpr_ifp
;
2294 struct nd_prefix
*pr
;
2298 struct in6_addrlifetime lt6_tmp
;
2299 uint64_t timenow
= net_uptime();
2301 /* no need to lock "new" here, as it is local to the caller */
2302 NDPR_LOCK_ASSERT_NOTHELD(new);
2307 * Authenticity for NA consists authentication for
2308 * both IP header and IP datagrams, doesn't it ?
2310 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
2311 auth
= (m
->m_flags
& M_AUTHIPHDR
) && (m
->m_flags
& M_AUTHIPDGM
);
2315 if ((pr
= nd6_prefix_lookup(new)) != NULL
) {
2317 * nd6_prefix_lookup() ensures that pr and new have the same
2318 * prefix on a same interface.
2322 * Update prefix information. Note that the on-link (L) bit
2323 * and the autonomous (A) bit should NOT be changed from 1
2326 lck_mtx_lock(nd6_mutex
);
2328 if (new->ndpr_raf_onlink
== 1)
2329 pr
->ndpr_raf_onlink
= 1;
2330 if (new->ndpr_raf_auto
== 1)
2331 pr
->ndpr_raf_auto
= 1;
2332 if (new->ndpr_raf_onlink
) {
2333 pr
->ndpr_vltime
= new->ndpr_vltime
;
2334 pr
->ndpr_pltime
= new->ndpr_pltime
;
2335 (void) in6_init_prefix_ltimes(pr
); /* XXX error case? */
2336 pr
->ndpr_lastupdate
= net_uptime();
2339 NDPR_ADDREF_LOCKED(pr
);
2340 if (new->ndpr_raf_onlink
&&
2341 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
2345 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2347 "prelist_update: failed to make "
2348 "the prefix %s/%d on-link on %s "
2350 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2351 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
2352 /* proceed anyway. XXX: is it correct? */
2357 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
) {
2364 lck_mtx_unlock(nd6_mutex
);
2366 struct nd_prefix
*newpr
= NULL
;
2370 if (new->ndpr_vltime
== 0)
2372 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
2375 bzero(&new->ndpr_addr
, sizeof (struct in6_addr
));
2377 error
= nd6_prelist_add(new, dr
, &newpr
, FALSE
);
2378 if (error
!= 0 || newpr
== NULL
) {
2379 nd6log((LOG_NOTICE
, "prelist_update: "
2380 "nd6_prelist_add failed for %s/%d on %s "
2381 "errno=%d, returnpr=0x%llx\n",
2382 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2383 new->ndpr_plen
, if_name(new->ndpr_ifp
),
2384 error
, (uint64_t)VM_KERNEL_ADDRPERM(newpr
)));
2385 goto end
; /* we should just give up in this case. */
2389 * XXX: from the ND point of view, we can ignore a prefix
2390 * with the on-link bit being zero. However, we need a
2391 * prefix structure for references from autoconfigured
2392 * addresses. Thus, we explicitly make sure that the prefix
2393 * itself expires now.
2396 if (newpr
->ndpr_raf_onlink
== 0) {
2397 newpr
->ndpr_vltime
= 0;
2398 newpr
->ndpr_pltime
= 0;
2399 in6_init_prefix_ltimes(newpr
);
2407 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
2408 * Note that pr must be non NULL at this point.
2411 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
2412 if (!new->ndpr_raf_auto
)
2416 * 5.5.3 (b). the link-local prefix should have been ignored in
2420 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2421 if (new->ndpr_pltime
> new->ndpr_vltime
) {
2422 error
= EINVAL
; /* XXX: won't be used */
2427 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2428 * an address configured by stateless autoconfiguration already in the
2429 * list of addresses associated with the interface, and the Valid
2430 * Lifetime is not 0, form an address. We first check if we have
2431 * a matching prefix.
2433 ifnet_lock_shared(ifp
);
2434 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2435 struct in6_ifaddr
*ifa6
;
2436 u_int32_t remaininglifetime
;
2439 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2443 ifa6
= (struct in6_ifaddr
*)ifa
;
2446 * We only consider autoconfigured addresses as per RFC 4862.
2448 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
2453 * Spec is not clear here, but I believe we should concentrate
2454 * on unicast (i.e. not anycast) addresses.
2455 * XXX: other ia6_flags? detached or duplicated?
2457 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0) {
2462 * Ignore the address if it is not associated with a prefix
2463 * or is associated with a prefix that is different from this
2464 * one. (pr is never NULL here)
2466 if (ifa6
->ia6_ndpr
!= pr
) {
2471 if (ia6_match
== NULL
) { /* remember the first one */
2473 IFA_ADDREF_LOCKED(ifa
); /* for ia6_match */
2477 * An already autoconfigured address matched. Now that we
2478 * are sure there is at least one matched address, we can
2479 * proceed to 5.5.3. (e): update the lifetimes according to the
2480 * "two hours" rule and the privacy extension.
2482 #define TWOHOUR (120*60)
2484 /* retrieve time as uptime (last arg is 0) */
2485 in6ifa_getlifetime(ifa6
, <6_tmp
, 0);
2487 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
2488 remaininglifetime
= ND6_INFINITE_LIFETIME
;
2489 else if (timenow
- ifa6
->ia6_updatetime
> lt6_tmp
.ia6t_vltime
) {
2491 * The case of "invalid" address. We should usually
2492 * not see this case.
2494 remaininglifetime
= 0;
2496 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
2497 (timenow
- ifa6
->ia6_updatetime
);
2499 /* when not updating, keep the current stored lifetime. */
2500 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
2502 if (TWOHOUR
< new->ndpr_vltime
||
2503 remaininglifetime
< new->ndpr_vltime
) {
2504 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2505 } else if (remaininglifetime
<= TWOHOUR
) {
2507 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2511 * new->ndpr_vltime <= TWOHOUR &&
2512 * TWOHOUR < remaininglifetime
2514 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
2517 /* The 2 hour rule is not imposed for preferred lifetime. */
2518 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
2520 /* Special handling for lifetimes of temporary addresses. */
2521 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
2522 u_int32_t maxvltime
, maxpltime
;
2524 /* Constrain lifetimes to system limits. */
2525 if (lt6_tmp
.ia6t_vltime
> ip6_temp_valid_lifetime
)
2526 lt6_tmp
.ia6t_vltime
= ip6_temp_valid_lifetime
;
2527 if (lt6_tmp
.ia6t_pltime
> ip6_temp_preferred_lifetime
)
2528 lt6_tmp
.ia6t_pltime
=
2529 ip6_temp_preferred_lifetime
-
2533 * According to RFC 4941, section 3.3 (1), we only
2534 * update the lifetimes when they are in the maximum
2537 if (ip6_temp_valid_lifetime
>
2538 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2539 ip6_desync_factor
)) {
2540 maxvltime
= ip6_temp_valid_lifetime
-
2541 (timenow
- ifa6
->ia6_createtime
) -
2545 if (ip6_temp_preferred_lifetime
>
2546 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2547 ip6_desync_factor
)) {
2548 maxpltime
= ip6_temp_preferred_lifetime
-
2549 (timenow
- ifa6
->ia6_createtime
) -
2554 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
2555 lt6_tmp
.ia6t_vltime
> maxvltime
)
2556 lt6_tmp
.ia6t_vltime
= maxvltime
;
2558 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
2559 lt6_tmp
.ia6t_pltime
> maxpltime
)
2560 lt6_tmp
.ia6t_pltime
= maxpltime
;
2563 in6_init_address_ltimes(pr
, <6_tmp
);
2565 in6ifa_setlifetime(ifa6
, <6_tmp
);
2566 ifa6
->ia6_updatetime
= timenow
;
2569 ifnet_lock_done(ifp
);
2570 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
2572 * 5.5.3 (d) (continued)
2573 * No address matched and the valid lifetime is non-zero.
2574 * Create a new address.
2577 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
))
2580 * note that we should use pr (not new) for reference.
2582 IFA_LOCK(&ia6
->ia_ifa
);
2585 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2587 VERIFY(pr
->ndpr_addrcnt
!= 0);
2589 IFA_UNLOCK(&ia6
->ia_ifa
);
2593 * When a new public address is created as described
2594 * in RFC 4862, also create a new temporary address.
2597 * When an interface connects to a new link, a new
2598 * randomized interface identifier should be generated
2599 * immediately together with a new set of temporary
2600 * addresses. Thus, we specifiy 1 as the 2nd arg of
2603 if (ip6_use_tempaddr
) {
2605 if ((e
= in6_tmpifadd(ia6
, 1)) != 0) {
2606 nd6log((LOG_NOTICE
, "prelist_update: "
2607 "failed to create a temporary "
2608 "address, errno=%d\n",
2612 IFA_REMREF(&ia6
->ia_ifa
);
2616 * A newly added address might affect the status
2617 * of other addresses, so we check and update it.
2618 * XXX: what if address duplication happens?
2620 lck_mtx_lock(nd6_mutex
);
2621 pfxlist_onlink_check();
2622 lck_mtx_unlock(nd6_mutex
);
2631 if (ia6_match
!= NULL
)
2632 IFA_REMREF(&ia6_match
->ia_ifa
);
2637 * Neighbor Discover Default Router structure reference counting routines.
2639 static struct nd_defrouter
*
2642 struct nd_defrouter
*dr
;
2644 dr
= (how
== M_WAITOK
) ? zalloc(nddr_zone
) : zalloc_noblock(nddr_zone
);
2646 bzero(dr
, nddr_size
);
2647 lck_mtx_init(&dr
->nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2648 dr
->nddr_debug
|= IFD_ALLOC
;
2649 if (nddr_debug
!= 0) {
2650 dr
->nddr_debug
|= IFD_DEBUG
;
2651 dr
->nddr_trace
= nddr_trace
;
2658 nddr_free(struct nd_defrouter
*dr
)
2661 if (dr
->nddr_debug
& IFD_ATTACHED
) {
2662 panic("%s: attached nddr %p is being freed", __func__
, dr
);
2664 } else if (!(dr
->nddr_debug
& IFD_ALLOC
)) {
2665 panic("%s: nddr %p cannot be freed", __func__
, dr
);
2668 dr
->nddr_debug
&= ~IFD_ALLOC
;
2671 lck_mtx_destroy(&dr
->nddr_lock
, ifa_mtx_grp
);
2672 zfree(nddr_zone
, dr
);
2676 nddr_trace(struct nd_defrouter
*dr
, int refhold
)
2678 struct nd_defrouter_dbg
*dr_dbg
= (struct nd_defrouter_dbg
*)dr
;
2683 if (!(dr
->nddr_debug
& IFD_DEBUG
)) {
2684 panic("%s: nddr %p has no debug structure", __func__
, dr
);
2688 cnt
= &dr_dbg
->nddr_refhold_cnt
;
2689 tr
= dr_dbg
->nddr_refhold
;
2691 cnt
= &dr_dbg
->nddr_refrele_cnt
;
2692 tr
= dr_dbg
->nddr_refrele
;
2695 idx
= atomic_add_16_ov(cnt
, 1) % NDDR_TRACE_HIST_SIZE
;
2696 ctrace_record(&tr
[idx
]);
2700 nddr_addref(struct nd_defrouter
*nddr
, int locked
)
2704 NDDR_LOCK_SPIN(nddr
);
2706 NDDR_LOCK_ASSERT_HELD(nddr
);
2708 if (++nddr
->nddr_refcount
== 0) {
2709 panic("%s: nddr %p wraparound refcnt\n", __func__
, nddr
);
2711 } else if (nddr
->nddr_trace
!= NULL
) {
2712 (*nddr
->nddr_trace
)(nddr
, TRUE
);
2719 struct nd_defrouter
*
2720 nddr_remref(struct nd_defrouter
*nddr
, int locked
)
2724 NDDR_LOCK_SPIN(nddr
);
2726 NDDR_LOCK_ASSERT_HELD(nddr
);
2728 if (nddr
->nddr_refcount
== 0) {
2729 panic("%s: nddr %p negative refcnt\n", __func__
, nddr
);
2731 } else if (nddr
->nddr_trace
!= NULL
) {
2732 (*nddr
->nddr_trace
)(nddr
, FALSE
);
2735 if (--nddr
->nddr_refcount
== 0) {
2741 if (!locked
&& nddr
!= NULL
)
2748 nddr_getexpire(struct nd_defrouter
*dr
)
2750 struct timeval caltime
;
2753 if (dr
->expire
!= 0) {
2754 /* account for system time change */
2755 getmicrotime(&caltime
);
2757 dr
->base_calendartime
+=
2758 NET_CALCULATE_CLOCKSKEW(caltime
,
2759 dr
->base_calendartime
, net_uptime(), dr
->base_uptime
);
2761 expiry
= dr
->base_calendartime
+
2762 dr
->expire
- dr
->base_uptime
;
2770 * Neighbor Discover Prefix structure reference counting routines.
2772 static struct nd_prefix
*
2775 struct nd_prefix
*pr
;
2777 pr
= (how
== M_WAITOK
) ? zalloc(ndpr_zone
) : zalloc_noblock(ndpr_zone
);
2779 bzero(pr
, ndpr_size
);
2780 lck_mtx_init(&pr
->ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2781 RB_INIT(&pr
->ndpr_prproxy_sols
);
2782 pr
->ndpr_debug
|= IFD_ALLOC
;
2783 if (ndpr_debug
!= 0) {
2784 pr
->ndpr_debug
|= IFD_DEBUG
;
2785 pr
->ndpr_trace
= ndpr_trace
;
2792 ndpr_free(struct nd_prefix
*pr
)
2795 if (pr
->ndpr_debug
& IFD_ATTACHED
) {
2796 panic("%s: attached ndpr %p is being freed", __func__
, pr
);
2798 } else if (!(pr
->ndpr_debug
& IFD_ALLOC
)) {
2799 panic("%s: ndpr %p cannot be freed", __func__
, pr
);
2801 } else if (pr
->ndpr_rt
!= NULL
) {
2802 panic("%s: ndpr %p route %p not freed", __func__
, pr
,
2805 } else if (pr
->ndpr_prproxy_sols_cnt
!= 0) {
2806 panic("%s: ndpr %p non-zero solicitors count (%d)",
2807 __func__
, pr
, pr
->ndpr_prproxy_sols_cnt
);
2809 } else if (!RB_EMPTY(&pr
->ndpr_prproxy_sols
)) {
2810 panic("%s: ndpr %p non-empty solicitors tree", __func__
, pr
);
2813 pr
->ndpr_debug
&= ~IFD_ALLOC
;
2816 lck_mtx_destroy(&pr
->ndpr_lock
, ifa_mtx_grp
);
2817 zfree(ndpr_zone
, pr
);
2821 ndpr_trace(struct nd_prefix
*pr
, int refhold
)
2823 struct nd_prefix_dbg
*pr_dbg
= (struct nd_prefix_dbg
*)pr
;
2828 if (!(pr
->ndpr_debug
& IFD_DEBUG
)) {
2829 panic("%s: ndpr %p has no debug structure", __func__
, pr
);
2833 cnt
= &pr_dbg
->ndpr_refhold_cnt
;
2834 tr
= pr_dbg
->ndpr_refhold
;
2836 cnt
= &pr_dbg
->ndpr_refrele_cnt
;
2837 tr
= pr_dbg
->ndpr_refrele
;
2840 idx
= atomic_add_16_ov(cnt
, 1) % NDPR_TRACE_HIST_SIZE
;
2841 ctrace_record(&tr
[idx
]);
2845 ndpr_addref(struct nd_prefix
*ndpr
, int locked
)
2848 NDPR_LOCK_SPIN(ndpr
);
2850 NDPR_LOCK_ASSERT_HELD(ndpr
);
2852 if (++ndpr
->ndpr_refcount
== 0) {
2853 panic("%s: ndpr %p wraparound refcnt\n", __func__
, ndpr
);
2855 } else if (ndpr
->ndpr_trace
!= NULL
) {
2856 (*ndpr
->ndpr_trace
)(ndpr
, TRUE
);
2864 ndpr_remref(struct nd_prefix
*ndpr
, int locked
)
2867 NDPR_LOCK_SPIN(ndpr
);
2869 NDPR_LOCK_ASSERT_HELD(ndpr
);
2871 if (ndpr
->ndpr_refcount
== 0) {
2872 panic("%s: ndpr %p negative refcnt\n", __func__
, ndpr
);
2874 } else if (ndpr
->ndpr_trace
!= NULL
) {
2875 (*ndpr
->ndpr_trace
)(ndpr
, FALSE
);
2878 if (--ndpr
->ndpr_refcount
== 0) {
2879 if (ndpr
->ndpr_addrcnt
!= 0) {
2880 panic("%s: freeing ndpr %p with outstanding address "
2881 "reference (%d)", __func__
, ndpr
,
2882 ndpr
->ndpr_addrcnt
);
2890 if (!locked
&& ndpr
!= NULL
)
2897 ndpr_getexpire(struct nd_prefix
*pr
)
2899 struct timeval caltime
;
2902 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_vltime
!= ND6_INFINITE_LIFETIME
) {
2903 /* account for system time change */
2904 getmicrotime(&caltime
);
2906 pr
->ndpr_base_calendartime
+=
2907 NET_CALCULATE_CLOCKSKEW(caltime
,
2908 pr
->ndpr_base_calendartime
, net_uptime(),
2909 pr
->ndpr_base_uptime
);
2911 expiry
= pr
->ndpr_base_calendartime
+
2912 pr
->ndpr_expire
- pr
->ndpr_base_uptime
;
2920 * A supplement function used in the on-link detection below;
2921 * detect if a given prefix has a (probably) reachable advertising router.
2922 * XXX: lengthy function name...
2924 * Callers *must* increase the reference count of nd_prefix.
2926 static struct nd_pfxrouter
*
2927 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
2929 struct nd_pfxrouter
*pfxrtr
;
2931 struct llinfo_nd6
*ln
;
2933 struct in6_addr rtaddr
;
2936 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2937 NDPR_LOCK_ASSERT_HELD(pr
);
2939 genid
= pr
->ndpr_genid
;
2940 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2942 ifp
= pfxrtr
->router
->ifp
;
2943 rtaddr
= pfxrtr
->router
->rtaddr
;
2945 lck_mtx_unlock(nd6_mutex
);
2946 /* Callee returns a locked route upon success */
2947 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
2948 RT_LOCK_ASSERT_HELD(rt
);
2949 if ((ln
= rt
->rt_llinfo
) != NULL
&&
2950 ND6_IS_LLINFO_PROBREACH(ln
)) {
2951 RT_REMREF_LOCKED(rt
);
2953 lck_mtx_lock(nd6_mutex
);
2957 RT_REMREF_LOCKED(rt
);
2960 lck_mtx_lock(nd6_mutex
);
2962 if (pr
->ndpr_genid
!= genid
) {
2963 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2964 genid
= pr
->ndpr_genid
;
2966 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
);
2968 NDPR_LOCK_ASSERT_HELD(pr
);
2975 * Check if each prefix in the prefix list has at least one available router
2976 * that advertised the prefix (a router is "available" if its neighbor cache
2977 * entry is reachable or probably reachable).
2978 * If the check fails, the prefix may be off-link, because, for example,
2979 * we have moved from the network but the lifetime of the prefix has not
2980 * expired yet. So we should not use the prefix if there is another prefix
2981 * that has an available router.
2982 * But, if there is no prefix that has an available router, we still regards
2983 * all the prefixes as on-link. This is because we can't tell if all the
2984 * routers are simply dead or if we really moved from the network and there
2985 * is no router around us.
2988 pfxlist_onlink_check(void)
2990 struct nd_prefix
*pr
, *prclear
;
2991 struct in6_ifaddr
*ifa
;
2992 struct nd_defrouter
*dr
;
2993 struct nd_pfxrouter
*pfxrtr
= NULL
;
2994 int err
, i
, found
= 0;
2995 struct ifaddr
**ifap
= NULL
;
2996 struct nd_prefix
*ndpr
;
2998 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3000 while (nd_prefix_busy
) {
3001 nd_prefix_waiters
++;
3002 msleep(nd_prefix_waitchan
, nd6_mutex
, (PZERO
-1),
3004 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3006 nd_prefix_busy
= TRUE
;
3009 * Check if there is a prefix that has a reachable advertising
3012 pr
= nd_prefix
.lh_first
;
3015 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
) {
3020 NDPR_ADDREF_LOCKED(pr
);
3021 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
) &&
3022 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
3023 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
3029 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
3033 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
3034 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
3035 * stop us from checking the same prefix twice.
3037 pr
= nd_prefix
.lh_first
;
3039 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
3041 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
3042 NDPR_UNLOCK(prclear
);
3045 * If we have no such prefix, check whether we still have a router
3046 * that does not advertise any prefixes.
3049 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
3050 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
3051 struct nd_prefix
*pr0
;
3053 for (pr0
= nd_prefix
.lh_first
; pr0
;
3054 pr0
= pr0
->ndpr_next
) {
3056 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
) {
3066 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && pfxrtr
== NULL
)) {
3068 * There is at least one prefix that has a reachable router,
3069 * or at least a router which probably does not advertise
3070 * any prefixes. The latter would be the case when we move
3071 * to a new link where we have a router that does not provide
3072 * prefixes and we configure an address by hand.
3073 * Detach prefixes which have no reachable advertising
3074 * router, and attach other prefixes.
3076 pr
= nd_prefix
.lh_first
;
3080 * We aren't interested prefixes already processed,
3081 * nor in prefixes without the L bit
3082 * set nor in static prefixes
3084 if (pr
->ndpr_raf_onlink
== 0 ||
3085 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
3086 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
3091 NDPR_ADDREF_LOCKED(pr
);
3092 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
3093 find_pfxlist_reachable_router(pr
) == NULL
&&
3094 (pr
->ndpr_debug
& IFD_ATTACHED
))
3095 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
3096 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
3097 find_pfxlist_reachable_router(pr
) != NULL
&&
3098 (pr
->ndpr_debug
& IFD_ATTACHED
))
3099 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
3100 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
3104 * Since find_pfxlist_reachable_router() drops the
3105 * nd6_mutex, we have to start over, but the
3106 * NDPRF_PROCESSED_ONLINK flag will stop us from
3107 * checking the same prefix twice.
3109 pr
= nd_prefix
.lh_first
;
3112 /* there is no prefix that has a reachable router */
3113 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3115 if (pr
->ndpr_raf_onlink
== 0 ||
3116 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
3120 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
3121 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
3125 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
3127 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
3128 NDPR_UNLOCK(prclear
);
3131 * Remove each interface route associated with a (just) detached
3132 * prefix, and reinstall the interface route for a (just) attached
3133 * prefix. Note that all attempt of reinstallation does not
3134 * necessarily success, when a same prefix is shared among multiple
3135 * interfaces. Such cases will be handled in nd6_prefix_onlink,
3136 * so we don't have to care about them.
3138 pr
= nd_prefix
.lh_first
;
3143 if (pr
->ndpr_raf_onlink
== 0 ||
3144 pr
->ndpr_stateflags
& NDPRF_STATIC
||
3145 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
3146 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
3151 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
3152 NDPR_ADDREF_LOCKED(pr
);
3153 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
3154 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3156 lck_mtx_unlock(nd6_mutex
);
3157 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
3159 "pfxlist_onlink_check: failed to "
3160 "make %s/%d offlink, errno=%d\n",
3161 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3164 lck_mtx_lock(nd6_mutex
);
3166 pr
= nd_prefix
.lh_first
;
3169 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
3170 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
3171 pr
->ndpr_raf_onlink
) {
3173 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
3175 "pfxlist_onlink_check: failed to "
3176 "make %s/%d offlink, errno=%d\n",
3177 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3181 pr
= nd_prefix
.lh_first
;
3189 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
3191 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
3192 NDPR_UNLOCK(prclear
);
3194 VERIFY(nd_prefix_busy
);
3195 nd_prefix_busy
= FALSE
;
3196 if (nd_prefix_waiters
> 0) {
3197 nd_prefix_waiters
= 0;
3198 wakeup(nd_prefix_waitchan
);
3202 * Changes on the prefix status might affect address status as well.
3203 * Make sure that all addresses derived from an attached prefix are
3204 * attached, and that all addresses derived from a detached prefix are
3205 * detached. Note, however, that a manually configured address should
3206 * always be attached.
3207 * The precise detection logic is same as the one for prefixes.
3209 * ifnet_get_address_list_family_internal() may fail due to memory
3210 * pressure, but we will eventually be called again when we receive
3211 * another NA, RA, or when the link status changes.
3213 err
= ifnet_get_address_list_family_internal(NULL
, &ifap
, AF_INET6
, 0,
3215 if (err
!= 0 || ifap
== NULL
) {
3216 nd6log((LOG_ERR
, "%s: ifnet_get_address_list_family_internal "
3217 "failed", __func__
));
3220 for (i
= 0; ifap
[i
]; i
++) {
3221 ifa
= ifatoia6(ifap
[i
]);
3222 IFA_LOCK(&ifa
->ia_ifa
);
3223 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3224 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3225 IFA_UNLOCK(&ifa
->ia_ifa
);
3228 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3230 * This can happen when we first configure the address
3231 * (i.e. the address exists, but the prefix does not).
3232 * XXX: complicated relationships...
3234 IFA_UNLOCK(&ifa
->ia_ifa
);
3237 IFA_UNLOCK(&ifa
->ia_ifa
);
3240 NDPR_ADDREF_LOCKED(ndpr
);
3241 if (find_pfxlist_reachable_router(ndpr
)) {
3242 if (NDPR_REMREF_LOCKED(ndpr
) == NULL
) {
3254 for (i
= 0; ifap
[i
]; i
++) {
3255 ifa
= ifatoia6(ifap
[i
]);
3256 IFA_LOCK(&ifa
->ia_ifa
);
3257 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3258 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3259 IFA_UNLOCK(&ifa
->ia_ifa
);
3262 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3263 /* XXX: see above. */
3264 IFA_UNLOCK(&ifa
->ia_ifa
);
3267 IFA_UNLOCK(&ifa
->ia_ifa
);
3269 NDPR_ADDREF_LOCKED(ndpr
);
3270 if (find_pfxlist_reachable_router(ndpr
)) {
3272 IFA_LOCK(&ifa
->ia_ifa
);
3273 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3274 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3275 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
3276 IFA_UNLOCK(&ifa
->ia_ifa
);
3277 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3279 IFA_UNLOCK(&ifa
->ia_ifa
);
3283 IFA_LOCK(&ifa
->ia_ifa
);
3284 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
3285 IFA_UNLOCK(&ifa
->ia_ifa
);
3290 for (i
= 0; ifap
[i
]; i
++) {
3291 ifa
= ifatoia6(ifap
[i
]);
3292 IFA_LOCK(&ifa
->ia_ifa
);
3293 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3294 IFA_UNLOCK(&ifa
->ia_ifa
);
3297 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3298 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3299 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
3300 IFA_UNLOCK(&ifa
->ia_ifa
);
3301 /* Do we need a delay in this case? */
3302 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3304 IFA_UNLOCK(&ifa
->ia_ifa
);
3308 ifnet_free_address_list(ifap
);
3311 static struct nd_prefix
*
3312 nd6_prefix_equal_lookup(struct nd_prefix
*pr
, boolean_t primary_only
)
3314 struct nd_prefix
*opr
;
3316 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3318 for (opr
= nd_prefix
.lh_first
; opr
; opr
= opr
->ndpr_next
) {
3323 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3327 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
3328 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
3329 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
) &&
3331 !(opr
->ndpr_stateflags
& NDPRF_IFSCOPE
))) {
3332 NDPR_ADDREF_LOCKED(opr
);
3342 * Synchronize the interface routes of similar prefixes on different
3343 * interfaces; the one using the default interface would be (re)installed
3344 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3347 nd6_prefix_sync(struct ifnet
*ifp
)
3349 struct nd_prefix
*pr
, *opr
;
3352 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3354 if (!ip6_doscopedroute
|| ifp
== NULL
)
3357 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3359 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
3363 if (pr
->ndpr_ifp
== ifp
&&
3364 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) &&
3365 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3375 /* Remove conflicting entries */
3376 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3378 lck_mtx_unlock(nd6_mutex
);
3379 err
= nd6_prefix_offlink(opr
);
3380 lck_mtx_lock(nd6_mutex
);
3383 "%s: failed to make %s/%d offlink on %s, "
3384 "errno=%d\n", __func__
,
3385 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3386 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3390 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3391 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3392 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
)));
3395 lck_mtx_unlock(nd6_mutex
);
3396 err
= nd6_prefix_offlink(pr
);
3397 lck_mtx_lock(nd6_mutex
);
3400 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3401 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3402 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3405 /* Add the entries back */
3407 err
= nd6_prefix_onlink_scoped(opr
, opr
->ndpr_ifp
->if_index
);
3410 "%s: failed to make %s/%d scoped onlink on %s, "
3411 "errno=%d\n", __func__
,
3412 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3413 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3417 err
= nd6_prefix_onlink_scoped(pr
, IFSCOPE_NONE
);
3420 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3421 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3422 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3427 "%s: error promoting %s/%d to %s from %s\n",
3428 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3429 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3430 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3433 "%s: %s/%d promoted, previously on %s\n",
3434 if_name(pr
->ndpr_ifp
),
3435 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
,
3436 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3444 nd6_prefix_onlink_common(struct nd_prefix
*pr
, boolean_t force_scoped
,
3445 unsigned int ifscope
)
3448 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3449 struct sockaddr_in6 mask6
, prefix
;
3450 struct nd_prefix
*opr
;
3452 int error
= 0, prproxy
= 0;
3453 struct rtentry
*rt
= NULL
;
3455 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3459 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3461 "%s: %s/%d on %s scoped=%d is already on-link\n",
3462 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3463 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3464 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3471 * Add the interface route associated with the prefix. Before
3472 * installing the route, check if there's the same prefix on another
3473 * interface, and the prefix has already installed the interface route.
3475 opr
= nd6_prefix_equal_lookup(pr
, FALSE
);
3479 if (!ip6_doscopedroute
) {
3480 /* if an interface route already exists, just return */
3483 ifscope
= IFSCOPE_NONE
;
3484 } else if (!force_scoped
) {
3486 * If a primary/non-scoped interface route already exists,
3487 * install the new one as a scoped entry. If the existing
3488 * interface route is scoped, install new as non-scoped.
3490 ifscope
= (opr
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
3491 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3494 else if (ifscope
!= IFSCOPE_NONE
)
3495 ifscope
= IFSCOPE_NONE
;
3499 * We prefer link-local addresses as the associated interface address.
3501 /* search for a link-local addr */
3502 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
3503 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3505 struct in6_ifaddr
*ia6
;
3506 ifnet_lock_shared(ifp
);
3507 IFP_TO_IA6(ifp
, ia6
);
3508 ifnet_lock_done(ifp
);
3511 /* should we care about ia6_flags? */
3516 * This can still happen, when, for example, we receive an RA
3517 * containing a prefix with the L bit set and the A bit clear,
3518 * after removing all IPv6 addresses on the receiving
3519 * interface. This should, of course, be rare though.
3522 "nd6_prefix_onlink: failed to find any ifaddr"
3523 " to add route for a prefix(%s/%d) on %s\n",
3524 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3525 pr
->ndpr_plen
, if_name(ifp
)));
3531 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3532 * ifa->ifa_rtrequest = nd6_rtrequest;
3534 bzero(&mask6
, sizeof (mask6
));
3535 mask6
.sin6_len
= sizeof (mask6
);
3536 mask6
.sin6_addr
= pr
->ndpr_mask
;
3537 prefix
= pr
->ndpr_prefix
;
3538 if ((rt
= pr
->ndpr_rt
) != NULL
)
3540 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3544 rtflags
= ifa
->ifa_flags
| RTF_CLONING
| RTF_UP
;
3546 if (nd6_need_cache(ifp
)) {
3547 /* explicitly set in case ifa_flags does not set the flag. */
3548 rtflags
|= RTF_CLONING
;
3551 * explicitly clear the cloning bit in case ifa_flags sets it.
3553 rtflags
&= ~RTF_CLONING
;
3556 lck_mtx_unlock(nd6_mutex
);
3563 error
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&prefix
,
3564 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
,
3568 * Serialize the setting of NDPRF_PRPROXY.
3570 lck_mtx_lock(&proxy6_lock
);
3574 nd6_rtmsg(RTM_ADD
, rt
);
3579 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a"
3580 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx,"
3581 " scoped=%d, errno = %d\n",
3582 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3583 pr
->ndpr_plen
, if_name(ifp
),
3584 ip6_sprintf(&((struct sockaddr_in6
*)
3585 (void *)ifa
->ifa_addr
)->sin6_addr
),
3586 ip6_sprintf(&mask6
.sin6_addr
), rtflags
,
3587 (ifscope
!= IFSCOPE_NONE
), error
));
3589 NDPR_LOCK_ASSERT_HELD(pr
);
3591 pr
->ndpr_stateflags
&= ~(NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3594 * TODO: If the prefix route exists, we should really find it and
3595 * refer the prefix to it; otherwise ndpr_rt is NULL.
3597 if (!(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) &&
3598 (rt
!= NULL
|| error
== EEXIST
)) {
3599 struct nd_ifinfo
*ndi
;
3601 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3602 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3604 lck_rw_lock_shared(nd_if_rwlock
);
3605 ndi
= ND_IFINFO(ifp
);
3606 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
3607 lck_mtx_lock(&ndi
->lock
);
3609 pr
->ndpr_rt
= rt
; /* keep reference from rtrequest */
3610 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
3611 if (ifscope
!= IFSCOPE_NONE
) {
3612 pr
->ndpr_stateflags
|= NDPRF_IFSCOPE
;
3613 } else if ((rtflags
& RTF_CLONING
) &&
3614 (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) &&
3615 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3617 * At present, in order for the prefix to be eligible
3618 * as a proxying/proxied prefix, we require that the
3619 * prefix route entry be marked as a cloning route with
3620 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3621 * true for the interface type, hence the test for
3622 * RTF_CLONING above.
3624 pr
->ndpr_stateflags
|= NDPRF_PRPROXY
;
3627 lck_mtx_unlock(&ndi
->lock
);
3628 lck_rw_done(nd_if_rwlock
);
3629 } else if (rt
!= NULL
&& pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
3632 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3633 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3639 * If this is an upstream prefix, find the downstream ones (if any)
3640 * and re-configure their prefix routes accordingly. Otherwise,
3641 * this could be potentially be a downstream prefix, and so find the
3642 * upstream prefix, if any.
3644 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3646 NDPR_REMREF(pr
); /* release reference for this routine */
3647 lck_mtx_unlock(&proxy6_lock
);
3649 lck_mtx_lock(nd6_mutex
);
3655 nd6_prefix_onlink(struct nd_prefix
*pr
)
3657 return (nd6_prefix_onlink_common(pr
, FALSE
, IFSCOPE_NONE
));
3661 nd6_prefix_onlink_scoped(struct nd_prefix
*pr
, unsigned int ifscope
)
3663 return (nd6_prefix_onlink_common(pr
, TRUE
, ifscope
));
3667 nd6_prefix_offlink(struct nd_prefix
*pr
)
3669 int plen
, error
= 0, prproxy
;
3670 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3671 struct nd_prefix
*opr
;
3672 struct sockaddr_in6 sa6
, mask6
, prefix
;
3673 struct rtentry
*rt
= NULL
, *ndpr_rt
= NULL
;
3674 unsigned int ifscope
;
3676 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3680 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3682 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3683 "off-link\n", ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3684 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3685 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0));
3690 bzero(&sa6
, sizeof (sa6
));
3691 sa6
.sin6_family
= AF_INET6
;
3692 sa6
.sin6_len
= sizeof (sa6
);
3693 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
,
3694 sizeof (struct in6_addr
));
3695 bzero(&mask6
, sizeof (mask6
));
3696 mask6
.sin6_family
= AF_INET6
;
3697 mask6
.sin6_len
= sizeof (sa6
);
3698 bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof (struct in6_addr
));
3699 prefix
= pr
->ndpr_prefix
;
3700 plen
= pr
->ndpr_plen
;
3701 if ((ndpr_rt
= pr
->ndpr_rt
) != NULL
)
3703 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3706 ifscope
= (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ?
3707 ifp
->if_index
: IFSCOPE_NONE
;
3709 error
= rtrequest_scoped(RTM_DELETE
, (struct sockaddr
*)&sa6
,
3710 NULL
, (struct sockaddr
*)&mask6
, 0, &rt
, ifscope
);
3713 /* report the route deletion to the routing socket. */
3715 nd6_rtmsg(RTM_DELETE
, rt
);
3720 * The following check takes place only when Scoped Routing
3721 * is not enabled. There might be the same prefix on another
3722 * interface, the prefix which could not be on-link just
3723 * because we have the interface route (see comments in
3724 * nd6_prefix_onlink). If there's one, try to make the prefix
3725 * on-link on the interface.
3727 lck_mtx_lock(nd6_mutex
);
3728 opr
= nd_prefix
.lh_first
;
3730 /* does not apply in the Scoped Routing case */
3731 if (ip6_doscopedroute
)
3735 opr
= opr
->ndpr_next
;
3740 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3742 opr
= opr
->ndpr_next
;
3746 * KAME specific: detached prefixes should not be
3749 if ((opr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0) {
3751 opr
= opr
->ndpr_next
;
3754 if (opr
->ndpr_plen
== plen
&&
3755 in6_are_prefix_equal(&prefix
.sin6_addr
,
3756 &opr
->ndpr_prefix
.sin6_addr
, plen
)) {
3759 NDPR_ADDREF_LOCKED(opr
);
3761 if ((e
= nd6_prefix_onlink(opr
)) != 0) {
3763 "nd6_prefix_offlink: failed to "
3764 "recover a prefix %s/%d from %s "
3765 "to %s (errno = %d)\n",
3767 &opr
->ndpr_prefix
.sin6_addr
),
3768 opr
->ndpr_plen
, if_name(ifp
),
3769 if_name(opr
->ndpr_ifp
), e
));
3772 opr
= nd_prefix
.lh_first
;
3775 opr
= opr
->ndpr_next
;
3778 lck_mtx_unlock(nd6_mutex
);
3781 "nd6_prefix_offlink: failed to delete route: "
3782 "%s/%d on %s, scoped %d, (errno = %d)\n",
3783 ip6_sprintf(&sa6
.sin6_addr
), plen
, if_name(ifp
),
3784 (ifscope
!= IFSCOPE_NONE
), error
));
3787 if (ndpr_rt
!= NULL
)
3790 lck_mtx_lock(&proxy6_lock
);
3793 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3794 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3795 pr
->ndpr_stateflags
&= ~(NDPRF_ONLINK
| NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3796 if (pr
->ndpr_prproxy_sols_cnt
> 0) {
3798 nd6_prproxy_sols_reap(pr
);
3799 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3800 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3805 * If this was an upstream prefix, find the downstream ones and do
3806 * some cleanups. If this was a downstream prefix, the prefix route
3807 * has been removed from the routing table above, but there may be
3808 * other tasks to perform.
3810 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3812 NDPR_REMREF(pr
); /* release reference for this routine */
3813 lck_mtx_unlock(&proxy6_lock
);
3818 static struct in6_ifaddr
*
3819 in6_pfx_newpersistaddr(struct nd_prefix
*pr
, int mcast
, int *errorp
)
3821 struct in6_ifaddr
*ia6
;
3823 struct nd_ifinfo
*ndi
;
3824 struct in6_addr mask
;
3825 struct in6_aliasreq ifra
;
3826 int error
, ifaupdate
, iidlen
, notcga
;
3829 VERIFY(errorp
!= NULL
);
3837 * Prefix Length check:
3838 * If the sum of the prefix length and interface identifier
3839 * length does not equal 128 bits, the Prefix Information
3840 * option MUST be ignored. The length of the interface
3841 * identifier is defined in a separate link-type specific
3844 iidlen
= in6_if2idlen(ifp
);
3846 error
= EADDRNOTAVAIL
;
3847 /* this should not happen, so we always log it. */
3848 log(LOG_ERR
, "%s: IID length undefined (%s)\n",
3849 __func__
, if_name(ifp
));
3851 } else if (iidlen
!= 64) {
3852 error
= EADDRNOTAVAIL
;
3854 * stateless autoconfiguration not yet well-defined for IID
3855 * lengths other than 64 octets. Just give up for now.
3857 nd6log((LOG_INFO
, "%s: IID length not 64 octets (%s)\n",
3858 __func__
, if_name(ifp
)));
3862 if (iidlen
+ pr
->ndpr_plen
!= 128) {
3863 error
= EADDRNOTAVAIL
;
3865 "%s: invalid prefix length %d for %s, ignored\n",
3866 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
3870 lck_rw_lock_shared(nd_if_rwlock
);
3871 if (ifp
->if_index
>= nd_ifinfo_indexlim
) {
3872 error
= EADDRNOTAVAIL
;
3874 "%s: invalid prefix length %d for %s, ignored\n",
3875 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
3879 bzero(&ifra
, sizeof (ifra
));
3880 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3881 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
3882 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
3885 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
,
3886 sizeof (ifra
.ifra_addr
.sin6_addr
));
3887 in6_len2mask(&mask
, pr
->ndpr_plen
);
3888 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
3889 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
3890 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
3891 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
3893 ndi
= &nd_ifinfo
[ifp
->if_index
];
3894 VERIFY(ndi
->initialized
);
3895 lck_mtx_lock(&ndi
->lock
);
3897 notcga
= nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
||
3898 (ndi
->flags
& ND6_IFF_INSECURE
) != 0;
3900 lck_mtx_unlock(&ndi
->lock
);
3901 lck_rw_done(nd_if_rwlock
);
3905 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
3907 error
= EADDRNOTAVAIL
;
3908 nd6log((LOG_INFO
, "%s: no link-local address (%s)\n",
3909 __func__
, if_name(ifp
)));
3913 IFA_LOCK(&ia6
->ia_ifa
);
3914 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
3915 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
3916 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
3917 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
3918 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3919 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
3920 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3921 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
3922 IFA_UNLOCK(&ia6
->ia_ifa
);
3923 IFA_REMREF(&ia6
->ia_ifa
);
3926 in6_cga_node_lock();
3927 error
= in6_cga_generate(NULL
, 0, &ifra
.ifra_addr
.sin6_addr
);
3928 in6_cga_node_unlock();
3930 ifra
.ifra_flags
|= IN6_IFF_SECURED
;
3932 nd6log((LOG_ERR
, "%s: no CGA available (%s)\n",
3933 __func__
, if_name(ifp
)));
3938 VERIFY(ia6
== NULL
);
3940 /* new prefix mask. */
3941 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
3942 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
3943 bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
,
3944 sizeof (ifra
.ifra_prefixmask
.sin6_addr
));
3947 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
3948 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
3951 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
3954 * Make sure that we do not have this address already. This should
3955 * usually not happen, but we can still see this case, e.g., if we
3956 * have manually configured the exact address to be configured.
3958 if ((ia6
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
))
3960 error
= EADDRNOTAVAIL
;
3961 IFA_REMREF(&ia6
->ia_ifa
);
3964 /* this should be rare enough to make an explicit log */
3965 log(LOG_INFO
, "%s: %s is already configured!\n",
3966 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
3971 * Allocate ifaddr structure, link into chain, etc.
3972 * If we are going to create a new address upon receiving a multicasted
3973 * RA, we need to impose a random delay before starting DAD.
3974 * [RFC 4862, Section 5.4.2]
3976 ifaupdate
= IN6_IFAUPDATE_NOWAIT
;
3978 ifaupdate
|= IN6_IFAUPDATE_DADDELAY
;
3979 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &ia6
);
3982 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
3983 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
),
3984 if_name(ifp
), error
));
3985 error
= EADDRNOTAVAIL
;
3989 VERIFY(ia6
!= NULL
);
3990 in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia6
, NULL
);
3994 lck_rw_done(nd_if_rwlock
);
4004 #define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
4007 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
)
4009 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
4010 struct in6_ifaddr
*ia
, *newia
;
4011 struct in6_aliasreq ifra
;
4012 int i
, error
, ifaupdate
;
4013 int trylimit
= 3; /* XXX: adhoc value */
4014 u_int32_t randid
[2];
4015 time_t vltime0
, pltime0
;
4016 uint64_t timenow
= net_uptime();
4017 struct in6_addr addr
;
4018 struct nd_prefix
*ndpr
;
4020 bzero(&ifra
, sizeof (ifra
));
4021 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
4022 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4023 ifra
.ifra_addr
= ia0
->ia_addr
;
4024 /* copy prefix mask */
4025 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
4026 /* clear the old IFID */
4027 for (i
= 0; i
< 4; i
++) {
4028 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
]
4029 &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
4031 addr
= ia0
->ia_addr
.sin6_addr
;
4032 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4035 in6_iid_mktmp(ifp
, (u_int8_t
*)randid
,
4036 (const u_int8_t
*)&addr
.s6_addr
[8], forcegen
);
4038 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
4039 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
4040 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
4041 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
4044 * in6_iid_mktmp() quite likely provided a unique interface ID.
4045 * However, we may still have a chance to see collision, because
4046 * there may be a time lag between generation of the ID and generation
4047 * of the address. So, we'll do one more sanity check.
4049 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
)) != NULL
) {
4050 IFA_REMREF(&ia
->ia_ifa
);
4051 if (trylimit
-- == 0) {
4052 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find "
4053 "a unique random IFID\n"));
4061 * The Valid Lifetime is the lower of the Valid Lifetime of the
4062 * public address or TEMP_VALID_LIFETIME.
4063 * The Preferred Lifetime is the lower of the Preferred Lifetime
4064 * of the public address or TEMP_PREFERRED_LIFETIME -
4067 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4068 if (ia0
->ia6_lifetime
.ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
4069 vltime0
= IFA6_IS_INVALID(ia0
, timenow
) ? 0 :
4070 (ia0
->ia6_lifetime
.ia6ti_vltime
-
4071 (timenow
- ia0
->ia6_updatetime
));
4072 if (vltime0
> ip6_temp_valid_lifetime
)
4073 vltime0
= ip6_temp_valid_lifetime
;
4075 vltime0
= ip6_temp_valid_lifetime
;
4077 if (ia0
->ia6_lifetime
.ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
4078 pltime0
= IFA6_IS_DEPRECATED(ia0
, timenow
) ? 0 :
4079 (ia0
->ia6_lifetime
.ia6ti_pltime
-
4080 (timenow
- ia0
->ia6_updatetime
));
4081 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
)
4082 pltime0
= ip6_temp_preferred_lifetime
-
4085 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
4087 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
4088 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
4089 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4091 * A temporary address is created only if this calculated Preferred
4092 * Lifetime is greater than REGEN_ADVANCE time units.
4094 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
)
4097 /* XXX: scope zone ID? */
4099 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
4101 /* allocate ifaddr structure, link into chain, etc. */
4102 ifaupdate
= IN6_IFAUPDATE_NOWAIT
| IN6_IFAUPDATE_DADDELAY
;
4103 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &newia
);
4105 nd6log((LOG_ERR
, "in6_tmpifadd: failed to add address.\n"));
4108 VERIFY(newia
!= NULL
);
4110 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4111 ndpr
= ia0
->ia6_ndpr
;
4114 * We lost the race with another thread that has purged
4115 * ia0 address; in this case, purge the tmp addr as well.
4117 nd6log((LOG_ERR
, "in6_tmpifadd: no public address\n"));
4118 VERIFY(!(ia0
->ia6_flags
& IN6_IFF_AUTOCONF
));
4119 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4120 in6_purgeaddr(&newia
->ia_ifa
);
4121 IFA_REMREF(&newia
->ia_ifa
);
4122 return (EADDRNOTAVAIL
);
4124 NDPR_ADDREF(ndpr
); /* for us */
4125 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
4126 IFA_LOCK(&newia
->ia_ifa
);
4127 if (newia
->ia6_ndpr
!= NULL
) {
4128 NDPR_LOCK(newia
->ia6_ndpr
);
4129 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
4130 newia
->ia6_ndpr
->ndpr_addrcnt
--;
4131 NDPR_UNLOCK(newia
->ia6_ndpr
);
4132 NDPR_REMREF(newia
->ia6_ndpr
); /* release addr reference */
4134 newia
->ia6_ndpr
= ndpr
;
4135 NDPR_LOCK(newia
->ia6_ndpr
);
4136 newia
->ia6_ndpr
->ndpr_addrcnt
++;
4137 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
4138 NDPR_ADDREF_LOCKED(newia
->ia6_ndpr
); /* for addr reference */
4139 NDPR_UNLOCK(newia
->ia6_ndpr
);
4140 IFA_UNLOCK(&newia
->ia_ifa
);
4142 * A newly added address might affect the status of other addresses.
4143 * XXX: when the temporary address is generated with a new public
4144 * address, the onlink check is redundant. However, it would be safe
4145 * to do the check explicitly everywhere a new address is generated,
4146 * and, in fact, we surely need the check when we create a new
4147 * temporary address due to deprecation of an old temporary address.
4149 lck_mtx_lock(nd6_mutex
);
4150 pfxlist_onlink_check();
4151 lck_mtx_unlock(nd6_mutex
);
4152 IFA_REMREF(&newia
->ia_ifa
);
4154 /* remove our reference */
4162 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
4164 struct timeval caltime
;
4165 u_int64_t timenow
= net_uptime();
4167 NDPR_LOCK_ASSERT_HELD(ndpr
);
4169 getmicrotime(&caltime
);
4170 ndpr
->ndpr_base_calendartime
= caltime
.tv_sec
;
4171 ndpr
->ndpr_base_uptime
= timenow
;
4173 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
4174 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
4175 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime"
4176 "(%d) is greater than valid lifetime(%d)\n",
4177 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
));
4180 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
4181 ndpr
->ndpr_preferred
= 0;
4183 ndpr
->ndpr_preferred
= timenow
+ ndpr
->ndpr_pltime
;
4184 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
4185 ndpr
->ndpr_expire
= 0;
4187 ndpr
->ndpr_expire
= timenow
+ ndpr
->ndpr_vltime
;
4193 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
4196 uint64_t timenow
= net_uptime();
4198 /* Valid lifetime must not be updated unless explicitly specified. */
4199 /* init ia6t_expire */
4200 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
4201 lt6
->ia6t_expire
= 0;
4203 lt6
->ia6t_expire
= timenow
;
4204 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
4207 /* init ia6t_preferred */
4208 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
) {
4209 lt6
->ia6t_preferred
= 0;
4211 lt6
->ia6t_preferred
= timenow
;
4212 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
4217 * Delete all the routing table entries that use the specified gateway.
4218 * XXX: this function causes search through all entries of routing table, so
4219 * it shouldn't be called when acting as a router.
4223 struct in6_addr
*gateway
,
4226 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
4228 /* We'll care only link-local addresses */
4229 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
4232 lck_mtx_lock(rnh_lock
);
4233 /* XXX: hack for KAME's link-local address kludge */
4234 gateway
->s6_addr16
[1] = htons(ifp
->if_index
);
4236 rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
);
4237 lck_mtx_unlock(rnh_lock
);
4242 struct radix_node
*rn
,
4245 struct rtentry
*rt
= (struct rtentry
*)rn
;
4246 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
4248 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
4251 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
) {
4256 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) {
4261 * Do not delete a static route.
4262 * XXX: this seems to be a bit ad-hoc. Should we consider the
4263 * 'cloned' bit instead?
4265 if ((rt
->rt_flags
& RTF_STATIC
) != 0) {
4270 * We delete only host route. This means, in particular, we don't
4271 * delete default route.
4273 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
4279 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
4280 * rnh_lock here prevents another thread from calling rt_setgate()
4284 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4285 rt_mask(rt
), rt
->rt_flags
, 0));
4289 nd6_setdefaultiface(
4293 ifnet_t def_ifp
= NULL
;
4295 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4297 ifnet_head_lock_shared();
4298 if (ifindex
< 0 || if_index
< ifindex
) {
4302 def_ifp
= ifindex2ifnet
[ifindex
];
4305 lck_mtx_lock(nd6_mutex
);
4306 if (nd6_defifindex
!= ifindex
) {
4307 struct ifnet
*odef_ifp
= nd6_defifp
;
4309 nd6_defifindex
= ifindex
;
4310 if (nd6_defifindex
> 0)
4311 nd6_defifp
= def_ifp
;
4315 if (nd6_defifp
!= NULL
)
4316 nd6log((LOG_INFO
, "%s: is now the default "
4317 "interface (was %s)\n", if_name(nd6_defifp
),
4318 odef_ifp
!= NULL
? if_name(odef_ifp
) : "NONE"));
4320 nd6log((LOG_INFO
, "No default interface set\n"));
4323 * If the Default Router List is empty, install a route
4324 * to the specified interface as default or remove the default
4325 * route when the default interface becomes canceled.
4326 * The check for the queue is actually redundant, but
4327 * we do this here to avoid re-install the default route
4328 * if the list is NOT empty.
4330 if (ip6_doscopedroute
|| TAILQ_FIRST(&nd_defrouter
) == NULL
) {
4331 defrtrlist_sync(nd6_defifp
);
4332 nd6_prefix_sync(nd6_defifp
);
4336 * Our current implementation assumes one-to-one mapping between
4337 * interfaces and links, so it would be natural to use the
4338 * default interface as the default link.
4340 scope6_setdefault(nd6_defifp
);
4342 lck_mtx_unlock(nd6_mutex
);