in6_src.c revision 1.13 1 /* $NetBSD: in6_src.c,v 1.13 2002/06/08 20:06:44 itojun Exp $ */
2 /* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 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/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.13 2002/06/08 20:06:44 itojun Exp $");
70
71 #include "opt_inet.h"
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/ioctl.h>
81 #include <sys/errno.h>
82 #include <sys/time.h>
83 #include <sys/proc.h>
84
85 #include <net/if.h>
86 #include <net/route.h>
87
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/nd6.h>
98 #ifdef ENABLE_DEFAULT_SCOPE
99 #include <netinet6/scope6_var.h>
100 #endif
101
102 #include <net/net_osdep.h>
103
104 #include "loop.h"
105 extern struct ifnet loif[NLOOP];
106
107 /*
108 * Return an IPv6 address, which is the most appropriate for a given
109 * destination and user specified options.
110 * If necessary, this function lookups the routing table and returns
111 * an entry to the caller for later use.
112 */
113 struct in6_addr *
114 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
115 struct sockaddr_in6 *dstsock;
116 struct ip6_pktopts *opts;
117 struct ip6_moptions *mopts;
118 struct route_in6 *ro;
119 struct in6_addr *laddr;
120 int *errorp;
121 {
122 struct in6_addr *dst;
123 struct in6_ifaddr *ia6 = 0;
124 struct in6_pktinfo *pi = NULL;
125
126 dst = &dstsock->sin6_addr;
127 *errorp = 0;
128
129 /*
130 * If the source address is explicitly specified by the caller,
131 * use it.
132 */
133 if (opts && (pi = opts->ip6po_pktinfo) &&
134 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
135 return(&pi->ipi6_addr);
136
137 /*
138 * If the source address is not specified but the socket(if any)
139 * is already bound, use the bound address.
140 */
141 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
142 return(laddr);
143
144 /*
145 * If the caller doesn't specify the source address but
146 * the outgoing interface, use an address associated with
147 * the interface.
148 */
149 if (pi && pi->ipi6_ifindex) {
150 /* XXX boundary check is assumed to be already done. */
151 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
152 dst);
153 if (ia6 == 0) {
154 *errorp = EADDRNOTAVAIL;
155 return(0);
156 }
157 return(&satosin6(&ia6->ia_addr)->sin6_addr);
158 }
159
160 /*
161 * If the destination address is a link-local unicast address or
162 * a multicast address, and if the outgoing interface is specified
163 * by the sin6_scope_id filed, use an address associated with the
164 * interface.
165 * XXX: We're now trying to define more specific semantics of
166 * sin6_scope_id field, so this part will be rewritten in
167 * the near future.
168 */
169 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
170 dstsock->sin6_scope_id) {
171 /*
172 * I'm not sure if boundary check for scope_id is done
173 * somewhere...
174 */
175 if (dstsock->sin6_scope_id < 0 ||
176 if_index < dstsock->sin6_scope_id) {
177 *errorp = ENXIO; /* XXX: better error? */
178 return(0);
179 }
180 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
181 dst);
182 if (ia6 == 0) {
183 *errorp = EADDRNOTAVAIL;
184 return(0);
185 }
186 return(&satosin6(&ia6->ia_addr)->sin6_addr);
187 }
188
189 /*
190 * If the destination address is a multicast address and
191 * the outgoing interface for the address is specified
192 * by the caller, use an address associated with the interface.
193 * There is a sanity check here; if the destination has node-local
194 * scope, the outgoing interfacde should be a loopback address.
195 * Even if the outgoing interface is not specified, we also
196 * choose a loopback interface as the outgoing interface.
197 */
198 if (IN6_IS_ADDR_MULTICAST(dst)) {
199 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
200
201 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
202 ifp = &loif[0];
203 }
204
205 if (ifp) {
206 ia6 = in6_ifawithscope(ifp, dst);
207 if (ia6 == 0) {
208 *errorp = EADDRNOTAVAIL;
209 return(0);
210 }
211 return(&satosin6(&ia6->ia_addr)->sin6_addr);
212 }
213 }
214
215 /*
216 * If the next hop address for the packet is specified
217 * by caller, use an address associated with the route
218 * to the next hop.
219 */
220 {
221 struct sockaddr_in6 *sin6_next;
222 struct rtentry *rt;
223
224 if (opts && opts->ip6po_nexthop) {
225 sin6_next = satosin6(opts->ip6po_nexthop);
226 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
227 if (rt) {
228 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
229 if (ia6 == 0)
230 ia6 = ifatoia6(rt->rt_ifa);
231 }
232 if (ia6 == 0) {
233 *errorp = EADDRNOTAVAIL;
234 return(0);
235 }
236 return(&satosin6(&ia6->ia_addr)->sin6_addr);
237 }
238 }
239
240 /*
241 * If route is known or can be allocated now,
242 * our src addr is taken from the i/f, else punt.
243 * Note that we should check the address family of the
244 * cached destination, in case of sharing the cache with IPv4.
245 */
246 if (ro) {
247 if (ro->ro_rt &&
248 (ro->ro_dst.sin6_family != AF_INET6 ||
249 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst))) {
250 RTFREE(ro->ro_rt);
251 ro->ro_rt = (struct rtentry *)0;
252 }
253 if (ro->ro_rt == (struct rtentry *)0 ||
254 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
255 struct sockaddr_in6 *sa6;
256
257 /* No route yet, so try to acquire one */
258 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
259 sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
260 sa6->sin6_family = AF_INET6;
261 sa6->sin6_len = sizeof(struct sockaddr_in6);
262 sa6->sin6_addr = *dst;
263 sa6->sin6_scope_id = dstsock->sin6_scope_id;
264 if (IN6_IS_ADDR_MULTICAST(dst)) {
265 ro->ro_rt = rtalloc1(&((struct route *)ro)
266 ->ro_dst, 0);
267 } else {
268 rtalloc((struct route *)ro);
269 }
270 }
271
272 /*
273 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
274 * the address. But we don't know why it does so.
275 * It is necessary to ensure the scope even for lo0
276 * so doesn't check out IFF_LOOPBACK.
277 */
278
279 if (ro->ro_rt) {
280 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
281 if (ia6 == 0) /* xxx scope error ?*/
282 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
283 }
284 #if 0
285 /*
286 * xxx The followings are necessary? (kazu)
287 * I don't think so.
288 * It's for SO_DONTROUTE option in IPv4.(jinmei)
289 */
290 if (ia6 == 0) {
291 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
292
293 sin6->sin6_addr = *dst;
294
295 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
296 if (ia6 == 0)
297 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
298 if (ia6 == 0)
299 return(0);
300 return(&satosin6(&ia6->ia_addr)->sin6_addr);
301 }
302 #endif /* 0 */
303 if (ia6 == 0) {
304 *errorp = EHOSTUNREACH; /* no route */
305 return(0);
306 }
307 return(&satosin6(&ia6->ia_addr)->sin6_addr);
308 }
309
310 *errorp = EADDRNOTAVAIL;
311 return(0);
312 }
313
314 /*
315 * Default hop limit selection. The precedence is as follows:
316 * 1. Hoplimit value specified via ioctl.
317 * 2. (If the outgoing interface is detected) the current
318 * hop limit of the interface specified by router advertisement.
319 * 3. The system default hoplimit.
320 */
321 int
322 in6_selecthlim(in6p, ifp)
323 struct in6pcb *in6p;
324 struct ifnet *ifp;
325 {
326 if (in6p && in6p->in6p_hops >= 0)
327 return(in6p->in6p_hops);
328 else if (ifp)
329 return(ND_IFINFO(ifp)->chlim);
330 else
331 return(ip6_defhlim);
332 }
333
334 /*
335 * Find an empty port and set it to the specified PCB.
336 */
337 int
338 in6_pcbsetport(laddr, in6p)
339 struct in6_addr *laddr;
340 struct in6pcb *in6p;
341 {
342 struct socket *so = in6p->in6p_socket;
343 struct in6pcb *head = in6p->in6p_head;
344 u_int16_t last_port, lport = 0;
345 int wild = 0;
346 void *t;
347 u_int16_t min, max;
348
349 /* XXX: this is redundant when called from in6_pcbbind */
350 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
351 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
352 (so->so_options & SO_ACCEPTCONN) == 0))
353 wild = IN6PLOOKUP_WILDCARD;
354
355 if (in6p->in6p_flags & IN6P_LOWPORT) {
356 #ifndef IPNOPRIVPORTS
357 struct proc *p = curproc; /* XXX */
358
359 if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0))
360 return (EACCES);
361 #endif
362 min = ip6_lowportmin;
363 max = ip6_lowportmax;
364 } else {
365 min = ip6_anonportmin;
366 max = ip6_anonportmax;
367 }
368
369 /* value out of range */
370 if (head->in6p_lport < min)
371 head->in6p_lport = min;
372 else if (head->in6p_lport > max)
373 head->in6p_lport = min;
374 last_port = head->in6p_lport;
375 goto startover; /*to randomize*/
376 for (;;) {
377 lport = htons(head->in6p_lport);
378 if (IN6_IS_ADDR_V4MAPPED(laddr)) {
379 #if 0
380 t = in_pcblookup_bind(&tcbtable,
381 (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
382 lport);
383 #else
384 t = NULL;
385 #endif
386 } else {
387 t = in6_pcblookup(head, &zeroin6_addr, 0, laddr,
388 lport, wild);
389 }
390 if (t == 0)
391 break;
392 startover:
393 if (head->in6p_lport >= max)
394 head->in6p_lport = min;
395 else
396 head->in6p_lport++;
397 if (head->in6p_lport == last_port)
398 return (EADDRINUSE);
399 }
400
401 in6p->in6p_lport = lport;
402 return(0); /* success */
403 }
404
405 /*
406 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
407 * If the address scope of is link-local, embed the interface index in the
408 * address. The routine determines our precedence
409 * between advanced API scope/interface specification and basic API
410 * specification.
411 *
412 * this function should be nuked in the future, when we get rid of
413 * embedded scopeid thing.
414 *
415 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
416 * there can be multiple interfaces that belong to a particular scope zone
417 * (in specification, we have 1:N mapping between a scope zone and interfaces).
418 * we may want to change the function to return something other than ifp.
419 */
420 int
421 in6_embedscope(in6, sin6, in6p, ifpp)
422 struct in6_addr *in6;
423 const struct sockaddr_in6 *sin6;
424 struct in6pcb *in6p;
425 struct ifnet **ifpp;
426 {
427 struct ifnet *ifp = NULL;
428 u_int32_t scopeid;
429
430 *in6 = sin6->sin6_addr;
431 scopeid = sin6->sin6_scope_id;
432 if (ifpp)
433 *ifpp = NULL;
434
435 /*
436 * don't try to read sin6->sin6_addr beyond here, since the caller may
437 * ask us to overwrite existing sockaddr_in6
438 */
439
440 #ifdef ENABLE_DEFAULT_SCOPE
441 if (scopeid == 0)
442 scopeid = scope6_addr2default(in6);
443 #endif
444
445 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
446 struct in6_pktinfo *pi;
447
448 /*
449 * KAME assumption: link id == interface id
450 */
451
452 if (in6p && in6p->in6p_outputopts &&
453 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
454 pi->ipi6_ifindex) {
455 ifp = ifindex2ifnet[pi->ipi6_ifindex];
456 in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
457 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
458 in6p->in6p_moptions &&
459 in6p->in6p_moptions->im6o_multicast_ifp) {
460 ifp = in6p->in6p_moptions->im6o_multicast_ifp;
461 in6->s6_addr16[1] = htons(ifp->if_index);
462 } else if (scopeid) {
463 /* boundary check */
464 if (scopeid < 0 || if_index < scopeid)
465 return ENXIO; /* XXX EINVAL? */
466 ifp = ifindex2ifnet[scopeid];
467 /* XXX assignment to 16bit from 32bit variable */
468 in6->s6_addr16[1] = htons(scopeid & 0xffff);
469 }
470
471 if (ifpp)
472 *ifpp = ifp;
473 }
474
475 return 0;
476 }
477
478 /*
479 * generate standard sockaddr_in6 from embedded form.
480 * touches sin6_addr and sin6_scope_id only.
481 *
482 * this function should be nuked in the future, when we get rid of
483 * embedded scopeid thing.
484 */
485 int
486 in6_recoverscope(sin6, in6, ifp)
487 struct sockaddr_in6 *sin6;
488 const struct in6_addr *in6;
489 struct ifnet *ifp;
490 {
491 u_int32_t scopeid;
492
493 sin6->sin6_addr = *in6;
494
495 /*
496 * don't try to read *in6 beyond here, since the caller may
497 * ask us to overwrite existing sockaddr_in6
498 */
499
500 sin6->sin6_scope_id = 0;
501 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
502 /*
503 * KAME assumption: link id == interface id
504 */
505 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
506 if (scopeid) {
507 /* sanity check */
508 if (scopeid < 0 || if_index < scopeid)
509 return ENXIO;
510 if (ifp && ifp->if_index != scopeid)
511 return ENXIO;
512 sin6->sin6_addr.s6_addr16[1] = 0;
513 sin6->sin6_scope_id = scopeid;
514 }
515 }
516
517 return 0;
518 }
519
520 /*
521 * just clear the embedded scope identifer.
522 */
523 void
524 in6_clearscope(addr)
525 struct in6_addr *addr;
526 {
527 if (IN6_IS_SCOPE_LINKLOCAL(addr))
528 addr->s6_addr16[1] = 0;
529 }
530