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