in6_src.c revision 1.68
1/*	$OpenBSD: in6_src.c,v 1.68 2015/10/24 16:57:46 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		if_put(ifp); /* put reference from in6_selectif */
131
132		ia6 = ifatoia6(ifa_ifwithaddr(sin6tosa(&sa6), rtableid));
133		if (ia6 == NULL ||
134		    (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)))
135			return (EADDRNOTAVAIL);
136
137		pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */
138
139		*in6src = &pi->ipi6_addr;
140		return (0);
141	}
142
143	/*
144	 * If the source address is not specified but the socket(if any)
145	 * is already bound, use the bound address.
146	 */
147	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
148		*in6src = laddr;
149		return (0);
150	}
151
152	/*
153	 * If the caller doesn't specify the source address but
154	 * the outgoing interface, use an address associated with
155	 * the interface.
156	 */
157	if (pi && pi->ipi6_ifindex) {
158		ifp = if_get(pi->ipi6_ifindex);
159		if (ifp == NULL)
160			return (ENXIO); /* XXX: better error? */
161
162		ia6 = in6_ifawithscope(ifp, dst, rtableid);
163		if_put(ifp);
164
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_put(ifp);
189
190		if (ia6 == NULL)
191			return (EADDRNOTAVAIL);
192
193		*in6src = &ia6->ia_addr.sin6_addr;
194		return (0);
195	}
196
197	/*
198	 * If the destination address is a multicast address and
199	 * the outgoing interface for the address is specified
200	 * by the caller, use an address associated with the interface.
201	 * Even if the outgoing interface is not specified, we also
202	 * choose a loopback interface as the outgoing interface.
203	 */
204	if (IN6_IS_ADDR_MULTICAST(dst)) {
205		ifp = mopts ? if_get(mopts->im6o_ifidx) : NULL;
206
207		if (!ifp && dstsock->sin6_scope_id)
208			ifp = if_get(htons(dstsock->sin6_scope_id));
209
210		if (ifp) {
211			ia6 = in6_ifawithscope(ifp, dst, rtableid);
212			if_put(ifp);
213
214			if (ia6 == NULL)
215				return (EADDRNOTAVAIL);
216
217			*in6src = &ia6->ia_addr.sin6_addr;
218			return (0);
219		}
220	}
221
222	/*
223	 * If the next hop address for the packet is specified
224	 * by caller, use an address associated with the route
225	 * to the next hop.
226	 */
227	{
228		struct sockaddr_in6 *sin6_next;
229		struct rtentry *rt;
230
231		if (opts && opts->ip6po_nexthop) {
232			sin6_next = satosin6(opts->ip6po_nexthop);
233			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL,
234			    rtableid);
235			if (rt != NULL) {
236				ifp = if_get(rt->rt_ifidx);
237				if (ifp != NULL) {
238					ia6 = in6_ifawithscope(ifp, dst,
239					    rtableid);
240					if_put(ifp);
241				}
242				if (ia6 == NULL)
243					ia6 = ifatoia6(rt->rt_ifa);
244				rtfree(rt);
245			}
246			if (ia6 == NULL)
247				return (EADDRNOTAVAIL);
248
249			*in6src = &ia6->ia_addr.sin6_addr;
250			return (0);
251		}
252	}
253
254	/*
255	 * If route is known or can be allocated now,
256	 * our src addr is taken from the i/f, else punt.
257	 */
258	if (ro) {
259		if (!rtisvalid(ro->ro_rt) || (ro->ro_tableid != rtableid) ||
260		    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst)) {
261			rtfree(ro->ro_rt);
262			ro->ro_rt = NULL;
263		}
264		if (ro->ro_rt == NULL) {
265			struct sockaddr_in6 *sa6;
266
267			/* No route yet, so try to acquire one */
268			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
269			ro->ro_tableid = rtableid;
270			sa6 = &ro->ro_dst;
271			sa6->sin6_family = AF_INET6;
272			sa6->sin6_len = sizeof(struct sockaddr_in6);
273			sa6->sin6_addr = *dst;
274			sa6->sin6_scope_id = dstsock->sin6_scope_id;
275			if (IN6_IS_ADDR_MULTICAST(dst)) {
276				ro->ro_rt = rtalloc(sin6tosa(&ro->ro_dst),
277				    RT_REPORT|RT_RESOLVE, ro->ro_tableid);
278			} else {
279				ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
280				    NULL, ro->ro_tableid);
281			}
282		}
283
284		/*
285		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
286		 * the address. But we don't know why it does so.
287		 * It is necessary to ensure the scope even for lo0
288		 * so doesn't check out IFF_LOOPBACK.
289		 */
290
291		if (ro->ro_rt) {
292			ifp = if_get(ro->ro_rt->rt_ifidx);
293			if (ifp != NULL) {
294				ia6 = in6_ifawithscope(ifp, dst, rtableid);
295				if_put(ifp);
296			}
297			if (ia6 == NULL) /* xxx scope error ?*/
298				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
299		}
300		if (ia6 == NULL)
301			return (EHOSTUNREACH);	/* no route */
302
303		*in6src = &ia6->ia_addr.sin6_addr;
304		return (0);
305	}
306
307	return (EADDRNOTAVAIL);
308}
309
310struct rtentry *
311in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
312    struct route_in6 *ro, unsigned int rtableid)
313{
314	struct sockaddr_in6 *sin6_next;
315	struct in6_addr *dst;
316
317	dst = &dstsock->sin6_addr;
318
319	/*
320	 * If the next hop address for the packet is specified by the caller,
321	 * use it as the gateway.
322	 */
323	if (opts && opts->ip6po_nexthop) {
324		struct route_in6 *ron;
325
326		sin6_next = satosin6(opts->ip6po_nexthop);
327
328		/* We only support AF_INET6 next hops */
329		if (sin6_next->sin6_family != AF_INET6)
330			return (NULL);
331
332		/*
333		 * If the next hop is an IPv6 address, then the node identified
334		 * by that address must be a neighbor of the sending host.
335		 */
336		ron = &opts->ip6po_nextroute;
337		if (!rtisvalid(ron->ro_rt) ||
338		    ISSET(ron->ro_rt->rt_flags, RTF_GATEWAY) ||
339		    !IN6_ARE_ADDR_EQUAL(&ron->ro_dst.sin6_addr,
340		    &sin6_next->sin6_addr)) {
341			if (ron->ro_rt) {
342				rtfree(ron->ro_rt);
343				ron->ro_rt = NULL;
344			}
345			ron->ro_dst = *sin6_next;
346			ron->ro_tableid = rtableid;
347		}
348		if (ron->ro_rt == NULL) {
349			/* multi path case? */
350			ron->ro_rt = rtalloc(sin6tosa(&ron->ro_dst),
351			    RT_REPORT|RT_RESOLVE, ron->ro_tableid);
352			if (ron->ro_rt == NULL ||
353			    (ron->ro_rt->rt_flags & RTF_GATEWAY)) {
354				if (ron->ro_rt) {
355					rtfree(ron->ro_rt);
356					ron->ro_rt = NULL;
357				}
358				return (NULL);
359			}
360		}
361		if (!nd6_is_addr_neighbor(sin6_next, ron->ro_rt->rt_ifp)) {
362			rtfree(ron->ro_rt);
363			ron->ro_rt = NULL;
364			return (NULL);
365		}
366
367		return (ron->ro_rt);
368	}
369
370	/*
371	 * Use a cached route if it exists and is valid, else try to allocate
372	 * a new one.  Note that we should check the address family of the
373	 * cached destination, in case of sharing the cache with IPv4.
374	 */
375	if (ro) {
376		if (!rtisvalid(ro->ro_rt) ||
377		     sin6tosa(&ro->ro_dst)->sa_family != AF_INET6 ||
378		     !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst)) {
379			rtfree(ro->ro_rt);
380			ro->ro_rt = NULL;
381		}
382		if (ro->ro_rt == NULL) {
383			struct sockaddr_in6 *sa6;
384
385			/* No route yet, so try to acquire one */
386			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
387			ro->ro_tableid = rtableid;
388			sa6 = &ro->ro_dst;
389			*sa6 = *dstsock;
390			sa6->sin6_scope_id = 0;
391			ro->ro_tableid = rtableid;
392			ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
393			    NULL, ro->ro_tableid);
394		}
395
396		/*
397		 * Check if the outgoing interface conflicts with
398		 * the interface specified by ipi6_ifindex (if specified).
399		 * Note that loopback interface is always okay.
400		 * (this may happen when we are sending a packet to one of
401		 *  our own addresses.)
402		 */
403		if (opts && opts->ip6po_pktinfo &&
404		    opts->ip6po_pktinfo->ipi6_ifindex) {
405			if (ro->ro_rt != NULL &&
406			    (ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0 &&
407			    ro->ro_rt->rt_ifp->if_index !=
408			    opts->ip6po_pktinfo->ipi6_ifindex) {
409			    	return (NULL);
410			}
411		}
412
413		return (ro->ro_rt);
414	}
415
416	return (NULL);
417}
418
419int
420in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
421    struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp,
422    u_int rtableid)
423{
424	struct rtentry *rt = NULL;
425	struct in6_pktinfo *pi = NULL;
426
427	/* If the caller specify the outgoing interface explicitly, use it. */
428	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
429		*retifp = if_get(pi->ipi6_ifindex);
430		if (*retifp != NULL)
431			return (0);
432	}
433
434	/*
435	 * If the destination address is a multicast address and the outgoing
436	 * interface for the address is specified by the caller, use it.
437	 */
438	if (IN6_IS_ADDR_MULTICAST(&dstsock->sin6_addr) &&
439	    mopts != NULL && (*retifp = if_get(mopts->im6o_ifidx)) != NULL)
440	    	return (0);
441
442	rt = in6_selectroute(dstsock, opts, ro, rtableid);
443	if (rt == NULL)
444		return (EHOSTUNREACH);
445
446	/*
447	 * do not use a rejected or black hole route.
448	 * XXX: this check should be done in the L2 output routine.
449	 * However, if we skipped this check here, we'd see the following
450	 * scenario:
451	 * - install a rejected route for a scoped address prefix
452	 *   (like fe80::/10)
453	 * - send a packet to a destination that matches the scoped prefix,
454	 *   with ambiguity about the scope zone.
455	 * - pick the outgoing interface from the route, and disambiguate the
456	 *   scope zone with the interface.
457	 * - ip6_output() would try to get another route with the "new"
458	 *   destination, which may be valid.
459	 * - we'd see no error on output.
460	 * Although this may not be very harmful, it should still be confusing.
461	 * We thus reject the case here.
462	 */
463	if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE)))
464		return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
465
466	if (rt != NULL)
467		*retifp = if_get(rt->rt_ifidx);
468
469	return (0);
470}
471
472/*
473 * Default hop limit selection. The precedence is as follows:
474 * 1. Hoplimit value specified via ioctl.
475 * 2. (If the outgoing interface is detected) the current
476 *     hop limit of the interface specified by router advertisement.
477 * 3. The system default hoplimit.
478*/
479int
480in6_selecthlim(struct inpcb *in6p)
481{
482	if (in6p && in6p->inp_hops >= 0)
483		return (in6p->inp_hops);
484
485	return (ip6_defhlim);
486}
487
488/*
489 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
490 * If the address scope of is link-local, embed the interface index in the
491 * address.  The routine determines our precedence
492 * between advanced API scope/interface specification and basic API
493 * specification.
494 *
495 * this function should be nuked in the future, when we get rid of
496 * embedded scopeid thing.
497 *
498 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
499 * there can be multiple interfaces that belong to a particular scope zone
500 * (in specification, we have 1:N mapping between a scope zone and interfaces).
501 * we may want to change the function to return something other than ifp.
502 */
503int
504in6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6,
505    struct inpcb *in6p)
506{
507	struct ifnet *ifp = NULL;
508	u_int32_t scopeid;
509
510	*in6 = sin6->sin6_addr;
511	scopeid = sin6->sin6_scope_id;
512
513	/*
514	 * don't try to read sin6->sin6_addr beyond here, since the caller may
515	 * ask us to overwrite existing sockaddr_in6
516	 */
517
518	if (IN6_IS_SCOPE_EMBED(in6)) {
519		struct in6_pktinfo *pi;
520
521		/*
522		 * KAME assumption: link id == interface id
523		 */
524
525		if (in6p && in6p->inp_outputopts6 &&
526		    (pi = in6p->inp_outputopts6->ip6po_pktinfo) &&
527		    pi->ipi6_ifindex) {
528			ifp = if_get(pi->ipi6_ifindex);
529			if (ifp == NULL)
530				return ENXIO;  /* XXX EINVAL? */
531			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
532		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
533		    in6p->inp_moptions6 &&
534		    (ifp = if_get(in6p->inp_moptions6->im6o_ifidx))) {
535			in6->s6_addr16[1] = htons(ifp->if_index);
536		} else if (scopeid) {
537			ifp = if_get(scopeid);
538			if (ifp == NULL)
539				return ENXIO;  /* XXX EINVAL? */
540			/*XXX assignment to 16bit from 32bit variable */
541			in6->s6_addr16[1] = htons(scopeid & 0xffff);
542		}
543		if_put(ifp);
544	}
545
546	return 0;
547}
548
549/*
550 * generate standard sockaddr_in6 from embedded form.
551 * touches sin6_addr and sin6_scope_id only.
552 *
553 * this function should be nuked in the future, when we get rid of
554 * embedded scopeid thing.
555 */
556void
557in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6)
558{
559	u_int32_t scopeid;
560
561	sin6->sin6_addr = *in6;
562
563	/*
564	 * don't try to read *in6 beyond here, since the caller may
565	 * ask us to overwrite existing sockaddr_in6
566	 */
567
568	sin6->sin6_scope_id = 0;
569	if (IN6_IS_SCOPE_EMBED(in6)) {
570		/*
571		 * KAME assumption: link id == interface id
572		 */
573		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
574		if (scopeid) {
575			sin6->sin6_addr.s6_addr16[1] = 0;
576			sin6->sin6_scope_id = scopeid;
577		}
578	}
579}
580
581/*
582 * just clear the embedded scope identifer.
583 */
584void
585in6_clearscope(struct in6_addr *addr)
586{
587	if (IN6_IS_SCOPE_EMBED(addr))
588		addr->s6_addr16[1] = 0;
589}
590