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