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