xref: /netbsd-src/sys/netinet6/in6_proto.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: in6_proto.c,v 1.83 2008/11/25 18:28:06 pooka Exp $	*/
2 /*	$KAME: in6_proto.c,v 1.66 2000/10/10 15:35:47 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)in_proto.c	8.1 (Berkeley) 6/10/93
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: in6_proto.c,v 1.83 2008/11/25 18:28:06 pooka Exp $");
66 
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69 #include "opt_iso.h"
70 
71 #include <sys/param.h>
72 #include <sys/socket.h>
73 #include <sys/protosw.h>
74 #include <sys/kernel.h>
75 #include <sys/domain.h>
76 #include <sys/mbuf.h>
77 
78 #include <net/if.h>
79 #include <net/radix.h>
80 #include <net/route.h>
81 
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip_encap.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_var.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/in6_pcb.h>
93 
94 #include <netinet/tcp.h>
95 #include <netinet/tcp_fsm.h>
96 #include <netinet/tcp_seq.h>
97 #include <netinet/tcp_timer.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcpip.h>
100 #include <netinet/tcp_debug.h>
101 
102 #include <netinet6/udp6.h>
103 #include <netinet6/udp6_var.h>
104 
105 #include <netinet6/pim6_var.h>
106 
107 #include <netinet6/nd6.h>
108 
109 #ifdef IPSEC
110 #include <netinet6/ipsec.h>
111 #include <netinet6/ah.h>
112 #ifdef IPSEC_ESP
113 #include <netinet6/esp.h>
114 #endif
115 #include <netinet6/ipcomp.h>
116 #endif /* IPSEC */
117 
118 #ifdef FAST_IPSEC
119 #include <netipsec/ipsec.h>
120 #include <netipsec/ipsec6.h>
121 #include <netipsec/key.h>
122 #endif /* FAST_IPSEC */
123 
124 
125 #include "carp.h"
126 #if NCARP > 0
127 #include <netinet/ip_carp.h>
128 #endif
129 
130 #include "etherip.h"
131 #if NETHERIP > 1
132 #include <netinet6/ip6_etherip.h>
133 #endif
134 
135 #include <netinet6/ip6protosw.h>
136 
137 #include <net/net_osdep.h>
138 
139 #ifndef offsetof		/* XXX */
140 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
141 #endif
142 
143 /*
144  * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
145  */
146 
147 DOMAIN_DEFINE(inet6domain);	/* forward declare and add to link set */
148 
149 /* Wrappers to acquire kernel_lock. */
150 
151 PR_WRAP_USRREQ(rip6_usrreq)
152 PR_WRAP_USRREQ(udp6_usrreq)
153 PR_WRAP_USRREQ(tcp_usrreq)
154 
155 #define	rip6_usrreq 	rip6_usrreq_wrapper
156 #define	udp6_usrreq 	udp6_usrreq_wrapper
157 #define	tcp_usrreq 	tcp_usrreq_wrapper
158 
159 PR_WRAP_CTLINPUT(rip6_ctlinput)
160 PR_WRAP_CTLINPUT(encap6_ctlinput)
161 PR_WRAP_CTLINPUT(udp6_ctlinput)
162 PR_WRAP_CTLINPUT(tcp6_ctlinput)
163 
164 #define	rip6_ctlinput	rip6_ctlinput_wrapper
165 #define	encap6_ctlinput	encap6_ctlinput_wrapper
166 #define	udp6_ctlinput	udp6_ctlinput_wrapper
167 #define	tcp6_ctlinput	tcp6_ctlinput_wrapper
168 
169 PR_WRAP_CTLOUTPUT(rip6_ctloutput)
170 PR_WRAP_CTLOUTPUT(ip6_ctloutput)
171 PR_WRAP_CTLOUTPUT(tcp_ctloutput)
172 PR_WRAP_CTLOUTPUT(icmp6_ctloutput)
173 
174 #define	rip6_ctloutput	rip6_ctloutput_wrapper
175 #define	ip6_ctloutput	ip6_ctloutput_wrapper
176 #define	tcp_ctloutput	tcp_ctloutput_wrapper
177 #define	icmp6_ctloutput	icmp6_ctloutput_wrapper
178 
179 #if defined(IPSEC) || defined(FAST_IPSEC)
180 PR_WRAP_CTLINPUT(ah6_ctlinput)
181 PR_WRAP_CTLINPUT(esp6_ctlinput)
182 
183 #define	ah6_ctlinput	ah6_ctlinput_wrapper
184 #define	esp6_ctlinput	esp6_ctlinput_wrapper
185 #endif
186 
187 const struct ip6protosw inet6sw[] = {
188 {	.pr_domain = &inet6domain,
189 	.pr_protocol = IPPROTO_IPV6,
190 	.pr_init = ip6_init,
191 	.pr_slowtimo = frag6_slowtimo,
192 	.pr_drain = frag6_drain,
193 },
194 {	.pr_type = SOCK_DGRAM,
195 	.pr_domain = &inet6domain,
196 	.pr_protocol = IPPROTO_UDP,
197 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
198 	.pr_input = udp6_input,
199 	.pr_ctlinput = udp6_ctlinput,
200 	.pr_ctloutput = ip6_ctloutput,
201 	.pr_usrreq = udp6_usrreq,
202 	.pr_init = udp6_init,
203 },
204 {	.pr_type = SOCK_STREAM,
205 	.pr_domain = &inet6domain,
206 	.pr_protocol = IPPROTO_TCP,
207 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
208 	.pr_input = tcp6_input,
209 	.pr_ctlinput = tcp6_ctlinput,
210 	.pr_ctloutput = tcp_ctloutput,
211 	.pr_usrreq = tcp_usrreq,
212 #ifndef INET	/* don't call initialization and timeout routines twice */
213 	.pr_init = tcp_init,
214 	.pr_slowtimo = tcp_slowtimo,
215 	.pr_drain = tcp_drain,
216 #endif
217 },
218 {	.pr_type = SOCK_RAW,
219 	.pr_domain = &inet6domain,
220 	.pr_protocol = IPPROTO_RAW,
221 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
222 	.pr_input = rip6_input,
223 	.pr_output = rip6_output,
224 	.pr_ctlinput = rip6_ctlinput,
225 	.pr_ctloutput = rip6_ctloutput,
226 	.pr_usrreq = rip6_usrreq,
227 },
228 #ifdef GATEWAY
229 {	.pr_domain = &inet6domain,
230 	.pr_protocol = IPPROTO_IPV6,
231 	.pr_slowtimo = ip6flow_slowtimo,
232 },
233 #endif /* GATEWAY */
234 {	.pr_type = SOCK_RAW,
235 	.pr_domain = &inet6domain,
236 	.pr_protocol = IPPROTO_ICMPV6,
237 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
238 	.pr_input = icmp6_input,
239 	.pr_output = rip6_output,
240 	.pr_ctlinput = rip6_ctlinput,
241 	.pr_ctloutput = icmp6_ctloutput,
242 	.pr_usrreq = rip6_usrreq,
243 	.pr_init = icmp6_init,
244 },
245 {	.pr_type = SOCK_RAW,
246 	.pr_domain = &inet6domain,
247 	.pr_protocol = IPPROTO_DSTOPTS,
248 	.pr_flags = PR_ATOMIC|PR_ADDR,
249 	.pr_input = dest6_input,
250 },
251 {	.pr_type = SOCK_RAW,
252 	.pr_domain = &inet6domain,
253 	.pr_protocol = IPPROTO_ROUTING,
254 	.pr_flags = PR_ATOMIC|PR_ADDR,
255 	.pr_input = route6_input,
256 },
257 {	.pr_type = SOCK_RAW,
258 	.pr_domain = &inet6domain,
259 	.pr_protocol = IPPROTO_FRAGMENT,
260 	.pr_flags = PR_ATOMIC|PR_ADDR,
261 	.pr_input = frag6_input,
262 },
263 #ifdef IPSEC
264 {	.pr_type = SOCK_RAW,
265 	.pr_domain = &inet6domain,
266 	.pr_protocol = IPPROTO_AH,
267 	.pr_flags = PR_ATOMIC|PR_ADDR,
268 	.pr_input = ah6_input,
269 	.pr_ctlinput = ah6_ctlinput,
270 	.pr_init = ah6_init,
271 },
272 #ifdef IPSEC_ESP
273 {	.pr_type = SOCK_RAW,
274 	.pr_domain = &inet6domain,
275 	.pr_protocol = IPPROTO_ESP,
276 	.pr_flags = PR_ATOMIC|PR_ADDR,
277 	.pr_input = esp6_input,
278 	.pr_ctlinput = esp6_ctlinput,
279 	.pr_init = esp6_init,
280 },
281 #endif
282 {	.pr_type = SOCK_RAW,
283 	.pr_domain = &inet6domain,
284 	.pr_protocol = IPPROTO_IPCOMP,
285 	.pr_flags = PR_ATOMIC|PR_ADDR,
286 	.pr_input = ipcomp6_input,
287 	.pr_init = ipcomp6_init,
288 },
289 #endif /* IPSEC */
290 #ifdef FAST_IPSEC
291 {	.pr_type = SOCK_RAW,
292 	.pr_domain = &inet6domain,
293 	.pr_protocol = IPPROTO_AH,
294 	.pr_flags = PR_ATOMIC|PR_ADDR,
295 	.pr_input = ipsec6_common_input,
296 	.pr_ctlinput = ah6_ctlinput,
297 },
298 {	.pr_type = SOCK_RAW,
299 	.pr_domain = &inet6domain,
300 	.pr_protocol = IPPROTO_ESP,
301 	.pr_flags = PR_ATOMIC|PR_ADDR,
302 	.pr_input = ipsec6_common_input,
303 	.pr_ctlinput = esp6_ctlinput,
304 },
305 {	.pr_type = SOCK_RAW,
306 	.pr_domain = &inet6domain,
307 	.pr_protocol = IPPROTO_IPCOMP,
308 	.pr_flags = PR_ATOMIC|PR_ADDR,
309 	.pr_input = ipsec6_common_input,
310 },
311 #endif /* FAST_IPSEC */
312 #ifdef INET
313 {	.pr_type = SOCK_RAW,
314 	.pr_domain = &inet6domain,
315 	.pr_protocol = IPPROTO_IPV4,
316 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
317 	.pr_input = encap6_input,
318 	.pr_output = rip6_output,
319 	.pr_ctlinput = encap6_ctlinput,
320 	.pr_ctloutput = rip6_ctloutput,
321 	.pr_usrreq = rip6_usrreq,
322 	.pr_init = encap_init,
323 },
324 #endif
325 {	.pr_type = SOCK_RAW,
326 	.pr_domain = &inet6domain,
327 	.pr_protocol = IPPROTO_IPV6,
328 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
329 	.pr_input = encap6_input,
330 	.pr_output = rip6_output,
331 	.pr_ctlinput = encap6_ctlinput,
332 	.pr_ctloutput = rip6_ctloutput,
333 	.pr_usrreq = rip6_usrreq,
334 	.pr_init = encap_init,
335 },
336 #if NETHERIP > 1
337 {	.pr_type = SOCK_RAW,
338 	.pr_domain = &inet6domain,
339 	.pr_protocol = IPPROTO_ETHERIP,
340 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
341 	.pr_input = ip6_etherip_input,
342 	.pr_output = rip6_output,
343 	.pr_ctlinput = rip6_ctlinput,
344 	.pr_ctloutput = rip6_ctloutput,
345 	.pr_usrreq = rip6_usrreq,
346 },
347 #endif
348 #if NCARP > 0
349 {	.pr_type = SOCK_RAW,
350 	.pr_domain = &inet6domain,
351 	.pr_protocol = IPPROTO_CARP,
352 	.pr_flags = PR_ATOMIC|PR_ADDR,
353 	.pr_input = carp6_proto_input,
354 	.pr_output = rip6_output,
355 	.pr_ctloutput = rip6_ctloutput,
356 	.pr_usrreq = rip6_usrreq,
357 },
358 #endif /* NCARP */
359 #ifdef ISO
360 {	.pr_type = SOCK_RAW,
361 	.pr_domain = &inet6domain,
362 	.pr_protocol = IPPROTO_EON,
363 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
364 	.pr_input = encap6_input,
365 	.pr_output = rip6_output,
366 	.pr_ctlinput = encap6_ctlinput,
367 	.pr_ctloutput = rip6_ctloutput,
368 	.pr_usrreq = rip6_usrreq,
369 	/*XXX*/
370 	.pr_init = encap_init,
371 },
372 #endif
373 {	.pr_type = SOCK_RAW,
374 	.pr_domain = &inet6domain,
375 	.pr_protocol = IPPROTO_PIM,
376 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
377 	.pr_input = pim6_input,
378 	.pr_output = rip6_output,
379 	.pr_ctloutput = rip6_ctloutput,
380 	.pr_usrreq = rip6_usrreq,
381 	.pr_init = pim6_init,
382 },
383 /* raw wildcard */
384 {	.pr_type = SOCK_RAW,
385 	.pr_domain = &inet6domain,
386 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
387 	.pr_input = rip6_input,
388 	.pr_output = rip6_output,
389 	.pr_ctloutput = rip6_ctloutput,
390 	.pr_usrreq = rip6_usrreq,
391 	.pr_init = rip6_init,
392 },
393 };
394 
395 static const struct sockaddr_in6 in6_any = {
396 	  .sin6_len = sizeof(in6_any)
397 	, .sin6_family = AF_INET6
398 	, .sin6_port = 0
399 	, .sin6_flowinfo = 0
400 	, .sin6_addr = IN6ADDR_ANY_INIT
401 	, .sin6_scope_id = 0
402 };
403 
404 struct domain inet6domain = {
405 	.dom_family = AF_INET6, .dom_name = "internet6",
406 	.dom_init = NULL, .dom_externalize = NULL, .dom_dispose = NULL,
407 	.dom_protosw = (const struct protosw *)inet6sw,
408 	.dom_protoswNPROTOSW = (const struct protosw *)&inet6sw[sizeof(inet6sw)/sizeof(inet6sw[0])],
409 	.dom_rtattach = rn_inithead,
410 	.dom_rtoffset = offsetof(struct sockaddr_in6, sin6_addr) << 3,
411 	.dom_maxrtkey = sizeof(struct ip_pack6),
412 	.dom_ifattach = in6_domifattach, .dom_ifdetach = in6_domifdetach,
413 	.dom_ifqueues = { &ip6intrq, NULL },
414 	.dom_link = { NULL },
415 	.dom_mowner = MOWNER_INIT("",""),
416 	.dom_sa_cmpofs = offsetof(struct sockaddr_in6, sin6_addr),
417 	.dom_sa_cmplen = sizeof(struct in6_addr),
418 	.dom_sa_any = (const struct sockaddr *)&in6_any,
419 	.dom_rtcache = LIST_HEAD_INITIALIZER(inet6domain.dom_rtcache)
420 };
421 
422 int
423 sockaddr_in6_cmp(const struct sockaddr *lsa, const struct sockaddr *rsa)
424 {
425 	uint_fast8_t len;
426 	const uint_fast8_t addrofs = offsetof(struct sockaddr_in6, sin6_addr),
427 			   addrend = addrofs + sizeof(struct in6_addr);
428 	int rc;
429 	const struct sockaddr_in6 *lsin6, *rsin6;
430 
431 	lsin6 = satocsin6(lsa);
432 	rsin6 = satocsin6(rsa);
433 
434 	len = MIN(addrend, MIN(lsin6->sin6_len, rsin6->sin6_len));
435 
436 	if (len > addrofs &&
437 	    (rc = memcmp(&lsin6->sin6_addr, &rsin6->sin6_addr,
438 	                  len - addrofs)) != 0)
439 		return rc;
440 
441 	return lsin6->sin6_len - rsin6->sin6_len;
442 }
443 
444 /*
445  * Internet configuration info
446  */
447 #ifndef	IPV6FORWARDING
448 #ifdef GATEWAY6
449 #define	IPV6FORWARDING	1	/* forward IP6 packets not for us */
450 #else
451 #define	IPV6FORWARDING	0	/* don't forward IP6 packets not for us */
452 #endif /* GATEWAY6 */
453 #endif /* !IPV6FORWARDING */
454 
455 int	ip6_forwarding = IPV6FORWARDING;	/* act as router? */
456 int	ip6_sendredirects = 1;
457 int	ip6_defhlim = IPV6_DEFHLIM;
458 int	ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
459 int	ip6_accept_rtadv = 0;	/* "IPV6FORWARDING ? 0 : 1" is dangerous */
460 int	ip6_maxfragpackets = 200;
461 int	ip6_maxfrags = 200;
462 int	ip6_log_interval = 5;
463 int	ip6_hdrnestlimit = 50;	/* appropriate? */
464 int	ip6_dad_count = 1;	/* DupAddrDetectionTransmits */
465 int	ip6_auto_flowlabel = 1;
466 int	ip6_use_deprecated = 1;	/* allow deprecated addr (RFC2462 5.5.4) */
467 int	ip6_rr_prune = 5;	/* router renumbering prefix
468 				 * walk list every 5 sec. */
469 int	ip6_mcast_pmtu = 0;	/* enable pMTU discovery for multicast? */
470 int	ip6_v6only = 1;
471 
472 int	ip6_keepfaith = 0;
473 time_t	ip6_log_time = (time_t)0L;
474 
475 /* icmp6 */
476 /*
477  * BSDI4 defines these variables in in_proto.c...
478  * XXX: what if we don't define INET? Should we define pmtu6_expire
479  * or so? (jinmei@kame.net 19990310)
480  */
481 int pmtu_expire = 60*10;
482 
483 /* raw IP6 parameters */
484 /*
485  * Nominal space allocated to a raw ip socket.
486  */
487 #define	RIPV6SNDQ	8192
488 #define	RIPV6RCVQ	8192
489 
490 u_long	rip6_sendspace = RIPV6SNDQ;
491 u_long	rip6_recvspace = RIPV6RCVQ;
492 
493 /* ICMPV6 parameters */
494 int	icmp6_rediraccept = 1;		/* accept and process redirects */
495 int	icmp6_redirtimeout = 10 * 60;	/* 10 minutes */
496 int	icmp6errppslim = 100;		/* 100pps */
497 int	icmp6_nodeinfo = 1;		/* enable/disable NI response */
498 
499 /* UDP on IP6 parameters */
500 int	udp6_sendspace = 9216;		/* really max datagram size */
501 int	udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
502 					/* 40 1K datagrams */
503