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