in6_src.c revision 1.59
1/*	$OpenBSD: in6_src.c,v 1.59 2015/09/11 13:53:04 mpi Exp $	*/
2/*	$KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 *	The Regents of the University of California.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 *    may be used to endorse or promote products derived from this software
47 *    without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
62 */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/mbuf.h>
67#include <sys/protosw.h>
68#include <sys/socket.h>
69#include <sys/socketvar.h>
70#include <sys/ioctl.h>
71#include <sys/errno.h>
72#include <sys/time.h>
73
74#include <net/if.h>
75#include <net/if_var.h>
76#include <net/route.h>
77
78#include <netinet/in.h>
79#include <netinet/ip.h>
80#include <netinet/in_pcb.h>
81#include <netinet6/in6_var.h>
82#include <netinet/ip6.h>
83#include <netinet6/ip6_var.h>
84#include <netinet6/nd6.h>
85
86int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
87    struct ip6_moptions *, struct route_in6 *, struct ifnet **, u_int);
88
89/*
90 * Return an IPv6 address, which is the most appropriate for a given
91 * destination and user specified options.
92 * If necessary, this function lookups the routing table and returns
93 * an entry to the caller for later use.
94 */
95int
96in6_selectsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock,
97    struct ip6_pktopts *opts, struct ip6_moptions *mopts,
98    struct route_in6 *ro, struct in6_addr *laddr, u_int rtableid)
99{
100	struct ifnet *ifp = NULL;
101	struct in6_addr *dst;
102	struct in6_ifaddr *ia6 = NULL;
103	struct in6_pktinfo *pi = NULL;
104	int	error;
105
106	dst = &dstsock->sin6_addr;
107
108	/*
109	 * If the source address is explicitly specified by the caller,
110	 * check if the requested source address is indeed a unicast address
111	 * assigned to the node, and can be used as the packet's source
112	 * address.  If everything is okay, use the address as source.
113	 */
114	if (opts && (pi = opts->ip6po_pktinfo) &&
115	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
116		struct sockaddr_in6 sa6;
117
118		/* get the outgoing interface */
119		error = in6_selectif(dstsock, opts, mopts, ro, &ifp, rtableid);
120		if (error)
121			return (error);
122
123		bzero(&sa6, sizeof(sa6));
124		sa6.sin6_family = AF_INET6;
125		sa6.sin6_len = sizeof(sa6);
126		sa6.sin6_addr = pi->ipi6_addr;
127
128		if (ifp && IN6_IS_SCOPE_EMBED(&sa6.sin6_addr))
129			sa6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
130
131		ia6 = ifatoia6(ifa_ifwithaddr(sin6tosa(&sa6), rtableid));
132		if (ia6 == NULL ||
133		    (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)))
134			return (EADDRNOTAVAIL);
135
136		pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */
137
138		*in6src = &pi->ipi6_addr;
139		return (0);
140	}
141
142	/*
143	 * If the source address is not specified but the socket(if any)
144	 * is already bound, use the bound address.
145	 */
146	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
147		*in6src = laddr;
148		return (0);
149	}
150
151	/*
152	 * If the caller doesn't specify the source address but
153	 * the outgoing interface, use an address associated with
154	 * the interface.
155	 */
156	if (pi && pi->ipi6_ifindex) {
157		ifp = if_get(pi->ipi6_ifindex);
158		if (ifp == NULL)
159			return (ENXIO); /* XXX: better error? */
160
161		ia6 = in6_ifawithscope(ifp, dst, rtableid);
162		if (ia6 == NULL)
163			return (EADDRNOTAVAIL);
164
165		*in6src = &ia6->ia_addr.sin6_addr;
166		return (0);
167	}
168
169	/*
170	 * If the destination address is a link-local unicast address or
171	 * a link/interface-local multicast address, and if the outgoing
172	 * interface is specified by the sin6_scope_id filed, use an address
173	 * associated with the interface.
174	 * XXX: We're now trying to define more specific semantics of
175	 *      sin6_scope_id field, so this part will be rewritten in
176	 *      the near future.
177	 */
178	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) ||
179	     IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) {
180		ifp = if_get(dstsock->sin6_scope_id);
181		if (ifp == NULL)
182			return (ENXIO); /* XXX: better error? */
183
184		ia6 = in6_ifawithscope(ifp, dst, rtableid);
185		if (ia6 == NULL)
186			return (EADDRNOTAVAIL);
187
188		*in6src = &ia6->ia_addr.sin6_addr;
189		return (0);
190	}
191
192	/*
193	 * If the destination address is a multicast address and
194	 * the outgoing interface for the address is specified
195	 * by the caller, use an address associated with the interface.
196	 * Even if the outgoing interface is not specified, we also
197	 * choose a loopback interface as the outgoing interface.
198	 */
199	if (IN6_IS_ADDR_MULTICAST(dst)) {
200		ifp = mopts ? if_get(mopts->im6o_ifidx) : NULL;
201
202		if (!ifp && dstsock->sin6_scope_id)
203			ifp = if_get(htons(dstsock->sin6_scope_id));
204
205		if (ifp) {
206			ia6 = in6_ifawithscope(ifp, dst, rtableid);
207			if (ia6 == NULL)
208				return (EADDRNOTAVAIL);
209
210			*in6src = &ia6->ia_addr.sin6_addr;
211			return (0);
212		}
213	}
214
215	/*
216	 * If the next hop address for the packet is specified
217	 * by caller, use an address associated with the route
218	 * to the next hop.
219	 */
220	{
221		struct sockaddr_in6 *sin6_next;
222		struct rtentry *rt;
223
224		if (opts && opts->ip6po_nexthop) {
225			sin6_next = satosin6(opts->ip6po_nexthop);
226			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL,
227			    rtableid);
228			if (rt) {
229				ia6 = in6_ifawithscope(rt->rt_ifp, dst,
230				    rtableid);
231				if (ia6 == NULL)
232					ia6 = ifatoia6(rt->rt_ifa);
233			}
234			if (ia6 == NULL)
235				return (EADDRNOTAVAIL);
236
237			*in6src = &ia6->ia_addr.sin6_addr;
238			return (0);
239		}
240	}
241
242	/*
243	 * If route is known or can be allocated now,
244	 * our src addr is taken from the i/f, else punt.
245	 */
246	if (ro) {
247		if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
248		    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) {
249			rtfree(ro->ro_rt);
250			ro->ro_rt = NULL;
251		}
252		if (ro->ro_rt == (struct rtentry *)0 ||
253		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
254			struct sockaddr_in6 *sa6;
255
256			/* No route yet, so try to acquire one */
257			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
258			ro->ro_tableid = rtableid;
259			sa6 = &ro->ro_dst;
260			sa6->sin6_family = AF_INET6;
261			sa6->sin6_len = sizeof(struct sockaddr_in6);
262			sa6->sin6_addr = *dst;
263			sa6->sin6_scope_id = dstsock->sin6_scope_id;
264			if (IN6_IS_ADDR_MULTICAST(dst)) {
265				ro->ro_rt = rtalloc(sin6tosa(&ro->ro_dst),
266				    RT_REPORT|RT_RESOLVE, ro->ro_tableid);
267			} else {
268				ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
269				    NULL, ro->ro_tableid);
270			}
271		}
272
273		/*
274		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
275		 * the address. But we don't know why it does so.
276		 * It is necessary to ensure the scope even for lo0
277		 * so doesn't check out IFF_LOOPBACK.
278		 */
279
280		if (ro->ro_rt) {
281			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst,
282			    rtableid);
283			if (ia6 == NULL) /* xxx scope error ?*/
284				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
285		}
286		if (ia6 == NULL)
287			return (EHOSTUNREACH);	/* no route */
288
289		*in6src = &ia6->ia_addr.sin6_addr;
290		return (0);
291	}
292
293	return (EADDRNOTAVAIL);
294}
295
296int
297in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
298    struct route_in6 *ro, struct ifnet **retifp,
299    struct rtentry **retrt, unsigned int rtableid)
300{
301	int error = 0;
302	struct ifnet *ifp = NULL;
303	struct rtentry *rt = NULL;
304	struct sockaddr_in6 *sin6_next;
305	struct in6_addr *dst;
306
307	dst = &dstsock->sin6_addr;
308
309	/*
310	 * If the next hop address for the packet is specified by the caller,
311	 * use it as the gateway.
312	 */
313	if (opts && opts->ip6po_nexthop) {
314		struct route_in6 *ron;
315
316		sin6_next = satosin6(opts->ip6po_nexthop);
317
318		/* at this moment, we only support AF_INET6 next hops */
319		if (sin6_next->sin6_family != AF_INET6) {
320			error = EAFNOSUPPORT; /* or should we proceed? */
321			goto done;
322		}
323
324		/*
325		 * If the next hop is an IPv6 address, then the node identified
326		 * by that address must be a neighbor of the sending host.
327		 */
328		ron = &opts->ip6po_nextroute;
329		if ((ron->ro_rt &&
330		    (ron->ro_rt->rt_flags & (RTF_UP | RTF_GATEWAY)) !=
331		    RTF_UP) ||
332		    !IN6_ARE_ADDR_EQUAL(&ron->ro_dst.sin6_addr,
333		    &sin6_next->sin6_addr)) {
334			if (ron->ro_rt) {
335				rtfree(ron->ro_rt);
336				ron->ro_rt = NULL;
337			}
338			ron->ro_dst = *sin6_next;
339			ron->ro_tableid = rtableid;
340		}
341		if (ron->ro_rt == NULL) {
342			/* multi path case? */
343			ron->ro_rt = rtalloc(sin6tosa(&ron->ro_dst),
344			    RT_REPORT|RT_RESOLVE, ron->ro_tableid);
345			if (ron->ro_rt == NULL ||
346			    (ron->ro_rt->rt_flags & RTF_GATEWAY)) {
347				if (ron->ro_rt) {
348					rtfree(ron->ro_rt);
349					ron->ro_rt = NULL;
350				}
351				error = EHOSTUNREACH;
352				goto done;
353			}
354		}
355		if (!nd6_is_addr_neighbor(sin6_next, ron->ro_rt->rt_ifp)) {
356			rtfree(ron->ro_rt);
357			ron->ro_rt = NULL;
358			error = EHOSTUNREACH;
359			goto done;
360		}
361		rt = ron->ro_rt;
362		ifp = rt->rt_ifp;
363
364		/*
365		 * When cloning is required, try to allocate a route to the
366		 * destination so that the caller can store path MTU
367		 * information.
368		 */
369		goto done;
370	}
371
372	/*
373	 * Use a cached route if it exists and is valid, else try to allocate
374	 * a new one.  Note that we should check the address family of the
375	 * cached destination, in case of sharing the cache with IPv4.
376	 */
377	if (ro) {
378		if (ro->ro_rt &&
379		    (!(ro->ro_rt->rt_flags & RTF_UP) ||
380		     sin6tosa(&ro->ro_dst)->sa_family != AF_INET6 ||
381		     !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) {
382			rtfree(ro->ro_rt);
383			ro->ro_rt = NULL;
384		}
385		if (ro->ro_rt == NULL) {
386			struct sockaddr_in6 *sa6;
387
388			/* No route yet, so try to acquire one */
389			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
390			ro->ro_tableid = rtableid;
391			sa6 = &ro->ro_dst;
392			*sa6 = *dstsock;
393			sa6->sin6_scope_id = 0;
394			ro->ro_tableid = rtableid;
395			ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
396			    NULL, ro->ro_tableid);
397		}
398
399		/*
400		 * do not care about the result if we have the nexthop
401		 * explicitly specified.
402		 */
403		if (opts && opts->ip6po_nexthop)
404			goto done;
405
406		if (ro->ro_rt) {
407			ifp = ro->ro_rt->rt_ifp;
408
409			if (ifp == NULL) { /* can this really happen? */
410				rtfree(ro->ro_rt);
411				ro->ro_rt = NULL;
412			}
413		}
414		if (ro->ro_rt == NULL)
415			error = EHOSTUNREACH;
416		rt = ro->ro_rt;
417
418		/*
419		 * Check if the outgoing interface conflicts with
420		 * the interface specified by ipi6_ifindex (if specified).
421		 * Note that loopback interface is always okay.
422		 * (this may happen when we are sending a packet to one of
423		 *  our own addresses.)
424		 */
425		if (opts && opts->ip6po_pktinfo &&
426		    opts->ip6po_pktinfo->ipi6_ifindex) {
427			if (!(ifp->if_flags & IFF_LOOPBACK) &&
428			    ifp->if_index !=
429			    opts->ip6po_pktinfo->ipi6_ifindex) {
430				error = EHOSTUNREACH;
431				goto done;
432			}
433		}
434	}
435
436  done:
437	if (ifp == NULL && rt == NULL) {
438		/*
439		 * This can happen if the caller did not pass a cached route
440		 * nor any other hints.  We treat this case an error.
441		 */
442		error = EHOSTUNREACH;
443	}
444
445	if (retifp != NULL)
446		*retifp = ifp;
447	if (retrt != NULL)
448		*retrt = rt;	/* rt may be NULL */
449
450	return (error);
451}
452
453int
454in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
455    struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp,
456    u_int rtableid)
457{
458	struct rtentry *rt = NULL;
459	struct in6_pktinfo *pi = NULL;
460	int error;
461
462	/* If the caller specify the outgoing interface explicitly, use it. */
463	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
464		*retifp = if_get(pi->ipi6_ifindex);
465		if (*retifp != NULL)
466			return (0);
467	}
468
469	/*
470	 * If the destination address is a multicast address and the outgoing
471	 * interface for the address is specified by the caller, use it.
472	 */
473	if (IN6_IS_ADDR_MULTICAST(&dstsock->sin6_addr) &&
474	    mopts != NULL && (*retifp = if_get(mopts->im6o_ifidx)) != NULL)
475	    	return (0);
476
477	if ((error = in6_selectroute(dstsock, opts, ro, retifp,
478	    &rt, rtableid)) != 0)
479		return (error);
480
481	/*
482	 * do not use a rejected or black hole route.
483	 * XXX: this check should be done in the L2 output routine.
484	 * However, if we skipped this check here, we'd see the following
485	 * scenario:
486	 * - install a rejected route for a scoped address prefix
487	 *   (like fe80::/10)
488	 * - send a packet to a destination that matches the scoped prefix,
489	 *   with ambiguity about the scope zone.
490	 * - pick the outgoing interface from the route, and disambiguate the
491	 *   scope zone with the interface.
492	 * - ip6_output() would try to get another route with the "new"
493	 *   destination, which may be valid.
494	 * - we'd see no error on output.
495	 * Although this may not be very harmful, it should still be confusing.
496	 * We thus reject the case here.
497	 */
498	if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE)))
499		return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
500
501	/*
502	 * Adjust the "outgoing" interface.  If we're going to loop the packet
503	 * back to ourselves, the ifp would be the loopback interface.
504	 * However, we'd rather know the interface associated to the
505	 * destination address (which should probably be one of our own
506	 * addresses.)
507	 */
508	if (rt && rt->rt_ifa && rt->rt_ifa->ifa_ifp)
509		*retifp = rt->rt_ifa->ifa_ifp;
510
511	return (0);
512}
513
514/*
515 * Default hop limit selection. The precedence is as follows:
516 * 1. Hoplimit value specified via ioctl.
517 * 2. (If the outgoing interface is detected) the current
518 *     hop limit of the interface specified by router advertisement.
519 * 3. The system default hoplimit.
520*/
521int
522in6_selecthlim(struct inpcb *in6p, struct ifnet *ifp)
523{
524	if (in6p && in6p->inp_hops >= 0)
525		return (in6p->inp_hops);
526	else if (ifp)
527		return (ND_IFINFO(ifp)->chlim);
528	else
529		return (ip6_defhlim);
530}
531
532/*
533 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
534 * If the address scope of is link-local, embed the interface index in the
535 * address.  The routine determines our precedence
536 * between advanced API scope/interface specification and basic API
537 * specification.
538 *
539 * this function should be nuked in the future, when we get rid of
540 * embedded scopeid thing.
541 *
542 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
543 * there can be multiple interfaces that belong to a particular scope zone
544 * (in specification, we have 1:N mapping between a scope zone and interfaces).
545 * we may want to change the function to return something other than ifp.
546 */
547int
548in6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6,
549    struct inpcb *in6p)
550{
551	struct ifnet *ifp = NULL;
552	u_int32_t scopeid;
553
554	*in6 = sin6->sin6_addr;
555	scopeid = sin6->sin6_scope_id;
556
557	/*
558	 * don't try to read sin6->sin6_addr beyond here, since the caller may
559	 * ask us to overwrite existing sockaddr_in6
560	 */
561
562	if (IN6_IS_SCOPE_EMBED(in6)) {
563		struct in6_pktinfo *pi;
564
565		/*
566		 * KAME assumption: link id == interface id
567		 */
568
569		if (in6p && in6p->inp_outputopts6 &&
570		    (pi = in6p->inp_outputopts6->ip6po_pktinfo) &&
571		    pi->ipi6_ifindex) {
572			ifp = if_get(pi->ipi6_ifindex);
573			if (ifp == NULL)
574				return ENXIO;  /* XXX EINVAL? */
575			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
576		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
577		    in6p->inp_moptions6 &&
578		    (ifp = if_get(in6p->inp_moptions6->im6o_ifidx))) {
579			in6->s6_addr16[1] = htons(ifp->if_index);
580		} else if (scopeid) {
581			ifp = if_get(scopeid);
582			if (ifp == NULL)
583				return ENXIO;  /* XXX EINVAL? */
584			/*XXX assignment to 16bit from 32bit variable */
585			in6->s6_addr16[1] = htons(scopeid & 0xffff);
586		}
587		if_put(ifp);
588	}
589
590	return 0;
591}
592
593/*
594 * generate standard sockaddr_in6 from embedded form.
595 * touches sin6_addr and sin6_scope_id only.
596 *
597 * this function should be nuked in the future, when we get rid of
598 * embedded scopeid thing.
599 */
600void
601in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6)
602{
603	u_int32_t scopeid;
604
605	sin6->sin6_addr = *in6;
606
607	/*
608	 * don't try to read *in6 beyond here, since the caller may
609	 * ask us to overwrite existing sockaddr_in6
610	 */
611
612	sin6->sin6_scope_id = 0;
613	if (IN6_IS_SCOPE_EMBED(in6)) {
614		/*
615		 * KAME assumption: link id == interface id
616		 */
617		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
618		if (scopeid) {
619			sin6->sin6_addr.s6_addr16[1] = 0;
620			sin6->sin6_scope_id = scopeid;
621		}
622	}
623}
624
625/*
626 * just clear the embedded scope identifer.
627 */
628void
629in6_clearscope(struct in6_addr *addr)
630{
631	if (IN6_IS_SCOPE_EMBED(addr))
632		addr->s6_addr16[1] = 0;
633}
634