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