in_proto.c revision 1.83 1 /* $NetBSD: in_proto.c,v 1.83 2007/03/05 00:50:53 liamjfoy Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in_proto.c 8.2 (Berkeley) 2/9/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: in_proto.c,v 1.83 2007/03/05 00:50:53 liamjfoy Exp $");
65
66 #include "opt_mrouting.h"
67 #include "opt_eon.h" /* ISO CLNL over IP */
68 #include "opt_iso.h" /* ISO TP tunneled over IP */
69 #include "opt_inet.h"
70 #include "opt_ipsec.h"
71 #include "opt_pim.h"
72
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/protosw.h>
76 #include <sys/domain.h>
77 #include <sys/mbuf.h>
78
79 #include <net/if.h>
80 #include <net/radix.h>
81 #include <net/route.h>
82
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/ip_icmp.h>
88 #include <netinet/in_ifattach.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_proto.h>
91 #include <netinet/in_route.h>
92
93 #ifdef INET6
94 #ifndef INET
95 #include <netinet/in.h>
96 #endif
97 #include <netinet/ip6.h>
98 #endif
99
100 #include <netinet/igmp_var.h>
101 #ifdef PIM
102 #include <netinet/pim_var.h>
103 #endif
104 #include <netinet/tcp.h>
105 #include <netinet/tcp_fsm.h>
106 #include <netinet/tcp_seq.h>
107 #include <netinet/tcp_timer.h>
108 #include <netinet/tcp_var.h>
109 #include <netinet/tcpip.h>
110 #include <netinet/tcp_debug.h>
111 #include <netinet/udp.h>
112 #include <netinet/udp_var.h>
113 #include <netinet/ip_encap.h>
114
115 /*
116 * TCP/IP protocol family: IP, ICMP, UDP, TCP.
117 */
118
119 #ifdef IPSEC
120 #include <netinet6/ipsec.h>
121 #include <netinet6/ah.h>
122 #ifdef IPSEC_ESP
123 #include <netinet6/esp.h>
124 #endif
125 #include <netinet6/ipcomp.h>
126 #endif /* IPSEC */
127
128 #ifdef FAST_IPSEC
129 #include <netipsec/ipsec.h>
130 #include <netipsec/key.h>
131 #endif /* FAST_IPSEC */
132
133 #ifdef TPIP
134 #include <netiso/tp_param.h>
135 #include <netiso/tp_var.h>
136 #endif /* TPIP */
137
138 #ifdef EON
139 #include <netiso/eonvar.h>
140 #endif /* EON */
141
142 #include "gre.h"
143 #if NGRE > 0
144 #include <netinet/ip_gre.h>
145 #endif
146
147 #include "carp.h"
148 #if NCARP > 0
149 #include <netinet/ip_carp.h>
150 #endif
151
152 #include "etherip.h"
153 #if NETHERIP > 0
154 #include <netinet/ip_etherip.h>
155 #endif
156
157 DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */
158
159 const struct protosw inetsw[] = {
160 { .pr_domain = &inetdomain,
161 .pr_init = ip_init,
162 .pr_output = ip_output,
163 .pr_slowtimo = ip_slowtimo,
164 .pr_drain = ip_drain,
165 },
166 { .pr_type = SOCK_DGRAM,
167 .pr_domain = &inetdomain,
168 .pr_protocol = IPPROTO_UDP,
169 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
170 .pr_input = udp_input,
171 .pr_ctlinput = udp_ctlinput,
172 .pr_ctloutput = udp_ctloutput,
173 .pr_usrreq = udp_usrreq,
174 .pr_init = udp_init,
175 },
176 { .pr_type = SOCK_STREAM,
177 .pr_domain = &inetdomain,
178 .pr_protocol = IPPROTO_TCP,
179 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
180 .pr_input = tcp_input,
181 .pr_ctlinput = tcp_ctlinput,
182 .pr_ctloutput = tcp_ctloutput,
183 .pr_usrreq = tcp_usrreq,
184 .pr_init = tcp_init,
185 .pr_slowtimo = tcp_slowtimo,
186 .pr_drain = tcp_drain,
187 },
188 { .pr_type = SOCK_RAW,
189 .pr_domain = &inetdomain,
190 .pr_protocol = IPPROTO_RAW,
191 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
192 .pr_input = rip_input,
193 .pr_output = rip_output,
194 .pr_ctlinput = rip_ctlinput,
195 .pr_ctloutput = rip_ctloutput,
196 .pr_usrreq = rip_usrreq,
197 },
198 { .pr_type = SOCK_RAW,
199 .pr_domain = &inetdomain,
200 .pr_protocol = IPPROTO_ICMP,
201 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
202 .pr_input = icmp_input,
203 .pr_output = rip_output,
204 .pr_ctlinput = rip_ctlinput,
205 .pr_ctloutput = rip_ctloutput,
206 .pr_usrreq = rip_usrreq,
207 .pr_init = icmp_init,
208 },
209 #ifdef GATEWAY
210 { .pr_domain = &inetdomain,
211 .pr_protocol = IPPROTO_IP,
212 .pr_slowtimo = ipflow_slowtimo,
213 },
214 #endif /* GATEWAY */
215 #ifdef IPSEC
216 { .pr_type = SOCK_RAW,
217 .pr_domain = &inetdomain,
218 .pr_protocol = IPPROTO_AH,
219 .pr_flags = PR_ATOMIC|PR_ADDR,
220 .pr_input = ah4_input,
221 .pr_ctlinput = ah4_ctlinput,
222 },
223 #ifdef IPSEC_ESP
224 { .pr_type = SOCK_RAW,
225 .pr_domain = &inetdomain,
226 .pr_protocol = IPPROTO_ESP,
227 .pr_flags = PR_ATOMIC|PR_ADDR,
228 .pr_input = esp4_input,
229 .pr_ctlinput = esp4_ctlinput,
230 },
231 #endif /* IPSEC_ESP */
232 { .pr_type = SOCK_RAW,
233 .pr_domain = &inetdomain,
234 .pr_protocol = IPPROTO_IPCOMP,
235 .pr_flags = PR_ATOMIC|PR_ADDR,
236 .pr_input = ipcomp4_input,
237 },
238 #endif /* IPSEC */
239 #ifdef FAST_IPSEC
240 { .pr_type = SOCK_RAW,
241 .pr_domain = &inetdomain,
242 .pr_protocol = IPPROTO_AH,
243 .pr_flags = PR_ATOMIC|PR_ADDR,
244 .pr_input = ipsec4_common_input,
245 .pr_ctlinput = ah4_ctlinput,
246 },
247 { .pr_type = SOCK_RAW,
248 .pr_domain = &inetdomain,
249 .pr_protocol = IPPROTO_ESP,
250 .pr_flags = PR_ATOMIC|PR_ADDR,
251 .pr_input = ipsec4_common_input,
252 .pr_ctlinput = esp4_ctlinput,
253 },
254 { .pr_type = SOCK_RAW,
255 .pr_domain = &inetdomain,
256 .pr_protocol = IPPROTO_IPCOMP,
257 .pr_flags = PR_ATOMIC|PR_ADDR,
258 .pr_input = ipsec4_common_input,
259 },
260 #endif /* FAST_IPSEC */
261 { .pr_type = SOCK_RAW,
262 .pr_domain = &inetdomain,
263 .pr_protocol = IPPROTO_IPV4,
264 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
265 .pr_input = encap4_input,
266 .pr_output = rip_output,
267 .pr_ctlinput = rip_ctlinput,
268 .pr_ctloutput = rip_ctloutput,
269 .pr_usrreq = rip_usrreq,
270 .pr_init = encap_init,
271 },
272 #ifdef INET6
273 { .pr_type = SOCK_RAW,
274 .pr_domain = &inetdomain,
275 .pr_protocol = IPPROTO_IPV6,
276 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
277 .pr_input = encap4_input,
278 .pr_output = rip_output,
279 .pr_ctlinput = rip_ctlinput,
280 .pr_ctloutput = rip_ctloutput,
281 .pr_usrreq = rip_usrreq,
282 .pr_init = encap_init,
283 },
284 #endif /* INET6 */
285 #if NETHERIP > 0
286 { .pr_type = SOCK_RAW,
287 .pr_domain = &inetdomain,
288 .pr_protocol = IPPROTO_ETHERIP,
289 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
290 .pr_input = ip_etherip_input,
291 .pr_output = rip_output,
292 .pr_ctlinput = rip_ctlinput,
293 .pr_ctloutput = rip_ctloutput,
294 .pr_usrreq = rip_usrreq,
295 },
296 #endif /* NETHERIP > 0 */
297 #if NCARP > 0
298 { .pr_type = SOCK_RAW,
299 .pr_domain = &inetdomain,
300 .pr_protocol = IPPROTO_CARP,
301 .pr_flags = PR_ATOMIC|PR_ADDR,
302 .pr_input = carp_proto_input,
303 .pr_output = rip_output,
304 .pr_ctloutput = rip_ctloutput,
305 .pr_usrreq = rip_usrreq,
306 },
307 #endif /* NCARP > 0 */
308 #if NGRE > 0
309 { .pr_type = SOCK_RAW,
310 .pr_domain = &inetdomain,
311 .pr_protocol = IPPROTO_GRE,
312 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
313 .pr_input = gre_input,
314 .pr_output = rip_output,
315 .pr_ctloutput = rip_ctloutput,
316 .pr_ctlinput = rip_ctlinput,
317 .pr_usrreq = rip_usrreq,
318 },
319 { .pr_type = SOCK_RAW,
320 .pr_domain = &inetdomain,
321 .pr_protocol = IPPROTO_MOBILE,
322 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
323 .pr_input = gre_mobile_input,
324 .pr_output = rip_output,
325 .pr_ctloutput = rip_ctloutput,
326 .pr_ctlinput = rip_ctlinput,
327 .pr_usrreq = rip_usrreq,
328 },
329 #endif /* NGRE > 0 */
330 { .pr_type = SOCK_RAW,
331 .pr_domain = &inetdomain,
332 .pr_protocol = IPPROTO_IGMP,
333 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
334 .pr_input = igmp_input,
335 .pr_output = rip_output,
336 .pr_ctloutput = rip_ctloutput,
337 .pr_ctlinput = rip_ctlinput,
338 .pr_usrreq = rip_usrreq,
339 .pr_fasttimo = igmp_fasttimo,
340 .pr_slowtimo = igmp_slowtimo,
341 },
342 #ifdef PIM
343 { .pr_type = SOCK_RAW,
344 .pr_domain = &inetdomain,
345 .pr_protocol = IPPROTO_PIM,
346 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
347 .pr_input = pim_input,
348 .pr_output = rip_output,
349 .pr_ctloutput = rip_ctloutput,
350 .pr_ctlinput = rip_ctlinput,
351 .pr_usrreq = rip_usrreq,
352 },
353 #endif /* PIM */
354 #ifdef TPIP
355 { .pr_type = SOCK_SEQPACKET,
356 .pr_domain = &inetdomain,
357 .pr_protocol = IPPROTO_TP,
358 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_LASTHDR|PR_ABRTACPTDIS,
359 .pr_input = tpip_input,
360 .pr_ctloutput = tp_ctloutput,
361 .pr_ctlinput = tpip_ctlinput,
362 .pr_usrreq = tp_usrreq,
363 .pr_init = tp_init,
364 .pr_slowtimo = tp_slowtimo,
365 .pr_drain = tp_drain,
366 },
367 #endif /* TPIP */
368 #ifdef ISO
369 /* EON (ISO CLNL over IP) */
370 #ifdef EON
371 { .pr_type = SOCK_RAW,
372 .pr_domain = &inetdomain,
373 .pr_protocol = IPPROTO_EON,
374 .pr_flags = PR_LASTHDR,
375 .pr_input = eoninput,
376 .pr_ctlinput = eonctlinput,
377 .pr_init = eonprotoinit,
378 },
379 #else
380 { .pr_type = SOCK_RAW,
381 .pr_domain = &inetdomain,
382 .pr_protocol = IPPROTO_EON,
383 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
384 .pr_input = encap4_input,
385 .pr_output = rip_output,
386 .pr_ctloutput = rip_ctloutput,
387 .pr_ctlinput = rip_ctlinput,
388 .pr_usrreq = rip_usrreq,
389 .pr_init = encap_init,
390 },
391 #endif /* EON */
392 #endif /* ISO */
393 /* raw wildcard */
394 { .pr_type = SOCK_RAW,
395 .pr_domain = &inetdomain,
396 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
397 .pr_input = rip_input,
398 .pr_output = rip_output,
399 .pr_ctloutput = rip_ctloutput,
400 .pr_ctlinput = rip_ctlinput,
401 .pr_usrreq = rip_usrreq,
402 .pr_init = rip_init,
403 },
404 };
405
406 extern struct ifqueue ipintrq;
407
408 struct domain inetdomain = {
409 .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
410 .dom_externalize = NULL, .dom_dispose = NULL,
411 .dom_protosw = inetsw,
412 .dom_protoswNPROTOSW = &inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
413 .dom_rtattach = rn_inithead,
414 .dom_rtoffset = 32, .dom_maxrtkey = sizeof(struct sockaddr_in),
415 #ifdef IPSELSRC
416 .dom_ifattach = in_domifattach,
417 .dom_ifdetach = in_domifdetach,
418 #else
419 .dom_ifattach = NULL,
420 .dom_ifdetach = NULL,
421 #endif
422 .dom_ifqueues = { &ipintrq, NULL },
423 .dom_link = { NULL },
424 .dom_mowner = MOWNER_INIT("",""),
425 .dom_rtcache = in_rtcache,
426 .dom_rtflush = in_rtflush,
427 .dom_rtflushall = in_rtflushall
428 };
429
430 u_char ip_protox[IPPROTO_MAX];
431
432 int icmperrppslim = 100; /* 100pps */
433