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