Merge from vendor branch OPENSSL:
[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. 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  * $DragonFly: src/sys/netinet/in_proto.c,v 1.16 2008/08/28 14:24:59 sephe Exp $
36  */
37
38 #include "opt_ipdivert.h"
39 #include "opt_ipx.h"
40 #include "opt_mrouting.h"
41 #include "opt_ipsec.h"
42 #include "opt_inet6.h"
43 #include "opt_sctp.h"
44 #include "opt_carp.h"
45
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/domain.h>
50 #include <sys/protosw.h>
51 #include <sys/queue.h>
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/route.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_icmp.h>
62 #include <netinet/igmp_var.h>
63 #ifdef PIM
64 #include <netinet/pim_var.h>
65 #endif
66 #include <netinet/tcp.h>
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
71 #include <netinet/ip_encap.h>
72 #ifdef IPDIVERT
73 #include <netinet/ip_divert.h>
74 #endif
75
76 /*
77  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
78  */
79
80 #ifdef IPSEC
81 #include <netinet6/ipsec.h>
82 #include <netinet6/ah.h>
83 #ifdef IPSEC_ESP
84 #include <netinet6/esp.h>
85 #endif
86 #include <netinet6/ipcomp.h>
87 #endif /* IPSEC */
88
89 #ifdef FAST_IPSEC
90 #include <netproto/ipsec/ipsec.h>
91 #endif /* FAST_IPSEC */
92
93 #ifdef IPXIP
94 #include <netproto/ipx/ipx_ip.h>
95 #endif
96
97 #ifdef NSIP
98 #include <netns/ns.h>
99 #include <netns/ns_if.h>
100 #endif
101
102 #ifdef SCTP
103 #include <netinet/in_pcb.h>
104 #include <netinet/sctp_pcb.h>
105 #include <netinet/sctp.h>
106 #include <netinet/sctp_var.h>
107 #endif /* SCTP */
108
109 #include <net/netisr.h>         /* for cpu0_soport */
110
111 #ifdef CARP
112 #include <netinet/ip_carp.h>
113 #endif
114
115 extern  struct domain inetdomain;
116 static  struct pr_usrreqs nousrreqs;
117
118 struct protosw inetsw[] = {
119 { 0,            &inetdomain,    0,              0,
120   0,            0,              0,              0,
121   cpu0_soport,
122   ip_init,      0,              ip_slowtimo,    ip_drain,
123   &nousrreqs
124 },
125 { SOCK_DGRAM,   &inetdomain,    IPPROTO_UDP,    PR_ATOMIC|PR_ADDR,
126   udp_input,    0,              udp_ctlinput,   ip_ctloutput,
127   udp_soport,
128   udp_init,     0,              0,              0,
129   &udp_usrreqs
130 },
131 { SOCK_STREAM,  &inetdomain,    IPPROTO_TCP,
132         PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
133   tcp_input,    0,              tcp_ctlinput,   tcp_ctloutput,
134   tcp_soport,
135   tcp_init,     0,              tcp_slowtimo,   tcp_drain,
136   &tcp_usrreqs
137 },
138 #ifdef SCTP
139 /*
140  * Order is very important here, we add the good one in
141  * in this postion so it maps to the right ip_protox[]
142  * postion for SCTP. Don't move the one above below
143  * this one or IPv6/4 compatability will break
144  */
145 { SOCK_DGRAM,   &inetdomain,    IPPROTO_SCTP,   PR_ADDR_OPT|PR_WANTRCVD,
146   sctp_input,   0,              sctp_ctlinput,  sctp_ctloutput,
147   cpu0_soport,
148   sctp_init,    0,              0,              sctp_drain,
149   &sctp_usrreqs
150 },
151 { SOCK_SEQPACKET,&inetdomain,   IPPROTO_SCTP,   PR_ADDR_OPT|PR_WANTRCVD,
152   sctp_input,   0,              sctp_ctlinput,  sctp_ctloutput,
153   cpu0_soport,
154   0,            0,              0,              sctp_drain,
155   &sctp_usrreqs
156 },
157
158 { SOCK_STREAM,  &inetdomain,    IPPROTO_SCTP,   PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD,
159   sctp_input,   0,              sctp_ctlinput,  sctp_ctloutput,
160   cpu0_soport,
161   0,            0,              0,              sctp_drain,
162   &sctp_usrreqs
163 },
164 #endif /* SCTP */
165 { SOCK_RAW,     &inetdomain,    IPPROTO_RAW,    PR_ATOMIC|PR_ADDR,
166   rip_input,    0,              rip_ctlinput,   rip_ctloutput,
167   cpu0_soport,
168   0,            0,              0,              0,
169   &rip_usrreqs
170 },
171 { SOCK_RAW,     &inetdomain,    IPPROTO_ICMP,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
172   icmp_input,   0,              0,              rip_ctloutput,
173   cpu0_soport,
174   0,            0,              0,              0,
175   &rip_usrreqs
176 },
177 { SOCK_RAW,     &inetdomain,    IPPROTO_IGMP,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
178   igmp_input,   0,              0,              rip_ctloutput,
179   cpu0_soport,
180   igmp_init,    igmp_fasttimo,  igmp_slowtimo,  0,
181   &rip_usrreqs
182 },
183 { SOCK_RAW,     &inetdomain,    IPPROTO_RSVP,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
184   rsvp_input,   0,              0,              rip_ctloutput,
185   cpu0_soport,
186   0,            0,              0,              0,
187   &rip_usrreqs
188 },
189 #ifdef IPSEC
190 { SOCK_RAW,     &inetdomain,    IPPROTO_AH,     PR_ATOMIC|PR_ADDR,
191   ah4_input,    0,              0,              0,
192   cpu0_soport,
193   0,            0,              0,              0,
194   &nousrreqs
195 },
196 #ifdef IPSEC_ESP
197 { SOCK_RAW,     &inetdomain,    IPPROTO_ESP,    PR_ATOMIC|PR_ADDR,
198   esp4_input,   0,              0,              0,
199   cpu0_soport,
200   0,            0,              0,              0,
201   &nousrreqs
202 },
203 #endif
204 { SOCK_RAW,     &inetdomain,    IPPROTO_IPCOMP, PR_ATOMIC|PR_ADDR,
205   ipcomp4_input, 0,             0,              0,
206   cpu0_soport,
207   0,            0,              0,              0,
208   &nousrreqs
209 },
210 #endif /* IPSEC */
211 #ifdef FAST_IPSEC
212 { SOCK_RAW,     &inetdomain,    IPPROTO_AH,     PR_ATOMIC|PR_ADDR,
213   ipsec4_common_input,  0,      0,              0,
214   cpu0_soport,
215   0,            0,              0,              0,
216   &nousrreqs
217 },
218 { SOCK_RAW,     &inetdomain,    IPPROTO_ESP,    PR_ATOMIC|PR_ADDR,
219   ipsec4_common_input,  0,      0,              0,
220   cpu0_soport,
221   0,            0,              0,              0,
222   &nousrreqs
223 },
224 { SOCK_RAW,     &inetdomain,    IPPROTO_IPCOMP, PR_ATOMIC|PR_ADDR,
225   ipsec4_common_input,  0,      0,              0,
226   cpu0_soport,
227   0,            0,              0,              0,
228   &nousrreqs
229 },
230 #endif /* FAST_IPSEC */
231 { SOCK_RAW,     &inetdomain,    IPPROTO_IPV4,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
232   encap4_input, 0,              0,              rip_ctloutput,
233   cpu0_soport,
234   encap_init,   0,              0,              0,
235   &rip_usrreqs
236 },
237 { SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
238   encap4_input, 0,              0,              rip_ctloutput,
239   cpu0_soport,
240   encap_init,   0,              0,              0,
241   &rip_usrreqs
242 },
243 { SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
244   encap4_input, 0,              0,              rip_ctloutput,
245   cpu0_soport,
246   encap_init,   0,              0,              0,
247   &rip_usrreqs
248 },
249 # ifdef INET6
250 { SOCK_RAW,     &inetdomain,    IPPROTO_IPV6,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
251   encap4_input, 0,              0,              rip_ctloutput,
252   cpu0_soport,
253   encap_init,   0,              0,              0,
254   &rip_usrreqs
255 },
256 #endif
257 #ifdef IPDIVERT
258 { SOCK_RAW,     &inetdomain,    IPPROTO_DIVERT, PR_ATOMIC|PR_ADDR,
259   div_input,    0,              0,              ip_ctloutput,
260   div_soport,
261   div_init,     0,              0,              0,
262   &div_usrreqs,
263 },
264 #endif
265 #ifdef IPXIP
266 { SOCK_RAW,     &inetdomain,    IPPROTO_IDP,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
267   ipxip_input,  0,              ipxip_ctlinput, 0,
268   cpu0_soport,
269   0,            0,              0,              0,
270   &rip_usrreqs
271 },
272 #endif
273 #ifdef NSIP
274 { SOCK_RAW,     &inetdomain,    IPPROTO_IDP,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
275   idpip_input,  0,              nsip_ctlinput,  0,
276   cpu0_soport,
277   0,            0,              0,              0,
278   &rip_usrreqs
279 },
280 #endif
281 #ifdef PIM
282 { SOCK_RAW,     &inetdomain,    IPPROTO_PIM,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
283   pim_input,    0,              0,              rip_ctloutput,
284   cpu0_soport,
285   0,            0,              0,              0,
286   &rip_usrreqs
287 },
288 #endif
289 #ifdef NPFSYNC
290 { SOCK_RAW,     &inetdomain,    IPPROTO_PFSYNC, PR_ATOMIC|PR_ADDR,
291   pfsync_input, 0,              0,              rip_ctloutput,
292   0,
293   0,            0,              0,              0,
294   &rip_usrreqs
295 },
296 #endif  /* NPFSYNC */
297         /* raw wildcard */
298 { SOCK_RAW,     &inetdomain,    0,              PR_ATOMIC|PR_ADDR,
299   rip_input,    0,              0,              rip_ctloutput,
300   cpu0_soport,
301   rip_init,     0,              0,              0,
302   &rip_usrreqs
303 },
304
305 #ifdef CARP
306  { SOCK_RAW,     &inetdomain,    IPPROTO_CARP,   PR_ATOMIC|PR_ADDR,
307   carp_input,   rip_output,       0,      rip_ctloutput,
308   0,
309   0,            0,              0,              0,
310   &rip_usrreqs
311 },
312
313 #endif
314 };
315
316 struct domain inetdomain = {
317         AF_INET, "internet", NULL, NULL, NULL,
318         inetsw, &inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
319         SLIST_ENTRY_INITIALIZER,
320         in_inithead, 32, sizeof(struct sockaddr_in),
321 };
322
323 DOMAIN_SET(inet);
324
325 SYSCTL_NODE(_net,      PF_INET,         inet,   CTLFLAG_RW, 0,
326         "Internet Family");
327
328 SYSCTL_NODE(_net_inet, IPPROTO_IP,      ip,     CTLFLAG_RW, 0,  "IP");
329 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,    icmp,   CTLFLAG_RW, 0,  "ICMP");
330 SYSCTL_NODE(_net_inet, IPPROTO_UDP,     udp,    CTLFLAG_RW, 0,  "UDP");
331 SYSCTL_NODE(_net_inet, IPPROTO_TCP,     tcp,    CTLFLAG_RW, 0,  "TCP");
332 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,    igmp,   CTLFLAG_RW, 0,  "IGMP");
333 #ifdef FAST_IPSEC
334 /* XXX no protocol # to use, pick something "reserved" */
335 SYSCTL_NODE(_net_inet, 253,             ipsec,  CTLFLAG_RW, 0,  "IPSEC");
336 SYSCTL_NODE(_net_inet, IPPROTO_AH,      ah,     CTLFLAG_RW, 0,  "AH");
337 SYSCTL_NODE(_net_inet, IPPROTO_ESP,     esp,    CTLFLAG_RW, 0,  "ESP");
338 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,  ipcomp, CTLFLAG_RW, 0,  "IPCOMP");
339 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,    ipip,   CTLFLAG_RW, 0,  "IPIP");
340 #else
341 #ifdef IPSEC
342 SYSCTL_NODE(_net_inet, IPPROTO_AH,      ipsec,  CTLFLAG_RW, 0,  "IPSEC");
343 #endif /* IPSEC */
344 #endif /* !FAST_IPSEC */
345 SYSCTL_NODE(_net_inet, IPPROTO_RAW,     raw,    CTLFLAG_RW, 0,  "RAW");
346 #ifdef IPDIVERT
347 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT,  divert, CTLFLAG_RW, 0,  "DIVERT");
348 #endif
349 #ifdef PIM
350 SYSCTL_NODE(_net_inet, IPPROTO_PIM,    pim,    CTLFLAG_RW, 0,  "PIM");
351 #endif
352 #ifdef CARP
353 SYSCTL_NODE(_net_inet, IPPROTO_CARP,    carp,    CTLFLAG_RW, 0,  "CARP");
354 #endif