1 /* $NetBSD: in_proto.c,v 1.115 2015/10/13 21:28:35 rjs 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.115 2015/10/13 21:28:35 rjs 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_output = ip_output, 203 .pr_fasttimo = ip_fasttimo, 204 .pr_slowtimo = ip_slowtimo, 205 .pr_drain = ip_drainstub, 206 }, 207 { .pr_type = SOCK_DGRAM, 208 .pr_domain = &inetdomain, 209 .pr_protocol = IPPROTO_UDP, 210 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF, 211 .pr_input = udp_input, 212 .pr_ctlinput = udp_ctlinput, 213 .pr_ctloutput = udp_ctloutput, 214 .pr_usrreqs = &udp_usrreqs, 215 .pr_init = udp_init, 216 }, 217 { .pr_type = SOCK_STREAM, 218 .pr_domain = &inetdomain, 219 .pr_protocol = IPPROTO_TCP, 220 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF, 221 .pr_input = tcp_input, 222 .pr_ctlinput = tcp_ctlinput, 223 .pr_ctloutput = tcp_ctloutput, 224 .pr_usrreqs = &tcp_usrreqs, 225 .pr_init = tcp_init, 226 .pr_fasttimo = tcp_fasttimo, 227 .pr_drain = tcp_drainstub, 228 }, 229 #ifdef DCCP 230 { .pr_type = SOCK_CONN_DGRAM, 231 .pr_domain = &inetdomain, 232 .pr_protocol = IPPROTO_DCCP, 233 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ATOMIC|PR_LISTEN|PR_ABRTACPTDIS, 234 .pr_input = dccp_input, 235 .pr_ctlinput = dccp_ctlinput, 236 .pr_ctloutput = dccp_ctloutput, 237 .pr_usrreqs = &dccp_usrreqs, 238 .pr_init = dccp_init, 239 }, 240 #endif 241 #ifdef SCTP 242 { .pr_type = SOCK_DGRAM, 243 .pr_domain = &inetdomain, 244 .pr_protocol = IPPROTO_SCTP, 245 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD, 246 .pr_input = sctp_input, 247 .pr_ctlinput = sctp_ctlinput, 248 .pr_ctloutput = sctp_ctloutput, 249 .pr_usrreqs = &sctp_usrreqs, 250 .pr_init = sctp_init, 251 .pr_drain = sctp_drain 252 }, 253 { .pr_type = SOCK_SEQPACKET, 254 .pr_domain = &inetdomain, 255 .pr_protocol = IPPROTO_SCTP, 256 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD, 257 .pr_input = sctp_input, 258 .pr_ctlinput = sctp_ctlinput, 259 .pr_ctloutput = sctp_ctloutput, 260 .pr_usrreqs = &sctp_usrreqs, 261 .pr_drain = sctp_drain 262 }, 263 { .pr_type = SOCK_STREAM, 264 .pr_domain = &inetdomain, 265 .pr_protocol = IPPROTO_SCTP, 266 .pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD|PR_LISTEN, 267 .pr_input = sctp_input, 268 .pr_ctlinput = sctp_ctlinput, 269 .pr_ctloutput = sctp_ctloutput, 270 .pr_usrreqs = &sctp_usrreqs, 271 .pr_drain = sctp_drain 272 }, 273 #endif /* SCTP */ 274 { .pr_type = SOCK_RAW, 275 .pr_domain = &inetdomain, 276 .pr_protocol = IPPROTO_RAW, 277 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF, 278 .pr_input = rip_input, 279 .pr_output = rip_output, 280 .pr_ctlinput = rip_ctlinput, 281 .pr_ctloutput = rip_ctloutput, 282 .pr_usrreqs = &rip_usrreqs, 283 }, 284 { .pr_type = SOCK_RAW, 285 .pr_domain = &inetdomain, 286 .pr_protocol = IPPROTO_ICMP, 287 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 288 .pr_input = icmp_input, 289 .pr_output = rip_output, 290 .pr_ctlinput = rip_ctlinput, 291 .pr_ctloutput = rip_ctloutput, 292 .pr_usrreqs = &rip_usrreqs, 293 .pr_init = icmp_init, 294 }, 295 #ifdef GATEWAY 296 { .pr_domain = &inetdomain, 297 .pr_protocol = IPPROTO_IP, 298 .pr_slowtimo = ipflow_slowtimo, 299 .pr_init = ipflow_poolinit, 300 }, 301 #endif /* GATEWAY */ 302 #ifdef IPSEC 303 { .pr_type = SOCK_RAW, 304 .pr_domain = &inetdomain, 305 .pr_protocol = IPPROTO_AH, 306 .pr_flags = PR_ATOMIC|PR_ADDR, 307 .pr_input = ipsec4_common_input, 308 .pr_ctlinput = ah4_ctlinput, 309 }, 310 { .pr_type = SOCK_RAW, 311 .pr_domain = &inetdomain, 312 .pr_protocol = IPPROTO_ESP, 313 .pr_flags = PR_ATOMIC|PR_ADDR, 314 .pr_input = ipsec4_common_input, 315 .pr_ctlinput = esp4_ctlinput, 316 }, 317 { .pr_type = SOCK_RAW, 318 .pr_domain = &inetdomain, 319 .pr_protocol = IPPROTO_IPCOMP, 320 .pr_flags = PR_ATOMIC|PR_ADDR, 321 .pr_input = ipsec4_common_input, 322 }, 323 #endif /* IPSEC */ 324 { .pr_type = SOCK_RAW, 325 .pr_domain = &inetdomain, 326 .pr_protocol = IPPROTO_IPV4, 327 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 328 .pr_input = encap4_input, 329 .pr_output = rip_output, 330 .pr_ctlinput = rip_ctlinput, 331 .pr_ctloutput = rip_ctloutput, 332 .pr_usrreqs = &rip_usrreqs, 333 .pr_init = encap_init, 334 }, 335 #ifdef INET6 336 { .pr_type = SOCK_RAW, 337 .pr_domain = &inetdomain, 338 .pr_protocol = IPPROTO_IPV6, 339 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 340 .pr_input = encap4_input, 341 .pr_output = rip_output, 342 .pr_ctlinput = rip_ctlinput, 343 .pr_ctloutput = rip_ctloutput, 344 .pr_usrreqs = &rip_usrreqs, 345 .pr_init = encap_init, 346 }, 347 #endif /* INET6 */ 348 #if NETHERIP > 0 349 { .pr_type = SOCK_RAW, 350 .pr_domain = &inetdomain, 351 .pr_protocol = IPPROTO_ETHERIP, 352 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 353 .pr_input = ip_etherip_input, 354 .pr_output = rip_output, 355 .pr_ctlinput = rip_ctlinput, 356 .pr_ctloutput = rip_ctloutput, 357 .pr_usrreqs = &rip_usrreqs, 358 }, 359 #endif /* NETHERIP > 0 */ 360 #if NCARP > 0 361 { .pr_type = SOCK_RAW, 362 .pr_domain = &inetdomain, 363 .pr_protocol = IPPROTO_CARP, 364 .pr_flags = PR_ATOMIC|PR_ADDR, 365 .pr_input = carp_proto_input, 366 .pr_output = rip_output, 367 .pr_ctloutput = rip_ctloutput, 368 .pr_usrreqs = &rip_usrreqs, 369 .pr_init = carp_init, 370 }, 371 #endif /* NCARP > 0 */ 372 #if NPFSYNC > 0 373 { .pr_type = SOCK_RAW, 374 .pr_domain = &inetdomain, 375 .pr_protocol = IPPROTO_PFSYNC, 376 .pr_flags = PR_ATOMIC|PR_ADDR, 377 .pr_input = pfsync_input, 378 .pr_output = rip_output, 379 .pr_ctloutput = rip_ctloutput, 380 .pr_usrreqs = &rip_usrreqs, 381 }, 382 #endif /* NPFSYNC > 0 */ 383 { .pr_type = SOCK_RAW, 384 .pr_domain = &inetdomain, 385 .pr_protocol = IPPROTO_IGMP, 386 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 387 .pr_input = igmp_input, 388 .pr_output = rip_output, 389 .pr_ctloutput = rip_ctloutput, 390 .pr_ctlinput = rip_ctlinput, 391 .pr_usrreqs = &rip_usrreqs, 392 .pr_fasttimo = igmp_fasttimo, 393 .pr_slowtimo = igmp_slowtimo, 394 .pr_init = igmp_init, 395 }, 396 #ifdef PIM 397 { .pr_type = SOCK_RAW, 398 .pr_domain = &inetdomain, 399 .pr_protocol = IPPROTO_PIM, 400 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 401 .pr_input = pim_input, 402 .pr_output = rip_output, 403 .pr_ctloutput = rip_ctloutput, 404 .pr_ctlinput = rip_ctlinput, 405 .pr_usrreqs = &rip_usrreqs, 406 }, 407 #endif /* PIM */ 408 /* raw wildcard */ 409 { .pr_type = SOCK_RAW, 410 .pr_domain = &inetdomain, 411 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 412 .pr_input = rip_input, 413 .pr_output = rip_output, 414 .pr_ctloutput = rip_ctloutput, 415 .pr_ctlinput = rip_ctlinput, 416 .pr_usrreqs = &rip_usrreqs, 417 .pr_init = rip_init, 418 }, 419 }; 420 421 const struct sockaddr_in in_any = { 422 .sin_len = sizeof(struct sockaddr_in) 423 , .sin_family = AF_INET 424 , .sin_port = 0 425 , .sin_addr = {.s_addr = 0 /* INADDR_ANY */} 426 }; 427 428 struct domain inetdomain = { 429 .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL, 430 .dom_externalize = NULL, .dom_dispose = NULL, 431 .dom_protosw = inetsw, 432 .dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)], 433 .dom_rtattach = rt_inithead, 434 .dom_rtoffset = 32, 435 .dom_maxrtkey = sizeof(struct ip_pack4), 436 .dom_if_up = in_if_up, 437 .dom_if_down = in_if_down, 438 .dom_ifattach = in_domifattach, 439 .dom_ifdetach = in_domifdetach, 440 .dom_if_link_state_change = in_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_in, sin_addr), 445 .dom_sa_cmplen = sizeof(struct in_addr), 446 .dom_sa_any = (const struct sockaddr *)&in_any, 447 .dom_sockaddr_const_addr = sockaddr_in_const_addr, 448 .dom_sockaddr_addr = sockaddr_in_addr, 449 .dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache) 450 }; 451 452 u_char ip_protox[IPPROTO_MAX]; 453 454 int icmperrppslim = 100; /* 100pps */ 455 456 static void 457 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp) 458 { 459 socklen_t slen; 460 461 if (slenp == NULL) 462 return; 463 464 slen = sockaddr_getlen(sa); 465 *slenp = (socklen_t)MIN(sizeof(struct in_addr), 466 slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr))); 467 } 468 469 const void * 470 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp) 471 { 472 const struct sockaddr_in *sin; 473 474 sockaddr_in_addrlen(sa, slenp); 475 sin = (const struct sockaddr_in *)sa; 476 return &sin->sin_addr; 477 } 478 479 void * 480 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp) 481 { 482 struct sockaddr_in *sin; 483 484 sockaddr_in_addrlen(sa, slenp); 485 sin = (struct sockaddr_in *)sa; 486 return &sin->sin_addr; 487 } 488 489 int 490 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2) 491 { 492 uint_fast8_t len; 493 const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr), 494 addrend = addrofs + sizeof(struct in_addr); 495 int rc; 496 const struct sockaddr_in *sin1, *sin2; 497 498 sin1 = satocsin(sa1); 499 sin2 = satocsin(sa2); 500 501 len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len)); 502 503 if (len > addrofs && 504 (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr, 505 len - addrofs)) != 0) 506 return rc; 507 508 return sin1->sin_len - sin2->sin_len; 509 } 510