2 * Copyright (c) 2000-2014 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@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Copyright (c) 1982, 1986, 1991, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
94 #include <sys/param.h>
95 #include <sys/systm.h>
96 #include <sys/malloc.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/errno.h>
102 #include <sys/time.h>
103 #include <sys/proc.h>
104 #include <sys/sysctl.h>
105 #include <sys/kauth.h>
106 #include <sys/priv.h>
107 #include <kern/locks.h>
110 #include <net/if_types.h>
111 #include <net/route.h>
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/ip.h>
117 #include <netinet/in_pcb.h>
118 #include <netinet6/in6_var.h>
119 #include <netinet/ip6.h>
120 #include <netinet6/in6_pcb.h>
121 #include <netinet6/ip6_var.h>
122 #include <netinet6/scope6_var.h>
123 #include <netinet6/nd6.h>
125 #include <net/net_osdep.h>
129 SYSCTL_DECL(_net_inet6_ip6
);
131 static int ip6_select_srcif_debug
= 0;
132 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, select_srcif_debug
,
133 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_select_srcif_debug
, 0,
134 "log source interface selection debug info");
136 #define ADDR_LABEL_NOTAPP (-1)
137 struct in6_addrpolicy defaultaddrpolicy
;
139 int ip6_prefer_tempaddr
= 1;
140 #ifdef ENABLE_ADDRSEL
141 extern lck_mtx_t
*addrsel_mutex
;
142 #define ADDRSEL_LOCK() lck_mtx_lock(addrsel_mutex)
143 #define ADDRSEL_UNLOCK() lck_mtx_unlock(addrsel_mutex)
145 #define ADDRSEL_LOCK()
146 #define ADDRSEL_UNLOCK()
149 static int selectroute(struct sockaddr_in6
*, struct sockaddr_in6
*,
150 struct ip6_pktopts
*, struct ip6_moptions
*, struct in6_ifaddr
**,
151 struct route_in6
*, struct ifnet
**, struct rtentry
**, int, int,
152 struct ip6_out_args
*ip6oa
);
153 static int in6_selectif(struct sockaddr_in6
*, struct ip6_pktopts
*,
154 struct ip6_moptions
*, struct route_in6
*ro
,
155 struct ip6_out_args
*, struct ifnet
**);
156 static void init_policy_queue(void);
157 static int add_addrsel_policyent(const struct in6_addrpolicy
*);
158 #ifdef ENABLE_ADDRSEL
159 static int delete_addrsel_policyent(const struct in6_addrpolicy
*);
161 static int walk_addrsel_policy(int (*)(const struct in6_addrpolicy
*, void *),
163 static int dump_addrsel_policyent(const struct in6_addrpolicy
*, void *);
164 static struct in6_addrpolicy
*match_addrsel_policy(struct sockaddr_in6
*);
165 void addrsel_policy_init(void);
168 * Return an IPv6 address, which is the most appropriate for a given
169 * destination and user specified options.
170 * If necessary, this function lookups the routing table and returns
171 * an entry to the caller for later use.
173 #define REPLACE(r) do {\
174 if ((r) < sizeof (ip6stat.ip6s_sources_rule) / \
175 sizeof (ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
176 ip6stat.ip6s_sources_rule[(r)]++; \
179 #define NEXTSRC(r) do {\
180 if ((r) < sizeof (ip6stat.ip6s_sources_rule) / \
181 sizeof (ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
182 ip6stat.ip6s_sources_rule[(r)]++; \
183 goto next; /* XXX: we can't use 'continue' here */ \
185 #define BREAK(r) do { \
186 if ((r) < sizeof (ip6stat.ip6s_sources_rule) / \
187 sizeof (ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
188 ip6stat.ip6s_sources_rule[(r)]++; \
189 goto out; /* XXX: we can't use 'break' here */ \
193 * Regardless of error, it will return an ifp with a reference held if the
194 * caller provides a non-NULL ifpp. The caller is responsible for checking
195 * if the returned ifp is valid and release its reference at all times.
198 in6_selectsrc(struct sockaddr_in6
*dstsock
, struct ip6_pktopts
*opts
,
199 struct inpcb
*inp
, struct route_in6
*ro
,
200 struct ifnet
**ifpp
, struct in6_addr
*src_storage
, unsigned int ifscope
,
204 struct ifnet
*ifp
= NULL
;
205 struct in6_ifaddr
*ia
= NULL
, *ia_best
= NULL
;
206 struct in6_pktinfo
*pi
= NULL
;
207 int dst_scope
= -1, best_scope
= -1, best_matchlen
= -1;
208 struct in6_addrpolicy
*dst_policy
= NULL
, *best_policy
= NULL
;
211 struct ip6_moptions
*mopts
;
212 struct ip6_out_args ip6oa
= { ifscope
, { 0 }, IP6OAF_SELECT_SRCIF
, 0 };
213 boolean_t islocal
= FALSE
;
214 uint64_t secs
= net_uptime();
216 dst
= dstsock
->sin6_addr
; /* make a copy for local operation */
222 mopts
= inp
->in6p_moptions
;
223 if (INP_NO_CELLULAR(inp
))
224 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
225 if (INP_NO_EXPENSIVE(inp
))
226 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
227 if (INP_AWDL_UNRESTRICTED(inp
))
228 ip6oa
.ip6oa_flags
|= IP6OAF_AWDL_UNRESTRICTED
;
234 if (ip6oa
.ip6oa_boundif
!= IFSCOPE_NONE
)
235 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
238 * If the source address is explicitly specified by the caller,
239 * check if the requested source address is indeed a unicast address
240 * assigned to the node, and can be used as the packet's source
241 * address. If everything is okay, use the address as source.
243 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
244 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
)) {
245 struct sockaddr_in6 srcsock
;
246 struct in6_ifaddr
*ia6
;
248 /* get the outgoing interface */
249 if ((*errorp
= in6_selectif(dstsock
, opts
, mopts
, ro
, &ip6oa
,
256 * determine the appropriate zone id of the source based on
257 * the zone of the destination and the outgoing interface.
258 * If the specified address is ambiguous wrt the scope zone,
259 * the interface must be specified; otherwise, ifa_ifwithaddr()
260 * will fail matching the address.
262 bzero(&srcsock
, sizeof (srcsock
));
263 srcsock
.sin6_family
= AF_INET6
;
264 srcsock
.sin6_len
= sizeof (srcsock
);
265 srcsock
.sin6_addr
= pi
->ipi6_addr
;
267 *errorp
= in6_setscope(&srcsock
.sin6_addr
, ifp
, NULL
);
273 ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr((struct sockaddr
*)
276 *errorp
= EADDRNOTAVAIL
;
280 IFA_LOCK_SPIN(&ia6
->ia_ifa
);
281 if ((ia6
->ia6_flags
& (IN6_IFF_ANYCAST
| IN6_IFF_NOTREADY
)) ||
282 (inp
&& inp_restricted_send(inp
, ia6
->ia_ifa
.ifa_ifp
))) {
283 IFA_UNLOCK(&ia6
->ia_ifa
);
284 IFA_REMREF(&ia6
->ia_ifa
);
285 *errorp
= EHOSTUNREACH
;
290 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
291 IFA_UNLOCK(&ia6
->ia_ifa
);
292 IFA_REMREF(&ia6
->ia_ifa
);
297 * Otherwise, if the socket has already bound the source, just use it.
299 if (inp
!= NULL
&& !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
300 src_storage
= &inp
->in6p_laddr
;
305 * If the address is not specified, choose the best one based on
306 * the outgoing interface and the destination address.
309 /* get the outgoing interface */
310 if ((*errorp
= in6_selectif(dstsock
, opts
, mopts
, ro
, &ip6oa
,
316 *errorp
= in6_setscope(&dst
, ifp
, &odstzone
);
321 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
323 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
324 int new_scope
= -1, new_matchlen
= -1;
325 struct in6_addrpolicy
*new_policy
= NULL
;
326 u_int32_t srczone
, osrczone
, dstzone
;
328 struct ifnet
*ifp1
= ia
->ia_ifp
;
330 IFA_LOCK(&ia
->ia_ifa
);
332 * We'll never take an address that breaks the scope zone
333 * of the destination. We also skip an address if its zone
334 * does not contain the outgoing interface.
335 * XXX: we should probably use sin6_scope_id here.
337 if (in6_setscope(&dst
, ifp1
, &dstzone
) ||
341 src
= ia
->ia_addr
.sin6_addr
;
342 if (in6_setscope(&src
, ifp
, &osrczone
) ||
343 in6_setscope(&src
, ifp1
, &srczone
) ||
347 /* avoid unusable addresses */
349 (IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
| IN6_IFF_DETACHED
)))
352 if (!ip6_use_deprecated
&& IFA6_IS_DEPRECATED(ia
, secs
))
355 if (!nd6_optimistic_dad
&&
356 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) != 0)
359 /* Rule 1: Prefer same address */
360 if (IN6_ARE_ADDR_EQUAL(&dst
, &ia
->ia_addr
.sin6_addr
))
361 BREAK(IP6S_SRCRULE_1
); /* there should be no better candidate */
364 REPLACE(IP6S_SRCRULE_0
);
366 /* Rule 2: Prefer appropriate scope */
368 dst_scope
= in6_addrscope(&dst
);
369 new_scope
= in6_addrscope(&ia
->ia_addr
.sin6_addr
);
370 if (IN6_ARE_SCOPE_CMP(best_scope
, new_scope
) < 0) {
371 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0)
372 REPLACE(IP6S_SRCRULE_2
);
373 NEXTSRC(IP6S_SRCRULE_2
);
374 } else if (IN6_ARE_SCOPE_CMP(new_scope
, best_scope
) < 0) {
375 if (IN6_ARE_SCOPE_CMP(new_scope
, dst_scope
) < 0)
376 NEXTSRC(IP6S_SRCRULE_2
);
377 REPLACE(IP6S_SRCRULE_2
);
381 * Rule 3: Avoid deprecated addresses. Note that the case of
382 * !ip6_use_deprecated is already rejected above.
384 if (!IFA6_IS_DEPRECATED(ia_best
, secs
) &&
385 IFA6_IS_DEPRECATED(ia
, secs
))
386 NEXTSRC(IP6S_SRCRULE_3
);
387 if (IFA6_IS_DEPRECATED(ia_best
, secs
) &&
388 !IFA6_IS_DEPRECATED(ia
, secs
))
389 REPLACE(IP6S_SRCRULE_3
);
392 * RFC 4429 says that optimistic addresses are equivalent to
393 * deprecated addresses, so avoid them here.
395 if ((ia_best
->ia6_flags
& IN6_IFF_OPTIMISTIC
) == 0 &&
396 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) != 0)
397 NEXTSRC(IP6S_SRCRULE_3
);
398 if ((ia_best
->ia6_flags
& IN6_IFF_OPTIMISTIC
) != 0 &&
399 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) == 0)
400 REPLACE(IP6S_SRCRULE_3
);
402 /* Rule 4: Prefer home addresses */
404 * XXX: This is a TODO. We should probably merge the MIP6
408 /* Rule 5: Prefer outgoing interface */
409 if (ia_best
->ia_ifp
== ifp
&& ia
->ia_ifp
!= ifp
)
410 NEXTSRC(IP6S_SRCRULE_5
);
411 if (ia_best
->ia_ifp
!= ifp
&& ia
->ia_ifp
== ifp
)
412 REPLACE(IP6S_SRCRULE_5
);
414 /* Rule 5.5: Prefer addresses in a prefix advertised by the next hop. */
415 if (ro
!= NULL
&& ro
->ro_rt
!= NULL
&& ia_best
->ia6_ndpr
!= NULL
&&
416 ia
->ia6_ndpr
!= NULL
) {
417 struct rtentry
*rta
, *rtb
;
420 NDPR_LOCK(ia_best
->ia6_ndpr
);
421 rta
= ia_best
->ia6_ndpr
->ndpr_rt
;
424 NDPR_UNLOCK(ia_best
->ia6_ndpr
);
426 NDPR_LOCK(ia
->ia6_ndpr
);
427 rtb
= ia
->ia6_ndpr
->ndpr_rt
;
430 NDPR_UNLOCK(ia
->ia6_ndpr
);
432 if (rta
== NULL
|| rtb
== NULL
)
434 else if (rta
== ro
->ro_rt
&& rtb
!= ro
->ro_rt
)
436 else if (rta
!= ro
->ro_rt
&& rtb
== ro
->ro_rt
)
448 NEXTSRC(IP6S_SRCRULE_5_5
);
451 REPLACE(IP6S_SRCRULE_5_5
);
459 * Rule 6: Prefer matching label
460 * Note that best_policy should be non-NULL here.
462 if (dst_policy
== NULL
)
463 dst_policy
= in6_addrsel_lookup_policy(dstsock
);
464 if (dst_policy
->label
!= ADDR_LABEL_NOTAPP
) {
465 new_policy
= in6_addrsel_lookup_policy(&ia
->ia_addr
);
466 if (dst_policy
->label
== best_policy
->label
&&
467 dst_policy
->label
!= new_policy
->label
)
468 NEXTSRC(IP6S_SRCRULE_6
);
469 if (dst_policy
->label
!= best_policy
->label
&&
470 dst_policy
->label
== new_policy
->label
)
471 REPLACE(IP6S_SRCRULE_6
);
475 * Rule 7: Prefer temporary addresses.
476 * We allow users to reverse the logic by configuring
477 * a sysctl variable, so that transparency conscious users can
478 * always prefer stable addresses.
479 * Don't use temporary addresses for local destinations or
480 * for multicast addresses unless we were passed in an option.
482 if (IN6_IS_ADDR_MULTICAST(&dst
) ||
483 in6_matchlen(&ia_best
->ia_addr
.sin6_addr
, &dst
) >=
487 opts
->ip6po_prefer_tempaddr
== IP6PO_TEMPADDR_SYSTEM
) {
488 prefer_tempaddr
= islocal
? 0 : ip6_prefer_tempaddr
;
489 } else if (opts
->ip6po_prefer_tempaddr
==
490 IP6PO_TEMPADDR_NOTPREFER
) {
494 if (!(ia_best
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
495 (ia
->ia6_flags
& IN6_IFF_TEMPORARY
)) {
497 REPLACE(IP6S_SRCRULE_7
);
499 NEXTSRC(IP6S_SRCRULE_7
);
501 if ((ia_best
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
502 !(ia
->ia6_flags
& IN6_IFF_TEMPORARY
)) {
504 NEXTSRC(IP6S_SRCRULE_7
);
506 REPLACE(IP6S_SRCRULE_7
);
510 * Rule 7x: prefer addresses on alive interfaces.
511 * This is a KAME specific rule.
513 if ((ia_best
->ia_ifp
->if_flags
& IFF_UP
) &&
514 !(ia
->ia_ifp
->if_flags
& IFF_UP
))
515 NEXTSRC(IP6S_SRCRULE_7x
);
516 if (!(ia_best
->ia_ifp
->if_flags
& IFF_UP
) &&
517 (ia
->ia_ifp
->if_flags
& IFF_UP
))
518 REPLACE(IP6S_SRCRULE_7x
);
521 * Rule 8: Use longest matching prefix.
523 new_matchlen
= in6_matchlen(&ia
->ia_addr
.sin6_addr
, &dst
);
524 if (best_matchlen
< new_matchlen
)
525 REPLACE(IP6S_SRCRULE_8
);
526 if (new_matchlen
< best_matchlen
)
527 NEXTSRC(IP6S_SRCRULE_8
);
530 * Last resort: just keep the current candidate.
531 * Or, do we need more rules?
533 IFA_UNLOCK(&ia
->ia_ifa
);
537 best_scope
= (new_scope
>= 0 ? new_scope
:
538 in6_addrscope(&ia
->ia_addr
.sin6_addr
));
539 best_policy
= (new_policy
? new_policy
:
540 in6_addrsel_lookup_policy(&ia
->ia_addr
));
541 best_matchlen
= (new_matchlen
>= 0 ? new_matchlen
:
542 in6_matchlen(&ia
->ia_addr
.sin6_addr
, &dst
));
543 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for ia_best */
544 IFA_UNLOCK(&ia
->ia_ifa
);
546 IFA_REMREF(&ia_best
->ia_ifa
);
551 IFA_UNLOCK(&ia
->ia_ifa
);
555 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for ia_best */
556 IFA_UNLOCK(&ia
->ia_ifa
);
558 IFA_REMREF(&ia_best
->ia_ifa
);
563 lck_rw_done(&in6_ifaddr_rwlock
);
565 if (ia_best
!= NULL
&& inp
&&
566 inp_restricted_send(inp
, ia_best
->ia_ifa
.ifa_ifp
)) {
567 IFA_REMREF(&ia_best
->ia_ifa
);
569 *errorp
= EHOSTUNREACH
;
572 if ((ia
= ia_best
) == NULL
) {
574 *errorp
= EADDRNOTAVAIL
;
579 IFA_LOCK_SPIN(&ia
->ia_ifa
);
580 *src_storage
= satosin6(&ia
->ia_addr
)->sin6_addr
;
581 IFA_UNLOCK(&ia
->ia_ifa
);
582 IFA_REMREF(&ia
->ia_ifa
);
585 /* if ifp is non-NULL, refcnt held in in6_selectif() */
587 } else if (ifp
!= NULL
) {
590 return (src_storage
);
594 * Given a source IPv6 address (and route, if available), determine the best
595 * interface to send the packet from. Checking for (and updating) the
596 * ROF_SRCIF_SELECTED flag in the pcb-supplied route placeholder is done
597 * without any locks, based on the assumption that in the event this is
598 * called from ip6_output(), the output operation is single-threaded per-pcb,
599 * i.e. for any given pcb there can only be one thread performing output at
602 * This routine is analogous to in_selectsrcif() for IPv4. Regardless of
603 * error, it will return an ifp with a reference held if the caller provides
604 * a non-NULL retifp. The caller is responsible for checking if the
605 * returned ifp is valid and release its reference at all times.
607 * clone - meaningful only for bsdi and freebsd
610 selectroute(struct sockaddr_in6
*srcsock
, struct sockaddr_in6
*dstsock
,
611 struct ip6_pktopts
*opts
, struct ip6_moptions
*mopts
,
612 struct in6_ifaddr
**retsrcia
, struct route_in6
*ro
,
613 struct ifnet
**retifp
, struct rtentry
**retrt
, int clone
,
614 int norouteok
, struct ip6_out_args
*ip6oa
)
617 struct ifnet
*ifp
= NULL
, *ifp0
= NULL
;
618 struct route_in6
*route
= NULL
;
619 struct sockaddr_in6
*sin6_next
;
620 struct in6_pktinfo
*pi
= NULL
;
621 struct in6_addr
*dst
= &dstsock
->sin6_addr
;
622 struct ifaddr
*ifa
= NULL
;
623 char s_src
[MAX_IPv6_STR_LEN
], s_dst
[MAX_IPv6_STR_LEN
];
624 boolean_t select_srcif
, proxied_ifa
= FALSE
, local_dst
= FALSE
;
625 unsigned int ifscope
= ((ip6oa
!= NULL
) ?
626 ip6oa
->ip6oa_boundif
: IFSCOPE_NONE
);
629 char ip6buf
[INET6_ADDRSTRLEN
];
631 if (dstsock
->sin6_addr
.s6_addr32
[0] == 0 &&
632 dstsock
->sin6_addr
.s6_addr32
[1] == 0 &&
633 !IN6_IS_ADDR_LOOPBACK(&dstsock
->sin6_addr
)) {
634 printf("in6_selectroute: strange destination %s\n",
635 ip6_sprintf(ip6buf
, &dstsock
->sin6_addr
));
637 printf("in6_selectroute: destination = %s%%%d\n",
638 ip6_sprintf(ip6buf
, &dstsock
->sin6_addr
),
639 dstsock
->sin6_scope_id
); /* for debug */
649 if (ip6_select_srcif_debug
) {
651 src
= (srcsock
!= NULL
) ? srcsock
->sin6_addr
: in6addr_any
;
652 (void) inet_ntop(AF_INET6
, &src
, s_src
, sizeof (s_src
));
653 (void) inet_ntop(AF_INET6
, dst
, s_dst
, sizeof (s_dst
));
657 * If the destination address is UNSPECIFIED addr, bail out.
659 if (IN6_IS_ADDR_UNSPECIFIED(dst
)) {
660 error
= EHOSTUNREACH
;
665 * Perform source interface selection only if Scoped Routing
666 * is enabled and a source address that isn't unspecified.
668 select_srcif
= (ip6_doscopedroute
&& srcsock
!= NULL
&&
669 !IN6_IS_ADDR_UNSPECIFIED(&srcsock
->sin6_addr
));
672 * If Scoped Routing is disabled, ignore the given ifscope.
673 * Otherwise even if source selection won't be performed,
674 * we still obey IPV6_BOUND_IF.
676 if (!ip6_doscopedroute
&& ifscope
!= IFSCOPE_NONE
)
677 ifscope
= IFSCOPE_NONE
;
679 /* If the caller specified the outgoing interface explicitly, use it */
680 if (opts
!= NULL
&& (pi
= opts
->ip6po_pktinfo
) != NULL
&&
681 pi
->ipi6_ifindex
!= 0) {
683 * If IPV6_PKTINFO takes precedence over IPV6_BOUND_IF.
685 ifscope
= pi
->ipi6_ifindex
;
686 ifnet_head_lock_shared();
687 /* ifp may be NULL if detached or out of range */
689 ((ifscope
<= if_index
) ? ifindex2ifnet
[ifscope
] : NULL
);
691 if (norouteok
|| retrt
== NULL
|| IN6_IS_ADDR_MULTICAST(dst
)) {
693 * We do not have to check or get the route for
694 * multicast. If the caller didn't ask/care for
695 * the route and we have no interface to use,
699 error
= EHOSTUNREACH
;
707 * If the destination address is a multicast address and the outgoing
708 * interface for the address is specified by the caller, use it.
710 if (IN6_IS_ADDR_MULTICAST(dst
) && mopts
!= NULL
) {
712 if ((ifp
= ifp0
= mopts
->im6o_multicast_ifp
) != NULL
) {
714 goto done
; /* we do not need a route for multicast. */
721 * If the outgoing interface was not set via IPV6_BOUND_IF or
722 * IPV6_PKTINFO, use the scope ID in the destination address.
724 if (ip6_doscopedroute
&& ifscope
== IFSCOPE_NONE
)
725 ifscope
= dstsock
->sin6_scope_id
;
728 * Perform source interface selection; the source IPv6 address
729 * must belong to one of the addresses of the interface used
730 * by the route. For performance reasons, do this only if
731 * there is no route, or if the routing table has changed,
732 * or if we haven't done source interface selection on this
733 * route (for this PCB instance) before.
737 } else if (!ROUTE_UNUSABLE(ro
) && ro
->ro_srcia
!= NULL
&&
738 (ro
->ro_flags
& ROF_SRCIF_SELECTED
)) {
739 if (ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)
742 IFA_ADDREF(ifa
); /* for caller */
747 * Given the source IPv6 address, find a suitable source interface
748 * to use for transmission; if a scope ID has been specified,
749 * optimize the search by looking at the addresses only for that
750 * interface. This is still suboptimal, however, as we need to
751 * traverse the per-interface list.
753 if (ifscope
!= IFSCOPE_NONE
|| (ro
!= NULL
&& ro
->ro_rt
!= NULL
)) {
754 unsigned int scope
= ifscope
;
755 struct ifnet
*rt_ifp
;
757 rt_ifp
= (ro
->ro_rt
!= NULL
) ? ro
->ro_rt
->rt_ifp
: NULL
;
760 * If no scope is specified and the route is stale (pointing
761 * to a defunct interface) use the current primary interface;
762 * this happens when switching between interfaces configured
763 * with the same IPv6 address. Otherwise pick up the scope
764 * information from the route; the ULP may have looked up a
765 * correct route and we just need to verify it here and mark
766 * it with the ROF_SRCIF_SELECTED flag below.
768 if (scope
== IFSCOPE_NONE
) {
769 scope
= rt_ifp
->if_index
;
770 if (scope
!= get_primary_ifscope(AF_INET6
) &&
772 scope
= get_primary_ifscope(AF_INET6
);
775 ifa
= (struct ifaddr
*)
776 ifa_foraddr6_scoped(&srcsock
->sin6_addr
, scope
);
779 * If we are forwarding and proxying prefix(es), see if the
780 * source address is one of ours and is a proxied address;
783 if (ifa
== NULL
&& ip6_forwarding
&& nd6_prproxy
) {
784 ifa
= (struct ifaddr
*)
785 ifa_foraddr6(&srcsock
->sin6_addr
);
786 if (ifa
!= NULL
&& !(proxied_ifa
=
787 nd6_prproxy_ifaddr((struct in6_ifaddr
*)ifa
))) {
793 if (ip6_select_srcif_debug
&& ifa
!= NULL
) {
794 if (ro
->ro_rt
!= NULL
) {
795 printf("%s->%s ifscope %d->%d ifa_if %s "
796 "ro_if %s\n", s_src
, s_dst
, ifscope
,
797 scope
, if_name(ifa
->ifa_ifp
),
800 printf("%s->%s ifscope %d->%d ifa_if %s\n",
801 s_src
, s_dst
, ifscope
, scope
,
802 if_name(ifa
->ifa_ifp
));
808 * Slow path; search for an interface having the corresponding source
809 * IPv6 address if the scope was not specified by the caller, and:
811 * 1) There currently isn't any route, or,
812 * 2) The interface used by the route does not own that source
813 * IPv6 address; in this case, the route will get blown away
814 * and we'll do a more specific scoped search using the newly
817 if (ifa
== NULL
&& ifscope
== IFSCOPE_NONE
) {
818 struct ifaddr
*ifadst
;
820 /* Check if the destination address is one of ours */
821 ifadst
= (struct ifaddr
*)ifa_foraddr6(&dstsock
->sin6_addr
);
822 if (ifadst
!= NULL
) {
827 ifa
= (struct ifaddr
*)ifa_foraddr6(&srcsock
->sin6_addr
);
829 if (ip6_select_srcif_debug
&& ifa
!= NULL
) {
830 printf("%s->%s ifscope %d ifa_if %s\n",
831 s_src
, s_dst
, ifscope
, if_name(ifa
->ifa_ifp
));
837 if (ifa
!= NULL
&& !proxied_ifa
&& !local_dst
)
838 ifscope
= ifa
->ifa_ifp
->if_index
;
841 * If the next hop address for the packet is specified by the caller,
842 * use it as the gateway.
844 if (opts
!= NULL
&& opts
->ip6po_nexthop
!= NULL
) {
845 struct route_in6
*ron
;
847 sin6_next
= satosin6(opts
->ip6po_nexthop
);
849 /* at this moment, we only support AF_INET6 next hops */
850 if (sin6_next
->sin6_family
!= AF_INET6
) {
851 error
= EAFNOSUPPORT
; /* or should we proceed? */
856 * If the next hop is an IPv6 address, then the node identified
857 * by that address must be a neighbor of the sending host.
859 ron
= &opts
->ip6po_nextroute
;
860 if (ron
->ro_rt
!= NULL
)
862 if (ROUTE_UNUSABLE(ron
) || (ron
->ro_rt
!= NULL
&&
863 (!(ron
->ro_rt
->rt_flags
& RTF_LLINFO
) ||
864 (select_srcif
&& (ifa
== NULL
||
865 (ifa
->ifa_ifp
!= ron
->ro_rt
->rt_ifp
&& !proxied_ifa
))))) ||
866 !IN6_ARE_ADDR_EQUAL(&satosin6(&ron
->ro_dst
)->sin6_addr
,
867 &sin6_next
->sin6_addr
)) {
868 if (ron
->ro_rt
!= NULL
)
869 RT_UNLOCK(ron
->ro_rt
);
872 *satosin6(&ron
->ro_dst
) = *sin6_next
;
874 if (ron
->ro_rt
== NULL
) {
875 rtalloc_scoped((struct route
*)ron
, ifscope
);
876 if (ron
->ro_rt
!= NULL
)
878 if (ROUTE_UNUSABLE(ron
) ||
879 !(ron
->ro_rt
->rt_flags
& RTF_LLINFO
) ||
880 !IN6_ARE_ADDR_EQUAL(&satosin6(rt_key(ron
->ro_rt
))->
881 sin6_addr
, &sin6_next
->sin6_addr
)) {
882 if (ron
->ro_rt
!= NULL
)
883 RT_UNLOCK(ron
->ro_rt
);
886 error
= EHOSTUNREACH
;
891 ifp
= ifp0
= ron
->ro_rt
->rt_ifp
;
894 * When cloning is required, try to allocate a route to the
895 * destination so that the caller can store path MTU
900 /* Keep the route locked */
903 RT_UNLOCK(ron
->ro_rt
);
906 RT_UNLOCK(ron
->ro_rt
);
910 * Use a cached route if it exists and is valid, else try to allocate
911 * a new one. Note that we should check the address family of the
912 * cached destination, in case of sharing the cache with IPv4.
916 if (ro
->ro_rt
!= NULL
)
917 RT_LOCK_SPIN(ro
->ro_rt
);
918 if (ROUTE_UNUSABLE(ro
) || (ro
->ro_rt
!= NULL
&&
919 (satosin6(&ro
->ro_dst
)->sin6_family
!= AF_INET6
||
920 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
) ||
921 (select_srcif
&& (ifa
== NULL
||
922 (ifa
->ifa_ifp
!= ro
->ro_rt
->rt_ifp
&& !proxied_ifa
)))))) {
923 if (ro
->ro_rt
!= NULL
)
924 RT_UNLOCK(ro
->ro_rt
);
928 if (ro
->ro_rt
== NULL
) {
929 struct sockaddr_in6
*sa6
;
931 if (ro
->ro_rt
!= NULL
)
932 RT_UNLOCK(ro
->ro_rt
);
933 /* No route yet, so try to acquire one */
934 bzero(&ro
->ro_dst
, sizeof (struct sockaddr_in6
));
935 sa6
= (struct sockaddr_in6
*)&ro
->ro_dst
;
936 sa6
->sin6_family
= AF_INET6
;
937 sa6
->sin6_len
= sizeof (struct sockaddr_in6
);
938 sa6
->sin6_addr
= *dst
;
939 if (IN6_IS_ADDR_MULTICAST(dst
)) {
940 ro
->ro_rt
= rtalloc1_scoped(
941 &((struct route
*)ro
)->ro_dst
, 0, 0, ifscope
);
943 rtalloc_scoped((struct route
*)ro
, ifscope
);
945 if (ro
->ro_rt
!= NULL
)
946 RT_LOCK_SPIN(ro
->ro_rt
);
950 * Do not care about the result if we have the nexthop
951 * explicitly specified (in case we're asked to clone.)
953 if (opts
!= NULL
&& opts
->ip6po_nexthop
!= NULL
) {
954 if (ro
->ro_rt
!= NULL
)
955 RT_UNLOCK(ro
->ro_rt
);
959 if (ro
->ro_rt
!= NULL
) {
960 RT_LOCK_ASSERT_HELD(ro
->ro_rt
);
961 ifp
= ifp0
= ro
->ro_rt
->rt_ifp
;
963 error
= EHOSTUNREACH
;
969 boolean_t has_route
= (route
!= NULL
&& route
->ro_rt
!= NULL
);
970 boolean_t srcif_selected
= FALSE
;
973 RT_LOCK_ASSERT_HELD(route
->ro_rt
);
975 * If there is a non-loopback route with the wrong interface,
976 * or if there is no interface configured with such an address,
977 * blow it away. Except for local/loopback, we look for one
978 * with a matching interface scope/index.
980 if (has_route
&& (ifa
== NULL
||
981 (ifa
->ifa_ifp
!= ifp
&& ifp
!= lo_ifp
) ||
982 !(route
->ro_rt
->rt_flags
& RTF_UP
))) {
984 * If the destination address belongs to a proxied
985 * prefix, relax the requirement and allow the packet
986 * to come out of the proxy interface with the source
987 * address of the real interface.
989 if (ifa
!= NULL
&& proxied_ifa
&&
990 (route
->ro_rt
->rt_flags
& (RTF_UP
|RTF_PROXY
)) ==
991 (RTF_UP
|RTF_PROXY
)) {
992 srcif_selected
= TRUE
;
994 if (ip6_select_srcif_debug
) {
996 printf("%s->%s ifscope %d "
997 "ro_if %s != ifa_if %s "
998 "(cached route cleared)\n",
1000 ifscope
, if_name(ifp
),
1001 if_name(ifa
->ifa_ifp
));
1003 printf("%s->%s ifscope %d "
1004 "ro_if %s (no ifa_if "
1005 "found)\n", s_src
, s_dst
,
1006 ifscope
, if_name(ifp
));
1009 RT_UNLOCK(route
->ro_rt
);
1010 ROUTE_RELEASE(route
);
1011 error
= EHOSTUNREACH
;
1012 /* Undo the settings done above */
1014 ifp
= NULL
; /* ditch ifp; keep ifp0 */
1017 } else if (has_route
) {
1018 srcif_selected
= TRUE
;
1021 if (srcif_selected
) {
1023 if (ifa
!= route
->ro_srcia
||
1024 !(route
->ro_flags
& ROF_SRCIF_SELECTED
)) {
1025 RT_CONVERT_LOCK(route
->ro_rt
);
1027 IFA_ADDREF(ifa
); /* for route_in6 */
1028 if (route
->ro_srcia
!= NULL
)
1029 IFA_REMREF(route
->ro_srcia
);
1030 route
->ro_srcia
= ifa
;
1031 route
->ro_flags
|= ROF_SRCIF_SELECTED
;
1032 RT_GENID_SYNC(route
->ro_rt
);
1034 RT_UNLOCK(route
->ro_rt
);
1037 if (ro
->ro_rt
!= NULL
)
1038 RT_UNLOCK(ro
->ro_rt
);
1039 if (ifp
!= NULL
&& opts
!= NULL
&&
1040 opts
->ip6po_pktinfo
!= NULL
&&
1041 opts
->ip6po_pktinfo
->ipi6_ifindex
!= 0) {
1043 * Check if the outgoing interface conflicts with the
1044 * interface specified by ipi6_ifindex (if specified).
1045 * Note that loopback interface is always okay.
1046 * (this may happen when we are sending a packet to
1047 * one of our own addresses.)
1049 if (!(ifp
->if_flags
& IFF_LOOPBACK
) && ifp
->if_index
!=
1050 opts
->ip6po_pktinfo
->ipi6_ifindex
) {
1051 error
= EHOSTUNREACH
;
1059 * Check for interface restrictions.
1061 #define CHECK_RESTRICTIONS(_ip6oa, _ifp) \
1062 ((((_ip6oa)->ip6oa_flags & IP6OAF_NO_CELLULAR) && \
1063 IFNET_IS_CELLULAR(_ifp)) || \
1064 (((_ip6oa)->ip6oa_flags & IP6OAF_NO_EXPENSIVE) && \
1065 IFNET_IS_EXPENSIVE(_ifp)) || \
1066 (!((_ip6oa)->ip6oa_flags & IP6OAF_AWDL_UNRESTRICTED) && \
1067 IFNET_IS_AWDL_RESTRICTED(_ifp)))
1069 if (error
== 0 && ip6oa
!= NULL
&&
1070 ((ifp
&& CHECK_RESTRICTIONS(ip6oa
, ifp
)) ||
1071 (route
&& route
->ro_rt
&&
1072 CHECK_RESTRICTIONS(ip6oa
, route
->ro_rt
->rt_ifp
)))) {
1073 if (route
!= NULL
&& route
->ro_rt
!= NULL
) {
1074 ROUTE_RELEASE(route
);
1077 ifp
= NULL
; /* ditch ifp; keep ifp0 */
1078 error
= EHOSTUNREACH
;
1079 ip6oa
->ip6oa_retflags
|= IP6OARF_IFDENIED
;
1081 #undef CHECK_RESTRICTIONS
1084 * If the interface is disabled for IPv6, then ENETDOWN error.
1087 ifp
!= NULL
&& (ifp
->if_eflags
& IFEF_IPV6_DISABLED
)) {
1091 if (ifp
== NULL
&& (route
== NULL
|| route
->ro_rt
== NULL
)) {
1093 * This can happen if the caller did not pass a cached route
1094 * nor any other hints. We treat this case an error.
1096 error
= EHOSTUNREACH
;
1098 if (error
== EHOSTUNREACH
|| error
== ENETDOWN
)
1099 ip6stat
.ip6s_noroute
++;
1102 * We'll return ifp regardless of error, so pick it up from ifp0
1103 * in case it was nullified above. Caller is responsible for
1104 * releasing the ifp if it is non-NULL.
1107 if (retifp
!= NULL
) {
1109 ifnet_reference(ifp
); /* for caller */
1113 if (retsrcia
!= NULL
) {
1115 IFA_ADDREF(ifa
); /* for caller */
1116 *retsrcia
= (struct in6_ifaddr
*)ifa
;
1120 if (retrt
!= NULL
&& route
!= NULL
)
1121 *retrt
= route
->ro_rt
; /* ro_rt may be NULL */
1122 } else if (select_srcif
&& ip6_select_srcif_debug
) {
1123 printf("%s->%s ifscope %d ifa_if %s ro_if %s (error=%d)\n",
1124 s_src
, s_dst
, ifscope
,
1125 (ifa
!= NULL
) ? if_name(ifa
->ifa_ifp
) : "NONE",
1126 (ifp
!= NULL
) ? if_name(ifp
) : "NONE", error
);
1136 * Regardless of error, it will return an ifp with a reference held if the
1137 * caller provides a non-NULL retifp. The caller is responsible for checking
1138 * if the returned ifp is valid and release its reference at all times.
1141 in6_selectif(struct sockaddr_in6
*dstsock
, struct ip6_pktopts
*opts
,
1142 struct ip6_moptions
*mopts
, struct route_in6
*ro
,
1143 struct ip6_out_args
*ip6oa
, struct ifnet
**retifp
)
1146 struct route_in6 sro
;
1147 struct rtentry
*rt
= NULL
;
1150 bzero(&sro
, sizeof (sro
));
1154 if ((err
= selectroute(NULL
, dstsock
, opts
, mopts
, NULL
, ro
, retifp
,
1155 &rt
, 0, 1, ip6oa
)) != 0)
1159 * do not use a rejected or black hole route.
1160 * XXX: this check should be done in the L2 output routine.
1161 * However, if we skipped this check here, we'd see the following
1163 * - install a rejected route for a scoped address prefix
1165 * - send a packet to a destination that matches the scoped prefix,
1166 * with ambiguity about the scope zone.
1167 * - pick the outgoing interface from the route, and disambiguate the
1168 * scope zone with the interface.
1169 * - ip6_output() would try to get another route with the "new"
1170 * destination, which may be valid.
1171 * - we'd see no error on output.
1172 * Although this may not be very harmful, it should still be confusing.
1173 * We thus reject the case here.
1175 if (rt
&& (rt
->rt_flags
& (RTF_REJECT
| RTF_BLACKHOLE
))) {
1176 err
= ((rt
->rt_flags
& RTF_HOST
) ? EHOSTUNREACH
: ENETUNREACH
);
1181 * Adjust the "outgoing" interface. If we're going to loop the packet
1182 * back to ourselves, the ifp would be the loopback interface.
1183 * However, we'd rather know the interface associated to the
1184 * destination address (which should probably be one of our own
1187 if (rt
!= NULL
&& rt
->rt_ifa
!= NULL
&& rt
->rt_ifa
->ifa_ifp
!= NULL
&&
1189 ifnet_reference(rt
->rt_ifa
->ifa_ifp
);
1190 if (*retifp
!= NULL
)
1191 ifnet_release(*retifp
);
1192 *retifp
= rt
->rt_ifa
->ifa_ifp
;
1197 VERIFY(rt
== NULL
|| rt
== ro
->ro_rt
);
1202 * retifp might point to a valid ifp with a reference held;
1203 * caller is responsible for releasing it if non-NULL.
1209 * Regardless of error, it will return an ifp with a reference held if the
1210 * caller provides a non-NULL retifp. The caller is responsible for checking
1211 * if the returned ifp is valid and release its reference at all times.
1213 * clone - meaningful only for bsdi and freebsd
1216 in6_selectroute(struct sockaddr_in6
*srcsock
, struct sockaddr_in6
*dstsock
,
1217 struct ip6_pktopts
*opts
, struct ip6_moptions
*mopts
,
1218 struct in6_ifaddr
**retsrcia
, struct route_in6
*ro
, struct ifnet
**retifp
,
1219 struct rtentry
**retrt
, int clone
, struct ip6_out_args
*ip6oa
)
1222 return (selectroute(srcsock
, dstsock
, opts
, mopts
, retsrcia
, ro
, retifp
,
1223 retrt
, clone
, 0, ip6oa
));
1227 * Default hop limit selection. The precedence is as follows:
1228 * 1. Hoplimit value specified via ioctl.
1229 * 2. (If the outgoing interface is detected) the current
1230 * hop limit of the interface specified by router advertisement.
1231 * 3. The system default hoplimit.
1234 in6_selecthlim(struct in6pcb
*in6p
, struct ifnet
*ifp
)
1236 if (in6p
&& in6p
->in6p_hops
>= 0) {
1237 return (in6p
->in6p_hops
);
1239 lck_rw_lock_shared(nd_if_rwlock
);
1240 if (ifp
&& ifp
->if_index
< nd_ifinfo_indexlim
) {
1242 struct nd_ifinfo
*ndi
= &nd_ifinfo
[ifp
->if_index
];
1244 if (ndi
->initialized
) {
1245 /* access chlim without lock, for performance */
1248 chlim
= ip6_defhlim
;
1250 lck_rw_done(nd_if_rwlock
);
1253 lck_rw_done(nd_if_rwlock
);
1254 return (ip6_defhlim
);
1260 * XXX: this is borrowed from in6_pcbbind(). If possible, we should
1261 * share this function by all *bsd*...
1264 in6_pcbsetport(struct in6_addr
*laddr
, struct inpcb
*inp
, struct proc
*p
,
1267 #pragma unused(laddr)
1268 struct socket
*so
= inp
->inp_socket
;
1269 u_int16_t lport
= 0, first
, last
, *lastport
;
1270 int count
, error
= 0, wild
= 0;
1271 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1273 if (!locked
) { /* Make sure we don't run into a deadlock: 4052373 */
1274 if (!lck_rw_try_lock_exclusive(pcbinfo
->ipi_lock
)) {
1275 socket_unlock(inp
->inp_socket
, 0);
1276 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
1277 socket_lock(inp
->inp_socket
, 0);
1281 * Check if a local port was assigned to the inp while
1282 * this thread was waiting for the pcbinfo lock
1284 if (inp
->inp_lport
!= 0) {
1285 VERIFY(inp
->inp_flags2
& INP2_INHASHLIST
);
1286 lck_rw_done(pcbinfo
->ipi_lock
);
1289 * It is not an error if another thread allocated
1296 /* XXX: this is redundant when called from in6_pcbbind */
1297 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
1298 wild
= INPLOOKUP_WILDCARD
;
1300 if (inp
->inp_flags
& INP_HIGHPORT
) {
1301 first
= ipport_hifirstauto
; /* sysctl */
1302 last
= ipport_hilastauto
;
1303 lastport
= &pcbinfo
->ipi_lasthi
;
1304 } else if (inp
->inp_flags
& INP_LOWPORT
) {
1305 cred
= kauth_cred_proc_ref(p
);
1306 error
= priv_check_cred(cred
, PRIV_NETINET_RESERVEDPORT
, 0);
1307 kauth_cred_unref(&cred
);
1310 lck_rw_done(pcbinfo
->ipi_lock
);
1313 first
= ipport_lowfirstauto
; /* 1023 */
1314 last
= ipport_lowlastauto
; /* 600 */
1315 lastport
= &pcbinfo
->ipi_lastlow
;
1317 first
= ipport_firstauto
; /* sysctl */
1318 last
= ipport_lastauto
;
1319 lastport
= &pcbinfo
->ipi_lastport
;
1322 * Simple check to ensure all ports are not used up causing
1325 * We split the two cases (up and down) so that the direction
1326 * is not being tested on each round of the loop.
1332 count
= first
- last
;
1335 if (count
-- < 0) { /* completely used? */
1337 * Undo any address bind that may have
1340 inp
->in6p_laddr
= in6addr_any
;
1341 inp
->in6p_last_outifp
= NULL
;
1343 lck_rw_done(pcbinfo
->ipi_lock
);
1347 if (*lastport
> first
|| *lastport
< last
)
1349 lport
= htons(*lastport
);
1350 } while (in6_pcblookup_local(pcbinfo
, &inp
->in6p_laddr
, lport
,
1354 count
= last
- first
;
1357 if (count
-- < 0) { /* completely used? */
1359 * Undo any address bind that may have
1362 inp
->in6p_laddr
= in6addr_any
;
1363 inp
->in6p_last_outifp
= NULL
;
1365 lck_rw_done(pcbinfo
->ipi_lock
);
1369 if (*lastport
< first
|| *lastport
> last
)
1371 lport
= htons(*lastport
);
1372 } while (in6_pcblookup_local(pcbinfo
, &inp
->in6p_laddr
, lport
,
1376 inp
->inp_lport
= lport
;
1377 inp
->inp_flags
|= INP_ANONPORT
;
1379 if (in_pcbinshash(inp
, 1) != 0) {
1380 inp
->in6p_laddr
= in6addr_any
;
1381 inp
->in6p_last_outifp
= NULL
;
1384 inp
->inp_flags
&= ~INP_ANONPORT
;
1386 lck_rw_done(pcbinfo
->ipi_lock
);
1391 lck_rw_done(pcbinfo
->ipi_lock
);
1396 * The followings are implementation of the policy table using a
1397 * simple tail queue.
1398 * XXX such details should be hidden.
1399 * XXX implementation using binary tree should be more efficient.
1401 struct addrsel_policyent
{
1402 TAILQ_ENTRY(addrsel_policyent
) ape_entry
;
1403 struct in6_addrpolicy ape_policy
;
1406 TAILQ_HEAD(addrsel_policyhead
, addrsel_policyent
);
1408 struct addrsel_policyhead addrsel_policytab
;
1411 init_policy_queue(void)
1413 TAILQ_INIT(&addrsel_policytab
);
1417 addrsel_policy_init(void)
1420 * Default address selection policy based on RFC 6724.
1422 static const struct in6_addrpolicy defaddrsel
[] = {
1423 /* Loopback -- prefix=::1/128, precedence=50, label=0 */
1426 .sin6_family
= AF_INET6
,
1427 .sin6_addr
= IN6ADDR_LOOPBACK_INIT
,
1428 .sin6_len
= sizeof (struct sockaddr_in6
)
1431 .sin6_family
= AF_INET6
,
1432 .sin6_addr
= IN6MASK128
,
1433 .sin6_len
= sizeof (struct sockaddr_in6
)
1439 /* Unspecified -- prefix=::/0, precedence=40, label=1 */
1442 .sin6_family
= AF_INET6
,
1443 .sin6_addr
= IN6ADDR_ANY_INIT
,
1444 .sin6_len
= sizeof (struct sockaddr_in6
)
1447 .sin6_family
= AF_INET6
,
1448 .sin6_addr
= IN6MASK0
,
1449 .sin6_len
= sizeof (struct sockaddr_in6
)
1455 /* IPv4 Mapped -- prefix=::ffff:0:0/96, precedence=35, label=4 */
1458 .sin6_family
= AF_INET6
,
1459 .sin6_addr
= IN6ADDR_V4MAPPED_INIT
,
1460 .sin6_len
= sizeof (struct sockaddr_in6
)
1463 .sin6_family
= AF_INET6
,
1464 .sin6_addr
= IN6MASK96
,
1465 .sin6_len
= sizeof (struct sockaddr_in6
)
1471 /* 6to4 -- prefix=2002::/16, precedence=30, label=2 */
1474 .sin6_family
= AF_INET6
,
1475 .sin6_addr
= {{{ 0x20, 0x02 }}},
1476 .sin6_len
= sizeof (struct sockaddr_in6
)
1479 .sin6_family
= AF_INET6
,
1480 .sin6_addr
= IN6MASK16
,
1481 .sin6_len
= sizeof (struct sockaddr_in6
)
1487 /* Teredo -- prefix=2001::/32, precedence=5, label=5 */
1490 .sin6_family
= AF_INET6
,
1491 .sin6_addr
= {{{ 0x20, 0x01 }}},
1492 .sin6_len
= sizeof (struct sockaddr_in6
)
1495 .sin6_family
= AF_INET6
,
1496 .sin6_addr
= IN6MASK32
,
1497 .sin6_len
= sizeof (struct sockaddr_in6
)
1503 /* Unique Local (ULA) -- prefix=fc00::/7, precedence=3, label=13 */
1506 .sin6_family
= AF_INET6
,
1507 .sin6_addr
= {{{ 0xfc }}},
1508 .sin6_len
= sizeof (struct sockaddr_in6
)
1511 .sin6_family
= AF_INET6
,
1512 .sin6_addr
= IN6MASK7
,
1513 .sin6_len
= sizeof (struct sockaddr_in6
)
1519 /* IPv4 Compatible -- prefix=::/96, precedence=1, label=3 */
1522 .sin6_family
= AF_INET6
,
1523 .sin6_addr
= IN6ADDR_ANY_INIT
,
1524 .sin6_len
= sizeof (struct sockaddr_in6
)
1527 .sin6_family
= AF_INET6
,
1528 .sin6_addr
= IN6MASK96
,
1529 .sin6_len
= sizeof (struct sockaddr_in6
)
1535 /* Site-local (deprecated) -- prefix=fec0::/10, precedence=1, label=11 */
1538 .sin6_family
= AF_INET6
,
1539 .sin6_addr
= {{{ 0xfe, 0xc0 }}},
1540 .sin6_len
= sizeof (struct sockaddr_in6
)
1543 .sin6_family
= AF_INET6
,
1544 .sin6_addr
= IN6MASK16
,
1545 .sin6_len
= sizeof (struct sockaddr_in6
)
1551 /* 6bone (deprecated) -- prefix=3ffe::/16, precedence=1, label=12 */
1554 .sin6_family
= AF_INET6
,
1555 .sin6_addr
= {{{ 0x3f, 0xfe }}},
1556 .sin6_len
= sizeof (struct sockaddr_in6
)
1559 .sin6_family
= AF_INET6
,
1560 .sin6_addr
= IN6MASK16
,
1561 .sin6_len
= sizeof (struct sockaddr_in6
)
1569 init_policy_queue();
1571 /* initialize the "last resort" policy */
1572 bzero(&defaultaddrpolicy
, sizeof (defaultaddrpolicy
));
1573 defaultaddrpolicy
.label
= ADDR_LABEL_NOTAPP
;
1575 for (i
= 0; i
< sizeof (defaddrsel
) / sizeof (defaddrsel
[0]); i
++)
1576 add_addrsel_policyent(&defaddrsel
[i
]);
1580 struct in6_addrpolicy
*
1581 in6_addrsel_lookup_policy(struct sockaddr_in6
*key
)
1583 struct in6_addrpolicy
*match
= NULL
;
1586 match
= match_addrsel_policy(key
);
1589 match
= &defaultaddrpolicy
;
1597 static struct in6_addrpolicy
*
1598 match_addrsel_policy(struct sockaddr_in6
*key
)
1600 struct addrsel_policyent
*pent
;
1601 struct in6_addrpolicy
*bestpol
= NULL
, *pol
;
1602 int matchlen
, bestmatchlen
= -1;
1603 u_char
*mp
, *ep
, *k
, *p
, m
;
1605 TAILQ_FOREACH(pent
, &addrsel_policytab
, ape_entry
) {
1608 pol
= &pent
->ape_policy
;
1609 mp
= (u_char
*)&pol
->addrmask
.sin6_addr
;
1610 ep
= mp
+ 16; /* XXX: scope field? */
1611 k
= (u_char
*)&key
->sin6_addr
;
1612 p
= (u_char
*)&pol
->addr
.sin6_addr
;
1613 for (; mp
< ep
&& *mp
; mp
++, k
++, p
++) {
1616 goto next
; /* not match */
1617 if (m
== 0xff) /* short cut for a typical case */
1627 /* matched. check if this is better than the current best. */
1628 if (bestpol
== NULL
||
1629 matchlen
> bestmatchlen
) {
1631 bestmatchlen
= matchlen
;
1642 add_addrsel_policyent(const struct in6_addrpolicy
*newpolicy
)
1644 struct addrsel_policyent
*new, *pol
;
1646 MALLOC(new, struct addrsel_policyent
*, sizeof (*new), M_IFADDR
,
1651 /* duplication check */
1652 TAILQ_FOREACH(pol
, &addrsel_policytab
, ape_entry
) {
1653 if (IN6_ARE_ADDR_EQUAL(&newpolicy
->addr
.sin6_addr
,
1654 &pol
->ape_policy
.addr
.sin6_addr
) &&
1655 IN6_ARE_ADDR_EQUAL(&newpolicy
->addrmask
.sin6_addr
,
1656 &pol
->ape_policy
.addrmask
.sin6_addr
)) {
1658 FREE(new, M_IFADDR
);
1659 return (EEXIST
); /* or override it? */
1663 bzero(new, sizeof (*new));
1665 /* XXX: should validate entry */
1666 new->ape_policy
= *newpolicy
;
1668 TAILQ_INSERT_TAIL(&addrsel_policytab
, new, ape_entry
);
1673 #ifdef ENABLE_ADDRSEL
1675 delete_addrsel_policyent(const struct in6_addrpolicy
*key
)
1677 struct addrsel_policyent
*pol
;
1682 /* search for the entry in the table */
1683 TAILQ_FOREACH(pol
, &addrsel_policytab
, ape_entry
) {
1684 if (IN6_ARE_ADDR_EQUAL(&key
->addr
.sin6_addr
,
1685 &pol
->ape_policy
.addr
.sin6_addr
) &&
1686 IN6_ARE_ADDR_EQUAL(&key
->addrmask
.sin6_addr
,
1687 &pol
->ape_policy
.addrmask
.sin6_addr
)) {
1696 TAILQ_REMOVE(&addrsel_policytab
, pol
, ape_entry
);
1697 FREE(pol
, M_IFADDR
);
1703 #endif /* ENABLE_ADDRSEL */
1706 walk_addrsel_policy(int (*callback
)(const struct in6_addrpolicy
*, void *),
1709 struct addrsel_policyent
*pol
;
1713 TAILQ_FOREACH(pol
, &addrsel_policytab
, ape_entry
) {
1714 if ((error
= (*callback
)(&pol
->ape_policy
, w
)) != 0) {
1723 * Subroutines to manage the address selection policy table via sysctl.
1726 struct sysctl_req
*w_req
;
1731 dump_addrsel_policyent(const struct in6_addrpolicy
*pol
, void *arg
)
1734 struct walkarg
*w
= arg
;
1736 error
= SYSCTL_OUT(w
->w_req
, pol
, sizeof (*pol
));
1742 in6_src_sysctl SYSCTL_HANDLER_ARGS
1744 #pragma unused(oidp, arg1, arg2)
1749 bzero(&w
, sizeof (w
));
1752 return (walk_addrsel_policy(dump_addrsel_policyent
, &w
));
1756 SYSCTL_NODE(_net_inet6_ip6
, IPV6CTL_ADDRCTLPOLICY
, addrctlpolicy
,
1757 CTLFLAG_RD
| CTLFLAG_LOCKED
, in6_src_sysctl
, "");
1759 in6_src_ioctl(u_long cmd
, caddr_t data
)
1762 struct in6_addrpolicy ent0
;
1764 if (cmd
!= SIOCAADDRCTL_POLICY
&& cmd
!= SIOCDADDRCTL_POLICY
)
1765 return (EOPNOTSUPP
); /* check for safety */
1767 bcopy(data
, &ent0
, sizeof (ent0
));
1769 if (ent0
.label
== ADDR_LABEL_NOTAPP
)
1771 /* check if the prefix mask is consecutive. */
1772 if (in6_mask2len(&ent0
.addrmask
.sin6_addr
, NULL
) < 0)
1774 /* clear trailing garbages (if any) of the prefix address. */
1775 for (i
= 0; i
< 4; i
++) {
1776 ent0
.addr
.sin6_addr
.s6_addr32
[i
] &=
1777 ent0
.addrmask
.sin6_addr
.s6_addr32
[i
];
1782 case SIOCAADDRCTL_POLICY
:
1783 #ifdef ENABLE_ADDRSEL
1784 return (add_addrsel_policyent(&ent0
));
1788 case SIOCDADDRCTL_POLICY
:
1789 #ifdef ENABLE_ADDRSEL
1790 return (delete_addrsel_policyent(&ent0
));
1796 return (0); /* XXX: compromise compilers */
1800 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
1801 * If the address scope of is link-local, embed the interface index in the
1802 * address. The routine determines our precedence
1803 * between advanced API scope/interface specification and basic API
1806 * this function should be nuked in the future, when we get rid of
1807 * embedded scopeid thing.
1809 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
1810 * there can be multiple interfaces that belong to a particular scope zone
1811 * (in specification, we have 1:N mapping between a scope zone and interfaces).
1812 * we may want to change the function to return something other than ifp.
1815 in6_embedscope(struct in6_addr
*in6
, const struct sockaddr_in6
*sin6
,
1816 struct in6pcb
*in6p
, struct ifnet
**ifpp
, struct ip6_pktopts
*opt
)
1818 struct ifnet
*ifp
= NULL
;
1820 struct ip6_pktopts
*optp
= NULL
;
1822 *in6
= sin6
->sin6_addr
;
1823 scopeid
= sin6
->sin6_scope_id
;
1828 * don't try to read sin6->sin6_addr beyond here, since the caller may
1829 * ask us to overwrite existing sockaddr_in6
1832 #ifdef ENABLE_DEFAULT_SCOPE
1834 scopeid
= scope6_addr2default(in6
);
1837 if (IN6_IS_SCOPE_LINKLOCAL(in6
) || IN6_IS_ADDR_MC_INTFACELOCAL(in6
)) {
1838 struct in6_pktinfo
*pi
;
1839 struct ifnet
*im6o_multicast_ifp
= NULL
;
1841 if (in6p
!= NULL
&& IN6_IS_ADDR_MULTICAST(in6
) &&
1842 in6p
->in6p_moptions
!= NULL
) {
1843 IM6O_LOCK(in6p
->in6p_moptions
);
1844 im6o_multicast_ifp
=
1845 in6p
->in6p_moptions
->im6o_multicast_ifp
;
1846 IM6O_UNLOCK(in6p
->in6p_moptions
);
1851 else if (in6p
!= NULL
)
1852 optp
= in6p
->in6p_outputopts
;
1854 * KAME assumption: link id == interface id
1856 if (in6p
!= NULL
&& optp
!= NULL
&&
1857 (pi
= optp
->ip6po_pktinfo
) != NULL
&&
1858 pi
->ipi6_ifindex
!= 0) {
1859 /* ifp is needed here if only we're returning it */
1861 ifnet_head_lock_shared();
1862 ifp
= ifindex2ifnet
[pi
->ipi6_ifindex
];
1865 in6
->s6_addr16
[1] = htons(pi
->ipi6_ifindex
);
1866 } else if (in6p
!= NULL
&& IN6_IS_ADDR_MULTICAST(in6
) &&
1867 in6p
->in6p_moptions
!= NULL
&& im6o_multicast_ifp
!= NULL
) {
1868 ifp
= im6o_multicast_ifp
;
1869 in6
->s6_addr16
[1] = htons(ifp
->if_index
);
1870 } else if (scopeid
!= 0) {
1872 * Since scopeid is unsigned, we only have to check it
1873 * against if_index (ifnet_head_lock not needed since
1874 * if_index is an ever-increasing integer.)
1876 if (if_index
< scopeid
)
1877 return (ENXIO
); /* XXX EINVAL? */
1879 /* ifp is needed here only if we're returning it */
1881 ifnet_head_lock_shared();
1882 ifp
= ifindex2ifnet
[scopeid
];
1885 /* XXX assignment to 16bit from 32bit variable */
1886 in6
->s6_addr16
[1] = htons(scopeid
& 0xffff);
1891 ifnet_reference(ifp
); /* for caller */
1900 * generate standard sockaddr_in6 from embedded form.
1901 * touches sin6_addr and sin6_scope_id only.
1903 * this function should be nuked in the future, when we get rid of
1904 * embedded scopeid thing.
1908 struct sockaddr_in6
*sin6
,
1909 const struct in6_addr
*in6
,
1914 sin6
->sin6_addr
= *in6
;
1917 * don't try to read *in6 beyond here, since the caller may
1918 * ask us to overwrite existing sockaddr_in6
1921 sin6
->sin6_scope_id
= 0;
1922 if (IN6_IS_SCOPE_LINKLOCAL(in6
) || IN6_IS_ADDR_MC_INTFACELOCAL(in6
)) {
1924 * KAME assumption: link id == interface id
1926 scopeid
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
1931 * Since scopeid is unsigned, we only have to check it
1934 if (if_index
< scopeid
)
1936 if (ifp
&& ifp
->if_index
!= scopeid
)
1938 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1939 sin6
->sin6_scope_id
= scopeid
;