in6_src.c revision 1.80
1/*	$OpenBSD: in6_src.c,v 1.80 2016/09/02 13:53:44 vgross 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 pcb. We need the additional opt parameter because
92 * the values set at pcb level can be overriden via cmsg.
93 */
94int
95in6_pcbselsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock,
96    struct inpcb *inp, struct ip6_pktopts *opts)
97{
98	struct ip6_moptions *mopts = inp->inp_moptions6;
99	struct route_in6 *ro = &inp->inp_route6;
100	struct in6_addr *laddr = &inp->inp_laddr6;
101	u_int rtableid = inp->inp_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		if_put(ifp); /* put reference from in6_selectif */
134
135		ia6 = ifatoia6(ifa_ifwithaddr(sin6tosa(&sa6), rtableid));
136		if (ia6 == NULL || (ia6->ia6_flags &
137		     (IN6_IFF_ANYCAST|IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED)))
138			return (EADDRNOTAVAIL);
139
140		pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */
141
142		*in6src = &pi->ipi6_addr;
143		return (0);
144	}
145
146	/*
147	 * If the source address is not specified but the socket(if any)
148	 * is already bound, use the bound address.
149	 */
150	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
151		*in6src = laddr;
152		return (0);
153	}
154
155	/*
156	 * If the caller doesn't specify the source address but
157	 * the outgoing interface, use an address associated with
158	 * the interface.
159	 */
160	if (pi && pi->ipi6_ifindex) {
161		ifp = if_get(pi->ipi6_ifindex);
162		if (ifp == NULL)
163			return (ENXIO); /* XXX: better error? */
164
165		ia6 = in6_ifawithscope(ifp, dst, rtableid);
166		if_put(ifp);
167
168		if (ia6 == NULL)
169			return (EADDRNOTAVAIL);
170
171		*in6src = &ia6->ia_addr.sin6_addr;
172		return (0);
173	}
174
175	return in6_selectsrc(in6src, dstsock, mopts, ro, rtableid);
176}
177
178/*
179 * Return an IPv6 address, which is the most appropriate for a given
180 * destination and multicast options.
181 * If necessary, this function lookups the routing table and returns
182 * an entry to the caller for later use.
183 */
184int
185in6_selectsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock,
186    struct ip6_moptions *mopts, struct route_in6 *ro, u_int rtableid)
187{
188	struct ifnet *ifp = NULL;
189	struct in6_addr *dst;
190	struct in6_ifaddr *ia6 = NULL;
191
192	dst = &dstsock->sin6_addr;
193
194	/*
195	 * If the destination address is a link-local unicast address or
196	 * a link/interface-local multicast address, and if the outgoing
197	 * interface is specified by the sin6_scope_id filed, use an address
198	 * associated with the interface.
199	 * XXX: We're now trying to define more specific semantics of
200	 *      sin6_scope_id field, so this part will be rewritten in
201	 *      the near future.
202	 */
203	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) ||
204	     IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) {
205		ifp = if_get(dstsock->sin6_scope_id);
206		if (ifp == NULL)
207			return (ENXIO); /* XXX: better error? */
208
209		ia6 = in6_ifawithscope(ifp, dst, rtableid);
210		if_put(ifp);
211
212		if (ia6 == NULL)
213			return (EADDRNOTAVAIL);
214
215		*in6src = &ia6->ia_addr.sin6_addr;
216		return (0);
217	}
218
219	/*
220	 * If the destination address is a multicast address and
221	 * the outgoing interface for the address is specified
222	 * by the caller, use an address associated with the interface.
223	 * Even if the outgoing interface is not specified, we also
224	 * choose a loopback interface as the outgoing interface.
225	 */
226	if (IN6_IS_ADDR_MULTICAST(dst)) {
227		ifp = mopts ? if_get(mopts->im6o_ifidx) : NULL;
228
229		if (!ifp && dstsock->sin6_scope_id)
230			ifp = if_get(htons(dstsock->sin6_scope_id));
231
232		if (ifp) {
233			ia6 = in6_ifawithscope(ifp, dst, rtableid);
234			if_put(ifp);
235
236			if (ia6 == NULL)
237				return (EADDRNOTAVAIL);
238
239			*in6src = &ia6->ia_addr.sin6_addr;
240			return (0);
241		}
242	}
243
244	/*
245	 * If route is known or can be allocated now,
246	 * our src addr is taken from the i/f, else punt.
247	 */
248	if (ro) {
249		if (!rtisvalid(ro->ro_rt) || (ro->ro_tableid != rtableid) ||
250		    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst)) {
251			rtfree(ro->ro_rt);
252			ro->ro_rt = NULL;
253		}
254		if (ro->ro_rt == NULL) {
255			struct sockaddr_in6 *sa6;
256
257			/* No route yet, so try to acquire one */
258			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
259			ro->ro_tableid = rtableid;
260			sa6 = &ro->ro_dst;
261			sa6->sin6_family = AF_INET6;
262			sa6->sin6_len = sizeof(struct sockaddr_in6);
263			sa6->sin6_addr = *dst;
264			sa6->sin6_scope_id = dstsock->sin6_scope_id;
265			ro->ro_rt = rtalloc(sin6tosa(&ro->ro_dst),
266			    RT_RESOLVE, ro->ro_tableid);
267		}
268
269		/*
270		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
271		 * the address. But we don't know why it does so.
272		 * It is necessary to ensure the scope even for lo0
273		 * so doesn't check out IFF_LOOPBACK.
274		 */
275
276		if (ro->ro_rt) {
277			ifp = if_get(ro->ro_rt->rt_ifidx);
278			if (ifp != NULL) {
279				ia6 = in6_ifawithscope(ifp, dst, rtableid);
280				if_put(ifp);
281			}
282			if (ia6 == NULL) /* xxx scope error ?*/
283				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
284		}
285		if (ia6 == NULL)
286			return (EHOSTUNREACH);	/* no route */
287
288		*in6src = &ia6->ia_addr.sin6_addr;
289		return (0);
290	}
291
292	return (EADDRNOTAVAIL);
293}
294
295struct rtentry *
296in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
297    struct route_in6 *ro, unsigned int rtableid)
298{
299	struct in6_addr *dst;
300
301	dst = &dstsock->sin6_addr;
302
303	/*
304	 * Use a cached route if it exists and is valid, else try to allocate
305	 * a new one.
306	 */
307	if (ro) {
308		if (rtisvalid(ro->ro_rt))
309			KASSERT(sin6tosa(&ro->ro_dst)->sa_family == AF_INET6);
310		if (!rtisvalid(ro->ro_rt) ||
311		    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst)) {
312			rtfree(ro->ro_rt);
313			ro->ro_rt = NULL;
314		}
315		if (ro->ro_rt == NULL) {
316			struct sockaddr_in6 *sa6;
317
318			/* No route yet, so try to acquire one */
319			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
320			ro->ro_tableid = rtableid;
321			sa6 = &ro->ro_dst;
322			*sa6 = *dstsock;
323			sa6->sin6_scope_id = 0;
324			ro->ro_tableid = rtableid;
325			ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
326			    NULL, ro->ro_tableid);
327		}
328
329		/*
330		 * Check if the outgoing interface conflicts with
331		 * the interface specified by ipi6_ifindex (if specified).
332		 * Note that loopback interface is always okay.
333		 * (this may happen when we are sending a packet to one of
334		 *  our own addresses.)
335		 */
336		if (opts && opts->ip6po_pktinfo &&
337		    opts->ip6po_pktinfo->ipi6_ifindex) {
338			if (ro->ro_rt != NULL &&
339			    !ISSET(ro->ro_rt->rt_flags, RTF_LOCAL) &&
340			    ro->ro_rt->rt_ifidx !=
341			    opts->ip6po_pktinfo->ipi6_ifindex) {
342			    	return (NULL);
343			}
344		}
345
346		return (ro->ro_rt);
347	}
348
349	return (NULL);
350}
351
352int
353in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
354    struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp,
355    u_int rtableid)
356{
357	struct rtentry *rt = NULL;
358	struct in6_pktinfo *pi = NULL;
359
360	/* If the caller specify the outgoing interface explicitly, use it. */
361	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
362		*retifp = if_get(pi->ipi6_ifindex);
363		if (*retifp != NULL)
364			return (0);
365	}
366
367	/*
368	 * If the destination address is a multicast address and the outgoing
369	 * interface for the address is specified by the caller, use it.
370	 */
371	if (IN6_IS_ADDR_MULTICAST(&dstsock->sin6_addr) &&
372	    mopts != NULL && (*retifp = if_get(mopts->im6o_ifidx)) != NULL)
373	    	return (0);
374
375	rt = in6_selectroute(dstsock, opts, ro, rtableid);
376	if (rt == NULL)
377		return (EHOSTUNREACH);
378
379	/*
380	 * do not use a rejected or black hole route.
381	 * XXX: this check should be done in the L2 output routine.
382	 * However, if we skipped this check here, we'd see the following
383	 * scenario:
384	 * - install a rejected route for a scoped address prefix
385	 *   (like fe80::/10)
386	 * - send a packet to a destination that matches the scoped prefix,
387	 *   with ambiguity about the scope zone.
388	 * - pick the outgoing interface from the route, and disambiguate the
389	 *   scope zone with the interface.
390	 * - ip6_output() would try to get another route with the "new"
391	 *   destination, which may be valid.
392	 * - we'd see no error on output.
393	 * Although this may not be very harmful, it should still be confusing.
394	 * We thus reject the case here.
395	 */
396	if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE)))
397		return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
398
399	if (rt != NULL)
400		*retifp = if_get(rt->rt_ifidx);
401
402	return (0);
403}
404
405int
406in6_selecthlim(struct inpcb *in6p)
407{
408	if (in6p && in6p->inp_hops >= 0)
409		return (in6p->inp_hops);
410
411	return (ip6_defhlim);
412}
413
414/*
415 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
416 * If the address scope of is link-local, embed the interface index in the
417 * address.  The routine determines our precedence
418 * between advanced API scope/interface specification and basic API
419 * specification.
420 *
421 * this function should be nuked in the future, when we get rid of
422 * embedded scopeid thing.
423 *
424 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
425 * there can be multiple interfaces that belong to a particular scope zone
426 * (in specification, we have 1:N mapping between a scope zone and interfaces).
427 * we may want to change the function to return something other than ifp.
428 */
429int
430in6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6,
431    struct inpcb *in6p)
432{
433	struct ifnet *ifp = NULL;
434	u_int32_t scopeid;
435
436	*in6 = sin6->sin6_addr;
437	scopeid = sin6->sin6_scope_id;
438
439	/*
440	 * don't try to read sin6->sin6_addr beyond here, since the caller may
441	 * ask us to overwrite existing sockaddr_in6
442	 */
443
444	if (IN6_IS_SCOPE_EMBED(in6)) {
445		struct in6_pktinfo *pi;
446
447		/*
448		 * KAME assumption: link id == interface id
449		 */
450
451		if (in6p && in6p->inp_outputopts6 &&
452		    (pi = in6p->inp_outputopts6->ip6po_pktinfo) &&
453		    pi->ipi6_ifindex) {
454			ifp = if_get(pi->ipi6_ifindex);
455			if (ifp == NULL)
456				return ENXIO;  /* XXX EINVAL? */
457			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
458		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
459		    in6p->inp_moptions6 &&
460		    (ifp = if_get(in6p->inp_moptions6->im6o_ifidx))) {
461			in6->s6_addr16[1] = htons(ifp->if_index);
462		} else if (scopeid) {
463			ifp = if_get(scopeid);
464			if (ifp == NULL)
465				return ENXIO;  /* XXX EINVAL? */
466			/*XXX assignment to 16bit from 32bit variable */
467			in6->s6_addr16[1] = htons(scopeid & 0xffff);
468		}
469		if_put(ifp);
470	}
471
472	return 0;
473}
474
475/*
476 * generate standard sockaddr_in6 from embedded form.
477 * touches sin6_addr and sin6_scope_id only.
478 *
479 * this function should be nuked in the future, when we get rid of
480 * embedded scopeid thing.
481 */
482void
483in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6)
484{
485	u_int32_t scopeid;
486
487	sin6->sin6_addr = *in6;
488
489	/*
490	 * don't try to read *in6 beyond here, since the caller may
491	 * ask us to overwrite existing sockaddr_in6
492	 */
493
494	sin6->sin6_scope_id = 0;
495	if (IN6_IS_SCOPE_EMBED(in6)) {
496		/*
497		 * KAME assumption: link id == interface id
498		 */
499		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
500		if (scopeid) {
501			sin6->sin6_addr.s6_addr16[1] = 0;
502			sin6->sin6_scope_id = scopeid;
503		}
504	}
505}
506
507/*
508 * just clear the embedded scope identifer.
509 */
510void
511in6_clearscope(struct in6_addr *addr)
512{
513	if (IN6_IS_SCOPE_EMBED(addr))
514		addr->s6_addr16[1] = 0;
515}
516