in_proto.c revision 1.87 1 /* $NetBSD: in_proto.c,v 1.87 2007/06/13 21:08:29 dyoung 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.87 2007/06/13 21:08:29 dyoung 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
92 #ifdef INET6
93 #ifndef INET
94 #include <netinet/in.h>
95 #endif
96 #include <netinet/ip6.h>
97 #endif
98
99 #include <netinet/igmp_var.h>
100 #ifdef PIM
101 #include <netinet/pim_var.h>
102 #endif
103 #include <netinet/tcp.h>
104 #include <netinet/tcp_fsm.h>
105 #include <netinet/tcp_seq.h>
106 #include <netinet/tcp_timer.h>
107 #include <netinet/tcp_var.h>
108 #include <netinet/tcpip.h>
109 #include <netinet/tcp_debug.h>
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
112 #include <netinet/ip_encap.h>
113
114 /*
115 * TCP/IP protocol family: IP, ICMP, UDP, TCP.
116 */
117
118 #ifdef IPSEC
119 #include <netinet6/ipsec.h>
120 #include <netinet6/ah.h>
121 #ifdef IPSEC_ESP
122 #include <netinet6/esp.h>
123 #endif
124 #include <netinet6/ipcomp.h>
125 #endif /* IPSEC */
126
127 #ifdef FAST_IPSEC
128 #include <netipsec/ipsec.h>
129 #include <netipsec/key.h>
130 #endif /* FAST_IPSEC */
131
132 #ifdef TPIP
133 #include <netiso/tp_param.h>
134 #include <netiso/tp_var.h>
135 #endif /* TPIP */
136
137 #ifdef EON
138 #include <netiso/eonvar.h>
139 #endif /* EON */
140
141 #include "gre.h"
142 #if NGRE > 0
143 #include <netinet/ip_gre.h>
144 #endif
145
146 #include "carp.h"
147 #if NCARP > 0
148 #include <netinet/ip_carp.h>
149 #endif
150
151 #include "etherip.h"
152 #if NETHERIP > 0
153 #include <netinet/ip_etherip.h>
154 #endif
155
156 DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */
157
158 const struct protosw inetsw[] = {
159 { .pr_domain = &inetdomain,
160 .pr_init = ip_init,
161 .pr_output = ip_output,
162 .pr_slowtimo = ip_slowtimo,
163 .pr_drain = ip_drain,
164 },
165 { .pr_type = SOCK_DGRAM,
166 .pr_domain = &inetdomain,
167 .pr_protocol = IPPROTO_UDP,
168 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
169 .pr_input = udp_input,
170 .pr_ctlinput = udp_ctlinput,
171 .pr_ctloutput = udp_ctloutput,
172 .pr_usrreq = udp_usrreq,
173 .pr_init = udp_init,
174 },
175 { .pr_type = SOCK_STREAM,
176 .pr_domain = &inetdomain,
177 .pr_protocol = IPPROTO_TCP,
178 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
179 .pr_input = tcp_input,
180 .pr_ctlinput = tcp_ctlinput,
181 .pr_ctloutput = tcp_ctloutput,
182 .pr_usrreq = tcp_usrreq,
183 .pr_init = tcp_init,
184 .pr_slowtimo = tcp_slowtimo,
185 .pr_drain = tcp_drain,
186 },
187 { .pr_type = SOCK_RAW,
188 .pr_domain = &inetdomain,
189 .pr_protocol = IPPROTO_RAW,
190 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
191 .pr_input = rip_input,
192 .pr_output = rip_output,
193 .pr_ctlinput = rip_ctlinput,
194 .pr_ctloutput = rip_ctloutput,
195 .pr_usrreq = rip_usrreq,
196 },
197 { .pr_type = SOCK_RAW,
198 .pr_domain = &inetdomain,
199 .pr_protocol = IPPROTO_ICMP,
200 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
201 .pr_input = icmp_input,
202 .pr_output = rip_output,
203 .pr_ctlinput = rip_ctlinput,
204 .pr_ctloutput = rip_ctloutput,
205 .pr_usrreq = rip_usrreq,
206 .pr_init = icmp_init,
207 },
208 #ifdef GATEWAY
209 { .pr_domain = &inetdomain,
210 .pr_protocol = IPPROTO_IP,
211 .pr_slowtimo = ipflow_slowtimo,
212 },
213 #endif /* GATEWAY */
214 #ifdef IPSEC
215 { .pr_type = SOCK_RAW,
216 .pr_domain = &inetdomain,
217 .pr_protocol = IPPROTO_AH,
218 .pr_flags = PR_ATOMIC|PR_ADDR,
219 .pr_input = ah4_input,
220 .pr_ctlinput = ah4_ctlinput,
221 },
222 #ifdef IPSEC_ESP
223 { .pr_type = SOCK_RAW,
224 .pr_domain = &inetdomain,
225 .pr_protocol = IPPROTO_ESP,
226 .pr_flags = PR_ATOMIC|PR_ADDR,
227 .pr_input = esp4_input,
228 .pr_ctlinput = esp4_ctlinput,
229 },
230 #endif /* IPSEC_ESP */
231 { .pr_type = SOCK_RAW,
232 .pr_domain = &inetdomain,
233 .pr_protocol = IPPROTO_IPCOMP,
234 .pr_flags = PR_ATOMIC|PR_ADDR,
235 .pr_input = ipcomp4_input,
236 },
237 #endif /* IPSEC */
238 #ifdef FAST_IPSEC
239 { .pr_type = SOCK_RAW,
240 .pr_domain = &inetdomain,
241 .pr_protocol = IPPROTO_AH,
242 .pr_flags = PR_ATOMIC|PR_ADDR,
243 .pr_input = ipsec4_common_input,
244 .pr_ctlinput = ah4_ctlinput,
245 },
246 { .pr_type = SOCK_RAW,
247 .pr_domain = &inetdomain,
248 .pr_protocol = IPPROTO_ESP,
249 .pr_flags = PR_ATOMIC|PR_ADDR,
250 .pr_input = ipsec4_common_input,
251 .pr_ctlinput = esp4_ctlinput,
252 },
253 { .pr_type = SOCK_RAW,
254 .pr_domain = &inetdomain,
255 .pr_protocol = IPPROTO_IPCOMP,
256 .pr_flags = PR_ATOMIC|PR_ADDR,
257 .pr_input = ipsec4_common_input,
258 },
259 #endif /* FAST_IPSEC */
260 { .pr_type = SOCK_RAW,
261 .pr_domain = &inetdomain,
262 .pr_protocol = IPPROTO_IPV4,
263 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
264 .pr_input = encap4_input,
265 .pr_output = rip_output,
266 .pr_ctlinput = rip_ctlinput,
267 .pr_ctloutput = rip_ctloutput,
268 .pr_usrreq = rip_usrreq,
269 .pr_init = encap_init,
270 },
271 #ifdef INET6
272 { .pr_type = SOCK_RAW,
273 .pr_domain = &inetdomain,
274 .pr_protocol = IPPROTO_IPV6,
275 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
276 .pr_input = encap4_input,
277 .pr_output = rip_output,
278 .pr_ctlinput = rip_ctlinput,
279 .pr_ctloutput = rip_ctloutput,
280 .pr_usrreq = rip_usrreq,
281 .pr_init = encap_init,
282 },
283 #endif /* INET6 */
284 #if NETHERIP > 0
285 { .pr_type = SOCK_RAW,
286 .pr_domain = &inetdomain,
287 .pr_protocol = IPPROTO_ETHERIP,
288 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
289 .pr_input = ip_etherip_input,
290 .pr_output = rip_output,
291 .pr_ctlinput = rip_ctlinput,
292 .pr_ctloutput = rip_ctloutput,
293 .pr_usrreq = rip_usrreq,
294 },
295 #endif /* NETHERIP > 0 */
296 #if NCARP > 0
297 { .pr_type = SOCK_RAW,
298 .pr_domain = &inetdomain,
299 .pr_protocol = IPPROTO_CARP,
300 .pr_flags = PR_ATOMIC|PR_ADDR,
301 .pr_input = carp_proto_input,
302 .pr_output = rip_output,
303 .pr_ctloutput = rip_ctloutput,
304 .pr_usrreq = rip_usrreq,
305 },
306 #endif /* NCARP > 0 */
307 #if NGRE > 0
308 { .pr_type = SOCK_RAW,
309 .pr_domain = &inetdomain,
310 .pr_protocol = IPPROTO_GRE,
311 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
312 .pr_input = gre_input,
313 .pr_output = rip_output,
314 .pr_ctloutput = rip_ctloutput,
315 .pr_ctlinput = rip_ctlinput,
316 .pr_usrreq = rip_usrreq,
317 },
318 { .pr_type = SOCK_RAW,
319 .pr_domain = &inetdomain,
320 .pr_protocol = IPPROTO_MOBILE,
321 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
322 .pr_input = gre_mobile_input,
323 .pr_output = rip_output,
324 .pr_ctloutput = rip_ctloutput,
325 .pr_ctlinput = rip_ctlinput,
326 .pr_usrreq = rip_usrreq,
327 },
328 #endif /* NGRE > 0 */
329 { .pr_type = SOCK_RAW,
330 .pr_domain = &inetdomain,
331 .pr_protocol = IPPROTO_IGMP,
332 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
333 .pr_input = igmp_input,
334 .pr_output = rip_output,
335 .pr_ctloutput = rip_ctloutput,
336 .pr_ctlinput = rip_ctlinput,
337 .pr_usrreq = rip_usrreq,
338 .pr_fasttimo = igmp_fasttimo,
339 .pr_slowtimo = igmp_slowtimo,
340 },
341 #ifdef PIM
342 { .pr_type = SOCK_RAW,
343 .pr_domain = &inetdomain,
344 .pr_protocol = IPPROTO_PIM,
345 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
346 .pr_input = pim_input,
347 .pr_output = rip_output,
348 .pr_ctloutput = rip_ctloutput,
349 .pr_ctlinput = rip_ctlinput,
350 .pr_usrreq = rip_usrreq,
351 },
352 #endif /* PIM */
353 #ifdef TPIP
354 { .pr_type = SOCK_SEQPACKET,
355 .pr_domain = &inetdomain,
356 .pr_protocol = IPPROTO_TP,
357 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_LASTHDR|PR_ABRTACPTDIS,
358 .pr_input = tpip_input,
359 .pr_ctloutput = tp_ctloutput,
360 .pr_ctlinput = tpip_ctlinput,
361 .pr_usrreq = tp_usrreq,
362 .pr_init = tp_init,
363 .pr_slowtimo = tp_slowtimo,
364 .pr_drain = tp_drain,
365 },
366 #endif /* TPIP */
367 #ifdef ISO
368 /* EON (ISO CLNL over IP) */
369 #ifdef EON
370 { .pr_type = SOCK_RAW,
371 .pr_domain = &inetdomain,
372 .pr_protocol = IPPROTO_EON,
373 .pr_flags = PR_LASTHDR,
374 .pr_input = eoninput,
375 .pr_ctlinput = eonctlinput,
376 .pr_init = eonprotoinit,
377 },
378 #else
379 { .pr_type = SOCK_RAW,
380 .pr_domain = &inetdomain,
381 .pr_protocol = IPPROTO_EON,
382 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
383 .pr_input = encap4_input,
384 .pr_output = rip_output,
385 .pr_ctloutput = rip_ctloutput,
386 .pr_ctlinput = rip_ctlinput,
387 .pr_usrreq = rip_usrreq,
388 .pr_init = encap_init,
389 },
390 #endif /* EON */
391 #endif /* ISO */
392 /* raw wildcard */
393 { .pr_type = SOCK_RAW,
394 .pr_domain = &inetdomain,
395 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
396 .pr_input = rip_input,
397 .pr_output = rip_output,
398 .pr_ctloutput = rip_ctloutput,
399 .pr_ctlinput = rip_ctlinput,
400 .pr_usrreq = rip_usrreq,
401 .pr_init = rip_init,
402 },
403 };
404
405 extern struct ifqueue ipintrq;
406
407 POOL_INIT(sockaddr_in_pool, sizeof(struct sockaddr_in), 0, 0, 0,
408 "sockaddr_in_pool", NULL, IPL_NET);
409
410 struct domain inetdomain = {
411 .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
412 .dom_externalize = NULL, .dom_dispose = NULL,
413 .dom_protosw = inetsw,
414 .dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)],
415 .dom_rtattach = rn_inithead,
416 .dom_rtoffset = 32, .dom_maxrtkey = sizeof(struct sockaddr_in),
417 #ifdef IPSELSRC
418 .dom_ifattach = in_domifattach,
419 .dom_ifdetach = in_domifdetach,
420 #else
421 .dom_ifattach = NULL,
422 .dom_ifdetach = NULL,
423 #endif
424 .dom_ifqueues = { &ipintrq, NULL },
425 .dom_link = { NULL },
426 .dom_mowner = MOWNER_INIT("",""),
427 .dom_sa_pool = &sockaddr_in_pool,
428 .dom_sa_len = sizeof(struct sockaddr_in),
429 .dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
430 .dom_sa_cmplen = sizeof(struct in_addr),
431 .dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache)
432 };
433
434 u_char ip_protox[IPPROTO_MAX];
435
436 int icmperrppslim = 100; /* 100pps */
437
438 int
439 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
440 {
441 uint_fast8_t len;
442 const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
443 addrend = addrofs + sizeof(struct in_addr);
444 int rc;
445 const struct sockaddr_in *sin1, *sin2;
446
447 sin1 = satocsin(sa1);
448 sin2 = satocsin(sa2);
449
450 len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
451
452 if (len > addrofs &&
453 (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
454 len - addrofs)) != 0)
455 return rc;
456
457 return sin1->sin_len - sin2->sin_len;
458 }
459