xref: /freebsd/sys/netinet/in_proto.c (revision a0ee8cc6)
1 /*-
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)in_proto.c	8.2 (Berkeley) 2/9/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mrouting.h"
36 #include "opt_ipsec.h"
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_sctp.h"
40 #include "opt_mpath.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/domain.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/queue.h>
50 #include <sys/sysctl.h>
51 
52 /*
53  * While this file provides the domain and protocol switch tables for IPv4, it
54  * also provides the sysctl node declarations for net.inet.* often shared with
55  * IPv6 for common features or by upper layer protocols.  In case of no IPv4
56  * support compile out everything but these sysctl nodes.
57  */
58 #ifdef INET
59 #include <net/if.h>
60 #include <net/if_var.h>
61 #include <net/route.h>
62 #ifdef RADIX_MPATH
63 #include <net/radix_mpath.h>
64 #endif
65 #include <net/vnet.h>
66 #endif /* INET */
67 
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #endif
71 
72 #ifdef INET
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/ip_icmp.h>
78 #include <netinet/igmp_var.h>
79 #include <netinet/tcp.h>
80 #include <netinet/tcp_timer.h>
81 #include <netinet/tcp_var.h>
82 #include <netinet/udp.h>
83 #include <netinet/udp_var.h>
84 #include <netinet/ip_encap.h>
85 
86 /*
87  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
88  */
89 
90 static struct pr_usrreqs nousrreqs;
91 
92 #ifdef IPSEC
93 #include <netipsec/ipsec.h>
94 #endif /* IPSEC */
95 
96 #ifdef SCTP
97 #include <netinet/in_pcb.h>
98 #include <netinet/sctp_pcb.h>
99 #include <netinet/sctp.h>
100 #include <netinet/sctp_var.h>
101 #endif /* SCTP */
102 
103 FEATURE(inet, "Internet Protocol version 4");
104 
105 extern	struct domain inetdomain;
106 
107 /* Spacer for loadable protocols. */
108 #define IPPROTOSPACER   			\
109 {						\
110 	.pr_domain =		&inetdomain,	\
111 	.pr_protocol =		PROTO_SPACER,	\
112 	.pr_usrreqs =		&nousrreqs	\
113 }
114 
115 struct protosw inetsw[] = {
116 {
117 	.pr_type =		0,
118 	.pr_domain =		&inetdomain,
119 	.pr_protocol =		IPPROTO_IP,
120 	.pr_init =		ip_init,
121 #ifdef VIMAGE
122 	.pr_destroy =		ip_destroy,
123 #endif
124 	.pr_slowtimo =		ip_slowtimo,
125 	.pr_drain =		ip_drain,
126 	.pr_usrreqs =		&nousrreqs
127 },
128 {
129 	.pr_type =		SOCK_DGRAM,
130 	.pr_domain =		&inetdomain,
131 	.pr_protocol =		IPPROTO_UDP,
132 	.pr_flags =		PR_ATOMIC|PR_ADDR,
133 	.pr_input =		udp_input,
134 	.pr_ctlinput =		udp_ctlinput,
135 	.pr_ctloutput =		udp_ctloutput,
136 	.pr_init =		udp_init,
137 #ifdef VIMAGE
138 	.pr_destroy =		udp_destroy,
139 #endif
140 	.pr_usrreqs =		&udp_usrreqs
141 },
142 {
143 	.pr_type =		SOCK_STREAM,
144 	.pr_domain =		&inetdomain,
145 	.pr_protocol =		IPPROTO_TCP,
146 	.pr_flags =		PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
147 	.pr_input =		tcp_input,
148 	.pr_ctlinput =		tcp_ctlinput,
149 	.pr_ctloutput =		tcp_ctloutput,
150 	.pr_init =		tcp_init,
151 #ifdef VIMAGE
152 	.pr_destroy =		tcp_destroy,
153 #endif
154 	.pr_slowtimo =		tcp_slowtimo,
155 	.pr_drain =		tcp_drain,
156 	.pr_usrreqs =		&tcp_usrreqs
157 },
158 #ifdef SCTP
159 {
160 	.pr_type =		SOCK_SEQPACKET,
161 	.pr_domain =		&inetdomain,
162 	.pr_protocol =		IPPROTO_SCTP,
163 	.pr_flags =		PR_WANTRCVD,
164 	.pr_input =		sctp_input,
165 	.pr_ctlinput =		sctp_ctlinput,
166 	.pr_ctloutput =		sctp_ctloutput,
167 	.pr_init =		sctp_init,
168 #ifdef VIMAGE
169 	.pr_destroy =		sctp_finish,
170 #endif
171 	.pr_drain =		sctp_drain,
172 	.pr_usrreqs =		&sctp_usrreqs
173 },
174 {
175 	.pr_type =		SOCK_STREAM,
176 	.pr_domain =		&inetdomain,
177 	.pr_protocol =		IPPROTO_SCTP,
178 	.pr_flags =		PR_WANTRCVD,
179 	.pr_input =		sctp_input,
180 	.pr_ctlinput =		sctp_ctlinput,
181 	.pr_ctloutput =		sctp_ctloutput,
182 	.pr_drain =		sctp_drain,
183 	.pr_usrreqs =		&sctp_usrreqs
184 },
185 #endif /* SCTP */
186 {
187 	.pr_type =		SOCK_DGRAM,
188 	.pr_domain =		&inetdomain,
189 	.pr_protocol =		IPPROTO_UDPLITE,
190 	.pr_flags =		PR_ATOMIC|PR_ADDR,
191 	.pr_input =		udp_input,
192 	.pr_ctlinput =		udplite_ctlinput,
193 	.pr_ctloutput =		udp_ctloutput,
194 	.pr_init =		udplite_init,
195 #ifdef VIMAGE
196 	.pr_destroy =		udplite_destroy,
197 #endif
198 	.pr_usrreqs =		&udp_usrreqs
199 },
200 {
201 	.pr_type =		SOCK_RAW,
202 	.pr_domain =		&inetdomain,
203 	.pr_protocol =		IPPROTO_RAW,
204 	.pr_flags =		PR_ATOMIC|PR_ADDR,
205 	.pr_input =		rip_input,
206 	.pr_ctlinput =		rip_ctlinput,
207 	.pr_ctloutput =		rip_ctloutput,
208 	.pr_usrreqs =		&rip_usrreqs
209 },
210 {
211 	.pr_type =		SOCK_RAW,
212 	.pr_domain =		&inetdomain,
213 	.pr_protocol =		IPPROTO_ICMP,
214 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
215 	.pr_input =		icmp_input,
216 	.pr_ctloutput =		rip_ctloutput,
217 	.pr_usrreqs =		&rip_usrreqs
218 },
219 {
220 	.pr_type =		SOCK_RAW,
221 	.pr_domain =		&inetdomain,
222 	.pr_protocol =		IPPROTO_IGMP,
223 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
224 	.pr_input =		igmp_input,
225 	.pr_ctloutput =		rip_ctloutput,
226 	.pr_fasttimo =		igmp_fasttimo,
227 	.pr_slowtimo =		igmp_slowtimo,
228 	.pr_usrreqs =		&rip_usrreqs
229 },
230 {
231 	.pr_type =		SOCK_RAW,
232 	.pr_domain =		&inetdomain,
233 	.pr_protocol =		IPPROTO_RSVP,
234 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
235 	.pr_input =		rsvp_input,
236 	.pr_ctloutput =		rip_ctloutput,
237 	.pr_usrreqs =		&rip_usrreqs
238 },
239 #ifdef IPSEC
240 {
241 	.pr_type =		SOCK_RAW,
242 	.pr_domain =		&inetdomain,
243 	.pr_protocol =		IPPROTO_AH,
244 	.pr_flags =		PR_ATOMIC|PR_ADDR,
245 	.pr_input =		ah4_input,
246 	.pr_ctlinput =		ah4_ctlinput,
247 	.pr_usrreqs =		&nousrreqs
248 },
249 {
250 	.pr_type =		SOCK_RAW,
251 	.pr_domain =		&inetdomain,
252 	.pr_protocol =		IPPROTO_ESP,
253 	.pr_flags =		PR_ATOMIC|PR_ADDR,
254 	.pr_input =		esp4_input,
255 	.pr_ctlinput =		esp4_ctlinput,
256 	.pr_usrreqs =		&nousrreqs
257 },
258 {
259 	.pr_type =		SOCK_RAW,
260 	.pr_domain =		&inetdomain,
261 	.pr_protocol =		IPPROTO_IPCOMP,
262 	.pr_flags =		PR_ATOMIC|PR_ADDR,
263 	.pr_input =		ipcomp4_input,
264 	.pr_usrreqs =		&nousrreqs
265 },
266 #endif /* IPSEC */
267 {
268 	.pr_type =		SOCK_RAW,
269 	.pr_domain =		&inetdomain,
270 	.pr_protocol =		IPPROTO_IPV4,
271 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
272 	.pr_input =		encap4_input,
273 	.pr_ctloutput =		rip_ctloutput,
274 	.pr_init =		encap_init,
275 	.pr_usrreqs =		&rip_usrreqs
276 },
277 {
278 	.pr_type =		SOCK_RAW,
279 	.pr_domain =		&inetdomain,
280 	.pr_protocol =		IPPROTO_MOBILE,
281 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
282 	.pr_input =		encap4_input,
283 	.pr_ctloutput =		rip_ctloutput,
284 	.pr_init =		encap_init,
285 	.pr_usrreqs =		&rip_usrreqs
286 },
287 {
288 	.pr_type =		SOCK_RAW,
289 	.pr_domain =		&inetdomain,
290 	.pr_protocol =		IPPROTO_ETHERIP,
291 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
292 	.pr_input =		encap4_input,
293 	.pr_ctloutput =		rip_ctloutput,
294 	.pr_init =		encap_init,
295 	.pr_usrreqs =		&rip_usrreqs
296 },
297 {
298 	.pr_type =		SOCK_RAW,
299 	.pr_domain =		&inetdomain,
300 	.pr_protocol =		IPPROTO_GRE,
301 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
302 	.pr_input =		encap4_input,
303 	.pr_ctloutput =		rip_ctloutput,
304 	.pr_init =		encap_init,
305 	.pr_usrreqs =		&rip_usrreqs
306 },
307 # ifdef INET6
308 {
309 	.pr_type =		SOCK_RAW,
310 	.pr_domain =		&inetdomain,
311 	.pr_protocol =		IPPROTO_IPV6,
312 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
313 	.pr_input =		encap4_input,
314 	.pr_ctloutput =		rip_ctloutput,
315 	.pr_init =		encap_init,
316 	.pr_usrreqs =		&rip_usrreqs
317 },
318 #endif
319 {
320 	.pr_type =		SOCK_RAW,
321 	.pr_domain =		&inetdomain,
322 	.pr_protocol =		IPPROTO_PIM,
323 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
324 	.pr_input =		encap4_input,
325 	.pr_ctloutput =		rip_ctloutput,
326 	.pr_usrreqs =		&rip_usrreqs
327 },
328 /* Spacer n-times for loadable protocols. */
329 IPPROTOSPACER,
330 IPPROTOSPACER,
331 IPPROTOSPACER,
332 IPPROTOSPACER,
333 IPPROTOSPACER,
334 IPPROTOSPACER,
335 IPPROTOSPACER,
336 IPPROTOSPACER,
337 /* raw wildcard */
338 {
339 	.pr_type =		SOCK_RAW,
340 	.pr_domain =		&inetdomain,
341 	.pr_flags =		PR_ATOMIC|PR_ADDR,
342 	.pr_input =		rip_input,
343 	.pr_ctloutput =		rip_ctloutput,
344 	.pr_init =		rip_init,
345 #ifdef VIMAGE
346 	.pr_destroy =		rip_destroy,
347 #endif
348 	.pr_usrreqs =		&rip_usrreqs
349 },
350 };
351 
352 extern int in_inithead(void **, int);
353 extern int in_detachhead(void **, int);
354 
355 struct domain inetdomain = {
356 	.dom_family =		AF_INET,
357 	.dom_name =		"internet",
358 	.dom_protosw =		inetsw,
359 	.dom_protoswNPROTOSW =	&inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
360 #ifdef RADIX_MPATH
361 	.dom_rtattach =		rn4_mpath_inithead,
362 #else
363 	.dom_rtattach =		in_inithead,
364 #endif
365 #ifdef VIMAGE
366 	.dom_rtdetach =		in_detachhead,
367 #endif
368 	.dom_ifattach =		in_domifattach,
369 	.dom_ifdetach =		in_domifdetach
370 };
371 
372 VNET_DOMAIN_SET(inet);
373 #endif /* INET */
374 
375 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
376 	"Internet Family");
377 
378 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
379 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
380 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
381 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
382 #ifdef SCTP
383 SYSCTL_NODE(_net_inet, IPPROTO_SCTP,	sctp,	CTLFLAG_RW, 0,	"SCTP");
384 #endif
385 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
386 #ifdef IPSEC
387 /* XXX no protocol # to use, pick something "reserved" */
388 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
389 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
390 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
391 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
392 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
393 #endif /* IPSEC */
394 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
395 SYSCTL_NODE(_net_inet, OID_AUTO,	accf,	CTLFLAG_RW, 0,
396     "Accept filters");
397