xref: /freebsd/sys/netinet/in_proto.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
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_usrreqs =		&rip_usrreqs
231 },
232 {
233 	.pr_type =		SOCK_RAW,
234 	.pr_domain =		&inetdomain,
235 	.pr_protocol =		IPPROTO_MOBILE,
236 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
237 	.pr_input =		encap4_input,
238 	.pr_ctloutput =		rip_ctloutput,
239 	.pr_usrreqs =		&rip_usrreqs
240 },
241 {
242 	.pr_type =		SOCK_RAW,
243 	.pr_domain =		&inetdomain,
244 	.pr_protocol =		IPPROTO_ETHERIP,
245 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
246 	.pr_input =		encap4_input,
247 	.pr_ctloutput =		rip_ctloutput,
248 	.pr_usrreqs =		&rip_usrreqs
249 },
250 {
251 	.pr_type =		SOCK_RAW,
252 	.pr_domain =		&inetdomain,
253 	.pr_protocol =		IPPROTO_GRE,
254 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
255 	.pr_input =		encap4_input,
256 	.pr_ctloutput =		rip_ctloutput,
257 	.pr_usrreqs =		&rip_usrreqs
258 },
259 # ifdef INET6
260 {
261 	.pr_type =		SOCK_RAW,
262 	.pr_domain =		&inetdomain,
263 	.pr_protocol =		IPPROTO_IPV6,
264 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
265 	.pr_input =		encap4_input,
266 	.pr_ctloutput =		rip_ctloutput,
267 	.pr_usrreqs =		&rip_usrreqs
268 },
269 #endif
270 {
271 	.pr_type =		SOCK_RAW,
272 	.pr_domain =		&inetdomain,
273 	.pr_protocol =		IPPROTO_PIM,
274 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
275 	.pr_input =		encap4_input,
276 	.pr_ctloutput =		rip_ctloutput,
277 	.pr_usrreqs =		&rip_usrreqs
278 },
279 /* Spacer n-times for loadable protocols. */
280 IPPROTOSPACER,
281 IPPROTOSPACER,
282 IPPROTOSPACER,
283 IPPROTOSPACER,
284 IPPROTOSPACER,
285 IPPROTOSPACER,
286 IPPROTOSPACER,
287 IPPROTOSPACER,
288 /* raw wildcard */
289 {
290 	.pr_type =		SOCK_RAW,
291 	.pr_domain =		&inetdomain,
292 	.pr_flags =		PR_ATOMIC|PR_ADDR,
293 	.pr_input =		rip_input,
294 	.pr_ctloutput =		rip_ctloutput,
295 	.pr_init =		rip_init,
296 	.pr_usrreqs =		&rip_usrreqs
297 },
298 };
299 
300 extern int in_inithead(void **, int);
301 extern int in_detachhead(void **, int);
302 
303 struct domain inetdomain = {
304 	.dom_family =		AF_INET,
305 	.dom_name =		"internet",
306 	.dom_protosw =		inetsw,
307 	.dom_protoswNPROTOSW =	&inetsw[nitems(inetsw)],
308 #ifdef RADIX_MPATH
309 	.dom_rtattach =		rn4_mpath_inithead,
310 #else
311 	.dom_rtattach =		in_inithead,
312 #endif
313 #ifdef VIMAGE
314 	.dom_rtdetach =		in_detachhead,
315 #endif
316 	.dom_ifattach =		in_domifattach,
317 	.dom_ifdetach =		in_domifdetach
318 };
319 
320 VNET_DOMAIN_SET(inet);
321 #endif /* INET */
322 
323 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
324 	"Internet Family");
325 
326 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
327 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
328 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
329 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
330 #ifdef SCTP
331 SYSCTL_NODE(_net_inet, IPPROTO_SCTP,	sctp,	CTLFLAG_RW, 0,	"SCTP");
332 #endif
333 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
334 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
335 /* XXX no protocol # to use, pick something "reserved" */
336 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
337 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
338 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
339 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
340 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
341 #endif /* IPSEC */
342 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
343 SYSCTL_NODE(_net_inet, OID_AUTO,	accf,	CTLFLAG_RW, 0,
344     "Accept filters");
345