xref: /openbsd/sys/netinet6/in6_proto.c (revision 6f40fd34)
1 /*	$OpenBSD: in6_proto.c,v 1.94 2017/05/18 10:56:45 bluhm 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 #include <netinet6/ip6protosw.h>
97 
98 #include "gif.h"
99 #if NGIF > 0
100 #include <netinet/ip_ether.h>
101 #include <net/if_gif.h>
102 #endif
103 
104 #include "carp.h"
105 #if NCARP > 0
106 #include <netinet/ip_carp.h>
107 #endif
108 
109 #include "pf.h"
110 #if NPF > 0
111 #include <netinet6/ip6_divert.h>
112 #endif
113 
114 #include "etherip.h"
115 #if NETHERIP > 0
116 #include <net/if_etherip.h>
117 #endif
118 
119 /*
120  * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
121  */
122 u_char ip6_protox[IPPROTO_MAX];
123 
124 struct protosw inet6sw[] = {
125 {
126   .pr_domain	= &inet6domain,
127   .pr_protocol	= IPPROTO_IPV6,
128   .pr_init	= ip6_init,
129   .pr_slowtimo	= frag6_slowtimo,
130   .pr_drain	= frag6_drain,
131   .pr_sysctl	= ip6_sysctl
132 },
133 {
134   .pr_type	= SOCK_DGRAM,
135   .pr_domain	= &inet6domain,
136   .pr_protocol	= IPPROTO_UDP,
137   .pr_flags	= PR_ATOMIC|PR_ADDR|PR_SPLICE,
138   .pr_input	= udp_input,
139   .pr_ctlinput	= udp6_ctlinput,
140   .pr_ctloutput	= ip6_ctloutput,
141   .pr_usrreq	= udp_usrreq,
142   .pr_attach	= udp_attach,
143   .pr_sysctl	= udp_sysctl
144 },
145 {
146   .pr_type	= SOCK_STREAM,
147   .pr_domain	= &inet6domain,
148   .pr_protocol	= IPPROTO_TCP,
149   .pr_flags	= PR_CONNREQUIRED|PR_WANTRCVD|PR_ABRTACPTDIS|PR_SPLICE,
150   .pr_input	= tcp_input,
151   .pr_ctlinput	= tcp6_ctlinput,
152   .pr_ctloutput	= tcp_ctloutput,
153   .pr_usrreq	= tcp_usrreq,
154   .pr_attach	= tcp_attach,
155   .pr_sysctl	= tcp_sysctl
156 },
157 {
158   .pr_type	= SOCK_RAW,
159   .pr_domain	= &inet6domain,
160   .pr_protocol	= IPPROTO_RAW,
161   .pr_flags	= PR_ATOMIC|PR_ADDR,
162   .pr_input	= rip6_input,
163   .pr_ctlinput	= rip6_ctlinput,
164   .pr_ctloutput	= rip6_ctloutput,
165   .pr_usrreq	= rip6_usrreq,
166   .pr_attach	= rip6_attach,
167   .pr_sysctl	= rip6_sysctl
168 },
169 {
170   .pr_type	= SOCK_RAW,
171   .pr_domain	= &inet6domain,
172   .pr_protocol	= IPPROTO_ICMPV6,
173   .pr_flags	= PR_ATOMIC|PR_ADDR,
174   .pr_input	= icmp6_input,
175   .pr_ctlinput	= rip6_ctlinput,
176   .pr_ctloutput	= rip6_ctloutput,
177   .pr_usrreq	= rip6_usrreq,
178   .pr_attach	= rip6_attach,
179   .pr_init	= icmp6_init,
180   .pr_fasttimo	= icmp6_fasttimo,
181   .pr_sysctl	= icmp6_sysctl
182 },
183 {
184   .pr_type	= SOCK_RAW,
185   .pr_domain	= &inet6domain,
186   .pr_protocol	= IPPROTO_DSTOPTS,
187   .pr_flags	= PR_ATOMIC|PR_ADDR,
188   .pr_input	= dest6_input
189 },
190 {
191   .pr_type	= SOCK_RAW,
192   .pr_domain	= &inet6domain,
193   .pr_protocol	= IPPROTO_ROUTING,
194   .pr_flags	= PR_ATOMIC|PR_ADDR,
195   .pr_input	= route6_input
196 },
197 {
198   .pr_type	= SOCK_RAW,
199   .pr_domain	= &inet6domain,
200   .pr_protocol	= IPPROTO_FRAGMENT,
201   .pr_flags	= PR_ATOMIC|PR_ADDR,
202   .pr_input	= frag6_input
203 },
204 #ifdef IPSEC
205 {
206   .pr_type	= SOCK_RAW,
207   .pr_domain	= &inet6domain,
208   .pr_protocol	= IPPROTO_AH,
209   .pr_flags	= PR_ATOMIC|PR_ADDR,
210   .pr_input	= ah6_input,
211   .pr_ctloutput	= rip6_ctloutput,
212   .pr_usrreq	= rip6_usrreq,
213   .pr_attach	= rip6_attach,
214   .pr_sysctl	= ah_sysctl
215 },
216 {
217   .pr_type	= SOCK_RAW,
218   .pr_domain	= &inet6domain,
219   .pr_protocol	= IPPROTO_ESP,
220   .pr_flags	= PR_ATOMIC|PR_ADDR,
221   .pr_input	= esp6_input,
222   .pr_ctloutput	= rip6_ctloutput,
223   .pr_usrreq	= rip6_usrreq,
224   .pr_attach	= rip6_attach,
225   .pr_sysctl	= esp_sysctl
226 },
227 {
228   .pr_type	= SOCK_RAW,
229   .pr_domain	= &inet6domain,
230   .pr_protocol	= IPPROTO_IPCOMP,
231   .pr_flags	= PR_ATOMIC|PR_ADDR,
232   .pr_input	= ipcomp6_input,
233   .pr_ctloutput	= rip6_ctloutput,
234   .pr_usrreq	= rip6_usrreq,
235   .pr_attach	= rip6_attach,
236   .pr_sysctl	= ipcomp_sysctl
237 },
238 #endif /* IPSEC */
239 {
240   .pr_type	= SOCK_RAW,
241   .pr_domain	= &inet6domain,
242   .pr_protocol	= IPPROTO_IPV4,
243   .pr_flags	= PR_ATOMIC|PR_ADDR,
244 #if NGIF > 0
245   .pr_input	= in6_gif_input,
246 #else
247   .pr_input	= ipip_input,
248 #endif
249   .pr_ctloutput	= rip6_ctloutput,
250   .pr_usrreq	= rip6_usrreq,	/* XXX */
251   .pr_attach	= rip6_attach
252 },
253 {
254   .pr_type	= SOCK_RAW,
255   .pr_domain	= &inet6domain,
256   .pr_protocol	= IPPROTO_IPV6,
257   .pr_flags	= PR_ATOMIC|PR_ADDR,
258 #if NGIF > 0
259   .pr_input	= in6_gif_input,
260 #else
261   .pr_input	= ipip_input,
262 #endif
263   .pr_ctloutput	= rip6_ctloutput,
264   .pr_usrreq	= rip6_usrreq,	/* XXX */
265   .pr_attach	= rip6_attach,
266 },
267 #if NGIF > 0
268 {
269   .pr_type	= SOCK_RAW,
270   .pr_domain	= &inet6domain,
271   .pr_protocol	= IPPROTO_ETHERIP,
272   .pr_flags	= PR_ATOMIC|PR_ADDR,
273   .pr_input	= etherip_input,
274   .pr_ctloutput	= rip6_ctloutput,
275   .pr_usrreq	= rip6_usrreq,
276   .pr_attach	= rip6_attach,
277   .pr_sysctl	= etherip_sysctl
278 },
279 #endif /* NGIF */
280 #if NCARP > 0
281 {
282   .pr_type	= SOCK_RAW,
283   .pr_domain	= &inet6domain,
284   .pr_protocol	= IPPROTO_CARP,
285   .pr_flags	= PR_ATOMIC|PR_ADDR,
286   .pr_input	= carp6_proto_input,
287   .pr_ctloutput = rip6_ctloutput,
288   .pr_usrreq	= rip6_usrreq,
289   .pr_attach	= rip6_attach,
290   .pr_sysctl	= carp_sysctl
291 },
292 #endif /* NCARP */
293 #if NPF > 0
294 {
295   .pr_type	= SOCK_RAW,
296   .pr_domain	= &inet6domain,
297   .pr_protocol	= IPPROTO_DIVERT,
298   .pr_flags	= PR_ATOMIC|PR_ADDR,
299   .pr_ctloutput	= rip6_ctloutput,
300   .pr_usrreq	= divert6_usrreq,
301   .pr_attach	= divert6_attach,
302   .pr_init	= divert6_init,
303   .pr_sysctl	= divert6_sysctl
304 },
305 #endif /* NPF > 0 */
306 #if NETHERIP > 0
307 {
308   .pr_type	= SOCK_RAW,
309   .pr_domain	= &inet6domain,
310   .pr_protocol	= IPPROTO_ETHERIP,
311   .pr_flags	= PR_ATOMIC|PR_ADDR,
312   .pr_input	= ip6_etherip_input,
313   .pr_ctloutput	= rip6_ctloutput,
314   .pr_usrreq	= rip6_usrreq,
315   .pr_attach	= rip6_attach,
316   .pr_sysctl	= ip_etherip_sysctl
317 },
318 #endif /* NETHERIP */
319 {
320   /* raw wildcard */
321   .pr_type	= SOCK_RAW,
322   .pr_domain	= &inet6domain,
323   .pr_flags	= PR_ATOMIC|PR_ADDR,
324   .pr_input	= rip6_input,
325   .pr_ctloutput	= rip6_ctloutput,
326   .pr_usrreq	= rip6_usrreq,
327   .pr_attach	= rip6_attach,
328   .pr_init	= rip6_init
329 }
330 };
331 
332 struct domain inet6domain = {
333   .dom_family = AF_INET6,
334   .dom_name = "internet6",
335   .dom_protosw = inet6sw,
336   .dom_protoswNPROTOSW = &inet6sw[nitems(inet6sw)],
337   .dom_rtkeylen = sizeof(struct sockaddr_in6),
338   .dom_rtoffset = offsetof(struct sockaddr_in6, sin6_addr),
339   .dom_maxplen = 128,
340   .dom_ifattach = in6_domifattach,
341   .dom_ifdetach = in6_domifdetach
342 };
343 
344 /*
345  * Internet configuration info
346  */
347 int	ip6_forwarding = 0;	/* no forwarding unless sysctl'd to enable */
348 int	ip6_mforwarding = 0;	/* no multicast forwarding unless ... */
349 int	ip6_multipath = 0;	/* no using multipath routes unless ... */
350 int	ip6_sendredirects = 1;
351 int	ip6_defhlim = IPV6_DEFHLIM;
352 int	ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
353 int	ip6_maxfragpackets = 200;
354 int	ip6_maxfrags = 200;
355 int	ip6_log_interval = 5;
356 int	ip6_hdrnestlimit = 10;	/* appropriate? */
357 int	ip6_dad_count = 1;	/* DupAddrDetectionTransmits */
358 int	ip6_dad_pending;	/* number of currently running DADs */
359 int	ip6_auto_flowlabel = 1;
360 int	ip6_use_deprecated = 1;	/* allow deprecated addr (RFC2462 5.5.4) */
361 int	ip6_mcast_pmtu = 0;	/* enable pMTU discovery for multicast? */
362 int	ip6_neighborgcthresh = 2048; /* Threshold # of NDP entries for GC */
363 int	ip6_maxifprefixes = 16; /* Max acceptable prefixes via RA per IF */
364 int	ip6_maxifdefrouters = 16; /* Max acceptable def routers via RA */
365 int	ip6_maxdynroutes = 4096; /* Max # of routes created via redirect */
366 time_t	ip6_log_time = (time_t)0L;
367 
368 /* raw IP6 parameters */
369 /*
370  * Nominal space allocated to a raw ip socket.
371  */
372 #define	RIPV6SNDQ	8192
373 #define	RIPV6RCVQ	8192
374 
375 u_long	rip6_sendspace = RIPV6SNDQ;
376 u_long	rip6_recvspace = RIPV6RCVQ;
377 
378 /* ICMPV6 parameters */
379 int	icmp6_redirtimeout = 10 * 60;	/* 10 minutes */
380 int	icmp6errppslim = 100;		/* 100pps */
381 int	ip6_mtudisc_timeout = IPMTUDISCTIMEOUT;
382