FreeBSD/Linux Kernel Cross Reference
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
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 = rtalloc1((struct sockaddr *)sin, 0, RTF_CLONING);
114 if (rt2) {
115 if (rt2->rt_flags & RTF_LLINFO &&
116 rt2->rt_flags & RTF_HOST &&
117 rt2->rt_gateway &&
118 rt2->rt_gateway->sa_family == AF_LINK) {
119 rtexpunge(rt2);
120 RTFREE_LOCKED(rt2);
121 ret = rn_addroute(v_arg, n_arg, head,
122 treenodes);
123 } else
124 RTFREE_LOCKED(rt2);
125 }
126 }
127
128 return ret;
129 }
130
131 /*
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
134 * back off again.
135 */
136 static struct radix_node *
137 in_matroute(void *v_arg, struct radix_node_head *head)
138 {
139 struct radix_node *rn = rn_match(v_arg, head);
140 struct rtentry *rt = (struct rtentry *)rn;
141
142 /*XXX locking? */
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;
147 }
148 }
149 return rn;
150 }
151
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");
155
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");
160
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");
164
165 /*
166 * On last reference drop, mark the route as belong to us so that it can be
167 * timed out.
168 */
169 static void
170 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
171 {
172 struct rtentry *rt = (struct rtentry *)rn;
173
174 RT_LOCK_ASSERT(rt);
175
176 if (!(rt->rt_flags & RTF_UP))
177 return; /* prophylactic measures */
178
179 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
180 return;
181
182 if (rt->rt_flags & RTPRF_OURS)
183 return;
184
185 if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC)))
186 return;
187
188 /*
189 * If rtq_reallyold is 0, just delete the route without
190 * waiting for a timeout cycle to kill it.
191 */
192 if (rtq_reallyold != 0) {
193 rt->rt_flags |= RTPRF_OURS;
194 rt->rt_rmx.rmx_expire = time_uptime + rtq_reallyold;
195 } else {
196 rtexpunge(rt);
197 }
198 }
199
200 struct rtqk_arg {
201 struct radix_node_head *rnh;
202 int draining;
203 int killed;
204 int found;
205 int updating;
206 time_t nextstop;
207 };
208
209 /*
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.
213 */
214 static int
215 in_rtqkill(struct radix_node *rn, void *rock)
216 {
217 struct rtqk_arg *ap = rock;
218 struct rtentry *rt = (struct rtentry *)rn;
219 int err;
220
221 if (rt->rt_flags & RTPRF_OURS) {
222 ap->found++;
223
224 if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
225 if (rt->rt_refcnt > 0)
226 panic("rtqkill route really not free");
227
228 err = rtrequest(RTM_DELETE,
229 (struct sockaddr *)rt_key(rt),
230 rt->rt_gateway, rt_mask(rt),
231 rt->rt_flags, 0);
232 if (err) {
233 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
234 } else {
235 ap->killed++;
236 }
237 } else {
238 if (ap->updating &&
239 (rt->rt_rmx.rmx_expire - time_uptime >
240 rtq_reallyold)) {
241 rt->rt_rmx.rmx_expire =
242 time_uptime + rtq_reallyold;
243 }
244 ap->nextstop = lmin(ap->nextstop,
245 rt->rt_rmx.rmx_expire);
246 }
247 }
248
249 return 0;
250 }
251
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;
255
256 static void
257 in_rtqtimo(void *rock)
258 {
259 struct radix_node_head *rnh = rock;
260 struct rtqk_arg arg;
261 struct timeval atv;
262 static time_t last_adjusted_timeout = 0;
263
264 arg.found = arg.killed = 0;
265 arg.rnh = rnh;
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);
271
272 /*
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
278 * hard.
279 */
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;
286 }
287
288 last_adjusted_timeout = time_uptime;
289 #ifdef DIAGNOSTIC
290 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
291 rtq_reallyold);
292 #endif
293 arg.found = arg.killed = 0;
294 arg.updating = 1;
295 RADIX_NODE_HEAD_LOCK(rnh);
296 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
297 RADIX_NODE_HEAD_UNLOCK(rnh);
298 }
299
300 atv.tv_usec = 0;
301 atv.tv_sec = arg.nextstop - time_uptime;
302 callout_reset(&rtq_timer, tvtohz(&atv), in_rtqtimo, rock);
303 }
304
305 void
306 in_rtqdrain(void)
307 {
308 struct radix_node_head *rnh = rt_tables[AF_INET];
309 struct rtqk_arg arg;
310
311 arg.found = arg.killed = 0;
312 arg.rnh = rnh;
313 arg.nextstop = 0;
314 arg.draining = 1;
315 arg.updating = 0;
316 RADIX_NODE_HEAD_LOCK(rnh);
317 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
318 RADIX_NODE_HEAD_UNLOCK(rnh);
319 }
320
321 /*
322 * Initialize our routing tree.
323 */
324 int
325 in_inithead(void **head, int off)
326 {
327 struct radix_node_head *rnh;
328
329 if (!rn_inithead(head, off))
330 return 0;
331
332 if (head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
333 return 1; /* only do this for the real routing table */
334
335 rnh = *head;
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 */
341 return 1;
342 }
343
344 /*
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
351 * plug back in.
352 */
353 struct in_ifadown_arg {
354 struct radix_node_head *rnh;
355 struct ifaddr *ifa;
356 int del;
357 };
358
359 static int
360 in_ifadownkill(struct radix_node *rn, void *xap)
361 {
362 struct in_ifadown_arg *ap = xap;
363 struct rtentry *rt = (struct rtentry *)rn;
364
365 RT_LOCK(rt);
366 if (rt->rt_ifa == ap->ifa &&
367 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
368 /*
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.
375 */
376 rt->rt_flags &= ~RTF_CLONING;
377 rtexpunge(rt);
378 }
379 RT_UNLOCK(rt);
380 return 0;
381 }
382
383 int
384 in_ifadown(struct ifaddr *ifa, int delete)
385 {
386 struct in_ifadown_arg arg;
387 struct radix_node_head *rnh;
388
389 if (ifa->ifa_addr->sa_family != AF_INET)
390 return 1;
391
392 arg.rnh = rnh = rt_tables[AF_INET];
393 arg.ifa = ifa;
394 arg.del = delete;
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? */
399 return 0;
400 }
Cache object: cb6cb69ac188a8ccaaa7cb50deed5a0c
|