xref: /freebsd/sys/netinet6/in6_pcb.c (revision 042fb58d)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
682cd038dSYoshinobu Inoue  * All rights reserved.
782cd038dSYoshinobu Inoue  *
8fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
9fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
10fa046d87SRobert Watson  *
1182cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
1282cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
1382cd038dSYoshinobu Inoue  * are met:
1482cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1582cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1682cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1782cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1882cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1982cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
2082cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
2182cd038dSYoshinobu Inoue  *    without specific prior written permission.
2282cd038dSYoshinobu Inoue  *
2382cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2482cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2582cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2682cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2782cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2882cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2982cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3082cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3182cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3282cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3382cd038dSYoshinobu Inoue  * SUCH DAMAGE.
3482cd038dSYoshinobu Inoue  *
35b48287a3SDavid E. O'Brien  *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
3682cd038dSYoshinobu Inoue  */
3782cd038dSYoshinobu Inoue 
38caf43b02SWarner Losh /*-
3982cd038dSYoshinobu Inoue  * Copyright (c) 1982, 1986, 1991, 1993
4082cd038dSYoshinobu Inoue  *	The Regents of the University of California.  All rights reserved.
4182cd038dSYoshinobu Inoue  *
4282cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
4382cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
4482cd038dSYoshinobu Inoue  * are met:
4582cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
4682cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
4782cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
4882cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
4982cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
50fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
5182cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
5282cd038dSYoshinobu Inoue  *    without specific prior written permission.
5382cd038dSYoshinobu Inoue  *
5482cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5582cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5682cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5782cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5882cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5982cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6082cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6182cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6282cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6382cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6482cd038dSYoshinobu Inoue  * SUCH DAMAGE.
6582cd038dSYoshinobu Inoue  */
6682cd038dSYoshinobu Inoue 
67b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
6833841545SHajimu UMEMOTO #include "opt_inet.h"
6933841545SHajimu UMEMOTO #include "opt_inet6.h"
706a800098SYoshinobu Inoue #include "opt_ipsec.h"
710c325f53SAlexander V. Chernikov #include "opt_route.h"
727527624eSRobert Watson #include "opt_rss.h"
7382cd038dSYoshinobu Inoue 
74a0577692SGleb Smirnoff #include <sys/hash.h>
7582cd038dSYoshinobu Inoue #include <sys/param.h>
7682cd038dSYoshinobu Inoue #include <sys/systm.h>
7782cd038dSYoshinobu Inoue #include <sys/malloc.h>
7882cd038dSYoshinobu Inoue #include <sys/mbuf.h>
79fb59c426SYoshinobu Inoue #include <sys/domain.h>
80fdb987beSMark Johnston #include <sys/proc.h>
8182cd038dSYoshinobu Inoue #include <sys/protosw.h>
82fdb987beSMark Johnston #include <sys/smr.h>
8382cd038dSYoshinobu Inoue #include <sys/socket.h>
8482cd038dSYoshinobu Inoue #include <sys/socketvar.h>
8582cd038dSYoshinobu Inoue #include <sys/sockio.h>
8682cd038dSYoshinobu Inoue #include <sys/errno.h>
8782cd038dSYoshinobu Inoue #include <sys/time.h>
88acd3428bSRobert Watson #include <sys/priv.h>
8982cd038dSYoshinobu Inoue #include <sys/proc.h>
9082cd038dSYoshinobu Inoue #include <sys/jail.h>
9182cd038dSYoshinobu Inoue 
9294540027SJeff Roberson #include <vm/uma.h>
9382cd038dSYoshinobu Inoue 
9482cd038dSYoshinobu Inoue #include <net/if.h>
9576039bc8SGleb Smirnoff #include <net/if_var.h>
966d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h>
9782cd038dSYoshinobu Inoue #include <net/if_types.h>
9882cd038dSYoshinobu Inoue #include <net/route.h>
99983066f0SAlexander V. Chernikov #include <net/route/nhop.h>
10082cd038dSYoshinobu Inoue 
10182cd038dSYoshinobu Inoue #include <netinet/in.h>
10282cd038dSYoshinobu Inoue #include <netinet/in_var.h>
10382cd038dSYoshinobu Inoue #include <netinet/in_systm.h>
104686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
105fb59c426SYoshinobu Inoue #include <netinet/ip_var.h>
106ac957cd2SJulian Elischer 
10782cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
10882cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
10982cd038dSYoshinobu Inoue #include <netinet/in_pcb.h>
1100f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h>
11182cd038dSYoshinobu Inoue #include <netinet6/in6_pcb.h>
112983066f0SAlexander V. Chernikov #include <netinet6/in6_fib.h>
113a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
11482cd038dSYoshinobu Inoue 
11582cd038dSYoshinobu Inoue int
in6_pcbsetport(struct in6_addr * laddr,struct inpcb * inp,struct ucred * cred)116147f018aSGleb Smirnoff in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred)
117147f018aSGleb Smirnoff {
118147f018aSGleb Smirnoff 	struct socket *so = inp->inp_socket;
119147f018aSGleb Smirnoff 	u_int16_t lport = 0;
120147f018aSGleb Smirnoff 	int error, lookupflags = 0;
121147f018aSGleb Smirnoff #ifdef INVARIANTS
122147f018aSGleb Smirnoff 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
123147f018aSGleb Smirnoff #endif
124147f018aSGleb Smirnoff 
125147f018aSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
126147f018aSGleb Smirnoff 	INP_HASH_WLOCK_ASSERT(pcbinfo);
127147f018aSGleb Smirnoff 
128147f018aSGleb Smirnoff 	error = prison_local_ip6(cred, laddr,
129147f018aSGleb Smirnoff 	    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0));
130147f018aSGleb Smirnoff 	if (error)
131147f018aSGleb Smirnoff 		return(error);
132147f018aSGleb Smirnoff 
133147f018aSGleb Smirnoff 	/* XXX: this is redundant when called from in6_pcbbind */
134147f018aSGleb Smirnoff 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
135147f018aSGleb Smirnoff 		lookupflags = INPLOOKUP_WILDCARD;
136147f018aSGleb Smirnoff 
137147f018aSGleb Smirnoff 	inp->inp_flags |= INP_ANONPORT;
138147f018aSGleb Smirnoff 
139147f018aSGleb Smirnoff 	error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags);
140147f018aSGleb Smirnoff 	if (error != 0)
141147f018aSGleb Smirnoff 		return (error);
142147f018aSGleb Smirnoff 
143147f018aSGleb Smirnoff 	inp->inp_lport = lport;
144147f018aSGleb Smirnoff 	if (in_pcbinshash(inp) != 0) {
145147f018aSGleb Smirnoff 		inp->in6p_laddr = in6addr_any;
146147f018aSGleb Smirnoff 		inp->inp_lport = 0;
147147f018aSGleb Smirnoff 		return (EAGAIN);
148147f018aSGleb Smirnoff 	}
149147f018aSGleb Smirnoff 
150147f018aSGleb Smirnoff 	return (0);
151147f018aSGleb Smirnoff }
152147f018aSGleb Smirnoff 
153147f018aSGleb Smirnoff int
in6_pcbbind(struct inpcb * inp,struct sockaddr_in6 * sin6,struct ucred * cred)15496871af0SGleb Smirnoff in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred)
15582cd038dSYoshinobu Inoue {
15682cd038dSYoshinobu Inoue 	struct socket *so = inp->inp_socket;
15782cd038dSYoshinobu Inoue 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
15882cd038dSYoshinobu Inoue 	u_short	lport = 0;
15968e0d7e0SRobert Watson 	int error, lookupflags = 0;
16068e0d7e0SRobert Watson 	int reuseport = (so->so_options & SO_REUSEPORT);
16182cd038dSYoshinobu Inoue 
1621a43cff9SSean Bruno 	/*
1631a43cff9SSean Bruno 	 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
1641a43cff9SSean Bruno 	 * so that we don't have to add to the (already messy) code below.
1651a43cff9SSean Bruno 	 */
1661a43cff9SSean Bruno 	int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
1671a43cff9SSean Bruno 
1688501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
169fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
17007385abdSRobert Watson 
17182cd038dSYoshinobu Inoue 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
17282cd038dSYoshinobu Inoue 		return (EINVAL);
1731a43cff9SSean Bruno 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
17468e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
17596871af0SGleb Smirnoff 	if (sin6 == NULL) {
1767c2f3cb9SJamie Gritton 		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
1777c2f3cb9SJamie Gritton 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
1787c2f3cb9SJamie Gritton 			return (error);
1797c2f3cb9SJamie Gritton 	} else {
180f161d294SMark Johnston 		KASSERT(sin6->sin6_family == AF_INET6,
181f161d294SMark Johnston 		    ("%s: invalid address family for %p", __func__, sin6));
182f161d294SMark Johnston 		KASSERT(sin6->sin6_len == sizeof(*sin6),
183f161d294SMark Johnston 		    ("%s: invalid address length for %p", __func__, sin6));
18482cd038dSYoshinobu Inoue 
185603724d3SBjoern A. Zeeb 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
186a1f7e5f8SHajimu UMEMOTO 			return(error);
18782cd038dSYoshinobu Inoue 
188b89e82ddSJamie Gritton 		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
189b89e82ddSJamie Gritton 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
190b89e82ddSJamie Gritton 			return (error);
191413628a7SBjoern A. Zeeb 
19282cd038dSYoshinobu Inoue 		lport = sin6->sin6_port;
19382cd038dSYoshinobu Inoue 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
19482cd038dSYoshinobu Inoue 			/*
19582cd038dSYoshinobu Inoue 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
19682cd038dSYoshinobu Inoue 			 * allow compepte duplication of binding if
19782cd038dSYoshinobu Inoue 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
19882cd038dSYoshinobu Inoue 			 * and a multicast address is bound on both
19982cd038dSYoshinobu Inoue 			 * new and duplicated sockets.
20082cd038dSYoshinobu Inoue 			 */
201f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
20282cd038dSYoshinobu Inoue 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
2031a43cff9SSean Bruno 			/*
2041a43cff9SSean Bruno 			 * XXX: How to deal with SO_REUSEPORT_LB here?
2051a43cff9SSean Bruno 			 * Treat same as SO_REUSEPORT for now.
2061a43cff9SSean Bruno 			 */
2071a43cff9SSean Bruno 			if ((so->so_options &
2081a43cff9SSean Bruno 			    (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
2091a43cff9SSean Bruno 				reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
21082cd038dSYoshinobu Inoue 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211a68cc388SGleb Smirnoff 			struct epoch_tracker et;
2128c0fec80SRobert Watson 			struct ifaddr *ifa;
21382cd038dSYoshinobu Inoue 
21482cd038dSYoshinobu Inoue 			sin6->sin6_port = 0;		/* yech... */
215a68cc388SGleb Smirnoff 			NET_EPOCH_ENTER(et);
2168c0fec80SRobert Watson 			if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
2178c0fec80SRobert Watson 			    NULL &&
218f44270e7SPawel Jakub Dawidek 			    (inp->inp_flags & INP_BINDANY) == 0) {
219a68cc388SGleb Smirnoff 				NET_EPOCH_EXIT(et);
22082cd038dSYoshinobu Inoue 				return (EADDRNOTAVAIL);
221f44270e7SPawel Jakub Dawidek 			}
22282cd038dSYoshinobu Inoue 
22382cd038dSYoshinobu Inoue 			/*
22482cd038dSYoshinobu Inoue 			 * XXX: bind to an anycast address might accidentally
22582cd038dSYoshinobu Inoue 			 * cause sending a packet with anycast source address.
22633841545SHajimu UMEMOTO 			 * We should allow to bind to a deprecated address, since
2277efe5d92SHajimu UMEMOTO 			 * the application dares to use it.
22882cd038dSYoshinobu Inoue 			 */
2298c0fec80SRobert Watson 			if (ifa != NULL &&
2308c0fec80SRobert Watson 			    ((struct in6_ifaddr *)ifa)->ia6_flags &
23133841545SHajimu UMEMOTO 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
232a68cc388SGleb Smirnoff 				NET_EPOCH_EXIT(et);
23382cd038dSYoshinobu Inoue 				return (EADDRNOTAVAIL);
23482cd038dSYoshinobu Inoue 			}
235a68cc388SGleb Smirnoff 			NET_EPOCH_EXIT(et);
23682cd038dSYoshinobu Inoue 		}
23782cd038dSYoshinobu Inoue 		if (lport) {
23882cd038dSYoshinobu Inoue 			struct inpcb *t;
23982cd038dSYoshinobu Inoue 
24082cd038dSYoshinobu Inoue 			/* GROSS */
241603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
242603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
243cc426dd3SMateusz Guzik 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
24482cd038dSYoshinobu Inoue 				return (EACCES);
245835d4b89SPawel Jakub Dawidek 			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
246cc426dd3SMateusz Guzik 			    priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
247fb59c426SYoshinobu Inoue 				t = in6_pcblookup_local(pcbinfo,
24882cd038dSYoshinobu Inoue 				    &sin6->sin6_addr, lport,
249078b7042SBjoern A. Zeeb 				    INPLOOKUP_WILDCARD, cred);
2503aff4ccdSMark Johnston 				if (t != NULL &&
251f66145c6SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
252f66145c6SYaroslav Tykhiy 				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
2534cc20ab1SSeigo Tanimura 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
25482cd038dSYoshinobu Inoue 				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
255e3ba0d6aSGleb Smirnoff 				     (t->inp_socket->so_options & SO_REUSEPORT) ||
256e3ba0d6aSGleb Smirnoff 				     (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) &&
257fc06cd42SMikolaj Golub 				    (inp->inp_cred->cr_uid !=
25886d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
25982cd038dSYoshinobu Inoue 					return (EADDRINUSE);
260c7c0d948SAdrian Chadd 
26167107f45SBjoern A. Zeeb #ifdef INET
26233841545SHajimu UMEMOTO 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
263fb59c426SYoshinobu Inoue 				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
264fb59c426SYoshinobu Inoue 					struct sockaddr_in sin;
265fb59c426SYoshinobu Inoue 
266fb59c426SYoshinobu Inoue 					in6_sin6_2_sin(&sin, sin6);
267fb59c426SYoshinobu Inoue 					t = in_pcblookup_local(pcbinfo,
268fb59c426SYoshinobu Inoue 					    sin.sin_addr, lport,
269078b7042SBjoern A. Zeeb 					    INPLOOKUP_WILDCARD, cred);
2703aff4ccdSMark Johnston 					if (t != NULL &&
271f66145c6SYaroslav Tykhiy 					    (so->so_type != SOCK_STREAM ||
272f66145c6SYaroslav Tykhiy 					     ntohl(t->inp_faddr.s_addr) ==
273f66145c6SYaroslav Tykhiy 					      INADDR_ANY) &&
27486d02c5cSBjoern A. Zeeb 					    (inp->inp_cred->cr_uid !=
27586d02c5cSBjoern A. Zeeb 					     t->inp_cred->cr_uid))
276fb59c426SYoshinobu Inoue 						return (EADDRINUSE);
277fb59c426SYoshinobu Inoue 				}
27867107f45SBjoern A. Zeeb #endif
27982cd038dSYoshinobu Inoue 			}
28082cd038dSYoshinobu Inoue 			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
28168e0d7e0SRobert Watson 			    lport, lookupflags, cred);
282e3ba0d6aSGleb Smirnoff 			if (t && (reuseport & t->inp_socket->so_options) == 0 &&
283e3ba0d6aSGleb Smirnoff 			    (reuseport_lb & t->inp_socket->so_options) == 0) {
28429381b36SMikolaj Golub 				return (EADDRINUSE);
28529381b36SMikolaj Golub 			}
28667107f45SBjoern A. Zeeb #ifdef INET
28733841545SHajimu UMEMOTO 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
288fb59c426SYoshinobu Inoue 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
289fb59c426SYoshinobu Inoue 				struct sockaddr_in sin;
290fb59c426SYoshinobu Inoue 
291fb59c426SYoshinobu Inoue 				in6_sin6_2_sin(&sin, sin6);
292fb59c426SYoshinobu Inoue 				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
29368e0d7e0SRobert Watson 				   lport, lookupflags, cred);
2940d744519SGleb Smirnoff 				if (t &&
295e3ba0d6aSGleb Smirnoff 				    (reuseport & t->inp_socket->so_options) == 0 &&
296e3ba0d6aSGleb Smirnoff 				    (reuseport_lb & t->inp_socket->so_options) == 0 &&
297fc06cd42SMikolaj Golub 				    (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
2981a43cff9SSean Bruno 				        (t->inp_vflag & INP_IPV6PROTO) != 0)) {
299fb59c426SYoshinobu Inoue 					return (EADDRINUSE);
300fb59c426SYoshinobu Inoue 				}
3011a43cff9SSean Bruno 			}
30267107f45SBjoern A. Zeeb #endif
30382cd038dSYoshinobu Inoue 		}
30482cd038dSYoshinobu Inoue 		inp->in6p_laddr = sin6->sin6_addr;
30582cd038dSYoshinobu Inoue 	}
30682cd038dSYoshinobu Inoue 	if (lport == 0) {
3074616026fSErmal Luçi 		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
3084a2b2562SBjoern A. Zeeb 			/* Undo an address bind that may have occurred. */
3094a2b2562SBjoern A. Zeeb 			inp->in6p_laddr = in6addr_any;
310b89e82ddSJamie Gritton 			return (error);
3114a2b2562SBjoern A. Zeeb 		}
31255fd3bafSBjoern A. Zeeb 	} else {
31382cd038dSYoshinobu Inoue 		inp->inp_lport = lport;
31482cd038dSYoshinobu Inoue 		if (in_pcbinshash(inp) != 0) {
31582cd038dSYoshinobu Inoue 			inp->in6p_laddr = in6addr_any;
31682cd038dSYoshinobu Inoue 			inp->inp_lport = 0;
31782cd038dSYoshinobu Inoue 			return (EAGAIN);
31882cd038dSYoshinobu Inoue 		}
319686cdd19SJun-ichiro itojun Hagino 	}
32082cd038dSYoshinobu Inoue 	return (0);
32182cd038dSYoshinobu Inoue }
32282cd038dSYoshinobu Inoue 
32382cd038dSYoshinobu Inoue /*
32482cd038dSYoshinobu Inoue  *   Transform old in6_pcbconnect() into an inner subroutine for new
32582cd038dSYoshinobu Inoue  *   in6_pcbconnect(): Do some validity-checking on the remote
32682cd038dSYoshinobu Inoue  *   address (in mbuf 'nam') and then determine local host address
32782cd038dSYoshinobu Inoue  *   (i.e., which interface) to use to access that remote host.
32882cd038dSYoshinobu Inoue  *
32982cd038dSYoshinobu Inoue  *   This preserves definition of in6_pcbconnect(), while supporting a
33082cd038dSYoshinobu Inoue  *   slightly different version for T/TCP.  (This is more than
33182cd038dSYoshinobu Inoue  *   a bit of a kludge, but cleaning up the internal interfaces would
33282cd038dSYoshinobu Inoue  *   have forced minor changes in every protocol).
33382cd038dSYoshinobu Inoue  */
334a7e201bbSAndrey V. Elsukov static int
in6_pcbladdr(struct inpcb * inp,struct sockaddr_in6 * sin6,struct in6_addr * plocal_addr6,bool sas_required)335f161d294SMark Johnston in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
3360bf5377bSAndrey V. Elsukov     struct in6_addr *plocal_addr6, bool sas_required)
33782cd038dSYoshinobu Inoue {
33882cd038dSYoshinobu Inoue 	int error = 0;
339a1f7e5f8SHajimu UMEMOTO 	int scope_ambiguous = 0;
34088d166bfSBjoern A. Zeeb 	struct in6_addr in6a;
34182cd038dSYoshinobu Inoue 
3423d76be28SGleb Smirnoff 	NET_EPOCH_ASSERT();
3438501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
344fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);	/* XXXRW: why? */
3458deea4a8SRobert Watson 
34682cd038dSYoshinobu Inoue 	if (sin6->sin6_port == 0)
34782cd038dSYoshinobu Inoue 		return (EADDRNOTAVAIL);
34882cd038dSYoshinobu Inoue 
349603724d3SBjoern A. Zeeb 	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
350a1f7e5f8SHajimu UMEMOTO 		scope_ambiguous = 1;
351603724d3SBjoern A. Zeeb 	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
352a1f7e5f8SHajimu UMEMOTO 		return(error);
353a1f7e5f8SHajimu UMEMOTO 
354d7c5a620SMatt Macy 	if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
35582cd038dSYoshinobu Inoue 		/*
35682cd038dSYoshinobu Inoue 		 * If the destination address is UNSPECIFIED addr,
35782cd038dSYoshinobu Inoue 		 * use the loopback addr, e.g ::1.
35882cd038dSYoshinobu Inoue 		 */
35982cd038dSYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
36082cd038dSYoshinobu Inoue 			sin6->sin6_addr = in6addr_loopback;
36182cd038dSYoshinobu Inoue 	}
362b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
363b89e82ddSJamie Gritton 		return (error);
364a1f7e5f8SHajimu UMEMOTO 
3650bf5377bSAndrey V. Elsukov 	if (sas_required) {
366601c0b8bSAlexander V. Chernikov 		error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
367601c0b8bSAlexander V. Chernikov 		    inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
36888d166bfSBjoern A. Zeeb 		if (error)
36988d166bfSBjoern A. Zeeb 			return (error);
3700bf5377bSAndrey V. Elsukov 	} else {
3710bf5377bSAndrey V. Elsukov 		/*
3720bf5377bSAndrey V. Elsukov 		 * Source address selection isn't required when syncache
3730bf5377bSAndrey V. Elsukov 		 * has already established connection and both source and
3740bf5377bSAndrey V. Elsukov 		 * destination addresses was chosen.
3750bf5377bSAndrey V. Elsukov 		 *
3760bf5377bSAndrey V. Elsukov 		 * This also includes the case when fwd_tag was used to
3770bf5377bSAndrey V. Elsukov 		 * select source address in tcp_input().
3780bf5377bSAndrey V. Elsukov 		 */
3790bf5377bSAndrey V. Elsukov 		in6a = inp->in6p_laddr;
3800bf5377bSAndrey V. Elsukov 	}
3810bf5377bSAndrey V. Elsukov 
382713264f6SMark Johnston 	if (IN6_IS_ADDR_UNSPECIFIED(&in6a))
383713264f6SMark Johnston 		return (EHOSTUNREACH);
38488d166bfSBjoern A. Zeeb 	/*
38588d166bfSBjoern A. Zeeb 	 * Do not update this earlier, in case we return with an error.
38688d166bfSBjoern A. Zeeb 	 *
387601c0b8bSAlexander V. Chernikov 	 * XXX: this in6_selectsrc_socket result might replace the bound local
388681ffdf9SBjoern A. Zeeb 	 * address with the address specified by setsockopt(IPV6_PKTINFO).
38988d166bfSBjoern A. Zeeb 	 * Is it the intended behavior?
39088d166bfSBjoern A. Zeeb 	 */
39188d166bfSBjoern A. Zeeb 	*plocal_addr6 = in6a;
39288d166bfSBjoern A. Zeeb 
39382cd038dSYoshinobu Inoue 	/*
39482cd038dSYoshinobu Inoue 	 * Don't do pcblookup call here; return interface in
39582cd038dSYoshinobu Inoue 	 * plocal_addr6
39682cd038dSYoshinobu Inoue 	 * and exit to caller, that will do the lookup.
39782cd038dSYoshinobu Inoue 	 */
398a1f7e5f8SHajimu UMEMOTO 
39982cd038dSYoshinobu Inoue 	return (0);
40082cd038dSYoshinobu Inoue }
40182cd038dSYoshinobu Inoue 
40282cd038dSYoshinobu Inoue /*
40382cd038dSYoshinobu Inoue  * Outer subroutine:
40482cd038dSYoshinobu Inoue  * Connect from a socket to a specified address.
40582cd038dSYoshinobu Inoue  * Both address and port must be specified in argument sin.
40682cd038dSYoshinobu Inoue  * If don't have a local address for this socket yet,
40782cd038dSYoshinobu Inoue  * then pick one.
40882cd038dSYoshinobu Inoue  */
40982cd038dSYoshinobu Inoue int
in6_pcbconnect(struct inpcb * inp,struct sockaddr_in6 * sin6,struct ucred * cred,bool sas_required)410a9d22cceSGleb Smirnoff in6_pcbconnect(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred,
4110bf5377bSAndrey V. Elsukov     bool sas_required)
41282cd038dSYoshinobu Inoue {
413fa046d87SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
41425102351SMike Karels 	struct sockaddr_in6 laddr6;
41582cd038dSYoshinobu Inoue 	int error;
41682cd038dSYoshinobu Inoue 
4173d76be28SGleb Smirnoff 	NET_EPOCH_ASSERT();
4183d76be28SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
4193d76be28SGleb Smirnoff 	INP_HASH_WLOCK_ASSERT(pcbinfo);
420f161d294SMark Johnston 	KASSERT(sin6->sin6_family == AF_INET6,
421f161d294SMark Johnston 	    ("%s: invalid address family for %p", __func__, sin6));
422f161d294SMark Johnston 	KASSERT(sin6->sin6_len == sizeof(*sin6),
423f161d294SMark Johnston 	    ("%s: invalid address length for %p", __func__, sin6));
424fdb987beSMark Johnston 	KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr),
425fdb987beSMark Johnston 	    ("%s: inp is already connected", __func__));
426f161d294SMark Johnston 
42725102351SMike Karels 	bzero(&laddr6, sizeof(laddr6));
42825102351SMike Karels 	laddr6.sin6_family = AF_INET6;
42925102351SMike Karels 
4300c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH
4310c325f53SAlexander V. Chernikov 	if (CALC_FLOWID_OUTBOUND) {
4320c325f53SAlexander V. Chernikov 		uint32_t hash_type, hash_val;
4330c325f53SAlexander V. Chernikov 
4340c325f53SAlexander V. Chernikov 		hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
4350c325f53SAlexander V. Chernikov 		    &sin6->sin6_addr, 0, sin6->sin6_port,
4360c325f53SAlexander V. Chernikov 		    inp->inp_socket->so_proto->pr_protocol, &hash_type);
4370c325f53SAlexander V. Chernikov 		inp->inp_flowid = hash_val;
4380c325f53SAlexander V. Chernikov 		inp->inp_flowtype = hash_type;
4390c325f53SAlexander V. Chernikov 	}
4400c325f53SAlexander V. Chernikov #endif
44182cd038dSYoshinobu Inoue 	/*
44282cd038dSYoshinobu Inoue 	 * Call inner routine, to assign local interface address.
44388ff5695SSUZUKI Shinsuke 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
44482cd038dSYoshinobu Inoue 	 */
4450bf5377bSAndrey V. Elsukov 	if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr,
4460bf5377bSAndrey V. Elsukov 	    sas_required)) != 0)
44782cd038dSYoshinobu Inoue 		return (error);
44882cd038dSYoshinobu Inoue 
449fa046d87SRobert Watson 	if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
4504130ea61SMark Johnston 	    sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
4514130ea61SMark Johnston 	    &laddr6.sin6_addr : &inp->in6p_laddr, inp->inp_lport, 0,
4524130ea61SMark Johnston 	    M_NODOM) != NULL)
45382cd038dSYoshinobu Inoue 		return (EADDRINUSE);
45482cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
45582cd038dSYoshinobu Inoue 		if (inp->inp_lport == 0) {
45625102351SMike Karels 			error = in_pcb_lport_dest(inp,
45725102351SMike Karels 			    (struct sockaddr *) &laddr6, &inp->inp_lport,
458440598ddSJonathan T. Looney 			    (struct sockaddr *) sin6, sin6->sin6_port, cred,
459440598ddSJonathan T. Looney 			    INPLOOKUP_WILDCARD);
4604616026fSErmal Luçi 			if (error)
46182cd038dSYoshinobu Inoue 				return (error);
46282cd038dSYoshinobu Inoue 		}
46325102351SMike Karels 		inp->in6p_laddr = laddr6.sin6_addr;
46482cd038dSYoshinobu Inoue 	}
46582cd038dSYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
46682cd038dSYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
46733841545SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
468fc384fa5SBjoern A. Zeeb 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
469fc384fa5SBjoern A. Zeeb 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
470fc384fa5SBjoern A. Zeeb 		inp->inp_flow |=
471b79274baSHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
47282cd038dSYoshinobu Inoue 
473fdb987beSMark Johnston 	if ((inp->inp_flags & INP_INHASHLIST) != 0) {
474db0ac6deSCy Schubert 		in_pcbrehash(inp);
475fe1274eeSMichael Tuexen 	} else {
476db0ac6deSCy Schubert 		in_pcbinshash(inp);
477fe1274eeSMichael Tuexen 	}
4782cb64cb2SGeorge V. Neville-Neil 
47982cd038dSYoshinobu Inoue 	return (0);
48082cd038dSYoshinobu Inoue }
48182cd038dSYoshinobu Inoue 
48282cd038dSYoshinobu Inoue void
in6_pcbdisconnect(struct inpcb * inp)4831272577eSXin LI in6_pcbdisconnect(struct inpcb *inp)
48482cd038dSYoshinobu Inoue {
48507385abdSRobert Watson 
4868501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
487fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
488fdb987beSMark Johnston 	KASSERT(inp->inp_smr == SMR_SEQ_INVALID,
489fdb987beSMark Johnston 	    ("%s: inp %p was already disconnected", __func__, inp));
49007385abdSRobert Watson 
491fdb987beSMark Johnston 	in_pcbremhash_locked(inp);
492fdb987beSMark Johnston 
493fdb987beSMark Johnston 	/* See the comment in in_pcbinshash(). */
494fdb987beSMark Johnston 	inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr);
495fdb987beSMark Johnston 
496fdb987beSMark Johnston 	/* XXX-MJ torn writes are visible to SMR lookup */
4972589ec0fSMark Johnston 	memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
4982589ec0fSMark Johnston 	memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr));
49982cd038dSYoshinobu Inoue 	inp->inp_fport = 0;
50033841545SHajimu UMEMOTO 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
501fc384fa5SBjoern A. Zeeb 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
50282cd038dSYoshinobu Inoue }
50382cd038dSYoshinobu Inoue 
50482cd038dSYoshinobu Inoue int
in6_getsockaddr(struct socket * so,struct sockaddr * sa)5050fac350cSGleb Smirnoff in6_getsockaddr(struct socket *so, struct sockaddr *sa)
50682cd038dSYoshinobu Inoue {
5073e85b721SEd Maste 	struct inpcb *inp;
50882cd038dSYoshinobu Inoue 
50982cd038dSYoshinobu Inoue 	inp = sotoinpcb(so);
51054d642bbSRobert Watson 	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
51148477723SRobert Watson 
5120fac350cSGleb Smirnoff 	*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
5130fac350cSGleb Smirnoff 		.sin6_len = sizeof(struct sockaddr_in6),
5140fac350cSGleb Smirnoff 		.sin6_family = AF_INET6,
5150fac350cSGleb Smirnoff 		.sin6_port = inp->inp_lport,
5160fac350cSGleb Smirnoff 		.sin6_addr = inp->in6p_laddr,
5170fac350cSGleb Smirnoff 	};
5180fac350cSGleb Smirnoff 	/* XXX: should catch errors */
5190fac350cSGleb Smirnoff 	(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
52082cd038dSYoshinobu Inoue 
5210fac350cSGleb Smirnoff 	return (0);
52282cd038dSYoshinobu Inoue }
52382cd038dSYoshinobu Inoue 
52482cd038dSYoshinobu Inoue int
in6_getpeeraddr(struct socket * so,struct sockaddr * sa)5250fac350cSGleb Smirnoff in6_getpeeraddr(struct socket *so, struct sockaddr *sa)
52682cd038dSYoshinobu Inoue {
52782cd038dSYoshinobu Inoue 	struct inpcb *inp;
52882cd038dSYoshinobu Inoue 
52982cd038dSYoshinobu Inoue 	inp = sotoinpcb(so);
53054d642bbSRobert Watson 	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
53148477723SRobert Watson 
5320fac350cSGleb Smirnoff 	*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
5330fac350cSGleb Smirnoff 		.sin6_len = sizeof(struct sockaddr_in6),
5340fac350cSGleb Smirnoff 		.sin6_family = AF_INET6,
5350fac350cSGleb Smirnoff 		.sin6_port = inp->inp_fport,
5360fac350cSGleb Smirnoff 		.sin6_addr = inp->in6p_faddr,
5370fac350cSGleb Smirnoff 	};
5380fac350cSGleb Smirnoff 	/* XXX: should catch errors */
5390fac350cSGleb Smirnoff 	(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
54082cd038dSYoshinobu Inoue 
5410fac350cSGleb Smirnoff 	return (0);
54282cd038dSYoshinobu Inoue }
54382cd038dSYoshinobu Inoue 
54482cd038dSYoshinobu Inoue int
in6_mapped_sockaddr(struct socket * so,struct sockaddr * sa)5450fac350cSGleb Smirnoff in6_mapped_sockaddr(struct socket *so, struct sockaddr *sa)
54682cd038dSYoshinobu Inoue {
54782cd038dSYoshinobu Inoue 	int	error;
548042fb58dSLexi Winter #ifdef INET
549042fb58dSLexi Winter 	struct	inpcb *inp;
55082cd038dSYoshinobu Inoue 
55148477723SRobert Watson 	inp = sotoinpcb(so);
55248477723SRobert Watson 	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
55348477723SRobert Watson 
5545fac4178SHajimu UMEMOTO 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
5550fac350cSGleb Smirnoff 		struct sockaddr_in sin;
5560fac350cSGleb Smirnoff 
5570fac350cSGleb Smirnoff 		error = in_getsockaddr(so, (struct sockaddr *)&sin);
558369dc8ceSEivind Eklund 		if (error == 0)
5590fac350cSGleb Smirnoff 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
56067107f45SBjoern A. Zeeb 	} else
56167107f45SBjoern A. Zeeb #endif
56267107f45SBjoern A. Zeeb 	{
56354d642bbSRobert Watson 		/* scope issues will be handled in in6_getsockaddr(). */
5640fac350cSGleb Smirnoff 		error = in6_getsockaddr(so, sa);
5657efe5d92SHajimu UMEMOTO 	}
56682cd038dSYoshinobu Inoue 
56782cd038dSYoshinobu Inoue 	return error;
56882cd038dSYoshinobu Inoue }
56982cd038dSYoshinobu Inoue 
57082cd038dSYoshinobu Inoue int
in6_mapped_peeraddr(struct socket * so,struct sockaddr * sa)5710fac350cSGleb Smirnoff in6_mapped_peeraddr(struct socket *so, struct sockaddr *sa)
57282cd038dSYoshinobu Inoue {
57382cd038dSYoshinobu Inoue 	int	error;
574042fb58dSLexi Winter #ifdef INET
575042fb58dSLexi Winter 	struct	inpcb *inp;
57682cd038dSYoshinobu Inoue 
57748477723SRobert Watson 	inp = sotoinpcb(so);
57848477723SRobert Watson 	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
57948477723SRobert Watson 
5805fac4178SHajimu UMEMOTO 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
5810fac350cSGleb Smirnoff 		struct sockaddr_in sin;
5820fac350cSGleb Smirnoff 
5830fac350cSGleb Smirnoff 		error = in_getpeeraddr(so, (struct sockaddr *)&sin);
584369dc8ceSEivind Eklund 		if (error == 0)
5850fac350cSGleb Smirnoff 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
58682cd038dSYoshinobu Inoue 	} else
58767107f45SBjoern A. Zeeb #endif
588042fb58dSLexi Winter 	{
58954d642bbSRobert Watson 		/* scope issues will be handled in in6_getpeeraddr(). */
5900fac350cSGleb Smirnoff 		error = in6_getpeeraddr(so, sa);
591042fb58dSLexi Winter 	}
59282cd038dSYoshinobu Inoue 
59382cd038dSYoshinobu Inoue 	return error;
59482cd038dSYoshinobu Inoue }
59582cd038dSYoshinobu Inoue 
59682cd038dSYoshinobu Inoue /*
59782cd038dSYoshinobu Inoue  * Pass some notification to all connections of a protocol
59882cd038dSYoshinobu Inoue  * associated with address dst.  The local address and/or port numbers
59982cd038dSYoshinobu Inoue  * may be specified to limit the search.  The "usual action" will be
60082cd038dSYoshinobu Inoue  * taken, depending on the ctlinput cmd.  The caller must filter any
60182cd038dSYoshinobu Inoue  * cmds that are uninteresting (e.g., no error in the map).
60282cd038dSYoshinobu Inoue  * Call the protocol specific routine (if any) to report
60382cd038dSYoshinobu Inoue  * any errors for each matching socket.
60482cd038dSYoshinobu Inoue  */
605db0ac6deSCy Schubert static bool
inp_match6(const struct inpcb * inp,void * v __unused)606db0ac6deSCy Schubert inp_match6(const struct inpcb *inp, void *v __unused)
607db0ac6deSCy Schubert {
608db0ac6deSCy Schubert 
609db0ac6deSCy Schubert 	return ((inp->inp_vflag & INP_IPV6) != 0);
610db0ac6deSCy Schubert }
611fcb3f813SGleb Smirnoff 
61282cd038dSYoshinobu Inoue void
in6_pcbnotify(struct inpcbinfo * pcbinfo,struct sockaddr_in6 * sa6_dst,u_int fport_arg,const struct sockaddr_in6 * src,u_int lport_arg,int errno,void * cmdarg,struct inpcb * (* notify)(struct inpcb *,int))61343d39ca7SGleb Smirnoff in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst,
61443d39ca7SGleb Smirnoff     u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg,
615fcb3f813SGleb Smirnoff     int errno, void *cmdarg,
6169233d8f3SDavid E. O'Brien     struct inpcb *(*notify)(struct inpcb *, int))
61782cd038dSYoshinobu Inoue {
618db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB,
619db0ac6deSCy Schubert 	    inp_match6, NULL);
620db0ac6deSCy Schubert 	struct inpcb *inp;
62143d39ca7SGleb Smirnoff 	struct sockaddr_in6 sa6_src;
62282cd038dSYoshinobu Inoue 	u_short	fport = fport_arg, lport = lport_arg;
62333841545SHajimu UMEMOTO 	u_int32_t flowinfo;
62433841545SHajimu UMEMOTO 
62533841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
62682cd038dSYoshinobu Inoue 		return;
62782cd038dSYoshinobu Inoue 
62882cd038dSYoshinobu Inoue 	/*
62933841545SHajimu UMEMOTO 	 * note that src can be NULL when we get notify by local fragmentation.
63033841545SHajimu UMEMOTO 	 */
63143d39ca7SGleb Smirnoff 	sa6_src = (src == NULL) ? sa6_any : *src;
63233841545SHajimu UMEMOTO 	flowinfo = sa6_src.sin6_flowinfo;
63333841545SHajimu UMEMOTO 
634db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
635db0ac6deSCy Schubert 		INP_WLOCK_ASSERT(inp);
636686cdd19SJun-ichiro itojun Hagino 		/*
637efddf5c6SHajimu UMEMOTO 		 * If the error designates a new path MTU for a destination
638efddf5c6SHajimu UMEMOTO 		 * and the application (associated with this socket) wanted to
6398f1beb88SAndrey V. Elsukov 		 * know the value, notify.
640efddf5c6SHajimu UMEMOTO 		 * XXX: should we avoid to notify the value to TCP sockets?
641efddf5c6SHajimu UMEMOTO 		 */
642fcb3f813SGleb Smirnoff 		if (errno == EMSGSIZE && cmdarg != NULL)
64343d39ca7SGleb Smirnoff 			ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg);
644efddf5c6SHajimu UMEMOTO 
645efddf5c6SHajimu UMEMOTO 		/*
64633841545SHajimu UMEMOTO 		 * Detect if we should notify the error. If no source and
64704389c85SGordon Bergling 		 * destination ports are specified, but non-zero flowinfo and
64833841545SHajimu UMEMOTO 		 * local address match, notify the error. This is the case
64933841545SHajimu UMEMOTO 		 * when the error is delivered with an encrypted buffer
65033841545SHajimu UMEMOTO 		 * by ESP. Otherwise, just compare addresses and ports
65133841545SHajimu UMEMOTO 		 * as usual.
652686cdd19SJun-ichiro itojun Hagino 		 */
65333841545SHajimu UMEMOTO 		if (lport == 0 && fport == 0 && flowinfo &&
65433841545SHajimu UMEMOTO 		    inp->inp_socket != NULL &&
655fc384fa5SBjoern A. Zeeb 		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
65633841545SHajimu UMEMOTO 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
65733841545SHajimu UMEMOTO 			goto do_notify;
65833841545SHajimu UMEMOTO 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
65933841545SHajimu UMEMOTO 					     &sa6_dst->sin6_addr) ||
66082cd038dSYoshinobu Inoue 			 inp->inp_socket == 0 ||
66182cd038dSYoshinobu Inoue 			 (lport && inp->inp_lport != lport) ||
66233841545SHajimu UMEMOTO 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
66333841545SHajimu UMEMOTO 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
66433841545SHajimu UMEMOTO 					      &sa6_src.sin6_addr)) ||
665f31f65a7SRobert Watson 			 (fport && inp->inp_fport != fport)) {
66682cd038dSYoshinobu Inoue 			continue;
667f31f65a7SRobert Watson 		}
668686cdd19SJun-ichiro itojun Hagino 
66933841545SHajimu UMEMOTO 	  do_notify:
670db0ac6deSCy Schubert 		if (notify)
671db0ac6deSCy Schubert 			(*notify)(inp, errno);
67282cd038dSYoshinobu Inoue 	}
67382cd038dSYoshinobu Inoue }
67482cd038dSYoshinobu Inoue 
67582cd038dSYoshinobu Inoue /*
676fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
677fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
67882cd038dSYoshinobu Inoue  */
67982cd038dSYoshinobu Inoue struct inpcb *
in6_pcblookup_local(struct inpcbinfo * pcbinfo,struct in6_addr * laddr,u_short lport,int lookupflags,struct ucred * cred)6801272577eSXin LI in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
68168e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
68282cd038dSYoshinobu Inoue {
6833e85b721SEd Maste 	struct inpcb *inp;
68482cd038dSYoshinobu Inoue 	int matchwild = 3, wildcard;
68582cd038dSYoshinobu Inoue 
68668e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
68768e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
68868e0d7e0SRobert Watson 
689c17cd08fSGleb Smirnoff 	INP_HASH_LOCK_ASSERT(pcbinfo);
6908deea4a8SRobert Watson 
69168e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
69282cd038dSYoshinobu Inoue 		struct inpcbhead *head;
69382cd038dSYoshinobu Inoue 		/*
69482cd038dSYoshinobu Inoue 		 * Look for an unconnected (wildcard foreign addr) PCB that
69582cd038dSYoshinobu Inoue 		 * matches the local address and port we're looking for.
69682cd038dSYoshinobu Inoue 		 */
697fdb987beSMark Johnston 		head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
6981b44e5ffSAndrey V. Elsukov 		    pcbinfo->ipi_hashmask)];
699fdb987beSMark Johnston 		CK_LIST_FOREACH(inp, head, inp_hash_wild) {
700413628a7SBjoern A. Zeeb 			/* XXX inp locking */
701369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV6) == 0)
70282cd038dSYoshinobu Inoue 				continue;
70382cd038dSYoshinobu Inoue 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
70482cd038dSYoshinobu Inoue 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
70582cd038dSYoshinobu Inoue 			    inp->inp_lport == lport) {
706413628a7SBjoern A. Zeeb 				/* Found. */
707ac1750ddSMark Johnston 				if (prison_equal_ip6(cred->cr_prison,
7080304c731SJamie Gritton 				    inp->inp_cred->cr_prison))
70982cd038dSYoshinobu Inoue 					return (inp);
71082cd038dSYoshinobu Inoue 			}
71182cd038dSYoshinobu Inoue 		}
71282cd038dSYoshinobu Inoue 		/*
71382cd038dSYoshinobu Inoue 		 * Not found.
71482cd038dSYoshinobu Inoue 		 */
71582cd038dSYoshinobu Inoue 		return (NULL);
71682cd038dSYoshinobu Inoue 	} else {
71782cd038dSYoshinobu Inoue 		struct inpcbporthead *porthash;
71882cd038dSYoshinobu Inoue 		struct inpcbport *phd;
71982cd038dSYoshinobu Inoue 		struct inpcb *match = NULL;
72082cd038dSYoshinobu Inoue 		/*
72182cd038dSYoshinobu Inoue 		 * Best fit PCB lookup.
72282cd038dSYoshinobu Inoue 		 *
72382cd038dSYoshinobu Inoue 		 * First see if this local port is in use by looking on the
72482cd038dSYoshinobu Inoue 		 * port hash list.
72582cd038dSYoshinobu Inoue 		 */
726712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
727712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
728b872626dSMatt Macy 		CK_LIST_FOREACH(phd, porthash, phd_hash) {
72982cd038dSYoshinobu Inoue 			if (phd->phd_port == lport)
73082cd038dSYoshinobu Inoue 				break;
73182cd038dSYoshinobu Inoue 		}
73282cd038dSYoshinobu Inoue 		if (phd != NULL) {
73382cd038dSYoshinobu Inoue 			/*
73482cd038dSYoshinobu Inoue 			 * Port is in use by one or more PCBs. Look for best
73582cd038dSYoshinobu Inoue 			 * fit.
73682cd038dSYoshinobu Inoue 			 */
737b872626dSMatt Macy 			CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
73882cd038dSYoshinobu Inoue 				wildcard = 0;
739ac1750ddSMark Johnston 				if (!prison_equal_ip6(cred->cr_prison,
7400304c731SJamie Gritton 				    inp->inp_cred->cr_prison))
741413628a7SBjoern A. Zeeb 					continue;
742413628a7SBjoern A. Zeeb 				/* XXX inp locking */
743369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV6) == 0)
74482cd038dSYoshinobu Inoue 					continue;
74582cd038dSYoshinobu Inoue 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
74682cd038dSYoshinobu Inoue 					wildcard++;
74782cd038dSYoshinobu Inoue 				if (!IN6_IS_ADDR_UNSPECIFIED(
74882cd038dSYoshinobu Inoue 					&inp->in6p_laddr)) {
74982cd038dSYoshinobu Inoue 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
75082cd038dSYoshinobu Inoue 						wildcard++;
75182cd038dSYoshinobu Inoue 					else if (!IN6_ARE_ADDR_EQUAL(
75282cd038dSYoshinobu Inoue 					    &inp->in6p_laddr, laddr))
75382cd038dSYoshinobu Inoue 						continue;
75482cd038dSYoshinobu Inoue 				} else {
75582cd038dSYoshinobu Inoue 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
75682cd038dSYoshinobu Inoue 						wildcard++;
75782cd038dSYoshinobu Inoue 				}
75882cd038dSYoshinobu Inoue 				if (wildcard < matchwild) {
75982cd038dSYoshinobu Inoue 					match = inp;
76082cd038dSYoshinobu Inoue 					matchwild = wildcard;
761413628a7SBjoern A. Zeeb 					if (matchwild == 0)
76282cd038dSYoshinobu Inoue 						break;
76382cd038dSYoshinobu Inoue 				}
76482cd038dSYoshinobu Inoue 			}
76582cd038dSYoshinobu Inoue 		}
76682cd038dSYoshinobu Inoue 		return (match);
76782cd038dSYoshinobu Inoue 	}
76882cd038dSYoshinobu Inoue }
76982cd038dSYoshinobu Inoue 
770db0ac6deSCy Schubert static bool
in6_multi_match(const struct inpcb * inp,void * v __unused)771db0ac6deSCy Schubert in6_multi_match(const struct inpcb *inp, void *v __unused)
772db0ac6deSCy Schubert {
773db0ac6deSCy Schubert 
774db0ac6deSCy Schubert 	if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL)
775db0ac6deSCy Schubert 		return (true);
776db0ac6deSCy Schubert 	else
777db0ac6deSCy Schubert 		return (false);
778db0ac6deSCy Schubert }
779db0ac6deSCy Schubert 
780e43cc4aeSHajimu UMEMOTO void
in6_pcbpurgeif0(struct inpcbinfo * pcbinfo,struct ifnet * ifp)7811272577eSXin LI in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
782e43cc4aeSHajimu UMEMOTO {
783db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB,
784db0ac6deSCy Schubert 	    in6_multi_match, NULL);
7850ecd976eSBjoern A. Zeeb 	struct inpcb *inp;
78659854ecfSHans Petter Selasky 	struct in6_multi *inm;
78759854ecfSHans Petter Selasky 	struct in6_mfilter *imf;
788e43cc4aeSHajimu UMEMOTO 	struct ip6_moptions *im6o;
789e43cc4aeSHajimu UMEMOTO 
790db0ac6deSCy Schubert 	IN6_MULTI_LOCK_ASSERT();
791db0ac6deSCy Schubert 
792db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
793db0ac6deSCy Schubert 		INP_RLOCK_ASSERT(inp);
794db0ac6deSCy Schubert 
7950ecd976eSBjoern A. Zeeb 		im6o = inp->in6p_moptions;
796e43cc4aeSHajimu UMEMOTO 		/*
79733cde130SBruce M Simpson 		 * Unselect the outgoing ifp for multicast if it
79833cde130SBruce M Simpson 		 * is being detached.
799e43cc4aeSHajimu UMEMOTO 		 */
800e43cc4aeSHajimu UMEMOTO 		if (im6o->im6o_multicast_ifp == ifp)
801e43cc4aeSHajimu UMEMOTO 			im6o->im6o_multicast_ifp = NULL;
802e43cc4aeSHajimu UMEMOTO 		/*
803e43cc4aeSHajimu UMEMOTO 		 * Drop multicast group membership if we joined
804e43cc4aeSHajimu UMEMOTO 		 * through the interface being detached.
805e43cc4aeSHajimu UMEMOTO 		 */
80659854ecfSHans Petter Selasky restart:
80759854ecfSHans Petter Selasky 		IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
80859854ecfSHans Petter Selasky 			if ((inm = imf->im6f_in6m) == NULL)
80959854ecfSHans Petter Selasky 				continue;
81059854ecfSHans Petter Selasky 			if (inm->in6m_ifp != ifp)
81159854ecfSHans Petter Selasky 				continue;
81259854ecfSHans Petter Selasky 			ip6_mfilter_remove(&im6o->im6o_head, imf);
81359854ecfSHans Petter Selasky 			in6_leavegroup_locked(inm, NULL);
81459854ecfSHans Petter Selasky 			ip6_mfilter_free(imf);
81559854ecfSHans Petter Selasky 			goto restart;
816e43cc4aeSHajimu UMEMOTO 		}
817e43cc4aeSHajimu UMEMOTO 	}
818e43cc4aeSHajimu UMEMOTO }
819e43cc4aeSHajimu UMEMOTO 
82082cd038dSYoshinobu Inoue /*
82182cd038dSYoshinobu Inoue  * Check for alternatives when higher level complains
82282cd038dSYoshinobu Inoue  * about service problems.  For now, invalidate cached
82382cd038dSYoshinobu Inoue  * routing information.  If the route was created dynamically
82482cd038dSYoshinobu Inoue  * (by a redirect), time to try a default gateway again.
82582cd038dSYoshinobu Inoue  */
82682cd038dSYoshinobu Inoue void
in6_losing(struct inpcb * inp)827ec86402eSBjoern A. Zeeb in6_losing(struct inpcb *inp)
82882cd038dSYoshinobu Inoue {
8291272577eSXin LI 
830ec86402eSBjoern A. Zeeb 	RO_INVALIDATE_CACHE(&inp->inp_route6);
83182cd038dSYoshinobu Inoue }
83282cd038dSYoshinobu Inoue 
83382cd038dSYoshinobu Inoue /*
83482cd038dSYoshinobu Inoue  * After a routing change, flush old routing
83582cd038dSYoshinobu Inoue  * and allocate a (hopefully) better one.
83682cd038dSYoshinobu Inoue  */
8373ce144eaSJeffrey Hsu struct inpcb *
in6_rtchange(struct inpcb * inp,int errno __unused)838ec86402eSBjoern A. Zeeb in6_rtchange(struct inpcb *inp, int errno __unused)
83982cd038dSYoshinobu Inoue {
84084cc0778SGeorge V. Neville-Neil 
841ec86402eSBjoern A. Zeeb 	RO_INVALIDATE_CACHE(&inp->inp_route6);
8423ce144eaSJeffrey Hsu 	return inp;
84382cd038dSYoshinobu Inoue }
84482cd038dSYoshinobu Inoue 
845d93ec8cbSMark Johnston static bool
in6_pcblookup_lb_numa_match(const struct inpcblbgroup * grp,int domain)846d93ec8cbSMark Johnston in6_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain)
847d93ec8cbSMark Johnston {
848d93ec8cbSMark Johnston 	return (domain == M_NODOM || domain == grp->il_numa_domain);
849d93ec8cbSMark Johnston }
850d93ec8cbSMark Johnston 
8511a43cff9SSean Bruno static struct inpcb *
in6_pcblookup_lbgroup(const struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,uint16_t fport,const struct in6_addr * laddr,uint16_t lport,uint8_t domain)8521a43cff9SSean Bruno in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
8534130ea61SMark Johnston     const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr,
8544130ea61SMark Johnston     uint16_t lport, uint8_t domain)
8551a43cff9SSean Bruno {
8561a43cff9SSean Bruno 	const struct inpcblbgrouphead *hdr;
8571a43cff9SSean Bruno 	struct inpcblbgroup *grp;
858d93ec8cbSMark Johnston 	struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild;
8591a43cff9SSean Bruno 
8601a43cff9SSean Bruno 	INP_HASH_LOCK_ASSERT(pcbinfo);
8611a43cff9SSean Bruno 
8629d2877fcSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[
8639d2877fcSMark Johnston 	    INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
8641a43cff9SSean Bruno 
8651a43cff9SSean Bruno 	/*
866d93ec8cbSMark Johnston 	 * Search for an LB group match based on the following criteria:
867d93ec8cbSMark Johnston 	 * - prefer jailed groups to non-jailed groups
868d93ec8cbSMark Johnston 	 * - prefer exact source address matches to wildcard matches
869d93ec8cbSMark Johnston 	 * - prefer groups bound to the specified NUMA domain
8701a43cff9SSean Bruno 	 */
871d93ec8cbSMark Johnston 	jail_exact = jail_wild = local_exact = local_wild = NULL;
87254af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
873d93ec8cbSMark Johnston 		bool injail;
874d93ec8cbSMark Johnston 
8754b82a7b6SAndrew Gallatin #ifdef INET
8764b82a7b6SAndrew Gallatin 		if (!(grp->il_vflag & INP_IPV6))
8774b82a7b6SAndrew Gallatin 			continue;
8784b82a7b6SAndrew Gallatin #endif
879d3a4b0daSMark Johnston 		if (grp->il_lport != lport)
880d3a4b0daSMark Johnston 			continue;
8811a43cff9SSean Bruno 
882d93ec8cbSMark Johnston 		injail = prison_flag(grp->il_cred, PR_IP6) != 0;
883d93ec8cbSMark Johnston 		if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison,
884d93ec8cbSMark Johnston 		    laddr) != 0)
885d93ec8cbSMark Johnston 			continue;
886d93ec8cbSMark Johnston 
887a034518aSAndrew Gallatin 		if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
888d93ec8cbSMark Johnston 			if (injail) {
889d93ec8cbSMark Johnston 				jail_exact = grp;
890d93ec8cbSMark Johnston 				if (in6_pcblookup_lb_numa_match(grp, domain))
891d93ec8cbSMark Johnston 					/* This is a perfect match. */
892d93ec8cbSMark Johnston 					goto out;
893d93ec8cbSMark Johnston 			} else if (local_exact == NULL ||
894d93ec8cbSMark Johnston 			    in6_pcblookup_lb_numa_match(grp, domain)) {
895d93ec8cbSMark Johnston 				local_exact = grp;
896a034518aSAndrew Gallatin 			}
8974130ea61SMark Johnston 		} else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) {
898d93ec8cbSMark Johnston 			if (injail) {
899d93ec8cbSMark Johnston 				if (jail_wild == NULL ||
900d93ec8cbSMark Johnston 				    in6_pcblookup_lb_numa_match(grp, domain))
901d93ec8cbSMark Johnston 					jail_wild = grp;
902d93ec8cbSMark Johnston 			} else if (local_wild == NULL ||
903d93ec8cbSMark Johnston 			    in6_pcblookup_lb_numa_match(grp, domain)) {
904d93ec8cbSMark Johnston 				local_wild = grp;
905d93ec8cbSMark Johnston 			}
906d93ec8cbSMark Johnston 		}
907d93ec8cbSMark Johnston 	}
908d93ec8cbSMark Johnston 
909d93ec8cbSMark Johnston 	if (jail_exact != NULL)
910d93ec8cbSMark Johnston 		grp = jail_exact;
911d93ec8cbSMark Johnston 	else if (jail_wild != NULL)
912d93ec8cbSMark Johnston 		grp = jail_wild;
913d93ec8cbSMark Johnston 	else if (local_exact != NULL)
914d93ec8cbSMark Johnston 		grp = local_exact;
915a034518aSAndrew Gallatin 	else
916d93ec8cbSMark Johnston 		grp = local_wild;
917d93ec8cbSMark Johnston 	if (grp == NULL)
918d93ec8cbSMark Johnston 		return (NULL);
919d93ec8cbSMark Johnston out:
920d93ec8cbSMark Johnston 	return (grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) %
921d93ec8cbSMark Johnston 	    grp->il_inpcnt]);
9221a43cff9SSean Bruno }
9231a43cff9SSean Bruno 
9243e98dcb3SMark Johnston static bool
in6_pcblookup_exact_match(const struct inpcb * inp,const struct in6_addr * faddr,u_short fport,const struct in6_addr * laddr,u_short lport)9253e98dcb3SMark Johnston in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr,
9263e98dcb3SMark Johnston     u_short fport, const struct in6_addr *laddr, u_short lport)
9273e98dcb3SMark Johnston {
9283e98dcb3SMark Johnston 	/* XXX inp locking */
9293e98dcb3SMark Johnston 	if ((inp->inp_vflag & INP_IPV6) == 0)
9303e98dcb3SMark Johnston 		return (false);
9313e98dcb3SMark Johnston 	if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
9323e98dcb3SMark Johnston 	    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
9333e98dcb3SMark Johnston 	    inp->inp_fport == fport && inp->inp_lport == lport)
9343e98dcb3SMark Johnston 		return (true);
9353e98dcb3SMark Johnston 	return (false);
9363e98dcb3SMark Johnston }
9373e98dcb3SMark Johnston 
9384130ea61SMark Johnston static struct inpcb *
in6_pcblookup_hash_exact(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_short fport,const struct in6_addr * laddr,u_short lport)9397b92493aSMark Johnston in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo,
9407b92493aSMark Johnston     const struct in6_addr *faddr, u_short fport,
9417b92493aSMark Johnston     const struct in6_addr *laddr, u_short lport)
94282cd038dSYoshinobu Inoue {
94382cd038dSYoshinobu Inoue 	struct inpcbhead *head;
9443e98dcb3SMark Johnston 	struct inpcb *inp;
94568e0d7e0SRobert Watson 
946fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
9478deea4a8SRobert Watson 
94882cd038dSYoshinobu Inoue 	/*
94982cd038dSYoshinobu Inoue 	 * First look for an exact match.
95082cd038dSYoshinobu Inoue 	 */
951fdb987beSMark Johnston 	head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport,
952a0577692SGleb Smirnoff 	    pcbinfo->ipi_hashmask)];
953fdb987beSMark Johnston 	CK_LIST_FOREACH(inp, head, inp_hash_exact) {
9543e98dcb3SMark Johnston 		if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport))
95582cd038dSYoshinobu Inoue 			return (inp);
95682cd038dSYoshinobu Inoue 	}
9573e98dcb3SMark Johnston 	return (NULL);
9583e98dcb3SMark Johnston }
9593e98dcb3SMark Johnston 
9603e98dcb3SMark Johnston typedef enum {
9613e98dcb3SMark Johnston 	INPLOOKUP_MATCH_NONE = 0,
9623e98dcb3SMark Johnston 	INPLOOKUP_MATCH_WILD = 1,
9633e98dcb3SMark Johnston 	INPLOOKUP_MATCH_LADDR = 2,
9643e98dcb3SMark Johnston } inp_lookup_match_t;
9653e98dcb3SMark Johnston 
9663e98dcb3SMark Johnston static inp_lookup_match_t
in6_pcblookup_wild_match(const struct inpcb * inp,const struct in6_addr * laddr,u_short lport)9673e98dcb3SMark Johnston in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr,
9683e98dcb3SMark Johnston     u_short lport)
9693e98dcb3SMark Johnston {
9703e98dcb3SMark Johnston 	/* XXX inp locking */
9713e98dcb3SMark Johnston 	if ((inp->inp_vflag & INP_IPV6) == 0)
9723e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_NONE);
9733e98dcb3SMark Johnston 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
9743e98dcb3SMark Johnston 	    inp->inp_lport != lport)
9753e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_NONE);
9763e98dcb3SMark Johnston 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
9773e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_WILD);
9783e98dcb3SMark Johnston 	if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))
9793e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_LADDR);
9803e98dcb3SMark Johnston 	return (INPLOOKUP_MATCH_NONE);
9814130ea61SMark Johnston }
98282cd038dSYoshinobu Inoue 
9837b92493aSMark Johnston #define	INP_LOOKUP_AGAIN	((struct inpcb *)(uintptr_t)-1)
9847b92493aSMark Johnston 
9857b92493aSMark Johnston static struct inpcb *
in6_pcblookup_hash_wild_smr(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_short fport,const struct in6_addr * laddr,u_short lport,const inp_lookup_t lockflags)9867b92493aSMark Johnston in6_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo,
9877b92493aSMark Johnston     const struct in6_addr *faddr, u_short fport, const struct in6_addr *laddr,
9887b92493aSMark Johnston     u_short lport, const inp_lookup_t lockflags)
9897b92493aSMark Johnston {
9907b92493aSMark Johnston 	struct inpcbhead *head;
9917b92493aSMark Johnston 	struct inpcb *inp;
9927b92493aSMark Johnston 
9937b92493aSMark Johnston 	KASSERT(SMR_ENTERED(pcbinfo->ipi_smr),
9947b92493aSMark Johnston 	    ("%s: not in SMR read section", __func__));
9957b92493aSMark Johnston 
9967b92493aSMark Johnston 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
9977b92493aSMark Johnston 	    pcbinfo->ipi_hashmask)];
9987b92493aSMark Johnston 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
9997b92493aSMark Johnston 		inp_lookup_match_t match;
10007b92493aSMark Johnston 
10017b92493aSMark Johnston 		match = in6_pcblookup_wild_match(inp, laddr, lport);
10027b92493aSMark Johnston 		if (match == INPLOOKUP_MATCH_NONE)
10037b92493aSMark Johnston 			continue;
10047b92493aSMark Johnston 
10057b92493aSMark Johnston 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
1006a306ed50SMark Johnston 			match = in6_pcblookup_wild_match(inp, laddr, lport);
1007a306ed50SMark Johnston 			if (match != INPLOOKUP_MATCH_NONE &&
1008a306ed50SMark Johnston 			    prison_check_ip6_locked(inp->inp_cred->cr_prison,
1009a306ed50SMark Johnston 			    laddr) == 0)
10107b92493aSMark Johnston 				return (inp);
10117b92493aSMark Johnston 			inp_unlock(inp, lockflags);
10127b92493aSMark Johnston 		}
10137b92493aSMark Johnston 
10147b92493aSMark Johnston 		/*
10157b92493aSMark Johnston 		 * The matching socket disappeared out from under us.  Fall back
10167b92493aSMark Johnston 		 * to a serialized lookup.
10177b92493aSMark Johnston 		 */
10187b92493aSMark Johnston 		return (INP_LOOKUP_AGAIN);
10197b92493aSMark Johnston 	}
10207b92493aSMark Johnston 	return (NULL);
10217b92493aSMark Johnston }
10227b92493aSMark Johnston 
10234130ea61SMark Johnston static struct inpcb *
in6_pcblookup_hash_wild_locked(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_short fport,const struct in6_addr * laddr,u_short lport)10244130ea61SMark Johnston in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo,
10257b92493aSMark Johnston     const struct in6_addr *faddr, u_short fport, const struct in6_addr *laddr,
10264130ea61SMark Johnston     u_short lport)
10274130ea61SMark Johnston {
10284130ea61SMark Johnston 	struct inpcbhead *head;
10294130ea61SMark Johnston 	struct inpcb *inp, *jail_wild, *local_exact, *local_wild;
1030d93ec8cbSMark Johnston 
10313e98dcb3SMark Johnston 	INP_HASH_LOCK_ASSERT(pcbinfo);
10323e98dcb3SMark Johnston 
1033d93ec8cbSMark Johnston 	/*
1034413628a7SBjoern A. Zeeb 	 * Order of socket selection - we always prefer jails.
1035413628a7SBjoern A. Zeeb 	 *      1. jailed, non-wild.
1036413628a7SBjoern A. Zeeb 	 *      2. jailed, wild.
1037413628a7SBjoern A. Zeeb 	 *      3. non-jailed, non-wild.
1038413628a7SBjoern A. Zeeb 	 *      4. non-jailed, wild.
1039413628a7SBjoern A. Zeeb 	 */
1040fdb987beSMark Johnston 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
10411b44e5ffSAndrey V. Elsukov 	    pcbinfo->ipi_hashmask)];
10424130ea61SMark Johnston 	local_wild = local_exact = jail_wild = NULL;
1043fdb987beSMark Johnston 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
10443e98dcb3SMark Johnston 		inp_lookup_match_t match;
10454130ea61SMark Johnston 		bool injail;
10464130ea61SMark Johnston 
10473e98dcb3SMark Johnston 		match = in6_pcblookup_wild_match(inp, laddr, lport);
10483e98dcb3SMark Johnston 		if (match == INPLOOKUP_MATCH_NONE)
104982cd038dSYoshinobu Inoue 			continue;
1050413628a7SBjoern A. Zeeb 
10514130ea61SMark Johnston 		injail = prison_flag(inp->inp_cred, PR_IP6) != 0;
1052413628a7SBjoern A. Zeeb 		if (injail) {
1053185e659cSGleb Smirnoff 			if (prison_check_ip6_locked(
1054185e659cSGleb Smirnoff 			    inp->inp_cred->cr_prison, laddr) != 0)
1055413628a7SBjoern A. Zeeb 				continue;
1056413628a7SBjoern A. Zeeb 		} else {
1057413628a7SBjoern A. Zeeb 			if (local_exact != NULL)
1058413628a7SBjoern A. Zeeb 				continue;
1059413628a7SBjoern A. Zeeb 		}
1060413628a7SBjoern A. Zeeb 
10613e98dcb3SMark Johnston 		if (match == INPLOOKUP_MATCH_LADDR) {
1062413628a7SBjoern A. Zeeb 			if (injail)
106382cd038dSYoshinobu Inoue 				return (inp);
1064413628a7SBjoern A. Zeeb 			else
1065413628a7SBjoern A. Zeeb 				local_exact = inp;
10663e98dcb3SMark Johnston 		} else {
1067413628a7SBjoern A. Zeeb 			if (injail)
1068413628a7SBjoern A. Zeeb 				jail_wild = inp;
1069413628a7SBjoern A. Zeeb 			else
107082cd038dSYoshinobu Inoue 				local_wild = inp;
107182cd038dSYoshinobu Inoue 		}
10724130ea61SMark Johnston 	}
1073413628a7SBjoern A. Zeeb 
1074413628a7SBjoern A. Zeeb 	if (jail_wild != NULL)
1075413628a7SBjoern A. Zeeb 		return (jail_wild);
1076413628a7SBjoern A. Zeeb 	if (local_exact != NULL)
1077413628a7SBjoern A. Zeeb 		return (local_exact);
1078413628a7SBjoern A. Zeeb 	if (local_wild != NULL)
107982cd038dSYoshinobu Inoue 		return (local_wild);
108082cd038dSYoshinobu Inoue 	return (NULL);
108182cd038dSYoshinobu Inoue }
108282cd038dSYoshinobu Inoue 
10834130ea61SMark Johnston struct inpcb *
in6_pcblookup_hash_locked(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_int fport_arg,const struct in6_addr * laddr,u_int lport_arg,int lookupflags,uint8_t numa_domain)10847b92493aSMark Johnston in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
10857b92493aSMark Johnston     const struct in6_addr *faddr, u_int fport_arg,
10867b92493aSMark Johnston     const struct in6_addr *laddr, u_int lport_arg,
10874130ea61SMark Johnston     int lookupflags, uint8_t numa_domain)
10884130ea61SMark Johnston {
10894130ea61SMark Johnston 	struct inpcb *inp;
10904130ea61SMark Johnston 	u_short fport = fport_arg, lport = lport_arg;
10914130ea61SMark Johnston 
10924130ea61SMark Johnston 	KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0,
10934130ea61SMark Johnston 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
10944130ea61SMark Johnston 	KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr),
10954130ea61SMark Johnston 	    ("%s: invalid foreign address", __func__));
10964130ea61SMark Johnston 	KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr),
10974130ea61SMark Johnston 	    ("%s: invalid local address", __func__));
10984130ea61SMark Johnston 	INP_HASH_LOCK_ASSERT(pcbinfo);
10994130ea61SMark Johnston 
11004130ea61SMark Johnston 	inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
11014130ea61SMark Johnston 	if (inp != NULL)
11024130ea61SMark Johnston 		return (inp);
11034130ea61SMark Johnston 
11044130ea61SMark Johnston 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
11054130ea61SMark Johnston 		inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr,
11064130ea61SMark Johnston 		    lport, numa_domain);
11074130ea61SMark Johnston 		if (inp == NULL) {
11084130ea61SMark Johnston 			inp = in6_pcblookup_hash_wild_locked(pcbinfo, faddr,
11094130ea61SMark Johnston 			    fport, laddr, lport);
11104130ea61SMark Johnston 		}
11114130ea61SMark Johnston 	}
11124130ea61SMark Johnston 	return (inp);
11134130ea61SMark Johnston }
11144130ea61SMark Johnston 
1115fa046d87SRobert Watson static struct inpcb *
in6_pcblookup_hash(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_int fport,const struct in6_addr * laddr,u_int lport,int lookupflags,uint8_t numa_domain)11167b92493aSMark Johnston in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
11177b92493aSMark Johnston     u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
11184130ea61SMark Johnston     uint8_t numa_domain)
1119fa046d87SRobert Watson {
1120fa046d87SRobert Watson 	struct inpcb *inp;
11217b92493aSMark Johnston 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
11227b92493aSMark Johnston 
11237b92493aSMark Johnston 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
11247b92493aSMark Johnston 	    ("%s: LOCKPCB not set", __func__));
11257b92493aSMark Johnston 
11267b92493aSMark Johnston 	INP_HASH_WLOCK(pcbinfo);
11277b92493aSMark Johnston 	inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
11287b92493aSMark Johnston 	    lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain);
11297b92493aSMark Johnston 	if (inp != NULL && !inp_trylock(inp, lockflags)) {
11307b92493aSMark Johnston 		in_pcbref(inp);
11317b92493aSMark Johnston 		INP_HASH_WUNLOCK(pcbinfo);
11327b92493aSMark Johnston 		inp_lock(inp, lockflags);
11337b92493aSMark Johnston 		if (in_pcbrele(inp, lockflags))
11347b92493aSMark Johnston 			/* XXX-MJ or retry until we get a negative match? */
11357b92493aSMark Johnston 			inp = NULL;
11367b92493aSMark Johnston 	} else {
11377b92493aSMark Johnston 		INP_HASH_WUNLOCK(pcbinfo);
11387b92493aSMark Johnston 	}
11397b92493aSMark Johnston 	return (inp);
11407b92493aSMark Johnston }
11417b92493aSMark Johnston 
11427b92493aSMark Johnston static struct inpcb *
in6_pcblookup_hash_smr(struct inpcbinfo * pcbinfo,struct in6_addr * faddr,u_int fport_arg,struct in6_addr * laddr,u_int lport_arg,int lookupflags,uint8_t numa_domain)11437b92493aSMark Johnston in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
11447b92493aSMark Johnston     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int lookupflags,
11457b92493aSMark Johnston     uint8_t numa_domain)
11467b92493aSMark Johnston {
11477b92493aSMark Johnston 	struct inpcb *inp;
11487b92493aSMark Johnston 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
11497b92493aSMark Johnston 	const u_short fport = fport_arg, lport = lport_arg;
1150fa046d87SRobert Watson 
1151675e2618SMark Johnston 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1152675e2618SMark Johnston 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1153675e2618SMark Johnston 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1154675e2618SMark Johnston 	    ("%s: LOCKPCB not set", __func__));
1155675e2618SMark Johnston 
1156db0ac6deSCy Schubert 	smr_enter(pcbinfo->ipi_smr);
11577b92493aSMark Johnston 	inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1158fa046d87SRobert Watson 	if (inp != NULL) {
11597b92493aSMark Johnston 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
11607b92493aSMark Johnston 			if (__predict_true(in6_pcblookup_exact_match(inp,
11617b92493aSMark Johnston 			    faddr, fport, laddr, lport)))
11627b92493aSMark Johnston 				return (inp);
11637b92493aSMark Johnston 			inp_unlock(inp, lockflags);
11647b92493aSMark Johnston 		}
11657b92493aSMark Johnston 		/*
11667b92493aSMark Johnston 		 * We failed to lock the inpcb, or its connection state changed
11677b92493aSMark Johnston 		 * out from under us.  Fall back to a precise search.
11687b92493aSMark Johnston 		 */
11697b92493aSMark Johnston 		return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
11707b92493aSMark Johnston 		    lookupflags, numa_domain));
11717b92493aSMark Johnston 	}
11727b92493aSMark Johnston 
11737b92493aSMark Johnston 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
11747b92493aSMark Johnston 		inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport,
11757b92493aSMark Johnston 		    laddr, lport, numa_domain);
11767b92493aSMark Johnston 		if (inp != NULL) {
11777b92493aSMark Johnston 			if (__predict_true(inp_smr_lock(inp, lockflags))) {
11787b92493aSMark Johnston 				if (__predict_true(in6_pcblookup_wild_match(inp,
11797b92493aSMark Johnston 				    laddr, lport) != INPLOOKUP_MATCH_NONE))
11807b92493aSMark Johnston 					return (inp);
11817b92493aSMark Johnston 				inp_unlock(inp, lockflags);
11827b92493aSMark Johnston 			}
11837b92493aSMark Johnston 			inp = INP_LOOKUP_AGAIN;
11847b92493aSMark Johnston 		} else {
11857b92493aSMark Johnston 			inp = in6_pcblookup_hash_wild_smr(pcbinfo, faddr, fport,
11867b92493aSMark Johnston 			    laddr, lport, lockflags);
11877b92493aSMark Johnston 		}
11887b92493aSMark Johnston 		if (inp == INP_LOOKUP_AGAIN) {
11897b92493aSMark Johnston 			return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr,
11907b92493aSMark Johnston 			    lport, lookupflags, numa_domain));
11917b92493aSMark Johnston 		}
11927b92493aSMark Johnston 	}
11937b92493aSMark Johnston 
11947b92493aSMark Johnston 	if (inp == NULL)
1195db0ac6deSCy Schubert 		smr_exit(pcbinfo->ipi_smr);
1196db0ac6deSCy Schubert 
1197fa046d87SRobert Watson 	return (inp);
1198fa046d87SRobert Watson }
1199fa046d87SRobert Watson 
1200fa046d87SRobert Watson /*
1201d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1202d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
1203fa046d87SRobert Watson  */
1204fa046d87SRobert Watson struct inpcb *
in6_pcblookup(struct inpcbinfo * pcbinfo,struct in6_addr * faddr,u_int fport,struct in6_addr * laddr,u_int lport,int lookupflags,struct ifnet * ifp __unused)1205fa046d87SRobert Watson in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
12064130ea61SMark Johnston     struct in6_addr *laddr, u_int lport, int lookupflags,
12074130ea61SMark Johnston     struct ifnet *ifp __unused)
1208fa046d87SRobert Watson {
12094130ea61SMark Johnston 	return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
12104130ea61SMark Johnston 	    lookupflags, M_NODOM));
1211d3c1f003SRobert Watson }
1212d3c1f003SRobert Watson 
1213d3c1f003SRobert Watson struct inpcb *
in6_pcblookup_mbuf(struct inpcbinfo * pcbinfo,struct in6_addr * faddr,u_int fport,struct in6_addr * laddr,u_int lport,int lookupflags,struct ifnet * ifp __unused,struct mbuf * m)1214d3c1f003SRobert Watson in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1215d3c1f003SRobert Watson     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
12164130ea61SMark Johnston     struct ifnet *ifp __unused, struct mbuf *m)
1217d3c1f003SRobert Watson {
12184130ea61SMark Johnston 	return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
12194130ea61SMark Johnston 	    lookupflags, m->m_pkthdr.numa_domain));
1220fa046d87SRobert Watson }
1221fa046d87SRobert Watson 
122282cd038dSYoshinobu Inoue void
init_sin6(struct sockaddr_in6 * sin6,struct mbuf * m,int srcordst)1223dce33a45SErmal Luçi init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
122482cd038dSYoshinobu Inoue {
122582cd038dSYoshinobu Inoue 	struct ip6_hdr *ip;
122682cd038dSYoshinobu Inoue 
122782cd038dSYoshinobu Inoue 	ip = mtod(m, struct ip6_hdr *);
122882cd038dSYoshinobu Inoue 	bzero(sin6, sizeof(*sin6));
122982cd038dSYoshinobu Inoue 	sin6->sin6_len = sizeof(*sin6);
123082cd038dSYoshinobu Inoue 	sin6->sin6_family = AF_INET6;
1231dce33a45SErmal Luçi 	sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1232a1f7e5f8SHajimu UMEMOTO 
1233a1f7e5f8SHajimu UMEMOTO 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
123482cd038dSYoshinobu Inoue 
123582cd038dSYoshinobu Inoue 	return;
123682cd038dSYoshinobu Inoue }
1237