xref: /freebsd/sys/netinet6/in6_pcb.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Robert N. M. Watson under
9  * contract to Juniper Networks, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
36  */
37 
38 /*-
39  * Copyright (c) 1982, 1986, 1991, 1993
40  *	The Regents of the University of California.  All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  */
66 
67 #include <sys/cdefs.h>
68 #include "opt_inet.h"
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71 #include "opt_route.h"
72 #include "opt_rss.h"
73 
74 #include <sys/hash.h>
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/domain.h>
80 #include <sys/proc.h>
81 #include <sys/protosw.h>
82 #include <sys/smr.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/sockio.h>
86 #include <sys/sysctl.h>
87 #include <sys/errno.h>
88 #include <sys/time.h>
89 #include <sys/priv.h>
90 #include <sys/proc.h>
91 #include <sys/jail.h>
92 
93 #include <vm/uma.h>
94 
95 #include <net/if.h>
96 #include <net/if_var.h>
97 #include <net/if_llatbl.h>
98 #include <net/if_types.h>
99 #include <net/route.h>
100 #include <net/route/nhop.h>
101 #include <net/vnet.h>
102 
103 #include <netinet/in.h>
104 #include <netinet/in_var.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip6.h>
107 #include <netinet/ip_var.h>
108 
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet/in_pcb_var.h>
113 #include <netinet6/in6_pcb.h>
114 #include <netinet6/in6_fib.h>
115 #include <netinet6/scope6_var.h>
116 
117 SYSCTL_DECL(_net_inet6);
118 SYSCTL_DECL(_net_inet6_ip6);
119 VNET_DEFINE_STATIC(int, connect_in6addr_wild) = 1;
120 #define	V_connect_in6addr_wild	VNET(connect_in6addr_wild)
121 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, connect_in6addr_wild,
122     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(connect_in6addr_wild), 0,
123     "Allow connecting to the unspecified address for connect(2)");
124 
125 int
126 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred)
127 {
128 	struct socket *so = inp->inp_socket;
129 	u_int16_t lport = 0;
130 	int error, lookupflags = 0;
131 #ifdef INVARIANTS
132 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
133 #endif
134 
135 	INP_WLOCK_ASSERT(inp);
136 	INP_HASH_WLOCK_ASSERT(pcbinfo);
137 
138 	error = prison_local_ip6(cred, laddr,
139 	    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0));
140 	if (error)
141 		return(error);
142 
143 	/* XXX: this is redundant when called from in6_pcbbind */
144 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
145 		lookupflags = INPLOOKUP_WILDCARD;
146 
147 	inp->inp_flags |= INP_ANONPORT;
148 
149 	error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags);
150 	if (error != 0)
151 		return (error);
152 
153 	inp->inp_lport = lport;
154 	if (in_pcbinshash(inp) != 0) {
155 		inp->in6p_laddr = in6addr_any;
156 		inp->inp_lport = 0;
157 		return (EAGAIN);
158 	}
159 
160 	return (0);
161 }
162 
163 /*
164  * Determine whether the inpcb can be bound to the specified address/port tuple.
165  */
166 static int
167 in6_pcbbind_avail(struct inpcb *inp, const struct sockaddr_in6 *sin6,
168     int sooptions, int lookupflags, struct ucred *cred)
169 {
170 	const struct in6_addr *laddr;
171 	int reuseport, reuseport_lb;
172 	u_short lport;
173 
174 	INP_LOCK_ASSERT(inp);
175 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
176 
177 	laddr = &sin6->sin6_addr;
178 	lport = sin6->sin6_port;
179 
180 	reuseport = (sooptions & SO_REUSEPORT);
181 	reuseport_lb = (sooptions & SO_REUSEPORT_LB);
182 
183 	if (IN6_IS_ADDR_MULTICAST(laddr)) {
184 		/*
185 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
186 		 * allow compepte duplication of binding if
187 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
188 		 * and a multicast address is bound on both
189 		 * new and duplicated sockets.
190 		 */
191 		if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0)
192 			reuseport = SO_REUSEADDR | SO_REUSEPORT;
193 		/*
194 		 * XXX: How to deal with SO_REUSEPORT_LB here?
195 		 * Treat same as SO_REUSEPORT for now.
196 		 */
197 		if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0)
198 			reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB;
199 	} else if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) {
200 		struct sockaddr_in6 sin6;
201 		struct epoch_tracker et;
202 		struct ifaddr *ifa;
203 
204 		memset(&sin6, 0, sizeof(sin6));
205 		sin6.sin6_family = AF_INET6;
206 		sin6.sin6_len = sizeof(sin6);
207 		sin6.sin6_addr = *laddr;
208 
209 		NET_EPOCH_ENTER(et);
210 		if ((ifa = ifa_ifwithaddr((const struct sockaddr *)&sin6)) ==
211 		    NULL && (inp->inp_flags & INP_BINDANY) == 0) {
212 			NET_EPOCH_EXIT(et);
213 			return (EADDRNOTAVAIL);
214 		}
215 
216 		/*
217 		 * XXX: bind to an anycast address might accidentally
218 		 * cause sending a packet with anycast source address.
219 		 * We should allow to bind to a deprecated address, since
220 		 * the application dares to use it.
221 		 */
222 		if (ifa != NULL &&
223 		    ((struct in6_ifaddr *)ifa)->ia6_flags &
224 		    (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_DETACHED)) {
225 			NET_EPOCH_EXIT(et);
226 			return (EADDRNOTAVAIL);
227 		}
228 		NET_EPOCH_EXIT(et);
229 	}
230 
231 	if (lport != 0) {
232 		struct inpcb *t;
233 
234 		if (ntohs(lport) <= V_ipport_reservedhigh &&
235 		    ntohs(lport) >= V_ipport_reservedlow &&
236 		    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
237 			return (EACCES);
238 
239 		if (!IN6_IS_ADDR_MULTICAST(laddr) &&
240 		    priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) !=
241 		    0) {
242 			t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport,
243 			    INPLOOKUP_WILDCARD, cred);
244 			if (t != NULL &&
245 			    (inp->inp_socket->so_type != SOCK_STREAM ||
246 			     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
247 			    (!IN6_IS_ADDR_UNSPECIFIED(laddr) ||
248 			     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr)) &&
249 			    (inp->inp_cred->cr_uid != t->inp_cred->cr_uid))
250 				return (EADDRINUSE);
251 
252 #ifdef INET
253 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
254 			    IN6_IS_ADDR_UNSPECIFIED(laddr)) {
255 				struct sockaddr_in sin;
256 
257 				in6_sin6_2_sin(&sin, sin6);
258 				t = in_pcblookup_local(inp->inp_pcbinfo,
259 				    sin.sin_addr, lport, INPLOOKUP_WILDCARD,
260 				    cred);
261 				if (t != NULL &&
262 				    (inp->inp_socket->so_type != SOCK_STREAM ||
263 				     in_nullhost(t->inp_faddr)) &&
264 				    (inp->inp_cred->cr_uid !=
265 				     t->inp_cred->cr_uid))
266 					return (EADDRINUSE);
267 			}
268 #endif
269 		}
270 		t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport,
271 		    lookupflags, cred);
272 		if (t != NULL && ((reuseport | reuseport_lb) &
273 		    t->inp_socket->so_options) == 0)
274 			return (EADDRINUSE);
275 #ifdef INET
276 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
277 		    IN6_IS_ADDR_UNSPECIFIED(laddr)) {
278 			struct sockaddr_in sin;
279 
280 			in6_sin6_2_sin(&sin, sin6);
281 			t = in_pcblookup_local(inp->inp_pcbinfo, sin.sin_addr,
282 			   lport, lookupflags, cred);
283 			if (t != NULL && ((reuseport | reuseport_lb) &
284 			    t->inp_socket->so_options) == 0 &&
285 			    (!in_nullhost(t->inp_laddr) ||
286 			     (t->inp_vflag & INP_IPV6PROTO) != 0)) {
287 				return (EADDRINUSE);
288 			}
289 		}
290 #endif
291 	}
292 	return (0);
293 }
294 
295 int
296 in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred)
297 {
298 	struct socket *so = inp->inp_socket;
299 	u_short	lport = 0;
300 	int error, lookupflags, sooptions;
301 
302 	INP_WLOCK_ASSERT(inp);
303 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
304 
305 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
306 		return (EINVAL);
307 
308 	lookupflags = 0;
309 	sooptions = atomic_load_int(&so->so_options);
310 	if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0)
311 		lookupflags = INPLOOKUP_WILDCARD;
312 	if (sin6 == NULL) {
313 		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
314 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
315 			return (error);
316 	} else {
317 		KASSERT(sin6->sin6_family == AF_INET6,
318 		    ("%s: invalid address family for %p", __func__, sin6));
319 		KASSERT(sin6->sin6_len == sizeof(*sin6),
320 		    ("%s: invalid address length for %p", __func__, sin6));
321 
322 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
323 			return(error);
324 
325 		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
326 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
327 			return (error);
328 
329 		/* See if this address/port combo is available. */
330 		error = in6_pcbbind_avail(inp, sin6, sooptions, lookupflags,
331 		    cred);
332 		if (error != 0)
333 			return (error);
334 
335 		lport = sin6->sin6_port;
336 		inp->in6p_laddr = sin6->sin6_addr;
337 	}
338 	if (lport == 0) {
339 		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
340 			/* Undo an address bind that may have occurred. */
341 			inp->in6p_laddr = in6addr_any;
342 			return (error);
343 		}
344 	} else {
345 		inp->inp_lport = lport;
346 		if (in_pcbinshash(inp) != 0) {
347 			inp->in6p_laddr = in6addr_any;
348 			inp->inp_lport = 0;
349 			return (EAGAIN);
350 		}
351 	}
352 	return (0);
353 }
354 
355 /*
356  *   Transform old in6_pcbconnect() into an inner subroutine for new
357  *   in6_pcbconnect(): Do some validity-checking on the remote
358  *   address (in mbuf 'nam') and then determine local host address
359  *   (i.e., which interface) to use to access that remote host.
360  *
361  *   This preserves definition of in6_pcbconnect(), while supporting a
362  *   slightly different version for T/TCP.  (This is more than
363  *   a bit of a kludge, but cleaning up the internal interfaces would
364  *   have forced minor changes in every protocol).
365  */
366 static int
367 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
368     struct in6_addr *plocal_addr6, bool sas_required)
369 {
370 	int error = 0;
371 	int scope_ambiguous = 0;
372 	struct in6_addr in6a;
373 
374 	NET_EPOCH_ASSERT();
375 	INP_WLOCK_ASSERT(inp);
376 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);	/* XXXRW: why? */
377 
378 	if (sin6->sin6_port == 0)
379 		return (EADDRNOTAVAIL);
380 
381 	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
382 		scope_ambiguous = 1;
383 	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
384 		return(error);
385 
386 	if (V_connect_in6addr_wild && !CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
387 		/*
388 		 * If the destination address is UNSPECIFIED addr,
389 		 * use the loopback addr, e.g ::1.
390 		 */
391 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
392 			sin6->sin6_addr = in6addr_loopback;
393 	} else if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
394 		return (ENETUNREACH);
395 	}
396 
397 	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
398 		return (error);
399 
400 	if (sas_required) {
401 		error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
402 		    inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
403 		if (error)
404 			return (error);
405 	} else {
406 		/*
407 		 * Source address selection isn't required when syncache
408 		 * has already established connection and both source and
409 		 * destination addresses was chosen.
410 		 *
411 		 * This also includes the case when fwd_tag was used to
412 		 * select source address in tcp_input().
413 		 */
414 		in6a = inp->in6p_laddr;
415 	}
416 
417 	if (IN6_IS_ADDR_UNSPECIFIED(&in6a))
418 		return (EHOSTUNREACH);
419 	/*
420 	 * Do not update this earlier, in case we return with an error.
421 	 *
422 	 * XXX: this in6_selectsrc_socket result might replace the bound local
423 	 * address with the address specified by setsockopt(IPV6_PKTINFO).
424 	 * Is it the intended behavior?
425 	 */
426 	*plocal_addr6 = in6a;
427 
428 	/*
429 	 * Don't do pcblookup call here; return interface in
430 	 * plocal_addr6
431 	 * and exit to caller, that will do the lookup.
432 	 */
433 
434 	return (0);
435 }
436 
437 /*
438  * Outer subroutine:
439  * Connect from a socket to a specified address.
440  * Both address and port must be specified in argument sin.
441  * If don't have a local address for this socket yet,
442  * then pick one.
443  */
444 int
445 in6_pcbconnect(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred,
446     bool sas_required)
447 {
448 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
449 	struct sockaddr_in6 laddr6;
450 	int error;
451 
452 	NET_EPOCH_ASSERT();
453 	INP_WLOCK_ASSERT(inp);
454 	INP_HASH_WLOCK_ASSERT(pcbinfo);
455 	KASSERT(sin6->sin6_family == AF_INET6,
456 	    ("%s: invalid address family for %p", __func__, sin6));
457 	KASSERT(sin6->sin6_len == sizeof(*sin6),
458 	    ("%s: invalid address length for %p", __func__, sin6));
459 	KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr),
460 	    ("%s: inp is already connected", __func__));
461 
462 	bzero(&laddr6, sizeof(laddr6));
463 	laddr6.sin6_family = AF_INET6;
464 
465 #ifdef ROUTE_MPATH
466 	if (CALC_FLOWID_OUTBOUND) {
467 		uint32_t hash_type, hash_val;
468 
469 		hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
470 		    &sin6->sin6_addr, 0, sin6->sin6_port,
471 		    inp->inp_socket->so_proto->pr_protocol, &hash_type);
472 		inp->inp_flowid = hash_val;
473 		inp->inp_flowtype = hash_type;
474 	}
475 #endif
476 	/*
477 	 * Call inner routine, to assign local interface address.
478 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
479 	 */
480 	if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr,
481 	    sas_required)) != 0)
482 		return (error);
483 
484 	if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
485 	    sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
486 	    &laddr6.sin6_addr : &inp->in6p_laddr, inp->inp_lport, 0,
487 	    M_NODOM) != NULL)
488 		return (EADDRINUSE);
489 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
490 		if (inp->inp_lport == 0) {
491 			error = in_pcb_lport_dest(inp,
492 			    (struct sockaddr *) &laddr6, &inp->inp_lport,
493 			    (struct sockaddr *) sin6, sin6->sin6_port, cred,
494 			    INPLOOKUP_WILDCARD);
495 			if (error)
496 				return (error);
497 		}
498 		inp->in6p_laddr = laddr6.sin6_addr;
499 	}
500 	inp->in6p_faddr = sin6->sin6_addr;
501 	inp->inp_fport = sin6->sin6_port;
502 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
503 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
504 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
505 		inp->inp_flow |=
506 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
507 
508 	if ((inp->inp_flags & INP_INHASHLIST) != 0) {
509 		in_pcbrehash(inp);
510 	} else {
511 		in_pcbinshash(inp);
512 	}
513 
514 	return (0);
515 }
516 
517 void
518 in6_pcbdisconnect(struct inpcb *inp)
519 {
520 
521 	INP_WLOCK_ASSERT(inp);
522 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
523 	KASSERT(inp->inp_smr == SMR_SEQ_INVALID,
524 	    ("%s: inp %p was already disconnected", __func__, inp));
525 
526 	in_pcbremhash_locked(inp);
527 
528 	/* See the comment in in_pcbinshash(). */
529 	inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr);
530 
531 	/* XXX-MJ torn writes are visible to SMR lookup */
532 	memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
533 	memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr));
534 	inp->inp_fport = 0;
535 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
536 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
537 }
538 
539 int
540 in6_getsockaddr(struct socket *so, struct sockaddr *sa)
541 {
542 	struct inpcb *inp;
543 
544 	inp = sotoinpcb(so);
545 	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
546 
547 	*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
548 		.sin6_len = sizeof(struct sockaddr_in6),
549 		.sin6_family = AF_INET6,
550 		.sin6_port = inp->inp_lport,
551 		.sin6_addr = inp->in6p_laddr,
552 	};
553 	/* XXX: should catch errors */
554 	(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
555 
556 	return (0);
557 }
558 
559 int
560 in6_getpeeraddr(struct socket *so, struct sockaddr *sa)
561 {
562 	struct inpcb *inp;
563 
564 	inp = sotoinpcb(so);
565 	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
566 
567 	*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
568 		.sin6_len = sizeof(struct sockaddr_in6),
569 		.sin6_family = AF_INET6,
570 		.sin6_port = inp->inp_fport,
571 		.sin6_addr = inp->in6p_faddr,
572 	};
573 	/* XXX: should catch errors */
574 	(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
575 
576 	return (0);
577 }
578 
579 int
580 in6_mapped_sockaddr(struct socket *so, struct sockaddr *sa)
581 {
582 	int	error;
583 #ifdef INET
584 	struct	inpcb *inp;
585 
586 	inp = sotoinpcb(so);
587 	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
588 
589 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
590 		struct sockaddr_in sin;
591 
592 		error = in_getsockaddr(so, (struct sockaddr *)&sin);
593 		if (error == 0)
594 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
595 	} else
596 #endif
597 	{
598 		/* scope issues will be handled in in6_getsockaddr(). */
599 		error = in6_getsockaddr(so, sa);
600 	}
601 
602 	return error;
603 }
604 
605 int
606 in6_mapped_peeraddr(struct socket *so, struct sockaddr *sa)
607 {
608 	int	error;
609 #ifdef INET
610 	struct	inpcb *inp;
611 
612 	inp = sotoinpcb(so);
613 	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
614 
615 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
616 		struct sockaddr_in sin;
617 
618 		error = in_getpeeraddr(so, (struct sockaddr *)&sin);
619 		if (error == 0)
620 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
621 	} else
622 #endif
623 	{
624 		/* scope issues will be handled in in6_getpeeraddr(). */
625 		error = in6_getpeeraddr(so, sa);
626 	}
627 
628 	return error;
629 }
630 
631 /*
632  * Pass some notification to all connections of a protocol
633  * associated with address dst.  The local address and/or port numbers
634  * may be specified to limit the search.  The "usual action" will be
635  * taken, depending on the ctlinput cmd.  The caller must filter any
636  * cmds that are uninteresting (e.g., no error in the map).
637  * Call the protocol specific routine (if any) to report
638  * any errors for each matching socket.
639  */
640 static bool
641 inp_match6(const struct inpcb *inp, void *v __unused)
642 {
643 
644 	return ((inp->inp_vflag & INP_IPV6) != 0);
645 }
646 
647 void
648 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst,
649     u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg,
650     int errno, void *cmdarg,
651     struct inpcb *(*notify)(struct inpcb *, int))
652 {
653 	struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB,
654 	    inp_match6, NULL);
655 	struct inpcb *inp;
656 	struct sockaddr_in6 sa6_src;
657 	u_short	fport = fport_arg, lport = lport_arg;
658 	u_int32_t flowinfo;
659 
660 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
661 		return;
662 
663 	/*
664 	 * note that src can be NULL when we get notify by local fragmentation.
665 	 */
666 	sa6_src = (src == NULL) ? sa6_any : *src;
667 	flowinfo = sa6_src.sin6_flowinfo;
668 
669 	while ((inp = inp_next(&inpi)) != NULL) {
670 		INP_WLOCK_ASSERT(inp);
671 		/*
672 		 * If the error designates a new path MTU for a destination
673 		 * and the application (associated with this socket) wanted to
674 		 * know the value, notify.
675 		 * XXX: should we avoid to notify the value to TCP sockets?
676 		 */
677 		if (errno == EMSGSIZE && cmdarg != NULL)
678 			ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg);
679 
680 		/*
681 		 * Detect if we should notify the error. If no source and
682 		 * destination ports are specified, but non-zero flowinfo and
683 		 * local address match, notify the error. This is the case
684 		 * when the error is delivered with an encrypted buffer
685 		 * by ESP. Otherwise, just compare addresses and ports
686 		 * as usual.
687 		 */
688 		if (lport == 0 && fport == 0 && flowinfo &&
689 		    inp->inp_socket != NULL &&
690 		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
691 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
692 			goto do_notify;
693 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
694 					     &sa6_dst->sin6_addr) ||
695 			 inp->inp_socket == 0 ||
696 			 (lport && inp->inp_lport != lport) ||
697 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
698 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
699 					      &sa6_src.sin6_addr)) ||
700 			 (fport && inp->inp_fport != fport)) {
701 			continue;
702 		}
703 
704 	  do_notify:
705 		if (notify)
706 			(*notify)(inp, errno);
707 	}
708 }
709 
710 /*
711  * Lookup a PCB based on the local address and port.  Caller must hold the
712  * hash lock.  No inpcb locks or references are acquired.
713  */
714 struct inpcb *
715 in6_pcblookup_local(struct inpcbinfo *pcbinfo, const struct in6_addr *laddr,
716     u_short lport, int lookupflags, struct ucred *cred)
717 {
718 	struct inpcb *inp;
719 	int matchwild = 3, wildcard;
720 
721 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
722 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
723 
724 	INP_HASH_LOCK_ASSERT(pcbinfo);
725 
726 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
727 		struct inpcbhead *head;
728 		/*
729 		 * Look for an unconnected (wildcard foreign addr) PCB that
730 		 * matches the local address and port we're looking for.
731 		 */
732 		head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
733 		    pcbinfo->ipi_hashmask)];
734 		CK_LIST_FOREACH(inp, head, inp_hash_wild) {
735 			/* XXX inp locking */
736 			if ((inp->inp_vflag & INP_IPV6) == 0)
737 				continue;
738 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
739 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
740 			    inp->inp_lport == lport) {
741 				/* Found. */
742 				if (prison_equal_ip6(cred->cr_prison,
743 				    inp->inp_cred->cr_prison))
744 					return (inp);
745 			}
746 		}
747 		/*
748 		 * Not found.
749 		 */
750 		return (NULL);
751 	} else {
752 		struct inpcbporthead *porthash;
753 		struct inpcbport *phd;
754 		struct inpcb *match = NULL;
755 		/*
756 		 * Best fit PCB lookup.
757 		 *
758 		 * First see if this local port is in use by looking on the
759 		 * port hash list.
760 		 */
761 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
762 		    pcbinfo->ipi_porthashmask)];
763 		CK_LIST_FOREACH(phd, porthash, phd_hash) {
764 			if (phd->phd_port == lport)
765 				break;
766 		}
767 		if (phd != NULL) {
768 			/*
769 			 * Port is in use by one or more PCBs. Look for best
770 			 * fit.
771 			 */
772 			CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
773 				wildcard = 0;
774 				if (!prison_equal_ip6(cred->cr_prison,
775 				    inp->inp_cred->cr_prison))
776 					continue;
777 				/* XXX inp locking */
778 				if ((inp->inp_vflag & INP_IPV6) == 0)
779 					continue;
780 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
781 					wildcard++;
782 				if (!IN6_IS_ADDR_UNSPECIFIED(
783 					&inp->in6p_laddr)) {
784 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
785 						wildcard++;
786 					else if (!IN6_ARE_ADDR_EQUAL(
787 					    &inp->in6p_laddr, laddr))
788 						continue;
789 				} else {
790 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
791 						wildcard++;
792 				}
793 				if (wildcard < matchwild) {
794 					match = inp;
795 					matchwild = wildcard;
796 					if (matchwild == 0)
797 						break;
798 				}
799 			}
800 		}
801 		return (match);
802 	}
803 }
804 
805 static bool
806 in6_multi_match(const struct inpcb *inp, void *v __unused)
807 {
808 
809 	if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL)
810 		return (true);
811 	else
812 		return (false);
813 }
814 
815 void
816 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
817 {
818 	struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB,
819 	    in6_multi_match, NULL);
820 	struct inpcb *inp;
821 	struct in6_multi *inm;
822 	struct in6_mfilter *imf;
823 	struct ip6_moptions *im6o;
824 
825 	IN6_MULTI_LOCK_ASSERT();
826 
827 	while ((inp = inp_next(&inpi)) != NULL) {
828 		INP_RLOCK_ASSERT(inp);
829 
830 		im6o = inp->in6p_moptions;
831 		/*
832 		 * Unselect the outgoing ifp for multicast if it
833 		 * is being detached.
834 		 */
835 		if (im6o->im6o_multicast_ifp == ifp)
836 			im6o->im6o_multicast_ifp = NULL;
837 		/*
838 		 * Drop multicast group membership if we joined
839 		 * through the interface being detached.
840 		 */
841 restart:
842 		IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
843 			if ((inm = imf->im6f_in6m) == NULL)
844 				continue;
845 			if (inm->in6m_ifp != ifp)
846 				continue;
847 			ip6_mfilter_remove(&im6o->im6o_head, imf);
848 			in6_leavegroup_locked(inm, NULL);
849 			ip6_mfilter_free(imf);
850 			goto restart;
851 		}
852 	}
853 }
854 
855 /*
856  * Check for alternatives when higher level complains
857  * about service problems.  For now, invalidate cached
858  * routing information.  If the route was created dynamically
859  * (by a redirect), time to try a default gateway again.
860  */
861 void
862 in6_losing(struct inpcb *inp)
863 {
864 
865 	RO_INVALIDATE_CACHE(&inp->inp_route6);
866 }
867 
868 /*
869  * After a routing change, flush old routing
870  * and allocate a (hopefully) better one.
871  */
872 struct inpcb *
873 in6_rtchange(struct inpcb *inp, int errno __unused)
874 {
875 
876 	RO_INVALIDATE_CACHE(&inp->inp_route6);
877 	return inp;
878 }
879 
880 static bool
881 in6_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain)
882 {
883 	return (domain == M_NODOM || domain == grp->il_numa_domain);
884 }
885 
886 static struct inpcb *
887 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
888     const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr,
889     uint16_t lport, uint8_t domain)
890 {
891 	const struct inpcblbgrouphead *hdr;
892 	struct inpcblbgroup *grp;
893 	struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild;
894 	struct inpcb *inp;
895 	u_int count;
896 
897 	INP_HASH_LOCK_ASSERT(pcbinfo);
898 
899 	hdr = &pcbinfo->ipi_lbgrouphashbase[
900 	    INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
901 
902 	/*
903 	 * Search for an LB group match based on the following criteria:
904 	 * - prefer jailed groups to non-jailed groups
905 	 * - prefer exact source address matches to wildcard matches
906 	 * - prefer groups bound to the specified NUMA domain
907 	 */
908 	jail_exact = jail_wild = local_exact = local_wild = NULL;
909 	CK_LIST_FOREACH(grp, hdr, il_list) {
910 		bool injail;
911 
912 #ifdef INET
913 		if (!(grp->il_vflag & INP_IPV6))
914 			continue;
915 #endif
916 		if (grp->il_lport != lport)
917 			continue;
918 
919 		injail = prison_flag(grp->il_cred, PR_IP6) != 0;
920 		if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison,
921 		    laddr) != 0)
922 			continue;
923 
924 		if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
925 			if (injail) {
926 				jail_exact = grp;
927 				if (in6_pcblookup_lb_numa_match(grp, domain))
928 					/* This is a perfect match. */
929 					goto out;
930 			} else if (local_exact == NULL ||
931 			    in6_pcblookup_lb_numa_match(grp, domain)) {
932 				local_exact = grp;
933 			}
934 		} else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) {
935 			if (injail) {
936 				if (jail_wild == NULL ||
937 				    in6_pcblookup_lb_numa_match(grp, domain))
938 					jail_wild = grp;
939 			} else if (local_wild == NULL ||
940 			    in6_pcblookup_lb_numa_match(grp, domain)) {
941 				local_wild = grp;
942 			}
943 		}
944 	}
945 
946 	if (jail_exact != NULL)
947 		grp = jail_exact;
948 	else if (jail_wild != NULL)
949 		grp = jail_wild;
950 	else if (local_exact != NULL)
951 		grp = local_exact;
952 	else
953 		grp = local_wild;
954 	if (grp == NULL)
955 		return (NULL);
956 out:
957 	/*
958 	 * Synchronize with in_pcblbgroup_insert().
959 	 */
960 	count = atomic_load_acq_int(&grp->il_inpcnt);
961 	if (count == 0)
962 		return (NULL);
963 	inp = grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) % count];
964 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
965 	return (inp);
966 }
967 
968 static bool
969 in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr,
970     u_short fport, const struct in6_addr *laddr, u_short lport)
971 {
972 	/* XXX inp locking */
973 	if ((inp->inp_vflag & INP_IPV6) == 0)
974 		return (false);
975 	if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
976 	    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
977 	    inp->inp_fport == fport && inp->inp_lport == lport)
978 		return (true);
979 	return (false);
980 }
981 
982 static struct inpcb *
983 in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo,
984     const struct in6_addr *faddr, u_short fport,
985     const struct in6_addr *laddr, u_short lport)
986 {
987 	struct inpcbhead *head;
988 	struct inpcb *inp;
989 
990 	INP_HASH_LOCK_ASSERT(pcbinfo);
991 
992 	/*
993 	 * First look for an exact match.
994 	 */
995 	head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport,
996 	    pcbinfo->ipi_hashmask)];
997 	CK_LIST_FOREACH(inp, head, inp_hash_exact) {
998 		if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport))
999 			return (inp);
1000 	}
1001 	return (NULL);
1002 }
1003 
1004 typedef enum {
1005 	INPLOOKUP_MATCH_NONE = 0,
1006 	INPLOOKUP_MATCH_WILD = 1,
1007 	INPLOOKUP_MATCH_LADDR = 2,
1008 } inp_lookup_match_t;
1009 
1010 static inp_lookup_match_t
1011 in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr,
1012     u_short lport)
1013 {
1014 	/* XXX inp locking */
1015 	if ((inp->inp_vflag & INP_IPV6) == 0)
1016 		return (INPLOOKUP_MATCH_NONE);
1017 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1018 	    inp->inp_lport != lport)
1019 		return (INPLOOKUP_MATCH_NONE);
1020 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1021 		return (INPLOOKUP_MATCH_WILD);
1022 	if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))
1023 		return (INPLOOKUP_MATCH_LADDR);
1024 	return (INPLOOKUP_MATCH_NONE);
1025 }
1026 
1027 #define	INP_LOOKUP_AGAIN	((struct inpcb *)(uintptr_t)-1)
1028 
1029 static struct inpcb *
1030 in6_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo,
1031     const struct in6_addr *laddr, u_short lport, const inp_lookup_t lockflags)
1032 {
1033 	struct inpcbhead *head;
1034 	struct inpcb *inp;
1035 
1036 	KASSERT(SMR_ENTERED(pcbinfo->ipi_smr),
1037 	    ("%s: not in SMR read section", __func__));
1038 
1039 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
1040 	    pcbinfo->ipi_hashmask)];
1041 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
1042 		inp_lookup_match_t match;
1043 
1044 		match = in6_pcblookup_wild_match(inp, laddr, lport);
1045 		if (match == INPLOOKUP_MATCH_NONE)
1046 			continue;
1047 
1048 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
1049 			match = in6_pcblookup_wild_match(inp, laddr, lport);
1050 			if (match != INPLOOKUP_MATCH_NONE &&
1051 			    prison_check_ip6_locked(inp->inp_cred->cr_prison,
1052 			    laddr) == 0)
1053 				return (inp);
1054 			inp_unlock(inp, lockflags);
1055 		}
1056 
1057 		/*
1058 		 * The matching socket disappeared out from under us.  Fall back
1059 		 * to a serialized lookup.
1060 		 */
1061 		return (INP_LOOKUP_AGAIN);
1062 	}
1063 	return (NULL);
1064 }
1065 
1066 static struct inpcb *
1067 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo,
1068     const struct in6_addr *laddr, u_short lport)
1069 {
1070 	struct inpcbhead *head;
1071 	struct inpcb *inp, *jail_wild, *local_exact, *local_wild;
1072 
1073 	INP_HASH_LOCK_ASSERT(pcbinfo);
1074 
1075 	/*
1076 	 * Order of socket selection - we always prefer jails.
1077 	 *      1. jailed, non-wild.
1078 	 *      2. jailed, wild.
1079 	 *      3. non-jailed, non-wild.
1080 	 *      4. non-jailed, wild.
1081 	 */
1082 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
1083 	    pcbinfo->ipi_hashmask)];
1084 	local_wild = local_exact = jail_wild = NULL;
1085 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
1086 		inp_lookup_match_t match;
1087 		bool injail;
1088 
1089 		match = in6_pcblookup_wild_match(inp, laddr, lport);
1090 		if (match == INPLOOKUP_MATCH_NONE)
1091 			continue;
1092 
1093 		injail = prison_flag(inp->inp_cred, PR_IP6) != 0;
1094 		if (injail) {
1095 			if (prison_check_ip6_locked(
1096 			    inp->inp_cred->cr_prison, laddr) != 0)
1097 				continue;
1098 		} else {
1099 			if (local_exact != NULL)
1100 				continue;
1101 		}
1102 
1103 		if (match == INPLOOKUP_MATCH_LADDR) {
1104 			if (injail)
1105 				return (inp);
1106 			else
1107 				local_exact = inp;
1108 		} else {
1109 			if (injail)
1110 				jail_wild = inp;
1111 			else
1112 				local_wild = inp;
1113 		}
1114 	}
1115 
1116 	if (jail_wild != NULL)
1117 		return (jail_wild);
1118 	if (local_exact != NULL)
1119 		return (local_exact);
1120 	if (local_wild != NULL)
1121 		return (local_wild);
1122 	return (NULL);
1123 }
1124 
1125 struct inpcb *
1126 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
1127     const struct in6_addr *faddr, u_int fport_arg,
1128     const struct in6_addr *laddr, u_int lport_arg,
1129     int lookupflags, uint8_t numa_domain)
1130 {
1131 	struct inpcb *inp;
1132 	u_short fport = fport_arg, lport = lport_arg;
1133 
1134 	KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0,
1135 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1136 	KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr),
1137 	    ("%s: invalid foreign address", __func__));
1138 	KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr),
1139 	    ("%s: invalid local address", __func__));
1140 	INP_HASH_LOCK_ASSERT(pcbinfo);
1141 
1142 	inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1143 	if (inp != NULL)
1144 		return (inp);
1145 
1146 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1147 		inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr,
1148 		    lport, numa_domain);
1149 		if (inp == NULL) {
1150 			inp = in6_pcblookup_hash_wild_locked(pcbinfo,
1151 			    laddr, lport);
1152 		}
1153 	}
1154 	return (inp);
1155 }
1156 
1157 static struct inpcb *
1158 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1159     u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1160     uint8_t numa_domain)
1161 {
1162 	struct inpcb *inp;
1163 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1164 
1165 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1166 	    ("%s: LOCKPCB not set", __func__));
1167 
1168 	INP_HASH_WLOCK(pcbinfo);
1169 	inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1170 	    lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain);
1171 	if (inp != NULL && !inp_trylock(inp, lockflags)) {
1172 		in_pcbref(inp);
1173 		INP_HASH_WUNLOCK(pcbinfo);
1174 		inp_lock(inp, lockflags);
1175 		if (in_pcbrele(inp, lockflags))
1176 			/* XXX-MJ or retry until we get a negative match? */
1177 			inp = NULL;
1178 	} else {
1179 		INP_HASH_WUNLOCK(pcbinfo);
1180 	}
1181 	return (inp);
1182 }
1183 
1184 static struct inpcb *
1185 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1186     u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg,
1187     int lookupflags, uint8_t numa_domain)
1188 {
1189 	struct inpcb *inp;
1190 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1191 	const u_short fport = fport_arg, lport = lport_arg;
1192 
1193 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1194 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1195 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1196 	    ("%s: LOCKPCB not set", __func__));
1197 
1198 	smr_enter(pcbinfo->ipi_smr);
1199 	inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1200 	if (inp != NULL) {
1201 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
1202 			if (__predict_true(in6_pcblookup_exact_match(inp,
1203 			    faddr, fport, laddr, lport)))
1204 				return (inp);
1205 			inp_unlock(inp, lockflags);
1206 		}
1207 		/*
1208 		 * We failed to lock the inpcb, or its connection state changed
1209 		 * out from under us.  Fall back to a precise search.
1210 		 */
1211 		return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1212 		    lookupflags, numa_domain));
1213 	}
1214 
1215 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1216 		inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport,
1217 		    laddr, lport, numa_domain);
1218 		if (inp != NULL) {
1219 			if (__predict_true(inp_smr_lock(inp, lockflags))) {
1220 				if (__predict_true(in6_pcblookup_wild_match(inp,
1221 				    laddr, lport) != INPLOOKUP_MATCH_NONE))
1222 					return (inp);
1223 				inp_unlock(inp, lockflags);
1224 			}
1225 			inp = INP_LOOKUP_AGAIN;
1226 		} else {
1227 			inp = in6_pcblookup_hash_wild_smr(pcbinfo, laddr, lport,
1228 			    lockflags);
1229 		}
1230 		if (inp == INP_LOOKUP_AGAIN) {
1231 			return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr,
1232 			    lport, lookupflags, numa_domain));
1233 		}
1234 	}
1235 
1236 	if (inp == NULL)
1237 		smr_exit(pcbinfo->ipi_smr);
1238 
1239 	return (inp);
1240 }
1241 
1242 /*
1243  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1244  * from which a pre-calculated hash value may be extracted.
1245  */
1246 struct inpcb *
1247 in6_pcblookup(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1248     u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1249     struct ifnet *ifp __unused)
1250 {
1251 	return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1252 	    lookupflags, M_NODOM));
1253 }
1254 
1255 struct inpcb *
1256 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1257     u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1258     struct ifnet *ifp __unused, struct mbuf *m)
1259 {
1260 	return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1261 	    lookupflags, m->m_pkthdr.numa_domain));
1262 }
1263 
1264 void
1265 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1266 {
1267 	struct ip6_hdr *ip;
1268 
1269 	ip = mtod(m, struct ip6_hdr *);
1270 	bzero(sin6, sizeof(*sin6));
1271 	sin6->sin6_len = sizeof(*sin6);
1272 	sin6->sin6_family = AF_INET6;
1273 	sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1274 
1275 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1276 
1277 	return;
1278 }
1279