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