xref: /xnu-11215.41.3/bsd/netinet6/in6_var.h (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2000-2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the project nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 /*
59  * Copyright (c) 1985, 1986, 1993
60  *	The Regents of the University of California.  All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions
64  * are met:
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in the
69  *    documentation and/or other materials provided with the distribution.
70  * 3. All advertising materials mentioning features or use of this software
71  *    must display the following acknowledgement:
72  *	This product includes software developed by the University of
73  *	California, Berkeley and its contributors.
74  * 4. Neither the name of the University nor the names of its contributors
75  *    may be used to endorse or promote products derived from this software
76  *    without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88  * SUCH DAMAGE.
89  *
90  *	@(#)in_var.h	8.1 (Berkeley) 6/10/93
91  */
92 
93 #ifndef _NETINET6_IN6_VAR_H_
94 #define _NETINET6_IN6_VAR_H_
95 #include <sys/appleapiopts.h>
96 #include <net/if_var.h>
97 #include <net/net_kev.h>
98 
99 #ifdef BSD_KERNEL_PRIVATE
100 #include <sys/tree.h>
101 #include <sys/mcache.h>
102 #endif /* BSD_KERNEL_PRIVATE */
103 #include <sys/protosw.h>
104 #include <netinet/in.h>
105 #include <netinet6/scope6_var.h>
106 #include <sys/kern_event.h>
107 #include <sys/protosw.h>
108 #include <net/ethernet.h>
109 
110 /*
111  * pltime/vltime are just for future reference (required to implements 2
112  * hour rule for hosts).  they should never be modified by nd6_timeout or
113  * anywhere else.
114  *	userland -> kernel: accept pltime/vltime
115  *	kernel -> userland: throw up everything
116  *	in kernel: modify preferred/expire only
117  */
118 struct in6_addrlifetime {
119 	time_t ia6t_expire;     /* valid lifetime expiration time */
120 	time_t ia6t_preferred;  /* preferred lifetime expiration time */
121 	u_int32_t ia6t_vltime;  /* valid lifetime */
122 	u_int32_t ia6t_pltime;  /* prefix lifetime */
123 };
124 
125 #ifdef BSD_KERNEL_PRIVATE
126 struct in6_addrlifetime_32 {
127 	u_int32_t ia6t_expire;
128 	u_int32_t ia6t_preferred;
129 	u_int32_t ia6t_vltime;
130 	u_int32_t ia6t_pltime;
131 };
132 
133 struct in6_addrlifetime_64 {
134 	u_int64_t ia6t_expire;
135 	u_int64_t ia6t_preferred;
136 	u_int32_t ia6t_vltime;
137 	u_int32_t ia6t_pltime;
138 };
139 
140 /*
141  * Internal representation of ia6_lifetime (expiration in uptime unit)
142  */
143 struct in6_addrlifetime_i {
144 	u_int64_t ia6ti_expire;         /* valid lifetime expiration time */
145 	u_int64_t ia6ti_preferred;      /* preferred lifetime expiration time */
146 	u_int32_t ia6ti_vltime;         /* valid lifetime */
147 	u_int32_t ia6ti_pltime;         /* prefix lifetime */
148 	u_int64_t ia6ti_base_calendartime; /* calendar time at creation */
149 	u_int64_t ia6ti_base_uptime;    /* uptime at creation */
150 };
151 
152 /*
153  * Interface address, IPv6 version.  One of these structures
154  * is allocated for each interface with an IPv6 address.
155  * The ifaddr structure contains the protocol-independent part
156  * of the structure and is assumed to be first.
157  */
158 
159 struct in6_ifaddr {
160 	struct ifaddr ia_ifa;           /* protocol-independent info */
161 #define ia_ifp          ia_ifa.ifa_ifp
162 #define ia_flags        ia_ifa.ifa_flags
163 	struct sockaddr_in6 ia_addr;    /* interface address */
164 	struct sockaddr_in6 ia_net;     /* network number of interface */
165 	struct sockaddr_in6 ia_dstaddr; /* space for destination addr */
166 	struct sockaddr_in6 ia_prefixmask; /* prefix mask */
167 	u_int32_t ia_plen;              /* prefix length */
168 	TAILQ_ENTRY(in6_ifaddr) ia6_link;     /* next in6 list of IP6 addresses */
169 	TAILQ_ENTRY(in6_ifaddr) ia6_hash; /* hash bucket entry */
170 	int ia6_flags;
171 
172 	/* cga collision count */
173 	uint8_t   ia6_cga_collision_count;
174 
175 	struct in6_addrlifetime_i ia6_lifetime;
176 	/*
177 	 * the creation time of this address, which is
178 	 * currently used for temporary addresses only.
179 	 */
180 	u_int64_t ia6_createtime;
181 	u_int64_t ia6_updatetime;
182 
183 	/* back pointer to the ND prefix */
184 	struct nd_prefix *ia6_ndpr;
185 
186 	/* multicast addresses joined from the kernel */
187 	LIST_HEAD(, in6_multi_mship) ia6_memberships;
188 };
189 
190 static inline const struct in6_ifaddr *
191 __attribute__((overloadable)) __pure2
__ifatoia6_const(const struct ifaddr * ifa)192 __ifatoia6_const(const struct ifaddr *ifa)
193 {
194 	return __container_of(ifa, const struct in6_ifaddr, ia_ifa);
195 }
196 static inline struct in6_ifaddr *
197 __attribute__((overloadable)) __pure2
__ifatoia6(struct ifaddr * ifa)198 __ifatoia6(struct ifaddr *ifa)
199 {
200 	return __container_of(ifa, struct in6_ifaddr, ia_ifa);
201 }
202 
203 #if __has_ptrcheck
204 #define ifatoia6(ifa) (_Generic((ifa),                           \
205 	const struct ifaddr * __single         : __ifatoia6_const,   \
206 	const struct ifaddr * __bidi_indexable : __ifatoia6_const,   \
207 	const struct ifaddr * __indexable      : __ifatoia6_const,   \
208 	      struct ifaddr * __single         : __ifatoia6,         \
209 	      struct ifaddr * __bidi_indexable : __ifatoia6,         \
210 	      struct ifaddr * __indexable      : __ifatoia6)         \
211 	((ifa)))
212 #else /* !__has_ptrcheck */
213 #define ifatoia6(ifa) (_Generic((ifa),                           \
214 	const struct ifaddr *                  : __ifatoia6_const,   \
215 	      struct ifaddr *                  : __ifatoia6)         \
216 	((ifa)))
217 #endif /* !__has_ptrcheck */
218 
219 extern TAILQ_HEAD(in6_ifaddrhead, in6_ifaddr) in6_ifaddrhead;
220 extern uint32_t in6_ifaddrlist_genid;
221 extern uint32_t in6addr_nhash;                  /* hash table size */
222 extern uint32_t in6addr_hashp;                  /* next largest prime */
223 extern TAILQ_HEAD(in6_ifaddrhashhead, in6_ifaddr) * __counted_by(in6addr_nhash) in6_ifaddrhashtbl;
224 
225 static __inline uint32_t
in6addr_hashval(const struct in6_addr * in6)226 in6addr_hashval(const struct in6_addr *in6)
227 {
228 	/*
229 	 * The hash index is the computed prime times the key modulo
230 	 * the hash size, as documented in "Introduction to Algorithms"
231 	 * (Cormen, Leiserson, Rivest).
232 	 */
233 	if (in6addr_nhash > 1) {
234 		uint32_t x;
235 
236 		x = in6->s6_addr32[0] ^ in6->s6_addr32[1] ^ in6->s6_addr32[2] ^
237 		    in6->s6_addr32[3];
238 
239 		return (x * in6addr_hashp) % in6addr_nhash;
240 	} else {
241 		return 0;
242 	}
243 }
244 
245 #define IN6ADDR_HASH(x)                 (&in6_ifaddrhashtbl[in6addr_hashval(x)])
246 
247 extern lck_mtx_t nd6_mutex_data;
248 #define nd6_mutex (&nd6_mutex_data)
249 #endif /* BSD_KERNEL_PRIVATE */
250 
251 /* control structure to manage address selection policy */
252 struct in6_addrpolicy {
253 	struct sockaddr_in6 addr; /* prefix address */
254 	struct sockaddr_in6 addrmask; /* prefix mask */
255 	int preced;             /* precedence */
256 	int label;              /* matching label */
257 	u_quad_t use;           /* statistics */
258 };
259 
260 /*
261  * IPv6 interface statistics, as defined in RFC2465 Ipv6IfStatsEntry (p12).
262  */
263 struct in6_ifstat {
264 	u_quad_t ifs6_in_receive;       /* # of total input datagram */
265 	u_quad_t ifs6_in_hdrerr;        /* # of datagrams with invalid hdr */
266 	u_quad_t ifs6_in_toobig;        /* # of datagrams exceeded MTU */
267 	u_quad_t ifs6_in_noroute;       /* # of datagrams with no route */
268 	u_quad_t ifs6_in_addrerr;       /* # of datagrams with invalid dst */
269 	u_quad_t ifs6_in_protounknown;  /* # of datagrams with unknown proto */
270 	                                /* NOTE: increment on final dst if */
271 	u_quad_t ifs6_in_truncated;     /* # of truncated datagrams */
272 	u_quad_t ifs6_in_discard;       /* # of discarded datagrams */
273 	                                /* NOTE: fragment timeout is not here */
274 	u_quad_t ifs6_in_deliver;       /* # of datagrams delivered to ULP */
275 	                                /* NOTE: increment on final dst if */
276 	u_quad_t ifs6_out_forward;      /* # of datagrams forwarded */
277 	                                /* NOTE: increment on outgoing if */
278 	u_quad_t ifs6_out_request;      /* # of outgoing datagrams from ULP */
279 	                                /* NOTE: does not include forwrads */
280 	u_quad_t ifs6_out_discard;      /* # of discarded datagrams */
281 	u_quad_t ifs6_out_fragok;       /* # of datagrams fragmented */
282 	u_quad_t ifs6_out_fragfail;     /* # of datagrams failed on fragment */
283 	u_quad_t ifs6_out_fragcreat;    /* # of fragment datagrams */
284 	                                /* NOTE: this is # after fragment */
285 	u_quad_t ifs6_reass_reqd;       /* # of incoming fragmented packets */
286 	                                /* NOTE: increment on final dst if */
287 	u_quad_t ifs6_reass_ok;         /* # of reassembled packets */
288 	                                /* NOTE: this is # after reass */
289 	                                /* NOTE: increment on final dst if */
290 	u_quad_t ifs6_atmfrag_rcvd;     /* # of atomic fragments received */
291 	u_quad_t ifs6_reass_fail;       /* # of reass failures */
292 	                                /* NOTE: may not be packet count */
293 	                                /* NOTE: increment on final dst if */
294 	u_quad_t ifs6_in_mcast;         /* # of inbound multicast datagrams */
295 	u_quad_t ifs6_out_mcast;        /* # of outbound multicast datagrams */
296 
297 	u_quad_t ifs6_cantfoward_icmp6; /* # of ICMPv6 packets received for unreachable dest */
298 	u_quad_t ifs6_addr_expiry_cnt;  /* # of address expiry events (excluding privacy addresses) */
299 	u_quad_t ifs6_pfx_expiry_cnt;   /* # of prefix expiry events */
300 	u_quad_t ifs6_defrtr_expiry_cnt;        /* # of default router expiry events */
301 };
302 
303 /*
304  * ICMPv6 interface statistics, as defined in RFC2466 Ipv6IfIcmpEntry.
305  * XXX: I'm not sure if this file is the right place for this structure...
306  */
307 struct icmp6_ifstat {
308 	/*
309 	 * Input statistics
310 	 */
311 	/* ipv6IfIcmpInMsgs, total # of input messages */
312 	u_quad_t ifs6_in_msg;
313 	/* ipv6IfIcmpInErrors, # of input error messages */
314 	u_quad_t ifs6_in_error;
315 	/* ipv6IfIcmpInDestUnreachs, # of input dest unreach errors */
316 	u_quad_t ifs6_in_dstunreach;
317 	/* ipv6IfIcmpInAdminProhibs, # of input admin. prohibited errs */
318 	u_quad_t ifs6_in_adminprohib;
319 	/* ipv6IfIcmpInTimeExcds, # of input time exceeded errors */
320 	u_quad_t ifs6_in_timeexceed;
321 	/* ipv6IfIcmpInParmProblems, # of input parameter problem errors */
322 	u_quad_t ifs6_in_paramprob;
323 	/* ipv6IfIcmpInPktTooBigs, # of input packet too big errors */
324 	u_quad_t ifs6_in_pkttoobig;
325 	/* ipv6IfIcmpInEchos, # of input echo requests */
326 	u_quad_t ifs6_in_echo;
327 	/* ipv6IfIcmpInEchoReplies, # of input echo replies */
328 	u_quad_t ifs6_in_echoreply;
329 	/* ipv6IfIcmpInRouterSolicits, # of input router solicitations */
330 	u_quad_t ifs6_in_routersolicit;
331 	/* ipv6IfIcmpInRouterAdvertisements, # of input router advertisements */
332 	u_quad_t ifs6_in_routeradvert;
333 	/* ipv6IfIcmpInNeighborSolicits, # of input neighbor solicitations */
334 	u_quad_t ifs6_in_neighborsolicit;
335 	/* ipv6IfIcmpInNeighborAdvertisements, # of input neighbor advs. */
336 	u_quad_t ifs6_in_neighboradvert;
337 	/* ipv6IfIcmpInRedirects, # of input redirects */
338 	u_quad_t ifs6_in_redirect;
339 	/* ipv6IfIcmpInGroupMembQueries, # of input MLD queries */
340 	u_quad_t ifs6_in_mldquery;
341 	/* ipv6IfIcmpInGroupMembResponses, # of input MLD reports */
342 	u_quad_t ifs6_in_mldreport;
343 	/* ipv6IfIcmpInGroupMembReductions, # of input MLD done */
344 	u_quad_t ifs6_in_mlddone;
345 
346 	/*
347 	 * Output statistics. We should solve unresolved routing problem...
348 	 */
349 	/* ipv6IfIcmpOutMsgs, total # of output messages */
350 	u_quad_t ifs6_out_msg;
351 	/* ipv6IfIcmpOutErrors, # of output error messages */
352 	u_quad_t ifs6_out_error;
353 	/* ipv6IfIcmpOutDestUnreachs, # of output dest unreach errors */
354 	u_quad_t ifs6_out_dstunreach;
355 	/* ipv6IfIcmpOutAdminProhibs, # of output admin. prohibited errs */
356 	u_quad_t ifs6_out_adminprohib;
357 	/* ipv6IfIcmpOutTimeExcds, # of output time exceeded errors */
358 	u_quad_t ifs6_out_timeexceed;
359 	/* ipv6IfIcmpOutParmProblems, # of output parameter problem errors */
360 	u_quad_t ifs6_out_paramprob;
361 	/* ipv6IfIcmpOutPktTooBigs, # of output packet too big errors */
362 	u_quad_t ifs6_out_pkttoobig;
363 	/* ipv6IfIcmpOutEchos, # of output echo requests */
364 	u_quad_t ifs6_out_echo;
365 	/* ipv6IfIcmpOutEchoReplies, # of output echo replies */
366 	u_quad_t ifs6_out_echoreply;
367 	/* ipv6IfIcmpOutRouterSolicits, # of output router solicitations */
368 	u_quad_t ifs6_out_routersolicit;
369 	/* ipv6IfIcmpOutRouterAdvertisements, # of output router advs. */
370 	u_quad_t ifs6_out_routeradvert;
371 	/* ipv6IfIcmpOutNeighborSolicits, # of output neighbor solicitations */
372 	u_quad_t ifs6_out_neighborsolicit;
373 	/* ipv6IfIcmpOutNeighborAdvertisements, # of output neighbor advs. */
374 	u_quad_t ifs6_out_neighboradvert;
375 	/* ipv6IfIcmpOutRedirects, # of output redirects */
376 	u_quad_t ifs6_out_redirect;
377 	/* ipv6IfIcmpOutGroupMembQueries, # of output MLD queries */
378 	u_quad_t ifs6_out_mldquery;
379 	/* ipv6IfIcmpOutGroupMembResponses, # of output MLD reports */
380 	u_quad_t ifs6_out_mldreport;
381 	/* ipv6IfIcmpOutGroupMembReductions, # of output MLD done */
382 	u_quad_t ifs6_out_mlddone;
383 };
384 
385 struct in6_ifreq {
386 	char    ifr_name[IFNAMSIZ];
387 	union {
388 		struct  sockaddr_in6 ifru_addr;
389 		struct  sockaddr_in6 ifru_dstaddr;
390 		int     ifru_flags;
391 		int     ifru_flags6;
392 		int     ifru_metric;
393 		int     ifru_intval;
394 		caddr_t ifru_data;
395 		struct in6_addrlifetime ifru_lifetime;
396 		struct in6_ifstat ifru_stat;
397 		struct icmp6_ifstat ifru_icmp6stat;
398 		u_int32_t ifru_scope_id[SCOPE6_ID_MAX];
399 	} ifr_ifru;
400 };
401 
402 struct in6_aliasreq {
403 	char    ifra_name[IFNAMSIZ];
404 	struct  sockaddr_in6 ifra_addr;
405 	struct  sockaddr_in6 ifra_dstaddr;
406 	struct  sockaddr_in6 ifra_prefixmask;
407 	int     ifra_flags;
408 	struct in6_addrlifetime ifra_lifetime;
409 };
410 
411 #ifdef PRIVATE
412 #define IN6_CGA_MODIFIER_LENGTH 16
413 
414 struct in6_cga_modifier {
415 	u_int8_t octets[IN6_CGA_MODIFIER_LENGTH];
416 };
417 
418 struct in6_cga_prepare {
419 	struct in6_cga_modifier cga_modifier;
420 	u_int8_t cga_security_level;
421 	u_int8_t reserved_A[15];
422 };
423 
424 #define IN6_CGA_KEY_MAXSIZE     2048    /* octets */
425 
426 struct in6_cga_nodecfg {
427 	struct iovec cga_privkey;
428 	struct iovec cga_pubkey;
429 	struct in6_cga_prepare cga_prepare;
430 };
431 
432 struct in6_cgareq {
433 	char cgar_name[IFNAMSIZ];
434 	int cgar_flags;
435 	struct in6_cga_prepare cgar_cgaprep;
436 	struct in6_addrlifetime cgar_lifetime;
437 	uint8_t cgar_collision_count;
438 };
439 
440 #ifdef BSD_KERNEL_PRIVATE
441 struct in6_cgareq_32 {
442 	char cgar_name[IFNAMSIZ];
443 	int cgar_flags;
444 	struct in6_cga_prepare cgar_cgaprep;
445 	struct in6_addrlifetime_32 cgar_lifetime;
446 	uint8_t cgar_collision_count;
447 };
448 
449 struct in6_cgareq_64 {
450 	char cgar_name[IFNAMSIZ];
451 	int cgar_flags;
452 	struct in6_cga_prepare cgar_cgaprep;
453 	struct in6_addrlifetime_64 cgar_lifetime;
454 	uint8_t cgar_collision_count;
455 };
456 
457 #endif /* !BSD_KERNEL_PRIVATE */
458 #endif /* PRIVATE */
459 
460 #ifdef BSD_KERNEL_PRIVATE
461 struct in6_aliasreq_32 {
462 	char    ifra_name[IFNAMSIZ];
463 	struct  sockaddr_in6 ifra_addr;
464 	struct  sockaddr_in6 ifra_dstaddr;
465 	struct  sockaddr_in6 ifra_prefixmask;
466 	int     ifra_flags;
467 	struct in6_addrlifetime_32 ifra_lifetime;
468 };
469 
470 struct in6_aliasreq_64 {
471 	char    ifra_name[IFNAMSIZ];
472 	struct  sockaddr_in6 ifra_addr;
473 	struct  sockaddr_in6 ifra_dstaddr;
474 	struct  sockaddr_in6 ifra_prefixmask;
475 	int     ifra_flags;
476 	struct in6_addrlifetime_64 ifra_lifetime;
477 };
478 #endif /* BSD_KERNEL_PRIVATE */
479 
480 /* prefix type macro */
481 #define IN6_PREFIX_ND   1
482 #define IN6_PREFIX_RR   2
483 
484 /*
485  * prefix related flags passed between kernel(NDP related part) and
486  * user land command(ifconfig) and daemon(rtadvd).
487  */
488 struct in6_prflags {
489 	struct prf_ra {
490 		u_char onlink : 1;
491 		u_char autonomous : 1;
492 		u_char reserved : 6;
493 	} prf_ra;
494 	u_char prf_reserved1;
495 	u_short prf_reserved2;
496 	/* want to put this on 4byte offset */
497 	struct prf_rr {
498 		u_char decrvalid : 1;
499 		u_char decrprefd : 1;
500 		u_char reserved : 6;
501 	} prf_rr;
502 	u_char prf_reserved3;
503 	u_short prf_reserved4;
504 };
505 
506 struct  in6_prefixreq {
507 	char    ipr_name[IFNAMSIZ];
508 	u_char  ipr_origin;
509 	u_char  ipr_plen;
510 	u_int32_t ipr_vltime;
511 	u_int32_t ipr_pltime;
512 	struct in6_prflags ipr_flags;
513 	struct  sockaddr_in6 ipr_prefix;
514 };
515 
516 #define PR_ORIG_RA      0
517 #define PR_ORIG_RR      1
518 #define PR_ORIG_STATIC  2
519 #define PR_ORIG_KERNEL  3
520 
521 #define ipr_raf_onlink          ipr_flags.prf_ra.onlink
522 #define ipr_raf_auto            ipr_flags.prf_ra.autonomous
523 
524 #define ipr_statef_onlink       ipr_flags.prf_state.onlink
525 
526 #define ipr_rrf_decrvalid       ipr_flags.prf_rr.decrvalid
527 #define ipr_rrf_decrprefd       ipr_flags.prf_rr.decrprefd
528 
529 struct in6_rrenumreq {
530 	char    irr_name[IFNAMSIZ];
531 	u_char  irr_origin;
532 	u_char  irr_m_len;      /* match len for matchprefix */
533 	u_char  irr_m_minlen;   /* minlen for matching prefix */
534 	u_char  irr_m_maxlen;   /* maxlen for matching prefix */
535 	u_char  irr_u_uselen;   /* uselen for adding prefix */
536 	u_char  irr_u_keeplen;  /* keeplen from matching prefix */
537 	struct irr_raflagmask {
538 		u_char onlink : 1;
539 		u_char autonomous : 1;
540 		u_char reserved : 6;
541 	} irr_raflagmask;
542 	u_int32_t irr_vltime;
543 	u_int32_t irr_pltime;
544 	struct in6_prflags irr_flags;
545 	struct  sockaddr_in6 irr_matchprefix;
546 	struct  sockaddr_in6 irr_useprefix;
547 };
548 
549 #define irr_raf_mask_onlink     irr_raflagmask.onlink
550 #define irr_raf_mask_auto       irr_raflagmask.autonomous
551 #define irr_raf_mask_reserved   irr_raflagmask.reserved
552 
553 #define irr_raf_onlink          irr_flags.prf_ra.onlink
554 #define irr_raf_auto            irr_flags.prf_ra.autonomous
555 
556 #define irr_statef_onlink       irr_flags.prf_state.onlink
557 
558 #define irr_rrf                 irr_flags.prf_rr
559 #define irr_rrf_decrvalid       irr_flags.prf_rr.decrvalid
560 #define irr_rrf_decrprefd       irr_flags.prf_rr.decrprefd
561 
562 /*
563  * Event data, inet6 style.
564  */
565 struct kev_in6_addrlifetime {
566 	u_int32_t ia6t_expire;
567 	u_int32_t ia6t_preferred;
568 	u_int32_t ia6t_vltime;
569 	u_int32_t ia6t_pltime;
570 };
571 
572 struct kev_in6_data {
573 	struct net_event_data link_data;
574 	struct sockaddr_in6 ia_addr;    /* interface address */
575 	struct sockaddr_in6 ia_net;     /* network number of interface */
576 	struct sockaddr_in6 ia_dstaddr; /* space for destination addr */
577 	struct sockaddr_in6 ia_prefixmask; /* prefix mask */
578 	u_int32_t ia_plen;              /* prefix length */
579 	u_int32_t ia6_flags;            /* address flags from in6_ifaddr */
580 	struct kev_in6_addrlifetime ia_lifetime; /* address life info */
581 	uint8_t ia_mac[ETHER_ADDR_LEN];
582 };
583 
584 #ifdef BSD_KERNEL_PRIVATE
585 /* Utility function used inside netinet6 kernel code for generating events */
586 void in6_post_msg(struct ifnet *, u_int32_t, struct in6_ifaddr *,
587     uint8_t *__sized_by(maclen) mac, size_t maclen);
588 
589 #define IN6_ARE_MASKED_ADDR_EQUAL(d, a, m)      (       \
590 	(((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \
591 	(((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == 0 && \
592 	(((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == 0 && \
593 	(((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == 0)
594 
595 /*
596  * Given a pointer to an in6_ifaddr (ifaddr),
597  * return a pointer to the addr as a sockaddr_in6
598  */
599 #define IA6_IN6(ia)     (&((ia)->ia_addr.sin6_addr))
600 #define IA6_DSTIN6(ia)  (&((ia)->ia_dstaddr.sin6_addr))
601 #define IA6_MASKIN6(ia) (&((ia)->ia_prefixmask.sin6_addr))
602 #define IA6_SIN6(ia)    (&((ia)->ia_addr))
603 #define IA6_DSTSIN6(ia) (&((ia)->ia_dstaddr))
604 #define IA6_SIN6_SCOPE(x) ((IA6_SIN6(x))->sin6_scope_id)
605 
606 #define IFA_IN6(x)      \
607 	(&((struct sockaddr_in6 *)(void *)((x)->ifa_addr))->sin6_addr)
608 #define IFA_SIN6(x)      \
609 	((struct sockaddr_in6 *)(void *)((x)->ifa_addr))
610 #define IFA_DSTIN6(x)   \
611 	(&((struct sockaddr_in6 *)(void *)((x)->ifa_dstaddr))->sin6_addr)
612 #define IFPR_IN6(x)     \
613 	(&((struct sockaddr_in6 *)(void *)((x)->ifpr_prefix))->sin6_addr)
614 #define IFA_SIN6_SCOPE(x) ((IFA_SIN6(x))->sin6_scope_id)
615 
616 #endif /* BSD_KERNEL_PRIVATE */
617 
618 #define SIOCSIFADDR_IN6          _IOW('i', 12, struct in6_ifreq)
619 #define SIOCGIFADDR_IN6         _IOWR('i', 33, struct in6_ifreq)
620 
621 /*
622  * SIOCSxxx ioctls should be unused (see comments in in6.c), but
623  * we do not shift numbers for binary compatibility.
624  */
625 #define SIOCSIFDSTADDR_IN6      _IOW('i', 14, struct in6_ifreq)
626 #define SIOCSIFNETMASK_IN6      _IOW('i', 22, struct in6_ifreq)
627 
628 #define SIOCGIFDSTADDR_IN6      _IOWR('i', 34, struct in6_ifreq)
629 #define SIOCGIFNETMASK_IN6      _IOWR('i', 37, struct in6_ifreq)
630 
631 #define SIOCDIFADDR_IN6         _IOW('i', 25, struct in6_ifreq)
632 #define SIOCAIFADDR_IN6         _IOW('i', 26, struct in6_aliasreq)
633 #ifdef BSD_KERNEL_PRIVATE
634 #define SIOCAIFADDR_IN6_32      _IOW('i', 26, struct in6_aliasreq_32)
635 #define SIOCAIFADDR_IN6_64      _IOW('i', 26, struct in6_aliasreq_64)
636 #endif /* BSD_KERNEL_PRIVATE */
637 
638 #define SIOCSIFPHYADDR_IN6      _IOW('i', 62, struct in6_aliasreq)
639 #ifdef BSD_KERNEL_PRIVATE
640 #define SIOCSIFPHYADDR_IN6_32   _IOW('i', 62, struct in6_aliasreq_32)
641 #define SIOCSIFPHYADDR_IN6_64   _IOW('i', 62, struct in6_aliasreq_64)
642 #endif /* BSD_KERNEL_PRIVATE */
643 #define SIOCGIFPSRCADDR_IN6     _IOWR('i', 63, struct in6_ifreq)
644 #define SIOCGIFPDSTADDR_IN6     _IOWR('i', 64, struct in6_ifreq)
645 #define SIOCGIFAFLAG_IN6        _IOWR('i', 73, struct in6_ifreq)
646 
647 /* removed: #define SIOCGDRLST_IN6          _IOWR('i', 74, struct in6_drlist) */
648 
649 #define OSIOCGIFINFO_IN6        _IOWR('i', 108, struct in6_ondireq)
650 #define SIOCGIFINFO_IN6         _IOWR('i', 76, struct in6_ondireq)
651 #define SIOCSNDFLUSH_IN6        _IOWR('i', 77, struct in6_ifreq)
652 
653 #define SIOCGNBRINFO_IN6        _IOWR('i', 78, struct in6_nbrinfo)
654 #ifdef BSD_KERNEL_PRIVATE
655 #define SIOCGNBRINFO_IN6_32     _IOWR('i', 78, struct in6_nbrinfo_32)
656 #define SIOCGNBRINFO_IN6_64     _IOWR('i', 78, struct in6_nbrinfo_64)
657 #endif /* BSD_KERNEL_PRIVATE */
658 
659 #define SIOCSPFXFLUSH_IN6       _IOWR('i', 79, struct in6_ifreq)
660 #define SIOCSRTRFLUSH_IN6       _IOWR('i', 80, struct in6_ifreq)
661 
662 #define SIOCGIFALIFETIME_IN6    _IOWR('i', 81, struct in6_ifreq)
663 #define SIOCSIFALIFETIME_IN6    _IOWR('i', 82, struct in6_ifreq)
664 #define SIOCGIFSTAT_IN6         _IOWR('i', 83, struct in6_ifreq)
665 #define SIOCGIFSTAT_ICMP6       _IOWR('i', 84, struct in6_ifreq)
666 
667 #define SIOCSDEFIFACE_IN6       _IOWR('i', 85, struct in6_ndifreq)
668 #define SIOCGDEFIFACE_IN6       _IOWR('i', 86, struct in6_ndifreq)
669 #ifdef BSD_KERNEL_PRIVATE
670 #define SIOCSDEFIFACE_IN6_32    _IOWR('i', 85, struct in6_ndifreq_32)
671 #define SIOCSDEFIFACE_IN6_64    _IOWR('i', 85, struct in6_ndifreq_64)
672 #define SIOCGDEFIFACE_IN6_32    _IOWR('i', 86, struct in6_ndifreq_32)
673 #define SIOCGDEFIFACE_IN6_64    _IOWR('i', 86, struct in6_ndifreq_64)
674 #endif /* BSD_KERNEL_PRIVATE */
675 
676 #define SIOCSIFINFO_FLAGS       _IOWR('i', 87, struct in6_ndireq) /* XXX */
677 
678 /* N.B.: These 3 ioctls are deprecated and won't work */
679 #define SIOCSSCOPE6             _IOW('i', 88, struct in6_ifreq)
680 #define SIOCGSCOPE6             _IOWR('i', 89, struct in6_ifreq)
681 #define SIOCGSCOPE6DEF          _IOWR('i', 90, struct in6_ifreq)
682 
683 #define SIOCSIFPREFIX_IN6       _IOW('i', 100, struct in6_prefixreq) /* set */
684 #define SIOCGIFPREFIX_IN6       _IOWR('i', 101, struct in6_prefixreq) /* get */
685 #define SIOCDIFPREFIX_IN6       _IOW('i', 102, struct in6_prefixreq) /* del */
686 #define SIOCAIFPREFIX_IN6       _IOW('i', 103, struct in6_rrenumreq) /* add */
687 
688 /* change */
689 #define SIOCCIFPREFIX_IN6       _IOW('i', 104, struct in6_rrenumreq)
690 
691 /* set global */
692 #define SIOCSGIFPREFIX_IN6      _IOW('i', 105, struct in6_rrenumreq)
693 
694 /*
695  * multicast routing, get s/g pkt cnt, pkt cnt per interface.
696  */
697 #define SIOCGETSGCNT_IN6        _IOWR('u', 28, struct sioc_sg_req6)
698 #define SIOCGETMIFCNT_IN6       _IOWR('u', 107, struct sioc_mif_req6)
699 #ifdef BSD_KERNEL_PRIVATE
700 #define SIOCGETMIFCNT_IN6_32    _IOWR('u', 107, struct sioc_mif_req6_32)
701 #define SIOCGETMIFCNT_IN6_64    _IOWR('u', 107, struct sioc_mif_req6_64)
702 #endif /* BSD_KERNEL_PRIVATE */
703 
704 #define SIOCAADDRCTL_POLICY     _IOW('u', 108, struct in6_addrpolicy)
705 #define SIOCDADDRCTL_POLICY     _IOW('u', 109, struct in6_addrpolicy)
706 
707 #ifdef PRIVATE
708 /*
709  * temporary control calls to attach/detach IPv6 to/from an interface.
710  */
711 #define SIOCPROTOATTACH_IN6     _IOWR('i', 110, struct in6_aliasreq)
712 #ifdef BSD_KERNEL_PRIVATE
713 #define SIOCPROTOATTACH_IN6_32  _IOWR('i', 110, struct in6_aliasreq_32)
714 #define SIOCPROTOATTACH_IN6_64  _IOWR('i', 110, struct in6_aliasreq_64)
715 #endif /* BSD_KERNEL_PRIVATE */
716 #define SIOCPROTODETACH_IN6     _IOWR('i', 111, struct in6_ifreq)
717 
718 /*
719  * start/stop aquiring linklocal on interface
720  */
721 #define SIOCLL_START            _IOWR('i', 130, struct in6_aliasreq)
722 #ifdef BSD_KERNEL_PRIVATE
723 #define SIOCLL_START_32         _IOWR('i', 130, struct in6_aliasreq_32)
724 #define SIOCLL_START_64         _IOWR('i', 130, struct in6_aliasreq_64)
725 #endif /* BSD_KERNEL_PRIVATE */
726 #define SIOCLL_STOP             _IOWR('i', 131, struct in6_ifreq)
727 
728 /*
729  * accept rtadvd (and stop accepting) on this interface.
730  */
731 #define SIOCAUTOCONF_START      _IOWR('i', 132, struct in6_ifreq)
732 #define SIOCAUTOCONF_STOP       _IOWR('i', 133, struct in6_ifreq)
733 
734 /*
735  * add/remove default IPv6 router.
736  */
737 #define SIOCDRADD_IN6           _IOWR('u', 134, struct in6_defrouter)
738 #ifdef BSD_KERNEL_PRIVATE
739 #define SIOCDRADD_IN6_32        _IOWR('u', 134, struct in6_defrouter_32)
740 #define SIOCDRADD_IN6_64        _IOWR('u', 134, struct in6_defrouter_64)
741 #endif /* BSD_KERNEL_PRIVATE */
742 #define SIOCDRDEL_IN6           _IOWR('u', 135, struct in6_defrouter)
743 #ifdef BSD_KERNEL_PRIVATE
744 #define SIOCDRDEL_IN6_32        _IOWR('u', 135, struct in6_defrouter_32)
745 #define SIOCDRDEL_IN6_64        _IOWR('u', 135, struct in6_defrouter_64)
746 #endif /* BSD_KERNEL_PRIVATE */
747 
748 /*
749  * SIOCSETROUTERMODE_IN6
750  * Set the IPv6 router mode on an interface.
751  *
752  * IPV6_ROUTER_MODE_DISABLED
753  * - disable IPv6 router mode if it is enabled
754  * - if the previous mode was IPV6_ROUTER_MODE_EXCUSIVE,
755  *   scrubs all IPv6 auto-configured addresses
756  *
757  * IPV6_ROUTER_MODE_EXCLUSIVE
758  * - act exclusively as an IPv6 router on the interface
759  * - disables accepting external Router Advertisements
760  * - scrubs all IPv6 auto-configured addresses
761  * - disables optimistic dad
762  * - disables ND6 prefix proxy, if enabled
763  * - used by the internet sharing/personal hotspot feature
764  *
765  * IPV6_ROUTER_MODE_HYBRID
766  * - act as both an IPv6 router and IPv6 client on the interface
767  * - does not modify whether to accept Router Advertisements
768  * - does not scrub any addresses
769  * - used when acting as the gateway/router for an otherwise isolated
770  *   network whose existence is likely advertised via a
771  *   a Route Information Option in a Router Advertisement
772  */
773 #define IPV6_ROUTER_MODE_DISABLED       0
774 #define IPV6_ROUTER_MODE_EXCLUSIVE      1
775 #define IPV6_ROUTER_MODE_HYBRID         2
776 #define SIOCSETROUTERMODE_IN6   _IOWR('i', 136, struct in6_ifreq)
777 
778 #define SIOCGETROUTERMODE_IN6   _IOWR('i', 137, struct in6_ifreq)
779 
780 /*
781  * start secure link-local interface addresses
782  */
783 #define SIOCLL_CGASTART         _IOW('i', 160, struct in6_cgareq)
784 #ifdef BSD_KERNEL_PRIVATE
785 #define SIOCLL_CGASTART_32      _IOW('i', 160, struct in6_cgareq_32)
786 #define SIOCLL_CGASTART_64      _IOW('i', 160, struct in6_cgareq_64)
787 #endif
788 
789 /*
790  * get/set the CGA parameters
791  */
792 #define SIOCGIFCGAPREP_IN6      _IOWR('i', 187, struct in6_cgareq)
793 #define SIOCSIFCGAPREP_IN6      _IOWR('i', 188, struct in6_cgareq)
794 #ifdef BSD_KERNEL_PRIVATE
795 #define SIOCGIFCGAPREP_IN6_32   _IOWR('i', 187, struct in6_cgareq_32)
796 #define SIOCGIFCGAPREP_IN6_64   _IOWR('i', 187, struct in6_cgareq_64)
797 #define SIOCSIFCGAPREP_IN6_32   _IOWR('i', 188, struct in6_cgareq_32)
798 #define SIOCSIFCGAPREP_IN6_64   _IOWR('i', 188, struct in6_cgareq_64)
799 #endif
800 
801 #define SIOCCLAT46_START        _IOWR('i', 189, struct in6_ifreq)
802 #define SIOCCLAT46_STOP         _IOWR('i', 190, struct in6_ifreq)
803 #endif /* PRIVATE */
804 
805 #ifdef BSD_KERNEL_PRIVATE
806 /*
807  * XXX: Do not extend IN6_IFF values beyond 16-bit.  We currently copy the
808  *	values from ia6_flags as is into m_pkthdr.{src,dst}_iff which are
809  *	defined as 16-bit variables.  We can relax this once we define a
810  *	unified set of interface flags for {ia,ia6}_flags and perform
811  *	translation between those and the publicly-defined ones below.
812  */
813 #endif /* BSD_KERNEL_PRIVATE */
814 #define IN6_IFF_ANYCAST         0x0001  /* anycast address */
815 #define IN6_IFF_TENTATIVE       0x0002  /* tentative address */
816 #define IN6_IFF_DUPLICATED      0x0004  /* DAD detected duplicate */
817 #define IN6_IFF_DETACHED        0x0008  /* XXX Obsolete. May be detached from the link */
818 #define IN6_IFF_DEPRECATED      0x0010  /* deprecated address */
819 
820 /* don't perform DAD on this address (used only at first SIOC* call) */
821 #define IN6_IFF_NODAD           0x0020
822 
823 #define IN6_IFF_AUTOCONF        0x0040  /* autoconfigurable address. */
824 #define IN6_IFF_TEMPORARY       0x0080  /* temporary (anonymous) address. */
825 #define IN6_IFF_DYNAMIC         0x0100  /* assigned by DHCPv6 service */
826 #define IN6_IFF_OPTIMISTIC      0x0200  /* optimistic DAD, i.e. RFC 4429 */
827 #define IN6_IFF_SECURED         0x0400  /* cryptographically generated */
828 #ifdef PRIVATE
829 #define IN6_IFF_SWIFTDAD        0x0800  /* DAD with no delay */
830 #endif
831 #define IN6_IFF_CLAT46          0x1000  /* Address reserved for CLAT46 */
832 #define IN6_IFF_NOPFX           0x8000  /* Depreciated. Don't use. */
833 
834 /* Duplicate Address Detection [DAD] in progress. */
835 #define IN6_IFF_DADPROGRESS     (IN6_IFF_TENTATIVE|IN6_IFF_OPTIMISTIC)
836 
837 /* do not input/output */
838 #define IN6_IFF_NOTREADY        (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED)
839 
840 /* SLAAC/DHCPv6 address */
841 #define IN6_IFF_NOTMANUAL       (IN6_IFF_AUTOCONF|IN6_IFF_DYNAMIC)
842 
843 #ifdef KERNEL
844 #define IN6_ARE_SCOPE_CMP(a, b)         ((a) - (b))
845 #define IN6_ARE_SCOPE_EQUAL(a, b)       ((a) == (b))
846 #endif /* KERNEL */
847 
848 #ifdef BSD_KERNEL_PRIVATE
849 extern struct icmp6stat icmp6stat;
850 extern lck_grp_t ip6_mutex_grp;
851 extern lck_attr_t ip6_mutex_attr;
852 extern lck_rw_t in6_ifaddr_rwlock;
853 extern lck_mtx_t proxy6_lock;
854 extern u_char inet6ctlerrmap[PRC_NCMDS];
855 extern u_int32_t in6_maxmtu;
856 
857 /* N.B.: if_inet6data is never freed once set, so we don't need to lock */
858 #define in6_ifstat_add_common(_ifp, _tag, _count, _atomic) do {         \
859 	if (_ifp != NULL && IN6_IFEXTRA(_ifp) != NULL) {                \
860 	        if (_atomic)                                            \
861 	                os_atomic_add(                                  \
862 	                        &IN6_IFEXTRA(_ifp)->in6_ifstat._tag,    \
863 	                        _count, relaxed);                       \
864 	        else                                                    \
865 	                IN6_IFEXTRA(_ifp)->in6_ifstat._tag += _count;   \
866 	}                                                               \
867 } while (0)
868 
869 /* atomic version */
870 #define in6_ifstat_inc(_ifp, _tag) \
871 	in6_ifstat_add_common(_ifp, _tag, 1, TRUE)
872 
873 /* non-atomic version (for fast paths) */
874 #define in6_ifstat_inc_na(_ifp, _tag) \
875 	in6_ifstat_add_common(_ifp, _tag, 1, FALSE)
876 
877 /* atomic add version */
878 #define in6_ifstat_add(_ifp, _tag, _count) \
879 	in6_ifstat_add_common(_ifp, _tag, _count, TRUE)
880 
881 /*
882  * Macro for finding the internet address structure (in6_ifaddr) corresponding
883  * to a given interface (ifnet structure).
884  */
885 
886 #define IFP_TO_IA6(ifp, ia)                                         \
887 	/* struct ifnet *ifp; */                                        \
888 	/* struct in6_ifaddr *ia; */                                    \
889 do {                                                                \
890 	struct ifaddr *_ifa;                                            \
891 	ifnet_lock_assert(ifp, LCK_RW_ASSERT_HELD);                     \
892 	for (_ifa = (ifp)->if_addrlist.tqh_first; _ifa != NULL;         \
893 	    _ifa = _ifa->ifa_list.tqe_next) {                           \
894 	        IFA_LOCK(_ifa);                                         \
895 	        if (_ifa->ifa_addr->sa_family == AF_INET6) {            \
896 	                ifa_addref(_ifa);                               \
897 	                IFA_UNLOCK(_ifa);                               \
898 	                break;                                          \
899 	        }                                                       \
900 	        IFA_UNLOCK(_ifa);                                       \
901 	}                                                               \
902 	(ia) = ifatoia6(_ifa);                                          \
903 } while (0)
904 
905 /*
906  * IPv6 multicast MLD-layer source entry.
907  */
908 struct ip6_msource {
909 	RB_ENTRY(ip6_msource)   im6s_link;      /* RB tree links */
910 	struct in6_addr         im6s_addr;
911 	struct im6s_st {
912 		uint16_t        ex;             /* # of exclusive members */
913 		uint16_t        in;             /* # of inclusive members */
914 	}                       im6s_st[2];     /* state at t0, t1 */
915 	uint8_t                 im6s_stp;       /* pending query */
916 };
917 
918 RB_HEAD(ip6_msource_tree, ip6_msource);
919 
920 RB_PROTOTYPE_SC_PREV(__private_extern__, ip6_msource_tree, ip6_msource,
921     im6s_link, ip6_msource_cmp);
922 
923 /*
924  * IPv6 multicast PCB-layer source entry.
925  *
926  * NOTE: overlapping use of struct ip6_msource fields at start.
927  */
928 struct in6_msource {
929 	RB_ENTRY(ip6_msource)   im6s_link;      /* Common field */
930 	struct in6_addr         im6s_addr;      /* Common field */
931 	uint8_t                 im6sl_st[2];    /* state before/at commit */
932 };
933 
934 /*
935  * IPv6 multicast PCB-layer group filter descriptor.
936  */
937 struct in6_mfilter {
938 	struct ip6_msource_tree im6f_sources; /* source list for (S,G) */
939 	u_long                  im6f_nsrc;    /* # of source entries */
940 	uint8_t                 im6f_st[2];   /* state before/at commit */
941 };
942 
943 /*
944  * Legacy KAME IPv6 multicast membership descriptor.
945  */
946 struct in6_multi_mship {
947 	struct  in6_multi *i6mm_maddr;  /* Multicast address pointer */
948 	LIST_ENTRY(in6_multi_mship) i6mm_chain;  /* multicast options chain */
949 };
950 
951 #ifdef BSD_KERNEL_PRIVATE
952 #include <netinet6/nd6_var.h>
953 
954 /*
955  * Per-interface IPv6 structures.
956  */
957 struct in6_ifextra {
958 	struct scope6_id        scope6_id;
959 	struct in6_ifstat       in6_ifstat;
960 	struct icmp6_ifstat     icmp6_ifstat;
961 	struct nd_ifinfo        nd_ifinfo;
962 	uint32_t                netsig_len;
963 	u_int8_t                netsig[IFNET_SIGNATURELEN];
964 	struct ipv6_prefix      nat64_prefixes[NAT64_MAX_NUM_PREFIXES];
965 };
966 #define IN6_IFEXTRA(_ifp)       (_ifp->if_inet6data)
967 #endif /* BSD_KERNEL_PRIVATE */
968 
969 struct mld_ifinfo;
970 
971 /*
972  * IPv6 group descriptor.
973  *
974  * For every entry on an ifnet's if_multiaddrs list which represents
975  * an IP multicast group, there is one of these structures.
976  *
977  * If any source filters are present, then a node will exist in the RB-tree
978  * to permit fast lookup by source whenever an operation takes place.
979  * This permits pre-order traversal when we issue reports.
980  * Source filter trees are kept separately from the socket layer to
981  * greatly simplify locking.
982  *
983  * When MLDv2 is active, in6m_timer is the response to group query timer.
984  * The state-change timer in6m_sctimer is separate; whenever state changes
985  * for the group the state change record is generated and transmitted,
986  * and kept if retransmissions are necessary.
987  *
988  * The request count here is a count of requests for this address, not a
989  * count of pointers to this structure.
990  *
991  * FUTURE: in6m_entry is now only used when groups are being purged
992  * on a detaching ifnet.  It could be demoted to a SLIST_ENTRY.
993  */
994 struct in6_multi {
995 	decl_lck_mtx_data(, in6m_lock);
996 	u_int32_t in6m_refcount;        /* reference count */
997 	u_int32_t in6m_reqcnt;          /* request count for this address */
998 	u_int32_t in6m_debug;           /* see ifa_debug flags */
999 	LIST_ENTRY(in6_multi) in6m_entry; /* list glue */
1000 	struct  in6_addr in6m_addr;     /* IP6 multicast address */
1001 	uint32_t ifscope;               /* IP6 address scope */
1002 	struct  ifnet *in6m_ifp;        /* back pointer to ifnet */
1003 	struct  ifmultiaddr *in6m_ifma; /* back pointer to ifmultiaddr */
1004 	u_int   in6m_state;             /* state of the membership */
1005 	u_int   in6m_timer;             /* MLD6 listener report timer */
1006 	bool    in6m_in_nrele;          /* if in nrele list */
1007 
1008 	/* New fields for MLDv2 follow. */
1009 	struct mld_ifinfo       *in6m_mli;      /* MLD info */
1010 	SLIST_ENTRY(in6_multi)   in6m_dtle;     /* detached waiting for rele  */
1011 	SLIST_ENTRY(in6_multi)   in6m_nrele;    /* to-be-released by MLD */
1012 	u_int32_t                in6m_nrelecnt; /* deferred release count */
1013 	struct ip6_msource_tree  in6m_srcs;     /* tree of sources */
1014 	u_long                   in6m_nsrc;     /* # of tree entries */
1015 
1016 	struct ifqueue           in6m_scq;  /* pending state-change packets */
1017 	struct timeval           in6m_lastgsrtv;        /* last G-S-R query */
1018 	uint16_t                 in6m_sctimer;  /* state-change timer */
1019 	uint16_t                 in6m_scrv;     /* state-change rexmit count */
1020 
1021 	/*
1022 	 * SSM state counters which track state at T0 (the time the last
1023 	 * state-change report's RV timer went to zero) and T1
1024 	 * (time of pending report, i.e. now).
1025 	 * Used for computing MLDv2 state-change reports. Several refcounts
1026 	 * are maintained here to optimize for common use-cases.
1027 	 */
1028 	struct in6m_st {
1029 		uint16_t        iss_fmode;      /* MLD filter mode */
1030 		uint16_t        iss_asm;        /* # of ASM listeners */
1031 		uint16_t        iss_ex;         /* # of exclusive members */
1032 		uint16_t        iss_in;         /* # of inclusive members */
1033 		uint16_t        iss_rec;        /* # of recorded sources */
1034 	}                       in6m_st[2];     /* state at t0, t1 */
1035 
1036 	void (*in6m_trace)              /* callback fn for tracing refs */
1037 	(struct in6_multi *, int);
1038 };
1039 
1040 #define IN6M_LOCK_ASSERT_HELD(_in6m)                                    \
1041 	LCK_MTX_ASSERT(&(_in6m)->in6m_lock, LCK_MTX_ASSERT_OWNED)
1042 
1043 #define IN6M_LOCK_ASSERT_NOTHELD(_in6m)                                 \
1044 	LCK_MTX_ASSERT(&(_in6m)->in6m_lock, LCK_MTX_ASSERT_NOTOWNED)
1045 
1046 #define IN6M_LOCK(_in6m)                                                \
1047 	lck_mtx_lock(&(_in6m)->in6m_lock)
1048 
1049 #define IN6M_LOCK_SPIN(_in6m)                                           \
1050 	lck_mtx_lock_spin(&(_in6m)->in6m_lock)
1051 
1052 #define IN6M_CONVERT_LOCK(_in6m) do {                                   \
1053 	IN6M_LOCK_ASSERT_HELD(_in6m);                                   \
1054 	lck_mtx_convert_spin(&(_in6m)->in6m_lock);                      \
1055 } while (0)
1056 
1057 #define IN6M_UNLOCK(_in6m)                                              \
1058 	lck_mtx_unlock(&(_in6m)->in6m_lock)
1059 
1060 #define IN6M_ADDREF(_in6m)                                              \
1061 	in6m_addref(_in6m, 0)
1062 
1063 #define IN6M_ADDREF_LOCKED(_in6m)                                       \
1064 	in6m_addref(_in6m, 1)
1065 
1066 #define IN6M_REMREF(_in6m)                                              \
1067 	in6m_remref(_in6m, 0)
1068 
1069 #define IN6M_REMREF_LOCKED(_in6m)                                       \
1070 	in6m_remref(_in6m, 1)
1071 
1072 /* flags to in6_update_ifa */
1073 #define IN6_IFAUPDATE_NOWAIT    0x1 /* don't block allocating memory */
1074 #define IN6_IFAUPDATE_1STADDR   0x2 /* first address on interface */
1075 #define IN6_IFAUPDATE_NEWADDR   0x4 /* new address on interface */
1076 #define IN6_IFAUPDATE_DADDELAY  0x8 /* must delay initial DAD probe */
1077 
1078 struct ip6_moptions;
1079 struct sockopt;
1080 struct inpcb;
1081 
1082 extern LIST_HEAD(in6_multihead, in6_multi) in6_multihead;
1083 
1084 /*
1085  * Structure used by macros below to remember position when stepping through
1086  * all of the in6_multi records.
1087  */
1088 struct in6_multistep {
1089 	struct in6_ifaddr *i_ia;
1090 	struct in6_multi *i_in6m;
1091 };
1092 
1093 /*
1094  * Macros for looking up the in6_multi record for a given IP6 multicast
1095  * address on a given interface. If no matching record is found, "in6m"
1096  * returns NULL.
1097  *
1098  * We do this differently compared other BSD implementations; instead of
1099  * walking the if_multiaddrs list at the interface and returning the
1100  * ifma_protospec value of a matching entry, we search the global list
1101  * of in6_multi records and find it that way.  Otherwise either the two
1102  * structures (in6_multi, ifmultiaddr) need to be ref counted both ways,
1103  * which will make things too complicated, or they need to reside in the
1104  * same protected domain, which they aren't.
1105  *
1106  * Must be called with in6_multihead_lock held.
1107  */
1108 #define IN6_LOOKUP_MULTI(addr, ifp, in6m)                               \
1109 	/* struct in6_addr *addr; */                                    \
1110 	/* struct ifnet *ifp; */                                        \
1111 	/* struct in6_multi *in6m; */                                   \
1112 do {                                                                    \
1113 	struct in6_multistep _step;                                     \
1114 	IN6_FIRST_MULTI(_step, in6m);                                   \
1115 	while ((in6m) != NULL) {                                        \
1116 	        IN6M_LOCK_SPIN(in6m);                                   \
1117 	        if ((in6m)->in6m_ifp == (ifp) &&                        \
1118 	            IN6_ARE_ADDR_EQUAL(&(in6m)->in6m_addr, (addr))) {   \
1119 	                IN6M_ADDREF_LOCKED(in6m);                       \
1120 	                IN6M_UNLOCK(in6m);                              \
1121 	                break;                                          \
1122 	        }                                                       \
1123 	        IN6M_UNLOCK(in6m);                                      \
1124 	        IN6_NEXT_MULTI(_step, in6m);                            \
1125 	}                                                               \
1126 } while (0)
1127 
1128 /*
1129  * Macro to step through all of the in6_multi records, one at a time.
1130  * The current position is remembered in "step", which the caller must
1131  * provide.  IN6_FIRST_MULTI(), below, must be called to initialize "step"
1132  * and get the first record.  Both macros return a NULL "in6m" when there
1133  * are no remaining records.
1134  *
1135  * Must be called with in6_multihead_lock held.
1136  */
1137 #define IN6_NEXT_MULTI(step, in6m)                                      \
1138 	/* struct in6_multistep step; */                                \
1139 	/* struct in6_multi *in6m; */                                   \
1140 do {                                                                    \
1141 	in6_multihead_lock_assert(LCK_RW_ASSERT_HELD);                  \
1142 	if (((in6m) = (step).i_in6m) != NULL)                           \
1143 	        (step).i_in6m = (step).i_in6m->in6m_entry.le_next;      \
1144 } while (0)
1145 
1146 #define IN6_FIRST_MULTI(step, in6m)                                     \
1147 	/* struct in6_multistep step; */                                \
1148 	/* struct in6_multi *in6m */                                    \
1149 do {                                                                    \
1150 	in6_multihead_lock_assert(LCK_RW_ASSERT_HELD);                  \
1151 	(step).i_in6m = in6_multihead.lh_first;                         \
1152 	        IN6_NEXT_MULTI((step), (in6m));                         \
1153 } while (0)
1154 
1155 extern lck_mtx_t *inet6_domain_mutex;
1156 extern struct domain *inet6domain;
1157 
1158 struct ip6_pktopts;
1159 
1160 /* Multicast private KPIs. */
1161 extern int im6o_mc_filter(const struct ip6_moptions *, struct ifnet *,
1162     const struct sockaddr_in6 *, const struct sockaddr_in6 *);
1163 extern int in6_mc_join(struct ifnet *, const struct in6_addr *,
1164     struct in6_mfilter *, struct in6_multi **, int);
1165 extern int in6_mc_leave(struct in6_multi *, struct in6_mfilter *);
1166 extern void in6m_clear_recorded(struct in6_multi *);
1167 extern void in6m_commit(struct in6_multi *);
1168 extern void in6m_purge(struct in6_multi *);
1169 extern void in6m_print(const struct in6_multi *);
1170 extern int in6m_record_source(struct in6_multi *, const struct in6_addr *);
1171 extern int ip6_getmoptions(struct inpcb *, struct sockopt *);
1172 extern int ip6_setmoptions(struct inpcb *, struct sockopt *);
1173 /* Legacy KAME multicast private KPIs. */
1174 extern struct in6_multi_mship *in6_joingroup(struct ifnet *,
1175     struct in6_addr *, int *, int);
1176 extern int in6_leavegroup(struct in6_multi_mship *);
1177 extern void in6m_addref(struct in6_multi *, int);
1178 extern void in6m_remref(struct in6_multi *, int);
1179 extern int in6_multi_detach(struct in6_multi *);
1180 extern int in6_ifindex2scopeid(int);
1181 extern int in6_mask2len(struct in6_addr *, u_char *__counted_by(0) lim0);
1182 extern void in6_len2mask(struct in6_addr *, int);
1183 extern int in6_control(struct socket *, u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)),
1184     struct ifnet *, struct proc *);
1185 extern int in6_update_ifa(struct ifnet *, struct in6_aliasreq *, int,
1186     struct in6_ifaddr **);
1187 extern void in6_purgeaddr(struct ifaddr *);
1188 extern int in6if_do_dad(struct ifnet *);
1189 extern void in6_purgeif(struct ifnet *);
1190 extern void in6_savemkludge(struct in6_ifaddr *);
1191 extern void in6_setmaxmtu(void);
1192 extern void in6_restoremkludge(struct in6_ifaddr *, struct ifnet *);
1193 extern void in6_purgemkludge(struct ifnet *);
1194 extern struct in6_ifaddr *in6ifa_ifpforlinklocal(struct ifnet *, int);
1195 extern struct in6_ifaddr *in6ifa_ifpwithflag(struct ifnet *, int);
1196 extern struct in6_ifaddr *in6ifa_ifpwithaddr(struct ifnet *, struct in6_addr *);
1197 extern struct in6_ifaddr *in6ifa_prproxyaddr(struct in6_addr *, uint32_t);
1198 extern void in6ifa_getlifetime(struct in6_ifaddr *,
1199     struct in6_addrlifetime *, int);
1200 extern void in6ifa_setlifetime(struct in6_ifaddr *, struct in6_addrlifetime *);
1201 extern const char *ip6_sprintf(const struct in6_addr *);
1202 extern int in6_addr2scopeid(struct ifnet *, struct in6_addr *);
1203 extern int in6_matchlen(struct in6_addr *, struct in6_addr *);
1204 extern int in6_are_prefix_equal(struct in6_addr *p1, uint32_t ifscope1, struct in6_addr *p2, uint32_t ifscope2,
1205     int len);
1206 extern void in6_prefixlen2mask(struct in6_addr *maskp, int len);
1207 extern int in6_prefix_add_ifid(int iilen, struct in6_ifaddr *ia);
1208 extern void in6_prefix_remove_ifid(int iilen, struct in6_ifaddr *ia);
1209 extern void in6_purgeprefix(struct ifnet *);
1210 extern void in6_purgeaddrs(struct ifnet *);
1211 extern uint8_t im6s_get_mode(const struct in6_multi *,
1212     const struct ip6_msource *, uint8_t);
1213 extern void im6f_leave(struct in6_mfilter *);
1214 extern void im6f_purge(struct in6_mfilter *);
1215 extern int in6_embedscope(struct in6_addr *, const struct sockaddr_in6 *,
1216     struct inpcb *, struct ifnet **, struct ip6_pktopts *, uint32_t *);
1217 extern int in6_recoverscope(struct sockaddr_in6 *, const struct in6_addr *,
1218     struct ifnet *);
1219 extern void in6_aliasreq_64_to_32(struct in6_aliasreq_64 *,
1220     struct in6_aliasreq_32 *);
1221 extern void in6_aliasreq_32_to_64(struct in6_aliasreq_32 *,
1222     struct in6_aliasreq_64 *);
1223 extern void in6_ifaddr_init(void);
1224 extern int in6_inithead(void **, int);
1225 extern void in6_rtqdrain(void);
1226 extern struct radix_node *in6_validate(struct radix_node *);
1227 extern int in6_if2idlen(struct ifnet *);
1228 extern int in6_src_ioctl(u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)));
1229 extern void in6_multihead_lock_exclusive(void);
1230 extern void in6_multihead_lock_shared(void);
1231 extern void in6_multihead_lock_assert(int);
1232 extern void in6_multihead_lock_done(void);
1233 
1234 extern void in6_cga_init(void);
1235 extern void in6_cga_node_lock(void);
1236 extern void in6_cga_node_unlock(void);
1237 extern void in6_cga_query(struct in6_cga_nodecfg *);
1238 extern int in6_cga_start(const struct in6_cga_nodecfg *);
1239 extern int in6_cga_stop(void);
1240 extern int in6_cga_generate(struct in6_cga_prepare *, u_int8_t,
1241     struct in6_addr *, struct ifnet *);
1242 extern int in6_getconninfo(struct socket *, sae_connid_t, uint32_t *,
1243     uint32_t *, int32_t *, user_addr_t, socklen_t *,
1244     user_addr_t, socklen_t *, uint32_t *, user_addr_t, uint32_t *);
1245 extern void in6_ip6_to_sockaddr(const struct in6_addr *ip6, u_int16_t port, uint32_t ifscope,
1246     struct sockaddr_in6 *sin6, u_int32_t maxlen);
1247 extern void in6_cgareq_copy_from_user32(const void *__sized_by(sizeof(struct in6_cgareq_32)), struct in6_cgareq*);
1248 extern void in6_cgareq_copy_from_user64(const void *__sized_by(sizeof(struct in6_cgareq_64)), struct in6_cgareq*);
1249 
1250 #endif /* BSD_KERNEL_PRIVATE */
1251 #endif /* _NETINET6_IN6_VAR_H_ */
1252