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