in6_pcb.c revision 175162
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
2853541Sshin *
29174510Sobrien *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
3053541Sshin */
3153541Sshin
32139826Simp/*-
3353541Sshin * Copyright (c) 1982, 1986, 1991, 1993
3453541Sshin *	The Regents of the University of California.  All rights reserved.
3553541Sshin *
3653541Sshin * Redistribution and use in source and binary forms, with or without
3753541Sshin * modification, are permitted provided that the following conditions
3853541Sshin * are met:
3953541Sshin * 1. Redistributions of source code must retain the above copyright
4053541Sshin *    notice, this list of conditions and the following disclaimer.
4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4253541Sshin *    notice, this list of conditions and the following disclaimer in the
4353541Sshin *    documentation and/or other materials provided with the distribution.
4453541Sshin * 4. Neither the name of the University nor the names of its contributors
4553541Sshin *    may be used to endorse or promote products derived from this software
4653541Sshin *    without specific prior written permission.
4753541Sshin *
4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5153541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5853541Sshin * SUCH DAMAGE.
5953541Sshin *
6053541Sshin *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
6153541Sshin */
6253541Sshin
63174510Sobrien#include <sys/cdefs.h>
64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 175162 2008-01-08 19:08:58Z obrien $");
65174510Sobrien
6678064Sume#include "opt_inet.h"
6778064Sume#include "opt_inet6.h"
6855009Sshin#include "opt_ipsec.h"
69174717Srwatson#include "opt_mac.h"
7053541Sshin
7153541Sshin#include <sys/param.h>
7253541Sshin#include <sys/systm.h>
7353541Sshin#include <sys/malloc.h>
7453541Sshin#include <sys/mbuf.h>
7555679Sshin#include <sys/domain.h>
7653541Sshin#include <sys/protosw.h>
7753541Sshin#include <sys/socket.h>
7853541Sshin#include <sys/socketvar.h>
7953541Sshin#include <sys/sockio.h>
8053541Sshin#include <sys/errno.h>
8153541Sshin#include <sys/time.h>
82164033Srwatson#include <sys/priv.h>
8353541Sshin#include <sys/proc.h>
8453541Sshin#include <sys/jail.h>
8553541Sshin
8692767Sjeff#include <vm/uma.h>
8753541Sshin
8853541Sshin#include <net/if.h>
8953541Sshin#include <net/if_types.h>
9053541Sshin#include <net/route.h>
9153541Sshin
9253541Sshin#include <netinet/in.h>
9353541Sshin#include <netinet/in_var.h>
9453541Sshin#include <netinet/in_systm.h>
9598102Shsu#include <netinet/tcp_var.h>
9662587Sitojun#include <netinet/ip6.h>
9755679Sshin#include <netinet/ip_var.h>
9853541Sshin#include <netinet6/ip6_var.h>
9953541Sshin#include <netinet6/nd6.h>
10053541Sshin#include <netinet/in_pcb.h>
10153541Sshin#include <netinet6/in6_pcb.h>
102148385Sume#include <netinet6/scope6_var.h>
10353541Sshin
104171167Sgnn#ifdef IPSEC
105105199Ssam#include <netipsec/ipsec.h>
106105199Ssam#include <netipsec/ipsec6.h>
107105199Ssam#include <netipsec/key.h>
108171167Sgnn#endif /* IPSEC */
109105199Ssam
110174717Srwatson#include <security/mac/mac_framework.h>
111174717Srwatson
11253541Sshinstruct	in6_addr zeroin6_addr;
11353541Sshin
11453541Sshinint
115171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
116171259Sdelphij    struct ucred *cred)
11753541Sshin{
11853541Sshin	struct socket *so = inp->inp_socket;
11953541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
12053541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
12153541Sshin	u_short	lport = 0;
12297658Stanimura	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
12353541Sshin
124132714Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
125132714Srwatson	INP_LOCK_ASSERT(inp);
126132714Srwatson
12753541Sshin	if (!in6_ifaddr) /* XXX broken! */
12853541Sshin		return (EADDRNOTAVAIL);
12953541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
130120856Sume		return (EINVAL);
13153541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
132160024Sbz		wild = INPLOOKUP_WILDCARD;
13353541Sshin	if (nam) {
134148385Sume		int error;
135148385Sume
13653541Sshin		sin6 = (struct sockaddr_in6 *)nam;
13753541Sshin		if (nam->sa_len != sizeof(*sin6))
138120856Sume			return (EINVAL);
13953541Sshin		/*
14053541Sshin		 * family check.
14153541Sshin		 */
14253541Sshin		if (nam->sa_family != AF_INET6)
143120856Sume			return (EAFNOSUPPORT);
14453541Sshin
145148385Sume		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
146148385Sume			return(error);
14753541Sshin
14853541Sshin		lport = sin6->sin6_port;
14953541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
15053541Sshin			/*
15153541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
15253541Sshin			 * allow compepte duplication of binding if
15353541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
15453541Sshin			 * and a multicast address is bound on both
15553541Sshin			 * new and duplicated sockets.
15653541Sshin			 */
15753541Sshin			if (so->so_options & SO_REUSEADDR)
15853541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
15953541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
16053541Sshin			struct ifaddr *ia = NULL;
16153541Sshin
16253541Sshin			sin6->sin6_port = 0;		/* yech... */
16353541Sshin			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
164120856Sume				return (EADDRNOTAVAIL);
16553541Sshin
16653541Sshin			/*
16753541Sshin			 * XXX: bind to an anycast address might accidentally
16853541Sshin			 * cause sending a packet with anycast source address.
16978064Sume			 * We should allow to bind to a deprecated address, since
170120913Sume			 * the application dares to use it.
17153541Sshin			 */
17253541Sshin			if (ia &&
17353541Sshin			    ((struct in6_ifaddr *)ia)->ia6_flags &
17478064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
175120856Sume				return (EADDRNOTAVAIL);
17653541Sshin			}
17753541Sshin		}
17853541Sshin		if (lport) {
17953541Sshin			struct inpcb *t;
18053541Sshin
18153541Sshin			/* GROSS */
182156877Sdwmalone			if (ntohs(lport) <= ipport_reservedhigh &&
183156877Sdwmalone			    ntohs(lport) >= ipport_reservedlow &&
184164033Srwatson			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
185170587Srwatson			    0))
186120856Sume				return (EACCES);
187159976Spjd			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
188168932Srwatson			    priv_check_cred(so->so_cred,
189170587Srwatson			    PRIV_NETINET_REUSEPORT, 0) != 0) {
19055679Sshin				t = in6_pcblookup_local(pcbinfo,
19153541Sshin				    &sin6->sin6_addr, lport,
19253541Sshin				    INPLOOKUP_WILDCARD);
193132699Syar				if (t &&
194132699Syar				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
195132699Syar				    (so->so_type != SOCK_STREAM ||
196132699Syar				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
19797658Stanimura				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
198171260Sdelphij				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
199171260Sdelphij				     (t->inp_socket->so_options & SO_REUSEPORT)
200116453Scognet				      == 0) && (so->so_cred->cr_uid !=
20197658Stanimura				     t->inp_socket->so_cred->cr_uid))
20297658Stanimura					return (EADDRINUSE);
20378064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
20455679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
20555679Sshin					struct sockaddr_in sin;
20655679Sshin
20755679Sshin					in6_sin6_2_sin(&sin, sin6);
20855679Sshin					t = in_pcblookup_local(pcbinfo,
20955679Sshin						sin.sin_addr, lport,
21055679Sshin						INPLOOKUP_WILDCARD);
211132699Syar					if (t &&
212132699Syar					    ((t->inp_vflag &
213132699Syar					      INP_TIMEWAIT) == 0) &&
214132699Syar					    (so->so_type != SOCK_STREAM ||
215132699Syar					     ntohl(t->inp_faddr.s_addr) ==
216132699Syar					      INADDR_ANY) &&
21755679Sshin					    (so->so_cred->cr_uid !=
218132794Syar					     t->inp_socket->so_cred->cr_uid))
21955679Sshin						return (EADDRINUSE);
22055679Sshin				}
22153541Sshin			}
22253541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
22353541Sshin						lport, wild);
224116453Scognet			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
225171260Sdelphij			    intotw(t)->tw_so_options :
226116453Scognet			    t->inp_socket->so_options)) == 0)
227120856Sume				return (EADDRINUSE);
22878064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
22955679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
23055679Sshin				struct sockaddr_in sin;
23155679Sshin
23255679Sshin				in6_sin6_2_sin(&sin, sin6);
23355679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
23455679Sshin						       lport, wild);
235116453Scognet				if (t && t->inp_vflag & INP_TIMEWAIT) {
236171260Sdelphij					if ((reuseport &
237116453Scognet					    intotw(t)->tw_so_options) == 0 &&
238116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
239171260Sdelphij					     INADDR_ANY || ((inp->inp_vflag &
240171260Sdelphij					     INP_IPV6PROTO) ==
241116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
242116453Scognet						return (EADDRINUSE);
243116453Scognet				}
244171260Sdelphij				else if (t &&
245171260Sdelphij				    (reuseport & t->inp_socket->so_options)
246171260Sdelphij				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
247116453Scognet				    INADDR_ANY || INP_SOCKAF(so) ==
24897658Stanimura				     INP_SOCKAF(t->inp_socket)))
24997658Stanimura					return (EADDRINUSE);
25055679Sshin			}
25153541Sshin		}
25253541Sshin		inp->in6p_laddr = sin6->sin6_addr;
25353541Sshin	}
25453541Sshin	if (lport == 0) {
25562587Sitojun		int e;
256127505Spjd		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
257120856Sume			return (e);
25862587Sitojun	}
25962587Sitojun	else {
26062587Sitojun		inp->inp_lport = lport;
26162587Sitojun		if (in_pcbinshash(inp) != 0) {
26262587Sitojun			inp->in6p_laddr = in6addr_any;
26362587Sitojun			inp->inp_lport = 0;
26462587Sitojun			return (EAGAIN);
26553541Sshin		}
26653541Sshin	}
267120856Sume	return (0);
26853541Sshin}
26953541Sshin
27053541Sshin/*
27153541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
27253541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
27353541Sshin *   address (in mbuf 'nam') and then determine local host address
27453541Sshin *   (i.e., which interface) to use to access that remote host.
27553541Sshin *
27653541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
27753541Sshin *   slightly different version for T/TCP.  (This is more than
27853541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
27953541Sshin *   have forced minor changes in every protocol).
28053541Sshin */
28153541Sshinint
282171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
283171259Sdelphij    struct in6_addr **plocal_addr6)
28453541Sshin{
28553541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
286148385Sume	int error = 0;
28753541Sshin	struct ifnet *ifp = NULL;
288148385Sume	int scope_ambiguous = 0;
28953541Sshin
290158011Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
291158011Srwatson	INP_LOCK_ASSERT(inp);
292158011Srwatson
29353541Sshin	if (nam->sa_len != sizeof (*sin6))
29453541Sshin		return (EINVAL);
29553541Sshin	if (sin6->sin6_family != AF_INET6)
29653541Sshin		return (EAFNOSUPPORT);
29753541Sshin	if (sin6->sin6_port == 0)
29853541Sshin		return (EADDRNOTAVAIL);
29953541Sshin
300148385Sume	if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
301148385Sume		scope_ambiguous = 1;
302148385Sume	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
303148385Sume		return(error);
304148385Sume
30553541Sshin	if (in6_ifaddr) {
30653541Sshin		/*
30753541Sshin		 * If the destination address is UNSPECIFIED addr,
30853541Sshin		 * use the loopback addr, e.g ::1.
30953541Sshin		 */
31053541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
31153541Sshin			sin6->sin6_addr = in6addr_loopback;
31253541Sshin	}
313148385Sume
314148385Sume	/*
315148385Sume	 * XXX: in6_selectsrc might replace the bound local address
316148385Sume	 * with the address specified by setsockopt(IPV6_PKTINFO).
317148385Sume	 * Is it the intended behavior?
318148385Sume	 */
319148385Sume	*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
320148385Sume				      inp->in6p_moptions, NULL,
321148385Sume				      &inp->in6p_laddr, &ifp, &error);
322148385Sume	if (ifp && scope_ambiguous &&
323148385Sume	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
324148385Sume		return(error);
32553541Sshin	}
326148385Sume
327148385Sume	if (*plocal_addr6 == 0) {
328148385Sume		if (error == 0)
329148385Sume			error = EADDRNOTAVAIL;
330148385Sume		return (error);
331148385Sume	}
332148385Sume	/*
333148385Sume	 * Don't do pcblookup call here; return interface in
334148385Sume	 * plocal_addr6
335148385Sume	 * and exit to caller, that will do the lookup.
336148385Sume	 */
337148385Sume
338120856Sume	return (0);
33953541Sshin}
34053541Sshin
34153541Sshin/*
34253541Sshin * Outer subroutine:
34353541Sshin * Connect from a socket to a specified address.
34453541Sshin * Both address and port must be specified in argument sin.
34553541Sshin * If don't have a local address for this socket yet,
34653541Sshin * then pick one.
34753541Sshin */
34853541Sshinint
349171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
350171259Sdelphij    struct ucred *cred)
35153541Sshin{
35253541Sshin	struct in6_addr *addr6;
35353541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
35453541Sshin	int error;
35553541Sshin
356132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
357132714Srwatson	INP_LOCK_ASSERT(inp);
358132714Srwatson
35953541Sshin	/*
36095023Ssuz	 * Call inner routine, to assign local interface address.
36195023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
36253541Sshin	 */
36353541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
364120856Sume		return (error);
36553541Sshin
36653541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
36753541Sshin			       sin6->sin6_port,
36853541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
36953541Sshin			      ? addr6 : &inp->in6p_laddr,
37053541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
37153541Sshin		return (EADDRINUSE);
37253541Sshin	}
37353541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
37453541Sshin		if (inp->inp_lport == 0) {
375127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
37653541Sshin			if (error)
37753541Sshin				return (error);
37853541Sshin		}
37953541Sshin		inp->in6p_laddr = *addr6;
38053541Sshin	}
38153541Sshin	inp->in6p_faddr = sin6->sin6_addr;
38253541Sshin	inp->inp_fport = sin6->sin6_port;
38378064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
38478064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
38578064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
38655679Sshin		inp->in6p_flowinfo |=
387120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
38853541Sshin
38953541Sshin	in_pcbrehash(inp);
390171133Sgnn
39153541Sshin	return (0);
39253541Sshin}
39353541Sshin
39453541Sshinvoid
395171259Sdelphijin6_pcbdisconnect(struct inpcb *inp)
39653541Sshin{
397132714Srwatson
398132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
399132714Srwatson	INP_LOCK_ASSERT(inp);
400132714Srwatson
40153541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
40253541Sshin	inp->inp_fport = 0;
40378064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
40478064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
40553541Sshin	in_pcbrehash(inp);
40653541Sshin}
40753541Sshin
40853541Sshinvoid
409157373Srwatsonin6_pcbdetach(struct inpcb *inp)
41053541Sshin{
411157373Srwatson
412157373Srwatson	KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL"));
413157373Srwatson	inp->inp_socket->so_pcb = NULL;
414157373Srwatson	inp->inp_socket = NULL;
415157373Srwatson}
416157373Srwatson
417157373Srwatsonvoid
418157373Srwatsonin6_pcbfree(struct inpcb *inp)
419157373Srwatson{
42053541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
42153541Sshin
422157373Srwatson	KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL"));
423132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
424132714Srwatson	INP_LOCK_ASSERT(inp);
425132714Srwatson
426171167Sgnn#ifdef IPSEC
42758452Sgreen	if (inp->in6p_sp != NULL)
42858452Sgreen		ipsec6_delete_pcbpolicy(inp);
429171167Sgnn#endif /* IPSEC */
43053541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
43153541Sshin	in_pcbremlists(inp);
432171260Sdelphij	ip6_freepcbopts(inp->in6p_outputopts);
433171260Sdelphij	ip6_freemoptions(inp->in6p_moptions);
43455679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
43555679Sshin	if (inp->inp_options)
43655679Sshin		(void)m_free(inp->inp_options);
437170613Sbms	if (inp->inp_moptions != NULL)
438170613Sbms		inp_freemoptions(inp->inp_moptions);
43953541Sshin	inp->inp_vflag = 0;
440174717Srwatson#ifdef MAC
441174717Srwatson	mac_inpcb_destroy(inp);
442174717Srwatson#endif
443160491Sups	INP_UNLOCK(inp);
44492767Sjeff	uma_zfree(ipi->ipi_zone, inp);
44553541Sshin}
44653541Sshin
447102218Struckmanstruct sockaddr *
448171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p)
449102218Struckman{
450102218Struckman	struct sockaddr_in6 *sin6;
451102218Struckman
452111119Simp	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
453102218Struckman	bzero(sin6, sizeof *sin6);
454102218Struckman	sin6->sin6_family = AF_INET6;
455102218Struckman	sin6->sin6_len = sizeof(*sin6);
456102218Struckman	sin6->sin6_port = port;
457102218Struckman	sin6->sin6_addr = *addr_p;
458148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
459102218Struckman
460102218Struckman	return (struct sockaddr *)sin6;
461102218Struckman}
462102218Struckman
463102218Struckmanstruct sockaddr *
464171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
465102218Struckman{
466102218Struckman	struct sockaddr_in sin;
467102218Struckman	struct sockaddr_in6 *sin6_p;
468102218Struckman
469102218Struckman	bzero(&sin, sizeof sin);
470102218Struckman	sin.sin_family = AF_INET;
471102218Struckman	sin.sin_len = sizeof(sin);
472102218Struckman	sin.sin_port = port;
473102218Struckman	sin.sin_addr = *addr_p;
474102218Struckman
475102218Struckman	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
476111119Simp		M_WAITOK);
477102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
478102218Struckman
479102218Struckman	return (struct sockaddr *)sin6_p;
480102218Struckman}
481102218Struckman
48253541Sshinint
483171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam)
48453541Sshin{
48553541Sshin	register struct inpcb *inp;
486102218Struckman	struct in6_addr addr;
487102218Struckman	in_port_t port;
48853541Sshin
48953541Sshin	inp = sotoinpcb(so);
490169462Srwatson	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
491157673Srwatson
492157767Srwatson	INP_LOCK(inp);
493102218Struckman	port = inp->inp_lport;
494102218Struckman	addr = inp->in6p_laddr;
495157767Srwatson	INP_UNLOCK(inp);
49653541Sshin
497102218Struckman	*nam = in6_sockaddr(port, &addr);
49853541Sshin	return 0;
49953541Sshin}
50053541Sshin
50153541Sshinint
502171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam)
50353541Sshin{
50453541Sshin	struct inpcb *inp;
505102218Struckman	struct in6_addr addr;
506102218Struckman	in_port_t port;
50753541Sshin
50853541Sshin	inp = sotoinpcb(so);
509169462Srwatson	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
510157673Srwatson
511157767Srwatson	INP_LOCK(inp);
512102218Struckman	port = inp->inp_fport;
513102218Struckman	addr = inp->in6p_faddr;
514157767Srwatson	INP_UNLOCK(inp);
51553541Sshin
516102218Struckman	*nam = in6_sockaddr(port, &addr);
51753541Sshin	return 0;
51853541Sshin}
51953541Sshin
52053541Sshinint
52153541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
52253541Sshin{
523157673Srwatson	struct	inpcb *inp;
52453541Sshin	int	error;
52553541Sshin
526157673Srwatson	inp = sotoinpcb(so);
527157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
528157673Srwatson
529124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
530169462Srwatson		error = in_getsockaddr(so, nam);
53154952Seivind		if (error == 0)
53253541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
533120913Sume	} else {
534169462Srwatson		/* scope issues will be handled in in6_getsockaddr(). */
535169462Srwatson		error = in6_getsockaddr(so, nam);
536120913Sume	}
53753541Sshin
53853541Sshin	return error;
53953541Sshin}
54053541Sshin
54153541Sshinint
54253541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
54353541Sshin{
544157673Srwatson	struct	inpcb *inp;
54553541Sshin	int	error;
54653541Sshin
547157673Srwatson	inp = sotoinpcb(so);
548157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
549157673Srwatson
550124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
551169462Srwatson		error = in_getpeeraddr(so, nam);
55254952Seivind		if (error == 0)
55353541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
55453541Sshin	} else
555169462Srwatson	/* scope issues will be handled in in6_getpeeraddr(). */
556169462Srwatson	error = in6_getpeeraddr(so, nam);
55753541Sshin
55853541Sshin	return error;
55953541Sshin}
56053541Sshin
56153541Sshin/*
56253541Sshin * Pass some notification to all connections of a protocol
56353541Sshin * associated with address dst.  The local address and/or port numbers
56453541Sshin * may be specified to limit the search.  The "usual action" will be
56553541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
56653541Sshin * cmds that are uninteresting (e.g., no error in the map).
56753541Sshin * Call the protocol specific routine (if any) to report
56853541Sshin * any errors for each matching socket.
56953541Sshin */
57053541Sshinvoid
571171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
572171259Sdelphij    u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
573171259Sdelphij    int cmd, void *cmdarg,
574175162Sobrien    struct inpcb *(*notify)(struct inpcb *, int))
57553541Sshin{
576133192Srwatson	struct inpcbhead *head;
57762587Sitojun	struct inpcb *inp, *ninp;
57878064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
57953541Sshin	u_short	fport = fport_arg, lport = lport_arg;
58078064Sume	u_int32_t flowinfo;
581157673Srwatson	int errno;
58253541Sshin
583119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
58453541Sshin		return;
58578064Sume
58678064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
58778064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
58853541Sshin		return;
58953541Sshin
59053541Sshin	/*
59178064Sume	 * note that src can be NULL when we get notify by local fragmentation.
59278064Sume	 */
59391346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
59478064Sume	flowinfo = sa6_src.sin6_flowinfo;
59578064Sume
59678064Sume	/*
59753541Sshin	 * Redirects go to all references to the destination,
59862587Sitojun	 * and use in6_rtchange to invalidate the route cache.
59962587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
60062587Sitojun	 * the cache, and deliver the error to all the sockets.
60153541Sshin	 * Otherwise, if we have knowledge of the local port and address,
60253541Sshin	 * deliver only to that socket.
60353541Sshin	 */
60453541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
60553541Sshin		fport = 0;
60653541Sshin		lport = 0;
60778064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
60862587Sitojun
60978064Sume		if (cmd != PRC_HOSTDEAD)
61078064Sume			notify = in6_rtchange;
61153541Sshin	}
61253541Sshin	errno = inet6ctlerrmap[cmd];
613169154Srwatson	head = pcbinfo->ipi_listhead;
614133192Srwatson	INP_INFO_WLOCK(pcbinfo);
615171260Sdelphij	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
616133192Srwatson		INP_LOCK(inp);
617171260Sdelphij		ninp = LIST_NEXT(inp, inp_list);
61862587Sitojun
619171260Sdelphij		if ((inp->inp_vflag & INP_IPV6) == 0) {
620133192Srwatson			INP_UNLOCK(inp);
62153541Sshin			continue;
622133192Srwatson		}
62362587Sitojun
62478064Sume		/*
625125776Sume		 * If the error designates a new path MTU for a destination
626125776Sume		 * and the application (associated with this socket) wanted to
627125776Sume		 * know the value, notify. Note that we notify for all
628125776Sume		 * disconnected sockets if the corresponding application
629125776Sume		 * wanted. This is because some UDP applications keep sending
630125776Sume		 * sockets disconnected.
631125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
632125776Sume		 */
633125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
634125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
635125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
636125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
637125776Sume					(u_int32_t *)cmdarg);
638125776Sume		}
639125776Sume
640125776Sume		/*
64178064Sume		 * Detect if we should notify the error. If no source and
64278064Sume		 * destination ports are specifed, but non-zero flowinfo and
64378064Sume		 * local address match, notify the error. This is the case
64478064Sume		 * when the error is delivered with an encrypted buffer
64578064Sume		 * by ESP. Otherwise, just compare addresses and ports
64678064Sume		 * as usual.
64778064Sume		 */
64878064Sume		if (lport == 0 && fport == 0 && flowinfo &&
64978064Sume		    inp->inp_socket != NULL &&
65078064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
65178064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
65278064Sume			goto do_notify;
65378064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
65478064Sume					     &sa6_dst->sin6_addr) ||
65578064Sume			 inp->inp_socket == 0 ||
65678064Sume			 (lport && inp->inp_lport != lport) ||
65778064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
65878064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
65978064Sume					      &sa6_src.sin6_addr)) ||
660133192Srwatson			 (fport && inp->inp_fport != fport)) {
661133192Srwatson			INP_UNLOCK(inp);
66253541Sshin			continue;
663133192Srwatson		}
66462587Sitojun
66578064Sume	  do_notify:
666134121Srwatson		if (notify) {
667134121Srwatson			if ((*notify)(inp, errno))
668134121Srwatson				INP_UNLOCK(inp);
669134121Srwatson		} else
670134121Srwatson			INP_UNLOCK(inp);
67153541Sshin	}
672133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
67353541Sshin}
67453541Sshin
67553541Sshin/*
67653541Sshin * Lookup a PCB based on the local address and port.
67753541Sshin */
67853541Sshinstruct inpcb *
679171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
680171259Sdelphij    u_int lport_arg, int wild_okay)
68153541Sshin{
68253541Sshin	register struct inpcb *inp;
68353541Sshin	int matchwild = 3, wildcard;
68453541Sshin	u_short lport = lport_arg;
68553541Sshin
686158011Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
687158011Srwatson
68853541Sshin	if (!wild_okay) {
68953541Sshin		struct inpcbhead *head;
69053541Sshin		/*
69153541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
69253541Sshin		 * matches the local address and port we're looking for.
69353541Sshin		 */
694169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
695169154Srwatson		    0, pcbinfo->ipi_hashmask)];
69653541Sshin		LIST_FOREACH(inp, head, inp_hash) {
69754952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
69853541Sshin				continue;
69953541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
70053541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
70153541Sshin			    inp->inp_lport == lport) {
70253541Sshin				/*
70353541Sshin				 * Found.
70453541Sshin				 */
70553541Sshin				return (inp);
70653541Sshin			}
70753541Sshin		}
70853541Sshin		/*
70953541Sshin		 * Not found.
71053541Sshin		 */
71153541Sshin		return (NULL);
71253541Sshin	} else {
71353541Sshin		struct inpcbporthead *porthash;
71453541Sshin		struct inpcbport *phd;
71553541Sshin		struct inpcb *match = NULL;
71653541Sshin		/*
71753541Sshin		 * Best fit PCB lookup.
71853541Sshin		 *
71953541Sshin		 * First see if this local port is in use by looking on the
72053541Sshin		 * port hash list.
72153541Sshin		 */
722169154Srwatson		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
723169154Srwatson		    pcbinfo->ipi_porthashmask)];
72453541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
72553541Sshin			if (phd->phd_port == lport)
72653541Sshin				break;
72753541Sshin		}
72853541Sshin		if (phd != NULL) {
72953541Sshin			/*
73053541Sshin			 * Port is in use by one or more PCBs. Look for best
73153541Sshin			 * fit.
73253541Sshin			 */
73353541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
73453541Sshin				wildcard = 0;
73554952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
73653541Sshin					continue;
73753541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
73853541Sshin					wildcard++;
73953541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
74053541Sshin					&inp->in6p_laddr)) {
74153541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
74253541Sshin						wildcard++;
74353541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
74453541Sshin						&inp->in6p_laddr, laddr))
74553541Sshin						continue;
74653541Sshin				} else {
74753541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
74853541Sshin						wildcard++;
74953541Sshin				}
75053541Sshin				if (wildcard < matchwild) {
75153541Sshin					match = inp;
75253541Sshin					matchwild = wildcard;
75353541Sshin					if (matchwild == 0) {
75453541Sshin						break;
75553541Sshin					}
75653541Sshin				}
75753541Sshin			}
75853541Sshin		}
75953541Sshin		return (match);
76053541Sshin	}
76153541Sshin}
76253541Sshin
76381127Sumevoid
764171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
76581127Sume{
76681127Sume	struct in6pcb *in6p;
76781127Sume	struct ip6_moptions *im6o;
76881127Sume	struct in6_multi_mship *imm, *nimm;
76981127Sume
770157978Srwatson	INP_INFO_RLOCK(pcbinfo);
771169154Srwatson	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
772157978Srwatson		INP_LOCK(in6p);
77381127Sume		im6o = in6p->in6p_moptions;
77481127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
77581127Sume		    im6o) {
77681127Sume			/*
77781127Sume			 * Unselect the outgoing interface if it is being
77881127Sume			 * detached.
77981127Sume			 */
78081127Sume			if (im6o->im6o_multicast_ifp == ifp)
78181127Sume				im6o->im6o_multicast_ifp = NULL;
78281127Sume
78381127Sume			/*
78481127Sume			 * Drop multicast group membership if we joined
78581127Sume			 * through the interface being detached.
78681127Sume			 * XXX controversial - is it really legal for kernel
78781127Sume			 * to force this?
78881127Sume			 */
78981127Sume			for (imm = im6o->im6o_memberships.lh_first;
79081127Sume			     imm != NULL; imm = nimm) {
79181127Sume				nimm = imm->i6mm_chain.le_next;
79281127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
79381127Sume					LIST_REMOVE(imm, i6mm_chain);
79481127Sume					in6_delmulti(imm->i6mm_maddr);
795149849Sobrien					free(imm, M_IP6MADDR);
79681127Sume				}
79781127Sume			}
79881127Sume		}
799157978Srwatson		INP_UNLOCK(in6p);
80081127Sume	}
801157978Srwatson	INP_INFO_RUNLOCK(pcbinfo);
80281127Sume}
80381127Sume
80453541Sshin/*
80553541Sshin * Check for alternatives when higher level complains
80653541Sshin * about service problems.  For now, invalidate cached
80753541Sshin * routing information.  If the route was created dynamically
80853541Sshin * (by a redirect), time to try a default gateway again.
80953541Sshin */
81053541Sshinvoid
811171259Sdelphijin6_losing(struct inpcb *in6p)
81253541Sshin{
813171259Sdelphij
814122922Sandre	/*
815122922Sandre	 * We don't store route pointers in the routing table anymore
816122922Sandre	 */
817122922Sandre	return;
81853541Sshin}
81953541Sshin
82053541Sshin/*
82153541Sshin * After a routing change, flush old routing
82253541Sshin * and allocate a (hopefully) better one.
82353541Sshin */
82498211Shsustruct inpcb *
825171259Sdelphijin6_rtchange(struct inpcb *inp, int errno)
82653541Sshin{
827122922Sandre	/*
828122922Sandre	 * We don't store route pointers in the routing table anymore
829122922Sandre	 */
83098211Shsu	return inp;
83153541Sshin}
83253541Sshin
83353541Sshin/*
83453541Sshin * Lookup PCB in hash list.
83553541Sshin */
83653541Sshinstruct inpcb *
837171259Sdelphijin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
838171259Sdelphij    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
839171259Sdelphij    int wildcard, struct ifnet *ifp)
84053541Sshin{
84153541Sshin	struct inpcbhead *head;
84253541Sshin	register struct inpcb *inp;
84353541Sshin	u_short fport = fport_arg, lport = lport_arg;
84478064Sume	int faith;
84553541Sshin
846158011Srwatson	INP_INFO_RLOCK_ASSERT(pcbinfo);
847158011Srwatson
84883934Sbrooks	if (faithprefix_p != NULL)
84983934Sbrooks		faith = (*faithprefix_p)(laddr);
85083934Sbrooks	else
85183934Sbrooks		faith = 0;
85278064Sume
85353541Sshin	/*
85453541Sshin	 * First look for an exact match.
85553541Sshin	 */
856169154Srwatson	head = &pcbinfo->ipi_hashbase[
857169154Srwatson	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
858169154Srwatson	    pcbinfo->ipi_hashmask)];
85954263Sshin	LIST_FOREACH(inp, head, inp_hash) {
86054952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
86153541Sshin			continue;
86253541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
86353541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
86453541Sshin		    inp->inp_fport == fport &&
86553541Sshin		    inp->inp_lport == lport) {
86653541Sshin			/*
86753541Sshin			 * Found.
86853541Sshin			 */
86953541Sshin			return (inp);
87053541Sshin		}
87153541Sshin	}
87253541Sshin	if (wildcard) {
87353541Sshin		struct inpcb *local_wild = NULL;
87453541Sshin
875169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
876169154Srwatson		    0, pcbinfo->ipi_hashmask)];
87754263Sshin		LIST_FOREACH(inp, head, inp_hash) {
87854952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
87953541Sshin				continue;
88053541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
88153541Sshin			    inp->inp_lport == lport) {
88278064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
88353541Sshin					continue;
88453541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
88553541Sshin						       laddr))
88653541Sshin					return (inp);
88753541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
88853541Sshin					local_wild = inp;
88953541Sshin			}
89053541Sshin		}
89153541Sshin		return (local_wild);
89253541Sshin	}
89353541Sshin
89453541Sshin	/*
89553541Sshin	 * Not found.
89653541Sshin	 */
89753541Sshin	return (NULL);
89853541Sshin}
89953541Sshin
90053541Sshinvoid
90153541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
90253541Sshin{
90353541Sshin	struct ip6_hdr *ip;
90453541Sshin
90553541Sshin	ip = mtod(m, struct ip6_hdr *);
90653541Sshin	bzero(sin6, sizeof(*sin6));
90753541Sshin	sin6->sin6_len = sizeof(*sin6);
90853541Sshin	sin6->sin6_family = AF_INET6;
90953541Sshin	sin6->sin6_addr = ip->ip6_src;
91053541Sshin
911148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
912148385Sume
91353541Sshin	return;
91453541Sshin}
915