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