in6_src.c revision 1.2
1/*	$OpenBSD: in6_src.c,v 1.2 1999/12/10 10:04:28 angelos Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 1982, 1986, 1991, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 *    must display the following acknowledgement:
46 *	This product includes software developed by the University of
47 *	California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 *    may be used to endorse or promote products derived from this software
50 *    without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
65 */
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/malloc.h>
70#include <sys/mbuf.h>
71#include <sys/protosw.h>
72#include <sys/socket.h>
73#include <sys/socketvar.h>
74#include <sys/ioctl.h>
75#include <sys/errno.h>
76#include <sys/time.h>
77#include <sys/proc.h>
78
79#include <net/if.h>
80#include <net/route.h>
81
82#include <netinet/in.h>
83#include <netinet/in_var.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#include <netinet/in_pcb.h>
87#include <netinet6/in6_var.h>
88#include <netinet6/ip6.h>
89#if 0
90#include <netinet6/in6_pcb.h>
91#endif
92#include <netinet6/ip6_var.h>
93#include <netinet6/nd6.h>
94
95#include "loop.h"
96
97extern struct ifnet loif[NLOOP];
98
99/*
100 * Return an IPv6 address, which is the most appropriate for given
101 * destination and user specified options.
102 * If necessary, this function lookups the routing table and return
103 * an entry to the caller for later use.
104 */
105struct in6_addr *
106in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
107	struct sockaddr_in6 *dstsock;
108	struct ip6_pktopts *opts;
109	struct ip6_moptions *mopts;
110	struct route_in6 *ro;
111	struct in6_addr *laddr;
112	int *errorp;
113{
114	struct in6_addr *dst;
115	struct in6_ifaddr *ia6 = 0;
116	struct in6_pktinfo *pi = NULL;
117
118	dst = &dstsock->sin6_addr;
119	*errorp = 0;
120
121	/*
122	 * If the source address is explicitly specified by the caller,
123	 * use it.
124	 */
125	if (opts && (pi = opts->ip6po_pktinfo) &&
126	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
127		return(&pi->ipi6_addr);
128
129	/*
130	 * If the source address is not specified but the socket(if any)
131	 * is already bound, use the bound address.
132	 */
133	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
134		return(laddr);
135
136	/*
137	 * If the caller doesn't specify the source address but
138	 * the outgoing interface, use an address associated with
139	 * the interface.
140	 */
141	if (pi && pi->ipi6_ifindex) {
142		/* XXX boundary check is assumed to be already done. */
143		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
144				       dst);
145		if (ia6 == 0) {
146			*errorp = EADDRNOTAVAIL;
147			return(0);
148		}
149		return(&satosin6(&ia6->ia_addr)->sin6_addr);
150	}
151
152	/*
153	 * If the destination address is a link-local unicast address or
154	 * a multicast address, and if the outgoing interface is specified
155	 * by the sin6_scope_id filed, use an address associated with the
156	 * interface.
157	 * XXX: We're now trying to define more specific semantics of
158	 *      sin6_scope_id field, so this part will be rewritten in
159	 *      the near future.
160	 */
161	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
162	    dstsock->sin6_scope_id) {
163		/*
164		 * I'm not sure if boundary check for scope_id is done
165		 * somewhere...
166		 */
167		if (dstsock->sin6_scope_id < 0 ||
168		    if_index < dstsock->sin6_scope_id) {
169			*errorp = ENXIO; /* XXX: better error? */
170			return(0);
171		}
172		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
173				       dst);
174		if (ia6 == 0) {
175			*errorp = EADDRNOTAVAIL;
176			return(0);
177		}
178		return(&satosin6(&ia6->ia_addr)->sin6_addr);
179	}
180
181	/*
182	 * If the destination address is a multicast address and
183	 * the outgoing interface for the address is specified
184	 * by the caller, use an address associated with the interface.
185	 * There is a sanity check here; if the destination has node-local
186	 * scope, the outgoing interfacde should be a loopback address.
187	 * Even if the outgoing interface is not specified, we also
188	 * choose a loopback interface as the outgoing interface.
189	 */
190	if (IN6_IS_ADDR_MULTICAST(dst)) {
191		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
192
193		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
194			ifp = &loif[0];
195		}
196
197		if (ifp) {
198			ia6 = in6_ifawithscope(ifp, dst);
199			if (ia6 == 0) {
200				*errorp = EADDRNOTAVAIL;
201				return(0);
202			}
203			return(&satosin6(&ia6->ia_addr)->sin6_addr);
204		}
205	}
206
207	/*
208	 * If the next hop address for the packet is specified
209	 * by caller, use an address associated with the route
210	 * to the next hop.
211	 */
212	{
213		struct sockaddr_in6 *sin6_next;
214		struct rtentry *rt;
215
216		if (opts && opts->ip6po_nexthop) {
217			sin6_next = satosin6(opts->ip6po_nexthop);
218			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
219			if (rt) {
220				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
221				if (ia6 == 0)
222					ia6 = ifatoia6(rt->rt_ifa);
223			}
224			if (ia6 == 0) {
225				*errorp = EADDRNOTAVAIL;
226				return(0);
227			}
228			return(&satosin6(&ia6->ia_addr)->sin6_addr);
229		}
230	}
231
232	/*
233	 * If route is known or can be allocated now,
234	 * our src addr is taken from the i/f, else punt.
235	 */
236	if (ro) {
237		if (ro->ro_rt &&
238		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
239			RTFREE(ro->ro_rt);
240			ro->ro_rt = (struct rtentry *)0;
241		}
242		if (ro->ro_rt == (struct rtentry *)0 ||
243		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
244			/* No route yet, so try to acquire one */
245			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
246			ro->ro_dst.sin6_family = AF_INET6;
247			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
248			ro->ro_dst.sin6_addr = *dst;
249			if (!IN6_IS_ADDR_MULTICAST(dst)) {
250				rtalloc((struct route *)ro);
251			}
252		}
253
254		/*
255		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
256		 * the address. But we don't know why it does so.
257		 * It is necessary to ensure the scope even for lo0
258		 * so doesn't check out IFF_LOOPBACK.
259		 */
260
261		if (ro->ro_rt) {
262			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
263			if (ia6 == 0) /* xxx scope error ?*/
264				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
265		}
266#if 0
267		/*
268		 * xxx The followings are necessary? (kazu)
269		 * I don't think so.
270		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
271		 */
272		if (ia6 == 0) {
273			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
274
275			sin6->sin6_addr = *dst;
276
277			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
278			if (ia6 == 0)
279				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
280			if (ia6 == 0)
281				return(0);
282			return(&satosin6(&ia6->ia_addr)->sin6_addr);
283		}
284#endif /* 0 */
285		if (ia6 == 0) {
286			*errorp = EHOSTUNREACH;	/* no route */
287			return(0);
288		}
289		return(&satosin6(&ia6->ia_addr)->sin6_addr);
290	}
291
292	*errorp = EADDRNOTAVAIL;
293	return(0);
294}
295
296/*
297 * Default hop limit selection. The precedence is as follows:
298 * 1. Hoplimit valued specified via ioctl.
299 * 2. (If the outgoing interface is detected) the current
300 *     hop limit of the interface specified by router advertisement.
301 * 3. The system default hoplimit.
302*/
303int
304in6_selecthlim(inp, ifp)
305	struct inpcb *inp;
306	struct ifnet *ifp;
307{
308	if (inp && inp->inp_hops >= 0)
309		return(inp->inp_hops);
310	else if (ifp)
311		return(nd_ifinfo[ifp->if_index].chlim);
312	else
313		return(ip6_defhlim);
314}
315