xref: /dragonfly/sys/netinet/in_proto.c (revision 19380330)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)in_proto.c	8.2 (Berkeley) 2/9/95
34  * $FreeBSD: src/sys/netinet/in_proto.c,v 1.53.2.7 2003/08/24 08:24:38 hsu Exp $
35  */
36 
37 #include "opt_ipdivert.h"
38 #include "opt_ipx.h"
39 #include "opt_mrouting.h"
40 #include "opt_ipsec.h"
41 #include "opt_inet6.h"
42 #include "opt_sctp.h"
43 #include "opt_carp.h"
44 
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/queue.h>
51 #include <sys/sysctl.h>
52 
53 #include <net/if.h>
54 #include <net/route.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/igmp_var.h>
62 #ifdef PIM
63 #include <netinet/pim_var.h>
64 #endif
65 #include <netinet/tcp.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70 #include <netinet/ip_encap.h>
71 #ifdef IPDIVERT
72 #include <netinet/ip_divert.h>
73 #endif
74 
75 /*
76  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
77  */
78 
79 #ifdef IPSEC
80 #include <netinet6/ipsec.h>
81 #include <netinet6/ah.h>
82 #ifdef IPSEC_ESP
83 #include <netinet6/esp.h>
84 #endif
85 #include <netinet6/ipcomp.h>
86 #endif /* IPSEC */
87 
88 #ifdef FAST_IPSEC
89 #include <netproto/ipsec/ipsec.h>
90 #endif /* FAST_IPSEC */
91 
92 #ifdef IPXIP
93 #include <netproto/ipx/ipx_ip.h>
94 #endif
95 
96 #ifdef SCTP
97 #include <netinet/in_pcb.h>
98 #include <netinet/sctp_pcb.h>
99 #include <netinet/sctp.h>
100 #include <netinet/sctp_var.h>
101 #endif /* SCTP */
102 
103 #include <net/netisr.h>		/* for cpu0_soport */
104 
105 #ifdef CARP
106 #include <netinet/ip_carp.h>
107 #endif
108 
109 extern	struct domain inetdomain;
110 static	struct pr_usrreqs nousrreqs;
111 
112 struct protosw inetsw[] = {
113     {
114 	.pr_type = 0,
115 	.pr_domain = &inetdomain,
116 	.pr_protocol = 0,
117 	.pr_flags = 0,
118 
119 	.pr_ctlport = NULL,
120 
121 	.pr_init = ip_init,
122 	.pr_fasttimo = NULL,
123 	.pr_slowtimo = ip_slowtimo,
124 	.pr_drain = ip_drain,
125 	.pr_usrreqs = &nousrreqs
126     },
127     {
128 	.pr_type = SOCK_DGRAM,
129 	.pr_domain = &inetdomain,
130 	.pr_protocol = IPPROTO_UDP,
131 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE|PR_ASYNC_SEND,
132 
133 	.pr_input = udp_input,
134 	.pr_output = NULL,
135 	.pr_ctlinput = udp_ctlinput,
136 	.pr_ctloutput = ip_ctloutput,
137 
138 	.pr_ctlport = udp_ctlport,
139 	.pr_init = udp_init,
140 	.pr_usrreqs = &udp_usrreqs
141     },
142     {
143 	.pr_type = SOCK_STREAM,
144 	.pr_domain = &inetdomain,
145 	.pr_protocol = IPPROTO_TCP,
146 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|
147 	    PR_MPSAFE|PR_ASYNC_SEND|PR_ASYNC_RCVD,
148 
149 	.pr_input = tcp_input,
150 	.pr_output = NULL,
151 	.pr_ctlinput = tcp_ctlinput,
152 	.pr_ctloutput = tcp_ctloutput,
153 
154 	.pr_ctlport = tcp_ctlport,
155 	.pr_init = tcp_init,
156 	.pr_fasttimo = NULL,
157 	.pr_slowtimo = NULL,
158 	.pr_drain = tcp_drain,
159 	.pr_usrreqs = &tcp_usrreqs
160     },
161 #ifdef SCTP
162     /*
163      * Order is very important here, we add the good one in
164      * in this postion so it maps to the right ip_protox[]
165      * postion for SCTP. Don't move the one above below
166      * this one or IPv6/4 compatability will break
167      */
168     {
169 	.pr_type = SOCK_DGRAM,
170 	.pr_domain = &inetdomain,
171 	.pr_protocol = IPPROTO_SCTP,
172 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
173 
174 	.pr_input = sctp_input,
175 	.pr_output = NULL,
176 	.pr_ctlinput = sctp_ctlinput,
177 	.pr_ctloutput = sctp_ctloutput,
178 
179 	.pr_ctlport = cpu0_ctlport,
180 	.pr_init = sctp_init,
181 	.pr_drain = sctp_drain,
182 	.pr_usrreqs = &sctp_usrreqs
183     },
184     {
185 	.pr_type = SOCK_SEQPACKET,
186 	.pr_domain = &inetdomain,
187 	.pr_protocol = IPPROTO_SCTP,
188 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
189 
190 	.pr_input = sctp_input,
191 	.pr_output = NULL,
192 	.pr_ctlinput = sctp_ctlinput,
193 	.pr_ctloutput = sctp_ctloutput,
194 
195 	.pr_ctlport = cpu0_ctlport,
196 	.pr_drain = sctp_drain,
197 	.pr_usrreqs = &sctp_usrreqs
198     },
199 
200     {
201 	.pr_type = SOCK_STREAM,
202 	.pr_domain = &inetdomain,
203 	.pr_protocol = IPPROTO_SCTP,
204 	.pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD,
205 
206 	.pr_input = sctp_input,
207 	.pr_output = NULL,
208 	.pr_ctlinput = sctp_ctlinput,
209 	.pr_ctloutput = sctp_ctloutput,
210 
211 	.pr_ctlport = cpu0_ctlport,
212 	.pr_drain = sctp_drain,
213 	.pr_usrreqs = &sctp_usrreqs
214     },
215 #endif /* SCTP */
216     {
217 	.pr_type = SOCK_RAW,
218 	.pr_domain = &inetdomain,
219 	.pr_protocol = IPPROTO_RAW,
220 	.pr_flags = PR_ATOMIC|PR_ADDR,
221 
222 	.pr_input = rip_input,
223 	.pr_output = NULL,
224 	.pr_ctlinput = rip_ctlinput,
225 	.pr_ctloutput = rip_ctloutput,
226 
227 	.pr_ctlport = cpu0_ctlport,
228 	.pr_usrreqs = &rip_usrreqs
229     },
230     {
231 	.pr_type = SOCK_RAW,
232 	.pr_domain = &inetdomain,
233 	.pr_protocol = IPPROTO_ICMP,
234 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
235 
236 	.pr_input = icmp_input,
237 	.pr_output = NULL,
238 	.pr_ctlinput = NULL,
239 	.pr_ctloutput = rip_ctloutput,
240 
241 	.pr_usrreqs = &rip_usrreqs
242     },
243     {
244 	.pr_type = SOCK_RAW,
245 	.pr_domain = &inetdomain,
246 	.pr_protocol = IPPROTO_IGMP,
247 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
248 
249 	.pr_input = igmp_input,
250 	.pr_output = NULL,
251 	.pr_ctlinput = NULL,
252 	.pr_ctloutput = rip_ctloutput,
253 
254 	.pr_init = igmp_init,
255 	.pr_fasttimo = igmp_fasttimo,
256 	.pr_slowtimo = igmp_slowtimo,
257 	.pr_drain = NULL,
258 	.pr_usrreqs = &rip_usrreqs
259     },
260     {
261 	.pr_type = SOCK_RAW,
262 	.pr_domain = &inetdomain,
263 	.pr_protocol = IPPROTO_RSVP,
264 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
265 
266 	.pr_input = rsvp_input,
267 	.pr_output = NULL,
268 	.pr_ctlinput = NULL,
269 	.pr_ctloutput = rip_ctloutput,
270 
271 	.pr_usrreqs = &rip_usrreqs
272     },
273 #ifdef IPSEC
274     {
275 	.pr_type = SOCK_RAW,
276 	.pr_domain = &inetdomain,
277 	.pr_protocol = IPPROTO_AH,
278 	.pr_flags = PR_ATOMIC|PR_ADDR,
279 
280 	.pr_input = ah4_input,
281 	.pr_output = NULL,
282 	.pr_ctlinput = NULL,
283 	.pr_ctloutput = NULL,
284 
285 	.pr_ctlport = NULL,
286 	.pr_usrreqs = &nousrreqs
287     },
288 #ifdef IPSEC_ESP
289     {
290 	.pr_type = SOCK_RAW,
291 	.pr_domain = &inetdomain,
292 	.pr_protocol = IPPROTO_ESP,
293 	.pr_flags = PR_ATOMIC|PR_ADDR,
294 
295 	.pr_input = esp4_input,
296 	.pr_output = NULL,
297 	.pr_ctlinput = NULL,
298 	.pr_ctloutput = NULL,
299 
300 	.pr_ctlport = NULL,
301 	.pr_usrreqs = &nousrreqs
302     },
303 #endif
304     {
305 	.pr_type = SOCK_RAW,
306 	.pr_domain = &inetdomain,
307 	.pr_protocol = IPPROTO_IPCOMP,
308 	.pr_flags = PR_ATOMIC|PR_ADDR,
309 
310 	.pr_input = ipcomp4_input,
311 	.pr_output = 0,
312 	.pr_ctlinput = NULL,
313 	.pr_ctloutput = NULL,
314 
315 	.pr_ctlport = NULL,
316 	.pr_usrreqs = &nousrreqs
317     },
318 #endif /* IPSEC */
319 #ifdef FAST_IPSEC
320     {
321 	.pr_type = SOCK_RAW,
322 	.pr_domain = &inetdomain,
323 	.pr_protocol = IPPROTO_AH,
324 	.pr_flags = PR_ATOMIC|PR_ADDR,
325 
326 	.pr_input = ipsec4_common_input,
327 	.pr_output = NULL,
328 	.pr_ctlinput = NULL,
329 	.pr_ctloutput = NULL,
330 
331 	.pr_ctlport = NULL,
332 	.pr_usrreqs = &nousrreqs
333     },
334     {
335 	.pr_type = SOCK_RAW,
336 	.pr_domain = &inetdomain,
337 	.pr_protocol = IPPROTO_ESP,
338 	.pr_flags = PR_ATOMIC|PR_ADDR,
339 
340 	.pr_input = ipsec4_common_input,
341 	.pr_output = NULL
342 	.pr_ctlinput = NULL,
343 	.pr_ctloutput = NULL,
344 
345 	.pr_ctlport = NULL,
346 	.pr_usrreqs = &nousrreqs
347     },
348     {
349 	.pr_type = SOCK_RAW,
350 	.pr_domain = &inetdomain,
351 	.pr_protocol = IPPROTO_IPCOMP,
352 	.pr_flags = PR_ATOMIC|PR_ADDR,
353 
354 	.pr_input = ipsec4_common_input,
355 	.pr_output = NULL,
356 	.pr_ctlinput = NULL,
357 	.pr_ctloutput = NULL,
358 
359 	.pr_ctlport = NULL,
360 	.pr_usrreqs = &nousrreqs
361     },
362 #endif /* FAST_IPSEC */
363     {
364 	.pr_type = SOCK_RAW,
365 	.pr_domain = &inetdomain,
366 	.pr_protocol = IPPROTO_IPV4,
367 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
368 	.pr_input = encap4_input,
369 	.pr_output = NULL,
370 	.pr_ctlinput = NULL,
371 	.pr_ctloutput = rip_ctloutput,
372 
373 	.pr_ctlport = NULL,
374 	.pr_init = encap_init,
375 	.pr_usrreqs = &rip_usrreqs
376     },
377     {
378 	.pr_type = SOCK_RAW,
379 	.pr_domain = &inetdomain,
380 	.pr_protocol = IPPROTO_MOBILE,
381 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
382 
383 	.pr_input = encap4_input,
384 	.pr_output = NULL,
385 	.pr_ctlinput = NULL,
386 	.pr_ctloutput = rip_ctloutput,
387 
388 	.pr_ctlport = NULL,
389 	.pr_init = encap_init,
390 	.pr_usrreqs = &rip_usrreqs
391     },
392     {
393 	.pr_type = SOCK_RAW,
394 	.pr_domain = &inetdomain,
395 	.pr_protocol = IPPROTO_GRE,
396 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
397 
398 	.pr_input = encap4_input,
399 	.pr_output = NULL,
400 	.pr_ctlinput = NULL,
401 	.pr_ctloutput = rip_ctloutput,
402 
403 	.pr_ctlport = NULL,
404 	.pr_init = encap_init,
405 	.pr_usrreqs = &rip_usrreqs
406     },
407 #ifdef INET6
408     {
409 	.pr_type = SOCK_RAW,
410 	.pr_domain = &inetdomain,
411 	.pr_protocol = IPPROTO_IPV6,
412 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
413 
414 	.pr_input = encap4_input,
415 	.pr_output = NULL,
416 	.pr_ctlinput = NULL,
417 	.pr_ctloutput = rip_ctloutput,
418 
419 	.pr_ctlport = NULL,
420 	.pr_init = encap_init,
421 	.pr_usrreqs = &rip_usrreqs
422     },
423 #endif
424 #ifdef IPDIVERT
425     {
426 	.pr_type = SOCK_RAW,
427 	.pr_domain = &inetdomain,
428 	.pr_protocol = IPPROTO_DIVERT,
429 	.pr_flags = PR_ATOMIC|PR_ADDR,
430 
431 	.pr_input = div_input,
432 	.pr_output = NULL,
433 	.pr_ctlinput = NULL,
434 	.pr_ctloutput = ip_ctloutput,
435 
436 	.pr_ctlport = NULL,
437 	.pr_init = div_init,
438 	.pr_usrreqs = &div_usrreqs
439     },
440 #endif
441 #ifdef IPXIP
442     {
443 	.pr_type = SOCK_RAW,
444 	.pr_domain = &inetdomain,
445 	.pr_protocol = IPPROTO_IDP,
446 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
447 
448 	.pr_input = ipxip_input,
449 	.pr_output = NULL,
450 	.pr_ctlinput = ipxip_ctlinput,
451 	.pr_ctloutput = NULL,
452 
453 	.pr_ctlport = cpu0_ctlport,
454 	.pr_usrreqs = &rip_usrreqs
455     },
456 #endif
457 #ifdef PIM
458     {
459 	.pr_type = SOCK_RAW,
460 	.pr_domain = &inetdomain,
461 	.pr_protocol = IPPROTO_PIM,
462 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
463 
464 	.pr_input = pim_input,
465 	.pr_output = NULL,
466 	.pr_ctlinput = NULL,
467 	.pr_ctloutput = rip_ctloutput,
468 
469 	.pr_ctlport = NULL,
470 	.pr_usrreqs = &rip_usrreqs
471     },
472 #endif
473 #ifdef NPFSYNC
474     {
475 	.pr_type = SOCK_RAW,
476 	.pr_domain = &inetdomain,
477 	.pr_protocol = IPPROTO_PFSYNC,
478 	.pr_flags = PR_ATOMIC|PR_ADDR,
479 
480 	.pr_input = pfsync_input,
481 	.pr_output = NULL,
482 	.pr_ctlinput = NULL,
483 	.pr_ctloutput = rip_ctloutput,
484 
485 	.pr_ctlport = NULL,
486 	.pr_usrreqs = &rip_usrreqs
487     },
488 #endif	/* NPFSYNC */
489     {
490 	/* raw wildcard */
491 	.pr_type = SOCK_RAW,
492 	.pr_domain = &inetdomain,
493 	.pr_protocol = 0,
494 	.pr_flags = PR_ATOMIC|PR_ADDR,
495 
496 	.pr_input = rip_input,
497 	.pr_output = NULL,
498 	.pr_ctlinput = NULL,
499 	.pr_ctloutput = rip_ctloutput,
500 
501 	.pr_init = rip_init,
502 	.pr_ctlport = NULL,
503 	.pr_usrreqs = &rip_usrreqs
504     },
505 #ifdef CARP
506     {
507 	.pr_type = SOCK_RAW,
508 	.pr_domain = &inetdomain,
509 	.pr_protocol = IPPROTO_CARP,
510 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE,
511 
512 	.pr_input = carp_proto_input,
513 	.pr_output = rip_output,
514 	.pr_ctlinput = carp_proto_ctlinput,
515 	.pr_ctloutput = rip_ctloutput,
516 
517 	.pr_ctlport = cpu0_ctlport,
518 	.pr_usrreqs = &rip_usrreqs
519     },
520 #endif
521 };
522 
523 struct domain inetdomain = {
524 	AF_INET, "internet", NULL, NULL, NULL,
525 	inetsw, &inetsw[NELEM(inetsw)],
526 	SLIST_ENTRY_INITIALIZER,
527 	in_inithead, 32, sizeof(struct sockaddr_in),
528 };
529 
530 DOMAIN_SET(inet);
531 
532 SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
533 	"Internet Family");
534 
535 SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
536 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
537 SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
538 SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
539 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
540 #ifdef FAST_IPSEC
541 /* XXX no protocol # to use, pick something "reserved" */
542 SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
543 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
544 SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
545 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
546 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
547 #else
548 #ifdef IPSEC
549 SYSCTL_NODE(_net_inet, IPPROTO_AH,	ipsec,	CTLFLAG_RW, 0,	"IPSEC");
550 #endif /* IPSEC */
551 #endif /* !FAST_IPSEC */
552 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
553 #ifdef IPDIVERT
554 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT,	divert,	CTLFLAG_RW, 0,	"DIVERT");
555 #endif
556 #ifdef PIM
557 SYSCTL_NODE(_net_inet, IPPROTO_PIM,    pim,    CTLFLAG_RW, 0,  "PIM");
558 #endif
559 #ifdef CARP
560 SYSCTL_NODE(_net_inet, IPPROTO_CARP,    carp,    CTLFLAG_RW, 0,  "CARP");
561 #endif
562