Merge from vendor branch CVS:
[dragonfly.git] / sys / netinet6 / in6_rmx.c
1 /*      $FreeBSD: src/sys/netinet6/in6_rmx.c,v 1.1.2.4 2004/10/06 02:35:17 suz Exp $    */
2 /*      $DragonFly: src/sys/netinet6/in6_rmx.c,v 1.13 2005/06/03 19:56:08 eirikn Exp $  */
3 /*      $KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $        */
4
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright 1994, 1995 Massachusetts Institute of Technology
36  *
37  * Permission to use, copy, modify, and distribute this software and
38  * its documentation for any purpose and without fee is hereby
39  * granted, provided that both the above copyright notice and this
40  * permission notice appear in all copies, that both the above
41  * copyright notice and this permission notice appear in all
42  * supporting documentation, and that the name of M.I.T. not be used
43  * in advertising or publicity pertaining to distribution of the
44  * software without specific, written prior permission.  M.I.T. makes
45  * no representations about the suitability of this software for any
46  * purpose.  It is provided "as is" without express or implied
47  * warranty.
48  *
49  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
50  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
51  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
53  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
56  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  */
63
64 /*
65  * This code does two things necessary for the enhanced TCP metrics to
66  * function in a useful manner:
67  *  1) It marks all non-host routes as `cloning', thus ensuring that
68  *     every actual reference to such a route actually gets turned
69  *     into a reference to a host route to the specific destination
70  *     requested.
71  *  2) When such routes lose all their references, it arranges for them
72  *     to be deleted in some random collection of circumstances, so that
73  *     a large quantity of stale routing data is not kept in kernel memory
74  *     indefinitely.  See in6_rtqtimo() below for the exact mechanism.
75  */
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/queue.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/mbuf.h>
85 #include <sys/syslog.h>
86 #include <sys/thread2.h>
87
88 #include <net/if.h>
89 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/in_var.h>
93
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
96
97 #include <netinet/icmp6.h>
98
99 #include <netinet/tcp.h>
100 #include <netinet/tcp_seq.h>
101 #include <netinet/tcp_timer.h>
102 #include <netinet/tcp_var.h>
103
104 static struct callout   in6_rtqtimo_ch;
105 static struct callout   in6_mtutimo_ch;
106
107 extern 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  */
114 static struct radix_node *
115 in6_addroute(char *key, char *mask, 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         /*
123          * For IPv6, all unicast non-host routes are automatically cloning.
124          */
125         if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
126                 rt->rt_flags |= RTF_MULTICAST;
127
128         if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
129                 rt->rt_flags |= RTF_PRCLONING;
130         }
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_rmx.rmx_locks & RTV_MTU) &&
155             rt->rt_ifp != NULL)
156                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
157
158         ret = rn_addroute(key, mask, head, treenodes);
159         if (ret == NULL && rt->rt_flags & RTF_HOST) {
160                 struct rtentry *rt2;
161
162                 /*
163                  * We are trying to add a host route, but can't.
164                  * Find out if it is because of an
165                  * ARP entry and delete it if so.
166                  */
167                 rt2 = rtpurelookup((struct sockaddr *)sin6);
168                 if (rt2 != NULL) {
169                         --rt2->rt_refcnt;
170                         if (rt2->rt_flags & RTF_LLINFO &&
171                             rt2->rt_flags & RTF_HOST &&
172                             rt2->rt_gateway &&
173                             rt2->rt_gateway->sa_family == AF_LINK) {
174                                 rtrequest(RTM_DELETE, rt_key(rt2),
175                                           rt2->rt_gateway, rt_mask(rt2),
176                                           rt2->rt_flags, NULL);
177                                 ret = rn_addroute(key, mask, head, treenodes);
178                         }
179                 }
180         } else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
181                 struct rtentry *rt2;
182
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 = rtpurelookup((struct sockaddr *)sin6);
196                 if (rt2 != NULL) {
197                         if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
198                                         == RTF_CLONING &&
199                             rt2->rt_gateway &&
200                             rt2->rt_gateway->sa_family == AF_LINK &&
201                             rt2->rt_ifp == rt->rt_ifp) {
202                                 ret = rt2->rt_nodes;
203                         }
204                         --rt2->rt_refcnt;
205                 }
206         }
207         return ret;
208 }
209
210 /*
211  * This code is the inverse of in6_clsroute: on first reference, if we
212  * were managing the route, stop doing so and set the expiration timer
213  * back off again.
214  */
215 static struct radix_node *
216 in6_matchroute(char *key, struct radix_node_head *head)
217 {
218         struct radix_node *rn = rn_match(key, head);
219         struct rtentry *rt = (struct rtentry *)rn;
220
221         if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
222                 if (rt->rt_flags & RTPRF_OURS) {
223                         rt->rt_flags &= ~RTPRF_OURS;
224                         rt->rt_rmx.rmx_expire = 0;
225                 }
226         }
227         return rn;
228 }
229
230 SYSCTL_DECL(_net_inet6_ip6);
231
232 static int rtq_reallyold = 60*60;
233         /* one hour is ``really old'' */
234 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
235         CTLFLAG_RW, &rtq_reallyold , 0, "");
236                                 
237 static int rtq_minreallyold = 10;
238         /* never automatically crank down to less */
239 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
240         CTLFLAG_RW, &rtq_minreallyold , 0, "");
241                                 
242 static int rtq_toomany = 128;
243         /* 128 cached routes is ``too many'' */
244 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
245         CTLFLAG_RW, &rtq_toomany , 0, "");
246                                 
247
248 /*
249  * On last reference drop, mark the route as belong to us so that it can be
250  * timed out.
251  */
252 static void
253 in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
254 {
255         struct rtentry *rt = (struct rtentry *)rn;
256
257         if (!(rt->rt_flags & RTF_UP))
258                 return;         /* prophylactic measures */
259
260         if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
261                 return;
262
263         if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
264                 return;
265
266         /*
267          * As requested by David Greenman:
268          * If rtq_reallyold is 0, just delete the route without
269          * waiting for a timeout cycle to kill it.
270          */
271         if (rtq_reallyold != 0) {
272                 rt->rt_flags |= RTPRF_OURS;
273                 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
274         } else {
275                 /*
276                  * Remove route from the radix tree, but defer deallocation
277                  * until we return to rtfree().
278                  */
279                 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
280                           rt->rt_flags, &rt);
281         }
282 }
283
284 struct rtqk_arg {
285         struct radix_node_head *rnh;
286         int mode;
287         int updating;
288         int draining;
289         int killed;
290         int found;
291         time_t nextstop;
292 };
293
294 /*
295  * Get rid of old routes.  When draining, this deletes everything, even when
296  * the timeout is not expired yet.  When updating, this makes sure that
297  * nothing has a timeout longer than the current value of rtq_reallyold.
298  */
299 static int
300 in6_rtqkill(struct radix_node *rn, void *rock)
301 {
302         struct rtqk_arg *ap = rock;
303         struct rtentry *rt = (struct rtentry *)rn;
304         int err;
305
306         if (rt->rt_flags & RTPRF_OURS) {
307                 ap->found++;
308
309                 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
310                         if (rt->rt_refcnt > 0)
311                                 panic("rtqkill route really not free");
312
313                         err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
314                                         rt_mask(rt), rt->rt_flags, NULL);
315                         if (err)
316                                 log(LOG_WARNING, "in6_rtqkill: error %d", err);
317                         else
318                                 ap->killed++;
319                 } else {
320                         if (ap->updating &&
321                             (rt->rt_rmx.rmx_expire - time_second >
322                              rtq_reallyold)) {
323                                 rt->rt_rmx.rmx_expire =
324                                     time_second + rtq_reallyold;
325                         }
326                         ap->nextstop = lmin(ap->nextstop,
327                                             rt->rt_rmx.rmx_expire);
328                 }
329         }
330
331         return 0;
332 }
333
334 #define RTQ_TIMEOUT     60*10   /* run no less than once every ten minutes */
335 static int rtq_timeout = RTQ_TIMEOUT;
336
337 static void
338 in6_rtqtimo(void *rock)
339 {
340         struct radix_node_head *rnh = rock;
341         struct rtqk_arg arg;
342         struct timeval atv;
343         static time_t last_adjusted_timeout = 0;
344
345         arg.found = arg.killed = 0;
346         arg.rnh = rnh;
347         arg.nextstop = time_second + rtq_timeout;
348         arg.draining = arg.updating = 0;
349         crit_enter();
350         rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
351         crit_exit();
352
353         /*
354          * Attempt to be somewhat dynamic about this:
355          * If there are ``too many'' routes sitting around taking up space,
356          * then crank down the timeout, and see if we can't make some more
357          * go away.  However, we make sure that we will never adjust more
358          * than once in rtq_timeout seconds, to keep from cranking down too
359          * hard.
360          */
361         if ((arg.found - arg.killed > rtq_toomany)
362            && (time_second - last_adjusted_timeout >= rtq_timeout)
363            && rtq_reallyold > rtq_minreallyold) {
364                 rtq_reallyold = 2*rtq_reallyold / 3;
365                 if (rtq_reallyold < rtq_minreallyold) {
366                         rtq_reallyold = rtq_minreallyold;
367                 }
368
369                 last_adjusted_timeout = time_second;
370 #ifdef DIAGNOSTIC
371                 log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
372                     rtq_reallyold);
373 #endif
374                 arg.found = arg.killed = 0;
375                 arg.updating = 1;
376                 crit_enter();
377                 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
378                 crit_exit();
379         }
380
381         atv.tv_usec = 0;
382         atv.tv_sec = arg.nextstop;
383         callout_reset(&in6_rtqtimo_ch, tvtohz_high(&atv), in6_rtqtimo, rock);
384 }
385
386 /*
387  * Age old PMTUs.
388  */
389 struct mtuex_arg {
390         struct radix_node_head *rnh;
391         time_t nextstop;
392 };
393
394 static int
395 in6_mtuexpire(struct radix_node *rn, void *rock)
396 {
397         struct rtentry *rt = (struct rtentry *)rn;
398         struct mtuex_arg *ap = rock;
399
400         /* sanity */
401         if (!rt)
402                 panic("rt == NULL in in6_mtuexpire");
403
404         if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
405                 if (rt->rt_rmx.rmx_expire <= time_second) {
406                         rt->rt_flags |= RTF_PROBEMTU;
407                 } else {
408                         ap->nextstop = lmin(ap->nextstop,
409                                         rt->rt_rmx.rmx_expire);
410                 }
411         }
412
413         return 0;
414 }
415
416 #define MTUTIMO_DEFAULT (60*1)
417
418 static void
419 in6_mtutimo(void *rock)
420 {
421         struct radix_node_head *rnh = rock;
422         struct mtuex_arg arg;
423         struct timeval atv;
424
425         arg.rnh = rnh;
426         arg.nextstop = time_second + MTUTIMO_DEFAULT;
427         crit_enter();
428         rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
429         crit_exit();
430
431         atv.tv_usec = 0;
432         atv.tv_sec = arg.nextstop;
433         if (atv.tv_sec < time_second) {
434                 printf("invalid mtu expiration time on routing table\n");
435                 arg.nextstop = time_second + 30;        /* last resort */
436         }
437         callout_reset(&in6_mtutimo_ch, tvtohz_high(&atv), in6_mtutimo, rock);
438 }
439
440 #if 0
441 void
442 in6_rtqdrain(void)
443 {
444         struct radix_node_head *rnh = rt_tables[AF_INET6];
445         struct rtqk_arg arg;
446
447         arg.found = arg.killed = 0;
448         arg.rnh = rnh;
449         arg.nextstop = 0;
450         arg.draining = 1;
451         arg.updating = 0;
452         crit_enter();
453         rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
454         crit_exit();
455 }
456 #endif
457
458 /*
459  * Initialize our routing tree.
460  */
461 int
462 in6_inithead(void **head, int off)
463 {
464         struct radix_node_head *rnh;
465
466         if (!rn_inithead(head, off))
467                 return 0;
468
469         if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
470                 return 1;       /* only do this for the real routing table */
471
472         rnh = *head;
473         rnh->rnh_addaddr = in6_addroute;
474         rnh->rnh_matchaddr = in6_matchroute;
475         rnh->rnh_close = in6_clsroute;
476         callout_init(&in6_mtutimo_ch);
477         callout_init(&in6_rtqtimo_ch);
478         in6_rtqtimo(rnh);       /* kick off timeout first time */
479         in6_mtutimo(rnh);       /* kick off timeout first time */
480         return 1;
481 }