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