xref: /freebsd/sys/netinet6/in6_rmx.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
1 /*
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * Copyright 1994, 1995 Massachusetts Institute of Technology
34  *
35  * Permission to use, copy, modify, and distribute this software and
36  * its documentation for any purpose and without fee is hereby
37  * granted, provided that both the above copyright notice and this
38  * permission notice appear in all copies, that both the above
39  * copyright notice and this permission notice appear in all
40  * supporting documentation, and that the name of M.I.T. not be used
41  * in advertising or publicity pertaining to distribution of the
42  * software without specific, written prior permission.  M.I.T. makes
43  * no representations about the suitability of this software for any
44  * purpose.  It is provided "as is" without express or implied
45  * warranty.
46  *
47  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
48  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
49  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
51  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  * $Id: in6_rmx.c,v 1.3 1999/08/16 13:42:53 itojun Exp $
61  */
62 
63 /*
64  * This code does two things necessary for the enhanced TCP metrics to
65  * function in a useful manner:
66  *  1) It marks all non-host routes as `cloning', thus ensuring that
67  *     every actual reference to such a route actually gets turned
68  *     into a reference to a host route to the specific destination
69  *     requested.
70  *  2) When such routes lose all their references, it arranges for them
71  *     to be deleted in some random collection of circumstances, so that
72  *     a large quantity of stale routing data is not kept in kernel memory
73  *     indefinitely.  See in6_rtqtimo() below for the exact mechanism.
74  */
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/queue.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/mbuf.h>
84 #include <sys/syslog.h>
85 
86 #include <net/if.h>
87 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/in_var.h>
91 
92 #include <netinet6/ip6.h>
93 #include <netinet6/ip6_var.h>
94 
95 #include <netinet6/icmp6.h>
96 
97 #include <netinet/tcp.h>
98 #include <netinet/tcp_seq.h>
99 #include <netinet/tcp_timer.h>
100 #include <netinet/tcp_var.h>
101 
102 extern int	in6_inithead __P((void **head, int off));
103 
104 #define	RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
105 
106 /*
107  * Do what we need to do when inserting a route.
108  */
109 static struct radix_node *
110 in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
111 	     struct radix_node *treenodes)
112 {
113 	struct rtentry *rt = (struct rtentry *)treenodes;
114 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
115 	struct radix_node *ret;
116 
117 	/*
118 	 * For IPv6, all unicast non-host routes are automatically cloning.
119 	 */
120 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
121 		rt->rt_flags |= RTF_MULTICAST;
122 
123 	if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
124 		rt->rt_flags |= RTF_PRCLONING;
125 	}
126 
127 	/*
128 	 * A little bit of help for both IPv6 output and input:
129 	 *   For local addresses, we make sure that RTF_LOCAL is set,
130 	 *   with the thought that this might one day be used to speed up
131 	 *   ip_input().
132 	 *
133 	 * We also mark routes to multicast addresses as such, because
134 	 * it's easy to do and might be useful (but this is much more
135 	 * dubious since it's so easy to inspect the address).  (This
136 	 * is done above.)
137 	 *
138 	 * XXX
139 	 * should elaborate the code.
140 	 */
141 	if (rt->rt_flags & RTF_HOST) {
142 		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
143 					->sin6_addr,
144 				       &sin6->sin6_addr)) {
145 			rt->rt_flags |= RTF_LOCAL;
146 		}
147 	}
148 
149 	/*
150 	 * We also specify a send and receive pipe size for every
151 	 * route added, to help TCP a bit.  TCP doesn't actually
152 	 * want a true pipe size, which would be prohibitive in memory
153 	 * costs and is hard to compute anyway; it simply uses these
154 	 * values to size its buffers.  So, we fill them in with the
155 	 * same values that TCP would have used anyway, and allow the
156 	 * installing program or the link layer to override these values
157 	 * as it sees fit.  This will hopefully allow TCP more
158 	 * opportunities to save its ssthresh value.
159 	 */
160 	if (!rt->rt_rmx.rmx_sendpipe && !(rt->rt_rmx.rmx_locks & RTV_SPIPE))
161 		rt->rt_rmx.rmx_sendpipe = tcp_sendspace;
162 
163 	if (!rt->rt_rmx.rmx_recvpipe && !(rt->rt_rmx.rmx_locks & RTV_RPIPE))
164 		rt->rt_rmx.rmx_recvpipe = tcp_recvspace;
165 
166 	if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
167 	    && rt->rt_ifp)
168 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
169 
170 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
171 	if (ret == NULL && rt->rt_flags & RTF_HOST) {
172 		struct rtentry *rt2;
173 		/*
174 		 * We are trying to add a host route, but can't.
175 		 * Find out if it is because of an
176 		 * ARP entry and delete it if so.
177 		 */
178 		rt2 = rtalloc1((struct sockaddr *)sin6, 0,
179 				RTF_CLONING | RTF_PRCLONING);
180 		if (rt2) {
181 			if (rt2->rt_flags & RTF_LLINFO &&
182 				rt2->rt_flags & RTF_HOST &&
183 				rt2->rt_gateway &&
184 				rt2->rt_gateway->sa_family == AF_LINK) {
185 				rtrequest(RTM_DELETE,
186 					  (struct sockaddr *)rt_key(rt2),
187 					  rt2->rt_gateway,
188 					  rt_mask(rt2), rt2->rt_flags, 0);
189 				ret = rn_addroute(v_arg, n_arg, head,
190 					treenodes);
191 			}
192 			RTFREE(rt2);
193 		}
194 	} else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
195 		struct rtentry *rt2;
196 		/*
197 		 * We are trying to add a net route, but can't.
198 		 * The following case should be allowed, so we'll make a
199 		 * special check for this:
200 		 *	Two IPv6 addresses with the same prefix is assigned
201 		 *	to a single interrface.
202 		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
203 		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
204 		 *	In this case, (*1) and (*2) want to add the same
205 		 *	net route entry, 3ffe:0501:: -> if0.
206 		 *	This case should not raise an error.
207 		 */
208 		rt2 = rtalloc1((struct sockaddr *)sin6, 0,
209 				RTF_CLONING | RTF_PRCLONING);
210 		if (rt2) {
211 			if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
212 					== RTF_CLONING
213 			 && rt2->rt_gateway
214 			 && rt2->rt_gateway->sa_family == AF_LINK
215 			 && rt2->rt_ifp == rt->rt_ifp) {
216 				ret = rt2->rt_nodes;
217 			}
218 			RTFREE(rt2);
219 		}
220 	}
221 	return ret;
222 }
223 
224 /*
225  * This code is the inverse of in6_clsroute: on first reference, if we
226  * were managing the route, stop doing so and set the expiration timer
227  * back off again.
228  */
229 static struct radix_node *
230 in6_matroute(void *v_arg, struct radix_node_head *head)
231 {
232 	struct radix_node *rn = rn_match(v_arg, head);
233 	struct rtentry *rt = (struct rtentry *)rn;
234 
235 	if (rt && rt->rt_refcnt == 0) { /* this is first reference */
236 		if (rt->rt_flags & RTPRF_OURS) {
237 			rt->rt_flags &= ~RTPRF_OURS;
238 			rt->rt_rmx.rmx_expire = 0;
239 		}
240 	}
241 	return rn;
242 }
243 
244 static int rtq_reallyold = 60*60;
245 	/* one hour is ``really old'' */
246 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire,
247 	CTLFLAG_RW, &rtq_reallyold , 0, "");
248 
249 static int rtq_minreallyold = 10;
250 	/* never automatically crank down to less */
251 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire,
252 	CTLFLAG_RW, &rtq_minreallyold , 0, "");
253 
254 static int rtq_toomany = 128;
255 	/* 128 cached routes is ``too many'' */
256 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache,
257 	CTLFLAG_RW, &rtq_toomany , 0, "");
258 
259 
260 /*
261  * On last reference drop, mark the route as belong to us so that it can be
262  * timed out.
263  */
264 static void
265 in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
266 {
267 	struct rtentry *rt = (struct rtentry *)rn;
268 
269 	if (!(rt->rt_flags & RTF_UP))
270 		return;		/* prophylactic measures */
271 
272 	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
273 		return;
274 
275 	if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
276 	   != RTF_WASCLONED)
277 		return;
278 
279 	/*
280 	 * As requested by David Greenman:
281 	 * If rtq_reallyold is 0, just delete the route without
282 	 * waiting for a timeout cycle to kill it.
283 	 */
284 	if (rtq_reallyold != 0) {
285 		rt->rt_flags |= RTPRF_OURS;
286 		rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
287 	} else {
288 		rtrequest(RTM_DELETE,
289 			  (struct sockaddr *)rt_key(rt),
290 			  rt->rt_gateway, rt_mask(rt),
291 			  rt->rt_flags, 0);
292 	}
293 }
294 
295 struct rtqk_arg {
296 	struct radix_node_head *rnh;
297 	int mode;
298 	int updating;
299 	int draining;
300 	int killed;
301 	int found;
302 	time_t nextstop;
303 };
304 
305 /*
306  * Get rid of old routes.  When draining, this deletes everything, even when
307  * the timeout is not expired yet.  When updating, this makes sure that
308  * nothing has a timeout longer than the current value of rtq_reallyold.
309  */
310 static int
311 in6_rtqkill(struct radix_node *rn, void *rock)
312 {
313 	struct rtqk_arg *ap = rock;
314 	struct rtentry *rt = (struct rtentry *)rn;
315 	int err;
316 
317 	if (rt->rt_flags & RTPRF_OURS) {
318 		ap->found++;
319 
320 		if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
321 			if (rt->rt_refcnt > 0)
322 				panic("rtqkill route really not free");
323 
324 			err = rtrequest(RTM_DELETE,
325 					(struct sockaddr *)rt_key(rt),
326 					rt->rt_gateway, rt_mask(rt),
327 					rt->rt_flags, 0);
328 			if (err) {
329 				log(LOG_WARNING, "in6_rtqkill: error %d", err);
330 			} else {
331 				ap->killed++;
332 			}
333 		} else {
334 			if (ap->updating
335 			   && (rt->rt_rmx.rmx_expire - time_second
336 			       > rtq_reallyold)) {
337 				rt->rt_rmx.rmx_expire = time_second
338 					+ rtq_reallyold;
339 			}
340 			ap->nextstop = lmin(ap->nextstop,
341 					    rt->rt_rmx.rmx_expire);
342 		}
343 	}
344 
345 	return 0;
346 }
347 
348 #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
349 static int rtq_timeout = RTQ_TIMEOUT;
350 
351 static void
352 in6_rtqtimo(void *rock)
353 {
354 	struct radix_node_head *rnh = rock;
355 	struct rtqk_arg arg;
356 	struct timeval atv;
357 	static time_t last_adjusted_timeout = 0;
358 	int s;
359 
360 	arg.found = arg.killed = 0;
361 	arg.rnh = rnh;
362 	arg.nextstop = time_second + rtq_timeout;
363 	arg.draining = arg.updating = 0;
364 	s = splnet();
365 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
366 	splx(s);
367 
368 	/*
369 	 * Attempt to be somewhat dynamic about this:
370 	 * If there are ``too many'' routes sitting around taking up space,
371 	 * then crank down the timeout, and see if we can't make some more
372 	 * go away.  However, we make sure that we will never adjust more
373 	 * than once in rtq_timeout seconds, to keep from cranking down too
374 	 * hard.
375 	 */
376 	if ((arg.found - arg.killed > rtq_toomany)
377 	   && (time_second - last_adjusted_timeout >= rtq_timeout)
378 	   && rtq_reallyold > rtq_minreallyold) {
379 		rtq_reallyold = 2*rtq_reallyold / 3;
380 		if (rtq_reallyold < rtq_minreallyold) {
381 			rtq_reallyold = rtq_minreallyold;
382 		}
383 
384 		last_adjusted_timeout = time_second;
385 #ifdef DIAGNOSTIC
386 		log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
387 		    rtq_reallyold);
388 #endif
389 		arg.found = arg.killed = 0;
390 		arg.updating = 1;
391 		s = splnet();
392 		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
393 		splx(s);
394 	}
395 
396 	atv.tv_usec = 0;
397 	atv.tv_sec = arg.nextstop;
398 	timeout(in6_rtqtimo, rock, tvtohz(&atv));
399 }
400 
401 /*
402  * Age old PMTUs.
403  */
404 struct mtuex_arg {
405 	struct radix_node_head *rnh;
406 	time_t nextstop;
407 };
408 
409 static int
410 in6_mtuexpire(struct radix_node *rn, void *rock)
411 {
412 	struct rtentry *rt = (struct rtentry *)rn;
413 	struct mtuex_arg *ap = rock;
414 
415 	/* sanity */
416 	if (!rt)
417 		panic("rt == NULL in in6_mtuexpire");
418 
419 	if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
420 		if (rt->rt_rmx.rmx_expire <= time_second) {
421 			rt->rt_flags |= RTF_PROBEMTU;
422 		} else {
423 			ap->nextstop = lmin(ap->nextstop,
424 					rt->rt_rmx.rmx_expire);
425 		}
426 	}
427 
428 	return 0;
429 }
430 
431 #define	MTUTIMO_DEFAULT	(60*1)
432 
433 static void
434 in6_mtutimo(void *rock)
435 {
436 	struct radix_node_head *rnh = rock;
437 	struct mtuex_arg arg;
438 	struct timeval atv;
439 	int s;
440 
441 	arg.rnh = rnh;
442 	arg.nextstop = time_second + MTUTIMO_DEFAULT;
443 	s = splnet();
444 	rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
445 	splx(s);
446 
447 	atv.tv_usec = 0;
448 	atv.tv_sec = arg.nextstop;
449 	if (atv.tv_sec < time_second) {
450 		printf("invalid mtu expiration time on routing table\n");
451 		arg.nextstop = time_second + 30;	/*last resort*/
452 	}
453 	timeout(in6_mtutimo, rock, tvtohz(&atv));
454 }
455 
456 /*
457  * Initialize our routing tree.
458  */
459 int
460 in6_inithead(void **head, int off)
461 {
462 	struct radix_node_head *rnh;
463 
464 	if (!rn_inithead(head, off))
465 		return 0;
466 
467 	if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
468 		return 1;	/* only do this for the real routing table */
469 
470 	rnh = *head;
471 	rnh->rnh_addaddr = in6_addroute;
472 	rnh->rnh_matchaddr = in6_matroute;
473 	rnh->rnh_close = in6_clsroute;
474 	in6_rtqtimo(rnh);	/* kick off timeout first time */
475 	in6_mtutimo(rnh);	/* kick off timeout first time */
476 	return 1;
477 }
478