xref: /freebsd/sys/net/route.c (revision cacdd70cc751fb68dec4b86c5e5b8c969b6e26ef)
1 /*-
2  * Copyright (c) 1980, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)route.c	8.3.1.1 (Berkeley) 2/23/95
30  * $FreeBSD$
31  */
32 /************************************************************************
33  * Note: In this file a 'fib' is a "forwarding information base"	*
34  * Which is the new name for an in kernel routing (next hop) table.	*
35  ***********************************************************************/
36 
37 #include "opt_inet.h"
38 #include "opt_route.h"
39 #include "opt_mrouting.h"
40 #include "opt_mpath.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysproto.h>
49 #include <sys/proc.h>
50 #include <sys/domain.h>
51 #include <sys/kernel.h>
52 
53 #include <net/if.h>
54 #include <net/route.h>
55 
56 #ifdef RADIX_MPATH
57 #include <net/radix_mpath.h>
58 #endif
59 
60 #include <netinet/in.h>
61 #include <netinet/ip_mroute.h>
62 
63 #include <vm/uma.h>
64 
65 #ifndef ROUTETABLES
66  #define RT_NUMFIBS 1
67  #define RT_MAXFIBS 1
68 #else
69  /* while we use 4 bits in the mbuf flags,
70   * we are limited to 16
71   */
72  #define RT_MAXFIBS 16
73  #if ROUTETABLES > RT_MAXFIBS
74   #define RT_NUMFIBS RT_MAXFIBS
75   #error "ROUTETABLES defined too big"
76  #else
77   #if ROUTETABLES == 0
78    #define RT_NUMFIBS 1
79   #else
80    #define RT_NUMFIBS ROUTETABLES
81   #endif
82  #endif
83 #endif
84 
85 u_int rt_numfibs = RT_NUMFIBS;
86 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
87 /*
88  * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
89  * We can't do more because storage is statically allocated for now.
90  * (for compatibility reasons.. this will change).
91  */
92 TUNABLE_INT("net.fibs", &rt_numfibs);
93 
94 /*
95  * By default add routes to all fibs for new interfaces.
96  * Once this is set to 0 then only allocate routes on interface
97  * changes for the FIB of the caller when adding a new set of addresses
98  * to an interface.  XXX this is a shotgun aproach to a problem that needs
99  * a more fine grained solution.. that will come.
100  */
101 u_int rt_add_addr_allfibs = 1;
102 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
103     &rt_add_addr_allfibs, 0, "");
104 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
105 
106 static struct rtstat rtstat;
107 
108 /* by default only the first 'row' of tables will be accessed. */
109 /*
110  * XXXMRT When we fix netstat, and do this differnetly,
111  * we can allocate this dynamically. As long as we are keeping
112  * things backwards compaitble we need to allocate this
113  * statically.
114  */
115 struct radix_node_head *rt_tables[RT_MAXFIBS][AF_MAX+1];
116 
117 static int	rttrash;		/* routes not in table but not freed */
118 
119 static void rt_maskedcopy(struct sockaddr *,
120 	    struct sockaddr *, struct sockaddr *);
121 
122 /* compare two sockaddr structures */
123 #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
124 
125 /*
126  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
127  * The operation can be done safely (in this code) because a
128  * 'struct rtentry' starts with two 'struct radix_node''s, the first
129  * one representing leaf nodes in the routing tree, which is
130  * what the code in radix.c passes us as a 'struct radix_node'.
131  *
132  * But because there are a lot of assumptions in this conversion,
133  * do not cast explicitly, but always use the macro below.
134  */
135 #define RNTORT(p)	((struct rtentry *)(p))
136 
137 static uma_zone_t rtzone;		/* Routing table UMA zone. */
138 
139 #if 0
140 /* default fib for tunnels to use */
141 u_int tunnel_fib = 0;
142 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
143 #endif
144 
145 /*
146  * handler for net.my_fibnum
147  */
148 static int
149 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
150 {
151         int fibnum;
152         int error;
153 
154         fibnum = curthread->td_proc->p_fibnum;
155         error = sysctl_handle_int(oidp, &fibnum, 0, req);
156         return (error);
157 }
158 
159 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
160             NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
161 
162 static void
163 route_init(void)
164 {
165 	int table;
166 	struct domain *dom;
167 	int fam;
168 
169 	/* whack the tunable ints into  line. */
170 	if (rt_numfibs > RT_MAXFIBS)
171 		rt_numfibs = RT_MAXFIBS;
172 	if (rt_numfibs == 0)
173 		rt_numfibs = 1;
174 	rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
175 	    NULL, NULL, UMA_ALIGN_PTR, 0);
176 	rn_init();	/* initialize all zeroes, all ones, mask table */
177 
178 	for (dom = domains; dom; dom = dom->dom_next) {
179 		if (dom->dom_rtattach)  {
180 			for  (table = 0; table < rt_numfibs; table++) {
181 				if ( (fam = dom->dom_family) == AF_INET ||
182 				    table == 0) {
183  			        	/* for now only AF_INET has > 1 table */
184 					/* XXX MRT
185 					 * rtattach will be also called
186 					 * from vfs_export.c but the
187 					 * offset will be 0
188 					 * (only for AF_INET and AF_INET6
189 					 * which don't need it anyhow)
190 					 */
191 					dom->dom_rtattach(
192 				    	    (void **)&rt_tables[table][fam],
193 				    	    dom->dom_rtoffset);
194 				} else {
195 					break;
196 				}
197 			}
198 		}
199 	}
200 }
201 
202 #ifndef _SYS_SYSPROTO_H_
203 struct setfib_args {
204 	int     fibnum;
205 };
206 #endif
207 int
208 setfib(struct thread *td, struct setfib_args *uap)
209 {
210 	if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
211 		return EINVAL;
212 	td->td_proc->p_fibnum = uap->fibnum;
213 	return (0);
214 }
215 
216 /*
217  * Packet routing routines.
218  */
219 void
220 rtalloc(struct route *ro)
221 {
222 	rtalloc_ign_fib(ro, 0UL, 0);
223 }
224 
225 void
226 rtalloc_fib(struct route *ro, u_int fibnum)
227 {
228 	rtalloc_ign_fib(ro, 0UL, fibnum);
229 }
230 
231 void
232 rtalloc_ign(struct route *ro, u_long ignore)
233 {
234 	struct rtentry *rt;
235 
236 	if ((rt = ro->ro_rt) != NULL) {
237 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
238 			return;
239 		RTFREE(rt);
240 		ro->ro_rt = NULL;
241 	}
242 	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
243 	if (ro->ro_rt)
244 		RT_UNLOCK(ro->ro_rt);
245 }
246 
247 void
248 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
249 {
250 	struct rtentry *rt;
251 
252 	if ((rt = ro->ro_rt) != NULL) {
253 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
254 			return;
255 		RTFREE(rt);
256 		ro->ro_rt = NULL;
257 	}
258 	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
259 	if (ro->ro_rt)
260 		RT_UNLOCK(ro->ro_rt);
261 }
262 
263 /*
264  * Look up the route that matches the address given
265  * Or, at least try.. Create a cloned route if needed.
266  *
267  * The returned route, if any, is locked.
268  */
269 struct rtentry *
270 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
271 {
272 	return (rtalloc1_fib(dst, report, ignflags, 0));
273 }
274 
275 struct rtentry *
276 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
277 		    u_int fibnum)
278 {
279 	struct radix_node_head *rnh;
280 	struct rtentry *rt;
281 	struct radix_node *rn;
282 	struct rtentry *newrt;
283 	struct rt_addrinfo info;
284 	u_long nflags;
285 	int err = 0, msgtype = RTM_MISS;
286 
287 	KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
288 	if (dst->sa_family != AF_INET)	/* Only INET supports > 1 fib now */
289 		fibnum = 0;
290 	rnh = rt_tables[fibnum][dst->sa_family];
291 	newrt = NULL;
292 	/*
293 	 * Look up the address in the table for that Address Family
294 	 */
295 	if (rnh == NULL) {
296 		rtstat.rts_unreach++;
297 		goto miss2;
298 	}
299 	RADIX_NODE_HEAD_LOCK(rnh);
300 	if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
301 	    (rn->rn_flags & RNF_ROOT) == 0) {
302 		/*
303 		 * If we find it and it's not the root node, then
304 		 * get a reference on the rtentry associated.
305 		 */
306 		newrt = rt = RNTORT(rn);
307 		nflags = rt->rt_flags & ~ignflags;
308 		if (report && (nflags & RTF_CLONING)) {
309 			/*
310 			 * We are apparently adding (report = 0 in delete).
311 			 * If it requires that it be cloned, do so.
312 			 * (This implies it wasn't a HOST route.)
313 			 */
314 			err = rtrequest_fib(RTM_RESOLVE, dst, NULL,
315 					      NULL, 0, &newrt, fibnum);
316 			if (err) {
317 				/*
318 				 * If the cloning didn't succeed, maybe
319 				 * what we have will do. Return that.
320 				 */
321 				newrt = rt;		/* existing route */
322 				RT_LOCK(newrt);
323 				RT_ADDREF(newrt);
324 				goto miss;
325 			}
326 			KASSERT(newrt, ("no route and no error"));
327 			RT_LOCK(newrt);
328 			if (newrt->rt_flags & RTF_XRESOLVE) {
329 				/*
330 				 * If the new route specifies it be
331 				 * externally resolved, then go do that.
332 				 */
333 				msgtype = RTM_RESOLVE;
334 				goto miss;
335 			}
336 			/* Inform listeners of the new route. */
337 			bzero(&info, sizeof(info));
338 			info.rti_info[RTAX_DST] = rt_key(newrt);
339 			info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
340 			info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
341 			if (newrt->rt_ifp != NULL) {
342 				info.rti_info[RTAX_IFP] =
343 				    newrt->rt_ifp->if_addr->ifa_addr;
344 				info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
345 			}
346 			rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
347 		} else {
348 			RT_LOCK(newrt);
349 			RT_ADDREF(newrt);
350 		}
351 		RADIX_NODE_HEAD_UNLOCK(rnh);
352 	} else {
353 		/*
354 		 * Either we hit the root or couldn't find any match,
355 		 * Which basically means
356 		 * "caint get there frm here"
357 		 */
358 		rtstat.rts_unreach++;
359 	miss:
360 		RADIX_NODE_HEAD_UNLOCK(rnh);
361 	miss2:	if (report) {
362 			/*
363 			 * If required, report the failure to the supervising
364 			 * Authorities.
365 			 * For a delete, this is not an error. (report == 0)
366 			 */
367 			bzero(&info, sizeof(info));
368 			info.rti_info[RTAX_DST] = dst;
369 			rt_missmsg(msgtype, &info, 0, err);
370 		}
371 	}
372 	if (newrt)
373 		RT_LOCK_ASSERT(newrt);
374 	return (newrt);
375 }
376 
377 /*
378  * Remove a reference count from an rtentry.
379  * If the count gets low enough, take it out of the routing table
380  */
381 void
382 rtfree(struct rtentry *rt)
383 {
384 	struct radix_node_head *rnh;
385 
386 	KASSERT(rt != NULL,("%s: NULL rt", __func__));
387 	rnh = rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
388 	KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
389 
390 	RT_LOCK_ASSERT(rt);
391 
392 	/*
393 	 * The callers should use RTFREE_LOCKED() or RTFREE(), so
394 	 * we should come here exactly with the last reference.
395 	 */
396 	RT_REMREF(rt);
397 	if (rt->rt_refcnt > 0) {
398 		printf("%s: %p has %lu refs\n", __func__, rt, rt->rt_refcnt);
399 		goto done;
400 	}
401 
402 	/*
403 	 * On last reference give the "close method" a chance
404 	 * to cleanup private state.  This also permits (for
405 	 * IPv4 and IPv6) a chance to decide if the routing table
406 	 * entry should be purged immediately or at a later time.
407 	 * When an immediate purge is to happen the close routine
408 	 * typically calls rtexpunge which clears the RTF_UP flag
409 	 * on the entry so that the code below reclaims the storage.
410 	 */
411 	if (rt->rt_refcnt == 0 && rnh->rnh_close)
412 		rnh->rnh_close((struct radix_node *)rt, rnh);
413 
414 	/*
415 	 * If we are no longer "up" (and ref == 0)
416 	 * then we can free the resources associated
417 	 * with the route.
418 	 */
419 	if ((rt->rt_flags & RTF_UP) == 0) {
420 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
421 			panic("rtfree 2");
422 		/*
423 		 * the rtentry must have been removed from the routing table
424 		 * so it is represented in rttrash.. remove that now.
425 		 */
426 		rttrash--;
427 #ifdef	DIAGNOSTIC
428 		if (rt->rt_refcnt < 0) {
429 			printf("rtfree: %p not freed (neg refs)\n", rt);
430 			goto done;
431 		}
432 #endif
433 		/*
434 		 * release references on items we hold them on..
435 		 * e.g other routes and ifaddrs.
436 		 */
437 		if (rt->rt_ifa)
438 			IFAFREE(rt->rt_ifa);
439 		rt->rt_parent = NULL;		/* NB: no refcnt on parent */
440 
441 		/*
442 		 * The key is separatly alloc'd so free it (see rt_setgate()).
443 		 * This also frees the gateway, as they are always malloc'd
444 		 * together.
445 		 */
446 		Free(rt_key(rt));
447 
448 		/*
449 		 * and the rtentry itself of course
450 		 */
451 		RT_LOCK_DESTROY(rt);
452 		uma_zfree(rtzone, rt);
453 		return;
454 	}
455 done:
456 	RT_UNLOCK(rt);
457 }
458 
459 
460 /*
461  * Force a routing table entry to the specified
462  * destination to go through the given gateway.
463  * Normally called as a result of a routing redirect
464  * message from the network layer.
465  */
466 void
467 rtredirect(struct sockaddr *dst,
468 	struct sockaddr *gateway,
469 	struct sockaddr *netmask,
470 	int flags,
471 	struct sockaddr *src)
472 {
473 	rtredirect_fib(dst, gateway, netmask, flags, src, 0);
474 }
475 
476 void
477 rtredirect_fib(struct sockaddr *dst,
478 	struct sockaddr *gateway,
479 	struct sockaddr *netmask,
480 	int flags,
481 	struct sockaddr *src,
482 	u_int fibnum)
483 {
484 	struct rtentry *rt, *rt0 = NULL;
485 	int error = 0;
486 	short *stat = NULL;
487 	struct rt_addrinfo info;
488 	struct ifaddr *ifa;
489 
490 	/* verify the gateway is directly reachable */
491 	if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
492 		error = ENETUNREACH;
493 		goto out;
494 	}
495 	rt = rtalloc1_fib(dst, 0, 0UL, fibnum);	/* NB: rt is locked */
496 	/*
497 	 * If the redirect isn't from our current router for this dst,
498 	 * it's either old or wrong.  If it redirects us to ourselves,
499 	 * we have a routing loop, perhaps as a result of an interface
500 	 * going down recently.
501 	 */
502 	if (!(flags & RTF_DONE) && rt &&
503 	     (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
504 		error = EINVAL;
505 	else if (ifa_ifwithaddr(gateway))
506 		error = EHOSTUNREACH;
507 	if (error)
508 		goto done;
509 	/*
510 	 * Create a new entry if we just got back a wildcard entry
511 	 * or the the lookup failed.  This is necessary for hosts
512 	 * which use routing redirects generated by smart gateways
513 	 * to dynamically build the routing tables.
514 	 */
515 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
516 		goto create;
517 	/*
518 	 * Don't listen to the redirect if it's
519 	 * for a route to an interface.
520 	 */
521 	if (rt->rt_flags & RTF_GATEWAY) {
522 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
523 			/*
524 			 * Changing from route to net => route to host.
525 			 * Create new route, rather than smashing route to net.
526 			 */
527 		create:
528 			rt0 = rt;
529 			rt = NULL;
530 
531 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
532 			bzero((caddr_t)&info, sizeof(info));
533 			info.rti_info[RTAX_DST] = dst;
534 			info.rti_info[RTAX_GATEWAY] = gateway;
535 			info.rti_info[RTAX_NETMASK] = netmask;
536 			info.rti_ifa = ifa;
537 			info.rti_flags = flags;
538 			error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
539 			if (rt != NULL) {
540 				RT_LOCK(rt);
541 				EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
542 				flags = rt->rt_flags;
543 			}
544 			if (rt0)
545 				RTFREE_LOCKED(rt0);
546 
547 			stat = &rtstat.rts_dynamic;
548 		} else {
549 			struct rtentry *gwrt;
550 
551 			/*
552 			 * Smash the current notion of the gateway to
553 			 * this destination.  Should check about netmask!!!
554 			 */
555 			rt->rt_flags |= RTF_MODIFIED;
556 			flags |= RTF_MODIFIED;
557 			stat = &rtstat.rts_newgateway;
558 			/*
559 			 * add the key and gateway (in one malloc'd chunk).
560 			 */
561 			rt_setgate(rt, rt_key(rt), gateway);
562 			gwrt = rtalloc1(gateway, 1, 0);
563 			EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
564 			RTFREE_LOCKED(gwrt);
565 		}
566 	} else
567 		error = EHOSTUNREACH;
568 done:
569 	if (rt)
570 		RTFREE_LOCKED(rt);
571 out:
572 	if (error)
573 		rtstat.rts_badredirect++;
574 	else if (stat != NULL)
575 		(*stat)++;
576 	bzero((caddr_t)&info, sizeof(info));
577 	info.rti_info[RTAX_DST] = dst;
578 	info.rti_info[RTAX_GATEWAY] = gateway;
579 	info.rti_info[RTAX_NETMASK] = netmask;
580 	info.rti_info[RTAX_AUTHOR] = src;
581 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
582 }
583 
584 int
585 rtioctl(u_long req, caddr_t data)
586 {
587 	return (rtioctl_fib(req, data, 0));
588 }
589 
590 /*
591  * Routing table ioctl interface.
592  */
593 int
594 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
595 {
596 
597 	/*
598 	 * If more ioctl commands are added here, make sure the proper
599 	 * super-user checks are being performed because it is possible for
600 	 * prison-root to make it this far if raw sockets have been enabled
601 	 * in jails.
602 	 */
603 #ifdef INET
604 	/* Multicast goop, grrr... */
605 	return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
606 #else /* INET */
607 	return ENXIO;
608 #endif /* INET */
609 }
610 
611 struct ifaddr *
612 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
613 {
614 	return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
615 }
616 
617 struct ifaddr *
618 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
619 				u_int fibnum)
620 {
621 	register struct ifaddr *ifa;
622 	int not_found = 0;
623 
624 	if ((flags & RTF_GATEWAY) == 0) {
625 		/*
626 		 * If we are adding a route to an interface,
627 		 * and the interface is a pt to pt link
628 		 * we should search for the destination
629 		 * as our clue to the interface.  Otherwise
630 		 * we can use the local address.
631 		 */
632 		ifa = NULL;
633 		if (flags & RTF_HOST)
634 			ifa = ifa_ifwithdstaddr(dst);
635 		if (ifa == NULL)
636 			ifa = ifa_ifwithaddr(gateway);
637 	} else {
638 		/*
639 		 * If we are adding a route to a remote net
640 		 * or host, the gateway may still be on the
641 		 * other end of a pt to pt link.
642 		 */
643 		ifa = ifa_ifwithdstaddr(gateway);
644 	}
645 	if (ifa == NULL)
646 		ifa = ifa_ifwithnet(gateway);
647 	if (ifa == NULL) {
648 		struct rtentry *rt = rtalloc1_fib(gateway, 0, 0UL, fibnum);
649 		if (rt == NULL)
650 			return (NULL);
651 		/*
652 		 * dismiss a gateway that is reachable only
653 		 * through the default router
654 		 */
655 		switch (gateway->sa_family) {
656 		case AF_INET:
657 			if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
658 				not_found = 1;
659 			break;
660 		case AF_INET6:
661 			if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
662 				not_found = 1;
663 			break;
664 		default:
665 			break;
666 		}
667 		RT_REMREF(rt);
668 		RT_UNLOCK(rt);
669 		if (not_found)
670 			return (NULL);
671 		if ((ifa = rt->rt_ifa) == NULL)
672 			return (NULL);
673 	}
674 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
675 		struct ifaddr *oifa = ifa;
676 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
677 		if (ifa == NULL)
678 			ifa = oifa;
679 	}
680 	return (ifa);
681 }
682 
683 static walktree_f_t rt_fixdelete;
684 static walktree_f_t rt_fixchange;
685 
686 struct rtfc_arg {
687 	struct rtentry *rt0;
688 	struct radix_node_head *rnh;
689 };
690 
691 /*
692  * Do appropriate manipulations of a routing tree given
693  * all the bits of info needed
694  */
695 int
696 rtrequest(int req,
697 	struct sockaddr *dst,
698 	struct sockaddr *gateway,
699 	struct sockaddr *netmask,
700 	int flags,
701 	struct rtentry **ret_nrt)
702 {
703 	return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
704 }
705 
706 int
707 rtrequest_fib(int req,
708 	struct sockaddr *dst,
709 	struct sockaddr *gateway,
710 	struct sockaddr *netmask,
711 	int flags,
712 	struct rtentry **ret_nrt,
713 	u_int fibnum)
714 {
715 	struct rt_addrinfo info;
716 
717 	if (dst->sa_len == 0)
718 		return(EINVAL);
719 
720 	bzero((caddr_t)&info, sizeof(info));
721 	info.rti_flags = flags;
722 	info.rti_info[RTAX_DST] = dst;
723 	info.rti_info[RTAX_GATEWAY] = gateway;
724 	info.rti_info[RTAX_NETMASK] = netmask;
725 	return rtrequest1_fib(req, &info, ret_nrt, fibnum);
726 }
727 
728 /*
729  * These (questionable) definitions of apparent local variables apply
730  * to the next two functions.  XXXXXX!!!
731  */
732 #define	dst	info->rti_info[RTAX_DST]
733 #define	gateway	info->rti_info[RTAX_GATEWAY]
734 #define	netmask	info->rti_info[RTAX_NETMASK]
735 #define	ifaaddr	info->rti_info[RTAX_IFA]
736 #define	ifpaddr	info->rti_info[RTAX_IFP]
737 #define	flags	info->rti_flags
738 
739 int
740 rt_getifa(struct rt_addrinfo *info)
741 {
742 	return (rt_getifa_fib(info, 0));
743 }
744 
745 int
746 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
747 {
748 	struct ifaddr *ifa;
749 	int error = 0;
750 
751 	/*
752 	 * ifp may be specified by sockaddr_dl
753 	 * when protocol address is ambiguous.
754 	 */
755 	if (info->rti_ifp == NULL && ifpaddr != NULL &&
756 	    ifpaddr->sa_family == AF_LINK &&
757 	    (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
758 		info->rti_ifp = ifa->ifa_ifp;
759 	if (info->rti_ifa == NULL && ifaaddr != NULL)
760 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
761 	if (info->rti_ifa == NULL) {
762 		struct sockaddr *sa;
763 
764 		sa = ifaaddr != NULL ? ifaaddr :
765 		    (gateway != NULL ? gateway : dst);
766 		if (sa != NULL && info->rti_ifp != NULL)
767 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
768 		else if (dst != NULL && gateway != NULL)
769 			info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
770 							fibnum);
771 		else if (sa != NULL)
772 			info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
773 							fibnum);
774 	}
775 	if ((ifa = info->rti_ifa) != NULL) {
776 		if (info->rti_ifp == NULL)
777 			info->rti_ifp = ifa->ifa_ifp;
778 	} else
779 		error = ENETUNREACH;
780 	return (error);
781 }
782 
783 /*
784  * Expunges references to a route that's about to be reclaimed.
785  * The route must be locked.
786  */
787 int
788 rtexpunge(struct rtentry *rt)
789 {
790 	struct radix_node *rn;
791 	struct radix_node_head *rnh;
792 	struct ifaddr *ifa;
793 	int error = 0;
794 
795 	RT_LOCK_ASSERT(rt);
796 #if 0
797 	/*
798 	 * We cannot assume anything about the reference count
799 	 * because protocols call us in many situations; often
800 	 * before unwinding references to the table entry.
801 	 */
802 	KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
803 #endif
804 	/*
805 	 * Find the correct routing tree to use for this Address Family
806 	 */
807 	rnh = rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
808 	if (rnh == NULL)
809 		return (EAFNOSUPPORT);
810 
811 	RADIX_NODE_HEAD_LOCK(rnh);
812 
813 	/*
814 	 * Remove the item from the tree; it should be there,
815 	 * but when callers invoke us blindly it may not (sigh).
816 	 */
817 	rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
818 	if (rn == NULL) {
819 		error = ESRCH;
820 		goto bad;
821 	}
822 	KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
823 		("unexpected flags 0x%x", rn->rn_flags));
824 	KASSERT(rt == RNTORT(rn),
825 		("lookup mismatch, rt %p rn %p", rt, rn));
826 
827 	rt->rt_flags &= ~RTF_UP;
828 
829 	/*
830 	 * Now search what's left of the subtree for any cloned
831 	 * routes which might have been formed from this node.
832 	 */
833 	if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
834 		rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
835 				       rt_fixdelete, rt);
836 
837 	/*
838 	 * Remove any external references we may have.
839 	 * This might result in another rtentry being freed if
840 	 * we held its last reference.
841 	 */
842 	if (rt->rt_gwroute) {
843 		RTFREE(rt->rt_gwroute);
844 		rt->rt_gwroute = NULL;
845 	}
846 
847 	/*
848 	 * Give the protocol a chance to keep things in sync.
849 	 */
850 	if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
851 		struct rt_addrinfo info;
852 
853 		bzero((caddr_t)&info, sizeof(info));
854 		info.rti_flags = rt->rt_flags;
855 		info.rti_info[RTAX_DST] = rt_key(rt);
856 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
857 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
858 		ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
859 	}
860 
861 	/*
862 	 * one more rtentry floating around that is not
863 	 * linked to the routing table.
864 	 */
865 	rttrash++;
866 bad:
867 	RADIX_NODE_HEAD_UNLOCK(rnh);
868 	return (error);
869 }
870 
871 int
872 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
873 {
874 	return (rtrequest1_fib(req, info, ret_nrt, 0));
875 }
876 
877 int
878 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
879 				u_int fibnum)
880 {
881 	int error = 0;
882 	register struct rtentry *rt;
883 	register struct radix_node *rn;
884 	register struct radix_node_head *rnh;
885 	struct ifaddr *ifa;
886 	struct sockaddr *ndst;
887 #define senderr(x) { error = x ; goto bad; }
888 
889 	KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
890 	if (dst->sa_family != AF_INET)	/* Only INET supports > 1 fib now */
891 		fibnum = 0;
892 	/*
893 	 * Find the correct routing tree to use for this Address Family
894 	 */
895 	rnh = rt_tables[fibnum][dst->sa_family];
896 	if (rnh == NULL)
897 		return (EAFNOSUPPORT);
898 	RADIX_NODE_HEAD_LOCK(rnh);
899 	/*
900 	 * If we are adding a host route then we don't want to put
901 	 * a netmask in the tree, nor do we want to clone it.
902 	 */
903 	if (flags & RTF_HOST) {
904 		netmask = NULL;
905 		flags &= ~RTF_CLONING;
906 	}
907 	switch (req) {
908 	case RTM_DELETE:
909 #ifdef RADIX_MPATH
910 		/*
911 		 * if we got multipath routes, we require users to specify
912 		 * a matching RTAX_GATEWAY.
913 		 */
914 		if (rn_mpath_capable(rnh)) {
915 			struct rtentry *rto = NULL;
916 
917 			rn = rnh->rnh_matchaddr(dst, rnh);
918 			if (rn == NULL)
919 				senderr(ESRCH);
920  			rto = rt = RNTORT(rn);
921 			rt = rt_mpath_matchgate(rt, gateway);
922 			if (!rt)
923 				senderr(ESRCH);
924 			/*
925 			 * this is the first entry in the chain
926 			 */
927 			if (rto == rt) {
928 				rn = rn_mpath_next((struct radix_node *)rt);
929 				/*
930 				 * there is another entry, now it's active
931 				 */
932 				if (rn) {
933 					rto = RNTORT(rn);
934 					RT_LOCK(rto);
935 					rto->rt_flags |= RTF_UP;
936 					RT_UNLOCK(rto);
937 				} else if (rt->rt_flags & RTF_GATEWAY) {
938 					/*
939 					 * For gateway routes, we need to
940 					 * make sure that we we are deleting
941 					 * the correct gateway.
942 					 * rt_mpath_matchgate() does not
943 					 * check the case when there is only
944 					 * one route in the chain.
945 					 */
946 					if (gateway &&
947 					    (rt->rt_gateway->sa_len != gateway->sa_len ||
948 					    memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
949 						senderr(ESRCH);
950 				}
951 				/*
952 				 * use the normal delete code to remove
953 				 * the first entry
954 				 */
955 				goto normal_rtdel;
956 			}
957 			/*
958 			 * if the entry is 2nd and on up
959 			 */
960 			if (!rt_mpath_deldup(rto, rt))
961 				panic ("rtrequest1: rt_mpath_deldup");
962 			RT_LOCK(rt);
963 			RT_ADDREF(rt);
964 			rt->rt_flags &= ~RTF_UP;
965 			goto deldone;  /* done with the RTM_DELETE command */
966 		}
967 
968 normal_rtdel:
969 #endif
970 		/*
971 		 * Remove the item from the tree and return it.
972 		 * Complain if it is not there and do no more processing.
973 		 */
974 		rn = rnh->rnh_deladdr(dst, netmask, rnh);
975 		if (rn == NULL)
976 			senderr(ESRCH);
977 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
978 			panic ("rtrequest delete");
979 		rt = RNTORT(rn);
980 		RT_LOCK(rt);
981 		RT_ADDREF(rt);
982 		rt->rt_flags &= ~RTF_UP;
983 
984 		/*
985 		 * Now search what's left of the subtree for any cloned
986 		 * routes which might have been formed from this node.
987 		 */
988 		if ((rt->rt_flags & RTF_CLONING) &&
989 		    rt_mask(rt)) {
990 			rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
991 					       rt_fixdelete, rt);
992 		}
993 
994 		/*
995 		 * Remove any external references we may have.
996 		 * This might result in another rtentry being freed if
997 		 * we held its last reference.
998 		 */
999 		if (rt->rt_gwroute) {
1000 			RTFREE(rt->rt_gwroute);
1001 			rt->rt_gwroute = NULL;
1002 		}
1003 
1004 		/*
1005 		 * give the protocol a chance to keep things in sync.
1006 		 */
1007 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1008 			ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1009 
1010 #ifdef RADIX_MPATH
1011 deldone:
1012 #endif
1013 		/*
1014 		 * One more rtentry floating around that is not
1015 		 * linked to the routing table. rttrash will be decremented
1016 		 * when RTFREE(rt) is eventually called.
1017 		 */
1018 		rttrash++;
1019 
1020 		/*
1021 		 * If the caller wants it, then it can have it,
1022 		 * but it's up to it to free the rtentry as we won't be
1023 		 * doing it.
1024 		 */
1025 		if (ret_nrt) {
1026 			*ret_nrt = rt;
1027 			RT_UNLOCK(rt);
1028 		} else
1029 			RTFREE_LOCKED(rt);
1030 		break;
1031 
1032 	case RTM_RESOLVE:
1033 		if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
1034 			senderr(EINVAL);
1035 		ifa = rt->rt_ifa;
1036 		/* XXX locking? */
1037 		flags = rt->rt_flags &
1038 		    ~(RTF_CLONING | RTF_STATIC);
1039 		flags |= RTF_WASCLONED;
1040 		gateway = rt->rt_gateway;
1041 		if ((netmask = rt->rt_genmask) == NULL)
1042 			flags |= RTF_HOST;
1043 		goto makeroute;
1044 
1045 	case RTM_ADD:
1046 		if ((flags & RTF_GATEWAY) && !gateway)
1047 			senderr(EINVAL);
1048 		if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1049 		    (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1050 			senderr(EINVAL);
1051 
1052 		if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum)))
1053 			senderr(error);
1054 		ifa = info->rti_ifa;
1055 
1056 	makeroute:
1057 		rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
1058 		if (rt == NULL)
1059 			senderr(ENOBUFS);
1060 		RT_LOCK_INIT(rt);
1061 		rt->rt_flags = RTF_UP | flags;
1062 		rt->rt_fibnum = fibnum;
1063 		/*
1064 		 * Add the gateway. Possibly re-malloc-ing the storage for it
1065 		 * also add the rt_gwroute if possible.
1066 		 */
1067 		RT_LOCK(rt);
1068 		if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1069 			RT_LOCK_DESTROY(rt);
1070 			uma_zfree(rtzone, rt);
1071 			senderr(error);
1072 		}
1073 
1074 		/*
1075 		 * point to the (possibly newly malloc'd) dest address.
1076 		 */
1077 		ndst = (struct sockaddr *)rt_key(rt);
1078 
1079 		/*
1080 		 * make sure it contains the value we want (masked if needed).
1081 		 */
1082 		if (netmask) {
1083 			rt_maskedcopy(dst, ndst, netmask);
1084 		} else
1085 			bcopy(dst, ndst, dst->sa_len);
1086 
1087 		/*
1088 		 * Note that we now have a reference to the ifa.
1089 		 * This moved from below so that rnh->rnh_addaddr() can
1090 		 * examine the ifa and  ifa->ifa_ifp if it so desires.
1091 		 */
1092 		IFAREF(ifa);
1093 		rt->rt_ifa = ifa;
1094 		rt->rt_ifp = ifa->ifa_ifp;
1095 
1096 #ifdef RADIX_MPATH
1097 		/* do not permit exactly the same dst/mask/gw pair */
1098 		if (rn_mpath_capable(rnh) &&
1099 			rt_mpath_conflict(rnh, rt, netmask)) {
1100 			if (rt->rt_gwroute)
1101 				RTFREE(rt->rt_gwroute);
1102 			if (rt->rt_ifa) {
1103 				IFAFREE(rt->rt_ifa);
1104 			}
1105 			Free(rt_key(rt));
1106 			RT_LOCK_DESTROY(rt);
1107 			uma_zfree(rtzone, rt);
1108 			senderr(EEXIST);
1109 		}
1110 #endif
1111 
1112 		/* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1113 		rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1114 		if (rn == NULL) {
1115 			struct rtentry *rt2;
1116 			/*
1117 			 * Uh-oh, we already have one of these in the tree.
1118 			 * We do a special hack: if the route that's already
1119 			 * there was generated by the cloning mechanism
1120 			 * then we just blow it away and retry the insertion
1121 			 * of the new one.
1122 			 */
1123 			rt2 = rtalloc1_fib(dst, 0, 0, fibnum);
1124 			if (rt2 && rt2->rt_parent) {
1125 				rtexpunge(rt2);
1126 				RT_UNLOCK(rt2);
1127 				rn = rnh->rnh_addaddr(ndst, netmask,
1128 						      rnh, rt->rt_nodes);
1129 			} else if (rt2) {
1130 				/* undo the extra ref we got */
1131 				RTFREE_LOCKED(rt2);
1132 			}
1133 		}
1134 
1135 		/*
1136 		 * If it still failed to go into the tree,
1137 		 * then un-make it (this should be a function)
1138 		 */
1139 		if (rn == NULL) {
1140 			if (rt->rt_gwroute)
1141 				RTFREE(rt->rt_gwroute);
1142 			if (rt->rt_ifa)
1143 				IFAFREE(rt->rt_ifa);
1144 			Free(rt_key(rt));
1145 			RT_LOCK_DESTROY(rt);
1146 			uma_zfree(rtzone, rt);
1147 			senderr(EEXIST);
1148 		}
1149 
1150 		rt->rt_parent = NULL;
1151 
1152 		/*
1153 		 * If we got here from RESOLVE, then we are cloning
1154 		 * so clone the rest, and note that we
1155 		 * are a clone (and increment the parent's references)
1156 		 */
1157 		if (req == RTM_RESOLVE) {
1158 			KASSERT(ret_nrt && *ret_nrt,
1159 				("no route to clone from"));
1160 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
1161 			rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
1162 			if ((*ret_nrt)->rt_flags & RTF_CLONING) {
1163 				/*
1164 				 * NB: We do not bump the refcnt on the parent
1165 				 * entry under the assumption that it will
1166 				 * remain so long as we do.  This is
1167 				 * important when deleting the parent route
1168 				 * as this operation requires traversing
1169 				 * the tree to delete all clones and futzing
1170 				 * with refcnts requires us to double-lock
1171 				 * parent through this back reference.
1172 				 */
1173 				rt->rt_parent = *ret_nrt;
1174 			}
1175 		}
1176 
1177 		/*
1178 		 * If this protocol has something to add to this then
1179 		 * allow it to do that as well.
1180 		 */
1181 		if (ifa->ifa_rtrequest)
1182 			ifa->ifa_rtrequest(req, rt, info);
1183 
1184 		/*
1185 		 * We repeat the same procedure from rt_setgate() here because
1186 		 * it doesn't fire when we call it there because the node
1187 		 * hasn't been added to the tree yet.
1188 		 */
1189 		if (req == RTM_ADD &&
1190 		    !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1191 			struct rtfc_arg arg;
1192 			arg.rnh = rnh;
1193 			arg.rt0 = rt;
1194 			rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1195 					       rt_fixchange, &arg);
1196 		}
1197 
1198 		/*
1199 		 * actually return a resultant rtentry and
1200 		 * give the caller a single reference.
1201 		 */
1202 		if (ret_nrt) {
1203 			*ret_nrt = rt;
1204 			RT_ADDREF(rt);
1205 		}
1206 		RT_UNLOCK(rt);
1207 		break;
1208 	default:
1209 		error = EOPNOTSUPP;
1210 	}
1211 bad:
1212 	RADIX_NODE_HEAD_UNLOCK(rnh);
1213 	return (error);
1214 #undef senderr
1215 }
1216 
1217 #undef dst
1218 #undef gateway
1219 #undef netmask
1220 #undef ifaaddr
1221 #undef ifpaddr
1222 #undef flags
1223 
1224 /*
1225  * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1226  * (i.e., the routes related to it by the operation of cloning).  This
1227  * routine is iterated over all potential former-child-routes by way of
1228  * rnh->rnh_walktree_from() above, and those that actually are children of
1229  * the late parent (passed in as VP here) are themselves deleted.
1230  */
1231 static int
1232 rt_fixdelete(struct radix_node *rn, void *vp)
1233 {
1234 	struct rtentry *rt = RNTORT(rn);
1235 	struct rtentry *rt0 = vp;
1236 
1237 	if (rt->rt_parent == rt0 &&
1238 	    !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
1239 		return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1240 				 rt->rt_flags, NULL, rt->rt_fibnum);
1241 	}
1242 	return 0;
1243 }
1244 
1245 /*
1246  * This routine is called from rt_setgate() to do the analogous thing for
1247  * adds and changes.  There is the added complication in this case of a
1248  * middle insert; i.e., insertion of a new network route between an older
1249  * network route and (cloned) host routes.  For this reason, a simple check
1250  * of rt->rt_parent is insufficient; each candidate route must be tested
1251  * against the (mask, value) of the new route (passed as before in vp)
1252  * to see if the new route matches it.
1253  *
1254  * XXX - it may be possible to do fixdelete() for changes and reserve this
1255  * routine just for adds.  I'm not sure why I thought it was necessary to do
1256  * changes this way.
1257  */
1258 
1259 static int
1260 rt_fixchange(struct radix_node *rn, void *vp)
1261 {
1262 	struct rtentry *rt = RNTORT(rn);
1263 	struct rtfc_arg *ap = vp;
1264 	struct rtentry *rt0 = ap->rt0;
1265 	struct radix_node_head *rnh = ap->rnh;
1266 	u_char *xk1, *xm1, *xk2, *xmp;
1267 	int i, len, mlen;
1268 
1269 	/* make sure we have a parent, and route is not pinned or cloning */
1270 	if (!rt->rt_parent ||
1271 	    (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
1272 		return 0;
1273 
1274 	if (rt->rt_parent == rt0)	/* parent match */
1275 		goto delete_rt;
1276 	/*
1277 	 * There probably is a function somewhere which does this...
1278 	 * if not, there should be.
1279 	 */
1280 	len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1281 
1282 	xk1 = (u_char *)rt_key(rt0);
1283 	xm1 = (u_char *)rt_mask(rt0);
1284 	xk2 = (u_char *)rt_key(rt);
1285 
1286 	/* avoid applying a less specific route */
1287 	xmp = (u_char *)rt_mask(rt->rt_parent);
1288 	mlen = rt_key(rt->rt_parent)->sa_len;
1289 	if (mlen > rt_key(rt0)->sa_len)		/* less specific route */
1290 		return 0;
1291 	for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
1292 		if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
1293 			return 0;	/* less specific route */
1294 
1295 	for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
1296 		if ((xk2[i] & xm1[i]) != xk1[i])
1297 			return 0;	/* no match */
1298 
1299 	/*
1300 	 * OK, this node is a clone, and matches the node currently being
1301 	 * changed/added under the node's mask.  So, get rid of it.
1302 	 */
1303 delete_rt:
1304 	return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL,
1305 			 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum);
1306 }
1307 
1308 int
1309 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1310 {
1311 	/* XXX dst may be overwritten, can we move this to below */
1312 	struct radix_node_head *rnh = rt_tables[rt->rt_fibnum][dst->sa_family];
1313 	int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1314 
1315 again:
1316 	RT_LOCK_ASSERT(rt);
1317 
1318 	/*
1319 	 * A host route with the destination equal to the gateway
1320 	 * will interfere with keeping LLINFO in the routing
1321 	 * table, so disallow it.
1322 	 */
1323 	if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
1324 					(RTF_HOST|RTF_GATEWAY)) &&
1325 	    dst->sa_len == gate->sa_len &&
1326 	    bcmp(dst, gate, dst->sa_len) == 0) {
1327 		/*
1328 		 * The route might already exist if this is an RTM_CHANGE
1329 		 * or a routing redirect, so try to delete it.
1330 		 */
1331 		if (rt_key(rt))
1332 			rtexpunge(rt);
1333 		return EADDRNOTAVAIL;
1334 	}
1335 
1336 	/*
1337 	 * Cloning loop avoidance in case of bad configuration.
1338 	 */
1339 	if (rt->rt_flags & RTF_GATEWAY) {
1340 		struct rtentry *gwrt;
1341 
1342 		RT_UNLOCK(rt);		/* XXX workaround LOR */
1343 		gwrt = rtalloc1_fib(gate, 1, 0, rt->rt_fibnum);
1344 		if (gwrt == rt) {
1345 			RT_REMREF(rt);
1346 			return (EADDRINUSE); /* failure */
1347 		}
1348 		/*
1349 		 * Try to reacquire the lock on rt, and if it fails,
1350 		 * clean state and restart from scratch.
1351 		 */
1352 		if (!RT_TRYLOCK(rt)) {
1353 			RTFREE_LOCKED(gwrt);
1354 			RT_LOCK(rt);
1355 			goto again;
1356 		}
1357 		/*
1358 		 * If there is already a gwroute, then drop it. If we
1359 		 * are asked to replace route with itself, then do
1360 		 * not leak its refcounter.
1361 		 */
1362 		if (rt->rt_gwroute != NULL) {
1363 			if (rt->rt_gwroute == gwrt) {
1364 				RT_REMREF(rt->rt_gwroute);
1365 			} else
1366 				RTFREE(rt->rt_gwroute);
1367 		}
1368 
1369 		if ((rt->rt_gwroute = gwrt) != NULL)
1370 			RT_UNLOCK(rt->rt_gwroute);
1371 	}
1372 
1373 	/*
1374 	 * Prepare to store the gateway in rt->rt_gateway.
1375 	 * Both dst and gateway are stored one after the other in the same
1376 	 * malloc'd chunk. If we have room, we can reuse the old buffer,
1377 	 * rt_gateway already points to the right place.
1378 	 * Otherwise, malloc a new block and update the 'dst' address.
1379 	 */
1380 	if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1381 		caddr_t new;
1382 
1383 		R_Malloc(new, caddr_t, dlen + glen);
1384 		if (new == NULL)
1385 			return ENOBUFS;
1386 		/*
1387 		 * XXX note, we copy from *dst and not *rt_key(rt) because
1388 		 * rt_setgate() can be called to initialize a newly
1389 		 * allocated route entry, in which case rt_key(rt) == NULL
1390 		 * (and also rt->rt_gateway == NULL).
1391 		 * Free()/free() handle a NULL argument just fine.
1392 		 */
1393 		bcopy(dst, new, dlen);
1394 		Free(rt_key(rt));	/* free old block, if any */
1395 		rt_key(rt) = (struct sockaddr *)new;
1396 		rt->rt_gateway = (struct sockaddr *)(new + dlen);
1397 	}
1398 
1399 	/*
1400 	 * Copy the new gateway value into the memory chunk.
1401 	 */
1402 	bcopy(gate, rt->rt_gateway, glen);
1403 
1404 	/*
1405 	 * This isn't going to do anything useful for host routes, so
1406 	 * don't bother.  Also make sure we have a reasonable mask
1407 	 * (we don't yet have one during adds).
1408 	 */
1409 	if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1410 		struct rtfc_arg arg;
1411 
1412 		arg.rnh = rnh;
1413 		arg.rt0 = rt;
1414 		RT_UNLOCK(rt);		/* XXX workaround LOR */
1415 		RADIX_NODE_HEAD_LOCK(rnh);
1416 		RT_LOCK(rt);
1417 		rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1418 				       rt_fixchange, &arg);
1419 		RADIX_NODE_HEAD_UNLOCK(rnh);
1420 	}
1421 
1422 	return 0;
1423 }
1424 
1425 static void
1426 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1427 {
1428 	register u_char *cp1 = (u_char *)src;
1429 	register u_char *cp2 = (u_char *)dst;
1430 	register u_char *cp3 = (u_char *)netmask;
1431 	u_char *cplim = cp2 + *cp3;
1432 	u_char *cplim2 = cp2 + *cp1;
1433 
1434 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1435 	cp3 += 2;
1436 	if (cplim > cplim2)
1437 		cplim = cplim2;
1438 	while (cp2 < cplim)
1439 		*cp2++ = *cp1++ & *cp3++;
1440 	if (cp2 < cplim2)
1441 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1442 }
1443 
1444 /*
1445  * Set up a routing table entry, normally
1446  * for an interface.
1447  */
1448 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1449 static inline  int
1450 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1451 {
1452 	struct sockaddr *dst;
1453 	struct sockaddr *netmask;
1454 	struct rtentry *rt = NULL;
1455 	struct rt_addrinfo info;
1456 	int error = 0;
1457 	int startfib, endfib;
1458 	char tempbuf[_SOCKADDR_TMPSIZE];
1459 	int didwork = 0;
1460 	int a_failure = 0;
1461 
1462 	if (flags & RTF_HOST) {
1463 		dst = ifa->ifa_dstaddr;
1464 		netmask = NULL;
1465 	} else {
1466 		dst = ifa->ifa_addr;
1467 		netmask = ifa->ifa_netmask;
1468 	}
1469 	if ( dst->sa_family != AF_INET)
1470 		fibnum = 0;
1471 	if (fibnum == -1) {
1472 		if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1473 			startfib = endfib = curthread->td_proc->p_fibnum;
1474 		} else {
1475 			startfib = 0;
1476 			endfib = rt_numfibs - 1;
1477 		}
1478 	} else {
1479 		KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1480 		startfib = fibnum;
1481 		endfib = fibnum;
1482 	}
1483 	if (dst->sa_len == 0)
1484 		return(EINVAL);
1485 
1486 	/*
1487 	 * If it's a delete, check that if it exists,
1488 	 * it's on the correct interface or we might scrub
1489 	 * a route to another ifa which would
1490 	 * be confusing at best and possibly worse.
1491 	 */
1492 	if (cmd == RTM_DELETE) {
1493 		/*
1494 		 * It's a delete, so it should already exist..
1495 		 * If it's a net, mask off the host bits
1496 		 * (Assuming we have a mask)
1497 		 * XXX this is kinda inet specific..
1498 		 */
1499 		if (netmask != NULL) {
1500 			rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1501 			dst = (struct sockaddr *)tempbuf;
1502 		}
1503 	}
1504 	/*
1505 	 * Now go through all the requested tables (fibs) and do the
1506 	 * requested action. Realistically, this will either be fib 0
1507 	 * for protocols that don't do multiple tables or all the
1508 	 * tables for those that do. XXX For this version only AF_INET.
1509 	 * When that changes code should be refactored to protocol
1510 	 * independent parts and protocol dependent parts.
1511 	 */
1512 	for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1513 		if (cmd == RTM_DELETE) {
1514 			struct radix_node_head *rnh;
1515 			struct radix_node *rn;
1516 			/*
1517 			 * Look up an rtentry that is in the routing tree and
1518 			 * contains the correct info.
1519 			 */
1520 			if ((rnh = rt_tables[fibnum][dst->sa_family]) == NULL)
1521 				/* this table doesn't exist but others might */
1522 				continue;
1523 			RADIX_NODE_HEAD_LOCK(rnh);
1524 #ifdef RADIX_MPATH
1525 			if (rn_mpath_capable(rnh)) {
1526 
1527 				rn = rnh->rnh_matchaddr(dst, rnh);
1528 				if (rn == NULL)
1529 					error = ESRCH;
1530 				else {
1531 					rt = RNTORT(rn);
1532 					/*
1533 					 * for interface route the
1534 					 * rt->rt_gateway is sockaddr_intf
1535 					 * for cloning ARP entries, so
1536 					 * rt_mpath_matchgate must use the
1537 					 * interface address
1538 					 */
1539 					rt = rt_mpath_matchgate(rt,
1540 					    ifa->ifa_addr);
1541 					if (!rt)
1542 						error = ESRCH;
1543 				}
1544 			}
1545 			else
1546 #endif
1547 			rn = rnh->rnh_lookup(dst, netmask, rnh);
1548 			error = (rn == NULL ||
1549 			    (rn->rn_flags & RNF_ROOT) ||
1550 			    RNTORT(rn)->rt_ifa != ifa ||
1551 			    !sa_equal((struct sockaddr *)rn->rn_key, dst));
1552 			RADIX_NODE_HEAD_UNLOCK(rnh);
1553 			if (error) {
1554 				/* this is only an error if bad on ALL tables */
1555 				continue;
1556 			}
1557 		}
1558 		/*
1559 		 * Do the actual request
1560 		 */
1561 		bzero((caddr_t)&info, sizeof(info));
1562 		info.rti_ifa = ifa;
1563 		info.rti_flags = flags | ifa->ifa_flags;
1564 		info.rti_info[RTAX_DST] = dst;
1565 		info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1566 		info.rti_info[RTAX_NETMASK] = netmask;
1567 		error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1568 		if (error == 0 && rt != NULL) {
1569 			/*
1570 			 * notify any listening routing agents of the change
1571 			 */
1572 			RT_LOCK(rt);
1573 #ifdef RADIX_MPATH
1574 			/*
1575 			 * in case address alias finds the first address
1576 			 * e.g. ifconfig bge0 192.103.54.246/24
1577 			 * e.g. ifconfig bge0 192.103.54.247/24
1578 			 * the address set in the route is 192.103.54.246
1579 			 * so we need to replace it with 192.103.54.247
1580 			 */
1581 			if (memcmp(rt->rt_ifa->ifa_addr,
1582 			    ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1583 				IFAFREE(rt->rt_ifa);
1584 				IFAREF(ifa);
1585 				rt->rt_ifp = ifa->ifa_ifp;
1586 				rt->rt_ifa = ifa;
1587 			}
1588 #endif
1589 			rt_newaddrmsg(cmd, ifa, error, rt);
1590 			if (cmd == RTM_DELETE) {
1591 				/*
1592 				 * If we are deleting, and we found an entry,
1593 				 * then it's been removed from the tree..
1594 				 * now throw it away.
1595 				 */
1596 				RTFREE_LOCKED(rt);
1597 			} else {
1598 				if (cmd == RTM_ADD) {
1599 					/*
1600 					 * We just wanted to add it..
1601 					 * we don't actually need a reference.
1602 					 */
1603 					RT_REMREF(rt);
1604 				}
1605 				RT_UNLOCK(rt);
1606 			}
1607 			didwork = 1;
1608 		}
1609 		if (error)
1610 			a_failure = error;
1611 	}
1612 	if (cmd == RTM_DELETE) {
1613 		if (didwork) {
1614 			error = 0;
1615 		} else {
1616 			/* we only give an error if it wasn't in any table */
1617 			error = ((flags & RTF_HOST) ?
1618 			    EHOSTUNREACH : ENETUNREACH);
1619 		}
1620 	} else {
1621 		if (a_failure) {
1622 			/* return an error if any of them failed */
1623 			error = a_failure;
1624 		}
1625 	}
1626 	return (error);
1627 }
1628 
1629 /* special one for inet internal use. may not use. */
1630 int
1631 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1632 {
1633 	return (rtinit1(ifa, cmd, flags, -1));
1634 }
1635 
1636 /*
1637  * Set up a routing table entry, normally
1638  * for an interface.
1639  */
1640 int
1641 rtinit(struct ifaddr *ifa, int cmd, int flags)
1642 {
1643 	struct sockaddr *dst;
1644 	int fib = 0;
1645 
1646 	if (flags & RTF_HOST) {
1647 		dst = ifa->ifa_dstaddr;
1648 	} else {
1649 		dst = ifa->ifa_addr;
1650 	}
1651 
1652 	if (dst->sa_family == AF_INET)
1653 		fib = -1;
1654 	return (rtinit1(ifa, cmd, flags, fib));
1655 }
1656 
1657 /*
1658  * rt_check() is invoked on each layer 2 output path, prior to
1659  * encapsulating outbound packets.
1660  *
1661  * The function is mostly used to find a routing entry for the gateway,
1662  * which in some protocol families could also point to the link-level
1663  * address for the gateway itself (the side effect of revalidating the
1664  * route to the destination is rather pointless at this stage, we did it
1665  * already a moment before in the pr_output() routine to locate the ifp
1666  * and gateway to use).
1667  *
1668  * When we remove the layer-3 to layer-2 mapping tables from the
1669  * routing table, this function can be removed.
1670  *
1671  * === On input ===
1672  *   *dst is the address of the NEXT HOP (which coincides with the
1673  *	final destination if directly reachable);
1674  *   *lrt0 points to the cached route to the final destination;
1675  *   *lrt is not meaningful;
1676  *    fibnum is the index to the correct network fib for this packet
1677  *
1678  * === Operation ===
1679  * If the route is marked down try to find a new route.  If the route
1680  * to the gateway is gone, try to setup a new route.  Otherwise,
1681  * if the route is marked for packets to be rejected, enforce that.
1682  *
1683  * === On return ===
1684  *   *dst is unchanged;
1685  *   *lrt0 points to the (possibly new) route to the final destination
1686  *   *lrt points to the route to the next hop
1687  *
1688  * Their values are meaningful ONLY if no error is returned.
1689  */
1690 int
1691 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
1692 {
1693 	return (rt_check_fib(lrt, lrt0, dst, 0));
1694 }
1695 
1696 int
1697 rt_check_fib(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst,
1698 		u_int fibnum)
1699 {
1700 	struct rtentry *rt;
1701 	struct rtentry *rt0;
1702 	int error;
1703 
1704 	KASSERT(*lrt0 != NULL, ("rt_check"));
1705 	rt = rt0 = *lrt0;
1706 
1707 	/* NB: the locking here is tortuous... */
1708 	RT_LOCK(rt);
1709 	if ((rt->rt_flags & RTF_UP) == 0) {
1710 		RT_UNLOCK(rt);
1711 		rt = rtalloc1_fib(dst, 1, 0UL, fibnum);
1712 		if (rt != NULL) {
1713 			RT_REMREF(rt);
1714 			/* XXX what about if change? */
1715 		} else
1716 			return (EHOSTUNREACH);
1717 		rt0 = rt;
1718 	}
1719 	/* XXX BSD/OS checks dst->sa_family != AF_NS */
1720 	if (rt->rt_flags & RTF_GATEWAY) {
1721 		if (rt->rt_gwroute == NULL)
1722 			goto lookup;
1723 		rt = rt->rt_gwroute;
1724 		RT_LOCK(rt);		/* NB: gwroute */
1725 		if ((rt->rt_flags & RTF_UP) == 0) {
1726 			RTFREE_LOCKED(rt);	/* unlock gwroute */
1727 			rt = rt0;
1728 			rt0->rt_gwroute = NULL;
1729 		lookup:
1730 			RT_UNLOCK(rt0);
1731 /* XXX MRT link level looked up in table 0 */
1732 			rt = rtalloc1_fib(rt->rt_gateway, 1, 0UL, 0);
1733 			if (rt == rt0) {
1734 				RT_REMREF(rt0);
1735 				RT_UNLOCK(rt0);
1736 				return (ENETUNREACH);
1737 			}
1738 			RT_LOCK(rt0);
1739 			if (rt0->rt_gwroute != NULL)
1740 				RTFREE(rt0->rt_gwroute);
1741 			rt0->rt_gwroute = rt;
1742 			if (rt == NULL) {
1743 				RT_UNLOCK(rt0);
1744 				return (EHOSTUNREACH);
1745 			}
1746 		}
1747 		RT_UNLOCK(rt0);
1748 	}
1749 	/* XXX why are we inspecting rmx_expire? */
1750 	error = (rt->rt_flags & RTF_REJECT) &&
1751 		(rt->rt_rmx.rmx_expire == 0 ||
1752 			time_uptime < rt->rt_rmx.rmx_expire);
1753 	if (error) {
1754 		RT_UNLOCK(rt);
1755 		return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1756 	}
1757 
1758 	*lrt = rt;
1759 	*lrt0 = rt0;
1760 	return (0);
1761 }
1762 
1763 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1764 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
1765