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