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