1 /* $NetBSD: in6_pcb.c,v 1.166 2019/05/15 02:59:18 ozaki-r Exp $ */ 2 /* $KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.166 2019/05/15 02:59:18 ozaki-r Exp $"); 66 67 #ifdef _KERNEL_OPT 68 #include "opt_inet.h" 69 #include "opt_ipsec.h" 70 #endif 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/mbuf.h> 75 #include <sys/protosw.h> 76 #include <sys/socket.h> 77 #include <sys/socketvar.h> 78 #include <sys/ioctl.h> 79 #include <sys/errno.h> 80 #include <sys/time.h> 81 #include <sys/proc.h> 82 #include <sys/kauth.h> 83 #include <sys/domain.h> 84 #include <sys/once.h> 85 86 #include <net/if.h> 87 #include <net/route.h> 88 89 #include <netinet/in.h> 90 #include <netinet/in_var.h> 91 #include <netinet/in_systm.h> 92 #include <netinet/ip.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/ip6.h> 95 #include <netinet/portalgo.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet6/in6_pcb.h> 98 #include <netinet6/scope6_var.h> 99 100 #include "faith.h" 101 102 #ifdef IPSEC 103 #include <netipsec/ipsec.h> 104 #include <netipsec/ipsec6.h> 105 #include <netipsec/key.h> 106 #endif /* IPSEC */ 107 108 #include <netinet/tcp_vtw.h> 109 110 const struct in6_addr zeroin6_addr; 111 112 #define IN6PCBHASH_PORT(table, lport) \ 113 &(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash] 114 #define IN6PCBHASH_BIND(table, laddr, lport) \ 115 &(table)->inpt_bindhashtbl[ \ 116 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \ 117 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \ 118 (table)->inpt_bindhash] 119 #define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \ 120 &(table)->inpt_bindhashtbl[ \ 121 ((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \ 122 (faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \ 123 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \ 124 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \ 125 ntohs(lport))) & (table)->inpt_bindhash] 126 127 int ip6_anonportmin = IPV6PORT_ANONMIN; 128 int ip6_anonportmax = IPV6PORT_ANONMAX; 129 int ip6_lowportmin = IPV6PORT_RESERVEDMIN; 130 int ip6_lowportmax = IPV6PORT_RESERVEDMAX; 131 132 static struct pool in6pcb_pool; 133 134 static int 135 in6pcb_poolinit(void) 136 { 137 138 pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl", 139 NULL, IPL_SOFTNET); 140 return 0; 141 } 142 143 void 144 in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize) 145 { 146 static ONCE_DECL(control); 147 148 in_pcbinit(table, bindhashsize, connecthashsize); 149 table->inpt_lastport = (u_int16_t)ip6_anonportmax; 150 151 RUN_ONCE(&control, in6pcb_poolinit); 152 } 153 154 int 155 in6_pcballoc(struct socket *so, void *v) 156 { 157 struct inpcbtable *table = v; 158 struct in6pcb *in6p; 159 int s; 160 161 KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6); 162 163 in6p = pool_get(&in6pcb_pool, PR_NOWAIT); 164 if (in6p == NULL) 165 return (ENOBUFS); 166 memset((void *)in6p, 0, sizeof(*in6p)); 167 in6p->in6p_af = AF_INET6; 168 in6p->in6p_table = table; 169 in6p->in6p_socket = so; 170 in6p->in6p_hops = -1; /* use kernel default */ 171 in6p->in6p_icmp6filt = NULL; 172 in6p->in6p_portalgo = PORTALGO_DEFAULT; 173 in6p->in6p_bindportonsend = false; 174 #if defined(IPSEC) 175 if (ipsec_enabled) { 176 int error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp); 177 if (error != 0) { 178 pool_put(&in6pcb_pool, in6p); 179 return error; 180 } 181 in6p->in6p_sp->sp_inph = (struct inpcb_hdr *)in6p; 182 } 183 #endif /* IPSEC */ 184 s = splsoftnet(); 185 TAILQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p, 186 inph_queue); 187 LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport), 188 &in6p->in6p_head, inph_lhash); 189 in6_pcbstate(in6p, IN6P_ATTACHED); 190 splx(s); 191 if (ip6_v6only) 192 in6p->in6p_flags |= IN6P_IPV6_V6ONLY; 193 so->so_pcb = (void *)in6p; 194 return (0); 195 } 196 197 /* 198 * Bind address from sin6 to in6p. 199 */ 200 static int 201 in6_pcbbind_addr(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l) 202 { 203 int error; 204 int s; 205 206 /* 207 * We should check the family, but old programs 208 * incorrectly fail to initialize it. 209 */ 210 if (sin6->sin6_family != AF_INET6) 211 return (EAFNOSUPPORT); 212 213 #ifndef INET 214 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 215 return (EADDRNOTAVAIL); 216 #endif 217 218 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) 219 return (error); 220 221 s = pserialize_read_enter(); 222 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 223 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) { 224 error = EINVAL; 225 goto out; 226 } 227 if (sin6->sin6_addr.s6_addr32[3]) { 228 struct sockaddr_in sin; 229 230 memset(&sin, 0, sizeof(sin)); 231 sin.sin_len = sizeof(sin); 232 sin.sin_family = AF_INET; 233 bcopy(&sin6->sin6_addr.s6_addr32[3], 234 &sin.sin_addr, sizeof(sin.sin_addr)); 235 if (!IN_MULTICAST(sin.sin_addr.s_addr)) { 236 struct ifaddr *ifa; 237 ifa = ifa_ifwithaddr((struct sockaddr *)&sin); 238 if (ifa == NULL) { 239 error = EADDRNOTAVAIL; 240 goto out; 241 } 242 } 243 } 244 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 245 // succeed 246 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 247 struct ifaddr *ifa = NULL; 248 249 if ((in6p->in6p_flags & IN6P_FAITH) == 0) { 250 ifa = ifa_ifwithaddr(sin6tosa(sin6)); 251 if (ifa == NULL) { 252 error = EADDRNOTAVAIL; 253 goto out; 254 } 255 } 256 257 /* 258 * bind to an anycast address might accidentally 259 * cause sending a packet with an anycast source 260 * address, so we forbid it. 261 * 262 * We should allow to bind to a deprecated address, 263 * since the application dare to use it. 264 * But, can we assume that they are careful enough 265 * to check if the address is deprecated or not? 266 * Maybe, as a safeguard, we should have a setsockopt 267 * flag to control the bind(2) behavior against 268 * deprecated addresses (default: forbid bind(2)). 269 */ 270 if (ifa && 271 ifatoia6(ifa)->ia6_flags & 272 (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) { 273 error = EADDRNOTAVAIL; 274 goto out; 275 } 276 } 277 in6p->in6p_laddr = sin6->sin6_addr; 278 error = 0; 279 out: 280 pserialize_read_exit(s); 281 return error; 282 } 283 284 /* 285 * Bind port from sin6 to in6p. 286 */ 287 static int 288 in6_pcbbind_port(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l) 289 { 290 struct inpcbtable *table = in6p->in6p_table; 291 struct socket *so = in6p->in6p_socket; 292 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 293 int error; 294 295 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 296 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 297 (so->so_options & SO_ACCEPTCONN) == 0)) 298 wild = 1; 299 300 if (sin6->sin6_port != 0) { 301 enum kauth_network_req req; 302 303 #ifndef IPNOPRIVPORTS 304 if (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED) 305 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT; 306 else 307 #endif /* IPNOPRIVPORTS */ 308 req = KAUTH_REQ_NETWORK_BIND_PORT; 309 310 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND, 311 req, so, sin6, NULL); 312 if (error) 313 return (EACCES); 314 } 315 316 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 317 /* 318 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 319 * allow compepte duplication of binding if 320 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 321 * and a multicast address is bound on both 322 * new and duplicated sockets. 323 */ 324 if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT)) 325 reuseport = SO_REUSEADDR|SO_REUSEPORT; 326 } 327 328 if (sin6->sin6_port != 0) { 329 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 330 #ifdef INET 331 struct inpcb *t; 332 struct vestigial_inpcb vestige; 333 334 t = in_pcblookup_port(table, 335 *(struct in_addr *)&sin6->sin6_addr.s6_addr32[3], 336 sin6->sin6_port, wild, &vestige); 337 if (t && (reuseport & t->inp_socket->so_options) == 0) 338 return (EADDRINUSE); 339 if (!t 340 && vestige.valid 341 && !(reuseport && vestige.reuse_port)) 342 return EADDRINUSE; 343 #else 344 return (EADDRNOTAVAIL); 345 #endif 346 } 347 348 { 349 struct in6pcb *t; 350 struct vestigial_inpcb vestige; 351 352 t = in6_pcblookup_port(table, &sin6->sin6_addr, 353 sin6->sin6_port, wild, &vestige); 354 if (t && (reuseport & t->in6p_socket->so_options) == 0) 355 return (EADDRINUSE); 356 if (!t 357 && vestige.valid 358 && !(reuseport && vestige.reuse_port)) 359 return EADDRINUSE; 360 } 361 } 362 363 if (sin6->sin6_port == 0) { 364 int e; 365 e = in6_pcbsetport(sin6, in6p, l); 366 if (e != 0) 367 return (e); 368 } else { 369 in6p->in6p_lport = sin6->sin6_port; 370 in6_pcbstate(in6p, IN6P_BOUND); 371 } 372 373 LIST_REMOVE(&in6p->in6p_head, inph_lhash); 374 LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport), 375 &in6p->in6p_head, inph_lhash); 376 377 return (0); 378 } 379 380 int 381 in6_pcbbind(void *v, struct sockaddr_in6 *sin6, struct lwp *l) 382 { 383 struct in6pcb *in6p = v; 384 struct sockaddr_in6 lsin6; 385 int error; 386 387 if (in6p->in6p_af != AF_INET6) 388 return (EINVAL); 389 390 /* 391 * If we already have a local port or a local address it means we're 392 * bounded. 393 */ 394 if (in6p->in6p_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) || 395 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && 396 in6p->in6p_laddr.s6_addr32[3] == 0))) 397 return (EINVAL); 398 399 if (NULL != sin6) { 400 /* We were provided a sockaddr_in6 to use. */ 401 if (sin6->sin6_len != sizeof(*sin6)) 402 return (EINVAL); 403 } else { 404 /* We always bind to *something*, even if it's "anything". */ 405 lsin6 = *((const struct sockaddr_in6 *) 406 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any); 407 sin6 = &lsin6; 408 } 409 410 /* Bind address. */ 411 error = in6_pcbbind_addr(in6p, sin6, l); 412 if (error) 413 return (error); 414 415 /* Bind port. */ 416 error = in6_pcbbind_port(in6p, sin6, l); 417 if (error) { 418 /* 419 * Reset the address here to "any" so we don't "leak" the 420 * in6pcb. 421 */ 422 in6p->in6p_laddr = in6addr_any; 423 424 return (error); 425 } 426 427 428 #if 0 429 in6p->in6p_flowinfo = 0; /* XXX */ 430 #endif 431 return (0); 432 } 433 434 /* 435 * Connect from a socket to a specified address. 436 * Both address and port must be specified in argument sin6. 437 * If don't have a local address for this socket yet, 438 * then pick one. 439 */ 440 int 441 in6_pcbconnect(void *v, struct sockaddr_in6 *sin6, struct lwp *l) 442 { 443 struct in6pcb *in6p = v; 444 struct in6_addr *in6a = NULL; 445 struct in6_addr ia6; 446 struct ifnet *ifp = NULL; /* outgoing interface */ 447 int error = 0; 448 int scope_ambiguous = 0; 449 #ifdef INET 450 struct in6_addr mapped; 451 #endif 452 struct sockaddr_in6 tmp; 453 struct vestigial_inpcb vestige; 454 struct psref psref; 455 int bound; 456 457 (void)&in6a; /* XXX fool gcc */ 458 459 if (in6p->in6p_af != AF_INET6) 460 return (EINVAL); 461 462 if (sin6->sin6_len != sizeof(*sin6)) 463 return (EINVAL); 464 if (sin6->sin6_family != AF_INET6) 465 return (EAFNOSUPPORT); 466 if (sin6->sin6_port == 0) 467 return (EADDRNOTAVAIL); 468 469 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 470 in6p->in6p_socket->so_type == SOCK_STREAM) 471 return EADDRNOTAVAIL; 472 473 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone) 474 scope_ambiguous = 1; 475 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) 476 return(error); 477 478 /* sanity check for mapped address case */ 479 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 480 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 481 return EINVAL; 482 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 483 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff); 484 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 485 return EINVAL; 486 } else 487 { 488 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 489 return EINVAL; 490 } 491 492 /* protect *sin6 from overwrites */ 493 tmp = *sin6; 494 sin6 = &tmp; 495 496 bound = curlwp_bind(); 497 /* Source address selection. */ 498 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && 499 in6p->in6p_laddr.s6_addr32[3] == 0) { 500 #ifdef INET 501 struct sockaddr_in sin; 502 struct in_ifaddr *ia4; 503 struct psref _psref; 504 505 memset(&sin, 0, sizeof(sin)); 506 sin.sin_len = sizeof(sin); 507 sin.sin_family = AF_INET; 508 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3], 509 sizeof(sin.sin_addr)); 510 ia4 = in_selectsrc(&sin, &in6p->in6p_route, 511 in6p->in6p_socket->so_options, NULL, &error, &_psref); 512 if (ia4 == NULL) { 513 if (error == 0) 514 error = EADDRNOTAVAIL; 515 curlwp_bindx(bound); 516 return (error); 517 } 518 memset(&mapped, 0, sizeof(mapped)); 519 mapped.s6_addr16[5] = htons(0xffff); 520 memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr, 521 sizeof(IA_SIN(ia4)->sin_addr)); 522 ia4_release(ia4, &_psref); 523 in6a = &mapped; 524 #else 525 curlwp_bindx(bound); 526 return EADDRNOTAVAIL; 527 #endif 528 } else { 529 /* 530 * XXX: in6_selectsrc might replace the bound local address 531 * with the address specified by setsockopt(IPV6_PKTINFO). 532 * Is it the intended behavior? 533 */ 534 error = in6_selectsrc(sin6, in6p->in6p_outputopts, 535 in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr, 536 &ifp, &psref, &ia6); 537 if (error == 0) 538 in6a = &ia6; 539 if (ifp && scope_ambiguous && 540 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { 541 if_put(ifp, &psref); 542 curlwp_bindx(bound); 543 return error; 544 } 545 546 if (in6a == NULL) { 547 if_put(ifp, &psref); 548 curlwp_bindx(bound); 549 if (error == 0) 550 error = EADDRNOTAVAIL; 551 return error; 552 } 553 } 554 555 if (ifp != NULL) { 556 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp); 557 if_put(ifp, &psref); 558 } else 559 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p); 560 curlwp_bindx(bound); 561 562 if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr, 563 sin6->sin6_port, 564 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr, 565 in6p->in6p_lport, 0, &vestige) 566 || vestige.valid) 567 return (EADDRINUSE); 568 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) || 569 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && 570 in6p->in6p_laddr.s6_addr32[3] == 0)) 571 { 572 if (in6p->in6p_lport == 0) { 573 error = in6_pcbbind(in6p, NULL, l); 574 if (error != 0) 575 return error; 576 } 577 in6p->in6p_laddr = *in6a; 578 } 579 in6p->in6p_faddr = sin6->sin6_addr; 580 in6p->in6p_fport = sin6->sin6_port; 581 582 /* Late bind, if needed */ 583 if (in6p->in6p_bindportonsend) { 584 struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *) 585 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any); 586 lsin.sin6_addr = in6p->in6p_laddr; 587 lsin.sin6_port = 0; 588 589 if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0) 590 return error; 591 } 592 593 in6_pcbstate(in6p, IN6P_CONNECTED); 594 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 595 if (ip6_auto_flowlabel) 596 in6p->in6p_flowinfo |= 597 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 598 #if defined(IPSEC) 599 if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM) 600 ipsec_pcbconn(in6p->in6p_sp); 601 #endif 602 return (0); 603 } 604 605 void 606 in6_pcbdisconnect(struct in6pcb *in6p) 607 { 608 memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr)); 609 in6p->in6p_fport = 0; 610 in6_pcbstate(in6p, IN6P_BOUND); 611 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 612 #if defined(IPSEC) 613 if (ipsec_enabled) 614 ipsec_pcbdisconn(in6p->in6p_sp); 615 #endif 616 if (in6p->in6p_socket->so_state & SS_NOFDREF) 617 in6_pcbdetach(in6p); 618 } 619 620 void 621 in6_pcbdetach(struct in6pcb *in6p) 622 { 623 struct socket *so = in6p->in6p_socket; 624 int s; 625 626 if (in6p->in6p_af != AF_INET6) 627 return; 628 629 #if defined(IPSEC) 630 if (ipsec_enabled) 631 ipsec_delete_pcbpolicy(in6p); 632 #endif 633 so->so_pcb = NULL; 634 635 s = splsoftnet(); 636 in6_pcbstate(in6p, IN6P_ATTACHED); 637 LIST_REMOVE(&in6p->in6p_head, inph_lhash); 638 TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head, 639 inph_queue); 640 splx(s); 641 642 if (in6p->in6p_options) { 643 m_freem(in6p->in6p_options); 644 } 645 if (in6p->in6p_outputopts != NULL) { 646 ip6_clearpktopts(in6p->in6p_outputopts, -1); 647 free(in6p->in6p_outputopts, M_IP6OPT); 648 } 649 rtcache_free(&in6p->in6p_route); 650 ip6_freemoptions(in6p->in6p_moptions); 651 ip_freemoptions(in6p->in6p_v4moptions); 652 sofree(so); /* drops the socket's lock */ 653 654 pool_put(&in6pcb_pool, in6p); 655 mutex_enter(softnet_lock); /* reacquire it */ 656 } 657 658 void 659 in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6) 660 { 661 662 if (in6p->in6p_af != AF_INET6) 663 return; 664 665 sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0); 666 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 667 } 668 669 void 670 in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6) 671 { 672 673 if (in6p->in6p_af != AF_INET6) 674 return; 675 676 sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0); 677 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 678 } 679 680 /* 681 * Pass some notification to all connections of a protocol 682 * associated with address dst. The local address and/or port numbers 683 * may be specified to limit the search. The "usual action" will be 684 * taken, depending on the ctlinput cmd. The caller must filter any 685 * cmds that are uninteresting (e.g., no error in the map). 686 * Call the protocol specific routine (if any) to report 687 * any errors for each matching socket. 688 * 689 * Must be called at splsoftnet. 690 * 691 * Note: src (4th arg) carries the flowlabel value on the original IPv6 692 * header, in sin6_flowinfo member. 693 */ 694 int 695 in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst, 696 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd, 697 void *cmdarg, void (*notify)(struct in6pcb *, int)) 698 { 699 struct inpcb_hdr *inph, *ninph; 700 struct sockaddr_in6 sa6_src; 701 const struct sockaddr_in6 *sa6_dst; 702 u_int16_t fport = fport_arg, lport = lport_arg; 703 int errno; 704 int nmatch = 0; 705 u_int32_t flowinfo; 706 707 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 708 return 0; 709 710 sa6_dst = (const struct sockaddr_in6 *)dst; 711 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 712 return 0; 713 714 /* 715 * note that src can be NULL when we get notify by local fragmentation. 716 */ 717 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 718 flowinfo = sa6_src.sin6_flowinfo; 719 720 /* 721 * Redirects go to all references to the destination, 722 * and use in6_rtchange to invalidate the route cache. 723 * Dead host indications: also use in6_rtchange to invalidate 724 * the cache, and deliver the error to all the sockets. 725 * Otherwise, if we have knowledge of the local port and address, 726 * deliver only to that socket. 727 */ 728 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 729 fport = 0; 730 lport = 0; 731 memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr)); 732 733 if (cmd != PRC_HOSTDEAD) 734 notify = in6_rtchange; 735 } 736 737 errno = inet6ctlerrmap[cmd]; 738 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { 739 struct in6pcb *in6p = (struct in6pcb *)inph; 740 struct rtentry *rt = NULL; 741 742 if (in6p->in6p_af != AF_INET6) 743 continue; 744 745 /* 746 * Under the following condition, notify of redirects 747 * to the pcb, without making address matches against inpcb. 748 * - redirect notification is arrived. 749 * - the inpcb is unconnected. 750 * - the inpcb is caching !RTF_HOST routing entry. 751 * - the ICMPv6 notification is from the gateway cached in the 752 * inpcb. i.e. ICMPv6 notification is from nexthop gateway 753 * the inpcb used very recently. 754 * 755 * This is to improve interaction between netbsd/openbsd 756 * redirect handling code, and inpcb route cache code. 757 * without the clause, !RTF_HOST routing entry (which carries 758 * gateway used by inpcb right before the ICMPv6 redirect) 759 * will be cached forever in unconnected inpcb. 760 * 761 * There still is a question regarding to what is TRT: 762 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be 763 * generated on packet output. inpcb will always cache 764 * RTF_HOST routing entry so there's no need for the clause 765 * (ICMPv6 redirect will update RTF_HOST routing entry, 766 * and inpcb is caching it already). 767 * However, bsdi/freebsd are vulnerable to local DoS attacks 768 * due to the cloned routing entries. 769 * - Specwise, "destination cache" is mentioned in RFC2461. 770 * Jinmei says that it implies bsdi/freebsd behavior, itojun 771 * is not really convinced. 772 * - Having hiwat/lowat on # of cloned host route (redirect/ 773 * pmtud) may be a good idea. netbsd/openbsd has it. see 774 * icmp6_mtudisc_update(). 775 */ 776 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) && 777 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 778 (rt = rtcache_validate(&in6p->in6p_route)) != NULL && 779 !(rt->rt_flags & RTF_HOST)) { 780 const struct sockaddr_in6 *dst6; 781 782 dst6 = (const struct sockaddr_in6 *) 783 rtcache_getdst(&in6p->in6p_route); 784 if (dst6 == NULL) 785 ; 786 else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, 787 &sa6_dst->sin6_addr)) { 788 rtcache_unref(rt, &in6p->in6p_route); 789 goto do_notify; 790 } 791 } 792 rtcache_unref(rt, &in6p->in6p_route); 793 794 /* 795 * If the error designates a new path MTU for a destination 796 * and the application (associated with this socket) wanted to 797 * know the value, notify. Note that we notify for all 798 * disconnected sockets if the corresponding application 799 * wanted. This is because some UDP applications keep sending 800 * sockets disconnected. 801 * XXX: should we avoid to notify the value to TCP sockets? 802 */ 803 if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 && 804 (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) || 805 IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) { 806 ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst, 807 (u_int32_t *)cmdarg); 808 } 809 810 /* 811 * Detect if we should notify the error. If no source and 812 * destination ports are specified, but non-zero flowinfo and 813 * local address match, notify the error. This is the case 814 * when the error is delivered with an encrypted buffer 815 * by ESP. Otherwise, just compare addresses and ports 816 * as usual. 817 */ 818 if (lport == 0 && fport == 0 && flowinfo && 819 in6p->in6p_socket != NULL && 820 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 821 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr)) 822 goto do_notify; 823 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 824 &sa6_dst->sin6_addr) || 825 in6p->in6p_socket == NULL || 826 (lport && in6p->in6p_lport != lport) || 827 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 828 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 829 &sa6_src.sin6_addr)) || 830 (fport && in6p->in6p_fport != fport)) 831 continue; 832 833 do_notify: 834 if (notify) 835 (*notify)(in6p, errno); 836 nmatch++; 837 } 838 return nmatch; 839 } 840 841 void 842 in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp) 843 { 844 struct inpcb_hdr *inph, *ninph; 845 struct ip6_moptions *im6o; 846 struct in6_multi_mship *imm, *nimm; 847 848 KASSERT(ifp != NULL); 849 850 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { 851 struct in6pcb *in6p = (struct in6pcb *)inph; 852 bool need_unlock = false; 853 if (in6p->in6p_af != AF_INET6) 854 continue; 855 856 /* The caller holds either one of in6ps' lock */ 857 if (!in6p_locked(in6p)) { 858 in6p_lock(in6p); 859 need_unlock = true; 860 } 861 im6o = in6p->in6p_moptions; 862 if (im6o) { 863 /* 864 * Unselect the outgoing interface if it is being 865 * detached. 866 */ 867 if (im6o->im6o_multicast_if_index == ifp->if_index) 868 im6o->im6o_multicast_if_index = 0; 869 870 /* 871 * Drop multicast group membership if we joined 872 * through the interface being detached. 873 * XXX controversial - is it really legal for kernel 874 * to force this? 875 */ 876 LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships, 877 i6mm_chain, nimm) { 878 if (imm->i6mm_maddr->in6m_ifp == ifp) { 879 LIST_REMOVE(imm, i6mm_chain); 880 in6_leavegroup(imm); 881 } 882 } 883 } 884 885 /* IFNET_LOCK must be taken after solock */ 886 in_purgeifmcast(in6p->in6p_v4moptions, ifp); 887 888 if (need_unlock) 889 in6p_unlock(in6p); 890 } 891 } 892 893 void 894 in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp) 895 { 896 struct rtentry *rt; 897 struct inpcb_hdr *inph, *ninph; 898 899 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { 900 struct in6pcb *in6p = (struct in6pcb *)inph; 901 if (in6p->in6p_af != AF_INET6) 902 continue; 903 if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL && 904 rt->rt_ifp == ifp) { 905 rtcache_unref(rt, &in6p->in6p_route); 906 in6_rtchange(in6p, 0); 907 } else 908 rtcache_unref(rt, &in6p->in6p_route); 909 } 910 } 911 912 /* 913 * Check for alternatives when higher level complains 914 * about service problems. For now, invalidate cached 915 * routing information. If the route was created dynamically 916 * (by a redirect), time to try a default gateway again. 917 */ 918 void 919 in6_losing(struct in6pcb *in6p) 920 { 921 struct rtentry *rt; 922 struct rt_addrinfo info; 923 924 if (in6p->in6p_af != AF_INET6) 925 return; 926 927 if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL) 928 return; 929 930 memset(&info, 0, sizeof(info)); 931 info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route); 932 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 933 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 934 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 935 if (rt->rt_flags & RTF_DYNAMIC) { 936 int error; 937 struct rtentry *nrt; 938 939 error = rtrequest(RTM_DELETE, rt_getkey(rt), 940 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt); 941 rtcache_unref(rt, &in6p->in6p_route); 942 if (error == 0) 943 rt_free(nrt); 944 } else 945 rtcache_unref(rt, &in6p->in6p_route); 946 /* 947 * A new route can be allocated 948 * the next time output is attempted. 949 */ 950 rtcache_free(&in6p->in6p_route); 951 } 952 953 /* 954 * After a routing change, flush old routing. A new route can be 955 * allocated the next time output is attempted. 956 */ 957 void 958 in6_rtchange(struct in6pcb *in6p, int errno) 959 { 960 if (in6p->in6p_af != AF_INET6) 961 return; 962 963 rtcache_free(&in6p->in6p_route); 964 /* 965 * A new route can be allocated the next time 966 * output is attempted. 967 */ 968 } 969 970 struct in6pcb * 971 in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6, 972 u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp) 973 { 974 struct inpcbhead *head; 975 struct inpcb_hdr *inph; 976 struct in6pcb *in6p, *match = NULL; 977 int matchwild = 3, wildcard; 978 u_int16_t lport = lport_arg; 979 980 if (vp) 981 vp->valid = 0; 982 983 head = IN6PCBHASH_PORT(table, lport); 984 LIST_FOREACH(inph, head, inph_lhash) { 985 in6p = (struct in6pcb *)inph; 986 if (in6p->in6p_af != AF_INET6) 987 continue; 988 989 if (in6p->in6p_lport != lport) 990 continue; 991 wildcard = 0; 992 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { 993 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 994 continue; 995 } 996 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 997 wildcard++; 998 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) { 999 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1000 continue; 1001 if (!IN6_IS_ADDR_V4MAPPED(laddr6)) 1002 continue; 1003 1004 /* duplicate of IPv4 logic */ 1005 wildcard = 0; 1006 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) && 1007 in6p->in6p_faddr.s6_addr32[3]) 1008 wildcard++; 1009 if (!in6p->in6p_laddr.s6_addr32[3]) { 1010 if (laddr6->s6_addr32[3]) 1011 wildcard++; 1012 } else { 1013 if (!laddr6->s6_addr32[3]) 1014 wildcard++; 1015 else { 1016 if (in6p->in6p_laddr.s6_addr32[3] != 1017 laddr6->s6_addr32[3]) 1018 continue; 1019 } 1020 } 1021 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1022 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 1023 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1024 continue; 1025 } 1026 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 1027 wildcard++; 1028 } else { 1029 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 1030 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1031 continue; 1032 } 1033 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 1034 wildcard++; 1035 else { 1036 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 1037 laddr6)) 1038 continue; 1039 } 1040 } 1041 if (wildcard && !lookup_wildcard) 1042 continue; 1043 if (wildcard < matchwild) { 1044 match = in6p; 1045 matchwild = wildcard; 1046 if (matchwild == 0) 1047 break; 1048 } 1049 } 1050 if (match && matchwild == 0) 1051 return match; 1052 1053 if (vp && table->vestige && table->vestige->init_ports6) { 1054 struct vestigial_inpcb better; 1055 void *state; 1056 1057 state = (*table->vestige->init_ports6)(laddr6, 1058 lport_arg, 1059 lookup_wildcard); 1060 while (table->vestige 1061 && (*table->vestige->next_port6)(state, vp)) { 1062 1063 if (vp->lport != lport) 1064 continue; 1065 wildcard = 0; 1066 if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6)) 1067 wildcard++; 1068 if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) { 1069 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 1070 wildcard++; 1071 } else { 1072 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 1073 if (vp->v6only) 1074 continue; 1075 } 1076 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 1077 wildcard++; 1078 else { 1079 if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6)) 1080 continue; 1081 } 1082 } 1083 if (wildcard && !lookup_wildcard) 1084 continue; 1085 if (wildcard < matchwild) { 1086 better = *vp; 1087 match = (void*)&better; 1088 1089 matchwild = wildcard; 1090 if (matchwild == 0) 1091 break; 1092 } 1093 } 1094 1095 if (match) { 1096 if (match != (void*)&better) 1097 return match; 1098 else { 1099 *vp = better; 1100 return 0; 1101 } 1102 } 1103 } 1104 return (match); 1105 } 1106 1107 /* 1108 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to 1109 * IPv4 mapped address. 1110 */ 1111 struct rtentry * 1112 in6_pcbrtentry(struct in6pcb *in6p) 1113 { 1114 struct rtentry *rt; 1115 struct route *ro; 1116 union { 1117 const struct sockaddr *sa; 1118 const struct sockaddr_in6 *sa6; 1119 #ifdef INET 1120 const struct sockaddr_in *sa4; 1121 #endif 1122 } cdst; 1123 1124 ro = &in6p->in6p_route; 1125 1126 if (in6p->in6p_af != AF_INET6) 1127 return (NULL); 1128 1129 cdst.sa = rtcache_getdst(ro); 1130 if (cdst.sa == NULL) 1131 ; 1132 #ifdef INET 1133 else if (cdst.sa->sa_family == AF_INET) { 1134 KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)); 1135 if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3]) 1136 rtcache_free(ro); 1137 } 1138 #endif 1139 else { 1140 if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr, 1141 &in6p->in6p_faddr)) 1142 rtcache_free(ro); 1143 } 1144 if ((rt = rtcache_validate(ro)) == NULL) 1145 rt = rtcache_update(ro, 1); 1146 #ifdef INET 1147 if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { 1148 union { 1149 struct sockaddr dst; 1150 struct sockaddr_in dst4; 1151 } u; 1152 struct in_addr addr; 1153 1154 addr.s_addr = in6p->in6p_faddr.s6_addr32[3]; 1155 1156 sockaddr_in_init(&u.dst4, &addr, 0); 1157 if (rtcache_setdst(ro, &u.dst) != 0) 1158 return NULL; 1159 1160 rt = rtcache_init(ro); 1161 } else 1162 #endif 1163 if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 1164 union { 1165 struct sockaddr dst; 1166 struct sockaddr_in6 dst6; 1167 } u; 1168 1169 sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0); 1170 if (rtcache_setdst(ro, &u.dst) != 0) 1171 return NULL; 1172 1173 rt = rtcache_init(ro); 1174 } 1175 return rt; 1176 } 1177 1178 void 1179 in6_pcbrtentry_unref(struct rtentry *rt, struct in6pcb *in6p) 1180 { 1181 1182 rtcache_unref(rt, &in6p->in6p_route); 1183 } 1184 1185 struct in6pcb * 1186 in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6, 1187 u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg, 1188 int faith, 1189 struct vestigial_inpcb *vp) 1190 { 1191 struct inpcbhead *head; 1192 struct inpcb_hdr *inph; 1193 struct in6pcb *in6p; 1194 u_int16_t fport = fport_arg, lport = lport_arg; 1195 1196 if (vp) 1197 vp->valid = 0; 1198 1199 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport); 1200 LIST_FOREACH(inph, head, inph_hash) { 1201 in6p = (struct in6pcb *)inph; 1202 if (in6p->in6p_af != AF_INET6) 1203 continue; 1204 1205 /* find exact match on both source and dest */ 1206 if (in6p->in6p_fport != fport) 1207 continue; 1208 if (in6p->in6p_lport != lport) 1209 continue; 1210 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 1211 continue; 1212 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)) 1213 continue; 1214 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 1215 continue; 1216 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 1217 continue; 1218 if ((IN6_IS_ADDR_V4MAPPED(laddr6) || 1219 IN6_IS_ADDR_V4MAPPED(faddr6)) && 1220 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 1221 continue; 1222 return in6p; 1223 } 1224 if (vp && table->vestige) { 1225 if ((*table->vestige->lookup6)(faddr6, fport_arg, 1226 laddr6, lport_arg, vp)) 1227 return NULL; 1228 } 1229 1230 return NULL; 1231 } 1232 1233 struct in6pcb * 1234 in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6, 1235 u_int lport_arg, int faith) 1236 { 1237 struct inpcbhead *head; 1238 struct inpcb_hdr *inph; 1239 struct in6pcb *in6p; 1240 u_int16_t lport = lport_arg; 1241 #ifdef INET 1242 struct in6_addr zero_mapped; 1243 #endif 1244 1245 head = IN6PCBHASH_BIND(table, laddr6, lport); 1246 LIST_FOREACH(inph, head, inph_hash) { 1247 in6p = (struct in6pcb *)inph; 1248 if (in6p->in6p_af != AF_INET6) 1249 continue; 1250 1251 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1252 continue; 1253 if (in6p->in6p_fport != 0) 1254 continue; 1255 if (in6p->in6p_lport != lport) 1256 continue; 1257 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 1258 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1259 continue; 1260 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 1261 goto out; 1262 } 1263 #ifdef INET 1264 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 1265 memset(&zero_mapped, 0, sizeof(zero_mapped)); 1266 zero_mapped.s6_addr16[5] = 0xffff; 1267 head = IN6PCBHASH_BIND(table, &zero_mapped, lport); 1268 LIST_FOREACH(inph, head, inph_hash) { 1269 in6p = (struct in6pcb *)inph; 1270 if (in6p->in6p_af != AF_INET6) 1271 continue; 1272 1273 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1274 continue; 1275 if (in6p->in6p_fport != 0) 1276 continue; 1277 if (in6p->in6p_lport != lport) 1278 continue; 1279 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1280 continue; 1281 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped)) 1282 goto out; 1283 } 1284 } 1285 #endif 1286 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport); 1287 LIST_FOREACH(inph, head, inph_hash) { 1288 in6p = (struct in6pcb *)inph; 1289 if (in6p->in6p_af != AF_INET6) 1290 continue; 1291 1292 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1293 continue; 1294 if (in6p->in6p_fport != 0) 1295 continue; 1296 if (in6p->in6p_lport != lport) 1297 continue; 1298 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 1299 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1300 continue; 1301 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr)) 1302 goto out; 1303 } 1304 return (NULL); 1305 1306 out: 1307 inph = &in6p->in6p_head; 1308 if (inph != LIST_FIRST(head)) { 1309 LIST_REMOVE(inph, inph_hash); 1310 LIST_INSERT_HEAD(head, inph, inph_hash); 1311 } 1312 return in6p; 1313 } 1314 1315 void 1316 in6_pcbstate(struct in6pcb *in6p, int state) 1317 { 1318 1319 if (in6p->in6p_af != AF_INET6) 1320 return; 1321 1322 if (in6p->in6p_state > IN6P_ATTACHED) 1323 LIST_REMOVE(&in6p->in6p_head, inph_hash); 1324 1325 switch (state) { 1326 case IN6P_BOUND: 1327 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table, 1328 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, 1329 inph_hash); 1330 break; 1331 case IN6P_CONNECTED: 1332 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table, 1333 &in6p->in6p_faddr, in6p->in6p_fport, 1334 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, 1335 inph_hash); 1336 break; 1337 } 1338 1339 in6p->in6p_state = state; 1340 } 1341