Remove mdoc.local, we need to make this truly local.
[dragonfly.git] / sys / netinet / in_rmx.c
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 $
30  * $DragonFly: src/sys/netinet/in_rmx.c,v 1.12 2005/06/02 23:52:42 dillon Exp $
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 #include <sys/thread2.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 #define RTPRF_EXPIRING  RTF_PROTO3      /* set on routes we manage */
62
63 static struct callout in_rtqtimo_ch;
64
65 /*
66  * Do what we need to do when inserting a route.
67  */
68 static struct radix_node *
69 in_addroute(char *key, char *mask, 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          * For IP, all unicast non-host routes are automatically cloning.
78          */
79         if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
80                 rt->rt_flags |= RTF_MULTICAST;
81
82         if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST)))
83                 rt->rt_flags |= RTF_PRCLONING;
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
111         if (rt->rt_rmx.rmx_mtu != 0 && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
112             rt->rt_ifp != NULL)
113                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
114
115         ret = rn_addroute(key, mask, head, treenodes);
116         if (ret == NULL && rt->rt_flags & RTF_HOST) {
117                 struct rtentry *oldrt;
118
119                 /*
120                  * We are trying to add a host route, but can't.
121                  * Find out if it is because of an ARP entry and
122                  * delete it if so.
123                  */
124                 oldrt = rtpurelookup((struct sockaddr *)sin);
125                 if (oldrt != NULL) {
126                         --oldrt->rt_refcnt;
127                         if (oldrt->rt_flags & RTF_LLINFO &&
128                             oldrt->rt_flags & RTF_HOST &&
129                             oldrt->rt_gateway &&
130                             oldrt->rt_gateway->sa_family == AF_LINK) {
131                                 rtrequest(RTM_DELETE, rt_key(oldrt),
132                                           oldrt->rt_gateway, rt_mask(oldrt),
133                                           oldrt->rt_flags, NULL);
134                                 ret = rn_addroute(key, mask, head, treenodes);
135                         }
136                 }
137         }
138
139         /*
140          * If the new route created successfully, and we are forwarding,
141          * and there is a cached route, free it.  Otherwise, we may end
142          * up using the wrong route.
143          */
144         if (ret != NULL && ipforwarding && ipforward_rt.ro_rt != NULL) {
145                 RTFREE(ipforward_rt.ro_rt);
146                 ipforward_rt.ro_rt = NULL;
147         }
148
149         return ret;
150 }
151
152 /*
153  * This code is the inverse of in_closeroute: on first reference, if we
154  * were managing the route, stop doing so and set the expiration timer
155  * back off again.
156  */
157 static struct radix_node *
158 in_matchroute(char *key, struct radix_node_head *head)
159 {
160         struct radix_node *rn = rn_match(key, head);
161         struct rtentry *rt = (struct rtentry *)rn;
162
163         if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
164                 if (rt->rt_flags & RTPRF_EXPIRING) {
165                         rt->rt_flags &= ~RTPRF_EXPIRING;
166                         rt->rt_rmx.rmx_expire = 0;
167                 }
168         }
169         return rn;
170 }
171
172 static int rtq_reallyold = 60*60;  /* one hour is ``really old'' */
173 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
174     &rtq_reallyold , 0,
175     "Default expiration time on cloned routes");
176
177 static int rtq_minreallyold = 10;  /* never automatically crank down to less */
178 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
179     &rtq_minreallyold , 0,
180     "Minimum time to attempt to hold onto cloned routes");
181
182 static int rtq_toomany = 128;      /* 128 cached routes is ``too many'' */
183 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
184     &rtq_toomany , 0, "Upper limit on cloned routes");
185
186 /*
187  * On last reference drop, mark the route as belong to us so that it can be
188  * timed out.
189  */
190 static void
191 in_closeroute(struct radix_node *rn, struct radix_node_head *head)
192 {
193         struct rtentry *rt = (struct rtentry *)rn;
194
195         if (!(rt->rt_flags & RTF_UP))
196                 return;         /* prophylactic measures */
197
198         if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
199                 return;
200
201         if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_EXPIRING)) != RTF_WASCLONED)
202                 return;
203
204         /*
205          * As requested by David Greenman:
206          * If rtq_reallyold is 0, just delete the route without
207          * waiting for a timeout cycle to kill it.
208          */
209         if (rtq_reallyold != 0) {
210                 rt->rt_flags |= RTPRF_EXPIRING;
211                 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
212         } else {
213                 /*
214                  * Remove route from the radix tree, but defer deallocation
215                  * until we return to rtfree().
216                  */
217                 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
218                           rt->rt_flags, &rt);
219         }
220 }
221
222 struct 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  */
236 static int
237 in_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
243         if (rt->rt_flags & RTPRF_EXPIRING) {
244                 ap->found++;
245                 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
246                         if (rt->rt_refcnt > 0)
247                                 panic("rtqkill route really not free");
248
249                         err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
250                                         rt_mask(rt), rt->rt_flags, NULL);
251                         if (err)
252                                 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
253                         else
254                                 ap->killed++;
255                 } else {
256                         if (ap->updating &&
257                             (rt->rt_rmx.rmx_expire - time_second >
258                              rtq_reallyold)) {
259                                 rt->rt_rmx.rmx_expire = time_second +
260                                     rtq_reallyold;
261                         }
262                         ap->nextstop = lmin(ap->nextstop,
263                                             rt->rt_rmx.rmx_expire);
264                 }
265         }
266
267         return 0;
268 }
269
270 #define RTQ_TIMEOUT     60*10   /* run no less than once every ten minutes */
271 static int rtq_timeout = RTQ_TIMEOUT;
272
273 static void
274 in_rtqtimo(void *rock)
275 {
276         struct radix_node_head *rnh = rock;
277         struct rtqk_arg arg;
278         struct timeval atv;
279         static time_t last_adjusted_timeout = 0;
280
281         arg.found = arg.killed = 0;
282         arg.rnh = rnh;
283         arg.nextstop = time_second + rtq_timeout;
284         arg.draining = arg.updating = 0;
285         crit_enter();
286         rnh->rnh_walktree(rnh, in_rtqkill, &arg);
287         crit_exit();
288
289         /*
290          * Attempt to be somewhat dynamic about this:
291          * If there are ``too many'' routes sitting around taking up space,
292          * then crank down the timeout, and see if we can't make some more
293          * go away.  However, we make sure that we will never adjust more
294          * than once in rtq_timeout seconds, to keep from cranking down too
295          * hard.
296          */
297         if ((arg.found - arg.killed > rtq_toomany) &&
298             (time_second - last_adjusted_timeout >= rtq_timeout) &&
299             rtq_reallyold > rtq_minreallyold) {
300                 rtq_reallyold = 2*rtq_reallyold / 3;
301                 if (rtq_reallyold < rtq_minreallyold) {
302                         rtq_reallyold = rtq_minreallyold;
303                 }
304
305                 last_adjusted_timeout = time_second;
306 #ifdef DIAGNOSTIC
307                 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
308                     rtq_reallyold);
309 #endif
310                 arg.found = arg.killed = 0;
311                 arg.updating = 1;
312                 crit_enter();
313                 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
314                 crit_exit();
315         }
316
317         atv.tv_usec = 0;
318         atv.tv_sec = arg.nextstop - time_second;
319         callout_reset(&in_rtqtimo_ch, tvtohz_high(&atv), in_rtqtimo, rock);
320 }
321
322 void
323 in_rtqdrain(void)
324 {
325         struct radix_node_head *rnh = rt_tables[AF_INET];
326         struct rtqk_arg arg;
327
328         arg.found = arg.killed = 0;
329         arg.rnh = rnh;
330         arg.nextstop = 0;
331         arg.draining = 1;
332         arg.updating = 0;
333         crit_enter();
334         rnh->rnh_walktree(rnh, in_rtqkill, &arg);
335         crit_exit();
336 }
337
338 /*
339  * Initialize our routing tree.
340  */
341 int
342 in_inithead(void **head, int off)
343 {
344         struct radix_node_head *rnh;
345
346         if (!rn_inithead(head, off))
347                 return 0;
348
349         if (head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
350                 return 1;       /* only do this for the real routing table */
351
352         rnh = *head;
353         rnh->rnh_addaddr = in_addroute;
354         rnh->rnh_matchaddr = in_matchroute;
355         rnh->rnh_close = in_closeroute;
356         callout_init(&in_rtqtimo_ch);
357         in_rtqtimo(rnh);        /* kick off timeout first time */
358         return 1;
359 }
360
361 /*
362  * This zaps old routes when the interface goes down or interface
363  * address is deleted.  In the latter case, it deletes static routes
364  * that point to this address.  If we don't do this, we may end up
365  * using the old address in the future.  The ones we always want to
366  * get rid of are things like ARP entries, since the user might down
367  * the interface, walk over to a completely different network, and
368  * plug back in.
369  */
370 struct in_ifadown_arg {
371         struct radix_node_head *rnh;
372         struct ifaddr *ifa;
373         int del;
374 };
375
376 static int
377 in_ifadownkill(struct radix_node *rn, void *xap)
378 {
379         struct in_ifadown_arg *ap = xap;
380         struct rtentry *rt = (struct rtentry *)rn;
381         int err;
382
383         if (rt->rt_ifa == ap->ifa &&
384             (ap->del || !(rt->rt_flags & RTF_STATIC))) {
385                 /*
386                  * We need to disable the automatic prune that happens
387                  * in this case in rtrequest() because it will blow
388                  * away the pointers that rn_walktree() needs in order
389                  * continue our descent.  We will end up deleting all
390                  * the routes that rtrequest() would have in any case,
391                  * so that behavior is not needed there.
392                  */
393                 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
394                 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
395                                 rt_mask(rt), rt->rt_flags, NULL);
396                 if (err)
397                         log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
398         }
399         return 0;
400 }
401
402 int
403 in_ifadown(struct ifaddr *ifa, int delete)
404 {
405         struct in_ifadown_arg arg;
406         struct radix_node_head *rnh;
407
408         if (ifa->ifa_addr->sa_family != AF_INET)
409                 return 1;
410
411         arg.rnh = rnh = rt_tables[AF_INET];
412         arg.ifa = ifa;
413         arg.del = delete;
414         rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
415         ifa->ifa_flags &= ~IFA_ROUTE;
416         return 0;
417 }