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