xref: /openbsd-src/sys/netinet6/in6_proto.c (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1 /*	$OpenBSD: in6_proto.c,v 1.109 2022/02/25 23:51:04 guenther Exp $	*/
2 /*	$KAME: in6_proto.c,v 1.66 2000/10/10 15:35:47 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)in_proto.c	8.1 (Berkeley) 6/10/93
62  */
63 
64 #include <sys/param.h>
65 #include <sys/socket.h>
66 #include <sys/protosw.h>
67 #include <sys/kernel.h>
68 #include <sys/domain.h>
69 #include <sys/mbuf.h>
70 
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/route.h>
74 #include <net/rtable.h>
75 
76 #include <netinet/in.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet/icmp6.h>
83 
84 #include <netinet/tcp.h>
85 #include <netinet/tcp_timer.h>
86 #include <netinet/tcp_var.h>
87 #include <netinet/udp.h>
88 #include <netinet/udp_var.h>
89 #include <netinet/ip_ipsp.h>
90 #include <netinet/ip_ah.h>
91 #include <netinet/ip_esp.h>
92 #include <netinet/ip_ipip.h>
93 
94 #include <netinet6/in6_var.h>
95 #include <netinet6/nd6.h>
96 
97 #include "gif.h"
98 #if NGIF > 0
99 #include <net/if_gif.h>
100 #endif
101 
102 #include "carp.h"
103 #if NCARP > 0
104 #include <netinet/ip_carp.h>
105 #endif
106 
107 #include "pf.h"
108 #if NPF > 0
109 #include <netinet6/ip6_divert.h>
110 #endif
111 
112 #include "etherip.h"
113 #if NETHERIP > 0
114 #include <net/if_etherip.h>
115 #endif
116 
117 #include "gre.h"
118 #if NGRE > 0
119 #include <net/if_gre.h>
120 #endif
121 
122 /*
123  * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
124  */
125 u_char ip6_protox[IPPROTO_MAX];
126 
127 const struct protosw inet6sw[] = {
128 {
129   .pr_domain	= &inet6domain,
130   .pr_protocol	= IPPROTO_IPV6,
131   .pr_init	= ip6_init,
132   .pr_slowtimo	= frag6_slowtimo,
133   .pr_sysctl	= ip6_sysctl
134 },
135 {
136   .pr_type	= SOCK_DGRAM,
137   .pr_domain	= &inet6domain,
138   .pr_protocol	= IPPROTO_UDP,
139   .pr_flags	= PR_ATOMIC|PR_ADDR|PR_SPLICE,
140   .pr_input	= udp_input,
141   .pr_ctlinput	= udp6_ctlinput,
142   .pr_ctloutput	= ip6_ctloutput,
143   .pr_usrreq	= udp_usrreq,
144   .pr_attach	= udp_attach,
145   .pr_detach	= udp_detach,
146   .pr_sysctl	= udp_sysctl
147 },
148 {
149   .pr_type	= SOCK_STREAM,
150   .pr_domain	= &inet6domain,
151   .pr_protocol	= IPPROTO_TCP,
152   .pr_flags	= PR_CONNREQUIRED|PR_WANTRCVD|PR_ABRTACPTDIS|PR_SPLICE,
153   .pr_input	= tcp_input,
154   .pr_ctlinput	= tcp6_ctlinput,
155   .pr_ctloutput	= tcp_ctloutput,
156   .pr_usrreq	= tcp_usrreq,
157   .pr_attach	= tcp_attach,
158   .pr_detach	= tcp_detach,
159   .pr_sysctl	= tcp_sysctl
160 },
161 {
162   .pr_type	= SOCK_RAW,
163   .pr_domain	= &inet6domain,
164   .pr_protocol	= IPPROTO_RAW,
165   .pr_flags	= PR_ATOMIC|PR_ADDR,
166   .pr_input	= rip6_input,
167   .pr_ctlinput	= rip6_ctlinput,
168   .pr_ctloutput	= rip6_ctloutput,
169   .pr_usrreq	= rip6_usrreq,
170   .pr_attach	= rip6_attach,
171   .pr_detach	= rip6_detach,
172   .pr_sysctl	= rip6_sysctl
173 },
174 {
175   .pr_type	= SOCK_RAW,
176   .pr_domain	= &inet6domain,
177   .pr_protocol	= IPPROTO_ICMPV6,
178   .pr_flags	= PR_ATOMIC|PR_ADDR,
179   .pr_input	= icmp6_input,
180   .pr_ctlinput	= rip6_ctlinput,
181   .pr_ctloutput	= rip6_ctloutput,
182   .pr_usrreq	= rip6_usrreq,
183   .pr_attach	= rip6_attach,
184   .pr_detach	= rip6_detach,
185   .pr_init	= icmp6_init,
186   .pr_fasttimo	= icmp6_fasttimo,
187   .pr_sysctl	= icmp6_sysctl
188 },
189 {
190   .pr_type	= SOCK_RAW,
191   .pr_domain	= &inet6domain,
192   .pr_protocol	= IPPROTO_DSTOPTS,
193   .pr_flags	= PR_ATOMIC|PR_ADDR,
194   .pr_input	= dest6_input
195 },
196 {
197   .pr_type	= SOCK_RAW,
198   .pr_domain	= &inet6domain,
199   .pr_protocol	= IPPROTO_ROUTING,
200   .pr_flags	= PR_ATOMIC|PR_ADDR,
201   .pr_input	= route6_input
202 },
203 {
204   .pr_type	= SOCK_RAW,
205   .pr_domain	= &inet6domain,
206   .pr_protocol	= IPPROTO_FRAGMENT,
207   .pr_flags	= PR_ATOMIC|PR_ADDR,
208   .pr_input	= frag6_input
209 },
210 #ifdef IPSEC
211 {
212   .pr_type	= SOCK_RAW,
213   .pr_domain	= &inet6domain,
214   .pr_protocol	= IPPROTO_AH,
215   .pr_flags	= PR_ATOMIC|PR_ADDR,
216   .pr_input	= ah46_input,
217   .pr_ctloutput	= rip6_ctloutput,
218   .pr_usrreq	= rip6_usrreq,
219   .pr_attach	= rip6_attach,
220   .pr_detach	= rip6_detach,
221   .pr_sysctl	= ah_sysctl
222 },
223 {
224   .pr_type	= SOCK_RAW,
225   .pr_domain	= &inet6domain,
226   .pr_protocol	= IPPROTO_ESP,
227   .pr_flags	= PR_ATOMIC|PR_ADDR,
228   .pr_input	= esp46_input,
229   .pr_ctloutput	= rip6_ctloutput,
230   .pr_usrreq	= rip6_usrreq,
231   .pr_attach	= rip6_attach,
232   .pr_detach	= rip6_detach,
233   .pr_sysctl	= esp_sysctl
234 },
235 {
236   .pr_type	= SOCK_RAW,
237   .pr_domain	= &inet6domain,
238   .pr_protocol	= IPPROTO_IPCOMP,
239   .pr_flags	= PR_ATOMIC|PR_ADDR,
240   .pr_input	= ipcomp46_input,
241   .pr_ctloutput	= rip6_ctloutput,
242   .pr_usrreq	= rip6_usrreq,
243   .pr_attach	= rip6_attach,
244   .pr_detach	= rip6_detach,
245   .pr_sysctl	= ipcomp_sysctl
246 },
247 #endif /* IPSEC */
248 {
249   .pr_type	= SOCK_RAW,
250   .pr_domain	= &inet6domain,
251   .pr_protocol	= IPPROTO_IPV4,
252   .pr_flags	= PR_ATOMIC|PR_ADDR,
253 #if NGIF > 0
254   .pr_input	= in6_gif_input,
255 #else
256   .pr_input	= ipip_input,
257 #endif
258   .pr_ctloutput	= rip6_ctloutput,
259   .pr_usrreq	= rip6_usrreq,	/* XXX */
260   .pr_attach	= rip6_attach,
261   .pr_detach	= rip6_detach,
262 },
263 {
264   .pr_type	= SOCK_RAW,
265   .pr_domain	= &inet6domain,
266   .pr_protocol	= IPPROTO_IPV6,
267   .pr_flags	= PR_ATOMIC|PR_ADDR,
268 #if NGIF > 0
269   .pr_input	= in6_gif_input,
270 #else
271   .pr_input	= ipip_input,
272 #endif
273   .pr_ctloutput	= rip6_ctloutput,
274   .pr_usrreq	= rip6_usrreq,	/* XXX */
275   .pr_attach	= rip6_attach,
276   .pr_detach	= rip6_detach,
277 },
278 #if defined(MPLS) && NGIF > 0
279 {
280   .pr_type	= SOCK_RAW,
281   .pr_domain	= &inet6domain,
282   .pr_protocol	= IPPROTO_MPLS,
283   .pr_flags	= PR_ATOMIC|PR_ADDR,
284 #if NGIF > 0
285   .pr_input	= in6_gif_input,
286 #else
287   .pr_input	= ipip_input,
288 #endif
289   .pr_ctloutput	= rip6_ctloutput,
290   .pr_usrreq	= rip6_usrreq,	/* XXX */
291   .pr_attach	= rip6_attach,
292   .pr_detach	= rip6_detach,
293 },
294 #endif /* MPLS */
295 #if NCARP > 0
296 {
297   .pr_type	= SOCK_RAW,
298   .pr_domain	= &inet6domain,
299   .pr_protocol	= IPPROTO_CARP,
300   .pr_flags	= PR_ATOMIC|PR_ADDR,
301   .pr_input	= carp6_proto_input,
302   .pr_ctloutput = rip6_ctloutput,
303   .pr_usrreq	= rip6_usrreq,
304   .pr_attach	= rip6_attach,
305   .pr_detach	= rip6_detach,
306   .pr_sysctl	= carp_sysctl
307 },
308 #endif /* NCARP */
309 #if NPF > 0
310 {
311   .pr_type	= SOCK_RAW,
312   .pr_domain	= &inet6domain,
313   .pr_protocol	= IPPROTO_DIVERT,
314   .pr_flags	= PR_ATOMIC|PR_ADDR,
315   .pr_ctloutput	= rip6_ctloutput,
316   .pr_usrreq	= divert6_usrreq,
317   .pr_attach	= divert6_attach,
318   .pr_detach	= divert6_detach,
319   .pr_init	= divert6_init,
320   .pr_sysctl	= divert6_sysctl
321 },
322 #endif /* NPF > 0 */
323 #if NETHERIP > 0
324 {
325   .pr_type	= SOCK_RAW,
326   .pr_domain	= &inet6domain,
327   .pr_protocol	= IPPROTO_ETHERIP,
328   .pr_flags	= PR_ATOMIC|PR_ADDR,
329   .pr_input	= ip6_etherip_input,
330   .pr_ctloutput	= rip6_ctloutput,
331   .pr_usrreq	= rip6_usrreq,
332   .pr_attach	= rip6_attach,
333   .pr_detach	= rip6_detach,
334 },
335 #endif /* NETHERIP */
336 #if NGRE > 0
337 {
338   .pr_type	= SOCK_RAW,
339   .pr_domain	= &inet6domain,
340   .pr_protocol	= IPPROTO_GRE,
341   .pr_flags	= PR_ATOMIC|PR_ADDR,
342   .pr_input	= gre_input6,
343   .pr_ctloutput	= rip6_ctloutput,
344   .pr_usrreq	= rip6_usrreq,
345   .pr_attach	= rip6_attach,
346   .pr_detach	= rip6_detach,
347 },
348 #endif /* NGRE */
349 {
350   /* raw wildcard */
351   .pr_type	= SOCK_RAW,
352   .pr_domain	= &inet6domain,
353   .pr_flags	= PR_ATOMIC|PR_ADDR,
354   .pr_input	= rip6_input,
355   .pr_ctloutput	= rip6_ctloutput,
356   .pr_usrreq	= rip6_usrreq,
357   .pr_attach	= rip6_attach,
358   .pr_detach	= rip6_detach,
359   .pr_init	= rip6_init
360 }
361 };
362 
363 const struct domain inet6domain = {
364   .dom_family = AF_INET6,
365   .dom_name = "internet6",
366   .dom_protosw = inet6sw,
367   .dom_protoswNPROTOSW = &inet6sw[nitems(inet6sw)],
368   .dom_sasize = sizeof(struct sockaddr_in6),
369   .dom_rtoffset = offsetof(struct sockaddr_in6, sin6_addr),
370   .dom_maxplen = 128,
371   .dom_ifattach = in6_domifattach,
372   .dom_ifdetach = in6_domifdetach
373 };
374 
375 /*
376  * Internet configuration info
377  */
378 int	ip6_forwarding = 0;	/* no forwarding unless sysctl'd to enable */
379 int	ip6_mforwarding = 0;	/* no multicast forwarding unless ... */
380 int	ip6_multipath = 0;	/* no using multipath routes unless ... */
381 int	ip6_sendredirects = 1;
382 int	ip6_defhlim = IPV6_DEFHLIM;
383 int	ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
384 int	ip6_maxfragpackets = 200;
385 int	ip6_maxfrags = 200;
386 int	ip6_log_interval = 5;
387 int	ip6_hdrnestlimit = 10;	/* appropriate? */
388 int	ip6_dad_count = 1;	/* DupAddrDetectionTransmits */
389 int	ip6_dad_pending;	/* number of currently running DADs */
390 int	ip6_auto_flowlabel = 1;
391 int	ip6_use_deprecated = 1;	/* allow deprecated addr (RFC2462 5.5.4) */
392 int	ip6_mcast_pmtu = 0;	/* enable pMTU discovery for multicast? */
393 int	ip6_neighborgcthresh = 2048; /* Threshold # of NDP entries for GC */
394 int	ip6_maxdynroutes = 4096; /* Max # of routes created via redirect */
395 time_t	ip6_log_time = (time_t)0L;
396 
397 /* raw IP6 parameters */
398 /*
399  * Nominal space allocated to a raw ip socket.
400  */
401 #define	RIPV6SNDQ	8192
402 #define	RIPV6RCVQ	8192
403 
404 u_long	rip6_sendspace = RIPV6SNDQ;
405 u_long	rip6_recvspace = RIPV6RCVQ;
406 
407 /* ICMPV6 parameters */
408 int	icmp6_redirtimeout = 10 * 60;	/* 10 minutes */
409 int	icmp6errppslim = 100;		/* 100pps */
410 int	ip6_mtudisc_timeout = IPMTUDISCTIMEOUT;
411