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_mrouting.h" 35 #include "opt_ipsec.h" 36 #include "opt_inet6.h" 37 #include "opt_sctp.h" 38 #include "opt_carp.h" 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/socket.h> 43 #include <sys/domain.h> 44 #include <sys/protosw.h> 45 #include <sys/queue.h> 46 #include <sys/sysctl.h> 47 48 #include <net/if.h> 49 #include <net/route.h> 50 51 #include <netinet/in.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/in_pcb.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 SCTP 89 #include <netinet/sctp_pcb.h> 90 #include <netinet/sctp.h> 91 #include <netinet/sctp_var.h> 92 #endif /* SCTP */ 93 94 #include <net/netisr.h> /* for cpu0_soport */ 95 96 #ifdef CARP 97 #include <netinet/ip_carp.h> 98 #endif 99 100 extern struct domain inetdomain; 101 static struct pr_usrreqs nousrreqs; 102 103 struct protosw inetsw[] = { 104 { 105 .pr_type = 0, 106 .pr_domain = &inetdomain, 107 .pr_protocol = 0, 108 .pr_flags = 0, 109 110 .pr_ctlport = NULL, 111 112 .pr_init = ip_init, 113 .pr_fasttimo = NULL, 114 .pr_slowtimo = ip_slowtimo, 115 .pr_drain = ip_drain, 116 .pr_usrreqs = &nousrreqs 117 }, 118 { 119 .pr_type = SOCK_DGRAM, 120 .pr_domain = &inetdomain, 121 .pr_protocol = IPPROTO_UDP, 122 .pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE| 123 PR_ASYNC_SEND|PR_ASEND_HOLDTD, 124 125 .pr_initport = udp_initport, 126 .pr_input = udp_input, 127 .pr_output = NULL, 128 .pr_ctlinput = udp_ctlinput, 129 .pr_ctloutput = udp_ctloutput, 130 131 .pr_ctlport = udp_ctlport, 132 .pr_init = udp_init, 133 .pr_usrreqs = &udp_usrreqs 134 }, 135 { 136 .pr_type = SOCK_STREAM, 137 .pr_domain = &inetdomain, 138 .pr_protocol = IPPROTO_TCP, 139 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_MPSAFE| 140 PR_ASYNC_SEND|PR_ASYNC_RCVD|PR_ACONN_HOLDTD, 141 142 .pr_initport = tcp_initport, 143 .pr_input = tcp_input, 144 .pr_output = NULL, 145 .pr_ctlinput = tcp_ctlinput, 146 .pr_ctloutput = tcp_ctloutput, 147 148 .pr_ctlport = tcp_ctlport, 149 .pr_init = tcp_init, 150 .pr_fasttimo = NULL, 151 .pr_slowtimo = NULL, 152 .pr_drain = tcp_drain, 153 .pr_usrreqs = &tcp_usrreqs 154 }, 155 #ifdef SCTP 156 /* 157 * Order is very important here, we add the good one in 158 * in this postion so it maps to the right ip_protox[] 159 * postion for SCTP. Don't move the one above below 160 * this one or IPv6/4 compatability will break 161 */ 162 { 163 .pr_type = SOCK_DGRAM, 164 .pr_domain = &inetdomain, 165 .pr_protocol = IPPROTO_SCTP, 166 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD, 167 168 .pr_input = sctp_input, 169 .pr_output = NULL, 170 .pr_ctlinput = sctp_ctlinput, 171 .pr_ctloutput = sctp_ctloutput, 172 173 .pr_ctlport = cpu0_ctlport, 174 .pr_init = sctp_init, 175 .pr_drain = sctp_drain, 176 .pr_usrreqs = &sctp_usrreqs 177 }, 178 { 179 .pr_type = SOCK_SEQPACKET, 180 .pr_domain = &inetdomain, 181 .pr_protocol = IPPROTO_SCTP, 182 .pr_flags = PR_ADDR_OPT|PR_WANTRCVD, 183 184 .pr_input = sctp_input, 185 .pr_output = NULL, 186 .pr_ctlinput = sctp_ctlinput, 187 .pr_ctloutput = sctp_ctloutput, 188 189 .pr_ctlport = cpu0_ctlport, 190 .pr_drain = sctp_drain, 191 .pr_usrreqs = &sctp_usrreqs 192 }, 193 194 { 195 .pr_type = SOCK_STREAM, 196 .pr_domain = &inetdomain, 197 .pr_protocol = IPPROTO_SCTP, 198 .pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD, 199 200 .pr_input = sctp_input, 201 .pr_output = NULL, 202 .pr_ctlinput = sctp_ctlinput, 203 .pr_ctloutput = sctp_ctloutput, 204 205 .pr_ctlport = cpu0_ctlport, 206 .pr_drain = sctp_drain, 207 .pr_usrreqs = &sctp_usrreqs 208 }, 209 #endif /* SCTP */ 210 { 211 .pr_type = SOCK_RAW, 212 .pr_domain = &inetdomain, 213 .pr_protocol = IPPROTO_RAW, 214 .pr_flags = PR_ATOMIC|PR_ADDR, 215 216 .pr_input = rip_input, 217 .pr_output = NULL, 218 .pr_ctlinput = rip_ctlinput, 219 .pr_ctloutput = rip_ctloutput, 220 221 .pr_ctlport = cpu0_ctlport, 222 .pr_usrreqs = &rip_usrreqs 223 }, 224 { 225 .pr_type = SOCK_RAW, 226 .pr_domain = &inetdomain, 227 .pr_protocol = IPPROTO_ICMP, 228 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE, 229 230 .pr_input = icmp_input, 231 .pr_output = NULL, 232 .pr_ctlinput = NULL, 233 .pr_ctloutput = rip_ctloutput, 234 235 .pr_usrreqs = &rip_usrreqs 236 }, 237 { 238 .pr_type = SOCK_RAW, 239 .pr_domain = &inetdomain, 240 .pr_protocol = IPPROTO_IGMP, 241 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE, 242 243 .pr_input = igmp_input, 244 .pr_output = NULL, 245 .pr_ctlinput = NULL, 246 .pr_ctloutput = rip_ctloutput, 247 248 .pr_init = igmp_init, 249 .pr_fasttimo = igmp_fasttimo, 250 .pr_slowtimo = igmp_slowtimo, 251 .pr_drain = NULL, 252 .pr_usrreqs = &rip_usrreqs 253 }, 254 { 255 .pr_type = SOCK_RAW, 256 .pr_domain = &inetdomain, 257 .pr_protocol = IPPROTO_RSVP, 258 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 259 260 .pr_input = rsvp_input, 261 .pr_output = NULL, 262 .pr_ctlinput = NULL, 263 .pr_ctloutput = rip_ctloutput, 264 265 .pr_usrreqs = &rip_usrreqs 266 }, 267 #ifdef IPSEC 268 { 269 .pr_type = SOCK_RAW, 270 .pr_domain = &inetdomain, 271 .pr_protocol = IPPROTO_AH, 272 .pr_flags = PR_ATOMIC|PR_ADDR, 273 274 .pr_input = ah4_input, 275 .pr_output = NULL, 276 .pr_ctlinput = NULL, 277 .pr_ctloutput = NULL, 278 279 .pr_ctlport = NULL, 280 .pr_usrreqs = &nousrreqs 281 }, 282 #ifdef IPSEC_ESP 283 { 284 .pr_type = SOCK_RAW, 285 .pr_domain = &inetdomain, 286 .pr_protocol = IPPROTO_ESP, 287 .pr_flags = PR_ATOMIC|PR_ADDR, 288 289 .pr_input = esp4_input, 290 .pr_output = NULL, 291 .pr_ctlinput = NULL, 292 .pr_ctloutput = NULL, 293 294 .pr_ctlport = NULL, 295 .pr_usrreqs = &nousrreqs 296 }, 297 #endif 298 { 299 .pr_type = SOCK_RAW, 300 .pr_domain = &inetdomain, 301 .pr_protocol = IPPROTO_IPCOMP, 302 .pr_flags = PR_ATOMIC|PR_ADDR, 303 304 .pr_input = ipcomp4_input, 305 .pr_output = 0, 306 .pr_ctlinput = NULL, 307 .pr_ctloutput = NULL, 308 309 .pr_ctlport = NULL, 310 .pr_usrreqs = &nousrreqs 311 }, 312 #endif /* IPSEC */ 313 #ifdef FAST_IPSEC 314 { 315 .pr_type = SOCK_RAW, 316 .pr_domain = &inetdomain, 317 .pr_protocol = IPPROTO_AH, 318 .pr_flags = PR_ATOMIC|PR_ADDR, 319 320 .pr_input = ipsec4_common_input, 321 .pr_output = NULL, 322 .pr_ctlinput = NULL, 323 .pr_ctloutput = NULL, 324 325 .pr_ctlport = NULL, 326 .pr_usrreqs = &nousrreqs 327 }, 328 { 329 .pr_type = SOCK_RAW, 330 .pr_domain = &inetdomain, 331 .pr_protocol = IPPROTO_ESP, 332 .pr_flags = PR_ATOMIC|PR_ADDR, 333 334 .pr_input = ipsec4_common_input, 335 .pr_output = NULL, 336 .pr_ctlinput = NULL, 337 .pr_ctloutput = NULL, 338 339 .pr_ctlport = NULL, 340 .pr_usrreqs = &nousrreqs 341 }, 342 { 343 .pr_type = SOCK_RAW, 344 .pr_domain = &inetdomain, 345 .pr_protocol = IPPROTO_IPCOMP, 346 .pr_flags = PR_ATOMIC|PR_ADDR, 347 348 .pr_input = ipsec4_common_input, 349 .pr_output = NULL, 350 .pr_ctlinput = NULL, 351 .pr_ctloutput = NULL, 352 353 .pr_ctlport = NULL, 354 .pr_usrreqs = &nousrreqs 355 }, 356 #endif /* FAST_IPSEC */ 357 { 358 .pr_type = SOCK_RAW, 359 .pr_domain = &inetdomain, 360 .pr_protocol = IPPROTO_IPV4, 361 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 362 .pr_input = encap4_input, 363 .pr_output = NULL, 364 .pr_ctlinput = NULL, 365 .pr_ctloutput = rip_ctloutput, 366 367 .pr_ctlport = NULL, 368 .pr_init = encap_init, 369 .pr_usrreqs = &rip_usrreqs 370 }, 371 { 372 .pr_type = SOCK_RAW, 373 .pr_domain = &inetdomain, 374 .pr_protocol = IPPROTO_MOBILE, 375 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 376 377 .pr_input = encap4_input, 378 .pr_output = NULL, 379 .pr_ctlinput = NULL, 380 .pr_ctloutput = rip_ctloutput, 381 382 .pr_ctlport = NULL, 383 .pr_init = encap_init, 384 .pr_usrreqs = &rip_usrreqs 385 }, 386 { 387 .pr_type = SOCK_RAW, 388 .pr_domain = &inetdomain, 389 .pr_protocol = IPPROTO_GRE, 390 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 391 392 .pr_input = encap4_input, 393 .pr_output = NULL, 394 .pr_ctlinput = NULL, 395 .pr_ctloutput = rip_ctloutput, 396 397 .pr_ctlport = NULL, 398 .pr_init = encap_init, 399 .pr_usrreqs = &rip_usrreqs 400 }, 401 #ifdef INET6 402 { 403 .pr_type = SOCK_RAW, 404 .pr_domain = &inetdomain, 405 .pr_protocol = IPPROTO_IPV6, 406 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 407 408 .pr_input = encap4_input, 409 .pr_output = NULL, 410 .pr_ctlinput = NULL, 411 .pr_ctloutput = rip_ctloutput, 412 413 .pr_ctlport = NULL, 414 .pr_init = encap_init, 415 .pr_usrreqs = &rip_usrreqs 416 }, 417 #endif 418 #ifdef IPDIVERT 419 { 420 .pr_type = SOCK_RAW, 421 .pr_domain = &inetdomain, 422 .pr_protocol = IPPROTO_DIVERT, 423 .pr_flags = PR_ATOMIC|PR_ADDR, 424 425 .pr_input = div_input, 426 .pr_output = NULL, 427 .pr_ctlinput = NULL, 428 .pr_ctloutput = ip_ctloutput, 429 430 .pr_ctlport = NULL, 431 .pr_init = div_init, 432 .pr_usrreqs = &div_usrreqs 433 }, 434 #endif 435 #ifdef PIM 436 { 437 .pr_type = SOCK_RAW, 438 .pr_domain = &inetdomain, 439 .pr_protocol = IPPROTO_PIM, 440 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 441 442 .pr_input = pim_input, 443 .pr_output = NULL, 444 .pr_ctlinput = NULL, 445 .pr_ctloutput = rip_ctloutput, 446 447 .pr_ctlport = NULL, 448 .pr_usrreqs = &rip_usrreqs 449 }, 450 #endif 451 #ifdef NPFSYNC 452 { 453 .pr_type = SOCK_RAW, 454 .pr_domain = &inetdomain, 455 .pr_protocol = IPPROTO_PFSYNC, 456 .pr_flags = PR_ATOMIC|PR_ADDR, 457 458 .pr_input = pfsync_input, 459 .pr_output = NULL, 460 .pr_ctlinput = NULL, 461 .pr_ctloutput = rip_ctloutput, 462 463 .pr_ctlport = NULL, 464 .pr_usrreqs = &rip_usrreqs 465 }, 466 #endif /* NPFSYNC */ 467 { 468 /* raw wildcard */ 469 .pr_type = SOCK_RAW, 470 .pr_domain = &inetdomain, 471 .pr_protocol = 0, 472 .pr_flags = PR_ATOMIC|PR_ADDR, 473 474 .pr_input = rip_input, 475 .pr_output = NULL, 476 .pr_ctlinput = NULL, 477 .pr_ctloutput = rip_ctloutput, 478 479 .pr_init = rip_init, 480 .pr_ctlport = NULL, 481 .pr_usrreqs = &rip_usrreqs 482 }, 483 #ifdef CARP 484 { 485 .pr_type = SOCK_RAW, 486 .pr_domain = &inetdomain, 487 .pr_protocol = IPPROTO_CARP, 488 .pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE, 489 490 .pr_input = carp_proto_input, 491 .pr_output = rip_output, 492 .pr_ctlinput = carp_proto_ctlinput, 493 .pr_ctloutput = rip_ctloutput, 494 495 .pr_ctlport = cpu0_ctlport, 496 .pr_usrreqs = &rip_usrreqs 497 }, 498 #endif 499 }; 500 501 static void 502 inetdomain_init(void) 503 { 504 in_pcbglobalinit(); 505 } 506 507 struct domain inetdomain = { 508 AF_INET, "internet", inetdomain_init, NULL, NULL, 509 inetsw, &inetsw[NELEM(inetsw)], 510 SLIST_ENTRY_INITIALIZER, 511 in_inithead, 32, sizeof(struct sockaddr_in), 512 }; 513 514 DOMAIN_SET(inet); 515 516 SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW, 0, 517 "Internet Family"); 518 519 SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW, 0, "IP"); 520 SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW, 0, "ICMP"); 521 SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW, 0, "UDP"); 522 SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW, 0, "TCP"); 523 SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW, 0, "IGMP"); 524 #ifdef FAST_IPSEC 525 /* XXX no protocol # to use, pick something "reserved" */ 526 SYSCTL_NODE(_net_inet, 253, ipsec, CTLFLAG_RW, 0, "IPSEC"); 527 SYSCTL_NODE(_net_inet, IPPROTO_AH, ah, CTLFLAG_RW, 0, "AH"); 528 SYSCTL_NODE(_net_inet, IPPROTO_ESP, esp, CTLFLAG_RW, 0, "ESP"); 529 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP, ipcomp, CTLFLAG_RW, 0, "IPCOMP"); 530 SYSCTL_NODE(_net_inet, IPPROTO_IPIP, ipip, CTLFLAG_RW, 0, "IPIP"); 531 #else 532 #ifdef IPSEC 533 SYSCTL_NODE(_net_inet, IPPROTO_AH, ipsec, CTLFLAG_RW, 0, "IPSEC"); 534 #endif /* IPSEC */ 535 #endif /* !FAST_IPSEC */ 536 SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW, 0, "RAW"); 537 #ifdef IPDIVERT 538 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "DIVERT"); 539 #endif 540 #ifdef PIM 541 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM"); 542 #endif 543 #ifdef CARP 544 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 545 #endif 546