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