well, if netproto doesnt need old prototypes inet6 doesnt either
[dragonfly.git] / sys / netinet6 / in6_rmx.c
1 /*      $FreeBSD: src/sys/netinet6/in6_rmx.c,v 1.1.2.3 2002/04/28 05:40:27 suz Exp $    */
2 /*      $DragonFly: src/sys/netinet6/in6_rmx.c,v 1.3 2003/08/23 11:02:45 rob 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
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 (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                                 rtrequest(RTM_DELETE,
170                                           (struct sockaddr *)rt_key(rt2),
171                                           rt2->rt_gateway,
172                                           rt_mask(rt2), rt2->rt_flags, 0);
173                                 ret = rn_addroute(v_arg, n_arg, head,
174                                         treenodes);
175                         }
176                         RTFREE(rt2);
177                 }
178         } else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
179                 struct rtentry *rt2;
180                 /*
181                  * We are trying to add a net route, but can't.
182                  * The following case should be allowed, so we'll make a
183                  * special check for this:
184                  *      Two IPv6 addresses with the same prefix is assigned
185                  *      to a single interrface.
186                  *      # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
187                  *      # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
188                  *      In this case, (*1) and (*2) want to add the same
189                  *      net route entry, 3ffe:0501:: -> if0.
190                  *      This case should not raise an error.
191                  */
192                 rt2 = rtalloc1((struct sockaddr *)sin6, 0,
193                                 RTF_CLONING | RTF_PRCLONING);
194                 if (rt2) {
195                         if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
196                                         == RTF_CLONING
197                          && rt2->rt_gateway
198                          && rt2->rt_gateway->sa_family == AF_LINK
199                          && rt2->rt_ifp == rt->rt_ifp) {
200                                 ret = rt2->rt_nodes;
201                         }
202                         RTFREE(rt2);
203                 }
204         }
205         return ret;
206 }
207
208 /*
209  * This code is the inverse of in6_clsroute: on first reference, if we
210  * were managing the route, stop doing so and set the expiration timer
211  * back off again.
212  */
213 static struct radix_node *
214 in6_matroute(void *v_arg, struct radix_node_head *head)
215 {
216         struct radix_node *rn = rn_match(v_arg, head);
217         struct rtentry *rt = (struct rtentry *)rn;
218
219         if (rt && rt->rt_refcnt == 0) { /* this is first reference */
220                 if (rt->rt_flags & RTPRF_OURS) {
221                         rt->rt_flags &= ~RTPRF_OURS;
222                         rt->rt_rmx.rmx_expire = 0;
223                 }
224         }
225         return rn;
226 }
227
228 SYSCTL_DECL(_net_inet6_ip6);
229
230 static int rtq_reallyold = 60*60;
231         /* one hour is ``really old'' */
232 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
233         CTLFLAG_RW, &rtq_reallyold , 0, "");
234                                 
235 static int rtq_minreallyold = 10;
236         /* never automatically crank down to less */
237 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
238         CTLFLAG_RW, &rtq_minreallyold , 0, "");
239                                 
240 static int rtq_toomany = 128;
241         /* 128 cached routes is ``too many'' */
242 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
243         CTLFLAG_RW, &rtq_toomany , 0, "");
244                                 
245
246 /*
247  * On last reference drop, mark the route as belong to us so that it can be
248  * timed out.
249  */
250 static void
251 in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
252 {
253         struct rtentry *rt = (struct rtentry *)rn;
254
255         if (!(rt->rt_flags & RTF_UP))
256                 return;         /* prophylactic measures */
257
258         if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
259                 return;
260
261         if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
262            != RTF_WASCLONED)
263                 return;
264
265         /*
266          * As requested by David Greenman:
267          * If rtq_reallyold is 0, just delete the route without
268          * waiting for a timeout cycle to kill it.
269          */
270         if (rtq_reallyold != 0) {
271                 rt->rt_flags |= RTPRF_OURS;
272                 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
273         } else {
274                 rtrequest(RTM_DELETE,
275                           (struct sockaddr *)rt_key(rt),
276                           rt->rt_gateway, rt_mask(rt),
277                           rt->rt_flags, 0);
278         }
279 }
280
281 struct rtqk_arg {
282         struct radix_node_head *rnh;
283         int mode;
284         int updating;
285         int draining;
286         int killed;
287         int found;
288         time_t nextstop;
289 };
290
291 /*
292  * Get rid of old routes.  When draining, this deletes everything, even when
293  * the timeout is not expired yet.  When updating, this makes sure that
294  * nothing has a timeout longer than the current value of rtq_reallyold.
295  */
296 static int
297 in6_rtqkill(struct radix_node *rn, void *rock)
298 {
299         struct rtqk_arg *ap = rock;
300         struct rtentry *rt = (struct rtentry *)rn;
301         int err;
302
303         if (rt->rt_flags & RTPRF_OURS) {
304                 ap->found++;
305
306                 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
307                         if (rt->rt_refcnt > 0)
308                                 panic("rtqkill route really not free");
309
310                         err = rtrequest(RTM_DELETE,
311                                         (struct sockaddr *)rt_key(rt),
312                                         rt->rt_gateway, rt_mask(rt),
313                                         rt->rt_flags, 0);
314                         if (err) {
315                                 log(LOG_WARNING, "in6_rtqkill: error %d", err);
316                         } else {
317                                 ap->killed++;
318                         }
319                 } else {
320                         if (ap->updating
321                            && (rt->rt_rmx.rmx_expire - time_second
322                                > rtq_reallyold)) {
323                                 rt->rt_rmx.rmx_expire = time_second
324                                         + 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         int s;
345
346         arg.found = arg.killed = 0;
347         arg.rnh = rnh;
348         arg.nextstop = time_second + rtq_timeout;
349         arg.draining = arg.updating = 0;
350         s = splnet();
351         rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
352         splx(s);
353
354         /*
355          * Attempt to be somewhat dynamic about this:
356          * If there are ``too many'' routes sitting around taking up space,
357          * then crank down the timeout, and see if we can't make some more
358          * go away.  However, we make sure that we will never adjust more
359          * than once in rtq_timeout seconds, to keep from cranking down too
360          * hard.
361          */
362         if ((arg.found - arg.killed > rtq_toomany)
363            && (time_second - last_adjusted_timeout >= rtq_timeout)
364            && rtq_reallyold > rtq_minreallyold) {
365                 rtq_reallyold = 2*rtq_reallyold / 3;
366                 if (rtq_reallyold < rtq_minreallyold) {
367                         rtq_reallyold = rtq_minreallyold;
368                 }
369
370                 last_adjusted_timeout = time_second;
371 #ifdef DIAGNOSTIC
372                 log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
373                     rtq_reallyold);
374 #endif
375                 arg.found = arg.killed = 0;
376                 arg.updating = 1;
377                 s = splnet();
378                 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
379                 splx(s);
380         }
381
382         atv.tv_usec = 0;
383         atv.tv_sec = arg.nextstop;
384         timeout(in6_rtqtimo, rock, tvtohz(&atv));
385 }
386
387 /*
388  * Age old PMTUs.
389  */
390 struct mtuex_arg {
391         struct radix_node_head *rnh;
392         time_t nextstop;
393 };
394
395 static int
396 in6_mtuexpire(struct radix_node *rn, void *rock)
397 {
398         struct rtentry *rt = (struct rtentry *)rn;
399         struct mtuex_arg *ap = rock;
400
401         /* sanity */
402         if (!rt)
403                 panic("rt == NULL in in6_mtuexpire");
404
405         if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
406                 if (rt->rt_rmx.rmx_expire <= time_second) {
407                         rt->rt_flags |= RTF_PROBEMTU;
408                 } else {
409                         ap->nextstop = lmin(ap->nextstop,
410                                         rt->rt_rmx.rmx_expire);
411                 }
412         }
413
414         return 0;
415 }
416
417 #define MTUTIMO_DEFAULT (60*1)
418
419 static void
420 in6_mtutimo(void *rock)
421 {
422         struct radix_node_head *rnh = rock;
423         struct mtuex_arg arg;
424         struct timeval atv;
425         int s;
426
427         arg.rnh = rnh;
428         arg.nextstop = time_second + MTUTIMO_DEFAULT;
429         s = splnet();
430         rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
431         splx(s);
432
433         atv.tv_usec = 0;
434         atv.tv_sec = arg.nextstop;
435         if (atv.tv_sec < time_second) {
436                 printf("invalid mtu expiration time on routing table\n");
437                 arg.nextstop = time_second + 30;        /* last resort */
438         }
439         timeout(in6_mtutimo, rock, tvtohz(&atv));
440 }
441
442 #if 0
443 void
444 in6_rtqdrain()
445 {
446         struct radix_node_head *rnh = rt_tables[AF_INET6];
447         struct rtqk_arg arg;
448         int s;
449         arg.found = arg.killed = 0;
450         arg.rnh = rnh;
451         arg.nextstop = 0;
452         arg.draining = 1;
453         arg.updating = 0;
454         s = splnet();
455         rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
456         splx(s);
457 }
458 #endif
459
460 /*
461  * Initialize our routing tree.
462  */
463 int
464 in6_inithead(void **head, int off)
465 {
466         struct radix_node_head *rnh;
467
468         if (!rn_inithead(head, off))
469                 return 0;
470
471         if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
472                 return 1;       /* only do this for the real routing table */
473
474         rnh = *head;
475         rnh->rnh_addaddr = in6_addroute;
476         rnh->rnh_matchaddr = in6_matroute;
477         rnh->rnh_close = in6_clsroute;
478         in6_rtqtimo(rnh);       /* kick off timeout first time */
479         in6_mtutimo(rnh);       /* kick off timeout first time */
480         return 1;
481 }