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