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_mrouting.h" 36 #include "opt_ipsec.h" 37 #include "opt_inet.h" 38 #include "opt_inet6.h" 39 #include "opt_sctp.h" 40 #include "opt_mpath.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/socket.h> 47 #include <sys/domain.h> 48 #include <sys/proc.h> 49 #include <sys/protosw.h> 50 #include <sys/queue.h> 51 #include <sys/sysctl.h> 52 53 /* 54 * While this file provides the domain and protocol switch tables for IPv4, it 55 * also provides the sysctl node declarations for net.inet.* often shared with 56 * IPv6 for common features or by upper layer protocols. In case of no IPv4 57 * support compile out everything but these sysctl nodes. 58 */ 59 #ifdef INET 60 #include <net/if.h> 61 #include <net/if_var.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 91 static 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 FEATURE(inet, "Internet Protocol version 4"); 105 106 extern struct domain inetdomain; 107 108 /* Spacer for loadable protocols. */ 109 #define IPPROTOSPACER \ 110 { \ 111 .pr_domain = &inetdomain, \ 112 .pr_protocol = PROTO_SPACER, \ 113 .pr_usrreqs = &nousrreqs \ 114 } 115 116 struct protosw inetsw[] = { 117 { 118 .pr_type = 0, 119 .pr_domain = &inetdomain, 120 .pr_protocol = IPPROTO_IP, 121 .pr_init = ip_init, 122 .pr_slowtimo = ip_slowtimo, 123 .pr_drain = ip_drain, 124 .pr_usrreqs = &nousrreqs 125 }, 126 { 127 .pr_type = SOCK_DGRAM, 128 .pr_domain = &inetdomain, 129 .pr_protocol = IPPROTO_UDP, 130 .pr_flags = PR_ATOMIC|PR_ADDR, 131 .pr_input = udp_input, 132 .pr_ctlinput = udp_ctlinput, 133 .pr_ctloutput = udp_ctloutput, 134 .pr_init = udp_init, 135 .pr_usrreqs = &udp_usrreqs 136 }, 137 { 138 .pr_type = SOCK_STREAM, 139 .pr_domain = &inetdomain, 140 .pr_protocol = IPPROTO_TCP, 141 .pr_flags = PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD, 142 .pr_input = tcp_input, 143 .pr_ctlinput = tcp_ctlinput, 144 .pr_ctloutput = tcp_ctloutput, 145 .pr_init = tcp_init, 146 .pr_slowtimo = tcp_slowtimo, 147 .pr_drain = tcp_drain, 148 .pr_usrreqs = &tcp_usrreqs 149 }, 150 #ifdef SCTP 151 { 152 .pr_type = SOCK_SEQPACKET, 153 .pr_domain = &inetdomain, 154 .pr_protocol = IPPROTO_SCTP, 155 .pr_flags = PR_WANTRCVD, 156 .pr_input = sctp_input, 157 .pr_ctlinput = sctp_ctlinput, 158 .pr_ctloutput = sctp_ctloutput, 159 .pr_init = sctp_init, 160 .pr_drain = sctp_drain, 161 .pr_usrreqs = &sctp_usrreqs 162 }, 163 { 164 .pr_type = SOCK_STREAM, 165 .pr_domain = &inetdomain, 166 .pr_protocol = IPPROTO_SCTP, 167 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD, 168 .pr_input = sctp_input, 169 .pr_ctlinput = sctp_ctlinput, 170 .pr_ctloutput = sctp_ctloutput, 171 .pr_drain = sctp_drain, 172 .pr_usrreqs = &sctp_usrreqs 173 }, 174 #endif /* SCTP */ 175 { 176 .pr_type = SOCK_DGRAM, 177 .pr_domain = &inetdomain, 178 .pr_protocol = IPPROTO_UDPLITE, 179 .pr_flags = PR_ATOMIC|PR_ADDR, 180 .pr_input = udp_input, 181 .pr_ctlinput = udplite_ctlinput, 182 .pr_ctloutput = udp_ctloutput, 183 .pr_init = udplite_init, 184 .pr_usrreqs = &udp_usrreqs 185 }, 186 { 187 .pr_type = SOCK_RAW, 188 .pr_domain = &inetdomain, 189 .pr_protocol = IPPROTO_RAW, 190 .pr_flags = PR_ATOMIC|PR_ADDR, 191 .pr_input = rip_input, 192 .pr_ctlinput = rip_ctlinput, 193 .pr_ctloutput = rip_ctloutput, 194 .pr_usrreqs = &rip_usrreqs 195 }, 196 { 197 .pr_type = SOCK_RAW, 198 .pr_domain = &inetdomain, 199 .pr_protocol = IPPROTO_ICMP, 200 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 201 .pr_input = icmp_input, 202 .pr_ctloutput = rip_ctloutput, 203 .pr_usrreqs = &rip_usrreqs 204 }, 205 { 206 .pr_type = SOCK_RAW, 207 .pr_domain = &inetdomain, 208 .pr_protocol = IPPROTO_IGMP, 209 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 210 .pr_input = igmp_input, 211 .pr_ctloutput = rip_ctloutput, 212 .pr_fasttimo = igmp_fasttimo, 213 .pr_slowtimo = igmp_slowtimo, 214 .pr_usrreqs = &rip_usrreqs 215 }, 216 { 217 .pr_type = SOCK_RAW, 218 .pr_domain = &inetdomain, 219 .pr_protocol = IPPROTO_RSVP, 220 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 221 .pr_input = rsvp_input, 222 .pr_ctloutput = rip_ctloutput, 223 .pr_usrreqs = &rip_usrreqs 224 }, 225 #ifdef IPSEC 226 { 227 .pr_type = SOCK_RAW, 228 .pr_domain = &inetdomain, 229 .pr_protocol = IPPROTO_AH, 230 .pr_flags = PR_ATOMIC|PR_ADDR, 231 .pr_input = ah4_input, 232 .pr_ctlinput = ah4_ctlinput, 233 .pr_usrreqs = &nousrreqs 234 }, 235 { 236 .pr_type = SOCK_RAW, 237 .pr_domain = &inetdomain, 238 .pr_protocol = IPPROTO_ESP, 239 .pr_flags = PR_ATOMIC|PR_ADDR, 240 .pr_input = esp4_input, 241 .pr_ctlinput = esp4_ctlinput, 242 .pr_usrreqs = &nousrreqs 243 }, 244 { 245 .pr_type = SOCK_RAW, 246 .pr_domain = &inetdomain, 247 .pr_protocol = IPPROTO_IPCOMP, 248 .pr_flags = PR_ATOMIC|PR_ADDR, 249 .pr_input = ipcomp4_input, 250 .pr_usrreqs = &nousrreqs 251 }, 252 #endif /* IPSEC */ 253 { 254 .pr_type = SOCK_RAW, 255 .pr_domain = &inetdomain, 256 .pr_protocol = IPPROTO_IPV4, 257 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 258 .pr_input = encap4_input, 259 .pr_ctloutput = rip_ctloutput, 260 .pr_init = encap_init, 261 .pr_usrreqs = &rip_usrreqs 262 }, 263 { 264 .pr_type = SOCK_RAW, 265 .pr_domain = &inetdomain, 266 .pr_protocol = IPPROTO_MOBILE, 267 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 268 .pr_input = encap4_input, 269 .pr_ctloutput = rip_ctloutput, 270 .pr_init = encap_init, 271 .pr_usrreqs = &rip_usrreqs 272 }, 273 { 274 .pr_type = SOCK_RAW, 275 .pr_domain = &inetdomain, 276 .pr_protocol = IPPROTO_ETHERIP, 277 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 278 .pr_input = encap4_input, 279 .pr_ctloutput = rip_ctloutput, 280 .pr_init = encap_init, 281 .pr_usrreqs = &rip_usrreqs 282 }, 283 { 284 .pr_type = SOCK_RAW, 285 .pr_domain = &inetdomain, 286 .pr_protocol = IPPROTO_GRE, 287 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 288 .pr_input = encap4_input, 289 .pr_ctloutput = rip_ctloutput, 290 .pr_init = encap_init, 291 .pr_usrreqs = &rip_usrreqs 292 }, 293 # ifdef INET6 294 { 295 .pr_type = SOCK_RAW, 296 .pr_domain = &inetdomain, 297 .pr_protocol = IPPROTO_IPV6, 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 #endif 305 { 306 .pr_type = SOCK_RAW, 307 .pr_domain = &inetdomain, 308 .pr_protocol = IPPROTO_PIM, 309 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 310 .pr_input = encap4_input, 311 .pr_ctloutput = rip_ctloutput, 312 .pr_usrreqs = &rip_usrreqs 313 }, 314 /* Spacer n-times for loadable protocols. */ 315 IPPROTOSPACER, 316 IPPROTOSPACER, 317 IPPROTOSPACER, 318 IPPROTOSPACER, 319 IPPROTOSPACER, 320 IPPROTOSPACER, 321 IPPROTOSPACER, 322 IPPROTOSPACER, 323 /* raw wildcard */ 324 { 325 .pr_type = SOCK_RAW, 326 .pr_domain = &inetdomain, 327 .pr_flags = PR_ATOMIC|PR_ADDR, 328 .pr_input = rip_input, 329 .pr_ctloutput = rip_ctloutput, 330 .pr_init = rip_init, 331 .pr_usrreqs = &rip_usrreqs 332 }, 333 }; 334 335 extern int in_inithead(void **, int); 336 extern int in_detachhead(void **, int); 337 338 struct domain inetdomain = { 339 .dom_family = AF_INET, 340 .dom_name = "internet", 341 .dom_protosw = inetsw, 342 .dom_protoswNPROTOSW = &inetsw[nitems(inetsw)], 343 #ifdef RADIX_MPATH 344 .dom_rtattach = rn4_mpath_inithead, 345 #else 346 .dom_rtattach = in_inithead, 347 #endif 348 #ifdef VIMAGE 349 .dom_rtdetach = in_detachhead, 350 #endif 351 .dom_ifattach = in_domifattach, 352 .dom_ifdetach = in_domifdetach 353 }; 354 355 VNET_DOMAIN_SET(inet); 356 #endif /* INET */ 357 358 SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW, 0, 359 "Internet Family"); 360 361 SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW, 0, "IP"); 362 SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW, 0, "ICMP"); 363 SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW, 0, "UDP"); 364 SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW, 0, "TCP"); 365 #ifdef SCTP 366 SYSCTL_NODE(_net_inet, IPPROTO_SCTP, sctp, CTLFLAG_RW, 0, "SCTP"); 367 #endif 368 SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW, 0, "IGMP"); 369 #ifdef IPSEC 370 /* XXX no protocol # to use, pick something "reserved" */ 371 SYSCTL_NODE(_net_inet, 253, ipsec, CTLFLAG_RW, 0, "IPSEC"); 372 SYSCTL_NODE(_net_inet, IPPROTO_AH, ah, CTLFLAG_RW, 0, "AH"); 373 SYSCTL_NODE(_net_inet, IPPROTO_ESP, esp, CTLFLAG_RW, 0, "ESP"); 374 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP, ipcomp, CTLFLAG_RW, 0, "IPCOMP"); 375 SYSCTL_NODE(_net_inet, IPPROTO_IPIP, ipip, CTLFLAG_RW, 0, "IPIP"); 376 #endif /* IPSEC */ 377 SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW, 0, "RAW"); 378 SYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, 379 "Accept filters"); 380