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 * $FreeBSD$ 31 */ 32 33 #include "opt_ipx.h" 34 #include "opt_mrouting.h" 35 #include "opt_ipsec.h" 36 #include "opt_inet6.h" 37 #include "opt_pf.h" 38 #include "opt_carp.h" 39 #include "opt_sctp.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/socket.h> 45 #include <sys/domain.h> 46 #include <sys/protosw.h> 47 #include <sys/queue.h> 48 #include <sys/sysctl.h> 49 50 #include <net/if.h> 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/ip_var.h> 57 #include <netinet/ip_icmp.h> 58 #include <netinet/igmp_var.h> 59 #ifdef PIM 60 #include <netinet/pim_var.h> 61 #endif 62 #include <netinet/tcp.h> 63 #include <netinet/tcp_timer.h> 64 #include <netinet/tcp_var.h> 65 #include <netinet/udp.h> 66 #include <netinet/udp_var.h> 67 #include <netinet/ip_encap.h> 68 69 /* 70 * TCP/IP protocol family: IP, ICMP, UDP, TCP. 71 */ 72 73 static struct pr_usrreqs nousrreqs; 74 75 #ifdef IPSEC 76 #include <netinet6/ipsec.h> 77 #include <netinet6/ah.h> 78 #ifdef IPSEC_ESP 79 #include <netinet6/esp.h> 80 #endif 81 #include <netinet6/ipcomp.h> 82 #endif /* IPSEC */ 83 84 #ifdef FAST_IPSEC 85 #include <netipsec/ipsec.h> 86 #endif /* FAST_IPSEC */ 87 88 #ifdef IPXIP 89 #include <netipx/ipx_ip.h> 90 #endif 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 #ifdef DEV_CARP 105 #include <netinet/ip_carp.h> 106 #endif 107 108 extern struct domain inetdomain; 109 110 /* Spacer for loadable protocols. */ 111 #define IPPROTOSPACER \ 112 { \ 113 .pr_domain = &inetdomain, \ 114 .pr_protocol = PROTO_SPACER, \ 115 .pr_usrreqs = &nousrreqs \ 116 } 117 118 struct protosw inetsw[] = { 119 { 120 .pr_type = 0, 121 .pr_domain = &inetdomain, 122 .pr_protocol = IPPROTO_IP, 123 .pr_init = ip_init, 124 .pr_slowtimo = ip_slowtimo, 125 .pr_drain = ip_drain, 126 .pr_usrreqs = &nousrreqs 127 }, 128 { 129 .pr_type = SOCK_DGRAM, 130 .pr_domain = &inetdomain, 131 .pr_protocol = IPPROTO_UDP, 132 .pr_flags = PR_ATOMIC|PR_ADDR, 133 .pr_input = udp_input, 134 .pr_ctlinput = udp_ctlinput, 135 .pr_ctloutput = ip_ctloutput, 136 .pr_init = udp_init, 137 .pr_usrreqs = &udp_usrreqs 138 }, 139 { 140 .pr_type = SOCK_STREAM, 141 .pr_domain = &inetdomain, 142 .pr_protocol = IPPROTO_TCP, 143 .pr_flags = PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD, 144 .pr_input = tcp_input, 145 .pr_ctlinput = tcp_ctlinput, 146 .pr_ctloutput = tcp_ctloutput, 147 .pr_init = tcp_init, 148 .pr_slowtimo = tcp_slowtimo, 149 .pr_drain = tcp_drain, 150 .pr_usrreqs = &tcp_usrreqs 151 }, 152 #ifdef SCTP 153 { 154 .pr_type = SOCK_DGRAM, 155 .pr_domain = &inetdomain, 156 .pr_protocol = IPPROTO_SCTP, 157 .pr_flags = PR_WANTRCVD, 158 .pr_input = sctp_input, 159 .pr_ctlinput = sctp_ctlinput, 160 .pr_ctloutput = sctp_ctloutput, 161 .pr_init = sctp_init, 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_init = igmp_init, 216 .pr_fasttimo = igmp_fasttimo, 217 .pr_slowtimo = igmp_slowtimo, 218 .pr_usrreqs = &rip_usrreqs 219 }, 220 { 221 .pr_type = SOCK_RAW, 222 .pr_domain = &inetdomain, 223 .pr_protocol = IPPROTO_RSVP, 224 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 225 .pr_input = rsvp_input, 226 .pr_ctloutput = rip_ctloutput, 227 .pr_usrreqs = &rip_usrreqs 228 }, 229 #ifdef IPSEC 230 { 231 .pr_type = SOCK_RAW, 232 .pr_domain = &inetdomain, 233 .pr_protocol = IPPROTO_AH, 234 .pr_flags = PR_ATOMIC|PR_ADDR, 235 .pr_input = ah4_input, 236 .pr_usrreqs = &nousrreqs 237 }, 238 #ifdef IPSEC_ESP 239 { 240 .pr_type = SOCK_RAW, 241 .pr_domain = &inetdomain, 242 .pr_protocol = IPPROTO_ESP, 243 .pr_flags = PR_ATOMIC|PR_ADDR, 244 .pr_input = esp4_input, 245 .pr_usrreqs = &nousrreqs 246 }, 247 #endif 248 { 249 .pr_type = SOCK_RAW, 250 .pr_domain = &inetdomain, 251 .pr_protocol = IPPROTO_IPCOMP, 252 .pr_flags = PR_ATOMIC|PR_ADDR, 253 .pr_input = ipcomp4_input, 254 .pr_usrreqs = &nousrreqs 255 }, 256 #endif /* IPSEC */ 257 #ifdef FAST_IPSEC 258 { 259 .pr_type = SOCK_RAW, 260 .pr_domain = &inetdomain, 261 .pr_protocol = IPPROTO_AH, 262 .pr_flags = PR_ATOMIC|PR_ADDR, 263 .pr_input = ah4_input, 264 .pr_ctlinput = ah4_ctlinput, 265 .pr_usrreqs = &nousrreqs 266 }, 267 { 268 .pr_type = SOCK_RAW, 269 .pr_domain = &inetdomain, 270 .pr_protocol = IPPROTO_ESP, 271 .pr_flags = PR_ATOMIC|PR_ADDR, 272 .pr_input = esp4_input, 273 .pr_ctlinput = esp4_ctlinput, 274 .pr_usrreqs = &nousrreqs 275 }, 276 { 277 .pr_type = SOCK_RAW, 278 .pr_domain = &inetdomain, 279 .pr_protocol = IPPROTO_IPCOMP, 280 .pr_flags = PR_ATOMIC|PR_ADDR, 281 .pr_input = ipcomp4_input, 282 .pr_usrreqs = &nousrreqs 283 }, 284 #endif /* FAST_IPSEC */ 285 { 286 .pr_type = SOCK_RAW, 287 .pr_domain = &inetdomain, 288 .pr_protocol = IPPROTO_IPV4, 289 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 290 .pr_input = encap4_input, 291 .pr_ctloutput = rip_ctloutput, 292 .pr_init = encap_init, 293 .pr_usrreqs = &rip_usrreqs 294 }, 295 { 296 .pr_type = SOCK_RAW, 297 .pr_domain = &inetdomain, 298 .pr_protocol = IPPROTO_MOBILE, 299 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 300 .pr_input = encap4_input, 301 .pr_ctloutput = rip_ctloutput, 302 .pr_init = encap_init, 303 .pr_usrreqs = &rip_usrreqs 304 }, 305 { 306 .pr_type = SOCK_RAW, 307 .pr_domain = &inetdomain, 308 .pr_protocol = IPPROTO_ETHERIP, 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 { 316 .pr_type = SOCK_RAW, 317 .pr_domain = &inetdomain, 318 .pr_protocol = IPPROTO_GRE, 319 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 320 .pr_input = encap4_input, 321 .pr_ctloutput = rip_ctloutput, 322 .pr_init = encap_init, 323 .pr_usrreqs = &rip_usrreqs 324 }, 325 # ifdef INET6 326 { 327 .pr_type = SOCK_RAW, 328 .pr_domain = &inetdomain, 329 .pr_protocol = IPPROTO_IPV6, 330 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 331 .pr_input = encap4_input, 332 .pr_ctloutput = rip_ctloutput, 333 .pr_init = encap_init, 334 .pr_usrreqs = &rip_usrreqs 335 }, 336 #endif 337 #ifdef IPXIP 338 { 339 .pr_type = SOCK_RAW, 340 .pr_domain = &inetdomain, 341 .pr_protocol = IPPROTO_IDP, 342 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 343 .pr_input = ipxip_input, 344 .pr_ctlinput = ipxip_ctlinput, 345 .pr_usrreqs = &rip_usrreqs 346 }, 347 #endif 348 #ifdef PIM 349 { 350 .pr_type = SOCK_RAW, 351 .pr_domain = &inetdomain, 352 .pr_protocol = IPPROTO_PIM, 353 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 354 .pr_input = pim_input, 355 .pr_ctloutput = rip_ctloutput, 356 .pr_usrreqs = &rip_usrreqs 357 }, 358 #endif /* PIM */ 359 #ifdef DEV_PFSYNC 360 { 361 .pr_type = SOCK_RAW, 362 .pr_domain = &inetdomain, 363 .pr_protocol = IPPROTO_PFSYNC, 364 .pr_flags = PR_ATOMIC|PR_ADDR, 365 .pr_input = pfsync_input, 366 .pr_ctloutput = rip_ctloutput, 367 .pr_usrreqs = &rip_usrreqs 368 }, 369 #endif /* DEV_PFSYNC */ 370 #ifdef DEV_CARP 371 { 372 .pr_type = SOCK_RAW, 373 .pr_domain = &inetdomain, 374 .pr_protocol = IPPROTO_CARP, 375 .pr_flags = PR_ATOMIC|PR_ADDR, 376 .pr_input = carp_input, 377 .pr_output = (pr_output_t*)rip_output, 378 .pr_ctloutput = rip_ctloutput, 379 .pr_usrreqs = &rip_usrreqs 380 }, 381 #endif /* DEV_CARP */ 382 /* Spacer n-times for loadable protocols. */ 383 IPPROTOSPACER, 384 IPPROTOSPACER, 385 IPPROTOSPACER, 386 IPPROTOSPACER, 387 IPPROTOSPACER, 388 IPPROTOSPACER, 389 IPPROTOSPACER, 390 IPPROTOSPACER, 391 /* raw wildcard */ 392 { 393 .pr_type = SOCK_RAW, 394 .pr_domain = &inetdomain, 395 .pr_flags = PR_ATOMIC|PR_ADDR, 396 .pr_input = rip_input, 397 .pr_ctloutput = rip_ctloutput, 398 .pr_init = rip_init, 399 .pr_usrreqs = &rip_usrreqs 400 }, 401 }; 402 403 extern int in_inithead(void **, int); 404 405 struct domain inetdomain = { 406 .dom_family = AF_INET, 407 .dom_name = "internet", 408 .dom_protosw = inetsw, 409 .dom_protoswNPROTOSW = &inetsw[sizeof(inetsw)/sizeof(inetsw[0])], 410 .dom_rtattach = in_inithead, 411 .dom_rtoffset = 32, 412 .dom_maxrtkey = sizeof(struct sockaddr_in) 413 }; 414 415 DOMAIN_SET(inet); 416 417 SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW, 0, 418 "Internet Family"); 419 420 SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW, 0, "IP"); 421 SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW, 0, "ICMP"); 422 SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW, 0, "UDP"); 423 SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW, 0, "TCP"); 424 #ifdef SCTP 425 SYSCTL_NODE(_net_inet, IPPROTO_SCTP, sctp, CTLFLAG_RW, 0, "SCTP"); 426 #endif 427 SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW, 0, "IGMP"); 428 #ifdef FAST_IPSEC 429 /* XXX no protocol # to use, pick something "reserved" */ 430 SYSCTL_NODE(_net_inet, 253, ipsec, CTLFLAG_RW, 0, "IPSEC"); 431 SYSCTL_NODE(_net_inet, IPPROTO_AH, ah, CTLFLAG_RW, 0, "AH"); 432 SYSCTL_NODE(_net_inet, IPPROTO_ESP, esp, CTLFLAG_RW, 0, "ESP"); 433 SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP, ipcomp, CTLFLAG_RW, 0, "IPCOMP"); 434 SYSCTL_NODE(_net_inet, IPPROTO_IPIP, ipip, CTLFLAG_RW, 0, "IPIP"); 435 #else 436 #ifdef IPSEC 437 SYSCTL_NODE(_net_inet, IPPROTO_AH, ipsec, CTLFLAG_RW, 0, "IPSEC"); 438 #endif /* IPSEC */ 439 #endif /* !FAST_IPSEC */ 440 SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW, 0, "RAW"); 441 #ifdef PIM 442 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM"); 443 #endif 444 #ifdef DEV_PFSYNC 445 SYSCTL_NODE(_net_inet, IPPROTO_PFSYNC, pfsync, CTLFLAG_RW, 0, "PFSYNC"); 446 #endif 447 #ifdef DEV_CARP 448 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 449 #endif 450