]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/netinet/in_rmx.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / netinet / in_rmx.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2009 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/*
29 * Copyright 1994, 1995 Massachusetts Institute of Technology
30 *
31 * Permission to use, copy, modify, and distribute this software and
32 * its documentation for any purpose and without fee is hereby
33 * granted, provided that both the above copyright notice and this
34 * permission notice appear in all copies, that both the above
35 * copyright notice and this permission notice appear in all
36 * supporting documentation, and that the name of M.I.T. not be used
37 * in advertising or publicity pertaining to distribution of the
38 * software without specific, written prior permission. M.I.T. makes
39 * no representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied
41 * warranty.
42 *
43 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
44 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
45 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
46 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
47 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
50 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.1 2001/05/14 08:23:49 ru Exp $
57 */
58
59/*
60 * This code does two things necessary for the enhanced TCP metrics to
61 * function in a useful manner:
62 * 1) It marks all non-host routes as `cloning', thus ensuring that
63 * every actual reference to such a route actually gets turned
64 * into a reference to a host route to the specific destination
65 * requested.
66 * 2) When such routes lose all their references, it arranges for them
67 * to be deleted in some random collection of circumstances, so that
68 * a large quantity of stale routing data is not kept in kernel memory
69 * indefinitely. See in_rtqtimo() below for the exact mechanism.
70 */
71
72#include <sys/param.h>
73#include <sys/systm.h>
74#include <sys/kernel.h>
75#include <sys/sysctl.h>
76#include <sys/socket.h>
77#include <sys/mbuf.h>
78#include <sys/syslog.h>
79#include <kern/lock.h>
80
81#include <net/if.h>
82#include <net/route.h>
83#include <netinet/in.h>
84#include <netinet/in_var.h>
85
86extern int tvtohz(struct timeval *);
87extern int in_inithead(void **head, int off);
88
89#ifdef __APPLE__
90static void in_rtqtimo(void *rock);
91#endif
92
93static struct radix_node *in_matroute_args(void *, struct radix_node_head *,
94 rn_matchf_t *f, void *);
95
96#define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
97
98/*
99 * Do what we need to do when inserting a route.
100 */
101static struct radix_node *
102in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
103 struct radix_node *treenodes)
104{
105 struct rtentry *rt = (struct rtentry *)treenodes;
106 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
107 struct radix_node *ret;
108
109 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
110 RT_LOCK_ASSERT_HELD(rt);
111
112 /*
113 * For IP, all unicast non-host routes are automatically cloning.
114 */
115 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
116 rt->rt_flags |= RTF_MULTICAST;
117
118 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
119 rt->rt_flags |= RTF_PRCLONING;
120 }
121
122 /*
123 * A little bit of help for both IP output and input:
124 * For host routes, we make sure that RTF_BROADCAST
125 * is set for anything that looks like a broadcast address.
126 * This way, we can avoid an expensive call to in_broadcast()
127 * in ip_output() most of the time (because the route passed
128 * to ip_output() is almost always a host route).
129 *
130 * We also do the same for local addresses, with the thought
131 * that this might one day be used to speed up ip_input().
132 *
133 * We also mark routes to multicast addresses as such, because
134 * it's easy to do and might be useful (but this is much more
135 * dubious since it's so easy to inspect the address). (This
136 * is done above.)
137 */
138 if (rt->rt_flags & RTF_HOST) {
139 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
140 rt->rt_flags |= RTF_BROADCAST;
141 } else {
142#define satosin(sa) ((struct sockaddr_in *)sa)
143 if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
144 == sin->sin_addr.s_addr)
145 rt->rt_flags |= RTF_LOCAL;
146#undef satosin
147 }
148 }
149
150 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
151 && rt->rt_ifp)
152 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
153
154 ret = rn_addroute(v_arg, n_arg, head, treenodes);
155 if (ret == NULL && rt->rt_flags & RTF_HOST) {
156 struct rtentry *rt2;
157 /*
158 * We are trying to add a host route, but can't.
159 * Find out if it is because of an
160 * ARP entry and delete it if so.
161 */
162 rt2 = rtalloc1_scoped_locked(rt_key(rt), 0,
163 RTF_CLONING | RTF_PRCLONING, sa_get_ifscope(rt_key(rt)));
164 if (rt2) {
165 RT_LOCK(rt2);
166 if ((rt2->rt_flags & RTF_LLINFO) &&
167 (rt2->rt_flags & RTF_HOST) &&
168 rt2->rt_gateway != NULL &&
169 rt2->rt_gateway->sa_family == AF_LINK) {
170 /*
171 * Safe to drop rt_lock and use rt_key,
172 * rt_gateway, since holding rnh_lock here
173 * prevents another thread from calling
174 * rt_setgate() on this route.
175 */
176 RT_UNLOCK(rt2);
177 rtrequest_locked(RTM_DELETE, rt_key(rt2),
178 rt2->rt_gateway, rt_mask(rt2),
179 rt2->rt_flags, 0);
180 ret = rn_addroute(v_arg, n_arg, head,
181 treenodes);
182 } else {
183 RT_UNLOCK(rt2);
184 }
185 rtfree_locked(rt2);
186 }
187 }
188 return ret;
189}
190
191/*
192 * Validate (unexpire) an expiring AF_INET route.
193 */
194struct radix_node *
195in_validate(struct radix_node *rn)
196{
197 struct rtentry *rt = (struct rtentry *)rn;
198
199 RT_LOCK_ASSERT_HELD(rt);
200
201 /* This is first reference? */
202 if (rt->rt_refcnt == 0 && (rt->rt_flags & RTPRF_OURS)) {
203 rt->rt_flags &= ~RTPRF_OURS;
204 rt->rt_rmx.rmx_expire = 0;
205 }
206 return (rn);
207}
208
209/*
210 * Similar to in_matroute_args except without the leaf-matching parameters.
211 */
212static struct radix_node *
213in_matroute(void *v_arg, struct radix_node_head *head)
214{
215 return (in_matroute_args(v_arg, head, NULL, NULL));
216}
217
218/*
219 * This code is the inverse of in_clsroute: on first reference, if we
220 * were managing the route, stop doing so and set the expiration timer
221 * back off again.
222 */
223static struct radix_node *
224in_matroute_args(void *v_arg, struct radix_node_head *head,
225 rn_matchf_t *f, void *w)
226{
227 struct radix_node *rn = rn_match_args(v_arg, head, f, w);
228
229 if (rn != NULL) {
230 RT_LOCK_SPIN((struct rtentry *)rn);
231 in_validate(rn);
232 RT_UNLOCK((struct rtentry *)rn);
233 }
234 return (rn);
235}
236
237static int rtq_reallyold = 60*60;
238 /* one hour is ``really old'' */
239SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
240 &rtq_reallyold , 0,
241 "Default expiration time on dynamically learned routes");
242
243static int rtq_minreallyold = 10;
244 /* never automatically crank down to less */
245SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
246 &rtq_minreallyold , 0,
247 "Minimum time to attempt to hold onto dynamically learned routes");
248
249static int rtq_toomany = 128;
250 /* 128 cached routes is ``too many'' */
251SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
252 &rtq_toomany , 0, "Upper limit on dynamically learned routes");
253
254#ifdef __APPLE__
255/* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
256 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
257 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
258 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
259 * That circular route was ignored on previous version of MacOS X because of a routing bug
260 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
261 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
262 * made in order to provide binary compatibility with AOL.
263 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
264 * confusing the routing table with a wrong route to the previous default gateway
265 * If for some reason a circular route is needed, turn this sysctl (net.inet.ip.check_route_selfref) to zero.
266 */
267int check_routeselfref = 1;
268SYSCTL_INT(_net_inet_ip, OID_AUTO, check_route_selfref, CTLFLAG_RW,
269 &check_routeselfref , 0, "");
270#endif
271
272int use_routegenid = 1;
273SYSCTL_INT(_net_inet_ip, OID_AUTO, use_route_genid, CTLFLAG_RW,
274 &use_routegenid , 0, "");
275
276/*
277 * On last reference drop, mark the route as belong to us so that it can be
278 * timed out.
279 */
280static void
281in_clsroute(struct radix_node *rn, __unused struct radix_node_head *head)
282{
283 struct rtentry *rt = (struct rtentry *)rn;
284
285 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
286 RT_LOCK_ASSERT_HELD(rt);
287
288 if (!(rt->rt_flags & RTF_UP))
289 return; /* prophylactic measures */
290
291 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
292 return;
293
294 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
295 return;
296
297 /*
298 * Delete the route immediately if RTF_DELCLONE is set or
299 * if route caching is disabled (rtq_reallyold set to 0).
300 * Otherwise, let it expire and be deleted by in_rtqkill().
301 */
302 if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) {
303 /*
304 * Delete the route from the radix tree but since we are
305 * called when the route's reference count is 0, don't
306 * deallocate it until we return from this routine by
307 * telling rtrequest that we're interested in it.
308 * Safe to drop rt_lock and use rt_key, rt_gateway since
309 * holding rnh_lock here prevents another thread from
310 * calling rt_setgate() on this route.
311 */
312 RT_UNLOCK(rt);
313 if (rtrequest_locked(RTM_DELETE, (struct sockaddr *)rt_key(rt),
314 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt) == 0) {
315 /* Now let the caller free it */
316 RT_LOCK(rt);
317 RT_REMREF_LOCKED(rt);
318 } else {
319 RT_LOCK(rt);
320 }
321 } else {
322 struct timeval timenow;
323
324 getmicrotime(&timenow);
325 rt->rt_flags |= RTPRF_OURS;
326 rt->rt_rmx.rmx_expire =
327 rt_expiry(rt, timenow.tv_sec, rtq_reallyold);
328 }
329}
330
331struct rtqk_arg {
332 struct radix_node_head *rnh;
333 int draining;
334 int killed;
335 int found;
336 int updating;
337 time_t nextstop;
338};
339
340/*
341 * Get rid of old routes. When draining, this deletes everything, even when
342 * the timeout is not expired yet. When updating, this makes sure that
343 * nothing has a timeout longer than the current value of rtq_reallyold.
344 */
345static int
346in_rtqkill(struct radix_node *rn, void *rock)
347{
348 struct rtqk_arg *ap = rock;
349 struct rtentry *rt = (struct rtentry *)rn;
350 int err;
351 struct timeval timenow;
352
353 getmicrotime(&timenow);
354 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
355
356 RT_LOCK(rt);
357 if (rt->rt_flags & RTPRF_OURS) {
358 ap->found++;
359
360 if (ap->draining || rt->rt_rmx.rmx_expire <= timenow.tv_sec) {
361 if (rt->rt_refcnt > 0)
362 panic("rtqkill route really not free");
363
364 /*
365 * Delete this route since we're done with it;
366 * the route may be freed afterwards, so we
367 * can no longer refer to 'rt' upon returning
368 * from rtrequest(). Safe to drop rt_lock and
369 * use rt_key, rt_gateway since holding rnh_lock
370 * here prevents another thread from calling
371 * rt_setgate() on this route.
372 */
373 RT_UNLOCK(rt);
374 err = rtrequest_locked(RTM_DELETE, rt_key(rt),
375 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
376 if (err) {
377 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
378 } else {
379 ap->killed++;
380 }
381 } else {
382 if (ap->updating &&
383 (unsigned)(rt->rt_rmx.rmx_expire - timenow.tv_sec) >
384 rt_expiry(rt, 0, rtq_reallyold)) {
385 rt->rt_rmx.rmx_expire = rt_expiry(rt,
386 timenow.tv_sec, rtq_reallyold);
387 }
388 ap->nextstop = lmin(ap->nextstop,
389 rt->rt_rmx.rmx_expire);
390 RT_UNLOCK(rt);
391 }
392 } else {
393 RT_UNLOCK(rt);
394 }
395
396 return 0;
397}
398
399static void
400in_rtqtimo_funnel(void *rock)
401{
402 in_rtqtimo(rock);
403
404}
405#define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
406static int rtq_timeout = RTQ_TIMEOUT;
407
408static void
409in_rtqtimo(void *rock)
410{
411 struct radix_node_head *rnh = rock;
412 struct rtqk_arg arg;
413 struct timeval atv;
414 static time_t last_adjusted_timeout = 0;
415 struct timeval timenow;
416
417 lck_mtx_lock(rnh_lock);
418 /* Get the timestamp after we acquire the lock for better accuracy */
419 getmicrotime(&timenow);
420
421 arg.found = arg.killed = 0;
422 arg.rnh = rnh;
423 arg.nextstop = timenow.tv_sec + rtq_timeout;
424 arg.draining = arg.updating = 0;
425 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
426
427 /*
428 * Attempt to be somewhat dynamic about this:
429 * If there are ``too many'' routes sitting around taking up space,
430 * then crank down the timeout, and see if we can't make some more
431 * go away. However, we make sure that we will never adjust more
432 * than once in rtq_timeout seconds, to keep from cranking down too
433 * hard.
434 */
435 if((arg.found - arg.killed > rtq_toomany)
436 && (timenow.tv_sec - last_adjusted_timeout >= rtq_timeout)
437 && rtq_reallyold > rtq_minreallyold) {
438 rtq_reallyold = 2*rtq_reallyold / 3;
439 if(rtq_reallyold < rtq_minreallyold) {
440 rtq_reallyold = rtq_minreallyold;
441 }
442
443 last_adjusted_timeout = timenow.tv_sec;
444#if DIAGNOSTIC
445 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
446 rtq_reallyold);
447#endif
448 arg.found = arg.killed = 0;
449 arg.updating = 1;
450 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
451 }
452
453 atv.tv_usec = 0;
454 atv.tv_sec = arg.nextstop - timenow.tv_sec;
455 lck_mtx_unlock(rnh_lock);
456 timeout(in_rtqtimo_funnel, rock, tvtohz(&atv));
457}
458
459void
460in_rtqdrain(void)
461{
462 struct radix_node_head *rnh = rt_tables[AF_INET];
463 struct rtqk_arg arg;
464 arg.found = arg.killed = 0;
465 arg.rnh = rnh;
466 arg.nextstop = 0;
467 arg.draining = 1;
468 arg.updating = 0;
469 lck_mtx_lock(rnh_lock);
470 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
471 lck_mtx_unlock(rnh_lock);
472}
473
474/*
475 * Initialize our routing tree.
476 */
477int
478in_inithead(void **head, int off)
479{
480 struct radix_node_head *rnh;
481
482#ifdef __APPLE__
483 if (*head)
484 return 1;
485#endif
486
487 if(!rn_inithead(head, off))
488 return 0;
489
490 if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
491 return 1; /* only do this for the real routing table */
492
493 rnh = *head;
494 rnh->rnh_addaddr = in_addroute;
495 rnh->rnh_matchaddr = in_matroute;
496 rnh->rnh_matchaddr_args = in_matroute_args;
497 rnh->rnh_close = in_clsroute;
498 in_rtqtimo(rnh); /* kick off timeout first time */
499 return 1;
500}
501
502\f
503/*
504 * This zaps old routes when the interface goes down or interface
505 * address is deleted. In the latter case, it deletes static routes
506 * that point to this address. If we don't do this, we may end up
507 * using the old address in the future. The ones we always want to
508 * get rid of are things like ARP entries, since the user might down
509 * the interface, walk over to a completely different network, and
510 * plug back in.
511 */
512struct in_ifadown_arg {
513 struct radix_node_head *rnh;
514 struct ifaddr *ifa;
515 int del;
516};
517
518static int
519in_ifadownkill(struct radix_node *rn, void *xap)
520{
521 struct in_ifadown_arg *ap = xap;
522 struct rtentry *rt = (struct rtentry *)rn;
523 int err;
524
525 RT_LOCK(rt);
526 if (rt->rt_ifa == ap->ifa &&
527 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
528 /*
529 * We need to disable the automatic prune that happens
530 * in this case in rtrequest() because it will blow
531 * away the pointers that rn_walktree() needs in order
532 * continue our descent. We will end up deleting all
533 * the routes that rtrequest() would have in any case,
534 * so that behavior is not needed there. Safe to drop
535 * rt_lock and use rt_key, rt_gateway, since holding
536 * rnh_lock here prevents another thread from calling
537 * rt_setgate() on this route.
538 */
539 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
540 RT_UNLOCK(rt);
541 err = rtrequest_locked(RTM_DELETE, rt_key(rt),
542 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
543 if (err) {
544 log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
545 }
546 } else {
547 RT_UNLOCK(rt);
548 }
549 return 0;
550}
551
552int
553in_ifadown(struct ifaddr *ifa, int delete)
554{
555 struct in_ifadown_arg arg;
556 struct radix_node_head *rnh;
557
558 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
559
560 if (ifa->ifa_addr->sa_family != AF_INET)
561 return 1;
562
563 /* trigger route cache reevaluation */
564 if (use_routegenid)
565 routegenid_update();
566
567 arg.rnh = rnh = rt_tables[AF_INET];
568 arg.ifa = ifa;
569 arg.del = delete;
570 rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
571 ifa->ifa_flags &= ~IFA_ROUTE;
572 return 0;
573}