in_rmx.c revision 191816
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 191816 2009-05-05 10:56:12Z zec $");
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_NET(curvnet);
255	INIT_VNET_INET(curvnet);
256	int fibnum;
257	void *newrock;
258	struct timeval atv;
259
260	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
261		if ((newrock = V_rt_tables[fibnum][AF_INET]) != 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		INIT_VNET_NET(vnet_iter);
328
329		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
330			rnh = V_rt_tables[fibnum][AF_INET];
331			arg.found = arg.killed = 0;
332			arg.rnh = rnh;
333			arg.nextstop = 0;
334			arg.draining = 1;
335			arg.updating = 0;
336			RADIX_NODE_HEAD_LOCK(rnh);
337			rnh->rnh_walktree(rnh, in_rtqkill, &arg);
338			RADIX_NODE_HEAD_UNLOCK(rnh);
339		}
340		CURVNET_RESTORE();
341	}
342	VNET_LIST_RUNLOCK();
343}
344
345static int _in_rt_was_here;
346/*
347 * Initialize our routing tree.
348 */
349int
350in_inithead(void **head, int off)
351{
352	INIT_VNET_INET(curvnet);
353	struct radix_node_head *rnh;
354
355	/* XXX MRT
356	 * This can be called from vfs_export.c too in which case 'off'
357	 * will be 0. We know the correct value so just use that and
358	 * return directly if it was 0.
359	 * This is a hack that replaces an even worse hack on a bad hack
360	 * on a bad design. After RELENG_7 this should be fixed but that
361	 * will change the ABI, so for now do it this way.
362	 */
363	if (!rn_inithead(head, 32))
364		return 0;
365
366	if (off == 0)		/* XXX MRT  see above */
367		return 1;	/* only do the rest for a real routing table */
368
369	V_rtq_reallyold = 60*60; /* one hour is "really old" */
370	V_rtq_minreallyold = 10; /* never automatically crank down to less */
371	V_rtq_toomany = 128;	 /* 128 cached routes is "too many" */
372	V_rtq_timeout = RTQ_TIMEOUT;
373
374	rnh = *head;
375	rnh->rnh_addaddr = in_addroute;
376	rnh->rnh_matchaddr = in_matroute;
377	rnh->rnh_close = in_clsroute;
378	if (_in_rt_was_here == 0 ) {
379		callout_init(&V_rtq_timer, CALLOUT_MPSAFE);
380		callout_reset(&V_rtq_timer, 1, in_rtqtimo, curvnet);
381		_in_rt_was_here = 1;
382	}
383	return 1;
384}
385
386/*
387 * This zaps old routes when the interface goes down or interface
388 * address is deleted.  In the latter case, it deletes static routes
389 * that point to this address.  If we don't do this, we may end up
390 * using the old address in the future.  The ones we always want to
391 * get rid of are things like ARP entries, since the user might down
392 * the interface, walk over to a completely different network, and
393 * plug back in.
394 */
395struct in_ifadown_arg {
396	struct ifaddr *ifa;
397	int del;
398};
399
400static int
401in_ifadownkill(struct radix_node *rn, void *xap)
402{
403	struct in_ifadown_arg *ap = xap;
404	struct rtentry *rt = (struct rtentry *)rn;
405
406	RT_LOCK(rt);
407	if (rt->rt_ifa == ap->ifa &&
408	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
409		/*
410		 * We need to disable the automatic prune that happens
411		 * in this case in rtrequest() because it will blow
412		 * away the pointers that rn_walktree() needs in order
413		 * continue our descent.  We will end up deleting all
414		 * the routes that rtrequest() would have in any case,
415		 * so that behavior is not needed there.
416		 */
417		rtexpunge(rt);
418	}
419	RT_UNLOCK(rt);
420	return 0;
421}
422
423int
424in_ifadown(struct ifaddr *ifa, int delete)
425{
426	INIT_VNET_NET(curvnet);
427	struct in_ifadown_arg arg;
428	struct radix_node_head *rnh;
429	int	fibnum;
430
431	if (ifa->ifa_addr->sa_family != AF_INET)
432		return 1;
433
434	for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
435		rnh = V_rt_tables[fibnum][AF_INET];
436		arg.ifa = ifa;
437		arg.del = delete;
438		RADIX_NODE_HEAD_LOCK(rnh);
439		rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
440		RADIX_NODE_HEAD_UNLOCK(rnh);
441		ifa->ifa_flags &= ~IFA_ROUTE;		/* XXXlocking? */
442	}
443	return 0;
444}
445
446/*
447 * inet versions of rt functions. These have fib extensions and
448 * for now will just reference the _fib variants.
449 * eventually this order will be reversed,
450 */
451void
452in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum)
453{
454	rtalloc_ign_fib(ro, ignflags, fibnum);
455}
456
457int
458in_rtrequest( int req,
459	struct sockaddr *dst,
460	struct sockaddr *gateway,
461	struct sockaddr *netmask,
462	int flags,
463	struct rtentry **ret_nrt,
464	u_int fibnum)
465{
466	return (rtrequest_fib(req, dst, gateway, netmask,
467	    flags, ret_nrt, fibnum));
468}
469
470struct rtentry *
471in_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum)
472{
473	return (rtalloc1_fib(dst, report, ignflags, fibnum));
474}
475
476void
477in_rtredirect(struct sockaddr *dst,
478	struct sockaddr *gateway,
479	struct sockaddr *netmask,
480	int flags,
481	struct sockaddr *src,
482	u_int fibnum)
483{
484	rtredirect_fib(dst, gateway, netmask, flags, src, fibnum);
485}
486
487void
488in_rtalloc(struct route *ro, u_int fibnum)
489{
490	rtalloc_ign_fib(ro, 0UL, fibnum);
491}
492
493#if 0
494int	 in_rt_getifa(struct rt_addrinfo *, u_int fibnum);
495int	 in_rtioctl(u_long, caddr_t, u_int);
496int	 in_rtrequest1(int, struct rt_addrinfo *, struct rtentry **, u_int);
497#endif
498
499
500