in6_src.c revision 1.1
1/*	$OpenBSD: in6_src.c,v 1.1 1999/12/08 06:50:21 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#ifdef __NetBSD__	/*XXX*/
68#include "opt_ipsec.h"
69#endif
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/malloc.h>
74#include <sys/mbuf.h>
75#include <sys/protosw.h>
76#include <sys/socket.h>
77#include <sys/socketvar.h>
78#include <sys/ioctl.h>
79#include <sys/errno.h>
80#include <sys/time.h>
81#include <sys/proc.h>
82
83#include <net/if.h>
84#include <net/route.h>
85
86#include <netinet/in.h>
87#include <netinet/in_var.h>
88#include <netinet/in_systm.h>
89#include <netinet/ip.h>
90#include <netinet/in_pcb.h>
91#include <netinet6/in6_var.h>
92#include <netinet6/ip6.h>
93#if 0
94#include <netinet6/in6_pcb.h>
95#endif
96#include <netinet6/ip6_var.h>
97#include <netinet6/nd6.h>
98
99#ifndef __bsdi__
100#include "loop.h"
101#endif
102#if defined(__NetBSD__) || defined(__OpenBSD__)
103extern struct ifnet loif[NLOOP];
104#endif
105
106/*
107 * Return an IPv6 address, which is the most appropriate for given
108 * destination and user specified options.
109 * If necessary, this function lookups the routing table and return
110 * an entry to the caller for later use.
111 */
112struct in6_addr *
113in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
114	struct sockaddr_in6 *dstsock;
115	struct ip6_pktopts *opts;
116	struct ip6_moptions *mopts;
117	struct route_in6 *ro;
118	struct in6_addr *laddr;
119	int *errorp;
120{
121	struct in6_addr *dst;
122	struct in6_ifaddr *ia6 = 0;
123	struct in6_pktinfo *pi = NULL;
124
125	dst = &dstsock->sin6_addr;
126	*errorp = 0;
127
128	/*
129	 * If the source address is explicitly specified by the caller,
130	 * use it.
131	 */
132	if (opts && (pi = opts->ip6po_pktinfo) &&
133	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
134		return(&pi->ipi6_addr);
135
136	/*
137	 * If the source address is not specified but the socket(if any)
138	 * is already bound, use the bound address.
139	 */
140	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
141		return(laddr);
142
143	/*
144	 * If the caller doesn't specify the source address but
145	 * the outgoing interface, use an address associated with
146	 * the interface.
147	 */
148	if (pi && pi->ipi6_ifindex) {
149		/* XXX boundary check is assumed to be already done. */
150		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
151				       dst);
152		if (ia6 == 0) {
153			*errorp = EADDRNOTAVAIL;
154			return(0);
155		}
156		return(&satosin6(&ia6->ia_addr)->sin6_addr);
157	}
158
159	/*
160	 * If the destination address is a link-local unicast address or
161	 * a multicast address, and if the outgoing interface is specified
162	 * by the sin6_scope_id filed, use an address associated with the
163	 * interface.
164	 * XXX: We're now trying to define more specific semantics of
165	 *      sin6_scope_id field, so this part will be rewritten in
166	 *      the near future.
167	 */
168	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
169	    dstsock->sin6_scope_id) {
170		/*
171		 * I'm not sure if boundary check for scope_id is done
172		 * somewhere...
173		 */
174		if (dstsock->sin6_scope_id < 0 ||
175		    if_index < dstsock->sin6_scope_id) {
176			*errorp = ENXIO; /* XXX: better error? */
177			return(0);
178		}
179		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
180				       dst);
181		if (ia6 == 0) {
182			*errorp = EADDRNOTAVAIL;
183			return(0);
184		}
185		return(&satosin6(&ia6->ia_addr)->sin6_addr);
186	}
187
188	/*
189	 * If the destination address is a multicast address and
190	 * the outgoing interface for the address is specified
191	 * by the caller, use an address associated with the interface.
192	 * There is a sanity check here; if the destination has node-local
193	 * scope, the outgoing interfacde should be a loopback address.
194	 * Even if the outgoing interface is not specified, we also
195	 * choose a loopback interface as the outgoing interface.
196	 */
197	if (IN6_IS_ADDR_MULTICAST(dst)) {
198		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
199#ifdef __bsdi__
200#if _BSDI_VERSION >= 199802
201		extern struct ifnet *loifp;
202#else
203		extern struct ifnet loif;
204		struct ifnet *loifp = &loif;
205#endif
206#endif
207
208		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
209#ifdef __bsdi__
210			ifp = loifp;
211#else
212			ifp = &loif[0];
213#endif
214		}
215
216		if (ifp) {
217			ia6 = in6_ifawithscope(ifp, dst);
218			if (ia6 == 0) {
219				*errorp = EADDRNOTAVAIL;
220				return(0);
221			}
222			return(&satosin6(&ia6->ia_addr)->sin6_addr);
223		}
224	}
225
226	/*
227	 * If the next hop address for the packet is specified
228	 * by caller, use an address associated with the route
229	 * to the next hop.
230	 */
231	{
232		struct sockaddr_in6 *sin6_next;
233		struct rtentry *rt;
234
235		if (opts && opts->ip6po_nexthop) {
236			sin6_next = satosin6(opts->ip6po_nexthop);
237			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
238			if (rt) {
239				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
240				if (ia6 == 0)
241					ia6 = ifatoia6(rt->rt_ifa);
242			}
243			if (ia6 == 0) {
244				*errorp = EADDRNOTAVAIL;
245				return(0);
246			}
247			return(&satosin6(&ia6->ia_addr)->sin6_addr);
248		}
249	}
250
251	/*
252	 * If route is known or can be allocated now,
253	 * our src addr is taken from the i/f, else punt.
254	 */
255	if (ro) {
256		if (ro->ro_rt &&
257		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
258			RTFREE(ro->ro_rt);
259			ro->ro_rt = (struct rtentry *)0;
260		}
261		if (ro->ro_rt == (struct rtentry *)0 ||
262		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
263			/* No route yet, so try to acquire one */
264			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
265			ro->ro_dst.sin6_family = AF_INET6;
266			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
267			ro->ro_dst.sin6_addr = *dst;
268			if (IN6_IS_ADDR_MULTICAST(dst)) {
269#ifdef __FreeBSD__
270				ro->ro_rt = rtalloc1(&((struct route *)ro)
271						     ->ro_dst, 0, 0UL);
272#endif /*__FreeBSD__*/
273#if defined(__bsdi__) || defined(__NetBSD__)
274				ro->ro_rt = rtalloc1(&((struct route *)ro)
275						     ->ro_dst, 0);
276#endif /*__bsdi__*/
277			} else {
278#ifdef __FreeBSD__
279				rtcalloc((struct route *)ro);
280#else
281				rtalloc((struct route *)ro);
282#endif
283			}
284		}
285
286		/*
287		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
288		 * the address. But we don't know why it does so.
289		 * It is necessary to ensure the scope even for lo0
290		 * so doesn't check out IFF_LOOPBACK.
291		 */
292
293		if (ro->ro_rt) {
294			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
295			if (ia6 == 0) /* xxx scope error ?*/
296				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
297		}
298#if 0
299		/*
300		 * xxx The followings are necessary? (kazu)
301		 * I don't think so.
302		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
303		 */
304		if (ia6 == 0) {
305			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
306
307			sin6->sin6_addr = *dst;
308
309			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
310			if (ia6 == 0)
311				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
312			if (ia6 == 0)
313				return(0);
314			return(&satosin6(&ia6->ia_addr)->sin6_addr);
315		}
316#endif /* 0 */
317		if (ia6 == 0) {
318			*errorp = EHOSTUNREACH;	/* no route */
319			return(0);
320		}
321		return(&satosin6(&ia6->ia_addr)->sin6_addr);
322	}
323
324	*errorp = EADDRNOTAVAIL;
325	return(0);
326}
327
328/*
329 * Default hop limit selection. The precedence is as follows:
330 * 1. Hoplimit valued specified via ioctl.
331 * 2. (If the outgoing interface is detected) the current
332 *     hop limit of the interface specified by router advertisement.
333 * 3. The system default hoplimit.
334*/
335int
336in6_selecthlim(inp, ifp)
337	struct inpcb *inp;
338	struct ifnet *ifp;
339{
340	if (inp && inp->inp_hops >= 0)
341		return(inp->inp_hops);
342	else if (ifp)
343		return(nd_ifinfo[ifp->if_index].chlim);
344	else
345		return(ip6_defhlim);
346}
347