Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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.17 2008/10/27 02:56:30 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,  NULL,
122   ip_init,      0,              ip_slowtimo,    ip_drain,
123   &nousrreqs
124 },
125 { SOCK_DGRAM,   &inetdomain,    IPPROTO_UDP,    PR_ATOMIC|PR_ADDR|PR_MPSAFE,
126   udp_input,    0,              udp_ctlinput,   ip_ctloutput,
127   udp_soport,   udp_ctlport,
128   udp_init,     0,              0,              0,
129   &udp_usrreqs
130 },
131 { SOCK_STREAM,  &inetdomain,    IPPROTO_TCP,    PR_CONNREQUIRED |
132                                                 PR_IMPLOPCL | PR_WANTRCVD |
133                                                 PR_MPSAFE,
134   tcp_input,    0,              tcp_ctlinput,   tcp_ctloutput,
135   tcp_soport,   tcp_ctlport,
136   tcp_init,     0,              tcp_slowtimo,   tcp_drain,
137   &tcp_usrreqs
138 },
139 #ifdef SCTP
140 /*
141  * Order is very important here, we add the good one in
142  * in this postion so it maps to the right ip_protox[]
143  * postion for SCTP. Don't move the one above below
144  * this one or IPv6/4 compatability will break
145  */
146 { SOCK_DGRAM,   &inetdomain,    IPPROTO_SCTP,   PR_ADDR_OPT|PR_WANTRCVD,
147   sctp_input,   0,              sctp_ctlinput,  sctp_ctloutput,
148   cpu0_soport,  cpu0_ctlport,
149   sctp_init,    0,              0,              sctp_drain,
150   &sctp_usrreqs
151 },
152 { SOCK_SEQPACKET,&inetdomain,   IPPROTO_SCTP,   PR_ADDR_OPT|PR_WANTRCVD,
153   sctp_input,   0,              sctp_ctlinput,  sctp_ctloutput,
154   cpu0_soport,  cpu0_ctlport,
155   0,            0,              0,              sctp_drain,
156   &sctp_usrreqs
157 },
158
159 { SOCK_STREAM,  &inetdomain,    IPPROTO_SCTP,   PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD,
160   sctp_input,   0,              sctp_ctlinput,  sctp_ctloutput,
161   cpu0_soport,  cpu0_ctlport,
162   0,            0,              0,              sctp_drain,
163   &sctp_usrreqs
164 },
165 #endif /* SCTP */
166 { SOCK_RAW,     &inetdomain,    IPPROTO_RAW,    PR_ATOMIC|PR_ADDR,
167   rip_input,    0,              rip_ctlinput,   rip_ctloutput,
168   cpu0_soport,  cpu0_ctlport,
169   0,            0,              0,              0,
170   &rip_usrreqs
171 },
172 { SOCK_RAW,     &inetdomain,    IPPROTO_ICMP,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
173   icmp_input,   0,              0,              rip_ctloutput,
174   cpu0_soport,  NULL,
175   0,            0,              0,              0,
176   &rip_usrreqs
177 },
178 { SOCK_RAW,     &inetdomain,    IPPROTO_IGMP,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
179   igmp_input,   0,              0,              rip_ctloutput,
180   cpu0_soport,  NULL,
181   igmp_init,    igmp_fasttimo,  igmp_slowtimo,  0,
182   &rip_usrreqs
183 },
184 { SOCK_RAW,     &inetdomain,    IPPROTO_RSVP,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
185   rsvp_input,   0,              0,              rip_ctloutput,
186   cpu0_soport,  NULL,
187   0,            0,              0,              0,
188   &rip_usrreqs
189 },
190 #ifdef IPSEC
191 { SOCK_RAW,     &inetdomain,    IPPROTO_AH,     PR_ATOMIC|PR_ADDR,
192   ah4_input,    0,              0,              0,
193   cpu0_soport,  NULL,
194   0,            0,              0,              0,
195   &nousrreqs
196 },
197 #ifdef IPSEC_ESP
198 { SOCK_RAW,     &inetdomain,    IPPROTO_ESP,    PR_ATOMIC|PR_ADDR,
199   esp4_input,   0,              0,              0,
200   cpu0_soport,  NULL,
201   0,            0,              0,              0,
202   &nousrreqs
203 },
204 #endif
205 { SOCK_RAW,     &inetdomain,    IPPROTO_IPCOMP, PR_ATOMIC|PR_ADDR,
206   ipcomp4_input, 0,             0,              0,
207   cpu0_soport,  NULL,
208   0,            0,              0,              0,
209   &nousrreqs
210 },
211 #endif /* IPSEC */
212 #ifdef FAST_IPSEC
213 { SOCK_RAW,     &inetdomain,    IPPROTO_AH,     PR_ATOMIC|PR_ADDR,
214   ipsec4_common_input,  0,      0,              0,
215   cpu0_soport,  NULL,
216   0,            0,              0,              0,
217   &nousrreqs
218 },
219 { SOCK_RAW,     &inetdomain,    IPPROTO_ESP,    PR_ATOMIC|PR_ADDR,
220   ipsec4_common_input,  0,      0,              0,
221   cpu0_soport,  NULL,
222   0,            0,              0,              0,
223   &nousrreqs
224 },
225 { SOCK_RAW,     &inetdomain,    IPPROTO_IPCOMP, PR_ATOMIC|PR_ADDR,
226   ipsec4_common_input,  0,      0,              0,
227   cpu0_soport,  NULL,
228   0,            0,              0,              0,
229   &nousrreqs
230 },
231 #endif /* FAST_IPSEC */
232 { SOCK_RAW,     &inetdomain,    IPPROTO_IPV4,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
233   encap4_input, 0,              0,              rip_ctloutput,
234   cpu0_soport,  NULL,
235   encap_init,   0,              0,              0,
236   &rip_usrreqs
237 },
238 { SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
239   encap4_input, 0,              0,              rip_ctloutput,
240   cpu0_soport,  NULL,
241   encap_init,   0,              0,              0,
242   &rip_usrreqs
243 },
244 { SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
245   encap4_input, 0,              0,              rip_ctloutput,
246   cpu0_soport,  NULL,
247   encap_init,   0,              0,              0,
248   &rip_usrreqs
249 },
250 # ifdef INET6
251 { SOCK_RAW,     &inetdomain,    IPPROTO_IPV6,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
252   encap4_input, 0,              0,              rip_ctloutput,
253   cpu0_soport,  NULL,
254   encap_init,   0,              0,              0,
255   &rip_usrreqs
256 },
257 #endif
258 #ifdef IPDIVERT
259 { SOCK_RAW,     &inetdomain,    IPPROTO_DIVERT, PR_ATOMIC|PR_ADDR,
260   div_input,    0,              0,              ip_ctloutput,
261   div_soport,   NULL,
262   div_init,     0,              0,              0,
263   &div_usrreqs,
264 },
265 #endif
266 #ifdef IPXIP
267 { SOCK_RAW,     &inetdomain,    IPPROTO_IDP,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
268   ipxip_input,  0,              ipxip_ctlinput, 0,
269   cpu0_soport,  cpu0_ctlport,
270   0,            0,              0,              0,
271   &rip_usrreqs
272 },
273 #endif
274 #ifdef NSIP
275 { SOCK_RAW,     &inetdomain,    IPPROTO_IDP,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
276   idpip_input,  0,              nsip_ctlinput,  0,
277   cpu0_soport,  cpu0_ctlport,
278   0,            0,              0,              0,
279   &rip_usrreqs
280 },
281 #endif
282 #ifdef PIM
283 { SOCK_RAW,     &inetdomain,    IPPROTO_PIM,    PR_ATOMIC|PR_ADDR|PR_LASTHDR,
284   pim_input,    0,              0,              rip_ctloutput,
285   cpu0_soport,  NULL,
286   0,            0,              0,              0,
287   &rip_usrreqs
288 },
289 #endif
290 #ifdef NPFSYNC
291 { SOCK_RAW,     &inetdomain,    IPPROTO_PFSYNC, PR_ATOMIC|PR_ADDR,
292   pfsync_input, 0,              0,              rip_ctloutput,
293   cpu0_soport,  NULL,
294   0,            0,              0,              0,
295   &rip_usrreqs
296 },
297 #endif  /* NPFSYNC */
298         /* raw wildcard */
299 { SOCK_RAW,     &inetdomain,    0,              PR_ATOMIC|PR_ADDR,
300   rip_input,    0,              0,              rip_ctloutput,
301   cpu0_soport,  NULL,
302   rip_init,     0,              0,              0,
303   &rip_usrreqs
304 },
305
306 #ifdef CARP
307 { SOCK_RAW,     &inetdomain,    IPPROTO_CARP,   PR_ATOMIC|PR_ADDR,
308   carp_input,   rip_output,     0,              rip_ctloutput,
309   cpu0_soport,  NULL,
310   0,            0,              0,              0,
311   &rip_usrreqs
312 },
313
314 #endif
315 };
316
317 struct domain inetdomain = {
318         AF_INET, "internet", NULL, NULL, NULL,
319         inetsw, &inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
320         SLIST_ENTRY_INITIALIZER,
321         in_inithead, 32, sizeof(struct sockaddr_in),
322 };
323
324 DOMAIN_SET(inet);
325
326 SYSCTL_NODE(_net,      PF_INET,         inet,   CTLFLAG_RW, 0,
327         "Internet Family");
328
329 SYSCTL_NODE(_net_inet, IPPROTO_IP,      ip,     CTLFLAG_RW, 0,  "IP");
330 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,    icmp,   CTLFLAG_RW, 0,  "ICMP");
331 SYSCTL_NODE(_net_inet, IPPROTO_UDP,     udp,    CTLFLAG_RW, 0,  "UDP");
332 SYSCTL_NODE(_net_inet, IPPROTO_TCP,     tcp,    CTLFLAG_RW, 0,  "TCP");
333 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,    igmp,   CTLFLAG_RW, 0,  "IGMP");
334 #ifdef FAST_IPSEC
335 /* XXX no protocol # to use, pick something "reserved" */
336 SYSCTL_NODE(_net_inet, 253,             ipsec,  CTLFLAG_RW, 0,  "IPSEC");
337 SYSCTL_NODE(_net_inet, IPPROTO_AH,      ah,     CTLFLAG_RW, 0,  "AH");
338 SYSCTL_NODE(_net_inet, IPPROTO_ESP,     esp,    CTLFLAG_RW, 0,  "ESP");
339 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,  ipcomp, CTLFLAG_RW, 0,  "IPCOMP");
340 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,    ipip,   CTLFLAG_RW, 0,  "IPIP");
341 #else
342 #ifdef IPSEC
343 SYSCTL_NODE(_net_inet, IPPROTO_AH,      ipsec,  CTLFLAG_RW, 0,  "IPSEC");
344 #endif /* IPSEC */
345 #endif /* !FAST_IPSEC */
346 SYSCTL_NODE(_net_inet, IPPROTO_RAW,     raw,    CTLFLAG_RW, 0,  "RAW");
347 #ifdef IPDIVERT
348 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT,  divert, CTLFLAG_RW, 0,  "DIVERT");
349 #endif
350 #ifdef PIM
351 SYSCTL_NODE(_net_inet, IPPROTO_PIM,    pim,    CTLFLAG_RW, 0,  "PIM");
352 #endif
353 #ifdef CARP
354 SYSCTL_NODE(_net_inet, IPPROTO_CARP,    carp,    CTLFLAG_RW, 0,  "CARP");
355 #endif