xref: /openbsd-src/sys/netinet6/in6_proto.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: in6_proto.c,v 1.62 2011/03/31 10:36:42 jasper 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/radix.h>
73 #ifndef SMALL_KERNEL
74 #include <net/radix_mpath.h>
75 #endif
76 #include <net/route.h>
77 
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet/icmp6.h>
87 
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_timer.h>
90 #include <netinet/tcp_var.h>
91 #include <netinet/udp.h>
92 #include <netinet/udp_var.h>
93 #include <netinet/ip_ipsp.h>
94 #include <netinet/ip_ah.h>
95 #include <netinet/ip_esp.h>
96 #include <netinet/ip_ipip.h>
97 
98 #ifdef MROUTING
99 #include <netinet6/pim6_var.h>
100 #endif
101 
102 #include <netinet6/nd6.h>
103 
104 #include <netinet6/ip6protosw.h>
105 
106 #include "gif.h"
107 #if NGIF > 0
108 #include <netinet6/in6_gif.h>
109 #endif
110 
111 #include "carp.h"
112 #if NCARP > 0
113 #include <netinet/ip_carp.h>
114 #endif
115 
116 #include "pf.h"
117 #if NPF > 0
118 #include <netinet6/ip6_divert.h>
119 #endif
120 
121 /*
122  * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
123  */
124 
125 extern	struct domain inet6domain;
126 
127 struct ip6protosw inet6sw[] = {
128 { 0,		&inet6domain,	IPPROTO_IPV6,	0,
129   0,		0,		0,		0,
130   0,
131   ip6_init,	0,		frag6_slowtimo,	frag6_drain,
132   ip6_sysctl,
133 },
134 { SOCK_DGRAM,	&inet6domain,	IPPROTO_UDP,	PR_ATOMIC|PR_ADDR,
135   udp6_input,	0,		udp6_ctlinput,	ip6_ctloutput,
136   udp_usrreq,	0,
137   0,		0,		0,
138   udp_sysctl,
139 },
140 { SOCK_STREAM,	&inet6domain,	IPPROTO_TCP,	PR_CONNREQUIRED|PR_WANTRCVD|PR_ABRTACPTDIS|PR_SPLICE,
141   tcp6_input,	0,		tcp6_ctlinput,	tcp_ctloutput,
142   tcp_usrreq,
143 #ifdef INET	/* don't call initialization and timeout routines twice */
144   0,		0,		0,		0,
145 #else
146   tcp_init,	tcp_fasttimo,	tcp_slowtimo,	0,
147 #endif
148   tcp_sysctl,
149 },
150 { SOCK_RAW,	&inet6domain,	IPPROTO_RAW,	PR_ATOMIC|PR_ADDR,
151   rip6_input,	rip6_output,	rip6_ctlinput,	rip6_ctloutput,
152   rip6_usrreq,
153   0,		0,		0,		0,		rip6_sysctl
154 },
155 { SOCK_RAW,	&inet6domain,	IPPROTO_ICMPV6,	PR_ATOMIC|PR_ADDR,
156   icmp6_input,	rip6_output,	rip6_ctlinput,	rip6_ctloutput,
157   rip6_usrreq,
158   icmp6_init,	icmp6_fasttimo,	0,		0,
159   icmp6_sysctl,
160 },
161 { SOCK_RAW,	&inet6domain,	IPPROTO_DSTOPTS,PR_ATOMIC|PR_ADDR,
162   dest6_input,	0,	 	0,		0,
163   0,
164   0,		0,		0,		0,
165 },
166 { SOCK_RAW,	&inet6domain,	IPPROTO_ROUTING,PR_ATOMIC|PR_ADDR,
167   route6_input,	0,	 	0,		0,
168   0,
169   0,		0,		0,		0,
170 },
171 { SOCK_RAW,	&inet6domain,	IPPROTO_FRAGMENT,PR_ATOMIC|PR_ADDR,
172   frag6_input,	0,	 	0,		0,
173   0,
174   0,		0,		0,		0,
175 },
176 #ifdef IPSEC
177 { SOCK_RAW,	&inet6domain,	IPPROTO_AH,	PR_ATOMIC|PR_ADDR,
178   ah6_input,	0,	 	0,		0,
179   0,
180   0,		0,		0,		0,
181   ah_sysctl,
182 },
183 { SOCK_RAW,	&inet6domain,	IPPROTO_ESP,	PR_ATOMIC|PR_ADDR,
184   esp6_input,	0,	 	0,		0,
185   0,
186   0,		0,		0,		0,
187   esp_sysctl,
188 },
189 { SOCK_RAW,	&inet6domain,	IPPROTO_IPCOMP,	PR_ATOMIC|PR_ADDR,
190   ipcomp6_input, 0,	 	0,		0,
191   0,
192   0,		0,		0,		0,
193   ipcomp_sysctl,
194 },
195 #endif /* IPSEC */
196 #if NGIF > 0
197 { SOCK_RAW,	&inet6domain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
198   in6_gif_input, rip6_output,	 	0,		rip6_ctloutput,
199   rip6_usrreq,	/* XXX */
200   0,		0,		0,		0,
201 },
202 #ifdef INET
203 { SOCK_RAW,	&inet6domain,	IPPROTO_IPV4,	PR_ATOMIC|PR_ADDR,
204   in6_gif_input, rip6_output, 	0,		rip6_ctloutput,
205   rip6_usrreq,	/* XXX */
206   0,		0,		0,		0,
207 },
208 #endif /* INET */
209 #else /* NGIF */
210 { SOCK_RAW,	&inet6domain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
211   ip4_input6,	rip6_output,	0,		rip6_ctloutput,
212   rip6_usrreq,	/* XXX */
213   0,		0,		0,		0,		ipip_sysctl
214 },
215 #ifdef INET
216 { SOCK_RAW,	&inet6domain,	IPPROTO_IPV4,	PR_ATOMIC|PR_ADDR,
217   ip4_input6,	rip6_output,	0,		rip6_ctloutput,
218   rip6_usrreq,	/* XXX */
219   0,		0,		0,		0,
220 },
221 #endif /* INET */
222 #endif /* GIF */
223 #ifdef MROUTING
224 { SOCK_RAW,	&inet6domain,	IPPROTO_PIM,	PR_ATOMIC|PR_ADDR,
225   pim6_input,	rip6_output,	0,		rip6_ctloutput,
226   rip6_usrreq,
227   0,		0,		0,		0,		pim6_sysctl
228 },
229 #endif
230 #if NCARP > 0
231 { SOCK_RAW,	&inet6domain,	IPPROTO_CARP,	PR_ATOMIC|PR_ADDR,
232   carp6_proto_input,	rip6_output,	0,	rip6_ctloutput,
233   rip6_usrreq,
234   0,		0,		0,		0,		carp_sysctl
235 },
236 #endif /* NCARP */
237 #if NPF > 0
238 { SOCK_RAW,	&inet6domain,	IPPROTO_DIVERT,	PR_ATOMIC|PR_ADDR,
239   divert6_input,	0,		0,	rip6_ctloutput,
240   divert6_usrreq,
241   divert6_init,	0,		0,		0,		divert6_sysctl
242 },
243 #endif /* NPF > 0 */
244 /* raw wildcard */
245 { SOCK_RAW,	&inet6domain,	0,		PR_ATOMIC|PR_ADDR,
246   rip6_input,	rip6_output,	0,		rip6_ctloutput,
247   rip6_usrreq,	rip6_init,
248   0,		0,		0,
249 },
250 };
251 
252 struct domain inet6domain =
253     { AF_INET6, "internet6", 0, 0, 0,
254       (struct protosw *)inet6sw,
255       (struct protosw *)&inet6sw[nitems(inet6sw)], 0,
256 #ifndef SMALL_KERNEL
257       rn_mpath_inithead,
258 #else
259       rn_inithead,
260 #endif
261       offsetof(struct sockaddr_in6, sin6_addr) << 3,
262       sizeof(struct sockaddr_in6),
263       in6_domifattach, in6_domifdetach, };
264 
265 /*
266  * Internet configuration info
267  */
268 int	ip6_forwarding = 0;	/* no forwarding unless sysctl'd to enable */
269 int	ip6_mforwarding = 0;	/* no multicast forwarding unless ... */
270 int	ip6_multipath = 0;	/* no using multipath routes unless ... */
271 int	ip6_sendredirects = 1;
272 int	ip6_defhlim = IPV6_DEFHLIM;
273 int	ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
274 int	ip6_accept_rtadv = 0;	/* enabling forwarding and rtadv concurrently is dangerous */
275 int	ip6_maxfragpackets = 200;
276 int	ip6_maxfrags = 200;
277 int	ip6_log_interval = 5;
278 int	ip6_hdrnestlimit = 10;	/* appropriate? */
279 int	ip6_dad_count = 1;	/* DupAddrDetectionTransmits */
280 int	ip6_dad_pending;	/* number of currently running DADs */
281 int	ip6_auto_flowlabel = 1;
282 int	ip6_use_deprecated = 1;	/* allow deprecated addr (RFC2462 5.5.4) */
283 int	ip6_rr_prune = 5;	/* router renumbering prefix
284 				 * walk list every 5 sec.    */
285 int	ip6_mcast_pmtu = 0;	/* enable pMTU discovery for multicast? */
286 const int ip6_v6only = 1;
287 int	ip6_neighborgcthresh = 2048; /* Threshold # of NDP entries for GC */
288 int	ip6_maxifprefixes = 16; /* Max acceptable prefixes via RA per IF */
289 int	ip6_maxifdefrouters = 16; /* Max acceptable def routers via RA */
290 int	ip6_maxdynroutes = 4096; /* Max # of routes created via redirect */
291 int	ip6_keepfaith = 0;
292 time_t	ip6_log_time = (time_t)0L;
293 
294 /* icmp6 */
295 /*
296  * BSDI4 defines these variables in in_proto.c...
297  * XXX: what if we don't define INET? Should we define pmtu6_expire
298  * or so? (jinmei@kame.net 19990310)
299  */
300 int pmtu_expire = 60*10;
301 
302 /* raw IP6 parameters */
303 /*
304  * Nominal space allocated to a raw ip socket.
305  */
306 #define	RIPV6SNDQ	8192
307 #define	RIPV6RCVQ	8192
308 
309 u_long	rip6_sendspace = RIPV6SNDQ;
310 u_long	rip6_recvspace = RIPV6RCVQ;
311 
312 /* ICMPV6 parameters */
313 int	icmp6_rediraccept = 0;		/* don't process redirects by default */
314 int	icmp6_redirtimeout = 10 * 60;	/* 10 minutes */
315 int	icmp6errppslim = 100;		/* 100pps */
316 int	icmp6_nodeinfo = 1;		/* enable/disable NI response */
317