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