xref: /freebsd/sys/net/route.c (revision c398230b64aea809cb7c5cea8db580af7097920c)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1980, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
2942e9e16dSRuslan Ermilov  *	@(#)route.c	8.3.1.1 (Berkeley) 2/23/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
331d5e9e22SEivind Eklund #include "opt_inet.h"
344bd49128SPeter Wemm #include "opt_mrouting.h"
354bd49128SPeter Wemm 
36df8bae1dSRodney W. Grimes #include <sys/param.h>
37df8bae1dSRodney W. Grimes #include <sys/systm.h>
384d1d4912SBruce Evans #include <sys/malloc.h>
39df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
40df8bae1dSRodney W. Grimes #include <sys/socket.h>
41df8bae1dSRodney W. Grimes #include <sys/domain.h>
42cb64988fSLuoqi Chen #include <sys/kernel.h>
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes #include <net/if.h>
45df8bae1dSRodney W. Grimes #include <net/route.h>
46df8bae1dSRodney W. Grimes 
47df8bae1dSRodney W. Grimes #include <netinet/in.h>
48b5e8ce9fSBruce Evans #include <netinet/ip_mroute.h>
49df8bae1dSRodney W. Grimes 
502dc1d581SAndre Oppermann #include <vm/uma.h>
512dc1d581SAndre Oppermann 
52f708ef1bSPoul-Henning Kamp static struct rtstat rtstat;
5328f8db14SBruce Evans struct radix_node_head *rt_tables[AF_MAX+1];
5428f8db14SBruce Evans 
55f708ef1bSPoul-Henning Kamp static int	rttrash;		/* routes not in table but not freed */
56df8bae1dSRodney W. Grimes 
57929ddbbbSAlfred Perlstein static void rt_maskedcopy(struct sockaddr *,
58929ddbbbSAlfred Perlstein 	    struct sockaddr *, struct sockaddr *);
59929ddbbbSAlfred Perlstein static void rtable_init(void **);
60f708ef1bSPoul-Henning Kamp 
61d6941ce9SLuigi Rizzo /* compare two sockaddr structures */
62d6941ce9SLuigi Rizzo #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
63d6941ce9SLuigi Rizzo 
64d6941ce9SLuigi Rizzo /*
65d6941ce9SLuigi Rizzo  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
66d6941ce9SLuigi Rizzo  * The operation can be done safely (in this code) because a
67d6941ce9SLuigi Rizzo  * 'struct rtentry' starts with two 'struct radix_node''s, the first
68d6941ce9SLuigi Rizzo  * one representing leaf nodes in the routing tree, which is
69d6941ce9SLuigi Rizzo  * what the code in radix.c passes us as a 'struct radix_node'.
70d6941ce9SLuigi Rizzo  *
71d6941ce9SLuigi Rizzo  * But because there are a lot of assumptions in this conversion,
72d6941ce9SLuigi Rizzo  * do not cast explicitly, but always use the macro below.
73d6941ce9SLuigi Rizzo  */
74d6941ce9SLuigi Rizzo #define RNTORT(p)	((struct rtentry *)(p))
75d6941ce9SLuigi Rizzo 
76f708ef1bSPoul-Henning Kamp static void
77d1dd20beSSam Leffler rtable_init(void **table)
78df8bae1dSRodney W. Grimes {
79df8bae1dSRodney W. Grimes 	struct domain *dom;
80df8bae1dSRodney W. Grimes 	for (dom = domains; dom; dom = dom->dom_next)
81df8bae1dSRodney W. Grimes 		if (dom->dom_rtattach)
82df8bae1dSRodney W. Grimes 			dom->dom_rtattach(&table[dom->dom_family],
83df8bae1dSRodney W. Grimes 			    dom->dom_rtoffset);
84df8bae1dSRodney W. Grimes }
85df8bae1dSRodney W. Grimes 
862dc1d581SAndre Oppermann static uma_zone_t rtzone;		/* Routing table UMA zone. */
872dc1d581SAndre Oppermann 
882eb5613fSLuigi Rizzo static void
892eb5613fSLuigi Rizzo route_init(void)
90df8bae1dSRodney W. Grimes {
912dc1d581SAndre Oppermann 	rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
922dc1d581SAndre Oppermann 	    NULL, NULL, UMA_ALIGN_PTR, 0);
93df8bae1dSRodney W. Grimes 	rn_init();	/* initialize all zeroes, all ones, mask table */
94df8bae1dSRodney W. Grimes 	rtable_init((void **)rt_tables);
95df8bae1dSRodney W. Grimes }
96df8bae1dSRodney W. Grimes 
97df8bae1dSRodney W. Grimes /*
98df8bae1dSRodney W. Grimes  * Packet routing routines.
99df8bae1dSRodney W. Grimes  */
100df8bae1dSRodney W. Grimes void
101d1dd20beSSam Leffler rtalloc(struct route *ro)
102df8bae1dSRodney W. Grimes {
10368f956b8SJohn Polstra 	rtalloc_ign(ro, 0UL);
104df8bae1dSRodney W. Grimes }
105df8bae1dSRodney W. Grimes 
106652082e6SGarrett Wollman void
107d1dd20beSSam Leffler rtalloc_ign(struct route *ro, u_long ignore)
108652082e6SGarrett Wollman {
10968f956b8SJohn Polstra 	struct rtentry *rt;
11068f956b8SJohn Polstra 
11168f956b8SJohn Polstra 	if ((rt = ro->ro_rt) != NULL) {
11268f956b8SJohn Polstra 		if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
11368f956b8SJohn Polstra 			return;
11468f956b8SJohn Polstra 		RTFREE(rt);
11566810dd0SYoshinobu Inoue 		ro->ro_rt = NULL;
11668f956b8SJohn Polstra 	}
117652082e6SGarrett Wollman 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
118d1dd20beSSam Leffler 	if (ro->ro_rt)
119d1dd20beSSam Leffler 		RT_UNLOCK(ro->ro_rt);
120652082e6SGarrett Wollman }
121652082e6SGarrett Wollman 
122b0a76b88SJulian Elischer /*
123b0a76b88SJulian Elischer  * Look up the route that matches the address given
124b0a76b88SJulian Elischer  * Or, at least try.. Create a cloned route if needed.
125d1dd20beSSam Leffler  *
126d1dd20beSSam Leffler  * The returned route, if any, is locked.
127b0a76b88SJulian Elischer  */
128df8bae1dSRodney W. Grimes struct rtentry *
129d1dd20beSSam Leffler rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
130df8bae1dSRodney W. Grimes {
131d1dd20beSSam Leffler 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
132d1dd20beSSam Leffler 	struct rtentry *rt;
133d1dd20beSSam Leffler 	struct radix_node *rn;
134d1dd20beSSam Leffler 	struct rtentry *newrt;
135df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
136995add1aSGarrett Wollman 	u_long nflags;
137d1dd20beSSam Leffler 	int err = 0, msgtype = RTM_MISS;
138df8bae1dSRodney W. Grimes 
13985911824SLuigi Rizzo 	newrt = NULL;
1403e6a836eSSam Leffler 	bzero(&info, sizeof(info));
141b0a76b88SJulian Elischer 	/*
142b0a76b88SJulian Elischer 	 * Look up the address in the table for that Address Family
143b0a76b88SJulian Elischer 	 */
144956b0b65SJeffrey Hsu 	if (rnh == NULL) {
145956b0b65SJeffrey Hsu 		rtstat.rts_unreach++;
146956b0b65SJeffrey Hsu 		goto miss2;
147956b0b65SJeffrey Hsu 	}
148956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
149d1dd20beSSam Leffler 	if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
150d1dd20beSSam Leffler 	    (rn->rn_flags & RNF_ROOT) == 0) {
151b0a76b88SJulian Elischer 		/*
152b0a76b88SJulian Elischer 		 * If we find it and it's not the root node, then
153b0a76b88SJulian Elischer 		 * get a refernce on the rtentry associated.
154b0a76b88SJulian Elischer 		 */
155d6941ce9SLuigi Rizzo 		newrt = rt = RNTORT(rn);
156995add1aSGarrett Wollman 		nflags = rt->rt_flags & ~ignflags;
15726d02ca7SAndre Oppermann 		if (report && (nflags & RTF_CLONING)) {
158b0a76b88SJulian Elischer 			/*
159b0a76b88SJulian Elischer 			 * We are apparently adding (report = 0 in delete).
160b0a76b88SJulian Elischer 			 * If it requires that it be cloned, do so.
161b0a76b88SJulian Elischer 			 * (This implies it wasn't a HOST route.)
162b0a76b88SJulian Elischer 			 */
16385911824SLuigi Rizzo 			err = rtrequest(RTM_RESOLVE, dst, NULL,
16485911824SLuigi Rizzo 					      NULL, 0, &newrt);
165df8bae1dSRodney W. Grimes 			if (err) {
166b0a76b88SJulian Elischer 				/*
167b0a76b88SJulian Elischer 				 * If the cloning didn't succeed, maybe
168b0a76b88SJulian Elischer 				 * what we have will do. Return that.
169b0a76b88SJulian Elischer 				 */
170d1dd20beSSam Leffler 				newrt = rt;		/* existing route */
171d1dd20beSSam Leffler 				RT_LOCK(newrt);
1727138d65cSSam Leffler 				RT_ADDREF(newrt);
173df8bae1dSRodney W. Grimes 				goto miss;
174df8bae1dSRodney W. Grimes 			}
175d1dd20beSSam Leffler 			KASSERT(newrt, ("no route and no error"));
176d1dd20beSSam Leffler 			RT_LOCK(newrt);
177d1dd20beSSam Leffler 			if (newrt->rt_flags & RTF_XRESOLVE) {
178b0a76b88SJulian Elischer 				/*
179b0a76b88SJulian Elischer 				 * If the new route specifies it be
180b0a76b88SJulian Elischer 				 * externally resolved, then go do that.
181b0a76b88SJulian Elischer 				 */
182df8bae1dSRodney W. Grimes 				msgtype = RTM_RESOLVE;
183df8bae1dSRodney W. Grimes 				goto miss;
184df8bae1dSRodney W. Grimes 			}
1858071913dSRuslan Ermilov 			/* Inform listeners of the new route. */
186d1dd20beSSam Leffler 			info.rti_info[RTAX_DST] = rt_key(newrt);
187d1dd20beSSam Leffler 			info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
188d1dd20beSSam Leffler 			info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
189d1dd20beSSam Leffler 			if (newrt->rt_ifp != NULL) {
1908071913dSRuslan Ermilov 				info.rti_info[RTAX_IFP] =
1919b98ee2cSLuigi Rizzo 				    ifaddr_byindex(newrt->rt_ifp->if_index)->ifa_addr;
192d1dd20beSSam Leffler 				info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
1938071913dSRuslan Ermilov 			}
194d1dd20beSSam Leffler 			rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
195d1dd20beSSam Leffler 		} else {
196d1dd20beSSam Leffler 			KASSERT(rt == newrt, ("locking wrong route"));
197d1dd20beSSam Leffler 			RT_LOCK(newrt);
1987138d65cSSam Leffler 			RT_ADDREF(newrt);
199d1dd20beSSam Leffler 		}
200956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
201df8bae1dSRodney W. Grimes 	} else {
202b0a76b88SJulian Elischer 		/*
203b0a76b88SJulian Elischer 		 * Either we hit the root or couldn't find any match,
204b0a76b88SJulian Elischer 		 * Which basically means
205b0a76b88SJulian Elischer 		 * "caint get there frm here"
206b0a76b88SJulian Elischer 		 */
207df8bae1dSRodney W. Grimes 		rtstat.rts_unreach++;
208956b0b65SJeffrey Hsu 	miss:
209956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
210956b0b65SJeffrey Hsu 	miss2:	if (report) {
211b0a76b88SJulian Elischer 			/*
212b0a76b88SJulian Elischer 			 * If required, report the failure to the supervising
213b0a76b88SJulian Elischer 			 * Authorities.
214b0a76b88SJulian Elischer 			 * For a delete, this is not an error. (report == 0)
215b0a76b88SJulian Elischer 			 */
216df8bae1dSRodney W. Grimes 			info.rti_info[RTAX_DST] = dst;
217df8bae1dSRodney W. Grimes 			rt_missmsg(msgtype, &info, 0, err);
218df8bae1dSRodney W. Grimes 		}
219df8bae1dSRodney W. Grimes 	}
220d1dd20beSSam Leffler 	if (newrt)
221d1dd20beSSam Leffler 		RT_LOCK_ASSERT(newrt);
222df8bae1dSRodney W. Grimes 	return (newrt);
223df8bae1dSRodney W. Grimes }
224df8bae1dSRodney W. Grimes 
225499676dfSJulian Elischer /*
226499676dfSJulian Elischer  * Remove a reference count from an rtentry.
227499676dfSJulian Elischer  * If the count gets low enough, take it out of the routing table
228499676dfSJulian Elischer  */
229df8bae1dSRodney W. Grimes void
230d1dd20beSSam Leffler rtfree(struct rtentry *rt)
231df8bae1dSRodney W. Grimes {
23285911824SLuigi Rizzo 	struct radix_node_head *rnh;
233df8bae1dSRodney W. Grimes 
23485911824SLuigi Rizzo 	/* XXX the NULL checks are probably useless */
23585911824SLuigi Rizzo 	if (rt == NULL)
23685911824SLuigi Rizzo 		panic("rtfree: NULL rt");
23785911824SLuigi Rizzo 	rnh = rt_tables[rt_key(rt)->sa_family];
23885911824SLuigi Rizzo 	if (rnh == NULL)
23985911824SLuigi Rizzo 		panic("rtfree: NULL rnh");
240499676dfSJulian Elischer 
241d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
242d1dd20beSSam Leffler 
243499676dfSJulian Elischer 	/*
244499676dfSJulian Elischer 	 * decrement the reference count by one and if it reaches 0,
245499676dfSJulian Elischer 	 * and there is a close function defined, call the close function
246499676dfSJulian Elischer 	 */
2477138d65cSSam Leffler 	RT_REMREF(rt);
2487138d65cSSam Leffler 	if (rt->rt_refcnt > 0)
249d1dd20beSSam Leffler 		goto done;
2509c63e9dbSSam Leffler 
2519c63e9dbSSam Leffler 	/*
2529c63e9dbSSam Leffler 	 * On last reference give the "close method" a chance
2539c63e9dbSSam Leffler 	 * to cleanup private state.  This also permits (for
2549c63e9dbSSam Leffler 	 * IPv4 and IPv6) a chance to decide if the routing table
2559c63e9dbSSam Leffler 	 * entry should be purged immediately or at a later time.
2569c63e9dbSSam Leffler 	 * When an immediate purge is to happen the close routine
2579c63e9dbSSam Leffler 	 * typically calls rtexpunge which clears the RTF_UP flag
2589c63e9dbSSam Leffler 	 * on the entry so that the code below reclaims the storage.
2599c63e9dbSSam Leffler 	 */
260d1dd20beSSam Leffler 	if (rt->rt_refcnt == 0 && rnh->rnh_close)
2615c2dae8eSGarrett Wollman 		rnh->rnh_close((struct radix_node *)rt, rnh);
262499676dfSJulian Elischer 
263499676dfSJulian Elischer 	/*
264499676dfSJulian Elischer 	 * If we are no longer "up" (and ref == 0)
265499676dfSJulian Elischer 	 * then we can free the resources associated
266499676dfSJulian Elischer 	 * with the route.
267499676dfSJulian Elischer 	 */
268d1dd20beSSam Leffler 	if ((rt->rt_flags & RTF_UP) == 0) {
269df8bae1dSRodney W. Grimes 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
270df8bae1dSRodney W. Grimes 			panic ("rtfree 2");
271499676dfSJulian Elischer 		/*
272499676dfSJulian Elischer 		 * the rtentry must have been removed from the routing table
273499676dfSJulian Elischer 		 * so it is represented in rttrash.. remove that now.
274499676dfSJulian Elischer 		 */
275df8bae1dSRodney W. Grimes 		rttrash--;
276499676dfSJulian Elischer #ifdef	DIAGNOSTIC
277df8bae1dSRodney W. Grimes 		if (rt->rt_refcnt < 0) {
278623ae52eSPoul-Henning Kamp 			printf("rtfree: %p not freed (neg refs)\n", rt);
279d1dd20beSSam Leffler 			goto done;
280df8bae1dSRodney W. Grimes 		}
281499676dfSJulian Elischer #endif
282499676dfSJulian Elischer 		/*
283499676dfSJulian Elischer 		 * release references on items we hold them on..
284499676dfSJulian Elischer 		 * e.g other routes and ifaddrs.
285499676dfSJulian Elischer 		 */
28619fc74fbSJeffrey Hsu 		if (rt->rt_ifa)
28719fc74fbSJeffrey Hsu 			IFAFREE(rt->rt_ifa);
288d1dd20beSSam Leffler 		rt->rt_parent = NULL;		/* NB: no refcnt on parent */
289499676dfSJulian Elischer 
290499676dfSJulian Elischer 		/*
291499676dfSJulian Elischer 		 * The key is separatly alloc'd so free it (see rt_setgate()).
292499676dfSJulian Elischer 		 * This also frees the gateway, as they are always malloc'd
293499676dfSJulian Elischer 		 * together.
294499676dfSJulian Elischer 		 */
295df8bae1dSRodney W. Grimes 		Free(rt_key(rt));
296499676dfSJulian Elischer 
297499676dfSJulian Elischer 		/*
298499676dfSJulian Elischer 		 * and the rtentry itself of course
299499676dfSJulian Elischer 		 */
300d1dd20beSSam Leffler 		RT_LOCK_DESTROY(rt);
3012dc1d581SAndre Oppermann 		uma_zfree(rtzone, rt);
302d1dd20beSSam Leffler 		return;
303df8bae1dSRodney W. Grimes 	}
304d1dd20beSSam Leffler done:
305d1dd20beSSam Leffler 	RT_UNLOCK(rt);
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes /*
310df8bae1dSRodney W. Grimes  * Force a routing table entry to the specified
311df8bae1dSRodney W. Grimes  * destination to go through the given gateway.
312df8bae1dSRodney W. Grimes  * Normally called as a result of a routing redirect
313df8bae1dSRodney W. Grimes  * message from the network layer.
314df8bae1dSRodney W. Grimes  */
31526f9a767SRodney W. Grimes void
316d1dd20beSSam Leffler rtredirect(struct sockaddr *dst,
317d1dd20beSSam Leffler 	struct sockaddr *gateway,
318d1dd20beSSam Leffler 	struct sockaddr *netmask,
319d1dd20beSSam Leffler 	int flags,
320d1dd20beSSam Leffler 	struct sockaddr *src)
321df8bae1dSRodney W. Grimes {
3228071913dSRuslan Ermilov 	struct rtentry *rt;
323df8bae1dSRodney W. Grimes 	int error = 0;
32485911824SLuigi Rizzo 	short *stat = NULL;
325df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
326df8bae1dSRodney W. Grimes 	struct ifaddr *ifa;
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 	/* verify the gateway is directly reachable */
32985911824SLuigi Rizzo 	if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
330df8bae1dSRodney W. Grimes 		error = ENETUNREACH;
331df8bae1dSRodney W. Grimes 		goto out;
332df8bae1dSRodney W. Grimes 	}
333d1dd20beSSam Leffler 	rt = rtalloc1(dst, 0, 0UL);	/* NB: rt is locked */
334df8bae1dSRodney W. Grimes 	/*
335df8bae1dSRodney W. Grimes 	 * If the redirect isn't from our current router for this dst,
336df8bae1dSRodney W. Grimes 	 * it's either old or wrong.  If it redirects us to ourselves,
337df8bae1dSRodney W. Grimes 	 * we have a routing loop, perhaps as a result of an interface
338df8bae1dSRodney W. Grimes 	 * going down recently.
339df8bae1dSRodney W. Grimes 	 */
340df8bae1dSRodney W. Grimes 	if (!(flags & RTF_DONE) && rt &&
341956b0b65SJeffrey Hsu 	     (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
342df8bae1dSRodney W. Grimes 		error = EINVAL;
343df8bae1dSRodney W. Grimes 	else if (ifa_ifwithaddr(gateway))
344df8bae1dSRodney W. Grimes 		error = EHOSTUNREACH;
345df8bae1dSRodney W. Grimes 	if (error)
346df8bae1dSRodney W. Grimes 		goto done;
347df8bae1dSRodney W. Grimes 	/*
348df8bae1dSRodney W. Grimes 	 * Create a new entry if we just got back a wildcard entry
349df8bae1dSRodney W. Grimes 	 * or the the lookup failed.  This is necessary for hosts
350df8bae1dSRodney W. Grimes 	 * which use routing redirects generated by smart gateways
351df8bae1dSRodney W. Grimes 	 * to dynamically build the routing tables.
352df8bae1dSRodney W. Grimes 	 */
35385911824SLuigi Rizzo 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
354df8bae1dSRodney W. Grimes 		goto create;
355df8bae1dSRodney W. Grimes 	/*
356df8bae1dSRodney W. Grimes 	 * Don't listen to the redirect if it's
357df8bae1dSRodney W. Grimes 	 * for a route to an interface.
358df8bae1dSRodney W. Grimes 	 */
359df8bae1dSRodney W. Grimes 	if (rt->rt_flags & RTF_GATEWAY) {
360df8bae1dSRodney W. Grimes 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
361df8bae1dSRodney W. Grimes 			/*
362df8bae1dSRodney W. Grimes 			 * Changing from route to net => route to host.
363df8bae1dSRodney W. Grimes 			 * Create new route, rather than smashing route to net.
364df8bae1dSRodney W. Grimes 			 */
365df8bae1dSRodney W. Grimes 		create:
3668071913dSRuslan Ermilov 			if (rt)
3678071913dSRuslan Ermilov 				rtfree(rt);
368df8bae1dSRodney W. Grimes 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
3698071913dSRuslan Ermilov 			bzero((caddr_t)&info, sizeof(info));
3708071913dSRuslan Ermilov 			info.rti_info[RTAX_DST] = dst;
3718071913dSRuslan Ermilov 			info.rti_info[RTAX_GATEWAY] = gateway;
3728071913dSRuslan Ermilov 			info.rti_info[RTAX_NETMASK] = netmask;
3738071913dSRuslan Ermilov 			info.rti_ifa = ifa;
3748071913dSRuslan Ermilov 			info.rti_flags = flags;
3758071913dSRuslan Ermilov 			rt = NULL;
3768071913dSRuslan Ermilov 			error = rtrequest1(RTM_ADD, &info, &rt);
377d1dd20beSSam Leffler 			if (rt != NULL) {
3784de5d90cSSam Leffler 				RT_LOCK(rt);
3798071913dSRuslan Ermilov 				flags = rt->rt_flags;
380d1dd20beSSam Leffler 			}
381df8bae1dSRodney W. Grimes 			stat = &rtstat.rts_dynamic;
382df8bae1dSRodney W. Grimes 		} else {
383df8bae1dSRodney W. Grimes 			/*
384df8bae1dSRodney W. Grimes 			 * Smash the current notion of the gateway to
385df8bae1dSRodney W. Grimes 			 * this destination.  Should check about netmask!!!
386df8bae1dSRodney W. Grimes 			 */
387df8bae1dSRodney W. Grimes 			rt->rt_flags |= RTF_MODIFIED;
388df8bae1dSRodney W. Grimes 			flags |= RTF_MODIFIED;
389df8bae1dSRodney W. Grimes 			stat = &rtstat.rts_newgateway;
390499676dfSJulian Elischer 			/*
391499676dfSJulian Elischer 			 * add the key and gateway (in one malloc'd chunk).
392499676dfSJulian Elischer 			 */
393df8bae1dSRodney W. Grimes 			rt_setgate(rt, rt_key(rt), gateway);
394df8bae1dSRodney W. Grimes 		}
395df8bae1dSRodney W. Grimes 	} else
396df8bae1dSRodney W. Grimes 		error = EHOSTUNREACH;
397df8bae1dSRodney W. Grimes done:
398d1dd20beSSam Leffler 	if (rt)
399df8bae1dSRodney W. Grimes 		rtfree(rt);
400df8bae1dSRodney W. Grimes out:
401df8bae1dSRodney W. Grimes 	if (error)
402df8bae1dSRodney W. Grimes 		rtstat.rts_badredirect++;
403df8bae1dSRodney W. Grimes 	else if (stat != NULL)
404df8bae1dSRodney W. Grimes 		(*stat)++;
405df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
406df8bae1dSRodney W. Grimes 	info.rti_info[RTAX_DST] = dst;
407df8bae1dSRodney W. Grimes 	info.rti_info[RTAX_GATEWAY] = gateway;
408df8bae1dSRodney W. Grimes 	info.rti_info[RTAX_NETMASK] = netmask;
409df8bae1dSRodney W. Grimes 	info.rti_info[RTAX_AUTHOR] = src;
410df8bae1dSRodney W. Grimes 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
411df8bae1dSRodney W. Grimes }
412df8bae1dSRodney W. Grimes 
413df8bae1dSRodney W. Grimes /*
414df8bae1dSRodney W. Grimes  * Routing table ioctl interface.
415df8bae1dSRodney W. Grimes  */
416df8bae1dSRodney W. Grimes int
417d1dd20beSSam Leffler rtioctl(u_long req, caddr_t data)
418df8bae1dSRodney W. Grimes {
4195090559bSChristian S.J. Peron 
4205090559bSChristian S.J. Peron 	/*
4215090559bSChristian S.J. Peron 	 * If more ioctl commands are added here, make sure the proper
4225090559bSChristian S.J. Peron 	 * super-user checks are being performed because it is possible for
4235090559bSChristian S.J. Peron 	 * prison-root to make it this far if raw sockets have been enabled
4245090559bSChristian S.J. Peron 	 * in jails.
4255090559bSChristian S.J. Peron 	 */
426623ae52eSPoul-Henning Kamp #ifdef INET
427f0068c4aSGarrett Wollman 	/* Multicast goop, grrr... */
428bbb4330bSLuigi Rizzo 	return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
429623ae52eSPoul-Henning Kamp #else /* INET */
430623ae52eSPoul-Henning Kamp 	return ENXIO;
431623ae52eSPoul-Henning Kamp #endif /* INET */
432df8bae1dSRodney W. Grimes }
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes struct ifaddr *
435d1dd20beSSam Leffler ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
436df8bae1dSRodney W. Grimes {
437df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
438d1dd20beSSam Leffler 
439df8bae1dSRodney W. Grimes 	if ((flags & RTF_GATEWAY) == 0) {
440df8bae1dSRodney W. Grimes 		/*
441df8bae1dSRodney W. Grimes 		 * If we are adding a route to an interface,
442df8bae1dSRodney W. Grimes 		 * and the interface is a pt to pt link
443df8bae1dSRodney W. Grimes 		 * we should search for the destination
444df8bae1dSRodney W. Grimes 		 * as our clue to the interface.  Otherwise
445df8bae1dSRodney W. Grimes 		 * we can use the local address.
446df8bae1dSRodney W. Grimes 		 */
44785911824SLuigi Rizzo 		ifa = NULL;
44885911824SLuigi Rizzo 		if (flags & RTF_HOST)
449df8bae1dSRodney W. Grimes 			ifa = ifa_ifwithdstaddr(dst);
45085911824SLuigi Rizzo 		if (ifa == NULL)
451df8bae1dSRodney W. Grimes 			ifa = ifa_ifwithaddr(gateway);
452df8bae1dSRodney W. Grimes 	} else {
453df8bae1dSRodney W. Grimes 		/*
454df8bae1dSRodney W. Grimes 		 * If we are adding a route to a remote net
455df8bae1dSRodney W. Grimes 		 * or host, the gateway may still be on the
456df8bae1dSRodney W. Grimes 		 * other end of a pt to pt link.
457df8bae1dSRodney W. Grimes 		 */
458df8bae1dSRodney W. Grimes 		ifa = ifa_ifwithdstaddr(gateway);
459df8bae1dSRodney W. Grimes 	}
46085911824SLuigi Rizzo 	if (ifa == NULL)
461df8bae1dSRodney W. Grimes 		ifa = ifa_ifwithnet(gateway);
46285911824SLuigi Rizzo 	if (ifa == NULL) {
463ffdc316dSRuslan Ermilov 		struct rtentry *rt = rtalloc1(gateway, 0, 0UL);
46485911824SLuigi Rizzo 		if (rt == NULL)
46585911824SLuigi Rizzo 			return (NULL);
4667138d65cSSam Leffler 		RT_REMREF(rt);
467d1dd20beSSam Leffler 		RT_UNLOCK(rt);
46885911824SLuigi Rizzo 		if ((ifa = rt->rt_ifa) == NULL)
46985911824SLuigi Rizzo 			return (NULL);
470df8bae1dSRodney W. Grimes 	}
471df8bae1dSRodney W. Grimes 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
472df8bae1dSRodney W. Grimes 		struct ifaddr *oifa = ifa;
473df8bae1dSRodney W. Grimes 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
47485911824SLuigi Rizzo 		if (ifa == NULL)
475df8bae1dSRodney W. Grimes 			ifa = oifa;
476df8bae1dSRodney W. Grimes 	}
477df8bae1dSRodney W. Grimes 	return (ifa);
478df8bae1dSRodney W. Grimes }
479df8bae1dSRodney W. Grimes 
48085911824SLuigi Rizzo static walktree_f_t rt_fixdelete;
48185911824SLuigi Rizzo static walktree_f_t rt_fixchange;
482cd02a0b7SGarrett Wollman 
483cd02a0b7SGarrett Wollman struct rtfc_arg {
484cd02a0b7SGarrett Wollman 	struct rtentry *rt0;
485cd02a0b7SGarrett Wollman 	struct radix_node_head *rnh;
486cd02a0b7SGarrett Wollman };
48718e1f1f1SGarrett Wollman 
488b0a76b88SJulian Elischer /*
489b0a76b88SJulian Elischer  * Do appropriate manipulations of a routing tree given
490b0a76b88SJulian Elischer  * all the bits of info needed
491b0a76b88SJulian Elischer  */
492df8bae1dSRodney W. Grimes int
493d1dd20beSSam Leffler rtrequest(int req,
494d1dd20beSSam Leffler 	struct sockaddr *dst,
495d1dd20beSSam Leffler 	struct sockaddr *gateway,
496d1dd20beSSam Leffler 	struct sockaddr *netmask,
497d1dd20beSSam Leffler 	int flags,
498d1dd20beSSam Leffler 	struct rtentry **ret_nrt)
499df8bae1dSRodney W. Grimes {
5008071913dSRuslan Ermilov 	struct rt_addrinfo info;
5018071913dSRuslan Ermilov 
5028071913dSRuslan Ermilov 	bzero((caddr_t)&info, sizeof(info));
5038071913dSRuslan Ermilov 	info.rti_flags = flags;
5048071913dSRuslan Ermilov 	info.rti_info[RTAX_DST] = dst;
5058071913dSRuslan Ermilov 	info.rti_info[RTAX_GATEWAY] = gateway;
5068071913dSRuslan Ermilov 	info.rti_info[RTAX_NETMASK] = netmask;
5078071913dSRuslan Ermilov 	return rtrequest1(req, &info, ret_nrt);
5088071913dSRuslan Ermilov }
5098071913dSRuslan Ermilov 
5108071913dSRuslan Ermilov /*
5118071913dSRuslan Ermilov  * These (questionable) definitions of apparent local variables apply
5128071913dSRuslan Ermilov  * to the next two functions.  XXXXXX!!!
5138071913dSRuslan Ermilov  */
5148071913dSRuslan Ermilov #define	dst	info->rti_info[RTAX_DST]
5158071913dSRuslan Ermilov #define	gateway	info->rti_info[RTAX_GATEWAY]
5168071913dSRuslan Ermilov #define	netmask	info->rti_info[RTAX_NETMASK]
5178071913dSRuslan Ermilov #define	ifaaddr	info->rti_info[RTAX_IFA]
5188071913dSRuslan Ermilov #define	ifpaddr	info->rti_info[RTAX_IFP]
5198071913dSRuslan Ermilov #define	flags	info->rti_flags
5208071913dSRuslan Ermilov 
5218071913dSRuslan Ermilov int
522d1dd20beSSam Leffler rt_getifa(struct rt_addrinfo *info)
5238071913dSRuslan Ermilov {
5248071913dSRuslan Ermilov 	struct ifaddr *ifa;
5258071913dSRuslan Ermilov 	int error = 0;
5268071913dSRuslan Ermilov 
5278071913dSRuslan Ermilov 	/*
5288071913dSRuslan Ermilov 	 * ifp may be specified by sockaddr_dl
5298071913dSRuslan Ermilov 	 * when protocol address is ambiguous.
5308071913dSRuslan Ermilov 	 */
5318071913dSRuslan Ermilov 	if (info->rti_ifp == NULL && ifpaddr != NULL &&
5328071913dSRuslan Ermilov 	    ifpaddr->sa_family == AF_LINK &&
5338071913dSRuslan Ermilov 	    (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
5348071913dSRuslan Ermilov 		info->rti_ifp = ifa->ifa_ifp;
5358071913dSRuslan Ermilov 	if (info->rti_ifa == NULL && ifaaddr != NULL)
5368071913dSRuslan Ermilov 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
5378071913dSRuslan Ermilov 	if (info->rti_ifa == NULL) {
5388071913dSRuslan Ermilov 		struct sockaddr *sa;
5398071913dSRuslan Ermilov 
5408071913dSRuslan Ermilov 		sa = ifaaddr != NULL ? ifaaddr :
5418071913dSRuslan Ermilov 		    (gateway != NULL ? gateway : dst);
5428071913dSRuslan Ermilov 		if (sa != NULL && info->rti_ifp != NULL)
5438071913dSRuslan Ermilov 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
5448071913dSRuslan Ermilov 		else if (dst != NULL && gateway != NULL)
5458071913dSRuslan Ermilov 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
5468071913dSRuslan Ermilov 		else if (sa != NULL)
5478071913dSRuslan Ermilov 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
5488071913dSRuslan Ermilov 	}
5498071913dSRuslan Ermilov 	if ((ifa = info->rti_ifa) != NULL) {
5508071913dSRuslan Ermilov 		if (info->rti_ifp == NULL)
5518071913dSRuslan Ermilov 			info->rti_ifp = ifa->ifa_ifp;
5528071913dSRuslan Ermilov 	} else
5538071913dSRuslan Ermilov 		error = ENETUNREACH;
5548071913dSRuslan Ermilov 	return (error);
5558071913dSRuslan Ermilov }
5568071913dSRuslan Ermilov 
5579c63e9dbSSam Leffler /*
5589c63e9dbSSam Leffler  * Expunges references to a route that's about to be reclaimed.
5599c63e9dbSSam Leffler  * The route must be locked.
5609c63e9dbSSam Leffler  */
5619c63e9dbSSam Leffler int
5629c63e9dbSSam Leffler rtexpunge(struct rtentry *rt)
5639c63e9dbSSam Leffler {
5649c63e9dbSSam Leffler 	struct radix_node *rn;
5659c63e9dbSSam Leffler 	struct radix_node_head *rnh;
5669c63e9dbSSam Leffler 	struct ifaddr *ifa;
5679c63e9dbSSam Leffler 	int error = 0;
5689c63e9dbSSam Leffler 
5699c63e9dbSSam Leffler 	RT_LOCK_ASSERT(rt);
5709c63e9dbSSam Leffler #if 0
5719c63e9dbSSam Leffler 	/*
5729c63e9dbSSam Leffler 	 * We cannot assume anything about the reference count
5739c63e9dbSSam Leffler 	 * because protocols call us in many situations; often
5749c63e9dbSSam Leffler 	 * before unwinding references to the table entry.
5759c63e9dbSSam Leffler 	 */
5769c63e9dbSSam Leffler 	KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
5779c63e9dbSSam Leffler #endif
5789c63e9dbSSam Leffler 	/*
5799c63e9dbSSam Leffler 	 * Find the correct routing tree to use for this Address Family
5809c63e9dbSSam Leffler 	 */
5819c63e9dbSSam Leffler 	rnh = rt_tables[rt_key(rt)->sa_family];
58285911824SLuigi Rizzo 	if (rnh == NULL)
5839c63e9dbSSam Leffler 		return (EAFNOSUPPORT);
5849c63e9dbSSam Leffler 
5859c63e9dbSSam Leffler 	RADIX_NODE_HEAD_LOCK(rnh);
5869c63e9dbSSam Leffler 
5879c63e9dbSSam Leffler 	/*
5889c63e9dbSSam Leffler 	 * Remove the item from the tree; it should be there,
5899c63e9dbSSam Leffler 	 * but when callers invoke us blindly it may not (sigh).
5909c63e9dbSSam Leffler 	 */
5919c63e9dbSSam Leffler 	rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
59285911824SLuigi Rizzo 	if (rn == NULL) {
5939c63e9dbSSam Leffler 		error = ESRCH;
5949c63e9dbSSam Leffler 		goto bad;
5959c63e9dbSSam Leffler 	}
5969c63e9dbSSam Leffler 	KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
5979c63e9dbSSam Leffler 		("unexpected flags 0x%x", rn->rn_flags));
598d6941ce9SLuigi Rizzo 	KASSERT(rt == RNTORT(rn),
5999c63e9dbSSam Leffler 		("lookup mismatch, rt %p rn %p", rt, rn));
6009c63e9dbSSam Leffler 
6019c63e9dbSSam Leffler 	rt->rt_flags &= ~RTF_UP;
6029c63e9dbSSam Leffler 
6039c63e9dbSSam Leffler 	/*
6049c63e9dbSSam Leffler 	 * Now search what's left of the subtree for any cloned
6059c63e9dbSSam Leffler 	 * routes which might have been formed from this node.
6069c63e9dbSSam Leffler 	 */
60726d02ca7SAndre Oppermann 	if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
6089c63e9dbSSam Leffler 		rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
6099c63e9dbSSam Leffler 				       rt_fixdelete, rt);
6109c63e9dbSSam Leffler 
6119c63e9dbSSam Leffler 	/*
6129c63e9dbSSam Leffler 	 * Remove any external references we may have.
6139c63e9dbSSam Leffler 	 * This might result in another rtentry being freed if
6149c63e9dbSSam Leffler 	 * we held its last reference.
6159c63e9dbSSam Leffler 	 */
6169c63e9dbSSam Leffler 	if (rt->rt_gwroute) {
617d6941ce9SLuigi Rizzo 		RTFREE(rt->rt_gwroute);
61885911824SLuigi Rizzo 		rt->rt_gwroute = NULL;
6199c63e9dbSSam Leffler 	}
6209c63e9dbSSam Leffler 
6219c63e9dbSSam Leffler 	/*
6229c63e9dbSSam Leffler 	 * Give the protocol a chance to keep things in sync.
6239c63e9dbSSam Leffler 	 */
6249c63e9dbSSam Leffler 	if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
6259c63e9dbSSam Leffler 		struct rt_addrinfo info;
6269c63e9dbSSam Leffler 
6279c63e9dbSSam Leffler 		bzero((caddr_t)&info, sizeof(info));
6289c63e9dbSSam Leffler 		info.rti_flags = rt->rt_flags;
6299c63e9dbSSam Leffler 		info.rti_info[RTAX_DST] = rt_key(rt);
6309c63e9dbSSam Leffler 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
6319c63e9dbSSam Leffler 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
6329c63e9dbSSam Leffler 		ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
6339c63e9dbSSam Leffler 	}
6349c63e9dbSSam Leffler 
6359c63e9dbSSam Leffler 	/*
6369c63e9dbSSam Leffler 	 * one more rtentry floating around that is not
6379c63e9dbSSam Leffler 	 * linked to the routing table.
6389c63e9dbSSam Leffler 	 */
6399c63e9dbSSam Leffler 	rttrash++;
6409c63e9dbSSam Leffler bad:
6419c63e9dbSSam Leffler 	RADIX_NODE_HEAD_UNLOCK(rnh);
6429c63e9dbSSam Leffler 	return (error);
6439c63e9dbSSam Leffler }
6449c63e9dbSSam Leffler 
6458071913dSRuslan Ermilov int
646d1dd20beSSam Leffler rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
6478071913dSRuslan Ermilov {
648d1dd20beSSam Leffler 	int error = 0;
649df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
650df8bae1dSRodney W. Grimes 	register struct radix_node *rn;
651df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
652df8bae1dSRodney W. Grimes 	struct ifaddr *ifa;
653df8bae1dSRodney W. Grimes 	struct sockaddr *ndst;
654df8bae1dSRodney W. Grimes #define senderr(x) { error = x ; goto bad; }
655df8bae1dSRodney W. Grimes 
656b0a76b88SJulian Elischer 	/*
657b0a76b88SJulian Elischer 	 * Find the correct routing tree to use for this Address Family
658b0a76b88SJulian Elischer 	 */
659d1dd20beSSam Leffler 	rnh = rt_tables[dst->sa_family];
66085911824SLuigi Rizzo 	if (rnh == NULL)
661983985c1SJeffrey Hsu 		return (EAFNOSUPPORT);
662956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
663b0a76b88SJulian Elischer 	/*
664b0a76b88SJulian Elischer 	 * If we are adding a host route then we don't want to put
66566953138SRuslan Ermilov 	 * a netmask in the tree, nor do we want to clone it.
666b0a76b88SJulian Elischer 	 */
66766953138SRuslan Ermilov 	if (flags & RTF_HOST) {
66885911824SLuigi Rizzo 		netmask = NULL;
66926d02ca7SAndre Oppermann 		flags &= ~RTF_CLONING;
67066953138SRuslan Ermilov 	}
671df8bae1dSRodney W. Grimes 	switch (req) {
672df8bae1dSRodney W. Grimes 	case RTM_DELETE:
673b0a76b88SJulian Elischer 		/*
674b0a76b88SJulian Elischer 		 * Remove the item from the tree and return it.
675b0a76b88SJulian Elischer 		 * Complain if it is not there and do no more processing.
676b0a76b88SJulian Elischer 		 */
677d1dd20beSSam Leffler 		rn = rnh->rnh_deladdr(dst, netmask, rnh);
67885911824SLuigi Rizzo 		if (rn == NULL)
679df8bae1dSRodney W. Grimes 			senderr(ESRCH);
680df8bae1dSRodney W. Grimes 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
681df8bae1dSRodney W. Grimes 			panic ("rtrequest delete");
682d6941ce9SLuigi Rizzo 		rt = RNTORT(rn);
683d1dd20beSSam Leffler 		RT_LOCK(rt);
6847138d65cSSam Leffler 		RT_ADDREF(rt);
68571eba915SRuslan Ermilov 		rt->rt_flags &= ~RTF_UP;
686c2bed6a3SGarrett Wollman 
687c2bed6a3SGarrett Wollman 		/*
688c2bed6a3SGarrett Wollman 		 * Now search what's left of the subtree for any cloned
689c2bed6a3SGarrett Wollman 		 * routes which might have been formed from this node.
690c2bed6a3SGarrett Wollman 		 */
69126d02ca7SAndre Oppermann 		if ((rt->rt_flags & RTF_CLONING) &&
692089cdfadSRuslan Ermilov 		    rt_mask(rt)) {
693089cdfadSRuslan Ermilov 			rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
694c2bed6a3SGarrett Wollman 					       rt_fixdelete, rt);
695c2bed6a3SGarrett Wollman 		}
6963545b048SGarrett Wollman 
697b0a76b88SJulian Elischer 		/*
698b0a76b88SJulian Elischer 		 * Remove any external references we may have.
699b0a76b88SJulian Elischer 		 * This might result in another rtentry being freed if
700dc733423SDag-Erling Smørgrav 		 * we held its last reference.
701b0a76b88SJulian Elischer 		 */
7026ac3b69dSBill Fenner 		if (rt->rt_gwroute) {
703d6941ce9SLuigi Rizzo 			RTFREE(rt->rt_gwroute);
70485911824SLuigi Rizzo 			rt->rt_gwroute = NULL;
7056ac3b69dSBill Fenner 		}
7066ac3b69dSBill Fenner 
7073545b048SGarrett Wollman 		/*
708499676dfSJulian Elischer 		 * give the protocol a chance to keep things in sync.
709b0a76b88SJulian Elischer 		 */
710df8bae1dSRodney W. Grimes 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
7118071913dSRuslan Ermilov 			ifa->ifa_rtrequest(RTM_DELETE, rt, info);
712499676dfSJulian Elischer 
713b0a76b88SJulian Elischer 		/*
714d6941ce9SLuigi Rizzo 		 * One more rtentry floating around that is not
715d6941ce9SLuigi Rizzo 		 * linked to the routing table. rttrash will be decremented
716d6941ce9SLuigi Rizzo 		 * when RTFREE(rt) is eventually called.
717499676dfSJulian Elischer 		 */
718499676dfSJulian Elischer 		rttrash++;
719499676dfSJulian Elischer 
720499676dfSJulian Elischer 		/*
721499676dfSJulian Elischer 		 * If the caller wants it, then it can have it,
722499676dfSJulian Elischer 		 * but it's up to it to free the rtentry as we won't be
723499676dfSJulian Elischer 		 * doing it.
724b0a76b88SJulian Elischer 		 */
725d1dd20beSSam Leffler 		if (ret_nrt) {
726df8bae1dSRodney W. Grimes 			*ret_nrt = rt;
727d1dd20beSSam Leffler 			RT_UNLOCK(rt);
728d1dd20beSSam Leffler 		} else
729d1dd20beSSam Leffler 			RTFREE_LOCKED(rt);
730df8bae1dSRodney W. Grimes 		break;
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes 	case RTM_RESOLVE:
73385911824SLuigi Rizzo 		if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
734df8bae1dSRodney W. Grimes 			senderr(EINVAL);
735df8bae1dSRodney W. Grimes 		ifa = rt->rt_ifa;
736d1dd20beSSam Leffler 		/* XXX locking? */
7373682d2baSDavid Greenman 		flags = rt->rt_flags &
73826d02ca7SAndre Oppermann 		    ~(RTF_CLONING | RTF_STATIC);
739995add1aSGarrett Wollman 		flags |= RTF_WASCLONED;
740df8bae1dSRodney W. Grimes 		gateway = rt->rt_gateway;
74185911824SLuigi Rizzo 		if ((netmask = rt->rt_genmask) == NULL)
742df8bae1dSRodney W. Grimes 			flags |= RTF_HOST;
743df8bae1dSRodney W. Grimes 		goto makeroute;
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes 	case RTM_ADD:
7465df72964SGarrett Wollman 		if ((flags & RTF_GATEWAY) && !gateway)
7475df72964SGarrett Wollman 			panic("rtrequest: GATEWAY but no gateway");
7485df72964SGarrett Wollman 
7498071913dSRuslan Ermilov 		if (info->rti_ifa == NULL && (error = rt_getifa(info)))
7508071913dSRuslan Ermilov 			senderr(error);
7518071913dSRuslan Ermilov 		ifa = info->rti_ifa;
7525df72964SGarrett Wollman 
753df8bae1dSRodney W. Grimes 	makeroute:
7542dc1d581SAndre Oppermann 		rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
75585911824SLuigi Rizzo 		if (rt == NULL)
756df8bae1dSRodney W. Grimes 			senderr(ENOBUFS);
757d1dd20beSSam Leffler 		RT_LOCK_INIT(rt);
758df8bae1dSRodney W. Grimes 		rt->rt_flags = RTF_UP | flags;
759499676dfSJulian Elischer 		/*
760499676dfSJulian Elischer 		 * Add the gateway. Possibly re-malloc-ing the storage for it
761499676dfSJulian Elischer 		 * also add the rt_gwroute if possible.
762499676dfSJulian Elischer 		 */
763d1dd20beSSam Leffler 		RT_LOCK(rt);
764831a80b0SMatthew Dillon 		if ((error = rt_setgate(rt, dst, gateway)) != 0) {
765d1dd20beSSam Leffler 			RT_LOCK_DESTROY(rt);
7662dc1d581SAndre Oppermann 			uma_zfree(rtzone, rt);
767704b0666SBill Fenner 			senderr(error);
768df8bae1dSRodney W. Grimes 		}
769499676dfSJulian Elischer 
770499676dfSJulian Elischer 		/*
771499676dfSJulian Elischer 		 * point to the (possibly newly malloc'd) dest address.
772499676dfSJulian Elischer 		 */
773d1dd20beSSam Leffler 		ndst = (struct sockaddr *)rt_key(rt);
774499676dfSJulian Elischer 
775499676dfSJulian Elischer 		/*
776499676dfSJulian Elischer 		 * make sure it contains the value we want (masked if needed).
777499676dfSJulian Elischer 		 */
778df8bae1dSRodney W. Grimes 		if (netmask) {
779df8bae1dSRodney W. Grimes 			rt_maskedcopy(dst, ndst, netmask);
780df8bae1dSRodney W. Grimes 		} else
7811838a647SLuigi Rizzo 			bcopy(dst, ndst, dst->sa_len);
7828e718bb4SGarrett Wollman 
7838e718bb4SGarrett Wollman 		/*
784499676dfSJulian Elischer 		 * Note that we now have a reference to the ifa.
7858e718bb4SGarrett Wollman 		 * This moved from below so that rnh->rnh_addaddr() can
786499676dfSJulian Elischer 		 * examine the ifa and  ifa->ifa_ifp if it so desires.
7878e718bb4SGarrett Wollman 		 */
78819fc74fbSJeffrey Hsu 		IFAREF(ifa);
7898e718bb4SGarrett Wollman 		rt->rt_ifa = ifa;
7908e718bb4SGarrett Wollman 		rt->rt_ifp = ifa->ifa_ifp;
7918e718bb4SGarrett Wollman 
792d1dd20beSSam Leffler 		/* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
793d1dd20beSSam Leffler 		rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
79485911824SLuigi Rizzo 		if (rn == NULL) {
795aca1a47cSGarrett Wollman 			struct rtentry *rt2;
796aca1a47cSGarrett Wollman 			/*
797aca1a47cSGarrett Wollman 			 * Uh-oh, we already have one of these in the tree.
798aca1a47cSGarrett Wollman 			 * We do a special hack: if the route that's already
79926d02ca7SAndre Oppermann 			 * there was generated by the cloning mechanism
80026d02ca7SAndre Oppermann 			 * then we just blow it away and retry the insertion
80126d02ca7SAndre Oppermann 			 * of the new one.
802aca1a47cSGarrett Wollman 			 */
80326d02ca7SAndre Oppermann 			rt2 = rtalloc1(dst, 0, 0);
804aca1a47cSGarrett Wollman 			if (rt2 && rt2->rt_parent) {
8059c63e9dbSSam Leffler 				rtexpunge(rt2);
8069c63e9dbSSam Leffler 				RT_UNLOCK(rt2);
807d1dd20beSSam Leffler 				rn = rnh->rnh_addaddr(ndst, netmask,
808aca1a47cSGarrett Wollman 						      rnh, rt->rt_nodes);
809fde327d6SGarrett Wollman 			} else if (rt2) {
810499676dfSJulian Elischer 				/* undo the extra ref we got */
811d1dd20beSSam Leffler 				RTFREE_LOCKED(rt2);
812aca1a47cSGarrett Wollman 			}
813aca1a47cSGarrett Wollman 		}
814aca1a47cSGarrett Wollman 
815499676dfSJulian Elischer 		/*
816499676dfSJulian Elischer 		 * If it still failed to go into the tree,
817499676dfSJulian Elischer 		 * then un-make it (this should be a function)
818499676dfSJulian Elischer 		 */
81985911824SLuigi Rizzo 		if (rn == NULL) {
820df8bae1dSRodney W. Grimes 			if (rt->rt_gwroute)
821d1dd20beSSam Leffler 				RTFREE(rt->rt_gwroute);
822d1dd20beSSam Leffler 			if (rt->rt_ifa)
8238e718bb4SGarrett Wollman 				IFAFREE(rt->rt_ifa);
824df8bae1dSRodney W. Grimes 			Free(rt_key(rt));
825d1dd20beSSam Leffler 			RT_LOCK_DESTROY(rt);
8262dc1d581SAndre Oppermann 			uma_zfree(rtzone, rt);
827df8bae1dSRodney W. Grimes 			senderr(EEXIST);
828df8bae1dSRodney W. Grimes 		}
829499676dfSJulian Elischer 
83085911824SLuigi Rizzo 		rt->rt_parent = NULL;
831771edb14SGarrett Wollman 
832499676dfSJulian Elischer 		/*
833499676dfSJulian Elischer 		 * If we got here from RESOLVE, then we are cloning
834499676dfSJulian Elischer 		 * so clone the rest, and note that we
835499676dfSJulian Elischer 		 * are a clone (and increment the parent's references)
836499676dfSJulian Elischer 		 */
837c2bed6a3SGarrett Wollman 		if (req == RTM_RESOLVE) {
838d1dd20beSSam Leffler 			KASSERT(ret_nrt && *ret_nrt,
839d1dd20beSSam Leffler 				("no route to clone from"));
840df8bae1dSRodney W. Grimes 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
84154e84abbSMike Silbersack 			rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
84226d02ca7SAndre Oppermann 			if ((*ret_nrt)->rt_flags & RTF_CLONING) {
843d1dd20beSSam Leffler 				/*
844d1dd20beSSam Leffler 				 * NB: We do not bump the refcnt on the parent
845d1dd20beSSam Leffler 				 * entry under the assumption that it will
846d1dd20beSSam Leffler 				 * remain so long as we do.  This is
847d1dd20beSSam Leffler 				 * important when deleting the parent route
848d1dd20beSSam Leffler 				 * as this operation requires traversing
849d1dd20beSSam Leffler 				 * the tree to delete all clones and futzing
850d1dd20beSSam Leffler 				 * with refcnts requires us to double-lock
851d1dd20beSSam Leffler 				 * parent through this back reference.
852d1dd20beSSam Leffler 				 */
853d1dd20beSSam Leffler 				rt->rt_parent = *ret_nrt;
854771edb14SGarrett Wollman 			}
85518e1f1f1SGarrett Wollman 		}
856499676dfSJulian Elischer 
857499676dfSJulian Elischer 		/*
858499676dfSJulian Elischer 		 * if this protocol has something to add to this then
859499676dfSJulian Elischer 		 * allow it to do that as well.
860499676dfSJulian Elischer 		 */
861df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest)
8628071913dSRuslan Ermilov 			ifa->ifa_rtrequest(req, rt, info);
863499676dfSJulian Elischer 
864cd02a0b7SGarrett Wollman 		/*
865cd02a0b7SGarrett Wollman 		 * We repeat the same procedure from rt_setgate() here because
866cd02a0b7SGarrett Wollman 		 * it doesn't fire when we call it there because the node
867cd02a0b7SGarrett Wollman 		 * hasn't been added to the tree yet.
868cd02a0b7SGarrett Wollman 		 */
86936fea5deSRuslan Ermilov 		if (req == RTM_ADD &&
87085911824SLuigi Rizzo 		    !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
871cd02a0b7SGarrett Wollman 			struct rtfc_arg arg;
872cd02a0b7SGarrett Wollman 			arg.rnh = rnh;
873cd02a0b7SGarrett Wollman 			arg.rt0 = rt;
874cd02a0b7SGarrett Wollman 			rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
875cd02a0b7SGarrett Wollman 					       rt_fixchange, &arg);
876cd02a0b7SGarrett Wollman 		}
877cd02a0b7SGarrett Wollman 
878499676dfSJulian Elischer 		/*
879499676dfSJulian Elischer 		 * actually return a resultant rtentry and
880499676dfSJulian Elischer 		 * give the caller a single reference.
881499676dfSJulian Elischer 		 */
882df8bae1dSRodney W. Grimes 		if (ret_nrt) {
883df8bae1dSRodney W. Grimes 			*ret_nrt = rt;
8847138d65cSSam Leffler 			RT_ADDREF(rt);
885df8bae1dSRodney W. Grimes 		}
886d1dd20beSSam Leffler 		RT_UNLOCK(rt);
887df8bae1dSRodney W. Grimes 		break;
8888071913dSRuslan Ermilov 	default:
8898071913dSRuslan Ermilov 		error = EOPNOTSUPP;
890df8bae1dSRodney W. Grimes 	}
891df8bae1dSRodney W. Grimes bad:
892956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
893df8bae1dSRodney W. Grimes 	return (error);
894d1dd20beSSam Leffler #undef senderr
895d1dd20beSSam Leffler }
896d1dd20beSSam Leffler 
8978071913dSRuslan Ermilov #undef dst
8988071913dSRuslan Ermilov #undef gateway
8998071913dSRuslan Ermilov #undef netmask
9008071913dSRuslan Ermilov #undef ifaaddr
9018071913dSRuslan Ermilov #undef ifpaddr
9028071913dSRuslan Ermilov #undef flags
903df8bae1dSRodney W. Grimes 
90418e1f1f1SGarrett Wollman /*
90518e1f1f1SGarrett Wollman  * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
90618e1f1f1SGarrett Wollman  * (i.e., the routes related to it by the operation of cloning).  This
907c2bed6a3SGarrett Wollman  * routine is iterated over all potential former-child-routes by way of
908c2bed6a3SGarrett Wollman  * rnh->rnh_walktree_from() above, and those that actually are children of
909c2bed6a3SGarrett Wollman  * the late parent (passed in as VP here) are themselves deleted.
91018e1f1f1SGarrett Wollman  */
911c2bed6a3SGarrett Wollman static int
912d1dd20beSSam Leffler rt_fixdelete(struct radix_node *rn, void *vp)
91318e1f1f1SGarrett Wollman {
914d6941ce9SLuigi Rizzo 	struct rtentry *rt = RNTORT(rn);
915c2bed6a3SGarrett Wollman 	struct rtentry *rt0 = vp;
91618e1f1f1SGarrett Wollman 
91736fea5deSRuslan Ermilov 	if (rt->rt_parent == rt0 &&
91826d02ca7SAndre Oppermann 	    !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
91985911824SLuigi Rizzo 		return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
92085911824SLuigi Rizzo 				 rt->rt_flags, NULL);
92118e1f1f1SGarrett Wollman 	}
922c2bed6a3SGarrett Wollman 	return 0;
92318e1f1f1SGarrett Wollman }
92418e1f1f1SGarrett Wollman 
925cd02a0b7SGarrett Wollman /*
926cd02a0b7SGarrett Wollman  * This routine is called from rt_setgate() to do the analogous thing for
927cd02a0b7SGarrett Wollman  * adds and changes.  There is the added complication in this case of a
928cd02a0b7SGarrett Wollman  * middle insert; i.e., insertion of a new network route between an older
929cd02a0b7SGarrett Wollman  * network route and (cloned) host routes.  For this reason, a simple check
930cd02a0b7SGarrett Wollman  * of rt->rt_parent is insufficient; each candidate route must be tested
931cd02a0b7SGarrett Wollman  * against the (mask, value) of the new route (passed as before in vp)
9329a701516SHajimu UMEMOTO  * to see if the new route matches it.
933cd02a0b7SGarrett Wollman  *
934cd02a0b7SGarrett Wollman  * XXX - it may be possible to do fixdelete() for changes and reserve this
935cd02a0b7SGarrett Wollman  * routine just for adds.  I'm not sure why I thought it was necessary to do
936cd02a0b7SGarrett Wollman  * changes this way.
937cd02a0b7SGarrett Wollman  */
938cd02a0b7SGarrett Wollman 
939cd02a0b7SGarrett Wollman static int
940d1dd20beSSam Leffler rt_fixchange(struct radix_node *rn, void *vp)
941cd02a0b7SGarrett Wollman {
942d6941ce9SLuigi Rizzo 	struct rtentry *rt = RNTORT(rn);
943cd02a0b7SGarrett Wollman 	struct rtfc_arg *ap = vp;
944cd02a0b7SGarrett Wollman 	struct rtentry *rt0 = ap->rt0;
945cd02a0b7SGarrett Wollman 	struct radix_node_head *rnh = ap->rnh;
9469a701516SHajimu UMEMOTO 	u_char *xk1, *xm1, *xk2, *xmp;
9479a701516SHajimu UMEMOTO 	int i, len, mlen;
948cd02a0b7SGarrett Wollman 
94985911824SLuigi Rizzo 	/* make sure we have a parent, and route is not pinned or cloning */
95036fea5deSRuslan Ermilov 	if (!rt->rt_parent ||
95185911824SLuigi Rizzo 	    (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
952cd02a0b7SGarrett Wollman 		return 0;
953cd02a0b7SGarrett Wollman 
95485911824SLuigi Rizzo 	if (rt->rt_parent == rt0)	/* parent match */
95585911824SLuigi Rizzo 		goto delete_rt;
956cd02a0b7SGarrett Wollman 	/*
957cd02a0b7SGarrett Wollman 	 * There probably is a function somewhere which does this...
958cd02a0b7SGarrett Wollman 	 * if not, there should be.
959cd02a0b7SGarrett Wollman 	 */
960d1dd20beSSam Leffler 	len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
961cd02a0b7SGarrett Wollman 
962cd02a0b7SGarrett Wollman 	xk1 = (u_char *)rt_key(rt0);
963cd02a0b7SGarrett Wollman 	xm1 = (u_char *)rt_mask(rt0);
964cd02a0b7SGarrett Wollman 	xk2 = (u_char *)rt_key(rt);
965cd02a0b7SGarrett Wollman 
9669a701516SHajimu UMEMOTO 	/* avoid applying a less specific route */
9679a701516SHajimu UMEMOTO 	xmp = (u_char *)rt_mask(rt->rt_parent);
968d1dd20beSSam Leffler 	mlen = rt_key(rt->rt_parent)->sa_len;
96985911824SLuigi Rizzo 	if (mlen > rt_key(rt0)->sa_len)		/* less specific route */
9709a701516SHajimu UMEMOTO 		return 0;
97185911824SLuigi Rizzo 	for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
97285911824SLuigi Rizzo 		if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
97385911824SLuigi Rizzo 			return 0;	/* less specific route */
9749a701516SHajimu UMEMOTO 
97585911824SLuigi Rizzo 	for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
97685911824SLuigi Rizzo 		if ((xk2[i] & xm1[i]) != xk1[i])
97785911824SLuigi Rizzo 			return 0;	/* no match */
978cd02a0b7SGarrett Wollman 
979cd02a0b7SGarrett Wollman 	/*
980cd02a0b7SGarrett Wollman 	 * OK, this node is a clone, and matches the node currently being
981cd02a0b7SGarrett Wollman 	 * changed/added under the node's mask.  So, get rid of it.
982cd02a0b7SGarrett Wollman 	 */
98385911824SLuigi Rizzo delete_rt:
98485911824SLuigi Rizzo 	return rtrequest(RTM_DELETE, rt_key(rt), NULL,
98585911824SLuigi Rizzo 			 rt_mask(rt), rt->rt_flags, NULL);
986cd02a0b7SGarrett Wollman }
987cd02a0b7SGarrett Wollman 
988df8bae1dSRodney W. Grimes int
989d1dd20beSSam Leffler rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
990df8bae1dSRodney W. Grimes {
991d1dd20beSSam Leffler 	/* XXX dst may be overwritten, can we move this to below */
992d1dd20beSSam Leffler 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
993e74642dfSLuigi Rizzo 	int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
994d1dd20beSSam Leffler 
995d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
996df8bae1dSRodney W. Grimes 
9971db1fffaSBill Fenner 	/*
9981db1fffaSBill Fenner 	 * A host route with the destination equal to the gateway
9991db1fffaSBill Fenner 	 * will interfere with keeping LLINFO in the routing
10001db1fffaSBill Fenner 	 * table, so disallow it.
10011db1fffaSBill Fenner 	 */
1002d1dd20beSSam Leffler 	if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
10031db1fffaSBill Fenner 					(RTF_HOST|RTF_GATEWAY)) &&
1004d1dd20beSSam Leffler 	    dst->sa_len == gate->sa_len &&
1005d1dd20beSSam Leffler 	    bcmp(dst, gate, dst->sa_len) == 0) {
10061db1fffaSBill Fenner 		/*
10071db1fffaSBill Fenner 		 * The route might already exist if this is an RTM_CHANGE
10081db1fffaSBill Fenner 		 * or a routing redirect, so try to delete it.
10091db1fffaSBill Fenner 		 */
1010d1dd20beSSam Leffler 		if (rt_key(rt))
10119c63e9dbSSam Leffler 			rtexpunge(rt);
10121db1fffaSBill Fenner 		return EADDRNOTAVAIL;
10131db1fffaSBill Fenner 	}
10141db1fffaSBill Fenner 
1015499676dfSJulian Elischer 	/*
101685911824SLuigi Rizzo 	 * Prepare to store the gateway in rt->rt_gateway.
101785911824SLuigi Rizzo 	 * Both dst and gateway are stored one after the other in the same
101885911824SLuigi Rizzo 	 * malloc'd chunk. If we have room, we can reuse the old buffer,
101985911824SLuigi Rizzo 	 * rt_gateway already points to the right place.
102085911824SLuigi Rizzo 	 * Otherwise, malloc a new block and update the 'dst' address.
1021499676dfSJulian Elischer 	 */
102285911824SLuigi Rizzo 	if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
102385911824SLuigi Rizzo 		caddr_t new;
102485911824SLuigi Rizzo 
1025df8bae1dSRodney W. Grimes 		R_Malloc(new, caddr_t, dlen + glen);
102685911824SLuigi Rizzo 		if (new == NULL)
10271db1fffaSBill Fenner 			return ENOBUFS;
1028499676dfSJulian Elischer 		/*
102985911824SLuigi Rizzo 		 * XXX note, we copy from *dst and not *rt_key(rt) because
103085911824SLuigi Rizzo 		 * rt_setgate() can be called to initialize a newly
103185911824SLuigi Rizzo 		 * allocated route entry, in which case rt_key(rt) == NULL
103285911824SLuigi Rizzo 		 * (and also rt->rt_gateway == NULL).
103385911824SLuigi Rizzo 		 * Free()/free() handle a NULL argument just fine.
1034499676dfSJulian Elischer 		 */
10351838a647SLuigi Rizzo 		bcopy(dst, new, dlen);
103685911824SLuigi Rizzo 		Free(rt_key(rt));	/* free old block, if any */
1037445e045bSAlexander Kabaev 		rt_key(rt) = (struct sockaddr *)new;
103885911824SLuigi Rizzo 		rt->rt_gateway = (struct sockaddr *)(new + dlen);
1039df8bae1dSRodney W. Grimes 	}
1040499676dfSJulian Elischer 
1041499676dfSJulian Elischer 	/*
104285911824SLuigi Rizzo 	 * Copy the new gateway value into the memory chunk.
104385911824SLuigi Rizzo 	 */
104485911824SLuigi Rizzo 	bcopy(gate, rt->rt_gateway, glen);
104585911824SLuigi Rizzo 
104685911824SLuigi Rizzo 	/*
1047499676dfSJulian Elischer 	 * If there is already a gwroute, it's now almost definitly wrong
1048499676dfSJulian Elischer 	 * so drop it.
1049499676dfSJulian Elischer 	 */
10508071913dSRuslan Ermilov 	if (rt->rt_gwroute != NULL) {
10518071913dSRuslan Ermilov 		RTFREE(rt->rt_gwroute);
10528071913dSRuslan Ermilov 		rt->rt_gwroute = NULL;
1053df8bae1dSRodney W. Grimes 	}
1054cd02a0b7SGarrett Wollman 	/*
1055cd02a0b7SGarrett Wollman 	 * Cloning loop avoidance:
1056cd02a0b7SGarrett Wollman 	 * In the presence of protocol-cloning and bad configuration,
1057cd02a0b7SGarrett Wollman 	 * it is possible to get stuck in bottomless mutual recursion
1058cd02a0b7SGarrett Wollman 	 * (rtrequest rt_setgate rtalloc1).  We avoid this by not allowing
1059cd02a0b7SGarrett Wollman 	 * protocol-cloning to operate for gateways (which is probably the
1060cd02a0b7SGarrett Wollman 	 * correct choice anyway), and avoid the resulting reference loops
1061cd02a0b7SGarrett Wollman 	 * by disallowing any route to run through itself as a gateway.
1062499676dfSJulian Elischer 	 * This is obviously mandatory when we get rt->rt_output().
106326d02ca7SAndre Oppermann 	 * XXX: After removal of PRCLONING this is probably not needed anymore.
1064cd02a0b7SGarrett Wollman 	 */
1065df8bae1dSRodney W. Grimes 	if (rt->rt_flags & RTF_GATEWAY) {
1066e21afc60SSam Leffler 		struct rtentry *gwrt;
1067e21afc60SSam Leffler 
1068e21afc60SSam Leffler 		RT_UNLOCK(rt);		/* XXX workaround LOR */
1069e21afc60SSam Leffler 		gwrt = rtalloc1(gate, 1, 0);
1070e21afc60SSam Leffler 		RT_LOCK(rt);
1071e21afc60SSam Leffler 		rt->rt_gwroute = gwrt;
1072cd02a0b7SGarrett Wollman 		if (rt->rt_gwroute == rt) {
1073d1dd20beSSam Leffler 			RTFREE_LOCKED(rt->rt_gwroute);
1074d6941ce9SLuigi Rizzo 			rt->rt_gwroute = NULL;
10751db1fffaSBill Fenner 			return EDQUOT; /* failure */
1076df8bae1dSRodney W. Grimes 		}
1077ea045210SSam Leffler 		if (rt->rt_gwroute != NULL)
1078d1dd20beSSam Leffler 			RT_UNLOCK(rt->rt_gwroute);
1079cd02a0b7SGarrett Wollman 	}
1080cd02a0b7SGarrett Wollman 
1081cd02a0b7SGarrett Wollman 	/*
1082cd02a0b7SGarrett Wollman 	 * This isn't going to do anything useful for host routes, so
1083cd02a0b7SGarrett Wollman 	 * don't bother.  Also make sure we have a reasonable mask
1084cd02a0b7SGarrett Wollman 	 * (we don't yet have one during adds).
1085cd02a0b7SGarrett Wollman 	 */
1086cd02a0b7SGarrett Wollman 	if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1087cd02a0b7SGarrett Wollman 		struct rtfc_arg arg;
1088d1dd20beSSam Leffler 
1089cd02a0b7SGarrett Wollman 		arg.rnh = rnh;
1090cd02a0b7SGarrett Wollman 		arg.rt0 = rt;
1091e21afc60SSam Leffler 		RT_UNLOCK(rt);		/* XXX workaround LOR */
1092956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK(rnh);
109372b9c8c9SSam Leffler 		RT_LOCK(rt);
1094cd02a0b7SGarrett Wollman 		rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1095cd02a0b7SGarrett Wollman 				       rt_fixchange, &arg);
1096956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
1097cd02a0b7SGarrett Wollman 	}
1098cd02a0b7SGarrett Wollman 
1099df8bae1dSRodney W. Grimes 	return 0;
1100df8bae1dSRodney W. Grimes }
1101df8bae1dSRodney W. Grimes 
1102f708ef1bSPoul-Henning Kamp static void
1103d1dd20beSSam Leffler rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1104df8bae1dSRodney W. Grimes {
1105df8bae1dSRodney W. Grimes 	register u_char *cp1 = (u_char *)src;
1106df8bae1dSRodney W. Grimes 	register u_char *cp2 = (u_char *)dst;
1107df8bae1dSRodney W. Grimes 	register u_char *cp3 = (u_char *)netmask;
1108df8bae1dSRodney W. Grimes 	u_char *cplim = cp2 + *cp3;
1109df8bae1dSRodney W. Grimes 	u_char *cplim2 = cp2 + *cp1;
1110df8bae1dSRodney W. Grimes 
1111df8bae1dSRodney W. Grimes 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1112df8bae1dSRodney W. Grimes 	cp3 += 2;
1113df8bae1dSRodney W. Grimes 	if (cplim > cplim2)
1114df8bae1dSRodney W. Grimes 		cplim = cplim2;
1115df8bae1dSRodney W. Grimes 	while (cp2 < cplim)
1116df8bae1dSRodney W. Grimes 		*cp2++ = *cp1++ & *cp3++;
1117df8bae1dSRodney W. Grimes 	if (cp2 < cplim2)
1118df8bae1dSRodney W. Grimes 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1119df8bae1dSRodney W. Grimes }
1120df8bae1dSRodney W. Grimes 
1121df8bae1dSRodney W. Grimes /*
1122df8bae1dSRodney W. Grimes  * Set up a routing table entry, normally
1123df8bae1dSRodney W. Grimes  * for an interface.
1124df8bae1dSRodney W. Grimes  */
1125df8bae1dSRodney W. Grimes int
1126d1dd20beSSam Leffler rtinit(struct ifaddr *ifa, int cmd, int flags)
1127df8bae1dSRodney W. Grimes {
11285aca0b30SLuigi Rizzo 	struct sockaddr *dst;
11298071913dSRuslan Ermilov 	struct sockaddr *netmask;
113085911824SLuigi Rizzo 	struct mbuf *m = NULL;
113185911824SLuigi Rizzo 	struct rtentry *rt = NULL;
11328071913dSRuslan Ermilov 	struct rt_addrinfo info;
11335aca0b30SLuigi Rizzo 	int error;
1134df8bae1dSRodney W. Grimes 
11358071913dSRuslan Ermilov 	if (flags & RTF_HOST) {
11368071913dSRuslan Ermilov 		dst = ifa->ifa_dstaddr;
11378071913dSRuslan Ermilov 		netmask = NULL;
11388071913dSRuslan Ermilov 	} else {
11398071913dSRuslan Ermilov 		dst = ifa->ifa_addr;
11408071913dSRuslan Ermilov 		netmask = ifa->ifa_netmask;
11418071913dSRuslan Ermilov 	}
1142b0a76b88SJulian Elischer 	/*
1143b0a76b88SJulian Elischer 	 * If it's a delete, check that if it exists, it's on the correct
1144b0a76b88SJulian Elischer 	 * interface or we might scrub a route to another ifa which would
1145b0a76b88SJulian Elischer 	 * be confusing at best and possibly worse.
1146b0a76b88SJulian Elischer 	 */
1147df8bae1dSRodney W. Grimes 	if (cmd == RTM_DELETE) {
11485aca0b30SLuigi Rizzo 		struct sockaddr *deldst;
11495aca0b30SLuigi Rizzo 		struct radix_node_head *rnh;
11505aca0b30SLuigi Rizzo 		struct radix_node *rn;
11515aca0b30SLuigi Rizzo 
1152b0a76b88SJulian Elischer 		/*
1153b0a76b88SJulian Elischer 		 * It's a delete, so it should already exist..
1154b0a76b88SJulian Elischer 		 * If it's a net, mask off the host bits
1155b0a76b88SJulian Elischer 		 * (Assuming we have a mask)
1156b0a76b88SJulian Elischer 		 */
11578071913dSRuslan Ermilov 		if (netmask != NULL) {
1158a163d034SWarner Losh 			m = m_get(M_DONTWAIT, MT_SONAME);
115982cd038dSYoshinobu Inoue 			if (m == NULL)
116082cd038dSYoshinobu Inoue 				return(ENOBUFS);
1161df8bae1dSRodney W. Grimes 			deldst = mtod(m, struct sockaddr *);
11628071913dSRuslan Ermilov 			rt_maskedcopy(dst, deldst, netmask);
1163df8bae1dSRodney W. Grimes 			dst = deldst;
1164df8bae1dSRodney W. Grimes 		}
1165b0a76b88SJulian Elischer 		/*
11668071913dSRuslan Ermilov 		 * Look up an rtentry that is in the routing tree and
11678071913dSRuslan Ermilov 		 * contains the correct info.
1168b0a76b88SJulian Elischer 		 */
1169956b0b65SJeffrey Hsu 		if ((rnh = rt_tables[dst->sa_family]) == NULL)
1170956b0b65SJeffrey Hsu 			goto bad;
1171956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK(rnh);
1172956b0b65SJeffrey Hsu 		error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL ||
11738071913dSRuslan Ermilov 		    (rn->rn_flags & RNF_ROOT) ||
1174d6941ce9SLuigi Rizzo 		    RNTORT(rn)->rt_ifa != ifa ||
117585911824SLuigi Rizzo 		    !sa_equal((struct sockaddr *)rn->rn_key, dst));
1176956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
1177956b0b65SJeffrey Hsu 		if (error) {
1178956b0b65SJeffrey Hsu bad:
1179df8bae1dSRodney W. Grimes 			if (m)
1180df8bae1dSRodney W. Grimes 				(void) m_free(m);
11818071913dSRuslan Ermilov 			return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1182df8bae1dSRodney W. Grimes 		}
1183b0a76b88SJulian Elischer 	}
1184b0a76b88SJulian Elischer 	/*
1185b0a76b88SJulian Elischer 	 * Do the actual request
1186b0a76b88SJulian Elischer 	 */
11878071913dSRuslan Ermilov 	bzero((caddr_t)&info, sizeof(info));
11888071913dSRuslan Ermilov 	info.rti_ifa = ifa;
11898071913dSRuslan Ermilov 	info.rti_flags = flags | ifa->ifa_flags;
11908071913dSRuslan Ermilov 	info.rti_info[RTAX_DST] = dst;
11918071913dSRuslan Ermilov 	info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
11928071913dSRuslan Ermilov 	info.rti_info[RTAX_NETMASK] = netmask;
11935aca0b30SLuigi Rizzo 	error = rtrequest1(cmd, &info, &rt);
11945aca0b30SLuigi Rizzo 	if (error == 0 && rt != NULL) {
11958071913dSRuslan Ermilov 		/*
11966f99b44cSBrian Somers 		 * notify any listening routing agents of the change
11978071913dSRuslan Ermilov 		 */
1198d1dd20beSSam Leffler 		RT_LOCK(rt);
11998071913dSRuslan Ermilov 		rt_newaddrmsg(cmd, ifa, error, rt);
12008071913dSRuslan Ermilov 		if (cmd == RTM_DELETE) {
1201b0a76b88SJulian Elischer 			/*
1202b0a76b88SJulian Elischer 			 * If we are deleting, and we found an entry, then
1203b0a76b88SJulian Elischer 			 * it's been removed from the tree.. now throw it away.
1204b0a76b88SJulian Elischer 			 */
1205d1dd20beSSam Leffler 			RTFREE_LOCKED(rt);
1206d1dd20beSSam Leffler 		} else {
1207d1dd20beSSam Leffler 			if (cmd == RTM_ADD) {
1208b0a76b88SJulian Elischer 				/*
12098071913dSRuslan Ermilov 				 * We just wanted to add it.. we don't actually
12108071913dSRuslan Ermilov 				 * need a reference.
1211b0a76b88SJulian Elischer 				 */
12127138d65cSSam Leffler 				RT_REMREF(rt);
1213df8bae1dSRodney W. Grimes 			}
1214d1dd20beSSam Leffler 			RT_UNLOCK(rt);
1215d1dd20beSSam Leffler 		}
1216df8bae1dSRodney W. Grimes 	}
12178071913dSRuslan Ermilov 	if (m)
12188071913dSRuslan Ermilov 		(void) m_free(m);
12193ec66d6cSDavid Greenman 	return (error);
12203ec66d6cSDavid Greenman }
1221cb64988fSLuoqi Chen 
1222d1dd20beSSam Leffler /*
122376927022SLuigi Rizzo  * rt_check() is invoked on each layer 2 output path, prior to
122476927022SLuigi Rizzo  * encapsulating outbound packets.
122576927022SLuigi Rizzo  *
122676927022SLuigi Rizzo  * The function is mostly used to find a routing entry for the gateway,
122776927022SLuigi Rizzo  * which in some protocol families could also point to the link-level
122876927022SLuigi Rizzo  * address for the gateway itself (the side effect of revalidating the
122976927022SLuigi Rizzo  * route to the destination is rather pointless at this stage, we did it
123076927022SLuigi Rizzo  * already a moment before in the pr_output() routine to locate the ifp
123176927022SLuigi Rizzo  * and gateway to use).
123276927022SLuigi Rizzo  *
123376927022SLuigi Rizzo  * When we remove the layer-3 to layer-2 mapping tables from the
123476927022SLuigi Rizzo  * routing table, this function can be removed.
123576927022SLuigi Rizzo  *
123676927022SLuigi Rizzo  * === On input ===
123776927022SLuigi Rizzo  *   *dst is the address of the NEXT HOP (which coincides with the
123876927022SLuigi Rizzo  *	final destination if directly reachable);
123976927022SLuigi Rizzo  *   *lrt0 points to the cached route to the final destination;
124076927022SLuigi Rizzo  *   *lrt is not meaningful;
124176927022SLuigi Rizzo  *
124276927022SLuigi Rizzo  * === Operation ===
124376927022SLuigi Rizzo  * If the route is marked down try to find a new route.  If the route
1244d1dd20beSSam Leffler  * to the gateway is gone, try to setup a new route.  Otherwise,
1245d1dd20beSSam Leffler  * if the route is marked for packets to be rejected, enforce that.
1246d1dd20beSSam Leffler  *
124776927022SLuigi Rizzo  * === On return ===
124876927022SLuigi Rizzo  *   *dst is unchanged;
124976927022SLuigi Rizzo  *   *lrt0 points to the (possibly new) route to the final destination
125076927022SLuigi Rizzo  *   *lrt points to the route to the next hop
1251d1dd20beSSam Leffler  *
1252490b9d88SLuigi Rizzo  * Their values are meaningful ONLY if no error is returned.
1253d1dd20beSSam Leffler  */
12547f760c48SMatthew N. Dodd int
1255d1dd20beSSam Leffler rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
12567f760c48SMatthew N. Dodd {
1257d1dd20beSSam Leffler #define senderr(x) { error = x ; goto bad; }
12587f760c48SMatthew N. Dodd 	struct rtentry *rt;
12597f760c48SMatthew N. Dodd 	struct rtentry *rt0;
12607f760c48SMatthew N. Dodd 	int error;
12617f760c48SMatthew N. Dodd 
12627f760c48SMatthew N. Dodd 	rt0 = *lrt0;
12637f760c48SMatthew N. Dodd 	rt = rt0;
1264d1dd20beSSam Leffler 	if (rt) {
1265d1dd20beSSam Leffler 		/* NB: the locking here is tortuous... */
1266d1dd20beSSam Leffler 		RT_LOCK(rt);
1267d1dd20beSSam Leffler 		if ((rt->rt_flags & RTF_UP) == 0) {
1268d1dd20beSSam Leffler 			RT_UNLOCK(rt);
1269d1dd20beSSam Leffler 			rt = rtalloc1(dst, 1, 0UL);
12707f760c48SMatthew N. Dodd 			if (rt != NULL) {
12717138d65cSSam Leffler 				RT_REMREF(rt);
1272d4b2657fSSam Leffler 				/* XXX what about if change? */
1273d1dd20beSSam Leffler 			} else
12747f760c48SMatthew N. Dodd 				senderr(EHOSTUNREACH);
1275d1dd20beSSam Leffler 			rt0 = rt;
12767f760c48SMatthew N. Dodd 		}
1277d1dd20beSSam Leffler 		/* XXX BSD/OS checks dst->sa_family != AF_NS */
12787f760c48SMatthew N. Dodd 		if (rt->rt_flags & RTF_GATEWAY) {
127985911824SLuigi Rizzo 			if (rt->rt_gwroute == NULL)
12807f760c48SMatthew N. Dodd 				goto lookup;
12817f760c48SMatthew N. Dodd 			rt = rt->rt_gwroute;
1282d1dd20beSSam Leffler 			RT_LOCK(rt);		/* NB: gwroute */
12837f760c48SMatthew N. Dodd 			if ((rt->rt_flags & RTF_UP) == 0) {
1284d1dd20beSSam Leffler 				rtfree(rt);	/* unlock gwroute */
12857f760c48SMatthew N. Dodd 				rt = rt0;
12867f760c48SMatthew N. Dodd 			lookup:
1287d1dd20beSSam Leffler 				RT_UNLOCK(rt0);
1288d1dd20beSSam Leffler 				rt = rtalloc1(rt->rt_gateway, 1, 0UL);
1289d1dd20beSSam Leffler 				RT_LOCK(rt0);
1290d1dd20beSSam Leffler 				rt0->rt_gwroute = rt;
129185911824SLuigi Rizzo 				if (rt == NULL) {
1292d1dd20beSSam Leffler 					RT_UNLOCK(rt0);
12937f760c48SMatthew N. Dodd 					senderr(EHOSTUNREACH);
12947f760c48SMatthew N. Dodd 				}
12957f760c48SMatthew N. Dodd 			}
1296d1dd20beSSam Leffler 			RT_UNLOCK(rt0);
1297d1dd20beSSam Leffler 		}
1298d1dd20beSSam Leffler 		/* XXX why are we inspecting rmx_expire? */
1299d1dd20beSSam Leffler 		error = (rt->rt_flags & RTF_REJECT) &&
1300d1dd20beSSam Leffler 			(rt->rt_rmx.rmx_expire == 0 ||
1301d1dd20beSSam Leffler 				time_second < rt->rt_rmx.rmx_expire);
1302d1dd20beSSam Leffler 		RT_UNLOCK(rt);
1303d1dd20beSSam Leffler 		if (error)
13047f760c48SMatthew N. Dodd 			senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
13057f760c48SMatthew N. Dodd 	}
1306d1dd20beSSam Leffler 	*lrt = rt;		/* NB: return unlocked */
13077f760c48SMatthew N. Dodd 	*lrt0 = rt0;
1308d1dd20beSSam Leffler 	return (0);
1309d1dd20beSSam Leffler bad:
1310d1dd20beSSam Leffler 	/* NB: lrt and lrt0 should not be interpreted if error is non-zero */
13117f760c48SMatthew N. Dodd 	return (error);
1312d1dd20beSSam Leffler #undef senderr
13137f760c48SMatthew N. Dodd }
13147f760c48SMatthew N. Dodd 
13156a800098SYoshinobu Inoue /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
13166a800098SYoshinobu Inoue SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
1317