xref: /xnu-11417.140.69/bsd/netinet6/nd6.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2000-2023 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31*43a90889SApple OSS Distributions  * All rights reserved.
32*43a90889SApple OSS Distributions  *
33*43a90889SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
34*43a90889SApple OSS Distributions  * modification, are permitted provided that the following conditions
35*43a90889SApple OSS Distributions  * are met:
36*43a90889SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
37*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
38*43a90889SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
39*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
40*43a90889SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
41*43a90889SApple OSS Distributions  * 3. Neither the name of the project nor the names of its contributors
42*43a90889SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
43*43a90889SApple OSS Distributions  *    without specific prior written permission.
44*43a90889SApple OSS Distributions  *
45*43a90889SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46*43a90889SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47*43a90889SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48*43a90889SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49*43a90889SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50*43a90889SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51*43a90889SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52*43a90889SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53*43a90889SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54*43a90889SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55*43a90889SApple OSS Distributions  * SUCH DAMAGE.
56*43a90889SApple OSS Distributions  */
57*43a90889SApple OSS Distributions 
58*43a90889SApple OSS Distributions /*
59*43a90889SApple OSS Distributions  * XXX
60*43a90889SApple OSS Distributions  * KAME 970409 note:
61*43a90889SApple OSS Distributions  * BSD/OS version heavily modifies this code, related to llinfo.
62*43a90889SApple OSS Distributions  * Since we don't have BSD/OS version of net/route.c in our hand,
63*43a90889SApple OSS Distributions  * I left the code mostly as it was in 970310.  -- itojun
64*43a90889SApple OSS Distributions  */
65*43a90889SApple OSS Distributions 
66*43a90889SApple OSS Distributions #include <sys/param.h>
67*43a90889SApple OSS Distributions #include <sys/systm.h>
68*43a90889SApple OSS Distributions #include <sys/malloc.h>
69*43a90889SApple OSS Distributions #include <sys/mbuf.h>
70*43a90889SApple OSS Distributions #include <sys/socket.h>
71*43a90889SApple OSS Distributions #include <sys/sockio.h>
72*43a90889SApple OSS Distributions #include <sys/time.h>
73*43a90889SApple OSS Distributions #include <sys/kernel.h>
74*43a90889SApple OSS Distributions #include <sys/sysctl.h>
75*43a90889SApple OSS Distributions #include <sys/errno.h>
76*43a90889SApple OSS Distributions #include <sys/syslog.h>
77*43a90889SApple OSS Distributions #include <sys/protosw.h>
78*43a90889SApple OSS Distributions #include <sys/proc.h>
79*43a90889SApple OSS Distributions #include <sys/mcache.h>
80*43a90889SApple OSS Distributions 
81*43a90889SApple OSS Distributions #include <dev/random/randomdev.h>
82*43a90889SApple OSS Distributions 
83*43a90889SApple OSS Distributions #include <kern/queue.h>
84*43a90889SApple OSS Distributions #include <kern/zalloc.h>
85*43a90889SApple OSS Distributions 
86*43a90889SApple OSS Distributions #include <net/if.h>
87*43a90889SApple OSS Distributions #include <net/if_dl.h>
88*43a90889SApple OSS Distributions #include <net/if_types.h>
89*43a90889SApple OSS Distributions #include <net/if_llreach.h>
90*43a90889SApple OSS Distributions #include <net/route.h>
91*43a90889SApple OSS Distributions #include <net/dlil.h>
92*43a90889SApple OSS Distributions #include <net/ntstat.h>
93*43a90889SApple OSS Distributions #include <net/net_osdep.h>
94*43a90889SApple OSS Distributions #include <net/nwk_wq.h>
95*43a90889SApple OSS Distributions 
96*43a90889SApple OSS Distributions #include <netinet/in.h>
97*43a90889SApple OSS Distributions #include <netinet/in_arp.h>
98*43a90889SApple OSS Distributions #include <netinet/if_ether.h>
99*43a90889SApple OSS Distributions #include <netinet6/in6_var.h>
100*43a90889SApple OSS Distributions #include <netinet/ip6.h>
101*43a90889SApple OSS Distributions #include <netinet6/ip6_var.h>
102*43a90889SApple OSS Distributions #include <netinet6/nd6.h>
103*43a90889SApple OSS Distributions #include <netinet6/scope6_var.h>
104*43a90889SApple OSS Distributions #include <netinet/icmp6.h>
105*43a90889SApple OSS Distributions 
106*43a90889SApple OSS Distributions #include <net/sockaddr_utils.h>
107*43a90889SApple OSS Distributions 
108*43a90889SApple OSS Distributions #include <os/log.h>
109*43a90889SApple OSS Distributions 
110*43a90889SApple OSS Distributions #include "loop.h"
111*43a90889SApple OSS Distributions 
112*43a90889SApple OSS Distributions #define ND6_SLOWTIMER_INTERVAL          (60 * 60)       /* 1 hour */
113*43a90889SApple OSS Distributions #define ND6_RECALC_REACHTM_INTERVAL     (60 * 120)      /* 2 hours */
114*43a90889SApple OSS Distributions 
115*43a90889SApple OSS Distributions /* timer values */
116*43a90889SApple OSS Distributions int     nd6_prune       = 1;    /* walk list every 1 seconds */
117*43a90889SApple OSS Distributions int     nd6_prune_lazy  = 5;    /* lazily walk list every 5 seconds */
118*43a90889SApple OSS Distributions int     nd6_delay       = 5;    /* delay first probe time 5 second */
119*43a90889SApple OSS Distributions int     nd6_umaxtries   = 3;    /* maximum unicast query */
120*43a90889SApple OSS Distributions int     nd6_mmaxtries   = 3;    /* maximum multicast query */
121*43a90889SApple OSS Distributions int     nd6_useloopback = 1;    /* use loopback interface for local traffic */
122*43a90889SApple OSS Distributions int     nd6_gctimer     = (60 * 60 * 24); /* 1 day: garbage collection timer */
123*43a90889SApple OSS Distributions 
124*43a90889SApple OSS Distributions /* preventing too many loops in ND option parsing */
125*43a90889SApple OSS Distributions int nd6_maxndopt = 10;  /* max # of ND options allowed */
126*43a90889SApple OSS Distributions 
127*43a90889SApple OSS Distributions int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
128*43a90889SApple OSS Distributions 
129*43a90889SApple OSS Distributions #if ND6_DEBUG
130*43a90889SApple OSS Distributions int nd6_debug = 1;
131*43a90889SApple OSS Distributions #else
132*43a90889SApple OSS Distributions int nd6_debug = 0;
133*43a90889SApple OSS Distributions #endif
134*43a90889SApple OSS Distributions 
135*43a90889SApple OSS Distributions int nd6_optimistic_dad = ND6_OPTIMISTIC_DAD_DEFAULT;
136*43a90889SApple OSS Distributions 
137*43a90889SApple OSS Distributions /* for debugging? */
138*43a90889SApple OSS Distributions static int nd6_inuse, nd6_allocated;
139*43a90889SApple OSS Distributions 
140*43a90889SApple OSS Distributions /*
141*43a90889SApple OSS Distributions  * Synchronization notes:
142*43a90889SApple OSS Distributions  *
143*43a90889SApple OSS Distributions  * The global list of ND entries are stored in llinfo_nd6; an entry
144*43a90889SApple OSS Distributions  * gets inserted into the list when the route is created and gets
145*43a90889SApple OSS Distributions  * removed from the list when it is deleted; this is done as part
146*43a90889SApple OSS Distributions  * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in nd6_rtrequest().
147*43a90889SApple OSS Distributions  *
148*43a90889SApple OSS Distributions  * Because rnh_lock and rt_lock for the entry are held during those
149*43a90889SApple OSS Distributions  * operations, the same locks (and thus lock ordering) must be used
150*43a90889SApple OSS Distributions  * elsewhere to access the relevant data structure fields:
151*43a90889SApple OSS Distributions  *
152*43a90889SApple OSS Distributions  * ln_next, ln_prev, ln_rt
153*43a90889SApple OSS Distributions  *
154*43a90889SApple OSS Distributions  *	- Routing lock (rnh_lock)
155*43a90889SApple OSS Distributions  *
156*43a90889SApple OSS Distributions  * ln_hold, ln_asked, ln_expire, ln_state, ln_router, ln_flags,
157*43a90889SApple OSS Distributions  * ln_llreach, ln_lastused
158*43a90889SApple OSS Distributions  *
159*43a90889SApple OSS Distributions  *	- Routing entry lock (rt_lock)
160*43a90889SApple OSS Distributions  *
161*43a90889SApple OSS Distributions  * Due to the dependency on rt_lock, llinfo_nd6 has the same lifetime
162*43a90889SApple OSS Distributions  * as the route entry itself.  When a route is deleted (RTM_DELETE),
163*43a90889SApple OSS Distributions  * it is simply removed from the global list but the memory is not
164*43a90889SApple OSS Distributions  * freed until the route itself is freed.
165*43a90889SApple OSS Distributions  */
166*43a90889SApple OSS Distributions struct llinfo_nd6 llinfo_nd6 = {
167*43a90889SApple OSS Distributions 	.ln_next = &llinfo_nd6,
168*43a90889SApple OSS Distributions 	.ln_prev = &llinfo_nd6,
169*43a90889SApple OSS Distributions };
170*43a90889SApple OSS Distributions 
171*43a90889SApple OSS Distributions static LCK_GRP_DECLARE(nd_if_lock_grp, "nd_if_lock");
172*43a90889SApple OSS Distributions static LCK_ATTR_DECLARE(nd_if_lock_attr, 0, 0);
173*43a90889SApple OSS Distributions 
174*43a90889SApple OSS Distributions /* Protected by nd6_mutex */
175*43a90889SApple OSS Distributions struct nd_drhead nd_defrouter_list;
176*43a90889SApple OSS Distributions struct nd_prhead nd_prefix = { .lh_first = 0 };
177*43a90889SApple OSS Distributions struct nd_rtihead nd_rti_list;
178*43a90889SApple OSS Distributions /*
179*43a90889SApple OSS Distributions  * nd6_timeout() is scheduled on a demand basis.  nd6_timeout_run is used
180*43a90889SApple OSS Distributions  * to indicate whether or not a timeout has been scheduled.  The rnh_lock
181*43a90889SApple OSS Distributions  * mutex is used to protect this scheduling; it is a natural choice given
182*43a90889SApple OSS Distributions  * the work done in the timer callback.  Unfortunately, there are cases
183*43a90889SApple OSS Distributions  * when nd6_timeout() needs to be scheduled while rnh_lock cannot be easily
184*43a90889SApple OSS Distributions  * held, due to lock ordering.  In those cases, we utilize a "demand" counter
185*43a90889SApple OSS Distributions  * nd6_sched_timeout_want which can be atomically incremented without
186*43a90889SApple OSS Distributions  * having to hold rnh_lock.  On places where we acquire rnh_lock, such as
187*43a90889SApple OSS Distributions  * nd6_rtrequest(), we check this counter and schedule the timer if it is
188*43a90889SApple OSS Distributions  * non-zero.  The increment happens on various places when we allocate
189*43a90889SApple OSS Distributions  * new ND entries, default routers, prefixes and addresses.
190*43a90889SApple OSS Distributions  */
191*43a90889SApple OSS Distributions static int nd6_timeout_run;             /* nd6_timeout is scheduled to run */
192*43a90889SApple OSS Distributions static void nd6_timeout(void *);
193*43a90889SApple OSS Distributions int nd6_sched_timeout_want;             /* demand count for timer to be sched */
194*43a90889SApple OSS Distributions static boolean_t nd6_fast_timer_on = FALSE;
195*43a90889SApple OSS Distributions 
196*43a90889SApple OSS Distributions /* Serialization variables for nd6_service(), protected by rnh_lock */
197*43a90889SApple OSS Distributions static boolean_t nd6_service_busy;
198*43a90889SApple OSS Distributions static void *nd6_service_wc = &nd6_service_busy;
199*43a90889SApple OSS Distributions static int nd6_service_waiters = 0;
200*43a90889SApple OSS Distributions 
201*43a90889SApple OSS Distributions int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
202*43a90889SApple OSS Distributions static struct sockaddr_in6 all1_sa;
203*43a90889SApple OSS Distributions 
204*43a90889SApple OSS Distributions static int regen_tmpaddr(struct in6_ifaddr *);
205*43a90889SApple OSS Distributions 
206*43a90889SApple OSS Distributions static struct llinfo_nd6 *nd6_llinfo_alloc(zalloc_flags_t);
207*43a90889SApple OSS Distributions static void nd6_llinfo_free(void *);
208*43a90889SApple OSS Distributions static void nd6_llinfo_purge(struct rtentry *);
209*43a90889SApple OSS Distributions static void nd6_llinfo_get_ri(struct rtentry *, struct rt_reach_info *);
210*43a90889SApple OSS Distributions static void nd6_llinfo_get_iflri(struct rtentry *, struct ifnet_llreach_info *);
211*43a90889SApple OSS Distributions static void nd6_llinfo_refresh(struct rtentry *);
212*43a90889SApple OSS Distributions static uint64_t ln_getexpire(struct llinfo_nd6 *);
213*43a90889SApple OSS Distributions 
214*43a90889SApple OSS Distributions static void nd6_service(void *);
215*43a90889SApple OSS Distributions static void nd6_slowtimo(void *);
216*43a90889SApple OSS Distributions static int nd6_is_new_addr_neighbor(struct sockaddr_in6 *, struct ifnet *);
217*43a90889SApple OSS Distributions 
218*43a90889SApple OSS Distributions static void nd6_router_select_rti_entries(struct ifnet *);
219*43a90889SApple OSS Distributions static void nd6_purge_interface_default_routers(struct ifnet *);
220*43a90889SApple OSS Distributions static void nd6_purge_interface_rti_entries(struct ifnet *);
221*43a90889SApple OSS Distributions static void nd6_purge_interface_prefixes(struct ifnet *);
222*43a90889SApple OSS Distributions static void nd6_purge_interface_llinfo(struct ifnet *);
223*43a90889SApple OSS Distributions 
224*43a90889SApple OSS Distributions static int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS;
225*43a90889SApple OSS Distributions static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS;
226*43a90889SApple OSS Distributions 
227*43a90889SApple OSS Distributions /*
228*43a90889SApple OSS Distributions  * Insertion and removal from llinfo_nd6 must be done with rnh_lock held.
229*43a90889SApple OSS Distributions  */
230*43a90889SApple OSS Distributions #define LN_DEQUEUE(_ln) do {                                            \
231*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);                 \
232*43a90889SApple OSS Distributions 	RT_LOCK_ASSERT_HELD((_ln)->ln_rt);                              \
233*43a90889SApple OSS Distributions 	(_ln)->ln_next->ln_prev = (_ln)->ln_prev;                       \
234*43a90889SApple OSS Distributions 	(_ln)->ln_prev->ln_next = (_ln)->ln_next;                       \
235*43a90889SApple OSS Distributions 	(_ln)->ln_prev = (_ln)->ln_next = NULL;                         \
236*43a90889SApple OSS Distributions 	(_ln)->ln_flags &= ~ND6_LNF_IN_USE;                             \
237*43a90889SApple OSS Distributions } while (0)
238*43a90889SApple OSS Distributions 
239*43a90889SApple OSS Distributions #define LN_INSERTHEAD(_ln) do {                                         \
240*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);                 \
241*43a90889SApple OSS Distributions 	RT_LOCK_ASSERT_HELD((_ln)->ln_rt);                              \
242*43a90889SApple OSS Distributions 	(_ln)->ln_next = llinfo_nd6.ln_next;                            \
243*43a90889SApple OSS Distributions 	llinfo_nd6.ln_next = (_ln);                                     \
244*43a90889SApple OSS Distributions 	(_ln)->ln_prev = &llinfo_nd6;                                   \
245*43a90889SApple OSS Distributions 	(_ln)->ln_next->ln_prev = (_ln);                                \
246*43a90889SApple OSS Distributions 	(_ln)->ln_flags |= ND6_LNF_IN_USE;                              \
247*43a90889SApple OSS Distributions } while (0)
248*43a90889SApple OSS Distributions 
249*43a90889SApple OSS Distributions static KALLOC_TYPE_DEFINE(llinfo_nd6_zone, struct llinfo_nd6, NET_KT_DEFAULT);
250*43a90889SApple OSS Distributions 
251*43a90889SApple OSS Distributions extern int tvtohz(struct timeval *);
252*43a90889SApple OSS Distributions 
253*43a90889SApple OSS Distributions static int nd6_init_done;
254*43a90889SApple OSS Distributions 
255*43a90889SApple OSS Distributions SYSCTL_DECL(_net_inet6_icmp6);
256*43a90889SApple OSS Distributions 
257*43a90889SApple OSS Distributions SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
258*43a90889SApple OSS Distributions     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
259*43a90889SApple OSS Distributions     nd6_sysctl_drlist, "S,in6_defrouter", "");
260*43a90889SApple OSS Distributions 
261*43a90889SApple OSS Distributions SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
262*43a90889SApple OSS Distributions     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
263*43a90889SApple OSS Distributions     nd6_sysctl_prlist, "S,in6_defrouter", "");
264*43a90889SApple OSS Distributions 
265*43a90889SApple OSS Distributions SYSCTL_DECL(_net_inet6_ip6);
266*43a90889SApple OSS Distributions 
267*43a90889SApple OSS Distributions static int ip6_maxchainsent = 0;
268*43a90889SApple OSS Distributions SYSCTL_INT(_net_inet6_ip6, OID_AUTO, maxchainsent,
269*43a90889SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxchainsent, 0,
270*43a90889SApple OSS Distributions     "use dlil_output_list");
271*43a90889SApple OSS Distributions 
272*43a90889SApple OSS Distributions SYSCTL_DECL(_net_inet6_icmp6);
273*43a90889SApple OSS Distributions int nd6_process_rti = ND6_PROCESS_RTI_DEFAULT;
274*43a90889SApple OSS Distributions 
275*43a90889SApple OSS Distributions SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_process_rti, CTLFLAG_RW | CTLFLAG_LOCKED,
276*43a90889SApple OSS Distributions     &nd6_process_rti, 0,
277*43a90889SApple OSS Distributions     "Enable/disable processing of Route Information Option in the "
278*43a90889SApple OSS Distributions     "IPv6 Router Advertisement.");
279*43a90889SApple OSS Distributions 
280*43a90889SApple OSS Distributions void
nd6_init(void)281*43a90889SApple OSS Distributions nd6_init(void)
282*43a90889SApple OSS Distributions {
283*43a90889SApple OSS Distributions 	int i;
284*43a90889SApple OSS Distributions 
285*43a90889SApple OSS Distributions 	VERIFY(!nd6_init_done);
286*43a90889SApple OSS Distributions 
287*43a90889SApple OSS Distributions 	all1_sa.sin6_family = AF_INET6;
288*43a90889SApple OSS Distributions 	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
289*43a90889SApple OSS Distributions 	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++) {
290*43a90889SApple OSS Distributions 		all1_sa.sin6_addr.s6_addr[i] = 0xff;
291*43a90889SApple OSS Distributions 	}
292*43a90889SApple OSS Distributions 
293*43a90889SApple OSS Distributions 	/* initialization of the default router list */
294*43a90889SApple OSS Distributions 	TAILQ_INIT(&nd_defrouter_list);
295*43a90889SApple OSS Distributions 	TAILQ_INIT(&nd_rti_list);
296*43a90889SApple OSS Distributions 
297*43a90889SApple OSS Distributions 	nd6_nbr_init();
298*43a90889SApple OSS Distributions 	nd6_rtr_init();
299*43a90889SApple OSS Distributions 
300*43a90889SApple OSS Distributions 	nd6_init_done = 1;
301*43a90889SApple OSS Distributions 
302*43a90889SApple OSS Distributions 	/* start timer */
303*43a90889SApple OSS Distributions 	timeout(nd6_slowtimo, NULL, ND6_SLOWTIMER_INTERVAL * hz);
304*43a90889SApple OSS Distributions }
305*43a90889SApple OSS Distributions 
306*43a90889SApple OSS Distributions static struct llinfo_nd6 *
nd6_llinfo_alloc(zalloc_flags_t how)307*43a90889SApple OSS Distributions nd6_llinfo_alloc(zalloc_flags_t how)
308*43a90889SApple OSS Distributions {
309*43a90889SApple OSS Distributions 	return zalloc_flags(llinfo_nd6_zone, how | Z_ZERO);
310*43a90889SApple OSS Distributions }
311*43a90889SApple OSS Distributions 
312*43a90889SApple OSS Distributions static void
nd6_llinfo_free(void * arg)313*43a90889SApple OSS Distributions nd6_llinfo_free(void *arg)
314*43a90889SApple OSS Distributions {
315*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = arg;
316*43a90889SApple OSS Distributions 
317*43a90889SApple OSS Distributions 	if (ln->ln_next != NULL || ln->ln_prev != NULL) {
318*43a90889SApple OSS Distributions 		panic("%s: trying to free %p when it is in use", __func__, ln);
319*43a90889SApple OSS Distributions 		/* NOTREACHED */
320*43a90889SApple OSS Distributions 	}
321*43a90889SApple OSS Distributions 
322*43a90889SApple OSS Distributions 	/* Just in case there's anything there, free it */
323*43a90889SApple OSS Distributions 	if (ln->ln_hold != NULL) {
324*43a90889SApple OSS Distributions 		m_freem_list(ln->ln_hold);
325*43a90889SApple OSS Distributions 		ln->ln_hold = NULL;
326*43a90889SApple OSS Distributions 	}
327*43a90889SApple OSS Distributions 
328*43a90889SApple OSS Distributions 	/* Purge any link-layer info caching */
329*43a90889SApple OSS Distributions 	VERIFY(ln->ln_rt->rt_llinfo == ln);
330*43a90889SApple OSS Distributions 	if (ln->ln_rt->rt_llinfo_purge != NULL) {
331*43a90889SApple OSS Distributions 		ln->ln_rt->rt_llinfo_purge(ln->ln_rt);
332*43a90889SApple OSS Distributions 	}
333*43a90889SApple OSS Distributions 
334*43a90889SApple OSS Distributions 	zfree(llinfo_nd6_zone, ln);
335*43a90889SApple OSS Distributions }
336*43a90889SApple OSS Distributions 
337*43a90889SApple OSS Distributions static void
nd6_llinfo_purge(struct rtentry * rt)338*43a90889SApple OSS Distributions nd6_llinfo_purge(struct rtentry *rt)
339*43a90889SApple OSS Distributions {
340*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = rt->rt_llinfo;
341*43a90889SApple OSS Distributions 
342*43a90889SApple OSS Distributions 	RT_LOCK_ASSERT_HELD(rt);
343*43a90889SApple OSS Distributions 	VERIFY(rt->rt_llinfo_purge == nd6_llinfo_purge && ln != NULL);
344*43a90889SApple OSS Distributions 
345*43a90889SApple OSS Distributions 	if (ln->ln_llreach != NULL) {
346*43a90889SApple OSS Distributions 		RT_CONVERT_LOCK(rt);
347*43a90889SApple OSS Distributions 		ifnet_llreach_free(ln->ln_llreach);
348*43a90889SApple OSS Distributions 		ln->ln_llreach = NULL;
349*43a90889SApple OSS Distributions 	}
350*43a90889SApple OSS Distributions 	ln->ln_lastused = 0;
351*43a90889SApple OSS Distributions }
352*43a90889SApple OSS Distributions 
353*43a90889SApple OSS Distributions static void
nd6_llinfo_get_ri(struct rtentry * rt,struct rt_reach_info * ri)354*43a90889SApple OSS Distributions nd6_llinfo_get_ri(struct rtentry *rt, struct rt_reach_info *ri)
355*43a90889SApple OSS Distributions {
356*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = rt->rt_llinfo;
357*43a90889SApple OSS Distributions 	struct if_llreach *__single lr = ln->ln_llreach;
358*43a90889SApple OSS Distributions 
359*43a90889SApple OSS Distributions 	if (lr == NULL) {
360*43a90889SApple OSS Distributions 		bzero(ri, sizeof(*ri));
361*43a90889SApple OSS Distributions 		ri->ri_rssi = IFNET_RSSI_UNKNOWN;
362*43a90889SApple OSS Distributions 		ri->ri_lqm = IFNET_LQM_THRESH_OFF;
363*43a90889SApple OSS Distributions 		ri->ri_npm = IFNET_NPM_THRESH_UNKNOWN;
364*43a90889SApple OSS Distributions 	} else {
365*43a90889SApple OSS Distributions 		IFLR_LOCK(lr);
366*43a90889SApple OSS Distributions 		/* Export to rt_reach_info structure */
367*43a90889SApple OSS Distributions 		ifnet_lr2ri(lr, ri);
368*43a90889SApple OSS Distributions 		/* Export ND6 send expiration (calendar) time */
369*43a90889SApple OSS Distributions 		ri->ri_snd_expire =
370*43a90889SApple OSS Distributions 		    ifnet_llreach_up2calexp(lr, ln->ln_lastused);
371*43a90889SApple OSS Distributions 		IFLR_UNLOCK(lr);
372*43a90889SApple OSS Distributions 	}
373*43a90889SApple OSS Distributions }
374*43a90889SApple OSS Distributions 
375*43a90889SApple OSS Distributions static void
nd6_llinfo_get_iflri(struct rtentry * rt,struct ifnet_llreach_info * iflri)376*43a90889SApple OSS Distributions nd6_llinfo_get_iflri(struct rtentry *rt, struct ifnet_llreach_info *iflri)
377*43a90889SApple OSS Distributions {
378*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = rt->rt_llinfo;
379*43a90889SApple OSS Distributions 	struct if_llreach *__single lr = ln->ln_llreach;
380*43a90889SApple OSS Distributions 
381*43a90889SApple OSS Distributions 	if (lr == NULL) {
382*43a90889SApple OSS Distributions 		bzero(iflri, sizeof(*iflri));
383*43a90889SApple OSS Distributions 		iflri->iflri_rssi = IFNET_RSSI_UNKNOWN;
384*43a90889SApple OSS Distributions 		iflri->iflri_lqm = IFNET_LQM_THRESH_OFF;
385*43a90889SApple OSS Distributions 		iflri->iflri_npm = IFNET_NPM_THRESH_UNKNOWN;
386*43a90889SApple OSS Distributions 	} else {
387*43a90889SApple OSS Distributions 		IFLR_LOCK(lr);
388*43a90889SApple OSS Distributions 		/* Export to ifnet_llreach_info structure */
389*43a90889SApple OSS Distributions 		ifnet_lr2iflri(lr, iflri);
390*43a90889SApple OSS Distributions 		/* Export ND6 send expiration (uptime) time */
391*43a90889SApple OSS Distributions 		iflri->iflri_snd_expire =
392*43a90889SApple OSS Distributions 		    ifnet_llreach_up2upexp(lr, ln->ln_lastused);
393*43a90889SApple OSS Distributions 		IFLR_UNLOCK(lr);
394*43a90889SApple OSS Distributions 	}
395*43a90889SApple OSS Distributions }
396*43a90889SApple OSS Distributions 
397*43a90889SApple OSS Distributions static void
nd6_llinfo_refresh(struct rtentry * rt)398*43a90889SApple OSS Distributions nd6_llinfo_refresh(struct rtentry *rt)
399*43a90889SApple OSS Distributions {
400*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = rt->rt_llinfo;
401*43a90889SApple OSS Distributions 	uint64_t timenow = net_uptime();
402*43a90889SApple OSS Distributions 	ifnet_ref_t ifp = rt->rt_ifp;
403*43a90889SApple OSS Distributions 	/*
404*43a90889SApple OSS Distributions 	 * Can't refresh permanent, static or entries that are
405*43a90889SApple OSS Distributions 	 * not direct host entries. Also skip if the entry is for
406*43a90889SApple OSS Distributions 	 * host over an interface that has alternate neighbor cache
407*43a90889SApple OSS Distributions 	 * management mechanisms (AWDL/NAN)
408*43a90889SApple OSS Distributions 	 */
409*43a90889SApple OSS Distributions 	if (!ln || ln->ln_expire == 0 || (rt->rt_flags & RTF_STATIC) ||
410*43a90889SApple OSS Distributions 	    !(rt->rt_flags & RTF_LLINFO) || !ifp ||
411*43a90889SApple OSS Distributions 	    (ifp->if_eflags & IFEF_IPV6_ND6ALT)) {
412*43a90889SApple OSS Distributions 		return;
413*43a90889SApple OSS Distributions 	}
414*43a90889SApple OSS Distributions 
415*43a90889SApple OSS Distributions 	if ((ln->ln_state > ND6_LLINFO_INCOMPLETE) &&
416*43a90889SApple OSS Distributions 	    (ln->ln_state < ND6_LLINFO_PROBE)) {
417*43a90889SApple OSS Distributions 		if (ln->ln_expire > timenow) {
418*43a90889SApple OSS Distributions 			ln_setexpire(ln, timenow);
419*43a90889SApple OSS Distributions 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_PROBE);
420*43a90889SApple OSS Distributions 		}
421*43a90889SApple OSS Distributions 	}
422*43a90889SApple OSS Distributions 	return;
423*43a90889SApple OSS Distributions }
424*43a90889SApple OSS Distributions 
425*43a90889SApple OSS Distributions const char *
ndcache_state2str(short ndp_state)426*43a90889SApple OSS Distributions ndcache_state2str(short ndp_state)
427*43a90889SApple OSS Distributions {
428*43a90889SApple OSS Distributions 	const char *__null_terminated ndp_state_str = "UNKNOWN";
429*43a90889SApple OSS Distributions 	switch (ndp_state) {
430*43a90889SApple OSS Distributions 	case ND6_LLINFO_PURGE:
431*43a90889SApple OSS Distributions 		ndp_state_str = "ND6_LLINFO_PURGE";
432*43a90889SApple OSS Distributions 		break;
433*43a90889SApple OSS Distributions 	case ND6_LLINFO_NOSTATE:
434*43a90889SApple OSS Distributions 		ndp_state_str = "ND6_LLINFO_NOSTATE";
435*43a90889SApple OSS Distributions 		break;
436*43a90889SApple OSS Distributions 	case ND6_LLINFO_INCOMPLETE:
437*43a90889SApple OSS Distributions 		ndp_state_str = "ND6_LLINFO_INCOMPLETE";
438*43a90889SApple OSS Distributions 		break;
439*43a90889SApple OSS Distributions 	case ND6_LLINFO_REACHABLE:
440*43a90889SApple OSS Distributions 		ndp_state_str = "ND6_LLINFO_REACHABLE";
441*43a90889SApple OSS Distributions 		break;
442*43a90889SApple OSS Distributions 	case ND6_LLINFO_STALE:
443*43a90889SApple OSS Distributions 		ndp_state_str = "ND6_LLINFO_STALE";
444*43a90889SApple OSS Distributions 		break;
445*43a90889SApple OSS Distributions 	case ND6_LLINFO_DELAY:
446*43a90889SApple OSS Distributions 		ndp_state_str = "ND6_LLINFO_DELAY";
447*43a90889SApple OSS Distributions 		break;
448*43a90889SApple OSS Distributions 	case ND6_LLINFO_PROBE:
449*43a90889SApple OSS Distributions 		ndp_state_str = "ND6_LLINFO_PROBE";
450*43a90889SApple OSS Distributions 		break;
451*43a90889SApple OSS Distributions 	default:
452*43a90889SApple OSS Distributions 		/* Init'd to UNKNOWN */
453*43a90889SApple OSS Distributions 		break;
454*43a90889SApple OSS Distributions 	}
455*43a90889SApple OSS Distributions 	return ndp_state_str;
456*43a90889SApple OSS Distributions }
457*43a90889SApple OSS Distributions 
458*43a90889SApple OSS Distributions void
ln_setexpire(struct llinfo_nd6 * ln,uint64_t expiry)459*43a90889SApple OSS Distributions ln_setexpire(struct llinfo_nd6 *ln, uint64_t expiry)
460*43a90889SApple OSS Distributions {
461*43a90889SApple OSS Distributions 	ln->ln_expire = expiry;
462*43a90889SApple OSS Distributions }
463*43a90889SApple OSS Distributions 
464*43a90889SApple OSS Distributions static uint64_t
ln_getexpire(struct llinfo_nd6 * ln)465*43a90889SApple OSS Distributions ln_getexpire(struct llinfo_nd6 *ln)
466*43a90889SApple OSS Distributions {
467*43a90889SApple OSS Distributions 	struct timeval caltime;
468*43a90889SApple OSS Distributions 	uint64_t expiry;
469*43a90889SApple OSS Distributions 
470*43a90889SApple OSS Distributions 	if (ln->ln_expire != 0) {
471*43a90889SApple OSS Distributions 		rtentry_ref_t rt = ln->ln_rt;
472*43a90889SApple OSS Distributions 
473*43a90889SApple OSS Distributions 		VERIFY(rt != NULL);
474*43a90889SApple OSS Distributions 		/* account for system time change */
475*43a90889SApple OSS Distributions 		getmicrotime(&caltime);
476*43a90889SApple OSS Distributions 
477*43a90889SApple OSS Distributions 		rt->base_calendartime +=
478*43a90889SApple OSS Distributions 		    NET_CALCULATE_CLOCKSKEW(caltime,
479*43a90889SApple OSS Distributions 		    rt->base_calendartime, net_uptime(), rt->base_uptime);
480*43a90889SApple OSS Distributions 
481*43a90889SApple OSS Distributions 		expiry = rt->base_calendartime +
482*43a90889SApple OSS Distributions 		    ln->ln_expire - rt->base_uptime;
483*43a90889SApple OSS Distributions 	} else {
484*43a90889SApple OSS Distributions 		expiry = 0;
485*43a90889SApple OSS Distributions 	}
486*43a90889SApple OSS Distributions 	return expiry;
487*43a90889SApple OSS Distributions }
488*43a90889SApple OSS Distributions 
489*43a90889SApple OSS Distributions void
nd6_ifreset(struct ifnet * ifp)490*43a90889SApple OSS Distributions nd6_ifreset(struct ifnet *ifp)
491*43a90889SApple OSS Distributions {
492*43a90889SApple OSS Distributions 	struct nd_ifinfo *__single ndi = ND_IFINFO(ifp);
493*43a90889SApple OSS Distributions 	VERIFY(NULL != ndi);
494*43a90889SApple OSS Distributions 	VERIFY(ndi->initialized);
495*43a90889SApple OSS Distributions 
496*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(&ndi->lock, LCK_MTX_ASSERT_OWNED);
497*43a90889SApple OSS Distributions 	ndi->linkmtu = ifp->if_mtu;
498*43a90889SApple OSS Distributions 	ndi->chlim = IPV6_DEFHLIM;
499*43a90889SApple OSS Distributions 	ndi->basereachable = REACHABLE_TIME;
500*43a90889SApple OSS Distributions 	ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
501*43a90889SApple OSS Distributions 	ndi->retrans = RETRANS_TIMER;
502*43a90889SApple OSS Distributions }
503*43a90889SApple OSS Distributions 
504*43a90889SApple OSS Distributions void
nd6_ifattach(struct ifnet * ifp)505*43a90889SApple OSS Distributions nd6_ifattach(struct ifnet *ifp)
506*43a90889SApple OSS Distributions {
507*43a90889SApple OSS Distributions 	struct nd_ifinfo *__single ndi = ND_IFINFO(ifp);
508*43a90889SApple OSS Distributions 	VERIFY(NULL != ndi);
509*43a90889SApple OSS Distributions 
510*43a90889SApple OSS Distributions 	if (!ndi->initialized) {
511*43a90889SApple OSS Distributions 		lck_mtx_init(&ndi->lock, &nd_if_lock_grp, &nd_if_lock_attr);
512*43a90889SApple OSS Distributions 		ndi->flags = ND6_IFF_PERFORMNUD;
513*43a90889SApple OSS Distributions 		ndi->flags |= ND6_IFF_DAD;
514*43a90889SApple OSS Distributions 		ndi->initialized = TRUE;
515*43a90889SApple OSS Distributions 	}
516*43a90889SApple OSS Distributions 
517*43a90889SApple OSS Distributions 	lck_mtx_lock(&ndi->lock);
518*43a90889SApple OSS Distributions 
519*43a90889SApple OSS Distributions 	if (!(ifp->if_flags & IFF_MULTICAST)) {
520*43a90889SApple OSS Distributions 		ndi->flags |= ND6_IFF_IFDISABLED;
521*43a90889SApple OSS Distributions 	}
522*43a90889SApple OSS Distributions 
523*43a90889SApple OSS Distributions 	nd6_ifreset(ifp);
524*43a90889SApple OSS Distributions 	lck_mtx_unlock(&ndi->lock);
525*43a90889SApple OSS Distributions 	nd6_setmtu(ifp);
526*43a90889SApple OSS Distributions 
527*43a90889SApple OSS Distributions 	nd6log0(info,
528*43a90889SApple OSS Distributions 	    "Reinit'd ND information for interface %s\n",
529*43a90889SApple OSS Distributions 	    if_name(ifp));
530*43a90889SApple OSS Distributions 	return;
531*43a90889SApple OSS Distributions }
532*43a90889SApple OSS Distributions 
533*43a90889SApple OSS Distributions #if 0
534*43a90889SApple OSS Distributions /*
535*43a90889SApple OSS Distributions  * XXX Look more into this. Especially since we recycle ifnets and do delayed
536*43a90889SApple OSS Distributions  * cleanup
537*43a90889SApple OSS Distributions  */
538*43a90889SApple OSS Distributions void
539*43a90889SApple OSS Distributions nd6_ifdetach(struct nd_ifinfo *nd)
540*43a90889SApple OSS Distributions {
541*43a90889SApple OSS Distributions 	/* XXX destroy nd's lock? */
542*43a90889SApple OSS Distributions 	FREE(nd, M_IP6NDP);
543*43a90889SApple OSS Distributions }
544*43a90889SApple OSS Distributions #endif
545*43a90889SApple OSS Distributions 
546*43a90889SApple OSS Distributions void
nd6_setmtu(struct ifnet * ifp)547*43a90889SApple OSS Distributions nd6_setmtu(struct ifnet *ifp)
548*43a90889SApple OSS Distributions {
549*43a90889SApple OSS Distributions 	struct nd_ifinfo *__single ndi = ND_IFINFO(ifp);
550*43a90889SApple OSS Distributions 	u_int32_t oldmaxmtu, maxmtu;
551*43a90889SApple OSS Distributions 
552*43a90889SApple OSS Distributions 	if ((NULL == ndi) || (FALSE == ndi->initialized)) {
553*43a90889SApple OSS Distributions 		return;
554*43a90889SApple OSS Distributions 	}
555*43a90889SApple OSS Distributions 
556*43a90889SApple OSS Distributions 	lck_mtx_lock(&ndi->lock);
557*43a90889SApple OSS Distributions 	oldmaxmtu = ndi->maxmtu;
558*43a90889SApple OSS Distributions 
559*43a90889SApple OSS Distributions 	/*
560*43a90889SApple OSS Distributions 	 * The ND level maxmtu is somewhat redundant to the interface MTU
561*43a90889SApple OSS Distributions 	 * and is an implementation artifact of KAME.  Instead of hard-
562*43a90889SApple OSS Distributions 	 * limiting the maxmtu based on the interface type here, we simply
563*43a90889SApple OSS Distributions 	 * take the if_mtu value since SIOCSIFMTU would have taken care of
564*43a90889SApple OSS Distributions 	 * the sanity checks related to the maximum MTU allowed for the
565*43a90889SApple OSS Distributions 	 * interface (a value that is known only by the interface layer),
566*43a90889SApple OSS Distributions 	 * by sending the request down via ifnet_ioctl().  The use of the
567*43a90889SApple OSS Distributions 	 * ND level maxmtu and linkmtu are done via IN6_LINKMTU() which
568*43a90889SApple OSS Distributions 	 * does further checking against if_mtu.
569*43a90889SApple OSS Distributions 	 */
570*43a90889SApple OSS Distributions 	maxmtu = ndi->maxmtu = ifp->if_mtu;
571*43a90889SApple OSS Distributions 
572*43a90889SApple OSS Distributions 	/*
573*43a90889SApple OSS Distributions 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
574*43a90889SApple OSS Distributions 	 * undesirable situation.  We thus notify the operator of the change
575*43a90889SApple OSS Distributions 	 * explicitly.  The check for oldmaxmtu is necessary to restrict the
576*43a90889SApple OSS Distributions 	 * log to the case of changing the MTU, not initializing it.
577*43a90889SApple OSS Distributions 	 */
578*43a90889SApple OSS Distributions 	if (oldmaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
579*43a90889SApple OSS Distributions 		log(LOG_NOTICE, "nd6_setmtu: "
580*43a90889SApple OSS Distributions 		    "new link MTU on %s (%u) is too small for IPv6\n",
581*43a90889SApple OSS Distributions 		    if_name(ifp), (uint32_t)ndi->maxmtu);
582*43a90889SApple OSS Distributions 	}
583*43a90889SApple OSS Distributions 	ndi->linkmtu = ifp->if_mtu;
584*43a90889SApple OSS Distributions 	lck_mtx_unlock(&ndi->lock);
585*43a90889SApple OSS Distributions 
586*43a90889SApple OSS Distributions 	/* also adjust in6_maxmtu if necessary. */
587*43a90889SApple OSS Distributions 	if (maxmtu > in6_maxmtu) {
588*43a90889SApple OSS Distributions 		in6_setmaxmtu();
589*43a90889SApple OSS Distributions 	}
590*43a90889SApple OSS Distributions }
591*43a90889SApple OSS Distributions 
592*43a90889SApple OSS Distributions void
nd6_option_init(void * __sized_by (icmp6len)opt,size_t icmp6len,union nd_opts * ndopts)593*43a90889SApple OSS Distributions nd6_option_init(void *__sized_by(icmp6len) opt, size_t icmp6len, union nd_opts *ndopts)
594*43a90889SApple OSS Distributions {
595*43a90889SApple OSS Distributions 	bzero(ndopts, sizeof(*ndopts));
596*43a90889SApple OSS Distributions 	if (icmp6len > 0) {
597*43a90889SApple OSS Distributions 		ndopts->nd_opts_done = 0;
598*43a90889SApple OSS Distributions 		ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
599*43a90889SApple OSS Distributions 		ndopts->nd_opts_last = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
600*43a90889SApple OSS Distributions 	} else {
601*43a90889SApple OSS Distributions 		ndopts->nd_opts_done = 1;
602*43a90889SApple OSS Distributions 		ndopts->nd_opts_search = NULL;
603*43a90889SApple OSS Distributions 		ndopts->nd_opts_last = NULL;
604*43a90889SApple OSS Distributions 	}
605*43a90889SApple OSS Distributions 	ndopts->nd_opts_initialized = 1;
606*43a90889SApple OSS Distributions }
607*43a90889SApple OSS Distributions 
608*43a90889SApple OSS Distributions /*
609*43a90889SApple OSS Distributions  * Take one ND option.
610*43a90889SApple OSS Distributions  */
611*43a90889SApple OSS Distributions struct nd_opt_hdr *
nd6_option(union nd_opts * ndopts)612*43a90889SApple OSS Distributions nd6_option(union nd_opts *ndopts)
613*43a90889SApple OSS Distributions {
614*43a90889SApple OSS Distributions 	struct nd_opt_hdr *nd_opt;
615*43a90889SApple OSS Distributions 	int olen;
616*43a90889SApple OSS Distributions 
617*43a90889SApple OSS Distributions 	if (!ndopts) {
618*43a90889SApple OSS Distributions 		panic("ndopts == NULL in nd6_option");
619*43a90889SApple OSS Distributions 	}
620*43a90889SApple OSS Distributions 	if (ndopts->nd_opts_initialized != 1) {
621*43a90889SApple OSS Distributions 		panic("uninitialized ndopts in nd6_option");
622*43a90889SApple OSS Distributions 	}
623*43a90889SApple OSS Distributions 	if (!ndopts->nd_opts_search) {
624*43a90889SApple OSS Distributions 		return NULL;
625*43a90889SApple OSS Distributions 	}
626*43a90889SApple OSS Distributions 	if (ndopts->nd_opts_done) {
627*43a90889SApple OSS Distributions 		return NULL;
628*43a90889SApple OSS Distributions 	}
629*43a90889SApple OSS Distributions 
630*43a90889SApple OSS Distributions 	nd_opt = ndopts->nd_opts_search;
631*43a90889SApple OSS Distributions 
632*43a90889SApple OSS Distributions 	/* make sure nd_opt_len is inside the buffer */
633*43a90889SApple OSS Distributions 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
634*43a90889SApple OSS Distributions 		bzero(ndopts, sizeof(*ndopts));
635*43a90889SApple OSS Distributions 		return NULL;
636*43a90889SApple OSS Distributions 	}
637*43a90889SApple OSS Distributions 
638*43a90889SApple OSS Distributions 	olen = nd_opt->nd_opt_len << 3;
639*43a90889SApple OSS Distributions 	if (olen == 0) {
640*43a90889SApple OSS Distributions 		/*
641*43a90889SApple OSS Distributions 		 * Message validation requires that all included
642*43a90889SApple OSS Distributions 		 * options have a length that is greater than zero.
643*43a90889SApple OSS Distributions 		 */
644*43a90889SApple OSS Distributions 		bzero(ndopts, sizeof(*ndopts));
645*43a90889SApple OSS Distributions 		return NULL;
646*43a90889SApple OSS Distributions 	}
647*43a90889SApple OSS Distributions 
648*43a90889SApple OSS Distributions 	VERIFY(ndopts->nd_opts_last >= ndopts->nd_opts_search);
649*43a90889SApple OSS Distributions 	size_t remaining = (caddr_t)ndopts->nd_opts_last - (caddr_t)ndopts->nd_opts_search;
650*43a90889SApple OSS Distributions 
651*43a90889SApple OSS Distributions 	if (olen > remaining) {
652*43a90889SApple OSS Distributions 		/* option overruns the end of buffer, invalid */
653*43a90889SApple OSS Distributions 		bzero(ndopts, sizeof(*ndopts));
654*43a90889SApple OSS Distributions 		return NULL;
655*43a90889SApple OSS Distributions 	} else if (olen == remaining) {
656*43a90889SApple OSS Distributions 		/* reached the end of options chain */
657*43a90889SApple OSS Distributions 		ndopts->nd_opts_done = 1;
658*43a90889SApple OSS Distributions 	} else {
659*43a90889SApple OSS Distributions 		/* more options */
660*43a90889SApple OSS Distributions 		ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
661*43a90889SApple OSS Distributions 		ndopts->nd_opts_last = ndopts->nd_opts_last;
662*43a90889SApple OSS Distributions 	}
663*43a90889SApple OSS Distributions 
664*43a90889SApple OSS Distributions 	return nd_opt;
665*43a90889SApple OSS Distributions }
666*43a90889SApple OSS Distributions 
667*43a90889SApple OSS Distributions /*
668*43a90889SApple OSS Distributions  * Parse multiple ND options.
669*43a90889SApple OSS Distributions  * This function is much easier to use, for ND routines that do not need
670*43a90889SApple OSS Distributions  * multiple options of the same type.
671*43a90889SApple OSS Distributions  */
672*43a90889SApple OSS Distributions int
nd6_options(union nd_opts * ndopts)673*43a90889SApple OSS Distributions nd6_options(union nd_opts *ndopts)
674*43a90889SApple OSS Distributions {
675*43a90889SApple OSS Distributions 	struct nd_opt_hdr *__single nd_opt;
676*43a90889SApple OSS Distributions 	int i = 0;
677*43a90889SApple OSS Distributions 
678*43a90889SApple OSS Distributions 	if (ndopts == NULL) {
679*43a90889SApple OSS Distributions 		panic("ndopts == NULL in nd6_options");
680*43a90889SApple OSS Distributions 	}
681*43a90889SApple OSS Distributions 	if (ndopts->nd_opts_initialized != 1) {
682*43a90889SApple OSS Distributions 		panic("uninitialized ndopts in nd6_options");
683*43a90889SApple OSS Distributions 	}
684*43a90889SApple OSS Distributions 	if (ndopts->nd_opts_search == NULL) {
685*43a90889SApple OSS Distributions 		return 0;
686*43a90889SApple OSS Distributions 	}
687*43a90889SApple OSS Distributions 
688*43a90889SApple OSS Distributions 	while (1) {
689*43a90889SApple OSS Distributions 		nd_opt = nd6_option(ndopts);
690*43a90889SApple OSS Distributions 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
691*43a90889SApple OSS Distributions 			/*
692*43a90889SApple OSS Distributions 			 * Message validation requires that all included
693*43a90889SApple OSS Distributions 			 * options have a length that is greater than zero.
694*43a90889SApple OSS Distributions 			 */
695*43a90889SApple OSS Distributions 			icmp6stat.icp6s_nd_badopt++;
696*43a90889SApple OSS Distributions 			bzero(ndopts, sizeof(*ndopts));
697*43a90889SApple OSS Distributions 			return -1;
698*43a90889SApple OSS Distributions 		}
699*43a90889SApple OSS Distributions 
700*43a90889SApple OSS Distributions 		if (nd_opt == NULL) {
701*43a90889SApple OSS Distributions 			goto skip1;
702*43a90889SApple OSS Distributions 		}
703*43a90889SApple OSS Distributions 
704*43a90889SApple OSS Distributions 		switch (nd_opt->nd_opt_type) {
705*43a90889SApple OSS Distributions 		case ND_OPT_SOURCE_LINKADDR:
706*43a90889SApple OSS Distributions 		case ND_OPT_TARGET_LINKADDR:
707*43a90889SApple OSS Distributions 		case ND_OPT_MTU:
708*43a90889SApple OSS Distributions 		case ND_OPT_REDIRECTED_HEADER:
709*43a90889SApple OSS Distributions 		case ND_OPT_NONCE:
710*43a90889SApple OSS Distributions 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
711*43a90889SApple OSS Distributions 				nd6log(error,
712*43a90889SApple OSS Distributions 				    "duplicated ND6 option found (type=%d)\n",
713*43a90889SApple OSS Distributions 				    nd_opt->nd_opt_type);
714*43a90889SApple OSS Distributions 				/* XXX bark? */
715*43a90889SApple OSS Distributions 			} else {
716*43a90889SApple OSS Distributions 				ndopts->nd_opt_array[nd_opt->nd_opt_type] =
717*43a90889SApple OSS Distributions 				    nd_opt;
718*43a90889SApple OSS Distributions 			}
719*43a90889SApple OSS Distributions 			break;
720*43a90889SApple OSS Distributions 		case ND_OPT_PREFIX_INFORMATION:
721*43a90889SApple OSS Distributions 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
722*43a90889SApple OSS Distributions 				ndopts->nd_opt_array[nd_opt->nd_opt_type] =
723*43a90889SApple OSS Distributions 				    nd_opt;
724*43a90889SApple OSS Distributions 			}
725*43a90889SApple OSS Distributions 			ndopts->nd_opts_pi_end =
726*43a90889SApple OSS Distributions 			    (struct nd_opt_prefix_info *)nd_opt;
727*43a90889SApple OSS Distributions 			break;
728*43a90889SApple OSS Distributions 		case ND_OPT_PVD:
729*43a90889SApple OSS Distributions 		case ND_OPT_RDNSS:
730*43a90889SApple OSS Distributions 		case ND_OPT_DNSSL:
731*43a90889SApple OSS Distributions 		case ND_OPT_CAPTIVE_PORTAL:
732*43a90889SApple OSS Distributions 			/* ignore */
733*43a90889SApple OSS Distributions 			break;
734*43a90889SApple OSS Distributions 		case ND_OPT_ROUTE_INFO:
735*43a90889SApple OSS Distributions 			if (nd6_process_rti) {
736*43a90889SApple OSS Distributions 				if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
737*43a90889SApple OSS Distributions 					ndopts->nd_opt_array[nd_opt->nd_opt_type]
738*43a90889SApple OSS Distributions 					        = nd_opt;
739*43a90889SApple OSS Distributions 				}
740*43a90889SApple OSS Distributions 				ndopts->nd_opts_rti_end =
741*43a90889SApple OSS Distributions 				    (struct nd_opt_route_info *)nd_opt;
742*43a90889SApple OSS Distributions 				break;
743*43a90889SApple OSS Distributions 			}
744*43a90889SApple OSS Distributions 			OS_FALLTHROUGH;
745*43a90889SApple OSS Distributions 		default:
746*43a90889SApple OSS Distributions 			/*
747*43a90889SApple OSS Distributions 			 * Unknown options must be silently ignored,
748*43a90889SApple OSS Distributions 			 * to accommodate future extensions to the protocol.
749*43a90889SApple OSS Distributions 			 */
750*43a90889SApple OSS Distributions 			nd6log2(debug,
751*43a90889SApple OSS Distributions 			    "nd6_options: unsupported option %d - "
752*43a90889SApple OSS Distributions 			    "option ignored\n", nd_opt->nd_opt_type);
753*43a90889SApple OSS Distributions 		}
754*43a90889SApple OSS Distributions 
755*43a90889SApple OSS Distributions skip1:
756*43a90889SApple OSS Distributions 		i++;
757*43a90889SApple OSS Distributions 		if (i > nd6_maxndopt) {
758*43a90889SApple OSS Distributions 			icmp6stat.icp6s_nd_toomanyopt++;
759*43a90889SApple OSS Distributions 			nd6log(info, "too many loop in nd opt\n");
760*43a90889SApple OSS Distributions 			break;
761*43a90889SApple OSS Distributions 		}
762*43a90889SApple OSS Distributions 
763*43a90889SApple OSS Distributions 		if (ndopts->nd_opts_done) {
764*43a90889SApple OSS Distributions 			break;
765*43a90889SApple OSS Distributions 		}
766*43a90889SApple OSS Distributions 	}
767*43a90889SApple OSS Distributions 
768*43a90889SApple OSS Distributions 	return 0;
769*43a90889SApple OSS Distributions }
770*43a90889SApple OSS Distributions 
771*43a90889SApple OSS Distributions struct nd6svc_arg {
772*43a90889SApple OSS Distributions 	int draining;
773*43a90889SApple OSS Distributions 	uint32_t killed;
774*43a90889SApple OSS Distributions 	uint32_t aging_lazy;
775*43a90889SApple OSS Distributions 	uint32_t aging;
776*43a90889SApple OSS Distributions 	uint32_t sticky;
777*43a90889SApple OSS Distributions 	uint32_t found;
778*43a90889SApple OSS Distributions };
779*43a90889SApple OSS Distributions 
780*43a90889SApple OSS Distributions 
781*43a90889SApple OSS Distributions static void
nd6_service_neighbor_cache(struct nd6svc_arg * ap,uint64_t timenow)782*43a90889SApple OSS Distributions nd6_service_neighbor_cache(struct nd6svc_arg *ap, uint64_t timenow)
783*43a90889SApple OSS Distributions {
784*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln;
785*43a90889SApple OSS Distributions 	ifnet_ref_t ifp = NULL;
786*43a90889SApple OSS Distributions 	boolean_t send_nc_failure_kev = FALSE;
787*43a90889SApple OSS Distributions 	radix_node_head_ref_t rnh = rt_tables[AF_INET6];
788*43a90889SApple OSS Distributions 
789*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
790*43a90889SApple OSS Distributions again:
791*43a90889SApple OSS Distributions 	/*
792*43a90889SApple OSS Distributions 	 * send_nc_failure_kev gets set when default router's IPv6 address
793*43a90889SApple OSS Distributions 	 * can't be resolved.
794*43a90889SApple OSS Distributions 	 * That can happen either:
795*43a90889SApple OSS Distributions 	 * 1. When the entry has resolved once but can't be
796*43a90889SApple OSS Distributions 	 * resolved later and the neighbor cache entry for gateway is deleted
797*43a90889SApple OSS Distributions 	 * after max probe attempts.
798*43a90889SApple OSS Distributions 	 *
799*43a90889SApple OSS Distributions 	 * 2. When the entry is in ND6_LLINFO_INCOMPLETE but can not be resolved
800*43a90889SApple OSS Distributions 	 * after max neighbor address resolution attempts.
801*43a90889SApple OSS Distributions 	 *
802*43a90889SApple OSS Distributions 	 * Both set send_nc_failure_kev to true. ifp is also set to the previous
803*43a90889SApple OSS Distributions 	 * neighbor cache entry's route's ifp.
804*43a90889SApple OSS Distributions 	 * Once we are done sending the notification, set send_nc_failure_kev
805*43a90889SApple OSS Distributions 	 * to false to stop sending false notifications for non default router
806*43a90889SApple OSS Distributions 	 * neighbors.
807*43a90889SApple OSS Distributions 	 *
808*43a90889SApple OSS Distributions 	 * We may to send more information like Gateway's IP that could not be
809*43a90889SApple OSS Distributions 	 * resolved, however right now we do not install more than one default
810*43a90889SApple OSS Distributions 	 * route per interface in the routing table.
811*43a90889SApple OSS Distributions 	 */
812*43a90889SApple OSS Distributions 	if (send_nc_failure_kev && ifp != NULL &&
813*43a90889SApple OSS Distributions 	    ifp->if_addrlen == IF_LLREACH_MAXLEN) {
814*43a90889SApple OSS Distributions 		struct kev_msg ev_msg;
815*43a90889SApple OSS Distributions 		struct kev_nd6_ndfailure nd6_ndfailure;
816*43a90889SApple OSS Distributions 		bzero(&ev_msg, sizeof(ev_msg));
817*43a90889SApple OSS Distributions 		bzero(&nd6_ndfailure, sizeof(nd6_ndfailure));
818*43a90889SApple OSS Distributions 		ev_msg.vendor_code      = KEV_VENDOR_APPLE;
819*43a90889SApple OSS Distributions 		ev_msg.kev_class        = KEV_NETWORK_CLASS;
820*43a90889SApple OSS Distributions 		ev_msg.kev_subclass     = KEV_ND6_SUBCLASS;
821*43a90889SApple OSS Distributions 		ev_msg.event_code       = KEV_ND6_NDFAILURE;
822*43a90889SApple OSS Distributions 
823*43a90889SApple OSS Distributions 		nd6_ndfailure.link_data.if_family = ifp->if_family;
824*43a90889SApple OSS Distributions 		nd6_ndfailure.link_data.if_unit = ifp->if_unit;
825*43a90889SApple OSS Distributions 		strlcpy(nd6_ndfailure.link_data.if_name,
826*43a90889SApple OSS Distributions 		    ifp->if_name,
827*43a90889SApple OSS Distributions 		    sizeof(nd6_ndfailure.link_data.if_name));
828*43a90889SApple OSS Distributions 		ev_msg.dv[0].data_ptr = &nd6_ndfailure;
829*43a90889SApple OSS Distributions 		ev_msg.dv[0].data_length =
830*43a90889SApple OSS Distributions 		    sizeof(nd6_ndfailure);
831*43a90889SApple OSS Distributions 		dlil_post_complete_msg(NULL, &ev_msg);
832*43a90889SApple OSS Distributions 	}
833*43a90889SApple OSS Distributions 
834*43a90889SApple OSS Distributions 	send_nc_failure_kev = FALSE;
835*43a90889SApple OSS Distributions 	ifp = NULL;
836*43a90889SApple OSS Distributions 	/*
837*43a90889SApple OSS Distributions 	 * The global list llinfo_nd6 is modified by nd6_request() and is
838*43a90889SApple OSS Distributions 	 * therefore protected by rnh_lock.  For obvious reasons, we cannot
839*43a90889SApple OSS Distributions 	 * hold rnh_lock across calls that might lead to code paths which
840*43a90889SApple OSS Distributions 	 * attempt to acquire rnh_lock, else we deadlock.  Hence for such
841*43a90889SApple OSS Distributions 	 * cases we drop rt_lock and rnh_lock, make the calls, and repeat the
842*43a90889SApple OSS Distributions 	 * loop.  To ensure that we don't process the same entry more than
843*43a90889SApple OSS Distributions 	 * once in a single timeout, we mark the "already-seen" entries with
844*43a90889SApple OSS Distributions 	 * ND6_LNF_TIMER_SKIP flag.  At the end of the loop, we do a second
845*43a90889SApple OSS Distributions 	 * pass thru the entries and clear the flag so they can be processed
846*43a90889SApple OSS Distributions 	 * during the next timeout.
847*43a90889SApple OSS Distributions 	 */
848*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
849*43a90889SApple OSS Distributions 
850*43a90889SApple OSS Distributions 	ln = llinfo_nd6.ln_next;
851*43a90889SApple OSS Distributions 	while (ln != NULL && ln != &llinfo_nd6) {
852*43a90889SApple OSS Distributions 		rtentry_ref_t rt;
853*43a90889SApple OSS Distributions 		struct sockaddr_in6 *__single dst;
854*43a90889SApple OSS Distributions 		struct llinfo_nd6 *__single next;
855*43a90889SApple OSS Distributions 		u_int32_t retrans, flags;
856*43a90889SApple OSS Distributions 		struct nd_ifinfo *__single ndi = NULL;
857*43a90889SApple OSS Distributions 		boolean_t is_router = FALSE;
858*43a90889SApple OSS Distributions 
859*43a90889SApple OSS Distributions 		/* ln_next/prev/rt is protected by rnh_lock */
860*43a90889SApple OSS Distributions 		next = ln->ln_next;
861*43a90889SApple OSS Distributions 		rt = ln->ln_rt;
862*43a90889SApple OSS Distributions 		RT_LOCK(rt);
863*43a90889SApple OSS Distributions 
864*43a90889SApple OSS Distributions 		/* We've seen this already; skip it */
865*43a90889SApple OSS Distributions 		if (ln->ln_flags & ND6_LNF_TIMER_SKIP) {
866*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
867*43a90889SApple OSS Distributions 			ln = next;
868*43a90889SApple OSS Distributions 			continue;
869*43a90889SApple OSS Distributions 		}
870*43a90889SApple OSS Distributions 		ap->found++;
871*43a90889SApple OSS Distributions 
872*43a90889SApple OSS Distributions 		/* rt->rt_ifp should never be NULL */
873*43a90889SApple OSS Distributions 		if ((ifp = rt->rt_ifp) == NULL) {
874*43a90889SApple OSS Distributions 			panic("%s: ln(%p) rt(%p) rt_ifp == NULL", __func__,
875*43a90889SApple OSS Distributions 			    ln, rt);
876*43a90889SApple OSS Distributions 			/* NOTREACHED */
877*43a90889SApple OSS Distributions 		}
878*43a90889SApple OSS Distributions 
879*43a90889SApple OSS Distributions 		/* rt_llinfo must always be equal to ln */
880*43a90889SApple OSS Distributions 		if ((struct llinfo_nd6 *)rt->rt_llinfo != ln) {
881*43a90889SApple OSS Distributions 			panic("%s: rt_llinfo(%p) is not equal to ln(%p)",
882*43a90889SApple OSS Distributions 			    __func__, rt->rt_llinfo, ln);
883*43a90889SApple OSS Distributions 			/* NOTREACHED */
884*43a90889SApple OSS Distributions 		}
885*43a90889SApple OSS Distributions 
886*43a90889SApple OSS Distributions 		/* rt_key should never be NULL */
887*43a90889SApple OSS Distributions 		dst = SIN6(rt_key(rt));
888*43a90889SApple OSS Distributions 		if (dst == NULL) {
889*43a90889SApple OSS Distributions 			panic("%s: rt(%p) key is NULL ln(%p)", __func__,
890*43a90889SApple OSS Distributions 			    rt, ln);
891*43a90889SApple OSS Distributions 			/* NOTREACHED */
892*43a90889SApple OSS Distributions 		}
893*43a90889SApple OSS Distributions 
894*43a90889SApple OSS Distributions 		/* Set the flag in case we jump to "again" */
895*43a90889SApple OSS Distributions 		ln->ln_flags |= ND6_LNF_TIMER_SKIP;
896*43a90889SApple OSS Distributions 
897*43a90889SApple OSS Distributions 		/*
898*43a90889SApple OSS Distributions 		 * Do not touch neighbor cache entries that are permanent,
899*43a90889SApple OSS Distributions 		 * static or are for interfaces that manage neighbor cache
900*43a90889SApple OSS Distributions 		 * entries via alternate NDP means.
901*43a90889SApple OSS Distributions 		 */
902*43a90889SApple OSS Distributions 		if (ln->ln_expire == 0 || (rt->rt_flags & RTF_STATIC) ||
903*43a90889SApple OSS Distributions 		    (rt->rt_ifp->if_eflags & IFEF_IPV6_ND6ALT)) {
904*43a90889SApple OSS Distributions 			ap->sticky++;
905*43a90889SApple OSS Distributions 		} else if (ap->draining && (rt->rt_refcnt == 0)) {
906*43a90889SApple OSS Distributions 			/*
907*43a90889SApple OSS Distributions 			 * If we are draining, immediately purge non-static
908*43a90889SApple OSS Distributions 			 * entries without oustanding route refcnt.
909*43a90889SApple OSS Distributions 			 */
910*43a90889SApple OSS Distributions 			if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
911*43a90889SApple OSS Distributions 				ND6_CACHE_STATE_TRANSITION(ln, (short)ND6_LLINFO_STALE);
912*43a90889SApple OSS Distributions 			} else {
913*43a90889SApple OSS Distributions 				ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_PURGE);
914*43a90889SApple OSS Distributions 			}
915*43a90889SApple OSS Distributions 			ln_setexpire(ln, timenow);
916*43a90889SApple OSS Distributions 		}
917*43a90889SApple OSS Distributions 
918*43a90889SApple OSS Distributions 		/*
919*43a90889SApple OSS Distributions 		 * If the entry has not expired, skip it. Take note on the
920*43a90889SApple OSS Distributions 		 * state, as entries that are in the STALE state are simply
921*43a90889SApple OSS Distributions 		 * waiting to be garbage collected, in which case we can
922*43a90889SApple OSS Distributions 		 * relax the callout scheduling (use nd6_prune_lazy).
923*43a90889SApple OSS Distributions 		 */
924*43a90889SApple OSS Distributions 		if (ln->ln_expire > timenow) {
925*43a90889SApple OSS Distributions 			switch (ln->ln_state) {
926*43a90889SApple OSS Distributions 			case ND6_LLINFO_STALE:
927*43a90889SApple OSS Distributions 				ap->aging_lazy++;
928*43a90889SApple OSS Distributions 				break;
929*43a90889SApple OSS Distributions 			default:
930*43a90889SApple OSS Distributions 				ap->aging++;
931*43a90889SApple OSS Distributions 				break;
932*43a90889SApple OSS Distributions 			}
933*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
934*43a90889SApple OSS Distributions 			ln = next;
935*43a90889SApple OSS Distributions 			continue;
936*43a90889SApple OSS Distributions 		}
937*43a90889SApple OSS Distributions 
938*43a90889SApple OSS Distributions 		ndi = ND_IFINFO(ifp);
939*43a90889SApple OSS Distributions 		/*
940*43a90889SApple OSS Distributions 		 * The IPv6 initialization of the loopback interface
941*43a90889SApple OSS Distributions 		 * may happen after another interface gets assigned
942*43a90889SApple OSS Distributions 		 * an IPv6 address
943*43a90889SApple OSS Distributions 		 */
944*43a90889SApple OSS Distributions 		if (ndi == NULL && ifp == lo_ifp) {
945*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
946*43a90889SApple OSS Distributions 			ln = next;
947*43a90889SApple OSS Distributions 			continue;
948*43a90889SApple OSS Distributions 		}
949*43a90889SApple OSS Distributions 		VERIFY(ndi->initialized);
950*43a90889SApple OSS Distributions 		retrans = ndi->retrans;
951*43a90889SApple OSS Distributions 		flags = ndi->flags;
952*43a90889SApple OSS Distributions 
953*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_HELD(rt);
954*43a90889SApple OSS Distributions 		is_router = (rt->rt_flags & RTF_ROUTER) ? TRUE : FALSE;
955*43a90889SApple OSS Distributions 
956*43a90889SApple OSS Distributions 		switch (ln->ln_state) {
957*43a90889SApple OSS Distributions 		case ND6_LLINFO_INCOMPLETE:
958*43a90889SApple OSS Distributions 			if (ln->ln_asked < nd6_mmaxtries) {
959*43a90889SApple OSS Distributions 				ifnet_ref_t exclifp = ln->ln_exclifp;
960*43a90889SApple OSS Distributions 				ln->ln_asked++;
961*43a90889SApple OSS Distributions 				ln_setexpire(ln, timenow + retrans / 1000);
962*43a90889SApple OSS Distributions 				RT_ADDREF_LOCKED(rt);
963*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
964*43a90889SApple OSS Distributions 				lck_mtx_unlock(rnh_lock);
965*43a90889SApple OSS Distributions 				if (ip6_forwarding) {
966*43a90889SApple OSS Distributions 					nd6_prproxy_ns_output(ifp, exclifp,
967*43a90889SApple OSS Distributions 					    NULL, &dst->sin6_addr, ln);
968*43a90889SApple OSS Distributions 				} else {
969*43a90889SApple OSS Distributions 					nd6_ns_output(ifp, NULL,
970*43a90889SApple OSS Distributions 					    &dst->sin6_addr, ln, NULL, 0);
971*43a90889SApple OSS Distributions 				}
972*43a90889SApple OSS Distributions 				RT_REMREF(rt);
973*43a90889SApple OSS Distributions 				ap->aging++;
974*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
975*43a90889SApple OSS Distributions 			} else {
976*43a90889SApple OSS Distributions 				mbuf_ref_t m = ln->ln_hold;
977*43a90889SApple OSS Distributions 				ln->ln_hold = NULL;
978*43a90889SApple OSS Distributions 				send_nc_failure_kev = is_router;
979*43a90889SApple OSS Distributions 				if (m != NULL) {
980*43a90889SApple OSS Distributions 					RT_ADDREF_LOCKED(rt);
981*43a90889SApple OSS Distributions 					RT_UNLOCK(rt);
982*43a90889SApple OSS Distributions 					lck_mtx_unlock(rnh_lock);
983*43a90889SApple OSS Distributions 
984*43a90889SApple OSS Distributions 					mbuf_ref_t mnext;
985*43a90889SApple OSS Distributions 					while (m) {
986*43a90889SApple OSS Distributions 						mnext = m->m_nextpkt;
987*43a90889SApple OSS Distributions 						m->m_nextpkt = NULL;
988*43a90889SApple OSS Distributions 						m->m_pkthdr.rcvif = ifp;
989*43a90889SApple OSS Distributions 						icmp6_error_flag(m, ICMP6_DST_UNREACH,
990*43a90889SApple OSS Distributions 						    ICMP6_DST_UNREACH_ADDR, 0, 0);
991*43a90889SApple OSS Distributions 						m = mnext;
992*43a90889SApple OSS Distributions 					}
993*43a90889SApple OSS Distributions 				} else {
994*43a90889SApple OSS Distributions 					RT_ADDREF_LOCKED(rt);
995*43a90889SApple OSS Distributions 					RT_UNLOCK(rt);
996*43a90889SApple OSS Distributions 					lck_mtx_unlock(rnh_lock);
997*43a90889SApple OSS Distributions 				}
998*43a90889SApple OSS Distributions 
999*43a90889SApple OSS Distributions 				/*
1000*43a90889SApple OSS Distributions 				 * Enqueue work item to invoke callback for
1001*43a90889SApple OSS Distributions 				 * this route entry
1002*43a90889SApple OSS Distributions 				 */
1003*43a90889SApple OSS Distributions 				route_event_enqueue_nwk_wq_entry(rt, NULL,
1004*43a90889SApple OSS Distributions 				    ROUTE_LLENTRY_UNREACH, NULL, FALSE);
1005*43a90889SApple OSS Distributions 				defrouter_set_reachability(&SIN6(rt_key(rt))->sin6_addr, rt->rt_ifp,
1006*43a90889SApple OSS Distributions 				    FALSE);
1007*43a90889SApple OSS Distributions 				nd6_free(rt);
1008*43a90889SApple OSS Distributions 				ap->killed++;
1009*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
1010*43a90889SApple OSS Distributions 				/*
1011*43a90889SApple OSS Distributions 				 * nd6_free above would flush out the routing table of
1012*43a90889SApple OSS Distributions 				 * any cloned routes with same next-hop.
1013*43a90889SApple OSS Distributions 				 * Walk the tree anyways as there could be static routes
1014*43a90889SApple OSS Distributions 				 * left.
1015*43a90889SApple OSS Distributions 				 *
1016*43a90889SApple OSS Distributions 				 * We also already have a reference to rt that gets freed right
1017*43a90889SApple OSS Distributions 				 * after the block below executes. Don't need an extra reference
1018*43a90889SApple OSS Distributions 				 * on rt here.
1019*43a90889SApple OSS Distributions 				 */
1020*43a90889SApple OSS Distributions 				if (is_router) {
1021*43a90889SApple OSS Distributions 					struct route_event rt_ev;
1022*43a90889SApple OSS Distributions 					route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_UNREACH);
1023*43a90889SApple OSS Distributions 					(void) rnh->rnh_walktree(rnh, route_event_walktree, (void *)&rt_ev);
1024*43a90889SApple OSS Distributions 				}
1025*43a90889SApple OSS Distributions 				rtfree_locked(rt);
1026*43a90889SApple OSS Distributions 			}
1027*43a90889SApple OSS Distributions 			LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1028*43a90889SApple OSS Distributions 			goto again;
1029*43a90889SApple OSS Distributions 
1030*43a90889SApple OSS Distributions 		case ND6_LLINFO_REACHABLE:
1031*43a90889SApple OSS Distributions 			if (ln->ln_expire != 0) {
1032*43a90889SApple OSS Distributions 				ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
1033*43a90889SApple OSS Distributions 				ln_setexpire(ln, timenow + nd6_gctimer);
1034*43a90889SApple OSS Distributions 				ap->aging_lazy++;
1035*43a90889SApple OSS Distributions 				/*
1036*43a90889SApple OSS Distributions 				 * Enqueue work item to invoke callback for
1037*43a90889SApple OSS Distributions 				 * this route entry
1038*43a90889SApple OSS Distributions 				 */
1039*43a90889SApple OSS Distributions 				route_event_enqueue_nwk_wq_entry(rt, NULL,
1040*43a90889SApple OSS Distributions 				    ROUTE_LLENTRY_STALE, NULL, TRUE);
1041*43a90889SApple OSS Distributions 
1042*43a90889SApple OSS Distributions 				RT_ADDREF_LOCKED(rt);
1043*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
1044*43a90889SApple OSS Distributions 				if (is_router) {
1045*43a90889SApple OSS Distributions 					struct route_event rt_ev;
1046*43a90889SApple OSS Distributions 					route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_STALE);
1047*43a90889SApple OSS Distributions 					(void) rnh->rnh_walktree(rnh, route_event_walktree, (void *)&rt_ev);
1048*43a90889SApple OSS Distributions 				}
1049*43a90889SApple OSS Distributions 				rtfree_locked(rt);
1050*43a90889SApple OSS Distributions 			} else {
1051*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
1052*43a90889SApple OSS Distributions 			}
1053*43a90889SApple OSS Distributions 			break;
1054*43a90889SApple OSS Distributions 
1055*43a90889SApple OSS Distributions 		case ND6_LLINFO_STALE:
1056*43a90889SApple OSS Distributions 		case ND6_LLINFO_PURGE:
1057*43a90889SApple OSS Distributions 			/* Garbage Collection(RFC 4861 5.3) */
1058*43a90889SApple OSS Distributions 			if (ln->ln_expire != 0) {
1059*43a90889SApple OSS Distributions 				RT_ADDREF_LOCKED(rt);
1060*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
1061*43a90889SApple OSS Distributions 				lck_mtx_unlock(rnh_lock);
1062*43a90889SApple OSS Distributions 				nd6_free(rt);
1063*43a90889SApple OSS Distributions 				ap->killed++;
1064*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
1065*43a90889SApple OSS Distributions 				rtfree_locked(rt);
1066*43a90889SApple OSS Distributions 				goto again;
1067*43a90889SApple OSS Distributions 			} else {
1068*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
1069*43a90889SApple OSS Distributions 			}
1070*43a90889SApple OSS Distributions 			break;
1071*43a90889SApple OSS Distributions 
1072*43a90889SApple OSS Distributions 		case ND6_LLINFO_DELAY:
1073*43a90889SApple OSS Distributions 			if ((flags & ND6_IFF_PERFORMNUD) != 0) {
1074*43a90889SApple OSS Distributions 				/* We need NUD */
1075*43a90889SApple OSS Distributions 				ln->ln_asked = 1;
1076*43a90889SApple OSS Distributions 				ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_PROBE);
1077*43a90889SApple OSS Distributions 				ln_setexpire(ln, timenow + retrans / 1000);
1078*43a90889SApple OSS Distributions 				RT_ADDREF_LOCKED(rt);
1079*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
1080*43a90889SApple OSS Distributions 				lck_mtx_unlock(rnh_lock);
1081*43a90889SApple OSS Distributions 				nd6_ns_output(ifp, &dst->sin6_addr,
1082*43a90889SApple OSS Distributions 				    &dst->sin6_addr, ln, NULL, 0);
1083*43a90889SApple OSS Distributions 				RT_REMREF(rt);
1084*43a90889SApple OSS Distributions 				ap->aging++;
1085*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
1086*43a90889SApple OSS Distributions 				goto again;
1087*43a90889SApple OSS Distributions 			}
1088*43a90889SApple OSS Distributions 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);         /* XXX */
1089*43a90889SApple OSS Distributions 			ln_setexpire(ln, timenow + nd6_gctimer);
1090*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
1091*43a90889SApple OSS Distributions 			ap->aging_lazy++;
1092*43a90889SApple OSS Distributions 			break;
1093*43a90889SApple OSS Distributions 
1094*43a90889SApple OSS Distributions 		case ND6_LLINFO_PROBE:
1095*43a90889SApple OSS Distributions 			if (ln->ln_asked < nd6_umaxtries) {
1096*43a90889SApple OSS Distributions 				ln->ln_asked++;
1097*43a90889SApple OSS Distributions 				ln_setexpire(ln, timenow + retrans / 1000);
1098*43a90889SApple OSS Distributions 				RT_ADDREF_LOCKED(rt);
1099*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
1100*43a90889SApple OSS Distributions 				lck_mtx_unlock(rnh_lock);
1101*43a90889SApple OSS Distributions 				nd6_ns_output(ifp, &dst->sin6_addr,
1102*43a90889SApple OSS Distributions 				    &dst->sin6_addr, ln, NULL, 0);
1103*43a90889SApple OSS Distributions 				RT_REMREF(rt);
1104*43a90889SApple OSS Distributions 				ap->aging++;
1105*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
1106*43a90889SApple OSS Distributions 			} else {
1107*43a90889SApple OSS Distributions 				is_router = (rt->rt_flags & RTF_ROUTER) ? TRUE : FALSE;
1108*43a90889SApple OSS Distributions 				send_nc_failure_kev = is_router;
1109*43a90889SApple OSS Distributions 				RT_ADDREF_LOCKED(rt);
1110*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
1111*43a90889SApple OSS Distributions 				lck_mtx_unlock(rnh_lock);
1112*43a90889SApple OSS Distributions 				nd6_free(rt);
1113*43a90889SApple OSS Distributions 				ap->killed++;
1114*43a90889SApple OSS Distributions 
1115*43a90889SApple OSS Distributions 				/*
1116*43a90889SApple OSS Distributions 				 * Enqueue work item to invoke callback for
1117*43a90889SApple OSS Distributions 				 * this route entry
1118*43a90889SApple OSS Distributions 				 */
1119*43a90889SApple OSS Distributions 				route_event_enqueue_nwk_wq_entry(rt, NULL,
1120*43a90889SApple OSS Distributions 				    ROUTE_LLENTRY_UNREACH, NULL, FALSE);
1121*43a90889SApple OSS Distributions 				defrouter_set_reachability(&SIN6(rt_key(rt))->sin6_addr, rt->rt_ifp,
1122*43a90889SApple OSS Distributions 				    FALSE);
1123*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
1124*43a90889SApple OSS Distributions 				/*
1125*43a90889SApple OSS Distributions 				 * nd6_free above would flush out the routing table of
1126*43a90889SApple OSS Distributions 				 * any cloned routes with same next-hop.
1127*43a90889SApple OSS Distributions 				 * Walk the tree anyways as there could be static routes
1128*43a90889SApple OSS Distributions 				 * left.
1129*43a90889SApple OSS Distributions 				 *
1130*43a90889SApple OSS Distributions 				 * We also already have a reference to rt that gets freed right
1131*43a90889SApple OSS Distributions 				 * after the block below executes. Don't need an extra reference
1132*43a90889SApple OSS Distributions 				 * on rt here.
1133*43a90889SApple OSS Distributions 				 */
1134*43a90889SApple OSS Distributions 				if (is_router) {
1135*43a90889SApple OSS Distributions 					struct route_event rt_ev;
1136*43a90889SApple OSS Distributions 					route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_UNREACH);
1137*43a90889SApple OSS Distributions 					(void) rnh->rnh_walktree(rnh,
1138*43a90889SApple OSS Distributions 					    route_event_walktree, (void *)&rt_ev);
1139*43a90889SApple OSS Distributions 				}
1140*43a90889SApple OSS Distributions 				rtfree_locked(rt);
1141*43a90889SApple OSS Distributions 			}
1142*43a90889SApple OSS Distributions 			LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1143*43a90889SApple OSS Distributions 			goto again;
1144*43a90889SApple OSS Distributions 
1145*43a90889SApple OSS Distributions 		default:
1146*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
1147*43a90889SApple OSS Distributions 			break;
1148*43a90889SApple OSS Distributions 		}
1149*43a90889SApple OSS Distributions 		ln = next;
1150*43a90889SApple OSS Distributions 	}
1151*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1152*43a90889SApple OSS Distributions 
1153*43a90889SApple OSS Distributions 	/* Now clear the flag from all entries */
1154*43a90889SApple OSS Distributions 	ln = llinfo_nd6.ln_next;
1155*43a90889SApple OSS Distributions 	while (ln != NULL && ln != &llinfo_nd6) {
1156*43a90889SApple OSS Distributions 		rtentry_ref_t rt = ln->ln_rt;
1157*43a90889SApple OSS Distributions 		struct llinfo_nd6 *__single next = ln->ln_next;
1158*43a90889SApple OSS Distributions 
1159*43a90889SApple OSS Distributions 		RT_LOCK_SPIN(rt);
1160*43a90889SApple OSS Distributions 		if (ln->ln_flags & ND6_LNF_TIMER_SKIP) {
1161*43a90889SApple OSS Distributions 			ln->ln_flags &= ~ND6_LNF_TIMER_SKIP;
1162*43a90889SApple OSS Distributions 		}
1163*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
1164*43a90889SApple OSS Distributions 		ln = next;
1165*43a90889SApple OSS Distributions 	}
1166*43a90889SApple OSS Distributions }
1167*43a90889SApple OSS Distributions 
1168*43a90889SApple OSS Distributions static void
nd6_service_expired_default_router(struct nd6svc_arg * ap,uint64_t timenow)1169*43a90889SApple OSS Distributions nd6_service_expired_default_router(struct nd6svc_arg *ap, uint64_t timenow)
1170*43a90889SApple OSS Distributions {
1171*43a90889SApple OSS Distributions 	struct nd_defrouter *__single dr = NULL;
1172*43a90889SApple OSS Distributions 	struct nd_defrouter *__single ndr = NULL;
1173*43a90889SApple OSS Distributions 	struct nd_drhead nd_defrouter_tmp;
1174*43a90889SApple OSS Distributions 	/* expire default router list */
1175*43a90889SApple OSS Distributions 	TAILQ_INIT(&nd_defrouter_tmp);
1176*43a90889SApple OSS Distributions 
1177*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
1178*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
1179*43a90889SApple OSS Distributions 
1180*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter_list, dr_entry, ndr) {
1181*43a90889SApple OSS Distributions 		ap->found++;
1182*43a90889SApple OSS Distributions 		if (dr->expire != 0 && dr->expire < timenow) {
1183*43a90889SApple OSS Distributions 			VERIFY(dr->ifp != NULL);
1184*43a90889SApple OSS Distributions 			in6_ifstat_inc(dr->ifp, ifs6_defrtr_expiry_cnt);
1185*43a90889SApple OSS Distributions 			if ((dr->stateflags & NDDRF_INELIGIBLE) == 0) {
1186*43a90889SApple OSS Distributions 				in6_event_enqueue_nwk_wq_entry(IN6_NDP_RTR_EXPIRY, dr->ifp,
1187*43a90889SApple OSS Distributions 				    &dr->rtaddr, dr->rtlifetime);
1188*43a90889SApple OSS Distributions 			}
1189*43a90889SApple OSS Distributions 			if (dr->ifp != NULL &&
1190*43a90889SApple OSS Distributions 			    dr->ifp->if_type == IFT_CELLULAR) {
1191*43a90889SApple OSS Distributions 				/*
1192*43a90889SApple OSS Distributions 				 * Some buggy cellular gateways may not send
1193*43a90889SApple OSS Distributions 				 * periodic router advertisements.
1194*43a90889SApple OSS Distributions 				 * Or they may send it with router lifetime
1195*43a90889SApple OSS Distributions 				 * value that is less than the configured Max and Min
1196*43a90889SApple OSS Distributions 				 * Router Advertisement interval.
1197*43a90889SApple OSS Distributions 				 * To top that an idle device may not wake up
1198*43a90889SApple OSS Distributions 				 * when periodic RA is received on cellular
1199*43a90889SApple OSS Distributions 				 * interface.
1200*43a90889SApple OSS Distributions 				 * We could send RS on every wake but RFC
1201*43a90889SApple OSS Distributions 				 * 4861 precludes that.
1202*43a90889SApple OSS Distributions 				 * The addresses are of infinite lifetimes
1203*43a90889SApple OSS Distributions 				 * and are tied to the lifetime of the bearer,
1204*43a90889SApple OSS Distributions 				 * so keeping the addresses and just getting rid of
1205*43a90889SApple OSS Distributions 				 * the router does not help us anyways.
1206*43a90889SApple OSS Distributions 				 * If there's network renumbering, a lifetime with
1207*43a90889SApple OSS Distributions 				 * value 0 would remove the default router.
1208*43a90889SApple OSS Distributions 				 * Also it will get deleted as part of purge when
1209*43a90889SApple OSS Distributions 				 * the PDP context is torn down and configured again.
1210*43a90889SApple OSS Distributions 				 * For that reason, do not expire the default router
1211*43a90889SApple OSS Distributions 				 * learned on cellular interface. Ever.
1212*43a90889SApple OSS Distributions 				 */
1213*43a90889SApple OSS Distributions 				dr->expire += dr->rtlifetime;
1214*43a90889SApple OSS Distributions 				nd6log2(debug,
1215*43a90889SApple OSS Distributions 				    "%s: Refreshing expired default router entry "
1216*43a90889SApple OSS Distributions 				    "%s for interface %s\n", __func__,
1217*43a90889SApple OSS Distributions 				    ip6_sprintf(&dr->rtaddr), if_name(dr->ifp));
1218*43a90889SApple OSS Distributions 			} else {
1219*43a90889SApple OSS Distributions 				ap->killed++;
1220*43a90889SApple OSS Distributions 				/*
1221*43a90889SApple OSS Distributions 				 * Remove the entry from default router list
1222*43a90889SApple OSS Distributions 				 * and add it to the temp list.
1223*43a90889SApple OSS Distributions 				 * nd_defrouter_tmp will be a local temporary
1224*43a90889SApple OSS Distributions 				 * list as no one else can get the same
1225*43a90889SApple OSS Distributions 				 * removed entry once it is removed from default
1226*43a90889SApple OSS Distributions 				 * router list.
1227*43a90889SApple OSS Distributions 				 * Remove the reference after calling defrtrlist_del
1228*43a90889SApple OSS Distributions 				 */
1229*43a90889SApple OSS Distributions 				TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
1230*43a90889SApple OSS Distributions 				TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
1231*43a90889SApple OSS Distributions 			}
1232*43a90889SApple OSS Distributions 		} else {
1233*43a90889SApple OSS Distributions 			if (dr->expire == 0 || (dr->stateflags & NDDRF_STATIC)) {
1234*43a90889SApple OSS Distributions 				ap->sticky++;
1235*43a90889SApple OSS Distributions 			} else {
1236*43a90889SApple OSS Distributions 				ap->aging_lazy++;
1237*43a90889SApple OSS Distributions 			}
1238*43a90889SApple OSS Distributions 		}
1239*43a90889SApple OSS Distributions 	}
1240*43a90889SApple OSS Distributions 
1241*43a90889SApple OSS Distributions 	/*
1242*43a90889SApple OSS Distributions 	 * Keep the following separate from the above
1243*43a90889SApple OSS Distributions 	 * iteration of nd_defrouter because it's not safe
1244*43a90889SApple OSS Distributions 	 * to call defrtrlist_del while iterating over the
1245*43a90889SApple OSS Distributions 	 * global default * router list. The Global list
1246*43a90889SApple OSS Distributions 	 * has to be traversed while holding nd6_mutex throughout.
1247*43a90889SApple OSS Distributions 	 *
1248*43a90889SApple OSS Distributions 	 * The following call to defrtrlist_del should be
1249*43a90889SApple OSS Distributions 	 * safe as we are iterating a local list of
1250*43a90889SApple OSS Distributions 	 * default routers.
1251*43a90889SApple OSS Distributions 	 */
1252*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter_tmp, dr_entry, ndr) {
1253*43a90889SApple OSS Distributions 		TAILQ_REMOVE(&nd_defrouter_tmp, dr, dr_entry);
1254*43a90889SApple OSS Distributions 		defrtrlist_del(dr, NULL);
1255*43a90889SApple OSS Distributions 		NDDR_REMREF(dr);        /* remove list reference */
1256*43a90889SApple OSS Distributions 	}
1257*43a90889SApple OSS Distributions 
1258*43a90889SApple OSS Distributions 	/* XXX TBD: Also iterate through RTI router lists */
1259*43a90889SApple OSS Distributions 	/*
1260*43a90889SApple OSS Distributions 	 * Also check if default router selection needs to be triggered
1261*43a90889SApple OSS Distributions 	 * for default interface, to avoid an issue with co-existence of
1262*43a90889SApple OSS Distributions 	 * static un-scoped default route configuration and default router
1263*43a90889SApple OSS Distributions 	 * discovery/selection.
1264*43a90889SApple OSS Distributions 	 */
1265*43a90889SApple OSS Distributions 	if (trigger_v6_defrtr_select) {
1266*43a90889SApple OSS Distributions 		defrouter_select(NULL, NULL);
1267*43a90889SApple OSS Distributions 		trigger_v6_defrtr_select = FALSE;
1268*43a90889SApple OSS Distributions 	}
1269*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
1270*43a90889SApple OSS Distributions }
1271*43a90889SApple OSS Distributions 
1272*43a90889SApple OSS Distributions static void
nd6_service_expired_route_info(struct nd6svc_arg * ap,uint64_t timenow)1273*43a90889SApple OSS Distributions nd6_service_expired_route_info(struct nd6svc_arg *ap, uint64_t timenow)
1274*43a90889SApple OSS Distributions {
1275*43a90889SApple OSS Distributions 	struct nd_route_info *__single rti = NULL;
1276*43a90889SApple OSS Distributions 	struct nd_route_info *__single rti_next = NULL;
1277*43a90889SApple OSS Distributions 
1278*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
1279*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
1280*43a90889SApple OSS Distributions 	nd6_rti_list_wait(__func__);
1281*43a90889SApple OSS Distributions 
1282*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(rti, &nd_rti_list, nd_rti_entry, rti_next) {
1283*43a90889SApple OSS Distributions 		struct nd_defrouter *__single dr = NULL;
1284*43a90889SApple OSS Distributions 		struct nd_defrouter *__single ndr = NULL;
1285*43a90889SApple OSS Distributions 		struct nd_route_info rti_tmp = {};
1286*43a90889SApple OSS Distributions 
1287*43a90889SApple OSS Distributions 		rti_tmp.nd_rti_prefix = rti->nd_rti_prefix;
1288*43a90889SApple OSS Distributions 		rti_tmp.nd_rti_prefixlen = rti->nd_rti_prefixlen;
1289*43a90889SApple OSS Distributions 		TAILQ_INIT(&rti_tmp.nd_rti_router_list);
1290*43a90889SApple OSS Distributions 
1291*43a90889SApple OSS Distributions 		TAILQ_FOREACH_SAFE(dr, &rti->nd_rti_router_list, dr_entry, ndr) {
1292*43a90889SApple OSS Distributions 			ap->found++;
1293*43a90889SApple OSS Distributions 			if (dr->expire != 0 && dr->expire < timenow) {
1294*43a90889SApple OSS Distributions 				VERIFY(dr->ifp != NULL);
1295*43a90889SApple OSS Distributions 				if (dr->ifp != NULL &&
1296*43a90889SApple OSS Distributions 				    dr->ifp->if_type == IFT_CELLULAR) {
1297*43a90889SApple OSS Distributions 					/*
1298*43a90889SApple OSS Distributions 					 * Don't expire these routes over cellular.
1299*43a90889SApple OSS Distributions 					 * XXX Should we change this for non default routes?
1300*43a90889SApple OSS Distributions 					 */
1301*43a90889SApple OSS Distributions 					dr->expire += dr->rtlifetime;
1302*43a90889SApple OSS Distributions 					nd6log2(debug,
1303*43a90889SApple OSS Distributions 					    "%s: Refreshing expired default router entry "
1304*43a90889SApple OSS Distributions 					    "%s for interface %s\n", __func__,
1305*43a90889SApple OSS Distributions 					    ip6_sprintf(&dr->rtaddr), if_name(dr->ifp));
1306*43a90889SApple OSS Distributions 				} else {
1307*43a90889SApple OSS Distributions 					ap->killed++;
1308*43a90889SApple OSS Distributions 					/*
1309*43a90889SApple OSS Distributions 					 * Remove the entry from rti entry's router list
1310*43a90889SApple OSS Distributions 					 * and add it to the temp list.
1311*43a90889SApple OSS Distributions 					 * Remove the reference after calling defrtrlist_del
1312*43a90889SApple OSS Distributions 					 */
1313*43a90889SApple OSS Distributions 					TAILQ_REMOVE(&rti->nd_rti_router_list, dr, dr_entry);
1314*43a90889SApple OSS Distributions 					TAILQ_INSERT_TAIL(&rti_tmp.nd_rti_router_list, dr, dr_entry);
1315*43a90889SApple OSS Distributions 				}
1316*43a90889SApple OSS Distributions 			} else {
1317*43a90889SApple OSS Distributions 				if (dr->expire == 0 || (dr->stateflags & NDDRF_STATIC)) {
1318*43a90889SApple OSS Distributions 					ap->sticky++;
1319*43a90889SApple OSS Distributions 				} else {
1320*43a90889SApple OSS Distributions 					ap->aging_lazy++;
1321*43a90889SApple OSS Distributions 				}
1322*43a90889SApple OSS Distributions 			}
1323*43a90889SApple OSS Distributions 		}
1324*43a90889SApple OSS Distributions 
1325*43a90889SApple OSS Distributions 		/*
1326*43a90889SApple OSS Distributions 		 * Keep the following separate from the above
1327*43a90889SApple OSS Distributions 		 * iteration of nd_defrouter because it's not safe
1328*43a90889SApple OSS Distributions 		 * to call defrtrlist_del while iterating global default
1329*43a90889SApple OSS Distributions 		 * router list. Global list has to be traversed
1330*43a90889SApple OSS Distributions 		 * while holding nd6_mutex throughout.
1331*43a90889SApple OSS Distributions 		 *
1332*43a90889SApple OSS Distributions 		 * The following call to defrtrlist_del should be
1333*43a90889SApple OSS Distributions 		 * safe as we are iterating a local list of
1334*43a90889SApple OSS Distributions 		 * default routers.
1335*43a90889SApple OSS Distributions 		 */
1336*43a90889SApple OSS Distributions 		TAILQ_FOREACH_SAFE(dr, &rti_tmp.nd_rti_router_list, dr_entry, ndr) {
1337*43a90889SApple OSS Distributions 			TAILQ_REMOVE(&rti_tmp.nd_rti_router_list, dr, dr_entry);
1338*43a90889SApple OSS Distributions 			defrtrlist_del(dr, &rti->nd_rti_router_list);
1339*43a90889SApple OSS Distributions 			NDDR_REMREF(dr);        /* remove list reference */
1340*43a90889SApple OSS Distributions 		}
1341*43a90889SApple OSS Distributions 
1342*43a90889SApple OSS Distributions 		/*
1343*43a90889SApple OSS Distributions 		 * The above may have removed an entry from default router list.
1344*43a90889SApple OSS Distributions 		 * If it did and the list is now empty, remove the rti as well.
1345*43a90889SApple OSS Distributions 		 */
1346*43a90889SApple OSS Distributions 		if (TAILQ_EMPTY(&rti->nd_rti_router_list)) {
1347*43a90889SApple OSS Distributions 			TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
1348*43a90889SApple OSS Distributions 			ndrti_free(rti);
1349*43a90889SApple OSS Distributions 		}
1350*43a90889SApple OSS Distributions 	}
1351*43a90889SApple OSS Distributions 
1352*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
1353*43a90889SApple OSS Distributions 	nd6_rti_list_signal_done();
1354*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
1355*43a90889SApple OSS Distributions }
1356*43a90889SApple OSS Distributions 
1357*43a90889SApple OSS Distributions 
1358*43a90889SApple OSS Distributions /*
1359*43a90889SApple OSS Distributions  * @function nd6_handle_duplicated_ip6_addr
1360*43a90889SApple OSS Distributions  *
1361*43a90889SApple OSS Distributions  * @brief
1362*43a90889SApple OSS Distributions  * Handle a duplicated IPv6 secured non-termporary address
1363*43a90889SApple OSS Distributions  *
1364*43a90889SApple OSS Distributions  * @discussion
1365*43a90889SApple OSS Distributions  * If the collision count hasn't been exceeded, removes the old
1366*43a90889SApple OSS Distributions  * conflicting IPv6 address, increments the collision count,
1367*43a90889SApple OSS Distributions  * and allocates a new address.
1368*43a90889SApple OSS Distributions  *
1369*43a90889SApple OSS Distributions  * Returns TRUE if the old address was removed, and the locks
1370*43a90889SApple OSS Distributions  * (in6_ifaddr_rwlock, ia6->ia_ifa) were unlocked.
1371*43a90889SApple OSS Distributions  */
1372*43a90889SApple OSS Distributions static boolean_t
nd6_handle_duplicated_ip6_addr(struct in6_ifaddr * ia6)1373*43a90889SApple OSS Distributions nd6_handle_duplicated_ip6_addr(struct in6_ifaddr *ia6)
1374*43a90889SApple OSS Distributions {
1375*43a90889SApple OSS Distributions 	uint8_t collision_count;
1376*43a90889SApple OSS Distributions 	int error = 0;
1377*43a90889SApple OSS Distributions 	struct in6_ifaddr *__single new_ia6;
1378*43a90889SApple OSS Distributions 	struct nd_prefix *__single pr;
1379*43a90889SApple OSS Distributions 	ifnet_ref_t ifp;
1380*43a90889SApple OSS Distributions 
1381*43a90889SApple OSS Distributions 	LCK_RW_ASSERT(&in6_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
1382*43a90889SApple OSS Distributions 	IFA_LOCK_ASSERT_HELD(&ia6->ia_ifa);
1383*43a90889SApple OSS Distributions 
1384*43a90889SApple OSS Distributions 	/* don't retry too many times */
1385*43a90889SApple OSS Distributions 	collision_count = ia6->ia6_cga_collision_count;
1386*43a90889SApple OSS Distributions 	if (collision_count >= ip6_cga_conflict_retries) {
1387*43a90889SApple OSS Distributions 		return FALSE;
1388*43a90889SApple OSS Distributions 	}
1389*43a90889SApple OSS Distributions 
1390*43a90889SApple OSS Distributions 	/* need the prefix to allocate a new address */
1391*43a90889SApple OSS Distributions 	pr = ia6->ia6_ndpr;
1392*43a90889SApple OSS Distributions 	if (pr == NULL) {
1393*43a90889SApple OSS Distributions 		return FALSE;
1394*43a90889SApple OSS Distributions 	}
1395*43a90889SApple OSS Distributions 	NDPR_ADDREF(pr);
1396*43a90889SApple OSS Distributions 	ifp = pr->ndpr_ifp;
1397*43a90889SApple OSS Distributions 	log(LOG_DEBUG,
1398*43a90889SApple OSS Distributions 	    "%s: %s duplicated (collision count %d)\n",
1399*43a90889SApple OSS Distributions 	    ifp->if_xname, ip6_sprintf(&ia6->ia_addr.sin6_addr),
1400*43a90889SApple OSS Distributions 	    collision_count);
1401*43a90889SApple OSS Distributions 
1402*43a90889SApple OSS Distributions 	/* remove the old address */
1403*43a90889SApple OSS Distributions 	IFA_UNLOCK(&ia6->ia_ifa);
1404*43a90889SApple OSS Distributions 	lck_rw_done(&in6_ifaddr_rwlock);
1405*43a90889SApple OSS Distributions 	in6_purgeaddr(&ia6->ia_ifa);
1406*43a90889SApple OSS Distributions 
1407*43a90889SApple OSS Distributions 	/* allocate a new address with new collision count */
1408*43a90889SApple OSS Distributions 	collision_count++;
1409*43a90889SApple OSS Distributions 	new_ia6 = in6_pfx_newpersistaddr(pr, 1, &error, FALSE, collision_count);
1410*43a90889SApple OSS Distributions 	if (new_ia6 != NULL) {
1411*43a90889SApple OSS Distributions 		log(LOG_DEBUG,
1412*43a90889SApple OSS Distributions 		    "%s: %s new (collision count %d)\n",
1413*43a90889SApple OSS Distributions 		    ifp->if_xname, ip6_sprintf(&new_ia6->ia_addr.sin6_addr),
1414*43a90889SApple OSS Distributions 		    collision_count);
1415*43a90889SApple OSS Distributions 		IFA_LOCK(&new_ia6->ia_ifa);
1416*43a90889SApple OSS Distributions 		NDPR_LOCK(pr);
1417*43a90889SApple OSS Distributions 		new_ia6->ia6_ndpr = pr;
1418*43a90889SApple OSS Distributions 		NDPR_ADDREF(pr); /* for addr reference */
1419*43a90889SApple OSS Distributions 		pr->ndpr_addrcnt++;
1420*43a90889SApple OSS Distributions 		VERIFY(pr->ndpr_addrcnt != 0);
1421*43a90889SApple OSS Distributions 		NDPR_UNLOCK(pr);
1422*43a90889SApple OSS Distributions 		IFA_UNLOCK(&new_ia6->ia_ifa);
1423*43a90889SApple OSS Distributions 		ifa_remref(&new_ia6->ia_ifa);
1424*43a90889SApple OSS Distributions 	} else {
1425*43a90889SApple OSS Distributions 		log(LOG_ERR, "%s: in6_pfx_newpersistaddr failed %d\n",
1426*43a90889SApple OSS Distributions 		    __func__, error);
1427*43a90889SApple OSS Distributions 	}
1428*43a90889SApple OSS Distributions 
1429*43a90889SApple OSS Distributions 	/* release extra prefix reference */
1430*43a90889SApple OSS Distributions 	NDPR_REMREF(pr);
1431*43a90889SApple OSS Distributions 	return TRUE;
1432*43a90889SApple OSS Distributions }
1433*43a90889SApple OSS Distributions 
1434*43a90889SApple OSS Distributions static boolean_t
secured_address_is_duplicated(int flags)1435*43a90889SApple OSS Distributions secured_address_is_duplicated(int flags)
1436*43a90889SApple OSS Distributions {
1437*43a90889SApple OSS Distributions #define _IN6_IFF_DUPLICATED_AUTOCONF_SECURED                            \
1438*43a90889SApple OSS Distributions 	(IN6_IFF_DUPLICATED | IN6_IFF_AUTOCONF | IN6_IFF_SECURED)
1439*43a90889SApple OSS Distributions 	return (flags & _IN6_IFF_DUPLICATED_AUTOCONF_SECURED) ==
1440*43a90889SApple OSS Distributions 	       _IN6_IFF_DUPLICATED_AUTOCONF_SECURED;
1441*43a90889SApple OSS Distributions }
1442*43a90889SApple OSS Distributions 
1443*43a90889SApple OSS Distributions static void
nd6_service_ip6_addr(struct nd6svc_arg * ap,uint64_t timenow)1444*43a90889SApple OSS Distributions nd6_service_ip6_addr(struct nd6svc_arg *ap, uint64_t timenow)
1445*43a90889SApple OSS Distributions {
1446*43a90889SApple OSS Distributions 	struct in6_ifaddr *__single ia6 = NULL;
1447*43a90889SApple OSS Distributions 	struct in6_ifaddr *__single nia6 = NULL;
1448*43a90889SApple OSS Distributions 	/*
1449*43a90889SApple OSS Distributions 	 * expire interface addresses.
1450*43a90889SApple OSS Distributions 	 * in the past the loop was inside prefix expiry processing.
1451*43a90889SApple OSS Distributions 	 * However, from a stricter spec-conformance standpoint, we should
1452*43a90889SApple OSS Distributions 	 * rather separate address lifetimes and prefix lifetimes.
1453*43a90889SApple OSS Distributions 	 */
1454*43a90889SApple OSS Distributions 
1455*43a90889SApple OSS Distributions addrloop:
1456*43a90889SApple OSS Distributions 	lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1457*43a90889SApple OSS Distributions 
1458*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(ia6, &in6_ifaddrhead, ia6_link, nia6) {
1459*43a90889SApple OSS Distributions 		int oldflags = ia6->ia6_flags;
1460*43a90889SApple OSS Distributions 		ap->found++;
1461*43a90889SApple OSS Distributions 		IFA_LOCK(&ia6->ia_ifa);
1462*43a90889SApple OSS Distributions 		/*
1463*43a90889SApple OSS Distributions 		 * Extra reference for ourselves; it's no-op if
1464*43a90889SApple OSS Distributions 		 * we don't have to regenerate temporary address,
1465*43a90889SApple OSS Distributions 		 * otherwise it protects the address from going
1466*43a90889SApple OSS Distributions 		 * away since we drop in6_ifaddr_rwlock below.
1467*43a90889SApple OSS Distributions 		 */
1468*43a90889SApple OSS Distributions 		ifa_addref(&ia6->ia_ifa);
1469*43a90889SApple OSS Distributions 
1470*43a90889SApple OSS Distributions 		/*
1471*43a90889SApple OSS Distributions 		 * Check for duplicated secured address
1472*43a90889SApple OSS Distributions 		 *
1473*43a90889SApple OSS Distributions 		 * nd6_handle_duplicated_ip6_addr attempts to regenerate
1474*43a90889SApple OSS Distributions 		 * secure address in the event of a collision.
1475*43a90889SApple OSS Distributions 		 * On successful generation this returns success
1476*43a90889SApple OSS Distributions 		 * and we restart the loop.
1477*43a90889SApple OSS Distributions 		 *
1478*43a90889SApple OSS Distributions 		 * When we hit the maximum attempts, this returns
1479*43a90889SApple OSS Distributions 		 * false.
1480*43a90889SApple OSS Distributions 		 */
1481*43a90889SApple OSS Distributions 		if (secured_address_is_duplicated(ia6->ia6_flags) &&
1482*43a90889SApple OSS Distributions 		    nd6_handle_duplicated_ip6_addr(ia6)) {
1483*43a90889SApple OSS Distributions 			/*
1484*43a90889SApple OSS Distributions 			 * nd6_handle_duplicated_ip6_addr() unlocked
1485*43a90889SApple OSS Distributions 			 * (in6_ifaddr_rwlock, ia6->ia_ifa) already.
1486*43a90889SApple OSS Distributions 			 * Still need to release extra reference on
1487*43a90889SApple OSS Distributions 			 * ia6->ia_ifa taken above.
1488*43a90889SApple OSS Distributions 			 */
1489*43a90889SApple OSS Distributions 			ifa_remref(&ia6->ia_ifa);
1490*43a90889SApple OSS Distributions 			goto addrloop;
1491*43a90889SApple OSS Distributions 		}
1492*43a90889SApple OSS Distributions 
1493*43a90889SApple OSS Distributions 		/* check address lifetime */
1494*43a90889SApple OSS Distributions 		if (IFA6_IS_INVALID(ia6, timenow)) {
1495*43a90889SApple OSS Distributions 			/*
1496*43a90889SApple OSS Distributions 			 * If the expiring address is temporary, try
1497*43a90889SApple OSS Distributions 			 * regenerating a new one.  This would be useful when
1498*43a90889SApple OSS Distributions 			 * we suspended a laptop PC, then turned it on after a
1499*43a90889SApple OSS Distributions 			 * period that could invalidate all temporary
1500*43a90889SApple OSS Distributions 			 * addresses.  Although we may have to restart the
1501*43a90889SApple OSS Distributions 			 * loop (see below), it must be after purging the
1502*43a90889SApple OSS Distributions 			 * address.  Otherwise, we'd see an infinite loop of
1503*43a90889SApple OSS Distributions 			 * regeneration.
1504*43a90889SApple OSS Distributions 			 */
1505*43a90889SApple OSS Distributions 			if (ip6_use_tempaddr &&
1506*43a90889SApple OSS Distributions 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1507*43a90889SApple OSS Distributions 				/*
1508*43a90889SApple OSS Distributions 				 * NOTE: We have to drop the lock here
1509*43a90889SApple OSS Distributions 				 * because regen_tmpaddr() eventually calls
1510*43a90889SApple OSS Distributions 				 * in6_update_ifa(), which must take the lock
1511*43a90889SApple OSS Distributions 				 * and would otherwise cause a hang.  This is
1512*43a90889SApple OSS Distributions 				 * safe because the goto addrloop leads to a
1513*43a90889SApple OSS Distributions 				 * re-evaluation of the in6_ifaddrs list
1514*43a90889SApple OSS Distributions 				 */
1515*43a90889SApple OSS Distributions 				IFA_UNLOCK(&ia6->ia_ifa);
1516*43a90889SApple OSS Distributions 				lck_rw_done(&in6_ifaddr_rwlock);
1517*43a90889SApple OSS Distributions 				(void) regen_tmpaddr(ia6);
1518*43a90889SApple OSS Distributions 			} else {
1519*43a90889SApple OSS Distributions 				IFA_UNLOCK(&ia6->ia_ifa);
1520*43a90889SApple OSS Distributions 				lck_rw_done(&in6_ifaddr_rwlock);
1521*43a90889SApple OSS Distributions 			}
1522*43a90889SApple OSS Distributions 
1523*43a90889SApple OSS Distributions 			/*
1524*43a90889SApple OSS Distributions 			 * Purging the address would have caused
1525*43a90889SApple OSS Distributions 			 * in6_ifaddr_rwlock to be dropped and reacquired;
1526*43a90889SApple OSS Distributions 			 * therefore search again from the beginning
1527*43a90889SApple OSS Distributions 			 * of in6_ifaddrs list.
1528*43a90889SApple OSS Distributions 			 */
1529*43a90889SApple OSS Distributions 			in6_purgeaddr(&ia6->ia_ifa);
1530*43a90889SApple OSS Distributions 			ap->killed++;
1531*43a90889SApple OSS Distributions 
1532*43a90889SApple OSS Distributions 			if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) == 0) {
1533*43a90889SApple OSS Distributions 				in6_ifstat_inc(ia6->ia_ifa.ifa_ifp, ifs6_addr_expiry_cnt);
1534*43a90889SApple OSS Distributions 				in6_event_enqueue_nwk_wq_entry(IN6_NDP_ADDR_EXPIRY,
1535*43a90889SApple OSS Distributions 				    ia6->ia_ifa.ifa_ifp, &ia6->ia_addr.sin6_addr,
1536*43a90889SApple OSS Distributions 				    0);
1537*43a90889SApple OSS Distributions 			}
1538*43a90889SApple OSS Distributions 			/* Release extra reference taken above */
1539*43a90889SApple OSS Distributions 			ifa_remref(&ia6->ia_ifa);
1540*43a90889SApple OSS Distributions 			goto addrloop;
1541*43a90889SApple OSS Distributions 		}
1542*43a90889SApple OSS Distributions 		/*
1543*43a90889SApple OSS Distributions 		 * The lazy timer runs every nd6_prune_lazy seconds with at
1544*43a90889SApple OSS Distributions 		 * most "2 * nd6_prune_lazy - 1" leeway. We consider the worst
1545*43a90889SApple OSS Distributions 		 * case here and make sure we schedule the regular timer if an
1546*43a90889SApple OSS Distributions 		 * interface address is about to expire.
1547*43a90889SApple OSS Distributions 		 */
1548*43a90889SApple OSS Distributions 		if (IFA6_IS_INVALID(ia6, timenow + 3 * nd6_prune_lazy)) {
1549*43a90889SApple OSS Distributions 			ap->aging++;
1550*43a90889SApple OSS Distributions 		} else {
1551*43a90889SApple OSS Distributions 			ap->aging_lazy++;
1552*43a90889SApple OSS Distributions 		}
1553*43a90889SApple OSS Distributions 		IFA_LOCK_ASSERT_HELD(&ia6->ia_ifa);
1554*43a90889SApple OSS Distributions 		if (IFA6_IS_DEPRECATED(ia6, timenow)) {
1555*43a90889SApple OSS Distributions 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
1556*43a90889SApple OSS Distributions 
1557*43a90889SApple OSS Distributions 			if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
1558*43a90889SApple OSS Distributions #if SKYWALK
1559*43a90889SApple OSS Distributions 				SK_NXS_MS_IF_ADDR_GENCNT_INC(ia6->ia_ifp);
1560*43a90889SApple OSS Distributions #endif /* SKYWALK */
1561*43a90889SApple OSS Distributions 				/*
1562*43a90889SApple OSS Distributions 				 * Only enqueue the Deprecated event when the address just
1563*43a90889SApple OSS Distributions 				 * becomes deprecated.
1564*43a90889SApple OSS Distributions 				 * Keep it limited to the stable address as it is common for
1565*43a90889SApple OSS Distributions 				 * older temporary addresses to get deprecated while we generate
1566*43a90889SApple OSS Distributions 				 * new ones.
1567*43a90889SApple OSS Distributions 				 */
1568*43a90889SApple OSS Distributions 				if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) == 0) {
1569*43a90889SApple OSS Distributions 					in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DEPRECATED,
1570*43a90889SApple OSS Distributions 					    ia6->ia_ifa.ifa_ifp, &ia6->ia_addr.sin6_addr,
1571*43a90889SApple OSS Distributions 					    0);
1572*43a90889SApple OSS Distributions 				}
1573*43a90889SApple OSS Distributions 			}
1574*43a90889SApple OSS Distributions 			/*
1575*43a90889SApple OSS Distributions 			 * If a temporary address has just become deprecated,
1576*43a90889SApple OSS Distributions 			 * regenerate a new one if possible.
1577*43a90889SApple OSS Distributions 			 */
1578*43a90889SApple OSS Distributions 			if (ip6_use_tempaddr &&
1579*43a90889SApple OSS Distributions 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1580*43a90889SApple OSS Distributions 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
1581*43a90889SApple OSS Distributions 				/* see NOTE above */
1582*43a90889SApple OSS Distributions 				IFA_UNLOCK(&ia6->ia_ifa);
1583*43a90889SApple OSS Distributions 				lck_rw_done(&in6_ifaddr_rwlock);
1584*43a90889SApple OSS Distributions 				if (regen_tmpaddr(ia6) == 0) {
1585*43a90889SApple OSS Distributions 					/*
1586*43a90889SApple OSS Distributions 					 * A new temporary address is
1587*43a90889SApple OSS Distributions 					 * generated.
1588*43a90889SApple OSS Distributions 					 * XXX: this means the address chain
1589*43a90889SApple OSS Distributions 					 * has changed while we are still in
1590*43a90889SApple OSS Distributions 					 * the loop.  Although the change
1591*43a90889SApple OSS Distributions 					 * would not cause disaster (because
1592*43a90889SApple OSS Distributions 					 * it's not a deletion, but an
1593*43a90889SApple OSS Distributions 					 * addition,) we'd rather restart the
1594*43a90889SApple OSS Distributions 					 * loop just for safety.  Or does this
1595*43a90889SApple OSS Distributions 					 * significantly reduce performance??
1596*43a90889SApple OSS Distributions 					 */
1597*43a90889SApple OSS Distributions 					/* Release extra reference */
1598*43a90889SApple OSS Distributions 					ifa_remref(&ia6->ia_ifa);
1599*43a90889SApple OSS Distributions 					goto addrloop;
1600*43a90889SApple OSS Distributions 				}
1601*43a90889SApple OSS Distributions 				lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1602*43a90889SApple OSS Distributions 			} else {
1603*43a90889SApple OSS Distributions 				IFA_UNLOCK(&ia6->ia_ifa);
1604*43a90889SApple OSS Distributions 			}
1605*43a90889SApple OSS Distributions 		} else {
1606*43a90889SApple OSS Distributions 			/*
1607*43a90889SApple OSS Distributions 			 * A new RA might have made a deprecated address
1608*43a90889SApple OSS Distributions 			 * preferred.
1609*43a90889SApple OSS Distributions 			 */
1610*43a90889SApple OSS Distributions 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
1611*43a90889SApple OSS Distributions #if SKYWALK
1612*43a90889SApple OSS Distributions 			if ((oldflags & IN6_IFF_DEPRECATED) != 0) {
1613*43a90889SApple OSS Distributions 				SK_NXS_MS_IF_ADDR_GENCNT_INC(ia6->ia_ifp);
1614*43a90889SApple OSS Distributions 			}
1615*43a90889SApple OSS Distributions #endif /* SKYWALK */
1616*43a90889SApple OSS Distributions 			IFA_UNLOCK(&ia6->ia_ifa);
1617*43a90889SApple OSS Distributions 		}
1618*43a90889SApple OSS Distributions 		LCK_RW_ASSERT(&in6_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
1619*43a90889SApple OSS Distributions 		/* Release extra reference taken above */
1620*43a90889SApple OSS Distributions 		ifa_remref(&ia6->ia_ifa);
1621*43a90889SApple OSS Distributions 	}
1622*43a90889SApple OSS Distributions 	lck_rw_done(&in6_ifaddr_rwlock);
1623*43a90889SApple OSS Distributions }
1624*43a90889SApple OSS Distributions 
1625*43a90889SApple OSS Distributions static void
nd6_service_expired_prefix(struct nd6svc_arg * ap,uint64_t timenow)1626*43a90889SApple OSS Distributions nd6_service_expired_prefix(struct nd6svc_arg *ap, uint64_t timenow)
1627*43a90889SApple OSS Distributions {
1628*43a90889SApple OSS Distributions 	struct nd_prefix *__single pr = NULL;
1629*43a90889SApple OSS Distributions 
1630*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
1631*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
1632*43a90889SApple OSS Distributions 	/* expire prefix list */
1633*43a90889SApple OSS Distributions 	pr = nd_prefix.lh_first;
1634*43a90889SApple OSS Distributions 	while (pr != NULL) {
1635*43a90889SApple OSS Distributions 		ap->found++;
1636*43a90889SApple OSS Distributions 		/*
1637*43a90889SApple OSS Distributions 		 * Skip already processed or defunct prefixes
1638*43a90889SApple OSS Distributions 		 * We may iterate the prefix list from head again
1639*43a90889SApple OSS Distributions 		 * so, we are trying to not revisit the same prefix
1640*43a90889SApple OSS Distributions 		 * for the same instance of nd6_service
1641*43a90889SApple OSS Distributions 		 */
1642*43a90889SApple OSS Distributions 		NDPR_LOCK(pr);
1643*43a90889SApple OSS Distributions 		if (pr->ndpr_stateflags & NDPRF_PROCESSED_SERVICE ||
1644*43a90889SApple OSS Distributions 		    pr->ndpr_stateflags & NDPRF_DEFUNCT) {
1645*43a90889SApple OSS Distributions 			pr->ndpr_stateflags |= NDPRF_PROCESSED_SERVICE;
1646*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
1647*43a90889SApple OSS Distributions 			pr = pr->ndpr_next;
1648*43a90889SApple OSS Distributions 			continue;
1649*43a90889SApple OSS Distributions 		}
1650*43a90889SApple OSS Distributions 
1651*43a90889SApple OSS Distributions 		/*
1652*43a90889SApple OSS Distributions 		 * If there are still manual addresses configured in the system
1653*43a90889SApple OSS Distributions 		 * that are associated with the prefix, ignore prefix expiry
1654*43a90889SApple OSS Distributions 		 */
1655*43a90889SApple OSS Distributions 		if (pr->ndpr_manual_addrcnt != 0) {
1656*43a90889SApple OSS Distributions 			pr->ndpr_stateflags |= NDPRF_PROCESSED_SERVICE;
1657*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
1658*43a90889SApple OSS Distributions 			pr = pr->ndpr_next;
1659*43a90889SApple OSS Distributions 			continue;
1660*43a90889SApple OSS Distributions 		}
1661*43a90889SApple OSS Distributions 
1662*43a90889SApple OSS Distributions 		/*
1663*43a90889SApple OSS Distributions 		 * check prefix lifetime.
1664*43a90889SApple OSS Distributions 		 * since pltime is just for autoconf, pltime processing for
1665*43a90889SApple OSS Distributions 		 * prefix is not necessary.
1666*43a90889SApple OSS Distributions 		 */
1667*43a90889SApple OSS Distributions 		if (pr->ndpr_expire != 0 && pr->ndpr_expire < timenow) {
1668*43a90889SApple OSS Distributions 			/*
1669*43a90889SApple OSS Distributions 			 * address expiration and prefix expiration are
1670*43a90889SApple OSS Distributions 			 * separate. NEVER perform in6_purgeaddr here.
1671*43a90889SApple OSS Distributions 			 */
1672*43a90889SApple OSS Distributions 			pr->ndpr_stateflags |= NDPRF_PROCESSED_SERVICE;
1673*43a90889SApple OSS Distributions 			NDPR_ADDREF(pr);
1674*43a90889SApple OSS Distributions 			prelist_remove(pr);
1675*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
1676*43a90889SApple OSS Distributions 
1677*43a90889SApple OSS Distributions 			in6_ifstat_inc(pr->ndpr_ifp, ifs6_pfx_expiry_cnt);
1678*43a90889SApple OSS Distributions 			in6_event_enqueue_nwk_wq_entry(IN6_NDP_PFX_EXPIRY,
1679*43a90889SApple OSS Distributions 			    pr->ndpr_ifp, &pr->ndpr_prefix.sin6_addr,
1680*43a90889SApple OSS Distributions 			    0);
1681*43a90889SApple OSS Distributions 			NDPR_REMREF(pr);
1682*43a90889SApple OSS Distributions 			pfxlist_onlink_check();
1683*43a90889SApple OSS Distributions 			pr = nd_prefix.lh_first;
1684*43a90889SApple OSS Distributions 			ap->killed++;
1685*43a90889SApple OSS Distributions 		} else {
1686*43a90889SApple OSS Distributions 			if (pr->ndpr_expire == 0 ||
1687*43a90889SApple OSS Distributions 			    (pr->ndpr_stateflags & NDPRF_STATIC)) {
1688*43a90889SApple OSS Distributions 				ap->sticky++;
1689*43a90889SApple OSS Distributions 			} else {
1690*43a90889SApple OSS Distributions 				ap->aging_lazy++;
1691*43a90889SApple OSS Distributions 			}
1692*43a90889SApple OSS Distributions 			pr->ndpr_stateflags |= NDPRF_PROCESSED_SERVICE;
1693*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
1694*43a90889SApple OSS Distributions 			pr = pr->ndpr_next;
1695*43a90889SApple OSS Distributions 		}
1696*43a90889SApple OSS Distributions 	}
1697*43a90889SApple OSS Distributions 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1698*43a90889SApple OSS Distributions 		NDPR_LOCK(pr);
1699*43a90889SApple OSS Distributions 		pr->ndpr_stateflags &= ~NDPRF_PROCESSED_SERVICE;
1700*43a90889SApple OSS Distributions 		NDPR_UNLOCK(pr);
1701*43a90889SApple OSS Distributions 	}
1702*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
1703*43a90889SApple OSS Distributions }
1704*43a90889SApple OSS Distributions 
1705*43a90889SApple OSS Distributions 
1706*43a90889SApple OSS Distributions /*
1707*43a90889SApple OSS Distributions  * ND6 service routine to expire default route list and prefix list
1708*43a90889SApple OSS Distributions  */
1709*43a90889SApple OSS Distributions static void
nd6_service(void * arg)1710*43a90889SApple OSS Distributions nd6_service(void *arg)
1711*43a90889SApple OSS Distributions {
1712*43a90889SApple OSS Distributions 	struct nd6svc_arg *__single ap = arg;
1713*43a90889SApple OSS Distributions 	uint64_t timenow;
1714*43a90889SApple OSS Distributions 
1715*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1716*43a90889SApple OSS Distributions 	/*
1717*43a90889SApple OSS Distributions 	 * Since we may drop rnh_lock and nd6_mutex below, we want
1718*43a90889SApple OSS Distributions 	 * to run this entire operation single threaded.
1719*43a90889SApple OSS Distributions 	 */
1720*43a90889SApple OSS Distributions 	while (nd6_service_busy) {
1721*43a90889SApple OSS Distributions 		nd6log2(debug, "%s: %s is blocked by %d waiters\n",
1722*43a90889SApple OSS Distributions 		    __func__, ap->draining ? "drainer" : "timer",
1723*43a90889SApple OSS Distributions 		    nd6_service_waiters);
1724*43a90889SApple OSS Distributions 		nd6_service_waiters++;
1725*43a90889SApple OSS Distributions 		(void) msleep(nd6_service_wc, rnh_lock, (PZERO - 1),
1726*43a90889SApple OSS Distributions 		    __func__, NULL);
1727*43a90889SApple OSS Distributions 		LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1728*43a90889SApple OSS Distributions 	}
1729*43a90889SApple OSS Distributions 
1730*43a90889SApple OSS Distributions 	/* We are busy now; tell everyone else to go away */
1731*43a90889SApple OSS Distributions 	nd6_service_busy = TRUE;
1732*43a90889SApple OSS Distributions 	net_update_uptime();
1733*43a90889SApple OSS Distributions 	timenow = net_uptime();
1734*43a90889SApple OSS Distributions 
1735*43a90889SApple OSS Distributions 	/* Iterate and service neighbor cache entries */
1736*43a90889SApple OSS Distributions 	nd6_service_neighbor_cache(ap, timenow);
1737*43a90889SApple OSS Distributions 
1738*43a90889SApple OSS Distributions 	/*
1739*43a90889SApple OSS Distributions 	 * There is lock ordering requirement and rnh_lock
1740*43a90889SApple OSS Distributions 	 * has to be released before acquiring nd6_mutex.
1741*43a90889SApple OSS Distributions 	 */
1742*43a90889SApple OSS Distributions 	lck_mtx_unlock(rnh_lock);
1743*43a90889SApple OSS Distributions 
1744*43a90889SApple OSS Distributions 	/* Iterate and service expired default router */
1745*43a90889SApple OSS Distributions 	nd6_service_expired_default_router(ap, timenow);
1746*43a90889SApple OSS Distributions 
1747*43a90889SApple OSS Distributions 	/* Iterate and service expired route information entries */
1748*43a90889SApple OSS Distributions 	nd6_service_expired_route_info(ap, timenow);
1749*43a90889SApple OSS Distributions 
1750*43a90889SApple OSS Distributions 	/* Iterate and service expired/duplicated IPv6 address */
1751*43a90889SApple OSS Distributions 	nd6_service_ip6_addr(ap, timenow);
1752*43a90889SApple OSS Distributions 
1753*43a90889SApple OSS Distributions 	/* Iterate and service expired IPv6 prefixes */
1754*43a90889SApple OSS Distributions 	nd6_service_expired_prefix(ap, timenow);
1755*43a90889SApple OSS Distributions 
1756*43a90889SApple OSS Distributions 	lck_mtx_lock(rnh_lock);
1757*43a90889SApple OSS Distributions 	/* We're done; let others enter */
1758*43a90889SApple OSS Distributions 	nd6_service_busy = FALSE;
1759*43a90889SApple OSS Distributions 	if (nd6_service_waiters > 0) {
1760*43a90889SApple OSS Distributions 		nd6_service_waiters = 0;
1761*43a90889SApple OSS Distributions 		wakeup(nd6_service_wc);
1762*43a90889SApple OSS Distributions 	}
1763*43a90889SApple OSS Distributions }
1764*43a90889SApple OSS Distributions 
1765*43a90889SApple OSS Distributions static int nd6_need_draining = 0;
1766*43a90889SApple OSS Distributions 
1767*43a90889SApple OSS Distributions void
nd6_drain(void * arg)1768*43a90889SApple OSS Distributions nd6_drain(void *arg)
1769*43a90889SApple OSS Distributions {
1770*43a90889SApple OSS Distributions #pragma unused(arg)
1771*43a90889SApple OSS Distributions 	nd6log2(debug, "%s: draining ND6 entries\n", __func__);
1772*43a90889SApple OSS Distributions 
1773*43a90889SApple OSS Distributions 	lck_mtx_lock(rnh_lock);
1774*43a90889SApple OSS Distributions 	nd6_need_draining = 1;
1775*43a90889SApple OSS Distributions 	nd6_sched_timeout(NULL, NULL);
1776*43a90889SApple OSS Distributions 	lck_mtx_unlock(rnh_lock);
1777*43a90889SApple OSS Distributions }
1778*43a90889SApple OSS Distributions 
1779*43a90889SApple OSS Distributions /*
1780*43a90889SApple OSS Distributions  * We use the ``arg'' variable to decide whether or not the timer we're
1781*43a90889SApple OSS Distributions  * running is the fast timer. We do this to reset the nd6_fast_timer_on
1782*43a90889SApple OSS Distributions  * variable so that later we don't end up ignoring a ``fast timer''
1783*43a90889SApple OSS Distributions  * request if the 5 second timer is running (see nd6_sched_timeout).
1784*43a90889SApple OSS Distributions  */
1785*43a90889SApple OSS Distributions static void
nd6_timeout(void * arg)1786*43a90889SApple OSS Distributions nd6_timeout(void *arg)
1787*43a90889SApple OSS Distributions {
1788*43a90889SApple OSS Distributions 	struct nd6svc_arg sarg;
1789*43a90889SApple OSS Distributions 	uint32_t buf;
1790*43a90889SApple OSS Distributions 
1791*43a90889SApple OSS Distributions 	lck_mtx_lock(rnh_lock);
1792*43a90889SApple OSS Distributions 	bzero(&sarg, sizeof(sarg));
1793*43a90889SApple OSS Distributions 	if (nd6_need_draining != 0) {
1794*43a90889SApple OSS Distributions 		nd6_need_draining = 0;
1795*43a90889SApple OSS Distributions 		sarg.draining = 1;
1796*43a90889SApple OSS Distributions 	}
1797*43a90889SApple OSS Distributions 	nd6_service(&sarg);
1798*43a90889SApple OSS Distributions 	nd6log3(debug, "%s: found %u, aging_lazy %u, aging %u, "
1799*43a90889SApple OSS Distributions 	    "sticky %u, killed %u\n", __func__, sarg.found, sarg.aging_lazy,
1800*43a90889SApple OSS Distributions 	    sarg.aging, sarg.sticky, sarg.killed);
1801*43a90889SApple OSS Distributions 	/* re-arm the timer if there's work to do */
1802*43a90889SApple OSS Distributions 	nd6_timeout_run--;
1803*43a90889SApple OSS Distributions 	VERIFY(nd6_timeout_run >= 0 && nd6_timeout_run < 2);
1804*43a90889SApple OSS Distributions 	if (arg == &nd6_fast_timer_on) {
1805*43a90889SApple OSS Distributions 		nd6_fast_timer_on = FALSE;
1806*43a90889SApple OSS Distributions 	}
1807*43a90889SApple OSS Distributions 	if (sarg.aging_lazy > 0 || sarg.aging > 0 || nd6_sched_timeout_want) {
1808*43a90889SApple OSS Distributions 		struct timeval atv, ltv;
1809*43a90889SApple OSS Distributions 		struct timeval *__single leeway;
1810*43a90889SApple OSS Distributions 		int lazy = nd6_prune_lazy;
1811*43a90889SApple OSS Distributions 
1812*43a90889SApple OSS Distributions 		if (sarg.aging > 0 || lazy < 1) {
1813*43a90889SApple OSS Distributions 			atv.tv_usec = 0;
1814*43a90889SApple OSS Distributions 			atv.tv_sec = nd6_prune;
1815*43a90889SApple OSS Distributions 			leeway = NULL;
1816*43a90889SApple OSS Distributions 		} else {
1817*43a90889SApple OSS Distributions 			VERIFY(lazy >= 1);
1818*43a90889SApple OSS Distributions 			atv.tv_usec = 0;
1819*43a90889SApple OSS Distributions 			atv.tv_sec = MAX(nd6_prune, lazy);
1820*43a90889SApple OSS Distributions 			ltv.tv_usec = 0;
1821*43a90889SApple OSS Distributions 			read_frandom(&buf, sizeof(buf));
1822*43a90889SApple OSS Distributions 			ltv.tv_sec = MAX(buf % lazy, 1) * 2;
1823*43a90889SApple OSS Distributions 			leeway = &ltv;
1824*43a90889SApple OSS Distributions 		}
1825*43a90889SApple OSS Distributions 		nd6_sched_timeout(&atv, leeway);
1826*43a90889SApple OSS Distributions 	} else if (nd6_debug) {
1827*43a90889SApple OSS Distributions 		nd6log3(debug, "%s: not rescheduling timer\n", __func__);
1828*43a90889SApple OSS Distributions 	}
1829*43a90889SApple OSS Distributions 	lck_mtx_unlock(rnh_lock);
1830*43a90889SApple OSS Distributions }
1831*43a90889SApple OSS Distributions 
1832*43a90889SApple OSS Distributions void
nd6_sched_timeout(struct timeval * atv,struct timeval * ltv)1833*43a90889SApple OSS Distributions nd6_sched_timeout(struct timeval *atv, struct timeval *ltv)
1834*43a90889SApple OSS Distributions {
1835*43a90889SApple OSS Distributions 	struct timeval tv;
1836*43a90889SApple OSS Distributions 
1837*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1838*43a90889SApple OSS Distributions 	if (atv == NULL) {
1839*43a90889SApple OSS Distributions 		tv.tv_usec = 0;
1840*43a90889SApple OSS Distributions 		tv.tv_sec = MAX(nd6_prune, 1);
1841*43a90889SApple OSS Distributions 		atv = &tv;
1842*43a90889SApple OSS Distributions 		ltv = NULL;     /* ignore leeway */
1843*43a90889SApple OSS Distributions 	}
1844*43a90889SApple OSS Distributions 	/* see comments on top of this file */
1845*43a90889SApple OSS Distributions 	if (nd6_timeout_run == 0) {
1846*43a90889SApple OSS Distributions 		if (ltv == NULL) {
1847*43a90889SApple OSS Distributions 			nd6log3(debug, "%s: timer scheduled in "
1848*43a90889SApple OSS Distributions 			    "T+%llus.%lluu (demand %d)\n", __func__,
1849*43a90889SApple OSS Distributions 			    (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec,
1850*43a90889SApple OSS Distributions 			    nd6_sched_timeout_want);
1851*43a90889SApple OSS Distributions 			nd6_fast_timer_on = TRUE;
1852*43a90889SApple OSS Distributions 			timeout(nd6_timeout, &nd6_fast_timer_on, tvtohz(atv));
1853*43a90889SApple OSS Distributions 		} else {
1854*43a90889SApple OSS Distributions 			nd6log3(debug, "%s: timer scheduled in "
1855*43a90889SApple OSS Distributions 			    "T+%llus.%lluu with %llus.%lluu leeway "
1856*43a90889SApple OSS Distributions 			    "(demand %d)\n", __func__, (uint64_t)atv->tv_sec,
1857*43a90889SApple OSS Distributions 			    (uint64_t)atv->tv_usec, (uint64_t)ltv->tv_sec,
1858*43a90889SApple OSS Distributions 			    (uint64_t)ltv->tv_usec, nd6_sched_timeout_want);
1859*43a90889SApple OSS Distributions 			nd6_fast_timer_on = FALSE;
1860*43a90889SApple OSS Distributions 			timeout_with_leeway(nd6_timeout, NULL,
1861*43a90889SApple OSS Distributions 			    tvtohz(atv), tvtohz(ltv));
1862*43a90889SApple OSS Distributions 		}
1863*43a90889SApple OSS Distributions 		nd6_timeout_run++;
1864*43a90889SApple OSS Distributions 		nd6_sched_timeout_want = 0;
1865*43a90889SApple OSS Distributions 	} else if (nd6_timeout_run == 1 && ltv == NULL &&
1866*43a90889SApple OSS Distributions 	    nd6_fast_timer_on == FALSE) {
1867*43a90889SApple OSS Distributions 		nd6log3(debug, "%s: fast timer scheduled in "
1868*43a90889SApple OSS Distributions 		    "T+%llus.%lluu (demand %d)\n", __func__,
1869*43a90889SApple OSS Distributions 		    (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec,
1870*43a90889SApple OSS Distributions 		    nd6_sched_timeout_want);
1871*43a90889SApple OSS Distributions 		nd6_fast_timer_on = TRUE;
1872*43a90889SApple OSS Distributions 		nd6_sched_timeout_want = 0;
1873*43a90889SApple OSS Distributions 		nd6_timeout_run++;
1874*43a90889SApple OSS Distributions 		timeout(nd6_timeout, &nd6_fast_timer_on, tvtohz(atv));
1875*43a90889SApple OSS Distributions 	} else {
1876*43a90889SApple OSS Distributions 		if (ltv == NULL) {
1877*43a90889SApple OSS Distributions 			nd6log3(debug, "%s: not scheduling timer: "
1878*43a90889SApple OSS Distributions 			    "timers %d, fast_timer %d, T+%llus.%lluu\n",
1879*43a90889SApple OSS Distributions 			    __func__, nd6_timeout_run, nd6_fast_timer_on,
1880*43a90889SApple OSS Distributions 			    (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec);
1881*43a90889SApple OSS Distributions 		} else {
1882*43a90889SApple OSS Distributions 			nd6log3(debug, "%s: not scheduling timer: "
1883*43a90889SApple OSS Distributions 			    "timers %d, fast_timer %d, T+%llus.%lluu "
1884*43a90889SApple OSS Distributions 			    "with %llus.%lluu leeway\n", __func__,
1885*43a90889SApple OSS Distributions 			    nd6_timeout_run, nd6_fast_timer_on,
1886*43a90889SApple OSS Distributions 			    (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec,
1887*43a90889SApple OSS Distributions 			    (uint64_t)ltv->tv_sec, (uint64_t)ltv->tv_usec);
1888*43a90889SApple OSS Distributions 		}
1889*43a90889SApple OSS Distributions 	}
1890*43a90889SApple OSS Distributions }
1891*43a90889SApple OSS Distributions 
1892*43a90889SApple OSS Distributions /*
1893*43a90889SApple OSS Distributions  * ND6 router advertisement kernel notification
1894*43a90889SApple OSS Distributions  */
1895*43a90889SApple OSS Distributions void
nd6_post_msg(u_int32_t code,struct nd_prefix_list * prefix_list,u_int32_t list_length,u_int32_t mtu)1896*43a90889SApple OSS Distributions nd6_post_msg(u_int32_t code, struct nd_prefix_list *prefix_list,
1897*43a90889SApple OSS Distributions     u_int32_t list_length, u_int32_t mtu)
1898*43a90889SApple OSS Distributions {
1899*43a90889SApple OSS Distributions 	struct kev_msg ev_msg;
1900*43a90889SApple OSS Distributions 	struct kev_nd6_ra_data nd6_ra_msg_data;
1901*43a90889SApple OSS Distributions 	struct nd_prefix_list *__single itr = prefix_list;
1902*43a90889SApple OSS Distributions 
1903*43a90889SApple OSS Distributions 	bzero(&ev_msg, sizeof(struct kev_msg));
1904*43a90889SApple OSS Distributions 	ev_msg.vendor_code      = KEV_VENDOR_APPLE;
1905*43a90889SApple OSS Distributions 	ev_msg.kev_class        = KEV_NETWORK_CLASS;
1906*43a90889SApple OSS Distributions 	ev_msg.kev_subclass     = KEV_ND6_SUBCLASS;
1907*43a90889SApple OSS Distributions 	ev_msg.event_code       = code;
1908*43a90889SApple OSS Distributions 
1909*43a90889SApple OSS Distributions 	bzero(&nd6_ra_msg_data, sizeof(nd6_ra_msg_data));
1910*43a90889SApple OSS Distributions 
1911*43a90889SApple OSS Distributions 	if (mtu > 0 && mtu >= IPV6_MMTU) {
1912*43a90889SApple OSS Distributions 		nd6_ra_msg_data.mtu = mtu;
1913*43a90889SApple OSS Distributions 		nd6_ra_msg_data.flags |= KEV_ND6_DATA_VALID_MTU;
1914*43a90889SApple OSS Distributions 	}
1915*43a90889SApple OSS Distributions 
1916*43a90889SApple OSS Distributions 	if (list_length > 0 && prefix_list != NULL) {
1917*43a90889SApple OSS Distributions 		nd6_ra_msg_data.list_length = list_length;
1918*43a90889SApple OSS Distributions 		nd6_ra_msg_data.flags |= KEV_ND6_DATA_VALID_PREFIX;
1919*43a90889SApple OSS Distributions 	}
1920*43a90889SApple OSS Distributions 
1921*43a90889SApple OSS Distributions 	while (itr != NULL && nd6_ra_msg_data.list_index < list_length) {
1922*43a90889SApple OSS Distributions 		SOCKADDR_COPY(&itr->pr.ndpr_prefix, &nd6_ra_msg_data.prefix.prefix,
1923*43a90889SApple OSS Distributions 		    sizeof(nd6_ra_msg_data.prefix.prefix));
1924*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.raflags = itr->pr.ndpr_raf;
1925*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.prefixlen = itr->pr.ndpr_plen;
1926*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.origin = PR_ORIG_RA;
1927*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.vltime = itr->pr.ndpr_vltime;
1928*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.pltime = itr->pr.ndpr_pltime;
1929*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.expire = ndpr_getexpire(&itr->pr);
1930*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.flags = itr->pr.ndpr_stateflags;
1931*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.refcnt = itr->pr.ndpr_addrcnt;
1932*43a90889SApple OSS Distributions 		nd6_ra_msg_data.prefix.if_index = itr->pr.ndpr_ifp->if_index;
1933*43a90889SApple OSS Distributions 
1934*43a90889SApple OSS Distributions 		/* send the message up */
1935*43a90889SApple OSS Distributions 		ev_msg.dv[0].data_ptr           = &nd6_ra_msg_data;
1936*43a90889SApple OSS Distributions 		ev_msg.dv[0].data_length        = sizeof(nd6_ra_msg_data);
1937*43a90889SApple OSS Distributions 		ev_msg.dv[1].data_length        = 0;
1938*43a90889SApple OSS Distributions 		dlil_post_complete_msg(NULL, &ev_msg);
1939*43a90889SApple OSS Distributions 
1940*43a90889SApple OSS Distributions 		/* clean up for the next prefix */
1941*43a90889SApple OSS Distributions 		bzero(&nd6_ra_msg_data.prefix, sizeof(nd6_ra_msg_data.prefix));
1942*43a90889SApple OSS Distributions 		itr = itr->next;
1943*43a90889SApple OSS Distributions 		nd6_ra_msg_data.list_index++;
1944*43a90889SApple OSS Distributions 	}
1945*43a90889SApple OSS Distributions }
1946*43a90889SApple OSS Distributions 
1947*43a90889SApple OSS Distributions /*
1948*43a90889SApple OSS Distributions  * Regenerate deprecated/invalidated temporary address
1949*43a90889SApple OSS Distributions  */
1950*43a90889SApple OSS Distributions static int
regen_tmpaddr(struct in6_ifaddr * ia6)1951*43a90889SApple OSS Distributions regen_tmpaddr(struct in6_ifaddr *ia6)
1952*43a90889SApple OSS Distributions {
1953*43a90889SApple OSS Distributions 	struct ifaddr *__single ifa;
1954*43a90889SApple OSS Distributions 	ifnet_ref_t ifp;
1955*43a90889SApple OSS Distributions 	struct in6_ifaddr *__single public_ifa6 = NULL;
1956*43a90889SApple OSS Distributions 	uint64_t timenow = net_uptime();
1957*43a90889SApple OSS Distributions 
1958*43a90889SApple OSS Distributions 	ifp = ia6->ia_ifa.ifa_ifp;
1959*43a90889SApple OSS Distributions 	ifnet_lock_shared(ifp);
1960*43a90889SApple OSS Distributions 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1961*43a90889SApple OSS Distributions 		struct in6_ifaddr *__single it6;
1962*43a90889SApple OSS Distributions 
1963*43a90889SApple OSS Distributions 		IFA_LOCK(ifa);
1964*43a90889SApple OSS Distributions 		if (ifa->ifa_addr->sa_family != AF_INET6) {
1965*43a90889SApple OSS Distributions 			IFA_UNLOCK(ifa);
1966*43a90889SApple OSS Distributions 			continue;
1967*43a90889SApple OSS Distributions 		}
1968*43a90889SApple OSS Distributions 		it6 = ifatoia6(ifa);
1969*43a90889SApple OSS Distributions 
1970*43a90889SApple OSS Distributions 		/* ignore no autoconf addresses. */
1971*43a90889SApple OSS Distributions 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) {
1972*43a90889SApple OSS Distributions 			IFA_UNLOCK(ifa);
1973*43a90889SApple OSS Distributions 			continue;
1974*43a90889SApple OSS Distributions 		}
1975*43a90889SApple OSS Distributions 		/* ignore autoconf addresses with different prefixes. */
1976*43a90889SApple OSS Distributions 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) {
1977*43a90889SApple OSS Distributions 			IFA_UNLOCK(ifa);
1978*43a90889SApple OSS Distributions 			continue;
1979*43a90889SApple OSS Distributions 		}
1980*43a90889SApple OSS Distributions 		/*
1981*43a90889SApple OSS Distributions 		 * Now we are looking at an autoconf address with the same
1982*43a90889SApple OSS Distributions 		 * prefix as ours.  If the address is temporary and is still
1983*43a90889SApple OSS Distributions 		 * preferred, do not create another one.  It would be rare, but
1984*43a90889SApple OSS Distributions 		 * could happen, for example, when we resume a laptop PC after
1985*43a90889SApple OSS Distributions 		 * a long period.
1986*43a90889SApple OSS Distributions 		 */
1987*43a90889SApple OSS Distributions 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1988*43a90889SApple OSS Distributions 		    !IFA6_IS_DEPRECATED(it6, timenow)) {
1989*43a90889SApple OSS Distributions 			IFA_UNLOCK(ifa);
1990*43a90889SApple OSS Distributions 			if (public_ifa6 != NULL) {
1991*43a90889SApple OSS Distributions 				ifa_remref(&public_ifa6->ia_ifa);
1992*43a90889SApple OSS Distributions 			}
1993*43a90889SApple OSS Distributions 			public_ifa6 = NULL;
1994*43a90889SApple OSS Distributions 			break;
1995*43a90889SApple OSS Distributions 		}
1996*43a90889SApple OSS Distributions 
1997*43a90889SApple OSS Distributions 		/*
1998*43a90889SApple OSS Distributions 		 * This is a public autoconf address that has the same prefix
1999*43a90889SApple OSS Distributions 		 * as ours.  If it is preferred, keep it.  We can't break the
2000*43a90889SApple OSS Distributions 		 * loop here, because there may be a still-preferred temporary
2001*43a90889SApple OSS Distributions 		 * address with the prefix.
2002*43a90889SApple OSS Distributions 		 */
2003*43a90889SApple OSS Distributions 		if (!IFA6_IS_DEPRECATED(it6, timenow)) {
2004*43a90889SApple OSS Distributions 			ifa_addref(ifa); /* for public_ifa6 */
2005*43a90889SApple OSS Distributions 			IFA_UNLOCK(ifa);
2006*43a90889SApple OSS Distributions 			if (public_ifa6 != NULL) {
2007*43a90889SApple OSS Distributions 				ifa_remref(&public_ifa6->ia_ifa);
2008*43a90889SApple OSS Distributions 			}
2009*43a90889SApple OSS Distributions 			public_ifa6 = it6;
2010*43a90889SApple OSS Distributions 		} else {
2011*43a90889SApple OSS Distributions 			IFA_UNLOCK(ifa);
2012*43a90889SApple OSS Distributions 		}
2013*43a90889SApple OSS Distributions 	}
2014*43a90889SApple OSS Distributions 	ifnet_lock_done(ifp);
2015*43a90889SApple OSS Distributions 
2016*43a90889SApple OSS Distributions 	if (public_ifa6 != NULL) {
2017*43a90889SApple OSS Distributions 		int e;
2018*43a90889SApple OSS Distributions 
2019*43a90889SApple OSS Distributions 		if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
2020*43a90889SApple OSS Distributions 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
2021*43a90889SApple OSS Distributions 			    " tmp addr,errno=%d\n", e);
2022*43a90889SApple OSS Distributions 			ifa_remref(&public_ifa6->ia_ifa);
2023*43a90889SApple OSS Distributions 			return -1;
2024*43a90889SApple OSS Distributions 		}
2025*43a90889SApple OSS Distributions 		ifa_remref(&public_ifa6->ia_ifa);
2026*43a90889SApple OSS Distributions 		return 0;
2027*43a90889SApple OSS Distributions 	}
2028*43a90889SApple OSS Distributions 
2029*43a90889SApple OSS Distributions 	return -1;
2030*43a90889SApple OSS Distributions }
2031*43a90889SApple OSS Distributions 
2032*43a90889SApple OSS Distributions static void
nd6_purge_interface_default_routers(struct ifnet * ifp)2033*43a90889SApple OSS Distributions nd6_purge_interface_default_routers(struct ifnet *ifp)
2034*43a90889SApple OSS Distributions {
2035*43a90889SApple OSS Distributions 	struct nd_defrouter *__single dr = NULL;
2036*43a90889SApple OSS Distributions 	struct nd_defrouter *__single ndr = NULL;
2037*43a90889SApple OSS Distributions 	struct nd_drhead nd_defrouter_tmp = {};
2038*43a90889SApple OSS Distributions 
2039*43a90889SApple OSS Distributions 	TAILQ_INIT(&nd_defrouter_tmp);
2040*43a90889SApple OSS Distributions 
2041*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
2042*43a90889SApple OSS Distributions 
2043*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter_list, dr_entry, ndr) {
2044*43a90889SApple OSS Distributions 		if (dr->ifp != ifp) {
2045*43a90889SApple OSS Distributions 			continue;
2046*43a90889SApple OSS Distributions 		}
2047*43a90889SApple OSS Distributions 		/*
2048*43a90889SApple OSS Distributions 		 * Remove the entry from default router list
2049*43a90889SApple OSS Distributions 		 * and add it to the temp list.
2050*43a90889SApple OSS Distributions 		 * nd_defrouter_tmp will be a local temporary
2051*43a90889SApple OSS Distributions 		 * list as no one else can get the same
2052*43a90889SApple OSS Distributions 		 * removed entry once it is removed from default
2053*43a90889SApple OSS Distributions 		 * router list.
2054*43a90889SApple OSS Distributions 		 * Remove the reference after calling defrtrlist_del.
2055*43a90889SApple OSS Distributions 		 *
2056*43a90889SApple OSS Distributions 		 * The uninstalled entries have to be iterated first
2057*43a90889SApple OSS Distributions 		 * when we call defrtrlist_del.
2058*43a90889SApple OSS Distributions 		 * This is to ensure that we don't end up calling
2059*43a90889SApple OSS Distributions 		 * default router  selection when there are other
2060*43a90889SApple OSS Distributions 		 * uninstalled candidate default routers on
2061*43a90889SApple OSS Distributions 		 * the interface.
2062*43a90889SApple OSS Distributions 		 * If we don't respect that order, we may end
2063*43a90889SApple OSS Distributions 		 * up missing out on some entries.
2064*43a90889SApple OSS Distributions 		 *
2065*43a90889SApple OSS Distributions 		 * For that reason, installed ones must be inserted
2066*43a90889SApple OSS Distributions 		 * at the tail and uninstalled ones at the head
2067*43a90889SApple OSS Distributions 		 */
2068*43a90889SApple OSS Distributions 		TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
2069*43a90889SApple OSS Distributions 
2070*43a90889SApple OSS Distributions 		if (dr->stateflags & NDDRF_INSTALLED) {
2071*43a90889SApple OSS Distributions 			TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
2072*43a90889SApple OSS Distributions 		} else {
2073*43a90889SApple OSS Distributions 			TAILQ_INSERT_HEAD(&nd_defrouter_tmp, dr, dr_entry);
2074*43a90889SApple OSS Distributions 		}
2075*43a90889SApple OSS Distributions 	}
2076*43a90889SApple OSS Distributions 
2077*43a90889SApple OSS Distributions 	/*
2078*43a90889SApple OSS Distributions 	 * The following call to defrtrlist_del should be
2079*43a90889SApple OSS Distributions 	 * safe as we are iterating a local list of
2080*43a90889SApple OSS Distributions 	 * default routers.
2081*43a90889SApple OSS Distributions 	 *
2082*43a90889SApple OSS Distributions 	 * We don't really need nd6_mutex here but keeping
2083*43a90889SApple OSS Distributions 	 * it as it is to avoid changing assertions held in
2084*43a90889SApple OSS Distributions 	 * the functions in the call-path.
2085*43a90889SApple OSS Distributions 	 */
2086*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter_tmp, dr_entry, ndr) {
2087*43a90889SApple OSS Distributions 		TAILQ_REMOVE(&nd_defrouter_tmp, dr, dr_entry);
2088*43a90889SApple OSS Distributions 		defrtrlist_del(dr, NULL);
2089*43a90889SApple OSS Distributions 		NDDR_REMREF(dr);        /* remove list reference */
2090*43a90889SApple OSS Distributions 	}
2091*43a90889SApple OSS Distributions }
2092*43a90889SApple OSS Distributions 
2093*43a90889SApple OSS Distributions static void
nd6_purge_interface_prefixes(struct ifnet * ifp)2094*43a90889SApple OSS Distributions nd6_purge_interface_prefixes(struct ifnet *ifp)
2095*43a90889SApple OSS Distributions {
2096*43a90889SApple OSS Distributions 	boolean_t removed = FALSE;
2097*43a90889SApple OSS Distributions 	struct nd_prefix *__single pr = NULL;
2098*43a90889SApple OSS Distributions 	struct nd_prefix *__single npr = NULL;
2099*43a90889SApple OSS Distributions 
2100*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
2101*43a90889SApple OSS Distributions 
2102*43a90889SApple OSS Distributions 	/* Nuke prefix list entries toward ifp */
2103*43a90889SApple OSS Distributions 	for (pr = nd_prefix.lh_first; pr; pr = npr) {
2104*43a90889SApple OSS Distributions 		NDPR_LOCK(pr);
2105*43a90889SApple OSS Distributions 		npr = pr->ndpr_next;
2106*43a90889SApple OSS Distributions 		if (pr->ndpr_ifp == ifp &&
2107*43a90889SApple OSS Distributions 		    !(pr->ndpr_stateflags & NDPRF_DEFUNCT)) {
2108*43a90889SApple OSS Distributions 			/*
2109*43a90889SApple OSS Distributions 			 * Because if_detach() does *not* release prefixes
2110*43a90889SApple OSS Distributions 			 * while purging addresses the reference count will
2111*43a90889SApple OSS Distributions 			 * still be above zero. We therefore reset it to
2112*43a90889SApple OSS Distributions 			 * make sure that the prefix really gets purged.
2113*43a90889SApple OSS Distributions 			 */
2114*43a90889SApple OSS Distributions 			pr->ndpr_addrcnt = 0;
2115*43a90889SApple OSS Distributions 
2116*43a90889SApple OSS Distributions 			/*
2117*43a90889SApple OSS Distributions 			 * Previously, pr->ndpr_addr is removed as well,
2118*43a90889SApple OSS Distributions 			 * but I strongly believe we don't have to do it.
2119*43a90889SApple OSS Distributions 			 * nd6_purge() is only called from in6_ifdetach(),
2120*43a90889SApple OSS Distributions 			 * which removes all the associated interface addresses
2121*43a90889SApple OSS Distributions 			 * by itself.
2122*43a90889SApple OSS Distributions 			 * ([email protected] 20010129)
2123*43a90889SApple OSS Distributions 			 */
2124*43a90889SApple OSS Distributions 			NDPR_ADDREF(pr);
2125*43a90889SApple OSS Distributions 			prelist_remove(pr);
2126*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
2127*43a90889SApple OSS Distributions 			NDPR_REMREF(pr);
2128*43a90889SApple OSS Distributions 			removed = TRUE;
2129*43a90889SApple OSS Distributions 			npr = nd_prefix.lh_first;
2130*43a90889SApple OSS Distributions 		} else {
2131*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
2132*43a90889SApple OSS Distributions 		}
2133*43a90889SApple OSS Distributions 	}
2134*43a90889SApple OSS Distributions 	if (removed) {
2135*43a90889SApple OSS Distributions 		pfxlist_onlink_check();
2136*43a90889SApple OSS Distributions 	}
2137*43a90889SApple OSS Distributions }
2138*43a90889SApple OSS Distributions 
2139*43a90889SApple OSS Distributions static void
nd6_router_select_rti_entries(struct ifnet * ifp)2140*43a90889SApple OSS Distributions nd6_router_select_rti_entries(struct ifnet *ifp)
2141*43a90889SApple OSS Distributions {
2142*43a90889SApple OSS Distributions 	struct nd_route_info *__single rti = NULL;
2143*43a90889SApple OSS Distributions 	struct nd_route_info *__single rti_next = NULL;
2144*43a90889SApple OSS Distributions 
2145*43a90889SApple OSS Distributions 	nd6_rti_list_wait(__func__);
2146*43a90889SApple OSS Distributions 
2147*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(rti, &nd_rti_list, nd_rti_entry, rti_next) {
2148*43a90889SApple OSS Distributions 		defrouter_select(ifp, &rti->nd_rti_router_list);
2149*43a90889SApple OSS Distributions 	}
2150*43a90889SApple OSS Distributions 
2151*43a90889SApple OSS Distributions 	nd6_rti_list_signal_done();
2152*43a90889SApple OSS Distributions }
2153*43a90889SApple OSS Distributions 
2154*43a90889SApple OSS Distributions static void
nd6_purge_interface_rti_entries(struct ifnet * ifp)2155*43a90889SApple OSS Distributions nd6_purge_interface_rti_entries(struct ifnet *ifp)
2156*43a90889SApple OSS Distributions {
2157*43a90889SApple OSS Distributions 	struct nd_route_info *__single rti = NULL;
2158*43a90889SApple OSS Distributions 	struct nd_route_info *__single rti_next = NULL;
2159*43a90889SApple OSS Distributions 
2160*43a90889SApple OSS Distributions 	nd6_rti_list_wait(__func__);
2161*43a90889SApple OSS Distributions 
2162*43a90889SApple OSS Distributions 	TAILQ_FOREACH_SAFE(rti, &nd_rti_list, nd_rti_entry, rti_next) {
2163*43a90889SApple OSS Distributions 		struct nd_route_info rti_tmp = {};
2164*43a90889SApple OSS Distributions 		struct nd_defrouter *__single dr = NULL;
2165*43a90889SApple OSS Distributions 		struct nd_defrouter *__single ndr = NULL;
2166*43a90889SApple OSS Distributions 
2167*43a90889SApple OSS Distributions 		rti_tmp.nd_rti_prefix = rti->nd_rti_prefix;
2168*43a90889SApple OSS Distributions 		rti_tmp.nd_rti_prefixlen = rti->nd_rti_prefixlen;
2169*43a90889SApple OSS Distributions 		TAILQ_INIT(&rti_tmp.nd_rti_router_list);
2170*43a90889SApple OSS Distributions 
2171*43a90889SApple OSS Distributions 		TAILQ_FOREACH_SAFE(dr, &rti->nd_rti_router_list, dr_entry, ndr) {
2172*43a90889SApple OSS Distributions 			/*
2173*43a90889SApple OSS Distributions 			 * If ifp is provided, skip the entries that don't match.
2174*43a90889SApple OSS Distributions 			 * Else it is treated as a purge.
2175*43a90889SApple OSS Distributions 			 */
2176*43a90889SApple OSS Distributions 			if (ifp != NULL && dr->ifp != ifp) {
2177*43a90889SApple OSS Distributions 				continue;
2178*43a90889SApple OSS Distributions 			}
2179*43a90889SApple OSS Distributions 
2180*43a90889SApple OSS Distributions 			/*
2181*43a90889SApple OSS Distributions 			 * Remove the entry from rti's router list
2182*43a90889SApple OSS Distributions 			 * and add it to the temp list.
2183*43a90889SApple OSS Distributions 			 * Remove the reference after calling defrtrlist_del.
2184*43a90889SApple OSS Distributions 			 *
2185*43a90889SApple OSS Distributions 			 * The uninstalled entries have to be iterated first
2186*43a90889SApple OSS Distributions 			 * when we call defrtrlist_del.
2187*43a90889SApple OSS Distributions 			 * This is to ensure that we don't end up calling
2188*43a90889SApple OSS Distributions 			 * router  selection when there are other
2189*43a90889SApple OSS Distributions 			 * uninstalled candidate default routers on
2190*43a90889SApple OSS Distributions 			 * the interface.
2191*43a90889SApple OSS Distributions 			 * If we don't respect that order, we may end
2192*43a90889SApple OSS Distributions 			 * up missing out on some entries.
2193*43a90889SApple OSS Distributions 			 *
2194*43a90889SApple OSS Distributions 			 * For that reason, installed ones must be inserted
2195*43a90889SApple OSS Distributions 			 * at the tail and uninstalled ones at the head
2196*43a90889SApple OSS Distributions 			 */
2197*43a90889SApple OSS Distributions 
2198*43a90889SApple OSS Distributions 			TAILQ_REMOVE(&rti->nd_rti_router_list, dr, dr_entry);
2199*43a90889SApple OSS Distributions 			if (dr->stateflags & NDDRF_INSTALLED) {
2200*43a90889SApple OSS Distributions 				TAILQ_INSERT_TAIL(&rti_tmp.nd_rti_router_list, dr, dr_entry);
2201*43a90889SApple OSS Distributions 			} else {
2202*43a90889SApple OSS Distributions 				TAILQ_INSERT_HEAD(&rti_tmp.nd_rti_router_list, dr, dr_entry);
2203*43a90889SApple OSS Distributions 			}
2204*43a90889SApple OSS Distributions 		}
2205*43a90889SApple OSS Distributions 
2206*43a90889SApple OSS Distributions 		/*
2207*43a90889SApple OSS Distributions 		 * The following call to defrtrlist_del should be
2208*43a90889SApple OSS Distributions 		 * safe as we are iterating a local list of
2209*43a90889SApple OSS Distributions 		 * routers.
2210*43a90889SApple OSS Distributions 		 *
2211*43a90889SApple OSS Distributions 		 * We don't really need nd6_mutex here but keeping
2212*43a90889SApple OSS Distributions 		 * it as it is to avoid changing assertions held in
2213*43a90889SApple OSS Distributions 		 * the functions in the call-path.
2214*43a90889SApple OSS Distributions 		 */
2215*43a90889SApple OSS Distributions 		TAILQ_FOREACH_SAFE(dr, &rti_tmp.nd_rti_router_list, dr_entry, ndr) {
2216*43a90889SApple OSS Distributions 			TAILQ_REMOVE(&rti_tmp.nd_rti_router_list, dr, dr_entry);
2217*43a90889SApple OSS Distributions 			defrtrlist_del(dr, &rti->nd_rti_router_list);
2218*43a90889SApple OSS Distributions 			NDDR_REMREF(dr);        /* remove list reference */
2219*43a90889SApple OSS Distributions 		}
2220*43a90889SApple OSS Distributions 		/*
2221*43a90889SApple OSS Distributions 		 * The above may have removed an entry from default router list.
2222*43a90889SApple OSS Distributions 		 * If it did and the list is now empty, remove the rti as well.
2223*43a90889SApple OSS Distributions 		 */
2224*43a90889SApple OSS Distributions 		if (TAILQ_EMPTY(&rti->nd_rti_router_list)) {
2225*43a90889SApple OSS Distributions 			TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
2226*43a90889SApple OSS Distributions 			ndrti_free(rti);
2227*43a90889SApple OSS Distributions 		}
2228*43a90889SApple OSS Distributions 	}
2229*43a90889SApple OSS Distributions 
2230*43a90889SApple OSS Distributions 	nd6_rti_list_signal_done();
2231*43a90889SApple OSS Distributions }
2232*43a90889SApple OSS Distributions 
2233*43a90889SApple OSS Distributions static void
nd6_purge_interface_llinfo(struct ifnet * ifp)2234*43a90889SApple OSS Distributions nd6_purge_interface_llinfo(struct ifnet *ifp)
2235*43a90889SApple OSS Distributions {
2236*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = NULL;
2237*43a90889SApple OSS Distributions 	/* Note that rt->rt_ifp may not be the same as ifp,
2238*43a90889SApple OSS Distributions 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
2239*43a90889SApple OSS Distributions 	 * nd6_rtrequest(), and ip6_input().
2240*43a90889SApple OSS Distributions 	 */
2241*43a90889SApple OSS Distributions again:
2242*43a90889SApple OSS Distributions 	lck_mtx_lock(rnh_lock);
2243*43a90889SApple OSS Distributions 	ln = llinfo_nd6.ln_next;
2244*43a90889SApple OSS Distributions 	while (ln != NULL && ln != &llinfo_nd6) {
2245*43a90889SApple OSS Distributions 		rtentry_ref_t rt;
2246*43a90889SApple OSS Distributions 		struct llinfo_nd6 *__single nln;
2247*43a90889SApple OSS Distributions 
2248*43a90889SApple OSS Distributions 		nln = ln->ln_next;
2249*43a90889SApple OSS Distributions 		rt = ln->ln_rt;
2250*43a90889SApple OSS Distributions 		RT_LOCK(rt);
2251*43a90889SApple OSS Distributions 		if (rt->rt_gateway != NULL &&
2252*43a90889SApple OSS Distributions 		    rt->rt_gateway->sa_family == AF_LINK &&
2253*43a90889SApple OSS Distributions 		    SDL(rt->rt_gateway)->sdl_index == ifp->if_index) {
2254*43a90889SApple OSS Distributions 			RT_ADDREF_LOCKED(rt);
2255*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
2256*43a90889SApple OSS Distributions 			lck_mtx_unlock(rnh_lock);
2257*43a90889SApple OSS Distributions 			/*
2258*43a90889SApple OSS Distributions 			 * See comments on nd6_service() for reasons why
2259*43a90889SApple OSS Distributions 			 * this loop is repeated; we bite the costs of
2260*43a90889SApple OSS Distributions 			 * going thru the same llinfo_nd6 more than once
2261*43a90889SApple OSS Distributions 			 * here, since this purge happens during detach,
2262*43a90889SApple OSS Distributions 			 * and that unlike the timer case, it's possible
2263*43a90889SApple OSS Distributions 			 * there's more than one purges happening at the
2264*43a90889SApple OSS Distributions 			 * same time (thus a flag wouldn't buy anything).
2265*43a90889SApple OSS Distributions 			 */
2266*43a90889SApple OSS Distributions 			nd6_free(rt);
2267*43a90889SApple OSS Distributions 			RT_REMREF(rt);
2268*43a90889SApple OSS Distributions 			LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2269*43a90889SApple OSS Distributions 			goto again;
2270*43a90889SApple OSS Distributions 		} else {
2271*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
2272*43a90889SApple OSS Distributions 		}
2273*43a90889SApple OSS Distributions 		ln = nln;
2274*43a90889SApple OSS Distributions 	}
2275*43a90889SApple OSS Distributions 	lck_mtx_unlock(rnh_lock);
2276*43a90889SApple OSS Distributions }
2277*43a90889SApple OSS Distributions 
2278*43a90889SApple OSS Distributions /*
2279*43a90889SApple OSS Distributions  * Nuke neighbor cache/prefix/default router management table, right before
2280*43a90889SApple OSS Distributions  * ifp goes away.
2281*43a90889SApple OSS Distributions  */
2282*43a90889SApple OSS Distributions void
nd6_purge(struct ifnet * ifp)2283*43a90889SApple OSS Distributions nd6_purge(struct ifnet *ifp)
2284*43a90889SApple OSS Distributions {
2285*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2286*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
2287*43a90889SApple OSS Distributions 
2288*43a90889SApple OSS Distributions 	/* Nuke default router list entries toward ifp */
2289*43a90889SApple OSS Distributions 	nd6_purge_interface_default_routers(ifp);
2290*43a90889SApple OSS Distributions 
2291*43a90889SApple OSS Distributions 	/* Nuke prefix list entries toward ifp */
2292*43a90889SApple OSS Distributions 	nd6_purge_interface_prefixes(ifp);
2293*43a90889SApple OSS Distributions 
2294*43a90889SApple OSS Distributions 	/* Nuke route info option entries toward ifp */
2295*43a90889SApple OSS Distributions 	nd6_purge_interface_rti_entries(ifp);
2296*43a90889SApple OSS Distributions 
2297*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
2298*43a90889SApple OSS Distributions 
2299*43a90889SApple OSS Distributions 	/* cancel default outgoing interface setting */
2300*43a90889SApple OSS Distributions 	if (nd6_defifindex == ifp->if_index) {
2301*43a90889SApple OSS Distributions 		nd6_setdefaultiface(0);
2302*43a90889SApple OSS Distributions 	}
2303*43a90889SApple OSS Distributions 
2304*43a90889SApple OSS Distributions 	/*
2305*43a90889SApple OSS Distributions 	 * Perform default router selection even when we are a router,
2306*43a90889SApple OSS Distributions 	 * if Scoped Routing is enabled.
2307*43a90889SApple OSS Distributions 	 * XXX ?Should really not be needed since when defrouter_select
2308*43a90889SApple OSS Distributions 	 * was changed to work on interface.
2309*43a90889SApple OSS Distributions 	 */
2310*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
2311*43a90889SApple OSS Distributions 	/* refresh default router list */
2312*43a90889SApple OSS Distributions 	defrouter_select(ifp, NULL);
2313*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
2314*43a90889SApple OSS Distributions 
2315*43a90889SApple OSS Distributions 	/* Nuke neighbor cache entries for the ifp. */
2316*43a90889SApple OSS Distributions 	nd6_purge_interface_llinfo(ifp);
2317*43a90889SApple OSS Distributions }
2318*43a90889SApple OSS Distributions 
2319*43a90889SApple OSS Distributions /*
2320*43a90889SApple OSS Distributions  * Upon success, the returned route will be locked and the caller is
2321*43a90889SApple OSS Distributions  * responsible for releasing the reference and doing RT_UNLOCK(rt).
2322*43a90889SApple OSS Distributions  * This routine does not require rnh_lock to be held by the caller,
2323*43a90889SApple OSS Distributions  * although it needs to be indicated of such a case in order to call
2324*43a90889SApple OSS Distributions  * the correct variant of the relevant routing routines.
2325*43a90889SApple OSS Distributions  */
2326*43a90889SApple OSS Distributions struct rtentry *
nd6_lookup(struct in6_addr * addr6,int create,struct ifnet * ifp,int rt_locked)2327*43a90889SApple OSS Distributions nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp, int rt_locked)
2328*43a90889SApple OSS Distributions {
2329*43a90889SApple OSS Distributions 	rtentry_ref_t rt;
2330*43a90889SApple OSS Distributions 	struct sockaddr_in6 sin6;
2331*43a90889SApple OSS Distributions 	unsigned int ifscope;
2332*43a90889SApple OSS Distributions 
2333*43a90889SApple OSS Distributions 	SOCKADDR_ZERO(&sin6, sizeof(sin6));
2334*43a90889SApple OSS Distributions 	sin6.sin6_len = sizeof(struct sockaddr_in6);
2335*43a90889SApple OSS Distributions 	sin6.sin6_family = AF_INET6;
2336*43a90889SApple OSS Distributions 	sin6.sin6_addr = *addr6;
2337*43a90889SApple OSS Distributions 
2338*43a90889SApple OSS Distributions 	ifscope = (ifp != NULL) ? ifp->if_index : IFSCOPE_NONE;
2339*43a90889SApple OSS Distributions 	if (rt_locked) {
2340*43a90889SApple OSS Distributions 		LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
2341*43a90889SApple OSS Distributions 		rt = rtalloc1_scoped_locked(SA(&sin6), create, 0, ifscope);
2342*43a90889SApple OSS Distributions 	} else {
2343*43a90889SApple OSS Distributions 		rt = rtalloc1_scoped(SA(&sin6), create, 0, ifscope);
2344*43a90889SApple OSS Distributions 	}
2345*43a90889SApple OSS Distributions 
2346*43a90889SApple OSS Distributions 	if (rt != NULL) {
2347*43a90889SApple OSS Distributions 		RT_LOCK(rt);
2348*43a90889SApple OSS Distributions 		if ((rt->rt_flags & RTF_LLINFO) == 0) {
2349*43a90889SApple OSS Distributions 			/*
2350*43a90889SApple OSS Distributions 			 * This is the case for the default route.
2351*43a90889SApple OSS Distributions 			 * If we want to create a neighbor cache for the
2352*43a90889SApple OSS Distributions 			 * address, we should free the route for the
2353*43a90889SApple OSS Distributions 			 * destination and allocate an interface route.
2354*43a90889SApple OSS Distributions 			 */
2355*43a90889SApple OSS Distributions 			if (create) {
2356*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
2357*43a90889SApple OSS Distributions 				if (rt_locked) {
2358*43a90889SApple OSS Distributions 					rtfree_locked(rt);
2359*43a90889SApple OSS Distributions 				} else {
2360*43a90889SApple OSS Distributions 					rtfree(rt);
2361*43a90889SApple OSS Distributions 				}
2362*43a90889SApple OSS Distributions 				rt = NULL;
2363*43a90889SApple OSS Distributions 			}
2364*43a90889SApple OSS Distributions 		}
2365*43a90889SApple OSS Distributions 	}
2366*43a90889SApple OSS Distributions 	if (rt == NULL) {
2367*43a90889SApple OSS Distributions 		if (create && ifp) {
2368*43a90889SApple OSS Distributions 			struct ifaddr *__single ifa;
2369*43a90889SApple OSS Distributions 			u_int32_t ifa_flags;
2370*43a90889SApple OSS Distributions 			int e;
2371*43a90889SApple OSS Distributions 
2372*43a90889SApple OSS Distributions 			/*
2373*43a90889SApple OSS Distributions 			 * If no route is available and create is set,
2374*43a90889SApple OSS Distributions 			 * we allocate a host route for the destination
2375*43a90889SApple OSS Distributions 			 * and treat it like an interface route.
2376*43a90889SApple OSS Distributions 			 * This hack is necessary for a neighbor which can't
2377*43a90889SApple OSS Distributions 			 * be covered by our own prefix.
2378*43a90889SApple OSS Distributions 			 */
2379*43a90889SApple OSS Distributions 			ifa = ifaof_ifpforaddr(SA(&sin6), ifp);
2380*43a90889SApple OSS Distributions 			if (ifa == NULL) {
2381*43a90889SApple OSS Distributions 				return NULL;
2382*43a90889SApple OSS Distributions 			}
2383*43a90889SApple OSS Distributions 
2384*43a90889SApple OSS Distributions 			/*
2385*43a90889SApple OSS Distributions 			 * Create a new route.  RTF_LLINFO is necessary
2386*43a90889SApple OSS Distributions 			 * to create a Neighbor Cache entry for the
2387*43a90889SApple OSS Distributions 			 * destination in nd6_rtrequest which will be
2388*43a90889SApple OSS Distributions 			 * called in rtrequest via ifa->ifa_rtrequest.
2389*43a90889SApple OSS Distributions 			 */
2390*43a90889SApple OSS Distributions 			if (!rt_locked) {
2391*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
2392*43a90889SApple OSS Distributions 			}
2393*43a90889SApple OSS Distributions 			IFA_LOCK_SPIN(ifa);
2394*43a90889SApple OSS Distributions 			ifa_flags = ifa->ifa_flags;
2395*43a90889SApple OSS Distributions 			IFA_UNLOCK(ifa);
2396*43a90889SApple OSS Distributions 			e = rtrequest_scoped_locked(RTM_ADD, SA(&sin6), ifa->ifa_addr, SA(&all1_sa),
2397*43a90889SApple OSS Distributions 			    (ifa_flags | RTF_HOST | RTF_LLINFO) & ~RTF_CLONING, &rt, ifscope);
2398*43a90889SApple OSS Distributions 			if (e != 0) {
2399*43a90889SApple OSS Distributions 				if (e != EEXIST) {
2400*43a90889SApple OSS Distributions 					log(LOG_ERR, "%s: failed to add route "
2401*43a90889SApple OSS Distributions 					    "for a neighbor(%s), errno=%d\n",
2402*43a90889SApple OSS Distributions 					    __func__, ip6_sprintf(addr6), e);
2403*43a90889SApple OSS Distributions 				}
2404*43a90889SApple OSS Distributions 			}
2405*43a90889SApple OSS Distributions 			if (!rt_locked) {
2406*43a90889SApple OSS Distributions 				lck_mtx_unlock(rnh_lock);
2407*43a90889SApple OSS Distributions 			}
2408*43a90889SApple OSS Distributions 			ifa_remref(ifa);
2409*43a90889SApple OSS Distributions 			if (rt == NULL) {
2410*43a90889SApple OSS Distributions 				return NULL;
2411*43a90889SApple OSS Distributions 			}
2412*43a90889SApple OSS Distributions 
2413*43a90889SApple OSS Distributions 			RT_LOCK(rt);
2414*43a90889SApple OSS Distributions 			if (rt->rt_llinfo) {
2415*43a90889SApple OSS Distributions 				struct llinfo_nd6 *__single ln = rt->rt_llinfo;
2416*43a90889SApple OSS Distributions 				boolean_t nud_enabled = FALSE;
2417*43a90889SApple OSS Distributions 
2418*43a90889SApple OSS Distributions 				/*
2419*43a90889SApple OSS Distributions 				 * The IPv6 initialization of the loopback interface
2420*43a90889SApple OSS Distributions 				 * may happen after another interface gets assigned
2421*43a90889SApple OSS Distributions 				 * an IPv6 address.
2422*43a90889SApple OSS Distributions 				 * To avoid asserting treat local routes as special
2423*43a90889SApple OSS Distributions 				 * case.
2424*43a90889SApple OSS Distributions 				 */
2425*43a90889SApple OSS Distributions 				if (rt->rt_ifp != lo_ifp) {
2426*43a90889SApple OSS Distributions 					struct nd_ifinfo *__single ndi = ND_IFINFO(rt->rt_ifp);
2427*43a90889SApple OSS Distributions 					VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
2428*43a90889SApple OSS Distributions 					nud_enabled = !!(ndi->flags & ND6_IFF_PERFORMNUD);
2429*43a90889SApple OSS Distributions 				}
2430*43a90889SApple OSS Distributions 
2431*43a90889SApple OSS Distributions 				/*
2432*43a90889SApple OSS Distributions 				 * For interface's that do not perform NUD
2433*43a90889SApple OSS Distributions 				 * neighbor cache entres must always be marked
2434*43a90889SApple OSS Distributions 				 * reachable with no expiry
2435*43a90889SApple OSS Distributions 				 */
2436*43a90889SApple OSS Distributions 				if (nud_enabled) {
2437*43a90889SApple OSS Distributions 					ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_NOSTATE);
2438*43a90889SApple OSS Distributions 				} else {
2439*43a90889SApple OSS Distributions 					ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2440*43a90889SApple OSS Distributions 					ln_setexpire(ln, 0);
2441*43a90889SApple OSS Distributions 				}
2442*43a90889SApple OSS Distributions 			}
2443*43a90889SApple OSS Distributions 		} else {
2444*43a90889SApple OSS Distributions 			return NULL;
2445*43a90889SApple OSS Distributions 		}
2446*43a90889SApple OSS Distributions 	}
2447*43a90889SApple OSS Distributions 	RT_LOCK_ASSERT_HELD(rt);
2448*43a90889SApple OSS Distributions 	/*
2449*43a90889SApple OSS Distributions 	 * Validation for the entry.
2450*43a90889SApple OSS Distributions 	 * Note that the check for rt_llinfo is necessary because a cloned
2451*43a90889SApple OSS Distributions 	 * route from a parent route that has the L flag (e.g. the default
2452*43a90889SApple OSS Distributions 	 * route to a p2p interface) may have the flag, too, while the
2453*43a90889SApple OSS Distributions 	 * destination is not actually a neighbor.
2454*43a90889SApple OSS Distributions 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
2455*43a90889SApple OSS Distributions 	 *	it might be the loopback interface if the entry is for our
2456*43a90889SApple OSS Distributions 	 *	own address on a non-loopback interface. Instead, we should
2457*43a90889SApple OSS Distributions 	 *	use rt->rt_ifa->ifa_ifp, which would specify the REAL
2458*43a90889SApple OSS Distributions 	 *	interface.
2459*43a90889SApple OSS Distributions 	 * Note also that ifa_ifp and ifp may differ when we connect two
2460*43a90889SApple OSS Distributions 	 * interfaces to a same link, install a link prefix to an interface,
2461*43a90889SApple OSS Distributions 	 * and try to install a neighbor cache on an interface that does not
2462*43a90889SApple OSS Distributions 	 * have a route to the prefix.
2463*43a90889SApple OSS Distributions 	 *
2464*43a90889SApple OSS Distributions 	 * If the address is from a proxied prefix, the ifa_ifp and ifp might
2465*43a90889SApple OSS Distributions 	 * not match, because nd6_na_input() could have modified the ifp
2466*43a90889SApple OSS Distributions 	 * of the route to point to the interface where the NA arrived on,
2467*43a90889SApple OSS Distributions 	 * hence the test for RTF_PROXY.
2468*43a90889SApple OSS Distributions 	 */
2469*43a90889SApple OSS Distributions 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
2470*43a90889SApple OSS Distributions 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
2471*43a90889SApple OSS Distributions 	    (ifp && rt->rt_ifa->ifa_ifp != ifp &&
2472*43a90889SApple OSS Distributions 	    !(rt->rt_flags & RTF_PROXY))) {
2473*43a90889SApple OSS Distributions 		RT_REMREF_LOCKED(rt);
2474*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
2475*43a90889SApple OSS Distributions 		if (create) {
2476*43a90889SApple OSS Distributions 			log(LOG_DEBUG, "%s: failed to lookup %s "
2477*43a90889SApple OSS Distributions 			    "(if = %s)\n", __func__, ip6_sprintf(addr6),
2478*43a90889SApple OSS Distributions 			    ifp ? if_name(ifp) : "unspec");
2479*43a90889SApple OSS Distributions 			/* xxx more logs... kazu */
2480*43a90889SApple OSS Distributions 		}
2481*43a90889SApple OSS Distributions 		return NULL;
2482*43a90889SApple OSS Distributions 	}
2483*43a90889SApple OSS Distributions 	/*
2484*43a90889SApple OSS Distributions 	 * Caller needs to release reference and call RT_UNLOCK(rt).
2485*43a90889SApple OSS Distributions 	 */
2486*43a90889SApple OSS Distributions 	return rt;
2487*43a90889SApple OSS Distributions }
2488*43a90889SApple OSS Distributions 
2489*43a90889SApple OSS Distributions /*
2490*43a90889SApple OSS Distributions  * Test whether a given IPv6 address is a neighbor or not, ignoring
2491*43a90889SApple OSS Distributions  * the actual neighbor cache.  The neighbor cache is ignored in order
2492*43a90889SApple OSS Distributions  * to not reenter the routing code from within itself.
2493*43a90889SApple OSS Distributions  */
2494*43a90889SApple OSS Distributions static int
nd6_is_new_addr_neighbor(struct sockaddr_in6 * addr,struct ifnet * ifp)2495*43a90889SApple OSS Distributions nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
2496*43a90889SApple OSS Distributions {
2497*43a90889SApple OSS Distributions 	struct nd_prefix *__single pr;
2498*43a90889SApple OSS Distributions 	struct ifaddr *__single dstaddr;
2499*43a90889SApple OSS Distributions 
2500*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
2501*43a90889SApple OSS Distributions 
2502*43a90889SApple OSS Distributions 	/*
2503*43a90889SApple OSS Distributions 	 * A link-local address is always a neighbor.
2504*43a90889SApple OSS Distributions 	 * XXX: a link does not necessarily specify a single interface.
2505*43a90889SApple OSS Distributions 	 */
2506*43a90889SApple OSS Distributions 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
2507*43a90889SApple OSS Distributions 		struct sockaddr_in6 sin6_copy;
2508*43a90889SApple OSS Distributions 		u_int32_t zone;
2509*43a90889SApple OSS Distributions 
2510*43a90889SApple OSS Distributions 		/*
2511*43a90889SApple OSS Distributions 		 * We need sin6_copy since sa6_recoverscope() may modify the
2512*43a90889SApple OSS Distributions 		 * content (XXX).
2513*43a90889SApple OSS Distributions 		 */
2514*43a90889SApple OSS Distributions 		sin6_copy = *addr;
2515*43a90889SApple OSS Distributions 		if (sa6_recoverscope(&sin6_copy, FALSE)) {
2516*43a90889SApple OSS Distributions 			return 0; /* XXX: should be impossible */
2517*43a90889SApple OSS Distributions 		}
2518*43a90889SApple OSS Distributions 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) {
2519*43a90889SApple OSS Distributions 			return 0;
2520*43a90889SApple OSS Distributions 		}
2521*43a90889SApple OSS Distributions 		if (sin6_copy.sin6_scope_id == zone) {
2522*43a90889SApple OSS Distributions 			return 1;
2523*43a90889SApple OSS Distributions 		} else {
2524*43a90889SApple OSS Distributions 			return 0;
2525*43a90889SApple OSS Distributions 		}
2526*43a90889SApple OSS Distributions 	}
2527*43a90889SApple OSS Distributions 
2528*43a90889SApple OSS Distributions 	/*
2529*43a90889SApple OSS Distributions 	 * If the address matches one of our addresses,
2530*43a90889SApple OSS Distributions 	 * it should be a neighbor.
2531*43a90889SApple OSS Distributions 	 * If the address matches one of our on-link prefixes, it should be a
2532*43a90889SApple OSS Distributions 	 * neighbor.
2533*43a90889SApple OSS Distributions 	 */
2534*43a90889SApple OSS Distributions 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2535*43a90889SApple OSS Distributions 		NDPR_LOCK(pr);
2536*43a90889SApple OSS Distributions 		if (pr->ndpr_ifp != ifp) {
2537*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
2538*43a90889SApple OSS Distributions 			continue;
2539*43a90889SApple OSS Distributions 		}
2540*43a90889SApple OSS Distributions 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
2541*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
2542*43a90889SApple OSS Distributions 			continue;
2543*43a90889SApple OSS Distributions 		}
2544*43a90889SApple OSS Distributions 		if (in6_are_masked_addr_scope_equal(&pr->ndpr_prefix.sin6_addr, pr->ndpr_prefix.sin6_scope_id,
2545*43a90889SApple OSS Distributions 		    &addr->sin6_addr, addr->sin6_scope_id, &pr->ndpr_mask)) {
2546*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
2547*43a90889SApple OSS Distributions 			return 1;
2548*43a90889SApple OSS Distributions 		}
2549*43a90889SApple OSS Distributions 		NDPR_UNLOCK(pr);
2550*43a90889SApple OSS Distributions 	}
2551*43a90889SApple OSS Distributions 
2552*43a90889SApple OSS Distributions 	/*
2553*43a90889SApple OSS Distributions 	 * If the address is assigned on the node of the other side of
2554*43a90889SApple OSS Distributions 	 * a p2p interface, the address should be a neighbor.
2555*43a90889SApple OSS Distributions 	 */
2556*43a90889SApple OSS Distributions 	dstaddr = ifa_ifwithdstaddr(SA(addr));
2557*43a90889SApple OSS Distributions 	if (dstaddr != NULL) {
2558*43a90889SApple OSS Distributions 		if (dstaddr->ifa_ifp == ifp) {
2559*43a90889SApple OSS Distributions 			ifa_remref(dstaddr);
2560*43a90889SApple OSS Distributions 			return 1;
2561*43a90889SApple OSS Distributions 		}
2562*43a90889SApple OSS Distributions 		ifa_remref(dstaddr);
2563*43a90889SApple OSS Distributions 		dstaddr = NULL;
2564*43a90889SApple OSS Distributions 	}
2565*43a90889SApple OSS Distributions 
2566*43a90889SApple OSS Distributions 	return 0;
2567*43a90889SApple OSS Distributions }
2568*43a90889SApple OSS Distributions 
2569*43a90889SApple OSS Distributions 
2570*43a90889SApple OSS Distributions /*
2571*43a90889SApple OSS Distributions  * Detect if a given IPv6 address identifies a neighbor on a given link.
2572*43a90889SApple OSS Distributions  * XXX: should take care of the destination of a p2p link?
2573*43a90889SApple OSS Distributions  */
2574*43a90889SApple OSS Distributions int
nd6_is_addr_neighbor(struct sockaddr_in6 * addr,struct ifnet * ifp,int rt_locked)2575*43a90889SApple OSS Distributions nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp,
2576*43a90889SApple OSS Distributions     int rt_locked)
2577*43a90889SApple OSS Distributions {
2578*43a90889SApple OSS Distributions 	rtentry_ref_t rt;
2579*43a90889SApple OSS Distributions 
2580*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
2581*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
2582*43a90889SApple OSS Distributions 	if (nd6_is_new_addr_neighbor(addr, ifp)) {
2583*43a90889SApple OSS Distributions 		lck_mtx_unlock(nd6_mutex);
2584*43a90889SApple OSS Distributions 		return 1;
2585*43a90889SApple OSS Distributions 	}
2586*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
2587*43a90889SApple OSS Distributions 
2588*43a90889SApple OSS Distributions 	/*
2589*43a90889SApple OSS Distributions 	 * Even if the address matches none of our addresses, it might be
2590*43a90889SApple OSS Distributions 	 * in the neighbor cache.
2591*43a90889SApple OSS Distributions 	 */
2592*43a90889SApple OSS Distributions 	if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp, rt_locked)) != NULL) {
2593*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_HELD(rt);
2594*43a90889SApple OSS Distributions 		RT_REMREF_LOCKED(rt);
2595*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
2596*43a90889SApple OSS Distributions 		return 1;
2597*43a90889SApple OSS Distributions 	}
2598*43a90889SApple OSS Distributions 
2599*43a90889SApple OSS Distributions 	return 0;
2600*43a90889SApple OSS Distributions }
2601*43a90889SApple OSS Distributions 
2602*43a90889SApple OSS Distributions /*
2603*43a90889SApple OSS Distributions  * Free an nd6 llinfo entry.
2604*43a90889SApple OSS Distributions  * Since the function would cause significant changes in the kernel, DO NOT
2605*43a90889SApple OSS Distributions  * make it global, unless you have a strong reason for the change, and are sure
2606*43a90889SApple OSS Distributions  * that the change is safe.
2607*43a90889SApple OSS Distributions  */
2608*43a90889SApple OSS Distributions void
nd6_free(struct rtentry * rt)2609*43a90889SApple OSS Distributions nd6_free(struct rtentry *rt)
2610*43a90889SApple OSS Distributions {
2611*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = NULL;
2612*43a90889SApple OSS Distributions 	struct in6_addr in6 = {};
2613*43a90889SApple OSS Distributions 	struct nd_defrouter *__single dr = NULL;
2614*43a90889SApple OSS Distributions 
2615*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2616*43a90889SApple OSS Distributions 	RT_LOCK_ASSERT_NOTHELD(rt);
2617*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
2618*43a90889SApple OSS Distributions 
2619*43a90889SApple OSS Distributions 	RT_LOCK(rt);
2620*43a90889SApple OSS Distributions 	RT_ADDREF_LOCKED(rt);   /* Extra ref */
2621*43a90889SApple OSS Distributions 	ln = rt->rt_llinfo;
2622*43a90889SApple OSS Distributions 	in6 = SIN6(rt_key(rt))->sin6_addr;
2623*43a90889SApple OSS Distributions 
2624*43a90889SApple OSS Distributions 	/*
2625*43a90889SApple OSS Distributions 	 * Prevent another thread from modifying rt_key, rt_gateway
2626*43a90889SApple OSS Distributions 	 * via rt_setgate() after the rt_lock is dropped by marking
2627*43a90889SApple OSS Distributions 	 * the route as defunct.
2628*43a90889SApple OSS Distributions 	 */
2629*43a90889SApple OSS Distributions 	rt->rt_flags |= RTF_CONDEMNED;
2630*43a90889SApple OSS Distributions 
2631*43a90889SApple OSS Distributions 	/*
2632*43a90889SApple OSS Distributions 	 * We used to have pfctlinput(PRC_HOSTDEAD) here.  Even though it is
2633*43a90889SApple OSS Distributions 	 * not harmful, it was not really necessary.  Perform default router
2634*43a90889SApple OSS Distributions 	 * selection even when we are a router, if Scoped Routing is enabled.
2635*43a90889SApple OSS Distributions 	 */
2636*43a90889SApple OSS Distributions 	/* XXX TDB Handle lists in route information option as well */
2637*43a90889SApple OSS Distributions 	dr = defrouter_lookup(NULL, &SIN6(rt_key(rt))->sin6_addr, rt->rt_ifp);
2638*43a90889SApple OSS Distributions 
2639*43a90889SApple OSS Distributions 	if ((ln && ln->ln_router) || dr) {
2640*43a90889SApple OSS Distributions 		/*
2641*43a90889SApple OSS Distributions 		 * rt6_flush must be called whether or not the neighbor
2642*43a90889SApple OSS Distributions 		 * is in the Default Router List.
2643*43a90889SApple OSS Distributions 		 * See a corresponding comment in nd6_na_input().
2644*43a90889SApple OSS Distributions 		 */
2645*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
2646*43a90889SApple OSS Distributions 		lck_mtx_unlock(nd6_mutex);
2647*43a90889SApple OSS Distributions 		rt6_flush(&in6, rt->rt_ifp);
2648*43a90889SApple OSS Distributions 		lck_mtx_lock(nd6_mutex);
2649*43a90889SApple OSS Distributions 	} else {
2650*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
2651*43a90889SApple OSS Distributions 	}
2652*43a90889SApple OSS Distributions 
2653*43a90889SApple OSS Distributions 	if (dr) {
2654*43a90889SApple OSS Distributions 		NDDR_REMREF(dr);
2655*43a90889SApple OSS Distributions 		/*
2656*43a90889SApple OSS Distributions 		 * Unreachablity of a router might affect the default
2657*43a90889SApple OSS Distributions 		 * router selection and on-link detection of advertised
2658*43a90889SApple OSS Distributions 		 * prefixes.
2659*43a90889SApple OSS Distributions 		 */
2660*43a90889SApple OSS Distributions 
2661*43a90889SApple OSS Distributions 		/*
2662*43a90889SApple OSS Distributions 		 * Temporarily fake the state to choose a new default
2663*43a90889SApple OSS Distributions 		 * router and to perform on-link determination of
2664*43a90889SApple OSS Distributions 		 * prefixes correctly.
2665*43a90889SApple OSS Distributions 		 * Below the state will be set correctly,
2666*43a90889SApple OSS Distributions 		 * or the entry itself will be deleted.
2667*43a90889SApple OSS Distributions 		 */
2668*43a90889SApple OSS Distributions 		RT_LOCK_SPIN(rt);
2669*43a90889SApple OSS Distributions 		ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_INCOMPLETE);
2670*43a90889SApple OSS Distributions 
2671*43a90889SApple OSS Distributions 		/*
2672*43a90889SApple OSS Distributions 		 * Since defrouter_select() does not affect the
2673*43a90889SApple OSS Distributions 		 * on-link determination and MIP6 needs the check
2674*43a90889SApple OSS Distributions 		 * before the default router selection, we perform
2675*43a90889SApple OSS Distributions 		 * the check now.
2676*43a90889SApple OSS Distributions 		 */
2677*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
2678*43a90889SApple OSS Distributions 		pfxlist_onlink_check();
2679*43a90889SApple OSS Distributions 
2680*43a90889SApple OSS Distributions 		/*
2681*43a90889SApple OSS Distributions 		 * refresh default router list
2682*43a90889SApple OSS Distributions 		 */
2683*43a90889SApple OSS Distributions 		defrouter_select(rt->rt_ifp, NULL);
2684*43a90889SApple OSS Distributions 
2685*43a90889SApple OSS Distributions 		/* Loop through all RTI's as well and trigger router selection. */
2686*43a90889SApple OSS Distributions 		nd6_router_select_rti_entries(rt->rt_ifp);
2687*43a90889SApple OSS Distributions 	}
2688*43a90889SApple OSS Distributions 	RT_LOCK_ASSERT_NOTHELD(rt);
2689*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
2690*43a90889SApple OSS Distributions 	/*
2691*43a90889SApple OSS Distributions 	 * Detach the route from the routing tree and the list of neighbor
2692*43a90889SApple OSS Distributions 	 * caches, and disable the route entry not to be used in already
2693*43a90889SApple OSS Distributions 	 * cached routes.
2694*43a90889SApple OSS Distributions 	 */
2695*43a90889SApple OSS Distributions 	(void) rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
2696*43a90889SApple OSS Distributions 
2697*43a90889SApple OSS Distributions 	/* Extra ref held above; now free it */
2698*43a90889SApple OSS Distributions 	rtfree(rt);
2699*43a90889SApple OSS Distributions }
2700*43a90889SApple OSS Distributions 
2701*43a90889SApple OSS Distributions void
nd6_rtrequest(int req,struct rtentry * rt,struct sockaddr * sa)2702*43a90889SApple OSS Distributions nd6_rtrequest(int req, struct rtentry *rt, struct sockaddr *sa)
2703*43a90889SApple OSS Distributions {
2704*43a90889SApple OSS Distributions #pragma unused(sa)
2705*43a90889SApple OSS Distributions 	struct sockaddr *gate = rt->rt_gateway;
2706*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = rt->rt_llinfo;
2707*43a90889SApple OSS Distributions 	static struct sockaddr_dl null_sdl =
2708*43a90889SApple OSS Distributions 	{ .sdl_len = sizeof(null_sdl), .sdl_family = AF_LINK };
2709*43a90889SApple OSS Distributions 	ifnet_ref_t ifp = rt->rt_ifp;
2710*43a90889SApple OSS Distributions 	struct ifaddr *__single ifa;
2711*43a90889SApple OSS Distributions 	uint64_t timenow;
2712*43a90889SApple OSS Distributions 	char buf[MAX_IPv6_STR_LEN];
2713*43a90889SApple OSS Distributions 	boolean_t nud_enabled = FALSE;
2714*43a90889SApple OSS Distributions 
2715*43a90889SApple OSS Distributions 	/*
2716*43a90889SApple OSS Distributions 	 * The IPv6 initialization of the loopback interface
2717*43a90889SApple OSS Distributions 	 * may happen after another interface gets assigned
2718*43a90889SApple OSS Distributions 	 * an IPv6 address.
2719*43a90889SApple OSS Distributions 	 * To avoid asserting treat local routes as special
2720*43a90889SApple OSS Distributions 	 * case.
2721*43a90889SApple OSS Distributions 	 */
2722*43a90889SApple OSS Distributions 	if (rt->rt_ifp != lo_ifp) {
2723*43a90889SApple OSS Distributions 		struct nd_ifinfo *__single ndi = ND_IFINFO(rt->rt_ifp);
2724*43a90889SApple OSS Distributions 		VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
2725*43a90889SApple OSS Distributions 		nud_enabled = !!(ndi->flags & ND6_IFF_PERFORMNUD);
2726*43a90889SApple OSS Distributions 	}
2727*43a90889SApple OSS Distributions 
2728*43a90889SApple OSS Distributions 	VERIFY(nd6_init_done);
2729*43a90889SApple OSS Distributions 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
2730*43a90889SApple OSS Distributions 	RT_LOCK_ASSERT_HELD(rt);
2731*43a90889SApple OSS Distributions 
2732*43a90889SApple OSS Distributions 	/*
2733*43a90889SApple OSS Distributions 	 * We have rnh_lock held, see if we need to schedule the timer;
2734*43a90889SApple OSS Distributions 	 * we might do this again below during RTM_RESOLVE, but doing it
2735*43a90889SApple OSS Distributions 	 * now handles all other cases.
2736*43a90889SApple OSS Distributions 	 */
2737*43a90889SApple OSS Distributions 	if (nd6_sched_timeout_want) {
2738*43a90889SApple OSS Distributions 		nd6_sched_timeout(NULL, NULL);
2739*43a90889SApple OSS Distributions 	}
2740*43a90889SApple OSS Distributions 
2741*43a90889SApple OSS Distributions 	if (rt->rt_flags & RTF_GATEWAY) {
2742*43a90889SApple OSS Distributions 		return;
2743*43a90889SApple OSS Distributions 	}
2744*43a90889SApple OSS Distributions 
2745*43a90889SApple OSS Distributions 	if (!nd6_need_cache(ifp) && !(rt->rt_flags & RTF_HOST)) {
2746*43a90889SApple OSS Distributions 		/*
2747*43a90889SApple OSS Distributions 		 * This is probably an interface direct route for a link
2748*43a90889SApple OSS Distributions 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
2749*43a90889SApple OSS Distributions 		 * We do not need special treatment below for such a route.
2750*43a90889SApple OSS Distributions 		 * Moreover, the RTF_LLINFO flag which would be set below
2751*43a90889SApple OSS Distributions 		 * would annoy the ndp(8) command.
2752*43a90889SApple OSS Distributions 		 */
2753*43a90889SApple OSS Distributions 		return;
2754*43a90889SApple OSS Distributions 	}
2755*43a90889SApple OSS Distributions 
2756*43a90889SApple OSS Distributions 	if (req == RTM_RESOLVE) {
2757*43a90889SApple OSS Distributions 		int no_nd_cache;
2758*43a90889SApple OSS Distributions 
2759*43a90889SApple OSS Distributions 		if (!nd6_need_cache(ifp)) {     /* stf case */
2760*43a90889SApple OSS Distributions 			no_nd_cache = 1;
2761*43a90889SApple OSS Distributions 		} else {
2762*43a90889SApple OSS Distributions 			struct sockaddr_in6 sin6;
2763*43a90889SApple OSS Distributions 
2764*43a90889SApple OSS Distributions 			rtkey_to_sa6(rt, &sin6);
2765*43a90889SApple OSS Distributions 			/*
2766*43a90889SApple OSS Distributions 			 * nd6_is_addr_neighbor() may call nd6_lookup(),
2767*43a90889SApple OSS Distributions 			 * therefore we drop rt_lock to avoid deadlock
2768*43a90889SApple OSS Distributions 			 * during the lookup.
2769*43a90889SApple OSS Distributions 			 */
2770*43a90889SApple OSS Distributions 			RT_ADDREF_LOCKED(rt);
2771*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
2772*43a90889SApple OSS Distributions 			no_nd_cache = !nd6_is_addr_neighbor(&sin6, ifp, 1);
2773*43a90889SApple OSS Distributions 			RT_LOCK(rt);
2774*43a90889SApple OSS Distributions 			RT_REMREF_LOCKED(rt);
2775*43a90889SApple OSS Distributions 		}
2776*43a90889SApple OSS Distributions 
2777*43a90889SApple OSS Distributions 		/*
2778*43a90889SApple OSS Distributions 		 * FreeBSD and BSD/OS often make a cloned host route based
2779*43a90889SApple OSS Distributions 		 * on a less-specific route (e.g. the default route).
2780*43a90889SApple OSS Distributions 		 * If the less specific route does not have a "gateway"
2781*43a90889SApple OSS Distributions 		 * (this is the case when the route just goes to a p2p or an
2782*43a90889SApple OSS Distributions 		 * stf interface), we'll mistakenly make a neighbor cache for
2783*43a90889SApple OSS Distributions 		 * the host route, and will see strange neighbor solicitation
2784*43a90889SApple OSS Distributions 		 * for the corresponding destination.  In order to avoid the
2785*43a90889SApple OSS Distributions 		 * confusion, we check if the destination of the route is
2786*43a90889SApple OSS Distributions 		 * a neighbor in terms of neighbor discovery, and stop the
2787*43a90889SApple OSS Distributions 		 * process if not.  Additionally, we remove the LLINFO flag
2788*43a90889SApple OSS Distributions 		 * so that ndp(8) will not try to get the neighbor information
2789*43a90889SApple OSS Distributions 		 * of the destination.
2790*43a90889SApple OSS Distributions 		 */
2791*43a90889SApple OSS Distributions 		if (no_nd_cache) {
2792*43a90889SApple OSS Distributions 			rt->rt_flags &= ~RTF_LLINFO;
2793*43a90889SApple OSS Distributions 			return;
2794*43a90889SApple OSS Distributions 		}
2795*43a90889SApple OSS Distributions 	}
2796*43a90889SApple OSS Distributions 
2797*43a90889SApple OSS Distributions 	timenow = net_uptime();
2798*43a90889SApple OSS Distributions 
2799*43a90889SApple OSS Distributions 	switch (req) {
2800*43a90889SApple OSS Distributions 	case RTM_ADD:
2801*43a90889SApple OSS Distributions 		/*
2802*43a90889SApple OSS Distributions 		 * There is no backward compatibility :)
2803*43a90889SApple OSS Distributions 		 *
2804*43a90889SApple OSS Distributions 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
2805*43a90889SApple OSS Distributions 		 *      SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
2806*43a90889SApple OSS Distributions 		 *              rt->rt_flags |= RTF_CLONING;
2807*43a90889SApple OSS Distributions 		 */
2808*43a90889SApple OSS Distributions 		if ((rt->rt_flags & RTF_CLONING) ||
2809*43a90889SApple OSS Distributions 		    ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
2810*43a90889SApple OSS Distributions 			/*
2811*43a90889SApple OSS Distributions 			 * Case 1: This route should come from a route to
2812*43a90889SApple OSS Distributions 			 * interface (RTF_CLONING case) or the route should be
2813*43a90889SApple OSS Distributions 			 * treated as on-link but is currently not
2814*43a90889SApple OSS Distributions 			 * (RTF_LLINFO && ln == NULL case).
2815*43a90889SApple OSS Distributions 			 */
2816*43a90889SApple OSS Distributions 			if (rt_setgate(rt, rt_key(rt), SA(&null_sdl)) == 0) {
2817*43a90889SApple OSS Distributions 				gate = rt->rt_gateway;
2818*43a90889SApple OSS Distributions 				SDL(gate)->sdl_type = ifp->if_type;
2819*43a90889SApple OSS Distributions 				SDL(gate)->sdl_index = ifp->if_index;
2820*43a90889SApple OSS Distributions 				/*
2821*43a90889SApple OSS Distributions 				 * In case we're called before 1.0 sec.
2822*43a90889SApple OSS Distributions 				 * has elapsed.
2823*43a90889SApple OSS Distributions 				 */
2824*43a90889SApple OSS Distributions 				if (ln != NULL) {
2825*43a90889SApple OSS Distributions 					ln_setexpire(ln,
2826*43a90889SApple OSS Distributions 					    (ifp->if_eflags & IFEF_IPV6_ND6ALT)
2827*43a90889SApple OSS Distributions 					    ? 0 : MAX(timenow, 1));
2828*43a90889SApple OSS Distributions 				}
2829*43a90889SApple OSS Distributions 			}
2830*43a90889SApple OSS Distributions 			if (rt->rt_flags & RTF_CLONING) {
2831*43a90889SApple OSS Distributions 				break;
2832*43a90889SApple OSS Distributions 			}
2833*43a90889SApple OSS Distributions 		}
2834*43a90889SApple OSS Distributions 		/*
2835*43a90889SApple OSS Distributions 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
2836*43a90889SApple OSS Distributions 		 * We don't do that here since llinfo is not ready yet.
2837*43a90889SApple OSS Distributions 		 *
2838*43a90889SApple OSS Distributions 		 * There are also couple of other things to be discussed:
2839*43a90889SApple OSS Distributions 		 * - unsolicited NA code needs improvement beforehand
2840*43a90889SApple OSS Distributions 		 * - RFC4861 says we MAY send multicast unsolicited NA
2841*43a90889SApple OSS Distributions 		 *   (7.2.6 paragraph 4), however, it also says that we
2842*43a90889SApple OSS Distributions 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
2843*43a90889SApple OSS Distributions 		 *   we don't have anything like it right now.
2844*43a90889SApple OSS Distributions 		 *   note that the mechanism needs a mutual agreement
2845*43a90889SApple OSS Distributions 		 *   between proxies, which means that we need to implement
2846*43a90889SApple OSS Distributions 		 *   a new protocol, or a new kludge.
2847*43a90889SApple OSS Distributions 		 * - from RFC4861 6.2.4, host MUST NOT send an unsolicited RA.
2848*43a90889SApple OSS Distributions 		 *   we need to check ip6forwarding before sending it.
2849*43a90889SApple OSS Distributions 		 *   (or should we allow proxy ND configuration only for
2850*43a90889SApple OSS Distributions 		 *   routers?  there's no mention about proxy ND from hosts)
2851*43a90889SApple OSS Distributions 		 */
2852*43a90889SApple OSS Distributions 		OS_FALLTHROUGH;
2853*43a90889SApple OSS Distributions 	case RTM_RESOLVE:
2854*43a90889SApple OSS Distributions 		if (!(ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))) {
2855*43a90889SApple OSS Distributions 			/*
2856*43a90889SApple OSS Distributions 			 * Address resolution isn't necessary for a point to
2857*43a90889SApple OSS Distributions 			 * point link, so we can skip this test for a p2p link.
2858*43a90889SApple OSS Distributions 			 */
2859*43a90889SApple OSS Distributions 			if (gate->sa_family != AF_LINK ||
2860*43a90889SApple OSS Distributions 			    gate->sa_len < sizeof(null_sdl)) {
2861*43a90889SApple OSS Distributions 				/* Don't complain in case of RTM_ADD */
2862*43a90889SApple OSS Distributions 				if (req == RTM_RESOLVE) {
2863*43a90889SApple OSS Distributions 					log(LOG_ERR, "%s: route to %s has bad "
2864*43a90889SApple OSS Distributions 					    "gateway address (sa_family %u "
2865*43a90889SApple OSS Distributions 					    "sa_len %u) on %s\n", __func__,
2866*43a90889SApple OSS Distributions 					    inet_ntop(AF_INET6,
2867*43a90889SApple OSS Distributions 					    &SIN6(rt_key(rt))->sin6_addr, buf,
2868*43a90889SApple OSS Distributions 					    sizeof(buf)), gate->sa_family,
2869*43a90889SApple OSS Distributions 					    gate->sa_len, if_name(ifp));
2870*43a90889SApple OSS Distributions 				}
2871*43a90889SApple OSS Distributions 				break;
2872*43a90889SApple OSS Distributions 			}
2873*43a90889SApple OSS Distributions 			SDL(gate)->sdl_type = ifp->if_type;
2874*43a90889SApple OSS Distributions 			SDL(gate)->sdl_index = ifp->if_index;
2875*43a90889SApple OSS Distributions 		}
2876*43a90889SApple OSS Distributions 		if (ln != NULL) {
2877*43a90889SApple OSS Distributions 			break;  /* This happens on a route change */
2878*43a90889SApple OSS Distributions 		}
2879*43a90889SApple OSS Distributions 		/*
2880*43a90889SApple OSS Distributions 		 * Case 2: This route may come from cloning, or a manual route
2881*43a90889SApple OSS Distributions 		 * add with a LL address.
2882*43a90889SApple OSS Distributions 		 */
2883*43a90889SApple OSS Distributions 		rt->rt_llinfo = ln = nd6_llinfo_alloc(Z_WAITOK);
2884*43a90889SApple OSS Distributions 
2885*43a90889SApple OSS Distributions 		nd6_allocated++;
2886*43a90889SApple OSS Distributions 		rt->rt_llinfo_get_ri    = nd6_llinfo_get_ri;
2887*43a90889SApple OSS Distributions 		rt->rt_llinfo_get_iflri = nd6_llinfo_get_iflri;
2888*43a90889SApple OSS Distributions 		rt->rt_llinfo_purge     = nd6_llinfo_purge;
2889*43a90889SApple OSS Distributions 		rt->rt_llinfo_free      = nd6_llinfo_free;
2890*43a90889SApple OSS Distributions 		rt->rt_llinfo_refresh   = nd6_llinfo_refresh;
2891*43a90889SApple OSS Distributions 		rt->rt_flags |= RTF_LLINFO;
2892*43a90889SApple OSS Distributions 		ln->ln_rt = rt;
2893*43a90889SApple OSS Distributions 		/* this is required for "ndp" command. - shin */
2894*43a90889SApple OSS Distributions 		/*
2895*43a90889SApple OSS Distributions 		 * For interface's that do not perform NUD
2896*43a90889SApple OSS Distributions 		 * neighbor cache entries must always be marked
2897*43a90889SApple OSS Distributions 		 * reachable with no expiry
2898*43a90889SApple OSS Distributions 		 */
2899*43a90889SApple OSS Distributions 		if ((req == RTM_ADD) || !nud_enabled) {
2900*43a90889SApple OSS Distributions 			/*
2901*43a90889SApple OSS Distributions 			 * gate should have some valid AF_LINK entry,
2902*43a90889SApple OSS Distributions 			 * and ln->ln_expire should have some lifetime
2903*43a90889SApple OSS Distributions 			 * which is specified by ndp command.
2904*43a90889SApple OSS Distributions 			 */
2905*43a90889SApple OSS Distributions 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2906*43a90889SApple OSS Distributions 			ln_setexpire(ln, 0);
2907*43a90889SApple OSS Distributions 		} else {
2908*43a90889SApple OSS Distributions 			/*
2909*43a90889SApple OSS Distributions 			 * When req == RTM_RESOLVE, rt is created and
2910*43a90889SApple OSS Distributions 			 * initialized in rtrequest(), so rt_expire is 0.
2911*43a90889SApple OSS Distributions 			 */
2912*43a90889SApple OSS Distributions 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_NOSTATE);
2913*43a90889SApple OSS Distributions 			/* In case we're called before 1.0 sec. has elapsed */
2914*43a90889SApple OSS Distributions 			ln_setexpire(ln, (ifp->if_eflags & IFEF_IPV6_ND6ALT) ?
2915*43a90889SApple OSS Distributions 			    0 : MAX(timenow, 1));
2916*43a90889SApple OSS Distributions 		}
2917*43a90889SApple OSS Distributions 		LN_INSERTHEAD(ln);
2918*43a90889SApple OSS Distributions 		nd6_inuse++;
2919*43a90889SApple OSS Distributions 
2920*43a90889SApple OSS Distributions 		/* We have at least one entry; arm the timer if not already */
2921*43a90889SApple OSS Distributions 		nd6_sched_timeout(NULL, NULL);
2922*43a90889SApple OSS Distributions 
2923*43a90889SApple OSS Distributions 		/*
2924*43a90889SApple OSS Distributions 		 * If we have too many cache entries, initiate immediate
2925*43a90889SApple OSS Distributions 		 * purging for some "less recently used" entries.  Note that
2926*43a90889SApple OSS Distributions 		 * we cannot directly call nd6_free() here because it would
2927*43a90889SApple OSS Distributions 		 * cause re-entering rtable related routines triggering an LOR
2928*43a90889SApple OSS Distributions 		 * problem.
2929*43a90889SApple OSS Distributions 		 */
2930*43a90889SApple OSS Distributions 		if (ip6_neighborgcthresh > 0 &&
2931*43a90889SApple OSS Distributions 		    nd6_inuse >= ip6_neighborgcthresh) {
2932*43a90889SApple OSS Distributions 			int i;
2933*43a90889SApple OSS Distributions 
2934*43a90889SApple OSS Distributions 			for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
2935*43a90889SApple OSS Distributions 				struct llinfo_nd6 *__single ln_end = llinfo_nd6.ln_prev;
2936*43a90889SApple OSS Distributions 				rtentry_ref_t rt_end = ln_end->ln_rt;
2937*43a90889SApple OSS Distributions 
2938*43a90889SApple OSS Distributions 				/* Move this entry to the head */
2939*43a90889SApple OSS Distributions 				RT_LOCK(rt_end);
2940*43a90889SApple OSS Distributions 				LN_DEQUEUE(ln_end);
2941*43a90889SApple OSS Distributions 				LN_INSERTHEAD(ln_end);
2942*43a90889SApple OSS Distributions 
2943*43a90889SApple OSS Distributions 				if (ln_end->ln_expire == 0) {
2944*43a90889SApple OSS Distributions 					RT_UNLOCK(rt_end);
2945*43a90889SApple OSS Distributions 					continue;
2946*43a90889SApple OSS Distributions 				}
2947*43a90889SApple OSS Distributions 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE) {
2948*43a90889SApple OSS Distributions 					ND6_CACHE_STATE_TRANSITION(ln_end, ND6_LLINFO_STALE);
2949*43a90889SApple OSS Distributions 				} else {
2950*43a90889SApple OSS Distributions 					ND6_CACHE_STATE_TRANSITION(ln_end, ND6_LLINFO_PURGE);
2951*43a90889SApple OSS Distributions 				}
2952*43a90889SApple OSS Distributions 				ln_setexpire(ln_end, timenow);
2953*43a90889SApple OSS Distributions 				RT_UNLOCK(rt_end);
2954*43a90889SApple OSS Distributions 			}
2955*43a90889SApple OSS Distributions 		}
2956*43a90889SApple OSS Distributions 
2957*43a90889SApple OSS Distributions 		/*
2958*43a90889SApple OSS Distributions 		 * check if rt_key(rt) is one of my address assigned
2959*43a90889SApple OSS Distributions 		 * to the interface.
2960*43a90889SApple OSS Distributions 		 */
2961*43a90889SApple OSS Distributions 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
2962*43a90889SApple OSS Distributions 		    &SIN6(rt_key(rt))->sin6_addr);
2963*43a90889SApple OSS Distributions 		if (ifa != NULL) {
2964*43a90889SApple OSS Distributions 			caddr_t macp = nd6_ifptomac(ifp);
2965*43a90889SApple OSS Distributions 			ln_setexpire(ln, 0);
2966*43a90889SApple OSS Distributions 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2967*43a90889SApple OSS Distributions 			if (macp != NULL) {
2968*43a90889SApple OSS Distributions 				SOCKADDR_COPY(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
2969*43a90889SApple OSS Distributions 				SDL(gate)->sdl_alen = ifp->if_addrlen;
2970*43a90889SApple OSS Distributions 			}
2971*43a90889SApple OSS Distributions 			if (nd6_useloopback) {
2972*43a90889SApple OSS Distributions 				if (rt->rt_ifp != lo_ifp) {
2973*43a90889SApple OSS Distributions 					/*
2974*43a90889SApple OSS Distributions 					 * Purge any link-layer info caching.
2975*43a90889SApple OSS Distributions 					 */
2976*43a90889SApple OSS Distributions 					if (rt->rt_llinfo_purge != NULL) {
2977*43a90889SApple OSS Distributions 						rt->rt_llinfo_purge(rt);
2978*43a90889SApple OSS Distributions 					}
2979*43a90889SApple OSS Distributions 
2980*43a90889SApple OSS Distributions 					/*
2981*43a90889SApple OSS Distributions 					 * Adjust route ref count for the
2982*43a90889SApple OSS Distributions 					 * interfaces.
2983*43a90889SApple OSS Distributions 					 */
2984*43a90889SApple OSS Distributions 					if (rt->rt_if_ref_fn != NULL) {
2985*43a90889SApple OSS Distributions 						rt->rt_if_ref_fn(lo_ifp, 1);
2986*43a90889SApple OSS Distributions 						rt->rt_if_ref_fn(rt->rt_ifp,
2987*43a90889SApple OSS Distributions 						    -1);
2988*43a90889SApple OSS Distributions 					}
2989*43a90889SApple OSS Distributions 				}
2990*43a90889SApple OSS Distributions 				rt->rt_ifp = lo_ifp;
2991*43a90889SApple OSS Distributions 				/*
2992*43a90889SApple OSS Distributions 				 * If rmx_mtu is not locked, update it
2993*43a90889SApple OSS Distributions 				 * to the MTU used by the new interface.
2994*43a90889SApple OSS Distributions 				 */
2995*43a90889SApple OSS Distributions 				if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) {
2996*43a90889SApple OSS Distributions 					rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
2997*43a90889SApple OSS Distributions 				}
2998*43a90889SApple OSS Distributions 				/*
2999*43a90889SApple OSS Distributions 				 * Make sure rt_ifa be equal to the ifaddr
3000*43a90889SApple OSS Distributions 				 * corresponding to the address.
3001*43a90889SApple OSS Distributions 				 * We need this because when we refer
3002*43a90889SApple OSS Distributions 				 * rt_ifa->ia6_flags in ip6_input, we assume
3003*43a90889SApple OSS Distributions 				 * that the rt_ifa points to the address instead
3004*43a90889SApple OSS Distributions 				 * of the loopback address.
3005*43a90889SApple OSS Distributions 				 */
3006*43a90889SApple OSS Distributions 				if (ifa != rt->rt_ifa) {
3007*43a90889SApple OSS Distributions 					rtsetifa(rt, ifa);
3008*43a90889SApple OSS Distributions 				}
3009*43a90889SApple OSS Distributions 			}
3010*43a90889SApple OSS Distributions 			ifa_remref(ifa);
3011*43a90889SApple OSS Distributions 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
3012*43a90889SApple OSS Distributions 			ln_setexpire(ln, 0);
3013*43a90889SApple OSS Distributions 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
3014*43a90889SApple OSS Distributions 
3015*43a90889SApple OSS Distributions 			/* join solicited node multicast for proxy ND */
3016*43a90889SApple OSS Distributions 			if (ifp->if_flags & IFF_MULTICAST) {
3017*43a90889SApple OSS Distributions 				struct in6_addr llsol;
3018*43a90889SApple OSS Distributions 				struct in6_multi *__single in6m;
3019*43a90889SApple OSS Distributions 				int error;
3020*43a90889SApple OSS Distributions 
3021*43a90889SApple OSS Distributions 				llsol = SIN6(rt_key(rt))->sin6_addr;
3022*43a90889SApple OSS Distributions 				llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
3023*43a90889SApple OSS Distributions 				llsol.s6_addr32[1] = 0;
3024*43a90889SApple OSS Distributions 				llsol.s6_addr32[2] = htonl(1);
3025*43a90889SApple OSS Distributions 				llsol.s6_addr8[12] = 0xff;
3026*43a90889SApple OSS Distributions 				if (in6_setscope(&llsol, ifp, NULL)) {
3027*43a90889SApple OSS Distributions 					break;
3028*43a90889SApple OSS Distributions 				}
3029*43a90889SApple OSS Distributions 				error = in6_mc_join(ifp, &llsol,
3030*43a90889SApple OSS Distributions 				    NULL, &in6m, 0);
3031*43a90889SApple OSS Distributions 				if (error) {
3032*43a90889SApple OSS Distributions 					nd6log(error, "%s: failed to join "
3033*43a90889SApple OSS Distributions 					    "%s (errno=%d)\n", if_name(ifp),
3034*43a90889SApple OSS Distributions 					    ip6_sprintf(&llsol), error);
3035*43a90889SApple OSS Distributions 				} else {
3036*43a90889SApple OSS Distributions 					IN6M_REMREF(in6m);
3037*43a90889SApple OSS Distributions 				}
3038*43a90889SApple OSS Distributions 			}
3039*43a90889SApple OSS Distributions 		}
3040*43a90889SApple OSS Distributions 		break;
3041*43a90889SApple OSS Distributions 
3042*43a90889SApple OSS Distributions 	case RTM_DELETE:
3043*43a90889SApple OSS Distributions 		if (ln == NULL) {
3044*43a90889SApple OSS Distributions 			break;
3045*43a90889SApple OSS Distributions 		}
3046*43a90889SApple OSS Distributions 		/* leave from solicited node multicast for proxy ND */
3047*43a90889SApple OSS Distributions 		if ((rt->rt_flags & RTF_ANNOUNCE) &&
3048*43a90889SApple OSS Distributions 		    (ifp->if_flags & IFF_MULTICAST)) {
3049*43a90889SApple OSS Distributions 			struct in6_addr llsol;
3050*43a90889SApple OSS Distributions 			struct in6_multi *__single in6m;
3051*43a90889SApple OSS Distributions 
3052*43a90889SApple OSS Distributions 			llsol = SIN6(rt_key(rt))->sin6_addr;
3053*43a90889SApple OSS Distributions 			llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
3054*43a90889SApple OSS Distributions 			llsol.s6_addr32[1] = 0;
3055*43a90889SApple OSS Distributions 			llsol.s6_addr32[2] = htonl(1);
3056*43a90889SApple OSS Distributions 			llsol.s6_addr8[12] = 0xff;
3057*43a90889SApple OSS Distributions 			if (in6_setscope(&llsol, ifp, NULL) == 0) {
3058*43a90889SApple OSS Distributions 				in6_multihead_lock_shared();
3059*43a90889SApple OSS Distributions 				IN6_LOOKUP_MULTI(&llsol, ifp, in6m);
3060*43a90889SApple OSS Distributions 				in6_multihead_lock_done();
3061*43a90889SApple OSS Distributions 				if (in6m != NULL) {
3062*43a90889SApple OSS Distributions 					in6_mc_leave(in6m, NULL);
3063*43a90889SApple OSS Distributions 					IN6M_REMREF(in6m);
3064*43a90889SApple OSS Distributions 				}
3065*43a90889SApple OSS Distributions 			}
3066*43a90889SApple OSS Distributions 		}
3067*43a90889SApple OSS Distributions 		nd6_inuse--;
3068*43a90889SApple OSS Distributions 		/*
3069*43a90889SApple OSS Distributions 		 * Unchain it but defer the actual freeing until the route
3070*43a90889SApple OSS Distributions 		 * itself is to be freed.  rt->rt_llinfo still points to
3071*43a90889SApple OSS Distributions 		 * llinfo_nd6, and likewise, ln->ln_rt still points to this
3072*43a90889SApple OSS Distributions 		 * route entry, except that RTF_LLINFO is now cleared.
3073*43a90889SApple OSS Distributions 		 */
3074*43a90889SApple OSS Distributions 		if (ln->ln_flags & ND6_LNF_IN_USE) {
3075*43a90889SApple OSS Distributions 			LN_DEQUEUE(ln);
3076*43a90889SApple OSS Distributions 		}
3077*43a90889SApple OSS Distributions 
3078*43a90889SApple OSS Distributions 		/*
3079*43a90889SApple OSS Distributions 		 * Purge any link-layer info caching.
3080*43a90889SApple OSS Distributions 		 */
3081*43a90889SApple OSS Distributions 		if (rt->rt_llinfo_purge != NULL) {
3082*43a90889SApple OSS Distributions 			rt->rt_llinfo_purge(rt);
3083*43a90889SApple OSS Distributions 		}
3084*43a90889SApple OSS Distributions 
3085*43a90889SApple OSS Distributions 		rt->rt_flags &= ~RTF_LLINFO;
3086*43a90889SApple OSS Distributions 		if (ln->ln_hold != NULL) {
3087*43a90889SApple OSS Distributions 			m_freem_list(ln->ln_hold);
3088*43a90889SApple OSS Distributions 			ln->ln_hold = NULL;
3089*43a90889SApple OSS Distributions 		}
3090*43a90889SApple OSS Distributions 	}
3091*43a90889SApple OSS Distributions }
3092*43a90889SApple OSS Distributions 
3093*43a90889SApple OSS Distributions int
nd6_ioctl(u_long cmd,caddr_t __sized_by (IOCPARM_LEN (cmd))data,struct ifnet * ifp)3094*43a90889SApple OSS Distributions nd6_ioctl(u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)) data, struct ifnet *ifp)
3095*43a90889SApple OSS Distributions {
3096*43a90889SApple OSS Distributions 	struct nd_defrouter *__single dr;
3097*43a90889SApple OSS Distributions 	struct nd_prefix *__single pr;
3098*43a90889SApple OSS Distributions 	rtentry_ref_t rt;
3099*43a90889SApple OSS Distributions 
3100*43a90889SApple OSS Distributions 	int error = 0;
3101*43a90889SApple OSS Distributions 
3102*43a90889SApple OSS Distributions 	VERIFY(ifp != NULL);
3103*43a90889SApple OSS Distributions 
3104*43a90889SApple OSS Distributions 	switch (cmd) {
3105*43a90889SApple OSS Distributions 	case OSIOCGIFINFO_IN6:          /* struct in6_ondireq */
3106*43a90889SApple OSS Distributions 	case SIOCGIFINFO_IN6: {         /* struct in6_ondireq */
3107*43a90889SApple OSS Distributions 		u_int32_t linkmtu;
3108*43a90889SApple OSS Distributions 		struct in6_ondireq *__single ondi = (struct in6_ondireq *)(void *)data;
3109*43a90889SApple OSS Distributions 		struct nd_ifinfo *__single ndi;
3110*43a90889SApple OSS Distributions 		/*
3111*43a90889SApple OSS Distributions 		 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
3112*43a90889SApple OSS Distributions 		 * instead of in6_ndireq, so we treat it as such.
3113*43a90889SApple OSS Distributions 		 */
3114*43a90889SApple OSS Distributions 		ndi = ND_IFINFO(ifp);
3115*43a90889SApple OSS Distributions 		if ((NULL == ndi) || (FALSE == ndi->initialized)) {
3116*43a90889SApple OSS Distributions 			error = EINVAL;
3117*43a90889SApple OSS Distributions 			break;
3118*43a90889SApple OSS Distributions 		}
3119*43a90889SApple OSS Distributions 		lck_mtx_lock(&ndi->lock);
3120*43a90889SApple OSS Distributions 		linkmtu = IN6_LINKMTU(ifp);
3121*43a90889SApple OSS Distributions 		bcopy(&linkmtu, &ondi->ndi.linkmtu, sizeof(linkmtu));
3122*43a90889SApple OSS Distributions 		bcopy(&ndi->maxmtu, &ondi->ndi.maxmtu,
3123*43a90889SApple OSS Distributions 		    sizeof(u_int32_t));
3124*43a90889SApple OSS Distributions 		bcopy(&ndi->basereachable, &ondi->ndi.basereachable,
3125*43a90889SApple OSS Distributions 		    sizeof(u_int32_t));
3126*43a90889SApple OSS Distributions 		bcopy(&ndi->reachable, &ondi->ndi.reachable,
3127*43a90889SApple OSS Distributions 		    sizeof(u_int32_t));
3128*43a90889SApple OSS Distributions 		bcopy(&ndi->retrans, &ondi->ndi.retrans,
3129*43a90889SApple OSS Distributions 		    sizeof(u_int32_t));
3130*43a90889SApple OSS Distributions 		bcopy(&ndi->flags, &ondi->ndi.flags,
3131*43a90889SApple OSS Distributions 		    sizeof(u_int32_t));
3132*43a90889SApple OSS Distributions 		bcopy(&ndi->recalctm, &ondi->ndi.recalctm,
3133*43a90889SApple OSS Distributions 		    sizeof(int));
3134*43a90889SApple OSS Distributions 		ondi->ndi.chlim = ndi->chlim;
3135*43a90889SApple OSS Distributions 		/*
3136*43a90889SApple OSS Distributions 		 * The below truncation is fine as we mostly use it for
3137*43a90889SApple OSS Distributions 		 * debugging purpose.
3138*43a90889SApple OSS Distributions 		 */
3139*43a90889SApple OSS Distributions 		ondi->ndi.receivedra = (uint8_t)ndi->ndefrouters;
3140*43a90889SApple OSS Distributions 		ondi->ndi.collision_count = (uint8_t)ndi->cga_collision_count;
3141*43a90889SApple OSS Distributions 		lck_mtx_unlock(&ndi->lock);
3142*43a90889SApple OSS Distributions 		break;
3143*43a90889SApple OSS Distributions 	}
3144*43a90889SApple OSS Distributions 
3145*43a90889SApple OSS Distributions 	case SIOCSIFINFO_FLAGS: {       /* struct in6_ndireq */
3146*43a90889SApple OSS Distributions 		/*
3147*43a90889SApple OSS Distributions 		 * XXX BSD has a bunch of checks here to ensure
3148*43a90889SApple OSS Distributions 		 * that interface disabled flag is not reset if
3149*43a90889SApple OSS Distributions 		 * link local address has failed DAD.
3150*43a90889SApple OSS Distributions 		 * Investigate that part.
3151*43a90889SApple OSS Distributions 		 */
3152*43a90889SApple OSS Distributions 		struct in6_ndireq *__single cndi = (struct in6_ndireq *)(void *)data;
3153*43a90889SApple OSS Distributions 		u_int32_t oflags, flags;
3154*43a90889SApple OSS Distributions 		struct nd_ifinfo *__single ndi = ND_IFINFO(ifp);
3155*43a90889SApple OSS Distributions 
3156*43a90889SApple OSS Distributions 		/* XXX: almost all other fields of cndi->ndi is unused */
3157*43a90889SApple OSS Distributions 		if ((NULL == ndi) || !ndi->initialized) {
3158*43a90889SApple OSS Distributions 			error = EINVAL;
3159*43a90889SApple OSS Distributions 			break;
3160*43a90889SApple OSS Distributions 		}
3161*43a90889SApple OSS Distributions 
3162*43a90889SApple OSS Distributions 		lck_mtx_lock(&ndi->lock);
3163*43a90889SApple OSS Distributions 		oflags = ndi->flags;
3164*43a90889SApple OSS Distributions 		bcopy(&cndi->ndi.flags, &(ndi->flags), sizeof(flags));
3165*43a90889SApple OSS Distributions 		flags = ndi->flags;
3166*43a90889SApple OSS Distributions 		lck_mtx_unlock(&ndi->lock);
3167*43a90889SApple OSS Distributions 
3168*43a90889SApple OSS Distributions 		if (oflags == flags) {
3169*43a90889SApple OSS Distributions 			break;
3170*43a90889SApple OSS Distributions 		}
3171*43a90889SApple OSS Distributions 
3172*43a90889SApple OSS Distributions 		error = nd6_setifinfo(ifp, oflags, flags);
3173*43a90889SApple OSS Distributions 		break;
3174*43a90889SApple OSS Distributions 	}
3175*43a90889SApple OSS Distributions 
3176*43a90889SApple OSS Distributions 	case SIOCSNDFLUSH_IN6:          /* struct in6_ifreq */
3177*43a90889SApple OSS Distributions 		/* flush default router list */
3178*43a90889SApple OSS Distributions 		/*
3179*43a90889SApple OSS Distributions 		 * xxx sumikawa: should not delete route if default
3180*43a90889SApple OSS Distributions 		 * route equals to the top of default router list
3181*43a90889SApple OSS Distributions 		 *
3182*43a90889SApple OSS Distributions 		 * XXX TODO: Needs to be done for RTI as well
3183*43a90889SApple OSS Distributions 		 * Is very specific flush command with ndp for default routers.
3184*43a90889SApple OSS Distributions 		 */
3185*43a90889SApple OSS Distributions 		lck_mtx_lock(nd6_mutex);
3186*43a90889SApple OSS Distributions 		defrouter_reset();
3187*43a90889SApple OSS Distributions 		defrouter_select(ifp, NULL);
3188*43a90889SApple OSS Distributions 		lck_mtx_unlock(nd6_mutex);
3189*43a90889SApple OSS Distributions 		/* xxx sumikawa: flush prefix list */
3190*43a90889SApple OSS Distributions 		break;
3191*43a90889SApple OSS Distributions 
3192*43a90889SApple OSS Distributions 	case SIOCSPFXFLUSH_IN6: {       /* struct in6_ifreq */
3193*43a90889SApple OSS Distributions 		/* flush all the prefix advertised by routers */
3194*43a90889SApple OSS Distributions 		struct nd_prefix *__single next = NULL;
3195*43a90889SApple OSS Distributions 
3196*43a90889SApple OSS Distributions 		lck_mtx_lock(nd6_mutex);
3197*43a90889SApple OSS Distributions 		for (pr = nd_prefix.lh_first; pr; pr = next) {
3198*43a90889SApple OSS Distributions 			struct in6_ifaddr *__single ia = NULL;
3199*43a90889SApple OSS Distributions 			bool iterate_pfxlist_again = false;
3200*43a90889SApple OSS Distributions 
3201*43a90889SApple OSS Distributions 			next = pr->ndpr_next;
3202*43a90889SApple OSS Distributions 
3203*43a90889SApple OSS Distributions 			NDPR_LOCK(pr);
3204*43a90889SApple OSS Distributions 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) {
3205*43a90889SApple OSS Distributions 				NDPR_UNLOCK(pr);
3206*43a90889SApple OSS Distributions 				continue; /* XXX */
3207*43a90889SApple OSS Distributions 			}
3208*43a90889SApple OSS Distributions 			if (ifp != lo_ifp && pr->ndpr_ifp != ifp) {
3209*43a90889SApple OSS Distributions 				NDPR_UNLOCK(pr);
3210*43a90889SApple OSS Distributions 				continue;
3211*43a90889SApple OSS Distributions 			}
3212*43a90889SApple OSS Distributions 			/* do we really have to remove addresses as well? */
3213*43a90889SApple OSS Distributions 			NDPR_ADDREF(pr);
3214*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
3215*43a90889SApple OSS Distributions 			lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
3216*43a90889SApple OSS Distributions 			bool from_begining = true;
3217*43a90889SApple OSS Distributions 			while (from_begining) {
3218*43a90889SApple OSS Distributions 				from_begining = false;
3219*43a90889SApple OSS Distributions 				TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
3220*43a90889SApple OSS Distributions 					IFA_LOCK(&ia->ia_ifa);
3221*43a90889SApple OSS Distributions 					if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) {
3222*43a90889SApple OSS Distributions 						IFA_UNLOCK(&ia->ia_ifa);
3223*43a90889SApple OSS Distributions 						continue;
3224*43a90889SApple OSS Distributions 					}
3225*43a90889SApple OSS Distributions 
3226*43a90889SApple OSS Distributions 					if (ia->ia6_ndpr == pr) {
3227*43a90889SApple OSS Distributions 						ifa_addref(&ia->ia_ifa);
3228*43a90889SApple OSS Distributions 						IFA_UNLOCK(&ia->ia_ifa);
3229*43a90889SApple OSS Distributions 						lck_rw_done(&in6_ifaddr_rwlock);
3230*43a90889SApple OSS Distributions 						lck_mtx_unlock(nd6_mutex);
3231*43a90889SApple OSS Distributions 						in6_purgeaddr(&ia->ia_ifa);
3232*43a90889SApple OSS Distributions 						ifa_remref(&ia->ia_ifa);
3233*43a90889SApple OSS Distributions 						lck_mtx_lock(nd6_mutex);
3234*43a90889SApple OSS Distributions 						lck_rw_lock_exclusive(
3235*43a90889SApple OSS Distributions 							&in6_ifaddr_rwlock);
3236*43a90889SApple OSS Distributions 						/*
3237*43a90889SApple OSS Distributions 						 * Purging the address caused
3238*43a90889SApple OSS Distributions 						 * in6_ifaddr_rwlock to be
3239*43a90889SApple OSS Distributions 						 * dropped and
3240*43a90889SApple OSS Distributions 						 * reacquired; therefore search again
3241*43a90889SApple OSS Distributions 						 * from the beginning of in6_ifaddrs.
3242*43a90889SApple OSS Distributions 						 * The same applies for the prefix list.
3243*43a90889SApple OSS Distributions 						 */
3244*43a90889SApple OSS Distributions 						iterate_pfxlist_again = true;
3245*43a90889SApple OSS Distributions 						from_begining = true;
3246*43a90889SApple OSS Distributions 						break;
3247*43a90889SApple OSS Distributions 					}
3248*43a90889SApple OSS Distributions 					IFA_UNLOCK(&ia->ia_ifa);
3249*43a90889SApple OSS Distributions 				}
3250*43a90889SApple OSS Distributions 			}
3251*43a90889SApple OSS Distributions 			lck_rw_done(&in6_ifaddr_rwlock);
3252*43a90889SApple OSS Distributions 			NDPR_LOCK(pr);
3253*43a90889SApple OSS Distributions 			prelist_remove(pr);
3254*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
3255*43a90889SApple OSS Distributions 			pfxlist_onlink_check();
3256*43a90889SApple OSS Distributions 			NDPR_REMREF(pr);
3257*43a90889SApple OSS Distributions 			if (iterate_pfxlist_again) {
3258*43a90889SApple OSS Distributions 				next = nd_prefix.lh_first;
3259*43a90889SApple OSS Distributions 			}
3260*43a90889SApple OSS Distributions 		}
3261*43a90889SApple OSS Distributions 		lck_mtx_unlock(nd6_mutex);
3262*43a90889SApple OSS Distributions 		break;
3263*43a90889SApple OSS Distributions 	}
3264*43a90889SApple OSS Distributions 
3265*43a90889SApple OSS Distributions 	case SIOCSRTRFLUSH_IN6: {       /* struct in6_ifreq */
3266*43a90889SApple OSS Distributions 		/* flush all the default routers */
3267*43a90889SApple OSS Distributions 		struct nd_defrouter *__single next;
3268*43a90889SApple OSS Distributions 		struct nd_drhead nd_defrouter_tmp;
3269*43a90889SApple OSS Distributions 
3270*43a90889SApple OSS Distributions 		TAILQ_INIT(&nd_defrouter_tmp);
3271*43a90889SApple OSS Distributions 		lck_mtx_lock(nd6_mutex);
3272*43a90889SApple OSS Distributions 		if ((dr = TAILQ_FIRST(&nd_defrouter_list)) != NULL) {
3273*43a90889SApple OSS Distributions 			/*
3274*43a90889SApple OSS Distributions 			 * The first entry of the list may be stored in
3275*43a90889SApple OSS Distributions 			 * the routing table, so we'll delete it later.
3276*43a90889SApple OSS Distributions 			 */
3277*43a90889SApple OSS Distributions 			for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
3278*43a90889SApple OSS Distributions 				next = TAILQ_NEXT(dr, dr_entry);
3279*43a90889SApple OSS Distributions 				if (ifp == lo_ifp || dr->ifp == ifp) {
3280*43a90889SApple OSS Distributions 					/*
3281*43a90889SApple OSS Distributions 					 * Remove the entry from default router list
3282*43a90889SApple OSS Distributions 					 * and add it to the temp list.
3283*43a90889SApple OSS Distributions 					 * nd_defrouter_tmp will be a local temporary
3284*43a90889SApple OSS Distributions 					 * list as no one else can get the same
3285*43a90889SApple OSS Distributions 					 * removed entry once it is removed from default
3286*43a90889SApple OSS Distributions 					 * router list.
3287*43a90889SApple OSS Distributions 					 * Remove the reference after calling defrtrlist_de
3288*43a90889SApple OSS Distributions 					 */
3289*43a90889SApple OSS Distributions 					TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
3290*43a90889SApple OSS Distributions 					TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
3291*43a90889SApple OSS Distributions 				}
3292*43a90889SApple OSS Distributions 			}
3293*43a90889SApple OSS Distributions 
3294*43a90889SApple OSS Distributions 			dr = TAILQ_FIRST(&nd_defrouter_list);
3295*43a90889SApple OSS Distributions 			if (ifp == lo_ifp ||
3296*43a90889SApple OSS Distributions 			    dr->ifp == ifp) {
3297*43a90889SApple OSS Distributions 				TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
3298*43a90889SApple OSS Distributions 				TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
3299*43a90889SApple OSS Distributions 			}
3300*43a90889SApple OSS Distributions 		}
3301*43a90889SApple OSS Distributions 
3302*43a90889SApple OSS Distributions 		/*
3303*43a90889SApple OSS Distributions 		 * Keep the following separate from the above iteration of
3304*43a90889SApple OSS Distributions 		 * nd_defrouter because it's not safe to call
3305*43a90889SApple OSS Distributions 		 * defrtrlist_del while iterating global default
3306*43a90889SApple OSS Distributions 		 * router list. Global list has to be traversed
3307*43a90889SApple OSS Distributions 		 * while holding nd6_mutex throughout.
3308*43a90889SApple OSS Distributions 		 *
3309*43a90889SApple OSS Distributions 		 * The following call to defrtrlist_del should be
3310*43a90889SApple OSS Distributions 		 * safe as we are iterating a local list of
3311*43a90889SApple OSS Distributions 		 * default routers.
3312*43a90889SApple OSS Distributions 		 */
3313*43a90889SApple OSS Distributions 		TAILQ_FOREACH_SAFE(dr, &nd_defrouter_tmp, dr_entry, next) {
3314*43a90889SApple OSS Distributions 			TAILQ_REMOVE(&nd_defrouter_tmp, dr, dr_entry);
3315*43a90889SApple OSS Distributions 			defrtrlist_del(dr, NULL);
3316*43a90889SApple OSS Distributions 			NDDR_REMREF(dr);        /* remove list reference */
3317*43a90889SApple OSS Distributions 		}
3318*43a90889SApple OSS Distributions 
3319*43a90889SApple OSS Distributions 		/* For now flush RTI routes here as well to avoid any regressions */
3320*43a90889SApple OSS Distributions 		nd6_purge_interface_rti_entries((ifp == lo_ifp) ? NULL : ifp);
3321*43a90889SApple OSS Distributions 
3322*43a90889SApple OSS Distributions 		lck_mtx_unlock(nd6_mutex);
3323*43a90889SApple OSS Distributions 		break;
3324*43a90889SApple OSS Distributions 	}
3325*43a90889SApple OSS Distributions 
3326*43a90889SApple OSS Distributions 	case SIOCGNBRINFO_IN6_32: {     /* struct in6_nbrinfo_32 */
3327*43a90889SApple OSS Distributions 		struct llinfo_nd6 *__single ln;
3328*43a90889SApple OSS Distributions 		struct in6_nbrinfo_32 nbi_32;
3329*43a90889SApple OSS Distributions 		struct in6_addr nb_addr; /* make local for safety */
3330*43a90889SApple OSS Distributions 
3331*43a90889SApple OSS Distributions 		bcopy(data, &nbi_32, sizeof(nbi_32));
3332*43a90889SApple OSS Distributions 		nb_addr = nbi_32.addr;
3333*43a90889SApple OSS Distributions 		/*
3334*43a90889SApple OSS Distributions 		 * XXX: KAME specific hack for scoped addresses
3335*43a90889SApple OSS Distributions 		 *      XXXX: for other scopes than link-local?
3336*43a90889SApple OSS Distributions 		 */
3337*43a90889SApple OSS Distributions 		if (in6_embedded_scope && (IN6_IS_ADDR_LINKLOCAL(&nbi_32.addr) ||
3338*43a90889SApple OSS Distributions 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32.addr))) {
3339*43a90889SApple OSS Distributions 			u_int16_t *idp =
3340*43a90889SApple OSS Distributions 			    (u_int16_t *)(void *)&nb_addr.s6_addr[2];
3341*43a90889SApple OSS Distributions 
3342*43a90889SApple OSS Distributions 			if (*idp == 0) {
3343*43a90889SApple OSS Distributions 				*idp = htons(ifp->if_index);
3344*43a90889SApple OSS Distributions 			}
3345*43a90889SApple OSS Distributions 		}
3346*43a90889SApple OSS Distributions 
3347*43a90889SApple OSS Distributions 		/* Callee returns a locked route upon success */
3348*43a90889SApple OSS Distributions 		if ((rt = nd6_lookup(&nb_addr, 0, ifp, 0)) == NULL) {
3349*43a90889SApple OSS Distributions 			error = EINVAL;
3350*43a90889SApple OSS Distributions 			break;
3351*43a90889SApple OSS Distributions 		}
3352*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_HELD(rt);
3353*43a90889SApple OSS Distributions 		ln = rt->rt_llinfo;
3354*43a90889SApple OSS Distributions 		nbi_32.state = ln->ln_state;
3355*43a90889SApple OSS Distributions 		nbi_32.asked = ln->ln_asked;
3356*43a90889SApple OSS Distributions 		nbi_32.isrouter = ln->ln_router;
3357*43a90889SApple OSS Distributions 		nbi_32.expire = (int)ln_getexpire(ln);
3358*43a90889SApple OSS Distributions 		RT_REMREF_LOCKED(rt);
3359*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
3360*43a90889SApple OSS Distributions 		bcopy(&nbi_32, data, sizeof(nbi_32));
3361*43a90889SApple OSS Distributions 		break;
3362*43a90889SApple OSS Distributions 	}
3363*43a90889SApple OSS Distributions 
3364*43a90889SApple OSS Distributions 	case SIOCGNBRINFO_IN6_64: {     /* struct in6_nbrinfo_64 */
3365*43a90889SApple OSS Distributions 		struct llinfo_nd6 *__single ln;
3366*43a90889SApple OSS Distributions 		struct in6_nbrinfo_64 nbi_64;
3367*43a90889SApple OSS Distributions 		struct in6_addr nb_addr; /* make local for safety */
3368*43a90889SApple OSS Distributions 
3369*43a90889SApple OSS Distributions 		bcopy(data, &nbi_64, sizeof(nbi_64));
3370*43a90889SApple OSS Distributions 		nb_addr = nbi_64.addr;
3371*43a90889SApple OSS Distributions 		/*
3372*43a90889SApple OSS Distributions 		 * XXX: KAME specific hack for scoped addresses
3373*43a90889SApple OSS Distributions 		 *      XXXX: for other scopes than link-local?
3374*43a90889SApple OSS Distributions 		 */
3375*43a90889SApple OSS Distributions 		if (in6_embedded_scope && (IN6_IS_ADDR_LINKLOCAL(&nbi_64.addr) ||
3376*43a90889SApple OSS Distributions 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64.addr))) {
3377*43a90889SApple OSS Distributions 			u_int16_t *idp =
3378*43a90889SApple OSS Distributions 			    (u_int16_t *)(void *)&nb_addr.s6_addr[2];
3379*43a90889SApple OSS Distributions 
3380*43a90889SApple OSS Distributions 			if (*idp == 0) {
3381*43a90889SApple OSS Distributions 				*idp = htons(ifp->if_index);
3382*43a90889SApple OSS Distributions 			}
3383*43a90889SApple OSS Distributions 		}
3384*43a90889SApple OSS Distributions 
3385*43a90889SApple OSS Distributions 		/* Callee returns a locked route upon success */
3386*43a90889SApple OSS Distributions 		if ((rt = nd6_lookup(&nb_addr, 0, ifp, 0)) == NULL) {
3387*43a90889SApple OSS Distributions 			error = EINVAL;
3388*43a90889SApple OSS Distributions 			break;
3389*43a90889SApple OSS Distributions 		}
3390*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_HELD(rt);
3391*43a90889SApple OSS Distributions 		ln = rt->rt_llinfo;
3392*43a90889SApple OSS Distributions 		nbi_64.state = ln->ln_state;
3393*43a90889SApple OSS Distributions 		nbi_64.asked = ln->ln_asked;
3394*43a90889SApple OSS Distributions 		nbi_64.isrouter = ln->ln_router;
3395*43a90889SApple OSS Distributions 		nbi_64.expire = (int)ln_getexpire(ln);
3396*43a90889SApple OSS Distributions 		RT_REMREF_LOCKED(rt);
3397*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
3398*43a90889SApple OSS Distributions 		bcopy(&nbi_64, data, sizeof(nbi_64));
3399*43a90889SApple OSS Distributions 		break;
3400*43a90889SApple OSS Distributions 	}
3401*43a90889SApple OSS Distributions 
3402*43a90889SApple OSS Distributions 	case SIOCGDEFIFACE_IN6_32:      /* struct in6_ndifreq_32 */
3403*43a90889SApple OSS Distributions 	case SIOCGDEFIFACE_IN6_64: {    /* struct in6_ndifreq_64 */
3404*43a90889SApple OSS Distributions 		struct in6_ndifreq_64 *__single ndif_64 =
3405*43a90889SApple OSS Distributions 		    (struct in6_ndifreq_64 *)(void *)data;
3406*43a90889SApple OSS Distributions 		struct in6_ndifreq_32 *__single ndif_32 =
3407*43a90889SApple OSS Distributions 		    (struct in6_ndifreq_32 *)(void *)data;
3408*43a90889SApple OSS Distributions 
3409*43a90889SApple OSS Distributions 		if (cmd == SIOCGDEFIFACE_IN6_64) {
3410*43a90889SApple OSS Distributions 			u_int64_t j = nd6_defifindex;
3411*43a90889SApple OSS Distributions 			__nochk_bcopy(&j, &ndif_64->ifindex, sizeof(j));
3412*43a90889SApple OSS Distributions 		} else {
3413*43a90889SApple OSS Distributions 			bcopy(&nd6_defifindex, &ndif_32->ifindex,
3414*43a90889SApple OSS Distributions 			    sizeof(u_int32_t));
3415*43a90889SApple OSS Distributions 		}
3416*43a90889SApple OSS Distributions 		break;
3417*43a90889SApple OSS Distributions 	}
3418*43a90889SApple OSS Distributions 
3419*43a90889SApple OSS Distributions 	case SIOCSDEFIFACE_IN6_32:      /* struct in6_ndifreq_32 */
3420*43a90889SApple OSS Distributions 	case SIOCSDEFIFACE_IN6_64: {    /* struct in6_ndifreq_64 */
3421*43a90889SApple OSS Distributions 		struct in6_ndifreq_64 *__single ndif_64 =
3422*43a90889SApple OSS Distributions 		    (struct in6_ndifreq_64 *)(void *)data;
3423*43a90889SApple OSS Distributions 		struct in6_ndifreq_32 *__single ndif_32 =
3424*43a90889SApple OSS Distributions 		    (struct in6_ndifreq_32 *)(void *)data;
3425*43a90889SApple OSS Distributions 		u_int32_t idx;
3426*43a90889SApple OSS Distributions 
3427*43a90889SApple OSS Distributions 		if (cmd == SIOCSDEFIFACE_IN6_64) {
3428*43a90889SApple OSS Distributions 			u_int64_t j;
3429*43a90889SApple OSS Distributions 			__nochk_bcopy(&ndif_64->ifindex, &j, sizeof(j));
3430*43a90889SApple OSS Distributions 			idx = (u_int32_t)j;
3431*43a90889SApple OSS Distributions 		} else {
3432*43a90889SApple OSS Distributions 			bcopy(&ndif_32->ifindex, &idx, sizeof(idx));
3433*43a90889SApple OSS Distributions 		}
3434*43a90889SApple OSS Distributions 
3435*43a90889SApple OSS Distributions 		error = nd6_setdefaultiface(idx);
3436*43a90889SApple OSS Distributions 		return error;
3437*43a90889SApple OSS Distributions 		/* NOTREACHED */
3438*43a90889SApple OSS Distributions 	}
3439*43a90889SApple OSS Distributions 	case SIOCGIFCGAPREP_IN6_32:
3440*43a90889SApple OSS Distributions 	case SIOCGIFCGAPREP_IN6_64: {
3441*43a90889SApple OSS Distributions 		/* get CGA parameters */
3442*43a90889SApple OSS Distributions 		union {
3443*43a90889SApple OSS Distributions 			struct in6_cgareq_32    *cga32;
3444*43a90889SApple OSS Distributions 			struct in6_cgareq_64    *cga64;
3445*43a90889SApple OSS Distributions 			void                    *data;
3446*43a90889SApple OSS Distributions 		} cgareq_u;
3447*43a90889SApple OSS Distributions 		struct nd_ifinfo        *__single ndi;
3448*43a90889SApple OSS Distributions 		struct in6_cga_modifier *__single ndi_cga_mod;
3449*43a90889SApple OSS Distributions 		struct in6_cga_modifier *__single req_cga_mod;
3450*43a90889SApple OSS Distributions 
3451*43a90889SApple OSS Distributions 		ndi = ND_IFINFO(ifp);
3452*43a90889SApple OSS Distributions 		if ((NULL == ndi) || !ndi->initialized) {
3453*43a90889SApple OSS Distributions 			error = EINVAL;
3454*43a90889SApple OSS Distributions 			break;
3455*43a90889SApple OSS Distributions 		}
3456*43a90889SApple OSS Distributions 		cgareq_u.data = data;
3457*43a90889SApple OSS Distributions 		req_cga_mod = (cmd == SIOCGIFCGAPREP_IN6_64)
3458*43a90889SApple OSS Distributions 		    ? &(cgareq_u.cga64->cgar_cgaprep.cga_modifier)
3459*43a90889SApple OSS Distributions 		    : &(cgareq_u.cga32->cgar_cgaprep.cga_modifier);
3460*43a90889SApple OSS Distributions 		lck_mtx_lock(&ndi->lock);
3461*43a90889SApple OSS Distributions 		ndi_cga_mod = &(ndi->local_cga_modifier);
3462*43a90889SApple OSS Distributions 		bcopy(ndi_cga_mod, req_cga_mod, sizeof(*req_cga_mod));
3463*43a90889SApple OSS Distributions 		lck_mtx_unlock(&ndi->lock);
3464*43a90889SApple OSS Distributions 		break;
3465*43a90889SApple OSS Distributions 	}
3466*43a90889SApple OSS Distributions 	case SIOCSIFCGAPREP_IN6_32:
3467*43a90889SApple OSS Distributions 	case SIOCSIFCGAPREP_IN6_64:
3468*43a90889SApple OSS Distributions 	{
3469*43a90889SApple OSS Distributions 		/* set CGA parameters */
3470*43a90889SApple OSS Distributions 		struct in6_cgareq       cgareq;
3471*43a90889SApple OSS Distributions 		struct nd_ifinfo        *__single ndi;
3472*43a90889SApple OSS Distributions 		struct in6_cga_modifier *__single ndi_cga_mod;
3473*43a90889SApple OSS Distributions 		struct in6_cga_modifier *__single req_cga_mod;
3474*43a90889SApple OSS Distributions 
3475*43a90889SApple OSS Distributions 		ndi = ND_IFINFO(ifp);
3476*43a90889SApple OSS Distributions 		if ((NULL == ndi) || !ndi->initialized) {
3477*43a90889SApple OSS Distributions 			error = EINVAL;
3478*43a90889SApple OSS Distributions 			break;
3479*43a90889SApple OSS Distributions 		}
3480*43a90889SApple OSS Distributions 		if (cmd == SIOCSIFCGAPREP_IN6_64) {
3481*43a90889SApple OSS Distributions 			in6_cgareq_copy_from_user64(data, &cgareq);
3482*43a90889SApple OSS Distributions 		} else {
3483*43a90889SApple OSS Distributions 			in6_cgareq_copy_from_user32(data, &cgareq);
3484*43a90889SApple OSS Distributions 		}
3485*43a90889SApple OSS Distributions 		req_cga_mod = &cgareq.cgar_cgaprep.cga_modifier;
3486*43a90889SApple OSS Distributions 		lck_mtx_lock(&ndi->lock);
3487*43a90889SApple OSS Distributions 		ndi_cga_mod = &(ndi->local_cga_modifier);
3488*43a90889SApple OSS Distributions 		bcopy(req_cga_mod, ndi_cga_mod, sizeof(*ndi_cga_mod));
3489*43a90889SApple OSS Distributions 		ndi->cga_initialized = TRUE;
3490*43a90889SApple OSS Distributions 		ndi->cga_collision_count = 0;
3491*43a90889SApple OSS Distributions 		lck_mtx_unlock(&ndi->lock);
3492*43a90889SApple OSS Distributions 		break;
3493*43a90889SApple OSS Distributions 	}
3494*43a90889SApple OSS Distributions 	default:
3495*43a90889SApple OSS Distributions 		break;
3496*43a90889SApple OSS Distributions 	}
3497*43a90889SApple OSS Distributions 	return error;
3498*43a90889SApple OSS Distributions }
3499*43a90889SApple OSS Distributions 
3500*43a90889SApple OSS Distributions /*
3501*43a90889SApple OSS Distributions  * Create neighbor cache entry and cache link-layer address,
3502*43a90889SApple OSS Distributions  * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
3503*43a90889SApple OSS Distributions  */
3504*43a90889SApple OSS Distributions void
nd6_cache_lladdr(struct ifnet * ifp,struct in6_addr * from,char * lladdr __sized_by (lladdrlen),int lladdrlen,int type,int code,int * did_update)3505*43a90889SApple OSS Distributions nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr __sized_by(lladdrlen),
3506*43a90889SApple OSS Distributions     int lladdrlen, int type, int code, int *did_update)
3507*43a90889SApple OSS Distributions {
3508*43a90889SApple OSS Distributions #pragma unused(lladdrlen)
3509*43a90889SApple OSS Distributions 	rtentry_ref_t rt = NULL;
3510*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = NULL;
3511*43a90889SApple OSS Distributions 	int is_newentry;
3512*43a90889SApple OSS Distributions 	struct sockaddr_dl *sdl = NULL;
3513*43a90889SApple OSS Distributions 	int do_update;
3514*43a90889SApple OSS Distributions 	int olladdr;
3515*43a90889SApple OSS Distributions 	int llchange;
3516*43a90889SApple OSS Distributions 	short newstate = 0;
3517*43a90889SApple OSS Distributions 	uint64_t timenow;
3518*43a90889SApple OSS Distributions 	boolean_t sched_timeout = FALSE;
3519*43a90889SApple OSS Distributions 	struct nd_ifinfo *__single ndi = NULL;
3520*43a90889SApple OSS Distributions 
3521*43a90889SApple OSS Distributions 	if (ifp == NULL) {
3522*43a90889SApple OSS Distributions 		panic("ifp == NULL in nd6_cache_lladdr");
3523*43a90889SApple OSS Distributions 	}
3524*43a90889SApple OSS Distributions 	if (from == NULL) {
3525*43a90889SApple OSS Distributions 		panic("from == NULL in nd6_cache_lladdr");
3526*43a90889SApple OSS Distributions 	}
3527*43a90889SApple OSS Distributions 
3528*43a90889SApple OSS Distributions 	if (did_update != NULL) {
3529*43a90889SApple OSS Distributions 		did_update = 0;
3530*43a90889SApple OSS Distributions 	}
3531*43a90889SApple OSS Distributions 
3532*43a90889SApple OSS Distributions 	/* nothing must be updated for unspecified address */
3533*43a90889SApple OSS Distributions 	if (IN6_IS_ADDR_UNSPECIFIED(from)) {
3534*43a90889SApple OSS Distributions 		return;
3535*43a90889SApple OSS Distributions 	}
3536*43a90889SApple OSS Distributions 
3537*43a90889SApple OSS Distributions 	/*
3538*43a90889SApple OSS Distributions 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
3539*43a90889SApple OSS Distributions 	 * the caller.
3540*43a90889SApple OSS Distributions 	 */
3541*43a90889SApple OSS Distributions 	timenow = net_uptime();
3542*43a90889SApple OSS Distributions 
3543*43a90889SApple OSS Distributions 	rt = nd6_lookup(from, 0, ifp, 0);
3544*43a90889SApple OSS Distributions 	if (rt == NULL) {
3545*43a90889SApple OSS Distributions 		if ((rt = nd6_lookup(from, 1, ifp, 0)) == NULL) {
3546*43a90889SApple OSS Distributions 			return;
3547*43a90889SApple OSS Distributions 		}
3548*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_HELD(rt);
3549*43a90889SApple OSS Distributions 		is_newentry = 1;
3550*43a90889SApple OSS Distributions 	} else {
3551*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_HELD(rt);
3552*43a90889SApple OSS Distributions 		/* do nothing if static ndp is set */
3553*43a90889SApple OSS Distributions 		if (rt->rt_flags & RTF_STATIC) {
3554*43a90889SApple OSS Distributions 			RT_REMREF_LOCKED(rt);
3555*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
3556*43a90889SApple OSS Distributions 			return;
3557*43a90889SApple OSS Distributions 		}
3558*43a90889SApple OSS Distributions 		is_newentry = 0;
3559*43a90889SApple OSS Distributions 	}
3560*43a90889SApple OSS Distributions 
3561*43a90889SApple OSS Distributions 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
3562*43a90889SApple OSS Distributions fail:
3563*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
3564*43a90889SApple OSS Distributions 		nd6_free(rt);
3565*43a90889SApple OSS Distributions 		rtfree(rt);
3566*43a90889SApple OSS Distributions 		return;
3567*43a90889SApple OSS Distributions 	}
3568*43a90889SApple OSS Distributions 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
3569*43a90889SApple OSS Distributions 	if (ln == NULL) {
3570*43a90889SApple OSS Distributions 		goto fail;
3571*43a90889SApple OSS Distributions 	}
3572*43a90889SApple OSS Distributions 	if (rt->rt_gateway == NULL) {
3573*43a90889SApple OSS Distributions 		goto fail;
3574*43a90889SApple OSS Distributions 	}
3575*43a90889SApple OSS Distributions 	if (rt->rt_gateway->sa_family != AF_LINK) {
3576*43a90889SApple OSS Distributions 		goto fail;
3577*43a90889SApple OSS Distributions 	}
3578*43a90889SApple OSS Distributions 	sdl = SDL(rt->rt_gateway);
3579*43a90889SApple OSS Distributions 
3580*43a90889SApple OSS Distributions 	olladdr = (sdl->sdl_alen) ? 1 : 0;
3581*43a90889SApple OSS Distributions 	if (olladdr && lladdr) {
3582*43a90889SApple OSS Distributions 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) {
3583*43a90889SApple OSS Distributions 			llchange = 1;
3584*43a90889SApple OSS Distributions 		} else {
3585*43a90889SApple OSS Distributions 			llchange = 0;
3586*43a90889SApple OSS Distributions 		}
3587*43a90889SApple OSS Distributions 	} else {
3588*43a90889SApple OSS Distributions 		llchange = 0;
3589*43a90889SApple OSS Distributions 	}
3590*43a90889SApple OSS Distributions 
3591*43a90889SApple OSS Distributions 	/*
3592*43a90889SApple OSS Distributions 	 * newentry olladdr  lladdr  llchange	(*=record)
3593*43a90889SApple OSS Distributions 	 *	0	n	n	--	(1)
3594*43a90889SApple OSS Distributions 	 *	0	y	n	--	(2)
3595*43a90889SApple OSS Distributions 	 *	0	n	y	--	(3) * STALE
3596*43a90889SApple OSS Distributions 	 *	0	y	y	n	(4) *
3597*43a90889SApple OSS Distributions 	 *	0	y	y	y	(5) * STALE
3598*43a90889SApple OSS Distributions 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
3599*43a90889SApple OSS Distributions 	 *	1	--	y	--	(7) * STALE
3600*43a90889SApple OSS Distributions 	 */
3601*43a90889SApple OSS Distributions 
3602*43a90889SApple OSS Distributions 	if (lladdr != NULL) {           /* (3-5) and (7) */
3603*43a90889SApple OSS Distributions 		/*
3604*43a90889SApple OSS Distributions 		 * Record source link-layer address
3605*43a90889SApple OSS Distributions 		 * XXX is it dependent to ifp->if_type?
3606*43a90889SApple OSS Distributions 		 */
3607*43a90889SApple OSS Distributions 		sdl->sdl_alen = ifp->if_addrlen;
3608*43a90889SApple OSS Distributions 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
3609*43a90889SApple OSS Distributions 
3610*43a90889SApple OSS Distributions 		/* cache the gateway (sender HW) address */
3611*43a90889SApple OSS Distributions 		nd6_llreach_alloc(rt, ifp, LLADDR(sdl), sdl->sdl_alen, FALSE);
3612*43a90889SApple OSS Distributions 	}
3613*43a90889SApple OSS Distributions 
3614*43a90889SApple OSS Distributions 	if (is_newentry == 0) {
3615*43a90889SApple OSS Distributions 		if ((!olladdr && lladdr != NULL) ||     /* (3) */
3616*43a90889SApple OSS Distributions 		    (olladdr && lladdr != NULL && llchange)) {  /* (5) */
3617*43a90889SApple OSS Distributions 			do_update = 1;
3618*43a90889SApple OSS Distributions 			newstate = ND6_LLINFO_STALE;
3619*43a90889SApple OSS Distributions 		} else {                                /* (1-2,4) */
3620*43a90889SApple OSS Distributions 			do_update = 0;
3621*43a90889SApple OSS Distributions 		}
3622*43a90889SApple OSS Distributions 	} else {
3623*43a90889SApple OSS Distributions 		do_update = 1;
3624*43a90889SApple OSS Distributions 		if (lladdr == NULL) {                   /* (6) */
3625*43a90889SApple OSS Distributions 			newstate = ND6_LLINFO_NOSTATE;
3626*43a90889SApple OSS Distributions 		} else {                                /* (7) */
3627*43a90889SApple OSS Distributions 			newstate = ND6_LLINFO_STALE;
3628*43a90889SApple OSS Distributions 		}
3629*43a90889SApple OSS Distributions 	}
3630*43a90889SApple OSS Distributions 
3631*43a90889SApple OSS Distributions 	/*
3632*43a90889SApple OSS Distributions 	 * For interface's that do not perform NUD or NDP
3633*43a90889SApple OSS Distributions 	 * neighbor cache entres must always be marked
3634*43a90889SApple OSS Distributions 	 * reachable with no expiry
3635*43a90889SApple OSS Distributions 	 */
3636*43a90889SApple OSS Distributions 	ndi = ND_IFINFO(ifp);
3637*43a90889SApple OSS Distributions 	VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
3638*43a90889SApple OSS Distributions 
3639*43a90889SApple OSS Distributions 	if ((ndi && !(ndi->flags & ND6_IFF_PERFORMNUD)) ||
3640*43a90889SApple OSS Distributions 	    (ifp->if_eflags & IFEF_IPV6_ND6ALT)) {
3641*43a90889SApple OSS Distributions 		newstate = ND6_LLINFO_REACHABLE;
3642*43a90889SApple OSS Distributions 		ln_setexpire(ln, 0);
3643*43a90889SApple OSS Distributions 	}
3644*43a90889SApple OSS Distributions 
3645*43a90889SApple OSS Distributions 	if (do_update) {
3646*43a90889SApple OSS Distributions 		/*
3647*43a90889SApple OSS Distributions 		 * Update the state of the neighbor cache.
3648*43a90889SApple OSS Distributions 		 */
3649*43a90889SApple OSS Distributions 		ND6_CACHE_STATE_TRANSITION(ln, newstate);
3650*43a90889SApple OSS Distributions 
3651*43a90889SApple OSS Distributions 		if ((ln->ln_state == ND6_LLINFO_STALE) ||
3652*43a90889SApple OSS Distributions 		    (ln->ln_state == ND6_LLINFO_REACHABLE)) {
3653*43a90889SApple OSS Distributions 			mbuf_ref_t m = ln->ln_hold;
3654*43a90889SApple OSS Distributions 			/*
3655*43a90889SApple OSS Distributions 			 * XXX: since nd6_output() below will cause
3656*43a90889SApple OSS Distributions 			 * a state transition to DELAY and reset the timer,
3657*43a90889SApple OSS Distributions 			 * we must set the timer now, although it is actually
3658*43a90889SApple OSS Distributions 			 * meaningless.
3659*43a90889SApple OSS Distributions 			 */
3660*43a90889SApple OSS Distributions 			if (ln->ln_state == ND6_LLINFO_STALE) {
3661*43a90889SApple OSS Distributions 				ln_setexpire(ln, timenow + nd6_gctimer);
3662*43a90889SApple OSS Distributions 			}
3663*43a90889SApple OSS Distributions 
3664*43a90889SApple OSS Distributions 			ln->ln_hold = NULL;
3665*43a90889SApple OSS Distributions 			if (m != NULL) {
3666*43a90889SApple OSS Distributions 				struct sockaddr_in6 sin6;
3667*43a90889SApple OSS Distributions 
3668*43a90889SApple OSS Distributions 				rtkey_to_sa6(rt, &sin6);
3669*43a90889SApple OSS Distributions 				/*
3670*43a90889SApple OSS Distributions 				 * we assume ifp is not a p2p here, so just
3671*43a90889SApple OSS Distributions 				 * set the 2nd argument as the 1st one.
3672*43a90889SApple OSS Distributions 				 */
3673*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
3674*43a90889SApple OSS Distributions 				nd6_output_list(ifp, ifp, m, &sin6, rt, NULL);
3675*43a90889SApple OSS Distributions 				RT_LOCK(rt);
3676*43a90889SApple OSS Distributions 			}
3677*43a90889SApple OSS Distributions 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
3678*43a90889SApple OSS Distributions 			/* probe right away */
3679*43a90889SApple OSS Distributions 			ln_setexpire(ln, timenow);
3680*43a90889SApple OSS Distributions 			sched_timeout = TRUE;
3681*43a90889SApple OSS Distributions 		}
3682*43a90889SApple OSS Distributions 	}
3683*43a90889SApple OSS Distributions 
3684*43a90889SApple OSS Distributions 	/*
3685*43a90889SApple OSS Distributions 	 * ICMP6 type dependent behavior.
3686*43a90889SApple OSS Distributions 	 *
3687*43a90889SApple OSS Distributions 	 * NS: clear IsRouter if new entry
3688*43a90889SApple OSS Distributions 	 * RS: clear IsRouter
3689*43a90889SApple OSS Distributions 	 * RA: set IsRouter if there's lladdr
3690*43a90889SApple OSS Distributions 	 * redir: clear IsRouter if new entry
3691*43a90889SApple OSS Distributions 	 *
3692*43a90889SApple OSS Distributions 	 * RA case, (1):
3693*43a90889SApple OSS Distributions 	 * The spec says that we must set IsRouter in the following cases:
3694*43a90889SApple OSS Distributions 	 * - If lladdr exist, set IsRouter.  This means (1-5).
3695*43a90889SApple OSS Distributions 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
3696*43a90889SApple OSS Distributions 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
3697*43a90889SApple OSS Distributions 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
3698*43a90889SApple OSS Distributions 	 * neighbor cache, this is similar to (6).
3699*43a90889SApple OSS Distributions 	 * This case is rare but we figured that we MUST NOT set IsRouter.
3700*43a90889SApple OSS Distributions 	 *
3701*43a90889SApple OSS Distributions 	 * newentry olladdr  lladdr  llchange	    NS  RS	RA	redir
3702*43a90889SApple OSS Distributions 	 *								D R
3703*43a90889SApple OSS Distributions 	 *	0	n	n	--	(1)	c	?	s
3704*43a90889SApple OSS Distributions 	 *	0	y	n	--	(2)	c	s	s
3705*43a90889SApple OSS Distributions 	 *	0	n	y	--	(3)	c	s	s
3706*43a90889SApple OSS Distributions 	 *	0	y	y	n	(4)	c	s	s
3707*43a90889SApple OSS Distributions 	 *	0	y	y	y	(5)	c	s	s
3708*43a90889SApple OSS Distributions 	 *	1	--	n	--	(6) c	c		c s
3709*43a90889SApple OSS Distributions 	 *	1	--	y	--	(7) c	c	s	c s
3710*43a90889SApple OSS Distributions 	 *
3711*43a90889SApple OSS Distributions 	 *					(c=clear s=set)
3712*43a90889SApple OSS Distributions 	 */
3713*43a90889SApple OSS Distributions 	switch (type & 0xff) {
3714*43a90889SApple OSS Distributions 	case ND_NEIGHBOR_SOLICIT:
3715*43a90889SApple OSS Distributions 		/*
3716*43a90889SApple OSS Distributions 		 * New entry must have is_router flag cleared.
3717*43a90889SApple OSS Distributions 		 */
3718*43a90889SApple OSS Distributions 		if (is_newentry) {      /* (6-7) */
3719*43a90889SApple OSS Distributions 			ln->ln_router = 0;
3720*43a90889SApple OSS Distributions 		}
3721*43a90889SApple OSS Distributions 		break;
3722*43a90889SApple OSS Distributions 	case ND_REDIRECT:
3723*43a90889SApple OSS Distributions 		/*
3724*43a90889SApple OSS Distributions 		 * If the ICMP message is a Redirect to a better router, always
3725*43a90889SApple OSS Distributions 		 * set the is_router flag.  Otherwise, if the entry is newly
3726*43a90889SApple OSS Distributions 		 * created, then clear the flag.  [RFC 4861, sec 8.3]
3727*43a90889SApple OSS Distributions 		 */
3728*43a90889SApple OSS Distributions 		if (code == ND_REDIRECT_ROUTER) {
3729*43a90889SApple OSS Distributions 			ln->ln_router = 1;
3730*43a90889SApple OSS Distributions 		} else if (is_newentry) { /* (6-7) */
3731*43a90889SApple OSS Distributions 			ln->ln_router = 0;
3732*43a90889SApple OSS Distributions 		}
3733*43a90889SApple OSS Distributions 		break;
3734*43a90889SApple OSS Distributions 	case ND_ROUTER_SOLICIT:
3735*43a90889SApple OSS Distributions 		/*
3736*43a90889SApple OSS Distributions 		 * is_router flag must always be cleared.
3737*43a90889SApple OSS Distributions 		 */
3738*43a90889SApple OSS Distributions 		ln->ln_router = 0;
3739*43a90889SApple OSS Distributions 		break;
3740*43a90889SApple OSS Distributions 	case ND_ROUTER_ADVERT:
3741*43a90889SApple OSS Distributions 		/*
3742*43a90889SApple OSS Distributions 		 * Mark an entry with lladdr as a router.
3743*43a90889SApple OSS Distributions 		 */
3744*43a90889SApple OSS Distributions 		if ((!is_newentry && (olladdr || lladdr)) ||    /* (2-5) */
3745*43a90889SApple OSS Distributions 		    (is_newentry && lladdr)) {                  /* (7) */
3746*43a90889SApple OSS Distributions 			ln->ln_router = 1;
3747*43a90889SApple OSS Distributions 		}
3748*43a90889SApple OSS Distributions 		break;
3749*43a90889SApple OSS Distributions 	}
3750*43a90889SApple OSS Distributions 
3751*43a90889SApple OSS Distributions 	if (do_update) {
3752*43a90889SApple OSS Distributions 		int route_ev_code = 0;
3753*43a90889SApple OSS Distributions 
3754*43a90889SApple OSS Distributions 		if (llchange) {
3755*43a90889SApple OSS Distributions 			route_ev_code = ROUTE_LLENTRY_CHANGED;
3756*43a90889SApple OSS Distributions 		} else {
3757*43a90889SApple OSS Distributions 			route_ev_code = ROUTE_LLENTRY_RESOLVED;
3758*43a90889SApple OSS Distributions 		}
3759*43a90889SApple OSS Distributions 
3760*43a90889SApple OSS Distributions 		/* Enqueue work item to invoke callback for this route entry */
3761*43a90889SApple OSS Distributions 		route_event_enqueue_nwk_wq_entry(rt, NULL, route_ev_code, NULL, TRUE);
3762*43a90889SApple OSS Distributions 
3763*43a90889SApple OSS Distributions 		if (ln->ln_router || (rt->rt_flags & RTF_ROUTER)) {
3764*43a90889SApple OSS Distributions 			radix_node_head_ref_t rnh = NULL;
3765*43a90889SApple OSS Distributions 			struct in6_addr rt_addr = SIN6(rt_key(rt))->sin6_addr;
3766*43a90889SApple OSS Distributions 			ifnet_ref_t rt_ifp = rt->rt_ifp;
3767*43a90889SApple OSS Distributions 			struct route_event rt_ev;
3768*43a90889SApple OSS Distributions 			route_event_init(&rt_ev, rt, NULL, llchange ? ROUTE_LLENTRY_CHANGED :
3769*43a90889SApple OSS Distributions 			    ROUTE_LLENTRY_RESOLVED);
3770*43a90889SApple OSS Distributions 			/*
3771*43a90889SApple OSS Distributions 			 * We already have a valid reference on rt.
3772*43a90889SApple OSS Distributions 			 * The function frees that before returning.
3773*43a90889SApple OSS Distributions 			 * We therefore don't need an extra reference here
3774*43a90889SApple OSS Distributions 			 */
3775*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
3776*43a90889SApple OSS Distributions 			defrouter_set_reachability(&rt_addr, rt_ifp, TRUE);
3777*43a90889SApple OSS Distributions 			lck_mtx_lock(rnh_lock);
3778*43a90889SApple OSS Distributions 
3779*43a90889SApple OSS Distributions 			rnh = rt_tables[AF_INET6];
3780*43a90889SApple OSS Distributions 			if (rnh != NULL) {
3781*43a90889SApple OSS Distributions 				(void) rnh->rnh_walktree(rnh, route_event_walktree,
3782*43a90889SApple OSS Distributions 				    (void *)&rt_ev);
3783*43a90889SApple OSS Distributions 			}
3784*43a90889SApple OSS Distributions 			lck_mtx_unlock(rnh_lock);
3785*43a90889SApple OSS Distributions 			RT_LOCK(rt);
3786*43a90889SApple OSS Distributions 		}
3787*43a90889SApple OSS Distributions 	}
3788*43a90889SApple OSS Distributions 
3789*43a90889SApple OSS Distributions 	if (did_update != NULL) {
3790*43a90889SApple OSS Distributions 		*did_update = do_update;
3791*43a90889SApple OSS Distributions 	}
3792*43a90889SApple OSS Distributions 
3793*43a90889SApple OSS Distributions 	/*
3794*43a90889SApple OSS Distributions 	 * When the link-layer address of a router changes, select the
3795*43a90889SApple OSS Distributions 	 * best router again.  In particular, when the neighbor entry is newly
3796*43a90889SApple OSS Distributions 	 * created, it might affect the selection policy.
3797*43a90889SApple OSS Distributions 	 * Question: can we restrict the first condition to the "is_newentry"
3798*43a90889SApple OSS Distributions 	 * case?
3799*43a90889SApple OSS Distributions 	 *
3800*43a90889SApple OSS Distributions 	 * Note: Perform default router selection even when we are a router,
3801*43a90889SApple OSS Distributions 	 * if Scoped Routing is enabled.
3802*43a90889SApple OSS Distributions 	 */
3803*43a90889SApple OSS Distributions 	if (do_update && ln->ln_router) {
3804*43a90889SApple OSS Distributions 		/*
3805*43a90889SApple OSS Distributions 		 * XXX TODO: This should also be iterated over router list
3806*43a90889SApple OSS Distributions 		 * for route information option's router lists as well.
3807*43a90889SApple OSS Distributions 		 */
3808*43a90889SApple OSS Distributions 		RT_REMREF_LOCKED(rt);
3809*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
3810*43a90889SApple OSS Distributions 		lck_mtx_lock(nd6_mutex);
3811*43a90889SApple OSS Distributions 		defrouter_select(ifp, NULL);
3812*43a90889SApple OSS Distributions 		nd6_router_select_rti_entries(ifp);
3813*43a90889SApple OSS Distributions 		lck_mtx_unlock(nd6_mutex);
3814*43a90889SApple OSS Distributions 	} else {
3815*43a90889SApple OSS Distributions 		RT_REMREF_LOCKED(rt);
3816*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
3817*43a90889SApple OSS Distributions 	}
3818*43a90889SApple OSS Distributions 	if (sched_timeout) {
3819*43a90889SApple OSS Distributions 		lck_mtx_lock(rnh_lock);
3820*43a90889SApple OSS Distributions 		nd6_sched_timeout(NULL, NULL);
3821*43a90889SApple OSS Distributions 		lck_mtx_unlock(rnh_lock);
3822*43a90889SApple OSS Distributions 	}
3823*43a90889SApple OSS Distributions }
3824*43a90889SApple OSS Distributions 
3825*43a90889SApple OSS Distributions static void
nd6_slowtimo(void * arg)3826*43a90889SApple OSS Distributions nd6_slowtimo(void *arg)
3827*43a90889SApple OSS Distributions {
3828*43a90889SApple OSS Distributions #pragma unused(arg)
3829*43a90889SApple OSS Distributions 	struct nd_ifinfo *__single nd6if = NULL;
3830*43a90889SApple OSS Distributions 	ifnet_ref_t ifp = NULL;
3831*43a90889SApple OSS Distributions 
3832*43a90889SApple OSS Distributions 	ifnet_head_lock_shared();
3833*43a90889SApple OSS Distributions 	for (ifp = ifnet_head.tqh_first; ifp;
3834*43a90889SApple OSS Distributions 	    ifp = ifp->if_link.tqe_next) {
3835*43a90889SApple OSS Distributions 		nd6if = ND_IFINFO(ifp);
3836*43a90889SApple OSS Distributions 		if ((NULL == nd6if) || (FALSE == nd6if->initialized)) {
3837*43a90889SApple OSS Distributions 			continue;
3838*43a90889SApple OSS Distributions 		}
3839*43a90889SApple OSS Distributions 
3840*43a90889SApple OSS Distributions 		lck_mtx_lock(&nd6if->lock);
3841*43a90889SApple OSS Distributions 		if (nd6if->basereachable && /* already initialized */
3842*43a90889SApple OSS Distributions 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
3843*43a90889SApple OSS Distributions 			/*
3844*43a90889SApple OSS Distributions 			 * Since reachable time rarely changes by router
3845*43a90889SApple OSS Distributions 			 * advertisements, we SHOULD insure that a new random
3846*43a90889SApple OSS Distributions 			 * value gets recomputed at least once every few hours.
3847*43a90889SApple OSS Distributions 			 * (RFC 4861, 6.3.4)
3848*43a90889SApple OSS Distributions 			 */
3849*43a90889SApple OSS Distributions 			nd6if->recalctm = nd6_recalc_reachtm_interval;
3850*43a90889SApple OSS Distributions 			nd6if->reachable =
3851*43a90889SApple OSS Distributions 			    ND_COMPUTE_RTIME(nd6if->basereachable);
3852*43a90889SApple OSS Distributions 		}
3853*43a90889SApple OSS Distributions 		lck_mtx_unlock(&nd6if->lock);
3854*43a90889SApple OSS Distributions 	}
3855*43a90889SApple OSS Distributions 	ifnet_head_done();
3856*43a90889SApple OSS Distributions 	timeout(nd6_slowtimo, NULL, ND6_SLOWTIMER_INTERVAL * hz);
3857*43a90889SApple OSS Distributions }
3858*43a90889SApple OSS Distributions 
3859*43a90889SApple OSS Distributions int
nd6_output(struct ifnet * ifp,struct ifnet * origifp,struct mbuf * m0,struct sockaddr_in6 * dst,struct rtentry * hint0,struct flowadv * adv)3860*43a90889SApple OSS Distributions nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
3861*43a90889SApple OSS Distributions     struct sockaddr_in6 *dst, struct rtentry *hint0, struct flowadv *adv)
3862*43a90889SApple OSS Distributions {
3863*43a90889SApple OSS Distributions 	return nd6_output_list(ifp, origifp, m0, dst, hint0, adv);
3864*43a90889SApple OSS Distributions }
3865*43a90889SApple OSS Distributions 
3866*43a90889SApple OSS Distributions /*
3867*43a90889SApple OSS Distributions  * nd6_output_list()
3868*43a90889SApple OSS Distributions  *
3869*43a90889SApple OSS Distributions  * Assumption: route determination for first packet can be correctly applied to
3870*43a90889SApple OSS Distributions  * all packets in the chain.
3871*43a90889SApple OSS Distributions  */
3872*43a90889SApple OSS Distributions #define senderr(e) { error = (e); goto bad; }
3873*43a90889SApple OSS Distributions int
nd6_output_list(struct ifnet * ifp,struct ifnet * origifp,struct mbuf * m0,struct sockaddr_in6 * dst,struct rtentry * hint0,struct flowadv * adv)3874*43a90889SApple OSS Distributions nd6_output_list(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
3875*43a90889SApple OSS Distributions     struct sockaddr_in6 *dst, struct rtentry *hint0, struct flowadv *adv)
3876*43a90889SApple OSS Distributions {
3877*43a90889SApple OSS Distributions 	rtentry_ref_t rt = hint0, hint = hint0;
3878*43a90889SApple OSS Distributions 	struct llinfo_nd6 *__single ln = NULL;
3879*43a90889SApple OSS Distributions 	int error = 0;
3880*43a90889SApple OSS Distributions 	uint64_t timenow;
3881*43a90889SApple OSS Distributions 	rtentry_ref_t rtrele = NULL;
3882*43a90889SApple OSS Distributions 	struct nd_ifinfo *__single ndi = NULL;
3883*43a90889SApple OSS Distributions 
3884*43a90889SApple OSS Distributions 	if (rt != NULL) {
3885*43a90889SApple OSS Distributions 		RT_LOCK_SPIN(rt);
3886*43a90889SApple OSS Distributions 		RT_ADDREF_LOCKED(rt);
3887*43a90889SApple OSS Distributions 	}
3888*43a90889SApple OSS Distributions 
3889*43a90889SApple OSS Distributions 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr) || !nd6_need_cache(ifp)) {
3890*43a90889SApple OSS Distributions 		if (rt != NULL) {
3891*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
3892*43a90889SApple OSS Distributions 		}
3893*43a90889SApple OSS Distributions 		goto sendpkt;
3894*43a90889SApple OSS Distributions 	}
3895*43a90889SApple OSS Distributions 
3896*43a90889SApple OSS Distributions 	/*
3897*43a90889SApple OSS Distributions 	 * Next hop determination.  Because we may involve the gateway route
3898*43a90889SApple OSS Distributions 	 * in addition to the original route, locking is rather complicated.
3899*43a90889SApple OSS Distributions 	 * The general concept is that regardless of whether the route points
3900*43a90889SApple OSS Distributions 	 * to the original route or to the gateway route, this routine takes
3901*43a90889SApple OSS Distributions 	 * an extra reference on such a route.  This extra reference will be
3902*43a90889SApple OSS Distributions 	 * released at the end.
3903*43a90889SApple OSS Distributions 	 *
3904*43a90889SApple OSS Distributions 	 * Care must be taken to ensure that the "hint0" route never gets freed
3905*43a90889SApple OSS Distributions 	 * via rtfree(), since the caller may have stored it inside a struct
3906*43a90889SApple OSS Distributions 	 * route with a reference held for that placeholder.
3907*43a90889SApple OSS Distributions 	 *
3908*43a90889SApple OSS Distributions 	 * This logic is similar to, though not exactly the same as the one
3909*43a90889SApple OSS Distributions 	 * used by route_to_gwroute().
3910*43a90889SApple OSS Distributions 	 */
3911*43a90889SApple OSS Distributions 	if (rt != NULL) {
3912*43a90889SApple OSS Distributions 		/*
3913*43a90889SApple OSS Distributions 		 * We have a reference to "rt" by now (or below via rtalloc1),
3914*43a90889SApple OSS Distributions 		 * which will either be released or freed at the end of this
3915*43a90889SApple OSS Distributions 		 * routine.
3916*43a90889SApple OSS Distributions 		 */
3917*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_HELD(rt);
3918*43a90889SApple OSS Distributions 		if (!(rt->rt_flags & RTF_UP)) {
3919*43a90889SApple OSS Distributions 			RT_REMREF_LOCKED(rt);
3920*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
3921*43a90889SApple OSS Distributions 			if ((hint = rt = rtalloc1_scoped(SA(dst), 1, 0,
3922*43a90889SApple OSS Distributions 			    ifp->if_index)) != NULL) {
3923*43a90889SApple OSS Distributions 				RT_LOCK_SPIN(rt);
3924*43a90889SApple OSS Distributions 				if (rt->rt_ifp != ifp) {
3925*43a90889SApple OSS Distributions 					/* XXX: loop care? */
3926*43a90889SApple OSS Distributions 					RT_UNLOCK(rt);
3927*43a90889SApple OSS Distributions 					error = nd6_output_list(ifp, origifp, m0,
3928*43a90889SApple OSS Distributions 					    dst, rt, adv);
3929*43a90889SApple OSS Distributions 					rtfree(rt);
3930*43a90889SApple OSS Distributions 					return error;
3931*43a90889SApple OSS Distributions 				}
3932*43a90889SApple OSS Distributions 			} else {
3933*43a90889SApple OSS Distributions 				senderr(EHOSTUNREACH);
3934*43a90889SApple OSS Distributions 			}
3935*43a90889SApple OSS Distributions 		}
3936*43a90889SApple OSS Distributions 
3937*43a90889SApple OSS Distributions 		if (rt->rt_flags & RTF_GATEWAY) {
3938*43a90889SApple OSS Distributions 			rtentry_ref_t gwrt;
3939*43a90889SApple OSS Distributions 			struct in6_ifaddr *__single ia6 = NULL;
3940*43a90889SApple OSS Distributions 			struct sockaddr_in6 gw6;
3941*43a90889SApple OSS Distributions 
3942*43a90889SApple OSS Distributions 			rtgw_to_sa6(rt, &gw6);
3943*43a90889SApple OSS Distributions 			/*
3944*43a90889SApple OSS Distributions 			 * Must drop rt_lock since nd6_is_addr_neighbor()
3945*43a90889SApple OSS Distributions 			 * calls nd6_lookup() and acquires rnh_lock.
3946*43a90889SApple OSS Distributions 			 */
3947*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
3948*43a90889SApple OSS Distributions 
3949*43a90889SApple OSS Distributions 			/*
3950*43a90889SApple OSS Distributions 			 * We skip link-layer address resolution and NUD
3951*43a90889SApple OSS Distributions 			 * if the gateway is not a neighbor from ND point
3952*43a90889SApple OSS Distributions 			 * of view, regardless of the value of nd_ifinfo.flags.
3953*43a90889SApple OSS Distributions 			 * The second condition is a bit tricky; we skip
3954*43a90889SApple OSS Distributions 			 * if the gateway is our own address, which is
3955*43a90889SApple OSS Distributions 			 * sometimes used to install a route to a p2p link.
3956*43a90889SApple OSS Distributions 			 */
3957*43a90889SApple OSS Distributions 			if (!nd6_is_addr_neighbor(&gw6, ifp, 0) ||
3958*43a90889SApple OSS Distributions 			    (ia6 = in6ifa_ifpwithaddr(ifp, &gw6.sin6_addr))) {
3959*43a90889SApple OSS Distributions 				/*
3960*43a90889SApple OSS Distributions 				 * We allow this kind of tricky route only
3961*43a90889SApple OSS Distributions 				 * when the outgoing interface is p2p.
3962*43a90889SApple OSS Distributions 				 * XXX: we may need a more generic rule here.
3963*43a90889SApple OSS Distributions 				 */
3964*43a90889SApple OSS Distributions 				if (ia6 != NULL) {
3965*43a90889SApple OSS Distributions 					ifa_remref(&ia6->ia_ifa);
3966*43a90889SApple OSS Distributions 				}
3967*43a90889SApple OSS Distributions 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
3968*43a90889SApple OSS Distributions 					senderr(EHOSTUNREACH);
3969*43a90889SApple OSS Distributions 				}
3970*43a90889SApple OSS Distributions 				goto sendpkt;
3971*43a90889SApple OSS Distributions 			}
3972*43a90889SApple OSS Distributions 
3973*43a90889SApple OSS Distributions 			RT_LOCK_SPIN(rt);
3974*43a90889SApple OSS Distributions 			gw6 = *(SIN6(rt->rt_gateway));
3975*43a90889SApple OSS Distributions 
3976*43a90889SApple OSS Distributions 			/* If hint is now down, give up */
3977*43a90889SApple OSS Distributions 			if (!(rt->rt_flags & RTF_UP)) {
3978*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
3979*43a90889SApple OSS Distributions 				senderr(EHOSTUNREACH);
3980*43a90889SApple OSS Distributions 			}
3981*43a90889SApple OSS Distributions 
3982*43a90889SApple OSS Distributions 			/* If there's no gateway route, look it up */
3983*43a90889SApple OSS Distributions 			if ((gwrt = rt->rt_gwroute) == NULL) {
3984*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
3985*43a90889SApple OSS Distributions 				goto lookup;
3986*43a90889SApple OSS Distributions 			}
3987*43a90889SApple OSS Distributions 			/* Become a regular mutex */
3988*43a90889SApple OSS Distributions 			RT_CONVERT_LOCK(rt);
3989*43a90889SApple OSS Distributions 
3990*43a90889SApple OSS Distributions 			/*
3991*43a90889SApple OSS Distributions 			 * Take gwrt's lock while holding route's lock;
3992*43a90889SApple OSS Distributions 			 * this is okay since gwrt never points back
3993*43a90889SApple OSS Distributions 			 * to rt, so no lock ordering issues.
3994*43a90889SApple OSS Distributions 			 */
3995*43a90889SApple OSS Distributions 			RT_LOCK_SPIN(gwrt);
3996*43a90889SApple OSS Distributions 			if (!(gwrt->rt_flags & RTF_UP)) {
3997*43a90889SApple OSS Distributions 				rt->rt_gwroute = NULL;
3998*43a90889SApple OSS Distributions 				RT_UNLOCK(gwrt);
3999*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
4000*43a90889SApple OSS Distributions 				rtfree(gwrt);
4001*43a90889SApple OSS Distributions lookup:
4002*43a90889SApple OSS Distributions 				lck_mtx_lock(rnh_lock);
4003*43a90889SApple OSS Distributions 				gwrt = rtalloc1_scoped_locked(SA(&gw6), 1, 0,
4004*43a90889SApple OSS Distributions 				    ifp->if_index);
4005*43a90889SApple OSS Distributions 
4006*43a90889SApple OSS Distributions 				RT_LOCK(rt);
4007*43a90889SApple OSS Distributions 				/*
4008*43a90889SApple OSS Distributions 				 * Bail out if the route is down, no route
4009*43a90889SApple OSS Distributions 				 * to gateway, circular route, or if the
4010*43a90889SApple OSS Distributions 				 * gateway portion of "rt" has changed.
4011*43a90889SApple OSS Distributions 				 */
4012*43a90889SApple OSS Distributions 				if (!(rt->rt_flags & RTF_UP) ||
4013*43a90889SApple OSS Distributions 				    gwrt == NULL || gwrt == rt ||
4014*43a90889SApple OSS Distributions 				    SOCKADDR_CMP(SA(&gw6), rt->rt_gateway, SA(&gw6)->sa_len) != 0) {
4015*43a90889SApple OSS Distributions 					if (gwrt == rt) {
4016*43a90889SApple OSS Distributions 						RT_REMREF_LOCKED(gwrt);
4017*43a90889SApple OSS Distributions 						gwrt = NULL;
4018*43a90889SApple OSS Distributions 					}
4019*43a90889SApple OSS Distributions 					RT_UNLOCK(rt);
4020*43a90889SApple OSS Distributions 					if (gwrt != NULL) {
4021*43a90889SApple OSS Distributions 						rtfree_locked(gwrt);
4022*43a90889SApple OSS Distributions 					}
4023*43a90889SApple OSS Distributions 					lck_mtx_unlock(rnh_lock);
4024*43a90889SApple OSS Distributions 					senderr(EHOSTUNREACH);
4025*43a90889SApple OSS Distributions 				}
4026*43a90889SApple OSS Distributions 				VERIFY(gwrt != NULL);
4027*43a90889SApple OSS Distributions 				/*
4028*43a90889SApple OSS Distributions 				 * Set gateway route; callee adds ref to gwrt;
4029*43a90889SApple OSS Distributions 				 * gwrt has an extra ref from rtalloc1() for
4030*43a90889SApple OSS Distributions 				 * this routine.
4031*43a90889SApple OSS Distributions 				 */
4032*43a90889SApple OSS Distributions 				rt_set_gwroute(rt, rt_key(rt), gwrt);
4033*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
4034*43a90889SApple OSS Distributions 				lck_mtx_unlock(rnh_lock);
4035*43a90889SApple OSS Distributions 				/* Remember to release/free "rt" at the end */
4036*43a90889SApple OSS Distributions 				rtrele = rt;
4037*43a90889SApple OSS Distributions 				rt = gwrt;
4038*43a90889SApple OSS Distributions 			} else {
4039*43a90889SApple OSS Distributions 				RT_ADDREF_LOCKED(gwrt);
4040*43a90889SApple OSS Distributions 				RT_UNLOCK(gwrt);
4041*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
4042*43a90889SApple OSS Distributions 				/* Remember to release/free "rt" at the end */
4043*43a90889SApple OSS Distributions 				rtrele = rt;
4044*43a90889SApple OSS Distributions 				rt = gwrt;
4045*43a90889SApple OSS Distributions 			}
4046*43a90889SApple OSS Distributions 			VERIFY(rt == gwrt);
4047*43a90889SApple OSS Distributions 
4048*43a90889SApple OSS Distributions 			/*
4049*43a90889SApple OSS Distributions 			 * This is an opportunity to revalidate the parent
4050*43a90889SApple OSS Distributions 			 * route's gwroute, in case it now points to a dead
4051*43a90889SApple OSS Distributions 			 * route entry.  Parent route won't go away since the
4052*43a90889SApple OSS Distributions 			 * clone (hint) holds a reference to it.  rt == gwrt.
4053*43a90889SApple OSS Distributions 			 */
4054*43a90889SApple OSS Distributions 			RT_LOCK_SPIN(hint);
4055*43a90889SApple OSS Distributions 			if ((hint->rt_flags & (RTF_WASCLONED | RTF_UP)) ==
4056*43a90889SApple OSS Distributions 			    (RTF_WASCLONED | RTF_UP)) {
4057*43a90889SApple OSS Distributions 				rtentry_ref_t prt = hint->rt_parent;
4058*43a90889SApple OSS Distributions 				VERIFY(prt != NULL);
4059*43a90889SApple OSS Distributions 
4060*43a90889SApple OSS Distributions 				RT_CONVERT_LOCK(hint);
4061*43a90889SApple OSS Distributions 				RT_ADDREF(prt);
4062*43a90889SApple OSS Distributions 				RT_UNLOCK(hint);
4063*43a90889SApple OSS Distributions 				rt_revalidate_gwroute(prt, rt);
4064*43a90889SApple OSS Distributions 				RT_REMREF(prt);
4065*43a90889SApple OSS Distributions 			} else {
4066*43a90889SApple OSS Distributions 				RT_UNLOCK(hint);
4067*43a90889SApple OSS Distributions 			}
4068*43a90889SApple OSS Distributions 
4069*43a90889SApple OSS Distributions 			RT_LOCK_SPIN(rt);
4070*43a90889SApple OSS Distributions 			/* rt == gwrt; if it is now down, give up */
4071*43a90889SApple OSS Distributions 			if (!(rt->rt_flags & RTF_UP)) {
4072*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
4073*43a90889SApple OSS Distributions 				rtfree(rt);
4074*43a90889SApple OSS Distributions 				rt = NULL;
4075*43a90889SApple OSS Distributions 				/* "rtrele" == original "rt" */
4076*43a90889SApple OSS Distributions 				senderr(EHOSTUNREACH);
4077*43a90889SApple OSS Distributions 			}
4078*43a90889SApple OSS Distributions 		}
4079*43a90889SApple OSS Distributions 
4080*43a90889SApple OSS Distributions 		/* Become a regular mutex */
4081*43a90889SApple OSS Distributions 		RT_CONVERT_LOCK(rt);
4082*43a90889SApple OSS Distributions 	}
4083*43a90889SApple OSS Distributions 
4084*43a90889SApple OSS Distributions 	/*
4085*43a90889SApple OSS Distributions 	 * Address resolution or Neighbor Unreachability Detection
4086*43a90889SApple OSS Distributions 	 * for the next hop.
4087*43a90889SApple OSS Distributions 	 * At this point, the destination of the packet must be a unicast
4088*43a90889SApple OSS Distributions 	 * or an anycast address(i.e. not a multicast).
4089*43a90889SApple OSS Distributions 	 */
4090*43a90889SApple OSS Distributions 
4091*43a90889SApple OSS Distributions 	/* Look up the neighbor cache for the nexthop */
4092*43a90889SApple OSS Distributions 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0) {
4093*43a90889SApple OSS Distributions 		ln = rt->rt_llinfo;
4094*43a90889SApple OSS Distributions 	} else {
4095*43a90889SApple OSS Distributions 		struct sockaddr_in6 sin6;
4096*43a90889SApple OSS Distributions 		/*
4097*43a90889SApple OSS Distributions 		 * Clear out Scope ID field in case it is set.
4098*43a90889SApple OSS Distributions 		 */
4099*43a90889SApple OSS Distributions 		sin6 = *dst;
4100*43a90889SApple OSS Distributions 		if (in6_embedded_scope) {
4101*43a90889SApple OSS Distributions 			sin6.sin6_scope_id = 0;
4102*43a90889SApple OSS Distributions 		}
4103*43a90889SApple OSS Distributions 		/*
4104*43a90889SApple OSS Distributions 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
4105*43a90889SApple OSS Distributions 		 * the condition below is not very efficient.  But we believe
4106*43a90889SApple OSS Distributions 		 * it is tolerable, because this should be a rare case.
4107*43a90889SApple OSS Distributions 		 * Must drop rt_lock since nd6_is_addr_neighbor() calls
4108*43a90889SApple OSS Distributions 		 * nd6_lookup() and acquires rnh_lock.
4109*43a90889SApple OSS Distributions 		 */
4110*43a90889SApple OSS Distributions 		if (rt != NULL) {
4111*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
4112*43a90889SApple OSS Distributions 		}
4113*43a90889SApple OSS Distributions 		if (nd6_is_addr_neighbor(&sin6, ifp, 0)) {
4114*43a90889SApple OSS Distributions 			/* "rtrele" may have been used, so clean up "rt" now */
4115*43a90889SApple OSS Distributions 			if (rt != NULL) {
4116*43a90889SApple OSS Distributions 				/* Don't free "hint0" */
4117*43a90889SApple OSS Distributions 				if (rt == hint0) {
4118*43a90889SApple OSS Distributions 					RT_REMREF(rt);
4119*43a90889SApple OSS Distributions 				} else {
4120*43a90889SApple OSS Distributions 					rtfree(rt);
4121*43a90889SApple OSS Distributions 				}
4122*43a90889SApple OSS Distributions 			}
4123*43a90889SApple OSS Distributions 			/* Callee returns a locked route upon success */
4124*43a90889SApple OSS Distributions 			rt = nd6_lookup(&dst->sin6_addr, 1, ifp, 0);
4125*43a90889SApple OSS Distributions 			if (rt != NULL) {
4126*43a90889SApple OSS Distributions 				RT_LOCK_ASSERT_HELD(rt);
4127*43a90889SApple OSS Distributions 				ln = rt->rt_llinfo;
4128*43a90889SApple OSS Distributions 			}
4129*43a90889SApple OSS Distributions 		} else if (rt != NULL) {
4130*43a90889SApple OSS Distributions 			RT_LOCK(rt);
4131*43a90889SApple OSS Distributions 		}
4132*43a90889SApple OSS Distributions 	}
4133*43a90889SApple OSS Distributions 
4134*43a90889SApple OSS Distributions 	if (!ln || !rt) {
4135*43a90889SApple OSS Distributions 		if (rt != NULL) {
4136*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
4137*43a90889SApple OSS Distributions 		}
4138*43a90889SApple OSS Distributions 		ndi = ND_IFINFO(ifp);
4139*43a90889SApple OSS Distributions 		VERIFY(ndi != NULL && ndi->initialized);
4140*43a90889SApple OSS Distributions 		lck_mtx_lock(&ndi->lock);
4141*43a90889SApple OSS Distributions 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
4142*43a90889SApple OSS Distributions 		    !(ndi->flags & ND6_IFF_PERFORMNUD)) {
4143*43a90889SApple OSS Distributions 			lck_mtx_unlock(&ndi->lock);
4144*43a90889SApple OSS Distributions 			log(LOG_DEBUG,
4145*43a90889SApple OSS Distributions 			    "nd6_output: can't allocate llinfo for %s "
4146*43a90889SApple OSS Distributions 			    "(ln=0x%llx, rt=0x%llx)\n",
4147*43a90889SApple OSS Distributions 			    ip6_sprintf(&dst->sin6_addr),
4148*43a90889SApple OSS Distributions 			    (uint64_t)VM_KERNEL_ADDRPERM(ln),
4149*43a90889SApple OSS Distributions 			    (uint64_t)VM_KERNEL_ADDRPERM(rt));
4150*43a90889SApple OSS Distributions 			senderr(EIO);   /* XXX: good error? */
4151*43a90889SApple OSS Distributions 		}
4152*43a90889SApple OSS Distributions 		lck_mtx_unlock(&ndi->lock);
4153*43a90889SApple OSS Distributions 
4154*43a90889SApple OSS Distributions 		goto sendpkt;   /* send anyway */
4155*43a90889SApple OSS Distributions 	}
4156*43a90889SApple OSS Distributions 
4157*43a90889SApple OSS Distributions 	net_update_uptime();
4158*43a90889SApple OSS Distributions 	timenow = net_uptime();
4159*43a90889SApple OSS Distributions 
4160*43a90889SApple OSS Distributions 	/* We don't have to do link-layer address resolution on a p2p link. */
4161*43a90889SApple OSS Distributions 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
4162*43a90889SApple OSS Distributions 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
4163*43a90889SApple OSS Distributions 		ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
4164*43a90889SApple OSS Distributions 		ln_setexpire(ln, timenow + nd6_gctimer);
4165*43a90889SApple OSS Distributions 	}
4166*43a90889SApple OSS Distributions 
4167*43a90889SApple OSS Distributions 	/*
4168*43a90889SApple OSS Distributions 	 * The first time we send a packet to a neighbor whose entry is
4169*43a90889SApple OSS Distributions 	 * STALE, we have to change the state to DELAY and a sets a timer to
4170*43a90889SApple OSS Distributions 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
4171*43a90889SApple OSS Distributions 	 * neighbor unreachability detection on expiration.
4172*43a90889SApple OSS Distributions 	 * (RFC 4861 7.3.3)
4173*43a90889SApple OSS Distributions 	 */
4174*43a90889SApple OSS Distributions 	if (ln->ln_state == ND6_LLINFO_STALE) {
4175*43a90889SApple OSS Distributions 		ln->ln_asked = 0;
4176*43a90889SApple OSS Distributions 		ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_DELAY);
4177*43a90889SApple OSS Distributions 		ln_setexpire(ln, timenow + nd6_delay);
4178*43a90889SApple OSS Distributions 		/* N.B.: we will re-arm the timer below. */
4179*43a90889SApple OSS Distributions 		_CASSERT(ND6_LLINFO_DELAY > ND6_LLINFO_INCOMPLETE);
4180*43a90889SApple OSS Distributions 	}
4181*43a90889SApple OSS Distributions 
4182*43a90889SApple OSS Distributions 	/*
4183*43a90889SApple OSS Distributions 	 * If the neighbor cache entry has a state other than INCOMPLETE
4184*43a90889SApple OSS Distributions 	 * (i.e. its link-layer address is already resolved), just
4185*43a90889SApple OSS Distributions 	 * send the packet.
4186*43a90889SApple OSS Distributions 	 */
4187*43a90889SApple OSS Distributions 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
4188*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
4189*43a90889SApple OSS Distributions 		/*
4190*43a90889SApple OSS Distributions 		 * Move this entry to the head of the queue so that it is
4191*43a90889SApple OSS Distributions 		 * less likely for this entry to be a target of forced
4192*43a90889SApple OSS Distributions 		 * garbage collection (see nd6_rtrequest()).  Do this only
4193*43a90889SApple OSS Distributions 		 * if the entry is non-permanent (as permanent ones will
4194*43a90889SApple OSS Distributions 		 * never be purged), and if the number of active entries
4195*43a90889SApple OSS Distributions 		 * is at least half of the threshold.
4196*43a90889SApple OSS Distributions 		 */
4197*43a90889SApple OSS Distributions 		if (ln->ln_state == ND6_LLINFO_DELAY ||
4198*43a90889SApple OSS Distributions 		    (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
4199*43a90889SApple OSS Distributions 		    nd6_inuse >= (ip6_neighborgcthresh >> 1))) {
4200*43a90889SApple OSS Distributions 			lck_mtx_lock(rnh_lock);
4201*43a90889SApple OSS Distributions 			if (ln->ln_state == ND6_LLINFO_DELAY) {
4202*43a90889SApple OSS Distributions 				nd6_sched_timeout(NULL, NULL);
4203*43a90889SApple OSS Distributions 			}
4204*43a90889SApple OSS Distributions 			if (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
4205*43a90889SApple OSS Distributions 			    nd6_inuse >= (ip6_neighborgcthresh >> 1)) {
4206*43a90889SApple OSS Distributions 				RT_LOCK_SPIN(rt);
4207*43a90889SApple OSS Distributions 				if (ln->ln_flags & ND6_LNF_IN_USE) {
4208*43a90889SApple OSS Distributions 					LN_DEQUEUE(ln);
4209*43a90889SApple OSS Distributions 					LN_INSERTHEAD(ln);
4210*43a90889SApple OSS Distributions 				}
4211*43a90889SApple OSS Distributions 				RT_UNLOCK(rt);
4212*43a90889SApple OSS Distributions 			}
4213*43a90889SApple OSS Distributions 			lck_mtx_unlock(rnh_lock);
4214*43a90889SApple OSS Distributions 		}
4215*43a90889SApple OSS Distributions 		goto sendpkt;
4216*43a90889SApple OSS Distributions 	}
4217*43a90889SApple OSS Distributions 
4218*43a90889SApple OSS Distributions 	/*
4219*43a90889SApple OSS Distributions 	 * If this is a prefix proxy route, record the inbound interface
4220*43a90889SApple OSS Distributions 	 * so that it can be excluded from the list of interfaces eligible
4221*43a90889SApple OSS Distributions 	 * for forwarding the proxied NS in nd6_prproxy_ns_output().
4222*43a90889SApple OSS Distributions 	 */
4223*43a90889SApple OSS Distributions 	if (rt->rt_flags & RTF_PROXY) {
4224*43a90889SApple OSS Distributions 		ln->ln_exclifp = ((origifp == ifp) ? NULL : origifp);
4225*43a90889SApple OSS Distributions 	}
4226*43a90889SApple OSS Distributions 
4227*43a90889SApple OSS Distributions 	/*
4228*43a90889SApple OSS Distributions 	 * There is a neighbor cache entry, but no ethernet address
4229*43a90889SApple OSS Distributions 	 * response yet.  Replace the held mbuf (if any) with this
4230*43a90889SApple OSS Distributions 	 * latest one.
4231*43a90889SApple OSS Distributions 	 *
4232*43a90889SApple OSS Distributions 	 * This code conforms to the rate-limiting rule described in Section
4233*43a90889SApple OSS Distributions 	 * 7.2.2 of RFC 4861, because the timer is set correctly after sending
4234*43a90889SApple OSS Distributions 	 * an NS below.
4235*43a90889SApple OSS Distributions 	 */
4236*43a90889SApple OSS Distributions 	if (ln->ln_state == ND6_LLINFO_NOSTATE) {
4237*43a90889SApple OSS Distributions 		ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_INCOMPLETE);
4238*43a90889SApple OSS Distributions 	}
4239*43a90889SApple OSS Distributions 	if (ln->ln_hold) {
4240*43a90889SApple OSS Distributions 		m_freem_list(ln->ln_hold);
4241*43a90889SApple OSS Distributions 	}
4242*43a90889SApple OSS Distributions 	ln->ln_hold = m0;
4243*43a90889SApple OSS Distributions 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
4244*43a90889SApple OSS Distributions 		ln->ln_asked++;
4245*43a90889SApple OSS Distributions 		ndi = ND_IFINFO(ifp);
4246*43a90889SApple OSS Distributions 		VERIFY(ndi != NULL && ndi->initialized);
4247*43a90889SApple OSS Distributions 		lck_mtx_lock(&ndi->lock);
4248*43a90889SApple OSS Distributions 		ln_setexpire(ln, timenow + ndi->retrans / 1000);
4249*43a90889SApple OSS Distributions 		lck_mtx_unlock(&ndi->lock);
4250*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
4251*43a90889SApple OSS Distributions 		/* We still have a reference on rt (for ln) */
4252*43a90889SApple OSS Distributions 		if (ip6_forwarding) {
4253*43a90889SApple OSS Distributions 			nd6_prproxy_ns_output(ifp, origifp, NULL,
4254*43a90889SApple OSS Distributions 			    &dst->sin6_addr, ln);
4255*43a90889SApple OSS Distributions 		} else {
4256*43a90889SApple OSS Distributions 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, NULL, 0);
4257*43a90889SApple OSS Distributions 		}
4258*43a90889SApple OSS Distributions 		lck_mtx_lock(rnh_lock);
4259*43a90889SApple OSS Distributions 		nd6_sched_timeout(NULL, NULL);
4260*43a90889SApple OSS Distributions 		lck_mtx_unlock(rnh_lock);
4261*43a90889SApple OSS Distributions 	} else {
4262*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
4263*43a90889SApple OSS Distributions 	}
4264*43a90889SApple OSS Distributions 	/*
4265*43a90889SApple OSS Distributions 	 * Move this entry to the head of the queue so that it is
4266*43a90889SApple OSS Distributions 	 * less likely for this entry to be a target of forced
4267*43a90889SApple OSS Distributions 	 * garbage collection (see nd6_rtrequest()).  Do this only
4268*43a90889SApple OSS Distributions 	 * if the entry is non-permanent (as permanent ones will
4269*43a90889SApple OSS Distributions 	 * never be purged), and if the number of active entries
4270*43a90889SApple OSS Distributions 	 * is at least half of the threshold.
4271*43a90889SApple OSS Distributions 	 */
4272*43a90889SApple OSS Distributions 	if (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
4273*43a90889SApple OSS Distributions 	    nd6_inuse >= (ip6_neighborgcthresh >> 1)) {
4274*43a90889SApple OSS Distributions 		lck_mtx_lock(rnh_lock);
4275*43a90889SApple OSS Distributions 		RT_LOCK_SPIN(rt);
4276*43a90889SApple OSS Distributions 		if (ln->ln_flags & ND6_LNF_IN_USE) {
4277*43a90889SApple OSS Distributions 			LN_DEQUEUE(ln);
4278*43a90889SApple OSS Distributions 			LN_INSERTHEAD(ln);
4279*43a90889SApple OSS Distributions 		}
4280*43a90889SApple OSS Distributions 		/* Clean up "rt" now while we can */
4281*43a90889SApple OSS Distributions 		if (rt == hint0) {
4282*43a90889SApple OSS Distributions 			RT_REMREF_LOCKED(rt);
4283*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
4284*43a90889SApple OSS Distributions 		} else {
4285*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
4286*43a90889SApple OSS Distributions 			rtfree_locked(rt);
4287*43a90889SApple OSS Distributions 		}
4288*43a90889SApple OSS Distributions 		rt = NULL;      /* "rt" has been taken care of */
4289*43a90889SApple OSS Distributions 		lck_mtx_unlock(rnh_lock);
4290*43a90889SApple OSS Distributions 	}
4291*43a90889SApple OSS Distributions 	error = 0;
4292*43a90889SApple OSS Distributions 	goto release;
4293*43a90889SApple OSS Distributions 
4294*43a90889SApple OSS Distributions sendpkt:
4295*43a90889SApple OSS Distributions 	if (rt != NULL) {
4296*43a90889SApple OSS Distributions 		RT_LOCK_ASSERT_NOTHELD(rt);
4297*43a90889SApple OSS Distributions 	}
4298*43a90889SApple OSS Distributions 
4299*43a90889SApple OSS Distributions 	/* discard the packet if IPv6 operation is disabled on the interface */
4300*43a90889SApple OSS Distributions 	if (ifp->if_eflags & IFEF_IPV6_DISABLED) {
4301*43a90889SApple OSS Distributions 		error = ENETDOWN; /* better error? */
4302*43a90889SApple OSS Distributions 		goto bad;
4303*43a90889SApple OSS Distributions 	}
4304*43a90889SApple OSS Distributions 
4305*43a90889SApple OSS Distributions 	if (ifp->if_flags & IFF_LOOPBACK) {
4306*43a90889SApple OSS Distributions 		/* forwarding rules require the original scope_id */
4307*43a90889SApple OSS Distributions 		m0->m_pkthdr.rcvif = origifp;
4308*43a90889SApple OSS Distributions 		error = dlil_output(origifp, PF_INET6, m0, (caddr_t)rt,
4309*43a90889SApple OSS Distributions 		    SA(dst), 0, adv);
4310*43a90889SApple OSS Distributions 		goto release;
4311*43a90889SApple OSS Distributions 	} else {
4312*43a90889SApple OSS Distributions 		/* Do not allow loopback address to wind up on a wire */
4313*43a90889SApple OSS Distributions 		struct ip6_hdr *__single ip6 = mtod(m0, struct ip6_hdr *);
4314*43a90889SApple OSS Distributions 
4315*43a90889SApple OSS Distributions 		if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
4316*43a90889SApple OSS Distributions 		    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst))) {
4317*43a90889SApple OSS Distributions 			ip6stat.ip6s_badscope++;
4318*43a90889SApple OSS Distributions 			error = EADDRNOTAVAIL;
4319*43a90889SApple OSS Distributions 			goto bad;
4320*43a90889SApple OSS Distributions 		}
4321*43a90889SApple OSS Distributions 	}
4322*43a90889SApple OSS Distributions 
4323*43a90889SApple OSS Distributions 	if (rt != NULL) {
4324*43a90889SApple OSS Distributions 		RT_LOCK_SPIN(rt);
4325*43a90889SApple OSS Distributions 		/* Mark use timestamp */
4326*43a90889SApple OSS Distributions 		if (rt->rt_llinfo != NULL) {
4327*43a90889SApple OSS Distributions 			nd6_llreach_use(rt->rt_llinfo);
4328*43a90889SApple OSS Distributions 		}
4329*43a90889SApple OSS Distributions 		RT_UNLOCK(rt);
4330*43a90889SApple OSS Distributions 	}
4331*43a90889SApple OSS Distributions 
4332*43a90889SApple OSS Distributions 	mbuf_ref_t mcur = m0;
4333*43a90889SApple OSS Distributions 	uint32_t pktcnt = 0;
4334*43a90889SApple OSS Distributions 
4335*43a90889SApple OSS Distributions 	while (mcur) {
4336*43a90889SApple OSS Distributions 		if (hint != NULL && nstat_collect) {
4337*43a90889SApple OSS Distributions 			int scnt;
4338*43a90889SApple OSS Distributions 
4339*43a90889SApple OSS Distributions 			if ((mcur->m_pkthdr.csum_flags & CSUM_TSO_IPV6) &&
4340*43a90889SApple OSS Distributions 			    (mcur->m_pkthdr.tso_segsz > 0)) {
4341*43a90889SApple OSS Distributions 				scnt = mcur->m_pkthdr.len / mcur->m_pkthdr.tso_segsz;
4342*43a90889SApple OSS Distributions 			} else {
4343*43a90889SApple OSS Distributions 				scnt = 1;
4344*43a90889SApple OSS Distributions 			}
4345*43a90889SApple OSS Distributions 
4346*43a90889SApple OSS Distributions 			nstat_route_tx(hint, scnt, mcur->m_pkthdr.len, 0);
4347*43a90889SApple OSS Distributions 		}
4348*43a90889SApple OSS Distributions 		pktcnt++;
4349*43a90889SApple OSS Distributions 
4350*43a90889SApple OSS Distributions 		mcur->m_pkthdr.rcvif = NULL;
4351*43a90889SApple OSS Distributions 		mcur = mcur->m_nextpkt;
4352*43a90889SApple OSS Distributions 	}
4353*43a90889SApple OSS Distributions 	if (pktcnt > ip6_maxchainsent) {
4354*43a90889SApple OSS Distributions 		ip6_maxchainsent = pktcnt;
4355*43a90889SApple OSS Distributions 	}
4356*43a90889SApple OSS Distributions 	error = dlil_output(ifp, PF_INET6, m0, (caddr_t)rt, SA(dst), 0, adv);
4357*43a90889SApple OSS Distributions 	goto release;
4358*43a90889SApple OSS Distributions 
4359*43a90889SApple OSS Distributions bad:
4360*43a90889SApple OSS Distributions 	if (m0 != NULL) {
4361*43a90889SApple OSS Distributions 		m_freem_list(m0);
4362*43a90889SApple OSS Distributions 	}
4363*43a90889SApple OSS Distributions 
4364*43a90889SApple OSS Distributions release:
4365*43a90889SApple OSS Distributions 	/* Clean up "rt" unless it's already been done */
4366*43a90889SApple OSS Distributions 	if (rt != NULL) {
4367*43a90889SApple OSS Distributions 		RT_LOCK_SPIN(rt);
4368*43a90889SApple OSS Distributions 		if (rt == hint0) {
4369*43a90889SApple OSS Distributions 			RT_REMREF_LOCKED(rt);
4370*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
4371*43a90889SApple OSS Distributions 		} else {
4372*43a90889SApple OSS Distributions 			RT_UNLOCK(rt);
4373*43a90889SApple OSS Distributions 			rtfree(rt);
4374*43a90889SApple OSS Distributions 		}
4375*43a90889SApple OSS Distributions 	}
4376*43a90889SApple OSS Distributions 	/* And now clean up "rtrele" if there is any */
4377*43a90889SApple OSS Distributions 	if (rtrele != NULL) {
4378*43a90889SApple OSS Distributions 		RT_LOCK_SPIN(rtrele);
4379*43a90889SApple OSS Distributions 		if (rtrele == hint0) {
4380*43a90889SApple OSS Distributions 			RT_REMREF_LOCKED(rtrele);
4381*43a90889SApple OSS Distributions 			RT_UNLOCK(rtrele);
4382*43a90889SApple OSS Distributions 		} else {
4383*43a90889SApple OSS Distributions 			RT_UNLOCK(rtrele);
4384*43a90889SApple OSS Distributions 			rtfree(rtrele);
4385*43a90889SApple OSS Distributions 		}
4386*43a90889SApple OSS Distributions 	}
4387*43a90889SApple OSS Distributions 	return error;
4388*43a90889SApple OSS Distributions }
4389*43a90889SApple OSS Distributions #undef senderr
4390*43a90889SApple OSS Distributions 
4391*43a90889SApple OSS Distributions int
nd6_need_cache(struct ifnet * ifp)4392*43a90889SApple OSS Distributions nd6_need_cache(struct ifnet *ifp)
4393*43a90889SApple OSS Distributions {
4394*43a90889SApple OSS Distributions 	/*
4395*43a90889SApple OSS Distributions 	 * XXX: we currently do not make neighbor cache on any interface
4396*43a90889SApple OSS Distributions 	 * other than ARCnet, Ethernet, FDDI and GIF.
4397*43a90889SApple OSS Distributions 	 *
4398*43a90889SApple OSS Distributions 	 * RFC2893 says:
4399*43a90889SApple OSS Distributions 	 * - unidirectional tunnels needs no ND
4400*43a90889SApple OSS Distributions 	 */
4401*43a90889SApple OSS Distributions 	switch (ifp->if_type) {
4402*43a90889SApple OSS Distributions 	case IFT_ARCNET:
4403*43a90889SApple OSS Distributions 	case IFT_ETHER:
4404*43a90889SApple OSS Distributions 	case IFT_FDDI:
4405*43a90889SApple OSS Distributions 	case IFT_IEEE1394:
4406*43a90889SApple OSS Distributions 	case IFT_L2VLAN:
4407*43a90889SApple OSS Distributions 	case IFT_IEEE8023ADLAG:
4408*43a90889SApple OSS Distributions #if IFT_IEEE80211
4409*43a90889SApple OSS Distributions 	case IFT_IEEE80211:
4410*43a90889SApple OSS Distributions #endif
4411*43a90889SApple OSS Distributions 	case IFT_GIF:           /* XXX need more cases? */
4412*43a90889SApple OSS Distributions 	case IFT_PPP:
4413*43a90889SApple OSS Distributions #if IFT_TUNNEL
4414*43a90889SApple OSS Distributions 	case IFT_TUNNEL:
4415*43a90889SApple OSS Distributions #endif
4416*43a90889SApple OSS Distributions 	case IFT_BRIDGE:
4417*43a90889SApple OSS Distributions 	case IFT_CELLULAR:
4418*43a90889SApple OSS Distributions 		return 1;
4419*43a90889SApple OSS Distributions 	default:
4420*43a90889SApple OSS Distributions 		return 0;
4421*43a90889SApple OSS Distributions 	}
4422*43a90889SApple OSS Distributions }
4423*43a90889SApple OSS Distributions 
4424*43a90889SApple OSS Distributions /*
4425*43a90889SApple OSS Distributions  * This is the ND pre-output routine; care must be taken to ensure that
4426*43a90889SApple OSS Distributions  * the "hint" route never gets freed via rtfree(), since the caller may
4427*43a90889SApple OSS Distributions  * have stored it inside a struct route with a reference held for that
4428*43a90889SApple OSS Distributions  * placeholder.
4429*43a90889SApple OSS Distributions  */
4430*43a90889SApple OSS Distributions errno_t
nd6_lookup_ipv6(ifnet_t ifp,const struct sockaddr_in6 * ip6_dest,struct sockaddr_dl * ll_dest,size_t ll_dest_len,route_t hint,mbuf_t packet)4431*43a90889SApple OSS Distributions nd6_lookup_ipv6(ifnet_t  ifp, const struct sockaddr_in6 *ip6_dest,
4432*43a90889SApple OSS Distributions     struct sockaddr_dl *ll_dest, size_t ll_dest_len, route_t hint,
4433*43a90889SApple OSS Distributions     mbuf_t packet)
4434*43a90889SApple OSS Distributions {
4435*43a90889SApple OSS Distributions 	route_t __single route = hint;
4436*43a90889SApple OSS Distributions 	errno_t result = 0;
4437*43a90889SApple OSS Distributions 	struct sockaddr_dl *__single sdl = NULL;
4438*43a90889SApple OSS Distributions 	size_t  copy_len;
4439*43a90889SApple OSS Distributions 
4440*43a90889SApple OSS Distributions 	if (ifp == NULL || ip6_dest == NULL) {
4441*43a90889SApple OSS Distributions 		return EINVAL;
4442*43a90889SApple OSS Distributions 	}
4443*43a90889SApple OSS Distributions 
4444*43a90889SApple OSS Distributions 	if (ip6_dest->sin6_family != AF_INET6) {
4445*43a90889SApple OSS Distributions 		return EAFNOSUPPORT;
4446*43a90889SApple OSS Distributions 	}
4447*43a90889SApple OSS Distributions 
4448*43a90889SApple OSS Distributions 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) {
4449*43a90889SApple OSS Distributions 		return ENETDOWN;
4450*43a90889SApple OSS Distributions 	}
4451*43a90889SApple OSS Distributions 
4452*43a90889SApple OSS Distributions 	if (hint != NULL) {
4453*43a90889SApple OSS Distributions 		/*
4454*43a90889SApple OSS Distributions 		 * Callee holds a reference on the route and returns
4455*43a90889SApple OSS Distributions 		 * with the route entry locked, upon success.
4456*43a90889SApple OSS Distributions 		 */
4457*43a90889SApple OSS Distributions 		result = route_to_gwroute(SA(ip6_dest), hint, &route);
4458*43a90889SApple OSS Distributions 		if (result != 0) {
4459*43a90889SApple OSS Distributions 			return result;
4460*43a90889SApple OSS Distributions 		}
4461*43a90889SApple OSS Distributions 		if (route != NULL) {
4462*43a90889SApple OSS Distributions 			RT_LOCK_ASSERT_HELD(route);
4463*43a90889SApple OSS Distributions 		}
4464*43a90889SApple OSS Distributions 	}
4465*43a90889SApple OSS Distributions 
4466*43a90889SApple OSS Distributions 	if ((packet != NULL && (packet->m_flags & M_MCAST) != 0) ||
4467*43a90889SApple OSS Distributions 	    ((ifp->if_flags & IFF_MULTICAST) &&
4468*43a90889SApple OSS Distributions 	    IN6_IS_ADDR_MULTICAST(&ip6_dest->sin6_addr))) {
4469*43a90889SApple OSS Distributions 		if (route != NULL) {
4470*43a90889SApple OSS Distributions 			RT_UNLOCK(route);
4471*43a90889SApple OSS Distributions 		}
4472*43a90889SApple OSS Distributions 		result = dlil_resolve_multi(ifp, SA(ip6_dest), SA(ll_dest), ll_dest_len);
4473*43a90889SApple OSS Distributions 		if (route != NULL) {
4474*43a90889SApple OSS Distributions 			RT_LOCK(route);
4475*43a90889SApple OSS Distributions 		}
4476*43a90889SApple OSS Distributions 		goto release;
4477*43a90889SApple OSS Distributions 	} else if (route == NULL) {
4478*43a90889SApple OSS Distributions 		/*
4479*43a90889SApple OSS Distributions 		 * rdar://24596652
4480*43a90889SApple OSS Distributions 		 * For unicast, lookup existing ND6 entries but
4481*43a90889SApple OSS Distributions 		 * do not trigger a resolution
4482*43a90889SApple OSS Distributions 		 */
4483*43a90889SApple OSS Distributions 		lck_mtx_lock(rnh_lock);
4484*43a90889SApple OSS Distributions 		route = rt_lookup(TRUE,
4485*43a90889SApple OSS Distributions 		    __DECONST(struct sockaddr *, ip6_dest), NULL,
4486*43a90889SApple OSS Distributions 		    rt_tables[AF_INET6], ifp->if_index);
4487*43a90889SApple OSS Distributions 		lck_mtx_unlock(rnh_lock);
4488*43a90889SApple OSS Distributions 
4489*43a90889SApple OSS Distributions 		if (route != NULL) {
4490*43a90889SApple OSS Distributions 			RT_LOCK(route);
4491*43a90889SApple OSS Distributions 		}
4492*43a90889SApple OSS Distributions 	}
4493*43a90889SApple OSS Distributions 
4494*43a90889SApple OSS Distributions 	if (route == NULL) {
4495*43a90889SApple OSS Distributions 		/*
4496*43a90889SApple OSS Distributions 		 * This could happen, if we could not allocate memory or
4497*43a90889SApple OSS Distributions 		 * if route_to_gwroute() didn't return a route.
4498*43a90889SApple OSS Distributions 		 */
4499*43a90889SApple OSS Distributions 		result = ENOBUFS;
4500*43a90889SApple OSS Distributions 		goto release;
4501*43a90889SApple OSS Distributions 	}
4502*43a90889SApple OSS Distributions 
4503*43a90889SApple OSS Distributions 	if (route->rt_gateway->sa_family != AF_LINK) {
4504*43a90889SApple OSS Distributions 		nd6log0(error, "%s: route %s on %s%d gateway address not AF_LINK\n",
4505*43a90889SApple OSS Distributions 		    __func__, ip6_sprintf(&ip6_dest->sin6_addr),
4506*43a90889SApple OSS Distributions 		    route->rt_ifp->if_name, route->rt_ifp->if_unit);
4507*43a90889SApple OSS Distributions 		result = EADDRNOTAVAIL;
4508*43a90889SApple OSS Distributions 		goto release;
4509*43a90889SApple OSS Distributions 	}
4510*43a90889SApple OSS Distributions 
4511*43a90889SApple OSS Distributions 	sdl = SDL(route->rt_gateway);
4512*43a90889SApple OSS Distributions 	if (sdl->sdl_alen == 0) {
4513*43a90889SApple OSS Distributions 		/* this should be impossible, but we bark here for debugging */
4514*43a90889SApple OSS Distributions 		nd6log(error, "%s: route %s on %s%d sdl_alen == 0\n", __func__,
4515*43a90889SApple OSS Distributions 		    ip6_sprintf(&ip6_dest->sin6_addr), route->rt_ifp->if_name,
4516*43a90889SApple OSS Distributions 		    route->rt_ifp->if_unit);
4517*43a90889SApple OSS Distributions 		result = EHOSTUNREACH;
4518*43a90889SApple OSS Distributions 		goto release;
4519*43a90889SApple OSS Distributions 	}
4520*43a90889SApple OSS Distributions 
4521*43a90889SApple OSS Distributions 	copy_len = sdl->sdl_len <= ll_dest_len ? sdl->sdl_len : ll_dest_len;
4522*43a90889SApple OSS Distributions 	SOCKADDR_COPY(sdl, ll_dest, copy_len);
4523*43a90889SApple OSS Distributions 
4524*43a90889SApple OSS Distributions release:
4525*43a90889SApple OSS Distributions 	if (route != NULL) {
4526*43a90889SApple OSS Distributions 		if (route == hint) {
4527*43a90889SApple OSS Distributions 			RT_REMREF_LOCKED(route);
4528*43a90889SApple OSS Distributions 			RT_UNLOCK(route);
4529*43a90889SApple OSS Distributions 		} else {
4530*43a90889SApple OSS Distributions 			RT_UNLOCK(route);
4531*43a90889SApple OSS Distributions 			rtfree(route);
4532*43a90889SApple OSS Distributions 		}
4533*43a90889SApple OSS Distributions 	}
4534*43a90889SApple OSS Distributions 	return result;
4535*43a90889SApple OSS Distributions }
4536*43a90889SApple OSS Distributions 
4537*43a90889SApple OSS Distributions #if (DEVELOPMENT || DEBUG)
4538*43a90889SApple OSS Distributions 
4539*43a90889SApple OSS Distributions static int sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS;
4540*43a90889SApple OSS Distributions SYSCTL_PROC(_net_inet6_icmp6, OID_AUTO, nd6_lookup_ipv6,
4541*43a90889SApple OSS Distributions     CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0,
4542*43a90889SApple OSS Distributions     sysctl_nd6_lookup_ipv6, "S", "");
4543*43a90889SApple OSS Distributions 
4544*43a90889SApple OSS Distributions int
4545*43a90889SApple OSS Distributions sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
4546*43a90889SApple OSS Distributions {
4547*43a90889SApple OSS Distributions #pragma unused(oidp, arg1, arg2)
4548*43a90889SApple OSS Distributions 	int error = 0;
4549*43a90889SApple OSS Distributions 	struct nd6_lookup_ipv6_args nd6_lookup_ipv6_args;
4550*43a90889SApple OSS Distributions 	ifnet_ref_t ifp = NULL;
4551*43a90889SApple OSS Distributions 
4552*43a90889SApple OSS Distributions 	/*
4553*43a90889SApple OSS Distributions 	 * Only root can lookup MAC addresses
4554*43a90889SApple OSS Distributions 	 */
4555*43a90889SApple OSS Distributions 	error = proc_suser(current_proc());
4556*43a90889SApple OSS Distributions 	if (error != 0) {
4557*43a90889SApple OSS Distributions 		nd6log0(error, "%s: proc_suser() error %d\n",
4558*43a90889SApple OSS Distributions 		    __func__, error);
4559*43a90889SApple OSS Distributions 		goto done;
4560*43a90889SApple OSS Distributions 	}
4561*43a90889SApple OSS Distributions 	if (req->oldptr == USER_ADDR_NULL) {
4562*43a90889SApple OSS Distributions 		req->oldidx = sizeof(struct nd6_lookup_ipv6_args);
4563*43a90889SApple OSS Distributions 	}
4564*43a90889SApple OSS Distributions 	if (req->newptr == USER_ADDR_NULL) {
4565*43a90889SApple OSS Distributions 		goto done;
4566*43a90889SApple OSS Distributions 	}
4567*43a90889SApple OSS Distributions 	if (req->oldlen != sizeof(struct nd6_lookup_ipv6_args) ||
4568*43a90889SApple OSS Distributions 	    req->newlen != sizeof(struct nd6_lookup_ipv6_args)) {
4569*43a90889SApple OSS Distributions 		error = EINVAL;
4570*43a90889SApple OSS Distributions 		nd6log0(error, "%s: bad req, error %d\n",
4571*43a90889SApple OSS Distributions 		    __func__, error);
4572*43a90889SApple OSS Distributions 		goto done;
4573*43a90889SApple OSS Distributions 	}
4574*43a90889SApple OSS Distributions 	error = SYSCTL_IN(req, &nd6_lookup_ipv6_args,
4575*43a90889SApple OSS Distributions 	    sizeof(struct nd6_lookup_ipv6_args));
4576*43a90889SApple OSS Distributions 	if (error != 0) {
4577*43a90889SApple OSS Distributions 		nd6log0(error, "%s: SYSCTL_IN() error %d\n",
4578*43a90889SApple OSS Distributions 		    __func__, error);
4579*43a90889SApple OSS Distributions 		goto done;
4580*43a90889SApple OSS Distributions 	}
4581*43a90889SApple OSS Distributions 
4582*43a90889SApple OSS Distributions 	if (nd6_lookup_ipv6_args.ll_dest_len > sizeof(nd6_lookup_ipv6_args.ll_dest_)) {
4583*43a90889SApple OSS Distributions 		error = EINVAL;
4584*43a90889SApple OSS Distributions 		nd6log0(error, "%s: bad ll_dest_len, error %d\n",
4585*43a90889SApple OSS Distributions 		    __func__, error);
4586*43a90889SApple OSS Distributions 		goto done;
4587*43a90889SApple OSS Distributions 	}
4588*43a90889SApple OSS Distributions 
4589*43a90889SApple OSS Distributions 	/* Make sure to terminate the string */
4590*43a90889SApple OSS Distributions 	nd6_lookup_ipv6_args.ifname[IFNAMSIZ - 1] = 0;
4591*43a90889SApple OSS Distributions 
4592*43a90889SApple OSS Distributions 	error = ifnet_find_by_name(__unsafe_null_terminated_from_indexable(nd6_lookup_ipv6_args.ifname), &ifp);
4593*43a90889SApple OSS Distributions 	if (error != 0) {
4594*43a90889SApple OSS Distributions 		nd6log0(error, "%s: ifnet_find_by_name() error %d\n",
4595*43a90889SApple OSS Distributions 		    __func__, error);
4596*43a90889SApple OSS Distributions 		goto done;
4597*43a90889SApple OSS Distributions 	}
4598*43a90889SApple OSS Distributions 
4599*43a90889SApple OSS Distributions 	error = nd6_lookup_ipv6(ifp, &nd6_lookup_ipv6_args.ip6_dest,
4600*43a90889SApple OSS Distributions 	    &nd6_lookup_ipv6_args.ll_dest_._sdl,
4601*43a90889SApple OSS Distributions 	    nd6_lookup_ipv6_args.ll_dest_len, NULL, NULL);
4602*43a90889SApple OSS Distributions 	if (error != 0) {
4603*43a90889SApple OSS Distributions 		nd6log0(error, "%s: nd6_lookup_ipv6() error %d\n",
4604*43a90889SApple OSS Distributions 		    __func__, error);
4605*43a90889SApple OSS Distributions 		goto done;
4606*43a90889SApple OSS Distributions 	}
4607*43a90889SApple OSS Distributions 
4608*43a90889SApple OSS Distributions 	error = SYSCTL_OUT(req, &nd6_lookup_ipv6_args,
4609*43a90889SApple OSS Distributions 	    sizeof(struct nd6_lookup_ipv6_args));
4610*43a90889SApple OSS Distributions 	if (error != 0) {
4611*43a90889SApple OSS Distributions 		nd6log0(error, "%s: SYSCTL_OUT() error %d\n",
4612*43a90889SApple OSS Distributions 		    __func__, error);
4613*43a90889SApple OSS Distributions 		goto done;
4614*43a90889SApple OSS Distributions 	}
4615*43a90889SApple OSS Distributions done:
4616*43a90889SApple OSS Distributions 	return error;
4617*43a90889SApple OSS Distributions }
4618*43a90889SApple OSS Distributions 
4619*43a90889SApple OSS Distributions #endif /* (DEVELOPMENT || DEBUG) */
4620*43a90889SApple OSS Distributions 
4621*43a90889SApple OSS Distributions int
nd6_setifinfo(struct ifnet * ifp,u_int32_t before,u_int32_t after)4622*43a90889SApple OSS Distributions nd6_setifinfo(struct ifnet *ifp, u_int32_t before, u_int32_t after)
4623*43a90889SApple OSS Distributions {
4624*43a90889SApple OSS Distributions 	uint32_t b, a;
4625*43a90889SApple OSS Distributions 	int err = 0;
4626*43a90889SApple OSS Distributions 
4627*43a90889SApple OSS Distributions 	/*
4628*43a90889SApple OSS Distributions 	 * Handle ND6_IFF_IFDISABLED
4629*43a90889SApple OSS Distributions 	 */
4630*43a90889SApple OSS Distributions 	if ((before & ND6_IFF_IFDISABLED) ||
4631*43a90889SApple OSS Distributions 	    (after & ND6_IFF_IFDISABLED)) {
4632*43a90889SApple OSS Distributions 		b = (before & ND6_IFF_IFDISABLED);
4633*43a90889SApple OSS Distributions 		a = (after & ND6_IFF_IFDISABLED);
4634*43a90889SApple OSS Distributions 
4635*43a90889SApple OSS Distributions 		if (b != a && (err = nd6_if_disable(ifp,
4636*43a90889SApple OSS Distributions 		    ((int32_t)(a - b) > 0))) != 0) {
4637*43a90889SApple OSS Distributions 			goto done;
4638*43a90889SApple OSS Distributions 		}
4639*43a90889SApple OSS Distributions 	}
4640*43a90889SApple OSS Distributions 
4641*43a90889SApple OSS Distributions 	/*
4642*43a90889SApple OSS Distributions 	 * Handle ND6_IFF_PROXY_PREFIXES
4643*43a90889SApple OSS Distributions 	 */
4644*43a90889SApple OSS Distributions 	if ((before & ND6_IFF_PROXY_PREFIXES) ||
4645*43a90889SApple OSS Distributions 	    (after & ND6_IFF_PROXY_PREFIXES)) {
4646*43a90889SApple OSS Distributions 		b = (before & ND6_IFF_PROXY_PREFIXES);
4647*43a90889SApple OSS Distributions 		a = (after & ND6_IFF_PROXY_PREFIXES);
4648*43a90889SApple OSS Distributions 
4649*43a90889SApple OSS Distributions 		if (b != a && (err = nd6_if_prproxy(ifp,
4650*43a90889SApple OSS Distributions 		    ((int32_t)(a - b) > 0))) != 0) {
4651*43a90889SApple OSS Distributions 			goto done;
4652*43a90889SApple OSS Distributions 		}
4653*43a90889SApple OSS Distributions 	}
4654*43a90889SApple OSS Distributions done:
4655*43a90889SApple OSS Distributions 	return err;
4656*43a90889SApple OSS Distributions }
4657*43a90889SApple OSS Distributions 
4658*43a90889SApple OSS Distributions /*
4659*43a90889SApple OSS Distributions  * Enable/disable IPv6 on an interface, called as part of
4660*43a90889SApple OSS Distributions  * setting/clearing ND6_IFF_IFDISABLED, or during DAD failure.
4661*43a90889SApple OSS Distributions  */
4662*43a90889SApple OSS Distributions int
nd6_if_disable(struct ifnet * ifp,boolean_t enable)4663*43a90889SApple OSS Distributions nd6_if_disable(struct ifnet *ifp, boolean_t enable)
4664*43a90889SApple OSS Distributions {
4665*43a90889SApple OSS Distributions 	if (enable) {
4666*43a90889SApple OSS Distributions 		if_set_eflags(ifp, IFEF_IPV6_DISABLED);
4667*43a90889SApple OSS Distributions 	} else {
4668*43a90889SApple OSS Distributions 		if_clear_eflags(ifp, IFEF_IPV6_DISABLED);
4669*43a90889SApple OSS Distributions 	}
4670*43a90889SApple OSS Distributions 
4671*43a90889SApple OSS Distributions 	return 0;
4672*43a90889SApple OSS Distributions }
4673*43a90889SApple OSS Distributions 
4674*43a90889SApple OSS Distributions static int
4675*43a90889SApple OSS Distributions nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
4676*43a90889SApple OSS Distributions {
4677*43a90889SApple OSS Distributions #pragma unused(oidp, arg1, arg2)
4678*43a90889SApple OSS Distributions 	char pbuf[MAX_IPv6_STR_LEN];
4679*43a90889SApple OSS Distributions 	struct nd_defrouter *__single dr;
4680*43a90889SApple OSS Distributions 	int error = 0;
4681*43a90889SApple OSS Distributions 
4682*43a90889SApple OSS Distributions 	if (req->newptr != USER_ADDR_NULL) {
4683*43a90889SApple OSS Distributions 		return EPERM;
4684*43a90889SApple OSS Distributions 	}
4685*43a90889SApple OSS Distributions 
4686*43a90889SApple OSS Distributions 	/* XXX Handle mapped defrouter entries */
4687*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
4688*43a90889SApple OSS Distributions 	if (proc_is64bit(req->p)) {
4689*43a90889SApple OSS Distributions 		struct in6_defrouter_64 d;
4690*43a90889SApple OSS Distributions 
4691*43a90889SApple OSS Distributions 		bzero(&d, sizeof(d));
4692*43a90889SApple OSS Distributions 		d.rtaddr.sin6_family = AF_INET6;
4693*43a90889SApple OSS Distributions 		d.rtaddr.sin6_len = sizeof(d.rtaddr);
4694*43a90889SApple OSS Distributions 
4695*43a90889SApple OSS Distributions 		TAILQ_FOREACH(dr, &nd_defrouter_list, dr_entry) {
4696*43a90889SApple OSS Distributions 			d.rtaddr.sin6_addr = dr->rtaddr;
4697*43a90889SApple OSS Distributions 			if (in6_recoverscope(&d.rtaddr,
4698*43a90889SApple OSS Distributions 			    &dr->rtaddr, dr->ifp) != 0) {
4699*43a90889SApple OSS Distributions 				log(LOG_ERR, "scope error in default router "
4700*43a90889SApple OSS Distributions 				    "list (%s)\n", inet_ntop(AF_INET6,
4701*43a90889SApple OSS Distributions 				    &dr->rtaddr, pbuf, sizeof(pbuf)));
4702*43a90889SApple OSS Distributions 			}
4703*43a90889SApple OSS Distributions 			d.flags = dr->flags;
4704*43a90889SApple OSS Distributions 			d.stateflags = dr->stateflags;
4705*43a90889SApple OSS Distributions 			d.rtlifetime = (u_short)dr->rtlifetime;
4706*43a90889SApple OSS Distributions 			d.expire = (int)nddr_getexpire(dr);
4707*43a90889SApple OSS Distributions 			d.if_index = dr->ifp->if_index;
4708*43a90889SApple OSS Distributions 			error = SYSCTL_OUT(req, &d, sizeof(d));
4709*43a90889SApple OSS Distributions 			if (error != 0) {
4710*43a90889SApple OSS Distributions 				break;
4711*43a90889SApple OSS Distributions 			}
4712*43a90889SApple OSS Distributions 		}
4713*43a90889SApple OSS Distributions 	} else {
4714*43a90889SApple OSS Distributions 		struct in6_defrouter_32 d;
4715*43a90889SApple OSS Distributions 
4716*43a90889SApple OSS Distributions 		bzero(&d, sizeof(d));
4717*43a90889SApple OSS Distributions 		d.rtaddr.sin6_family = AF_INET6;
4718*43a90889SApple OSS Distributions 		d.rtaddr.sin6_len = sizeof(d.rtaddr);
4719*43a90889SApple OSS Distributions 
4720*43a90889SApple OSS Distributions 		TAILQ_FOREACH(dr, &nd_defrouter_list, dr_entry) {
4721*43a90889SApple OSS Distributions 			d.rtaddr.sin6_addr = dr->rtaddr;
4722*43a90889SApple OSS Distributions 			if (in6_recoverscope(&d.rtaddr,
4723*43a90889SApple OSS Distributions 			    &dr->rtaddr, dr->ifp) != 0) {
4724*43a90889SApple OSS Distributions 				log(LOG_ERR, "scope error in default router "
4725*43a90889SApple OSS Distributions 				    "list (%s)\n", inet_ntop(AF_INET6,
4726*43a90889SApple OSS Distributions 				    &dr->rtaddr, pbuf, sizeof(pbuf)));
4727*43a90889SApple OSS Distributions 			}
4728*43a90889SApple OSS Distributions 			d.flags = dr->flags;
4729*43a90889SApple OSS Distributions 			d.stateflags = dr->stateflags;
4730*43a90889SApple OSS Distributions 			d.rtlifetime = (u_short)dr->rtlifetime;
4731*43a90889SApple OSS Distributions 			d.expire = (int)nddr_getexpire(dr);
4732*43a90889SApple OSS Distributions 			d.if_index = dr->ifp->if_index;
4733*43a90889SApple OSS Distributions 			error = SYSCTL_OUT(req, &d, sizeof(d));
4734*43a90889SApple OSS Distributions 			if (error != 0) {
4735*43a90889SApple OSS Distributions 				break;
4736*43a90889SApple OSS Distributions 			}
4737*43a90889SApple OSS Distributions 		}
4738*43a90889SApple OSS Distributions 	}
4739*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
4740*43a90889SApple OSS Distributions 	return error;
4741*43a90889SApple OSS Distributions }
4742*43a90889SApple OSS Distributions 
4743*43a90889SApple OSS Distributions static int
4744*43a90889SApple OSS Distributions nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
4745*43a90889SApple OSS Distributions {
4746*43a90889SApple OSS Distributions #pragma unused(oidp, arg1, arg2)
4747*43a90889SApple OSS Distributions 	char pbuf[MAX_IPv6_STR_LEN];
4748*43a90889SApple OSS Distributions 	struct nd_pfxrouter *__single pfr;
4749*43a90889SApple OSS Distributions 	struct sockaddr_in6 s6;
4750*43a90889SApple OSS Distributions 	struct nd_prefix *__single pr;
4751*43a90889SApple OSS Distributions 	int error = 0;
4752*43a90889SApple OSS Distributions 
4753*43a90889SApple OSS Distributions 	if (req->newptr != USER_ADDR_NULL) {
4754*43a90889SApple OSS Distributions 		return EPERM;
4755*43a90889SApple OSS Distributions 	}
4756*43a90889SApple OSS Distributions 
4757*43a90889SApple OSS Distributions 	SOCKADDR_ZERO(&s6, sizeof(s6));
4758*43a90889SApple OSS Distributions 	s6.sin6_family = AF_INET6;
4759*43a90889SApple OSS Distributions 	s6.sin6_len = sizeof(s6);
4760*43a90889SApple OSS Distributions 
4761*43a90889SApple OSS Distributions 	/* XXX Handle mapped defrouter entries */
4762*43a90889SApple OSS Distributions 	lck_mtx_lock(nd6_mutex);
4763*43a90889SApple OSS Distributions 	if (proc_is64bit(req->p)) {
4764*43a90889SApple OSS Distributions 		struct in6_prefix_64 p;
4765*43a90889SApple OSS Distributions 
4766*43a90889SApple OSS Distributions 		bzero(&p, sizeof(p));
4767*43a90889SApple OSS Distributions 		p.origin = PR_ORIG_RA;
4768*43a90889SApple OSS Distributions 
4769*43a90889SApple OSS Distributions 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
4770*43a90889SApple OSS Distributions 			NDPR_LOCK(pr);
4771*43a90889SApple OSS Distributions 			p.prefix = pr->ndpr_prefix;
4772*43a90889SApple OSS Distributions 			if (in6_recoverscope(&p.prefix,
4773*43a90889SApple OSS Distributions 			    &pr->ndpr_prefix.sin6_addr, pr->ndpr_ifp) != 0) {
4774*43a90889SApple OSS Distributions 				log(LOG_ERR, "scope error in "
4775*43a90889SApple OSS Distributions 				    "prefix list (%s)\n", inet_ntop(AF_INET6,
4776*43a90889SApple OSS Distributions 				    &p.prefix.sin6_addr, pbuf, sizeof(pbuf)));
4777*43a90889SApple OSS Distributions 			}
4778*43a90889SApple OSS Distributions 			p.raflags = pr->ndpr_raf;
4779*43a90889SApple OSS Distributions 			p.prefixlen = pr->ndpr_plen;
4780*43a90889SApple OSS Distributions 			p.vltime = pr->ndpr_vltime;
4781*43a90889SApple OSS Distributions 			p.pltime = pr->ndpr_pltime;
4782*43a90889SApple OSS Distributions 			p.if_index = pr->ndpr_ifp->if_index;
4783*43a90889SApple OSS Distributions 			p.expire = (u_long)ndpr_getexpire(pr);
4784*43a90889SApple OSS Distributions 			p.refcnt = pr->ndpr_addrcnt;
4785*43a90889SApple OSS Distributions 			p.flags = pr->ndpr_stateflags;
4786*43a90889SApple OSS Distributions 			p.advrtrs = 0;
4787*43a90889SApple OSS Distributions 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
4788*43a90889SApple OSS Distributions 			p.advrtrs++;
4789*43a90889SApple OSS Distributions 			error = SYSCTL_OUT(req, &p, sizeof(p));
4790*43a90889SApple OSS Distributions 			if (error != 0) {
4791*43a90889SApple OSS Distributions 				NDPR_UNLOCK(pr);
4792*43a90889SApple OSS Distributions 				break;
4793*43a90889SApple OSS Distributions 			}
4794*43a90889SApple OSS Distributions 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
4795*43a90889SApple OSS Distributions 				s6.sin6_addr = pfr->router->rtaddr;
4796*43a90889SApple OSS Distributions 				if (in6_recoverscope(&s6, &pfr->router->rtaddr,
4797*43a90889SApple OSS Distributions 				    pfr->router->ifp) != 0) {
4798*43a90889SApple OSS Distributions 					log(LOG_ERR,
4799*43a90889SApple OSS Distributions 					    "scope error in prefix list (%s)\n",
4800*43a90889SApple OSS Distributions 					    inet_ntop(AF_INET6, &s6.sin6_addr,
4801*43a90889SApple OSS Distributions 					    pbuf, sizeof(pbuf)));
4802*43a90889SApple OSS Distributions 				}
4803*43a90889SApple OSS Distributions 				error = SYSCTL_OUT(req, &s6, sizeof(s6));
4804*43a90889SApple OSS Distributions 				if (error != 0) {
4805*43a90889SApple OSS Distributions 					break;
4806*43a90889SApple OSS Distributions 				}
4807*43a90889SApple OSS Distributions 			}
4808*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
4809*43a90889SApple OSS Distributions 			if (error != 0) {
4810*43a90889SApple OSS Distributions 				break;
4811*43a90889SApple OSS Distributions 			}
4812*43a90889SApple OSS Distributions 		}
4813*43a90889SApple OSS Distributions 	} else {
4814*43a90889SApple OSS Distributions 		struct in6_prefix_32 p;
4815*43a90889SApple OSS Distributions 
4816*43a90889SApple OSS Distributions 		bzero(&p, sizeof(p));
4817*43a90889SApple OSS Distributions 		p.origin = PR_ORIG_RA;
4818*43a90889SApple OSS Distributions 
4819*43a90889SApple OSS Distributions 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
4820*43a90889SApple OSS Distributions 			NDPR_LOCK(pr);
4821*43a90889SApple OSS Distributions 			p.prefix = pr->ndpr_prefix;
4822*43a90889SApple OSS Distributions 			if (in6_recoverscope(&p.prefix,
4823*43a90889SApple OSS Distributions 			    &pr->ndpr_prefix.sin6_addr, pr->ndpr_ifp) != 0) {
4824*43a90889SApple OSS Distributions 				log(LOG_ERR,
4825*43a90889SApple OSS Distributions 				    "scope error in prefix list (%s)\n",
4826*43a90889SApple OSS Distributions 				    inet_ntop(AF_INET6, &p.prefix.sin6_addr,
4827*43a90889SApple OSS Distributions 				    pbuf, sizeof(pbuf)));
4828*43a90889SApple OSS Distributions 			}
4829*43a90889SApple OSS Distributions 			p.raflags = pr->ndpr_raf;
4830*43a90889SApple OSS Distributions 			p.prefixlen = pr->ndpr_plen;
4831*43a90889SApple OSS Distributions 			p.vltime = pr->ndpr_vltime;
4832*43a90889SApple OSS Distributions 			p.pltime = pr->ndpr_pltime;
4833*43a90889SApple OSS Distributions 			p.if_index = pr->ndpr_ifp->if_index;
4834*43a90889SApple OSS Distributions 			p.expire = (u_int32_t)ndpr_getexpire(pr);
4835*43a90889SApple OSS Distributions 			p.refcnt = pr->ndpr_addrcnt;
4836*43a90889SApple OSS Distributions 			p.flags = pr->ndpr_stateflags;
4837*43a90889SApple OSS Distributions 			p.advrtrs = 0;
4838*43a90889SApple OSS Distributions 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
4839*43a90889SApple OSS Distributions 			p.advrtrs++;
4840*43a90889SApple OSS Distributions 			error = SYSCTL_OUT(req, &p, sizeof(p));
4841*43a90889SApple OSS Distributions 			if (error != 0) {
4842*43a90889SApple OSS Distributions 				NDPR_UNLOCK(pr);
4843*43a90889SApple OSS Distributions 				break;
4844*43a90889SApple OSS Distributions 			}
4845*43a90889SApple OSS Distributions 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
4846*43a90889SApple OSS Distributions 				s6.sin6_addr = pfr->router->rtaddr;
4847*43a90889SApple OSS Distributions 				if (in6_recoverscope(&s6, &pfr->router->rtaddr,
4848*43a90889SApple OSS Distributions 				    pfr->router->ifp) != 0) {
4849*43a90889SApple OSS Distributions 					log(LOG_ERR,
4850*43a90889SApple OSS Distributions 					    "scope error in prefix list (%s)\n",
4851*43a90889SApple OSS Distributions 					    inet_ntop(AF_INET6, &s6.sin6_addr,
4852*43a90889SApple OSS Distributions 					    pbuf, sizeof(pbuf)));
4853*43a90889SApple OSS Distributions 				}
4854*43a90889SApple OSS Distributions 				error = SYSCTL_OUT(req, &s6, sizeof(s6));
4855*43a90889SApple OSS Distributions 				if (error != 0) {
4856*43a90889SApple OSS Distributions 					break;
4857*43a90889SApple OSS Distributions 				}
4858*43a90889SApple OSS Distributions 			}
4859*43a90889SApple OSS Distributions 			NDPR_UNLOCK(pr);
4860*43a90889SApple OSS Distributions 			if (error != 0) {
4861*43a90889SApple OSS Distributions 				break;
4862*43a90889SApple OSS Distributions 			}
4863*43a90889SApple OSS Distributions 		}
4864*43a90889SApple OSS Distributions 	}
4865*43a90889SApple OSS Distributions 	lck_mtx_unlock(nd6_mutex);
4866*43a90889SApple OSS Distributions 
4867*43a90889SApple OSS Distributions 	return error;
4868*43a90889SApple OSS Distributions }
4869*43a90889SApple OSS Distributions 
4870*43a90889SApple OSS Distributions void
in6_ifaddr_set_dadprogress(struct in6_ifaddr * ia)4871*43a90889SApple OSS Distributions in6_ifaddr_set_dadprogress(struct in6_ifaddr *ia)
4872*43a90889SApple OSS Distributions {
4873*43a90889SApple OSS Distributions 	ifnet_ref_t ifp = ia->ia_ifp;
4874*43a90889SApple OSS Distributions 	uint32_t flags = IN6_IFF_TENTATIVE;
4875*43a90889SApple OSS Distributions 	uint32_t optdad = nd6_optimistic_dad;
4876*43a90889SApple OSS Distributions 	struct nd_ifinfo *__single ndi = NULL;
4877*43a90889SApple OSS Distributions 
4878*43a90889SApple OSS Distributions 	ndi = ND_IFINFO(ifp);
4879*43a90889SApple OSS Distributions 	VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
4880*43a90889SApple OSS Distributions 	if (!(ndi->flags & ND6_IFF_DAD)) {
4881*43a90889SApple OSS Distributions 		return;
4882*43a90889SApple OSS Distributions 	}
4883*43a90889SApple OSS Distributions 
4884*43a90889SApple OSS Distributions 	if (optdad) {
4885*43a90889SApple OSS Distributions 		if (ifp->if_ipv6_router_mode == IPV6_ROUTER_MODE_EXCLUSIVE) {
4886*43a90889SApple OSS Distributions 			optdad = 0;
4887*43a90889SApple OSS Distributions 		} else {
4888*43a90889SApple OSS Distributions 			lck_mtx_lock(&ndi->lock);
4889*43a90889SApple OSS Distributions 			if ((ndi->flags & ND6_IFF_REPLICATED) != 0) {
4890*43a90889SApple OSS Distributions 				optdad = 0;
4891*43a90889SApple OSS Distributions 			}
4892*43a90889SApple OSS Distributions 			lck_mtx_unlock(&ndi->lock);
4893*43a90889SApple OSS Distributions 		}
4894*43a90889SApple OSS Distributions 	}
4895*43a90889SApple OSS Distributions 
4896*43a90889SApple OSS Distributions 	if (optdad) {
4897*43a90889SApple OSS Distributions 		if ((optdad & ND6_OPTIMISTIC_DAD_LINKLOCAL) &&
4898*43a90889SApple OSS Distributions 		    IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
4899*43a90889SApple OSS Distributions 			flags = IN6_IFF_OPTIMISTIC;
4900*43a90889SApple OSS Distributions 		} else if ((optdad & ND6_OPTIMISTIC_DAD_AUTOCONF) &&
4901*43a90889SApple OSS Distributions 		    (ia->ia6_flags & IN6_IFF_AUTOCONF)) {
4902*43a90889SApple OSS Distributions 			if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
4903*43a90889SApple OSS Distributions 				if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) {
4904*43a90889SApple OSS Distributions 					flags = IN6_IFF_OPTIMISTIC;
4905*43a90889SApple OSS Distributions 				}
4906*43a90889SApple OSS Distributions 			} else if (ia->ia6_flags & IN6_IFF_SECURED) {
4907*43a90889SApple OSS Distributions 				if (optdad & ND6_OPTIMISTIC_DAD_SECURED) {
4908*43a90889SApple OSS Distributions 					flags = IN6_IFF_OPTIMISTIC;
4909*43a90889SApple OSS Distributions 				}
4910*43a90889SApple OSS Distributions 			} else {
4911*43a90889SApple OSS Distributions 				/*
4912*43a90889SApple OSS Distributions 				 * Keeping the behavior for temp and CGA
4913*43a90889SApple OSS Distributions 				 * SLAAC addresses to have a knob for optimistic
4914*43a90889SApple OSS Distributions 				 * DAD.
4915*43a90889SApple OSS Distributions 				 * Other than that if ND6_OPTIMISTIC_DAD_AUTOCONF
4916*43a90889SApple OSS Distributions 				 * is set, we should default to optimistic
4917*43a90889SApple OSS Distributions 				 * DAD.
4918*43a90889SApple OSS Distributions 				 * For now this means SLAAC addresses with interface
4919*43a90889SApple OSS Distributions 				 * identifier derived from modified EUI-64 bit
4920*43a90889SApple OSS Distributions 				 * identifiers.
4921*43a90889SApple OSS Distributions 				 */
4922*43a90889SApple OSS Distributions 				flags = IN6_IFF_OPTIMISTIC;
4923*43a90889SApple OSS Distributions 			}
4924*43a90889SApple OSS Distributions 		} else if ((optdad & ND6_OPTIMISTIC_DAD_DYNAMIC) &&
4925*43a90889SApple OSS Distributions 		    (ia->ia6_flags & IN6_IFF_DYNAMIC)) {
4926*43a90889SApple OSS Distributions 			if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
4927*43a90889SApple OSS Distributions 				if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) {
4928*43a90889SApple OSS Distributions 					flags = IN6_IFF_OPTIMISTIC;
4929*43a90889SApple OSS Distributions 				}
4930*43a90889SApple OSS Distributions 			} else {
4931*43a90889SApple OSS Distributions 				flags = IN6_IFF_OPTIMISTIC;
4932*43a90889SApple OSS Distributions 			}
4933*43a90889SApple OSS Distributions 		} else if ((optdad & ND6_OPTIMISTIC_DAD_MANUAL) &&
4934*43a90889SApple OSS Distributions 		    (ia->ia6_flags & IN6_IFF_OPTIMISTIC)) {
4935*43a90889SApple OSS Distributions 			/*
4936*43a90889SApple OSS Distributions 			 * rdar://17483438
4937*43a90889SApple OSS Distributions 			 * Bypass tentative for address assignments
4938*43a90889SApple OSS Distributions 			 * not covered above (e.g. manual) upon request
4939*43a90889SApple OSS Distributions 			 */
4940*43a90889SApple OSS Distributions 			if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) &&
4941*43a90889SApple OSS Distributions 			    !(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
4942*43a90889SApple OSS Distributions 			    !(ia->ia6_flags & IN6_IFF_DYNAMIC)) {
4943*43a90889SApple OSS Distributions 				flags = IN6_IFF_OPTIMISTIC;
4944*43a90889SApple OSS Distributions 			}
4945*43a90889SApple OSS Distributions 		}
4946*43a90889SApple OSS Distributions 	}
4947*43a90889SApple OSS Distributions 
4948*43a90889SApple OSS Distributions 	ia->ia6_flags &= ~(IN6_IFF_DUPLICATED | IN6_IFF_DADPROGRESS);
4949*43a90889SApple OSS Distributions 	ia->ia6_flags |= flags;
4950*43a90889SApple OSS Distributions 
4951*43a90889SApple OSS Distributions 	nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
4952*43a90889SApple OSS Distributions 	    __func__,
4953*43a90889SApple OSS Distributions 	    ip6_sprintf(&ia->ia_addr.sin6_addr),
4954*43a90889SApple OSS Distributions 	    if_name(ia->ia_ifp),
4955*43a90889SApple OSS Distributions 	    ia->ia6_flags);
4956*43a90889SApple OSS Distributions }
4957