]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/nd6.c
xnu-6153.121.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / nd6.c
1 /*
2 * Copyright (c) 2000-2019 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 for (ia6 = in6_ifaddrs; ia6; ia6 = nia6) {
1271 int oldflags = ia6->ia6_flags;
1272 ap->found++;
1273 nia6 = ia6->ia_next;
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 ia = in6_ifaddrs;
2995 while (ia != NULL) {
2996 IFA_LOCK(&ia->ia_ifa);
2997 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) {
2998 IFA_UNLOCK(&ia->ia_ifa);
2999 ia = ia->ia_next;
3000 continue;
3001 }
3002
3003 if (ia->ia6_ndpr == pr) {
3004 IFA_ADDREF_LOCKED(&ia->ia_ifa);
3005 IFA_UNLOCK(&ia->ia_ifa);
3006 lck_rw_done(&in6_ifaddr_rwlock);
3007 lck_mtx_unlock(nd6_mutex);
3008 in6_purgeaddr(&ia->ia_ifa);
3009 IFA_REMREF(&ia->ia_ifa);
3010 lck_mtx_lock(nd6_mutex);
3011 lck_rw_lock_exclusive(
3012 &in6_ifaddr_rwlock);
3013 /*
3014 * Purging the address caused
3015 * in6_ifaddr_rwlock to be
3016 * dropped and
3017 * reacquired; therefore search again
3018 * from the beginning of in6_ifaddrs.
3019 * The same applies for the prefix list.
3020 */
3021 ia = in6_ifaddrs;
3022 iterate_pfxlist_again = true;
3023 continue;
3024 }
3025 IFA_UNLOCK(&ia->ia_ifa);
3026 ia = ia->ia_next;
3027 }
3028 lck_rw_done(&in6_ifaddr_rwlock);
3029 NDPR_LOCK(pr);
3030 prelist_remove(pr);
3031 NDPR_UNLOCK(pr);
3032 pfxlist_onlink_check();
3033 NDPR_REMREF(pr);
3034 if (iterate_pfxlist_again) {
3035 next = nd_prefix.lh_first;
3036 }
3037 }
3038 lck_mtx_unlock(nd6_mutex);
3039 break;
3040 }
3041
3042 case SIOCSRTRFLUSH_IN6: { /* struct in6_ifreq */
3043 /* flush all the default routers */
3044 struct nd_defrouter *next;
3045 struct nd_drhead nd_defrouter_tmp;
3046
3047 TAILQ_INIT(&nd_defrouter_tmp);
3048 lck_mtx_lock(nd6_mutex);
3049 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
3050 /*
3051 * The first entry of the list may be stored in
3052 * the routing table, so we'll delete it later.
3053 */
3054 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
3055 next = TAILQ_NEXT(dr, dr_entry);
3056 if (ifp == lo_ifp || dr->ifp == ifp) {
3057 /*
3058 * Remove the entry from default router list
3059 * and add it to the temp list.
3060 * nd_defrouter_tmp will be a local temporary
3061 * list as no one else can get the same
3062 * removed entry once it is removed from default
3063 * router list.
3064 * Remove the reference after calling defrtrlist_de
3065 */
3066 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
3067 TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
3068 }
3069 }
3070
3071 dr = TAILQ_FIRST(&nd_defrouter);
3072 if (ifp == lo_ifp ||
3073 dr->ifp == ifp) {
3074 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
3075 TAILQ_INSERT_TAIL(&nd_defrouter_tmp, dr, dr_entry);
3076 }
3077 }
3078
3079 /*
3080 * Keep the following separate from the above iteration of
3081 * nd_defrouter because it's not safe to call
3082 * defrtrlist_del while iterating global default
3083 * router list. Global list has to be traversed
3084 * while holding nd6_mutex throughout.
3085 *
3086 * The following call to defrtrlist_del should be
3087 * safe as we are iterating a local list of
3088 * default routers.
3089 */
3090 TAILQ_FOREACH_SAFE(dr, &nd_defrouter_tmp, dr_entry, next) {
3091 TAILQ_REMOVE(&nd_defrouter_tmp, dr, dr_entry);
3092 defrtrlist_del(dr);
3093 NDDR_REMREF(dr); /* remove list reference */
3094 }
3095 lck_mtx_unlock(nd6_mutex);
3096 break;
3097 }
3098
3099 case SIOCGNBRINFO_IN6_32: { /* struct in6_nbrinfo_32 */
3100 struct llinfo_nd6 *ln;
3101 struct in6_nbrinfo_32 nbi_32;
3102 struct in6_addr nb_addr; /* make local for safety */
3103
3104 bcopy(data, &nbi_32, sizeof(nbi_32));
3105 nb_addr = nbi_32.addr;
3106 /*
3107 * XXX: KAME specific hack for scoped addresses
3108 * XXXX: for other scopes than link-local?
3109 */
3110 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32.addr) ||
3111 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32.addr)) {
3112 u_int16_t *idp =
3113 (u_int16_t *)(void *)&nb_addr.s6_addr[2];
3114
3115 if (*idp == 0) {
3116 *idp = htons(ifp->if_index);
3117 }
3118 }
3119
3120 /* Callee returns a locked route upon success */
3121 if ((rt = nd6_lookup(&nb_addr, 0, ifp, 0)) == NULL) {
3122 error = EINVAL;
3123 break;
3124 }
3125 RT_LOCK_ASSERT_HELD(rt);
3126 ln = rt->rt_llinfo;
3127 nbi_32.state = ln->ln_state;
3128 nbi_32.asked = ln->ln_asked;
3129 nbi_32.isrouter = ln->ln_router;
3130 nbi_32.expire = ln_getexpire(ln);
3131 RT_REMREF_LOCKED(rt);
3132 RT_UNLOCK(rt);
3133 bcopy(&nbi_32, data, sizeof(nbi_32));
3134 break;
3135 }
3136
3137 case SIOCGNBRINFO_IN6_64: { /* struct in6_nbrinfo_64 */
3138 struct llinfo_nd6 *ln;
3139 struct in6_nbrinfo_64 nbi_64;
3140 struct in6_addr nb_addr; /* make local for safety */
3141
3142 bcopy(data, &nbi_64, sizeof(nbi_64));
3143 nb_addr = nbi_64.addr;
3144 /*
3145 * XXX: KAME specific hack for scoped addresses
3146 * XXXX: for other scopes than link-local?
3147 */
3148 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64.addr) ||
3149 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64.addr)) {
3150 u_int16_t *idp =
3151 (u_int16_t *)(void *)&nb_addr.s6_addr[2];
3152
3153 if (*idp == 0) {
3154 *idp = htons(ifp->if_index);
3155 }
3156 }
3157
3158 /* Callee returns a locked route upon success */
3159 if ((rt = nd6_lookup(&nb_addr, 0, ifp, 0)) == NULL) {
3160 error = EINVAL;
3161 break;
3162 }
3163 RT_LOCK_ASSERT_HELD(rt);
3164 ln = rt->rt_llinfo;
3165 nbi_64.state = ln->ln_state;
3166 nbi_64.asked = ln->ln_asked;
3167 nbi_64.isrouter = ln->ln_router;
3168 nbi_64.expire = ln_getexpire(ln);
3169 RT_REMREF_LOCKED(rt);
3170 RT_UNLOCK(rt);
3171 bcopy(&nbi_64, data, sizeof(nbi_64));
3172 break;
3173 }
3174
3175 case SIOCGDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
3176 case SIOCGDEFIFACE_IN6_64: { /* struct in6_ndifreq_64 */
3177 struct in6_ndifreq_64 *ndif_64 =
3178 (struct in6_ndifreq_64 *)(void *)data;
3179 struct in6_ndifreq_32 *ndif_32 =
3180 (struct in6_ndifreq_32 *)(void *)data;
3181
3182 if (cmd == SIOCGDEFIFACE_IN6_64) {
3183 u_int64_t j = nd6_defifindex;
3184 __nochk_bcopy(&j, &ndif_64->ifindex, sizeof(j));
3185 } else {
3186 bcopy(&nd6_defifindex, &ndif_32->ifindex,
3187 sizeof(u_int32_t));
3188 }
3189 break;
3190 }
3191
3192 case SIOCSDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
3193 case SIOCSDEFIFACE_IN6_64: { /* struct in6_ndifreq_64 */
3194 struct in6_ndifreq_64 *ndif_64 =
3195 (struct in6_ndifreq_64 *)(void *)data;
3196 struct in6_ndifreq_32 *ndif_32 =
3197 (struct in6_ndifreq_32 *)(void *)data;
3198 u_int32_t idx;
3199
3200 if (cmd == SIOCSDEFIFACE_IN6_64) {
3201 u_int64_t j;
3202 __nochk_bcopy(&ndif_64->ifindex, &j, sizeof(j));
3203 idx = (u_int32_t)j;
3204 } else {
3205 bcopy(&ndif_32->ifindex, &idx, sizeof(idx));
3206 }
3207
3208 error = nd6_setdefaultiface(idx);
3209 return error;
3210 /* NOTREACHED */
3211 }
3212 case SIOCGIFCGAPREP_IN6:
3213 case SIOCSIFCGAPREP_IN6:
3214 {
3215 struct in6_cgareq *p_cgareq =
3216 (struct in6_cgareq *)(void *)data;
3217 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
3218
3219 struct in6_cga_modifier *req_cga_mod =
3220 &(p_cgareq->cgar_cgaprep.cga_modifier);
3221 struct in6_cga_modifier *ndi_cga_mod = NULL;
3222
3223 if ((NULL == ndi) || !ndi->initialized) {
3224 error = EINVAL;
3225 break;
3226 }
3227
3228 lck_mtx_lock(&ndi->lock);
3229 ndi_cga_mod = &(ndi->local_cga_modifier);
3230
3231 if (cmd == SIOCSIFCGAPREP_IN6) {
3232 bcopy(req_cga_mod, ndi_cga_mod, sizeof(*ndi_cga_mod));
3233 ndi->cga_initialized = TRUE;
3234 } else {
3235 bcopy(ndi_cga_mod, req_cga_mod, sizeof(*req_cga_mod));
3236 }
3237
3238 lck_mtx_unlock(&ndi->lock);
3239 return error;
3240 /* NOTREACHED */
3241 }
3242 }
3243 return error;
3244 }
3245
3246 /*
3247 * Create neighbor cache entry and cache link-layer address,
3248 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
3249 */
3250 void
3251 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
3252 int lladdrlen, int type, int code)
3253 {
3254 #pragma unused(lladdrlen)
3255 struct rtentry *rt = NULL;
3256 struct llinfo_nd6 *ln = NULL;
3257 int is_newentry;
3258 struct sockaddr_dl *sdl = NULL;
3259 int do_update;
3260 int olladdr;
3261 int llchange;
3262 int newstate = 0;
3263 uint64_t timenow;
3264 boolean_t sched_timeout = FALSE;
3265 struct nd_ifinfo *ndi = NULL;
3266
3267 if (ifp == NULL) {
3268 panic("ifp == NULL in nd6_cache_lladdr");
3269 }
3270 if (from == NULL) {
3271 panic("from == NULL in nd6_cache_lladdr");
3272 }
3273
3274 /* nothing must be updated for unspecified address */
3275 if (IN6_IS_ADDR_UNSPECIFIED(from)) {
3276 return;
3277 }
3278
3279 /*
3280 * Validation about ifp->if_addrlen and lladdrlen must be done in
3281 * the caller.
3282 */
3283 timenow = net_uptime();
3284
3285 rt = nd6_lookup(from, 0, ifp, 0);
3286 if (rt == NULL) {
3287 if ((rt = nd6_lookup(from, 1, ifp, 0)) == NULL) {
3288 return;
3289 }
3290 RT_LOCK_ASSERT_HELD(rt);
3291 is_newentry = 1;
3292 } else {
3293 RT_LOCK_ASSERT_HELD(rt);
3294 /* do nothing if static ndp is set */
3295 if (rt->rt_flags & RTF_STATIC) {
3296 RT_REMREF_LOCKED(rt);
3297 RT_UNLOCK(rt);
3298 return;
3299 }
3300 is_newentry = 0;
3301 }
3302
3303 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
3304 fail:
3305 RT_UNLOCK(rt);
3306 nd6_free(rt);
3307 rtfree(rt);
3308 return;
3309 }
3310 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
3311 if (ln == NULL) {
3312 goto fail;
3313 }
3314 if (rt->rt_gateway == NULL) {
3315 goto fail;
3316 }
3317 if (rt->rt_gateway->sa_family != AF_LINK) {
3318 goto fail;
3319 }
3320 sdl = SDL(rt->rt_gateway);
3321
3322 olladdr = (sdl->sdl_alen) ? 1 : 0;
3323 if (olladdr && lladdr) {
3324 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) {
3325 llchange = 1;
3326 } else {
3327 llchange = 0;
3328 }
3329 } else {
3330 llchange = 0;
3331 }
3332
3333 /*
3334 * newentry olladdr lladdr llchange (*=record)
3335 * 0 n n -- (1)
3336 * 0 y n -- (2)
3337 * 0 n y -- (3) * STALE
3338 * 0 y y n (4) *
3339 * 0 y y y (5) * STALE
3340 * 1 -- n -- (6) NOSTATE(= PASSIVE)
3341 * 1 -- y -- (7) * STALE
3342 */
3343
3344 if (lladdr != NULL) { /* (3-5) and (7) */
3345 /*
3346 * Record source link-layer address
3347 * XXX is it dependent to ifp->if_type?
3348 */
3349 sdl->sdl_alen = ifp->if_addrlen;
3350 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
3351
3352 /* cache the gateway (sender HW) address */
3353 nd6_llreach_alloc(rt, ifp, LLADDR(sdl), sdl->sdl_alen, FALSE);
3354 }
3355
3356 if (is_newentry == 0) {
3357 if ((!olladdr && lladdr != NULL) || /* (3) */
3358 (olladdr && lladdr != NULL && llchange)) { /* (5) */
3359 do_update = 1;
3360 newstate = ND6_LLINFO_STALE;
3361 } else { /* (1-2,4) */
3362 do_update = 0;
3363 }
3364 } else {
3365 do_update = 1;
3366 if (lladdr == NULL) { /* (6) */
3367 newstate = ND6_LLINFO_NOSTATE;
3368 } else { /* (7) */
3369 newstate = ND6_LLINFO_STALE;
3370 }
3371 }
3372
3373 /*
3374 * For interface's that do not perform NUD
3375 * neighbor cache entres must always be marked
3376 * reachable with no expiry
3377 */
3378 ndi = ND_IFINFO(ifp);
3379 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
3380
3381 if (ndi && !(ndi->flags & ND6_IFF_PERFORMNUD)) {
3382 newstate = ND6_LLINFO_REACHABLE;
3383 ln_setexpire(ln, 0);
3384 }
3385
3386 if (do_update) {
3387 /*
3388 * Update the state of the neighbor cache.
3389 */
3390 ND6_CACHE_STATE_TRANSITION(ln, newstate);
3391
3392 if ((ln->ln_state == ND6_LLINFO_STALE) ||
3393 (ln->ln_state == ND6_LLINFO_REACHABLE)) {
3394 struct mbuf *m = ln->ln_hold;
3395 /*
3396 * XXX: since nd6_output() below will cause
3397 * state tansition to DELAY and reset the timer,
3398 * we must set the timer now, although it is actually
3399 * meaningless.
3400 */
3401 if (ln->ln_state == ND6_LLINFO_STALE) {
3402 ln_setexpire(ln, timenow + nd6_gctimer);
3403 }
3404
3405 ln->ln_hold = NULL;
3406 if (m != NULL) {
3407 struct sockaddr_in6 sin6;
3408
3409 rtkey_to_sa6(rt, &sin6);
3410 /*
3411 * we assume ifp is not a p2p here, so just
3412 * set the 2nd argument as the 1st one.
3413 */
3414 RT_UNLOCK(rt);
3415 nd6_output_list(ifp, ifp, m, &sin6, rt, NULL);
3416 RT_LOCK(rt);
3417 }
3418 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
3419 /* probe right away */
3420 ln_setexpire(ln, timenow);
3421 sched_timeout = TRUE;
3422 }
3423 }
3424
3425 /*
3426 * ICMP6 type dependent behavior.
3427 *
3428 * NS: clear IsRouter if new entry
3429 * RS: clear IsRouter
3430 * RA: set IsRouter if there's lladdr
3431 * redir: clear IsRouter if new entry
3432 *
3433 * RA case, (1):
3434 * The spec says that we must set IsRouter in the following cases:
3435 * - If lladdr exist, set IsRouter. This means (1-5).
3436 * - If it is old entry (!newentry), set IsRouter. This means (7).
3437 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
3438 * A quetion arises for (1) case. (1) case has no lladdr in the
3439 * neighbor cache, this is similar to (6).
3440 * This case is rare but we figured that we MUST NOT set IsRouter.
3441 *
3442 * newentry olladdr lladdr llchange NS RS RA redir
3443 * D R
3444 * 0 n n -- (1) c ? s
3445 * 0 y n -- (2) c s s
3446 * 0 n y -- (3) c s s
3447 * 0 y y n (4) c s s
3448 * 0 y y y (5) c s s
3449 * 1 -- n -- (6) c c c s
3450 * 1 -- y -- (7) c c s c s
3451 *
3452 * (c=clear s=set)
3453 */
3454 switch (type & 0xff) {
3455 case ND_NEIGHBOR_SOLICIT:
3456 /*
3457 * New entry must have is_router flag cleared.
3458 */
3459 if (is_newentry) { /* (6-7) */
3460 ln->ln_router = 0;
3461 }
3462 break;
3463 case ND_REDIRECT:
3464 /*
3465 * If the ICMP message is a Redirect to a better router, always
3466 * set the is_router flag. Otherwise, if the entry is newly
3467 * created, then clear the flag. [RFC 4861, sec 8.3]
3468 */
3469 if (code == ND_REDIRECT_ROUTER) {
3470 ln->ln_router = 1;
3471 } else if (is_newentry) { /* (6-7) */
3472 ln->ln_router = 0;
3473 }
3474 break;
3475 case ND_ROUTER_SOLICIT:
3476 /*
3477 * is_router flag must always be cleared.
3478 */
3479 ln->ln_router = 0;
3480 break;
3481 case ND_ROUTER_ADVERT:
3482 /*
3483 * Mark an entry with lladdr as a router.
3484 */
3485 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
3486 (is_newentry && lladdr)) { /* (7) */
3487 ln->ln_router = 1;
3488 }
3489 break;
3490 }
3491
3492 if (do_update) {
3493 int route_ev_code = 0;
3494
3495 if (llchange) {
3496 route_ev_code = ROUTE_LLENTRY_CHANGED;
3497 } else {
3498 route_ev_code = ROUTE_LLENTRY_RESOLVED;
3499 }
3500
3501 /* Enqueue work item to invoke callback for this route entry */
3502 route_event_enqueue_nwk_wq_entry(rt, NULL, route_ev_code, NULL, TRUE);
3503
3504 if (ln->ln_router || (rt->rt_flags & RTF_ROUTER)) {
3505 struct radix_node_head *rnh = NULL;
3506 struct route_event rt_ev;
3507 route_event_init(&rt_ev, rt, NULL, llchange ? ROUTE_LLENTRY_CHANGED :
3508 ROUTE_LLENTRY_RESOLVED);
3509 /*
3510 * We already have a valid reference on rt.
3511 * The function frees that before returning.
3512 * We therefore don't need an extra reference here
3513 */
3514 RT_UNLOCK(rt);
3515 lck_mtx_lock(rnh_lock);
3516
3517 rnh = rt_tables[AF_INET6];
3518 if (rnh != NULL) {
3519 (void) rnh->rnh_walktree(rnh, route_event_walktree,
3520 (void *)&rt_ev);
3521 }
3522 lck_mtx_unlock(rnh_lock);
3523 RT_LOCK(rt);
3524 }
3525 }
3526
3527 /*
3528 * When the link-layer address of a router changes, select the
3529 * best router again. In particular, when the neighbor entry is newly
3530 * created, it might affect the selection policy.
3531 * Question: can we restrict the first condition to the "is_newentry"
3532 * case?
3533 *
3534 * Note: Perform default router selection even when we are a router,
3535 * if Scoped Routing is enabled.
3536 */
3537 if (do_update && ln->ln_router) {
3538 RT_REMREF_LOCKED(rt);
3539 RT_UNLOCK(rt);
3540 lck_mtx_lock(nd6_mutex);
3541 defrouter_select(ifp);
3542 lck_mtx_unlock(nd6_mutex);
3543 } else {
3544 RT_REMREF_LOCKED(rt);
3545 RT_UNLOCK(rt);
3546 }
3547 if (sched_timeout) {
3548 lck_mtx_lock(rnh_lock);
3549 nd6_sched_timeout(NULL, NULL);
3550 lck_mtx_unlock(rnh_lock);
3551 }
3552 }
3553
3554 static void
3555 nd6_slowtimo(void *arg)
3556 {
3557 #pragma unused(arg)
3558 struct nd_ifinfo *nd6if = NULL;
3559 struct ifnet *ifp = NULL;
3560
3561 ifnet_head_lock_shared();
3562 for (ifp = ifnet_head.tqh_first; ifp;
3563 ifp = ifp->if_link.tqe_next) {
3564 nd6if = ND_IFINFO(ifp);
3565 if ((NULL == nd6if) || (FALSE == nd6if->initialized)) {
3566 continue;
3567 }
3568
3569 lck_mtx_lock(&nd6if->lock);
3570 if (nd6if->basereachable && /* already initialized */
3571 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
3572 /*
3573 * Since reachable time rarely changes by router
3574 * advertisements, we SHOULD insure that a new random
3575 * value gets recomputed at least once every few hours.
3576 * (RFC 4861, 6.3.4)
3577 */
3578 nd6if->recalctm = nd6_recalc_reachtm_interval;
3579 nd6if->reachable =
3580 ND_COMPUTE_RTIME(nd6if->basereachable);
3581 }
3582 lck_mtx_unlock(&nd6if->lock);
3583 }
3584 ifnet_head_done();
3585 timeout(nd6_slowtimo, NULL, ND6_SLOWTIMER_INTERVAL * hz);
3586 }
3587
3588 int
3589 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
3590 struct sockaddr_in6 *dst, struct rtentry *hint0, struct flowadv *adv)
3591 {
3592 return nd6_output_list(ifp, origifp, m0, dst, hint0, adv);
3593 }
3594
3595 /*
3596 * nd6_output_list()
3597 *
3598 * Assumption: route determination for first packet can be correctly applied to
3599 * all packets in the chain.
3600 */
3601 #define senderr(e) { error = (e); goto bad; }
3602 int
3603 nd6_output_list(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
3604 struct sockaddr_in6 *dst, struct rtentry *hint0, struct flowadv *adv)
3605 {
3606 struct rtentry *rt = hint0, *hint = hint0;
3607 struct llinfo_nd6 *ln = NULL;
3608 int error = 0;
3609 uint64_t timenow;
3610 struct rtentry *rtrele = NULL;
3611 struct nd_ifinfo *ndi = NULL;
3612
3613 if (rt != NULL) {
3614 RT_LOCK_SPIN(rt);
3615 RT_ADDREF_LOCKED(rt);
3616 }
3617
3618 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr) || !nd6_need_cache(ifp)) {
3619 if (rt != NULL) {
3620 RT_UNLOCK(rt);
3621 }
3622 goto sendpkt;
3623 }
3624
3625 /*
3626 * Next hop determination. Because we may involve the gateway route
3627 * in addition to the original route, locking is rather complicated.
3628 * The general concept is that regardless of whether the route points
3629 * to the original route or to the gateway route, this routine takes
3630 * an extra reference on such a route. This extra reference will be
3631 * released at the end.
3632 *
3633 * Care must be taken to ensure that the "hint0" route never gets freed
3634 * via rtfree(), since the caller may have stored it inside a struct
3635 * route with a reference held for that placeholder.
3636 *
3637 * This logic is similar to, though not exactly the same as the one
3638 * used by route_to_gwroute().
3639 */
3640 if (rt != NULL) {
3641 /*
3642 * We have a reference to "rt" by now (or below via rtalloc1),
3643 * which will either be released or freed at the end of this
3644 * routine.
3645 */
3646 RT_LOCK_ASSERT_HELD(rt);
3647 if (!(rt->rt_flags & RTF_UP)) {
3648 RT_REMREF_LOCKED(rt);
3649 RT_UNLOCK(rt);
3650 if ((hint = rt = rtalloc1_scoped(SA(dst), 1, 0,
3651 ifp->if_index)) != NULL) {
3652 RT_LOCK_SPIN(rt);
3653 if (rt->rt_ifp != ifp) {
3654 /* XXX: loop care? */
3655 RT_UNLOCK(rt);
3656 error = nd6_output_list(ifp, origifp, m0,
3657 dst, rt, adv);
3658 rtfree(rt);
3659 return error;
3660 }
3661 } else {
3662 senderr(EHOSTUNREACH);
3663 }
3664 }
3665
3666 if (rt->rt_flags & RTF_GATEWAY) {
3667 struct rtentry *gwrt;
3668 struct in6_ifaddr *ia6 = NULL;
3669 struct sockaddr_in6 gw6;
3670
3671 rtgw_to_sa6(rt, &gw6);
3672 /*
3673 * Must drop rt_lock since nd6_is_addr_neighbor()
3674 * calls nd6_lookup() and acquires rnh_lock.
3675 */
3676 RT_UNLOCK(rt);
3677
3678 /*
3679 * We skip link-layer address resolution and NUD
3680 * if the gateway is not a neighbor from ND point
3681 * of view, regardless of the value of nd_ifinfo.flags.
3682 * The second condition is a bit tricky; we skip
3683 * if the gateway is our own address, which is
3684 * sometimes used to install a route to a p2p link.
3685 */
3686 if (!nd6_is_addr_neighbor(&gw6, ifp, 0) ||
3687 (ia6 = in6ifa_ifpwithaddr(ifp, &gw6.sin6_addr))) {
3688 /*
3689 * We allow this kind of tricky route only
3690 * when the outgoing interface is p2p.
3691 * XXX: we may need a more generic rule here.
3692 */
3693 if (ia6 != NULL) {
3694 IFA_REMREF(&ia6->ia_ifa);
3695 }
3696 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
3697 senderr(EHOSTUNREACH);
3698 }
3699 goto sendpkt;
3700 }
3701
3702 RT_LOCK_SPIN(rt);
3703 gw6 = *(SIN6(rt->rt_gateway));
3704
3705 /* If hint is now down, give up */
3706 if (!(rt->rt_flags & RTF_UP)) {
3707 RT_UNLOCK(rt);
3708 senderr(EHOSTUNREACH);
3709 }
3710
3711 /* If there's no gateway route, look it up */
3712 if ((gwrt = rt->rt_gwroute) == NULL) {
3713 RT_UNLOCK(rt);
3714 goto lookup;
3715 }
3716 /* Become a regular mutex */
3717 RT_CONVERT_LOCK(rt);
3718
3719 /*
3720 * Take gwrt's lock while holding route's lock;
3721 * this is okay since gwrt never points back
3722 * to rt, so no lock ordering issues.
3723 */
3724 RT_LOCK_SPIN(gwrt);
3725 if (!(gwrt->rt_flags & RTF_UP)) {
3726 rt->rt_gwroute = NULL;
3727 RT_UNLOCK(gwrt);
3728 RT_UNLOCK(rt);
3729 rtfree(gwrt);
3730 lookup:
3731 lck_mtx_lock(rnh_lock);
3732 gwrt = rtalloc1_scoped_locked(SA(&gw6), 1, 0,
3733 ifp->if_index);
3734
3735 RT_LOCK(rt);
3736 /*
3737 * Bail out if the route is down, no route
3738 * to gateway, circular route, or if the
3739 * gateway portion of "rt" has changed.
3740 */
3741 if (!(rt->rt_flags & RTF_UP) ||
3742 gwrt == NULL || gwrt == rt ||
3743 !equal(SA(&gw6), rt->rt_gateway)) {
3744 if (gwrt == rt) {
3745 RT_REMREF_LOCKED(gwrt);
3746 gwrt = NULL;
3747 }
3748 RT_UNLOCK(rt);
3749 if (gwrt != NULL) {
3750 rtfree_locked(gwrt);
3751 }
3752 lck_mtx_unlock(rnh_lock);
3753 senderr(EHOSTUNREACH);
3754 }
3755 VERIFY(gwrt != NULL);
3756 /*
3757 * Set gateway route; callee adds ref to gwrt;
3758 * gwrt has an extra ref from rtalloc1() for
3759 * this routine.
3760 */
3761 rt_set_gwroute(rt, rt_key(rt), gwrt);
3762 RT_UNLOCK(rt);
3763 lck_mtx_unlock(rnh_lock);
3764 /* Remember to release/free "rt" at the end */
3765 rtrele = rt;
3766 rt = gwrt;
3767 } else {
3768 RT_ADDREF_LOCKED(gwrt);
3769 RT_UNLOCK(gwrt);
3770 RT_UNLOCK(rt);
3771 /* Remember to release/free "rt" at the end */
3772 rtrele = rt;
3773 rt = gwrt;
3774 }
3775 VERIFY(rt == gwrt);
3776
3777 /*
3778 * This is an opportunity to revalidate the parent
3779 * route's gwroute, in case it now points to a dead
3780 * route entry. Parent route won't go away since the
3781 * clone (hint) holds a reference to it. rt == gwrt.
3782 */
3783 RT_LOCK_SPIN(hint);
3784 if ((hint->rt_flags & (RTF_WASCLONED | RTF_UP)) ==
3785 (RTF_WASCLONED | RTF_UP)) {
3786 struct rtentry *prt = hint->rt_parent;
3787 VERIFY(prt != NULL);
3788
3789 RT_CONVERT_LOCK(hint);
3790 RT_ADDREF(prt);
3791 RT_UNLOCK(hint);
3792 rt_revalidate_gwroute(prt, rt);
3793 RT_REMREF(prt);
3794 } else {
3795 RT_UNLOCK(hint);
3796 }
3797
3798 RT_LOCK_SPIN(rt);
3799 /* rt == gwrt; if it is now down, give up */
3800 if (!(rt->rt_flags & RTF_UP)) {
3801 RT_UNLOCK(rt);
3802 rtfree(rt);
3803 rt = NULL;
3804 /* "rtrele" == original "rt" */
3805 senderr(EHOSTUNREACH);
3806 }
3807 }
3808
3809 /* Become a regular mutex */
3810 RT_CONVERT_LOCK(rt);
3811 }
3812
3813 /*
3814 * Address resolution or Neighbor Unreachability Detection
3815 * for the next hop.
3816 * At this point, the destination of the packet must be a unicast
3817 * or an anycast address(i.e. not a multicast).
3818 */
3819
3820 /* Look up the neighbor cache for the nexthop */
3821 if (rt && (rt->rt_flags & RTF_LLINFO) != 0) {
3822 ln = rt->rt_llinfo;
3823 } else {
3824 struct sockaddr_in6 sin6;
3825 /*
3826 * Clear out Scope ID field in case it is set.
3827 */
3828 sin6 = *dst;
3829 sin6.sin6_scope_id = 0;
3830 /*
3831 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
3832 * the condition below is not very efficient. But we believe
3833 * it is tolerable, because this should be a rare case.
3834 * Must drop rt_lock since nd6_is_addr_neighbor() calls
3835 * nd6_lookup() and acquires rnh_lock.
3836 */
3837 if (rt != NULL) {
3838 RT_UNLOCK(rt);
3839 }
3840 if (nd6_is_addr_neighbor(&sin6, ifp, 0)) {
3841 /* "rtrele" may have been used, so clean up "rt" now */
3842 if (rt != NULL) {
3843 /* Don't free "hint0" */
3844 if (rt == hint0) {
3845 RT_REMREF(rt);
3846 } else {
3847 rtfree(rt);
3848 }
3849 }
3850 /* Callee returns a locked route upon success */
3851 rt = nd6_lookup(&dst->sin6_addr, 1, ifp, 0);
3852 if (rt != NULL) {
3853 RT_LOCK_ASSERT_HELD(rt);
3854 ln = rt->rt_llinfo;
3855 }
3856 } else if (rt != NULL) {
3857 RT_LOCK(rt);
3858 }
3859 }
3860
3861 if (!ln || !rt) {
3862 if (rt != NULL) {
3863 RT_UNLOCK(rt);
3864 }
3865 ndi = ND_IFINFO(ifp);
3866 VERIFY(ndi != NULL && ndi->initialized);
3867 lck_mtx_lock(&ndi->lock);
3868 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
3869 !(ndi->flags & ND6_IFF_PERFORMNUD)) {
3870 lck_mtx_unlock(&ndi->lock);
3871 log(LOG_DEBUG,
3872 "nd6_output: can't allocate llinfo for %s "
3873 "(ln=0x%llx, rt=0x%llx)\n",
3874 ip6_sprintf(&dst->sin6_addr),
3875 (uint64_t)VM_KERNEL_ADDRPERM(ln),
3876 (uint64_t)VM_KERNEL_ADDRPERM(rt));
3877 senderr(EIO); /* XXX: good error? */
3878 }
3879 lck_mtx_unlock(&ndi->lock);
3880
3881 goto sendpkt; /* send anyway */
3882 }
3883
3884 net_update_uptime();
3885 timenow = net_uptime();
3886
3887 /* We don't have to do link-layer address resolution on a p2p link. */
3888 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
3889 ln->ln_state < ND6_LLINFO_REACHABLE) {
3890 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
3891 ln_setexpire(ln, timenow + nd6_gctimer);
3892 }
3893
3894 /*
3895 * The first time we send a packet to a neighbor whose entry is
3896 * STALE, we have to change the state to DELAY and a sets a timer to
3897 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
3898 * neighbor unreachability detection on expiration.
3899 * (RFC 4861 7.3.3)
3900 */
3901 if (ln->ln_state == ND6_LLINFO_STALE) {
3902 ln->ln_asked = 0;
3903 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_DELAY);
3904 ln_setexpire(ln, timenow + nd6_delay);
3905 /* N.B.: we will re-arm the timer below. */
3906 _CASSERT(ND6_LLINFO_DELAY > ND6_LLINFO_INCOMPLETE);
3907 }
3908
3909 /*
3910 * If the neighbor cache entry has a state other than INCOMPLETE
3911 * (i.e. its link-layer address is already resolved), just
3912 * send the packet.
3913 */
3914 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
3915 RT_UNLOCK(rt);
3916 /*
3917 * Move this entry to the head of the queue so that it is
3918 * less likely for this entry to be a target of forced
3919 * garbage collection (see nd6_rtrequest()). Do this only
3920 * if the entry is non-permanent (as permanent ones will
3921 * never be purged), and if the number of active entries
3922 * is at least half of the threshold.
3923 */
3924 if (ln->ln_state == ND6_LLINFO_DELAY ||
3925 (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
3926 nd6_inuse >= (ip6_neighborgcthresh >> 1))) {
3927 lck_mtx_lock(rnh_lock);
3928 if (ln->ln_state == ND6_LLINFO_DELAY) {
3929 nd6_sched_timeout(NULL, NULL);
3930 }
3931 if (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
3932 nd6_inuse >= (ip6_neighborgcthresh >> 1)) {
3933 RT_LOCK_SPIN(rt);
3934 if (ln->ln_flags & ND6_LNF_IN_USE) {
3935 LN_DEQUEUE(ln);
3936 LN_INSERTHEAD(ln);
3937 }
3938 RT_UNLOCK(rt);
3939 }
3940 lck_mtx_unlock(rnh_lock);
3941 }
3942 goto sendpkt;
3943 }
3944
3945 /*
3946 * If this is a prefix proxy route, record the inbound interface
3947 * so that it can be excluded from the list of interfaces eligible
3948 * for forwarding the proxied NS in nd6_prproxy_ns_output().
3949 */
3950 if (rt->rt_flags & RTF_PROXY) {
3951 ln->ln_exclifp = ((origifp == ifp) ? NULL : origifp);
3952 }
3953
3954 /*
3955 * There is a neighbor cache entry, but no ethernet address
3956 * response yet. Replace the held mbuf (if any) with this
3957 * latest one.
3958 *
3959 * This code conforms to the rate-limiting rule described in Section
3960 * 7.2.2 of RFC 4861, because the timer is set correctly after sending
3961 * an NS below.
3962 */
3963 if (ln->ln_state == ND6_LLINFO_NOSTATE) {
3964 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_INCOMPLETE);
3965 }
3966 if (ln->ln_hold) {
3967 m_freem_list(ln->ln_hold);
3968 }
3969 ln->ln_hold = m0;
3970 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
3971 ln->ln_asked++;
3972 ndi = ND_IFINFO(ifp);
3973 VERIFY(ndi != NULL && ndi->initialized);
3974 lck_mtx_lock(&ndi->lock);
3975 ln_setexpire(ln, timenow + ndi->retrans / 1000);
3976 lck_mtx_unlock(&ndi->lock);
3977 RT_UNLOCK(rt);
3978 /* We still have a reference on rt (for ln) */
3979 if (ip6_forwarding) {
3980 nd6_prproxy_ns_output(ifp, origifp, NULL,
3981 &dst->sin6_addr, ln);
3982 } else {
3983 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, NULL);
3984 }
3985 lck_mtx_lock(rnh_lock);
3986 nd6_sched_timeout(NULL, NULL);
3987 lck_mtx_unlock(rnh_lock);
3988 } else {
3989 RT_UNLOCK(rt);
3990 }
3991 /*
3992 * Move this entry to the head of the queue so that it is
3993 * less likely for this entry to be a target of forced
3994 * garbage collection (see nd6_rtrequest()). Do this only
3995 * if the entry is non-permanent (as permanent ones will
3996 * never be purged), and if the number of active entries
3997 * is at least half of the threshold.
3998 */
3999 if (ln->ln_expire != 0 && ip6_neighborgcthresh > 0 &&
4000 nd6_inuse >= (ip6_neighborgcthresh >> 1)) {
4001 lck_mtx_lock(rnh_lock);
4002 RT_LOCK_SPIN(rt);
4003 if (ln->ln_flags & ND6_LNF_IN_USE) {
4004 LN_DEQUEUE(ln);
4005 LN_INSERTHEAD(ln);
4006 }
4007 /* Clean up "rt" now while we can */
4008 if (rt == hint0) {
4009 RT_REMREF_LOCKED(rt);
4010 RT_UNLOCK(rt);
4011 } else {
4012 RT_UNLOCK(rt);
4013 rtfree_locked(rt);
4014 }
4015 rt = NULL; /* "rt" has been taken care of */
4016 lck_mtx_unlock(rnh_lock);
4017 }
4018 error = 0;
4019 goto release;
4020
4021 sendpkt:
4022 if (rt != NULL) {
4023 RT_LOCK_ASSERT_NOTHELD(rt);
4024 }
4025
4026 /* discard the packet if IPv6 operation is disabled on the interface */
4027 if (ifp->if_eflags & IFEF_IPV6_DISABLED) {
4028 error = ENETDOWN; /* better error? */
4029 goto bad;
4030 }
4031
4032 if (ifp->if_flags & IFF_LOOPBACK) {
4033 /* forwarding rules require the original scope_id */
4034 m0->m_pkthdr.rcvif = origifp;
4035 error = dlil_output(origifp, PF_INET6, m0, (caddr_t)rt,
4036 SA(dst), 0, adv);
4037 goto release;
4038 } else {
4039 /* Do not allow loopback address to wind up on a wire */
4040 struct ip6_hdr *ip6 = mtod(m0, struct ip6_hdr *);
4041
4042 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
4043 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst))) {
4044 ip6stat.ip6s_badscope++;
4045 error = EADDRNOTAVAIL;
4046 goto bad;
4047 }
4048 }
4049
4050 if (rt != NULL) {
4051 RT_LOCK_SPIN(rt);
4052 /* Mark use timestamp */
4053 if (rt->rt_llinfo != NULL) {
4054 nd6_llreach_use(rt->rt_llinfo);
4055 }
4056 RT_UNLOCK(rt);
4057 }
4058
4059 struct mbuf *mcur = m0;
4060 uint32_t pktcnt = 0;
4061
4062 while (mcur) {
4063 if (hint != NULL && nstat_collect) {
4064 int scnt;
4065
4066 if ((mcur->m_pkthdr.csum_flags & CSUM_TSO_IPV6) &&
4067 (mcur->m_pkthdr.tso_segsz > 0)) {
4068 scnt = mcur->m_pkthdr.len / mcur->m_pkthdr.tso_segsz;
4069 } else {
4070 scnt = 1;
4071 }
4072
4073 nstat_route_tx(hint, scnt, mcur->m_pkthdr.len, 0);
4074 }
4075 pktcnt++;
4076
4077 mcur->m_pkthdr.rcvif = NULL;
4078 mcur = mcur->m_nextpkt;
4079 }
4080 if (pktcnt > ip6_maxchainsent) {
4081 ip6_maxchainsent = pktcnt;
4082 }
4083 error = dlil_output(ifp, PF_INET6, m0, (caddr_t)rt, SA(dst), 0, adv);
4084 goto release;
4085
4086 bad:
4087 if (m0 != NULL) {
4088 m_freem_list(m0);
4089 }
4090
4091 release:
4092 /* Clean up "rt" unless it's already been done */
4093 if (rt != NULL) {
4094 RT_LOCK_SPIN(rt);
4095 if (rt == hint0) {
4096 RT_REMREF_LOCKED(rt);
4097 RT_UNLOCK(rt);
4098 } else {
4099 RT_UNLOCK(rt);
4100 rtfree(rt);
4101 }
4102 }
4103 /* And now clean up "rtrele" if there is any */
4104 if (rtrele != NULL) {
4105 RT_LOCK_SPIN(rtrele);
4106 if (rtrele == hint0) {
4107 RT_REMREF_LOCKED(rtrele);
4108 RT_UNLOCK(rtrele);
4109 } else {
4110 RT_UNLOCK(rtrele);
4111 rtfree(rtrele);
4112 }
4113 }
4114 return error;
4115 }
4116 #undef senderr
4117
4118 int
4119 nd6_need_cache(struct ifnet *ifp)
4120 {
4121 /*
4122 * XXX: we currently do not make neighbor cache on any interface
4123 * other than ARCnet, Ethernet, FDDI and GIF.
4124 *
4125 * RFC2893 says:
4126 * - unidirectional tunnels needs no ND
4127 */
4128 switch (ifp->if_type) {
4129 case IFT_ARCNET:
4130 case IFT_ETHER:
4131 case IFT_FDDI:
4132 case IFT_IEEE1394:
4133 case IFT_L2VLAN:
4134 case IFT_IEEE8023ADLAG:
4135 #if IFT_IEEE80211
4136 case IFT_IEEE80211:
4137 #endif
4138 case IFT_GIF: /* XXX need more cases? */
4139 case IFT_PPP:
4140 #if IFT_TUNNEL
4141 case IFT_TUNNEL:
4142 #endif
4143 case IFT_BRIDGE:
4144 case IFT_CELLULAR:
4145 case IFT_6LOWPAN:
4146 return 1;
4147 default:
4148 return 0;
4149 }
4150 }
4151
4152 int
4153 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
4154 struct sockaddr *dst, u_char *desten)
4155 {
4156 int i;
4157 struct sockaddr_dl *sdl;
4158
4159 if (m->m_flags & M_MCAST) {
4160 switch (ifp->if_type) {
4161 case IFT_ETHER:
4162 case IFT_FDDI:
4163 case IFT_L2VLAN:
4164 case IFT_IEEE8023ADLAG:
4165 #if IFT_IEEE80211
4166 case IFT_IEEE80211:
4167 #endif
4168 case IFT_BRIDGE:
4169 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, desten);
4170 return 1;
4171 case IFT_IEEE1394:
4172 for (i = 0; i < ifp->if_addrlen; i++) {
4173 desten[i] = ~0;
4174 }
4175 return 1;
4176 case IFT_ARCNET:
4177 *desten = 0;
4178 return 1;
4179 default:
4180 return 0; /* caller will free mbuf */
4181 }
4182 }
4183
4184 if (rt == NULL) {
4185 /* this could happen, if we could not allocate memory */
4186 return 0; /* caller will free mbuf */
4187 }
4188 RT_LOCK(rt);
4189 if (rt->rt_gateway->sa_family != AF_LINK) {
4190 printf("nd6_storelladdr: something odd happens\n");
4191 RT_UNLOCK(rt);
4192 return 0; /* caller will free mbuf */
4193 }
4194 sdl = SDL(rt->rt_gateway);
4195 if (sdl->sdl_alen == 0) {
4196 /* this should be impossible, but we bark here for debugging */
4197 printf("nd6_storelladdr: sdl_alen == 0\n");
4198 RT_UNLOCK(rt);
4199 return 0; /* caller will free mbuf */
4200 }
4201
4202 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
4203 RT_UNLOCK(rt);
4204 return 1;
4205 }
4206
4207 /*
4208 * This is the ND pre-output routine; care must be taken to ensure that
4209 * the "hint" route never gets freed via rtfree(), since the caller may
4210 * have stored it inside a struct route with a reference held for that
4211 * placeholder.
4212 */
4213 errno_t
4214 nd6_lookup_ipv6(ifnet_t ifp, const struct sockaddr_in6 *ip6_dest,
4215 struct sockaddr_dl *ll_dest, size_t ll_dest_len, route_t hint,
4216 mbuf_t packet)
4217 {
4218 route_t route = hint;
4219 errno_t result = 0;
4220 struct sockaddr_dl *sdl = NULL;
4221 size_t copy_len;
4222
4223 if (ifp == NULL || ip6_dest == NULL) {
4224 return EINVAL;
4225 }
4226
4227 if (ip6_dest->sin6_family != AF_INET6) {
4228 return EAFNOSUPPORT;
4229 }
4230
4231 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) {
4232 return ENETDOWN;
4233 }
4234
4235 if (hint != NULL) {
4236 /*
4237 * Callee holds a reference on the route and returns
4238 * with the route entry locked, upon success.
4239 */
4240 result = route_to_gwroute((const struct sockaddr *)ip6_dest,
4241 hint, &route);
4242 if (result != 0) {
4243 return result;
4244 }
4245 if (route != NULL) {
4246 RT_LOCK_ASSERT_HELD(route);
4247 }
4248 }
4249
4250 if ((packet != NULL && (packet->m_flags & M_MCAST) != 0) ||
4251 ((ifp->if_flags & IFF_MULTICAST) &&
4252 IN6_IS_ADDR_MULTICAST(&ip6_dest->sin6_addr))) {
4253 if (route != NULL) {
4254 RT_UNLOCK(route);
4255 }
4256 result = dlil_resolve_multi(ifp,
4257 (const struct sockaddr *)ip6_dest,
4258 SA(ll_dest), ll_dest_len);
4259 if (route != NULL) {
4260 RT_LOCK(route);
4261 }
4262 goto release;
4263 } else if (route == NULL) {
4264 /*
4265 * rdar://24596652
4266 * For unicast, lookup existing ND6 entries but
4267 * do not trigger a resolution
4268 */
4269 lck_mtx_lock(rnh_lock);
4270 route = rt_lookup(TRUE,
4271 __DECONST(struct sockaddr *, ip6_dest), NULL,
4272 rt_tables[AF_INET6], ifp->if_index);
4273 lck_mtx_unlock(rnh_lock);
4274
4275 if (route != NULL) {
4276 RT_LOCK(route);
4277 }
4278 }
4279
4280 if (route == NULL) {
4281 /*
4282 * This could happen, if we could not allocate memory or
4283 * if route_to_gwroute() didn't return a route.
4284 */
4285 result = ENOBUFS;
4286 goto release;
4287 }
4288
4289 if (route->rt_gateway->sa_family != AF_LINK) {
4290 printf("%s: route %s on %s%d gateway address not AF_LINK\n",
4291 __func__, ip6_sprintf(&ip6_dest->sin6_addr),
4292 route->rt_ifp->if_name, route->rt_ifp->if_unit);
4293 result = EADDRNOTAVAIL;
4294 goto release;
4295 }
4296
4297 sdl = SDL(route->rt_gateway);
4298 if (sdl->sdl_alen == 0) {
4299 /* this should be impossible, but we bark here for debugging */
4300 printf("%s: route %s on %s%d sdl_alen == 0\n", __func__,
4301 ip6_sprintf(&ip6_dest->sin6_addr), route->rt_ifp->if_name,
4302 route->rt_ifp->if_unit);
4303 result = EHOSTUNREACH;
4304 goto release;
4305 }
4306
4307 copy_len = sdl->sdl_len <= ll_dest_len ? sdl->sdl_len : ll_dest_len;
4308 bcopy(sdl, ll_dest, copy_len);
4309
4310 release:
4311 if (route != NULL) {
4312 if (route == hint) {
4313 RT_REMREF_LOCKED(route);
4314 RT_UNLOCK(route);
4315 } else {
4316 RT_UNLOCK(route);
4317 rtfree(route);
4318 }
4319 }
4320 return result;
4321 }
4322
4323 #if (DEVELOPMENT || DEBUG)
4324
4325 static int sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS;
4326 SYSCTL_PROC(_net_inet6_icmp6, OID_AUTO, nd6_lookup_ipv6,
4327 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0,
4328 sysctl_nd6_lookup_ipv6, "S", "");
4329
4330 int
4331 sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
4332 {
4333 #pragma unused(oidp, arg1, arg2)
4334 int error = 0;
4335 struct nd6_lookup_ipv6_args nd6_lookup_ipv6_args;
4336 ifnet_t ifp = NULL;
4337
4338 /*
4339 * Only root can lookup MAC addresses
4340 */
4341 error = proc_suser(current_proc());
4342 if (error != 0) {
4343 nd6log0(error, "%s: proc_suser() error %d\n",
4344 __func__, error);
4345 goto done;
4346 }
4347 if (req->oldptr == USER_ADDR_NULL) {
4348 req->oldidx = sizeof(struct nd6_lookup_ipv6_args);
4349 }
4350 if (req->newptr == USER_ADDR_NULL) {
4351 goto done;
4352 }
4353 if (req->oldlen != sizeof(struct nd6_lookup_ipv6_args) ||
4354 req->newlen != sizeof(struct nd6_lookup_ipv6_args)) {
4355 error = EINVAL;
4356 nd6log0(error, "%s: bad req, error %d\n",
4357 __func__, error);
4358 goto done;
4359 }
4360 error = SYSCTL_IN(req, &nd6_lookup_ipv6_args,
4361 sizeof(struct nd6_lookup_ipv6_args));
4362 if (error != 0) {
4363 nd6log0(error, "%s: SYSCTL_IN() error %d\n",
4364 __func__, error);
4365 goto done;
4366 }
4367
4368 if (nd6_lookup_ipv6_args.ll_dest_len > sizeof(nd6_lookup_ipv6_args.ll_dest_)) {
4369 error = EINVAL;
4370 nd6log0(error, "%s: bad ll_dest_len, error %d\n",
4371 __func__, error);
4372 goto done;
4373 }
4374
4375 /* Make sure to terminate the string */
4376 nd6_lookup_ipv6_args.ifname[IFNAMSIZ - 1] = 0;
4377
4378 error = ifnet_find_by_name(nd6_lookup_ipv6_args.ifname, &ifp);
4379 if (error != 0) {
4380 nd6log0(error, "%s: ifnet_find_by_name() error %d\n",
4381 __func__, error);
4382 goto done;
4383 }
4384
4385 error = nd6_lookup_ipv6(ifp, &nd6_lookup_ipv6_args.ip6_dest,
4386 &nd6_lookup_ipv6_args.ll_dest_._sdl,
4387 nd6_lookup_ipv6_args.ll_dest_len, NULL, NULL);
4388 if (error != 0) {
4389 nd6log0(error, "%s: nd6_lookup_ipv6() error %d\n",
4390 __func__, error);
4391 goto done;
4392 }
4393
4394 error = SYSCTL_OUT(req, &nd6_lookup_ipv6_args,
4395 sizeof(struct nd6_lookup_ipv6_args));
4396 if (error != 0) {
4397 nd6log0(error, "%s: SYSCTL_OUT() error %d\n",
4398 __func__, error);
4399 goto done;
4400 }
4401 done:
4402 return error;
4403 }
4404
4405 #endif /* (DEVELOPEMENT || DEBUG) */
4406
4407 int
4408 nd6_setifinfo(struct ifnet *ifp, u_int32_t before, u_int32_t after)
4409 {
4410 uint32_t b, a;
4411 int err = 0;
4412
4413 /*
4414 * Handle ND6_IFF_IFDISABLED
4415 */
4416 if ((before & ND6_IFF_IFDISABLED) ||
4417 (after & ND6_IFF_IFDISABLED)) {
4418 b = (before & ND6_IFF_IFDISABLED);
4419 a = (after & ND6_IFF_IFDISABLED);
4420
4421 if (b != a && (err = nd6_if_disable(ifp,
4422 ((int32_t)(a - b) > 0))) != 0) {
4423 goto done;
4424 }
4425 }
4426
4427 /*
4428 * Handle ND6_IFF_PROXY_PREFIXES
4429 */
4430 if ((before & ND6_IFF_PROXY_PREFIXES) ||
4431 (after & ND6_IFF_PROXY_PREFIXES)) {
4432 b = (before & ND6_IFF_PROXY_PREFIXES);
4433 a = (after & ND6_IFF_PROXY_PREFIXES);
4434
4435 if (b != a && (err = nd6_if_prproxy(ifp,
4436 ((int32_t)(a - b) > 0))) != 0) {
4437 goto done;
4438 }
4439 }
4440 done:
4441 return err;
4442 }
4443
4444 /*
4445 * Enable/disable IPv6 on an interface, called as part of
4446 * setting/clearing ND6_IFF_IFDISABLED, or during DAD failure.
4447 */
4448 int
4449 nd6_if_disable(struct ifnet *ifp, boolean_t enable)
4450 {
4451 ifnet_lock_shared(ifp);
4452 if (enable) {
4453 ifp->if_eflags |= IFEF_IPV6_DISABLED;
4454 } else {
4455 ifp->if_eflags &= ~IFEF_IPV6_DISABLED;
4456 }
4457 ifnet_lock_done(ifp);
4458
4459 return 0;
4460 }
4461
4462 static int
4463 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
4464 {
4465 #pragma unused(oidp, arg1, arg2)
4466 char pbuf[MAX_IPv6_STR_LEN];
4467 struct nd_defrouter *dr;
4468 int error = 0;
4469
4470 if (req->newptr != USER_ADDR_NULL) {
4471 return EPERM;
4472 }
4473
4474 /* XXX Handle mapped defrouter entries */
4475 lck_mtx_lock(nd6_mutex);
4476 if (proc_is64bit(req->p)) {
4477 struct in6_defrouter_64 d;
4478
4479 bzero(&d, sizeof(d));
4480 d.rtaddr.sin6_family = AF_INET6;
4481 d.rtaddr.sin6_len = sizeof(d.rtaddr);
4482
4483 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
4484 d.rtaddr.sin6_addr = dr->rtaddr;
4485 if (in6_recoverscope(&d.rtaddr,
4486 &dr->rtaddr, dr->ifp) != 0) {
4487 log(LOG_ERR, "scope error in default router "
4488 "list (%s)\n", inet_ntop(AF_INET6,
4489 &dr->rtaddr, pbuf, sizeof(pbuf)));
4490 }
4491 d.flags = dr->flags;
4492 d.stateflags = dr->stateflags;
4493 d.rtlifetime = dr->rtlifetime;
4494 d.expire = nddr_getexpire(dr);
4495 d.if_index = dr->ifp->if_index;
4496 error = SYSCTL_OUT(req, &d, sizeof(d));
4497 if (error != 0) {
4498 break;
4499 }
4500 }
4501 } else {
4502 struct in6_defrouter_32 d;
4503
4504 bzero(&d, sizeof(d));
4505 d.rtaddr.sin6_family = AF_INET6;
4506 d.rtaddr.sin6_len = sizeof(d.rtaddr);
4507
4508 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
4509 d.rtaddr.sin6_addr = dr->rtaddr;
4510 if (in6_recoverscope(&d.rtaddr,
4511 &dr->rtaddr, dr->ifp) != 0) {
4512 log(LOG_ERR, "scope error in default router "
4513 "list (%s)\n", inet_ntop(AF_INET6,
4514 &dr->rtaddr, pbuf, sizeof(pbuf)));
4515 }
4516 d.flags = dr->flags;
4517 d.stateflags = dr->stateflags;
4518 d.rtlifetime = dr->rtlifetime;
4519 d.expire = nddr_getexpire(dr);
4520 d.if_index = dr->ifp->if_index;
4521 error = SYSCTL_OUT(req, &d, sizeof(d));
4522 if (error != 0) {
4523 break;
4524 }
4525 }
4526 }
4527 lck_mtx_unlock(nd6_mutex);
4528 return error;
4529 }
4530
4531 static int
4532 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
4533 {
4534 #pragma unused(oidp, arg1, arg2)
4535 char pbuf[MAX_IPv6_STR_LEN];
4536 struct nd_pfxrouter *pfr;
4537 struct sockaddr_in6 s6;
4538 struct nd_prefix *pr;
4539 int error = 0;
4540
4541 if (req->newptr != USER_ADDR_NULL) {
4542 return EPERM;
4543 }
4544
4545 bzero(&s6, sizeof(s6));
4546 s6.sin6_family = AF_INET6;
4547 s6.sin6_len = sizeof(s6);
4548
4549 /* XXX Handle mapped defrouter entries */
4550 lck_mtx_lock(nd6_mutex);
4551 if (proc_is64bit(req->p)) {
4552 struct in6_prefix_64 p;
4553
4554 bzero(&p, sizeof(p));
4555 p.origin = PR_ORIG_RA;
4556
4557 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
4558 NDPR_LOCK(pr);
4559 p.prefix = pr->ndpr_prefix;
4560 if (in6_recoverscope(&p.prefix,
4561 &pr->ndpr_prefix.sin6_addr, pr->ndpr_ifp) != 0) {
4562 log(LOG_ERR, "scope error in "
4563 "prefix list (%s)\n", inet_ntop(AF_INET6,
4564 &p.prefix.sin6_addr, pbuf, sizeof(pbuf)));
4565 }
4566 p.raflags = pr->ndpr_raf;
4567 p.prefixlen = pr->ndpr_plen;
4568 p.vltime = pr->ndpr_vltime;
4569 p.pltime = pr->ndpr_pltime;
4570 p.if_index = pr->ndpr_ifp->if_index;
4571 p.expire = ndpr_getexpire(pr);
4572 p.refcnt = pr->ndpr_addrcnt;
4573 p.flags = pr->ndpr_stateflags;
4574 p.advrtrs = 0;
4575 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
4576 p.advrtrs++;
4577 error = SYSCTL_OUT(req, &p, sizeof(p));
4578 if (error != 0) {
4579 NDPR_UNLOCK(pr);
4580 break;
4581 }
4582 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
4583 s6.sin6_addr = pfr->router->rtaddr;
4584 if (in6_recoverscope(&s6, &pfr->router->rtaddr,
4585 pfr->router->ifp) != 0) {
4586 log(LOG_ERR,
4587 "scope error in prefix list (%s)\n",
4588 inet_ntop(AF_INET6, &s6.sin6_addr,
4589 pbuf, sizeof(pbuf)));
4590 }
4591 error = SYSCTL_OUT(req, &s6, sizeof(s6));
4592 if (error != 0) {
4593 break;
4594 }
4595 }
4596 NDPR_UNLOCK(pr);
4597 if (error != 0) {
4598 break;
4599 }
4600 }
4601 } else {
4602 struct in6_prefix_32 p;
4603
4604 bzero(&p, sizeof(p));
4605 p.origin = PR_ORIG_RA;
4606
4607 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
4608 NDPR_LOCK(pr);
4609 p.prefix = pr->ndpr_prefix;
4610 if (in6_recoverscope(&p.prefix,
4611 &pr->ndpr_prefix.sin6_addr, pr->ndpr_ifp) != 0) {
4612 log(LOG_ERR,
4613 "scope error in prefix list (%s)\n",
4614 inet_ntop(AF_INET6, &p.prefix.sin6_addr,
4615 pbuf, sizeof(pbuf)));
4616 }
4617 p.raflags = pr->ndpr_raf;
4618 p.prefixlen = pr->ndpr_plen;
4619 p.vltime = pr->ndpr_vltime;
4620 p.pltime = pr->ndpr_pltime;
4621 p.if_index = pr->ndpr_ifp->if_index;
4622 p.expire = ndpr_getexpire(pr);
4623 p.refcnt = pr->ndpr_addrcnt;
4624 p.flags = pr->ndpr_stateflags;
4625 p.advrtrs = 0;
4626 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
4627 p.advrtrs++;
4628 error = SYSCTL_OUT(req, &p, sizeof(p));
4629 if (error != 0) {
4630 NDPR_UNLOCK(pr);
4631 break;
4632 }
4633 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
4634 s6.sin6_addr = pfr->router->rtaddr;
4635 if (in6_recoverscope(&s6, &pfr->router->rtaddr,
4636 pfr->router->ifp) != 0) {
4637 log(LOG_ERR,
4638 "scope error in prefix list (%s)\n",
4639 inet_ntop(AF_INET6, &s6.sin6_addr,
4640 pbuf, sizeof(pbuf)));
4641 }
4642 error = SYSCTL_OUT(req, &s6, sizeof(s6));
4643 if (error != 0) {
4644 break;
4645 }
4646 }
4647 NDPR_UNLOCK(pr);
4648 if (error != 0) {
4649 break;
4650 }
4651 }
4652 }
4653 lck_mtx_unlock(nd6_mutex);
4654
4655 return error;
4656 }
4657
4658 void
4659 in6_ifaddr_set_dadprogress(struct in6_ifaddr *ia)
4660 {
4661 struct ifnet* ifp = ia->ia_ifp;
4662 uint32_t flags = IN6_IFF_TENTATIVE;
4663 uint32_t optdad = nd6_optimistic_dad;
4664 struct nd_ifinfo *ndi = NULL;
4665
4666 ndi = ND_IFINFO(ifp);
4667 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
4668 if (!(ndi->flags & ND6_IFF_DAD)) {
4669 return;
4670 }
4671
4672 if (optdad) {
4673 if ((ifp->if_eflags & IFEF_IPV6_ROUTER) != 0) {
4674 optdad = 0;
4675 } else {
4676 lck_mtx_lock(&ndi->lock);
4677 if ((ndi->flags & ND6_IFF_REPLICATED) != 0) {
4678 optdad = 0;
4679 }
4680 lck_mtx_unlock(&ndi->lock);
4681 }
4682 }
4683
4684 if (optdad) {
4685 if ((optdad & ND6_OPTIMISTIC_DAD_LINKLOCAL) &&
4686 IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
4687 flags = IN6_IFF_OPTIMISTIC;
4688 } else if ((optdad & ND6_OPTIMISTIC_DAD_AUTOCONF) &&
4689 (ia->ia6_flags & IN6_IFF_AUTOCONF)) {
4690 if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
4691 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) {
4692 flags = IN6_IFF_OPTIMISTIC;
4693 }
4694 } else if (ia->ia6_flags & IN6_IFF_SECURED) {
4695 if (optdad & ND6_OPTIMISTIC_DAD_SECURED) {
4696 flags = IN6_IFF_OPTIMISTIC;
4697 }
4698 } else {
4699 /*
4700 * Keeping the behavior for temp and CGA
4701 * SLAAC addresses to have a knob for optimistic
4702 * DAD.
4703 * Other than that if ND6_OPTIMISTIC_DAD_AUTOCONF
4704 * is set, we should default to optimistic
4705 * DAD.
4706 * For now this means SLAAC addresses with interface
4707 * identifier derived from modified EUI-64 bit
4708 * identifiers.
4709 */
4710 flags = IN6_IFF_OPTIMISTIC;
4711 }
4712 } else if ((optdad & ND6_OPTIMISTIC_DAD_DYNAMIC) &&
4713 (ia->ia6_flags & IN6_IFF_DYNAMIC)) {
4714 if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
4715 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) {
4716 flags = IN6_IFF_OPTIMISTIC;
4717 }
4718 } else {
4719 flags = IN6_IFF_OPTIMISTIC;
4720 }
4721 } else if ((optdad & ND6_OPTIMISTIC_DAD_MANUAL) &&
4722 (ia->ia6_flags & IN6_IFF_OPTIMISTIC)) {
4723 /*
4724 * rdar://17483438
4725 * Bypass tentative for address assignments
4726 * not covered above (e.g. manual) upon request
4727 */
4728 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) &&
4729 !(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
4730 !(ia->ia6_flags & IN6_IFF_DYNAMIC)) {
4731 flags = IN6_IFF_OPTIMISTIC;
4732 }
4733 }
4734 }
4735
4736 ia->ia6_flags &= ~(IN6_IFF_DUPLICATED | IN6_IFF_DADPROGRESS);
4737 ia->ia6_flags |= flags;
4738
4739 nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
4740 __func__,
4741 ip6_sprintf(&ia->ia_addr.sin6_addr),
4742 if_name(ia->ia_ifp),
4743 ia->ia6_flags);
4744 }