xref: /dragonfly/sys/netinet/in_proto.c (revision ee0be9ca)
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_sctp.h"
38 #include "opt_carp.h"
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.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 #ifdef SCTP
88 #include <netinet/in_pcb.h>
89 #include <netinet/sctp_pcb.h>
90 #include <netinet/sctp.h>
91 #include <netinet/sctp_var.h>
92 #endif /* SCTP */
93 
94 #include <net/netisr.h>		/* for cpu0_soport */
95 
96 #ifdef CARP
97 #include <netinet/ip_carp.h>
98 #endif
99 
100 extern	struct domain inetdomain;
101 static	struct pr_usrreqs nousrreqs;
102 
103 struct protosw inetsw[] = {
104     {
105 	.pr_type = 0,
106 	.pr_domain = &inetdomain,
107 	.pr_protocol = 0,
108 	.pr_flags = 0,
109 
110 	.pr_ctlport = NULL,
111 
112 	.pr_init = ip_init,
113 	.pr_fasttimo = NULL,
114 	.pr_slowtimo = ip_slowtimo,
115 	.pr_drain = ip_drain,
116 	.pr_usrreqs = &nousrreqs
117     },
118     {
119 	.pr_type = SOCK_DGRAM,
120 	.pr_domain = &inetdomain,
121 	.pr_protocol = IPPROTO_UDP,
122 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE|
123 	    PR_ASYNC_SEND|PR_ASEND_HOLDTD,
124 
125 	.pr_input = udp_input,
126 	.pr_output = NULL,
127 	.pr_ctlinput = udp_ctlinput,
128 	.pr_ctloutput = ip_ctloutput,
129 
130 	.pr_ctlport = udp_ctlport,
131 	.pr_init = udp_init,
132 	.pr_usrreqs = &udp_usrreqs
133     },
134     {
135 	.pr_type = SOCK_STREAM,
136 	.pr_domain = &inetdomain,
137 	.pr_protocol = IPPROTO_TCP,
138 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_MPSAFE|
139 	    PR_ASYNC_SEND|PR_ASYNC_RCVD|PR_ACONN_HOLDTD,
140 
141 	.pr_initport = tcp_initport,
142 	.pr_input = tcp_input,
143 	.pr_output = NULL,
144 	.pr_ctlinput = tcp_ctlinput,
145 	.pr_ctloutput = tcp_ctloutput,
146 
147 	.pr_ctlport = tcp_ctlport,
148 	.pr_init = tcp_init,
149 	.pr_fasttimo = NULL,
150 	.pr_slowtimo = NULL,
151 	.pr_drain = tcp_drain,
152 	.pr_usrreqs = &tcp_usrreqs
153     },
154 #ifdef SCTP
155     /*
156      * Order is very important here, we add the good one in
157      * in this postion so it maps to the right ip_protox[]
158      * postion for SCTP. Don't move the one above below
159      * this one or IPv6/4 compatability will break
160      */
161     {
162 	.pr_type = SOCK_DGRAM,
163 	.pr_domain = &inetdomain,
164 	.pr_protocol = IPPROTO_SCTP,
165 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
166 
167 	.pr_input = sctp_input,
168 	.pr_output = NULL,
169 	.pr_ctlinput = sctp_ctlinput,
170 	.pr_ctloutput = sctp_ctloutput,
171 
172 	.pr_ctlport = cpu0_ctlport,
173 	.pr_init = sctp_init,
174 	.pr_drain = sctp_drain,
175 	.pr_usrreqs = &sctp_usrreqs
176     },
177     {
178 	.pr_type = SOCK_SEQPACKET,
179 	.pr_domain = &inetdomain,
180 	.pr_protocol = IPPROTO_SCTP,
181 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
182 
183 	.pr_input = sctp_input,
184 	.pr_output = NULL,
185 	.pr_ctlinput = sctp_ctlinput,
186 	.pr_ctloutput = sctp_ctloutput,
187 
188 	.pr_ctlport = cpu0_ctlport,
189 	.pr_drain = sctp_drain,
190 	.pr_usrreqs = &sctp_usrreqs
191     },
192 
193     {
194 	.pr_type = SOCK_STREAM,
195 	.pr_domain = &inetdomain,
196 	.pr_protocol = IPPROTO_SCTP,
197 	.pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD,
198 
199 	.pr_input = sctp_input,
200 	.pr_output = NULL,
201 	.pr_ctlinput = sctp_ctlinput,
202 	.pr_ctloutput = sctp_ctloutput,
203 
204 	.pr_ctlport = cpu0_ctlport,
205 	.pr_drain = sctp_drain,
206 	.pr_usrreqs = &sctp_usrreqs
207     },
208 #endif /* SCTP */
209     {
210 	.pr_type = SOCK_RAW,
211 	.pr_domain = &inetdomain,
212 	.pr_protocol = IPPROTO_RAW,
213 	.pr_flags = PR_ATOMIC|PR_ADDR,
214 
215 	.pr_input = rip_input,
216 	.pr_output = NULL,
217 	.pr_ctlinput = rip_ctlinput,
218 	.pr_ctloutput = rip_ctloutput,
219 
220 	.pr_ctlport = cpu0_ctlport,
221 	.pr_usrreqs = &rip_usrreqs
222     },
223     {
224 	.pr_type = SOCK_RAW,
225 	.pr_domain = &inetdomain,
226 	.pr_protocol = IPPROTO_ICMP,
227 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE,
228 
229 	.pr_input = icmp_input,
230 	.pr_output = NULL,
231 	.pr_ctlinput = NULL,
232 	.pr_ctloutput = rip_ctloutput,
233 
234 	.pr_usrreqs = &rip_usrreqs
235     },
236     {
237 	.pr_type = SOCK_RAW,
238 	.pr_domain = &inetdomain,
239 	.pr_protocol = IPPROTO_IGMP,
240 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
241 
242 	.pr_input = igmp_input,
243 	.pr_output = NULL,
244 	.pr_ctlinput = NULL,
245 	.pr_ctloutput = rip_ctloutput,
246 
247 	.pr_init = igmp_init,
248 	.pr_fasttimo = igmp_fasttimo,
249 	.pr_slowtimo = igmp_slowtimo,
250 	.pr_drain = NULL,
251 	.pr_usrreqs = &rip_usrreqs
252     },
253     {
254 	.pr_type = SOCK_RAW,
255 	.pr_domain = &inetdomain,
256 	.pr_protocol = IPPROTO_RSVP,
257 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
258 
259 	.pr_input = rsvp_input,
260 	.pr_output = NULL,
261 	.pr_ctlinput = NULL,
262 	.pr_ctloutput = rip_ctloutput,
263 
264 	.pr_usrreqs = &rip_usrreqs
265     },
266 #ifdef IPSEC
267     {
268 	.pr_type = SOCK_RAW,
269 	.pr_domain = &inetdomain,
270 	.pr_protocol = IPPROTO_AH,
271 	.pr_flags = PR_ATOMIC|PR_ADDR,
272 
273 	.pr_input = ah4_input,
274 	.pr_output = NULL,
275 	.pr_ctlinput = NULL,
276 	.pr_ctloutput = NULL,
277 
278 	.pr_ctlport = NULL,
279 	.pr_usrreqs = &nousrreqs
280     },
281 #ifdef IPSEC_ESP
282     {
283 	.pr_type = SOCK_RAW,
284 	.pr_domain = &inetdomain,
285 	.pr_protocol = IPPROTO_ESP,
286 	.pr_flags = PR_ATOMIC|PR_ADDR,
287 
288 	.pr_input = esp4_input,
289 	.pr_output = NULL,
290 	.pr_ctlinput = NULL,
291 	.pr_ctloutput = NULL,
292 
293 	.pr_ctlport = NULL,
294 	.pr_usrreqs = &nousrreqs
295     },
296 #endif
297     {
298 	.pr_type = SOCK_RAW,
299 	.pr_domain = &inetdomain,
300 	.pr_protocol = IPPROTO_IPCOMP,
301 	.pr_flags = PR_ATOMIC|PR_ADDR,
302 
303 	.pr_input = ipcomp4_input,
304 	.pr_output = 0,
305 	.pr_ctlinput = NULL,
306 	.pr_ctloutput = NULL,
307 
308 	.pr_ctlport = NULL,
309 	.pr_usrreqs = &nousrreqs
310     },
311 #endif /* IPSEC */
312 #ifdef FAST_IPSEC
313     {
314 	.pr_type = SOCK_RAW,
315 	.pr_domain = &inetdomain,
316 	.pr_protocol = IPPROTO_AH,
317 	.pr_flags = PR_ATOMIC|PR_ADDR,
318 
319 	.pr_input = ipsec4_common_input,
320 	.pr_output = NULL,
321 	.pr_ctlinput = NULL,
322 	.pr_ctloutput = NULL,
323 
324 	.pr_ctlport = NULL,
325 	.pr_usrreqs = &nousrreqs
326     },
327     {
328 	.pr_type = SOCK_RAW,
329 	.pr_domain = &inetdomain,
330 	.pr_protocol = IPPROTO_ESP,
331 	.pr_flags = PR_ATOMIC|PR_ADDR,
332 
333 	.pr_input = ipsec4_common_input,
334 	.pr_output = NULL,
335 	.pr_ctlinput = NULL,
336 	.pr_ctloutput = NULL,
337 
338 	.pr_ctlport = NULL,
339 	.pr_usrreqs = &nousrreqs
340     },
341     {
342 	.pr_type = SOCK_RAW,
343 	.pr_domain = &inetdomain,
344 	.pr_protocol = IPPROTO_IPCOMP,
345 	.pr_flags = PR_ATOMIC|PR_ADDR,
346 
347 	.pr_input = ipsec4_common_input,
348 	.pr_output = NULL,
349 	.pr_ctlinput = NULL,
350 	.pr_ctloutput = NULL,
351 
352 	.pr_ctlport = NULL,
353 	.pr_usrreqs = &nousrreqs
354     },
355 #endif /* FAST_IPSEC */
356     {
357 	.pr_type = SOCK_RAW,
358 	.pr_domain = &inetdomain,
359 	.pr_protocol = IPPROTO_IPV4,
360 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
361 	.pr_input = encap4_input,
362 	.pr_output = NULL,
363 	.pr_ctlinput = NULL,
364 	.pr_ctloutput = rip_ctloutput,
365 
366 	.pr_ctlport = NULL,
367 	.pr_init = encap_init,
368 	.pr_usrreqs = &rip_usrreqs
369     },
370     {
371 	.pr_type = SOCK_RAW,
372 	.pr_domain = &inetdomain,
373 	.pr_protocol = IPPROTO_MOBILE,
374 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
375 
376 	.pr_input = encap4_input,
377 	.pr_output = NULL,
378 	.pr_ctlinput = NULL,
379 	.pr_ctloutput = rip_ctloutput,
380 
381 	.pr_ctlport = NULL,
382 	.pr_init = encap_init,
383 	.pr_usrreqs = &rip_usrreqs
384     },
385     {
386 	.pr_type = SOCK_RAW,
387 	.pr_domain = &inetdomain,
388 	.pr_protocol = IPPROTO_GRE,
389 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
390 
391 	.pr_input = encap4_input,
392 	.pr_output = NULL,
393 	.pr_ctlinput = NULL,
394 	.pr_ctloutput = rip_ctloutput,
395 
396 	.pr_ctlport = NULL,
397 	.pr_init = encap_init,
398 	.pr_usrreqs = &rip_usrreqs
399     },
400 #ifdef INET6
401     {
402 	.pr_type = SOCK_RAW,
403 	.pr_domain = &inetdomain,
404 	.pr_protocol = IPPROTO_IPV6,
405 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
406 
407 	.pr_input = encap4_input,
408 	.pr_output = NULL,
409 	.pr_ctlinput = NULL,
410 	.pr_ctloutput = rip_ctloutput,
411 
412 	.pr_ctlport = NULL,
413 	.pr_init = encap_init,
414 	.pr_usrreqs = &rip_usrreqs
415     },
416 #endif
417 #ifdef IPDIVERT
418     {
419 	.pr_type = SOCK_RAW,
420 	.pr_domain = &inetdomain,
421 	.pr_protocol = IPPROTO_DIVERT,
422 	.pr_flags = PR_ATOMIC|PR_ADDR,
423 
424 	.pr_input = div_input,
425 	.pr_output = NULL,
426 	.pr_ctlinput = NULL,
427 	.pr_ctloutput = ip_ctloutput,
428 
429 	.pr_ctlport = NULL,
430 	.pr_init = div_init,
431 	.pr_usrreqs = &div_usrreqs
432     },
433 #endif
434 #ifdef PIM
435     {
436 	.pr_type = SOCK_RAW,
437 	.pr_domain = &inetdomain,
438 	.pr_protocol = IPPROTO_PIM,
439 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
440 
441 	.pr_input = pim_input,
442 	.pr_output = NULL,
443 	.pr_ctlinput = NULL,
444 	.pr_ctloutput = rip_ctloutput,
445 
446 	.pr_ctlport = NULL,
447 	.pr_usrreqs = &rip_usrreqs
448     },
449 #endif
450 #ifdef NPFSYNC
451     {
452 	.pr_type = SOCK_RAW,
453 	.pr_domain = &inetdomain,
454 	.pr_protocol = IPPROTO_PFSYNC,
455 	.pr_flags = PR_ATOMIC|PR_ADDR,
456 
457 	.pr_input = pfsync_input,
458 	.pr_output = NULL,
459 	.pr_ctlinput = NULL,
460 	.pr_ctloutput = rip_ctloutput,
461 
462 	.pr_ctlport = NULL,
463 	.pr_usrreqs = &rip_usrreqs
464     },
465 #endif	/* NPFSYNC */
466     {
467 	/* raw wildcard */
468 	.pr_type = SOCK_RAW,
469 	.pr_domain = &inetdomain,
470 	.pr_protocol = 0,
471 	.pr_flags = PR_ATOMIC|PR_ADDR,
472 
473 	.pr_input = rip_input,
474 	.pr_output = NULL,
475 	.pr_ctlinput = NULL,
476 	.pr_ctloutput = rip_ctloutput,
477 
478 	.pr_init = rip_init,
479 	.pr_ctlport = NULL,
480 	.pr_usrreqs = &rip_usrreqs
481     },
482 #ifdef CARP
483     {
484 	.pr_type = SOCK_RAW,
485 	.pr_domain = &inetdomain,
486 	.pr_protocol = IPPROTO_CARP,
487 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE,
488 
489 	.pr_input = carp_proto_input,
490 	.pr_output = rip_output,
491 	.pr_ctlinput = carp_proto_ctlinput,
492 	.pr_ctloutput = rip_ctloutput,
493 
494 	.pr_ctlport = cpu0_ctlport,
495 	.pr_usrreqs = &rip_usrreqs
496     },
497 #endif
498 };
499 
500 struct domain inetdomain = {
501 	AF_INET, "internet", NULL, NULL, NULL,
502 	inetsw, &inetsw[NELEM(inetsw)],
503 	SLIST_ENTRY_INITIALIZER,
504 	in_inithead, 32, sizeof(struct sockaddr_in),
505 };
506 
507 DOMAIN_SET(inet);
508 
509 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
510 	"Internet Family");
511 
512 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
513 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
514 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
515 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
516 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
517 #ifdef FAST_IPSEC
518 /* XXX no protocol # to use, pick something "reserved" */
519 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
520 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
521 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
522 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
523 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
524 #else
525 #ifdef IPSEC
526 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ipsec,	CTLFLAG_RW, 0,	"IPSEC");
527 #endif /* IPSEC */
528 #endif /* !FAST_IPSEC */
529 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
530 #ifdef IPDIVERT
531 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT,	divert,	CTLFLAG_RW, 0,	"DIVERT");
532 #endif
533 #ifdef PIM
534 SYSCTL_NODE(_net_inet, IPPROTO_PIM,    pim,    CTLFLAG_RW, 0,  "PIM");
535 #endif
536 #ifdef CARP
537 SYSCTL_NODE(_net_inet, IPPROTO_CARP,    carp,    CTLFLAG_RW, 0,  "CARP");
538 #endif
539