1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// src/sys/netinet/in_rmx.c
|
4 |
|
|
//
|
5 |
|
|
//==========================================================================
|
6 |
|
|
//####BSDCOPYRIGHTBEGIN####
|
7 |
|
|
//
|
8 |
|
|
// -------------------------------------------
|
9 |
|
|
//
|
10 |
|
|
// Portions of this software may have been derived from OpenBSD,
|
11 |
|
|
// FreeBSD or other sources, and are covered by the appropriate
|
12 |
|
|
// copyright disclaimers included herein.
|
13 |
|
|
//
|
14 |
|
|
// Portions created by Red Hat are
|
15 |
|
|
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
|
16 |
|
|
//
|
17 |
|
|
// -------------------------------------------
|
18 |
|
|
//
|
19 |
|
|
//####BSDCOPYRIGHTEND####
|
20 |
|
|
//==========================================================================
|
21 |
|
|
|
22 |
|
|
/*
|
23 |
|
|
* Copyright 1994, 1995 Massachusetts Institute of Technology
|
24 |
|
|
*
|
25 |
|
|
* Permission to use, copy, modify, and distribute this software and
|
26 |
|
|
* its documentation for any purpose and without fee is hereby
|
27 |
|
|
* granted, provided that both the above copyright notice and this
|
28 |
|
|
* permission notice appear in all copies, that both the above
|
29 |
|
|
* copyright notice and this permission notice appear in all
|
30 |
|
|
* supporting documentation, and that the name of M.I.T. not be used
|
31 |
|
|
* in advertising or publicity pertaining to distribution of the
|
32 |
|
|
* software without specific, written prior permission. M.I.T. makes
|
33 |
|
|
* no representations about the suitability of this software for any
|
34 |
|
|
* purpose. It is provided "as is" without express or implied
|
35 |
|
|
* warranty.
|
36 |
|
|
*
|
37 |
|
|
* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
|
38 |
|
|
* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
39 |
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
40 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
|
41 |
|
|
* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
42 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
43 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
44 |
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
45 |
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
46 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
47 |
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
48 |
|
|
* SUCH DAMAGE.
|
49 |
|
|
*
|
50 |
|
|
* $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.1 2001/05/14 08:23:49 ru Exp $
|
51 |
|
|
*/
|
52 |
|
|
|
53 |
|
|
/*
|
54 |
|
|
* This code does two things necessary for the enhanced TCP metrics to
|
55 |
|
|
* function in a useful manner:
|
56 |
|
|
* 1) It marks all non-host routes as `cloning', thus ensuring that
|
57 |
|
|
* every actual reference to such a route actually gets turned
|
58 |
|
|
* into a reference to a host route to the specific destination
|
59 |
|
|
* requested.
|
60 |
|
|
* 2) When such routes lose all their references, it arranges for them
|
61 |
|
|
* to be deleted in some random collection of circumstances, so that
|
62 |
|
|
* a large quantity of stale routing data is not kept in kernel memory
|
63 |
|
|
* indefinitely. See in_rtqtimo() below for the exact mechanism.
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
#include <sys/param.h>
|
67 |
|
|
#include <sys/socket.h>
|
68 |
|
|
#include <sys/mbuf.h>
|
69 |
|
|
|
70 |
|
|
#include <net/if.h>
|
71 |
|
|
#include <net/route.h>
|
72 |
|
|
#include <netinet/in.h>
|
73 |
|
|
#include <netinet/in_var.h>
|
74 |
|
|
|
75 |
|
|
extern int in_inithead __P((void **head, int off));
|
76 |
|
|
|
77 |
|
|
#define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
|
78 |
|
|
|
79 |
|
|
/*
|
80 |
|
|
* Do what we need to do when inserting a route.
|
81 |
|
|
*/
|
82 |
|
|
static struct radix_node *
|
83 |
|
|
in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
|
84 |
|
|
struct radix_node *treenodes)
|
85 |
|
|
{
|
86 |
|
|
struct rtentry *rt = (struct rtentry *)treenodes;
|
87 |
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
|
88 |
|
|
struct radix_node *ret;
|
89 |
|
|
|
90 |
|
|
/*
|
91 |
|
|
* For IP, all unicast non-host routes are automatically cloning.
|
92 |
|
|
*/
|
93 |
|
|
if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
|
94 |
|
|
rt->rt_flags |= RTF_MULTICAST;
|
95 |
|
|
|
96 |
|
|
if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
|
97 |
|
|
rt->rt_flags |= RTF_PRCLONING;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/*
|
101 |
|
|
* A little bit of help for both IP output and input:
|
102 |
|
|
* For host routes, we make sure that RTF_BROADCAST
|
103 |
|
|
* is set for anything that looks like a broadcast address.
|
104 |
|
|
* This way, we can avoid an expensive call to in_broadcast()
|
105 |
|
|
* in ip_output() most of the time (because the route passed
|
106 |
|
|
* to ip_output() is almost always a host route).
|
107 |
|
|
*
|
108 |
|
|
* We also do the same for local addresses, with the thought
|
109 |
|
|
* that this might one day be used to speed up ip_input().
|
110 |
|
|
*
|
111 |
|
|
* We also mark routes to multicast addresses as such, because
|
112 |
|
|
* it's easy to do and might be useful (but this is much more
|
113 |
|
|
* dubious since it's so easy to inspect the address). (This
|
114 |
|
|
* is done above.)
|
115 |
|
|
*/
|
116 |
|
|
if (rt->rt_flags & RTF_HOST) {
|
117 |
|
|
if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
|
118 |
|
|
rt->rt_flags |= RTF_BROADCAST;
|
119 |
|
|
} else {
|
120 |
|
|
#define satosin(sa) ((struct sockaddr_in *)sa)
|
121 |
|
|
if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
|
122 |
|
|
== sin->sin_addr.s_addr)
|
123 |
|
|
rt->rt_flags |= RTF_LOCAL;
|
124 |
|
|
#undef satosin
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
|
129 |
|
|
&& rt->rt_ifp)
|
130 |
|
|
rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
|
131 |
|
|
|
132 |
|
|
ret = rn_addroute(v_arg, n_arg, head, treenodes);
|
133 |
|
|
if (ret == NULL && rt->rt_flags & RTF_HOST) {
|
134 |
|
|
struct rtentry *rt2;
|
135 |
|
|
/*
|
136 |
|
|
* We are trying to add a host route, but can't.
|
137 |
|
|
* Find out if it is because of an
|
138 |
|
|
* ARP entry and delete it if so.
|
139 |
|
|
*/
|
140 |
|
|
rt2 = rtalloc1((struct sockaddr *)sin, 0,
|
141 |
|
|
RTF_CLONING | RTF_PRCLONING);
|
142 |
|
|
if (rt2) {
|
143 |
|
|
if (rt2->rt_flags & RTF_LLINFO &&
|
144 |
|
|
rt2->rt_flags & RTF_HOST &&
|
145 |
|
|
rt2->rt_gateway &&
|
146 |
|
|
rt2->rt_gateway->sa_family == AF_LINK) {
|
147 |
|
|
rtrequest(RTM_DELETE,
|
148 |
|
|
(struct sockaddr *)rt_key(rt2),
|
149 |
|
|
rt2->rt_gateway,
|
150 |
|
|
rt_mask(rt2), rt2->rt_flags, 0);
|
151 |
|
|
ret = rn_addroute(v_arg, n_arg, head,
|
152 |
|
|
treenodes);
|
153 |
|
|
}
|
154 |
|
|
RTFREE(rt2);
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
return ret;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
/*
|
161 |
|
|
* This code is the inverse of in_clsroute: on first reference, if we
|
162 |
|
|
* were managing the route, stop doing so and set the expiration timer
|
163 |
|
|
* back off again.
|
164 |
|
|
*/
|
165 |
|
|
static struct radix_node *
|
166 |
|
|
in_matroute(void *v_arg, struct radix_node_head *head)
|
167 |
|
|
{
|
168 |
|
|
struct radix_node *rn = rn_match(v_arg, head);
|
169 |
|
|
struct rtentry *rt = (struct rtentry *)rn;
|
170 |
|
|
|
171 |
|
|
if(rt && rt->rt_refcnt == 0) { /* this is first reference */
|
172 |
|
|
if(rt->rt_flags & RTPRF_OURS) {
|
173 |
|
|
rt->rt_flags &= ~RTPRF_OURS;
|
174 |
|
|
rt->rt_rmx.rmx_expire = 0;
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
return rn;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
static int rtq_reallyold = 60*60;
|
181 |
|
|
/* one hour is ``really old'' */
|
182 |
|
|
static int rtq_minreallyold = 10;
|
183 |
|
|
/* never automatically crank down to less */
|
184 |
|
|
static int rtq_toomany = 128;
|
185 |
|
|
/* 128 cached routes is ``too many'' */
|
186 |
|
|
|
187 |
|
|
/*
|
188 |
|
|
* On last reference drop, mark the route as belong to us so that it can be
|
189 |
|
|
* timed out.
|
190 |
|
|
*/
|
191 |
|
|
static void
|
192 |
|
|
in_clsroute(struct radix_node *rn, struct radix_node_head *head)
|
193 |
|
|
{
|
194 |
|
|
struct rtentry *rt = (struct rtentry *)rn;
|
195 |
|
|
|
196 |
|
|
if(!(rt->rt_flags & RTF_UP))
|
197 |
|
|
return; /* prophylactic measures */
|
198 |
|
|
|
199 |
|
|
if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
|
200 |
|
|
return;
|
201 |
|
|
|
202 |
|
|
if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
|
203 |
|
|
!= RTF_WASCLONED)
|
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 |
|
|
*/
|
211 |
|
|
if(rtq_reallyold != 0) {
|
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 |
|
|
|
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_OURS) {
|
244 |
|
|
ap->found++;
|
245 |
|
|
|
246 |
|
|
if(ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
|
247 |
|
|
if(rt->rt_refcnt > 0)
|
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);
|
254 |
|
|
if(err) {
|
255 |
|
|
log(LOG_WARNING, "in_rtqkill: error %d\n", err);
|
256 |
|
|
} else {
|
257 |
|
|
ap->killed++;
|
258 |
|
|
}
|
259 |
|
|
} else {
|
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;
|
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 */
|
275 |
|
|
static int rtq_timeout = RTQ_TIMEOUT;
|
276 |
|
|
|
277 |
|
|
static void
|
278 |
|
|
in_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 |
|
|
*/
|
302 |
|
|
if((arg.found - arg.killed > rtq_toomany)
|
303 |
|
|
&& (time_second - last_adjusted_timeout >= rtq_timeout)
|
304 |
|
|
&& rtq_reallyold > rtq_minreallyold) {
|
305 |
|
|
rtq_reallyold = 2*rtq_reallyold / 3;
|
306 |
|
|
if(rtq_reallyold < rtq_minreallyold) {
|
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;
|
324 |
|
|
timeout(in_rtqtimo, rock, tvtohz(&atv));
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
void
|
328 |
|
|
in_rtqdrain(void)
|
329 |
|
|
{
|
330 |
|
|
struct radix_node_head *rnh = rt_tables[AF_INET];
|
331 |
|
|
struct rtqk_arg arg;
|
332 |
|
|
int s;
|
333 |
|
|
arg.found = arg.killed = 0;
|
334 |
|
|
arg.rnh = rnh;
|
335 |
|
|
arg.nextstop = 0;
|
336 |
|
|
arg.draining = 1;
|
337 |
|
|
arg.updating = 0;
|
338 |
|
|
s = splnet();
|
339 |
|
|
rnh->rnh_walktree(rnh, in_rtqkill, &arg);
|
340 |
|
|
splx(s);
|
341 |
|
|
}
|
342 |
|
|
|
343 |
|
|
/*
|
344 |
|
|
* Initialize our routing tree.
|
345 |
|
|
*/
|
346 |
|
|
int
|
347 |
|
|
in_inithead(void **head, int off)
|
348 |
|
|
{
|
349 |
|
|
struct radix_node_head *rnh;
|
350 |
|
|
|
351 |
|
|
if(!rn_inithead(head, off))
|
352 |
|
|
return 0;
|
353 |
|
|
|
354 |
|
|
if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
|
355 |
|
|
return 1; /* only do this for the real routing table */
|
356 |
|
|
|
357 |
|
|
rnh = *head;
|
358 |
|
|
rnh->rnh_addaddr = in_addroute;
|
359 |
|
|
rnh->rnh_matchaddr = in_matroute;
|
360 |
|
|
rnh->rnh_close = in_clsroute;
|
361 |
|
|
in_rtqtimo(rnh); /* kick off timeout first time */
|
362 |
|
|
return 1;
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
/*
|
367 |
|
|
* This zaps old routes when the interface goes down or interface
|
368 |
|
|
* address is deleted. In the latter case, it deletes static routes
|
369 |
|
|
* that point to this address. If we don't do this, we may end up
|
370 |
|
|
* using the old address in the future. The ones we always want to
|
371 |
|
|
* get rid of are things like ARP entries, since the user might down
|
372 |
|
|
* the interface, walk over to a completely different network, and
|
373 |
|
|
* plug back in.
|
374 |
|
|
*/
|
375 |
|
|
struct in_ifadown_arg {
|
376 |
|
|
struct radix_node_head *rnh;
|
377 |
|
|
struct ifaddr *ifa;
|
378 |
|
|
int del;
|
379 |
|
|
};
|
380 |
|
|
|
381 |
|
|
static int
|
382 |
|
|
in_ifadownkill(struct radix_node *rn, void *xap)
|
383 |
|
|
{
|
384 |
|
|
struct in_ifadown_arg *ap = xap;
|
385 |
|
|
struct rtentry *rt = (struct rtentry *)rn;
|
386 |
|
|
int err;
|
387 |
|
|
|
388 |
|
|
if (rt->rt_ifa == ap->ifa &&
|
389 |
|
|
(ap->del || !(rt->rt_flags & RTF_STATIC))) {
|
390 |
|
|
/*
|
391 |
|
|
* We need to disable the automatic prune that happens
|
392 |
|
|
* in this case in rtrequest() because it will blow
|
393 |
|
|
* away the pointers that rn_walktree() needs in order
|
394 |
|
|
* continue our descent. We will end up deleting all
|
395 |
|
|
* the routes that rtrequest() would have in any case,
|
396 |
|
|
* so that behavior is not needed there.
|
397 |
|
|
*/
|
398 |
|
|
rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
|
399 |
|
|
err = rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
|
400 |
|
|
rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
|
401 |
|
|
if (err) {
|
402 |
|
|
log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
|
403 |
|
|
}
|
404 |
|
|
}
|
405 |
|
|
return 0;
|
406 |
|
|
}
|
407 |
|
|
|
408 |
|
|
int
|
409 |
|
|
in_ifadown(struct ifaddr *ifa, int delete)
|
410 |
|
|
{
|
411 |
|
|
struct in_ifadown_arg arg;
|
412 |
|
|
struct radix_node_head *rnh;
|
413 |
|
|
|
414 |
|
|
if (ifa->ifa_addr->sa_family != AF_INET)
|
415 |
|
|
return 1;
|
416 |
|
|
|
417 |
|
|
arg.rnh = rnh = rt_tables[AF_INET];
|
418 |
|
|
arg.ifa = ifa;
|
419 |
|
|
arg.del = delete;
|
420 |
|
|
rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
|
421 |
|
|
ifa->ifa_flags &= ~IFA_ROUTE;
|
422 |
|
|
return 0;
|
423 |
|
|
}
|