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