2 * Copyright (c) 2000-2018 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 static int ip6_select_srcaddr_debug
= 0;
137 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, select_srcaddr_debug
,
138 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_select_srcaddr_debug
, 0,
139 "log source address selection debug info");
141 static int ip6_select_src_expensive_secondary_if
= 0;
142 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, select_src_expensive_secondary_if
,
143 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_select_src_expensive_secondary_if
, 0,
144 "allow source interface selection to use expensive secondaries");
146 static int ip6_select_src_strong_end
= 1;
147 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, select_src_strong_end
,
148 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_select_src_strong_end
, 0,
149 "limit source address selection to outgoing interface");
151 #define ADDR_LABEL_NOTAPP (-1)
152 struct in6_addrpolicy defaultaddrpolicy
;
154 int ip6_prefer_tempaddr
= 1;
155 #ifdef ENABLE_ADDRSEL
156 extern lck_mtx_t
*addrsel_mutex
;
157 #define ADDRSEL_LOCK() lck_mtx_lock(addrsel_mutex)
158 #define ADDRSEL_UNLOCK() lck_mtx_unlock(addrsel_mutex)
160 #define ADDRSEL_LOCK()
161 #define ADDRSEL_UNLOCK()
164 static int selectroute(struct sockaddr_in6
*, struct sockaddr_in6
*,
165 struct ip6_pktopts
*, struct ip6_moptions
*, struct in6_ifaddr
**,
166 struct route_in6
*, struct ifnet
**, struct rtentry
**, int, int,
167 struct ip6_out_args
*ip6oa
);
168 static int in6_selectif(struct sockaddr_in6
*, struct ip6_pktopts
*,
169 struct ip6_moptions
*, struct route_in6
*ro
,
170 struct ip6_out_args
*, struct ifnet
**);
171 static void init_policy_queue(void);
172 static int add_addrsel_policyent(const struct in6_addrpolicy
*);
173 #ifdef ENABLE_ADDRSEL
174 static int delete_addrsel_policyent(const struct in6_addrpolicy
*);
176 static int walk_addrsel_policy(int (*)(const struct in6_addrpolicy
*, void *),
178 static int dump_addrsel_policyent(const struct in6_addrpolicy
*, void *);
179 static struct in6_addrpolicy
*match_addrsel_policy(struct sockaddr_in6
*);
180 void addrsel_policy_init(void);
182 #define SASEL_DO_DBG(inp) \
183 (ip6_select_srcaddr_debug && (inp) != NULL && \
184 (inp)->inp_socket != NULL && \
185 ((inp)->inp_socket->so_options & SO_DEBUG))
187 #define SASEL_LOG(fmt, ...) \
190 printf("%s:%d " fmt "\n",\
191 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
195 * Return an IPv6 address, which is the most appropriate for a given
196 * destination and user specified options.
197 * If necessary, this function lookups the routing table and returns
198 * an entry to the caller for later use.
200 #define REPLACE(r) do {\
201 SASEL_LOG("REPLACE r %d ia %s ifp1 %s\n", \
202 (r), s_src, ifp1->if_xname); \
207 #define NEXTSRC(r) do {\
208 SASEL_LOG("NEXTSRC r %d ia %s ifp1 %s\n", \
209 (r), s_src, ifp1->if_xname); \
210 goto next; /* XXX: we can't use 'continue' here */ \
213 #define BREAK(r) do { \
214 SASEL_LOG("BREAK r %d ia %s ifp1 %s\n", \
215 (r), s_src, ifp1->if_xname); \
217 goto out; /* XXX: we can't use 'break' here */ \
222 in6_selectsrc_core_ifa(struct sockaddr_in6
*addr
, struct ifnet
*ifp
, int srcsel_debug
) {
224 struct ifnet
*src_ifp
= NULL
;
225 struct in6_addr src_storage
= {};
226 struct in6_addr
*in6
= NULL
;
227 struct ifaddr
*ifa
= NULL
;
229 if((in6
= in6_selectsrc_core(addr
,
230 (ip6_prefer_tempaddr
? IPV6_SRCSEL_HINT_PREFER_TMPADDR
: 0),
231 ifp
, 0, &src_storage
, &src_ifp
, &err
, &ifa
)) == NULL
) {
234 VERIFY(src_ifp
== NULL
);
242 if (src_ifp
!= ifp
) {
253 ifnet_lock_shared(ifp
);
254 if ((ifa
->ifa_debug
& IFD_DETACHING
) != 0) {
256 ifnet_lock_done(ifp
);
263 ifnet_lock_done(ifp
);
266 SASEL_LOG("Returned with error: %d", err
);
268 ifnet_release(src_ifp
);
273 in6_selectsrc_core(struct sockaddr_in6
*dstsock
, uint32_t hint_mask
,
274 struct ifnet
*ifp
, int srcsel_debug
, struct in6_addr
*src_storage
,
275 struct ifnet
**sifp
, int *errorp
, struct ifaddr
**ifapp
)
278 int bestrule
= IP6S_SRCRULE_0
;
279 struct in6_addrpolicy
*dst_policy
= NULL
, *best_policy
= NULL
;
281 struct in6_ifaddr
*ia
= NULL
, *ia_best
= NULL
;
282 char s_src
[MAX_IPv6_STR_LEN
] = {0};
283 char s_dst
[MAX_IPv6_STR_LEN
] = {0};
284 const struct in6_addr
*tmp
= NULL
;
285 int dst_scope
= -1, best_scope
= -1, best_matchlen
= -1;
286 uint64_t secs
= net_uptime();
287 VERIFY(dstsock
!= NULL
);
288 VERIFY(src_storage
!= NULL
);
297 dst
= dstsock
->sin6_addr
; /* make a copy for local operation */
300 (void) inet_ntop(AF_INET6
, &dst
, s_dst
, sizeof (s_src
));
303 (void) inet_ntop(AF_INET6
, tmp
, s_src
, sizeof (s_src
));
304 printf("%s out src %s dst %s ifp %s\n",
305 __func__
, s_src
, s_dst
, ifp
->if_xname
);
308 *errorp
= in6_setscope(&dst
, ifp
, &odstzone
);
314 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
315 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
316 int new_scope
= -1, new_matchlen
= -1;
317 struct in6_addrpolicy
*new_policy
= NULL
;
318 u_int32_t srczone
= 0, osrczone
, dstzone
;
320 struct ifnet
*ifp1
= ia
->ia_ifp
;
324 (void) inet_ntop(AF_INET6
, &ia
->ia_addr
.sin6_addr
,
325 s_src
, sizeof (s_src
));
327 IFA_LOCK(&ia
->ia_ifa
);
330 * Simply skip addresses reserved for CLAT46
332 if (ia
->ia6_flags
& IN6_IFF_CLAT46
) {
333 SASEL_LOG("NEXT ia %s address on ifp1 %s skipped as it is "
334 "reserved for CLAT46", s_src
, ifp1
->if_xname
);
339 * XXX By default we are strong end system and will
340 * limit candidate set of source address to the ones
341 * configured on the outgoing interface.
343 if (ip6_select_src_strong_end
&&
345 SASEL_LOG("NEXT ia %s ifp1 %s address is not on outgoing "
346 "interface \n", s_src
, ifp1
->if_xname
);
351 * We'll never take an address that breaks the scope zone
352 * of the destination. We also skip an address if its zone
353 * does not contain the outgoing interface.
354 * XXX: we should probably use sin6_scope_id here.
356 if (in6_setscope(&dst
, ifp1
, &dstzone
) ||
357 odstzone
!= dstzone
) {
358 SASEL_LOG("NEXT ia %s ifp1 %s odstzone %d != dstzone %d\n",
359 s_src
, ifp1
->if_xname
, odstzone
, dstzone
);
362 src
= ia
->ia_addr
.sin6_addr
;
363 if (in6_setscope(&src
, ifp
, &osrczone
) ||
364 in6_setscope(&src
, ifp1
, &srczone
) ||
365 osrczone
!= srczone
) {
366 SASEL_LOG("NEXT ia %s ifp1 %s osrczone %d != srczone %d\n",
367 s_src
, ifp1
->if_xname
, osrczone
, srczone
);
370 /* avoid unusable addresses */
372 (IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
| IN6_IFF_DETACHED
))) {
373 SASEL_LOG("NEXT ia %s ifp1 %s ia6_flags 0x%x\n",
374 s_src
, ifp1
->if_xname
, ia
->ia6_flags
);
377 if (!ip6_use_deprecated
&& IFA6_IS_DEPRECATED(ia
, secs
)) {
378 SASEL_LOG("NEXT ia %s ifp1 %s IFA6_IS_DEPRECATED\n",
379 s_src
, ifp1
->if_xname
);
382 if (!nd6_optimistic_dad
&&
383 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) != 0) {
384 SASEL_LOG("NEXT ia %s ifp1 %s IN6_IFF_OPTIMISTIC\n",
385 s_src
, ifp1
->if_xname
);
388 /* Rule 1: Prefer same address */
389 if (IN6_ARE_ADDR_EQUAL(&dst
, &ia
->ia_addr
.sin6_addr
))
390 BREAK(IP6S_SRCRULE_1
); /* there should be no better candidate */
393 REPLACE(IP6S_SRCRULE_0
);
395 /* Rule 2: Prefer appropriate scope */
397 dst_scope
= in6_addrscope(&dst
);
398 new_scope
= in6_addrscope(&ia
->ia_addr
.sin6_addr
);
399 if (IN6_ARE_SCOPE_CMP(best_scope
, new_scope
) < 0) {
400 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0)
401 REPLACE(IP6S_SRCRULE_2
);
402 NEXTSRC(IP6S_SRCRULE_2
);
403 } else if (IN6_ARE_SCOPE_CMP(new_scope
, best_scope
) < 0) {
404 if (IN6_ARE_SCOPE_CMP(new_scope
, dst_scope
) < 0)
405 NEXTSRC(IP6S_SRCRULE_2
);
406 REPLACE(IP6S_SRCRULE_2
);
410 * Rule 3: Avoid deprecated addresses. Note that the case of
411 * !ip6_use_deprecated is already rejected above.
413 if (!IFA6_IS_DEPRECATED(ia_best
, secs
) &&
414 IFA6_IS_DEPRECATED(ia
, secs
))
415 NEXTSRC(IP6S_SRCRULE_3
);
416 if (IFA6_IS_DEPRECATED(ia_best
, secs
) &&
417 !IFA6_IS_DEPRECATED(ia
, secs
))
418 REPLACE(IP6S_SRCRULE_3
);
421 * RFC 4429 says that optimistic addresses are equivalent to
422 * deprecated addresses, so avoid them here.
424 if ((ia_best
->ia6_flags
& IN6_IFF_OPTIMISTIC
) == 0 &&
425 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) != 0)
426 NEXTSRC(IP6S_SRCRULE_3
);
427 if ((ia_best
->ia6_flags
& IN6_IFF_OPTIMISTIC
) != 0 &&
428 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) == 0)
429 REPLACE(IP6S_SRCRULE_3
);
431 /* Rule 4: Prefer home addresses */
433 * XXX: This is a TODO. We should probably merge the MIP6
437 /* Rule 5: Prefer outgoing interface */
439 * XXX By default we are strong end with source address
440 * selection. That means all address selection candidate
441 * addresses will be the ones hosted on the outgoing interface
442 * making the following check redundant.
444 if (ip6_select_src_strong_end
== 0) {
445 if (ia_best
->ia_ifp
== ifp
&& ia
->ia_ifp
!= ifp
)
446 NEXTSRC(IP6S_SRCRULE_5
);
447 if (ia_best
->ia_ifp
!= ifp
&& ia
->ia_ifp
== ifp
)
448 REPLACE(IP6S_SRCRULE_5
);
452 * Rule 6: Prefer matching label
453 * Note that best_policy should be non-NULL here.
455 if (dst_policy
== NULL
)
456 dst_policy
= in6_addrsel_lookup_policy(dstsock
);
457 if (dst_policy
->label
!= ADDR_LABEL_NOTAPP
) {
458 new_policy
= in6_addrsel_lookup_policy(&ia
->ia_addr
);
459 if (dst_policy
->label
== best_policy
->label
&&
460 dst_policy
->label
!= new_policy
->label
)
461 NEXTSRC(IP6S_SRCRULE_6
);
462 if (dst_policy
->label
!= best_policy
->label
&&
463 dst_policy
->label
== new_policy
->label
)
464 REPLACE(IP6S_SRCRULE_6
);
468 * Rule 7: Prefer temporary addresses.
469 * We allow users to reverse the logic by configuring
470 * a sysctl variable, so that transparency conscious users can
471 * always prefer stable addresses.
473 if (!(ia_best
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
474 (ia
->ia6_flags
& IN6_IFF_TEMPORARY
)) {
475 if (hint_mask
& IPV6_SRCSEL_HINT_PREFER_TMPADDR
)
476 REPLACE(IP6S_SRCRULE_7
);
478 NEXTSRC(IP6S_SRCRULE_7
);
480 if ((ia_best
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
481 !(ia
->ia6_flags
& IN6_IFF_TEMPORARY
)) {
482 if (hint_mask
& IPV6_SRCSEL_HINT_PREFER_TMPADDR
)
483 NEXTSRC(IP6S_SRCRULE_7
);
485 REPLACE(IP6S_SRCRULE_7
);
489 * Rule 7x: prefer addresses on alive interfaces.
490 * This is a KAME specific rule.
492 if ((ia_best
->ia_ifp
->if_flags
& IFF_UP
) &&
493 !(ia
->ia_ifp
->if_flags
& IFF_UP
))
494 NEXTSRC(IP6S_SRCRULE_7x
);
495 if (!(ia_best
->ia_ifp
->if_flags
& IFF_UP
) &&
496 (ia
->ia_ifp
->if_flags
& IFF_UP
))
497 REPLACE(IP6S_SRCRULE_7x
);
500 * Rule 8: Use longest matching prefix.
502 new_matchlen
= in6_matchlen(&ia
->ia_addr
.sin6_addr
, &dst
);
503 if (best_matchlen
< new_matchlen
)
504 REPLACE(IP6S_SRCRULE_8
);
505 if (new_matchlen
< best_matchlen
)
506 NEXTSRC(IP6S_SRCRULE_8
);
509 * Last resort: just keep the current candidate.
510 * Or, do we need more rules?
512 if (ifp1
!= ifp
&& (ifp1
->if_eflags
& IFEF_EXPENSIVE
) &&
513 ip6_select_src_expensive_secondary_if
== 0) {
514 SASEL_LOG("NEXT ia %s ifp1 %s IFEF_EXPENSIVE\n",
515 s_src
, ifp1
->if_xname
);
516 ip6stat
.ip6s_sources_skip_expensive_secondary_if
++;
519 SASEL_LOG("NEXT ia %s ifp1 %s last resort\n",
520 s_src
, ifp1
->if_xname
);
521 IFA_UNLOCK(&ia
->ia_ifa
);
526 * Ignore addresses on secondary interfaces that are marked
529 if (ifp1
!= ifp
&& (ifp1
->if_eflags
& IFEF_EXPENSIVE
) &&
530 ip6_select_src_expensive_secondary_if
== 0) {
531 SASEL_LOG("NEXT ia %s ifp1 %s IFEF_EXPENSIVE\n",
532 s_src
, ifp1
->if_xname
);
533 ip6stat
.ip6s_sources_skip_expensive_secondary_if
++;
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 SASEL_LOG("NEXT ia %s ifp1 %s best_scope %d new_scope %d dst_scope %d\n",
544 s_src
, ifp1
->if_xname
, best_scope
, new_scope
, dst_scope
);
545 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for ia_best */
546 IFA_UNLOCK(&ia
->ia_ifa
);
548 IFA_REMREF(&ia_best
->ia_ifa
);
553 IFA_UNLOCK(&ia
->ia_ifa
);
557 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for ia_best */
558 IFA_UNLOCK(&ia
->ia_ifa
);
560 IFA_REMREF(&ia_best
->ia_ifa
);
565 lck_rw_done(&in6_ifaddr_rwlock
);
567 if ((ia
= ia_best
) == NULL
) {
569 *errorp
= EADDRNOTAVAIL
;
575 *sifp
= ia
->ia_ifa
.ifa_ifp
;
576 ifnet_reference(*sifp
);
579 IFA_LOCK_SPIN(&ia
->ia_ifa
);
580 if (bestrule
< IP6S_SRCRULE_COUNT
)
581 ip6stat
.ip6s_sources_rule
[bestrule
]++;
582 *src_storage
= satosin6(&ia
->ia_addr
)->sin6_addr
;
583 IFA_UNLOCK(&ia
->ia_ifa
);
586 *ifapp
= &ia
->ia_ifa
;
588 IFA_REMREF(&ia
->ia_ifa
);
592 (void) inet_ntop(AF_INET6
, &dst
, s_dst
, sizeof (s_src
));
594 tmp
= (src_storage
!= NULL
) ? src_storage
: &in6addr_any
;
595 (void) inet_ntop(AF_INET6
, tmp
, s_src
, sizeof (s_src
));
597 printf("%s out src %s dst %s dst_scope %d best_scope %d\n",
598 __func__
, s_src
, s_dst
, dst_scope
, best_scope
);
601 return (src_storage
);
605 * Regardless of error, it will return an ifp with a reference held if the
606 * caller provides a non-NULL ifpp. The caller is responsible for checking
607 * if the returned ifp is valid and release its reference at all times.
610 in6_selectsrc(struct sockaddr_in6
*dstsock
, struct ip6_pktopts
*opts
,
611 struct inpcb
*inp
, struct route_in6
*ro
,
612 struct ifnet
**ifpp
, struct in6_addr
*src_storage
, unsigned int ifscope
,
615 struct ifnet
*ifp
= NULL
;
616 struct in6_pktinfo
*pi
= NULL
;
617 struct ip6_moptions
*mopts
;
618 struct ip6_out_args ip6oa
;
619 boolean_t inp_debug
= FALSE
;
620 uint32_t hint_mask
= 0;
621 int prefer_tempaddr
= 0;
622 struct ifnet
*sifp
= NULL
;
624 bzero(&ip6oa
, sizeof(ip6oa
));
625 ip6oa
.ip6oa_boundif
= ifscope
;
626 ip6oa
.ip6oa_flags
= IP6OAF_SELECT_SRCIF
;
627 ip6oa
.ip6oa_sotc
= SO_TC_UNSPEC
;
628 ip6oa
.ip6oa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
635 inp_debug
= SASEL_DO_DBG(inp
);
636 mopts
= inp
->in6p_moptions
;
637 if (INP_NO_CELLULAR(inp
))
638 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
639 if (INP_NO_EXPENSIVE(inp
))
640 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
641 if (INP_AWDL_UNRESTRICTED(inp
))
642 ip6oa
.ip6oa_flags
|= IP6OAF_AWDL_UNRESTRICTED
;
643 if (INP_INTCOPROC_ALLOWED(inp
))
644 ip6oa
.ip6oa_flags
|= IP6OAF_INTCOPROC_ALLOWED
;
647 /* Allow the kernel to retransmit packets. */
648 ip6oa
.ip6oa_flags
|= IP6OAF_INTCOPROC_ALLOWED
|
649 IP6OAF_AWDL_UNRESTRICTED
;
652 if (ip6oa
.ip6oa_boundif
!= IFSCOPE_NONE
)
653 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
656 * If the source address is explicitly specified by the caller,
657 * check if the requested source address is indeed a unicast address
658 * assigned to the node, and can be used as the packet's source
659 * address. If everything is okay, use the address as source.
661 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
662 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
)) {
663 struct sockaddr_in6 srcsock
;
664 struct in6_ifaddr
*ia6
;
666 /* get the outgoing interface */
667 if ((*errorp
= in6_selectif(dstsock
, opts
, mopts
, ro
, &ip6oa
,
674 * determine the appropriate zone id of the source based on
675 * the zone of the destination and the outgoing interface.
676 * If the specified address is ambiguous wrt the scope zone,
677 * the interface must be specified; otherwise, ifa_ifwithaddr()
678 * will fail matching the address.
680 bzero(&srcsock
, sizeof (srcsock
));
681 srcsock
.sin6_family
= AF_INET6
;
682 srcsock
.sin6_len
= sizeof (srcsock
);
683 srcsock
.sin6_addr
= pi
->ipi6_addr
;
685 *errorp
= in6_setscope(&srcsock
.sin6_addr
, ifp
, NULL
);
691 ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr((struct sockaddr
*)
694 *errorp
= EADDRNOTAVAIL
;
698 IFA_LOCK_SPIN(&ia6
->ia_ifa
);
699 if ((ia6
->ia6_flags
& (IN6_IFF_ANYCAST
| IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) ||
700 (inp
&& inp_restricted_send(inp
, ia6
->ia_ifa
.ifa_ifp
))) {
701 IFA_UNLOCK(&ia6
->ia_ifa
);
702 IFA_REMREF(&ia6
->ia_ifa
);
703 *errorp
= EHOSTUNREACH
;
708 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
709 IFA_UNLOCK(&ia6
->ia_ifa
);
710 IFA_REMREF(&ia6
->ia_ifa
);
715 * Otherwise, if the socket has already bound the source, just use it.
717 if (inp
!= NULL
&& !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
718 src_storage
= &inp
->in6p_laddr
;
723 * If the address is not specified, choose the best one based on
724 * the outgoing interface and the destination address.
726 /* get the outgoing interface */
727 if ((*errorp
= in6_selectif(dstsock
, opts
, mopts
, ro
, &ip6oa
,
736 opts
->ip6po_prefer_tempaddr
== IP6PO_TEMPADDR_SYSTEM
) {
737 prefer_tempaddr
= ip6_prefer_tempaddr
;
738 } else if (opts
->ip6po_prefer_tempaddr
== IP6PO_TEMPADDR_NOTPREFER
) {
744 hint_mask
|= IPV6_SRCSEL_HINT_PREFER_TMPADDR
;
746 if (in6_selectsrc_core(dstsock
, hint_mask
, ifp
, inp_debug
, src_storage
,
747 &sifp
, errorp
, NULL
) == NULL
) {
752 VERIFY(sifp
!= NULL
);
754 if (inp
&& inp_restricted_send(inp
, sifp
)) {
756 *errorp
= EHOSTUNREACH
;
765 /* if ifp is non-NULL, refcnt held in in6_selectif() */
767 } else if (ifp
!= NULL
) {
770 return (src_storage
);
774 * Given a source IPv6 address (and route, if available), determine the best
775 * interface to send the packet from. Checking for (and updating) the
776 * ROF_SRCIF_SELECTED flag in the pcb-supplied route placeholder is done
777 * without any locks, based on the assumption that in the event this is
778 * called from ip6_output(), the output operation is single-threaded per-pcb,
779 * i.e. for any given pcb there can only be one thread performing output at
782 * This routine is analogous to in_selectsrcif() for IPv4. Regardless of
783 * error, it will return an ifp with a reference held if the caller provides
784 * a non-NULL retifp. The caller is responsible for checking if the
785 * returned ifp is valid and release its reference at all times.
787 * clone - meaningful only for bsdi and freebsd
790 selectroute(struct sockaddr_in6
*srcsock
, struct sockaddr_in6
*dstsock
,
791 struct ip6_pktopts
*opts
, struct ip6_moptions
*mopts
,
792 struct in6_ifaddr
**retsrcia
, struct route_in6
*ro
,
793 struct ifnet
**retifp
, struct rtentry
**retrt
, int clone
,
794 int norouteok
, struct ip6_out_args
*ip6oa
)
797 struct ifnet
*ifp
= NULL
, *ifp0
= NULL
;
798 struct route_in6
*route
= NULL
;
799 struct sockaddr_in6
*sin6_next
;
800 struct in6_pktinfo
*pi
= NULL
;
801 struct in6_addr
*dst
= &dstsock
->sin6_addr
;
802 struct ifaddr
*ifa
= NULL
;
803 char s_src
[MAX_IPv6_STR_LEN
], s_dst
[MAX_IPv6_STR_LEN
];
804 boolean_t select_srcif
, proxied_ifa
= FALSE
, local_dst
= FALSE
;
805 unsigned int ifscope
= ((ip6oa
!= NULL
) ?
806 ip6oa
->ip6oa_boundif
: IFSCOPE_NONE
);
814 if (ip6_select_srcif_debug
) {
816 src
= (srcsock
!= NULL
) ? srcsock
->sin6_addr
: in6addr_any
;
817 (void) inet_ntop(AF_INET6
, &src
, s_src
, sizeof (s_src
));
818 (void) inet_ntop(AF_INET6
, dst
, s_dst
, sizeof (s_dst
));
822 * If the destination address is UNSPECIFIED addr, bail out.
824 if (IN6_IS_ADDR_UNSPECIFIED(dst
)) {
825 error
= EHOSTUNREACH
;
830 * Perform source interface selection only if Scoped Routing
831 * is enabled and a source address that isn't unspecified.
833 select_srcif
= (srcsock
!= NULL
&&
834 !IN6_IS_ADDR_UNSPECIFIED(&srcsock
->sin6_addr
));
836 if (ip6_select_srcif_debug
) {
837 printf("%s src %s dst %s ifscope %d select_srcif %d\n",
838 __func__
, s_src
, s_dst
, ifscope
, select_srcif
);
841 /* If the caller specified the outgoing interface explicitly, use it */
842 if (opts
!= NULL
&& (pi
= opts
->ip6po_pktinfo
) != NULL
&&
843 pi
->ipi6_ifindex
!= 0) {
845 * If IPV6_PKTINFO takes precedence over IPV6_BOUND_IF.
847 ifscope
= pi
->ipi6_ifindex
;
848 ifnet_head_lock_shared();
849 /* ifp may be NULL if detached or out of range */
851 ((ifscope
<= if_index
) ? ifindex2ifnet
[ifscope
] : NULL
);
853 if (norouteok
|| retrt
== NULL
|| IN6_IS_ADDR_MULTICAST(dst
)) {
855 * We do not have to check or get the route for
856 * multicast. If the caller didn't ask/care for
857 * the route and we have no interface to use,
861 error
= EHOSTUNREACH
;
869 * If the destination address is a multicast address and the outgoing
870 * interface for the address is specified by the caller, use it.
872 if (IN6_IS_ADDR_MULTICAST(dst
) && mopts
!= NULL
) {
874 if ((ifp
= ifp0
= mopts
->im6o_multicast_ifp
) != NULL
) {
876 goto done
; /* we do not need a route for multicast. */
883 * If the outgoing interface was not set via IPV6_BOUND_IF or
884 * IPV6_PKTINFO, use the scope ID in the destination address.
886 if (ifscope
== IFSCOPE_NONE
)
887 ifscope
= dstsock
->sin6_scope_id
;
890 * Perform source interface selection; the source IPv6 address
891 * must belong to one of the addresses of the interface used
892 * by the route. For performance reasons, do this only if
893 * there is no route, or if the routing table has changed,
894 * or if we haven't done source interface selection on this
895 * route (for this PCB instance) before.
899 } else if (!ROUTE_UNUSABLE(ro
) && ro
->ro_srcia
!= NULL
&&
900 (ro
->ro_flags
& ROF_SRCIF_SELECTED
)) {
901 if (ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)
904 IFA_ADDREF(ifa
); /* for caller */
909 * Given the source IPv6 address, find a suitable source interface
910 * to use for transmission; if a scope ID has been specified,
911 * optimize the search by looking at the addresses only for that
912 * interface. This is still suboptimal, however, as we need to
913 * traverse the per-interface list.
915 if (ifscope
!= IFSCOPE_NONE
|| (ro
!= NULL
&& ro
->ro_rt
!= NULL
)) {
916 unsigned int scope
= ifscope
;
917 struct ifnet
*rt_ifp
;
919 rt_ifp
= (ro
->ro_rt
!= NULL
) ? ro
->ro_rt
->rt_ifp
: NULL
;
922 * If no scope is specified and the route is stale (pointing
923 * to a defunct interface) use the current primary interface;
924 * this happens when switching between interfaces configured
925 * with the same IPv6 address. Otherwise pick up the scope
926 * information from the route; the ULP may have looked up a
927 * correct route and we just need to verify it here and mark
928 * it with the ROF_SRCIF_SELECTED flag below.
930 if (scope
== IFSCOPE_NONE
) {
931 scope
= rt_ifp
->if_index
;
932 if (scope
!= get_primary_ifscope(AF_INET6
) &&
934 scope
= get_primary_ifscope(AF_INET6
);
937 ifa
= (struct ifaddr
*)
938 ifa_foraddr6_scoped(&srcsock
->sin6_addr
, scope
);
941 * If we are forwarding and proxying prefix(es), see if the
942 * source address is one of ours and is a proxied address;
945 if (ifa
== NULL
&& ip6_forwarding
&& nd6_prproxy
) {
946 ifa
= (struct ifaddr
*)
947 ifa_foraddr6(&srcsock
->sin6_addr
);
948 if (ifa
!= NULL
&& !(proxied_ifa
=
949 nd6_prproxy_ifaddr((struct in6_ifaddr
*)ifa
))) {
955 if (ip6_select_srcif_debug
&& ifa
!= NULL
) {
956 if (ro
->ro_rt
!= NULL
) {
957 printf("%s %s->%s ifscope %d->%d ifa_if %s "
960 s_src
, s_dst
, ifscope
,
961 scope
, if_name(ifa
->ifa_ifp
),
964 printf("%s %s->%s ifscope %d->%d ifa_if %s\n",
966 s_src
, s_dst
, ifscope
, scope
,
967 if_name(ifa
->ifa_ifp
));
973 * Slow path; search for an interface having the corresponding source
974 * IPv6 address if the scope was not specified by the caller, and:
976 * 1) There currently isn't any route, or,
977 * 2) The interface used by the route does not own that source
978 * IPv6 address; in this case, the route will get blown away
979 * and we'll do a more specific scoped search using the newly
982 if (ifa
== NULL
&& ifscope
== IFSCOPE_NONE
) {
983 struct ifaddr
*ifadst
;
985 /* Check if the destination address is one of ours */
986 ifadst
= (struct ifaddr
*)ifa_foraddr6(&dstsock
->sin6_addr
);
987 if (ifadst
!= NULL
) {
992 ifa
= (struct ifaddr
*)ifa_foraddr6(&srcsock
->sin6_addr
);
994 if (ip6_select_srcif_debug
&& ifa
!= NULL
) {
995 printf("%s %s->%s ifscope %d ifa_if %s\n",
997 s_src
, s_dst
, ifscope
, if_name(ifa
->ifa_ifp
));
998 } else if (ip6_select_srcif_debug
) {
999 printf("%s %s->%s ifscope %d ifa_if NULL\n",
1001 s_src
, s_dst
, ifscope
);
1006 if (ifa
!= NULL
&& !proxied_ifa
&& !local_dst
)
1007 ifscope
= ifa
->ifa_ifp
->if_index
;
1010 * If the next hop address for the packet is specified by the caller,
1011 * use it as the gateway.
1013 if (opts
!= NULL
&& opts
->ip6po_nexthop
!= NULL
) {
1014 struct route_in6
*ron
;
1016 sin6_next
= satosin6(opts
->ip6po_nexthop
);
1018 /* at this moment, we only support AF_INET6 next hops */
1019 if (sin6_next
->sin6_family
!= AF_INET6
) {
1020 error
= EAFNOSUPPORT
; /* or should we proceed? */
1025 * If the next hop is an IPv6 address, then the node identified
1026 * by that address must be a neighbor of the sending host.
1028 ron
= &opts
->ip6po_nextroute
;
1029 if (ron
->ro_rt
!= NULL
)
1030 RT_LOCK(ron
->ro_rt
);
1031 if (ROUTE_UNUSABLE(ron
) || (ron
->ro_rt
!= NULL
&&
1032 (!(ron
->ro_rt
->rt_flags
& RTF_LLINFO
) ||
1033 (select_srcif
&& (ifa
== NULL
||
1034 (ifa
->ifa_ifp
!= ron
->ro_rt
->rt_ifp
&& !proxied_ifa
))))) ||
1035 !IN6_ARE_ADDR_EQUAL(&satosin6(&ron
->ro_dst
)->sin6_addr
,
1036 &sin6_next
->sin6_addr
)) {
1037 if (ron
->ro_rt
!= NULL
)
1038 RT_UNLOCK(ron
->ro_rt
);
1041 *satosin6(&ron
->ro_dst
) = *sin6_next
;
1043 if (ron
->ro_rt
== NULL
) {
1044 rtalloc_scoped((struct route
*)ron
, ifscope
);
1045 if (ron
->ro_rt
!= NULL
)
1046 RT_LOCK(ron
->ro_rt
);
1047 if (ROUTE_UNUSABLE(ron
) ||
1048 !(ron
->ro_rt
->rt_flags
& RTF_LLINFO
) ||
1049 !IN6_ARE_ADDR_EQUAL(&satosin6(rt_key(ron
->ro_rt
))->
1050 sin6_addr
, &sin6_next
->sin6_addr
)) {
1051 if (ron
->ro_rt
!= NULL
)
1052 RT_UNLOCK(ron
->ro_rt
);
1055 error
= EHOSTUNREACH
;
1060 ifp
= ifp0
= ron
->ro_rt
->rt_ifp
;
1063 * When cloning is required, try to allocate a route to the
1064 * destination so that the caller can store path MTU
1069 /* Keep the route locked */
1072 RT_UNLOCK(ron
->ro_rt
);
1075 RT_UNLOCK(ron
->ro_rt
);
1079 * Use a cached route if it exists and is valid, else try to allocate
1080 * a new one. Note that we should check the address family of the
1081 * cached destination, in case of sharing the cache with IPv4.
1085 if (ro
->ro_rt
!= NULL
)
1086 RT_LOCK_SPIN(ro
->ro_rt
);
1087 if (ROUTE_UNUSABLE(ro
) || (ro
->ro_rt
!= NULL
&&
1088 (satosin6(&ro
->ro_dst
)->sin6_family
!= AF_INET6
||
1089 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
) ||
1090 (select_srcif
&& (ifa
== NULL
||
1091 (ifa
->ifa_ifp
!= ro
->ro_rt
->rt_ifp
&& !proxied_ifa
)))))) {
1092 if (ro
->ro_rt
!= NULL
)
1093 RT_UNLOCK(ro
->ro_rt
);
1097 if (ro
->ro_rt
== NULL
) {
1098 struct sockaddr_in6
*sa6
;
1100 if (ro
->ro_rt
!= NULL
)
1101 RT_UNLOCK(ro
->ro_rt
);
1102 /* No route yet, so try to acquire one */
1103 bzero(&ro
->ro_dst
, sizeof (struct sockaddr_in6
));
1104 sa6
= (struct sockaddr_in6
*)&ro
->ro_dst
;
1105 sa6
->sin6_family
= AF_INET6
;
1106 sa6
->sin6_len
= sizeof (struct sockaddr_in6
);
1107 sa6
->sin6_addr
= *dst
;
1108 if (IN6_IS_ADDR_MULTICAST(dst
)) {
1109 ro
->ro_rt
= rtalloc1_scoped(
1110 &((struct route
*)ro
)->ro_dst
, 0, 0, ifscope
);
1112 rtalloc_scoped((struct route
*)ro
, ifscope
);
1114 if (ro
->ro_rt
!= NULL
)
1115 RT_LOCK_SPIN(ro
->ro_rt
);
1119 * Do not care about the result if we have the nexthop
1120 * explicitly specified (in case we're asked to clone.)
1122 if (opts
!= NULL
&& opts
->ip6po_nexthop
!= NULL
) {
1123 if (ro
->ro_rt
!= NULL
)
1124 RT_UNLOCK(ro
->ro_rt
);
1128 if (ro
->ro_rt
!= NULL
) {
1129 RT_LOCK_ASSERT_HELD(ro
->ro_rt
);
1130 ifp
= ifp0
= ro
->ro_rt
->rt_ifp
;
1132 error
= EHOSTUNREACH
;
1138 boolean_t has_route
= (route
!= NULL
&& route
->ro_rt
!= NULL
);
1139 boolean_t srcif_selected
= FALSE
;
1142 RT_LOCK_ASSERT_HELD(route
->ro_rt
);
1144 * If there is a non-loopback route with the wrong interface,
1145 * or if there is no interface configured with such an address,
1146 * blow it away. Except for local/loopback, we look for one
1147 * with a matching interface scope/index.
1149 if (has_route
&& (ifa
== NULL
||
1150 (ifa
->ifa_ifp
!= ifp
&& ifp
!= lo_ifp
) ||
1151 !(route
->ro_rt
->rt_flags
& RTF_UP
))) {
1153 * If the destination address belongs to a proxied
1154 * prefix, relax the requirement and allow the packet
1155 * to come out of the proxy interface with the source
1156 * address of the real interface.
1158 if (ifa
!= NULL
&& proxied_ifa
&&
1159 (route
->ro_rt
->rt_flags
& (RTF_UP
|RTF_PROXY
)) ==
1160 (RTF_UP
|RTF_PROXY
)) {
1161 srcif_selected
= TRUE
;
1163 if (ip6_select_srcif_debug
) {
1165 printf("%s->%s ifscope %d "
1166 "ro_if %s != ifa_if %s "
1167 "(cached route cleared)\n",
1169 ifscope
, if_name(ifp
),
1170 if_name(ifa
->ifa_ifp
));
1172 printf("%s->%s ifscope %d "
1173 "ro_if %s (no ifa_if "
1174 "found)\n", s_src
, s_dst
,
1175 ifscope
, if_name(ifp
));
1178 RT_UNLOCK(route
->ro_rt
);
1179 ROUTE_RELEASE(route
);
1180 error
= EHOSTUNREACH
;
1181 /* Undo the settings done above */
1183 ifp
= NULL
; /* ditch ifp; keep ifp0 */
1186 } else if (has_route
) {
1187 srcif_selected
= TRUE
;
1190 if (srcif_selected
) {
1192 if (ifa
!= route
->ro_srcia
||
1193 !(route
->ro_flags
& ROF_SRCIF_SELECTED
)) {
1194 RT_CONVERT_LOCK(route
->ro_rt
);
1196 IFA_ADDREF(ifa
); /* for route_in6 */
1197 if (route
->ro_srcia
!= NULL
)
1198 IFA_REMREF(route
->ro_srcia
);
1199 route
->ro_srcia
= ifa
;
1200 route
->ro_flags
|= ROF_SRCIF_SELECTED
;
1201 RT_GENID_SYNC(route
->ro_rt
);
1203 RT_UNLOCK(route
->ro_rt
);
1206 if (ro
->ro_rt
!= NULL
)
1207 RT_UNLOCK(ro
->ro_rt
);
1208 if (ifp
!= NULL
&& opts
!= NULL
&&
1209 opts
->ip6po_pktinfo
!= NULL
&&
1210 opts
->ip6po_pktinfo
->ipi6_ifindex
!= 0) {
1212 * Check if the outgoing interface conflicts with the
1213 * interface specified by ipi6_ifindex (if specified).
1214 * Note that loopback interface is always okay.
1215 * (this may happen when we are sending a packet to
1216 * one of our own addresses.)
1218 if (!(ifp
->if_flags
& IFF_LOOPBACK
) && ifp
->if_index
!=
1219 opts
->ip6po_pktinfo
->ipi6_ifindex
) {
1220 error
= EHOSTUNREACH
;
1228 * Check for interface restrictions.
1230 #define CHECK_RESTRICTIONS(_ip6oa, _ifp) \
1231 ((((_ip6oa)->ip6oa_flags & IP6OAF_NO_CELLULAR) && \
1232 IFNET_IS_CELLULAR(_ifp)) || \
1233 (((_ip6oa)->ip6oa_flags & IP6OAF_NO_EXPENSIVE) && \
1234 IFNET_IS_EXPENSIVE(_ifp)) || \
1235 (!((_ip6oa)->ip6oa_flags & IP6OAF_INTCOPROC_ALLOWED) && \
1236 IFNET_IS_INTCOPROC(_ifp)) || \
1237 (!((_ip6oa)->ip6oa_flags & IP6OAF_AWDL_UNRESTRICTED) && \
1238 IFNET_IS_AWDL_RESTRICTED(_ifp)))
1240 if (error
== 0 && ip6oa
!= NULL
&&
1241 ((ifp
&& CHECK_RESTRICTIONS(ip6oa
, ifp
)) ||
1242 (route
&& route
->ro_rt
&&
1243 CHECK_RESTRICTIONS(ip6oa
, route
->ro_rt
->rt_ifp
)))) {
1244 if (route
!= NULL
&& route
->ro_rt
!= NULL
) {
1245 ROUTE_RELEASE(route
);
1248 ifp
= NULL
; /* ditch ifp; keep ifp0 */
1249 error
= EHOSTUNREACH
;
1250 ip6oa
->ip6oa_retflags
|= IP6OARF_IFDENIED
;
1252 #undef CHECK_RESTRICTIONS
1255 * If the interface is disabled for IPv6, then ENETDOWN error.
1258 ifp
!= NULL
&& (ifp
->if_eflags
& IFEF_IPV6_DISABLED
)) {
1262 if (ifp
== NULL
&& (route
== NULL
|| route
->ro_rt
== NULL
)) {
1264 * This can happen if the caller did not pass a cached route
1265 * nor any other hints. We treat this case an error.
1267 error
= EHOSTUNREACH
;
1269 if (error
== EHOSTUNREACH
|| error
== ENETDOWN
)
1270 ip6stat
.ip6s_noroute
++;
1273 * We'll return ifp regardless of error, so pick it up from ifp0
1274 * in case it was nullified above. Caller is responsible for
1275 * releasing the ifp if it is non-NULL.
1278 if (retifp
!= NULL
) {
1280 ifnet_reference(ifp
); /* for caller */
1284 if (retsrcia
!= NULL
) {
1286 IFA_ADDREF(ifa
); /* for caller */
1287 *retsrcia
= (struct in6_ifaddr
*)ifa
;
1291 if (retrt
!= NULL
&& route
!= NULL
)
1292 *retrt
= route
->ro_rt
; /* ro_rt may be NULL */
1294 if (ip6_select_srcif_debug
) {
1295 printf("%s %s->%s ifscope %d ifa_if %s ro_if %s (error=%d)\n",
1297 s_src
, s_dst
, ifscope
,
1298 (ifa
!= NULL
) ? if_name(ifa
->ifa_ifp
) : "NONE",
1299 (ifp
!= NULL
) ? if_name(ifp
) : "NONE", error
);
1309 * Regardless of error, it will return an ifp with a reference held if the
1310 * caller provides a non-NULL retifp. The caller is responsible for checking
1311 * if the returned ifp is valid and release its reference at all times.
1314 in6_selectif(struct sockaddr_in6
*dstsock
, struct ip6_pktopts
*opts
,
1315 struct ip6_moptions
*mopts
, struct route_in6
*ro
,
1316 struct ip6_out_args
*ip6oa
, struct ifnet
**retifp
)
1319 struct route_in6 sro
;
1320 struct rtentry
*rt
= NULL
;
1323 bzero(&sro
, sizeof (sro
));
1327 if ((err
= selectroute(NULL
, dstsock
, opts
, mopts
, NULL
, ro
, retifp
,
1328 &rt
, 0, 1, ip6oa
)) != 0)
1332 * do not use a rejected or black hole route.
1333 * XXX: this check should be done in the L2 output routine.
1334 * However, if we skipped this check here, we'd see the following
1336 * - install a rejected route for a scoped address prefix
1338 * - send a packet to a destination that matches the scoped prefix,
1339 * with ambiguity about the scope zone.
1340 * - pick the outgoing interface from the route, and disambiguate the
1341 * scope zone with the interface.
1342 * - ip6_output() would try to get another route with the "new"
1343 * destination, which may be valid.
1344 * - we'd see no error on output.
1345 * Although this may not be very harmful, it should still be confusing.
1346 * We thus reject the case here.
1348 if (rt
&& (rt
->rt_flags
& (RTF_REJECT
| RTF_BLACKHOLE
))) {
1349 err
= ((rt
->rt_flags
& RTF_HOST
) ? EHOSTUNREACH
: ENETUNREACH
);
1354 * Adjust the "outgoing" interface. If we're going to loop the packet
1355 * back to ourselves, the ifp would be the loopback interface.
1356 * However, we'd rather know the interface associated to the
1357 * destination address (which should probably be one of our own
1360 if (rt
!= NULL
&& rt
->rt_ifa
!= NULL
&& rt
->rt_ifa
->ifa_ifp
!= NULL
&&
1362 ifnet_reference(rt
->rt_ifa
->ifa_ifp
);
1363 if (*retifp
!= NULL
)
1364 ifnet_release(*retifp
);
1365 *retifp
= rt
->rt_ifa
->ifa_ifp
;
1370 VERIFY(rt
== NULL
|| rt
== ro
->ro_rt
);
1375 * retifp might point to a valid ifp with a reference held;
1376 * caller is responsible for releasing it if non-NULL.
1382 * Regardless of error, it will return an ifp with a reference held if the
1383 * caller provides a non-NULL retifp. The caller is responsible for checking
1384 * if the returned ifp is valid and release its reference at all times.
1386 * clone - meaningful only for bsdi and freebsd
1389 in6_selectroute(struct sockaddr_in6
*srcsock
, struct sockaddr_in6
*dstsock
,
1390 struct ip6_pktopts
*opts
, struct ip6_moptions
*mopts
,
1391 struct in6_ifaddr
**retsrcia
, struct route_in6
*ro
, struct ifnet
**retifp
,
1392 struct rtentry
**retrt
, int clone
, struct ip6_out_args
*ip6oa
)
1395 return (selectroute(srcsock
, dstsock
, opts
, mopts
, retsrcia
, ro
, retifp
,
1396 retrt
, clone
, 0, ip6oa
));
1400 * Default hop limit selection. The precedence is as follows:
1401 * 1. Hoplimit value specified via ioctl.
1402 * 2. (If the outgoing interface is detected) the current
1403 * hop limit of the interface specified by router advertisement.
1404 * 3. The system default hoplimit.
1407 in6_selecthlim(struct in6pcb
*in6p
, struct ifnet
*ifp
)
1409 if (in6p
&& in6p
->in6p_hops
>= 0) {
1410 return (in6p
->in6p_hops
);
1411 } else if (NULL
!= ifp
) {
1413 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
1414 if (ndi
&& ndi
->initialized
) {
1415 /* access chlim without lock, for performance */
1418 chlim
= ip6_defhlim
;
1423 return (ip6_defhlim
);
1427 * XXX: this is borrowed from in6_pcbbind(). If possible, we should
1428 * share this function by all *bsd*...
1431 in6_pcbsetport(struct in6_addr
*laddr
, struct inpcb
*inp
, struct proc
*p
,
1434 struct socket
*so
= inp
->inp_socket
;
1435 u_int16_t lport
= 0, first
, last
, *lastport
;
1436 int count
, error
= 0, wild
= 0;
1437 boolean_t counting_down
;
1439 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1441 #pragma unused(laddr)
1442 if (!locked
) { /* Make sure we don't run into a deadlock: 4052373 */
1443 if (!lck_rw_try_lock_exclusive(pcbinfo
->ipi_lock
)) {
1444 socket_unlock(inp
->inp_socket
, 0);
1445 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
1446 socket_lock(inp
->inp_socket
, 0);
1450 * Check if a local port was assigned to the inp while
1451 * this thread was waiting for the pcbinfo lock
1453 if (inp
->inp_lport
!= 0) {
1454 VERIFY(inp
->inp_flags2
& INP2_INHASHLIST
);
1455 lck_rw_done(pcbinfo
->ipi_lock
);
1458 * It is not an error if another thread allocated
1465 /* XXX: this is redundant when called from in6_pcbbind */
1466 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
1467 wild
= INPLOOKUP_WILDCARD
;
1469 if (inp
->inp_flags
& INP_HIGHPORT
) {
1470 first
= ipport_hifirstauto
; /* sysctl */
1471 last
= ipport_hilastauto
;
1472 lastport
= &pcbinfo
->ipi_lasthi
;
1473 } else if (inp
->inp_flags
& INP_LOWPORT
) {
1474 cred
= kauth_cred_proc_ref(p
);
1475 error
= priv_check_cred(cred
, PRIV_NETINET_RESERVEDPORT
, 0);
1476 kauth_cred_unref(&cred
);
1479 lck_rw_done(pcbinfo
->ipi_lock
);
1482 first
= ipport_lowfirstauto
; /* 1023 */
1483 last
= ipport_lowlastauto
; /* 600 */
1484 lastport
= &pcbinfo
->ipi_lastlow
;
1486 first
= ipport_firstauto
; /* sysctl */
1487 last
= ipport_lastauto
;
1488 lastport
= &pcbinfo
->ipi_lastport
;
1491 * Simple check to ensure all ports are not used up causing
1497 count
= first
- last
;
1498 counting_down
= TRUE
;
1501 count
= last
- first
;
1502 counting_down
= FALSE
;
1505 if (count
-- < 0) { /* completely used? */
1507 * Undo any address bind that may have
1510 inp
->in6p_laddr
= in6addr_any
;
1511 inp
->in6p_last_outifp
= NULL
;
1513 lck_rw_done(pcbinfo
->ipi_lock
);
1516 if (counting_down
) {
1518 if (*lastport
> first
|| *lastport
< last
) {
1523 if (*lastport
< first
|| *lastport
> last
)
1526 lport
= htons(*lastport
);
1527 found
= (in6_pcblookup_local(pcbinfo
, &inp
->in6p_laddr
,
1528 lport
, wild
) == NULL
);
1531 inp
->inp_lport
= lport
;
1532 inp
->inp_flags
|= INP_ANONPORT
;
1534 if (in_pcbinshash(inp
, 1) != 0) {
1535 inp
->in6p_laddr
= in6addr_any
;
1536 inp
->in6p_last_outifp
= NULL
;
1539 inp
->inp_flags
&= ~INP_ANONPORT
;
1541 lck_rw_done(pcbinfo
->ipi_lock
);
1546 lck_rw_done(pcbinfo
->ipi_lock
);
1551 * The followings are implementation of the policy table using a
1552 * simple tail queue.
1553 * XXX such details should be hidden.
1554 * XXX implementation using binary tree should be more efficient.
1556 struct addrsel_policyent
{
1557 TAILQ_ENTRY(addrsel_policyent
) ape_entry
;
1558 struct in6_addrpolicy ape_policy
;
1561 TAILQ_HEAD(addrsel_policyhead
, addrsel_policyent
);
1563 struct addrsel_policyhead addrsel_policytab
;
1566 init_policy_queue(void)
1568 TAILQ_INIT(&addrsel_policytab
);
1572 addrsel_policy_init(void)
1575 * Default address selection policy based on RFC 6724.
1577 static const struct in6_addrpolicy defaddrsel
[] = {
1578 /* Loopback -- prefix=::1/128, precedence=50, label=0 */
1581 .sin6_family
= AF_INET6
,
1582 .sin6_addr
= IN6ADDR_LOOPBACK_INIT
,
1583 .sin6_len
= sizeof (struct sockaddr_in6
)
1586 .sin6_family
= AF_INET6
,
1587 .sin6_addr
= IN6MASK128
,
1588 .sin6_len
= sizeof (struct sockaddr_in6
)
1594 /* Unspecified -- prefix=::/0, precedence=40, label=1 */
1597 .sin6_family
= AF_INET6
,
1598 .sin6_addr
= IN6ADDR_ANY_INIT
,
1599 .sin6_len
= sizeof (struct sockaddr_in6
)
1602 .sin6_family
= AF_INET6
,
1603 .sin6_addr
= IN6MASK0
,
1604 .sin6_len
= sizeof (struct sockaddr_in6
)
1610 /* IPv4 Mapped -- prefix=::ffff:0:0/96, precedence=35, label=4 */
1613 .sin6_family
= AF_INET6
,
1614 .sin6_addr
= IN6ADDR_V4MAPPED_INIT
,
1615 .sin6_len
= sizeof (struct sockaddr_in6
)
1618 .sin6_family
= AF_INET6
,
1619 .sin6_addr
= IN6MASK96
,
1620 .sin6_len
= sizeof (struct sockaddr_in6
)
1626 /* 6to4 -- prefix=2002::/16, precedence=30, label=2 */
1629 .sin6_family
= AF_INET6
,
1630 .sin6_addr
= {{{ 0x20, 0x02 }}},
1631 .sin6_len
= sizeof (struct sockaddr_in6
)
1634 .sin6_family
= AF_INET6
,
1635 .sin6_addr
= IN6MASK16
,
1636 .sin6_len
= sizeof (struct sockaddr_in6
)
1642 /* Teredo -- prefix=2001::/32, precedence=5, label=5 */
1645 .sin6_family
= AF_INET6
,
1646 .sin6_addr
= {{{ 0x20, 0x01 }}},
1647 .sin6_len
= sizeof (struct sockaddr_in6
)
1650 .sin6_family
= AF_INET6
,
1651 .sin6_addr
= IN6MASK32
,
1652 .sin6_len
= sizeof (struct sockaddr_in6
)
1658 /* Unique Local (ULA) -- prefix=fc00::/7, precedence=3, label=13 */
1661 .sin6_family
= AF_INET6
,
1662 .sin6_addr
= {{{ 0xfc }}},
1663 .sin6_len
= sizeof (struct sockaddr_in6
)
1666 .sin6_family
= AF_INET6
,
1667 .sin6_addr
= IN6MASK7
,
1668 .sin6_len
= sizeof (struct sockaddr_in6
)
1674 /* IPv4 Compatible -- prefix=::/96, precedence=1, label=3 */
1677 .sin6_family
= AF_INET6
,
1678 .sin6_addr
= IN6ADDR_ANY_INIT
,
1679 .sin6_len
= sizeof (struct sockaddr_in6
)
1682 .sin6_family
= AF_INET6
,
1683 .sin6_addr
= IN6MASK96
,
1684 .sin6_len
= sizeof (struct sockaddr_in6
)
1690 /* Site-local (deprecated) -- prefix=fec0::/10, precedence=1, label=11 */
1693 .sin6_family
= AF_INET6
,
1694 .sin6_addr
= {{{ 0xfe, 0xc0 }}},
1695 .sin6_len
= sizeof (struct sockaddr_in6
)
1698 .sin6_family
= AF_INET6
,
1699 .sin6_addr
= IN6MASK16
,
1700 .sin6_len
= sizeof (struct sockaddr_in6
)
1706 /* 6bone (deprecated) -- prefix=3ffe::/16, precedence=1, label=12 */
1709 .sin6_family
= AF_INET6
,
1710 .sin6_addr
= {{{ 0x3f, 0xfe }}},
1711 .sin6_len
= sizeof (struct sockaddr_in6
)
1714 .sin6_family
= AF_INET6
,
1715 .sin6_addr
= IN6MASK16
,
1716 .sin6_len
= sizeof (struct sockaddr_in6
)
1724 init_policy_queue();
1726 /* initialize the "last resort" policy */
1727 bzero(&defaultaddrpolicy
, sizeof (defaultaddrpolicy
));
1728 defaultaddrpolicy
.label
= ADDR_LABEL_NOTAPP
;
1730 for (i
= 0; i
< sizeof (defaddrsel
) / sizeof (defaddrsel
[0]); i
++)
1731 add_addrsel_policyent(&defaddrsel
[i
]);
1735 struct in6_addrpolicy
*
1736 in6_addrsel_lookup_policy(struct sockaddr_in6
*key
)
1738 struct in6_addrpolicy
*match
= NULL
;
1741 match
= match_addrsel_policy(key
);
1744 match
= &defaultaddrpolicy
;
1752 static struct in6_addrpolicy
*
1753 match_addrsel_policy(struct sockaddr_in6
*key
)
1755 struct addrsel_policyent
*pent
;
1756 struct in6_addrpolicy
*bestpol
= NULL
, *pol
;
1757 int matchlen
, bestmatchlen
= -1;
1758 u_char
*mp
, *ep
, *k
, *p
, m
;
1760 TAILQ_FOREACH(pent
, &addrsel_policytab
, ape_entry
) {
1763 pol
= &pent
->ape_policy
;
1764 mp
= (u_char
*)&pol
->addrmask
.sin6_addr
;
1765 ep
= mp
+ 16; /* XXX: scope field? */
1766 k
= (u_char
*)&key
->sin6_addr
;
1767 p
= (u_char
*)&pol
->addr
.sin6_addr
;
1768 for (; mp
< ep
&& *mp
; mp
++, k
++, p
++) {
1771 goto next
; /* not match */
1772 if (m
== 0xff) /* short cut for a typical case */
1782 /* matched. check if this is better than the current best. */
1783 if (bestpol
== NULL
||
1784 matchlen
> bestmatchlen
) {
1786 bestmatchlen
= matchlen
;
1797 add_addrsel_policyent(const struct in6_addrpolicy
*newpolicy
)
1799 struct addrsel_policyent
*new, *pol
;
1801 MALLOC(new, struct addrsel_policyent
*, sizeof (*new), M_IFADDR
,
1806 /* duplication check */
1807 TAILQ_FOREACH(pol
, &addrsel_policytab
, ape_entry
) {
1808 if (IN6_ARE_ADDR_EQUAL(&newpolicy
->addr
.sin6_addr
,
1809 &pol
->ape_policy
.addr
.sin6_addr
) &&
1810 IN6_ARE_ADDR_EQUAL(&newpolicy
->addrmask
.sin6_addr
,
1811 &pol
->ape_policy
.addrmask
.sin6_addr
)) {
1813 FREE(new, M_IFADDR
);
1814 return (EEXIST
); /* or override it? */
1818 bzero(new, sizeof (*new));
1820 /* XXX: should validate entry */
1821 new->ape_policy
= *newpolicy
;
1823 TAILQ_INSERT_TAIL(&addrsel_policytab
, new, ape_entry
);
1828 #ifdef ENABLE_ADDRSEL
1830 delete_addrsel_policyent(const struct in6_addrpolicy
*key
)
1832 struct addrsel_policyent
*pol
;
1837 /* search for the entry in the table */
1838 TAILQ_FOREACH(pol
, &addrsel_policytab
, ape_entry
) {
1839 if (IN6_ARE_ADDR_EQUAL(&key
->addr
.sin6_addr
,
1840 &pol
->ape_policy
.addr
.sin6_addr
) &&
1841 IN6_ARE_ADDR_EQUAL(&key
->addrmask
.sin6_addr
,
1842 &pol
->ape_policy
.addrmask
.sin6_addr
)) {
1851 TAILQ_REMOVE(&addrsel_policytab
, pol
, ape_entry
);
1852 FREE(pol
, M_IFADDR
);
1858 #endif /* ENABLE_ADDRSEL */
1861 walk_addrsel_policy(int (*callback
)(const struct in6_addrpolicy
*, void *),
1864 struct addrsel_policyent
*pol
;
1868 TAILQ_FOREACH(pol
, &addrsel_policytab
, ape_entry
) {
1869 if ((error
= (*callback
)(&pol
->ape_policy
, w
)) != 0) {
1878 * Subroutines to manage the address selection policy table via sysctl.
1881 struct sysctl_req
*w_req
;
1886 dump_addrsel_policyent(const struct in6_addrpolicy
*pol
, void *arg
)
1889 struct walkarg
*w
= arg
;
1891 error
= SYSCTL_OUT(w
->w_req
, pol
, sizeof (*pol
));
1897 in6_src_sysctl SYSCTL_HANDLER_ARGS
1899 #pragma unused(oidp, arg1, arg2)
1904 bzero(&w
, sizeof (w
));
1907 return (walk_addrsel_policy(dump_addrsel_policyent
, &w
));
1911 SYSCTL_NODE(_net_inet6_ip6
, IPV6CTL_ADDRCTLPOLICY
, addrctlpolicy
,
1912 CTLFLAG_RD
| CTLFLAG_LOCKED
, in6_src_sysctl
, "");
1914 in6_src_ioctl(u_long cmd
, caddr_t data
)
1917 struct in6_addrpolicy ent0
;
1919 if (cmd
!= SIOCAADDRCTL_POLICY
&& cmd
!= SIOCDADDRCTL_POLICY
)
1920 return (EOPNOTSUPP
); /* check for safety */
1922 bcopy(data
, &ent0
, sizeof (ent0
));
1924 if (ent0
.label
== ADDR_LABEL_NOTAPP
)
1926 /* check if the prefix mask is consecutive. */
1927 if (in6_mask2len(&ent0
.addrmask
.sin6_addr
, NULL
) < 0)
1929 /* clear trailing garbages (if any) of the prefix address. */
1930 for (i
= 0; i
< 4; i
++) {
1931 ent0
.addr
.sin6_addr
.s6_addr32
[i
] &=
1932 ent0
.addrmask
.sin6_addr
.s6_addr32
[i
];
1937 case SIOCAADDRCTL_POLICY
:
1938 #ifdef ENABLE_ADDRSEL
1939 return (add_addrsel_policyent(&ent0
));
1943 case SIOCDADDRCTL_POLICY
:
1944 #ifdef ENABLE_ADDRSEL
1945 return (delete_addrsel_policyent(&ent0
));
1951 return (0); /* XXX: compromise compilers */
1955 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
1956 * If the address scope of is link-local, embed the interface index in the
1957 * address. The routine determines our precedence
1958 * between advanced API scope/interface specification and basic API
1961 * this function should be nuked in the future, when we get rid of
1962 * embedded scopeid thing.
1964 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
1965 * there can be multiple interfaces that belong to a particular scope zone
1966 * (in specification, we have 1:N mapping between a scope zone and interfaces).
1967 * we may want to change the function to return something other than ifp.
1970 in6_embedscope(struct in6_addr
*in6
, const struct sockaddr_in6
*sin6
,
1971 struct in6pcb
*in6p
, struct ifnet
**ifpp
, struct ip6_pktopts
*opt
)
1973 struct ifnet
*ifp
= NULL
;
1975 struct ip6_pktopts
*optp
= NULL
;
1977 *in6
= sin6
->sin6_addr
;
1978 scopeid
= sin6
->sin6_scope_id
;
1983 * don't try to read sin6->sin6_addr beyond here, since the caller may
1984 * ask us to overwrite existing sockaddr_in6
1987 #ifdef ENABLE_DEFAULT_SCOPE
1989 scopeid
= scope6_addr2default(in6
);
1992 if (IN6_IS_SCOPE_LINKLOCAL(in6
) || IN6_IS_ADDR_MC_INTFACELOCAL(in6
)) {
1993 struct in6_pktinfo
*pi
;
1994 struct ifnet
*im6o_multicast_ifp
= NULL
;
1996 if (in6p
!= NULL
&& IN6_IS_ADDR_MULTICAST(in6
) &&
1997 in6p
->in6p_moptions
!= NULL
) {
1998 IM6O_LOCK(in6p
->in6p_moptions
);
1999 im6o_multicast_ifp
=
2000 in6p
->in6p_moptions
->im6o_multicast_ifp
;
2001 IM6O_UNLOCK(in6p
->in6p_moptions
);
2006 else if (in6p
!= NULL
)
2007 optp
= in6p
->in6p_outputopts
;
2009 * KAME assumption: link id == interface id
2011 if (in6p
!= NULL
&& optp
!= NULL
&&
2012 (pi
= optp
->ip6po_pktinfo
) != NULL
&&
2013 pi
->ipi6_ifindex
!= 0) {
2014 /* ifp is needed here if only we're returning it */
2016 ifnet_head_lock_shared();
2017 ifp
= ifindex2ifnet
[pi
->ipi6_ifindex
];
2020 in6
->s6_addr16
[1] = htons(pi
->ipi6_ifindex
);
2021 } else if (in6p
!= NULL
&& IN6_IS_ADDR_MULTICAST(in6
) &&
2022 in6p
->in6p_moptions
!= NULL
&& im6o_multicast_ifp
!= NULL
) {
2023 ifp
= im6o_multicast_ifp
;
2024 in6
->s6_addr16
[1] = htons(ifp
->if_index
);
2025 } else if (scopeid
!= 0) {
2027 * Since scopeid is unsigned, we only have to check it
2028 * against if_index (ifnet_head_lock not needed since
2029 * if_index is an ever-increasing integer.)
2031 if (if_index
< scopeid
)
2032 return (ENXIO
); /* XXX EINVAL? */
2034 /* ifp is needed here only if we're returning it */
2036 ifnet_head_lock_shared();
2037 ifp
= ifindex2ifnet
[scopeid
];
2040 /* XXX assignment to 16bit from 32bit variable */
2041 in6
->s6_addr16
[1] = htons(scopeid
& 0xffff);
2046 ifnet_reference(ifp
); /* for caller */
2055 * generate standard sockaddr_in6 from embedded form.
2056 * touches sin6_addr and sin6_scope_id only.
2058 * this function should be nuked in the future, when we get rid of
2059 * embedded scopeid thing.
2063 struct sockaddr_in6
*sin6
,
2064 const struct in6_addr
*in6
,
2069 sin6
->sin6_addr
= *in6
;
2072 * don't try to read *in6 beyond here, since the caller may
2073 * ask us to overwrite existing sockaddr_in6
2076 sin6
->sin6_scope_id
= 0;
2077 if (IN6_IS_SCOPE_LINKLOCAL(in6
) || IN6_IS_ADDR_MC_INTFACELOCAL(in6
)) {
2079 * KAME assumption: link id == interface id
2081 scopeid
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
2086 * Since scopeid is unsigned, we only have to check it
2089 if (if_index
< scopeid
)
2091 if (ifp
&& ifp
->if_index
!= scopeid
)
2093 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2094 sin6
->sin6_scope_id
= scopeid
;