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