xref: /freebsd/sys/netinet6/in6_pcb.c (revision 243e928310d073338c5ec089f0dce238a80b9866)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * Copyright (c) 2010-2011 Juniper Networks, Inc.
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Robert N. M. Watson under
7  * contract to Juniper Networks, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
34  */
35 
36 /*-
37  * Copyright (c) 1982, 1986, 1991, 1993
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
65  */
66 
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69 
70 #include "opt_inet.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_pcbgroup.h"
74 #include "opt_rss.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/domain.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/sockio.h>
85 #include <sys/errno.h>
86 #include <sys/time.h>
87 #include <sys/priv.h>
88 #include <sys/proc.h>
89 #include <sys/jail.h>
90 
91 #include <vm/uma.h>
92 
93 #include <net/if.h>
94 #include <net/if_var.h>
95 #include <net/if_types.h>
96 #include <net/route.h>
97 
98 #include <netinet/in.h>
99 #include <netinet/in_var.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/tcp_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet/ip_var.h>
104 
105 #include <netinet6/ip6_var.h>
106 #include <netinet6/nd6.h>
107 #include <netinet/in_pcb.h>
108 #include <netinet6/in6_pcb.h>
109 #include <netinet6/scope6_var.h>
110 
111 static struct inpcb *in6_pcblookup_hash_locked(struct inpcbinfo *,
112     struct in6_addr *, u_int, struct in6_addr *, u_int, int, struct ifnet *);
113 
114 int
115 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
116     struct ucred *cred)
117 {
118 	struct socket *so = inp->inp_socket;
119 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
120 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
121 	u_short	lport = 0;
122 	int error, lookupflags = 0;
123 	int reuseport = (so->so_options & SO_REUSEPORT);
124 
125 	INP_WLOCK_ASSERT(inp);
126 	INP_HASH_WLOCK_ASSERT(pcbinfo);
127 
128 	if (TAILQ_EMPTY(&V_in6_ifaddrhead))	/* XXX broken! */
129 		return (EADDRNOTAVAIL);
130 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
131 		return (EINVAL);
132 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
133 		lookupflags = INPLOOKUP_WILDCARD;
134 	if (nam == NULL) {
135 		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
136 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
137 			return (error);
138 	} else {
139 		sin6 = (struct sockaddr_in6 *)nam;
140 		if (nam->sa_len != sizeof(*sin6))
141 			return (EINVAL);
142 		/*
143 		 * family check.
144 		 */
145 		if (nam->sa_family != AF_INET6)
146 			return (EAFNOSUPPORT);
147 
148 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
149 			return(error);
150 
151 		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
152 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
153 			return (error);
154 
155 		lport = sin6->sin6_port;
156 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
157 			/*
158 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
159 			 * allow compepte duplication of binding if
160 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
161 			 * and a multicast address is bound on both
162 			 * new and duplicated sockets.
163 			 */
164 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
165 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
166 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
167 			struct ifaddr *ifa;
168 
169 			sin6->sin6_port = 0;		/* yech... */
170 			if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
171 			    NULL &&
172 			    (inp->inp_flags & INP_BINDANY) == 0) {
173 				return (EADDRNOTAVAIL);
174 			}
175 
176 			/*
177 			 * XXX: bind to an anycast address might accidentally
178 			 * cause sending a packet with anycast source address.
179 			 * We should allow to bind to a deprecated address, since
180 			 * the application dares to use it.
181 			 */
182 			if (ifa != NULL &&
183 			    ((struct in6_ifaddr *)ifa)->ia6_flags &
184 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
185 				ifa_free(ifa);
186 				return (EADDRNOTAVAIL);
187 			}
188 			if (ifa != NULL)
189 				ifa_free(ifa);
190 		}
191 		if (lport) {
192 			struct inpcb *t;
193 			struct tcptw *tw;
194 
195 			/* GROSS */
196 			if (ntohs(lport) <= V_ipport_reservedhigh &&
197 			    ntohs(lport) >= V_ipport_reservedlow &&
198 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
199 			    0))
200 				return (EACCES);
201 			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
202 			    priv_check_cred(inp->inp_cred,
203 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
204 				t = in6_pcblookup_local(pcbinfo,
205 				    &sin6->sin6_addr, lport,
206 				    INPLOOKUP_WILDCARD, cred);
207 				if (t &&
208 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
209 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
210 				    (so->so_type != SOCK_STREAM ||
211 				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
212 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
213 				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
214 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
215 				    (inp->inp_cred->cr_uid !=
216 				     t->inp_cred->cr_uid))
217 					return (EADDRINUSE);
218 
219 				/*
220 				 * If the socket is a BINDMULTI socket, then
221 				 * the credentials need to match and the
222 				 * original socket also has to have been bound
223 				 * with BINDMULTI.
224 				 */
225 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
226 					return (EADDRINUSE);
227 
228 #ifdef INET
229 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
230 				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
231 					struct sockaddr_in sin;
232 
233 					in6_sin6_2_sin(&sin, sin6);
234 					t = in_pcblookup_local(pcbinfo,
235 					    sin.sin_addr, lport,
236 					    INPLOOKUP_WILDCARD, cred);
237 					if (t &&
238 					    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
239 					    ((t->inp_flags &
240 					      INP_TIMEWAIT) == 0) &&
241 					    (so->so_type != SOCK_STREAM ||
242 					     ntohl(t->inp_faddr.s_addr) ==
243 					      INADDR_ANY) &&
244 					    (inp->inp_cred->cr_uid !=
245 					     t->inp_cred->cr_uid))
246 						return (EADDRINUSE);
247 
248 					if (t && (! in_pcbbind_check_bindmulti(inp, t)))
249 						return (EADDRINUSE);
250 				}
251 #endif
252 			}
253 			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
254 			    lport, lookupflags, cred);
255 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
256 				/*
257 				 * XXXRW: If an incpb has had its timewait
258 				 * state recycled, we treat the address as
259 				 * being in use (for now).  This is better
260 				 * than a panic, but not desirable.
261 				 */
262 				tw = intotw(t);
263 				if (tw == NULL ||
264 				    (reuseport & tw->tw_so_options) == 0)
265 					return (EADDRINUSE);
266 			} else if (t && (reuseport & inp_so_options(t)) == 0) {
267 				return (EADDRINUSE);
268 			}
269 #ifdef INET
270 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
271 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
272 				struct sockaddr_in sin;
273 
274 				in6_sin6_2_sin(&sin, sin6);
275 				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
276 				    lport, lookupflags, cred);
277 				if (t && t->inp_flags & INP_TIMEWAIT) {
278 					tw = intotw(t);
279 					if (tw == NULL)
280 						return (EADDRINUSE);
281 					if ((reuseport & tw->tw_so_options) == 0
282 					    && (ntohl(t->inp_laddr.s_addr) !=
283 					     INADDR_ANY || ((inp->inp_vflag &
284 					     INP_IPV6PROTO) ==
285 					     (t->inp_vflag & INP_IPV6PROTO))))
286 						return (EADDRINUSE);
287 				} else if (t &&
288 				    (reuseport & inp_so_options(t)) == 0 &&
289 				    (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
290 				    (t->inp_vflag & INP_IPV6PROTO) != 0))
291 					return (EADDRINUSE);
292 			}
293 #endif
294 		}
295 		inp->in6p_laddr = sin6->sin6_addr;
296 	}
297 	if (lport == 0) {
298 		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
299 			/* Undo an address bind that may have occurred. */
300 			inp->in6p_laddr = in6addr_any;
301 			return (error);
302 		}
303 	} else {
304 		inp->inp_lport = lport;
305 		if (in_pcbinshash(inp) != 0) {
306 			inp->in6p_laddr = in6addr_any;
307 			inp->inp_lport = 0;
308 			return (EAGAIN);
309 		}
310 	}
311 	return (0);
312 }
313 
314 /*
315  *   Transform old in6_pcbconnect() into an inner subroutine for new
316  *   in6_pcbconnect(): Do some validity-checking on the remote
317  *   address (in mbuf 'nam') and then determine local host address
318  *   (i.e., which interface) to use to access that remote host.
319  *
320  *   This preserves definition of in6_pcbconnect(), while supporting a
321  *   slightly different version for T/TCP.  (This is more than
322  *   a bit of a kludge, but cleaning up the internal interfaces would
323  *   have forced minor changes in every protocol).
324  */
325 static int
326 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
327     struct in6_addr *plocal_addr6)
328 {
329 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
330 	int error = 0;
331 	int scope_ambiguous = 0;
332 	struct in6_addr in6a;
333 
334 	INP_WLOCK_ASSERT(inp);
335 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);	/* XXXRW: why? */
336 
337 	if (nam->sa_len != sizeof (*sin6))
338 		return (EINVAL);
339 	if (sin6->sin6_family != AF_INET6)
340 		return (EAFNOSUPPORT);
341 	if (sin6->sin6_port == 0)
342 		return (EADDRNOTAVAIL);
343 
344 	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
345 		scope_ambiguous = 1;
346 	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
347 		return(error);
348 
349 	if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
350 		/*
351 		 * If the destination address is UNSPECIFIED addr,
352 		 * use the loopback addr, e.g ::1.
353 		 */
354 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
355 			sin6->sin6_addr = in6addr_loopback;
356 	}
357 	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
358 		return (error);
359 
360 	error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
361 	    inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
362 	if (error)
363 		return (error);
364 
365 	/*
366 	 * Do not update this earlier, in case we return with an error.
367 	 *
368 	 * XXX: this in6_selectsrc_socket result might replace the bound local
369 	 * address with the address specified by setsockopt(IPV6_PKTINFO).
370 	 * Is it the intended behavior?
371 	 */
372 	*plocal_addr6 = in6a;
373 
374 	/*
375 	 * Don't do pcblookup call here; return interface in
376 	 * plocal_addr6
377 	 * and exit to caller, that will do the lookup.
378 	 */
379 
380 	return (0);
381 }
382 
383 /*
384  * Outer subroutine:
385  * Connect from a socket to a specified address.
386  * Both address and port must be specified in argument sin.
387  * If don't have a local address for this socket yet,
388  * then pick one.
389  */
390 int
391 in6_pcbconnect_mbuf(register struct inpcb *inp, struct sockaddr *nam,
392     struct ucred *cred, struct mbuf *m)
393 {
394 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
395 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
396 	struct in6_addr addr6;
397 	int error;
398 
399 	INP_WLOCK_ASSERT(inp);
400 	INP_HASH_WLOCK_ASSERT(pcbinfo);
401 
402 	/*
403 	 * Call inner routine, to assign local interface address.
404 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
405 	 */
406 	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
407 		return (error);
408 
409 	if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
410 			       sin6->sin6_port,
411 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
412 			      ? &addr6 : &inp->in6p_laddr,
413 			      inp->inp_lport, 0, NULL) != NULL) {
414 		return (EADDRINUSE);
415 	}
416 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
417 		if (inp->inp_lport == 0) {
418 			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
419 			if (error)
420 				return (error);
421 		}
422 		inp->in6p_laddr = addr6;
423 	}
424 	inp->in6p_faddr = sin6->sin6_addr;
425 	inp->inp_fport = sin6->sin6_port;
426 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
427 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
428 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
429 		inp->inp_flow |=
430 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
431 
432 	in_pcbrehash_mbuf(inp, m);
433 
434 	return (0);
435 }
436 
437 int
438 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
439 {
440 
441 	return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
442 }
443 
444 void
445 in6_pcbdisconnect(struct inpcb *inp)
446 {
447 
448 	INP_WLOCK_ASSERT(inp);
449 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
450 
451 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
452 	inp->inp_fport = 0;
453 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
454 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
455 	in_pcbrehash(inp);
456 }
457 
458 struct sockaddr *
459 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
460 {
461 	struct sockaddr_in6 *sin6;
462 
463 	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
464 	bzero(sin6, sizeof *sin6);
465 	sin6->sin6_family = AF_INET6;
466 	sin6->sin6_len = sizeof(*sin6);
467 	sin6->sin6_port = port;
468 	sin6->sin6_addr = *addr_p;
469 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
470 
471 	return (struct sockaddr *)sin6;
472 }
473 
474 struct sockaddr *
475 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
476 {
477 	struct sockaddr_in sin;
478 	struct sockaddr_in6 *sin6_p;
479 
480 	bzero(&sin, sizeof sin);
481 	sin.sin_family = AF_INET;
482 	sin.sin_len = sizeof(sin);
483 	sin.sin_port = port;
484 	sin.sin_addr = *addr_p;
485 
486 	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
487 		M_WAITOK);
488 	in6_sin_2_v4mapsin6(&sin, sin6_p);
489 
490 	return (struct sockaddr *)sin6_p;
491 }
492 
493 int
494 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
495 {
496 	register struct inpcb *inp;
497 	struct in6_addr addr;
498 	in_port_t port;
499 
500 	inp = sotoinpcb(so);
501 	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
502 
503 	INP_RLOCK(inp);
504 	port = inp->inp_lport;
505 	addr = inp->in6p_laddr;
506 	INP_RUNLOCK(inp);
507 
508 	*nam = in6_sockaddr(port, &addr);
509 	return 0;
510 }
511 
512 int
513 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
514 {
515 	struct inpcb *inp;
516 	struct in6_addr addr;
517 	in_port_t port;
518 
519 	inp = sotoinpcb(so);
520 	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
521 
522 	INP_RLOCK(inp);
523 	port = inp->inp_fport;
524 	addr = inp->in6p_faddr;
525 	INP_RUNLOCK(inp);
526 
527 	*nam = in6_sockaddr(port, &addr);
528 	return 0;
529 }
530 
531 int
532 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
533 {
534 	struct	inpcb *inp;
535 	int	error;
536 
537 	inp = sotoinpcb(so);
538 	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
539 
540 #ifdef INET
541 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
542 		error = in_getsockaddr(so, nam);
543 		if (error == 0)
544 			in6_sin_2_v4mapsin6_in_sock(nam);
545 	} else
546 #endif
547 	{
548 		/* scope issues will be handled in in6_getsockaddr(). */
549 		error = in6_getsockaddr(so, nam);
550 	}
551 
552 	return error;
553 }
554 
555 int
556 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
557 {
558 	struct	inpcb *inp;
559 	int	error;
560 
561 	inp = sotoinpcb(so);
562 	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
563 
564 #ifdef INET
565 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
566 		error = in_getpeeraddr(so, nam);
567 		if (error == 0)
568 			in6_sin_2_v4mapsin6_in_sock(nam);
569 	} else
570 #endif
571 	/* scope issues will be handled in in6_getpeeraddr(). */
572 	error = in6_getpeeraddr(so, nam);
573 
574 	return error;
575 }
576 
577 /*
578  * Pass some notification to all connections of a protocol
579  * associated with address dst.  The local address and/or port numbers
580  * may be specified to limit the search.  The "usual action" will be
581  * taken, depending on the ctlinput cmd.  The caller must filter any
582  * cmds that are uninteresting (e.g., no error in the map).
583  * Call the protocol specific routine (if any) to report
584  * any errors for each matching socket.
585  */
586 void
587 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
588     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
589     int cmd, void *cmdarg,
590     struct inpcb *(*notify)(struct inpcb *, int))
591 {
592 	struct inpcb *inp, *inp_temp;
593 	struct sockaddr_in6 sa6_src, *sa6_dst;
594 	u_short	fport = fport_arg, lport = lport_arg;
595 	u_int32_t flowinfo;
596 	int errno;
597 
598 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
599 		return;
600 
601 	sa6_dst = (struct sockaddr_in6 *)dst;
602 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
603 		return;
604 
605 	/*
606 	 * note that src can be NULL when we get notify by local fragmentation.
607 	 */
608 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
609 	flowinfo = sa6_src.sin6_flowinfo;
610 
611 	/*
612 	 * Redirects go to all references to the destination,
613 	 * and use in6_rtchange to invalidate the route cache.
614 	 * Dead host indications: also use in6_rtchange to invalidate
615 	 * the cache, and deliver the error to all the sockets.
616 	 * Otherwise, if we have knowledge of the local port and address,
617 	 * deliver only to that socket.
618 	 */
619 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
620 		fport = 0;
621 		lport = 0;
622 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
623 
624 		if (cmd != PRC_HOSTDEAD)
625 			notify = in6_rtchange;
626 	}
627 	errno = inet6ctlerrmap[cmd];
628 	INP_INFO_WLOCK(pcbinfo);
629 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
630 		INP_WLOCK(inp);
631 		if ((inp->inp_vflag & INP_IPV6) == 0) {
632 			INP_WUNLOCK(inp);
633 			continue;
634 		}
635 
636 		/*
637 		 * If the error designates a new path MTU for a destination
638 		 * and the application (associated with this socket) wanted to
639 		 * know the value, notify.
640 		 * XXX: should we avoid to notify the value to TCP sockets?
641 		 */
642 		if (cmd == PRC_MSGSIZE && cmdarg != NULL)
643 			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
644 					*(u_int32_t *)cmdarg);
645 
646 		/*
647 		 * Detect if we should notify the error. If no source and
648 		 * destination ports are specifed, but non-zero flowinfo and
649 		 * local address match, notify the error. This is the case
650 		 * when the error is delivered with an encrypted buffer
651 		 * by ESP. Otherwise, just compare addresses and ports
652 		 * as usual.
653 		 */
654 		if (lport == 0 && fport == 0 && flowinfo &&
655 		    inp->inp_socket != NULL &&
656 		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
657 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
658 			goto do_notify;
659 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
660 					     &sa6_dst->sin6_addr) ||
661 			 inp->inp_socket == 0 ||
662 			 (lport && inp->inp_lport != lport) ||
663 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
664 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
665 					      &sa6_src.sin6_addr)) ||
666 			 (fport && inp->inp_fport != fport)) {
667 			INP_WUNLOCK(inp);
668 			continue;
669 		}
670 
671 	  do_notify:
672 		if (notify) {
673 			if ((*notify)(inp, errno))
674 				INP_WUNLOCK(inp);
675 		} else
676 			INP_WUNLOCK(inp);
677 	}
678 	INP_INFO_WUNLOCK(pcbinfo);
679 }
680 
681 /*
682  * Lookup a PCB based on the local address and port.  Caller must hold the
683  * hash lock.  No inpcb locks or references are acquired.
684  */
685 struct inpcb *
686 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
687     u_short lport, int lookupflags, struct ucred *cred)
688 {
689 	register struct inpcb *inp;
690 	int matchwild = 3, wildcard;
691 
692 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
693 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
694 
695 	INP_HASH_WLOCK_ASSERT(pcbinfo);
696 
697 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
698 		struct inpcbhead *head;
699 		/*
700 		 * Look for an unconnected (wildcard foreign addr) PCB that
701 		 * matches the local address and port we're looking for.
702 		 */
703 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
704 		    INP6_PCBHASHKEY(&in6addr_any), lport, 0,
705 		    pcbinfo->ipi_hashmask)];
706 		LIST_FOREACH(inp, head, inp_hash) {
707 			/* XXX inp locking */
708 			if ((inp->inp_vflag & INP_IPV6) == 0)
709 				continue;
710 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
711 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
712 			    inp->inp_lport == lport) {
713 				/* Found. */
714 				if (cred == NULL ||
715 				    prison_equal_ip6(cred->cr_prison,
716 					inp->inp_cred->cr_prison))
717 					return (inp);
718 			}
719 		}
720 		/*
721 		 * Not found.
722 		 */
723 		return (NULL);
724 	} else {
725 		struct inpcbporthead *porthash;
726 		struct inpcbport *phd;
727 		struct inpcb *match = NULL;
728 		/*
729 		 * Best fit PCB lookup.
730 		 *
731 		 * First see if this local port is in use by looking on the
732 		 * port hash list.
733 		 */
734 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
735 		    pcbinfo->ipi_porthashmask)];
736 		LIST_FOREACH(phd, porthash, phd_hash) {
737 			if (phd->phd_port == lport)
738 				break;
739 		}
740 		if (phd != NULL) {
741 			/*
742 			 * Port is in use by one or more PCBs. Look for best
743 			 * fit.
744 			 */
745 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
746 				wildcard = 0;
747 				if (cred != NULL &&
748 				    !prison_equal_ip6(cred->cr_prison,
749 					inp->inp_cred->cr_prison))
750 					continue;
751 				/* XXX inp locking */
752 				if ((inp->inp_vflag & INP_IPV6) == 0)
753 					continue;
754 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
755 					wildcard++;
756 				if (!IN6_IS_ADDR_UNSPECIFIED(
757 					&inp->in6p_laddr)) {
758 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
759 						wildcard++;
760 					else if (!IN6_ARE_ADDR_EQUAL(
761 					    &inp->in6p_laddr, laddr))
762 						continue;
763 				} else {
764 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
765 						wildcard++;
766 				}
767 				if (wildcard < matchwild) {
768 					match = inp;
769 					matchwild = wildcard;
770 					if (matchwild == 0)
771 						break;
772 				}
773 			}
774 		}
775 		return (match);
776 	}
777 }
778 
779 void
780 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
781 {
782 	struct inpcb *in6p;
783 	struct ip6_moptions *im6o;
784 	int i, gap;
785 
786 	INP_INFO_WLOCK(pcbinfo);
787 	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
788 		INP_WLOCK(in6p);
789 		im6o = in6p->in6p_moptions;
790 		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
791 			/*
792 			 * Unselect the outgoing ifp for multicast if it
793 			 * is being detached.
794 			 */
795 			if (im6o->im6o_multicast_ifp == ifp)
796 				im6o->im6o_multicast_ifp = NULL;
797 			/*
798 			 * Drop multicast group membership if we joined
799 			 * through the interface being detached.
800 			 */
801 			gap = 0;
802 			for (i = 0; i < im6o->im6o_num_memberships; i++) {
803 				if (im6o->im6o_membership[i]->in6m_ifp ==
804 				    ifp) {
805 					in6_mc_leave(im6o->im6o_membership[i],
806 					    NULL);
807 					gap++;
808 				} else if (gap != 0) {
809 					im6o->im6o_membership[i - gap] =
810 					    im6o->im6o_membership[i];
811 				}
812 			}
813 			im6o->im6o_num_memberships -= gap;
814 		}
815 		INP_WUNLOCK(in6p);
816 	}
817 	INP_INFO_WUNLOCK(pcbinfo);
818 }
819 
820 /*
821  * Check for alternatives when higher level complains
822  * about service problems.  For now, invalidate cached
823  * routing information.  If the route was created dynamically
824  * (by a redirect), time to try a default gateway again.
825  */
826 void
827 in6_losing(struct inpcb *in6p)
828 {
829 
830 	if (in6p->inp_route6.ro_rt) {
831 		RTFREE(in6p->inp_route6.ro_rt);
832 		in6p->inp_route6.ro_rt = (struct rtentry *)NULL;
833 	}
834 	return;
835 }
836 
837 /*
838  * After a routing change, flush old routing
839  * and allocate a (hopefully) better one.
840  */
841 struct inpcb *
842 in6_rtchange(struct inpcb *inp, int errno)
843 {
844 
845 	if (inp->inp_route6.ro_rt) {
846 		RTFREE(inp->inp_route6.ro_rt);
847 		inp->inp_route6.ro_rt = (struct rtentry *)NULL;
848 	}
849 	return inp;
850 }
851 
852 #ifdef PCBGROUP
853 /*
854  * Lookup PCB in hash list, using pcbgroup tables.
855  */
856 static struct inpcb *
857 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
858     struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
859     u_int lport_arg, int lookupflags, struct ifnet *ifp)
860 {
861 	struct inpcbhead *head;
862 	struct inpcb *inp, *tmpinp;
863 	u_short fport = fport_arg, lport = lport_arg;
864 
865 	/*
866 	 * First look for an exact match.
867 	 */
868 	tmpinp = NULL;
869 	INP_GROUP_LOCK(pcbgroup);
870 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
871 	    INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
872 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
873 		/* XXX inp locking */
874 		if ((inp->inp_vflag & INP_IPV6) == 0)
875 			continue;
876 		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
877 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
878 		    inp->inp_fport == fport &&
879 		    inp->inp_lport == lport) {
880 			/*
881 			 * XXX We should be able to directly return
882 			 * the inp here, without any checks.
883 			 * Well unless both bound with SO_REUSEPORT?
884 			 */
885 			if (prison_flag(inp->inp_cred, PR_IP6))
886 				goto found;
887 			if (tmpinp == NULL)
888 				tmpinp = inp;
889 		}
890 	}
891 	if (tmpinp != NULL) {
892 		inp = tmpinp;
893 		goto found;
894 	}
895 
896 	/*
897 	 * Then look for a wildcard match in the pcbgroup.
898 	 */
899 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
900 		struct inpcb *local_wild = NULL, *local_exact = NULL;
901 		struct inpcb *jail_wild = NULL;
902 		int injail;
903 
904 		/*
905 		 * Order of socket selection - we always prefer jails.
906 		 *      1. jailed, non-wild.
907 		 *      2. jailed, wild.
908 		 *      3. non-jailed, non-wild.
909 		 *      4. non-jailed, wild.
910 		 */
911 		head = &pcbgroup->ipg_hashbase[
912 		    INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
913 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
914 			/* XXX inp locking */
915 			if ((inp->inp_vflag & INP_IPV6) == 0)
916 				continue;
917 
918 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
919 			    inp->inp_lport != lport) {
920 				continue;
921 			}
922 
923 			injail = prison_flag(inp->inp_cred, PR_IP6);
924 			if (injail) {
925 				if (prison_check_ip6(inp->inp_cred,
926 				    laddr) != 0)
927 					continue;
928 			} else {
929 				if (local_exact != NULL)
930 					continue;
931 			}
932 
933 			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
934 				if (injail)
935 					goto found;
936 				else
937 					local_exact = inp;
938 			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
939 				if (injail)
940 					jail_wild = inp;
941 				else
942 					local_wild = inp;
943 			}
944 		} /* LIST_FOREACH */
945 
946 		inp = jail_wild;
947 		if (inp == NULL)
948 			inp = jail_wild;
949 		if (inp == NULL)
950 			inp = local_exact;
951 		if (inp == NULL)
952 			inp = local_wild;
953 		if (inp != NULL)
954 			goto found;
955 	}
956 
957 	/*
958 	 * Then look for a wildcard match, if requested.
959 	 */
960 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
961 		struct inpcb *local_wild = NULL, *local_exact = NULL;
962 		struct inpcb *jail_wild = NULL;
963 		int injail;
964 
965 		/*
966 		 * Order of socket selection - we always prefer jails.
967 		 *      1. jailed, non-wild.
968 		 *      2. jailed, wild.
969 		 *      3. non-jailed, non-wild.
970 		 *      4. non-jailed, wild.
971 		 */
972 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
973 		    INP6_PCBHASHKEY(&in6addr_any), lport, 0,
974 		    pcbinfo->ipi_wildmask)];
975 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
976 			/* XXX inp locking */
977 			if ((inp->inp_vflag & INP_IPV6) == 0)
978 				continue;
979 
980 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
981 			    inp->inp_lport != lport) {
982 				continue;
983 			}
984 
985 			injail = prison_flag(inp->inp_cred, PR_IP6);
986 			if (injail) {
987 				if (prison_check_ip6(inp->inp_cred,
988 				    laddr) != 0)
989 					continue;
990 			} else {
991 				if (local_exact != NULL)
992 					continue;
993 			}
994 
995 			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
996 				if (injail)
997 					goto found;
998 				else
999 					local_exact = inp;
1000 			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1001 				if (injail)
1002 					jail_wild = inp;
1003 				else
1004 					local_wild = inp;
1005 			}
1006 		} /* LIST_FOREACH */
1007 
1008 		inp = jail_wild;
1009 		if (inp == NULL)
1010 			inp = jail_wild;
1011 		if (inp == NULL)
1012 			inp = local_exact;
1013 		if (inp == NULL)
1014 			inp = local_wild;
1015 		if (inp != NULL)
1016 			goto found;
1017 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1018 	INP_GROUP_UNLOCK(pcbgroup);
1019 	return (NULL);
1020 
1021 found:
1022 	in_pcbref(inp);
1023 	INP_GROUP_UNLOCK(pcbgroup);
1024 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
1025 		INP_WLOCK(inp);
1026 		if (in_pcbrele_wlocked(inp))
1027 			return (NULL);
1028 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1029 		INP_RLOCK(inp);
1030 		if (in_pcbrele_rlocked(inp))
1031 			return (NULL);
1032 	} else
1033 		panic("%s: locking buf", __func__);
1034 	return (inp);
1035 }
1036 #endif /* PCBGROUP */
1037 
1038 /*
1039  * Lookup PCB in hash list.
1040  */
1041 static struct inpcb *
1042 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1043     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1044     int lookupflags, struct ifnet *ifp)
1045 {
1046 	struct inpcbhead *head;
1047 	struct inpcb *inp, *tmpinp;
1048 	u_short fport = fport_arg, lport = lport_arg;
1049 
1050 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1051 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1052 
1053 	INP_HASH_LOCK_ASSERT(pcbinfo);
1054 
1055 	/*
1056 	 * First look for an exact match.
1057 	 */
1058 	tmpinp = NULL;
1059 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1060 	    INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1061 	LIST_FOREACH(inp, head, inp_hash) {
1062 		/* XXX inp locking */
1063 		if ((inp->inp_vflag & INP_IPV6) == 0)
1064 			continue;
1065 		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1066 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1067 		    inp->inp_fport == fport &&
1068 		    inp->inp_lport == lport) {
1069 			/*
1070 			 * XXX We should be able to directly return
1071 			 * the inp here, without any checks.
1072 			 * Well unless both bound with SO_REUSEPORT?
1073 			 */
1074 			if (prison_flag(inp->inp_cred, PR_IP6))
1075 				return (inp);
1076 			if (tmpinp == NULL)
1077 				tmpinp = inp;
1078 		}
1079 	}
1080 	if (tmpinp != NULL)
1081 		return (tmpinp);
1082 
1083 	/*
1084 	 * Then look for a wildcard match, if requested.
1085 	 */
1086 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1087 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1088 		struct inpcb *jail_wild = NULL;
1089 		int injail;
1090 
1091 		/*
1092 		 * Order of socket selection - we always prefer jails.
1093 		 *      1. jailed, non-wild.
1094 		 *      2. jailed, wild.
1095 		 *      3. non-jailed, non-wild.
1096 		 *      4. non-jailed, wild.
1097 		 */
1098 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1099 		    INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1100 		    pcbinfo->ipi_hashmask)];
1101 		LIST_FOREACH(inp, head, inp_hash) {
1102 			/* XXX inp locking */
1103 			if ((inp->inp_vflag & INP_IPV6) == 0)
1104 				continue;
1105 
1106 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1107 			    inp->inp_lport != lport) {
1108 				continue;
1109 			}
1110 
1111 			injail = prison_flag(inp->inp_cred, PR_IP6);
1112 			if (injail) {
1113 				if (prison_check_ip6(inp->inp_cred,
1114 				    laddr) != 0)
1115 					continue;
1116 			} else {
1117 				if (local_exact != NULL)
1118 					continue;
1119 			}
1120 
1121 			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1122 				if (injail)
1123 					return (inp);
1124 				else
1125 					local_exact = inp;
1126 			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1127 				if (injail)
1128 					jail_wild = inp;
1129 				else
1130 					local_wild = inp;
1131 			}
1132 		} /* LIST_FOREACH */
1133 
1134 		if (jail_wild != NULL)
1135 			return (jail_wild);
1136 		if (local_exact != NULL)
1137 			return (local_exact);
1138 		if (local_wild != NULL)
1139 			return (local_wild);
1140 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1141 
1142 	/*
1143 	 * Not found.
1144 	 */
1145 	return (NULL);
1146 }
1147 
1148 /*
1149  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1150  * hash list lock, and will return the inpcb locked (i.e., requires
1151  * INPLOOKUP_LOCKPCB).
1152  */
1153 static struct inpcb *
1154 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1155     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1156     struct ifnet *ifp)
1157 {
1158 	struct inpcb *inp;
1159 
1160 	INP_HASH_RLOCK(pcbinfo);
1161 	inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1162 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1163 	if (inp != NULL) {
1164 		in_pcbref(inp);
1165 		INP_HASH_RUNLOCK(pcbinfo);
1166 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1167 			INP_WLOCK(inp);
1168 			if (in_pcbrele_wlocked(inp))
1169 				return (NULL);
1170 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1171 			INP_RLOCK(inp);
1172 			if (in_pcbrele_rlocked(inp))
1173 				return (NULL);
1174 		} else
1175 			panic("%s: locking bug", __func__);
1176 	} else
1177 		INP_HASH_RUNLOCK(pcbinfo);
1178 	return (inp);
1179 }
1180 
1181 /*
1182  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1183  * from which a pre-calculated hash value may be extracted.
1184  *
1185  * Possibly more of this logic should be in in6_pcbgroup.c.
1186  */
1187 struct inpcb *
1188 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1189     struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1190 {
1191 #if defined(PCBGROUP) && !defined(RSS)
1192 	struct inpcbgroup *pcbgroup;
1193 #endif
1194 
1195 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1196 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1197 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1198 	    ("%s: LOCKPCB not set", __func__));
1199 
1200 	/*
1201 	 * When not using RSS, use connection groups in preference to the
1202 	 * reservation table when looking up 4-tuples.  When using RSS, just
1203 	 * use the reservation table, due to the cost of the Toeplitz hash
1204 	 * in software.
1205 	 *
1206 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1207 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
1208 	 * in software.
1209 	 */
1210 #if defined(PCBGROUP) && !defined(RSS)
1211 	if (in_pcbgroup_enabled(pcbinfo)) {
1212 		pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1213 		    fport);
1214 		return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1215 		    laddr, lport, lookupflags, ifp));
1216 	}
1217 #endif
1218 	return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1219 	    lookupflags, ifp));
1220 }
1221 
1222 struct inpcb *
1223 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1224     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1225     struct ifnet *ifp, struct mbuf *m)
1226 {
1227 #ifdef PCBGROUP
1228 	struct inpcbgroup *pcbgroup;
1229 #endif
1230 
1231 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1232 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1233 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1234 	    ("%s: LOCKPCB not set", __func__));
1235 
1236 #ifdef PCBGROUP
1237 	/*
1238 	 * If we can use a hardware-generated hash to look up the connection
1239 	 * group, use that connection group to find the inpcb.  Otherwise
1240 	 * fall back on a software hash -- or the reservation table if we're
1241 	 * using RSS.
1242 	 *
1243 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
1244 	 */
1245 	if (in_pcbgroup_enabled(pcbinfo) &&
1246 	    M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1247 		pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1248 		    m->m_pkthdr.flowid);
1249 		if (pcbgroup != NULL)
1250 			return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1251 			    fport, laddr, lport, lookupflags, ifp));
1252 #ifndef RSS
1253 		pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1254 		    fport);
1255 		return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1256 		    laddr, lport, lookupflags, ifp));
1257 #endif
1258 	}
1259 #endif
1260 	return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1261 	    lookupflags, ifp));
1262 }
1263 
1264 void
1265 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1266 {
1267 	struct ip6_hdr *ip;
1268 
1269 	ip = mtod(m, struct ip6_hdr *);
1270 	bzero(sin6, sizeof(*sin6));
1271 	sin6->sin6_len = sizeof(*sin6);
1272 	sin6->sin6_family = AF_INET6;
1273 	sin6->sin6_addr = ip->ip6_src;
1274 
1275 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1276 
1277 	return;
1278 }
1279