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