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