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