Clean up routing code before I parallelize it.
[dragonfly.git] / sys / netinet / in_rmx.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.3 2002/08/09 14:49:23 ru Exp $
2e9572df 30 * $DragonFly: src/sys/netinet/in_rmx.c,v 1.6 2004/12/14 18:46:08 hsu Exp $
984263bc
MD
31 */
32
33/*
34 * This code does two things necessary for the enhanced TCP metrics to
35 * function in a useful manner:
36 * 1) It marks all non-host routes as `cloning', thus ensuring that
37 * every actual reference to such a route actually gets turned
38 * into a reference to a host route to the specific destination
39 * requested.
40 * 2) When such routes lose all their references, it arranges for them
41 * to be deleted in some random collection of circumstances, so that
42 * a large quantity of stale routing data is not kept in kernel memory
43 * indefinitely. See in_rtqtimo() below for the exact mechanism.
44 */
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
54#include <net/if.h>
55#include <net/route.h>
56#include <netinet/in.h>
57#include <netinet/in_var.h>
58#include <netinet/ip_var.h>
59
2e9572df 60#define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
ac311540 61
2e9572df 62static struct callout in_rtqtimo_ch;
984263bc
MD
63
64/*
65 * Do what we need to do when inserting a route.
66 */
67static struct radix_node *
2e9572df 68in_addroute(char *key, char *mask, struct radix_node_head *head,
984263bc
MD
69 struct radix_node *treenodes)
70{
71 struct rtentry *rt = (struct rtentry *)treenodes;
72 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
73 struct radix_node *ret;
74
75 /*
76 * For IP, all unicast non-host routes are automatically cloning.
77 */
2e9572df 78 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
984263bc
MD
79 rt->rt_flags |= RTF_MULTICAST;
80
2e9572df 81 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
984263bc
MD
82 rt->rt_flags |= RTF_PRCLONING;
83 }
84
85 /*
86 * A little bit of help for both IP output and input:
87 * For host routes, we make sure that RTF_BROADCAST
88 * is set for anything that looks like a broadcast address.
89 * This way, we can avoid an expensive call to in_broadcast()
90 * in ip_output() most of the time (because the route passed
91 * to ip_output() is almost always a host route).
92 *
93 * We also do the same for local addresses, with the thought
94 * that this might one day be used to speed up ip_input().
95 *
96 * We also mark routes to multicast addresses as such, because
97 * it's easy to do and might be useful (but this is much more
98 * dubious since it's so easy to inspect the address). (This
99 * is done above.)
100 */
101 if (rt->rt_flags & RTF_HOST) {
102 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
103 rt->rt_flags |= RTF_BROADCAST;
104 } else {
105 if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
106 == sin->sin_addr.s_addr)
107 rt->rt_flags |= RTF_LOCAL;
108 }
109 }
110
2e9572df
JH
111 if (rt->rt_rmx.rmx_mtu != 0 && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
112 rt->rt_ifp != NULL)
984263bc
MD
113 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
114
2e9572df 115 ret = rn_addroute(key, mask, head, treenodes);
984263bc
MD
116 if (ret == NULL && rt->rt_flags & RTF_HOST) {
117 struct rtentry *rt2;
118 /*
119 * We are trying to add a host route, but can't.
120 * Find out if it is because of an
121 * ARP entry and delete it if so.
122 */
123 rt2 = rtalloc1((struct sockaddr *)sin, 0,
124 RTF_CLONING | RTF_PRCLONING);
125 if (rt2) {
126 if (rt2->rt_flags & RTF_LLINFO &&
127 rt2->rt_flags & RTF_HOST &&
128 rt2->rt_gateway &&
129 rt2->rt_gateway->sa_family == AF_LINK) {
130 rtrequest(RTM_DELETE,
131 (struct sockaddr *)rt_key(rt2),
132 rt2->rt_gateway,
133 rt_mask(rt2), rt2->rt_flags, 0);
2e9572df 134 ret = rn_addroute(key, mask, head,
984263bc
MD
135 treenodes);
136 }
137 RTFREE(rt2);
138 }
139 }
140
141 /*
142 * If the new route created successfully, and we are forwarding,
143 * and there is a cached route, free it. Otherwise, we may end
144 * up using the wrong route.
145 */
2e9572df 146 if (ret != NULL && ipforwarding && ipforward_rt.ro_rt != NULL) {
984263bc 147 RTFREE(ipforward_rt.ro_rt);
2e9572df 148 ipforward_rt.ro_rt = NULL;
984263bc
MD
149 }
150
151 return ret;
152}
153
154/*
155 * This code is the inverse of in_clsroute: on first reference, if we
156 * were managing the route, stop doing so and set the expiration timer
157 * back off again.
158 */
159static struct radix_node *
2e9572df 160in_matchroute(char *key, struct radix_node_head *head)
984263bc 161{
2e9572df 162 struct radix_node *rn = rn_match(key, head);
984263bc
MD
163 struct rtentry *rt = (struct rtentry *)rn;
164
2e9572df
JH
165 if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
166 if (rt->rt_flags & RTPRF_OURS) {
984263bc
MD
167 rt->rt_flags &= ~RTPRF_OURS;
168 rt->rt_rmx.rmx_expire = 0;
169 }
170 }
171 return rn;
172}
173
2e9572df
JH
174static int rtq_reallyold = 60*60; /* one hour is ``really old'' */
175SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
176 &rtq_reallyold , 0,
984263bc 177 "Default expiration time on dynamically learned routes");
2e9572df
JH
178
179static int rtq_minreallyold = 10; /* never automatically crank down to less */
180SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
181 &rtq_minreallyold , 0,
984263bc 182 "Minimum time to attempt to hold onto dynamically learned routes");
2e9572df
JH
183
184static int rtq_toomany = 128; /* 128 cached routes is ``too many'' */
185SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
984263bc
MD
186 &rtq_toomany , 0, "Upper limit on dynamically learned routes");
187
188/*
189 * On last reference drop, mark the route as belong to us so that it can be
190 * timed out.
191 */
192static void
193in_clsroute(struct radix_node *rn, struct radix_node_head *head)
194{
195 struct rtentry *rt = (struct rtentry *)rn;
196
2e9572df 197 if (!(rt->rt_flags & RTF_UP))
984263bc
MD
198 return; /* prophylactic measures */
199
2e9572df 200 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
984263bc
MD
201 return;
202
2e9572df 203 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
984263bc
MD
204 return;
205
206 /*
207 * As requested by David Greenman:
208 * If rtq_reallyold is 0, just delete the route without
209 * waiting for a timeout cycle to kill it.
210 */
2e9572df 211 if (rtq_reallyold != 0) {
984263bc
MD
212 rt->rt_flags |= RTPRF_OURS;
213 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
214 } else {
215 rtrequest(RTM_DELETE,
216 (struct sockaddr *)rt_key(rt),
217 rt->rt_gateway, rt_mask(rt),
218 rt->rt_flags, 0);
219 }
220}
221
222struct rtqk_arg {
223 struct radix_node_head *rnh;
224 int draining;
225 int killed;
226 int found;
227 int updating;
228 time_t nextstop;
229};
230
231/*
232 * Get rid of old routes. When draining, this deletes everything, even when
233 * the timeout is not expired yet. When updating, this makes sure that
234 * nothing has a timeout longer than the current value of rtq_reallyold.
235 */
236static int
237in_rtqkill(struct radix_node *rn, void *rock)
238{
239 struct rtqk_arg *ap = rock;
240 struct rtentry *rt = (struct rtentry *)rn;
241 int err;
242
2e9572df 243 if (rt->rt_flags & RTPRF_OURS) {
984263bc
MD
244 ap->found++;
245
2e9572df
JH
246 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
247 if (rt->rt_refcnt > 0)
984263bc
MD
248 panic("rtqkill route really not free");
249
250 err = rtrequest(RTM_DELETE,
251 (struct sockaddr *)rt_key(rt),
252 rt->rt_gateway, rt_mask(rt),
253 rt->rt_flags, 0);
2e9572df 254 if (err) {
984263bc
MD
255 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
256 } else {
257 ap->killed++;
258 }
259 } else {
2e9572df
JH
260 if (ap->updating &&
261 (rt->rt_rmx.rmx_expire - time_second >
262 rtq_reallyold)) {
263 rt->rt_rmx.rmx_expire = time_second +
264 rtq_reallyold;
984263bc
MD
265 }
266 ap->nextstop = lmin(ap->nextstop,
267 rt->rt_rmx.rmx_expire);
268 }
269 }
270
271 return 0;
272}
273
274#define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
275static int rtq_timeout = RTQ_TIMEOUT;
276
277static void
278in_rtqtimo(void *rock)
279{
280 struct radix_node_head *rnh = rock;
281 struct rtqk_arg arg;
282 struct timeval atv;
283 static time_t last_adjusted_timeout = 0;
284 int s;
285
286 arg.found = arg.killed = 0;
287 arg.rnh = rnh;
288 arg.nextstop = time_second + rtq_timeout;
289 arg.draining = arg.updating = 0;
290 s = splnet();
291 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
292 splx(s);
293
294 /*
295 * Attempt to be somewhat dynamic about this:
296 * If there are ``too many'' routes sitting around taking up space,
297 * then crank down the timeout, and see if we can't make some more
298 * go away. However, we make sure that we will never adjust more
299 * than once in rtq_timeout seconds, to keep from cranking down too
300 * hard.
301 */
2e9572df
JH
302 if ((arg.found - arg.killed > rtq_toomany) &&
303 (time_second - last_adjusted_timeout >= rtq_timeout) &&
304 rtq_reallyold > rtq_minreallyold) {
984263bc 305 rtq_reallyold = 2*rtq_reallyold / 3;
2e9572df 306 if (rtq_reallyold < rtq_minreallyold) {
984263bc
MD
307 rtq_reallyold = rtq_minreallyold;
308 }
309
310 last_adjusted_timeout = time_second;
311#ifdef DIAGNOSTIC
312 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
313 rtq_reallyold);
314#endif
315 arg.found = arg.killed = 0;
316 arg.updating = 1;
317 s = splnet();
318 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
319 splx(s);
320 }
321
322 atv.tv_usec = 0;
323 atv.tv_sec = arg.nextstop - time_second;
ac311540 324 callout_reset(&in_rtqtimo_ch, tvtohz_high(&atv), in_rtqtimo, rock);
984263bc
MD
325}
326
327void
328in_rtqdrain(void)
329{
330 struct radix_node_head *rnh = rt_tables[AF_INET];
331 struct rtqk_arg arg;
332 int s;
2e9572df 333
984263bc
MD
334 arg.found = arg.killed = 0;
335 arg.rnh = rnh;
336 arg.nextstop = 0;
337 arg.draining = 1;
338 arg.updating = 0;
339 s = splnet();
340 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
341 splx(s);
342}
343
344/*
345 * Initialize our routing tree.
346 */
347int
348in_inithead(void **head, int off)
349{
350 struct radix_node_head *rnh;
351
2e9572df 352 if (!rn_inithead(head, off))
984263bc
MD
353 return 0;
354
2e9572df 355 if (head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
984263bc
MD
356 return 1; /* only do this for the real routing table */
357
358 rnh = *head;
359 rnh->rnh_addaddr = in_addroute;
2e9572df 360 rnh->rnh_matchaddr = in_matchroute;
984263bc 361 rnh->rnh_close = in_clsroute;
ac311540 362 callout_init(&in_rtqtimo_ch);
984263bc
MD
363 in_rtqtimo(rnh); /* kick off timeout first time */
364 return 1;
365}
366
367\f
368/*
369 * This zaps old routes when the interface goes down or interface
370 * address is deleted. In the latter case, it deletes static routes
371 * that point to this address. If we don't do this, we may end up
372 * using the old address in the future. The ones we always want to
373 * get rid of are things like ARP entries, since the user might down
374 * the interface, walk over to a completely different network, and
375 * plug back in.
376 */
377struct in_ifadown_arg {
378 struct radix_node_head *rnh;
379 struct ifaddr *ifa;
380 int del;
381};
382
383static int
384in_ifadownkill(struct radix_node *rn, void *xap)
385{
386 struct in_ifadown_arg *ap = xap;
387 struct rtentry *rt = (struct rtentry *)rn;
388 int err;
389
390 if (rt->rt_ifa == ap->ifa &&
391 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
392 /*
393 * We need to disable the automatic prune that happens
394 * in this case in rtrequest() because it will blow
395 * away the pointers that rn_walktree() needs in order
396 * continue our descent. We will end up deleting all
397 * the routes that rtrequest() would have in any case,
398 * so that behavior is not needed there.
399 */
400 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
401 err = rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
402 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
403 if (err) {
404 log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
405 }
406 }
407 return 0;
408}
409
410int
411in_ifadown(struct ifaddr *ifa, int delete)
412{
413 struct in_ifadown_arg arg;
414 struct radix_node_head *rnh;
415
416 if (ifa->ifa_addr->sa_family != AF_INET)
417 return 1;
418
419 arg.rnh = rnh = rt_tables[AF_INET];
420 arg.ifa = ifa;
421 arg.del = delete;
422 rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
423 ifa->ifa_flags &= ~IFA_ROUTE;
424 return 0;
425}