xref: /freebsd/sys/netinet/in_rmx.c (revision 3193579b66fd7067f898dbc54bdea81a0e6f9bd0)
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$
30  */
31 
32 /*
33  * This code does two things necessary for the enhanced TCP metrics to
34  * function in a useful manner:
35  *  1) It marks all non-host routes as `cloning', thus ensuring that
36  *     every actual reference to such a route actually gets turned
37  *     into a reference to a host route to the specific destination
38  *     requested.
39  *  2) When such routes lose all their references, it arranges for them
40  *     to be deleted in some random collection of circumstances, so that
41  *     a large quantity of stale routing data is not kept in kernel memory
42  *     indefinitely.  See in_rtqtimo() below for the exact mechanism.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/socket.h>
50 #include <sys/mbuf.h>
51 #include <sys/syslog.h>
52 #include <sys/callout.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 	 * 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 if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
104 		    sin->sin_addr.s_addr) {
105 			rt->rt_flags |= RTF_LOCAL;
106 		}
107 	}
108 
109 	if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
110 	    rt->rt_ifp)
111 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
112 
113 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
114 	if (ret == NULL && rt->rt_flags & RTF_HOST) {
115 		struct rtentry *rt2;
116 		/*
117 		 * We are trying to add a host route, but can't.
118 		 * Find out if it is because of an
119 		 * ARP entry and delete it if so.
120 		 */
121 		rt2 = rtalloc1((struct sockaddr *)sin, 0,
122 				RTF_CLONING | RTF_PRCLONING);
123 		if (rt2) {
124 			if (rt2->rt_flags & RTF_LLINFO &&
125 			    rt2->rt_flags & RTF_HOST &&
126 			    rt2->rt_gateway &&
127 			    rt2->rt_gateway->sa_family == AF_LINK) {
128 				rtexpunge(rt2);
129 				RTFREE_LOCKED(rt2);
130 				ret = rn_addroute(v_arg, n_arg, head,
131 						  treenodes);
132 			} else
133 				RTFREE_LOCKED(rt2);
134 		}
135 	}
136 
137 	return ret;
138 }
139 
140 /*
141  * This code is the inverse of in_clsroute: on first reference, if we
142  * were managing the route, stop doing so and set the expiration timer
143  * back off again.
144  */
145 static struct radix_node *
146 in_matroute(void *v_arg, struct radix_node_head *head)
147 {
148 	struct radix_node *rn = rn_match(v_arg, head);
149 	struct rtentry *rt = (struct rtentry *)rn;
150 
151 	/*XXX locking? */
152 	if (rt && rt->rt_refcnt == 0) {		/* this is first reference */
153 		if (rt->rt_flags & RTPRF_OURS) {
154 			rt->rt_flags &= ~RTPRF_OURS;
155 			rt->rt_rmx.rmx_expire = 0;
156 		}
157 	}
158 	return rn;
159 }
160 
161 static int rtq_reallyold = 60*60;		/* one hour is "really old" */
162 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
163     &rtq_reallyold, 0, "Default expiration time on dynamically learned routes");
164 
165 static int rtq_minreallyold = 10;  /* never automatically crank down to less */
166 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
167     &rtq_minreallyold, 0,
168     "Minimum time to attempt to hold onto dynamically learned routes");
169 
170 static int rtq_toomany = 128;		/* 128 cached routes is "too many" */
171 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
172     &rtq_toomany, 0, "Upper limit on dynamically learned routes");
173 
174 /*
175  * On last reference drop, mark the route as belong to us so that it can be
176  * timed out.
177  */
178 static void
179 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
180 {
181 	struct rtentry *rt = (struct rtentry *)rn;
182 
183 	RT_LOCK_ASSERT(rt);
184 
185 	if (!(rt->rt_flags & RTF_UP))
186 		return;			/* prophylactic measures */
187 
188 	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
189 		return;
190 
191 	if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
192 		return;
193 
194 	/*
195 	 * If rtq_reallyold is 0, just delete the route without
196 	 * waiting for a timeout cycle to kill it.
197 	 */
198 	if (rtq_reallyold != 0) {
199 		rt->rt_flags |= RTPRF_OURS;
200 		rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
201 	} else {
202 		rtexpunge(rt);
203 	}
204 }
205 
206 struct rtqk_arg {
207 	struct radix_node_head *rnh;
208 	int draining;
209 	int killed;
210 	int found;
211 	int updating;
212 	time_t nextstop;
213 };
214 
215 /*
216  * Get rid of old routes.  When draining, this deletes everything, even when
217  * the timeout is not expired yet.  When updating, this makes sure that
218  * nothing has a timeout longer than the current value of rtq_reallyold.
219  */
220 static int
221 in_rtqkill(struct radix_node *rn, void *rock)
222 {
223 	struct rtqk_arg *ap = rock;
224 	struct rtentry *rt = (struct rtentry *)rn;
225 	int err;
226 
227 	if (rt->rt_flags & RTPRF_OURS) {
228 		ap->found++;
229 
230 		if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
231 			if (rt->rt_refcnt > 0)
232 				panic("rtqkill route really not free");
233 
234 			err = rtrequest(RTM_DELETE,
235 					(struct sockaddr *)rt_key(rt),
236 					rt->rt_gateway, rt_mask(rt),
237 					rt->rt_flags, 0);
238 			if (err) {
239 				log(LOG_WARNING, "in_rtqkill: error %d\n", err);
240 			} else {
241 				ap->killed++;
242 			}
243 		} else {
244 			if (ap->updating &&
245 			    (rt->rt_rmx.rmx_expire - time_second >
246 			     rtq_reallyold)) {
247 				rt->rt_rmx.rmx_expire =
248 				    time_second + rtq_reallyold;
249 			}
250 			ap->nextstop = lmin(ap->nextstop,
251 					    rt->rt_rmx.rmx_expire);
252 		}
253 	}
254 
255 	return 0;
256 }
257 
258 #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
259 static int rtq_timeout = RTQ_TIMEOUT;
260 static struct callout rtq_timer;
261 
262 static void
263 in_rtqtimo(void *rock)
264 {
265 	struct radix_node_head *rnh = rock;
266 	struct rtqk_arg arg;
267 	struct timeval atv;
268 	static time_t last_adjusted_timeout = 0;
269 
270 	arg.found = arg.killed = 0;
271 	arg.rnh = rnh;
272 	arg.nextstop = time_second + rtq_timeout;
273 	arg.draining = arg.updating = 0;
274 	RADIX_NODE_HEAD_LOCK(rnh);
275 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
276 	RADIX_NODE_HEAD_UNLOCK(rnh);
277 
278 	/*
279 	 * Attempt to be somewhat dynamic about this:
280 	 * If there are ``too many'' routes sitting around taking up space,
281 	 * then crank down the timeout, and see if we can't make some more
282 	 * go away.  However, we make sure that we will never adjust more
283 	 * than once in rtq_timeout seconds, to keep from cranking down too
284 	 * hard.
285 	 */
286 	if ((arg.found - arg.killed > rtq_toomany) &&
287 	    (time_second - last_adjusted_timeout >= rtq_timeout) &&
288 	    rtq_reallyold > rtq_minreallyold) {
289 		rtq_reallyold = 2 * rtq_reallyold / 3;
290 		if (rtq_reallyold < rtq_minreallyold) {
291 			rtq_reallyold = rtq_minreallyold;
292 		}
293 
294 		last_adjusted_timeout = time_second;
295 #ifdef DIAGNOSTIC
296 		log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
297 		    rtq_reallyold);
298 #endif
299 		arg.found = arg.killed = 0;
300 		arg.updating = 1;
301 		RADIX_NODE_HEAD_LOCK(rnh);
302 		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
303 		RADIX_NODE_HEAD_UNLOCK(rnh);
304 	}
305 
306 	atv.tv_usec = 0;
307 	atv.tv_sec = arg.nextstop - time_second;
308 	callout_reset(&rtq_timer, tvtohz(&atv), in_rtqtimo, rock);
309 }
310 
311 void
312 in_rtqdrain(void)
313 {
314 	struct radix_node_head *rnh = rt_tables[AF_INET];
315 	struct rtqk_arg arg;
316 
317 	arg.found = arg.killed = 0;
318 	arg.rnh = rnh;
319 	arg.nextstop = 0;
320 	arg.draining = 1;
321 	arg.updating = 0;
322 	RADIX_NODE_HEAD_LOCK(rnh);
323 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
324 	RADIX_NODE_HEAD_UNLOCK(rnh);
325 }
326 
327 /*
328  * Initialize our routing tree.
329  */
330 int
331 in_inithead(void **head, int off)
332 {
333 	struct radix_node_head *rnh;
334 
335 	if (!rn_inithead(head, off))
336 		return 0;
337 
338 	if (head != (void **)&rt_tables[AF_INET])	/* BOGUS! */
339 		return 1;	/* only do this for the real routing table */
340 
341 	rnh = *head;
342 	rnh->rnh_addaddr = in_addroute;
343 	rnh->rnh_matchaddr = in_matroute;
344 	rnh->rnh_close = in_clsroute;
345 	callout_init(&rtq_timer, CALLOUT_MPSAFE);
346 	in_rtqtimo(rnh);	/* kick off timeout first time */
347 	return 1;
348 }
349 
350 /*
351  * This zaps old routes when the interface goes down or interface
352  * address is deleted.  In the latter case, it deletes static routes
353  * that point to this address.  If we don't do this, we may end up
354  * using the old address in the future.  The ones we always want to
355  * get rid of are things like ARP entries, since the user might down
356  * the interface, walk over to a completely different network, and
357  * plug back in.
358  */
359 struct in_ifadown_arg {
360 	struct radix_node_head *rnh;
361 	struct ifaddr *ifa;
362 	int del;
363 };
364 
365 static int
366 in_ifadownkill(struct radix_node *rn, void *xap)
367 {
368 	struct in_ifadown_arg *ap = xap;
369 	struct rtentry *rt = (struct rtentry *)rn;
370 
371 	RT_LOCK(rt);
372 	if (rt->rt_ifa == ap->ifa &&
373 	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
374 		/*
375 		 * We need to disable the automatic prune that happens
376 		 * in this case in rtrequest() because it will blow
377 		 * away the pointers that rn_walktree() needs in order
378 		 * continue our descent.  We will end up deleting all
379 		 * the routes that rtrequest() would have in any case,
380 		 * so that behavior is not needed there.
381 		 */
382 		rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
383 		rtexpunge(rt);
384 	}
385 	RT_UNLOCK(rt);
386 	return 0;
387 }
388 
389 int
390 in_ifadown(struct ifaddr *ifa, int delete)
391 {
392 	struct in_ifadown_arg arg;
393 	struct radix_node_head *rnh;
394 
395 	if (ifa->ifa_addr->sa_family != AF_INET)
396 		return 1;
397 
398 	arg.rnh = rnh = rt_tables[AF_INET];
399 	arg.ifa = ifa;
400 	arg.del = delete;
401 	RADIX_NODE_HEAD_LOCK(rnh);
402 	rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
403 	RADIX_NODE_HEAD_UNLOCK(rnh);
404 	ifa->ifa_flags &= ~IFA_ROUTE;		/* XXXlocking? */
405 	return 0;
406 }
407