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