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