xref: /freebsd/sys/netinet/in_proto.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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_ipx.h"
36 #include "opt_mrouting.h"
37 #include "opt_ipsec.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_pf.h"
41 #include "opt_sctp.h"
42 #include "opt_mpath.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/domain.h>
49 #include <sys/proc.h>
50 #include <sys/protosw.h>
51 #include <sys/queue.h>
52 #include <sys/sysctl.h>
53 
54 /*
55  * While this file provides the domain and protocol switch tables for IPv4, it
56  * also provides the sysctl node declarations for net.inet.* often shared with
57  * IPv6 for common features or by upper layer protocols.  In case of no IPv4
58  * support compile out everything but these sysctl nodes.
59  */
60 #ifdef INET
61 #include <net/if.h>
62 #include <net/route.h>
63 #ifdef RADIX_MPATH
64 #include <net/radix_mpath.h>
65 #endif
66 #include <net/vnet.h>
67 #endif /* INET */
68 
69 #if defined(INET) || defined(INET6)
70 #include <netinet/in.h>
71 #endif
72 
73 #ifdef INET
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_icmp.h>
79 #include <netinet/igmp_var.h>
80 #include <netinet/tcp.h>
81 #include <netinet/tcp_timer.h>
82 #include <netinet/tcp_var.h>
83 #include <netinet/udp.h>
84 #include <netinet/udp_var.h>
85 #include <netinet/ip_encap.h>
86 
87 /*
88  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
89  */
90 
91 static struct pr_usrreqs nousrreqs;
92 
93 #ifdef IPSEC
94 #include <netipsec/ipsec.h>
95 #endif /* IPSEC */
96 
97 #ifdef SCTP
98 #include <netinet/in_pcb.h>
99 #include <netinet/sctp_pcb.h>
100 #include <netinet/sctp.h>
101 #include <netinet/sctp_var.h>
102 #endif /* SCTP */
103 
104 #ifdef DEV_PFSYNC
105 #include <net/pfvar.h>
106 #include <net/if_pfsync.h>
107 #endif
108 
109 FEATURE(inet, "Internet Protocol version 4");
110 
111 extern	struct domain inetdomain;
112 
113 /* Spacer for loadable protocols. */
114 #define IPPROTOSPACER   			\
115 {						\
116 	.pr_domain =		&inetdomain,	\
117 	.pr_protocol =		PROTO_SPACER,	\
118 	.pr_usrreqs =		&nousrreqs	\
119 }
120 
121 struct protosw inetsw[] = {
122 {
123 	.pr_type =		0,
124 	.pr_domain =		&inetdomain,
125 	.pr_protocol =		IPPROTO_IP,
126 	.pr_init =		ip_init,
127 #ifdef VIMAGE
128 	.pr_destroy =		ip_destroy,
129 #endif
130 	.pr_slowtimo =		ip_slowtimo,
131 	.pr_drain =		ip_drain,
132 	.pr_usrreqs =		&nousrreqs
133 },
134 {
135 	.pr_type =		SOCK_DGRAM,
136 	.pr_domain =		&inetdomain,
137 	.pr_protocol =		IPPROTO_UDP,
138 	.pr_flags =		PR_ATOMIC|PR_ADDR,
139 	.pr_input =		udp_input,
140 	.pr_ctlinput =		udp_ctlinput,
141 	.pr_ctloutput =		udp_ctloutput,
142 	.pr_init =		udp_init,
143 #ifdef VIMAGE
144 	.pr_destroy =		udp_destroy,
145 #endif
146 	.pr_usrreqs =		&udp_usrreqs
147 },
148 {
149 	.pr_type =		SOCK_STREAM,
150 	.pr_domain =		&inetdomain,
151 	.pr_protocol =		IPPROTO_TCP,
152 	.pr_flags =		PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
153 	.pr_input =		tcp_input,
154 	.pr_ctlinput =		tcp_ctlinput,
155 	.pr_ctloutput =		tcp_ctloutput,
156 	.pr_init =		tcp_init,
157 #ifdef VIMAGE
158 	.pr_destroy =		tcp_destroy,
159 #endif
160 	.pr_slowtimo =		tcp_slowtimo,
161 	.pr_drain =		tcp_drain,
162 	.pr_usrreqs =		&tcp_usrreqs
163 },
164 #ifdef SCTP
165 {
166 	.pr_type =		SOCK_DGRAM,
167 	.pr_domain =		&inetdomain,
168 	.pr_protocol =		IPPROTO_SCTP,
169 	.pr_flags =		PR_WANTRCVD,
170 	.pr_input =		sctp_input,
171 	.pr_ctlinput =		sctp_ctlinput,
172 	.pr_ctloutput =		sctp_ctloutput,
173 	.pr_init =		sctp_init,
174 #ifdef VIMAGE
175 	.pr_destroy =		sctp_finish,
176 #endif
177 	.pr_drain =		sctp_drain,
178 	.pr_usrreqs =		&sctp_usrreqs
179 },
180 {
181 	.pr_type =		SOCK_SEQPACKET,
182 	.pr_domain =		&inetdomain,
183 	.pr_protocol =		IPPROTO_SCTP,
184 	.pr_flags =		PR_WANTRCVD,
185 	.pr_input =		sctp_input,
186 	.pr_ctlinput =		sctp_ctlinput,
187 	.pr_ctloutput =		sctp_ctloutput,
188 	.pr_drain =		sctp_drain,
189 	.pr_usrreqs =		&sctp_usrreqs
190 },
191 
192 {
193 	.pr_type =		SOCK_STREAM,
194 	.pr_domain =		&inetdomain,
195 	.pr_protocol =		IPPROTO_SCTP,
196 	.pr_flags =		PR_WANTRCVD,
197 	.pr_input =		sctp_input,
198 	.pr_ctlinput =		sctp_ctlinput,
199 	.pr_ctloutput =		sctp_ctloutput,
200 	.pr_drain =		sctp_drain,
201 	.pr_usrreqs =		&sctp_usrreqs
202 },
203 #endif /* SCTP */
204 {
205 	.pr_type =		SOCK_RAW,
206 	.pr_domain =		&inetdomain,
207 	.pr_protocol =		IPPROTO_RAW,
208 	.pr_flags =		PR_ATOMIC|PR_ADDR,
209 	.pr_input =		rip_input,
210 	.pr_ctlinput =		rip_ctlinput,
211 	.pr_ctloutput =		rip_ctloutput,
212 	.pr_usrreqs =		&rip_usrreqs
213 },
214 {
215 	.pr_type =		SOCK_RAW,
216 	.pr_domain =		&inetdomain,
217 	.pr_protocol =		IPPROTO_ICMP,
218 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
219 	.pr_input =		icmp_input,
220 	.pr_ctloutput =		rip_ctloutput,
221 	.pr_usrreqs =		&rip_usrreqs
222 },
223 {
224 	.pr_type =		SOCK_RAW,
225 	.pr_domain =		&inetdomain,
226 	.pr_protocol =		IPPROTO_IGMP,
227 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
228 	.pr_input =		igmp_input,
229 	.pr_ctloutput =		rip_ctloutput,
230 	.pr_fasttimo =		igmp_fasttimo,
231 	.pr_slowtimo =		igmp_slowtimo,
232 	.pr_usrreqs =		&rip_usrreqs
233 },
234 {
235 	.pr_type =		SOCK_RAW,
236 	.pr_domain =		&inetdomain,
237 	.pr_protocol =		IPPROTO_RSVP,
238 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
239 	.pr_input =		rsvp_input,
240 	.pr_ctloutput =		rip_ctloutput,
241 	.pr_usrreqs =		&rip_usrreqs
242 },
243 #ifdef IPSEC
244 {
245 	.pr_type =		SOCK_RAW,
246 	.pr_domain =		&inetdomain,
247 	.pr_protocol =		IPPROTO_AH,
248 	.pr_flags =		PR_ATOMIC|PR_ADDR,
249 	.pr_input =		ah4_input,
250 	.pr_ctlinput =		ah4_ctlinput,
251 	.pr_usrreqs =		&nousrreqs
252 },
253 {
254 	.pr_type =		SOCK_RAW,
255 	.pr_domain =		&inetdomain,
256 	.pr_protocol =		IPPROTO_ESP,
257 	.pr_flags =		PR_ATOMIC|PR_ADDR,
258 	.pr_input =		esp4_input,
259 	.pr_ctlinput =		esp4_ctlinput,
260 	.pr_usrreqs =		&nousrreqs
261 },
262 {
263 	.pr_type =		SOCK_RAW,
264 	.pr_domain =		&inetdomain,
265 	.pr_protocol =		IPPROTO_IPCOMP,
266 	.pr_flags =		PR_ATOMIC|PR_ADDR,
267 	.pr_input =		ipcomp4_input,
268 	.pr_usrreqs =		&nousrreqs
269 },
270 #endif /* IPSEC */
271 {
272 	.pr_type =		SOCK_RAW,
273 	.pr_domain =		&inetdomain,
274 	.pr_protocol =		IPPROTO_IPV4,
275 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
276 	.pr_input =		encap4_input,
277 	.pr_ctloutput =		rip_ctloutput,
278 	.pr_init =		encap_init,
279 	.pr_usrreqs =		&rip_usrreqs
280 },
281 {
282 	.pr_type =		SOCK_RAW,
283 	.pr_domain =		&inetdomain,
284 	.pr_protocol =		IPPROTO_MOBILE,
285 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
286 	.pr_input =		encap4_input,
287 	.pr_ctloutput =		rip_ctloutput,
288 	.pr_init =		encap_init,
289 	.pr_usrreqs =		&rip_usrreqs
290 },
291 {
292 	.pr_type =		SOCK_RAW,
293 	.pr_domain =		&inetdomain,
294 	.pr_protocol =		IPPROTO_ETHERIP,
295 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
296 	.pr_input =		encap4_input,
297 	.pr_ctloutput =		rip_ctloutput,
298 	.pr_init =		encap_init,
299 	.pr_usrreqs =		&rip_usrreqs
300 },
301 {
302 	.pr_type =		SOCK_RAW,
303 	.pr_domain =		&inetdomain,
304 	.pr_protocol =		IPPROTO_GRE,
305 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
306 	.pr_input =		encap4_input,
307 	.pr_ctloutput =		rip_ctloutput,
308 	.pr_init =		encap_init,
309 	.pr_usrreqs =		&rip_usrreqs
310 },
311 # ifdef INET6
312 {
313 	.pr_type =		SOCK_RAW,
314 	.pr_domain =		&inetdomain,
315 	.pr_protocol =		IPPROTO_IPV6,
316 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
317 	.pr_input =		encap4_input,
318 	.pr_ctloutput =		rip_ctloutput,
319 	.pr_init =		encap_init,
320 	.pr_usrreqs =		&rip_usrreqs
321 },
322 #endif
323 {
324 	.pr_type =		SOCK_RAW,
325 	.pr_domain =		&inetdomain,
326 	.pr_protocol =		IPPROTO_PIM,
327 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
328 	.pr_input =		encap4_input,
329 	.pr_ctloutput =		rip_ctloutput,
330 	.pr_usrreqs =		&rip_usrreqs
331 },
332 #ifdef DEV_PFSYNC
333 {
334 	.pr_type =		SOCK_RAW,
335 	.pr_domain =		&inetdomain,
336 	.pr_protocol =		IPPROTO_PFSYNC,
337 	.pr_flags =		PR_ATOMIC|PR_ADDR,
338 	.pr_input =		pfsync_input,
339 	.pr_ctloutput =		rip_ctloutput,
340 	.pr_usrreqs =		&rip_usrreqs
341 },
342 #endif	/* DEV_PFSYNC */
343 /* Spacer n-times for loadable protocols. */
344 IPPROTOSPACER,
345 IPPROTOSPACER,
346 IPPROTOSPACER,
347 IPPROTOSPACER,
348 IPPROTOSPACER,
349 IPPROTOSPACER,
350 IPPROTOSPACER,
351 IPPROTOSPACER,
352 /* raw wildcard */
353 {
354 	.pr_type =		SOCK_RAW,
355 	.pr_domain =		&inetdomain,
356 	.pr_flags =		PR_ATOMIC|PR_ADDR,
357 	.pr_input =		rip_input,
358 	.pr_ctloutput =		rip_ctloutput,
359 	.pr_init =		rip_init,
360 #ifdef VIMAGE
361 	.pr_destroy =		rip_destroy,
362 #endif
363 	.pr_usrreqs =		&rip_usrreqs
364 },
365 };
366 
367 extern int in_inithead(void **, int);
368 extern int in_detachhead(void **, int);
369 
370 struct domain inetdomain = {
371 	.dom_family =		AF_INET,
372 	.dom_name =		"internet",
373 	.dom_protosw =		inetsw,
374 	.dom_protoswNPROTOSW =	&inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
375 #ifdef RADIX_MPATH
376 	.dom_rtattach =		rn4_mpath_inithead,
377 #else
378 	.dom_rtattach =		in_inithead,
379 #endif
380 #ifdef VIMAGE
381 	.dom_rtdetach =		in_detachhead,
382 #endif
383 	.dom_rtoffset =		32,
384 	.dom_maxrtkey =		sizeof(struct sockaddr_in),
385 	.dom_ifattach =		in_domifattach,
386 	.dom_ifdetach =		in_domifdetach
387 };
388 
389 VNET_DOMAIN_SET(inet);
390 #endif /* INET */
391 
392 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
393 	"Internet Family");
394 
395 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
396 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
397 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
398 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
399 #ifdef SCTP
400 SYSCTL_NODE(_net_inet, IPPROTO_SCTP,	sctp,	CTLFLAG_RW, 0,	"SCTP");
401 #endif
402 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
403 #ifdef IPSEC
404 /* XXX no protocol # to use, pick something "reserved" */
405 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
406 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
407 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
408 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
409 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
410 #endif /* IPSEC */
411 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
412 #ifdef DEV_PFSYNC
413 SYSCTL_NODE(_net_inet, IPPROTO_PFSYNC,	pfsync,	CTLFLAG_RW, 0,	"PFSYNC");
414 #endif
415