]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_proto.c
Import to 0.6.1
[FreeBSD/FreeBSD.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  * 4. 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  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_mrouting.h"
36 #include "opt_ipsec.h"
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_sctp.h"
40 #include "opt_mpath.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/domain.h>
48 #include <sys/proc.h>
49 #include <sys/protosw.h>
50 #include <sys/queue.h>
51 #include <sys/sysctl.h>
52
53 /*
54  * While this file provides the domain and protocol switch tables for IPv4, it
55  * also provides the sysctl node declarations for net.inet.* often shared with
56  * IPv6 for common features or by upper layer protocols.  In case of no IPv4
57  * support compile out everything but these sysctl nodes.
58  */
59 #ifdef INET
60 #include <net/if.h>
61 #include <net/if_var.h>
62 #include <net/route.h>
63 #ifdef RADIX_MPATH
64 #include <net/radix_mpath.h>
65 #endif
66 #include <net/vnet.h>
67 #endif /* INET */
68
69 #if defined(INET) || defined(INET6)
70 #include <netinet/in.h>
71 #endif
72
73 #ifdef INET
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_icmp.h>
79 #include <netinet/igmp_var.h>
80 #include <netinet/tcp.h>
81 #include <netinet/tcp_timer.h>
82 #include <netinet/tcp_var.h>
83 #include <netinet/udp.h>
84 #include <netinet/udp_var.h>
85 #include <netinet/ip_encap.h>
86
87 /*
88  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
89  */
90
91 static struct pr_usrreqs nousrreqs;
92
93 #ifdef IPSEC
94 #include <netipsec/ipsec.h>
95 #endif /* IPSEC */
96
97 #ifdef SCTP
98 #include <netinet/in_pcb.h>
99 #include <netinet/sctp_pcb.h>
100 #include <netinet/sctp.h>
101 #include <netinet/sctp_var.h>
102 #endif /* SCTP */
103
104 FEATURE(inet, "Internet Protocol version 4");
105
106 extern  struct domain inetdomain;
107
108 /* Spacer for loadable protocols. */
109 #define IPPROTOSPACER                           \
110 {                                               \
111         .pr_domain =            &inetdomain,    \
112         .pr_protocol =          PROTO_SPACER,   \
113         .pr_usrreqs =           &nousrreqs      \
114 }
115
116 struct protosw inetsw[] = {
117 {
118         .pr_type =              0,
119         .pr_domain =            &inetdomain,
120         .pr_protocol =          IPPROTO_IP,
121         .pr_init =              ip_init,
122 #ifdef VIMAGE
123         .pr_destroy =           ip_destroy,
124 #endif
125         .pr_slowtimo =          ip_slowtimo,
126         .pr_drain =             ip_drain,
127         .pr_usrreqs =           &nousrreqs
128 },
129 {
130         .pr_type =              SOCK_DGRAM,
131         .pr_domain =            &inetdomain,
132         .pr_protocol =          IPPROTO_UDP,
133         .pr_flags =             PR_ATOMIC|PR_ADDR,
134         .pr_input =             udp_input,
135         .pr_ctlinput =          udp_ctlinput,
136         .pr_ctloutput =         udp_ctloutput,
137         .pr_init =              udp_init,
138 #ifdef VIMAGE
139         .pr_destroy =           udp_destroy,
140 #endif
141         .pr_usrreqs =           &udp_usrreqs
142 },
143 {
144         .pr_type =              SOCK_STREAM,
145         .pr_domain =            &inetdomain,
146         .pr_protocol =          IPPROTO_TCP,
147         .pr_flags =             PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
148         .pr_input =             tcp_input,
149         .pr_ctlinput =          tcp_ctlinput,
150         .pr_ctloutput =         tcp_ctloutput,
151         .pr_init =              tcp_init,
152 #ifdef VIMAGE
153         .pr_destroy =           tcp_destroy,
154 #endif
155         .pr_slowtimo =          tcp_slowtimo,
156         .pr_drain =             tcp_drain,
157         .pr_usrreqs =           &tcp_usrreqs
158 },
159 #ifdef SCTP
160
161         .pr_type =              SOCK_SEQPACKET,
162         .pr_domain =            &inetdomain,
163         .pr_protocol =          IPPROTO_SCTP,
164         .pr_flags =             PR_WANTRCVD,
165         .pr_input =             sctp_input,
166         .pr_ctlinput =          sctp_ctlinput,
167         .pr_ctloutput =         sctp_ctloutput,
168         .pr_init =              sctp_init,
169 #ifdef VIMAGE
170         .pr_destroy =           sctp_finish,
171 #endif
172         .pr_drain =             sctp_drain,
173         .pr_usrreqs =           &sctp_usrreqs
174 },
175
176         .pr_type =              SOCK_STREAM,
177         .pr_domain =            &inetdomain,
178         .pr_protocol =          IPPROTO_SCTP,
179         .pr_flags =             PR_WANTRCVD,
180         .pr_input =             sctp_input,
181         .pr_ctlinput =          sctp_ctlinput,
182         .pr_ctloutput =         sctp_ctloutput,
183         .pr_drain =             sctp_drain,
184         .pr_usrreqs =           &sctp_usrreqs
185 },
186 #endif /* SCTP */
187 {
188         .pr_type =              SOCK_DGRAM,
189         .pr_domain =            &inetdomain,
190         .pr_protocol =          IPPROTO_UDPLITE,
191         .pr_flags =             PR_ATOMIC|PR_ADDR,
192         .pr_input =             udp_input,
193         .pr_ctlinput =          udplite_ctlinput,
194         .pr_ctloutput =         udp_ctloutput,
195         .pr_init =              udplite_init,
196 #ifdef VIMAGE
197         .pr_destroy =           udplite_destroy,
198 #endif
199         .pr_usrreqs =           &udp_usrreqs
200 },
201 {
202         .pr_type =              SOCK_RAW,
203         .pr_domain =            &inetdomain,
204         .pr_protocol =          IPPROTO_RAW,
205         .pr_flags =             PR_ATOMIC|PR_ADDR,
206         .pr_input =             rip_input,
207         .pr_ctlinput =          rip_ctlinput,
208         .pr_ctloutput =         rip_ctloutput,
209         .pr_usrreqs =           &rip_usrreqs
210 },
211 {
212         .pr_type =              SOCK_RAW,
213         .pr_domain =            &inetdomain,
214         .pr_protocol =          IPPROTO_ICMP,
215         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
216         .pr_input =             icmp_input,
217         .pr_ctloutput =         rip_ctloutput,
218         .pr_usrreqs =           &rip_usrreqs
219 },
220 {
221         .pr_type =              SOCK_RAW,
222         .pr_domain =            &inetdomain,
223         .pr_protocol =          IPPROTO_IGMP,
224         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
225         .pr_input =             igmp_input,
226         .pr_ctloutput =         rip_ctloutput,
227         .pr_fasttimo =          igmp_fasttimo,
228         .pr_slowtimo =          igmp_slowtimo,
229         .pr_usrreqs =           &rip_usrreqs
230 },
231 {
232         .pr_type =              SOCK_RAW,
233         .pr_domain =            &inetdomain,
234         .pr_protocol =          IPPROTO_RSVP,
235         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
236         .pr_input =             rsvp_input,
237         .pr_ctloutput =         rip_ctloutput,
238         .pr_usrreqs =           &rip_usrreqs
239 },
240 #ifdef IPSEC
241 {
242         .pr_type =              SOCK_RAW,
243         .pr_domain =            &inetdomain,
244         .pr_protocol =          IPPROTO_AH,
245         .pr_flags =             PR_ATOMIC|PR_ADDR,
246         .pr_input =             ah4_input,
247         .pr_ctlinput =          ah4_ctlinput,
248         .pr_usrreqs =           &nousrreqs
249 },
250 {
251         .pr_type =              SOCK_RAW,
252         .pr_domain =            &inetdomain,
253         .pr_protocol =          IPPROTO_ESP,
254         .pr_flags =             PR_ATOMIC|PR_ADDR,
255         .pr_input =             esp4_input,
256         .pr_ctlinput =          esp4_ctlinput,
257         .pr_usrreqs =           &nousrreqs
258 },
259 {
260         .pr_type =              SOCK_RAW,
261         .pr_domain =            &inetdomain,
262         .pr_protocol =          IPPROTO_IPCOMP,
263         .pr_flags =             PR_ATOMIC|PR_ADDR,
264         .pr_input =             ipcomp4_input,
265         .pr_usrreqs =           &nousrreqs
266 },
267 #endif /* IPSEC */
268 {
269         .pr_type =              SOCK_RAW,
270         .pr_domain =            &inetdomain,
271         .pr_protocol =          IPPROTO_IPV4,
272         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
273         .pr_input =             encap4_input,
274         .pr_ctloutput =         rip_ctloutput,
275         .pr_init =              encap_init,
276         .pr_usrreqs =           &rip_usrreqs
277 },
278 {
279         .pr_type =              SOCK_RAW,
280         .pr_domain =            &inetdomain,
281         .pr_protocol =          IPPROTO_MOBILE,
282         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
283         .pr_input =             encap4_input,
284         .pr_ctloutput =         rip_ctloutput,
285         .pr_init =              encap_init,
286         .pr_usrreqs =           &rip_usrreqs
287 },
288 {
289         .pr_type =              SOCK_RAW,
290         .pr_domain =            &inetdomain,
291         .pr_protocol =          IPPROTO_ETHERIP,
292         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
293         .pr_input =             encap4_input,
294         .pr_ctloutput =         rip_ctloutput,
295         .pr_init =              encap_init,
296         .pr_usrreqs =           &rip_usrreqs
297 },
298 {
299         .pr_type =              SOCK_RAW,
300         .pr_domain =            &inetdomain,
301         .pr_protocol =          IPPROTO_GRE,
302         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
303         .pr_input =             encap4_input,
304         .pr_ctloutput =         rip_ctloutput,
305         .pr_init =              encap_init,
306         .pr_usrreqs =           &rip_usrreqs
307 },
308 # ifdef INET6
309 {
310         .pr_type =              SOCK_RAW,
311         .pr_domain =            &inetdomain,
312         .pr_protocol =          IPPROTO_IPV6,
313         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
314         .pr_input =             encap4_input,
315         .pr_ctloutput =         rip_ctloutput,
316         .pr_init =              encap_init,
317         .pr_usrreqs =           &rip_usrreqs
318 },
319 #endif
320 {
321         .pr_type =              SOCK_RAW,
322         .pr_domain =            &inetdomain,
323         .pr_protocol =          IPPROTO_PIM,
324         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_LASTHDR,
325         .pr_input =             encap4_input,
326         .pr_ctloutput =         rip_ctloutput,
327         .pr_usrreqs =           &rip_usrreqs
328 },
329 /* Spacer n-times for loadable protocols. */
330 IPPROTOSPACER,
331 IPPROTOSPACER,
332 IPPROTOSPACER,
333 IPPROTOSPACER,
334 IPPROTOSPACER,
335 IPPROTOSPACER,
336 IPPROTOSPACER,
337 IPPROTOSPACER,
338 /* raw wildcard */
339 {
340         .pr_type =              SOCK_RAW,
341         .pr_domain =            &inetdomain,
342         .pr_flags =             PR_ATOMIC|PR_ADDR,
343         .pr_input =             rip_input,
344         .pr_ctloutput =         rip_ctloutput,
345         .pr_init =              rip_init,
346 #ifdef VIMAGE
347         .pr_destroy =           rip_destroy,
348 #endif
349         .pr_usrreqs =           &rip_usrreqs
350 },
351 };
352
353 extern int in_inithead(void **, int);
354 extern int in_detachhead(void **, int);
355
356 struct domain inetdomain = {
357         .dom_family =           AF_INET,
358         .dom_name =             "internet",
359         .dom_protosw =          inetsw,
360         .dom_protoswNPROTOSW =  &inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
361 #ifdef RADIX_MPATH
362         .dom_rtattach =         rn4_mpath_inithead,
363 #else
364         .dom_rtattach =         in_inithead,
365 #endif
366 #ifdef VIMAGE
367         .dom_rtdetach =         in_detachhead,
368 #endif
369         .dom_ifattach =         in_domifattach,
370         .dom_ifdetach =         in_domifdetach
371 };
372
373 VNET_DOMAIN_SET(inet);
374 #endif /* INET */
375
376 SYSCTL_NODE(_net,      PF_INET,         inet,   CTLFLAG_RW, 0,
377         "Internet Family");
378
379 SYSCTL_NODE(_net_inet, IPPROTO_IP,      ip,     CTLFLAG_RW, 0,  "IP");
380 SYSCTL_NODE(_net_inet, IPPROTO_ICMP,    icmp,   CTLFLAG_RW, 0,  "ICMP");
381 SYSCTL_NODE(_net_inet, IPPROTO_UDP,     udp,    CTLFLAG_RW, 0,  "UDP");
382 SYSCTL_NODE(_net_inet, IPPROTO_TCP,     tcp,    CTLFLAG_RW, 0,  "TCP");
383 #ifdef SCTP
384 SYSCTL_NODE(_net_inet, IPPROTO_SCTP,    sctp,   CTLFLAG_RW, 0,  "SCTP");
385 #endif
386 SYSCTL_NODE(_net_inet, IPPROTO_IGMP,    igmp,   CTLFLAG_RW, 0,  "IGMP");
387 #ifdef IPSEC
388 /* XXX no protocol # to use, pick something "reserved" */
389 SYSCTL_NODE(_net_inet, 253,             ipsec,  CTLFLAG_RW, 0,  "IPSEC");
390 SYSCTL_NODE(_net_inet, IPPROTO_AH,      ah,     CTLFLAG_RW, 0,  "AH");
391 SYSCTL_NODE(_net_inet, IPPROTO_ESP,     esp,    CTLFLAG_RW, 0,  "ESP");
392 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,  ipcomp, CTLFLAG_RW, 0,  "IPCOMP");
393 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,    ipip,   CTLFLAG_RW, 0,  "IPIP");
394 #endif /* IPSEC */
395 SYSCTL_NODE(_net_inet, IPPROTO_RAW,     raw,    CTLFLAG_RW, 0,  "RAW");
396 SYSCTL_NODE(_net_inet, OID_AUTO,        accf,   CTLFLAG_RW, 0,
397     "Accept filters");