]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6_rmx.c
xnu-1228.3.13.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_rmx.c
CommitLineData
2d21ac55
A
1/*
2 * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
9bccf70c
A
29/* $FreeBSD: src/sys/netinet6/in6_rmx.c,v 1.1.2.2 2001/07/03 11:01:52 ume Exp $ */
30/* $KAME: in6_rmx.c,v 1.10 2001/05/24 05:44:58 itojun Exp $ */
1c79356b
A
31
32/*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61/*
62 * Copyright 1994, 1995 Massachusetts Institute of Technology
63 *
64 * Permission to use, copy, modify, and distribute this software and
65 * its documentation for any purpose and without fee is hereby
66 * granted, provided that both the above copyright notice and this
67 * permission notice appear in all copies, that both the above
68 * copyright notice and this permission notice appear in all
69 * supporting documentation, and that the name of M.I.T. not be used
70 * in advertising or publicity pertaining to distribution of the
71 * software without specific, written prior permission. M.I.T. makes
72 * no representations about the suitability of this software for any
73 * purpose. It is provided "as is" without express or implied
74 * warranty.
75 *
76 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
77 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
78 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
79 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
80 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
81 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
82 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
83 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
84 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
85 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
86 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 */
90
91/*
92 * This code does two things necessary for the enhanced TCP metrics to
93 * function in a useful manner:
94 * 1) It marks all non-host routes as `cloning', thus ensuring that
95 * every actual reference to such a route actually gets turned
96 * into a reference to a host route to the specific destination
97 * requested.
98 * 2) When such routes lose all their references, it arranges for them
99 * to be deleted in some random collection of circumstances, so that
100 * a large quantity of stale routing data is not kept in kernel memory
101 * indefinitely. See in6_rtqtimo() below for the exact mechanism.
102 */
103
104#include <sys/param.h>
105#include <sys/systm.h>
106#include <sys/kernel.h>
107#include <sys/sysctl.h>
108#include <kern/queue.h>
109#include <sys/socket.h>
110#include <sys/socketvar.h>
111#include <sys/mbuf.h>
112#include <sys/syslog.h>
91447636 113#include <kern/lock.h>
1c79356b
A
114
115#include <net/if.h>
116#include <net/route.h>
117#include <netinet/in.h>
1c79356b 118#include <netinet/ip_var.h>
1c79356b
A
119#include <netinet/in_var.h>
120
121#include <netinet/ip6.h>
122#include <netinet6/ip6_var.h>
123
124#include <netinet/icmp6.h>
125
1c79356b
A
126#include <netinet/tcp.h>
127#include <netinet/tcp_seq.h>
128#include <netinet/tcp_timer.h>
129#include <netinet/tcp_var.h>
1c79356b 130
91447636
A
131extern int in6_inithead(void **head, int off);
132static void in6_rtqtimo(void *rock);
133static void in6_mtutimo(void *rock);
2d21ac55 134extern int tvtohz(struct timeval *);
1c79356b
A
135
136#define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
137
138/*
139 * Do what we need to do when inserting a route.
140 */
141static struct radix_node *
142in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
143 struct radix_node *treenodes)
144{
145 struct rtentry *rt = (struct rtentry *)treenodes;
146 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
147 struct radix_node *ret;
148
149 /*
150 * For IPv6, all unicast non-host routes are automatically cloning.
151 */
152 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
153 rt->rt_flags |= RTF_MULTICAST;
154
155 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
156 rt->rt_flags |= RTF_PRCLONING;
157 }
158
159 /*
160 * A little bit of help for both IPv6 output and input:
161 * For local addresses, we make sure that RTF_LOCAL is set,
162 * with the thought that this might one day be used to speed up
163 * ip_input().
164 *
165 * We also mark routes to multicast addresses as such, because
166 * it's easy to do and might be useful (but this is much more
167 * dubious since it's so easy to inspect the address). (This
168 * is done above.)
169 *
170 * XXX
171 * should elaborate the code.
172 */
173 if (rt->rt_flags & RTF_HOST) {
174 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
175 ->sin6_addr,
176 &sin6->sin6_addr)) {
177 rt->rt_flags |= RTF_LOCAL;
178 }
179 }
180
1c79356b
A
181 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
182 && rt->rt_ifp)
183 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
184
185 ret = rn_addroute(v_arg, n_arg, head, treenodes);
186 if (ret == NULL && rt->rt_flags & RTF_HOST) {
187 struct rtentry *rt2;
188 /*
189 * We are trying to add a host route, but can't.
190 * Find out if it is because of an
191 * ARP entry and delete it if so.
192 */
91447636 193 rt2 = rtalloc1_locked((struct sockaddr *)sin6, 0,
1c79356b
A
194 RTF_CLONING | RTF_PRCLONING);
195 if (rt2) {
196 if (rt2->rt_flags & RTF_LLINFO &&
197 rt2->rt_flags & RTF_HOST &&
198 rt2->rt_gateway &&
199 rt2->rt_gateway->sa_family == AF_LINK) {
91447636 200 rtrequest_locked(RTM_DELETE,
1c79356b
A
201 (struct sockaddr *)rt_key(rt2),
202 rt2->rt_gateway,
203 rt_mask(rt2), rt2->rt_flags, 0);
204 ret = rn_addroute(v_arg, n_arg, head,
205 treenodes);
206 }
91447636 207 rtfree_locked(rt2);
1c79356b
A
208 }
209 } else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
210 struct rtentry *rt2;
211 /*
212 * We are trying to add a net route, but can't.
213 * The following case should be allowed, so we'll make a
214 * special check for this:
215 * Two IPv6 addresses with the same prefix is assigned
216 * to a single interrface.
217 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
218 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
219 * In this case, (*1) and (*2) want to add the same
220 * net route entry, 3ffe:0501:: -> if0.
221 * This case should not raise an error.
222 */
91447636 223 rt2 = rtalloc1_locked((struct sockaddr *)sin6, 0,
1c79356b
A
224 RTF_CLONING | RTF_PRCLONING);
225 if (rt2) {
226 if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
227 == RTF_CLONING
228 && rt2->rt_gateway
229 && rt2->rt_gateway->sa_family == AF_LINK
230 && rt2->rt_ifp == rt->rt_ifp) {
231 ret = rt2->rt_nodes;
232 }
91447636 233 rtfree_locked(rt2);
1c79356b
A
234 }
235 }
236 return ret;
237}
238
239/*
240 * This code is the inverse of in6_clsroute: on first reference, if we
241 * were managing the route, stop doing so and set the expiration timer
242 * back off again.
243 */
244static struct radix_node *
245in6_matroute(void *v_arg, struct radix_node_head *head)
246{
247 struct radix_node *rn = rn_match(v_arg, head);
248 struct rtentry *rt = (struct rtentry *)rn;
249
250 if (rt && rt->rt_refcnt == 0) { /* this is first reference */
251 if (rt->rt_flags & RTPRF_OURS) {
252 rt->rt_flags &= ~RTPRF_OURS;
253 rt->rt_rmx.rmx_expire = 0;
254 }
255 }
256 return rn;
257}
258
9bccf70c
A
259SYSCTL_DECL(_net_inet6_ip6);
260
1c79356b
A
261static int rtq_reallyold = 60*60;
262 /* one hour is ``really old'' */
9bccf70c
A
263SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
264 CTLFLAG_RW, &rtq_reallyold , 0, "");
265
1c79356b
A
266static int rtq_minreallyold = 10;
267 /* never automatically crank down to less */
9bccf70c
A
268SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
269 CTLFLAG_RW, &rtq_minreallyold , 0, "");
270
1c79356b
A
271static int rtq_toomany = 128;
272 /* 128 cached routes is ``too many'' */
9bccf70c
A
273SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
274 CTLFLAG_RW, &rtq_toomany , 0, "");
275
1c79356b
A
276
277/*
278 * On last reference drop, mark the route as belong to us so that it can be
279 * timed out.
280 */
281static void
2d21ac55 282in6_clsroute(struct radix_node *rn, __unused struct radix_node_head *head)
1c79356b
A
283{
284 struct rtentry *rt = (struct rtentry *)rn;
285
286 if (!(rt->rt_flags & RTF_UP))
287 return; /* prophylactic measures */
288
289 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
290 return;
291
2d21ac55 292 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
1c79356b
A
293 return;
294
295 /*
2d21ac55
A
296 * Delete the route immediately if RTF_DELCLONE is set or
297 * if route caching is disabled (rtq_reallyold set to 0).
298 * Otherwise, let it expire and be deleted by in6_rtqkill().
1c79356b 299 */
2d21ac55
A
300 if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) {
301 /*
302 * Delete the route from the radix tree but since we are
303 * called when the route's reference count is 0, don't
304 * deallocate it until we return from this routine by
305 * telling rtrequest that we're interested in it.
306 */
307 if (rtrequest_locked(RTM_DELETE, (struct sockaddr *)rt_key(rt),
308 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt) == 0) {
309 /* Now let the caller free it */
310 rtunref(rt);
311 }
312 } else {
313 struct timeval timenow;
314
315 getmicrotime(&timenow);
1c79356b 316 rt->rt_flags |= RTPRF_OURS;
91447636 317 rt->rt_rmx.rmx_expire = timenow.tv_sec + rtq_reallyold;
1c79356b
A
318 }
319}
320
321struct rtqk_arg {
322 struct radix_node_head *rnh;
323 int mode;
324 int updating;
325 int draining;
326 int killed;
327 int found;
328 time_t nextstop;
329};
330
331/*
332 * Get rid of old routes. When draining, this deletes everything, even when
333 * the timeout is not expired yet. When updating, this makes sure that
334 * nothing has a timeout longer than the current value of rtq_reallyold.
335 */
336static int
337in6_rtqkill(struct radix_node *rn, void *rock)
338{
339 struct rtqk_arg *ap = rock;
340 struct rtentry *rt = (struct rtentry *)rn;
341 int err;
91447636
A
342 struct timeval timenow;
343
344 getmicrotime(&timenow);
91447636 345 lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED);
2d21ac55 346
1c79356b
A
347 if (rt->rt_flags & RTPRF_OURS) {
348 ap->found++;
349
91447636 350 if (ap->draining || rt->rt_rmx.rmx_expire <= timenow.tv_sec) {
1c79356b
A
351 if (rt->rt_refcnt > 0)
352 panic("rtqkill route really not free");
353
91447636 354 err = rtrequest_locked(RTM_DELETE,
1c79356b
A
355 (struct sockaddr *)rt_key(rt),
356 rt->rt_gateway, rt_mask(rt),
357 rt->rt_flags, 0);
358 if (err) {
359 log(LOG_WARNING, "in6_rtqkill: error %d", err);
360 } else {
361 ap->killed++;
362 }
363 } else {
364 if (ap->updating
91447636 365 && (rt->rt_rmx.rmx_expire - timenow.tv_sec
1c79356b 366 > rtq_reallyold)) {
91447636 367 rt->rt_rmx.rmx_expire = timenow.tv_sec
1c79356b
A
368 + rtq_reallyold;
369 }
370 ap->nextstop = lmin(ap->nextstop,
371 rt->rt_rmx.rmx_expire);
372 }
373 }
374
375 return 0;
376}
377
378#define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
379static int rtq_timeout = RTQ_TIMEOUT;
380
381static void
382in6_rtqtimo(void *rock)
383{
384 struct radix_node_head *rnh = rock;
385 struct rtqk_arg arg;
386 struct timeval atv;
387 static time_t last_adjusted_timeout = 0;
91447636
A
388 struct timeval timenow;
389
2d21ac55
A
390 lck_mtx_lock(rt_mtx);
391 /* Get the timestamp after we acquire the lock for better accuracy */
91447636 392 getmicrotime(&timenow);
1c79356b
A
393
394 arg.found = arg.killed = 0;
395 arg.rnh = rnh;
91447636 396 arg.nextstop = timenow.tv_sec + rtq_timeout;
1c79356b 397 arg.draining = arg.updating = 0;
1c79356b 398 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
1c79356b
A
399
400 /*
401 * Attempt to be somewhat dynamic about this:
402 * If there are ``too many'' routes sitting around taking up space,
403 * then crank down the timeout, and see if we can't make some more
404 * go away. However, we make sure that we will never adjust more
405 * than once in rtq_timeout seconds, to keep from cranking down too
406 * hard.
407 */
408 if ((arg.found - arg.killed > rtq_toomany)
91447636 409 && (timenow.tv_sec - last_adjusted_timeout >= rtq_timeout)
1c79356b
A
410 && rtq_reallyold > rtq_minreallyold) {
411 rtq_reallyold = 2*rtq_reallyold / 3;
412 if (rtq_reallyold < rtq_minreallyold) {
413 rtq_reallyold = rtq_minreallyold;
414 }
415
91447636 416 last_adjusted_timeout = timenow.tv_sec;
1c79356b
A
417#if DIAGNOSTIC
418 log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
419 rtq_reallyold);
420#endif
421 arg.found = arg.killed = 0;
422 arg.updating = 1;
1c79356b 423 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
1c79356b
A
424 }
425
426 atv.tv_usec = 0;
91447636
A
427 atv.tv_sec = arg.nextstop - timenow.tv_sec;
428 lck_mtx_unlock(rt_mtx);
429 timeout(in6_rtqtimo, rock, tvtohz(&atv));
1c79356b
A
430}
431
432/*
433 * Age old PMTUs.
434 */
435struct mtuex_arg {
436 struct radix_node_head *rnh;
437 time_t nextstop;
438};
439
440static int
441in6_mtuexpire(struct radix_node *rn, void *rock)
442{
443 struct rtentry *rt = (struct rtentry *)rn;
444 struct mtuex_arg *ap = rock;
91447636
A
445 struct timeval timenow;
446
447 getmicrotime(&timenow);
1c79356b
A
448
449 /* sanity */
450 if (!rt)
451 panic("rt == NULL in in6_mtuexpire");
452
453 if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
91447636 454 if (rt->rt_rmx.rmx_expire <= timenow.tv_sec) {
1c79356b
A
455 rt->rt_flags |= RTF_PROBEMTU;
456 } else {
457 ap->nextstop = lmin(ap->nextstop,
458 rt->rt_rmx.rmx_expire);
459 }
460 }
461
462 return 0;
463}
464
465#define MTUTIMO_DEFAULT (60*1)
466
467static void
468in6_mtutimo(void *rock)
469{
470 struct radix_node_head *rnh = rock;
471 struct mtuex_arg arg;
472 struct timeval atv;
91447636
A
473 struct timeval timenow;
474
475 getmicrotime(&timenow);
1c79356b
A
476
477 arg.rnh = rnh;
91447636
A
478 arg.nextstop = timenow.tv_sec + MTUTIMO_DEFAULT;
479 lck_mtx_lock(rt_mtx);
1c79356b 480 rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
1c79356b
A
481
482 atv.tv_usec = 0;
483 atv.tv_sec = arg.nextstop;
91447636 484 if (atv.tv_sec < timenow.tv_sec) {
9bccf70c
A
485#if DIAGNOSTIC
486 log(LOG_DEBUG, "IPv6: invalid mtu expiration time on routing table\n");
487#endif
91447636 488 arg.nextstop = timenow.tv_sec + 30; /*last resort*/
1c79356b 489 }
91447636
A
490 atv.tv_sec -= timenow.tv_sec;
491 lck_mtx_unlock(rt_mtx);
492 timeout(in6_mtutimo, rock, tvtohz(&atv));
1c79356b
A
493}
494
495#if 0
496void
497in6_rtqdrain()
498{
499 struct radix_node_head *rnh = rt_tables[AF_INET6];
500 struct rtqk_arg arg;
501 int s;
502 arg.found = arg.killed = 0;
503 arg.rnh = rnh;
504 arg.nextstop = 0;
505 arg.draining = 1;
506 arg.updating = 0;
507 s = splnet();
508 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
509 splx(s);
510}
511#endif
512
513/*
514 * Initialize our routing tree.
515 */
516int
517in6_inithead(void **head, int off)
518{
519 struct radix_node_head *rnh;
520
521 if (!rn_inithead(head, off))
522 return 0;
523
524 if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
525 return 1; /* only do this for the real routing table */
526
527 rnh = *head;
528 rnh->rnh_addaddr = in6_addroute;
529 rnh->rnh_matchaddr = in6_matroute;
530 rnh->rnh_close = in6_clsroute;
531 in6_rtqtimo(rnh); /* kick off timeout first time */
532 in6_mtutimo(rnh); /* kick off timeout first time */
533 return 1;
534}