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