]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/nd6.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / nd6.c
1 /*
2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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>
74 #include <sys/sysctl.h>
75 #include <sys/errno.h>
76 #include <sys/syslog.h>
77 #include <sys/protosw.h>
78 #include <sys/proc.h>
79 #include <sys/mcache.h>
80
81 #include <dev/random/randomdev.h>
82
83 #include <kern/queue.h>
84 #include <kern/zalloc.h>
85
86 #include <net/if.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
89 #include <net/if_llreach.h>
90 #include <net/route.h>
91 #include <net/dlil.h>
92 #include <net/ntstat.h>
93 #include <net/net_osdep.h>
94 #include <net/nwk_wq.h>
95
96 #include <netinet/in.h>
97 #include <netinet/in_arp.h>
98 #include <netinet/if_ether.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet/icmp6.h>
105
106 #include <os/log.h>
107
108 #include "loop.h"
109
110 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
111 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
112
113 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
114
115 /* timer values */
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 */
123
124 /* preventing too many loops in ND option parsing */
125 int nd6_maxndopt = 10; /* max # of ND options allowed */
126
127 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
128
129 #if ND6_DEBUG
130 int nd6_debug = 1;
131 #else
132 int nd6_debug = 0;
133 #endif
134
135 int nd6_optimistic_dad =
136 (ND6_OPTIMISTIC_DAD_LINKLOCAL | ND6_OPTIMISTIC_DAD_AUTOCONF |
137 ND6_OPTIMISTIC_DAD_TEMPORARY | ND6_OPTIMISTIC_DAD_DYNAMIC |
138 ND6_OPTIMISTIC_DAD_SECURED | ND6_OPTIMISTIC_DAD_MANUAL);
139
140 /* for debugging? */
141 static int nd6_inuse, nd6_allocated;
142
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 *
159 * ln_hold, ln_asked, ln_expire, ln_state, ln_router, ln_flags,
160 * ln_llreach, ln_lastused
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 = {
170 .ln_next = &llinfo_nd6,
171 .ln_prev = &llinfo_nd6,
172 };
173
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;
177
178 /* Protected by nd6_mutex */
179 struct nd_drhead nd_defrouter;
180 struct nd_prhead nd_prefix = { .lh_first = 0 };
181
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 */
195 static int nd6_timeout_run; /* nd6_timeout is scheduled to run */
196 static void nd6_timeout(void *);
197 int nd6_sched_timeout_want; /* demand count for timer to be sched */
198 static boolean_t nd6_fast_timer_on = FALSE;
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;
204
205 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
206 static struct sockaddr_in6 all1_sa;
207
208 static int regen_tmpaddr(struct in6_ifaddr *);
209 extern lck_mtx_t *nd6_mutex;
210
211 static struct llinfo_nd6 *nd6_llinfo_alloc(int);
212 static void nd6_llinfo_free(void *);
213 static void nd6_llinfo_purge(struct rtentry *);
214 static void nd6_llinfo_get_ri(struct rtentry *, struct rt_reach_info *);
215 static void nd6_llinfo_get_iflri(struct rtentry *, struct ifnet_llreach_info *);
216 static void nd6_llinfo_refresh(struct rtentry *);
217 static uint64_t ln_getexpire(struct llinfo_nd6 *);
218
219 static void nd6_service(void *);
220 static void nd6_slowtimo(void *);
221 static int nd6_is_new_addr_neighbor(struct sockaddr_in6 *, struct ifnet *);
222 static int nd6_siocgdrlst(void *, int);
223 static int nd6_siocgprlst(void *, int);
224
225 static int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS;
226 static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS;
227
228 /*
229 * Insertion and removal from llinfo_nd6 must be done with rnh_lock held.
230 */
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; \
238 } while (0)
239
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; \
248 } while (0)
249
250 static struct zone *llinfo_nd6_zone;
251 #define LLINFO_ND6_ZONE_MAX 256 /* maximum elements in zone */
252 #define LLINFO_ND6_ZONE_NAME "llinfo_nd6" /* name for zone */
253
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,
261 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
262 nd6_sysctl_drlist, "S,in6_defrouter", "");
263
264 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
265 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
266 nd6_sysctl_prlist, "S,in6_defrouter", "");
267
268 SYSCTL_DECL(_net_inet6_ip6);
269
270 static int ip6_maxchainsent = 0;
271 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, maxchainsent,
272 CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxchainsent, 0,
273 "use dlil_output_list");
274
275 void
276 nd6_init(void)
277 {
278 int i;
279
280 VERIFY(!nd6_init_done);
281
282 all1_sa.sin6_family = AF_INET6;
283 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
284 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++) {
285 all1_sa.sin6_addr.s6_addr[i] = 0xff;
286 }
287
288 /* initialization of the default router list */
289 TAILQ_INIT(&nd_defrouter);
290
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();
294
295 llinfo_nd6_zone = zinit(sizeof(struct llinfo_nd6),
296 LLINFO_ND6_ZONE_MAX * sizeof(struct llinfo_nd6), 0,
297 LLINFO_ND6_ZONE_NAME);
298 if (llinfo_nd6_zone == NULL) {
299 panic("%s: failed allocating llinfo_nd6_zone", __func__);
300 }
301
302 zone_change(llinfo_nd6_zone, Z_EXPAND, TRUE);
303 zone_change(llinfo_nd6_zone, Z_CALLERACCT, FALSE);
304
305 nd6_nbr_init();
306 nd6_rtr_init();
307 nd6_prproxy_init();
308
309 nd6_init_done = 1;
310
311 /* start timer */
312 timeout(nd6_slowtimo, NULL, ND6_SLOWTIMER_INTERVAL * hz);
313 }
314
315 static struct llinfo_nd6 *
316 nd6_llinfo_alloc(int how)
317 {
318 struct llinfo_nd6 *ln;
319
320 ln = (how == M_WAITOK) ? zalloc(llinfo_nd6_zone) :
321 zalloc_noblock(llinfo_nd6_zone);
322 if (ln != NULL) {
323 bzero(ln, sizeof(*ln));
324 }
325
326 return ln;
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) {
341 m_freem_list(ln->ln_hold);
342 ln->ln_hold = NULL;
343 }
344
345 /* Purge any link-layer info caching */
346 VERIFY(ln->ln_rt->rt_llinfo == ln);
347 if (ln->ln_rt->rt_llinfo_purge != NULL) {
348 ln->ln_rt->rt_llinfo_purge(ln->ln_rt);
349 }
350
351 zfree(llinfo_nd6_zone, ln);
352 }
353
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) {
377 bzero(ri, sizeof(*ri));
378 ri->ri_rssi = IFNET_RSSI_UNKNOWN;
379 ri->ri_lqm = IFNET_LQM_THRESH_OFF;
380 ri->ri_npm = IFNET_NPM_THRESH_UNKNOWN;
381 } else {
382 IFLR_LOCK(lr);
383 /* Export to rt_reach_info structure */
384 ifnet_lr2ri(lr, ri);
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) {
399 bzero(iflri, sizeof(*iflri));
400 iflri->iflri_rssi = IFNET_RSSI_UNKNOWN;
401 iflri->iflri_lqm = IFNET_LQM_THRESH_OFF;
402 iflri->iflri_npm = IFNET_NPM_THRESH_UNKNOWN;
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);
410 IFLR_UNLOCK(lr);
411 }
412 }
413
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) {
432 ln_setexpire(ln, timenow);
433 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_PROBE);
434 }
435 }
436 return;
437 }
438
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 }
469 return ndp_state_str;
470 }
471
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 }
500 return expiry;
501 }
502
503 void
504 nd6_ifreset(struct ifnet *ifp)
505 {
506 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
507 VERIFY(NULL != ndi);
508 VERIFY(ndi->initialized);
509
510 LCK_MTX_ASSERT(&ndi->lock, LCK_MTX_ASSERT_OWNED);
511 ndi->linkmtu = ifp->if_mtu;
512 ndi->chlim = IPV6_DEFHLIM;
513 ndi->basereachable = REACHABLE_TIME;
514 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
515 ndi->retrans = RETRANS_TIMER;
516 }
517
518 void
519 nd6_ifattach(struct ifnet *ifp)
520 {
521 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
522
523 VERIFY(NULL != ndi);
524 if (!ndi->initialized) {
525 lck_mtx_init(&ndi->lock, nd_if_lock_grp, nd_if_lock_attr);
526 ndi->flags = ND6_IFF_PERFORMNUD;
527 ndi->flags |= ND6_IFF_DAD;
528 ndi->initialized = TRUE;
529 }
530
531 lck_mtx_lock(&ndi->lock);
532
533 if (!(ifp->if_flags & IFF_MULTICAST)) {
534 ndi->flags |= ND6_IFF_IFDISABLED;
535 }
536
537 nd6_ifreset(ifp);
538 lck_mtx_unlock(&ndi->lock);
539 nd6_setmtu(ifp);
540
541 nd6log0(info,
542 "Reinit'd ND information for interface %s\n",
543 if_name(ifp));
544 return;
545 }
546
547 #if 0
548 /*
549 * XXX Look more into this. Especially since we recycle ifnets and do delayed
550 * cleanup
551 */
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
560 void
561 nd6_setmtu(struct ifnet *ifp)
562 {
563 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
564 u_int32_t oldmaxmtu, maxmtu;
565
566 if ((NULL == ndi) || (FALSE == ndi->initialized)) {
567 return;
568 }
569
570 lck_mtx_lock(&ndi->lock);
571 oldmaxmtu = ndi->maxmtu;
572
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
581 * ND level maxmtu and linkmtu are done via IN6_LINKMTU() which
582 * does further checking against if_mtu.
583 */
584 maxmtu = ndi->maxmtu = ifp->if_mtu;
585
586 /*
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 */
592 if (oldmaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
593 log(LOG_NOTICE, "nd6_setmtu: "
594 "new link MTU on %s (%u) is too small for IPv6\n",
595 if_name(ifp), (uint32_t)ndi->maxmtu);
596 }
597 ndi->linkmtu = ifp->if_mtu;
598 lck_mtx_unlock(&ndi->lock);
599
600 /* also adjust in6_maxmtu if necessary. */
601 if (maxmtu > in6_maxmtu) {
602 in6_setmaxmtu();
603 }
604 }
605
606 void
607 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
608 {
609 bzero(ndopts, sizeof(*ndopts));
610 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
611 ndopts->nd_opts_last =
612 (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
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 *
624 nd6_option(union nd_opts *ndopts)
625 {
626 struct nd_opt_hdr *nd_opt;
627 int olen;
628
629 if (!ndopts) {
630 panic("ndopts == NULL in nd6_option\n");
631 }
632 if (!ndopts->nd_opts_last) {
633 panic("uninitialized ndopts in nd6_option\n");
634 }
635 if (!ndopts->nd_opts_search) {
636 return NULL;
637 }
638 if (ndopts->nd_opts_done) {
639 return NULL;
640 }
641
642 nd_opt = ndopts->nd_opts_search;
643
644 /* make sure nd_opt_len is inside the buffer */
645 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
646 bzero(ndopts, sizeof(*ndopts));
647 return NULL;
648 }
649
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 */
656 bzero(ndopts, sizeof(*ndopts));
657 return NULL;
658 }
659
660 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
661 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
662 /* option overruns the end of buffer, invalid */
663 bzero(ndopts, sizeof(*ndopts));
664 return NULL;
665 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
666 /* reached the end of options chain */
667 ndopts->nd_opts_done = 1;
668 ndopts->nd_opts_search = NULL;
669 }
670 return nd_opt;
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
679 nd6_options(union nd_opts *ndopts)
680 {
681 struct nd_opt_hdr *nd_opt;
682 int i = 0;
683
684 if (ndopts == NULL) {
685 panic("ndopts == NULL in nd6_options");
686 }
687 if (ndopts->nd_opts_last == NULL) {
688 panic("uninitialized ndopts in nd6_options");
689 }
690 if (ndopts->nd_opts_search == NULL) {
691 return 0;
692 }
693
694 while (1) {
695 nd_opt = nd6_option(ndopts);
696 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
697 /*
698 * Message validation requires that all included
699 * options have a length that is greater than zero.
700 */
701 icmp6stat.icp6s_nd_badopt++;
702 bzero(ndopts, sizeof(*ndopts));
703 return -1;
704 }
705
706 if (nd_opt == NULL) {
707 goto skip1;
708 }
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:
715 case ND_OPT_NONCE:
716 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
717 nd6log(error,
718 "duplicated ND6 option found (type=%d)\n",
719 nd_opt->nd_opt_type);
720 /* XXX bark? */
721 } else {
722 ndopts->nd_opt_array[nd_opt->nd_opt_type] =
723 nd_opt;
724 }
725 break;
726 case ND_OPT_PREFIX_INFORMATION:
727 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
728 ndopts->nd_opt_array[nd_opt->nd_opt_type] =
729 nd_opt;
730 }
731 ndopts->nd_opts_pi_end =
732 (struct nd_opt_prefix_info *)nd_opt;
733 break;
734 case ND_OPT_RDNSS:
735 case ND_OPT_DNSSL:
736 /* ignore */
737 break;
738 default:
739 /*
740 * Unknown options must be silently ignored,
741 * to accomodate future extension to the protocol.
742 */
743 nd6log(debug,
744 "nd6_options: unsupported option %d - "
745 "option ignored\n", nd_opt->nd_opt_type);
746 }
747
748 skip1:
749 i++;
750 if (i > nd6_maxndopt) {
751 icmp6stat.icp6s_nd_toomanyopt++;
752 nd6log(info, "too many loop in nd opt\n");
753 break;
754 }
755
756 if (ndopts->nd_opts_done) {
757 break;
758 }
759 }
760
761 return 0;
762 }
763
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)
778 {
779 struct nd6svc_arg *ap = arg;
780 struct llinfo_nd6 *ln;
781 struct nd_defrouter *dr = NULL;
782 struct nd_prefix *pr = NULL;
783 struct ifnet *ifp = NULL;
784 struct in6_ifaddr *ia6, *nia6;
785 uint64_t timenow;
786 boolean_t send_nc_failure_kev = FALSE;
787 struct nd_drhead nd_defrouter_tmp;
788 struct nd_defrouter *ndr = NULL;
789 struct radix_node_head *rnh = rt_tables[AF_INET6];
790
791 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
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) {
797 nd6log2(debug, "%s: %s is blocked by %d waiters\n",
798 __func__, ap->draining ? "drainer" : "timer",
799 nd6_service_waiters);
800 nd6_service_waiters++;
801 (void) msleep(nd6_service_wc, rnh_lock, (PZERO - 1),
802 __func__, NULL);
803 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
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();
811 again:
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 */
833 if (send_nc_failure_kev && ifp != NULL &&
834 ifp->if_addrlen == IF_LLREACH_MAXLEN) {
835 struct kev_msg ev_msg;
836 struct kev_nd6_ndfailure nd6_ndfailure;
837 bzero(&ev_msg, sizeof(ev_msg));
838 bzero(&nd6_ndfailure, sizeof(nd6_ndfailure));
839 ev_msg.vendor_code = KEV_VENDOR_APPLE;
840 ev_msg.kev_class = KEV_NETWORK_CLASS;
841 ev_msg.kev_subclass = KEV_ND6_SUBCLASS;
842 ev_msg.event_code = KEV_ND6_NDFAILURE;
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 =
851 sizeof(nd6_ndfailure);
852 dlil_post_complete_msg(NULL, &ev_msg);
853 }
854
855 send_nc_failure_kev = FALSE;
856 ifp = NULL;
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 */
869 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
870
871 ln = llinfo_nd6.ln_next;
872 while (ln != NULL && ln != &llinfo_nd6) {
873 struct rtentry *rt;
874 struct sockaddr_in6 *dst;
875 struct llinfo_nd6 *next;
876 u_int32_t retrans, flags;
877 struct nd_ifinfo *ndi = NULL;
878 boolean_t is_router = FALSE;
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);
884
885 /* We've seen this already; skip it */
886 if (ln->ln_flags & ND6_LNF_TIMER_SKIP) {
887 RT_UNLOCK(rt);
888 ln = next;
889 continue;
890 }
891 ap->found++;
892
893 /* rt->rt_ifp should never be NULL */
894 if ((ifp = rt->rt_ifp) == NULL) {
895 panic("%s: ln(%p) rt(%p) rt_ifp == NULL", __func__,
896 ln, rt);
897 /* NOTREACHED */
898 }
899
900 /* rt_llinfo must always be equal to ln */
901 if ((struct llinfo_nd6 *)rt->rt_llinfo != ln) {
902 panic("%s: rt_llinfo(%p) is not equal to ln(%p)",
903 __func__, rt->rt_llinfo, ln);
904 /* NOTREACHED */
905 }
906
907 /* rt_key should never be NULL */
908 dst = SIN6(rt_key(rt));
909 if (dst == NULL) {
910 panic("%s: rt(%p) key is NULL ln(%p)", __func__,
911 rt, ln);
912 /* NOTREACHED */
913 }
914
915 /* Set the flag in case we jump to "again" */
916 ln->ln_flags |= ND6_LNF_TIMER_SKIP;
917
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 */
925 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
926 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
927 } else {
928 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_PURGE);
929 }
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 }
948 RT_UNLOCK(rt);
949 ln = next;
950 continue;
951 }
952
953 ndi = ND_IFINFO(ifp);
954 VERIFY(ndi->initialized);
955 retrans = ndi->retrans;
956 flags = ndi->flags;
957
958 RT_LOCK_ASSERT_HELD(rt);
959 is_router = (rt->rt_flags & RTF_ROUTER) ? TRUE : FALSE;
960
961 switch (ln->ln_state) {
962 case ND6_LLINFO_INCOMPLETE:
963 if (ln->ln_asked < nd6_mmaxtries) {
964 struct ifnet *exclifp = ln->ln_exclifp;
965 ln->ln_asked++;
966 ln_setexpire(ln, timenow + retrans / 1000);
967 RT_ADDREF_LOCKED(rt);
968 RT_UNLOCK(rt);
969 lck_mtx_unlock(rnh_lock);
970 if (ip6_forwarding) {
971 nd6_prproxy_ns_output(ifp, exclifp,
972 NULL, &dst->sin6_addr, ln);
973 } else {
974 nd6_ns_output(ifp, NULL,
975 &dst->sin6_addr, ln, NULL);
976 }
977 RT_REMREF(rt);
978 ap->aging++;
979 lck_mtx_lock(rnh_lock);
980 } else {
981 struct mbuf *m = ln->ln_hold;
982 ln->ln_hold = NULL;
983 send_nc_failure_kev = is_router;
984 if (m != NULL) {
985 RT_ADDREF_LOCKED(rt);
986 RT_UNLOCK(rt);
987 lck_mtx_unlock(rnh_lock);
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 }
998 } else {
999 RT_ADDREF_LOCKED(rt);
1000 RT_UNLOCK(rt);
1001 lck_mtx_unlock(rnh_lock);
1002 }
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);
1010 nd6_free(rt);
1011 ap->killed++;
1012 lck_mtx_lock(rnh_lock);
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 }
1028 rtfree_locked(rt);
1029 }
1030 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1031 goto again;
1032
1033 case ND6_LLINFO_REACHABLE:
1034 if (ln->ln_expire != 0) {
1035 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
1036 ln_setexpire(ln, timenow + nd6_gctimer);
1037 ap->aging_lazy++;
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);
1055 }
1056 break;
1057
1058 case ND6_LLINFO_STALE:
1059 case ND6_LLINFO_PURGE:
1060 /* Garbage Collection(RFC 4861 5.3) */
1061 if (ln->ln_expire != 0) {
1062 RT_ADDREF_LOCKED(rt);
1063 RT_UNLOCK(rt);
1064 lck_mtx_unlock(rnh_lock);
1065 nd6_free(rt);
1066 ap->killed++;
1067 lck_mtx_lock(rnh_lock);
1068 rtfree_locked(rt);
1069 goto again;
1070 } else {
1071 RT_UNLOCK(rt);
1072 }
1073 break;
1074
1075 case ND6_LLINFO_DELAY:
1076 if ((flags & ND6_IFF_PERFORMNUD) != 0) {
1077 /* We need NUD */
1078 ln->ln_asked = 1;
1079 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_PROBE);
1080 ln_setexpire(ln, timenow + retrans / 1000);
1081 RT_ADDREF_LOCKED(rt);
1082 RT_UNLOCK(rt);
1083 lck_mtx_unlock(rnh_lock);
1084 nd6_ns_output(ifp, &dst->sin6_addr,
1085 &dst->sin6_addr, ln, NULL);
1086 RT_REMREF(rt);
1087 ap->aging++;
1088 lck_mtx_lock(rnh_lock);
1089 goto again;
1090 }
1091 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE); /* XXX */
1092 ln_setexpire(ln, timenow + nd6_gctimer);
1093 RT_UNLOCK(rt);
1094 ap->aging_lazy++;
1095 break;
1096
1097 case ND6_LLINFO_PROBE:
1098 if (ln->ln_asked < nd6_umaxtries) {
1099 ln->ln_asked++;
1100 ln_setexpire(ln, timenow + retrans / 1000);
1101 RT_ADDREF_LOCKED(rt);
1102 RT_UNLOCK(rt);
1103 lck_mtx_unlock(rnh_lock);
1104 nd6_ns_output(ifp, &dst->sin6_addr,
1105 &dst->sin6_addr, ln, NULL);
1106 RT_REMREF(rt);
1107 ap->aging++;
1108 lck_mtx_lock(rnh_lock);
1109 } else {
1110 is_router = (rt->rt_flags & RTF_ROUTER) ? TRUE : FALSE;
1111 send_nc_failure_kev = is_router;
1112 RT_ADDREF_LOCKED(rt);
1113 RT_UNLOCK(rt);
1114 lck_mtx_unlock(rnh_lock);
1115 nd6_free(rt);
1116 ap->killed++;
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
1125 lck_mtx_lock(rnh_lock);
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 }
1142 rtfree_locked(rt);
1143 }
1144 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1145 goto again;
1146
1147 default:
1148 RT_UNLOCK(rt);
1149 break;
1150 }
1151 ln = next;
1152 }
1153 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
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);
1162 if (ln->ln_flags & ND6_LNF_TIMER_SKIP) {
1163 ln->ln_flags &= ~ND6_LNF_TIMER_SKIP;
1164 }
1165 RT_UNLOCK(rt);
1166 ln = next;
1167 }
1168 lck_mtx_unlock(rnh_lock);
1169
1170 /* expire default router list */
1171 TAILQ_INIT(&nd_defrouter_tmp);
1172
1173 lck_mtx_lock(nd6_mutex);
1174 TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
1175 ap->found++;
1176 if (dr->expire != 0 && dr->expire < timenow) {
1177 VERIFY(dr->ifp != NULL);
1178 in6_ifstat_inc(dr->ifp, ifs6_defrtr_expiry_cnt);
1179 in6_event_enqueue_nwk_wq_entry(IN6_NDP_RTR_EXPIRY, dr->ifp,
1180 &dr->rtaddr, dr->rtlifetime);
1181 if (dr->ifp != NULL &&
1182 dr->ifp->if_type == IFT_CELLULAR) {
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;
1206 nd6log2(debug,
1207 "%s: Refreshing expired default router entry "
1208 "%s for interface %s\n", __func__,
1209 ip6_sprintf(&dr->rtaddr), if_name(dr->ifp));
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 }
1224 } else {
1225 if (dr->expire == 0 || (dr->stateflags & NDDRF_STATIC)) {
1226 ap->sticky++;
1227 } else {
1228 ap->aging_lazy++;
1229 }
1230 }
1231 }
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 }
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 }
1260 lck_mtx_unlock(nd6_mutex);
1261
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 */
1268 addrloop:
1269 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1270
1271 TAILQ_FOREACH_SAFE(ia6, &in6_ifaddrhead, ia6_link, nia6) {
1272 int oldflags = ia6->ia6_flags;
1273 ap->found++;
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);
1282 /* check address lifetime */
1283 if (IFA6_IS_INVALID(ia6, timenow)) {
1284 /*
1285 * If the expiring address is temporary, try
1286 * regenerating a new one. This would be useful when
1287 * we suspended a laptop PC, then turned it on after a
1288 * period that could invalidate all temporary
1289 * addresses. Although we may have to restart the
1290 * loop (see below), it must be after purging the
1291 * address. Otherwise, we'd see an infinite loop of
1292 * regeneration.
1293 */
1294 if (ip6_use_tempaddr &&
1295 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
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
1303 */
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);
1310 }
1311
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);
1319 ap->killed++;
1320
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 }
1327 /* Release extra reference taken above */
1328 IFA_REMREF(&ia6->ia_ifa);
1329 goto addrloop;
1330 }
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 */
1337 if (IFA6_IS_INVALID(ia6, timenow + 3 * nd6_prune_lazy)) {
1338 ap->aging++;
1339 } else {
1340 ap->aging_lazy++;
1341 }
1342 IFA_LOCK_ASSERT_HELD(&ia6->ia_ifa);
1343 if (IFA6_IS_DEPRECATED(ia6, timenow)) {
1344 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
1345
1346 if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
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 }
1359 }
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) {
1367 /* see NOTE above */
1368 IFA_UNLOCK(&ia6->ia_ifa);
1369 lck_rw_done(&in6_ifaddr_rwlock);
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
1378 * it's not a deletion, but an
1379 * addition,) we'd rather restart the
1380 * loop just for safety. Or does this
1381 * significantly reduce performance??
1382 */
1383 /* Release extra reference */
1384 IFA_REMREF(&ia6->ia_ifa);
1385 goto addrloop;
1386 }
1387 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1388 } else {
1389 IFA_UNLOCK(&ia6->ia_ifa);
1390 }
1391 } else {
1392 /*
1393 * A new RA might have made a deprecated address
1394 * preferred.
1395 */
1396 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
1397 IFA_UNLOCK(&ia6->ia_ifa);
1398 }
1399 LCK_RW_ASSERT(&in6_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
1400 /* Release extra reference taken above */
1401 IFA_REMREF(&ia6->ia_ifa);
1402 }
1403 lck_rw_done(&in6_ifaddr_rwlock);
1404
1405 lck_mtx_lock(nd6_mutex);
1406 /* expire prefix list */
1407 pr = nd_prefix.lh_first;
1408 while (pr != NULL) {
1409 ap->found++;
1410 /*
1411 * check prefix lifetime.
1412 * since pltime is just for autoconf, pltime processing for
1413 * prefix is not necessary.
1414 */
1415 NDPR_LOCK(pr);
1416 if (pr->ndpr_stateflags & NDPRF_PROCESSED_SERVICE ||
1417 pr->ndpr_stateflags & NDPRF_DEFUNCT) {
1418 pr->ndpr_stateflags |= NDPRF_PROCESSED_SERVICE;
1419 NDPR_UNLOCK(pr);
1420 pr = pr->ndpr_next;
1421 continue;
1422 }
1423 if (pr->ndpr_expire != 0 && pr->ndpr_expire < timenow) {
1424 /*
1425 * address expiration and prefix expiration are
1426 * separate. NEVER perform in6_purgeaddr here.
1427 */
1428 pr->ndpr_stateflags |= NDPRF_PROCESSED_SERVICE;
1429 NDPR_ADDREF_LOCKED(pr);
1430 prelist_remove(pr);
1431 NDPR_UNLOCK(pr);
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,
1436 0);
1437 NDPR_REMREF(pr);
1438 pfxlist_onlink_check();
1439 pr = nd_prefix.lh_first;
1440 ap->killed++;
1441 } else {
1442 if (pr->ndpr_expire == 0 ||
1443 (pr->ndpr_stateflags & NDPRF_STATIC)) {
1444 ap->sticky++;
1445 } else {
1446 ap->aging_lazy++;
1447 }
1448 pr->ndpr_stateflags |= NDPRF_PROCESSED_SERVICE;
1449 NDPR_UNLOCK(pr);
1450 pr = pr->ndpr_next;
1451 }
1452 }
1453 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1454 NDPR_LOCK(pr);
1455 pr->ndpr_stateflags &= ~NDPRF_PROCESSED_SERVICE;
1456 NDPR_UNLOCK(pr);
1457 }
1458 lck_mtx_unlock(nd6_mutex);
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
1469
1470 static int nd6_need_draining = 0;
1471
1472 void
1473 nd6_drain(void *arg)
1474 {
1475 #pragma unused(arg)
1476 nd6log2(debug, "%s: draining ND6 entries\n", __func__);
1477
1478 lck_mtx_lock(rnh_lock);
1479 nd6_need_draining = 1;
1480 nd6_sched_timeout(NULL, NULL);
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
1487 * variable so that later we don't end up ignoring a ``fast timer''
1488 * request if the 5 second timer is running (see nd6_sched_timeout).
1489 */
1490 static void
1491 nd6_timeout(void *arg)
1492 {
1493 struct nd6svc_arg sarg;
1494 uint32_t buf;
1495
1496 lck_mtx_lock(rnh_lock);
1497 bzero(&sarg, sizeof(sarg));
1498 if (nd6_need_draining != 0) {
1499 nd6_need_draining = 0;
1500 sarg.draining = 1;
1501 }
1502 nd6_service(&sarg);
1503 nd6log2(debug, "%s: found %u, aging_lazy %u, aging %u, "
1504 "sticky %u, killed %u\n", __func__, sarg.found, sarg.aging_lazy,
1505 sarg.aging, sarg.sticky, sarg.killed);
1506 /* re-arm the timer if there's work to do */
1507 nd6_timeout_run--;
1508 VERIFY(nd6_timeout_run >= 0 && nd6_timeout_run < 2);
1509 if (arg == &nd6_fast_timer_on) {
1510 nd6_fast_timer_on = FALSE;
1511 }
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;
1525 read_frandom(&buf, sizeof(buf));
1526 ltv.tv_sec = MAX(buf % lazy, 1) * 2;
1527 leeway = &ltv;
1528 }
1529 nd6_sched_timeout(&atv, leeway);
1530 } else if (nd6_debug) {
1531 nd6log2(debug, "%s: not rescheduling timer\n", __func__);
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
1541 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1542 if (atv == NULL) {
1543 tv.tv_usec = 0;
1544 tv.tv_sec = MAX(nd6_prune, 1);
1545 atv = &tv;
1546 ltv = NULL; /* ignore leeway */
1547 }
1548 /* see comments on top of this file */
1549 if (nd6_timeout_run == 0) {
1550 if (ltv == NULL) {
1551 nd6log2(debug, "%s: timer scheduled in "
1552 "T+%llus.%lluu (demand %d)\n", __func__,
1553 (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec,
1554 nd6_sched_timeout_want);
1555 nd6_fast_timer_on = TRUE;
1556 timeout(nd6_timeout, &nd6_fast_timer_on, tvtohz(atv));
1557 } else {
1558 nd6log2(debug, "%s: timer scheduled in "
1559 "T+%llus.%lluu with %llus.%lluu leeway "
1560 "(demand %d)\n", __func__, (uint64_t)atv->tv_sec,
1561 (uint64_t)atv->tv_usec, (uint64_t)ltv->tv_sec,
1562 (uint64_t)ltv->tv_usec, nd6_sched_timeout_want);
1563 nd6_fast_timer_on = FALSE;
1564 timeout_with_leeway(nd6_timeout, NULL,
1565 tvtohz(atv), tvtohz(ltv));
1566 }
1567 nd6_timeout_run++;
1568 nd6_sched_timeout_want = 0;
1569 } else if (nd6_timeout_run == 1 && ltv == NULL &&
1570 nd6_fast_timer_on == FALSE) {
1571 nd6log2(debug, "%s: fast timer scheduled in "
1572 "T+%llus.%lluu (demand %d)\n", __func__,
1573 (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec,
1574 nd6_sched_timeout_want);
1575 nd6_fast_timer_on = TRUE;
1576 nd6_sched_timeout_want = 0;
1577 nd6_timeout_run++;
1578 timeout(nd6_timeout, &nd6_fast_timer_on, tvtohz(atv));
1579 } else {
1580 if (ltv == NULL) {
1581 nd6log2(debug, "%s: not scheduling timer: "
1582 "timers %d, fast_timer %d, T+%llus.%lluu\n",
1583 __func__, nd6_timeout_run, nd6_fast_timer_on,
1584 (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec);
1585 } else {
1586 nd6log2(debug, "%s: not scheduling timer: "
1587 "timers %d, fast_timer %d, T+%llus.%lluu "
1588 "with %llus.%lluu leeway\n", __func__,
1589 nd6_timeout_run, nd6_fast_timer_on,
1590 (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec,
1591 (uint64_t)ltv->tv_sec, (uint64_t)ltv->tv_usec);
1592 }
1593 }
1594 }
1595
1596 /*
1597 * ND6 router advertisement kernel notification
1598 */
1599 void
1600 nd6_post_msg(u_int32_t code, struct nd_prefix_list *prefix_list,
1601 u_int32_t list_length, u_int32_t mtu)
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
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;
1612
1613 bzero(&nd6_ra_msg_data, sizeof(nd6_ra_msg_data));
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,
1627 sizeof(nd6_ra_msg_data.prefix.prefix));
1628 nd6_ra_msg_data.prefix.raflags = itr->pr.ndpr_raf;
1629 nd6_ra_msg_data.prefix.prefixlen = itr->pr.ndpr_plen;
1630 nd6_ra_msg_data.prefix.origin = PR_ORIG_RA;
1631 nd6_ra_msg_data.prefix.vltime = itr->pr.ndpr_vltime;
1632 nd6_ra_msg_data.prefix.pltime = itr->pr.ndpr_pltime;
1633 nd6_ra_msg_data.prefix.expire = ndpr_getexpire(&itr->pr);
1634 nd6_ra_msg_data.prefix.flags = itr->pr.ndpr_stateflags;
1635 nd6_ra_msg_data.prefix.refcnt = itr->pr.ndpr_addrcnt;
1636 nd6_ra_msg_data.prefix.if_index = itr->pr.ndpr_ifp->if_index;
1637
1638 /* send the message up */
1639 ev_msg.dv[0].data_ptr = &nd6_ra_msg_data;
1640 ev_msg.dv[0].data_length = sizeof(nd6_ra_msg_data);
1641 ev_msg.dv[1].data_length = 0;
1642 dlil_post_complete_msg(NULL, &ev_msg);
1643
1644 /* clean up for the next prefix */
1645 bzero(&nd6_ra_msg_data.prefix, sizeof(nd6_ra_msg_data.prefix));
1646 itr = itr->next;
1647 nd6_ra_msg_data.list_index++;
1648 }
1649 }
1650
1651 /*
1652 * Regenerate deprecated/invalidated temporary address
1653 */
1654 static int
1655 regen_tmpaddr(struct in6_ifaddr *ia6)
1656 {
1657 struct ifaddr *ifa;
1658 struct ifnet *ifp;
1659 struct in6_ifaddr *public_ifa6 = NULL;
1660 uint64_t timenow = net_uptime();
1661
1662 ifp = ia6->ia_ifa.ifa_ifp;
1663 ifnet_lock_shared(ifp);
1664 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1665 struct in6_ifaddr *it6;
1666
1667 IFA_LOCK(ifa);
1668 if (ifa->ifa_addr->sa_family != AF_INET6) {
1669 IFA_UNLOCK(ifa);
1670 continue;
1671 }
1672 it6 = (struct in6_ifaddr *)ifa;
1673
1674 /* ignore no autoconf addresses. */
1675 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) {
1676 IFA_UNLOCK(ifa);
1677 continue;
1678 }
1679 /* ignore autoconf addresses with different prefixes. */
1680 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) {
1681 IFA_UNLOCK(ifa);
1682 continue;
1683 }
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 &&
1692 !IFA6_IS_DEPRECATED(it6, timenow)) {
1693 IFA_UNLOCK(ifa);
1694 if (public_ifa6 != NULL) {
1695 IFA_REMREF(&public_ifa6->ia_ifa);
1696 }
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 */
1707 if (!IFA6_IS_DEPRECATED(it6, timenow)) {
1708 IFA_ADDREF_LOCKED(ifa); /* for public_ifa6 */
1709 IFA_UNLOCK(ifa);
1710 if (public_ifa6 != NULL) {
1711 IFA_REMREF(&public_ifa6->ia_ifa);
1712 }
1713 public_ifa6 = it6;
1714 } else {
1715 IFA_UNLOCK(ifa);
1716 }
1717 }
1718 ifnet_lock_done(ifp);
1719
1720 if (public_ifa6 != NULL) {
1721 int e;
1722
1723 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
1724 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
1725 " tmp addr,errno=%d\n", e);
1726 IFA_REMREF(&public_ifa6->ia_ifa);
1727 return -1;
1728 }
1729 IFA_REMREF(&public_ifa6->ia_ifa);
1730 return 0;
1731 }
1732
1733 return -1;
1734 }
1735
1736 /*
1737 * Nuke neighbor cache/prefix/default router management table, right before
1738 * ifp goes away.
1739 */
1740 void
1741 nd6_purge(struct ifnet *ifp)
1742 {
1743 struct llinfo_nd6 *ln;
1744 struct nd_defrouter *dr, *ndr;
1745 struct nd_prefix *pr, *npr;
1746 boolean_t removed;
1747 struct nd_drhead nd_defrouter_tmp;
1748
1749 TAILQ_INIT(&nd_defrouter_tmp);
1750
1751 /* Nuke default router list entries toward ifp */
1752 lck_mtx_lock(nd6_mutex);
1753 TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
1754 if (dr->ifp != ifp) {
1755 continue;
1756 }
1757 /*
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
1777 */
1778 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
1779
1780 if (dr->stateflags & NDDRF_INSTALLED) {
1781 TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
1782 } else {
1783 TAILQ_INSERT_HEAD(&nd_defrouter_tmp, dr, dr_entry);
1784 }
1785 }
1786
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 */
1800 }
1801
1802 /* Nuke prefix list entries toward ifp */
1803 removed = FALSE;
1804 for (pr = nd_prefix.lh_first; pr; pr = npr) {
1805 NDPR_LOCK(pr);
1806 npr = pr->ndpr_next;
1807 if (pr->ndpr_ifp == ifp &&
1808 !(pr->ndpr_stateflags & NDPRF_DEFUNCT)) {
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
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 */
1825 NDPR_ADDREF_LOCKED(pr);
1826 prelist_remove(pr);
1827 NDPR_UNLOCK(pr);
1828 NDPR_REMREF(pr);
1829 removed = TRUE;
1830 npr = nd_prefix.lh_first;
1831 } else {
1832 NDPR_UNLOCK(pr);
1833 }
1834 }
1835 if (removed) {
1836 pfxlist_onlink_check();
1837 }
1838 lck_mtx_unlock(nd6_mutex);
1839
1840 /* cancel default outgoing interface setting */
1841 if (nd6_defifindex == ifp->if_index) {
1842 nd6_setdefaultiface(0);
1843 }
1844
1845 /*
1846 * Perform default router selection even when we are a router,
1847 * if Scoped Routing is enabled.
1848 */
1849 lck_mtx_lock(nd6_mutex);
1850 /* refresh default router list */
1851 defrouter_select(ifp);
1852 lck_mtx_unlock(nd6_mutex);
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 */
1860 again:
1861 lck_mtx_lock(rnh_lock);
1862 ln = llinfo_nd6.ln_next;
1863 while (ln != NULL && ln != &llinfo_nd6) {
1864 struct rtentry *rt;
1865 struct llinfo_nd6 *nln;
1866
1867 nln = ln->ln_next;
1868 rt = ln->ln_rt;
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) {
1873 RT_ADDREF_LOCKED(rt);
1874 RT_UNLOCK(rt);
1875 lck_mtx_unlock(rnh_lock);
1876 /*
1877 * See comments on nd6_service() for reasons why
1878 * this loop is repeated; we bite the costs of
1879 * going thru the same llinfo_nd6 more than once
1880 * here, since this purge happens during detach,
1881 * and that unlike the timer case, it's possible
1882 * there's more than one purges happening at the
1883 * same time (thus a flag wouldn't buy anything).
1884 */
1885 nd6_free(rt);
1886 RT_REMREF(rt);
1887 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
1888 goto again;
1889 } else {
1890 RT_UNLOCK(rt);
1891 }
1892 ln = nln;
1893 }
1894 lck_mtx_unlock(rnh_lock);
1895 }
1896
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 */
1904 struct rtentry *
1905 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp, int rt_locked)
1906 {
1907 struct rtentry *rt;
1908 struct sockaddr_in6 sin6;
1909 unsigned int ifscope;
1910
1911 bzero(&sin6, sizeof(sin6));
1912 sin6.sin6_len = sizeof(struct sockaddr_in6);
1913 sin6.sin6_family = AF_INET6;
1914 sin6.sin6_addr = *addr6;
1915
1916 ifscope = (ifp != NULL) ? ifp->if_index : IFSCOPE_NONE;
1917 if (rt_locked) {
1918 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1919 rt = rtalloc1_scoped_locked(SA(&sin6), create, 0, ifscope);
1920 } else {
1921 rt = rtalloc1_scoped(SA(&sin6), create, 0, ifscope);
1922 }
1923
1924 if (rt != NULL) {
1925 RT_LOCK(rt);
1926 if ((rt->rt_flags & RTF_LLINFO) == 0) {
1927 /*
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.
1932 */
1933 if (create) {
1934 RT_UNLOCK(rt);
1935 if (rt_locked) {
1936 rtfree_locked(rt);
1937 } else {
1938 rtfree(rt);
1939 }
1940 rt = NULL;
1941 }
1942 }
1943 }
1944 if (rt == NULL) {
1945 if (create && ifp) {
1946 struct ifaddr *ifa;
1947 u_int32_t ifa_flags;
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 */
1957 ifa = ifaof_ifpforaddr(SA(&sin6), ifp);
1958 if (ifa == NULL) {
1959 return NULL;
1960 }
1961
1962 /*
1963 * Create a new route. RTF_LLINFO is necessary
1964 * to create a Neighbor Cache entry for the
1965 * destination in nd6_rtrequest which will be
1966 * called in rtrequest via ifa->ifa_rtrequest.
1967 */
1968 if (!rt_locked) {
1969 lck_mtx_lock(rnh_lock);
1970 }
1971 IFA_LOCK_SPIN(ifa);
1972 ifa_flags = ifa->ifa_flags;
1973 IFA_UNLOCK(ifa);
1974 if ((e = rtrequest_scoped_locked(RTM_ADD,
1975 SA(&sin6), ifa->ifa_addr, SA(&all1_sa),
1976 (ifa_flags | RTF_HOST | RTF_LLINFO) &
1977 ~RTF_CLONING, &rt, ifscope)) != 0) {
1978 if (e != EEXIST) {
1979 log(LOG_ERR, "%s: failed to add route "
1980 "for a neighbor(%s), errno=%d\n",
1981 __func__, ip6_sprintf(addr6), e);
1982 }
1983 }
1984 if (!rt_locked) {
1985 lck_mtx_unlock(rnh_lock);
1986 }
1987 IFA_REMREF(ifa);
1988 if (rt == NULL) {
1989 return NULL;
1990 }
1991
1992 RT_LOCK(rt);
1993 if (rt->rt_llinfo) {
1994 struct llinfo_nd6 *ln = rt->rt_llinfo;
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 }
2009 }
2010 } else {
2011 return NULL;
2012 }
2013 }
2014 RT_LOCK_ASSERT_HELD(rt);
2015 /*
2016 * Validation for the entry.
2017 * Note that the check for rt_llinfo is necessary because a cloned
2018 * route from a parent route that has the L flag (e.g. the default
2019 * route to a p2p interface) may have the flag, too, while the
2020 * destination is not actually a neighbor.
2021 * XXX: we can't use rt->rt_ifp to check for the interface, since
2022 * it might be the loopback interface if the entry is for our
2023 * own address on a non-loopback interface. Instead, we should
2024 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
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.
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.
2035 */
2036 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
2037 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
2038 (ifp && rt->rt_ifa->ifa_ifp != ifp &&
2039 !(rt->rt_flags & RTF_PROXY))) {
2040 RT_REMREF_LOCKED(rt);
2041 RT_UNLOCK(rt);
2042 if (create) {
2043 log(LOG_DEBUG, "%s: failed to lookup %s "
2044 "(if = %s)\n", __func__, ip6_sprintf(addr6),
2045 ifp ? if_name(ifp) : "unspec");
2046 /* xxx more logs... kazu */
2047 }
2048 return NULL;
2049 }
2050 /*
2051 * Caller needs to release reference and call RT_UNLOCK(rt).
2052 */
2053 return rt;
2054 }
2055
2056 /*
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.
2060 */
2061 static int
2062 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
2063 {
2064 struct nd_prefix *pr;
2065 struct ifaddr *dstaddr;
2066
2067 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
2068
2069 /*
2070 * A link-local address is always a neighbor.
2071 * XXX: a link does not necessarily specify a single interface.
2072 */
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;
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 }
2093 }
2094
2095 /*
2096 * If the address matches one of our addresses,
2097 * it should be a neighbor.
2098 * If the address matches one of our on-link prefixes, it should be a
2099 * neighbor.
2100 */
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);
2109 continue;
2110 }
2111 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
2112 &addr->sin6_addr, &pr->ndpr_mask)) {
2113 NDPR_UNLOCK(pr);
2114 return 1;
2115 }
2116 NDPR_UNLOCK(pr);
2117 }
2118
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 */
2123 dstaddr = ifa_ifwithdstaddr(SA(addr));
2124 if (dstaddr != NULL) {
2125 if (dstaddr->ifa_ifp == ifp) {
2126 IFA_REMREF(dstaddr);
2127 return 1;
2128 }
2129 IFA_REMREF(dstaddr);
2130 dstaddr = NULL;
2131 }
2132
2133 return 0;
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
2142 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp,
2143 int rt_locked)
2144 {
2145 struct rtentry *rt;
2146
2147 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
2148 lck_mtx_lock(nd6_mutex);
2149 if (nd6_is_new_addr_neighbor(addr, ifp)) {
2150 lck_mtx_unlock(nd6_mutex);
2151 return 1;
2152 }
2153 lck_mtx_unlock(nd6_mutex);
2154
2155 /*
2156 * Even if the address matches none of our addresses, it might be
2157 * in the neighbor cache.
2158 */
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);
2163 return 1;
2164 }
2165
2166 return 0;
2167 }
2168
2169 /*
2170 * Free an nd6 llinfo entry.
2171 * Since the function would cause significant changes in the kernel, DO NOT
2172 * make it global, unless you have a strong reason for the change, and are sure
2173 * that the change is safe.
2174 */
2175 void
2176 nd6_free(struct rtentry *rt)
2177 {
2178 struct llinfo_nd6 *ln = NULL;
2179 struct in6_addr in6 = {};
2180 struct nd_defrouter *dr = NULL;
2181
2182 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2183 RT_LOCK_ASSERT_NOTHELD(rt);
2184 lck_mtx_lock(nd6_mutex);
2185
2186 RT_LOCK(rt);
2187 RT_ADDREF_LOCKED(rt); /* Extra ref */
2188 ln = rt->rt_llinfo;
2189 in6 = SIN6(rt_key(rt))->sin6_addr;
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
2198 /*
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.
2202 */
2203 dr = defrouter_lookup(&SIN6(rt_key(rt))->sin6_addr, rt->rt_ifp);
2204
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 }
2218
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 */
2226
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);
2236
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 */
2243 RT_UNLOCK(rt);
2244 pfxlist_onlink_check();
2245
2246 /*
2247 * refresh default router list
2248 */
2249 defrouter_select(rt->rt_ifp);
2250 }
2251 RT_LOCK_ASSERT_NOTHELD(rt);
2252 lck_mtx_unlock(nd6_mutex);
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 */
2258 (void) rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
2259
2260 /* Extra ref held above; now free it */
2261 rtfree(rt);
2262 }
2263
2264 void
2265 nd6_rtrequest(int req, struct rtentry *rt, struct sockaddr *sa)
2266 {
2267 #pragma unused(sa)
2268 struct sockaddr *gate = rt->rt_gateway;
2269 struct llinfo_nd6 *ln = rt->rt_llinfo;
2270 static struct sockaddr_dl null_sdl =
2271 { .sdl_len = sizeof(null_sdl), .sdl_family = AF_LINK };
2272 struct ifnet *ifp = rt->rt_ifp;
2273 struct ifaddr *ifa;
2274 uint64_t timenow;
2275 char buf[MAX_IPv6_STR_LEN];
2276 struct nd_ifinfo *ndi = ND_IFINFO(rt->rt_ifp);
2277
2278 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
2279 VERIFY(nd6_init_done);
2280 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
2281 RT_LOCK_ASSERT_HELD(rt);
2282
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 */
2288 if (nd6_sched_timeout_want) {
2289 nd6_sched_timeout(NULL, NULL);
2290 }
2291
2292 if (rt->rt_flags & RTF_GATEWAY) {
2293 return;
2294 }
2295
2296 if (!nd6_need_cache(ifp) && !(rt->rt_flags & RTF_HOST)) {
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
2307 if (req == RTM_RESOLVE) {
2308 int no_nd_cache;
2309
2310 if (!nd6_need_cache(ifp)) { /* stf case */
2311 no_nd_cache = 1;
2312 } else {
2313 struct sockaddr_in6 sin6;
2314
2315 rtkey_to_sa6(rt, &sin6);
2316 /*
2317 * nd6_is_addr_neighbor() may call nd6_lookup(),
2318 * therefore we drop rt_lock to avoid deadlock
2319 * during the lookup.
2320 */
2321 RT_ADDREF_LOCKED(rt);
2322 RT_UNLOCK(rt);
2323 no_nd_cache = !nd6_is_addr_neighbor(&sin6, ifp, 1);
2324 RT_LOCK(rt);
2325 RT_REMREF_LOCKED(rt);
2326 }
2327
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 */
2342 if (no_nd_cache) {
2343 rt->rt_flags &= ~RTF_LLINFO;
2344 return;
2345 }
2346 }
2347
2348 timenow = net_uptime();
2349
2350 switch (req) {
2351 case RTM_ADD:
2352 /*
2353 * There is no backward compatibility :)
2354 *
2355 * if ((rt->rt_flags & RTF_HOST) == 0 &&
2356 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
2357 * rt->rt_flags |= RTF_CLONING;
2358 */
2359 if ((rt->rt_flags & RTF_CLONING) ||
2360 ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
2361 /*
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).
2366 */
2367 if (rt_setgate(rt, rt_key(rt), SA(&null_sdl)) == 0) {
2368 gate = rt->rt_gateway;
2369 SDL(gate)->sdl_type = ifp->if_type;
2370 SDL(gate)->sdl_index = ifp->if_index;
2371 /*
2372 * In case we're called before 1.0 sec.
2373 * has elapsed.
2374 */
2375 if (ln != NULL) {
2376 ln_setexpire(ln,
2377 (ifp->if_eflags & IFEF_IPV6_ND6ALT)
2378 ? 0 : MAX(timenow, 1));
2379 }
2380 }
2381 if (rt->rt_flags & RTF_CLONING) {
2382 break;
2383 }
2384 }
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 */
2404 case RTM_RESOLVE:
2405 if (!(ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))) {
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 ||
2411 gate->sa_len < sizeof(null_sdl)) {
2412 /* Don't complain in case of RTM_ADD */
2413 if (req == RTM_RESOLVE) {
2414 log(LOG_ERR, "%s: route to %s has bad "
2415 "gateway address (sa_family %u "
2416 "sa_len %u) on %s\n", __func__,
2417 inet_ntop(AF_INET6,
2418 &SIN6(rt_key(rt))->sin6_addr, buf,
2419 sizeof(buf)), gate->sa_family,
2420 gate->sa_len, if_name(ifp));
2421 }
2422 break;
2423 }
2424 SDL(gate)->sdl_type = ifp->if_type;
2425 SDL(gate)->sdl_index = ifp->if_index;
2426 }
2427 if (ln != NULL) {
2428 break; /* This happens on a route change */
2429 }
2430 /*
2431 * Case 2: This route may come from cloning, or a manual route
2432 * add with a LL address.
2433 */
2434 rt->rt_llinfo = ln = nd6_llinfo_alloc(M_WAITOK);
2435 if (ln == NULL) {
2436 break;
2437 }
2438
2439 nd6_allocated++;
2440 rt->rt_llinfo_get_ri = nd6_llinfo_get_ri;
2441 rt->rt_llinfo_get_iflri = nd6_llinfo_get_iflri;
2442 rt->rt_llinfo_purge = nd6_llinfo_purge;
2443 rt->rt_llinfo_free = nd6_llinfo_free;
2444 rt->rt_llinfo_refresh = nd6_llinfo_refresh;
2445 rt->rt_flags |= RTF_LLINFO;
2446 ln->ln_rt = rt;
2447 /* this is required for "ndp" command. - shin */
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)) {
2455 /*
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 */
2460 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2461 ln_setexpire(ln, 0);
2462 } else {
2463 /*
2464 * When req == RTM_RESOLVE, rt is created and
2465 * initialized in rtrequest(), so rt_expire is 0.
2466 */
2467 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_NOSTATE);
2468 /* In case we're called before 1.0 sec. has elapsed */
2469 ln_setexpire(ln, (ifp->if_eflags & IFEF_IPV6_ND6ALT) ?
2470 0 : MAX(timenow, 1));
2471 }
2472 LN_INSERTHEAD(ln);
2473 nd6_inuse++;
2474
2475 /* We have at least one entry; arm the timer if not already */
2476 nd6_sched_timeout(NULL, NULL);
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 */
2485 if (ip6_neighborgcthresh > 0 &&
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 }
2502 if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE) {
2503 ND6_CACHE_STATE_TRANSITION(ln_end, ND6_LLINFO_STALE);
2504 } else {
2505 ND6_CACHE_STATE_TRANSITION(ln_end, ND6_LLINFO_PURGE);
2506 }
2507 ln_setexpire(ln_end, timenow);
2508 RT_UNLOCK(rt_end);
2509 }
2510 }
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,
2517 &SIN6(rt_key(rt))->sin6_addr);
2518 if (ifa != NULL) {
2519 caddr_t macp = nd6_ifptomac(ifp);
2520 ln_setexpire(ln, 0);
2521 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2522 if (macp != NULL) {
2523 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
2524 SDL(gate)->sdl_alen = ifp->if_addrlen;
2525 }
2526 if (nd6_useloopback) {
2527 if (rt->rt_ifp != lo_ifp) {
2528 /*
2529 * Purge any link-layer info caching.
2530 */
2531 if (rt->rt_llinfo_purge != NULL) {
2532 rt->rt_llinfo_purge(rt);
2533 }
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);
2541 rt->rt_if_ref_fn(rt->rt_ifp,
2542 -1);
2543 }
2544 }
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 */
2550 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) {
2551 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
2552 }
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) {
2562 rtsetifa(rt, ifa);
2563 }
2564 }
2565 IFA_REMREF(ifa);
2566 } else if (rt->rt_flags & RTF_ANNOUNCE) {
2567 ln_setexpire(ln, 0);
2568 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2569
2570 /* join solicited node multicast for proxy ND */
2571 if (ifp->if_flags & IFF_MULTICAST) {
2572 struct in6_addr llsol;
2573 struct in6_multi *in6m;
2574 int error;
2575
2576 llsol = SIN6(rt_key(rt))->sin6_addr;
2577 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
2578 llsol.s6_addr32[1] = 0;
2579 llsol.s6_addr32[2] = htonl(1);
2580 llsol.s6_addr8[12] = 0xff;
2581 if (in6_setscope(&llsol, ifp, NULL)) {
2582 break;
2583 }
2584 error = in6_mc_join(ifp, &llsol,
2585 NULL, &in6m, 0);
2586 if (error) {
2587 nd6log(error, "%s: failed to join "
2588 "%s (errno=%d)\n", if_name(ifp),
2589 ip6_sprintf(&llsol), error);
2590 } else {
2591 IN6M_REMREF(in6m);
2592 }
2593 }
2594 }
2595 break;
2596
2597 case RTM_DELETE:
2598 if (ln == NULL) {
2599 break;
2600 }
2601 /* leave from solicited node multicast for proxy ND */
2602 if ((rt->rt_flags & RTF_ANNOUNCE) &&
2603 (ifp->if_flags & IFF_MULTICAST)) {
2604 struct in6_addr llsol;
2605 struct in6_multi *in6m;
2606
2607 llsol = SIN6(rt_key(rt))->sin6_addr;
2608 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
2609 llsol.s6_addr32[1] = 0;
2610 llsol.s6_addr32[2] = htonl(1);
2611 llsol.s6_addr8[12] = 0xff;
2612 if (in6_setscope(&llsol, ifp, NULL) == 0) {
2613 in6_multihead_lock_shared();
2614 IN6_LOOKUP_MULTI(&llsol, ifp, in6m);
2615 in6_multihead_lock_done();
2616 if (in6m != NULL) {
2617 in6_mc_leave(in6m, NULL);
2618 IN6M_REMREF(in6m);
2619 }
2620 }
2621 }
2622 nd6_inuse--;
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 */
2629 if (ln->ln_flags & ND6_LNF_IN_USE) {
2630 LN_DEQUEUE(ln);
2631 }
2632
2633 /*
2634 * Purge any link-layer info caching.
2635 */
2636 if (rt->rt_llinfo_purge != NULL) {
2637 rt->rt_llinfo_purge(rt);
2638 }
2639
2640 rt->rt_flags &= ~RTF_LLINFO;
2641 if (ln->ln_hold != NULL) {
2642 m_freem_list(ln->ln_hold);
2643 ln->ln_hold = NULL;
2644 }
2645 }
2646 }
2647
2648 static int
2649 nd6_siocgdrlst(void *data, int data_is_64)
2650 {
2651 struct in6_drlist_32 *drl_32;
2652 struct nd_defrouter *dr;
2653 int i = 0;
2654
2655 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
2656
2657 dr = TAILQ_FIRST(&nd_defrouter);
2658
2659 /* XXX Handle mapped defrouter entries */
2660 /* For 64-bit process */
2661 if (data_is_64) {
2662 struct in6_drlist_64 *drl_64;
2663
2664 drl_64 = _MALLOC(sizeof(*drl_64), M_TEMP, M_WAITOK | M_ZERO);
2665 if (drl_64 == NULL) {
2666 return ENOMEM;
2667 }
2668
2669 /* preserve the interface name */
2670 bcopy(data, drl_64, sizeof(drl_64->ifname));
2671
2672 while (dr && i < DRLSTSIZ) {
2673 drl_64->defrouter[i].rtaddr = dr->rtaddr;
2674 if (IN6_IS_ADDR_LINKLOCAL(
2675 &drl_64->defrouter[i].rtaddr)) {
2676 /* XXX: need to this hack for KAME stack */
2677 drl_64->defrouter[i].rtaddr.s6_addr16[1] = 0;
2678 } else {
2679 log(LOG_ERR,
2680 "default router list contains a "
2681 "non-linklocal address(%s)\n",
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;
2686 drl_64->defrouter[i].expire = nddr_getexpire(dr);
2687 drl_64->defrouter[i].if_index = dr->ifp->if_index;
2688 i++;
2689 dr = TAILQ_NEXT(dr, dr_entry);
2690 }
2691 bcopy(drl_64, data, sizeof(*drl_64));
2692 _FREE(drl_64, M_TEMP);
2693 return 0;
2694 }
2695
2696 /* For 32-bit process */
2697 drl_32 = _MALLOC(sizeof(*drl_32), M_TEMP, M_WAITOK | M_ZERO);
2698 if (drl_32 == NULL) {
2699 return ENOMEM;
2700 }
2701
2702 /* preserve the interface name */
2703 bcopy(data, drl_32, sizeof(drl_32->ifname));
2704
2705 while (dr != NULL && i < DRLSTSIZ) {
2706 drl_32->defrouter[i].rtaddr = dr->rtaddr;
2707 if (IN6_IS_ADDR_LINKLOCAL(&drl_32->defrouter[i].rtaddr)) {
2708 /* XXX: need to this hack for KAME stack */
2709 drl_32->defrouter[i].rtaddr.s6_addr16[1] = 0;
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;
2718 drl_32->defrouter[i].expire = nddr_getexpire(dr);
2719 drl_32->defrouter[i].if_index = dr->ifp->if_index;
2720 i++;
2721 dr = TAILQ_NEXT(dr, dr_entry);
2722 }
2723 bcopy(drl_32, data, sizeof(*drl_32));
2724 _FREE(drl_32, M_TEMP);
2725 return 0;
2726 }
2727
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
2734 nd6_siocgprlst(void *data, int data_is_64)
2735 {
2736 struct in6_prlist_32 *prl_32;
2737 struct nd_prefix *pr;
2738 int i = 0;
2739
2740 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
2741
2742 pr = nd_prefix.lh_first;
2743
2744 /* XXX Handle mapped defrouter entries */
2745 /* For 64-bit process */
2746 if (data_is_64) {
2747 struct in6_prlist_64 *prl_64;
2748
2749 prl_64 = _MALLOC(sizeof(*prl_64), M_TEMP, M_WAITOK | M_ZERO);
2750 if (prl_64 == NULL) {
2751 return ENOMEM;
2752 }
2753
2754 /* preserve the interface name */
2755 bcopy(data, prl_64, sizeof(prl_64->ifname));
2756
2757 while (pr && i < PRLSTSIZ) {
2758 struct nd_pfxrouter *pfr;
2759 int j;
2760
2761 NDPR_LOCK(pr);
2762 (void) in6_embedscope(&prl_64->prefix[i].prefix,
2763 &pr->ndpr_prefix, NULL, NULL, NULL);
2764 prl_64->prefix[i].raflags = pr->ndpr_raf;
2765 prl_64->prefix[i].prefixlen = pr->ndpr_plen;
2766 prl_64->prefix[i].vltime = pr->ndpr_vltime;
2767 prl_64->prefix[i].pltime = pr->ndpr_pltime;
2768 prl_64->prefix[i].if_index = pr->ndpr_ifp->if_index;
2769 prl_64->prefix[i].expire = ndpr_getexpire(pr);
2770
2771 pfr = pr->ndpr_advrtrs.lh_first;
2772 j = 0;
2773 while (pfr) {
2774 if (j < DRLSTSIZ) {
2775 #define RTRADDR prl_64->prefix[i].advrtr[j]
2776 RTRADDR = pfr->router->rtaddr;
2777 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
2778 /* XXX: hack for KAME */
2779 RTRADDR.s6_addr16[1] = 0;
2780 } else {
2781 log(LOG_ERR,
2782 "a router(%s) advertises "
2783 "a prefix with "
2784 "non-link local address\n",
2785 ip6_sprintf(&RTRADDR));
2786 }
2787 #undef RTRADDR
2788 }
2789 j++;
2790 pfr = pfr->pfr_next;
2791 }
2792 prl_64->prefix[i].advrtrs = j;
2793 prl_64->prefix[i].origin = PR_ORIG_RA;
2794 NDPR_UNLOCK(pr);
2795
2796 i++;
2797 pr = pr->ndpr_next;
2798 }
2799 bcopy(prl_64, data, sizeof(*prl_64));
2800 _FREE(prl_64, M_TEMP);
2801 return 0;
2802 }
2803
2804 /* For 32-bit process */
2805 prl_32 = _MALLOC(sizeof(*prl_32), M_TEMP, M_WAITOK | M_ZERO);
2806 if (prl_32 == NULL) {
2807 return ENOMEM;
2808 }
2809
2810 /* preserve the interface name */
2811 bcopy(data, prl_32, sizeof(prl_32->ifname));
2812
2813 while (pr && i < PRLSTSIZ) {
2814 struct nd_pfxrouter *pfr;
2815 int j;
2816
2817 NDPR_LOCK(pr);
2818 (void) in6_embedscope(&prl_32->prefix[i].prefix,
2819 &pr->ndpr_prefix, NULL, NULL, NULL);
2820 prl_32->prefix[i].raflags = pr->ndpr_raf;
2821 prl_32->prefix[i].prefixlen = pr->ndpr_plen;
2822 prl_32->prefix[i].vltime = pr->ndpr_vltime;
2823 prl_32->prefix[i].pltime = pr->ndpr_pltime;
2824 prl_32->prefix[i].if_index = pr->ndpr_ifp->if_index;
2825 prl_32->prefix[i].expire = ndpr_getexpire(pr);
2826
2827 pfr = pr->ndpr_advrtrs.lh_first;
2828 j = 0;
2829 while (pfr) {
2830 if (j < DRLSTSIZ) {
2831 #define RTRADDR prl_32->prefix[i].advrtr[j]
2832 RTRADDR = pfr->router->rtaddr;
2833 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
2834 /* XXX: hack for KAME */
2835 RTRADDR.s6_addr16[1] = 0;
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;
2850 NDPR_UNLOCK(pr);
2851
2852 i++;
2853 pr = pr->ndpr_next;
2854 }
2855 bcopy(prl_32, data, sizeof(*prl_32));
2856 _FREE(prl_32, M_TEMP);
2857 return 0;
2858 }
2859
2860 int
2861 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
2862 {
2863 struct nd_defrouter *dr;
2864 struct nd_prefix *pr;
2865 struct rtentry *rt;
2866 int error = 0;
2867
2868 VERIFY(ifp != NULL);
2869
2870 switch (cmd) {
2871 case SIOCGDRLST_IN6_32: /* struct in6_drlist_32 */
2872 case SIOCGDRLST_IN6_64: /* struct in6_drlist_64 */
2873 /*
2874 * obsolete API, use sysctl under net.inet6.icmp6
2875 */
2876 lck_mtx_lock(nd6_mutex);
2877 error = nd6_siocgdrlst(data, cmd == SIOCGDRLST_IN6_64);
2878 lck_mtx_unlock(nd6_mutex);
2879 break;
2880
2881 case SIOCGPRLST_IN6_32: /* struct in6_prlist_32 */
2882 case SIOCGPRLST_IN6_64: /* struct in6_prlist_64 */
2883 /*
2884 * obsolete API, use sysctl under net.inet6.icmp6
2885 */
2886 lck_mtx_lock(nd6_mutex);
2887 error = nd6_siocgprlst(data, cmd == SIOCGPRLST_IN6_64);
2888 lck_mtx_unlock(nd6_mutex);
2889 break;
2890
2891 case OSIOCGIFINFO_IN6: /* struct in6_ondireq */
2892 case SIOCGIFINFO_IN6: { /* struct in6_ondireq */
2893 u_int32_t linkmtu;
2894 struct in6_ondireq *ondi = (struct in6_ondireq *)(void *)data;
2895 struct nd_ifinfo *ndi;
2896 /*
2897 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
2898 * instead of in6_ndireq, so we treat it as such.
2899 */
2900 ndi = ND_IFINFO(ifp);
2901 if ((NULL == ndi) || (FALSE == ndi->initialized)) {
2902 error = EINVAL;
2903 break;
2904 }
2905 lck_mtx_lock(&ndi->lock);
2906 linkmtu = IN6_LINKMTU(ifp);
2907 bcopy(&linkmtu, &ondi->ndi.linkmtu, sizeof(linkmtu));
2908 bcopy(&ndi->maxmtu, &ondi->ndi.maxmtu,
2909 sizeof(u_int32_t));
2910 bcopy(&ndi->basereachable, &ondi->ndi.basereachable,
2911 sizeof(u_int32_t));
2912 bcopy(&ndi->reachable, &ondi->ndi.reachable,
2913 sizeof(u_int32_t));
2914 bcopy(&ndi->retrans, &ondi->ndi.retrans,
2915 sizeof(u_int32_t));
2916 bcopy(&ndi->flags, &ondi->ndi.flags,
2917 sizeof(u_int32_t));
2918 bcopy(&ndi->recalctm, &ondi->ndi.recalctm,
2919 sizeof(int));
2920 ondi->ndi.chlim = ndi->chlim;
2921 ondi->ndi.receivedra = 0;
2922 lck_mtx_unlock(&ndi->lock);
2923 break;
2924 }
2925
2926 case SIOCSIFINFO_FLAGS: { /* struct in6_ndireq */
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 */
2933 struct in6_ndireq *cndi = (struct in6_ndireq *)(void *)data;
2934 u_int32_t oflags, flags;
2935 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
2936
2937 /* XXX: almost all other fields of cndi->ndi is unused */
2938 if ((NULL == ndi) || !ndi->initialized) {
2939 error = EINVAL;
2940 break;
2941 }
2942
2943 lck_mtx_lock(&ndi->lock);
2944 oflags = ndi->flags;
2945 bcopy(&cndi->ndi.flags, &(ndi->flags), sizeof(flags));
2946 flags = ndi->flags;
2947 lck_mtx_unlock(&ndi->lock);
2948
2949 if (oflags == flags) {
2950 break;
2951 }
2952
2953 error = nd6_setifinfo(ifp, oflags, flags);
2954 break;
2955 }
2956
2957 case SIOCSNDFLUSH_IN6: /* struct in6_ifreq */
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 */
2963 lck_mtx_lock(nd6_mutex);
2964 defrouter_reset();
2965 defrouter_select(ifp);
2966 lck_mtx_unlock(nd6_mutex);
2967 /* xxx sumikawa: flush prefix list */
2968 break;
2969
2970 case SIOCSPFXFLUSH_IN6: { /* struct in6_ifreq */
2971 /* flush all the prefix advertised by routers */
2972 struct nd_prefix *next = NULL;
2973
2974 lck_mtx_lock(nd6_mutex);
2975 for (pr = nd_prefix.lh_first; pr; pr = next) {
2976 struct in6_ifaddr *ia = NULL;
2977 bool iterate_pfxlist_again = false;
2978
2979 next = pr->ndpr_next;
2980
2981 NDPR_LOCK(pr);
2982 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) {
2983 NDPR_UNLOCK(pr);
2984 continue; /* XXX */
2985 }
2986 if (ifp != lo_ifp && pr->ndpr_ifp != ifp) {
2987 NDPR_UNLOCK(pr);
2988 continue;
2989 }
2990 /* do we really have to remove addresses as well? */
2991 NDPR_ADDREF_LOCKED(pr);
2992 NDPR_UNLOCK(pr);
2993 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
2994 bool from_begining = true;
2995 while (from_begining) {
2996 from_begining = false;
2997 TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
2998 IFA_LOCK(&ia->ia_ifa);
2999 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) {
3000 IFA_UNLOCK(&ia->ia_ifa);
3001 continue;
3002 }
3003
3004 if (ia->ia6_ndpr == pr) {
3005 IFA_ADDREF_LOCKED(&ia->ia_ifa);
3006 IFA_UNLOCK(&ia->ia_ifa);
3007 lck_rw_done(&in6_ifaddr_rwlock);
3008 lck_mtx_unlock(nd6_mutex);
3009 in6_purgeaddr(&ia->ia_ifa);
3010 IFA_REMREF(&ia->ia_ifa);
3011 lck_mtx_lock(nd6_mutex);
3012 lck_rw_lock_exclusive(
3013 &in6_ifaddr_rwlock);
3014 /*
3015 * Purging the address caused
3016 * in6_ifaddr_rwlock to be
3017 * dropped and
3018 * reacquired; therefore search again
3019 * from the beginning of in6_ifaddrs.
3020 * The same applies for the prefix list.
3021 */
3022 iterate_pfxlist_again = true;
3023 from_begining = true;
3024 break;
3025 }
3026 IFA_UNLOCK(&ia->ia_ifa);
3027 }
3028 }
3029 lck_rw_done(&in6_ifaddr_rwlock);
3030 NDPR_LOCK(pr);
3031 prelist_remove(pr);
3032 NDPR_UNLOCK(pr);
3033 pfxlist_onlink_check();
3034 NDPR_REMREF(pr);
3035 if (iterate_pfxlist_again) {
3036 next = nd_prefix.lh_first;
3037 }
3038 }
3039 lck_mtx_unlock(nd6_mutex);
3040 break;
3041 }
3042
3043 case SIOCSRTRFLUSH_IN6: { /* struct in6_ifreq */
3044 /* flush all the default routers */
3045 struct nd_defrouter *next;
3046 struct nd_drhead nd_defrouter_tmp;
3047
3048 TAILQ_INIT(&nd_defrouter_tmp);
3049 lck_mtx_lock(nd6_mutex);
3050 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
3051 /*
3052 * The first entry of the list may be stored in
3053 * the routing table, so we'll delete it later.
3054 */
3055 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
3056 next = TAILQ_NEXT(dr, dr_entry);
3057 if (ifp == lo_ifp || dr->ifp == ifp) {
3058 /*
3059 * Remove the entry from default router list
3060 * and add it to the temp list.
3061 * nd_defrouter_tmp will be a local temporary
3062 * list as no one else can get the same
3063 * removed entry once it is removed from default
3064 * router list.
3065 * Remove the reference after calling defrtrlist_de
3066 */
3067 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
3068 TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
3069 }
3070 }
3071
3072 dr = TAILQ_FIRST(&nd_defrouter);
3073 if (ifp == lo_ifp ||
3074 dr->ifp == ifp) {
3075 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
3076 TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
3077 }
3078 }
3079
3080 /*
3081 * Keep the following separate from the above iteration of
3082 * nd_defrouter because it's not safe to call
3083 * defrtrlist_del while iterating global default
3084 * router list. Global list has to be traversed
3085 * while holding nd6_mutex throughout.
3086 *
3087 * The following call to defrtrlist_del should be
3088 * safe as we are iterating a local list of
3089 * default routers.
3090 */
3091 TAILQ_FOREACH_SAFE(dr, &nd_defrouter_tmp, dr_entry, next) {
3092 TAILQ_REMOVE(&nd_defrouter_tmp, dr, dr_entry);
3093 defrtrlist_del(dr);
3094 NDDR_REMREF(dr); /* remove list reference */
3095 }
3096 lck_mtx_unlock(nd6_mutex);
3097 break;
3098 }
3099
3100 case SIOCGNBRINFO_IN6_32: { /* struct in6_nbrinfo_32 */
3101 struct llinfo_nd6 *ln;
3102 struct in6_nbrinfo_32 nbi_32;
3103 struct in6_addr nb_addr; /* make local for safety */
3104
3105 bcopy(data, &nbi_32, sizeof(nbi_32));
3106 nb_addr = nbi_32.addr;
3107 /*
3108 * XXX: KAME specific hack for scoped addresses
3109 * XXXX: for other scopes than link-local?
3110 */
3111 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32.addr) ||
3112 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32.addr)) {
3113 u_int16_t *idp =
3114 (u_int16_t *)(void *)&nb_addr.s6_addr[2];
3115
3116 if (*idp == 0) {
3117 *idp = htons(ifp->if_index);
3118 }
3119 }
3120
3121 /* Callee returns a locked route upon success */
3122 if ((rt = nd6_lookup(&nb_addr, 0, ifp, 0)) == NULL) {
3123 error = EINVAL;
3124 break;
3125 }
3126 RT_LOCK_ASSERT_HELD(rt);
3127 ln = rt->rt_llinfo;
3128 nbi_32.state = ln->ln_state;
3129 nbi_32.asked = ln->ln_asked;
3130 nbi_32.isrouter = ln->ln_router;
3131 nbi_32.expire = ln_getexpire(ln);
3132 RT_REMREF_LOCKED(rt);
3133 RT_UNLOCK(rt);
3134 bcopy(&nbi_32, data, sizeof(nbi_32));
3135 break;
3136 }
3137
3138 case SIOCGNBRINFO_IN6_64: { /* struct in6_nbrinfo_64 */
3139 struct llinfo_nd6 *ln;
3140 struct in6_nbrinfo_64 nbi_64;
3141 struct in6_addr nb_addr; /* make local for safety */
3142
3143 bcopy(data, &nbi_64, sizeof(nbi_64));
3144 nb_addr = nbi_64.addr;
3145 /*
3146 * XXX: KAME specific hack for scoped addresses
3147 * XXXX: for other scopes than link-local?
3148 */
3149 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64.addr) ||
3150 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64.addr)) {
3151 u_int16_t *idp =
3152 (u_int16_t *)(void *)&nb_addr.s6_addr[2];
3153
3154 if (*idp == 0) {
3155 *idp = htons(ifp->if_index);
3156 }
3157 }
3158
3159 /* Callee returns a locked route upon success */
3160 if ((rt = nd6_lookup(&nb_addr, 0, ifp, 0)) == NULL) {
3161 error = EINVAL;
3162 break;
3163 }
3164 RT_LOCK_ASSERT_HELD(rt);
3165 ln = rt->rt_llinfo;
3166 nbi_64.state = ln->ln_state;
3167 nbi_64.asked = ln->ln_asked;
3168 nbi_64.isrouter = ln->ln_router;
3169 nbi_64.expire = ln_getexpire(ln);
3170 RT_REMREF_LOCKED(rt);
3171 RT_UNLOCK(rt);
3172 bcopy(&nbi_64, data, sizeof(nbi_64));
3173 break;
3174 }
3175
3176 case SIOCGDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
3177 case SIOCGDEFIFACE_IN6_64: { /* struct in6_ndifreq_64 */
3178 struct in6_ndifreq_64 *ndif_64 =
3179 (struct in6_ndifreq_64 *)(void *)data;
3180 struct in6_ndifreq_32 *ndif_32 =
3181 (struct in6_ndifreq_32 *)(void *)data;
3182
3183 if (cmd == SIOCGDEFIFACE_IN6_64) {
3184 u_int64_t j = nd6_defifindex;
3185 __nochk_bcopy(&j, &ndif_64->ifindex, sizeof(j));
3186 } else {
3187 bcopy(&nd6_defifindex, &ndif_32->ifindex,
3188 sizeof(u_int32_t));
3189 }
3190 break;
3191 }
3192
3193 case SIOCSDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
3194 case SIOCSDEFIFACE_IN6_64: { /* struct in6_ndifreq_64 */
3195 struct in6_ndifreq_64 *ndif_64 =
3196 (struct in6_ndifreq_64 *)(void *)data;
3197 struct in6_ndifreq_32 *ndif_32 =
3198 (struct in6_ndifreq_32 *)(void *)data;
3199 u_int32_t idx;
3200
3201 if (cmd == SIOCSDEFIFACE_IN6_64) {
3202 u_int64_t j;
3203 __nochk_bcopy(&ndif_64->ifindex, &j, sizeof(j));
3204 idx = (u_int32_t)j;
3205 } else {
3206 bcopy(&ndif_32->ifindex, &idx, sizeof(idx));
3207 }
3208
3209 error = nd6_setdefaultiface(idx);
3210 return error;
3211 /* NOTREACHED */
3212 }
3213 case SIOCGIFCGAPREP_IN6:
3214 case SIOCSIFCGAPREP_IN6:
3215 {
3216 struct in6_cgareq *p_cgareq =
3217 (struct in6_cgareq *)(void *)data;
3218 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
3219
3220 struct in6_cga_modifier *req_cga_mod =
3221 &(p_cgareq->cgar_cgaprep.cga_modifier);
3222 struct in6_cga_modifier *ndi_cga_mod = NULL;
3223
3224 if ((NULL == ndi) || !ndi->initialized) {
3225 error = EINVAL;
3226 break;
3227 }
3228
3229 lck_mtx_lock(&ndi->lock);
3230 ndi_cga_mod = &(ndi->local_cga_modifier);
3231
3232 if (cmd == SIOCSIFCGAPREP_IN6) {
3233 bcopy(req_cga_mod, ndi_cga_mod, sizeof(*ndi_cga_mod));
3234 ndi->cga_initialized = TRUE;
3235 } else {
3236 bcopy(ndi_cga_mod, req_cga_mod, sizeof(*req_cga_mod));
3237 }
3238
3239 lck_mtx_unlock(&ndi->lock);
3240 return error;
3241 /* NOTREACHED */
3242 }
3243 }
3244 return error;
3245 }
3246
3247 /*
3248 * Create neighbor cache entry and cache link-layer address,
3249 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
3250 */
3251 void
3252 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
3253 int lladdrlen, int type, int code)
3254 {
3255 #pragma unused(lladdrlen)
3256 struct rtentry *rt = NULL;
3257 struct llinfo_nd6 *ln = NULL;
3258 int is_newentry;
3259 struct sockaddr_dl *sdl = NULL;
3260 int do_update;
3261 int olladdr;
3262 int llchange;
3263 int newstate = 0;
3264 uint64_t timenow;
3265 boolean_t sched_timeout = FALSE;
3266 struct nd_ifinfo *ndi = NULL;
3267
3268 if (ifp == NULL) {
3269 panic("ifp == NULL in nd6_cache_lladdr");
3270 }
3271 if (from == NULL) {
3272 panic("from == NULL in nd6_cache_lladdr");
3273 }
3274
3275 /* nothing must be updated for unspecified address */
3276 if (IN6_IS_ADDR_UNSPECIFIED(from)) {
3277 return;
3278 }
3279
3280 /*
3281 * Validation about ifp->if_addrlen and lladdrlen must be done in
3282 * the caller.
3283 */
3284 timenow = net_uptime();
3285
3286 rt = nd6_lookup(from, 0, ifp, 0);
3287 if (rt == NULL) {
3288 if ((rt = nd6_lookup(from, 1, ifp, 0)) == NULL) {
3289 return;
3290 }
3291 RT_LOCK_ASSERT_HELD(rt);
3292 is_newentry = 1;
3293 } else {
3294 RT_LOCK_ASSERT_HELD(rt);
3295 /* do nothing if static ndp is set */
3296 if (rt->rt_flags & RTF_STATIC) {
3297 RT_REMREF_LOCKED(rt);
3298 RT_UNLOCK(rt);
3299 return;
3300 }
3301 is_newentry = 0;
3302 }
3303
3304 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
3305 fail:
3306 RT_UNLOCK(rt);
3307 nd6_free(rt);
3308 rtfree(rt);
3309 return;
3310 }
3311 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
3312 if (ln == NULL) {
3313 goto fail;
3314 }
3315 if (rt->rt_gateway == NULL) {
3316 goto fail;
3317 }
3318 if (rt->rt_gateway->sa_family != AF_LINK) {
3319 goto fail;
3320 }
3321 sdl = SDL(rt->rt_gateway);
3322
3323 olladdr = (sdl->sdl_alen) ? 1 : 0;
3324 if (olladdr && lladdr) {
3325 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) {
3326 llchange = 1;
3327 } else {
3328 llchange = 0;
3329 }
3330 } else {
3331 llchange = 0;
3332 }
3333
3334 /*
3335 * newentry olladdr lladdr llchange (*=record)
3336 * 0 n n -- (1)
3337 * 0 y n -- (2)
3338 * 0 n y -- (3) * STALE
3339 * 0 y y n (4) *
3340 * 0 y y y (5) * STALE
3341 * 1 -- n -- (6) NOSTATE(= PASSIVE)
3342 * 1 -- y -- (7) * STALE
3343 */
3344
3345 if (lladdr != NULL) { /* (3-5) and (7) */
3346 /*
3347 * Record source link-layer address
3348 * XXX is it dependent to ifp->if_type?
3349 */
3350 sdl->sdl_alen = ifp->if_addrlen;
3351 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
3352
3353 /* cache the gateway (sender HW) address */
3354 nd6_llreach_alloc(rt, ifp, LLADDR(sdl), sdl->sdl_alen, FALSE);
3355 }
3356
3357 if (is_newentry == 0) {
3358 if ((!olladdr && lladdr != NULL) || /* (3) */
3359 (olladdr && lladdr != NULL && llchange)) { /* (5) */
3360 do_update = 1;
3361 newstate = ND6_LLINFO_STALE;
3362 } else { /* (1-2,4) */
3363 do_update = 0;
3364 }
3365 } else {
3366 do_update = 1;
3367 if (lladdr == NULL) { /* (6) */
3368 newstate = ND6_LLINFO_NOSTATE;
3369 } else { /* (7) */
3370 newstate = ND6_LLINFO_STALE;
3371 }
3372 }
3373
3374 /*
3375 * For interface's that do not perform NUD
3376 * neighbor cache entres must always be marked
3377 * reachable with no expiry
3378 */
3379 ndi = ND_IFINFO(ifp);
3380 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
3381
3382 if (ndi && !(ndi->flags & ND6_IFF_PERFORMNUD)) {
3383 newstate = ND6_LLINFO_REACHABLE;
3384 ln_setexpire(ln, 0);
3385 }
3386
3387 if (do_update) {
3388 /*
3389 * Update the state of the neighbor cache.
3390 */
3391 ND6_CACHE_STATE_TRANSITION(ln, newstate);
3392
3393 if ((ln->ln_state == ND6_LLINFO_STALE) ||
3394 (ln->ln_state == ND6_LLINFO_REACHABLE)) {
3395 struct mbuf *m = ln->ln_hold;
3396 /*
3397 * XXX: since nd6_output() below will cause
3398 * state tansition to DELAY and reset the timer,
3399 * we must set the timer now, although it is actually
3400 * meaningless.
3401 */
3402 if (ln->ln_state == ND6_LLINFO_STALE) {
3403 ln_setexpire(ln, timenow + nd6_gctimer);
3404 }
3405
3406 ln->ln_hold = NULL;
3407 if (m != NULL) {
3408 struct sockaddr_in6 sin6;
3409
3410 rtkey_to_sa6(rt, &sin6);
3411 /*
3412 * we assume ifp is not a p2p here, so just
3413 * set the 2nd argument as the 1st one.
3414 */
3415 RT_UNLOCK(rt);
3416 nd6_output_list(ifp, ifp, m, &sin6, rt, NULL);
3417 RT_LOCK(rt);
3418 }
3419 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
3420 /* probe right away */
3421 ln_setexpire(ln, timenow);
3422 sched_timeout = TRUE;
3423 }
3424 }
3425
3426 /*
3427 * ICMP6 type dependent behavior.
3428 *
3429 * NS: clear IsRouter if new entry
3430 * RS: clear IsRouter
3431 * RA: set IsRouter if there's lladdr
3432 * redir: clear IsRouter if new entry
3433 *
3434 * RA case, (1):
3435 * The spec says that we must set IsRouter in the following cases:
3436 * - If lladdr exist, set IsRouter. This means (1-5).
3437 * - If it is old entry (!newentry), set IsRouter. This means (7).
3438 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
3439 * A quetion arises for (1) case. (1) case has no lladdr in the
3440 * neighbor cache, this is similar to (6).
3441 * This case is rare but we figured that we MUST NOT set IsRouter.
3442 *
3443 * newentry olladdr lladdr llchange NS RS RA redir
3444 * D R
3445 * 0 n n -- (1) c ? s
3446 * 0 y n -- (2) c s s
3447 * 0 n y -- (3) c s s
3448 * 0 y y n (4) c s s
3449 * 0 y y y (5) c s s
3450 * 1 -- n -- (6) c c c s
3451 * 1 -- y -- (7) c c s c s
3452 *
3453 * (c=clear s=set)
3454 */
3455 switch (type & 0xff) {
3456 case ND_NEIGHBOR_SOLICIT:
3457 /*
3458 * New entry must have is_router flag cleared.
3459 */
3460 if (is_newentry) { /* (6-7) */
3461 ln->ln_router = 0;
3462 }
3463 break;
3464 case ND_REDIRECT:
3465 /*
3466 * If the ICMP message is a Redirect to a better router, always
3467 * set the is_router flag. Otherwise, if the entry is newly
3468 * created, then clear the flag. [RFC 4861, sec 8.3]
3469 */
3470 if (code == ND_REDIRECT_ROUTER) {
3471 ln->ln_router = 1;
3472 } else if (is_newentry) { /* (6-7) */
3473 ln->ln_router = 0;
3474 }
3475 break;
3476 case ND_ROUTER_SOLICIT:
3477 /*
3478 * is_router flag must always be cleared.
3479 */
3480 ln->ln_router = 0;
3481 break;
3482 case ND_ROUTER_ADVERT:
3483 /*
3484 * Mark an entry with lladdr as a router.
3485 */
3486 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
3487 (is_newentry && lladdr)) { /* (7) */
3488 ln->ln_router = 1;
3489 }
3490 break;
3491 }
3492
3493 if (do_update) {
3494 int route_ev_code = 0;
3495
3496 if (llchange) {
3497 route_ev_code = ROUTE_LLENTRY_CHANGED;
3498 } else {
3499 route_ev_code = ROUTE_LLENTRY_RESOLVED;
3500 }
3501
3502 /* Enqueue work item to invoke callback for this route entry */
3503 route_event_enqueue_nwk_wq_entry(rt, NULL, route_ev_code, NULL, TRUE);
3504
3505 if (ln->ln_router || (rt->rt_flags & RTF_ROUTER)) {
3506 struct radix_node_head *rnh = NULL;
3507 struct route_event rt_ev;
3508 route_event_init(&rt_ev, rt, NULL, llchange ? ROUTE_LLENTRY_CHANGED :
3509 ROUTE_LLENTRY_RESOLVED);
3510 /*
3511 * We already have a valid reference on rt.
3512 * The function frees that before returning.
3513 * We therefore don't need an extra reference here
3514 */
3515 RT_UNLOCK(rt);
3516 lck_mtx_lock(rnh_lock);
3517
3518 rnh = rt_tables[AF_INET6];
3519 if (rnh != NULL) {
3520 (void) rnh->rnh_walktree(rnh, route_event_walktree,
3521 (void *)&rt_ev);
3522 }
3523 lck_mtx_unlock(rnh_lock);
3524 RT_LOCK(rt);
3525 }
3526 }
3527
3528 /*
3529 * When the link-layer address of a router changes, select the
3530 * best router again. In particular, when the neighbor entry is newly
3531 * created, it might affect the selection policy.
3532 * Question: can we restrict the first condition to the "is_newentry"
3533 * case?
3534 *
3535 * Note: Perform default router selection even when we are a router,
3536 * if Scoped Routing is enabled.
3537 */
3538 if (do_update && ln->ln_router) {
3539 RT_REMREF_LOCKED(rt);
3540 RT_UNLOCK(rt);
3541 lck_mtx_lock(nd6_mutex);
3542 defrouter_select(ifp);
3543 lck_mtx_unlock(nd6_mutex);
3544 } else {
3545 RT_REMREF_LOCKED(rt);
3546 RT_UNLOCK(rt);
3547 }
3548 if (sched_timeout) {
3549 lck_mtx_lock(rnh_lock);
3550 nd6_sched_timeout(NULL, NULL);
3551 lck_mtx_unlock(rnh_lock);
3552 }
3553 }
3554
3555 static void
3556 nd6_slowtimo(void *arg)
3557 {
3558 #pragma unused(arg)
3559 struct nd_ifinfo *nd6if = NULL;
3560 struct ifnet *ifp = NULL;
3561
3562 ifnet_head_lock_shared();
3563 for (ifp = ifnet_head.tqh_first; ifp;
3564 ifp = ifp->if_link.tqe_next) {
3565 nd6if = ND_IFINFO(ifp);
3566 if ((NULL == nd6if) || (FALSE == nd6if->initialized)) {
3567 continue;
3568 }
3569
3570 lck_mtx_lock(&nd6if->lock);
3571 if (nd6if->basereachable && /* already initialized */
3572 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
3573 /*
3574 * Since reachable time rarely changes by router
3575 * advertisements, we SHOULD insure that a new random
3576 * value gets recomputed at least once every few hours.
3577 * (RFC 4861, 6.3.4)
3578 */
3579 nd6if->recalctm = nd6_recalc_reachtm_interval;
3580 nd6if->reachable =
3581 ND_COMPUTE_RTIME(nd6if->basereachable);
3582 }
3583 lck_mtx_unlock(&nd6if->lock);
3584 }
3585 ifnet_head_done();
3586 timeout(nd6_slowtimo, NULL, ND6_SLOWTIMER_INTERVAL * hz);
3587 }
3588
3589 int
3590 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
3591 struct sockaddr_in6 *dst, struct rtentry *hint0, struct flowadv *adv)
3592 {
3593 return nd6_output_list(ifp, origifp, m0, dst, hint0, adv);
3594 }
3595
3596 /*
3597 * nd6_output_list()
3598 *
3599 * Assumption: route determination for first packet can be correctly applied to
3600 * all packets in the chain.
3601 */
3602 #define senderr(e) { error = (e); goto bad; }
3603 int
3604 nd6_output_list(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
3605 struct sockaddr_in6 *dst, struct rtentry *hint0, struct flowadv *adv)
3606 {
3607 struct rtentry *rt = hint0, *hint = hint0;
3608 struct llinfo_nd6 *ln = NULL;
3609 int error = 0;
3610 uint64_t timenow;
3611 struct rtentry *rtrele = NULL;
3612 struct nd_ifinfo *ndi = NULL;
3613
3614 if (rt != NULL) {
3615 RT_LOCK_SPIN(rt);
3616 RT_ADDREF_LOCKED(rt);
3617 }
3618
3619 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr) || !nd6_need_cache(ifp)) {
3620 if (rt != NULL) {
3621 RT_UNLOCK(rt);
3622 }
3623 goto sendpkt;
3624 }
3625
3626 /*
3627 * Next hop determination. Because we may involve the gateway route
3628 * in addition to the original route, locking is rather complicated.
3629 * The general concept is that regardless of whether the route points
3630 * to the original route or to the gateway route, this routine takes
3631 * an extra reference on such a route. This extra reference will be
3632 * released at the end.
3633 *
3634 * Care must be taken to ensure that the "hint0" route never gets freed
3635 * via rtfree(), since the caller may have stored it inside a struct
3636 * route with a reference held for that placeholder.
3637 *
3638 * This logic is similar to, though not exactly the same as the one
3639 * used by route_to_gwroute().
3640 */
3641 if (rt != NULL) {
3642 /*
3643 * We have a reference to "rt" by now (or below via rtalloc1),
3644 * which will either be released or freed at the end of this
3645 * routine.
3646 */
3647 RT_LOCK_ASSERT_HELD(rt);
3648 if (!(rt->rt_flags & RTF_UP)) {
3649 RT_REMREF_LOCKED(rt);
3650 RT_UNLOCK(rt);
3651 if ((hint = rt = rtalloc1_scoped(SA(dst), 1, 0,
3652 ifp->if_index)) != NULL) {
3653 RT_LOCK_SPIN(rt);
3654 if (rt->rt_ifp != ifp) {
3655 /* XXX: loop care? */
3656 RT_UNLOCK(rt);
3657 error = nd6_output_list(ifp, origifp, m0,
3658 dst, rt, adv);
3659 rtfree(rt);
3660 return error;
3661 }
3662 } else {
3663 senderr(EHOSTUNREACH);
3664 }
3665 }
3666
3667 if (rt->rt_flags & RTF_GATEWAY) {
3668 struct rtentry *gwrt;
3669 struct in6_ifaddr *ia6 = NULL;
3670 struct sockaddr_in6 gw6;
3671
3672 rtgw_to_sa6(rt, &gw6);
3673 /*
3674 * Must drop rt_lock since nd6_is_addr_neighbor()
3675 * calls nd6_lookup() and acquires rnh_lock.
3676 */
3677 RT_UNLOCK(rt);
3678
3679 /*
3680 * We skip link-layer address resolution and NUD
3681 * if the gateway is not a neighbor from ND point
3682 * of view, regardless of the value of nd_ifinfo.flags.
3683 * The second condition is a bit tricky; we skip
3684 * if the gateway is our own address, which is
3685 * sometimes used to install a route to a p2p link.
3686 */
3687 if (!nd6_is_addr_neighbor(&gw6, ifp, 0) ||
3688 (ia6 = in6ifa_ifpwithaddr(ifp, &gw6.sin6_addr))) {
3689 /*
3690 * We allow this kind of tricky route only
3691 * when the outgoing interface is p2p.
3692 * XXX: we may need a more generic rule here.
3693 */
3694 if (ia6 != NULL) {
3695 IFA_REMREF(&ia6->ia_ifa);
3696 }
3697 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
3698 senderr(EHOSTUNREACH);
3699 }
3700 goto sendpkt;
3701 }
3702
3703 RT_LOCK_SPIN(rt);
3704 gw6 = *(SIN6(rt->rt_gateway));
3705
3706 /* If hint is now down, give up */
3707 if (!(rt->rt_flags & RTF_UP)) {
3708 RT_UNLOCK(rt);
3709 senderr(EHOSTUNREACH);
3710 }
3711
3712 /* If there's no gateway route, look it up */
3713 if ((gwrt = rt->rt_gwroute) == NULL) {
3714 RT_UNLOCK(rt);
3715 goto lookup;
3716 }
3717 /* Become a regular mutex */
3718 RT_CONVERT_LOCK(rt);
3719
3720 /*
3721 * Take gwrt's lock while holding route's lock;
3722 * this is okay since gwrt never points back
3723 * to rt, so no lock ordering issues.
3724 */
3725 RT_LOCK_SPIN(gwrt);
3726 if (!(gwrt->rt_flags & RTF_UP)) {
3727 rt->rt_gwroute = NULL;
3728 RT_UNLOCK(gwrt);
3729 RT_UNLOCK(rt);
3730 rtfree(gwrt);
3731 lookup:
3732 lck_mtx_lock(rnh_lock);
3733 gwrt = rtalloc1_scoped_locked(SA(&gw6), 1, 0,
3734 ifp->if_index);
3735
3736 RT_LOCK(rt);
3737 /*
3738 * Bail out if the route is down, no route
3739 * to gateway, circular route, or if the
3740 * gateway portion of "rt" has changed.
3741 */
3742 if (!(rt->rt_flags & RTF_UP) ||
3743 gwrt == NULL || gwrt == rt ||
3744 !equal(SA(&gw6), rt->rt_gateway)) {
3745 if (gwrt == rt) {
3746 RT_REMREF_LOCKED(gwrt);
3747 gwrt = NULL;
3748 }
3749 RT_UNLOCK(rt);
3750 if (gwrt != NULL) {
3751 rtfree_locked(gwrt);
3752 }
3753 lck_mtx_unlock(rnh_lock);
3754 senderr(EHOSTUNREACH);
3755 }
3756 VERIFY(gwrt != NULL);
3757 /*
3758 * Set gateway route; callee adds ref to gwrt;
3759 * gwrt has an extra ref from rtalloc1() for
3760 * this routine.
3761 */
3762 rt_set_gwroute(rt, rt_key(rt), gwrt);
3763 RT_UNLOCK(rt);
3764 lck_mtx_unlock(rnh_lock);
3765 /* Remember to release/free "rt" at the end */
3766 rtrele = rt;
3767 rt = gwrt;
3768 } else {
3769 RT_ADDREF_LOCKED(gwrt);
3770 RT_UNLOCK(gwrt);
3771 RT_UNLOCK(rt);
3772 /* Remember to release/free "rt" at the end */
3773 rtrele = rt;
3774 rt = gwrt;
3775 }
3776 VERIFY(rt == gwrt);
3777
3778 /*
3779 * This is an opportunity to revalidate the parent
3780 * route's gwroute, in case it now points to a dead
3781 * route entry. Parent route won't go away since the
3782 * clone (hint) holds a reference to it. rt == gwrt.
3783 */
3784 RT_LOCK_SPIN(hint);
3785 if ((hint->rt_flags & (RTF_WASCLONED | RTF_UP)) ==
3786 (RTF_WASCLONED | RTF_UP)) {
3787 struct rtentry *prt = hint->rt_parent;
3788 VERIFY(prt != NULL);
3789
3790 RT_CONVERT_LOCK(hint);
3791 RT_ADDREF(prt);
3792 RT_UNLOCK(hint);
3793 rt_revalidate_gwroute(prt, rt);
3794 RT_REMREF(prt);
3795 } else {
3796 RT_UNLOCK(hint);
3797 }
3798
3799 RT_LOCK_SPIN(rt);
3800 /* rt == gwrt; if it is now down, give up */
3801 if (!(rt->rt_flags & RTF_UP)) {
3802 RT_UNLOCK(rt);
3803 rtfree(rt);
3804 rt = NULL;
3805 /* "rtrele" == original "rt" */
3806 senderr(EHOSTUNREACH);
3807 }
3808 }
3809
3810 /* Become a regular mutex */
3811 RT_CONVERT_LOCK(rt);
3812 }
3813
3814 /*
3815 * Address resolution or Neighbor Unreachability Detection
3816 * for the next hop.
3817 * At this point, the destination of the packet must be a unicast
3818 * or an anycast address(i.e. not a multicast).
3819 */
3820
3821 /* Look up the neighbor cache for the nexthop */
3822 if (rt && (rt->rt_flags & RTF_LLINFO) != 0) {
3823 ln = rt->rt_llinfo;
3824 } else {
3825 struct sockaddr_in6 sin6;
3826 /*
3827 * Clear out Scope ID field in case it is set.
3828 */
3829 sin6 = *dst;
3830 sin6.sin6_scope_id = 0;
3831 /*
3832 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
3833 * the condition below is not very efficient. But we believe
3834 * it is tolerable, because this should be a rare case.
3835 * Must drop rt_lock since nd6_is_addr_neighbor() calls
3836 * nd6_lookup() and acquires rnh_lock.
3837 */
3838 if (rt != NULL) {
3839 RT_UNLOCK(rt);
3840 }
3841 if (nd6_is_addr_neighbor(&sin6, ifp, 0)) {
3842 /* "rtrele" may have been used, so clean up "rt" now */
3843 if (rt != NULL) {
3844 /* Don't free "hint0" */
3845 if (rt == hint0) {
3846 RT_REMREF(rt);
3847 } else {
3848 rtfree(rt);
3849 }
3850 }
3851 /* Callee returns a locked route upon success */
3852 rt = nd6_lookup(&dst->sin6_addr, 1, ifp, 0);
3853 if (rt != NULL) {
3854 RT_LOCK_ASSERT_HELD(rt);
3855 ln = rt->rt_llinfo;
3856 }
3857 } else if (rt != NULL) {
3858 RT_LOCK(rt);
3859 }
3860 }
3861
3862 if (!ln || !rt) {
3863 if (rt != NULL) {
3864 RT_UNLOCK(rt);
3865 }
3866 ndi = ND_IFINFO(ifp);
3867 VERIFY(ndi != NULL && ndi->initialized);
3868 lck_mtx_lock(&ndi->lock);
3869 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
3870 !(ndi->flags & ND6_IFF_PERFORMNUD)) {
3871 lck_mtx_unlock(&ndi->lock);
3872 log(LOG_DEBUG,
3873 "nd6_output: can't allocate llinfo for %s "
3874 "(ln=0x%llx, rt=0x%llx)\n",
3875 ip6_sprintf(&dst->sin6_addr),
3876 (uint64_t)VM_KERNEL_ADDRPERM(ln),
3877 (uint64_t)VM_KERNEL_ADDRPERM(rt));
3878 senderr(EIO); /* XXX: good error? */
3879 }
3880 lck_mtx_unlock(&ndi->lock);
3881
3882 goto sendpkt; /* send anyway */
3883 }
3884
3885 net_update_uptime();
3886 timenow = net_uptime();
3887
3888 /* We don't have to do link-layer address resolution on a p2p link. */
3889 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
3890 ln->ln_state < ND6_LLINFO_REACHABLE) {
3891 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
3892 ln_setexpire(ln, timenow + nd6_gctimer);
3893 }
3894
3895 /*
3896 * The first time we send a packet to a neighbor whose entry is
3897 * STALE, we have to change the state to DELAY and a sets a timer to
3898 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
3899 * neighbor unreachability detection on expiration.
3900 * (RFC 4861 7.3.3)
3901 */
3902 if (ln->ln_state == ND6_LLINFO_STALE) {
3903 ln->ln_asked = 0;
3904 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_DELAY);
3905 ln_setexpire(ln, timenow + nd6_delay);
3906 /* N.B.: we will re-arm the timer below. */
3907 _CASSERT(ND6_LLINFO_DELAY > ND6_LLINFO_INCOMPLETE);
3908 }
3909
3910 /*
3911 * If the neighbor cache entry has a state other than INCOMPLETE
3912 * (i.e. its link-layer address is already resolved), just
3913 * send the packet.
3914 */
3915 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
3916 RT_UNLOCK(rt);
3917 /*
3918 * Move this entry to the head of the queue so that it is
3919 * less likely for this entry to be a target of forced
3920 * garbage collection (see nd6_rtrequest()). Do this only
3921 * if the entry is non-permanent (as permanent ones will
3922 * never be purged), and if the number of active entries
3923 * is at least half of the threshold.
3924 */
3925 if (ln->ln_state == ND6_LLINFO_DELAY ||
3926 (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
3927 nd6_inuse >= (ip6_neighborgcthresh >> 1))) {
3928 lck_mtx_lock(rnh_lock);
3929 if (ln->ln_state == ND6_LLINFO_DELAY) {
3930 nd6_sched_timeout(NULL, NULL);
3931 }
3932 if (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
3933 nd6_inuse >= (ip6_neighborgcthresh >> 1)) {
3934 RT_LOCK_SPIN(rt);
3935 if (ln->ln_flags & ND6_LNF_IN_USE) {
3936 LN_DEQUEUE(ln);
3937 LN_INSERTHEAD(ln);
3938 }
3939 RT_UNLOCK(rt);
3940 }
3941 lck_mtx_unlock(rnh_lock);
3942 }
3943 goto sendpkt;
3944 }
3945
3946 /*
3947 * If this is a prefix proxy route, record the inbound interface
3948 * so that it can be excluded from the list of interfaces eligible
3949 * for forwarding the proxied NS in nd6_prproxy_ns_output().
3950 */
3951 if (rt->rt_flags & RTF_PROXY) {
3952 ln->ln_exclifp = ((origifp == ifp) ? NULL : origifp);
3953 }
3954
3955 /*
3956 * There is a neighbor cache entry, but no ethernet address
3957 * response yet. Replace the held mbuf (if any) with this
3958 * latest one.
3959 *
3960 * This code conforms to the rate-limiting rule described in Section
3961 * 7.2.2 of RFC 4861, because the timer is set correctly after sending
3962 * an NS below.
3963 */
3964 if (ln->ln_state == ND6_LLINFO_NOSTATE) {
3965 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_INCOMPLETE);
3966 }
3967 if (ln->ln_hold) {
3968 m_freem_list(ln->ln_hold);
3969 }
3970 ln->ln_hold = m0;
3971 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
3972 ln->ln_asked++;
3973 ndi = ND_IFINFO(ifp);
3974 VERIFY(ndi != NULL && ndi->initialized);
3975 lck_mtx_lock(&ndi->lock);
3976 ln_setexpire(ln, timenow + ndi->retrans / 1000);
3977 lck_mtx_unlock(&ndi->lock);
3978 RT_UNLOCK(rt);
3979 /* We still have a reference on rt (for ln) */
3980 if (ip6_forwarding) {
3981 nd6_prproxy_ns_output(ifp, origifp, NULL,
3982 &dst->sin6_addr, ln);
3983 } else {
3984 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, NULL);
3985 }
3986 lck_mtx_lock(rnh_lock);
3987 nd6_sched_timeout(NULL, NULL);
3988 lck_mtx_unlock(rnh_lock);
3989 } else {
3990 RT_UNLOCK(rt);
3991 }
3992 /*
3993 * Move this entry to the head of the queue so that it is
3994 * less likely for this entry to be a target of forced
3995 * garbage collection (see nd6_rtrequest()). Do this only
3996 * if the entry is non-permanent (as permanent ones will
3997 * never be purged), and if the number of active entries
3998 * is at least half of the threshold.
3999 */
4000 if (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
4001 nd6_inuse >= (ip6_neighborgcthresh >> 1)) {
4002 lck_mtx_lock(rnh_lock);
4003 RT_LOCK_SPIN(rt);
4004 if (ln->ln_flags & ND6_LNF_IN_USE) {
4005 LN_DEQUEUE(ln);
4006 LN_INSERTHEAD(ln);
4007 }
4008 /* Clean up "rt" now while we can */
4009 if (rt == hint0) {
4010 RT_REMREF_LOCKED(rt);
4011 RT_UNLOCK(rt);
4012 } else {
4013 RT_UNLOCK(rt);
4014 rtfree_locked(rt);
4015 }
4016 rt = NULL; /* "rt" has been taken care of */
4017 lck_mtx_unlock(rnh_lock);
4018 }
4019 error = 0;
4020 goto release;
4021
4022 sendpkt:
4023 if (rt != NULL) {
4024 RT_LOCK_ASSERT_NOTHELD(rt);
4025 }
4026
4027 /* discard the packet if IPv6 operation is disabled on the interface */
4028 if (ifp->if_eflags & IFEF_IPV6_DISABLED) {
4029 error = ENETDOWN; /* better error? */
4030 goto bad;
4031 }
4032
4033 if (ifp->if_flags & IFF_LOOPBACK) {
4034 /* forwarding rules require the original scope_id */
4035 m0->m_pkthdr.rcvif = origifp;
4036 error = dlil_output(origifp, PF_INET6, m0, (caddr_t)rt,
4037 SA(dst), 0, adv);
4038 goto release;
4039 } else {
4040 /* Do not allow loopback address to wind up on a wire */
4041 struct ip6_hdr *ip6 = mtod(m0, struct ip6_hdr *);
4042
4043 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
4044 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst))) {
4045 ip6stat.ip6s_badscope++;
4046 error = EADDRNOTAVAIL;
4047 goto bad;
4048 }
4049 }
4050
4051 if (rt != NULL) {
4052 RT_LOCK_SPIN(rt);
4053 /* Mark use timestamp */
4054 if (rt->rt_llinfo != NULL) {
4055 nd6_llreach_use(rt->rt_llinfo);
4056 }
4057 RT_UNLOCK(rt);
4058 }
4059
4060 struct mbuf *mcur = m0;
4061 uint32_t pktcnt = 0;
4062
4063 while (mcur) {
4064 if (hint != NULL && nstat_collect) {
4065 int scnt;
4066
4067 if ((mcur->m_pkthdr.csum_flags & CSUM_TSO_IPV6) &&
4068 (mcur->m_pkthdr.tso_segsz > 0)) {
4069 scnt = mcur->m_pkthdr.len / mcur->m_pkthdr.tso_segsz;
4070 } else {
4071 scnt = 1;
4072 }
4073
4074 nstat_route_tx(hint, scnt, mcur->m_pkthdr.len, 0);
4075 }
4076 pktcnt++;
4077
4078 mcur->m_pkthdr.rcvif = NULL;
4079 mcur = mcur->m_nextpkt;
4080 }
4081 if (pktcnt > ip6_maxchainsent) {
4082 ip6_maxchainsent = pktcnt;
4083 }
4084 error = dlil_output(ifp, PF_INET6, m0, (caddr_t)rt, SA(dst), 0, adv);
4085 goto release;
4086
4087 bad:
4088 if (m0 != NULL) {
4089 m_freem_list(m0);
4090 }
4091
4092 release:
4093 /* Clean up "rt" unless it's already been done */
4094 if (rt != NULL) {
4095 RT_LOCK_SPIN(rt);
4096 if (rt == hint0) {
4097 RT_REMREF_LOCKED(rt);
4098 RT_UNLOCK(rt);
4099 } else {
4100 RT_UNLOCK(rt);
4101 rtfree(rt);
4102 }
4103 }
4104 /* And now clean up "rtrele" if there is any */
4105 if (rtrele != NULL) {
4106 RT_LOCK_SPIN(rtrele);
4107 if (rtrele == hint0) {
4108 RT_REMREF_LOCKED(rtrele);
4109 RT_UNLOCK(rtrele);
4110 } else {
4111 RT_UNLOCK(rtrele);
4112 rtfree(rtrele);
4113 }
4114 }
4115 return error;
4116 }
4117 #undef senderr
4118
4119 int
4120 nd6_need_cache(struct ifnet *ifp)
4121 {
4122 /*
4123 * XXX: we currently do not make neighbor cache on any interface
4124 * other than ARCnet, Ethernet, FDDI and GIF.
4125 *
4126 * RFC2893 says:
4127 * - unidirectional tunnels needs no ND
4128 */
4129 switch (ifp->if_type) {
4130 case IFT_ARCNET:
4131 case IFT_ETHER:
4132 case IFT_FDDI:
4133 case IFT_IEEE1394:
4134 case IFT_L2VLAN:
4135 case IFT_IEEE8023ADLAG:
4136 #if IFT_IEEE80211
4137 case IFT_IEEE80211:
4138 #endif
4139 case IFT_GIF: /* XXX need more cases? */
4140 case IFT_PPP:
4141 #if IFT_TUNNEL
4142 case IFT_TUNNEL:
4143 #endif
4144 case IFT_BRIDGE:
4145 case IFT_CELLULAR:
4146 case IFT_6LOWPAN:
4147 return 1;
4148 default:
4149 return 0;
4150 }
4151 }
4152
4153 int
4154 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
4155 struct sockaddr *dst, u_char *desten)
4156 {
4157 int i;
4158 struct sockaddr_dl *sdl;
4159
4160 if (m->m_flags & M_MCAST) {
4161 switch (ifp->if_type) {
4162 case IFT_ETHER:
4163 case IFT_FDDI:
4164 case IFT_L2VLAN:
4165 case IFT_IEEE8023ADLAG:
4166 #if IFT_IEEE80211
4167 case IFT_IEEE80211:
4168 #endif
4169 case IFT_BRIDGE:
4170 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, desten);
4171 return 1;
4172 case IFT_IEEE1394:
4173 for (i = 0; i < ifp->if_addrlen; i++) {
4174 desten[i] = ~0;
4175 }
4176 return 1;
4177 case IFT_ARCNET:
4178 *desten = 0;
4179 return 1;
4180 default:
4181 return 0; /* caller will free mbuf */
4182 }
4183 }
4184
4185 if (rt == NULL) {
4186 /* this could happen, if we could not allocate memory */
4187 return 0; /* caller will free mbuf */
4188 }
4189 RT_LOCK(rt);
4190 if (rt->rt_gateway->sa_family != AF_LINK) {
4191 printf("nd6_storelladdr: something odd happens\n");
4192 RT_UNLOCK(rt);
4193 return 0; /* caller will free mbuf */
4194 }
4195 sdl = SDL(rt->rt_gateway);
4196 if (sdl->sdl_alen == 0) {
4197 /* this should be impossible, but we bark here for debugging */
4198 printf("nd6_storelladdr: sdl_alen == 0\n");
4199 RT_UNLOCK(rt);
4200 return 0; /* caller will free mbuf */
4201 }
4202
4203 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
4204 RT_UNLOCK(rt);
4205 return 1;
4206 }
4207
4208 /*
4209 * This is the ND pre-output routine; care must be taken to ensure that
4210 * the "hint" route never gets freed via rtfree(), since the caller may
4211 * have stored it inside a struct route with a reference held for that
4212 * placeholder.
4213 */
4214 errno_t
4215 nd6_lookup_ipv6(ifnet_t ifp, const struct sockaddr_in6 *ip6_dest,
4216 struct sockaddr_dl *ll_dest, size_t ll_dest_len, route_t hint,
4217 mbuf_t packet)
4218 {
4219 route_t route = hint;
4220 errno_t result = 0;
4221 struct sockaddr_dl *sdl = NULL;
4222 size_t copy_len;
4223
4224 if (ifp == NULL || ip6_dest == NULL) {
4225 return EINVAL;
4226 }
4227
4228 if (ip6_dest->sin6_family != AF_INET6) {
4229 return EAFNOSUPPORT;
4230 }
4231
4232 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) {
4233 return ENETDOWN;
4234 }
4235
4236 if (hint != NULL) {
4237 /*
4238 * Callee holds a reference on the route and returns
4239 * with the route entry locked, upon success.
4240 */
4241 result = route_to_gwroute((const struct sockaddr *)ip6_dest,
4242 hint, &route);
4243 if (result != 0) {
4244 return result;
4245 }
4246 if (route != NULL) {
4247 RT_LOCK_ASSERT_HELD(route);
4248 }
4249 }
4250
4251 if ((packet != NULL && (packet->m_flags & M_MCAST) != 0) ||
4252 ((ifp->if_flags & IFF_MULTICAST) &&
4253 IN6_IS_ADDR_MULTICAST(&ip6_dest->sin6_addr))) {
4254 if (route != NULL) {
4255 RT_UNLOCK(route);
4256 }
4257 result = dlil_resolve_multi(ifp,
4258 (const struct sockaddr *)ip6_dest,
4259 SA(ll_dest), ll_dest_len);
4260 if (route != NULL) {
4261 RT_LOCK(route);
4262 }
4263 goto release;
4264 } else if (route == NULL) {
4265 /*
4266 * rdar://24596652
4267 * For unicast, lookup existing ND6 entries but
4268 * do not trigger a resolution
4269 */
4270 lck_mtx_lock(rnh_lock);
4271 route = rt_lookup(TRUE,
4272 __DECONST(struct sockaddr *, ip6_dest), NULL,
4273 rt_tables[AF_INET6], ifp->if_index);
4274 lck_mtx_unlock(rnh_lock);
4275
4276 if (route != NULL) {
4277 RT_LOCK(route);
4278 }
4279 }
4280
4281 if (route == NULL) {
4282 /*
4283 * This could happen, if we could not allocate memory or
4284 * if route_to_gwroute() didn't return a route.
4285 */
4286 result = ENOBUFS;
4287 goto release;
4288 }
4289
4290 if (route->rt_gateway->sa_family != AF_LINK) {
4291 printf("%s: route %s on %s%d gateway address not AF_LINK\n",
4292 __func__, ip6_sprintf(&ip6_dest->sin6_addr),
4293 route->rt_ifp->if_name, route->rt_ifp->if_unit);
4294 result = EADDRNOTAVAIL;
4295 goto release;
4296 }
4297
4298 sdl = SDL(route->rt_gateway);
4299 if (sdl->sdl_alen == 0) {
4300 /* this should be impossible, but we bark here for debugging */
4301 printf("%s: route %s on %s%d sdl_alen == 0\n", __func__,
4302 ip6_sprintf(&ip6_dest->sin6_addr), route->rt_ifp->if_name,
4303 route->rt_ifp->if_unit);
4304 result = EHOSTUNREACH;
4305 goto release;
4306 }
4307
4308 copy_len = sdl->sdl_len <= ll_dest_len ? sdl->sdl_len : ll_dest_len;
4309 bcopy(sdl, ll_dest, copy_len);
4310
4311 release:
4312 if (route != NULL) {
4313 if (route == hint) {
4314 RT_REMREF_LOCKED(route);
4315 RT_UNLOCK(route);
4316 } else {
4317 RT_UNLOCK(route);
4318 rtfree(route);
4319 }
4320 }
4321 return result;
4322 }
4323
4324 #if (DEVELOPMENT || DEBUG)
4325
4326 static int sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS;
4327 SYSCTL_PROC(_net_inet6_icmp6, OID_AUTO, nd6_lookup_ipv6,
4328 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0,
4329 sysctl_nd6_lookup_ipv6, "S", "");
4330
4331 int
4332 sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
4333 {
4334 #pragma unused(oidp, arg1, arg2)
4335 int error = 0;
4336 struct nd6_lookup_ipv6_args nd6_lookup_ipv6_args;
4337 ifnet_t ifp = NULL;
4338
4339 /*
4340 * Only root can lookup MAC addresses
4341 */
4342 error = proc_suser(current_proc());
4343 if (error != 0) {
4344 nd6log0(error, "%s: proc_suser() error %d\n",
4345 __func__, error);
4346 goto done;
4347 }
4348 if (req->oldptr == USER_ADDR_NULL) {
4349 req->oldidx = sizeof(struct nd6_lookup_ipv6_args);
4350 }
4351 if (req->newptr == USER_ADDR_NULL) {
4352 goto done;
4353 }
4354 if (req->oldlen != sizeof(struct nd6_lookup_ipv6_args) ||
4355 req->newlen != sizeof(struct nd6_lookup_ipv6_args)) {
4356 error = EINVAL;
4357 nd6log0(error, "%s: bad req, error %d\n",
4358 __func__, error);
4359 goto done;
4360 }
4361 error = SYSCTL_IN(req, &nd6_lookup_ipv6_args,
4362 sizeof(struct nd6_lookup_ipv6_args));
4363 if (error != 0) {
4364 nd6log0(error, "%s: SYSCTL_IN() error %d\n",
4365 __func__, error);
4366 goto done;
4367 }
4368
4369 if (nd6_lookup_ipv6_args.ll_dest_len > sizeof(nd6_lookup_ipv6_args.ll_dest_)) {
4370 error = EINVAL;
4371 nd6log0(error, "%s: bad ll_dest_len, error %d\n",
4372 __func__, error);
4373 goto done;
4374 }
4375
4376 /* Make sure to terminate the string */
4377 nd6_lookup_ipv6_args.ifname[IFNAMSIZ - 1] = 0;
4378
4379 error = ifnet_find_by_name(nd6_lookup_ipv6_args.ifname, &ifp);
4380 if (error != 0) {
4381 nd6log0(error, "%s: ifnet_find_by_name() error %d\n",
4382 __func__, error);
4383 goto done;
4384 }
4385
4386 error = nd6_lookup_ipv6(ifp, &nd6_lookup_ipv6_args.ip6_dest,
4387 &nd6_lookup_ipv6_args.ll_dest_._sdl,
4388 nd6_lookup_ipv6_args.ll_dest_len, NULL, NULL);
4389 if (error != 0) {
4390 nd6log0(error, "%s: nd6_lookup_ipv6() error %d\n",
4391 __func__, error);
4392 goto done;
4393 }
4394
4395 error = SYSCTL_OUT(req, &nd6_lookup_ipv6_args,
4396 sizeof(struct nd6_lookup_ipv6_args));
4397 if (error != 0) {
4398 nd6log0(error, "%s: SYSCTL_OUT() error %d\n",
4399 __func__, error);
4400 goto done;
4401 }
4402 done:
4403 return error;
4404 }
4405
4406 #endif /* (DEVELOPEMENT || DEBUG) */
4407
4408 int
4409 nd6_setifinfo(struct ifnet *ifp, u_int32_t before, u_int32_t after)
4410 {
4411 uint32_t b, a;
4412 int err = 0;
4413
4414 /*
4415 * Handle ND6_IFF_IFDISABLED
4416 */
4417 if ((before & ND6_IFF_IFDISABLED) ||
4418 (after & ND6_IFF_IFDISABLED)) {
4419 b = (before & ND6_IFF_IFDISABLED);
4420 a = (after & ND6_IFF_IFDISABLED);
4421
4422 if (b != a && (err = nd6_if_disable(ifp,
4423 ((int32_t)(a - b) > 0))) != 0) {
4424 goto done;
4425 }
4426 }
4427
4428 /*
4429 * Handle ND6_IFF_PROXY_PREFIXES
4430 */
4431 if ((before & ND6_IFF_PROXY_PREFIXES) ||
4432 (after & ND6_IFF_PROXY_PREFIXES)) {
4433 b = (before & ND6_IFF_PROXY_PREFIXES);
4434 a = (after & ND6_IFF_PROXY_PREFIXES);
4435
4436 if (b != a && (err = nd6_if_prproxy(ifp,
4437 ((int32_t)(a - b) > 0))) != 0) {
4438 goto done;
4439 }
4440 }
4441 done:
4442 return err;
4443 }
4444
4445 /*
4446 * Enable/disable IPv6 on an interface, called as part of
4447 * setting/clearing ND6_IFF_IFDISABLED, or during DAD failure.
4448 */
4449 int
4450 nd6_if_disable(struct ifnet *ifp, boolean_t enable)
4451 {
4452 ifnet_lock_shared(ifp);
4453 if (enable) {
4454 ifp->if_eflags |= IFEF_IPV6_DISABLED;
4455 } else {
4456 ifp->if_eflags &= ~IFEF_IPV6_DISABLED;
4457 }
4458 ifnet_lock_done(ifp);
4459
4460 return 0;
4461 }
4462
4463 static int
4464 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
4465 {
4466 #pragma unused(oidp, arg1, arg2)
4467 char pbuf[MAX_IPv6_STR_LEN];
4468 struct nd_defrouter *dr;
4469 int error = 0;
4470
4471 if (req->newptr != USER_ADDR_NULL) {
4472 return EPERM;
4473 }
4474
4475 /* XXX Handle mapped defrouter entries */
4476 lck_mtx_lock(nd6_mutex);
4477 if (proc_is64bit(req->p)) {
4478 struct in6_defrouter_64 d;
4479
4480 bzero(&d, sizeof(d));
4481 d.rtaddr.sin6_family = AF_INET6;
4482 d.rtaddr.sin6_len = sizeof(d.rtaddr);
4483
4484 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
4485 d.rtaddr.sin6_addr = dr->rtaddr;
4486 if (in6_recoverscope(&d.rtaddr,
4487 &dr->rtaddr, dr->ifp) != 0) {
4488 log(LOG_ERR, "scope error in default router "
4489 "list (%s)\n", inet_ntop(AF_INET6,
4490 &dr->rtaddr, pbuf, sizeof(pbuf)));
4491 }
4492 d.flags = dr->flags;
4493 d.stateflags = dr->stateflags;
4494 d.rtlifetime = dr->rtlifetime;
4495 d.expire = nddr_getexpire(dr);
4496 d.if_index = dr->ifp->if_index;
4497 error = SYSCTL_OUT(req, &d, sizeof(d));
4498 if (error != 0) {
4499 break;
4500 }
4501 }
4502 } else {
4503 struct in6_defrouter_32 d;
4504
4505 bzero(&d, sizeof(d));
4506 d.rtaddr.sin6_family = AF_INET6;
4507 d.rtaddr.sin6_len = sizeof(d.rtaddr);
4508
4509 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
4510 d.rtaddr.sin6_addr = dr->rtaddr;
4511 if (in6_recoverscope(&d.rtaddr,
4512 &dr->rtaddr, dr->ifp) != 0) {
4513 log(LOG_ERR, "scope error in default router "
4514 "list (%s)\n", inet_ntop(AF_INET6,
4515 &dr->rtaddr, pbuf, sizeof(pbuf)));
4516 }
4517 d.flags = dr->flags;
4518 d.stateflags = dr->stateflags;
4519 d.rtlifetime = dr->rtlifetime;
4520 d.expire = nddr_getexpire(dr);
4521 d.if_index = dr->ifp->if_index;
4522 error = SYSCTL_OUT(req, &d, sizeof(d));
4523 if (error != 0) {
4524 break;
4525 }
4526 }
4527 }
4528 lck_mtx_unlock(nd6_mutex);
4529 return error;
4530 }
4531
4532 static int
4533 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
4534 {
4535 #pragma unused(oidp, arg1, arg2)
4536 char pbuf[MAX_IPv6_STR_LEN];
4537 struct nd_pfxrouter *pfr;
4538 struct sockaddr_in6 s6;
4539 struct nd_prefix *pr;
4540 int error = 0;
4541
4542 if (req->newptr != USER_ADDR_NULL) {
4543 return EPERM;
4544 }
4545
4546 bzero(&s6, sizeof(s6));
4547 s6.sin6_family = AF_INET6;
4548 s6.sin6_len = sizeof(s6);
4549
4550 /* XXX Handle mapped defrouter entries */
4551 lck_mtx_lock(nd6_mutex);
4552 if (proc_is64bit(req->p)) {
4553 struct in6_prefix_64 p;
4554
4555 bzero(&p, sizeof(p));
4556 p.origin = PR_ORIG_RA;
4557
4558 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
4559 NDPR_LOCK(pr);
4560 p.prefix = pr->ndpr_prefix;
4561 if (in6_recoverscope(&p.prefix,
4562 &pr->ndpr_prefix.sin6_addr, pr->ndpr_ifp) != 0) {
4563 log(LOG_ERR, "scope error in "
4564 "prefix list (%s)\n", inet_ntop(AF_INET6,
4565 &p.prefix.sin6_addr, pbuf, sizeof(pbuf)));
4566 }
4567 p.raflags = pr->ndpr_raf;
4568 p.prefixlen = pr->ndpr_plen;
4569 p.vltime = pr->ndpr_vltime;
4570 p.pltime = pr->ndpr_pltime;
4571 p.if_index = pr->ndpr_ifp->if_index;
4572 p.expire = ndpr_getexpire(pr);
4573 p.refcnt = pr->ndpr_addrcnt;
4574 p.flags = pr->ndpr_stateflags;
4575 p.advrtrs = 0;
4576 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
4577 p.advrtrs++;
4578 error = SYSCTL_OUT(req, &p, sizeof(p));
4579 if (error != 0) {
4580 NDPR_UNLOCK(pr);
4581 break;
4582 }
4583 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
4584 s6.sin6_addr = pfr->router->rtaddr;
4585 if (in6_recoverscope(&s6, &pfr->router->rtaddr,
4586 pfr->router->ifp) != 0) {
4587 log(LOG_ERR,
4588 "scope error in prefix list (%s)\n",
4589 inet_ntop(AF_INET6, &s6.sin6_addr,
4590 pbuf, sizeof(pbuf)));
4591 }
4592 error = SYSCTL_OUT(req, &s6, sizeof(s6));
4593 if (error != 0) {
4594 break;
4595 }
4596 }
4597 NDPR_UNLOCK(pr);
4598 if (error != 0) {
4599 break;
4600 }
4601 }
4602 } else {
4603 struct in6_prefix_32 p;
4604
4605 bzero(&p, sizeof(p));
4606 p.origin = PR_ORIG_RA;
4607
4608 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
4609 NDPR_LOCK(pr);
4610 p.prefix = pr->ndpr_prefix;
4611 if (in6_recoverscope(&p.prefix,
4612 &pr->ndpr_prefix.sin6_addr, pr->ndpr_ifp) != 0) {
4613 log(LOG_ERR,
4614 "scope error in prefix list (%s)\n",
4615 inet_ntop(AF_INET6, &p.prefix.sin6_addr,
4616 pbuf, sizeof(pbuf)));
4617 }
4618 p.raflags = pr->ndpr_raf;
4619 p.prefixlen = pr->ndpr_plen;
4620 p.vltime = pr->ndpr_vltime;
4621 p.pltime = pr->ndpr_pltime;
4622 p.if_index = pr->ndpr_ifp->if_index;
4623 p.expire = ndpr_getexpire(pr);
4624 p.refcnt = pr->ndpr_addrcnt;
4625 p.flags = pr->ndpr_stateflags;
4626 p.advrtrs = 0;
4627 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
4628 p.advrtrs++;
4629 error = SYSCTL_OUT(req, &p, sizeof(p));
4630 if (error != 0) {
4631 NDPR_UNLOCK(pr);
4632 break;
4633 }
4634 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
4635 s6.sin6_addr = pfr->router->rtaddr;
4636 if (in6_recoverscope(&s6, &pfr->router->rtaddr,
4637 pfr->router->ifp) != 0) {
4638 log(LOG_ERR,
4639 "scope error in prefix list (%s)\n",
4640 inet_ntop(AF_INET6, &s6.sin6_addr,
4641 pbuf, sizeof(pbuf)));
4642 }
4643 error = SYSCTL_OUT(req, &s6, sizeof(s6));
4644 if (error != 0) {
4645 break;
4646 }
4647 }
4648 NDPR_UNLOCK(pr);
4649 if (error != 0) {
4650 break;
4651 }
4652 }
4653 }
4654 lck_mtx_unlock(nd6_mutex);
4655
4656 return error;
4657 }
4658
4659 void
4660 in6_ifaddr_set_dadprogress(struct in6_ifaddr *ia)
4661 {
4662 struct ifnet* ifp = ia->ia_ifp;
4663 uint32_t flags = IN6_IFF_TENTATIVE;
4664 uint32_t optdad = nd6_optimistic_dad;
4665 struct nd_ifinfo *ndi = NULL;
4666
4667 ndi = ND_IFINFO(ifp);
4668 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
4669 if (!(ndi->flags & ND6_IFF_DAD)) {
4670 return;
4671 }
4672
4673 if (optdad) {
4674 if ((ifp->if_eflags & IFEF_IPV6_ROUTER) != 0) {
4675 optdad = 0;
4676 } else {
4677 lck_mtx_lock(&ndi->lock);
4678 if ((ndi->flags & ND6_IFF_REPLICATED) != 0) {
4679 optdad = 0;
4680 }
4681 lck_mtx_unlock(&ndi->lock);
4682 }
4683 }
4684
4685 if (optdad) {
4686 if ((optdad & ND6_OPTIMISTIC_DAD_LINKLOCAL) &&
4687 IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
4688 flags = IN6_IFF_OPTIMISTIC;
4689 } else if ((optdad & ND6_OPTIMISTIC_DAD_AUTOCONF) &&
4690 (ia->ia6_flags & IN6_IFF_AUTOCONF)) {
4691 if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
4692 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) {
4693 flags = IN6_IFF_OPTIMISTIC;
4694 }
4695 } else if (ia->ia6_flags & IN6_IFF_SECURED) {
4696 if (optdad & ND6_OPTIMISTIC_DAD_SECURED) {
4697 flags = IN6_IFF_OPTIMISTIC;
4698 }
4699 } else {
4700 /*
4701 * Keeping the behavior for temp and CGA
4702 * SLAAC addresses to have a knob for optimistic
4703 * DAD.
4704 * Other than that if ND6_OPTIMISTIC_DAD_AUTOCONF
4705 * is set, we should default to optimistic
4706 * DAD.
4707 * For now this means SLAAC addresses with interface
4708 * identifier derived from modified EUI-64 bit
4709 * identifiers.
4710 */
4711 flags = IN6_IFF_OPTIMISTIC;
4712 }
4713 } else if ((optdad & ND6_OPTIMISTIC_DAD_DYNAMIC) &&
4714 (ia->ia6_flags & IN6_IFF_DYNAMIC)) {
4715 if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
4716 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) {
4717 flags = IN6_IFF_OPTIMISTIC;
4718 }
4719 } else {
4720 flags = IN6_IFF_OPTIMISTIC;
4721 }
4722 } else if ((optdad & ND6_OPTIMISTIC_DAD_MANUAL) &&
4723 (ia->ia6_flags & IN6_IFF_OPTIMISTIC)) {
4724 /*
4725 * rdar://17483438
4726 * Bypass tentative for address assignments
4727 * not covered above (e.g. manual) upon request
4728 */
4729 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) &&
4730 !(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
4731 !(ia->ia6_flags & IN6_IFF_DYNAMIC)) {
4732 flags = IN6_IFF_OPTIMISTIC;
4733 }
4734 }
4735 }
4736
4737 ia->ia6_flags &= ~(IN6_IFF_DUPLICATED | IN6_IFF_DADPROGRESS);
4738 ia->ia6_flags |= flags;
4739
4740 nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
4741 __func__,
4742 ip6_sprintf(&ia->ia_addr.sin6_addr),
4743 if_name(ia->ia_ifp),
4744 ia->ia6_flags);
4745 }