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