1 /* 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)in_proto.c 8.2 (Berkeley) 2/9/95 30 * $FreeBSD: src/sys/netinet/in_proto.c,v 1.53.2.7 2003/08/24 08:24:38 hsu Exp $ 31 */ 32 33 #include "opt_ipdivert.h" 34 #include "opt_ipx.h" 35 #include "opt_mrouting.h" 36 #include "opt_ipsec.h" 37 #include "opt_inet6.h" 38 #include "opt_sctp.h" 39 #include "opt_carp.h" 40 41 #include <sys/param.h> 42 #include <sys/kernel.h> 43 #include <sys/socket.h> 44 #include <sys/domain.h> 45 #include <sys/protosw.h> 46 #include <sys/queue.h> 47 #include <sys/sysctl.h> 48 49 #include <net/if.h> 50 #include <net/route.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_systm.h> 54 #include <netinet/ip.h> 55 #include <netinet/ip_var.h> 56 #include <netinet/ip_icmp.h> 57 #include <netinet/igmp_var.h> 58 #ifdef PIM 59 #include <netinet/pim_var.h> 60 #endif 61 #include <netinet/tcp.h> 62 #include <netinet/tcp_timer.h> 63 #include <netinet/tcp_var.h> 64 #include <netinet/udp.h> 65 #include <netinet/udp_var.h> 66 #include <netinet/ip_encap.h> 67 #ifdef IPDIVERT 68 #include <netinet/ip_divert.h> 69 #endif 70 71 /* 72 * TCP/IP protocol family: IP, ICMP, UDP, TCP. 73 */ 74 75 #ifdef IPSEC 76 #include <netinet6/ipsec.h> 77 #include <netinet6/ah.h> 78 #ifdef IPSEC_ESP 79 #include <netinet6/esp.h> 80 #endif 81 #include <netinet6/ipcomp.h> 82 #endif /* IPSEC */ 83 84 #ifdef FAST_IPSEC 85 #include <netproto/ipsec/ipsec.h> 86 #endif /* FAST_IPSEC */ 87 88 #ifdef IPXIP 89 #include <netproto/ipx/ipx_ip.h> 90 #endif 91 92 #ifdef SCTP 93 #include <netinet/in_pcb.h> 94 #include <netinet/sctp_pcb.h> 95 #include <netinet/sctp.h> 96 #include <netinet/sctp_var.h> 97 #endif /* SCTP */ 98 99 #include <net/netisr.h> /* for cpu0_soport */ 100 101 #ifdef CARP 102 #include <netinet/ip_carp.h> 103 #endif 104 105 extern struct domain inetdomain; 106 static struct pr_usrreqs nousrreqs; 107 108 struct protosw inetsw[] = { 109 { 110 .pr_type = 0, 111 .pr_domain = &inetdomain, 112 .pr_protocol = 0, 113 .pr_flags = 0, 114 115 .pr_ctlport = NULL, 116 117 .pr_init = ip_init, 118 .pr_fasttimo = NULL, 119 .pr_slowtimo = ip_slowtimo, 120 .pr_drain = ip_drain, 121 .pr_usrreqs = &nousrreqs 122 }, 123 { 124 .pr_type = SOCK_DGRAM, 125 .pr_domain = &inetdomain, 126 .pr_protocol = IPPROTO_UDP, 127 .pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE| 128 PR_ASYNC_SEND|PR_ASEND_HOLDTD, 129 130 .pr_input = udp_input, 131 .pr_output = NULL, 132 .pr_ctlinput = udp_ctlinput, 133 .pr_ctloutput = ip_ctloutput, 134 135 .pr_ctlport = udp_ctlport, 136 .pr_init = udp_init, 137 .pr_usrreqs = &udp_usrreqs 138 }, 139 { 140 .pr_type = SOCK_STREAM, 141 .pr_domain = &inetdomain, 142 .pr_protocol = IPPROTO_TCP, 143 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD| 144 PR_MPSAFE|PR_ASYNC_SEND|PR_ASYNC_RCVD, 145 146 .pr_input = tcp_input, 147 .pr_output = NULL, 148 .pr_ctlinput = tcp_ctlinput, 149 .pr_ctloutput = tcp_ctloutput, 150 151 .pr_ctlport = tcp_ctlport, 152 .pr_init = tcp_init, 153 .pr_fasttimo = NULL, 154 .pr_slowtimo = NULL, 155 .pr_drain = tcp_drain, 156 .pr_usrreqs = &tcp_usrreqs 157 }, 158 #ifdef SCTP 159 /* 160 * Order is very important here, we add the good one in 161 * in this postion so it maps to the right ip_protox[] 162 * postion for SCTP. Don't move the one above below 163 * this one or IPv6/4 compatability will break 164 */ 165 { 166 .pr_type = SOCK_DGRAM, 167 .pr_domain = &inetdomain, 168 .pr_protocol = IPPROTO_SCTP, 169 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD, 170 171 .pr_input = sctp_input, 172 .pr_output = NULL, 173 .pr_ctlinput = sctp_ctlinput, 174 .pr_ctloutput = sctp_ctloutput, 175 176 .pr_ctlport = cpu0_ctlport, 177 .pr_init = sctp_init, 178 .pr_drain = sctp_drain, 179 .pr_usrreqs = &sctp_usrreqs 180 }, 181 { 182 .pr_type = SOCK_SEQPACKET, 183 .pr_domain = &inetdomain, 184 .pr_protocol = IPPROTO_SCTP, 185 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD, 186 187 .pr_input = sctp_input, 188 .pr_output = NULL, 189 .pr_ctlinput = sctp_ctlinput, 190 .pr_ctloutput = sctp_ctloutput, 191 192 .pr_ctlport = cpu0_ctlport, 193 .pr_drain = sctp_drain, 194 .pr_usrreqs = &sctp_usrreqs 195 }, 196 197 { 198 .pr_type = SOCK_STREAM, 199 .pr_domain = &inetdomain, 200 .pr_protocol = IPPROTO_SCTP, 201 .pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD, 202 203 .pr_input = sctp_input, 204 .pr_output = NULL, 205 .pr_ctlinput = sctp_ctlinput, 206 .pr_ctloutput = sctp_ctloutput, 207 208 .pr_ctlport = cpu0_ctlport, 209 .pr_drain = sctp_drain, 210 .pr_usrreqs = &sctp_usrreqs 211 }, 212 #endif /* SCTP */ 213 { 214 .pr_type = SOCK_RAW, 215 .pr_domain = &inetdomain, 216 .pr_protocol = IPPROTO_RAW, 217 .pr_flags = PR_ATOMIC|PR_ADDR, 218 219 .pr_input = rip_input, 220 .pr_output = NULL, 221 .pr_ctlinput = rip_ctlinput, 222 .pr_ctloutput = rip_ctloutput, 223 224 .pr_ctlport = cpu0_ctlport, 225 .pr_usrreqs = &rip_usrreqs 226 }, 227 { 228 .pr_type = SOCK_RAW, 229 .pr_domain = &inetdomain, 230 .pr_protocol = IPPROTO_ICMP, 231 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE, 232 233 .pr_input = icmp_input, 234 .pr_output = NULL, 235 .pr_ctlinput = NULL, 236 .pr_ctloutput = rip_ctloutput, 237 238 .pr_usrreqs = &rip_usrreqs 239 }, 240 { 241 .pr_type = SOCK_RAW, 242 .pr_domain = &inetdomain, 243 .pr_protocol = IPPROTO_IGMP, 244 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 245 246 .pr_input = igmp_input, 247 .pr_output = NULL, 248 .pr_ctlinput = NULL, 249 .pr_ctloutput = rip_ctloutput, 250 251 .pr_init = igmp_init, 252 .pr_fasttimo = igmp_fasttimo, 253 .pr_slowtimo = igmp_slowtimo, 254 .pr_drain = NULL, 255 .pr_usrreqs = &rip_usrreqs 256 }, 257 { 258 .pr_type = SOCK_RAW, 259 .pr_domain = &inetdomain, 260 .pr_protocol = IPPROTO_RSVP, 261 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 262 263 .pr_input = rsvp_input, 264 .pr_output = NULL, 265 .pr_ctlinput = NULL, 266 .pr_ctloutput = rip_ctloutput, 267 268 .pr_usrreqs = &rip_usrreqs 269 }, 270 #ifdef IPSEC 271 { 272 .pr_type = SOCK_RAW, 273 .pr_domain = &inetdomain, 274 .pr_protocol = IPPROTO_AH, 275 .pr_flags = PR_ATOMIC|PR_ADDR, 276 277 .pr_input = ah4_input, 278 .pr_output = NULL, 279 .pr_ctlinput = NULL, 280 .pr_ctloutput = NULL, 281 282 .pr_ctlport = NULL, 283 .pr_usrreqs = &nousrreqs 284 }, 285 #ifdef IPSEC_ESP 286 { 287 .pr_type = SOCK_RAW, 288 .pr_domain = &inetdomain, 289 .pr_protocol = IPPROTO_ESP, 290 .pr_flags = PR_ATOMIC|PR_ADDR, 291 292 .pr_input = esp4_input, 293 .pr_output = NULL, 294 .pr_ctlinput = NULL, 295 .pr_ctloutput = NULL, 296 297 .pr_ctlport = NULL, 298 .pr_usrreqs = &nousrreqs 299 }, 300 #endif 301 { 302 .pr_type = SOCK_RAW, 303 .pr_domain = &inetdomain, 304 .pr_protocol = IPPROTO_IPCOMP, 305 .pr_flags = PR_ATOMIC|PR_ADDR, 306 307 .pr_input = ipcomp4_input, 308 .pr_output = 0, 309 .pr_ctlinput = NULL, 310 .pr_ctloutput = NULL, 311 312 .pr_ctlport = NULL, 313 .pr_usrreqs = &nousrreqs 314 }, 315 #endif /* IPSEC */ 316 #ifdef FAST_IPSEC 317 { 318 .pr_type = SOCK_RAW, 319 .pr_domain = &inetdomain, 320 .pr_protocol = IPPROTO_AH, 321 .pr_flags = PR_ATOMIC|PR_ADDR, 322 323 .pr_input = ipsec4_common_input, 324 .pr_output = NULL, 325 .pr_ctlinput = NULL, 326 .pr_ctloutput = NULL, 327 328 .pr_ctlport = NULL, 329 .pr_usrreqs = &nousrreqs 330 }, 331 { 332 .pr_type = SOCK_RAW, 333 .pr_domain = &inetdomain, 334 .pr_protocol = IPPROTO_ESP, 335 .pr_flags = PR_ATOMIC|PR_ADDR, 336 337 .pr_input = ipsec4_common_input, 338 .pr_output = NULL 339 .pr_ctlinput = NULL, 340 .pr_ctloutput = NULL, 341 342 .pr_ctlport = NULL, 343 .pr_usrreqs = &nousrreqs 344 }, 345 { 346 .pr_type = SOCK_RAW, 347 .pr_domain = &inetdomain, 348 .pr_protocol = IPPROTO_IPCOMP, 349 .pr_flags = PR_ATOMIC|PR_ADDR, 350 351 .pr_input = ipsec4_common_input, 352 .pr_output = NULL, 353 .pr_ctlinput = NULL, 354 .pr_ctloutput = NULL, 355 356 .pr_ctlport = NULL, 357 .pr_usrreqs = &nousrreqs 358 }, 359 #endif /* FAST_IPSEC */ 360 { 361 .pr_type = SOCK_RAW, 362 .pr_domain = &inetdomain, 363 .pr_protocol = IPPROTO_IPV4, 364 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 365 .pr_input = encap4_input, 366 .pr_output = NULL, 367 .pr_ctlinput = NULL, 368 .pr_ctloutput = rip_ctloutput, 369 370 .pr_ctlport = NULL, 371 .pr_init = encap_init, 372 .pr_usrreqs = &rip_usrreqs 373 }, 374 { 375 .pr_type = SOCK_RAW, 376 .pr_domain = &inetdomain, 377 .pr_protocol = IPPROTO_MOBILE, 378 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 379 380 .pr_input = encap4_input, 381 .pr_output = NULL, 382 .pr_ctlinput = NULL, 383 .pr_ctloutput = rip_ctloutput, 384 385 .pr_ctlport = NULL, 386 .pr_init = encap_init, 387 .pr_usrreqs = &rip_usrreqs 388 }, 389 { 390 .pr_type = SOCK_RAW, 391 .pr_domain = &inetdomain, 392 .pr_protocol = IPPROTO_GRE, 393 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 394 395 .pr_input = encap4_input, 396 .pr_output = NULL, 397 .pr_ctlinput = NULL, 398 .pr_ctloutput = rip_ctloutput, 399 400 .pr_ctlport = NULL, 401 .pr_init = encap_init, 402 .pr_usrreqs = &rip_usrreqs 403 }, 404 #ifdef INET6 405 { 406 .pr_type = SOCK_RAW, 407 .pr_domain = &inetdomain, 408 .pr_protocol = IPPROTO_IPV6, 409 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 410 411 .pr_input = encap4_input, 412 .pr_output = NULL, 413 .pr_ctlinput = NULL, 414 .pr_ctloutput = rip_ctloutput, 415 416 .pr_ctlport = NULL, 417 .pr_init = encap_init, 418 .pr_usrreqs = &rip_usrreqs 419 }, 420 #endif 421 #ifdef IPDIVERT 422 { 423 .pr_type = SOCK_RAW, 424 .pr_domain = &inetdomain, 425 .pr_protocol = IPPROTO_DIVERT, 426 .pr_flags = PR_ATOMIC|PR_ADDR, 427 428 .pr_input = div_input, 429 .pr_output = NULL, 430 .pr_ctlinput = NULL, 431 .pr_ctloutput = ip_ctloutput, 432 433 .pr_ctlport = NULL, 434 .pr_init = div_init, 435 .pr_usrreqs = &div_usrreqs 436 }, 437 #endif 438 #ifdef IPXIP 439 { 440 .pr_type = SOCK_RAW, 441 .pr_domain = &inetdomain, 442 .pr_protocol = IPPROTO_IDP, 443 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 444 445 .pr_input = ipxip_input, 446 .pr_output = NULL, 447 .pr_ctlinput = ipxip_ctlinput, 448 .pr_ctloutput = NULL, 449 450 .pr_ctlport = cpu0_ctlport, 451 .pr_usrreqs = &rip_usrreqs 452 }, 453 #endif 454 #ifdef PIM 455 { 456 .pr_type = SOCK_RAW, 457 .pr_domain = &inetdomain, 458 .pr_protocol = IPPROTO_PIM, 459 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 460 461 .pr_input = pim_input, 462 .pr_output = NULL, 463 .pr_ctlinput = NULL, 464 .pr_ctloutput = rip_ctloutput, 465 466 .pr_ctlport = NULL, 467 .pr_usrreqs = &rip_usrreqs 468 }, 469 #endif 470 #ifdef NPFSYNC 471 { 472 .pr_type = SOCK_RAW, 473 .pr_domain = &inetdomain, 474 .pr_protocol = IPPROTO_PFSYNC, 475 .pr_flags = PR_ATOMIC|PR_ADDR, 476 477 .pr_input = pfsync_input, 478 .pr_output = NULL, 479 .pr_ctlinput = NULL, 480 .pr_ctloutput = rip_ctloutput, 481 482 .pr_ctlport = NULL, 483 .pr_usrreqs = &rip_usrreqs 484 }, 485 #endif /* NPFSYNC */ 486 { 487 /* raw wildcard */ 488 .pr_type = SOCK_RAW, 489 .pr_domain = &inetdomain, 490 .pr_protocol = 0, 491 .pr_flags = PR_ATOMIC|PR_ADDR, 492 493 .pr_input = rip_input, 494 .pr_output = NULL, 495 .pr_ctlinput = NULL, 496 .pr_ctloutput = rip_ctloutput, 497 498 .pr_init = rip_init, 499 .pr_ctlport = NULL, 500 .pr_usrreqs = &rip_usrreqs 501 }, 502 #ifdef CARP 503 { 504 .pr_type = SOCK_RAW, 505 .pr_domain = &inetdomain, 506 .pr_protocol = IPPROTO_CARP, 507 .pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE, 508 509 .pr_input = carp_proto_input, 510 .pr_output = rip_output, 511 .pr_ctlinput = carp_proto_ctlinput, 512 .pr_ctloutput = rip_ctloutput, 513 514 .pr_ctlport = cpu0_ctlport, 515 .pr_usrreqs = &rip_usrreqs 516 }, 517 #endif 518 }; 519 520 struct domain inetdomain = { 521 AF_INET, "internet", NULL, NULL, NULL, 522 inetsw, &inetsw[NELEM(inetsw)], 523 SLIST_ENTRY_INITIALIZER, 524 in_inithead, 32, sizeof(struct sockaddr_in), 525 }; 526 527 DOMAIN_SET(inet); 528 529 SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW, 0, 530 "Internet Family"); 531 532 SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW, 0, "IP"); 533 SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW, 0, "ICMP"); 534 SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW, 0, "UDP"); 535 SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW, 0, "TCP"); 536 SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW, 0, "IGMP"); 537 #ifdef FAST_IPSEC 538 /* XXX no protocol # to use, pick something "reserved" */ 539 SYSCTL_NODE(_net_inet, 253, ipsec, CTLFLAG_RW, 0, "IPSEC"); 540 SYSCTL_NODE(_net_inet, IPPROTO_AH, ah, CTLFLAG_RW, 0, "AH"); 541 SYSCTL_NODE(_net_inet, IPPROTO_ESP, esp, CTLFLAG_RW, 0, "ESP"); 542 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP, ipcomp, CTLFLAG_RW, 0, "IPCOMP"); 543 SYSCTL_NODE(_net_inet, IPPROTO_IPIP, ipip, CTLFLAG_RW, 0, "IPIP"); 544 #else 545 #ifdef IPSEC 546 SYSCTL_NODE(_net_inet, IPPROTO_AH, ipsec, CTLFLAG_RW, 0, "IPSEC"); 547 #endif /* IPSEC */ 548 #endif /* !FAST_IPSEC */ 549 SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW, 0, "RAW"); 550 #ifdef IPDIVERT 551 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "DIVERT"); 552 #endif 553 #ifdef PIM 554 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM"); 555 #endif 556 #ifdef CARP 557 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 558 #endif 559