in6_src.c revision 1.55
1/*	$OpenBSD: in6_src.c,v 1.55 2015/09/03 14:59:23 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);
88int selectroute(struct sockaddr_in6 *, struct ip6_pktopts *,
89    struct ip6_moptions *, struct route_in6 *, struct ifnet **,
90    struct rtentry **, int, u_int);
91
92/*
93 * Return an IPv6 address, which is the most appropriate for a given
94 * destination and user specified options.
95 * If necessary, this function lookups the routing table and returns
96 * an entry to the caller for later use.
97 */
98int
99in6_selectsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock,
100    struct ip6_pktopts *opts, struct ip6_moptions *mopts,
101    struct route_in6 *ro, struct in6_addr *laddr, u_int rtableid)
102{
103	struct ifnet *ifp = NULL;
104	struct in6_addr *dst;
105	struct in6_ifaddr *ia6 = NULL;
106	struct in6_pktinfo *pi = NULL;
107	int	error;
108
109	dst = &dstsock->sin6_addr;
110
111	/*
112	 * If the source address is explicitly specified by the caller,
113	 * check if the requested source address is indeed a unicast address
114	 * assigned to the node, and can be used as the packet's source
115	 * address.  If everything is okay, use the address as source.
116	 */
117	if (opts && (pi = opts->ip6po_pktinfo) &&
118	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
119		struct sockaddr_in6 sa6;
120
121		/* get the outgoing interface */
122		error = in6_selectif(dstsock, opts, mopts, ro, &ifp, rtableid);
123		if (error)
124			return (error);
125
126		bzero(&sa6, sizeof(sa6));
127		sa6.sin6_family = AF_INET6;
128		sa6.sin6_len = sizeof(sa6);
129		sa6.sin6_addr = pi->ipi6_addr;
130
131		if (ifp && IN6_IS_SCOPE_EMBED(&sa6.sin6_addr))
132			sa6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
133
134		ia6 = ifatoia6(ifa_ifwithaddr(sin6tosa(&sa6), rtableid));
135		if (ia6 == NULL ||
136		    (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)))
137			return (EADDRNOTAVAIL);
138
139		pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */
140
141		*in6src = &pi->ipi6_addr;
142		return (0);
143	}
144
145	/*
146	 * If the source address is not specified but the socket(if any)
147	 * is already bound, use the bound address.
148	 */
149	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
150		*in6src = laddr;
151		return (0);
152	}
153
154	/*
155	 * If the caller doesn't specify the source address but
156	 * the outgoing interface, use an address associated with
157	 * the interface.
158	 */
159	if (pi && pi->ipi6_ifindex) {
160		ifp = if_get(pi->ipi6_ifindex);
161		if (ifp == NULL)
162			return (ENXIO); /* XXX: better error? */
163
164		ia6 = in6_ifawithscope(ifp, dst, rtableid);
165		if (ia6 == NULL)
166			return (EADDRNOTAVAIL);
167
168		*in6src = &ia6->ia_addr.sin6_addr;
169		return (0);
170	}
171
172	/*
173	 * If the destination address is a link-local unicast address or
174	 * a link/interface-local multicast address, and if the outgoing
175	 * interface is specified by the sin6_scope_id filed, use an address
176	 * associated with the interface.
177	 * XXX: We're now trying to define more specific semantics of
178	 *      sin6_scope_id field, so this part will be rewritten in
179	 *      the near future.
180	 */
181	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) ||
182	     IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) {
183		ifp = if_get(dstsock->sin6_scope_id);
184		if (ifp == NULL)
185			return (ENXIO); /* XXX: better error? */
186
187		ia6 = in6_ifawithscope(ifp, dst, rtableid);
188		if (ia6 == NULL)
189			return (EADDRNOTAVAIL);
190
191		*in6src = &ia6->ia_addr.sin6_addr;
192		return (0);
193	}
194
195	/*
196	 * If the destination address is a multicast address and
197	 * the outgoing interface for the address is specified
198	 * by the caller, use an address associated with the interface.
199	 * Even if the outgoing interface is not specified, we also
200	 * choose a loopback interface as the outgoing interface.
201	 */
202	if (IN6_IS_ADDR_MULTICAST(dst)) {
203		ifp = mopts ? if_get(mopts->im6o_ifidx) : NULL;
204
205		if (!ifp && dstsock->sin6_scope_id)
206			ifp = if_get(htons(dstsock->sin6_scope_id));
207
208		if (ifp) {
209			ia6 = in6_ifawithscope(ifp, dst, rtableid);
210			if (ia6 == NULL)
211				return (EADDRNOTAVAIL);
212
213			*in6src = &ia6->ia_addr.sin6_addr;
214			return (0);
215		}
216	}
217
218	/*
219	 * If the next hop address for the packet is specified
220	 * by caller, use an address associated with the route
221	 * to the next hop.
222	 */
223	{
224		struct sockaddr_in6 *sin6_next;
225		struct rtentry *rt;
226
227		if (opts && opts->ip6po_nexthop) {
228			sin6_next = satosin6(opts->ip6po_nexthop);
229			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL,
230			    rtableid);
231			if (rt) {
232				ia6 = in6_ifawithscope(rt->rt_ifp, dst,
233				    rtableid);
234				if (ia6 == NULL)
235					ia6 = ifatoia6(rt->rt_ifa);
236			}
237			if (ia6 == NULL)
238				return (EADDRNOTAVAIL);
239
240			*in6src = &ia6->ia_addr.sin6_addr;
241			return (0);
242		}
243	}
244
245	/*
246	 * If route is known or can be allocated now,
247	 * our src addr is taken from the i/f, else punt.
248	 */
249	if (ro) {
250		if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
251		    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) {
252			rtfree(ro->ro_rt);
253			ro->ro_rt = NULL;
254		}
255		if (ro->ro_rt == (struct rtentry *)0 ||
256		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
257			struct sockaddr_in6 *sa6;
258
259			/* No route yet, so try to acquire one */
260			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
261			ro->ro_tableid = rtableid;
262			sa6 = &ro->ro_dst;
263			sa6->sin6_family = AF_INET6;
264			sa6->sin6_len = sizeof(struct sockaddr_in6);
265			sa6->sin6_addr = *dst;
266			sa6->sin6_scope_id = dstsock->sin6_scope_id;
267			if (IN6_IS_ADDR_MULTICAST(dst)) {
268				ro->ro_rt = rtalloc(sin6tosa(&ro->ro_dst),
269				    RT_REPORT|RT_RESOLVE, ro->ro_tableid);
270			} else {
271				ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
272				    NULL, ro->ro_tableid);
273			}
274		}
275
276		/*
277		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
278		 * the address. But we don't know why it does so.
279		 * It is necessary to ensure the scope even for lo0
280		 * so doesn't check out IFF_LOOPBACK.
281		 */
282
283		if (ro->ro_rt) {
284			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst,
285			    rtableid);
286			if (ia6 == NULL) /* xxx scope error ?*/
287				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
288		}
289		if (ia6 == NULL)
290			return (EHOSTUNREACH);	/* no route */
291
292		*in6src = &ia6->ia_addr.sin6_addr;
293		return (0);
294	}
295
296	return (EADDRNOTAVAIL);
297}
298
299int
300selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
301    struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp,
302    struct rtentry **retrt, int norouteok, u_int rtableid)
303{
304	int error = 0;
305	struct ifnet *ifp = NULL;
306	struct rtentry *rt = NULL;
307	struct sockaddr_in6 *sin6_next;
308	struct in6_pktinfo *pi = NULL;
309	struct in6_addr *dst;
310
311	dst = &dstsock->sin6_addr;
312
313#if 0
314	char ip[INET6_ADDRSTRLEN];
315
316	if (dstsock->sin6_addr.s6_addr32[0] == 0 &&
317	    dstsock->sin6_addr.s6_addr32[1] == 0 &&
318	    !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) {
319		printf("in6_selectroute: strange destination %s\n",
320		    inet_ntop(AF_INET6, &dstsock->sin6_addr, ip, sizeof(ip)));
321	} else {
322		printf("in6_selectroute: destination = %s%%%d\n",
323		    inet_ntop(AF_INET6, &dstsock->sin6_addr, ip, sizeof(ip)),
324		    dstsock->sin6_scope_id); /* for debug */
325	}
326#endif
327
328	/* If the caller specify the outgoing interface explicitly, use it. */
329	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
330		ifp = if_get(pi->ipi6_ifindex);
331		if (ifp != NULL &&
332		    (norouteok || retrt == NULL ||
333		     IN6_IS_ADDR_MULTICAST(dst))) {
334			/*
335			 * we do not have to check or get the route for
336			 * multicast.
337			 */
338			goto done;
339		} else
340			goto getroute;
341	}
342
343	/*
344	 * If the destination address is a multicast address and the outgoing
345	 * interface for the address is specified by the caller, use it.
346	 */
347	if (IN6_IS_ADDR_MULTICAST(dst) &&
348	    mopts != NULL && (ifp = if_get(mopts->im6o_ifidx)) != NULL) {
349		goto done; /* we do not need a route for multicast. */
350	}
351
352  getroute:
353	/*
354	 * If the next hop address for the packet is specified by the caller,
355	 * use it as the gateway.
356	 */
357	if (opts && opts->ip6po_nexthop) {
358		struct route_in6 *ron;
359
360		sin6_next = satosin6(opts->ip6po_nexthop);
361
362		/* at this moment, we only support AF_INET6 next hops */
363		if (sin6_next->sin6_family != AF_INET6) {
364			error = EAFNOSUPPORT; /* or should we proceed? */
365			goto done;
366		}
367
368		/*
369		 * If the next hop is an IPv6 address, then the node identified
370		 * by that address must be a neighbor of the sending host.
371		 */
372		ron = &opts->ip6po_nextroute;
373		if ((ron->ro_rt &&
374		    (ron->ro_rt->rt_flags & (RTF_UP | RTF_GATEWAY)) !=
375		    RTF_UP) ||
376		    !IN6_ARE_ADDR_EQUAL(&ron->ro_dst.sin6_addr,
377		    &sin6_next->sin6_addr)) {
378			if (ron->ro_rt) {
379				rtfree(ron->ro_rt);
380				ron->ro_rt = NULL;
381			}
382			ron->ro_dst = *sin6_next;
383			ron->ro_tableid = rtableid;
384		}
385		if (ron->ro_rt == NULL) {
386			/* multi path case? */
387			ron->ro_rt = rtalloc(sin6tosa(&ron->ro_dst),
388			    RT_REPORT|RT_RESOLVE, ron->ro_tableid);
389			if (ron->ro_rt == NULL ||
390			    (ron->ro_rt->rt_flags & RTF_GATEWAY)) {
391				if (ron->ro_rt) {
392					rtfree(ron->ro_rt);
393					ron->ro_rt = NULL;
394				}
395				error = EHOSTUNREACH;
396				goto done;
397			}
398		}
399		if (!nd6_is_addr_neighbor(sin6_next, ron->ro_rt->rt_ifp)) {
400			rtfree(ron->ro_rt);
401			ron->ro_rt = NULL;
402			error = EHOSTUNREACH;
403			goto done;
404		}
405		rt = ron->ro_rt;
406		ifp = rt->rt_ifp;
407
408		/*
409		 * When cloning is required, try to allocate a route to the
410		 * destination so that the caller can store path MTU
411		 * information.
412		 */
413		goto done;
414	}
415
416	/*
417	 * Use a cached route if it exists and is valid, else try to allocate
418	 * a new one.  Note that we should check the address family of the
419	 * cached destination, in case of sharing the cache with IPv4.
420	 */
421	if (ro) {
422		if (ro->ro_rt &&
423		    (!(ro->ro_rt->rt_flags & RTF_UP) ||
424		     sin6tosa(&ro->ro_dst)->sa_family != AF_INET6 ||
425		     !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) {
426			rtfree(ro->ro_rt);
427			ro->ro_rt = NULL;
428		}
429		if (ro->ro_rt == NULL) {
430			struct sockaddr_in6 *sa6;
431
432			/* No route yet, so try to acquire one */
433			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
434			ro->ro_tableid = rtableid;
435			sa6 = &ro->ro_dst;
436			*sa6 = *dstsock;
437			sa6->sin6_scope_id = 0;
438			ro->ro_tableid = rtableid;
439			ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
440			    NULL, ro->ro_tableid);
441		}
442
443		/*
444		 * do not care about the result if we have the nexthop
445		 * explicitly specified.
446		 */
447		if (opts && opts->ip6po_nexthop)
448			goto done;
449
450		if (ro->ro_rt) {
451			ifp = ro->ro_rt->rt_ifp;
452
453			if (ifp == NULL) { /* can this really happen? */
454				rtfree(ro->ro_rt);
455				ro->ro_rt = NULL;
456			}
457		}
458		if (ro->ro_rt == NULL)
459			error = EHOSTUNREACH;
460		rt = ro->ro_rt;
461
462		/*
463		 * Check if the outgoing interface conflicts with
464		 * the interface specified by ipi6_ifindex (if specified).
465		 * Note that loopback interface is always okay.
466		 * (this may happen when we are sending a packet to one of
467		 *  our own addresses.)
468		 */
469		if (opts && opts->ip6po_pktinfo &&
470		    opts->ip6po_pktinfo->ipi6_ifindex) {
471			if (!(ifp->if_flags & IFF_LOOPBACK) &&
472			    ifp->if_index !=
473			    opts->ip6po_pktinfo->ipi6_ifindex) {
474				error = EHOSTUNREACH;
475				goto done;
476			}
477		}
478	}
479
480  done:
481	if (ifp == NULL && rt == NULL) {
482		/*
483		 * This can happen if the caller did not pass a cached route
484		 * nor any other hints.  We treat this case an error.
485		 */
486		error = EHOSTUNREACH;
487	}
488	if (error == EHOSTUNREACH)
489		ip6stat.ip6s_noroute++;
490
491	if (retifp != NULL)
492		*retifp = ifp;
493	if (retrt != NULL)
494		*retrt = rt;	/* rt may be NULL */
495
496	return (error);
497}
498
499int
500in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
501    struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp,
502    u_int rtableid)
503{
504	struct rtentry *rt = NULL;
505	int error;
506
507	if ((error = selectroute(dstsock, opts, mopts, ro, retifp,
508	    &rt, 1, rtableid)) != 0)
509		return (error);
510
511	/*
512	 * do not use a rejected or black hole route.
513	 * XXX: this check should be done in the L2 output routine.
514	 * However, if we skipped this check here, we'd see the following
515	 * scenario:
516	 * - install a rejected route for a scoped address prefix
517	 *   (like fe80::/10)
518	 * - send a packet to a destination that matches the scoped prefix,
519	 *   with ambiguity about the scope zone.
520	 * - pick the outgoing interface from the route, and disambiguate the
521	 *   scope zone with the interface.
522	 * - ip6_output() would try to get another route with the "new"
523	 *   destination, which may be valid.
524	 * - we'd see no error on output.
525	 * Although this may not be very harmful, it should still be confusing.
526	 * We thus reject the case here.
527	 */
528	if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE)))
529		return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
530
531	/*
532	 * Adjust the "outgoing" interface.  If we're going to loop the packet
533	 * back to ourselves, the ifp would be the loopback interface.
534	 * However, we'd rather know the interface associated to the
535	 * destination address (which should probably be one of our own
536	 * addresses.)
537	 */
538	if (rt && rt->rt_ifa && rt->rt_ifa->ifa_ifp)
539		*retifp = rt->rt_ifa->ifa_ifp;
540
541	return (0);
542}
543
544int
545in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
546    struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp,
547    struct rtentry **retrt, u_int rtableid)
548{
549
550	return (selectroute(dstsock, opts, mopts, ro, retifp, retrt, 0,
551	    rtableid));
552}
553
554/*
555 * Default hop limit selection. The precedence is as follows:
556 * 1. Hoplimit value specified via ioctl.
557 * 2. (If the outgoing interface is detected) the current
558 *     hop limit of the interface specified by router advertisement.
559 * 3. The system default hoplimit.
560*/
561int
562in6_selecthlim(struct inpcb *in6p, struct ifnet *ifp)
563{
564	if (in6p && in6p->inp_hops >= 0)
565		return (in6p->inp_hops);
566	else if (ifp)
567		return (ND_IFINFO(ifp)->chlim);
568	else
569		return (ip6_defhlim);
570}
571
572/*
573 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
574 * If the address scope of is link-local, embed the interface index in the
575 * address.  The routine determines our precedence
576 * between advanced API scope/interface specification and basic API
577 * specification.
578 *
579 * this function should be nuked in the future, when we get rid of
580 * embedded scopeid thing.
581 *
582 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
583 * there can be multiple interfaces that belong to a particular scope zone
584 * (in specification, we have 1:N mapping between a scope zone and interfaces).
585 * we may want to change the function to return something other than ifp.
586 */
587int
588in6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6,
589    struct inpcb *in6p, struct ifnet **ifpp)
590{
591	struct ifnet *ifp = NULL;
592	u_int32_t scopeid;
593
594	*in6 = sin6->sin6_addr;
595	scopeid = sin6->sin6_scope_id;
596	if (ifpp)
597		*ifpp = NULL;
598
599	/*
600	 * don't try to read sin6->sin6_addr beyond here, since the caller may
601	 * ask us to overwrite existing sockaddr_in6
602	 */
603
604	if (IN6_IS_SCOPE_EMBED(in6)) {
605		struct in6_pktinfo *pi;
606
607		/*
608		 * KAME assumption: link id == interface id
609		 */
610
611		if (in6p && in6p->inp_outputopts6 &&
612		    (pi = in6p->inp_outputopts6->ip6po_pktinfo) &&
613		    pi->ipi6_ifindex) {
614			ifp = if_get(pi->ipi6_ifindex);
615			if (ifp == NULL)
616				return ENXIO;  /* XXX EINVAL? */
617			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
618		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
619			   in6p->inp_moptions6 &&
620			   (ifp = if_get(in6p->inp_moptions6->im6o_ifidx))) {
621			in6->s6_addr16[1] = htons(ifp->if_index);
622		} else if (scopeid) {
623			ifp = if_get(scopeid);
624			if (ifp == NULL)
625				return ENXIO;  /* XXX EINVAL? */
626			/*XXX assignment to 16bit from 32bit variable */
627			in6->s6_addr16[1] = htons(scopeid & 0xffff);
628		}
629
630		if (ifpp)
631			*ifpp = ifp;
632	}
633
634	return 0;
635}
636
637/*
638 * generate standard sockaddr_in6 from embedded form.
639 * touches sin6_addr and sin6_scope_id only.
640 *
641 * this function should be nuked in the future, when we get rid of
642 * embedded scopeid thing.
643 */
644int
645in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6,
646    struct ifnet *ifp)
647{
648	u_int32_t scopeid;
649
650	sin6->sin6_addr = *in6;
651
652	/*
653	 * don't try to read *in6 beyond here, since the caller may
654	 * ask us to overwrite existing sockaddr_in6
655	 */
656
657	sin6->sin6_scope_id = 0;
658	if (IN6_IS_SCOPE_EMBED(in6)) {
659		/*
660		 * KAME assumption: link id == interface id
661		 */
662		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
663		if (scopeid) {
664			/* sanity check */
665			if (if_get(scopeid) == NULL)
666				return ENXIO;
667			if (ifp && ifp->if_index != scopeid)
668				return ENXIO;
669			sin6->sin6_addr.s6_addr16[1] = 0;
670			sin6->sin6_scope_id = scopeid;
671		}
672	}
673
674	return 0;
675}
676
677/*
678 * just clear the embedded scope identifer.
679 */
680void
681in6_clearscope(struct in6_addr *addr)
682{
683	if (IN6_IS_SCOPE_EMBED(addr))
684		addr->s6_addr16[1] = 0;
685}
686