in6_src.c revision 1.4
1/*	$OpenBSD: in6_src.c,v 1.4 2000/02/28 11:55:22 itojun 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 <netinet/ip6.h>
89#include <netinet6/ip6_var.h>
90#include <netinet6/nd6.h>
91
92#include "loop.h"
93
94extern struct ifnet loif[NLOOP];
95
96/*
97 * Return an IPv6 address, which is the most appropriate for given
98 * destination and user specified options.
99 * If necessary, this function lookups the routing table and return
100 * an entry to the caller for later use.
101 */
102struct in6_addr *
103in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
104	struct sockaddr_in6 *dstsock;
105	struct ip6_pktopts *opts;
106	struct ip6_moptions *mopts;
107	struct route_in6 *ro;
108	struct in6_addr *laddr;
109	int *errorp;
110{
111	struct in6_addr *dst;
112	struct in6_ifaddr *ia6 = 0;
113	struct in6_pktinfo *pi = NULL;
114
115	dst = &dstsock->sin6_addr;
116	*errorp = 0;
117
118	/*
119	 * If the source address is explicitly specified by the caller,
120	 * use it.
121	 */
122	if (opts && (pi = opts->ip6po_pktinfo) &&
123	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
124		return(&pi->ipi6_addr);
125
126	/*
127	 * If the source address is not specified but the socket(if any)
128	 * is already bound, use the bound address.
129	 */
130	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
131		return(laddr);
132
133	/*
134	 * If the caller doesn't specify the source address but
135	 * the outgoing interface, use an address associated with
136	 * the interface.
137	 */
138	if (pi && pi->ipi6_ifindex) {
139		/* XXX boundary check is assumed to be already done. */
140		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
141				       dst);
142		if (ia6 == 0) {
143			*errorp = EADDRNOTAVAIL;
144			return(0);
145		}
146		return(&satosin6(&ia6->ia_addr)->sin6_addr);
147	}
148
149	/*
150	 * If the destination address is a link-local unicast address or
151	 * a multicast address, and if the outgoing interface is specified
152	 * by the sin6_scope_id filed, use an address associated with the
153	 * interface.
154	 * XXX: We're now trying to define more specific semantics of
155	 *      sin6_scope_id field, so this part will be rewritten in
156	 *      the near future.
157	 */
158	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
159	    dstsock->sin6_scope_id) {
160		/*
161		 * I'm not sure if boundary check for scope_id is done
162		 * somewhere...
163		 */
164		if (dstsock->sin6_scope_id < 0 ||
165		    if_index < dstsock->sin6_scope_id) {
166			*errorp = ENXIO; /* XXX: better error? */
167			return(0);
168		}
169		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
170				       dst);
171		if (ia6 == 0) {
172			*errorp = EADDRNOTAVAIL;
173			return(0);
174		}
175		return(&satosin6(&ia6->ia_addr)->sin6_addr);
176	}
177
178	/*
179	 * If the destination address is a multicast address and
180	 * the outgoing interface for the address is specified
181	 * by the caller, use an address associated with the interface.
182	 * There is a sanity check here; if the destination has node-local
183	 * scope, the outgoing interfacde should be a loopback address.
184	 * Even if the outgoing interface is not specified, we also
185	 * choose a loopback interface as the outgoing interface.
186	 */
187	if (IN6_IS_ADDR_MULTICAST(dst)) {
188		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
189
190		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
191			ifp = &loif[0];
192		}
193
194		if (ifp) {
195			ia6 = in6_ifawithscope(ifp, dst);
196			if (ia6 == 0) {
197				*errorp = EADDRNOTAVAIL;
198				return(0);
199			}
200			return(&satosin6(&ia6->ia_addr)->sin6_addr);
201		}
202	}
203
204	/*
205	 * If the next hop address for the packet is specified
206	 * by caller, use an address associated with the route
207	 * to the next hop.
208	 */
209	{
210		struct sockaddr_in6 *sin6_next;
211		struct rtentry *rt;
212
213		if (opts && opts->ip6po_nexthop) {
214			sin6_next = satosin6(opts->ip6po_nexthop);
215			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
216			if (rt) {
217				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
218				if (ia6 == 0)
219					ia6 = ifatoia6(rt->rt_ifa);
220			}
221			if (ia6 == 0) {
222				*errorp = EADDRNOTAVAIL;
223				return(0);
224			}
225			return(&satosin6(&ia6->ia_addr)->sin6_addr);
226		}
227	}
228
229	/*
230	 * If route is known or can be allocated now,
231	 * our src addr is taken from the i/f, else punt.
232	 */
233	if (ro) {
234		if (ro->ro_rt &&
235		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
236			RTFREE(ro->ro_rt);
237			ro->ro_rt = (struct rtentry *)0;
238		}
239		if (ro->ro_rt == (struct rtentry *)0 ||
240		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
241			/* No route yet, so try to acquire one */
242			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
243			ro->ro_dst.sin6_family = AF_INET6;
244			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
245			ro->ro_dst.sin6_addr = *dst;
246			if (!IN6_IS_ADDR_MULTICAST(dst)) {
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 valued 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*/
300int
301in6_selecthlim(inp, ifp)
302	struct inpcb *inp;
303	struct ifnet *ifp;
304{
305	if (inp && inp->inp_hops >= 0)
306		return(inp->inp_hops);
307	else if (ifp)
308		return(nd_ifinfo[ifp->if_index].chlim);
309	else
310		return(ip6_defhlim);
311}
312