2 * Copyright (c) 2000-2013 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>
71 #include <sys/syslog.h>
72 #include <sys/queue.h>
73 #include <sys/mcache.h>
74 #include <sys/protosw.h>
75 #include <sys/kernel.h>
76 #include <kern/lock.h>
77 #include <kern/zalloc.h>
81 #include <net/route.h>
82 #include <net/ntstat.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip_mroute.h>
87 #include <netinet/ip_var.h>
88 #include <netinet/ip6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/nd6.h>
96 #include <net/if_dl.h>
98 #include <libkern/OSAtomic.h>
99 #include <libkern/OSDebug.h>
101 #include <pexpert/pexpert.h>
104 #include <sys/kauth.h>
108 * Synchronization notes:
110 * Routing entries fall under two locking domains: the global routing table
111 * lock (rnh_lock) and the per-entry lock (rt_lock); the latter is a mutex that
112 * resides (statically defined) in the rtentry structure.
114 * The locking domains for routing are defined as follows:
116 * The global routing lock is used to serialize all accesses to the radix
117 * trees defined by rt_tables[], as well as the tree of masks. This includes
118 * lookups, insertions and removals of nodes to/from the respective tree.
119 * It is also used to protect certain fields in the route entry that aren't
120 * often modified and/or require global serialization (more details below.)
122 * The per-route entry lock is used to serialize accesses to several routing
123 * entry fields (more details below.) Acquiring and releasing this lock is
124 * done via RT_LOCK() and RT_UNLOCK() routines.
126 * In cases where both rnh_lock and rt_lock must be held, the former must be
127 * acquired first in order to maintain lock ordering. It is not a requirement
128 * that rnh_lock be acquired first before rt_lock, but in case both must be
129 * acquired in succession, the correct lock ordering must be followed.
131 * The fields of the rtentry structure are protected in the following way:
135 * - Routing table lock (rnh_lock).
137 * rt_parent, rt_mask, rt_llinfo_free, rt_tree_genid
139 * - Set once during creation and never changes; no locks to read.
141 * rt_flags, rt_genmask, rt_llinfo, rt_rmx, rt_refcnt, rt_gwroute
143 * - Routing entry lock (rt_lock) for read/write access.
145 * - Some values of rt_flags are either set once at creation time,
146 * or aren't currently used, and thus checking against them can
147 * be done without rt_lock: RTF_GATEWAY, RTF_HOST, RTF_DYNAMIC,
148 * RTF_DONE, RTF_XRESOLVE, RTF_STATIC, RTF_BLACKHOLE, RTF_ANNOUNCE,
149 * RTF_USETRAILERS, RTF_WASCLONED, RTF_PINNED, RTF_LOCAL,
150 * RTF_BROADCAST, RTF_MULTICAST, RTF_IFSCOPE, RTF_IFREF.
152 * rt_key, rt_gateway, rt_ifp, rt_ifa
154 * - Always written/modified with both rnh_lock and rt_lock held.
156 * - May be read freely with rnh_lock held, else must hold rt_lock
157 * for read access; holding both locks for read is also okay.
159 * - In the event rnh_lock is not acquired, or is not possible to be
160 * acquired across the operation, setting RTF_CONDEMNED on a route
161 * entry will prevent its rt_key, rt_gateway, rt_ifp and rt_ifa
162 * from being modified. This is typically done on a route that
163 * has been chosen for a removal (from the tree) prior to dropping
164 * the rt_lock, so that those values will remain the same until
165 * the route is freed.
167 * When rnh_lock is held rt_setgate(), rt_setif(), and rtsetifa() are
168 * single-threaded, thus exclusive. This flag will also prevent the
169 * route from being looked up via rt_lookup().
173 * - Assumes that 32-bit writes are atomic; no locks.
177 * - Currently unused; no locks.
179 * Operations on a route entry can be described as follows:
181 * CREATE an entry with reference count set to 0 as part of RTM_ADD/RESOLVE.
183 * INSERTION of an entry into the radix tree holds the rnh_lock, checks
184 * for duplicates and then adds the entry. rtrequest returns the entry
185 * after bumping up the reference count to 1 (for the caller).
187 * LOOKUP of an entry holds the rnh_lock and bumps up the reference count
188 * before returning; it is valid to also bump up the reference count using
189 * RT_ADDREF after the lookup has returned an entry.
191 * REMOVAL of an entry from the radix tree holds the rnh_lock, removes the
192 * entry but does not decrement the reference count. Removal happens when
193 * the route is explicitly deleted (RTM_DELETE) or when it is in the cached
194 * state and it expires. The route is said to be "down" when it is no
195 * longer present in the tree. Freeing the entry will happen on the last
196 * reference release of such a "down" route.
198 * RT_ADDREF/RT_REMREF operates on the routing entry which increments/
199 * decrements the reference count, rt_refcnt, atomically on the rtentry.
200 * rt_refcnt is modified only using this routine. The general rule is to
201 * do RT_ADDREF in the function that is passing the entry as an argument,
202 * in order to prevent the entry from being freed by the callee.
205 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
207 extern void kdp_set_gateway_mac(void *gatewaymac
);
209 __private_extern__
struct rtstat rtstat
= { 0, 0, 0, 0, 0 };
210 struct radix_node_head
*rt_tables
[AF_MAX
+1];
212 decl_lck_mtx_data(, rnh_lock_data
); /* global routing tables mutex */
213 lck_mtx_t
*rnh_lock
= &rnh_lock_data
;
214 static lck_attr_t
*rnh_lock_attr
;
215 static lck_grp_t
*rnh_lock_grp
;
216 static lck_grp_attr_t
*rnh_lock_grp_attr
;
218 /* Lock group and attribute for routing entry locks */
219 static lck_attr_t
*rte_mtx_attr
;
220 static lck_grp_t
*rte_mtx_grp
;
221 static lck_grp_attr_t
*rte_mtx_grp_attr
;
223 int rttrash
= 0; /* routes not in table but not freed */
225 unsigned int rte_debug
;
227 /* Possible flags for rte_debug */
228 #define RTD_DEBUG 0x1 /* enable or disable rtentry debug facility */
229 #define RTD_TRACE 0x2 /* trace alloc, free, refcnt and lock */
230 #define RTD_NO_FREE 0x4 /* don't free (good to catch corruptions) */
232 #define RTE_NAME "rtentry" /* name for zone and rt_lock */
234 static struct zone
*rte_zone
; /* special zone for rtentry */
235 #define RTE_ZONE_MAX 65536 /* maximum elements in zone */
236 #define RTE_ZONE_NAME RTE_NAME /* name of rtentry zone */
238 #define RTD_INUSE 0xFEEDFACE /* entry is in use */
239 #define RTD_FREED 0xDEADBEEF /* entry is freed */
242 __private_extern__
unsigned int ctrace_stack_size
= CTRACE_STACK_SIZE
;
243 __private_extern__
unsigned int ctrace_hist_size
= CTRACE_HIST_SIZE
;
246 * Debug variant of rtentry structure.
249 struct rtentry rtd_entry
; /* rtentry */
250 struct rtentry rtd_entry_saved
; /* saved rtentry */
251 uint32_t rtd_inuse
; /* in use pattern */
252 uint16_t rtd_refhold_cnt
; /* # of rtref */
253 uint16_t rtd_refrele_cnt
; /* # of rtunref */
254 uint32_t rtd_lock_cnt
; /* # of locks */
255 uint32_t rtd_unlock_cnt
; /* # of unlocks */
257 * Alloc and free callers.
262 * Circular lists of rtref and rtunref callers.
264 ctrace_t rtd_refhold
[CTRACE_HIST_SIZE
];
265 ctrace_t rtd_refrele
[CTRACE_HIST_SIZE
];
267 * Circular lists of locks and unlocks.
269 ctrace_t rtd_lock
[CTRACE_HIST_SIZE
];
270 ctrace_t rtd_unlock
[CTRACE_HIST_SIZE
];
274 TAILQ_ENTRY(rtentry_dbg
) rtd_trash_link
;
277 /* List of trash route entries protected by rnh_lock */
278 static TAILQ_HEAD(, rtentry_dbg
) rttrash_head
;
280 static void rte_lock_init(struct rtentry
*);
281 static void rte_lock_destroy(struct rtentry
*);
282 static inline struct rtentry
*rte_alloc_debug(void);
283 static inline void rte_free_debug(struct rtentry
*);
284 static inline void rte_lock_debug(struct rtentry_dbg
*);
285 static inline void rte_unlock_debug(struct rtentry_dbg
*);
286 static void rt_maskedcopy(struct sockaddr
*,
287 struct sockaddr
*, struct sockaddr
*);
288 static void rtable_init(void **);
289 static inline void rtref_audit(struct rtentry_dbg
*);
290 static inline void rtunref_audit(struct rtentry_dbg
*);
291 static struct rtentry
*rtalloc1_common_locked(struct sockaddr
*, int, uint32_t,
293 static int rtrequest_common_locked(int, struct sockaddr
*,
294 struct sockaddr
*, struct sockaddr
*, int, struct rtentry
**,
296 static struct rtentry
*rtalloc1_locked(struct sockaddr
*, int, uint32_t);
297 static void rtalloc_ign_common_locked(struct route
*, uint32_t, unsigned int);
298 static inline void sin6_set_ifscope(struct sockaddr
*, unsigned int);
299 static inline void sin6_set_embedded_ifscope(struct sockaddr
*, unsigned int);
300 static inline unsigned int sin6_get_embedded_ifscope(struct sockaddr
*);
301 static struct sockaddr
*sa_copy(struct sockaddr
*, struct sockaddr_storage
*,
303 static struct sockaddr
*ma_copy(int, struct sockaddr
*,
304 struct sockaddr_storage
*, unsigned int);
305 static struct sockaddr
*sa_trim(struct sockaddr
*, int);
306 static struct radix_node
*node_lookup(struct sockaddr
*, struct sockaddr
*,
308 static struct radix_node
*node_lookup_default(int);
309 static struct rtentry
*rt_lookup_common(boolean_t
, boolean_t
, struct sockaddr
*,
310 struct sockaddr
*, struct radix_node_head
*, unsigned int);
311 static int rn_match_ifscope(struct radix_node
*, void *);
312 static struct ifaddr
*ifa_ifwithroute_common_locked(int,
313 const struct sockaddr
*, const struct sockaddr
*, unsigned int);
314 static struct rtentry
*rte_alloc(void);
315 static void rte_free(struct rtentry
*);
316 static void rtfree_common(struct rtentry
*, boolean_t
);
317 static void rte_if_ref(struct ifnet
*, int);
318 static void rt_set_idleref(struct rtentry
*);
319 static void rt_clear_idleref(struct rtentry
*);
320 static void rt_str4(struct rtentry
*, char *, uint32_t, char *, uint32_t);
322 static void rt_str6(struct rtentry
*, char *, uint32_t, char *, uint32_t);
325 uint32_t route_genid_inet
= 0;
327 uint32_t route_genid_inet6
= 0;
330 #define ASSERT_SINIFSCOPE(sa) { \
331 if ((sa)->sa_family != AF_INET || \
332 (sa)->sa_len < sizeof (struct sockaddr_in)) \
333 panic("%s: bad sockaddr_in %p\n", __func__, sa); \
336 #define ASSERT_SIN6IFSCOPE(sa) { \
337 if ((sa)->sa_family != AF_INET6 || \
338 (sa)->sa_len < sizeof (struct sockaddr_in6)) \
339 panic("%s: bad sockaddr_in %p\n", __func__, sa); \
343 * Argument to leaf-matching routine; at present it is scoped routing
344 * specific but can be expanded in future to include other search filters.
346 struct matchleaf_arg
{
347 unsigned int ifscope
; /* interface scope */
351 * For looking up the non-scoped default route (sockaddr instead
352 * of sockaddr_in for convenience).
354 static struct sockaddr sin_def
= {
355 sizeof (struct sockaddr_in
), AF_INET
, { 0, }
358 static struct sockaddr_in6 sin6_def
= {
359 sizeof (struct sockaddr_in6
), AF_INET6
, 0, 0, IN6ADDR_ANY_INIT
, 0
363 * Interface index (scope) of the primary interface; determined at
364 * the time when the default, non-scoped route gets added, changed
365 * or deleted. Protected by rnh_lock.
367 static unsigned int primary_ifscope
= IFSCOPE_NONE
;
368 static unsigned int primary6_ifscope
= IFSCOPE_NONE
;
370 #define INET_DEFAULT(sa) \
371 ((sa)->sa_family == AF_INET && SIN(sa)->sin_addr.s_addr == 0)
373 #define INET6_DEFAULT(sa) \
374 ((sa)->sa_family == AF_INET6 && \
375 IN6_IS_ADDR_UNSPECIFIED(&SIN6(sa)->sin6_addr))
377 #define SA_DEFAULT(sa) (INET_DEFAULT(sa) || INET6_DEFAULT(sa))
378 #define RT(r) ((struct rtentry *)r)
379 #define RN(r) ((struct radix_node *)r)
380 #define RT_HOST(r) (RT(r)->rt_flags & RTF_HOST)
382 SYSCTL_DECL(_net_route
);
384 unsigned int rt_verbose
; /* verbosity level (0 to disable) */
385 SYSCTL_UINT(_net_route
, OID_AUTO
, verbose
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
389 rtable_init(void **table
)
393 domain_proto_mtx_lock_assert_held();
395 TAILQ_FOREACH(dom
, &domains
, dom_entry
) {
396 if (dom
->dom_rtattach
!= NULL
)
397 dom
->dom_rtattach(&table
[dom
->dom_family
],
403 * Called by route_dinit().
411 _CASSERT(offsetof(struct route
, ro_rt
) ==
412 offsetof(struct route_in6
, ro_rt
));
413 _CASSERT(offsetof(struct route
, ro_srcia
) ==
414 offsetof(struct route_in6
, ro_srcia
));
415 _CASSERT(offsetof(struct route
, ro_flags
) ==
416 offsetof(struct route_in6
, ro_flags
));
417 _CASSERT(offsetof(struct route
, ro_dst
) ==
418 offsetof(struct route_in6
, ro_dst
));
421 PE_parse_boot_argn("rte_debug", &rte_debug
, sizeof (rte_debug
));
423 rte_debug
|= RTD_DEBUG
;
425 rnh_lock_grp_attr
= lck_grp_attr_alloc_init();
426 rnh_lock_grp
= lck_grp_alloc_init("route", rnh_lock_grp_attr
);
427 rnh_lock_attr
= lck_attr_alloc_init();
428 lck_mtx_init(rnh_lock
, rnh_lock_grp
, rnh_lock_attr
);
430 rte_mtx_grp_attr
= lck_grp_attr_alloc_init();
431 rte_mtx_grp
= lck_grp_alloc_init(RTE_NAME
, rte_mtx_grp_attr
);
432 rte_mtx_attr
= lck_attr_alloc_init();
434 lck_mtx_lock(rnh_lock
);
435 rn_init(); /* initialize all zeroes, all ones, mask table */
436 lck_mtx_unlock(rnh_lock
);
437 rtable_init((void **)rt_tables
);
439 if (rte_debug
& RTD_DEBUG
)
440 size
= sizeof (struct rtentry_dbg
);
442 size
= sizeof (struct rtentry
);
444 rte_zone
= zinit(size
, RTE_ZONE_MAX
* size
, 0, RTE_ZONE_NAME
);
445 if (rte_zone
== NULL
) {
446 panic("%s: failed allocating rte_zone", __func__
);
449 zone_change(rte_zone
, Z_EXPAND
, TRUE
);
450 zone_change(rte_zone
, Z_CALLERACCT
, FALSE
);
451 zone_change(rte_zone
, Z_NOENCRYPT
, TRUE
);
453 TAILQ_INIT(&rttrash_head
);
457 * Given a route, determine whether or not it is the non-scoped default
458 * route; dst typically comes from rt_key(rt) but may be coming from
459 * a separate place when rt is in the process of being created.
462 rt_primary_default(struct rtentry
*rt
, struct sockaddr
*dst
)
464 return (SA_DEFAULT(dst
) && !(rt
->rt_flags
& RTF_IFSCOPE
));
468 * Set the ifscope of the primary interface; caller holds rnh_lock.
471 set_primary_ifscope(int af
, unsigned int ifscope
)
474 primary_ifscope
= ifscope
;
476 primary6_ifscope
= ifscope
;
480 * Return the ifscope of the primary interface; caller holds rnh_lock.
483 get_primary_ifscope(int af
)
485 return (af
== AF_INET
? primary_ifscope
: primary6_ifscope
);
489 * Set the scope ID of a given a sockaddr_in.
492 sin_set_ifscope(struct sockaddr
*sa
, unsigned int ifscope
)
494 /* Caller must pass in sockaddr_in */
495 ASSERT_SINIFSCOPE(sa
);
497 SINIFSCOPE(sa
)->sin_scope_id
= ifscope
;
501 * Set the scope ID of given a sockaddr_in6.
504 sin6_set_ifscope(struct sockaddr
*sa
, unsigned int ifscope
)
506 /* Caller must pass in sockaddr_in6 */
507 ASSERT_SIN6IFSCOPE(sa
);
509 SIN6IFSCOPE(sa
)->sin6_scope_id
= ifscope
;
513 * Given a sockaddr_in, return the scope ID to the caller.
516 sin_get_ifscope(struct sockaddr
*sa
)
518 /* Caller must pass in sockaddr_in */
519 ASSERT_SINIFSCOPE(sa
);
521 return (SINIFSCOPE(sa
)->sin_scope_id
);
525 * Given a sockaddr_in6, return the scope ID to the caller.
528 sin6_get_ifscope(struct sockaddr
*sa
)
530 /* Caller must pass in sockaddr_in6 */
531 ASSERT_SIN6IFSCOPE(sa
);
533 return (SIN6IFSCOPE(sa
)->sin6_scope_id
);
537 sin6_set_embedded_ifscope(struct sockaddr
*sa
, unsigned int ifscope
)
539 /* Caller must pass in sockaddr_in6 */
540 ASSERT_SIN6IFSCOPE(sa
);
541 VERIFY(IN6_IS_SCOPE_EMBED(&(SIN6(sa
)->sin6_addr
)));
543 SIN6(sa
)->sin6_addr
.s6_addr16
[1] = htons(ifscope
);
546 static inline unsigned int
547 sin6_get_embedded_ifscope(struct sockaddr
*sa
)
549 /* Caller must pass in sockaddr_in6 */
550 ASSERT_SIN6IFSCOPE(sa
);
552 return (ntohs(SIN6(sa
)->sin6_addr
.s6_addr16
[1]));
556 * Copy a sockaddr_{in,in6} src to a dst storage and set scope ID into dst.
558 * To clear the scope ID, pass is a NULL pifscope. To set the scope ID, pass
559 * in a non-NULL pifscope with non-zero ifscope. Otherwise if pifscope is
560 * non-NULL and ifscope is IFSCOPE_NONE, the existing scope ID is left intact.
561 * In any case, the effective scope ID value is returned to the caller via
562 * pifscope, if it is non-NULL.
564 static struct sockaddr
*
565 sa_copy(struct sockaddr
*src
, struct sockaddr_storage
*dst
,
566 unsigned int *pifscope
)
568 int af
= src
->sa_family
;
569 unsigned int ifscope
= (pifscope
!= NULL
) ? *pifscope
: IFSCOPE_NONE
;
571 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
573 bzero(dst
, sizeof (*dst
));
576 bcopy(src
, dst
, sizeof (struct sockaddr_in
));
577 if (pifscope
== NULL
|| ifscope
!= IFSCOPE_NONE
)
578 sin_set_ifscope(SA(dst
), ifscope
);
580 bcopy(src
, dst
, sizeof (struct sockaddr_in6
));
581 if (pifscope
!= NULL
&&
582 IN6_IS_SCOPE_EMBED(&SIN6(dst
)->sin6_addr
)) {
583 unsigned int eifscope
;
585 * If the address contains the embedded scope ID,
586 * use that as the value for sin6_scope_id as long
587 * the caller doesn't insist on clearing it (by
588 * passing NULL) or setting it.
590 eifscope
= sin6_get_embedded_ifscope(SA(dst
));
591 if (eifscope
!= IFSCOPE_NONE
&& ifscope
== IFSCOPE_NONE
)
593 sin6_set_ifscope(SA(dst
), ifscope
);
595 * If sin6_scope_id is set but the address doesn't
596 * contain the equivalent embedded value, set it.
598 if (ifscope
!= IFSCOPE_NONE
&& eifscope
!= ifscope
)
599 sin6_set_embedded_ifscope(SA(dst
), ifscope
);
600 } else if (pifscope
== NULL
|| ifscope
!= IFSCOPE_NONE
) {
601 sin6_set_ifscope(SA(dst
), ifscope
);
605 if (pifscope
!= NULL
) {
606 *pifscope
= (af
== AF_INET
) ? sin_get_ifscope(SA(dst
)) :
607 sin6_get_ifscope(SA(dst
));
614 * Copy a mask from src to a dst storage and set scope ID into dst.
616 static struct sockaddr
*
617 ma_copy(int af
, struct sockaddr
*src
, struct sockaddr_storage
*dst
,
618 unsigned int ifscope
)
620 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
622 bzero(dst
, sizeof (*dst
));
623 rt_maskedcopy(src
, SA(dst
), src
);
626 * The length of the mask sockaddr would need to be adjusted
627 * to cover the additional {sin,sin6}_ifscope field; when ifscope
628 * is IFSCOPE_NONE, we'd end up clearing the scope ID field on
629 * the destination mask in addition to extending the length
630 * of the sockaddr, as a side effect. This is okay, as any
631 * trailing zeroes would be skipped by rn_addmask prior to
632 * inserting or looking up the mask in the mask tree.
635 SINIFSCOPE(dst
)->sin_scope_id
= ifscope
;
636 SINIFSCOPE(dst
)->sin_len
=
637 offsetof(struct sockaddr_inifscope
, sin_scope_id
) +
638 sizeof (SINIFSCOPE(dst
)->sin_scope_id
);
640 SIN6IFSCOPE(dst
)->sin6_scope_id
= ifscope
;
641 SIN6IFSCOPE(dst
)->sin6_len
=
642 offsetof(struct sockaddr_in6
, sin6_scope_id
) +
643 sizeof (SIN6IFSCOPE(dst
)->sin6_scope_id
);
650 * Trim trailing zeroes on a sockaddr and update its length.
652 static struct sockaddr
*
653 sa_trim(struct sockaddr
*sa
, int skip
)
655 caddr_t cp
, base
= (caddr_t
)sa
+ skip
;
657 if (sa
->sa_len
<= skip
)
660 for (cp
= base
+ (sa
->sa_len
- skip
); cp
> base
&& cp
[-1] == 0; )
663 sa
->sa_len
= (cp
- base
) + skip
;
664 if (sa
->sa_len
< skip
) {
665 /* Must not happen, and if so, panic */
666 panic("%s: broken logic (sa_len %d < skip %d )", __func__
,
669 } else if (sa
->sa_len
== skip
) {
670 /* If we end up with all zeroes, then there's no mask */
678 * Called by rtm_msg{1,2} routines to "scrub" socket address structures of
679 * kernel private information, so that clients of the routing socket will
680 * not be confused by the presence of the information, or the side effect of
681 * the increased length due to that. The source sockaddr is not modified;
682 * instead, the scrubbing happens on the destination sockaddr storage that
683 * is passed in by the caller.
686 * - removing embedded scope identifiers from network mask and destination
687 * IPv4 and IPv6 socket addresses
688 * - optionally removing global scope interface hardware addresses from
689 * link-layer interface addresses when the MAC framework check fails.
692 rtm_scrub(int type
, int idx
, struct sockaddr
*hint
, struct sockaddr
*sa
,
693 void *buf
, uint32_t buflen
, kauth_cred_t
*credp
)
695 struct sockaddr_storage
*ss
= (struct sockaddr_storage
*)buf
;
696 struct sockaddr
*ret
= sa
;
698 VERIFY(buf
!= NULL
&& buflen
>= sizeof (*ss
));
704 * If this is for an AF_INET/AF_INET6 destination address,
705 * call sa_copy() to clear the scope ID field.
707 if (sa
->sa_family
== AF_INET
&&
708 SINIFSCOPE(sa
)->sin_scope_id
!= IFSCOPE_NONE
) {
709 ret
= sa_copy(sa
, ss
, NULL
);
710 } else if (sa
->sa_family
== AF_INET6
&&
711 SIN6IFSCOPE(sa
)->sin6_scope_id
!= IFSCOPE_NONE
) {
712 ret
= sa_copy(sa
, ss
, NULL
);
719 * If this is for a mask, we can't tell whether or not there
720 * is an valid scope ID value, as the span of bytes between
721 * sa_len and the beginning of the mask (offset of sin_addr in
722 * the case of AF_INET, or sin6_addr for AF_INET6) may be
723 * filled with all-ones by rn_addmask(), and hence we cannot
724 * rely on sa_family. Because of this, we use the sa_family
725 * of the hint sockaddr (RTAX_{DST,IFA}) as indicator as to
726 * whether or not the mask is to be treated as one for AF_INET
727 * or AF_INET6. Clearing the scope ID field involves setting
728 * it to IFSCOPE_NONE followed by calling sa_trim() to trim
729 * trailing zeroes from the storage sockaddr, which reverses
730 * what was done earlier by ma_copy() on the source sockaddr.
733 ((af
= hint
->sa_family
) != AF_INET
&& af
!= AF_INET6
))
734 break; /* nothing to do */
736 skip
= (af
== AF_INET
) ?
737 offsetof(struct sockaddr_in
, sin_addr
) :
738 offsetof(struct sockaddr_in6
, sin6_addr
);
740 if (sa
->sa_len
> skip
&& sa
->sa_len
<= sizeof (*ss
)) {
741 bcopy(sa
, ss
, sa
->sa_len
);
743 * Don't use {sin,sin6}_set_ifscope() as sa_family
744 * and sa_len for the netmask might not be set to
745 * the corresponding expected values of the hint.
747 if (hint
->sa_family
== AF_INET
)
748 SINIFSCOPE(ss
)->sin_scope_id
= IFSCOPE_NONE
;
750 SIN6IFSCOPE(ss
)->sin6_scope_id
= IFSCOPE_NONE
;
751 ret
= sa_trim(SA(ss
), skip
);
754 * For AF_INET6 mask, set sa_len appropriately unless
755 * this is requested via systl_dumpentry(), in which
756 * case we return the raw value.
758 if (hint
->sa_family
== AF_INET6
&&
759 type
!= RTM_GET
&& type
!= RTM_GET2
)
760 SA(ret
)->sa_len
= sizeof (struct sockaddr_in6
);
765 if (sa
->sa_family
== AF_LINK
&& credp
) {
766 struct sockaddr_dl
*sdl
= SDL(buf
);
770 /* caller should handle worst case: SOCK_MAXADDRLEN */
771 VERIFY(buflen
>= sa
->sa_len
);
773 bcopy(sa
, sdl
, sa
->sa_len
);
774 bytes
= dlil_ifaddr_bytes(sdl
, &size
, credp
);
775 if (bytes
!= CONST_LLADDR(sdl
)) {
776 VERIFY(sdl
->sdl_alen
== size
);
777 bcopy(bytes
, LLADDR(sdl
), size
);
779 ret
= (struct sockaddr
*)sdl
;
791 * Callback leaf-matching routine for rn_matchaddr_args used
792 * for looking up an exact match for a scoped route entry.
795 rn_match_ifscope(struct radix_node
*rn
, void *arg
)
797 struct rtentry
*rt
= (struct rtentry
*)rn
;
798 struct matchleaf_arg
*ma
= arg
;
799 int af
= rt_key(rt
)->sa_family
;
801 if (!(rt
->rt_flags
& RTF_IFSCOPE
) || (af
!= AF_INET
&& af
!= AF_INET6
))
804 return (af
== AF_INET
?
805 (SINIFSCOPE(rt_key(rt
))->sin_scope_id
== ma
->ifscope
) :
806 (SIN6IFSCOPE(rt_key(rt
))->sin6_scope_id
== ma
->ifscope
));
810 * Atomically increment route generation counter
813 routegenid_update(void)
815 routegenid_inet_update();
817 routegenid_inet6_update();
822 routegenid_inet_update(void)
824 atomic_add_32(&route_genid_inet
, 1);
829 routegenid_inet6_update(void)
831 atomic_add_32(&route_genid_inet6
, 1);
836 * Packet routing routines.
839 rtalloc(struct route
*ro
)
845 rtalloc_scoped(struct route
*ro
, unsigned int ifscope
)
847 rtalloc_scoped_ign(ro
, 0, ifscope
);
851 rtalloc_ign_common_locked(struct route
*ro
, uint32_t ignore
,
852 unsigned int ifscope
)
856 if ((rt
= ro
->ro_rt
) != NULL
) {
858 if (rt
->rt_ifp
!= NULL
&& !ROUTE_UNUSABLE(ro
)) {
863 ROUTE_RELEASE_LOCKED(ro
); /* rnh_lock already held */
865 ro
->ro_rt
= rtalloc1_common_locked(&ro
->ro_dst
, 1, ignore
, ifscope
);
866 if (ro
->ro_rt
!= NULL
) {
867 RT_GENID_SYNC(ro
->ro_rt
);
868 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
873 rtalloc_ign(struct route
*ro
, uint32_t ignore
)
875 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
876 lck_mtx_lock(rnh_lock
);
877 rtalloc_ign_common_locked(ro
, ignore
, IFSCOPE_NONE
);
878 lck_mtx_unlock(rnh_lock
);
882 rtalloc_scoped_ign(struct route
*ro
, uint32_t ignore
, unsigned int ifscope
)
884 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
885 lck_mtx_lock(rnh_lock
);
886 rtalloc_ign_common_locked(ro
, ignore
, ifscope
);
887 lck_mtx_unlock(rnh_lock
);
890 static struct rtentry
*
891 rtalloc1_locked(struct sockaddr
*dst
, int report
, uint32_t ignflags
)
893 return (rtalloc1_common_locked(dst
, report
, ignflags
, IFSCOPE_NONE
));
897 rtalloc1_scoped_locked(struct sockaddr
*dst
, int report
, uint32_t ignflags
,
898 unsigned int ifscope
)
900 return (rtalloc1_common_locked(dst
, report
, ignflags
, ifscope
));
904 * Look up the route that matches the address given
905 * Or, at least try.. Create a cloned route if needed.
907 static struct rtentry
*
908 rtalloc1_common_locked(struct sockaddr
*dst
, int report
, uint32_t ignflags
,
909 unsigned int ifscope
)
911 struct radix_node_head
*rnh
= rt_tables
[dst
->sa_family
];
912 struct rtentry
*rt
, *newrt
= NULL
;
913 struct rt_addrinfo info
;
915 int err
= 0, msgtype
= RTM_MISS
;
921 * Find the longest prefix or exact (in the scoped case) address match;
922 * callee adds a reference to entry and checks for root node as well
924 rt
= rt_lookup(FALSE
, dst
, NULL
, rnh
, ifscope
);
930 nflags
= rt
->rt_flags
& ~ignflags
;
932 if (report
&& (nflags
& (RTF_CLONING
| RTF_PRCLONING
))) {
934 * We are apparently adding (report = 0 in delete).
935 * If it requires that it be cloned, do so.
936 * (This implies it wasn't a HOST route.)
938 err
= rtrequest_locked(RTM_RESOLVE
, dst
, NULL
, NULL
, 0, &newrt
);
941 * If the cloning didn't succeed, maybe what we
942 * have from lookup above will do. Return that;
943 * no need to hold another reference since it's
951 * We cloned it; drop the original route found during lookup.
952 * The resulted cloned route (newrt) would now have an extra
953 * reference held during rtrequest.
956 if ((rt
= newrt
) && (rt
->rt_flags
& RTF_XRESOLVE
)) {
958 * If the new route specifies it be
959 * externally resolved, then go do that.
961 msgtype
= RTM_RESOLVE
;
969 * Either we hit the root or couldn't find any match,
970 * Which basically means "cant get there from here"
972 rtstat
.rts_unreach
++;
976 * If required, report the failure to the supervising
978 * For a delete, this is not an error. (report == 0)
980 bzero((caddr_t
)&info
, sizeof(info
));
981 info
.rti_info
[RTAX_DST
] = dst
;
982 rt_missmsg(msgtype
, &info
, 0, err
);
989 rtalloc1(struct sockaddr
*dst
, int report
, uint32_t ignflags
)
991 struct rtentry
*entry
;
992 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
993 lck_mtx_lock(rnh_lock
);
994 entry
= rtalloc1_locked(dst
, report
, ignflags
);
995 lck_mtx_unlock(rnh_lock
);
1000 rtalloc1_scoped(struct sockaddr
*dst
, int report
, uint32_t ignflags
,
1001 unsigned int ifscope
)
1003 struct rtentry
*entry
;
1004 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1005 lck_mtx_lock(rnh_lock
);
1006 entry
= rtalloc1_scoped_locked(dst
, report
, ignflags
, ifscope
);
1007 lck_mtx_unlock(rnh_lock
);
1012 * Remove a reference count from an rtentry.
1013 * If the count gets low enough, take it out of the routing table
1016 rtfree_locked(struct rtentry
*rt
)
1018 rtfree_common(rt
, TRUE
);
1022 rtfree_common(struct rtentry
*rt
, boolean_t locked
)
1024 struct radix_node_head
*rnh
;
1026 lck_mtx_assert(rnh_lock
, locked
?
1027 LCK_MTX_ASSERT_OWNED
: LCK_MTX_ASSERT_NOTOWNED
);
1030 * Atomically decrement the reference count and if it reaches 0,
1031 * and there is a close function defined, call the close function.
1034 if (rtunref(rt
) > 0) {
1040 * To avoid violating lock ordering, we must drop rt_lock before
1041 * trying to acquire the global rnh_lock. If we are called with
1042 * rnh_lock held, then we already have exclusive access; otherwise
1043 * we do the lock dance.
1047 * Note that we check it again below after grabbing rnh_lock,
1048 * since it is possible that another thread doing a lookup wins
1049 * the race, grabs the rnh_lock first, and bumps up reference
1050 * count in which case the route should be left alone as it is
1051 * still in use. It's also possible that another thread frees
1052 * the route after we drop rt_lock; to prevent the route from
1053 * being freed, we hold an extra reference.
1055 RT_ADDREF_LOCKED(rt
);
1057 lck_mtx_lock(rnh_lock
);
1059 if (rtunref(rt
) > 0) {
1060 /* We've lost the race, so abort */
1067 * We may be blocked on other lock(s) as part of freeing
1068 * the entry below, so convert from spin to full mutex.
1070 RT_CONVERT_LOCK(rt
);
1072 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1074 /* Negative refcnt must never happen */
1075 if (rt
->rt_refcnt
!= 0) {
1076 panic("rt %p invalid refcnt %d", rt
, rt
->rt_refcnt
);
1079 /* Idle refcnt must have been dropped during rtunref() */
1080 VERIFY(!(rt
->rt_flags
& RTF_IFREF
));
1083 * find the tree for that address family
1084 * Note: in the case of igmp packets, there might not be an rnh
1086 rnh
= rt_tables
[rt_key(rt
)->sa_family
];
1089 * On last reference give the "close method" a chance to cleanup
1090 * private state. This also permits (for IPv4 and IPv6) a chance
1091 * to decide if the routing table entry should be purged immediately
1092 * or at a later time. When an immediate purge is to happen the
1093 * close routine typically issues RTM_DELETE which clears the RTF_UP
1094 * flag on the entry so that the code below reclaims the storage.
1096 if (rnh
!= NULL
&& rnh
->rnh_close
!= NULL
)
1097 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
1100 * If we are no longer "up" (and ref == 0) then we can free the
1101 * resources associated with the route.
1103 if (!(rt
->rt_flags
& RTF_UP
)) {
1104 struct rtentry
*rt_parent
;
1105 struct ifaddr
*rt_ifa
;
1107 if (rt
->rt_nodes
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)) {
1108 panic("rt %p freed while in radix tree\n", rt
);
1112 * the rtentry must have been removed from the routing table
1113 * so it is represented in rttrash; remove that now.
1115 (void) OSDecrementAtomic(&rttrash
);
1116 if (rte_debug
& RTD_DEBUG
) {
1117 TAILQ_REMOVE(&rttrash_head
, (struct rtentry_dbg
*)rt
,
1122 * release references on items we hold them on..
1123 * e.g other routes and ifaddrs.
1125 if ((rt_parent
= rt
->rt_parent
) != NULL
)
1126 rt
->rt_parent
= NULL
;
1128 if ((rt_ifa
= rt
->rt_ifa
) != NULL
)
1132 * Now free any attached link-layer info.
1134 if (rt
->rt_llinfo
!= NULL
) {
1135 if (rt
->rt_llinfo_free
!= NULL
)
1136 (*rt
->rt_llinfo_free
)(rt
->rt_llinfo
);
1138 R_Free(rt
->rt_llinfo
);
1139 rt
->rt_llinfo
= NULL
;
1143 * Route is no longer in the tree and refcnt is 0;
1144 * we have exclusive access, so destroy it.
1148 if (rt_parent
!= NULL
)
1149 rtfree_locked(rt_parent
);
1155 * The key is separately alloc'd so free it (see rt_setgate()).
1156 * This also frees the gateway, as they are always malloc'd
1162 * Free any statistics that may have been allocated
1164 nstat_route_detach(rt
);
1167 * and the rtentry itself of course
1169 rte_lock_destroy(rt
);
1173 * The "close method" has been called, but the route is
1174 * still in the radix tree with zero refcnt, i.e. "up"
1175 * and in the cached state.
1181 lck_mtx_unlock(rnh_lock
);
1185 rtfree(struct rtentry
*rt
)
1187 rtfree_common(rt
, FALSE
);
1191 * Decrements the refcount but does not free the route when
1192 * the refcount reaches zero. Unless you have really good reason,
1193 * use rtfree not rtunref.
1196 rtunref(struct rtentry
*p
)
1198 RT_LOCK_ASSERT_HELD(p
);
1200 if (p
->rt_refcnt
== 0) {
1201 panic("%s(%p) bad refcnt\n", __func__
, p
);
1203 } else if (--p
->rt_refcnt
== 0) {
1205 * Release any idle reference count held on the interface;
1206 * if the route is eligible, still UP and the refcnt becomes
1207 * non-zero at some point in future before it is purged from
1208 * the routing table, rt_set_idleref() will undo this.
1210 rt_clear_idleref(p
);
1213 if (rte_debug
& RTD_DEBUG
)
1214 rtunref_audit((struct rtentry_dbg
*)p
);
1216 /* Return new value */
1217 return (p
->rt_refcnt
);
1221 rtunref_audit(struct rtentry_dbg
*rte
)
1225 if (rte
->rtd_inuse
!= RTD_INUSE
) {
1226 panic("rtunref: on freed rte=%p\n", rte
);
1229 idx
= atomic_add_16_ov(&rte
->rtd_refrele_cnt
, 1) % CTRACE_HIST_SIZE
;
1230 if (rte_debug
& RTD_TRACE
)
1231 ctrace_record(&rte
->rtd_refrele
[idx
]);
1235 * Add a reference count from an rtentry.
1238 rtref(struct rtentry
*p
)
1240 RT_LOCK_ASSERT_HELD(p
);
1242 if (++p
->rt_refcnt
== 0) {
1243 panic("%s(%p) bad refcnt\n", __func__
, p
);
1245 } else if (p
->rt_refcnt
== 1) {
1247 * Hold an idle reference count on the interface,
1248 * if the route is eligible for it.
1253 if (rte_debug
& RTD_DEBUG
)
1254 rtref_audit((struct rtentry_dbg
*)p
);
1258 rtref_audit(struct rtentry_dbg
*rte
)
1262 if (rte
->rtd_inuse
!= RTD_INUSE
) {
1263 panic("rtref_audit: on freed rte=%p\n", rte
);
1266 idx
= atomic_add_16_ov(&rte
->rtd_refhold_cnt
, 1) % CTRACE_HIST_SIZE
;
1267 if (rte_debug
& RTD_TRACE
)
1268 ctrace_record(&rte
->rtd_refhold
[idx
]);
1272 rtsetifa(struct rtentry
*rt
, struct ifaddr
*ifa
)
1274 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1276 RT_LOCK_ASSERT_HELD(rt
);
1278 if (rt
->rt_ifa
== ifa
)
1281 /* Become a regular mutex, just in case */
1282 RT_CONVERT_LOCK(rt
);
1284 /* Release the old ifa */
1286 IFA_REMREF(rt
->rt_ifa
);
1291 /* Take a reference to the ifa */
1293 IFA_ADDREF(rt
->rt_ifa
);
1297 * Force a routing table entry to the specified
1298 * destination to go through the given gateway.
1299 * Normally called as a result of a routing redirect
1300 * message from the network layer.
1303 rtredirect(struct ifnet
*ifp
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
1304 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
,
1305 struct rtentry
**rtp
)
1307 struct rtentry
*rt
= NULL
;
1310 struct rt_addrinfo info
;
1311 struct ifaddr
*ifa
= NULL
;
1312 unsigned int ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
1313 struct sockaddr_storage ss
;
1314 int af
= src
->sa_family
;
1316 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1317 lck_mtx_lock(rnh_lock
);
1320 * Transform src into the internal routing table form for
1321 * comparison against rt_gateway below.
1324 if ((af
== AF_INET
&& ip_doscopedroute
) ||
1325 (af
== AF_INET6
&& ip6_doscopedroute
))
1327 if (af
== AF_INET
&& ip_doscopedroute
)
1329 src
= sa_copy(src
, &ss
, &ifscope
);
1332 * Verify the gateway is directly reachable; if scoped routing
1333 * is enabled, verify that it is reachable from the interface
1334 * where the ICMP redirect arrived on.
1336 if ((ifa
= ifa_ifwithnet_scoped(gateway
, ifscope
)) == NULL
) {
1337 error
= ENETUNREACH
;
1341 /* Lookup route to the destination (from the original IP header) */
1342 rt
= rtalloc1_scoped_locked(dst
, 0, RTF_CLONING
|RTF_PRCLONING
, ifscope
);
1347 * If the redirect isn't from our current router for this dst,
1348 * it's either old or wrong. If it redirects us to ourselves,
1349 * we have a routing loop, perhaps as a result of an interface
1350 * going down recently. Holding rnh_lock here prevents the
1351 * possibility of rt_ifa/ifa's ifa_addr from changing (e.g.
1352 * in_ifinit), so okay to access ifa_addr without locking.
1354 if (!(flags
& RTF_DONE
) && rt
!= NULL
&&
1355 (!equal(src
, rt
->rt_gateway
) || !equal(rt
->rt_ifa
->ifa_addr
,
1360 if ((ifa
= ifa_ifwithaddr(gateway
))) {
1363 error
= EHOSTUNREACH
;
1379 * Create a new entry if we just got back a wildcard entry
1380 * or the the lookup failed. This is necessary for hosts
1381 * which use routing redirects generated by smart gateways
1382 * to dynamically build the routing tables.
1384 if ((rt
== NULL
) || (rt_mask(rt
) != NULL
&& rt_mask(rt
)->sa_len
< 2))
1387 * Don't listen to the redirect if it's
1388 * for a route to an interface.
1390 RT_LOCK_ASSERT_HELD(rt
);
1391 if (rt
->rt_flags
& RTF_GATEWAY
) {
1392 if (((rt
->rt_flags
& RTF_HOST
) == 0) && (flags
& RTF_HOST
)) {
1394 * Changing from route to net => route to host.
1395 * Create new route, rather than smashing route
1396 * to net; similar to cloned routes, the newly
1397 * created host route is scoped as well.
1402 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
1403 error
= rtrequest_scoped_locked(RTM_ADD
, dst
,
1404 gateway
, netmask
, flags
, NULL
, ifscope
);
1405 stat
= &rtstat
.rts_dynamic
;
1408 * Smash the current notion of the gateway to
1409 * this destination. Should check about netmask!!!
1411 rt
->rt_flags
|= RTF_MODIFIED
;
1412 flags
|= RTF_MODIFIED
;
1413 stat
= &rtstat
.rts_newgateway
;
1415 * add the key and gateway (in one malloc'd chunk).
1417 error
= rt_setgate(rt
, rt_key(rt
), gateway
);
1422 error
= EHOSTUNREACH
;
1426 RT_LOCK_ASSERT_NOTHELD(rt
);
1434 rtstat
.rts_badredirect
++;
1440 routegenid_inet_update();
1442 else if (af
== AF_INET6
)
1443 routegenid_inet6_update();
1446 lck_mtx_unlock(rnh_lock
);
1447 bzero((caddr_t
)&info
, sizeof(info
));
1448 info
.rti_info
[RTAX_DST
] = dst
;
1449 info
.rti_info
[RTAX_GATEWAY
] = gateway
;
1450 info
.rti_info
[RTAX_NETMASK
] = netmask
;
1451 info
.rti_info
[RTAX_AUTHOR
] = src
;
1452 rt_missmsg(RTM_REDIRECT
, &info
, flags
, error
);
1456 * Routing table ioctl interface.
1459 rtioctl(unsigned long req
, caddr_t data
, struct proc
*p
)
1462 #if INET && MROUTING
1463 return (mrt_ioctl(req
, data
));
1466 #pragma unused(data)
1474 const struct sockaddr
*dst
,
1475 const struct sockaddr
*gateway
)
1479 lck_mtx_lock(rnh_lock
);
1480 ifa
= ifa_ifwithroute_locked(flags
, dst
, gateway
);
1481 lck_mtx_unlock(rnh_lock
);
1487 ifa_ifwithroute_locked(int flags
, const struct sockaddr
*dst
,
1488 const struct sockaddr
*gateway
)
1490 return (ifa_ifwithroute_common_locked((flags
& ~RTF_IFSCOPE
), dst
,
1491 gateway
, IFSCOPE_NONE
));
1495 ifa_ifwithroute_scoped_locked(int flags
, const struct sockaddr
*dst
,
1496 const struct sockaddr
*gateway
, unsigned int ifscope
)
1498 if (ifscope
!= IFSCOPE_NONE
)
1499 flags
|= RTF_IFSCOPE
;
1501 flags
&= ~RTF_IFSCOPE
;
1503 return (ifa_ifwithroute_common_locked(flags
, dst
, gateway
, ifscope
));
1506 static struct ifaddr
*
1507 ifa_ifwithroute_common_locked(int flags
, const struct sockaddr
*dst
,
1508 const struct sockaddr
*gw
, unsigned int ifscope
)
1510 struct ifaddr
*ifa
= NULL
;
1511 struct rtentry
*rt
= NULL
;
1512 struct sockaddr_storage dst_ss
, gw_ss
;
1514 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1517 * Just in case the sockaddr passed in by the caller
1518 * contains a scope ID, make sure to clear it since
1519 * interface addresses aren't scoped.
1523 ((dst
->sa_family
== AF_INET
&& ip_doscopedroute
) ||
1524 (dst
->sa_family
== AF_INET6
&& ip6_doscopedroute
)))
1526 if (dst
!= NULL
&& dst
->sa_family
== AF_INET
&& ip_doscopedroute
)
1528 dst
= sa_copy(SA((uintptr_t)dst
), &dst_ss
, NULL
);
1532 ((gw
->sa_family
== AF_INET
&& ip_doscopedroute
) ||
1533 (gw
->sa_family
== AF_INET6
&& ip6_doscopedroute
)))
1535 if (gw
!= NULL
&& gw
->sa_family
== AF_INET
&& ip_doscopedroute
)
1537 gw
= sa_copy(SA((uintptr_t)gw
), &gw_ss
, NULL
);
1539 if (!(flags
& RTF_GATEWAY
)) {
1541 * If we are adding a route to an interface,
1542 * and the interface is a pt to pt link
1543 * we should search for the destination
1544 * as our clue to the interface. Otherwise
1545 * we can use the local address.
1547 if (flags
& RTF_HOST
) {
1548 ifa
= ifa_ifwithdstaddr(dst
);
1551 ifa
= ifa_ifwithaddr_scoped(gw
, ifscope
);
1554 * If we are adding a route to a remote net
1555 * or host, the gateway may still be on the
1556 * other end of a pt to pt link.
1558 ifa
= ifa_ifwithdstaddr(gw
);
1561 ifa
= ifa_ifwithnet_scoped(gw
, ifscope
);
1563 /* Workaround to avoid gcc warning regarding const variable */
1564 rt
= rtalloc1_scoped_locked((struct sockaddr
*)(size_t)dst
,
1570 /* Become a regular mutex */
1571 RT_CONVERT_LOCK(rt
);
1574 RT_REMREF_LOCKED(rt
);
1580 * Holding rnh_lock here prevents the possibility of ifa from
1581 * changing (e.g. in_ifinit), so it is safe to access its
1582 * ifa_addr (here and down below) without locking.
1584 if (ifa
!= NULL
&& ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
1585 struct ifaddr
*newifa
;
1586 /* Callee adds reference to newifa upon success */
1587 newifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
1588 if (newifa
!= NULL
) {
1594 * If we are adding a gateway, it is quite possible that the
1595 * routing table has a static entry in place for the gateway,
1596 * that may not agree with info garnered from the interfaces.
1597 * The routing table should carry more precedence than the
1598 * interfaces in this matter. Must be careful not to stomp
1599 * on new entries from rtinit, hence (ifa->ifa_addr != gw).
1602 !equal(ifa
->ifa_addr
, (struct sockaddr
*)(size_t)gw
)) &&
1603 (rt
= rtalloc1_scoped_locked((struct sockaddr
*)(size_t)gw
,
1604 0, 0, ifscope
)) != NULL
) {
1610 /* Become a regular mutex */
1611 RT_CONVERT_LOCK(rt
);
1614 RT_REMREF_LOCKED(rt
);
1618 * If an interface scope was specified, the interface index of
1619 * the found ifaddr must be equivalent to that of the scope;
1620 * otherwise there is no match.
1622 if ((flags
& RTF_IFSCOPE
) &&
1623 ifa
!= NULL
&& ifa
->ifa_ifp
->if_index
!= ifscope
) {
1631 static int rt_fixdelete(struct radix_node
*, void *);
1632 static int rt_fixchange(struct radix_node
*, void *);
1635 struct rtentry
*rt0
;
1636 struct radix_node_head
*rnh
;
1640 rtrequest_locked(int req
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
1641 struct sockaddr
*netmask
, int flags
, struct rtentry
**ret_nrt
)
1643 return (rtrequest_common_locked(req
, dst
, gateway
, netmask
,
1644 (flags
& ~RTF_IFSCOPE
), ret_nrt
, IFSCOPE_NONE
));
1648 rtrequest_scoped_locked(int req
, struct sockaddr
*dst
,
1649 struct sockaddr
*gateway
, struct sockaddr
*netmask
, int flags
,
1650 struct rtentry
**ret_nrt
, unsigned int ifscope
)
1652 if (ifscope
!= IFSCOPE_NONE
)
1653 flags
|= RTF_IFSCOPE
;
1655 flags
&= ~RTF_IFSCOPE
;
1657 return (rtrequest_common_locked(req
, dst
, gateway
, netmask
,
1658 flags
, ret_nrt
, ifscope
));
1662 * Do appropriate manipulations of a routing tree given all the bits of
1665 * Storing the scope ID in the radix key is an internal job that should be
1666 * left to routines in this module. Callers should specify the scope value
1667 * to the "scoped" variants of route routines instead of manipulating the
1668 * key itself. This is typically done when creating a scoped route, e.g.
1669 * rtrequest(RTM_ADD). Once such a route is created and marked with the
1670 * RTF_IFSCOPE flag, callers can simply use its rt_key(rt) to clone it
1671 * (RTM_RESOLVE) or to remove it (RTM_DELETE). An exception to this is
1672 * during certain routing socket operations where the search key might be
1673 * derived from the routing message itself, in which case the caller must
1674 * specify the destination address and scope value for RTM_ADD/RTM_DELETE.
1677 rtrequest_common_locked(int req
, struct sockaddr
*dst0
,
1678 struct sockaddr
*gateway
, struct sockaddr
*netmask
, int flags
,
1679 struct rtentry
**ret_nrt
, unsigned int ifscope
)
1683 struct radix_node
*rn
;
1684 struct radix_node_head
*rnh
;
1685 struct ifaddr
*ifa
= NULL
;
1686 struct sockaddr
*ndst
, *dst
= dst0
;
1687 struct sockaddr_storage ss
, mask
;
1688 struct timeval caltime
;
1689 int af
= dst
->sa_family
;
1690 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1692 #define senderr(x) { error = x; goto bad; }
1694 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1696 * Find the correct routing tree to use for this Address Family
1698 if ((rnh
= rt_tables
[af
]) == NULL
)
1701 * If we are adding a host route then we don't want to put
1702 * a netmask in the tree
1704 if (flags
& RTF_HOST
)
1708 * If Scoped Routing is enabled, use a local copy of the destination
1709 * address to store the scope ID into. This logic is repeated below
1710 * in the RTM_RESOLVE handler since the caller does not normally
1711 * specify such a flag during a resolve, as well as for the handling
1712 * of IPv4 link-local address; instead, it passes in the route used for
1713 * cloning for which the scope info is derived from. Note also that
1714 * in the case of RTM_DELETE, the address passed in by the caller
1715 * might already contain the scope ID info when it is the key itself,
1716 * thus making RTF_IFSCOPE unnecessary; one instance where it is
1717 * explicitly set is inside route_output() as part of handling a
1718 * routing socket request.
1721 if (req
!= RTM_RESOLVE
&&
1722 ((af
== AF_INET
&& ip_doscopedroute
) ||
1723 (af
== AF_INET6
&& ip6_doscopedroute
))) {
1725 if (req
!= RTM_RESOLVE
&& af
== AF_INET
&& ip_doscopedroute
) {
1727 /* Transform dst into the internal routing table form */
1728 dst
= sa_copy(dst
, &ss
, &ifscope
);
1730 /* Transform netmask into the internal routing table form */
1731 if (netmask
!= NULL
)
1732 netmask
= ma_copy(af
, netmask
, &mask
, ifscope
);
1734 if (ifscope
!= IFSCOPE_NONE
)
1735 flags
|= RTF_IFSCOPE
;
1737 if ((flags
& RTF_IFSCOPE
) && (af
!= AF_INET
&& af
!= AF_INET6
))
1741 if ((af
== AF_INET
&& !ip_doscopedroute
) ||
1742 (af
== AF_INET6
&& !ip6_doscopedroute
))
1744 if (af
== AF_INET
&& !ip_doscopedroute
)
1746 ifscope
= IFSCOPE_NONE
;
1749 if (ifscope
== IFSCOPE_NONE
)
1750 flags
&= ~RTF_IFSCOPE
;
1754 struct rtentry
*gwrt
= NULL
;
1756 * Remove the item from the tree and return it.
1757 * Complain if it is not there and do no more processing.
1759 if ((rn
= rnh
->rnh_deladdr(dst
, netmask
, rnh
)) == NULL
)
1761 if (rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
)) {
1762 panic("rtrequest delete");
1765 rt
= (struct rtentry
*)rn
;
1768 rt
->rt_flags
&= ~RTF_UP
;
1770 * Release any idle reference count held on the interface
1771 * as this route is no longer externally visible.
1773 rt_clear_idleref(rt
);
1775 * Take an extra reference to handle the deletion of a route
1776 * entry whose reference count is already 0; e.g. an expiring
1777 * cloned route entry or an entry that was added to the table
1778 * with 0 reference. If the caller is interested in this route,
1779 * we will return it with the reference intact. Otherwise we
1780 * will decrement the reference via rtfree_locked() and then
1781 * possibly deallocate it.
1783 RT_ADDREF_LOCKED(rt
);
1786 * For consistency, in case the caller didn't set the flag.
1788 rt
->rt_flags
|= RTF_CONDEMNED
;
1791 * Clear RTF_ROUTER if it's set.
1793 if (rt
->rt_flags
& RTF_ROUTER
) {
1794 VERIFY(rt
->rt_flags
& RTF_HOST
);
1795 rt
->rt_flags
&= ~RTF_ROUTER
;
1799 * Now search what's left of the subtree for any cloned
1800 * routes which might have been formed from this node.
1802 if ((rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) &&
1805 rnh
->rnh_walktree_from(rnh
, dst
, rt_mask(rt
),
1811 * Remove any external references we may have.
1813 if ((gwrt
= rt
->rt_gwroute
) != NULL
)
1814 rt
->rt_gwroute
= NULL
;
1817 * give the protocol a chance to keep things in sync.
1819 if ((ifa
= rt
->rt_ifa
) != NULL
) {
1821 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1823 if (ifa_rtrequest
!= NULL
)
1824 ifa_rtrequest(RTM_DELETE
, rt
, NULL
);
1825 /* keep reference on rt_ifa */
1830 * one more rtentry floating around that is not
1831 * linked to the routing table.
1833 (void) OSIncrementAtomic(&rttrash
);
1834 if (rte_debug
& RTD_DEBUG
) {
1835 TAILQ_INSERT_TAIL(&rttrash_head
,
1836 (struct rtentry_dbg
*)rt
, rtd_trash_link
);
1840 * If this is the (non-scoped) default route, clear
1841 * the interface index used for the primary ifscope.
1843 if (rt_primary_default(rt
, rt_key(rt
))) {
1844 set_primary_ifscope(rt_key(rt
)->sa_family
,
1851 * This might result in another rtentry being freed if
1852 * we held its last reference. Do this after the rtentry
1853 * lock is dropped above, as it could lead to the same
1854 * lock being acquired if gwrt is a clone of rt.
1857 rtfree_locked(gwrt
);
1860 * If the caller wants it, then it can have it,
1861 * but it's up to it to free the rtentry as we won't be
1864 if (ret_nrt
!= NULL
) {
1865 /* Return the route to caller with reference intact */
1868 /* Dereference or deallocate the route */
1872 routegenid_inet_update();
1874 else if (af
== AF_INET6
)
1875 routegenid_inet6_update();
1880 if (ret_nrt
== NULL
|| (rt
= *ret_nrt
) == NULL
)
1883 * If cloning, we have the parent route given by the caller
1884 * and will use its rt_gateway, rt_rmx as part of the cloning
1885 * process below. Since rnh_lock is held at this point, the
1886 * parent's rt_ifa and rt_gateway will not change, and its
1887 * relevant rt_flags will not change as well. The only thing
1888 * that could change are the metrics, and thus we hold the
1889 * parent route's rt_lock later on during the actual copying
1894 flags
= rt
->rt_flags
&
1895 ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
1896 flags
|= RTF_WASCLONED
;
1897 gateway
= rt
->rt_gateway
;
1898 if ((netmask
= rt
->rt_genmask
) == NULL
)
1902 if ((af
!= AF_INET
&& af
!= AF_INET6
) ||
1903 (af
== AF_INET
&& !ip_doscopedroute
) ||
1904 (af
== AF_INET6
&& !ip6_doscopedroute
))
1906 if (af
!= AF_INET
|| !ip_doscopedroute
)
1911 * When scoped routing is enabled, cloned entries are
1912 * always scoped according to the interface portion of
1913 * the parent route. The exception to this are IPv4
1914 * link local addresses, or those routes that are cloned
1915 * from a RTF_PROXY route. For the latter, the clone
1916 * gets to keep the RTF_PROXY flag.
1918 if ((af
== AF_INET
&&
1919 IN_LINKLOCAL(ntohl(SIN(dst
)->sin_addr
.s_addr
))) ||
1920 (rt
->rt_flags
& RTF_PROXY
)) {
1921 ifscope
= IFSCOPE_NONE
;
1922 flags
&= ~RTF_IFSCOPE
;
1924 * These types of cloned routes aren't currently
1925 * eligible for idle interface reference counting.
1927 flags
|= RTF_NOIFREF
;
1929 if (flags
& RTF_IFSCOPE
) {
1930 ifscope
= (af
== AF_INET
) ?
1931 sin_get_ifscope(rt_key(rt
)) :
1932 sin6_get_ifscope(rt_key(rt
));
1934 ifscope
= rt
->rt_ifp
->if_index
;
1935 flags
|= RTF_IFSCOPE
;
1937 VERIFY(ifscope
!= IFSCOPE_NONE
);
1941 * Transform dst into the internal routing table form,
1942 * clearing out the scope ID field if ifscope isn't set.
1944 dst
= sa_copy(dst
, &ss
, (ifscope
== IFSCOPE_NONE
) ?
1947 /* Transform netmask into the internal routing table form */
1948 if (netmask
!= NULL
)
1949 netmask
= ma_copy(af
, netmask
, &mask
, ifscope
);
1954 if ((flags
& RTF_GATEWAY
) && !gateway
) {
1955 panic("rtrequest: RTF_GATEWAY but no gateway");
1958 if (flags
& RTF_IFSCOPE
) {
1959 ifa
= ifa_ifwithroute_scoped_locked(flags
, dst0
,
1962 ifa
= ifa_ifwithroute_locked(flags
, dst0
, gateway
);
1965 senderr(ENETUNREACH
);
1967 if ((rt
= rte_alloc()) == NULL
)
1969 Bzero(rt
, sizeof(*rt
));
1971 getmicrotime(&caltime
);
1972 rt
->base_calendartime
= caltime
.tv_sec
;
1973 rt
->base_uptime
= net_uptime();
1975 rt
->rt_flags
= RTF_UP
| flags
;
1978 * Point the generation ID to the tree's.
1982 rt
->rt_tree_genid
= &route_genid_inet
;
1986 rt
->rt_tree_genid
= &route_genid_inet6
;
1994 * Add the gateway. Possibly re-malloc-ing the storage for it
1995 * also add the rt_gwroute if possible.
1997 if ((error
= rt_setgate(rt
, dst
, gateway
)) != 0) {
2000 nstat_route_detach(rt
);
2001 rte_lock_destroy(rt
);
2007 * point to the (possibly newly malloc'd) dest address.
2012 * make sure it contains the value we want (masked if needed).
2015 rt_maskedcopy(dst
, ndst
, netmask
);
2017 Bcopy(dst
, ndst
, dst
->sa_len
);
2020 * Note that we now have a reference to the ifa.
2021 * This moved from below so that rnh->rnh_addaddr() can
2022 * examine the ifa and ifa->ifa_ifp if it so desires.
2025 rt
->rt_ifp
= rt
->rt_ifa
->ifa_ifp
;
2027 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
2029 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
, (caddr_t
)netmask
,
2032 struct rtentry
*rt2
;
2034 * Uh-oh, we already have one of these in the tree.
2035 * We do a special hack: if the route that's already
2036 * there was generated by the protocol-cloning
2037 * mechanism, then we just blow it away and retry
2038 * the insertion of the new one.
2040 if (flags
& RTF_IFSCOPE
) {
2041 rt2
= rtalloc1_scoped_locked(dst0
, 0,
2042 RTF_CLONING
| RTF_PRCLONING
, ifscope
);
2044 rt2
= rtalloc1_locked(dst
, 0,
2045 RTF_CLONING
| RTF_PRCLONING
);
2047 if (rt2
&& rt2
->rt_parent
) {
2049 * rnh_lock is held here, so rt_key and
2050 * rt_gateway of rt2 will not change.
2052 (void) rtrequest_locked(RTM_DELETE
, rt_key(rt2
),
2053 rt2
->rt_gateway
, rt_mask(rt2
),
2056 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
,
2057 (caddr_t
)netmask
, rnh
, rt
->rt_nodes
);
2059 /* undo the extra ref we got */
2065 * If it still failed to go into the tree,
2066 * then un-make it (this should be a function)
2069 /* Clear gateway route */
2070 rt_set_gwroute(rt
, rt_key(rt
), NULL
);
2072 IFA_REMREF(rt
->rt_ifa
);
2077 nstat_route_detach(rt
);
2078 rte_lock_destroy(rt
);
2083 rt
->rt_parent
= NULL
;
2086 * If we got here from RESOLVE, then we are cloning so clone
2087 * the rest, and note that we are a clone (and increment the
2088 * parent's references). rnh_lock is still held, which prevents
2089 * a lookup from returning the newly-created route. Hence
2090 * holding and releasing the parent's rt_lock while still
2091 * holding the route's rt_lock is safe since the new route
2092 * is not yet externally visible.
2094 if (req
== RTM_RESOLVE
) {
2095 RT_LOCK_SPIN(*ret_nrt
);
2096 VERIFY((*ret_nrt
)->rt_expire
== 0 ||
2097 (*ret_nrt
)->rt_rmx
.rmx_expire
!= 0);
2098 VERIFY((*ret_nrt
)->rt_expire
!= 0 ||
2099 (*ret_nrt
)->rt_rmx
.rmx_expire
== 0);
2100 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
;
2101 rt_setexpire(rt
, (*ret_nrt
)->rt_expire
);
2102 if ((*ret_nrt
)->rt_flags
&
2103 (RTF_CLONING
| RTF_PRCLONING
)) {
2104 rt
->rt_parent
= (*ret_nrt
);
2105 RT_ADDREF_LOCKED(*ret_nrt
);
2107 RT_UNLOCK(*ret_nrt
);
2111 * if this protocol has something to add to this then
2112 * allow it to do that as well.
2115 ifa_rtrequest
= ifa
->ifa_rtrequest
;
2117 if (ifa_rtrequest
!= NULL
)
2118 ifa_rtrequest(req
, rt
, SA(ret_nrt
? *ret_nrt
: NULL
));
2123 * If this is the (non-scoped) default route, record
2124 * the interface index used for the primary ifscope.
2126 if (rt_primary_default(rt
, rt_key(rt
))) {
2127 set_primary_ifscope(rt_key(rt
)->sa_family
,
2128 rt
->rt_ifp
->if_index
);
2132 * actually return a resultant rtentry and
2133 * give the caller a single reference.
2137 RT_ADDREF_LOCKED(rt
);
2141 routegenid_inet_update();
2143 else if (af
== AF_INET6
)
2144 routegenid_inet6_update();
2150 * We repeat the same procedures from rt_setgate() here
2151 * because they weren't completed when we called it earlier,
2152 * since the node was embryonic.
2154 if ((rt
->rt_flags
& RTF_GATEWAY
) && rt
->rt_gwroute
!= NULL
)
2155 rt_set_gwroute(rt
, rt_key(rt
), rt
->rt_gwroute
);
2157 if (req
== RTM_ADD
&&
2158 !(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != NULL
) {
2159 struct rtfc_arg arg
;
2163 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
2164 rt_fixchange
, &arg
);
2169 nstat_route_new_entry(rt
);
2180 rtrequest(int req
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
2181 struct sockaddr
*netmask
, int flags
, struct rtentry
**ret_nrt
)
2184 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2185 lck_mtx_lock(rnh_lock
);
2186 error
= rtrequest_locked(req
, dst
, gateway
, netmask
, flags
, ret_nrt
);
2187 lck_mtx_unlock(rnh_lock
);
2192 rtrequest_scoped(int req
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
2193 struct sockaddr
*netmask
, int flags
, struct rtentry
**ret_nrt
,
2194 unsigned int ifscope
)
2197 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2198 lck_mtx_lock(rnh_lock
);
2199 error
= rtrequest_scoped_locked(req
, dst
, gateway
, netmask
, flags
,
2201 lck_mtx_unlock(rnh_lock
);
2206 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
2207 * (i.e., the routes related to it by the operation of cloning). This
2208 * routine is iterated over all potential former-child-routes by way of
2209 * rnh->rnh_walktree_from() above, and those that actually are children of
2210 * the late parent (passed in as VP here) are themselves deleted.
2213 rt_fixdelete(struct radix_node
*rn
, void *vp
)
2215 struct rtentry
*rt
= (struct rtentry
*)rn
;
2216 struct rtentry
*rt0
= vp
;
2218 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2221 if (rt
->rt_parent
== rt0
&&
2222 !(rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
))) {
2224 * Safe to drop rt_lock and use rt_key, since holding
2225 * rnh_lock here prevents another thread from calling
2226 * rt_setgate() on this route.
2229 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), NULL
,
2230 rt_mask(rt
), rt
->rt_flags
, NULL
));
2237 * This routine is called from rt_setgate() to do the analogous thing for
2238 * adds and changes. There is the added complication in this case of a
2239 * middle insert; i.e., insertion of a new network route between an older
2240 * network route and (cloned) host routes. For this reason, a simple check
2241 * of rt->rt_parent is insufficient; each candidate route must be tested
2242 * against the (mask, value) of the new route (passed as before in vp)
2243 * to see if the new route matches it.
2245 * XXX - it may be possible to do fixdelete() for changes and reserve this
2246 * routine just for adds. I'm not sure why I thought it was necessary to do
2250 rt_fixchange(struct radix_node
*rn
, void *vp
)
2252 struct rtentry
*rt
= (struct rtentry
*)rn
;
2253 struct rtfc_arg
*ap
= vp
;
2254 struct rtentry
*rt0
= ap
->rt0
;
2255 struct radix_node_head
*rnh
= ap
->rnh
;
2256 u_char
*xk1
, *xm1
, *xk2
, *xmp
;
2259 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2263 if (!rt
->rt_parent
||
2264 (rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
))) {
2269 if (rt
->rt_parent
== rt0
)
2273 * There probably is a function somewhere which does this...
2274 * if not, there should be.
2276 len
= imin(rt_key(rt0
)->sa_len
, rt_key(rt
)->sa_len
);
2278 xk1
= (u_char
*)rt_key(rt0
);
2279 xm1
= (u_char
*)rt_mask(rt0
);
2280 xk2
= (u_char
*)rt_key(rt
);
2283 * Avoid applying a less specific route; do this only if the parent
2284 * route (rt->rt_parent) is a network route, since otherwise its mask
2285 * will be NULL if it is a cloning host route.
2287 if ((xmp
= (u_char
*)rt_mask(rt
->rt_parent
)) != NULL
) {
2288 int mlen
= rt_mask(rt
->rt_parent
)->sa_len
;
2289 if (mlen
> rt_mask(rt0
)->sa_len
) {
2294 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< mlen
; i
++) {
2295 if ((xmp
[i
] & ~(xmp
[i
] ^ xm1
[i
])) != xmp
[i
]) {
2302 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< len
; i
++) {
2303 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
]) {
2310 * OK, this node is a clone, and matches the node currently being
2311 * changed/added under the node's mask. So, get rid of it.
2315 * Safe to drop rt_lock and use rt_key, since holding rnh_lock here
2316 * prevents another thread from calling rt_setgate() on this route.
2319 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), NULL
,
2320 rt_mask(rt
), rt
->rt_flags
, NULL
));
2324 * Round up sockaddr len to multiples of 32-bytes. This will reduce
2325 * or even eliminate the need to re-allocate the chunk of memory used
2326 * for rt_key and rt_gateway in the event the gateway portion changes.
2327 * Certain code paths (e.g. IPSec) are notorious for caching the address
2328 * of rt_gateway; this rounding-up would help ensure that the gateway
2329 * portion never gets deallocated (though it may change contents) and
2330 * thus greatly simplifies things.
2332 #define SA_SIZE(x) (-(-((uintptr_t)(x)) & -(32)))
2335 * Sets the gateway and/or gateway route portion of a route; may be
2336 * called on an existing route to modify the gateway portion. Both
2337 * rt_key and rt_gateway are allocated out of the same memory chunk.
2338 * Route entry lock must be held by caller; this routine will return
2339 * with the lock held.
2342 rt_setgate(struct rtentry
*rt
, struct sockaddr
*dst
, struct sockaddr
*gate
)
2344 int dlen
= SA_SIZE(dst
->sa_len
), glen
= SA_SIZE(gate
->sa_len
);
2345 struct radix_node_head
*rnh
= rt_tables
[dst
->sa_family
];
2346 boolean_t loop
= FALSE
;
2348 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2349 RT_LOCK_ASSERT_HELD(rt
);
2352 * If this is for a route that is on its way of being removed,
2353 * or is temporarily frozen, reject the modification request.
2355 if (rt
->rt_flags
& RTF_CONDEMNED
)
2358 /* Add an extra ref for ourselves */
2359 RT_ADDREF_LOCKED(rt
);
2361 if (rt
->rt_flags
& RTF_GATEWAY
) {
2362 if ((dst
->sa_len
== gate
->sa_len
) &&
2363 (dst
->sa_family
== AF_INET
|| dst
->sa_family
== AF_INET6
)) {
2364 struct sockaddr_storage dst_ss
, gate_ss
;
2366 (void) sa_copy(dst
, &dst_ss
, NULL
);
2367 (void) sa_copy(gate
, &gate_ss
, NULL
);
2369 loop
= equal(SA(&dst_ss
), SA(&gate_ss
));
2371 loop
= (dst
->sa_len
== gate
->sa_len
&&
2377 * A (cloning) network route with the destination equal to the gateway
2378 * will create an endless loop (see notes below), so disallow it.
2380 if (((rt
->rt_flags
& (RTF_HOST
|RTF_GATEWAY
|RTF_LLINFO
)) ==
2381 RTF_GATEWAY
) && loop
) {
2382 /* Release extra ref */
2383 RT_REMREF_LOCKED(rt
);
2384 return (EADDRNOTAVAIL
);
2388 * A host route with the destination equal to the gateway
2389 * will interfere with keeping LLINFO in the routing
2390 * table, so disallow it.
2392 if (((rt
->rt_flags
& (RTF_HOST
|RTF_GATEWAY
|RTF_LLINFO
)) ==
2393 (RTF_HOST
|RTF_GATEWAY
)) && loop
) {
2395 * The route might already exist if this is an RTM_CHANGE
2396 * or a routing redirect, so try to delete it.
2398 if (rt_key(rt
) != NULL
) {
2400 * Safe to drop rt_lock and use rt_key, rt_gateway,
2401 * since holding rnh_lock here prevents another thread
2402 * from calling rt_setgate() on this route.
2405 (void) rtrequest_locked(RTM_DELETE
, rt_key(rt
),
2406 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
2409 /* Release extra ref */
2410 RT_REMREF_LOCKED(rt
);
2411 return (EADDRNOTAVAIL
);
2415 * The destination is not directly reachable. Get a route
2416 * to the next-hop gateway and store it in rt_gwroute.
2418 if (rt
->rt_flags
& RTF_GATEWAY
) {
2419 struct rtentry
*gwrt
;
2420 unsigned int ifscope
;
2422 if (dst
->sa_family
== AF_INET
)
2423 ifscope
= sin_get_ifscope(dst
);
2424 else if (dst
->sa_family
== AF_INET6
)
2425 ifscope
= sin6_get_ifscope(dst
);
2427 ifscope
= IFSCOPE_NONE
;
2431 * Don't ignore RTF_CLONING, since we prefer that rt_gwroute
2432 * points to a clone rather than a cloning route; see above
2433 * check for cloning loop avoidance (dst == gate).
2435 gwrt
= rtalloc1_scoped_locked(gate
, 1, RTF_PRCLONING
, ifscope
);
2437 RT_LOCK_ASSERT_NOTHELD(gwrt
);
2441 * Cloning loop avoidance:
2443 * In the presence of protocol-cloning and bad configuration,
2444 * it is possible to get stuck in bottomless mutual recursion
2445 * (rtrequest rt_setgate rtalloc1). We avoid this by not
2446 * allowing protocol-cloning to operate for gateways (which
2447 * is probably the correct choice anyway), and avoid the
2448 * resulting reference loops by disallowing any route to run
2449 * through itself as a gateway. This is obviously mandatory
2450 * when we get rt->rt_output(). It implies that a route to
2451 * the gateway must already be present in the system in order
2452 * for the gateway to be referred to by another route.
2455 RT_REMREF_LOCKED(gwrt
);
2456 /* Release extra ref */
2457 RT_REMREF_LOCKED(rt
);
2458 return (EADDRINUSE
); /* failure */
2462 * If scoped, the gateway route must use the same interface;
2463 * we're holding rnh_lock now, so rt_gateway and rt_ifp of gwrt
2464 * should not change and are freely accessible.
2466 if (ifscope
!= IFSCOPE_NONE
&& (rt
->rt_flags
& RTF_IFSCOPE
) &&
2467 gwrt
!= NULL
&& gwrt
->rt_ifp
!= NULL
&&
2468 gwrt
->rt_ifp
->if_index
!= ifscope
) {
2469 rtfree_locked(gwrt
); /* rt != gwrt, no deadlock */
2470 /* Release extra ref */
2471 RT_REMREF_LOCKED(rt
);
2472 return ((rt
->rt_flags
& RTF_HOST
) ?
2473 EHOSTUNREACH
: ENETUNREACH
);
2476 /* Check again since we dropped the lock above */
2477 if (rt
->rt_flags
& RTF_CONDEMNED
) {
2479 rtfree_locked(gwrt
);
2480 /* Release extra ref */
2481 RT_REMREF_LOCKED(rt
);
2485 /* Set gateway route; callee adds ref to gwrt if non-NULL */
2486 rt_set_gwroute(rt
, dst
, gwrt
);
2489 * In case the (non-scoped) default route gets modified via
2490 * an ICMP redirect, record the interface index used for the
2491 * primary ifscope. Also done in rt_setif() to take care
2492 * of the non-redirect cases.
2494 if (rt_primary_default(rt
, dst
) && rt
->rt_ifp
!= NULL
) {
2495 set_primary_ifscope(dst
->sa_family
,
2496 rt
->rt_ifp
->if_index
);
2500 * Tell the kernel debugger about the new default gateway
2501 * if the gateway route uses the primary interface, or
2502 * if we are in a transient state before the non-scoped
2503 * default gateway is installed (similar to how the system
2504 * was behaving in the past). In future, it would be good
2505 * to do all this only when KDP is enabled.
2507 if ((dst
->sa_family
== AF_INET
) &&
2508 gwrt
!= NULL
&& gwrt
->rt_gateway
->sa_family
== AF_LINK
&&
2509 (gwrt
->rt_ifp
->if_index
== get_primary_ifscope(AF_INET
) ||
2510 get_primary_ifscope(AF_INET
) == IFSCOPE_NONE
)) {
2511 kdp_set_gateway_mac(SDL((void *)gwrt
->rt_gateway
)->
2515 /* Release extra ref from rtalloc1() */
2521 * Prepare to store the gateway in rt_gateway. Both dst and gateway
2522 * are stored one after the other in the same malloc'd chunk. If we
2523 * have room, reuse the old buffer since rt_gateway already points
2524 * to the right place. Otherwise, malloc a new block and update
2525 * the 'dst' address and point rt_gateway to the right place.
2527 if (rt
->rt_gateway
== NULL
|| glen
> SA_SIZE(rt
->rt_gateway
->sa_len
)) {
2530 /* The underlying allocation is done with M_WAITOK set */
2531 R_Malloc(new, caddr_t
, dlen
+ glen
);
2533 /* Clear gateway route */
2534 rt_set_gwroute(rt
, dst
, NULL
);
2535 /* Release extra ref */
2536 RT_REMREF_LOCKED(rt
);
2541 * Copy from 'dst' and not rt_key(rt) because we can get
2542 * here to initialize a newly allocated route entry, in
2543 * which case rt_key(rt) is NULL (and so does rt_gateway).
2545 bzero(new, dlen
+ glen
);
2546 Bcopy(dst
, new, dst
->sa_len
);
2547 R_Free(rt_key(rt
)); /* free old block; NULL is okay */
2548 rt
->rt_nodes
->rn_key
= new;
2549 rt
->rt_gateway
= (struct sockaddr
*)(new + dlen
);
2553 * Copy the new gateway value into the memory chunk.
2555 Bcopy(gate
, rt
->rt_gateway
, gate
->sa_len
);
2558 * For consistency between rt_gateway and rt_key(gwrt).
2560 if ((rt
->rt_flags
& RTF_GATEWAY
) && rt
->rt_gwroute
!= NULL
&&
2561 (rt
->rt_gwroute
->rt_flags
& RTF_IFSCOPE
)) {
2562 if (rt
->rt_gateway
->sa_family
== AF_INET
&&
2563 rt_key(rt
->rt_gwroute
)->sa_family
== AF_INET
) {
2564 sin_set_ifscope(rt
->rt_gateway
,
2565 sin_get_ifscope(rt_key(rt
->rt_gwroute
)));
2566 } else if (rt
->rt_gateway
->sa_family
== AF_INET6
&&
2567 rt_key(rt
->rt_gwroute
)->sa_family
== AF_INET6
) {
2568 sin6_set_ifscope(rt
->rt_gateway
,
2569 sin6_get_ifscope(rt_key(rt
->rt_gwroute
)));
2574 * This isn't going to do anything useful for host routes, so
2575 * don't bother. Also make sure we have a reasonable mask
2576 * (we don't yet have one during adds).
2578 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != 0) {
2579 struct rtfc_arg arg
;
2583 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
2584 rt_fixchange
, &arg
);
2588 /* Release extra ref */
2589 RT_REMREF_LOCKED(rt
);
2596 rt_set_gwroute(struct rtentry
*rt
, struct sockaddr
*dst
, struct rtentry
*gwrt
)
2598 boolean_t gwrt_isrouter
;
2600 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2601 RT_LOCK_ASSERT_HELD(rt
);
2604 RT_ADDREF(gwrt
); /* for this routine */
2607 * Get rid of existing gateway route; if rt_gwroute is already
2608 * set to gwrt, this is slightly redundant (though safe since
2609 * we held an extra ref above) but makes the code simpler.
2611 if (rt
->rt_gwroute
!= NULL
) {
2612 struct rtentry
*ogwrt
= rt
->rt_gwroute
;
2614 VERIFY(rt
!= ogwrt
); /* sanity check */
2615 rt
->rt_gwroute
= NULL
;
2617 rtfree_locked(ogwrt
);
2619 VERIFY(rt
->rt_gwroute
== NULL
);
2623 * And associate the new gateway route.
2625 if ((rt
->rt_gwroute
= gwrt
) != NULL
) {
2626 RT_ADDREF(gwrt
); /* for rt */
2628 if (rt
->rt_flags
& RTF_WASCLONED
) {
2629 /* rt_parent might be NULL if rt is embryonic */
2630 gwrt_isrouter
= (rt
->rt_parent
!= NULL
&&
2631 SA_DEFAULT(rt_key(rt
->rt_parent
)) &&
2632 !RT_HOST(rt
->rt_parent
));
2634 gwrt_isrouter
= (SA_DEFAULT(dst
) && !RT_HOST(rt
));
2637 /* If gwrt points to a default router, mark it accordingly */
2638 if (gwrt_isrouter
&& RT_HOST(gwrt
) &&
2639 !(gwrt
->rt_flags
& RTF_ROUTER
)) {
2641 gwrt
->rt_flags
|= RTF_ROUTER
;
2645 RT_REMREF(gwrt
); /* for this routine */
2650 rt_maskedcopy(struct sockaddr
*src
, struct sockaddr
*dst
,
2651 struct sockaddr
*netmask
)
2653 u_char
*cp1
= (u_char
*)src
;
2654 u_char
*cp2
= (u_char
*)dst
;
2655 u_char
*cp3
= (u_char
*)netmask
;
2656 u_char
*cplim
= cp2
+ *cp3
;
2657 u_char
*cplim2
= cp2
+ *cp1
;
2659 *cp2
++ = *cp1
++; *cp2
++ = *cp1
++; /* copies sa_len & sa_family */
2664 *cp2
++ = *cp1
++ & *cp3
++;
2666 bzero((caddr_t
)cp2
, (unsigned)(cplim2
- cp2
));
2670 * Lookup an AF_INET/AF_INET6 scoped or non-scoped route depending on the
2671 * ifscope value passed in by the caller (IFSCOPE_NONE implies non-scoped).
2673 static struct radix_node
*
2674 node_lookup(struct sockaddr
*dst
, struct sockaddr
*netmask
,
2675 unsigned int ifscope
)
2677 struct radix_node_head
*rnh
;
2678 struct radix_node
*rn
;
2679 struct sockaddr_storage ss
, mask
;
2680 int af
= dst
->sa_family
;
2681 struct matchleaf_arg ma
= { ifscope
};
2682 rn_matchf_t
*f
= rn_match_ifscope
;
2685 if (af
!= AF_INET
&& af
!= AF_INET6
)
2688 rnh
= rt_tables
[af
];
2691 * Transform dst into the internal routing table form,
2692 * clearing out the scope ID field if ifscope isn't set.
2694 dst
= sa_copy(dst
, &ss
, (ifscope
== IFSCOPE_NONE
) ? NULL
: &ifscope
);
2696 /* Transform netmask into the internal routing table form */
2697 if (netmask
!= NULL
)
2698 netmask
= ma_copy(af
, netmask
, &mask
, ifscope
);
2700 if (ifscope
== IFSCOPE_NONE
)
2703 rn
= rnh
->rnh_lookup_args(dst
, netmask
, rnh
, f
, w
);
2704 if (rn
!= NULL
&& (rn
->rn_flags
& RNF_ROOT
))
2711 * Lookup the AF_INET/AF_INET6 non-scoped default route.
2713 static struct radix_node
*
2714 node_lookup_default(int af
)
2716 struct radix_node_head
*rnh
;
2718 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
2719 rnh
= rt_tables
[af
];
2721 return (af
== AF_INET
? rnh
->rnh_lookup(&sin_def
, NULL
, rnh
) :
2722 rnh
->rnh_lookup(&sin6_def
, NULL
, rnh
));
2726 * Common routine to lookup/match a route. It invokes the lookup/matchaddr
2727 * callback which could be address family-specific. The main difference
2728 * between the two (at least for AF_INET/AF_INET6) is that a lookup does
2729 * not alter the expiring state of a route, whereas a match would unexpire
2730 * or revalidate the route.
2732 * The optional scope or interface index property of a route allows for a
2733 * per-interface route instance. This permits multiple route entries having
2734 * the same destination (but not necessarily the same gateway) to exist in
2735 * the routing table; each of these entries is specific to the corresponding
2736 * interface. This is made possible by storing the scope ID value into the
2737 * radix key, thus making each route entry unique. These scoped entries
2738 * exist along with the regular, non-scoped entries in the same radix tree
2739 * for a given address family (AF_INET/AF_INET6); the scope logically
2740 * partitions it into multiple per-interface sub-trees.
2742 * When a scoped route lookup is performed, the routing table is searched for
2743 * the best match that would result in a route using the same interface as the
2744 * one associated with the scope (the exception to this are routes that point
2745 * to the loopback interface). The search rule follows the longest matching
2746 * prefix with the additional interface constraint.
2748 static struct rtentry
*
2749 rt_lookup_common(boolean_t lookup_only
, boolean_t coarse
, struct sockaddr
*dst
,
2750 struct sockaddr
*netmask
, struct radix_node_head
*rnh
, unsigned int ifscope
)
2752 struct radix_node
*rn0
, *rn
;
2754 int af
= dst
->sa_family
;
2755 struct sockaddr_storage dst_ss
, mask_ss
;
2757 VERIFY(!coarse
|| ifscope
== IFSCOPE_NONE
);
2759 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2762 * While we have rnh_lock held, see if we need to schedule the timer.
2764 if (nd6_sched_timeout_want
)
2765 nd6_sched_timeout(NULL
, NULL
);
2772 * Non-scoped route lookup.
2775 if ((af
!= AF_INET
&& af
!= AF_INET6
) ||
2776 (af
== AF_INET
&& !ip_doscopedroute
) ||
2777 (af
== AF_INET6
&& !ip6_doscopedroute
)) {
2779 if (af
!= AF_INET
|| !ip_doscopedroute
) {
2781 rn
= rnh
->rnh_matchaddr(dst
, rnh
);
2784 * Don't return a root node; also, rnh_matchaddr callback
2785 * would have done the necessary work to clear RTPRF_OURS
2786 * for certain protocol families.
2788 if (rn
!= NULL
&& (rn
->rn_flags
& RNF_ROOT
))
2791 RT_LOCK_SPIN(RT(rn
));
2792 if (!(RT(rn
)->rt_flags
& RTF_CONDEMNED
)) {
2793 RT_ADDREF_LOCKED(RT(rn
));
2803 /* Transform dst/netmask into the internal routing table form */
2804 dst
= sa_copy(dst
, &dst_ss
, &ifscope
);
2805 if (netmask
!= NULL
)
2806 netmask
= ma_copy(af
, netmask
, &mask_ss
, ifscope
);
2807 dontcare
= (ifscope
== IFSCOPE_NONE
);
2810 * Scoped route lookup:
2812 * We first perform a non-scoped lookup for the original result.
2813 * Afterwards, depending on whether or not the caller has specified
2814 * a scope, we perform a more specific scoped search and fallback
2815 * to this original result upon failure.
2817 rn0
= rn
= node_lookup(dst
, netmask
, IFSCOPE_NONE
);
2820 * If the caller did not specify a scope, use the primary scope
2821 * derived from the system's non-scoped default route. If, for
2822 * any reason, there is no primary interface, ifscope will be
2823 * set to IFSCOPE_NONE; if the above lookup resulted in a route,
2824 * we'll do a more-specific search below, scoped to the interface
2828 ifscope
= get_primary_ifscope(af
);
2831 * Keep the original result if either of the following is true:
2833 * 1) The interface portion of the route has the same interface
2834 * index as the scope value and it is marked with RTF_IFSCOPE.
2835 * 2) The route uses the loopback interface, in which case the
2836 * destination (host/net) is local/loopback.
2838 * Otherwise, do a more specified search using the scope;
2839 * we're holding rnh_lock now, so rt_ifp should not change.
2842 struct rtentry
*rt
= RT(rn
);
2843 if (!(rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
2844 if (rt
->rt_ifp
->if_index
!= ifscope
) {
2846 * Wrong interface; keep the original result
2847 * only if the caller did not specify a scope,
2848 * and do a more specific scoped search using
2849 * the scope of the found route. Otherwise,
2850 * start again from scratch.
2854 ifscope
= rt
->rt_ifp
->if_index
;
2857 } else if (!(rt
->rt_flags
& RTF_IFSCOPE
)) {
2859 * Right interface, except that this route
2860 * isn't marked with RTF_IFSCOPE. Do a more
2861 * specific scoped search. Keep the original
2862 * result and return it it in case the scoped
2871 * Scoped search. Find the most specific entry having the same
2872 * interface scope as the one requested. The following will result
2873 * in searching for the longest prefix scoped match.
2876 rn
= node_lookup(dst
, netmask
, ifscope
);
2879 * Use the original result if either of the following is true:
2881 * 1) The scoped search did not yield any result.
2882 * 2) The caller insists on performing a coarse-grained lookup.
2883 * 3) The result from the scoped search is a scoped default route,
2884 * and the original (non-scoped) result is not a default route,
2885 * i.e. the original result is a more specific host/net route.
2886 * 4) The scoped search yielded a net route but the original
2887 * result is a host route, i.e. the original result is treated
2888 * as a more specific route.
2890 if (rn
== NULL
|| coarse
|| (rn0
!= NULL
&&
2891 ((SA_DEFAULT(rt_key(RT(rn
))) && !SA_DEFAULT(rt_key(RT(rn0
)))) ||
2892 (!RT_HOST(rn
) && RT_HOST(rn0
)))))
2896 * If we still don't have a route, use the non-scoped default
2897 * route as long as the interface portion satistifes the scope.
2899 if (rn
== NULL
&& (rn
= node_lookup_default(af
)) != NULL
&&
2900 RT(rn
)->rt_ifp
->if_index
!= ifscope
)
2905 * Manually clear RTPRF_OURS using rt_validate() and
2906 * bump up the reference count after, and not before;
2907 * we only get here for AF_INET/AF_INET6. node_lookup()
2908 * has done the check against RNF_ROOT, so we can be sure
2909 * that we're not returning a root node here.
2911 RT_LOCK_SPIN(RT(rn
));
2912 if (rt_validate(RT(rn
))) {
2913 RT_ADDREF_LOCKED(RT(rn
));
2925 rt_lookup(boolean_t lookup_only
, struct sockaddr
*dst
, struct sockaddr
*netmask
,
2926 struct radix_node_head
*rnh
, unsigned int ifscope
)
2928 return (rt_lookup_common(lookup_only
, FALSE
, dst
, netmask
,
2933 rt_lookup_coarse(boolean_t lookup_only
, struct sockaddr
*dst
,
2934 struct sockaddr
*netmask
, struct radix_node_head
*rnh
)
2936 return (rt_lookup_common(lookup_only
, TRUE
, dst
, netmask
,
2937 rnh
, IFSCOPE_NONE
));
2941 rt_validate(struct rtentry
*rt
)
2943 RT_LOCK_ASSERT_HELD(rt
);
2945 if ((rt
->rt_flags
& (RTF_UP
| RTF_CONDEMNED
)) == RTF_UP
) {
2946 int af
= rt_key(rt
)->sa_family
;
2949 (void) in_validate(RN(rt
));
2950 else if (af
== AF_INET6
)
2951 (void) in6_validate(RN(rt
));
2956 return (rt
!= NULL
);
2960 * Set up a routing table entry, normally
2964 rtinit(struct ifaddr
*ifa
, int cmd
, int flags
)
2968 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2970 lck_mtx_lock(rnh_lock
);
2971 error
= rtinit_locked(ifa
, cmd
, flags
);
2972 lck_mtx_unlock(rnh_lock
);
2978 rtinit_locked(struct ifaddr
*ifa
, int cmd
, int flags
)
2980 struct radix_node_head
*rnh
;
2981 uint8_t nbuf
[128]; /* long enough for IPv6 */
2982 char dbuf
[MAX_IPv6_STR_LEN
], gbuf
[MAX_IPv6_STR_LEN
];
2983 char abuf
[MAX_IPv6_STR_LEN
];
2984 struct rtentry
*rt
= NULL
;
2985 struct sockaddr
*dst
;
2986 struct sockaddr
*netmask
;
2990 * Holding rnh_lock here prevents the possibility of ifa from
2991 * changing (e.g. in_ifinit), so it is safe to access its
2992 * ifa_{dst}addr (here and down below) without locking.
2994 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2996 if (flags
& RTF_HOST
) {
2997 dst
= ifa
->ifa_dstaddr
;
3000 dst
= ifa
->ifa_addr
;
3001 netmask
= ifa
->ifa_netmask
;
3004 if (dst
->sa_len
== 0) {
3005 log(LOG_ERR
, "%s: %s failed, invalid dst sa_len %d\n",
3006 __func__
, rtm2str(cmd
), dst
->sa_len
);
3010 if (netmask
!= NULL
&& netmask
->sa_len
> sizeof (nbuf
)) {
3011 log(LOG_ERR
, "%s: %s failed, mask sa_len %d too large\n",
3012 __func__
, rtm2str(cmd
), dst
->sa_len
);
3017 if (dst
->sa_family
== AF_INET
) {
3018 (void) inet_ntop(AF_INET
, &SIN(dst
)->sin_addr
.s_addr
,
3019 abuf
, sizeof (abuf
));
3022 else if (dst
->sa_family
== AF_INET6
) {
3023 (void) inet_ntop(AF_INET6
, &SIN6(dst
)->sin6_addr
,
3024 abuf
, sizeof (abuf
));
3028 if ((rnh
= rt_tables
[dst
->sa_family
]) == NULL
) {
3034 * If it's a delete, check that if it exists, it's on the correct
3035 * interface or we might scrub a route to another ifa which would
3036 * be confusing at best and possibly worse.
3038 if (cmd
== RTM_DELETE
) {
3040 * It's a delete, so it should already exist..
3041 * If it's a net, mask off the host bits
3042 * (Assuming we have a mask)
3044 if (netmask
!= NULL
) {
3045 rt_maskedcopy(dst
, SA(nbuf
), netmask
);
3049 * Get an rtentry that is in the routing tree and contains
3050 * the correct info. Note that we perform a coarse-grained
3051 * lookup here, in case there is a scoped variant of the
3052 * subnet/prefix route which we should ignore, as we never
3053 * add a scoped subnet/prefix route as part of adding an
3054 * interface address.
3056 rt
= rt_lookup_coarse(TRUE
, dst
, NULL
, rnh
);
3058 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
3060 * Ok so we found the rtentry. it has an extra reference
3061 * for us at this stage. we won't need that so
3065 if (rt
->rt_ifa
!= ifa
) {
3067 * If the interface address in the rtentry
3068 * doesn't match the interface we are using,
3069 * then we don't want to delete it, so return
3070 * an error. This seems to be the only point
3071 * of this whole RTM_DELETE clause.
3074 log(LOG_DEBUG
, "%s: not removing "
3075 "route to %s->%s->%s, flags %b, "
3076 "ifaddr %s, rt_ifa 0x%llx != "
3077 "ifa 0x%llx\n", __func__
, dbuf
,
3078 gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3079 rt
->rt_ifp
->if_xname
: ""),
3080 rt
->rt_flags
, RTF_BITS
, abuf
,
3081 (uint64_t)VM_KERNEL_ADDRPERM(
3083 (uint64_t)VM_KERNEL_ADDRPERM(ifa
));
3085 RT_REMREF_LOCKED(rt
);
3088 error
= ((flags
& RTF_HOST
) ?
3089 EHOSTUNREACH
: ENETUNREACH
);
3091 } else if (rt
->rt_flags
& RTF_STATIC
) {
3093 * Don't remove the subnet/prefix route if
3094 * this was manually added from above.
3097 log(LOG_DEBUG
, "%s: not removing "
3098 "static route to %s->%s->%s, "
3099 "flags %b, ifaddr %s\n", __func__
,
3100 dbuf
, gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3101 rt
->rt_ifp
->if_xname
: ""),
3102 rt
->rt_flags
, RTF_BITS
, abuf
);
3104 RT_REMREF_LOCKED(rt
);
3111 log(LOG_DEBUG
, "%s: removing route to "
3112 "%s->%s->%s, flags %b, ifaddr %s\n",
3113 __func__
, dbuf
, gbuf
,
3114 ((rt
->rt_ifp
!= NULL
) ?
3115 rt
->rt_ifp
->if_xname
: ""),
3116 rt
->rt_flags
, RTF_BITS
, abuf
);
3118 RT_REMREF_LOCKED(rt
);
3124 * Do the actual request
3126 if ((error
= rtrequest_locked(cmd
, dst
, ifa
->ifa_addr
, netmask
,
3127 flags
| ifa
->ifa_flags
, &rt
)) != 0)
3132 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
3137 * If we are deleting, and we found an entry, then it's
3138 * been removed from the tree. Notify any listening
3139 * routing agents of the change and throw it away.
3142 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
3145 log(LOG_DEBUG
, "%s: removed route to %s->%s->%s, "
3146 "flags %b, ifaddr %s\n", __func__
, dbuf
, gbuf
,
3147 ((rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: ""),
3148 rt
->rt_flags
, RTF_BITS
, abuf
);
3155 * We are adding, and we have a returned routing entry.
3156 * We need to sanity check the result. If it came back
3157 * with an unexpected interface, then it must have already
3158 * existed or something.
3161 if (rt
->rt_ifa
!= ifa
) {
3162 void (*ifa_rtrequest
)
3163 (int, struct rtentry
*, struct sockaddr
*);
3165 if (!(rt
->rt_ifa
->ifa_ifp
->if_flags
&
3166 (IFF_POINTOPOINT
|IFF_LOOPBACK
))) {
3167 log(LOG_ERR
, "%s: %s route to %s->%s->%s, "
3168 "flags %b, ifaddr %s, rt_ifa 0x%llx != "
3169 "ifa 0x%llx\n", __func__
, rtm2str(cmd
),
3170 dbuf
, gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3171 rt
->rt_ifp
->if_xname
: ""), rt
->rt_flags
,
3173 (uint64_t)VM_KERNEL_ADDRPERM(rt
->rt_ifa
),
3174 (uint64_t)VM_KERNEL_ADDRPERM(ifa
));
3178 log(LOG_DEBUG
, "%s: %s route to %s->%s->%s, "
3179 "flags %b, ifaddr %s, rt_ifa was 0x%llx "
3180 "now 0x%llx\n", __func__
, rtm2str(cmd
),
3181 dbuf
, gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3182 rt
->rt_ifp
->if_xname
: ""), rt
->rt_flags
,
3184 (uint64_t)VM_KERNEL_ADDRPERM(rt
->rt_ifa
),
3185 (uint64_t)VM_KERNEL_ADDRPERM(ifa
));
3189 * Ask that the protocol in question
3190 * remove anything it has associated with
3191 * this route and ifaddr.
3193 ifa_rtrequest
= rt
->rt_ifa
->ifa_rtrequest
;
3194 if (ifa_rtrequest
!= NULL
)
3195 ifa_rtrequest(RTM_DELETE
, rt
, NULL
);
3197 * Set the route's ifa.
3201 if (rt
->rt_ifp
!= ifa
->ifa_ifp
) {
3203 * Purge any link-layer info caching.
3205 if (rt
->rt_llinfo_purge
!= NULL
)
3206 rt
->rt_llinfo_purge(rt
);
3208 * Adjust route ref count for the interfaces.
3210 if (rt
->rt_if_ref_fn
!= NULL
) {
3211 rt
->rt_if_ref_fn(ifa
->ifa_ifp
, 1);
3212 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
3217 * And substitute in references to the ifaddr
3220 rt
->rt_ifp
= ifa
->ifa_ifp
;
3222 * If rmx_mtu is not locked, update it
3223 * to the MTU used by the new interface.
3225 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
3226 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
3229 * Now ask the protocol to check if it needs
3230 * any special processing in its new form.
3232 ifa_rtrequest
= ifa
->ifa_rtrequest
;
3233 if (ifa_rtrequest
!= NULL
)
3234 ifa_rtrequest(RTM_ADD
, rt
, NULL
);
3237 log(LOG_DEBUG
, "%s: added route to %s->%s->%s, "
3238 "flags %b, ifaddr %s\n", __func__
, dbuf
,
3239 gbuf
, ((rt
->rt_ifp
!= NULL
) ?
3240 rt
->rt_ifp
->if_xname
: ""), rt
->rt_flags
,
3245 * notify any listenning routing agents of the change
3247 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
3249 * We just wanted to add it; we don't actually need a
3250 * reference. This will result in a route that's added
3251 * to the routing table without a reference count. The
3252 * RTM_DELETE code will do the necessary step to adjust
3253 * the reference count at deletion time.
3255 RT_REMREF_LOCKED(rt
);
3268 rt_set_idleref(struct rtentry
*rt
)
3270 RT_LOCK_ASSERT_HELD(rt
);
3273 * We currently keep idle refcnt only on unicast cloned routes
3274 * that aren't marked with RTF_NOIFREF.
3276 if (rt
->rt_parent
!= NULL
&& !(rt
->rt_flags
&
3277 (RTF_NOIFREF
|RTF_BROADCAST
| RTF_MULTICAST
)) &&
3278 (rt
->rt_flags
& (RTF_UP
|RTF_WASCLONED
|RTF_IFREF
)) ==
3279 (RTF_UP
|RTF_WASCLONED
)) {
3280 rt_clear_idleref(rt
); /* drop existing refcnt if any */
3281 rt
->rt_if_ref_fn
= rte_if_ref
;
3282 /* Become a regular mutex, just in case */
3283 RT_CONVERT_LOCK(rt
);
3284 rt
->rt_if_ref_fn(rt
->rt_ifp
, 1);
3285 rt
->rt_flags
|= RTF_IFREF
;
3290 rt_clear_idleref(struct rtentry
*rt
)
3292 RT_LOCK_ASSERT_HELD(rt
);
3294 if (rt
->rt_if_ref_fn
!= NULL
) {
3295 VERIFY((rt
->rt_flags
& (RTF_NOIFREF
| RTF_IFREF
)) == RTF_IFREF
);
3296 /* Become a regular mutex, just in case */
3297 RT_CONVERT_LOCK(rt
);
3298 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
3299 rt
->rt_flags
&= ~RTF_IFREF
;
3300 rt
->rt_if_ref_fn
= NULL
;
3305 rt_set_proxy(struct rtentry
*rt
, boolean_t set
)
3307 lck_mtx_lock(rnh_lock
);
3310 * Search for any cloned routes which might have
3311 * been formed from this node, and delete them.
3313 if (rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) {
3314 struct radix_node_head
*rnh
= rt_tables
[rt_key(rt
)->sa_family
];
3317 rt
->rt_flags
|= RTF_PROXY
;
3319 rt
->rt_flags
&= ~RTF_PROXY
;
3322 if (rnh
!= NULL
&& rt_mask(rt
)) {
3323 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
3329 lck_mtx_unlock(rnh_lock
);
3333 rte_lock_init(struct rtentry
*rt
)
3335 lck_mtx_init(&rt
->rt_lock
, rte_mtx_grp
, rte_mtx_attr
);
3339 rte_lock_destroy(struct rtentry
*rt
)
3341 RT_LOCK_ASSERT_NOTHELD(rt
);
3342 lck_mtx_destroy(&rt
->rt_lock
, rte_mtx_grp
);
3346 rt_lock(struct rtentry
*rt
, boolean_t spin
)
3348 RT_LOCK_ASSERT_NOTHELD(rt
);
3350 lck_mtx_lock_spin(&rt
->rt_lock
);
3352 lck_mtx_lock(&rt
->rt_lock
);
3353 if (rte_debug
& RTD_DEBUG
)
3354 rte_lock_debug((struct rtentry_dbg
*)rt
);
3358 rt_unlock(struct rtentry
*rt
)
3360 if (rte_debug
& RTD_DEBUG
)
3361 rte_unlock_debug((struct rtentry_dbg
*)rt
);
3362 lck_mtx_unlock(&rt
->rt_lock
);
3367 rte_lock_debug(struct rtentry_dbg
*rte
)
3371 RT_LOCK_ASSERT_HELD((struct rtentry
*)rte
);
3372 idx
= atomic_add_32_ov(&rte
->rtd_lock_cnt
, 1) % CTRACE_HIST_SIZE
;
3373 if (rte_debug
& RTD_TRACE
)
3374 ctrace_record(&rte
->rtd_lock
[idx
]);
3378 rte_unlock_debug(struct rtentry_dbg
*rte
)
3382 RT_LOCK_ASSERT_HELD((struct rtentry
*)rte
);
3383 idx
= atomic_add_32_ov(&rte
->rtd_unlock_cnt
, 1) % CTRACE_HIST_SIZE
;
3384 if (rte_debug
& RTD_TRACE
)
3385 ctrace_record(&rte
->rtd_unlock
[idx
]);
3388 static struct rtentry
*
3391 if (rte_debug
& RTD_DEBUG
)
3392 return (rte_alloc_debug());
3394 return ((struct rtentry
*)zalloc(rte_zone
));
3398 rte_free(struct rtentry
*p
)
3400 if (rte_debug
& RTD_DEBUG
) {
3405 if (p
->rt_refcnt
!= 0) {
3406 panic("rte_free: rte=%p refcnt=%d non-zero\n", p
, p
->rt_refcnt
);
3413 rte_if_ref(struct ifnet
*ifp
, int cnt
)
3415 struct kev_msg ev_msg
;
3416 struct net_event_data ev_data
;
3419 /* Force cnt to 1 increment/decrement */
3420 if (cnt
< -1 || cnt
> 1) {
3421 panic("%s: invalid count argument (%d)", __func__
, cnt
);
3424 old
= atomic_add_32_ov(&ifp
->if_route_refcnt
, cnt
);
3425 if (cnt
< 0 && old
== 0) {
3426 panic("%s: ifp=%p negative route refcnt!", __func__
, ifp
);
3430 * The following is done without first holding the ifnet lock,
3431 * for performance reasons. The relevant ifnet fields, with
3432 * the exception of the if_idle_flags, are never changed
3433 * during the lifetime of the ifnet. The if_idle_flags
3434 * may possibly be modified, so in the event that the value
3435 * is stale because IFRF_IDLE_NOTIFY was cleared, we'd end up
3436 * sending the event anyway. This is harmless as it is just
3437 * a notification to the monitoring agent in user space, and
3438 * it is expected to check via SIOCGIFGETRTREFCNT again anyway.
3440 if ((ifp
->if_idle_flags
& IFRF_IDLE_NOTIFY
) && cnt
< 0 && old
== 1) {
3441 bzero(&ev_msg
, sizeof (ev_msg
));
3442 bzero(&ev_data
, sizeof (ev_data
));
3444 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3445 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3446 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3447 ev_msg
.event_code
= KEV_DL_IF_IDLE_ROUTE_REFCNT
;
3449 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3451 ev_data
.if_family
= ifp
->if_family
;
3452 ev_data
.if_unit
= ifp
->if_unit
;
3453 ev_msg
.dv
[0].data_length
= sizeof (struct net_event_data
);
3454 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3456 kev_post_msg(&ev_msg
);
3460 static inline struct rtentry
*
3461 rte_alloc_debug(void)
3463 struct rtentry_dbg
*rte
;
3465 rte
= ((struct rtentry_dbg
*)zalloc(rte_zone
));
3467 bzero(rte
, sizeof (*rte
));
3468 if (rte_debug
& RTD_TRACE
)
3469 ctrace_record(&rte
->rtd_alloc
);
3470 rte
->rtd_inuse
= RTD_INUSE
;
3472 return ((struct rtentry
*)rte
);
3476 rte_free_debug(struct rtentry
*p
)
3478 struct rtentry_dbg
*rte
= (struct rtentry_dbg
*)p
;
3480 if (p
->rt_refcnt
!= 0) {
3481 panic("rte_free: rte=%p refcnt=%d\n", p
, p
->rt_refcnt
);
3484 if (rte
->rtd_inuse
== RTD_FREED
) {
3485 panic("rte_free: double free rte=%p\n", rte
);
3487 } else if (rte
->rtd_inuse
!= RTD_INUSE
) {
3488 panic("rte_free: corrupted rte=%p\n", rte
);
3491 bcopy((caddr_t
)p
, (caddr_t
)&rte
->rtd_entry_saved
, sizeof (*p
));
3492 /* Preserve rt_lock to help catch use-after-free cases */
3493 bzero((caddr_t
)p
, offsetof(struct rtentry
, rt_lock
));
3495 rte
->rtd_inuse
= RTD_FREED
;
3497 if (rte_debug
& RTD_TRACE
)
3498 ctrace_record(&rte
->rtd_free
);
3500 if (!(rte_debug
& RTD_NO_FREE
))
3505 ctrace_record(ctrace_t
*tr
)
3507 tr
->th
= current_thread();
3508 bzero(tr
->pc
, sizeof (tr
->pc
));
3509 (void) OSBacktrace(tr
->pc
, CTRACE_STACK_SIZE
);
3513 route_copyout(struct route
*dst
, const struct route
*src
, size_t length
)
3515 /* Copy everything (rt, srcif, flags, dst) from src */
3516 bcopy(src
, dst
, length
);
3518 /* Hold one reference for the local copy of struct route */
3519 if (dst
->ro_rt
!= NULL
)
3520 RT_ADDREF(dst
->ro_rt
);
3522 /* Hold one reference for the local copy of struct ifaddr */
3523 if (dst
->ro_srcia
!= NULL
)
3524 IFA_ADDREF(dst
->ro_srcia
);
3528 route_copyin(struct route
*src
, struct route
*dst
, size_t length
)
3530 /* No cached route at the destination? */
3531 if (dst
->ro_rt
== NULL
) {
3533 * Ditch the address in the cached copy (dst) since
3534 * we're about to take everything there is in src.
3536 if (dst
->ro_srcia
!= NULL
)
3537 IFA_REMREF(dst
->ro_srcia
);
3539 * Copy everything (rt, srcia, flags, dst) from src; the
3540 * references to rt and/or srcia were held at the time
3541 * of storage and are kept intact.
3543 bcopy(src
, dst
, length
);
3544 } else if (src
->ro_rt
!= NULL
) {
3546 * If the same, update srcia and flags, and ditch the route
3547 * in the local copy. Else ditch the one that is currently
3548 * cached, and cache the new route.
3550 if (dst
->ro_rt
== src
->ro_rt
) {
3551 dst
->ro_flags
= src
->ro_flags
;
3552 if (dst
->ro_srcia
!= src
->ro_srcia
) {
3553 if (dst
->ro_srcia
!= NULL
)
3554 IFA_REMREF(dst
->ro_srcia
);
3555 dst
->ro_srcia
= src
->ro_srcia
;
3556 } else if (src
->ro_srcia
!= NULL
) {
3557 IFA_REMREF(src
->ro_srcia
);
3562 if (dst
->ro_srcia
!= NULL
)
3563 IFA_REMREF(dst
->ro_srcia
);
3564 bcopy(src
, dst
, length
);
3566 } else if (src
->ro_srcia
!= NULL
) {
3568 * Ditch src address in the local copy (src) since we're
3569 * not caching the route entry anyway (ro_rt is NULL).
3571 IFA_REMREF(src
->ro_srcia
);
3574 /* This function consumes the references on src */
3576 src
->ro_srcia
= NULL
;
3580 * route_to_gwroute will find the gateway route for a given route.
3582 * If the route is down, look the route up again.
3583 * If the route goes through a gateway, get the route to the gateway.
3584 * If the gateway route is down, look it up again.
3585 * If the route is set to reject, verify it hasn't expired.
3587 * If the returned route is non-NULL, the caller is responsible for
3588 * releasing the reference and unlocking the route.
3590 #define senderr(e) { error = (e); goto bad; }
3592 route_to_gwroute(const struct sockaddr
*net_dest
, struct rtentry
*hint0
,
3593 struct rtentry
**out_route
)
3596 struct rtentry
*rt
= hint0
, *hint
= hint0
;
3598 unsigned int ifindex
;
3607 * Next hop determination. Because we may involve the gateway route
3608 * in addition to the original route, locking is rather complicated.
3609 * The general concept is that regardless of whether the route points
3610 * to the original route or to the gateway route, this routine takes
3611 * an extra reference on such a route. This extra reference will be
3612 * released at the end.
3614 * Care must be taken to ensure that the "hint0" route never gets freed
3615 * via rtfree(), since the caller may have stored it inside a struct
3616 * route with a reference held for that placeholder.
3619 ifindex
= rt
->rt_ifp
->if_index
;
3620 RT_ADDREF_LOCKED(rt
);
3621 if (!(rt
->rt_flags
& RTF_UP
)) {
3622 RT_REMREF_LOCKED(rt
);
3624 /* route is down, find a new one */
3625 hint
= rt
= rtalloc1_scoped((struct sockaddr
*)
3626 (size_t)net_dest
, 1, 0, ifindex
);
3629 ifindex
= rt
->rt_ifp
->if_index
;
3631 senderr(EHOSTUNREACH
);
3636 * We have a reference to "rt" by now; it will either
3637 * be released or freed at the end of this routine.
3639 RT_LOCK_ASSERT_HELD(rt
);
3640 if ((gwroute
= (rt
->rt_flags
& RTF_GATEWAY
))) {
3641 struct rtentry
*gwrt
= rt
->rt_gwroute
;
3642 struct sockaddr_storage ss
;
3643 struct sockaddr
*gw
= (struct sockaddr
*)&ss
;
3646 RT_ADDREF_LOCKED(hint
);
3648 /* If there's no gateway rt, look it up */
3650 bcopy(rt
->rt_gateway
, gw
, MIN(sizeof (ss
),
3651 rt
->rt_gateway
->sa_len
));
3655 /* Become a regular mutex */
3656 RT_CONVERT_LOCK(rt
);
3659 * Take gwrt's lock while holding route's lock;
3660 * this is okay since gwrt never points back
3661 * to "rt", so no lock ordering issues.
3664 if (!(gwrt
->rt_flags
& RTF_UP
)) {
3665 rt
->rt_gwroute
= NULL
;
3667 bcopy(rt
->rt_gateway
, gw
, MIN(sizeof (ss
),
3668 rt
->rt_gateway
->sa_len
));
3672 lck_mtx_lock(rnh_lock
);
3673 gwrt
= rtalloc1_scoped_locked(gw
, 1, 0, ifindex
);
3677 * Bail out if the route is down, no route
3678 * to gateway, circular route, or if the
3679 * gateway portion of "rt" has changed.
3681 if (!(rt
->rt_flags
& RTF_UP
) || gwrt
== NULL
||
3682 gwrt
== rt
|| !equal(gw
, rt
->rt_gateway
)) {
3684 RT_REMREF_LOCKED(gwrt
);
3688 RT_REMREF_LOCKED(hint
);
3692 rtfree_locked(gwrt
);
3693 lck_mtx_unlock(rnh_lock
);
3694 senderr(EHOSTUNREACH
);
3696 VERIFY(gwrt
!= NULL
);
3698 * Set gateway route; callee adds ref to gwrt;
3699 * gwrt has an extra ref from rtalloc1() for
3702 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
3704 RT_REMREF_LOCKED(rt
); /* hint still holds a refcnt */
3706 lck_mtx_unlock(rnh_lock
);
3709 RT_ADDREF_LOCKED(gwrt
);
3712 RT_REMREF_LOCKED(rt
); /* hint still holds a refcnt */
3716 VERIFY(rt
== gwrt
&& rt
!= hint
);
3719 * This is an opportunity to revalidate the parent route's
3720 * rt_gwroute, in case it now points to a dead route entry.
3721 * Parent route won't go away since the clone (hint) holds
3722 * a reference to it. rt == gwrt.
3725 if ((hint
->rt_flags
& (RTF_WASCLONED
| RTF_UP
)) ==
3726 (RTF_WASCLONED
| RTF_UP
)) {
3727 struct rtentry
*prt
= hint
->rt_parent
;
3728 VERIFY(prt
!= NULL
);
3730 RT_CONVERT_LOCK(hint
);
3733 rt_revalidate_gwroute(prt
, rt
);
3739 /* Clean up "hint" now; see notes above regarding hint0 */
3746 /* rt == gwrt; if it is now down, give up */
3748 if (!(rt
->rt_flags
& RTF_UP
)) {
3750 senderr(EHOSTUNREACH
);
3754 if (rt
->rt_flags
& RTF_REJECT
) {
3755 VERIFY(rt
->rt_expire
== 0 || rt
->rt_rmx
.rmx_expire
!= 0);
3756 VERIFY(rt
->rt_expire
!= 0 || rt
->rt_rmx
.rmx_expire
== 0);
3757 timenow
= net_uptime();
3758 if (rt
->rt_expire
== 0 || timenow
< rt
->rt_expire
) {
3760 senderr(!gwroute
? EHOSTDOWN
: EHOSTUNREACH
);
3764 /* Become a regular mutex */
3765 RT_CONVERT_LOCK(rt
);
3767 /* Caller is responsible for cleaning up "rt" */
3772 /* Clean up route (either it is "rt" or "gwrt") */
3776 RT_REMREF_LOCKED(rt
);
3788 rt_revalidate_gwroute(struct rtentry
*rt
, struct rtentry
*gwrt
)
3790 VERIFY(rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
));
3791 VERIFY(gwrt
!= NULL
);
3794 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_UP
)) == (RTF_GATEWAY
| RTF_UP
) &&
3795 rt
->rt_ifp
== gwrt
->rt_ifp
&& rt
->rt_gateway
->sa_family
==
3796 rt_key(gwrt
)->sa_family
&& (rt
->rt_gwroute
== NULL
||
3797 !(rt
->rt_gwroute
->rt_flags
& RTF_UP
))) {
3800 if (rt
->rt_gateway
->sa_family
== AF_INET
||
3801 rt
->rt_gateway
->sa_family
== AF_INET6
) {
3802 struct sockaddr_storage key_ss
, gw_ss
;
3804 * We need to compare rt_key and rt_gateway; create
3805 * local copies to get rid of any ifscope association.
3807 (void) sa_copy(rt_key(gwrt
), &key_ss
, NULL
);
3808 (void) sa_copy(rt
->rt_gateway
, &gw_ss
, NULL
);
3810 isequal
= equal(SA(&key_ss
), SA(&gw_ss
));
3812 isequal
= equal(rt_key(gwrt
), rt
->rt_gateway
);
3815 /* If they are the same, update gwrt */
3818 lck_mtx_lock(rnh_lock
);
3820 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
3822 lck_mtx_unlock(rnh_lock
);
3832 rt_str4(struct rtentry
*rt
, char *ds
, uint32_t dslen
, char *gs
, uint32_t gslen
)
3834 VERIFY(rt_key(rt
)->sa_family
== AF_INET
);
3837 (void) inet_ntop(AF_INET
,
3838 &SIN(rt_key(rt
))->sin_addr
.s_addr
, ds
, dslen
);
3840 if (rt
->rt_flags
& RTF_GATEWAY
) {
3841 (void) inet_ntop(AF_INET
,
3842 &SIN(rt
->rt_gateway
)->sin_addr
.s_addr
, gs
, gslen
);
3843 } else if (rt
->rt_ifp
!= NULL
) {
3844 snprintf(gs
, gslen
, "link#%u", rt
->rt_ifp
->if_unit
);
3846 snprintf(gs
, gslen
, "%s", "link");
3853 rt_str6(struct rtentry
*rt
, char *ds
, uint32_t dslen
, char *gs
, uint32_t gslen
)
3855 VERIFY(rt_key(rt
)->sa_family
== AF_INET6
);
3858 (void) inet_ntop(AF_INET6
,
3859 &SIN6(rt_key(rt
))->sin6_addr
, ds
, dslen
);
3861 if (rt
->rt_flags
& RTF_GATEWAY
) {
3862 (void) inet_ntop(AF_INET6
,
3863 &SIN6(rt
->rt_gateway
)->sin6_addr
, gs
, gslen
);
3864 } else if (rt
->rt_ifp
!= NULL
) {
3865 snprintf(gs
, gslen
, "link#%u", rt
->rt_ifp
->if_unit
);
3867 snprintf(gs
, gslen
, "%s", "link");
3875 rt_str(struct rtentry
*rt
, char *ds
, uint32_t dslen
, char *gs
, uint32_t gslen
)
3877 switch (rt_key(rt
)->sa_family
) {
3879 rt_str4(rt
, ds
, dslen
, gs
, gslen
);
3883 rt_str6(rt
, ds
, dslen
, gs
, gslen
);