xref: /freebsd/sys/netinet/in_rmx.c (revision c0020399a650364d0134f79f3fa319f84064372d)
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$");
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 
67 extern 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  */
74 static struct radix_node *
75 in_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  */
119 static struct radix_node *
120 in_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
136 static int rtq_reallyold;
137 static int rtq_minreallyold;
138 static int rtq_toomany;
139 #endif
140 
141 SYSCTL_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 
145 SYSCTL_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 
149 SYSCTL_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  */
157 static void
158 in_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 
186 struct 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  */
200 static int
201 in_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
244 static int rtq_timeout;
245 static struct callout rtq_timer;
246 #endif
247 
248 static void in_rtqtimo_one(void *rock);
249 
250 static void
251 in_rtqtimo(void *rock)
252 {
253 	int fibnum;
254 	void *newrock;
255 	struct timeval atv;
256 
257 	KASSERT((rock == (void *)V_rt_tables[0][AF_INET]),
258 			("in_rtqtimo: unexpected arg"));
259 	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
260 		if ((newrock = V_rt_tables[fibnum][AF_INET]) != NULL)
261 			in_rtqtimo_one(newrock);
262 	}
263 	atv.tv_usec = 0;
264 	atv.tv_sec = V_rtq_timeout;
265 	callout_reset(&V_rtq_timer, tvtohz(&atv), in_rtqtimo, rock);
266 }
267 
268 static void
269 in_rtqtimo_one(void *rock)
270 {
271 	INIT_VNET_INET(curvnet);
272 	struct radix_node_head *rnh = rock;
273 	struct rtqk_arg arg;
274 	static time_t last_adjusted_timeout = 0;
275 
276 	arg.found = arg.killed = 0;
277 	arg.rnh = rnh;
278 	arg.nextstop = time_uptime + V_rtq_timeout;
279 	arg.draining = arg.updating = 0;
280 	RADIX_NODE_HEAD_LOCK(rnh);
281 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
282 	RADIX_NODE_HEAD_UNLOCK(rnh);
283 
284 	/*
285 	 * Attempt to be somewhat dynamic about this:
286 	 * If there are ``too many'' routes sitting around taking up space,
287 	 * then crank down the timeout, and see if we can't make some more
288 	 * go away.  However, we make sure that we will never adjust more
289 	 * than once in rtq_timeout seconds, to keep from cranking down too
290 	 * hard.
291 	 */
292 	if ((arg.found - arg.killed > V_rtq_toomany) &&
293 	    (time_uptime - last_adjusted_timeout >= V_rtq_timeout) &&
294 	    V_rtq_reallyold > V_rtq_minreallyold) {
295 		V_rtq_reallyold = 2 * V_rtq_reallyold / 3;
296 		if (V_rtq_reallyold < V_rtq_minreallyold) {
297 			V_rtq_reallyold = V_rtq_minreallyold;
298 		}
299 
300 		last_adjusted_timeout = time_uptime;
301 #ifdef DIAGNOSTIC
302 		log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
303 		    V_rtq_reallyold);
304 #endif
305 		arg.found = arg.killed = 0;
306 		arg.updating = 1;
307 		RADIX_NODE_HEAD_LOCK(rnh);
308 		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
309 		RADIX_NODE_HEAD_UNLOCK(rnh);
310 	}
311 
312 }
313 
314 void
315 in_rtqdrain(void)
316 {
317 	VNET_ITERATOR_DECL(vnet_iter);
318 	struct radix_node_head *rnh;
319 	struct rtqk_arg arg;
320 	int 	fibnum;
321 
322 	VNET_LIST_RLOCK();
323 	VNET_FOREACH(vnet_iter) {
324 		CURVNET_SET(vnet_iter);
325 		INIT_VNET_NET(vnet_iter);
326 
327 		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
328 			rnh = V_rt_tables[fibnum][AF_INET];
329 			arg.found = arg.killed = 0;
330 			arg.rnh = rnh;
331 			arg.nextstop = 0;
332 			arg.draining = 1;
333 			arg.updating = 0;
334 			RADIX_NODE_HEAD_LOCK(rnh);
335 			rnh->rnh_walktree(rnh, in_rtqkill, &arg);
336 			RADIX_NODE_HEAD_UNLOCK(rnh);
337 		}
338 		CURVNET_RESTORE();
339 	}
340 	VNET_LIST_RUNLOCK();
341 }
342 
343 static int _in_rt_was_here;
344 /*
345  * Initialize our routing tree.
346  */
347 int
348 in_inithead(void **head, int off)
349 {
350 	INIT_VNET_INET(curvnet);
351 	struct radix_node_head *rnh;
352 
353 	/* XXX MRT
354 	 * This can be called from vfs_export.c too in which case 'off'
355 	 * will be 0. We know the correct value so just use that and
356 	 * return directly if it was 0.
357 	 * This is a hack that replaces an even worse hack on a bad hack
358 	 * on a bad design. After RELENG_7 this should be fixed but that
359 	 * will change the ABI, so for now do it this way.
360 	 */
361 	if (!rn_inithead(head, 32))
362 		return 0;
363 
364 	if (off == 0)		/* XXX MRT  see above */
365 		return 1;	/* only do the rest for a real routing table */
366 
367 	V_rtq_reallyold = 60*60; /* one hour is "really old" */
368 	V_rtq_minreallyold = 10; /* never automatically crank down to less */
369 	V_rtq_toomany = 128;	 /* 128 cached routes is "too many" */
370 	V_rtq_timeout = RTQ_TIMEOUT;
371 
372 	rnh = *head;
373 	rnh->rnh_addaddr = in_addroute;
374 	rnh->rnh_matchaddr = in_matroute;
375 	rnh->rnh_close = in_clsroute;
376 	if (_in_rt_was_here == 0 ) {
377 		callout_init(&V_rtq_timer, CALLOUT_MPSAFE);
378 		in_rtqtimo(rnh);	/* kick off timeout first time */
379 		_in_rt_was_here = 1;
380 	}
381 	return 1;
382 }
383 
384 /*
385  * This zaps old routes when the interface goes down or interface
386  * address is deleted.  In the latter case, it deletes static routes
387  * that point to this address.  If we don't do this, we may end up
388  * using the old address in the future.  The ones we always want to
389  * get rid of are things like ARP entries, since the user might down
390  * the interface, walk over to a completely different network, and
391  * plug back in.
392  */
393 struct in_ifadown_arg {
394 	struct ifaddr *ifa;
395 	int del;
396 };
397 
398 static int
399 in_ifadownkill(struct radix_node *rn, void *xap)
400 {
401 	struct in_ifadown_arg *ap = xap;
402 	struct rtentry *rt = (struct rtentry *)rn;
403 
404 	RT_LOCK(rt);
405 	if (rt->rt_ifa == ap->ifa &&
406 	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
407 		/*
408 		 * We need to disable the automatic prune that happens
409 		 * in this case in rtrequest() because it will blow
410 		 * away the pointers that rn_walktree() needs in order
411 		 * continue our descent.  We will end up deleting all
412 		 * the routes that rtrequest() would have in any case,
413 		 * so that behavior is not needed there.
414 		 */
415 		rtexpunge(rt);
416 	}
417 	RT_UNLOCK(rt);
418 	return 0;
419 }
420 
421 int
422 in_ifadown(struct ifaddr *ifa, int delete)
423 {
424 	INIT_VNET_NET(curvnet);
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 = V_rt_tables[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  */
449 void
450 in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum)
451 {
452 	rtalloc_ign_fib(ro, ignflags, fibnum);
453 }
454 
455 int
456 in_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 
468 struct rtentry *
469 in_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum)
470 {
471 	return (rtalloc1_fib(dst, report, ignflags, fibnum));
472 }
473 
474 void
475 in_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 
485 void
486 in_rtalloc(struct route *ro, u_int fibnum)
487 {
488 	rtalloc_ign_fib(ro, 0UL, fibnum);
489 }
490 
491 #if 0
492 int	 in_rt_getifa(struct rt_addrinfo *, u_int fibnum);
493 int	 in_rtioctl(u_long, caddr_t, u_int);
494 int	 in_rtrequest1(int, struct rt_addrinfo *, struct rtentry **, u_int);
495 #endif
496 
497 
498