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>
101 #include <netinet/in.h>
102 #include <netinet/in_var.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/tcp_var.h>
105 #include <netinet/ip6.h>
106 #include <netinet/ip_var.h>
108 #include <netinet6/ip6_var.h>
109 #include <netinet6/nd6.h>
110 #include <netinet/in_pcb.h>
111 #include <netinet6/in6_pcb.h>
112 #include <netinet6/scope6_var.h>
114 static struct inpcb *in6_pcblookup_hash_locked(struct inpcbinfo *,
115 struct in6_addr *, u_int, struct in6_addr *, u_int, int, struct ifnet *);
118 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
121 struct socket *so = inp->inp_socket;
122 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
123 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
125 int error, lookupflags = 0;
126 int reuseport = (so->so_options & SO_REUSEPORT);
129 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
130 * so that we don't have to add to the (already messy) code below.
132 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
134 INP_WLOCK_ASSERT(inp);
135 INP_HASH_WLOCK_ASSERT(pcbinfo);
137 if (CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
138 return (EADDRNOTAVAIL);
139 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
141 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
142 lookupflags = INPLOOKUP_WILDCARD;
144 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
145 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
148 sin6 = (struct sockaddr_in6 *)nam;
149 if (nam->sa_len != sizeof(*sin6))
154 if (nam->sa_family != AF_INET6)
155 return (EAFNOSUPPORT);
157 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
160 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
161 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
164 lport = sin6->sin6_port;
165 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
167 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
168 * allow compepte duplication of binding if
169 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
170 * and a multicast address is bound on both
171 * new and duplicated sockets.
173 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
174 reuseport = SO_REUSEADDR|SO_REUSEPORT;
176 * XXX: How to deal with SO_REUSEPORT_LB here?
177 * Treat same as SO_REUSEPORT for now.
179 if ((so->so_options &
180 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
181 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
182 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
183 struct epoch_tracker et;
186 sin6->sin6_port = 0; /* yech... */
188 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
190 (inp->inp_flags & INP_BINDANY) == 0) {
192 return (EADDRNOTAVAIL);
196 * XXX: bind to an anycast address might accidentally
197 * cause sending a packet with anycast source address.
198 * We should allow to bind to a deprecated address, since
199 * the application dares to use it.
202 ((struct in6_ifaddr *)ifa)->ia6_flags &
203 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
205 return (EADDRNOTAVAIL);
214 if (ntohs(lport) <= V_ipport_reservedhigh &&
215 ntohs(lport) >= V_ipport_reservedlow &&
216 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
218 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
219 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
220 t = in6_pcblookup_local(pcbinfo,
221 &sin6->sin6_addr, lport,
222 INPLOOKUP_WILDCARD, cred);
224 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
225 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
226 (so->so_type != SOCK_STREAM ||
227 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
228 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
229 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
230 (t->inp_flags2 & INP_REUSEPORT) ||
231 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
232 (inp->inp_cred->cr_uid !=
233 t->inp_cred->cr_uid))
237 * If the socket is a BINDMULTI socket, then
238 * the credentials need to match and the
239 * original socket also has to have been bound
242 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
246 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
247 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
248 struct sockaddr_in sin;
250 in6_sin6_2_sin(&sin, sin6);
251 t = in_pcblookup_local(pcbinfo,
253 INPLOOKUP_WILDCARD, cred);
255 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
257 INP_TIMEWAIT) == 0) &&
258 (so->so_type != SOCK_STREAM ||
259 ntohl(t->inp_faddr.s_addr) ==
261 (inp->inp_cred->cr_uid !=
262 t->inp_cred->cr_uid))
265 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
270 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
271 lport, lookupflags, cred);
272 if (t && (t->inp_flags & INP_TIMEWAIT)) {
274 * XXXRW: If an incpb has had its timewait
275 * state recycled, we treat the address as
276 * being in use (for now). This is better
277 * than a panic, but not desirable.
281 ((reuseport & tw->tw_so_options) == 0 &&
282 (reuseport_lb & tw->tw_so_options) == 0))
284 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
285 (reuseport_lb & inp_so_options(t)) == 0) {
289 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
290 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
291 struct sockaddr_in sin;
293 in6_sin6_2_sin(&sin, sin6);
294 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
295 lport, lookupflags, cred);
296 if (t && t->inp_flags & INP_TIMEWAIT) {
300 if ((reuseport & tw->tw_so_options) == 0
301 && (reuseport_lb & tw->tw_so_options) == 0
302 && (ntohl(t->inp_laddr.s_addr) !=
303 INADDR_ANY || ((inp->inp_vflag &
305 (t->inp_vflag & INP_IPV6PROTO))))
308 (reuseport & inp_so_options(t)) == 0 &&
309 (reuseport_lb & inp_so_options(t)) == 0 &&
310 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
311 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
317 inp->in6p_laddr = sin6->sin6_addr;
320 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
321 /* Undo an address bind that may have occurred. */
322 inp->in6p_laddr = in6addr_any;
326 inp->inp_lport = lport;
327 if (in_pcbinshash(inp) != 0) {
328 inp->in6p_laddr = in6addr_any;
337 * Transform old in6_pcbconnect() into an inner subroutine for new
338 * in6_pcbconnect(): Do some validity-checking on the remote
339 * address (in mbuf 'nam') and then determine local host address
340 * (i.e., which interface) to use to access that remote host.
342 * This preserves definition of in6_pcbconnect(), while supporting a
343 * slightly different version for T/TCP. (This is more than
344 * a bit of a kludge, but cleaning up the internal interfaces would
345 * have forced minor changes in every protocol).
348 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
349 struct in6_addr *plocal_addr6)
351 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
353 int scope_ambiguous = 0;
354 struct in6_addr in6a;
356 INP_WLOCK_ASSERT(inp);
357 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
359 if (nam->sa_len != sizeof (*sin6))
361 if (sin6->sin6_family != AF_INET6)
362 return (EAFNOSUPPORT);
363 if (sin6->sin6_port == 0)
364 return (EADDRNOTAVAIL);
366 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
368 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
371 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
373 * If the destination address is UNSPECIFIED addr,
374 * use the loopback addr, e.g ::1.
376 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
377 sin6->sin6_addr = in6addr_loopback;
379 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
382 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
383 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
388 * Do not update this earlier, in case we return with an error.
390 * XXX: this in6_selectsrc_socket result might replace the bound local
391 * address with the address specified by setsockopt(IPV6_PKTINFO).
392 * Is it the intended behavior?
394 *plocal_addr6 = in6a;
397 * Don't do pcblookup call here; return interface in
399 * and exit to caller, that will do the lookup.
407 * Connect from a socket to a specified address.
408 * Both address and port must be specified in argument sin.
409 * If don't have a local address for this socket yet,
413 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
414 struct ucred *cred, struct mbuf *m, bool rehash)
416 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
417 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
418 struct in6_addr addr6;
421 INP_WLOCK_ASSERT(inp);
422 INP_HASH_WLOCK_ASSERT(pcbinfo);
425 * Call inner routine, to assign local interface address.
426 * in6_pcbladdr() may automatically fill in sin6_scope_id.
428 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
431 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
433 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
434 ? &addr6 : &inp->in6p_laddr,
435 inp->inp_lport, 0, NULL) != NULL) {
438 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
439 if (inp->inp_lport == 0) {
440 KASSERT(rehash == true,
441 ("Rehashing required for unbound inps"));
442 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
446 inp->in6p_laddr = addr6;
448 inp->in6p_faddr = sin6->sin6_addr;
449 inp->inp_fport = sin6->sin6_port;
450 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
451 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
452 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
454 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
457 in_pcbrehash_mbuf(inp, m);
459 in_pcbinshash_mbuf(inp, m);
466 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
469 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true));
473 in6_pcbdisconnect(struct inpcb *inp)
476 INP_WLOCK_ASSERT(inp);
477 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
479 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
481 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
482 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
487 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
489 struct sockaddr_in6 *sin6;
491 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
492 bzero(sin6, sizeof *sin6);
493 sin6->sin6_family = AF_INET6;
494 sin6->sin6_len = sizeof(*sin6);
495 sin6->sin6_port = port;
496 sin6->sin6_addr = *addr_p;
497 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
499 return (struct sockaddr *)sin6;
503 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
505 struct sockaddr_in sin;
506 struct sockaddr_in6 *sin6_p;
508 bzero(&sin, sizeof sin);
509 sin.sin_family = AF_INET;
510 sin.sin_len = sizeof(sin);
512 sin.sin_addr = *addr_p;
514 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
516 in6_sin_2_v4mapsin6(&sin, sin6_p);
518 return (struct sockaddr *)sin6_p;
522 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
525 struct in6_addr addr;
529 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
532 port = inp->inp_lport;
533 addr = inp->in6p_laddr;
536 *nam = in6_sockaddr(port, &addr);
541 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
544 struct in6_addr addr;
548 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
551 port = inp->inp_fport;
552 addr = inp->in6p_faddr;
555 *nam = in6_sockaddr(port, &addr);
560 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
566 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
569 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
570 error = in_getsockaddr(so, nam);
572 in6_sin_2_v4mapsin6_in_sock(nam);
576 /* scope issues will be handled in in6_getsockaddr(). */
577 error = in6_getsockaddr(so, nam);
584 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
590 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
593 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
594 error = in_getpeeraddr(so, nam);
596 in6_sin_2_v4mapsin6_in_sock(nam);
599 /* scope issues will be handled in in6_getpeeraddr(). */
600 error = in6_getpeeraddr(so, nam);
606 * Pass some notification to all connections of a protocol
607 * associated with address dst. The local address and/or port numbers
608 * may be specified to limit the search. The "usual action" will be
609 * taken, depending on the ctlinput cmd. The caller must filter any
610 * cmds that are uninteresting (e.g., no error in the map).
611 * Call the protocol specific routine (if any) to report
612 * any errors for each matching socket.
615 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
616 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
617 int cmd, void *cmdarg,
618 struct inpcb *(*notify)(struct inpcb *, int))
620 struct inpcb *inp, *inp_temp;
621 struct sockaddr_in6 sa6_src, *sa6_dst;
622 u_short fport = fport_arg, lport = lport_arg;
626 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
629 sa6_dst = (struct sockaddr_in6 *)dst;
630 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
634 * note that src can be NULL when we get notify by local fragmentation.
636 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
637 flowinfo = sa6_src.sin6_flowinfo;
640 * Redirects go to all references to the destination,
641 * and use in6_rtchange to invalidate the route cache.
642 * Dead host indications: also use in6_rtchange to invalidate
643 * the cache, and deliver the error to all the sockets.
644 * Otherwise, if we have knowledge of the local port and address,
645 * deliver only to that socket.
647 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
650 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
652 if (cmd != PRC_HOSTDEAD)
653 notify = in6_rtchange;
655 errno = inet6ctlerrmap[cmd];
656 INP_INFO_WLOCK(pcbinfo);
657 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
659 if ((inp->inp_vflag & INP_IPV6) == 0) {
665 * If the error designates a new path MTU for a destination
666 * and the application (associated with this socket) wanted to
667 * know the value, notify.
668 * XXX: should we avoid to notify the value to TCP sockets?
670 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
671 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
672 *(u_int32_t *)cmdarg);
675 * Detect if we should notify the error. If no source and
676 * destination ports are specifed, but non-zero flowinfo and
677 * local address match, notify the error. This is the case
678 * when the error is delivered with an encrypted buffer
679 * by ESP. Otherwise, just compare addresses and ports
682 if (lport == 0 && fport == 0 && flowinfo &&
683 inp->inp_socket != NULL &&
684 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
685 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
687 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
688 &sa6_dst->sin6_addr) ||
689 inp->inp_socket == 0 ||
690 (lport && inp->inp_lport != lport) ||
691 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
692 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
693 &sa6_src.sin6_addr)) ||
694 (fport && inp->inp_fport != fport)) {
701 if ((*notify)(inp, errno))
706 INP_INFO_WUNLOCK(pcbinfo);
710 * Lookup a PCB based on the local address and port. Caller must hold the
711 * hash lock. No inpcb locks or references are acquired.
714 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
715 u_short lport, int lookupflags, struct ucred *cred)
718 int matchwild = 3, wildcard;
720 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
721 ("%s: invalid lookup flags %d", __func__, lookupflags));
723 INP_HASH_LOCK_ASSERT(pcbinfo);
725 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
726 struct inpcbhead *head;
728 * Look for an unconnected (wildcard foreign addr) PCB that
729 * matches the local address and port we're looking for.
731 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
732 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
733 pcbinfo->ipi_hashmask)];
734 CK_LIST_FOREACH(inp, head, inp_hash) {
735 /* XXX inp locking */
736 if ((inp->inp_vflag & INP_IPV6) == 0)
738 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
739 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
740 inp->inp_lport == lport) {
743 prison_equal_ip6(cred->cr_prison,
744 inp->inp_cred->cr_prison))
753 struct inpcbporthead *porthash;
754 struct inpcbport *phd;
755 struct inpcb *match = NULL;
757 * Best fit PCB lookup.
759 * First see if this local port is in use by looking on the
762 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
763 pcbinfo->ipi_porthashmask)];
764 CK_LIST_FOREACH(phd, porthash, phd_hash) {
765 if (phd->phd_port == lport)
770 * Port is in use by one or more PCBs. Look for best
773 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
776 !prison_equal_ip6(cred->cr_prison,
777 inp->inp_cred->cr_prison))
779 /* XXX inp locking */
780 if ((inp->inp_vflag & INP_IPV6) == 0)
782 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
784 if (!IN6_IS_ADDR_UNSPECIFIED(
786 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
788 else if (!IN6_ARE_ADDR_EQUAL(
789 &inp->in6p_laddr, laddr))
792 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
795 if (wildcard < matchwild) {
797 matchwild = wildcard;
808 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
811 struct in6_multi *inm;
812 struct in6_mfilter *imf;
813 struct ip6_moptions *im6o;
815 INP_INFO_WLOCK(pcbinfo);
816 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
818 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
822 im6o = inp->in6p_moptions;
823 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) {
825 * Unselect the outgoing ifp for multicast if it
828 if (im6o->im6o_multicast_ifp == ifp)
829 im6o->im6o_multicast_ifp = NULL;
831 * Drop multicast group membership if we joined
832 * through the interface being detached.
835 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
836 if ((inm = imf->im6f_in6m) == NULL)
838 if (inm->in6m_ifp != ifp)
840 ip6_mfilter_remove(&im6o->im6o_head, imf);
841 IN6_MULTI_LOCK_ASSERT();
842 in6_leavegroup_locked(inm, NULL);
843 ip6_mfilter_free(imf);
849 INP_INFO_WUNLOCK(pcbinfo);
853 * Check for alternatives when higher level complains
854 * about service problems. For now, invalidate cached
855 * routing information. If the route was created dynamically
856 * (by a redirect), time to try a default gateway again.
859 in6_losing(struct inpcb *inp)
862 RO_INVALIDATE_CACHE(&inp->inp_route6);
866 * After a routing change, flush old routing
867 * and allocate a (hopefully) better one.
870 in6_rtchange(struct inpcb *inp, int errno __unused)
873 RO_INVALIDATE_CACHE(&inp->inp_route6);
877 static struct inpcb *
878 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
879 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
880 uint16_t fport, int lookupflags)
882 struct inpcb *local_wild;
883 const struct inpcblbgrouphead *hdr;
884 struct inpcblbgroup *grp;
887 INP_HASH_LOCK_ASSERT(pcbinfo);
889 hdr = &pcbinfo->ipi_lbgrouphashbase[
890 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
893 * Order of socket selection:
895 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
898 * - Load balanced group does not contain jailed sockets.
899 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
902 CK_LIST_FOREACH(grp, hdr, il_list) {
904 if (!(grp->il_vflag & INP_IPV6))
907 if (grp->il_lport != lport)
910 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
911 fport) % grp->il_inpcnt;
912 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr))
913 return (grp->il_inp[idx]);
914 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
915 (lookupflags & INPLOOKUP_WILDCARD) != 0)
916 local_wild = grp->il_inp[idx];
923 * Lookup PCB in hash list, using pcbgroup tables.
925 static struct inpcb *
926 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
927 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
928 u_int lport_arg, int lookupflags, struct ifnet *ifp)
930 struct inpcbhead *head;
931 struct inpcb *inp, *tmpinp;
932 u_short fport = fport_arg, lport = lport_arg;
936 * First look for an exact match.
939 INP_GROUP_LOCK(pcbgroup);
940 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
941 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
942 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
943 /* XXX inp locking */
944 if ((inp->inp_vflag & INP_IPV6) == 0)
946 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
947 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
948 inp->inp_fport == fport &&
949 inp->inp_lport == lport) {
951 * XXX We should be able to directly return
952 * the inp here, without any checks.
953 * Well unless both bound with SO_REUSEPORT?
955 if (prison_flag(inp->inp_cred, PR_IP6))
961 if (tmpinp != NULL) {
967 * Then look for a wildcard match in the pcbgroup.
969 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
970 struct inpcb *local_wild = NULL, *local_exact = NULL;
971 struct inpcb *jail_wild = NULL;
975 * Order of socket selection - we always prefer jails.
976 * 1. jailed, non-wild.
978 * 3. non-jailed, non-wild.
979 * 4. non-jailed, wild.
981 head = &pcbgroup->ipg_hashbase[
982 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
983 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
984 /* XXX inp locking */
985 if ((inp->inp_vflag & INP_IPV6) == 0)
988 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
989 inp->inp_lport != lport) {
993 injail = prison_flag(inp->inp_cred, PR_IP6);
995 if (prison_check_ip6(inp->inp_cred,
999 if (local_exact != NULL)
1003 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1008 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1014 } /* LIST_FOREACH */
1028 * Then look for a wildcard match, if requested.
1030 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1031 struct inpcb *local_wild = NULL, *local_exact = NULL;
1032 struct inpcb *jail_wild = NULL;
1036 * Order of socket selection - we always prefer jails.
1037 * 1. jailed, non-wild.
1039 * 3. non-jailed, non-wild.
1040 * 4. non-jailed, wild.
1042 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1043 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1044 pcbinfo->ipi_wildmask)];
1045 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1046 /* XXX inp locking */
1047 if ((inp->inp_vflag & INP_IPV6) == 0)
1050 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1051 inp->inp_lport != lport) {
1055 injail = prison_flag(inp->inp_cred, PR_IP6);
1057 if (prison_check_ip6(inp->inp_cred,
1061 if (local_exact != NULL)
1065 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1070 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1076 } /* LIST_FOREACH */
1087 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1088 INP_GROUP_UNLOCK(pcbgroup);
1092 if (lookupflags & INPLOOKUP_WLOCKPCB)
1093 locked = INP_TRY_WLOCK(inp);
1094 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1095 locked = INP_TRY_RLOCK(inp);
1097 panic("%s: locking buf", __func__);
1100 INP_GROUP_UNLOCK(pcbgroup);
1102 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1104 if (in_pcbrele_wlocked(inp))
1108 if (in_pcbrele_rlocked(inp))
1113 if (lookupflags & INPLOOKUP_WLOCKPCB)
1114 INP_WLOCK_ASSERT(inp);
1116 INP_RLOCK_ASSERT(inp);
1120 #endif /* PCBGROUP */
1123 * Lookup PCB in hash list.
1125 static struct inpcb *
1126 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1127 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1128 int lookupflags, struct ifnet *ifp)
1130 struct inpcbhead *head;
1131 struct inpcb *inp, *tmpinp;
1132 u_short fport = fport_arg, lport = lport_arg;
1134 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1135 ("%s: invalid lookup flags %d", __func__, lookupflags));
1137 INP_HASH_LOCK_ASSERT(pcbinfo);
1140 * First look for an exact match.
1143 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1144 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1145 CK_LIST_FOREACH(inp, head, inp_hash) {
1146 /* XXX inp locking */
1147 if ((inp->inp_vflag & INP_IPV6) == 0)
1149 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1150 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1151 inp->inp_fport == fport &&
1152 inp->inp_lport == lport) {
1154 * XXX We should be able to directly return
1155 * the inp here, without any checks.
1156 * Well unless both bound with SO_REUSEPORT?
1158 if (prison_flag(inp->inp_cred, PR_IP6))
1168 * Then look in lb group (for wildcard match).
1170 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1171 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1172 fport, lookupflags);
1178 * Then look for a wildcard match, if requested.
1180 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1181 struct inpcb *local_wild = NULL, *local_exact = NULL;
1182 struct inpcb *jail_wild = NULL;
1186 * Order of socket selection - we always prefer jails.
1187 * 1. jailed, non-wild.
1189 * 3. non-jailed, non-wild.
1190 * 4. non-jailed, wild.
1192 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1193 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1194 pcbinfo->ipi_hashmask)];
1195 CK_LIST_FOREACH(inp, head, inp_hash) {
1196 /* XXX inp locking */
1197 if ((inp->inp_vflag & INP_IPV6) == 0)
1200 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1201 inp->inp_lport != lport) {
1205 injail = prison_flag(inp->inp_cred, PR_IP6);
1207 if (prison_check_ip6(inp->inp_cred,
1211 if (local_exact != NULL)
1215 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1220 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1226 } /* LIST_FOREACH */
1228 if (jail_wild != NULL)
1230 if (local_exact != NULL)
1231 return (local_exact);
1232 if (local_wild != NULL)
1233 return (local_wild);
1234 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1243 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1244 * hash list lock, and will return the inpcb locked (i.e., requires
1245 * INPLOOKUP_LOCKPCB).
1247 static struct inpcb *
1248 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1249 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1254 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1255 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1257 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1259 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1263 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1265 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1270 panic("%s: locking bug", __func__);
1273 if (lookupflags & INPLOOKUP_WLOCKPCB)
1274 INP_WLOCK_ASSERT(inp);
1276 INP_RLOCK_ASSERT(inp);
1284 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1285 * from which a pre-calculated hash value may be extracted.
1287 * Possibly more of this logic should be in in6_pcbgroup.c.
1290 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1291 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1293 #if defined(PCBGROUP) && !defined(RSS)
1294 struct inpcbgroup *pcbgroup;
1297 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1298 ("%s: invalid lookup flags %d", __func__, lookupflags));
1299 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1300 ("%s: LOCKPCB not set", __func__));
1303 * When not using RSS, use connection groups in preference to the
1304 * reservation table when looking up 4-tuples. When using RSS, just
1305 * use the reservation table, due to the cost of the Toeplitz hash
1308 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1309 * we could be doing RSS with a non-Toeplitz hash that is affordable
1312 #if defined(PCBGROUP) && !defined(RSS)
1313 if (in_pcbgroup_enabled(pcbinfo)) {
1314 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1316 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1317 laddr, lport, lookupflags, ifp));
1320 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1325 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1326 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1327 struct ifnet *ifp, struct mbuf *m)
1330 struct inpcbgroup *pcbgroup;
1333 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1334 ("%s: invalid lookup flags %d", __func__, lookupflags));
1335 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1336 ("%s: LOCKPCB not set", __func__));
1340 * If we can use a hardware-generated hash to look up the connection
1341 * group, use that connection group to find the inpcb. Otherwise
1342 * fall back on a software hash -- or the reservation table if we're
1345 * XXXRW: As above, that policy belongs in the pcbgroup code.
1347 if (in_pcbgroup_enabled(pcbinfo) &&
1348 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1349 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1350 m->m_pkthdr.flowid);
1351 if (pcbgroup != NULL)
1352 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1353 fport, laddr, lport, lookupflags, ifp));
1355 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1357 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1358 laddr, lport, lookupflags, ifp));
1362 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1367 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1371 ip = mtod(m, struct ip6_hdr *);
1372 bzero(sin6, sizeof(*sin6));
1373 sin6->sin6_len = sizeof(*sin6);
1374 sin6->sin6_family = AF_INET6;
1375 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1377 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */