1 | /* |
2 | * Copyright (c) 2000-2018 Apple Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * |
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. |
14 | * |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | * |
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. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | |
29 | /* |
30 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
31 | * All rights reserved. |
32 | * |
33 | * Redistribution and use in source and binary forms, with or without |
34 | * modification, are permitted provided that the following conditions |
35 | * are met: |
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. |
44 | * |
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 |
55 | * SUCH DAMAGE. |
56 | */ |
57 | |
58 | /* |
59 | * Copyright (c) 1982, 1986, 1991, 1993 |
60 | * The Regents of the University of California. All rights reserved. |
61 | * |
62 | * Redistribution and use in source and binary forms, with or without |
63 | * modification, are permitted provided that the following conditions |
64 | * are met: |
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. |
77 | * |
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 |
88 | * SUCH DAMAGE. |
89 | * |
90 | * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 |
91 | */ |
92 | |
93 | |
94 | #include <sys/param.h> |
95 | #include <sys/systm.h> |
96 | #include <sys/malloc.h> |
97 | #include <sys/mbuf.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> |
108 | |
109 | #include <net/if.h> |
110 | #include <net/if_types.h> |
111 | #include <net/route.h> |
112 | |
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> |
124 | |
125 | #include <net/net_osdep.h> |
126 | |
127 | #include "loop.h" |
128 | |
129 | SYSCTL_DECL(_net_inet6_ip6); |
130 | |
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" ); |
135 | |
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" ); |
140 | |
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" ); |
145 | |
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" ); |
150 | |
151 | #define ADDR_LABEL_NOTAPP (-1) |
152 | struct in6_addrpolicy defaultaddrpolicy; |
153 | |
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) |
159 | #else |
160 | #define ADDRSEL_LOCK() |
161 | #define ADDRSEL_UNLOCK() |
162 | #endif |
163 | |
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 *); |
175 | #endif |
176 | static int walk_addrsel_policy(int (*)(const struct in6_addrpolicy *, void *), |
177 | 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); |
181 | |
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)) |
186 | |
187 | #define SASEL_LOG(fmt, ...) \ |
188 | do { \ |
189 | if (srcsel_debug) \ |
190 | printf("%s:%d " fmt "\n",\ |
191 | __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
192 | } while (0); \ |
193 | |
194 | /* |
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. |
199 | */ |
200 | #define REPLACE(r) do {\ |
201 | SASEL_LOG("REPLACE r %d ia %s ifp1 %s\n", \ |
202 | (r), s_src, ifp1->if_xname); \ |
203 | srcrule = (r); \ |
204 | goto replace; \ |
205 | } while (0) |
206 | |
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 */ \ |
211 | } while (0) |
212 | |
213 | #define BREAK(r) do { \ |
214 | SASEL_LOG("BREAK r %d ia %s ifp1 %s\n", \ |
215 | (r), s_src, ifp1->if_xname); \ |
216 | srcrule = (r); \ |
217 | goto out; /* XXX: we can't use 'break' here */ \ |
218 | } while (0) |
219 | |
220 | |
221 | struct ifaddr * |
222 | in6_selectsrc_core_ifa(struct sockaddr_in6 *addr, struct ifnet *ifp, int srcsel_debug) { |
223 | int err = 0; |
224 | struct ifnet *src_ifp = NULL; |
225 | struct in6_addr src_storage = {}; |
226 | struct in6_addr *in6 = NULL; |
227 | struct ifaddr *ifa = NULL; |
228 | |
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) { |
232 | if (err == 0) |
233 | err = EADDRNOTAVAIL; |
234 | VERIFY(src_ifp == NULL); |
235 | if (ifa != NULL) { |
236 | IFA_REMREF(ifa); |
237 | ifa = NULL; |
238 | } |
239 | goto done; |
240 | } |
241 | |
242 | if (src_ifp != ifp) { |
243 | if (err == 0) |
244 | err = ENETUNREACH; |
245 | if (ifa != NULL) { |
246 | IFA_REMREF(ifa); |
247 | ifa = NULL; |
248 | } |
249 | goto done; |
250 | } |
251 | |
252 | VERIFY(ifa != NULL); |
253 | ifnet_lock_shared(ifp); |
254 | if ((ifa->ifa_debug & IFD_DETACHING) != 0) { |
255 | err = EHOSTUNREACH; |
256 | ifnet_lock_done(ifp); |
257 | if (ifa != NULL) { |
258 | IFA_REMREF(ifa); |
259 | ifa = NULL; |
260 | } |
261 | goto done; |
262 | } |
263 | ifnet_lock_done(ifp); |
264 | |
265 | done: |
266 | SASEL_LOG("Returned with error: %d" , err); |
267 | if (src_ifp != NULL) |
268 | ifnet_release(src_ifp); |
269 | return (ifa); |
270 | } |
271 | |
272 | struct in6_addr * |
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) |
276 | { |
277 | u_int32_t odstzone; |
278 | int bestrule = IP6S_SRCRULE_0; |
279 | struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL; |
280 | struct in6_addr dst; |
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); |
289 | VERIFY(ifp != NULL); |
290 | |
291 | if (sifp != NULL) |
292 | *sifp = NULL; |
293 | |
294 | if (ifapp != NULL) |
295 | *ifapp = NULL; |
296 | |
297 | dst = dstsock->sin6_addr; /* make a copy for local operation */ |
298 | |
299 | if (srcsel_debug) { |
300 | (void) inet_ntop(AF_INET6, &dst, s_dst, sizeof (s_src)); |
301 | |
302 | tmp = &in6addr_any; |
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); |
306 | } |
307 | |
308 | *errorp = in6_setscope(&dst, ifp, &odstzone); |
309 | if (*errorp != 0) { |
310 | src_storage = NULL; |
311 | goto done; |
312 | } |
313 | |
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; |
319 | struct in6_addr src; |
320 | struct ifnet *ifp1 = ia->ia_ifp; |
321 | int srcrule; |
322 | |
323 | if (srcsel_debug) |
324 | (void) inet_ntop(AF_INET6, &ia->ia_addr.sin6_addr, |
325 | s_src, sizeof (s_src)); |
326 | |
327 | IFA_LOCK(&ia->ia_ifa); |
328 | |
329 | /* |
330 | * Simply skip addresses reserved for CLAT46 |
331 | */ |
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); |
335 | goto next; |
336 | } |
337 | |
338 | /* |
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. |
342 | */ |
343 | if (ip6_select_src_strong_end && |
344 | ifp1 != ifp) { |
345 | SASEL_LOG("NEXT ia %s ifp1 %s address is not on outgoing " |
346 | "interface \n" , s_src, ifp1->if_xname); |
347 | goto next; |
348 | } |
349 | |
350 | /* |
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. |
355 | */ |
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); |
360 | goto next; |
361 | } |
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); |
368 | goto next; |
369 | } |
370 | /* avoid unusable addresses */ |
371 | if ((ia->ia6_flags & |
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); |
375 | goto next; |
376 | } |
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); |
380 | goto next; |
381 | } |
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); |
386 | goto next; |
387 | } |
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 */ |
391 | |
392 | if (ia_best == NULL) |
393 | REPLACE(IP6S_SRCRULE_0); |
394 | |
395 | /* Rule 2: Prefer appropriate scope */ |
396 | if (dst_scope < 0) |
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); |
407 | } |
408 | |
409 | /* |
410 | * Rule 3: Avoid deprecated addresses. Note that the case of |
411 | * !ip6_use_deprecated is already rejected above. |
412 | */ |
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); |
419 | |
420 | /* |
421 | * RFC 4429 says that optimistic addresses are equivalent to |
422 | * deprecated addresses, so avoid them here. |
423 | */ |
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); |
430 | |
431 | /* Rule 4: Prefer home addresses */ |
432 | /* |
433 | * XXX: This is a TODO. We should probably merge the MIP6 |
434 | * case above. |
435 | */ |
436 | |
437 | /* Rule 5: Prefer outgoing interface */ |
438 | /* |
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. |
443 | */ |
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); |
449 | } |
450 | |
451 | /* |
452 | * Rule 6: Prefer matching label |
453 | * Note that best_policy should be non-NULL here. |
454 | */ |
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); |
465 | } |
466 | |
467 | /* |
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. |
472 | */ |
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); |
477 | else |
478 | NEXTSRC(IP6S_SRCRULE_7); |
479 | } |
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); |
484 | else |
485 | REPLACE(IP6S_SRCRULE_7); |
486 | } |
487 | |
488 | /* |
489 | * Rule 7x: prefer addresses on alive interfaces. |
490 | * This is a KAME specific rule. |
491 | */ |
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); |
498 | |
499 | /* |
500 | * Rule 8: Use longest matching prefix. |
501 | */ |
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); |
507 | |
508 | /* |
509 | * Last resort: just keep the current candidate. |
510 | * Or, do we need more rules? |
511 | */ |
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++; |
517 | goto next; |
518 | } |
519 | SASEL_LOG("NEXT ia %s ifp1 %s last resort\n" , |
520 | s_src, ifp1->if_xname); |
521 | IFA_UNLOCK(&ia->ia_ifa); |
522 | continue; |
523 | |
524 | replace: |
525 | /* |
526 | * Ignore addresses on secondary interfaces that are marked |
527 | * expensive |
528 | */ |
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++; |
534 | goto next; |
535 | } |
536 | bestrule = srcrule; |
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); |
547 | if (ia_best != NULL) |
548 | IFA_REMREF(&ia_best->ia_ifa); |
549 | ia_best = ia; |
550 | continue; |
551 | |
552 | next: |
553 | IFA_UNLOCK(&ia->ia_ifa); |
554 | continue; |
555 | |
556 | out: |
557 | IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for ia_best */ |
558 | IFA_UNLOCK(&ia->ia_ifa); |
559 | if (ia_best != NULL) |
560 | IFA_REMREF(&ia_best->ia_ifa); |
561 | ia_best = ia; |
562 | break; |
563 | } |
564 | |
565 | lck_rw_done(&in6_ifaddr_rwlock); |
566 | |
567 | if ((ia = ia_best) == NULL) { |
568 | if (*errorp == 0) |
569 | *errorp = EADDRNOTAVAIL; |
570 | src_storage = NULL; |
571 | goto done; |
572 | } |
573 | |
574 | if (sifp != NULL) { |
575 | *sifp = ia->ia_ifa.ifa_ifp; |
576 | ifnet_reference(*sifp); |
577 | } |
578 | |
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); |
584 | |
585 | if (ifapp != NULL) |
586 | *ifapp = &ia->ia_ifa; |
587 | else |
588 | IFA_REMREF(&ia->ia_ifa); |
589 | |
590 | done: |
591 | if (srcsel_debug) { |
592 | (void) inet_ntop(AF_INET6, &dst, s_dst, sizeof (s_src)); |
593 | |
594 | tmp = (src_storage != NULL) ? src_storage : &in6addr_any; |
595 | (void) inet_ntop(AF_INET6, tmp, s_src, sizeof (s_src)); |
596 | |
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); |
599 | } |
600 | |
601 | return (src_storage); |
602 | } |
603 | |
604 | /* |
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. |
608 | */ |
609 | struct in6_addr * |
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, |
613 | int *errorp) |
614 | { |
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; |
623 | |
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; |
629 | |
630 | *errorp = 0; |
631 | if (ifpp != NULL) |
632 | *ifpp = NULL; |
633 | |
634 | if (inp != NULL) { |
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; |
645 | } else { |
646 | mopts = NULL; |
647 | /* Allow the kernel to retransmit packets. */ |
648 | ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED | |
649 | IP6OAF_AWDL_UNRESTRICTED; |
650 | } |
651 | |
652 | if (ip6oa.ip6oa_boundif != IFSCOPE_NONE) |
653 | ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; |
654 | |
655 | /* |
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. |
660 | */ |
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; |
665 | |
666 | /* get the outgoing interface */ |
667 | if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ip6oa, |
668 | &ifp)) != 0) { |
669 | src_storage = NULL; |
670 | goto done; |
671 | } |
672 | |
673 | /* |
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. |
679 | */ |
680 | bzero(&srcsock, sizeof (srcsock)); |
681 | srcsock.sin6_family = AF_INET6; |
682 | srcsock.sin6_len = sizeof (srcsock); |
683 | srcsock.sin6_addr = pi->ipi6_addr; |
684 | if (ifp != NULL) { |
685 | *errorp = in6_setscope(&srcsock.sin6_addr, ifp, NULL); |
686 | if (*errorp != 0) { |
687 | src_storage = NULL; |
688 | goto done; |
689 | } |
690 | } |
691 | ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *) |
692 | (&srcsock)); |
693 | if (ia6 == NULL) { |
694 | *errorp = EADDRNOTAVAIL; |
695 | src_storage = NULL; |
696 | goto done; |
697 | } |
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; |
704 | src_storage = NULL; |
705 | goto done; |
706 | } |
707 | |
708 | *src_storage = satosin6(&ia6->ia_addr)->sin6_addr; |
709 | IFA_UNLOCK(&ia6->ia_ifa); |
710 | IFA_REMREF(&ia6->ia_ifa); |
711 | goto done; |
712 | } |
713 | |
714 | /* |
715 | * Otherwise, if the socket has already bound the source, just use it. |
716 | */ |
717 | if (inp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { |
718 | src_storage = &inp->in6p_laddr; |
719 | goto done; |
720 | } |
721 | |
722 | /* |
723 | * If the address is not specified, choose the best one based on |
724 | * the outgoing interface and the destination address. |
725 | */ |
726 | /* get the outgoing interface */ |
727 | if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ip6oa, |
728 | &ifp)) != 0) { |
729 | src_storage = NULL; |
730 | goto done; |
731 | } |
732 | |
733 | VERIFY(ifp != NULL); |
734 | |
735 | if (opts == NULL || |
736 | opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) { |
737 | prefer_tempaddr = ip6_prefer_tempaddr; |
738 | } else if (opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_NOTPREFER) { |
739 | prefer_tempaddr = 0; |
740 | } else |
741 | prefer_tempaddr = 1; |
742 | |
743 | if (prefer_tempaddr) |
744 | hint_mask |= IPV6_SRCSEL_HINT_PREFER_TMPADDR; |
745 | |
746 | if (in6_selectsrc_core(dstsock, hint_mask, ifp, inp_debug, src_storage, |
747 | &sifp, errorp, NULL) == NULL) { |
748 | src_storage = NULL; |
749 | goto done; |
750 | } |
751 | |
752 | VERIFY(sifp != NULL); |
753 | |
754 | if (inp && inp_restricted_send(inp, sifp)) { |
755 | src_storage = NULL; |
756 | *errorp = EHOSTUNREACH; |
757 | ifnet_release(sifp); |
758 | goto done; |
759 | } else { |
760 | ifnet_release(sifp); |
761 | } |
762 | |
763 | done: |
764 | if (ifpp != NULL) { |
765 | /* if ifp is non-NULL, refcnt held in in6_selectif() */ |
766 | *ifpp = ifp; |
767 | } else if (ifp != NULL) { |
768 | ifnet_release(ifp); |
769 | } |
770 | return (src_storage); |
771 | } |
772 | |
773 | /* |
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 |
780 | * the IPv6 layer. |
781 | * |
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. |
786 | * |
787 | * clone - meaningful only for bsdi and freebsd |
788 | */ |
789 | static int |
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) |
795 | { |
796 | int error = 0; |
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); |
807 | |
808 | if (retifp != NULL) |
809 | *retifp = NULL; |
810 | |
811 | if (retrt != NULL) |
812 | *retrt = NULL; |
813 | |
814 | if (ip6_select_srcif_debug) { |
815 | struct in6_addr src; |
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)); |
819 | } |
820 | |
821 | /* |
822 | * If the destination address is UNSPECIFIED addr, bail out. |
823 | */ |
824 | if (IN6_IS_ADDR_UNSPECIFIED(dst)) { |
825 | error = EHOSTUNREACH; |
826 | goto done; |
827 | } |
828 | |
829 | /* |
830 | * Perform source interface selection only if Scoped Routing |
831 | * is enabled and a source address that isn't unspecified. |
832 | */ |
833 | select_srcif = (srcsock != NULL && |
834 | !IN6_IS_ADDR_UNSPECIFIED(&srcsock->sin6_addr)); |
835 | |
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); |
839 | } |
840 | |
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) { |
844 | /* |
845 | * If IPV6_PKTINFO takes precedence over IPV6_BOUND_IF. |
846 | */ |
847 | ifscope = pi->ipi6_ifindex; |
848 | ifnet_head_lock_shared(); |
849 | /* ifp may be NULL if detached or out of range */ |
850 | ifp = ifp0 = |
851 | ((ifscope <= if_index) ? ifindex2ifnet[ifscope] : NULL); |
852 | ifnet_head_done(); |
853 | if (norouteok || retrt == NULL || IN6_IS_ADDR_MULTICAST(dst)) { |
854 | /* |
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, |
858 | * it's an error. |
859 | */ |
860 | if (ifp == NULL) |
861 | error = EHOSTUNREACH; |
862 | goto done; |
863 | } else { |
864 | goto getsrcif; |
865 | } |
866 | } |
867 | |
868 | /* |
869 | * If the destination address is a multicast address and the outgoing |
870 | * interface for the address is specified by the caller, use it. |
871 | */ |
872 | if (IN6_IS_ADDR_MULTICAST(dst) && mopts != NULL) { |
873 | IM6O_LOCK(mopts); |
874 | if ((ifp = ifp0 = mopts->im6o_multicast_ifp) != NULL) { |
875 | IM6O_UNLOCK(mopts); |
876 | goto done; /* we do not need a route for multicast. */ |
877 | } |
878 | IM6O_UNLOCK(mopts); |
879 | } |
880 | |
881 | getsrcif: |
882 | /* |
883 | * If the outgoing interface was not set via IPV6_BOUND_IF or |
884 | * IPV6_PKTINFO, use the scope ID in the destination address. |
885 | */ |
886 | if (ifscope == IFSCOPE_NONE) |
887 | ifscope = dstsock->sin6_scope_id; |
888 | |
889 | /* |
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. |
896 | */ |
897 | if (!select_srcif) { |
898 | goto getroute; |
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) |
902 | local_dst = TRUE; |
903 | ifa = ro->ro_srcia; |
904 | IFA_ADDREF(ifa); /* for caller */ |
905 | goto getroute; |
906 | } |
907 | |
908 | /* |
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. |
914 | */ |
915 | if (ifscope != IFSCOPE_NONE || (ro != NULL && ro->ro_rt != NULL)) { |
916 | unsigned int scope = ifscope; |
917 | struct ifnet *rt_ifp; |
918 | |
919 | rt_ifp = (ro->ro_rt != NULL) ? ro->ro_rt->rt_ifp : NULL; |
920 | |
921 | /* |
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. |
929 | */ |
930 | if (scope == IFSCOPE_NONE) { |
931 | scope = rt_ifp->if_index; |
932 | if (scope != get_primary_ifscope(AF_INET6) && |
933 | ROUTE_UNUSABLE(ro)) |
934 | scope = get_primary_ifscope(AF_INET6); |
935 | } |
936 | |
937 | ifa = (struct ifaddr *) |
938 | ifa_foraddr6_scoped(&srcsock->sin6_addr, scope); |
939 | |
940 | /* |
941 | * If we are forwarding and proxying prefix(es), see if the |
942 | * source address is one of ours and is a proxied address; |
943 | * if so, use it. |
944 | */ |
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))) { |
950 | IFA_REMREF(ifa); |
951 | ifa = NULL; |
952 | } |
953 | } |
954 | |
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 " |
958 | "ro_if %s\n" , |
959 | __func__, |
960 | s_src, s_dst, ifscope, |
961 | scope, if_name(ifa->ifa_ifp), |
962 | if_name(rt_ifp)); |
963 | } else { |
964 | printf("%s %s->%s ifscope %d->%d ifa_if %s\n" , |
965 | __func__, |
966 | s_src, s_dst, ifscope, scope, |
967 | if_name(ifa->ifa_ifp)); |
968 | } |
969 | } |
970 | } |
971 | |
972 | /* |
973 | * Slow path; search for an interface having the corresponding source |
974 | * IPv6 address if the scope was not specified by the caller, and: |
975 | * |
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 |
980 | * found interface. |
981 | */ |
982 | if (ifa == NULL && ifscope == IFSCOPE_NONE) { |
983 | struct ifaddr *ifadst; |
984 | |
985 | /* Check if the destination address is one of ours */ |
986 | ifadst = (struct ifaddr *)ifa_foraddr6(&dstsock->sin6_addr); |
987 | if (ifadst != NULL) { |
988 | local_dst = TRUE; |
989 | IFA_REMREF(ifadst); |
990 | } |
991 | |
992 | ifa = (struct ifaddr *)ifa_foraddr6(&srcsock->sin6_addr); |
993 | |
994 | if (ip6_select_srcif_debug && ifa != NULL) { |
995 | printf("%s %s->%s ifscope %d ifa_if %s\n" , |
996 | __func__, |
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" , |
1000 | __func__, |
1001 | s_src, s_dst, ifscope); |
1002 | } |
1003 | } |
1004 | |
1005 | getroute: |
1006 | if (ifa != NULL && !proxied_ifa && !local_dst) |
1007 | ifscope = ifa->ifa_ifp->if_index; |
1008 | |
1009 | /* |
1010 | * If the next hop address for the packet is specified by the caller, |
1011 | * use it as the gateway. |
1012 | */ |
1013 | if (opts != NULL && opts->ip6po_nexthop != NULL) { |
1014 | struct route_in6 *ron; |
1015 | |
1016 | sin6_next = satosin6(opts->ip6po_nexthop); |
1017 | |
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? */ |
1021 | goto done; |
1022 | } |
1023 | |
1024 | /* |
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. |
1027 | */ |
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); |
1039 | |
1040 | ROUTE_RELEASE(ron); |
1041 | *satosin6(&ron->ro_dst) = *sin6_next; |
1042 | } |
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); |
1053 | |
1054 | ROUTE_RELEASE(ron); |
1055 | error = EHOSTUNREACH; |
1056 | goto done; |
1057 | } |
1058 | } |
1059 | route = ron; |
1060 | ifp = ifp0 = ron->ro_rt->rt_ifp; |
1061 | |
1062 | /* |
1063 | * When cloning is required, try to allocate a route to the |
1064 | * destination so that the caller can store path MTU |
1065 | * information. |
1066 | */ |
1067 | if (!clone) { |
1068 | if (select_srcif) { |
1069 | /* Keep the route locked */ |
1070 | goto validateroute; |
1071 | } |
1072 | RT_UNLOCK(ron->ro_rt); |
1073 | goto done; |
1074 | } |
1075 | RT_UNLOCK(ron->ro_rt); |
1076 | } |
1077 | |
1078 | /* |
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. |
1082 | */ |
1083 | if (ro == NULL) |
1084 | goto done; |
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); |
1094 | |
1095 | ROUTE_RELEASE(ro); |
1096 | } |
1097 | if (ro->ro_rt == NULL) { |
1098 | struct sockaddr_in6 *sa6; |
1099 | |
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); |
1111 | } else { |
1112 | rtalloc_scoped((struct route *)ro, ifscope); |
1113 | } |
1114 | if (ro->ro_rt != NULL) |
1115 | RT_LOCK_SPIN(ro->ro_rt); |
1116 | } |
1117 | |
1118 | /* |
1119 | * Do not care about the result if we have the nexthop |
1120 | * explicitly specified (in case we're asked to clone.) |
1121 | */ |
1122 | if (opts != NULL && opts->ip6po_nexthop != NULL) { |
1123 | if (ro->ro_rt != NULL) |
1124 | RT_UNLOCK(ro->ro_rt); |
1125 | goto done; |
1126 | } |
1127 | |
1128 | if (ro->ro_rt != NULL) { |
1129 | RT_LOCK_ASSERT_HELD(ro->ro_rt); |
1130 | ifp = ifp0 = ro->ro_rt->rt_ifp; |
1131 | } else { |
1132 | error = EHOSTUNREACH; |
1133 | } |
1134 | route = ro; |
1135 | |
1136 | validateroute: |
1137 | if (select_srcif) { |
1138 | boolean_t has_route = (route != NULL && route->ro_rt != NULL); |
1139 | boolean_t srcif_selected = FALSE; |
1140 | |
1141 | if (has_route) |
1142 | RT_LOCK_ASSERT_HELD(route->ro_rt); |
1143 | /* |
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. |
1148 | */ |
1149 | if (has_route && (ifa == NULL || |
1150 | (ifa->ifa_ifp != ifp && ifp != lo_ifp) || |
1151 | !(route->ro_rt->rt_flags & RTF_UP))) { |
1152 | /* |
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. |
1157 | */ |
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; |
1162 | } else { |
1163 | if (ip6_select_srcif_debug) { |
1164 | if (ifa != NULL) { |
1165 | printf("%s->%s ifscope %d " |
1166 | "ro_if %s != ifa_if %s " |
1167 | "(cached route cleared)\n" , |
1168 | s_src, s_dst, |
1169 | ifscope, if_name(ifp), |
1170 | if_name(ifa->ifa_ifp)); |
1171 | } else { |
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)); |
1176 | } |
1177 | } |
1178 | RT_UNLOCK(route->ro_rt); |
1179 | ROUTE_RELEASE(route); |
1180 | error = EHOSTUNREACH; |
1181 | /* Undo the settings done above */ |
1182 | route = NULL; |
1183 | ifp = NULL; /* ditch ifp; keep ifp0 */ |
1184 | has_route = FALSE; |
1185 | } |
1186 | } else if (has_route) { |
1187 | srcif_selected = TRUE; |
1188 | } |
1189 | |
1190 | if (srcif_selected) { |
1191 | VERIFY(has_route); |
1192 | if (ifa != route->ro_srcia || |
1193 | !(route->ro_flags & ROF_SRCIF_SELECTED)) { |
1194 | RT_CONVERT_LOCK(route->ro_rt); |
1195 | if (ifa != NULL) |
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); |
1202 | } |
1203 | RT_UNLOCK(route->ro_rt); |
1204 | } |
1205 | } else { |
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) { |
1211 | /* |
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.) |
1217 | */ |
1218 | if (!(ifp->if_flags & IFF_LOOPBACK) && ifp->if_index != |
1219 | opts->ip6po_pktinfo->ipi6_ifindex) { |
1220 | error = EHOSTUNREACH; |
1221 | goto done; |
1222 | } |
1223 | } |
1224 | } |
1225 | |
1226 | done: |
1227 | /* |
1228 | * Check for interface restrictions. |
1229 | */ |
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))) |
1239 | |
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); |
1246 | route = NULL; |
1247 | } |
1248 | ifp = NULL; /* ditch ifp; keep ifp0 */ |
1249 | error = EHOSTUNREACH; |
1250 | ip6oa->ip6oa_retflags |= IP6OARF_IFDENIED; |
1251 | } |
1252 | #undef CHECK_RESTRICTIONS |
1253 | |
1254 | /* |
1255 | * If the interface is disabled for IPv6, then ENETDOWN error. |
1256 | */ |
1257 | if (error == 0 && |
1258 | ifp != NULL && (ifp->if_eflags & IFEF_IPV6_DISABLED)) { |
1259 | error = ENETDOWN; |
1260 | } |
1261 | |
1262 | if (ifp == NULL && (route == NULL || route->ro_rt == NULL)) { |
1263 | /* |
1264 | * This can happen if the caller did not pass a cached route |
1265 | * nor any other hints. We treat this case an error. |
1266 | */ |
1267 | error = EHOSTUNREACH; |
1268 | } |
1269 | if (error == EHOSTUNREACH || error == ENETDOWN) |
1270 | ip6stat.ip6s_noroute++; |
1271 | |
1272 | /* |
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. |
1276 | */ |
1277 | ifp = ifp0; |
1278 | if (retifp != NULL) { |
1279 | if (ifp != NULL) |
1280 | ifnet_reference(ifp); /* for caller */ |
1281 | *retifp = ifp; |
1282 | } |
1283 | |
1284 | if (retsrcia != NULL) { |
1285 | if (ifa != NULL) |
1286 | IFA_ADDREF(ifa); /* for caller */ |
1287 | *retsrcia = (struct in6_ifaddr *)ifa; |
1288 | } |
1289 | |
1290 | if (error == 0) { |
1291 | if (retrt != NULL && route != NULL) |
1292 | *retrt = route->ro_rt; /* ro_rt may be NULL */ |
1293 | } |
1294 | if (ip6_select_srcif_debug) { |
1295 | printf("%s %s->%s ifscope %d ifa_if %s ro_if %s (error=%d)\n" , |
1296 | __func__, |
1297 | s_src, s_dst, ifscope, |
1298 | (ifa != NULL) ? if_name(ifa->ifa_ifp) : "NONE" , |
1299 | (ifp != NULL) ? if_name(ifp) : "NONE" , error); |
1300 | } |
1301 | |
1302 | if (ifa != NULL) |
1303 | IFA_REMREF(ifa); |
1304 | |
1305 | return (error); |
1306 | } |
1307 | |
1308 | /* |
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. |
1312 | */ |
1313 | int |
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) |
1317 | { |
1318 | int err = 0; |
1319 | struct route_in6 sro; |
1320 | struct rtentry *rt = NULL; |
1321 | |
1322 | if (ro == NULL) { |
1323 | bzero(&sro, sizeof (sro)); |
1324 | ro = &sro; |
1325 | } |
1326 | |
1327 | if ((err = selectroute(NULL, dstsock, opts, mopts, NULL, ro, retifp, |
1328 | &rt, 0, 1, ip6oa)) != 0) |
1329 | goto done; |
1330 | |
1331 | /* |
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 |
1335 | * scenario: |
1336 | * - install a rejected route for a scoped address prefix |
1337 | * (like fe80::/10) |
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. |
1347 | */ |
1348 | if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) { |
1349 | err = ((rt->rt_flags & RTF_HOST) ? EHOSTUNREACH : ENETUNREACH); |
1350 | goto done; |
1351 | } |
1352 | |
1353 | /* |
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 |
1358 | * addresses.) |
1359 | */ |
1360 | if (rt != NULL && rt->rt_ifa != NULL && rt->rt_ifa->ifa_ifp != NULL && |
1361 | retifp != NULL) { |
1362 | ifnet_reference(rt->rt_ifa->ifa_ifp); |
1363 | if (*retifp != NULL) |
1364 | ifnet_release(*retifp); |
1365 | *retifp = rt->rt_ifa->ifa_ifp; |
1366 | } |
1367 | |
1368 | done: |
1369 | if (ro == &sro) { |
1370 | VERIFY(rt == NULL || rt == ro->ro_rt); |
1371 | ROUTE_RELEASE(ro); |
1372 | } |
1373 | |
1374 | /* |
1375 | * retifp might point to a valid ifp with a reference held; |
1376 | * caller is responsible for releasing it if non-NULL. |
1377 | */ |
1378 | return (err); |
1379 | } |
1380 | |
1381 | /* |
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. |
1385 | * |
1386 | * clone - meaningful only for bsdi and freebsd |
1387 | */ |
1388 | int |
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) |
1393 | { |
1394 | |
1395 | return (selectroute(srcsock, dstsock, opts, mopts, retsrcia, ro, retifp, |
1396 | retrt, clone, 0, ip6oa)); |
1397 | } |
1398 | |
1399 | /* |
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. |
1405 | */ |
1406 | int |
1407 | in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp) |
1408 | { |
1409 | if (in6p && in6p->in6p_hops >= 0) { |
1410 | return (in6p->in6p_hops); |
1411 | } else if (NULL != ifp) { |
1412 | u_int8_t chlim; |
1413 | struct nd_ifinfo *ndi = ND_IFINFO(ifp); |
1414 | if (ndi && ndi->initialized) { |
1415 | /* access chlim without lock, for performance */ |
1416 | chlim = ndi->chlim; |
1417 | } else { |
1418 | chlim = ip6_defhlim; |
1419 | } |
1420 | return (chlim); |
1421 | } |
1422 | |
1423 | return (ip6_defhlim); |
1424 | } |
1425 | |
1426 | /* |
1427 | * XXX: this is borrowed from in6_pcbbind(). If possible, we should |
1428 | * share this function by all *bsd*... |
1429 | */ |
1430 | int |
1431 | in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct proc *p, |
1432 | int locked) |
1433 | { |
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; |
1438 | bool found; |
1439 | struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; |
1440 | kauth_cred_t cred; |
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); |
1447 | } |
1448 | |
1449 | /* |
1450 | * Check if a local port was assigned to the inp while |
1451 | * this thread was waiting for the pcbinfo lock |
1452 | */ |
1453 | if (inp->inp_lport != 0) { |
1454 | VERIFY(inp->inp_flags2 & INP2_INHASHLIST); |
1455 | lck_rw_done(pcbinfo->ipi_lock); |
1456 | |
1457 | /* |
1458 | * It is not an error if another thread allocated |
1459 | * a port |
1460 | */ |
1461 | return (0); |
1462 | } |
1463 | } |
1464 | |
1465 | /* XXX: this is redundant when called from in6_pcbbind */ |
1466 | if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) |
1467 | wild = INPLOOKUP_WILDCARD; |
1468 | |
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); |
1477 | if (error != 0) { |
1478 | if (!locked) |
1479 | lck_rw_done(pcbinfo->ipi_lock); |
1480 | return (error); |
1481 | } |
1482 | first = ipport_lowfirstauto; /* 1023 */ |
1483 | last = ipport_lowlastauto; /* 600 */ |
1484 | lastport = &pcbinfo->ipi_lastlow; |
1485 | } else { |
1486 | first = ipport_firstauto; /* sysctl */ |
1487 | last = ipport_lastauto; |
1488 | lastport = &pcbinfo->ipi_lastport; |
1489 | } |
1490 | /* |
1491 | * Simple check to ensure all ports are not used up causing |
1492 | * a deadlock here. |
1493 | */ |
1494 | found = false; |
1495 | if (first > last) { |
1496 | /* counting down */ |
1497 | count = first - last; |
1498 | counting_down = TRUE; |
1499 | } else { |
1500 | /* counting up */ |
1501 | count = last - first; |
1502 | counting_down = FALSE; |
1503 | } |
1504 | do { |
1505 | if (count-- < 0) { /* completely used? */ |
1506 | /* |
1507 | * Undo any address bind that may have |
1508 | * occurred above. |
1509 | */ |
1510 | inp->in6p_laddr = in6addr_any; |
1511 | inp->in6p_last_outifp = NULL; |
1512 | if (!locked) |
1513 | lck_rw_done(pcbinfo->ipi_lock); |
1514 | return (EAGAIN); |
1515 | } |
1516 | if (counting_down) { |
1517 | --*lastport; |
1518 | if (*lastport > first || *lastport < last) { |
1519 | *lastport = first; |
1520 | } |
1521 | } else { |
1522 | ++*lastport; |
1523 | if (*lastport < first || *lastport > last) |
1524 | *lastport = first; |
1525 | } |
1526 | lport = htons(*lastport); |
1527 | found = (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr, |
1528 | lport, wild) == NULL); |
1529 | } while (!found); |
1530 | |
1531 | inp->inp_lport = lport; |
1532 | inp->inp_flags |= INP_ANONPORT; |
1533 | |
1534 | if (in_pcbinshash(inp, 1) != 0) { |
1535 | inp->in6p_laddr = in6addr_any; |
1536 | inp->in6p_last_outifp = NULL; |
1537 | |
1538 | inp->inp_lport = 0; |
1539 | inp->inp_flags &= ~INP_ANONPORT; |
1540 | if (!locked) |
1541 | lck_rw_done(pcbinfo->ipi_lock); |
1542 | return (EAGAIN); |
1543 | } |
1544 | |
1545 | if (!locked) |
1546 | lck_rw_done(pcbinfo->ipi_lock); |
1547 | return (0); |
1548 | } |
1549 | |
1550 | /* |
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. |
1555 | */ |
1556 | struct addrsel_policyent { |
1557 | TAILQ_ENTRY(addrsel_policyent) ape_entry; |
1558 | struct in6_addrpolicy ape_policy; |
1559 | }; |
1560 | |
1561 | TAILQ_HEAD(addrsel_policyhead, addrsel_policyent); |
1562 | |
1563 | struct addrsel_policyhead addrsel_policytab; |
1564 | |
1565 | static void |
1566 | init_policy_queue(void) |
1567 | { |
1568 | TAILQ_INIT(&addrsel_policytab); |
1569 | } |
1570 | |
1571 | void |
1572 | addrsel_policy_init(void) |
1573 | { |
1574 | /* |
1575 | * Default address selection policy based on RFC 6724. |
1576 | */ |
1577 | static const struct in6_addrpolicy defaddrsel[] = { |
1578 | /* Loopback -- prefix=::1/128, precedence=50, label=0 */ |
1579 | { |
1580 | .addr = { |
1581 | .sin6_family = AF_INET6, |
1582 | .sin6_addr = IN6ADDR_LOOPBACK_INIT, |
1583 | .sin6_len = sizeof (struct sockaddr_in6) |
1584 | }, |
1585 | .addrmask = { |
1586 | .sin6_family = AF_INET6, |
1587 | .sin6_addr = IN6MASK128, |
1588 | .sin6_len = sizeof (struct sockaddr_in6) |
1589 | }, |
1590 | .preced = 50, |
1591 | .label = 0 |
1592 | }, |
1593 | |
1594 | /* Unspecified -- prefix=::/0, precedence=40, label=1 */ |
1595 | { |
1596 | .addr = { |
1597 | .sin6_family = AF_INET6, |
1598 | .sin6_addr = IN6ADDR_ANY_INIT, |
1599 | .sin6_len = sizeof (struct sockaddr_in6) |
1600 | }, |
1601 | .addrmask = { |
1602 | .sin6_family = AF_INET6, |
1603 | .sin6_addr = IN6MASK0, |
1604 | .sin6_len = sizeof (struct sockaddr_in6) |
1605 | }, |
1606 | .preced = 40, |
1607 | .label = 1 |
1608 | }, |
1609 | |
1610 | /* IPv4 Mapped -- prefix=::ffff:0:0/96, precedence=35, label=4 */ |
1611 | { |
1612 | .addr = { |
1613 | .sin6_family = AF_INET6, |
1614 | .sin6_addr = IN6ADDR_V4MAPPED_INIT, |
1615 | .sin6_len = sizeof (struct sockaddr_in6) |
1616 | }, |
1617 | .addrmask = { |
1618 | .sin6_family = AF_INET6, |
1619 | .sin6_addr = IN6MASK96, |
1620 | .sin6_len = sizeof (struct sockaddr_in6) |
1621 | }, |
1622 | .preced = 35, |
1623 | .label = 4 |
1624 | }, |
1625 | |
1626 | /* 6to4 -- prefix=2002::/16, precedence=30, label=2 */ |
1627 | { |
1628 | .addr = { |
1629 | .sin6_family = AF_INET6, |
1630 | .sin6_addr = {{{ 0x20, 0x02 }}}, |
1631 | .sin6_len = sizeof (struct sockaddr_in6) |
1632 | }, |
1633 | .addrmask = { |
1634 | .sin6_family = AF_INET6, |
1635 | .sin6_addr = IN6MASK16, |
1636 | .sin6_len = sizeof (struct sockaddr_in6) |
1637 | }, |
1638 | .preced = 30, |
1639 | .label = 2 |
1640 | }, |
1641 | |
1642 | /* Teredo -- prefix=2001::/32, precedence=5, label=5 */ |
1643 | { |
1644 | .addr = { |
1645 | .sin6_family = AF_INET6, |
1646 | .sin6_addr = {{{ 0x20, 0x01 }}}, |
1647 | .sin6_len = sizeof (struct sockaddr_in6) |
1648 | }, |
1649 | .addrmask = { |
1650 | .sin6_family = AF_INET6, |
1651 | .sin6_addr = IN6MASK32, |
1652 | .sin6_len = sizeof (struct sockaddr_in6) |
1653 | }, |
1654 | .preced = 5, |
1655 | .label = 5 |
1656 | }, |
1657 | |
1658 | /* Unique Local (ULA) -- prefix=fc00::/7, precedence=3, label=13 */ |
1659 | { |
1660 | .addr = { |
1661 | .sin6_family = AF_INET6, |
1662 | .sin6_addr = {{{ 0xfc }}}, |
1663 | .sin6_len = sizeof (struct sockaddr_in6) |
1664 | }, |
1665 | .addrmask = { |
1666 | .sin6_family = AF_INET6, |
1667 | .sin6_addr = IN6MASK7, |
1668 | .sin6_len = sizeof (struct sockaddr_in6) |
1669 | }, |
1670 | .preced = 3, |
1671 | .label = 13 |
1672 | }, |
1673 | |
1674 | /* IPv4 Compatible -- prefix=::/96, precedence=1, label=3 */ |
1675 | { |
1676 | .addr = { |
1677 | .sin6_family = AF_INET6, |
1678 | .sin6_addr = IN6ADDR_ANY_INIT, |
1679 | .sin6_len = sizeof (struct sockaddr_in6) |
1680 | }, |
1681 | .addrmask = { |
1682 | .sin6_family = AF_INET6, |
1683 | .sin6_addr = IN6MASK96, |
1684 | .sin6_len = sizeof (struct sockaddr_in6) |
1685 | }, |
1686 | .preced = 1, |
1687 | .label = 3 |
1688 | }, |
1689 | |
1690 | /* Site-local (deprecated) -- prefix=fec0::/10, precedence=1, label=11 */ |
1691 | { |
1692 | .addr = { |
1693 | .sin6_family = AF_INET6, |
1694 | .sin6_addr = {{{ 0xfe, 0xc0 }}}, |
1695 | .sin6_len = sizeof (struct sockaddr_in6) |
1696 | }, |
1697 | .addrmask = { |
1698 | .sin6_family = AF_INET6, |
1699 | .sin6_addr = IN6MASK16, |
1700 | .sin6_len = sizeof (struct sockaddr_in6) |
1701 | }, |
1702 | .preced = 1, |
1703 | .label = 11 |
1704 | }, |
1705 | |
1706 | /* 6bone (deprecated) -- prefix=3ffe::/16, precedence=1, label=12 */ |
1707 | { |
1708 | .addr = { |
1709 | .sin6_family = AF_INET6, |
1710 | .sin6_addr = {{{ 0x3f, 0xfe }}}, |
1711 | .sin6_len = sizeof (struct sockaddr_in6) |
1712 | }, |
1713 | .addrmask = { |
1714 | .sin6_family = AF_INET6, |
1715 | .sin6_addr = IN6MASK16, |
1716 | .sin6_len = sizeof (struct sockaddr_in6) |
1717 | }, |
1718 | .preced = 1, |
1719 | .label = 12 |
1720 | }, |
1721 | }; |
1722 | int i; |
1723 | |
1724 | init_policy_queue(); |
1725 | |
1726 | /* initialize the "last resort" policy */ |
1727 | bzero(&defaultaddrpolicy, sizeof (defaultaddrpolicy)); |
1728 | defaultaddrpolicy.label = ADDR_LABEL_NOTAPP; |
1729 | |
1730 | for (i = 0; i < sizeof (defaddrsel) / sizeof (defaddrsel[0]); i++) |
1731 | add_addrsel_policyent(&defaddrsel[i]); |
1732 | |
1733 | } |
1734 | |
1735 | struct in6_addrpolicy * |
1736 | in6_addrsel_lookup_policy(struct sockaddr_in6 *key) |
1737 | { |
1738 | struct in6_addrpolicy *match = NULL; |
1739 | |
1740 | ADDRSEL_LOCK(); |
1741 | match = match_addrsel_policy(key); |
1742 | |
1743 | if (match == NULL) |
1744 | match = &defaultaddrpolicy; |
1745 | else |
1746 | match->use++; |
1747 | ADDRSEL_UNLOCK(); |
1748 | |
1749 | return (match); |
1750 | } |
1751 | |
1752 | static struct in6_addrpolicy * |
1753 | match_addrsel_policy(struct sockaddr_in6 *key) |
1754 | { |
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; |
1759 | |
1760 | TAILQ_FOREACH(pent, &addrsel_policytab, ape_entry) { |
1761 | matchlen = 0; |
1762 | |
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++) { |
1769 | m = *mp; |
1770 | if ((*k & m) != *p) |
1771 | goto next; /* not match */ |
1772 | if (m == 0xff) /* short cut for a typical case */ |
1773 | matchlen += 8; |
1774 | else { |
1775 | while (m >= 0x80) { |
1776 | matchlen++; |
1777 | m <<= 1; |
1778 | } |
1779 | } |
1780 | } |
1781 | |
1782 | /* matched. check if this is better than the current best. */ |
1783 | if (bestpol == NULL || |
1784 | matchlen > bestmatchlen) { |
1785 | bestpol = pol; |
1786 | bestmatchlen = matchlen; |
1787 | } |
1788 | |
1789 | next: |
1790 | continue; |
1791 | } |
1792 | |
1793 | return (bestpol); |
1794 | } |
1795 | |
1796 | static int |
1797 | add_addrsel_policyent(const struct in6_addrpolicy *newpolicy) |
1798 | { |
1799 | struct addrsel_policyent *new, *pol; |
1800 | |
1801 | MALLOC(new, struct addrsel_policyent *, sizeof (*new), M_IFADDR, |
1802 | M_WAITOK); |
1803 | |
1804 | ADDRSEL_LOCK(); |
1805 | |
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)) { |
1812 | ADDRSEL_UNLOCK(); |
1813 | FREE(new, M_IFADDR); |
1814 | return (EEXIST); /* or override it? */ |
1815 | } |
1816 | } |
1817 | |
1818 | bzero(new, sizeof (*new)); |
1819 | |
1820 | /* XXX: should validate entry */ |
1821 | new->ape_policy = *newpolicy; |
1822 | |
1823 | TAILQ_INSERT_TAIL(&addrsel_policytab, new, ape_entry); |
1824 | ADDRSEL_UNLOCK(); |
1825 | |
1826 | return (0); |
1827 | } |
1828 | #ifdef ENABLE_ADDRSEL |
1829 | static int |
1830 | delete_addrsel_policyent(const struct in6_addrpolicy *key) |
1831 | { |
1832 | struct addrsel_policyent *pol; |
1833 | |
1834 | |
1835 | ADDRSEL_LOCK(); |
1836 | |
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)) { |
1843 | break; |
1844 | } |
1845 | } |
1846 | if (pol == NULL) { |
1847 | ADDRSEL_UNLOCK(); |
1848 | return (ESRCH); |
1849 | } |
1850 | |
1851 | TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry); |
1852 | FREE(pol, M_IFADDR); |
1853 | pol = NULL; |
1854 | ADDRSEL_UNLOCK(); |
1855 | |
1856 | return (0); |
1857 | } |
1858 | #endif /* ENABLE_ADDRSEL */ |
1859 | |
1860 | int |
1861 | walk_addrsel_policy(int (*callback)(const struct in6_addrpolicy *, void *), |
1862 | void *w) |
1863 | { |
1864 | struct addrsel_policyent *pol; |
1865 | int error = 0; |
1866 | |
1867 | ADDRSEL_LOCK(); |
1868 | TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) { |
1869 | if ((error = (*callback)(&pol->ape_policy, w)) != 0) { |
1870 | ADDRSEL_UNLOCK(); |
1871 | return (error); |
1872 | } |
1873 | } |
1874 | ADDRSEL_UNLOCK(); |
1875 | return (error); |
1876 | } |
1877 | /* |
1878 | * Subroutines to manage the address selection policy table via sysctl. |
1879 | */ |
1880 | struct walkarg { |
1881 | struct sysctl_req *w_req; |
1882 | }; |
1883 | |
1884 | |
1885 | static int |
1886 | dump_addrsel_policyent(const struct in6_addrpolicy *pol, void *arg) |
1887 | { |
1888 | int error = 0; |
1889 | struct walkarg *w = arg; |
1890 | |
1891 | error = SYSCTL_OUT(w->w_req, pol, sizeof (*pol)); |
1892 | |
1893 | return (error); |
1894 | } |
1895 | |
1896 | static int |
1897 | in6_src_sysctl SYSCTL_HANDLER_ARGS |
1898 | { |
1899 | #pragma unused(oidp, arg1, arg2) |
1900 | struct walkarg w; |
1901 | |
1902 | if (req->newptr) |
1903 | return (EPERM); |
1904 | bzero(&w, sizeof (w)); |
1905 | w.w_req = req; |
1906 | |
1907 | return (walk_addrsel_policy(dump_addrsel_policyent, &w)); |
1908 | } |
1909 | |
1910 | |
1911 | SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy, |
1912 | CTLFLAG_RD | CTLFLAG_LOCKED, in6_src_sysctl, "" ); |
1913 | int |
1914 | in6_src_ioctl(u_long cmd, caddr_t data) |
1915 | { |
1916 | int i; |
1917 | struct in6_addrpolicy ent0; |
1918 | |
1919 | if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY) |
1920 | return (EOPNOTSUPP); /* check for safety */ |
1921 | |
1922 | bcopy(data, &ent0, sizeof (ent0)); |
1923 | |
1924 | if (ent0.label == ADDR_LABEL_NOTAPP) |
1925 | return (EINVAL); |
1926 | /* check if the prefix mask is consecutive. */ |
1927 | if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0) |
1928 | return (EINVAL); |
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]; |
1933 | } |
1934 | ent0.use = 0; |
1935 | |
1936 | switch (cmd) { |
1937 | case SIOCAADDRCTL_POLICY: |
1938 | #ifdef ENABLE_ADDRSEL |
1939 | return (add_addrsel_policyent(&ent0)); |
1940 | #else |
1941 | return (ENOTSUP); |
1942 | #endif |
1943 | case SIOCDADDRCTL_POLICY: |
1944 | #ifdef ENABLE_ADDRSEL |
1945 | return (delete_addrsel_policyent(&ent0)); |
1946 | #else |
1947 | return (ENOTSUP); |
1948 | #endif |
1949 | } |
1950 | |
1951 | return (0); /* XXX: compromise compilers */ |
1952 | } |
1953 | |
1954 | /* |
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 |
1959 | * specification. |
1960 | * |
1961 | * this function should be nuked in the future, when we get rid of |
1962 | * embedded scopeid thing. |
1963 | * |
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. |
1968 | */ |
1969 | int |
1970 | in6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6, |
1971 | struct in6pcb *in6p, struct ifnet **ifpp, struct ip6_pktopts *opt) |
1972 | { |
1973 | struct ifnet *ifp = NULL; |
1974 | u_int32_t scopeid; |
1975 | struct ip6_pktopts *optp = NULL; |
1976 | |
1977 | *in6 = sin6->sin6_addr; |
1978 | scopeid = sin6->sin6_scope_id; |
1979 | if (ifpp != NULL) |
1980 | *ifpp = NULL; |
1981 | |
1982 | /* |
1983 | * don't try to read sin6->sin6_addr beyond here, since the caller may |
1984 | * ask us to overwrite existing sockaddr_in6 |
1985 | */ |
1986 | |
1987 | #ifdef ENABLE_DEFAULT_SCOPE |
1988 | if (scopeid == 0) |
1989 | scopeid = scope6_addr2default(in6); |
1990 | #endif |
1991 | |
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; |
1995 | |
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); |
2002 | } |
2003 | |
2004 | if (opt != NULL) |
2005 | optp = opt; |
2006 | else if (in6p != NULL) |
2007 | optp = in6p->in6p_outputopts; |
2008 | /* |
2009 | * KAME assumption: link id == interface id |
2010 | */ |
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 */ |
2015 | if (ifpp != NULL) { |
2016 | ifnet_head_lock_shared(); |
2017 | ifp = ifindex2ifnet[pi->ipi6_ifindex]; |
2018 | ifnet_head_done(); |
2019 | } |
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) { |
2026 | /* |
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.) |
2030 | */ |
2031 | if (if_index < scopeid) |
2032 | return (ENXIO); /* XXX EINVAL? */ |
2033 | |
2034 | /* ifp is needed here only if we're returning it */ |
2035 | if (ifpp != NULL) { |
2036 | ifnet_head_lock_shared(); |
2037 | ifp = ifindex2ifnet[scopeid]; |
2038 | ifnet_head_done(); |
2039 | } |
2040 | /* XXX assignment to 16bit from 32bit variable */ |
2041 | in6->s6_addr16[1] = htons(scopeid & 0xffff); |
2042 | } |
2043 | |
2044 | if (ifpp != NULL) { |
2045 | if (ifp != NULL) |
2046 | ifnet_reference(ifp); /* for caller */ |
2047 | *ifpp = ifp; |
2048 | } |
2049 | } |
2050 | |
2051 | return (0); |
2052 | } |
2053 | |
2054 | /* |
2055 | * generate standard sockaddr_in6 from embedded form. |
2056 | * touches sin6_addr and sin6_scope_id only. |
2057 | * |
2058 | * this function should be nuked in the future, when we get rid of |
2059 | * embedded scopeid thing. |
2060 | */ |
2061 | int |
2062 | in6_recoverscope( |
2063 | struct sockaddr_in6 *sin6, |
2064 | const struct in6_addr *in6, |
2065 | struct ifnet *ifp) |
2066 | { |
2067 | u_int32_t scopeid; |
2068 | |
2069 | sin6->sin6_addr = *in6; |
2070 | |
2071 | /* |
2072 | * don't try to read *in6 beyond here, since the caller may |
2073 | * ask us to overwrite existing sockaddr_in6 |
2074 | */ |
2075 | |
2076 | sin6->sin6_scope_id = 0; |
2077 | if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) { |
2078 | /* |
2079 | * KAME assumption: link id == interface id |
2080 | */ |
2081 | scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); |
2082 | if (scopeid) { |
2083 | /* |
2084 | * sanity check |
2085 | * |
2086 | * Since scopeid is unsigned, we only have to check it |
2087 | * against if_index |
2088 | */ |
2089 | if (if_index < scopeid) |
2090 | return (ENXIO); |
2091 | if (ifp && ifp->if_index != scopeid) |
2092 | return (ENXIO); |
2093 | sin6->sin6_addr.s6_addr16[1] = 0; |
2094 | sin6->sin6_scope_id = scopeid; |
2095 | } |
2096 | } |
2097 | |
2098 | return (0); |
2099 | } |
2100 | |