in6_src.c revision 1.5
1/*	$OpenBSD: in6_src.c,v 1.5 2000/06/13 10:12:01 itojun Exp $	*/
2/*	$KAME: in6_src.c,v 1.23 2000/06/12 08:15:27 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. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *	This product includes software developed by the University of
48 *	California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 *    may be used to endorse or promote products derived from this software
51 *    without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
66 */
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/malloc.h>
71#include <sys/mbuf.h>
72#include <sys/protosw.h>
73#include <sys/socket.h>
74#include <sys/socketvar.h>
75#include <sys/ioctl.h>
76#include <sys/errno.h>
77#include <sys/time.h>
78#include <sys/proc.h>
79
80#include <net/if.h>
81#include <net/route.h>
82
83#include <netinet/in.h>
84#include <netinet/in_var.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/in_pcb.h>
88#include <netinet6/in6_var.h>
89#include <netinet/ip6.h>
90#include <netinet6/ip6_var.h>
91#include <netinet6/nd6.h>
92
93#include "loop.h"
94
95extern struct ifnet loif[NLOOP];
96
97/*
98 * Return an IPv6 address, which is the most appropriate for given
99 * destination and user specified options.
100 * If necessary, this function lookups the routing table and return
101 * an entry to the caller for later use.
102 */
103struct in6_addr *
104in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
105	struct sockaddr_in6 *dstsock;
106	struct ip6_pktopts *opts;
107	struct ip6_moptions *mopts;
108	struct route_in6 *ro;
109	struct in6_addr *laddr;
110	int *errorp;
111{
112	struct in6_addr *dst;
113	struct in6_ifaddr *ia6 = 0;
114	struct in6_pktinfo *pi = NULL;
115
116	dst = &dstsock->sin6_addr;
117	*errorp = 0;
118
119	/*
120	 * If the source address is explicitly specified by the caller,
121	 * use it.
122	 */
123	if (opts && (pi = opts->ip6po_pktinfo) &&
124	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
125		return(&pi->ipi6_addr);
126
127	/*
128	 * If the source address is not specified but the socket(if any)
129	 * is already bound, use the bound address.
130	 */
131	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
132		return(laddr);
133
134	/*
135	 * If the caller doesn't specify the source address but
136	 * the outgoing interface, use an address associated with
137	 * the interface.
138	 */
139	if (pi && pi->ipi6_ifindex) {
140		/* XXX boundary check is assumed to be already done. */
141		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
142				       dst);
143		if (ia6 == 0) {
144			*errorp = EADDRNOTAVAIL;
145			return(0);
146		}
147		return(&satosin6(&ia6->ia_addr)->sin6_addr);
148	}
149
150	/*
151	 * If the destination address is a link-local unicast address or
152	 * a multicast address, and if the outgoing interface is specified
153	 * by the sin6_scope_id filed, use an address associated with the
154	 * interface.
155	 * XXX: We're now trying to define more specific semantics of
156	 *      sin6_scope_id field, so this part will be rewritten in
157	 *      the near future.
158	 */
159	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
160	    dstsock->sin6_scope_id) {
161		/*
162		 * I'm not sure if boundary check for scope_id is done
163		 * somewhere...
164		 */
165		if (dstsock->sin6_scope_id < 0 ||
166		    if_index < dstsock->sin6_scope_id) {
167			*errorp = ENXIO; /* XXX: better error? */
168			return(0);
169		}
170		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
171				       dst);
172		if (ia6 == 0) {
173			*errorp = EADDRNOTAVAIL;
174			return(0);
175		}
176		return(&satosin6(&ia6->ia_addr)->sin6_addr);
177	}
178
179	/*
180	 * If the destination address is a multicast address and
181	 * the outgoing interface for the address is specified
182	 * by the caller, use an address associated with the interface.
183	 * There is a sanity check here; if the destination has node-local
184	 * scope, the outgoing interfacde should be a loopback address.
185	 * Even if the outgoing interface is not specified, we also
186	 * choose a loopback interface as the outgoing interface.
187	 */
188	if (IN6_IS_ADDR_MULTICAST(dst)) {
189		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
190
191		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
192			ifp = &loif[0];
193		}
194
195		if (ifp) {
196			ia6 = in6_ifawithscope(ifp, dst);
197			if (ia6 == 0) {
198				*errorp = EADDRNOTAVAIL;
199				return(0);
200			}
201			return(&satosin6(&ia6->ia_addr)->sin6_addr);
202		}
203	}
204
205	/*
206	 * If the next hop address for the packet is specified
207	 * by caller, use an address associated with the route
208	 * to the next hop.
209	 */
210	{
211		struct sockaddr_in6 *sin6_next;
212		struct rtentry *rt;
213
214		if (opts && opts->ip6po_nexthop) {
215			sin6_next = satosin6(opts->ip6po_nexthop);
216			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
217			if (rt) {
218				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
219				if (ia6 == 0)
220					ia6 = ifatoia6(rt->rt_ifa);
221			}
222			if (ia6 == 0) {
223				*errorp = EADDRNOTAVAIL;
224				return(0);
225			}
226			return(&satosin6(&ia6->ia_addr)->sin6_addr);
227		}
228	}
229
230	/*
231	 * If route is known or can be allocated now,
232	 * our src addr is taken from the i/f, else punt.
233	 */
234	if (ro) {
235		if (ro->ro_rt &&
236		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
237			RTFREE(ro->ro_rt);
238			ro->ro_rt = (struct rtentry *)0;
239		}
240		if (ro->ro_rt == (struct rtentry *)0 ||
241		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
242			/* No route yet, so try to acquire one */
243			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
244			ro->ro_dst.sin6_family = AF_INET6;
245			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
246			ro->ro_dst.sin6_addr = *dst;
247			if (!IN6_IS_ADDR_MULTICAST(dst)) {
248				rtalloc((struct route *)ro);
249			}
250		}
251
252		/*
253		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
254		 * the address. But we don't know why it does so.
255		 * It is necessary to ensure the scope even for lo0
256		 * so doesn't check out IFF_LOOPBACK.
257		 */
258
259		if (ro->ro_rt) {
260			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
261			if (ia6 == 0) /* xxx scope error ?*/
262				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
263		}
264#if 0
265		/*
266		 * xxx The followings are necessary? (kazu)
267		 * I don't think so.
268		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
269		 */
270		if (ia6 == 0) {
271			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
272
273			sin6->sin6_addr = *dst;
274
275			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
276			if (ia6 == 0)
277				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
278			if (ia6 == 0)
279				return(0);
280			return(&satosin6(&ia6->ia_addr)->sin6_addr);
281		}
282#endif /* 0 */
283		if (ia6 == 0) {
284			*errorp = EHOSTUNREACH;	/* no route */
285			return(0);
286		}
287		return(&satosin6(&ia6->ia_addr)->sin6_addr);
288	}
289
290	*errorp = EADDRNOTAVAIL;
291	return(0);
292}
293
294/*
295 * Default hop limit selection. The precedence is as follows:
296 * 1. Hoplimit value specified via ioctl.
297 * 2. (If the outgoing interface is detected) the current
298 *     hop limit of the interface specified by router advertisement.
299 * 3. The system default hoplimit.
300*/
301int
302in6_selecthlim(inp, ifp)
303	struct inpcb *inp;
304	struct ifnet *ifp;
305{
306	if (inp && inp->inp_hops >= 0)
307		return(inp->inp_hops);
308	else if (ifp)
309		return(nd_ifinfo[ifp->if_index].chlim);
310	else
311		return(ip6_defhlim);
312}
313
314/*
315 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
316 * If the address scope of is link-local, embed the interface index in the
317 * address.  The routine determines our precedence
318 * between advanced API scope/interface specification and basic API
319 * specification.
320 *
321 * this function should be nuked in the future, when we get rid of
322 * embedded scopeid thing.
323 *
324 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
325 * there can be multiple interfaces that belong to a particular scope zone
326 * (in specification, we have 1:N mapping between a scope zone and interfaces).
327 * we may want to change the function to return something other than ifp.
328 */
329int
330in6_embedscope(in6, sin6, in6p, ifpp)
331	struct in6_addr *in6;
332	const struct sockaddr_in6 *sin6;
333	struct inpcb *in6p;
334	struct ifnet **ifpp;
335{
336	struct ifnet *ifp = NULL;
337	u_int32_t scopeid;
338
339	*in6 = sin6->sin6_addr;
340	scopeid = sin6->sin6_scope_id;
341	if (ifpp)
342		*ifpp = NULL;
343
344	/*
345	 * don't try to read sin6->sin6_addr beyond here, since the caller may
346	 * ask us to overwrite existing sockaddr_in6
347	 */
348
349	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
350		struct in6_pktinfo *pi;
351
352		/*
353		 * KAME assumption: link id == interface id
354		 */
355
356		if (in6p->inp_outputopts6 &&
357		    (pi = in6p->inp_outputopts6->ip6po_pktinfo) &&
358		    pi->ipi6_ifindex) {
359			ifp = ifindex2ifnet[pi->ipi6_ifindex];
360			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
361		} else if (IN6_IS_ADDR_MULTICAST(in6)
362			&& in6p->inp_moptions6
363			&& in6p->inp_moptions6->im6o_multicast_ifp) {
364			ifp = in6p->inp_moptions6->im6o_multicast_ifp;
365			in6->s6_addr16[1] = htons(ifp->if_index);
366		} else if (scopeid) {
367			/* boundary check */
368			if (scopeid < 0 || if_index < scopeid)
369				return ENXIO;  /* XXX EINVAL? */
370			ifp = ifindex2ifnet[scopeid];
371			/*XXX assignment to 16bit from 32bit variable */
372			in6->s6_addr16[1] = htons(scopeid & 0xffff);
373		}
374
375		if (ifpp)
376			*ifpp = ifp;
377	}
378
379	return 0;
380}
381
382/*
383 * generate standard sockaddr_in6 from embedded form.
384 * touches sin6_addr and sin6_scope_id only.
385 *
386 * this function should be nuked in the future, when we get rid of
387 * embedded scopeid thing.
388 */
389int
390in6_recoverscope(sin6, in6, ifp)
391	struct sockaddr_in6 *sin6;
392	const struct in6_addr *in6;
393	struct ifnet *ifp;
394{
395	u_int32_t scopeid;
396
397	sin6->sin6_addr = *in6;
398
399	/*
400	 * don't try to read *in6 beyond here, since the caller may
401	 * ask us to overwrite existing sockaddr_in6
402	 */
403
404	sin6->sin6_scope_id = 0;
405	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
406		/*
407		 * KAME assumption: link id == interface id
408		 */
409		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
410		if (scopeid) {
411			/* sanity check */
412			if (scopeid < 0 || if_index < scopeid)
413				return ENXIO;
414			if (ifp && ifp->if_index != scopeid)
415				return ENXIO;
416			sin6->sin6_addr.s6_addr16[1] = 0;
417			sin6->sin6_scope_id = scopeid;
418		}
419	}
420
421	return 0;
422}
423