xref: /freebsd/sys/netinet/in_proto.c (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 #include <net/vnet.h>
66 #endif /* INET */
67 
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #endif
71 
72 #ifdef INET
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/ip_icmp.h>
78 #include <netinet/igmp_var.h>
79 #include <netinet/tcp.h>
80 #include <netinet/tcp_timer.h>
81 #include <netinet/tcp_var.h>
82 #include <netinet/udp.h>
83 #include <netinet/udp_var.h>
84 #include <netinet/ip_encap.h>
85 
86 /*
87  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
88  */
89 
90 static struct pr_usrreqs nousrreqs;
91 
92 #ifdef SCTP
93 #include <netinet/in_pcb.h>
94 #include <netinet/sctp_pcb.h>
95 #include <netinet/sctp.h>
96 #include <netinet/sctp_var.h>
97 #endif
98 
99 FEATURE(inet, "Internet Protocol version 4");
100 
101 extern	struct domain inetdomain;
102 
103 /* Spacer for loadable protocols. */
104 #define IPPROTOSPACER   			\
105 {						\
106 	.pr_domain =		&inetdomain,	\
107 	.pr_protocol =		PROTO_SPACER,	\
108 	.pr_usrreqs =		&nousrreqs	\
109 }
110 
111 struct protosw inetsw[] = {
112 {
113 	.pr_type =		0,
114 	.pr_domain =		&inetdomain,
115 	.pr_protocol =		IPPROTO_IP,
116 	.pr_init =		ip_init,
117 	.pr_slowtimo =		ip_slowtimo,
118 	.pr_drain =		ip_drain,
119 	.pr_usrreqs =		&nousrreqs
120 },
121 {
122 	.pr_type =		SOCK_DGRAM,
123 	.pr_domain =		&inetdomain,
124 	.pr_protocol =		IPPROTO_UDP,
125 	.pr_flags =		PR_ATOMIC|PR_ADDR,
126 	.pr_input =		udp_input,
127 	.pr_ctlinput =		udp_ctlinput,
128 	.pr_ctloutput =		udp_ctloutput,
129 	.pr_init =		udp_init,
130 	.pr_usrreqs =		&udp_usrreqs
131 },
132 {
133 	.pr_type =		SOCK_STREAM,
134 	.pr_domain =		&inetdomain,
135 	.pr_protocol =		IPPROTO_TCP,
136 	.pr_flags =		PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
137 	.pr_input =		tcp_input,
138 	.pr_ctlinput =		tcp_ctlinput,
139 	.pr_ctloutput =		tcp_ctloutput,
140 	.pr_init =		tcp_init,
141 	.pr_slowtimo =		tcp_slowtimo,
142 	.pr_drain =		tcp_drain,
143 	.pr_usrreqs =		&tcp_usrreqs
144 },
145 #ifdef SCTP
146 {
147 	.pr_type =		SOCK_SEQPACKET,
148 	.pr_domain =		&inetdomain,
149 	.pr_protocol =		IPPROTO_SCTP,
150 	.pr_flags =		PR_WANTRCVD|PR_LASTHDR,
151 	.pr_input =		sctp_input,
152 	.pr_ctlinput =		sctp_ctlinput,
153 	.pr_ctloutput =		sctp_ctloutput,
154 	.pr_init =		sctp_init,
155 	.pr_drain =		sctp_drain,
156 	.pr_usrreqs =		&sctp_usrreqs
157 },
158 {
159 	.pr_type =		SOCK_STREAM,
160 	.pr_domain =		&inetdomain,
161 	.pr_protocol =		IPPROTO_SCTP,
162 	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_LASTHDR,
163 	.pr_input =		sctp_input,
164 	.pr_ctlinput =		sctp_ctlinput,
165 	.pr_ctloutput =		sctp_ctloutput,
166 	.pr_drain =		NULL, /* Covered by the SOCK_SEQPACKET entry. */
167 	.pr_usrreqs =		&sctp_usrreqs
168 },
169 #endif /* SCTP */
170 {
171 	.pr_type =		SOCK_DGRAM,
172 	.pr_domain =		&inetdomain,
173 	.pr_protocol =		IPPROTO_UDPLITE,
174 	.pr_flags =		PR_ATOMIC|PR_ADDR,
175 	.pr_input =		udp_input,
176 	.pr_ctlinput =		udplite_ctlinput,
177 	.pr_ctloutput =		udp_ctloutput,
178 	.pr_init =		udplite_init,
179 	.pr_usrreqs =		&udp_usrreqs
180 },
181 {
182 	.pr_type =		SOCK_RAW,
183 	.pr_domain =		&inetdomain,
184 	.pr_protocol =		IPPROTO_RAW,
185 	.pr_flags =		PR_ATOMIC|PR_ADDR,
186 	.pr_input =		rip_input,
187 	.pr_ctlinput =		rip_ctlinput,
188 	.pr_ctloutput =		rip_ctloutput,
189 	.pr_usrreqs =		&rip_usrreqs
190 },
191 {
192 	.pr_type =		SOCK_RAW,
193 	.pr_domain =		&inetdomain,
194 	.pr_protocol =		IPPROTO_ICMP,
195 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
196 	.pr_input =		icmp_input,
197 	.pr_ctloutput =		rip_ctloutput,
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_fasttimo =		igmp_fasttimo,
208 	.pr_slowtimo =		igmp_slowtimo,
209 	.pr_usrreqs =		&rip_usrreqs
210 },
211 {
212 	.pr_type =		SOCK_RAW,
213 	.pr_domain =		&inetdomain,
214 	.pr_protocol =		IPPROTO_RSVP,
215 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
216 	.pr_input =		rsvp_input,
217 	.pr_ctloutput =		rip_ctloutput,
218 	.pr_usrreqs =		&rip_usrreqs
219 },
220 {
221 	.pr_type =		SOCK_RAW,
222 	.pr_domain =		&inetdomain,
223 	.pr_protocol =		IPPROTO_IPV4,
224 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
225 	.pr_input =		encap4_input,
226 	.pr_ctloutput =		rip_ctloutput,
227 	.pr_usrreqs =		&rip_usrreqs
228 },
229 {
230 	.pr_type =		SOCK_RAW,
231 	.pr_domain =		&inetdomain,
232 	.pr_protocol =		IPPROTO_MOBILE,
233 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
234 	.pr_input =		encap4_input,
235 	.pr_ctloutput =		rip_ctloutput,
236 	.pr_usrreqs =		&rip_usrreqs
237 },
238 {
239 	.pr_type =		SOCK_RAW,
240 	.pr_domain =		&inetdomain,
241 	.pr_protocol =		IPPROTO_ETHERIP,
242 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
243 	.pr_input =		encap4_input,
244 	.pr_ctloutput =		rip_ctloutput,
245 	.pr_usrreqs =		&rip_usrreqs
246 },
247 {
248 	.pr_type =		SOCK_RAW,
249 	.pr_domain =		&inetdomain,
250 	.pr_protocol =		IPPROTO_GRE,
251 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
252 	.pr_input =		encap4_input,
253 	.pr_ctloutput =		rip_ctloutput,
254 	.pr_usrreqs =		&rip_usrreqs
255 },
256 # ifdef INET6
257 {
258 	.pr_type =		SOCK_RAW,
259 	.pr_domain =		&inetdomain,
260 	.pr_protocol =		IPPROTO_IPV6,
261 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
262 	.pr_input =		encap4_input,
263 	.pr_ctloutput =		rip_ctloutput,
264 	.pr_usrreqs =		&rip_usrreqs
265 },
266 #endif
267 {
268 	.pr_type =		SOCK_RAW,
269 	.pr_domain =		&inetdomain,
270 	.pr_protocol =		IPPROTO_PIM,
271 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
272 	.pr_input =		encap4_input,
273 	.pr_ctloutput =		rip_ctloutput,
274 	.pr_usrreqs =		&rip_usrreqs
275 },
276 /* Spacer n-times for loadable protocols. */
277 IPPROTOSPACER,
278 IPPROTOSPACER,
279 IPPROTOSPACER,
280 IPPROTOSPACER,
281 IPPROTOSPACER,
282 IPPROTOSPACER,
283 IPPROTOSPACER,
284 IPPROTOSPACER,
285 /* raw wildcard */
286 {
287 	.pr_type =		SOCK_RAW,
288 	.pr_domain =		&inetdomain,
289 	.pr_flags =		PR_ATOMIC|PR_ADDR,
290 	.pr_input =		rip_input,
291 	.pr_ctloutput =		rip_ctloutput,
292 	.pr_init =		rip_init,
293 	.pr_usrreqs =		&rip_usrreqs
294 },
295 };
296 
297 extern int in_inithead(void **, int, u_int);
298 extern int in_detachhead(void **, int);
299 
300 struct domain inetdomain = {
301 	.dom_family =		AF_INET,
302 	.dom_name =		"internet",
303 	.dom_protosw =		inetsw,
304 	.dom_protoswNPROTOSW =	&inetsw[nitems(inetsw)],
305 	.dom_rtattach =		in_inithead,
306 #ifdef VIMAGE
307 	.dom_rtdetach =		in_detachhead,
308 #endif
309 	.dom_ifattach =		in_domifattach,
310 	.dom_ifdetach =		in_domifdetach
311 };
312 
313 VNET_DOMAIN_SET(inet);
314 #endif /* INET */
315 
316 SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
317     "Internet Family");
318 
319 SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
320     "IP");
321 SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
322     "ICMP");
323 SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
324     "UDP");
325 SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
326     "TCP");
327 #if defined(SCTP) || defined(SCTP_SUPPORT)
328 SYSCTL_NODE(_net_inet, IPPROTO_SCTP, sctp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
329     "SCTP");
330 #endif
331 SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
332     "IGMP");
333 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
334 /* XXX no protocol # to use, pick something "reserved" */
335 SYSCTL_NODE(_net_inet, 253, ipsec, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
336     "IPSEC");
337 SYSCTL_NODE(_net_inet, IPPROTO_AH, ah, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
338     "AH");
339 SYSCTL_NODE(_net_inet, IPPROTO_ESP, esp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
340     "ESP");
341 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP, ipcomp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
342     "IPCOMP");
343 SYSCTL_NODE(_net_inet, IPPROTO_IPIP, ipip, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
344     "IPIP");
345 #endif /* IPSEC */
346 SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
347     "RAW");
348 SYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
349     "Accept filters");
350