1/* $NetBSD: in6_proto.c,v 1.113 2016/07/06 10:49:49 ozaki-r 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.113 2016/07/06 10:49:49 ozaki-r Exp $");
66
67#ifdef _KERNEL_OPT
68#include "opt_gateway.h"
69#include "opt_inet.h"
70#include "opt_ipsec.h"
71#include "opt_dccp.h"
72#include "opt_sctp.h"
73#endif
74
75#include <sys/param.h>
76#include <sys/socket.h>
77#include <sys/protosw.h>
78#include <sys/kernel.h>
79#include <sys/domain.h>
80#include <sys/mbuf.h>
81
82#include <net/if.h>
83
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/in_var.h>
87#include <netinet/ip_encap.h>
88#include <netinet/ip.h>
89#include <netinet/ip_var.h>
90#include <netinet/in_pcb.h>
91#include <netinet/ip6.h>
92#include <netinet6/ip6_var.h>
93#include <netinet/icmp6.h>
94#include <netinet6/in6_pcb.h>
95
96#include <netinet/tcp.h>
97#include <netinet/tcp_fsm.h>
98#include <netinet/tcp_seq.h>
99#include <netinet/tcp_timer.h>
100#include <netinet/tcp_var.h>
101#include <netinet/tcpip.h>
102#include <netinet/tcp_debug.h>
103
104#include <netinet6/udp6.h>
105#include <netinet6/udp6_var.h>
106
107#ifdef DCCP
108#include <netinet/dccp.h>
109#include <netinet/dccp_var.h>
110#include <netinet6/dccp6_var.h>
111#endif
112
113#ifdef SCTP
114#include <netinet/sctp_pcb.h>
115#include <netinet/sctp.h>
116#include <netinet/sctp_var.h>
117#include <netinet6/sctp6_var.h>
118#endif
119
120#include <netinet6/pim6_var.h>
121
122#include <netinet6/nd6.h>
123
124#ifdef IPSEC
125#include <netipsec/ipsec.h>
126#include <netipsec/ipsec6.h>
127#include <netipsec/key.h>
128#endif /* IPSEC */
129
130
131#include "carp.h"
132#if NCARP > 0
133#include <netinet/ip_carp.h>
134#endif
135
136#include "etherip.h"
137#if NETHERIP > 0
138#include <netinet6/ip6_etherip.h>
139#endif
140
141#include <netinet6/ip6protosw.h>
142
143#include <net/net_osdep.h>
144
145/*
146 * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
147 */
148
149DOMAIN_DEFINE(inet6domain); /* forward declare and add to link set */
150
151/* Wrappers to acquire kernel_lock. */
152
153PR_WRAP_CTLINPUT(rip6_ctlinput)
154PR_WRAP_CTLINPUT(encap6_ctlinput)
155PR_WRAP_CTLINPUT(udp6_ctlinput)
156PR_WRAP_CTLINPUT(tcp6_ctlinput)
157
158#define rip6_ctlinput rip6_ctlinput_wrapper
159#define encap6_ctlinput encap6_ctlinput_wrapper
160#define udp6_ctlinput udp6_ctlinput_wrapper
161#define tcp6_ctlinput tcp6_ctlinput_wrapper
162
163PR_WRAP_CTLOUTPUT(rip6_ctloutput)
164PR_WRAP_CTLOUTPUT(tcp_ctloutput)
165PR_WRAP_CTLOUTPUT(udp6_ctloutput)
166PR_WRAP_CTLOUTPUT(icmp6_ctloutput)
167
168#define rip6_ctloutput rip6_ctloutput_wrapper
169#define tcp_ctloutput tcp_ctloutput_wrapper
170#define udp6_ctloutput udp6_ctloutput_wrapper
171#define icmp6_ctloutput icmp6_ctloutput_wrapper
172
173#if defined(DCCP)
174PR_WRAP_CTLINPUT(dccp6_ctlinput)
175PR_WRAP_CTLOUTPUT(dccp_ctloutput)
176
177#define dccp6_ctlinput dccp6_ctlinput_wrapper
178#define dccp_ctloutput dccp_ctloutput_wrapper
179#endif
180
181#if defined(SCTP)
182PR_WRAP_CTLINPUT(sctp6_ctlinput)
183PR_WRAP_CTLOUTPUT(sctp_ctloutput)
184
185#define sctp6_ctlinput sctp6_ctlinput_wrapper
186#define sctp_ctloutput sctp_ctloutput_wrapper
187#endif
188
189#if defined(IPSEC)
190PR_WRAP_CTLINPUT(ah6_ctlinput)
191
192#define ah6_ctlinput ah6_ctlinput_wrapper
193#endif
194
195#if defined(IPSEC)
196PR_WRAP_CTLINPUT(esp6_ctlinput)
197
198#define esp6_ctlinput esp6_ctlinput_wrapper
199#endif
200
201static void
202tcp6_init(void)
203{
204
205 icmp6_mtudisc_callback_register(tcp6_mtudisc_callback);
206
207 tcp_init_common(sizeof(struct ip6_hdr));
208}
209
210const struct ip6protosw inet6sw[] = {
211{ .pr_domain = &inet6domain,
212 .pr_protocol = IPPROTO_IPV6,
213 .pr_init = ip6_init,
214 .pr_fasttimo = frag6_fasttimo,
215 .pr_slowtimo = frag6_slowtimo,
216 .pr_drain = frag6_drainstub,
217},
218{ .pr_type = SOCK_DGRAM,
219 .pr_domain = &inet6domain,
220 .pr_protocol = IPPROTO_UDP,
221 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
222 .pr_input = udp6_input,
223 .pr_ctlinput = udp6_ctlinput,
224 .pr_ctloutput = udp6_ctloutput,
225 .pr_usrreqs = &udp6_usrreqs,
226 .pr_init = udp6_init,
227},
228{ .pr_type = SOCK_STREAM,
229 .pr_domain = &inet6domain,
230 .pr_protocol = IPPROTO_TCP,
231 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
232 .pr_input = tcp6_input,
233 .pr_ctlinput = tcp6_ctlinput,
234 .pr_ctloutput = tcp_ctloutput,
235 .pr_usrreqs = &tcp_usrreqs,
236 .pr_init = tcp6_init,
237 .pr_fasttimo = tcp_fasttimo,
238 .pr_drain = tcp_drainstub,
239},
240#ifdef DCCP
241{ .pr_type = SOCK_CONN_DGRAM,
242 .pr_domain = &inet6domain,
243 .pr_protocol = IPPROTO_DCCP,
244 .pr_flags = PR_CONNREQUIRED|PR_ATOMIC|PR_LISTEN,
245 .pr_input = dccp6_input,
246 .pr_ctlinput = dccp6_ctlinput,
247 .pr_ctloutput = dccp_ctloutput,
248 .pr_usrreqs = &dccp6_usrreqs,
249#ifndef INET
250 .pr_init = dccp_init,
251#endif
252},
253#endif /* DCCP */
254#ifdef SCTP
255{ .pr_type = SOCK_DGRAM,
256 .pr_domain = &inet6domain,
257 .pr_protocol = IPPROTO_SCTP,
258 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
259 .pr_input = sctp6_input,
260 .pr_ctlinput = sctp6_ctlinput,
261 .pr_ctloutput = sctp_ctloutput,
262 .pr_usrreqs = &sctp6_usrreqs,
263 .pr_drain = sctp_drain,
264},
265{ .pr_type = SOCK_SEQPACKET,
266 .pr_domain = &inet6domain,
267 .pr_protocol = IPPROTO_SCTP,
268 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
269 .pr_input = sctp6_input,
270 .pr_ctlinput = sctp6_ctlinput,
271 .pr_ctloutput = sctp_ctloutput,
272 .pr_drain = sctp_drain,
273},
274{ .pr_type = SOCK_STREAM,
275 .pr_domain = &inet6domain,
276 .pr_protocol = IPPROTO_SCTP,
277 .pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD|PR_LISTEN,
278 .pr_input = sctp6_input,
279 .pr_ctlinput = sctp6_ctlinput,
280 .pr_ctloutput = sctp_ctloutput,
281 .pr_drain = sctp_drain,
282},
283#endif /* SCTP */
284{ .pr_type = SOCK_RAW,
285 .pr_domain = &inet6domain,
286 .pr_protocol = IPPROTO_RAW,
287 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
288 .pr_input = rip6_input,
289 .pr_ctlinput = rip6_ctlinput,
290 .pr_ctloutput = rip6_ctloutput,
291 .pr_usrreqs = &rip6_usrreqs,
292},
293#ifdef GATEWAY
294{ .pr_domain = &inet6domain,
295 .pr_protocol = IPPROTO_IPV6,
296 .pr_slowtimo = ip6flow_slowtimo,
297 .pr_init = ip6flow_poolinit,
298},
299#endif /* GATEWAY */
300{ .pr_type = SOCK_RAW,
301 .pr_domain = &inet6domain,
302 .pr_protocol = IPPROTO_ICMPV6,
303 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
304 .pr_input = icmp6_input,
305 .pr_ctlinput = rip6_ctlinput,
306 .pr_ctloutput = icmp6_ctloutput,
307 .pr_usrreqs = &rip6_usrreqs,
308 .pr_init = icmp6_init,
309},
310{ .pr_type = SOCK_RAW,
311 .pr_domain = &inet6domain,
312 .pr_protocol = IPPROTO_DSTOPTS,
313 .pr_flags = PR_ATOMIC|PR_ADDR,
314 .pr_input = dest6_input,
315},
316{ .pr_type = SOCK_RAW,
317 .pr_domain = &inet6domain,
318 .pr_protocol = IPPROTO_ROUTING,
319 .pr_flags = PR_ATOMIC|PR_ADDR,
320 .pr_input = route6_input,
321},
322{ .pr_type = SOCK_RAW,
323 .pr_domain = &inet6domain,
324 .pr_protocol = IPPROTO_FRAGMENT,
325 .pr_flags = PR_ATOMIC|PR_ADDR,
326 .pr_input = frag6_input,
327},
328#ifdef IPSEC
329{ .pr_type = SOCK_RAW,
330 .pr_domain = &inet6domain,
331 .pr_protocol = IPPROTO_AH,
332 .pr_flags = PR_ATOMIC|PR_ADDR,
333 .pr_input = ipsec6_common_input,
334 .pr_ctlinput = ah6_ctlinput,
335},
336{ .pr_type = SOCK_RAW,
337 .pr_domain = &inet6domain,
338 .pr_protocol = IPPROTO_ESP,
339 .pr_flags = PR_ATOMIC|PR_ADDR,
340 .pr_input = ipsec6_common_input,
341 .pr_ctlinput = esp6_ctlinput,
342},
343{ .pr_type = SOCK_RAW,
344 .pr_domain = &inet6domain,
345 .pr_protocol = IPPROTO_IPCOMP,
346 .pr_flags = PR_ATOMIC|PR_ADDR,
347 .pr_input = ipsec6_common_input,
348},
349#endif /* IPSEC */
350#ifdef INET
351{ .pr_type = SOCK_RAW,
352 .pr_domain = &inet6domain,
353 .pr_protocol = IPPROTO_IPV4,
354 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
355 .pr_input = encap6_input,
356 .pr_ctlinput = encap6_ctlinput,
357 .pr_ctloutput = rip6_ctloutput,
358 .pr_usrreqs = &rip6_usrreqs,
359 .pr_init = encap_init,
360},
361#endif
362{ .pr_type = SOCK_RAW,
363 .pr_domain = &inet6domain,
364 .pr_protocol = IPPROTO_IPV6,
365 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
366 .pr_input = encap6_input,
367 .pr_ctlinput = encap6_ctlinput,
368 .pr_ctloutput = rip6_ctloutput,
369 .pr_usrreqs = &rip6_usrreqs,
370 .pr_init = encap_init,
371},
372#if NETHERIP > 0
373{ .pr_type = SOCK_RAW,
374 .pr_domain = &inet6domain,
375 .pr_protocol = IPPROTO_ETHERIP,
376 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
377 .pr_input = ip6_etherip_input,
378 .pr_ctlinput = rip6_ctlinput,
379 .pr_ctloutput = rip6_ctloutput,
380 .pr_usrreqs = &rip6_usrreqs,
381},
382#endif
383#if NCARP > 0
384{ .pr_type = SOCK_RAW,
385 .pr_domain = &inet6domain,
386 .pr_protocol = IPPROTO_CARP,
387 .pr_flags = PR_ATOMIC|PR_ADDR,
388 .pr_input = carp6_proto_input,
389 .pr_ctloutput = rip6_ctloutput,
390 .pr_usrreqs = &rip6_usrreqs,
391},
392#endif /* NCARP */
393{ .pr_type = SOCK_RAW,
394 .pr_domain = &inet6domain,
395 .pr_protocol = IPPROTO_PIM,
396 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
397 .pr_input = pim6_input,
398 .pr_ctloutput = rip6_ctloutput,
399 .pr_usrreqs = &rip6_usrreqs,
400 .pr_init = pim6_init,
401},
402/* raw wildcard */
403{ .pr_type = SOCK_RAW,
404 .pr_domain = &inet6domain,
405 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
406 .pr_input = rip6_input,
407 .pr_ctloutput = rip6_ctloutput,
408 .pr_usrreqs = &rip6_usrreqs,
409 .pr_init = rip6_init,
410},
411};
412
413static const struct sockaddr_in6 in6_any = {
414 .sin6_len = sizeof(in6_any)
415 , .sin6_family = AF_INET6
416 , .sin6_port = 0
417 , .sin6_flowinfo = 0
418 , .sin6_addr = IN6ADDR_ANY_INIT
419 , .sin6_scope_id = 0
420};
421
422bool in6_present = false;
423static void
424in6_dom_init(void)
425{
426
427 in6_present = true;
428}
429
430struct domain inet6domain = {
431 .dom_family = AF_INET6, .dom_name = "internet6",
432 .dom_init = in6_dom_init, .dom_externalize = NULL, .dom_dispose = NULL,
433 .dom_protosw = (const struct protosw *)inet6sw,
434 .dom_protoswNPROTOSW = (const struct protosw *)&inet6sw[sizeof(inet6sw)/sizeof(inet6sw[0])],
435 .dom_rtattach = rt_inithead,
436 .dom_rtoffset = offsetof(struct sockaddr_in6, sin6_addr) << 3,
437 .dom_maxrtkey = sizeof(struct ip_pack6),
438 .dom_if_up = in6_if_up, .dom_if_down = in6_if_down,
439 .dom_ifattach = in6_domifattach, .dom_ifdetach = in6_domifdetach,
440 .dom_if_link_state_change = in6_if_link_state_change,
441 .dom_ifqueues = { NULL, NULL },
442 .dom_link = { NULL },
443 .dom_mowner = MOWNER_INIT("",""),
444 .dom_sa_cmpofs = offsetof(struct sockaddr_in6, sin6_addr),
445 .dom_sa_cmplen = sizeof(struct in6_addr),
446 .dom_sa_any = (const struct sockaddr *)&in6_any,
447 .dom_sockaddr_externalize = sockaddr_in6_externalize,
448 .dom_rtcache = LIST_HEAD_INITIALIZER(inet6domain.dom_rtcache)
449};
450
451#if 0
452int
453sockaddr_in6_cmp(const struct sockaddr *lsa, const struct sockaddr *rsa)
454{
455 uint_fast8_t len;
456 const uint_fast8_t addrofs = offsetof(struct sockaddr_in6, sin6_addr),
457 addrend = addrofs + sizeof(struct in6_addr);
458 int rc;
459 const struct sockaddr_in6 *lsin6, *rsin6;
460
461 lsin6 = satocsin6(lsa);
462 rsin6 = satocsin6(rsa);
463
464 len = MIN(addrend, MIN(lsin6->sin6_len, rsin6->sin6_len));
465
466 if (len > addrofs &&
467 (rc = memcmp(&lsin6->sin6_addr, &rsin6->sin6_addr,
468 len - addrofs)) != 0)
469 return rc;
470
471 return lsin6->sin6_len - rsin6->sin6_len;
472}
473#endif
474
475/*
476 * Internet configuration info
477 */
478#ifndef IPV6FORWARDING
479#ifdef GATEWAY6
480#define IPV6FORWARDING 1 /* forward IP6 packets not for us */
481#else
482#define IPV6FORWARDING 0 /* don't forward IP6 packets not for us */
483#endif /* GATEWAY6 */
484#endif /* !IPV6FORWARDING */
485
486int ip6_forwarding = IPV6FORWARDING; /* act as router? */
487int ip6_sendredirects = 1;
488int ip6_defhlim = IPV6_DEFHLIM;
489int ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
490int ip6_accept_rtadv = 0; /* "IPV6FORWARDING ? 0 : 1" is dangerous */
491int ip6_maxfragpackets = 200;
492int ip6_maxfrags = 200;
493int ip6_log_interval = 5;
494int ip6_hdrnestlimit = 50; /* appropriate? */
495int ip6_dad_count = 1; /* DupAddrDetectionTransmits */
496int ip6_auto_flowlabel = 1;
497int ip6_use_deprecated = 1; /* allow deprecated addr (RFC2462 5.5.4) */
498int ip6_rr_prune = 5; /* router renumbering prefix
499 * walk list every 5 sec. */
500int ip6_mcast_pmtu = 0; /* enable pMTU discovery for multicast? */
501int ip6_v6only = 1;
502int ip6_neighborgcthresh = 2048; /* Threshold # of NDP entries for GC */
503int ip6_maxifprefixes = 16; /* Max acceptable prefixes via RA per IF */
504int ip6_maxifdefrouters = 16; /* Max acceptable def routers via RA */
505int ip6_maxdynroutes = 4096; /* Max # of routes created via redirect */
506
507int ip6_keepfaith = 0;
508time_t ip6_log_time = 0;
509int ip6_rtadv_maxroutes = 100; /* (arbitrary) initial maximum number of
510 * routes via rtadv expected to be
511 * significantly larger than common use.
512 * if you need to count: 3 extra initial
513 * routes, plus 1 per interface after the
514 * first one, then one per non-linklocal
515 * prefix */
516
517/* icmp6 */
518/*
519 * BSDI4 defines these variables in in_proto.c...
520 * XXX: what if we don't define INET? Should we define pmtu6_expire
521 * or so? (jinmei@kame.net 19990310)
522 */
523int pmtu_expire = 60*10;
524
525/* raw IP6 parameters */
526/*
527 * Nominal space allocated to a raw ip socket.
528 */
529#define RIPV6SNDQ 8192
530#define RIPV6RCVQ 8192
531
532u_long rip6_sendspace = RIPV6SNDQ;
533u_long rip6_recvspace = RIPV6RCVQ;
534
535/* ICMPV6 parameters */
536int icmp6_rediraccept = 1; /* accept and process redirects */
537int icmp6_redirtimeout = 10 * 60; /* 10 minutes */
538int icmp6errppslim = 100; /* 100pps */
539int icmp6_nodeinfo = 1; /* enable/disable NI response */
540