]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/nd6.h
a831fb5eef86a8ad6d8f84e4b38be462b5e331f0
[apple/xnu.git] / bsd / netinet6 / nd6.h
1 /*
2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $FreeBSD: src/sys/netinet6/nd6.h,v 1.2.2.3 2001/08/13 01:10:49 simokawa Exp $ */
30 /* $KAME: nd6.h,v 1.55 2001/04/27 15:09:49 itojun Exp $ */
31
32 /*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #ifndef _NETINET6_ND6_H_
62 #define _NETINET6_ND6_H_
63 #include <sys/appleapiopts.h>
64
65 /* see net/route.h, or net/if_inarp.h */
66 #ifndef RTF_ANNOUNCE
67 #define RTF_ANNOUNCE RTF_PROTO2
68 #endif
69
70 #include <sys/queue.h>
71
72 #ifdef XNU_KERNEL_PRIVATE
73 #include <net/flowadv.h>
74 #include <kern/locks.h>
75 #include <sys/tree.h>
76
77 struct llinfo_nd6 {
78 /*
79 * The following are protected by rnh_lock
80 */
81 struct llinfo_nd6 *ln_next;
82 struct llinfo_nd6 *ln_prev;
83 struct rtentry *ln_rt;
84 /*
85 * The following are protected by rt_lock
86 */
87 struct mbuf *ln_hold; /* last packet until resolved/timeout */
88 long ln_asked; /* number of queries already sent for this addr */
89 u_int32_t ln_expire; /* lifetime for NDP state transition */
90 short ln_state; /* reachability state */
91 short ln_router; /* 2^0: ND6 router bit */
92 int ln_byhint; /* # of times we made it reachable by UL hint */
93 u_int32_t ln_flags; /* flags; see below */
94 struct if_llreach *ln_llreach; /* link-layer reachability record */
95 u_int64_t ln_lastused; /* last used timestamp */
96 };
97
98 /* Values for ln_flags */
99 #define ND6_LNF_TIMER_SKIP 0x1 /* modified by nd6_timer() */
100 #define ND6_LNF_IN_USE 0x2 /* currently in llinfo_nd6 list */
101 #endif /* XNU_KERNEL_PRIVATE */
102
103 #define ND6_LLINFO_PURGE -3
104 #define ND6_LLINFO_NOSTATE -2
105 /*
106 * We don't need the WAITDELETE state any more, but we keep the definition
107 * in a comment line instead of removing it. This is necessary to avoid
108 * unintentionally reusing the value for another purpose, which might
109 * affect backward compatibility with old applications.
110 * (20000711 jinmei@kame.net)
111 */
112 /* #define ND6_LLINFO_WAITDELETE -1 */
113 #define ND6_LLINFO_INCOMPLETE 0
114 #define ND6_LLINFO_REACHABLE 1
115 #define ND6_LLINFO_STALE 2
116 #define ND6_LLINFO_DELAY 3
117 #define ND6_LLINFO_PROBE 4
118
119 #ifdef XNU_KERNEL_PRIVATE
120 #define ND6_IS_LLINFO_PROBREACH(n) ((n)->ln_state > ND6_LLINFO_INCOMPLETE)
121 #define ND6_LLINFO_PERMANENT(n) (((n)->ln_expire == 0) && ((n)->ln_state > ND6_LLINFO_INCOMPLETE))
122
123 #define ND6_EUI64_GBIT 0x01
124 #define ND6_EUI64_UBIT 0x02
125
126 #define ND6_EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= ND6_EUI64_UBIT; } while (0)
127 #define ND6_EUI64_GROUP(in6) ((in6)->s6_addr[8] & ND6_EUI64_GBIT)
128 #define ND6_EUI64_INDIVIDUAL(in6) (!ND6_EUI64_GROUP(in6))
129 #define ND6_EUI64_LOCAL(in6) ((in6)->s6_addr[8] & ND6_EUI64_UBIT)
130 #define ND6_EUI64_UNIVERSAL(in6) (!ND6_EUI64_LOCAL(in6))
131 #define ND6_IFID_LOCAL(in6) (!ND6_EUI64_LOCAL(in6))
132 #define ND6_IFID_UNIVERSAL(in6) (!ND6_EUI64_UNIVERSAL(in6))
133 #endif /* XNU_KERNEL_PRIVATE */
134
135 #if !defined(XNU_KERNEL_PRIVATE)
136 struct nd_ifinfo {
137 #else
138 /* For binary compatibility, this structure must not change */
139 struct nd_ifinfo_compat {
140 #endif /* !XNU_KERNEL_PRIVATE */
141 u_int32_t linkmtu; /* LinkMTU */
142 u_int32_t maxmtu; /* Upper bound of LinkMTU */
143 u_int32_t basereachable; /* BaseReachableTime */
144 u_int32_t reachable; /* Reachable Time */
145 u_int32_t retrans; /* Retrans Timer */
146 u_int32_t flags; /* Flags */
147 int recalctm; /* BaseReacable re-calculation timer */
148 u_int8_t chlim; /* CurHopLimit */
149 u_int8_t receivedra;
150 /* the following 3 members are for privacy extension for addrconf */
151 u_int8_t randomseed0[8]; /* upper 64 bits of SHA1 digest */
152 u_int8_t randomseed1[8]; /* lower 64 bits (usually the EUI64 IFID) */
153 u_int8_t randomid[8]; /* current random ID */
154 };
155
156 #if defined(XNU_KERNEL_PRIVATE)
157 struct nd_ifinfo {
158 decl_lck_mtx_data(, lock);
159 boolean_t initialized; /* Flag to see the entry is initialized */
160 u_int32_t linkmtu; /* LinkMTU */
161 u_int32_t maxmtu; /* Upper bound of LinkMTU */
162 u_int32_t basereachable; /* BaseReachableTime */
163 u_int32_t reachable; /* Reachable Time */
164 u_int32_t retrans; /* Retrans Timer */
165 u_int32_t flags; /* Flags */
166 int recalctm; /* BaseReacable re-calculation timer */
167 u_int8_t chlim; /* CurHopLimit */
168 u_int8_t _pad[3];
169 /* the following 3 members are for privacy extension for addrconf */
170 u_int8_t randomseed0[8]; /* upper 64 bits of SHA1 digest */
171 u_int8_t randomseed1[8]; /* lower 64 bits (usually the EUI64 IFID) */
172 u_int8_t randomid[8]; /* current random ID */
173 /* keep track of routers and prefixes on this link */
174 int32_t nprefixes;
175 int32_t ndefrouters;
176 };
177 #endif /* XNU_KERNEL_PRIVATE */
178
179 #define ND6_IFF_PERFORMNUD 0x1
180 #if defined(PRIVATE)
181 #define ND6_IFF_ACCEPT_RTADV 0x2 /* APPLE: not used. Innterface specific router
182 * advertisments are handled with a specific ifnet
183 * flag: IFEF_ACCEPT_RTADVD
184 */
185 #define ND6_IFF_PREFER_SOURCE 0x4 /* APPLE: NOT USED not related to ND. */
186 #define ND6_IFF_IFDISABLED 0x8 /* IPv6 operation is disabled due to
187 * DAD failure. (XXX: not ND-specific)
188 */
189 #define ND6_IFF_DONT_SET_IFROUTE 0x10 /* NOT USED */
190 #endif /* PRIVATE */
191 #define ND6_IFF_PROXY_PREFIXES 0x20
192 #define ND6_IFF_IGNORE_NA 0x40
193
194 struct in6_nbrinfo {
195 char ifname[IFNAMSIZ]; /* if name, e.g. "en0" */
196 struct in6_addr addr; /* IPv6 address of the neighbor */
197 long asked; /* number of queries already sent for this addr */
198 int isrouter; /* if it acts as a router */
199 int state; /* reachability state */
200 int expire; /* lifetime for NDP state transition */
201 };
202
203 #if defined(XNU_KERNEL_PRIVATE)
204 struct in6_nbrinfo_32 {
205 char ifname[IFNAMSIZ];
206 struct in6_addr addr;
207 u_int32_t asked;
208 int isrouter;
209 int state;
210 int expire;
211 };
212
213 struct in6_nbrinfo_64 {
214 char ifname[IFNAMSIZ];
215 struct in6_addr addr;
216 long asked;
217 int isrouter __attribute__((aligned(8)));
218 int state;
219 int expire;
220 } __attribute__((aligned(8)));
221 #endif /* XNU_KERNEL_PRIVATE */
222
223 #define DRLSTSIZ 10
224 #define PRLSTSIZ 10
225
226 struct in6_drlist {
227 char ifname[IFNAMSIZ];
228 struct {
229 struct in6_addr rtaddr;
230 u_char flags;
231 u_short rtlifetime;
232 u_long expire;
233 u_short if_index;
234 } defrouter[DRLSTSIZ];
235 };
236
237 #if defined(XNU_KERNEL_PRIVATE)
238 struct in6_drlist_32 {
239 char ifname[IFNAMSIZ];
240 struct {
241 struct in6_addr rtaddr;
242 u_char flags;
243 u_short rtlifetime;
244 u_int32_t expire;
245 u_short if_index;
246 } defrouter[DRLSTSIZ];
247 };
248
249 struct in6_drlist_64 {
250 char ifname[IFNAMSIZ];
251 struct {
252 struct in6_addr rtaddr;
253 u_char flags;
254 u_short rtlifetime;
255 u_long expire __attribute__((aligned(8)));
256 u_short if_index __attribute__((aligned(8)));
257 } defrouter[DRLSTSIZ] __attribute__((aligned(8)));
258 };
259 #endif /* XNU_KERNEL_PRIVATE */
260
261 /* valid values for stateflags */
262 #define NDDRF_INSTALLED 0x1 /* installed in the routing table */
263 #define NDDRF_IFSCOPE 0x2 /* installed as a scoped route */
264 #define NDDRF_STATIC 0x4 /* for internal use only */
265 #ifdef XNU_KERNEL_PRIVATE
266 #define NDDRF_PROCESSED 0x10
267 #endif
268
269 struct in6_defrouter {
270 struct sockaddr_in6 rtaddr;
271 u_char flags;
272 u_char stateflags;
273 u_short rtlifetime;
274 u_long expire;
275 u_short if_index;
276 };
277
278 #if defined(XNU_KERNEL_PRIVATE)
279 struct in6_defrouter_32 {
280 struct sockaddr_in6 rtaddr;
281 u_char flags;
282 u_char stateflags;
283 u_short rtlifetime;
284 u_int32_t expire;
285 u_short if_index;
286 };
287
288 struct in6_defrouter_64 {
289 struct sockaddr_in6 rtaddr;
290 u_char flags;
291 u_char stateflags;
292 u_short rtlifetime;
293 u_long expire __attribute__((aligned(8)));
294 u_short if_index __attribute__((aligned(8)));
295 } __attribute__((aligned(8)));
296 #endif /* XNU_KERNEL_PRIVATE */
297
298 struct in6_prlist {
299 char ifname[IFNAMSIZ];
300 struct {
301 struct in6_addr prefix;
302 struct prf_ra raflags;
303 u_char prefixlen;
304 u_char origin;
305 u_long vltime;
306 u_long pltime;
307 u_long expire;
308 u_short if_index;
309 u_short advrtrs; /* number of advertisement routers */
310 struct in6_addr advrtr[DRLSTSIZ]; /* XXX: explicit limit */
311 } prefix[PRLSTSIZ];
312 };
313
314 #if defined(XNU_KERNEL_PRIVATE)
315 struct in6_prlist_32 {
316 char ifname[IFNAMSIZ];
317 struct {
318 struct in6_addr prefix;
319 struct prf_ra raflags;
320 u_char prefixlen;
321 u_char origin;
322 u_int32_t vltime;
323 u_int32_t pltime;
324 u_int32_t expire;
325 u_short if_index;
326 u_short advrtrs;
327 struct in6_addr advrtr[DRLSTSIZ];
328 } prefix[PRLSTSIZ];
329 };
330
331 struct in6_prlist_64 {
332 char ifname[IFNAMSIZ];
333 struct {
334 struct in6_addr prefix;
335 struct prf_ra raflags;
336 u_char prefixlen;
337 u_char origin;
338 u_long vltime __attribute__((aligned(8)));
339 u_long pltime __attribute__((aligned(8)));
340 u_long expire __attribute__((aligned(8)));
341 u_short if_index;
342 u_short advrtrs;
343 u_int32_t pad;
344 struct in6_addr advrtr[DRLSTSIZ];
345 } prefix[PRLSTSIZ];
346 };
347 #endif /* XNU_KERNEL_PRIVATE */
348
349 struct in6_prefix {
350 struct sockaddr_in6 prefix;
351 struct prf_ra raflags;
352 u_char prefixlen;
353 u_char origin;
354 u_long vltime;
355 u_long pltime;
356 u_long expire;
357 u_int32_t flags;
358 int refcnt;
359 u_short if_index;
360 u_short advrtrs; /* number of advertisement routers */
361 /* struct sockaddr_in6 advrtr[] */
362 };
363
364 #if defined(XNU_KERNEL_PRIVATE)
365 struct in6_prefix_32 {
366 struct sockaddr_in6 prefix;
367 struct prf_ra raflags;
368 u_char prefixlen;
369 u_char origin;
370 u_int32_t vltime;
371 u_int32_t pltime;
372 u_int32_t expire;
373 u_int32_t flags;
374 int refcnt;
375 u_short if_index;
376 u_short advrtrs; /* number of advertisement routers */
377 /* struct sockaddr_in6 advrtr[] */
378 };
379
380 struct in6_prefix_64 {
381 struct sockaddr_in6 prefix;
382 struct prf_ra raflags;
383 u_char prefixlen;
384 u_char origin;
385 u_long vltime __attribute__((aligned(8)));
386 u_long pltime __attribute__((aligned(8)));
387 u_long expire __attribute__((aligned(8)));
388 u_int32_t flags __attribute__((aligned(8)));
389 int refcnt;
390 u_short if_index;
391 u_short advrtrs;
392 /* struct sockaddr_in6 advrtr[] */
393 };
394 #endif /* XNU_KERNEL_PRIVATE */
395
396 struct in6_ondireq {
397 char ifname[IFNAMSIZ];
398 struct {
399 u_int32_t linkmtu; /* LinkMTU */
400 u_int32_t maxmtu; /* Upper bound of LinkMTU */
401 u_int32_t basereachable; /* BaseReachableTime */
402 u_int32_t reachable; /* Reachable Time */
403 u_int32_t retrans; /* Retrans Timer */
404 u_int32_t flags; /* Flags */
405 int recalctm; /* BaseReacable re-calculation timer */
406 u_int8_t chlim; /* CurHopLimit */
407 u_int8_t receivedra;
408 } ndi;
409 };
410
411 #if !defined(XNU_KERNEL_PRIVATE)
412 struct in6_ndireq {
413 char ifname[IFNAMSIZ];
414 struct nd_ifinfo ndi;
415 };
416 #else
417 struct in6_ndireq {
418 char ifname[IFNAMSIZ];
419 struct nd_ifinfo_compat ndi;
420 };
421 #endif /* !XNU_KERNEL_PRIVATE */
422
423 struct in6_ndifreq {
424 char ifname[IFNAMSIZ];
425 u_long ifindex;
426 };
427
428 #define MAX_RTR_SOLICITATION_DELAY 1 /* 1sec */
429 #define RTR_SOLICITATION_INTERVAL 4 /* 4sec */
430
431 #if defined(XNU_KERNEL_PRIVATE)
432 struct in6_ndifreq_32 {
433 char ifname[IFNAMSIZ];
434 u_int32_t ifindex;
435 };
436
437 struct in6_ndifreq_64 {
438 char ifname[IFNAMSIZ];
439 u_long ifindex __attribute__((aligned(8)));
440 };
441 #endif /* XNU_KERNEL_PRIVATE */
442
443 /* Prefix status */
444 #define NDPRF_ONLINK 0x1
445 #define NDPRF_DETACHED 0x2
446 #define NDPRF_STATIC 0x100
447 #define NDPRF_IFSCOPE 0x1000
448 #define NDPRF_PRPROXY 0x2000
449 #ifdef XNU_KERNEL_PRIVATE
450 #define NDPRF_PROCESSED 0x08000
451 #endif
452
453 /* protocol constants */
454 #define MAX_RTR_SOLICITATION_DELAY 1 /*1sec*/
455 #define RTR_SOLICITATION_INTERVAL 4 /*4sec*/
456 #define MAX_RTR_SOLICITATIONS 3
457
458 #define ND6_INFINITE_LIFETIME 0xffffffff
459 #define ND6_MAX_LIFETIME 0x7fffffff
460
461 #ifdef XNU_KERNEL_PRIVATE
462 /*
463 * Protects nd_ifinfo[]
464 */
465 __private_extern__ lck_rw_t *nd_if_rwlock;
466
467 #define ND_IFINFO(ifp) \
468 ((ifp)->if_index < nd_ifinfo_indexlim ? &nd_ifinfo[(ifp)->if_index] : NULL)
469
470 /*
471 * In a more readable form, we derive linkmtu based on:
472 *
473 * if (ND_IFINFO(ifp) == NULL || !ND_IFINFO(ifp)->initialized)
474 * linkmtu = ifp->if_mtu;
475 * else if (ND_IFINFO(ifp)->linkmtu && ND_IFINFO(ifp)->linkmtu < ifp->if_mtu)
476 * linkmtu = ND_IFINFO(ifp)->linkmtu;
477 * else if ((ND_IFINFO(ifp)->maxmtu && ND_IFINFO(ifp)->maxmtu < ifp->if_mtu))
478 * linkmtu = ND_IFINFO(ifp)->maxmtu;
479 * else
480 * linkmtu = ifp->if_mtu;
481 */
482 #define IN6_LINKMTU(ifp) \
483 ((ND_IFINFO(ifp) == NULL || !ND_IFINFO(ifp)->initialized) ? \
484 (ifp)->if_mtu : ((ND_IFINFO(ifp)->linkmtu && \
485 ND_IFINFO(ifp)->linkmtu < (ifp)->if_mtu) ? ND_IFINFO(ifp)->linkmtu : \
486 ((ND_IFINFO(ifp)->maxmtu && ND_IFINFO(ifp)->maxmtu < (ifp)->if_mtu) ? \
487 ND_IFINFO(ifp)->maxmtu : (ifp)->if_mtu)))
488
489 /* node constants */
490 #define MAX_REACHABLE_TIME 3600000 /* msec */
491 #define REACHABLE_TIME 30000 /* msec */
492 #define RETRANS_TIMER 1000 /* msec */
493 #define MIN_RANDOM_FACTOR 512 /* 1024 * 0.5 */
494 #define MAX_RANDOM_FACTOR 1536 /* 1024 * 1.5 */
495 #define DEF_TEMP_VALID_LIFETIME 604800 /* 1 week */
496 #define DEF_TEMP_PREFERRED_LIFETIME 86400 /* 1 day */
497 #define TEMPADDR_REGEN_ADVANCE 5 /* sec */
498 #define MAX_TEMP_DESYNC_FACTOR 600 /* 10 min */
499 #define ND_COMPUTE_RTIME(x) \
500 (((MIN_RANDOM_FACTOR * (x >> 10)) + (random() & \
501 ((MAX_RANDOM_FACTOR - MIN_RANDOM_FACTOR) * (x >> 10)))) /1000)
502
503 TAILQ_HEAD(nd_drhead, nd_defrouter);
504 struct nd_defrouter {
505 decl_lck_mtx_data(, nddr_lock);
506 uint32_t nddr_refcount;
507 uint32_t nddr_debug;
508 TAILQ_ENTRY(nd_defrouter) dr_entry;
509 struct in6_addr rtaddr;
510 u_char flags; /* flags on RA message */
511 u_char stateflags;
512 u_short rtlifetime;
513 u_int32_t expire;
514 struct ifnet *ifp;
515 unsigned int genid;
516 int err;
517 void (*nddr_trace) /* callback fn for tracing refs */
518 (struct nd_defrouter *, int);
519 };
520
521 #define NDDR_LOCK_ASSERT_HELD(_nddr) \
522 lck_mtx_assert(&(_nddr)->nddr_lock, LCK_MTX_ASSERT_OWNED)
523
524 #define NDDR_LOCK_ASSERT_NOTHELD(_nddr) \
525 lck_mtx_assert(&(_nddr)->nddr_lock, LCK_MTX_ASSERT_NOTOWNED)
526
527 #define NDDR_LOCK(_nddr) \
528 lck_mtx_lock(&(_nddr)->nddr_lock)
529
530 #define NDDR_LOCK_SPIN(_nddr) \
531 lck_mtx_lock_spin(&(_nddr)->nddr_lock)
532
533 #define NDDR_CONVERT_LOCK(_nddr) do { \
534 NDPR_LOCK_ASSERT_HELD(_nddr); \
535 lck_mtx_convert_spin(&(_nddr)->nddr_lock); \
536 } while (0)
537
538 #define NDDR_UNLOCK(_nddr) \
539 lck_mtx_unlock(&(_nddr)->nddr_lock)
540
541 #define NDDR_ADDREF(_nddr) \
542 nddr_addref(_nddr, 0)
543
544 #define NDDR_ADDREF_LOCKED(_nddr) \
545 nddr_addref(_nddr, 1)
546
547 #define NDDR_REMREF(_nddr) do { \
548 (void) nddr_remref(_nddr, 0); \
549 } while (0)
550
551 #define NDDR_REMREF_LOCKED(_nddr) \
552 nddr_remref(_nddr, 1)
553
554 /* define struct prproxy_sols_tree */
555 RB_HEAD(prproxy_sols_tree, nd6_prproxy_soltgt);
556
557 struct nd_prefix {
558 decl_lck_mtx_data(, ndpr_lock);
559 u_int32_t ndpr_refcount; /* reference count */
560 u_int32_t ndpr_debug; /* see ifa_debug flags */
561 struct ifnet *ndpr_ifp;
562 struct rtentry *ndpr_rt;
563 LIST_ENTRY(nd_prefix) ndpr_entry;
564 struct sockaddr_in6 ndpr_prefix; /* prefix */
565 struct in6_addr ndpr_mask; /* netmask derived from the prefix */
566 struct in6_addr ndpr_addr; /* address that is derived from the prefix */
567 u_int32_t ndpr_vltime; /* advertised valid lifetime */
568 u_int32_t ndpr_pltime; /* advertised preferred lifetime */
569 time_t ndpr_preferred; /* preferred time of the prefix */
570 time_t ndpr_expire; /* expiration time of the prefix */
571 time_t ndpr_lastupdate; /* reception time of last advertisement */
572 struct prf_ra ndpr_flags;
573 u_int32_t ndpr_stateflags; /* actual state flags */
574 /* list of routers that advertise the prefix: */
575 LIST_HEAD(pr_rtrhead, nd_pfxrouter) ndpr_advrtrs;
576 u_char ndpr_plen;
577 int ndpr_addrcnt; /* reference counter from addresses */
578 u_int32_t ndpr_allmulti_cnt; /* total all-multi reqs */
579 u_int32_t ndpr_prproxy_sols_cnt; /* total # of proxied NS */
580 struct prproxy_sols_tree ndpr_prproxy_sols; /* tree of proxied NS */
581 void (*ndpr_trace) /* callback fn for tracing refs */
582 (struct nd_prefix *, int);
583 };
584
585 #define ndpr_next ndpr_entry.le_next
586
587 #define ndpr_raf ndpr_flags
588 #define ndpr_raf_onlink ndpr_flags.onlink
589 #define ndpr_raf_auto ndpr_flags.autonomous
590 #define ndpr_raf_router ndpr_flags.router
591 /*
592 * We keep expired prefix for certain amount of time, for validation purposes.
593 * 1800s = MaxRtrAdvInterval
594 */
595 #define NDPR_KEEP_EXPIRED (1800 * 2)
596
597 #define NDPR_LOCK_ASSERT_HELD(_ndpr) \
598 lck_mtx_assert(&(_ndpr)->ndpr_lock, LCK_MTX_ASSERT_OWNED)
599
600 #define NDPR_LOCK_ASSERT_NOTHELD(_ndpr) \
601 lck_mtx_assert(&(_ndpr)->ndpr_lock, LCK_MTX_ASSERT_NOTOWNED)
602
603 #define NDPR_LOCK(_ndpr) \
604 lck_mtx_lock(&(_ndpr)->ndpr_lock)
605
606 #define NDPR_LOCK_SPIN(_ndpr) \
607 lck_mtx_lock_spin(&(_ndpr)->ndpr_lock)
608
609 #define NDPR_CONVERT_LOCK(_ndpr) do { \
610 NDPR_LOCK_ASSERT_HELD(_ndpr); \
611 lck_mtx_convert_spin(&(_ndpr)->ndpr_lock); \
612 } while (0)
613
614 #define NDPR_UNLOCK(_ndpr) \
615 lck_mtx_unlock(&(_ndpr)->ndpr_lock)
616
617 #define NDPR_ADDREF(_ndpr) \
618 ndpr_addref(_ndpr, 0)
619
620 #define NDPR_ADDREF_LOCKED(_ndpr) \
621 ndpr_addref(_ndpr, 1)
622
623 #define NDPR_REMREF(_ndpr) do { \
624 (void) ndpr_remref(_ndpr, 0); \
625 } while (0)
626
627 #define NDPR_REMREF_LOCKED(_ndpr) \
628 ndpr_remref(_ndpr, 1)
629
630 /*
631 * Message format for use in obtaining information about prefixes
632 * from inet6 sysctl function
633 */
634 struct inet6_ndpr_msghdr {
635 u_short inpm_msglen; /* to skip over non-understood messages */
636 u_char inpm_version; /* future binary compatibility */
637 u_char inpm_type; /* message type */
638 struct in6_addr inpm_prefix;
639 u_int32_t prm_vltim;
640 u_int32_t prm_pltime;
641 u_int32_t prm_expire;
642 u_int32_t prm_preferred;
643 struct in6_prflags prm_flags;
644 u_short prm_index; /* index for associated ifp */
645 u_char prm_plen; /* length of prefix in bits */
646 };
647
648 #define prm_raf_onlink prm_flags.prf_ra.onlink
649 #define prm_raf_auto prm_flags.prf_ra.autonomous
650
651 #define prm_statef_onlink prm_flags.prf_state.onlink
652
653 #define prm_rrf_decrvalid prm_flags.prf_rr.decrvalid
654 #define prm_rrf_decrprefd prm_flags.prf_rr.decrprefd
655
656 #define ifpr2ndpr(ifpr) ((struct nd_prefix *)(ifpr))
657 #define ndpr2ifpr(ndpr) ((struct ifprefix *)(ndpr))
658
659 struct nd_pfxrouter {
660 LIST_ENTRY(nd_pfxrouter) pfr_entry;
661 #define pfr_next pfr_entry.le_next
662 struct nd_defrouter *router;
663 };
664
665 LIST_HEAD(nd_prhead, nd_prefix);
666
667 struct nd_prefix_list {
668 struct nd_prefix_list *next;
669 struct nd_prefix pr;
670 };
671 #endif /* XNU_KERNEL_PRIVATE */
672
673 #if defined(PRIVATE)
674 /* ND6 kernel event subclass value */
675 #define KEV_ND6_SUBCLASS 7
676 /* ND6 kernel event action type */
677 #define KEV_ND6_RA 1
678 /* ND6 RA L2 source address length */
679 #define ND6_ROUTER_LL_SIZE 64
680
681 struct nd6_ra_prefix {
682 struct sockaddr_in6 prefix;
683 struct prf_ra raflags;
684 u_int32_t prefixlen;
685 u_int32_t origin;
686 u_int64_t vltime;
687 u_int64_t pltime;
688 u_int64_t expire;
689 u_int32_t flags;
690 u_int32_t refcnt;
691 u_int32_t if_index;
692 u_int32_t pad;
693 };
694
695 /* ND6 router advertisement valid bits */
696 #define KEV_ND6_DATA_VALID_MTU (0x1 << 0)
697 #define KEV_ND6_DATA_VALID_PREFIX (0x1 << 1)
698
699 struct kev_nd6_ra_data {
700 u_int8_t lladdr[ND6_ROUTER_LL_SIZE];
701 u_int32_t lladdrlen;
702 u_int32_t mtu;
703 u_int32_t list_index;
704 u_int32_t list_length;
705 u_int32_t flags;
706 struct nd6_ra_prefix prefix;
707 u_int32_t pad;
708 };
709 #endif /* PRIVATE */
710
711 #if defined(XNU_KERNEL_PRIVATE)
712 /* nd6.c */
713 extern int nd6_prune;
714 extern int nd6_delay;
715 extern int nd6_umaxtries;
716 extern int nd6_mmaxtries;
717 extern int nd6_useloopback;
718 extern int nd6_accept_6to4;
719 extern int nd6_maxnudhint;
720 extern int nd6_gctimer;
721 extern struct llinfo_nd6 llinfo_nd6;
722 extern struct nd_ifinfo *nd_ifinfo;
723 extern struct nd_drhead nd_defrouter;
724 extern struct nd_prhead nd_prefix;
725 extern int nd6_debug;
726 extern size_t nd_ifinfo_indexlim;
727 extern int nd6_onlink_ns_rfc4861;
728 extern int nd6_optimistic_dad;
729
730 #define nd6log(x) do { if (nd6_debug >= 1) log x; } while (0)
731 #define nd6log2(x) do { if (nd6_debug >= 2) log x; } while (0)
732
733 #define ND6_OPTIMISTIC_DAD_LINKLOCAL (1 << 0)
734 #define ND6_OPTIMISTIC_DAD_AUTOCONF (1 << 1)
735 #define ND6_OPTIMISTIC_DAD_TEMPORARY (1 << 2)
736 #define ND6_OPTIMISTIC_DAD_DYNAMIC (1 << 3)
737
738 /* nd6_rtr.c */
739 extern int nd6_defifindex;
740 extern int ip6_desync_factor; /* seconds */
741 /* ND6_INFINITE_LIFETIME does not apply to temporary addresses */
742 extern u_int32_t ip6_temp_preferred_lifetime; /* seconds */
743 extern u_int32_t ip6_temp_valid_lifetime; /* seconds */
744 extern int ip6_temp_regen_advance; /* seconds */
745
746 union nd_opts {
747 struct nd_opt_hdr *nd_opt_array[8]; /* max = target address list */
748 struct {
749 struct nd_opt_hdr *zero;
750 struct nd_opt_hdr *src_lladdr;
751 struct nd_opt_hdr *tgt_lladdr;
752 struct nd_opt_prefix_info *pi_beg; /* multiple opts, start */
753 struct nd_opt_rd_hdr *rh;
754 struct nd_opt_mtu *mtu;
755 struct nd_opt_hdr *search; /* multiple opts */
756 struct nd_opt_hdr *last; /* multiple opts */
757 int done;
758 struct nd_opt_prefix_info *pi_end;/* multiple opts, end */
759 } nd_opt_each;
760 };
761 #define nd_opts_src_lladdr nd_opt_each.src_lladdr
762 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
763 #define nd_opts_pi nd_opt_each.pi_beg
764 #define nd_opts_pi_end nd_opt_each.pi_end
765 #define nd_opts_rh nd_opt_each.rh
766 #define nd_opts_mtu nd_opt_each.mtu
767 #define nd_opts_search nd_opt_each.search
768 #define nd_opts_last nd_opt_each.last
769 #define nd_opts_done nd_opt_each.done
770
771 /* XXX: need nd6_var.h?? */
772 /* nd6.c */
773 extern void nd6_init(void);
774 extern int nd6_ifattach(struct ifnet *);
775 extern int nd6_is_addr_neighbor(struct sockaddr_in6 *, struct ifnet *, int);
776 extern void nd6_option_init(void *, int, union nd_opts *);
777 extern struct nd_opt_hdr *nd6_option(union nd_opts *);
778 extern int nd6_options(union nd_opts *);
779 extern struct rtentry *nd6_lookup(struct in6_addr *, int, struct ifnet *, int);
780 extern void nd6_setmtu(struct ifnet *);
781 extern void nd6_timer(void *);
782 extern void nd6_purge(struct ifnet *);
783 extern void nd6_free(struct rtentry *);
784 extern void nd6_nud_hint(struct rtentry *, struct in6_addr *, int);
785 extern int nd6_resolve(struct ifnet *, struct rtentry *,
786 struct mbuf *, struct sockaddr *, u_char *);
787 extern void nd6_rtrequest(int, struct rtentry *, struct sockaddr *);
788 extern int nd6_ioctl(u_long, caddr_t, struct ifnet *);
789 extern void nd6_cache_lladdr(struct ifnet *, struct in6_addr *,
790 char *, int, int, int);
791 extern int nd6_output(struct ifnet *, struct ifnet *, struct mbuf *,
792 struct sockaddr_in6 *, struct rtentry *, struct flowadv *);
793 extern int nd6_storelladdr(struct ifnet *, struct rtentry *, struct mbuf *,
794 struct sockaddr *, u_char *);
795 extern int nd6_need_cache(struct ifnet *);
796 extern void nd6_drain(void *);
797 extern void nd6_post_msg(u_int32_t, struct nd_prefix_list *, u_int32_t, u_int32_t, char *, u_int32_t);
798 extern int nd6_setifinfo(struct ifnet *, u_int32_t, u_int32_t);
799
800 /* nd6_nbr.c */
801 extern void nd6_nbr_init(void);
802 extern void nd6_na_input(struct mbuf *, int, int);
803 extern void nd6_na_output(struct ifnet *, const struct in6_addr *,
804 const struct in6_addr *, u_int32_t, int, struct sockaddr *);
805 extern void nd6_ns_input(struct mbuf *, int, int);
806 extern void nd6_ns_output(struct ifnet *, const struct in6_addr *,
807 const struct in6_addr *, struct llinfo_nd6 *, int);
808 extern caddr_t nd6_ifptomac(struct ifnet *);
809 extern void nd6_dad_start(struct ifaddr *, int *);
810 extern void nd6_dad_stop(struct ifaddr *);
811 extern void nd6_dad_duplicated(struct ifaddr *, boolean_t);
812 extern void nd6_llreach_alloc(struct rtentry *, struct ifnet *, void *,
813 unsigned int, boolean_t);
814 extern void nd6_llreach_set_reachable(struct ifnet *, void *, unsigned int);
815 extern void nd6_llreach_use(struct llinfo_nd6 *);
816 extern void nd6_alt_node_addr_decompose(struct ifnet *, struct sockaddr *,
817 struct sockaddr_dl *, struct sockaddr_in6 *);
818 extern void nd6_alt_node_present(struct ifnet *, struct sockaddr_in6 *,
819 struct sockaddr_dl *, int32_t, int, int);
820 extern void nd6_alt_node_absent(struct ifnet *, struct sockaddr_in6 *);
821
822 /* nd6_rtr.c */
823 extern void nd6_rtr_init(void);
824 extern void nd6_rs_input(struct mbuf *, int, int);
825 extern void nd6_ra_input(struct mbuf *, int, int);
826 extern void prelist_del(struct nd_prefix *);
827 extern void defrouter_select(struct ifnet *);
828 extern void defrouter_reset(void);
829 extern int defrtrlist_ioctl(u_long, caddr_t);
830 extern void defrtrlist_del(struct nd_defrouter *);
831 extern int defrtrlist_add_static(struct nd_defrouter *);
832 extern int defrtrlist_del_static(struct nd_defrouter *);
833 extern void prelist_remove(struct nd_prefix *);
834 extern int prelist_update(struct nd_prefix *, struct nd_defrouter *,
835 struct mbuf *, int);
836 extern int nd6_prelist_add(struct nd_prefix *, struct nd_defrouter *,
837 struct nd_prefix **, boolean_t);
838 extern int nd6_prefix_onlink(struct nd_prefix *);
839 extern int nd6_prefix_onlink_scoped(struct nd_prefix *, unsigned int);
840 extern int nd6_prefix_offlink(struct nd_prefix *);
841 extern void pfxlist_onlink_check(void);
842 extern struct nd_defrouter *defrouter_lookup(struct in6_addr *, struct ifnet *);
843 extern struct nd_prefix *nd6_prefix_lookup(struct nd_prefix *);
844 extern int in6_init_prefix_ltimes(struct nd_prefix *ndpr);
845 extern void rt6_flush(struct in6_addr *, struct ifnet *);
846 extern int nd6_setdefaultiface(int);
847 extern int in6_tmpifadd(const struct in6_ifaddr *, int, int);
848 extern void nddr_addref(struct nd_defrouter *, int);
849 extern struct nd_defrouter *nddr_remref(struct nd_defrouter *, int);
850 extern void ndpr_addref(struct nd_prefix *, int);
851 extern struct nd_prefix *ndpr_remref(struct nd_prefix *, int);
852
853 /* nd6_prproxy.c */
854 struct ip6_hdr;
855 extern u_int32_t nd6_prproxy;
856 extern void nd6_prproxy_init(void);
857 extern int nd6_if_prproxy(struct ifnet *, boolean_t);
858 extern void nd6_prproxy_prelist_update(struct nd_prefix *, struct nd_prefix *);
859 extern boolean_t nd6_prproxy_ifaddr(struct in6_ifaddr *);
860 extern boolean_t nd6_prproxy_isours(struct mbuf *, struct ip6_hdr *,
861 struct route_in6 *, unsigned int);
862 extern void nd6_prproxy_ns_output(struct ifnet *, struct in6_addr *,
863 struct in6_addr *, struct llinfo_nd6 *);
864 extern void nd6_prproxy_ns_input(struct ifnet *, struct in6_addr *,
865 char *, int, struct in6_addr *, struct in6_addr *);
866 extern void nd6_prproxy_na_input(struct ifnet *, struct in6_addr *,
867 struct in6_addr *, struct in6_addr *, int);
868 extern void nd6_prproxy_sols_reap(struct nd_prefix *);
869 extern void nd6_prproxy_sols_prune(struct nd_prefix *, u_int32_t);
870 #endif /* XNU_KERNEL_PRIVATE */
871
872 #ifdef KERNEL
873
874 /*!
875 @function nd6_lookup_ipv6
876 @discussion This function will check the routing table for a cached
877 neighbor discovery entry or trigger an neighbor discovery query
878 to resolve the IPv6 address to a link-layer address.
879
880 nd entries are stored in the routing table. This function will
881 lookup the IPv6 destination in the routing table. If the
882 destination requires forwarding to a gateway, the route of the
883 gateway will be looked up. The route entry is inspected to
884 determine if the link layer destination address is known. If
885 unknown, neighbor discovery will be used to resolve the entry.
886 @param interface The interface the packet is being sent on.
887 @param ip6_dest The IPv6 destination of the packet.
888 @param ll_dest On output, the link-layer destination.
889 @param ll_dest_len The length of the buffer for ll_dest.
890 @param hint Any routing hint passed down from the protocol.
891 @param packet The packet being transmitted.
892 @result May return an error such as EHOSTDOWN or ENETUNREACH. If
893 this function returns EJUSTRETURN, the packet has been queued
894 and will be sent when the address is resolved. If any other
895 value is returned, the caller is responsible for disposing of
896 the packet.
897 */
898 extern errno_t nd6_lookup_ipv6(ifnet_t interface,
899 const struct sockaddr_in6 *ip6_dest, struct sockaddr_dl *ll_dest,
900 size_t ll_dest_len, route_t hint, mbuf_t packet);
901
902 #endif /* KERNEL */
903 #endif /* _NETINET6_ND6_H_ */