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