2 * Copyright 1994, 1995 Massachusetts Institute of Technology
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
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
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
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.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
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>
52 #include <sys/syslog.h>
53 #include <sys/callout.h>
56 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
61 extern int in_inithead(void **head, int off);
63 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
66 * Do what we need to do when inserting a route.
68 static struct radix_node *
69 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
70 struct radix_node *treenodes)
72 struct rtentry *rt = (struct rtentry *)treenodes;
73 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
74 struct radix_node *ret;
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).
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().
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).
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;
99 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
100 rt->rt_flags |= RTF_MULTICAST;
102 if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
103 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
105 ret = rn_addroute(v_arg, n_arg, head, treenodes);
106 if (ret == NULL && rt->rt_flags & RTF_HOST) {
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.
113 rt2 = rtalloc1((struct sockaddr *)sin, 0, RTF_CLONING);
115 if (rt2->rt_flags & RTF_LLINFO &&
116 rt2->rt_flags & RTF_HOST &&
118 rt2->rt_gateway->sa_family == AF_LINK) {
121 ret = rn_addroute(v_arg, n_arg, head,
132 * This code is the inverse of in_clsroute: on first reference, if we
133 * were managing the route, stop doing so and set the expiration timer
136 static struct radix_node *
137 in_matroute(void *v_arg, struct radix_node_head *head)
139 struct radix_node *rn = rn_match(v_arg, head);
140 struct rtentry *rt = (struct rtentry *)rn;
143 if (rt && rt->rt_refcnt == 0) { /* this is first reference */
144 if (rt->rt_flags & RTPRF_OURS) {
145 rt->rt_flags &= ~RTPRF_OURS;
146 rt->rt_rmx.rmx_expire = 0;
152 static int rtq_reallyold = 60*60; /* one hour is "really old" */
153 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
154 &rtq_reallyold, 0, "Default expiration time on dynamically learned routes");
156 static int rtq_minreallyold = 10; /* never automatically crank down to less */
157 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
158 &rtq_minreallyold, 0,
159 "Minimum time to attempt to hold onto dynamically learned routes");
161 static int rtq_toomany = 128; /* 128 cached routes is "too many" */
162 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
163 &rtq_toomany, 0, "Upper limit on dynamically learned routes");
166 * On last reference drop, mark the route as belong to us so that it can be
170 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
172 struct rtentry *rt = (struct rtentry *)rn;
176 if (!(rt->rt_flags & RTF_UP))
177 return; /* prophylactic measures */
179 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
182 if (rt->rt_flags & RTPRF_OURS)
185 if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC)))
189 * If rtq_reallyold is 0, just delete the route without
190 * waiting for a timeout cycle to kill it.
192 if (rtq_reallyold != 0) {
193 rt->rt_flags |= RTPRF_OURS;
194 rt->rt_rmx.rmx_expire = time_uptime + rtq_reallyold;
201 struct radix_node_head *rnh;
210 * Get rid of old routes. When draining, this deletes everything, even when
211 * the timeout is not expired yet. When updating, this makes sure that
212 * nothing has a timeout longer than the current value of rtq_reallyold.
215 in_rtqkill(struct radix_node *rn, void *rock)
217 struct rtqk_arg *ap = rock;
218 struct rtentry *rt = (struct rtentry *)rn;
221 if (rt->rt_flags & RTPRF_OURS) {
224 if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
225 if (rt->rt_refcnt > 0)
226 panic("rtqkill route really not free");
228 err = rtrequest(RTM_DELETE,
229 (struct sockaddr *)rt_key(rt),
230 rt->rt_gateway, rt_mask(rt),
233 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
239 (rt->rt_rmx.rmx_expire - time_uptime >
241 rt->rt_rmx.rmx_expire =
242 time_uptime + rtq_reallyold;
244 ap->nextstop = lmin(ap->nextstop,
245 rt->rt_rmx.rmx_expire);
252 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
253 static int rtq_timeout = RTQ_TIMEOUT;
254 static struct callout rtq_timer;
257 in_rtqtimo(void *rock)
259 struct radix_node_head *rnh = rock;
262 static time_t last_adjusted_timeout = 0;
264 arg.found = arg.killed = 0;
266 arg.nextstop = time_uptime + rtq_timeout;
267 arg.draining = arg.updating = 0;
268 RADIX_NODE_HEAD_LOCK(rnh);
269 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
270 RADIX_NODE_HEAD_UNLOCK(rnh);
273 * Attempt to be somewhat dynamic about this:
274 * If there are ``too many'' routes sitting around taking up space,
275 * then crank down the timeout, and see if we can't make some more
276 * go away. However, we make sure that we will never adjust more
277 * than once in rtq_timeout seconds, to keep from cranking down too
280 if ((arg.found - arg.killed > rtq_toomany) &&
281 (time_uptime - last_adjusted_timeout >= rtq_timeout) &&
282 rtq_reallyold > rtq_minreallyold) {
283 rtq_reallyold = 2 * rtq_reallyold / 3;
284 if (rtq_reallyold < rtq_minreallyold) {
285 rtq_reallyold = rtq_minreallyold;
288 last_adjusted_timeout = time_uptime;
290 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
293 arg.found = arg.killed = 0;
295 RADIX_NODE_HEAD_LOCK(rnh);
296 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
297 RADIX_NODE_HEAD_UNLOCK(rnh);
301 atv.tv_sec = arg.nextstop - time_uptime;
302 callout_reset(&rtq_timer, tvtohz(&atv), in_rtqtimo, rock);
308 struct radix_node_head *rnh = rt_tables[AF_INET];
311 arg.found = arg.killed = 0;
316 RADIX_NODE_HEAD_LOCK(rnh);
317 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
318 RADIX_NODE_HEAD_UNLOCK(rnh);
322 * Initialize our routing tree.
325 in_inithead(void **head, int off)
327 struct radix_node_head *rnh;
329 if (!rn_inithead(head, off))
332 if (head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
333 return 1; /* only do this for the real routing table */
336 rnh->rnh_addaddr = in_addroute;
337 rnh->rnh_matchaddr = in_matroute;
338 rnh->rnh_close = in_clsroute;
339 callout_init(&rtq_timer, CALLOUT_MPSAFE);
340 in_rtqtimo(rnh); /* kick off timeout first time */
345 * This zaps old routes when the interface goes down or interface
346 * address is deleted. In the latter case, it deletes static routes
347 * that point to this address. If we don't do this, we may end up
348 * using the old address in the future. The ones we always want to
349 * get rid of are things like ARP entries, since the user might down
350 * the interface, walk over to a completely different network, and
353 struct in_ifadown_arg {
354 struct radix_node_head *rnh;
360 in_ifadownkill(struct radix_node *rn, void *xap)
362 struct in_ifadown_arg *ap = xap;
363 struct rtentry *rt = (struct rtentry *)rn;
366 if (rt->rt_ifa == ap->ifa &&
367 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
369 * We need to disable the automatic prune that happens
370 * in this case in rtrequest() because it will blow
371 * away the pointers that rn_walktree() needs in order
372 * continue our descent. We will end up deleting all
373 * the routes that rtrequest() would have in any case,
374 * so that behavior is not needed there.
376 rt->rt_flags &= ~RTF_CLONING;
384 in_ifadown(struct ifaddr *ifa, int delete)
386 struct in_ifadown_arg arg;
387 struct radix_node_head *rnh;
389 if (ifa->ifa_addr->sa_family != AF_INET)
392 arg.rnh = rnh = rt_tables[AF_INET];
395 RADIX_NODE_HEAD_LOCK(rnh);
396 rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
397 RADIX_NODE_HEAD_UNLOCK(rnh);
398 ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */