in6_rmx.c revision 62604
1/*	$FreeBSD: head/sys/netinet6/in6_rmx.c 62604 2000-07-05 01:40:29Z itojun $	*/
2/*	$KAME: in6_rmx.c,v 1.7 2000/04/06 08:30:43 sumikawa Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright 1994, 1995 Massachusetts Institute of Technology
35 *
36 * Permission to use, copy, modify, and distribute this software and
37 * its documentation for any purpose and without fee is hereby
38 * granted, provided that both the above copyright notice and this
39 * permission notice appear in all copies, that both the above
40 * copyright notice and this permission notice appear in all
41 * supporting documentation, and that the name of M.I.T. not be used
42 * in advertising or publicity pertaining to distribution of the
43 * software without specific, written prior permission.  M.I.T. makes
44 * no representations about the suitability of this software for any
45 * purpose.  It is provided "as is" without express or implied
46 * warranty.
47 *
48 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
49 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
50 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
51 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
52 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
55 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
58 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
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 <netinet/ip6.h>
93#include <netinet6/ip6_var.h>
94
95#include <netinet/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
102extern 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 */
109static struct radix_node *
110in6_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 */
229static struct radix_node *
230in6_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
244SYSCTL_DECL(_net_inet6_ip6);
245
246static int rtq_reallyold = 60*60;
247	/* one hour is ``really old'' */
248SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
249	CTLFLAG_RW, &rtq_reallyold , 0, "");
250
251static int rtq_minreallyold = 10;
252	/* never automatically crank down to less */
253SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
254	CTLFLAG_RW, &rtq_minreallyold , 0, "");
255
256static int rtq_toomany = 128;
257	/* 128 cached routes is ``too many'' */
258SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
259	CTLFLAG_RW, &rtq_toomany , 0, "");
260
261
262/*
263 * On last reference drop, mark the route as belong to us so that it can be
264 * timed out.
265 */
266static void
267in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
268{
269	struct rtentry *rt = (struct rtentry *)rn;
270
271	if (!(rt->rt_flags & RTF_UP))
272		return;		/* prophylactic measures */
273
274	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
275		return;
276
277	if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
278	   != RTF_WASCLONED)
279		return;
280
281	/*
282	 * As requested by David Greenman:
283	 * If rtq_reallyold is 0, just delete the route without
284	 * waiting for a timeout cycle to kill it.
285	 */
286	if (rtq_reallyold != 0) {
287		rt->rt_flags |= RTPRF_OURS;
288		rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
289	} else {
290		rtrequest(RTM_DELETE,
291			  (struct sockaddr *)rt_key(rt),
292			  rt->rt_gateway, rt_mask(rt),
293			  rt->rt_flags, 0);
294	}
295}
296
297struct rtqk_arg {
298	struct radix_node_head *rnh;
299	int mode;
300	int updating;
301	int draining;
302	int killed;
303	int found;
304	time_t nextstop;
305};
306
307/*
308 * Get rid of old routes.  When draining, this deletes everything, even when
309 * the timeout is not expired yet.  When updating, this makes sure that
310 * nothing has a timeout longer than the current value of rtq_reallyold.
311 */
312static int
313in6_rtqkill(struct radix_node *rn, void *rock)
314{
315	struct rtqk_arg *ap = rock;
316	struct rtentry *rt = (struct rtentry *)rn;
317	int err;
318
319	if (rt->rt_flags & RTPRF_OURS) {
320		ap->found++;
321
322		if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
323			if (rt->rt_refcnt > 0)
324				panic("rtqkill route really not free");
325
326			err = rtrequest(RTM_DELETE,
327					(struct sockaddr *)rt_key(rt),
328					rt->rt_gateway, rt_mask(rt),
329					rt->rt_flags, 0);
330			if (err) {
331				log(LOG_WARNING, "in6_rtqkill: error %d", err);
332			} else {
333				ap->killed++;
334			}
335		} else {
336			if (ap->updating
337			   && (rt->rt_rmx.rmx_expire - time_second
338			       > rtq_reallyold)) {
339				rt->rt_rmx.rmx_expire = time_second
340					+ rtq_reallyold;
341			}
342			ap->nextstop = lmin(ap->nextstop,
343					    rt->rt_rmx.rmx_expire);
344		}
345	}
346
347	return 0;
348}
349
350#define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
351static int rtq_timeout = RTQ_TIMEOUT;
352
353static void
354in6_rtqtimo(void *rock)
355{
356	struct radix_node_head *rnh = rock;
357	struct rtqk_arg arg;
358	struct timeval atv;
359	static time_t last_adjusted_timeout = 0;
360	int s;
361
362	arg.found = arg.killed = 0;
363	arg.rnh = rnh;
364	arg.nextstop = time_second + rtq_timeout;
365	arg.draining = arg.updating = 0;
366	s = splnet();
367	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
368	splx(s);
369
370	/*
371	 * Attempt to be somewhat dynamic about this:
372	 * If there are ``too many'' routes sitting around taking up space,
373	 * then crank down the timeout, and see if we can't make some more
374	 * go away.  However, we make sure that we will never adjust more
375	 * than once in rtq_timeout seconds, to keep from cranking down too
376	 * hard.
377	 */
378	if ((arg.found - arg.killed > rtq_toomany)
379	   && (time_second - last_adjusted_timeout >= rtq_timeout)
380	   && rtq_reallyold > rtq_minreallyold) {
381		rtq_reallyold = 2*rtq_reallyold / 3;
382		if (rtq_reallyold < rtq_minreallyold) {
383			rtq_reallyold = rtq_minreallyold;
384		}
385
386		last_adjusted_timeout = time_second;
387#ifdef DIAGNOSTIC
388		log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
389		    rtq_reallyold);
390#endif
391		arg.found = arg.killed = 0;
392		arg.updating = 1;
393		s = splnet();
394		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
395		splx(s);
396	}
397
398	atv.tv_usec = 0;
399	atv.tv_sec = arg.nextstop;
400	timeout(in6_rtqtimo, rock, tvtohz(&atv));
401}
402
403/*
404 * Age old PMTUs.
405 */
406struct mtuex_arg {
407	struct radix_node_head *rnh;
408	time_t nextstop;
409};
410
411static int
412in6_mtuexpire(struct radix_node *rn, void *rock)
413{
414	struct rtentry *rt = (struct rtentry *)rn;
415	struct mtuex_arg *ap = rock;
416
417	/* sanity */
418	if (!rt)
419		panic("rt == NULL in in6_mtuexpire");
420
421	if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
422		if (rt->rt_rmx.rmx_expire <= time_second) {
423			rt->rt_flags |= RTF_PROBEMTU;
424		} else {
425			ap->nextstop = lmin(ap->nextstop,
426					rt->rt_rmx.rmx_expire);
427		}
428	}
429
430	return 0;
431}
432
433#define	MTUTIMO_DEFAULT	(60*1)
434
435static void
436in6_mtutimo(void *rock)
437{
438	struct radix_node_head *rnh = rock;
439	struct mtuex_arg arg;
440	struct timeval atv;
441	int s;
442
443	arg.rnh = rnh;
444	arg.nextstop = time_second + MTUTIMO_DEFAULT;
445	s = splnet();
446	rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
447	splx(s);
448
449	atv.tv_usec = 0;
450	atv.tv_sec = arg.nextstop;
451	if (atv.tv_sec < time_second) {
452		printf("invalid mtu expiration time on routing table\n");
453		arg.nextstop = time_second + 30;	/*last resort*/
454	}
455	timeout(in6_mtutimo, rock, tvtohz(&atv));
456}
457
458#if 0
459void
460in6_rtqdrain()
461{
462	struct radix_node_head *rnh = rt_tables[AF_INET6];
463	struct rtqk_arg arg;
464	int s;
465	arg.found = arg.killed = 0;
466	arg.rnh = rnh;
467	arg.nextstop = 0;
468	arg.draining = 1;
469	arg.updating = 0;
470	s = splnet();
471	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
472	splx(s);
473}
474#endif
475
476/*
477 * Initialize our routing tree.
478 */
479int
480in6_inithead(void **head, int off)
481{
482	struct radix_node_head *rnh;
483
484	if (!rn_inithead(head, off))
485		return 0;
486
487	if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
488		return 1;	/* only do this for the real routing table */
489
490	rnh = *head;
491	rnh->rnh_addaddr = in6_addroute;
492	rnh->rnh_matchaddr = in6_matroute;
493	rnh->rnh_close = in6_clsroute;
494	in6_rtqtimo(rnh);	/* kick off timeout first time */
495	in6_mtutimo(rnh);	/* kick off timeout first time */
496	return 1;
497}
498