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