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