]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_src.c
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/in6_src.c,v 1.1.2.2 2001/07/03 11:01:52 ume Exp $ */
30 /* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Copyright (c) 1982, 1986, 1991, 1993
63 * The Regents of the University of California. All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/malloc.h>
100 #include <sys/mbuf.h>
101 #include <sys/protosw.h>
102 #include <sys/socket.h>
103 #include <sys/socketvar.h>
104 #include <sys/errno.h>
105 #include <sys/time.h>
106 #include <sys/proc.h>
107 #include <kern/lock.h>
110 #include <net/route.h>
112 #include <netinet/in.h>
113 #include <netinet/in_var.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/ip.h>
116 #include <netinet/in_pcb.h>
117 #include <netinet6/in6_var.h>
118 #include <netinet/ip6.h>
119 #include <netinet6/in6_pcb.h>
120 #include <netinet6/ip6_var.h>
121 #include <netinet6/nd6.h>
122 #if ENABLE_DEFAULT_SCOPE
123 #include <netinet6/scope6_var.h>
126 #include <net/net_osdep.h>
131 * Return an IPv6 address, which is the most appropriate for a given
132 * destination and user specified options.
133 * If necessary, this function lookups the routing table and returns
134 * an entry to the caller for later use.
138 struct sockaddr_in6
*dstsock
,
139 struct ip6_pktopts
*opts
,
140 struct ip6_moptions
*mopts
,
141 struct route_in6
*ro
,
142 struct in6_addr
*laddr
,
143 struct in6_addr
*src_storage
,
146 struct in6_addr
*dst
;
147 struct in6_ifaddr
*ia6
= 0;
148 struct in6_pktinfo
*pi
= NULL
;
150 dst
= &dstsock
->sin6_addr
;
154 * If the source address is explicitly specified by the caller,
157 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
158 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
))
159 return(&pi
->ipi6_addr
);
162 * If the source address is not specified but the socket(if any)
163 * is already bound, use the bound address.
165 if (laddr
&& !IN6_IS_ADDR_UNSPECIFIED(laddr
))
169 * If the caller doesn't specify the source address but
170 * the outgoing interface, use an address associated with
173 if (pi
&& pi
->ipi6_ifindex
) {
174 ifnet_t out_ifp
= NULL
;
175 ifnet_head_lock_shared();
176 if (pi
->ipi6_ifindex
> if_index
) {
178 *errorp
= EADDRNOTAVAIL
;
181 out_ifp
= ifindex2ifnet
[pi
->ipi6_ifindex
];
185 /* XXX boundary check is assumed to be already done. */
186 ia6
= in6_ifawithscope(out_ifp
, dst
);
188 *errorp
= EADDRNOTAVAIL
;
191 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
192 ifafree(&ia6
->ia_ifa
);
197 * If the destination address is a link-local unicast address or
198 * a multicast address, and if the outgoing interface is specified
199 * by the sin6_scope_id filed, use an address associated with the
201 * XXX: We're now trying to define more specific semantics of
202 * sin6_scope_id field, so this part will be rewritten in
205 if ((IN6_IS_ADDR_LINKLOCAL(dst
) || IN6_IS_ADDR_MULTICAST(dst
)) &&
206 dstsock
->sin6_scope_id
) {
208 * I'm not sure if boundary check for scope_id is done
211 * Since sin6_scope_id is unsigned, we only need to check against if_index.
213 ifnet_t out_ifp
= NULL
;
214 ifnet_head_lock_shared();
215 if (if_index
< dstsock
->sin6_scope_id
) {
216 *errorp
= ENXIO
; /* XXX: better error? */
220 out_ifp
= ifindex2ifnet
[dstsock
->sin6_scope_id
];
224 ia6
= in6_ifawithscope(out_ifp
, dst
);
226 *errorp
= EADDRNOTAVAIL
;
229 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
230 ifafree(&ia6
->ia_ifa
);
235 * If the destination address is a multicast address and
236 * the outgoing interface for the address is specified
237 * by the caller, use an address associated with the interface.
238 * There is a sanity check here; if the destination has node-local
239 * scope, the outgoing interfacde should be a loopback address.
240 * Even if the outgoing interface is not specified, we also
241 * choose a loopback interface as the outgoing interface.
243 if (IN6_IS_ADDR_MULTICAST(dst
)) {
244 struct ifnet
*ifp
= mopts
? mopts
->im6o_multicast_ifp
: NULL
;
246 if (ifp
== NULL
&& IN6_IS_ADDR_MC_NODELOCAL(dst
)) {
251 ia6
= in6_ifawithscope(ifp
, dst
);
253 *errorp
= EADDRNOTAVAIL
;
256 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
257 ifafree(&ia6
->ia_ifa
);
263 * If the next hop address for the packet is specified
264 * by caller, use an address associated with the route
268 struct sockaddr_in6
*sin6_next
;
271 if (opts
&& opts
->ip6po_nexthop
) {
272 sin6_next
= satosin6(opts
->ip6po_nexthop
);
273 rt
= nd6_lookup(&sin6_next
->sin6_addr
, 1, NULL
, 0);
275 RT_LOCK_ASSERT_HELD(rt
);
276 ia6
= in6_ifawithscope(rt
->rt_ifp
, dst
);
278 ia6
= ifatoia6(rt
->rt_ifa
);
280 ifaref(&ia6
->ia_ifa
);
284 *errorp
= EADDRNOTAVAIL
;
286 RT_REMREF_LOCKED(rt
);
291 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
292 ifafree(&ia6
->ia_ifa
);
293 RT_REMREF_LOCKED(rt
);
300 * If route is known or can be allocated now,
301 * our src addr is taken from the i/f, else punt.
304 if (ro
->ro_rt
!= NULL
)
306 if (ro
->ro_rt
!= NULL
&&
307 (!(ro
->ro_rt
->rt_flags
& RTF_UP
) ||
308 satosin6(&ro
->ro_dst
)->sin6_family
!= AF_INET6
||
309 ro
->ro_rt
->generation_id
!= route_generation
||
310 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
,
312 RT_UNLOCK(ro
->ro_rt
);
316 if (ro
->ro_rt
== NULL
|| ro
->ro_rt
->rt_ifp
== NULL
) {
317 struct sockaddr_in6
*sa6
;
319 if (ro
->ro_rt
!= NULL
)
320 RT_UNLOCK(ro
->ro_rt
);
321 /* No route yet, so try to acquire one */
322 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in6
));
323 sa6
= (struct sockaddr_in6
*)&ro
->ro_dst
;
324 sa6
->sin6_family
= AF_INET6
;
325 sa6
->sin6_len
= sizeof(struct sockaddr_in6
);
326 sa6
->sin6_addr
= *dst
;
328 sa6
->sin6_scope_id
= dstsock
->sin6_scope_id
;
330 if (IN6_IS_ADDR_MULTICAST(dst
)) {
331 ro
->ro_rt
= rtalloc1(
332 &((struct route
*)ro
)->ro_dst
, 0, 0);
334 rtalloc_ign((struct route
*)ro
, 0);
336 if (ro
->ro_rt
!= NULL
)
341 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
342 * the address. But we don't know why it does so.
343 * It is necessary to ensure the scope even for lo0
344 * so doesn't check out IFF_LOOPBACK.
346 if (ro
->ro_rt
!= NULL
) {
347 RT_LOCK_ASSERT_HELD(ro
->ro_rt
);
348 ia6
= in6_ifawithscope(ro
->ro_rt
->rt_ifa
->ifa_ifp
, dst
);
350 ia6
= ifatoia6(ro
->ro_rt
->rt_ifa
);
352 ifaref(&ia6
->ia_ifa
);
354 RT_UNLOCK(ro
->ro_rt
);
358 * xxx The followings are necessary? (kazu)
360 * It's for SO_DONTROUTE option in IPv4.(jinmei)
363 struct sockaddr_in6 sin6
= {sizeof(sin6
), AF_INET6
, 0};
365 sin6
->sin6_addr
= *dst
;
367 ia6
= ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6
)));
369 ia6
= ifatoia6(ifa_ifwithnet(sin6tosa(&sin6
)));
372 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
373 ifafree(&ia6
->ia_ifa
);
378 *errorp
= EHOSTUNREACH
; /* no route */
381 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
382 ifafree(&ia6
->ia_ifa
);
386 *errorp
= EADDRNOTAVAIL
;
391 * Default hop limit selection. The precedence is as follows:
392 * 1. Hoplimit value specified via ioctl.
393 * 2. (If the outgoing interface is detected) the current
394 * hop limit of the interface specified by router advertisement.
395 * 3. The system default hoplimit.
402 if (in6p
&& in6p
->in6p_hops
>= 0) {
403 return(in6p
->in6p_hops
);
405 lck_rw_lock_shared(nd_if_rwlock
);
406 if (ifp
&& ifp
->if_index
< nd_ifinfo_indexlim
) {
407 u_int8_t chlim
= nd_ifinfo
[ifp
->if_index
].chlim
;
408 lck_rw_done(nd_if_rwlock
);
411 lck_rw_done(nd_if_rwlock
);
418 * XXX: this is borrowed from in6_pcbbind(). If possible, we should
419 * share this function by all *bsd*...
423 __unused
struct in6_addr
*laddr
,
428 struct socket
*so
= inp
->inp_socket
;
429 u_int16_t lport
= 0, first
, last
, *lastport
;
430 int count
, error
= 0, wild
= 0;
431 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
432 if (!locked
) { /* Make sure we don't run into a deadlock: 4052373 */
433 if (!lck_rw_try_lock_exclusive(pcbinfo
->mtx
)) {
434 socket_unlock(inp
->inp_socket
, 0);
435 lck_rw_lock_exclusive(pcbinfo
->mtx
);
436 socket_lock(inp
->inp_socket
, 0);
440 /* XXX: this is redundant when called from in6_pcbbind */
441 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
442 wild
= INPLOOKUP_WILDCARD
;
444 inp
->inp_flags
|= INP_ANONPORT
;
446 if (inp
->inp_flags
& INP_HIGHPORT
) {
447 first
= ipport_hifirstauto
; /* sysctl */
448 last
= ipport_hilastauto
;
449 lastport
= &pcbinfo
->lasthi
;
450 } else if (inp
->inp_flags
& INP_LOWPORT
) {
451 if ((error
= proc_suser(p
)) != 0) {
453 lck_rw_done(pcbinfo
->mtx
);
456 first
= ipport_lowfirstauto
; /* 1023 */
457 last
= ipport_lowlastauto
; /* 600 */
458 lastport
= &pcbinfo
->lastlow
;
460 first
= ipport_firstauto
; /* sysctl */
461 last
= ipport_lastauto
;
462 lastport
= &pcbinfo
->lastport
;
465 * Simple check to ensure all ports are not used up causing
468 * We split the two cases (up and down) so that the direction
469 * is not being tested on each round of the loop.
475 count
= first
- last
;
478 if (count
-- < 0) { /* completely used? */
480 * Undo any address bind that may have
483 inp
->in6p_laddr
= in6addr_any
;
485 lck_rw_done(pcbinfo
->mtx
);
489 if (*lastport
> first
|| *lastport
< last
)
491 lport
= htons(*lastport
);
492 } while (in6_pcblookup_local(pcbinfo
,
493 &inp
->in6p_laddr
, lport
, wild
));
498 count
= last
- first
;
501 if (count
-- < 0) { /* completely used? */
503 * Undo any address bind that may have
506 inp
->in6p_laddr
= in6addr_any
;
508 lck_rw_done(pcbinfo
->mtx
);
512 if (*lastport
< first
|| *lastport
> last
)
514 lport
= htons(*lastport
);
515 } while (in6_pcblookup_local(pcbinfo
,
516 &inp
->in6p_laddr
, lport
, wild
));
519 inp
->inp_lport
= lport
;
520 if (in_pcbinshash(inp
, 1) != 0) {
521 inp
->in6p_laddr
= in6addr_any
;
524 lck_rw_done(pcbinfo
->mtx
);
529 lck_rw_done(pcbinfo
->mtx
);
534 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
535 * If the address scope of is link-local, embed the interface index in the
536 * address. The routine determines our precedence
537 * between advanced API scope/interface specification and basic API
540 * this function should be nuked in the future, when we get rid of
541 * embedded scopeid thing.
543 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
544 * there can be multiple interfaces that belong to a particular scope zone
545 * (in specification, we have 1:N mapping between a scope zone and interfaces).
546 * we may want to change the function to return something other than ifp.
550 struct in6_addr
*in6
,
551 const struct sockaddr_in6
*sin6
,
552 #ifdef HAVE_NRL_INPCB
554 #define in6p_outputopts inp_outputopts6
555 #define in6p_moptions inp_moptions6
561 struct ifnet
*ifp
= NULL
;
564 *in6
= sin6
->sin6_addr
;
565 scopeid
= sin6
->sin6_scope_id
;
570 * don't try to read sin6->sin6_addr beyond here, since the caller may
571 * ask us to overwrite existing sockaddr_in6
574 #ifdef ENABLE_DEFAULT_SCOPE
576 scopeid
= scope6_addr2default(in6
);
579 if (IN6_IS_SCOPE_LINKLOCAL(in6
)) {
580 struct in6_pktinfo
*pi
;
583 * KAME assumption: link id == interface id
586 ifnet_head_lock_shared();
587 if (in6p
&& in6p
->in6p_outputopts
&&
588 (pi
= in6p
->in6p_outputopts
->ip6po_pktinfo
) &&
590 ifp
= ifindex2ifnet
[pi
->ipi6_ifindex
];
591 in6
->s6_addr16
[1] = htons(pi
->ipi6_ifindex
);
592 } else if (in6p
&& IN6_IS_ADDR_MULTICAST(in6
) &&
593 in6p
->in6p_moptions
&&
594 in6p
->in6p_moptions
->im6o_multicast_ifp
) {
595 ifp
= in6p
->in6p_moptions
->im6o_multicast_ifp
;
596 in6
->s6_addr16
[1] = htons(ifp
->if_index
);
597 } else if (scopeid
) {
599 * Since scopeid is unsigned, we only have to check it
602 if (if_index
< scopeid
) {
604 return ENXIO
; /* XXX EINVAL? */
607 ifp
= ifindex2ifnet
[scopeid
];
608 /*XXX assignment to 16bit from 32bit variable */
609 in6
->s6_addr16
[1] = htons(scopeid
& 0xffff);
620 #undef in6p_outputopts
625 * generate standard sockaddr_in6 from embedded form.
626 * touches sin6_addr and sin6_scope_id only.
628 * this function should be nuked in the future, when we get rid of
629 * embedded scopeid thing.
633 struct sockaddr_in6
*sin6
,
634 const struct in6_addr
*in6
,
639 sin6
->sin6_addr
= *in6
;
642 * don't try to read *in6 beyond here, since the caller may
643 * ask us to overwrite existing sockaddr_in6
646 sin6
->sin6_scope_id
= 0;
647 if (IN6_IS_SCOPE_LINKLOCAL(in6
)) {
649 * KAME assumption: link id == interface id
651 scopeid
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
656 * Since scopeid is unsigned, we only have to check it
659 if (if_index
< scopeid
)
661 if (ifp
&& ifp
->if_index
!= scopeid
)
663 sin6
->sin6_addr
.s6_addr16
[1] = 0;
664 sin6
->sin6_scope_id
= scopeid
;
672 * just clear the embedded scope identifer.
673 * XXX: currently used for bsdi4 only as a supplement function.
677 struct in6_addr
*addr
;
679 if (IN6_IS_SCOPE_LINKLOCAL(addr
))
680 addr
->s6_addr16
[1] = 0;