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