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