xref: /openbsd-src/sys/net/route.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /*	$OpenBSD: route.c,v 1.394 2020/06/24 22:03:43 cheloha Exp $	*/
2 /*	$NetBSD: route.c,v 1.14 1996/02/13 22:00:46 christos Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1980, 1986, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)route.c	8.2 (Berkeley) 11/15/93
62  */
63 
64 /*
65  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
66  *
67  * NRL grants permission for redistribution and use in source and binary
68  * forms, with or without modification, of the software and documentation
69  * created at NRL provided that the following conditions are met:
70  *
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce the above copyright
74  *    notice, this list of conditions and the following disclaimer in the
75  *    documentation and/or other materials provided with the distribution.
76  * 3. All advertising materials mentioning features or use of this software
77  *    must display the following acknowledgements:
78  *	This product includes software developed by the University of
79  *	California, Berkeley and its contributors.
80  *	This product includes software developed at the Information
81  *	Technology Division, US Naval Research Laboratory.
82  * 4. Neither the name of the NRL nor the names of its contributors
83  *    may be used to endorse or promote products derived from this software
84  *    without specific prior written permission.
85  *
86  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
87  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
88  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
89  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
90  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
91  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
92  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
93  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
94  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
95  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
96  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  *
98  * The views and conclusions contained in the software and documentation
99  * are those of the authors and should not be interpreted as representing
100  * official policies, either expressed or implied, of the US Naval
101  * Research Laboratory (NRL).
102  */
103 
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/mbuf.h>
107 #include <sys/socket.h>
108 #include <sys/socketvar.h>
109 #include <sys/timeout.h>
110 #include <sys/domain.h>
111 #include <sys/protosw.h>
112 #include <sys/ioctl.h>
113 #include <sys/kernel.h>
114 #include <sys/queue.h>
115 #include <sys/pool.h>
116 #include <sys/atomic.h>
117 
118 #include <net/if.h>
119 #include <net/if_var.h>
120 #include <net/if_dl.h>
121 #include <net/route.h>
122 
123 #include <netinet/in.h>
124 #include <netinet/ip_var.h>
125 #include <netinet/in_var.h>
126 
127 #ifdef INET6
128 #include <netinet/ip6.h>
129 #include <netinet6/ip6_var.h>
130 #include <netinet6/in6_var.h>
131 #endif
132 
133 #ifdef MPLS
134 #include <netmpls/mpls.h>
135 #endif
136 
137 #ifdef BFD
138 #include <net/bfd.h>
139 #endif
140 
141 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
142 
143 /* Give some jitter to hash, to avoid synchronization between routers. */
144 static uint32_t		rt_hashjitter;
145 
146 extern unsigned int	rtmap_limit;
147 
148 struct cpumem *		rtcounters;
149 int			rttrash;	/* routes not in table but not freed */
150 int			ifatrash;	/* ifas not in ifp list but not free */
151 
152 struct pool		rtentry_pool;	/* pool for rtentry structures */
153 struct pool		rttimer_pool;	/* pool for rttimer structures */
154 
155 void	rt_timer_init(void);
156 int	rt_setgwroute(struct rtentry *, u_int);
157 void	rt_putgwroute(struct rtentry *);
158 int	rtflushclone1(struct rtentry *, void *, u_int);
159 int	rtflushclone(struct rtentry *, unsigned int);
160 int	rt_ifa_purge_walker(struct rtentry *, void *, unsigned int);
161 struct rtentry *rt_match(struct sockaddr *, uint32_t *, int, unsigned int);
162 int	rt_clone(struct rtentry **, struct sockaddr *, unsigned int);
163 struct sockaddr *rt_plentosa(sa_family_t, int, struct sockaddr_in6 *);
164 static int rt_copysa(struct sockaddr *, struct sockaddr *, struct sockaddr **);
165 
166 #ifdef DDB
167 void	db_print_sa(struct sockaddr *);
168 void	db_print_ifa(struct ifaddr *);
169 int	db_show_rtentry(struct rtentry *, void *, unsigned int);
170 #endif
171 
172 #define	LABELID_MAX	50000
173 
174 struct rt_label {
175 	TAILQ_ENTRY(rt_label)	rtl_entry;
176 	char			rtl_name[RTLABEL_LEN];
177 	u_int16_t		rtl_id;
178 	int			rtl_ref;
179 };
180 
181 TAILQ_HEAD(rt_labels, rt_label)	rt_labels = TAILQ_HEAD_INITIALIZER(rt_labels);
182 
183 void
184 route_init(void)
185 {
186 	rtcounters = counters_alloc(rts_ncounters);
187 
188 	pool_init(&rtentry_pool, sizeof(struct rtentry), 0, IPL_SOFTNET, 0,
189 	    "rtentry", NULL);
190 
191 	while (rt_hashjitter == 0)
192 		rt_hashjitter = arc4random();
193 
194 #ifdef BFD
195 	bfdinit();
196 #endif
197 }
198 
199 /*
200  * Returns 1 if the (cached) ``rt'' entry is still valid, 0 otherwise.
201  */
202 int
203 rtisvalid(struct rtentry *rt)
204 {
205 	if (rt == NULL)
206 		return (0);
207 
208 	if (!ISSET(rt->rt_flags, RTF_UP))
209 		return (0);
210 
211 	if (ISSET(rt->rt_flags, RTF_GATEWAY)) {
212 		KASSERT(rt->rt_gwroute != NULL);
213 		KASSERT(!ISSET(rt->rt_gwroute->rt_flags, RTF_GATEWAY));
214 		if (!ISSET(rt->rt_gwroute->rt_flags, RTF_UP))
215 			return (0);
216 	}
217 
218 	return (1);
219 }
220 
221 /*
222  * Do the actual lookup for rtalloc(9), do not use directly!
223  *
224  * Return the best matching entry for the destination ``dst''.
225  *
226  * "RT_RESOLVE" means that a corresponding L2 entry should
227  *   be added to the routing table and resolved (via ARP or
228  *   NDP), if it does not exist.
229  */
230 struct rtentry *
231 rt_match(struct sockaddr *dst, uint32_t *src, int flags, unsigned int tableid)
232 {
233 	struct rtentry		*rt = NULL;
234 
235 	rt = rtable_match(tableid, dst, src);
236 	if (rt == NULL) {
237 		rtstat_inc(rts_unreach);
238 		return (NULL);
239 	}
240 
241 	if (ISSET(rt->rt_flags, RTF_CLONING) && ISSET(flags, RT_RESOLVE))
242 		rt_clone(&rt, dst, tableid);
243 
244 	rt->rt_use++;
245 	return (rt);
246 }
247 
248 int
249 rt_clone(struct rtentry **rtp, struct sockaddr *dst, unsigned int rtableid)
250 {
251 	struct rt_addrinfo	 info;
252 	struct rtentry		*rt = *rtp;
253 	int			 error = 0;
254 
255 	memset(&info, 0, sizeof(info));
256 	info.rti_info[RTAX_DST] = dst;
257 
258 	/*
259 	 * The priority of cloned route should be different
260 	 * to avoid conflict with /32 cloning routes.
261 	 *
262 	 * It should also be higher to let the ARP layer find
263 	 * cloned routes instead of the cloning one.
264 	 */
265 	KERNEL_LOCK();
266 	error = rtrequest(RTM_RESOLVE, &info, rt->rt_priority - 1, &rt,
267 	    rtableid);
268 	KERNEL_UNLOCK();
269 	if (error) {
270 		rtm_miss(RTM_MISS, &info, 0, RTP_NONE, 0, error, rtableid);
271 	} else {
272 		/* Inform listeners of the new route */
273 		rtm_send(rt, RTM_ADD, 0, rtableid);
274 		rtfree(*rtp);
275 		*rtp = rt;
276 	}
277 	return (error);
278 }
279 
280 /*
281  * Originated from bridge_hash() in if_bridge.c
282  */
283 #define mix(a, b, c) do {						\
284 	a -= b; a -= c; a ^= (c >> 13);					\
285 	b -= c; b -= a; b ^= (a << 8);					\
286 	c -= a; c -= b; c ^= (b >> 13);					\
287 	a -= b; a -= c; a ^= (c >> 12);					\
288 	b -= c; b -= a; b ^= (a << 16);					\
289 	c -= a; c -= b; c ^= (b >> 5);					\
290 	a -= b; a -= c; a ^= (c >> 3);					\
291 	b -= c; b -= a; b ^= (a << 10);					\
292 	c -= a; c -= b; c ^= (b >> 15);					\
293 } while (0)
294 
295 int
296 rt_hash(struct rtentry *rt, struct sockaddr *dst, uint32_t *src)
297 {
298 	uint32_t a, b, c;
299 
300 	if (src == NULL || !rtisvalid(rt) || !ISSET(rt->rt_flags, RTF_MPATH))
301 		return (-1);
302 
303 	a = b = 0x9e3779b9;
304 	c = rt_hashjitter;
305 
306 	switch (dst->sa_family) {
307 	case AF_INET:
308 	    {
309 		struct sockaddr_in *sin;
310 
311 		if (!ipmultipath)
312 			return (-1);
313 
314 		sin = satosin(dst);
315 		a += sin->sin_addr.s_addr;
316 		b += src[0];
317 		mix(a, b, c);
318 		break;
319 	    }
320 #ifdef INET6
321 	case AF_INET6:
322 	    {
323 		struct sockaddr_in6 *sin6;
324 
325 		if (!ip6_multipath)
326 			return (-1);
327 
328 		sin6 = satosin6(dst);
329 		a += sin6->sin6_addr.s6_addr32[0];
330 		b += sin6->sin6_addr.s6_addr32[2];
331 		c += src[0];
332 		mix(a, b, c);
333 		a += sin6->sin6_addr.s6_addr32[1];
334 		b += sin6->sin6_addr.s6_addr32[3];
335 		c += src[1];
336 		mix(a, b, c);
337 		a += sin6->sin6_addr.s6_addr32[2];
338 		b += sin6->sin6_addr.s6_addr32[1];
339 		c += src[2];
340 		mix(a, b, c);
341 		a += sin6->sin6_addr.s6_addr32[3];
342 		b += sin6->sin6_addr.s6_addr32[0];
343 		c += src[3];
344 		mix(a, b, c);
345 		break;
346 	    }
347 #endif /* INET6 */
348 	}
349 
350 	return (c & 0xffff);
351 }
352 
353 /*
354  * Allocate a route, potentially using multipath to select the peer.
355  */
356 struct rtentry *
357 rtalloc_mpath(struct sockaddr *dst, uint32_t *src, unsigned int rtableid)
358 {
359 	return (rt_match(dst, src, RT_RESOLVE, rtableid));
360 }
361 
362 /*
363  * Look in the routing table for the best matching entry for
364  * ``dst''.
365  *
366  * If a route with a gateway is found and its next hop is no
367  * longer valid, try to cache it.
368  */
369 struct rtentry *
370 rtalloc(struct sockaddr *dst, int flags, unsigned int rtableid)
371 {
372 	return (rt_match(dst, NULL, flags, rtableid));
373 }
374 
375 /*
376  * Cache the route entry corresponding to a reachable next hop in
377  * the gateway entry ``rt''.
378  */
379 int
380 rt_setgwroute(struct rtentry *rt, u_int rtableid)
381 {
382 	struct rtentry *prt, *nhrt;
383 	unsigned int rdomain = rtable_l2(rtableid);
384 	int error;
385 
386 	NET_ASSERT_LOCKED();
387 
388 	KASSERT(ISSET(rt->rt_flags, RTF_GATEWAY));
389 
390 	/* If we cannot find a valid next hop bail. */
391 	nhrt = rt_match(rt->rt_gateway, NULL, RT_RESOLVE, rdomain);
392 	if (nhrt == NULL)
393 		return (ENOENT);
394 
395 	/* Next hop entry must be on the same interface. */
396 	if (nhrt->rt_ifidx != rt->rt_ifidx) {
397 		struct sockaddr_in6	sa_mask;
398 
399 		if (!ISSET(nhrt->rt_flags, RTF_LLINFO) ||
400 		    !ISSET(nhrt->rt_flags, RTF_CLONED)) {
401 			rtfree(nhrt);
402 			return (EHOSTUNREACH);
403 		}
404 
405 		/*
406 		 * We found a L2 entry, so we might have multiple
407 		 * RTF_CLONING routes for the same subnet.  Query
408 		 * the first route of the multipath chain and iterate
409 		 * until we find the correct one.
410 		 */
411 		prt = rtable_lookup(rdomain, rt_key(nhrt->rt_parent),
412 		    rt_plen2mask(nhrt->rt_parent, &sa_mask), NULL, RTP_ANY);
413 		rtfree(nhrt);
414 
415 		while (prt != NULL && prt->rt_ifidx != rt->rt_ifidx)
416 			prt = rtable_iterate(prt);
417 
418 		/* We found nothing or a non-cloning MPATH route. */
419 		if (prt == NULL || !ISSET(prt->rt_flags, RTF_CLONING)) {
420 			rtfree(prt);
421 			return (EHOSTUNREACH);
422 		}
423 
424 		error = rt_clone(&prt, rt->rt_gateway, rdomain);
425 		if (error) {
426 			rtfree(prt);
427 			return (error);
428 		}
429 		nhrt = prt;
430 	}
431 
432 	/*
433 	 * Next hop must be reachable, this also prevents rtentry
434 	 * loops for example when rt->rt_gwroute points to rt.
435 	 */
436 	if (ISSET(nhrt->rt_flags, RTF_CLONING|RTF_GATEWAY)) {
437 		rtfree(nhrt);
438 		return (ENETUNREACH);
439 	}
440 
441 	/* Next hop is valid so remove possible old cache. */
442 	rt_putgwroute(rt);
443 	KASSERT(rt->rt_gwroute == NULL);
444 
445 	/*
446 	 * If the MTU of next hop is 0, this will reset the MTU of the
447 	 * route to run PMTUD again from scratch.
448 	 */
449 	if (!ISSET(rt->rt_locks, RTV_MTU) && (rt->rt_mtu > nhrt->rt_mtu))
450 		rt->rt_mtu = nhrt->rt_mtu;
451 
452 	/*
453 	 * To avoid reference counting problems when writting link-layer
454 	 * addresses in an outgoing packet, we ensure that the lifetime
455 	 * of a cached entry is greater than the bigger lifetime of the
456 	 * gateway entries it is pointed by.
457 	 */
458 	nhrt->rt_flags |= RTF_CACHED;
459 	nhrt->rt_cachecnt++;
460 
461 	rt->rt_gwroute = nhrt;
462 
463 	return (0);
464 }
465 
466 /*
467  * Invalidate the cached route entry of the gateway entry ``rt''.
468  */
469 void
470 rt_putgwroute(struct rtentry *rt)
471 {
472 	struct rtentry *nhrt = rt->rt_gwroute;
473 
474 	NET_ASSERT_LOCKED();
475 
476 	if (!ISSET(rt->rt_flags, RTF_GATEWAY) || nhrt == NULL)
477 		return;
478 
479 	KASSERT(ISSET(nhrt->rt_flags, RTF_CACHED));
480 	KASSERT(nhrt->rt_cachecnt > 0);
481 
482 	--nhrt->rt_cachecnt;
483 	if (nhrt->rt_cachecnt == 0)
484 		nhrt->rt_flags &= ~RTF_CACHED;
485 
486 	rtfree(rt->rt_gwroute);
487 	rt->rt_gwroute = NULL;
488 }
489 
490 void
491 rtref(struct rtentry *rt)
492 {
493 	atomic_inc_int(&rt->rt_refcnt);
494 }
495 
496 void
497 rtfree(struct rtentry *rt)
498 {
499 	int		 refcnt;
500 
501 	if (rt == NULL)
502 		return;
503 
504 	refcnt = (int)atomic_dec_int_nv(&rt->rt_refcnt);
505 	if (refcnt <= 0) {
506 		KASSERT(!ISSET(rt->rt_flags, RTF_UP));
507 		KASSERT(!RT_ROOT(rt));
508 		atomic_dec_int(&rttrash);
509 		if (refcnt < 0) {
510 			printf("rtfree: %p not freed (neg refs)\n", rt);
511 			return;
512 		}
513 
514 		KERNEL_LOCK();
515 		rt_timer_remove_all(rt);
516 		ifafree(rt->rt_ifa);
517 		rtlabel_unref(rt->rt_labelid);
518 #ifdef MPLS
519 		rt_mpls_clear(rt);
520 #endif
521 		free(rt->rt_gateway, M_RTABLE, ROUNDUP(rt->rt_gateway->sa_len));
522 		free(rt_key(rt), M_RTABLE, rt_key(rt)->sa_len);
523 		KERNEL_UNLOCK();
524 
525 		pool_put(&rtentry_pool, rt);
526 	}
527 }
528 
529 void
530 ifafree(struct ifaddr *ifa)
531 {
532 	if (ifa == NULL)
533 		panic("ifafree");
534 	if (ifa->ifa_refcnt == 0) {
535 		ifatrash--;
536 		free(ifa, M_IFADDR, 0);
537 	} else
538 		ifa->ifa_refcnt--;
539 }
540 
541 /*
542  * Force a routing table entry to the specified
543  * destination to go through the given gateway.
544  * Normally called as a result of a routing redirect
545  * message from the network layer.
546  */
547 void
548 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
549     struct sockaddr *src, struct rtentry **rtp, unsigned int rdomain)
550 {
551 	struct rtentry		*rt;
552 	int			 error = 0;
553 	enum rtstat_counters	 stat = rts_ncounters;
554 	struct rt_addrinfo	 info;
555 	struct ifaddr		*ifa;
556 	unsigned int		 ifidx = 0;
557 	int			 flags = RTF_GATEWAY|RTF_HOST;
558 	uint8_t			 prio = RTP_NONE;
559 
560 	NET_ASSERT_LOCKED();
561 
562 	/* verify the gateway is directly reachable */
563 	rt = rtalloc(gateway, 0, rdomain);
564 	if (!rtisvalid(rt) || ISSET(rt->rt_flags, RTF_GATEWAY)) {
565 		rtfree(rt);
566 		error = ENETUNREACH;
567 		goto out;
568 	}
569 	ifidx = rt->rt_ifidx;
570 	ifa = rt->rt_ifa;
571 	rtfree(rt);
572 	rt = NULL;
573 
574 	rt = rtable_lookup(rdomain, dst, NULL, NULL, RTP_ANY);
575 	/*
576 	 * If the redirect isn't from our current router for this dst,
577 	 * it's either old or wrong.  If it redirects us to ourselves,
578 	 * we have a routing loop, perhaps as a result of an interface
579 	 * going down recently.
580 	 */
581 #define	equal(a1, a2) \
582 	((a1)->sa_len == (a2)->sa_len && \
583 	 bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
584 	if (rt != NULL && (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
585 		error = EINVAL;
586 	else if (ifa_ifwithaddr(gateway, rdomain) != NULL ||
587 	    (gateway->sa_family = AF_INET &&
588 	    in_broadcast(satosin(gateway)->sin_addr, rdomain)))
589 		error = EHOSTUNREACH;
590 	if (error)
591 		goto done;
592 	/*
593 	 * Create a new entry if we just got back a wildcard entry
594 	 * or the lookup failed.  This is necessary for hosts
595 	 * which use routing redirects generated by smart gateways
596 	 * to dynamically build the routing tables.
597 	 */
598 	if (rt == NULL)
599 		goto create;
600 	/*
601 	 * Don't listen to the redirect if it's
602 	 * for a route to an interface.
603 	 */
604 	if (ISSET(rt->rt_flags, RTF_GATEWAY)) {
605 		if (!ISSET(rt->rt_flags, RTF_HOST)) {
606 			/*
607 			 * Changing from route to net => route to host.
608 			 * Create new route, rather than smashing route to net.
609 			 */
610 create:
611 			rtfree(rt);
612 			flags |= RTF_DYNAMIC;
613 			bzero(&info, sizeof(info));
614 			info.rti_info[RTAX_DST] = dst;
615 			info.rti_info[RTAX_GATEWAY] = gateway;
616 			info.rti_ifa = ifa;
617 			info.rti_flags = flags;
618 			rt = NULL;
619 			error = rtrequest(RTM_ADD, &info, RTP_DEFAULT, &rt,
620 			    rdomain);
621 			if (error == 0) {
622 				flags = rt->rt_flags;
623 				prio = rt->rt_priority;
624 			}
625 			stat = rts_dynamic;
626 		} else {
627 			/*
628 			 * Smash the current notion of the gateway to
629 			 * this destination.  Should check about netmask!!!
630 			 */
631 			rt->rt_flags |= RTF_MODIFIED;
632 			flags |= RTF_MODIFIED;
633 			prio = rt->rt_priority;
634 			stat = rts_newgateway;
635 			rt_setgate(rt, gateway, rdomain);
636 		}
637 	} else
638 		error = EHOSTUNREACH;
639 done:
640 	if (rt) {
641 		if (rtp && !error)
642 			*rtp = rt;
643 		else
644 			rtfree(rt);
645 	}
646 out:
647 	if (error)
648 		rtstat_inc(rts_badredirect);
649 	else if (stat != rts_ncounters)
650 		rtstat_inc(stat);
651 	bzero((caddr_t)&info, sizeof(info));
652 	info.rti_info[RTAX_DST] = dst;
653 	info.rti_info[RTAX_GATEWAY] = gateway;
654 	info.rti_info[RTAX_AUTHOR] = src;
655 	rtm_miss(RTM_REDIRECT, &info, flags, prio, ifidx, error, rdomain);
656 }
657 
658 /*
659  * Delete a route and generate a message
660  */
661 int
662 rtdeletemsg(struct rtentry *rt, struct ifnet *ifp, u_int tableid)
663 {
664 	int			error;
665 	struct rt_addrinfo	info;
666 	struct sockaddr_in6	sa_mask;
667 
668 	KASSERT(rt->rt_ifidx == ifp->if_index);
669 
670 	/*
671 	 * Request the new route so that the entry is not actually
672 	 * deleted.  That will allow the information being reported to
673 	 * be accurate (and consistent with route_output()).
674 	 */
675 	memset(&info, 0, sizeof(info));
676 	info.rti_info[RTAX_DST] = rt_key(rt);
677 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
678 	if (!ISSET(rt->rt_flags, RTF_HOST))
679 		info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask);
680 	error = rtrequest_delete(&info, rt->rt_priority, ifp, &rt, tableid);
681 	rtm_send(rt, RTM_DELETE, error, tableid);
682 	if (error == 0)
683 		rtfree(rt);
684 	return (error);
685 }
686 
687 static inline int
688 rtequal(struct rtentry *a, struct rtentry *b)
689 {
690 	if (a == b)
691 		return 1;
692 
693 	if (memcmp(rt_key(a), rt_key(b), rt_key(a)->sa_len) == 0 &&
694 	    rt_plen(a) == rt_plen(b))
695 		return 1;
696 	else
697 		return 0;
698 }
699 
700 int
701 rtflushclone1(struct rtentry *rt, void *arg, u_int id)
702 {
703 	struct rtentry *cloningrt = arg;
704 	struct ifnet *ifp;
705 
706 	if (!ISSET(rt->rt_flags, RTF_CLONED))
707 		return 0;
708 
709 	/* Cached route must stay alive as long as their parent are alive. */
710 	if (ISSET(rt->rt_flags, RTF_CACHED) && (rt->rt_parent != cloningrt))
711 		return 0;
712 
713 	if (!rtequal(rt->rt_parent, cloningrt))
714 		return 0;
715 	/*
716 	 * This happens when an interface with a RTF_CLONING route is
717 	 * being detached.  In this case it's safe to bail because all
718 	 * the routes are being purged by rt_ifa_purge().
719 	 */
720 	ifp = if_get(rt->rt_ifidx);
721 	if (ifp == NULL)
722 	        return 0;
723 
724 	if_put(ifp);
725 	return EEXIST;
726 }
727 
728 int
729 rtflushclone(struct rtentry *parent, unsigned int rtableid)
730 {
731 	struct rtentry *rt = NULL;
732 	struct ifnet *ifp;
733 	int error;
734 
735 #ifdef DIAGNOSTIC
736 	if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
737 		panic("rtflushclone: called with a non-cloning route");
738 #endif
739 
740 	do {
741 		error = rtable_walk(rtableid, rt_key(parent)->sa_family, &rt,
742 		    rtflushclone1, parent);
743 		if (rt != NULL && error == EEXIST) {
744 			ifp = if_get(rt->rt_ifidx);
745 			if (ifp == NULL) {
746 				error = EAGAIN;
747 			} else {
748 				error = rtdeletemsg(rt, ifp, rtableid);
749 				if (error == 0)
750 					error = EAGAIN;
751 				if_put(ifp);
752 			}
753 		}
754 		rtfree(rt);
755 		rt = NULL;
756 	} while (error == EAGAIN);
757 
758 	return error;
759 
760 }
761 
762 int
763 rtrequest_delete(struct rt_addrinfo *info, u_int8_t prio, struct ifnet *ifp,
764     struct rtentry **ret_nrt, u_int tableid)
765 {
766 	struct rtentry	*rt;
767 	int		 error;
768 
769 	NET_ASSERT_LOCKED();
770 
771 	if (!rtable_exists(tableid))
772 		return (EAFNOSUPPORT);
773 	rt = rtable_lookup(tableid, info->rti_info[RTAX_DST],
774 	    info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], prio);
775 	if (rt == NULL)
776 		return (ESRCH);
777 
778 	/* Make sure that's the route the caller want to delete. */
779 	if (ifp != NULL && ifp->if_index != rt->rt_ifidx) {
780 		rtfree(rt);
781 		return (ESRCH);
782 	}
783 
784 #ifdef BFD
785 	if (ISSET(rt->rt_flags, RTF_BFD))
786 		bfdclear(rt);
787 #endif
788 
789 	error = rtable_delete(tableid, info->rti_info[RTAX_DST],
790 	    info->rti_info[RTAX_NETMASK], rt);
791 	if (error != 0) {
792 		rtfree(rt);
793 		return (ESRCH);
794 	}
795 
796 	/* Release next hop cache before flushing cloned entries. */
797 	rt_putgwroute(rt);
798 
799 	/* Clean up any cloned children. */
800 	if (ISSET(rt->rt_flags, RTF_CLONING))
801 		rtflushclone(rt, tableid);
802 
803 	rtfree(rt->rt_parent);
804 	rt->rt_parent = NULL;
805 
806 	rt->rt_flags &= ~RTF_UP;
807 
808 	KASSERT(ifp->if_index == rt->rt_ifidx);
809 	ifp->if_rtrequest(ifp, RTM_DELETE, rt);
810 
811 	atomic_inc_int(&rttrash);
812 
813 	if (ret_nrt != NULL)
814 		*ret_nrt = rt;
815 	else
816 		rtfree(rt);
817 
818 	return (0);
819 }
820 
821 int
822 rtrequest(int req, struct rt_addrinfo *info, u_int8_t prio,
823     struct rtentry **ret_nrt, u_int tableid)
824 {
825 	struct ifnet		*ifp;
826 	struct rtentry		*rt, *crt;
827 	struct ifaddr		*ifa;
828 	struct sockaddr		*ndst;
829 	struct sockaddr_rtlabel	*sa_rl, sa_rl2;
830 	struct sockaddr_dl	 sa_dl = { sizeof(sa_dl), AF_LINK };
831 	int			 error;
832 
833 	NET_ASSERT_LOCKED();
834 
835 	if (!rtable_exists(tableid))
836 		return (EAFNOSUPPORT);
837 	if (info->rti_flags & RTF_HOST)
838 		info->rti_info[RTAX_NETMASK] = NULL;
839 	switch (req) {
840 	case RTM_DELETE:
841 		return (EINVAL);
842 
843 	case RTM_RESOLVE:
844 		if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
845 			return (EINVAL);
846 		if ((rt->rt_flags & RTF_CLONING) == 0)
847 			return (EINVAL);
848 		KASSERT(rt->rt_ifa->ifa_ifp != NULL);
849 		info->rti_ifa = rt->rt_ifa;
850 		info->rti_flags = rt->rt_flags | (RTF_CLONED|RTF_HOST);
851 		info->rti_flags &= ~(RTF_CLONING|RTF_CONNECTED|RTF_STATIC);
852 		info->rti_info[RTAX_GATEWAY] = sdltosa(&sa_dl);
853 		info->rti_info[RTAX_LABEL] =
854 		    rtlabel_id2sa(rt->rt_labelid, &sa_rl2);
855 		/* FALLTHROUGH */
856 
857 	case RTM_ADD:
858 		if (info->rti_ifa == NULL)
859 			return (EINVAL);
860 		ifa = info->rti_ifa;
861 		ifp = ifa->ifa_ifp;
862 		if (prio == 0)
863 			prio = ifp->if_priority + RTP_STATIC;
864 
865 		error = rt_copysa(info->rti_info[RTAX_DST],
866 		    info->rti_info[RTAX_NETMASK], &ndst);
867 		if (error)
868 			return (error);
869 
870 		rt = pool_get(&rtentry_pool, PR_NOWAIT | PR_ZERO);
871 		if (rt == NULL) {
872 			free(ndst, M_RTABLE, ndst->sa_len);
873 			return (ENOBUFS);
874 		}
875 
876 		rt->rt_refcnt = 1;
877 		rt->rt_flags = info->rti_flags | RTF_UP;
878 		rt->rt_priority = prio;	/* init routing priority */
879 		LIST_INIT(&rt->rt_timer);
880 
881 		/* Check the link state if the table supports it. */
882 		if (rtable_mpath_capable(tableid, ndst->sa_family) &&
883 		    !ISSET(rt->rt_flags, RTF_LOCAL) &&
884 		    (!LINK_STATE_IS_UP(ifp->if_link_state) ||
885 		    !ISSET(ifp->if_flags, IFF_UP))) {
886 			rt->rt_flags &= ~RTF_UP;
887 			rt->rt_priority |= RTP_DOWN;
888 		}
889 
890 		if (info->rti_info[RTAX_LABEL] != NULL) {
891 			sa_rl = (struct sockaddr_rtlabel *)
892 			    info->rti_info[RTAX_LABEL];
893 			rt->rt_labelid = rtlabel_name2id(sa_rl->sr_label);
894 		}
895 
896 #ifdef MPLS
897 		/* We have to allocate additional space for MPLS infos */
898 		if (info->rti_flags & RTF_MPLS &&
899 		    (info->rti_info[RTAX_SRC] != NULL ||
900 		    info->rti_info[RTAX_DST]->sa_family == AF_MPLS)) {
901 			error = rt_mpls_set(rt, info->rti_info[RTAX_SRC],
902 			    info->rti_mpls);
903 			if (error) {
904 				free(ndst, M_RTABLE, ndst->sa_len);
905 				pool_put(&rtentry_pool, rt);
906 				return (error);
907 			}
908 		} else
909 			rt_mpls_clear(rt);
910 #endif
911 
912 		ifa->ifa_refcnt++;
913 		rt->rt_ifa = ifa;
914 		rt->rt_ifidx = ifp->if_index;
915 		/*
916 		 * Copy metrics and a back pointer from the cloned
917 		 * route's parent.
918 		 */
919 		if (ISSET(rt->rt_flags, RTF_CLONED)) {
920 			rtref(*ret_nrt);
921 			rt->rt_parent = *ret_nrt;
922 			rt->rt_rmx = (*ret_nrt)->rt_rmx;
923 		}
924 
925 		/*
926 		 * We must set rt->rt_gateway before adding ``rt'' to
927 		 * the routing table because the radix MPATH code use
928 		 * it to (re)order routes.
929 		 */
930 		if ((error = rt_setgate(rt, info->rti_info[RTAX_GATEWAY],
931 		    tableid))) {
932 			ifafree(ifa);
933 			rtfree(rt->rt_parent);
934 			rt_putgwroute(rt);
935 			free(rt->rt_gateway, M_RTABLE, 0);
936 			free(ndst, M_RTABLE, ndst->sa_len);
937 			pool_put(&rtentry_pool, rt);
938 			return (error);
939 		}
940 
941 		error = rtable_insert(tableid, ndst,
942 		    info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY],
943 		    rt->rt_priority, rt);
944 		if (error != 0 &&
945 		    (crt = rtable_match(tableid, ndst, NULL)) != NULL) {
946 			/* overwrite cloned route */
947 			if (ISSET(crt->rt_flags, RTF_CLONED) &&
948 			    !ISSET(crt->rt_flags, RTF_CACHED)) {
949 				struct ifnet *cifp;
950 
951 				cifp = if_get(crt->rt_ifidx);
952 				KASSERT(cifp != NULL);
953 				rtdeletemsg(crt, cifp, tableid);
954 				if_put(cifp);
955 
956 				error = rtable_insert(tableid, ndst,
957 				    info->rti_info[RTAX_NETMASK],
958 				    info->rti_info[RTAX_GATEWAY],
959 				    rt->rt_priority, rt);
960 			}
961 			rtfree(crt);
962 		}
963 		if (error != 0) {
964 			ifafree(ifa);
965 			rtfree(rt->rt_parent);
966 			rt_putgwroute(rt);
967 			free(rt->rt_gateway, M_RTABLE, 0);
968 			free(ndst, M_RTABLE, ndst->sa_len);
969 			pool_put(&rtentry_pool, rt);
970 			return (EEXIST);
971 		}
972 		ifp->if_rtrequest(ifp, req, rt);
973 
974 		if_group_routechange(info->rti_info[RTAX_DST],
975 			info->rti_info[RTAX_NETMASK]);
976 
977 		if (ret_nrt != NULL)
978 			*ret_nrt = rt;
979 		else
980 			rtfree(rt);
981 		break;
982 	}
983 
984 	return (0);
985 }
986 
987 int
988 rt_setgate(struct rtentry *rt, struct sockaddr *gate, u_int rtableid)
989 {
990 	int glen = ROUNDUP(gate->sa_len);
991 	struct sockaddr *sa;
992 
993 	if (rt->rt_gateway == NULL || glen != ROUNDUP(rt->rt_gateway->sa_len)) {
994 		sa = malloc(glen, M_RTABLE, M_NOWAIT);
995 		if (sa == NULL)
996 			return (ENOBUFS);
997 		if (rt->rt_gateway != NULL) {
998 			free(rt->rt_gateway, M_RTABLE,
999 			    ROUNDUP(rt->rt_gateway->sa_len));
1000 		}
1001 		rt->rt_gateway = sa;
1002 	}
1003 	memmove(rt->rt_gateway, gate, glen);
1004 
1005 	if (ISSET(rt->rt_flags, RTF_GATEWAY))
1006 		return (rt_setgwroute(rt, rtableid));
1007 
1008 	return (0);
1009 }
1010 
1011 /*
1012  * Return the route entry containing the next hop link-layer
1013  * address corresponding to ``rt''.
1014  */
1015 struct rtentry *
1016 rt_getll(struct rtentry *rt)
1017 {
1018 	if (ISSET(rt->rt_flags, RTF_GATEWAY)) {
1019 		KASSERT(rt->rt_gwroute != NULL);
1020 		return (rt->rt_gwroute);
1021 	}
1022 
1023 	return (rt);
1024 }
1025 
1026 void
1027 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst,
1028     struct sockaddr *netmask)
1029 {
1030 	u_char	*cp1 = (u_char *)src;
1031 	u_char	*cp2 = (u_char *)dst;
1032 	u_char	*cp3 = (u_char *)netmask;
1033 	u_char	*cplim = cp2 + *cp3;
1034 	u_char	*cplim2 = cp2 + *cp1;
1035 
1036 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1037 	cp3 += 2;
1038 	if (cplim > cplim2)
1039 		cplim = cplim2;
1040 	while (cp2 < cplim)
1041 		*cp2++ = *cp1++ & *cp3++;
1042 	if (cp2 < cplim2)
1043 		bzero(cp2, cplim2 - cp2);
1044 }
1045 
1046 /*
1047  * allocate new sockaddr structure based on the user supplied src and mask
1048  * that is useable for the routing table.
1049  */
1050 static int
1051 rt_copysa(struct sockaddr *src, struct sockaddr *mask, struct sockaddr **dst)
1052 {
1053 	static const u_char maskarray[] = {
1054 	    0x0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
1055 	struct sockaddr *ndst;
1056 	struct domain *dp;
1057 	u_char *csrc, *cdst;
1058 	int i, plen;
1059 
1060 	for (i = 0; (dp = domains[i]) != NULL; i++) {
1061 		if (dp->dom_rtoffset == 0)
1062 			continue;
1063 		if (src->sa_family == dp->dom_family)
1064 			break;
1065 	}
1066 	if (dp == NULL)
1067 		return (EAFNOSUPPORT);
1068 
1069 	if (src->sa_len < dp->dom_sasize)
1070 		return (EINVAL);
1071 
1072 	plen = rtable_satoplen(src->sa_family, mask);
1073 	if (plen == -1)
1074 		return (EINVAL);
1075 
1076 	ndst = malloc(dp->dom_sasize, M_RTABLE, M_NOWAIT|M_ZERO);
1077 	if (ndst == NULL)
1078 		return (ENOBUFS);
1079 
1080 	ndst->sa_family = src->sa_family;
1081 	ndst->sa_len = dp->dom_sasize;
1082 
1083 	csrc = (u_char *)src + dp->dom_rtoffset;
1084 	cdst = (u_char *)ndst + dp->dom_rtoffset;
1085 
1086 	memcpy(cdst, csrc, plen / 8);
1087 	if (plen % 8 != 0)
1088 		cdst[plen / 8] = csrc[plen / 8] & maskarray[plen % 8];
1089 
1090 	*dst = ndst;
1091 	return (0);
1092 }
1093 
1094 int
1095 rt_ifa_add(struct ifaddr *ifa, int flags, struct sockaddr *dst,
1096     unsigned int rdomain)
1097 {
1098 	struct ifnet		*ifp = ifa->ifa_ifp;
1099 	struct rtentry		*rt;
1100 	struct sockaddr_rtlabel	 sa_rl;
1101 	struct rt_addrinfo	 info;
1102 	uint8_t			 prio = ifp->if_priority + RTP_STATIC;
1103 	int			 error;
1104 
1105 	KASSERT(rdomain == rtable_l2(rdomain));
1106 
1107 	memset(&info, 0, sizeof(info));
1108 	info.rti_ifa = ifa;
1109 	info.rti_flags = flags;
1110 	info.rti_info[RTAX_DST] = dst;
1111 	if (flags & RTF_LLINFO)
1112 		info.rti_info[RTAX_GATEWAY] = sdltosa(ifp->if_sadl);
1113 	else
1114 		info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1115 	info.rti_info[RTAX_LABEL] = rtlabel_id2sa(ifp->if_rtlabelid, &sa_rl);
1116 
1117 #ifdef MPLS
1118 	if ((flags & RTF_MPLS) == RTF_MPLS)
1119 		info.rti_mpls = MPLS_OP_POP;
1120 #endif /* MPLS */
1121 
1122 	if ((flags & RTF_HOST) == 0)
1123 		info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1124 
1125 	if (flags & (RTF_LOCAL|RTF_BROADCAST))
1126 		prio = RTP_LOCAL;
1127 
1128 	if (flags & RTF_CONNECTED)
1129 		prio = ifp->if_priority + RTP_CONNECTED;
1130 
1131 	error = rtrequest(RTM_ADD, &info, prio, &rt, rdomain);
1132 	if (error == 0) {
1133 		/*
1134 		 * A local route is created for every address configured
1135 		 * on an interface, so use this information to notify
1136 		 * userland that a new address has been added.
1137 		 */
1138 		if (flags & RTF_LOCAL)
1139 			rtm_addr(RTM_NEWADDR, ifa);
1140 		rtm_send(rt, RTM_ADD, 0, rdomain);
1141 		rtfree(rt);
1142 	}
1143 	return (error);
1144 }
1145 
1146 int
1147 rt_ifa_del(struct ifaddr *ifa, int flags, struct sockaddr *dst,
1148     unsigned int rdomain)
1149 {
1150 	struct ifnet		*ifp = ifa->ifa_ifp;
1151 	struct rtentry		*rt;
1152 	struct mbuf		*m = NULL;
1153 	struct sockaddr		*deldst;
1154 	struct rt_addrinfo	 info;
1155 	struct sockaddr_rtlabel	 sa_rl;
1156 	uint8_t			 prio = ifp->if_priority + RTP_STATIC;
1157 	int			 error;
1158 
1159 	KASSERT(rdomain == rtable_l2(rdomain));
1160 
1161 	if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
1162 		m = m_get(M_DONTWAIT, MT_SONAME);
1163 		if (m == NULL)
1164 			return (ENOBUFS);
1165 		deldst = mtod(m, struct sockaddr *);
1166 		rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
1167 		dst = deldst;
1168 	}
1169 
1170 	memset(&info, 0, sizeof(info));
1171 	info.rti_ifa = ifa;
1172 	info.rti_flags = flags;
1173 	info.rti_info[RTAX_DST] = dst;
1174 	if ((flags & RTF_LLINFO) == 0)
1175 		info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1176 	info.rti_info[RTAX_LABEL] = rtlabel_id2sa(ifp->if_rtlabelid, &sa_rl);
1177 
1178 	if ((flags & RTF_HOST) == 0)
1179 		info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1180 
1181 	if (flags & (RTF_LOCAL|RTF_BROADCAST))
1182 		prio = RTP_LOCAL;
1183 
1184 	if (flags & RTF_CONNECTED)
1185 		prio = ifp->if_priority + RTP_CONNECTED;
1186 
1187 	error = rtrequest_delete(&info, prio, ifp, &rt, rdomain);
1188 	if (error == 0) {
1189 		rtm_send(rt, RTM_DELETE, 0, rdomain);
1190 		if (flags & RTF_LOCAL)
1191 			rtm_addr(RTM_DELADDR, ifa);
1192 		rtfree(rt);
1193 	}
1194 	m_free(m);
1195 
1196 	return (error);
1197 }
1198 
1199 /*
1200  * Add ifa's address as a local rtentry.
1201  */
1202 int
1203 rt_ifa_addlocal(struct ifaddr *ifa)
1204 {
1205 	struct ifnet *ifp = ifa->ifa_ifp;
1206 	struct rtentry *rt;
1207 	u_int flags = RTF_HOST|RTF_LOCAL;
1208 	int error = 0;
1209 
1210 	/*
1211 	 * If the configured address correspond to the magical "any"
1212 	 * address do not add a local route entry because that might
1213 	 * corrupt the routing tree which uses this value for the
1214 	 * default routes.
1215 	 */
1216 	switch (ifa->ifa_addr->sa_family) {
1217 	case AF_INET:
1218 		if (satosin(ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY)
1219 			return (0);
1220 		break;
1221 #ifdef INET6
1222 	case AF_INET6:
1223 		if (IN6_ARE_ADDR_EQUAL(&satosin6(ifa->ifa_addr)->sin6_addr,
1224 		    &in6addr_any))
1225 			return (0);
1226 		break;
1227 #endif
1228 	default:
1229 		break;
1230 	}
1231 
1232 	if (!ISSET(ifp->if_flags, (IFF_LOOPBACK|IFF_POINTOPOINT)))
1233 		flags |= RTF_LLINFO;
1234 
1235 	/* If there is no local entry, allocate one. */
1236 	rt = rtalloc(ifa->ifa_addr, 0, ifp->if_rdomain);
1237 	if (rt == NULL || ISSET(rt->rt_flags, flags) != flags) {
1238 		error = rt_ifa_add(ifa, flags | RTF_MPATH, ifa->ifa_addr,
1239 		    ifp->if_rdomain);
1240 	}
1241 	rtfree(rt);
1242 
1243 	return (error);
1244 }
1245 
1246 /*
1247  * Remove local rtentry of ifa's addresss if it exists.
1248  */
1249 int
1250 rt_ifa_dellocal(struct ifaddr *ifa)
1251 {
1252 	struct ifnet *ifp = ifa->ifa_ifp;
1253 	struct rtentry *rt;
1254 	u_int flags = RTF_HOST|RTF_LOCAL;
1255 	int error = 0;
1256 
1257 	/*
1258 	 * We do not add local routes for such address, so do not bother
1259 	 * removing them.
1260 	 */
1261 	switch (ifa->ifa_addr->sa_family) {
1262 	case AF_INET:
1263 		if (satosin(ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY)
1264 			return (0);
1265 		break;
1266 #ifdef INET6
1267 	case AF_INET6:
1268 		if (IN6_ARE_ADDR_EQUAL(&satosin6(ifa->ifa_addr)->sin6_addr,
1269 		    &in6addr_any))
1270 			return (0);
1271 		break;
1272 #endif
1273 	default:
1274 		break;
1275 	}
1276 
1277 	if (!ISSET(ifp->if_flags, (IFF_LOOPBACK|IFF_POINTOPOINT)))
1278 		flags |= RTF_LLINFO;
1279 
1280 	/*
1281 	 * Before deleting, check if a corresponding local host
1282 	 * route surely exists.  With this check, we can avoid to
1283 	 * delete an interface direct route whose destination is same
1284 	 * as the address being removed.  This can happen when removing
1285 	 * a subnet-router anycast address on an interface attached
1286 	 * to a shared medium.
1287 	 */
1288 	rt = rtalloc(ifa->ifa_addr, 0, ifp->if_rdomain);
1289 	if (rt != NULL && ISSET(rt->rt_flags, flags) == flags) {
1290 		error = rt_ifa_del(ifa, flags, ifa->ifa_addr,
1291 		    ifp->if_rdomain);
1292 	}
1293 	rtfree(rt);
1294 
1295 	return (error);
1296 }
1297 
1298 /*
1299  * Remove all addresses attached to ``ifa''.
1300  */
1301 void
1302 rt_ifa_purge(struct ifaddr *ifa)
1303 {
1304 	struct ifnet		*ifp = ifa->ifa_ifp;
1305 	struct rtentry		*rt = NULL;
1306 	unsigned int		 rtableid;
1307 	int			 error, af = ifa->ifa_addr->sa_family;
1308 
1309 	KASSERT(ifp != NULL);
1310 
1311 	for (rtableid = 0; rtableid < rtmap_limit; rtableid++) {
1312 		/* skip rtables that are not in the rdomain of the ifp */
1313 		if (rtable_l2(rtableid) != ifp->if_rdomain)
1314 			continue;
1315 
1316 		do {
1317 			error = rtable_walk(rtableid, af, &rt,
1318 			    rt_ifa_purge_walker, ifa);
1319 			if (rt != NULL && error == EEXIST) {
1320 				error = rtdeletemsg(rt, ifp, rtableid);
1321 				if (error == 0)
1322 					error = EAGAIN;
1323 			}
1324 			rtfree(rt);
1325 			rt = NULL;
1326 		} while (error == EAGAIN);
1327 
1328 		if (error == EAFNOSUPPORT)
1329 			error = 0;
1330 
1331 		if (error)
1332 			break;
1333 	}
1334 }
1335 
1336 int
1337 rt_ifa_purge_walker(struct rtentry *rt, void *vifa, unsigned int rtableid)
1338 {
1339 	struct ifaddr		*ifa = vifa;
1340 
1341 	if (rt->rt_ifa == ifa)
1342 		return EEXIST;
1343 
1344 	return 0;
1345 }
1346 
1347 /*
1348  * Route timer routines.  These routes allow functions to be called
1349  * for various routes at any time.  This is useful in supporting
1350  * path MTU discovery and redirect route deletion.
1351  *
1352  * This is similar to some BSDI internal functions, but it provides
1353  * for multiple queues for efficiency's sake...
1354  */
1355 
1356 LIST_HEAD(, rttimer_queue)	rttimer_queue_head;
1357 static int			rt_init_done = 0;
1358 
1359 #define RTTIMER_CALLOUT(r)	{					\
1360 	if (r->rtt_func != NULL) {					\
1361 		(*r->rtt_func)(r->rtt_rt, r);				\
1362 	} else {							\
1363 		struct ifnet *ifp;					\
1364 									\
1365 		ifp = if_get(r->rtt_rt->rt_ifidx);			\
1366 		if (ifp != NULL) 					\
1367 			rtdeletemsg(r->rtt_rt, ifp, r->rtt_tableid);	\
1368 		if_put(ifp);						\
1369 	}								\
1370 }
1371 
1372 /*
1373  * Some subtle order problems with domain initialization mean that
1374  * we cannot count on this being run from rt_init before various
1375  * protocol initializations are done.  Therefore, we make sure
1376  * that this is run when the first queue is added...
1377  */
1378 
1379 void
1380 rt_timer_init(void)
1381 {
1382 	static struct timeout	rt_timer_timeout;
1383 
1384 	if (rt_init_done)
1385 		panic("rt_timer_init: already initialized");
1386 
1387 	pool_init(&rttimer_pool, sizeof(struct rttimer), 0, IPL_SOFTNET, 0,
1388 	    "rttmr", NULL);
1389 
1390 	LIST_INIT(&rttimer_queue_head);
1391 	timeout_set_proc(&rt_timer_timeout, rt_timer_timer, &rt_timer_timeout);
1392 	timeout_add_sec(&rt_timer_timeout, 1);
1393 	rt_init_done = 1;
1394 }
1395 
1396 struct rttimer_queue *
1397 rt_timer_queue_create(u_int timeout)
1398 {
1399 	struct rttimer_queue	*rtq;
1400 
1401 	if (rt_init_done == 0)
1402 		rt_timer_init();
1403 
1404 	if ((rtq = malloc(sizeof(*rtq), M_RTABLE, M_NOWAIT|M_ZERO)) == NULL)
1405 		return (NULL);
1406 
1407 	rtq->rtq_timeout = timeout;
1408 	rtq->rtq_count = 0;
1409 	TAILQ_INIT(&rtq->rtq_head);
1410 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
1411 
1412 	return (rtq);
1413 }
1414 
1415 void
1416 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
1417 {
1418 	rtq->rtq_timeout = timeout;
1419 }
1420 
1421 void
1422 rt_timer_queue_destroy(struct rttimer_queue *rtq)
1423 {
1424 	struct rttimer	*r;
1425 
1426 	NET_ASSERT_LOCKED();
1427 
1428 	while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
1429 		LIST_REMOVE(r, rtt_link);
1430 		TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1431 		RTTIMER_CALLOUT(r);
1432 		pool_put(&rttimer_pool, r);
1433 		if (rtq->rtq_count > 0)
1434 			rtq->rtq_count--;
1435 		else
1436 			printf("rt_timer_queue_destroy: rtq_count reached 0\n");
1437 	}
1438 
1439 	LIST_REMOVE(rtq, rtq_link);
1440 	free(rtq, M_RTABLE, sizeof(*rtq));
1441 }
1442 
1443 unsigned long
1444 rt_timer_queue_count(struct rttimer_queue *rtq)
1445 {
1446 	return (rtq->rtq_count);
1447 }
1448 
1449 void
1450 rt_timer_remove_all(struct rtentry *rt)
1451 {
1452 	struct rttimer	*r;
1453 
1454 	while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
1455 		LIST_REMOVE(r, rtt_link);
1456 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1457 		if (r->rtt_queue->rtq_count > 0)
1458 			r->rtt_queue->rtq_count--;
1459 		else
1460 			printf("rt_timer_remove_all: rtq_count reached 0\n");
1461 		pool_put(&rttimer_pool, r);
1462 	}
1463 }
1464 
1465 int
1466 rt_timer_add(struct rtentry *rt, void (*func)(struct rtentry *,
1467     struct rttimer *), struct rttimer_queue *queue, u_int rtableid)
1468 {
1469 	struct rttimer	*r;
1470 	long		 current_time;
1471 
1472 	current_time = getuptime();
1473 	rt->rt_expire = getuptime() + queue->rtq_timeout;
1474 
1475 	/*
1476 	 * If there's already a timer with this action, destroy it before
1477 	 * we add a new one.
1478 	 */
1479 	LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
1480 		if (r->rtt_func == func) {
1481 			LIST_REMOVE(r, rtt_link);
1482 			TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1483 			if (r->rtt_queue->rtq_count > 0)
1484 				r->rtt_queue->rtq_count--;
1485 			else
1486 				printf("rt_timer_add: rtq_count reached 0\n");
1487 			pool_put(&rttimer_pool, r);
1488 			break;  /* only one per list, so we can quit... */
1489 		}
1490 	}
1491 
1492 	r = pool_get(&rttimer_pool, PR_NOWAIT | PR_ZERO);
1493 	if (r == NULL)
1494 		return (ENOBUFS);
1495 
1496 	r->rtt_rt = rt;
1497 	r->rtt_time = current_time;
1498 	r->rtt_func = func;
1499 	r->rtt_queue = queue;
1500 	r->rtt_tableid = rtableid;
1501 	LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
1502 	TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
1503 	r->rtt_queue->rtq_count++;
1504 
1505 	return (0);
1506 }
1507 
1508 void
1509 rt_timer_timer(void *arg)
1510 {
1511 	struct timeout		*to = (struct timeout *)arg;
1512 	struct rttimer_queue	*rtq;
1513 	struct rttimer		*r;
1514 	long			 current_time;
1515 
1516 	current_time = getuptime();
1517 
1518 	NET_LOCK();
1519 	LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
1520 		while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1521 		    (r->rtt_time + rtq->rtq_timeout) < current_time) {
1522 			LIST_REMOVE(r, rtt_link);
1523 			TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1524 			RTTIMER_CALLOUT(r);
1525 			pool_put(&rttimer_pool, r);
1526 			if (rtq->rtq_count > 0)
1527 				rtq->rtq_count--;
1528 			else
1529 				printf("rt_timer_timer: rtq_count reached 0\n");
1530 		}
1531 	}
1532 	NET_UNLOCK();
1533 
1534 	timeout_add_sec(to, 1);
1535 }
1536 
1537 #ifdef MPLS
1538 int
1539 rt_mpls_set(struct rtentry *rt, struct sockaddr *src, uint8_t op)
1540 {
1541 	struct sockaddr_mpls	*psa_mpls = (struct sockaddr_mpls *)src;
1542 	struct rt_mpls		*rt_mpls;
1543 
1544 	if (psa_mpls == NULL && op != MPLS_OP_POP)
1545 		return (EOPNOTSUPP);
1546 	if (psa_mpls != NULL && psa_mpls->smpls_len != sizeof(*psa_mpls))
1547 		return (EINVAL);
1548 	if (psa_mpls != NULL && psa_mpls->smpls_family != AF_MPLS)
1549 		return (EAFNOSUPPORT);
1550 
1551 	rt->rt_llinfo = malloc(sizeof(struct rt_mpls), M_TEMP, M_NOWAIT|M_ZERO);
1552 	if (rt->rt_llinfo == NULL)
1553 		return (ENOMEM);
1554 
1555 	rt_mpls = (struct rt_mpls *)rt->rt_llinfo;
1556 	if (psa_mpls != NULL)
1557 		rt_mpls->mpls_label = psa_mpls->smpls_label;
1558 	rt_mpls->mpls_operation = op;
1559 	/* XXX: set experimental bits */
1560 	rt->rt_flags |= RTF_MPLS;
1561 
1562 	return (0);
1563 }
1564 
1565 void
1566 rt_mpls_clear(struct rtentry *rt)
1567 {
1568 	if (rt->rt_llinfo != NULL && rt->rt_flags & RTF_MPLS) {
1569 		free(rt->rt_llinfo, M_TEMP, sizeof(struct rt_mpls));
1570 		rt->rt_llinfo = NULL;
1571 	}
1572 	rt->rt_flags &= ~RTF_MPLS;
1573 }
1574 #endif
1575 
1576 u_int16_t
1577 rtlabel_name2id(char *name)
1578 {
1579 	struct rt_label		*label, *p;
1580 	u_int16_t		 new_id = 1;
1581 
1582 	if (!name[0])
1583 		return (0);
1584 
1585 	TAILQ_FOREACH(label, &rt_labels, rtl_entry)
1586 		if (strcmp(name, label->rtl_name) == 0) {
1587 			label->rtl_ref++;
1588 			return (label->rtl_id);
1589 		}
1590 
1591 	/*
1592 	 * to avoid fragmentation, we do a linear search from the beginning
1593 	 * and take the first free slot we find. if there is none or the list
1594 	 * is empty, append a new entry at the end.
1595 	 */
1596 	TAILQ_FOREACH(p, &rt_labels, rtl_entry) {
1597 		if (p->rtl_id != new_id)
1598 			break;
1599 		new_id = p->rtl_id + 1;
1600 	}
1601 	if (new_id > LABELID_MAX)
1602 		return (0);
1603 
1604 	label = malloc(sizeof(*label), M_RTABLE, M_NOWAIT|M_ZERO);
1605 	if (label == NULL)
1606 		return (0);
1607 	strlcpy(label->rtl_name, name, sizeof(label->rtl_name));
1608 	label->rtl_id = new_id;
1609 	label->rtl_ref++;
1610 
1611 	if (p != NULL)	/* insert new entry before p */
1612 		TAILQ_INSERT_BEFORE(p, label, rtl_entry);
1613 	else		/* either list empty or no free slot in between */
1614 		TAILQ_INSERT_TAIL(&rt_labels, label, rtl_entry);
1615 
1616 	return (label->rtl_id);
1617 }
1618 
1619 const char *
1620 rtlabel_id2name(u_int16_t id)
1621 {
1622 	struct rt_label	*label;
1623 
1624 	TAILQ_FOREACH(label, &rt_labels, rtl_entry)
1625 		if (label->rtl_id == id)
1626 			return (label->rtl_name);
1627 
1628 	return (NULL);
1629 }
1630 
1631 struct sockaddr *
1632 rtlabel_id2sa(u_int16_t labelid, struct sockaddr_rtlabel *sa_rl)
1633 {
1634 	const char	*label;
1635 
1636 	if (labelid == 0 || (label = rtlabel_id2name(labelid)) == NULL)
1637 		return (NULL);
1638 
1639 	bzero(sa_rl, sizeof(*sa_rl));
1640 	sa_rl->sr_len = sizeof(*sa_rl);
1641 	sa_rl->sr_family = AF_UNSPEC;
1642 	strlcpy(sa_rl->sr_label, label, sizeof(sa_rl->sr_label));
1643 
1644 	return ((struct sockaddr *)sa_rl);
1645 }
1646 
1647 void
1648 rtlabel_unref(u_int16_t id)
1649 {
1650 	struct rt_label	*p, *next;
1651 
1652 	if (id == 0)
1653 		return;
1654 
1655 	TAILQ_FOREACH_SAFE(p, &rt_labels, rtl_entry, next) {
1656 		if (id == p->rtl_id) {
1657 			if (--p->rtl_ref == 0) {
1658 				TAILQ_REMOVE(&rt_labels, p, rtl_entry);
1659 				free(p, M_RTABLE, sizeof(*p));
1660 			}
1661 			break;
1662 		}
1663 	}
1664 }
1665 
1666 int
1667 rt_if_track(struct ifnet *ifp)
1668 {
1669 	unsigned int rtableid;
1670 	struct rtentry *rt = NULL;
1671 	int i, error = 0;
1672 
1673 	for (rtableid = 0; rtableid < rtmap_limit; rtableid++) {
1674 		/* skip rtables that are not in the rdomain of the ifp */
1675 		if (rtable_l2(rtableid) != ifp->if_rdomain)
1676 			continue;
1677 		for (i = 1; i <= AF_MAX; i++) {
1678 			if (!rtable_mpath_capable(rtableid, i))
1679 				continue;
1680 
1681 			do {
1682 				error = rtable_walk(rtableid, i, &rt,
1683 				    rt_if_linkstate_change, ifp);
1684 				if (rt != NULL && error == EEXIST) {
1685 					error = rtdeletemsg(rt, ifp, rtableid);
1686 					if (error == 0)
1687 						error = EAGAIN;
1688 				}
1689 				rtfree(rt);
1690 				rt = NULL;
1691 			} while (error == EAGAIN);
1692 
1693 			if (error == EAFNOSUPPORT)
1694 				error = 0;
1695 
1696 			if (error)
1697 				break;
1698 		}
1699 	}
1700 
1701 	return (error);
1702 }
1703 
1704 int
1705 rt_if_linkstate_change(struct rtentry *rt, void *arg, u_int id)
1706 {
1707 	struct ifnet *ifp = arg;
1708 	struct sockaddr_in6 sa_mask;
1709 	int error;
1710 
1711 	if (rt->rt_ifidx != ifp->if_index)
1712 		return (0);
1713 
1714 	/* Local routes are always usable. */
1715 	if (rt->rt_flags & RTF_LOCAL) {
1716 		rt->rt_flags |= RTF_UP;
1717 		return (0);
1718 	}
1719 
1720 	if (LINK_STATE_IS_UP(ifp->if_link_state) && ifp->if_flags & IFF_UP) {
1721 		if (ISSET(rt->rt_flags, RTF_UP))
1722 			return (0);
1723 
1724 		/* bring route up */
1725 		rt->rt_flags |= RTF_UP;
1726 		error = rtable_mpath_reprio(id, rt_key(rt), rt_plen(rt),
1727 		    rt->rt_priority & RTP_MASK, rt);
1728 	} else {
1729 		/*
1730 		 * Remove redirected and cloned routes (mainly ARP)
1731 		 * from down interfaces so we have a chance to get
1732 		 * new routes from a better source.
1733 		 */
1734 		if (ISSET(rt->rt_flags, RTF_CLONED|RTF_DYNAMIC) &&
1735 		    !ISSET(rt->rt_flags, RTF_CACHED|RTF_BFD)) {
1736 			return (EEXIST);
1737 		}
1738 
1739 		if (!ISSET(rt->rt_flags, RTF_UP))
1740 			return (0);
1741 
1742 		/* take route down */
1743 		rt->rt_flags &= ~RTF_UP;
1744 		error = rtable_mpath_reprio(id, rt_key(rt), rt_plen(rt),
1745 		    rt->rt_priority | RTP_DOWN, rt);
1746 	}
1747 	if_group_routechange(rt_key(rt), rt_plen2mask(rt, &sa_mask));
1748 
1749 	return (error);
1750 }
1751 
1752 struct sockaddr *
1753 rt_plentosa(sa_family_t af, int plen, struct sockaddr_in6 *sa_mask)
1754 {
1755 	struct sockaddr_in	*sin = (struct sockaddr_in *)sa_mask;
1756 #ifdef INET6
1757 	struct sockaddr_in6	*sin6 = (struct sockaddr_in6 *)sa_mask;
1758 #endif
1759 
1760 	KASSERT(plen >= 0 || plen == -1);
1761 
1762 	if (plen == -1)
1763 		return (NULL);
1764 
1765 	memset(sa_mask, 0, sizeof(*sa_mask));
1766 
1767 	switch (af) {
1768 	case AF_INET:
1769 		sin->sin_family = AF_INET;
1770 		sin->sin_len = sizeof(struct sockaddr_in);
1771 		in_prefixlen2mask(&sin->sin_addr, plen);
1772 		break;
1773 #ifdef INET6
1774 	case AF_INET6:
1775 		sin6->sin6_family = AF_INET6;
1776 		sin6->sin6_len = sizeof(struct sockaddr_in6);
1777 		in6_prefixlen2mask(&sin6->sin6_addr, plen);
1778 		break;
1779 #endif /* INET6 */
1780 	default:
1781 		return (NULL);
1782 	}
1783 
1784 	return ((struct sockaddr *)sa_mask);
1785 }
1786 
1787 struct sockaddr *
1788 rt_plen2mask(struct rtentry *rt, struct sockaddr_in6 *sa_mask)
1789 {
1790 	return (rt_plentosa(rt_key(rt)->sa_family, rt_plen(rt), sa_mask));
1791 }
1792 
1793 #ifdef DDB
1794 #include <machine/db_machdep.h>
1795 #include <ddb/db_output.h>
1796 
1797 void
1798 db_print_sa(struct sockaddr *sa)
1799 {
1800 	int len;
1801 	u_char *p;
1802 
1803 	if (sa == NULL) {
1804 		db_printf("[NULL]");
1805 		return;
1806 	}
1807 
1808 	p = (u_char *)sa;
1809 	len = sa->sa_len;
1810 	db_printf("[");
1811 	while (len > 0) {
1812 		db_printf("%d", *p);
1813 		p++;
1814 		len--;
1815 		if (len)
1816 			db_printf(",");
1817 	}
1818 	db_printf("]\n");
1819 }
1820 
1821 void
1822 db_print_ifa(struct ifaddr *ifa)
1823 {
1824 	if (ifa == NULL)
1825 		return;
1826 	db_printf("  ifa_addr=");
1827 	db_print_sa(ifa->ifa_addr);
1828 	db_printf("  ifa_dsta=");
1829 	db_print_sa(ifa->ifa_dstaddr);
1830 	db_printf("  ifa_mask=");
1831 	db_print_sa(ifa->ifa_netmask);
1832 	db_printf("  flags=0x%x, refcnt=%d, metric=%d\n",
1833 	    ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric);
1834 }
1835 
1836 /*
1837  * Function to pass to rtalble_walk().
1838  * Return non-zero error to abort walk.
1839  */
1840 int
1841 db_show_rtentry(struct rtentry *rt, void *w, unsigned int id)
1842 {
1843 	db_printf("rtentry=%p", rt);
1844 
1845 	db_printf(" flags=0x%x refcnt=%d use=%llu expire=%lld rtableid=%u\n",
1846 	    rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire, id);
1847 
1848 	db_printf(" key="); db_print_sa(rt_key(rt));
1849 	db_printf(" plen=%d", rt_plen(rt));
1850 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1851 	db_printf(" ifidx=%u ", rt->rt_ifidx);
1852 	db_printf(" ifa=%p\n", rt->rt_ifa);
1853 	db_print_ifa(rt->rt_ifa);
1854 
1855 	db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo);
1856 	return (0);
1857 }
1858 
1859 /*
1860  * Function to print all the route trees.
1861  * Use this from ddb:  "call db_show_arptab"
1862  */
1863 int
1864 db_show_arptab(void)
1865 {
1866 	db_printf("Route tree for AF_INET\n");
1867 	rtable_walk(0, AF_INET, NULL, db_show_rtentry, NULL);
1868 	return (0);
1869 }
1870 #endif /* DDB */
1871