in6_rmx.c revision 181803
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 *	$KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $
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 */
61
62/*
63 * This code does two things necessary for the enhanced TCP metrics to
64 * function in a useful manner:
65 *  1) It marks all non-host routes as `cloning', thus ensuring that
66 *     every actual reference to such a route actually gets turned
67 *     into a reference to a host route to the specific destination
68 *     requested.
69 *  2) When such routes lose all their references, it arranges for them
70 *     to be deleted in some random collection of circumstances, so that
71 *     a large quantity of stale routing data is not kept in kernel memory
72 *     indefinitely.  See in6_rtqtimo() below for the exact mechanism.
73 */
74
75#include <sys/cdefs.h>
76__FBSDID("$FreeBSD: head/sys/netinet6/in6_rmx.c 181803 2008-08-17 23:27:27Z bz $");
77
78#include <sys/param.h>
79#include <sys/systm.h>
80#include <sys/kernel.h>
81#include <sys/sysctl.h>
82#include <sys/queue.h>
83#include <sys/socket.h>
84#include <sys/socketvar.h>
85#include <sys/mbuf.h>
86#include <sys/syslog.h>
87#include <sys/callout.h>
88#include <sys/vimage.h>
89
90#include <net/if.h>
91#include <net/route.h>
92#include <netinet/in.h>
93#include <netinet/ip_var.h>
94#include <netinet/in_var.h>
95
96#include <netinet/ip6.h>
97#include <netinet6/ip6_var.h>
98
99#include <netinet/icmp6.h>
100#include <netinet6/nd6.h>
101
102#include <netinet/tcp.h>
103#include <netinet/tcp_seq.h>
104#include <netinet/tcp_timer.h>
105#include <netinet/tcp_var.h>
106
107extern int	in6_inithead(void **head, int off);
108
109#define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
110
111/*
112 * Do what we need to do when inserting a route.
113 */
114static struct radix_node *
115in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
116    struct radix_node *treenodes)
117{
118	struct rtentry *rt = (struct rtentry *)treenodes;
119	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
120	struct radix_node *ret;
121
122	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
123		rt->rt_flags |= RTF_MULTICAST;
124
125	/*
126	 * A little bit of help for both IPv6 output and input:
127	 *   For local addresses, we make sure that RTF_LOCAL is set,
128	 *   with the thought that this might one day be used to speed up
129	 *   ip_input().
130	 *
131	 * We also mark routes to multicast addresses as such, because
132	 * it's easy to do and might be useful (but this is much more
133	 * dubious since it's so easy to inspect the address).  (This
134	 * is done above.)
135	 *
136	 * XXX
137	 * should elaborate the code.
138	 */
139	if (rt->rt_flags & RTF_HOST) {
140		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
141					->sin6_addr,
142				       &sin6->sin6_addr)) {
143			rt->rt_flags |= RTF_LOCAL;
144		}
145	}
146
147	if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
148		rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
149
150	ret = rn_addroute(v_arg, n_arg, head, treenodes);
151	if (ret == NULL && rt->rt_flags & RTF_HOST) {
152		struct rtentry *rt2;
153		/*
154		 * We are trying to add a host route, but can't.
155		 * Find out if it is because of an
156		 * ARP entry and delete it if so.
157		 */
158		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
159		if (rt2) {
160			if (rt2->rt_flags & RTF_LLINFO &&
161				rt2->rt_flags & RTF_HOST &&
162				rt2->rt_gateway &&
163				rt2->rt_gateway->sa_family == AF_LINK) {
164				rtexpunge(rt2);
165				RTFREE_LOCKED(rt2);
166				ret = rn_addroute(v_arg, n_arg, head,
167					treenodes);
168			} else
169				RTFREE_LOCKED(rt2);
170		}
171	} else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
172		struct rtentry *rt2;
173		/*
174		 * We are trying to add a net route, but can't.
175		 * The following case should be allowed, so we'll make a
176		 * special check for this:
177		 *	Two IPv6 addresses with the same prefix is assigned
178		 *	to a single interrface.
179		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
180		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
181		 *	In this case, (*1) and (*2) want to add the same
182		 *	net route entry, 3ffe:0501:: -> if0.
183		 *	This case should not raise an error.
184		 */
185		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
186		if (rt2) {
187			if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
188					== RTF_CLONING
189			 && rt2->rt_gateway
190			 && rt2->rt_gateway->sa_family == AF_LINK
191			 && rt2->rt_ifp == rt->rt_ifp) {
192				ret = rt2->rt_nodes;
193			}
194			RTFREE_LOCKED(rt2);
195		}
196	}
197	return ret;
198}
199
200/*
201 * This code is the inverse of in6_clsroute: on first reference, if we
202 * were managing the route, stop doing so and set the expiration timer
203 * back off again.
204 */
205static struct radix_node *
206in6_matroute(void *v_arg, struct radix_node_head *head)
207{
208	struct radix_node *rn = rn_match(v_arg, head);
209	struct rtentry *rt = (struct rtentry *)rn;
210
211	if (rt && rt->rt_refcnt == 0) { /* this is first reference */
212		if (rt->rt_flags & RTPRF_OURS) {
213			rt->rt_flags &= ~RTPRF_OURS;
214			rt->rt_rmx.rmx_expire = 0;
215		}
216	}
217	return rn;
218}
219
220SYSCTL_DECL(_net_inet6_ip6);
221
222static int rtq_reallyold = 60*60;
223	/* one hour is ``really old'' */
224SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
225	CTLFLAG_RW, &rtq_reallyold , 0, "");
226
227static int rtq_minreallyold = 10;
228	/* never automatically crank down to less */
229SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
230	CTLFLAG_RW, &rtq_minreallyold , 0, "");
231
232static int rtq_toomany = 128;
233	/* 128 cached routes is ``too many'' */
234SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
235	CTLFLAG_RW, &rtq_toomany , 0, "");
236
237
238/*
239 * On last reference drop, mark the route as belong to us so that it can be
240 * timed out.
241 */
242static void
243in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
244{
245	struct rtentry *rt = (struct rtentry *)rn;
246
247	RT_LOCK_ASSERT(rt);
248
249	if (!(rt->rt_flags & RTF_UP))
250		return;		/* prophylactic measures */
251
252	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
253		return;
254
255	if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
256		return;
257
258	/*
259	 * As requested by David Greenman:
260	 * If rtq_reallyold is 0, just delete the route without
261	 * waiting for a timeout cycle to kill it.
262	 */
263	if (V_rtq_reallyold != 0) {
264		rt->rt_flags |= RTPRF_OURS;
265		rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold;
266	} else {
267		rtexpunge(rt);
268	}
269}
270
271struct rtqk_arg {
272	struct radix_node_head *rnh;
273	int mode;
274	int updating;
275	int draining;
276	int killed;
277	int found;
278	time_t nextstop;
279};
280
281/*
282 * Get rid of old routes.  When draining, this deletes everything, even when
283 * the timeout is not expired yet.  When updating, this makes sure that
284 * nothing has a timeout longer than the current value of rtq_reallyold.
285 */
286static int
287in6_rtqkill(struct radix_node *rn, void *rock)
288{
289	struct rtqk_arg *ap = rock;
290	struct rtentry *rt = (struct rtentry *)rn;
291	int err;
292
293	if (rt->rt_flags & RTPRF_OURS) {
294		ap->found++;
295
296		if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
297			if (rt->rt_refcnt > 0)
298				panic("rtqkill route really not free");
299
300			err = rtrequest(RTM_DELETE,
301					(struct sockaddr *)rt_key(rt),
302					rt->rt_gateway, rt_mask(rt),
303					rt->rt_flags, 0);
304			if (err) {
305				log(LOG_WARNING, "in6_rtqkill: error %d", err);
306			} else {
307				ap->killed++;
308			}
309		} else {
310			if (ap->updating
311			   && (rt->rt_rmx.rmx_expire - time_uptime
312			       > V_rtq_reallyold)) {
313				rt->rt_rmx.rmx_expire = time_uptime
314					+ V_rtq_reallyold;
315			}
316			ap->nextstop = lmin(ap->nextstop,
317					    rt->rt_rmx.rmx_expire);
318		}
319	}
320
321	return 0;
322}
323
324#define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
325static int rtq_timeout6 = RTQ_TIMEOUT;
326static struct callout rtq_timer6;
327
328static void
329in6_rtqtimo(void *rock)
330{
331	struct radix_node_head *rnh = rock;
332	struct rtqk_arg arg;
333	struct timeval atv;
334	static time_t last_adjusted_timeout = 0;
335
336	arg.found = arg.killed = 0;
337	arg.rnh = rnh;
338	arg.nextstop = time_uptime + V_rtq_timeout6;
339	arg.draining = arg.updating = 0;
340	RADIX_NODE_HEAD_LOCK(rnh);
341	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
342	RADIX_NODE_HEAD_UNLOCK(rnh);
343
344	/*
345	 * Attempt to be somewhat dynamic about this:
346	 * If there are ``too many'' routes sitting around taking up space,
347	 * then crank down the timeout, and see if we can't make some more
348	 * go away.  However, we make sure that we will never adjust more
349	 * than once in rtq_timeout6 seconds, to keep from cranking down too
350	 * hard.
351	 */
352	if ((arg.found - arg.killed > V_rtq_toomany)
353	   && (time_uptime - last_adjusted_timeout >= V_rtq_timeout6)
354	   && V_rtq_reallyold > V_rtq_minreallyold) {
355		V_rtq_reallyold = 2*V_rtq_reallyold / 3;
356		if (V_rtq_reallyold < V_rtq_minreallyold) {
357			V_rtq_reallyold = V_rtq_minreallyold;
358		}
359
360		last_adjusted_timeout = time_uptime;
361#ifdef DIAGNOSTIC
362		log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
363		    V_rtq_reallyold);
364#endif
365		arg.found = arg.killed = 0;
366		arg.updating = 1;
367		RADIX_NODE_HEAD_LOCK(rnh);
368		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
369		RADIX_NODE_HEAD_UNLOCK(rnh);
370	}
371
372	atv.tv_usec = 0;
373	atv.tv_sec = arg.nextstop - time_uptime;
374	callout_reset(&V_rtq_timer6, tvtohz(&atv), in6_rtqtimo, rock);
375}
376
377/*
378 * Age old PMTUs.
379 */
380struct mtuex_arg {
381	struct radix_node_head *rnh;
382	time_t nextstop;
383};
384static struct callout rtq_mtutimer;
385
386static int
387in6_mtuexpire(struct radix_node *rn, void *rock)
388{
389	struct rtentry *rt = (struct rtentry *)rn;
390	struct mtuex_arg *ap = rock;
391
392	/* sanity */
393	if (!rt)
394		panic("rt == NULL in in6_mtuexpire");
395
396	if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
397		if (rt->rt_rmx.rmx_expire <= time_uptime) {
398			rt->rt_flags |= RTF_PROBEMTU;
399		} else {
400			ap->nextstop = lmin(ap->nextstop,
401					rt->rt_rmx.rmx_expire);
402		}
403	}
404
405	return 0;
406}
407
408#define	MTUTIMO_DEFAULT	(60*1)
409
410static void
411in6_mtutimo(void *rock)
412{
413	struct radix_node_head *rnh = rock;
414	struct mtuex_arg arg;
415	struct timeval atv;
416
417	arg.rnh = rnh;
418	arg.nextstop = time_uptime + MTUTIMO_DEFAULT;
419	RADIX_NODE_HEAD_LOCK(rnh);
420	rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
421	RADIX_NODE_HEAD_UNLOCK(rnh);
422
423	atv.tv_usec = 0;
424	atv.tv_sec = arg.nextstop - time_uptime;
425	if (atv.tv_sec < 0) {
426		printf("invalid mtu expiration time on routing table\n");
427		arg.nextstop = time_uptime + 30;	/* last resort */
428		atv.tv_sec = 30;
429	}
430	callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
431}
432
433#if 0
434void
435in6_rtqdrain(void)
436{
437	struct radix_node_head *rnh = V_rt_tables[AF_INET6];
438	struct rtqk_arg arg;
439
440	arg.found = arg.killed = 0;
441	arg.rnh = rnh;
442	arg.nextstop = 0;
443	arg.draining = 1;
444	arg.updating = 0;
445	RADIX_NODE_HEAD_LOCK(rnh);
446	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
447	RADIX_NODE_HEAD_UNLOCK(rnh);
448}
449#endif
450
451/*
452 * Initialize our routing tree.
453 * XXX MRT When off == 0, we are being called from vfs_export.c
454 * so just set up their table and leave. (we know what the correct
455 * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd
456 * see also comments in in_inithead() vfs_export.c and domain.h
457 */
458int
459in6_inithead(void **head, int off)
460{
461	struct radix_node_head *rnh;
462
463	if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3))
464		return 0;		/* See above */
465
466	if (off == 0)		/* See above */
467		return 1;	/* only do the rest for the real thing */
468
469	rnh = *head;
470	rnh->rnh_addaddr = in6_addroute;
471	rnh->rnh_matchaddr = in6_matroute;
472	rnh->rnh_close = in6_clsroute;
473	callout_init(&V_rtq_timer6, CALLOUT_MPSAFE);
474	in6_rtqtimo(rnh);	/* kick off timeout first time */
475	callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE);
476	in6_mtutimo(rnh);	/* kick off timeout first time */
477	return 1;
478}
479