in6_src.c revision 1.15
1/*	$OpenBSD: in6_src.c,v 1.15 2003/06/02 23:28:15 millert 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/*
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 */
95struct in6_addr *
96in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
97	struct sockaddr_in6 *dstsock;
98	struct ip6_pktopts *opts;
99	struct ip6_moptions *mopts;
100	struct route_in6 *ro;
101	struct in6_addr *laddr;
102	int *errorp;
103{
104	struct in6_addr *dst;
105	struct in6_ifaddr *ia6 = 0;
106	struct in6_pktinfo *pi = NULL;
107
108	dst = &dstsock->sin6_addr;
109	*errorp = 0;
110
111	/*
112	 * If the source address is explicitly specified by the caller,
113	 * use it.
114	 */
115	if (opts && (pi = opts->ip6po_pktinfo) &&
116	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
117		return (&pi->ipi6_addr);
118
119	/*
120	 * If the source address is not specified but the socket(if any)
121	 * is already bound, use the bound address.
122	 */
123	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
124		return (laddr);
125
126	/*
127	 * If the caller doesn't specify the source address but
128	 * the outgoing interface, use an address associated with
129	 * the interface.
130	 */
131	if (pi && pi->ipi6_ifindex) {
132		/* XXX boundary check is assumed to be already done. */
133		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
134				       dst);
135		if (ia6 == 0) {
136			*errorp = EADDRNOTAVAIL;
137			return (0);
138		}
139		return (&satosin6(&ia6->ia_addr)->sin6_addr);
140	}
141
142	/*
143	 * If the destination address is a link-local unicast address or
144	 * a multicast address, and if the outgoing interface is specified
145	 * by the sin6_scope_id filed, use an address associated with the
146	 * interface.
147	 * XXX: We're now trying to define more specific semantics of
148	 *      sin6_scope_id field, so this part will be rewritten in
149	 *      the near future.
150	 */
151	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
152	    dstsock->sin6_scope_id) {
153		/*
154		 * I'm not sure if boundary check for scope_id is done
155		 * somewhere...
156		 */
157		if (dstsock->sin6_scope_id < 0 ||
158		    if_index < dstsock->sin6_scope_id) {
159			*errorp = ENXIO; /* XXX: better error? */
160			return (0);
161		}
162		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
163				       dst);
164		if (ia6 == 0) {
165			*errorp = EADDRNOTAVAIL;
166			return (0);
167		}
168		return (&satosin6(&ia6->ia_addr)->sin6_addr);
169	}
170
171	/*
172	 * If the destination address is a multicast address and
173	 * the outgoing interface for the address is specified
174	 * by the caller, use an address associated with the interface.
175	 * There is a sanity check here; if the destination has node-local
176	 * scope, the outgoing interfacde should be a loopback address.
177	 * Even if the outgoing interface is not specified, we also
178	 * choose a loopback interface as the outgoing interface.
179	 */
180	if (IN6_IS_ADDR_MULTICAST(dst)) {
181		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
182
183		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
184			ifp = lo0ifp;
185		}
186
187		if (ifp) {
188			ia6 = in6_ifawithscope(ifp, dst);
189			if (ia6 == 0) {
190				*errorp = EADDRNOTAVAIL;
191				return (0);
192			}
193			return (&satosin6(&ia6->ia_addr)->sin6_addr);
194		}
195	}
196
197	/*
198	 * If the next hop address for the packet is specified
199	 * by caller, use an address associated with the route
200	 * to the next hop.
201	 */
202	{
203		struct sockaddr_in6 *sin6_next;
204		struct rtentry *rt;
205
206		if (opts && opts->ip6po_nexthop) {
207			sin6_next = satosin6(opts->ip6po_nexthop);
208			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
209			if (rt) {
210				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
211				if (ia6 == 0)
212					ia6 = ifatoia6(rt->rt_ifa);
213			}
214			if (ia6 == 0) {
215				*errorp = EADDRNOTAVAIL;
216				return (0);
217			}
218			return (&satosin6(&ia6->ia_addr)->sin6_addr);
219		}
220	}
221
222	/*
223	 * If route is known or can be allocated now,
224	 * our src addr is taken from the i/f, else punt.
225	 */
226	if (ro) {
227		if (ro->ro_rt &&
228		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
229			RTFREE(ro->ro_rt);
230			ro->ro_rt = (struct rtentry *)0;
231		}
232		if (ro->ro_rt == (struct rtentry *)0 ||
233		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
234			struct sockaddr_in6 *sa6;
235
236			/* No route yet, so try to acquire one */
237			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
238			sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
239			sa6->sin6_family = AF_INET6;
240			sa6->sin6_len = sizeof(struct sockaddr_in6);
241			sa6->sin6_addr = *dst;
242			sa6->sin6_scope_id = dstsock->sin6_scope_id;
243			if (IN6_IS_ADDR_MULTICAST(dst)) {
244				ro->ro_rt = rtalloc1(&((struct route *)ro)
245						     ->ro_dst, 0);
246			} else {
247				rtalloc((struct route *)ro);
248			}
249		}
250
251		/*
252		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
253		 * the address. But we don't know why it does so.
254		 * It is necessary to ensure the scope even for lo0
255		 * so doesn't check out IFF_LOOPBACK.
256		 */
257
258		if (ro->ro_rt) {
259			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
260			if (ia6 == 0) /* xxx scope error ?*/
261				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
262		}
263#if 0
264		/*
265		 * xxx The followings are necessary? (kazu)
266		 * I don't think so.
267		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
268		 */
269		if (ia6 == 0) {
270			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
271
272			sin6->sin6_addr = *dst;
273
274			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
275			if (ia6 == 0)
276				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
277			if (ia6 == 0)
278				return (0);
279			return (&satosin6(&ia6->ia_addr)->sin6_addr);
280		}
281#endif /* 0 */
282		if (ia6 == 0) {
283			*errorp = EHOSTUNREACH;	/* no route */
284			return (0);
285		}
286		return (&satosin6(&ia6->ia_addr)->sin6_addr);
287	}
288
289	*errorp = EADDRNOTAVAIL;
290	return (0);
291}
292
293/*
294 * Default hop limit selection. The precedence is as follows:
295 * 1. Hoplimit value specified via ioctl.
296 * 2. (If the outgoing interface is detected) the current
297 *     hop limit of the interface specified by router advertisement.
298 * 3. The system default hoplimit.
299*/
300#define in6pcb		inpcb
301#define in6p_hops	inp_hops
302int
303in6_selecthlim(in6p, ifp)
304	struct in6pcb *in6p;
305	struct ifnet *ifp;
306{
307	if (in6p && in6p->in6p_hops >= 0)
308		return (in6p->in6p_hops);
309	else if (ifp)
310		return (ND_IFINFO(ifp)->chlim);
311	else
312		return (ip6_defhlim);
313}
314#undef in6pcb
315#undef in6p_hops
316
317/*
318 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
319 * If the address scope of is link-local, embed the interface index in the
320 * address.  The routine determines our precedence
321 * between advanced API scope/interface specification and basic API
322 * specification.
323 *
324 * this function should be nuked in the future, when we get rid of
325 * embedded scopeid thing.
326 *
327 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
328 * there can be multiple interfaces that belong to a particular scope zone
329 * (in specification, we have 1:N mapping between a scope zone and interfaces).
330 * we may want to change the function to return something other than ifp.
331 */
332int
333in6_embedscope(in6, sin6, in6p, ifpp)
334	struct in6_addr *in6;
335	const struct sockaddr_in6 *sin6;
336	struct inpcb *in6p;
337#define in6p_outputopts	inp_outputopts6
338#define in6p_moptions	inp_moptions6
339	struct ifnet **ifpp;
340{
341	struct ifnet *ifp = NULL;
342	u_int32_t scopeid;
343
344	*in6 = sin6->sin6_addr;
345	scopeid = sin6->sin6_scope_id;
346	if (ifpp)
347		*ifpp = NULL;
348
349	/*
350	 * don't try to read sin6->sin6_addr beyond here, since the caller may
351	 * ask us to overwrite existing sockaddr_in6
352	 */
353
354	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
355		struct in6_pktinfo *pi;
356
357		/*
358		 * KAME assumption: link id == interface id
359		 */
360
361		if (in6p && in6p->in6p_outputopts &&
362		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
363		    pi->ipi6_ifindex) {
364			ifp = ifindex2ifnet[pi->ipi6_ifindex];
365			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
366		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
367			   in6p->in6p_moptions &&
368			   in6p->in6p_moptions->im6o_multicast_ifp) {
369			ifp = in6p->in6p_moptions->im6o_multicast_ifp;
370			in6->s6_addr16[1] = htons(ifp->if_index);
371		} else if (scopeid) {
372			/* boundary check */
373			if (scopeid < 0 || if_index < scopeid)
374				return ENXIO;  /* XXX EINVAL? */
375			ifp = ifindex2ifnet[scopeid];
376			/*XXX assignment to 16bit from 32bit variable */
377			in6->s6_addr16[1] = htons(scopeid & 0xffff);
378		}
379
380		if (ifpp)
381			*ifpp = ifp;
382	}
383
384	return 0;
385}
386#undef in6p_outputopts
387#undef in6p_moptions
388
389/*
390 * generate standard sockaddr_in6 from embedded form.
391 * touches sin6_addr and sin6_scope_id only.
392 *
393 * this function should be nuked in the future, when we get rid of
394 * embedded scopeid thing.
395 */
396int
397in6_recoverscope(sin6, in6, ifp)
398	struct sockaddr_in6 *sin6;
399	const struct in6_addr *in6;
400	struct ifnet *ifp;
401{
402	u_int32_t scopeid;
403
404	sin6->sin6_addr = *in6;
405
406	/*
407	 * don't try to read *in6 beyond here, since the caller may
408	 * ask us to overwrite existing sockaddr_in6
409	 */
410
411	sin6->sin6_scope_id = 0;
412	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
413		/*
414		 * KAME assumption: link id == interface id
415		 */
416		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
417		if (scopeid) {
418			/* sanity check */
419			if (scopeid < 0 || if_index < scopeid)
420				return ENXIO;
421			if (ifp && ifp->if_index != scopeid)
422				return ENXIO;
423			sin6->sin6_addr.s6_addr16[1] = 0;
424			sin6->sin6_scope_id = scopeid;
425		}
426	}
427
428	return 0;
429}
430
431/*
432 * just clear the embedded scope identifer.
433 */
434void
435in6_clearscope(addr)
436	struct in6_addr *addr;
437{
438	if (IN6_IS_SCOPE_LINKLOCAL(addr))
439		addr->s6_addr16[1] = 0;
440}
441