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