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