xref: /freebsd/sys/netinet6/in6_pcb.c (revision c5405d1c850765d04f74067ebb71f57e9a26b8ea)
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 struct sockaddr *
505 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
506 {
507 	struct sockaddr_in6 *sin6;
508 
509 	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
510 	bzero(sin6, sizeof *sin6);
511 	sin6->sin6_family = AF_INET6;
512 	sin6->sin6_len = sizeof(*sin6);
513 	sin6->sin6_port = port;
514 	sin6->sin6_addr = *addr_p;
515 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
516 
517 	return (struct sockaddr *)sin6;
518 }
519 
520 struct sockaddr *
521 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
522 {
523 	struct sockaddr_in sin;
524 	struct sockaddr_in6 *sin6_p;
525 
526 	bzero(&sin, sizeof sin);
527 	sin.sin_family = AF_INET;
528 	sin.sin_len = sizeof(sin);
529 	sin.sin_port = port;
530 	sin.sin_addr = *addr_p;
531 
532 	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
533 		M_WAITOK);
534 	in6_sin_2_v4mapsin6(&sin, sin6_p);
535 
536 	return (struct sockaddr *)sin6_p;
537 }
538 
539 int
540 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
541 {
542 	struct inpcb *inp;
543 	struct in6_addr addr;
544 	in_port_t port;
545 
546 	inp = sotoinpcb(so);
547 	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
548 
549 	INP_RLOCK(inp);
550 	port = inp->inp_lport;
551 	addr = inp->in6p_laddr;
552 	INP_RUNLOCK(inp);
553 
554 	*nam = in6_sockaddr(port, &addr);
555 	return 0;
556 }
557 
558 int
559 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
560 {
561 	struct inpcb *inp;
562 	struct in6_addr addr;
563 	in_port_t port;
564 
565 	inp = sotoinpcb(so);
566 	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
567 
568 	INP_RLOCK(inp);
569 	port = inp->inp_fport;
570 	addr = inp->in6p_faddr;
571 	INP_RUNLOCK(inp);
572 
573 	*nam = in6_sockaddr(port, &addr);
574 	return 0;
575 }
576 
577 int
578 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
579 {
580 	struct	inpcb *inp;
581 	int	error;
582 
583 	inp = sotoinpcb(so);
584 	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
585 
586 #ifdef INET
587 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
588 		error = in_getsockaddr(so, nam);
589 		if (error == 0)
590 			in6_sin_2_v4mapsin6_in_sock(nam);
591 	} else
592 #endif
593 	{
594 		/* scope issues will be handled in in6_getsockaddr(). */
595 		error = in6_getsockaddr(so, nam);
596 	}
597 
598 	return error;
599 }
600 
601 int
602 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
603 {
604 	struct	inpcb *inp;
605 	int	error;
606 
607 	inp = sotoinpcb(so);
608 	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
609 
610 #ifdef INET
611 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
612 		error = in_getpeeraddr(so, nam);
613 		if (error == 0)
614 			in6_sin_2_v4mapsin6_in_sock(nam);
615 	} else
616 #endif
617 	/* scope issues will be handled in in6_getpeeraddr(). */
618 	error = in6_getpeeraddr(so, nam);
619 
620 	return error;
621 }
622 
623 /*
624  * Pass some notification to all connections of a protocol
625  * associated with address dst.  The local address and/or port numbers
626  * may be specified to limit the search.  The "usual action" will be
627  * taken, depending on the ctlinput cmd.  The caller must filter any
628  * cmds that are uninteresting (e.g., no error in the map).
629  * Call the protocol specific routine (if any) to report
630  * any errors for each matching socket.
631  */
632 static bool
633 inp_match6(const struct inpcb *inp, void *v __unused)
634 {
635 
636 	return ((inp->inp_vflag & INP_IPV6) != 0);
637 }
638 
639 void
640 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst,
641     u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg,
642     int errno, void *cmdarg,
643     struct inpcb *(*notify)(struct inpcb *, int))
644 {
645 	struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB,
646 	    inp_match6, NULL);
647 	struct inpcb *inp;
648 	struct sockaddr_in6 sa6_src;
649 	u_short	fport = fport_arg, lport = lport_arg;
650 	u_int32_t flowinfo;
651 
652 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
653 		return;
654 
655 	/*
656 	 * note that src can be NULL when we get notify by local fragmentation.
657 	 */
658 	sa6_src = (src == NULL) ? sa6_any : *src;
659 	flowinfo = sa6_src.sin6_flowinfo;
660 
661 	while ((inp = inp_next(&inpi)) != NULL) {
662 		INP_WLOCK_ASSERT(inp);
663 		/*
664 		 * If the error designates a new path MTU for a destination
665 		 * and the application (associated with this socket) wanted to
666 		 * know the value, notify.
667 		 * XXX: should we avoid to notify the value to TCP sockets?
668 		 */
669 		if (errno == EMSGSIZE && cmdarg != NULL)
670 			ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg);
671 
672 		/*
673 		 * Detect if we should notify the error. If no source and
674 		 * destination ports are specified, but non-zero flowinfo and
675 		 * local address match, notify the error. This is the case
676 		 * when the error is delivered with an encrypted buffer
677 		 * by ESP. Otherwise, just compare addresses and ports
678 		 * as usual.
679 		 */
680 		if (lport == 0 && fport == 0 && flowinfo &&
681 		    inp->inp_socket != NULL &&
682 		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
683 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
684 			goto do_notify;
685 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
686 					     &sa6_dst->sin6_addr) ||
687 			 inp->inp_socket == 0 ||
688 			 (lport && inp->inp_lport != lport) ||
689 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
690 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
691 					      &sa6_src.sin6_addr)) ||
692 			 (fport && inp->inp_fport != fport)) {
693 			continue;
694 		}
695 
696 	  do_notify:
697 		if (notify)
698 			(*notify)(inp, errno);
699 	}
700 }
701 
702 /*
703  * Lookup a PCB based on the local address and port.  Caller must hold the
704  * hash lock.  No inpcb locks or references are acquired.
705  */
706 struct inpcb *
707 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
708     u_short lport, int lookupflags, struct ucred *cred)
709 {
710 	struct inpcb *inp;
711 	int matchwild = 3, wildcard;
712 
713 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
714 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
715 
716 	INP_HASH_LOCK_ASSERT(pcbinfo);
717 
718 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
719 		struct inpcbhead *head;
720 		/*
721 		 * Look for an unconnected (wildcard foreign addr) PCB that
722 		 * matches the local address and port we're looking for.
723 		 */
724 		head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
725 		    pcbinfo->ipi_hashmask)];
726 		CK_LIST_FOREACH(inp, head, inp_hash_wild) {
727 			/* XXX inp locking */
728 			if ((inp->inp_vflag & INP_IPV6) == 0)
729 				continue;
730 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
731 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
732 			    inp->inp_lport == lport) {
733 				/* Found. */
734 				if (prison_equal_ip6(cred->cr_prison,
735 				    inp->inp_cred->cr_prison))
736 					return (inp);
737 			}
738 		}
739 		/*
740 		 * Not found.
741 		 */
742 		return (NULL);
743 	} else {
744 		struct inpcbporthead *porthash;
745 		struct inpcbport *phd;
746 		struct inpcb *match = NULL;
747 		/*
748 		 * Best fit PCB lookup.
749 		 *
750 		 * First see if this local port is in use by looking on the
751 		 * port hash list.
752 		 */
753 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
754 		    pcbinfo->ipi_porthashmask)];
755 		CK_LIST_FOREACH(phd, porthash, phd_hash) {
756 			if (phd->phd_port == lport)
757 				break;
758 		}
759 		if (phd != NULL) {
760 			/*
761 			 * Port is in use by one or more PCBs. Look for best
762 			 * fit.
763 			 */
764 			CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
765 				wildcard = 0;
766 				if (!prison_equal_ip6(cred->cr_prison,
767 				    inp->inp_cred->cr_prison))
768 					continue;
769 				/* XXX inp locking */
770 				if ((inp->inp_vflag & INP_IPV6) == 0)
771 					continue;
772 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
773 					wildcard++;
774 				if (!IN6_IS_ADDR_UNSPECIFIED(
775 					&inp->in6p_laddr)) {
776 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
777 						wildcard++;
778 					else if (!IN6_ARE_ADDR_EQUAL(
779 					    &inp->in6p_laddr, laddr))
780 						continue;
781 				} else {
782 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
783 						wildcard++;
784 				}
785 				if (wildcard < matchwild) {
786 					match = inp;
787 					matchwild = wildcard;
788 					if (matchwild == 0)
789 						break;
790 				}
791 			}
792 		}
793 		return (match);
794 	}
795 }
796 
797 static bool
798 in6_multi_match(const struct inpcb *inp, void *v __unused)
799 {
800 
801 	if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL)
802 		return (true);
803 	else
804 		return (false);
805 }
806 
807 void
808 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
809 {
810 	struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB,
811 	    in6_multi_match, NULL);
812 	struct inpcb *inp;
813 	struct in6_multi *inm;
814 	struct in6_mfilter *imf;
815 	struct ip6_moptions *im6o;
816 
817 	IN6_MULTI_LOCK_ASSERT();
818 
819 	while ((inp = inp_next(&inpi)) != NULL) {
820 		INP_RLOCK_ASSERT(inp);
821 
822 		im6o = inp->in6p_moptions;
823 		/*
824 		 * Unselect the outgoing ifp for multicast if it
825 		 * is being detached.
826 		 */
827 		if (im6o->im6o_multicast_ifp == ifp)
828 			im6o->im6o_multicast_ifp = NULL;
829 		/*
830 		 * Drop multicast group membership if we joined
831 		 * through the interface being detached.
832 		 */
833 restart:
834 		IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
835 			if ((inm = imf->im6f_in6m) == NULL)
836 				continue;
837 			if (inm->in6m_ifp != ifp)
838 				continue;
839 			ip6_mfilter_remove(&im6o->im6o_head, imf);
840 			in6_leavegroup_locked(inm, NULL);
841 			ip6_mfilter_free(imf);
842 			goto restart;
843 		}
844 	}
845 }
846 
847 /*
848  * Check for alternatives when higher level complains
849  * about service problems.  For now, invalidate cached
850  * routing information.  If the route was created dynamically
851  * (by a redirect), time to try a default gateway again.
852  */
853 void
854 in6_losing(struct inpcb *inp)
855 {
856 
857 	RO_INVALIDATE_CACHE(&inp->inp_route6);
858 }
859 
860 /*
861  * After a routing change, flush old routing
862  * and allocate a (hopefully) better one.
863  */
864 struct inpcb *
865 in6_rtchange(struct inpcb *inp, int errno __unused)
866 {
867 
868 	RO_INVALIDATE_CACHE(&inp->inp_route6);
869 	return inp;
870 }
871 
872 static bool
873 in6_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain)
874 {
875 	return (domain == M_NODOM || domain == grp->il_numa_domain);
876 }
877 
878 static struct inpcb *
879 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
880     const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr,
881     uint16_t lport, uint8_t domain)
882 {
883 	const struct inpcblbgrouphead *hdr;
884 	struct inpcblbgroup *grp;
885 	struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild;
886 
887 	INP_HASH_LOCK_ASSERT(pcbinfo);
888 
889 	hdr = &pcbinfo->ipi_lbgrouphashbase[
890 	    INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
891 
892 	/*
893 	 * Search for an LB group match based on the following criteria:
894 	 * - prefer jailed groups to non-jailed groups
895 	 * - prefer exact source address matches to wildcard matches
896 	 * - prefer groups bound to the specified NUMA domain
897 	 */
898 	jail_exact = jail_wild = local_exact = local_wild = NULL;
899 	CK_LIST_FOREACH(grp, hdr, il_list) {
900 		bool injail;
901 
902 #ifdef INET
903 		if (!(grp->il_vflag & INP_IPV6))
904 			continue;
905 #endif
906 		if (grp->il_lport != lport)
907 			continue;
908 
909 		injail = prison_flag(grp->il_cred, PR_IP6) != 0;
910 		if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison,
911 		    laddr) != 0)
912 			continue;
913 
914 		if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
915 			if (injail) {
916 				jail_exact = grp;
917 				if (in6_pcblookup_lb_numa_match(grp, domain))
918 					/* This is a perfect match. */
919 					goto out;
920 			} else if (local_exact == NULL ||
921 			    in6_pcblookup_lb_numa_match(grp, domain)) {
922 				local_exact = grp;
923 			}
924 		} else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) {
925 			if (injail) {
926 				if (jail_wild == NULL ||
927 				    in6_pcblookup_lb_numa_match(grp, domain))
928 					jail_wild = grp;
929 			} else if (local_wild == NULL ||
930 			    in6_pcblookup_lb_numa_match(grp, domain)) {
931 				local_wild = grp;
932 			}
933 		}
934 	}
935 
936 	if (jail_exact != NULL)
937 		grp = jail_exact;
938 	else if (jail_wild != NULL)
939 		grp = jail_wild;
940 	else if (local_exact != NULL)
941 		grp = local_exact;
942 	else
943 		grp = local_wild;
944 	if (grp == NULL)
945 		return (NULL);
946 out:
947 	return (grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) %
948 	    grp->il_inpcnt]);
949 }
950 
951 static bool
952 in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr,
953     u_short fport, const struct in6_addr *laddr, u_short lport)
954 {
955 	/* XXX inp locking */
956 	if ((inp->inp_vflag & INP_IPV6) == 0)
957 		return (false);
958 	if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
959 	    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
960 	    inp->inp_fport == fport && inp->inp_lport == lport)
961 		return (true);
962 	return (false);
963 }
964 
965 static struct inpcb *
966 in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo,
967     const struct in6_addr *faddr, u_short fport,
968     const struct in6_addr *laddr, u_short lport)
969 {
970 	struct inpcbhead *head;
971 	struct inpcb *inp;
972 
973 	INP_HASH_LOCK_ASSERT(pcbinfo);
974 
975 	/*
976 	 * First look for an exact match.
977 	 */
978 	head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport,
979 	    pcbinfo->ipi_hashmask)];
980 	CK_LIST_FOREACH(inp, head, inp_hash_exact) {
981 		if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport))
982 			return (inp);
983 	}
984 	return (NULL);
985 }
986 
987 typedef enum {
988 	INPLOOKUP_MATCH_NONE = 0,
989 	INPLOOKUP_MATCH_WILD = 1,
990 	INPLOOKUP_MATCH_LADDR = 2,
991 } inp_lookup_match_t;
992 
993 static inp_lookup_match_t
994 in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr,
995     u_short lport)
996 {
997 	/* XXX inp locking */
998 	if ((inp->inp_vflag & INP_IPV6) == 0)
999 		return (INPLOOKUP_MATCH_NONE);
1000 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1001 	    inp->inp_lport != lport)
1002 		return (INPLOOKUP_MATCH_NONE);
1003 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1004 		return (INPLOOKUP_MATCH_WILD);
1005 	if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))
1006 		return (INPLOOKUP_MATCH_LADDR);
1007 	return (INPLOOKUP_MATCH_NONE);
1008 }
1009 
1010 #define	INP_LOOKUP_AGAIN	((struct inpcb *)(uintptr_t)-1)
1011 
1012 static struct inpcb *
1013 in6_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo,
1014     const struct in6_addr *faddr, u_short fport, const struct in6_addr *laddr,
1015     u_short lport, const inp_lookup_t lockflags)
1016 {
1017 	struct inpcbhead *head;
1018 	struct inpcb *inp;
1019 
1020 	KASSERT(SMR_ENTERED(pcbinfo->ipi_smr),
1021 	    ("%s: not in SMR read section", __func__));
1022 
1023 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
1024 	    pcbinfo->ipi_hashmask)];
1025 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
1026 		inp_lookup_match_t match;
1027 
1028 		match = in6_pcblookup_wild_match(inp, laddr, lport);
1029 		if (match == INPLOOKUP_MATCH_NONE)
1030 			continue;
1031 
1032 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
1033 			match = in6_pcblookup_wild_match(inp, laddr, lport);
1034 			if (match != INPLOOKUP_MATCH_NONE &&
1035 			    prison_check_ip6_locked(inp->inp_cred->cr_prison,
1036 			    laddr) == 0)
1037 				return (inp);
1038 			inp_unlock(inp, lockflags);
1039 		}
1040 
1041 		/*
1042 		 * The matching socket disappeared out from under us.  Fall back
1043 		 * to a serialized lookup.
1044 		 */
1045 		return (INP_LOOKUP_AGAIN);
1046 	}
1047 	return (NULL);
1048 }
1049 
1050 static struct inpcb *
1051 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo,
1052     const struct in6_addr *faddr, u_short fport, const struct in6_addr *laddr,
1053     u_short lport)
1054 {
1055 	struct inpcbhead *head;
1056 	struct inpcb *inp, *jail_wild, *local_exact, *local_wild;
1057 
1058 	INP_HASH_LOCK_ASSERT(pcbinfo);
1059 
1060 	/*
1061 	 * Order of socket selection - we always prefer jails.
1062 	 *      1. jailed, non-wild.
1063 	 *      2. jailed, wild.
1064 	 *      3. non-jailed, non-wild.
1065 	 *      4. non-jailed, wild.
1066 	 */
1067 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
1068 	    pcbinfo->ipi_hashmask)];
1069 	local_wild = local_exact = jail_wild = NULL;
1070 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
1071 		inp_lookup_match_t match;
1072 		bool injail;
1073 
1074 		match = in6_pcblookup_wild_match(inp, laddr, lport);
1075 		if (match == INPLOOKUP_MATCH_NONE)
1076 			continue;
1077 
1078 		injail = prison_flag(inp->inp_cred, PR_IP6) != 0;
1079 		if (injail) {
1080 			if (prison_check_ip6_locked(
1081 			    inp->inp_cred->cr_prison, laddr) != 0)
1082 				continue;
1083 		} else {
1084 			if (local_exact != NULL)
1085 				continue;
1086 		}
1087 
1088 		if (match == INPLOOKUP_MATCH_LADDR) {
1089 			if (injail)
1090 				return (inp);
1091 			else
1092 				local_exact = inp;
1093 		} else {
1094 			if (injail)
1095 				jail_wild = inp;
1096 			else
1097 				local_wild = inp;
1098 		}
1099 	}
1100 
1101 	if (jail_wild != NULL)
1102 		return (jail_wild);
1103 	if (local_exact != NULL)
1104 		return (local_exact);
1105 	if (local_wild != NULL)
1106 		return (local_wild);
1107 	return (NULL);
1108 }
1109 
1110 struct inpcb *
1111 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
1112     const struct in6_addr *faddr, u_int fport_arg,
1113     const struct in6_addr *laddr, u_int lport_arg,
1114     int lookupflags, uint8_t numa_domain)
1115 {
1116 	struct inpcb *inp;
1117 	u_short fport = fport_arg, lport = lport_arg;
1118 
1119 	KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0,
1120 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1121 	KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr),
1122 	    ("%s: invalid foreign address", __func__));
1123 	KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr),
1124 	    ("%s: invalid local address", __func__));
1125 	INP_HASH_LOCK_ASSERT(pcbinfo);
1126 
1127 	inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1128 	if (inp != NULL)
1129 		return (inp);
1130 
1131 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1132 		inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr,
1133 		    lport, numa_domain);
1134 		if (inp == NULL) {
1135 			inp = in6_pcblookup_hash_wild_locked(pcbinfo, faddr,
1136 			    fport, laddr, lport);
1137 		}
1138 	}
1139 	return (inp);
1140 }
1141 
1142 static struct inpcb *
1143 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1144     u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1145     uint8_t numa_domain)
1146 {
1147 	struct inpcb *inp;
1148 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1149 
1150 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1151 	    ("%s: LOCKPCB not set", __func__));
1152 
1153 	INP_HASH_WLOCK(pcbinfo);
1154 	inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1155 	    lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain);
1156 	if (inp != NULL && !inp_trylock(inp, lockflags)) {
1157 		in_pcbref(inp);
1158 		INP_HASH_WUNLOCK(pcbinfo);
1159 		inp_lock(inp, lockflags);
1160 		if (in_pcbrele(inp, lockflags))
1161 			/* XXX-MJ or retry until we get a negative match? */
1162 			inp = NULL;
1163 	} else {
1164 		INP_HASH_WUNLOCK(pcbinfo);
1165 	}
1166 	return (inp);
1167 }
1168 
1169 static struct inpcb *
1170 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1171     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int lookupflags,
1172     uint8_t numa_domain)
1173 {
1174 	struct inpcb *inp;
1175 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1176 	const u_short fport = fport_arg, lport = lport_arg;
1177 
1178 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1179 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1180 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1181 	    ("%s: LOCKPCB not set", __func__));
1182 
1183 	smr_enter(pcbinfo->ipi_smr);
1184 	inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1185 	if (inp != NULL) {
1186 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
1187 			if (__predict_true(in6_pcblookup_exact_match(inp,
1188 			    faddr, fport, laddr, lport)))
1189 				return (inp);
1190 			inp_unlock(inp, lockflags);
1191 		}
1192 		/*
1193 		 * We failed to lock the inpcb, or its connection state changed
1194 		 * out from under us.  Fall back to a precise search.
1195 		 */
1196 		return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1197 		    lookupflags, numa_domain));
1198 	}
1199 
1200 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1201 		inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport,
1202 		    laddr, lport, numa_domain);
1203 		if (inp != NULL) {
1204 			if (__predict_true(inp_smr_lock(inp, lockflags))) {
1205 				if (__predict_true(in6_pcblookup_wild_match(inp,
1206 				    laddr, lport) != INPLOOKUP_MATCH_NONE))
1207 					return (inp);
1208 				inp_unlock(inp, lockflags);
1209 			}
1210 			inp = INP_LOOKUP_AGAIN;
1211 		} else {
1212 			inp = in6_pcblookup_hash_wild_smr(pcbinfo, faddr, fport,
1213 			    laddr, lport, lockflags);
1214 		}
1215 		if (inp == INP_LOOKUP_AGAIN) {
1216 			return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr,
1217 			    lport, lookupflags, numa_domain));
1218 		}
1219 	}
1220 
1221 	if (inp == NULL)
1222 		smr_exit(pcbinfo->ipi_smr);
1223 
1224 	return (inp);
1225 }
1226 
1227 /*
1228  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1229  * from which a pre-calculated hash value may be extracted.
1230  */
1231 struct inpcb *
1232 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1233     struct in6_addr *laddr, u_int lport, int lookupflags,
1234     struct ifnet *ifp __unused)
1235 {
1236 	return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1237 	    lookupflags, M_NODOM));
1238 }
1239 
1240 struct inpcb *
1241 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1242     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1243     struct ifnet *ifp __unused, struct mbuf *m)
1244 {
1245 	return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1246 	    lookupflags, m->m_pkthdr.numa_domain));
1247 }
1248 
1249 void
1250 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1251 {
1252 	struct ip6_hdr *ip;
1253 
1254 	ip = mtod(m, struct ip6_hdr *);
1255 	bzero(sin6, sizeof(*sin6));
1256 	sin6->sin6_len = sizeof(*sin6);
1257 	sin6->sin6_family = AF_INET6;
1258 	sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1259 
1260 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1261 
1262 	return;
1263 }
1264