in_rmx.c revision 193232
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
30/*
31 * This code does two things necessary for the enhanced TCP metrics to
32 * function in a useful manner:
33 *  1) It marks all non-host routes as `cloning', thus ensuring that
34 *     every actual reference to such a route actually gets turned
35 *     into a reference to a host route to the specific destination
36 *     requested.
37 *  2) When such routes lose all their references, it arranges for them
38 *     to be deleted in some random collection of circumstances, so that
39 *     a large quantity of stale routing data is not kept in kernel memory
40 *     indefinitely.  See in_rtqtimo() below for the exact mechanism.
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sys/netinet/in_rmx.c 193232 2009-06-01 15:49:42Z bz $");
45
46#include "opt_route.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/sysctl.h>
52#include <sys/socket.h>
53#include <sys/mbuf.h>
54#include <sys/syslog.h>
55#include <sys/callout.h>
56#include <sys/vimage.h>
57
58#include <net/if.h>
59#include <net/route.h>
60#include <net/vnet.h>
61
62#include <netinet/in.h>
63#include <netinet/in_var.h>
64#include <netinet/ip_var.h>
65#include <netinet/vinet.h>
66
67extern int	in_inithead(void **head, int off);
68
69#define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
70
71/*
72 * Do what we need to do when inserting a route.
73 */
74static struct radix_node *
75in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
76    struct radix_node *treenodes)
77{
78	struct rtentry *rt = (struct rtentry *)treenodes;
79	struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
80
81	RADIX_NODE_HEAD_WLOCK_ASSERT(head);
82	/*
83	 * A little bit of help for both IP output and input:
84	 *   For host routes, we make sure that RTF_BROADCAST
85	 *   is set for anything that looks like a broadcast address.
86	 *   This way, we can avoid an expensive call to in_broadcast()
87	 *   in ip_output() most of the time (because the route passed
88	 *   to ip_output() is almost always a host route).
89	 *
90	 *   We also do the same for local addresses, with the thought
91	 *   that this might one day be used to speed up ip_input().
92	 *
93	 * We also mark routes to multicast addresses as such, because
94	 * it's easy to do and might be useful (but this is much more
95	 * dubious since it's so easy to inspect the address).
96	 */
97	if (rt->rt_flags & RTF_HOST) {
98		if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
99			rt->rt_flags |= RTF_BROADCAST;
100		} else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
101		    sin->sin_addr.s_addr) {
102			rt->rt_flags |= RTF_LOCAL;
103		}
104	}
105	if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
106		rt->rt_flags |= RTF_MULTICAST;
107
108	if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
109		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
110
111	return (rn_addroute(v_arg, n_arg, head, treenodes));
112}
113
114/*
115 * This code is the inverse of in_clsroute: on first reference, if we
116 * were managing the route, stop doing so and set the expiration timer
117 * back off again.
118 */
119static struct radix_node *
120in_matroute(void *v_arg, struct radix_node_head *head)
121{
122	struct radix_node *rn = rn_match(v_arg, head);
123	struct rtentry *rt = (struct rtentry *)rn;
124
125	/*XXX locking? */
126	if (rt && rt->rt_refcnt == 0) {		/* this is first reference */
127		if (rt->rt_flags & RTPRF_OURS) {
128			rt->rt_flags &= ~RTPRF_OURS;
129			rt->rt_rmx.rmx_expire = 0;
130		}
131	}
132	return rn;
133}
134
135#ifdef VIMAGE_GLOBALS
136static int rtq_reallyold;
137static int rtq_minreallyold;
138static int rtq_toomany;
139#endif
140
141SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTEXPIRE, rtexpire,
142    CTLFLAG_RW, rtq_reallyold, 0,
143    "Default expiration time on dynamically learned routes");
144
145SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTMINEXPIRE,
146    rtminexpire, CTLFLAG_RW, rtq_minreallyold, 0,
147    "Minimum time to attempt to hold onto dynamically learned routes");
148
149SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTMAXCACHE,
150    rtmaxcache, CTLFLAG_RW, rtq_toomany, 0,
151    "Upper limit on dynamically learned routes");
152
153/*
154 * On last reference drop, mark the route as belong to us so that it can be
155 * timed out.
156 */
157static void
158in_clsroute(struct radix_node *rn, struct radix_node_head *head)
159{
160	INIT_VNET_INET(curvnet);
161	struct rtentry *rt = (struct rtentry *)rn;
162
163	RT_LOCK_ASSERT(rt);
164
165	if (!(rt->rt_flags & RTF_UP))
166		return;			/* prophylactic measures */
167
168	if (rt->rt_flags & RTPRF_OURS)
169		return;
170
171	if (!(rt->rt_flags & RTF_DYNAMIC))
172		return;
173
174	/*
175	 * If rtq_reallyold is 0, just delete the route without
176	 * waiting for a timeout cycle to kill it.
177	 */
178	if (V_rtq_reallyold != 0) {
179		rt->rt_flags |= RTPRF_OURS;
180		rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold;
181	} else {
182		rtexpunge(rt);
183	}
184}
185
186struct rtqk_arg {
187	struct radix_node_head *rnh;
188	int draining;
189	int killed;
190	int found;
191	int updating;
192	time_t nextstop;
193};
194
195/*
196 * Get rid of old routes.  When draining, this deletes everything, even when
197 * the timeout is not expired yet.  When updating, this makes sure that
198 * nothing has a timeout longer than the current value of rtq_reallyold.
199 */
200static int
201in_rtqkill(struct radix_node *rn, void *rock)
202{
203	INIT_VNET_INET(curvnet);
204	struct rtqk_arg *ap = rock;
205	struct rtentry *rt = (struct rtentry *)rn;
206	int err;
207
208	RADIX_NODE_HEAD_WLOCK_ASSERT(ap->rnh);
209
210	if (rt->rt_flags & RTPRF_OURS) {
211		ap->found++;
212
213		if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
214			if (rt->rt_refcnt > 0)
215				panic("rtqkill route really not free");
216
217			err = in_rtrequest(RTM_DELETE,
218					(struct sockaddr *)rt_key(rt),
219					rt->rt_gateway, rt_mask(rt),
220					rt->rt_flags | RTF_RNH_LOCKED, 0,
221					rt->rt_fibnum);
222			if (err) {
223				log(LOG_WARNING, "in_rtqkill: error %d\n", err);
224			} else {
225				ap->killed++;
226			}
227		} else {
228			if (ap->updating &&
229			    (rt->rt_rmx.rmx_expire - time_uptime >
230			     V_rtq_reallyold)) {
231				rt->rt_rmx.rmx_expire =
232				    time_uptime + V_rtq_reallyold;
233			}
234			ap->nextstop = lmin(ap->nextstop,
235					    rt->rt_rmx.rmx_expire);
236		}
237	}
238
239	return 0;
240}
241
242#define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
243#ifdef VIMAGE_GLOBALS
244static int rtq_timeout;
245static struct callout rtq_timer;
246#endif
247
248static void in_rtqtimo_one(void *rock);
249
250static void
251in_rtqtimo(void *rock)
252{
253	CURVNET_SET((struct vnet *) rock);
254	INIT_VNET_INET(curvnet);
255	int fibnum;
256	void *newrock;
257	struct timeval atv;
258
259	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
260		newrock = rt_tables_get_rnh(fibnum, AF_INET);
261		if (newrock != NULL)
262			in_rtqtimo_one(newrock);
263	}
264	atv.tv_usec = 0;
265	atv.tv_sec = V_rtq_timeout;
266	callout_reset(&V_rtq_timer, tvtohz(&atv), in_rtqtimo, rock);
267	CURVNET_RESTORE();
268}
269
270static void
271in_rtqtimo_one(void *rock)
272{
273	INIT_VNET_INET(curvnet);
274	struct radix_node_head *rnh = rock;
275	struct rtqk_arg arg;
276	static time_t last_adjusted_timeout = 0;
277
278	arg.found = arg.killed = 0;
279	arg.rnh = rnh;
280	arg.nextstop = time_uptime + V_rtq_timeout;
281	arg.draining = arg.updating = 0;
282	RADIX_NODE_HEAD_LOCK(rnh);
283	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
284	RADIX_NODE_HEAD_UNLOCK(rnh);
285
286	/*
287	 * Attempt to be somewhat dynamic about this:
288	 * If there are ``too many'' routes sitting around taking up space,
289	 * then crank down the timeout, and see if we can't make some more
290	 * go away.  However, we make sure that we will never adjust more
291	 * than once in rtq_timeout seconds, to keep from cranking down too
292	 * hard.
293	 */
294	if ((arg.found - arg.killed > V_rtq_toomany) &&
295	    (time_uptime - last_adjusted_timeout >= V_rtq_timeout) &&
296	    V_rtq_reallyold > V_rtq_minreallyold) {
297		V_rtq_reallyold = 2 * V_rtq_reallyold / 3;
298		if (V_rtq_reallyold < V_rtq_minreallyold) {
299			V_rtq_reallyold = V_rtq_minreallyold;
300		}
301
302		last_adjusted_timeout = time_uptime;
303#ifdef DIAGNOSTIC
304		log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
305		    V_rtq_reallyold);
306#endif
307		arg.found = arg.killed = 0;
308		arg.updating = 1;
309		RADIX_NODE_HEAD_LOCK(rnh);
310		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
311		RADIX_NODE_HEAD_UNLOCK(rnh);
312	}
313
314}
315
316void
317in_rtqdrain(void)
318{
319	VNET_ITERATOR_DECL(vnet_iter);
320	struct radix_node_head *rnh;
321	struct rtqk_arg arg;
322	int 	fibnum;
323
324	VNET_LIST_RLOCK();
325	VNET_FOREACH(vnet_iter) {
326		CURVNET_SET(vnet_iter);
327
328		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
329			rnh = rt_tables_get_rnh(fibnum, AF_INET);
330			arg.found = arg.killed = 0;
331			arg.rnh = rnh;
332			arg.nextstop = 0;
333			arg.draining = 1;
334			arg.updating = 0;
335			RADIX_NODE_HEAD_LOCK(rnh);
336			rnh->rnh_walktree(rnh, in_rtqkill, &arg);
337			RADIX_NODE_HEAD_UNLOCK(rnh);
338		}
339		CURVNET_RESTORE();
340	}
341	VNET_LIST_RUNLOCK();
342}
343
344static int _in_rt_was_here;
345/*
346 * Initialize our routing tree.
347 */
348int
349in_inithead(void **head, int off)
350{
351	INIT_VNET_INET(curvnet);
352	struct radix_node_head *rnh;
353
354	/* XXX MRT
355	 * This can be called from vfs_export.c too in which case 'off'
356	 * will be 0. We know the correct value so just use that and
357	 * return directly if it was 0.
358	 * This is a hack that replaces an even worse hack on a bad hack
359	 * on a bad design. After RELENG_7 this should be fixed but that
360	 * will change the ABI, so for now do it this way.
361	 */
362	if (!rn_inithead(head, 32))
363		return 0;
364
365	if (off == 0)		/* XXX MRT  see above */
366		return 1;	/* only do the rest for a real routing table */
367
368	V_rtq_reallyold = 60*60; /* one hour is "really old" */
369	V_rtq_minreallyold = 10; /* never automatically crank down to less */
370	V_rtq_toomany = 128;	 /* 128 cached routes is "too many" */
371	V_rtq_timeout = RTQ_TIMEOUT;
372
373	rnh = *head;
374	rnh->rnh_addaddr = in_addroute;
375	rnh->rnh_matchaddr = in_matroute;
376	rnh->rnh_close = in_clsroute;
377	if (_in_rt_was_here == 0 ) {
378		callout_init(&V_rtq_timer, CALLOUT_MPSAFE);
379		callout_reset(&V_rtq_timer, 1, in_rtqtimo, curvnet);
380		_in_rt_was_here = 1;
381	}
382	return 1;
383}
384
385/*
386 * This zaps old routes when the interface goes down or interface
387 * address is deleted.  In the latter case, it deletes static routes
388 * that point to this address.  If we don't do this, we may end up
389 * using the old address in the future.  The ones we always want to
390 * get rid of are things like ARP entries, since the user might down
391 * the interface, walk over to a completely different network, and
392 * plug back in.
393 */
394struct in_ifadown_arg {
395	struct ifaddr *ifa;
396	int del;
397};
398
399static int
400in_ifadownkill(struct radix_node *rn, void *xap)
401{
402	struct in_ifadown_arg *ap = xap;
403	struct rtentry *rt = (struct rtentry *)rn;
404
405	RT_LOCK(rt);
406	if (rt->rt_ifa == ap->ifa &&
407	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
408		/*
409		 * We need to disable the automatic prune that happens
410		 * in this case in rtrequest() because it will blow
411		 * away the pointers that rn_walktree() needs in order
412		 * continue our descent.  We will end up deleting all
413		 * the routes that rtrequest() would have in any case,
414		 * so that behavior is not needed there.
415		 */
416		rtexpunge(rt);
417	}
418	RT_UNLOCK(rt);
419	return 0;
420}
421
422int
423in_ifadown(struct ifaddr *ifa, int delete)
424{
425	struct in_ifadown_arg arg;
426	struct radix_node_head *rnh;
427	int	fibnum;
428
429	if (ifa->ifa_addr->sa_family != AF_INET)
430		return 1;
431
432	for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
433		rnh = rt_tables_get_rnh(fibnum, AF_INET);
434		arg.ifa = ifa;
435		arg.del = delete;
436		RADIX_NODE_HEAD_LOCK(rnh);
437		rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
438		RADIX_NODE_HEAD_UNLOCK(rnh);
439		ifa->ifa_flags &= ~IFA_ROUTE;		/* XXXlocking? */
440	}
441	return 0;
442}
443
444/*
445 * inet versions of rt functions. These have fib extensions and
446 * for now will just reference the _fib variants.
447 * eventually this order will be reversed,
448 */
449void
450in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum)
451{
452	rtalloc_ign_fib(ro, ignflags, fibnum);
453}
454
455int
456in_rtrequest( int req,
457	struct sockaddr *dst,
458	struct sockaddr *gateway,
459	struct sockaddr *netmask,
460	int flags,
461	struct rtentry **ret_nrt,
462	u_int fibnum)
463{
464	return (rtrequest_fib(req, dst, gateway, netmask,
465	    flags, ret_nrt, fibnum));
466}
467
468struct rtentry *
469in_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum)
470{
471	return (rtalloc1_fib(dst, report, ignflags, fibnum));
472}
473
474void
475in_rtredirect(struct sockaddr *dst,
476	struct sockaddr *gateway,
477	struct sockaddr *netmask,
478	int flags,
479	struct sockaddr *src,
480	u_int fibnum)
481{
482	rtredirect_fib(dst, gateway, netmask, flags, src, fibnum);
483}
484
485void
486in_rtalloc(struct route *ro, u_int fibnum)
487{
488	rtalloc_ign_fib(ro, 0UL, fibnum);
489}
490
491#if 0
492int	 in_rt_getifa(struct rt_addrinfo *, u_int fibnum);
493int	 in_rtioctl(u_long, caddr_t, u_int);
494int	 in_rtrequest1(int, struct rt_addrinfo *, struct rtentry **, u_int);
495#endif
496
497
498