xref: /netbsd-src/sys/netinet/in_proto.c (revision 6cd39ddb8550f6fa1bff3fed32053d7f19fd0453)
1 /*	$NetBSD: in_proto.c,v 1.118 2016/01/21 15:41:30 riastradh 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.118 2016/01/21 15:41:30 riastradh Exp $");
65 
66 #ifdef _KERNEL_OPT
67 #include "opt_mrouting.h"
68 #include "opt_inet.h"
69 #include "opt_ipsec.h"
70 #include "opt_pim.h"
71 #include "opt_gateway.h"
72 #include "opt_dccp.h"
73 #include "opt_sctp.h"
74 #include "opt_compat_netbsd.h"
75 #endif
76 
77 #include <sys/param.h>
78 #include <sys/socket.h>
79 #include <sys/protosw.h>
80 #include <sys/domain.h>
81 #include <sys/mbuf.h>
82 
83 #include <net/if.h>
84 #include <net/radix.h>
85 #include <net/route.h>
86 
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/ip_icmp.h>
93 #include <netinet/in_ifattach.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_proto.h>
96 
97 #ifdef INET6
98 #ifndef INET
99 #include <netinet/in.h>
100 #endif
101 #include <netinet/ip6.h>
102 #endif
103 
104 #include <netinet/igmp_var.h>
105 #ifdef PIM
106 #include <netinet/pim_var.h>
107 #endif
108 #include <netinet/tcp.h>
109 #include <netinet/tcp_fsm.h>
110 #include <netinet/tcp_seq.h>
111 #include <netinet/tcp_timer.h>
112 #include <netinet/tcp_var.h>
113 #include <netinet/tcpip.h>
114 #include <netinet/tcp_debug.h>
115 #include <netinet/udp.h>
116 #include <netinet/udp_var.h>
117 #include <netinet/ip_encap.h>
118 
119 #ifdef DCCP
120 #include <netinet/dccp.h>
121 #include <netinet/dccp_var.h>
122 #endif
123 
124 #ifdef SCTP
125 #include <netinet/sctp.h>
126 #include <netinet/sctp_var.h>
127 #endif
128 
129 /*
130  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
131  */
132 
133 #ifdef IPSEC
134 #include <netipsec/ipsec.h>
135 #include <netipsec/key.h>
136 #endif	/* IPSEC */
137 
138 #include "carp.h"
139 #if NCARP > 0
140 #include <netinet/ip_carp.h>
141 #endif
142 
143 #include "pfsync.h"
144 #if NPFSYNC > 0
145 #include <net/pfvar.h>
146 #include <net/if_pfsync.h>
147 #endif
148 
149 #include "etherip.h"
150 #if NETHERIP > 0
151 #include <netinet/ip_etherip.h>
152 #endif
153 
154 DOMAIN_DEFINE(inetdomain);	/* forward declare and add to link set */
155 
156 /* Wrappers to acquire kernel_lock. */
157 
158 PR_WRAP_CTLINPUT(rip_ctlinput)
159 PR_WRAP_CTLINPUT(udp_ctlinput)
160 PR_WRAP_CTLINPUT(tcp_ctlinput)
161 
162 #define	rip_ctlinput	rip_ctlinput_wrapper
163 #define	udp_ctlinput	udp_ctlinput_wrapper
164 #define	tcp_ctlinput	tcp_ctlinput_wrapper
165 
166 PR_WRAP_CTLOUTPUT(rip_ctloutput)
167 PR_WRAP_CTLOUTPUT(udp_ctloutput)
168 PR_WRAP_CTLOUTPUT(tcp_ctloutput)
169 
170 #define	rip_ctloutput	rip_ctloutput_wrapper
171 #define	udp_ctloutput	udp_ctloutput_wrapper
172 #define	tcp_ctloutput	tcp_ctloutput_wrapper
173 
174 #ifdef DCCP
175 PR_WRAP_CTLINPUT(dccp_ctlinput)
176 PR_WRAP_CTLOUTPUT(dccp_ctloutput)
177 
178 #define dccp_ctlinput	dccp_ctlinput_wrapper
179 #define dccp_ctloutput	dccp_ctloutput_wrapper
180 #endif
181 
182 #ifdef SCTP
183 PR_WRAP_CTLINPUT(sctp_ctlinput)
184 PR_WRAP_CTLOUTPUT(sctp_ctloutput)
185 
186 #define sctp_ctlinput	sctp_ctlinput_wrapper
187 #define sctp_ctloutput	sctp_ctloutput_wrapper
188 #endif
189 
190 #if defined(IPSEC)
191 PR_WRAP_CTLINPUT(ah4_ctlinput)
192 
193 #define	ah4_ctlinput	ah4_ctlinput_wrapper
194 PR_WRAP_CTLINPUT(esp4_ctlinput)
195 
196 #define	esp4_ctlinput	esp4_ctlinput_wrapper
197 #endif
198 
199 const struct protosw inetsw[] = {
200 {	.pr_domain = &inetdomain,
201 	.pr_init = ip_init,
202 	.pr_fasttimo = ip_fasttimo,
203 	.pr_slowtimo = ip_slowtimo,
204 	.pr_drain = ip_drainstub,
205 },
206 {	.pr_type = SOCK_DGRAM,
207 	.pr_domain = &inetdomain,
208 	.pr_protocol = IPPROTO_UDP,
209 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
210 	.pr_input = udp_input,
211 	.pr_ctlinput = udp_ctlinput,
212 	.pr_ctloutput = udp_ctloutput,
213 	.pr_usrreqs = &udp_usrreqs,
214 	.pr_init = udp_init,
215 },
216 {	.pr_type = SOCK_STREAM,
217 	.pr_domain = &inetdomain,
218 	.pr_protocol = IPPROTO_TCP,
219 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
220 	.pr_input = tcp_input,
221 	.pr_ctlinput = tcp_ctlinput,
222 	.pr_ctloutput = tcp_ctloutput,
223 	.pr_usrreqs = &tcp_usrreqs,
224 	.pr_init = tcp_init,
225 	.pr_fasttimo = tcp_fasttimo,
226 	.pr_drain = tcp_drainstub,
227 },
228 #ifdef DCCP
229 {	.pr_type = SOCK_CONN_DGRAM,
230 	.pr_domain = &inetdomain,
231 	.pr_protocol = IPPROTO_DCCP,
232 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ATOMIC|PR_LISTEN|PR_ABRTACPTDIS,
233 	.pr_input = dccp_input,
234 	.pr_ctlinput = dccp_ctlinput,
235 	.pr_ctloutput = dccp_ctloutput,
236 	.pr_usrreqs = &dccp_usrreqs,
237 	.pr_init = dccp_init,
238 },
239 #endif
240 #ifdef SCTP
241 {	.pr_type = SOCK_DGRAM,
242 	.pr_domain = &inetdomain,
243 	.pr_protocol = IPPROTO_SCTP,
244 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
245 	.pr_input = sctp_input,
246 	.pr_ctlinput = sctp_ctlinput,
247 	.pr_ctloutput = sctp_ctloutput,
248 	.pr_usrreqs = &sctp_usrreqs,
249 	.pr_init = sctp_init,
250 	.pr_drain = sctp_drain
251 },
252 {	.pr_type = SOCK_SEQPACKET,
253 	.pr_domain = &inetdomain,
254 	.pr_protocol = IPPROTO_SCTP,
255 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
256 	.pr_input = sctp_input,
257 	.pr_ctlinput = sctp_ctlinput,
258 	.pr_ctloutput = sctp_ctloutput,
259 	.pr_usrreqs = &sctp_usrreqs,
260 	.pr_drain = sctp_drain
261 },
262 {	.pr_type = SOCK_STREAM,
263 	.pr_domain = &inetdomain,
264 	.pr_protocol = IPPROTO_SCTP,
265 	.pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD|PR_LISTEN,
266 	.pr_input = sctp_input,
267 	.pr_ctlinput = sctp_ctlinput,
268 	.pr_ctloutput = sctp_ctloutput,
269 	.pr_usrreqs = &sctp_usrreqs,
270 	.pr_drain = sctp_drain
271 },
272 #endif /* SCTP */
273 {	.pr_type = SOCK_RAW,
274 	.pr_domain = &inetdomain,
275 	.pr_protocol = IPPROTO_RAW,
276 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
277 	.pr_input = rip_input,
278 	.pr_ctlinput = rip_ctlinput,
279 	.pr_ctloutput = rip_ctloutput,
280 	.pr_usrreqs = &rip_usrreqs,
281 },
282 {	.pr_type = SOCK_RAW,
283 	.pr_domain = &inetdomain,
284 	.pr_protocol = IPPROTO_ICMP,
285 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
286 	.pr_input = icmp_input,
287 	.pr_ctlinput = rip_ctlinput,
288 	.pr_ctloutput = rip_ctloutput,
289 	.pr_usrreqs = &rip_usrreqs,
290 	.pr_init = icmp_init,
291 },
292 #ifdef GATEWAY
293 {	.pr_domain = &inetdomain,
294 	.pr_protocol = IPPROTO_IP,
295 	.pr_slowtimo = ipflow_slowtimo,
296 	.pr_init = ipflow_poolinit,
297 },
298 #endif /* GATEWAY */
299 #ifdef IPSEC
300 {	.pr_type = SOCK_RAW,
301 	.pr_domain = &inetdomain,
302 	.pr_protocol = IPPROTO_AH,
303 	.pr_flags = PR_ATOMIC|PR_ADDR,
304 	.pr_input = ipsec4_common_input,
305 	.pr_ctlinput = ah4_ctlinput,
306 },
307 {	.pr_type = SOCK_RAW,
308 	.pr_domain = &inetdomain,
309 	.pr_protocol = IPPROTO_ESP,
310 	.pr_flags = PR_ATOMIC|PR_ADDR,
311 	.pr_input = ipsec4_common_input,
312 	.pr_ctlinput = esp4_ctlinput,
313 },
314 {	.pr_type = SOCK_RAW,
315 	.pr_domain = &inetdomain,
316 	.pr_protocol = IPPROTO_IPCOMP,
317 	.pr_flags = PR_ATOMIC|PR_ADDR,
318 	.pr_input = ipsec4_common_input,
319 },
320 #endif /* IPSEC */
321 {	.pr_type = SOCK_RAW,
322 	.pr_domain = &inetdomain,
323 	.pr_protocol = IPPROTO_IPV4,
324 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
325 	.pr_input = encap4_input,
326 	.pr_ctlinput = rip_ctlinput,
327 	.pr_ctloutput = rip_ctloutput,
328 	.pr_usrreqs = &rip_usrreqs,
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_ctlinput = rip_ctlinput,
338 	.pr_ctloutput = rip_ctloutput,
339 	.pr_usrreqs = &rip_usrreqs,
340 	.pr_init = encap_init,
341 },
342 #endif /* INET6 */
343 #if NETHERIP > 0
344 {	.pr_type = SOCK_RAW,
345 	.pr_domain = &inetdomain,
346 	.pr_protocol = IPPROTO_ETHERIP,
347 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
348 	.pr_input = ip_etherip_input,
349 	.pr_ctlinput = rip_ctlinput,
350 	.pr_ctloutput = rip_ctloutput,
351 	.pr_usrreqs = &rip_usrreqs,
352 },
353 #endif /* NETHERIP > 0 */
354 #if NCARP > 0
355 {	.pr_type = SOCK_RAW,
356 	.pr_domain = &inetdomain,
357 	.pr_protocol = IPPROTO_CARP,
358 	.pr_flags = PR_ATOMIC|PR_ADDR,
359 	.pr_input = carp_proto_input,
360 	.pr_ctloutput = rip_ctloutput,
361 	.pr_usrreqs = &rip_usrreqs,
362 	.pr_init = carp_init,
363 },
364 #endif /* NCARP > 0 */
365 #if NPFSYNC > 0
366 {	.pr_type = SOCK_RAW,
367 	.pr_domain = &inetdomain,
368 	.pr_protocol = IPPROTO_PFSYNC,
369 	.pr_flags	 = PR_ATOMIC|PR_ADDR,
370 	.pr_input	 = pfsync_input,
371 	.pr_ctloutput = rip_ctloutput,
372 	.pr_usrreqs	 = &rip_usrreqs,
373 },
374 #endif /* NPFSYNC > 0 */
375 {	.pr_type = SOCK_RAW,
376 	.pr_domain = &inetdomain,
377 	.pr_protocol = IPPROTO_IGMP,
378 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
379 	.pr_input = igmp_input,
380 	.pr_ctloutput = rip_ctloutput,
381 	.pr_ctlinput = rip_ctlinput,
382 	.pr_usrreqs = &rip_usrreqs,
383 	.pr_fasttimo = igmp_fasttimo,
384 	.pr_slowtimo = igmp_slowtimo,
385 	.pr_init = igmp_init,
386 },
387 #ifdef PIM
388 {	.pr_type = SOCK_RAW,
389 	.pr_domain = &inetdomain,
390 	.pr_protocol = IPPROTO_PIM,
391 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
392 	.pr_input = pim_input,
393 	.pr_ctloutput = rip_ctloutput,
394 	.pr_ctlinput = rip_ctlinput,
395 	.pr_usrreqs = &rip_usrreqs,
396 },
397 #endif /* PIM */
398 /* raw wildcard */
399 {	.pr_type = SOCK_RAW,
400 	.pr_domain = &inetdomain,
401 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
402 	.pr_input = rip_input,
403 	.pr_ctloutput = rip_ctloutput,
404 	.pr_ctlinput = rip_ctlinput,
405 	.pr_usrreqs = &rip_usrreqs,
406 	.pr_init = rip_init,
407 },
408 };
409 
410 const struct sockaddr_in in_any = {
411 	  .sin_len = sizeof(struct sockaddr_in)
412 	, .sin_family = AF_INET
413 	, .sin_port = 0
414 	, .sin_addr = {.s_addr = 0 /* INADDR_ANY */}
415 };
416 
417 struct domain inetdomain = {
418 	.dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
419 	.dom_externalize = NULL, .dom_dispose = NULL,
420 	.dom_protosw = inetsw,
421 	.dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)],
422 	.dom_rtattach = rt_inithead,
423 	.dom_rtoffset = 32,
424 	.dom_maxrtkey = sizeof(struct ip_pack4),
425 	.dom_if_up = in_if_up,
426 	.dom_if_down = in_if_down,
427 	.dom_ifattach = in_domifattach,
428 	.dom_ifdetach = in_domifdetach,
429 	.dom_if_link_state_change = in_if_link_state_change,
430 	.dom_ifqueues = { NULL, NULL },
431 	.dom_link = { NULL },
432 	.dom_mowner = MOWNER_INIT("",""),
433 	.dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
434 	.dom_sa_cmplen = sizeof(struct in_addr),
435 	.dom_sa_any = (const struct sockaddr *)&in_any,
436 	.dom_sockaddr_const_addr = sockaddr_in_const_addr,
437 	.dom_sockaddr_addr = sockaddr_in_addr,
438 	.dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache)
439 };
440 
441 u_char	ip_protox[IPPROTO_MAX];
442 
443 int icmperrppslim = 100;			/* 100pps */
444 
445 static void
446 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp)
447 {
448 	socklen_t slen;
449 
450 	if (slenp == NULL)
451 		return;
452 
453 	slen = sockaddr_getlen(sa);
454 	*slenp = (socklen_t)MIN(sizeof(struct in_addr),
455 	    slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr)));
456 }
457 
458 const void *
459 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp)
460 {
461 	const struct sockaddr_in *sin;
462 
463 	sockaddr_in_addrlen(sa, slenp);
464 	sin = (const struct sockaddr_in *)sa;
465 	return &sin->sin_addr;
466 }
467 
468 void *
469 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp)
470 {
471 	struct sockaddr_in *sin;
472 
473 	sockaddr_in_addrlen(sa, slenp);
474 	sin = (struct sockaddr_in *)sa;
475 	return &sin->sin_addr;
476 }
477 
478 int
479 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
480 {
481 	uint_fast8_t len;
482 	const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
483 			   addrend = addrofs + sizeof(struct in_addr);
484 	int rc;
485 	const struct sockaddr_in *sin1, *sin2;
486 
487 	sin1 = satocsin(sa1);
488 	sin2 = satocsin(sa2);
489 
490 	len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
491 
492 	if (len > addrofs &&
493 	     (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
494 	                  len - addrofs)) != 0)
495 		return rc;
496 
497 	return sin1->sin_len - sin2->sin_len;
498 }
499