1 /* $NetBSD: in_proto.c,v 1.101 2011/05/03 17:44:31 dyoung 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.101 2011/05/03 17:44:31 dyoung Exp $"); 65 66 #include "opt_mrouting.h" 67 #include "opt_eon.h" /* ISO CLNL over IP */ 68 #include "opt_iso.h" /* ISO TP tunneled over IP */ 69 #include "opt_inet.h" 70 #include "opt_ipsec.h" 71 #include "opt_pim.h" 72 #include "opt_gateway.h" 73 74 #include <sys/param.h> 75 #include <sys/socket.h> 76 #include <sys/protosw.h> 77 #include <sys/domain.h> 78 #include <sys/mbuf.h> 79 80 #include <net/if.h> 81 #include <net/radix.h> 82 #include <net/route.h> 83 84 #include <netinet/in.h> 85 #include <netinet/in_systm.h> 86 #include <netinet/ip.h> 87 #include <netinet/ip_var.h> 88 #include <netinet/ip_icmp.h> 89 #include <netinet/in_ifattach.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/in_proto.h> 92 93 #ifdef INET6 94 #ifndef INET 95 #include <netinet/in.h> 96 #endif 97 #include <netinet/ip6.h> 98 #endif 99 100 #include <netinet/igmp_var.h> 101 #ifdef PIM 102 #include <netinet/pim_var.h> 103 #endif 104 #include <netinet/tcp.h> 105 #include <netinet/tcp_fsm.h> 106 #include <netinet/tcp_seq.h> 107 #include <netinet/tcp_timer.h> 108 #include <netinet/tcp_var.h> 109 #include <netinet/tcpip.h> 110 #include <netinet/tcp_debug.h> 111 #include <netinet/udp.h> 112 #include <netinet/udp_var.h> 113 #include <netinet/ip_encap.h> 114 115 /* 116 * TCP/IP protocol family: IP, ICMP, UDP, TCP. 117 */ 118 119 #ifdef IPSEC 120 #include <netinet6/ipsec.h> 121 #include <netinet6/ah.h> 122 #ifdef IPSEC_ESP 123 #include <netinet6/esp.h> 124 #endif 125 #include <netinet6/ipcomp.h> 126 #endif /* IPSEC */ 127 128 #ifdef FAST_IPSEC 129 #include <netipsec/ipsec.h> 130 #include <netipsec/key.h> 131 #endif /* FAST_IPSEC */ 132 133 #ifdef TPIP 134 #include <netiso/tp_param.h> 135 #include <netiso/tp_var.h> 136 #endif /* TPIP */ 137 138 #ifdef EON 139 #include <netiso/eonvar.h> 140 #endif /* EON */ 141 142 #include "carp.h" 143 #if NCARP > 0 144 #include <netinet/ip_carp.h> 145 #endif 146 147 #include "pfsync.h" 148 #if NPFSYNC > 0 149 #include <net/pfvar.h> 150 #include <net/if_pfsync.h> 151 #endif 152 153 #include "etherip.h" 154 #if NETHERIP > 0 155 #include <netinet/ip_etherip.h> 156 #endif 157 158 DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */ 159 160 /* Wrappers to acquire kernel_lock. */ 161 162 PR_WRAP_USRREQ(rip_usrreq) 163 PR_WRAP_USRREQ(udp_usrreq) 164 PR_WRAP_USRREQ(tcp_usrreq) 165 166 #define rip_usrreq rip_usrreq_wrapper 167 #define udp_usrreq udp_usrreq_wrapper 168 #define tcp_usrreq tcp_usrreq_wrapper 169 170 PR_WRAP_CTLINPUT(rip_ctlinput) 171 PR_WRAP_CTLINPUT(udp_ctlinput) 172 PR_WRAP_CTLINPUT(tcp_ctlinput) 173 174 #define rip_ctlinput rip_ctlinput_wrapper 175 #define udp_ctlinput udp_ctlinput_wrapper 176 #define tcp_ctlinput tcp_ctlinput_wrapper 177 178 PR_WRAP_CTLOUTPUT(rip_ctloutput) 179 PR_WRAP_CTLOUTPUT(udp_ctloutput) 180 PR_WRAP_CTLOUTPUT(tcp_ctloutput) 181 182 #define rip_ctloutput rip_ctloutput_wrapper 183 #define udp_ctloutput udp_ctloutput_wrapper 184 #define tcp_ctloutput tcp_ctloutput_wrapper 185 186 #if defined(IPSEC) || defined(FAST_IPSEC) 187 PR_WRAP_CTLINPUT(ah4_ctlinput) 188 189 #define ah4_ctlinput ah4_ctlinput_wrapper 190 #endif 191 192 #if defined(IPSEC_ESP) || defined(FAST_IPSEC) 193 PR_WRAP_CTLINPUT(esp4_ctlinput) 194 195 #define esp4_ctlinput esp4_ctlinput_wrapper 196 #endif 197 198 #ifdef TPIP 199 PR_WRAP_CTLOUTPUT(tp_ctloutput) 200 201 #define tp_ctloutput tp_ctloutput_wrapper 202 203 PR_WRAP_CTLINPUT(tpip_ctlinput) 204 205 #define tpip_ctlinput tpip_ctlinput_wrapper 206 #endif 207 208 #ifdef EON 209 PR_WRAP_CTLINPUT(eonctlinput) 210 211 #define eonctlinput eonctlinput_wrapper 212 #endif 213 214 const struct protosw inetsw[] = { 215 { .pr_domain = &inetdomain, 216 .pr_init = ip_init, 217 .pr_output = ip_output, 218 .pr_fasttimo = ip_fasttimo, 219 .pr_slowtimo = ip_slowtimo, 220 .pr_drain = ip_drainstub, 221 }, 222 { .pr_type = SOCK_DGRAM, 223 .pr_domain = &inetdomain, 224 .pr_protocol = IPPROTO_UDP, 225 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF, 226 .pr_input = udp_input, 227 .pr_ctlinput = udp_ctlinput, 228 .pr_ctloutput = udp_ctloutput, 229 .pr_usrreq = udp_usrreq, 230 .pr_init = udp_init, 231 }, 232 { .pr_type = SOCK_STREAM, 233 .pr_domain = &inetdomain, 234 .pr_protocol = IPPROTO_TCP, 235 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF, 236 .pr_input = tcp_input, 237 .pr_ctlinput = tcp_ctlinput, 238 .pr_ctloutput = tcp_ctloutput, 239 .pr_usrreq = tcp_usrreq, 240 .pr_init = tcp_init, 241 .pr_fasttimo = tcp_fasttimo, 242 .pr_slowtimo = tcp_slowtimo, 243 .pr_drain = tcp_drainstub, 244 }, 245 { .pr_type = SOCK_RAW, 246 .pr_domain = &inetdomain, 247 .pr_protocol = IPPROTO_RAW, 248 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF, 249 .pr_input = rip_input, 250 .pr_output = rip_output, 251 .pr_ctlinput = rip_ctlinput, 252 .pr_ctloutput = rip_ctloutput, 253 .pr_usrreq = rip_usrreq, 254 }, 255 { .pr_type = SOCK_RAW, 256 .pr_domain = &inetdomain, 257 .pr_protocol = IPPROTO_ICMP, 258 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 259 .pr_input = icmp_input, 260 .pr_output = rip_output, 261 .pr_ctlinput = rip_ctlinput, 262 .pr_ctloutput = rip_ctloutput, 263 .pr_usrreq = rip_usrreq, 264 .pr_init = icmp_init, 265 }, 266 #ifdef GATEWAY 267 { .pr_domain = &inetdomain, 268 .pr_protocol = IPPROTO_IP, 269 .pr_slowtimo = ipflow_slowtimo, 270 .pr_init = ipflow_poolinit, 271 }, 272 #endif /* GATEWAY */ 273 #ifdef IPSEC 274 { .pr_type = SOCK_RAW, 275 .pr_domain = &inetdomain, 276 .pr_protocol = IPPROTO_AH, 277 .pr_flags = PR_ATOMIC|PR_ADDR, 278 .pr_input = ah4_input, 279 .pr_ctlinput = ah4_ctlinput, 280 .pr_init = ah4_init, 281 }, 282 #ifdef IPSEC_ESP 283 { .pr_type = SOCK_RAW, 284 .pr_domain = &inetdomain, 285 .pr_protocol = IPPROTO_ESP, 286 .pr_flags = PR_ATOMIC|PR_ADDR, 287 .pr_input = esp4_input, 288 .pr_ctlinput = esp4_ctlinput, 289 .pr_init = esp4_init, 290 }, 291 #endif /* IPSEC_ESP */ 292 { .pr_type = SOCK_RAW, 293 .pr_domain = &inetdomain, 294 .pr_protocol = IPPROTO_IPCOMP, 295 .pr_flags = PR_ATOMIC|PR_ADDR, 296 .pr_input = ipcomp4_input, 297 .pr_init = ipcomp4_init, 298 }, 299 #endif /* IPSEC */ 300 #ifdef FAST_IPSEC 301 { .pr_type = SOCK_RAW, 302 .pr_domain = &inetdomain, 303 .pr_protocol = IPPROTO_AH, 304 .pr_flags = PR_ATOMIC|PR_ADDR, 305 .pr_input = ipsec4_common_input, 306 .pr_ctlinput = ah4_ctlinput, 307 }, 308 { .pr_type = SOCK_RAW, 309 .pr_domain = &inetdomain, 310 .pr_protocol = IPPROTO_ESP, 311 .pr_flags = PR_ATOMIC|PR_ADDR, 312 .pr_input = ipsec4_common_input, 313 .pr_ctlinput = esp4_ctlinput, 314 }, 315 { .pr_type = SOCK_RAW, 316 .pr_domain = &inetdomain, 317 .pr_protocol = IPPROTO_IPCOMP, 318 .pr_flags = PR_ATOMIC|PR_ADDR, 319 .pr_input = ipsec4_common_input, 320 }, 321 #endif /* FAST_IPSEC */ 322 { .pr_type = SOCK_RAW, 323 .pr_domain = &inetdomain, 324 .pr_protocol = IPPROTO_IPV4, 325 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 326 .pr_input = encap4_input, 327 .pr_output = rip_output, 328 .pr_ctlinput = rip_ctlinput, 329 .pr_ctloutput = rip_ctloutput, 330 .pr_usrreq = rip_usrreq, 331 .pr_init = encap_init, 332 }, 333 #ifdef INET6 334 { .pr_type = SOCK_RAW, 335 .pr_domain = &inetdomain, 336 .pr_protocol = IPPROTO_IPV6, 337 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 338 .pr_input = encap4_input, 339 .pr_output = rip_output, 340 .pr_ctlinput = rip_ctlinput, 341 .pr_ctloutput = rip_ctloutput, 342 .pr_usrreq = rip_usrreq, 343 .pr_init = encap_init, 344 }, 345 #endif /* INET6 */ 346 #if NETHERIP > 0 347 { .pr_type = SOCK_RAW, 348 .pr_domain = &inetdomain, 349 .pr_protocol = IPPROTO_ETHERIP, 350 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 351 .pr_input = ip_etherip_input, 352 .pr_output = rip_output, 353 .pr_ctlinput = rip_ctlinput, 354 .pr_ctloutput = rip_ctloutput, 355 .pr_usrreq = rip_usrreq, 356 }, 357 #endif /* NETHERIP > 0 */ 358 #if NCARP > 0 359 { .pr_type = SOCK_RAW, 360 .pr_domain = &inetdomain, 361 .pr_protocol = IPPROTO_CARP, 362 .pr_flags = PR_ATOMIC|PR_ADDR, 363 .pr_input = carp_proto_input, 364 .pr_output = rip_output, 365 .pr_ctloutput = rip_ctloutput, 366 .pr_usrreq = rip_usrreq, 367 .pr_init = carp_init, 368 }, 369 #endif /* NCARP > 0 */ 370 #if NPFSYNC > 0 371 { .pr_type = SOCK_RAW, 372 .pr_domain = &inetdomain, 373 .pr_protocol = IPPROTO_PFSYNC, 374 .pr_flags = PR_ATOMIC|PR_ADDR, 375 .pr_input = pfsync_input, 376 .pr_output = rip_output, 377 .pr_ctloutput = rip_ctloutput, 378 .pr_usrreq = rip_usrreq, 379 }, 380 #endif /* NPFSYNC > 0 */ 381 { .pr_type = SOCK_RAW, 382 .pr_domain = &inetdomain, 383 .pr_protocol = IPPROTO_IGMP, 384 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 385 .pr_input = igmp_input, 386 .pr_output = rip_output, 387 .pr_ctloutput = rip_ctloutput, 388 .pr_ctlinput = rip_ctlinput, 389 .pr_usrreq = rip_usrreq, 390 .pr_fasttimo = igmp_fasttimo, 391 .pr_slowtimo = igmp_slowtimo, 392 .pr_init = igmp_init, 393 }, 394 #ifdef PIM 395 { .pr_type = SOCK_RAW, 396 .pr_domain = &inetdomain, 397 .pr_protocol = IPPROTO_PIM, 398 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 399 .pr_input = pim_input, 400 .pr_output = rip_output, 401 .pr_ctloutput = rip_ctloutput, 402 .pr_ctlinput = rip_ctlinput, 403 .pr_usrreq = rip_usrreq, 404 }, 405 #endif /* PIM */ 406 #ifdef TPIP 407 { .pr_type = SOCK_SEQPACKET, 408 .pr_domain = &inetdomain, 409 .pr_protocol = IPPROTO_TP, 410 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_LASTHDR|PR_ABRTACPTDIS, 411 .pr_input = tpip_input, 412 .pr_ctloutput = tp_ctloutput, 413 .pr_ctlinput = tpip_ctlinput, 414 .pr_usrreq = tp_usrreq, 415 .pr_init = tp_init, 416 .pr_slowtimo = tp_slowtimo, 417 .pr_drain = tp_drain, 418 }, 419 #endif /* TPIP */ 420 #ifdef ISO 421 /* EON (ISO CLNL over IP) */ 422 #ifdef EON 423 { .pr_type = SOCK_RAW, 424 .pr_domain = &inetdomain, 425 .pr_protocol = IPPROTO_EON, 426 .pr_flags = PR_LASTHDR, 427 .pr_input = eoninput, 428 .pr_ctlinput = eonctlinput, 429 .pr_init = eonprotoinit, 430 }, 431 #else 432 { .pr_type = SOCK_RAW, 433 .pr_domain = &inetdomain, 434 .pr_protocol = IPPROTO_EON, 435 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 436 .pr_input = encap4_input, 437 .pr_output = rip_output, 438 .pr_ctloutput = rip_ctloutput, 439 .pr_ctlinput = rip_ctlinput, 440 .pr_usrreq = rip_usrreq, 441 .pr_init = encap_init, 442 }, 443 #endif /* EON */ 444 #endif /* ISO */ 445 /* raw wildcard */ 446 { .pr_type = SOCK_RAW, 447 .pr_domain = &inetdomain, 448 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 449 .pr_input = rip_input, 450 .pr_output = rip_output, 451 .pr_ctloutput = rip_ctloutput, 452 .pr_ctlinput = rip_ctlinput, 453 .pr_usrreq = rip_usrreq, 454 .pr_init = rip_init, 455 }, 456 }; 457 458 extern struct ifqueue ipintrq; 459 460 const struct sockaddr_in in_any = { 461 .sin_len = sizeof(struct sockaddr_in) 462 , .sin_family = AF_INET 463 , .sin_port = 0 464 , .sin_addr = {.s_addr = 0 /* INADDR_ANY */} 465 }; 466 467 struct domain inetdomain = { 468 .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL, 469 .dom_externalize = NULL, .dom_dispose = NULL, 470 .dom_protosw = inetsw, 471 .dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)], 472 .dom_rtattach = rt_inithead, 473 .dom_rtoffset = 32, 474 .dom_maxrtkey = sizeof(struct ip_pack4), 475 #ifdef IPSELSRC 476 .dom_ifattach = in_domifattach, 477 .dom_ifdetach = in_domifdetach, 478 #else 479 .dom_ifattach = NULL, 480 .dom_ifdetach = NULL, 481 #endif 482 .dom_ifqueues = { &ipintrq, NULL }, 483 .dom_link = { NULL }, 484 .dom_mowner = MOWNER_INIT("",""), 485 .dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr), 486 .dom_sa_cmplen = sizeof(struct in_addr), 487 .dom_sa_any = (const struct sockaddr *)&in_any, 488 .dom_sockaddr_const_addr = sockaddr_in_const_addr, 489 .dom_sockaddr_addr = sockaddr_in_addr, 490 .dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache) 491 }; 492 493 u_char ip_protox[IPPROTO_MAX]; 494 495 int icmperrppslim = 100; /* 100pps */ 496 497 static void 498 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp) 499 { 500 socklen_t slen; 501 502 if (slenp == NULL) 503 return; 504 505 slen = sockaddr_getlen(sa); 506 *slenp = (socklen_t)MIN(sizeof(struct in_addr), 507 slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr))); 508 } 509 510 const void * 511 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp) 512 { 513 const struct sockaddr_in *sin; 514 515 sockaddr_in_addrlen(sa, slenp); 516 sin = (const struct sockaddr_in *)sa; 517 return &sin->sin_addr; 518 } 519 520 void * 521 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp) 522 { 523 struct sockaddr_in *sin; 524 525 sockaddr_in_addrlen(sa, slenp); 526 sin = (struct sockaddr_in *)sa; 527 return &sin->sin_addr; 528 } 529 530 int 531 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2) 532 { 533 uint_fast8_t len; 534 const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr), 535 addrend = addrofs + sizeof(struct in_addr); 536 int rc; 537 const struct sockaddr_in *sin1, *sin2; 538 539 sin1 = satocsin(sa1); 540 sin2 = satocsin(sa2); 541 542 len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len)); 543 544 if (len > addrofs && 545 (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr, 546 len - addrofs)) != 0) 547 return rc; 548 549 return sin1->sin_len - sin2->sin_len; 550 } 551