in_rmx.c revision 108250
1/*
2 * Copyright 1994, 1995 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/netinet/in_rmx.c 108250 2002-12-24 03:03:39Z hsu $
30 */
31
32/*
33 * This code does two things necessary for the enhanced TCP metrics to
34 * function in a useful manner:
35 *  1) It marks all non-host routes as `cloning', thus ensuring that
36 *     every actual reference to such a route actually gets turned
37 *     into a reference to a host route to the specific destination
38 *     requested.
39 *  2) When such routes lose all their references, it arranges for them
40 *     to be deleted in some random collection of circumstances, so that
41 *     a large quantity of stale routing data is not kept in kernel memory
42 *     indefinitely.  See in_rtqtimo() below for the exact mechanism.
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/sysctl.h>
49#include <sys/socket.h>
50#include <sys/mbuf.h>
51#include <sys/syslog.h>
52
53#include <net/if.h>
54#include <net/route.h>
55#include <netinet/in.h>
56#include <netinet/in_var.h>
57#include <netinet/ip_var.h>
58
59extern int	in_inithead(void **head, int off);
60
61#define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
62
63/*
64 * Do what we need to do when inserting a route.
65 */
66static struct radix_node *
67in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
68	    struct radix_node *treenodes)
69{
70	struct rtentry *rt = (struct rtentry *)treenodes;
71	struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
72	struct radix_node *ret;
73
74	/*
75	 * For IP, all unicast non-host routes are automatically cloning.
76	 */
77	if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
78		rt->rt_flags |= RTF_MULTICAST;
79
80	if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
81		rt->rt_flags |= RTF_PRCLONING;
82	}
83
84	/*
85	 * A little bit of help for both IP output and input:
86	 *   For host routes, we make sure that RTF_BROADCAST
87	 *   is set for anything that looks like a broadcast address.
88	 *   This way, we can avoid an expensive call to in_broadcast()
89	 *   in ip_output() most of the time (because the route passed
90	 *   to ip_output() is almost always a host route).
91	 *
92	 *   We also do the same for local addresses, with the thought
93	 *   that this might one day be used to speed up ip_input().
94	 *
95	 * We also mark routes to multicast addresses as such, because
96	 * it's easy to do and might be useful (but this is much more
97	 * dubious since it's so easy to inspect the address).  (This
98	 * is done above.)
99	 */
100	if (rt->rt_flags & RTF_HOST) {
101		if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
102			rt->rt_flags |= RTF_BROADCAST;
103		} else {
104			if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
105			    == sin->sin_addr.s_addr)
106				rt->rt_flags |= RTF_LOCAL;
107		}
108	}
109
110	if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
111	    && rt->rt_ifp)
112		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
113
114	ret = rn_addroute(v_arg, n_arg, head, treenodes);
115	if (ret == NULL && rt->rt_flags & RTF_HOST) {
116		struct rtentry *rt2;
117		/*
118		 * We are trying to add a host route, but can't.
119		 * Find out if it is because of an
120		 * ARP entry and delete it if so.
121		 */
122		rt2 = rtalloc1((struct sockaddr *)sin, 0,
123				RTF_CLONING | RTF_PRCLONING);
124		if (rt2) {
125			if (rt2->rt_flags & RTF_LLINFO &&
126				rt2->rt_flags & RTF_HOST &&
127				rt2->rt_gateway &&
128				rt2->rt_gateway->sa_family == AF_LINK) {
129				rtrequest(RTM_DELETE,
130					  (struct sockaddr *)rt_key(rt2),
131					  rt2->rt_gateway,
132					  rt_mask(rt2), rt2->rt_flags, 0);
133				ret = rn_addroute(v_arg, n_arg, head,
134					treenodes);
135			}
136			RTFREE(rt2);
137		}
138	}
139
140	/*
141	 * If the new route created successfully, and we are forwarding,
142	 * and there is a cached route, free it.  Otherwise, we may end
143	 * up using the wrong route.
144	 */
145	if (ret != NULL && ipforwarding && ipforward_rt.ro_rt) {
146		RTFREE(ipforward_rt.ro_rt);
147		ipforward_rt.ro_rt = 0;
148	}
149
150	return ret;
151}
152
153/*
154 * This code is the inverse of in_clsroute: on first reference, if we
155 * were managing the route, stop doing so and set the expiration timer
156 * back off again.
157 */
158static struct radix_node *
159in_matroute(void *v_arg, struct radix_node_head *head)
160{
161	struct radix_node *rn = rn_match(v_arg, head);
162	struct rtentry *rt = (struct rtentry *)rn;
163
164	if(rt && rt->rt_refcnt == 0) { /* this is first reference */
165		if(rt->rt_flags & RTPRF_OURS) {
166			rt->rt_flags &= ~RTPRF_OURS;
167			rt->rt_rmx.rmx_expire = 0;
168		}
169	}
170	return rn;
171}
172
173static int rtq_reallyold = 60*60;
174	/* one hour is ``really old'' */
175SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
176    &rtq_reallyold , 0,
177    "Default expiration time on dynamically learned routes");
178
179static int rtq_minreallyold = 10;
180	/* never automatically crank down to less */
181SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
182    &rtq_minreallyold , 0,
183    "Minimum time to attempt to hold onto dynamically learned routes");
184
185static int rtq_toomany = 128;
186	/* 128 cached routes is ``too many'' */
187SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
188    &rtq_toomany , 0, "Upper limit on dynamically learned routes");
189
190/*
191 * On last reference drop, mark the route as belong to us so that it can be
192 * timed out.
193 */
194static void
195in_clsroute(struct radix_node *rn, struct radix_node_head *head)
196{
197	struct rtentry *rt = (struct rtentry *)rn;
198
199	if(!(rt->rt_flags & RTF_UP))
200		return;		/* prophylactic measures */
201
202	if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
203		return;
204
205	if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
206	   != RTF_WASCLONED)
207		return;
208
209	/*
210	 * As requested by David Greenman:
211	 * If rtq_reallyold is 0, just delete the route without
212	 * waiting for a timeout cycle to kill it.
213	 */
214	if(rtq_reallyold != 0) {
215		rt->rt_flags |= RTPRF_OURS;
216		rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
217	} else {
218		rtrequest(RTM_DELETE,
219			  (struct sockaddr *)rt_key(rt),
220			  rt->rt_gateway, rt_mask(rt),
221			  rt->rt_flags, 0);
222	}
223}
224
225struct rtqk_arg {
226	struct radix_node_head *rnh;
227	int draining;
228	int killed;
229	int found;
230	int updating;
231	time_t nextstop;
232};
233
234/*
235 * Get rid of old routes.  When draining, this deletes everything, even when
236 * the timeout is not expired yet.  When updating, this makes sure that
237 * nothing has a timeout longer than the current value of rtq_reallyold.
238 */
239static int
240in_rtqkill(struct radix_node *rn, void *rock)
241{
242	struct rtqk_arg *ap = rock;
243	struct rtentry *rt = (struct rtentry *)rn;
244	int err;
245
246	if(rt->rt_flags & RTPRF_OURS) {
247		ap->found++;
248
249		if(ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
250			if(rt->rt_refcnt > 0)
251				panic("rtqkill route really not free");
252
253			err = rtrequest(RTM_DELETE,
254					(struct sockaddr *)rt_key(rt),
255					rt->rt_gateway, rt_mask(rt),
256					rt->rt_flags, 0);
257			if(err) {
258				log(LOG_WARNING, "in_rtqkill: error %d\n", err);
259			} else {
260				ap->killed++;
261			}
262		} else {
263			if(ap->updating
264			   && (rt->rt_rmx.rmx_expire - time_second
265			       > rtq_reallyold)) {
266				rt->rt_rmx.rmx_expire = time_second
267					+ rtq_reallyold;
268			}
269			ap->nextstop = lmin(ap->nextstop,
270					    rt->rt_rmx.rmx_expire);
271		}
272	}
273
274	return 0;
275}
276
277#define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
278static int rtq_timeout = RTQ_TIMEOUT;
279
280static void
281in_rtqtimo(void *rock)
282{
283	struct radix_node_head *rnh = rock;
284	struct rtqk_arg arg;
285	struct timeval atv;
286	static time_t last_adjusted_timeout = 0;
287	int s;
288
289	arg.found = arg.killed = 0;
290	arg.rnh = rnh;
291	arg.nextstop = time_second + rtq_timeout;
292	arg.draining = arg.updating = 0;
293	s = splnet();
294	RADIX_NODE_HEAD_LOCK(rnh);
295	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
296	RADIX_NODE_HEAD_UNLOCK(rnh);
297	splx(s);
298
299	/*
300	 * Attempt to be somewhat dynamic about this:
301	 * If there are ``too many'' routes sitting around taking up space,
302	 * then crank down the timeout, and see if we can't make some more
303	 * go away.  However, we make sure that we will never adjust more
304	 * than once in rtq_timeout seconds, to keep from cranking down too
305	 * hard.
306	 */
307	if((arg.found - arg.killed > rtq_toomany)
308	   && (time_second - last_adjusted_timeout >= rtq_timeout)
309	   && rtq_reallyold > rtq_minreallyold) {
310		rtq_reallyold = 2*rtq_reallyold / 3;
311		if(rtq_reallyold < rtq_minreallyold) {
312			rtq_reallyold = rtq_minreallyold;
313		}
314
315		last_adjusted_timeout = time_second;
316#ifdef DIAGNOSTIC
317		log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
318		    rtq_reallyold);
319#endif
320		arg.found = arg.killed = 0;
321		arg.updating = 1;
322		s = splnet();
323		RADIX_NODE_HEAD_LOCK(rnh);
324		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
325		RADIX_NODE_HEAD_UNLOCK(rnh);
326		splx(s);
327	}
328
329	atv.tv_usec = 0;
330	atv.tv_sec = arg.nextstop - time_second;
331	timeout(in_rtqtimo, rock, tvtohz(&atv));
332}
333
334void
335in_rtqdrain(void)
336{
337	struct radix_node_head *rnh = rt_tables[AF_INET];
338	struct rtqk_arg arg;
339	int s;
340	arg.found = arg.killed = 0;
341	arg.rnh = rnh;
342	arg.nextstop = 0;
343	arg.draining = 1;
344	arg.updating = 0;
345	s = splnet();
346	RADIX_NODE_HEAD_LOCK(rnh);
347	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
348	RADIX_NODE_HEAD_UNLOCK(rnh);
349	splx(s);
350}
351
352/*
353 * Initialize our routing tree.
354 */
355int
356in_inithead(void **head, int off)
357{
358	struct radix_node_head *rnh;
359
360	if(!rn_inithead(head, off))
361		return 0;
362
363	if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
364		return 1;	/* only do this for the real routing table */
365
366	rnh = *head;
367	rnh->rnh_addaddr = in_addroute;
368	rnh->rnh_matchaddr = in_matroute;
369	rnh->rnh_close = in_clsroute;
370	in_rtqtimo(rnh);	/* kick off timeout first time */
371	return 1;
372}
373
374
375/*
376 * This zaps old routes when the interface goes down or interface
377 * address is deleted.  In the latter case, it deletes static routes
378 * that point to this address.  If we don't do this, we may end up
379 * using the old address in the future.  The ones we always want to
380 * get rid of are things like ARP entries, since the user might down
381 * the interface, walk over to a completely different network, and
382 * plug back in.
383 */
384struct in_ifadown_arg {
385	struct radix_node_head *rnh;
386	struct ifaddr *ifa;
387	int del;
388};
389
390static int
391in_ifadownkill(struct radix_node *rn, void *xap)
392{
393	struct in_ifadown_arg *ap = xap;
394	struct rtentry *rt = (struct rtentry *)rn;
395	int err;
396
397	if (rt->rt_ifa == ap->ifa &&
398	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
399		/*
400		 * We need to disable the automatic prune that happens
401		 * in this case in rtrequest() because it will blow
402		 * away the pointers that rn_walktree() needs in order
403		 * continue our descent.  We will end up deleting all
404		 * the routes that rtrequest() would have in any case,
405		 * so that behavior is not needed there.
406		 */
407		rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
408		err = rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
409				rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
410		if (err) {
411			log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
412		}
413	}
414	return 0;
415}
416
417int
418in_ifadown(struct ifaddr *ifa, int delete)
419{
420	struct in_ifadown_arg arg;
421	struct radix_node_head *rnh;
422
423	if (ifa->ifa_addr->sa_family != AF_INET)
424		return 1;
425
426	arg.rnh = rnh = rt_tables[AF_INET];
427	arg.ifa = ifa;
428	arg.del = delete;
429	RADIX_NODE_HEAD_LOCK(rnh);
430	rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
431	RADIX_NODE_HEAD_UNLOCK(rnh);
432	ifa->ifa_flags &= ~IFA_ROUTE;
433	return 0;
434}
435