xref: /dragonfly/sys/netinet/in_proto.c (revision 6700dd34)
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  * 3. 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: src/sys/netinet/in_proto.c,v 1.53.2.7 2003/08/24 08:24:38 hsu Exp $
31  */
32 
33 #include "opt_ipdivert.h"
34 #include "opt_mrouting.h"
35 #include "opt_ipsec.h"
36 #include "opt_inet6.h"
37 #include "opt_carp.h"
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/queue.h>
45 #include <sys/sysctl.h>
46 
47 #include <net/if.h>
48 #include <net/route.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/ip_icmp.h>
56 #include <netinet/igmp_var.h>
57 #ifdef PIM
58 #include <netinet/pim_var.h>
59 #endif
60 #include <netinet/tcp.h>
61 #include <netinet/tcp_timer.h>
62 #include <netinet/tcp_var.h>
63 #include <netinet/udp.h>
64 #include <netinet/udp_var.h>
65 #include <netinet/ip_encap.h>
66 #ifdef IPDIVERT
67 #include <netinet/ip_divert.h>
68 #endif
69 
70 /*
71  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
72  */
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 <netproto/ipsec/ipsec.h>
85 #endif /* FAST_IPSEC */
86 
87 #include <net/netisr.h>		/* for cpu0_soport */
88 
89 #ifdef CARP
90 #include <netinet/ip_carp.h>
91 #endif
92 
93 extern	struct domain inetdomain;
94 static	struct pr_usrreqs nousrreqs;
95 
96 struct protosw inetsw[] = {
97     {
98 	.pr_type = 0,
99 	.pr_domain = &inetdomain,
100 	.pr_protocol = 0,
101 	.pr_flags = 0,
102 
103 	.pr_ctlport = NULL,
104 
105 	.pr_init = ip_init,
106 	.pr_drain = ip_drain,
107 	.pr_usrreqs = &nousrreqs
108     },
109     {
110 	.pr_type = SOCK_DGRAM,
111 	.pr_domain = &inetdomain,
112 	.pr_protocol = IPPROTO_UDP,
113 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE|
114 	    PR_ASYNC_SEND|PR_ASEND_HOLDTD|PR_ACONN_HOLDTD,
115 
116 	.pr_initport = udp_initport,
117 	.pr_input = udp_input,
118 	.pr_output = NULL,
119 	.pr_ctlinput = udp_ctlinput,
120 	.pr_ctloutput = udp_ctloutput,
121 
122 	.pr_ctlport = udp_ctlport,
123 	.pr_init = udp_init,
124 	.pr_usrreqs = &udp_usrreqs
125     },
126     {
127 	.pr_type = SOCK_STREAM,
128 	.pr_domain = &inetdomain,
129 	.pr_protocol = IPPROTO_TCP,
130 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_MPSAFE|
131 	    PR_ASYNC_SEND|PR_ASYNC_RCVD|PR_ACONN_HOLDTD,
132 
133 	.pr_initport = tcp_initport,
134 	.pr_input = tcp_input,
135 	.pr_output = NULL,
136 	.pr_ctlinput = tcp_ctlinput,
137 	.pr_ctloutmsg = tcp_ctloutmsg,
138 	.pr_ctloutput = tcp_ctloutput,
139 
140 	.pr_ctlport = tcp_ctlport,
141 	.pr_init = tcp_init,
142 	.pr_drain = tcp_drain,
143 	.pr_usrreqs = &tcp_usrreqs
144     },
145     {
146 	.pr_type = SOCK_RAW,
147 	.pr_domain = &inetdomain,
148 	.pr_protocol = IPPROTO_RAW,
149 	.pr_flags = PR_ATOMIC|PR_ADDR,
150 
151 	.pr_input = rip_input,
152 	.pr_output = NULL,
153 	.pr_ctlinput = rip_ctlinput,
154 	.pr_ctloutput = rip_ctloutput,
155 
156 	.pr_ctlport = cpu0_ctlport,
157 	.pr_usrreqs = &rip_usrreqs
158     },
159     {
160 	.pr_type = SOCK_RAW,
161 	.pr_domain = &inetdomain,
162 	.pr_protocol = IPPROTO_ICMP,
163 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE,
164 
165 	.pr_input = icmp_input,
166 	.pr_output = NULL,
167 	.pr_ctlinput = NULL,
168 	.pr_ctloutput = rip_ctloutput,
169 
170 	.pr_usrreqs = &rip_usrreqs
171     },
172     {
173 	.pr_type = SOCK_RAW,
174 	.pr_domain = &inetdomain,
175 	.pr_protocol = IPPROTO_IGMP,
176 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE,
177 
178 	.pr_input = igmp_input,
179 	.pr_output = NULL,
180 	.pr_ctlinput = NULL,
181 	.pr_ctloutput = rip_ctloutput,
182 
183 	.pr_init = igmp_init,
184 	.pr_drain = NULL,
185 	.pr_usrreqs = &rip_usrreqs
186     },
187     {
188 	.pr_type = SOCK_RAW,
189 	.pr_domain = &inetdomain,
190 	.pr_protocol = IPPROTO_RSVP,
191 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
192 
193 	.pr_input = rsvp_input,
194 	.pr_output = NULL,
195 	.pr_ctlinput = NULL,
196 	.pr_ctloutput = rip_ctloutput,
197 
198 	.pr_usrreqs = &rip_usrreqs
199     },
200 #ifdef IPSEC
201     {
202 	.pr_type = SOCK_RAW,
203 	.pr_domain = &inetdomain,
204 	.pr_protocol = IPPROTO_AH,
205 	.pr_flags = PR_ATOMIC|PR_ADDR,
206 
207 	.pr_input = ah4_input,
208 	.pr_output = NULL,
209 	.pr_ctlinput = NULL,
210 	.pr_ctloutput = NULL,
211 
212 	.pr_ctlport = NULL,
213 	.pr_usrreqs = &nousrreqs
214     },
215 #ifdef IPSEC_ESP
216     {
217 	.pr_type = SOCK_RAW,
218 	.pr_domain = &inetdomain,
219 	.pr_protocol = IPPROTO_ESP,
220 	.pr_flags = PR_ATOMIC|PR_ADDR,
221 
222 	.pr_input = esp4_input,
223 	.pr_output = NULL,
224 	.pr_ctlinput = NULL,
225 	.pr_ctloutput = NULL,
226 
227 	.pr_ctlport = NULL,
228 	.pr_usrreqs = &nousrreqs
229     },
230 #endif
231     {
232 	.pr_type = SOCK_RAW,
233 	.pr_domain = &inetdomain,
234 	.pr_protocol = IPPROTO_IPCOMP,
235 	.pr_flags = PR_ATOMIC|PR_ADDR,
236 
237 	.pr_input = ipcomp4_input,
238 	.pr_output = 0,
239 	.pr_ctlinput = NULL,
240 	.pr_ctloutput = NULL,
241 
242 	.pr_ctlport = NULL,
243 	.pr_usrreqs = &nousrreqs
244     },
245 #endif /* IPSEC */
246 #ifdef FAST_IPSEC
247     {
248 	.pr_type = SOCK_RAW,
249 	.pr_domain = &inetdomain,
250 	.pr_protocol = IPPROTO_AH,
251 	.pr_flags = PR_ATOMIC|PR_ADDR,
252 
253 	.pr_input = ipsec4_common_input,
254 	.pr_output = NULL,
255 	.pr_ctlinput = NULL,
256 	.pr_ctloutput = NULL,
257 
258 	.pr_ctlport = NULL,
259 	.pr_usrreqs = &nousrreqs
260     },
261     {
262 	.pr_type = SOCK_RAW,
263 	.pr_domain = &inetdomain,
264 	.pr_protocol = IPPROTO_ESP,
265 	.pr_flags = PR_ATOMIC|PR_ADDR,
266 
267 	.pr_input = ipsec4_common_input,
268 	.pr_output = NULL,
269 	.pr_ctlinput = NULL,
270 	.pr_ctloutput = NULL,
271 
272 	.pr_ctlport = NULL,
273 	.pr_usrreqs = &nousrreqs
274     },
275     {
276 	.pr_type = SOCK_RAW,
277 	.pr_domain = &inetdomain,
278 	.pr_protocol = IPPROTO_IPCOMP,
279 	.pr_flags = PR_ATOMIC|PR_ADDR,
280 
281 	.pr_input = ipsec4_common_input,
282 	.pr_output = NULL,
283 	.pr_ctlinput = NULL,
284 	.pr_ctloutput = NULL,
285 
286 	.pr_ctlport = NULL,
287 	.pr_usrreqs = &nousrreqs
288     },
289 #endif /* FAST_IPSEC */
290     {
291 	.pr_type = SOCK_RAW,
292 	.pr_domain = &inetdomain,
293 	.pr_protocol = IPPROTO_IPV4,
294 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
295 	.pr_input = encap4_input,
296 	.pr_output = NULL,
297 	.pr_ctlinput = NULL,
298 	.pr_ctloutput = rip_ctloutput,
299 
300 	.pr_ctlport = NULL,
301 	.pr_init = encap_init,
302 	.pr_usrreqs = &rip_usrreqs
303     },
304     {
305 	.pr_type = SOCK_RAW,
306 	.pr_domain = &inetdomain,
307 	.pr_protocol = IPPROTO_MOBILE,
308 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
309 
310 	.pr_input = encap4_input,
311 	.pr_output = NULL,
312 	.pr_ctlinput = NULL,
313 	.pr_ctloutput = rip_ctloutput,
314 
315 	.pr_ctlport = NULL,
316 	.pr_init = encap_init,
317 	.pr_usrreqs = &rip_usrreqs
318     },
319     {
320 	.pr_type = SOCK_RAW,
321 	.pr_domain = &inetdomain,
322 	.pr_protocol = IPPROTO_GRE,
323 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
324 
325 	.pr_input = encap4_input,
326 	.pr_output = NULL,
327 	.pr_ctlinput = NULL,
328 	.pr_ctloutput = rip_ctloutput,
329 
330 	.pr_ctlport = NULL,
331 	.pr_init = encap_init,
332 	.pr_usrreqs = &rip_usrreqs
333     },
334 #ifdef INET6
335     {
336 	.pr_type = SOCK_RAW,
337 	.pr_domain = &inetdomain,
338 	.pr_protocol = IPPROTO_IPV6,
339 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
340 
341 	.pr_input = encap4_input,
342 	.pr_output = NULL,
343 	.pr_ctlinput = NULL,
344 	.pr_ctloutput = rip_ctloutput,
345 
346 	.pr_ctlport = NULL,
347 	.pr_init = encap_init,
348 	.pr_usrreqs = &rip_usrreqs
349     },
350 #endif
351 #ifdef IPDIVERT
352     {
353 	.pr_type = SOCK_RAW,
354 	.pr_domain = &inetdomain,
355 	.pr_protocol = IPPROTO_DIVERT,
356 	.pr_flags = PR_ATOMIC|PR_ADDR,
357 
358 	.pr_input = div_input,
359 	.pr_output = NULL,
360 	.pr_ctlinput = NULL,
361 	.pr_ctloutput = ip_ctloutput,
362 
363 	.pr_ctlport = NULL,
364 	.pr_init = div_init,
365 	.pr_usrreqs = &div_usrreqs
366     },
367 #endif
368 #ifdef PIM
369     {
370 	.pr_type = SOCK_RAW,
371 	.pr_domain = &inetdomain,
372 	.pr_protocol = IPPROTO_PIM,
373 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
374 
375 	.pr_input = pim_input,
376 	.pr_output = NULL,
377 	.pr_ctlinput = NULL,
378 	.pr_ctloutput = rip_ctloutput,
379 
380 	.pr_ctlport = NULL,
381 	.pr_usrreqs = &rip_usrreqs
382     },
383 #endif
384 #ifdef NPFSYNC
385     {
386 	.pr_type = SOCK_RAW,
387 	.pr_domain = &inetdomain,
388 	.pr_protocol = IPPROTO_PFSYNC,
389 	.pr_flags = PR_ATOMIC|PR_ADDR,
390 
391 	.pr_input = pfsync_input,
392 	.pr_output = NULL,
393 	.pr_ctlinput = NULL,
394 	.pr_ctloutput = rip_ctloutput,
395 
396 	.pr_ctlport = NULL,
397 	.pr_usrreqs = &rip_usrreqs
398     },
399 #endif	/* NPFSYNC */
400     {
401 	/* raw wildcard */
402 	.pr_type = SOCK_RAW,
403 	.pr_domain = &inetdomain,
404 	.pr_protocol = 0,
405 	.pr_flags = PR_ATOMIC|PR_ADDR,
406 
407 	.pr_input = rip_input,
408 	.pr_output = NULL,
409 	.pr_ctlinput = NULL,
410 	.pr_ctloutput = rip_ctloutput,
411 
412 	.pr_init = rip_init,
413 	.pr_ctlport = NULL,
414 	.pr_usrreqs = &rip_usrreqs
415     },
416 #ifdef CARP
417     {
418 	.pr_type = SOCK_RAW,
419 	.pr_domain = &inetdomain,
420 	.pr_protocol = IPPROTO_CARP,
421 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE,
422 
423 	.pr_input = carp_proto_input,
424 	.pr_output = rip_output,
425 	.pr_ctlinput = carp_proto_ctlinput,
426 	.pr_ctloutput = rip_ctloutput,
427 
428 	.pr_ctlport = cpu0_ctlport,
429 	.pr_usrreqs = &rip_usrreqs
430     },
431 #endif
432 };
433 
434 static void
435 inetdomain_init(void)
436 {
437 	in_pcbglobalinit();
438 }
439 
440 struct domain inetdomain = {
441 	.dom_family		= AF_INET,
442 	.dom_name		= "internet",
443 	.dom_init		= inetdomain_init,
444 	.dom_externalize	= NULL,
445 	.dom_dispose		= NULL,
446 	.dom_protosw		= inetsw,
447 	.dom_protoswNPROTOSW	= &inetsw[NELEM(inetsw)],
448 	.dom_next		= SLIST_ENTRY_INITIALIZER,
449 	.dom_rtattach		= in_inithead,
450 	.dom_rtoffset		= 32,
451 	.dom_maxrtkey		= sizeof(struct sockaddr_in),
452 	.dom_ifattach		= NULL,
453 	.dom_ifdetach		= NULL
454 };
455 
456 DOMAIN_SET(inet);
457 
458 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
459 	"Internet Family");
460 
461 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
462 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
463 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
464 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
465 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
466 #ifdef FAST_IPSEC
467 /* XXX no protocol # to use, pick something "reserved" */
468 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
469 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
470 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
471 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
472 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
473 #else
474 #ifdef IPSEC
475 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ipsec,	CTLFLAG_RW, 0,	"IPSEC");
476 #endif /* IPSEC */
477 #endif /* !FAST_IPSEC */
478 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
479 #ifdef IPDIVERT
480 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT,	divert,	CTLFLAG_RW, 0,	"DIVERT");
481 #endif
482 #ifdef PIM
483 SYSCTL_NODE(_net_inet, IPPROTO_PIM,    pim,    CTLFLAG_RW, 0,  "PIM");
484 #endif
485 #ifdef CARP
486 SYSCTL_NODE(_net_inet, IPPROTO_CARP,    carp,    CTLFLAG_RW, 0,  "CARP");
487 #endif
488