xref: /NextBSD/sys/net/route.c (revision 0a828b7c75eed213014f367fa3b27fe2654b00c7)
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_sctp.h"
41 #include "opt_mrouting.h"
42 #include "opt_mpath.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.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 #if defined(INET) || defined(INET6)
90 #ifdef SCTP
91 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
92 #endif /* SCTP */
93 #endif
94 
95 
96 /* This is read-only.. */
97 u_int rt_numfibs = RT_NUMFIBS;
98 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RDTUN, &rt_numfibs, 0, "");
99 
100 u_int inpcb_rt_cache_enable = 0;
101 SYSCTL_UINT(_net, OID_AUTO, conn_rt_cache, CTLFLAG_RW|CTLFLAG_TUN, &inpcb_rt_cache_enable, 0, "");
102 TUNABLE_INT("net.conn_rt_cache", &inpcb_rt_cache_enable);
103 
104 
105 /*
106  * By default add routes to all fibs for new interfaces.
107  * Once this is set to 0 then only allocate routes on interface
108  * changes for the FIB of the caller when adding a new set of addresses
109  * to an interface.  XXX this is a shotgun aproach to a problem that needs
110  * a more fine grained solution.. that will come.
111  * XXX also has the problems getting the FIB from curthread which will not
112  * always work given the fib can be overridden and prefixes can be added
113  * from the network stack context.
114  */
115 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 1;
116 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
117     &VNET_NAME(rt_add_addr_allfibs), 0, "");
118 
119 VNET_DEFINE(struct rtstat, rtstat);
120 #define	V_rtstat	VNET(rtstat)
121 
122 VNET_DEFINE(struct radix_node_head *, rt_tables);
123 #define	V_rt_tables	VNET(rt_tables)
124 
125 VNET_DEFINE(int, rttrash);		/* routes not in table but not freed */
126 #define	V_rttrash	VNET(rttrash)
127 
128 
129 /*
130  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
131  * The operation can be done safely (in this code) because a
132  * 'struct rtentry' starts with two 'struct radix_node''s, the first
133  * one representing leaf nodes in the routing tree, which is
134  * what the code in radix.c passes us as a 'struct radix_node'.
135  *
136  * But because there are a lot of assumptions in this conversion,
137  * do not cast explicitly, but always use the macro below.
138  */
139 #define RNTORT(p)	((struct rtentry *)(p))
140 
141 static VNET_DEFINE(uma_zone_t, rtzone);		/* Routing table UMA zone. */
142 #define	V_rtzone	VNET(rtzone)
143 
144 static int rtrequest1_fib_change(struct radix_node_head *, struct rt_addrinfo *,
145     struct rtentry **, u_int);
146 static void rt_setmetrics(const struct rt_addrinfo *, struct rtentry *);
147 static int rt_ifdelroute(const struct rtentry *rt, void *arg);
148 static struct rtentry *rt_unlinkrte(struct radix_node_head *rnh,
149     struct rt_addrinfo *info, int *perror);
150 static void rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info);
151 #ifdef RADIX_MPATH
152 static struct radix_node *rt_mpath_unlink(struct radix_node_head *rnh,
153     struct rt_addrinfo *info, struct rtentry *rto, int *perror);
154 #endif
155 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info,
156     int flags);
157 
158 struct if_mtuinfo
159 {
160 	struct ifnet	*ifp;
161 	int		mtu;
162 };
163 
164 static int	if_updatemtu_cb(struct radix_node *, void *);
165 
166 /*
167  * handler for net.my_fibnum
168  */
169 static int
sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)170 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
171 {
172         int fibnum;
173         int error;
174 
175         fibnum = curthread->td_proc->p_fibnum;
176         error = sysctl_handle_int(oidp, &fibnum, 0, req);
177         return (error);
178 }
179 
180 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
181             NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
182 
183 static __inline struct radix_node_head **
rt_tables_get_rnh_ptr(int table,int fam)184 rt_tables_get_rnh_ptr(int table, int fam)
185 {
186 	struct radix_node_head **rnh;
187 
188 	KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
189 	    __func__));
190 	KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
191 	    __func__));
192 
193 	/* rnh is [fib=0][af=0]. */
194 	rnh = (struct radix_node_head **)V_rt_tables;
195 	/* Get the offset to the requested table and fam. */
196 	rnh += table * (AF_MAX+1) + fam;
197 
198 	return (rnh);
199 }
200 
201 struct radix_node_head *
rt_tables_get_rnh(int table,int fam)202 rt_tables_get_rnh(int table, int fam)
203 {
204 
205 	return (*rt_tables_get_rnh_ptr(table, fam));
206 }
207 
208 /*
209  * route initialization must occur before ip6_init2(), which happenas at
210  * SI_ORDER_MIDDLE.
211  */
212 static void
route_init(void)213 route_init(void)
214 {
215 
216 	/* whack the tunable ints into  line. */
217 	if (rt_numfibs > RT_MAXFIBS)
218 		rt_numfibs = RT_MAXFIBS;
219 	if (rt_numfibs == 0)
220 		rt_numfibs = 1;
221 }
222 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
223 
224 static int
rtentry_zinit(void * mem,int size,int how)225 rtentry_zinit(void *mem, int size, int how)
226 {
227 	struct rtentry *rt = mem;
228 
229 	rt->rt_pksent = counter_u64_alloc(how);
230 	if (rt->rt_pksent == NULL)
231 		return (ENOMEM);
232 
233 	RT_LOCK_INIT(rt);
234 
235 	return (0);
236 }
237 
238 static void
rtentry_zfini(void * mem,int size)239 rtentry_zfini(void *mem, int size)
240 {
241 	struct rtentry *rt = mem;
242 
243 	RT_LOCK_DESTROY(rt);
244 	counter_u64_free(rt->rt_pksent);
245 }
246 
247 static int
rtentry_ctor(void * mem,int size,void * arg,int how)248 rtentry_ctor(void *mem, int size, void *arg, int how)
249 {
250 	struct rtentry *rt = mem;
251 
252 	bzero(rt, offsetof(struct rtentry, rt_endzero));
253 	counter_u64_zero(rt->rt_pksent);
254 	rt->rt_chain = NULL;
255 
256 	return (0);
257 }
258 
259 static void
rtentry_dtor(void * mem,int size,void * arg)260 rtentry_dtor(void *mem, int size, void *arg)
261 {
262 	struct rtentry *rt = mem;
263 
264 	RT_UNLOCK_COND(rt);
265 }
266 
267 static void
vnet_route_init(const void * unused __unused)268 vnet_route_init(const void *unused __unused)
269 {
270 	struct domain *dom;
271 	struct radix_node_head **rnh;
272 	int table;
273 	int fam;
274 
275 	V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
276 	    sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
277 
278 	V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
279 	    rtentry_ctor, rtentry_dtor,
280 	    rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0);
281 	for (dom = domains; dom; dom = dom->dom_next) {
282 		if (dom->dom_rtattach == NULL)
283 			continue;
284 
285 		for  (table = 0; table < rt_numfibs; table++) {
286 			fam = dom->dom_family;
287 			if (table != 0 && fam != AF_INET6 && fam != AF_INET)
288 				break;
289 
290 			rnh = rt_tables_get_rnh_ptr(table, fam);
291 			if (rnh == NULL)
292 				panic("%s: rnh NULL", __func__);
293 			dom->dom_rtattach((void **)rnh, 0);
294 		}
295 	}
296 }
297 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
298     vnet_route_init, 0);
299 
300 #ifdef VIMAGE
301 static void
vnet_route_uninit(const void * unused __unused)302 vnet_route_uninit(const void *unused __unused)
303 {
304 	int table;
305 	int fam;
306 	struct domain *dom;
307 	struct radix_node_head **rnh;
308 
309 	for (dom = domains; dom; dom = dom->dom_next) {
310 		if (dom->dom_rtdetach == NULL)
311 			continue;
312 
313 		for (table = 0; table < rt_numfibs; table++) {
314 			fam = dom->dom_family;
315 
316 			if (table != 0 && fam != AF_INET6 && fam != AF_INET)
317 				break;
318 
319 			rnh = rt_tables_get_rnh_ptr(table, fam);
320 			if (rnh == NULL)
321 				panic("%s: rnh NULL", __func__);
322 			dom->dom_rtdetach((void **)rnh, 0);
323 		}
324 	}
325 
326 	free(V_rt_tables, M_RTABLE);
327 	uma_zdestroy(V_rtzone);
328 }
329 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
330     vnet_route_uninit, 0);
331 #endif
332 
333 #ifndef _SYS_SYSPROTO_H_
334 struct setfib_args {
335 	int     fibnum;
336 };
337 #endif
338 int
sys_setfib(struct thread * td,struct setfib_args * uap)339 sys_setfib(struct thread *td, struct setfib_args *uap)
340 {
341 	if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
342 		return EINVAL;
343 	td->td_proc->p_fibnum = uap->fibnum;
344 	return (0);
345 }
346 
347 /*
348  * Packet routing routines.
349  */
350 void
rtalloc(struct route * ro)351 rtalloc(struct route *ro)
352 {
353 
354 	rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB);
355 }
356 
357 void
rtalloc_fib(struct route * ro,u_int fibnum)358 rtalloc_fib(struct route *ro, u_int fibnum)
359 {
360 	rtalloc_ign_fib(ro, 0UL, fibnum);
361 }
362 
363 void
rtalloc_ign(struct route * ro,u_long ignore)364 rtalloc_ign(struct route *ro, u_long ignore)
365 {
366 	struct rtentry *rt;
367 
368 	if ((rt = ro->ro_rt) != NULL) {
369 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
370 			return;
371 		RTFREE(rt);
372 		ro->ro_rt = NULL;
373 	}
374 	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB);
375 	if (ro->ro_rt)
376 		RT_UNLOCK(ro->ro_rt);
377 }
378 
379 void
rtalloc_ign_fib(struct route * ro,u_long ignore,u_int fibnum)380 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
381 {
382 	struct rtentry *rt;
383 
384 	if ((rt = ro->ro_rt) != NULL) {
385 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
386 			return;
387 		RTFREE(rt);
388 		ro->ro_rt = NULL;
389 	}
390 	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
391 	if (ro->ro_rt)
392 		RT_UNLOCK(ro->ro_rt);
393 }
394 
395 /*
396  * Look up the route that matches the address given
397  * Or, at least try.. Create a cloned route if needed.
398  *
399  * The returned route, if any, is locked.
400  */
401 struct rtentry *
rtalloc1(struct sockaddr * dst,int report,u_long ignflags)402 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
403 {
404 
405 	return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
406 }
407 
408 struct rtentry *
rtalloc1_fib(struct sockaddr * dst,int report,u_long ignflags,u_int fibnum)409 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
410 		    u_int fibnum)
411 {
412 	struct radix_node_head *rnh;
413 	struct radix_node *rn;
414 	struct rtentry *newrt;
415 	struct rt_addrinfo info;
416 	int err = 0, msgtype = RTM_MISS;
417 	int needlock;
418 
419 	KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
420 	rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
421 	newrt = NULL;
422 	if (rnh == NULL)
423 		goto miss;
424 
425 	/*
426 	 * Look up the address in the table for that Address Family
427 	 */
428 	needlock = !(ignflags & RTF_RNH_LOCKED);
429 	if (needlock)
430 		RADIX_NODE_HEAD_RLOCK(rnh);
431 #ifdef INVARIANTS
432 	else
433 		RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
434 #endif
435 	rn = rnh->rnh_matchaddr(dst, rnh);
436 	if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
437 		newrt = RNTORT(rn);
438 		RT_LOCK(newrt);
439 		RT_ADDREF(newrt);
440 		if (needlock)
441 			RADIX_NODE_HEAD_RUNLOCK(rnh);
442 		goto done;
443 
444 	} else if (needlock)
445 		RADIX_NODE_HEAD_RUNLOCK(rnh);
446 
447 	/*
448 	 * Either we hit the root or couldn't find any match,
449 	 * Which basically means
450 	 * "caint get there frm here"
451 	 */
452 miss:
453 	V_rtstat.rts_unreach++;
454 
455 	if (report) {
456 		/*
457 		 * If required, report the failure to the supervising
458 		 * Authorities.
459 		 * For a delete, this is not an error. (report == 0)
460 		 */
461 		bzero(&info, sizeof(info));
462 		info.rti_info[RTAX_DST] = dst;
463 		rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
464 	}
465 done:
466 	if (newrt)
467 		RT_LOCK_ASSERT(newrt);
468 	return (newrt);
469 }
470 
471 /*
472  * Remove a reference count from an rtentry.
473  * If the count gets low enough, take it out of the routing table
474  */
475 void
rtfree(struct rtentry * rt)476 rtfree(struct rtentry *rt)
477 {
478 	struct radix_node_head *rnh;
479 
480 	KASSERT(rt != NULL,("%s: NULL rt", __func__));
481 	rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
482 	KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
483 
484 	RT_LOCK_ASSERT(rt);
485 
486 	/*
487 	 * The callers should use RTFREE_LOCKED() or RTFREE(), so
488 	 * we should come here exactly with the last reference.
489 	 */
490 	RT_REMREF(rt);
491 	if (rt->rt_refcnt > 0) {
492 		log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
493 		goto done;
494 	}
495 
496 	/*
497 	 * On last reference give the "close method" a chance
498 	 * to cleanup private state.  This also permits (for
499 	 * IPv4 and IPv6) a chance to decide if the routing table
500 	 * entry should be purged immediately or at a later time.
501 	 * When an immediate purge is to happen the close routine
502 	 * typically calls rtexpunge which clears the RTF_UP flag
503 	 * on the entry so that the code below reclaims the storage.
504 	 */
505 	if (rt->rt_refcnt == 0 && rnh->rnh_close)
506 		rnh->rnh_close((struct radix_node *)rt, rnh);
507 
508 	/*
509 	 * If we are no longer "up" (and ref == 0)
510 	 * then we can free the resources associated
511 	 * with the route.
512 	 */
513 	if ((rt->rt_flags & RTF_UP) == 0) {
514 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
515 			panic("rtfree 2");
516 		/*
517 		 * the rtentry must have been removed from the routing table
518 		 * so it is represented in rttrash.. remove that now.
519 		 */
520 		V_rttrash--;
521 #ifdef	DIAGNOSTIC
522 		if (rt->rt_refcnt < 0) {
523 			printf("rtfree: %p not freed (neg refs)\n", rt);
524 			goto done;
525 		}
526 #endif
527 		/*
528 		 * release references on items we hold them on..
529 		 * e.g other routes and ifaddrs.
530 		 */
531 		if (rt->rt_ifa)
532 			ifa_free(rt->rt_ifa);
533 		/*
534 		 * The key is separatly alloc'd so free it (see rt_setgate()).
535 		 * This also frees the gateway, as they are always malloc'd
536 		 * together.
537 		 */
538 		R_Free(rt_key(rt));
539 
540 		/*
541 		 * and the rtentry itself of course
542 		 */
543 		uma_zfree(V_rtzone, rt);
544 		return;
545 	}
546 done:
547 	RT_UNLOCK(rt);
548 }
549 
550 
551 /*
552  * Force a routing table entry to the specified
553  * destination to go through the given gateway.
554  * Normally called as a result of a routing redirect
555  * message from the network layer.
556  */
557 void
rtredirect(struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct sockaddr * src)558 rtredirect(struct sockaddr *dst,
559 	struct sockaddr *gateway,
560 	struct sockaddr *netmask,
561 	int flags,
562 	struct sockaddr *src)
563 {
564 
565 	rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB);
566 }
567 
568 void
rtredirect_fib(struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct sockaddr * src,u_int fibnum)569 rtredirect_fib(struct sockaddr *dst,
570 	struct sockaddr *gateway,
571 	struct sockaddr *netmask,
572 	int flags,
573 	struct sockaddr *src,
574 	u_int fibnum)
575 {
576 	struct rtentry *rt;
577 	int error = 0;
578 	short *stat = NULL;
579 	struct rt_addrinfo info;
580 	struct ifaddr *ifa;
581 	struct radix_node_head *rnh;
582 
583 	ifa = NULL;
584 	rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
585 	if (rnh == NULL) {
586 		error = EAFNOSUPPORT;
587 		goto out;
588 	}
589 
590 	/* verify the gateway is directly reachable */
591 	if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) {
592 		error = ENETUNREACH;
593 		goto out;
594 	}
595 	rt = rtalloc1_fib(dst, 0, 0UL, fibnum);	/* NB: rt is locked */
596 	/*
597 	 * If the redirect isn't from our current router for this dst,
598 	 * it's either old or wrong.  If it redirects us to ourselves,
599 	 * we have a routing loop, perhaps as a result of an interface
600 	 * going down recently.
601 	 */
602 	if (!(flags & RTF_DONE) && rt) {
603 		if (!sa_equal(src, rt->rt_gateway)) {
604 			error = EINVAL;
605 			goto done;
606 		}
607 		if (rt->rt_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK) {
608 			error = EINVAL;
609 			goto done;
610 		}
611 	}
612 	if ((flags & RTF_GATEWAY) && ifa_ifwithaddr_check(gateway)) {
613 		error = EHOSTUNREACH;
614 		goto done;
615 	}
616 	/*
617 	 * Create a new entry if we just got back a wildcard entry
618 	 * or the lookup failed.  This is necessary for hosts
619 	 * which use routing redirects generated by smart gateways
620 	 * to dynamically build the routing tables.
621 	 */
622 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
623 		goto create;
624 	/*
625 	 * Don't listen to the redirect if it's
626 	 * for a route to an interface.
627 	 */
628 	if (rt->rt_flags & RTF_GATEWAY) {
629 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
630 			/*
631 			 * Changing from route to net => route to host.
632 			 * Create new route, rather than smashing route to net.
633 			 */
634 		create:
635 			RTFREE(rt);
636 			rt = NULL;
637 
638 			flags |= RTF_DYNAMIC;
639 			bzero((caddr_t)&info, sizeof(info));
640 			info.rti_info[RTAX_DST] = dst;
641 			info.rti_info[RTAX_GATEWAY] = gateway;
642 			info.rti_info[RTAX_NETMASK] = netmask;
643 			info.rti_ifa = ifa;
644 			info.rti_flags = flags;
645 			error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
646 			if (rt != NULL) {
647 				RT_LOCK(rt);
648 				flags = rt->rt_flags;
649 			}
650 
651 			stat = &V_rtstat.rts_dynamic;
652 		} else {
653 
654 			/*
655 			 * Smash the current notion of the gateway to
656 			 * this destination.  Should check about netmask!!!
657 			 */
658 			if ((flags & RTF_GATEWAY) == 0)
659 				rt->rt_flags &= ~RTF_GATEWAY;
660 			rt->rt_flags |= RTF_MODIFIED;
661 			flags |= RTF_MODIFIED;
662 			stat = &V_rtstat.rts_newgateway;
663 			/*
664 			 * add the key and gateway (in one malloc'd chunk).
665 			 */
666 			RT_UNLOCK(rt);
667 			RADIX_NODE_HEAD_LOCK(rnh);
668 			RT_LOCK(rt);
669 			rt_setgate(rt, rt_key(rt), gateway);
670 			RADIX_NODE_HEAD_UNLOCK(rnh);
671 		}
672 	} else
673 		error = EHOSTUNREACH;
674 done:
675 	if (rt)
676 		RTFREE_LOCKED(rt);
677 out:
678 	if (error)
679 		V_rtstat.rts_badredirect++;
680 	else if (stat != NULL)
681 		(*stat)++;
682 	bzero((caddr_t)&info, sizeof(info));
683 	info.rti_info[RTAX_DST] = dst;
684 	info.rti_info[RTAX_GATEWAY] = gateway;
685 	info.rti_info[RTAX_NETMASK] = netmask;
686 	info.rti_info[RTAX_AUTHOR] = src;
687 	rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
688 	if (ifa != NULL)
689 		ifa_free(ifa);
690 }
691 
692 int
rtioctl(u_long req,caddr_t data)693 rtioctl(u_long req, caddr_t data)
694 {
695 
696 	return (rtioctl_fib(req, data, RT_DEFAULT_FIB));
697 }
698 
699 /*
700  * Routing table ioctl interface.
701  */
702 int
rtioctl_fib(u_long req,caddr_t data,u_int fibnum)703 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
704 {
705 
706 	/*
707 	 * If more ioctl commands are added here, make sure the proper
708 	 * super-user checks are being performed because it is possible for
709 	 * prison-root to make it this far if raw sockets have been enabled
710 	 * in jails.
711 	 */
712 #ifdef INET
713 	/* Multicast goop, grrr... */
714 	return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
715 #else /* INET */
716 	return ENXIO;
717 #endif /* INET */
718 }
719 
720 struct ifaddr *
ifa_ifwithroute(int flags,const struct sockaddr * dst,struct sockaddr * gateway,u_int fibnum)721 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway,
722 				u_int fibnum)
723 {
724 	struct ifaddr *ifa;
725 	int not_found = 0;
726 
727 	if ((flags & RTF_GATEWAY) == 0) {
728 		/*
729 		 * If we are adding a route to an interface,
730 		 * and the interface is a pt to pt link
731 		 * we should search for the destination
732 		 * as our clue to the interface.  Otherwise
733 		 * we can use the local address.
734 		 */
735 		ifa = NULL;
736 		if (flags & RTF_HOST)
737 			ifa = ifa_ifwithdstaddr(dst, fibnum);
738 		if (ifa == NULL)
739 			ifa = ifa_ifwithaddr(gateway);
740 	} else {
741 		/*
742 		 * If we are adding a route to a remote net
743 		 * or host, the gateway may still be on the
744 		 * other end of a pt to pt link.
745 		 */
746 		ifa = ifa_ifwithdstaddr(gateway, fibnum);
747 	}
748 	if (ifa == NULL)
749 		ifa = ifa_ifwithnet(gateway, 0, fibnum);
750 	if (ifa == NULL) {
751 		struct rtentry *rt = rtalloc1_fib(gateway, 0, 0, fibnum);
752 		if (rt == NULL)
753 			return (NULL);
754 		/*
755 		 * dismiss a gateway that is reachable only
756 		 * through the default router
757 		 */
758 		switch (gateway->sa_family) {
759 		case AF_INET:
760 			if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
761 				not_found = 1;
762 			break;
763 		case AF_INET6:
764 			if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
765 				not_found = 1;
766 			break;
767 		default:
768 			break;
769 		}
770 		if (!not_found && rt->rt_ifa != NULL) {
771 			ifa = rt->rt_ifa;
772 			ifa_ref(ifa);
773 		}
774 		RT_REMREF(rt);
775 		RT_UNLOCK(rt);
776 		if (not_found || ifa == NULL)
777 			return (NULL);
778 	}
779 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
780 		struct ifaddr *oifa = ifa;
781 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
782 		if (ifa == NULL)
783 			ifa = oifa;
784 		else
785 			ifa_free(oifa);
786 	}
787 	return (ifa);
788 }
789 
790 /*
791  * Do appropriate manipulations of a routing tree given
792  * all the bits of info needed
793  */
794 int
rtrequest(int req,struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct rtentry ** ret_nrt)795 rtrequest(int req,
796 	struct sockaddr *dst,
797 	struct sockaddr *gateway,
798 	struct sockaddr *netmask,
799 	int flags,
800 	struct rtentry **ret_nrt)
801 {
802 
803 	return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt,
804 	    RT_DEFAULT_FIB));
805 }
806 
807 int
rtrequest_fib(int req,struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct rtentry ** ret_nrt,u_int fibnum)808 rtrequest_fib(int req,
809 	struct sockaddr *dst,
810 	struct sockaddr *gateway,
811 	struct sockaddr *netmask,
812 	int flags,
813 	struct rtentry **ret_nrt,
814 	u_int fibnum)
815 {
816 	struct rt_addrinfo info;
817 
818 	if (dst->sa_len == 0)
819 		return(EINVAL);
820 
821 	bzero((caddr_t)&info, sizeof(info));
822 	info.rti_flags = flags;
823 	info.rti_info[RTAX_DST] = dst;
824 	info.rti_info[RTAX_GATEWAY] = gateway;
825 	info.rti_info[RTAX_NETMASK] = netmask;
826 	return rtrequest1_fib(req, &info, ret_nrt, fibnum);
827 }
828 
829 
830 /*
831  * Copy most of @rt data into @info.
832  *
833  * If @flags contains NHR_COPY, copies dst,netmask and gw to the
834  * pointers specified by @info structure. Assume such pointers
835  * are zeroed sockaddr-like structures with sa_len field initialized
836  * to reflect size of the provided buffer. if no NHR_COPY is specified,
837  * point dst,netmask and gw @info fields to appropriate @rt values.
838  *
839  * if @flags contains NHR_REF, do refcouting on rt_ifp.
840  *
841  * Returns 0 on success.
842  */
843 int
rt_exportinfo(struct rtentry * rt,struct rt_addrinfo * info,int flags)844 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags)
845 {
846 	struct rt_metrics *rmx;
847 	struct sockaddr *src, *dst;
848 	int sa_len;
849 
850 	if (flags & NHR_COPY) {
851 		/* Copy destination if dst is non-zero */
852 		src = rt_key(rt);
853 		dst = info->rti_info[RTAX_DST];
854 		sa_len = src->sa_len;
855 		if (dst != NULL) {
856 			if (src->sa_len > dst->sa_len)
857 				return (ENOMEM);
858 			memcpy(dst, src, src->sa_len);
859 			info->rti_addrs |= RTA_DST;
860 		}
861 
862 		/* Copy mask if set && dst is non-zero */
863 		src = rt_mask(rt);
864 		dst = info->rti_info[RTAX_NETMASK];
865 		if (src != NULL && dst != NULL) {
866 
867 			/*
868 			 * Radix stores different value in sa_len,
869 			 * assume rt_mask() to have the same length
870 			 * as rt_key()
871 			 */
872 			if (sa_len > dst->sa_len)
873 				return (ENOMEM);
874 			memcpy(dst, src, src->sa_len);
875 			info->rti_addrs |= RTA_NETMASK;
876 		}
877 
878 		/* Copy gateway is set && dst is non-zero */
879 		src = rt->rt_gateway;
880 		dst = info->rti_info[RTAX_GATEWAY];
881 		if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){
882 			if (src->sa_len > dst->sa_len)
883 				return (ENOMEM);
884 			memcpy(dst, src, src->sa_len);
885 			info->rti_addrs |= RTA_GATEWAY;
886 		}
887 	} else {
888 		info->rti_info[RTAX_DST] = rt_key(rt);
889 		info->rti_addrs |= RTA_DST;
890 		if (rt_mask(rt) != NULL) {
891 			info->rti_info[RTAX_NETMASK] = rt_mask(rt);
892 			info->rti_addrs |= RTA_NETMASK;
893 		}
894 		if (rt->rt_flags & RTF_GATEWAY) {
895 			info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
896 			info->rti_addrs |= RTA_GATEWAY;
897 		}
898 	}
899 
900 	rmx = info->rti_rmx;
901 	if (rmx != NULL) {
902 		info->rti_mflags |= RTV_MTU;
903 		rmx->rmx_mtu = rt->rt_mtu;
904 	}
905 
906 	info->rti_flags = rt->rt_flags;
907 	info->rti_ifp = rt->rt_ifp;
908 	info->rti_ifa = rt->rt_ifa;
909 
910 	if (flags & NHR_REF) {
911 		/* Do 'traditional' refcouting */
912 		if_ref(info->rti_ifp);
913 	}
914 
915 	return (0);
916 }
917 
918 /*
919  * Lookups up route entry for @dst in RIB database for fib @fibnum.
920  * Exports entry data to @info using rt_exportinfo().
921  *
922  * if @flags contains NHR_REF, refcouting is performed on rt_ifp.
923  *   All references can be released later by calling rib_free_info()
924  *
925  * Returns 0 on success.
926  * Returns ENOENT for lookup failure, ENOMEM for export failure.
927  */
928 int
rib_lookup_info(uint32_t fibnum,const struct sockaddr * dst,uint32_t flags,uint32_t flowid,struct rt_addrinfo * info)929 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
930     uint32_t flowid, struct rt_addrinfo *info)
931 {
932 	struct radix_node_head *rh;
933 	struct radix_node *rn;
934 	struct rtentry *rt;
935 	int error;
936 
937 	KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum"));
938 	rh = rt_tables_get_rnh(fibnum, dst->sa_family);
939 	if (rh == NULL)
940 		return (ENOENT);
941 
942 	RADIX_NODE_HEAD_RLOCK(rh);
943 	rn = rh->rnh_matchaddr(__DECONST(void *, dst), rh);
944 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
945 		rt = RNTORT(rn);
946 		/* Ensure route & ifp is UP */
947 		if (RT_LINK_IS_UP(rt->rt_ifp)) {
948 			flags = (flags & NHR_REF) | NHR_COPY;
949 			error = rt_exportinfo(rt, info, flags);
950 			RADIX_NODE_HEAD_RUNLOCK(rh);
951 
952 			return (error);
953 		}
954 	}
955 	RADIX_NODE_HEAD_RUNLOCK(rh);
956 
957 	return (ENOENT);
958 }
959 
960 /*
961  * Releases all references acquired by rib_lookup_info() when
962  * called with NHR_REF flags.
963  */
964 void
rib_free_info(struct rt_addrinfo * info)965 rib_free_info(struct rt_addrinfo *info)
966 {
967 
968 	if_rele(info->rti_ifp);
969 }
970 
971 /*
972  * Iterates over all existing fibs in system calling
973  *  @setwa_f function prior to traversing each fib.
974  *  Calls @wa_f function for each element in current fib.
975  * If af is not AF_UNSPEC, iterates over fibs in particular
976  * address family.
977  */
978 void
rt_foreach_fib_walk(int af,rt_setwarg_t * setwa_f,rt_walktree_f_t * wa_f,void * arg)979 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f,
980     void *arg)
981 {
982 	struct radix_node_head *rnh;
983 	uint32_t fibnum;
984 	int i;
985 
986 	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
987 		/* Do we want some specific family? */
988 		if (af != AF_UNSPEC) {
989 			rnh = rt_tables_get_rnh(fibnum, af);
990 			if (rnh == NULL)
991 				continue;
992 			if (setwa_f != NULL)
993 				setwa_f(rnh, fibnum, af, arg);
994 
995 			RADIX_NODE_HEAD_LOCK(rnh);
996 			rnh->rnh_walktree(rnh, (walktree_f_t *)wa_f, arg);
997 			RADIX_NODE_HEAD_UNLOCK(rnh);
998 			continue;
999 		}
1000 
1001 		for (i = 1; i <= AF_MAX; i++) {
1002 			rnh = rt_tables_get_rnh(fibnum, i);
1003 			if (rnh == NULL)
1004 				continue;
1005 			if (setwa_f != NULL)
1006 				setwa_f(rnh, fibnum, i, arg);
1007 
1008 			RADIX_NODE_HEAD_LOCK(rnh);
1009 			rnh->rnh_walktree(rnh, (walktree_f_t *)wa_f, arg);
1010 			RADIX_NODE_HEAD_UNLOCK(rnh);
1011 		}
1012 	}
1013 }
1014 
1015 struct rt_delinfo
1016 {
1017 	struct rt_addrinfo info;
1018 	struct radix_node_head *rnh;
1019 	struct rtentry *head;
1020 };
1021 
1022 /*
1023  * Conditionally unlinks @rn from radix tree based
1024  * on info data passed in @arg.
1025  */
1026 static int
rt_checkdelroute(struct radix_node * rn,void * arg)1027 rt_checkdelroute(struct radix_node *rn, void *arg)
1028 {
1029 	struct rt_delinfo *di;
1030 	struct rt_addrinfo *info;
1031 	struct rtentry *rt;
1032 	int error;
1033 
1034 	di = (struct rt_delinfo *)arg;
1035 	rt = (struct rtentry *)rn;
1036 	info = &di->info;
1037 	error = 0;
1038 
1039 	info->rti_info[RTAX_DST] = rt_key(rt);
1040 	info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1041 	info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1042 
1043 	rt = rt_unlinkrte(di->rnh, info, &error);
1044 	if (rt == NULL) {
1045 		/* Either not allowed or not matched. Skip entry */
1046 		return (0);
1047 	}
1048 
1049 	/* Entry was unlinked. Add to the list and return */
1050 	rt->rt_chain = di->head;
1051 	di->head = rt;
1052 
1053 	return (0);
1054 }
1055 
1056 /*
1057  * Iterates over all existing fibs in system.
1058  * Deletes each element for which @filter_f function returned
1059  * non-zero value.
1060  * If @af is not AF_UNSPEC, iterates over fibs in particular
1061  * address family.
1062  */
1063 void
rt_foreach_fib_walk_del(int af,rt_filter_f_t * filter_f,void * arg)1064 rt_foreach_fib_walk_del(int af, rt_filter_f_t *filter_f, void *arg)
1065 {
1066 	struct radix_node_head *rnh;
1067 	struct rt_delinfo di;
1068 	struct rtentry *rt;
1069 	uint32_t fibnum;
1070 	int i, start, end;
1071 
1072 	bzero(&di, sizeof(di));
1073 	di.info.rti_filter = filter_f;
1074 	di.info.rti_filterdata = arg;
1075 
1076 	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1077 		/* Do we want some specific family? */
1078 		if (af != AF_UNSPEC) {
1079 			start = af;
1080 			end = af;
1081 		} else {
1082 			start = 1;
1083 			end = AF_MAX;
1084 		}
1085 
1086 		for (i = start; i <= end; i++) {
1087 			rnh = rt_tables_get_rnh(fibnum, i);
1088 			if (rnh == NULL)
1089 				continue;
1090 			di.rnh = rnh;
1091 
1092 			RADIX_NODE_HEAD_LOCK(rnh);
1093 			rnh->rnh_walktree(rnh, rt_checkdelroute, &di);
1094 			RADIX_NODE_HEAD_UNLOCK(rnh);
1095 
1096 			if (di.head == NULL)
1097 				continue;
1098 
1099 			/* We might have something to reclaim */
1100 			while (di.head != NULL) {
1101 				rt = di.head;
1102 				di.head = rt->rt_chain;
1103 				rt->rt_chain = NULL;
1104 
1105 				/* TODO std rt -> rt_addrinfo export */
1106 				di.info.rti_info[RTAX_DST] = rt_key(rt);
1107 				di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1108 
1109 				rt_notifydelete(rt, &di.info);
1110 				RTFREE_LOCKED(rt);
1111 			}
1112 
1113 		}
1114 	}
1115 }
1116 
1117 /*
1118  * Delete Routes for a Network Interface
1119  *
1120  * Called for each routing entry via the rnh->rnh_walktree() call above
1121  * to delete all route entries referencing a detaching network interface.
1122  *
1123  * Arguments:
1124  *	rt	pointer to rtentry
1125  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
1126  *
1127  * Returns:
1128  *	0	successful
1129  *	errno	failed - reason indicated
1130  */
1131 static int
rt_ifdelroute(const struct rtentry * rt,void * arg)1132 rt_ifdelroute(const struct rtentry *rt, void *arg)
1133 {
1134 	struct ifnet	*ifp = arg;
1135 
1136 	if (rt->rt_ifp != ifp)
1137 		return (0);
1138 
1139 	/*
1140 	 * Protect (sorta) against walktree recursion problems
1141 	 * with cloned routes
1142 	 */
1143 	if ((rt->rt_flags & RTF_UP) == 0)
1144 		return (0);
1145 
1146 	return (1);
1147 }
1148 
1149 /*
1150  * Delete all remaining routes using this interface
1151  * Unfortuneatly the only way to do this is to slog through
1152  * the entire routing table looking for routes which point
1153  * to this interface...oh well...
1154  */
1155 void
rt_flushifroutes(struct ifnet * ifp)1156 rt_flushifroutes(struct ifnet *ifp)
1157 {
1158 
1159 	rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
1160 }
1161 
1162 /*
1163  * Conditionally unlinks rtentry matching data inside @info from @rnh.
1164  * Returns unlinked, locked and referenced @rtentry on success,
1165  * Returns NULL and sets @perror to:
1166  * ESRCH - if prefix was not found,
1167  * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
1168  * ENOENT - if supplied filter function returned 0 (not matched).
1169  */
1170 static struct rtentry *
rt_unlinkrte(struct radix_node_head * rnh,struct rt_addrinfo * info,int * perror)1171 rt_unlinkrte(struct radix_node_head *rnh, struct rt_addrinfo *info, int *perror)
1172 {
1173 	struct sockaddr *dst, *netmask;
1174 	struct rtentry *rt;
1175 	struct radix_node *rn;
1176 
1177 	dst = info->rti_info[RTAX_DST];
1178 	netmask = info->rti_info[RTAX_NETMASK];
1179 
1180 	rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, rnh);
1181 	if (rt == NULL) {
1182 		*perror = ESRCH;
1183 		return (NULL);
1184 	}
1185 
1186 	if ((info->rti_flags & RTF_PINNED) == 0) {
1187 		/* Check if target route can be deleted */
1188 		if (rt->rt_flags & RTF_PINNED) {
1189 			*perror = EADDRINUSE;
1190 			return (NULL);
1191 		}
1192 	}
1193 
1194 	if (info->rti_filter != NULL) {
1195 		if (info->rti_filter(rt, info->rti_filterdata) == 0) {
1196 			/* Not matched */
1197 			*perror = ENOENT;
1198 			return (NULL);
1199 		}
1200 
1201 		/*
1202 		 * Filter function requested rte deletion.
1203 		 * Ease the caller work by filling in remaining info
1204 		 * from that particular entry.
1205 		 */
1206 		info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1207 	}
1208 
1209 	/*
1210 	 * Remove the item from the tree and return it.
1211 	 * Complain if it is not there and do no more processing.
1212 	 */
1213 	*perror = ESRCH;
1214 #ifdef RADIX_MPATH
1215 	if (rn_mpath_capable(rnh))
1216 		rn = rt_mpath_unlink(rnh, info, rt, perror);
1217 	else
1218 #endif
1219 	rn = rnh->rnh_deladdr(dst, netmask, rnh);
1220 	atomic_add_int(&rnh->rnh_gen, 1);
1221 	if (rn == NULL)
1222 		return (NULL);
1223 
1224 	if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1225 		panic ("rtrequest delete");
1226 
1227 	rt = RNTORT(rn);
1228 	RT_LOCK(rt);
1229 	RT_ADDREF(rt);
1230 	rt->rt_flags &= ~RTF_UP;
1231 
1232 	*perror = 0;
1233 
1234 	return (rt);
1235 }
1236 
1237 static void
rt_notifydelete(struct rtentry * rt,struct rt_addrinfo * info)1238 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info)
1239 {
1240 	struct ifaddr *ifa;
1241 
1242 	/*
1243 	 * give the protocol a chance to keep things in sync.
1244 	 */
1245 	ifa = rt->rt_ifa;
1246 	if (ifa != NULL && ifa->ifa_rtrequest != NULL)
1247 		ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1248 
1249 	/*
1250 	 * One more rtentry floating around that is not
1251 	 * linked to the routing table. rttrash will be decremented
1252 	 * when RTFREE(rt) is eventually called.
1253 	 */
1254 	V_rttrash++;
1255 }
1256 
1257 
1258 /*
1259  * These (questionable) definitions of apparent local variables apply
1260  * to the next two functions.  XXXXXX!!!
1261  */
1262 #define	dst	info->rti_info[RTAX_DST]
1263 #define	gateway	info->rti_info[RTAX_GATEWAY]
1264 #define	netmask	info->rti_info[RTAX_NETMASK]
1265 #define	ifaaddr	info->rti_info[RTAX_IFA]
1266 #define	ifpaddr	info->rti_info[RTAX_IFP]
1267 #define	flags	info->rti_flags
1268 
1269 /*
1270  * Look up rt_addrinfo for a specific fib.  Note that if rti_ifa is defined,
1271  * it will be referenced so the caller must free it.
1272  */
1273 int
rt_getifa_fib(struct rt_addrinfo * info,u_int fibnum)1274 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
1275 {
1276 	struct ifaddr *ifa;
1277 	int error = 0;
1278 
1279 	/*
1280 	 * ifp may be specified by sockaddr_dl
1281 	 * when protocol address is ambiguous.
1282 	 */
1283 	if (info->rti_ifp == NULL && ifpaddr != NULL &&
1284 	    ifpaddr->sa_family == AF_LINK &&
1285 	    (ifa = ifa_ifwithnet(ifpaddr, 0, fibnum)) != NULL) {
1286 		info->rti_ifp = ifa->ifa_ifp;
1287 		ifa_free(ifa);
1288 	}
1289 	if (info->rti_ifa == NULL && ifaaddr != NULL)
1290 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
1291 	if (info->rti_ifa == NULL) {
1292 		struct sockaddr *sa;
1293 
1294 		sa = ifaaddr != NULL ? ifaaddr :
1295 		    (gateway != NULL ? gateway : dst);
1296 		if (sa != NULL && info->rti_ifp != NULL)
1297 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
1298 		else if (dst != NULL && gateway != NULL)
1299 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
1300 							fibnum);
1301 		else if (sa != NULL)
1302 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
1303 							fibnum);
1304 	}
1305 	if ((ifa = info->rti_ifa) != NULL) {
1306 		if (info->rti_ifp == NULL)
1307 			info->rti_ifp = ifa->ifa_ifp;
1308 	} else
1309 		error = ENETUNREACH;
1310 	return (error);
1311 }
1312 
1313 static int
if_updatemtu_cb(struct radix_node * rn,void * arg)1314 if_updatemtu_cb(struct radix_node *rn, void *arg)
1315 {
1316 	struct rtentry *rt;
1317 	struct if_mtuinfo *ifmtu;
1318 
1319 	rt = (struct rtentry *)rn;
1320 	ifmtu = (struct if_mtuinfo *)arg;
1321 
1322 	if (rt->rt_ifp != ifmtu->ifp)
1323 		return (0);
1324 
1325 	if (rt->rt_mtu >= ifmtu->mtu) {
1326 		/* We have to decrease mtu regardless of flags */
1327 		rt->rt_mtu = ifmtu->mtu;
1328 		return (0);
1329 	}
1330 
1331 	/*
1332 	 * New MTU is bigger. Check if are allowed to alter it
1333 	 */
1334 	if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) {
1335 
1336 		/*
1337 		 * Skip routes with user-supplied MTU and
1338 		 * non-interface routes
1339 		 */
1340 		return (0);
1341 	}
1342 
1343 	/* We are safe to update route MTU */
1344 	rt->rt_mtu = ifmtu->mtu;
1345 
1346 	return (0);
1347 }
1348 
1349 void
rt_updatemtu(struct ifnet * ifp)1350 rt_updatemtu(struct ifnet *ifp)
1351 {
1352 	struct if_mtuinfo ifmtu;
1353 	struct radix_node_head *rnh;
1354 	int i, j;
1355 
1356 	ifmtu.ifp = ifp;
1357 
1358 	/*
1359 	 * Try to update rt_mtu for all routes using this interface
1360 	 * Unfortunately the only way to do this is to traverse all
1361 	 * routing tables in all fibs/domains.
1362 	 */
1363 	for (i = 1; i <= AF_MAX; i++) {
1364 		ifmtu.mtu = if_getmtu_family(ifp, i);
1365 		for (j = 0; j < rt_numfibs; j++) {
1366 			rnh = rt_tables_get_rnh(j, i);
1367 			if (rnh == NULL)
1368 				continue;
1369 			RADIX_NODE_HEAD_LOCK(rnh);
1370 			rnh->rnh_walktree(rnh, if_updatemtu_cb, &ifmtu);
1371 			RADIX_NODE_HEAD_UNLOCK(rnh);
1372 		}
1373 	}
1374 }
1375 
1376 
1377 #if 0
1378 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
1379 int rt_print(char *buf, int buflen, struct rtentry *rt);
1380 
1381 int
1382 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
1383 {
1384 	void *paddr = NULL;
1385 
1386 	switch (s->sa_family) {
1387 	case AF_INET:
1388 		paddr = &((struct sockaddr_in *)s)->sin_addr;
1389 		break;
1390 	case AF_INET6:
1391 		paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
1392 		break;
1393 	}
1394 
1395 	if (paddr == NULL)
1396 		return (0);
1397 
1398 	if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
1399 		return (0);
1400 
1401 	return (strlen(buf));
1402 }
1403 
1404 int
1405 rt_print(char *buf, int buflen, struct rtentry *rt)
1406 {
1407 	struct sockaddr *addr, *mask;
1408 	int i = 0;
1409 
1410 	addr = rt_key(rt);
1411 	mask = rt_mask(rt);
1412 
1413 	i = p_sockaddr(buf, buflen, addr);
1414 	if (!(rt->rt_flags & RTF_HOST)) {
1415 		buf[i++] = '/';
1416 		i += p_sockaddr(buf + i, buflen - i, mask);
1417 	}
1418 
1419 	if (rt->rt_flags & RTF_GATEWAY) {
1420 		buf[i++] = '>';
1421 		i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway);
1422 	}
1423 
1424 	return (i);
1425 }
1426 #endif
1427 
1428 #ifdef RADIX_MPATH
1429 /*
1430  * Deletes key for single-path routes, unlinks rtentry with
1431  * gateway specified in @info from multi-path routes.
1432  *
1433  * Returnes unlinked entry. In case of failure, returns NULL
1434  * and sets @perror to ESRCH.
1435  */
1436 static struct radix_node *
rt_mpath_unlink(struct radix_node_head * rnh,struct rt_addrinfo * info,struct rtentry * rto,int * perror)1437 rt_mpath_unlink(struct radix_node_head *rnh, struct rt_addrinfo *info,
1438     struct rtentry *rto, int *perror)
1439 {
1440 	/*
1441 	 * if we got multipath routes, we require users to specify
1442 	 * a matching RTAX_GATEWAY.
1443 	 */
1444 	struct rtentry *rt; // *rto = NULL;
1445 	struct radix_node *rn;
1446 	struct sockaddr *gw;
1447 
1448 	gw = info->rti_info[RTAX_GATEWAY];
1449 	rt = rt_mpath_matchgate(rto, gw);
1450 	if (rt == NULL) {
1451 		*perror = ESRCH;
1452 		return (NULL);
1453 	}
1454 
1455 	/*
1456 	 * this is the first entry in the chain
1457 	 */
1458 	if (rto == rt) {
1459 		rn = rn_mpath_next((struct radix_node *)rt);
1460 		/*
1461 		 * there is another entry, now it's active
1462 		 */
1463 		if (rn) {
1464 			rto = RNTORT(rn);
1465 			RT_LOCK(rto);
1466 			rto->rt_flags |= RTF_UP;
1467 			RT_UNLOCK(rto);
1468 		} else if (rt->rt_flags & RTF_GATEWAY) {
1469 			/*
1470 			 * For gateway routes, we need to
1471 			 * make sure that we we are deleting
1472 			 * the correct gateway.
1473 			 * rt_mpath_matchgate() does not
1474 			 * check the case when there is only
1475 			 * one route in the chain.
1476 			 */
1477 			if (gw &&
1478 			    (rt->rt_gateway->sa_len != gw->sa_len ||
1479 				memcmp(rt->rt_gateway, gw, gw->sa_len))) {
1480 				*perror = ESRCH;
1481 				return (NULL);
1482 			}
1483 		}
1484 
1485 		/*
1486 		 * use the normal delete code to remove
1487 		 * the first entry
1488 		 */
1489 		rn = rnh->rnh_deladdr(dst, netmask, rnh);
1490 		atomic_add_int(&rnh->rnh_gen, 1);
1491 		*perror = 0;
1492 		return (rn);
1493 	}
1494 
1495 	/*
1496 	 * if the entry is 2nd and on up
1497 	 */
1498 	if (rt_mpath_deldup(rto, rt) == 0)
1499 		panic ("rtrequest1: rt_mpath_deldup");
1500 	*perror = 0;
1501 	rn = (struct radix_node *)rt;
1502 	return (rn);
1503 }
1504 #endif
1505 
1506 #ifdef FLOWTABLE
1507 static struct rtentry *
rt_flowtable_check_route(struct radix_node_head * rnh,struct rt_addrinfo * info)1508 rt_flowtable_check_route(struct radix_node_head *rnh, struct rt_addrinfo *info)
1509 {
1510 #if defined(INET6) || defined(INET)
1511 	struct radix_node *rn;
1512 #endif
1513 	struct rtentry *rt0;
1514 
1515 	rt0 = NULL;
1516 	/* "flow-table" only supports IPv6 and IPv4 at the moment. */
1517 	switch (dst->sa_family) {
1518 #ifdef INET6
1519 	case AF_INET6:
1520 #endif
1521 #ifdef INET
1522 	case AF_INET:
1523 #endif
1524 #if defined(INET6) || defined(INET)
1525 		rn = rnh->rnh_matchaddr(dst, rnh);
1526 		if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
1527 			struct sockaddr *mask;
1528 			u_char *m, *n;
1529 			int len;
1530 
1531 			/*
1532 			 * compare mask to see if the new route is
1533 			 * more specific than the existing one
1534 			 */
1535 			rt0 = RNTORT(rn);
1536 			RT_LOCK(rt0);
1537 			RT_ADDREF(rt0);
1538 			RT_UNLOCK(rt0);
1539 			/*
1540 			 * A host route is already present, so
1541 			 * leave the flow-table entries as is.
1542 			 */
1543 			if (rt0->rt_flags & RTF_HOST) {
1544 				RTFREE(rt0);
1545 				rt0 = NULL;
1546 			} else if (!(flags & RTF_HOST) && netmask) {
1547 				mask = rt_mask(rt0);
1548 				len = mask->sa_len;
1549 				m = (u_char *)mask;
1550 				n = (u_char *)netmask;
1551 				while (len-- > 0) {
1552 					if (*n != *m)
1553 						break;
1554 					n++;
1555 					m++;
1556 				}
1557 				if (len == 0 || (*n < *m)) {
1558 					RTFREE(rt0);
1559 					rt0 = NULL;
1560 				}
1561 			}
1562 		}
1563 #endif/* INET6 || INET */
1564 	}
1565 
1566 	return (rt0);
1567 }
1568 #endif
1569 
1570 int
rtrequest1_fib(int req,struct rt_addrinfo * info,struct rtentry ** ret_nrt,u_int fibnum)1571 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
1572 				u_int fibnum)
1573 {
1574 	int error = 0;
1575 	struct rtentry *rt, *rt_old;
1576 #ifdef FLOWTABLE
1577 	struct rtentry *rt0;
1578 #endif
1579 	struct radix_node *rn;
1580 	struct radix_node_head *rnh;
1581 	struct ifaddr *ifa;
1582 	struct sockaddr *ndst;
1583 	struct sockaddr_storage mdst;
1584 
1585 	KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1586 	KASSERT((flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked"));
1587 	switch (dst->sa_family) {
1588 	case AF_INET6:
1589 	case AF_INET:
1590 		/* We support multiple FIBs. */
1591 		break;
1592 	default:
1593 		fibnum = RT_DEFAULT_FIB;
1594 		break;
1595 	}
1596 
1597 	/*
1598 	 * Find the correct routing tree to use for this Address Family
1599 	 */
1600 	rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1601 	if (rnh == NULL)
1602 		return (EAFNOSUPPORT);
1603 
1604 	/*
1605 	 * If we are adding a host route then we don't want to put
1606 	 * a netmask in the tree, nor do we want to clone it.
1607 	 */
1608 	if (flags & RTF_HOST)
1609 		netmask = NULL;
1610 
1611 	switch (req) {
1612 	case RTM_DELETE:
1613 		if (netmask) {
1614 			rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
1615 			dst = (struct sockaddr *)&mdst;
1616 		}
1617 		RADIX_NODE_HEAD_LOCK(rnh);
1618 		rt = rt_unlinkrte(rnh, info, &error);
1619 		RADIX_NODE_HEAD_UNLOCK(rnh);
1620 		if (error != 0)
1621 			return (error);
1622 
1623 		rt_notifydelete(rt, info);
1624 
1625 		/*
1626 		 * If the caller wants it, then it can have it,
1627 		 * but it's up to it to free the rtentry as we won't be
1628 		 * doing it.
1629 		 */
1630 		if (ret_nrt) {
1631 			*ret_nrt = rt;
1632 			RT_UNLOCK(rt);
1633 		} else
1634 			RTFREE_LOCKED(rt);
1635 		break;
1636 	case RTM_RESOLVE:
1637 		/*
1638 		 * resolve was only used for route cloning
1639 		 * here for compat
1640 		 */
1641 		break;
1642 	case RTM_ADD:
1643 		if ((flags & RTF_GATEWAY) && !gateway)
1644 			return (EINVAL);
1645 		if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1646 		    (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1647 			return (EINVAL);
1648 
1649 		if (info->rti_ifa == NULL) {
1650 			error = rt_getifa_fib(info, fibnum);
1651 			if (error)
1652 				return (error);
1653 		} else
1654 			ifa_ref(info->rti_ifa);
1655 		ifa = info->rti_ifa;
1656 		rt = uma_zalloc(V_rtzone, M_NOWAIT);
1657 		if (rt == NULL) {
1658 			ifa_free(ifa);
1659 			return (ENOBUFS);
1660 		}
1661 		rt->rt_flags = RTF_UP | flags;
1662 		rt->rt_fibnum = fibnum;
1663 		/*
1664 		 * Add the gateway. Possibly re-malloc-ing the storage for it.
1665 		 */
1666 		if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1667 			ifa_free(ifa);
1668 			uma_zfree(V_rtzone, rt);
1669 			return (error);
1670 		}
1671 
1672 		/*
1673 		 * point to the (possibly newly malloc'd) dest address.
1674 		 */
1675 		ndst = (struct sockaddr *)rt_key(rt);
1676 
1677 		/*
1678 		 * make sure it contains the value we want (masked if needed).
1679 		 */
1680 		if (netmask) {
1681 			rt_maskedcopy(dst, ndst, netmask);
1682 		} else
1683 			bcopy(dst, ndst, dst->sa_len);
1684 
1685 		/*
1686 		 * We use the ifa reference returned by rt_getifa_fib().
1687 		 * This moved from below so that rnh->rnh_addaddr() can
1688 		 * examine the ifa and  ifa->ifa_ifp if it so desires.
1689 		 */
1690 		rt->rt_ifa = ifa;
1691 		rt->rt_ifp = ifa->ifa_ifp;
1692 		rt->rt_weight = 1;
1693 
1694 		rt_setmetrics(info, rt);
1695 
1696 		RADIX_NODE_HEAD_LOCK(rnh);
1697 		RT_LOCK(rt);
1698 #ifdef RADIX_MPATH
1699 		/* do not permit exactly the same dst/mask/gw pair */
1700 		if (rn_mpath_capable(rnh) &&
1701 			rt_mpath_conflict(rnh, rt, netmask)) {
1702 			RADIX_NODE_HEAD_UNLOCK(rnh);
1703 
1704 			ifa_free(rt->rt_ifa);
1705 			R_Free(rt_key(rt));
1706 			uma_zfree(V_rtzone, rt);
1707 			return (EEXIST);
1708 		}
1709 #endif
1710 
1711 #ifdef FLOWTABLE
1712 		rt0 = rt_flowtable_check_route(rnh, info);
1713 #endif /* FLOWTABLE */
1714 
1715 		/* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1716 		rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1717 		atomic_add_int(&rnh->rnh_gen, 1);
1718 
1719 		rt_old = NULL;
1720 		if (rn == NULL && (info->rti_flags & RTF_PINNED) != 0) {
1721 
1722 			/*
1723 			 * Force removal and re-try addition
1724 			 * TODO: better multipath&pinned support
1725 			 */
1726 			struct sockaddr *info_dst = info->rti_info[RTAX_DST];
1727 			info->rti_info[RTAX_DST] = ndst;
1728 			/* Do not delete existing PINNED(interface) routes */
1729 			info->rti_flags &= ~RTF_PINNED;
1730 			rt_old = rt_unlinkrte(rnh, info, &error);
1731 			info->rti_flags |= RTF_PINNED;
1732 			info->rti_info[RTAX_DST] = info_dst;
1733 			if (rt_old != NULL) {
1734 				rn = rnh->rnh_addaddr(ndst, netmask, rnh,
1735 						      rt->rt_nodes);
1736 				atomic_add_int(&rnh->rnh_gen, 1);
1737 			}
1738 		}
1739 		RADIX_NODE_HEAD_UNLOCK(rnh);
1740 
1741 		if (rt_old != NULL)
1742 			RT_UNLOCK(rt_old);
1743 
1744 		/*
1745 		 * If it still failed to go into the tree,
1746 		 * then un-make it (this should be a function)
1747 		 */
1748 		if (rn == NULL) {
1749 			ifa_free(rt->rt_ifa);
1750 			R_Free(rt_key(rt));
1751 			uma_zfree(V_rtzone, rt);
1752 #ifdef FLOWTABLE
1753 			if (rt0 != NULL)
1754 				RTFREE(rt0);
1755 #endif
1756 			return (EEXIST);
1757 		}
1758 #ifdef FLOWTABLE
1759 		else if (rt0 != NULL) {
1760 			flowtable_route_flush(dst->sa_family, rt0);
1761 			RTFREE(rt0);
1762 		}
1763 #endif
1764 
1765 		if (rt_old != NULL) {
1766 			rt_notifydelete(rt_old, info);
1767 			RTFREE(rt_old);
1768 		}
1769 
1770 		/*
1771 		 * If this protocol has something to add to this then
1772 		 * allow it to do that as well.
1773 		 */
1774 		if (ifa->ifa_rtrequest)
1775 			ifa->ifa_rtrequest(req, rt, info);
1776 
1777 		/*
1778 		 * actually return a resultant rtentry and
1779 		 * give the caller a single reference.
1780 		 */
1781 		if (ret_nrt) {
1782 			*ret_nrt = rt;
1783 			RT_ADDREF(rt);
1784 		}
1785 		RT_UNLOCK(rt);
1786 		break;
1787 	case RTM_CHANGE:
1788 		RADIX_NODE_HEAD_LOCK(rnh);
1789 		error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum);
1790 		RADIX_NODE_HEAD_UNLOCK(rnh);
1791 		break;
1792 	default:
1793 		error = EOPNOTSUPP;
1794 	}
1795 
1796 	return (error);
1797 }
1798 
1799 #undef dst
1800 #undef gateway
1801 #undef netmask
1802 #undef ifaaddr
1803 #undef ifpaddr
1804 #undef flags
1805 
1806 static int
rtrequest1_fib_change(struct radix_node_head * rnh,struct rt_addrinfo * info,struct rtentry ** ret_nrt,u_int fibnum)1807 rtrequest1_fib_change(struct radix_node_head *rnh, struct rt_addrinfo *info,
1808     struct rtentry **ret_nrt, u_int fibnum)
1809 {
1810 	struct rtentry *rt = NULL;
1811 	int error = 0;
1812 	int free_ifa = 0;
1813 	int family, mtu;
1814 	struct if_mtuinfo ifmtu;
1815 
1816 	rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1817 	    info->rti_info[RTAX_NETMASK], rnh);
1818 
1819 	if (rt == NULL)
1820 		return (ESRCH);
1821 
1822 #ifdef RADIX_MPATH
1823 	/*
1824 	 * If we got multipath routes,
1825 	 * we require users to specify a matching RTAX_GATEWAY.
1826 	 */
1827 	if (rn_mpath_capable(rnh)) {
1828 		rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
1829 		if (rt == NULL)
1830 			return (ESRCH);
1831 	}
1832 #endif
1833 
1834 	RT_LOCK(rt);
1835 
1836 	rt_setmetrics(info, rt);
1837 
1838 	/*
1839 	 * New gateway could require new ifaddr, ifp;
1840 	 * flags may also be different; ifp may be specified
1841 	 * by ll sockaddr when protocol address is ambiguous
1842 	 */
1843 	if (((rt->rt_flags & RTF_GATEWAY) &&
1844 	    info->rti_info[RTAX_GATEWAY] != NULL) ||
1845 	    info->rti_info[RTAX_IFP] != NULL ||
1846 	    (info->rti_info[RTAX_IFA] != NULL &&
1847 	     !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) {
1848 
1849 		error = rt_getifa_fib(info, fibnum);
1850 		if (info->rti_ifa != NULL)
1851 			free_ifa = 1;
1852 
1853 		if (error != 0)
1854 			goto bad;
1855 	}
1856 
1857 	/* Check if outgoing interface has changed */
1858 	if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa &&
1859 	    rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) {
1860 		rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1861 		ifa_free(rt->rt_ifa);
1862 	}
1863 	/* Update gateway address */
1864 	if (info->rti_info[RTAX_GATEWAY] != NULL) {
1865 		error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]);
1866 		if (error != 0)
1867 			goto bad;
1868 
1869 		rt->rt_flags &= ~RTF_GATEWAY;
1870 		rt->rt_flags |= (RTF_GATEWAY & info->rti_flags);
1871 	}
1872 
1873 	if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) {
1874 		ifa_ref(info->rti_ifa);
1875 		rt->rt_ifa = info->rti_ifa;
1876 		rt->rt_ifp = info->rti_ifp;
1877 	}
1878 	/* Allow some flags to be toggled on change. */
1879 	rt->rt_flags &= ~RTF_FMASK;
1880 	rt->rt_flags |= info->rti_flags & RTF_FMASK;
1881 
1882 	if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL)
1883 	       rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
1884 
1885 	/* Alter route MTU if necessary */
1886 	if (rt->rt_ifp != NULL) {
1887 		family = info->rti_info[RTAX_DST]->sa_family;
1888 		mtu = if_getmtu_family(rt->rt_ifp, family);
1889 		/* Set default MTU */
1890 		if (rt->rt_mtu == 0)
1891 			rt->rt_mtu = mtu;
1892 		if (rt->rt_mtu != mtu) {
1893 			/* Check if we really need to update */
1894 			ifmtu.ifp = rt->rt_ifp;
1895 			ifmtu.mtu = mtu;
1896 			if_updatemtu_cb(rt->rt_nodes, &ifmtu);
1897 		}
1898 	}
1899 
1900 	if (ret_nrt) {
1901 		*ret_nrt = rt;
1902 		RT_ADDREF(rt);
1903 	}
1904 bad:
1905 	RT_UNLOCK(rt);
1906 	if (free_ifa != 0)
1907 		ifa_free(info->rti_ifa);
1908 	return (error);
1909 }
1910 
1911 static void
rt_setmetrics(const struct rt_addrinfo * info,struct rtentry * rt)1912 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt)
1913 {
1914 
1915 	if (info->rti_mflags & RTV_MTU) {
1916 		if (info->rti_rmx->rmx_mtu != 0) {
1917 
1918 			/*
1919 			 * MTU was explicitly provided by user.
1920 			 * Keep it.
1921 			 */
1922 			rt->rt_flags |= RTF_FIXEDMTU;
1923 		} else {
1924 
1925 			/*
1926 			 * User explicitly sets MTU to 0.
1927 			 * Assume rollback to default.
1928 			 */
1929 			rt->rt_flags &= ~RTF_FIXEDMTU;
1930 		}
1931 		rt->rt_mtu = info->rti_rmx->rmx_mtu;
1932 	}
1933 	if (info->rti_mflags & RTV_WEIGHT)
1934 		rt->rt_weight = info->rti_rmx->rmx_weight;
1935 	/* Kernel -> userland timebase conversion. */
1936 	if (info->rti_mflags & RTV_EXPIRE)
1937 		rt->rt_expire = info->rti_rmx->rmx_expire ?
1938 		    info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
1939 }
1940 
1941 int
rt_setgate(struct rtentry * rt,struct sockaddr * dst,struct sockaddr * gate)1942 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1943 {
1944 	/* XXX dst may be overwritten, can we move this to below */
1945 	int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1946 
1947 	/*
1948 	 * Prepare to store the gateway in rt->rt_gateway.
1949 	 * Both dst and gateway are stored one after the other in the same
1950 	 * malloc'd chunk. If we have room, we can reuse the old buffer,
1951 	 * rt_gateway already points to the right place.
1952 	 * Otherwise, malloc a new block and update the 'dst' address.
1953 	 */
1954 	if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1955 		caddr_t new;
1956 
1957 		R_Malloc(new, caddr_t, dlen + glen);
1958 		if (new == NULL)
1959 			return ENOBUFS;
1960 		/*
1961 		 * XXX note, we copy from *dst and not *rt_key(rt) because
1962 		 * rt_setgate() can be called to initialize a newly
1963 		 * allocated route entry, in which case rt_key(rt) == NULL
1964 		 * (and also rt->rt_gateway == NULL).
1965 		 * Free()/free() handle a NULL argument just fine.
1966 		 */
1967 		bcopy(dst, new, dlen);
1968 		R_Free(rt_key(rt));	/* free old block, if any */
1969 		rt_key(rt) = (struct sockaddr *)new;
1970 		rt->rt_gateway = (struct sockaddr *)(new + dlen);
1971 	}
1972 
1973 	/*
1974 	 * Copy the new gateway value into the memory chunk.
1975 	 */
1976 	bcopy(gate, rt->rt_gateway, glen);
1977 
1978 	return (0);
1979 }
1980 
1981 void
rt_maskedcopy(struct sockaddr * src,struct sockaddr * dst,struct sockaddr * netmask)1982 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1983 {
1984 	u_char *cp1 = (u_char *)src;
1985 	u_char *cp2 = (u_char *)dst;
1986 	u_char *cp3 = (u_char *)netmask;
1987 	u_char *cplim = cp2 + *cp3;
1988 	u_char *cplim2 = cp2 + *cp1;
1989 
1990 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1991 	cp3 += 2;
1992 	if (cplim > cplim2)
1993 		cplim = cplim2;
1994 	while (cp2 < cplim)
1995 		*cp2++ = *cp1++ & *cp3++;
1996 	if (cp2 < cplim2)
1997 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1998 }
1999 
2000 /*
2001  * Set up a routing table entry, normally
2002  * for an interface.
2003  */
2004 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
2005 static inline  int
rtinit1(struct ifaddr * ifa,int cmd,int flags,int fibnum)2006 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
2007 {
2008 	struct sockaddr *dst;
2009 	struct sockaddr *netmask;
2010 	struct rtentry *rt = NULL;
2011 	struct rt_addrinfo info;
2012 	int error = 0;
2013 	int startfib, endfib;
2014 	char tempbuf[_SOCKADDR_TMPSIZE];
2015 	int didwork = 0;
2016 	int a_failure = 0;
2017 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
2018 	struct radix_node_head *rnh;
2019 
2020 	if (flags & RTF_HOST) {
2021 		dst = ifa->ifa_dstaddr;
2022 		netmask = NULL;
2023 	} else {
2024 		dst = ifa->ifa_addr;
2025 		netmask = ifa->ifa_netmask;
2026 	}
2027 	if (dst->sa_len == 0)
2028 		return(EINVAL);
2029 	switch (dst->sa_family) {
2030 	case AF_INET6:
2031 	case AF_INET:
2032 		/* We support multiple FIBs. */
2033 		break;
2034 	default:
2035 		fibnum = RT_DEFAULT_FIB;
2036 		break;
2037 	}
2038 	if (fibnum == RT_ALL_FIBS) {
2039 		if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD)
2040 			startfib = endfib = ifa->ifa_ifp->if_fib;
2041 		else {
2042 			startfib = 0;
2043 			endfib = rt_numfibs - 1;
2044 		}
2045 	} else {
2046 		KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
2047 		startfib = fibnum;
2048 		endfib = fibnum;
2049 	}
2050 
2051 	/*
2052 	 * If it's a delete, check that if it exists,
2053 	 * it's on the correct interface or we might scrub
2054 	 * a route to another ifa which would
2055 	 * be confusing at best and possibly worse.
2056 	 */
2057 	if (cmd == RTM_DELETE) {
2058 		/*
2059 		 * It's a delete, so it should already exist..
2060 		 * If it's a net, mask off the host bits
2061 		 * (Assuming we have a mask)
2062 		 * XXX this is kinda inet specific..
2063 		 */
2064 		if (netmask != NULL) {
2065 			rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
2066 			dst = (struct sockaddr *)tempbuf;
2067 		}
2068 	}
2069 	/*
2070 	 * Now go through all the requested tables (fibs) and do the
2071 	 * requested action. Realistically, this will either be fib 0
2072 	 * for protocols that don't do multiple tables or all the
2073 	 * tables for those that do.
2074 	 */
2075 	for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
2076 		if (cmd == RTM_DELETE) {
2077 			struct radix_node *rn;
2078 			/*
2079 			 * Look up an rtentry that is in the routing tree and
2080 			 * contains the correct info.
2081 			 */
2082 			rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
2083 			if (rnh == NULL)
2084 				/* this table doesn't exist but others might */
2085 				continue;
2086 			RADIX_NODE_HEAD_RLOCK(rnh);
2087 			rn = rnh->rnh_lookup(dst, netmask, rnh);
2088 #ifdef RADIX_MPATH
2089 			if (rn_mpath_capable(rnh)) {
2090 
2091 				if (rn == NULL)
2092 					error = ESRCH;
2093 				else {
2094 					rt = RNTORT(rn);
2095 					/*
2096 					 * for interface route the
2097 					 * rt->rt_gateway is sockaddr_intf
2098 					 * for cloning ARP entries, so
2099 					 * rt_mpath_matchgate must use the
2100 					 * interface address
2101 					 */
2102 					rt = rt_mpath_matchgate(rt,
2103 					    ifa->ifa_addr);
2104 					if (rt == NULL)
2105 						error = ESRCH;
2106 				}
2107 			}
2108 #endif
2109 			error = (rn == NULL ||
2110 			    (rn->rn_flags & RNF_ROOT) ||
2111 			    RNTORT(rn)->rt_ifa != ifa);
2112 			RADIX_NODE_HEAD_RUNLOCK(rnh);
2113 			if (error) {
2114 				/* this is only an error if bad on ALL tables */
2115 				continue;
2116 			}
2117 		}
2118 		/*
2119 		 * Do the actual request
2120 		 */
2121 		bzero((caddr_t)&info, sizeof(info));
2122 		info.rti_ifa = ifa;
2123 		info.rti_flags = flags |
2124 		    (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
2125 		info.rti_info[RTAX_DST] = dst;
2126 		/*
2127 		 * doing this for compatibility reasons
2128 		 */
2129 		if (cmd == RTM_ADD)
2130 			info.rti_info[RTAX_GATEWAY] =
2131 			    (struct sockaddr *)&null_sdl;
2132 		else
2133 			info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
2134 		info.rti_info[RTAX_NETMASK] = netmask;
2135 		error = rtrequest1_fib(cmd, &info, &rt, fibnum);
2136 
2137 		if (error == 0 && rt != NULL) {
2138 			/*
2139 			 * notify any listening routing agents of the change
2140 			 */
2141 			RT_LOCK(rt);
2142 #ifdef RADIX_MPATH
2143 			/*
2144 			 * in case address alias finds the first address
2145 			 * e.g. ifconfig bge0 192.0.2.246/24
2146 			 * e.g. ifconfig bge0 192.0.2.247/24
2147 			 * the address set in the route is 192.0.2.246
2148 			 * so we need to replace it with 192.0.2.247
2149 			 */
2150 			if (memcmp(rt->rt_ifa->ifa_addr,
2151 			    ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
2152 				ifa_free(rt->rt_ifa);
2153 				ifa_ref(ifa);
2154 				rt->rt_ifp = ifa->ifa_ifp;
2155 				rt->rt_ifa = ifa;
2156 			}
2157 #endif
2158 			/*
2159 			 * doing this for compatibility reasons
2160 			 */
2161 			if (cmd == RTM_ADD) {
2162 			    ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
2163 				rt->rt_ifp->if_type;
2164 			    ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
2165 				rt->rt_ifp->if_index;
2166 			}
2167 			RT_ADDREF(rt);
2168 			RT_UNLOCK(rt);
2169 			rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum);
2170 			RT_LOCK(rt);
2171 			RT_REMREF(rt);
2172 			if (cmd == RTM_DELETE) {
2173 				/*
2174 				 * If we are deleting, and we found an entry,
2175 				 * then it's been removed from the tree..
2176 				 * now throw it away.
2177 				 */
2178 				RTFREE_LOCKED(rt);
2179 			} else {
2180 				if (cmd == RTM_ADD) {
2181 					/*
2182 					 * We just wanted to add it..
2183 					 * we don't actually need a reference.
2184 					 */
2185 					RT_REMREF(rt);
2186 				}
2187 				RT_UNLOCK(rt);
2188 			}
2189 			didwork = 1;
2190 		}
2191 		if (error)
2192 			a_failure = error;
2193 	}
2194 	if (cmd == RTM_DELETE) {
2195 		if (didwork) {
2196 			error = 0;
2197 		} else {
2198 			/* we only give an error if it wasn't in any table */
2199 			error = ((flags & RTF_HOST) ?
2200 			    EHOSTUNREACH : ENETUNREACH);
2201 		}
2202 	} else {
2203 		if (a_failure) {
2204 			/* return an error if any of them failed */
2205 			error = a_failure;
2206 		}
2207 	}
2208 	return (error);
2209 }
2210 
2211 /*
2212  * Set up a routing table entry, normally
2213  * for an interface.
2214  */
2215 int
rtinit(struct ifaddr * ifa,int cmd,int flags)2216 rtinit(struct ifaddr *ifa, int cmd, int flags)
2217 {
2218 	struct sockaddr *dst;
2219 	int fib = RT_DEFAULT_FIB;
2220 
2221 	if (flags & RTF_HOST) {
2222 		dst = ifa->ifa_dstaddr;
2223 	} else {
2224 		dst = ifa->ifa_addr;
2225 	}
2226 
2227 	switch (dst->sa_family) {
2228 	case AF_INET6:
2229 	case AF_INET:
2230 		/* We do support multiple FIBs. */
2231 		fib = RT_ALL_FIBS;
2232 		break;
2233 	}
2234 	return (rtinit1(ifa, cmd, flags, fib));
2235 }
2236 
2237 /*
2238  * Announce interface address arrival/withdraw
2239  * Returns 0 on success.
2240  */
2241 int
rt_addrmsg(int cmd,struct ifaddr * ifa,int fibnum)2242 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
2243 {
2244 
2245 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2246 	    ("unexpected cmd %d", cmd));
2247 
2248 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2249 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2250 
2251 #if defined(INET) || defined(INET6)
2252 #ifdef SCTP
2253 	/*
2254 	 * notify the SCTP stack
2255 	 * this will only get called when an address is added/deleted
2256 	 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
2257 	 */
2258 	sctp_addr_change(ifa, cmd);
2259 #endif /* SCTP */
2260 #endif
2261 	return (rtsock_addrmsg(cmd, ifa, fibnum));
2262 }
2263 
2264 /*
2265  * Announce route addition/removal.
2266  * Users of this function MUST validate input data BEFORE calling.
2267  * However we have to be able to handle invalid data:
2268  * if some userland app sends us "invalid" route message (invalid mask,
2269  * no dst, wrong address families, etc...) we need to pass it back
2270  * to app (and any other rtsock consumers) with rtm_errno field set to
2271  * non-zero value.
2272  * Returns 0 on success.
2273  */
2274 int
rt_routemsg(int cmd,struct ifnet * ifp,int error,struct rtentry * rt,int fibnum)2275 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
2276     int fibnum)
2277 {
2278 
2279 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2280 	    ("unexpected cmd %d", cmd));
2281 
2282 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2283 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2284 
2285 	KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
2286 
2287 	return (rtsock_routemsg(cmd, ifp, error, rt, fibnum));
2288 }
2289 
2290 void
rt_newaddrmsg(int cmd,struct ifaddr * ifa,int error,struct rtentry * rt)2291 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
2292 {
2293 
2294 	rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
2295 }
2296 
2297 /*
2298  * This is called to generate messages from the routing socket
2299  * indicating a network interface has had addresses associated with it.
2300  */
2301 void
rt_newaddrmsg_fib(int cmd,struct ifaddr * ifa,int error,struct rtentry * rt,int fibnum)2302 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
2303     int fibnum)
2304 {
2305 
2306 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2307 		("unexpected cmd %u", cmd));
2308 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2309 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2310 
2311 	if (cmd == RTM_ADD) {
2312 		rt_addrmsg(cmd, ifa, fibnum);
2313 		if (rt != NULL)
2314 			rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2315 	} else {
2316 		if (rt != NULL)
2317 			rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2318 		rt_addrmsg(cmd, ifa, fibnum);
2319 	}
2320 }
2321 
2322