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