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
33 * This code does two things necessary for the enhanced TCP metrics to
34 * function in a useful manner:
35 * 1) It marks all non-host routes as `cloning', thus ensuring that
36 * every actual reference to such a route actually gets turned
37 * into a reference to a host route to the specific destination
39 * 2) When such routes lose all their references, it arranges for them
40 * to be deleted in some random collection of circumstances, so that
41 * a large quantity of stale routing data is not kept in kernel memory
42 * indefinitely. See in_rtqtimo() below for the exact mechanism.
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 #include <sys/callout.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip_var.h>
60 extern int in_inithead(void **head, int off);
62 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
65 * Do what we need to do when inserting a route.
67 static struct radix_node *
68 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
69 struct radix_node *treenodes)
71 struct rtentry *rt = (struct rtentry *)treenodes;
72 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
73 struct radix_node *ret;
76 * A little bit of help for both IP output and input:
77 * For host routes, we make sure that RTF_BROADCAST
78 * is set for anything that looks like a broadcast address.
79 * This way, we can avoid an expensive call to in_broadcast()
80 * in ip_output() most of the time (because the route passed
81 * to ip_output() is almost always a host route).
83 * We also do the same for local addresses, with the thought
84 * that this might one day be used to speed up ip_input().
86 * We also mark routes to multicast addresses as such, because
87 * it's easy to do and might be useful (but this is much more
88 * dubious since it's so easy to inspect the address).
90 if (rt->rt_flags & RTF_HOST) {
91 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
92 rt->rt_flags |= RTF_BROADCAST;
93 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
94 sin->sin_addr.s_addr) {
95 rt->rt_flags |= RTF_LOCAL;
98 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
99 rt->rt_flags |= RTF_MULTICAST;
101 if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
102 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
104 ret = rn_addroute(v_arg, n_arg, head, treenodes);
105 if (ret == NULL && rt->rt_flags & RTF_HOST) {
108 * We are trying to add a host route, but can't.
109 * Find out if it is because of an
110 * ARP entry and delete it if so.
112 rt2 = rtalloc1((struct sockaddr *)sin, 0, RTF_CLONING);
114 if (rt2->rt_flags & RTF_LLINFO &&
115 rt2->rt_flags & RTF_HOST &&
117 rt2->rt_gateway->sa_family == AF_LINK) {
120 ret = rn_addroute(v_arg, n_arg, head,
131 * This code is the inverse of in_clsroute: on first reference, if we
132 * were managing the route, stop doing so and set the expiration timer
135 static struct radix_node *
136 in_matroute(void *v_arg, struct radix_node_head *head)
138 struct radix_node *rn = rn_match(v_arg, head);
139 struct rtentry *rt = (struct rtentry *)rn;
142 if (rt && rt->rt_refcnt == 0) { /* this is first reference */
143 if (rt->rt_flags & RTPRF_OURS) {
144 rt->rt_flags &= ~RTPRF_OURS;
145 rt->rt_rmx.rmx_expire = 0;
151 static int rtq_reallyold = 60*60; /* one hour is "really old" */
152 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
153 &rtq_reallyold, 0, "Default expiration time on dynamically learned routes");
155 static int rtq_minreallyold = 10; /* never automatically crank down to less */
156 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
157 &rtq_minreallyold, 0,
158 "Minimum time to attempt to hold onto dynamically learned routes");
160 static int rtq_toomany = 128; /* 128 cached routes is "too many" */
161 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
162 &rtq_toomany, 0, "Upper limit on dynamically learned routes");
165 * On last reference drop, mark the route as belong to us so that it can be
169 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
171 struct rtentry *rt = (struct rtentry *)rn;
175 if (!(rt->rt_flags & RTF_UP))
176 return; /* prophylactic measures */
178 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
181 if (rt->rt_flags & RTPRF_OURS)
184 if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC)))
188 * If rtq_reallyold is 0, just delete the route without
189 * waiting for a timeout cycle to kill it.
191 if (rtq_reallyold != 0) {
192 rt->rt_flags |= RTPRF_OURS;
193 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
200 struct radix_node_head *rnh;
209 * Get rid of old routes. When draining, this deletes everything, even when
210 * the timeout is not expired yet. When updating, this makes sure that
211 * nothing has a timeout longer than the current value of rtq_reallyold.
214 in_rtqkill(struct radix_node *rn, void *rock)
216 struct rtqk_arg *ap = rock;
217 struct rtentry *rt = (struct rtentry *)rn;
220 if (rt->rt_flags & RTPRF_OURS) {
223 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
224 if (rt->rt_refcnt > 0)
225 panic("rtqkill route really not free");
227 err = rtrequest(RTM_DELETE,
228 (struct sockaddr *)rt_key(rt),
229 rt->rt_gateway, rt_mask(rt),
232 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
238 (rt->rt_rmx.rmx_expire - time_second >
240 rt->rt_rmx.rmx_expire =
241 time_second + rtq_reallyold;
243 ap->nextstop = lmin(ap->nextstop,
244 rt->rt_rmx.rmx_expire);
251 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
252 static int rtq_timeout = RTQ_TIMEOUT;
253 static struct callout rtq_timer;
256 in_rtqtimo(void *rock)
258 struct radix_node_head *rnh = rock;
261 static time_t last_adjusted_timeout = 0;
263 arg.found = arg.killed = 0;
265 arg.nextstop = time_second + rtq_timeout;
266 arg.draining = arg.updating = 0;
267 RADIX_NODE_HEAD_LOCK(rnh);
268 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
269 RADIX_NODE_HEAD_UNLOCK(rnh);
272 * Attempt to be somewhat dynamic about this:
273 * If there are ``too many'' routes sitting around taking up space,
274 * then crank down the timeout, and see if we can't make some more
275 * go away. However, we make sure that we will never adjust more
276 * than once in rtq_timeout seconds, to keep from cranking down too
279 if ((arg.found - arg.killed > rtq_toomany) &&
280 (time_second - last_adjusted_timeout >= rtq_timeout) &&
281 rtq_reallyold > rtq_minreallyold) {
282 rtq_reallyold = 2 * rtq_reallyold / 3;
283 if (rtq_reallyold < rtq_minreallyold) {
284 rtq_reallyold = rtq_minreallyold;
287 last_adjusted_timeout = time_second;
289 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
292 arg.found = arg.killed = 0;
294 RADIX_NODE_HEAD_LOCK(rnh);
295 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
296 RADIX_NODE_HEAD_UNLOCK(rnh);
300 atv.tv_sec = arg.nextstop - time_second;
301 callout_reset(&rtq_timer, tvtohz(&atv), in_rtqtimo, rock);
307 struct radix_node_head *rnh = rt_tables[AF_INET];
310 arg.found = arg.killed = 0;
315 RADIX_NODE_HEAD_LOCK(rnh);
316 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
317 RADIX_NODE_HEAD_UNLOCK(rnh);
321 * Initialize our routing tree.
324 in_inithead(void **head, int off)
326 struct radix_node_head *rnh;
328 if (!rn_inithead(head, off))
331 if (head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
332 return 1; /* only do this for the real routing table */
335 rnh->rnh_addaddr = in_addroute;
336 rnh->rnh_matchaddr = in_matroute;
337 rnh->rnh_close = in_clsroute;
338 callout_init(&rtq_timer, CALLOUT_MPSAFE);
339 in_rtqtimo(rnh); /* kick off timeout first time */
344 * This zaps old routes when the interface goes down or interface
345 * address is deleted. In the latter case, it deletes static routes
346 * that point to this address. If we don't do this, we may end up
347 * using the old address in the future. The ones we always want to
348 * get rid of are things like ARP entries, since the user might down
349 * the interface, walk over to a completely different network, and
352 struct in_ifadown_arg {
353 struct radix_node_head *rnh;
359 in_ifadownkill(struct radix_node *rn, void *xap)
361 struct in_ifadown_arg *ap = xap;
362 struct rtentry *rt = (struct rtentry *)rn;
365 if (rt->rt_ifa == ap->ifa &&
366 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
368 * We need to disable the automatic prune that happens
369 * in this case in rtrequest() because it will blow
370 * away the pointers that rn_walktree() needs in order
371 * continue our descent. We will end up deleting all
372 * the routes that rtrequest() would have in any case,
373 * so that behavior is not needed there.
375 rt->rt_flags &= ~RTF_CLONING;
383 in_ifadown(struct ifaddr *ifa, int delete)
385 struct in_ifadown_arg arg;
386 struct radix_node_head *rnh;
388 if (ifa->ifa_addr->sa_family != AF_INET)
391 arg.rnh = rnh = rt_tables[AF_INET];
394 RADIX_NODE_HEAD_LOCK(rnh);
395 rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
396 RADIX_NODE_HEAD_UNLOCK(rnh);
397 ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */