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