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