xref: /freebsd/sys/netinet6/in6_pcb.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
30  */
31 
32 /*-
33  * Copyright (c) 1982, 1986, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
61  */
62 
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
69 #include "opt_mac.h"
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sockio.h>
80 #include <sys/errno.h>
81 #include <sys/time.h>
82 #include <sys/priv.h>
83 #include <sys/proc.h>
84 #include <sys/jail.h>
85 #include <sys/vimage.h>
86 
87 #include <vm/uma.h>
88 
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet/vinet.h>
100 
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet/in_pcb.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet6/scope6_var.h>
106 #include <netinet6/vinet6.h>
107 
108 #include <security/mac/mac_framework.h>
109 
110 struct	in6_addr zeroin6_addr;
111 
112 int
113 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
114     struct ucred *cred)
115 {
116 	INIT_VNET_INET6(inp->inp_vnet);
117 	INIT_VNET_INET(inp->inp_vnet);
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, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
123 
124 	INP_INFO_WLOCK_ASSERT(pcbinfo);
125 	INP_WLOCK_ASSERT(inp);
126 
127 	if (!V_in6_ifaddr) /* XXX broken! */
128 		return (EADDRNOTAVAIL);
129 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
130 		return (EINVAL);
131 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
132 		wild = INPLOOKUP_WILDCARD;
133 	if (nam == NULL) {
134 		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
135 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
136 			return (error);
137 	} else {
138 		sin6 = (struct sockaddr_in6 *)nam;
139 		if (nam->sa_len != sizeof(*sin6))
140 			return (EINVAL);
141 		/*
142 		 * family check.
143 		 */
144 		if (nam->sa_family != AF_INET6)
145 			return (EAFNOSUPPORT);
146 
147 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
148 			return(error);
149 
150 		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
151 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
152 			return (error);
153 
154 		lport = sin6->sin6_port;
155 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
156 			/*
157 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
158 			 * allow compepte duplication of binding if
159 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
160 			 * and a multicast address is bound on both
161 			 * new and duplicated sockets.
162 			 */
163 			if (so->so_options & SO_REUSEADDR)
164 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
165 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
166 			struct ifaddr *ia;
167 
168 			sin6->sin6_port = 0;		/* yech... */
169 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == NULL &&
170 			    (inp->inp_flags & INP_BINDANY) == 0) {
171 				return (EADDRNOTAVAIL);
172 			}
173 
174 			/*
175 			 * XXX: bind to an anycast address might accidentally
176 			 * cause sending a packet with anycast source address.
177 			 * We should allow to bind to a deprecated address, since
178 			 * the application dares to use it.
179 			 */
180 			if (ia &&
181 			    ((struct in6_ifaddr *)ia)->ia6_flags &
182 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
183 				return (EADDRNOTAVAIL);
184 			}
185 		}
186 		if (lport) {
187 			struct inpcb *t;
188 
189 			/* GROSS */
190 			if (ntohs(lport) <= V_ipport_reservedhigh &&
191 			    ntohs(lport) >= V_ipport_reservedlow &&
192 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
193 			    0))
194 				return (EACCES);
195 			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
196 			    priv_check_cred(inp->inp_cred,
197 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
198 				t = in6_pcblookup_local(pcbinfo,
199 				    &sin6->sin6_addr, lport,
200 				    INPLOOKUP_WILDCARD, cred);
201 				if (t &&
202 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
203 				    (so->so_type != SOCK_STREAM ||
204 				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
205 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
206 				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
207 				     (t->inp_socket->so_options & SO_REUSEPORT)
208 				      == 0) && (inp->inp_cred->cr_uid !=
209 				     t->inp_cred->cr_uid))
210 					return (EADDRINUSE);
211 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
212 				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
213 					struct sockaddr_in sin;
214 
215 					in6_sin6_2_sin(&sin, sin6);
216 					t = in_pcblookup_local(pcbinfo,
217 					    sin.sin_addr, lport,
218 					    INPLOOKUP_WILDCARD, cred);
219 					if (t &&
220 					    ((t->inp_flags &
221 					      INP_TIMEWAIT) == 0) &&
222 					    (so->so_type != SOCK_STREAM ||
223 					     ntohl(t->inp_faddr.s_addr) ==
224 					      INADDR_ANY) &&
225 					    (inp->inp_cred->cr_uid !=
226 					     t->inp_cred->cr_uid))
227 						return (EADDRINUSE);
228 				}
229 			}
230 			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
231 			    lport, wild, cred);
232 			if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
233 			    intotw(t)->tw_so_options :
234 			    t->inp_socket->so_options)) == 0)
235 				return (EADDRINUSE);
236 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
237 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
238 				struct sockaddr_in sin;
239 
240 				in6_sin6_2_sin(&sin, sin6);
241 				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
242 				    lport, wild, cred);
243 				if (t && t->inp_flags & INP_TIMEWAIT) {
244 					if ((reuseport &
245 					    intotw(t)->tw_so_options) == 0 &&
246 					    (ntohl(t->inp_laddr.s_addr) !=
247 					     INADDR_ANY || ((inp->inp_vflag &
248 					     INP_IPV6PROTO) ==
249 					     (t->inp_vflag & INP_IPV6PROTO))))
250 						return (EADDRINUSE);
251 				}
252 				else if (t &&
253 				    (reuseport & t->inp_socket->so_options)
254 				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
255 				    INADDR_ANY || INP_SOCKAF(so) ==
256 				     INP_SOCKAF(t->inp_socket)))
257 					return (EADDRINUSE);
258 			}
259 		}
260 		inp->in6p_laddr = sin6->sin6_addr;
261 	}
262 	if (lport == 0) {
263 		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
264 			return (error);
265 	} else {
266 		inp->inp_lport = lport;
267 		if (in_pcbinshash(inp) != 0) {
268 			inp->in6p_laddr = in6addr_any;
269 			inp->inp_lport = 0;
270 			return (EAGAIN);
271 		}
272 	}
273 	return (0);
274 }
275 
276 /*
277  *   Transform old in6_pcbconnect() into an inner subroutine for new
278  *   in6_pcbconnect(): Do some validity-checking on the remote
279  *   address (in mbuf 'nam') and then determine local host address
280  *   (i.e., which interface) to use to access that remote host.
281  *
282  *   This preserves definition of in6_pcbconnect(), while supporting a
283  *   slightly different version for T/TCP.  (This is more than
284  *   a bit of a kludge, but cleaning up the internal interfaces would
285  *   have forced minor changes in every protocol).
286  */
287 int
288 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
289     struct in6_addr **plocal_addr6)
290 {
291 	INIT_VNET_INET6(inp->inp_vnet);
292 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
293 	int error = 0;
294 	struct ifnet *ifp = NULL;
295 	int scope_ambiguous = 0;
296 
297 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
298 	INP_WLOCK_ASSERT(inp);
299 
300 	if (nam->sa_len != sizeof (*sin6))
301 		return (EINVAL);
302 	if (sin6->sin6_family != AF_INET6)
303 		return (EAFNOSUPPORT);
304 	if (sin6->sin6_port == 0)
305 		return (EADDRNOTAVAIL);
306 
307 	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
308 		scope_ambiguous = 1;
309 	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
310 		return(error);
311 
312 	if (V_in6_ifaddr) {
313 		/*
314 		 * If the destination address is UNSPECIFIED addr,
315 		 * use the loopback addr, e.g ::1.
316 		 */
317 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
318 			sin6->sin6_addr = in6addr_loopback;
319 	}
320 	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
321 		return (error);
322 
323 	/*
324 	 * XXX: in6_selectsrc might replace the bound local address
325 	 * with the address specified by setsockopt(IPV6_PKTINFO).
326 	 * Is it the intended behavior?
327 	 */
328 	*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
329 				      inp, NULL,
330 				      inp->inp_cred,
331 				      &ifp, &error);
332 	if (ifp && scope_ambiguous &&
333 	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
334 		return(error);
335 	}
336 
337 	if (*plocal_addr6 == NULL) {
338 		if (error == 0)
339 			error = EADDRNOTAVAIL;
340 		return (error);
341 	}
342 	/*
343 	 * Don't do pcblookup call here; return interface in
344 	 * plocal_addr6
345 	 * and exit to caller, that will do the lookup.
346 	 */
347 
348 	return (0);
349 }
350 
351 /*
352  * Outer subroutine:
353  * Connect from a socket to a specified address.
354  * Both address and port must be specified in argument sin.
355  * If don't have a local address for this socket yet,
356  * then pick one.
357  */
358 int
359 in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
360     struct ucred *cred)
361 {
362 	struct in6_addr *addr6;
363 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
364 	int error;
365 
366 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
367 	INP_WLOCK_ASSERT(inp);
368 
369 	/*
370 	 * Call inner routine, to assign local interface address.
371 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
372 	 */
373 	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
374 		return (error);
375 
376 	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
377 			       sin6->sin6_port,
378 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
379 			      ? addr6 : &inp->in6p_laddr,
380 			      inp->inp_lport, 0, NULL) != NULL) {
381 		return (EADDRINUSE);
382 	}
383 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
384 		if (inp->inp_lport == 0) {
385 			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
386 			if (error)
387 				return (error);
388 		}
389 		inp->in6p_laddr = *addr6;
390 	}
391 	inp->in6p_faddr = sin6->sin6_addr;
392 	inp->inp_fport = sin6->sin6_port;
393 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
394 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
395 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
396 		inp->inp_flow |=
397 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
398 
399 	in_pcbrehash(inp);
400 
401 	return (0);
402 }
403 
404 void
405 in6_pcbdisconnect(struct inpcb *inp)
406 {
407 
408 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
409 	INP_WLOCK_ASSERT(inp);
410 
411 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
412 	inp->inp_fport = 0;
413 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
414 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
415 	in_pcbrehash(inp);
416 }
417 
418 struct sockaddr *
419 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
420 {
421 	struct sockaddr_in6 *sin6;
422 
423 	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
424 	bzero(sin6, sizeof *sin6);
425 	sin6->sin6_family = AF_INET6;
426 	sin6->sin6_len = sizeof(*sin6);
427 	sin6->sin6_port = port;
428 	sin6->sin6_addr = *addr_p;
429 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
430 
431 	return (struct sockaddr *)sin6;
432 }
433 
434 struct sockaddr *
435 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
436 {
437 	struct sockaddr_in sin;
438 	struct sockaddr_in6 *sin6_p;
439 
440 	bzero(&sin, sizeof sin);
441 	sin.sin_family = AF_INET;
442 	sin.sin_len = sizeof(sin);
443 	sin.sin_port = port;
444 	sin.sin_addr = *addr_p;
445 
446 	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
447 		M_WAITOK);
448 	in6_sin_2_v4mapsin6(&sin, sin6_p);
449 
450 	return (struct sockaddr *)sin6_p;
451 }
452 
453 int
454 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
455 {
456 	register struct inpcb *inp;
457 	struct in6_addr addr;
458 	in_port_t port;
459 
460 	inp = sotoinpcb(so);
461 	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
462 
463 	INP_RLOCK(inp);
464 	port = inp->inp_lport;
465 	addr = inp->in6p_laddr;
466 	INP_RUNLOCK(inp);
467 
468 	*nam = in6_sockaddr(port, &addr);
469 	return 0;
470 }
471 
472 int
473 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
474 {
475 	struct inpcb *inp;
476 	struct in6_addr addr;
477 	in_port_t port;
478 
479 	inp = sotoinpcb(so);
480 	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
481 
482 	INP_RLOCK(inp);
483 	port = inp->inp_fport;
484 	addr = inp->in6p_faddr;
485 	INP_RUNLOCK(inp);
486 
487 	*nam = in6_sockaddr(port, &addr);
488 	return 0;
489 }
490 
491 int
492 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
493 {
494 	struct	inpcb *inp;
495 	int	error;
496 
497 	inp = sotoinpcb(so);
498 	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
499 
500 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
501 		error = in_getsockaddr(so, nam);
502 		if (error == 0)
503 			in6_sin_2_v4mapsin6_in_sock(nam);
504 	} else {
505 		/* scope issues will be handled in in6_getsockaddr(). */
506 		error = in6_getsockaddr(so, nam);
507 	}
508 
509 	return error;
510 }
511 
512 int
513 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
514 {
515 	struct	inpcb *inp;
516 	int	error;
517 
518 	inp = sotoinpcb(so);
519 	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
520 
521 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
522 		error = in_getpeeraddr(so, nam);
523 		if (error == 0)
524 			in6_sin_2_v4mapsin6_in_sock(nam);
525 	} else
526 	/* scope issues will be handled in in6_getpeeraddr(). */
527 	error = in6_getpeeraddr(so, nam);
528 
529 	return error;
530 }
531 
532 /*
533  * Pass some notification to all connections of a protocol
534  * associated with address dst.  The local address and/or port numbers
535  * may be specified to limit the search.  The "usual action" will be
536  * taken, depending on the ctlinput cmd.  The caller must filter any
537  * cmds that are uninteresting (e.g., no error in the map).
538  * Call the protocol specific routine (if any) to report
539  * any errors for each matching socket.
540  */
541 void
542 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
543     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
544     int cmd, void *cmdarg,
545     struct inpcb *(*notify)(struct inpcb *, int))
546 {
547 	struct inpcb *inp, *inp_temp;
548 	struct sockaddr_in6 sa6_src, *sa6_dst;
549 	u_short	fport = fport_arg, lport = lport_arg;
550 	u_int32_t flowinfo;
551 	int errno;
552 
553 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
554 		return;
555 
556 	sa6_dst = (struct sockaddr_in6 *)dst;
557 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
558 		return;
559 
560 	/*
561 	 * note that src can be NULL when we get notify by local fragmentation.
562 	 */
563 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
564 	flowinfo = sa6_src.sin6_flowinfo;
565 
566 	/*
567 	 * Redirects go to all references to the destination,
568 	 * and use in6_rtchange to invalidate the route cache.
569 	 * Dead host indications: also use in6_rtchange to invalidate
570 	 * the cache, and deliver the error to all the sockets.
571 	 * Otherwise, if we have knowledge of the local port and address,
572 	 * deliver only to that socket.
573 	 */
574 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
575 		fport = 0;
576 		lport = 0;
577 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
578 
579 		if (cmd != PRC_HOSTDEAD)
580 			notify = in6_rtchange;
581 	}
582 	errno = inet6ctlerrmap[cmd];
583 	INP_INFO_WLOCK(pcbinfo);
584 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
585 		INP_WLOCK(inp);
586 		if ((inp->inp_vflag & INP_IPV6) == 0) {
587 			INP_WUNLOCK(inp);
588 			continue;
589 		}
590 
591 		/*
592 		 * If the error designates a new path MTU for a destination
593 		 * and the application (associated with this socket) wanted to
594 		 * know the value, notify. Note that we notify for all
595 		 * disconnected sockets if the corresponding application
596 		 * wanted. This is because some UDP applications keep sending
597 		 * sockets disconnected.
598 		 * XXX: should we avoid to notify the value to TCP sockets?
599 		 */
600 		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
601 		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
602 		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
603 			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
604 					(u_int32_t *)cmdarg);
605 		}
606 
607 		/*
608 		 * Detect if we should notify the error. If no source and
609 		 * destination ports are specifed, but non-zero flowinfo and
610 		 * local address match, notify the error. This is the case
611 		 * when the error is delivered with an encrypted buffer
612 		 * by ESP. Otherwise, just compare addresses and ports
613 		 * as usual.
614 		 */
615 		if (lport == 0 && fport == 0 && flowinfo &&
616 		    inp->inp_socket != NULL &&
617 		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
618 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
619 			goto do_notify;
620 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
621 					     &sa6_dst->sin6_addr) ||
622 			 inp->inp_socket == 0 ||
623 			 (lport && inp->inp_lport != lport) ||
624 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
625 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
626 					      &sa6_src.sin6_addr)) ||
627 			 (fport && inp->inp_fport != fport)) {
628 			INP_WUNLOCK(inp);
629 			continue;
630 		}
631 
632 	  do_notify:
633 		if (notify) {
634 			if ((*notify)(inp, errno))
635 				INP_WUNLOCK(inp);
636 		} else
637 			INP_WUNLOCK(inp);
638 	}
639 	INP_INFO_WUNLOCK(pcbinfo);
640 }
641 
642 /*
643  * Lookup a PCB based on the local address and port.
644  */
645 struct inpcb *
646 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
647     u_short lport, int wild_okay, struct ucred *cred)
648 {
649 	register struct inpcb *inp;
650 	int matchwild = 3, wildcard;
651 
652 	INP_INFO_WLOCK_ASSERT(pcbinfo);
653 
654 	if (!wild_okay) {
655 		struct inpcbhead *head;
656 		/*
657 		 * Look for an unconnected (wildcard foreign addr) PCB that
658 		 * matches the local address and port we're looking for.
659 		 */
660 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
661 		    0, pcbinfo->ipi_hashmask)];
662 		LIST_FOREACH(inp, head, inp_hash) {
663 			/* XXX inp locking */
664 			if ((inp->inp_vflag & INP_IPV6) == 0)
665 				continue;
666 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
667 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
668 			    inp->inp_lport == lport) {
669 				/* Found. */
670 				if (cred == NULL ||
671 				    prison_equal_ip6(cred->cr_prison,
672 					inp->inp_cred->cr_prison))
673 					return (inp);
674 			}
675 		}
676 		/*
677 		 * Not found.
678 		 */
679 		return (NULL);
680 	} else {
681 		struct inpcbporthead *porthash;
682 		struct inpcbport *phd;
683 		struct inpcb *match = NULL;
684 		/*
685 		 * Best fit PCB lookup.
686 		 *
687 		 * First see if this local port is in use by looking on the
688 		 * port hash list.
689 		 */
690 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
691 		    pcbinfo->ipi_porthashmask)];
692 		LIST_FOREACH(phd, porthash, phd_hash) {
693 			if (phd->phd_port == lport)
694 				break;
695 		}
696 		if (phd != NULL) {
697 			/*
698 			 * Port is in use by one or more PCBs. Look for best
699 			 * fit.
700 			 */
701 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
702 				wildcard = 0;
703 				if (cred != NULL &&
704 				    !prison_equal_ip6(cred->cr_prison,
705 					inp->inp_cred->cr_prison))
706 					continue;
707 				/* XXX inp locking */
708 				if ((inp->inp_vflag & INP_IPV6) == 0)
709 					continue;
710 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
711 					wildcard++;
712 				if (!IN6_IS_ADDR_UNSPECIFIED(
713 					&inp->in6p_laddr)) {
714 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
715 						wildcard++;
716 					else if (!IN6_ARE_ADDR_EQUAL(
717 					    &inp->in6p_laddr, laddr))
718 						continue;
719 				} else {
720 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
721 						wildcard++;
722 				}
723 				if (wildcard < matchwild) {
724 					match = inp;
725 					matchwild = wildcard;
726 					if (matchwild == 0)
727 						break;
728 				}
729 			}
730 		}
731 		return (match);
732 	}
733 }
734 
735 void
736 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
737 {
738 	struct inpcb *in6p;
739 	struct ip6_moptions *im6o;
740 	int i, gap;
741 
742 	INP_INFO_RLOCK(pcbinfo);
743 	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
744 		INP_WLOCK(in6p);
745 		im6o = in6p->in6p_moptions;
746 		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
747 			/*
748 			 * Unselect the outgoing ifp for multicast if it
749 			 * is being detached.
750 			 */
751 			if (im6o->im6o_multicast_ifp == ifp)
752 				im6o->im6o_multicast_ifp = NULL;
753 			/*
754 			 * Drop multicast group membership if we joined
755 			 * through the interface being detached.
756 			 */
757 			gap = 0;
758 			for (i = 0; i < im6o->im6o_num_memberships; i++) {
759 				if (im6o->im6o_membership[i]->in6m_ifp ==
760 				    ifp) {
761 					in6_mc_leave(im6o->im6o_membership[i],
762 					    NULL);
763 					gap++;
764 				} else if (gap != 0) {
765 					im6o->im6o_membership[i - gap] =
766 					    im6o->im6o_membership[i];
767 				}
768 			}
769 			im6o->im6o_num_memberships -= gap;
770 		}
771 		INP_WUNLOCK(in6p);
772 	}
773 	INP_INFO_RUNLOCK(pcbinfo);
774 }
775 
776 /*
777  * Check for alternatives when higher level complains
778  * about service problems.  For now, invalidate cached
779  * routing information.  If the route was created dynamically
780  * (by a redirect), time to try a default gateway again.
781  */
782 void
783 in6_losing(struct inpcb *in6p)
784 {
785 
786 	/*
787 	 * We don't store route pointers in the routing table anymore
788 	 */
789 	return;
790 }
791 
792 /*
793  * After a routing change, flush old routing
794  * and allocate a (hopefully) better one.
795  */
796 struct inpcb *
797 in6_rtchange(struct inpcb *inp, int errno)
798 {
799 	/*
800 	 * We don't store route pointers in the routing table anymore
801 	 */
802 	return inp;
803 }
804 
805 /*
806  * Lookup PCB in hash list.
807  */
808 struct inpcb *
809 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
810     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
811     struct ifnet *ifp)
812 {
813 	struct inpcbhead *head;
814 	struct inpcb *inp, *tmpinp;
815 	u_short fport = fport_arg, lport = lport_arg;
816 	int faith;
817 
818 	INP_INFO_LOCK_ASSERT(pcbinfo);
819 
820 	if (faithprefix_p != NULL)
821 		faith = (*faithprefix_p)(laddr);
822 	else
823 		faith = 0;
824 
825 	/*
826 	 * First look for an exact match.
827 	 */
828 	tmpinp = NULL;
829 	head = &pcbinfo->ipi_hashbase[
830 	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
831 	    pcbinfo->ipi_hashmask)];
832 	LIST_FOREACH(inp, head, inp_hash) {
833 		/* XXX inp locking */
834 		if ((inp->inp_vflag & INP_IPV6) == 0)
835 			continue;
836 		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
837 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
838 		    inp->inp_fport == fport &&
839 		    inp->inp_lport == lport) {
840 			/*
841 			 * XXX We should be able to directly return
842 			 * the inp here, without any checks.
843 			 * Well unless both bound with SO_REUSEPORT?
844 			 */
845 			if (prison_flag(inp->inp_cred, PR_IP6))
846 				return (inp);
847 			if (tmpinp == NULL)
848 				tmpinp = inp;
849 		}
850 	}
851 	if (tmpinp != NULL)
852 		return (tmpinp);
853 
854 	/*
855 	 * Then look for a wildcard match, if requested.
856 	 */
857 	if (wildcard == INPLOOKUP_WILDCARD) {
858 		struct inpcb *local_wild = NULL, *local_exact = NULL;
859 		struct inpcb *jail_wild = NULL;
860 		int injail;
861 
862 		/*
863 		 * Order of socket selection - we always prefer jails.
864 		 *      1. jailed, non-wild.
865 		 *      2. jailed, wild.
866 		 *      3. non-jailed, non-wild.
867 		 *      4. non-jailed, wild.
868 		 */
869 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
870 		    0, pcbinfo->ipi_hashmask)];
871 		LIST_FOREACH(inp, head, inp_hash) {
872 			/* XXX inp locking */
873 			if ((inp->inp_vflag & INP_IPV6) == 0)
874 				continue;
875 
876 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
877 			    inp->inp_lport != lport) {
878 				continue;
879 			}
880 
881 			/* XXX inp locking */
882 			if (faith && (inp->inp_flags & INP_FAITH) == 0)
883 				continue;
884 
885 			injail = prison_flag(inp->inp_cred, PR_IP6);
886 			if (injail) {
887 				if (prison_check_ip6(inp->inp_cred,
888 				    laddr) != 0)
889 					continue;
890 			} else {
891 				if (local_exact != NULL)
892 					continue;
893 			}
894 
895 			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
896 				if (injail)
897 					return (inp);
898 				else
899 					local_exact = inp;
900 			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
901 				if (injail)
902 					jail_wild = inp;
903 				else
904 					local_wild = inp;
905 			}
906 		} /* LIST_FOREACH */
907 
908 		if (jail_wild != NULL)
909 			return (jail_wild);
910 		if (local_exact != NULL)
911 			return (local_exact);
912 		if (local_wild != NULL)
913 			return (local_wild);
914 	} /* if (wildcard == INPLOOKUP_WILDCARD) */
915 
916 	/*
917 	 * Not found.
918 	 */
919 	return (NULL);
920 }
921 
922 void
923 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
924 {
925 	struct ip6_hdr *ip;
926 
927 	ip = mtod(m, struct ip6_hdr *);
928 	bzero(sin6, sizeof(*sin6));
929 	sin6->sin6_len = sizeof(*sin6);
930 	sin6->sin6_family = AF_INET6;
931 	sin6->sin6_addr = ip->ip6_src;
932 
933 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
934 
935 	return;
936 }
937