]>
Commit | Line | Data |
---|---|---|
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 | |
6d2010ae | 130 | extern int tvtohz(struct timeval *); |
1c79356b | 131 | |
39236c6e A |
132 | static int in6_rtqtimo_run; /* in6_rtqtimo is scheduled to run */ |
133 | static void in6_rtqtimo(void *); | |
134 | static void in6_sched_rtqtimo(struct timeval *); | |
135 | ||
136 | static struct radix_node *in6_addroute(void *, void *, struct radix_node_head *, | |
137 | struct radix_node *); | |
138 | static struct radix_node *in6_deleteroute(void *, void *, | |
139 | struct radix_node_head *); | |
140 | static struct radix_node *in6_matroute(void *, struct radix_node_head *); | |
c910b4d9 A |
141 | static struct radix_node *in6_matroute_args(void *, struct radix_node_head *, |
142 | rn_matchf_t *, void *); | |
39236c6e A |
143 | static void in6_clsroute(struct radix_node *, struct radix_node_head *); |
144 | static 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 | 150 | static int in6dynroutes; |
b0d623f7 | 151 | |
1c79356b A |
152 | /* |
153 | * Do what we need to do when inserting a route. | |
154 | */ | |
155 | static struct radix_node * | |
156 | in6_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 | ||
39236c6e A |
169 | if (verbose) |
170 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
171 | ||
b0d623f7 A |
172 | /* |
173 | * If this is a dynamic route (which is created via Redirect) and | |
174 | * we already have the maximum acceptable number of such route entries, | |
175 | * reject creating a new one. We could initiate garbage collection to | |
176 | * make available space right now, but the benefit would probably not | |
177 | * be worth the cleaning overhead; we only have to endure a slightly | |
39236c6e | 178 | * suboptimal path even without the redirected route. |
b0d623f7 | 179 | */ |
39236c6e | 180 | if ((rt->rt_flags & RTF_DYNAMIC) && |
b0d623f7 A |
181 | ip6_maxdynroutes >= 0 && in6dynroutes >= ip6_maxdynroutes) |
182 | return (NULL); | |
183 | ||
1c79356b A |
184 | /* |
185 | * For IPv6, all unicast non-host routes are automatically cloning. | |
186 | */ | |
187 | if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | |
188 | rt->rt_flags |= RTF_MULTICAST; | |
189 | ||
39236c6e | 190 | if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) |
1c79356b | 191 | rt->rt_flags |= RTF_PRCLONING; |
1c79356b A |
192 | |
193 | /* | |
194 | * A little bit of help for both IPv6 output and input: | |
195 | * For local addresses, we make sure that RTF_LOCAL is set, | |
196 | * with the thought that this might one day be used to speed up | |
197 | * ip_input(). | |
198 | * | |
199 | * We also mark routes to multicast addresses as such, because | |
200 | * it's easy to do and might be useful (but this is much more | |
201 | * dubious since it's so easy to inspect the address). (This | |
202 | * is done above.) | |
203 | * | |
204 | * XXX | |
205 | * should elaborate the code. | |
206 | */ | |
207 | if (rt->rt_flags & RTF_HOST) { | |
6d2010ae | 208 | IFA_LOCK_SPIN(rt->rt_ifa); |
39236c6e A |
209 | if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)-> |
210 | sin6_addr, &sin6->sin6_addr)) { | |
1c79356b A |
211 | rt->rt_flags |= RTF_LOCAL; |
212 | } | |
6d2010ae | 213 | IFA_UNLOCK(rt->rt_ifa); |
1c79356b A |
214 | } |
215 | ||
39236c6e A |
216 | if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) && |
217 | rt->rt_ifp) | |
1c79356b A |
218 | rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; |
219 | ||
220 | ret = rn_addroute(v_arg, n_arg, head, treenodes); | |
b0d623f7 | 221 | if (ret == NULL && (rt->rt_flags & RTF_HOST)) { |
1c79356b A |
222 | struct rtentry *rt2; |
223 | /* | |
224 | * We are trying to add a host route, but can't. | |
225 | * Find out if it is because of an | |
39236c6e | 226 | * ND6 entry and delete it if so. |
1c79356b | 227 | */ |
6d2010ae A |
228 | rt2 = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0, |
229 | RTF_CLONING | RTF_PRCLONING, sin6_get_ifscope(rt_key(rt))); | |
39236c6e A |
230 | if (rt2 != NULL) { |
231 | char dbufc[MAX_IPv6_STR_LEN]; | |
232 | ||
b0d623f7 | 233 | RT_LOCK(rt2); |
39236c6e A |
234 | if (verbose) |
235 | rt_str(rt2, dbufc, sizeof (dbufc), NULL, 0); | |
236 | ||
b0d623f7 A |
237 | if ((rt2->rt_flags & RTF_LLINFO) && |
238 | (rt2->rt_flags & RTF_HOST) && | |
239 | rt2->rt_gateway != NULL && | |
240 | rt2->rt_gateway->sa_family == AF_LINK) { | |
39236c6e A |
241 | if (verbose) { |
242 | log(LOG_DEBUG, "%s: unable to insert " | |
243 | "route to %s:%s, flags=%b, due to " | |
244 | "existing ND6 route %s->%s " | |
245 | "flags=%b, attempting to delete\n", | |
246 | __func__, dbuf, | |
247 | (rt->rt_ifp != NULL) ? | |
248 | rt->rt_ifp->if_xname : "", | |
249 | rt->rt_flags, RTF_BITS, | |
250 | dbufc, (rt2->rt_ifp != NULL) ? | |
251 | rt2->rt_ifp->if_xname : "", | |
252 | rt2->rt_flags, RTF_BITS); | |
253 | } | |
b0d623f7 A |
254 | /* |
255 | * Safe to drop rt_lock and use rt_key, | |
256 | * rt_gateway, since holding rnh_lock here | |
257 | * prevents another thread from calling | |
258 | * rt_setgate() on this route. | |
259 | */ | |
260 | RT_UNLOCK(rt2); | |
261 | (void) rtrequest_locked(RTM_DELETE, rt_key(rt2), | |
262 | rt2->rt_gateway, rt_mask(rt2), | |
39236c6e | 263 | rt2->rt_flags, NULL); |
1c79356b | 264 | ret = rn_addroute(v_arg, n_arg, head, |
39236c6e | 265 | treenodes); |
b0d623f7 A |
266 | } else { |
267 | RT_UNLOCK(rt2); | |
1c79356b | 268 | } |
91447636 | 269 | rtfree_locked(rt2); |
1c79356b | 270 | } |
b0d623f7 | 271 | } else if (ret == NULL && (rt->rt_flags & RTF_CLONING)) { |
1c79356b A |
272 | struct rtentry *rt2; |
273 | /* | |
274 | * We are trying to add a net route, but can't. | |
275 | * The following case should be allowed, so we'll make a | |
276 | * special check for this: | |
277 | * Two IPv6 addresses with the same prefix is assigned | |
278 | * to a single interrface. | |
279 | * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1) | |
280 | * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2) | |
281 | * In this case, (*1) and (*2) want to add the same | |
282 | * net route entry, 3ffe:0501:: -> if0. | |
283 | * This case should not raise an error. | |
284 | */ | |
6d2010ae A |
285 | rt2 = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0, |
286 | RTF_CLONING | RTF_PRCLONING, sin6_get_ifscope(rt_key(rt))); | |
39236c6e | 287 | if (rt2 != NULL) { |
b0d623f7 | 288 | RT_LOCK(rt2); |
39236c6e A |
289 | if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST| |
290 | RTF_GATEWAY)) == RTF_CLONING && | |
291 | rt2->rt_gateway && | |
292 | rt2->rt_gateway->sa_family == AF_LINK && | |
293 | rt2->rt_ifp == rt->rt_ifp) { | |
1c79356b A |
294 | ret = rt2->rt_nodes; |
295 | } | |
b0d623f7 | 296 | RT_UNLOCK(rt2); |
91447636 | 297 | rtfree_locked(rt2); |
1c79356b A |
298 | } |
299 | } | |
b0d623f7 | 300 | |
39236c6e | 301 | if (ret != NULL && (rt->rt_flags & RTF_DYNAMIC)) |
b0d623f7 A |
302 | in6dynroutes++; |
303 | ||
39236c6e A |
304 | if (!verbose) |
305 | goto done; | |
306 | ||
307 | if (ret != NULL) { | |
308 | if (flags != rt->rt_flags) { | |
309 | log(LOG_DEBUG, "%s: route to %s->%s->%s inserted, " | |
310 | "oflags=%b, flags=%b\n", __func__, | |
311 | dbuf, gbuf, (rt->rt_ifp != NULL) ? | |
312 | rt->rt_ifp->if_xname : "", flags, RTF_BITS, | |
313 | rt->rt_flags, RTF_BITS); | |
314 | } else { | |
315 | log(LOG_DEBUG, "%s: route to %s->%s->%s inserted, " | |
316 | "flags=%b\n", __func__, dbuf, gbuf, | |
317 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
318 | rt->rt_flags, RTF_BITS); | |
319 | } | |
320 | } else { | |
321 | log(LOG_DEBUG, "%s: unable to insert route to %s->%s->%s, " | |
322 | "flags=%b, already exists\n", __func__, dbuf, gbuf, | |
323 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
324 | rt->rt_flags, RTF_BITS); | |
325 | } | |
326 | done: | |
327 | return (ret); | |
1c79356b A |
328 | } |
329 | ||
b0d623f7 | 330 | static struct radix_node * |
39236c6e | 331 | in6_deleteroute(void *v_arg, void *netmask_arg, struct radix_node_head *head) |
b0d623f7 A |
332 | { |
333 | struct radix_node *rn; | |
334 | ||
5ba3f43e | 335 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
b0d623f7 A |
336 | |
337 | rn = rn_delete(v_arg, netmask_arg, head); | |
338 | if (rn != NULL) { | |
339 | struct rtentry *rt = (struct rtentry *)rn; | |
39236c6e A |
340 | |
341 | RT_LOCK(rt); | |
342 | if (rt->rt_flags & RTF_DYNAMIC) | |
b0d623f7 | 343 | in6dynroutes--; |
39236c6e A |
344 | if (rt_verbose > 1) { |
345 | char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN]; | |
346 | ||
347 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
348 | log(LOG_DEBUG, "%s: route to %s->%s->%s deleted, " | |
349 | "flags=%b\n", __func__, dbuf, gbuf, | |
350 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
351 | rt->rt_flags, RTF_BITS); | |
352 | } | |
b0d623f7 A |
353 | RT_UNLOCK(rt); |
354 | } | |
b0d623f7 A |
355 | return (rn); |
356 | } | |
357 | ||
6d2010ae A |
358 | /* |
359 | * Validate (unexpire) an expiring AF_INET6 route. | |
360 | */ | |
361 | struct radix_node * | |
362 | in6_validate(struct radix_node *rn) | |
363 | { | |
364 | struct rtentry *rt = (struct rtentry *)rn; | |
365 | ||
366 | RT_LOCK_ASSERT_HELD(rt); | |
367 | ||
368 | /* This is first reference? */ | |
39236c6e A |
369 | if (rt->rt_refcnt == 0) { |
370 | if (rt_verbose > 2) { | |
371 | char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN]; | |
372 | ||
373 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
374 | log(LOG_DEBUG, "%s: route to %s->%s->%s validated, " | |
375 | "flags=%b\n", __func__, dbuf, gbuf, | |
376 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
377 | rt->rt_flags, RTF_BITS); | |
378 | } | |
379 | ||
380 | /* | |
381 | * It's one of ours; unexpire it. If the timer is already | |
382 | * scheduled, let it run later as it won't re-arm itself | |
383 | * if there's nothing to do. | |
384 | */ | |
385 | if (rt->rt_flags & RTPRF_OURS) { | |
386 | rt->rt_flags &= ~RTPRF_OURS; | |
387 | rt_setexpire(rt, 0); | |
388 | } | |
6d2010ae A |
389 | } |
390 | return (rn); | |
391 | } | |
392 | ||
c910b4d9 A |
393 | /* |
394 | * Similar to in6_matroute_args except without the leaf-matching parameters. | |
395 | */ | |
396 | static struct radix_node * | |
397 | in6_matroute(void *v_arg, struct radix_node_head *head) | |
398 | { | |
399 | return (in6_matroute_args(v_arg, head, NULL, NULL)); | |
400 | } | |
401 | ||
1c79356b A |
402 | /* |
403 | * This code is the inverse of in6_clsroute: on first reference, if we | |
404 | * were managing the route, stop doing so and set the expiration timer | |
405 | * back off again. | |
406 | */ | |
407 | static struct radix_node * | |
c910b4d9 A |
408 | in6_matroute_args(void *v_arg, struct radix_node_head *head, |
409 | rn_matchf_t *f, void *w) | |
1c79356b | 410 | { |
c910b4d9 | 411 | struct radix_node *rn = rn_match_args(v_arg, head, f, w); |
1c79356b | 412 | |
6d2010ae A |
413 | if (rn != NULL) { |
414 | RT_LOCK_SPIN((struct rtentry *)rn); | |
415 | in6_validate(rn); | |
416 | RT_UNLOCK((struct rtentry *)rn); | |
1c79356b | 417 | } |
c910b4d9 | 418 | return (rn); |
1c79356b A |
419 | } |
420 | ||
9bccf70c A |
421 | SYSCTL_DECL(_net_inet6_ip6); |
422 | ||
39236c6e A |
423 | /* one hour is ``really old'' */ |
424 | static uint32_t rtq_reallyold = 60*60; | |
425 | SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire, | |
426 | CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_reallyold, 0, ""); | |
b0d623f7 | 427 | |
39236c6e A |
428 | /* never automatically crank down to less */ |
429 | static uint32_t rtq_minreallyold = 10; | |
430 | SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire, | |
431 | CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_minreallyold, 0, ""); | |
b0d623f7 | 432 | |
39236c6e A |
433 | /* 128 cached routes is ``too many'' */ |
434 | static uint32_t rtq_toomany = 128; | |
435 | SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache, | |
436 | CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_toomany, 0, ""); | |
1c79356b A |
437 | |
438 | /* | |
439 | * On last reference drop, mark the route as belong to us so that it can be | |
440 | * timed out. | |
441 | */ | |
442 | static void | |
39236c6e | 443 | in6_clsroute(struct radix_node *rn, struct radix_node_head *head) |
1c79356b | 444 | { |
39236c6e A |
445 | #pragma unused(head) |
446 | char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN]; | |
1c79356b | 447 | struct rtentry *rt = (struct rtentry *)rn; |
39236c6e | 448 | boolean_t verbose = (rt_verbose > 1); |
1c79356b | 449 | |
5ba3f43e | 450 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
b0d623f7 A |
451 | RT_LOCK_ASSERT_HELD(rt); |
452 | ||
1c79356b | 453 | if (!(rt->rt_flags & RTF_UP)) |
5ba3f43e | 454 | return; /* prophylactic measures */ |
1c79356b A |
455 | |
456 | if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) | |
457 | return; | |
458 | ||
b0d623f7 A |
459 | if (rt->rt_flags & RTPRF_OURS) |
460 | return; | |
461 | ||
462 | if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC))) | |
1c79356b A |
463 | return; |
464 | ||
39236c6e A |
465 | if (verbose) |
466 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
467 | ||
1c79356b | 468 | /* |
2d21ac55 A |
469 | * Delete the route immediately if RTF_DELCLONE is set or |
470 | * if route caching is disabled (rtq_reallyold set to 0). | |
471 | * Otherwise, let it expire and be deleted by in6_rtqkill(). | |
1c79356b | 472 | */ |
2d21ac55 | 473 | if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) { |
39236c6e A |
474 | int err; |
475 | ||
476 | if (verbose) { | |
477 | log(LOG_DEBUG, "%s: deleting route to %s->%s->%s, " | |
478 | "flags=%b\n", __func__, dbuf, gbuf, | |
479 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
480 | rt->rt_flags, RTF_BITS); | |
481 | } | |
2d21ac55 A |
482 | /* |
483 | * Delete the route from the radix tree but since we are | |
484 | * called when the route's reference count is 0, don't | |
485 | * deallocate it until we return from this routine by | |
486 | * telling rtrequest that we're interested in it. | |
b0d623f7 A |
487 | * Safe to drop rt_lock and use rt_key, rt_gateway, |
488 | * since holding rnh_lock here prevents another thread | |
489 | * from calling rt_setgate() on this route. | |
2d21ac55 | 490 | */ |
b0d623f7 | 491 | RT_UNLOCK(rt); |
39236c6e A |
492 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), |
493 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt); | |
494 | if (err == 0) { | |
2d21ac55 | 495 | /* Now let the caller free it */ |
b0d623f7 A |
496 | RT_LOCK(rt); |
497 | RT_REMREF_LOCKED(rt); | |
498 | } else { | |
499 | RT_LOCK(rt); | |
39236c6e A |
500 | if (!verbose) |
501 | rt_str(rt, dbuf, sizeof (dbuf), | |
502 | gbuf, sizeof (gbuf)); | |
503 | log(LOG_ERR, "%s: error deleting route to " | |
504 | "%s->%s->%s, flags=%b, err=%d\n", __func__, | |
505 | dbuf, gbuf, (rt->rt_ifp != NULL) ? | |
506 | rt->rt_ifp->if_xname : "", rt->rt_flags, | |
507 | RTF_BITS, err); | |
2d21ac55 A |
508 | } |
509 | } else { | |
6d2010ae | 510 | uint64_t timenow; |
2d21ac55 | 511 | |
6d2010ae | 512 | timenow = net_uptime(); |
1c79356b | 513 | rt->rt_flags |= RTPRF_OURS; |
39236c6e A |
514 | rt_setexpire(rt, timenow + rtq_reallyold); |
515 | ||
516 | if (verbose) { | |
517 | log(LOG_DEBUG, "%s: route to %s->%s->%s invalidated, " | |
5ba3f43e A |
518 | "flags=%b, expire=T+%u\n", __func__, dbuf, gbuf, |
519 | (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "", | |
520 | rt->rt_flags, RTF_BITS, rt->rt_expire - timenow); | |
39236c6e A |
521 | } |
522 | ||
523 | /* We have at least one entry; arm the timer if not already */ | |
524 | in6_sched_rtqtimo(NULL); | |
1c79356b A |
525 | } |
526 | } | |
527 | ||
528 | struct rtqk_arg { | |
529 | struct radix_node_head *rnh; | |
1c79356b A |
530 | int updating; |
531 | int draining; | |
39236c6e A |
532 | uint32_t killed; |
533 | uint32_t found; | |
6d2010ae | 534 | uint64_t nextstop; |
1c79356b A |
535 | }; |
536 | ||
537 | /* | |
538 | * Get rid of old routes. When draining, this deletes everything, even when | |
b0d623f7 A |
539 | * the timeout is not expired yet. This also applies if the route is dynamic |
540 | * and there are sufficiently large number of such routes (more than a half of | |
541 | * maximum). When updating, this makes sure that nothing has a timeout longer | |
542 | * than the current value of rtq_reallyold. | |
1c79356b A |
543 | */ |
544 | static int | |
545 | in6_rtqkill(struct radix_node *rn, void *rock) | |
546 | { | |
547 | struct rtqk_arg *ap = rock; | |
548 | struct rtentry *rt = (struct rtentry *)rn; | |
39236c6e | 549 | boolean_t verbose = (rt_verbose > 1); |
6d2010ae | 550 | uint64_t timenow; |
39236c6e | 551 | int err; |
91447636 | 552 | |
6d2010ae | 553 | timenow = net_uptime(); |
5ba3f43e | 554 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 555 | |
b0d623f7 | 556 | RT_LOCK(rt); |
1c79356b | 557 | if (rt->rt_flags & RTPRF_OURS) { |
39236c6e A |
558 | char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN]; |
559 | ||
560 | if (verbose) | |
561 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
562 | ||
1c79356b | 563 | ap->found++; |
6d2010ae A |
564 | VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0); |
565 | VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0); | |
566 | if (ap->draining || rt->rt_expire <= timenow || | |
39236c6e | 567 | ((rt->rt_flags & RTF_DYNAMIC) && ip6_maxdynroutes >= 0 && |
b0d623f7 | 568 | in6dynroutes > ip6_maxdynroutes / 2)) { |
39236c6e A |
569 | if (rt->rt_refcnt > 0) { |
570 | panic("%s: route %p marked with RTPRF_OURS " | |
571 | "with non-zero refcnt (%u)", __func__, | |
572 | rt, rt->rt_refcnt); | |
573 | /* NOTREACHED */ | |
574 | } | |
5ba3f43e | 575 | |
39236c6e A |
576 | if (verbose) { |
577 | log(LOG_DEBUG, "%s: deleting route to " | |
578 | "%s->%s->%s, flags=%b, draining=%d\n", | |
579 | __func__, dbuf, gbuf, (rt->rt_ifp != NULL) ? | |
580 | rt->rt_ifp->if_xname : "", rt->rt_flags, | |
581 | RTF_BITS, ap->draining); | |
582 | } | |
583 | RT_ADDREF_LOCKED(rt); /* for us to free below */ | |
b0d623f7 A |
584 | /* |
585 | * Delete this route since we're done with it; | |
586 | * the route may be freed afterwards, so we | |
587 | * can no longer refer to 'rt' upon returning | |
588 | * from rtrequest(). Safe to drop rt_lock and | |
589 | * use rt_key, rt_gateway, since holding rnh_lock | |
590 | * here prevents another thread from calling | |
591 | * rt_setgate() on this route. | |
592 | */ | |
593 | RT_UNLOCK(rt); | |
594 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), | |
39236c6e A |
595 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL); |
596 | if (err != 0) { | |
597 | RT_LOCK(rt); | |
598 | if (!verbose) | |
599 | rt_str(rt, dbuf, sizeof (dbuf), | |
600 | gbuf, sizeof (gbuf)); | |
601 | log(LOG_ERR, "%s: error deleting route to " | |
602 | "%s->%s->%s, flags=%b, err=%d\n", __func__, | |
603 | dbuf, gbuf, (rt->rt_ifp != NULL) ? | |
604 | rt->rt_ifp->if_xname : "", rt->rt_flags, | |
605 | RTF_BITS, err); | |
606 | RT_UNLOCK(rt); | |
1c79356b A |
607 | } else { |
608 | ap->killed++; | |
609 | } | |
39236c6e | 610 | rtfree_locked(rt); |
1c79356b | 611 | } else { |
39236c6e A |
612 | uint64_t expire = (rt->rt_expire - timenow); |
613 | ||
614 | if (ap->updating && expire > rtq_reallyold) { | |
615 | rt_setexpire(rt, timenow + rtq_reallyold); | |
616 | if (verbose) { | |
617 | log(LOG_DEBUG, "%s: route to " | |
618 | "%s->%s->%s, flags=%b, adjusted " | |
619 | "expire=T+%u (was T+%u)\n", | |
620 | __func__, dbuf, gbuf, | |
621 | (rt->rt_ifp != NULL) ? | |
622 | rt->rt_ifp->if_xname : "", | |
623 | rt->rt_flags, RTF_BITS, | |
624 | (rt->rt_expire - timenow), expire); | |
625 | } | |
1c79356b | 626 | } |
39236c6e | 627 | ap->nextstop = lmin(ap->nextstop, rt->rt_expire); |
b0d623f7 | 628 | RT_UNLOCK(rt); |
1c79356b | 629 | } |
b0d623f7 A |
630 | } else { |
631 | RT_UNLOCK(rt); | |
1c79356b A |
632 | } |
633 | ||
39236c6e | 634 | return (0); |
1c79356b A |
635 | } |
636 | ||
39236c6e | 637 | #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ |
1c79356b A |
638 | static int rtq_timeout = RTQ_TIMEOUT; |
639 | ||
640 | static void | |
39236c6e | 641 | in6_rtqtimo(void *targ) |
1c79356b | 642 | { |
39236c6e A |
643 | #pragma unused(targ) |
644 | struct radix_node_head *rnh; | |
1c79356b A |
645 | struct rtqk_arg arg; |
646 | struct timeval atv; | |
6d2010ae | 647 | static uint64_t last_adjusted_timeout = 0; |
39236c6e | 648 | boolean_t verbose = (rt_verbose > 1); |
6d2010ae | 649 | uint64_t timenow; |
39236c6e | 650 | uint32_t ours; |
91447636 | 651 | |
b0d623f7 | 652 | lck_mtx_lock(rnh_lock); |
39236c6e A |
653 | rnh = rt_tables[AF_INET6]; |
654 | VERIFY(rnh != NULL); | |
655 | ||
2d21ac55 | 656 | /* Get the timestamp after we acquire the lock for better accuracy */ |
6d2010ae | 657 | timenow = net_uptime(); |
39236c6e A |
658 | if (verbose) { |
659 | log(LOG_DEBUG, "%s: initial nextstop is T+%u seconds\n", | |
660 | __func__, rtq_timeout); | |
661 | } | |
662 | bzero(&arg, sizeof (arg)); | |
1c79356b | 663 | arg.rnh = rnh; |
6d2010ae | 664 | arg.nextstop = timenow + rtq_timeout; |
1c79356b | 665 | rnh->rnh_walktree(rnh, in6_rtqkill, &arg); |
39236c6e A |
666 | if (verbose) { |
667 | log(LOG_DEBUG, "%s: found %u, killed %u\n", __func__, | |
668 | arg.found, arg.killed); | |
669 | } | |
1c79356b A |
670 | /* |
671 | * Attempt to be somewhat dynamic about this: | |
672 | * If there are ``too many'' routes sitting around taking up space, | |
673 | * then crank down the timeout, and see if we can't make some more | |
674 | * go away. However, we make sure that we will never adjust more | |
675 | * than once in rtq_timeout seconds, to keep from cranking down too | |
676 | * hard. | |
677 | */ | |
39236c6e A |
678 | ours = (arg.found - arg.killed); |
679 | if (ours > rtq_toomany && | |
680 | ((timenow - last_adjusted_timeout) >= (uint64_t)rtq_timeout) && | |
681 | rtq_reallyold > rtq_minreallyold) { | |
682 | rtq_reallyold = 2 * rtq_reallyold / 3; | |
683 | if (rtq_reallyold < rtq_minreallyold) | |
1c79356b | 684 | rtq_reallyold = rtq_minreallyold; |
1c79356b | 685 | |
6d2010ae | 686 | last_adjusted_timeout = timenow; |
39236c6e A |
687 | if (verbose) { |
688 | log(LOG_DEBUG, "%s: adjusted rtq_reallyold to %d " | |
689 | "seconds\n", __func__, rtq_reallyold); | |
690 | } | |
1c79356b A |
691 | arg.found = arg.killed = 0; |
692 | arg.updating = 1; | |
1c79356b | 693 | rnh->rnh_walktree(rnh, in6_rtqkill, &arg); |
1c79356b A |
694 | } |
695 | ||
696 | atv.tv_usec = 0; | |
6d2010ae | 697 | atv.tv_sec = arg.nextstop - timenow; |
39236c6e A |
698 | /* re-arm the timer only if there's work to do */ |
699 | in6_rtqtimo_run = 0; | |
700 | if (ours > 0) | |
701 | in6_sched_rtqtimo(&atv); | |
702 | else if (verbose) | |
703 | log(LOG_DEBUG, "%s: not rescheduling timer\n", __func__); | |
b0d623f7 | 704 | lck_mtx_unlock(rnh_lock); |
1c79356b A |
705 | } |
706 | ||
39236c6e A |
707 | static void |
708 | in6_sched_rtqtimo(struct timeval *atv) | |
1c79356b | 709 | { |
5ba3f43e | 710 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
1c79356b | 711 | |
39236c6e A |
712 | if (!in6_rtqtimo_run) { |
713 | struct timeval tv; | |
1c79356b | 714 | |
39236c6e A |
715 | if (atv == NULL) { |
716 | tv.tv_usec = 0; | |
717 | tv.tv_sec = MAX(rtq_timeout / 10, 1); | |
718 | atv = &tv; | |
719 | } | |
720 | if (rt_verbose > 1) { | |
721 | log(LOG_DEBUG, "%s: timer scheduled in " | |
722 | "T+%llus.%lluu\n", __func__, | |
723 | (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec); | |
1c79356b | 724 | } |
39236c6e A |
725 | in6_rtqtimo_run = 1; |
726 | timeout(in6_rtqtimo, NULL, tvtohz(atv)); | |
1c79356b | 727 | } |
1c79356b A |
728 | } |
729 | ||
39236c6e A |
730 | void |
731 | in6_rtqdrain(void) | |
1c79356b | 732 | { |
39236c6e A |
733 | struct radix_node_head *rnh; |
734 | struct rtqk_arg arg; | |
91447636 | 735 | |
39236c6e A |
736 | if (rt_verbose > 1) |
737 | log(LOG_DEBUG, "%s: draining routes\n", __func__); | |
1c79356b | 738 | |
b0d623f7 | 739 | lck_mtx_lock(rnh_lock); |
39236c6e A |
740 | rnh = rt_tables[AF_INET6]; |
741 | VERIFY(rnh != NULL); | |
742 | bzero(&arg, sizeof (arg)); | |
1c79356b | 743 | arg.rnh = rnh; |
1c79356b | 744 | arg.draining = 1; |
1c79356b | 745 | rnh->rnh_walktree(rnh, in6_rtqkill, &arg); |
d1ecb069 | 746 | lck_mtx_unlock(rnh_lock); |
1c79356b | 747 | } |
1c79356b A |
748 | |
749 | /* | |
750 | * Initialize our routing tree. | |
751 | */ | |
752 | int | |
753 | in6_inithead(void **head, int off) | |
754 | { | |
755 | struct radix_node_head *rnh; | |
756 | ||
39236c6e A |
757 | /* If called from route_init(), make sure it is exactly once */ |
758 | VERIFY(head != (void **)&rt_tables[AF_INET6] || *head == NULL); | |
759 | ||
1c79356b | 760 | if (!rn_inithead(head, off)) |
39236c6e | 761 | return (0); |
1c79356b | 762 | |
39236c6e A |
763 | /* |
764 | * We can get here from nfs_subs.c as well, in which case this | |
765 | * won't be for the real routing table and thus we're done; | |
766 | * this also takes care of the case when we're called more than | |
767 | * once from anywhere but route_init(). | |
768 | */ | |
769 | if (head != (void **)&rt_tables[AF_INET6]) | |
770 | return (1); /* only do this for the real routing table */ | |
1c79356b A |
771 | |
772 | rnh = *head; | |
773 | rnh->rnh_addaddr = in6_addroute; | |
b0d623f7 | 774 | rnh->rnh_deladdr = in6_deleteroute; |
1c79356b | 775 | rnh->rnh_matchaddr = in6_matroute; |
c910b4d9 | 776 | rnh->rnh_matchaddr_args = in6_matroute_args; |
1c79356b | 777 | rnh->rnh_close = in6_clsroute; |
39236c6e | 778 | return (1); |
1c79356b | 779 | } |