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