xref: /freebsd/sys/netinet/in_proto.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
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 { 0,		&inetdomain,	PROTO_SPACER,	0,	\
105   NULL,		NULL,		NULL,		NULL,	\
106   NULL,							\
107   NULL,		NULL,		NULL,		NULL,	\
108   &nousrreqs						\
109 }
110 
111 struct protosw inetsw[] = {
112 { 0,		&inetdomain,	0,		0,
113   0,		0,		0,		0,
114   0,
115   ip_init,	0,		ip_slowtimo,	ip_drain,
116   &nousrreqs
117 },
118 { SOCK_DGRAM,	&inetdomain,	IPPROTO_UDP,	PR_ATOMIC|PR_ADDR,
119   udp_input,	0,		udp_ctlinput,	ip_ctloutput,
120   0,
121   udp_init,	0,		0,		0,
122   &udp_usrreqs
123 },
124 { SOCK_STREAM,	&inetdomain,	IPPROTO_TCP,
125 	PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
126   tcp_input,	0,		tcp_ctlinput,	tcp_ctloutput,
127   0,
128   tcp_init,	0,		tcp_slowtimo,	tcp_drain,
129   &tcp_usrreqs
130 },
131 { SOCK_RAW,	&inetdomain,	IPPROTO_RAW,	PR_ATOMIC|PR_ADDR,
132   rip_input,	0,		rip_ctlinput,	rip_ctloutput,
133   0,
134   0,		0,		0,		0,
135   &rip_usrreqs
136 },
137 { SOCK_RAW,	&inetdomain,	IPPROTO_ICMP,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
138   icmp_input,	0,		0,		rip_ctloutput,
139   0,
140   0,		0,		0,		0,
141   &rip_usrreqs
142 },
143 { SOCK_RAW,	&inetdomain,	IPPROTO_IGMP,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
144   igmp_input,	0,		0,		rip_ctloutput,
145   0,
146   igmp_init,	igmp_fasttimo,	igmp_slowtimo,	0,
147   &rip_usrreqs
148 },
149 { SOCK_RAW,	&inetdomain,	IPPROTO_RSVP,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
150   rsvp_input,	0,		0,		rip_ctloutput,
151   0,
152   0,		0,		0,		0,
153   &rip_usrreqs
154 },
155 #ifdef IPSEC
156 { SOCK_RAW,	&inetdomain,	IPPROTO_AH,	PR_ATOMIC|PR_ADDR,
157   ah4_input,	0,		0,		0,
158   0,
159   0,		0,		0,		0,
160   &nousrreqs
161 },
162 #ifdef IPSEC_ESP
163 { SOCK_RAW,	&inetdomain,	IPPROTO_ESP,	PR_ATOMIC|PR_ADDR,
164   esp4_input,	0,		0,		0,
165   0,
166   0,		0,		0,		0,
167   &nousrreqs
168 },
169 #endif
170 { SOCK_RAW,	&inetdomain,	IPPROTO_IPCOMP,	PR_ATOMIC|PR_ADDR,
171   ipcomp4_input, 0,		0,		0,
172   0,
173   0,		0,		0,		0,
174   &nousrreqs
175 },
176 #endif /* IPSEC */
177 #ifdef FAST_IPSEC
178 { SOCK_RAW,	&inetdomain,	IPPROTO_AH,	PR_ATOMIC|PR_ADDR,
179   ah4_input,	0,		ah4_ctlinput,	0,
180   0,
181   0,		0,		0,		0,
182   &nousrreqs
183 },
184 { SOCK_RAW,	&inetdomain,	IPPROTO_ESP,	PR_ATOMIC|PR_ADDR,
185   esp4_input,	0,		esp4_ctlinput,	0,
186   0,
187   0,		0,		0,		0,
188   &nousrreqs
189 },
190 { SOCK_RAW,	&inetdomain,	IPPROTO_IPCOMP,	PR_ATOMIC|PR_ADDR,
191   ipcomp4_input,	0,		0,		0,
192   0,
193   0,		0,		0,		0,
194   &nousrreqs
195 },
196 #endif /* FAST_IPSEC */
197 { SOCK_RAW,	&inetdomain,	IPPROTO_IPV4,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
198   encap4_input,	0,		0,		rip_ctloutput,
199   0,
200   encap_init,		0,		0,		0,
201   &rip_usrreqs
202 },
203 { SOCK_RAW,	&inetdomain,	IPPROTO_MOBILE,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
204   encap4_input,	0,		0,		rip_ctloutput,
205   0,
206   encap_init,	0,		0,		0,
207   &rip_usrreqs
208 },
209 { SOCK_RAW,	&inetdomain,	IPPROTO_GRE,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
210   encap4_input,	0,		0,		rip_ctloutput,
211   0,
212   encap_init,	0,		0,		0,
213   &rip_usrreqs
214 },
215 # ifdef INET6
216 { SOCK_RAW,	&inetdomain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
217   encap4_input,	0,		0,		rip_ctloutput,
218   0,
219   encap_init,	0,		0,		0,
220   &rip_usrreqs
221 },
222 #endif
223 #ifdef IPXIP
224 { SOCK_RAW,	&inetdomain,	IPPROTO_IDP,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
225   ipxip_input,	0,		ipxip_ctlinput,	0,
226   0,
227   0,		0,		0,		0,
228   &rip_usrreqs
229 },
230 #endif
231 #ifdef PIM
232 { SOCK_RAW,	&inetdomain,	IPPROTO_PIM,	PR_ATOMIC|PR_ADDR|PR_LASTHDR,
233   pim_input,	0,		0,		rip_ctloutput,
234   0,
235   0,		0,		0,		0,
236   &rip_usrreqs
237 },
238 #endif	/* PIM */
239 #ifdef DEV_PFSYNC
240 { SOCK_RAW,	&inetdomain,	IPPROTO_PFSYNC,	PR_ATOMIC|PR_ADDR,
241   pfsync_input,	0,		0,		rip_ctloutput,
242   0,
243   0,		0,		0,		0,
244   &rip_usrreqs
245 },
246 #endif	/* DEV_PFSYNC */
247 #ifdef DEV_CARP
248 { SOCK_RAW,	&inetdomain,	IPPROTO_CARP,	PR_ATOMIC|PR_ADDR,
249   carp_input,	(pr_output_t*)rip_output,	0,	rip_ctloutput,
250   0,
251   0,		0,		0,		0,
252   &rip_usrreqs
253 },
254 #endif /* DEV_CARP */
255 /* Spacer n-times for loadable protocols. */
256 IPPROTOSPACER,
257 IPPROTOSPACER,
258 IPPROTOSPACER,
259 IPPROTOSPACER,
260 IPPROTOSPACER,
261 IPPROTOSPACER,
262 IPPROTOSPACER,
263 IPPROTOSPACER,
264 /* raw wildcard */
265 { SOCK_RAW,	&inetdomain,	0,		PR_ATOMIC|PR_ADDR,
266   rip_input,	0,		0,		rip_ctloutput,
267   0,
268   rip_init,	0,		0,		0,
269   &rip_usrreqs
270 },
271 };
272 
273 extern int in_inithead(void **, int);
274 
275 struct domain inetdomain =
276     { AF_INET, "internet", 0, 0, 0,
277       inetsw,
278       &inetsw[sizeof(inetsw)/sizeof(inetsw[0])], 0,
279       in_inithead, 32, sizeof(struct sockaddr_in)
280     };
281 
282 DOMAIN_SET(inet);
283 
284 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
285 	"Internet Family");
286 
287 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
288 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
289 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
290 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
291 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
292 #ifdef FAST_IPSEC
293 /* XXX no protocol # to use, pick something "reserved" */
294 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
295 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
296 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
297 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
298 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
299 #else
300 #ifdef IPSEC
301 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ipsec,	CTLFLAG_RW, 0,	"IPSEC");
302 #endif /* IPSEC */
303 #endif /* !FAST_IPSEC */
304 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
305 #ifdef PIM
306 SYSCTL_NODE(_net_inet, IPPROTO_PIM,	pim,	CTLFLAG_RW, 0,	"PIM");
307 #endif
308 #ifdef DEV_PFSYNC
309 SYSCTL_NODE(_net_inet, IPPROTO_PFSYNC,	pfsync,	CTLFLAG_RW, 0,	"PFSYNC");
310 #endif
311 #ifdef DEV_CARP
312 SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
313 #endif
314