in_proto.c revision 166622
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 * 4. 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: head/sys/netinet/in_proto.c 166622 2007-02-10 13:59:13Z bms $ 31 */ 32 33#include "opt_ipx.h" 34#include "opt_mrouting.h" 35#include "opt_ipsec.h" 36#include "opt_inet6.h" 37#include "opt_pf.h" 38#include "opt_carp.h" 39#include "opt_sctp.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/socket.h> 45#include <sys/domain.h> 46#include <sys/protosw.h> 47#include <sys/queue.h> 48#include <sys/sysctl.h> 49 50#include <net/if.h> 51#include <net/route.h> 52 53#include <netinet/in.h> 54#include <netinet/in_systm.h> 55#include <netinet/ip.h> 56#include <netinet/ip_var.h> 57#include <netinet/ip_icmp.h> 58#include <netinet/igmp_var.h> 59#include <netinet/tcp.h> 60#include <netinet/tcp_timer.h> 61#include <netinet/tcp_var.h> 62#include <netinet/udp.h> 63#include <netinet/udp_var.h> 64#include <netinet/ip_encap.h> 65 66/* 67 * TCP/IP protocol family: IP, ICMP, UDP, TCP. 68 */ 69 70static struct pr_usrreqs nousrreqs; 71 72#ifdef IPSEC 73#include <netinet6/ipsec.h> 74#include <netinet6/ah.h> 75#ifdef IPSEC_ESP 76#include <netinet6/esp.h> 77#endif 78#include <netinet6/ipcomp.h> 79#endif /* IPSEC */ 80 81#ifdef FAST_IPSEC 82#include <netipsec/ipsec.h> 83#endif /* FAST_IPSEC */ 84 85#ifdef IPXIP 86#include <netipx/ipx_ip.h> 87#endif 88 89#ifdef SCTP 90#include <netinet/in_pcb.h> 91#include <netinet/sctp_pcb.h> 92#include <netinet/sctp.h> 93#include <netinet/sctp_var.h> 94#endif /* SCTP */ 95 96#ifdef DEV_PFSYNC 97#include <net/pfvar.h> 98#include <net/if_pfsync.h> 99#endif 100 101#ifdef DEV_CARP 102#include <netinet/ip_carp.h> 103#endif 104 105extern struct domain inetdomain; 106 107/* Spacer for loadable protocols. */ 108#define IPPROTOSPACER \ 109{ \ 110 .pr_domain = &inetdomain, \ 111 .pr_protocol = PROTO_SPACER, \ 112 .pr_usrreqs = &nousrreqs \ 113} 114 115struct protosw inetsw[] = { 116{ 117 .pr_type = 0, 118 .pr_domain = &inetdomain, 119 .pr_protocol = IPPROTO_IP, 120 .pr_init = ip_init, 121 .pr_slowtimo = ip_slowtimo, 122 .pr_drain = ip_drain, 123 .pr_usrreqs = &nousrreqs 124}, 125{ 126 .pr_type = SOCK_DGRAM, 127 .pr_domain = &inetdomain, 128 .pr_protocol = IPPROTO_UDP, 129 .pr_flags = PR_ATOMIC|PR_ADDR, 130 .pr_input = udp_input, 131 .pr_ctlinput = udp_ctlinput, 132 .pr_ctloutput = ip_ctloutput, 133 .pr_init = udp_init, 134 .pr_usrreqs = &udp_usrreqs 135}, 136{ 137 .pr_type = SOCK_STREAM, 138 .pr_domain = &inetdomain, 139 .pr_protocol = IPPROTO_TCP, 140 .pr_flags = PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD, 141 .pr_input = tcp_input, 142 .pr_ctlinput = tcp_ctlinput, 143 .pr_ctloutput = tcp_ctloutput, 144 .pr_init = tcp_init, 145 .pr_slowtimo = tcp_slowtimo, 146 .pr_drain = tcp_drain, 147 .pr_usrreqs = &tcp_usrreqs 148}, 149#ifdef SCTP 150{ 151 .pr_type = SOCK_DGRAM, 152 .pr_domain = &inetdomain, 153 .pr_protocol = IPPROTO_SCTP, 154 .pr_flags = PR_WANTRCVD, 155 .pr_input = sctp_input, 156 .pr_ctlinput = sctp_ctlinput, 157 .pr_ctloutput = sctp_ctloutput, 158 .pr_init = sctp_init, 159 .pr_drain = sctp_drain, 160 .pr_usrreqs = &sctp_usrreqs 161}, 162{ 163 .pr_type = SOCK_SEQPACKET, 164 .pr_domain = &inetdomain, 165 .pr_protocol = IPPROTO_SCTP, 166 .pr_flags = PR_WANTRCVD, 167 .pr_input = sctp_input, 168 .pr_ctlinput = sctp_ctlinput, 169 .pr_ctloutput = sctp_ctloutput, 170 .pr_drain = sctp_drain, 171 .pr_usrreqs = &sctp_usrreqs 172}, 173 174{ 175 .pr_type = SOCK_STREAM, 176 .pr_domain = &inetdomain, 177 .pr_protocol = IPPROTO_SCTP, 178 .pr_flags = PR_WANTRCVD, 179 .pr_input = sctp_input, 180 .pr_ctlinput = sctp_ctlinput, 181 .pr_ctloutput = sctp_ctloutput, 182 .pr_drain = sctp_drain, 183 .pr_usrreqs = &sctp_usrreqs 184}, 185#endif /* SCTP */ 186{ 187 .pr_type = SOCK_RAW, 188 .pr_domain = &inetdomain, 189 .pr_protocol = IPPROTO_RAW, 190 .pr_flags = PR_ATOMIC|PR_ADDR, 191 .pr_input = rip_input, 192 .pr_ctlinput = rip_ctlinput, 193 .pr_ctloutput = rip_ctloutput, 194 .pr_usrreqs = &rip_usrreqs 195}, 196{ 197 .pr_type = SOCK_RAW, 198 .pr_domain = &inetdomain, 199 .pr_protocol = IPPROTO_ICMP, 200 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 201 .pr_input = icmp_input, 202 .pr_ctloutput = rip_ctloutput, 203 .pr_usrreqs = &rip_usrreqs 204}, 205{ 206 .pr_type = SOCK_RAW, 207 .pr_domain = &inetdomain, 208 .pr_protocol = IPPROTO_IGMP, 209 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 210 .pr_input = igmp_input, 211 .pr_ctloutput = rip_ctloutput, 212 .pr_init = igmp_init, 213 .pr_fasttimo = igmp_fasttimo, 214 .pr_slowtimo = igmp_slowtimo, 215 .pr_usrreqs = &rip_usrreqs 216}, 217{ 218 .pr_type = SOCK_RAW, 219 .pr_domain = &inetdomain, 220 .pr_protocol = IPPROTO_RSVP, 221 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 222 .pr_input = rsvp_input, 223 .pr_ctloutput = rip_ctloutput, 224 .pr_usrreqs = &rip_usrreqs 225}, 226#ifdef IPSEC 227{ 228 .pr_type = SOCK_RAW, 229 .pr_domain = &inetdomain, 230 .pr_protocol = IPPROTO_AH, 231 .pr_flags = PR_ATOMIC|PR_ADDR, 232 .pr_input = ah4_input, 233 .pr_usrreqs = &nousrreqs 234}, 235#ifdef IPSEC_ESP 236{ 237 .pr_type = SOCK_RAW, 238 .pr_domain = &inetdomain, 239 .pr_protocol = IPPROTO_ESP, 240 .pr_flags = PR_ATOMIC|PR_ADDR, 241 .pr_input = esp4_input, 242 .pr_usrreqs = &nousrreqs 243}, 244#endif 245{ 246 .pr_type = SOCK_RAW, 247 .pr_domain = &inetdomain, 248 .pr_protocol = IPPROTO_IPCOMP, 249 .pr_flags = PR_ATOMIC|PR_ADDR, 250 .pr_input = ipcomp4_input, 251 .pr_usrreqs = &nousrreqs 252}, 253#endif /* IPSEC */ 254#ifdef FAST_IPSEC 255{ 256 .pr_type = SOCK_RAW, 257 .pr_domain = &inetdomain, 258 .pr_protocol = IPPROTO_AH, 259 .pr_flags = PR_ATOMIC|PR_ADDR, 260 .pr_input = ah4_input, 261 .pr_ctlinput = ah4_ctlinput, 262 .pr_usrreqs = &nousrreqs 263}, 264{ 265 .pr_type = SOCK_RAW, 266 .pr_domain = &inetdomain, 267 .pr_protocol = IPPROTO_ESP, 268 .pr_flags = PR_ATOMIC|PR_ADDR, 269 .pr_input = esp4_input, 270 .pr_ctlinput = esp4_ctlinput, 271 .pr_usrreqs = &nousrreqs 272}, 273{ 274 .pr_type = SOCK_RAW, 275 .pr_domain = &inetdomain, 276 .pr_protocol = IPPROTO_IPCOMP, 277 .pr_flags = PR_ATOMIC|PR_ADDR, 278 .pr_input = ipcomp4_input, 279 .pr_usrreqs = &nousrreqs 280}, 281#endif /* FAST_IPSEC */ 282{ 283 .pr_type = SOCK_RAW, 284 .pr_domain = &inetdomain, 285 .pr_protocol = IPPROTO_IPV4, 286 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 287 .pr_input = encap4_input, 288 .pr_ctloutput = rip_ctloutput, 289 .pr_init = encap_init, 290 .pr_usrreqs = &rip_usrreqs 291}, 292{ 293 .pr_type = SOCK_RAW, 294 .pr_domain = &inetdomain, 295 .pr_protocol = IPPROTO_MOBILE, 296 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 297 .pr_input = encap4_input, 298 .pr_ctloutput = rip_ctloutput, 299 .pr_init = encap_init, 300 .pr_usrreqs = &rip_usrreqs 301}, 302{ 303 .pr_type = SOCK_RAW, 304 .pr_domain = &inetdomain, 305 .pr_protocol = IPPROTO_ETHERIP, 306 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 307 .pr_input = encap4_input, 308 .pr_ctloutput = rip_ctloutput, 309 .pr_init = encap_init, 310 .pr_usrreqs = &rip_usrreqs 311}, 312{ 313 .pr_type = SOCK_RAW, 314 .pr_domain = &inetdomain, 315 .pr_protocol = IPPROTO_GRE, 316 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 317 .pr_input = encap4_input, 318 .pr_ctloutput = rip_ctloutput, 319 .pr_init = encap_init, 320 .pr_usrreqs = &rip_usrreqs 321}, 322# ifdef INET6 323{ 324 .pr_type = SOCK_RAW, 325 .pr_domain = &inetdomain, 326 .pr_protocol = IPPROTO_IPV6, 327 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 328 .pr_input = encap4_input, 329 .pr_ctloutput = rip_ctloutput, 330 .pr_init = encap_init, 331 .pr_usrreqs = &rip_usrreqs 332}, 333#endif 334#ifdef IPXIP 335{ 336 .pr_type = SOCK_RAW, 337 .pr_domain = &inetdomain, 338 .pr_protocol = IPPROTO_IDP, 339 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 340 .pr_input = ipxip_input, 341 .pr_ctlinput = ipxip_ctlinput, 342 .pr_usrreqs = &rip_usrreqs 343}, 344#endif 345{ 346 .pr_type = SOCK_RAW, 347 .pr_domain = &inetdomain, 348 .pr_protocol = IPPROTO_PIM, 349 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 350 .pr_input = encap4_input, 351 .pr_ctloutput = rip_ctloutput, 352 .pr_usrreqs = &rip_usrreqs 353}, 354#ifdef DEV_PFSYNC 355{ 356 .pr_type = SOCK_RAW, 357 .pr_domain = &inetdomain, 358 .pr_protocol = IPPROTO_PFSYNC, 359 .pr_flags = PR_ATOMIC|PR_ADDR, 360 .pr_input = pfsync_input, 361 .pr_ctloutput = rip_ctloutput, 362 .pr_usrreqs = &rip_usrreqs 363}, 364#endif /* DEV_PFSYNC */ 365#ifdef DEV_CARP 366{ 367 .pr_type = SOCK_RAW, 368 .pr_domain = &inetdomain, 369 .pr_protocol = IPPROTO_CARP, 370 .pr_flags = PR_ATOMIC|PR_ADDR, 371 .pr_input = carp_input, 372 .pr_output = (pr_output_t*)rip_output, 373 .pr_ctloutput = rip_ctloutput, 374 .pr_usrreqs = &rip_usrreqs 375}, 376#endif /* DEV_CARP */ 377/* Spacer n-times for loadable protocols. */ 378IPPROTOSPACER, 379IPPROTOSPACER, 380IPPROTOSPACER, 381IPPROTOSPACER, 382IPPROTOSPACER, 383IPPROTOSPACER, 384IPPROTOSPACER, 385IPPROTOSPACER, 386/* raw wildcard */ 387{ 388 .pr_type = SOCK_RAW, 389 .pr_domain = &inetdomain, 390 .pr_flags = PR_ATOMIC|PR_ADDR, 391 .pr_input = rip_input, 392 .pr_ctloutput = rip_ctloutput, 393 .pr_init = rip_init, 394 .pr_usrreqs = &rip_usrreqs 395}, 396}; 397 398extern int in_inithead(void **, int); 399 400struct domain inetdomain = { 401 .dom_family = AF_INET, 402 .dom_name = "internet", 403 .dom_protosw = inetsw, 404 .dom_protoswNPROTOSW = &inetsw[sizeof(inetsw)/sizeof(inetsw[0])], 405 .dom_rtattach = in_inithead, 406 .dom_rtoffset = 32, 407 .dom_maxrtkey = sizeof(struct sockaddr_in) 408}; 409 410DOMAIN_SET(inet); 411 412SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW, 0, 413 "Internet Family"); 414 415SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW, 0, "IP"); 416SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW, 0, "ICMP"); 417SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW, 0, "UDP"); 418SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW, 0, "TCP"); 419#ifdef SCTP 420SYSCTL_NODE(_net_inet, IPPROTO_SCTP, sctp, CTLFLAG_RW, 0, "SCTP"); 421#endif 422SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW, 0, "IGMP"); 423#ifdef FAST_IPSEC 424/* XXX no protocol # to use, pick something "reserved" */ 425SYSCTL_NODE(_net_inet, 253, ipsec, CTLFLAG_RW, 0, "IPSEC"); 426SYSCTL_NODE(_net_inet, IPPROTO_AH, ah, CTLFLAG_RW, 0, "AH"); 427SYSCTL_NODE(_net_inet, IPPROTO_ESP, esp, CTLFLAG_RW, 0, "ESP"); 428SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP, ipcomp, CTLFLAG_RW, 0, "IPCOMP"); 429SYSCTL_NODE(_net_inet, IPPROTO_IPIP, ipip, CTLFLAG_RW, 0, "IPIP"); 430#else 431#ifdef IPSEC 432SYSCTL_NODE(_net_inet, IPPROTO_AH, ipsec, CTLFLAG_RW, 0, "IPSEC"); 433#endif /* IPSEC */ 434#endif /* !FAST_IPSEC */ 435SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW, 0, "RAW"); 436#ifdef DEV_PFSYNC 437SYSCTL_NODE(_net_inet, IPPROTO_PFSYNC, pfsync, CTLFLAG_RW, 0, "PFSYNC"); 438#endif 439#ifdef DEV_CARP 440SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 441#endif 442