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