]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2016 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 | * Copyright (c) 1980, 1986, 1991, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)route.c 8.2 (Berkeley) 11/15/93 | |
61 | * $FreeBSD: src/sys/net/route.c,v 1.59.2.3 2001/07/29 19:18:02 ume Exp $ | |
62 | */ | |
63 | ||
64 | #include <sys/param.h> | |
65 | #include <sys/sysctl.h> | |
66 | #include <sys/systm.h> | |
67 | #include <sys/malloc.h> | |
68 | #include <sys/mbuf.h> | |
69 | #include <sys/socket.h> | |
70 | #include <sys/domain.h> | |
71 | #include <sys/stat.h> | |
72 | #include <sys/ubc.h> | |
73 | #include <sys/vnode.h> | |
74 | #include <sys/syslog.h> | |
75 | #include <sys/queue.h> | |
76 | #include <sys/mcache.h> | |
77 | #include <sys/protosw.h> | |
78 | #include <sys/kernel.h> | |
79 | #include <kern/locks.h> | |
80 | #include <kern/zalloc.h> | |
81 | ||
82 | #include <net/dlil.h> | |
83 | #include <net/if.h> | |
84 | #include <net/route.h> | |
85 | #include <net/ntstat.h> | |
86 | ||
87 | #include <netinet/in.h> | |
88 | #include <netinet/in_var.h> | |
89 | #include <netinet/ip_var.h> | |
90 | #include <netinet/ip6.h> | |
91 | ||
92 | #if INET6 | |
93 | #include <netinet6/ip6_var.h> | |
94 | #include <netinet6/in6_var.h> | |
95 | #include <netinet6/nd6.h> | |
96 | #endif /* INET6 */ | |
97 | ||
98 | #include <net/if_dl.h> | |
99 | ||
100 | #include <libkern/OSAtomic.h> | |
101 | #include <libkern/OSDebug.h> | |
102 | ||
103 | #include <pexpert/pexpert.h> | |
104 | ||
105 | #if CONFIG_MACF | |
106 | #include <sys/kauth.h> | |
107 | #endif | |
108 | ||
109 | /* | |
110 | * Synchronization notes: | |
111 | * | |
112 | * Routing entries fall under two locking domains: the global routing table | |
113 | * lock (rnh_lock) and the per-entry lock (rt_lock); the latter is a mutex that | |
114 | * resides (statically defined) in the rtentry structure. | |
115 | * | |
116 | * The locking domains for routing are defined as follows: | |
117 | * | |
118 | * The global routing lock is used to serialize all accesses to the radix | |
119 | * trees defined by rt_tables[], as well as the tree of masks. This includes | |
120 | * lookups, insertions and removals of nodes to/from the respective tree. | |
121 | * It is also used to protect certain fields in the route entry that aren't | |
122 | * often modified and/or require global serialization (more details below.) | |
123 | * | |
124 | * The per-route entry lock is used to serialize accesses to several routing | |
125 | * entry fields (more details below.) Acquiring and releasing this lock is | |
126 | * done via RT_LOCK() and RT_UNLOCK() routines. | |
127 | * | |
128 | * In cases where both rnh_lock and rt_lock must be held, the former must be | |
129 | * acquired first in order to maintain lock ordering. It is not a requirement | |
130 | * that rnh_lock be acquired first before rt_lock, but in case both must be | |
131 | * acquired in succession, the correct lock ordering must be followed. | |
132 | * | |
133 | * The fields of the rtentry structure are protected in the following way: | |
134 | * | |
135 | * rt_nodes[] | |
136 | * | |
137 | * - Routing table lock (rnh_lock). | |
138 | * | |
139 | * rt_parent, rt_mask, rt_llinfo_free, rt_tree_genid | |
140 | * | |
141 | * - Set once during creation and never changes; no locks to read. | |
142 | * | |
143 | * rt_flags, rt_genmask, rt_llinfo, rt_rmx, rt_refcnt, rt_gwroute | |
144 | * | |
145 | * - Routing entry lock (rt_lock) for read/write access. | |
146 | * | |
147 | * - Some values of rt_flags are either set once at creation time, | |
148 | * or aren't currently used, and thus checking against them can | |
149 | * be done without rt_lock: RTF_GATEWAY, RTF_HOST, RTF_DYNAMIC, | |
150 | * RTF_DONE, RTF_XRESOLVE, RTF_STATIC, RTF_BLACKHOLE, RTF_ANNOUNCE, | |
151 | * RTF_USETRAILERS, RTF_WASCLONED, RTF_PINNED, RTF_LOCAL, | |
152 | * RTF_BROADCAST, RTF_MULTICAST, RTF_IFSCOPE, RTF_IFREF. | |
153 | * | |
154 | * rt_key, rt_gateway, rt_ifp, rt_ifa | |
155 | * | |
156 | * - Always written/modified with both rnh_lock and rt_lock held. | |
157 | * | |
158 | * - May be read freely with rnh_lock held, else must hold rt_lock | |
159 | * for read access; holding both locks for read is also okay. | |
160 | * | |
161 | * - In the event rnh_lock is not acquired, or is not possible to be | |
162 | * acquired across the operation, setting RTF_CONDEMNED on a route | |
163 | * entry will prevent its rt_key, rt_gateway, rt_ifp and rt_ifa | |
164 | * from being modified. This is typically done on a route that | |
165 | * has been chosen for a removal (from the tree) prior to dropping | |
166 | * the rt_lock, so that those values will remain the same until | |
167 | * the route is freed. | |
168 | * | |
169 | * When rnh_lock is held rt_setgate(), rt_setif(), and rtsetifa() are | |
170 | * single-threaded, thus exclusive. This flag will also prevent the | |
171 | * route from being looked up via rt_lookup(). | |
172 | * | |
173 | * rt_genid | |
174 | * | |
175 | * - Assumes that 32-bit writes are atomic; no locks. | |
176 | * | |
177 | * rt_dlt, rt_output | |
178 | * | |
179 | * - Currently unused; no locks. | |
180 | * | |
181 | * Operations on a route entry can be described as follows: | |
182 | * | |
183 | * CREATE an entry with reference count set to 0 as part of RTM_ADD/RESOLVE. | |
184 | * | |
185 | * INSERTION of an entry into the radix tree holds the rnh_lock, checks | |
186 | * for duplicates and then adds the entry. rtrequest returns the entry | |
187 | * after bumping up the reference count to 1 (for the caller). | |
188 | * | |
189 | * LOOKUP of an entry holds the rnh_lock and bumps up the reference count | |
190 | * before returning; it is valid to also bump up the reference count using | |
191 | * RT_ADDREF after the lookup has returned an entry. | |
192 | * | |
193 | * REMOVAL of an entry from the radix tree holds the rnh_lock, removes the | |
194 | * entry but does not decrement the reference count. Removal happens when | |
195 | * the route is explicitly deleted (RTM_DELETE) or when it is in the cached | |
196 | * state and it expires. The route is said to be "down" when it is no | |
197 | * longer present in the tree. Freeing the entry will happen on the last | |
198 | * reference release of such a "down" route. | |
199 | * | |
200 | * RT_ADDREF/RT_REMREF operates on the routing entry which increments/ | |
201 | * decrements the reference count, rt_refcnt, atomically on the rtentry. | |
202 | * rt_refcnt is modified only using this routine. The general rule is to | |
203 | * do RT_ADDREF in the function that is passing the entry as an argument, | |
204 | * in order to prevent the entry from being freed by the callee. | |
205 | */ | |
206 | ||
207 | #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0) | |
208 | ||
209 | extern void kdp_set_gateway_mac(void *gatewaymac); | |
210 | ||
211 | __private_extern__ struct rtstat rtstat = { 0, 0, 0, 0, 0 }; | |
212 | struct radix_node_head *rt_tables[AF_MAX+1]; | |
213 | ||
214 | decl_lck_mtx_data(, rnh_lock_data); /* global routing tables mutex */ | |
215 | lck_mtx_t *rnh_lock = &rnh_lock_data; | |
216 | static lck_attr_t *rnh_lock_attr; | |
217 | static lck_grp_t *rnh_lock_grp; | |
218 | static lck_grp_attr_t *rnh_lock_grp_attr; | |
219 | ||
220 | /* Lock group and attribute for routing entry locks */ | |
221 | static lck_attr_t *rte_mtx_attr; | |
222 | static lck_grp_t *rte_mtx_grp; | |
223 | static lck_grp_attr_t *rte_mtx_grp_attr; | |
224 | ||
225 | int rttrash = 0; /* routes not in table but not freed */ | |
226 | ||
227 | unsigned int rte_debug; | |
228 | ||
229 | /* Possible flags for rte_debug */ | |
230 | #define RTD_DEBUG 0x1 /* enable or disable rtentry debug facility */ | |
231 | #define RTD_TRACE 0x2 /* trace alloc, free, refcnt and lock */ | |
232 | #define RTD_NO_FREE 0x4 /* don't free (good to catch corruptions) */ | |
233 | ||
234 | #define RTE_NAME "rtentry" /* name for zone and rt_lock */ | |
235 | ||
236 | static struct zone *rte_zone; /* special zone for rtentry */ | |
237 | #define RTE_ZONE_MAX 65536 /* maximum elements in zone */ | |
238 | #define RTE_ZONE_NAME RTE_NAME /* name of rtentry zone */ | |
239 | ||
240 | #define RTD_INUSE 0xFEEDFACE /* entry is in use */ | |
241 | #define RTD_FREED 0xDEADBEEF /* entry is freed */ | |
242 | ||
243 | #define MAX_SCOPE_ADDR_STR_LEN (MAX_IPv6_STR_LEN + 6) | |
244 | ||
245 | /* For gdb */ | |
246 | __private_extern__ unsigned int ctrace_stack_size = CTRACE_STACK_SIZE; | |
247 | __private_extern__ unsigned int ctrace_hist_size = CTRACE_HIST_SIZE; | |
248 | ||
249 | /* | |
250 | * Debug variant of rtentry structure. | |
251 | */ | |
252 | struct rtentry_dbg { | |
253 | struct rtentry rtd_entry; /* rtentry */ | |
254 | struct rtentry rtd_entry_saved; /* saved rtentry */ | |
255 | uint32_t rtd_inuse; /* in use pattern */ | |
256 | uint16_t rtd_refhold_cnt; /* # of rtref */ | |
257 | uint16_t rtd_refrele_cnt; /* # of rtunref */ | |
258 | uint32_t rtd_lock_cnt; /* # of locks */ | |
259 | uint32_t rtd_unlock_cnt; /* # of unlocks */ | |
260 | /* | |
261 | * Alloc and free callers. | |
262 | */ | |
263 | ctrace_t rtd_alloc; | |
264 | ctrace_t rtd_free; | |
265 | /* | |
266 | * Circular lists of rtref and rtunref callers. | |
267 | */ | |
268 | ctrace_t rtd_refhold[CTRACE_HIST_SIZE]; | |
269 | ctrace_t rtd_refrele[CTRACE_HIST_SIZE]; | |
270 | /* | |
271 | * Circular lists of locks and unlocks. | |
272 | */ | |
273 | ctrace_t rtd_lock[CTRACE_HIST_SIZE]; | |
274 | ctrace_t rtd_unlock[CTRACE_HIST_SIZE]; | |
275 | /* | |
276 | * Trash list linkage | |
277 | */ | |
278 | TAILQ_ENTRY(rtentry_dbg) rtd_trash_link; | |
279 | }; | |
280 | ||
281 | /* List of trash route entries protected by rnh_lock */ | |
282 | static TAILQ_HEAD(, rtentry_dbg) rttrash_head; | |
283 | ||
284 | static void rte_lock_init(struct rtentry *); | |
285 | static void rte_lock_destroy(struct rtentry *); | |
286 | static inline struct rtentry *rte_alloc_debug(void); | |
287 | static inline void rte_free_debug(struct rtentry *); | |
288 | static inline void rte_lock_debug(struct rtentry_dbg *); | |
289 | static inline void rte_unlock_debug(struct rtentry_dbg *); | |
290 | static void rt_maskedcopy(const struct sockaddr *, | |
291 | struct sockaddr *, const struct sockaddr *); | |
292 | static void rtable_init(void **); | |
293 | static inline void rtref_audit(struct rtentry_dbg *); | |
294 | static inline void rtunref_audit(struct rtentry_dbg *); | |
295 | static struct rtentry *rtalloc1_common_locked(struct sockaddr *, int, uint32_t, | |
296 | unsigned int); | |
297 | static int rtrequest_common_locked(int, struct sockaddr *, | |
298 | struct sockaddr *, struct sockaddr *, int, struct rtentry **, | |
299 | unsigned int); | |
300 | static struct rtentry *rtalloc1_locked(struct sockaddr *, int, uint32_t); | |
301 | static void rtalloc_ign_common_locked(struct route *, uint32_t, unsigned int); | |
302 | static inline void sin6_set_ifscope(struct sockaddr *, unsigned int); | |
303 | static inline void sin6_set_embedded_ifscope(struct sockaddr *, unsigned int); | |
304 | static inline unsigned int sin6_get_embedded_ifscope(struct sockaddr *); | |
305 | static struct sockaddr *ma_copy(int, struct sockaddr *, | |
306 | struct sockaddr_storage *, unsigned int); | |
307 | static struct sockaddr *sa_trim(struct sockaddr *, int); | |
308 | static struct radix_node *node_lookup(struct sockaddr *, struct sockaddr *, | |
309 | unsigned int); | |
310 | static struct radix_node *node_lookup_default(int); | |
311 | static struct rtentry *rt_lookup_common(boolean_t, boolean_t, struct sockaddr *, | |
312 | struct sockaddr *, struct radix_node_head *, unsigned int); | |
313 | static int rn_match_ifscope(struct radix_node *, void *); | |
314 | static struct ifaddr *ifa_ifwithroute_common_locked(int, | |
315 | const struct sockaddr *, const struct sockaddr *, unsigned int); | |
316 | static struct rtentry *rte_alloc(void); | |
317 | static void rte_free(struct rtentry *); | |
318 | static void rtfree_common(struct rtentry *, boolean_t); | |
319 | static void rte_if_ref(struct ifnet *, int); | |
320 | static void rt_set_idleref(struct rtentry *); | |
321 | static void rt_clear_idleref(struct rtentry *); | |
322 | static void rt_str4(struct rtentry *, char *, uint32_t, char *, uint32_t); | |
323 | #if INET6 | |
324 | static void rt_str6(struct rtentry *, char *, uint32_t, char *, uint32_t); | |
325 | #endif /* INET6 */ | |
326 | ||
327 | uint32_t route_genid_inet = 0; | |
328 | #if INET6 | |
329 | uint32_t route_genid_inet6 = 0; | |
330 | #endif /* INET6 */ | |
331 | ||
332 | #define ASSERT_SINIFSCOPE(sa) { \ | |
333 | if ((sa)->sa_family != AF_INET || \ | |
334 | (sa)->sa_len < sizeof (struct sockaddr_in)) \ | |
335 | panic("%s: bad sockaddr_in %p\n", __func__, sa); \ | |
336 | } | |
337 | ||
338 | #define ASSERT_SIN6IFSCOPE(sa) { \ | |
339 | if ((sa)->sa_family != AF_INET6 || \ | |
340 | (sa)->sa_len < sizeof (struct sockaddr_in6)) \ | |
341 | panic("%s: bad sockaddr_in6 %p\n", __func__, sa); \ | |
342 | } | |
343 | ||
344 | /* | |
345 | * Argument to leaf-matching routine; at present it is scoped routing | |
346 | * specific but can be expanded in future to include other search filters. | |
347 | */ | |
348 | struct matchleaf_arg { | |
349 | unsigned int ifscope; /* interface scope */ | |
350 | }; | |
351 | ||
352 | /* | |
353 | * For looking up the non-scoped default route (sockaddr instead | |
354 | * of sockaddr_in for convenience). | |
355 | */ | |
356 | static struct sockaddr sin_def = { | |
357 | sizeof (struct sockaddr_in), AF_INET, { 0, } | |
358 | }; | |
359 | ||
360 | static struct sockaddr_in6 sin6_def = { | |
361 | sizeof (struct sockaddr_in6), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 | |
362 | }; | |
363 | ||
364 | /* | |
365 | * Interface index (scope) of the primary interface; determined at | |
366 | * the time when the default, non-scoped route gets added, changed | |
367 | * or deleted. Protected by rnh_lock. | |
368 | */ | |
369 | static unsigned int primary_ifscope = IFSCOPE_NONE; | |
370 | static unsigned int primary6_ifscope = IFSCOPE_NONE; | |
371 | ||
372 | #define INET_DEFAULT(sa) \ | |
373 | ((sa)->sa_family == AF_INET && SIN(sa)->sin_addr.s_addr == 0) | |
374 | ||
375 | #define INET6_DEFAULT(sa) \ | |
376 | ((sa)->sa_family == AF_INET6 && \ | |
377 | IN6_IS_ADDR_UNSPECIFIED(&SIN6(sa)->sin6_addr)) | |
378 | ||
379 | #define SA_DEFAULT(sa) (INET_DEFAULT(sa) || INET6_DEFAULT(sa)) | |
380 | #define RT(r) ((struct rtentry *)r) | |
381 | #define RN(r) ((struct radix_node *)r) | |
382 | #define RT_HOST(r) (RT(r)->rt_flags & RTF_HOST) | |
383 | ||
384 | unsigned int rt_verbose = 0; | |
385 | #if (DEVELOPMENT || DEBUG) | |
386 | SYSCTL_DECL(_net_route); | |
387 | SYSCTL_UINT(_net_route, OID_AUTO, verbose, CTLFLAG_RW | CTLFLAG_LOCKED, | |
388 | &rt_verbose, 0, ""); | |
389 | #endif /* (DEVELOPMENT || DEBUG) */ | |
390 | ||
391 | static void | |
392 | rtable_init(void **table) | |
393 | { | |
394 | struct domain *dom; | |
395 | ||
396 | domain_proto_mtx_lock_assert_held(); | |
397 | ||
398 | TAILQ_FOREACH(dom, &domains, dom_entry) { | |
399 | if (dom->dom_rtattach != NULL) | |
400 | dom->dom_rtattach(&table[dom->dom_family], | |
401 | dom->dom_rtoffset); | |
402 | } | |
403 | } | |
404 | ||
405 | /* | |
406 | * Called by route_dinit(). | |
407 | */ | |
408 | void | |
409 | route_init(void) | |
410 | { | |
411 | int size; | |
412 | ||
413 | #if INET6 | |
414 | _CASSERT(offsetof(struct route, ro_rt) == | |
415 | offsetof(struct route_in6, ro_rt)); | |
416 | _CASSERT(offsetof(struct route, ro_srcia) == | |
417 | offsetof(struct route_in6, ro_srcia)); | |
418 | _CASSERT(offsetof(struct route, ro_flags) == | |
419 | offsetof(struct route_in6, ro_flags)); | |
420 | _CASSERT(offsetof(struct route, ro_dst) == | |
421 | offsetof(struct route_in6, ro_dst)); | |
422 | #endif /* INET6 */ | |
423 | ||
424 | PE_parse_boot_argn("rte_debug", &rte_debug, sizeof (rte_debug)); | |
425 | if (rte_debug != 0) | |
426 | rte_debug |= RTD_DEBUG; | |
427 | ||
428 | rnh_lock_grp_attr = lck_grp_attr_alloc_init(); | |
429 | rnh_lock_grp = lck_grp_alloc_init("route", rnh_lock_grp_attr); | |
430 | rnh_lock_attr = lck_attr_alloc_init(); | |
431 | lck_mtx_init(rnh_lock, rnh_lock_grp, rnh_lock_attr); | |
432 | ||
433 | rte_mtx_grp_attr = lck_grp_attr_alloc_init(); | |
434 | rte_mtx_grp = lck_grp_alloc_init(RTE_NAME, rte_mtx_grp_attr); | |
435 | rte_mtx_attr = lck_attr_alloc_init(); | |
436 | ||
437 | lck_mtx_lock(rnh_lock); | |
438 | rn_init(); /* initialize all zeroes, all ones, mask table */ | |
439 | lck_mtx_unlock(rnh_lock); | |
440 | rtable_init((void **)rt_tables); | |
441 | ||
442 | if (rte_debug & RTD_DEBUG) | |
443 | size = sizeof (struct rtentry_dbg); | |
444 | else | |
445 | size = sizeof (struct rtentry); | |
446 | ||
447 | rte_zone = zinit(size, RTE_ZONE_MAX * size, 0, RTE_ZONE_NAME); | |
448 | if (rte_zone == NULL) { | |
449 | panic("%s: failed allocating rte_zone", __func__); | |
450 | /* NOTREACHED */ | |
451 | } | |
452 | zone_change(rte_zone, Z_EXPAND, TRUE); | |
453 | zone_change(rte_zone, Z_CALLERACCT, FALSE); | |
454 | zone_change(rte_zone, Z_NOENCRYPT, TRUE); | |
455 | ||
456 | TAILQ_INIT(&rttrash_head); | |
457 | } | |
458 | ||
459 | /* | |
460 | * Given a route, determine whether or not it is the non-scoped default | |
461 | * route; dst typically comes from rt_key(rt) but may be coming from | |
462 | * a separate place when rt is in the process of being created. | |
463 | */ | |
464 | boolean_t | |
465 | rt_primary_default(struct rtentry *rt, struct sockaddr *dst) | |
466 | { | |
467 | return (SA_DEFAULT(dst) && !(rt->rt_flags & RTF_IFSCOPE)); | |
468 | } | |
469 | ||
470 | /* | |
471 | * Set the ifscope of the primary interface; caller holds rnh_lock. | |
472 | */ | |
473 | void | |
474 | set_primary_ifscope(int af, unsigned int ifscope) | |
475 | { | |
476 | if (af == AF_INET) | |
477 | primary_ifscope = ifscope; | |
478 | else | |
479 | primary6_ifscope = ifscope; | |
480 | } | |
481 | ||
482 | /* | |
483 | * Return the ifscope of the primary interface; caller holds rnh_lock. | |
484 | */ | |
485 | unsigned int | |
486 | get_primary_ifscope(int af) | |
487 | { | |
488 | return (af == AF_INET ? primary_ifscope : primary6_ifscope); | |
489 | } | |
490 | ||
491 | /* | |
492 | * Set the scope ID of a given a sockaddr_in. | |
493 | */ | |
494 | void | |
495 | sin_set_ifscope(struct sockaddr *sa, unsigned int ifscope) | |
496 | { | |
497 | /* Caller must pass in sockaddr_in */ | |
498 | ASSERT_SINIFSCOPE(sa); | |
499 | ||
500 | SINIFSCOPE(sa)->sin_scope_id = ifscope; | |
501 | } | |
502 | ||
503 | /* | |
504 | * Set the scope ID of given a sockaddr_in6. | |
505 | */ | |
506 | static inline void | |
507 | sin6_set_ifscope(struct sockaddr *sa, unsigned int ifscope) | |
508 | { | |
509 | /* Caller must pass in sockaddr_in6 */ | |
510 | ASSERT_SIN6IFSCOPE(sa); | |
511 | ||
512 | SIN6IFSCOPE(sa)->sin6_scope_id = ifscope; | |
513 | } | |
514 | ||
515 | /* | |
516 | * Given a sockaddr_in, return the scope ID to the caller. | |
517 | */ | |
518 | unsigned int | |
519 | sin_get_ifscope(struct sockaddr *sa) | |
520 | { | |
521 | /* Caller must pass in sockaddr_in */ | |
522 | ASSERT_SINIFSCOPE(sa); | |
523 | ||
524 | return (SINIFSCOPE(sa)->sin_scope_id); | |
525 | } | |
526 | ||
527 | /* | |
528 | * Given a sockaddr_in6, return the scope ID to the caller. | |
529 | */ | |
530 | unsigned int | |
531 | sin6_get_ifscope(struct sockaddr *sa) | |
532 | { | |
533 | /* Caller must pass in sockaddr_in6 */ | |
534 | ASSERT_SIN6IFSCOPE(sa); | |
535 | ||
536 | return (SIN6IFSCOPE(sa)->sin6_scope_id); | |
537 | } | |
538 | ||
539 | static inline void | |
540 | sin6_set_embedded_ifscope(struct sockaddr *sa, unsigned int ifscope) | |
541 | { | |
542 | /* Caller must pass in sockaddr_in6 */ | |
543 | ASSERT_SIN6IFSCOPE(sa); | |
544 | VERIFY(IN6_IS_SCOPE_EMBED(&(SIN6(sa)->sin6_addr))); | |
545 | ||
546 | SIN6(sa)->sin6_addr.s6_addr16[1] = htons(ifscope); | |
547 | } | |
548 | ||
549 | static inline unsigned int | |
550 | sin6_get_embedded_ifscope(struct sockaddr *sa) | |
551 | { | |
552 | /* Caller must pass in sockaddr_in6 */ | |
553 | ASSERT_SIN6IFSCOPE(sa); | |
554 | ||
555 | return (ntohs(SIN6(sa)->sin6_addr.s6_addr16[1])); | |
556 | } | |
557 | ||
558 | /* | |
559 | * Copy a sockaddr_{in,in6} src to a dst storage and set scope ID into dst. | |
560 | * | |
561 | * To clear the scope ID, pass is a NULL pifscope. To set the scope ID, pass | |
562 | * in a non-NULL pifscope with non-zero ifscope. Otherwise if pifscope is | |
563 | * non-NULL and ifscope is IFSCOPE_NONE, the existing scope ID is left intact. | |
564 | * In any case, the effective scope ID value is returned to the caller via | |
565 | * pifscope, if it is non-NULL. | |
566 | */ | |
567 | struct sockaddr * | |
568 | sa_copy(struct sockaddr *src, struct sockaddr_storage *dst, | |
569 | unsigned int *pifscope) | |
570 | { | |
571 | int af = src->sa_family; | |
572 | unsigned int ifscope = (pifscope != NULL) ? *pifscope : IFSCOPE_NONE; | |
573 | ||
574 | VERIFY(af == AF_INET || af == AF_INET6); | |
575 | ||
576 | bzero(dst, sizeof (*dst)); | |
577 | ||
578 | if (af == AF_INET) { | |
579 | bcopy(src, dst, sizeof (struct sockaddr_in)); | |
580 | if (pifscope == NULL || ifscope != IFSCOPE_NONE) | |
581 | sin_set_ifscope(SA(dst), ifscope); | |
582 | } else { | |
583 | bcopy(src, dst, sizeof (struct sockaddr_in6)); | |
584 | if (pifscope != NULL && | |
585 | IN6_IS_SCOPE_EMBED(&SIN6(dst)->sin6_addr)) { | |
586 | unsigned int eifscope; | |
587 | /* | |
588 | * If the address contains the embedded scope ID, | |
589 | * use that as the value for sin6_scope_id as long | |
590 | * the caller doesn't insist on clearing it (by | |
591 | * passing NULL) or setting it. | |
592 | */ | |
593 | eifscope = sin6_get_embedded_ifscope(SA(dst)); | |
594 | if (eifscope != IFSCOPE_NONE && ifscope == IFSCOPE_NONE) | |
595 | ifscope = eifscope; | |
596 | if (ifscope != IFSCOPE_NONE) { | |
597 | /* Set ifscope from pifscope or eifscope */ | |
598 | sin6_set_ifscope(SA(dst), ifscope); | |
599 | } else { | |
600 | /* If sin6_scope_id has a value, use that one */ | |
601 | ifscope = sin6_get_ifscope(SA(dst)); | |
602 | } | |
603 | /* | |
604 | * If sin6_scope_id is set but the address doesn't | |
605 | * contain the equivalent embedded value, set it. | |
606 | */ | |
607 | if (ifscope != IFSCOPE_NONE && eifscope != ifscope) | |
608 | sin6_set_embedded_ifscope(SA(dst), ifscope); | |
609 | } else if (pifscope == NULL || ifscope != IFSCOPE_NONE) { | |
610 | sin6_set_ifscope(SA(dst), ifscope); | |
611 | } | |
612 | } | |
613 | ||
614 | if (pifscope != NULL) { | |
615 | *pifscope = (af == AF_INET) ? sin_get_ifscope(SA(dst)) : | |
616 | sin6_get_ifscope(SA(dst)); | |
617 | } | |
618 | ||
619 | return (SA(dst)); | |
620 | } | |
621 | ||
622 | /* | |
623 | * Copy a mask from src to a dst storage and set scope ID into dst. | |
624 | */ | |
625 | static struct sockaddr * | |
626 | ma_copy(int af, struct sockaddr *src, struct sockaddr_storage *dst, | |
627 | unsigned int ifscope) | |
628 | { | |
629 | VERIFY(af == AF_INET || af == AF_INET6); | |
630 | ||
631 | bzero(dst, sizeof (*dst)); | |
632 | rt_maskedcopy(src, SA(dst), src); | |
633 | ||
634 | /* | |
635 | * The length of the mask sockaddr would need to be adjusted | |
636 | * to cover the additional {sin,sin6}_ifscope field; when ifscope | |
637 | * is IFSCOPE_NONE, we'd end up clearing the scope ID field on | |
638 | * the destination mask in addition to extending the length | |
639 | * of the sockaddr, as a side effect. This is okay, as any | |
640 | * trailing zeroes would be skipped by rn_addmask prior to | |
641 | * inserting or looking up the mask in the mask tree. | |
642 | */ | |
643 | if (af == AF_INET) { | |
644 | SINIFSCOPE(dst)->sin_scope_id = ifscope; | |
645 | SINIFSCOPE(dst)->sin_len = | |
646 | offsetof(struct sockaddr_inifscope, sin_scope_id) + | |
647 | sizeof (SINIFSCOPE(dst)->sin_scope_id); | |
648 | } else { | |
649 | SIN6IFSCOPE(dst)->sin6_scope_id = ifscope; | |
650 | SIN6IFSCOPE(dst)->sin6_len = | |
651 | offsetof(struct sockaddr_in6, sin6_scope_id) + | |
652 | sizeof (SIN6IFSCOPE(dst)->sin6_scope_id); | |
653 | } | |
654 | ||
655 | return (SA(dst)); | |
656 | } | |
657 | ||
658 | /* | |
659 | * Trim trailing zeroes on a sockaddr and update its length. | |
660 | */ | |
661 | static struct sockaddr * | |
662 | sa_trim(struct sockaddr *sa, int skip) | |
663 | { | |
664 | caddr_t cp, base = (caddr_t)sa + skip; | |
665 | ||
666 | if (sa->sa_len <= skip) | |
667 | return (sa); | |
668 | ||
669 | for (cp = base + (sa->sa_len - skip); cp > base && cp[-1] == 0; ) | |
670 | cp--; | |
671 | ||
672 | sa->sa_len = (cp - base) + skip; | |
673 | if (sa->sa_len < skip) { | |
674 | /* Must not happen, and if so, panic */ | |
675 | panic("%s: broken logic (sa_len %d < skip %d )", __func__, | |
676 | sa->sa_len, skip); | |
677 | /* NOTREACHED */ | |
678 | } else if (sa->sa_len == skip) { | |
679 | /* If we end up with all zeroes, then there's no mask */ | |
680 | sa->sa_len = 0; | |
681 | } | |
682 | ||
683 | return (sa); | |
684 | } | |
685 | ||
686 | /* | |
687 | * Called by rtm_msg{1,2} routines to "scrub" socket address structures of | |
688 | * kernel private information, so that clients of the routing socket will | |
689 | * not be confused by the presence of the information, or the side effect of | |
690 | * the increased length due to that. The source sockaddr is not modified; | |
691 | * instead, the scrubbing happens on the destination sockaddr storage that | |
692 | * is passed in by the caller. | |
693 | * | |
694 | * Scrubbing entails: | |
695 | * - removing embedded scope identifiers from network mask and destination | |
696 | * IPv4 and IPv6 socket addresses | |
697 | * - optionally removing global scope interface hardware addresses from | |
698 | * link-layer interface addresses when the MAC framework check fails. | |
699 | */ | |
700 | struct sockaddr * | |
701 | rtm_scrub(int type, int idx, struct sockaddr *hint, struct sockaddr *sa, | |
702 | void *buf, uint32_t buflen, kauth_cred_t *credp) | |
703 | { | |
704 | struct sockaddr_storage *ss = (struct sockaddr_storage *)buf; | |
705 | struct sockaddr *ret = sa; | |
706 | ||
707 | VERIFY(buf != NULL && buflen >= sizeof (*ss)); | |
708 | bzero(buf, buflen); | |
709 | ||
710 | switch (idx) { | |
711 | case RTAX_DST: | |
712 | /* | |
713 | * If this is for an AF_INET/AF_INET6 destination address, | |
714 | * call sa_copy() to clear the scope ID field. | |
715 | */ | |
716 | if (sa->sa_family == AF_INET && | |
717 | SINIFSCOPE(sa)->sin_scope_id != IFSCOPE_NONE) { | |
718 | ret = sa_copy(sa, ss, NULL); | |
719 | } else if (sa->sa_family == AF_INET6 && | |
720 | SIN6IFSCOPE(sa)->sin6_scope_id != IFSCOPE_NONE) { | |
721 | ret = sa_copy(sa, ss, NULL); | |
722 | } | |
723 | break; | |
724 | ||
725 | case RTAX_NETMASK: { | |
726 | int skip, af; | |
727 | /* | |
728 | * If this is for a mask, we can't tell whether or not there | |
729 | * is an valid scope ID value, as the span of bytes between | |
730 | * sa_len and the beginning of the mask (offset of sin_addr in | |
731 | * the case of AF_INET, or sin6_addr for AF_INET6) may be | |
732 | * filled with all-ones by rn_addmask(), and hence we cannot | |
733 | * rely on sa_family. Because of this, we use the sa_family | |
734 | * of the hint sockaddr (RTAX_{DST,IFA}) as indicator as to | |
735 | * whether or not the mask is to be treated as one for AF_INET | |
736 | * or AF_INET6. Clearing the scope ID field involves setting | |
737 | * it to IFSCOPE_NONE followed by calling sa_trim() to trim | |
738 | * trailing zeroes from the storage sockaddr, which reverses | |
739 | * what was done earlier by ma_copy() on the source sockaddr. | |
740 | */ | |
741 | if (hint == NULL || | |
742 | ((af = hint->sa_family) != AF_INET && af != AF_INET6)) | |
743 | break; /* nothing to do */ | |
744 | ||
745 | skip = (af == AF_INET) ? | |
746 | offsetof(struct sockaddr_in, sin_addr) : | |
747 | offsetof(struct sockaddr_in6, sin6_addr); | |
748 | ||
749 | if (sa->sa_len > skip && sa->sa_len <= sizeof (*ss)) { | |
750 | bcopy(sa, ss, sa->sa_len); | |
751 | /* | |
752 | * Don't use {sin,sin6}_set_ifscope() as sa_family | |
753 | * and sa_len for the netmask might not be set to | |
754 | * the corresponding expected values of the hint. | |
755 | */ | |
756 | if (hint->sa_family == AF_INET) | |
757 | SINIFSCOPE(ss)->sin_scope_id = IFSCOPE_NONE; | |
758 | else | |
759 | SIN6IFSCOPE(ss)->sin6_scope_id = IFSCOPE_NONE; | |
760 | ret = sa_trim(SA(ss), skip); | |
761 | ||
762 | /* | |
763 | * For AF_INET6 mask, set sa_len appropriately unless | |
764 | * this is requested via systl_dumpentry(), in which | |
765 | * case we return the raw value. | |
766 | */ | |
767 | if (hint->sa_family == AF_INET6 && | |
768 | type != RTM_GET && type != RTM_GET2) | |
769 | SA(ret)->sa_len = sizeof (struct sockaddr_in6); | |
770 | } | |
771 | break; | |
772 | } | |
773 | case RTAX_IFP: { | |
774 | if (sa->sa_family == AF_LINK && credp) { | |
775 | struct sockaddr_dl *sdl = SDL(buf); | |
776 | const void *bytes; | |
777 | size_t size; | |
778 | ||
779 | /* caller should handle worst case: SOCK_MAXADDRLEN */ | |
780 | VERIFY(buflen >= sa->sa_len); | |
781 | ||
782 | bcopy(sa, sdl, sa->sa_len); | |
783 | bytes = dlil_ifaddr_bytes(sdl, &size, credp); | |
784 | if (bytes != CONST_LLADDR(sdl)) { | |
785 | VERIFY(sdl->sdl_alen == size); | |
786 | bcopy(bytes, LLADDR(sdl), size); | |
787 | } | |
788 | ret = (struct sockaddr *)sdl; | |
789 | } | |
790 | break; | |
791 | } | |
792 | default: | |
793 | break; | |
794 | } | |
795 | ||
796 | return (ret); | |
797 | } | |
798 | ||
799 | /* | |
800 | * Callback leaf-matching routine for rn_matchaddr_args used | |
801 | * for looking up an exact match for a scoped route entry. | |
802 | */ | |
803 | static int | |
804 | rn_match_ifscope(struct radix_node *rn, void *arg) | |
805 | { | |
806 | struct rtentry *rt = (struct rtentry *)rn; | |
807 | struct matchleaf_arg *ma = arg; | |
808 | int af = rt_key(rt)->sa_family; | |
809 | ||
810 | if (!(rt->rt_flags & RTF_IFSCOPE) || (af != AF_INET && af != AF_INET6)) | |
811 | return (0); | |
812 | ||
813 | return (af == AF_INET ? | |
814 | (SINIFSCOPE(rt_key(rt))->sin_scope_id == ma->ifscope) : | |
815 | (SIN6IFSCOPE(rt_key(rt))->sin6_scope_id == ma->ifscope)); | |
816 | } | |
817 | ||
818 | /* | |
819 | * Atomically increment route generation counter | |
820 | */ | |
821 | void | |
822 | routegenid_update(void) | |
823 | { | |
824 | routegenid_inet_update(); | |
825 | #if INET6 | |
826 | routegenid_inet6_update(); | |
827 | #endif /* INET6 */ | |
828 | } | |
829 | ||
830 | void | |
831 | routegenid_inet_update(void) | |
832 | { | |
833 | atomic_add_32(&route_genid_inet, 1); | |
834 | } | |
835 | ||
836 | #if INET6 | |
837 | void | |
838 | routegenid_inet6_update(void) | |
839 | { | |
840 | atomic_add_32(&route_genid_inet6, 1); | |
841 | } | |
842 | #endif /* INET6 */ | |
843 | ||
844 | /* | |
845 | * Packet routing routines. | |
846 | */ | |
847 | void | |
848 | rtalloc(struct route *ro) | |
849 | { | |
850 | rtalloc_ign(ro, 0); | |
851 | } | |
852 | ||
853 | void | |
854 | rtalloc_scoped(struct route *ro, unsigned int ifscope) | |
855 | { | |
856 | rtalloc_scoped_ign(ro, 0, ifscope); | |
857 | } | |
858 | ||
859 | static void | |
860 | rtalloc_ign_common_locked(struct route *ro, uint32_t ignore, | |
861 | unsigned int ifscope) | |
862 | { | |
863 | struct rtentry *rt; | |
864 | ||
865 | if ((rt = ro->ro_rt) != NULL) { | |
866 | RT_LOCK_SPIN(rt); | |
867 | if (rt->rt_ifp != NULL && !ROUTE_UNUSABLE(ro)) { | |
868 | RT_UNLOCK(rt); | |
869 | return; | |
870 | } | |
871 | RT_UNLOCK(rt); | |
872 | ROUTE_RELEASE_LOCKED(ro); /* rnh_lock already held */ | |
873 | } | |
874 | ro->ro_rt = rtalloc1_common_locked(&ro->ro_dst, 1, ignore, ifscope); | |
875 | if (ro->ro_rt != NULL) { | |
876 | RT_GENID_SYNC(ro->ro_rt); | |
877 | RT_LOCK_ASSERT_NOTHELD(ro->ro_rt); | |
878 | } | |
879 | } | |
880 | ||
881 | void | |
882 | rtalloc_ign(struct route *ro, uint32_t ignore) | |
883 | { | |
884 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
885 | lck_mtx_lock(rnh_lock); | |
886 | rtalloc_ign_common_locked(ro, ignore, IFSCOPE_NONE); | |
887 | lck_mtx_unlock(rnh_lock); | |
888 | } | |
889 | ||
890 | void | |
891 | rtalloc_scoped_ign(struct route *ro, uint32_t ignore, unsigned int ifscope) | |
892 | { | |
893 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
894 | lck_mtx_lock(rnh_lock); | |
895 | rtalloc_ign_common_locked(ro, ignore, ifscope); | |
896 | lck_mtx_unlock(rnh_lock); | |
897 | } | |
898 | ||
899 | static struct rtentry * | |
900 | rtalloc1_locked(struct sockaddr *dst, int report, uint32_t ignflags) | |
901 | { | |
902 | return (rtalloc1_common_locked(dst, report, ignflags, IFSCOPE_NONE)); | |
903 | } | |
904 | ||
905 | struct rtentry * | |
906 | rtalloc1_scoped_locked(struct sockaddr *dst, int report, uint32_t ignflags, | |
907 | unsigned int ifscope) | |
908 | { | |
909 | return (rtalloc1_common_locked(dst, report, ignflags, ifscope)); | |
910 | } | |
911 | ||
912 | struct rtentry * | |
913 | rtalloc1_common_locked(struct sockaddr *dst, int report, uint32_t ignflags, | |
914 | unsigned int ifscope) | |
915 | { | |
916 | struct radix_node_head *rnh = rt_tables[dst->sa_family]; | |
917 | struct rtentry *rt, *newrt = NULL; | |
918 | struct rt_addrinfo info; | |
919 | uint32_t nflags; | |
920 | int err = 0, msgtype = RTM_MISS; | |
921 | ||
922 | if (rnh == NULL) | |
923 | goto unreachable; | |
924 | ||
925 | /* | |
926 | * Find the longest prefix or exact (in the scoped case) address match; | |
927 | * callee adds a reference to entry and checks for root node as well | |
928 | */ | |
929 | rt = rt_lookup(FALSE, dst, NULL, rnh, ifscope); | |
930 | if (rt == NULL) | |
931 | goto unreachable; | |
932 | ||
933 | RT_LOCK_SPIN(rt); | |
934 | newrt = rt; | |
935 | nflags = rt->rt_flags & ~ignflags; | |
936 | RT_UNLOCK(rt); | |
937 | if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) { | |
938 | /* | |
939 | * We are apparently adding (report = 0 in delete). | |
940 | * If it requires that it be cloned, do so. | |
941 | * (This implies it wasn't a HOST route.) | |
942 | */ | |
943 | err = rtrequest_locked(RTM_RESOLVE, dst, NULL, NULL, 0, &newrt); | |
944 | if (err) { | |
945 | /* | |
946 | * If the cloning didn't succeed, maybe what we | |
947 | * have from lookup above will do. Return that; | |
948 | * no need to hold another reference since it's | |
949 | * already done. | |
950 | */ | |
951 | newrt = rt; | |
952 | goto miss; | |
953 | } | |
954 | ||
955 | /* | |
956 | * We cloned it; drop the original route found during lookup. | |
957 | * The resulted cloned route (newrt) would now have an extra | |
958 | * reference held during rtrequest. | |
959 | */ | |
960 | rtfree_locked(rt); | |
961 | ||
962 | /* | |
963 | * If the newly created cloned route is a direct host route | |
964 | * then also check if it is to a router or not. | |
965 | * If it is, then set the RTF_ROUTER flag on the host route | |
966 | * for the gateway. | |
967 | * | |
968 | * XXX It is possible for the default route to be created post | |
969 | * cloned route creation of router's IP. | |
970 | * We can handle that corner case by special handing for RTM_ADD | |
971 | * of default route. | |
972 | */ | |
973 | if ((newrt->rt_flags & (RTF_HOST | RTF_LLINFO)) == | |
974 | (RTF_HOST | RTF_LLINFO)) { | |
975 | struct rtentry *defrt = NULL; | |
976 | struct sockaddr_storage def_key; | |
977 | ||
978 | bzero(&def_key, sizeof(def_key)); | |
979 | def_key.ss_len = rt_key(newrt)->sa_len; | |
980 | def_key.ss_family = rt_key(newrt)->sa_family; | |
981 | ||
982 | defrt = rtalloc1_scoped_locked((struct sockaddr *)&def_key, | |
983 | 0, 0, newrt->rt_ifp->if_index); | |
984 | ||
985 | if (defrt) { | |
986 | if (equal(rt_key(newrt), defrt->rt_gateway)) { | |
987 | newrt->rt_flags |= RTF_ROUTER; | |
988 | } | |
989 | rtfree_locked(defrt); | |
990 | } | |
991 | } | |
992 | ||
993 | if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { | |
994 | /* | |
995 | * If the new route specifies it be | |
996 | * externally resolved, then go do that. | |
997 | */ | |
998 | msgtype = RTM_RESOLVE; | |
999 | goto miss; | |
1000 | } | |
1001 | } | |
1002 | goto done; | |
1003 | ||
1004 | unreachable: | |
1005 | /* | |
1006 | * Either we hit the root or couldn't find any match, | |
1007 | * Which basically means "cant get there from here" | |
1008 | */ | |
1009 | rtstat.rts_unreach++; | |
1010 | ||
1011 | miss: | |
1012 | if (report) { | |
1013 | /* | |
1014 | * If required, report the failure to the supervising | |
1015 | * Authorities. | |
1016 | * For a delete, this is not an error. (report == 0) | |
1017 | */ | |
1018 | bzero((caddr_t)&info, sizeof(info)); | |
1019 | info.rti_info[RTAX_DST] = dst; | |
1020 | rt_missmsg(msgtype, &info, 0, err); | |
1021 | } | |
1022 | done: | |
1023 | return (newrt); | |
1024 | } | |
1025 | ||
1026 | struct rtentry * | |
1027 | rtalloc1(struct sockaddr *dst, int report, uint32_t ignflags) | |
1028 | { | |
1029 | struct rtentry *entry; | |
1030 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
1031 | lck_mtx_lock(rnh_lock); | |
1032 | entry = rtalloc1_locked(dst, report, ignflags); | |
1033 | lck_mtx_unlock(rnh_lock); | |
1034 | return (entry); | |
1035 | } | |
1036 | ||
1037 | struct rtentry * | |
1038 | rtalloc1_scoped(struct sockaddr *dst, int report, uint32_t ignflags, | |
1039 | unsigned int ifscope) | |
1040 | { | |
1041 | struct rtentry *entry; | |
1042 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
1043 | lck_mtx_lock(rnh_lock); | |
1044 | entry = rtalloc1_scoped_locked(dst, report, ignflags, ifscope); | |
1045 | lck_mtx_unlock(rnh_lock); | |
1046 | return (entry); | |
1047 | } | |
1048 | ||
1049 | /* | |
1050 | * Remove a reference count from an rtentry. | |
1051 | * If the count gets low enough, take it out of the routing table | |
1052 | */ | |
1053 | void | |
1054 | rtfree_locked(struct rtentry *rt) | |
1055 | { | |
1056 | rtfree_common(rt, TRUE); | |
1057 | } | |
1058 | ||
1059 | static void | |
1060 | rtfree_common(struct rtentry *rt, boolean_t locked) | |
1061 | { | |
1062 | struct radix_node_head *rnh; | |
1063 | ||
1064 | lck_mtx_assert(rnh_lock, locked ? | |
1065 | LCK_MTX_ASSERT_OWNED : LCK_MTX_ASSERT_NOTOWNED); | |
1066 | ||
1067 | /* | |
1068 | * Atomically decrement the reference count and if it reaches 0, | |
1069 | * and there is a close function defined, call the close function. | |
1070 | */ | |
1071 | RT_LOCK_SPIN(rt); | |
1072 | if (rtunref(rt) > 0) { | |
1073 | RT_UNLOCK(rt); | |
1074 | return; | |
1075 | } | |
1076 | ||
1077 | /* | |
1078 | * To avoid violating lock ordering, we must drop rt_lock before | |
1079 | * trying to acquire the global rnh_lock. If we are called with | |
1080 | * rnh_lock held, then we already have exclusive access; otherwise | |
1081 | * we do the lock dance. | |
1082 | */ | |
1083 | if (!locked) { | |
1084 | /* | |
1085 | * Note that we check it again below after grabbing rnh_lock, | |
1086 | * since it is possible that another thread doing a lookup wins | |
1087 | * the race, grabs the rnh_lock first, and bumps up reference | |
1088 | * count in which case the route should be left alone as it is | |
1089 | * still in use. It's also possible that another thread frees | |
1090 | * the route after we drop rt_lock; to prevent the route from | |
1091 | * being freed, we hold an extra reference. | |
1092 | */ | |
1093 | RT_ADDREF_LOCKED(rt); | |
1094 | RT_UNLOCK(rt); | |
1095 | lck_mtx_lock(rnh_lock); | |
1096 | RT_LOCK_SPIN(rt); | |
1097 | if (rtunref(rt) > 0) { | |
1098 | /* We've lost the race, so abort */ | |
1099 | RT_UNLOCK(rt); | |
1100 | goto done; | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | /* | |
1105 | * We may be blocked on other lock(s) as part of freeing | |
1106 | * the entry below, so convert from spin to full mutex. | |
1107 | */ | |
1108 | RT_CONVERT_LOCK(rt); | |
1109 | ||
1110 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
1111 | ||
1112 | /* Negative refcnt must never happen */ | |
1113 | if (rt->rt_refcnt != 0) { | |
1114 | panic("rt %p invalid refcnt %d", rt, rt->rt_refcnt); | |
1115 | /* NOTREACHED */ | |
1116 | } | |
1117 | /* Idle refcnt must have been dropped during rtunref() */ | |
1118 | VERIFY(!(rt->rt_flags & RTF_IFREF)); | |
1119 | ||
1120 | /* | |
1121 | * find the tree for that address family | |
1122 | * Note: in the case of igmp packets, there might not be an rnh | |
1123 | */ | |
1124 | rnh = rt_tables[rt_key(rt)->sa_family]; | |
1125 | ||
1126 | /* | |
1127 | * On last reference give the "close method" a chance to cleanup | |
1128 | * private state. This also permits (for IPv4 and IPv6) a chance | |
1129 | * to decide if the routing table entry should be purged immediately | |
1130 | * or at a later time. When an immediate purge is to happen the | |
1131 | * close routine typically issues RTM_DELETE which clears the RTF_UP | |
1132 | * flag on the entry so that the code below reclaims the storage. | |
1133 | */ | |
1134 | if (rnh != NULL && rnh->rnh_close != NULL) | |
1135 | rnh->rnh_close((struct radix_node *)rt, rnh); | |
1136 | ||
1137 | /* | |
1138 | * If we are no longer "up" (and ref == 0) then we can free the | |
1139 | * resources associated with the route. | |
1140 | */ | |
1141 | if (!(rt->rt_flags & RTF_UP)) { | |
1142 | struct rtentry *rt_parent; | |
1143 | struct ifaddr *rt_ifa; | |
1144 | ||
1145 | if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) { | |
1146 | panic("rt %p freed while in radix tree\n", rt); | |
1147 | /* NOTREACHED */ | |
1148 | } | |
1149 | /* | |
1150 | * the rtentry must have been removed from the routing table | |
1151 | * so it is represented in rttrash; remove that now. | |
1152 | */ | |
1153 | (void) OSDecrementAtomic(&rttrash); | |
1154 | if (rte_debug & RTD_DEBUG) { | |
1155 | TAILQ_REMOVE(&rttrash_head, (struct rtentry_dbg *)rt, | |
1156 | rtd_trash_link); | |
1157 | } | |
1158 | ||
1159 | /* | |
1160 | * release references on items we hold them on.. | |
1161 | * e.g other routes and ifaddrs. | |
1162 | */ | |
1163 | if ((rt_parent = rt->rt_parent) != NULL) | |
1164 | rt->rt_parent = NULL; | |
1165 | ||
1166 | if ((rt_ifa = rt->rt_ifa) != NULL) | |
1167 | rt->rt_ifa = NULL; | |
1168 | ||
1169 | /* | |
1170 | * Now free any attached link-layer info. | |
1171 | */ | |
1172 | if (rt->rt_llinfo != NULL) { | |
1173 | if (rt->rt_llinfo_free != NULL) | |
1174 | (*rt->rt_llinfo_free)(rt->rt_llinfo); | |
1175 | else | |
1176 | R_Free(rt->rt_llinfo); | |
1177 | rt->rt_llinfo = NULL; | |
1178 | } | |
1179 | ||
1180 | /* | |
1181 | * Route is no longer in the tree and refcnt is 0; | |
1182 | * we have exclusive access, so destroy it. | |
1183 | */ | |
1184 | RT_UNLOCK(rt); | |
1185 | ||
1186 | if (rt_parent != NULL) | |
1187 | rtfree_locked(rt_parent); | |
1188 | ||
1189 | if (rt_ifa != NULL) | |
1190 | IFA_REMREF(rt_ifa); | |
1191 | ||
1192 | /* | |
1193 | * The key is separately alloc'd so free it (see rt_setgate()). | |
1194 | * This also frees the gateway, as they are always malloc'd | |
1195 | * together. | |
1196 | */ | |
1197 | R_Free(rt_key(rt)); | |
1198 | ||
1199 | /* | |
1200 | * Free any statistics that may have been allocated | |
1201 | */ | |
1202 | nstat_route_detach(rt); | |
1203 | ||
1204 | /* | |
1205 | * and the rtentry itself of course | |
1206 | */ | |
1207 | rte_lock_destroy(rt); | |
1208 | rte_free(rt); | |
1209 | } else { | |
1210 | /* | |
1211 | * The "close method" has been called, but the route is | |
1212 | * still in the radix tree with zero refcnt, i.e. "up" | |
1213 | * and in the cached state. | |
1214 | */ | |
1215 | RT_UNLOCK(rt); | |
1216 | } | |
1217 | done: | |
1218 | if (!locked) | |
1219 | lck_mtx_unlock(rnh_lock); | |
1220 | } | |
1221 | ||
1222 | void | |
1223 | rtfree(struct rtentry *rt) | |
1224 | { | |
1225 | rtfree_common(rt, FALSE); | |
1226 | } | |
1227 | ||
1228 | /* | |
1229 | * Decrements the refcount but does not free the route when | |
1230 | * the refcount reaches zero. Unless you have really good reason, | |
1231 | * use rtfree not rtunref. | |
1232 | */ | |
1233 | int | |
1234 | rtunref(struct rtentry *p) | |
1235 | { | |
1236 | RT_LOCK_ASSERT_HELD(p); | |
1237 | ||
1238 | if (p->rt_refcnt == 0) { | |
1239 | panic("%s(%p) bad refcnt\n", __func__, p); | |
1240 | /* NOTREACHED */ | |
1241 | } else if (--p->rt_refcnt == 0) { | |
1242 | /* | |
1243 | * Release any idle reference count held on the interface; | |
1244 | * if the route is eligible, still UP and the refcnt becomes | |
1245 | * non-zero at some point in future before it is purged from | |
1246 | * the routing table, rt_set_idleref() will undo this. | |
1247 | */ | |
1248 | rt_clear_idleref(p); | |
1249 | } | |
1250 | ||
1251 | if (rte_debug & RTD_DEBUG) | |
1252 | rtunref_audit((struct rtentry_dbg *)p); | |
1253 | ||
1254 | /* Return new value */ | |
1255 | return (p->rt_refcnt); | |
1256 | } | |
1257 | ||
1258 | static inline void | |
1259 | rtunref_audit(struct rtentry_dbg *rte) | |
1260 | { | |
1261 | uint16_t idx; | |
1262 | ||
1263 | if (rte->rtd_inuse != RTD_INUSE) { | |
1264 | panic("rtunref: on freed rte=%p\n", rte); | |
1265 | /* NOTREACHED */ | |
1266 | } | |
1267 | idx = atomic_add_16_ov(&rte->rtd_refrele_cnt, 1) % CTRACE_HIST_SIZE; | |
1268 | if (rte_debug & RTD_TRACE) | |
1269 | ctrace_record(&rte->rtd_refrele[idx]); | |
1270 | } | |
1271 | ||
1272 | /* | |
1273 | * Add a reference count from an rtentry. | |
1274 | */ | |
1275 | void | |
1276 | rtref(struct rtentry *p) | |
1277 | { | |
1278 | RT_LOCK_ASSERT_HELD(p); | |
1279 | ||
1280 | if (++p->rt_refcnt == 0) { | |
1281 | panic("%s(%p) bad refcnt\n", __func__, p); | |
1282 | /* NOTREACHED */ | |
1283 | } else if (p->rt_refcnt == 1) { | |
1284 | /* | |
1285 | * Hold an idle reference count on the interface, | |
1286 | * if the route is eligible for it. | |
1287 | */ | |
1288 | rt_set_idleref(p); | |
1289 | } | |
1290 | ||
1291 | if (rte_debug & RTD_DEBUG) | |
1292 | rtref_audit((struct rtentry_dbg *)p); | |
1293 | } | |
1294 | ||
1295 | static inline void | |
1296 | rtref_audit(struct rtentry_dbg *rte) | |
1297 | { | |
1298 | uint16_t idx; | |
1299 | ||
1300 | if (rte->rtd_inuse != RTD_INUSE) { | |
1301 | panic("rtref_audit: on freed rte=%p\n", rte); | |
1302 | /* NOTREACHED */ | |
1303 | } | |
1304 | idx = atomic_add_16_ov(&rte->rtd_refhold_cnt, 1) % CTRACE_HIST_SIZE; | |
1305 | if (rte_debug & RTD_TRACE) | |
1306 | ctrace_record(&rte->rtd_refhold[idx]); | |
1307 | } | |
1308 | ||
1309 | void | |
1310 | rtsetifa(struct rtentry *rt, struct ifaddr *ifa) | |
1311 | { | |
1312 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
1313 | ||
1314 | RT_LOCK_ASSERT_HELD(rt); | |
1315 | ||
1316 | if (rt->rt_ifa == ifa) | |
1317 | return; | |
1318 | ||
1319 | /* Become a regular mutex, just in case */ | |
1320 | RT_CONVERT_LOCK(rt); | |
1321 | ||
1322 | /* Release the old ifa */ | |
1323 | if (rt->rt_ifa) | |
1324 | IFA_REMREF(rt->rt_ifa); | |
1325 | ||
1326 | /* Set rt_ifa */ | |
1327 | rt->rt_ifa = ifa; | |
1328 | ||
1329 | /* Take a reference to the ifa */ | |
1330 | if (rt->rt_ifa) | |
1331 | IFA_ADDREF(rt->rt_ifa); | |
1332 | } | |
1333 | ||
1334 | /* | |
1335 | * Force a routing table entry to the specified | |
1336 | * destination to go through the given gateway. | |
1337 | * Normally called as a result of a routing redirect | |
1338 | * message from the network layer. | |
1339 | */ | |
1340 | void | |
1341 | rtredirect(struct ifnet *ifp, struct sockaddr *dst, struct sockaddr *gateway, | |
1342 | struct sockaddr *netmask, int flags, struct sockaddr *src, | |
1343 | struct rtentry **rtp) | |
1344 | { | |
1345 | struct rtentry *rt = NULL; | |
1346 | int error = 0; | |
1347 | short *stat = 0; | |
1348 | struct rt_addrinfo info; | |
1349 | struct ifaddr *ifa = NULL; | |
1350 | unsigned int ifscope = (ifp != NULL) ? ifp->if_index : IFSCOPE_NONE; | |
1351 | struct sockaddr_storage ss; | |
1352 | int af = src->sa_family; | |
1353 | ||
1354 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
1355 | lck_mtx_lock(rnh_lock); | |
1356 | ||
1357 | /* | |
1358 | * Transform src into the internal routing table form for | |
1359 | * comparison against rt_gateway below. | |
1360 | */ | |
1361 | #if INET6 | |
1362 | if ((af == AF_INET) || (af == AF_INET6)) | |
1363 | #else | |
1364 | if (af == AF_INET) | |
1365 | #endif /* !INET6 */ | |
1366 | src = sa_copy(src, &ss, &ifscope); | |
1367 | ||
1368 | /* | |
1369 | * Verify the gateway is directly reachable; if scoped routing | |
1370 | * is enabled, verify that it is reachable from the interface | |
1371 | * where the ICMP redirect arrived on. | |
1372 | */ | |
1373 | if ((ifa = ifa_ifwithnet_scoped(gateway, ifscope)) == NULL) { | |
1374 | error = ENETUNREACH; | |
1375 | goto out; | |
1376 | } | |
1377 | ||
1378 | /* Lookup route to the destination (from the original IP header) */ | |
1379 | rt = rtalloc1_scoped_locked(dst, 0, RTF_CLONING|RTF_PRCLONING, ifscope); | |
1380 | if (rt != NULL) | |
1381 | RT_LOCK(rt); | |
1382 | ||
1383 | /* | |
1384 | * If the redirect isn't from our current router for this dst, | |
1385 | * it's either old or wrong. If it redirects us to ourselves, | |
1386 | * we have a routing loop, perhaps as a result of an interface | |
1387 | * going down recently. Holding rnh_lock here prevents the | |
1388 | * possibility of rt_ifa/ifa's ifa_addr from changing (e.g. | |
1389 | * in_ifinit), so okay to access ifa_addr without locking. | |
1390 | */ | |
1391 | if (!(flags & RTF_DONE) && rt != NULL && | |
1392 | (!equal(src, rt->rt_gateway) || !equal(rt->rt_ifa->ifa_addr, | |
1393 | ifa->ifa_addr))) { | |
1394 | error = EINVAL; | |
1395 | } else { | |
1396 | IFA_REMREF(ifa); | |
1397 | if ((ifa = ifa_ifwithaddr(gateway))) { | |
1398 | IFA_REMREF(ifa); | |
1399 | ifa = NULL; | |
1400 | error = EHOSTUNREACH; | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | if (ifa) { | |
1405 | IFA_REMREF(ifa); | |
1406 | ifa = NULL; | |
1407 | } | |
1408 | ||
1409 | if (error) { | |
1410 | if (rt != NULL) | |
1411 | RT_UNLOCK(rt); | |
1412 | goto done; | |
1413 | } | |
1414 | ||
1415 | /* | |
1416 | * Create a new entry if we just got back a wildcard entry | |
1417 | * or the the lookup failed. This is necessary for hosts | |
1418 | * which use routing redirects generated by smart gateways | |
1419 | * to dynamically build the routing tables. | |
1420 | */ | |
1421 | if ((rt == NULL) || (rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) | |
1422 | goto create; | |
1423 | /* | |
1424 | * Don't listen to the redirect if it's | |
1425 | * for a route to an interface. | |
1426 | */ | |
1427 | RT_LOCK_ASSERT_HELD(rt); | |
1428 | if (rt->rt_flags & RTF_GATEWAY) { | |
1429 | if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { | |
1430 | /* | |
1431 | * Changing from route to net => route to host. | |
1432 | * Create new route, rather than smashing route | |
1433 | * to net; similar to cloned routes, the newly | |
1434 | * created host route is scoped as well. | |
1435 | */ | |
1436 | create: | |
1437 | if (rt != NULL) | |
1438 | RT_UNLOCK(rt); | |
1439 | flags |= RTF_GATEWAY | RTF_DYNAMIC; | |
1440 | error = rtrequest_scoped_locked(RTM_ADD, dst, | |
1441 | gateway, netmask, flags, NULL, ifscope); | |
1442 | stat = &rtstat.rts_dynamic; | |
1443 | } else { | |
1444 | /* | |
1445 | * Smash the current notion of the gateway to | |
1446 | * this destination. Should check about netmask!!! | |
1447 | */ | |
1448 | rt->rt_flags |= RTF_MODIFIED; | |
1449 | flags |= RTF_MODIFIED; | |
1450 | stat = &rtstat.rts_newgateway; | |
1451 | /* | |
1452 | * add the key and gateway (in one malloc'd chunk). | |
1453 | */ | |
1454 | error = rt_setgate(rt, rt_key(rt), gateway); | |
1455 | RT_UNLOCK(rt); | |
1456 | } | |
1457 | } else { | |
1458 | RT_UNLOCK(rt); | |
1459 | error = EHOSTUNREACH; | |
1460 | } | |
1461 | done: | |
1462 | if (rt != NULL) { | |
1463 | RT_LOCK_ASSERT_NOTHELD(rt); | |
1464 | if (rtp && !error) | |
1465 | *rtp = rt; | |
1466 | else | |
1467 | rtfree_locked(rt); | |
1468 | } | |
1469 | out: | |
1470 | if (error) { | |
1471 | rtstat.rts_badredirect++; | |
1472 | } else { | |
1473 | if (stat != NULL) | |
1474 | (*stat)++; | |
1475 | ||
1476 | if (af == AF_INET) | |
1477 | routegenid_inet_update(); | |
1478 | #if INET6 | |
1479 | else if (af == AF_INET6) | |
1480 | routegenid_inet6_update(); | |
1481 | #endif /* INET6 */ | |
1482 | } | |
1483 | lck_mtx_unlock(rnh_lock); | |
1484 | bzero((caddr_t)&info, sizeof(info)); | |
1485 | info.rti_info[RTAX_DST] = dst; | |
1486 | info.rti_info[RTAX_GATEWAY] = gateway; | |
1487 | info.rti_info[RTAX_NETMASK] = netmask; | |
1488 | info.rti_info[RTAX_AUTHOR] = src; | |
1489 | rt_missmsg(RTM_REDIRECT, &info, flags, error); | |
1490 | } | |
1491 | ||
1492 | /* | |
1493 | * Routing table ioctl interface. | |
1494 | */ | |
1495 | int | |
1496 | rtioctl(unsigned long req, caddr_t data, struct proc *p) | |
1497 | { | |
1498 | #pragma unused(p, req, data) | |
1499 | return (ENXIO); | |
1500 | } | |
1501 | ||
1502 | struct ifaddr * | |
1503 | ifa_ifwithroute( | |
1504 | int flags, | |
1505 | const struct sockaddr *dst, | |
1506 | const struct sockaddr *gateway) | |
1507 | { | |
1508 | struct ifaddr *ifa; | |
1509 | ||
1510 | lck_mtx_lock(rnh_lock); | |
1511 | ifa = ifa_ifwithroute_locked(flags, dst, gateway); | |
1512 | lck_mtx_unlock(rnh_lock); | |
1513 | ||
1514 | return (ifa); | |
1515 | } | |
1516 | ||
1517 | struct ifaddr * | |
1518 | ifa_ifwithroute_locked(int flags, const struct sockaddr *dst, | |
1519 | const struct sockaddr *gateway) | |
1520 | { | |
1521 | return (ifa_ifwithroute_common_locked((flags & ~RTF_IFSCOPE), dst, | |
1522 | gateway, IFSCOPE_NONE)); | |
1523 | } | |
1524 | ||
1525 | struct ifaddr * | |
1526 | ifa_ifwithroute_scoped_locked(int flags, const struct sockaddr *dst, | |
1527 | const struct sockaddr *gateway, unsigned int ifscope) | |
1528 | { | |
1529 | if (ifscope != IFSCOPE_NONE) | |
1530 | flags |= RTF_IFSCOPE; | |
1531 | else | |
1532 | flags &= ~RTF_IFSCOPE; | |
1533 | ||
1534 | return (ifa_ifwithroute_common_locked(flags, dst, gateway, ifscope)); | |
1535 | } | |
1536 | ||
1537 | static struct ifaddr * | |
1538 | ifa_ifwithroute_common_locked(int flags, const struct sockaddr *dst, | |
1539 | const struct sockaddr *gw, unsigned int ifscope) | |
1540 | { | |
1541 | struct ifaddr *ifa = NULL; | |
1542 | struct rtentry *rt = NULL; | |
1543 | struct sockaddr_storage dst_ss, gw_ss; | |
1544 | ||
1545 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
1546 | ||
1547 | /* | |
1548 | * Just in case the sockaddr passed in by the caller | |
1549 | * contains a scope ID, make sure to clear it since | |
1550 | * interface addresses aren't scoped. | |
1551 | */ | |
1552 | #if INET6 | |
1553 | if (dst != NULL && | |
1554 | ((dst->sa_family == AF_INET) || | |
1555 | (dst->sa_family == AF_INET6))) | |
1556 | #else | |
1557 | if (dst != NULL && dst->sa_family == AF_INET) | |
1558 | #endif /* !INET6 */ | |
1559 | dst = sa_copy(SA((uintptr_t)dst), &dst_ss, NULL); | |
1560 | ||
1561 | #if INET6 | |
1562 | if (gw != NULL && | |
1563 | ((gw->sa_family == AF_INET) || | |
1564 | (gw->sa_family == AF_INET6))) | |
1565 | #else | |
1566 | if (gw != NULL && gw->sa_family == AF_INET) | |
1567 | #endif /* !INET6 */ | |
1568 | gw = sa_copy(SA((uintptr_t)gw), &gw_ss, NULL); | |
1569 | ||
1570 | if (!(flags & RTF_GATEWAY)) { | |
1571 | /* | |
1572 | * If we are adding a route to an interface, | |
1573 | * and the interface is a pt to pt link | |
1574 | * we should search for the destination | |
1575 | * as our clue to the interface. Otherwise | |
1576 | * we can use the local address. | |
1577 | */ | |
1578 | if (flags & RTF_HOST) { | |
1579 | ifa = ifa_ifwithdstaddr(dst); | |
1580 | } | |
1581 | if (ifa == NULL) | |
1582 | ifa = ifa_ifwithaddr_scoped(gw, ifscope); | |
1583 | } else { | |
1584 | /* | |
1585 | * If we are adding a route to a remote net | |
1586 | * or host, the gateway may still be on the | |
1587 | * other end of a pt to pt link. | |
1588 | */ | |
1589 | ifa = ifa_ifwithdstaddr(gw); | |
1590 | } | |
1591 | if (ifa == NULL) | |
1592 | ifa = ifa_ifwithnet_scoped(gw, ifscope); | |
1593 | if (ifa == NULL) { | |
1594 | /* Workaround to avoid gcc warning regarding const variable */ | |
1595 | rt = rtalloc1_scoped_locked((struct sockaddr *)(size_t)dst, | |
1596 | 0, 0, ifscope); | |
1597 | if (rt != NULL) { | |
1598 | RT_LOCK_SPIN(rt); | |
1599 | ifa = rt->rt_ifa; | |
1600 | if (ifa != NULL) { | |
1601 | /* Become a regular mutex */ | |
1602 | RT_CONVERT_LOCK(rt); | |
1603 | IFA_ADDREF(ifa); | |
1604 | } | |
1605 | RT_REMREF_LOCKED(rt); | |
1606 | RT_UNLOCK(rt); | |
1607 | rt = NULL; | |
1608 | } | |
1609 | } | |
1610 | /* | |
1611 | * Holding rnh_lock here prevents the possibility of ifa from | |
1612 | * changing (e.g. in_ifinit), so it is safe to access its | |
1613 | * ifa_addr (here and down below) without locking. | |
1614 | */ | |
1615 | if (ifa != NULL && ifa->ifa_addr->sa_family != dst->sa_family) { | |
1616 | struct ifaddr *newifa; | |
1617 | /* Callee adds reference to newifa upon success */ | |
1618 | newifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); | |
1619 | if (newifa != NULL) { | |
1620 | IFA_REMREF(ifa); | |
1621 | ifa = newifa; | |
1622 | } | |
1623 | } | |
1624 | /* | |
1625 | * If we are adding a gateway, it is quite possible that the | |
1626 | * routing table has a static entry in place for the gateway, | |
1627 | * that may not agree with info garnered from the interfaces. | |
1628 | * The routing table should carry more precedence than the | |
1629 | * interfaces in this matter. Must be careful not to stomp | |
1630 | * on new entries from rtinit, hence (ifa->ifa_addr != gw). | |
1631 | */ | |
1632 | if ((ifa == NULL || | |
1633 | !equal(ifa->ifa_addr, (struct sockaddr *)(size_t)gw)) && | |
1634 | (rt = rtalloc1_scoped_locked((struct sockaddr *)(size_t)gw, | |
1635 | 0, 0, ifscope)) != NULL) { | |
1636 | if (ifa != NULL) | |
1637 | IFA_REMREF(ifa); | |
1638 | RT_LOCK_SPIN(rt); | |
1639 | ifa = rt->rt_ifa; | |
1640 | if (ifa != NULL) { | |
1641 | /* Become a regular mutex */ | |
1642 | RT_CONVERT_LOCK(rt); | |
1643 | IFA_ADDREF(ifa); | |
1644 | } | |
1645 | RT_REMREF_LOCKED(rt); | |
1646 | RT_UNLOCK(rt); | |
1647 | } | |
1648 | /* | |
1649 | * If an interface scope was specified, the interface index of | |
1650 | * the found ifaddr must be equivalent to that of the scope; | |
1651 | * otherwise there is no match. | |
1652 | */ | |
1653 | if ((flags & RTF_IFSCOPE) && | |
1654 | ifa != NULL && ifa->ifa_ifp->if_index != ifscope) { | |
1655 | IFA_REMREF(ifa); | |
1656 | ifa = NULL; | |
1657 | } | |
1658 | ||
1659 | return (ifa); | |
1660 | } | |
1661 | ||
1662 | static int rt_fixdelete(struct radix_node *, void *); | |
1663 | static int rt_fixchange(struct radix_node *, void *); | |
1664 | ||
1665 | struct rtfc_arg { | |
1666 | struct rtentry *rt0; | |
1667 | struct radix_node_head *rnh; | |
1668 | }; | |
1669 | ||
1670 | int | |
1671 | rtrequest_locked(int req, struct sockaddr *dst, struct sockaddr *gateway, | |
1672 | struct sockaddr *netmask, int flags, struct rtentry **ret_nrt) | |
1673 | { | |
1674 | return (rtrequest_common_locked(req, dst, gateway, netmask, | |
1675 | (flags & ~RTF_IFSCOPE), ret_nrt, IFSCOPE_NONE)); | |
1676 | } | |
1677 | ||
1678 | int | |
1679 | rtrequest_scoped_locked(int req, struct sockaddr *dst, | |
1680 | struct sockaddr *gateway, struct sockaddr *netmask, int flags, | |
1681 | struct rtentry **ret_nrt, unsigned int ifscope) | |
1682 | { | |
1683 | if (ifscope != IFSCOPE_NONE) | |
1684 | flags |= RTF_IFSCOPE; | |
1685 | else | |
1686 | flags &= ~RTF_IFSCOPE; | |
1687 | ||
1688 | return (rtrequest_common_locked(req, dst, gateway, netmask, | |
1689 | flags, ret_nrt, ifscope)); | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * Do appropriate manipulations of a routing tree given all the bits of | |
1694 | * info needed. | |
1695 | * | |
1696 | * Storing the scope ID in the radix key is an internal job that should be | |
1697 | * left to routines in this module. Callers should specify the scope value | |
1698 | * to the "scoped" variants of route routines instead of manipulating the | |
1699 | * key itself. This is typically done when creating a scoped route, e.g. | |
1700 | * rtrequest(RTM_ADD). Once such a route is created and marked with the | |
1701 | * RTF_IFSCOPE flag, callers can simply use its rt_key(rt) to clone it | |
1702 | * (RTM_RESOLVE) or to remove it (RTM_DELETE). An exception to this is | |
1703 | * during certain routing socket operations where the search key might be | |
1704 | * derived from the routing message itself, in which case the caller must | |
1705 | * specify the destination address and scope value for RTM_ADD/RTM_DELETE. | |
1706 | */ | |
1707 | static int | |
1708 | rtrequest_common_locked(int req, struct sockaddr *dst0, | |
1709 | struct sockaddr *gateway, struct sockaddr *netmask, int flags, | |
1710 | struct rtentry **ret_nrt, unsigned int ifscope) | |
1711 | { | |
1712 | int error = 0; | |
1713 | struct rtentry *rt; | |
1714 | struct radix_node *rn; | |
1715 | struct radix_node_head *rnh; | |
1716 | struct ifaddr *ifa = NULL; | |
1717 | struct sockaddr *ndst, *dst = dst0; | |
1718 | struct sockaddr_storage ss, mask; | |
1719 | struct timeval caltime; | |
1720 | int af = dst->sa_family; | |
1721 | void (*ifa_rtrequest)(int, struct rtentry *, struct sockaddr *); | |
1722 | ||
1723 | #define senderr(x) { error = x; goto bad; } | |
1724 | ||
1725 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
1726 | /* | |
1727 | * Find the correct routing tree to use for this Address Family | |
1728 | */ | |
1729 | if ((rnh = rt_tables[af]) == NULL) | |
1730 | senderr(ESRCH); | |
1731 | /* | |
1732 | * If we are adding a host route then we don't want to put | |
1733 | * a netmask in the tree | |
1734 | */ | |
1735 | if (flags & RTF_HOST) | |
1736 | netmask = NULL; | |
1737 | ||
1738 | /* | |
1739 | * If Scoped Routing is enabled, use a local copy of the destination | |
1740 | * address to store the scope ID into. This logic is repeated below | |
1741 | * in the RTM_RESOLVE handler since the caller does not normally | |
1742 | * specify such a flag during a resolve, as well as for the handling | |
1743 | * of IPv4 link-local address; instead, it passes in the route used for | |
1744 | * cloning for which the scope info is derived from. Note also that | |
1745 | * in the case of RTM_DELETE, the address passed in by the caller | |
1746 | * might already contain the scope ID info when it is the key itself, | |
1747 | * thus making RTF_IFSCOPE unnecessary; one instance where it is | |
1748 | * explicitly set is inside route_output() as part of handling a | |
1749 | * routing socket request. | |
1750 | */ | |
1751 | #if INET6 | |
1752 | if (req != RTM_RESOLVE && ((af == AF_INET) || (af == AF_INET6))) { | |
1753 | #else | |
1754 | if (req != RTM_RESOLVE && af == AF_INET) { | |
1755 | #endif /* !INET6 */ | |
1756 | /* Transform dst into the internal routing table form */ | |
1757 | dst = sa_copy(dst, &ss, &ifscope); | |
1758 | ||
1759 | /* Transform netmask into the internal routing table form */ | |
1760 | if (netmask != NULL) | |
1761 | netmask = ma_copy(af, netmask, &mask, ifscope); | |
1762 | ||
1763 | if (ifscope != IFSCOPE_NONE) | |
1764 | flags |= RTF_IFSCOPE; | |
1765 | } else if ((flags & RTF_IFSCOPE) && | |
1766 | (af != AF_INET && af != AF_INET6)) { | |
1767 | senderr(EINVAL); | |
1768 | } | |
1769 | ||
1770 | if (ifscope == IFSCOPE_NONE) | |
1771 | flags &= ~RTF_IFSCOPE; | |
1772 | ||
1773 | switch (req) { | |
1774 | case RTM_DELETE: { | |
1775 | struct rtentry *gwrt = NULL; | |
1776 | /* | |
1777 | * Remove the item from the tree and return it. | |
1778 | * Complain if it is not there and do no more processing. | |
1779 | */ | |
1780 | if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL) | |
1781 | senderr(ESRCH); | |
1782 | if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) { | |
1783 | panic("rtrequest delete"); | |
1784 | /* NOTREACHED */ | |
1785 | } | |
1786 | rt = (struct rtentry *)rn; | |
1787 | ||
1788 | RT_LOCK(rt); | |
1789 | rt->rt_flags &= ~RTF_UP; | |
1790 | /* | |
1791 | * Release any idle reference count held on the interface | |
1792 | * as this route is no longer externally visible. | |
1793 | */ | |
1794 | rt_clear_idleref(rt); | |
1795 | /* | |
1796 | * Take an extra reference to handle the deletion of a route | |
1797 | * entry whose reference count is already 0; e.g. an expiring | |
1798 | * cloned route entry or an entry that was added to the table | |
1799 | * with 0 reference. If the caller is interested in this route, | |
1800 | * we will return it with the reference intact. Otherwise we | |
1801 | * will decrement the reference via rtfree_locked() and then | |
1802 | * possibly deallocate it. | |
1803 | */ | |
1804 | RT_ADDREF_LOCKED(rt); | |
1805 | ||
1806 | /* | |
1807 | * For consistency, in case the caller didn't set the flag. | |
1808 | */ | |
1809 | rt->rt_flags |= RTF_CONDEMNED; | |
1810 | ||
1811 | /* | |
1812 | * Clear RTF_ROUTER if it's set. | |
1813 | */ | |
1814 | if (rt->rt_flags & RTF_ROUTER) { | |
1815 | VERIFY(rt->rt_flags & RTF_HOST); | |
1816 | rt->rt_flags &= ~RTF_ROUTER; | |
1817 | } | |
1818 | ||
1819 | /* | |
1820 | * Now search what's left of the subtree for any cloned | |
1821 | * routes which might have been formed from this node. | |
1822 | */ | |
1823 | if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) && | |
1824 | rt_mask(rt)) { | |
1825 | RT_UNLOCK(rt); | |
1826 | rnh->rnh_walktree_from(rnh, dst, rt_mask(rt), | |
1827 | rt_fixdelete, rt); | |
1828 | RT_LOCK(rt); | |
1829 | } | |
1830 | ||
1831 | /* | |
1832 | * Remove any external references we may have. | |
1833 | */ | |
1834 | if ((gwrt = rt->rt_gwroute) != NULL) | |
1835 | rt->rt_gwroute = NULL; | |
1836 | ||
1837 | /* | |
1838 | * give the protocol a chance to keep things in sync. | |
1839 | */ | |
1840 | if ((ifa = rt->rt_ifa) != NULL) { | |
1841 | IFA_LOCK_SPIN(ifa); | |
1842 | ifa_rtrequest = ifa->ifa_rtrequest; | |
1843 | IFA_UNLOCK(ifa); | |
1844 | if (ifa_rtrequest != NULL) | |
1845 | ifa_rtrequest(RTM_DELETE, rt, NULL); | |
1846 | /* keep reference on rt_ifa */ | |
1847 | ifa = NULL; | |
1848 | } | |
1849 | ||
1850 | /* | |
1851 | * one more rtentry floating around that is not | |
1852 | * linked to the routing table. | |
1853 | */ | |
1854 | (void) OSIncrementAtomic(&rttrash); | |
1855 | if (rte_debug & RTD_DEBUG) { | |
1856 | TAILQ_INSERT_TAIL(&rttrash_head, | |
1857 | (struct rtentry_dbg *)rt, rtd_trash_link); | |
1858 | } | |
1859 | ||
1860 | /* | |
1861 | * If this is the (non-scoped) default route, clear | |
1862 | * the interface index used for the primary ifscope. | |
1863 | */ | |
1864 | if (rt_primary_default(rt, rt_key(rt))) { | |
1865 | set_primary_ifscope(rt_key(rt)->sa_family, | |
1866 | IFSCOPE_NONE); | |
1867 | } | |
1868 | ||
1869 | RT_UNLOCK(rt); | |
1870 | ||
1871 | /* | |
1872 | * This might result in another rtentry being freed if | |
1873 | * we held its last reference. Do this after the rtentry | |
1874 | * lock is dropped above, as it could lead to the same | |
1875 | * lock being acquired if gwrt is a clone of rt. | |
1876 | */ | |
1877 | if (gwrt != NULL) | |
1878 | rtfree_locked(gwrt); | |
1879 | ||
1880 | /* | |
1881 | * If the caller wants it, then it can have it, | |
1882 | * but it's up to it to free the rtentry as we won't be | |
1883 | * doing it. | |
1884 | */ | |
1885 | if (ret_nrt != NULL) { | |
1886 | /* Return the route to caller with reference intact */ | |
1887 | *ret_nrt = rt; | |
1888 | } else { | |
1889 | /* Dereference or deallocate the route */ | |
1890 | rtfree_locked(rt); | |
1891 | } | |
1892 | if (af == AF_INET) | |
1893 | routegenid_inet_update(); | |
1894 | #if INET6 | |
1895 | else if (af == AF_INET6) | |
1896 | routegenid_inet6_update(); | |
1897 | #endif /* INET6 */ | |
1898 | break; | |
1899 | } | |
1900 | case RTM_RESOLVE: | |
1901 | if (ret_nrt == NULL || (rt = *ret_nrt) == NULL) | |
1902 | senderr(EINVAL); | |
1903 | /* | |
1904 | * According to the UNIX conformance tests, we need to return | |
1905 | * ENETUNREACH when the parent route is RTF_REJECT. | |
1906 | * However, there isn't any point in cloning RTF_REJECT | |
1907 | * routes, so we immediately return an error. | |
1908 | */ | |
1909 | if (rt->rt_flags & RTF_REJECT) { | |
1910 | if (rt->rt_flags & RTF_HOST) { | |
1911 | senderr(EHOSTUNREACH); | |
1912 | } else { | |
1913 | senderr(ENETUNREACH); | |
1914 | } | |
1915 | } | |
1916 | /* | |
1917 | * If cloning, we have the parent route given by the caller | |
1918 | * and will use its rt_gateway, rt_rmx as part of the cloning | |
1919 | * process below. Since rnh_lock is held at this point, the | |
1920 | * parent's rt_ifa and rt_gateway will not change, and its | |
1921 | * relevant rt_flags will not change as well. The only thing | |
1922 | * that could change are the metrics, and thus we hold the | |
1923 | * parent route's rt_lock later on during the actual copying | |
1924 | * of rt_rmx. | |
1925 | */ | |
1926 | ifa = rt->rt_ifa; | |
1927 | IFA_ADDREF(ifa); | |
1928 | flags = rt->rt_flags & | |
1929 | ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); | |
1930 | flags |= RTF_WASCLONED; | |
1931 | gateway = rt->rt_gateway; | |
1932 | if ((netmask = rt->rt_genmask) == NULL) | |
1933 | flags |= RTF_HOST; | |
1934 | ||
1935 | #if INET6 | |
1936 | if (af != AF_INET && af != AF_INET6) | |
1937 | #else | |
1938 | if (af != AF_INET) | |
1939 | #endif /* !INET6 */ | |
1940 | goto makeroute; | |
1941 | ||
1942 | /* | |
1943 | * When scoped routing is enabled, cloned entries are | |
1944 | * always scoped according to the interface portion of | |
1945 | * the parent route. The exception to this are IPv4 | |
1946 | * link local addresses, or those routes that are cloned | |
1947 | * from a RTF_PROXY route. For the latter, the clone | |
1948 | * gets to keep the RTF_PROXY flag. | |
1949 | */ | |
1950 | if ((af == AF_INET && | |
1951 | IN_LINKLOCAL(ntohl(SIN(dst)->sin_addr.s_addr))) || | |
1952 | (rt->rt_flags & RTF_PROXY)) { | |
1953 | ifscope = IFSCOPE_NONE; | |
1954 | flags &= ~RTF_IFSCOPE; | |
1955 | /* | |
1956 | * These types of cloned routes aren't currently | |
1957 | * eligible for idle interface reference counting. | |
1958 | */ | |
1959 | flags |= RTF_NOIFREF; | |
1960 | } else { | |
1961 | if (flags & RTF_IFSCOPE) { | |
1962 | ifscope = (af == AF_INET) ? | |
1963 | sin_get_ifscope(rt_key(rt)) : | |
1964 | sin6_get_ifscope(rt_key(rt)); | |
1965 | } else { | |
1966 | ifscope = rt->rt_ifp->if_index; | |
1967 | flags |= RTF_IFSCOPE; | |
1968 | } | |
1969 | VERIFY(ifscope != IFSCOPE_NONE); | |
1970 | } | |
1971 | ||
1972 | /* | |
1973 | * Transform dst into the internal routing table form, | |
1974 | * clearing out the scope ID field if ifscope isn't set. | |
1975 | */ | |
1976 | dst = sa_copy(dst, &ss, (ifscope == IFSCOPE_NONE) ? | |
1977 | NULL : &ifscope); | |
1978 | ||
1979 | /* Transform netmask into the internal routing table form */ | |
1980 | if (netmask != NULL) | |
1981 | netmask = ma_copy(af, netmask, &mask, ifscope); | |
1982 | ||
1983 | goto makeroute; | |
1984 | ||
1985 | case RTM_ADD: | |
1986 | if ((flags & RTF_GATEWAY) && !gateway) { | |
1987 | panic("rtrequest: RTF_GATEWAY but no gateway"); | |
1988 | /* NOTREACHED */ | |
1989 | } | |
1990 | if (flags & RTF_IFSCOPE) { | |
1991 | ifa = ifa_ifwithroute_scoped_locked(flags, dst0, | |
1992 | gateway, ifscope); | |
1993 | } else { | |
1994 | ifa = ifa_ifwithroute_locked(flags, dst0, gateway); | |
1995 | } | |
1996 | if (ifa == NULL) | |
1997 | senderr(ENETUNREACH); | |
1998 | makeroute: | |
1999 | if ((rt = rte_alloc()) == NULL) | |
2000 | senderr(ENOBUFS); | |
2001 | Bzero(rt, sizeof(*rt)); | |
2002 | rte_lock_init(rt); | |
2003 | getmicrotime(&caltime); | |
2004 | rt->base_calendartime = caltime.tv_sec; | |
2005 | rt->base_uptime = net_uptime(); | |
2006 | RT_LOCK(rt); | |
2007 | rt->rt_flags = RTF_UP | flags; | |
2008 | ||
2009 | /* | |
2010 | * Point the generation ID to the tree's. | |
2011 | */ | |
2012 | switch (af) { | |
2013 | case AF_INET: | |
2014 | rt->rt_tree_genid = &route_genid_inet; | |
2015 | break; | |
2016 | #if INET6 | |
2017 | case AF_INET6: | |
2018 | rt->rt_tree_genid = &route_genid_inet6; | |
2019 | break; | |
2020 | #endif /* INET6 */ | |
2021 | default: | |
2022 | break; | |
2023 | } | |
2024 | ||
2025 | /* | |
2026 | * Add the gateway. Possibly re-malloc-ing the storage for it | |
2027 | * also add the rt_gwroute if possible. | |
2028 | */ | |
2029 | if ((error = rt_setgate(rt, dst, gateway)) != 0) { | |
2030 | int tmp = error; | |
2031 | RT_UNLOCK(rt); | |
2032 | nstat_route_detach(rt); | |
2033 | rte_lock_destroy(rt); | |
2034 | rte_free(rt); | |
2035 | senderr(tmp); | |
2036 | } | |
2037 | ||
2038 | /* | |
2039 | * point to the (possibly newly malloc'd) dest address. | |
2040 | */ | |
2041 | ndst = rt_key(rt); | |
2042 | ||
2043 | /* | |
2044 | * make sure it contains the value we want (masked if needed). | |
2045 | */ | |
2046 | if (netmask) | |
2047 | rt_maskedcopy(dst, ndst, netmask); | |
2048 | else | |
2049 | Bcopy(dst, ndst, dst->sa_len); | |
2050 | ||
2051 | /* | |
2052 | * Note that we now have a reference to the ifa. | |
2053 | * This moved from below so that rnh->rnh_addaddr() can | |
2054 | * examine the ifa and ifa->ifa_ifp if it so desires. | |
2055 | */ | |
2056 | rtsetifa(rt, ifa); | |
2057 | rt->rt_ifp = rt->rt_ifa->ifa_ifp; | |
2058 | ||
2059 | /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ | |
2060 | ||
2061 | rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, | |
2062 | rnh, rt->rt_nodes); | |
2063 | if (rn == 0) { | |
2064 | struct rtentry *rt2; | |
2065 | /* | |
2066 | * Uh-oh, we already have one of these in the tree. | |
2067 | * We do a special hack: if the route that's already | |
2068 | * there was generated by the protocol-cloning | |
2069 | * mechanism, then we just blow it away and retry | |
2070 | * the insertion of the new one. | |
2071 | */ | |
2072 | if (flags & RTF_IFSCOPE) { | |
2073 | rt2 = rtalloc1_scoped_locked(dst0, 0, | |
2074 | RTF_CLONING | RTF_PRCLONING, ifscope); | |
2075 | } else { | |
2076 | rt2 = rtalloc1_locked(dst, 0, | |
2077 | RTF_CLONING | RTF_PRCLONING); | |
2078 | } | |
2079 | if (rt2 && rt2->rt_parent) { | |
2080 | /* | |
2081 | * rnh_lock is held here, so rt_key and | |
2082 | * rt_gateway of rt2 will not change. | |
2083 | */ | |
2084 | (void) rtrequest_locked(RTM_DELETE, rt_key(rt2), | |
2085 | rt2->rt_gateway, rt_mask(rt2), | |
2086 | rt2->rt_flags, 0); | |
2087 | rtfree_locked(rt2); | |
2088 | rn = rnh->rnh_addaddr((caddr_t)ndst, | |
2089 | (caddr_t)netmask, rnh, rt->rt_nodes); | |
2090 | } else if (rt2) { | |
2091 | /* undo the extra ref we got */ | |
2092 | rtfree_locked(rt2); | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | /* | |
2097 | * If it still failed to go into the tree, | |
2098 | * then un-make it (this should be a function) | |
2099 | */ | |
2100 | if (rn == NULL) { | |
2101 | /* Clear gateway route */ | |
2102 | rt_set_gwroute(rt, rt_key(rt), NULL); | |
2103 | if (rt->rt_ifa) { | |
2104 | IFA_REMREF(rt->rt_ifa); | |
2105 | rt->rt_ifa = NULL; | |
2106 | } | |
2107 | R_Free(rt_key(rt)); | |
2108 | RT_UNLOCK(rt); | |
2109 | nstat_route_detach(rt); | |
2110 | rte_lock_destroy(rt); | |
2111 | rte_free(rt); | |
2112 | senderr(EEXIST); | |
2113 | } | |
2114 | ||
2115 | rt->rt_parent = NULL; | |
2116 | ||
2117 | /* | |
2118 | * If we got here from RESOLVE, then we are cloning so clone | |
2119 | * the rest, and note that we are a clone (and increment the | |
2120 | * parent's references). rnh_lock is still held, which prevents | |
2121 | * a lookup from returning the newly-created route. Hence | |
2122 | * holding and releasing the parent's rt_lock while still | |
2123 | * holding the route's rt_lock is safe since the new route | |
2124 | * is not yet externally visible. | |
2125 | */ | |
2126 | if (req == RTM_RESOLVE) { | |
2127 | RT_LOCK_SPIN(*ret_nrt); | |
2128 | VERIFY((*ret_nrt)->rt_expire == 0 || | |
2129 | (*ret_nrt)->rt_rmx.rmx_expire != 0); | |
2130 | VERIFY((*ret_nrt)->rt_expire != 0 || | |
2131 | (*ret_nrt)->rt_rmx.rmx_expire == 0); | |
2132 | rt->rt_rmx = (*ret_nrt)->rt_rmx; | |
2133 | rt_setexpire(rt, (*ret_nrt)->rt_expire); | |
2134 | if ((*ret_nrt)->rt_flags & | |
2135 | (RTF_CLONING | RTF_PRCLONING)) { | |
2136 | rt->rt_parent = (*ret_nrt); | |
2137 | RT_ADDREF_LOCKED(*ret_nrt); | |
2138 | } | |
2139 | RT_UNLOCK(*ret_nrt); | |
2140 | } | |
2141 | ||
2142 | /* | |
2143 | * if this protocol has something to add to this then | |
2144 | * allow it to do that as well. | |
2145 | */ | |
2146 | IFA_LOCK_SPIN(ifa); | |
2147 | ifa_rtrequest = ifa->ifa_rtrequest; | |
2148 | IFA_UNLOCK(ifa); | |
2149 | if (ifa_rtrequest != NULL) | |
2150 | ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : NULL)); | |
2151 | IFA_REMREF(ifa); | |
2152 | ifa = NULL; | |
2153 | ||
2154 | /* | |
2155 | * If this is the (non-scoped) default route, record | |
2156 | * the interface index used for the primary ifscope. | |
2157 | */ | |
2158 | if (rt_primary_default(rt, rt_key(rt))) { | |
2159 | set_primary_ifscope(rt_key(rt)->sa_family, | |
2160 | rt->rt_ifp->if_index); | |
2161 | } | |
2162 | ||
2163 | /* | |
2164 | * actually return a resultant rtentry and | |
2165 | * give the caller a single reference. | |
2166 | */ | |
2167 | if (ret_nrt) { | |
2168 | *ret_nrt = rt; | |
2169 | RT_ADDREF_LOCKED(rt); | |
2170 | } | |
2171 | ||
2172 | if (af == AF_INET) | |
2173 | routegenid_inet_update(); | |
2174 | #if INET6 | |
2175 | else if (af == AF_INET6) | |
2176 | routegenid_inet6_update(); | |
2177 | #endif /* INET6 */ | |
2178 | ||
2179 | RT_GENID_SYNC(rt); | |
2180 | ||
2181 | /* | |
2182 | * We repeat the same procedures from rt_setgate() here | |
2183 | * because they weren't completed when we called it earlier, | |
2184 | * since the node was embryonic. | |
2185 | */ | |
2186 | if ((rt->rt_flags & RTF_GATEWAY) && rt->rt_gwroute != NULL) | |
2187 | rt_set_gwroute(rt, rt_key(rt), rt->rt_gwroute); | |
2188 | ||
2189 | if (req == RTM_ADD && | |
2190 | !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) { | |
2191 | struct rtfc_arg arg; | |
2192 | arg.rnh = rnh; | |
2193 | arg.rt0 = rt; | |
2194 | RT_UNLOCK(rt); | |
2195 | rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), | |
2196 | rt_fixchange, &arg); | |
2197 | } else { | |
2198 | RT_UNLOCK(rt); | |
2199 | } | |
2200 | ||
2201 | nstat_route_new_entry(rt); | |
2202 | break; | |
2203 | } | |
2204 | bad: | |
2205 | if (ifa) | |
2206 | IFA_REMREF(ifa); | |
2207 | return (error); | |
2208 | } | |
2209 | #undef senderr | |
2210 | ||
2211 | int | |
2212 | rtrequest(int req, struct sockaddr *dst, struct sockaddr *gateway, | |
2213 | struct sockaddr *netmask, int flags, struct rtentry **ret_nrt) | |
2214 | { | |
2215 | int error; | |
2216 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
2217 | lck_mtx_lock(rnh_lock); | |
2218 | error = rtrequest_locked(req, dst, gateway, netmask, flags, ret_nrt); | |
2219 | lck_mtx_unlock(rnh_lock); | |
2220 | return (error); | |
2221 | } | |
2222 | ||
2223 | int | |
2224 | rtrequest_scoped(int req, struct sockaddr *dst, struct sockaddr *gateway, | |
2225 | struct sockaddr *netmask, int flags, struct rtentry **ret_nrt, | |
2226 | unsigned int ifscope) | |
2227 | { | |
2228 | int error; | |
2229 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
2230 | lck_mtx_lock(rnh_lock); | |
2231 | error = rtrequest_scoped_locked(req, dst, gateway, netmask, flags, | |
2232 | ret_nrt, ifscope); | |
2233 | lck_mtx_unlock(rnh_lock); | |
2234 | return (error); | |
2235 | } | |
2236 | ||
2237 | /* | |
2238 | * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' | |
2239 | * (i.e., the routes related to it by the operation of cloning). This | |
2240 | * routine is iterated over all potential former-child-routes by way of | |
2241 | * rnh->rnh_walktree_from() above, and those that actually are children of | |
2242 | * the late parent (passed in as VP here) are themselves deleted. | |
2243 | */ | |
2244 | static int | |
2245 | rt_fixdelete(struct radix_node *rn, void *vp) | |
2246 | { | |
2247 | struct rtentry *rt = (struct rtentry *)rn; | |
2248 | struct rtentry *rt0 = vp; | |
2249 | ||
2250 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
2251 | ||
2252 | RT_LOCK(rt); | |
2253 | if (rt->rt_parent == rt0 && | |
2254 | !(rt->rt_flags & (RTF_CLONING | RTF_PRCLONING))) { | |
2255 | /* | |
2256 | * Safe to drop rt_lock and use rt_key, since holding | |
2257 | * rnh_lock here prevents another thread from calling | |
2258 | * rt_setgate() on this route. | |
2259 | */ | |
2260 | RT_UNLOCK(rt); | |
2261 | return (rtrequest_locked(RTM_DELETE, rt_key(rt), NULL, | |
2262 | rt_mask(rt), rt->rt_flags, NULL)); | |
2263 | } | |
2264 | RT_UNLOCK(rt); | |
2265 | return (0); | |
2266 | } | |
2267 | ||
2268 | /* | |
2269 | * This routine is called from rt_setgate() to do the analogous thing for | |
2270 | * adds and changes. There is the added complication in this case of a | |
2271 | * middle insert; i.e., insertion of a new network route between an older | |
2272 | * network route and (cloned) host routes. For this reason, a simple check | |
2273 | * of rt->rt_parent is insufficient; each candidate route must be tested | |
2274 | * against the (mask, value) of the new route (passed as before in vp) | |
2275 | * to see if the new route matches it. | |
2276 | * | |
2277 | * XXX - it may be possible to do fixdelete() for changes and reserve this | |
2278 | * routine just for adds. I'm not sure why I thought it was necessary to do | |
2279 | * changes this way. | |
2280 | */ | |
2281 | static int | |
2282 | rt_fixchange(struct radix_node *rn, void *vp) | |
2283 | { | |
2284 | struct rtentry *rt = (struct rtentry *)rn; | |
2285 | struct rtfc_arg *ap = vp; | |
2286 | struct rtentry *rt0 = ap->rt0; | |
2287 | struct radix_node_head *rnh = ap->rnh; | |
2288 | u_char *xk1, *xm1, *xk2, *xmp; | |
2289 | int i, len; | |
2290 | ||
2291 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
2292 | ||
2293 | RT_LOCK(rt); | |
2294 | ||
2295 | if (!rt->rt_parent || | |
2296 | (rt->rt_flags & (RTF_CLONING | RTF_PRCLONING))) { | |
2297 | RT_UNLOCK(rt); | |
2298 | return (0); | |
2299 | } | |
2300 | ||
2301 | if (rt->rt_parent == rt0) | |
2302 | goto delete_rt; | |
2303 | ||
2304 | /* | |
2305 | * There probably is a function somewhere which does this... | |
2306 | * if not, there should be. | |
2307 | */ | |
2308 | len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len); | |
2309 | ||
2310 | xk1 = (u_char *)rt_key(rt0); | |
2311 | xm1 = (u_char *)rt_mask(rt0); | |
2312 | xk2 = (u_char *)rt_key(rt); | |
2313 | ||
2314 | /* | |
2315 | * Avoid applying a less specific route; do this only if the parent | |
2316 | * route (rt->rt_parent) is a network route, since otherwise its mask | |
2317 | * will be NULL if it is a cloning host route. | |
2318 | */ | |
2319 | if ((xmp = (u_char *)rt_mask(rt->rt_parent)) != NULL) { | |
2320 | int mlen = rt_mask(rt->rt_parent)->sa_len; | |
2321 | if (mlen > rt_mask(rt0)->sa_len) { | |
2322 | RT_UNLOCK(rt); | |
2323 | return (0); | |
2324 | } | |
2325 | ||
2326 | for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) { | |
2327 | if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) { | |
2328 | RT_UNLOCK(rt); | |
2329 | return (0); | |
2330 | } | |
2331 | } | |
2332 | } | |
2333 | ||
2334 | for (i = rnh->rnh_treetop->rn_offset; i < len; i++) { | |
2335 | if ((xk2[i] & xm1[i]) != xk1[i]) { | |
2336 | RT_UNLOCK(rt); | |
2337 | return (0); | |
2338 | } | |
2339 | } | |
2340 | ||
2341 | /* | |
2342 | * OK, this node is a clone, and matches the node currently being | |
2343 | * changed/added under the node's mask. So, get rid of it. | |
2344 | */ | |
2345 | delete_rt: | |
2346 | /* | |
2347 | * Safe to drop rt_lock and use rt_key, since holding rnh_lock here | |
2348 | * prevents another thread from calling rt_setgate() on this route. | |
2349 | */ | |
2350 | RT_UNLOCK(rt); | |
2351 | return (rtrequest_locked(RTM_DELETE, rt_key(rt), NULL, | |
2352 | rt_mask(rt), rt->rt_flags, NULL)); | |
2353 | } | |
2354 | ||
2355 | /* | |
2356 | * Round up sockaddr len to multiples of 32-bytes. This will reduce | |
2357 | * or even eliminate the need to re-allocate the chunk of memory used | |
2358 | * for rt_key and rt_gateway in the event the gateway portion changes. | |
2359 | * Certain code paths (e.g. IPSec) are notorious for caching the address | |
2360 | * of rt_gateway; this rounding-up would help ensure that the gateway | |
2361 | * portion never gets deallocated (though it may change contents) and | |
2362 | * thus greatly simplifies things. | |
2363 | */ | |
2364 | #define SA_SIZE(x) (-(-((uintptr_t)(x)) & -(32))) | |
2365 | ||
2366 | /* | |
2367 | * Sets the gateway and/or gateway route portion of a route; may be | |
2368 | * called on an existing route to modify the gateway portion. Both | |
2369 | * rt_key and rt_gateway are allocated out of the same memory chunk. | |
2370 | * Route entry lock must be held by caller; this routine will return | |
2371 | * with the lock held. | |
2372 | */ | |
2373 | int | |
2374 | rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) | |
2375 | { | |
2376 | int dlen = SA_SIZE(dst->sa_len), glen = SA_SIZE(gate->sa_len); | |
2377 | struct radix_node_head *rnh = NULL; | |
2378 | boolean_t loop = FALSE; | |
2379 | ||
2380 | if (dst->sa_family != AF_INET && dst->sa_family != AF_INET6) { | |
2381 | return (EINVAL); | |
2382 | } | |
2383 | ||
2384 | rnh = rt_tables[dst->sa_family]; | |
2385 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
2386 | RT_LOCK_ASSERT_HELD(rt); | |
2387 | ||
2388 | /* | |
2389 | * If this is for a route that is on its way of being removed, | |
2390 | * or is temporarily frozen, reject the modification request. | |
2391 | */ | |
2392 | if (rt->rt_flags & RTF_CONDEMNED) { | |
2393 | return (EBUSY); | |
2394 | } | |
2395 | ||
2396 | /* Add an extra ref for ourselves */ | |
2397 | RT_ADDREF_LOCKED(rt); | |
2398 | ||
2399 | if (rt->rt_flags & RTF_GATEWAY) { | |
2400 | if ((dst->sa_len == gate->sa_len) && | |
2401 | (dst->sa_family == AF_INET || dst->sa_family == AF_INET6)) { | |
2402 | struct sockaddr_storage dst_ss, gate_ss; | |
2403 | ||
2404 | (void) sa_copy(dst, &dst_ss, NULL); | |
2405 | (void) sa_copy(gate, &gate_ss, NULL); | |
2406 | ||
2407 | loop = equal(SA(&dst_ss), SA(&gate_ss)); | |
2408 | } else { | |
2409 | loop = (dst->sa_len == gate->sa_len && | |
2410 | equal(dst, gate)); | |
2411 | } | |
2412 | } | |
2413 | ||
2414 | /* | |
2415 | * A (cloning) network route with the destination equal to the gateway | |
2416 | * will create an endless loop (see notes below), so disallow it. | |
2417 | */ | |
2418 | if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == | |
2419 | RTF_GATEWAY) && loop) { | |
2420 | /* Release extra ref */ | |
2421 | RT_REMREF_LOCKED(rt); | |
2422 | return (EADDRNOTAVAIL); | |
2423 | } | |
2424 | ||
2425 | /* | |
2426 | * A host route with the destination equal to the gateway | |
2427 | * will interfere with keeping LLINFO in the routing | |
2428 | * table, so disallow it. | |
2429 | */ | |
2430 | if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == | |
2431 | (RTF_HOST|RTF_GATEWAY)) && loop) { | |
2432 | /* | |
2433 | * The route might already exist if this is an RTM_CHANGE | |
2434 | * or a routing redirect, so try to delete it. | |
2435 | */ | |
2436 | if (rt_key(rt) != NULL) { | |
2437 | /* | |
2438 | * Safe to drop rt_lock and use rt_key, rt_gateway, | |
2439 | * since holding rnh_lock here prevents another thread | |
2440 | * from calling rt_setgate() on this route. | |
2441 | */ | |
2442 | RT_UNLOCK(rt); | |
2443 | (void) rtrequest_locked(RTM_DELETE, rt_key(rt), | |
2444 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL); | |
2445 | RT_LOCK(rt); | |
2446 | } | |
2447 | /* Release extra ref */ | |
2448 | RT_REMREF_LOCKED(rt); | |
2449 | return (EADDRNOTAVAIL); | |
2450 | } | |
2451 | ||
2452 | /* | |
2453 | * The destination is not directly reachable. Get a route | |
2454 | * to the next-hop gateway and store it in rt_gwroute. | |
2455 | */ | |
2456 | if (rt->rt_flags & RTF_GATEWAY) { | |
2457 | struct rtentry *gwrt; | |
2458 | unsigned int ifscope; | |
2459 | ||
2460 | if (dst->sa_family == AF_INET) | |
2461 | ifscope = sin_get_ifscope(dst); | |
2462 | else if (dst->sa_family == AF_INET6) | |
2463 | ifscope = sin6_get_ifscope(dst); | |
2464 | else | |
2465 | ifscope = IFSCOPE_NONE; | |
2466 | ||
2467 | RT_UNLOCK(rt); | |
2468 | /* | |
2469 | * Don't ignore RTF_CLONING, since we prefer that rt_gwroute | |
2470 | * points to a clone rather than a cloning route; see above | |
2471 | * check for cloning loop avoidance (dst == gate). | |
2472 | */ | |
2473 | gwrt = rtalloc1_scoped_locked(gate, 1, RTF_PRCLONING, ifscope); | |
2474 | if (gwrt != NULL) | |
2475 | RT_LOCK_ASSERT_NOTHELD(gwrt); | |
2476 | RT_LOCK(rt); | |
2477 | ||
2478 | /* | |
2479 | * Cloning loop avoidance: | |
2480 | * | |
2481 | * In the presence of protocol-cloning and bad configuration, | |
2482 | * it is possible to get stuck in bottomless mutual recursion | |
2483 | * (rtrequest rt_setgate rtalloc1). We avoid this by not | |
2484 | * allowing protocol-cloning to operate for gateways (which | |
2485 | * is probably the correct choice anyway), and avoid the | |
2486 | * resulting reference loops by disallowing any route to run | |
2487 | * through itself as a gateway. This is obviously mandatory | |
2488 | * when we get rt->rt_output(). It implies that a route to | |
2489 | * the gateway must already be present in the system in order | |
2490 | * for the gateway to be referred to by another route. | |
2491 | */ | |
2492 | if (gwrt == rt) { | |
2493 | RT_REMREF_LOCKED(gwrt); | |
2494 | /* Release extra ref */ | |
2495 | RT_REMREF_LOCKED(rt); | |
2496 | return (EADDRINUSE); /* failure */ | |
2497 | } | |
2498 | ||
2499 | /* | |
2500 | * If scoped, the gateway route must use the same interface; | |
2501 | * we're holding rnh_lock now, so rt_gateway and rt_ifp of gwrt | |
2502 | * should not change and are freely accessible. | |
2503 | */ | |
2504 | if (ifscope != IFSCOPE_NONE && (rt->rt_flags & RTF_IFSCOPE) && | |
2505 | gwrt != NULL && gwrt->rt_ifp != NULL && | |
2506 | gwrt->rt_ifp->if_index != ifscope) { | |
2507 | rtfree_locked(gwrt); /* rt != gwrt, no deadlock */ | |
2508 | /* Release extra ref */ | |
2509 | RT_REMREF_LOCKED(rt); | |
2510 | return ((rt->rt_flags & RTF_HOST) ? | |
2511 | EHOSTUNREACH : ENETUNREACH); | |
2512 | } | |
2513 | ||
2514 | /* Check again since we dropped the lock above */ | |
2515 | if (rt->rt_flags & RTF_CONDEMNED) { | |
2516 | if (gwrt != NULL) | |
2517 | rtfree_locked(gwrt); | |
2518 | /* Release extra ref */ | |
2519 | RT_REMREF_LOCKED(rt); | |
2520 | return (EBUSY); | |
2521 | } | |
2522 | ||
2523 | /* Set gateway route; callee adds ref to gwrt if non-NULL */ | |
2524 | rt_set_gwroute(rt, dst, gwrt); | |
2525 | ||
2526 | /* | |
2527 | * In case the (non-scoped) default route gets modified via | |
2528 | * an ICMP redirect, record the interface index used for the | |
2529 | * primary ifscope. Also done in rt_setif() to take care | |
2530 | * of the non-redirect cases. | |
2531 | */ | |
2532 | if (rt_primary_default(rt, dst) && rt->rt_ifp != NULL) { | |
2533 | set_primary_ifscope(dst->sa_family, | |
2534 | rt->rt_ifp->if_index); | |
2535 | } | |
2536 | ||
2537 | /* | |
2538 | * Tell the kernel debugger about the new default gateway | |
2539 | * if the gateway route uses the primary interface, or | |
2540 | * if we are in a transient state before the non-scoped | |
2541 | * default gateway is installed (similar to how the system | |
2542 | * was behaving in the past). In future, it would be good | |
2543 | * to do all this only when KDP is enabled. | |
2544 | */ | |
2545 | if ((dst->sa_family == AF_INET) && | |
2546 | gwrt != NULL && gwrt->rt_gateway->sa_family == AF_LINK && | |
2547 | (gwrt->rt_ifp->if_index == get_primary_ifscope(AF_INET) || | |
2548 | get_primary_ifscope(AF_INET) == IFSCOPE_NONE)) { | |
2549 | kdp_set_gateway_mac(SDL((void *)gwrt->rt_gateway)-> | |
2550 | sdl_data); | |
2551 | } | |
2552 | ||
2553 | /* Release extra ref from rtalloc1() */ | |
2554 | if (gwrt != NULL) | |
2555 | RT_REMREF(gwrt); | |
2556 | } | |
2557 | ||
2558 | /* | |
2559 | * Prepare to store the gateway in rt_gateway. Both dst and gateway | |
2560 | * are stored one after the other in the same malloc'd chunk. If we | |
2561 | * have room, reuse the old buffer since rt_gateway already points | |
2562 | * to the right place. Otherwise, malloc a new block and update | |
2563 | * the 'dst' address and point rt_gateway to the right place. | |
2564 | */ | |
2565 | if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway->sa_len)) { | |
2566 | caddr_t new; | |
2567 | ||
2568 | /* The underlying allocation is done with M_WAITOK set */ | |
2569 | R_Malloc(new, caddr_t, dlen + glen); | |
2570 | if (new == NULL) { | |
2571 | /* Clear gateway route */ | |
2572 | rt_set_gwroute(rt, dst, NULL); | |
2573 | /* Release extra ref */ | |
2574 | RT_REMREF_LOCKED(rt); | |
2575 | return (ENOBUFS); | |
2576 | } | |
2577 | ||
2578 | /* | |
2579 | * Copy from 'dst' and not rt_key(rt) because we can get | |
2580 | * here to initialize a newly allocated route entry, in | |
2581 | * which case rt_key(rt) is NULL (and so does rt_gateway). | |
2582 | */ | |
2583 | bzero(new, dlen + glen); | |
2584 | Bcopy(dst, new, dst->sa_len); | |
2585 | R_Free(rt_key(rt)); /* free old block; NULL is okay */ | |
2586 | rt->rt_nodes->rn_key = new; | |
2587 | rt->rt_gateway = (struct sockaddr *)(new + dlen); | |
2588 | } | |
2589 | ||
2590 | /* | |
2591 | * Copy the new gateway value into the memory chunk. | |
2592 | */ | |
2593 | Bcopy(gate, rt->rt_gateway, gate->sa_len); | |
2594 | ||
2595 | /* | |
2596 | * For consistency between rt_gateway and rt_key(gwrt). | |
2597 | */ | |
2598 | if ((rt->rt_flags & RTF_GATEWAY) && rt->rt_gwroute != NULL && | |
2599 | (rt->rt_gwroute->rt_flags & RTF_IFSCOPE)) { | |
2600 | if (rt->rt_gateway->sa_family == AF_INET && | |
2601 | rt_key(rt->rt_gwroute)->sa_family == AF_INET) { | |
2602 | sin_set_ifscope(rt->rt_gateway, | |
2603 | sin_get_ifscope(rt_key(rt->rt_gwroute))); | |
2604 | } else if (rt->rt_gateway->sa_family == AF_INET6 && | |
2605 | rt_key(rt->rt_gwroute)->sa_family == AF_INET6) { | |
2606 | sin6_set_ifscope(rt->rt_gateway, | |
2607 | sin6_get_ifscope(rt_key(rt->rt_gwroute))); | |
2608 | } | |
2609 | } | |
2610 | ||
2611 | /* | |
2612 | * This isn't going to do anything useful for host routes, so | |
2613 | * don't bother. Also make sure we have a reasonable mask | |
2614 | * (we don't yet have one during adds). | |
2615 | */ | |
2616 | if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { | |
2617 | struct rtfc_arg arg; | |
2618 | arg.rnh = rnh; | |
2619 | arg.rt0 = rt; | |
2620 | RT_UNLOCK(rt); | |
2621 | rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), | |
2622 | rt_fixchange, &arg); | |
2623 | RT_LOCK(rt); | |
2624 | } | |
2625 | ||
2626 | /* Release extra ref */ | |
2627 | RT_REMREF_LOCKED(rt); | |
2628 | return (0); | |
2629 | } | |
2630 | ||
2631 | #undef SA_SIZE | |
2632 | ||
2633 | void | |
2634 | rt_set_gwroute(struct rtentry *rt, struct sockaddr *dst, struct rtentry *gwrt) | |
2635 | { | |
2636 | boolean_t gwrt_isrouter; | |
2637 | ||
2638 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
2639 | RT_LOCK_ASSERT_HELD(rt); | |
2640 | ||
2641 | if (gwrt != NULL) | |
2642 | RT_ADDREF(gwrt); /* for this routine */ | |
2643 | ||
2644 | /* | |
2645 | * Get rid of existing gateway route; if rt_gwroute is already | |
2646 | * set to gwrt, this is slightly redundant (though safe since | |
2647 | * we held an extra ref above) but makes the code simpler. | |
2648 | */ | |
2649 | if (rt->rt_gwroute != NULL) { | |
2650 | struct rtentry *ogwrt = rt->rt_gwroute; | |
2651 | ||
2652 | VERIFY(rt != ogwrt); /* sanity check */ | |
2653 | rt->rt_gwroute = NULL; | |
2654 | RT_UNLOCK(rt); | |
2655 | rtfree_locked(ogwrt); | |
2656 | RT_LOCK(rt); | |
2657 | VERIFY(rt->rt_gwroute == NULL); | |
2658 | } | |
2659 | ||
2660 | /* | |
2661 | * And associate the new gateway route. | |
2662 | */ | |
2663 | if ((rt->rt_gwroute = gwrt) != NULL) { | |
2664 | RT_ADDREF(gwrt); /* for rt */ | |
2665 | ||
2666 | if (rt->rt_flags & RTF_WASCLONED) { | |
2667 | /* rt_parent might be NULL if rt is embryonic */ | |
2668 | gwrt_isrouter = (rt->rt_parent != NULL && | |
2669 | SA_DEFAULT(rt_key(rt->rt_parent)) && | |
2670 | !RT_HOST(rt->rt_parent)); | |
2671 | } else { | |
2672 | gwrt_isrouter = (SA_DEFAULT(dst) && !RT_HOST(rt)); | |
2673 | } | |
2674 | ||
2675 | /* If gwrt points to a default router, mark it accordingly */ | |
2676 | if (gwrt_isrouter && RT_HOST(gwrt) && | |
2677 | !(gwrt->rt_flags & RTF_ROUTER)) { | |
2678 | RT_LOCK(gwrt); | |
2679 | gwrt->rt_flags |= RTF_ROUTER; | |
2680 | RT_UNLOCK(gwrt); | |
2681 | } | |
2682 | ||
2683 | RT_REMREF(gwrt); /* for this routine */ | |
2684 | } | |
2685 | } | |
2686 | ||
2687 | static void | |
2688 | rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst, | |
2689 | const struct sockaddr *netmask) | |
2690 | { | |
2691 | const char *netmaskp = &netmask->sa_data[0]; | |
2692 | const char *srcp = &src->sa_data[0]; | |
2693 | char *dstp = &dst->sa_data[0]; | |
2694 | const char *maskend = (char *)dst | |
2695 | + MIN(netmask->sa_len, src->sa_len); | |
2696 | const char *srcend = (char *)dst + src->sa_len; | |
2697 | ||
2698 | dst->sa_len = src->sa_len; | |
2699 | dst->sa_family = src->sa_family; | |
2700 | ||
2701 | while (dstp < maskend) | |
2702 | *dstp++ = *srcp++ & *netmaskp++; | |
2703 | if (dstp < srcend) | |
2704 | memset(dstp, 0, (size_t)(srcend - dstp)); | |
2705 | } | |
2706 | ||
2707 | /* | |
2708 | * Lookup an AF_INET/AF_INET6 scoped or non-scoped route depending on the | |
2709 | * ifscope value passed in by the caller (IFSCOPE_NONE implies non-scoped). | |
2710 | */ | |
2711 | static struct radix_node * | |
2712 | node_lookup(struct sockaddr *dst, struct sockaddr *netmask, | |
2713 | unsigned int ifscope) | |
2714 | { | |
2715 | struct radix_node_head *rnh; | |
2716 | struct radix_node *rn; | |
2717 | struct sockaddr_storage ss, mask; | |
2718 | int af = dst->sa_family; | |
2719 | struct matchleaf_arg ma = { ifscope }; | |
2720 | rn_matchf_t *f = rn_match_ifscope; | |
2721 | void *w = &ma; | |
2722 | ||
2723 | if (af != AF_INET && af != AF_INET6) | |
2724 | return (NULL); | |
2725 | ||
2726 | rnh = rt_tables[af]; | |
2727 | ||
2728 | /* | |
2729 | * Transform dst into the internal routing table form, | |
2730 | * clearing out the scope ID field if ifscope isn't set. | |
2731 | */ | |
2732 | dst = sa_copy(dst, &ss, (ifscope == IFSCOPE_NONE) ? NULL : &ifscope); | |
2733 | ||
2734 | /* Transform netmask into the internal routing table form */ | |
2735 | if (netmask != NULL) | |
2736 | netmask = ma_copy(af, netmask, &mask, ifscope); | |
2737 | ||
2738 | if (ifscope == IFSCOPE_NONE) | |
2739 | f = w = NULL; | |
2740 | ||
2741 | rn = rnh->rnh_lookup_args(dst, netmask, rnh, f, w); | |
2742 | if (rn != NULL && (rn->rn_flags & RNF_ROOT)) | |
2743 | rn = NULL; | |
2744 | ||
2745 | return (rn); | |
2746 | } | |
2747 | ||
2748 | /* | |
2749 | * Lookup the AF_INET/AF_INET6 non-scoped default route. | |
2750 | */ | |
2751 | static struct radix_node * | |
2752 | node_lookup_default(int af) | |
2753 | { | |
2754 | struct radix_node_head *rnh; | |
2755 | ||
2756 | VERIFY(af == AF_INET || af == AF_INET6); | |
2757 | rnh = rt_tables[af]; | |
2758 | ||
2759 | return (af == AF_INET ? rnh->rnh_lookup(&sin_def, NULL, rnh) : | |
2760 | rnh->rnh_lookup(&sin6_def, NULL, rnh)); | |
2761 | } | |
2762 | ||
2763 | boolean_t | |
2764 | rt_ifa_is_dst(struct sockaddr *dst, struct ifaddr *ifa) | |
2765 | { | |
2766 | boolean_t result = FALSE; | |
2767 | ||
2768 | if (ifa == NULL || ifa->ifa_addr == NULL) | |
2769 | return (result); | |
2770 | ||
2771 | IFA_LOCK_SPIN(ifa); | |
2772 | ||
2773 | if (dst->sa_family == ifa->ifa_addr->sa_family && | |
2774 | ((dst->sa_family == AF_INET && | |
2775 | SIN(dst)->sin_addr.s_addr == | |
2776 | SIN(ifa->ifa_addr)->sin_addr.s_addr) || | |
2777 | (dst->sa_family == AF_INET6 && | |
2778 | SA6_ARE_ADDR_EQUAL(SIN6(dst), SIN6(ifa->ifa_addr))))) | |
2779 | result = TRUE; | |
2780 | ||
2781 | IFA_UNLOCK(ifa); | |
2782 | ||
2783 | return (result); | |
2784 | } | |
2785 | ||
2786 | /* | |
2787 | * Common routine to lookup/match a route. It invokes the lookup/matchaddr | |
2788 | * callback which could be address family-specific. The main difference | |
2789 | * between the two (at least for AF_INET/AF_INET6) is that a lookup does | |
2790 | * not alter the expiring state of a route, whereas a match would unexpire | |
2791 | * or revalidate the route. | |
2792 | * | |
2793 | * The optional scope or interface index property of a route allows for a | |
2794 | * per-interface route instance. This permits multiple route entries having | |
2795 | * the same destination (but not necessarily the same gateway) to exist in | |
2796 | * the routing table; each of these entries is specific to the corresponding | |
2797 | * interface. This is made possible by storing the scope ID value into the | |
2798 | * radix key, thus making each route entry unique. These scoped entries | |
2799 | * exist along with the regular, non-scoped entries in the same radix tree | |
2800 | * for a given address family (AF_INET/AF_INET6); the scope logically | |
2801 | * partitions it into multiple per-interface sub-trees. | |
2802 | * | |
2803 | * When a scoped route lookup is performed, the routing table is searched for | |
2804 | * the best match that would result in a route using the same interface as the | |
2805 | * one associated with the scope (the exception to this are routes that point | |
2806 | * to the loopback interface). The search rule follows the longest matching | |
2807 | * prefix with the additional interface constraint. | |
2808 | */ | |
2809 | static struct rtentry * | |
2810 | rt_lookup_common(boolean_t lookup_only, boolean_t coarse, struct sockaddr *dst, | |
2811 | struct sockaddr *netmask, struct radix_node_head *rnh, unsigned int ifscope) | |
2812 | { | |
2813 | struct radix_node *rn0, *rn = NULL; | |
2814 | int af = dst->sa_family; | |
2815 | struct sockaddr_storage dst_ss; | |
2816 | struct sockaddr_storage mask_ss; | |
2817 | boolean_t dontcare; | |
2818 | #if (DEVELOPMENT || DEBUG) | |
2819 | char dbuf[MAX_SCOPE_ADDR_STR_LEN], gbuf[MAX_IPv6_STR_LEN]; | |
2820 | char s_dst[MAX_IPv6_STR_LEN], s_netmask[MAX_IPv6_STR_LEN]; | |
2821 | #endif | |
2822 | VERIFY(!coarse || ifscope == IFSCOPE_NONE); | |
2823 | ||
2824 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
2825 | #if INET6 | |
2826 | /* | |
2827 | * While we have rnh_lock held, see if we need to schedule the timer. | |
2828 | */ | |
2829 | if (nd6_sched_timeout_want) | |
2830 | nd6_sched_timeout(NULL, NULL); | |
2831 | #endif /* INET6 */ | |
2832 | ||
2833 | if (!lookup_only) | |
2834 | netmask = NULL; | |
2835 | ||
2836 | /* | |
2837 | * Non-scoped route lookup. | |
2838 | */ | |
2839 | #if INET6 | |
2840 | if (af != AF_INET && af != AF_INET6) { | |
2841 | #else | |
2842 | if (af != AF_INET) { | |
2843 | #endif /* !INET6 */ | |
2844 | rn = rnh->rnh_matchaddr(dst, rnh); | |
2845 | ||
2846 | /* | |
2847 | * Don't return a root node; also, rnh_matchaddr callback | |
2848 | * would have done the necessary work to clear RTPRF_OURS | |
2849 | * for certain protocol families. | |
2850 | */ | |
2851 | if (rn != NULL && (rn->rn_flags & RNF_ROOT)) | |
2852 | rn = NULL; | |
2853 | if (rn != NULL) { | |
2854 | RT_LOCK_SPIN(RT(rn)); | |
2855 | if (!(RT(rn)->rt_flags & RTF_CONDEMNED)) { | |
2856 | RT_ADDREF_LOCKED(RT(rn)); | |
2857 | RT_UNLOCK(RT(rn)); | |
2858 | } else { | |
2859 | RT_UNLOCK(RT(rn)); | |
2860 | rn = NULL; | |
2861 | } | |
2862 | } | |
2863 | return (RT(rn)); | |
2864 | } | |
2865 | ||
2866 | /* Transform dst/netmask into the internal routing table form */ | |
2867 | dst = sa_copy(dst, &dst_ss, &ifscope); | |
2868 | if (netmask != NULL) | |
2869 | netmask = ma_copy(af, netmask, &mask_ss, ifscope); | |
2870 | dontcare = (ifscope == IFSCOPE_NONE); | |
2871 | ||
2872 | #if (DEVELOPMENT || DEBUG) | |
2873 | if (rt_verbose) { | |
2874 | if (af == AF_INET) | |
2875 | (void) inet_ntop(af, &SIN(dst)->sin_addr.s_addr, | |
2876 | s_dst, sizeof (s_dst)); | |
2877 | else | |
2878 | (void) inet_ntop(af, &SIN6(dst)->sin6_addr, | |
2879 | s_dst, sizeof (s_dst)); | |
2880 | ||
2881 | if (netmask != NULL && af == AF_INET) | |
2882 | (void) inet_ntop(af, &SIN(netmask)->sin_addr.s_addr, | |
2883 | s_netmask, sizeof (s_netmask)); | |
2884 | if (netmask != NULL && af == AF_INET6) | |
2885 | (void) inet_ntop(af, &SIN6(netmask)->sin6_addr, | |
2886 | s_netmask, sizeof (s_netmask)); | |
2887 | else | |
2888 | *s_netmask = '\0'; | |
2889 | printf("%s (%d, %d, %s, %s, %u)\n", | |
2890 | __func__, lookup_only, coarse, s_dst, s_netmask, ifscope); | |
2891 | } | |
2892 | #endif | |
2893 | ||
2894 | /* | |
2895 | * Scoped route lookup: | |
2896 | * | |
2897 | * We first perform a non-scoped lookup for the original result. | |
2898 | * Afterwards, depending on whether or not the caller has specified | |
2899 | * a scope, we perform a more specific scoped search and fallback | |
2900 | * to this original result upon failure. | |
2901 | */ | |
2902 | rn0 = rn = node_lookup(dst, netmask, IFSCOPE_NONE); | |
2903 | ||
2904 | /* | |
2905 | * If the caller did not specify a scope, use the primary scope | |
2906 | * derived from the system's non-scoped default route. If, for | |
2907 | * any reason, there is no primary interface, ifscope will be | |
2908 | * set to IFSCOPE_NONE; if the above lookup resulted in a route, | |
2909 | * we'll do a more-specific search below, scoped to the interface | |
2910 | * of that route. | |
2911 | */ | |
2912 | if (dontcare) | |
2913 | ifscope = get_primary_ifscope(af); | |
2914 | ||
2915 | /* | |
2916 | * Keep the original result if either of the following is true: | |
2917 | * | |
2918 | * 1) The interface portion of the route has the same interface | |
2919 | * index as the scope value and it is marked with RTF_IFSCOPE. | |
2920 | * 2) The route uses the loopback interface, in which case the | |
2921 | * destination (host/net) is local/loopback. | |
2922 | * | |
2923 | * Otherwise, do a more specified search using the scope; | |
2924 | * we're holding rnh_lock now, so rt_ifp should not change. | |
2925 | */ | |
2926 | if (rn != NULL) { | |
2927 | struct rtentry *rt = RT(rn); | |
2928 | #if (DEVELOPMENT || DEBUG) | |
2929 | if (rt_verbose) { | |
2930 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
2931 | printf("%s unscoped search %p to %s->%s->%s ifa_ifp %s\n", | |
2932 | __func__, rt, | |
2933 | dbuf, gbuf, | |
2934 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
2935 | (rt->rt_ifa->ifa_ifp != NULL) ? | |
2936 | rt->rt_ifa->ifa_ifp->if_xname : ""); | |
2937 | } | |
2938 | #endif | |
2939 | if (!(rt->rt_ifp->if_flags & IFF_LOOPBACK) || | |
2940 | (rt->rt_flags & RTF_GATEWAY)) { | |
2941 | if (rt->rt_ifp->if_index != ifscope) { | |
2942 | /* | |
2943 | * Wrong interface; keep the original result | |
2944 | * only if the caller did not specify a scope, | |
2945 | * and do a more specific scoped search using | |
2946 | * the scope of the found route. Otherwise, | |
2947 | * start again from scratch. | |
2948 | * | |
2949 | * For loopback scope we keep the unscoped | |
2950 | * route for local addresses | |
2951 | */ | |
2952 | rn = NULL; | |
2953 | if (dontcare) | |
2954 | ifscope = rt->rt_ifp->if_index; | |
2955 | else if (ifscope != lo_ifp->if_index || | |
2956 | rt_ifa_is_dst(dst, rt->rt_ifa) == FALSE) | |
2957 | rn0 = NULL; | |
2958 | } else if (!(rt->rt_flags & RTF_IFSCOPE)) { | |
2959 | /* | |
2960 | * Right interface, except that this route | |
2961 | * isn't marked with RTF_IFSCOPE. Do a more | |
2962 | * specific scoped search. Keep the original | |
2963 | * result and return it it in case the scoped | |
2964 | * search fails. | |
2965 | */ | |
2966 | rn = NULL; | |
2967 | } | |
2968 | } | |
2969 | } | |
2970 | ||
2971 | /* | |
2972 | * Scoped search. Find the most specific entry having the same | |
2973 | * interface scope as the one requested. The following will result | |
2974 | * in searching for the longest prefix scoped match. | |
2975 | */ | |
2976 | if (rn == NULL) { | |
2977 | rn = node_lookup(dst, netmask, ifscope); | |
2978 | #if (DEVELOPMENT || DEBUG) | |
2979 | if (rt_verbose && rn != NULL) { | |
2980 | struct rtentry *rt = RT(rn); | |
2981 | ||
2982 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
2983 | printf("%s scoped search %p to %s->%s->%s ifa %s\n", | |
2984 | __func__, rt, | |
2985 | dbuf, gbuf, | |
2986 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
2987 | (rt->rt_ifa->ifa_ifp != NULL) ? | |
2988 | rt->rt_ifa->ifa_ifp->if_xname : ""); | |
2989 | } | |
2990 | #endif | |
2991 | } | |
2992 | /* | |
2993 | * Use the original result if either of the following is true: | |
2994 | * | |
2995 | * 1) The scoped search did not yield any result. | |
2996 | * 2) The caller insists on performing a coarse-grained lookup. | |
2997 | * 3) The result from the scoped search is a scoped default route, | |
2998 | * and the original (non-scoped) result is not a default route, | |
2999 | * i.e. the original result is a more specific host/net route. | |
3000 | * 4) The scoped search yielded a net route but the original | |
3001 | * result is a host route, i.e. the original result is treated | |
3002 | * as a more specific route. | |
3003 | */ | |
3004 | if (rn == NULL || coarse || (rn0 != NULL && | |
3005 | ((SA_DEFAULT(rt_key(RT(rn))) && !SA_DEFAULT(rt_key(RT(rn0)))) || | |
3006 | (!RT_HOST(rn) && RT_HOST(rn0))))) | |
3007 | rn = rn0; | |
3008 | ||
3009 | /* | |
3010 | * If we still don't have a route, use the non-scoped default | |
3011 | * route as long as the interface portion satistifes the scope. | |
3012 | */ | |
3013 | if (rn == NULL && (rn = node_lookup_default(af)) != NULL && | |
3014 | RT(rn)->rt_ifp->if_index != ifscope) { | |
3015 | rn = NULL; | |
3016 | } | |
3017 | ||
3018 | if (rn != NULL) { | |
3019 | /* | |
3020 | * Manually clear RTPRF_OURS using rt_validate() and | |
3021 | * bump up the reference count after, and not before; | |
3022 | * we only get here for AF_INET/AF_INET6. node_lookup() | |
3023 | * has done the check against RNF_ROOT, so we can be sure | |
3024 | * that we're not returning a root node here. | |
3025 | */ | |
3026 | RT_LOCK_SPIN(RT(rn)); | |
3027 | if (rt_validate(RT(rn))) { | |
3028 | RT_ADDREF_LOCKED(RT(rn)); | |
3029 | RT_UNLOCK(RT(rn)); | |
3030 | } else { | |
3031 | RT_UNLOCK(RT(rn)); | |
3032 | rn = NULL; | |
3033 | } | |
3034 | } | |
3035 | #if (DEVELOPMENT || DEBUG) | |
3036 | if (rt_verbose) { | |
3037 | if (rn == NULL) | |
3038 | printf("%s %u return NULL\n", __func__, ifscope); | |
3039 | else { | |
3040 | struct rtentry *rt = RT(rn); | |
3041 | ||
3042 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
3043 | ||
3044 | printf("%s %u return %p to %s->%s->%s ifa_ifp %s\n", | |
3045 | __func__, ifscope, rt, | |
3046 | dbuf, gbuf, | |
3047 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
3048 | (rt->rt_ifa->ifa_ifp != NULL) ? | |
3049 | rt->rt_ifa->ifa_ifp->if_xname : ""); | |
3050 | } | |
3051 | } | |
3052 | #endif | |
3053 | return (RT(rn)); | |
3054 | } | |
3055 | ||
3056 | struct rtentry * | |
3057 | rt_lookup(boolean_t lookup_only, struct sockaddr *dst, struct sockaddr *netmask, | |
3058 | struct radix_node_head *rnh, unsigned int ifscope) | |
3059 | { | |
3060 | return (rt_lookup_common(lookup_only, FALSE, dst, netmask, | |
3061 | rnh, ifscope)); | |
3062 | } | |
3063 | ||
3064 | struct rtentry * | |
3065 | rt_lookup_coarse(boolean_t lookup_only, struct sockaddr *dst, | |
3066 | struct sockaddr *netmask, struct radix_node_head *rnh) | |
3067 | { | |
3068 | return (rt_lookup_common(lookup_only, TRUE, dst, netmask, | |
3069 | rnh, IFSCOPE_NONE)); | |
3070 | } | |
3071 | ||
3072 | boolean_t | |
3073 | rt_validate(struct rtentry *rt) | |
3074 | { | |
3075 | RT_LOCK_ASSERT_HELD(rt); | |
3076 | ||
3077 | if ((rt->rt_flags & (RTF_UP | RTF_CONDEMNED)) == RTF_UP) { | |
3078 | int af = rt_key(rt)->sa_family; | |
3079 | ||
3080 | if (af == AF_INET) | |
3081 | (void) in_validate(RN(rt)); | |
3082 | else if (af == AF_INET6) | |
3083 | (void) in6_validate(RN(rt)); | |
3084 | } else { | |
3085 | rt = NULL; | |
3086 | } | |
3087 | ||
3088 | return (rt != NULL); | |
3089 | } | |
3090 | ||
3091 | /* | |
3092 | * Set up a routing table entry, normally | |
3093 | * for an interface. | |
3094 | */ | |
3095 | int | |
3096 | rtinit(struct ifaddr *ifa, int cmd, int flags) | |
3097 | { | |
3098 | int error; | |
3099 | ||
3100 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); | |
3101 | ||
3102 | lck_mtx_lock(rnh_lock); | |
3103 | error = rtinit_locked(ifa, cmd, flags); | |
3104 | lck_mtx_unlock(rnh_lock); | |
3105 | ||
3106 | return (error); | |
3107 | } | |
3108 | ||
3109 | int | |
3110 | rtinit_locked(struct ifaddr *ifa, int cmd, int flags) | |
3111 | { | |
3112 | struct radix_node_head *rnh; | |
3113 | uint8_t nbuf[128]; /* long enough for IPv6 */ | |
3114 | #if (DEVELOPMENT || DEBUG) | |
3115 | char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN]; | |
3116 | char abuf[MAX_IPv6_STR_LEN]; | |
3117 | #endif | |
3118 | struct rtentry *rt = NULL; | |
3119 | struct sockaddr *dst; | |
3120 | struct sockaddr *netmask; | |
3121 | int error = 0; | |
3122 | ||
3123 | /* | |
3124 | * Holding rnh_lock here prevents the possibility of ifa from | |
3125 | * changing (e.g. in_ifinit), so it is safe to access its | |
3126 | * ifa_{dst}addr (here and down below) without locking. | |
3127 | */ | |
3128 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); | |
3129 | ||
3130 | if (flags & RTF_HOST) { | |
3131 | dst = ifa->ifa_dstaddr; | |
3132 | netmask = NULL; | |
3133 | } else { | |
3134 | dst = ifa->ifa_addr; | |
3135 | netmask = ifa->ifa_netmask; | |
3136 | } | |
3137 | ||
3138 | if (dst->sa_len == 0) { | |
3139 | log(LOG_ERR, "%s: %s failed, invalid dst sa_len %d\n", | |
3140 | __func__, rtm2str(cmd), dst->sa_len); | |
3141 | error = EINVAL; | |
3142 | goto done; | |
3143 | } | |
3144 | if (netmask != NULL && netmask->sa_len > sizeof (nbuf)) { | |
3145 | log(LOG_ERR, "%s: %s failed, mask sa_len %d too large\n", | |
3146 | __func__, rtm2str(cmd), dst->sa_len); | |
3147 | error = EINVAL; | |
3148 | goto done; | |
3149 | } | |
3150 | ||
3151 | #if (DEVELOPMENT || DEBUG) | |
3152 | if (dst->sa_family == AF_INET) { | |
3153 | (void) inet_ntop(AF_INET, &SIN(dst)->sin_addr.s_addr, | |
3154 | abuf, sizeof (abuf)); | |
3155 | } | |
3156 | #if INET6 | |
3157 | else if (dst->sa_family == AF_INET6) { | |
3158 | (void) inet_ntop(AF_INET6, &SIN6(dst)->sin6_addr, | |
3159 | abuf, sizeof (abuf)); | |
3160 | } | |
3161 | #endif /* INET6 */ | |
3162 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3163 | ||
3164 | if ((rnh = rt_tables[dst->sa_family]) == NULL) { | |
3165 | error = EINVAL; | |
3166 | goto done; | |
3167 | } | |
3168 | ||
3169 | /* | |
3170 | * If it's a delete, check that if it exists, it's on the correct | |
3171 | * interface or we might scrub a route to another ifa which would | |
3172 | * be confusing at best and possibly worse. | |
3173 | */ | |
3174 | if (cmd == RTM_DELETE) { | |
3175 | /* | |
3176 | * It's a delete, so it should already exist.. | |
3177 | * If it's a net, mask off the host bits | |
3178 | * (Assuming we have a mask) | |
3179 | */ | |
3180 | if (netmask != NULL) { | |
3181 | rt_maskedcopy(dst, SA(nbuf), netmask); | |
3182 | dst = SA(nbuf); | |
3183 | } | |
3184 | /* | |
3185 | * Get an rtentry that is in the routing tree and contains | |
3186 | * the correct info. Note that we perform a coarse-grained | |
3187 | * lookup here, in case there is a scoped variant of the | |
3188 | * subnet/prefix route which we should ignore, as we never | |
3189 | * add a scoped subnet/prefix route as part of adding an | |
3190 | * interface address. | |
3191 | */ | |
3192 | rt = rt_lookup_coarse(TRUE, dst, NULL, rnh); | |
3193 | if (rt != NULL) { | |
3194 | #if (DEVELOPMENT || DEBUG) | |
3195 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
3196 | #endif | |
3197 | /* | |
3198 | * Ok so we found the rtentry. it has an extra reference | |
3199 | * for us at this stage. we won't need that so | |
3200 | * lop that off now. | |
3201 | */ | |
3202 | RT_LOCK(rt); | |
3203 | if (rt->rt_ifa != ifa) { | |
3204 | /* | |
3205 | * If the interface address in the rtentry | |
3206 | * doesn't match the interface we are using, | |
3207 | * then we don't want to delete it, so return | |
3208 | * an error. This seems to be the only point | |
3209 | * of this whole RTM_DELETE clause. | |
3210 | */ | |
3211 | #if (DEVELOPMENT || DEBUG) | |
3212 | if (rt_verbose) { | |
3213 | log(LOG_DEBUG, "%s: not removing " | |
3214 | "route to %s->%s->%s, flags %b, " | |
3215 | "ifaddr %s, rt_ifa 0x%llx != " | |
3216 | "ifa 0x%llx\n", __func__, dbuf, | |
3217 | gbuf, ((rt->rt_ifp != NULL) ? | |
3218 | rt->rt_ifp->if_xname : ""), | |
3219 | rt->rt_flags, RTF_BITS, abuf, | |
3220 | (uint64_t)VM_KERNEL_ADDRPERM( | |
3221 | rt->rt_ifa), | |
3222 | (uint64_t)VM_KERNEL_ADDRPERM(ifa)); | |
3223 | } | |
3224 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3225 | RT_REMREF_LOCKED(rt); | |
3226 | RT_UNLOCK(rt); | |
3227 | rt = NULL; | |
3228 | error = ((flags & RTF_HOST) ? | |
3229 | EHOSTUNREACH : ENETUNREACH); | |
3230 | goto done; | |
3231 | } else if (rt->rt_flags & RTF_STATIC) { | |
3232 | /* | |
3233 | * Don't remove the subnet/prefix route if | |
3234 | * this was manually added from above. | |
3235 | */ | |
3236 | #if (DEVELOPMENT || DEBUG) | |
3237 | if (rt_verbose) { | |
3238 | log(LOG_DEBUG, "%s: not removing " | |
3239 | "static route to %s->%s->%s, " | |
3240 | "flags %b, ifaddr %s\n", __func__, | |
3241 | dbuf, gbuf, ((rt->rt_ifp != NULL) ? | |
3242 | rt->rt_ifp->if_xname : ""), | |
3243 | rt->rt_flags, RTF_BITS, abuf); | |
3244 | } | |
3245 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3246 | RT_REMREF_LOCKED(rt); | |
3247 | RT_UNLOCK(rt); | |
3248 | rt = NULL; | |
3249 | error = EBUSY; | |
3250 | goto done; | |
3251 | } | |
3252 | #if (DEVELOPMENT || DEBUG) | |
3253 | if (rt_verbose) { | |
3254 | log(LOG_DEBUG, "%s: removing route to " | |
3255 | "%s->%s->%s, flags %b, ifaddr %s\n", | |
3256 | __func__, dbuf, gbuf, | |
3257 | ((rt->rt_ifp != NULL) ? | |
3258 | rt->rt_ifp->if_xname : ""), | |
3259 | rt->rt_flags, RTF_BITS, abuf); | |
3260 | } | |
3261 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3262 | RT_REMREF_LOCKED(rt); | |
3263 | RT_UNLOCK(rt); | |
3264 | rt = NULL; | |
3265 | } | |
3266 | } | |
3267 | /* | |
3268 | * Do the actual request | |
3269 | */ | |
3270 | if ((error = rtrequest_locked(cmd, dst, ifa->ifa_addr, netmask, | |
3271 | flags | ifa->ifa_flags, &rt)) != 0) | |
3272 | goto done; | |
3273 | ||
3274 | VERIFY(rt != NULL); | |
3275 | #if (DEVELOPMENT || DEBUG) | |
3276 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
3277 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3278 | switch (cmd) { | |
3279 | case RTM_DELETE: | |
3280 | /* | |
3281 | * If we are deleting, and we found an entry, then it's | |
3282 | * been removed from the tree. Notify any listening | |
3283 | * routing agents of the change and throw it away. | |
3284 | */ | |
3285 | RT_LOCK(rt); | |
3286 | rt_newaddrmsg(cmd, ifa, error, rt); | |
3287 | RT_UNLOCK(rt); | |
3288 | #if (DEVELOPMENT || DEBUG) | |
3289 | if (rt_verbose) { | |
3290 | log(LOG_DEBUG, "%s: removed route to %s->%s->%s, " | |
3291 | "flags %b, ifaddr %s\n", __func__, dbuf, gbuf, | |
3292 | ((rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : ""), | |
3293 | rt->rt_flags, RTF_BITS, abuf); | |
3294 | } | |
3295 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3296 | rtfree_locked(rt); | |
3297 | break; | |
3298 | ||
3299 | case RTM_ADD: | |
3300 | /* | |
3301 | * We are adding, and we have a returned routing entry. | |
3302 | * We need to sanity check the result. If it came back | |
3303 | * with an unexpected interface, then it must have already | |
3304 | * existed or something. | |
3305 | */ | |
3306 | RT_LOCK(rt); | |
3307 | if (rt->rt_ifa != ifa) { | |
3308 | void (*ifa_rtrequest) | |
3309 | (int, struct rtentry *, struct sockaddr *); | |
3310 | #if (DEVELOPMENT || DEBUG) | |
3311 | if (rt_verbose) { | |
3312 | if (!(rt->rt_ifa->ifa_ifp->if_flags & | |
3313 | (IFF_POINTOPOINT|IFF_LOOPBACK))) { | |
3314 | log(LOG_ERR, "%s: %s route to %s->%s->%s, " | |
3315 | "flags %b, ifaddr %s, rt_ifa 0x%llx != " | |
3316 | "ifa 0x%llx\n", __func__, rtm2str(cmd), | |
3317 | dbuf, gbuf, ((rt->rt_ifp != NULL) ? | |
3318 | rt->rt_ifp->if_xname : ""), rt->rt_flags, | |
3319 | RTF_BITS, abuf, | |
3320 | (uint64_t)VM_KERNEL_ADDRPERM(rt->rt_ifa), | |
3321 | (uint64_t)VM_KERNEL_ADDRPERM(ifa)); | |
3322 | } | |
3323 | ||
3324 | log(LOG_DEBUG, "%s: %s route to %s->%s->%s, " | |
3325 | "flags %b, ifaddr %s, rt_ifa was 0x%llx " | |
3326 | "now 0x%llx\n", __func__, rtm2str(cmd), | |
3327 | dbuf, gbuf, ((rt->rt_ifp != NULL) ? | |
3328 | rt->rt_ifp->if_xname : ""), rt->rt_flags, | |
3329 | RTF_BITS, abuf, | |
3330 | (uint64_t)VM_KERNEL_ADDRPERM(rt->rt_ifa), | |
3331 | (uint64_t)VM_KERNEL_ADDRPERM(ifa)); | |
3332 | } | |
3333 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3334 | ||
3335 | /* | |
3336 | * Ask that the protocol in question | |
3337 | * remove anything it has associated with | |
3338 | * this route and ifaddr. | |
3339 | */ | |
3340 | ifa_rtrequest = rt->rt_ifa->ifa_rtrequest; | |
3341 | if (ifa_rtrequest != NULL) | |
3342 | ifa_rtrequest(RTM_DELETE, rt, NULL); | |
3343 | /* | |
3344 | * Set the route's ifa. | |
3345 | */ | |
3346 | rtsetifa(rt, ifa); | |
3347 | ||
3348 | if (rt->rt_ifp != ifa->ifa_ifp) { | |
3349 | /* | |
3350 | * Purge any link-layer info caching. | |
3351 | */ | |
3352 | if (rt->rt_llinfo_purge != NULL) | |
3353 | rt->rt_llinfo_purge(rt); | |
3354 | /* | |
3355 | * Adjust route ref count for the interfaces. | |
3356 | */ | |
3357 | if (rt->rt_if_ref_fn != NULL) { | |
3358 | rt->rt_if_ref_fn(ifa->ifa_ifp, 1); | |
3359 | rt->rt_if_ref_fn(rt->rt_ifp, -1); | |
3360 | } | |
3361 | } | |
3362 | ||
3363 | /* | |
3364 | * And substitute in references to the ifaddr | |
3365 | * we are adding. | |
3366 | */ | |
3367 | rt->rt_ifp = ifa->ifa_ifp; | |
3368 | /* | |
3369 | * If rmx_mtu is not locked, update it | |
3370 | * to the MTU used by the new interface. | |
3371 | */ | |
3372 | if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) | |
3373 | rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; | |
3374 | ||
3375 | /* | |
3376 | * Now ask the protocol to check if it needs | |
3377 | * any special processing in its new form. | |
3378 | */ | |
3379 | ifa_rtrequest = ifa->ifa_rtrequest; | |
3380 | if (ifa_rtrequest != NULL) | |
3381 | ifa_rtrequest(RTM_ADD, rt, NULL); | |
3382 | } else { | |
3383 | #if (DEVELOPMENT || DEBUG) | |
3384 | if (rt_verbose) { | |
3385 | log(LOG_DEBUG, "%s: added route to %s->%s->%s, " | |
3386 | "flags %b, ifaddr %s\n", __func__, dbuf, | |
3387 | gbuf, ((rt->rt_ifp != NULL) ? | |
3388 | rt->rt_ifp->if_xname : ""), rt->rt_flags, | |
3389 | RTF_BITS, abuf); | |
3390 | } | |
3391 | #endif /* (DEVELOPMENT || DEBUG) */ | |
3392 | } | |
3393 | /* | |
3394 | * notify any listenning routing agents of the change | |
3395 | */ | |
3396 | rt_newaddrmsg(cmd, ifa, error, rt); | |
3397 | /* | |
3398 | * We just wanted to add it; we don't actually need a | |
3399 | * reference. This will result in a route that's added | |
3400 | * to the routing table without a reference count. The | |
3401 | * RTM_DELETE code will do the necessary step to adjust | |
3402 | * the reference count at deletion time. | |
3403 | */ | |
3404 | RT_REMREF_LOCKED(rt); | |
3405 | RT_UNLOCK(rt); | |
3406 | break; | |
3407 | ||
3408 | default: | |
3409 | VERIFY(0); | |
3410 | /* NOTREACHED */ | |
3411 | } | |
3412 | done: | |
3413 | return (error); | |
3414 | } | |
3415 | ||
3416 | static void | |
3417 | rt_set_idleref(struct rtentry *rt) | |
3418 | { | |
3419 | RT_LOCK_ASSERT_HELD(rt); | |
3420 | ||
3421 | /* | |
3422 | * We currently keep idle refcnt only on unicast cloned routes | |
3423 | * that aren't marked with RTF_NOIFREF. | |
3424 | */ | |
3425 | if (rt->rt_parent != NULL && !(rt->rt_flags & | |
3426 | (RTF_NOIFREF|RTF_BROADCAST | RTF_MULTICAST)) && | |
3427 | (rt->rt_flags & (RTF_UP|RTF_WASCLONED|RTF_IFREF)) == | |
3428 | (RTF_UP|RTF_WASCLONED)) { | |
3429 | rt_clear_idleref(rt); /* drop existing refcnt if any */ | |
3430 | rt->rt_if_ref_fn = rte_if_ref; | |
3431 | /* Become a regular mutex, just in case */ | |
3432 | RT_CONVERT_LOCK(rt); | |
3433 | rt->rt_if_ref_fn(rt->rt_ifp, 1); | |
3434 | rt->rt_flags |= RTF_IFREF; | |
3435 | } | |
3436 | } | |
3437 | ||
3438 | void | |
3439 | rt_clear_idleref(struct rtentry *rt) | |
3440 | { | |
3441 | RT_LOCK_ASSERT_HELD(rt); | |
3442 | ||
3443 | if (rt->rt_if_ref_fn != NULL) { | |
3444 | VERIFY((rt->rt_flags & (RTF_NOIFREF | RTF_IFREF)) == RTF_IFREF); | |
3445 | /* Become a regular mutex, just in case */ | |
3446 | RT_CONVERT_LOCK(rt); | |
3447 | rt->rt_if_ref_fn(rt->rt_ifp, -1); | |
3448 | rt->rt_flags &= ~RTF_IFREF; | |
3449 | rt->rt_if_ref_fn = NULL; | |
3450 | } | |
3451 | } | |
3452 | ||
3453 | void | |
3454 | rt_set_proxy(struct rtentry *rt, boolean_t set) | |
3455 | { | |
3456 | lck_mtx_lock(rnh_lock); | |
3457 | RT_LOCK(rt); | |
3458 | /* | |
3459 | * Search for any cloned routes which might have | |
3460 | * been formed from this node, and delete them. | |
3461 | */ | |
3462 | if (rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) { | |
3463 | struct radix_node_head *rnh = rt_tables[rt_key(rt)->sa_family]; | |
3464 | ||
3465 | if (set) | |
3466 | rt->rt_flags |= RTF_PROXY; | |
3467 | else | |
3468 | rt->rt_flags &= ~RTF_PROXY; | |
3469 | ||
3470 | RT_UNLOCK(rt); | |
3471 | if (rnh != NULL && rt_mask(rt)) { | |
3472 | rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), | |
3473 | rt_fixdelete, rt); | |
3474 | } | |
3475 | } else { | |
3476 | RT_UNLOCK(rt); | |
3477 | } | |
3478 | lck_mtx_unlock(rnh_lock); | |
3479 | } | |
3480 | ||
3481 | static void | |
3482 | rte_lock_init(struct rtentry *rt) | |
3483 | { | |
3484 | lck_mtx_init(&rt->rt_lock, rte_mtx_grp, rte_mtx_attr); | |
3485 | } | |
3486 | ||
3487 | static void | |
3488 | rte_lock_destroy(struct rtentry *rt) | |
3489 | { | |
3490 | RT_LOCK_ASSERT_NOTHELD(rt); | |
3491 | lck_mtx_destroy(&rt->rt_lock, rte_mtx_grp); | |
3492 | } | |
3493 | ||
3494 | void | |
3495 | rt_lock(struct rtentry *rt, boolean_t spin) | |
3496 | { | |
3497 | RT_LOCK_ASSERT_NOTHELD(rt); | |
3498 | if (spin) | |
3499 | lck_mtx_lock_spin(&rt->rt_lock); | |
3500 | else | |
3501 | lck_mtx_lock(&rt->rt_lock); | |
3502 | if (rte_debug & RTD_DEBUG) | |
3503 | rte_lock_debug((struct rtentry_dbg *)rt); | |
3504 | } | |
3505 | ||
3506 | void | |
3507 | rt_unlock(struct rtentry *rt) | |
3508 | { | |
3509 | if (rte_debug & RTD_DEBUG) | |
3510 | rte_unlock_debug((struct rtentry_dbg *)rt); | |
3511 | lck_mtx_unlock(&rt->rt_lock); | |
3512 | ||
3513 | } | |
3514 | ||
3515 | static inline void | |
3516 | rte_lock_debug(struct rtentry_dbg *rte) | |
3517 | { | |
3518 | uint32_t idx; | |
3519 | ||
3520 | RT_LOCK_ASSERT_HELD((struct rtentry *)rte); | |
3521 | idx = atomic_add_32_ov(&rte->rtd_lock_cnt, 1) % CTRACE_HIST_SIZE; | |
3522 | if (rte_debug & RTD_TRACE) | |
3523 | ctrace_record(&rte->rtd_lock[idx]); | |
3524 | } | |
3525 | ||
3526 | static inline void | |
3527 | rte_unlock_debug(struct rtentry_dbg *rte) | |
3528 | { | |
3529 | uint32_t idx; | |
3530 | ||
3531 | RT_LOCK_ASSERT_HELD((struct rtentry *)rte); | |
3532 | idx = atomic_add_32_ov(&rte->rtd_unlock_cnt, 1) % CTRACE_HIST_SIZE; | |
3533 | if (rte_debug & RTD_TRACE) | |
3534 | ctrace_record(&rte->rtd_unlock[idx]); | |
3535 | } | |
3536 | ||
3537 | static struct rtentry * | |
3538 | rte_alloc(void) | |
3539 | { | |
3540 | if (rte_debug & RTD_DEBUG) | |
3541 | return (rte_alloc_debug()); | |
3542 | ||
3543 | return ((struct rtentry *)zalloc(rte_zone)); | |
3544 | } | |
3545 | ||
3546 | static void | |
3547 | rte_free(struct rtentry *p) | |
3548 | { | |
3549 | if (rte_debug & RTD_DEBUG) { | |
3550 | rte_free_debug(p); | |
3551 | return; | |
3552 | } | |
3553 | ||
3554 | if (p->rt_refcnt != 0) { | |
3555 | panic("rte_free: rte=%p refcnt=%d non-zero\n", p, p->rt_refcnt); | |
3556 | /* NOTREACHED */ | |
3557 | } | |
3558 | ||
3559 | zfree(rte_zone, p); | |
3560 | } | |
3561 | ||
3562 | static void | |
3563 | rte_if_ref(struct ifnet *ifp, int cnt) | |
3564 | { | |
3565 | struct kev_msg ev_msg; | |
3566 | struct net_event_data ev_data; | |
3567 | uint32_t old; | |
3568 | ||
3569 | /* Force cnt to 1 increment/decrement */ | |
3570 | if (cnt < -1 || cnt > 1) { | |
3571 | panic("%s: invalid count argument (%d)", __func__, cnt); | |
3572 | /* NOTREACHED */ | |
3573 | } | |
3574 | old = atomic_add_32_ov(&ifp->if_route_refcnt, cnt); | |
3575 | if (cnt < 0 && old == 0) { | |
3576 | panic("%s: ifp=%p negative route refcnt!", __func__, ifp); | |
3577 | /* NOTREACHED */ | |
3578 | } | |
3579 | /* | |
3580 | * The following is done without first holding the ifnet lock, | |
3581 | * for performance reasons. The relevant ifnet fields, with | |
3582 | * the exception of the if_idle_flags, are never changed | |
3583 | * during the lifetime of the ifnet. The if_idle_flags | |
3584 | * may possibly be modified, so in the event that the value | |
3585 | * is stale because IFRF_IDLE_NOTIFY was cleared, we'd end up | |
3586 | * sending the event anyway. This is harmless as it is just | |
3587 | * a notification to the monitoring agent in user space, and | |
3588 | * it is expected to check via SIOCGIFGETRTREFCNT again anyway. | |
3589 | */ | |
3590 | if ((ifp->if_idle_flags & IFRF_IDLE_NOTIFY) && cnt < 0 && old == 1) { | |
3591 | bzero(&ev_msg, sizeof (ev_msg)); | |
3592 | bzero(&ev_data, sizeof (ev_data)); | |
3593 | ||
3594 | ev_msg.vendor_code = KEV_VENDOR_APPLE; | |
3595 | ev_msg.kev_class = KEV_NETWORK_CLASS; | |
3596 | ev_msg.kev_subclass = KEV_DL_SUBCLASS; | |
3597 | ev_msg.event_code = KEV_DL_IF_IDLE_ROUTE_REFCNT; | |
3598 | ||
3599 | strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ); | |
3600 | ||
3601 | ev_data.if_family = ifp->if_family; | |
3602 | ev_data.if_unit = ifp->if_unit; | |
3603 | ev_msg.dv[0].data_length = sizeof (struct net_event_data); | |
3604 | ev_msg.dv[0].data_ptr = &ev_data; | |
3605 | ||
3606 | dlil_post_complete_msg(NULL, &ev_msg); | |
3607 | } | |
3608 | } | |
3609 | ||
3610 | static inline struct rtentry * | |
3611 | rte_alloc_debug(void) | |
3612 | { | |
3613 | struct rtentry_dbg *rte; | |
3614 | ||
3615 | rte = ((struct rtentry_dbg *)zalloc(rte_zone)); | |
3616 | if (rte != NULL) { | |
3617 | bzero(rte, sizeof (*rte)); | |
3618 | if (rte_debug & RTD_TRACE) | |
3619 | ctrace_record(&rte->rtd_alloc); | |
3620 | rte->rtd_inuse = RTD_INUSE; | |
3621 | } | |
3622 | return ((struct rtentry *)rte); | |
3623 | } | |
3624 | ||
3625 | static inline void | |
3626 | rte_free_debug(struct rtentry *p) | |
3627 | { | |
3628 | struct rtentry_dbg *rte = (struct rtentry_dbg *)p; | |
3629 | ||
3630 | if (p->rt_refcnt != 0) { | |
3631 | panic("rte_free: rte=%p refcnt=%d\n", p, p->rt_refcnt); | |
3632 | /* NOTREACHED */ | |
3633 | } | |
3634 | if (rte->rtd_inuse == RTD_FREED) { | |
3635 | panic("rte_free: double free rte=%p\n", rte); | |
3636 | /* NOTREACHED */ | |
3637 | } else if (rte->rtd_inuse != RTD_INUSE) { | |
3638 | panic("rte_free: corrupted rte=%p\n", rte); | |
3639 | /* NOTREACHED */ | |
3640 | } | |
3641 | bcopy((caddr_t)p, (caddr_t)&rte->rtd_entry_saved, sizeof (*p)); | |
3642 | /* Preserve rt_lock to help catch use-after-free cases */ | |
3643 | bzero((caddr_t)p, offsetof(struct rtentry, rt_lock)); | |
3644 | ||
3645 | rte->rtd_inuse = RTD_FREED; | |
3646 | ||
3647 | if (rte_debug & RTD_TRACE) | |
3648 | ctrace_record(&rte->rtd_free); | |
3649 | ||
3650 | if (!(rte_debug & RTD_NO_FREE)) | |
3651 | zfree(rte_zone, p); | |
3652 | } | |
3653 | ||
3654 | void | |
3655 | ctrace_record(ctrace_t *tr) | |
3656 | { | |
3657 | tr->th = current_thread(); | |
3658 | bzero(tr->pc, sizeof (tr->pc)); | |
3659 | (void) OSBacktrace(tr->pc, CTRACE_STACK_SIZE); | |
3660 | } | |
3661 | ||
3662 | void | |
3663 | route_copyout(struct route *dst, const struct route *src, size_t length) | |
3664 | { | |
3665 | /* Copy everything (rt, srcif, flags, dst) from src */ | |
3666 | bcopy(src, dst, length); | |
3667 | ||
3668 | /* Hold one reference for the local copy of struct route */ | |
3669 | if (dst->ro_rt != NULL) | |
3670 | RT_ADDREF(dst->ro_rt); | |
3671 | ||
3672 | /* Hold one reference for the local copy of struct ifaddr */ | |
3673 | if (dst->ro_srcia != NULL) | |
3674 | IFA_ADDREF(dst->ro_srcia); | |
3675 | } | |
3676 | ||
3677 | void | |
3678 | route_copyin(struct route *src, struct route *dst, size_t length) | |
3679 | { | |
3680 | /* No cached route at the destination? */ | |
3681 | if (dst->ro_rt == NULL) { | |
3682 | /* | |
3683 | * Ditch the address in the cached copy (dst) since | |
3684 | * we're about to take everything there is in src. | |
3685 | */ | |
3686 | if (dst->ro_srcia != NULL) | |
3687 | IFA_REMREF(dst->ro_srcia); | |
3688 | /* | |
3689 | * Copy everything (rt, srcia, flags, dst) from src; the | |
3690 | * references to rt and/or srcia were held at the time | |
3691 | * of storage and are kept intact. | |
3692 | */ | |
3693 | bcopy(src, dst, length); | |
3694 | } else if (src->ro_rt != NULL) { | |
3695 | /* | |
3696 | * If the same, update srcia and flags, and ditch the route | |
3697 | * in the local copy. Else ditch the one that is currently | |
3698 | * cached, and cache the new route. | |
3699 | */ | |
3700 | if (dst->ro_rt == src->ro_rt) { | |
3701 | dst->ro_flags = src->ro_flags; | |
3702 | if (dst->ro_srcia != src->ro_srcia) { | |
3703 | if (dst->ro_srcia != NULL) | |
3704 | IFA_REMREF(dst->ro_srcia); | |
3705 | dst->ro_srcia = src->ro_srcia; | |
3706 | } else if (src->ro_srcia != NULL) { | |
3707 | IFA_REMREF(src->ro_srcia); | |
3708 | } | |
3709 | rtfree(src->ro_rt); | |
3710 | } else { | |
3711 | rtfree(dst->ro_rt); | |
3712 | if (dst->ro_srcia != NULL) | |
3713 | IFA_REMREF(dst->ro_srcia); | |
3714 | bcopy(src, dst, length); | |
3715 | } | |
3716 | } else if (src->ro_srcia != NULL) { | |
3717 | /* | |
3718 | * Ditch src address in the local copy (src) since we're | |
3719 | * not caching the route entry anyway (ro_rt is NULL). | |
3720 | */ | |
3721 | IFA_REMREF(src->ro_srcia); | |
3722 | } | |
3723 | ||
3724 | /* This function consumes the references on src */ | |
3725 | src->ro_rt = NULL; | |
3726 | src->ro_srcia = NULL; | |
3727 | } | |
3728 | ||
3729 | /* | |
3730 | * route_to_gwroute will find the gateway route for a given route. | |
3731 | * | |
3732 | * If the route is down, look the route up again. | |
3733 | * If the route goes through a gateway, get the route to the gateway. | |
3734 | * If the gateway route is down, look it up again. | |
3735 | * If the route is set to reject, verify it hasn't expired. | |
3736 | * | |
3737 | * If the returned route is non-NULL, the caller is responsible for | |
3738 | * releasing the reference and unlocking the route. | |
3739 | */ | |
3740 | #define senderr(e) { error = (e); goto bad; } | |
3741 | errno_t | |
3742 | route_to_gwroute(const struct sockaddr *net_dest, struct rtentry *hint0, | |
3743 | struct rtentry **out_route) | |
3744 | { | |
3745 | uint64_t timenow; | |
3746 | struct rtentry *rt = hint0, *hint = hint0; | |
3747 | errno_t error = 0; | |
3748 | unsigned int ifindex; | |
3749 | boolean_t gwroute; | |
3750 | ||
3751 | *out_route = NULL; | |
3752 | ||
3753 | if (rt == NULL) | |
3754 | return (0); | |
3755 | ||
3756 | /* | |
3757 | * Next hop determination. Because we may involve the gateway route | |
3758 | * in addition to the original route, locking is rather complicated. | |
3759 | * The general concept is that regardless of whether the route points | |
3760 | * to the original route or to the gateway route, this routine takes | |
3761 | * an extra reference on such a route. This extra reference will be | |
3762 | * released at the end. | |
3763 | * | |
3764 | * Care must be taken to ensure that the "hint0" route never gets freed | |
3765 | * via rtfree(), since the caller may have stored it inside a struct | |
3766 | * route with a reference held for that placeholder. | |
3767 | */ | |
3768 | RT_LOCK_SPIN(rt); | |
3769 | ifindex = rt->rt_ifp->if_index; | |
3770 | RT_ADDREF_LOCKED(rt); | |
3771 | if (!(rt->rt_flags & RTF_UP)) { | |
3772 | RT_REMREF_LOCKED(rt); | |
3773 | RT_UNLOCK(rt); | |
3774 | /* route is down, find a new one */ | |
3775 | hint = rt = rtalloc1_scoped((struct sockaddr *) | |
3776 | (size_t)net_dest, 1, 0, ifindex); | |
3777 | if (hint != NULL) { | |
3778 | RT_LOCK_SPIN(rt); | |
3779 | ifindex = rt->rt_ifp->if_index; | |
3780 | } else { | |
3781 | senderr(EHOSTUNREACH); | |
3782 | } | |
3783 | } | |
3784 | ||
3785 | /* | |
3786 | * We have a reference to "rt" by now; it will either | |
3787 | * be released or freed at the end of this routine. | |
3788 | */ | |
3789 | RT_LOCK_ASSERT_HELD(rt); | |
3790 | if ((gwroute = (rt->rt_flags & RTF_GATEWAY))) { | |
3791 | struct rtentry *gwrt = rt->rt_gwroute; | |
3792 | struct sockaddr_storage ss; | |
3793 | struct sockaddr *gw = (struct sockaddr *)&ss; | |
3794 | ||
3795 | VERIFY(rt == hint); | |
3796 | RT_ADDREF_LOCKED(hint); | |
3797 | ||
3798 | /* If there's no gateway rt, look it up */ | |
3799 | if (gwrt == NULL) { | |
3800 | bcopy(rt->rt_gateway, gw, MIN(sizeof (ss), | |
3801 | rt->rt_gateway->sa_len)); | |
3802 | RT_UNLOCK(rt); | |
3803 | goto lookup; | |
3804 | } | |
3805 | /* Become a regular mutex */ | |
3806 | RT_CONVERT_LOCK(rt); | |
3807 | ||
3808 | /* | |
3809 | * Take gwrt's lock while holding route's lock; | |
3810 | * this is okay since gwrt never points back | |
3811 | * to "rt", so no lock ordering issues. | |
3812 | */ | |
3813 | RT_LOCK_SPIN(gwrt); | |
3814 | if (!(gwrt->rt_flags & RTF_UP)) { | |
3815 | rt->rt_gwroute = NULL; | |
3816 | RT_UNLOCK(gwrt); | |
3817 | bcopy(rt->rt_gateway, gw, MIN(sizeof (ss), | |
3818 | rt->rt_gateway->sa_len)); | |
3819 | RT_UNLOCK(rt); | |
3820 | rtfree(gwrt); | |
3821 | lookup: | |
3822 | lck_mtx_lock(rnh_lock); | |
3823 | gwrt = rtalloc1_scoped_locked(gw, 1, 0, ifindex); | |
3824 | ||
3825 | RT_LOCK(rt); | |
3826 | /* | |
3827 | * Bail out if the route is down, no route | |
3828 | * to gateway, circular route, or if the | |
3829 | * gateway portion of "rt" has changed. | |
3830 | */ | |
3831 | if (!(rt->rt_flags & RTF_UP) || gwrt == NULL || | |
3832 | gwrt == rt || !equal(gw, rt->rt_gateway)) { | |
3833 | if (gwrt == rt) { | |
3834 | RT_REMREF_LOCKED(gwrt); | |
3835 | gwrt = NULL; | |
3836 | } | |
3837 | VERIFY(rt == hint); | |
3838 | RT_REMREF_LOCKED(hint); | |
3839 | hint = NULL; | |
3840 | RT_UNLOCK(rt); | |
3841 | if (gwrt != NULL) | |
3842 | rtfree_locked(gwrt); | |
3843 | lck_mtx_unlock(rnh_lock); | |
3844 | senderr(EHOSTUNREACH); | |
3845 | } | |
3846 | VERIFY(gwrt != NULL); | |
3847 | /* | |
3848 | * Set gateway route; callee adds ref to gwrt; | |
3849 | * gwrt has an extra ref from rtalloc1() for | |
3850 | * this routine. | |
3851 | */ | |
3852 | rt_set_gwroute(rt, rt_key(rt), gwrt); | |
3853 | VERIFY(rt == hint); | |
3854 | RT_REMREF_LOCKED(rt); /* hint still holds a refcnt */ | |
3855 | RT_UNLOCK(rt); | |
3856 | lck_mtx_unlock(rnh_lock); | |
3857 | rt = gwrt; | |
3858 | } else { | |
3859 | RT_ADDREF_LOCKED(gwrt); | |
3860 | RT_UNLOCK(gwrt); | |
3861 | VERIFY(rt == hint); | |
3862 | RT_REMREF_LOCKED(rt); /* hint still holds a refcnt */ | |
3863 | RT_UNLOCK(rt); | |
3864 | rt = gwrt; | |
3865 | } | |
3866 | VERIFY(rt == gwrt && rt != hint); | |
3867 | ||
3868 | /* | |
3869 | * This is an opportunity to revalidate the parent route's | |
3870 | * rt_gwroute, in case it now points to a dead route entry. | |
3871 | * Parent route won't go away since the clone (hint) holds | |
3872 | * a reference to it. rt == gwrt. | |
3873 | */ | |
3874 | RT_LOCK_SPIN(hint); | |
3875 | if ((hint->rt_flags & (RTF_WASCLONED | RTF_UP)) == | |
3876 | (RTF_WASCLONED | RTF_UP)) { | |
3877 | struct rtentry *prt = hint->rt_parent; | |
3878 | VERIFY(prt != NULL); | |
3879 | ||
3880 | RT_CONVERT_LOCK(hint); | |
3881 | RT_ADDREF(prt); | |
3882 | RT_UNLOCK(hint); | |
3883 | rt_revalidate_gwroute(prt, rt); | |
3884 | RT_REMREF(prt); | |
3885 | } else { | |
3886 | RT_UNLOCK(hint); | |
3887 | } | |
3888 | ||
3889 | /* Clean up "hint" now; see notes above regarding hint0 */ | |
3890 | if (hint == hint0) | |
3891 | RT_REMREF(hint); | |
3892 | else | |
3893 | rtfree(hint); | |
3894 | hint = NULL; | |
3895 | ||
3896 | /* rt == gwrt; if it is now down, give up */ | |
3897 | RT_LOCK_SPIN(rt); | |
3898 | if (!(rt->rt_flags & RTF_UP)) { | |
3899 | RT_UNLOCK(rt); | |
3900 | senderr(EHOSTUNREACH); | |
3901 | } | |
3902 | } | |
3903 | ||
3904 | if (rt->rt_flags & RTF_REJECT) { | |
3905 | VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0); | |
3906 | VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0); | |
3907 | timenow = net_uptime(); | |
3908 | if (rt->rt_expire == 0 || timenow < rt->rt_expire) { | |
3909 | RT_UNLOCK(rt); | |
3910 | senderr(!gwroute ? EHOSTDOWN : EHOSTUNREACH); | |
3911 | } | |
3912 | } | |
3913 | ||
3914 | /* Become a regular mutex */ | |
3915 | RT_CONVERT_LOCK(rt); | |
3916 | ||
3917 | /* Caller is responsible for cleaning up "rt" */ | |
3918 | *out_route = rt; | |
3919 | return (0); | |
3920 | ||
3921 | bad: | |
3922 | /* Clean up route (either it is "rt" or "gwrt") */ | |
3923 | if (rt != NULL) { | |
3924 | RT_LOCK_SPIN(rt); | |
3925 | if (rt == hint0) { | |
3926 | RT_REMREF_LOCKED(rt); | |
3927 | RT_UNLOCK(rt); | |
3928 | } else { | |
3929 | RT_UNLOCK(rt); | |
3930 | rtfree(rt); | |
3931 | } | |
3932 | } | |
3933 | return (error); | |
3934 | } | |
3935 | #undef senderr | |
3936 | ||
3937 | void | |
3938 | rt_revalidate_gwroute(struct rtentry *rt, struct rtentry *gwrt) | |
3939 | { | |
3940 | VERIFY(gwrt != NULL); | |
3941 | ||
3942 | RT_LOCK_SPIN(rt); | |
3943 | if ((rt->rt_flags & (RTF_GATEWAY | RTF_UP)) == (RTF_GATEWAY | RTF_UP) && | |
3944 | rt->rt_ifp == gwrt->rt_ifp && rt->rt_gateway->sa_family == | |
3945 | rt_key(gwrt)->sa_family && (rt->rt_gwroute == NULL || | |
3946 | !(rt->rt_gwroute->rt_flags & RTF_UP))) { | |
3947 | boolean_t isequal; | |
3948 | VERIFY(rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)); | |
3949 | ||
3950 | if (rt->rt_gateway->sa_family == AF_INET || | |
3951 | rt->rt_gateway->sa_family == AF_INET6) { | |
3952 | struct sockaddr_storage key_ss, gw_ss; | |
3953 | /* | |
3954 | * We need to compare rt_key and rt_gateway; create | |
3955 | * local copies to get rid of any ifscope association. | |
3956 | */ | |
3957 | (void) sa_copy(rt_key(gwrt), &key_ss, NULL); | |
3958 | (void) sa_copy(rt->rt_gateway, &gw_ss, NULL); | |
3959 | ||
3960 | isequal = equal(SA(&key_ss), SA(&gw_ss)); | |
3961 | } else { | |
3962 | isequal = equal(rt_key(gwrt), rt->rt_gateway); | |
3963 | } | |
3964 | ||
3965 | /* If they are the same, update gwrt */ | |
3966 | if (isequal) { | |
3967 | RT_UNLOCK(rt); | |
3968 | lck_mtx_lock(rnh_lock); | |
3969 | RT_LOCK(rt); | |
3970 | rt_set_gwroute(rt, rt_key(rt), gwrt); | |
3971 | RT_UNLOCK(rt); | |
3972 | lck_mtx_unlock(rnh_lock); | |
3973 | } else { | |
3974 | RT_UNLOCK(rt); | |
3975 | } | |
3976 | } else { | |
3977 | RT_UNLOCK(rt); | |
3978 | } | |
3979 | } | |
3980 | ||
3981 | static void | |
3982 | rt_str4(struct rtentry *rt, char *ds, uint32_t dslen, char *gs, uint32_t gslen) | |
3983 | { | |
3984 | VERIFY(rt_key(rt)->sa_family == AF_INET); | |
3985 | ||
3986 | if (ds != NULL) { | |
3987 | (void) inet_ntop(AF_INET, | |
3988 | &SIN(rt_key(rt))->sin_addr.s_addr, ds, dslen); | |
3989 | if (dslen >= MAX_SCOPE_ADDR_STR_LEN && | |
3990 | SINIFSCOPE(rt_key(rt))->sin_scope_id != IFSCOPE_NONE) { | |
3991 | char scpstr[16]; | |
3992 | ||
3993 | snprintf(scpstr, sizeof(scpstr), "@%u", | |
3994 | SINIFSCOPE(rt_key(rt))->sin_scope_id); | |
3995 | ||
3996 | strlcat(ds, scpstr, dslen); | |
3997 | } | |
3998 | } | |
3999 | ||
4000 | if (gs != NULL) { | |
4001 | if (rt->rt_flags & RTF_GATEWAY) { | |
4002 | (void) inet_ntop(AF_INET, | |
4003 | &SIN(rt->rt_gateway)->sin_addr.s_addr, gs, gslen); | |
4004 | } else if (rt->rt_ifp != NULL) { | |
4005 | snprintf(gs, gslen, "link#%u", rt->rt_ifp->if_unit); | |
4006 | } else { | |
4007 | snprintf(gs, gslen, "%s", "link"); | |
4008 | } | |
4009 | } | |
4010 | } | |
4011 | ||
4012 | #if INET6 | |
4013 | static void | |
4014 | rt_str6(struct rtentry *rt, char *ds, uint32_t dslen, char *gs, uint32_t gslen) | |
4015 | { | |
4016 | VERIFY(rt_key(rt)->sa_family == AF_INET6); | |
4017 | ||
4018 | if (ds != NULL) { | |
4019 | (void) inet_ntop(AF_INET6, | |
4020 | &SIN6(rt_key(rt))->sin6_addr, ds, dslen); | |
4021 | if (dslen >= MAX_SCOPE_ADDR_STR_LEN && | |
4022 | SIN6IFSCOPE(rt_key(rt))->sin6_scope_id != IFSCOPE_NONE) { | |
4023 | char scpstr[16]; | |
4024 | ||
4025 | snprintf(scpstr, sizeof(scpstr), "@%u", | |
4026 | SIN6IFSCOPE(rt_key(rt))->sin6_scope_id); | |
4027 | ||
4028 | strlcat(ds, scpstr, dslen); | |
4029 | } | |
4030 | } | |
4031 | ||
4032 | if (gs != NULL) { | |
4033 | if (rt->rt_flags & RTF_GATEWAY) { | |
4034 | (void) inet_ntop(AF_INET6, | |
4035 | &SIN6(rt->rt_gateway)->sin6_addr, gs, gslen); | |
4036 | } else if (rt->rt_ifp != NULL) { | |
4037 | snprintf(gs, gslen, "link#%u", rt->rt_ifp->if_unit); | |
4038 | } else { | |
4039 | snprintf(gs, gslen, "%s", "link"); | |
4040 | } | |
4041 | } | |
4042 | } | |
4043 | #endif /* INET6 */ | |
4044 | ||
4045 | ||
4046 | void | |
4047 | rt_str(struct rtentry *rt, char *ds, uint32_t dslen, char *gs, uint32_t gslen) | |
4048 | { | |
4049 | switch (rt_key(rt)->sa_family) { | |
4050 | case AF_INET: | |
4051 | rt_str4(rt, ds, dslen, gs, gslen); | |
4052 | break; | |
4053 | #if INET6 | |
4054 | case AF_INET6: | |
4055 | rt_str6(rt, ds, dslen, gs, gslen); | |
4056 | break; | |
4057 | #endif /* INET6 */ | |
4058 | default: | |
4059 | if (ds != NULL) | |
4060 | bzero(ds, dslen); | |
4061 | if (gs != NULL) | |
4062 | bzero(gs, gslen); | |
4063 | break; | |
4064 | } | |
4065 | } |