]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6_rmx.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_rmx.c
CommitLineData
2d21ac55 1/*
39037602 2 * Copyright (c) 2003-2016 Apple Inc. All rights reserved.
2d21ac55
A
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
1c79356b
A
29/*
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58/*
59 * Copyright 1994, 1995 Massachusetts Institute of Technology
60 *
61 * Permission to use, copy, modify, and distribute this software and
62 * its documentation for any purpose and without fee is hereby
63 * granted, provided that both the above copyright notice and this
64 * permission notice appear in all copies, that both the above
65 * copyright notice and this permission notice appear in all
66 * supporting documentation, and that the name of M.I.T. not be used
67 * in advertising or publicity pertaining to distribution of the
68 * software without specific, written prior permission. M.I.T. makes
69 * no representations about the suitability of this software for any
70 * purpose. It is provided "as is" without express or implied
71 * warranty.
72 *
73 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
74 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
75 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
76 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
77 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
79 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
80 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
81 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
82 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
83 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 * SUCH DAMAGE.
85 *
86 */
87
88/*
89 * This code does two things necessary for the enhanced TCP metrics to
90 * function in a useful manner:
91 * 1) It marks all non-host routes as `cloning', thus ensuring that
92 * every actual reference to such a route actually gets turned
93 * into a reference to a host route to the specific destination
94 * requested.
95 * 2) When such routes lose all their references, it arranges for them
96 * to be deleted in some random collection of circumstances, so that
97 * a large quantity of stale routing data is not kept in kernel memory
98 * indefinitely. See in6_rtqtimo() below for the exact mechanism.
99 */
100
101#include <sys/param.h>
102#include <sys/systm.h>
103#include <sys/kernel.h>
104#include <sys/sysctl.h>
105#include <kern/queue.h>
106#include <sys/socket.h>
107#include <sys/socketvar.h>
6d2010ae 108#include <sys/protosw.h>
1c79356b
A
109#include <sys/mbuf.h>
110#include <sys/syslog.h>
6d2010ae 111#include <sys/mcache.h>
fe8ab488 112#include <kern/locks.h>
1c79356b
A
113
114#include <net/if.h>
115#include <net/route.h>
116#include <netinet/in.h>
1c79356b 117#include <netinet/ip_var.h>
1c79356b
A
118#include <netinet/in_var.h>
119
120#include <netinet/ip6.h>
121#include <netinet6/ip6_var.h>
122
123#include <netinet/icmp6.h>
124
1c79356b
A
125#include <netinet/tcp.h>
126#include <netinet/tcp_seq.h>
127#include <netinet/tcp_timer.h>
128#include <netinet/tcp_var.h>
1c79356b 129
0a7de745 130extern int tvtohz(struct timeval *);
1c79356b 131
0a7de745 132static int in6_rtqtimo_run; /* in6_rtqtimo is scheduled to run */
39236c6e
A
133static void in6_rtqtimo(void *);
134static void in6_sched_rtqtimo(struct timeval *);
135
136static struct radix_node *in6_addroute(void *, void *, struct radix_node_head *,
137 struct radix_node *);
138static struct radix_node *in6_deleteroute(void *, void *,
139 struct radix_node_head *);
140static struct radix_node *in6_matroute(void *, struct radix_node_head *);
c910b4d9
A
141static struct radix_node *in6_matroute_args(void *, struct radix_node_head *,
142 rn_matchf_t *, void *);
39236c6e
A
143static void in6_clsroute(struct radix_node *, struct radix_node_head *);
144static int in6_rtqkill(struct radix_node *, void *);
c910b4d9 145
b0d623f7
A
146/*
147 * Accessed by in6_addroute(), in6_deleteroute() and in6_rtqkill(), during
148 * which the routing lock (rnh_lock) is held and thus protects the variable.
149 */
39236c6e 150static int in6dynroutes;
b0d623f7 151
1c79356b
A
152/*
153 * Do what we need to do when inserting a route.
154 */
155static struct radix_node *
156in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
39236c6e 157 struct radix_node *treenodes)
1c79356b
A
158{
159 struct rtentry *rt = (struct rtentry *)treenodes;
316670eb 160 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)rt_key(rt);
1c79356b 161 struct radix_node *ret;
39236c6e
A
162 char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
163 uint32_t flags = rt->rt_flags;
164 boolean_t verbose = (rt_verbose > 1);
1c79356b 165
5ba3f43e 166 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
167 RT_LOCK_ASSERT_HELD(rt);
168
0a7de745
A
169 if (verbose) {
170 rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
171 }
39236c6e 172
b0d623f7
A
173 /*
174 * If this is a dynamic route (which is created via Redirect) and
175 * we already have the maximum acceptable number of such route entries,
176 * reject creating a new one. We could initiate garbage collection to
177 * make available space right now, but the benefit would probably not
178 * be worth the cleaning overhead; we only have to endure a slightly
39236c6e 179 * suboptimal path even without the redirected route.
b0d623f7 180 */
39236c6e 181 if ((rt->rt_flags & RTF_DYNAMIC) &&
0a7de745
A
182 ip6_maxdynroutes >= 0 && in6dynroutes >= ip6_maxdynroutes) {
183 return NULL;
184 }
b0d623f7 185
1c79356b
A
186 /*
187 * For IPv6, all unicast non-host routes are automatically cloning.
188 */
0a7de745 189 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1c79356b 190 rt->rt_flags |= RTF_MULTICAST;
0a7de745 191 }
1c79356b 192
0a7de745 193 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
1c79356b 194 rt->rt_flags |= RTF_PRCLONING;
0a7de745 195 }
1c79356b
A
196
197 /*
198 * A little bit of help for both IPv6 output and input:
199 * For local addresses, we make sure that RTF_LOCAL is set,
200 * with the thought that this might one day be used to speed up
201 * ip_input().
202 *
203 * We also mark routes to multicast addresses as such, because
204 * it's easy to do and might be useful (but this is much more
205 * dubious since it's so easy to inspect the address). (This
206 * is done above.)
207 *
208 * XXX
209 * should elaborate the code.
210 */
211 if (rt->rt_flags & RTF_HOST) {
6d2010ae 212 IFA_LOCK_SPIN(rt->rt_ifa);
39236c6e
A
213 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)->
214 sin6_addr, &sin6->sin6_addr)) {
1c79356b
A
215 rt->rt_flags |= RTF_LOCAL;
216 }
6d2010ae 217 IFA_UNLOCK(rt->rt_ifa);
1c79356b
A
218 }
219
39236c6e 220 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
0a7de745 221 rt->rt_ifp) {
1c79356b 222 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
0a7de745 223 }
1c79356b
A
224
225 ret = rn_addroute(v_arg, n_arg, head, treenodes);
b0d623f7 226 if (ret == NULL && (rt->rt_flags & RTF_HOST)) {
1c79356b
A
227 struct rtentry *rt2;
228 /*
229 * We are trying to add a host route, but can't.
230 * Find out if it is because of an
39236c6e 231 * ND6 entry and delete it if so.
1c79356b 232 */
6d2010ae
A
233 rt2 = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0,
234 RTF_CLONING | RTF_PRCLONING, sin6_get_ifscope(rt_key(rt)));
39236c6e
A
235 if (rt2 != NULL) {
236 char dbufc[MAX_IPv6_STR_LEN];
237
b0d623f7 238 RT_LOCK(rt2);
0a7de745
A
239 if (verbose) {
240 rt_str(rt2, dbufc, sizeof(dbufc), NULL, 0);
241 }
39236c6e 242
b0d623f7
A
243 if ((rt2->rt_flags & RTF_LLINFO) &&
244 (rt2->rt_flags & RTF_HOST) &&
245 rt2->rt_gateway != NULL &&
246 rt2->rt_gateway->sa_family == AF_LINK) {
39236c6e
A
247 if (verbose) {
248 log(LOG_DEBUG, "%s: unable to insert "
249 "route to %s:%s, flags=%b, due to "
250 "existing ND6 route %s->%s "
251 "flags=%b, attempting to delete\n",
252 __func__, dbuf,
253 (rt->rt_ifp != NULL) ?
254 rt->rt_ifp->if_xname : "",
255 rt->rt_flags, RTF_BITS,
256 dbufc, (rt2->rt_ifp != NULL) ?
257 rt2->rt_ifp->if_xname : "",
258 rt2->rt_flags, RTF_BITS);
259 }
b0d623f7
A
260 /*
261 * Safe to drop rt_lock and use rt_key,
262 * rt_gateway, since holding rnh_lock here
263 * prevents another thread from calling
264 * rt_setgate() on this route.
265 */
266 RT_UNLOCK(rt2);
267 (void) rtrequest_locked(RTM_DELETE, rt_key(rt2),
268 rt2->rt_gateway, rt_mask(rt2),
39236c6e 269 rt2->rt_flags, NULL);
1c79356b 270 ret = rn_addroute(v_arg, n_arg, head,
39236c6e 271 treenodes);
b0d623f7
A
272 } else {
273 RT_UNLOCK(rt2);
1c79356b 274 }
91447636 275 rtfree_locked(rt2);
1c79356b 276 }
b0d623f7 277 } else if (ret == NULL && (rt->rt_flags & RTF_CLONING)) {
1c79356b
A
278 struct rtentry *rt2;
279 /*
280 * We are trying to add a net route, but can't.
281 * The following case should be allowed, so we'll make a
282 * special check for this:
283 * Two IPv6 addresses with the same prefix is assigned
284 * to a single interrface.
285 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
286 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
287 * In this case, (*1) and (*2) want to add the same
288 * net route entry, 3ffe:0501:: -> if0.
289 * This case should not raise an error.
290 */
6d2010ae
A
291 rt2 = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0,
292 RTF_CLONING | RTF_PRCLONING, sin6_get_ifscope(rt_key(rt)));
39236c6e 293 if (rt2 != NULL) {
b0d623f7 294 RT_LOCK(rt2);
0a7de745 295 if ((rt2->rt_flags & (RTF_CLONING | RTF_HOST |
39236c6e
A
296 RTF_GATEWAY)) == RTF_CLONING &&
297 rt2->rt_gateway &&
298 rt2->rt_gateway->sa_family == AF_LINK &&
299 rt2->rt_ifp == rt->rt_ifp) {
1c79356b
A
300 ret = rt2->rt_nodes;
301 }
b0d623f7 302 RT_UNLOCK(rt2);
91447636 303 rtfree_locked(rt2);
1c79356b
A
304 }
305 }
b0d623f7 306
0a7de745 307 if (ret != NULL && (rt->rt_flags & RTF_DYNAMIC)) {
b0d623f7 308 in6dynroutes++;
0a7de745 309 }
b0d623f7 310
0a7de745 311 if (!verbose) {
39236c6e 312 goto done;
0a7de745 313 }
39236c6e
A
314
315 if (ret != NULL) {
316 if (flags != rt->rt_flags) {
317 log(LOG_DEBUG, "%s: route to %s->%s->%s inserted, "
318 "oflags=%b, flags=%b\n", __func__,
319 dbuf, gbuf, (rt->rt_ifp != NULL) ?
320 rt->rt_ifp->if_xname : "", flags, RTF_BITS,
321 rt->rt_flags, RTF_BITS);
322 } else {
323 log(LOG_DEBUG, "%s: route to %s->%s->%s inserted, "
324 "flags=%b\n", __func__, dbuf, gbuf,
325 (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
326 rt->rt_flags, RTF_BITS);
327 }
328 } else {
329 log(LOG_DEBUG, "%s: unable to insert route to %s->%s->%s, "
330 "flags=%b, already exists\n", __func__, dbuf, gbuf,
331 (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
332 rt->rt_flags, RTF_BITS);
333 }
334done:
0a7de745 335 return ret;
1c79356b
A
336}
337
b0d623f7 338static struct radix_node *
39236c6e 339in6_deleteroute(void *v_arg, void *netmask_arg, struct radix_node_head *head)
b0d623f7
A
340{
341 struct radix_node *rn;
342
5ba3f43e 343 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
344
345 rn = rn_delete(v_arg, netmask_arg, head);
346 if (rn != NULL) {
347 struct rtentry *rt = (struct rtentry *)rn;
39236c6e
A
348
349 RT_LOCK(rt);
0a7de745 350 if (rt->rt_flags & RTF_DYNAMIC) {
b0d623f7 351 in6dynroutes--;
0a7de745 352 }
39236c6e
A
353 if (rt_verbose > 1) {
354 char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
355
0a7de745 356 rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
39236c6e
A
357 log(LOG_DEBUG, "%s: route to %s->%s->%s deleted, "
358 "flags=%b\n", __func__, dbuf, gbuf,
359 (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
360 rt->rt_flags, RTF_BITS);
361 }
b0d623f7
A
362 RT_UNLOCK(rt);
363 }
0a7de745 364 return rn;
b0d623f7
A
365}
366
6d2010ae
A
367/*
368 * Validate (unexpire) an expiring AF_INET6 route.
369 */
370struct radix_node *
371in6_validate(struct radix_node *rn)
372{
373 struct rtentry *rt = (struct rtentry *)rn;
374
375 RT_LOCK_ASSERT_HELD(rt);
376
377 /* This is first reference? */
39236c6e
A
378 if (rt->rt_refcnt == 0) {
379 if (rt_verbose > 2) {
380 char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
381
0a7de745 382 rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
39236c6e
A
383 log(LOG_DEBUG, "%s: route to %s->%s->%s validated, "
384 "flags=%b\n", __func__, dbuf, gbuf,
385 (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
386 rt->rt_flags, RTF_BITS);
387 }
388
389 /*
390 * It's one of ours; unexpire it. If the timer is already
391 * scheduled, let it run later as it won't re-arm itself
392 * if there's nothing to do.
393 */
394 if (rt->rt_flags & RTPRF_OURS) {
395 rt->rt_flags &= ~RTPRF_OURS;
396 rt_setexpire(rt, 0);
397 }
6d2010ae 398 }
0a7de745 399 return rn;
6d2010ae
A
400}
401
c910b4d9
A
402/*
403 * Similar to in6_matroute_args except without the leaf-matching parameters.
404 */
405static struct radix_node *
406in6_matroute(void *v_arg, struct radix_node_head *head)
407{
0a7de745 408 return in6_matroute_args(v_arg, head, NULL, NULL);
c910b4d9
A
409}
410
1c79356b
A
411/*
412 * This code is the inverse of in6_clsroute: on first reference, if we
413 * were managing the route, stop doing so and set the expiration timer
414 * back off again.
415 */
416static struct radix_node *
c910b4d9
A
417in6_matroute_args(void *v_arg, struct radix_node_head *head,
418 rn_matchf_t *f, void *w)
1c79356b 419{
c910b4d9 420 struct radix_node *rn = rn_match_args(v_arg, head, f, w);
1c79356b 421
6d2010ae
A
422 if (rn != NULL) {
423 RT_LOCK_SPIN((struct rtentry *)rn);
424 in6_validate(rn);
425 RT_UNLOCK((struct rtentry *)rn);
1c79356b 426 }
0a7de745 427 return rn;
1c79356b
A
428}
429
9bccf70c
A
430SYSCTL_DECL(_net_inet6_ip6);
431
39236c6e 432/* one hour is ``really old'' */
0a7de745 433static uint32_t rtq_reallyold = 60 * 60;
39236c6e 434SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
0a7de745 435 CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_reallyold, 0, "");
b0d623f7 436
39236c6e
A
437/* never automatically crank down to less */
438static uint32_t rtq_minreallyold = 10;
439SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
0a7de745 440 CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_minreallyold, 0, "");
b0d623f7 441
39236c6e
A
442/* 128 cached routes is ``too many'' */
443static uint32_t rtq_toomany = 128;
444SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
0a7de745 445 CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_toomany, 0, "");
1c79356b
A
446
447/*
448 * On last reference drop, mark the route as belong to us so that it can be
449 * timed out.
450 */
451static void
39236c6e 452in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
1c79356b 453{
39236c6e
A
454#pragma unused(head)
455 char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
1c79356b 456 struct rtentry *rt = (struct rtentry *)rn;
39236c6e 457 boolean_t verbose = (rt_verbose > 1);
1c79356b 458
5ba3f43e 459 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
460 RT_LOCK_ASSERT_HELD(rt);
461
0a7de745 462 if (!(rt->rt_flags & RTF_UP)) {
5ba3f43e 463 return; /* prophylactic measures */
0a7de745
A
464 }
465 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) {
1c79356b 466 return;
0a7de745 467 }
1c79356b 468
0a7de745 469 if (rt->rt_flags & RTPRF_OURS) {
b0d623f7 470 return;
0a7de745 471 }
b0d623f7 472
0a7de745 473 if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC))) {
1c79356b 474 return;
0a7de745 475 }
1c79356b 476
0a7de745
A
477 if (verbose) {
478 rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
479 }
39236c6e 480
1c79356b 481 /*
2d21ac55
A
482 * Delete the route immediately if RTF_DELCLONE is set or
483 * if route caching is disabled (rtq_reallyold set to 0).
484 * Otherwise, let it expire and be deleted by in6_rtqkill().
1c79356b 485 */
2d21ac55 486 if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) {
39236c6e
A
487 int err;
488
489 if (verbose) {
490 log(LOG_DEBUG, "%s: deleting route to %s->%s->%s, "
491 "flags=%b\n", __func__, dbuf, gbuf,
492 (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
493 rt->rt_flags, RTF_BITS);
494 }
2d21ac55
A
495 /*
496 * Delete the route from the radix tree but since we are
497 * called when the route's reference count is 0, don't
498 * deallocate it until we return from this routine by
499 * telling rtrequest that we're interested in it.
b0d623f7
A
500 * Safe to drop rt_lock and use rt_key, rt_gateway,
501 * since holding rnh_lock here prevents another thread
502 * from calling rt_setgate() on this route.
2d21ac55 503 */
b0d623f7 504 RT_UNLOCK(rt);
39236c6e
A
505 err = rtrequest_locked(RTM_DELETE, rt_key(rt),
506 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt);
507 if (err == 0) {
2d21ac55 508 /* Now let the caller free it */
b0d623f7
A
509 RT_LOCK(rt);
510 RT_REMREF_LOCKED(rt);
511 } else {
512 RT_LOCK(rt);
0a7de745
A
513 if (!verbose) {
514 rt_str(rt, dbuf, sizeof(dbuf),
515 gbuf, sizeof(gbuf));
516 }
39236c6e
A
517 log(LOG_ERR, "%s: error deleting route to "
518 "%s->%s->%s, flags=%b, err=%d\n", __func__,
519 dbuf, gbuf, (rt->rt_ifp != NULL) ?
520 rt->rt_ifp->if_xname : "", rt->rt_flags,
521 RTF_BITS, err);
2d21ac55
A
522 }
523 } else {
6d2010ae 524 uint64_t timenow;
2d21ac55 525
6d2010ae 526 timenow = net_uptime();
1c79356b 527 rt->rt_flags |= RTPRF_OURS;
39236c6e
A
528 rt_setexpire(rt, timenow + rtq_reallyold);
529
530 if (verbose) {
531 log(LOG_DEBUG, "%s: route to %s->%s->%s invalidated, "
0a7de745
A
532 "flags=%b, expire=T+%u\n", __func__, dbuf, gbuf,
533 (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
534 rt->rt_flags, RTF_BITS, rt->rt_expire - timenow);
39236c6e
A
535 }
536
537 /* We have at least one entry; arm the timer if not already */
538 in6_sched_rtqtimo(NULL);
1c79356b
A
539 }
540}
541
542struct rtqk_arg {
543 struct radix_node_head *rnh;
1c79356b
A
544 int updating;
545 int draining;
39236c6e
A
546 uint32_t killed;
547 uint32_t found;
6d2010ae 548 uint64_t nextstop;
1c79356b
A
549};
550
551/*
552 * Get rid of old routes. When draining, this deletes everything, even when
b0d623f7
A
553 * the timeout is not expired yet. This also applies if the route is dynamic
554 * and there are sufficiently large number of such routes (more than a half of
555 * maximum). When updating, this makes sure that nothing has a timeout longer
556 * than the current value of rtq_reallyold.
1c79356b
A
557 */
558static int
559in6_rtqkill(struct radix_node *rn, void *rock)
560{
561 struct rtqk_arg *ap = rock;
562 struct rtentry *rt = (struct rtentry *)rn;
39236c6e 563 boolean_t verbose = (rt_verbose > 1);
6d2010ae 564 uint64_t timenow;
39236c6e 565 int err;
91447636 566
6d2010ae 567 timenow = net_uptime();
5ba3f43e 568 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
2d21ac55 569
b0d623f7 570 RT_LOCK(rt);
1c79356b 571 if (rt->rt_flags & RTPRF_OURS) {
39236c6e
A
572 char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
573
0a7de745
A
574 if (verbose) {
575 rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
576 }
39236c6e 577
1c79356b 578 ap->found++;
6d2010ae
A
579 VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0);
580 VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0);
581 if (ap->draining || rt->rt_expire <= timenow ||
39236c6e 582 ((rt->rt_flags & RTF_DYNAMIC) && ip6_maxdynroutes >= 0 &&
b0d623f7 583 in6dynroutes > ip6_maxdynroutes / 2)) {
39236c6e
A
584 if (rt->rt_refcnt > 0) {
585 panic("%s: route %p marked with RTPRF_OURS "
586 "with non-zero refcnt (%u)", __func__,
587 rt, rt->rt_refcnt);
588 /* NOTREACHED */
589 }
5ba3f43e 590
39236c6e
A
591 if (verbose) {
592 log(LOG_DEBUG, "%s: deleting route to "
593 "%s->%s->%s, flags=%b, draining=%d\n",
594 __func__, dbuf, gbuf, (rt->rt_ifp != NULL) ?
595 rt->rt_ifp->if_xname : "", rt->rt_flags,
596 RTF_BITS, ap->draining);
597 }
0a7de745 598 RT_ADDREF_LOCKED(rt); /* for us to free below */
b0d623f7
A
599 /*
600 * Delete this route since we're done with it;
601 * the route may be freed afterwards, so we
602 * can no longer refer to 'rt' upon returning
603 * from rtrequest(). Safe to drop rt_lock and
604 * use rt_key, rt_gateway, since holding rnh_lock
605 * here prevents another thread from calling
606 * rt_setgate() on this route.
607 */
608 RT_UNLOCK(rt);
609 err = rtrequest_locked(RTM_DELETE, rt_key(rt),
39236c6e
A
610 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
611 if (err != 0) {
612 RT_LOCK(rt);
0a7de745
A
613 if (!verbose) {
614 rt_str(rt, dbuf, sizeof(dbuf),
615 gbuf, sizeof(gbuf));
616 }
39236c6e
A
617 log(LOG_ERR, "%s: error deleting route to "
618 "%s->%s->%s, flags=%b, err=%d\n", __func__,
619 dbuf, gbuf, (rt->rt_ifp != NULL) ?
620 rt->rt_ifp->if_xname : "", rt->rt_flags,
621 RTF_BITS, err);
622 RT_UNLOCK(rt);
1c79356b
A
623 } else {
624 ap->killed++;
625 }
39236c6e 626 rtfree_locked(rt);
1c79356b 627 } else {
39236c6e
A
628 uint64_t expire = (rt->rt_expire - timenow);
629
630 if (ap->updating && expire > rtq_reallyold) {
631 rt_setexpire(rt, timenow + rtq_reallyold);
632 if (verbose) {
633 log(LOG_DEBUG, "%s: route to "
634 "%s->%s->%s, flags=%b, adjusted "
635 "expire=T+%u (was T+%u)\n",
636 __func__, dbuf, gbuf,
637 (rt->rt_ifp != NULL) ?
638 rt->rt_ifp->if_xname : "",
639 rt->rt_flags, RTF_BITS,
640 (rt->rt_expire - timenow), expire);
641 }
1c79356b 642 }
39236c6e 643 ap->nextstop = lmin(ap->nextstop, rt->rt_expire);
b0d623f7 644 RT_UNLOCK(rt);
1c79356b 645 }
b0d623f7
A
646 } else {
647 RT_UNLOCK(rt);
1c79356b
A
648 }
649
0a7de745 650 return 0;
1c79356b
A
651}
652
0a7de745 653#define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
1c79356b
A
654static int rtq_timeout = RTQ_TIMEOUT;
655
656static void
39236c6e 657in6_rtqtimo(void *targ)
1c79356b 658{
39236c6e
A
659#pragma unused(targ)
660 struct radix_node_head *rnh;
1c79356b
A
661 struct rtqk_arg arg;
662 struct timeval atv;
6d2010ae 663 static uint64_t last_adjusted_timeout = 0;
39236c6e 664 boolean_t verbose = (rt_verbose > 1);
6d2010ae 665 uint64_t timenow;
39236c6e 666 uint32_t ours;
91447636 667
b0d623f7 668 lck_mtx_lock(rnh_lock);
39236c6e
A
669 rnh = rt_tables[AF_INET6];
670 VERIFY(rnh != NULL);
671
2d21ac55 672 /* Get the timestamp after we acquire the lock for better accuracy */
6d2010ae 673 timenow = net_uptime();
39236c6e
A
674 if (verbose) {
675 log(LOG_DEBUG, "%s: initial nextstop is T+%u seconds\n",
676 __func__, rtq_timeout);
677 }
0a7de745 678 bzero(&arg, sizeof(arg));
1c79356b 679 arg.rnh = rnh;
6d2010ae 680 arg.nextstop = timenow + rtq_timeout;
1c79356b 681 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
39236c6e
A
682 if (verbose) {
683 log(LOG_DEBUG, "%s: found %u, killed %u\n", __func__,
684 arg.found, arg.killed);
685 }
1c79356b
A
686 /*
687 * Attempt to be somewhat dynamic about this:
688 * If there are ``too many'' routes sitting around taking up space,
689 * then crank down the timeout, and see if we can't make some more
690 * go away. However, we make sure that we will never adjust more
691 * than once in rtq_timeout seconds, to keep from cranking down too
692 * hard.
693 */
39236c6e
A
694 ours = (arg.found - arg.killed);
695 if (ours > rtq_toomany &&
696 ((timenow - last_adjusted_timeout) >= (uint64_t)rtq_timeout) &&
697 rtq_reallyold > rtq_minreallyold) {
698 rtq_reallyold = 2 * rtq_reallyold / 3;
0a7de745 699 if (rtq_reallyold < rtq_minreallyold) {
1c79356b 700 rtq_reallyold = rtq_minreallyold;
0a7de745 701 }
1c79356b 702
6d2010ae 703 last_adjusted_timeout = timenow;
39236c6e
A
704 if (verbose) {
705 log(LOG_DEBUG, "%s: adjusted rtq_reallyold to %d "
706 "seconds\n", __func__, rtq_reallyold);
707 }
1c79356b
A
708 arg.found = arg.killed = 0;
709 arg.updating = 1;
1c79356b 710 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
1c79356b
A
711 }
712
713 atv.tv_usec = 0;
6d2010ae 714 atv.tv_sec = arg.nextstop - timenow;
39236c6e
A
715 /* re-arm the timer only if there's work to do */
716 in6_rtqtimo_run = 0;
0a7de745 717 if (ours > 0) {
39236c6e 718 in6_sched_rtqtimo(&atv);
0a7de745 719 } else if (verbose) {
39236c6e 720 log(LOG_DEBUG, "%s: not rescheduling timer\n", __func__);
0a7de745 721 }
b0d623f7 722 lck_mtx_unlock(rnh_lock);
1c79356b
A
723}
724
39236c6e
A
725static void
726in6_sched_rtqtimo(struct timeval *atv)
1c79356b 727{
5ba3f43e 728 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1c79356b 729
39236c6e
A
730 if (!in6_rtqtimo_run) {
731 struct timeval tv;
1c79356b 732
39236c6e
A
733 if (atv == NULL) {
734 tv.tv_usec = 0;
735 tv.tv_sec = MAX(rtq_timeout / 10, 1);
736 atv = &tv;
737 }
738 if (rt_verbose > 1) {
739 log(LOG_DEBUG, "%s: timer scheduled in "
740 "T+%llus.%lluu\n", __func__,
741 (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec);
1c79356b 742 }
39236c6e
A
743 in6_rtqtimo_run = 1;
744 timeout(in6_rtqtimo, NULL, tvtohz(atv));
1c79356b 745 }
1c79356b
A
746}
747
39236c6e
A
748void
749in6_rtqdrain(void)
1c79356b 750{
39236c6e
A
751 struct radix_node_head *rnh;
752 struct rtqk_arg arg;
91447636 753
0a7de745 754 if (rt_verbose > 1) {
39236c6e 755 log(LOG_DEBUG, "%s: draining routes\n", __func__);
0a7de745 756 }
1c79356b 757
b0d623f7 758 lck_mtx_lock(rnh_lock);
39236c6e
A
759 rnh = rt_tables[AF_INET6];
760 VERIFY(rnh != NULL);
0a7de745 761 bzero(&arg, sizeof(arg));
1c79356b 762 arg.rnh = rnh;
1c79356b 763 arg.draining = 1;
1c79356b 764 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
d1ecb069 765 lck_mtx_unlock(rnh_lock);
1c79356b 766}
1c79356b
A
767
768/*
769 * Initialize our routing tree.
770 */
771int
772in6_inithead(void **head, int off)
773{
774 struct radix_node_head *rnh;
775
39236c6e
A
776 /* If called from route_init(), make sure it is exactly once */
777 VERIFY(head != (void **)&rt_tables[AF_INET6] || *head == NULL);
778
0a7de745
A
779 if (!rn_inithead(head, off)) {
780 return 0;
781 }
1c79356b 782
39236c6e
A
783 /*
784 * We can get here from nfs_subs.c as well, in which case this
785 * won't be for the real routing table and thus we're done;
786 * this also takes care of the case when we're called more than
787 * once from anywhere but route_init().
788 */
0a7de745
A
789 if (head != (void **)&rt_tables[AF_INET6]) {
790 return 1; /* only do this for the real routing table */
791 }
1c79356b
A
792 rnh = *head;
793 rnh->rnh_addaddr = in6_addroute;
b0d623f7 794 rnh->rnh_deladdr = in6_deleteroute;
1c79356b 795 rnh->rnh_matchaddr = in6_matroute;
c910b4d9 796 rnh->rnh_matchaddr_args = in6_matroute_args;
1c79356b 797 rnh->rnh_close = in6_clsroute;
0a7de745 798 return 1;
1c79356b 799}