xref: /freebsd/sys/netinet/in_proto.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)in_proto.c	8.2 (Berkeley) 2/9/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_mrouting.h"
38 #include "opt_ipsec.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.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/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/domain.h>
50 #include <sys/proc.h>
51 #include <sys/protosw.h>
52 #include <sys/queue.h>
53 #include <sys/sysctl.h>
54 
55 /*
56  * While this file provides the domain and protocol switch tables for IPv4, it
57  * also provides the sysctl node declarations for net.inet.* often shared with
58  * IPv6 for common features or by upper layer protocols.  In case of no IPv4
59  * support compile out everything but these sysctl nodes.
60  */
61 #ifdef INET
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <net/route.h>
65 #ifdef RADIX_MPATH
66 #include <net/radix_mpath.h>
67 #endif
68 #include <net/vnet.h>
69 #endif /* INET */
70 
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #endif
74 
75 #ifdef INET
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip_var.h>
80 #include <netinet/ip_icmp.h>
81 #include <netinet/igmp_var.h>
82 #include <netinet/tcp.h>
83 #include <netinet/tcp_timer.h>
84 #include <netinet/tcp_var.h>
85 #include <netinet/udp.h>
86 #include <netinet/udp_var.h>
87 #include <netinet/ip_encap.h>
88 
89 /*
90  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
91  */
92 
93 static struct pr_usrreqs nousrreqs;
94 
95 #ifdef SCTP
96 #include <netinet/in_pcb.h>
97 #include <netinet/sctp_pcb.h>
98 #include <netinet/sctp.h>
99 #include <netinet/sctp_var.h>
100 #endif /* SCTP */
101 
102 FEATURE(inet, "Internet Protocol version 4");
103 
104 extern	struct domain inetdomain;
105 
106 /* Spacer for loadable protocols. */
107 #define IPPROTOSPACER   			\
108 {						\
109 	.pr_domain =		&inetdomain,	\
110 	.pr_protocol =		PROTO_SPACER,	\
111 	.pr_usrreqs =		&nousrreqs	\
112 }
113 
114 struct protosw inetsw[] = {
115 {
116 	.pr_type =		0,
117 	.pr_domain =		&inetdomain,
118 	.pr_protocol =		IPPROTO_IP,
119 	.pr_init =		ip_init,
120 	.pr_slowtimo =		ip_slowtimo,
121 	.pr_drain =		ip_drain,
122 	.pr_usrreqs =		&nousrreqs
123 },
124 {
125 	.pr_type =		SOCK_DGRAM,
126 	.pr_domain =		&inetdomain,
127 	.pr_protocol =		IPPROTO_UDP,
128 	.pr_flags =		PR_ATOMIC|PR_ADDR,
129 	.pr_input =		udp_input,
130 	.pr_ctlinput =		udp_ctlinput,
131 	.pr_ctloutput =		udp_ctloutput,
132 	.pr_init =		udp_init,
133 	.pr_usrreqs =		&udp_usrreqs
134 },
135 {
136 	.pr_type =		SOCK_STREAM,
137 	.pr_domain =		&inetdomain,
138 	.pr_protocol =		IPPROTO_TCP,
139 	.pr_flags =		PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
140 	.pr_input =		tcp_input,
141 	.pr_ctlinput =		tcp_ctlinput,
142 	.pr_ctloutput =		tcp_ctloutput,
143 	.pr_init =		tcp_init,
144 	.pr_slowtimo =		tcp_slowtimo,
145 	.pr_drain =		tcp_drain,
146 	.pr_usrreqs =		&tcp_usrreqs
147 },
148 #ifdef SCTP
149 {
150 	.pr_type =		SOCK_SEQPACKET,
151 	.pr_domain =		&inetdomain,
152 	.pr_protocol =		IPPROTO_SCTP,
153 	.pr_flags =		PR_WANTRCVD|PR_LASTHDR,
154 	.pr_input =		sctp_input,
155 	.pr_ctlinput =		sctp_ctlinput,
156 	.pr_ctloutput =		sctp_ctloutput,
157 	.pr_init =		sctp_init,
158 	.pr_drain =		sctp_drain,
159 	.pr_usrreqs =		&sctp_usrreqs
160 },
161 {
162 	.pr_type =		SOCK_STREAM,
163 	.pr_domain =		&inetdomain,
164 	.pr_protocol =		IPPROTO_SCTP,
165 	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_LASTHDR,
166 	.pr_input =		sctp_input,
167 	.pr_ctlinput =		sctp_ctlinput,
168 	.pr_ctloutput =		sctp_ctloutput,
169 	.pr_drain =		sctp_drain,
170 	.pr_usrreqs =		&sctp_usrreqs
171 },
172 #endif /* SCTP */
173 {
174 	.pr_type =		SOCK_DGRAM,
175 	.pr_domain =		&inetdomain,
176 	.pr_protocol =		IPPROTO_UDPLITE,
177 	.pr_flags =		PR_ATOMIC|PR_ADDR,
178 	.pr_input =		udp_input,
179 	.pr_ctlinput =		udplite_ctlinput,
180 	.pr_ctloutput =		udp_ctloutput,
181 	.pr_init =		udplite_init,
182 	.pr_usrreqs =		&udp_usrreqs
183 },
184 {
185 	.pr_type =		SOCK_RAW,
186 	.pr_domain =		&inetdomain,
187 	.pr_protocol =		IPPROTO_RAW,
188 	.pr_flags =		PR_ATOMIC|PR_ADDR,
189 	.pr_input =		rip_input,
190 	.pr_ctlinput =		rip_ctlinput,
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_ICMP,
198 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
199 	.pr_input =		icmp_input,
200 	.pr_ctloutput =		rip_ctloutput,
201 	.pr_usrreqs =		&rip_usrreqs
202 },
203 {
204 	.pr_type =		SOCK_RAW,
205 	.pr_domain =		&inetdomain,
206 	.pr_protocol =		IPPROTO_IGMP,
207 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
208 	.pr_input =		igmp_input,
209 	.pr_ctloutput =		rip_ctloutput,
210 	.pr_fasttimo =		igmp_fasttimo,
211 	.pr_slowtimo =		igmp_slowtimo,
212 	.pr_usrreqs =		&rip_usrreqs
213 },
214 {
215 	.pr_type =		SOCK_RAW,
216 	.pr_domain =		&inetdomain,
217 	.pr_protocol =		IPPROTO_RSVP,
218 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
219 	.pr_input =		rsvp_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_IPV4,
227 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
228 	.pr_input =		encap4_input,
229 	.pr_ctloutput =		rip_ctloutput,
230 	.pr_init =		encap_init,
231 	.pr_usrreqs =		&rip_usrreqs
232 },
233 {
234 	.pr_type =		SOCK_RAW,
235 	.pr_domain =		&inetdomain,
236 	.pr_protocol =		IPPROTO_MOBILE,
237 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
238 	.pr_input =		encap4_input,
239 	.pr_ctloutput =		rip_ctloutput,
240 	.pr_init =		encap_init,
241 	.pr_usrreqs =		&rip_usrreqs
242 },
243 {
244 	.pr_type =		SOCK_RAW,
245 	.pr_domain =		&inetdomain,
246 	.pr_protocol =		IPPROTO_ETHERIP,
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_GRE,
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 # ifdef INET6
264 {
265 	.pr_type =		SOCK_RAW,
266 	.pr_domain =		&inetdomain,
267 	.pr_protocol =		IPPROTO_IPV6,
268 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
269 	.pr_input =		encap4_input,
270 	.pr_ctloutput =		rip_ctloutput,
271 	.pr_init =		encap_init,
272 	.pr_usrreqs =		&rip_usrreqs
273 },
274 #endif
275 {
276 	.pr_type =		SOCK_RAW,
277 	.pr_domain =		&inetdomain,
278 	.pr_protocol =		IPPROTO_PIM,
279 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
280 	.pr_input =		encap4_input,
281 	.pr_ctloutput =		rip_ctloutput,
282 	.pr_usrreqs =		&rip_usrreqs
283 },
284 /* Spacer n-times for loadable protocols. */
285 IPPROTOSPACER,
286 IPPROTOSPACER,
287 IPPROTOSPACER,
288 IPPROTOSPACER,
289 IPPROTOSPACER,
290 IPPROTOSPACER,
291 IPPROTOSPACER,
292 IPPROTOSPACER,
293 /* raw wildcard */
294 {
295 	.pr_type =		SOCK_RAW,
296 	.pr_domain =		&inetdomain,
297 	.pr_flags =		PR_ATOMIC|PR_ADDR,
298 	.pr_input =		rip_input,
299 	.pr_ctloutput =		rip_ctloutput,
300 	.pr_init =		rip_init,
301 	.pr_usrreqs =		&rip_usrreqs
302 },
303 };
304 
305 extern int in_inithead(void **, int);
306 extern int in_detachhead(void **, int);
307 
308 struct domain inetdomain = {
309 	.dom_family =		AF_INET,
310 	.dom_name =		"internet",
311 	.dom_protosw =		inetsw,
312 	.dom_protoswNPROTOSW =	&inetsw[nitems(inetsw)],
313 #ifdef RADIX_MPATH
314 	.dom_rtattach =		rn4_mpath_inithead,
315 #else
316 	.dom_rtattach =		in_inithead,
317 #endif
318 #ifdef VIMAGE
319 	.dom_rtdetach =		in_detachhead,
320 #endif
321 	.dom_ifattach =		in_domifattach,
322 	.dom_ifdetach =		in_domifdetach
323 };
324 
325 VNET_DOMAIN_SET(inet);
326 #endif /* INET */
327 
328 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
329 	"Internet Family");
330 
331 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
332 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
333 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
334 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
335 #ifdef SCTP
336 SYSCTL_NODE(_net_inet, IPPROTO_SCTP,	sctp,	CTLFLAG_RW, 0,	"SCTP");
337 #endif
338 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
339 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
340 /* XXX no protocol # to use, pick something "reserved" */
341 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
342 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
343 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
344 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
345 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
346 #endif /* IPSEC */
347 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
348 SYSCTL_NODE(_net_inet, OID_AUTO,	accf,	CTLFLAG_RW, 0,
349     "Accept filters");
350