2 * Copyright (c) 2000-2019 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
61 * BSD/OS version heavily modifies this code, related to llinfo.
62 * Since we don't have BSD/OS version of net/route.c in our hand,
63 * I left the code mostly as it was in 970310. -- itojun
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/sockio.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/errno.h>
76 #include <sys/syslog.h>
77 #include <sys/protosw.h>
79 #include <sys/mcache.h>
81 #include <dev/random/randomdev.h>
83 #include <kern/queue.h>
84 #include <kern/zalloc.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
89 #include <net/if_llreach.h>
90 #include <net/route.h>
92 #include <net/ntstat.h>
93 #include <net/net_osdep.h>
94 #include <net/nwk_wq.h>
96 #include <netinet/in.h>
97 #include <netinet/in_arp.h>
98 #include <netinet/if_ether.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet/icmp6.h>
110 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
111 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
113 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
116 int nd6_prune
= 1; /* walk list every 1 seconds */
117 int nd6_prune_lazy
= 5; /* lazily walk list every 5 seconds */
118 int nd6_delay
= 5; /* delay first probe time 5 second */
119 int nd6_umaxtries
= 3; /* maximum unicast query */
120 int nd6_mmaxtries
= 3; /* maximum multicast query */
121 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
122 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
124 /* preventing too many loops in ND option parsing */
125 int nd6_maxndopt
= 10; /* max # of ND options allowed */
127 int nd6_maxqueuelen
= 1; /* max # of packets cached in unresolved ND entries */
135 int nd6_optimistic_dad
=
136 (ND6_OPTIMISTIC_DAD_LINKLOCAL
| ND6_OPTIMISTIC_DAD_AUTOCONF
|
137 ND6_OPTIMISTIC_DAD_TEMPORARY
| ND6_OPTIMISTIC_DAD_DYNAMIC
|
138 ND6_OPTIMISTIC_DAD_SECURED
| ND6_OPTIMISTIC_DAD_MANUAL
);
141 static int nd6_inuse
, nd6_allocated
;
144 * Synchronization notes:
146 * The global list of ND entries are stored in llinfo_nd6; an entry
147 * gets inserted into the list when the route is created and gets
148 * removed from the list when it is deleted; this is done as part
149 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in nd6_rtrequest().
151 * Because rnh_lock and rt_lock for the entry are held during those
152 * operations, the same locks (and thus lock ordering) must be used
153 * elsewhere to access the relevant data structure fields:
155 * ln_next, ln_prev, ln_rt
157 * - Routing lock (rnh_lock)
159 * ln_hold, ln_asked, ln_expire, ln_state, ln_router, ln_flags,
160 * ln_llreach, ln_lastused
162 * - Routing entry lock (rt_lock)
164 * Due to the dependency on rt_lock, llinfo_nd6 has the same lifetime
165 * as the route entry itself. When a route is deleted (RTM_DELETE),
166 * it is simply removed from the global list but the memory is not
167 * freed until the route itself is freed.
169 struct llinfo_nd6 llinfo_nd6
= {
170 .ln_next
= &llinfo_nd6
,
171 .ln_prev
= &llinfo_nd6
,
174 static lck_grp_attr_t
*nd_if_lock_grp_attr
= NULL
;
175 static lck_grp_t
*nd_if_lock_grp
= NULL
;
176 static lck_attr_t
*nd_if_lock_attr
= NULL
;
178 /* Protected by nd6_mutex */
179 struct nd_drhead nd_defrouter
;
180 struct nd_prhead nd_prefix
= { .lh_first
= 0 };
183 * nd6_timeout() is scheduled on a demand basis. nd6_timeout_run is used
184 * to indicate whether or not a timeout has been scheduled. The rnh_lock
185 * mutex is used to protect this scheduling; it is a natural choice given
186 * the work done in the timer callback. Unfortunately, there are cases
187 * when nd6_timeout() needs to be scheduled while rnh_lock cannot be easily
188 * held, due to lock ordering. In those cases, we utilize a "demand" counter
189 * nd6_sched_timeout_want which can be atomically incremented without
190 * having to hold rnh_lock. On places where we acquire rnh_lock, such as
191 * nd6_rtrequest(), we check this counter and schedule the timer if it is
192 * non-zero. The increment happens on various places when we allocate
193 * new ND entries, default routers, prefixes and addresses.
195 static int nd6_timeout_run
; /* nd6_timeout is scheduled to run */
196 static void nd6_timeout(void *);
197 int nd6_sched_timeout_want
; /* demand count for timer to be sched */
198 static boolean_t nd6_fast_timer_on
= FALSE
;
200 /* Serialization variables for nd6_service(), protected by rnh_lock */
201 static boolean_t nd6_service_busy
;
202 static void *nd6_service_wc
= &nd6_service_busy
;
203 static int nd6_service_waiters
= 0;
205 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
206 static struct sockaddr_in6 all1_sa
;
208 static int regen_tmpaddr(struct in6_ifaddr
*);
209 extern lck_mtx_t
*nd6_mutex
;
211 static struct llinfo_nd6
*nd6_llinfo_alloc(int);
212 static void nd6_llinfo_free(void *);
213 static void nd6_llinfo_purge(struct rtentry
*);
214 static void nd6_llinfo_get_ri(struct rtentry
*, struct rt_reach_info
*);
215 static void nd6_llinfo_get_iflri(struct rtentry
*, struct ifnet_llreach_info
*);
216 static void nd6_llinfo_refresh(struct rtentry
*);
217 static uint64_t ln_getexpire(struct llinfo_nd6
*);
219 static void nd6_service(void *);
220 static void nd6_slowtimo(void *);
221 static int nd6_is_new_addr_neighbor(struct sockaddr_in6
*, struct ifnet
*);
222 static int nd6_siocgdrlst(void *, int);
223 static int nd6_siocgprlst(void *, int);
225 static int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
;
226 static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
;
229 * Insertion and removal from llinfo_nd6 must be done with rnh_lock held.
231 #define LN_DEQUEUE(_ln) do { \
232 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); \
233 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
234 (_ln)->ln_next->ln_prev = (_ln)->ln_prev; \
235 (_ln)->ln_prev->ln_next = (_ln)->ln_next; \
236 (_ln)->ln_prev = (_ln)->ln_next = NULL; \
237 (_ln)->ln_flags &= ~ND6_LNF_IN_USE; \
240 #define LN_INSERTHEAD(_ln) do { \
241 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); \
242 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
243 (_ln)->ln_next = llinfo_nd6.ln_next; \
244 llinfo_nd6.ln_next = (_ln); \
245 (_ln)->ln_prev = &llinfo_nd6; \
246 (_ln)->ln_next->ln_prev = (_ln); \
247 (_ln)->ln_flags |= ND6_LNF_IN_USE; \
250 static struct zone
*llinfo_nd6_zone
;
251 #define LLINFO_ND6_ZONE_MAX 256 /* maximum elements in zone */
252 #define LLINFO_ND6_ZONE_NAME "llinfo_nd6" /* name for zone */
254 extern int tvtohz(struct timeval
*);
256 static int nd6_init_done
;
258 SYSCTL_DECL(_net_inet6_icmp6
);
260 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
261 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
262 nd6_sysctl_drlist
, "S,in6_defrouter", "");
264 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
265 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
266 nd6_sysctl_prlist
, "S,in6_defrouter", "");
268 SYSCTL_DECL(_net_inet6_ip6
);
270 static int ip6_maxchainsent
= 0;
271 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, maxchainsent
,
272 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_maxchainsent
, 0,
273 "use dlil_output_list");
280 VERIFY(!nd6_init_done
);
282 all1_sa
.sin6_family
= AF_INET6
;
283 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
284 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++) {
285 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
288 /* initialization of the default router list */
289 TAILQ_INIT(&nd_defrouter
);
291 nd_if_lock_grp_attr
= lck_grp_attr_alloc_init();
292 nd_if_lock_grp
= lck_grp_alloc_init("nd_if_lock", nd_if_lock_grp_attr
);
293 nd_if_lock_attr
= lck_attr_alloc_init();
295 llinfo_nd6_zone
= zinit(sizeof(struct llinfo_nd6
),
296 LLINFO_ND6_ZONE_MAX
* sizeof(struct llinfo_nd6
), 0,
297 LLINFO_ND6_ZONE_NAME
);
298 if (llinfo_nd6_zone
== NULL
) {
299 panic("%s: failed allocating llinfo_nd6_zone", __func__
);
302 zone_change(llinfo_nd6_zone
, Z_EXPAND
, TRUE
);
303 zone_change(llinfo_nd6_zone
, Z_CALLERACCT
, FALSE
);
312 timeout(nd6_slowtimo
, NULL
, ND6_SLOWTIMER_INTERVAL
* hz
);
315 static struct llinfo_nd6
*
316 nd6_llinfo_alloc(int how
)
318 struct llinfo_nd6
*ln
;
320 ln
= (how
== M_WAITOK
) ? zalloc(llinfo_nd6_zone
) :
321 zalloc_noblock(llinfo_nd6_zone
);
323 bzero(ln
, sizeof(*ln
));
330 nd6_llinfo_free(void *arg
)
332 struct llinfo_nd6
*ln
= arg
;
334 if (ln
->ln_next
!= NULL
|| ln
->ln_prev
!= NULL
) {
335 panic("%s: trying to free %p when it is in use", __func__
, ln
);
339 /* Just in case there's anything there, free it */
340 if (ln
->ln_hold
!= NULL
) {
341 m_freem_list(ln
->ln_hold
);
345 /* Purge any link-layer info caching */
346 VERIFY(ln
->ln_rt
->rt_llinfo
== ln
);
347 if (ln
->ln_rt
->rt_llinfo_purge
!= NULL
) {
348 ln
->ln_rt
->rt_llinfo_purge(ln
->ln_rt
);
351 zfree(llinfo_nd6_zone
, ln
);
355 nd6_llinfo_purge(struct rtentry
*rt
)
357 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
359 RT_LOCK_ASSERT_HELD(rt
);
360 VERIFY(rt
->rt_llinfo_purge
== nd6_llinfo_purge
&& ln
!= NULL
);
362 if (ln
->ln_llreach
!= NULL
) {
364 ifnet_llreach_free(ln
->ln_llreach
);
365 ln
->ln_llreach
= NULL
;
371 nd6_llinfo_get_ri(struct rtentry
*rt
, struct rt_reach_info
*ri
)
373 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
374 struct if_llreach
*lr
= ln
->ln_llreach
;
377 bzero(ri
, sizeof(*ri
));
378 ri
->ri_rssi
= IFNET_RSSI_UNKNOWN
;
379 ri
->ri_lqm
= IFNET_LQM_THRESH_OFF
;
380 ri
->ri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
383 /* Export to rt_reach_info structure */
385 /* Export ND6 send expiration (calendar) time */
387 ifnet_llreach_up2calexp(lr
, ln
->ln_lastused
);
393 nd6_llinfo_get_iflri(struct rtentry
*rt
, struct ifnet_llreach_info
*iflri
)
395 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
396 struct if_llreach
*lr
= ln
->ln_llreach
;
399 bzero(iflri
, sizeof(*iflri
));
400 iflri
->iflri_rssi
= IFNET_RSSI_UNKNOWN
;
401 iflri
->iflri_lqm
= IFNET_LQM_THRESH_OFF
;
402 iflri
->iflri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
405 /* Export to ifnet_llreach_info structure */
406 ifnet_lr2iflri(lr
, iflri
);
407 /* Export ND6 send expiration (uptime) time */
408 iflri
->iflri_snd_expire
=
409 ifnet_llreach_up2upexp(lr
, ln
->ln_lastused
);
415 nd6_llinfo_refresh(struct rtentry
*rt
)
417 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
418 uint64_t timenow
= net_uptime();
420 * Can't refresh permanent, static or entries that are
421 * not direct host entries
423 if (!ln
|| ln
->ln_expire
== 0 ||
424 (rt
->rt_flags
& RTF_STATIC
) ||
425 !(rt
->rt_flags
& RTF_LLINFO
)) {
429 if ((ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) &&
430 (ln
->ln_state
< ND6_LLINFO_PROBE
)) {
431 if (ln
->ln_expire
> timenow
) {
432 ln_setexpire(ln
, timenow
);
433 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PROBE
);
440 ndcache_state2str(short ndp_state
)
442 const char *ndp_state_str
= "UNKNOWN";
444 case ND6_LLINFO_PURGE
:
445 ndp_state_str
= "ND6_LLINFO_PURGE";
447 case ND6_LLINFO_NOSTATE
:
448 ndp_state_str
= "ND6_LLINFO_NOSTATE";
450 case ND6_LLINFO_INCOMPLETE
:
451 ndp_state_str
= "ND6_LLINFO_INCOMPLETE";
453 case ND6_LLINFO_REACHABLE
:
454 ndp_state_str
= "ND6_LLINFO_REACHABLE";
456 case ND6_LLINFO_STALE
:
457 ndp_state_str
= "ND6_LLINFO_STALE";
459 case ND6_LLINFO_DELAY
:
460 ndp_state_str
= "ND6_LLINFO_DELAY";
462 case ND6_LLINFO_PROBE
:
463 ndp_state_str
= "ND6_LLINFO_PROBE";
466 /* Init'd to UNKNOWN */
469 return ndp_state_str
;
473 ln_setexpire(struct llinfo_nd6
*ln
, uint64_t expiry
)
475 ln
->ln_expire
= expiry
;
479 ln_getexpire(struct llinfo_nd6
*ln
)
481 struct timeval caltime
;
484 if (ln
->ln_expire
!= 0) {
485 struct rtentry
*rt
= ln
->ln_rt
;
488 /* account for system time change */
489 getmicrotime(&caltime
);
491 rt
->base_calendartime
+=
492 NET_CALCULATE_CLOCKSKEW(caltime
,
493 rt
->base_calendartime
, net_uptime(), rt
->base_uptime
);
495 expiry
= rt
->base_calendartime
+
496 ln
->ln_expire
- rt
->base_uptime
;
504 nd6_ifreset(struct ifnet
*ifp
)
506 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
508 VERIFY(ndi
->initialized
);
510 LCK_MTX_ASSERT(&ndi
->lock
, LCK_MTX_ASSERT_OWNED
);
511 ndi
->linkmtu
= ifp
->if_mtu
;
512 ndi
->chlim
= IPV6_DEFHLIM
;
513 ndi
->basereachable
= REACHABLE_TIME
;
514 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
515 ndi
->retrans
= RETRANS_TIMER
;
519 nd6_ifattach(struct ifnet
*ifp
)
521 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
524 if (!ndi
->initialized
) {
525 lck_mtx_init(&ndi
->lock
, nd_if_lock_grp
, nd_if_lock_attr
);
526 ndi
->flags
= ND6_IFF_PERFORMNUD
;
527 ndi
->flags
|= ND6_IFF_DAD
;
528 ndi
->initialized
= TRUE
;
531 lck_mtx_lock(&ndi
->lock
);
533 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
534 ndi
->flags
|= ND6_IFF_IFDISABLED
;
538 lck_mtx_unlock(&ndi
->lock
);
542 "Reinit'd ND information for interface %s\n",
549 * XXX Look more into this. Especially since we recycle ifnets and do delayed
553 nd6_ifdetach(struct nd_ifinfo
*nd
)
555 /* XXX destroy nd's lock? */
561 nd6_setmtu(struct ifnet
*ifp
)
563 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
564 u_int32_t oldmaxmtu
, maxmtu
;
566 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)) {
570 lck_mtx_lock(&ndi
->lock
);
571 oldmaxmtu
= ndi
->maxmtu
;
574 * The ND level maxmtu is somewhat redundant to the interface MTU
575 * and is an implementation artifact of KAME. Instead of hard-
576 * limiting the maxmtu based on the interface type here, we simply
577 * take the if_mtu value since SIOCSIFMTU would have taken care of
578 * the sanity checks related to the maximum MTU allowed for the
579 * interface (a value that is known only by the interface layer),
580 * by sending the request down via ifnet_ioctl(). The use of the
581 * ND level maxmtu and linkmtu are done via IN6_LINKMTU() which
582 * does further checking against if_mtu.
584 maxmtu
= ndi
->maxmtu
= ifp
->if_mtu
;
587 * Decreasing the interface MTU under IPV6 minimum MTU may cause
588 * undesirable situation. We thus notify the operator of the change
589 * explicitly. The check for oldmaxmtu is necessary to restrict the
590 * log to the case of changing the MTU, not initializing it.
592 if (oldmaxmtu
>= IPV6_MMTU
&& ndi
->maxmtu
< IPV6_MMTU
) {
593 log(LOG_NOTICE
, "nd6_setmtu: "
594 "new link MTU on %s (%u) is too small for IPv6\n",
595 if_name(ifp
), (uint32_t)ndi
->maxmtu
);
597 ndi
->linkmtu
= ifp
->if_mtu
;
598 lck_mtx_unlock(&ndi
->lock
);
600 /* also adjust in6_maxmtu if necessary. */
601 if (maxmtu
> in6_maxmtu
) {
607 nd6_option_init(void *opt
, int icmp6len
, union nd_opts
*ndopts
)
609 bzero(ndopts
, sizeof(*ndopts
));
610 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
611 ndopts
->nd_opts_last
=
612 (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
615 ndopts
->nd_opts_done
= 1;
616 ndopts
->nd_opts_search
= NULL
;
621 * Take one ND option.
624 nd6_option(union nd_opts
*ndopts
)
626 struct nd_opt_hdr
*nd_opt
;
630 panic("ndopts == NULL in nd6_option\n");
632 if (!ndopts
->nd_opts_last
) {
633 panic("uninitialized ndopts in nd6_option\n");
635 if (!ndopts
->nd_opts_search
) {
638 if (ndopts
->nd_opts_done
) {
642 nd_opt
= ndopts
->nd_opts_search
;
644 /* make sure nd_opt_len is inside the buffer */
645 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
646 bzero(ndopts
, sizeof(*ndopts
));
650 olen
= nd_opt
->nd_opt_len
<< 3;
653 * Message validation requires that all included
654 * options have a length that is greater than zero.
656 bzero(ndopts
, sizeof(*ndopts
));
660 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
661 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
662 /* option overruns the end of buffer, invalid */
663 bzero(ndopts
, sizeof(*ndopts
));
665 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
666 /* reached the end of options chain */
667 ndopts
->nd_opts_done
= 1;
668 ndopts
->nd_opts_search
= NULL
;
674 * Parse multiple ND options.
675 * This function is much easier to use, for ND routines that do not need
676 * multiple options of the same type.
679 nd6_options(union nd_opts
*ndopts
)
681 struct nd_opt_hdr
*nd_opt
;
684 if (ndopts
== NULL
) {
685 panic("ndopts == NULL in nd6_options");
687 if (ndopts
->nd_opts_last
== NULL
) {
688 panic("uninitialized ndopts in nd6_options");
690 if (ndopts
->nd_opts_search
== NULL
) {
695 nd_opt
= nd6_option(ndopts
);
696 if (nd_opt
== NULL
&& ndopts
->nd_opts_last
== NULL
) {
698 * Message validation requires that all included
699 * options have a length that is greater than zero.
701 icmp6stat
.icp6s_nd_badopt
++;
702 bzero(ndopts
, sizeof(*ndopts
));
706 if (nd_opt
== NULL
) {
710 switch (nd_opt
->nd_opt_type
) {
711 case ND_OPT_SOURCE_LINKADDR
:
712 case ND_OPT_TARGET_LINKADDR
:
714 case ND_OPT_REDIRECTED_HEADER
:
716 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
718 "duplicated ND6 option found (type=%d)\n",
719 nd_opt
->nd_opt_type
);
722 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
726 case ND_OPT_PREFIX_INFORMATION
:
727 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
728 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
731 ndopts
->nd_opts_pi_end
=
732 (struct nd_opt_prefix_info
*)nd_opt
;
740 * Unknown options must be silently ignored,
741 * to accomodate future extension to the protocol.
744 "nd6_options: unsupported option %d - "
745 "option ignored\n", nd_opt
->nd_opt_type
);
750 if (i
> nd6_maxndopt
) {
751 icmp6stat
.icp6s_nd_toomanyopt
++;
752 nd6log(info
, "too many loop in nd opt\n");
756 if (ndopts
->nd_opts_done
) {
774 * ND6 service routine to expire default route list and prefix list
777 nd6_service(void *arg
)
779 struct nd6svc_arg
*ap
= arg
;
780 struct llinfo_nd6
*ln
;
781 struct nd_defrouter
*dr
= NULL
;
782 struct nd_prefix
*pr
= NULL
;
783 struct ifnet
*ifp
= NULL
;
784 struct in6_ifaddr
*ia6
, *nia6
;
786 boolean_t send_nc_failure_kev
= FALSE
;
787 struct nd_drhead nd_defrouter_tmp
;
788 struct nd_defrouter
*ndr
= NULL
;
789 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
791 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
793 * Since we may drop rnh_lock and nd6_mutex below, we want
794 * to run this entire operation single threaded.
796 while (nd6_service_busy
) {
797 nd6log2(debug
, "%s: %s is blocked by %d waiters\n",
798 __func__
, ap
->draining
? "drainer" : "timer",
799 nd6_service_waiters
);
800 nd6_service_waiters
++;
801 (void) msleep(nd6_service_wc
, rnh_lock
, (PZERO
- 1),
803 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
806 /* We are busy now; tell everyone else to go away */
807 nd6_service_busy
= TRUE
;
810 timenow
= net_uptime();
813 * send_nc_failure_kev gets set when default router's IPv6 address
815 * That can happen either:
816 * 1. When the entry has resolved once but can't be
817 * resolved later and the neighbor cache entry for gateway is deleted
818 * after max probe attempts.
820 * 2. When the entry is in ND6_LLINFO_INCOMPLETE but can not be resolved
821 * after max neighbor address resolution attempts.
823 * Both set send_nc_failure_kev to true. ifp is also set to the previous
824 * neighbor cache entry's route's ifp.
825 * Once we are done sending the notification, set send_nc_failure_kev
826 * to false to stop sending false notifications for non default router
829 * We may to send more information like Gateway's IP that could not be
830 * resolved, however right now we do not install more than one default
831 * route per interface in the routing table.
833 if (send_nc_failure_kev
&& ifp
!= NULL
&&
834 ifp
->if_addrlen
== IF_LLREACH_MAXLEN
) {
835 struct kev_msg ev_msg
;
836 struct kev_nd6_ndfailure nd6_ndfailure
;
837 bzero(&ev_msg
, sizeof(ev_msg
));
838 bzero(&nd6_ndfailure
, sizeof(nd6_ndfailure
));
839 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
840 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
841 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
842 ev_msg
.event_code
= KEV_ND6_NDFAILURE
;
844 nd6_ndfailure
.link_data
.if_family
= ifp
->if_family
;
845 nd6_ndfailure
.link_data
.if_unit
= ifp
->if_unit
;
846 strlcpy(nd6_ndfailure
.link_data
.if_name
,
848 sizeof(nd6_ndfailure
.link_data
.if_name
));
849 ev_msg
.dv
[0].data_ptr
= &nd6_ndfailure
;
850 ev_msg
.dv
[0].data_length
=
851 sizeof(nd6_ndfailure
);
852 dlil_post_complete_msg(NULL
, &ev_msg
);
855 send_nc_failure_kev
= FALSE
;
858 * The global list llinfo_nd6 is modified by nd6_request() and is
859 * therefore protected by rnh_lock. For obvious reasons, we cannot
860 * hold rnh_lock across calls that might lead to code paths which
861 * attempt to acquire rnh_lock, else we deadlock. Hence for such
862 * cases we drop rt_lock and rnh_lock, make the calls, and repeat the
863 * loop. To ensure that we don't process the same entry more than
864 * once in a single timeout, we mark the "already-seen" entries with
865 * ND6_LNF_TIMER_SKIP flag. At the end of the loop, we do a second
866 * pass thru the entries and clear the flag so they can be processed
867 * during the next timeout.
869 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
871 ln
= llinfo_nd6
.ln_next
;
872 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
874 struct sockaddr_in6
*dst
;
875 struct llinfo_nd6
*next
;
876 u_int32_t retrans
, flags
;
877 struct nd_ifinfo
*ndi
= NULL
;
878 boolean_t is_router
= FALSE
;
880 /* ln_next/prev/rt is protected by rnh_lock */
885 /* We've seen this already; skip it */
886 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
) {
893 /* rt->rt_ifp should never be NULL */
894 if ((ifp
= rt
->rt_ifp
) == NULL
) {
895 panic("%s: ln(%p) rt(%p) rt_ifp == NULL", __func__
,
900 /* rt_llinfo must always be equal to ln */
901 if ((struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
902 panic("%s: rt_llinfo(%p) is not equal to ln(%p)",
903 __func__
, rt
->rt_llinfo
, ln
);
907 /* rt_key should never be NULL */
908 dst
= SIN6(rt_key(rt
));
910 panic("%s: rt(%p) key is NULL ln(%p)", __func__
,
915 /* Set the flag in case we jump to "again" */
916 ln
->ln_flags
|= ND6_LNF_TIMER_SKIP
;
918 if (ln
->ln_expire
== 0 || (rt
->rt_flags
& RTF_STATIC
)) {
920 } else if (ap
->draining
&& (rt
->rt_refcnt
== 0)) {
922 * If we are draining, immediately purge non-static
923 * entries without oustanding route refcnt.
925 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
926 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
);
928 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PURGE
);
930 ln_setexpire(ln
, timenow
);
934 * If the entry has not expired, skip it. Take note on the
935 * state, as entries that are in the STALE state are simply
936 * waiting to be garbage collected, in which case we can
937 * relax the callout scheduling (use nd6_prune_lazy).
939 if (ln
->ln_expire
> timenow
) {
940 switch (ln
->ln_state
) {
941 case ND6_LLINFO_STALE
:
953 ndi
= ND_IFINFO(ifp
);
954 VERIFY(ndi
->initialized
);
955 retrans
= ndi
->retrans
;
958 RT_LOCK_ASSERT_HELD(rt
);
959 is_router
= (rt
->rt_flags
& RTF_ROUTER
) ? TRUE
: FALSE
;
961 switch (ln
->ln_state
) {
962 case ND6_LLINFO_INCOMPLETE
:
963 if (ln
->ln_asked
< nd6_mmaxtries
) {
964 struct ifnet
*exclifp
= ln
->ln_exclifp
;
966 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
967 RT_ADDREF_LOCKED(rt
);
969 lck_mtx_unlock(rnh_lock
);
970 if (ip6_forwarding
) {
971 nd6_prproxy_ns_output(ifp
, exclifp
,
972 NULL
, &dst
->sin6_addr
, ln
);
974 nd6_ns_output(ifp
, NULL
,
975 &dst
->sin6_addr
, ln
, NULL
);
979 lck_mtx_lock(rnh_lock
);
981 struct mbuf
*m
= ln
->ln_hold
;
983 send_nc_failure_kev
= is_router
;
985 RT_ADDREF_LOCKED(rt
);
987 lck_mtx_unlock(rnh_lock
);
991 mnext
= m
->m_nextpkt
;
993 m
->m_pkthdr
.rcvif
= ifp
;
994 icmp6_error_flag(m
, ICMP6_DST_UNREACH
,
995 ICMP6_DST_UNREACH_ADDR
, 0, 0);
999 RT_ADDREF_LOCKED(rt
);
1001 lck_mtx_unlock(rnh_lock
);
1005 * Enqueue work item to invoke callback for
1008 route_event_enqueue_nwk_wq_entry(rt
, NULL
,
1009 ROUTE_LLENTRY_UNREACH
, NULL
, FALSE
);
1012 lck_mtx_lock(rnh_lock
);
1014 * nd6_free above would flush out the routing table of
1015 * any cloned routes with same next-hop.
1016 * Walk the tree anyways as there could be static routes
1019 * We also already have a reference to rt that gets freed right
1020 * after the block below executes. Don't need an extra reference
1024 struct route_event rt_ev
;
1025 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_LLENTRY_UNREACH
);
1026 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
, (void *)&rt_ev
);
1030 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1033 case ND6_LLINFO_REACHABLE
:
1034 if (ln
->ln_expire
!= 0) {
1035 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
);
1036 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
1039 * Enqueue work item to invoke callback for
1042 route_event_enqueue_nwk_wq_entry(rt
, NULL
,
1043 ROUTE_LLENTRY_STALE
, NULL
, TRUE
);
1045 RT_ADDREF_LOCKED(rt
);
1048 struct route_event rt_ev
;
1049 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_LLENTRY_STALE
);
1050 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
, (void *)&rt_ev
);
1058 case ND6_LLINFO_STALE
:
1059 case ND6_LLINFO_PURGE
:
1060 /* Garbage Collection(RFC 4861 5.3) */
1061 if (ln
->ln_expire
!= 0) {
1062 RT_ADDREF_LOCKED(rt
);
1064 lck_mtx_unlock(rnh_lock
);
1067 lck_mtx_lock(rnh_lock
);
1075 case ND6_LLINFO_DELAY
:
1076 if ((flags
& ND6_IFF_PERFORMNUD
) != 0) {
1079 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PROBE
);
1080 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
1081 RT_ADDREF_LOCKED(rt
);
1083 lck_mtx_unlock(rnh_lock
);
1084 nd6_ns_output(ifp
, &dst
->sin6_addr
,
1085 &dst
->sin6_addr
, ln
, NULL
);
1088 lck_mtx_lock(rnh_lock
);
1091 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
); /* XXX */
1092 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
1097 case ND6_LLINFO_PROBE
:
1098 if (ln
->ln_asked
< nd6_umaxtries
) {
1100 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
1101 RT_ADDREF_LOCKED(rt
);
1103 lck_mtx_unlock(rnh_lock
);
1104 nd6_ns_output(ifp
, &dst
->sin6_addr
,
1105 &dst
->sin6_addr
, ln
, NULL
);
1108 lck_mtx_lock(rnh_lock
);
1110 is_router
= (rt
->rt_flags
& RTF_ROUTER
) ? TRUE
: FALSE
;
1111 send_nc_failure_kev
= is_router
;
1112 RT_ADDREF_LOCKED(rt
);
1114 lck_mtx_unlock(rnh_lock
);
1119 * Enqueue work item to invoke callback for
1122 route_event_enqueue_nwk_wq_entry(rt
, NULL
,
1123 ROUTE_LLENTRY_UNREACH
, NULL
, FALSE
);
1125 lck_mtx_lock(rnh_lock
);
1127 * nd6_free above would flush out the routing table of
1128 * any cloned routes with same next-hop.
1129 * Walk the tree anyways as there could be static routes
1132 * We also already have a reference to rt that gets freed right
1133 * after the block below executes. Don't need an extra reference
1137 struct route_event rt_ev
;
1138 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_LLENTRY_UNREACH
);
1139 (void) rnh
->rnh_walktree(rnh
,
1140 route_event_walktree
, (void *)&rt_ev
);
1144 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1153 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1155 /* Now clear the flag from all entries */
1156 ln
= llinfo_nd6
.ln_next
;
1157 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1158 struct rtentry
*rt
= ln
->ln_rt
;
1159 struct llinfo_nd6
*next
= ln
->ln_next
;
1162 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
) {
1163 ln
->ln_flags
&= ~ND6_LNF_TIMER_SKIP
;
1168 lck_mtx_unlock(rnh_lock
);
1170 /* expire default router list */
1171 TAILQ_INIT(&nd_defrouter_tmp
);
1173 lck_mtx_lock(nd6_mutex
);
1174 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter
, dr_entry
, ndr
) {
1176 if (dr
->expire
!= 0 && dr
->expire
< timenow
) {
1177 VERIFY(dr
->ifp
!= NULL
);
1178 in6_ifstat_inc(dr
->ifp
, ifs6_defrtr_expiry_cnt
);
1179 in6_event_enqueue_nwk_wq_entry(IN6_NDP_RTR_EXPIRY
, dr
->ifp
,
1180 &dr
->rtaddr
, dr
->rtlifetime
);
1181 if (dr
->ifp
!= NULL
&&
1182 dr
->ifp
->if_type
== IFT_CELLULAR
) {
1184 * Some buggy cellular gateways may not send
1185 * periodic router advertisements.
1186 * Or they may send it with router lifetime
1187 * value that is less than the configured Max and Min
1188 * Router Advertisement interval.
1189 * To top that an idle device may not wake up
1190 * when periodic RA is received on cellular
1192 * We could send RS on every wake but RFC
1193 * 4861 precludes that.
1194 * The addresses are of infinite lifetimes
1195 * and are tied to the lifetime of the bearer,
1196 * so keeping the addresses and just getting rid of
1197 * the router does not help us anyways.
1198 * If there's network renumbering, a lifetime with
1199 * value 0 would remove the default router.
1200 * Also it will get deleted as part of purge when
1201 * the PDP context is torn down and configured again.
1202 * For that reason, do not expire the default router
1203 * learned on cellular interface. Ever.
1205 dr
->expire
+= dr
->rtlifetime
;
1207 "%s: Refreshing expired default router entry "
1208 "%s for interface %s\n", __func__
,
1209 ip6_sprintf(&dr
->rtaddr
), if_name(dr
->ifp
));
1213 * Remove the entry from default router list
1214 * and add it to the temp list.
1215 * nd_defrouter_tmp will be a local temporary
1216 * list as no one else can get the same
1217 * removed entry once it is removed from default
1219 * Remove the reference after calling defrtrlist_del
1221 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1222 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
1225 if (dr
->expire
== 0 || (dr
->stateflags
& NDDRF_STATIC
)) {
1234 * Keep the following separate from the above
1235 * iteration of nd_defrouter because it's not safe
1236 * to call defrtrlist_del while iterating global default
1237 * router list. Global list has to be traversed
1238 * while holding nd6_mutex throughout.
1240 * The following call to defrtrlist_del should be
1241 * safe as we are iterating a local list of
1244 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, ndr
) {
1245 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
1247 NDDR_REMREF(dr
); /* remove list reference */
1251 * Also check if default router selection needs to be triggered
1252 * for default interface, to avoid an issue with co-existence of
1253 * static un-scoped default route configuration and default router
1254 * discovery/selection.
1256 if (trigger_v6_defrtr_select
) {
1257 defrouter_select(NULL
);
1258 trigger_v6_defrtr_select
= FALSE
;
1260 lck_mtx_unlock(nd6_mutex
);
1263 * expire interface addresses.
1264 * in the past the loop was inside prefix expiry processing.
1265 * However, from a stricter speci-confrmance standpoint, we should
1266 * rather separate address lifetimes and prefix lifetimes.
1269 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1270 for (ia6
= in6_ifaddrs
; ia6
; ia6
= nia6
) {
1271 int oldflags
= ia6
->ia6_flags
;
1273 nia6
= ia6
->ia_next
;
1274 IFA_LOCK(&ia6
->ia_ifa
);
1276 * Extra reference for ourselves; it's no-op if
1277 * we don't have to regenerate temporary address,
1278 * otherwise it protects the address from going
1279 * away since we drop in6_ifaddr_rwlock below.
1281 IFA_ADDREF_LOCKED(&ia6
->ia_ifa
);
1282 /* check address lifetime */
1283 if (IFA6_IS_INVALID(ia6
, timenow
)) {
1285 * If the expiring address is temporary, try
1286 * regenerating a new one. This would be useful when
1287 * we suspended a laptop PC, then turned it on after a
1288 * period that could invalidate all temporary
1289 * addresses. Although we may have to restart the
1290 * loop (see below), it must be after purging the
1291 * address. Otherwise, we'd see an infinite loop of
1294 if (ip6_use_tempaddr
&&
1295 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1297 * NOTE: We have to drop the lock here
1298 * because regen_tmpaddr() eventually calls
1299 * in6_update_ifa(), which must take the lock
1300 * and would otherwise cause a hang. This is
1301 * safe because the goto addrloop leads to a
1302 * re-evaluation of the in6_ifaddrs list
1304 IFA_UNLOCK(&ia6
->ia_ifa
);
1305 lck_rw_done(&in6_ifaddr_rwlock
);
1306 (void) regen_tmpaddr(ia6
);
1308 IFA_UNLOCK(&ia6
->ia_ifa
);
1309 lck_rw_done(&in6_ifaddr_rwlock
);
1313 * Purging the address would have caused
1314 * in6_ifaddr_rwlock to be dropped and reacquired;
1315 * therefore search again from the beginning
1316 * of in6_ifaddrs list.
1318 in6_purgeaddr(&ia6
->ia_ifa
);
1321 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) == 0) {
1322 in6_ifstat_inc(ia6
->ia_ifa
.ifa_ifp
, ifs6_addr_expiry_cnt
);
1323 in6_event_enqueue_nwk_wq_entry(IN6_NDP_ADDR_EXPIRY
,
1324 ia6
->ia_ifa
.ifa_ifp
, &ia6
->ia_addr
.sin6_addr
,
1327 /* Release extra reference taken above */
1328 IFA_REMREF(&ia6
->ia_ifa
);
1332 * The lazy timer runs every nd6_prune_lazy seconds with at
1333 * most "2 * nd6_prune_lazy - 1" leeway. We consider the worst
1334 * case here and make sure we schedule the regular timer if an
1335 * interface address is about to expire.
1337 if (IFA6_IS_INVALID(ia6
, timenow
+ 3 * nd6_prune_lazy
)) {
1342 IFA_LOCK_ASSERT_HELD(&ia6
->ia_ifa
);
1343 if (IFA6_IS_DEPRECATED(ia6
, timenow
)) {
1344 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
1346 if ((oldflags
& IN6_IFF_DEPRECATED
) == 0) {
1348 * Only enqueue the Deprecated event when the address just
1349 * becomes deprecated.
1350 * Keep it limited to the stable address as it is common for
1351 * older temporary addresses to get deprecated while we generate
1354 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) == 0) {
1355 in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DEPRECATED
,
1356 ia6
->ia_ifa
.ifa_ifp
, &ia6
->ia_addr
.sin6_addr
,
1361 * If a temporary address has just become deprecated,
1362 * regenerate a new one if possible.
1364 if (ip6_use_tempaddr
&&
1365 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1366 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
1367 /* see NOTE above */
1368 IFA_UNLOCK(&ia6
->ia_ifa
);
1369 lck_rw_done(&in6_ifaddr_rwlock
);
1370 if (regen_tmpaddr(ia6
) == 0) {
1372 * A new temporary address is
1374 * XXX: this means the address chain
1375 * has changed while we are still in
1376 * the loop. Although the change
1377 * would not cause disaster (because
1378 * it's not a deletion, but an
1379 * addition,) we'd rather restart the
1380 * loop just for safety. Or does this
1381 * significantly reduce performance??
1383 /* Release extra reference */
1384 IFA_REMREF(&ia6
->ia_ifa
);
1387 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1389 IFA_UNLOCK(&ia6
->ia_ifa
);
1393 * A new RA might have made a deprecated address
1396 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
1397 IFA_UNLOCK(&ia6
->ia_ifa
);
1399 LCK_RW_ASSERT(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1400 /* Release extra reference taken above */
1401 IFA_REMREF(&ia6
->ia_ifa
);
1403 lck_rw_done(&in6_ifaddr_rwlock
);
1405 lck_mtx_lock(nd6_mutex
);
1406 /* expire prefix list */
1407 pr
= nd_prefix
.lh_first
;
1408 while (pr
!= NULL
) {
1411 * check prefix lifetime.
1412 * since pltime is just for autoconf, pltime processing for
1413 * prefix is not necessary.
1416 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_SERVICE
||
1417 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
1418 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1423 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_expire
< timenow
) {
1425 * address expiration and prefix expiration are
1426 * separate. NEVER perform in6_purgeaddr here.
1428 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1429 NDPR_ADDREF_LOCKED(pr
);
1433 in6_ifstat_inc(pr
->ndpr_ifp
, ifs6_pfx_expiry_cnt
);
1434 in6_event_enqueue_nwk_wq_entry(IN6_NDP_PFX_EXPIRY
,
1435 pr
->ndpr_ifp
, &pr
->ndpr_prefix
.sin6_addr
,
1438 pfxlist_onlink_check();
1439 pr
= nd_prefix
.lh_first
;
1442 if (pr
->ndpr_expire
== 0 ||
1443 (pr
->ndpr_stateflags
& NDPRF_STATIC
)) {
1448 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1453 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1455 pr
->ndpr_stateflags
&= ~NDPRF_PROCESSED_SERVICE
;
1458 lck_mtx_unlock(nd6_mutex
);
1460 lck_mtx_lock(rnh_lock
);
1461 /* We're done; let others enter */
1462 nd6_service_busy
= FALSE
;
1463 if (nd6_service_waiters
> 0) {
1464 nd6_service_waiters
= 0;
1465 wakeup(nd6_service_wc
);
1470 static int nd6_need_draining
= 0;
1473 nd6_drain(void *arg
)
1476 nd6log2(debug
, "%s: draining ND6 entries\n", __func__
);
1478 lck_mtx_lock(rnh_lock
);
1479 nd6_need_draining
= 1;
1480 nd6_sched_timeout(NULL
, NULL
);
1481 lck_mtx_unlock(rnh_lock
);
1485 * We use the ``arg'' variable to decide whether or not the timer we're
1486 * running is the fast timer. We do this to reset the nd6_fast_timer_on
1487 * variable so that later we don't end up ignoring a ``fast timer''
1488 * request if the 5 second timer is running (see nd6_sched_timeout).
1491 nd6_timeout(void *arg
)
1493 struct nd6svc_arg sarg
;
1496 lck_mtx_lock(rnh_lock
);
1497 bzero(&sarg
, sizeof(sarg
));
1498 if (nd6_need_draining
!= 0) {
1499 nd6_need_draining
= 0;
1503 nd6log2(debug
, "%s: found %u, aging_lazy %u, aging %u, "
1504 "sticky %u, killed %u\n", __func__
, sarg
.found
, sarg
.aging_lazy
,
1505 sarg
.aging
, sarg
.sticky
, sarg
.killed
);
1506 /* re-arm the timer if there's work to do */
1508 VERIFY(nd6_timeout_run
>= 0 && nd6_timeout_run
< 2);
1509 if (arg
== &nd6_fast_timer_on
) {
1510 nd6_fast_timer_on
= FALSE
;
1512 if (sarg
.aging_lazy
> 0 || sarg
.aging
> 0 || nd6_sched_timeout_want
) {
1513 struct timeval atv
, ltv
, *leeway
;
1514 int lazy
= nd6_prune_lazy
;
1516 if (sarg
.aging
> 0 || lazy
< 1) {
1518 atv
.tv_sec
= nd6_prune
;
1523 atv
.tv_sec
= MAX(nd6_prune
, lazy
);
1525 read_frandom(&buf
, sizeof(buf
));
1526 ltv
.tv_sec
= MAX(buf
% lazy
, 1) * 2;
1529 nd6_sched_timeout(&atv
, leeway
);
1530 } else if (nd6_debug
) {
1531 nd6log2(debug
, "%s: not rescheduling timer\n", __func__
);
1533 lck_mtx_unlock(rnh_lock
);
1537 nd6_sched_timeout(struct timeval
*atv
, struct timeval
*ltv
)
1541 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1544 tv
.tv_sec
= MAX(nd6_prune
, 1);
1546 ltv
= NULL
; /* ignore leeway */
1548 /* see comments on top of this file */
1549 if (nd6_timeout_run
== 0) {
1551 nd6log2(debug
, "%s: timer scheduled in "
1552 "T+%llus.%lluu (demand %d)\n", __func__
,
1553 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1554 nd6_sched_timeout_want
);
1555 nd6_fast_timer_on
= TRUE
;
1556 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1558 nd6log2(debug
, "%s: timer scheduled in "
1559 "T+%llus.%lluu with %llus.%lluu leeway "
1560 "(demand %d)\n", __func__
, (uint64_t)atv
->tv_sec
,
1561 (uint64_t)atv
->tv_usec
, (uint64_t)ltv
->tv_sec
,
1562 (uint64_t)ltv
->tv_usec
, nd6_sched_timeout_want
);
1563 nd6_fast_timer_on
= FALSE
;
1564 timeout_with_leeway(nd6_timeout
, NULL
,
1565 tvtohz(atv
), tvtohz(ltv
));
1568 nd6_sched_timeout_want
= 0;
1569 } else if (nd6_timeout_run
== 1 && ltv
== NULL
&&
1570 nd6_fast_timer_on
== FALSE
) {
1571 nd6log2(debug
, "%s: fast timer scheduled in "
1572 "T+%llus.%lluu (demand %d)\n", __func__
,
1573 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1574 nd6_sched_timeout_want
);
1575 nd6_fast_timer_on
= TRUE
;
1576 nd6_sched_timeout_want
= 0;
1578 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1581 nd6log2(debug
, "%s: not scheduling timer: "
1582 "timers %d, fast_timer %d, T+%llus.%lluu\n",
1583 __func__
, nd6_timeout_run
, nd6_fast_timer_on
,
1584 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
);
1586 nd6log2(debug
, "%s: not scheduling timer: "
1587 "timers %d, fast_timer %d, T+%llus.%lluu "
1588 "with %llus.%lluu leeway\n", __func__
,
1589 nd6_timeout_run
, nd6_fast_timer_on
,
1590 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1591 (uint64_t)ltv
->tv_sec
, (uint64_t)ltv
->tv_usec
);
1597 * ND6 router advertisement kernel notification
1600 nd6_post_msg(u_int32_t code
, struct nd_prefix_list
*prefix_list
,
1601 u_int32_t list_length
, u_int32_t mtu
)
1603 struct kev_msg ev_msg
;
1604 struct kev_nd6_ra_data nd6_ra_msg_data
;
1605 struct nd_prefix_list
*itr
= prefix_list
;
1607 bzero(&ev_msg
, sizeof(struct kev_msg
));
1608 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1609 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1610 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
1611 ev_msg
.event_code
= code
;
1613 bzero(&nd6_ra_msg_data
, sizeof(nd6_ra_msg_data
));
1615 if (mtu
> 0 && mtu
>= IPV6_MMTU
) {
1616 nd6_ra_msg_data
.mtu
= mtu
;
1617 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_MTU
;
1620 if (list_length
> 0 && prefix_list
!= NULL
) {
1621 nd6_ra_msg_data
.list_length
= list_length
;
1622 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_PREFIX
;
1625 while (itr
!= NULL
&& nd6_ra_msg_data
.list_index
< list_length
) {
1626 bcopy(&itr
->pr
.ndpr_prefix
, &nd6_ra_msg_data
.prefix
.prefix
,
1627 sizeof(nd6_ra_msg_data
.prefix
.prefix
));
1628 nd6_ra_msg_data
.prefix
.raflags
= itr
->pr
.ndpr_raf
;
1629 nd6_ra_msg_data
.prefix
.prefixlen
= itr
->pr
.ndpr_plen
;
1630 nd6_ra_msg_data
.prefix
.origin
= PR_ORIG_RA
;
1631 nd6_ra_msg_data
.prefix
.vltime
= itr
->pr
.ndpr_vltime
;
1632 nd6_ra_msg_data
.prefix
.pltime
= itr
->pr
.ndpr_pltime
;
1633 nd6_ra_msg_data
.prefix
.expire
= ndpr_getexpire(&itr
->pr
);
1634 nd6_ra_msg_data
.prefix
.flags
= itr
->pr
.ndpr_stateflags
;
1635 nd6_ra_msg_data
.prefix
.refcnt
= itr
->pr
.ndpr_addrcnt
;
1636 nd6_ra_msg_data
.prefix
.if_index
= itr
->pr
.ndpr_ifp
->if_index
;
1638 /* send the message up */
1639 ev_msg
.dv
[0].data_ptr
= &nd6_ra_msg_data
;
1640 ev_msg
.dv
[0].data_length
= sizeof(nd6_ra_msg_data
);
1641 ev_msg
.dv
[1].data_length
= 0;
1642 dlil_post_complete_msg(NULL
, &ev_msg
);
1644 /* clean up for the next prefix */
1645 bzero(&nd6_ra_msg_data
.prefix
, sizeof(nd6_ra_msg_data
.prefix
));
1647 nd6_ra_msg_data
.list_index
++;
1652 * Regenerate deprecated/invalidated temporary address
1655 regen_tmpaddr(struct in6_ifaddr
*ia6
)
1659 struct in6_ifaddr
*public_ifa6
= NULL
;
1660 uint64_t timenow
= net_uptime();
1662 ifp
= ia6
->ia_ifa
.ifa_ifp
;
1663 ifnet_lock_shared(ifp
);
1664 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
1665 struct in6_ifaddr
*it6
;
1668 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1672 it6
= (struct in6_ifaddr
*)ifa
;
1674 /* ignore no autoconf addresses. */
1675 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
1679 /* ignore autoconf addresses with different prefixes. */
1680 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
) {
1685 * Now we are looking at an autoconf address with the same
1686 * prefix as ours. If the address is temporary and is still
1687 * preferred, do not create another one. It would be rare, but
1688 * could happen, for example, when we resume a laptop PC after
1691 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1692 !IFA6_IS_DEPRECATED(it6
, timenow
)) {
1694 if (public_ifa6
!= NULL
) {
1695 IFA_REMREF(&public_ifa6
->ia_ifa
);
1702 * This is a public autoconf address that has the same prefix
1703 * as ours. If it is preferred, keep it. We can't break the
1704 * loop here, because there may be a still-preferred temporary
1705 * address with the prefix.
1707 if (!IFA6_IS_DEPRECATED(it6
, timenow
)) {
1708 IFA_ADDREF_LOCKED(ifa
); /* for public_ifa6 */
1710 if (public_ifa6
!= NULL
) {
1711 IFA_REMREF(&public_ifa6
->ia_ifa
);
1718 ifnet_lock_done(ifp
);
1720 if (public_ifa6
!= NULL
) {
1723 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
1724 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
1725 " tmp addr,errno=%d\n", e
);
1726 IFA_REMREF(&public_ifa6
->ia_ifa
);
1729 IFA_REMREF(&public_ifa6
->ia_ifa
);
1737 * Nuke neighbor cache/prefix/default router management table, right before
1741 nd6_purge(struct ifnet
*ifp
)
1743 struct llinfo_nd6
*ln
;
1744 struct nd_defrouter
*dr
, *ndr
;
1745 struct nd_prefix
*pr
, *npr
;
1747 struct nd_drhead nd_defrouter_tmp
;
1749 TAILQ_INIT(&nd_defrouter_tmp
);
1751 /* Nuke default router list entries toward ifp */
1752 lck_mtx_lock(nd6_mutex
);
1753 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter
, dr_entry
, ndr
) {
1754 if (dr
->ifp
!= ifp
) {
1758 * Remove the entry from default router list
1759 * and add it to the temp list.
1760 * nd_defrouter_tmp will be a local temporary
1761 * list as no one else can get the same
1762 * removed entry once it is removed from default
1764 * Remove the reference after calling defrtrlist_del.
1766 * The uninstalled entries have to be iterated first
1767 * when we call defrtrlist_del.
1768 * This is to ensure that we don't end up calling
1769 * default router selection when there are other
1770 * uninstalled candidate default routers on
1772 * If we don't respect that order, we may end
1773 * up missing out on some entries.
1775 * For that reason, installed ones must be inserted
1776 * at the tail and uninstalled ones at the head
1778 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1780 if (dr
->stateflags
& NDDRF_INSTALLED
) {
1781 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
1783 TAILQ_INSERT_HEAD(&nd_defrouter_tmp
, dr
, dr_entry
);
1788 * The following call to defrtrlist_del should be
1789 * safe as we are iterating a local list of
1792 * We don't really need nd6_mutex here but keeping
1793 * it as it is to avoid changing assertios held in
1794 * the functions in the call-path.
1796 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, ndr
) {
1797 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
1799 NDDR_REMREF(dr
); /* remove list reference */
1802 /* Nuke prefix list entries toward ifp */
1804 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
1806 npr
= pr
->ndpr_next
;
1807 if (pr
->ndpr_ifp
== ifp
&&
1808 !(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)) {
1810 * Because if_detach() does *not* release prefixes
1811 * while purging addresses the reference count will
1812 * still be above zero. We therefore reset it to
1813 * make sure that the prefix really gets purged.
1815 pr
->ndpr_addrcnt
= 0;
1818 * Previously, pr->ndpr_addr is removed as well,
1819 * but I strongly believe we don't have to do it.
1820 * nd6_purge() is only called from in6_ifdetach(),
1821 * which removes all the associated interface addresses
1823 * (jinmei@kame.net 20010129)
1825 NDPR_ADDREF_LOCKED(pr
);
1830 npr
= nd_prefix
.lh_first
;
1836 pfxlist_onlink_check();
1838 lck_mtx_unlock(nd6_mutex
);
1840 /* cancel default outgoing interface setting */
1841 if (nd6_defifindex
== ifp
->if_index
) {
1842 nd6_setdefaultiface(0);
1846 * Perform default router selection even when we are a router,
1847 * if Scoped Routing is enabled.
1849 lck_mtx_lock(nd6_mutex
);
1850 /* refresh default router list */
1851 defrouter_select(ifp
);
1852 lck_mtx_unlock(nd6_mutex
);
1855 * Nuke neighbor cache entries for the ifp.
1856 * Note that rt->rt_ifp may not be the same as ifp,
1857 * due to KAME goto ours hack. See RTM_RESOLVE case in
1858 * nd6_rtrequest(), and ip6_input().
1861 lck_mtx_lock(rnh_lock
);
1862 ln
= llinfo_nd6
.ln_next
;
1863 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1865 struct llinfo_nd6
*nln
;
1870 if (rt
->rt_gateway
!= NULL
&&
1871 rt
->rt_gateway
->sa_family
== AF_LINK
&&
1872 SDL(rt
->rt_gateway
)->sdl_index
== ifp
->if_index
) {
1873 RT_ADDREF_LOCKED(rt
);
1875 lck_mtx_unlock(rnh_lock
);
1877 * See comments on nd6_service() for reasons why
1878 * this loop is repeated; we bite the costs of
1879 * going thru the same llinfo_nd6 more than once
1880 * here, since this purge happens during detach,
1881 * and that unlike the timer case, it's possible
1882 * there's more than one purges happening at the
1883 * same time (thus a flag wouldn't buy anything).
1887 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1894 lck_mtx_unlock(rnh_lock
);
1898 * Upon success, the returned route will be locked and the caller is
1899 * responsible for releasing the reference and doing RT_UNLOCK(rt).
1900 * This routine does not require rnh_lock to be held by the caller,
1901 * although it needs to be indicated of such a case in order to call
1902 * the correct variant of the relevant routing routines.
1905 nd6_lookup(struct in6_addr
*addr6
, int create
, struct ifnet
*ifp
, int rt_locked
)
1908 struct sockaddr_in6 sin6
;
1909 unsigned int ifscope
;
1911 bzero(&sin6
, sizeof(sin6
));
1912 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1913 sin6
.sin6_family
= AF_INET6
;
1914 sin6
.sin6_addr
= *addr6
;
1916 ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
1918 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1919 rt
= rtalloc1_scoped_locked(SA(&sin6
), create
, 0, ifscope
);
1921 rt
= rtalloc1_scoped(SA(&sin6
), create
, 0, ifscope
);
1926 if ((rt
->rt_flags
& RTF_LLINFO
) == 0) {
1928 * This is the case for the default route.
1929 * If we want to create a neighbor cache for the
1930 * address, we should free the route for the
1931 * destination and allocate an interface route.
1945 if (create
&& ifp
) {
1947 u_int32_t ifa_flags
;
1951 * If no route is available and create is set,
1952 * we allocate a host route for the destination
1953 * and treat it like an interface route.
1954 * This hack is necessary for a neighbor which can't
1955 * be covered by our own prefix.
1957 ifa
= ifaof_ifpforaddr(SA(&sin6
), ifp
);
1963 * Create a new route. RTF_LLINFO is necessary
1964 * to create a Neighbor Cache entry for the
1965 * destination in nd6_rtrequest which will be
1966 * called in rtrequest via ifa->ifa_rtrequest.
1969 lck_mtx_lock(rnh_lock
);
1972 ifa_flags
= ifa
->ifa_flags
;
1974 if ((e
= rtrequest_scoped_locked(RTM_ADD
,
1975 SA(&sin6
), ifa
->ifa_addr
, SA(&all1_sa
),
1976 (ifa_flags
| RTF_HOST
| RTF_LLINFO
) &
1977 ~RTF_CLONING
, &rt
, ifscope
)) != 0) {
1979 log(LOG_ERR
, "%s: failed to add route "
1980 "for a neighbor(%s), errno=%d\n",
1981 __func__
, ip6_sprintf(addr6
), e
);
1985 lck_mtx_unlock(rnh_lock
);
1993 if (rt
->rt_llinfo
) {
1994 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1995 struct nd_ifinfo
*ndi
= ND_IFINFO(rt
->rt_ifp
);
1997 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1999 * For interface's that do not perform NUD
2000 * neighbor cache entres must always be marked
2001 * reachable with no expiry
2003 if (ndi
->flags
& ND6_IFF_PERFORMNUD
) {
2004 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_NOSTATE
);
2006 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2007 ln_setexpire(ln
, 0);
2014 RT_LOCK_ASSERT_HELD(rt
);
2016 * Validation for the entry.
2017 * Note that the check for rt_llinfo is necessary because a cloned
2018 * route from a parent route that has the L flag (e.g. the default
2019 * route to a p2p interface) may have the flag, too, while the
2020 * destination is not actually a neighbor.
2021 * XXX: we can't use rt->rt_ifp to check for the interface, since
2022 * it might be the loopback interface if the entry is for our
2023 * own address on a non-loopback interface. Instead, we should
2024 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
2026 * Note also that ifa_ifp and ifp may differ when we connect two
2027 * interfaces to a same link, install a link prefix to an interface,
2028 * and try to install a neighbor cache on an interface that does not
2029 * have a route to the prefix.
2031 * If the address is from a proxied prefix, the ifa_ifp and ifp might
2032 * not match, because nd6_na_input() could have modified the ifp
2033 * of the route to point to the interface where the NA arrived on,
2034 * hence the test for RTF_PROXY.
2036 if ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
2037 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
2038 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
&&
2039 !(rt
->rt_flags
& RTF_PROXY
))) {
2040 RT_REMREF_LOCKED(rt
);
2043 log(LOG_DEBUG
, "%s: failed to lookup %s "
2044 "(if = %s)\n", __func__
, ip6_sprintf(addr6
),
2045 ifp
? if_name(ifp
) : "unspec");
2046 /* xxx more logs... kazu */
2051 * Caller needs to release reference and call RT_UNLOCK(rt).
2057 * Test whether a given IPv6 address is a neighbor or not, ignoring
2058 * the actual neighbor cache. The neighbor cache is ignored in order
2059 * to not reenter the routing code from within itself.
2062 nd6_is_new_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
)
2064 struct nd_prefix
*pr
;
2065 struct ifaddr
*dstaddr
;
2067 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2070 * A link-local address is always a neighbor.
2071 * XXX: a link does not necessarily specify a single interface.
2073 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
)) {
2074 struct sockaddr_in6 sin6_copy
;
2078 * We need sin6_copy since sa6_recoverscope() may modify the
2082 if (sa6_recoverscope(&sin6_copy
, FALSE
)) {
2083 return 0; /* XXX: should be impossible */
2085 if (in6_setscope(&sin6_copy
.sin6_addr
, ifp
, &zone
)) {
2088 if (sin6_copy
.sin6_scope_id
== zone
) {
2096 * If the address matches one of our addresses,
2097 * it should be a neighbor.
2098 * If the address matches one of our on-link prefixes, it should be a
2101 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2103 if (pr
->ndpr_ifp
!= ifp
) {
2107 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
2111 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr
->ndpr_prefix
.sin6_addr
,
2112 &addr
->sin6_addr
, &pr
->ndpr_mask
)) {
2120 * If the address is assigned on the node of the other side of
2121 * a p2p interface, the address should be a neighbor.
2123 dstaddr
= ifa_ifwithdstaddr(SA(addr
));
2124 if (dstaddr
!= NULL
) {
2125 if (dstaddr
->ifa_ifp
== ifp
) {
2126 IFA_REMREF(dstaddr
);
2129 IFA_REMREF(dstaddr
);
2138 * Detect if a given IPv6 address identifies a neighbor on a given link.
2139 * XXX: should take care of the destination of a p2p link?
2142 nd6_is_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
,
2147 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2148 lck_mtx_lock(nd6_mutex
);
2149 if (nd6_is_new_addr_neighbor(addr
, ifp
)) {
2150 lck_mtx_unlock(nd6_mutex
);
2153 lck_mtx_unlock(nd6_mutex
);
2156 * Even if the address matches none of our addresses, it might be
2157 * in the neighbor cache.
2159 if ((rt
= nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
)) != NULL
) {
2160 RT_LOCK_ASSERT_HELD(rt
);
2161 RT_REMREF_LOCKED(rt
);
2170 * Free an nd6 llinfo entry.
2171 * Since the function would cause significant changes in the kernel, DO NOT
2172 * make it global, unless you have a strong reason for the change, and are sure
2173 * that the change is safe.
2176 nd6_free(struct rtentry
*rt
)
2178 struct llinfo_nd6
*ln
= NULL
;
2179 struct in6_addr in6
= {};
2180 struct nd_defrouter
*dr
= NULL
;
2182 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2183 RT_LOCK_ASSERT_NOTHELD(rt
);
2184 lck_mtx_lock(nd6_mutex
);
2187 RT_ADDREF_LOCKED(rt
); /* Extra ref */
2189 in6
= SIN6(rt_key(rt
))->sin6_addr
;
2192 * Prevent another thread from modifying rt_key, rt_gateway
2193 * via rt_setgate() after the rt_lock is dropped by marking
2194 * the route as defunct.
2196 rt
->rt_flags
|= RTF_CONDEMNED
;
2199 * We used to have pfctlinput(PRC_HOSTDEAD) here. Even though it is
2200 * not harmful, it was not really necessary. Perform default router
2201 * selection even when we are a router, if Scoped Routing is enabled.
2203 dr
= defrouter_lookup(&SIN6(rt_key(rt
))->sin6_addr
, rt
->rt_ifp
);
2205 if ((ln
&& ln
->ln_router
) || dr
) {
2207 * rt6_flush must be called whether or not the neighbor
2208 * is in the Default Router List.
2209 * See a corresponding comment in nd6_na_input().
2212 lck_mtx_unlock(nd6_mutex
);
2213 rt6_flush(&in6
, rt
->rt_ifp
);
2214 lck_mtx_lock(nd6_mutex
);
2222 * Unreachablity of a router might affect the default
2223 * router selection and on-link detection of advertised
2228 * Temporarily fake the state to choose a new default
2229 * router and to perform on-link determination of
2230 * prefixes correctly.
2231 * Below the state will be set correctly,
2232 * or the entry itself will be deleted.
2235 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_INCOMPLETE
);
2238 * Since defrouter_select() does not affect the
2239 * on-link determination and MIP6 needs the check
2240 * before the default router selection, we perform
2244 pfxlist_onlink_check();
2247 * refresh default router list
2249 defrouter_select(rt
->rt_ifp
);
2251 RT_LOCK_ASSERT_NOTHELD(rt
);
2252 lck_mtx_unlock(nd6_mutex
);
2254 * Detach the route from the routing tree and the list of neighbor
2255 * caches, and disable the route entry not to be used in already
2258 (void) rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
), 0, NULL
);
2260 /* Extra ref held above; now free it */
2265 nd6_rtrequest(int req
, struct rtentry
*rt
, struct sockaddr
*sa
)
2268 struct sockaddr
*gate
= rt
->rt_gateway
;
2269 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
2270 static struct sockaddr_dl null_sdl
=
2271 { .sdl_len
= sizeof(null_sdl
), .sdl_family
= AF_LINK
};
2272 struct ifnet
*ifp
= rt
->rt_ifp
;
2275 char buf
[MAX_IPv6_STR_LEN
];
2276 struct nd_ifinfo
*ndi
= ND_IFINFO(rt
->rt_ifp
);
2278 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2279 VERIFY(nd6_init_done
);
2280 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2281 RT_LOCK_ASSERT_HELD(rt
);
2284 * We have rnh_lock held, see if we need to schedule the timer;
2285 * we might do this again below during RTM_RESOLVE, but doing it
2286 * now handles all other cases.
2288 if (nd6_sched_timeout_want
) {
2289 nd6_sched_timeout(NULL
, NULL
);
2292 if (rt
->rt_flags
& RTF_GATEWAY
) {
2296 if (!nd6_need_cache(ifp
) && !(rt
->rt_flags
& RTF_HOST
)) {
2298 * This is probably an interface direct route for a link
2299 * which does not need neighbor caches (e.g. fe80::%lo0/64).
2300 * We do not need special treatment below for such a route.
2301 * Moreover, the RTF_LLINFO flag which would be set below
2302 * would annoy the ndp(8) command.
2307 if (req
== RTM_RESOLVE
) {
2310 if (!nd6_need_cache(ifp
)) { /* stf case */
2313 struct sockaddr_in6 sin6
;
2315 rtkey_to_sa6(rt
, &sin6
);
2317 * nd6_is_addr_neighbor() may call nd6_lookup(),
2318 * therefore we drop rt_lock to avoid deadlock
2319 * during the lookup.
2321 RT_ADDREF_LOCKED(rt
);
2323 no_nd_cache
= !nd6_is_addr_neighbor(&sin6
, ifp
, 1);
2325 RT_REMREF_LOCKED(rt
);
2329 * FreeBSD and BSD/OS often make a cloned host route based
2330 * on a less-specific route (e.g. the default route).
2331 * If the less specific route does not have a "gateway"
2332 * (this is the case when the route just goes to a p2p or an
2333 * stf interface), we'll mistakenly make a neighbor cache for
2334 * the host route, and will see strange neighbor solicitation
2335 * for the corresponding destination. In order to avoid the
2336 * confusion, we check if the destination of the route is
2337 * a neighbor in terms of neighbor discovery, and stop the
2338 * process if not. Additionally, we remove the LLINFO flag
2339 * so that ndp(8) will not try to get the neighbor information
2340 * of the destination.
2343 rt
->rt_flags
&= ~RTF_LLINFO
;
2348 timenow
= net_uptime();
2353 * There is no backward compatibility :)
2355 * if ((rt->rt_flags & RTF_HOST) == 0 &&
2356 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
2357 * rt->rt_flags |= RTF_CLONING;
2359 if ((rt
->rt_flags
& RTF_CLONING
) ||
2360 ((rt
->rt_flags
& RTF_LLINFO
) && ln
== NULL
)) {
2362 * Case 1: This route should come from a route to
2363 * interface (RTF_CLONING case) or the route should be
2364 * treated as on-link but is currently not
2365 * (RTF_LLINFO && ln == NULL case).
2367 if (rt_setgate(rt
, rt_key(rt
), SA(&null_sdl
)) == 0) {
2368 gate
= rt
->rt_gateway
;
2369 SDL(gate
)->sdl_type
= ifp
->if_type
;
2370 SDL(gate
)->sdl_index
= ifp
->if_index
;
2372 * In case we're called before 1.0 sec.
2377 (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
)
2378 ? 0 : MAX(timenow
, 1));
2381 if (rt
->rt_flags
& RTF_CLONING
) {
2386 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
2387 * We don't do that here since llinfo is not ready yet.
2389 * There are also couple of other things to be discussed:
2390 * - unsolicited NA code needs improvement beforehand
2391 * - RFC4861 says we MAY send multicast unsolicited NA
2392 * (7.2.6 paragraph 4), however, it also says that we
2393 * SHOULD provide a mechanism to prevent multicast NA storm.
2394 * we don't have anything like it right now.
2395 * note that the mechanism needs a mutual agreement
2396 * between proxies, which means that we need to implement
2397 * a new protocol, or a new kludge.
2398 * - from RFC4861 6.2.4, host MUST NOT send an unsolicited RA.
2399 * we need to check ip6forwarding before sending it.
2400 * (or should we allow proxy ND configuration only for
2401 * routers? there's no mention about proxy ND from hosts)
2405 if (!(ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))) {
2407 * Address resolution isn't necessary for a point to
2408 * point link, so we can skip this test for a p2p link.
2410 if (gate
->sa_family
!= AF_LINK
||
2411 gate
->sa_len
< sizeof(null_sdl
)) {
2412 /* Don't complain in case of RTM_ADD */
2413 if (req
== RTM_RESOLVE
) {
2414 log(LOG_ERR
, "%s: route to %s has bad "
2415 "gateway address (sa_family %u "
2416 "sa_len %u) on %s\n", __func__
,
2418 &SIN6(rt_key(rt
))->sin6_addr
, buf
,
2419 sizeof(buf
)), gate
->sa_family
,
2420 gate
->sa_len
, if_name(ifp
));
2424 SDL(gate
)->sdl_type
= ifp
->if_type
;
2425 SDL(gate
)->sdl_index
= ifp
->if_index
;
2428 break; /* This happens on a route change */
2431 * Case 2: This route may come from cloning, or a manual route
2432 * add with a LL address.
2434 rt
->rt_llinfo
= ln
= nd6_llinfo_alloc(M_WAITOK
);
2440 rt
->rt_llinfo_get_ri
= nd6_llinfo_get_ri
;
2441 rt
->rt_llinfo_get_iflri
= nd6_llinfo_get_iflri
;
2442 rt
->rt_llinfo_purge
= nd6_llinfo_purge
;
2443 rt
->rt_llinfo_free
= nd6_llinfo_free
;
2444 rt
->rt_llinfo_refresh
= nd6_llinfo_refresh
;
2445 rt
->rt_flags
|= RTF_LLINFO
;
2447 /* this is required for "ndp" command. - shin */
2449 * For interface's that do not perform NUD
2450 * neighbor cache entries must always be marked
2451 * reachable with no expiry
2453 if ((req
== RTM_ADD
) ||
2454 !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
2456 * gate should have some valid AF_LINK entry,
2457 * and ln->ln_expire should have some lifetime
2458 * which is specified by ndp command.
2460 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2461 ln_setexpire(ln
, 0);
2464 * When req == RTM_RESOLVE, rt is created and
2465 * initialized in rtrequest(), so rt_expire is 0.
2467 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_NOSTATE
);
2468 /* In case we're called before 1.0 sec. has elapsed */
2469 ln_setexpire(ln
, (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
) ?
2470 0 : MAX(timenow
, 1));
2475 /* We have at least one entry; arm the timer if not already */
2476 nd6_sched_timeout(NULL
, NULL
);
2479 * If we have too many cache entries, initiate immediate
2480 * purging for some "less recently used" entries. Note that
2481 * we cannot directly call nd6_free() here because it would
2482 * cause re-entering rtable related routines triggering an LOR
2485 if (ip6_neighborgcthresh
> 0 &&
2486 nd6_inuse
>= ip6_neighborgcthresh
) {
2489 for (i
= 0; i
< 10 && llinfo_nd6
.ln_prev
!= ln
; i
++) {
2490 struct llinfo_nd6
*ln_end
= llinfo_nd6
.ln_prev
;
2491 struct rtentry
*rt_end
= ln_end
->ln_rt
;
2493 /* Move this entry to the head */
2496 LN_INSERTHEAD(ln_end
);
2498 if (ln_end
->ln_expire
== 0) {
2502 if (ln_end
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
2503 ND6_CACHE_STATE_TRANSITION(ln_end
, ND6_LLINFO_STALE
);
2505 ND6_CACHE_STATE_TRANSITION(ln_end
, ND6_LLINFO_PURGE
);
2507 ln_setexpire(ln_end
, timenow
);
2513 * check if rt_key(rt) is one of my address assigned
2516 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
2517 &SIN6(rt_key(rt
))->sin6_addr
);
2519 caddr_t macp
= nd6_ifptomac(ifp
);
2520 ln_setexpire(ln
, 0);
2521 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2523 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
2524 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
2526 if (nd6_useloopback
) {
2527 if (rt
->rt_ifp
!= lo_ifp
) {
2529 * Purge any link-layer info caching.
2531 if (rt
->rt_llinfo_purge
!= NULL
) {
2532 rt
->rt_llinfo_purge(rt
);
2536 * Adjust route ref count for the
2539 if (rt
->rt_if_ref_fn
!= NULL
) {
2540 rt
->rt_if_ref_fn(lo_ifp
, 1);
2541 rt
->rt_if_ref_fn(rt
->rt_ifp
,
2545 rt
->rt_ifp
= lo_ifp
;
2547 * If rmx_mtu is not locked, update it
2548 * to the MTU used by the new interface.
2550 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
2551 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
2554 * Make sure rt_ifa be equal to the ifaddr
2555 * corresponding to the address.
2556 * We need this because when we refer
2557 * rt_ifa->ia6_flags in ip6_input, we assume
2558 * that the rt_ifa points to the address instead
2559 * of the loopback address.
2561 if (ifa
!= rt
->rt_ifa
) {
2566 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
2567 ln_setexpire(ln
, 0);
2568 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2570 /* join solicited node multicast for proxy ND */
2571 if (ifp
->if_flags
& IFF_MULTICAST
) {
2572 struct in6_addr llsol
;
2573 struct in6_multi
*in6m
;
2576 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2577 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2578 llsol
.s6_addr32
[1] = 0;
2579 llsol
.s6_addr32
[2] = htonl(1);
2580 llsol
.s6_addr8
[12] = 0xff;
2581 if (in6_setscope(&llsol
, ifp
, NULL
)) {
2584 error
= in6_mc_join(ifp
, &llsol
,
2587 nd6log(error
, "%s: failed to join "
2588 "%s (errno=%d)\n", if_name(ifp
),
2589 ip6_sprintf(&llsol
), error
);
2601 /* leave from solicited node multicast for proxy ND */
2602 if ((rt
->rt_flags
& RTF_ANNOUNCE
) &&
2603 (ifp
->if_flags
& IFF_MULTICAST
)) {
2604 struct in6_addr llsol
;
2605 struct in6_multi
*in6m
;
2607 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2608 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2609 llsol
.s6_addr32
[1] = 0;
2610 llsol
.s6_addr32
[2] = htonl(1);
2611 llsol
.s6_addr8
[12] = 0xff;
2612 if (in6_setscope(&llsol
, ifp
, NULL
) == 0) {
2613 in6_multihead_lock_shared();
2614 IN6_LOOKUP_MULTI(&llsol
, ifp
, in6m
);
2615 in6_multihead_lock_done();
2617 in6_mc_leave(in6m
, NULL
);
2624 * Unchain it but defer the actual freeing until the route
2625 * itself is to be freed. rt->rt_llinfo still points to
2626 * llinfo_nd6, and likewise, ln->ln_rt stil points to this
2627 * route entry, except that RTF_LLINFO is now cleared.
2629 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
2634 * Purge any link-layer info caching.
2636 if (rt
->rt_llinfo_purge
!= NULL
) {
2637 rt
->rt_llinfo_purge(rt
);
2640 rt
->rt_flags
&= ~RTF_LLINFO
;
2641 if (ln
->ln_hold
!= NULL
) {
2642 m_freem_list(ln
->ln_hold
);
2649 nd6_siocgdrlst(void *data
, int data_is_64
)
2651 struct in6_drlist_32
*drl_32
;
2652 struct nd_defrouter
*dr
;
2655 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2657 dr
= TAILQ_FIRST(&nd_defrouter
);
2659 /* XXX Handle mapped defrouter entries */
2660 /* For 64-bit process */
2662 struct in6_drlist_64
*drl_64
;
2664 drl_64
= _MALLOC(sizeof(*drl_64
), M_TEMP
, M_WAITOK
| M_ZERO
);
2665 if (drl_64
== NULL
) {
2669 /* preserve the interface name */
2670 bcopy(data
, drl_64
, sizeof(drl_64
->ifname
));
2672 while (dr
&& i
< DRLSTSIZ
) {
2673 drl_64
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2674 if (IN6_IS_ADDR_LINKLOCAL(
2675 &drl_64
->defrouter
[i
].rtaddr
)) {
2676 /* XXX: need to this hack for KAME stack */
2677 drl_64
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2680 "default router list contains a "
2681 "non-linklocal address(%s)\n",
2682 ip6_sprintf(&drl_64
->defrouter
[i
].rtaddr
));
2684 drl_64
->defrouter
[i
].flags
= dr
->flags
;
2685 drl_64
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2686 drl_64
->defrouter
[i
].expire
= nddr_getexpire(dr
);
2687 drl_64
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2689 dr
= TAILQ_NEXT(dr
, dr_entry
);
2691 bcopy(drl_64
, data
, sizeof(*drl_64
));
2692 _FREE(drl_64
, M_TEMP
);
2696 /* For 32-bit process */
2697 drl_32
= _MALLOC(sizeof(*drl_32
), M_TEMP
, M_WAITOK
| M_ZERO
);
2698 if (drl_32
== NULL
) {
2702 /* preserve the interface name */
2703 bcopy(data
, drl_32
, sizeof(drl_32
->ifname
));
2705 while (dr
!= NULL
&& i
< DRLSTSIZ
) {
2706 drl_32
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2707 if (IN6_IS_ADDR_LINKLOCAL(&drl_32
->defrouter
[i
].rtaddr
)) {
2708 /* XXX: need to this hack for KAME stack */
2709 drl_32
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2712 "default router list contains a "
2713 "non-linklocal address(%s)\n",
2714 ip6_sprintf(&drl_32
->defrouter
[i
].rtaddr
));
2716 drl_32
->defrouter
[i
].flags
= dr
->flags
;
2717 drl_32
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2718 drl_32
->defrouter
[i
].expire
= nddr_getexpire(dr
);
2719 drl_32
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2721 dr
= TAILQ_NEXT(dr
, dr_entry
);
2723 bcopy(drl_32
, data
, sizeof(*drl_32
));
2724 _FREE(drl_32
, M_TEMP
);
2729 * XXX meaning of fields, especialy "raflags", is very
2730 * differnet between RA prefix list and RR/static prefix list.
2731 * how about separating ioctls into two?
2734 nd6_siocgprlst(void *data
, int data_is_64
)
2736 struct in6_prlist_32
*prl_32
;
2737 struct nd_prefix
*pr
;
2740 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2742 pr
= nd_prefix
.lh_first
;
2744 /* XXX Handle mapped defrouter entries */
2745 /* For 64-bit process */
2747 struct in6_prlist_64
*prl_64
;
2749 prl_64
= _MALLOC(sizeof(*prl_64
), M_TEMP
, M_WAITOK
| M_ZERO
);
2750 if (prl_64
== NULL
) {
2754 /* preserve the interface name */
2755 bcopy(data
, prl_64
, sizeof(prl_64
->ifname
));
2757 while (pr
&& i
< PRLSTSIZ
) {
2758 struct nd_pfxrouter
*pfr
;
2762 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
2763 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2764 prl_64
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2765 prl_64
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2766 prl_64
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2767 prl_64
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2768 prl_64
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2769 prl_64
->prefix
[i
].expire
= ndpr_getexpire(pr
);
2771 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2775 #define RTRADDR prl_64->prefix[i].advrtr[j]
2776 RTRADDR
= pfr
->router
->rtaddr
;
2777 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2778 /* XXX: hack for KAME */
2779 RTRADDR
.s6_addr16
[1] = 0;
2782 "a router(%s) advertises "
2784 "non-link local address\n",
2785 ip6_sprintf(&RTRADDR
));
2790 pfr
= pfr
->pfr_next
;
2792 prl_64
->prefix
[i
].advrtrs
= j
;
2793 prl_64
->prefix
[i
].origin
= PR_ORIG_RA
;
2799 bcopy(prl_64
, data
, sizeof(*prl_64
));
2800 _FREE(prl_64
, M_TEMP
);
2804 /* For 32-bit process */
2805 prl_32
= _MALLOC(sizeof(*prl_32
), M_TEMP
, M_WAITOK
| M_ZERO
);
2806 if (prl_32
== NULL
) {
2810 /* preserve the interface name */
2811 bcopy(data
, prl_32
, sizeof(prl_32
->ifname
));
2813 while (pr
&& i
< PRLSTSIZ
) {
2814 struct nd_pfxrouter
*pfr
;
2818 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
2819 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2820 prl_32
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2821 prl_32
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2822 prl_32
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2823 prl_32
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2824 prl_32
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2825 prl_32
->prefix
[i
].expire
= ndpr_getexpire(pr
);
2827 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2831 #define RTRADDR prl_32->prefix[i].advrtr[j]
2832 RTRADDR
= pfr
->router
->rtaddr
;
2833 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2834 /* XXX: hack for KAME */
2835 RTRADDR
.s6_addr16
[1] = 0;
2838 "a router(%s) advertises "
2840 "non-link local address\n",
2841 ip6_sprintf(&RTRADDR
));
2846 pfr
= pfr
->pfr_next
;
2848 prl_32
->prefix
[i
].advrtrs
= j
;
2849 prl_32
->prefix
[i
].origin
= PR_ORIG_RA
;
2855 bcopy(prl_32
, data
, sizeof(*prl_32
));
2856 _FREE(prl_32
, M_TEMP
);
2861 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
2863 struct nd_defrouter
*dr
;
2864 struct nd_prefix
*pr
;
2868 VERIFY(ifp
!= NULL
);
2871 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
2872 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
2874 * obsolete API, use sysctl under net.inet6.icmp6
2876 lck_mtx_lock(nd6_mutex
);
2877 error
= nd6_siocgdrlst(data
, cmd
== SIOCGDRLST_IN6_64
);
2878 lck_mtx_unlock(nd6_mutex
);
2881 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
2882 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
2884 * obsolete API, use sysctl under net.inet6.icmp6
2886 lck_mtx_lock(nd6_mutex
);
2887 error
= nd6_siocgprlst(data
, cmd
== SIOCGPRLST_IN6_64
);
2888 lck_mtx_unlock(nd6_mutex
);
2891 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
2892 case SIOCGIFINFO_IN6
: { /* struct in6_ondireq */
2894 struct in6_ondireq
*ondi
= (struct in6_ondireq
*)(void *)data
;
2895 struct nd_ifinfo
*ndi
;
2897 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
2898 * instead of in6_ndireq, so we treat it as such.
2900 ndi
= ND_IFINFO(ifp
);
2901 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)) {
2905 lck_mtx_lock(&ndi
->lock
);
2906 linkmtu
= IN6_LINKMTU(ifp
);
2907 bcopy(&linkmtu
, &ondi
->ndi
.linkmtu
, sizeof(linkmtu
));
2908 bcopy(&ndi
->maxmtu
, &ondi
->ndi
.maxmtu
,
2910 bcopy(&ndi
->basereachable
, &ondi
->ndi
.basereachable
,
2912 bcopy(&ndi
->reachable
, &ondi
->ndi
.reachable
,
2914 bcopy(&ndi
->retrans
, &ondi
->ndi
.retrans
,
2916 bcopy(&ndi
->flags
, &ondi
->ndi
.flags
,
2918 bcopy(&ndi
->recalctm
, &ondi
->ndi
.recalctm
,
2920 ondi
->ndi
.chlim
= ndi
->chlim
;
2921 ondi
->ndi
.receivedra
= 0;
2922 lck_mtx_unlock(&ndi
->lock
);
2926 case SIOCSIFINFO_FLAGS
: { /* struct in6_ndireq */
2928 * XXX BSD has a bunch of checks here to ensure
2929 * that interface disabled flag is not reset if
2930 * link local address has failed DAD.
2931 * Investigate that part.
2933 struct in6_ndireq
*cndi
= (struct in6_ndireq
*)(void *)data
;
2934 u_int32_t oflags
, flags
;
2935 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
2937 /* XXX: almost all other fields of cndi->ndi is unused */
2938 if ((NULL
== ndi
) || !ndi
->initialized
) {
2943 lck_mtx_lock(&ndi
->lock
);
2944 oflags
= ndi
->flags
;
2945 bcopy(&cndi
->ndi
.flags
, &(ndi
->flags
), sizeof(flags
));
2947 lck_mtx_unlock(&ndi
->lock
);
2949 if (oflags
== flags
) {
2953 error
= nd6_setifinfo(ifp
, oflags
, flags
);
2957 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
2958 /* flush default router list */
2960 * xxx sumikawa: should not delete route if default
2961 * route equals to the top of default router list
2963 lck_mtx_lock(nd6_mutex
);
2965 defrouter_select(ifp
);
2966 lck_mtx_unlock(nd6_mutex
);
2967 /* xxx sumikawa: flush prefix list */
2970 case SIOCSPFXFLUSH_IN6
: { /* struct in6_ifreq */
2971 /* flush all the prefix advertised by routers */
2972 struct nd_prefix
*next
= NULL
;
2974 lck_mtx_lock(nd6_mutex
);
2975 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
2976 struct in6_ifaddr
*ia
= NULL
;
2977 bool iterate_pfxlist_again
= false;
2979 next
= pr
->ndpr_next
;
2982 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
2986 if (ifp
!= lo_ifp
&& pr
->ndpr_ifp
!= ifp
) {
2990 /* do we really have to remove addresses as well? */
2991 NDPR_ADDREF_LOCKED(pr
);
2993 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2995 while (ia
!= NULL
) {
2996 IFA_LOCK(&ia
->ia_ifa
);
2997 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
2998 IFA_UNLOCK(&ia
->ia_ifa
);
3003 if (ia
->ia6_ndpr
== pr
) {
3004 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
3005 IFA_UNLOCK(&ia
->ia_ifa
);
3006 lck_rw_done(&in6_ifaddr_rwlock
);
3007 lck_mtx_unlock(nd6_mutex
);
3008 in6_purgeaddr(&ia
->ia_ifa
);
3009 IFA_REMREF(&ia
->ia_ifa
);
3010 lck_mtx_lock(nd6_mutex
);
3011 lck_rw_lock_exclusive(
3012 &in6_ifaddr_rwlock
);
3014 * Purging the address caused
3015 * in6_ifaddr_rwlock to be
3017 * reacquired; therefore search again
3018 * from the beginning of in6_ifaddrs.
3019 * The same applies for the prefix list.
3022 iterate_pfxlist_again
= true;
3025 IFA_UNLOCK(&ia
->ia_ifa
);
3028 lck_rw_done(&in6_ifaddr_rwlock
);
3032 pfxlist_onlink_check();
3034 if (iterate_pfxlist_again
) {
3035 next
= nd_prefix
.lh_first
;
3038 lck_mtx_unlock(nd6_mutex
);
3042 case SIOCSRTRFLUSH_IN6
: { /* struct in6_ifreq */
3043 /* flush all the default routers */
3044 struct nd_defrouter
*next
;
3045 struct nd_drhead nd_defrouter_tmp
;
3047 TAILQ_INIT(&nd_defrouter_tmp
);
3048 lck_mtx_lock(nd6_mutex
);
3049 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
3051 * The first entry of the list may be stored in
3052 * the routing table, so we'll delete it later.
3054 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
3055 next
= TAILQ_NEXT(dr
, dr_entry
);
3056 if (ifp
== lo_ifp
|| dr
->ifp
== ifp
) {
3058 * Remove the entry from default router list
3059 * and add it to the temp list.
3060 * nd_defrouter_tmp will be a local temporary
3061 * list as no one else can get the same
3062 * removed entry once it is removed from default
3064 * Remove the reference after calling defrtrlist_de
3066 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
3067 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
3071 dr
= TAILQ_FIRST(&nd_defrouter
);
3072 if (ifp
== lo_ifp
||
3074 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
3075 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
3080 * Keep the following separate from the above iteration of
3081 * nd_defrouter because it's not safe to call
3082 * defrtrlist_del while iterating global default
3083 * router list. Global list has to be traversed
3084 * while holding nd6_mutex throughout.
3086 * The following call to defrtrlist_del should be
3087 * safe as we are iterating a local list of
3090 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, next
) {
3091 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
3093 NDDR_REMREF(dr
); /* remove list reference */
3095 lck_mtx_unlock(nd6_mutex
);
3099 case SIOCGNBRINFO_IN6_32
: { /* struct in6_nbrinfo_32 */
3100 struct llinfo_nd6
*ln
;
3101 struct in6_nbrinfo_32 nbi_32
;
3102 struct in6_addr nb_addr
; /* make local for safety */
3104 bcopy(data
, &nbi_32
, sizeof(nbi_32
));
3105 nb_addr
= nbi_32
.addr
;
3107 * XXX: KAME specific hack for scoped addresses
3108 * XXXX: for other scopes than link-local?
3110 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32
.addr
) ||
3111 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32
.addr
)) {
3113 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
3116 *idp
= htons(ifp
->if_index
);
3120 /* Callee returns a locked route upon success */
3121 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
3125 RT_LOCK_ASSERT_HELD(rt
);
3127 nbi_32
.state
= ln
->ln_state
;
3128 nbi_32
.asked
= ln
->ln_asked
;
3129 nbi_32
.isrouter
= ln
->ln_router
;
3130 nbi_32
.expire
= ln_getexpire(ln
);
3131 RT_REMREF_LOCKED(rt
);
3133 bcopy(&nbi_32
, data
, sizeof(nbi_32
));
3137 case SIOCGNBRINFO_IN6_64
: { /* struct in6_nbrinfo_64 */
3138 struct llinfo_nd6
*ln
;
3139 struct in6_nbrinfo_64 nbi_64
;
3140 struct in6_addr nb_addr
; /* make local for safety */
3142 bcopy(data
, &nbi_64
, sizeof(nbi_64
));
3143 nb_addr
= nbi_64
.addr
;
3145 * XXX: KAME specific hack for scoped addresses
3146 * XXXX: for other scopes than link-local?
3148 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64
.addr
) ||
3149 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64
.addr
)) {
3151 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
3154 *idp
= htons(ifp
->if_index
);
3158 /* Callee returns a locked route upon success */
3159 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
3163 RT_LOCK_ASSERT_HELD(rt
);
3165 nbi_64
.state
= ln
->ln_state
;
3166 nbi_64
.asked
= ln
->ln_asked
;
3167 nbi_64
.isrouter
= ln
->ln_router
;
3168 nbi_64
.expire
= ln_getexpire(ln
);
3169 RT_REMREF_LOCKED(rt
);
3171 bcopy(&nbi_64
, data
, sizeof(nbi_64
));
3175 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
3176 case SIOCGDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
3177 struct in6_ndifreq_64
*ndif_64
=
3178 (struct in6_ndifreq_64
*)(void *)data
;
3179 struct in6_ndifreq_32
*ndif_32
=
3180 (struct in6_ndifreq_32
*)(void *)data
;
3182 if (cmd
== SIOCGDEFIFACE_IN6_64
) {
3183 u_int64_t j
= nd6_defifindex
;
3184 __nochk_bcopy(&j
, &ndif_64
->ifindex
, sizeof(j
));
3186 bcopy(&nd6_defifindex
, &ndif_32
->ifindex
,
3192 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
3193 case SIOCSDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
3194 struct in6_ndifreq_64
*ndif_64
=
3195 (struct in6_ndifreq_64
*)(void *)data
;
3196 struct in6_ndifreq_32
*ndif_32
=
3197 (struct in6_ndifreq_32
*)(void *)data
;
3200 if (cmd
== SIOCSDEFIFACE_IN6_64
) {
3202 __nochk_bcopy(&ndif_64
->ifindex
, &j
, sizeof(j
));
3205 bcopy(&ndif_32
->ifindex
, &idx
, sizeof(idx
));
3208 error
= nd6_setdefaultiface(idx
);
3212 case SIOCGIFCGAPREP_IN6
:
3213 case SIOCSIFCGAPREP_IN6
:
3215 struct in6_cgareq
*p_cgareq
=
3216 (struct in6_cgareq
*)(void *)data
;
3217 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
3219 struct in6_cga_modifier
*req_cga_mod
=
3220 &(p_cgareq
->cgar_cgaprep
.cga_modifier
);
3221 struct in6_cga_modifier
*ndi_cga_mod
= NULL
;
3223 if ((NULL
== ndi
) || !ndi
->initialized
) {
3228 lck_mtx_lock(&ndi
->lock
);
3229 ndi_cga_mod
= &(ndi
->local_cga_modifier
);
3231 if (cmd
== SIOCSIFCGAPREP_IN6
) {
3232 bcopy(req_cga_mod
, ndi_cga_mod
, sizeof(*ndi_cga_mod
));
3233 ndi
->cga_initialized
= TRUE
;
3235 bcopy(ndi_cga_mod
, req_cga_mod
, sizeof(*req_cga_mod
));
3238 lck_mtx_unlock(&ndi
->lock
);
3247 * Create neighbor cache entry and cache link-layer address,
3248 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
3251 nd6_cache_lladdr(struct ifnet
*ifp
, struct in6_addr
*from
, char *lladdr
,
3252 int lladdrlen
, int type
, int code
)
3254 #pragma unused(lladdrlen)
3255 struct rtentry
*rt
= NULL
;
3256 struct llinfo_nd6
*ln
= NULL
;
3258 struct sockaddr_dl
*sdl
= NULL
;
3264 boolean_t sched_timeout
= FALSE
;
3265 struct nd_ifinfo
*ndi
= NULL
;
3268 panic("ifp == NULL in nd6_cache_lladdr");
3271 panic("from == NULL in nd6_cache_lladdr");
3274 /* nothing must be updated for unspecified address */
3275 if (IN6_IS_ADDR_UNSPECIFIED(from
)) {
3280 * Validation about ifp->if_addrlen and lladdrlen must be done in
3283 timenow
= net_uptime();
3285 rt
= nd6_lookup(from
, 0, ifp
, 0);
3287 if ((rt
= nd6_lookup(from
, 1, ifp
, 0)) == NULL
) {
3290 RT_LOCK_ASSERT_HELD(rt
);
3293 RT_LOCK_ASSERT_HELD(rt
);
3294 /* do nothing if static ndp is set */
3295 if (rt
->rt_flags
& RTF_STATIC
) {
3296 RT_REMREF_LOCKED(rt
);
3303 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
3310 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
3314 if (rt
->rt_gateway
== NULL
) {
3317 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
3320 sdl
= SDL(rt
->rt_gateway
);
3322 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
3323 if (olladdr
&& lladdr
) {
3324 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
)) {
3334 * newentry olladdr lladdr llchange (*=record)
3337 * 0 n y -- (3) * STALE
3339 * 0 y y y (5) * STALE
3340 * 1 -- n -- (6) NOSTATE(= PASSIVE)
3341 * 1 -- y -- (7) * STALE
3344 if (lladdr
!= NULL
) { /* (3-5) and (7) */
3346 * Record source link-layer address
3347 * XXX is it dependent to ifp->if_type?
3349 sdl
->sdl_alen
= ifp
->if_addrlen
;
3350 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
3352 /* cache the gateway (sender HW) address */
3353 nd6_llreach_alloc(rt
, ifp
, LLADDR(sdl
), sdl
->sdl_alen
, FALSE
);
3356 if (is_newentry
== 0) {
3357 if ((!olladdr
&& lladdr
!= NULL
) || /* (3) */
3358 (olladdr
&& lladdr
!= NULL
&& llchange
)) { /* (5) */
3360 newstate
= ND6_LLINFO_STALE
;
3361 } else { /* (1-2,4) */
3366 if (lladdr
== NULL
) { /* (6) */
3367 newstate
= ND6_LLINFO_NOSTATE
;
3369 newstate
= ND6_LLINFO_STALE
;
3374 * For interface's that do not perform NUD
3375 * neighbor cache entres must always be marked
3376 * reachable with no expiry
3378 ndi
= ND_IFINFO(ifp
);
3379 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3381 if (ndi
&& !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
3382 newstate
= ND6_LLINFO_REACHABLE
;
3383 ln_setexpire(ln
, 0);
3388 * Update the state of the neighbor cache.
3390 ND6_CACHE_STATE_TRANSITION(ln
, newstate
);
3392 if ((ln
->ln_state
== ND6_LLINFO_STALE
) ||
3393 (ln
->ln_state
== ND6_LLINFO_REACHABLE
)) {
3394 struct mbuf
*m
= ln
->ln_hold
;
3396 * XXX: since nd6_output() below will cause
3397 * state tansition to DELAY and reset the timer,
3398 * we must set the timer now, although it is actually
3401 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
3402 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
3407 struct sockaddr_in6 sin6
;
3409 rtkey_to_sa6(rt
, &sin6
);
3411 * we assume ifp is not a p2p here, so just
3412 * set the 2nd argument as the 1st one.
3415 nd6_output_list(ifp
, ifp
, m
, &sin6
, rt
, NULL
);
3418 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
3419 /* probe right away */
3420 ln_setexpire(ln
, timenow
);
3421 sched_timeout
= TRUE
;
3426 * ICMP6 type dependent behavior.
3428 * NS: clear IsRouter if new entry
3429 * RS: clear IsRouter
3430 * RA: set IsRouter if there's lladdr
3431 * redir: clear IsRouter if new entry
3434 * The spec says that we must set IsRouter in the following cases:
3435 * - If lladdr exist, set IsRouter. This means (1-5).
3436 * - If it is old entry (!newentry), set IsRouter. This means (7).
3437 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
3438 * A quetion arises for (1) case. (1) case has no lladdr in the
3439 * neighbor cache, this is similar to (6).
3440 * This case is rare but we figured that we MUST NOT set IsRouter.
3442 * newentry olladdr lladdr llchange NS RS RA redir
3444 * 0 n n -- (1) c ? s
3445 * 0 y n -- (2) c s s
3446 * 0 n y -- (3) c s s
3449 * 1 -- n -- (6) c c c s
3450 * 1 -- y -- (7) c c s c s
3454 switch (type
& 0xff) {
3455 case ND_NEIGHBOR_SOLICIT
:
3457 * New entry must have is_router flag cleared.
3459 if (is_newentry
) { /* (6-7) */
3465 * If the ICMP message is a Redirect to a better router, always
3466 * set the is_router flag. Otherwise, if the entry is newly
3467 * created, then clear the flag. [RFC 4861, sec 8.3]
3469 if (code
== ND_REDIRECT_ROUTER
) {
3471 } else if (is_newentry
) { /* (6-7) */
3475 case ND_ROUTER_SOLICIT
:
3477 * is_router flag must always be cleared.
3481 case ND_ROUTER_ADVERT
:
3483 * Mark an entry with lladdr as a router.
3485 if ((!is_newentry
&& (olladdr
|| lladdr
)) || /* (2-5) */
3486 (is_newentry
&& lladdr
)) { /* (7) */
3493 int route_ev_code
= 0;
3496 route_ev_code
= ROUTE_LLENTRY_CHANGED
;
3498 route_ev_code
= ROUTE_LLENTRY_RESOLVED
;
3501 /* Enqueue work item to invoke callback for this route entry */
3502 route_event_enqueue_nwk_wq_entry(rt
, NULL
, route_ev_code
, NULL
, TRUE
);
3504 if (ln
->ln_router
|| (rt
->rt_flags
& RTF_ROUTER
)) {
3505 struct radix_node_head
*rnh
= NULL
;
3506 struct route_event rt_ev
;
3507 route_event_init(&rt_ev
, rt
, NULL
, llchange
? ROUTE_LLENTRY_CHANGED
:
3508 ROUTE_LLENTRY_RESOLVED
);
3510 * We already have a valid reference on rt.
3511 * The function frees that before returning.
3512 * We therefore don't need an extra reference here
3515 lck_mtx_lock(rnh_lock
);
3517 rnh
= rt_tables
[AF_INET6
];
3519 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
,
3522 lck_mtx_unlock(rnh_lock
);
3528 * When the link-layer address of a router changes, select the
3529 * best router again. In particular, when the neighbor entry is newly
3530 * created, it might affect the selection policy.
3531 * Question: can we restrict the first condition to the "is_newentry"
3534 * Note: Perform default router selection even when we are a router,
3535 * if Scoped Routing is enabled.
3537 if (do_update
&& ln
->ln_router
) {
3538 RT_REMREF_LOCKED(rt
);
3540 lck_mtx_lock(nd6_mutex
);
3541 defrouter_select(ifp
);
3542 lck_mtx_unlock(nd6_mutex
);
3544 RT_REMREF_LOCKED(rt
);
3547 if (sched_timeout
) {
3548 lck_mtx_lock(rnh_lock
);
3549 nd6_sched_timeout(NULL
, NULL
);
3550 lck_mtx_unlock(rnh_lock
);
3555 nd6_slowtimo(void *arg
)
3558 struct nd_ifinfo
*nd6if
= NULL
;
3559 struct ifnet
*ifp
= NULL
;
3561 ifnet_head_lock_shared();
3562 for (ifp
= ifnet_head
.tqh_first
; ifp
;
3563 ifp
= ifp
->if_link
.tqe_next
) {
3564 nd6if
= ND_IFINFO(ifp
);
3565 if ((NULL
== nd6if
) || (FALSE
== nd6if
->initialized
)) {
3569 lck_mtx_lock(&nd6if
->lock
);
3570 if (nd6if
->basereachable
&& /* already initialized */
3571 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
3573 * Since reachable time rarely changes by router
3574 * advertisements, we SHOULD insure that a new random
3575 * value gets recomputed at least once every few hours.
3578 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
3580 ND_COMPUTE_RTIME(nd6if
->basereachable
);
3582 lck_mtx_unlock(&nd6if
->lock
);
3585 timeout(nd6_slowtimo
, NULL
, ND6_SLOWTIMER_INTERVAL
* hz
);
3589 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
3590 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
3592 return nd6_output_list(ifp
, origifp
, m0
, dst
, hint0
, adv
);
3598 * Assumption: route determination for first packet can be correctly applied to
3599 * all packets in the chain.
3601 #define senderr(e) { error = (e); goto bad; }
3603 nd6_output_list(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
3604 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
3606 struct rtentry
*rt
= hint0
, *hint
= hint0
;
3607 struct llinfo_nd6
*ln
= NULL
;
3610 struct rtentry
*rtrele
= NULL
;
3611 struct nd_ifinfo
*ndi
= NULL
;
3615 RT_ADDREF_LOCKED(rt
);
3618 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
) || !nd6_need_cache(ifp
)) {
3626 * Next hop determination. Because we may involve the gateway route
3627 * in addition to the original route, locking is rather complicated.
3628 * The general concept is that regardless of whether the route points
3629 * to the original route or to the gateway route, this routine takes
3630 * an extra reference on such a route. This extra reference will be
3631 * released at the end.
3633 * Care must be taken to ensure that the "hint0" route never gets freed
3634 * via rtfree(), since the caller may have stored it inside a struct
3635 * route with a reference held for that placeholder.
3637 * This logic is similar to, though not exactly the same as the one
3638 * used by route_to_gwroute().
3642 * We have a reference to "rt" by now (or below via rtalloc1),
3643 * which will either be released or freed at the end of this
3646 RT_LOCK_ASSERT_HELD(rt
);
3647 if (!(rt
->rt_flags
& RTF_UP
)) {
3648 RT_REMREF_LOCKED(rt
);
3650 if ((hint
= rt
= rtalloc1_scoped(SA(dst
), 1, 0,
3651 ifp
->if_index
)) != NULL
) {
3653 if (rt
->rt_ifp
!= ifp
) {
3654 /* XXX: loop care? */
3656 error
= nd6_output_list(ifp
, origifp
, m0
,
3662 senderr(EHOSTUNREACH
);
3666 if (rt
->rt_flags
& RTF_GATEWAY
) {
3667 struct rtentry
*gwrt
;
3668 struct in6_ifaddr
*ia6
= NULL
;
3669 struct sockaddr_in6 gw6
;
3671 rtgw_to_sa6(rt
, &gw6
);
3673 * Must drop rt_lock since nd6_is_addr_neighbor()
3674 * calls nd6_lookup() and acquires rnh_lock.
3679 * We skip link-layer address resolution and NUD
3680 * if the gateway is not a neighbor from ND point
3681 * of view, regardless of the value of nd_ifinfo.flags.
3682 * The second condition is a bit tricky; we skip
3683 * if the gateway is our own address, which is
3684 * sometimes used to install a route to a p2p link.
3686 if (!nd6_is_addr_neighbor(&gw6
, ifp
, 0) ||
3687 (ia6
= in6ifa_ifpwithaddr(ifp
, &gw6
.sin6_addr
))) {
3689 * We allow this kind of tricky route only
3690 * when the outgoing interface is p2p.
3691 * XXX: we may need a more generic rule here.
3694 IFA_REMREF(&ia6
->ia_ifa
);
3696 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
3697 senderr(EHOSTUNREACH
);
3703 gw6
= *(SIN6(rt
->rt_gateway
));
3705 /* If hint is now down, give up */
3706 if (!(rt
->rt_flags
& RTF_UP
)) {
3708 senderr(EHOSTUNREACH
);
3711 /* If there's no gateway route, look it up */
3712 if ((gwrt
= rt
->rt_gwroute
) == NULL
) {
3716 /* Become a regular mutex */
3717 RT_CONVERT_LOCK(rt
);
3720 * Take gwrt's lock while holding route's lock;
3721 * this is okay since gwrt never points back
3722 * to rt, so no lock ordering issues.
3725 if (!(gwrt
->rt_flags
& RTF_UP
)) {
3726 rt
->rt_gwroute
= NULL
;
3731 lck_mtx_lock(rnh_lock
);
3732 gwrt
= rtalloc1_scoped_locked(SA(&gw6
), 1, 0,
3737 * Bail out if the route is down, no route
3738 * to gateway, circular route, or if the
3739 * gateway portion of "rt" has changed.
3741 if (!(rt
->rt_flags
& RTF_UP
) ||
3742 gwrt
== NULL
|| gwrt
== rt
||
3743 !equal(SA(&gw6
), rt
->rt_gateway
)) {
3745 RT_REMREF_LOCKED(gwrt
);
3750 rtfree_locked(gwrt
);
3752 lck_mtx_unlock(rnh_lock
);
3753 senderr(EHOSTUNREACH
);
3755 VERIFY(gwrt
!= NULL
);
3757 * Set gateway route; callee adds ref to gwrt;
3758 * gwrt has an extra ref from rtalloc1() for
3761 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
3763 lck_mtx_unlock(rnh_lock
);
3764 /* Remember to release/free "rt" at the end */
3768 RT_ADDREF_LOCKED(gwrt
);
3771 /* Remember to release/free "rt" at the end */
3778 * This is an opportunity to revalidate the parent
3779 * route's gwroute, in case it now points to a dead
3780 * route entry. Parent route won't go away since the
3781 * clone (hint) holds a reference to it. rt == gwrt.
3784 if ((hint
->rt_flags
& (RTF_WASCLONED
| RTF_UP
)) ==
3785 (RTF_WASCLONED
| RTF_UP
)) {
3786 struct rtentry
*prt
= hint
->rt_parent
;
3787 VERIFY(prt
!= NULL
);
3789 RT_CONVERT_LOCK(hint
);
3792 rt_revalidate_gwroute(prt
, rt
);
3799 /* rt == gwrt; if it is now down, give up */
3800 if (!(rt
->rt_flags
& RTF_UP
)) {
3804 /* "rtrele" == original "rt" */
3805 senderr(EHOSTUNREACH
);
3809 /* Become a regular mutex */
3810 RT_CONVERT_LOCK(rt
);
3814 * Address resolution or Neighbor Unreachability Detection
3816 * At this point, the destination of the packet must be a unicast
3817 * or an anycast address(i.e. not a multicast).
3820 /* Look up the neighbor cache for the nexthop */
3821 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0) {
3824 struct sockaddr_in6 sin6
;
3826 * Clear out Scope ID field in case it is set.
3829 sin6
.sin6_scope_id
= 0;
3831 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
3832 * the condition below is not very efficient. But we believe
3833 * it is tolerable, because this should be a rare case.
3834 * Must drop rt_lock since nd6_is_addr_neighbor() calls
3835 * nd6_lookup() and acquires rnh_lock.
3840 if (nd6_is_addr_neighbor(&sin6
, ifp
, 0)) {
3841 /* "rtrele" may have been used, so clean up "rt" now */
3843 /* Don't free "hint0" */
3850 /* Callee returns a locked route upon success */
3851 rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 0);
3853 RT_LOCK_ASSERT_HELD(rt
);
3856 } else if (rt
!= NULL
) {
3865 ndi
= ND_IFINFO(ifp
);
3866 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
3867 lck_mtx_lock(&ndi
->lock
);
3868 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
3869 !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
3870 lck_mtx_unlock(&ndi
->lock
);
3872 "nd6_output: can't allocate llinfo for %s "
3873 "(ln=0x%llx, rt=0x%llx)\n",
3874 ip6_sprintf(&dst
->sin6_addr
),
3875 (uint64_t)VM_KERNEL_ADDRPERM(ln
),
3876 (uint64_t)VM_KERNEL_ADDRPERM(rt
));
3877 senderr(EIO
); /* XXX: good error? */
3879 lck_mtx_unlock(&ndi
->lock
);
3881 goto sendpkt
; /* send anyway */
3884 net_update_uptime();
3885 timenow
= net_uptime();
3887 /* We don't have to do link-layer address resolution on a p2p link. */
3888 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
3889 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
3890 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
);
3891 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
3895 * The first time we send a packet to a neighbor whose entry is
3896 * STALE, we have to change the state to DELAY and a sets a timer to
3897 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
3898 * neighbor unreachability detection on expiration.
3901 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
3903 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_DELAY
);
3904 ln_setexpire(ln
, timenow
+ nd6_delay
);
3905 /* N.B.: we will re-arm the timer below. */
3906 _CASSERT(ND6_LLINFO_DELAY
> ND6_LLINFO_INCOMPLETE
);
3910 * If the neighbor cache entry has a state other than INCOMPLETE
3911 * (i.e. its link-layer address is already resolved), just
3914 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
3917 * Move this entry to the head of the queue so that it is
3918 * less likely for this entry to be a target of forced
3919 * garbage collection (see nd6_rtrequest()). Do this only
3920 * if the entry is non-permanent (as permanent ones will
3921 * never be purged), and if the number of active entries
3922 * is at least half of the threshold.
3924 if (ln
->ln_state
== ND6_LLINFO_DELAY
||
3925 (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3926 nd6_inuse
>= (ip6_neighborgcthresh
>> 1))) {
3927 lck_mtx_lock(rnh_lock
);
3928 if (ln
->ln_state
== ND6_LLINFO_DELAY
) {
3929 nd6_sched_timeout(NULL
, NULL
);
3931 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3932 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
3934 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
3940 lck_mtx_unlock(rnh_lock
);
3946 * If this is a prefix proxy route, record the inbound interface
3947 * so that it can be excluded from the list of interfaces eligible
3948 * for forwarding the proxied NS in nd6_prproxy_ns_output().
3950 if (rt
->rt_flags
& RTF_PROXY
) {
3951 ln
->ln_exclifp
= ((origifp
== ifp
) ? NULL
: origifp
);
3955 * There is a neighbor cache entry, but no ethernet address
3956 * response yet. Replace the held mbuf (if any) with this
3959 * This code conforms to the rate-limiting rule described in Section
3960 * 7.2.2 of RFC 4861, because the timer is set correctly after sending
3963 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
) {
3964 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_INCOMPLETE
);
3967 m_freem_list(ln
->ln_hold
);
3970 if (!ND6_LLINFO_PERMANENT(ln
) && ln
->ln_asked
== 0) {
3972 ndi
= ND_IFINFO(ifp
);
3973 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
3974 lck_mtx_lock(&ndi
->lock
);
3975 ln_setexpire(ln
, timenow
+ ndi
->retrans
/ 1000);
3976 lck_mtx_unlock(&ndi
->lock
);
3978 /* We still have a reference on rt (for ln) */
3979 if (ip6_forwarding
) {
3980 nd6_prproxy_ns_output(ifp
, origifp
, NULL
,
3981 &dst
->sin6_addr
, ln
);
3983 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, NULL
);
3985 lck_mtx_lock(rnh_lock
);
3986 nd6_sched_timeout(NULL
, NULL
);
3987 lck_mtx_unlock(rnh_lock
);
3992 * Move this entry to the head of the queue so that it is
3993 * less likely for this entry to be a target of forced
3994 * garbage collection (see nd6_rtrequest()). Do this only
3995 * if the entry is non-permanent (as permanent ones will
3996 * never be purged), and if the number of active entries
3997 * is at least half of the threshold.
3999 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
4000 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
4001 lck_mtx_lock(rnh_lock
);
4003 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
4007 /* Clean up "rt" now while we can */
4009 RT_REMREF_LOCKED(rt
);
4015 rt
= NULL
; /* "rt" has been taken care of */
4016 lck_mtx_unlock(rnh_lock
);
4023 RT_LOCK_ASSERT_NOTHELD(rt
);
4026 /* discard the packet if IPv6 operation is disabled on the interface */
4027 if (ifp
->if_eflags
& IFEF_IPV6_DISABLED
) {
4028 error
= ENETDOWN
; /* better error? */
4032 if (ifp
->if_flags
& IFF_LOOPBACK
) {
4033 /* forwarding rules require the original scope_id */
4034 m0
->m_pkthdr
.rcvif
= origifp
;
4035 error
= dlil_output(origifp
, PF_INET6
, m0
, (caddr_t
)rt
,
4039 /* Do not allow loopback address to wind up on a wire */
4040 struct ip6_hdr
*ip6
= mtod(m0
, struct ip6_hdr
*);
4042 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
4043 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
4044 ip6stat
.ip6s_badscope
++;
4045 error
= EADDRNOTAVAIL
;
4052 /* Mark use timestamp */
4053 if (rt
->rt_llinfo
!= NULL
) {
4054 nd6_llreach_use(rt
->rt_llinfo
);
4059 struct mbuf
*mcur
= m0
;
4060 uint32_t pktcnt
= 0;
4063 if (hint
!= NULL
&& nstat_collect
) {
4066 if ((mcur
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
) &&
4067 (mcur
->m_pkthdr
.tso_segsz
> 0)) {
4068 scnt
= mcur
->m_pkthdr
.len
/ mcur
->m_pkthdr
.tso_segsz
;
4073 nstat_route_tx(hint
, scnt
, mcur
->m_pkthdr
.len
, 0);
4077 mcur
->m_pkthdr
.rcvif
= NULL
;
4078 mcur
= mcur
->m_nextpkt
;
4080 if (pktcnt
> ip6_maxchainsent
) {
4081 ip6_maxchainsent
= pktcnt
;
4083 error
= dlil_output(ifp
, PF_INET6
, m0
, (caddr_t
)rt
, SA(dst
), 0, adv
);
4092 /* Clean up "rt" unless it's already been done */
4096 RT_REMREF_LOCKED(rt
);
4103 /* And now clean up "rtrele" if there is any */
4104 if (rtrele
!= NULL
) {
4105 RT_LOCK_SPIN(rtrele
);
4106 if (rtrele
== hint0
) {
4107 RT_REMREF_LOCKED(rtrele
);
4119 nd6_need_cache(struct ifnet
*ifp
)
4122 * XXX: we currently do not make neighbor cache on any interface
4123 * other than ARCnet, Ethernet, FDDI and GIF.
4126 * - unidirectional tunnels needs no ND
4128 switch (ifp
->if_type
) {
4134 case IFT_IEEE8023ADLAG
:
4138 case IFT_GIF
: /* XXX need more cases? */
4153 nd6_storelladdr(struct ifnet
*ifp
, struct rtentry
*rt
, struct mbuf
*m
,
4154 struct sockaddr
*dst
, u_char
*desten
)
4157 struct sockaddr_dl
*sdl
;
4159 if (m
->m_flags
& M_MCAST
) {
4160 switch (ifp
->if_type
) {
4164 case IFT_IEEE8023ADLAG
:
4169 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
, desten
);
4172 for (i
= 0; i
< ifp
->if_addrlen
; i
++) {
4180 return 0; /* caller will free mbuf */
4185 /* this could happen, if we could not allocate memory */
4186 return 0; /* caller will free mbuf */
4189 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
4190 printf("nd6_storelladdr: something odd happens\n");
4192 return 0; /* caller will free mbuf */
4194 sdl
= SDL(rt
->rt_gateway
);
4195 if (sdl
->sdl_alen
== 0) {
4196 /* this should be impossible, but we bark here for debugging */
4197 printf("nd6_storelladdr: sdl_alen == 0\n");
4199 return 0; /* caller will free mbuf */
4202 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
4208 * This is the ND pre-output routine; care must be taken to ensure that
4209 * the "hint" route never gets freed via rtfree(), since the caller may
4210 * have stored it inside a struct route with a reference held for that
4214 nd6_lookup_ipv6(ifnet_t ifp
, const struct sockaddr_in6
*ip6_dest
,
4215 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
4218 route_t route
= hint
;
4220 struct sockaddr_dl
*sdl
= NULL
;
4223 if (ifp
== NULL
|| ip6_dest
== NULL
) {
4227 if (ip6_dest
->sin6_family
!= AF_INET6
) {
4228 return EAFNOSUPPORT
;
4231 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
4237 * Callee holds a reference on the route and returns
4238 * with the route entry locked, upon success.
4240 result
= route_to_gwroute((const struct sockaddr
*)ip6_dest
,
4245 if (route
!= NULL
) {
4246 RT_LOCK_ASSERT_HELD(route
);
4250 if ((packet
!= NULL
&& (packet
->m_flags
& M_MCAST
) != 0) ||
4251 ((ifp
->if_flags
& IFF_MULTICAST
) &&
4252 IN6_IS_ADDR_MULTICAST(&ip6_dest
->sin6_addr
))) {
4253 if (route
!= NULL
) {
4256 result
= dlil_resolve_multi(ifp
,
4257 (const struct sockaddr
*)ip6_dest
,
4258 SA(ll_dest
), ll_dest_len
);
4259 if (route
!= NULL
) {
4263 } else if (route
== NULL
) {
4266 * For unicast, lookup existing ND6 entries but
4267 * do not trigger a resolution
4269 lck_mtx_lock(rnh_lock
);
4270 route
= rt_lookup(TRUE
,
4271 __DECONST(struct sockaddr
*, ip6_dest
), NULL
,
4272 rt_tables
[AF_INET6
], ifp
->if_index
);
4273 lck_mtx_unlock(rnh_lock
);
4275 if (route
!= NULL
) {
4280 if (route
== NULL
) {
4282 * This could happen, if we could not allocate memory or
4283 * if route_to_gwroute() didn't return a route.
4289 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
4290 printf("%s: route %s on %s%d gateway address not AF_LINK\n",
4291 __func__
, ip6_sprintf(&ip6_dest
->sin6_addr
),
4292 route
->rt_ifp
->if_name
, route
->rt_ifp
->if_unit
);
4293 result
= EADDRNOTAVAIL
;
4297 sdl
= SDL(route
->rt_gateway
);
4298 if (sdl
->sdl_alen
== 0) {
4299 /* this should be impossible, but we bark here for debugging */
4300 printf("%s: route %s on %s%d sdl_alen == 0\n", __func__
,
4301 ip6_sprintf(&ip6_dest
->sin6_addr
), route
->rt_ifp
->if_name
,
4302 route
->rt_ifp
->if_unit
);
4303 result
= EHOSTUNREACH
;
4307 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
4308 bcopy(sdl
, ll_dest
, copy_len
);
4311 if (route
!= NULL
) {
4312 if (route
== hint
) {
4313 RT_REMREF_LOCKED(route
);
4323 #if (DEVELOPMENT || DEBUG)
4325 static int sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
;
4326 SYSCTL_PROC(_net_inet6_icmp6
, OID_AUTO
, nd6_lookup_ipv6
,
4327 CTLTYPE_STRUCT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0,
4328 sysctl_nd6_lookup_ipv6
, "S", "");
4331 sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
4333 #pragma unused(oidp, arg1, arg2)
4335 struct nd6_lookup_ipv6_args nd6_lookup_ipv6_args
;
4339 * Only root can lookup MAC addresses
4341 error
= proc_suser(current_proc());
4343 nd6log0(error
, "%s: proc_suser() error %d\n",
4347 if (req
->oldptr
== USER_ADDR_NULL
) {
4348 req
->oldidx
= sizeof(struct nd6_lookup_ipv6_args
);
4350 if (req
->newptr
== USER_ADDR_NULL
) {
4353 if (req
->oldlen
!= sizeof(struct nd6_lookup_ipv6_args
) ||
4354 req
->newlen
!= sizeof(struct nd6_lookup_ipv6_args
)) {
4356 nd6log0(error
, "%s: bad req, error %d\n",
4360 error
= SYSCTL_IN(req
, &nd6_lookup_ipv6_args
,
4361 sizeof(struct nd6_lookup_ipv6_args
));
4363 nd6log0(error
, "%s: SYSCTL_IN() error %d\n",
4368 if (nd6_lookup_ipv6_args
.ll_dest_len
> sizeof(nd6_lookup_ipv6_args
.ll_dest_
)) {
4370 nd6log0(error
, "%s: bad ll_dest_len, error %d\n",
4375 /* Make sure to terminate the string */
4376 nd6_lookup_ipv6_args
.ifname
[IFNAMSIZ
- 1] = 0;
4378 error
= ifnet_find_by_name(nd6_lookup_ipv6_args
.ifname
, &ifp
);
4380 nd6log0(error
, "%s: ifnet_find_by_name() error %d\n",
4385 error
= nd6_lookup_ipv6(ifp
, &nd6_lookup_ipv6_args
.ip6_dest
,
4386 &nd6_lookup_ipv6_args
.ll_dest_
._sdl
,
4387 nd6_lookup_ipv6_args
.ll_dest_len
, NULL
, NULL
);
4389 nd6log0(error
, "%s: nd6_lookup_ipv6() error %d\n",
4394 error
= SYSCTL_OUT(req
, &nd6_lookup_ipv6_args
,
4395 sizeof(struct nd6_lookup_ipv6_args
));
4397 nd6log0(error
, "%s: SYSCTL_OUT() error %d\n",
4405 #endif /* (DEVELOPEMENT || DEBUG) */
4408 nd6_setifinfo(struct ifnet
*ifp
, u_int32_t before
, u_int32_t after
)
4414 * Handle ND6_IFF_IFDISABLED
4416 if ((before
& ND6_IFF_IFDISABLED
) ||
4417 (after
& ND6_IFF_IFDISABLED
)) {
4418 b
= (before
& ND6_IFF_IFDISABLED
);
4419 a
= (after
& ND6_IFF_IFDISABLED
);
4421 if (b
!= a
&& (err
= nd6_if_disable(ifp
,
4422 ((int32_t)(a
- b
) > 0))) != 0) {
4428 * Handle ND6_IFF_PROXY_PREFIXES
4430 if ((before
& ND6_IFF_PROXY_PREFIXES
) ||
4431 (after
& ND6_IFF_PROXY_PREFIXES
)) {
4432 b
= (before
& ND6_IFF_PROXY_PREFIXES
);
4433 a
= (after
& ND6_IFF_PROXY_PREFIXES
);
4435 if (b
!= a
&& (err
= nd6_if_prproxy(ifp
,
4436 ((int32_t)(a
- b
) > 0))) != 0) {
4445 * Enable/disable IPv6 on an interface, called as part of
4446 * setting/clearing ND6_IFF_IFDISABLED, or during DAD failure.
4449 nd6_if_disable(struct ifnet
*ifp
, boolean_t enable
)
4451 ifnet_lock_shared(ifp
);
4453 ifp
->if_eflags
|= IFEF_IPV6_DISABLED
;
4455 ifp
->if_eflags
&= ~IFEF_IPV6_DISABLED
;
4457 ifnet_lock_done(ifp
);
4463 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
4465 #pragma unused(oidp, arg1, arg2)
4466 char pbuf
[MAX_IPv6_STR_LEN
];
4467 struct nd_defrouter
*dr
;
4470 if (req
->newptr
!= USER_ADDR_NULL
) {
4474 /* XXX Handle mapped defrouter entries */
4475 lck_mtx_lock(nd6_mutex
);
4476 if (proc_is64bit(req
->p
)) {
4477 struct in6_defrouter_64 d
;
4479 bzero(&d
, sizeof(d
));
4480 d
.rtaddr
.sin6_family
= AF_INET6
;
4481 d
.rtaddr
.sin6_len
= sizeof(d
.rtaddr
);
4483 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
4484 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
4485 if (in6_recoverscope(&d
.rtaddr
,
4486 &dr
->rtaddr
, dr
->ifp
) != 0) {
4487 log(LOG_ERR
, "scope error in default router "
4488 "list (%s)\n", inet_ntop(AF_INET6
,
4489 &dr
->rtaddr
, pbuf
, sizeof(pbuf
)));
4491 d
.flags
= dr
->flags
;
4492 d
.stateflags
= dr
->stateflags
;
4493 d
.rtlifetime
= dr
->rtlifetime
;
4494 d
.expire
= nddr_getexpire(dr
);
4495 d
.if_index
= dr
->ifp
->if_index
;
4496 error
= SYSCTL_OUT(req
, &d
, sizeof(d
));
4502 struct in6_defrouter_32 d
;
4504 bzero(&d
, sizeof(d
));
4505 d
.rtaddr
.sin6_family
= AF_INET6
;
4506 d
.rtaddr
.sin6_len
= sizeof(d
.rtaddr
);
4508 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
4509 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
4510 if (in6_recoverscope(&d
.rtaddr
,
4511 &dr
->rtaddr
, dr
->ifp
) != 0) {
4512 log(LOG_ERR
, "scope error in default router "
4513 "list (%s)\n", inet_ntop(AF_INET6
,
4514 &dr
->rtaddr
, pbuf
, sizeof(pbuf
)));
4516 d
.flags
= dr
->flags
;
4517 d
.stateflags
= dr
->stateflags
;
4518 d
.rtlifetime
= dr
->rtlifetime
;
4519 d
.expire
= nddr_getexpire(dr
);
4520 d
.if_index
= dr
->ifp
->if_index
;
4521 error
= SYSCTL_OUT(req
, &d
, sizeof(d
));
4527 lck_mtx_unlock(nd6_mutex
);
4532 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
4534 #pragma unused(oidp, arg1, arg2)
4535 char pbuf
[MAX_IPv6_STR_LEN
];
4536 struct nd_pfxrouter
*pfr
;
4537 struct sockaddr_in6 s6
;
4538 struct nd_prefix
*pr
;
4541 if (req
->newptr
!= USER_ADDR_NULL
) {
4545 bzero(&s6
, sizeof(s6
));
4546 s6
.sin6_family
= AF_INET6
;
4547 s6
.sin6_len
= sizeof(s6
);
4549 /* XXX Handle mapped defrouter entries */
4550 lck_mtx_lock(nd6_mutex
);
4551 if (proc_is64bit(req
->p
)) {
4552 struct in6_prefix_64 p
;
4554 bzero(&p
, sizeof(p
));
4555 p
.origin
= PR_ORIG_RA
;
4557 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
4559 p
.prefix
= pr
->ndpr_prefix
;
4560 if (in6_recoverscope(&p
.prefix
,
4561 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0) {
4562 log(LOG_ERR
, "scope error in "
4563 "prefix list (%s)\n", inet_ntop(AF_INET6
,
4564 &p
.prefix
.sin6_addr
, pbuf
, sizeof(pbuf
)));
4566 p
.raflags
= pr
->ndpr_raf
;
4567 p
.prefixlen
= pr
->ndpr_plen
;
4568 p
.vltime
= pr
->ndpr_vltime
;
4569 p
.pltime
= pr
->ndpr_pltime
;
4570 p
.if_index
= pr
->ndpr_ifp
->if_index
;
4571 p
.expire
= ndpr_getexpire(pr
);
4572 p
.refcnt
= pr
->ndpr_addrcnt
;
4573 p
.flags
= pr
->ndpr_stateflags
;
4575 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
4577 error
= SYSCTL_OUT(req
, &p
, sizeof(p
));
4582 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
4583 s6
.sin6_addr
= pfr
->router
->rtaddr
;
4584 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
4585 pfr
->router
->ifp
) != 0) {
4587 "scope error in prefix list (%s)\n",
4588 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
4589 pbuf
, sizeof(pbuf
)));
4591 error
= SYSCTL_OUT(req
, &s6
, sizeof(s6
));
4602 struct in6_prefix_32 p
;
4604 bzero(&p
, sizeof(p
));
4605 p
.origin
= PR_ORIG_RA
;
4607 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
4609 p
.prefix
= pr
->ndpr_prefix
;
4610 if (in6_recoverscope(&p
.prefix
,
4611 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0) {
4613 "scope error in prefix list (%s)\n",
4614 inet_ntop(AF_INET6
, &p
.prefix
.sin6_addr
,
4615 pbuf
, sizeof(pbuf
)));
4617 p
.raflags
= pr
->ndpr_raf
;
4618 p
.prefixlen
= pr
->ndpr_plen
;
4619 p
.vltime
= pr
->ndpr_vltime
;
4620 p
.pltime
= pr
->ndpr_pltime
;
4621 p
.if_index
= pr
->ndpr_ifp
->if_index
;
4622 p
.expire
= ndpr_getexpire(pr
);
4623 p
.refcnt
= pr
->ndpr_addrcnt
;
4624 p
.flags
= pr
->ndpr_stateflags
;
4626 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
4628 error
= SYSCTL_OUT(req
, &p
, sizeof(p
));
4633 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
4634 s6
.sin6_addr
= pfr
->router
->rtaddr
;
4635 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
4636 pfr
->router
->ifp
) != 0) {
4638 "scope error in prefix list (%s)\n",
4639 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
4640 pbuf
, sizeof(pbuf
)));
4642 error
= SYSCTL_OUT(req
, &s6
, sizeof(s6
));
4653 lck_mtx_unlock(nd6_mutex
);
4659 in6_ifaddr_set_dadprogress(struct in6_ifaddr
*ia
)
4661 struct ifnet
* ifp
= ia
->ia_ifp
;
4662 uint32_t flags
= IN6_IFF_TENTATIVE
;
4663 uint32_t optdad
= nd6_optimistic_dad
;
4664 struct nd_ifinfo
*ndi
= NULL
;
4666 ndi
= ND_IFINFO(ifp
);
4667 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
4668 if (!(ndi
->flags
& ND6_IFF_DAD
)) {
4673 if ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) != 0) {
4676 lck_mtx_lock(&ndi
->lock
);
4677 if ((ndi
->flags
& ND6_IFF_REPLICATED
) != 0) {
4680 lck_mtx_unlock(&ndi
->lock
);
4685 if ((optdad
& ND6_OPTIMISTIC_DAD_LINKLOCAL
) &&
4686 IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
4687 flags
= IN6_IFF_OPTIMISTIC
;
4688 } else if ((optdad
& ND6_OPTIMISTIC_DAD_AUTOCONF
) &&
4689 (ia
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
4690 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
4691 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
) {
4692 flags
= IN6_IFF_OPTIMISTIC
;
4694 } else if (ia
->ia6_flags
& IN6_IFF_SECURED
) {
4695 if (optdad
& ND6_OPTIMISTIC_DAD_SECURED
) {
4696 flags
= IN6_IFF_OPTIMISTIC
;
4700 * Keeping the behavior for temp and CGA
4701 * SLAAC addresses to have a knob for optimistic
4703 * Other than that if ND6_OPTIMISTIC_DAD_AUTOCONF
4704 * is set, we should default to optimistic
4706 * For now this means SLAAC addresses with interface
4707 * identifier derived from modified EUI-64 bit
4710 flags
= IN6_IFF_OPTIMISTIC
;
4712 } else if ((optdad
& ND6_OPTIMISTIC_DAD_DYNAMIC
) &&
4713 (ia
->ia6_flags
& IN6_IFF_DYNAMIC
)) {
4714 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
4715 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
) {
4716 flags
= IN6_IFF_OPTIMISTIC
;
4719 flags
= IN6_IFF_OPTIMISTIC
;
4721 } else if ((optdad
& ND6_OPTIMISTIC_DAD_MANUAL
) &&
4722 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)) {
4725 * Bypass tentative for address assignments
4726 * not covered above (e.g. manual) upon request
4728 if (!IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
) &&
4729 !(ia
->ia6_flags
& IN6_IFF_AUTOCONF
) &&
4730 !(ia
->ia6_flags
& IN6_IFF_DYNAMIC
)) {
4731 flags
= IN6_IFF_OPTIMISTIC
;
4736 ia
->ia6_flags
&= ~(IN6_IFF_DUPLICATED
| IN6_IFF_DADPROGRESS
);
4737 ia
->ia6_flags
|= flags
;
4739 nd6log2(debug
, "%s - %s ifp %s ia6_flags 0x%x\n",
4741 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
4742 if_name(ia
->ia_ifp
),