in6_pcb.c revision 221247
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 221247 2011-04-30 11:04:34Z bz $");
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
85#include <vm/uma.h>
86
87#include <net/if.h>
88#include <net/if_types.h>
89#include <net/route.h>
90
91#include <netinet/in.h>
92#include <netinet/in_var.h>
93#include <netinet/in_systm.h>
94#include <netinet/tcp_var.h>
95#include <netinet/ip6.h>
96#include <netinet/ip_var.h>
97
98#include <netinet6/ip6_var.h>
99#include <netinet6/nd6.h>
100#include <netinet/in_pcb.h>
101#include <netinet6/in6_pcb.h>
102#include <netinet6/scope6_var.h>
103
104struct	in6_addr zeroin6_addr;
105
106int
107in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
108    struct ucred *cred)
109{
110	struct socket *so = inp->inp_socket;
111	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
112	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
113	u_short	lport = 0;
114	int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
115
116	INP_INFO_WLOCK_ASSERT(pcbinfo);
117	INP_WLOCK_ASSERT(inp);
118
119	if (TAILQ_EMPTY(&V_in6_ifaddrhead))	/* XXX broken! */
120		return (EADDRNOTAVAIL);
121	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
122		return (EINVAL);
123	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
124		wild = INPLOOKUP_WILDCARD;
125	if (nam == NULL) {
126		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
127		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
128			return (error);
129	} else {
130		sin6 = (struct sockaddr_in6 *)nam;
131		if (nam->sa_len != sizeof(*sin6))
132			return (EINVAL);
133		/*
134		 * family check.
135		 */
136		if (nam->sa_family != AF_INET6)
137			return (EAFNOSUPPORT);
138
139		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
140			return(error);
141
142		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
143		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
144			return (error);
145
146		lport = sin6->sin6_port;
147		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
148			/*
149			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
150			 * allow compepte duplication of binding if
151			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
152			 * and a multicast address is bound on both
153			 * new and duplicated sockets.
154			 */
155			if (so->so_options & SO_REUSEADDR)
156				reuseport = SO_REUSEADDR|SO_REUSEPORT;
157		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
158			struct ifaddr *ifa;
159
160			sin6->sin6_port = 0;		/* yech... */
161			if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
162			    NULL &&
163			    (inp->inp_flags & INP_BINDANY) == 0) {
164				return (EADDRNOTAVAIL);
165			}
166
167			/*
168			 * XXX: bind to an anycast address might accidentally
169			 * cause sending a packet with anycast source address.
170			 * We should allow to bind to a deprecated address, since
171			 * the application dares to use it.
172			 */
173			if (ifa != NULL &&
174			    ((struct in6_ifaddr *)ifa)->ia6_flags &
175			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
176				ifa_free(ifa);
177				return (EADDRNOTAVAIL);
178			}
179			if (ifa != NULL)
180				ifa_free(ifa);
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_flags & 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#ifdef INET
208				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
209				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
210					struct sockaddr_in sin;
211
212					in6_sin6_2_sin(&sin, sin6);
213					t = in_pcblookup_local(pcbinfo,
214					    sin.sin_addr, lport,
215					    INPLOOKUP_WILDCARD, cred);
216					if (t &&
217					    ((t->inp_flags &
218					      INP_TIMEWAIT) == 0) &&
219					    (so->so_type != SOCK_STREAM ||
220					     ntohl(t->inp_faddr.s_addr) ==
221					      INADDR_ANY) &&
222					    (inp->inp_cred->cr_uid !=
223					     t->inp_cred->cr_uid))
224						return (EADDRINUSE);
225				}
226#endif
227			}
228			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
229			    lport, wild, cred);
230			if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
231			    intotw(t)->tw_so_options :
232			    t->inp_socket->so_options)) == 0)
233				return (EADDRINUSE);
234#ifdef INET
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_flags & 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#endif
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			/* Undo an address bind that may have occurred. */
265			inp->in6p_laddr = in6addr_any;
266			return (error);
267		}
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	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
295	int error = 0;
296	struct ifnet *ifp = NULL;
297	int scope_ambiguous = 0;
298	struct in6_addr in6a;
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 (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
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 ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
324		return (error);
325
326	error = in6_selectsrc(sin6, inp->in6p_outputopts,
327	    inp, NULL, inp->inp_cred, &ifp, &in6a);
328	if (error)
329		return (error);
330
331	if (ifp && scope_ambiguous &&
332	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
333		return(error);
334	}
335
336	/*
337	 * Do not update this earlier, in case we return with an error.
338	 *
339	 * XXX: this in6_selectsrc result might replace the bound local
340	 * address with the address specified by setsockopt(IPV6_PKTINFO).
341	 * Is it the intended behavior?
342	 */
343	*plocal_addr6 = in6a;
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	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
366	struct in6_addr addr6;
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->inp_flow &= ~IPV6_FLOWLABEL_MASK;
398	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
399		inp->inp_flow |=
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->inp_flow &= ~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#ifdef INET
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#endif
510	{
511		/* scope issues will be handled in in6_getsockaddr(). */
512		error = in6_getsockaddr(so, nam);
513	}
514
515	return error;
516}
517
518int
519in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
520{
521	struct	inpcb *inp;
522	int	error;
523
524	inp = sotoinpcb(so);
525	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
526
527#ifdef INET
528	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
529		error = in_getpeeraddr(so, nam);
530		if (error == 0)
531			in6_sin_2_v4mapsin6_in_sock(nam);
532	} else
533#endif
534	/* scope issues will be handled in in6_getpeeraddr(). */
535	error = in6_getpeeraddr(so, nam);
536
537	return error;
538}
539
540/*
541 * Pass some notification to all connections of a protocol
542 * associated with address dst.  The local address and/or port numbers
543 * may be specified to limit the search.  The "usual action" will be
544 * taken, depending on the ctlinput cmd.  The caller must filter any
545 * cmds that are uninteresting (e.g., no error in the map).
546 * Call the protocol specific routine (if any) to report
547 * any errors for each matching socket.
548 */
549void
550in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
551    u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
552    int cmd, void *cmdarg,
553    struct inpcb *(*notify)(struct inpcb *, int))
554{
555	struct inpcb *inp, *inp_temp;
556	struct sockaddr_in6 sa6_src, *sa6_dst;
557	u_short	fport = fport_arg, lport = lport_arg;
558	u_int32_t flowinfo;
559	int errno;
560
561	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
562		return;
563
564	sa6_dst = (struct sockaddr_in6 *)dst;
565	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
566		return;
567
568	/*
569	 * note that src can be NULL when we get notify by local fragmentation.
570	 */
571	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
572	flowinfo = sa6_src.sin6_flowinfo;
573
574	/*
575	 * Redirects go to all references to the destination,
576	 * and use in6_rtchange to invalidate the route cache.
577	 * Dead host indications: also use in6_rtchange to invalidate
578	 * the cache, and deliver the error to all the sockets.
579	 * Otherwise, if we have knowledge of the local port and address,
580	 * deliver only to that socket.
581	 */
582	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
583		fport = 0;
584		lport = 0;
585		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
586
587		if (cmd != PRC_HOSTDEAD)
588			notify = in6_rtchange;
589	}
590	errno = inet6ctlerrmap[cmd];
591	INP_INFO_WLOCK(pcbinfo);
592	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
593		INP_WLOCK(inp);
594		if ((inp->inp_vflag & INP_IPV6) == 0) {
595			INP_WUNLOCK(inp);
596			continue;
597		}
598
599		/*
600		 * If the error designates a new path MTU for a destination
601		 * and the application (associated with this socket) wanted to
602		 * know the value, notify. Note that we notify for all
603		 * disconnected sockets if the corresponding application
604		 * wanted. This is because some UDP applications keep sending
605		 * sockets disconnected.
606		 * XXX: should we avoid to notify the value to TCP sockets?
607		 */
608		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
609		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
610		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
611			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
612					(u_int32_t *)cmdarg);
613		}
614
615		/*
616		 * Detect if we should notify the error. If no source and
617		 * destination ports are specifed, but non-zero flowinfo and
618		 * local address match, notify the error. This is the case
619		 * when the error is delivered with an encrypted buffer
620		 * by ESP. Otherwise, just compare addresses and ports
621		 * as usual.
622		 */
623		if (lport == 0 && fport == 0 && flowinfo &&
624		    inp->inp_socket != NULL &&
625		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
626		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
627			goto do_notify;
628		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
629					     &sa6_dst->sin6_addr) ||
630			 inp->inp_socket == 0 ||
631			 (lport && inp->inp_lport != lport) ||
632			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
633			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
634					      &sa6_src.sin6_addr)) ||
635			 (fport && inp->inp_fport != fport)) {
636			INP_WUNLOCK(inp);
637			continue;
638		}
639
640	  do_notify:
641		if (notify) {
642			if ((*notify)(inp, errno))
643				INP_WUNLOCK(inp);
644		} else
645			INP_WUNLOCK(inp);
646	}
647	INP_INFO_WUNLOCK(pcbinfo);
648}
649
650/*
651 * Lookup a PCB based on the local address and port.
652 */
653struct inpcb *
654in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
655    u_short lport, int wild_okay, struct ucred *cred)
656{
657	register struct inpcb *inp;
658	int matchwild = 3, wildcard;
659
660	INP_INFO_WLOCK_ASSERT(pcbinfo);
661
662	if (!wild_okay) {
663		struct inpcbhead *head;
664		/*
665		 * Look for an unconnected (wildcard foreign addr) PCB that
666		 * matches the local address and port we're looking for.
667		 */
668		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
669		    0, pcbinfo->ipi_hashmask)];
670		LIST_FOREACH(inp, head, inp_hash) {
671			/* XXX inp locking */
672			if ((inp->inp_vflag & INP_IPV6) == 0)
673				continue;
674			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
675			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
676			    inp->inp_lport == lport) {
677				/* Found. */
678				if (cred == NULL ||
679				    prison_equal_ip6(cred->cr_prison,
680					inp->inp_cred->cr_prison))
681					return (inp);
682			}
683		}
684		/*
685		 * Not found.
686		 */
687		return (NULL);
688	} else {
689		struct inpcbporthead *porthash;
690		struct inpcbport *phd;
691		struct inpcb *match = NULL;
692		/*
693		 * Best fit PCB lookup.
694		 *
695		 * First see if this local port is in use by looking on the
696		 * port hash list.
697		 */
698		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
699		    pcbinfo->ipi_porthashmask)];
700		LIST_FOREACH(phd, porthash, phd_hash) {
701			if (phd->phd_port == lport)
702				break;
703		}
704		if (phd != NULL) {
705			/*
706			 * Port is in use by one or more PCBs. Look for best
707			 * fit.
708			 */
709			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
710				wildcard = 0;
711				if (cred != NULL &&
712				    !prison_equal_ip6(cred->cr_prison,
713					inp->inp_cred->cr_prison))
714					continue;
715				/* XXX inp locking */
716				if ((inp->inp_vflag & INP_IPV6) == 0)
717					continue;
718				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
719					wildcard++;
720				if (!IN6_IS_ADDR_UNSPECIFIED(
721					&inp->in6p_laddr)) {
722					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
723						wildcard++;
724					else if (!IN6_ARE_ADDR_EQUAL(
725					    &inp->in6p_laddr, laddr))
726						continue;
727				} else {
728					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
729						wildcard++;
730				}
731				if (wildcard < matchwild) {
732					match = inp;
733					matchwild = wildcard;
734					if (matchwild == 0)
735						break;
736				}
737			}
738		}
739		return (match);
740	}
741}
742
743void
744in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
745{
746	struct inpcb *in6p;
747	struct ip6_moptions *im6o;
748	int i, gap;
749
750	INP_INFO_RLOCK(pcbinfo);
751	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
752		INP_WLOCK(in6p);
753		im6o = in6p->in6p_moptions;
754		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
755			/*
756			 * Unselect the outgoing ifp for multicast if it
757			 * is being detached.
758			 */
759			if (im6o->im6o_multicast_ifp == ifp)
760				im6o->im6o_multicast_ifp = NULL;
761			/*
762			 * Drop multicast group membership if we joined
763			 * through the interface being detached.
764			 */
765			gap = 0;
766			for (i = 0; i < im6o->im6o_num_memberships; i++) {
767				if (im6o->im6o_membership[i]->in6m_ifp ==
768				    ifp) {
769					in6_mc_leave(im6o->im6o_membership[i],
770					    NULL);
771					gap++;
772				} else if (gap != 0) {
773					im6o->im6o_membership[i - gap] =
774					    im6o->im6o_membership[i];
775				}
776			}
777			im6o->im6o_num_memberships -= gap;
778		}
779		INP_WUNLOCK(in6p);
780	}
781	INP_INFO_RUNLOCK(pcbinfo);
782}
783
784/*
785 * Check for alternatives when higher level complains
786 * about service problems.  For now, invalidate cached
787 * routing information.  If the route was created dynamically
788 * (by a redirect), time to try a default gateway again.
789 */
790void
791in6_losing(struct inpcb *in6p)
792{
793
794	/*
795	 * We don't store route pointers in the routing table anymore
796	 */
797	return;
798}
799
800/*
801 * After a routing change, flush old routing
802 * and allocate a (hopefully) better one.
803 */
804struct inpcb *
805in6_rtchange(struct inpcb *inp, int errno)
806{
807	/*
808	 * We don't store route pointers in the routing table anymore
809	 */
810	return inp;
811}
812
813/*
814 * Lookup PCB in hash list.
815 */
816struct inpcb *
817in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
818    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
819    struct ifnet *ifp)
820{
821	struct inpcbhead *head;
822	struct inpcb *inp, *tmpinp;
823	u_short fport = fport_arg, lport = lport_arg;
824	int faith;
825
826	INP_INFO_LOCK_ASSERT(pcbinfo);
827
828	if (faithprefix_p != NULL)
829		faith = (*faithprefix_p)(laddr);
830	else
831		faith = 0;
832
833	/*
834	 * First look for an exact match.
835	 */
836	tmpinp = NULL;
837	head = &pcbinfo->ipi_hashbase[
838	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
839	    pcbinfo->ipi_hashmask)];
840	LIST_FOREACH(inp, head, inp_hash) {
841		/* XXX inp locking */
842		if ((inp->inp_vflag & INP_IPV6) == 0)
843			continue;
844		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
845		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
846		    inp->inp_fport == fport &&
847		    inp->inp_lport == lport) {
848			/*
849			 * XXX We should be able to directly return
850			 * the inp here, without any checks.
851			 * Well unless both bound with SO_REUSEPORT?
852			 */
853			if (prison_flag(inp->inp_cred, PR_IP6))
854				return (inp);
855			if (tmpinp == NULL)
856				tmpinp = inp;
857		}
858	}
859	if (tmpinp != NULL)
860		return (tmpinp);
861
862	/*
863	 * Then look for a wildcard match, if requested.
864	 */
865	if (wildcard == INPLOOKUP_WILDCARD) {
866		struct inpcb *local_wild = NULL, *local_exact = NULL;
867		struct inpcb *jail_wild = NULL;
868		int injail;
869
870		/*
871		 * Order of socket selection - we always prefer jails.
872		 *      1. jailed, non-wild.
873		 *      2. jailed, wild.
874		 *      3. non-jailed, non-wild.
875		 *      4. non-jailed, wild.
876		 */
877		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
878		    0, pcbinfo->ipi_hashmask)];
879		LIST_FOREACH(inp, head, inp_hash) {
880			/* XXX inp locking */
881			if ((inp->inp_vflag & INP_IPV6) == 0)
882				continue;
883
884			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
885			    inp->inp_lport != lport) {
886				continue;
887			}
888
889			/* XXX inp locking */
890			if (faith && (inp->inp_flags & INP_FAITH) == 0)
891				continue;
892
893			injail = prison_flag(inp->inp_cred, PR_IP6);
894			if (injail) {
895				if (prison_check_ip6(inp->inp_cred,
896				    laddr) != 0)
897					continue;
898			} else {
899				if (local_exact != NULL)
900					continue;
901			}
902
903			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
904				if (injail)
905					return (inp);
906				else
907					local_exact = inp;
908			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
909				if (injail)
910					jail_wild = inp;
911				else
912					local_wild = inp;
913			}
914		} /* LIST_FOREACH */
915
916		if (jail_wild != NULL)
917			return (jail_wild);
918		if (local_exact != NULL)
919			return (local_exact);
920		if (local_wild != NULL)
921			return (local_wild);
922	} /* if (wildcard == INPLOOKUP_WILDCARD) */
923
924	/*
925	 * Not found.
926	 */
927	return (NULL);
928}
929
930void
931init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
932{
933	struct ip6_hdr *ip;
934
935	ip = mtod(m, struct ip6_hdr *);
936	bzero(sin6, sizeof(*sin6));
937	sin6->sin6_len = sizeof(*sin6);
938	sin6->sin6_family = AF_INET6;
939	sin6->sin6_addr = ip->ip6_src;
940
941	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
942
943	return;
944}
945