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