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