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