1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * Copyright (c) 2010-2011 Juniper Networks, Inc. 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Robert N. M. Watson under 9 * contract to Juniper Networks, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ 36 */ 37 38 /*- 39 * Copyright (c) 1982, 1986, 1991, 1993 40 * The Regents of the University of California. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 67 */ 68 69 #include <sys/cdefs.h> 70 __FBSDID("$FreeBSD$"); 71 72 #include "opt_inet.h" 73 #include "opt_inet6.h" 74 #include "opt_ipsec.h" 75 #include "opt_route.h" 76 #include "opt_rss.h" 77 78 #include <sys/hash.h> 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/malloc.h> 82 #include <sys/mbuf.h> 83 #include <sys/domain.h> 84 #include <sys/protosw.h> 85 #include <sys/socket.h> 86 #include <sys/socketvar.h> 87 #include <sys/sockio.h> 88 #include <sys/errno.h> 89 #include <sys/time.h> 90 #include <sys/priv.h> 91 #include <sys/proc.h> 92 #include <sys/jail.h> 93 94 #include <vm/uma.h> 95 96 #include <net/if.h> 97 #include <net/if_var.h> 98 #include <net/if_llatbl.h> 99 #include <net/if_types.h> 100 #include <net/route.h> 101 #include <net/route/nhop.h> 102 103 #include <netinet/in.h> 104 #include <netinet/in_var.h> 105 #include <netinet/in_systm.h> 106 #include <netinet/ip6.h> 107 #include <netinet/ip_var.h> 108 109 #include <netinet6/ip6_var.h> 110 #include <netinet6/nd6.h> 111 #include <netinet/in_pcb.h> 112 #include <netinet/in_pcb_var.h> 113 #include <netinet6/in6_pcb.h> 114 #include <netinet6/in6_fib.h> 115 #include <netinet6/scope6_var.h> 116 117 int 118 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred) 119 { 120 struct socket *so = inp->inp_socket; 121 u_int16_t lport = 0; 122 int error, lookupflags = 0; 123 #ifdef INVARIANTS 124 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 125 #endif 126 127 INP_WLOCK_ASSERT(inp); 128 INP_HASH_WLOCK_ASSERT(pcbinfo); 129 130 error = prison_local_ip6(cred, laddr, 131 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)); 132 if (error) 133 return(error); 134 135 /* XXX: this is redundant when called from in6_pcbbind */ 136 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 137 lookupflags = INPLOOKUP_WILDCARD; 138 139 inp->inp_flags |= INP_ANONPORT; 140 141 error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags); 142 if (error != 0) 143 return (error); 144 145 inp->inp_lport = lport; 146 if (in_pcbinshash(inp) != 0) { 147 inp->in6p_laddr = in6addr_any; 148 inp->inp_lport = 0; 149 return (EAGAIN); 150 } 151 152 return (0); 153 } 154 155 int 156 in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) 157 { 158 struct socket *so = inp->inp_socket; 159 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 160 u_short lport = 0; 161 int error, lookupflags = 0; 162 int reuseport = (so->so_options & SO_REUSEPORT); 163 164 /* 165 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 166 * so that we don't have to add to the (already messy) code below. 167 */ 168 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 169 170 INP_WLOCK_ASSERT(inp); 171 INP_HASH_WLOCK_ASSERT(pcbinfo); 172 173 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 174 return (EINVAL); 175 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 176 lookupflags = INPLOOKUP_WILDCARD; 177 if (sin6 == NULL) { 178 if ((error = prison_local_ip6(cred, &inp->in6p_laddr, 179 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 180 return (error); 181 } else { 182 KASSERT(sin6->sin6_family == AF_INET6, 183 ("%s: invalid address family for %p", __func__, sin6)); 184 KASSERT(sin6->sin6_len == sizeof(*sin6), 185 ("%s: invalid address length for %p", __func__, sin6)); 186 187 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 188 return(error); 189 190 if ((error = prison_local_ip6(cred, &sin6->sin6_addr, 191 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 192 return (error); 193 194 lport = sin6->sin6_port; 195 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 196 /* 197 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 198 * allow compepte duplication of binding if 199 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 200 * and a multicast address is bound on both 201 * new and duplicated sockets. 202 */ 203 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 204 reuseport = SO_REUSEADDR|SO_REUSEPORT; 205 /* 206 * XXX: How to deal with SO_REUSEPORT_LB here? 207 * Treat same as SO_REUSEPORT for now. 208 */ 209 if ((so->so_options & 210 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 211 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 212 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 213 struct epoch_tracker et; 214 struct ifaddr *ifa; 215 216 sin6->sin6_port = 0; /* yech... */ 217 NET_EPOCH_ENTER(et); 218 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) == 219 NULL && 220 (inp->inp_flags & INP_BINDANY) == 0) { 221 NET_EPOCH_EXIT(et); 222 return (EADDRNOTAVAIL); 223 } 224 225 /* 226 * XXX: bind to an anycast address might accidentally 227 * cause sending a packet with anycast source address. 228 * We should allow to bind to a deprecated address, since 229 * the application dares to use it. 230 */ 231 if (ifa != NULL && 232 ((struct in6_ifaddr *)ifa)->ia6_flags & 233 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 234 NET_EPOCH_EXIT(et); 235 return (EADDRNOTAVAIL); 236 } 237 NET_EPOCH_EXIT(et); 238 } 239 if (lport) { 240 struct inpcb *t; 241 242 /* GROSS */ 243 if (ntohs(lport) <= V_ipport_reservedhigh && 244 ntohs(lport) >= V_ipport_reservedlow && 245 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 246 return (EACCES); 247 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 248 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 249 t = in6_pcblookup_local(pcbinfo, 250 &sin6->sin6_addr, lport, 251 INPLOOKUP_WILDCARD, cred); 252 if (t != NULL && 253 (so->so_type != SOCK_STREAM || 254 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 255 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 256 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 257 (t->inp_flags2 & INP_REUSEPORT) || 258 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 259 (inp->inp_cred->cr_uid != 260 t->inp_cred->cr_uid)) 261 return (EADDRINUSE); 262 263 #ifdef INET 264 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 265 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 266 struct sockaddr_in sin; 267 268 in6_sin6_2_sin(&sin, sin6); 269 t = in_pcblookup_local(pcbinfo, 270 sin.sin_addr, lport, 271 INPLOOKUP_WILDCARD, cred); 272 if (t != NULL && 273 (so->so_type != SOCK_STREAM || 274 ntohl(t->inp_faddr.s_addr) == 275 INADDR_ANY) && 276 (inp->inp_cred->cr_uid != 277 t->inp_cred->cr_uid)) 278 return (EADDRINUSE); 279 } 280 #endif 281 } 282 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 283 lport, lookupflags, cred); 284 if (t && (reuseport & inp_so_options(t)) == 0 && 285 (reuseport_lb & inp_so_options(t)) == 0) { 286 return (EADDRINUSE); 287 } 288 #ifdef INET 289 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 290 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 291 struct sockaddr_in sin; 292 293 in6_sin6_2_sin(&sin, sin6); 294 t = in_pcblookup_local(pcbinfo, sin.sin_addr, 295 lport, lookupflags, cred); 296 if (t && 297 (reuseport & inp_so_options(t)) == 0 && 298 (reuseport_lb & inp_so_options(t)) == 0 && 299 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 300 (t->inp_vflag & INP_IPV6PROTO) != 0)) { 301 return (EADDRINUSE); 302 } 303 } 304 #endif 305 } 306 inp->in6p_laddr = sin6->sin6_addr; 307 } 308 if (lport == 0) { 309 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) { 310 /* Undo an address bind that may have occurred. */ 311 inp->in6p_laddr = in6addr_any; 312 return (error); 313 } 314 } else { 315 inp->inp_lport = lport; 316 if (in_pcbinshash(inp) != 0) { 317 inp->in6p_laddr = in6addr_any; 318 inp->inp_lport = 0; 319 return (EAGAIN); 320 } 321 } 322 return (0); 323 } 324 325 /* 326 * Transform old in6_pcbconnect() into an inner subroutine for new 327 * in6_pcbconnect(): Do some validity-checking on the remote 328 * address (in mbuf 'nam') and then determine local host address 329 * (i.e., which interface) to use to access that remote host. 330 * 331 * This preserves definition of in6_pcbconnect(), while supporting a 332 * slightly different version for T/TCP. (This is more than 333 * a bit of a kludge, but cleaning up the internal interfaces would 334 * have forced minor changes in every protocol). 335 */ 336 static int 337 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6, 338 struct in6_addr *plocal_addr6) 339 { 340 int error = 0; 341 int scope_ambiguous = 0; 342 struct in6_addr in6a; 343 344 NET_EPOCH_ASSERT(); 345 INP_WLOCK_ASSERT(inp); 346 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */ 347 348 if (sin6->sin6_port == 0) 349 return (EADDRNOTAVAIL); 350 351 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 352 scope_ambiguous = 1; 353 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 354 return(error); 355 356 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) { 357 /* 358 * If the destination address is UNSPECIFIED addr, 359 * use the loopback addr, e.g ::1. 360 */ 361 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 362 sin6->sin6_addr = in6addr_loopback; 363 } 364 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0) 365 return (error); 366 367 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts, 368 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL); 369 if (error) 370 return (error); 371 if (IN6_IS_ADDR_UNSPECIFIED(&in6a)) 372 return (EHOSTUNREACH); 373 374 /* 375 * Do not update this earlier, in case we return with an error. 376 * 377 * XXX: this in6_selectsrc_socket result might replace the bound local 378 * address with the address specified by setsockopt(IPV6_PKTINFO). 379 * Is it the intended behavior? 380 */ 381 *plocal_addr6 = in6a; 382 383 /* 384 * Don't do pcblookup call here; return interface in 385 * plocal_addr6 386 * and exit to caller, that will do the lookup. 387 */ 388 389 return (0); 390 } 391 392 /* 393 * Outer subroutine: 394 * Connect from a socket to a specified address. 395 * Both address and port must be specified in argument sin. 396 * If don't have a local address for this socket yet, 397 * then pick one. 398 */ 399 int 400 in6_pcbconnect(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred, 401 bool rehash) 402 { 403 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 404 struct sockaddr_in6 laddr6; 405 int error; 406 407 NET_EPOCH_ASSERT(); 408 INP_WLOCK_ASSERT(inp); 409 INP_HASH_WLOCK_ASSERT(pcbinfo); 410 KASSERT(sin6->sin6_family == AF_INET6, 411 ("%s: invalid address family for %p", __func__, sin6)); 412 KASSERT(sin6->sin6_len == sizeof(*sin6), 413 ("%s: invalid address length for %p", __func__, sin6)); 414 415 bzero(&laddr6, sizeof(laddr6)); 416 laddr6.sin6_family = AF_INET6; 417 418 #ifdef ROUTE_MPATH 419 if (CALC_FLOWID_OUTBOUND) { 420 uint32_t hash_type, hash_val; 421 422 hash_val = fib6_calc_software_hash(&inp->in6p_laddr, 423 &sin6->sin6_addr, 0, sin6->sin6_port, 424 inp->inp_socket->so_proto->pr_protocol, &hash_type); 425 inp->inp_flowid = hash_val; 426 inp->inp_flowtype = hash_type; 427 } 428 #endif 429 /* 430 * Call inner routine, to assign local interface address. 431 * in6_pcbladdr() may automatically fill in sin6_scope_id. 432 */ 433 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr)) != 0) 434 return (error); 435 436 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr, 437 sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ? 438 &laddr6.sin6_addr : &inp->in6p_laddr, inp->inp_lport, 0, 439 M_NODOM) != NULL) 440 return (EADDRINUSE); 441 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 442 if (inp->inp_lport == 0) { 443 /* 444 * rehash was required to be true in the past for 445 * this case; retain that convention. However, 446 * we now call in_pcb_lport_dest rather than 447 * in6_pcbbind; the former does not insert into 448 * the hash table, the latter does. Change rehash 449 * to false to do the in_pcbinshash below. 450 */ 451 KASSERT(rehash == true, 452 ("Rehashing required for unbound inps")); 453 rehash = false; 454 error = in_pcb_lport_dest(inp, 455 (struct sockaddr *) &laddr6, &inp->inp_lport, 456 (struct sockaddr *) sin6, sin6->sin6_port, cred, 457 INPLOOKUP_WILDCARD); 458 if (error) 459 return (error); 460 } 461 inp->in6p_laddr = laddr6.sin6_addr; 462 } 463 inp->in6p_faddr = sin6->sin6_addr; 464 inp->inp_fport = sin6->sin6_port; 465 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 466 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 467 if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 468 inp->inp_flow |= 469 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 470 471 if (rehash) { 472 in_pcbrehash(inp); 473 } else { 474 in_pcbinshash(inp); 475 } 476 477 return (0); 478 } 479 480 void 481 in6_pcbdisconnect(struct inpcb *inp) 482 { 483 484 INP_WLOCK_ASSERT(inp); 485 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 486 487 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr)); 488 memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr)); 489 inp->inp_fport = 0; 490 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 491 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 492 in_pcbrehash(inp); 493 } 494 495 struct sockaddr * 496 in6_sockaddr(in_port_t port, struct in6_addr *addr_p) 497 { 498 struct sockaddr_in6 *sin6; 499 500 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK); 501 bzero(sin6, sizeof *sin6); 502 sin6->sin6_family = AF_INET6; 503 sin6->sin6_len = sizeof(*sin6); 504 sin6->sin6_port = port; 505 sin6->sin6_addr = *addr_p; 506 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 507 508 return (struct sockaddr *)sin6; 509 } 510 511 struct sockaddr * 512 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 513 { 514 struct sockaddr_in sin; 515 struct sockaddr_in6 *sin6_p; 516 517 bzero(&sin, sizeof sin); 518 sin.sin_family = AF_INET; 519 sin.sin_len = sizeof(sin); 520 sin.sin_port = port; 521 sin.sin_addr = *addr_p; 522 523 sin6_p = malloc(sizeof *sin6_p, M_SONAME, 524 M_WAITOK); 525 in6_sin_2_v4mapsin6(&sin, sin6_p); 526 527 return (struct sockaddr *)sin6_p; 528 } 529 530 int 531 in6_getsockaddr(struct socket *so, struct sockaddr **nam) 532 { 533 struct inpcb *inp; 534 struct in6_addr addr; 535 in_port_t port; 536 537 inp = sotoinpcb(so); 538 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 539 540 INP_RLOCK(inp); 541 port = inp->inp_lport; 542 addr = inp->in6p_laddr; 543 INP_RUNLOCK(inp); 544 545 *nam = in6_sockaddr(port, &addr); 546 return 0; 547 } 548 549 int 550 in6_getpeeraddr(struct socket *so, struct sockaddr **nam) 551 { 552 struct inpcb *inp; 553 struct in6_addr addr; 554 in_port_t port; 555 556 inp = sotoinpcb(so); 557 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 558 559 INP_RLOCK(inp); 560 port = inp->inp_fport; 561 addr = inp->in6p_faddr; 562 INP_RUNLOCK(inp); 563 564 *nam = in6_sockaddr(port, &addr); 565 return 0; 566 } 567 568 int 569 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 570 { 571 struct inpcb *inp; 572 int error; 573 574 inp = sotoinpcb(so); 575 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 576 577 #ifdef INET 578 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 579 error = in_getsockaddr(so, nam); 580 if (error == 0) 581 in6_sin_2_v4mapsin6_in_sock(nam); 582 } else 583 #endif 584 { 585 /* scope issues will be handled in in6_getsockaddr(). */ 586 error = in6_getsockaddr(so, nam); 587 } 588 589 return error; 590 } 591 592 int 593 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 594 { 595 struct inpcb *inp; 596 int error; 597 598 inp = sotoinpcb(so); 599 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 600 601 #ifdef INET 602 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 603 error = in_getpeeraddr(so, nam); 604 if (error == 0) 605 in6_sin_2_v4mapsin6_in_sock(nam); 606 } else 607 #endif 608 /* scope issues will be handled in in6_getpeeraddr(). */ 609 error = in6_getpeeraddr(so, nam); 610 611 return error; 612 } 613 614 /* 615 * Pass some notification to all connections of a protocol 616 * associated with address dst. The local address and/or port numbers 617 * may be specified to limit the search. The "usual action" will be 618 * taken, depending on the ctlinput cmd. The caller must filter any 619 * cmds that are uninteresting (e.g., no error in the map). 620 * Call the protocol specific routine (if any) to report 621 * any errors for each matching socket. 622 */ 623 static bool 624 inp_match6(const struct inpcb *inp, void *v __unused) 625 { 626 627 return ((inp->inp_vflag & INP_IPV6) != 0); 628 } 629 630 void 631 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst, 632 u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg, 633 int errno, void *cmdarg, 634 struct inpcb *(*notify)(struct inpcb *, int)) 635 { 636 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 637 inp_match6, NULL); 638 struct inpcb *inp; 639 struct sockaddr_in6 sa6_src; 640 u_short fport = fport_arg, lport = lport_arg; 641 u_int32_t flowinfo; 642 643 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 644 return; 645 646 /* 647 * note that src can be NULL when we get notify by local fragmentation. 648 */ 649 sa6_src = (src == NULL) ? sa6_any : *src; 650 flowinfo = sa6_src.sin6_flowinfo; 651 652 while ((inp = inp_next(&inpi)) != NULL) { 653 INP_WLOCK_ASSERT(inp); 654 /* 655 * If the error designates a new path MTU for a destination 656 * and the application (associated with this socket) wanted to 657 * know the value, notify. 658 * XXX: should we avoid to notify the value to TCP sockets? 659 */ 660 if (errno == EMSGSIZE && cmdarg != NULL) 661 ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg); 662 663 /* 664 * Detect if we should notify the error. If no source and 665 * destination ports are specified, but non-zero flowinfo and 666 * local address match, notify the error. This is the case 667 * when the error is delivered with an encrypted buffer 668 * by ESP. Otherwise, just compare addresses and ports 669 * as usual. 670 */ 671 if (lport == 0 && fport == 0 && flowinfo && 672 inp->inp_socket != NULL && 673 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) && 674 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 675 goto do_notify; 676 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 677 &sa6_dst->sin6_addr) || 678 inp->inp_socket == 0 || 679 (lport && inp->inp_lport != lport) || 680 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 681 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 682 &sa6_src.sin6_addr)) || 683 (fport && inp->inp_fport != fport)) { 684 continue; 685 } 686 687 do_notify: 688 if (notify) 689 (*notify)(inp, errno); 690 } 691 } 692 693 /* 694 * Lookup a PCB based on the local address and port. Caller must hold the 695 * hash lock. No inpcb locks or references are acquired. 696 */ 697 struct inpcb * 698 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 699 u_short lport, int lookupflags, struct ucred *cred) 700 { 701 struct inpcb *inp; 702 int matchwild = 3, wildcard; 703 704 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 705 ("%s: invalid lookup flags %d", __func__, lookupflags)); 706 707 INP_HASH_LOCK_ASSERT(pcbinfo); 708 709 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 710 struct inpcbhead *head; 711 /* 712 * Look for an unconnected (wildcard foreign addr) PCB that 713 * matches the local address and port we're looking for. 714 */ 715 head = &pcbinfo->ipi_hashbase[INP_PCBHASH_WILD(lport, 716 pcbinfo->ipi_hashmask)]; 717 CK_LIST_FOREACH(inp, head, inp_hash) { 718 /* XXX inp locking */ 719 if ((inp->inp_vflag & INP_IPV6) == 0) 720 continue; 721 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 722 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 723 inp->inp_lport == lport) { 724 /* Found. */ 725 if (prison_equal_ip6(cred->cr_prison, 726 inp->inp_cred->cr_prison)) 727 return (inp); 728 } 729 } 730 /* 731 * Not found. 732 */ 733 return (NULL); 734 } else { 735 struct inpcbporthead *porthash; 736 struct inpcbport *phd; 737 struct inpcb *match = NULL; 738 /* 739 * Best fit PCB lookup. 740 * 741 * First see if this local port is in use by looking on the 742 * port hash list. 743 */ 744 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 745 pcbinfo->ipi_porthashmask)]; 746 CK_LIST_FOREACH(phd, porthash, phd_hash) { 747 if (phd->phd_port == lport) 748 break; 749 } 750 if (phd != NULL) { 751 /* 752 * Port is in use by one or more PCBs. Look for best 753 * fit. 754 */ 755 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 756 wildcard = 0; 757 if (!prison_equal_ip6(cred->cr_prison, 758 inp->inp_cred->cr_prison)) 759 continue; 760 /* XXX inp locking */ 761 if ((inp->inp_vflag & INP_IPV6) == 0) 762 continue; 763 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 764 wildcard++; 765 if (!IN6_IS_ADDR_UNSPECIFIED( 766 &inp->in6p_laddr)) { 767 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 768 wildcard++; 769 else if (!IN6_ARE_ADDR_EQUAL( 770 &inp->in6p_laddr, laddr)) 771 continue; 772 } else { 773 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 774 wildcard++; 775 } 776 if (wildcard < matchwild) { 777 match = inp; 778 matchwild = wildcard; 779 if (matchwild == 0) 780 break; 781 } 782 } 783 } 784 return (match); 785 } 786 } 787 788 static bool 789 in6_multi_match(const struct inpcb *inp, void *v __unused) 790 { 791 792 if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL) 793 return (true); 794 else 795 return (false); 796 } 797 798 void 799 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 800 { 801 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB, 802 in6_multi_match, NULL); 803 struct inpcb *inp; 804 struct in6_multi *inm; 805 struct in6_mfilter *imf; 806 struct ip6_moptions *im6o; 807 808 IN6_MULTI_LOCK_ASSERT(); 809 810 while ((inp = inp_next(&inpi)) != NULL) { 811 INP_RLOCK_ASSERT(inp); 812 813 im6o = inp->in6p_moptions; 814 /* 815 * Unselect the outgoing ifp for multicast if it 816 * is being detached. 817 */ 818 if (im6o->im6o_multicast_ifp == ifp) 819 im6o->im6o_multicast_ifp = NULL; 820 /* 821 * Drop multicast group membership if we joined 822 * through the interface being detached. 823 */ 824 restart: 825 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) { 826 if ((inm = imf->im6f_in6m) == NULL) 827 continue; 828 if (inm->in6m_ifp != ifp) 829 continue; 830 ip6_mfilter_remove(&im6o->im6o_head, imf); 831 in6_leavegroup_locked(inm, NULL); 832 ip6_mfilter_free(imf); 833 goto restart; 834 } 835 } 836 } 837 838 /* 839 * Check for alternatives when higher level complains 840 * about service problems. For now, invalidate cached 841 * routing information. If the route was created dynamically 842 * (by a redirect), time to try a default gateway again. 843 */ 844 void 845 in6_losing(struct inpcb *inp) 846 { 847 848 RO_INVALIDATE_CACHE(&inp->inp_route6); 849 } 850 851 /* 852 * After a routing change, flush old routing 853 * and allocate a (hopefully) better one. 854 */ 855 struct inpcb * 856 in6_rtchange(struct inpcb *inp, int errno __unused) 857 { 858 859 RO_INVALIDATE_CACHE(&inp->inp_route6); 860 return inp; 861 } 862 863 static bool 864 in6_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain) 865 { 866 return (domain == M_NODOM || domain == grp->il_numa_domain); 867 } 868 869 static struct inpcb * 870 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 871 const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr, 872 uint16_t lport, uint8_t domain) 873 { 874 const struct inpcblbgrouphead *hdr; 875 struct inpcblbgroup *grp; 876 struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 877 878 INP_HASH_LOCK_ASSERT(pcbinfo); 879 880 hdr = &pcbinfo->ipi_lbgrouphashbase[ 881 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 882 883 /* 884 * Search for an LB group match based on the following criteria: 885 * - prefer jailed groups to non-jailed groups 886 * - prefer exact source address matches to wildcard matches 887 * - prefer groups bound to the specified NUMA domain 888 */ 889 jail_exact = jail_wild = local_exact = local_wild = NULL; 890 CK_LIST_FOREACH(grp, hdr, il_list) { 891 bool injail; 892 893 #ifdef INET 894 if (!(grp->il_vflag & INP_IPV6)) 895 continue; 896 #endif 897 if (grp->il_lport != lport) 898 continue; 899 900 injail = prison_flag(grp->il_cred, PR_IP6) != 0; 901 if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison, 902 laddr) != 0) 903 continue; 904 905 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) { 906 if (injail) { 907 jail_exact = grp; 908 if (in6_pcblookup_lb_numa_match(grp, domain)) 909 /* This is a perfect match. */ 910 goto out; 911 } else if (local_exact == NULL || 912 in6_pcblookup_lb_numa_match(grp, domain)) { 913 local_exact = grp; 914 } 915 } else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) { 916 if (injail) { 917 if (jail_wild == NULL || 918 in6_pcblookup_lb_numa_match(grp, domain)) 919 jail_wild = grp; 920 } else if (local_wild == NULL || 921 in6_pcblookup_lb_numa_match(grp, domain)) { 922 local_wild = grp; 923 } 924 } 925 } 926 927 if (jail_exact != NULL) 928 grp = jail_exact; 929 else if (jail_wild != NULL) 930 grp = jail_wild; 931 else if (local_exact != NULL) 932 grp = local_exact; 933 else 934 grp = local_wild; 935 if (grp == NULL) 936 return (NULL); 937 out: 938 return (grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) % 939 grp->il_inpcnt]); 940 } 941 942 static struct inpcb * 943 in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 944 u_short fport, struct in6_addr *laddr, u_short lport) 945 { 946 struct inpcbhead *head; 947 struct inpcb *inp, *match; 948 949 INP_HASH_LOCK_ASSERT(pcbinfo); 950 951 /* 952 * First look for an exact match. 953 */ 954 match = NULL; 955 head = &pcbinfo->ipi_hashbase[INP6_PCBHASH(faddr, lport, fport, 956 pcbinfo->ipi_hashmask)]; 957 CK_LIST_FOREACH(inp, head, inp_hash) { 958 /* XXX inp locking */ 959 if ((inp->inp_vflag & INP_IPV6) == 0) 960 continue; 961 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 962 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 963 inp->inp_fport == fport && 964 inp->inp_lport == lport) 965 return (inp); 966 } 967 return (match); 968 } 969 970 static struct inpcb * 971 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, 972 struct in6_addr *faddr, u_short fport, struct in6_addr *laddr, 973 u_short lport) 974 { 975 struct inpcbhead *head; 976 struct inpcb *inp, *jail_wild, *local_exact, *local_wild; 977 978 /* 979 * Order of socket selection - we always prefer jails. 980 * 1. jailed, non-wild. 981 * 2. jailed, wild. 982 * 3. non-jailed, non-wild. 983 * 4. non-jailed, wild. 984 */ 985 head = &pcbinfo->ipi_hashbase[INP_PCBHASH_WILD(lport, 986 pcbinfo->ipi_hashmask)]; 987 local_wild = local_exact = jail_wild = NULL; 988 CK_LIST_FOREACH(inp, head, inp_hash) { 989 bool injail; 990 991 /* XXX inp locking */ 992 if ((inp->inp_vflag & INP_IPV6) == 0) 993 continue; 994 995 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 996 inp->inp_lport != lport) { 997 continue; 998 } 999 1000 injail = prison_flag(inp->inp_cred, PR_IP6) != 0; 1001 if (injail) { 1002 if (prison_check_ip6_locked( 1003 inp->inp_cred->cr_prison, laddr) != 0) 1004 continue; 1005 } else { 1006 if (local_exact != NULL) 1007 continue; 1008 } 1009 1010 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) { 1011 if (injail) 1012 return (inp); 1013 else 1014 local_exact = inp; 1015 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 1016 if (injail) 1017 jail_wild = inp; 1018 else 1019 local_wild = inp; 1020 } 1021 } 1022 1023 if (jail_wild != NULL) 1024 return (jail_wild); 1025 if (local_exact != NULL) 1026 return (local_exact); 1027 if (local_wild != NULL) 1028 return (local_wild); 1029 return (NULL); 1030 } 1031 1032 struct inpcb * 1033 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1034 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, 1035 int lookupflags, uint8_t numa_domain) 1036 { 1037 struct inpcb *inp; 1038 u_short fport = fport_arg, lport = lport_arg; 1039 1040 KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0, 1041 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1042 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr), 1043 ("%s: invalid foreign address", __func__)); 1044 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr), 1045 ("%s: invalid local address", __func__)); 1046 1047 INP_HASH_LOCK_ASSERT(pcbinfo); 1048 1049 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 1050 if (inp != NULL) 1051 return (inp); 1052 1053 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1054 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr, 1055 lport, numa_domain); 1056 if (inp == NULL) { 1057 inp = in6_pcblookup_hash_wild_locked(pcbinfo, faddr, 1058 fport, laddr, lport); 1059 } 1060 } 1061 return (inp); 1062 } 1063 1064 static struct inpcb * 1065 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1066 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags, 1067 uint8_t numa_domain) 1068 { 1069 struct inpcb *inp; 1070 1071 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1072 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1073 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1074 ("%s: LOCKPCB not set", __func__)); 1075 1076 smr_enter(pcbinfo->ipi_smr); 1077 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1078 lookupflags & INPLOOKUP_WILDCARD, numa_domain); 1079 if (inp != NULL) { 1080 if (__predict_false(inp_smr_lock(inp, 1081 (lookupflags & INPLOOKUP_LOCKMASK)) == false)) 1082 inp = NULL; 1083 } else 1084 smr_exit(pcbinfo->ipi_smr); 1085 1086 return (inp); 1087 } 1088 1089 /* 1090 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1091 * from which a pre-calculated hash value may be extracted. 1092 */ 1093 struct inpcb * 1094 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport, 1095 struct in6_addr *laddr, u_int lport, int lookupflags, 1096 struct ifnet *ifp __unused) 1097 { 1098 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1099 lookupflags, M_NODOM)); 1100 } 1101 1102 struct inpcb * 1103 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1104 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags, 1105 struct ifnet *ifp __unused, struct mbuf *m) 1106 { 1107 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1108 lookupflags, m->m_pkthdr.numa_domain)); 1109 } 1110 1111 void 1112 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst) 1113 { 1114 struct ip6_hdr *ip; 1115 1116 ip = mtod(m, struct ip6_hdr *); 1117 bzero(sin6, sizeof(*sin6)); 1118 sin6->sin6_len = sizeof(*sin6); 1119 sin6->sin6_family = AF_INET6; 1120 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src; 1121 1122 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 1123 1124 return; 1125 } 1126