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