in_proto.c revision 189106
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 189106 2009-02-27 14:12:05Z 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_route.h" 40#include "opt_pf.h" 41#include "opt_carp.h" 42#include "opt_sctp.h" 43#include "opt_mpath.h" 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/socket.h> 49#include <sys/domain.h> 50#include <sys/proc.h> 51#include <sys/protosw.h> 52#include <sys/queue.h> 53#include <sys/sysctl.h> 54 55#include <net/if.h> 56#include <net/route.h> 57#ifdef RADIX_MPATH 58#include <net/radix_mpath.h> 59#endif 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 .pr_slowtimo = ip_slowtimo, 118 .pr_drain = ip_drain, 119 .pr_usrreqs = &nousrreqs 120}, 121{ 122 .pr_type = SOCK_DGRAM, 123 .pr_domain = &inetdomain, 124 .pr_protocol = IPPROTO_UDP, 125 .pr_flags = PR_ATOMIC|PR_ADDR, 126 .pr_input = udp_input, 127 .pr_ctlinput = udp_ctlinput, 128 .pr_ctloutput = ip_ctloutput, 129 .pr_init = udp_init, 130 .pr_usrreqs = &udp_usrreqs 131}, 132{ 133 .pr_type = SOCK_STREAM, 134 .pr_domain = &inetdomain, 135 .pr_protocol = IPPROTO_TCP, 136 .pr_flags = PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD, 137 .pr_input = tcp_input, 138 .pr_ctlinput = tcp_ctlinput, 139 .pr_ctloutput = tcp_ctloutput, 140 .pr_init = tcp_init, 141 .pr_slowtimo = tcp_slowtimo, 142 .pr_drain = tcp_drain, 143 .pr_usrreqs = &tcp_usrreqs 144}, 145#ifdef SCTP 146{ 147 .pr_type = SOCK_DGRAM, 148 .pr_domain = &inetdomain, 149 .pr_protocol = IPPROTO_SCTP, 150 .pr_flags = PR_WANTRCVD, 151 .pr_input = sctp_input, 152 .pr_ctlinput = sctp_ctlinput, 153 .pr_ctloutput = sctp_ctloutput, 154 .pr_init = sctp_init, 155 .pr_drain = sctp_drain, 156 .pr_usrreqs = &sctp_usrreqs 157}, 158{ 159 .pr_type = SOCK_SEQPACKET, 160 .pr_domain = &inetdomain, 161 .pr_protocol = IPPROTO_SCTP, 162 .pr_flags = PR_WANTRCVD, 163 .pr_input = sctp_input, 164 .pr_ctlinput = sctp_ctlinput, 165 .pr_ctloutput = sctp_ctloutput, 166 .pr_drain = sctp_drain, 167 .pr_usrreqs = &sctp_usrreqs 168}, 169 170{ 171 .pr_type = SOCK_STREAM, 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#endif /* SCTP */ 182{ 183 .pr_type = SOCK_RAW, 184 .pr_domain = &inetdomain, 185 .pr_protocol = IPPROTO_RAW, 186 .pr_flags = PR_ATOMIC|PR_ADDR, 187 .pr_input = rip_input, 188 .pr_ctlinput = rip_ctlinput, 189 .pr_ctloutput = rip_ctloutput, 190 .pr_usrreqs = &rip_usrreqs 191}, 192{ 193 .pr_type = SOCK_RAW, 194 .pr_domain = &inetdomain, 195 .pr_protocol = IPPROTO_ICMP, 196 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 197 .pr_input = icmp_input, 198 .pr_ctloutput = rip_ctloutput, 199 .pr_init = icmp_init, 200 .pr_usrreqs = &rip_usrreqs 201}, 202{ 203 .pr_type = SOCK_RAW, 204 .pr_domain = &inetdomain, 205 .pr_protocol = IPPROTO_IGMP, 206 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 207 .pr_input = igmp_input, 208 .pr_ctloutput = rip_ctloutput, 209 .pr_init = igmp_init, 210 .pr_fasttimo = igmp_fasttimo, 211 .pr_slowtimo = igmp_slowtimo, 212 .pr_usrreqs = &rip_usrreqs 213}, 214{ 215 .pr_type = SOCK_RAW, 216 .pr_domain = &inetdomain, 217 .pr_protocol = IPPROTO_RSVP, 218 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 219 .pr_input = rsvp_input, 220 .pr_ctloutput = rip_ctloutput, 221 .pr_usrreqs = &rip_usrreqs 222}, 223#ifdef IPSEC 224{ 225 .pr_type = SOCK_RAW, 226 .pr_domain = &inetdomain, 227 .pr_protocol = IPPROTO_AH, 228 .pr_flags = PR_ATOMIC|PR_ADDR, 229 .pr_input = ah4_input, 230 .pr_ctlinput = ah4_ctlinput, 231 .pr_usrreqs = &nousrreqs 232}, 233{ 234 .pr_type = SOCK_RAW, 235 .pr_domain = &inetdomain, 236 .pr_protocol = IPPROTO_ESP, 237 .pr_flags = PR_ATOMIC|PR_ADDR, 238 .pr_input = esp4_input, 239 .pr_ctlinput = esp4_ctlinput, 240 .pr_usrreqs = &nousrreqs 241}, 242{ 243 .pr_type = SOCK_RAW, 244 .pr_domain = &inetdomain, 245 .pr_protocol = IPPROTO_IPCOMP, 246 .pr_flags = PR_ATOMIC|PR_ADDR, 247 .pr_input = ipcomp4_input, 248 .pr_usrreqs = &nousrreqs 249}, 250#endif /* IPSEC */ 251{ 252 .pr_type = SOCK_RAW, 253 .pr_domain = &inetdomain, 254 .pr_protocol = IPPROTO_IPV4, 255 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 256 .pr_input = encap4_input, 257 .pr_ctloutput = rip_ctloutput, 258 .pr_init = encap_init, 259 .pr_usrreqs = &rip_usrreqs 260}, 261{ 262 .pr_type = SOCK_RAW, 263 .pr_domain = &inetdomain, 264 .pr_protocol = IPPROTO_MOBILE, 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_ETHERIP, 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_GRE, 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# ifdef INET6 292{ 293 .pr_type = SOCK_RAW, 294 .pr_domain = &inetdomain, 295 .pr_protocol = IPPROTO_IPV6, 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#endif 303{ 304 .pr_type = SOCK_RAW, 305 .pr_domain = &inetdomain, 306 .pr_protocol = IPPROTO_PIM, 307 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 308 .pr_input = encap4_input, 309 .pr_ctloutput = rip_ctloutput, 310 .pr_usrreqs = &rip_usrreqs 311}, 312#ifdef DEV_PFSYNC 313{ 314 .pr_type = SOCK_RAW, 315 .pr_domain = &inetdomain, 316 .pr_protocol = IPPROTO_PFSYNC, 317 .pr_flags = PR_ATOMIC|PR_ADDR, 318 .pr_input = pfsync_input, 319 .pr_ctloutput = rip_ctloutput, 320 .pr_usrreqs = &rip_usrreqs 321}, 322#endif /* DEV_PFSYNC */ 323#ifdef DEV_CARP 324{ 325 .pr_type = SOCK_RAW, 326 .pr_domain = &inetdomain, 327 .pr_protocol = IPPROTO_CARP, 328 .pr_flags = PR_ATOMIC|PR_ADDR, 329 .pr_input = carp_input, 330 .pr_output = (pr_output_t*)rip_output, 331 .pr_ctloutput = rip_ctloutput, 332 .pr_usrreqs = &rip_usrreqs 333}, 334#endif /* DEV_CARP */ 335/* Spacer n-times for loadable protocols. */ 336IPPROTOSPACER, 337IPPROTOSPACER, 338IPPROTOSPACER, 339IPPROTOSPACER, 340IPPROTOSPACER, 341IPPROTOSPACER, 342IPPROTOSPACER, 343IPPROTOSPACER, 344/* raw wildcard */ 345{ 346 .pr_type = SOCK_RAW, 347 .pr_domain = &inetdomain, 348 .pr_flags = PR_ATOMIC|PR_ADDR, 349 .pr_input = rip_input, 350 .pr_ctloutput = rip_ctloutput, 351 .pr_init = rip_init, 352 .pr_usrreqs = &rip_usrreqs 353}, 354}; 355 356extern int in_inithead(void **, int); 357 358struct domain inetdomain = { 359 .dom_family = AF_INET, 360 .dom_name = "internet", 361 .dom_protosw = inetsw, 362 .dom_protoswNPROTOSW = &inetsw[sizeof(inetsw)/sizeof(inetsw[0])], 363#ifdef RADIX_MPATH 364 .dom_rtattach = rn4_mpath_inithead, 365#else 366 .dom_rtattach = in_inithead, 367#endif 368 .dom_rtoffset = 32, 369 .dom_maxrtkey = sizeof(struct sockaddr_in), 370 .dom_ifattach = in_domifattach, 371 .dom_ifdetach = in_domifdetach 372}; 373 374DOMAIN_SET(inet); 375 376SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW, 0, 377 "Internet Family"); 378 379SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW, 0, "IP"); 380SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW, 0, "ICMP"); 381SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW, 0, "UDP"); 382SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW, 0, "TCP"); 383#ifdef SCTP 384SYSCTL_NODE(_net_inet, IPPROTO_SCTP, sctp, CTLFLAG_RW, 0, "SCTP"); 385#endif 386SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW, 0, "IGMP"); 387#ifdef IPSEC 388/* XXX no protocol # to use, pick something "reserved" */ 389SYSCTL_NODE(_net_inet, 253, ipsec, CTLFLAG_RW, 0, "IPSEC"); 390SYSCTL_NODE(_net_inet, IPPROTO_AH, ah, CTLFLAG_RW, 0, "AH"); 391SYSCTL_NODE(_net_inet, IPPROTO_ESP, esp, CTLFLAG_RW, 0, "ESP"); 392SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP, ipcomp, CTLFLAG_RW, 0, "IPCOMP"); 393SYSCTL_NODE(_net_inet, IPPROTO_IPIP, ipip, CTLFLAG_RW, 0, "IPIP"); 394#endif /* IPSEC */ 395SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW, 0, "RAW"); 396#ifdef DEV_PFSYNC 397SYSCTL_NODE(_net_inet, IPPROTO_PFSYNC, pfsync, CTLFLAG_RW, 0, "PFSYNC"); 398#endif 399#ifdef DEV_CARP 400SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 401#endif 402