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