in_proto.c revision 1.94.2.1 1 /* $NetBSD: in_proto.c,v 1.94.2.1 2009/05/04 08:14:17 yamt 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.94.2.1 2009/05/04 08:14:17 yamt 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 #include "opt_gateway.h"
73
74 #include <sys/param.h>
75 #include <sys/socket.h>
76 #include <sys/protosw.h>
77 #include <sys/domain.h>
78 #include <sys/mbuf.h>
79
80 #include <net/if.h>
81 #include <net/radix.h>
82 #include <net/route.h>
83
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_var.h>
88 #include <netinet/ip_icmp.h>
89 #include <netinet/in_ifattach.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/in_proto.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 "carp.h"
143 #if NCARP > 0
144 #include <netinet/ip_carp.h>
145 #endif
146
147 #include "etherip.h"
148 #if NETHERIP > 0
149 #include <netinet/ip_etherip.h>
150 #endif
151
152 DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */
153
154 /* Wrappers to acquire kernel_lock. */
155
156 PR_WRAP_USRREQ(rip_usrreq)
157 PR_WRAP_USRREQ(udp_usrreq)
158 PR_WRAP_USRREQ(tcp_usrreq)
159
160 #define rip_usrreq rip_usrreq_wrapper
161 #define udp_usrreq udp_usrreq_wrapper
162 #define tcp_usrreq tcp_usrreq_wrapper
163
164 PR_WRAP_CTLINPUT(rip_ctlinput)
165 PR_WRAP_CTLINPUT(udp_ctlinput)
166 PR_WRAP_CTLINPUT(tcp_ctlinput)
167
168 #define rip_ctlinput rip_ctlinput_wrapper
169 #define udp_ctlinput udp_ctlinput_wrapper
170 #define tcp_ctlinput tcp_ctlinput_wrapper
171
172 PR_WRAP_CTLOUTPUT(rip_ctloutput)
173 PR_WRAP_CTLOUTPUT(udp_ctloutput)
174 PR_WRAP_CTLOUTPUT(tcp_ctloutput)
175
176 #define rip_ctloutput rip_ctloutput_wrapper
177 #define udp_ctloutput udp_ctloutput_wrapper
178 #define tcp_ctloutput tcp_ctloutput_wrapper
179
180 #if defined(IPSEC) || defined(FAST_IPSEC)
181 PR_WRAP_CTLINPUT(ah4_ctlinput)
182
183 #define ah4_ctlinput ah4_ctlinput_wrapper
184 #endif
185
186 #if defined(IPSEC_ESP) || defined(FAST_IPSEC)
187 PR_WRAP_CTLINPUT(esp4_ctlinput)
188
189 #define esp4_ctlinput esp4_ctlinput_wrapper
190 #endif
191
192 #ifdef TPIP
193 PR_WRAP_CTLOUTPUT(tp_ctloutput)
194
195 #define tp_ctloutput tp_ctloutput_wrapper
196
197 PR_WRAP_CTLINPUT(tpip_ctlinput)
198
199 #define tpip_ctlinput tpip_ctlinput_wrapper
200 #endif
201
202 #ifdef EON
203 PR_WRAP_CTLINPUT(eonctlinput)
204
205 #define eonctlinput eonctlinput_wrapper
206 #endif
207
208 const struct protosw inetsw[] = {
209 { .pr_domain = &inetdomain,
210 .pr_init = ip_init,
211 .pr_output = ip_output,
212 .pr_slowtimo = ip_slowtimo,
213 .pr_drain = ip_drain,
214 },
215 { .pr_type = SOCK_DGRAM,
216 .pr_domain = &inetdomain,
217 .pr_protocol = IPPROTO_UDP,
218 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
219 .pr_input = udp_input,
220 .pr_ctlinput = udp_ctlinput,
221 .pr_ctloutput = udp_ctloutput,
222 .pr_usrreq = udp_usrreq,
223 .pr_init = udp_init,
224 },
225 { .pr_type = SOCK_STREAM,
226 .pr_domain = &inetdomain,
227 .pr_protocol = IPPROTO_TCP,
228 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
229 .pr_input = tcp_input,
230 .pr_ctlinput = tcp_ctlinput,
231 .pr_ctloutput = tcp_ctloutput,
232 .pr_usrreq = tcp_usrreq,
233 .pr_init = tcp_init,
234 .pr_slowtimo = tcp_slowtimo,
235 .pr_drain = tcp_drain,
236 },
237 { .pr_type = SOCK_RAW,
238 .pr_domain = &inetdomain,
239 .pr_protocol = IPPROTO_RAW,
240 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
241 .pr_input = rip_input,
242 .pr_output = rip_output,
243 .pr_ctlinput = rip_ctlinput,
244 .pr_ctloutput = rip_ctloutput,
245 .pr_usrreq = rip_usrreq,
246 },
247 { .pr_type = SOCK_RAW,
248 .pr_domain = &inetdomain,
249 .pr_protocol = IPPROTO_ICMP,
250 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
251 .pr_input = icmp_input,
252 .pr_output = rip_output,
253 .pr_ctlinput = rip_ctlinput,
254 .pr_ctloutput = rip_ctloutput,
255 .pr_usrreq = rip_usrreq,
256 .pr_init = icmp_init,
257 },
258 #ifdef GATEWAY
259 { .pr_domain = &inetdomain,
260 .pr_protocol = IPPROTO_IP,
261 .pr_slowtimo = ipflow_slowtimo,
262 .pr_init = ipflow_poolinit,
263 },
264 #endif /* GATEWAY */
265 #ifdef IPSEC
266 { .pr_type = SOCK_RAW,
267 .pr_domain = &inetdomain,
268 .pr_protocol = IPPROTO_AH,
269 .pr_flags = PR_ATOMIC|PR_ADDR,
270 .pr_input = ah4_input,
271 .pr_ctlinput = ah4_ctlinput,
272 .pr_init = ah4_init,
273 },
274 #ifdef IPSEC_ESP
275 { .pr_type = SOCK_RAW,
276 .pr_domain = &inetdomain,
277 .pr_protocol = IPPROTO_ESP,
278 .pr_flags = PR_ATOMIC|PR_ADDR,
279 .pr_input = esp4_input,
280 .pr_ctlinput = esp4_ctlinput,
281 .pr_init = esp4_init,
282 },
283 #endif /* IPSEC_ESP */
284 { .pr_type = SOCK_RAW,
285 .pr_domain = &inetdomain,
286 .pr_protocol = IPPROTO_IPCOMP,
287 .pr_flags = PR_ATOMIC|PR_ADDR,
288 .pr_input = ipcomp4_input,
289 .pr_init = ipcomp4_init,
290 },
291 #endif /* IPSEC */
292 #ifdef FAST_IPSEC
293 { .pr_type = SOCK_RAW,
294 .pr_domain = &inetdomain,
295 .pr_protocol = IPPROTO_AH,
296 .pr_flags = PR_ATOMIC|PR_ADDR,
297 .pr_input = ipsec4_common_input,
298 .pr_ctlinput = ah4_ctlinput,
299 },
300 { .pr_type = SOCK_RAW,
301 .pr_domain = &inetdomain,
302 .pr_protocol = IPPROTO_ESP,
303 .pr_flags = PR_ATOMIC|PR_ADDR,
304 .pr_input = ipsec4_common_input,
305 .pr_ctlinput = esp4_ctlinput,
306 },
307 { .pr_type = SOCK_RAW,
308 .pr_domain = &inetdomain,
309 .pr_protocol = IPPROTO_IPCOMP,
310 .pr_flags = PR_ATOMIC|PR_ADDR,
311 .pr_input = ipsec4_common_input,
312 },
313 #endif /* FAST_IPSEC */
314 { .pr_type = SOCK_RAW,
315 .pr_domain = &inetdomain,
316 .pr_protocol = IPPROTO_IPV4,
317 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
318 .pr_input = encap4_input,
319 .pr_output = rip_output,
320 .pr_ctlinput = rip_ctlinput,
321 .pr_ctloutput = rip_ctloutput,
322 .pr_usrreq = rip_usrreq,
323 .pr_init = encap_init,
324 },
325 #ifdef INET6
326 { .pr_type = SOCK_RAW,
327 .pr_domain = &inetdomain,
328 .pr_protocol = IPPROTO_IPV6,
329 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
330 .pr_input = encap4_input,
331 .pr_output = rip_output,
332 .pr_ctlinput = rip_ctlinput,
333 .pr_ctloutput = rip_ctloutput,
334 .pr_usrreq = rip_usrreq,
335 .pr_init = encap_init,
336 },
337 #endif /* INET6 */
338 #if NETHERIP > 0
339 { .pr_type = SOCK_RAW,
340 .pr_domain = &inetdomain,
341 .pr_protocol = IPPROTO_ETHERIP,
342 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
343 .pr_input = ip_etherip_input,
344 .pr_output = rip_output,
345 .pr_ctlinput = rip_ctlinput,
346 .pr_ctloutput = rip_ctloutput,
347 .pr_usrreq = rip_usrreq,
348 },
349 #endif /* NETHERIP > 0 */
350 #if NCARP > 0
351 { .pr_type = SOCK_RAW,
352 .pr_domain = &inetdomain,
353 .pr_protocol = IPPROTO_CARP,
354 .pr_flags = PR_ATOMIC|PR_ADDR,
355 .pr_input = carp_proto_input,
356 .pr_output = rip_output,
357 .pr_ctloutput = rip_ctloutput,
358 .pr_usrreq = rip_usrreq,
359 },
360 #endif /* NCARP > 0 */
361 { .pr_type = SOCK_RAW,
362 .pr_domain = &inetdomain,
363 .pr_protocol = IPPROTO_IGMP,
364 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
365 .pr_input = igmp_input,
366 .pr_output = rip_output,
367 .pr_ctloutput = rip_ctloutput,
368 .pr_ctlinput = rip_ctlinput,
369 .pr_usrreq = rip_usrreq,
370 .pr_fasttimo = igmp_fasttimo,
371 .pr_slowtimo = igmp_slowtimo,
372 .pr_init = igmp_init,
373 },
374 #ifdef PIM
375 { .pr_type = SOCK_RAW,
376 .pr_domain = &inetdomain,
377 .pr_protocol = IPPROTO_PIM,
378 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
379 .pr_input = pim_input,
380 .pr_output = rip_output,
381 .pr_ctloutput = rip_ctloutput,
382 .pr_ctlinput = rip_ctlinput,
383 .pr_usrreq = rip_usrreq,
384 },
385 #endif /* PIM */
386 #ifdef TPIP
387 { .pr_type = SOCK_SEQPACKET,
388 .pr_domain = &inetdomain,
389 .pr_protocol = IPPROTO_TP,
390 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_LASTHDR|PR_ABRTACPTDIS,
391 .pr_input = tpip_input,
392 .pr_ctloutput = tp_ctloutput,
393 .pr_ctlinput = tpip_ctlinput,
394 .pr_usrreq = tp_usrreq,
395 .pr_init = tp_init,
396 .pr_slowtimo = tp_slowtimo,
397 .pr_drain = tp_drain,
398 },
399 #endif /* TPIP */
400 #ifdef ISO
401 /* EON (ISO CLNL over IP) */
402 #ifdef EON
403 { .pr_type = SOCK_RAW,
404 .pr_domain = &inetdomain,
405 .pr_protocol = IPPROTO_EON,
406 .pr_flags = PR_LASTHDR,
407 .pr_input = eoninput,
408 .pr_ctlinput = eonctlinput,
409 .pr_init = eonprotoinit,
410 },
411 #else
412 { .pr_type = SOCK_RAW,
413 .pr_domain = &inetdomain,
414 .pr_protocol = IPPROTO_EON,
415 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
416 .pr_input = encap4_input,
417 .pr_output = rip_output,
418 .pr_ctloutput = rip_ctloutput,
419 .pr_ctlinput = rip_ctlinput,
420 .pr_usrreq = rip_usrreq,
421 .pr_init = encap_init,
422 },
423 #endif /* EON */
424 #endif /* ISO */
425 /* raw wildcard */
426 { .pr_type = SOCK_RAW,
427 .pr_domain = &inetdomain,
428 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
429 .pr_input = rip_input,
430 .pr_output = rip_output,
431 .pr_ctloutput = rip_ctloutput,
432 .pr_ctlinput = rip_ctlinput,
433 .pr_usrreq = rip_usrreq,
434 .pr_init = rip_init,
435 },
436 };
437
438 extern struct ifqueue ipintrq;
439
440 const struct sockaddr_in in_any = {
441 .sin_len = sizeof(struct sockaddr_in)
442 , .sin_family = AF_INET
443 , .sin_port = 0
444 , .sin_addr = {.s_addr = 0 /* INADDR_ANY */}
445 };
446
447 struct domain inetdomain = {
448 .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
449 .dom_externalize = NULL, .dom_dispose = NULL,
450 .dom_protosw = inetsw,
451 .dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)],
452 .dom_rtattach = rn_inithead,
453 .dom_rtoffset = 32,
454 .dom_maxrtkey = sizeof(struct ip_pack4),
455 #ifdef IPSELSRC
456 .dom_ifattach = in_domifattach,
457 .dom_ifdetach = in_domifdetach,
458 #else
459 .dom_ifattach = NULL,
460 .dom_ifdetach = NULL,
461 #endif
462 .dom_ifqueues = { &ipintrq, NULL },
463 .dom_link = { NULL },
464 .dom_mowner = MOWNER_INIT("",""),
465 .dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
466 .dom_sa_cmplen = sizeof(struct in_addr),
467 .dom_sa_any = (const struct sockaddr *)&in_any,
468 .dom_sockaddr_const_addr = sockaddr_in_const_addr,
469 .dom_sockaddr_addr = sockaddr_in_addr,
470 .dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache)
471 };
472
473 u_char ip_protox[IPPROTO_MAX];
474
475 int icmperrppslim = 100; /* 100pps */
476
477 static void
478 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp)
479 {
480 socklen_t slen;
481
482 if (slenp == NULL)
483 return;
484
485 slen = sockaddr_getlen(sa);
486 *slenp = (socklen_t)MIN(sizeof(struct in_addr),
487 slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr)));
488 }
489
490 const void *
491 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp)
492 {
493 const struct sockaddr_in *sin;
494
495 sockaddr_in_addrlen(sa, slenp);
496 sin = (const struct sockaddr_in *)sa;
497 return &sin->sin_addr;
498 }
499
500 void *
501 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp)
502 {
503 struct sockaddr_in *sin;
504
505 sockaddr_in_addrlen(sa, slenp);
506 sin = (struct sockaddr_in *)sa;
507 return &sin->sin_addr;
508 }
509
510 int
511 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
512 {
513 uint_fast8_t len;
514 const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
515 addrend = addrofs + sizeof(struct in_addr);
516 int rc;
517 const struct sockaddr_in *sin1, *sin2;
518
519 sin1 = satocsin(sa1);
520 sin2 = satocsin(sa2);
521
522 len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
523
524 if (len > addrofs &&
525 (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
526 len - addrofs)) != 0)
527 return rc;
528
529 return sin1->sin_len - sin2->sin_len;
530 }
531