1 /* $NetBSD: in_proto.c,v 1.110 2014/06/05 23:48:16 rmind Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)in_proto.c 8.2 (Berkeley) 2/9/95 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: in_proto.c,v 1.110 2014/06/05 23:48:16 rmind Exp $"); 65 66 #include "opt_mrouting.h" 67 #include "opt_inet.h" 68 #include "opt_ipsec.h" 69 #include "opt_pim.h" 70 #include "opt_gateway.h" 71 #include "opt_compat_netbsd.h" 72 73 #include <sys/param.h> 74 #include <sys/socket.h> 75 #include <sys/protosw.h> 76 #include <sys/domain.h> 77 #include <sys/mbuf.h> 78 79 #include <net/if.h> 80 #include <net/radix.h> 81 #include <net/route.h> 82 83 #include <netinet/in.h> 84 #include <netinet/in_systm.h> 85 #include <netinet/in_var.h> 86 #include <netinet/ip.h> 87 #include <netinet/ip_var.h> 88 #include <netinet/ip_icmp.h> 89 #include <netinet/in_ifattach.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/in_proto.h> 92 93 #ifdef INET6 94 #ifndef INET 95 #include <netinet/in.h> 96 #endif 97 #include <netinet/ip6.h> 98 #endif 99 100 #include <netinet/igmp_var.h> 101 #ifdef PIM 102 #include <netinet/pim_var.h> 103 #endif 104 #include <netinet/tcp.h> 105 #include <netinet/tcp_fsm.h> 106 #include <netinet/tcp_seq.h> 107 #include <netinet/tcp_timer.h> 108 #include <netinet/tcp_var.h> 109 #include <netinet/tcpip.h> 110 #include <netinet/tcp_debug.h> 111 #include <netinet/udp.h> 112 #include <netinet/udp_var.h> 113 #include <netinet/ip_encap.h> 114 115 /* 116 * TCP/IP protocol family: IP, ICMP, UDP, TCP. 117 */ 118 119 #ifdef IPSEC 120 #include <netipsec/ipsec.h> 121 #include <netipsec/key.h> 122 #endif /* IPSEC */ 123 124 #include "carp.h" 125 #if NCARP > 0 126 #include <netinet/ip_carp.h> 127 #endif 128 129 #include "pfsync.h" 130 #if NPFSYNC > 0 131 #include <net/pfvar.h> 132 #include <net/if_pfsync.h> 133 #endif 134 135 #include "etherip.h" 136 #if NETHERIP > 0 137 #include <netinet/ip_etherip.h> 138 #endif 139 140 DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */ 141 142 /* Wrappers to acquire kernel_lock. */ 143 144 PR_WRAP_CTLINPUT(rip_ctlinput) 145 PR_WRAP_CTLINPUT(udp_ctlinput) 146 PR_WRAP_CTLINPUT(tcp_ctlinput) 147 148 #define rip_ctlinput rip_ctlinput_wrapper 149 #define udp_ctlinput udp_ctlinput_wrapper 150 #define tcp_ctlinput tcp_ctlinput_wrapper 151 152 PR_WRAP_CTLOUTPUT(rip_ctloutput) 153 PR_WRAP_CTLOUTPUT(udp_ctloutput) 154 PR_WRAP_CTLOUTPUT(tcp_ctloutput) 155 156 #define rip_ctloutput rip_ctloutput_wrapper 157 #define udp_ctloutput udp_ctloutput_wrapper 158 #define tcp_ctloutput tcp_ctloutput_wrapper 159 160 #if defined(IPSEC) 161 PR_WRAP_CTLINPUT(ah4_ctlinput) 162 163 #define ah4_ctlinput ah4_ctlinput_wrapper 164 PR_WRAP_CTLINPUT(esp4_ctlinput) 165 166 #define esp4_ctlinput esp4_ctlinput_wrapper 167 #endif 168 169 const struct protosw inetsw[] = { 170 { .pr_domain = &inetdomain, 171 .pr_init = ip_init, 172 .pr_output = ip_output, 173 .pr_fasttimo = ip_fasttimo, 174 .pr_slowtimo = ip_slowtimo, 175 .pr_drain = ip_drainstub, 176 }, 177 { .pr_type = SOCK_DGRAM, 178 .pr_domain = &inetdomain, 179 .pr_protocol = IPPROTO_UDP, 180 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF, 181 .pr_input = udp_input, 182 .pr_ctlinput = udp_ctlinput, 183 .pr_ctloutput = udp_ctloutput, 184 .pr_usrreqs = &udp_usrreqs, 185 .pr_init = udp_init, 186 }, 187 { .pr_type = SOCK_STREAM, 188 .pr_domain = &inetdomain, 189 .pr_protocol = IPPROTO_TCP, 190 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF, 191 .pr_input = tcp_input, 192 .pr_ctlinput = tcp_ctlinput, 193 .pr_ctloutput = tcp_ctloutput, 194 .pr_usrreqs = &tcp_usrreqs, 195 .pr_init = tcp_init, 196 .pr_fasttimo = tcp_fasttimo, 197 .pr_drain = tcp_drainstub, 198 }, 199 { .pr_type = SOCK_RAW, 200 .pr_domain = &inetdomain, 201 .pr_protocol = IPPROTO_RAW, 202 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF, 203 .pr_input = rip_input, 204 .pr_output = rip_output, 205 .pr_ctlinput = rip_ctlinput, 206 .pr_ctloutput = rip_ctloutput, 207 .pr_usrreqs = &rip_usrreqs, 208 }, 209 { .pr_type = SOCK_RAW, 210 .pr_domain = &inetdomain, 211 .pr_protocol = IPPROTO_ICMP, 212 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 213 .pr_input = icmp_input, 214 .pr_output = rip_output, 215 .pr_ctlinput = rip_ctlinput, 216 .pr_ctloutput = rip_ctloutput, 217 .pr_usrreqs = &rip_usrreqs, 218 .pr_init = icmp_init, 219 }, 220 #ifdef GATEWAY 221 { .pr_domain = &inetdomain, 222 .pr_protocol = IPPROTO_IP, 223 .pr_slowtimo = ipflow_slowtimo, 224 .pr_init = ipflow_poolinit, 225 }, 226 #endif /* GATEWAY */ 227 #ifdef IPSEC 228 { .pr_type = SOCK_RAW, 229 .pr_domain = &inetdomain, 230 .pr_protocol = IPPROTO_AH, 231 .pr_flags = PR_ATOMIC|PR_ADDR, 232 .pr_input = ipsec4_common_input, 233 .pr_ctlinput = ah4_ctlinput, 234 }, 235 { .pr_type = SOCK_RAW, 236 .pr_domain = &inetdomain, 237 .pr_protocol = IPPROTO_ESP, 238 .pr_flags = PR_ATOMIC|PR_ADDR, 239 .pr_input = ipsec4_common_input, 240 .pr_ctlinput = esp4_ctlinput, 241 }, 242 { .pr_type = SOCK_RAW, 243 .pr_domain = &inetdomain, 244 .pr_protocol = IPPROTO_IPCOMP, 245 .pr_flags = PR_ATOMIC|PR_ADDR, 246 .pr_input = ipsec4_common_input, 247 }, 248 #endif /* IPSEC */ 249 { .pr_type = SOCK_RAW, 250 .pr_domain = &inetdomain, 251 .pr_protocol = IPPROTO_IPV4, 252 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 253 .pr_input = encap4_input, 254 .pr_output = rip_output, 255 .pr_ctlinput = rip_ctlinput, 256 .pr_ctloutput = rip_ctloutput, 257 .pr_usrreqs = &rip_usrreqs, 258 .pr_init = encap_init, 259 }, 260 #ifdef INET6 261 { .pr_type = SOCK_RAW, 262 .pr_domain = &inetdomain, 263 .pr_protocol = IPPROTO_IPV6, 264 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 265 .pr_input = encap4_input, 266 .pr_output = rip_output, 267 .pr_ctlinput = rip_ctlinput, 268 .pr_ctloutput = rip_ctloutput, 269 .pr_usrreqs = &rip_usrreqs, 270 .pr_init = encap_init, 271 }, 272 #endif /* INET6 */ 273 #if NETHERIP > 0 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 = ip_etherip_input, 279 .pr_output = rip_output, 280 .pr_ctlinput = rip_ctlinput, 281 .pr_ctloutput = rip_ctloutput, 282 .pr_usrreqs = &rip_usrreqs, 283 }, 284 #endif /* NETHERIP > 0 */ 285 #if NCARP > 0 286 { .pr_type = SOCK_RAW, 287 .pr_domain = &inetdomain, 288 .pr_protocol = IPPROTO_CARP, 289 .pr_flags = PR_ATOMIC|PR_ADDR, 290 .pr_input = carp_proto_input, 291 .pr_output = rip_output, 292 .pr_ctloutput = rip_ctloutput, 293 .pr_usrreqs = &rip_usrreqs, 294 .pr_init = carp_init, 295 }, 296 #endif /* NCARP > 0 */ 297 #if NPFSYNC > 0 298 { .pr_type = SOCK_RAW, 299 .pr_domain = &inetdomain, 300 .pr_protocol = IPPROTO_PFSYNC, 301 .pr_flags = PR_ATOMIC|PR_ADDR, 302 .pr_input = pfsync_input, 303 .pr_output = rip_output, 304 .pr_ctloutput = rip_ctloutput, 305 .pr_usrreqs = &rip_usrreqs, 306 }, 307 #endif /* NPFSYNC > 0 */ 308 { .pr_type = SOCK_RAW, 309 .pr_domain = &inetdomain, 310 .pr_protocol = IPPROTO_IGMP, 311 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 312 .pr_input = igmp_input, 313 .pr_output = rip_output, 314 .pr_ctloutput = rip_ctloutput, 315 .pr_ctlinput = rip_ctlinput, 316 .pr_usrreqs = &rip_usrreqs, 317 .pr_fasttimo = igmp_fasttimo, 318 .pr_slowtimo = igmp_slowtimo, 319 .pr_init = igmp_init, 320 }, 321 #ifdef PIM 322 { .pr_type = SOCK_RAW, 323 .pr_domain = &inetdomain, 324 .pr_protocol = IPPROTO_PIM, 325 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 326 .pr_input = pim_input, 327 .pr_output = rip_output, 328 .pr_ctloutput = rip_ctloutput, 329 .pr_ctlinput = rip_ctlinput, 330 .pr_usrreqs = &rip_usrreqs, 331 }, 332 #endif /* PIM */ 333 /* raw wildcard */ 334 { .pr_type = SOCK_RAW, 335 .pr_domain = &inetdomain, 336 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR, 337 .pr_input = rip_input, 338 .pr_output = rip_output, 339 .pr_ctloutput = rip_ctloutput, 340 .pr_ctlinput = rip_ctlinput, 341 .pr_usrreqs = &rip_usrreqs, 342 .pr_init = rip_init, 343 }, 344 }; 345 346 const struct sockaddr_in in_any = { 347 .sin_len = sizeof(struct sockaddr_in) 348 , .sin_family = AF_INET 349 , .sin_port = 0 350 , .sin_addr = {.s_addr = 0 /* INADDR_ANY */} 351 }; 352 353 struct domain inetdomain = { 354 .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL, 355 .dom_externalize = NULL, .dom_dispose = NULL, 356 .dom_protosw = inetsw, 357 .dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)], 358 .dom_rtattach = rt_inithead, 359 .dom_rtoffset = 32, 360 .dom_maxrtkey = sizeof(struct ip_pack4), 361 #ifdef IPSELSRC 362 .dom_ifattach = in_domifattach, 363 .dom_ifdetach = in_domifdetach, 364 #else 365 .dom_ifattach = NULL, 366 .dom_ifdetach = NULL, 367 #endif 368 .dom_ifqueues = { NULL, NULL }, 369 .dom_link = { NULL }, 370 .dom_mowner = MOWNER_INIT("",""), 371 .dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr), 372 .dom_sa_cmplen = sizeof(struct in_addr), 373 .dom_sa_any = (const struct sockaddr *)&in_any, 374 .dom_sockaddr_const_addr = sockaddr_in_const_addr, 375 .dom_sockaddr_addr = sockaddr_in_addr, 376 .dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache) 377 }; 378 379 u_char ip_protox[IPPROTO_MAX]; 380 381 int icmperrppslim = 100; /* 100pps */ 382 383 static void 384 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp) 385 { 386 socklen_t slen; 387 388 if (slenp == NULL) 389 return; 390 391 slen = sockaddr_getlen(sa); 392 *slenp = (socklen_t)MIN(sizeof(struct in_addr), 393 slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr))); 394 } 395 396 const void * 397 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp) 398 { 399 const struct sockaddr_in *sin; 400 401 sockaddr_in_addrlen(sa, slenp); 402 sin = (const struct sockaddr_in *)sa; 403 return &sin->sin_addr; 404 } 405 406 void * 407 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp) 408 { 409 struct sockaddr_in *sin; 410 411 sockaddr_in_addrlen(sa, slenp); 412 sin = (struct sockaddr_in *)sa; 413 return &sin->sin_addr; 414 } 415 416 int 417 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2) 418 { 419 uint_fast8_t len; 420 const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr), 421 addrend = addrofs + sizeof(struct in_addr); 422 int rc; 423 const struct sockaddr_in *sin1, *sin2; 424 425 sin1 = satocsin(sa1); 426 sin2 = satocsin(sa2); 427 428 len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len)); 429 430 if (len > addrofs && 431 (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr, 432 len - addrofs)) != 0) 433 return rc; 434 435 return sin1->sin_len - sin2->sin_len; 436 } 437