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