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