2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
39 * Copyright (c) 1982, 1986, 1991, 1993
40 * The Regents of the University of California. All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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
66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
73 #include "opt_inet6.h"
74 #include "opt_ipsec.h"
75 #include "opt_pcbgroup.h"
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/malloc.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>
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>
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>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet6/in6_pcb.h>
113 #include <netinet6/in6_fib.h>
114 #include <netinet6/scope6_var.h>
116 static struct inpcb *in6_pcblookup_hash_locked(struct inpcbinfo *,
117 struct in6_addr *, u_int, struct in6_addr *, u_int, int, struct ifnet *);
120 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
123 struct socket *so = inp->inp_socket;
124 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
125 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
127 int error, lookupflags = 0;
128 int reuseport = (so->so_options & SO_REUSEPORT);
131 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
132 * so that we don't have to add to the (already messy) code below.
134 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
136 INP_WLOCK_ASSERT(inp);
137 INP_HASH_WLOCK_ASSERT(pcbinfo);
139 if (CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
140 return (EADDRNOTAVAIL);
141 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
143 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
144 lookupflags = INPLOOKUP_WILDCARD;
146 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
147 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
150 sin6 = (struct sockaddr_in6 *)nam;
151 if (nam->sa_len != sizeof(*sin6))
156 if (nam->sa_family != AF_INET6)
157 return (EAFNOSUPPORT);
159 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
162 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
163 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
166 lport = sin6->sin6_port;
167 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
169 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
170 * allow compepte duplication of binding if
171 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
172 * and a multicast address is bound on both
173 * new and duplicated sockets.
175 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
176 reuseport = SO_REUSEADDR|SO_REUSEPORT;
178 * XXX: How to deal with SO_REUSEPORT_LB here?
179 * Treat same as SO_REUSEPORT for now.
181 if ((so->so_options &
182 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
183 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
184 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
185 struct epoch_tracker et;
188 sin6->sin6_port = 0; /* yech... */
190 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
192 (inp->inp_flags & INP_BINDANY) == 0) {
194 return (EADDRNOTAVAIL);
198 * XXX: bind to an anycast address might accidentally
199 * cause sending a packet with anycast source address.
200 * We should allow to bind to a deprecated address, since
201 * the application dares to use it.
204 ((struct in6_ifaddr *)ifa)->ia6_flags &
205 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
207 return (EADDRNOTAVAIL);
216 if (ntohs(lport) <= V_ipport_reservedhigh &&
217 ntohs(lport) >= V_ipport_reservedlow &&
218 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
220 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
221 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
222 t = in6_pcblookup_local(pcbinfo,
223 &sin6->sin6_addr, lport,
224 INPLOOKUP_WILDCARD, cred);
226 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
227 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
228 (so->so_type != SOCK_STREAM ||
229 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
230 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
231 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
232 (t->inp_flags2 & INP_REUSEPORT) ||
233 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
234 (inp->inp_cred->cr_uid !=
235 t->inp_cred->cr_uid))
239 * If the socket is a BINDMULTI socket, then
240 * the credentials need to match and the
241 * original socket also has to have been bound
244 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
248 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
249 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
250 struct sockaddr_in sin;
252 in6_sin6_2_sin(&sin, sin6);
253 t = in_pcblookup_local(pcbinfo,
255 INPLOOKUP_WILDCARD, cred);
257 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
259 INP_TIMEWAIT) == 0) &&
260 (so->so_type != SOCK_STREAM ||
261 ntohl(t->inp_faddr.s_addr) ==
263 (inp->inp_cred->cr_uid !=
264 t->inp_cred->cr_uid))
267 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
272 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
273 lport, lookupflags, cred);
274 if (t && (t->inp_flags & INP_TIMEWAIT)) {
276 * XXXRW: If an incpb has had its timewait
277 * state recycled, we treat the address as
278 * being in use (for now). This is better
279 * than a panic, but not desirable.
283 ((reuseport & tw->tw_so_options) == 0 &&
284 (reuseport_lb & tw->tw_so_options) == 0))
286 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
287 (reuseport_lb & inp_so_options(t)) == 0) {
291 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
292 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
293 struct sockaddr_in sin;
295 in6_sin6_2_sin(&sin, sin6);
296 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
297 lport, lookupflags, cred);
298 if (t && t->inp_flags & INP_TIMEWAIT) {
302 if ((reuseport & tw->tw_so_options) == 0
303 && (reuseport_lb & tw->tw_so_options) == 0
304 && (ntohl(t->inp_laddr.s_addr) !=
305 INADDR_ANY || ((inp->inp_vflag &
307 (t->inp_vflag & INP_IPV6PROTO))))
310 (reuseport & inp_so_options(t)) == 0 &&
311 (reuseport_lb & inp_so_options(t)) == 0 &&
312 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
313 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
319 inp->in6p_laddr = sin6->sin6_addr;
322 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
323 /* Undo an address bind that may have occurred. */
324 inp->in6p_laddr = in6addr_any;
328 inp->inp_lport = lport;
329 if (in_pcbinshash(inp) != 0) {
330 inp->in6p_laddr = in6addr_any;
339 * Transform old in6_pcbconnect() into an inner subroutine for new
340 * in6_pcbconnect(): Do some validity-checking on the remote
341 * address (in mbuf 'nam') and then determine local host address
342 * (i.e., which interface) to use to access that remote host.
344 * This preserves definition of in6_pcbconnect(), while supporting a
345 * slightly different version for T/TCP. (This is more than
346 * a bit of a kludge, but cleaning up the internal interfaces would
347 * have forced minor changes in every protocol).
350 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
351 struct in6_addr *plocal_addr6)
353 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
355 int scope_ambiguous = 0;
356 struct in6_addr in6a;
358 INP_WLOCK_ASSERT(inp);
359 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
361 if (nam->sa_len != sizeof (*sin6))
363 if (sin6->sin6_family != AF_INET6)
364 return (EAFNOSUPPORT);
365 if (sin6->sin6_port == 0)
366 return (EADDRNOTAVAIL);
368 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
370 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
373 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
375 * If the destination address is UNSPECIFIED addr,
376 * use the loopback addr, e.g ::1.
378 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
379 sin6->sin6_addr = in6addr_loopback;
381 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
384 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
385 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
390 * Do not update this earlier, in case we return with an error.
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?
396 *plocal_addr6 = in6a;
399 * Don't do pcblookup call here; return interface in
401 * and exit to caller, that will do the lookup.
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,
415 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
416 struct ucred *cred, struct mbuf *m, bool rehash)
418 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
419 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
420 struct in6_addr addr6;
423 INP_WLOCK_ASSERT(inp);
424 INP_HASH_WLOCK_ASSERT(pcbinfo);
427 * Call inner routine, to assign local interface address.
428 * in6_pcbladdr() may automatically fill in sin6_scope_id.
430 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
433 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
435 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
436 ? &addr6 : &inp->in6p_laddr,
437 inp->inp_lport, 0, NULL) != NULL) {
440 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
441 if (inp->inp_lport == 0) {
442 KASSERT(rehash == true,
443 ("Rehashing required for unbound inps"));
444 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
448 inp->in6p_laddr = addr6;
450 inp->in6p_faddr = sin6->sin6_addr;
451 inp->inp_fport = sin6->sin6_port;
452 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
453 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
454 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
456 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
459 in_pcbrehash_mbuf(inp, m);
461 in_pcbinshash_mbuf(inp, m);
468 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
471 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true));
475 in6_pcbdisconnect(struct inpcb *inp)
478 INP_WLOCK_ASSERT(inp);
479 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
481 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
483 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
484 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
489 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
491 struct sockaddr_in6 *sin6;
493 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
494 bzero(sin6, sizeof *sin6);
495 sin6->sin6_family = AF_INET6;
496 sin6->sin6_len = sizeof(*sin6);
497 sin6->sin6_port = port;
498 sin6->sin6_addr = *addr_p;
499 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
501 return (struct sockaddr *)sin6;
505 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
507 struct sockaddr_in sin;
508 struct sockaddr_in6 *sin6_p;
510 bzero(&sin, sizeof sin);
511 sin.sin_family = AF_INET;
512 sin.sin_len = sizeof(sin);
514 sin.sin_addr = *addr_p;
516 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
518 in6_sin_2_v4mapsin6(&sin, sin6_p);
520 return (struct sockaddr *)sin6_p;
524 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
527 struct in6_addr addr;
531 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
534 port = inp->inp_lport;
535 addr = inp->in6p_laddr;
538 *nam = in6_sockaddr(port, &addr);
543 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
546 struct in6_addr addr;
550 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
553 port = inp->inp_fport;
554 addr = inp->in6p_faddr;
557 *nam = in6_sockaddr(port, &addr);
562 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
568 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
571 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
572 error = in_getsockaddr(so, nam);
574 in6_sin_2_v4mapsin6_in_sock(nam);
578 /* scope issues will be handled in in6_getsockaddr(). */
579 error = in6_getsockaddr(so, nam);
586 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
592 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
595 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
596 error = in_getpeeraddr(so, nam);
598 in6_sin_2_v4mapsin6_in_sock(nam);
601 /* scope issues will be handled in in6_getpeeraddr(). */
602 error = in6_getpeeraddr(so, nam);
608 * Pass some notification to all connections of a protocol
609 * associated with address dst. The local address and/or port numbers
610 * may be specified to limit the search. The "usual action" will be
611 * taken, depending on the ctlinput cmd. The caller must filter any
612 * cmds that are uninteresting (e.g., no error in the map).
613 * Call the protocol specific routine (if any) to report
614 * any errors for each matching socket.
617 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
618 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
619 int cmd, void *cmdarg,
620 struct inpcb *(*notify)(struct inpcb *, int))
622 struct inpcb *inp, *inp_temp;
623 struct sockaddr_in6 sa6_src, *sa6_dst;
624 u_short fport = fport_arg, lport = lport_arg;
628 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
631 sa6_dst = (struct sockaddr_in6 *)dst;
632 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
636 * note that src can be NULL when we get notify by local fragmentation.
638 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
639 flowinfo = sa6_src.sin6_flowinfo;
642 * Redirects go to all references to the destination,
643 * and use in6_rtchange to invalidate the route cache.
644 * Dead host indications: also use in6_rtchange to invalidate
645 * the cache, and deliver the error to all the sockets.
646 * Otherwise, if we have knowledge of the local port and address,
647 * deliver only to that socket.
649 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
652 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
654 if (cmd != PRC_HOSTDEAD)
655 notify = in6_rtchange;
657 errno = inet6ctlerrmap[cmd];
658 INP_INFO_WLOCK(pcbinfo);
659 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
661 if ((inp->inp_vflag & INP_IPV6) == 0) {
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?
672 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
673 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
674 *(u_int32_t *)cmdarg);
677 * Detect if we should notify the error. If no source and
678 * destination ports are specifed, but non-zero flowinfo and
679 * local address match, notify the error. This is the case
680 * when the error is delivered with an encrypted buffer
681 * by ESP. Otherwise, just compare addresses and ports
684 if (lport == 0 && fport == 0 && flowinfo &&
685 inp->inp_socket != NULL &&
686 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
687 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
689 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
690 &sa6_dst->sin6_addr) ||
691 inp->inp_socket == 0 ||
692 (lport && inp->inp_lport != lport) ||
693 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
694 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
695 &sa6_src.sin6_addr)) ||
696 (fport && inp->inp_fport != fport)) {
703 if ((*notify)(inp, errno))
708 INP_INFO_WUNLOCK(pcbinfo);
712 * Lookup a PCB based on the local address and port. Caller must hold the
713 * hash lock. No inpcb locks or references are acquired.
716 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
717 u_short lport, int lookupflags, struct ucred *cred)
720 int matchwild = 3, wildcard;
722 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
723 ("%s: invalid lookup flags %d", __func__, lookupflags));
725 INP_HASH_LOCK_ASSERT(pcbinfo);
727 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
728 struct inpcbhead *head;
730 * Look for an unconnected (wildcard foreign addr) PCB that
731 * matches the local address and port we're looking for.
733 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
734 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
735 pcbinfo->ipi_hashmask)];
736 CK_LIST_FOREACH(inp, head, inp_hash) {
737 /* XXX inp locking */
738 if ((inp->inp_vflag & INP_IPV6) == 0)
740 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
741 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
742 inp->inp_lport == lport) {
745 prison_equal_ip6(cred->cr_prison,
746 inp->inp_cred->cr_prison))
755 struct inpcbporthead *porthash;
756 struct inpcbport *phd;
757 struct inpcb *match = NULL;
759 * Best fit PCB lookup.
761 * First see if this local port is in use by looking on the
764 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
765 pcbinfo->ipi_porthashmask)];
766 CK_LIST_FOREACH(phd, porthash, phd_hash) {
767 if (phd->phd_port == lport)
772 * Port is in use by one or more PCBs. Look for best
775 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
778 !prison_equal_ip6(cred->cr_prison,
779 inp->inp_cred->cr_prison))
781 /* XXX inp locking */
782 if ((inp->inp_vflag & INP_IPV6) == 0)
784 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
786 if (!IN6_IS_ADDR_UNSPECIFIED(
788 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
790 else if (!IN6_ARE_ADDR_EQUAL(
791 &inp->in6p_laddr, laddr))
794 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
797 if (wildcard < matchwild) {
799 matchwild = wildcard;
810 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
813 struct in6_multi *inm;
814 struct in6_mfilter *imf;
815 struct ip6_moptions *im6o;
817 INP_INFO_WLOCK(pcbinfo);
818 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
820 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
824 im6o = inp->in6p_moptions;
825 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) {
827 * Unselect the outgoing ifp for multicast if it
830 if (im6o->im6o_multicast_ifp == ifp)
831 im6o->im6o_multicast_ifp = NULL;
833 * Drop multicast group membership if we joined
834 * through the interface being detached.
837 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
838 if ((inm = imf->im6f_in6m) == NULL)
840 if (inm->in6m_ifp != ifp)
842 ip6_mfilter_remove(&im6o->im6o_head, imf);
843 IN6_MULTI_LOCK_ASSERT();
844 in6_leavegroup_locked(inm, NULL);
845 ip6_mfilter_free(imf);
851 INP_INFO_WUNLOCK(pcbinfo);
855 * Check for alternatives when higher level complains
856 * about service problems. For now, invalidate cached
857 * routing information. If the route was created dynamically
858 * (by a redirect), time to try a default gateway again.
861 in6_losing(struct inpcb *inp)
864 RO_INVALIDATE_CACHE(&inp->inp_route6);
868 * After a routing change, flush old routing
869 * and allocate a (hopefully) better one.
872 in6_rtchange(struct inpcb *inp, int errno __unused)
875 RO_INVALIDATE_CACHE(&inp->inp_route6);
879 static struct inpcb *
880 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
881 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
882 uint16_t fport, int lookupflags)
884 struct inpcb *local_wild;
885 const struct inpcblbgrouphead *hdr;
886 struct inpcblbgroup *grp;
889 INP_HASH_LOCK_ASSERT(pcbinfo);
891 hdr = &pcbinfo->ipi_lbgrouphashbase[
892 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
895 * Order of socket selection:
897 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
900 * - Load balanced group does not contain jailed sockets.
901 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
904 CK_LIST_FOREACH(grp, hdr, il_list) {
906 if (!(grp->il_vflag & INP_IPV6))
909 if (grp->il_lport != lport)
912 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
913 fport) % grp->il_inpcnt;
914 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr))
915 return (grp->il_inp[idx]);
916 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
917 (lookupflags & INPLOOKUP_WILDCARD) != 0)
918 local_wild = grp->il_inp[idx];
925 * Lookup PCB in hash list, using pcbgroup tables.
927 static struct inpcb *
928 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
929 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
930 u_int lport_arg, int lookupflags, struct ifnet *ifp)
932 struct inpcbhead *head;
933 struct inpcb *inp, *tmpinp;
934 u_short fport = fport_arg, lport = lport_arg;
938 * First look for an exact match.
941 INP_GROUP_LOCK(pcbgroup);
942 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
943 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
944 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
945 /* XXX inp locking */
946 if ((inp->inp_vflag & INP_IPV6) == 0)
948 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
949 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
950 inp->inp_fport == fport &&
951 inp->inp_lport == lport) {
953 * XXX We should be able to directly return
954 * the inp here, without any checks.
955 * Well unless both bound with SO_REUSEPORT?
957 if (prison_flag(inp->inp_cred, PR_IP6))
963 if (tmpinp != NULL) {
969 * Then look for a wildcard match in the pcbgroup.
971 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
972 struct inpcb *local_wild = NULL, *local_exact = NULL;
973 struct inpcb *jail_wild = NULL;
977 * Order of socket selection - we always prefer jails.
978 * 1. jailed, non-wild.
980 * 3. non-jailed, non-wild.
981 * 4. non-jailed, wild.
983 head = &pcbgroup->ipg_hashbase[
984 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
985 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
986 /* XXX inp locking */
987 if ((inp->inp_vflag & INP_IPV6) == 0)
990 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
991 inp->inp_lport != lport) {
995 injail = prison_flag(inp->inp_cred, PR_IP6);
997 if (prison_check_ip6(inp->inp_cred,
1001 if (local_exact != NULL)
1005 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1010 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1016 } /* LIST_FOREACH */
1030 * Then look for a wildcard match, if requested.
1032 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1033 struct inpcb *local_wild = NULL, *local_exact = NULL;
1034 struct inpcb *jail_wild = NULL;
1038 * Order of socket selection - we always prefer jails.
1039 * 1. jailed, non-wild.
1041 * 3. non-jailed, non-wild.
1042 * 4. non-jailed, wild.
1044 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1045 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1046 pcbinfo->ipi_wildmask)];
1047 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1048 /* XXX inp locking */
1049 if ((inp->inp_vflag & INP_IPV6) == 0)
1052 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1053 inp->inp_lport != lport) {
1057 injail = prison_flag(inp->inp_cred, PR_IP6);
1059 if (prison_check_ip6(inp->inp_cred,
1063 if (local_exact != NULL)
1067 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1072 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1078 } /* LIST_FOREACH */
1089 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1090 INP_GROUP_UNLOCK(pcbgroup);
1094 if (lookupflags & INPLOOKUP_WLOCKPCB)
1095 locked = INP_TRY_WLOCK(inp);
1096 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1097 locked = INP_TRY_RLOCK(inp);
1099 panic("%s: locking buf", __func__);
1102 INP_GROUP_UNLOCK(pcbgroup);
1104 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1106 if (in_pcbrele_wlocked(inp))
1110 if (in_pcbrele_rlocked(inp))
1115 if (lookupflags & INPLOOKUP_WLOCKPCB)
1116 INP_WLOCK_ASSERT(inp);
1118 INP_RLOCK_ASSERT(inp);
1122 #endif /* PCBGROUP */
1125 * Lookup PCB in hash list.
1127 static struct inpcb *
1128 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1129 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1130 int lookupflags, struct ifnet *ifp)
1132 struct inpcbhead *head;
1133 struct inpcb *inp, *tmpinp;
1134 u_short fport = fport_arg, lport = lport_arg;
1136 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1137 ("%s: invalid lookup flags %d", __func__, lookupflags));
1139 INP_HASH_LOCK_ASSERT(pcbinfo);
1142 * First look for an exact match.
1145 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1146 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1147 CK_LIST_FOREACH(inp, head, inp_hash) {
1148 /* XXX inp locking */
1149 if ((inp->inp_vflag & INP_IPV6) == 0)
1151 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1152 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1153 inp->inp_fport == fport &&
1154 inp->inp_lport == lport) {
1156 * XXX We should be able to directly return
1157 * the inp here, without any checks.
1158 * Well unless both bound with SO_REUSEPORT?
1160 if (prison_flag(inp->inp_cred, PR_IP6))
1170 * Then look in lb group (for wildcard match).
1172 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1173 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1174 fport, lookupflags);
1180 * Then look for a wildcard match, if requested.
1182 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1183 struct inpcb *local_wild = NULL, *local_exact = NULL;
1184 struct inpcb *jail_wild = NULL;
1188 * Order of socket selection - we always prefer jails.
1189 * 1. jailed, non-wild.
1191 * 3. non-jailed, non-wild.
1192 * 4. non-jailed, wild.
1194 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1195 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1196 pcbinfo->ipi_hashmask)];
1197 CK_LIST_FOREACH(inp, head, inp_hash) {
1198 /* XXX inp locking */
1199 if ((inp->inp_vflag & INP_IPV6) == 0)
1202 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1203 inp->inp_lport != lport) {
1207 injail = prison_flag(inp->inp_cred, PR_IP6);
1209 if (prison_check_ip6(inp->inp_cred,
1213 if (local_exact != NULL)
1217 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1222 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1228 } /* LIST_FOREACH */
1230 if (jail_wild != NULL)
1232 if (local_exact != NULL)
1233 return (local_exact);
1234 if (local_wild != NULL)
1235 return (local_wild);
1236 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1245 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1246 * hash list lock, and will return the inpcb locked (i.e., requires
1247 * INPLOOKUP_LOCKPCB).
1249 static struct inpcb *
1250 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1251 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1256 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1257 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1259 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1261 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1265 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1267 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1272 panic("%s: locking bug", __func__);
1275 if (lookupflags & INPLOOKUP_WLOCKPCB)
1276 INP_WLOCK_ASSERT(inp);
1278 INP_RLOCK_ASSERT(inp);
1286 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1287 * from which a pre-calculated hash value may be extracted.
1289 * Possibly more of this logic should be in in6_pcbgroup.c.
1292 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1293 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1295 #if defined(PCBGROUP) && !defined(RSS)
1296 struct inpcbgroup *pcbgroup;
1299 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1300 ("%s: invalid lookup flags %d", __func__, lookupflags));
1301 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1302 ("%s: LOCKPCB not set", __func__));
1305 * When not using RSS, use connection groups in preference to the
1306 * reservation table when looking up 4-tuples. When using RSS, just
1307 * use the reservation table, due to the cost of the Toeplitz hash
1310 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1311 * we could be doing RSS with a non-Toeplitz hash that is affordable
1314 #if defined(PCBGROUP) && !defined(RSS)
1315 if (in_pcbgroup_enabled(pcbinfo)) {
1316 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1318 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1319 laddr, lport, lookupflags, ifp));
1322 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1327 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1328 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1329 struct ifnet *ifp, struct mbuf *m)
1332 struct inpcbgroup *pcbgroup;
1335 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1336 ("%s: invalid lookup flags %d", __func__, lookupflags));
1337 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1338 ("%s: LOCKPCB not set", __func__));
1342 * If we can use a hardware-generated hash to look up the connection
1343 * group, use that connection group to find the inpcb. Otherwise
1344 * fall back on a software hash -- or the reservation table if we're
1347 * XXXRW: As above, that policy belongs in the pcbgroup code.
1349 if (in_pcbgroup_enabled(pcbinfo) &&
1350 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1351 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1352 m->m_pkthdr.flowid);
1353 if (pcbgroup != NULL)
1354 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1355 fport, laddr, lport, lookupflags, ifp));
1357 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1359 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1360 laddr, lport, lookupflags, ifp));
1364 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1369 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1373 ip = mtod(m, struct ip6_hdr *);
1374 bzero(sin6, sizeof(*sin6));
1375 sin6->sin6_len = sizeof(*sin6);
1376 sin6->sin6_family = AF_INET6;
1377 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1379 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */