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