in6_pcb.c revision 185571
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 185571 2008-12-02 21:37:28Z bz $");
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
110struct	in6_addr zeroin6_addr;
111
112int
113in6_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 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) {
134		int error;
135
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 (prison_local_ip6(cred, &sin6->sin6_addr,
149		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0)
150			return (EINVAL);
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)
162				reuseport = SO_REUSEADDR|SO_REUSEPORT;
163		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
164			struct ifaddr *ia = NULL;
165
166			sin6->sin6_port = 0;		/* yech... */
167			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
168				return (EADDRNOTAVAIL);
169
170			/*
171			 * XXX: bind to an anycast address might accidentally
172			 * cause sending a packet with anycast source address.
173			 * We should allow to bind to a deprecated address, since
174			 * the application dares to use it.
175			 */
176			if (ia &&
177			    ((struct in6_ifaddr *)ia)->ia6_flags &
178			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
179				return (EADDRNOTAVAIL);
180			}
181		}
182		if (lport) {
183			struct inpcb *t;
184
185			/* GROSS */
186			if (ntohs(lport) <= V_ipport_reservedhigh &&
187			    ntohs(lport) >= V_ipport_reservedlow &&
188			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
189			    0))
190				return (EACCES);
191			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
192			    priv_check_cred(inp->inp_cred,
193			    PRIV_NETINET_REUSEPORT, 0) != 0) {
194				t = in6_pcblookup_local(pcbinfo,
195				    &sin6->sin6_addr, lport,
196				    INPLOOKUP_WILDCARD, cred);
197				if (t &&
198				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
199				    (so->so_type != SOCK_STREAM ||
200				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
201				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
202				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
203				     (t->inp_socket->so_options & SO_REUSEPORT)
204				      == 0) && (inp->inp_cred->cr_uid !=
205				     t->inp_cred->cr_uid))
206					return (EADDRINUSE);
207				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
208				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
209					struct sockaddr_in sin;
210
211					in6_sin6_2_sin(&sin, sin6);
212					t = in_pcblookup_local(pcbinfo,
213					    sin.sin_addr, lport,
214					    INPLOOKUP_WILDCARD, cred);
215					if (t &&
216					    ((t->inp_vflag &
217					      INP_TIMEWAIT) == 0) &&
218					    (so->so_type != SOCK_STREAM ||
219					     ntohl(t->inp_faddr.s_addr) ==
220					      INADDR_ANY) &&
221					    (inp->inp_cred->cr_uid !=
222					     t->inp_cred->cr_uid))
223						return (EADDRINUSE);
224				}
225			}
226			if (prison_local_ip6(cred, &sin6->sin6_addr,
227			    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0)
228				return (EADDRNOTAVAIL);
229			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
230			    lport, wild, cred);
231			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
232			    intotw(t)->tw_so_options :
233			    t->inp_socket->so_options)) == 0)
234				return (EADDRINUSE);
235			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
236			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
237				struct sockaddr_in sin;
238
239				in6_sin6_2_sin(&sin, sin6);
240				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
241				    lport, wild, cred);
242				if (t && t->inp_vflag & INP_TIMEWAIT) {
243					if ((reuseport &
244					    intotw(t)->tw_so_options) == 0 &&
245					    (ntohl(t->inp_laddr.s_addr) !=
246					     INADDR_ANY || ((inp->inp_vflag &
247					     INP_IPV6PROTO) ==
248					     (t->inp_vflag & INP_IPV6PROTO))))
249						return (EADDRINUSE);
250				}
251				else if (t &&
252				    (reuseport & t->inp_socket->so_options)
253				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
254				    INADDR_ANY || INP_SOCKAF(so) ==
255				     INP_SOCKAF(t->inp_socket)))
256					return (EADDRINUSE);
257			}
258		}
259		inp->in6p_laddr = sin6->sin6_addr;
260	}
261	if (prison_local_ip6(cred, &inp->in6p_laddr,
262	    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0)
263		return (EINVAL);
264	if (lport == 0) {
265		int e;
266		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
267			return (e);
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
300	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
301	INP_WLOCK_ASSERT(inp);
302
303	if (nam->sa_len != sizeof (*sin6))
304		return (EINVAL);
305	if (sin6->sin6_family != AF_INET6)
306		return (EAFNOSUPPORT);
307	if (sin6->sin6_port == 0)
308		return (EADDRNOTAVAIL);
309
310	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
311		scope_ambiguous = 1;
312	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
313		return(error);
314
315	if (V_in6_ifaddr) {
316		/*
317		 * If the destination address is UNSPECIFIED addr,
318		 * use the loopback addr, e.g ::1.
319		 */
320		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
321			sin6->sin6_addr = in6addr_loopback;
322	}
323	if (prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr) != 0)
324		return (EADDRNOTAVAIL);
325
326	/*
327	 * XXX: in6_selectsrc might replace the bound local address
328	 * with the address specified by setsockopt(IPV6_PKTINFO).
329	 * Is it the intended behavior?
330	 */
331	*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
332				      inp, NULL,
333				      inp->inp_cred,
334				      &ifp, &error);
335	if (ifp && scope_ambiguous &&
336	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
337		return(error);
338	}
339
340	if (*plocal_addr6 == NULL) {
341		if (error == 0)
342			error = EADDRNOTAVAIL;
343		return (error);
344	}
345	/*
346	 * Don't do pcblookup call here; return interface in
347	 * plocal_addr6
348	 * and exit to caller, that will do the lookup.
349	 */
350
351	return (0);
352}
353
354/*
355 * Outer subroutine:
356 * Connect from a socket to a specified address.
357 * Both address and port must be specified in argument sin.
358 * If don't have a local address for this socket yet,
359 * then pick one.
360 */
361int
362in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
363    struct ucred *cred)
364{
365	struct in6_addr *addr6;
366	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
367	int error;
368
369	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
370	INP_WLOCK_ASSERT(inp);
371
372	/*
373	 * Call inner routine, to assign local interface address.
374	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
375	 */
376	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
377		return (error);
378
379	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
380			       sin6->sin6_port,
381			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
382			      ? addr6 : &inp->in6p_laddr,
383			      inp->inp_lport, 0, NULL) != NULL) {
384		return (EADDRINUSE);
385	}
386	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
387		if (inp->inp_lport == 0) {
388			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
389			if (error)
390				return (error);
391		}
392		inp->in6p_laddr = *addr6;
393	}
394	inp->in6p_faddr = sin6->sin6_addr;
395	inp->inp_fport = sin6->sin6_port;
396	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
397	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
398	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
399		inp->in6p_flowinfo |=
400		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
401
402	in_pcbrehash(inp);
403
404	return (0);
405}
406
407void
408in6_pcbdisconnect(struct inpcb *inp)
409{
410
411	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
412	INP_WLOCK_ASSERT(inp);
413
414	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
415	inp->inp_fport = 0;
416	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
417	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
418	in_pcbrehash(inp);
419}
420
421struct sockaddr *
422in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
423{
424	struct sockaddr_in6 *sin6;
425
426	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
427	bzero(sin6, sizeof *sin6);
428	sin6->sin6_family = AF_INET6;
429	sin6->sin6_len = sizeof(*sin6);
430	sin6->sin6_port = port;
431	sin6->sin6_addr = *addr_p;
432	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
433
434	return (struct sockaddr *)sin6;
435}
436
437struct sockaddr *
438in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
439{
440	struct sockaddr_in sin;
441	struct sockaddr_in6 *sin6_p;
442
443	bzero(&sin, sizeof sin);
444	sin.sin_family = AF_INET;
445	sin.sin_len = sizeof(sin);
446	sin.sin_port = port;
447	sin.sin_addr = *addr_p;
448
449	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
450		M_WAITOK);
451	in6_sin_2_v4mapsin6(&sin, sin6_p);
452
453	return (struct sockaddr *)sin6_p;
454}
455
456int
457in6_getsockaddr(struct socket *so, struct sockaddr **nam)
458{
459	register struct inpcb *inp;
460	struct in6_addr addr;
461	in_port_t port;
462
463	inp = sotoinpcb(so);
464	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
465
466	INP_RLOCK(inp);
467	port = inp->inp_lport;
468	addr = inp->in6p_laddr;
469	INP_RUNLOCK(inp);
470
471	*nam = in6_sockaddr(port, &addr);
472	return 0;
473}
474
475int
476in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
477{
478	struct inpcb *inp;
479	struct in6_addr addr;
480	in_port_t port;
481
482	inp = sotoinpcb(so);
483	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
484
485	INP_RLOCK(inp);
486	port = inp->inp_fport;
487	addr = inp->in6p_faddr;
488	INP_RUNLOCK(inp);
489
490	*nam = in6_sockaddr(port, &addr);
491	return 0;
492}
493
494int
495in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
496{
497	struct	inpcb *inp;
498	int	error;
499
500	inp = sotoinpcb(so);
501	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
502
503	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
504		error = in_getsockaddr(so, nam);
505		if (error == 0)
506			in6_sin_2_v4mapsin6_in_sock(nam);
507	} else {
508		/* scope issues will be handled in in6_getsockaddr(). */
509		error = in6_getsockaddr(so, nam);
510	}
511
512	return error;
513}
514
515int
516in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
517{
518	struct	inpcb *inp;
519	int	error;
520
521	inp = sotoinpcb(so);
522	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
523
524	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
525		error = in_getpeeraddr(so, nam);
526		if (error == 0)
527			in6_sin_2_v4mapsin6_in_sock(nam);
528	} else
529	/* scope issues will be handled in in6_getpeeraddr(). */
530	error = in6_getpeeraddr(so, nam);
531
532	return error;
533}
534
535/*
536 * Pass some notification to all connections of a protocol
537 * associated with address dst.  The local address and/or port numbers
538 * may be specified to limit the search.  The "usual action" will be
539 * taken, depending on the ctlinput cmd.  The caller must filter any
540 * cmds that are uninteresting (e.g., no error in the map).
541 * Call the protocol specific routine (if any) to report
542 * any errors for each matching socket.
543 */
544void
545in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
546    u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
547    int cmd, void *cmdarg,
548    struct inpcb *(*notify)(struct inpcb *, int))
549{
550	struct inpcb *inp, *inp_temp;
551	struct sockaddr_in6 sa6_src, *sa6_dst;
552	u_short	fport = fport_arg, lport = lport_arg;
553	u_int32_t flowinfo;
554	int errno;
555
556	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
557		return;
558
559	sa6_dst = (struct sockaddr_in6 *)dst;
560	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
561		return;
562
563	/*
564	 * note that src can be NULL when we get notify by local fragmentation.
565	 */
566	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
567	flowinfo = sa6_src.sin6_flowinfo;
568
569	/*
570	 * Redirects go to all references to the destination,
571	 * and use in6_rtchange to invalidate the route cache.
572	 * Dead host indications: also use in6_rtchange to invalidate
573	 * the cache, and deliver the error to all the sockets.
574	 * Otherwise, if we have knowledge of the local port and address,
575	 * deliver only to that socket.
576	 */
577	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
578		fport = 0;
579		lport = 0;
580		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
581
582		if (cmd != PRC_HOSTDEAD)
583			notify = in6_rtchange;
584	}
585	errno = inet6ctlerrmap[cmd];
586	INP_INFO_WLOCK(pcbinfo);
587	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
588		INP_WLOCK(inp);
589		if ((inp->inp_vflag & INP_IPV6) == 0) {
590			INP_WUNLOCK(inp);
591			continue;
592		}
593
594		/*
595		 * If the error designates a new path MTU for a destination
596		 * and the application (associated with this socket) wanted to
597		 * know the value, notify. Note that we notify for all
598		 * disconnected sockets if the corresponding application
599		 * wanted. This is because some UDP applications keep sending
600		 * sockets disconnected.
601		 * XXX: should we avoid to notify the value to TCP sockets?
602		 */
603		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
604		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
605		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
606			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
607					(u_int32_t *)cmdarg);
608		}
609
610		/*
611		 * Detect if we should notify the error. If no source and
612		 * destination ports are specifed, but non-zero flowinfo and
613		 * local address match, notify the error. This is the case
614		 * when the error is delivered with an encrypted buffer
615		 * by ESP. Otherwise, just compare addresses and ports
616		 * as usual.
617		 */
618		if (lport == 0 && fport == 0 && flowinfo &&
619		    inp->inp_socket != NULL &&
620		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
621		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
622			goto do_notify;
623		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
624					     &sa6_dst->sin6_addr) ||
625			 inp->inp_socket == 0 ||
626			 (lport && inp->inp_lport != lport) ||
627			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
628			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
629					      &sa6_src.sin6_addr)) ||
630			 (fport && inp->inp_fport != fport)) {
631			INP_WUNLOCK(inp);
632			continue;
633		}
634
635	  do_notify:
636		if (notify) {
637			if ((*notify)(inp, errno))
638				INP_WUNLOCK(inp);
639		} else
640			INP_WUNLOCK(inp);
641	}
642	INP_INFO_WUNLOCK(pcbinfo);
643}
644
645/*
646 * Lookup a PCB based on the local address and port.
647 */
648struct inpcb *
649in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
650    u_short lport, int wild_okay, struct ucred *cred)
651{
652	register struct inpcb *inp;
653	int matchwild = 3, wildcard;
654
655	INP_INFO_WLOCK_ASSERT(pcbinfo);
656
657	if (!wild_okay) {
658		struct inpcbhead *head;
659		/*
660		 * Look for an unconnected (wildcard foreign addr) PCB that
661		 * matches the local address and port we're looking for.
662		 */
663		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
664		    0, pcbinfo->ipi_hashmask)];
665		LIST_FOREACH(inp, head, inp_hash) {
666			/* XXX inp locking */
667			if ((inp->inp_vflag & INP_IPV6) == 0)
668				continue;
669			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
670			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
671			    inp->inp_lport == lport) {
672				/* Found. */
673				if (cred == NULL ||
674				    inp->inp_cred->cr_prison == cred->cr_prison)
675					return (inp);
676			}
677		}
678		/*
679		 * Not found.
680		 */
681		return (NULL);
682	} else {
683		struct inpcbporthead *porthash;
684		struct inpcbport *phd;
685		struct inpcb *match = NULL;
686		/*
687		 * Best fit PCB lookup.
688		 *
689		 * First see if this local port is in use by looking on the
690		 * port hash list.
691		 */
692		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
693		    pcbinfo->ipi_porthashmask)];
694		LIST_FOREACH(phd, porthash, phd_hash) {
695			if (phd->phd_port == lport)
696				break;
697		}
698		if (phd != NULL) {
699			/*
700			 * Port is in use by one or more PCBs. Look for best
701			 * fit.
702			 */
703			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
704				wildcard = 0;
705				if (cred != NULL &&
706				    inp->inp_cred->cr_prison != cred->cr_prison)
707					continue;
708				/* XXX inp locking */
709				if ((inp->inp_vflag & INP_IPV6) == 0)
710					continue;
711				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
712					wildcard++;
713				if (!IN6_IS_ADDR_UNSPECIFIED(
714					&inp->in6p_laddr)) {
715					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
716						wildcard++;
717					else if (!IN6_ARE_ADDR_EQUAL(
718					    &inp->in6p_laddr, laddr))
719						continue;
720				} else {
721					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
722						wildcard++;
723				}
724				if (wildcard < matchwild) {
725					match = inp;
726					matchwild = wildcard;
727					if (matchwild == 0)
728						break;
729				}
730			}
731		}
732		return (match);
733	}
734}
735
736void
737in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
738{
739	struct in6pcb *in6p;
740	struct ip6_moptions *im6o;
741	struct in6_multi_mship *imm, *nimm;
742
743	INP_INFO_RLOCK(pcbinfo);
744	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
745		INP_WLOCK(in6p);
746		im6o = in6p->in6p_moptions;
747		if ((in6p->inp_vflag & INP_IPV6) &&
748		    im6o) {
749			/*
750			 * Unselect the outgoing interface if it is being
751			 * detached.
752			 */
753			if (im6o->im6o_multicast_ifp == ifp)
754				im6o->im6o_multicast_ifp = NULL;
755
756			/*
757			 * Drop multicast group membership if we joined
758			 * through the interface being detached.
759			 * XXX controversial - is it really legal for kernel
760			 * to force this?
761			 */
762			for (imm = im6o->im6o_memberships.lh_first;
763			     imm != NULL; imm = nimm) {
764				nimm = imm->i6mm_chain.le_next;
765				if (imm->i6mm_maddr->in6m_ifp == ifp) {
766					LIST_REMOVE(imm, i6mm_chain);
767					in6_delmulti(imm->i6mm_maddr);
768					free(imm, M_IP6MADDR);
769				}
770			}
771		}
772		INP_WUNLOCK(in6p);
773	}
774	INP_INFO_RUNLOCK(pcbinfo);
775}
776
777/*
778 * Check for alternatives when higher level complains
779 * about service problems.  For now, invalidate cached
780 * routing information.  If the route was created dynamically
781 * (by a redirect), time to try a default gateway again.
782 */
783void
784in6_losing(struct inpcb *in6p)
785{
786
787	/*
788	 * We don't store route pointers in the routing table anymore
789	 */
790	return;
791}
792
793/*
794 * After a routing change, flush old routing
795 * and allocate a (hopefully) better one.
796 */
797struct inpcb *
798in6_rtchange(struct inpcb *inp, int errno)
799{
800	/*
801	 * We don't store route pointers in the routing table anymore
802	 */
803	return inp;
804}
805
806/*
807 * Lookup PCB in hash list.
808 */
809struct inpcb *
810in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
811    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
812    struct ifnet *ifp)
813{
814	struct inpcbhead *head;
815	struct inpcb *inp, *tmpinp;
816	u_short fport = fport_arg, lport = lport_arg;
817	int faith;
818
819	INP_INFO_LOCK_ASSERT(pcbinfo);
820
821	if (faithprefix_p != NULL)
822		faith = (*faithprefix_p)(laddr);
823	else
824		faith = 0;
825
826	/*
827	 * First look for an exact match.
828	 */
829	tmpinp = NULL;
830	head = &pcbinfo->ipi_hashbase[
831	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
832	    pcbinfo->ipi_hashmask)];
833	LIST_FOREACH(inp, head, inp_hash) {
834		/* XXX inp locking */
835		if ((inp->inp_vflag & INP_IPV6) == 0)
836			continue;
837		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
838		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
839		    inp->inp_fport == fport &&
840		    inp->inp_lport == lport) {
841			/*
842			 * XXX We should be able to directly return
843			 * the inp here, without any checks.
844			 * Well unless both bound with SO_REUSEPORT?
845			 */
846			if (jailed(inp->inp_cred))
847				return (inp);
848			if (tmpinp == NULL)
849				tmpinp = inp;
850		}
851	}
852	if (tmpinp != NULL)
853		return (tmpinp);
854
855	/*
856	 * Then look for a wildcard match, if requested.
857	 */
858	if (wildcard == INPLOOKUP_WILDCARD) {
859		struct inpcb *local_wild = NULL, *local_exact = NULL;
860		struct inpcb *jail_wild = NULL;
861		int injail;
862
863		/*
864		 * Order of socket selection - we always prefer jails.
865		 *      1. jailed, non-wild.
866		 *      2. jailed, wild.
867		 *      3. non-jailed, non-wild.
868		 *      4. non-jailed, wild.
869		 */
870		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
871		    0, pcbinfo->ipi_hashmask)];
872		LIST_FOREACH(inp, head, inp_hash) {
873			/* XXX inp locking */
874			if ((inp->inp_vflag & INP_IPV6) == 0)
875				continue;
876
877			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
878			    inp->inp_lport != lport) {
879				continue;
880			}
881
882			/* XXX inp locking */
883			if (faith && (inp->inp_flags & INP_FAITH) == 0)
884				continue;
885
886			injail = jailed(inp->inp_cred);
887			if (injail) {
888				if (!prison_check_ip6(inp->inp_cred, laddr))
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
922void
923init_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