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