xref: /dragonfly/sys/netinet/in_rmx.c (revision 1d1731fa)
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.3 2003/08/23 11:18:00 rob 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 
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 
60 extern int	in_inithead (void **head, int off);
61 
62 #define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
63 
64 /*
65  * Do what we need to do when inserting a route.
66  */
67 static struct radix_node *
68 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
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 	 */
78 	if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
79 		rt->rt_flags |= RTF_MULTICAST;
80 
81 	if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
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 
111 	if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
112 	    && rt->rt_ifp)
113 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
114 
115 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
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);
134 				ret = rn_addroute(v_arg, n_arg, head,
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 	 */
146 	if (ret != NULL && ipforwarding && ipforward_rt.ro_rt) {
147 		RTFREE(ipforward_rt.ro_rt);
148 		ipforward_rt.ro_rt = 0;
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  */
159 static struct radix_node *
160 in_matroute(void *v_arg, struct radix_node_head *head)
161 {
162 	struct radix_node *rn = rn_match(v_arg, head);
163 	struct rtentry *rt = (struct rtentry *)rn;
164 
165 	if(rt && rt->rt_refcnt == 0) { /* this is first reference */
166 		if(rt->rt_flags & RTPRF_OURS) {
167 			rt->rt_flags &= ~RTPRF_OURS;
168 			rt->rt_rmx.rmx_expire = 0;
169 		}
170 	}
171 	return rn;
172 }
173 
174 static int rtq_reallyold = 60*60;
175 	/* one hour is ``really old'' */
176 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
177     &rtq_reallyold , 0,
178     "Default expiration time on dynamically learned routes");
179 
180 static int rtq_minreallyold = 10;
181 	/* never automatically crank down to less */
182 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
183     &rtq_minreallyold , 0,
184     "Minimum time to attempt to hold onto dynamically learned routes");
185 
186 static int rtq_toomany = 128;
187 	/* 128 cached routes is ``too many'' */
188 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
189     &rtq_toomany , 0, "Upper limit on dynamically learned routes");
190 
191 /*
192  * On last reference drop, mark the route as belong to us so that it can be
193  * timed out.
194  */
195 static void
196 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
197 {
198 	struct rtentry *rt = (struct rtentry *)rn;
199 
200 	if(!(rt->rt_flags & RTF_UP))
201 		return;		/* prophylactic measures */
202 
203 	if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
204 		return;
205 
206 	if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
207 	   != RTF_WASCLONED)
208 		return;
209 
210 	/*
211 	 * As requested by David Greenman:
212 	 * If rtq_reallyold is 0, just delete the route without
213 	 * waiting for a timeout cycle to kill it.
214 	 */
215 	if(rtq_reallyold != 0) {
216 		rt->rt_flags |= RTPRF_OURS;
217 		rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
218 	} else {
219 		rtrequest(RTM_DELETE,
220 			  (struct sockaddr *)rt_key(rt),
221 			  rt->rt_gateway, rt_mask(rt),
222 			  rt->rt_flags, 0);
223 	}
224 }
225 
226 struct rtqk_arg {
227 	struct radix_node_head *rnh;
228 	int draining;
229 	int killed;
230 	int found;
231 	int updating;
232 	time_t nextstop;
233 };
234 
235 /*
236  * Get rid of old routes.  When draining, this deletes everything, even when
237  * the timeout is not expired yet.  When updating, this makes sure that
238  * nothing has a timeout longer than the current value of rtq_reallyold.
239  */
240 static int
241 in_rtqkill(struct radix_node *rn, void *rock)
242 {
243 	struct rtqk_arg *ap = rock;
244 	struct rtentry *rt = (struct rtentry *)rn;
245 	int err;
246 
247 	if(rt->rt_flags & RTPRF_OURS) {
248 		ap->found++;
249 
250 		if(ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
251 			if(rt->rt_refcnt > 0)
252 				panic("rtqkill route really not free");
253 
254 			err = rtrequest(RTM_DELETE,
255 					(struct sockaddr *)rt_key(rt),
256 					rt->rt_gateway, rt_mask(rt),
257 					rt->rt_flags, 0);
258 			if(err) {
259 				log(LOG_WARNING, "in_rtqkill: error %d\n", err);
260 			} else {
261 				ap->killed++;
262 			}
263 		} else {
264 			if(ap->updating
265 			   && (rt->rt_rmx.rmx_expire - time_second
266 			       > rtq_reallyold)) {
267 				rt->rt_rmx.rmx_expire = time_second
268 					+ rtq_reallyold;
269 			}
270 			ap->nextstop = lmin(ap->nextstop,
271 					    rt->rt_rmx.rmx_expire);
272 		}
273 	}
274 
275 	return 0;
276 }
277 
278 #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
279 static int rtq_timeout = RTQ_TIMEOUT;
280 
281 static void
282 in_rtqtimo(void *rock)
283 {
284 	struct radix_node_head *rnh = rock;
285 	struct rtqk_arg arg;
286 	struct timeval atv;
287 	static time_t last_adjusted_timeout = 0;
288 	int s;
289 
290 	arg.found = arg.killed = 0;
291 	arg.rnh = rnh;
292 	arg.nextstop = time_second + rtq_timeout;
293 	arg.draining = arg.updating = 0;
294 	s = splnet();
295 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
296 	splx(s);
297 
298 	/*
299 	 * Attempt to be somewhat dynamic about this:
300 	 * If there are ``too many'' routes sitting around taking up space,
301 	 * then crank down the timeout, and see if we can't make some more
302 	 * go away.  However, we make sure that we will never adjust more
303 	 * than once in rtq_timeout seconds, to keep from cranking down too
304 	 * hard.
305 	 */
306 	if((arg.found - arg.killed > rtq_toomany)
307 	   && (time_second - last_adjusted_timeout >= rtq_timeout)
308 	   && rtq_reallyold > rtq_minreallyold) {
309 		rtq_reallyold = 2*rtq_reallyold / 3;
310 		if(rtq_reallyold < rtq_minreallyold) {
311 			rtq_reallyold = rtq_minreallyold;
312 		}
313 
314 		last_adjusted_timeout = time_second;
315 #ifdef DIAGNOSTIC
316 		log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
317 		    rtq_reallyold);
318 #endif
319 		arg.found = arg.killed = 0;
320 		arg.updating = 1;
321 		s = splnet();
322 		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
323 		splx(s);
324 	}
325 
326 	atv.tv_usec = 0;
327 	atv.tv_sec = arg.nextstop - time_second;
328 	timeout(in_rtqtimo, rock, tvtohz(&atv));
329 }
330 
331 void
332 in_rtqdrain(void)
333 {
334 	struct radix_node_head *rnh = rt_tables[AF_INET];
335 	struct rtqk_arg arg;
336 	int s;
337 	arg.found = arg.killed = 0;
338 	arg.rnh = rnh;
339 	arg.nextstop = 0;
340 	arg.draining = 1;
341 	arg.updating = 0;
342 	s = splnet();
343 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
344 	splx(s);
345 }
346 
347 /*
348  * Initialize our routing tree.
349  */
350 int
351 in_inithead(void **head, int off)
352 {
353 	struct radix_node_head *rnh;
354 
355 	if(!rn_inithead(head, off))
356 		return 0;
357 
358 	if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
359 		return 1;	/* only do this for the real routing table */
360 
361 	rnh = *head;
362 	rnh->rnh_addaddr = in_addroute;
363 	rnh->rnh_matchaddr = in_matroute;
364 	rnh->rnh_close = in_clsroute;
365 	in_rtqtimo(rnh);	/* kick off timeout first time */
366 	return 1;
367 }
368 
369 
370 /*
371  * This zaps old routes when the interface goes down or interface
372  * address is deleted.  In the latter case, it deletes static routes
373  * that point to this address.  If we don't do this, we may end up
374  * using the old address in the future.  The ones we always want to
375  * get rid of are things like ARP entries, since the user might down
376  * the interface, walk over to a completely different network, and
377  * plug back in.
378  */
379 struct in_ifadown_arg {
380 	struct radix_node_head *rnh;
381 	struct ifaddr *ifa;
382 	int del;
383 };
384 
385 static int
386 in_ifadownkill(struct radix_node *rn, void *xap)
387 {
388 	struct in_ifadown_arg *ap = xap;
389 	struct rtentry *rt = (struct rtentry *)rn;
390 	int err;
391 
392 	if (rt->rt_ifa == ap->ifa &&
393 	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
394 		/*
395 		 * We need to disable the automatic prune that happens
396 		 * in this case in rtrequest() because it will blow
397 		 * away the pointers that rn_walktree() needs in order
398 		 * continue our descent.  We will end up deleting all
399 		 * the routes that rtrequest() would have in any case,
400 		 * so that behavior is not needed there.
401 		 */
402 		rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
403 		err = rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
404 				rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
405 		if (err) {
406 			log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
407 		}
408 	}
409 	return 0;
410 }
411 
412 int
413 in_ifadown(struct ifaddr *ifa, int delete)
414 {
415 	struct in_ifadown_arg arg;
416 	struct radix_node_head *rnh;
417 
418 	if (ifa->ifa_addr->sa_family != AF_INET)
419 		return 1;
420 
421 	arg.rnh = rnh = rt_tables[AF_INET];
422 	arg.ifa = ifa;
423 	arg.del = delete;
424 	rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
425 	ifa->ifa_flags &= ~IFA_ROUTE;
426 	return 0;
427 }
428