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