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