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