xref: /freebsd/sys/net/route.c (revision 7d0d268b8a67f28ccefdd0b8ce6fb38acac78d80)
1 /*-
2  * Copyright (c) 1980, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)route.c	8.3.1.1 (Berkeley) 2/23/95
30  * $FreeBSD$
31  */
32 /************************************************************************
33  * Note: In this file a 'fib' is a "forwarding information base"	*
34  * Which is the new name for an in kernel routing (next hop) table.	*
35  ***********************************************************************/
36 
37 #include "opt_inet.h"
38 #include "opt_route.h"
39 #include "opt_mrouting.h"
40 #include "opt_mpath.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/syslog.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/sysproto.h>
51 #include <sys/proc.h>
52 #include <sys/domain.h>
53 #include <sys/kernel.h>
54 #include <sys/vimage.h>
55 
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 
60 #ifdef RADIX_MPATH
61 #include <net/radix_mpath.h>
62 #endif
63 #include <net/vnet.h>
64 
65 #include <netinet/in.h>
66 #include <netinet/ip_mroute.h>
67 #include <netinet/vinet.h>
68 
69 #include <vm/uma.h>
70 
71 u_int rt_numfibs = RT_NUMFIBS;
72 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
73 /*
74  * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
75  * We can't do more because storage is statically allocated for now.
76  * (for compatibility reasons.. this will change).
77  */
78 TUNABLE_INT("net.fibs", &rt_numfibs);
79 
80 /*
81  * By default add routes to all fibs for new interfaces.
82  * Once this is set to 0 then only allocate routes on interface
83  * changes for the FIB of the caller when adding a new set of addresses
84  * to an interface.  XXX this is a shotgun aproach to a problem that needs
85  * a more fine grained solution.. that will come.
86  */
87 u_int rt_add_addr_allfibs = 1;
88 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
89     &rt_add_addr_allfibs, 0, "");
90 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
91 
92 #ifdef VIMAGE_GLOBALS
93 static struct rtstat rtstat;
94 
95 /* by default only the first 'row' of tables will be accessed. */
96 /*
97  * XXXMRT When we fix netstat, and do this differnetly,
98  * we can allocate this dynamically. As long as we are keeping
99  * things backwards compaitble we need to allocate this
100  * statically.
101  */
102 struct radix_node_head *rt_tables[RT_MAXFIBS][AF_MAX+1];
103 
104 static int	rttrash;		/* routes not in table but not freed */
105 #endif
106 
107 static void rt_maskedcopy(struct sockaddr *,
108 	    struct sockaddr *, struct sockaddr *);
109 
110 /* compare two sockaddr structures */
111 #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
112 
113 /*
114  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
115  * The operation can be done safely (in this code) because a
116  * 'struct rtentry' starts with two 'struct radix_node''s, the first
117  * one representing leaf nodes in the routing tree, which is
118  * what the code in radix.c passes us as a 'struct radix_node'.
119  *
120  * But because there are a lot of assumptions in this conversion,
121  * do not cast explicitly, but always use the macro below.
122  */
123 #define RNTORT(p)	((struct rtentry *)(p))
124 
125 static uma_zone_t rtzone;		/* Routing table UMA zone. */
126 
127 #if 0
128 /* default fib for tunnels to use */
129 u_int tunnel_fib = 0;
130 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
131 #endif
132 
133 /*
134  * handler for net.my_fibnum
135  */
136 static int
137 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
138 {
139         int fibnum;
140         int error;
141 
142         fibnum = curthread->td_proc->p_fibnum;
143         error = sysctl_handle_int(oidp, &fibnum, 0, req);
144         return (error);
145 }
146 
147 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
148             NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
149 
150 static void
151 route_init(void)
152 {
153 	INIT_VNET_INET(curvnet);
154 	int table;
155 	struct domain *dom;
156 	int fam;
157 
158 	/* whack the tunable ints into  line. */
159 	if (rt_numfibs > RT_MAXFIBS)
160 		rt_numfibs = RT_MAXFIBS;
161 	if (rt_numfibs == 0)
162 		rt_numfibs = 1;
163 	rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
164 	    NULL, NULL, UMA_ALIGN_PTR, 0);
165 	rn_init();	/* initialize all zeroes, all ones, mask table */
166 
167 	for (dom = domains; dom; dom = dom->dom_next) {
168 		if (dom->dom_rtattach)  {
169 			for  (table = 0; table < rt_numfibs; table++) {
170 				if ( (fam = dom->dom_family) == AF_INET ||
171 				    table == 0) {
172  			        	/* for now only AF_INET has > 1 table */
173 					/* XXX MRT
174 					 * rtattach will be also called
175 					 * from vfs_export.c but the
176 					 * offset will be 0
177 					 * (only for AF_INET and AF_INET6
178 					 * which don't need it anyhow)
179 					 */
180 					dom->dom_rtattach(
181 				    	    (void **)&V_rt_tables[table][fam],
182 				    	    dom->dom_rtoffset);
183 				} else {
184 					break;
185 				}
186 			}
187 		}
188 	}
189 }
190 
191 #ifndef _SYS_SYSPROTO_H_
192 struct setfib_args {
193 	int     fibnum;
194 };
195 #endif
196 int
197 setfib(struct thread *td, struct setfib_args *uap)
198 {
199 	if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
200 		return EINVAL;
201 	td->td_proc->p_fibnum = uap->fibnum;
202 	return (0);
203 }
204 
205 /*
206  * Packet routing routines.
207  */
208 void
209 rtalloc(struct route *ro)
210 {
211 	rtalloc_ign_fib(ro, 0UL, 0);
212 }
213 
214 void
215 rtalloc_fib(struct route *ro, u_int fibnum)
216 {
217 	rtalloc_ign_fib(ro, 0UL, fibnum);
218 }
219 
220 void
221 rtalloc_ign(struct route *ro, u_long ignore)
222 {
223 	struct rtentry *rt;
224 
225 	if ((rt = ro->ro_rt) != NULL) {
226 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
227 			return;
228 		RTFREE(rt);
229 		ro->ro_rt = NULL;
230 	}
231 	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
232 	if (ro->ro_rt)
233 		RT_UNLOCK(ro->ro_rt);
234 }
235 
236 void
237 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
238 {
239 	struct rtentry *rt;
240 
241 	if ((rt = ro->ro_rt) != NULL) {
242 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
243 			return;
244 		RTFREE(rt);
245 		ro->ro_rt = NULL;
246 	}
247 	ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
248 	if (ro->ro_rt)
249 		RT_UNLOCK(ro->ro_rt);
250 }
251 
252 /*
253  * Look up the route that matches the address given
254  * Or, at least try.. Create a cloned route if needed.
255  *
256  * The returned route, if any, is locked.
257  */
258 struct rtentry *
259 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
260 {
261 	return (rtalloc1_fib(dst, report, ignflags, 0));
262 }
263 
264 struct rtentry *
265 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
266 		    u_int fibnum)
267 {
268 	INIT_VNET_NET(curvnet);
269 	struct radix_node_head *rnh;
270 	struct rtentry *rt;
271 	struct radix_node *rn;
272 	struct rtentry *newrt;
273 	struct rt_addrinfo info;
274 	int err = 0, msgtype = RTM_MISS;
275 	int needlock;
276 
277 	KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
278 	if (dst->sa_family != AF_INET)	/* Only INET supports > 1 fib now */
279 		fibnum = 0;
280 	rnh = V_rt_tables[fibnum][dst->sa_family];
281 	newrt = NULL;
282 	/*
283 	 * Look up the address in the table for that Address Family
284 	 */
285 	if (rnh == NULL) {
286 		V_rtstat.rts_unreach++;
287 		goto miss;
288 	}
289 	needlock = !(ignflags & RTF_RNH_LOCKED);
290 	if (needlock)
291 		RADIX_NODE_HEAD_RLOCK(rnh);
292 #ifdef INVARIANTS
293 	else
294 		RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
295 #endif
296 	rn = rnh->rnh_matchaddr(dst, rnh);
297 	if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
298 		newrt = rt = RNTORT(rn);
299 		RT_LOCK(newrt);
300 		RT_ADDREF(newrt);
301 		if (needlock)
302 			RADIX_NODE_HEAD_RUNLOCK(rnh);
303 		goto done;
304 
305 	} else if (needlock)
306 		RADIX_NODE_HEAD_RUNLOCK(rnh);
307 
308 	/*
309 	 * Either we hit the root or couldn't find any match,
310 	 * Which basically means
311 	 * "caint get there frm here"
312 	 */
313 	V_rtstat.rts_unreach++;
314 miss:
315 	if (report) {
316 		/*
317 		 * If required, report the failure to the supervising
318 		 * Authorities.
319 		 * For a delete, this is not an error. (report == 0)
320 		 */
321 		bzero(&info, sizeof(info));
322 		info.rti_info[RTAX_DST] = dst;
323 		rt_missmsg(msgtype, &info, 0, err);
324 	}
325 done:
326 	if (newrt)
327 		RT_LOCK_ASSERT(newrt);
328 	return (newrt);
329 }
330 
331 /*
332  * Remove a reference count from an rtentry.
333  * If the count gets low enough, take it out of the routing table
334  */
335 void
336 rtfree(struct rtentry *rt)
337 {
338 	INIT_VNET_NET(curvnet);
339 	struct radix_node_head *rnh;
340 
341 	KASSERT(rt != NULL,("%s: NULL rt", __func__));
342 	rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
343 	KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
344 
345 	RT_LOCK_ASSERT(rt);
346 
347 	/*
348 	 * The callers should use RTFREE_LOCKED() or RTFREE(), so
349 	 * we should come here exactly with the last reference.
350 	 */
351 	RT_REMREF(rt);
352 	if (rt->rt_refcnt > 0) {
353 		log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
354 		goto done;
355 	}
356 
357 	/*
358 	 * On last reference give the "close method" a chance
359 	 * to cleanup private state.  This also permits (for
360 	 * IPv4 and IPv6) a chance to decide if the routing table
361 	 * entry should be purged immediately or at a later time.
362 	 * When an immediate purge is to happen the close routine
363 	 * typically calls rtexpunge which clears the RTF_UP flag
364 	 * on the entry so that the code below reclaims the storage.
365 	 */
366 	if (rt->rt_refcnt == 0 && rnh->rnh_close)
367 		rnh->rnh_close((struct radix_node *)rt, rnh);
368 
369 	/*
370 	 * If we are no longer "up" (and ref == 0)
371 	 * then we can free the resources associated
372 	 * with the route.
373 	 */
374 	if ((rt->rt_flags & RTF_UP) == 0) {
375 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
376 			panic("rtfree 2");
377 		/*
378 		 * the rtentry must have been removed from the routing table
379 		 * so it is represented in rttrash.. remove that now.
380 		 */
381 		V_rttrash--;
382 #ifdef	DIAGNOSTIC
383 		if (rt->rt_refcnt < 0) {
384 			printf("rtfree: %p not freed (neg refs)\n", rt);
385 			goto done;
386 		}
387 #endif
388 		/*
389 		 * release references on items we hold them on..
390 		 * e.g other routes and ifaddrs.
391 		 */
392 		if (rt->rt_ifa)
393 			IFAFREE(rt->rt_ifa);
394 		/*
395 		 * The key is separatly alloc'd so free it (see rt_setgate()).
396 		 * This also frees the gateway, as they are always malloc'd
397 		 * together.
398 		 */
399 		Free(rt_key(rt));
400 
401 		/*
402 		 * and the rtentry itself of course
403 		 */
404 		RT_LOCK_DESTROY(rt);
405 		uma_zfree(rtzone, rt);
406 		return;
407 	}
408 done:
409 	RT_UNLOCK(rt);
410 }
411 
412 
413 /*
414  * Force a routing table entry to the specified
415  * destination to go through the given gateway.
416  * Normally called as a result of a routing redirect
417  * message from the network layer.
418  */
419 void
420 rtredirect(struct sockaddr *dst,
421 	struct sockaddr *gateway,
422 	struct sockaddr *netmask,
423 	int flags,
424 	struct sockaddr *src)
425 {
426 	rtredirect_fib(dst, gateway, netmask, flags, src, 0);
427 }
428 
429 void
430 rtredirect_fib(struct sockaddr *dst,
431 	struct sockaddr *gateway,
432 	struct sockaddr *netmask,
433 	int flags,
434 	struct sockaddr *src,
435 	u_int fibnum)
436 {
437 	INIT_VNET_NET(curvnet);
438 	struct rtentry *rt, *rt0 = NULL;
439 	int error = 0;
440 	short *stat = NULL;
441 	struct rt_addrinfo info;
442 	struct ifaddr *ifa;
443 	struct radix_node_head *rnh =
444 	    V_rt_tables[fibnum][dst->sa_family];
445 
446 	/* verify the gateway is directly reachable */
447 	if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
448 		error = ENETUNREACH;
449 		goto out;
450 	}
451 	rt = rtalloc1_fib(dst, 0, 0UL, fibnum);	/* NB: rt is locked */
452 	/*
453 	 * If the redirect isn't from our current router for this dst,
454 	 * it's either old or wrong.  If it redirects us to ourselves,
455 	 * we have a routing loop, perhaps as a result of an interface
456 	 * going down recently.
457 	 */
458 	if (!(flags & RTF_DONE) && rt &&
459 	     (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
460 		error = EINVAL;
461 	else if (ifa_ifwithaddr(gateway))
462 		error = EHOSTUNREACH;
463 	if (error)
464 		goto done;
465 	/*
466 	 * Create a new entry if we just got back a wildcard entry
467 	 * or the the lookup failed.  This is necessary for hosts
468 	 * which use routing redirects generated by smart gateways
469 	 * to dynamically build the routing tables.
470 	 */
471 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
472 		goto create;
473 	/*
474 	 * Don't listen to the redirect if it's
475 	 * for a route to an interface.
476 	 */
477 	if (rt->rt_flags & RTF_GATEWAY) {
478 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
479 			/*
480 			 * Changing from route to net => route to host.
481 			 * Create new route, rather than smashing route to net.
482 			 */
483 		create:
484 			rt0 = rt;
485 			rt = NULL;
486 
487 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
488 			bzero((caddr_t)&info, sizeof(info));
489 			info.rti_info[RTAX_DST] = dst;
490 			info.rti_info[RTAX_GATEWAY] = gateway;
491 			info.rti_info[RTAX_NETMASK] = netmask;
492 			info.rti_ifa = ifa;
493 			info.rti_flags = flags;
494 			if (rt0 != NULL)
495 				RT_UNLOCK(rt0);	/* drop lock to avoid LOR with RNH */
496 			error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
497 			if (rt != NULL) {
498 				RT_LOCK(rt);
499 				if (rt0 != NULL)
500 					EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
501 				flags = rt->rt_flags;
502 			}
503 			if (rt0 != NULL)
504 				RTFREE(rt0);
505 
506 			stat = &V_rtstat.rts_dynamic;
507 		} else {
508 			struct rtentry *gwrt;
509 
510 			/*
511 			 * Smash the current notion of the gateway to
512 			 * this destination.  Should check about netmask!!!
513 			 */
514 			rt->rt_flags |= RTF_MODIFIED;
515 			flags |= RTF_MODIFIED;
516 			stat = &V_rtstat.rts_newgateway;
517 			/*
518 			 * add the key and gateway (in one malloc'd chunk).
519 			 */
520 			RT_UNLOCK(rt);
521 			RADIX_NODE_HEAD_LOCK(rnh);
522 			RT_LOCK(rt);
523 			rt_setgate(rt, rt_key(rt), gateway);
524 			gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
525 			RADIX_NODE_HEAD_UNLOCK(rnh);
526 			EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
527 			RTFREE_LOCKED(gwrt);
528 		}
529 	} else
530 		error = EHOSTUNREACH;
531 done:
532 	if (rt)
533 		RTFREE_LOCKED(rt);
534 out:
535 	if (error)
536 		V_rtstat.rts_badredirect++;
537 	else if (stat != NULL)
538 		(*stat)++;
539 	bzero((caddr_t)&info, sizeof(info));
540 	info.rti_info[RTAX_DST] = dst;
541 	info.rti_info[RTAX_GATEWAY] = gateway;
542 	info.rti_info[RTAX_NETMASK] = netmask;
543 	info.rti_info[RTAX_AUTHOR] = src;
544 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
545 }
546 
547 int
548 rtioctl(u_long req, caddr_t data)
549 {
550 	return (rtioctl_fib(req, data, 0));
551 }
552 
553 /*
554  * Routing table ioctl interface.
555  */
556 int
557 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
558 {
559 
560 	/*
561 	 * If more ioctl commands are added here, make sure the proper
562 	 * super-user checks are being performed because it is possible for
563 	 * prison-root to make it this far if raw sockets have been enabled
564 	 * in jails.
565 	 */
566 #ifdef INET
567 	/* Multicast goop, grrr... */
568 	return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
569 #else /* INET */
570 	return ENXIO;
571 #endif /* INET */
572 }
573 
574 struct ifaddr *
575 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
576 {
577 	return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
578 }
579 
580 struct ifaddr *
581 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
582 				u_int fibnum)
583 {
584 	register struct ifaddr *ifa;
585 	int not_found = 0;
586 
587 	if ((flags & RTF_GATEWAY) == 0) {
588 		/*
589 		 * If we are adding a route to an interface,
590 		 * and the interface is a pt to pt link
591 		 * we should search for the destination
592 		 * as our clue to the interface.  Otherwise
593 		 * we can use the local address.
594 		 */
595 		ifa = NULL;
596 		if (flags & RTF_HOST)
597 			ifa = ifa_ifwithdstaddr(dst);
598 		if (ifa == NULL)
599 			ifa = ifa_ifwithaddr(gateway);
600 	} else {
601 		/*
602 		 * If we are adding a route to a remote net
603 		 * or host, the gateway may still be on the
604 		 * other end of a pt to pt link.
605 		 */
606 		ifa = ifa_ifwithdstaddr(gateway);
607 	}
608 	if (ifa == NULL)
609 		ifa = ifa_ifwithnet(gateway);
610 	if (ifa == NULL) {
611 		struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
612 		if (rt == NULL)
613 			return (NULL);
614 		/*
615 		 * dismiss a gateway that is reachable only
616 		 * through the default router
617 		 */
618 		switch (gateway->sa_family) {
619 		case AF_INET:
620 			if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
621 				not_found = 1;
622 			break;
623 		case AF_INET6:
624 			if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
625 				not_found = 1;
626 			break;
627 		default:
628 			break;
629 		}
630 		RT_REMREF(rt);
631 		RT_UNLOCK(rt);
632 		if (not_found)
633 			return (NULL);
634 		if ((ifa = rt->rt_ifa) == NULL)
635 			return (NULL);
636 	}
637 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
638 		struct ifaddr *oifa = ifa;
639 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
640 		if (ifa == NULL)
641 			ifa = oifa;
642 	}
643 	return (ifa);
644 }
645 
646 /*
647  * Do appropriate manipulations of a routing tree given
648  * all the bits of info needed
649  */
650 int
651 rtrequest(int req,
652 	struct sockaddr *dst,
653 	struct sockaddr *gateway,
654 	struct sockaddr *netmask,
655 	int flags,
656 	struct rtentry **ret_nrt)
657 {
658 	return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
659 }
660 
661 int
662 rtrequest_fib(int req,
663 	struct sockaddr *dst,
664 	struct sockaddr *gateway,
665 	struct sockaddr *netmask,
666 	int flags,
667 	struct rtentry **ret_nrt,
668 	u_int fibnum)
669 {
670 	struct rt_addrinfo info;
671 
672 	if (dst->sa_len == 0)
673 		return(EINVAL);
674 
675 	bzero((caddr_t)&info, sizeof(info));
676 	info.rti_flags = flags;
677 	info.rti_info[RTAX_DST] = dst;
678 	info.rti_info[RTAX_GATEWAY] = gateway;
679 	info.rti_info[RTAX_NETMASK] = netmask;
680 	return rtrequest1_fib(req, &info, ret_nrt, fibnum);
681 }
682 
683 /*
684  * These (questionable) definitions of apparent local variables apply
685  * to the next two functions.  XXXXXX!!!
686  */
687 #define	dst	info->rti_info[RTAX_DST]
688 #define	gateway	info->rti_info[RTAX_GATEWAY]
689 #define	netmask	info->rti_info[RTAX_NETMASK]
690 #define	ifaaddr	info->rti_info[RTAX_IFA]
691 #define	ifpaddr	info->rti_info[RTAX_IFP]
692 #define	flags	info->rti_flags
693 
694 int
695 rt_getifa(struct rt_addrinfo *info)
696 {
697 	return (rt_getifa_fib(info, 0));
698 }
699 
700 int
701 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
702 {
703 	struct ifaddr *ifa;
704 	int error = 0;
705 
706 	/*
707 	 * ifp may be specified by sockaddr_dl
708 	 * when protocol address is ambiguous.
709 	 */
710 	if (info->rti_ifp == NULL && ifpaddr != NULL &&
711 	    ifpaddr->sa_family == AF_LINK &&
712 	    (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
713 		info->rti_ifp = ifa->ifa_ifp;
714 	if (info->rti_ifa == NULL && ifaaddr != NULL)
715 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
716 	if (info->rti_ifa == NULL) {
717 		struct sockaddr *sa;
718 
719 		sa = ifaaddr != NULL ? ifaaddr :
720 		    (gateway != NULL ? gateway : dst);
721 		if (sa != NULL && info->rti_ifp != NULL)
722 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
723 		else if (dst != NULL && gateway != NULL)
724 			info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
725 							fibnum);
726 		else if (sa != NULL)
727 			info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
728 							fibnum);
729 	}
730 	if ((ifa = info->rti_ifa) != NULL) {
731 		if (info->rti_ifp == NULL)
732 			info->rti_ifp = ifa->ifa_ifp;
733 	} else
734 		error = ENETUNREACH;
735 	return (error);
736 }
737 
738 /*
739  * Expunges references to a route that's about to be reclaimed.
740  * The route must be locked.
741  */
742 int
743 rtexpunge(struct rtentry *rt)
744 {
745 	INIT_VNET_NET(curvnet);
746 	struct radix_node *rn;
747 	struct radix_node_head *rnh;
748 	struct ifaddr *ifa;
749 	int error = 0;
750 
751 	/*
752 	 * Find the correct routing tree to use for this Address Family
753 	 */
754 	rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
755 	RT_LOCK_ASSERT(rt);
756 	if (rnh == NULL)
757 		return (EAFNOSUPPORT);
758 	RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
759 #if 0
760 	/*
761 	 * We cannot assume anything about the reference count
762 	 * because protocols call us in many situations; often
763 	 * before unwinding references to the table entry.
764 	 */
765 	KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
766 #endif
767 	/*
768 	 * Remove the item from the tree; it should be there,
769 	 * but when callers invoke us blindly it may not (sigh).
770 	 */
771 	rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
772 	if (rn == NULL) {
773 		error = ESRCH;
774 		goto bad;
775 	}
776 	KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
777 		("unexpected flags 0x%x", rn->rn_flags));
778 	KASSERT(rt == RNTORT(rn),
779 		("lookup mismatch, rt %p rn %p", rt, rn));
780 
781 	rt->rt_flags &= ~RTF_UP;
782 
783 	/*
784 	 * Give the protocol a chance to keep things in sync.
785 	 */
786 	if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
787 		struct rt_addrinfo info;
788 
789 		bzero((caddr_t)&info, sizeof(info));
790 		info.rti_flags = rt->rt_flags;
791 		info.rti_info[RTAX_DST] = rt_key(rt);
792 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
793 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
794 		ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
795 	}
796 
797 	/*
798 	 * one more rtentry floating around that is not
799 	 * linked to the routing table.
800 	 */
801 	V_rttrash++;
802 bad:
803 	return (error);
804 }
805 
806 int
807 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
808 				u_int fibnum)
809 {
810 	INIT_VNET_NET(curvnet);
811 	int error = 0, needlock = 0;
812 	register struct rtentry *rt;
813 	register struct radix_node *rn;
814 	register struct radix_node_head *rnh;
815 	struct ifaddr *ifa;
816 	struct sockaddr *ndst;
817 #define senderr(x) { error = x ; goto bad; }
818 
819 	KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
820 	if (dst->sa_family != AF_INET)	/* Only INET supports > 1 fib now */
821 		fibnum = 0;
822 	/*
823 	 * Find the correct routing tree to use for this Address Family
824 	 */
825 	rnh = V_rt_tables[fibnum][dst->sa_family];
826 	if (rnh == NULL)
827 		return (EAFNOSUPPORT);
828 	needlock = ((flags & RTF_RNH_LOCKED) == 0);
829 	flags &= ~RTF_RNH_LOCKED;
830 	if (needlock)
831 		RADIX_NODE_HEAD_LOCK(rnh);
832 	else
833 		RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
834 	/*
835 	 * If we are adding a host route then we don't want to put
836 	 * a netmask in the tree, nor do we want to clone it.
837 	 */
838 	if (flags & RTF_HOST)
839 		netmask = NULL;
840 
841 	switch (req) {
842 	case RTM_DELETE:
843 #ifdef RADIX_MPATH
844 		/*
845 		 * if we got multipath routes, we require users to specify
846 		 * a matching RTAX_GATEWAY.
847 		 */
848 		if (rn_mpath_capable(rnh)) {
849 			struct rtentry *rto = NULL;
850 
851 			rn = rnh->rnh_matchaddr(dst, rnh);
852 			if (rn == NULL)
853 				senderr(ESRCH);
854  			rto = rt = RNTORT(rn);
855 			rt = rt_mpath_matchgate(rt, gateway);
856 			if (!rt)
857 				senderr(ESRCH);
858 			/*
859 			 * this is the first entry in the chain
860 			 */
861 			if (rto == rt) {
862 				rn = rn_mpath_next((struct radix_node *)rt);
863 				/*
864 				 * there is another entry, now it's active
865 				 */
866 				if (rn) {
867 					rto = RNTORT(rn);
868 					RT_LOCK(rto);
869 					rto->rt_flags |= RTF_UP;
870 					RT_UNLOCK(rto);
871 				} else if (rt->rt_flags & RTF_GATEWAY) {
872 					/*
873 					 * For gateway routes, we need to
874 					 * make sure that we we are deleting
875 					 * the correct gateway.
876 					 * rt_mpath_matchgate() does not
877 					 * check the case when there is only
878 					 * one route in the chain.
879 					 */
880 					if (gateway &&
881 					    (rt->rt_gateway->sa_len != gateway->sa_len ||
882 					    memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
883 						senderr(ESRCH);
884 				}
885 				/*
886 				 * use the normal delete code to remove
887 				 * the first entry
888 				 */
889 				goto normal_rtdel;
890 			}
891 			/*
892 			 * if the entry is 2nd and on up
893 			 */
894 			if (!rt_mpath_deldup(rto, rt))
895 				panic ("rtrequest1: rt_mpath_deldup");
896 			RT_LOCK(rt);
897 			RT_ADDREF(rt);
898 			rt->rt_flags &= ~RTF_UP;
899 			goto deldone;  /* done with the RTM_DELETE command */
900 		}
901 
902 normal_rtdel:
903 #endif
904 		/*
905 		 * Remove the item from the tree and return it.
906 		 * Complain if it is not there and do no more processing.
907 		 */
908 		rn = rnh->rnh_deladdr(dst, netmask, rnh);
909 		if (rn == NULL)
910 			senderr(ESRCH);
911 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
912 			panic ("rtrequest delete");
913 		rt = RNTORT(rn);
914 		RT_LOCK(rt);
915 		RT_ADDREF(rt);
916 		rt->rt_flags &= ~RTF_UP;
917 
918 		/*
919 		 * give the protocol a chance to keep things in sync.
920 		 */
921 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
922 			ifa->ifa_rtrequest(RTM_DELETE, rt, info);
923 
924 #ifdef RADIX_MPATH
925 deldone:
926 #endif
927 		/*
928 		 * One more rtentry floating around that is not
929 		 * linked to the routing table. rttrash will be decremented
930 		 * when RTFREE(rt) is eventually called.
931 		 */
932 		V_rttrash++;
933 
934 		/*
935 		 * If the caller wants it, then it can have it,
936 		 * but it's up to it to free the rtentry as we won't be
937 		 * doing it.
938 		 */
939 		if (ret_nrt) {
940 			*ret_nrt = rt;
941 			RT_UNLOCK(rt);
942 		} else
943 			RTFREE_LOCKED(rt);
944 		break;
945 	case RTM_RESOLVE:
946 		/*
947 		 * resolve was only used for route cloning
948 		 * here for compat
949 		 */
950 		break;
951 	case RTM_ADD:
952 		if ((flags & RTF_GATEWAY) && !gateway)
953 			senderr(EINVAL);
954 		if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
955 		    (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
956 			senderr(EINVAL);
957 
958 		if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum)))
959 			senderr(error);
960 		ifa = info->rti_ifa;
961 		rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
962 		if (rt == NULL)
963 			senderr(ENOBUFS);
964 		RT_LOCK_INIT(rt);
965 		rt->rt_flags = RTF_UP | flags;
966 		rt->rt_fibnum = fibnum;
967 		/*
968 		 * Add the gateway. Possibly re-malloc-ing the storage for it
969 		 *
970 		 */
971 		RT_LOCK(rt);
972 		if ((error = rt_setgate(rt, dst, gateway)) != 0) {
973 			RT_LOCK_DESTROY(rt);
974 			uma_zfree(rtzone, rt);
975 			senderr(error);
976 		}
977 
978 		/*
979 		 * point to the (possibly newly malloc'd) dest address.
980 		 */
981 		ndst = (struct sockaddr *)rt_key(rt);
982 
983 		/*
984 		 * make sure it contains the value we want (masked if needed).
985 		 */
986 		if (netmask) {
987 			rt_maskedcopy(dst, ndst, netmask);
988 		} else
989 			bcopy(dst, ndst, dst->sa_len);
990 
991 		/*
992 		 * Note that we now have a reference to the ifa.
993 		 * This moved from below so that rnh->rnh_addaddr() can
994 		 * examine the ifa and  ifa->ifa_ifp if it so desires.
995 		 */
996 		IFAREF(ifa);
997 		rt->rt_ifa = ifa;
998 		rt->rt_ifp = ifa->ifa_ifp;
999 
1000 #ifdef RADIX_MPATH
1001 		/* do not permit exactly the same dst/mask/gw pair */
1002 		if (rn_mpath_capable(rnh) &&
1003 			rt_mpath_conflict(rnh, rt, netmask)) {
1004 			if (rt->rt_ifa) {
1005 				IFAFREE(rt->rt_ifa);
1006 			}
1007 			Free(rt_key(rt));
1008 			RT_LOCK_DESTROY(rt);
1009 			uma_zfree(rtzone, rt);
1010 			senderr(EEXIST);
1011 		}
1012 #endif
1013 
1014 		/* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1015 		rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1016 		/*
1017 		 * If it still failed to go into the tree,
1018 		 * then un-make it (this should be a function)
1019 		 */
1020 		if (rn == NULL) {
1021 			if (rt->rt_ifa)
1022 				IFAFREE(rt->rt_ifa);
1023 			Free(rt_key(rt));
1024 			RT_LOCK_DESTROY(rt);
1025 			uma_zfree(rtzone, rt);
1026 			senderr(EEXIST);
1027 		}
1028 
1029 		/*
1030 		 * If this protocol has something to add to this then
1031 		 * allow it to do that as well.
1032 		 */
1033 		if (ifa->ifa_rtrequest)
1034 			ifa->ifa_rtrequest(req, rt, info);
1035 
1036 		/*
1037 		 * actually return a resultant rtentry and
1038 		 * give the caller a single reference.
1039 		 */
1040 		if (ret_nrt) {
1041 			*ret_nrt = rt;
1042 			RT_ADDREF(rt);
1043 		}
1044 		RT_UNLOCK(rt);
1045 		break;
1046 	default:
1047 		error = EOPNOTSUPP;
1048 	}
1049 bad:
1050 	if (needlock)
1051 		RADIX_NODE_HEAD_UNLOCK(rnh);
1052 	return (error);
1053 #undef senderr
1054 }
1055 
1056 #undef dst
1057 #undef gateway
1058 #undef netmask
1059 #undef ifaaddr
1060 #undef ifpaddr
1061 #undef flags
1062 
1063 int
1064 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1065 {
1066 	INIT_VNET_NET(curvnet);
1067 	/* XXX dst may be overwritten, can we move this to below */
1068 	int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1069 #ifdef INVARIANTS
1070 	struct radix_node_head *rnh =
1071 	    V_rt_tables[rt->rt_fibnum][dst->sa_family];
1072 #endif
1073 
1074 	RT_LOCK_ASSERT(rt);
1075 	RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1076 
1077 	/*
1078 	 * Prepare to store the gateway in rt->rt_gateway.
1079 	 * Both dst and gateway are stored one after the other in the same
1080 	 * malloc'd chunk. If we have room, we can reuse the old buffer,
1081 	 * rt_gateway already points to the right place.
1082 	 * Otherwise, malloc a new block and update the 'dst' address.
1083 	 */
1084 	if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1085 		caddr_t new;
1086 
1087 		R_Malloc(new, caddr_t, dlen + glen);
1088 		if (new == NULL)
1089 			return ENOBUFS;
1090 		/*
1091 		 * XXX note, we copy from *dst and not *rt_key(rt) because
1092 		 * rt_setgate() can be called to initialize a newly
1093 		 * allocated route entry, in which case rt_key(rt) == NULL
1094 		 * (and also rt->rt_gateway == NULL).
1095 		 * Free()/free() handle a NULL argument just fine.
1096 		 */
1097 		bcopy(dst, new, dlen);
1098 		Free(rt_key(rt));	/* free old block, if any */
1099 		rt_key(rt) = (struct sockaddr *)new;
1100 		rt->rt_gateway = (struct sockaddr *)(new + dlen);
1101 	}
1102 
1103 	/*
1104 	 * Copy the new gateway value into the memory chunk.
1105 	 */
1106 	bcopy(gate, rt->rt_gateway, glen);
1107 
1108 	return (0);
1109 }
1110 
1111 static void
1112 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1113 {
1114 	register u_char *cp1 = (u_char *)src;
1115 	register u_char *cp2 = (u_char *)dst;
1116 	register u_char *cp3 = (u_char *)netmask;
1117 	u_char *cplim = cp2 + *cp3;
1118 	u_char *cplim2 = cp2 + *cp1;
1119 
1120 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1121 	cp3 += 2;
1122 	if (cplim > cplim2)
1123 		cplim = cplim2;
1124 	while (cp2 < cplim)
1125 		*cp2++ = *cp1++ & *cp3++;
1126 	if (cp2 < cplim2)
1127 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1128 }
1129 
1130 /*
1131  * Set up a routing table entry, normally
1132  * for an interface.
1133  */
1134 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1135 static inline  int
1136 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1137 {
1138 	INIT_VNET_NET(curvnet);
1139 	struct sockaddr *dst;
1140 	struct sockaddr *netmask;
1141 	struct rtentry *rt = NULL;
1142 	struct rt_addrinfo info;
1143 	int error = 0;
1144 	int startfib, endfib;
1145 	char tempbuf[_SOCKADDR_TMPSIZE];
1146 	int didwork = 0;
1147 	int a_failure = 0;
1148 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1149 
1150 	if (flags & RTF_HOST) {
1151 		dst = ifa->ifa_dstaddr;
1152 		netmask = NULL;
1153 	} else {
1154 		dst = ifa->ifa_addr;
1155 		netmask = ifa->ifa_netmask;
1156 	}
1157 	if ( dst->sa_family != AF_INET)
1158 		fibnum = 0;
1159 	if (fibnum == -1) {
1160 		if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1161 			startfib = endfib = curthread->td_proc->p_fibnum;
1162 		} else {
1163 			startfib = 0;
1164 			endfib = rt_numfibs - 1;
1165 		}
1166 	} else {
1167 		KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1168 		startfib = fibnum;
1169 		endfib = fibnum;
1170 	}
1171 	if (dst->sa_len == 0)
1172 		return(EINVAL);
1173 
1174 	/*
1175 	 * If it's a delete, check that if it exists,
1176 	 * it's on the correct interface or we might scrub
1177 	 * a route to another ifa which would
1178 	 * be confusing at best and possibly worse.
1179 	 */
1180 	if (cmd == RTM_DELETE) {
1181 		/*
1182 		 * It's a delete, so it should already exist..
1183 		 * If it's a net, mask off the host bits
1184 		 * (Assuming we have a mask)
1185 		 * XXX this is kinda inet specific..
1186 		 */
1187 		if (netmask != NULL) {
1188 			rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1189 			dst = (struct sockaddr *)tempbuf;
1190 		}
1191 	}
1192 	/*
1193 	 * Now go through all the requested tables (fibs) and do the
1194 	 * requested action. Realistically, this will either be fib 0
1195 	 * for protocols that don't do multiple tables or all the
1196 	 * tables for those that do. XXX For this version only AF_INET.
1197 	 * When that changes code should be refactored to protocol
1198 	 * independent parts and protocol dependent parts.
1199 	 */
1200 	for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1201 		if (cmd == RTM_DELETE) {
1202 			struct radix_node_head *rnh;
1203 			struct radix_node *rn;
1204 			/*
1205 			 * Look up an rtentry that is in the routing tree and
1206 			 * contains the correct info.
1207 			 */
1208 			if ((rnh = V_rt_tables[fibnum][dst->sa_family]) == NULL)
1209 				/* this table doesn't exist but others might */
1210 				continue;
1211 			RADIX_NODE_HEAD_LOCK(rnh);
1212 #ifdef RADIX_MPATH
1213 			if (rn_mpath_capable(rnh)) {
1214 
1215 				rn = rnh->rnh_matchaddr(dst, rnh);
1216 				if (rn == NULL)
1217 					error = ESRCH;
1218 				else {
1219 					rt = RNTORT(rn);
1220 					/*
1221 					 * for interface route the
1222 					 * rt->rt_gateway is sockaddr_intf
1223 					 * for cloning ARP entries, so
1224 					 * rt_mpath_matchgate must use the
1225 					 * interface address
1226 					 */
1227 					rt = rt_mpath_matchgate(rt,
1228 					    ifa->ifa_addr);
1229 					if (!rt)
1230 						error = ESRCH;
1231 				}
1232 			}
1233 			else
1234 #endif
1235 			rn = rnh->rnh_lookup(dst, netmask, rnh);
1236 			error = (rn == NULL ||
1237 			    (rn->rn_flags & RNF_ROOT) ||
1238 			    RNTORT(rn)->rt_ifa != ifa ||
1239 			    !sa_equal((struct sockaddr *)rn->rn_key, dst));
1240 			RADIX_NODE_HEAD_UNLOCK(rnh);
1241 			if (error) {
1242 				/* this is only an error if bad on ALL tables */
1243 				continue;
1244 			}
1245 		}
1246 		/*
1247 		 * Do the actual request
1248 		 */
1249 		bzero((caddr_t)&info, sizeof(info));
1250 		info.rti_ifa = ifa;
1251 		info.rti_flags = flags | ifa->ifa_flags;
1252 		info.rti_info[RTAX_DST] = dst;
1253 		/*
1254 		 * doing this for compatibility reasons
1255 		 */
1256 		if (cmd == RTM_ADD)
1257 			info.rti_info[RTAX_GATEWAY] =
1258 			    (struct sockaddr *)&null_sdl;
1259 		else
1260 			info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1261 		info.rti_info[RTAX_NETMASK] = netmask;
1262 		error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1263 		if (error == 0 && rt != NULL) {
1264 			/*
1265 			 * notify any listening routing agents of the change
1266 			 */
1267 			RT_LOCK(rt);
1268 #ifdef RADIX_MPATH
1269 			/*
1270 			 * in case address alias finds the first address
1271 			 * e.g. ifconfig bge0 192.103.54.246/24
1272 			 * e.g. ifconfig bge0 192.103.54.247/24
1273 			 * the address set in the route is 192.103.54.246
1274 			 * so we need to replace it with 192.103.54.247
1275 			 */
1276 			if (memcmp(rt->rt_ifa->ifa_addr,
1277 			    ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1278 				IFAFREE(rt->rt_ifa);
1279 				IFAREF(ifa);
1280 				rt->rt_ifp = ifa->ifa_ifp;
1281 				rt->rt_ifa = ifa;
1282 			}
1283 #endif
1284 			/*
1285 			 * doing this for compatibility reasons
1286 			 */
1287 			if (cmd == RTM_ADD) {
1288 			    ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
1289 				rt->rt_ifp->if_type;
1290 			    ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1291 				rt->rt_ifp->if_index;
1292 			}
1293 			rt_newaddrmsg(cmd, ifa, error, rt);
1294 			if (cmd == RTM_DELETE) {
1295 				/*
1296 				 * If we are deleting, and we found an entry,
1297 				 * then it's been removed from the tree..
1298 				 * now throw it away.
1299 				 */
1300 				RTFREE_LOCKED(rt);
1301 			} else {
1302 				if (cmd == RTM_ADD) {
1303 					/*
1304 					 * We just wanted to add it..
1305 					 * we don't actually need a reference.
1306 					 */
1307 					RT_REMREF(rt);
1308 				}
1309 				RT_UNLOCK(rt);
1310 			}
1311 			didwork = 1;
1312 		}
1313 		if (error)
1314 			a_failure = error;
1315 	}
1316 	if (cmd == RTM_DELETE) {
1317 		if (didwork) {
1318 			error = 0;
1319 		} else {
1320 			/* we only give an error if it wasn't in any table */
1321 			error = ((flags & RTF_HOST) ?
1322 			    EHOSTUNREACH : ENETUNREACH);
1323 		}
1324 	} else {
1325 		if (a_failure) {
1326 			/* return an error if any of them failed */
1327 			error = a_failure;
1328 		}
1329 	}
1330 	return (error);
1331 }
1332 
1333 /* special one for inet internal use. may not use. */
1334 int
1335 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1336 {
1337 	return (rtinit1(ifa, cmd, flags, -1));
1338 }
1339 
1340 /*
1341  * Set up a routing table entry, normally
1342  * for an interface.
1343  */
1344 int
1345 rtinit(struct ifaddr *ifa, int cmd, int flags)
1346 {
1347 	struct sockaddr *dst;
1348 	int fib = 0;
1349 
1350 	if (flags & RTF_HOST) {
1351 		dst = ifa->ifa_dstaddr;
1352 	} else {
1353 		dst = ifa->ifa_addr;
1354 	}
1355 
1356 	if (dst->sa_family == AF_INET)
1357 		fib = -1;
1358 	return (rtinit1(ifa, cmd, flags, fib));
1359 }
1360 
1361 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1362 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
1363