2 * Copyright (c) 2000-2008 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/systm.h>
66 #include <sys/malloc.h>
68 #include <sys/socket.h>
69 #include <sys/domain.h>
70 #include <sys/syslog.h>
71 #include <sys/queue.h>
72 #include <kern/lock.h>
73 #include <kern/zalloc.h>
76 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip_mroute.h>
81 #include <netinet/ip_var.h>
83 #include <net/if_dl.h>
85 #include <libkern/OSAtomic.h>
86 #include <libkern/OSDebug.h>
88 #include <pexpert/pexpert.h>
90 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
91 #define SA(p) ((struct sockaddr *)(p))
93 extern void kdp_set_gateway_mac (void *gatewaymac
);
95 extern struct domain routedomain
;
96 struct route_cb route_cb
;
97 __private_extern__
struct rtstat rtstat
= { 0, 0, 0, 0, 0 };
98 struct radix_node_head
*rt_tables
[AF_MAX
+1];
100 lck_mtx_t
*rt_mtx
; /*### global routing tables mutex for now */
101 lck_attr_t
*rt_mtx_attr
;
102 lck_grp_t
*rt_mtx_grp
;
103 lck_grp_attr_t
*rt_mtx_grp_attr
;
105 lck_mtx_t
*route_domain_mtx
; /*### global routing tables mutex for now */
106 int rttrash
= 0; /* routes not in table but not freed */
108 static unsigned int rte_debug
;
110 /* Possible flags for rte_debug */
111 #define RTD_DEBUG 0x1 /* enable or disable rtentry debug facility */
112 #define RTD_TRACE 0x2 /* trace alloc, free and refcnt */
113 #define RTD_NO_FREE 0x4 /* don't free (good to catch corruptions) */
115 static struct zone
*rte_zone
; /* special zone for rtentry */
116 #define RTE_ZONE_MAX 65536 /* maximum elements in zone */
117 #define RTE_ZONE_NAME "rtentry" /* name of rtentry zone */
119 #define RTD_INUSE 0xFEEDFACE /* entry is in use */
120 #define RTD_FREED 0xDEADBEEF /* entry is freed */
122 #define RTD_TRSTACK_SIZE 8 /* depth of stack trace */
123 #define RTD_REFHIST_SIZE 4 /* refcnt history size */
126 * Debug variant of rtentry structure.
129 struct rtentry rtd_entry
; /* rtentry */
130 struct rtentry rtd_entry_saved
; /* saved rtentry */
131 u_int32_t rtd_inuse
; /* in use pattern */
132 u_int16_t rtd_refhold_cnt
; /* # of rtref */
133 u_int16_t rtd_refrele_cnt
; /* # of rtunref */
135 * Thread and PC stack trace up to RTD_TRSTACK_SIZE
136 * deep during alloc and free.
138 struct thread
*rtd_alloc_thread
;
139 void *rtd_alloc_stk_pc
[RTD_TRSTACK_SIZE
];
140 struct thread
*rtd_free_thread
;
141 void *rtd_free_stk_pc
[RTD_TRSTACK_SIZE
];
143 * Circular lists of rtref and rtunref callers.
145 u_int16_t rtd_refhold_next
;
146 u_int16_t rtd_refrele_next
;
149 void *pc
[RTD_TRSTACK_SIZE
];
150 } rtd_refhold
[RTD_REFHIST_SIZE
];
153 void *pc
[RTD_TRSTACK_SIZE
];
154 } rtd_refrele
[RTD_REFHIST_SIZE
];
158 TAILQ_ENTRY(rtentry_dbg
) rtd_trash_link
;
161 /* List of trash route entries protected by rt_mtx */
162 static TAILQ_HEAD(, rtentry_dbg
) rttrash_head
;
164 static inline struct rtentry
*rte_alloc_debug(void);
165 static inline void rte_free_debug(struct rtentry
*);
166 static void rt_maskedcopy(struct sockaddr
*,
167 struct sockaddr
*, struct sockaddr
*);
168 static void rtable_init(void **);
169 static inline void rtref_audit(struct rtentry_dbg
*);
170 static inline void rtunref_audit(struct rtentry_dbg
*);
171 static struct rtentry
*rtalloc1_common_locked(struct sockaddr
*, int, u_long
,
173 static int rtrequest_common_locked(int, struct sockaddr
*,
174 struct sockaddr
*, struct sockaddr
*, int, struct rtentry
**,
176 static void rtalloc_ign_common_locked(struct route
*, u_long
, unsigned int);
177 static inline void sa_set_ifscope(struct sockaddr
*, unsigned int);
178 static struct sockaddr
*sin_copy(struct sockaddr_in
*, struct sockaddr_in
*,
180 static struct sockaddr
*mask_copy(struct sockaddr
*, struct sockaddr_in
*,
182 static struct radix_node
*node_lookup(struct sockaddr
*, struct sockaddr
*,
184 static struct radix_node
*node_lookup_default(void);
185 static int rn_match_ifscope(struct radix_node
*, void *);
186 static struct ifaddr
*ifa_ifwithroute_common_locked(int,
187 const struct sockaddr
*, const struct sockaddr
*, unsigned int);
189 __private_extern__ u_long route_generation
= 0;
190 extern int use_routegenid
;
193 * sockaddr_in with embedded interface scope; this is used internally
194 * to keep track of scoped route entries in the routing table. The
195 * fact that such a scope is embedded in the structure is an artifact
196 * of the current implementation which could change in future.
198 struct sockaddr_inifscope
{
200 sa_family_t sin_family
;
202 struct in_addr sin_addr
;
204 * To avoid possible conflict with an overlaid sockaddr_inarp
205 * having sin_other set to SIN_PROXY, we use the first 4-bytes
206 * of sin_zero since sin_srcaddr is one of the unused fields
215 #define sin_ifscope un._in_index.ifscope
218 #define SIN(sa) ((struct sockaddr_in *)(size_t)(sa))
219 #define SINIFSCOPE(sa) ((struct sockaddr_inifscope *)(size_t)(sa))
221 #define ASSERT_SINIFSCOPE(sa) { \
222 if ((sa)->sa_family != AF_INET || \
223 (sa)->sa_len < sizeof (struct sockaddr_in)) \
224 panic("%s: bad sockaddr_in %p\n", __func__, sa); \
228 * Argument to leaf-matching routine; at present it is scoped routing
229 * specific but can be expanded in future to include other search filters.
231 struct matchleaf_arg
{
232 unsigned int ifscope
; /* interface scope */
236 * For looking up the non-scoped default route (sockaddr instead
237 * of sockaddr_in for convenience).
239 static struct sockaddr sin_def
= {
240 sizeof (struct sockaddr_in
), AF_INET
, { 0, }
244 * Interface index (scope) of the primary interface; determined at
245 * the time when the default, non-scoped route gets added, changed
246 * or deleted. Protected by rt_mtx.
248 static unsigned int primary_ifscope
= IFSCOPE_NONE
;
250 #define INET_DEFAULT(dst) \
251 ((dst)->sa_family == AF_INET && SIN(dst)->sin_addr.s_addr == 0)
253 #define RT(r) ((struct rtentry *)r)
254 #define RT_HOST(r) (RT(r)->rt_flags & RTF_HOST)
257 * Given a route, determine whether or not it is the non-scoped default
258 * route; dst typically comes from rt_key(rt) but may be coming from
259 * a separate place when rt is in the process of being created.
262 rt_inet_default(struct rtentry
*rt
, struct sockaddr
*dst
)
264 return (INET_DEFAULT(dst
) && !(rt
->rt_flags
& RTF_IFSCOPE
));
268 * Set the ifscope of the primary interface; caller holds rt_mtx.
271 set_primary_ifscope(unsigned int ifscope
)
273 primary_ifscope
= ifscope
;
277 * Return the ifscope of the primary interface; caller holds rt_mtx.
280 get_primary_ifscope(void)
282 return (primary_ifscope
);
286 * Embed ifscope into a given a sockaddr_in.
289 sa_set_ifscope(struct sockaddr
*sa
, unsigned int ifscope
)
291 /* Caller must pass in sockaddr_in */
292 ASSERT_SINIFSCOPE(sa
);
294 SINIFSCOPE(sa
)->sin_ifscope
= ifscope
;
298 * Given a sockaddr_in, return the embedded ifscope to the caller.
301 sa_get_ifscope(struct sockaddr
*sa
)
303 /* Caller must pass in sockaddr_in */
304 ASSERT_SINIFSCOPE(sa
);
306 return (SINIFSCOPE(sa
)->sin_ifscope
);
310 * Copy a sockaddr_in src to dst and embed ifscope into dst.
312 static struct sockaddr
*
313 sin_copy(struct sockaddr_in
*src
, struct sockaddr_in
*dst
, unsigned int ifscope
)
316 sa_set_ifscope(SA(dst
), ifscope
);
322 * Copy a mask from src to a sockaddr_in dst and embed ifscope into dst.
324 static struct sockaddr
*
325 mask_copy(struct sockaddr
*src
, struct sockaddr_in
*dst
, unsigned int ifscope
)
327 /* We know dst is at least the size of sockaddr{_in} */
328 bzero(dst
, sizeof (*dst
));
329 rt_maskedcopy(src
, SA(dst
), src
);
332 * The length of the mask sockaddr would need to be adjusted
333 * to cover the additional sin_ifscope field; when ifscope is
334 * IFSCOPE_NONE, we'd end up clearing the embedded ifscope on
335 * the destination mask in addition to extending the length
336 * of the sockaddr, as a side effect. This is okay, as any
337 * trailing zeroes would be skipped by rn_addmask prior to
338 * inserting or looking up the mask in the mask tree.
340 SINIFSCOPE(dst
)->sin_ifscope
= ifscope
;
341 SINIFSCOPE(dst
)->sin_len
=
342 offsetof(struct sockaddr_inifscope
, sin_ifscope
) +
343 sizeof (SINIFSCOPE(dst
)->sin_ifscope
);
349 * Callback leaf-matching routine for rn_matchaddr_args used
350 * for looking up an exact match for a scoped route entry.
353 rn_match_ifscope(struct radix_node
*rn
, void *arg
)
355 struct rtentry
*rt
= (struct rtentry
*)rn
;
356 struct matchleaf_arg
*ma
= arg
;
358 if (!(rt
->rt_flags
& RTF_IFSCOPE
) || rt_key(rt
)->sa_family
!= AF_INET
)
361 return (SINIFSCOPE(rt_key(rt
))->sin_ifscope
== ma
->ifscope
);
365 rtable_init(void **table
)
368 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
369 if (dom
->dom_rtattach
)
370 dom
->dom_rtattach(&table
[dom
->dom_family
],
379 PE_parse_boot_argn("rte_debug", &rte_debug
, sizeof (rte_debug
));
381 rte_debug
|= RTD_DEBUG
;
383 rt_mtx_grp_attr
= lck_grp_attr_alloc_init();
385 rt_mtx_grp
= lck_grp_alloc_init("route", rt_mtx_grp_attr
);
387 rt_mtx_attr
= lck_attr_alloc_init();
389 if ((rt_mtx
= lck_mtx_alloc_init(rt_mtx_grp
, rt_mtx_attr
)) == NULL
) {
390 printf("route_init: can't alloc rt_mtx\n");
394 lck_mtx_lock(rt_mtx
);
395 rn_init(); /* initialize all zeroes, all ones, mask table */
396 lck_mtx_unlock(rt_mtx
);
397 rtable_init((void **)rt_tables
);
398 route_domain_mtx
= routedomain
.dom_mtx
;
400 if (rte_debug
& RTD_DEBUG
)
401 size
= sizeof (struct rtentry_dbg
);
403 size
= sizeof (struct rtentry
);
405 rte_zone
= zinit(size
, RTE_ZONE_MAX
* size
, 0, RTE_ZONE_NAME
);
406 if (rte_zone
== NULL
)
407 panic("route_init: failed allocating rte_zone");
409 zone_change(rte_zone
, Z_EXPAND
, TRUE
);
411 TAILQ_INIT(&rttrash_head
);
415 * Packet routing routines.
418 rtalloc(struct route
*ro
)
420 rtalloc_ign(ro
, 0UL);
424 rtalloc_ign_locked(struct route
*ro
, u_long ignore
)
426 return (rtalloc_ign_common_locked(ro
, ignore
, IFSCOPE_NONE
));
430 rtalloc_scoped_ign_locked(struct route
*ro
, u_long ignore
, unsigned int ifscope
)
432 return (rtalloc_ign_common_locked(ro
, ignore
, ifscope
));
436 rtalloc_ign_common_locked(struct route
*ro
, u_long ignore
,
437 unsigned int ifscope
)
441 if ((rt
= ro
->ro_rt
) != NULL
) {
442 if (rt
->rt_ifp
!= NULL
&& rt
->rt_flags
& RTF_UP
)
447 ro
->ro_rt
= rtalloc1_common_locked(&ro
->ro_dst
, 1, ignore
, ifscope
);
449 ro
->ro_rt
->generation_id
= route_generation
;
452 rtalloc_ign(struct route
*ro
, u_long ignore
)
454 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
455 lck_mtx_lock(rt_mtx
);
456 rtalloc_ign_locked(ro
, ignore
);
457 lck_mtx_unlock(rt_mtx
);
461 rtalloc1_locked(struct sockaddr
*dst
, int report
, u_long ignflags
)
463 return (rtalloc1_common_locked(dst
, report
, ignflags
, IFSCOPE_NONE
));
467 rtalloc1_scoped_locked(struct sockaddr
*dst
, int report
, u_long ignflags
,
468 unsigned int ifscope
)
470 return (rtalloc1_common_locked(dst
, report
, ignflags
, ifscope
));
474 * Look up the route that matches the address given
475 * Or, at least try.. Create a cloned route if needed.
477 static struct rtentry
*
478 rtalloc1_common_locked(struct sockaddr
*dst
, int report
, u_long ignflags
,
479 unsigned int ifscope
)
481 struct radix_node_head
*rnh
= rt_tables
[dst
->sa_family
];
482 struct rtentry
*rt
, *newrt
= NULL
;
483 struct rt_addrinfo info
;
485 int err
= 0, msgtype
= RTM_MISS
;
491 * Find the longest prefix or exact (in the scoped case) address match;
492 * callee adds a reference to entry and checks for root node as well
494 rt
= rt_lookup(FALSE
, dst
, NULL
, rnh
, ifscope
);
499 nflags
= rt
->rt_flags
& ~ignflags
;
500 if (report
&& (nflags
& (RTF_CLONING
| RTF_PRCLONING
))) {
502 * We are apparently adding (report = 0 in delete).
503 * If it requires that it be cloned, do so.
504 * (This implies it wasn't a HOST route.)
506 err
= rtrequest_locked(RTM_RESOLVE
, dst
, NULL
, NULL
, 0, &newrt
);
509 * If the cloning didn't succeed, maybe what we
510 * have from lookup above will do. Return that;
511 * no need to hold another reference since it's
519 * We cloned it; drop the original route found during lookup.
520 * The resulted cloned route (newrt) would now have an extra
521 * reference held during rtrequest.
524 if ((rt
= newrt
) && (rt
->rt_flags
& RTF_XRESOLVE
)) {
526 * If the new route specifies it be
527 * externally resolved, then go do that.
529 msgtype
= RTM_RESOLVE
;
537 * Either we hit the root or couldn't find any match,
538 * Which basically means "cant get there from here"
540 rtstat
.rts_unreach
++;
544 * If required, report the failure to the supervising
546 * For a delete, this is not an error. (report == 0)
548 bzero((caddr_t
)&info
, sizeof(info
));
549 info
.rti_info
[RTAX_DST
] = dst
;
550 rt_missmsg(msgtype
, &info
, 0, err
);
557 rtalloc1(struct sockaddr
*dst
, int report
, u_long ignflags
)
559 struct rtentry
* entry
;
560 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
561 lck_mtx_lock(rt_mtx
);
562 entry
= rtalloc1_locked(dst
, report
, ignflags
);
563 lck_mtx_unlock(rt_mtx
);
568 * Remove a reference count from an rtentry.
569 * If the count gets low enough, take it out of the routing table
572 rtfree_locked(struct rtentry
*rt
)
575 * find the tree for that address family
576 * Note: in the case of igmp packets, there might not be an rnh
578 struct radix_node_head
*rnh
;
580 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
582 /* See 3582620 - We hit this during the transition from funnels to locks */
584 printf("rtfree - rt is NULL\n");
588 rnh
= rt_tables
[rt_key(rt
)->sa_family
];
591 * decrement the reference count by one and if it reaches 0,
592 * and there is a close function defined, call the close function
595 if (rt
->rt_refcnt
> 0)
599 * On last reference give the "close method" a chance to cleanup
600 * private state. This also permits (for IPv4 and IPv6) a chance
601 * to decide if the routing table entry should be purged immediately
602 * or at a later time. When an immediate purge is to happen the
603 * close routine typically issues RTM_DELETE which clears the RTF_UP
604 * flag on the entry so that the code below reclaims the storage.
606 if (rnh
&& rnh
->rnh_close
&& rt
->rt_refcnt
== 0)
607 rnh
->rnh_close((struct radix_node
*)rt
, rnh
);
610 * If we are no longer "up" (and ref == 0)
611 * then we can free the resources associated
614 if (!(rt
->rt_flags
& RTF_UP
)) {
615 if (rt
->rt_nodes
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
))
618 * the rtentry must have been removed from the routing table
619 * so it is represented in rttrash.. remove that now.
621 (void) OSDecrementAtomic((SInt32
*)&rttrash
);
622 if (rte_debug
& RTD_DEBUG
) {
623 TAILQ_REMOVE(&rttrash_head
, (struct rtentry_dbg
*)rt
,
628 if (rt
->rt_refcnt
< 0) {
629 printf("rtfree: %p not freed (neg refs) cnt=%d\n",
636 * release references on items we hold them on..
637 * e.g other routes and ifaddrs.
640 rtfree_locked(rt
->rt_parent
);
648 * The key is separatly alloc'd so free it (see rt_setgate()).
649 * This also frees the gateway, as they are always malloc'd
655 * and the rtentry itself of course
662 rtfree(struct rtentry
*rt
)
664 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
665 lck_mtx_lock(rt_mtx
);
667 lck_mtx_unlock(rt_mtx
);
671 * Decrements the refcount but does not free the route when
672 * the refcount reaches zero. Unless you have really good reason,
673 * use rtfree not rtunref.
676 rtunref(struct rtentry
*p
)
678 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
680 if (p
->rt_refcnt
<= 0)
681 panic("rtunref: bad refcnt %d for rt=%p\n", p
->rt_refcnt
, p
);
683 if (rte_debug
& RTD_DEBUG
)
684 rtunref_audit((struct rtentry_dbg
*)p
);
690 rtunref_audit(struct rtentry_dbg
*rte
)
692 if (rte
->rtd_inuse
!= RTD_INUSE
)
693 panic("rtunref: on freed rte=%p\n", rte
);
695 rte
->rtd_refrele_cnt
++;
697 if (rte_debug
& RTD_TRACE
) {
698 rte
->rtd_refrele
[rte
->rtd_refrele_next
].th
= current_thread();
699 bzero(rte
->rtd_refrele
[rte
->rtd_refrele_next
].pc
,
700 sizeof (rte
->rtd_refrele
[rte
->rtd_refrele_next
].pc
));
701 (void) OSBacktrace(rte
->rtd_refrele
[rte
->rtd_refrele_next
].pc
,
704 rte
->rtd_refrele_next
=
705 (rte
->rtd_refrele_next
+ 1) % RTD_REFHIST_SIZE
;
710 * Add a reference count from an rtentry.
713 rtref(struct rtentry
*p
)
715 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
717 if (p
->rt_refcnt
< 0)
718 panic("rtref: bad refcnt %d for rt=%p\n", p
->rt_refcnt
, p
);
720 if (rte_debug
& RTD_DEBUG
)
721 rtref_audit((struct rtentry_dbg
*)p
);
727 rtref_audit(struct rtentry_dbg
*rte
)
729 if (rte
->rtd_inuse
!= RTD_INUSE
)
730 panic("rtref_audit: on freed rte=%p\n", rte
);
732 rte
->rtd_refhold_cnt
++;
734 if (rte_debug
& RTD_TRACE
) {
735 rte
->rtd_refhold
[rte
->rtd_refhold_next
].th
= current_thread();
736 bzero(rte
->rtd_refhold
[rte
->rtd_refhold_next
].pc
,
737 sizeof (rte
->rtd_refhold
[rte
->rtd_refhold_next
].pc
));
738 (void) OSBacktrace(rte
->rtd_refhold
[rte
->rtd_refhold_next
].pc
,
741 rte
->rtd_refhold_next
=
742 (rte
->rtd_refhold_next
+ 1) % RTD_REFHIST_SIZE
;
747 rtsetifa(struct rtentry
*rt
, struct ifaddr
* ifa
)
752 if (rt
->rt_ifa
== ifa
)
755 /* Release the old ifa */
762 /* Take a reference to the ifa */
768 ifafree(struct ifaddr
*ifa
)
775 oldval
= OSAddAtomic(-1, (SInt32
*)&ifa
->ifa_refcnt
);
778 if ((ifa
->ifa_debug
& IFA_ATTACHED
) != 0) {
779 panic("ifa attached to ifp is being freed\n");
786 ifaref(struct ifaddr
*ifa
)
791 if (OSAddAtomic(1, (SInt32
*)&ifa
->ifa_refcnt
) == 0xffffffff)
792 panic("ifaref - reference count rolled over!");
796 * Force a routing table entry to the specified
797 * destination to go through the given gateway.
798 * Normally called as a result of a routing redirect
799 * message from the network layer.
802 rtredirect(struct ifnet
*ifp
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
803 struct sockaddr
*netmask
, int flags
, struct sockaddr
*src
,
804 struct rtentry
**rtp
)
806 struct rtentry
*rt
= NULL
;
809 struct rt_addrinfo info
;
810 struct ifaddr
*ifa
= NULL
;
811 unsigned int ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
812 struct sockaddr_in sin
;
814 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
815 lck_mtx_lock(rt_mtx
);
818 * Verify the gateway is directly reachable; if scoped routing
819 * is enabled, verify that it is reachable from the interface
820 * where the ICMP redirect arrived on.
822 if ((ifa
= ifa_ifwithnet_scoped(gateway
, ifscope
)) == NULL
) {
827 /* Lookup route to the destination (from the original IP header) */
828 rt
= rtalloc1_scoped_locked(dst
, 0, RTF_CLONING
|RTF_PRCLONING
, ifscope
);
830 /* Embed scope in src for comparison against rt_gateway below */
831 if (ip_doscopedroute
&& src
->sa_family
== AF_INET
)
832 src
= sin_copy(SIN(src
), &sin
, ifscope
);
835 * If the redirect isn't from our current router for this dst,
836 * it's either old or wrong. If it redirects us to ourselves,
837 * we have a routing loop, perhaps as a result of an interface
838 * going down recently.
840 if (!(flags
& RTF_DONE
) && rt
&&
841 (!equal(src
, rt
->rt_gateway
) || !equal(rt
->rt_ifa
->ifa_addr
,
846 if ((ifa
= ifa_ifwithaddr(gateway
))) {
849 error
= EHOSTUNREACH
;
861 * Create a new entry if we just got back a wildcard entry
862 * or the the lookup failed. This is necessary for hosts
863 * which use routing redirects generated by smart gateways
864 * to dynamically build the routing tables.
866 if ((rt
== 0) || (rt_mask(rt
) && rt_mask(rt
)->sa_len
< 2))
869 * Don't listen to the redirect if it's
870 * for a route to an interface.
872 if (rt
->rt_flags
& RTF_GATEWAY
) {
873 if (((rt
->rt_flags
& RTF_HOST
) == 0) && (flags
& RTF_HOST
)) {
875 * Changing from route to net => route to host.
876 * Create new route, rather than smashing route
877 * to net; similar to cloned routes, the newly
878 * created host route is scoped as well.
881 flags
|= RTF_GATEWAY
| RTF_DYNAMIC
;
882 error
= rtrequest_scoped_locked(RTM_ADD
, dst
,
883 gateway
, netmask
, flags
, NULL
, ifscope
);
884 stat
= &rtstat
.rts_dynamic
;
887 * Smash the current notion of the gateway to
888 * this destination. Should check about netmask!!!
890 rt
->rt_flags
|= RTF_MODIFIED
;
891 flags
|= RTF_MODIFIED
;
892 stat
= &rtstat
.rts_newgateway
;
894 * add the key and gateway (in one malloc'd chunk).
896 error
= rt_setgate(rt
, rt_key(rt
), gateway
);
899 error
= EHOSTUNREACH
;
910 rtstat
.rts_badredirect
++;
917 bzero((caddr_t
)&info
, sizeof(info
));
918 info
.rti_info
[RTAX_DST
] = dst
;
919 info
.rti_info
[RTAX_GATEWAY
] = gateway
;
920 info
.rti_info
[RTAX_NETMASK
] = netmask
;
921 info
.rti_info
[RTAX_AUTHOR
] = src
;
922 rt_missmsg(RTM_REDIRECT
, &info
, flags
, error
);
923 lck_mtx_unlock(rt_mtx
);
927 * Routing table ioctl interface.
930 rtioctl(int req
, caddr_t data
, struct proc
*p
)
934 return mrt_ioctl(req
, data
);
943 const struct sockaddr
*dst
,
944 const struct sockaddr
*gateway
)
948 lck_mtx_lock(rt_mtx
);
949 ifa
= ifa_ifwithroute_locked(flags
, dst
, gateway
);
950 lck_mtx_unlock(rt_mtx
);
956 ifa_ifwithroute_locked(int flags
, const struct sockaddr
*dst
,
957 const struct sockaddr
*gateway
)
959 return (ifa_ifwithroute_common_locked((flags
& ~RTF_IFSCOPE
), dst
,
960 gateway
, IFSCOPE_NONE
));
964 ifa_ifwithroute_scoped_locked(int flags
, const struct sockaddr
*dst
,
965 const struct sockaddr
*gateway
, unsigned int ifscope
)
967 if (ifscope
!= IFSCOPE_NONE
)
968 flags
|= RTF_IFSCOPE
;
970 flags
&= ~RTF_IFSCOPE
;
972 return (ifa_ifwithroute_common_locked(flags
, dst
, gateway
, ifscope
));
975 static struct ifaddr
*
976 ifa_ifwithroute_common_locked(int flags
, const struct sockaddr
*dst
,
977 const struct sockaddr
*gateway
, unsigned int ifscope
)
979 struct ifaddr
*ifa
= NULL
;
980 struct rtentry
*rt
= NULL
;
981 struct sockaddr_in dst_in
, gw_in
;
983 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
985 if (ip_doscopedroute
) {
987 * Just in case the sockaddr passed in by the caller
988 * contains embedded scope, make sure to clear it since
989 * IPv4 interface addresses aren't scoped.
991 if (dst
!= NULL
&& dst
->sa_family
== AF_INET
)
992 dst
= sin_copy(SIN(dst
), &dst_in
, IFSCOPE_NONE
);
993 if (gateway
!= NULL
&& gateway
->sa_family
== AF_INET
)
994 gateway
= sin_copy(SIN(gateway
), &gw_in
, IFSCOPE_NONE
);
997 if (!(flags
& RTF_GATEWAY
)) {
999 * If we are adding a route to an interface,
1000 * and the interface is a pt to pt link
1001 * we should search for the destination
1002 * as our clue to the interface. Otherwise
1003 * we can use the local address.
1005 if (flags
& RTF_HOST
) {
1006 ifa
= ifa_ifwithdstaddr(dst
);
1009 ifa
= ifa_ifwithaddr_scoped(gateway
, ifscope
);
1012 * If we are adding a route to a remote net
1013 * or host, the gateway may still be on the
1014 * other end of a pt to pt link.
1016 ifa
= ifa_ifwithdstaddr(gateway
);
1019 ifa
= ifa_ifwithnet_scoped(gateway
, ifscope
);
1021 /* Workaround to avoid gcc warning regarding const variable */
1022 rt
= rtalloc1_scoped_locked((struct sockaddr
*)(size_t)dst
,
1032 if (ifa
!= NULL
&& ifa
->ifa_addr
->sa_family
!= dst
->sa_family
) {
1033 struct ifaddr
*newifa
;
1034 /* Callee adds reference to newifa upon success */
1035 newifa
= ifaof_ifpforaddr(dst
, ifa
->ifa_ifp
);
1036 if (newifa
!= NULL
) {
1042 * If we are adding a gateway, it is quite possible that the
1043 * routing table has a static entry in place for the gateway,
1044 * that may not agree with info garnered from the interfaces.
1045 * The routing table should carry more precedence than the
1046 * interfaces in this matter. Must be careful not to stomp
1047 * on new entries from rtinit, hence (ifa->ifa_addr != gateway).
1050 !equal(ifa
->ifa_addr
, (struct sockaddr
*)(size_t)gateway
)) &&
1051 (rt
= rtalloc1_scoped_locked((struct sockaddr
*)(size_t)gateway
,
1052 0, 0UL, ifscope
)) != NULL
) {
1061 * If an interface scope was specified, the interface index of
1062 * the found ifaddr must be equivalent to that of the scope;
1063 * otherwise there is no match.
1065 if ((flags
& RTF_IFSCOPE
) &&
1066 ifa
!= NULL
&& ifa
->ifa_ifp
->if_index
!= ifscope
) {
1074 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1076 static int rt_fixdelete
__P((struct radix_node
*, void *));
1077 static int rt_fixchange
__P((struct radix_node
*, void *));
1080 struct rtentry
*rt0
;
1081 struct radix_node_head
*rnh
;
1085 rtrequest_locked(int req
, struct sockaddr
*dst
, struct sockaddr
*gateway
,
1086 struct sockaddr
*netmask
, int flags
, struct rtentry
**ret_nrt
)
1088 return (rtrequest_common_locked(req
, dst
, gateway
, netmask
,
1089 (flags
& ~RTF_IFSCOPE
), ret_nrt
, IFSCOPE_NONE
));
1093 rtrequest_scoped_locked(int req
, struct sockaddr
*dst
,
1094 struct sockaddr
*gateway
, struct sockaddr
*netmask
, int flags
,
1095 struct rtentry
**ret_nrt
, unsigned int ifscope
)
1097 if (ifscope
!= IFSCOPE_NONE
)
1098 flags
|= RTF_IFSCOPE
;
1100 flags
&= ~RTF_IFSCOPE
;
1102 return (rtrequest_common_locked(req
, dst
, gateway
, netmask
,
1103 flags
, ret_nrt
, ifscope
));
1107 * Do appropriate manipulations of a routing tree given all the bits of
1110 * Embedding the scope in the radix key is an internal job that should be
1111 * left to routines in this module. Callers should specify the scope value
1112 * to the "scoped" variants of route routines instead of manipulating the
1113 * key itself. This is typically done when creating a scoped route, e.g.
1114 * rtrequest(RTM_ADD). Once such a route is created and marked with the
1115 * RTF_IFSCOPE flag, callers can simply use its rt_key(rt) to clone it
1116 * (RTM_RESOLVE) or to remove it (RTM_DELETE). An exception to this is
1117 * during certain routing socket operations where the search key might be
1118 * derived from the routing message itself, in which case the caller must
1119 * specify the destination address and scope value for RTM_ADD/RTM_DELETE.
1122 rtrequest_common_locked(int req
, struct sockaddr
*dst0
,
1123 struct sockaddr
*gateway
, struct sockaddr
*netmask
, int flags
,
1124 struct rtentry
**ret_nrt
, unsigned int ifscope
)
1128 struct radix_node
*rn
;
1129 struct radix_node_head
*rnh
;
1130 struct ifaddr
*ifa
= NULL
;
1131 struct sockaddr
*ndst
, *dst
= dst0
;
1132 struct sockaddr_in sin
, mask
;
1133 #define senderr(x) { error = x ; goto bad; }
1135 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
1137 * Find the correct routing tree to use for this Address Family
1139 if ((rnh
= rt_tables
[dst
->sa_family
]) == 0)
1142 * If we are adding a host route then we don't want to put
1143 * a netmask in the tree
1145 if (flags
& RTF_HOST
)
1149 * If RTF_IFSCOPE is specified, use a local copy of the destination
1150 * address to embed the scope into. This logic is repeated below
1151 * in the RTM_RESOLVE handler since the caller does not normally
1152 * specify such a flag during a resolve; instead it passes in the
1153 * route used for cloning for which the scope info is derived from.
1154 * Note also that in the case of RTM_DELETE, the address passed in
1155 * by the caller might already contain the embedded scope info when
1156 * it is the key itself, thus making RTF_IFSCOPE unnecessary; one
1157 * instance where it is explicitly set is inside route_output()
1158 * as part of handling a routing socket request.
1160 if (req
!= RTM_RESOLVE
&& (flags
& RTF_IFSCOPE
)) {
1161 /* Scoped routing is for AF_INET only */
1162 if (dst
->sa_family
!= AF_INET
||
1163 (req
== RTM_ADD
&& !ip_doscopedroute
))
1166 if (ifscope
== IFSCOPE_NONE
) {
1167 flags
&= ~RTF_IFSCOPE
;
1169 /* Embed ifscope into the key (local copy) */
1170 dst
= sin_copy(SIN(dst
), &sin
, ifscope
);
1172 /* Embed ifscope into netmask (local copy) */
1173 if (netmask
!= NULL
)
1174 netmask
= mask_copy(netmask
, &mask
, ifscope
);
1181 * Remove the item from the tree and return it.
1182 * Complain if it is not there and do no more processing.
1184 if ((rn
= rnh
->rnh_deladdr(dst
, netmask
, rnh
)) == 0)
1186 if (rn
->rn_flags
& (RNF_ACTIVE
| RNF_ROOT
))
1187 panic ("rtrequest delete");
1188 rt
= (struct rtentry
*)rn
;
1191 * Take an extra reference to handle the deletion of a route
1192 * entry whose reference count is already 0; e.g. an expiring
1193 * cloned route entry or an entry that was added to the table
1194 * with 0 reference. If the caller is interested in this route,
1195 * we will return it with the reference intact. Otherwise we
1196 * will decrement the reference via rtfree_locked() and then
1197 * possibly deallocate it.
1200 rt
->rt_flags
&= ~RTF_UP
;
1203 * Now search what's left of the subtree for any cloned
1204 * routes which might have been formed from this node.
1206 if ((rt
->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) &&
1208 rnh
->rnh_walktree_from(rnh
, dst
, rt_mask(rt
),
1213 * Remove any external references we may have.
1214 * This might result in another rtentry being freed if
1215 * we held its last reference.
1217 if (rt
->rt_gwroute
) {
1218 rt
= rt
->rt_gwroute
;
1220 (rt
= (struct rtentry
*)rn
)->rt_gwroute
= 0;
1224 * give the protocol a chance to keep things in sync.
1226 if ((ifa
= rt
->rt_ifa
) && ifa
->ifa_rtrequest
)
1227 ifa
->ifa_rtrequest(RTM_DELETE
, rt
, SA(0));
1231 * one more rtentry floating around that is not
1232 * linked to the routing table.
1234 (void) OSIncrementAtomic((SInt32
*)&rttrash
);
1235 if (rte_debug
& RTD_DEBUG
) {
1236 TAILQ_INSERT_TAIL(&rttrash_head
,
1237 (struct rtentry_dbg
*)rt
, rtd_trash_link
);
1241 * If this is the (non-scoped) default route, clear
1242 * the interface index used for the primary ifscope.
1244 if (rt_inet_default(rt
, rt_key(rt
)))
1245 set_primary_ifscope(IFSCOPE_NONE
);
1248 * If the caller wants it, then it can have it,
1249 * but it's up to it to free the rtentry as we won't be
1252 if (ret_nrt
!= NULL
) {
1253 /* Return the route to caller with reference intact */
1256 /* Dereference or deallocate the route */
1262 if (ret_nrt
== 0 || (rt
= *ret_nrt
) == 0)
1266 flags
= rt
->rt_flags
&
1267 ~(RTF_CLONING
| RTF_PRCLONING
| RTF_STATIC
);
1268 flags
|= RTF_WASCLONED
;
1269 gateway
= rt
->rt_gateway
;
1270 if ((netmask
= rt
->rt_genmask
) == 0)
1273 if (!ip_doscopedroute
|| dst
->sa_family
!= AF_INET
)
1276 * When scoped routing is enabled, cloned entries are
1277 * always scoped according to the interface portion of
1278 * the parent route. The exception to this are IPv4
1279 * link local addresses.
1281 if (!IN_LINKLOCAL(ntohl(SIN(dst
)->sin_addr
.s_addr
))) {
1282 if (flags
& RTF_IFSCOPE
) {
1283 ifscope
= sa_get_ifscope(rt_key(rt
));
1285 ifscope
= rt
->rt_ifp
->if_index
;
1286 flags
|= RTF_IFSCOPE
;
1289 ifscope
= IFSCOPE_NONE
;
1290 flags
&= ~RTF_IFSCOPE
;
1293 /* Embed or clear ifscope into/from the key (local copy) */
1294 dst
= sin_copy(SIN(dst
), &sin
, ifscope
);
1296 /* Embed or clear ifscope into/from netmask (local copy) */
1297 if (netmask
!= NULL
)
1298 netmask
= mask_copy(netmask
, &mask
, ifscope
);
1303 if ((flags
& RTF_GATEWAY
) && !gateway
)
1304 panic("rtrequest: RTF_GATEWAY but no gateway");
1306 if (flags
& RTF_IFSCOPE
) {
1307 ifa
= ifa_ifwithroute_scoped_locked(flags
, dst0
,
1310 ifa
= ifa_ifwithroute_locked(flags
, dst0
, gateway
);
1313 senderr(ENETUNREACH
);
1315 if ((rt
= rte_alloc()) == NULL
)
1317 Bzero(rt
, sizeof(*rt
));
1318 rt
->rt_flags
= RTF_UP
| flags
;
1321 * Add the gateway. Possibly re-malloc-ing the storage for it
1322 * also add the rt_gwroute if possible.
1324 if ((error
= rt_setgate(rt
, dst
, gateway
)) != 0) {
1330 * point to the (possibly newly malloc'd) dest address.
1335 * make sure it contains the value we want (masked if needed).
1338 rt_maskedcopy(dst
, ndst
, netmask
);
1340 Bcopy(dst
, ndst
, dst
->sa_len
);
1343 * Note that we now have a reference to the ifa.
1344 * This moved from below so that rnh->rnh_addaddr() can
1345 * examine the ifa and ifa->ifa_ifp if it so desires.
1348 rt
->rt_ifp
= rt
->rt_ifa
->ifa_ifp
;
1350 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1352 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
, (caddr_t
)netmask
,
1355 struct rtentry
*rt2
;
1357 * Uh-oh, we already have one of these in the tree.
1358 * We do a special hack: if the route that's already
1359 * there was generated by the protocol-cloning
1360 * mechanism, then we just blow it away and retry
1361 * the insertion of the new one.
1363 if (flags
& RTF_IFSCOPE
) {
1364 rt2
= rtalloc1_scoped_locked(dst0
, 0,
1365 RTF_CLONING
| RTF_PRCLONING
, ifscope
);
1367 rt2
= rtalloc1_locked(dst
, 0,
1368 RTF_CLONING
| RTF_PRCLONING
);
1370 if (rt2
&& rt2
->rt_parent
) {
1371 rtrequest_locked(RTM_DELETE
,
1372 (struct sockaddr
*)rt_key(rt2
),
1374 rt_mask(rt2
), rt2
->rt_flags
, 0);
1376 rn
= rnh
->rnh_addaddr((caddr_t
)ndst
,
1380 /* undo the extra ref we got */
1386 * If it still failed to go into the tree,
1387 * then un-make it (this should be a function)
1391 rtfree_locked(rt
->rt_gwroute
);
1393 ifafree(rt
->rt_ifa
);
1403 * If we got here from RESOLVE, then we are cloning
1404 * so clone the rest, and note that we
1405 * are a clone (and increment the parent's references)
1407 if (req
== RTM_RESOLVE
) {
1408 rt
->rt_rmx
= (*ret_nrt
)->rt_rmx
; /* copy metrics */
1409 if ((*ret_nrt
)->rt_flags
& (RTF_CLONING
| RTF_PRCLONING
)) {
1410 rt
->rt_parent
= (*ret_nrt
);
1416 * if this protocol has something to add to this then
1417 * allow it to do that as well.
1419 if (ifa
->ifa_rtrequest
)
1420 ifa
->ifa_rtrequest(req
, rt
, SA(ret_nrt
? *ret_nrt
: 0));
1425 * We repeat the same procedure from rt_setgate() here because
1426 * it doesn't fire when we call it there because the node
1427 * hasn't been added to the tree yet.
1429 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != 0) {
1430 struct rtfc_arg arg
;
1433 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
1434 rt_fixchange
, &arg
);
1438 * If this is the (non-scoped) default route, record
1439 * the interface index used for the primary ifscope.
1441 if (rt_inet_default(rt
, rt_key(rt
)))
1442 set_primary_ifscope(rt
->rt_ifp
->if_index
);
1445 * actually return a resultant rtentry and
1446 * give the caller a single reference.
1463 struct sockaddr
*dst
,
1464 struct sockaddr
*gateway
,
1465 struct sockaddr
*netmask
,
1467 struct rtentry
**ret_nrt
)
1470 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1471 lck_mtx_lock(rt_mtx
);
1472 error
= rtrequest_locked(req
, dst
, gateway
, netmask
, flags
, ret_nrt
);
1473 lck_mtx_unlock(rt_mtx
);
1477 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1478 * (i.e., the routes related to it by the operation of cloning). This
1479 * routine is iterated over all potential former-child-routes by way of
1480 * rnh->rnh_walktree_from() above, and those that actually are children of
1481 * the late parent (passed in as VP here) are themselves deleted.
1484 rt_fixdelete(struct radix_node
*rn
, void *vp
)
1486 struct rtentry
*rt
= (struct rtentry
*)rn
;
1487 struct rtentry
*rt0
= vp
;
1489 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
1491 if (rt
->rt_parent
== rt0
&&
1492 !(rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
))) {
1493 return rtrequest_locked(RTM_DELETE
, rt_key(rt
),
1494 (struct sockaddr
*)0, rt_mask(rt
),
1495 rt
->rt_flags
, (struct rtentry
**)0);
1501 * This routine is called from rt_setgate() to do the analogous thing for
1502 * adds and changes. There is the added complication in this case of a
1503 * middle insert; i.e., insertion of a new network route between an older
1504 * network route and (cloned) host routes. For this reason, a simple check
1505 * of rt->rt_parent is insufficient; each candidate route must be tested
1506 * against the (mask, value) of the new route (passed as before in vp)
1507 * to see if the new route matches it.
1509 * XXX - it may be possible to do fixdelete() for changes and reserve this
1510 * routine just for adds. I'm not sure why I thought it was necessary to do
1514 rt_fixchange(struct radix_node
*rn
, void *vp
)
1516 struct rtentry
*rt
= (struct rtentry
*)rn
;
1517 struct rtfc_arg
*ap
= vp
;
1518 struct rtentry
*rt0
= ap
->rt0
;
1519 struct radix_node_head
*rnh
= ap
->rnh
;
1520 u_char
*xk1
, *xm1
, *xk2
, *xmp
;
1523 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
1525 if (!rt
->rt_parent
||
1526 (rt
->rt_flags
& (RTF_PINNED
| RTF_CLONING
| RTF_PRCLONING
)))
1529 if (rt
->rt_parent
== rt0
)
1533 * There probably is a function somewhere which does this...
1534 * if not, there should be.
1536 len
= imin(rt_key(rt0
)->sa_len
, rt_key(rt
)->sa_len
);
1538 xk1
= (u_char
*)rt_key(rt0
);
1539 xm1
= (u_char
*)rt_mask(rt0
);
1540 xk2
= (u_char
*)rt_key(rt
);
1542 /* avoid applying a less specific route */
1543 xmp
= (u_char
*)rt_mask(rt
->rt_parent
);
1544 mlen
= rt_key(rt
->rt_parent
)->sa_len
;
1545 if (mlen
> rt_key(rt0
)->sa_len
)
1548 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< mlen
; i
++) {
1549 if ((xmp
[i
] & ~(xmp
[i
] ^ xm1
[i
])) != xmp
[i
])
1553 for (i
= rnh
->rnh_treetop
->rn_offset
; i
< len
; i
++) {
1554 if ((xk2
[i
] & xm1
[i
]) != xk1
[i
])
1559 * OK, this node is a clone, and matches the node currently being
1560 * changed/added under the node's mask. So, get rid of it.
1563 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), NULL
,
1564 rt_mask(rt
), rt
->rt_flags
, NULL
));
1568 rt_setgate(struct rtentry
*rt
, struct sockaddr
*dst
, struct sockaddr
*gate
)
1570 int dlen
= ROUNDUP(dst
->sa_len
), glen
= ROUNDUP(gate
->sa_len
);
1571 struct radix_node_head
*rnh
= rt_tables
[dst
->sa_family
];
1573 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
1576 * A host route with the destination equal to the gateway
1577 * will interfere with keeping LLINFO in the routing
1578 * table, so disallow it.
1580 if (((rt
->rt_flags
& (RTF_HOST
|RTF_GATEWAY
|RTF_LLINFO
)) ==
1581 (RTF_HOST
|RTF_GATEWAY
)) && (dst
->sa_len
== gate
->sa_len
) &&
1582 (bcmp(dst
, gate
, dst
->sa_len
) == 0)) {
1584 * The route might already exist if this is an RTM_CHANGE
1585 * or a routing redirect, so try to delete it.
1588 rtrequest_locked(RTM_DELETE
, rt_key(rt
),
1589 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
1590 return (EADDRNOTAVAIL
);
1594 * The destination is not directly reachable. Get a route
1595 * to the next-hop gateway and store it in rt_gwroute.
1597 if (rt
->rt_flags
& RTF_GATEWAY
) {
1598 struct rtentry
*gwrt
;
1599 unsigned int ifscope
;
1601 ifscope
= (dst
->sa_family
== AF_INET
) ?
1602 sa_get_ifscope(dst
) : IFSCOPE_NONE
;
1604 gwrt
= rtalloc1_scoped_locked(gate
, 1, RTF_PRCLONING
, ifscope
);
1607 * Cloning loop avoidance:
1609 * In the presence of protocol-cloning and bad configuration,
1610 * it is possible to get stuck in bottomless mutual recursion
1611 * (rtrequest rt_setgate rtalloc1). We avoid this by not
1612 * allowing protocol-cloning to operate for gateways (which
1613 * is probably the correct choice anyway), and avoid the
1614 * resulting reference loops by disallowing any route to run
1615 * through itself as a gateway. This is obviously mandatory
1616 * when we get rt->rt_output(). It implies that a route to
1617 * the gateway must already be present in the system in order
1618 * for the gateway to be referred to by another route.
1622 return (EADDRINUSE
); /* failure */
1625 /* If scoped, the gateway route must use the same interface */
1626 if (ifscope
!= IFSCOPE_NONE
&& (rt
->rt_flags
& RTF_IFSCOPE
) &&
1627 gwrt
!= NULL
&& gwrt
->rt_ifp
!= NULL
&&
1628 gwrt
->rt_ifp
->if_index
!= ifscope
) {
1629 rtfree_locked(gwrt
);
1630 return ((rt
->rt_flags
& RTF_HOST
) ?
1631 EHOSTUNREACH
: ENETUNREACH
);
1634 if (rt
->rt_gwroute
!= NULL
)
1635 rtfree_locked(rt
->rt_gwroute
);
1636 rt
->rt_gwroute
= gwrt
;
1639 * In case the (non-scoped) default route gets modified via
1640 * an ICMP redirect, record the interface index used for the
1641 * primary ifscope. Also done in rt_setif() to take care
1642 * of the non-redirect cases.
1644 if (rt_inet_default(rt
, dst
) && rt
->rt_ifp
!= NULL
)
1645 set_primary_ifscope(rt
->rt_ifp
->if_index
);
1648 * Tell the kernel debugger about the new default gateway
1649 * if the gateway route uses the primary interface, or
1650 * if we are in a transient state before the non-scoped
1651 * default gateway is installed (similar to how the system
1652 * was behaving in the past). In future, it would be good
1653 * to do all this only when KDP is enabled.
1655 if ((dst
->sa_family
== AF_INET
) &&
1656 gwrt
!= NULL
&& gwrt
->rt_gateway
->sa_family
== AF_LINK
&&
1657 (gwrt
->rt_ifp
->if_index
== get_primary_ifscope() ||
1658 get_primary_ifscope() == IFSCOPE_NONE
))
1659 kdp_set_gateway_mac(SDL(gwrt
->rt_gateway
)->sdl_data
);
1663 * Prepare to store the gateway in rt_gateway. Both dst and gateway
1664 * are stored one after the other in the same malloc'd chunk. If we
1665 * have room, reuse the old buffer since rt_gateway already points
1666 * to the right place. Otherwise, malloc a new block and update
1667 * the 'dst' address and point rt_gateway to the right place.
1669 if (rt
->rt_gateway
== NULL
|| glen
> ROUNDUP(rt
->rt_gateway
->sa_len
)) {
1672 /* The underlying allocation is done with M_WAITOK set */
1673 R_Malloc(new, caddr_t
, dlen
+ glen
);
1675 if (rt
->rt_gwroute
!= NULL
)
1676 rtfree_locked(rt
->rt_gwroute
);
1677 rt
->rt_gwroute
= NULL
;
1682 * Copy from 'dst' and not rt_key(rt) because we can get
1683 * here to initialize a newly allocated route entry, in
1684 * which case rt_key(rt) is NULL (and so does rt_gateway).
1686 Bcopy(dst
, new, dlen
);
1687 R_Free(rt_key(rt
)); /* free old block; NULL is okay */
1688 rt
->rt_nodes
->rn_key
= new;
1689 rt
->rt_gateway
= (struct sockaddr
*)(new + dlen
);
1693 * Copy the new gateway value into the memory chunk.
1695 Bcopy(gate
, rt
->rt_gateway
, glen
);
1698 * For consistency between rt_gateway and rt_key(gwrt).
1700 if ((rt
->rt_flags
& RTF_GATEWAY
) && rt
->rt_gwroute
!= NULL
&&
1701 (rt
->rt_gwroute
->rt_flags
& RTF_IFSCOPE
) &&
1702 rt
->rt_gateway
->sa_family
== AF_INET
&&
1703 rt_key(rt
->rt_gwroute
)->sa_family
== AF_INET
) {
1704 sa_set_ifscope(rt
->rt_gateway
,
1705 sa_get_ifscope(rt_key(rt
->rt_gwroute
)));
1709 * This isn't going to do anything useful for host routes, so
1710 * don't bother. Also make sure we have a reasonable mask
1711 * (we don't yet have one during adds).
1713 if (!(rt
->rt_flags
& RTF_HOST
) && rt_mask(rt
) != 0) {
1714 struct rtfc_arg arg
;
1717 rnh
->rnh_walktree_from(rnh
, rt_key(rt
), rt_mask(rt
),
1718 rt_fixchange
, &arg
);
1725 rt_maskedcopy(struct sockaddr
*src
, struct sockaddr
*dst
,
1726 struct sockaddr
*netmask
)
1728 u_char
*cp1
= (u_char
*)src
;
1729 u_char
*cp2
= (u_char
*)dst
;
1730 u_char
*cp3
= (u_char
*)netmask
;
1731 u_char
*cplim
= cp2
+ *cp3
;
1732 u_char
*cplim2
= cp2
+ *cp1
;
1734 *cp2
++ = *cp1
++; *cp2
++ = *cp1
++; /* copies sa_len & sa_family */
1739 *cp2
++ = *cp1
++ & *cp3
++;
1741 bzero((caddr_t
)cp2
, (unsigned)(cplim2
- cp2
));
1745 * Lookup an AF_INET scoped or non-scoped route depending on the ifscope
1746 * value passed in by the caller (IFSCOPE_NONE implies non-scoped).
1748 static struct radix_node
*
1749 node_lookup(struct sockaddr
*dst
, struct sockaddr
*netmask
,
1750 unsigned int ifscope
)
1752 struct radix_node_head
*rnh
= rt_tables
[AF_INET
];
1753 struct radix_node
*rn
;
1754 struct sockaddr_in sin
, mask
;
1755 struct matchleaf_arg ma
= { ifscope
};
1756 rn_matchf_t
*f
= rn_match_ifscope
;
1759 if (dst
->sa_family
!= AF_INET
)
1763 * Embed ifscope into the search key; for a non-scoped
1764 * search this will clear out any embedded scope value.
1766 dst
= sin_copy(SIN(dst
), &sin
, ifscope
);
1768 /* Embed (or clear) ifscope into netmask */
1769 if (netmask
!= NULL
)
1770 netmask
= mask_copy(netmask
, &mask
, ifscope
);
1772 if (ifscope
== IFSCOPE_NONE
)
1775 rn
= rnh
->rnh_lookup_args(dst
, netmask
, rnh
, f
, w
);
1776 if (rn
!= NULL
&& (rn
->rn_flags
& RNF_ROOT
))
1783 * Lookup the AF_INET non-scoped default route.
1785 static struct radix_node
*
1786 node_lookup_default(void)
1788 struct radix_node_head
*rnh
= rt_tables
[AF_INET
];
1789 return (rnh
->rnh_lookup(&sin_def
, NULL
, rnh
));
1793 * Common routine to lookup/match a route. It invokes the lookup/matchaddr
1794 * callback which could be address family-specific. The main difference
1795 * between the two (at least for AF_INET/AF_INET6) is that a lookup does
1796 * not alter the expiring state of a route, whereas a match would unexpire
1797 * or revalidate the route.
1799 * The optional scope or interface index property of a route allows for a
1800 * per-interface route instance. This permits multiple route entries having
1801 * the same destination (but not necessarily the same gateway) to exist in
1802 * the routing table; each of these entries is specific to the corresponding
1803 * interface. This is made possible by embedding the scope value into the
1804 * radix key, thus making each route entry unique. These scoped entries
1805 * exist along with the regular, non-scoped entries in the same radix tree
1806 * for a given address family (currently AF_INET only); the scope logically
1807 * partitions it into multiple per-interface sub-trees.
1809 * When a scoped route lookup is performed, the routing table is searched for
1810 * the best match that would result in a route using the same interface as the
1811 * one associated with the scope (the exception to this are routes that point
1812 * to the loopback interface). The search rule follows the longest matching
1813 * prefix with the additional interface constraint.
1816 rt_lookup(boolean_t lookup_only
, struct sockaddr
*dst
, struct sockaddr
*netmask
,
1817 struct radix_node_head
*rnh
, unsigned int ifscope
)
1819 struct radix_node
*rn0
, *rn
;
1820 boolean_t dontcare
= (ifscope
== IFSCOPE_NONE
);
1822 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
1828 * Non-scoped route lookup.
1830 if (!ip_doscopedroute
|| dst
->sa_family
!= AF_INET
) {
1832 rn
= rnh
->rnh_lookup(dst
, netmask
, rnh
);
1834 rn
= rnh
->rnh_matchaddr(dst
, rnh
);
1839 * Scoped route lookup:
1841 * We first perform a non-scoped lookup for the original result.
1842 * Afterwards, depending on whether or not the caller has specified
1843 * a scope, we perform a more specific scoped search and fallback
1844 * to this original result upon failure.
1846 rn0
= rn
= node_lookup(dst
, netmask
, IFSCOPE_NONE
);
1849 * If the caller did not specify a scope, use the primary scope
1850 * derived from the system's non-scoped default route. If, for
1851 * any reason, there is no primary interface, return what we have.
1853 if (dontcare
&& (ifscope
= get_primary_ifscope()) == IFSCOPE_NONE
)
1857 * Keep the original result if either of the following is true:
1859 * 1) The interface portion of the route has the same interface
1860 * index as the scope value and it is marked with RTF_IFSCOPE.
1861 * 2) The route uses the loopback interface, in which case the
1862 * destination (host/net) is local/loopback.
1864 * Otherwise, do a more specified search using the scope.
1867 struct rtentry
*rt
= RT(rn
);
1868 if (rt
->rt_ifp
!= lo_ifp
) {
1869 if (rt
->rt_ifp
->if_index
!= ifscope
) {
1871 * Wrong interface; keep the original result
1872 * only if the caller did not specify a scope,
1873 * and do a more specific scoped search using
1874 * the scope of the found route. Otherwise,
1875 * start again from scratch.
1879 ifscope
= rt
->rt_ifp
->if_index
;
1882 } else if (!(rt
->rt_flags
& RTF_IFSCOPE
)) {
1884 * Right interface, except that this route
1885 * isn't marked with RTF_IFSCOPE. Do a more
1886 * specific scoped search. Keep the original
1887 * result and return it it in case the scoped
1896 * Scoped search. Find the most specific entry having the same
1897 * interface scope as the one requested. The following will result
1898 * in searching for the longest prefix scoped match.
1901 rn
= node_lookup(dst
, netmask
, ifscope
);
1904 * Use the original result if either of the following is true:
1906 * 1) The scoped search did not yield any result.
1907 * 2) The result from the scoped search is a scoped default route,
1908 * and the original (non-scoped) result is not a default route,
1909 * i.e. the original result is a more specific host/net route.
1910 * 3) The scoped search yielded a net route but the original
1911 * result is a host route, i.e. the original result is treated
1912 * as a more specific route.
1914 if (rn
== NULL
|| (rn0
!= NULL
&&
1915 ((INET_DEFAULT(rt_key(RT(rn
))) && !INET_DEFAULT(rt_key(RT(rn0
)))) ||
1916 (!RT_HOST(rn
) && RT_HOST(rn0
)))))
1920 * If we still don't have a route, use the non-scoped default
1921 * route as long as the interface portion satistifes the scope.
1923 if (rn
== NULL
&& (rn
= node_lookup_default()) != NULL
&&
1924 RT(rn
)->rt_ifp
->if_index
!= ifscope
)
1928 if (rn
!= NULL
&& !lookup_only
)
1929 (void) in_validate(rn
);
1932 if (rn
!= NULL
&& (rn
->rn_flags
& RNF_ROOT
))
1934 else if (rn
!= NULL
)
1941 * Set up a routing table entry, normally
1945 rtinit(struct ifaddr
*ifa
, int cmd
, int flags
)
1948 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1949 lck_mtx_lock(rt_mtx
);
1950 error
= rtinit_locked(ifa
, cmd
, flags
);
1951 lck_mtx_unlock(rt_mtx
);
1956 rtinit_locked(struct ifaddr
*ifa
, int cmd
, int flags
)
1959 struct sockaddr
*dst
;
1960 struct sockaddr
*deldst
;
1962 struct rtentry
*nrt
= 0;
1965 dst
= flags
& RTF_HOST
? ifa
->ifa_dstaddr
: ifa
->ifa_addr
;
1967 * If it's a delete, check that if it exists, it's on the correct
1968 * interface or we might scrub a route to another ifa which would
1969 * be confusing at best and possibly worse.
1971 if (cmd
== RTM_DELETE
) {
1973 * It's a delete, so it should already exist..
1974 * If it's a net, mask off the host bits
1975 * (Assuming we have a mask)
1977 if ((flags
& RTF_HOST
) == 0 && ifa
->ifa_netmask
) {
1978 m
= m_get(M_DONTWAIT
, MT_SONAME
);
1982 deldst
= mtod(m
, struct sockaddr
*);
1983 rt_maskedcopy(dst
, deldst
, ifa
->ifa_netmask
);
1987 * Get an rtentry that is in the routing tree and
1988 * contains the correct info. (if this fails, can't get there).
1989 * We set "report" to FALSE so that if it doesn't exist,
1990 * it doesn't report an error or clone a route, etc. etc.
1992 rt
= rtalloc1_locked(dst
, 0, 0UL);
1995 * Ok so we found the rtentry. it has an extra reference
1996 * for us at this stage. we won't need that so
2000 if (rt
->rt_ifa
!= ifa
) {
2002 * If the interface in the rtentry doesn't match
2003 * the interface we are using, then we don't
2004 * want to delete it, so return an error.
2005 * This seems to be the only point of
2006 * this whole RTM_DELETE clause.
2010 return (flags
& RTF_HOST
? EHOSTUNREACH
2018 * One would think that as we are deleting, and we know
2019 * it doesn't exist, we could just return at this point
2020 * with an "ELSE" clause, but apparently not..
2022 lck_mtx_unlock(rt_mtx
);
2023 return (flags
& RTF_HOST
? EHOSTUNREACH
2029 * Do the actual request
2031 error
= rtrequest_locked(cmd
, dst
, ifa
->ifa_addr
, ifa
->ifa_netmask
,
2032 flags
| ifa
->ifa_flags
, &nrt
);
2036 * If we are deleting, and we found an entry, then
2037 * it's been removed from the tree.. now throw it away.
2039 if (cmd
== RTM_DELETE
&& error
== 0 && (rt
= nrt
)) {
2041 * notify any listenning routing agents of the change
2043 rt_newaddrmsg(cmd
, ifa
, error
, nrt
);
2050 * We are adding, and we have a returned routing entry.
2051 * We need to sanity check the result.
2053 if (cmd
== RTM_ADD
&& error
== 0 && (rt
= nrt
)) {
2055 * If it came back with an unexpected interface, then it must
2056 * have already existed or something. (XXX)
2058 if (rt
->rt_ifa
!= ifa
) {
2059 if (!(rt
->rt_ifa
->ifa_ifp
->if_flags
&
2060 (IFF_POINTOPOINT
|IFF_LOOPBACK
)))
2061 printf("rtinit: wrong ifa (%p) was (%p)\n",
2064 * Ask that the protocol in question
2065 * remove anything it has associated with
2066 * this route and ifaddr.
2068 if (rt
->rt_ifa
->ifa_rtrequest
)
2069 rt
->rt_ifa
->ifa_rtrequest(RTM_DELETE
, rt
, SA(0));
2071 * Set the route's ifa.
2075 * And substitute in references to the ifaddr
2078 rt
->rt_ifp
= ifa
->ifa_ifp
;
2079 rt
->rt_rmx
.rmx_mtu
= ifa
->ifa_ifp
->if_mtu
; /*XXX*/
2081 * Now ask the protocol to check if it needs
2082 * any special processing in its new form.
2084 if (ifa
->ifa_rtrequest
)
2085 ifa
->ifa_rtrequest(RTM_ADD
, rt
, SA(0));
2088 * notify any listenning routing agents of the change
2090 rt_newaddrmsg(cmd
, ifa
, error
, nrt
);
2094 * We just wanted to add it; we don't actually need a
2095 * reference. This will result in a route that's added
2096 * to the routing table without a reference count. The
2097 * RTM_DELETE code will do the necessary step to adjust
2098 * the reference count at deletion time.
2108 if (rte_debug
& RTD_DEBUG
)
2109 return (rte_alloc_debug());
2111 return ((struct rtentry
*)zalloc(rte_zone
));
2115 rte_free(struct rtentry
*p
)
2117 if (rte_debug
& RTD_DEBUG
) {
2122 if (p
->rt_refcnt
!= 0)
2123 panic("rte_free: rte=%p refcnt=%d non-zero\n", p
, p
->rt_refcnt
);
2128 static inline struct rtentry
*
2129 rte_alloc_debug(void)
2131 struct rtentry_dbg
*rte
;
2133 rte
= ((struct rtentry_dbg
*)zalloc(rte_zone
));
2135 bzero(rte
, sizeof (*rte
));
2136 if (rte_debug
& RTD_TRACE
) {
2137 rte
->rtd_alloc_thread
= current_thread();
2138 (void) OSBacktrace(rte
->rtd_alloc_stk_pc
,
2141 rte
->rtd_inuse
= RTD_INUSE
;
2143 return ((struct rtentry
*)rte
);
2147 rte_free_debug(struct rtentry
*p
)
2149 struct rtentry_dbg
*rte
= (struct rtentry_dbg
*)p
;
2151 if (p
->rt_refcnt
!= 0)
2152 panic("rte_free: rte=%p refcnt=%d\n", p
, p
->rt_refcnt
);
2154 if (rte
->rtd_inuse
== RTD_FREED
)
2155 panic("rte_free: double free rte=%p\n", rte
);
2156 else if (rte
->rtd_inuse
!= RTD_INUSE
)
2157 panic("rte_free: corrupted rte=%p\n", rte
);
2159 bcopy((caddr_t
)p
, (caddr_t
)&rte
->rtd_entry_saved
, sizeof (*p
));
2160 bzero((caddr_t
)p
, sizeof (*p
));
2162 rte
->rtd_inuse
= RTD_FREED
;
2164 if (rte_debug
& RTD_TRACE
) {
2165 rte
->rtd_free_thread
= current_thread();
2166 (void) OSBacktrace(rte
->rtd_free_stk_pc
, RTD_TRSTACK_SIZE
);
2169 if (!(rte_debug
& RTD_NO_FREE
))