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