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