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