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