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