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 * 3. 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: src/sys/netinet/in_proto.c,v 1.53.2.7 2003/08/24 08:24:38 hsu Exp $ 31 */ 32 33 #include "opt_ipdivert.h" 34 #include "opt_mrouting.h" 35 #include "opt_inet6.h" 36 #include "opt_carp.h" 37 38 #include <sys/param.h> 39 #include <sys/kernel.h> 40 #include <sys/socket.h> 41 #include <sys/domain.h> 42 #include <sys/protosw.h> 43 #include <sys/queue.h> 44 #include <sys/sysctl.h> 45 46 #include <net/if.h> 47 #include <net/route.h> 48 49 #include <netinet/in.h> 50 #include <netinet/in_systm.h> 51 #include <netinet/in_pcb.h> 52 #include <netinet/in_var.h> 53 #include <netinet/ip.h> 54 #include <netinet/ip_var.h> 55 #include <netinet/ip_icmp.h> 56 #include <netinet/igmp_var.h> 57 #ifdef PIM 58 #include <netinet/pim_var.h> 59 #endif 60 #include <netinet/tcp.h> 61 #include <netinet/tcp_timer.h> 62 #include <netinet/tcp_var.h> 63 #include <netinet/udp.h> 64 #include <netinet/udp_var.h> 65 #include <netinet/ip_encap.h> 66 #ifdef IPDIVERT 67 #include <netinet/ip_divert.h> 68 #endif 69 70 /* 71 * TCP/IP protocol family: IP, ICMP, UDP, TCP. 72 */ 73 74 #include <net/netisr.h> /* for cpu0_soport */ 75 76 #ifdef CARP 77 #include <netinet/ip_carp.h> 78 #endif 79 80 extern struct domain inetdomain; 81 static struct pr_usrreqs nousrreqs; 82 83 struct protosw inetsw[] = { 84 { 85 .pr_type = 0, 86 .pr_domain = &inetdomain, 87 .pr_protocol = 0, 88 .pr_flags = 0, 89 90 .pr_ctlport = NULL, 91 92 .pr_init = ip_init, 93 .pr_drain = ip_drain, 94 .pr_usrreqs = &nousrreqs 95 }, 96 { 97 .pr_type = SOCK_DGRAM, 98 .pr_domain = &inetdomain, 99 .pr_protocol = IPPROTO_UDP, 100 .pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE| 101 PR_ASYNC_SEND|PR_ASEND_HOLDTD|PR_ACONN_HOLDTD, 102 103 .pr_initport = udp_initport, 104 .pr_input = udp_input, 105 .pr_output = NULL, 106 .pr_ctlinput = udp_ctlinput, 107 .pr_ctloutput = udp_ctloutput, 108 109 .pr_ctlport = udp_ctlport, 110 .pr_init = udp_init, 111 .pr_usrreqs = &udp_usrreqs 112 }, 113 { 114 .pr_type = SOCK_STREAM, 115 .pr_domain = &inetdomain, 116 .pr_protocol = IPPROTO_TCP, 117 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_MPSAFE| 118 PR_ASYNC_SEND|PR_ASYNC_RCVD|PR_ACONN_HOLDTD, 119 120 .pr_initport = tcp_initport, 121 .pr_input = tcp_input, 122 .pr_output = NULL, 123 .pr_ctlinput = tcp_ctlinput, 124 .pr_ctloutmsg = tcp_ctloutmsg, 125 .pr_ctloutput = tcp_ctloutput, 126 127 .pr_ctlport = tcp_ctlport, 128 .pr_init = tcp_init, 129 .pr_drain = tcp_drain, 130 .pr_usrreqs = &tcp_usrreqs 131 }, 132 { 133 .pr_type = SOCK_RAW, 134 .pr_domain = &inetdomain, 135 .pr_protocol = IPPROTO_RAW, 136 .pr_flags = PR_ATOMIC|PR_ADDR, 137 138 .pr_input = rip_input, 139 .pr_output = NULL, 140 .pr_ctlinput = NULL, 141 .pr_ctloutput = rip_ctloutput, 142 143 .pr_ctlport = NULL, 144 .pr_usrreqs = &rip_usrreqs 145 }, 146 { 147 .pr_type = SOCK_RAW, 148 .pr_domain = &inetdomain, 149 .pr_protocol = IPPROTO_ICMP, 150 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE, 151 152 .pr_input = icmp_input, 153 .pr_output = NULL, 154 .pr_ctlinput = NULL, 155 .pr_ctloutput = rip_ctloutput, 156 157 .pr_usrreqs = &rip_usrreqs 158 }, 159 { 160 .pr_type = SOCK_RAW, 161 .pr_domain = &inetdomain, 162 .pr_protocol = IPPROTO_IGMP, 163 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR|PR_MPSAFE, 164 165 .pr_input = igmp_input, 166 .pr_output = NULL, 167 .pr_ctlinput = NULL, 168 .pr_ctloutput = rip_ctloutput, 169 170 .pr_init = igmp_init, 171 .pr_drain = NULL, 172 .pr_usrreqs = &rip_usrreqs 173 }, 174 { 175 .pr_type = SOCK_RAW, 176 .pr_domain = &inetdomain, 177 .pr_protocol = IPPROTO_RSVP, 178 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 179 180 .pr_input = rsvp_input, 181 .pr_output = NULL, 182 .pr_ctlinput = NULL, 183 .pr_ctloutput = rip_ctloutput, 184 185 .pr_usrreqs = &rip_usrreqs 186 }, 187 { 188 .pr_type = SOCK_RAW, 189 .pr_domain = &inetdomain, 190 .pr_protocol = IPPROTO_IPV4, 191 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 192 .pr_input = encap4_input, 193 .pr_output = NULL, 194 .pr_ctlinput = NULL, 195 .pr_ctloutput = rip_ctloutput, 196 197 .pr_ctlport = NULL, 198 .pr_init = encap_init, 199 .pr_usrreqs = &rip_usrreqs 200 }, 201 { 202 .pr_type = SOCK_RAW, 203 .pr_domain = &inetdomain, 204 .pr_protocol = IPPROTO_MOBILE, 205 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 206 207 .pr_input = encap4_input, 208 .pr_output = NULL, 209 .pr_ctlinput = NULL, 210 .pr_ctloutput = rip_ctloutput, 211 212 .pr_ctlport = NULL, 213 .pr_init = encap_init, 214 .pr_usrreqs = &rip_usrreqs 215 }, 216 { 217 .pr_type = SOCK_RAW, 218 .pr_domain = &inetdomain, 219 .pr_protocol = IPPROTO_GRE, 220 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 221 222 .pr_input = encap4_input, 223 .pr_output = NULL, 224 .pr_ctlinput = NULL, 225 .pr_ctloutput = rip_ctloutput, 226 227 .pr_ctlport = NULL, 228 .pr_init = encap_init, 229 .pr_usrreqs = &rip_usrreqs 230 }, 231 #ifdef INET6 232 { 233 .pr_type = SOCK_RAW, 234 .pr_domain = &inetdomain, 235 .pr_protocol = IPPROTO_IPV6, 236 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 237 238 .pr_input = encap4_input, 239 .pr_output = NULL, 240 .pr_ctlinput = NULL, 241 .pr_ctloutput = rip_ctloutput, 242 243 .pr_ctlport = NULL, 244 .pr_init = encap_init, 245 .pr_usrreqs = &rip_usrreqs 246 }, 247 #endif 248 #ifdef IPDIVERT 249 { 250 .pr_type = SOCK_RAW, 251 .pr_domain = &inetdomain, 252 .pr_protocol = IPPROTO_DIVERT, 253 .pr_flags = PR_ATOMIC|PR_ADDR, 254 255 .pr_input = div_input, 256 .pr_output = NULL, 257 .pr_ctlinput = NULL, 258 .pr_ctloutput = ip_ctloutput, 259 260 .pr_ctlport = NULL, 261 .pr_init = div_init, 262 .pr_usrreqs = &div_usrreqs 263 }, 264 #endif 265 #ifdef PIM 266 { 267 .pr_type = SOCK_RAW, 268 .pr_domain = &inetdomain, 269 .pr_protocol = IPPROTO_PIM, 270 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 271 272 .pr_input = pim_input, 273 .pr_output = NULL, 274 .pr_ctlinput = NULL, 275 .pr_ctloutput = rip_ctloutput, 276 277 .pr_ctlport = NULL, 278 .pr_usrreqs = &rip_usrreqs 279 }, 280 #endif 281 #ifdef NPFSYNC 282 { 283 .pr_type = SOCK_RAW, 284 .pr_domain = &inetdomain, 285 .pr_protocol = IPPROTO_PFSYNC, 286 .pr_flags = PR_ATOMIC|PR_ADDR, 287 288 .pr_input = pfsync_input, 289 .pr_output = NULL, 290 .pr_ctlinput = NULL, 291 .pr_ctloutput = rip_ctloutput, 292 293 .pr_ctlport = NULL, 294 .pr_usrreqs = &rip_usrreqs 295 }, 296 #endif /* NPFSYNC */ 297 { 298 /* raw wildcard */ 299 .pr_type = SOCK_RAW, 300 .pr_domain = &inetdomain, 301 .pr_protocol = 0, 302 .pr_flags = PR_ATOMIC|PR_ADDR, 303 304 .pr_input = rip_input, 305 .pr_output = NULL, 306 .pr_ctlinput = NULL, 307 .pr_ctloutput = rip_ctloutput, 308 309 .pr_init = rip_init, 310 .pr_ctlport = NULL, 311 .pr_usrreqs = &rip_usrreqs 312 }, 313 #ifdef CARP 314 { 315 .pr_type = SOCK_RAW, 316 .pr_domain = &inetdomain, 317 .pr_protocol = IPPROTO_CARP, 318 .pr_flags = PR_ATOMIC|PR_ADDR|PR_MPSAFE, 319 320 .pr_input = carp_proto_input, 321 .pr_output = rip_output, 322 .pr_ctlinput = carp_proto_ctlinput, 323 .pr_ctloutput = rip_ctloutput, 324 325 .pr_ctlport = cpu0_ctlport, 326 .pr_usrreqs = &rip_usrreqs 327 }, 328 #endif 329 }; 330 331 static void 332 inetdomain_init(void) 333 { 334 in_pcbglobalinit(); 335 } 336 337 struct domain inetdomain = { 338 .dom_family = AF_INET, 339 .dom_name = "internet", 340 .dom_init = inetdomain_init, 341 .dom_externalize = NULL, 342 .dom_dispose = NULL, 343 .dom_protosw = inetsw, 344 .dom_protoswNPROTOSW = &inetsw[NELEM(inetsw)], 345 .dom_next = SLIST_ENTRY_INITIALIZER, 346 .dom_rtattach = in_inithead, 347 .dom_rtoffset = 32, 348 .dom_maxrtkey = sizeof(struct sockaddr_in), 349 .dom_ifattach = NULL, 350 .dom_ifdetach = NULL, 351 .dom_if_up = NULL, 352 .dom_if_down = in_if_down 353 }; 354 355 DOMAIN_SET(inet); 356 357 SYSCTL_NODE(_net, PF_INET, inet, CTLFLAG_RW, 0, 358 "Internet Family"); 359 360 SYSCTL_NODE(_net_inet, IPPROTO_IP, ip, CTLFLAG_RW, 0, "IP"); 361 SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp, CTLFLAG_RW, 0, "ICMP"); 362 SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp, CTLFLAG_RW, 0, "UDP"); 363 SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp, CTLFLAG_RW, 0, "TCP"); 364 SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp, CTLFLAG_RW, 0, "IGMP"); 365 SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw, CTLFLAG_RW, 0, "RAW"); 366 #ifdef IPDIVERT 367 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "DIVERT"); 368 #endif 369 #ifdef PIM 370 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM"); 371 #endif 372 #ifdef CARP 373 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 374 #endif 375