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