2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1980, 1986, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
60 * @(#)route.c 8.2 (Berkeley) 11/15/93
61 * $FreeBSD: src/sys/net/route.c,v 1.59.2.3 2001/07/29 19:18:02 ume Exp $
64 #include <sys/param.h>
65 #include <sys/sysctl.h>
66 #include <sys/systm.h>
67 #include <sys/malloc.h>
69 #include <sys/socket.h>
70 #include <sys/domain.h>
73 #include <sys/vnode.h>
74 #include <sys/syslog.h>
75 #include <sys/queue.h>
76 #include <sys/mcache.h>
78 #include <sys/protosw.h>
79 #include <sys/kernel.h>
80 #include <kern/locks.h>
81 #include <kern/zalloc.h>
85 #include <net/route.h>
86 #include <net/ntstat.h>
87 #include <net/nwk_wq.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet/in_arp.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/in6_var.h>
101 #include <netinet6/nd6.h>
104 #include <net/if_dl.h>
106 #include <libkern/OSAtomic.h>
107 #include <libkern/OSDebug.h>
109 #include <pexpert/pexpert.h>
112 #include <sys/kauth.h>
116 * Synchronization notes:
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.
122 * The locking domains for routing are defined as follows:
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.)
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.
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.
139 * The fields of the rtentry structure are protected in the following way:
143 * - Routing table lock (rnh_lock).
145 * rt_parent, rt_mask, rt_llinfo_free, rt_tree_genid
147 * - Set once during creation and never changes; no locks to read.
149 * rt_flags, rt_genmask, rt_llinfo, rt_rmx, rt_refcnt, rt_gwroute
151 * - Routing entry lock (rt_lock) for read/write access.
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,
158 * RTF_BROADCAST, RTF_MULTICAST, RTF_IFSCOPE, RTF_IFREF.
160 * rt_key, rt_gateway, rt_ifp, rt_ifa
162 * - Always written/modified with both rnh_lock and rt_lock held.
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.
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.
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().
181 * - Assumes that 32-bit writes are atomic; no locks.
185 * - Currently unused; no locks.
187 * Operations on a route entry can be described as follows:
189 * CREATE an entry with reference count set to 0 as part of RTM_ADD/RESOLVE.
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).
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.
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.
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.
213 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
215 extern void kdp_set_gateway_mac(void *gatewaymac
);
217 __private_extern__
struct rtstat rtstat
= { 0, 0, 0, 0, 0, 0 };
218 struct radix_node_head
*rt_tables
[AF_MAX
+1];
220 decl_lck_mtx_data(, rnh_lock_data
); /* global routing tables mutex */
221 lck_mtx_t
*rnh_lock
= &rnh_lock_data
;
222 static lck_attr_t
*rnh_lock_attr
;
223 static lck_grp_t
*rnh_lock_grp
;
224 static lck_grp_attr_t
*rnh_lock_grp_attr
;
226 /* Lock group and attribute for routing entry locks */
227 static lck_attr_t
*rte_mtx_attr
;
228 static lck_grp_t
*rte_mtx_grp
;
229 static lck_grp_attr_t
*rte_mtx_grp_attr
;
231 int rttrash
= 0; /* routes not in table but not freed */
233 unsigned int rte_debug
= 0;
235 /* Possible flags for rte_debug */
236 #define RTD_DEBUG 0x1 /* enable or disable rtentry debug facility */
237 #define RTD_TRACE 0x2 /* trace alloc, free, refcnt and lock */
238 #define RTD_NO_FREE 0x4 /* don't free (good to catch corruptions) */
240 #define RTE_NAME "rtentry" /* name for zone and rt_lock */
242 static struct zone
*rte_zone
; /* special zone for rtentry */
243 #define RTE_ZONE_MAX 65536 /* maximum elements in zone */
244 #define RTE_ZONE_NAME RTE_NAME /* name of rtentry zone */
246 #define RTD_INUSE 0xFEEDFACE /* entry is in use */
247 #define RTD_FREED 0xDEADBEEF /* entry is freed */
249 #define MAX_SCOPE_ADDR_STR_LEN (MAX_IPv6_STR_LEN + 6)
252 __private_extern__
unsigned int ctrace_stack_size
= CTRACE_STACK_SIZE
;
253 __private_extern__
unsigned int ctrace_hist_size
= CTRACE_HIST_SIZE
;
256 * Debug variant of rtentry structure.
259 struct rtentry rtd_entry
; /* rtentry */
260 struct rtentry rtd_entry_saved
; /* saved rtentry */
261 uint32_t rtd_inuse
; /* in use pattern */
262 uint16_t rtd_refhold_cnt
; /* # of rtref */
263 uint16_t rtd_refrele_cnt
; /* # of rtunref */
264 uint32_t rtd_lock_cnt
; /* # of locks */
265 uint32_t rtd_unlock_cnt
; /* # of unlocks */
267 * Alloc and free callers.
272 * Circular lists of rtref and rtunref callers.
274 ctrace_t rtd_refhold
[CTRACE_HIST_SIZE
];
275 ctrace_t rtd_refrele
[CTRACE_HIST_SIZE
];
277 * Circular lists of locks and unlocks.
279 ctrace_t rtd_lock
[CTRACE_HIST_SIZE
];
280 ctrace_t rtd_unlock
[CTRACE_HIST_SIZE
];
284 TAILQ_ENTRY(rtentry_dbg
) rtd_trash_link
;
287 /* List of trash route entries protected by rnh_lock */
288 static TAILQ_HEAD(, rtentry_dbg
) rttrash_head
;
290 static void rte_lock_init(struct rtentry
*);
291 static void rte_lock_destroy(struct rtentry
*);
292 static inline struct rtentry
*rte_alloc_debug(void);
293 static inline void rte_free_debug(struct rtentry
*);
294 static inline void rte_lock_debug(struct rtentry_dbg
*);
295 static inline void rte_unlock_debug(struct rtentry_dbg
*);
296 static void rt_maskedcopy(const struct sockaddr
*,
297 struct sockaddr
*, const struct sockaddr
*);
298 static void rtable_init(void **);
299 static inline void rtref_audit(struct rtentry_dbg
*);
300 static inline void rtunref_audit(struct rtentry_dbg
*);
301 static struct rtentry
*rtalloc1_common_locked(struct sockaddr
*, int, uint32_t,
303 static int rtrequest_common_locked(int, struct sockaddr
*,
304 struct sockaddr
*, struct sockaddr
*, int, struct rtentry
**,
306 static struct rtentry
*rtalloc1_locked(struct sockaddr
*, int, uint32_t);
307 static void rtalloc_ign_common_locked(struct route
*, uint32_t, unsigned int);
308 static inline void sin6_set_ifscope(struct sockaddr
*, unsigned int);
309 static inline void sin6_set_embedded_ifscope(struct sockaddr
*, unsigned int);
310 static inline unsigned int sin6_get_embedded_ifscope(struct sockaddr
*);
311 static struct sockaddr
*ma_copy(int, struct sockaddr
*,
312 struct sockaddr_storage
*, unsigned int);
313 static struct sockaddr
*sa_trim(struct sockaddr
*, int);
314 static struct radix_node
*node_lookup(struct sockaddr
*, struct sockaddr
*,
316 static struct radix_node
*node_lookup_default(int);
317 static struct rtentry
*rt_lookup_common(boolean_t
, boolean_t
, struct sockaddr
*,
318 struct sockaddr
*, struct radix_node_head
*, unsigned int);
319 static int rn_match_ifscope(struct radix_node
*, void *);
320 static struct ifaddr
*ifa_ifwithroute_common_locked(int,
321 const struct sockaddr
*, const struct sockaddr
*, unsigned int);
322 static struct rtentry
*rte_alloc(void);
323 static void rte_free(struct rtentry
*);
324 static void rtfree_common(struct rtentry
*, boolean_t
);
325 static void rte_if_ref(struct ifnet
*, int);
326 static void rt_set_idleref(struct rtentry
*);
327 static void rt_clear_idleref(struct rtentry
*);
328 static void route_event_callback(void *);
329 static void rt_str4(struct rtentry
*, char *, uint32_t, char *, uint32_t);
331 static void rt_str6(struct rtentry
*, char *, uint32_t, char *, uint32_t);
334 uint32_t route_genid_inet
= 0;
336 uint32_t route_genid_inet6
= 0;
339 #define ASSERT_SINIFSCOPE(sa) { \
340 if ((sa)->sa_family != AF_INET || \
341 (sa)->sa_len < sizeof (struct sockaddr_in)) \
342 panic("%s: bad sockaddr_in %p\n", __func__, sa); \
345 #define ASSERT_SIN6IFSCOPE(sa) { \
346 if ((sa)->sa_family != AF_INET6 || \
347 (sa)->sa_len < sizeof (struct sockaddr_in6)) \
348 panic("%s: bad sockaddr_in6 %p\n", __func__, sa); \
352 * Argument to leaf-matching routine; at present it is scoped routing
353 * specific but can be expanded in future to include other search filters.
355 struct matchleaf_arg
{
356 unsigned int ifscope
; /* interface scope */
360 * For looking up the non-scoped default route (sockaddr instead
361 * of sockaddr_in for convenience).
363 static struct sockaddr sin_def
= {
364 sizeof (struct sockaddr_in
), AF_INET
, { 0, }
367 static struct sockaddr_in6 sin6_def
= {
368 sizeof (struct sockaddr_in6
), AF_INET6
, 0, 0, IN6ADDR_ANY_INIT
, 0
372 * Interface index (scope) of the primary interface; determined at
373 * the time when the default, non-scoped route gets added, changed
374 * or deleted. Protected by rnh_lock.
376 static unsigned int primary_ifscope
= IFSCOPE_NONE
;
377 static unsigned int primary6_ifscope
= IFSCOPE_NONE
;
379 #define INET_DEFAULT(sa) \
380 ((sa)->sa_family == AF_INET && SIN(sa)->sin_addr.s_addr == 0)
382 #define INET6_DEFAULT(sa) \
383 ((sa)->sa_family == AF_INET6 && \
384 IN6_IS_ADDR_UNSPECIFIED(&SIN6(sa)->sin6_addr))
386 #define SA_DEFAULT(sa) (INET_DEFAULT(sa) || INET6_DEFAULT(sa))
387 #define RT(r) ((struct rtentry *)r)
388 #define RN(r) ((struct radix_node *)r)
389 #define RT_HOST(r) (RT(r)->rt_flags & RTF_HOST)
391 unsigned int rt_verbose
= 0;
392 #if (DEVELOPMENT || DEBUG)
393 SYSCTL_DECL(_net_route
);
394 SYSCTL_UINT(_net_route
, OID_AUTO
, verbose
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
396 #endif /* (DEVELOPMENT || DEBUG) */
399 rtable_init(void **table
)
403 domain_proto_mtx_lock_assert_held();
405 TAILQ_FOREACH(dom
, &domains
, dom_entry
) {
406 if (dom
->dom_rtattach
!= NULL
)
407 dom
->dom_rtattach(&table
[dom
->dom_family
],
413 * Called by route_dinit().
421 _CASSERT(offsetof(struct route
, ro_rt
) ==
422 offsetof(struct route_in6
, ro_rt
));
423 _CASSERT(offsetof(struct route
, ro_lle
) ==
424 offsetof(struct route_in6
, ro_lle
));
425 _CASSERT(offsetof(struct route
, ro_srcia
) ==
426 offsetof(struct route_in6
, ro_srcia
));
427 _CASSERT(offsetof(struct route
, ro_flags
) ==
428 offsetof(struct route_in6
, ro_flags
));
429 _CASSERT(offsetof(struct route
, ro_dst
) ==
430 offsetof(struct route_in6
, ro_dst
));
433 PE_parse_boot_argn("rte_debug", &rte_debug
, sizeof (rte_debug
));
435 rte_debug
|= RTD_DEBUG
;
437 rnh_lock_grp_attr
= lck_grp_attr_alloc_init();
438 rnh_lock_grp
= lck_grp_alloc_init("route", rnh_lock_grp_attr
);
439 rnh_lock_attr
= lck_attr_alloc_init();
440 lck_mtx_init(rnh_lock
, rnh_lock_grp
, rnh_lock_attr
);
442 rte_mtx_grp_attr
= lck_grp_attr_alloc_init();
443 rte_mtx_grp
= lck_grp_alloc_init(RTE_NAME
, rte_mtx_grp_attr
);
444 rte_mtx_attr
= lck_attr_alloc_init();
446 lck_mtx_lock(rnh_lock
);
447 rn_init(); /* initialize all zeroes, all ones, mask table */
448 lck_mtx_unlock(rnh_lock
);
449 rtable_init((void **)rt_tables
);
451 if (rte_debug
& RTD_DEBUG
)
452 size
= sizeof (struct rtentry_dbg
);
454 size
= sizeof (struct rtentry
);
456 rte_zone
= zinit(size
, RTE_ZONE_MAX
* size
, 0, RTE_ZONE_NAME
);
457 if (rte_zone
== NULL
) {
458 panic("%s: failed allocating rte_zone", __func__
);
461 zone_change(rte_zone
, Z_EXPAND
, TRUE
);
462 zone_change(rte_zone
, Z_CALLERACCT
, FALSE
);
463 zone_change(rte_zone
, Z_NOENCRYPT
, TRUE
);
465 TAILQ_INIT(&rttrash_head
);
469 * Given a route, determine whether or not it is the non-scoped default
470 * route; dst typically comes from rt_key(rt) but may be coming from
471 * a separate place when rt is in the process of being created.
474 rt_primary_default(struct rtentry
*rt
, struct sockaddr
*dst
)
476 return (SA_DEFAULT(dst
) && !(rt
->rt_flags
& RTF_IFSCOPE
));
480 * Set the ifscope of the primary interface; caller holds rnh_lock.
483 set_primary_ifscope(int af
, unsigned int ifscope
)
486 primary_ifscope
= ifscope
;
488 primary6_ifscope
= ifscope
;
492 * Return the ifscope of the primary interface; caller holds rnh_lock.
495 get_primary_ifscope(int af
)
497 return (af
== AF_INET
? primary_ifscope
: primary6_ifscope
);
501 * Set the scope ID of a given a sockaddr_in.
504 sin_set_ifscope(struct sockaddr
*sa
, unsigned int ifscope
)
506 /* Caller must pass in sockaddr_in */
507 ASSERT_SINIFSCOPE(sa
);
509 SINIFSCOPE(sa
)->sin_scope_id
= ifscope
;
513 * Set the scope ID of given a sockaddr_in6.
516 sin6_set_ifscope(struct sockaddr
*sa
, unsigned int ifscope
)
518 /* Caller must pass in sockaddr_in6 */
519 ASSERT_SIN6IFSCOPE(sa
);
521 SIN6IFSCOPE(sa
)->sin6_scope_id
= ifscope
;
525 * Given a sockaddr_in, return the scope ID to the caller.
528 sin_get_ifscope(struct sockaddr
*sa
)
530 /* Caller must pass in sockaddr_in */
531 ASSERT_SINIFSCOPE(sa
);
533 return (SINIFSCOPE(sa
)->sin_scope_id
);
537 * Given a sockaddr_in6, return the scope ID to the caller.
540 sin6_get_ifscope(struct sockaddr
*sa
)
542 /* Caller must pass in sockaddr_in6 */
543 ASSERT_SIN6IFSCOPE(sa
);
545 return (SIN6IFSCOPE(sa
)->sin6_scope_id
);
549 sin6_set_embedded_ifscope(struct sockaddr
*sa
, unsigned int ifscope
)
551 /* Caller must pass in sockaddr_in6 */
552 ASSERT_SIN6IFSCOPE(sa
);
553 VERIFY(IN6_IS_SCOPE_EMBED(&(SIN6(sa
)->sin6_addr
)));
555 SIN6(sa
)->sin6_addr
.s6_addr16
[1] = htons(ifscope
);
558 static inline unsigned int
559 sin6_get_embedded_ifscope(struct sockaddr
*sa
)
561 /* Caller must pass in sockaddr_in6 */
562 ASSERT_SIN6IFSCOPE(sa
);
564 return (ntohs(SIN6(sa
)->sin6_addr
.s6_addr16
[1]));
568 * Copy a sockaddr_{in,in6} src to a dst storage and set scope ID into dst.
570 * To clear the scope ID, pass is a NULL pifscope. To set the scope ID, pass
571 * in a non-NULL pifscope with non-zero ifscope. Otherwise if pifscope is
572 * non-NULL and ifscope is IFSCOPE_NONE, the existing scope ID is left intact.
573 * In any case, the effective scope ID value is returned to the caller via
574 * pifscope, if it is non-NULL.
577 sa_copy(struct sockaddr
*src
, struct sockaddr_storage
*dst
,
578 unsigned int *pifscope
)
580 int af
= src
->sa_family
;
581 unsigned int ifscope
= (pifscope
!= NULL
) ? *pifscope
: IFSCOPE_NONE
;
583 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
585 bzero(dst
, sizeof (*dst
));
588 bcopy(src
, dst
, sizeof (struct sockaddr_in
));
589 if (pifscope
== NULL
|| ifscope
!= IFSCOPE_NONE
)
590 sin_set_ifscope(SA(dst
), ifscope
);
592 bcopy(src
, dst
, sizeof (struct sockaddr_in6
));
593 if (pifscope
!= NULL
&&
594 IN6_IS_SCOPE_EMBED(&SIN6(dst
)->sin6_addr
)) {
595 unsigned int eifscope
;
597 * If the address contains the embedded scope ID,
598 * use that as the value for sin6_scope_id as long
599 * the caller doesn't insist on clearing it (by
600 * passing NULL) or setting it.
602 eifscope
= sin6_get_embedded_ifscope(SA(dst
));
603 if (eifscope
!= IFSCOPE_NONE
&& ifscope
== IFSCOPE_NONE
)
605 if (ifscope
!= IFSCOPE_NONE
) {
606 /* Set ifscope from pifscope or eifscope */
607 sin6_set_ifscope(SA(dst
), ifscope
);
609 /* If sin6_scope_id has a value, use that one */
610 ifscope
= sin6_get_ifscope(SA(dst
));
613 * If sin6_scope_id is set but the address doesn't
614 * contain the equivalent embedded value, set it.
616 if (ifscope
!= IFSCOPE_NONE
&& eifscope
!= ifscope
)
617 sin6_set_embedded_ifscope(SA(dst
), ifscope
);
618 } else if (pifscope
== NULL
|| ifscope
!= IFSCOPE_NONE
) {
619 sin6_set_ifscope(SA(dst
), ifscope
);
623 if (pifscope
!= NULL
) {
624 *pifscope
= (af
== AF_INET
) ? sin_get_ifscope(SA(dst
)) :
625 sin6_get_ifscope(SA(dst
));
632 * Copy a mask from src to a dst storage and set scope ID into dst.
634 static struct sockaddr
*
635 ma_copy(int af
, struct sockaddr
*src
, struct sockaddr_storage
*dst
,
636 unsigned int ifscope
)
638 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
640 bzero(dst
, sizeof (*dst
));
641 rt_maskedcopy(src
, SA(dst
), src
);
644 * The length of the mask sockaddr would need to be adjusted
645 * to cover the additional {sin,sin6}_ifscope field; when ifscope
646 * is IFSCOPE_NONE, we'd end up clearing the scope ID field on
647 * the destination mask in addition to extending the length
648 * of the sockaddr, as a side effect. This is okay, as any
649 * trailing zeroes would be skipped by rn_addmask prior to
650 * inserting or looking up the mask in the mask tree.
653 SINIFSCOPE(dst
)->sin_scope_id
= ifscope
;
654 SINIFSCOPE(dst
)->sin_len
=
655 offsetof(struct sockaddr_inifscope
, sin_scope_id
) +
656 sizeof (SINIFSCOPE(dst
)->sin_scope_id
);
658 SIN6IFSCOPE(dst
)->sin6_scope_id
= ifscope
;
659 SIN6IFSCOPE(dst
)->sin6_len
=
660 offsetof(struct sockaddr_in6
, sin6_scope_id
) +
661 sizeof (SIN6IFSCOPE(dst
)->sin6_scope_id
);
668 * Trim trailing zeroes on a sockaddr and update its length.
670 static struct sockaddr
*
671 sa_trim(struct sockaddr
*sa
, int skip
)
673 caddr_t cp
, base
= (caddr_t
)sa
+ skip
;
675 if (sa
->sa_len
<= skip
)
678 for (cp
= base
+ (sa
->sa_len
- skip
); cp
> base
&& cp
[-1] == 0; )
681 sa
->sa_len
= (cp
- base
) + skip
;
682 if (sa
->sa_len
< skip
) {
683 /* Must not happen, and if so, panic */
684 panic("%s: broken logic (sa_len %d < skip %d )", __func__
,
687 } else if (sa
->sa_len
== skip
) {
688 /* If we end up with all zeroes, then there's no mask */
696 * Called by rtm_msg{1,2} routines to "scrub" socket address structures of
697 * kernel private information, so that clients of the routing socket will
698 * not be confused by the presence of the information, or the side effect of
699 * the increased length due to that. The source sockaddr is not modified;
700 * instead, the scrubbing happens on the destination sockaddr storage that
701 * is passed in by the caller.
704 * - removing embedded scope identifiers from network mask and destination
705 * IPv4 and IPv6 socket addresses
706 * - optionally removing global scope interface hardware addresses from
707 * link-layer interface addresses when the MAC framework check fails.
710 rtm_scrub(int type
, int idx
, struct sockaddr
*hint
, struct sockaddr
*sa
,
711 void *buf
, uint32_t buflen
, kauth_cred_t
*credp
)
713 struct sockaddr_storage
*ss
= (struct sockaddr_storage
*)buf
;
714 struct sockaddr
*ret
= sa
;
716 VERIFY(buf
!= NULL
&& buflen
>= sizeof (*ss
));
722 * If this is for an AF_INET/AF_INET6 destination address,
723 * call sa_copy() to clear the scope ID field.
725 if (sa
->sa_family
== AF_INET
&&
726 SINIFSCOPE(sa
)->sin_scope_id
!= IFSCOPE_NONE
) {
727 ret
= sa_copy(sa
, ss
, NULL
);
728 } else if (sa
->sa_family
== AF_INET6
&&
729 SIN6IFSCOPE(sa
)->sin6_scope_id
!= IFSCOPE_NONE
) {
730 ret
= sa_copy(sa
, ss
, NULL
);
737 * If this is for a mask, we can't tell whether or not there
738 * is an valid scope ID value, as the span of bytes between
739 * sa_len and the beginning of the mask (offset of sin_addr in
740 * the case of AF_INET, or sin6_addr for AF_INET6) may be
741 * filled with all-ones by rn_addmask(), and hence we cannot
742 * rely on sa_family. Because of this, we use the sa_family
743 * of the hint sockaddr (RTAX_{DST,IFA}) as indicator as to
744 * whether or not the mask is to be treated as one for AF_INET
745 * or AF_INET6. Clearing the scope ID field involves setting
746 * it to IFSCOPE_NONE followed by calling sa_trim() to trim
747 * trailing zeroes from the storage sockaddr, which reverses
748 * what was done earlier by ma_copy() on the source sockaddr.
751 ((af
= hint
->sa_family
) != AF_INET
&& af
!= AF_INET6
))
752 break; /* nothing to do */
754 skip
= (af
== AF_INET
) ?
755 offsetof(struct sockaddr_in
, sin_addr
) :
756 offsetof(struct sockaddr_in6
, sin6_addr
);
758 if (sa
->sa_len
> skip
&& sa
->sa_len
<= sizeof (*ss
)) {
759 bcopy(sa
, ss
, sa
->sa_len
);
761 * Don't use {sin,sin6}_set_ifscope() as sa_family
762 * and sa_len for the netmask might not be set to
763 * the corresponding expected values of the hint.
765 if (hint
->sa_family
== AF_INET
)
766 SINIFSCOPE(ss
)->sin_scope_id
= IFSCOPE_NONE
;
768 SIN6IFSCOPE(ss
)->sin6_scope_id
= IFSCOPE_NONE
;
769 ret
= sa_trim(SA(ss
), skip
);
772 * For AF_INET6 mask, set sa_len appropriately unless
773 * this is requested via systl_dumpentry(), in which
774 * case we return the raw value.
776 if (hint
->sa_family
== AF_INET6
&&
777 type
!= RTM_GET
&& type
!= RTM_GET2
)
778 SA(ret
)->sa_len
= sizeof (struct sockaddr_in6
);
784 * Break if the gateway is not AF_LINK type (indirect routes)
786 * Else, if is, check if it is resolved. If not yet resolved
787 * simply break else scrub the link layer address.
789 if ((sa
->sa_family
!= AF_LINK
) || (SDL(sa
)->sdl_alen
== 0))
794 if (sa
->sa_family
== AF_LINK
&& credp
) {
795 struct sockaddr_dl
*sdl
= SDL(buf
);
799 /* caller should handle worst case: SOCK_MAXADDRLEN */
800 VERIFY(buflen
>= sa
->sa_len
);
802 bcopy(sa
, sdl
, sa
->sa_len
);
803 bytes
= dlil_ifaddr_bytes(sdl
, &size
, credp
);
804 if (bytes
!= CONST_LLADDR(sdl
)) {
805 VERIFY(sdl
->sdl_alen
== size
);
806 bcopy(bytes
, LLADDR(sdl
), size
);
808 ret
= (struct sockaddr
*)sdl
;
820 * Callback leaf-matching routine for rn_matchaddr_args used
821 * for looking up an exact match for a scoped route entry.
824 rn_match_ifscope(struct radix_node
*rn
, void *arg
)
826 struct rtentry
*rt
= (struct rtentry
*)rn
;
827 struct matchleaf_arg
*ma
= arg
;
828 int af
= rt_key(rt
)->sa_family
;
830 if (!(rt
->rt_flags
& RTF_IFSCOPE
) || (af
!= AF_INET
&& af
!= AF_INET6
))
833 return (af
== AF_INET
?
834 (SINIFSCOPE(rt_key(rt
))->sin_scope_id
== ma
->ifscope
) :
835 (SIN6IFSCOPE(rt_key(rt
))->sin6_scope_id
== ma
->ifscope
));
839 * Atomically increment route generation counter
842 routegenid_update(void)
844 routegenid_inet_update();
846 routegenid_inet6_update();
851 routegenid_inet_update(void)
853 atomic_add_32(&route_genid_inet
, 1);
858 routegenid_inet6_update(void)
860 atomic_add_32(&route_genid_inet6
, 1);
865 * Packet routing routines.
868 rtalloc(struct route
*ro
)
874 rtalloc_scoped(struct route
*ro
, unsigned int ifscope
)
876 rtalloc_scoped_ign(ro
, 0, ifscope
);
880 rtalloc_ign_common_locked(struct route
*ro
, uint32_t ignore
,
881 unsigned int ifscope
)
885 if ((rt
= ro
->ro_rt
) != NULL
) {
887 if (rt
->rt_ifp
!= NULL
&& !ROUTE_UNUSABLE(ro
)) {
892 ROUTE_RELEASE_LOCKED(ro
); /* rnh_lock already held */
894 ro
->ro_rt
= rtalloc1_common_locked(&ro
->ro_dst
, 1, ignore
, ifscope
);
895 if (ro
->ro_rt
!= NULL
) {
896 RT_GENID_SYNC(ro
->ro_rt
);
897 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
902 rtalloc_ign(struct route
*ro
, uint32_t ignore
)
904 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
905 lck_mtx_lock(rnh_lock
);
906 rtalloc_ign_common_locked(ro
, ignore
, IFSCOPE_NONE
);
907 lck_mtx_unlock(rnh_lock
);
911 rtalloc_scoped_ign(struct route
*ro
, uint32_t ignore
, unsigned int ifscope
)
913 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
914 lck_mtx_lock(rnh_lock
);
915 rtalloc_ign_common_locked(ro
, ignore
, ifscope
);
916 lck_mtx_unlock(rnh_lock
);
919 static struct rtentry
*
920 rtalloc1_locked(struct sockaddr
*dst
, int report
, uint32_t ignflags
)
922 return (rtalloc1_common_locked(dst
, report
, ignflags
, IFSCOPE_NONE
));
926 rtalloc1_scoped_locked(struct sockaddr
*dst
, int report
, uint32_t ignflags
,
927 unsigned int ifscope
)
929 return (rtalloc1_common_locked(dst
, report
, ignflags
, ifscope
));
933 rtalloc1_common_locked(struct sockaddr
*dst
, int report
, uint32_t ignflags
,
934 unsigned int ifscope
)
936 struct radix_node_head
*rnh
= rt_tables
[dst
->sa_family
];
937 struct rtentry
*rt
, *newrt
= NULL
;
938 struct rt_addrinfo info
;
940 int err
= 0, msgtype
= RTM_MISS
;
946 * Find the longest prefix or exact (in the scoped case) address match;
947 * callee adds a reference to entry and checks for root node as well
949 rt
= rt_lookup(FALSE
, dst
, NULL
, rnh
, ifscope
);
955 nflags
= rt
->rt_flags
& ~ignflags
;
957 if (report
&& (nflags
& (RTF_CLONING
| RTF_PRCLONING
))) {
959 * We are apparently adding (report = 0 in delete).
960 * If it requires that it be cloned, do so.
961 * (This implies it wasn't a HOST route.)
963 err
= rtrequest_locked(RTM_RESOLVE
, dst
, NULL
, NULL
, 0, &newrt
);
966 * If the cloning didn't succeed, maybe what we
967 * have from lookup above will do. Return that;
968 * no need to hold another reference since it's
976 * We cloned it; drop the original route found during lookup.
977 * The resulted cloned route (newrt) would now have an extra
978 * reference held during rtrequest.
983 * If the newly created cloned route is a direct host route
984 * then also check if it is to a router or not.
985 * If it is, then set the RTF_ROUTER flag on the host route
988 * XXX It is possible for the default route to be created post
989 * cloned route creation of router's IP.
990 * We can handle that corner case by special handing for RTM_ADD
993 if ((newrt
->rt_flags
& (RTF_HOST
| RTF_LLINFO
)) ==
994 (RTF_HOST
| RTF_LLINFO
)) {
995 struct rtentry
*defrt
= NULL
;
996 struct sockaddr_storage def_key
;
998 bzero(&def_key
, sizeof(def_key
));
999 def_key
.ss_len
= rt_key(newrt
)->sa_len
;
1000 def_key
.ss_family
= rt_key(newrt
)->sa_family
;
1002 defrt
= rtalloc1_scoped_locked((struct sockaddr
*)&def_key
,
1003 0, 0, newrt
->rt_ifp
->if_index
);
1006 if (equal(rt_key(newrt
), defrt
->rt_gateway
)) {
1007 newrt
->rt_flags
|= RTF_ROUTER
;
1009 rtfree_locked(defrt
);
1013 if ((rt
= newrt
) && (rt
->rt_flags
& RTF_XRESOLVE
)) {
1015 * If the new route specifies it be
1016 * externally resolved, then go do that.
1018 msgtype
= RTM_RESOLVE
;
1026 * Either we hit the root or couldn't find any match,
1027 * Which basically means "cant get there from here"
1029 rtstat
.rts_unreach
++;
1034 * If required, report the failure to the supervising
1036 * For a delete, this is not an error. (report == 0)
1038 bzero((caddr_t
)&info
, sizeof(info
));
1039 info
.rti_info
[RTAX_DST
] = dst
;
1040 rt_missmsg(msgtype
, &info
, 0, err
);
1047 rtalloc1(struct sockaddr
*dst
, int report
, uint32_t ignflags
)
1049 struct rtentry
*entry
;
1050 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1051 lck_mtx_lock(rnh_lock
);
1052 entry
= rtalloc1_locked(dst
, report
, ignflags
);
1053 lck_mtx_unlock(rnh_lock
);
1058 rtalloc1_scoped(struct sockaddr
*dst
, int report
, uint32_t ignflags
,
1059 unsigned int ifscope
)
1061 struct rtentry
*entry
;
1062 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1063 lck_mtx_lock(rnh_lock
);
1064 entry
= rtalloc1_scoped_locked(dst
, report
, ignflags
, ifscope
);
1065 lck_mtx_unlock(rnh_lock
);
1070 * Remove a reference count from an rtentry.
1071 * If the count gets low enough, take it out of the routing table
1074 rtfree_locked(struct rtentry
*rt
)
1076 rtfree_common(rt
, TRUE
);
1080 rtfree_common(struct rtentry
*rt
, boolean_t locked
)
1082 struct radix_node_head
*rnh
;
1084 LCK_MTX_ASSERT(rnh_lock
, locked
?
1085 LCK_MTX_ASSERT_OWNED
: LCK_MTX_ASSERT_NOTOWNED
);
1088 * Atomically decrement the reference count and if it reaches 0,
1089 * and there is a close function defined, call the close function.
1092 if (rtunref(rt
) > 0) {
1098 * To avoid violating lock ordering, we must drop rt_lock before
1099 * trying to acquire the global rnh_lock. If we are called with
1100 * rnh_lock held, then we already have exclusive access; otherwise
1101 * we do the lock dance.
1105 * Note that we check it again below after grabbing rnh_lock,
1106 * since it is possible that another thread doing a lookup wins
1107 * the race, grabs the rnh_lock first, and bumps up reference
1108 * count in which case the route should be left alone as it is
1109 * still in use. It's also possible that another thread frees
1110 * the route after we drop rt_lock; to prevent the route from
1111 * being freed, we hold an extra reference.
1113 RT_ADDREF_LOCKED(rt
);
1115 lck_mtx_lock(rnh_lock
);
1117 if (rtunref(rt
) > 0) {
1118 /* We've lost the race, so abort */
1125 * We may be blocked on other lock(s) as part of freeing
1126 * the entry below, so convert from spin to full mutex.
1128 RT_CONVERT_LOCK(rt
);
1130 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1132 /* Negative refcnt must never happen */
1133 if (rt
->rt_refcnt
!= 0) {
1134 panic("rt %p invalid refcnt %d", rt
, rt
->rt_refcnt
);
1137 /* Idle refcnt must have been dropped during rtunref() */
1138 VERIFY(!(rt
->rt_flags
& RTF_IFREF
));
1141 * find the tree for that address family
1142 * Note: in the case of igmp packets, there might not be an rnh
1144 rnh
= rt_tables
[rt_key(rt
)->sa_family
];
1147 * On last reference give the "close method" a chance to cleanup
1148 * private state. This also permits (for IPv4 and IPv6) a chance
1149 * to decide if the routing table entry should be purged immediately
1150 * or at a later time. When an immediate purge is to happen the
1151 * close routine typically issues RTM_DELETE which clears the RTF_UP
1152 * flag on the entry so that the code below reclaims the storage.
1154 if (rnh
!= NULL
&& rnh
->rnh_close
!= NULL
)
1155 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
1158 * If we are no longer "up" (and ref == 0) then we can free the
1159 * resources associated with the route.
1161 if (!(rt
->rt_flags
& RTF_UP
)) {
1162 struct rtentry
*rt_parent
;
1163 struct ifaddr
*rt_ifa
;
1165 rt
->rt_flags
|= RTF_DEAD
;
1166 if (rt
->rt_nodes
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)) {
1167 panic("rt %p freed while in radix tree\n", rt
);
1171 * the rtentry must have been removed from the routing table
1172 * so it is represented in rttrash; remove that now.
1174 (void) OSDecrementAtomic(&rttrash
);
1175 if (rte_debug
& RTD_DEBUG
) {
1176 TAILQ_REMOVE(&rttrash_head
, (struct rtentry_dbg
*)rt
,
1181 * release references on items we hold them on..
1182 * e.g other routes and ifaddrs.
1184 if ((rt_parent
= rt
->rt_parent
) != NULL
)
1185 rt
->rt_parent
= NULL
;
1187 if ((rt_ifa
= rt
->rt_ifa
) != NULL
)
1191 * Now free any attached link-layer info.
1193 if (rt
->rt_llinfo
!= NULL
) {
1194 if (rt
->rt_llinfo_free
!= NULL
)
1195 (*rt
->rt_llinfo_free
)(rt
->rt_llinfo
);
1197 R_Free(rt
->rt_llinfo
);
1198 rt
->rt_llinfo
= NULL
;
1201 /* Destroy eventhandler lists context */
1202 eventhandler_lists_ctxt_destroy(&rt
->rt_evhdlr_ctxt
);
1205 * Route is no longer in the tree and refcnt is 0;
1206 * we have exclusive access, so destroy it.
1209 rte_lock_destroy(rt
);
1211 if (rt_parent
!= NULL
)
1212 rtfree_locked(rt_parent
);
1218 * The key is separately alloc'd so free it (see rt_setgate()).
1219 * This also frees the gateway, as they are always malloc'd
1225 * Free any statistics that may have been allocated
1227 nstat_route_detach(rt
);
1230 * and the rtentry itself of course
1235 * The "close method" has been called, but the route is
1236 * still in the radix tree with zero refcnt, i.e. "up"
1237 * and in the cached state.
1243 lck_mtx_unlock(rnh_lock
);
1247 rtfree(struct rtentry
*rt
)
1249 rtfree_common(rt
, FALSE
);
1253 * Decrements the refcount but does not free the route when
1254 * the refcount reaches zero. Unless you have really good reason,
1255 * use rtfree not rtunref.
1258 rtunref(struct rtentry
*p
)
1260 RT_LOCK_ASSERT_HELD(p
);
1262 if (p
->rt_refcnt
== 0) {
1263 panic("%s(%p) bad refcnt\n", __func__
, p
);
1265 } else if (--p
->rt_refcnt
== 0) {
1267 * Release any idle reference count held on the interface;
1268 * if the route is eligible, still UP and the refcnt becomes
1269 * non-zero at some point in future before it is purged from
1270 * the routing table, rt_set_idleref() will undo this.
1272 rt_clear_idleref(p
);
1275 if (rte_debug
& RTD_DEBUG
)
1276 rtunref_audit((struct rtentry_dbg
*)p
);
1278 /* Return new value */
1279 return (p
->rt_refcnt
);
1283 rtunref_audit(struct rtentry_dbg
*rte
)
1287 if (rte
->rtd_inuse
!= RTD_INUSE
) {
1288 panic("rtunref: on freed rte=%p\n", rte
);
1291 idx
= atomic_add_16_ov(&rte
->rtd_refrele_cnt
, 1) % CTRACE_HIST_SIZE
;
1292 if (rte_debug
& RTD_TRACE
)
1293 ctrace_record(&rte
->rtd_refrele
[idx
]);
1297 * Add a reference count from an rtentry.
1300 rtref(struct rtentry
*p
)
1302 RT_LOCK_ASSERT_HELD(p
);
1304 VERIFY((p
->rt_flags
& RTF_DEAD
) == 0);
1305 if (++p
->rt_refcnt
== 0) {
1306 panic("%s(%p) bad refcnt\n", __func__
, p
);
1308 } else if (p
->rt_refcnt
== 1) {
1310 * Hold an idle reference count on the interface,
1311 * if the route is eligible for it.
1316 if (rte_debug
& RTD_DEBUG
)
1317 rtref_audit((struct rtentry_dbg
*)p
);
1321 rtref_audit(struct rtentry_dbg
*rte
)
1325 if (rte
->rtd_inuse
!= RTD_INUSE
) {
1326 panic("rtref_audit: on freed rte=%p\n", rte
);
1329 idx
= atomic_add_16_ov(&rte
->rtd_refhold_cnt
, 1) % CTRACE_HIST_SIZE
;
1330 if (rte_debug
& RTD_TRACE
)
1331 ctrace_record(&rte
->rtd_refhold
[idx
]);
1335 rtsetifa(struct rtentry
*rt
, struct ifaddr
*ifa
)
1337 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1339 RT_LOCK_ASSERT_HELD(rt
);
1341 if (rt
->rt_ifa
== ifa
)
1344 /* Become a regular mutex, just in case */
1345 RT_CONVERT_LOCK(rt
);
1347 /* Release the old ifa */
1349 IFA_REMREF(rt
->rt_ifa
);
1354 /* Take a reference to the ifa */
1356 IFA_ADDREF(rt
->rt_ifa
);
1360 * Force a routing table entry to the specified
1361 * destination to go through the given gateway.
1362 * Normally called as a result of a routing redirect
1363 * message from the network layer.
1366 rtredirect(struct ifnet
*ifp
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
1367 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
,
1368 struct rtentry
**rtp
)
1370 struct rtentry
*rt
= NULL
;
1373 struct rt_addrinfo info
;
1374 struct ifaddr
*ifa
= NULL
;
1375 unsigned int ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
1376 struct sockaddr_storage ss
;
1377 int af
= src
->sa_family
;
1379 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1380 lck_mtx_lock(rnh_lock
);
1383 * Transform src into the internal routing table form for
1384 * comparison against rt_gateway below.
1387 if ((af
== AF_INET
) || (af
== AF_INET6
))
1391 src
= sa_copy(src
, &ss
, &ifscope
);
1394 * Verify the gateway is directly reachable; if scoped routing
1395 * is enabled, verify that it is reachable from the interface
1396 * where the ICMP redirect arrived on.
1398 if ((ifa
= ifa_ifwithnet_scoped(gateway
, ifscope
)) == NULL
) {
1399 error
= ENETUNREACH
;
1403 /* Lookup route to the destination (from the original IP header) */
1404 rt
= rtalloc1_scoped_locked(dst
, 0, RTF_CLONING
|RTF_PRCLONING
, ifscope
);
1409 * If the redirect isn't from our current router for this dst,
1410 * it's either old or wrong. If it redirects us to ourselves,
1411 * we have a routing loop, perhaps as a result of an interface
1412 * going down recently. Holding rnh_lock here prevents the
1413 * possibility of rt_ifa/ifa's ifa_addr from changing (e.g.
1414 * in_ifinit), so okay to access ifa_addr without locking.
1416 if (!(flags
& RTF_DONE
) && rt
!= NULL
&&
1417 (!equal(src
, rt
->rt_gateway
) || !equal(rt
->rt_ifa
->ifa_addr
,
1422 if ((ifa
= ifa_ifwithaddr(gateway
))) {
1425 error
= EHOSTUNREACH
;
1441 * Create a new entry if we just got back a wildcard entry
1442 * or the the lookup failed. This is necessary for hosts
1443 * which use routing redirects generated by smart gateways
1444 * to dynamically build the routing tables.
1446 if ((rt
== NULL
) || (rt_mask(rt
) != NULL
&& rt_mask(rt
)->sa_len
< 2))
1449 * Don't listen to the redirect if it's
1450 * for a route to an interface.
1452 RT_LOCK_ASSERT_HELD(rt
);
1453 if (rt
->rt_flags
& RTF_GATEWAY
) {
1454 if (((rt
->rt_flags
& RTF_HOST
) == 0) && (flags
& RTF_HOST
)) {
1456 * Changing from route to net => route to host.
1457 * Create new route, rather than smashing route
1458 * to net; similar to cloned routes, the newly
1459 * created host route is scoped as well.
1464 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
1465 error
= rtrequest_scoped_locked(RTM_ADD
, dst
,
1466 gateway
, netmask
, flags
, NULL
, ifscope
);
1467 stat
= &rtstat
.rts_dynamic
;
1470 * Smash the current notion of the gateway to
1471 * this destination. Should check about netmask!!!
1473 rt
->rt_flags
|= RTF_MODIFIED
;
1474 flags
|= RTF_MODIFIED
;
1475 stat
= &rtstat
.rts_newgateway
;
1477 * add the key and gateway (in one malloc'd chunk).
1479 error
= rt_setgate(rt
, rt_key(rt
), gateway
);
1484 error
= EHOSTUNREACH
;
1488 RT_LOCK_ASSERT_NOTHELD(rt
);
1490 /* Enqueue event to refresh flow route entries */
1491 route_event_enqueue_nwk_wq_entry(rt
, NULL
, ROUTE_ENTRY_REFRESH
, NULL
, FALSE
);
1502 rtstat
.rts_badredirect
++;
1508 routegenid_inet_update();
1510 else if (af
== AF_INET6
)
1511 routegenid_inet6_update();
1514 lck_mtx_unlock(rnh_lock
);
1515 bzero((caddr_t
)&info
, sizeof(info
));
1516 info
.rti_info
[RTAX_DST
] = dst
;
1517 info
.rti_info
[RTAX_GATEWAY
] = gateway
;
1518 info
.rti_info
[RTAX_NETMASK
] = netmask
;
1519 info
.rti_info
[RTAX_AUTHOR
] = src
;
1520 rt_missmsg(RTM_REDIRECT
, &info
, flags
, error
);
1524 * Routing table ioctl interface.
1527 rtioctl(unsigned long req
, caddr_t data
, struct proc
*p
)
1529 #pragma unused(p, req, data)
1536 const struct sockaddr
*dst
,
1537 const struct sockaddr
*gateway
)
1541 lck_mtx_lock(rnh_lock
);
1542 ifa
= ifa_ifwithroute_locked(flags
, dst
, gateway
);
1543 lck_mtx_unlock(rnh_lock
);
1549 ifa_ifwithroute_locked(int flags
, const struct sockaddr
*dst
,
1550 const struct sockaddr
*gateway
)
1552 return (ifa_ifwithroute_common_locked((flags
& ~RTF_IFSCOPE
), dst
,
1553 gateway
, IFSCOPE_NONE
));
1557 ifa_ifwithroute_scoped_locked(int flags
, const struct sockaddr
*dst
,
1558 const struct sockaddr
*gateway
, unsigned int ifscope
)
1560 if (ifscope
!= IFSCOPE_NONE
)
1561 flags
|= RTF_IFSCOPE
;
1563 flags
&= ~RTF_IFSCOPE
;
1565 return (ifa_ifwithroute_common_locked(flags
, dst
, gateway
, ifscope
));
1568 static struct ifaddr
*
1569 ifa_ifwithroute_common_locked(int flags
, const struct sockaddr
*dst
,
1570 const struct sockaddr
*gw
, unsigned int ifscope
)
1572 struct ifaddr
*ifa
= NULL
;
1573 struct rtentry
*rt
= NULL
;
1574 struct sockaddr_storage dst_ss
, gw_ss
;
1576 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1579 * Just in case the sockaddr passed in by the caller
1580 * contains a scope ID, make sure to clear it since
1581 * interface addresses aren't scoped.
1585 ((dst
->sa_family
== AF_INET
) ||
1586 (dst
->sa_family
== AF_INET6
)))
1588 if (dst
!= NULL
&& dst
->sa_family
== AF_INET
)
1590 dst
= sa_copy(SA((uintptr_t)dst
), &dst_ss
, NULL
);
1594 ((gw
->sa_family
== AF_INET
) ||
1595 (gw
->sa_family
== AF_INET6
)))
1597 if (gw
!= NULL
&& gw
->sa_family
== AF_INET
)
1599 gw
= sa_copy(SA((uintptr_t)gw
), &gw_ss
, NULL
);
1601 if (!(flags
& RTF_GATEWAY
)) {
1603 * If we are adding a route to an interface,
1604 * and the interface is a pt to pt link
1605 * we should search for the destination
1606 * as our clue to the interface. Otherwise
1607 * we can use the local address.
1609 if (flags
& RTF_HOST
) {
1610 ifa
= ifa_ifwithdstaddr(dst
);
1613 ifa
= ifa_ifwithaddr_scoped(gw
, ifscope
);
1616 * If we are adding a route to a remote net
1617 * or host, the gateway may still be on the
1618 * other end of a pt to pt link.
1620 ifa
= ifa_ifwithdstaddr(gw
);
1623 ifa
= ifa_ifwithnet_scoped(gw
, ifscope
);
1625 /* Workaround to avoid gcc warning regarding const variable */
1626 rt
= rtalloc1_scoped_locked((struct sockaddr
*)(size_t)dst
,
1632 /* Become a regular mutex */
1633 RT_CONVERT_LOCK(rt
);
1636 RT_REMREF_LOCKED(rt
);
1642 * Holding rnh_lock here prevents the possibility of ifa from
1643 * changing (e.g. in_ifinit), so it is safe to access its
1644 * ifa_addr (here and down below) without locking.
1646 if (ifa
!= NULL
&& ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
1647 struct ifaddr
*newifa
;
1648 /* Callee adds reference to newifa upon success */
1649 newifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
1650 if (newifa
!= NULL
) {
1656 * If we are adding a gateway, it is quite possible that the
1657 * routing table has a static entry in place for the gateway,
1658 * that may not agree with info garnered from the interfaces.
1659 * The routing table should carry more precedence than the
1660 * interfaces in this matter. Must be careful not to stomp
1661 * on new entries from rtinit, hence (ifa->ifa_addr != gw).
1664 !equal(ifa
->ifa_addr
, (struct sockaddr
*)(size_t)gw
)) &&
1665 (rt
= rtalloc1_scoped_locked((struct sockaddr
*)(size_t)gw
,
1666 0, 0, ifscope
)) != NULL
) {
1672 /* Become a regular mutex */
1673 RT_CONVERT_LOCK(rt
);
1676 RT_REMREF_LOCKED(rt
);
1680 * If an interface scope was specified, the interface index of
1681 * the found ifaddr must be equivalent to that of the scope;
1682 * otherwise there is no match.
1684 if ((flags
& RTF_IFSCOPE
) &&
1685 ifa
!= NULL
&& ifa
->ifa_ifp
->if_index
!= ifscope
) {
1693 static int rt_fixdelete(struct radix_node
*, void *);
1694 static int rt_fixchange(struct radix_node
*, void *);
1697 struct rtentry
*rt0
;
1698 struct radix_node_head
*rnh
;
1702 rtrequest_locked(int req
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
1703 struct sockaddr
*netmask
, int flags
, struct rtentry
**ret_nrt
)
1705 return (rtrequest_common_locked(req
, dst
, gateway
, netmask
,
1706 (flags
& ~RTF_IFSCOPE
), ret_nrt
, IFSCOPE_NONE
));
1710 rtrequest_scoped_locked(int req
, struct sockaddr
*dst
,
1711 struct sockaddr
*gateway
, struct sockaddr
*netmask
, int flags
,
1712 struct rtentry
**ret_nrt
, unsigned int ifscope
)
1714 if (ifscope
!= IFSCOPE_NONE
)
1715 flags
|= RTF_IFSCOPE
;
1717 flags
&= ~RTF_IFSCOPE
;
1719 return (rtrequest_common_locked(req
, dst
, gateway
, netmask
,
1720 flags
, ret_nrt
, ifscope
));
1724 * Do appropriate manipulations of a routing tree given all the bits of
1727 * Storing the scope ID in the radix key is an internal job that should be
1728 * left to routines in this module. Callers should specify the scope value
1729 * to the "scoped" variants of route routines instead of manipulating the
1730 * key itself. This is typically done when creating a scoped route, e.g.
1731 * rtrequest(RTM_ADD). Once such a route is created and marked with the
1732 * RTF_IFSCOPE flag, callers can simply use its rt_key(rt) to clone it
1733 * (RTM_RESOLVE) or to remove it (RTM_DELETE). An exception to this is
1734 * during certain routing socket operations where the search key might be
1735 * derived from the routing message itself, in which case the caller must
1736 * specify the destination address and scope value for RTM_ADD/RTM_DELETE.
1739 rtrequest_common_locked(int req
, struct sockaddr
*dst0
,
1740 struct sockaddr
*gateway
, struct sockaddr
*netmask
, int flags
,
1741 struct rtentry
**ret_nrt
, unsigned int ifscope
)
1745 struct radix_node
*rn
;
1746 struct radix_node_head
*rnh
;
1747 struct ifaddr
*ifa
= NULL
;
1748 struct sockaddr
*ndst
, *dst
= dst0
;
1749 struct sockaddr_storage ss
, mask
;
1750 struct timeval caltime
;
1751 int af
= dst
->sa_family
;
1752 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1754 #define senderr(x) { error = x; goto bad; }
1756 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1758 * Find the correct routing tree to use for this Address Family
1760 if ((rnh
= rt_tables
[af
]) == NULL
)
1763 * If we are adding a host route then we don't want to put
1764 * a netmask in the tree
1766 if (flags
& RTF_HOST
)
1770 * If Scoped Routing is enabled, use a local copy of the destination
1771 * address to store the scope ID into. This logic is repeated below
1772 * in the RTM_RESOLVE handler since the caller does not normally
1773 * specify such a flag during a resolve, as well as for the handling
1774 * of IPv4 link-local address; instead, it passes in the route used for
1775 * cloning for which the scope info is derived from. Note also that
1776 * in the case of RTM_DELETE, the address passed in by the caller
1777 * might already contain the scope ID info when it is the key itself,
1778 * thus making RTF_IFSCOPE unnecessary; one instance where it is
1779 * explicitly set is inside route_output() as part of handling a
1780 * routing socket request.
1783 if (req
!= RTM_RESOLVE
&& ((af
== AF_INET
) || (af
== AF_INET6
))) {
1785 if (req
!= RTM_RESOLVE
&& af
== AF_INET
) {
1787 /* Transform dst into the internal routing table form */
1788 dst
= sa_copy(dst
, &ss
, &ifscope
);
1790 /* Transform netmask into the internal routing table form */
1791 if (netmask
!= NULL
)
1792 netmask
= ma_copy(af
, netmask
, &mask
, ifscope
);
1794 if (ifscope
!= IFSCOPE_NONE
)
1795 flags
|= RTF_IFSCOPE
;
1796 } else if ((flags
& RTF_IFSCOPE
) &&
1797 (af
!= AF_INET
&& af
!= AF_INET6
)) {
1801 if (ifscope
== IFSCOPE_NONE
)
1802 flags
&= ~RTF_IFSCOPE
;
1806 struct rtentry
*gwrt
= NULL
;
1807 boolean_t was_router
= FALSE
;
1808 uint32_t old_rt_refcnt
= 0;
1810 * Remove the item from the tree and return it.
1811 * Complain if it is not there and do no more processing.
1813 if ((rn
= rnh
->rnh_deladdr(dst
, netmask
, rnh
)) == NULL
)
1815 if (rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)) {
1816 panic("rtrequest delete");
1819 rt
= (struct rtentry
*)rn
;
1822 old_rt_refcnt
= rt
->rt_refcnt
;
1823 rt
->rt_flags
&= ~RTF_UP
;
1825 * Release any idle reference count held on the interface
1826 * as this route is no longer externally visible.
1828 rt_clear_idleref(rt
);
1830 * Take an extra reference to handle the deletion of a route
1831 * entry whose reference count is already 0; e.g. an expiring
1832 * cloned route entry or an entry that was added to the table
1833 * with 0 reference. If the caller is interested in this route,
1834 * we will return it with the reference intact. Otherwise we
1835 * will decrement the reference via rtfree_locked() and then
1836 * possibly deallocate it.
1838 RT_ADDREF_LOCKED(rt
);
1841 * For consistency, in case the caller didn't set the flag.
1843 rt
->rt_flags
|= RTF_CONDEMNED
;
1846 * Clear RTF_ROUTER if it's set.
1848 if (rt
->rt_flags
& RTF_ROUTER
) {
1850 VERIFY(rt
->rt_flags
& RTF_HOST
);
1851 rt
->rt_flags
&= ~RTF_ROUTER
;
1855 * Enqueue work item to invoke callback for this route entry
1857 * If the old count is 0, it implies that last reference is being
1858 * removed and there's no one listening for this route event.
1860 if (old_rt_refcnt
!= 0)
1861 route_event_enqueue_nwk_wq_entry(rt
, NULL
,
1862 ROUTE_ENTRY_DELETED
, NULL
, TRUE
);
1865 * Now search what's left of the subtree for any cloned
1866 * routes which might have been formed from this node.
1868 if ((rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) &&
1871 rnh
->rnh_walktree_from(rnh
, dst
, rt_mask(rt
),
1877 struct route_event rt_ev
;
1878 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_LLENTRY_DELETED
);
1880 (void) rnh
->rnh_walktree(rnh
,
1881 route_event_walktree
, (void *)&rt_ev
);
1886 * Remove any external references we may have.
1888 if ((gwrt
= rt
->rt_gwroute
) != NULL
)
1889 rt
->rt_gwroute
= NULL
;
1892 * give the protocol a chance to keep things in sync.
1894 if ((ifa
= rt
->rt_ifa
) != NULL
) {
1896 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1898 if (ifa_rtrequest
!= NULL
)
1899 ifa_rtrequest(RTM_DELETE
, rt
, NULL
);
1900 /* keep reference on rt_ifa */
1905 * one more rtentry floating around that is not
1906 * linked to the routing table.
1908 (void) OSIncrementAtomic(&rttrash
);
1909 if (rte_debug
& RTD_DEBUG
) {
1910 TAILQ_INSERT_TAIL(&rttrash_head
,
1911 (struct rtentry_dbg
*)rt
, rtd_trash_link
);
1915 * If this is the (non-scoped) default route, clear
1916 * the interface index used for the primary ifscope.
1918 if (rt_primary_default(rt
, rt_key(rt
))) {
1919 set_primary_ifscope(rt_key(rt
)->sa_family
,
1925 * If this is a change in a default route, update
1926 * necp client watchers to re-evaluate
1928 if (SA_DEFAULT(rt_key(rt
))) {
1929 necp_update_all_clients();
1936 * This might result in another rtentry being freed if
1937 * we held its last reference. Do this after the rtentry
1938 * lock is dropped above, as it could lead to the same
1939 * lock being acquired if gwrt is a clone of rt.
1942 rtfree_locked(gwrt
);
1945 * If the caller wants it, then it can have it,
1946 * but it's up to it to free the rtentry as we won't be
1949 if (ret_nrt
!= NULL
) {
1950 /* Return the route to caller with reference intact */
1953 /* Dereference or deallocate the route */
1957 routegenid_inet_update();
1959 else if (af
== AF_INET6
)
1960 routegenid_inet6_update();
1965 if (ret_nrt
== NULL
|| (rt
= *ret_nrt
) == NULL
)
1968 * According to the UNIX conformance tests, we need to return
1969 * ENETUNREACH when the parent route is RTF_REJECT.
1970 * However, there isn't any point in cloning RTF_REJECT
1971 * routes, so we immediately return an error.
1973 if (rt
->rt_flags
& RTF_REJECT
) {
1974 if (rt
->rt_flags
& RTF_HOST
) {
1975 senderr(EHOSTUNREACH
);
1977 senderr(ENETUNREACH
);
1981 * If cloning, we have the parent route given by the caller
1982 * and will use its rt_gateway, rt_rmx as part of the cloning
1983 * process below. Since rnh_lock is held at this point, the
1984 * parent's rt_ifa and rt_gateway will not change, and its
1985 * relevant rt_flags will not change as well. The only thing
1986 * that could change are the metrics, and thus we hold the
1987 * parent route's rt_lock later on during the actual copying
1992 flags
= rt
->rt_flags
&
1993 ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
1994 flags
|= RTF_WASCLONED
;
1995 gateway
= rt
->rt_gateway
;
1996 if ((netmask
= rt
->rt_genmask
) == NULL
)
2000 if (af
!= AF_INET
&& af
!= AF_INET6
)
2007 * When scoped routing is enabled, cloned entries are
2008 * always scoped according to the interface portion of
2009 * the parent route. The exception to this are IPv4
2010 * link local addresses, or those routes that are cloned
2011 * from a RTF_PROXY route. For the latter, the clone
2012 * gets to keep the RTF_PROXY flag.
2014 if ((af
== AF_INET
&&
2015 IN_LINKLOCAL(ntohl(SIN(dst
)->sin_addr
.s_addr
))) ||
2016 (rt
->rt_flags
& RTF_PROXY
)) {
2017 ifscope
= IFSCOPE_NONE
;
2018 flags
&= ~RTF_IFSCOPE
;
2020 * These types of cloned routes aren't currently
2021 * eligible for idle interface reference counting.
2023 flags
|= RTF_NOIFREF
;
2025 if (flags
& RTF_IFSCOPE
) {
2026 ifscope
= (af
== AF_INET
) ?
2027 sin_get_ifscope(rt_key(rt
)) :
2028 sin6_get_ifscope(rt_key(rt
));
2030 ifscope
= rt
->rt_ifp
->if_index
;
2031 flags
|= RTF_IFSCOPE
;
2033 VERIFY(ifscope
!= IFSCOPE_NONE
);
2037 * Transform dst into the internal routing table form,
2038 * clearing out the scope ID field if ifscope isn't set.
2040 dst
= sa_copy(dst
, &ss
, (ifscope
== IFSCOPE_NONE
) ?
2043 /* Transform netmask into the internal routing table form */
2044 if (netmask
!= NULL
)
2045 netmask
= ma_copy(af
, netmask
, &mask
, ifscope
);
2050 if ((flags
& RTF_GATEWAY
) && !gateway
) {
2051 panic("rtrequest: RTF_GATEWAY but no gateway");
2054 if (flags
& RTF_IFSCOPE
) {
2055 ifa
= ifa_ifwithroute_scoped_locked(flags
, dst0
,
2058 ifa
= ifa_ifwithroute_locked(flags
, dst0
, gateway
);
2061 senderr(ENETUNREACH
);
2064 * We land up here for both RTM_RESOLVE and RTM_ADD
2065 * when we decide to create a route.
2067 if ((rt
= rte_alloc()) == NULL
)
2069 Bzero(rt
, sizeof(*rt
));
2071 eventhandler_lists_ctxt_init(&rt
->rt_evhdlr_ctxt
);
2072 getmicrotime(&caltime
);
2073 rt
->base_calendartime
= caltime
.tv_sec
;
2074 rt
->base_uptime
= net_uptime();
2076 rt
->rt_flags
= RTF_UP
| flags
;
2079 * Point the generation ID to the tree's.
2083 rt
->rt_tree_genid
= &route_genid_inet
;
2087 rt
->rt_tree_genid
= &route_genid_inet6
;
2095 * Add the gateway. Possibly re-malloc-ing the storage for it
2096 * also add the rt_gwroute if possible.
2098 if ((error
= rt_setgate(rt
, dst
, gateway
)) != 0) {
2101 nstat_route_detach(rt
);
2102 rte_lock_destroy(rt
);
2108 * point to the (possibly newly malloc'd) dest address.
2113 * make sure it contains the value we want (masked if needed).
2116 rt_maskedcopy(dst
, ndst
, netmask
);
2118 Bcopy(dst
, ndst
, dst
->sa_len
);
2121 * Note that we now have a reference to the ifa.
2122 * This moved from below so that rnh->rnh_addaddr() can
2123 * examine the ifa and ifa->ifa_ifp if it so desires.
2126 rt
->rt_ifp
= rt
->rt_ifa
->ifa_ifp
;
2128 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
2130 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
, (caddr_t
)netmask
,
2133 struct rtentry
*rt2
;
2135 * Uh-oh, we already have one of these in the tree.
2136 * We do a special hack: if the route that's already
2137 * there was generated by the protocol-cloning
2138 * mechanism, then we just blow it away and retry
2139 * the insertion of the new one.
2141 if (flags
& RTF_IFSCOPE
) {
2142 rt2
= rtalloc1_scoped_locked(dst0
, 0,
2143 RTF_CLONING
| RTF_PRCLONING
, ifscope
);
2145 rt2
= rtalloc1_locked(dst
, 0,
2146 RTF_CLONING
| RTF_PRCLONING
);
2148 if (rt2
&& rt2
->rt_parent
) {
2150 * rnh_lock is held here, so rt_key and
2151 * rt_gateway of rt2 will not change.
2153 (void) rtrequest_locked(RTM_DELETE
, rt_key(rt2
),
2154 rt2
->rt_gateway
, rt_mask(rt2
),
2157 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
,
2158 (caddr_t
)netmask
, rnh
, rt
->rt_nodes
);
2160 /* undo the extra ref we got */
2166 * If it still failed to go into the tree,
2167 * then un-make it (this should be a function)
2170 /* Clear gateway route */
2171 rt_set_gwroute(rt
, rt_key(rt
), NULL
);
2173 IFA_REMREF(rt
->rt_ifa
);
2178 nstat_route_detach(rt
);
2179 rte_lock_destroy(rt
);
2184 rt
->rt_parent
= NULL
;
2187 * If we got here from RESOLVE, then we are cloning so clone
2188 * the rest, and note that we are a clone (and increment the
2189 * parent's references). rnh_lock is still held, which prevents
2190 * a lookup from returning the newly-created route. Hence
2191 * holding and releasing the parent's rt_lock while still
2192 * holding the route's rt_lock is safe since the new route
2193 * is not yet externally visible.
2195 if (req
== RTM_RESOLVE
) {
2196 RT_LOCK_SPIN(*ret_nrt
);
2197 VERIFY((*ret_nrt
)->rt_expire
== 0 ||
2198 (*ret_nrt
)->rt_rmx
.rmx_expire
!= 0);
2199 VERIFY((*ret_nrt
)->rt_expire
!= 0 ||
2200 (*ret_nrt
)->rt_rmx
.rmx_expire
== 0);
2201 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
;
2202 rt_setexpire(rt
, (*ret_nrt
)->rt_expire
);
2203 if ((*ret_nrt
)->rt_flags
&
2204 (RTF_CLONING
| RTF_PRCLONING
)) {
2205 rt
->rt_parent
= (*ret_nrt
);
2206 RT_ADDREF_LOCKED(*ret_nrt
);
2208 RT_UNLOCK(*ret_nrt
);
2212 * if this protocol has something to add to this then
2213 * allow it to do that as well.
2216 ifa_rtrequest
= ifa
->ifa_rtrequest
;
2218 if (ifa_rtrequest
!= NULL
)
2219 ifa_rtrequest(req
, rt
, SA(ret_nrt
? *ret_nrt
: NULL
));
2224 * If this is the (non-scoped) default route, record
2225 * the interface index used for the primary ifscope.
2227 if (rt_primary_default(rt
, rt_key(rt
))) {
2228 set_primary_ifscope(rt_key(rt
)->sa_family
,
2229 rt
->rt_ifp
->if_index
);
2234 * If this is a change in a default route, update
2235 * necp client watchers to re-evaluate
2237 if (SA_DEFAULT(rt_key(rt
))) {
2238 necp_update_all_clients();
2243 * actually return a resultant rtentry and
2244 * give the caller a single reference.
2248 RT_ADDREF_LOCKED(rt
);
2252 routegenid_inet_update();
2254 else if (af
== AF_INET6
)
2255 routegenid_inet6_update();
2261 * We repeat the same procedures from rt_setgate() here
2262 * because they weren't completed when we called it earlier,
2263 * since the node was embryonic.
2265 if ((rt
->rt_flags
& RTF_GATEWAY
) && rt
->rt_gwroute
!= NULL
)
2266 rt_set_gwroute(rt
, rt_key(rt
), rt
->rt_gwroute
);
2268 if (req
== RTM_ADD
&&
2269 !(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != NULL
) {
2270 struct rtfc_arg arg
;
2274 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
2275 rt_fixchange
, &arg
);
2280 nstat_route_new_entry(rt
);
2291 rtrequest(int req
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
2292 struct sockaddr
*netmask
, int flags
, struct rtentry
**ret_nrt
)
2295 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2296 lck_mtx_lock(rnh_lock
);
2297 error
= rtrequest_locked(req
, dst
, gateway
, netmask
, flags
, ret_nrt
);
2298 lck_mtx_unlock(rnh_lock
);
2303 rtrequest_scoped(int req
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
2304 struct sockaddr
*netmask
, int flags
, struct rtentry
**ret_nrt
,
2305 unsigned int ifscope
)
2308 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2309 lck_mtx_lock(rnh_lock
);
2310 error
= rtrequest_scoped_locked(req
, dst
, gateway
, netmask
, flags
,
2312 lck_mtx_unlock(rnh_lock
);
2317 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
2318 * (i.e., the routes related to it by the operation of cloning). This
2319 * routine is iterated over all potential former-child-routes by way of
2320 * rnh->rnh_walktree_from() above, and those that actually are children of
2321 * the late parent (passed in as VP here) are themselves deleted.
2324 rt_fixdelete(struct radix_node
*rn
, void *vp
)
2326 struct rtentry
*rt
= (struct rtentry
*)rn
;
2327 struct rtentry
*rt0
= vp
;
2329 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2332 if (rt
->rt_parent
== rt0
&&
2333 !(rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
))) {
2335 * Safe to drop rt_lock and use rt_key, since holding
2336 * rnh_lock here prevents another thread from calling
2337 * rt_setgate() on this route.
2340 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), NULL
,
2341 rt_mask(rt
), rt
->rt_flags
, NULL
));
2348 * This routine is called from rt_setgate() to do the analogous thing for
2349 * adds and changes. There is the added complication in this case of a
2350 * middle insert; i.e., insertion of a new network route between an older
2351 * network route and (cloned) host routes. For this reason, a simple check
2352 * of rt->rt_parent is insufficient; each candidate route must be tested
2353 * against the (mask, value) of the new route (passed as before in vp)
2354 * to see if the new route matches it.
2356 * XXX - it may be possible to do fixdelete() for changes and reserve this
2357 * routine just for adds. I'm not sure why I thought it was necessary to do
2361 rt_fixchange(struct radix_node
*rn
, void *vp
)
2363 struct rtentry
*rt
= (struct rtentry
*)rn
;
2364 struct rtfc_arg
*ap
= vp
;
2365 struct rtentry
*rt0
= ap
->rt0
;
2366 struct radix_node_head
*rnh
= ap
->rnh
;
2367 u_char
*xk1
, *xm1
, *xk2
, *xmp
;
2370 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2374 if (!rt
->rt_parent
||
2375 (rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
))) {
2380 if (rt
->rt_parent
== rt0
)
2384 * There probably is a function somewhere which does this...
2385 * if not, there should be.
2387 len
= imin(rt_key(rt0
)->sa_len
, rt_key(rt
)->sa_len
);
2389 xk1
= (u_char
*)rt_key(rt0
);
2390 xm1
= (u_char
*)rt_mask(rt0
);
2391 xk2
= (u_char
*)rt_key(rt
);
2394 * Avoid applying a less specific route; do this only if the parent
2395 * route (rt->rt_parent) is a network route, since otherwise its mask
2396 * will be NULL if it is a cloning host route.
2398 if ((xmp
= (u_char
*)rt_mask(rt
->rt_parent
)) != NULL
) {
2399 int mlen
= rt_mask(rt
->rt_parent
)->sa_len
;
2400 if (mlen
> rt_mask(rt0
)->sa_len
) {
2405 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< mlen
; i
++) {
2406 if ((xmp
[i
] & ~(xmp
[i
] ^ xm1
[i
])) != xmp
[i
]) {
2413 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< len
; i
++) {
2414 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
]) {
2421 * OK, this node is a clone, and matches the node currently being
2422 * changed/added under the node's mask. So, get rid of it.
2426 * Safe to drop rt_lock and use rt_key, since holding rnh_lock here
2427 * prevents another thread from calling rt_setgate() on this route.
2430 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), NULL
,
2431 rt_mask(rt
), rt
->rt_flags
, NULL
));
2435 * Round up sockaddr len to multiples of 32-bytes. This will reduce
2436 * or even eliminate the need to re-allocate the chunk of memory used
2437 * for rt_key and rt_gateway in the event the gateway portion changes.
2438 * Certain code paths (e.g. IPSec) are notorious for caching the address
2439 * of rt_gateway; this rounding-up would help ensure that the gateway
2440 * portion never gets deallocated (though it may change contents) and
2441 * thus greatly simplifies things.
2443 #define SA_SIZE(x) (-(-((uintptr_t)(x)) & -(32)))
2446 * Sets the gateway and/or gateway route portion of a route; may be
2447 * called on an existing route to modify the gateway portion. Both
2448 * rt_key and rt_gateway are allocated out of the same memory chunk.
2449 * Route entry lock must be held by caller; this routine will return
2450 * with the lock held.
2453 rt_setgate(struct rtentry
*rt
, struct sockaddr
*dst
, struct sockaddr
*gate
)
2455 int dlen
= SA_SIZE(dst
->sa_len
), glen
= SA_SIZE(gate
->sa_len
);
2456 struct radix_node_head
*rnh
= NULL
;
2457 boolean_t loop
= FALSE
;
2459 if (dst
->sa_family
!= AF_INET
&& dst
->sa_family
!= AF_INET6
) {
2463 rnh
= rt_tables
[dst
->sa_family
];
2464 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2465 RT_LOCK_ASSERT_HELD(rt
);
2468 * If this is for a route that is on its way of being removed,
2469 * or is temporarily frozen, reject the modification request.
2471 if (rt
->rt_flags
& RTF_CONDEMNED
) {
2475 /* Add an extra ref for ourselves */
2476 RT_ADDREF_LOCKED(rt
);
2478 if (rt
->rt_flags
& RTF_GATEWAY
) {
2479 if ((dst
->sa_len
== gate
->sa_len
) &&
2480 (dst
->sa_family
== AF_INET
|| dst
->sa_family
== AF_INET6
)) {
2481 struct sockaddr_storage dst_ss
, gate_ss
;
2483 (void) sa_copy(dst
, &dst_ss
, NULL
);
2484 (void) sa_copy(gate
, &gate_ss
, NULL
);
2486 loop
= equal(SA(&dst_ss
), SA(&gate_ss
));
2488 loop
= (dst
->sa_len
== gate
->sa_len
&&
2494 * A (cloning) network route with the destination equal to the gateway
2495 * will create an endless loop (see notes below), so disallow it.
2497 if (((rt
->rt_flags
& (RTF_HOST
|RTF_GATEWAY
|RTF_LLINFO
)) ==
2498 RTF_GATEWAY
) && loop
) {
2499 /* Release extra ref */
2500 RT_REMREF_LOCKED(rt
);
2501 return (EADDRNOTAVAIL
);
2505 * A host route with the destination equal to the gateway
2506 * will interfere with keeping LLINFO in the routing
2507 * table, so disallow it.
2509 if (((rt
->rt_flags
& (RTF_HOST
|RTF_GATEWAY
|RTF_LLINFO
)) ==
2510 (RTF_HOST
|RTF_GATEWAY
)) && loop
) {
2512 * The route might already exist if this is an RTM_CHANGE
2513 * or a routing redirect, so try to delete it.
2515 if (rt_key(rt
) != NULL
) {
2517 * Safe to drop rt_lock and use rt_key, rt_gateway,
2518 * since holding rnh_lock here prevents another thread
2519 * from calling rt_setgate() on this route.
2522 (void) rtrequest_locked(RTM_DELETE
, rt_key(rt
),
2523 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
2526 /* Release extra ref */
2527 RT_REMREF_LOCKED(rt
);
2528 return (EADDRNOTAVAIL
);
2532 * The destination is not directly reachable. Get a route
2533 * to the next-hop gateway and store it in rt_gwroute.
2535 if (rt
->rt_flags
& RTF_GATEWAY
) {
2536 struct rtentry
*gwrt
;
2537 unsigned int ifscope
;
2539 if (dst
->sa_family
== AF_INET
)
2540 ifscope
= sin_get_ifscope(dst
);
2541 else if (dst
->sa_family
== AF_INET6
)
2542 ifscope
= sin6_get_ifscope(dst
);
2544 ifscope
= IFSCOPE_NONE
;
2548 * Don't ignore RTF_CLONING, since we prefer that rt_gwroute
2549 * points to a clone rather than a cloning route; see above
2550 * check for cloning loop avoidance (dst == gate).
2552 gwrt
= rtalloc1_scoped_locked(gate
, 1, RTF_PRCLONING
, ifscope
);
2554 RT_LOCK_ASSERT_NOTHELD(gwrt
);
2558 * Cloning loop avoidance:
2560 * In the presence of protocol-cloning and bad configuration,
2561 * it is possible to get stuck in bottomless mutual recursion
2562 * (rtrequest rt_setgate rtalloc1). We avoid this by not
2563 * allowing protocol-cloning to operate for gateways (which
2564 * is probably the correct choice anyway), and avoid the
2565 * resulting reference loops by disallowing any route to run
2566 * through itself as a gateway. This is obviously mandatory
2567 * when we get rt->rt_output(). It implies that a route to
2568 * the gateway must already be present in the system in order
2569 * for the gateway to be referred to by another route.
2572 RT_REMREF_LOCKED(gwrt
);
2573 /* Release extra ref */
2574 RT_REMREF_LOCKED(rt
);
2575 return (EADDRINUSE
); /* failure */
2579 * If scoped, the gateway route must use the same interface;
2580 * we're holding rnh_lock now, so rt_gateway and rt_ifp of gwrt
2581 * should not change and are freely accessible.
2583 if (ifscope
!= IFSCOPE_NONE
&& (rt
->rt_flags
& RTF_IFSCOPE
) &&
2584 gwrt
!= NULL
&& gwrt
->rt_ifp
!= NULL
&&
2585 gwrt
->rt_ifp
->if_index
!= ifscope
) {
2586 rtfree_locked(gwrt
); /* rt != gwrt, no deadlock */
2587 /* Release extra ref */
2588 RT_REMREF_LOCKED(rt
);
2589 return ((rt
->rt_flags
& RTF_HOST
) ?
2590 EHOSTUNREACH
: ENETUNREACH
);
2593 /* Check again since we dropped the lock above */
2594 if (rt
->rt_flags
& RTF_CONDEMNED
) {
2596 rtfree_locked(gwrt
);
2597 /* Release extra ref */
2598 RT_REMREF_LOCKED(rt
);
2602 /* Set gateway route; callee adds ref to gwrt if non-NULL */
2603 rt_set_gwroute(rt
, dst
, gwrt
);
2606 * In case the (non-scoped) default route gets modified via
2607 * an ICMP redirect, record the interface index used for the
2608 * primary ifscope. Also done in rt_setif() to take care
2609 * of the non-redirect cases.
2611 if (rt_primary_default(rt
, dst
) && rt
->rt_ifp
!= NULL
) {
2612 set_primary_ifscope(dst
->sa_family
,
2613 rt
->rt_ifp
->if_index
);
2618 * If this is a change in a default route, update
2619 * necp client watchers to re-evaluate
2621 if (SA_DEFAULT(dst
)) {
2622 necp_update_all_clients();
2627 * Tell the kernel debugger about the new default gateway
2628 * if the gateway route uses the primary interface, or
2629 * if we are in a transient state before the non-scoped
2630 * default gateway is installed (similar to how the system
2631 * was behaving in the past). In future, it would be good
2632 * to do all this only when KDP is enabled.
2634 if ((dst
->sa_family
== AF_INET
) &&
2635 gwrt
!= NULL
&& gwrt
->rt_gateway
->sa_family
== AF_LINK
&&
2636 (gwrt
->rt_ifp
->if_index
== get_primary_ifscope(AF_INET
) ||
2637 get_primary_ifscope(AF_INET
) == IFSCOPE_NONE
)) {
2638 kdp_set_gateway_mac(SDL((void *)gwrt
->rt_gateway
)->
2642 /* Release extra ref from rtalloc1() */
2648 * Prepare to store the gateway in rt_gateway. Both dst and gateway
2649 * are stored one after the other in the same malloc'd chunk. If we
2650 * have room, reuse the old buffer since rt_gateway already points
2651 * to the right place. Otherwise, malloc a new block and update
2652 * the 'dst' address and point rt_gateway to the right place.
2654 if (rt
->rt_gateway
== NULL
|| glen
> SA_SIZE(rt
->rt_gateway
->sa_len
)) {
2657 /* The underlying allocation is done with M_WAITOK set */
2658 R_Malloc(new, caddr_t
, dlen
+ glen
);
2660 /* Clear gateway route */
2661 rt_set_gwroute(rt
, dst
, NULL
);
2662 /* Release extra ref */
2663 RT_REMREF_LOCKED(rt
);
2668 * Copy from 'dst' and not rt_key(rt) because we can get
2669 * here to initialize a newly allocated route entry, in
2670 * which case rt_key(rt) is NULL (and so does rt_gateway).
2672 bzero(new, dlen
+ glen
);
2673 Bcopy(dst
, new, dst
->sa_len
);
2674 R_Free(rt_key(rt
)); /* free old block; NULL is okay */
2675 rt
->rt_nodes
->rn_key
= new;
2676 rt
->rt_gateway
= (struct sockaddr
*)(new + dlen
);
2680 * Copy the new gateway value into the memory chunk.
2682 Bcopy(gate
, rt
->rt_gateway
, gate
->sa_len
);
2685 * For consistency between rt_gateway and rt_key(gwrt).
2687 if ((rt
->rt_flags
& RTF_GATEWAY
) && rt
->rt_gwroute
!= NULL
&&
2688 (rt
->rt_gwroute
->rt_flags
& RTF_IFSCOPE
)) {
2689 if (rt
->rt_gateway
->sa_family
== AF_INET
&&
2690 rt_key(rt
->rt_gwroute
)->sa_family
== AF_INET
) {
2691 sin_set_ifscope(rt
->rt_gateway
,
2692 sin_get_ifscope(rt_key(rt
->rt_gwroute
)));
2693 } else if (rt
->rt_gateway
->sa_family
== AF_INET6
&&
2694 rt_key(rt
->rt_gwroute
)->sa_family
== AF_INET6
) {
2695 sin6_set_ifscope(rt
->rt_gateway
,
2696 sin6_get_ifscope(rt_key(rt
->rt_gwroute
)));
2701 * This isn't going to do anything useful for host routes, so
2702 * don't bother. Also make sure we have a reasonable mask
2703 * (we don't yet have one during adds).
2705 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != 0) {
2706 struct rtfc_arg arg
;
2710 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
2711 rt_fixchange
, &arg
);
2715 /* Release extra ref */
2716 RT_REMREF_LOCKED(rt
);
2723 rt_set_gwroute(struct rtentry
*rt
, struct sockaddr
*dst
, struct rtentry
*gwrt
)
2725 boolean_t gwrt_isrouter
;
2727 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2728 RT_LOCK_ASSERT_HELD(rt
);
2731 RT_ADDREF(gwrt
); /* for this routine */
2734 * Get rid of existing gateway route; if rt_gwroute is already
2735 * set to gwrt, this is slightly redundant (though safe since
2736 * we held an extra ref above) but makes the code simpler.
2738 if (rt
->rt_gwroute
!= NULL
) {
2739 struct rtentry
*ogwrt
= rt
->rt_gwroute
;
2741 VERIFY(rt
!= ogwrt
); /* sanity check */
2742 rt
->rt_gwroute
= NULL
;
2744 rtfree_locked(ogwrt
);
2746 VERIFY(rt
->rt_gwroute
== NULL
);
2750 * And associate the new gateway route.
2752 if ((rt
->rt_gwroute
= gwrt
) != NULL
) {
2753 RT_ADDREF(gwrt
); /* for rt */
2755 if (rt
->rt_flags
& RTF_WASCLONED
) {
2756 /* rt_parent might be NULL if rt is embryonic */
2757 gwrt_isrouter
= (rt
->rt_parent
!= NULL
&&
2758 SA_DEFAULT(rt_key(rt
->rt_parent
)) &&
2759 !RT_HOST(rt
->rt_parent
));
2761 gwrt_isrouter
= (SA_DEFAULT(dst
) && !RT_HOST(rt
));
2764 /* If gwrt points to a default router, mark it accordingly */
2765 if (gwrt_isrouter
&& RT_HOST(gwrt
) &&
2766 !(gwrt
->rt_flags
& RTF_ROUTER
)) {
2768 gwrt
->rt_flags
|= RTF_ROUTER
;
2772 RT_REMREF(gwrt
); /* for this routine */
2777 rt_maskedcopy(const struct sockaddr
*src
, struct sockaddr
*dst
,
2778 const struct sockaddr
*netmask
)
2780 const char *netmaskp
= &netmask
->sa_data
[0];
2781 const char *srcp
= &src
->sa_data
[0];
2782 char *dstp
= &dst
->sa_data
[0];
2783 const char *maskend
= (char *)dst
2784 + MIN(netmask
->sa_len
, src
->sa_len
);
2785 const char *srcend
= (char *)dst
+ src
->sa_len
;
2787 dst
->sa_len
= src
->sa_len
;
2788 dst
->sa_family
= src
->sa_family
;
2790 while (dstp
< maskend
)
2791 *dstp
++ = *srcp
++ & *netmaskp
++;
2793 memset(dstp
, 0, (size_t)(srcend
- dstp
));
2797 * Lookup an AF_INET/AF_INET6 scoped or non-scoped route depending on the
2798 * ifscope value passed in by the caller (IFSCOPE_NONE implies non-scoped).
2800 static struct radix_node
*
2801 node_lookup(struct sockaddr
*dst
, struct sockaddr
*netmask
,
2802 unsigned int ifscope
)
2804 struct radix_node_head
*rnh
;
2805 struct radix_node
*rn
;
2806 struct sockaddr_storage ss
, mask
;
2807 int af
= dst
->sa_family
;
2808 struct matchleaf_arg ma
= { ifscope
};
2809 rn_matchf_t
*f
= rn_match_ifscope
;
2812 if (af
!= AF_INET
&& af
!= AF_INET6
)
2815 rnh
= rt_tables
[af
];
2818 * Transform dst into the internal routing table form,
2819 * clearing out the scope ID field if ifscope isn't set.
2821 dst
= sa_copy(dst
, &ss
, (ifscope
== IFSCOPE_NONE
) ? NULL
: &ifscope
);
2823 /* Transform netmask into the internal routing table form */
2824 if (netmask
!= NULL
)
2825 netmask
= ma_copy(af
, netmask
, &mask
, ifscope
);
2827 if (ifscope
== IFSCOPE_NONE
)
2830 rn
= rnh
->rnh_lookup_args(dst
, netmask
, rnh
, f
, w
);
2831 if (rn
!= NULL
&& (rn
->rn_flags
& RNF_ROOT
))
2838 * Lookup the AF_INET/AF_INET6 non-scoped default route.
2840 static struct radix_node
*
2841 node_lookup_default(int af
)
2843 struct radix_node_head
*rnh
;
2845 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
2846 rnh
= rt_tables
[af
];
2848 return (af
== AF_INET
? rnh
->rnh_lookup(&sin_def
, NULL
, rnh
) :
2849 rnh
->rnh_lookup(&sin6_def
, NULL
, rnh
));
2853 rt_ifa_is_dst(struct sockaddr
*dst
, struct ifaddr
*ifa
)
2855 boolean_t result
= FALSE
;
2857 if (ifa
== NULL
|| ifa
->ifa_addr
== NULL
)
2862 if (dst
->sa_family
== ifa
->ifa_addr
->sa_family
&&
2863 ((dst
->sa_family
== AF_INET
&&
2864 SIN(dst
)->sin_addr
.s_addr
==
2865 SIN(ifa
->ifa_addr
)->sin_addr
.s_addr
) ||
2866 (dst
->sa_family
== AF_INET6
&&
2867 SA6_ARE_ADDR_EQUAL(SIN6(dst
), SIN6(ifa
->ifa_addr
)))))
2876 * Common routine to lookup/match a route. It invokes the lookup/matchaddr
2877 * callback which could be address family-specific. The main difference
2878 * between the two (at least for AF_INET/AF_INET6) is that a lookup does
2879 * not alter the expiring state of a route, whereas a match would unexpire
2880 * or revalidate the route.
2882 * The optional scope or interface index property of a route allows for a
2883 * per-interface route instance. This permits multiple route entries having
2884 * the same destination (but not necessarily the same gateway) to exist in
2885 * the routing table; each of these entries is specific to the corresponding
2886 * interface. This is made possible by storing the scope ID value into the
2887 * radix key, thus making each route entry unique. These scoped entries
2888 * exist along with the regular, non-scoped entries in the same radix tree
2889 * for a given address family (AF_INET/AF_INET6); the scope logically
2890 * partitions it into multiple per-interface sub-trees.
2892 * When a scoped route lookup is performed, the routing table is searched for
2893 * the best match that would result in a route using the same interface as the
2894 * one associated with the scope (the exception to this are routes that point
2895 * to the loopback interface). The search rule follows the longest matching
2896 * prefix with the additional interface constraint.
2898 static struct rtentry
*
2899 rt_lookup_common(boolean_t lookup_only
, boolean_t coarse
, struct sockaddr
*dst
,
2900 struct sockaddr
*netmask
, struct radix_node_head
*rnh
, unsigned int ifscope
)
2902 struct radix_node
*rn0
, *rn
= NULL
;
2903 int af
= dst
->sa_family
;
2904 struct sockaddr_storage dst_ss
;
2905 struct sockaddr_storage mask_ss
;
2907 #if (DEVELOPMENT || DEBUG)
2908 char dbuf
[MAX_SCOPE_ADDR_STR_LEN
], gbuf
[MAX_IPv6_STR_LEN
];
2909 char s_dst
[MAX_IPv6_STR_LEN
], s_netmask
[MAX_IPv6_STR_LEN
];
2911 VERIFY(!coarse
|| ifscope
== IFSCOPE_NONE
);
2913 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2916 * While we have rnh_lock held, see if we need to schedule the timer.
2918 if (nd6_sched_timeout_want
)
2919 nd6_sched_timeout(NULL
, NULL
);
2926 * Non-scoped route lookup.
2929 if (af
!= AF_INET
&& af
!= AF_INET6
) {
2931 if (af
!= AF_INET
) {
2933 rn
= rnh
->rnh_matchaddr(dst
, rnh
);
2936 * Don't return a root node; also, rnh_matchaddr callback
2937 * would have done the necessary work to clear RTPRF_OURS
2938 * for certain protocol families.
2940 if (rn
!= NULL
&& (rn
->rn_flags
& RNF_ROOT
))
2943 RT_LOCK_SPIN(RT(rn
));
2944 if (!(RT(rn
)->rt_flags
& RTF_CONDEMNED
)) {
2945 RT_ADDREF_LOCKED(RT(rn
));
2955 /* Transform dst/netmask into the internal routing table form */
2956 dst
= sa_copy(dst
, &dst_ss
, &ifscope
);
2957 if (netmask
!= NULL
)
2958 netmask
= ma_copy(af
, netmask
, &mask_ss
, ifscope
);
2959 dontcare
= (ifscope
== IFSCOPE_NONE
);
2961 #if (DEVELOPMENT || DEBUG)
2964 (void) inet_ntop(af
, &SIN(dst
)->sin_addr
.s_addr
,
2965 s_dst
, sizeof (s_dst
));
2967 (void) inet_ntop(af
, &SIN6(dst
)->sin6_addr
,
2968 s_dst
, sizeof (s_dst
));
2970 if (netmask
!= NULL
&& af
== AF_INET
)
2971 (void) inet_ntop(af
, &SIN(netmask
)->sin_addr
.s_addr
,
2972 s_netmask
, sizeof (s_netmask
));
2973 if (netmask
!= NULL
&& af
== AF_INET6
)
2974 (void) inet_ntop(af
, &SIN6(netmask
)->sin6_addr
,
2975 s_netmask
, sizeof (s_netmask
));
2978 printf("%s (%d, %d, %s, %s, %u)\n",
2979 __func__
, lookup_only
, coarse
, s_dst
, s_netmask
, ifscope
);
2984 * Scoped route lookup:
2986 * We first perform a non-scoped lookup for the original result.
2987 * Afterwards, depending on whether or not the caller has specified
2988 * a scope, we perform a more specific scoped search and fallback
2989 * to this original result upon failure.
2991 rn0
= rn
= node_lookup(dst
, netmask
, IFSCOPE_NONE
);
2994 * If the caller did not specify a scope, use the primary scope
2995 * derived from the system's non-scoped default route. If, for
2996 * any reason, there is no primary interface, ifscope will be
2997 * set to IFSCOPE_NONE; if the above lookup resulted in a route,
2998 * we'll do a more-specific search below, scoped to the interface
3002 ifscope
= get_primary_ifscope(af
);
3005 * Keep the original result if either of the following is true:
3007 * 1) The interface portion of the route has the same interface
3008 * index as the scope value and it is marked with RTF_IFSCOPE.
3009 * 2) The route uses the loopback interface, in which case the
3010 * destination (host/net) is local/loopback.
3012 * Otherwise, do a more specified search using the scope;
3013 * we're holding rnh_lock now, so rt_ifp should not change.
3016 struct rtentry
*rt
= RT(rn
);
3017 #if (DEVELOPMENT || DEBUG)
3019 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
3020 printf("%s unscoped search %p to %s->%s->%s ifa_ifp %s\n",
3023 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
3024 (rt
->rt_ifa
->ifa_ifp
!= NULL
) ?
3025 rt
->rt_ifa
->ifa_ifp
->if_xname
: "");
3028 if (!(rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) ||
3029 (rt
->rt_flags
& RTF_GATEWAY
)) {
3030 if (rt
->rt_ifp
->if_index
!= ifscope
) {
3032 * Wrong interface; keep the original result
3033 * only if the caller did not specify a scope,
3034 * and do a more specific scoped search using
3035 * the scope of the found route. Otherwise,
3036 * start again from scratch.
3038 * For loopback scope we keep the unscoped
3039 * route for local addresses
3043 ifscope
= rt
->rt_ifp
->if_index
;
3044 else if (ifscope
!= lo_ifp
->if_index
||
3045 rt_ifa_is_dst(dst
, rt
->rt_ifa
) == FALSE
)
3047 } else if (!(rt
->rt_flags
& RTF_IFSCOPE
)) {
3049 * Right interface, except that this route
3050 * isn't marked with RTF_IFSCOPE. Do a more
3051 * specific scoped search. Keep the original
3052 * result and return it it in case the scoped
3061 * Scoped search. Find the most specific entry having the same
3062 * interface scope as the one requested. The following will result
3063 * in searching for the longest prefix scoped match.
3066 rn
= node_lookup(dst
, netmask
, ifscope
);
3067 #if (DEVELOPMENT || DEBUG)
3068 if (rt_verbose
&& rn
!= NULL
) {
3069 struct rtentry
*rt
= RT(rn
);
3071 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
3072 printf("%s scoped search %p to %s->%s->%s ifa %s\n",
3075 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
3076 (rt
->rt_ifa
->ifa_ifp
!= NULL
) ?
3077 rt
->rt_ifa
->ifa_ifp
->if_xname
: "");
3082 * Use the original result if either of the following is true:
3084 * 1) The scoped search did not yield any result.
3085 * 2) The caller insists on performing a coarse-grained lookup.
3086 * 3) The result from the scoped search is a scoped default route,
3087 * and the original (non-scoped) result is not a default route,
3088 * i.e. the original result is a more specific host/net route.
3089 * 4) The scoped search yielded a net route but the original
3090 * result is a host route, i.e. the original result is treated
3091 * as a more specific route.
3093 if (rn
== NULL
|| coarse
|| (rn0
!= NULL
&&
3094 ((SA_DEFAULT(rt_key(RT(rn
))) && !SA_DEFAULT(rt_key(RT(rn0
)))) ||
3095 (!RT_HOST(rn
) && RT_HOST(rn0
)))))
3099 * If we still don't have a route, use the non-scoped default
3100 * route as long as the interface portion satistifes the scope.
3102 if (rn
== NULL
&& (rn
= node_lookup_default(af
)) != NULL
&&
3103 RT(rn
)->rt_ifp
->if_index
!= ifscope
) {
3109 * Manually clear RTPRF_OURS using rt_validate() and
3110 * bump up the reference count after, and not before;
3111 * we only get here for AF_INET/AF_INET6. node_lookup()
3112 * has done the check against RNF_ROOT, so we can be sure
3113 * that we're not returning a root node here.
3115 RT_LOCK_SPIN(RT(rn
));
3116 if (rt_validate(RT(rn
))) {
3117 RT_ADDREF_LOCKED(RT(rn
));
3124 #if (DEVELOPMENT || DEBUG)
3127 printf("%s %u return NULL\n", __func__
, ifscope
);
3129 struct rtentry
*rt
= RT(rn
);
3131 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
3133 printf("%s %u return %p to %s->%s->%s ifa_ifp %s\n",
3134 __func__
, ifscope
, rt
,
3136 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
3137 (rt
->rt_ifa
->ifa_ifp
!= NULL
) ?
3138 rt
->rt_ifa
->ifa_ifp
->if_xname
: "");
3146 rt_lookup(boolean_t lookup_only
, struct sockaddr
*dst
, struct sockaddr
*netmask
,
3147 struct radix_node_head
*rnh
, unsigned int ifscope
)
3149 return (rt_lookup_common(lookup_only
, FALSE
, dst
, netmask
,
3154 rt_lookup_coarse(boolean_t lookup_only
, struct sockaddr
*dst
,
3155 struct sockaddr
*netmask
, struct radix_node_head
*rnh
)
3157 return (rt_lookup_common(lookup_only
, TRUE
, dst
, netmask
,
3158 rnh
, IFSCOPE_NONE
));
3162 rt_validate(struct rtentry
*rt
)
3164 RT_LOCK_ASSERT_HELD(rt
);
3166 if ((rt
->rt_flags
& (RTF_UP
| RTF_CONDEMNED
)) == RTF_UP
) {
3167 int af
= rt_key(rt
)->sa_family
;
3170 (void) in_validate(RN(rt
));
3171 else if (af
== AF_INET6
)
3172 (void) in6_validate(RN(rt
));
3177 return (rt
!= NULL
);
3181 * Set up a routing table entry, normally
3185 rtinit(struct ifaddr
*ifa
, int cmd
, int flags
)
3189 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
3191 lck_mtx_lock(rnh_lock
);
3192 error
= rtinit_locked(ifa
, cmd
, flags
);
3193 lck_mtx_unlock(rnh_lock
);
3199 rtinit_locked(struct ifaddr
*ifa
, int cmd
, int flags
)
3201 struct radix_node_head
*rnh
;
3202 uint8_t nbuf
[128]; /* long enough for IPv6 */
3203 #if (DEVELOPMENT || DEBUG)
3204 char dbuf
[MAX_IPv6_STR_LEN
], gbuf
[MAX_IPv6_STR_LEN
];
3205 char abuf
[MAX_IPv6_STR_LEN
];
3207 struct rtentry
*rt
= NULL
;
3208 struct sockaddr
*dst
;
3209 struct sockaddr
*netmask
;
3213 * Holding rnh_lock here prevents the possibility of ifa from
3214 * changing (e.g. in_ifinit), so it is safe to access its
3215 * ifa_{dst}addr (here and down below) without locking.
3217 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
3219 if (flags
& RTF_HOST
) {
3220 dst
= ifa
->ifa_dstaddr
;
3223 dst
= ifa
->ifa_addr
;
3224 netmask
= ifa
->ifa_netmask
;
3227 if (dst
->sa_len
== 0) {
3228 log(LOG_ERR
, "%s: %s failed, invalid dst sa_len %d\n",
3229 __func__
, rtm2str(cmd
), dst
->sa_len
);
3233 if (netmask
!= NULL
&& netmask
->sa_len
> sizeof (nbuf
)) {
3234 log(LOG_ERR
, "%s: %s failed, mask sa_len %d too large\n",
3235 __func__
, rtm2str(cmd
), dst
->sa_len
);
3240 #if (DEVELOPMENT || DEBUG)
3241 if (dst
->sa_family
== AF_INET
) {
3242 (void) inet_ntop(AF_INET
, &SIN(dst
)->sin_addr
.s_addr
,
3243 abuf
, sizeof (abuf
));
3246 else if (dst
->sa_family
== AF_INET6
) {
3247 (void) inet_ntop(AF_INET6
, &SIN6(dst
)->sin6_addr
,
3248 abuf
, sizeof (abuf
));
3251 #endif /* (DEVELOPMENT || DEBUG) */
3253 if ((rnh
= rt_tables
[dst
->sa_family
]) == NULL
) {
3259 * If it's a delete, check that if it exists, it's on the correct
3260 * interface or we might scrub a route to another ifa which would
3261 * be confusing at best and possibly worse.
3263 if (cmd
== RTM_DELETE
) {
3265 * It's a delete, so it should already exist..
3266 * If it's a net, mask off the host bits
3267 * (Assuming we have a mask)
3269 if (netmask
!= NULL
) {
3270 rt_maskedcopy(dst
, SA(nbuf
), netmask
);
3274 * Get an rtentry that is in the routing tree and contains
3275 * the correct info. Note that we perform a coarse-grained
3276 * lookup here, in case there is a scoped variant of the
3277 * subnet/prefix route which we should ignore, as we never
3278 * add a scoped subnet/prefix route as part of adding an
3279 * interface address.
3281 rt
= rt_lookup_coarse(TRUE
, dst
, NULL
, rnh
);
3283 #if (DEVELOPMENT || DEBUG)
3284 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
3287 * Ok so we found the rtentry. it has an extra reference
3288 * for us at this stage. we won't need that so
3292 if (rt
->rt_ifa
!= ifa
) {
3294 * If the interface address in the rtentry
3295 * doesn't match the interface we are using,
3296 * then we don't want to delete it, so return
3297 * an error. This seems to be the only point
3298 * of this whole RTM_DELETE clause.
3300 #if (DEVELOPMENT || DEBUG)
3302 log(LOG_DEBUG
, "%s: not removing "
3303 "route to %s->%s->%s, flags %b, "
3304 "ifaddr %s, rt_ifa 0x%llx != "
3305 "ifa 0x%llx\n", __func__
, dbuf
,
3306 gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3307 rt
->rt_ifp
->if_xname
: ""),
3308 rt
->rt_flags
, RTF_BITS
, abuf
,
3309 (uint64_t)VM_KERNEL_ADDRPERM(
3311 (uint64_t)VM_KERNEL_ADDRPERM(ifa
));
3313 #endif /* (DEVELOPMENT || DEBUG) */
3314 RT_REMREF_LOCKED(rt
);
3317 error
= ((flags
& RTF_HOST
) ?
3318 EHOSTUNREACH
: ENETUNREACH
);
3320 } else if (rt
->rt_flags
& RTF_STATIC
) {
3322 * Don't remove the subnet/prefix route if
3323 * this was manually added from above.
3325 #if (DEVELOPMENT || DEBUG)
3327 log(LOG_DEBUG
, "%s: not removing "
3328 "static route to %s->%s->%s, "
3329 "flags %b, ifaddr %s\n", __func__
,
3330 dbuf
, gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3331 rt
->rt_ifp
->if_xname
: ""),
3332 rt
->rt_flags
, RTF_BITS
, abuf
);
3334 #endif /* (DEVELOPMENT || DEBUG) */
3335 RT_REMREF_LOCKED(rt
);
3341 #if (DEVELOPMENT || DEBUG)
3343 log(LOG_DEBUG
, "%s: removing route to "
3344 "%s->%s->%s, flags %b, ifaddr %s\n",
3345 __func__
, dbuf
, gbuf
,
3346 ((rt
->rt_ifp
!= NULL
) ?
3347 rt
->rt_ifp
->if_xname
: ""),
3348 rt
->rt_flags
, RTF_BITS
, abuf
);
3350 #endif /* (DEVELOPMENT || DEBUG) */
3351 RT_REMREF_LOCKED(rt
);
3357 * Do the actual request
3359 if ((error
= rtrequest_locked(cmd
, dst
, ifa
->ifa_addr
, netmask
,
3360 flags
| ifa
->ifa_flags
, &rt
)) != 0)
3364 #if (DEVELOPMENT || DEBUG)
3365 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
3366 #endif /* (DEVELOPMENT || DEBUG) */
3370 * If we are deleting, and we found an entry, then it's
3371 * been removed from the tree. Notify any listening
3372 * routing agents of the change and throw it away.
3375 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
3377 #if (DEVELOPMENT || DEBUG)
3379 log(LOG_DEBUG
, "%s: removed route to %s->%s->%s, "
3380 "flags %b, ifaddr %s\n", __func__
, dbuf
, gbuf
,
3381 ((rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: ""),
3382 rt
->rt_flags
, RTF_BITS
, abuf
);
3384 #endif /* (DEVELOPMENT || DEBUG) */
3390 * We are adding, and we have a returned routing entry.
3391 * We need to sanity check the result. If it came back
3392 * with an unexpected interface, then it must have already
3393 * existed or something.
3396 if (rt
->rt_ifa
!= ifa
) {
3397 void (*ifa_rtrequest
)
3398 (int, struct rtentry
*, struct sockaddr
*);
3399 #if (DEVELOPMENT || DEBUG)
3401 if (!(rt
->rt_ifa
->ifa_ifp
->if_flags
&
3402 (IFF_POINTOPOINT
|IFF_LOOPBACK
))) {
3403 log(LOG_ERR
, "%s: %s route to %s->%s->%s, "
3404 "flags %b, ifaddr %s, rt_ifa 0x%llx != "
3405 "ifa 0x%llx\n", __func__
, rtm2str(cmd
),
3406 dbuf
, gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3407 rt
->rt_ifp
->if_xname
: ""), rt
->rt_flags
,
3409 (uint64_t)VM_KERNEL_ADDRPERM(rt
->rt_ifa
),
3410 (uint64_t)VM_KERNEL_ADDRPERM(ifa
));
3413 log(LOG_DEBUG
, "%s: %s route to %s->%s->%s, "
3414 "flags %b, ifaddr %s, rt_ifa was 0x%llx "
3415 "now 0x%llx\n", __func__
, rtm2str(cmd
),
3416 dbuf
, gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3417 rt
->rt_ifp
->if_xname
: ""), rt
->rt_flags
,
3419 (uint64_t)VM_KERNEL_ADDRPERM(rt
->rt_ifa
),
3420 (uint64_t)VM_KERNEL_ADDRPERM(ifa
));
3422 #endif /* (DEVELOPMENT || DEBUG) */
3425 * Ask that the protocol in question
3426 * remove anything it has associated with
3427 * this route and ifaddr.
3429 ifa_rtrequest
= rt
->rt_ifa
->ifa_rtrequest
;
3430 if (ifa_rtrequest
!= NULL
)
3431 ifa_rtrequest(RTM_DELETE
, rt
, NULL
);
3433 * Set the route's ifa.
3437 if (rt
->rt_ifp
!= ifa
->ifa_ifp
) {
3439 * Purge any link-layer info caching.
3441 if (rt
->rt_llinfo_purge
!= NULL
)
3442 rt
->rt_llinfo_purge(rt
);
3444 * Adjust route ref count for the interfaces.
3446 if (rt
->rt_if_ref_fn
!= NULL
) {
3447 rt
->rt_if_ref_fn(ifa
->ifa_ifp
, 1);
3448 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
3453 * And substitute in references to the ifaddr
3456 rt
->rt_ifp
= ifa
->ifa_ifp
;
3458 * If rmx_mtu is not locked, update it
3459 * to the MTU used by the new interface.
3461 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
3462 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
3465 * Now ask the protocol to check if it needs
3466 * any special processing in its new form.
3468 ifa_rtrequest
= ifa
->ifa_rtrequest
;
3469 if (ifa_rtrequest
!= NULL
)
3470 ifa_rtrequest(RTM_ADD
, rt
, NULL
);
3472 #if (DEVELOPMENT || DEBUG)
3474 log(LOG_DEBUG
, "%s: added route to %s->%s->%s, "
3475 "flags %b, ifaddr %s\n", __func__
, dbuf
,
3476 gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3477 rt
->rt_ifp
->if_xname
: ""), rt
->rt_flags
,
3480 #endif /* (DEVELOPMENT || DEBUG) */
3483 * notify any listenning routing agents of the change
3485 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
3487 * We just wanted to add it; we don't actually need a
3488 * reference. This will result in a route that's added
3489 * to the routing table without a reference count. The
3490 * RTM_DELETE code will do the necessary step to adjust
3491 * the reference count at deletion time.
3493 RT_REMREF_LOCKED(rt
);
3506 rt_set_idleref(struct rtentry
*rt
)
3508 RT_LOCK_ASSERT_HELD(rt
);
3511 * We currently keep idle refcnt only on unicast cloned routes
3512 * that aren't marked with RTF_NOIFREF.
3514 if (rt
->rt_parent
!= NULL
&& !(rt
->rt_flags
&
3515 (RTF_NOIFREF
|RTF_BROADCAST
| RTF_MULTICAST
)) &&
3516 (rt
->rt_flags
& (RTF_UP
|RTF_WASCLONED
|RTF_IFREF
)) ==
3517 (RTF_UP
|RTF_WASCLONED
)) {
3518 rt_clear_idleref(rt
); /* drop existing refcnt if any */
3519 rt
->rt_if_ref_fn
= rte_if_ref
;
3520 /* Become a regular mutex, just in case */
3521 RT_CONVERT_LOCK(rt
);
3522 rt
->rt_if_ref_fn(rt
->rt_ifp
, 1);
3523 rt
->rt_flags
|= RTF_IFREF
;
3528 rt_clear_idleref(struct rtentry
*rt
)
3530 RT_LOCK_ASSERT_HELD(rt
);
3532 if (rt
->rt_if_ref_fn
!= NULL
) {
3533 VERIFY((rt
->rt_flags
& (RTF_NOIFREF
| RTF_IFREF
)) == RTF_IFREF
);
3534 /* Become a regular mutex, just in case */
3535 RT_CONVERT_LOCK(rt
);
3536 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
3537 rt
->rt_flags
&= ~RTF_IFREF
;
3538 rt
->rt_if_ref_fn
= NULL
;
3543 rt_set_proxy(struct rtentry
*rt
, boolean_t set
)
3545 lck_mtx_lock(rnh_lock
);
3548 * Search for any cloned routes which might have
3549 * been formed from this node, and delete them.
3551 if (rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) {
3552 struct radix_node_head
*rnh
= rt_tables
[rt_key(rt
)->sa_family
];
3555 rt
->rt_flags
|= RTF_PROXY
;
3557 rt
->rt_flags
&= ~RTF_PROXY
;
3560 if (rnh
!= NULL
&& rt_mask(rt
)) {
3561 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
3567 lck_mtx_unlock(rnh_lock
);
3571 rte_lock_init(struct rtentry
*rt
)
3573 lck_mtx_init(&rt
->rt_lock
, rte_mtx_grp
, rte_mtx_attr
);
3577 rte_lock_destroy(struct rtentry
*rt
)
3579 RT_LOCK_ASSERT_NOTHELD(rt
);
3580 lck_mtx_destroy(&rt
->rt_lock
, rte_mtx_grp
);
3584 rt_lock(struct rtentry
*rt
, boolean_t spin
)
3586 RT_LOCK_ASSERT_NOTHELD(rt
);
3588 lck_mtx_lock_spin(&rt
->rt_lock
);
3590 lck_mtx_lock(&rt
->rt_lock
);
3591 if (rte_debug
& RTD_DEBUG
)
3592 rte_lock_debug((struct rtentry_dbg
*)rt
);
3596 rt_unlock(struct rtentry
*rt
)
3598 if (rte_debug
& RTD_DEBUG
)
3599 rte_unlock_debug((struct rtentry_dbg
*)rt
);
3600 lck_mtx_unlock(&rt
->rt_lock
);
3605 rte_lock_debug(struct rtentry_dbg
*rte
)
3609 RT_LOCK_ASSERT_HELD((struct rtentry
*)rte
);
3610 idx
= atomic_add_32_ov(&rte
->rtd_lock_cnt
, 1) % CTRACE_HIST_SIZE
;
3611 if (rte_debug
& RTD_TRACE
)
3612 ctrace_record(&rte
->rtd_lock
[idx
]);
3616 rte_unlock_debug(struct rtentry_dbg
*rte
)
3620 RT_LOCK_ASSERT_HELD((struct rtentry
*)rte
);
3621 idx
= atomic_add_32_ov(&rte
->rtd_unlock_cnt
, 1) % CTRACE_HIST_SIZE
;
3622 if (rte_debug
& RTD_TRACE
)
3623 ctrace_record(&rte
->rtd_unlock
[idx
]);
3626 static struct rtentry
*
3629 if (rte_debug
& RTD_DEBUG
)
3630 return (rte_alloc_debug());
3632 return ((struct rtentry
*)zalloc(rte_zone
));
3636 rte_free(struct rtentry
*p
)
3638 if (rte_debug
& RTD_DEBUG
) {
3643 if (p
->rt_refcnt
!= 0) {
3644 panic("rte_free: rte=%p refcnt=%d non-zero\n", p
, p
->rt_refcnt
);
3652 rte_if_ref(struct ifnet
*ifp
, int cnt
)
3654 struct kev_msg ev_msg
;
3655 struct net_event_data ev_data
;
3658 /* Force cnt to 1 increment/decrement */
3659 if (cnt
< -1 || cnt
> 1) {
3660 panic("%s: invalid count argument (%d)", __func__
, cnt
);
3663 old
= atomic_add_32_ov(&ifp
->if_route_refcnt
, cnt
);
3664 if (cnt
< 0 && old
== 0) {
3665 panic("%s: ifp=%p negative route refcnt!", __func__
, ifp
);
3669 * The following is done without first holding the ifnet lock,
3670 * for performance reasons. The relevant ifnet fields, with
3671 * the exception of the if_idle_flags, are never changed
3672 * during the lifetime of the ifnet. The if_idle_flags
3673 * may possibly be modified, so in the event that the value
3674 * is stale because IFRF_IDLE_NOTIFY was cleared, we'd end up
3675 * sending the event anyway. This is harmless as it is just
3676 * a notification to the monitoring agent in user space, and
3677 * it is expected to check via SIOCGIFGETRTREFCNT again anyway.
3679 if ((ifp
->if_idle_flags
& IFRF_IDLE_NOTIFY
) && cnt
< 0 && old
== 1) {
3680 bzero(&ev_msg
, sizeof (ev_msg
));
3681 bzero(&ev_data
, sizeof (ev_data
));
3683 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3684 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3685 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3686 ev_msg
.event_code
= KEV_DL_IF_IDLE_ROUTE_REFCNT
;
3688 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3690 ev_data
.if_family
= ifp
->if_family
;
3691 ev_data
.if_unit
= ifp
->if_unit
;
3692 ev_msg
.dv
[0].data_length
= sizeof (struct net_event_data
);
3693 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3695 dlil_post_complete_msg(NULL
, &ev_msg
);
3699 static inline struct rtentry
*
3700 rte_alloc_debug(void)
3702 struct rtentry_dbg
*rte
;
3704 rte
= ((struct rtentry_dbg
*)zalloc(rte_zone
));
3706 bzero(rte
, sizeof (*rte
));
3707 if (rte_debug
& RTD_TRACE
)
3708 ctrace_record(&rte
->rtd_alloc
);
3709 rte
->rtd_inuse
= RTD_INUSE
;
3711 return ((struct rtentry
*)rte
);
3715 rte_free_debug(struct rtentry
*p
)
3717 struct rtentry_dbg
*rte
= (struct rtentry_dbg
*)p
;
3719 if (p
->rt_refcnt
!= 0) {
3720 panic("rte_free: rte=%p refcnt=%d\n", p
, p
->rt_refcnt
);
3723 if (rte
->rtd_inuse
== RTD_FREED
) {
3724 panic("rte_free: double free rte=%p\n", rte
);
3726 } else if (rte
->rtd_inuse
!= RTD_INUSE
) {
3727 panic("rte_free: corrupted rte=%p\n", rte
);
3730 bcopy((caddr_t
)p
, (caddr_t
)&rte
->rtd_entry_saved
, sizeof (*p
));
3731 /* Preserve rt_lock to help catch use-after-free cases */
3732 bzero((caddr_t
)p
, offsetof(struct rtentry
, rt_lock
));
3734 rte
->rtd_inuse
= RTD_FREED
;
3736 if (rte_debug
& RTD_TRACE
)
3737 ctrace_record(&rte
->rtd_free
);
3739 if (!(rte_debug
& RTD_NO_FREE
))
3744 ctrace_record(ctrace_t
*tr
)
3746 tr
->th
= current_thread();
3747 bzero(tr
->pc
, sizeof (tr
->pc
));
3748 (void) OSBacktrace(tr
->pc
, CTRACE_STACK_SIZE
);
3752 route_copyout(struct route
*dst
, const struct route
*src
, size_t length
)
3754 /* Copy everything (rt, srcif, flags, dst) from src */
3755 bcopy(src
, dst
, length
);
3757 /* Hold one reference for the local copy of struct route */
3758 if (dst
->ro_rt
!= NULL
)
3759 RT_ADDREF(dst
->ro_rt
);
3761 /* Hold one reference for the local copy of struct lle */
3762 if (dst
->ro_lle
!= NULL
)
3763 LLE_ADDREF(dst
->ro_lle
);
3765 /* Hold one reference for the local copy of struct ifaddr */
3766 if (dst
->ro_srcia
!= NULL
)
3767 IFA_ADDREF(dst
->ro_srcia
);
3771 route_copyin(struct route
*src
, struct route
*dst
, size_t length
)
3774 * No cached route at the destination?
3775 * If none, then remove old references if present
3776 * and copy entire src route.
3778 if (dst
->ro_rt
== NULL
) {
3780 * Ditch the cached link layer reference (dst)
3781 * since we're about to take everything there is in src
3783 if (dst
->ro_lle
!= NULL
)
3784 LLE_REMREF(dst
->ro_lle
);
3786 * Ditch the address in the cached copy (dst) since
3787 * we're about to take everything there is in src.
3789 if (dst
->ro_srcia
!= NULL
)
3790 IFA_REMREF(dst
->ro_srcia
);
3792 * Copy everything (rt, ro_lle, srcia, flags, dst) from src; the
3793 * references to rt and/or srcia were held at the time
3794 * of storage and are kept intact.
3796 bcopy(src
, dst
, length
);
3801 * We know dst->ro_rt is not NULL here.
3802 * If the src->ro_rt is the same, update ro_lle, srcia and flags
3803 * and ditch the route in the local copy.
3805 if (dst
->ro_rt
== src
->ro_rt
) {
3806 dst
->ro_flags
= src
->ro_flags
;
3808 if (dst
->ro_lle
!= src
->ro_lle
) {
3809 if (dst
->ro_lle
!= NULL
)
3810 LLE_REMREF(dst
->ro_lle
);
3811 dst
->ro_lle
= src
->ro_lle
;
3812 } else if (src
->ro_lle
!= NULL
) {
3813 LLE_REMREF(src
->ro_lle
);
3816 if (dst
->ro_srcia
!= src
->ro_srcia
) {
3817 if (dst
->ro_srcia
!= NULL
)
3818 IFA_REMREF(dst
->ro_srcia
);
3819 dst
->ro_srcia
= src
->ro_srcia
;
3820 } else if (src
->ro_srcia
!= NULL
) {
3821 IFA_REMREF(src
->ro_srcia
);
3828 * If they are dst's ro_rt is not equal to src's,
3829 * and src'd rt is not NULL, then remove old references
3830 * if present and copy entire src route.
3832 if (src
->ro_rt
!= NULL
) {
3835 if (dst
->ro_lle
!= NULL
)
3836 LLE_REMREF(dst
->ro_lle
);
3837 if (dst
->ro_srcia
!= NULL
)
3838 IFA_REMREF(dst
->ro_srcia
);
3839 bcopy(src
, dst
, length
);
3844 * Here, dst's cached route is not NULL but source's is.
3845 * Just get rid of all the other cached reference in src.
3847 if (src
->ro_srcia
!= NULL
) {
3849 * Ditch src address in the local copy (src) since we're
3850 * not caching the route entry anyway (ro_rt is NULL).
3852 IFA_REMREF(src
->ro_srcia
);
3854 if (src
->ro_lle
!= NULL
) {
3856 * Ditch cache lle in the local copy (src) since we're
3857 * not caching the route anyway (ro_rt is NULL).
3859 LLE_REMREF(src
->ro_lle
);
3862 /* This function consumes the references on src */
3865 src
->ro_srcia
= NULL
;
3869 * route_to_gwroute will find the gateway route for a given route.
3871 * If the route is down, look the route up again.
3872 * If the route goes through a gateway, get the route to the gateway.
3873 * If the gateway route is down, look it up again.
3874 * If the route is set to reject, verify it hasn't expired.
3876 * If the returned route is non-NULL, the caller is responsible for
3877 * releasing the reference and unlocking the route.
3879 #define senderr(e) { error = (e); goto bad; }
3881 route_to_gwroute(const struct sockaddr
*net_dest
, struct rtentry
*hint0
,
3882 struct rtentry
**out_route
)
3885 struct rtentry
*rt
= hint0
, *hint
= hint0
;
3887 unsigned int ifindex
;
3896 * Next hop determination. Because we may involve the gateway route
3897 * in addition to the original route, locking is rather complicated.
3898 * The general concept is that regardless of whether the route points
3899 * to the original route or to the gateway route, this routine takes
3900 * an extra reference on such a route. This extra reference will be
3901 * released at the end.
3903 * Care must be taken to ensure that the "hint0" route never gets freed
3904 * via rtfree(), since the caller may have stored it inside a struct
3905 * route with a reference held for that placeholder.
3908 ifindex
= rt
->rt_ifp
->if_index
;
3909 RT_ADDREF_LOCKED(rt
);
3910 if (!(rt
->rt_flags
& RTF_UP
)) {
3911 RT_REMREF_LOCKED(rt
);
3913 /* route is down, find a new one */
3914 hint
= rt
= rtalloc1_scoped((struct sockaddr
*)
3915 (size_t)net_dest
, 1, 0, ifindex
);
3918 ifindex
= rt
->rt_ifp
->if_index
;
3920 senderr(EHOSTUNREACH
);
3925 * We have a reference to "rt" by now; it will either
3926 * be released or freed at the end of this routine.
3928 RT_LOCK_ASSERT_HELD(rt
);
3929 if ((gwroute
= (rt
->rt_flags
& RTF_GATEWAY
))) {
3930 struct rtentry
*gwrt
= rt
->rt_gwroute
;
3931 struct sockaddr_storage ss
;
3932 struct sockaddr
*gw
= (struct sockaddr
*)&ss
;
3935 RT_ADDREF_LOCKED(hint
);
3937 /* If there's no gateway rt, look it up */
3939 bcopy(rt
->rt_gateway
, gw
, MIN(sizeof (ss
),
3940 rt
->rt_gateway
->sa_len
));
3944 /* Become a regular mutex */
3945 RT_CONVERT_LOCK(rt
);
3948 * Take gwrt's lock while holding route's lock;
3949 * this is okay since gwrt never points back
3950 * to "rt", so no lock ordering issues.
3953 if (!(gwrt
->rt_flags
& RTF_UP
)) {
3954 rt
->rt_gwroute
= NULL
;
3956 bcopy(rt
->rt_gateway
, gw
, MIN(sizeof (ss
),
3957 rt
->rt_gateway
->sa_len
));
3961 lck_mtx_lock(rnh_lock
);
3962 gwrt
= rtalloc1_scoped_locked(gw
, 1, 0, ifindex
);
3966 * Bail out if the route is down, no route
3967 * to gateway, circular route, or if the
3968 * gateway portion of "rt" has changed.
3970 if (!(rt
->rt_flags
& RTF_UP
) || gwrt
== NULL
||
3971 gwrt
== rt
|| !equal(gw
, rt
->rt_gateway
)) {
3973 RT_REMREF_LOCKED(gwrt
);
3977 RT_REMREF_LOCKED(hint
);
3981 rtfree_locked(gwrt
);
3982 lck_mtx_unlock(rnh_lock
);
3983 senderr(EHOSTUNREACH
);
3985 VERIFY(gwrt
!= NULL
);
3987 * Set gateway route; callee adds ref to gwrt;
3988 * gwrt has an extra ref from rtalloc1() for
3991 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
3993 RT_REMREF_LOCKED(rt
); /* hint still holds a refcnt */
3995 lck_mtx_unlock(rnh_lock
);
3998 RT_ADDREF_LOCKED(gwrt
);
4001 RT_REMREF_LOCKED(rt
); /* hint still holds a refcnt */
4005 VERIFY(rt
== gwrt
&& rt
!= hint
);
4008 * This is an opportunity to revalidate the parent route's
4009 * rt_gwroute, in case it now points to a dead route entry.
4010 * Parent route won't go away since the clone (hint) holds
4011 * a reference to it. rt == gwrt.
4014 if ((hint
->rt_flags
& (RTF_WASCLONED
| RTF_UP
)) ==
4015 (RTF_WASCLONED
| RTF_UP
)) {
4016 struct rtentry
*prt
= hint
->rt_parent
;
4017 VERIFY(prt
!= NULL
);
4019 RT_CONVERT_LOCK(hint
);
4022 rt_revalidate_gwroute(prt
, rt
);
4028 /* Clean up "hint" now; see notes above regarding hint0 */
4035 /* rt == gwrt; if it is now down, give up */
4037 if (!(rt
->rt_flags
& RTF_UP
)) {
4039 senderr(EHOSTUNREACH
);
4043 if (rt
->rt_flags
& RTF_REJECT
) {
4044 VERIFY(rt
->rt_expire
== 0 || rt
->rt_rmx
.rmx_expire
!= 0);
4045 VERIFY(rt
->rt_expire
!= 0 || rt
->rt_rmx
.rmx_expire
== 0);
4046 timenow
= net_uptime();
4047 if (rt
->rt_expire
== 0 || timenow
< rt
->rt_expire
) {
4049 senderr(!gwroute
? EHOSTDOWN
: EHOSTUNREACH
);
4053 /* Become a regular mutex */
4054 RT_CONVERT_LOCK(rt
);
4056 /* Caller is responsible for cleaning up "rt" */
4061 /* Clean up route (either it is "rt" or "gwrt") */
4065 RT_REMREF_LOCKED(rt
);
4077 rt_revalidate_gwroute(struct rtentry
*rt
, struct rtentry
*gwrt
)
4079 VERIFY(gwrt
!= NULL
);
4082 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_UP
)) == (RTF_GATEWAY
| RTF_UP
) &&
4083 rt
->rt_ifp
== gwrt
->rt_ifp
&& rt
->rt_gateway
->sa_family
==
4084 rt_key(gwrt
)->sa_family
&& (rt
->rt_gwroute
== NULL
||
4085 !(rt
->rt_gwroute
->rt_flags
& RTF_UP
))) {
4087 VERIFY(rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
));
4089 if (rt
->rt_gateway
->sa_family
== AF_INET
||
4090 rt
->rt_gateway
->sa_family
== AF_INET6
) {
4091 struct sockaddr_storage key_ss
, gw_ss
;
4093 * We need to compare rt_key and rt_gateway; create
4094 * local copies to get rid of any ifscope association.
4096 (void) sa_copy(rt_key(gwrt
), &key_ss
, NULL
);
4097 (void) sa_copy(rt
->rt_gateway
, &gw_ss
, NULL
);
4099 isequal
= equal(SA(&key_ss
), SA(&gw_ss
));
4101 isequal
= equal(rt_key(gwrt
), rt
->rt_gateway
);
4104 /* If they are the same, update gwrt */
4107 lck_mtx_lock(rnh_lock
);
4109 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
4111 lck_mtx_unlock(rnh_lock
);
4121 rt_str4(struct rtentry
*rt
, char *ds
, uint32_t dslen
, char *gs
, uint32_t gslen
)
4123 VERIFY(rt_key(rt
)->sa_family
== AF_INET
);
4126 (void) inet_ntop(AF_INET
,
4127 &SIN(rt_key(rt
))->sin_addr
.s_addr
, ds
, dslen
);
4128 if (dslen
>= MAX_SCOPE_ADDR_STR_LEN
&&
4129 SINIFSCOPE(rt_key(rt
))->sin_scope_id
!= IFSCOPE_NONE
) {
4132 snprintf(scpstr
, sizeof(scpstr
), "@%u",
4133 SINIFSCOPE(rt_key(rt
))->sin_scope_id
);
4135 strlcat(ds
, scpstr
, dslen
);
4140 if (rt
->rt_flags
& RTF_GATEWAY
) {
4141 (void) inet_ntop(AF_INET
,
4142 &SIN(rt
->rt_gateway
)->sin_addr
.s_addr
, gs
, gslen
);
4143 } else if (rt
->rt_ifp
!= NULL
) {
4144 snprintf(gs
, gslen
, "link#%u", rt
->rt_ifp
->if_unit
);
4146 snprintf(gs
, gslen
, "%s", "link");
4153 rt_str6(struct rtentry
*rt
, char *ds
, uint32_t dslen
, char *gs
, uint32_t gslen
)
4155 VERIFY(rt_key(rt
)->sa_family
== AF_INET6
);
4158 (void) inet_ntop(AF_INET6
,
4159 &SIN6(rt_key(rt
))->sin6_addr
, ds
, dslen
);
4160 if (dslen
>= MAX_SCOPE_ADDR_STR_LEN
&&
4161 SIN6IFSCOPE(rt_key(rt
))->sin6_scope_id
!= IFSCOPE_NONE
) {
4164 snprintf(scpstr
, sizeof(scpstr
), "@%u",
4165 SIN6IFSCOPE(rt_key(rt
))->sin6_scope_id
);
4167 strlcat(ds
, scpstr
, dslen
);
4172 if (rt
->rt_flags
& RTF_GATEWAY
) {
4173 (void) inet_ntop(AF_INET6
,
4174 &SIN6(rt
->rt_gateway
)->sin6_addr
, gs
, gslen
);
4175 } else if (rt
->rt_ifp
!= NULL
) {
4176 snprintf(gs
, gslen
, "link#%u", rt
->rt_ifp
->if_unit
);
4178 snprintf(gs
, gslen
, "%s", "link");
4186 rt_str(struct rtentry
*rt
, char *ds
, uint32_t dslen
, char *gs
, uint32_t gslen
)
4188 switch (rt_key(rt
)->sa_family
) {
4190 rt_str4(rt
, ds
, dslen
, gs
, gslen
);
4194 rt_str6(rt
, ds
, dslen
, gs
, gslen
);
4206 void route_event_init(struct route_event
*p_route_ev
, struct rtentry
*rt
,
4207 struct rtentry
*gwrt
, int route_ev_code
)
4209 VERIFY(p_route_ev
!= NULL
);
4210 bzero(p_route_ev
, sizeof(*p_route_ev
));
4212 p_route_ev
->rt
= rt
;
4213 p_route_ev
->gwrt
= gwrt
;
4214 p_route_ev
->route_event_code
= route_ev_code
;
4218 route_event_callback(void *arg
)
4220 struct route_event
*p_rt_ev
= (struct route_event
*)arg
;
4221 struct rtentry
*rt
= p_rt_ev
->rt
;
4222 eventhandler_tag evtag
= p_rt_ev
->evtag
;
4223 int route_ev_code
= p_rt_ev
->route_event_code
;
4225 if (route_ev_code
== ROUTE_EVHDLR_DEREGISTER
) {
4226 VERIFY(evtag
!= NULL
);
4227 EVENTHANDLER_DEREGISTER(&rt
->rt_evhdlr_ctxt
, route_event
,
4233 EVENTHANDLER_INVOKE(&rt
->rt_evhdlr_ctxt
, route_event
, rt_key(rt
),
4234 route_ev_code
, (struct sockaddr
*)&p_rt_ev
->rt_addr
,
4237 /* The code enqueuing the route event held a reference */
4239 /* XXX No reference is taken on gwrt */
4243 route_event_walktree(struct radix_node
*rn
, void *arg
)
4245 struct route_event
*p_route_ev
= (struct route_event
*)arg
;
4246 struct rtentry
*rt
= (struct rtentry
*)rn
;
4247 struct rtentry
*gwrt
= p_route_ev
->rt
;
4249 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
4253 /* Return if the entry is pending cleanup */
4254 if (rt
->rt_flags
& RTPRF_OURS
) {
4259 /* Return if it is not an indirect route */
4260 if (!(rt
->rt_flags
& RTF_GATEWAY
)) {
4265 if (rt
->rt_gwroute
!= gwrt
) {
4270 route_event_enqueue_nwk_wq_entry(rt
, gwrt
, p_route_ev
->route_event_code
,
4277 struct route_event_nwk_wq_entry
4279 struct nwk_wq_entry nwk_wqe
;
4280 struct route_event rt_ev_arg
;
4284 route_event_enqueue_nwk_wq_entry(struct rtentry
*rt
, struct rtentry
*gwrt
,
4285 uint32_t route_event_code
, eventhandler_tag evtag
, boolean_t rt_locked
)
4287 struct route_event_nwk_wq_entry
*p_rt_ev
= NULL
;
4288 struct sockaddr
*p_gw_saddr
= NULL
;
4290 MALLOC(p_rt_ev
, struct route_event_nwk_wq_entry
*,
4291 sizeof(struct route_event_nwk_wq_entry
),
4292 M_NWKWQ
, M_WAITOK
| M_ZERO
);
4295 * If the intent is to de-register, don't take
4296 * reference, route event registration already takes
4297 * a reference on route.
4299 if (route_event_code
!= ROUTE_EVHDLR_DEREGISTER
) {
4300 /* The reference is released by route_event_callback */
4302 RT_ADDREF_LOCKED(rt
);
4307 p_rt_ev
->rt_ev_arg
.rt
= rt
;
4308 p_rt_ev
->rt_ev_arg
.gwrt
= gwrt
;
4309 p_rt_ev
->rt_ev_arg
.evtag
= evtag
;
4312 p_gw_saddr
= gwrt
->rt_gateway
;
4314 p_gw_saddr
= rt
->rt_gateway
;
4316 VERIFY(p_gw_saddr
->sa_len
<= sizeof(p_rt_ev
->rt_ev_arg
.rt_addr
));
4317 bcopy(p_gw_saddr
, &(p_rt_ev
->rt_ev_arg
.rt_addr
), p_gw_saddr
->sa_len
);
4319 p_rt_ev
->rt_ev_arg
.route_event_code
= route_event_code
;
4320 p_rt_ev
->nwk_wqe
.func
= route_event_callback
;
4321 p_rt_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
4322 p_rt_ev
->nwk_wqe
.arg
= &p_rt_ev
->rt_ev_arg
;
4323 nwk_wq_enqueue((struct nwk_wq_entry
*)p_rt_ev
);
4327 route_event2str(int route_event
)
4329 const char *route_event_str
= "ROUTE_EVENT_UNKNOWN";
4330 switch (route_event
) {
4331 case ROUTE_STATUS_UPDATE
:
4332 route_event_str
= "ROUTE_STATUS_UPDATE";
4334 case ROUTE_ENTRY_REFRESH
:
4335 route_event_str
= "ROUTE_ENTRY_REFRESH";
4337 case ROUTE_ENTRY_DELETED
:
4338 route_event_str
= "ROUTE_ENTRY_DELETED";
4340 case ROUTE_LLENTRY_RESOLVED
:
4341 route_event_str
= "ROUTE_LLENTRY_RESOLVED";
4343 case ROUTE_LLENTRY_UNREACH
:
4344 route_event_str
= "ROUTE_LLENTRY_UNREACH";
4346 case ROUTE_LLENTRY_CHANGED
:
4347 route_event_str
= "ROUTE_LLENTRY_CHANGED";
4349 case ROUTE_LLENTRY_STALE
:
4350 route_event_str
= "ROUTE_LLENTRY_STALE";
4352 case ROUTE_LLENTRY_TIMEDOUT
:
4353 route_event_str
= "ROUTE_LLENTRY_TIMEDOUT";
4355 case ROUTE_LLENTRY_DELETED
:
4356 route_event_str
= "ROUTE_LLENTRY_DELETED";
4358 case ROUTE_LLENTRY_EXPIRED
:
4359 route_event_str
= "ROUTE_LLENTRY_EXPIRED";
4361 case ROUTE_LLENTRY_PROBED
:
4362 route_event_str
= "ROUTE_LLENTRY_PROBED";
4364 case ROUTE_EVHDLR_DEREGISTER
:
4365 route_event_str
= "ROUTE_EVHDLR_DEREGISTER";
4368 /* Init'd to ROUTE_EVENT_UNKNOWN */
4371 return route_event_str
;
4375 route_op_entitlement_check(struct socket
*so
,
4378 boolean_t allow_root
)
4381 if (route_op_type
== ROUTE_OP_READ
) {
4383 * If needed we can later extend this for more
4384 * granular entitlements and return a bit set of
4387 if (soopt_cred_check(so
, PRIV_NET_RESTRICTED_ROUTE_NC_READ
,
4393 } else if (cred
!= NULL
) {
4394 uid_t uid
= kauth_cred_getuid(cred
);
4396 /* uid is 0 for root */
4397 if (uid
!= 0 || !allow_root
) {
4398 if (route_op_type
== ROUTE_OP_READ
) {
4399 if (priv_check_cred(cred
,
4400 PRIV_NET_RESTRICTED_ROUTE_NC_READ
, 0) == 0)