xref: /dflybsd-src/sys/netinet/in_rmx.c (revision 23c32883e759b0ea42fdaff39e661bd1a12e3b9f)
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.11 2005/03/09 23:31:44 hsu 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 #define RTPRF_EXPIRING	RTF_PROTO3	/* set on routes we manage */
61 
62 static struct callout in_rtqtimo_ch;
63 
64 /*
65  * Do what we need to do when inserting a route.
66  */
67 static struct radix_node *
68 in_addroute(char *key, char *mask, 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 	 * A little bit of help for both IP output and input:
86 	 *   For host routes, we make sure that RTF_BROADCAST
87 	 *   is set for anything that looks like a broadcast address.
88 	 *   This way, we can avoid an expensive call to in_broadcast()
89 	 *   in ip_output() most of the time (because the route passed
90 	 *   to ip_output() is almost always a host route).
91 	 *
92 	 *   We also do the same for local addresses, with the thought
93 	 *   that this might one day be used to speed up ip_input().
94 	 *
95 	 * We also mark routes to multicast addresses as such, because
96 	 * it's easy to do and might be useful (but this is much more
97 	 * dubious since it's so easy to inspect the address).  (This
98 	 * is done above.)
99 	 */
100 	if (rt->rt_flags & RTF_HOST) {
101 		if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
102 			rt->rt_flags |= RTF_BROADCAST;
103 		} else {
104 			if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
105 			    == sin->sin_addr.s_addr)
106 				rt->rt_flags |= RTF_LOCAL;
107 		}
108 	}
109 
110 	if (rt->rt_rmx.rmx_mtu != 0 && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
111 	    rt->rt_ifp != NULL)
112 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
113 
114 	ret = rn_addroute(key, mask, head, treenodes);
115 	if (ret == NULL && rt->rt_flags & RTF_HOST) {
116 		struct rtentry *oldrt;
117 
118 		/*
119 		 * We are trying to add a host route, but can't.
120 		 * Find out if it is because of an ARP entry and
121 		 * delete it if so.
122 		 */
123 		oldrt = rtpurelookup((struct sockaddr *)sin);
124 		if (oldrt != NULL) {
125 			--oldrt->rt_refcnt;
126 			if (oldrt->rt_flags & RTF_LLINFO &&
127 			    oldrt->rt_flags & RTF_HOST &&
128 			    oldrt->rt_gateway &&
129 			    oldrt->rt_gateway->sa_family == AF_LINK) {
130 				rtrequest(RTM_DELETE, rt_key(oldrt),
131 					  oldrt->rt_gateway, rt_mask(oldrt),
132 					  oldrt->rt_flags, NULL);
133 				ret = rn_addroute(key, mask, head, treenodes);
134 			}
135 		}
136 	}
137 
138 	/*
139 	 * If the new route created successfully, and we are forwarding,
140 	 * and there is a cached route, free it.  Otherwise, we may end
141 	 * up using the wrong route.
142 	 */
143 	if (ret != NULL && ipforwarding && ipforward_rt.ro_rt != NULL) {
144 		RTFREE(ipforward_rt.ro_rt);
145 		ipforward_rt.ro_rt = NULL;
146 	}
147 
148 	return ret;
149 }
150 
151 /*
152  * This code is the inverse of in_closeroute: on first reference, if we
153  * were managing the route, stop doing so and set the expiration timer
154  * back off again.
155  */
156 static struct radix_node *
157 in_matchroute(char *key, struct radix_node_head *head)
158 {
159 	struct radix_node *rn = rn_match(key, head);
160 	struct rtentry *rt = (struct rtentry *)rn;
161 
162 	if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
163 		if (rt->rt_flags & RTPRF_EXPIRING) {
164 			rt->rt_flags &= ~RTPRF_EXPIRING;
165 			rt->rt_rmx.rmx_expire = 0;
166 		}
167 	}
168 	return rn;
169 }
170 
171 static int rtq_reallyold = 60*60;  /* one hour is ``really old'' */
172 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
173     &rtq_reallyold , 0,
174     "Default expiration time on cloned routes");
175 
176 static int rtq_minreallyold = 10;  /* never automatically crank down to less */
177 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
178     &rtq_minreallyold , 0,
179     "Minimum time to attempt to hold onto cloned routes");
180 
181 static int rtq_toomany = 128;	   /* 128 cached routes is ``too many'' */
182 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
183     &rtq_toomany , 0, "Upper limit on cloned routes");
184 
185 /*
186  * On last reference drop, mark the route as belong to us so that it can be
187  * timed out.
188  */
189 static void
190 in_closeroute(struct radix_node *rn, struct radix_node_head *head)
191 {
192 	struct rtentry *rt = (struct rtentry *)rn;
193 
194 	if (!(rt->rt_flags & RTF_UP))
195 		return;		/* prophylactic measures */
196 
197 	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
198 		return;
199 
200 	if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_EXPIRING)) != RTF_WASCLONED)
201 		return;
202 
203 	/*
204 	 * As requested by David Greenman:
205 	 * If rtq_reallyold is 0, just delete the route without
206 	 * waiting for a timeout cycle to kill it.
207 	 */
208 	if (rtq_reallyold != 0) {
209 		rt->rt_flags |= RTPRF_EXPIRING;
210 		rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
211 	} else {
212 		/*
213 		 * Remove route from the radix tree, but defer deallocation
214 		 * until we return to rtfree().
215 		 */
216 		rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
217 			  rt->rt_flags, &rt);
218 	}
219 }
220 
221 struct rtqk_arg {
222 	struct radix_node_head *rnh;
223 	int draining;
224 	int killed;
225 	int found;
226 	int updating;
227 	time_t nextstop;
228 };
229 
230 /*
231  * Get rid of old routes.  When draining, this deletes everything, even when
232  * the timeout is not expired yet.  When updating, this makes sure that
233  * nothing has a timeout longer than the current value of rtq_reallyold.
234  */
235 static int
236 in_rtqkill(struct radix_node *rn, void *rock)
237 {
238 	struct rtqk_arg *ap = rock;
239 	struct rtentry *rt = (struct rtentry *)rn;
240 	int err;
241 
242 	if (rt->rt_flags & RTPRF_EXPIRING) {
243 		ap->found++;
244 		if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
245 			if (rt->rt_refcnt > 0)
246 				panic("rtqkill route really not free");
247 
248 			err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
249 					rt_mask(rt), rt->rt_flags, NULL);
250 			if (err)
251 				log(LOG_WARNING, "in_rtqkill: error %d\n", err);
252 			else
253 				ap->killed++;
254 		} else {
255 			if (ap->updating &&
256 			    (rt->rt_rmx.rmx_expire - time_second >
257 			     rtq_reallyold)) {
258 				rt->rt_rmx.rmx_expire = time_second +
259 				    rtq_reallyold;
260 			}
261 			ap->nextstop = lmin(ap->nextstop,
262 					    rt->rt_rmx.rmx_expire);
263 		}
264 	}
265 
266 	return 0;
267 }
268 
269 #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
270 static int rtq_timeout = RTQ_TIMEOUT;
271 
272 static void
273 in_rtqtimo(void *rock)
274 {
275 	struct radix_node_head *rnh = rock;
276 	struct rtqk_arg arg;
277 	struct timeval atv;
278 	static time_t last_adjusted_timeout = 0;
279 	int s;
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 	s = splnet();
286 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
287 	splx(s);
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 		s = splnet();
313 		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
314 		splx(s);
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 	int s;
328 
329 	arg.found = arg.killed = 0;
330 	arg.rnh = rnh;
331 	arg.nextstop = 0;
332 	arg.draining = 1;
333 	arg.updating = 0;
334 	s = splnet();
335 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
336 	splx(s);
337 }
338 
339 /*
340  * Initialize our routing tree.
341  */
342 int
343 in_inithead(void **head, int off)
344 {
345 	struct radix_node_head *rnh;
346 
347 	if (!rn_inithead(head, off))
348 		return 0;
349 
350 	if (head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
351 		return 1;	/* only do this for the real routing table */
352 
353 	rnh = *head;
354 	rnh->rnh_addaddr = in_addroute;
355 	rnh->rnh_matchaddr = in_matchroute;
356 	rnh->rnh_close = in_closeroute;
357 	callout_init(&in_rtqtimo_ch);
358 	in_rtqtimo(rnh);	/* kick off timeout first time */
359 	return 1;
360 }
361 
362 /*
363  * This zaps old routes when the interface goes down or interface
364  * address is deleted.  In the latter case, it deletes static routes
365  * that point to this address.  If we don't do this, we may end up
366  * using the old address in the future.  The ones we always want to
367  * get rid of are things like ARP entries, since the user might down
368  * the interface, walk over to a completely different network, and
369  * plug back in.
370  */
371 struct in_ifadown_arg {
372 	struct radix_node_head *rnh;
373 	struct ifaddr *ifa;
374 	int del;
375 };
376 
377 static int
378 in_ifadownkill(struct radix_node *rn, void *xap)
379 {
380 	struct in_ifadown_arg *ap = xap;
381 	struct rtentry *rt = (struct rtentry *)rn;
382 	int err;
383 
384 	if (rt->rt_ifa == ap->ifa &&
385 	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
386 		/*
387 		 * We need to disable the automatic prune that happens
388 		 * in this case in rtrequest() because it will blow
389 		 * away the pointers that rn_walktree() needs in order
390 		 * continue our descent.  We will end up deleting all
391 		 * the routes that rtrequest() would have in any case,
392 		 * so that behavior is not needed there.
393 		 */
394 		rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
395 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
396 				rt_mask(rt), rt->rt_flags, NULL);
397 		if (err)
398 			log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
399 	}
400 	return 0;
401 }
402 
403 int
404 in_ifadown(struct ifaddr *ifa, int delete)
405 {
406 	struct in_ifadown_arg arg;
407 	struct radix_node_head *rnh;
408 
409 	if (ifa->ifa_addr->sa_family != AF_INET)
410 		return 1;
411 
412 	arg.rnh = rnh = rt_tables[AF_INET];
413 	arg.ifa = ifa;
414 	arg.del = delete;
415 	rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
416 	ifa->ifa_flags &= ~IFA_ROUTE;
417 	return 0;
418 }
419