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