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