xref: /xnu-12377.41.6/bsd/netinet6/in6_proto.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2008-2024 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) 1982, 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_proto.c	8.1 (Berkeley) 6/10/93
91  */
92 
93 
94 #include <sys/param.h>
95 #include <sys/socket.h>
96 #include <sys/socketvar.h>
97 #include <sys/protosw.h>
98 #include <sys/kauth.h>
99 #include <sys/kernel.h>
100 #include <sys/domain.h>
101 #include <sys/mbuf.h>
102 #include <sys/systm.h>
103 #include <sys/sysctl.h>
104 
105 #include <net/if.h>
106 #include <net/radix.h>
107 #include <net/route.h>
108 #include <net/nat464_utils.h>
109 
110 #include <netinet/in.h>
111 #include <netinet/in_systm.h>
112 #include <netinet/in_var.h>
113 #include <netinet/ip_encap.h>
114 #include <netinet/ip.h>
115 #include <netinet/ip_var.h>
116 #include <netinet/ip6.h>
117 #include <netinet6/ip6_var.h>
118 #include <netinet6/in6_var.h>
119 #include <netinet/icmp6.h>
120 
121 #include <netinet/tcp.h>
122 #include <netinet/tcp_timer.h>
123 #include <netinet/tcp_var.h>
124 #include <netinet/udp.h>
125 #include <netinet/udp_var.h>
126 #include <netinet6/tcp6_var.h>
127 #include <netinet6/raw_ip6.h>
128 #include <netinet6/udp6_var.h>
129 #include <netinet6/nd6.h>
130 #include <netinet6/mld6_var.h>
131 
132 #if IPSEC
133 #include <netinet6/ipsec.h>
134 #include <netinet6/ipsec6.h>
135 #include <netinet6/ah.h>
136 #include <netinet6/ah6.h>
137 #if IPSEC_ESP
138 #include <netinet6/esp.h>
139 #include <netinet6/esp6.h>
140 #endif
141 #endif /*IPSEC*/
142 
143 #include <netinet6/ip6protosw.h>
144 
145 #include <net/net_osdep.h>
146 #include <os/log.h>
147 
148 #include <IOKit/IOBSD.h>
149 
150 /*
151  * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
152  */
153 
154 extern struct domain inet6domain_s;
155 struct domain *inet6domain = NULL;
156 
157 static struct pr_usrreqs nousrreqs;
158 lck_mtx_t *inet6_domain_mutex;
159 
160 static void in6_dinit(struct domain *);
161 static int rip6_pr_output(struct mbuf *, struct socket *,
162     struct sockaddr_in6 *, struct mbuf *);
163 
164 struct ip6protosw inet6sw[] = {
165 	{
166 		.pr_type =              0,
167 		.pr_protocol =          IPPROTO_IPV6,
168 		.pr_init =              ip6_init,
169 		.pr_drain =             ip6_drain,
170 		.pr_usrreqs =           &nousrreqs,
171 	},
172 	{
173 		.pr_type =              SOCK_DGRAM,
174 		.pr_protocol =          IPPROTO_UDP,
175 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_PROTOLOCK | PR_PCBLOCK |
176     PR_EVCONNINFO | PR_PRECONN_WRITE,
177 		.pr_input =             udp6_input,
178 		.pr_ctlinput =          udp6_ctlinput,
179 		.pr_ctloutput =         udp_ctloutput,
180 		.pr_init =              udp_init,
181 		.pr_usrreqs =           &udp6_usrreqs,
182 		.pr_lock =              udp_lock,
183 		.pr_unlock =            udp_unlock,
184 		.pr_getlock =           udp_getlock,
185 		.pr_update_last_owner = inp_update_last_owner,
186 		.pr_copy_last_owner =   inp_copy_last_owner,
187 	},
188 	{
189 		.pr_type =              SOCK_STREAM,
190 		.pr_protocol =          IPPROTO_TCP,
191 		.pr_flags =             PR_CONNREQUIRED | PR_WANTRCVD | PR_PCBLOCK |
192     PR_PROTOLOCK | PR_DISPOSE | PR_EVCONNINFO |
193     PR_PRECONN_WRITE | PR_DATA_IDEMPOTENT,
194 		.pr_input =             tcp6_input,
195 		.pr_ctlinput =          tcp6_ctlinput,
196 		.pr_ctloutput =         tcp_ctloutput,
197 		.pr_init =              tcp_init,
198 		.pr_drain =             tcp_drain,
199 		.pr_usrreqs =           &tcp6_usrreqs,
200 		.pr_lock =              tcp_lock,
201 		.pr_unlock =            tcp_unlock,
202 		.pr_getlock =           tcp_getlock,
203 		.pr_update_last_owner = inp_update_last_owner,
204 		.pr_copy_last_owner =   inp_copy_last_owner,
205 	},
206 	{
207 		.pr_type =              SOCK_RAW,
208 		.pr_protocol =          IPPROTO_RAW,
209 		.pr_flags =             PR_ATOMIC | PR_ADDR,
210 		.pr_input =             rip6_input,
211 		.pr_output =            rip6_pr_output,
212 		.pr_ctlinput =          rip6_ctlinput,
213 		.pr_ctloutput =         rip6_ctloutput,
214 		.pr_init =              rip_init,
215 		.pr_usrreqs =           &rip6_usrreqs,
216 		.pr_unlock =            rip_unlock,
217 		.pr_update_last_owner = inp_update_last_owner,
218 		.pr_copy_last_owner =   inp_copy_last_owner,
219 	},
220 	{
221 		.pr_type =              SOCK_RAW,
222 		.pr_protocol =          IPPROTO_ICMPV6,
223 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_LASTHDR,
224 		.pr_input =             icmp6_input,
225 		.pr_output =            rip6_pr_output,
226 		.pr_ctlinput =          rip6_ctlinput,
227 		.pr_ctloutput =         rip6_ctloutput,
228 		.pr_init =              icmp6_init,
229 		.pr_usrreqs =           &rip6_usrreqs,
230 		.pr_unlock =            rip_unlock,
231 		.pr_update_last_owner = inp_update_last_owner,
232 		.pr_copy_last_owner =   inp_copy_last_owner,
233 	},
234 	{
235 		.pr_type =              SOCK_DGRAM,
236 		.pr_protocol =          IPPROTO_ICMPV6,
237 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_LASTHDR,
238 		.pr_input =             icmp6_input,
239 		.pr_output =            rip6_pr_output,
240 		.pr_ctlinput =          rip6_ctlinput,
241 		.pr_ctloutput =         icmp6_dgram_ctloutput,
242 		.pr_init =              icmp6_init,
243 		.pr_usrreqs =           &icmp6_dgram_usrreqs,
244 		.pr_unlock =            rip_unlock,
245 		.pr_update_last_owner = inp_update_last_owner,
246 		.pr_copy_last_owner =   inp_copy_last_owner,
247 	},
248 	{
249 		.pr_type =              SOCK_RAW,
250 		.pr_protocol =          IPPROTO_DSTOPTS,
251 		.pr_flags =             PR_ATOMIC | PR_ADDR,
252 		.pr_input =             dest6_input,
253 		.pr_usrreqs =           &nousrreqs,
254 	},
255 	{
256 		.pr_type =              SOCK_RAW,
257 		.pr_protocol =          IPPROTO_ROUTING,
258 		.pr_flags =             PR_ATOMIC | PR_ADDR,
259 		.pr_input =             route6_input,
260 		.pr_usrreqs =           &nousrreqs,
261 	},
262 	{
263 		.pr_type =              SOCK_RAW,
264 		.pr_protocol =          IPPROTO_FRAGMENT,
265 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_PROTOLOCK,
266 		.pr_input =             frag6_input,
267 		.pr_usrreqs =           &nousrreqs,
268 	},
269 #if IPSEC
270 	{
271 		.pr_type =              SOCK_RAW,
272 		.pr_protocol =          IPPROTO_AH,
273 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_PROTOLOCK,
274 		.pr_input =             ah6_input,
275 		.pr_usrreqs =           &nousrreqs,
276 	},
277 #if IPSEC_ESP
278 	{
279 		.pr_type =              SOCK_RAW,
280 		.pr_protocol =          IPPROTO_ESP,
281 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_PROTOLOCK,
282 		.pr_input =             esp6_input,
283 		.pr_ctlinput =          esp6_ctlinput,
284 		.pr_usrreqs =           &nousrreqs,
285 	},
286 #endif /* IPSEC_ESP */
287 #endif /* IPSEC */
288 #if INET
289 	{
290 		.pr_type =              SOCK_RAW,
291 		.pr_protocol =          IPPROTO_IPV4,
292 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_LASTHDR,
293 		.pr_input =             encap6_input,
294 		.pr_output =            rip6_pr_output,
295 		.pr_ctloutput =         rip6_ctloutput,
296 		.pr_usrreqs =           &rip6_usrreqs,
297 		.pr_unlock =            rip_unlock,
298 		.pr_update_last_owner = inp_update_last_owner,
299 		.pr_copy_last_owner =   inp_copy_last_owner,
300 	},
301 #endif /*INET*/
302 	{
303 		.pr_type =              SOCK_RAW,
304 		.pr_protocol =          IPPROTO_IPV6,
305 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_LASTHDR,
306 		.pr_input =             encap6_input,
307 		.pr_output =            rip6_pr_output,
308 		.pr_ctloutput =         rip6_ctloutput,
309 		.pr_usrreqs =           &rip6_usrreqs,
310 		.pr_unlock =            rip_unlock,
311 		.pr_update_last_owner = inp_update_last_owner,
312 		.pr_copy_last_owner =   inp_copy_last_owner,
313 	},
314 /* raw wildcard */
315 	{
316 		.pr_type =              SOCK_RAW,
317 		.pr_protocol =          0,
318 		.pr_flags =             PR_ATOMIC | PR_ADDR | PR_LASTHDR,
319 		.pr_input =             rip6_input,
320 		.pr_output =            rip6_pr_output,
321 		.pr_ctloutput =         rip6_ctloutput,
322 		.pr_usrreqs =           &rip6_usrreqs,
323 		.pr_unlock =            rip_unlock,
324 		.pr_update_last_owner = inp_update_last_owner,
325 		.pr_copy_last_owner =   inp_copy_last_owner,
326 	},
327 };
328 
329 int in6_proto_count = (sizeof(inet6sw) / sizeof(struct ip6protosw));
330 
331 struct domain inet6domain_s = {
332 	.dom_family =           PF_INET6,
333 	.dom_flags =            DOM_REENTRANT,
334 	.dom_name =             "internet6",
335 	.dom_init =             in6_dinit,
336 	.dom_rtattach =         in6_inithead,
337 	.dom_rtoffset =         offsetof(struct sockaddr_in6, sin6_addr) << 3,
338 	        .dom_maxrtkey =         sizeof(struct sockaddr_in6),
339 	        .dom_protohdrlen =      sizeof(struct sockaddr_in6),
340 };
341 
342 /* Initialize the PF_INET6 domain, and add in the pre-defined protos */
343 void
in6_dinit(struct domain * dp)344 in6_dinit(struct domain *dp)
345 {
346 	struct ip6protosw *pr;
347 	int i;
348 
349 	VERIFY(!(dp->dom_flags & DOM_INITIALIZED));
350 	VERIFY(inet6domain == NULL);
351 
352 	inet6domain = dp;
353 
354 	static_assert(sizeof(struct protosw) == sizeof(struct ip6protosw));
355 	static_assert(offsetof(struct ip6protosw, pr_entry) == offsetof(struct protosw, pr_entry));
356 	static_assert(offsetof(struct ip6protosw, pr_domain) == offsetof(struct protosw, pr_domain));
357 	static_assert(offsetof(struct ip6protosw, pr_protosw) == offsetof(struct protosw, pr_protosw));
358 	static_assert(offsetof(struct ip6protosw, pr_type) == offsetof(struct protosw, pr_type));
359 	static_assert(offsetof(struct ip6protosw, pr_protocol) == offsetof(struct protosw, pr_protocol));
360 	static_assert(offsetof(struct ip6protosw, pr_flags) == offsetof(struct protosw, pr_flags));
361 	static_assert(offsetof(struct ip6protosw, pr_input) == offsetof(struct protosw, pr_input));
362 	static_assert(offsetof(struct ip6protosw, pr_output) == offsetof(struct protosw, pr_output));
363 	static_assert(offsetof(struct ip6protosw, pr_ctlinput) == offsetof(struct protosw, pr_ctlinput));
364 	static_assert(offsetof(struct ip6protosw, pr_ctloutput) == offsetof(struct protosw, pr_ctloutput));
365 	static_assert(offsetof(struct ip6protosw, pr_usrreqs) == offsetof(struct protosw, pr_usrreqs));
366 	static_assert(offsetof(struct ip6protosw, pr_init) == offsetof(struct protosw, pr_init));
367 	static_assert(offsetof(struct ip6protosw, pr_drain) == offsetof(struct protosw, pr_drain));
368 	static_assert(offsetof(struct ip6protosw, pr_lock) == offsetof(struct protosw, pr_lock));
369 	static_assert(offsetof(struct ip6protosw, pr_unlock) == offsetof(struct protosw, pr_unlock));
370 	static_assert(offsetof(struct ip6protosw, pr_getlock) == offsetof(struct protosw, pr_getlock));
371 	static_assert(offsetof(struct ip6protosw, pr_filter_head) == offsetof(struct protosw, pr_filter_head));
372 	static_assert(offsetof(struct ip6protosw, pr_old) == offsetof(struct protosw, pr_old));
373 	static_assert(offsetof(struct ip6protosw, pr_update_last_owner) == offsetof(struct protosw, pr_update_last_owner));
374 	static_assert(offsetof(struct ip6protosw, pr_copy_last_owner) == offsetof(struct protosw, pr_copy_last_owner));
375 	static_assert(offsetof(struct ip6protosw, pr_mem_acct) == offsetof(struct protosw, pr_mem_acct));
376 
377 	/*
378 	 * Attach first, then initialize.  ip6_init() needs raw IP6 handler.
379 	 */
380 	for (i = 0, pr = &inet6sw[0]; i < in6_proto_count; i++, pr++) {
381 		net_add_proto((struct protosw *)pr, dp, 0);
382 	}
383 	for (i = 0, pr = &inet6sw[0]; i < in6_proto_count; i++, pr++) {
384 		net_init_proto((struct protosw *)pr, dp);
385 	}
386 
387 	inet6_domain_mutex = dp->dom_mtx;
388 }
389 
390 static int
rip6_pr_output(struct mbuf * m,struct socket * so,struct sockaddr_in6 * sin6,struct mbuf * m1)391 rip6_pr_output(struct mbuf *m, struct socket *so, struct sockaddr_in6 *sin6,
392     struct mbuf *m1)
393 {
394 #pragma unused(m, so, sin6, m1)
395 	panic("%s", __func__);
396 	/* NOTREACHED */
397 	return 0;
398 }
399 
400 /*
401  * Internet configuration info
402  */
403 #ifndef IPV6FORWARDING
404 #if GATEWAY6
405 #define IPV6FORWARDING  1       /* forward IP6 packets not for us */
406 #else
407 #define IPV6FORWARDING  0       /* don't forward IP6 packets not for us */
408 #endif /* GATEWAY6 */
409 #endif /* !IPV6FORWARDING */
410 
411 #ifndef IPV6_SENDREDIRECTS
412 #define IPV6_SENDREDIRECTS      1
413 #endif
414 
415 int     ip6_forwarding = IPV6FORWARDING;        /* act as router? */
416 int     ip6_sendredirects = IPV6_SENDREDIRECTS;
417 int     ip6_defhlim = IPV6_DEFHLIM;
418 int     ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
419 int     ip6_accept_rtadv = 1;   /* deprecated */
420 int     ip6_log_interval = 5;
421 int     ip6_hdrnestlimit = 15;  /* How many header options will we process? */
422 int     ip6_dad_count = 1;      /* DupAddrDetectionTransmits */
423 int     ip6_auto_flowlabel = 1;
424 int     ip6_gif_hlim = 0;
425 int     ip6_use_deprecated = 1; /* allow deprecated addr [RFC 4862, 5.5.4] */
426 int     ip6_rr_prune = 5;       /* router renumbering prefix
427                                  * walk list every 5 sec.    */
428 int     ip6_mcast_pmtu = 0;     /* enable pMTU discovery for multicast? */
429 int     ip6_v6only = 0;         /* Mapped addresses off by default -  Radar 3347718  -- REVISITING FOR 10.7 -- TESTING WITH MAPPED@ OFF */
430 
431 int     ip6_neighborgcthresh = 1024;    /* Threshold # of NDP entries for GC */
432 int     ip6_maxifprefixes = 16;         /* Max acceptable prefixes via RA per IF */
433 int     ip6_maxifdefrouters = 64;       /* Max acceptable default or RTI routers via RA */
434 int     ip6_maxdynroutes = 1024;        /* Max # of routes created via redirect */
435 int     ip6_only_allow_rfc4193_prefix = 0;      /* Only allow RFC4193 style Unique Local IPv6 Unicast prefixes */
436 
437 static int ip6_keepfaith = 0;
438 uint64_t ip6_log_time = 0;
439 int     nd6_onlink_ns_rfc4861 = 0; /* allow 'on-link' nd6 NS (as in RFC 4861) */
440 
441 /* icmp6 */
442 /*
443  * BSDI4 defines these variables in in_proto.c...
444  * XXX: what if we don't define INET? Should we define pmtu6_expire
445  * or so? ([email protected] 19990310)
446  */
447 int pmtu_expire = 60 * 10;
448 int pmtu_probe = 60 * 2;
449 
450 /* raw IP6 parameters */
451 /*
452  * Nominal space allocated to a raw ip socket.
453  */
454 #define RIPV6SNDQ       8192
455 #define RIPV6RCVQ       8192
456 
457 u_int32_t       rip6_sendspace = RIPV6SNDQ;
458 u_int32_t       rip6_recvspace = RIPV6RCVQ;
459 
460 /* ICMPV6 parameters */
461 int     icmp6_rediraccept = 1;          /* accept and process redirects */
462 int     icmp6_redirtimeout = 10 * 60;   /* 10 minutes */
463 uint32_t     icmp6errppslim = 500;           /* 500 packets per second */
464 uint32_t     icmp6errppslim_random_incr = 500; /* We further randomize icmp6errppslim
465                                                 *  with this during icmpv6 initialization*/
466 int     icmp6rappslim = 10;             /* 10 packets per second */
467 int     icmp6_nodeinfo = 0;             /* enable/disable NI response */
468 
469 /* UDP on IP6 parameters */
470 int     udp6_sendspace = 9216;          /* really max datagram size */
471 int     udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
472 /* 40 1K datagrams */
473 
474 /*
475  * sysctl related items.
476  */
477 SYSCTL_NODE(_net, PF_INET6, inet6,
478     CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Internet6 Family");
479 
480 /* net.inet6 */
481 SYSCTL_NODE(_net_inet6, IPPROTO_IPV6, ip6,
482     CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IP6");
483 SYSCTL_NODE(_net_inet6, IPPROTO_ICMPV6, icmp6,
484     CTLFLAG_RW | CTLFLAG_LOCKED, 0, "ICMP6");
485 SYSCTL_NODE(_net_inet6, IPPROTO_UDP, udp6,
486     CTLFLAG_RW | CTLFLAG_LOCKED, 0, "UDP6");
487 SYSCTL_NODE(_net_inet6, IPPROTO_TCP, tcp6,
488     CTLFLAG_RW | CTLFLAG_LOCKED, 0, "TCP6");
489 #if IPSEC
490 SYSCTL_NODE(_net_inet6, IPPROTO_ESP, ipsec6,
491     CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IPSEC6");
492 #endif /* IPSEC */
493 
494 /* net.inet6.ip6 */
495 static int
496 sysctl_ip6_temppltime SYSCTL_HANDLER_ARGS
497 {
498 #pragma unused(oidp, arg2)
499 	int error = 0;
500 	int value = 0;
501 
502 	error = SYSCTL_OUT(req, arg1, sizeof(int));
503 	if (error || !req->newptr) {
504 		return error;
505 	}
506 
507 	error = SYSCTL_IN(req, &value, sizeof(value));
508 	if (error) {
509 		return error;
510 	}
511 
512 	if (value > ND6_MAX_LIFETIME ||
513 	    value < ip6_desync_factor + ip6_temp_regen_advance) {
514 		return EINVAL;
515 	}
516 
517 	ip6_temp_preferred_lifetime = value;
518 	return error;
519 }
520 
521 static int
522 sysctl_ip6_tempvltime SYSCTL_HANDLER_ARGS
523 {
524 #pragma unused(oidp, arg2)
525 	int error = 0;
526 	uint32_t value = 0;
527 
528 	error = SYSCTL_OUT(req, arg1, sizeof(uint32_t));
529 	if (error || !req->newptr) {
530 		return error;
531 	}
532 
533 	error = SYSCTL_IN(req, &value, sizeof(value));
534 	if (error) {
535 		return error;
536 	}
537 
538 	if (value > ND6_MAX_LIFETIME ||
539 	    value < ip6_temp_preferred_lifetime) {
540 		return EINVAL;
541 	}
542 
543 	ip6_temp_valid_lifetime = value;
544 	return error;
545 }
546 
547 static int
548 sysctl_ip6_cga_conflict_retries SYSCTL_HANDLER_ARGS
549 {
550 #pragma unused(oidp, arg2)
551 	int error = 0;
552 	int value = 0;
553 
554 	error = SYSCTL_OUT(req, arg1, sizeof(int));
555 	if (error || !req->newptr) {
556 		return error;
557 	}
558 
559 	error = SYSCTL_IN(req, &value, sizeof(value));
560 	if (error) {
561 		return error;
562 	}
563 	if (value > IPV6_CGA_CONFLICT_RETRIES_MAX || value < 0) {
564 		return EINVAL;
565 	}
566 
567 	ip6_cga_conflict_retries = value;
568 	return 0;
569 }
570 
571 static int
572 ip6_getstat SYSCTL_HANDLER_ARGS
573 {
574 #pragma unused(oidp, arg1, arg2)
575 	if (req->oldptr == USER_ADDR_NULL) {
576 		req->oldlen = (size_t)sizeof(struct ip6stat);
577 	}
578 
579 	return SYSCTL_OUT(req, &ip6stat, MIN(sizeof(ip6stat), req->oldlen));
580 }
581 
582 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_SENDREDIRECTS,
583     redirect, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_sendredirects, 0, "");
584 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_DEFHLIM,
585     hlim, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_defhlim, 0, "");
586 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_STATS, stats,
587     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
588     0, 0, ip6_getstat, "S,ip6stat", "");
589 
590 #if (DEVELOPMENT || DEBUG)
591 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_ACCEPT_RTADV,
592     accept_rtadv, CTLFLAG_RW | CTLFLAG_LOCKED,
593     &ip6_accept_rtadv, 0, "");
594 #else
595 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_ACCEPT_RTADV,
596     accept_rtadv, CTLFLAG_RD | CTLFLAG_LOCKED,
597     &ip6_accept_rtadv, 0, "");
598 #endif /* (DEVELOPMENT || DEBUG) */
599 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_KEEPFAITH,
600     keepfaith, CTLFLAG_RD | CTLFLAG_LOCKED, &ip6_keepfaith, 0, "");
601 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_LOG_INTERVAL,
602     log_interval, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_log_interval, 0, "");
603 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_HDRNESTLIMIT,
604     hdrnestlimit, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_hdrnestlimit, 0, "");
605 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_DAD_COUNT,
606     dad_count, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_dad_count, 0, "");
607 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_AUTO_FLOWLABEL,
608     auto_flowlabel, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_auto_flowlabel, 0, "");
609 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_DEFMCASTHLIM,
610     defmcasthlim, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_defmcasthlim, 0, "");
611 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM,
612     gifhlim, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_gif_hlim, 0, "");
613 SYSCTL_STRING(_net_inet6_ip6, IPV6CTL_KAME_VERSION,
614     kame_version, CTLFLAG_RD | CTLFLAG_LOCKED, __unsafe_forge_single(void *, __KAME_VERSION), 0, "");
615 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_USE_DEPRECATED,
616     use_deprecated, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_use_deprecated, 0, "");
617 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RR_PRUNE,
618     rr_prune, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_rr_prune, 0, "");
619 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_USETEMPADDR,
620     use_tempaddr, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_use_tempaddr, 0, "");
621 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_ULA_USETEMPADDR,
622     ula_use_tempaddr, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_ula_use_tempaddr, 0, "");
623 SYSCTL_OID(_net_inet6_ip6, IPV6CTL_TEMPPLTIME, temppltime,
624     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_temp_preferred_lifetime, 0,
625     sysctl_ip6_temppltime, "I", "");
626 SYSCTL_OID(_net_inet6_ip6, IPV6CTL_TEMPVLTIME, tempvltime,
627     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_temp_valid_lifetime, 0,
628     sysctl_ip6_tempvltime, "I", "");
629 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_V6ONLY,
630     v6only, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_v6only, 0, "");
631 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_AUTO_LINKLOCAL,
632     auto_linklocal, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_auto_linklocal, 0, "");
633 SYSCTL_STRUCT(_net_inet6_ip6, IPV6CTL_RIP6STATS, rip6stats, CTLFLAG_RD | CTLFLAG_LOCKED,
634     &rip6stat, rip6stat, "");
635 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_PREFER_TEMPADDR,
636     prefer_tempaddr, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_prefer_tempaddr, 0, "");
637 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_USE_DEFAULTZONE,
638     use_defaultzone, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_use_defzone, 0, "");
639 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MCAST_PMTU,
640     mcast_pmtu, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_mcast_pmtu, 0, "");
641 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_NEIGHBORGCTHRESH,
642     neighborgcthresh, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_neighborgcthresh, 0, "");
643 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXIFPREFIXES,
644     maxifprefixes, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxifprefixes, 0, "");
645 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXIFDEFROUTERS,
646     maxifdefrouters, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxifdefrouters, 0, "");
647 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXDYNROUTES,
648     maxdynroutes, CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxdynroutes, 0, "");
649 SYSCTL_INT(_net_inet6_ip6, OID_AUTO,
650     only_allow_rfc4193_prefixes, CTLFLAG_RW | CTLFLAG_LOCKED,
651     &ip6_only_allow_rfc4193_prefix, 0, "");
652 SYSCTL_INT(_net_inet6_ip6, OID_AUTO,
653     clat_debug, CTLFLAG_RW | CTLFLAG_LOCKED, &clat_debug, 0, "");
654 
655 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO,
656     cga_conflict_retries, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
657     &ip6_cga_conflict_retries, 0, sysctl_ip6_cga_conflict_retries, "IU", "");
658 
659 
660 static int sysctl_ip6_forwarding SYSCTL_HANDLER_ARGS;
661 
662 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_FORWARDING, forwarding,
663     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_forwarding, 0,
664     sysctl_ip6_forwarding, "I", "");
665 
666 static int
667 sysctl_ip6_forwarding SYSCTL_HANDLER_ARGS
668 {
669 #pragma unused(arg1, arg2)
670 	int error, i;
671 	char proc_name_string[MAXCOMLEN + 1];
672 	proc_name(proc_pid(current_proc()), proc_name_string, sizeof(proc_name_string));
673 
674 	i = ip6_forwarding;
675 	os_log(OS_LOG_DEFAULT, "%s:%s entry: ip6_forwarding is %d",
676 	    proc_name_string, __func__, ip6_forwarding);
677 
678 	error = sysctl_handle_int(oidp, &i, 0, req);
679 	if (error || req->newptr == USER_ADDR_NULL) {
680 		goto done;
681 	}
682 	/* impose bounds */
683 	if (i < 0) {
684 		error = EINVAL;
685 		goto done;
686 	}
687 
688 	if (i > 0) {
689 		i = 1;
690 	}
691 
692 	ip6_forwarding = i;
693 done:
694 	os_log(OS_LOG_DEFAULT, "%s:%s return: ip6_forwarding is %d "
695 	    "and error is %d", proc_name_string, __func__, ip6_forwarding, error);
696 	return error;
697 }
698 
699 static int sysctl_nd6_debug SYSCTL_HANDLER_ARGS;
700 
701 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DEBUG, nd6_debug,
702     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED, &nd6_debug, 0,
703     sysctl_nd6_debug, "I", "");
704 
705 static int
706 sysctl_nd6_debug SYSCTL_HANDLER_ARGS
707 {
708 #pragma unused(arg1, arg2)
709 	int error;
710 	int old_value = nd6_debug;
711 	int value = old_value;
712 #if (DEBUG || DEVELOPMENT)
713 	char proc_name_string[MAXCOMLEN + 1];
714 
715 	proc_name(proc_pid(current_proc()), proc_name_string, sizeof(proc_name_string));
716 #endif
717 
718 	error = sysctl_handle_int(oidp, &value, 0, req);
719 	if (error || req->newptr == USER_ADDR_NULL) {
720 		goto done;
721 	}
722 
723 	if (!(kauth_cred_issuser(kauth_cred_get()) != 0 ||
724 	    IOCurrentTaskHasEntitlement("com.apple.private.networking.elevated-logging"))) {
725 #if (DEBUG || DEVELOPMENT)
726 		os_log(OS_LOG_DEFAULT, "%s:%s: sysctl not allowed\n",
727 		    proc_name_string, __func__);
728 #endif
729 		error = EPERM;
730 		goto done;
731 	}
732 
733 	/* impose bounds */
734 	if (value < 0) {
735 		error = EINVAL;
736 		goto done;
737 	}
738 
739 	nd6_debug = value;
740 
741 done:
742 #if (DEBUG || DEVELOPMENT)
743 	os_log(OS_LOG_DEFAULT, "%s:%s return: nd6_debug is %d "
744 	    "and error is %d\n", proc_name_string, __func__, nd6_debug, error);
745 #endif
746 	return error;
747 }
748 
749 /*
750  * One single sysctl to set v6 stack profile for IPv6 compliance testing.
751  * A lot of compliance test suites are not aware of other enhancements in IPv6
752  * protocol and expect some arguably obsolete behavior.
753  */
754 static int v6_compliance_profile;
755 static int
756 sysctl_set_v6_compliance_profile SYSCTL_HANDLER_ARGS
757 {
758 #pragma unused(oidp, arg2)
759 	int changed, error;
760 	int value = *(int *) arg1;
761 
762 	error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
763 	if (error || !changed) {
764 		return error;
765 	}
766 
767 	if (value != 0 && value != 1) {
768 		return ERANGE;
769 	}
770 
771 	if (value == 1) {
772 		ip6_use_tempaddr = 0;
773 		dad_enhanced = 0;
774 		icmp6_rediraccept = 1;
775 		nd6_optimistic_dad = 0;
776 		nd6_process_rti = ND6_PROCESS_RTI_ENABLE;
777 	} else {
778 		ip6_use_tempaddr = IP6_USE_TMPADDR_DEFAULT;
779 		dad_enhanced = ND6_DAD_ENHANCED_DEFAULT;
780 		icmp6_rediraccept = ICMP6_REDIRACCEPT_DEFAULT;
781 		nd6_optimistic_dad = ND6_OPTIMISTIC_DAD_DEFAULT;
782 		nd6_process_rti = ND6_PROCESS_RTI_DEFAULT;
783 	}
784 
785 	v6_compliance_profile = value;
786 	return 0;
787 }
788 
789 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, compliance_profile,
790     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
791     &v6_compliance_profile, 0, sysctl_set_v6_compliance_profile,
792     "I", "set IPv6 compliance profile");
793 
794 /* net.inet6.icmp6 */
795 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_REDIRACCEPT,
796     rediraccept, CTLFLAG_RW | CTLFLAG_LOCKED, &icmp6_rediraccept, 0, "");
797 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_REDIRTIMEOUT,
798     redirtimeout, CTLFLAG_RW | CTLFLAG_LOCKED, &icmp6_redirtimeout, 0, "");
799 SYSCTL_STRUCT(_net_inet6_icmp6, ICMPV6CTL_STATS, stats, CTLFLAG_RD | CTLFLAG_LOCKED,
800     &icmp6stat, icmp6stat, "");
801 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_PRUNE,
802     nd6_prune, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_prune, 0, "");
803 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO,
804     nd6_prune_lazy, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_prune_lazy, 0, "");
805 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_DELAY,
806     nd6_delay, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_delay, 0, "");
807 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_UMAXTRIES,
808     nd6_umaxtries, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_umaxtries, 0, "");
809 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MMAXTRIES,
810     nd6_mmaxtries, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_mmaxtries, 0, "");
811 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_USELOOPBACK,
812     nd6_useloopback, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_useloopback, 0, "");
813 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_ACCEPT_6TO4,
814     nd6_accept_6to4, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_accept_6to4, 0, "");
815 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_NODEINFO,
816     nodeinfo, CTLFLAG_RW | CTLFLAG_LOCKED, &icmp6_nodeinfo, 0, "");
817 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ERRPPSLIMIT,
818     errppslimit, CTLFLAG_RW | CTLFLAG_LOCKED, &icmp6errppslim, 0, "");
819 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ERRPPSLIMIT_RANDOM_INCR,
820     errppslimit_random_incr, CTLFLAG_RW | CTLFLAG_LOCKED, &icmp6errppslim_random_incr, 0, "");
821 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO,
822     rappslimit, CTLFLAG_RW | CTLFLAG_LOCKED, &icmp6rappslim, 0, "");
823 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_ONLINKNSRFC4861,
824     nd6_onlink_ns_rfc4861, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_onlink_ns_rfc4861, 0,
825     "Accept 'on-link' nd6 NS in compliance with RFC 4861.");
826 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_OPTIMISTIC_DAD,
827     nd6_optimistic_dad, CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_optimistic_dad, 0, "");
828