]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/netinet/in_rmx.c
xnu-1486.2.11.tar.gz
[apple/xnu.git] / bsd / netinet / in_rmx.c
index 56d33d251e3d9d17b307492c1d1c1aa0ed138381..c7410a0a357e373d58be6b689da654b924d9e1c2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
 #include <netinet/in.h>
 #include <netinet/in_var.h>
 
+extern int tvtohz(struct timeval *);
 extern int     in_inithead(void **head, int off);
 
 #ifdef __APPLE__
 static void in_rtqtimo(void *rock);
 #endif
 
+static struct radix_node *in_matroute_args(void *, struct radix_node_head *,
+    rn_matchf_t *f, void *);
+
 #define RTPRF_OURS             RTF_PROTO3      /* set on routes we manage */
-extern lck_mtx_t *rt_mtx;
 
 /*
  * Do what we need to do when inserting a route.
@@ -103,13 +106,16 @@ in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
        struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
        struct radix_node *ret;
 
+       lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
+       RT_LOCK_ASSERT_HELD(rt);
+
        /*
         * For IP, all unicast non-host routes are automatically cloning.
         */
-       if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
+       if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
                rt->rt_flags |= RTF_MULTICAST;
 
-       if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
+       if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
                rt->rt_flags |= RTF_PRCLONING;
        }
 
@@ -153,19 +159,28 @@ in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
                 * Find out if it is because of an
                 * ARP entry and delete it if so.
                 */
-               rt2 = rtalloc1_locked((struct sockaddr *)sin, 0,
-                               RTF_CLONING | RTF_PRCLONING);
+               rt2 = rtalloc1_scoped_locked(rt_key(rt), 0,
+                   RTF_CLONING | RTF_PRCLONING, sa_get_ifscope(rt_key(rt)));
                if (rt2) {
-                       if (rt2->rt_flags & RTF_LLINFO &&
-                               rt2->rt_flags & RTF_HOST &&
-                               rt2->rt_gateway &&
-                               rt2->rt_gateway->sa_family == AF_LINK) {
-                               rtrequest_locked(RTM_DELETE,
-                                         (struct sockaddr *)rt_key(rt2),
-                                         rt2->rt_gateway,
-                                         rt_mask(rt2), rt2->rt_flags, 0);
+                       RT_LOCK(rt2);
+                       if ((rt2->rt_flags & RTF_LLINFO) &&
+                           (rt2->rt_flags & RTF_HOST) &&
+                           rt2->rt_gateway != NULL &&
+                           rt2->rt_gateway->sa_family == AF_LINK) {
+                               /*
+                                * Safe to drop rt_lock and use rt_key,
+                                * rt_gateway, since holding rnh_lock here
+                                * prevents another thread from calling
+                                * rt_setgate() on this route.
+                                */
+                               RT_UNLOCK(rt2);
+                               rtrequest_locked(RTM_DELETE, rt_key(rt2),
+                                   rt2->rt_gateway, rt_mask(rt2),
+                                   rt2->rt_flags, 0);
                                ret = rn_addroute(v_arg, n_arg, head,
                                        treenodes);
+                       } else {
+                               RT_UNLOCK(rt2);
                        }
                        rtfree_locked(rt2);
                }
@@ -173,24 +188,50 @@ in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
        return ret;
 }
 
+/*
+ * Validate (unexpire) an expiring AF_INET route.
+ */
+struct radix_node *
+in_validate(struct radix_node *rn)
+{
+       struct rtentry *rt = (struct rtentry *)rn;
+
+       RT_LOCK_ASSERT_HELD(rt);
+
+       /* This is first reference? */
+       if (rt->rt_refcnt == 0 && (rt->rt_flags & RTPRF_OURS)) {
+               rt->rt_flags &= ~RTPRF_OURS;
+               rt->rt_rmx.rmx_expire = 0;
+       }
+       return (rn);
+}
+
+/*
+ * Similar to in_matroute_args except without the leaf-matching parameters.
+ */
+static struct radix_node *
+in_matroute(void *v_arg, struct radix_node_head *head)
+{
+       return (in_matroute_args(v_arg, head, NULL, NULL));
+}
+
 /*
  * This code is the inverse of in_clsroute: on first reference, if we
  * were managing the route, stop doing so and set the expiration timer
  * back off again.
  */
 static struct radix_node *
-in_matroute(void *v_arg, struct radix_node_head *head)
+in_matroute_args(void *v_arg, struct radix_node_head *head,
+    rn_matchf_t *f, void *w)
 {
-       struct radix_node *rn = rn_match(v_arg, head);
-       struct rtentry *rt = (struct rtentry *)rn;
+       struct radix_node *rn = rn_match_args(v_arg, head, f, w);
 
-       if(rt && rt->rt_refcnt == 0) { /* this is first reference */
-               if(rt->rt_flags & RTPRF_OURS) {
-                       rt->rt_flags &= ~RTPRF_OURS;
-                       rt->rt_rmx.rmx_expire = 0;
-               }
+       if (rn != NULL) {
+               RT_LOCK_SPIN((struct rtentry *)rn);
+               in_validate(rn);
+               RT_UNLOCK((struct rtentry *)rn);
        }
-       return rn;
+       return (rn);
 }
 
 static int rtq_reallyold = 60*60;
@@ -228,7 +269,7 @@ SYSCTL_INT(_net_inet_ip, OID_AUTO, check_route_selfref, CTLFLAG_RW,
     &check_routeselfref , 0, "");
 #endif
 
-__private_extern__ int use_routegenid = 1;
+int use_routegenid = 1;
 SYSCTL_INT(_net_inet_ip, OID_AUTO, use_route_genid, CTLFLAG_RW, 
     &use_routegenid , 0, "");
 
@@ -237,35 +278,52 @@ SYSCTL_INT(_net_inet_ip, OID_AUTO, use_route_genid, CTLFLAG_RW,
  * timed out.
  */
 static void
-in_clsroute(struct radix_node *rn, struct radix_node_head *head)
+in_clsroute(struct radix_node *rn, __unused struct radix_node_head *head)
 {
        struct rtentry *rt = (struct rtentry *)rn;
-       struct timeval timenow;
 
-       if(!(rt->rt_flags & RTF_UP))
+       lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
+       RT_LOCK_ASSERT_HELD(rt);
+
+       if (!(rt->rt_flags & RTF_UP))
                return;         /* prophylactic measures */
 
-       if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
+       if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
                return;
 
-       if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
-          != RTF_WASCLONED)
+       if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
                return;
 
        /*
-        * As requested by David Greenman:
-        * If rtq_reallyold is 0, just delete the route without
-        * waiting for a timeout cycle to kill it.
+        * Delete the route immediately if RTF_DELCLONE is set or
+        * if route caching is disabled (rtq_reallyold set to 0).
+        * Otherwise, let it expire and be deleted by in_rtqkill().
         */
-       if(rtq_reallyold != 0) {
+       if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) {
+               /*
+                * Delete the route from the radix tree but since we are
+                * called when the route's reference count is 0, don't
+                * deallocate it until we return from this routine by
+                * telling rtrequest that we're interested in it.
+                * Safe to drop rt_lock and use rt_key, rt_gateway since
+                * holding rnh_lock here prevents another thread from
+                * calling rt_setgate() on this route.
+                */
+               RT_UNLOCK(rt);
+               if (rtrequest_locked(RTM_DELETE, (struct sockaddr *)rt_key(rt),
+                   rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt) == 0) {
+                       /* Now let the caller free it */
+                       RT_LOCK(rt);
+                       RT_REMREF_LOCKED(rt);
+               } else {
+                       RT_LOCK(rt);
+               }
+       } else {
+               struct timeval timenow;
+
                getmicrotime(&timenow);
                rt->rt_flags |= RTPRF_OURS;
                rt->rt_rmx.rmx_expire = timenow.tv_sec + rtq_reallyold;
-       } else {
-               rtrequest_locked(RTM_DELETE,
-                         (struct sockaddr *)rt_key(rt),
-                         rt->rt_gateway, rt_mask(rt),
-                         rt->rt_flags, 0);
        }
 }
 
@@ -292,25 +350,35 @@ in_rtqkill(struct radix_node *rn, void *rock)
        struct timeval timenow;
 
        getmicrotime(&timenow);
-       lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED);
-       if(rt->rt_flags & RTPRF_OURS) {
+       lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
+
+       RT_LOCK(rt);
+       if (rt->rt_flags & RTPRF_OURS) {
                ap->found++;
 
-               if(ap->draining || rt->rt_rmx.rmx_expire <= timenow.tv_sec) {
-                       if(rt->rt_refcnt > 0)
+               if (ap->draining || rt->rt_rmx.rmx_expire <= timenow.tv_sec) {
+                       if (rt->rt_refcnt > 0)
                                panic("rtqkill route really not free");
 
-                       err = rtrequest_locked(RTM_DELETE,
-                                       (struct sockaddr *)rt_key(rt),
-                                       rt->rt_gateway, rt_mask(rt),
-                                       rt->rt_flags, 0);
-                       if(err) {
+                       /*
+                        * Delete this route since we're done with it;
+                        * the route may be freed afterwards, so we
+                        * can no longer refer to 'rt' upon returning
+                        * from rtrequest().  Safe to drop rt_lock and
+                        * use rt_key, rt_gateway since holding rnh_lock
+                        * here prevents another thread from calling
+                        * rt_setgate() on this route.
+                        */
+                       RT_UNLOCK(rt);
+                       err = rtrequest_locked(RTM_DELETE, rt_key(rt),
+                           rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
+                       if (err) {
                                log(LOG_WARNING, "in_rtqkill: error %d\n", err);
                        } else {
                                ap->killed++;
                        }
                } else {
-                       if(ap->updating
+                       if (ap->updating
                           && (rt->rt_rmx.rmx_expire - timenow.tv_sec
                               > rtq_reallyold)) {
                                rt->rt_rmx.rmx_expire = timenow.tv_sec
@@ -318,7 +386,10 @@ in_rtqkill(struct radix_node *rn, void *rock)
                        }
                        ap->nextstop = lmin(ap->nextstop,
                                            rt->rt_rmx.rmx_expire);
+                       RT_UNLOCK(rt);
                }
+       } else {
+               RT_UNLOCK(rt);
        }
 
        return 0;
@@ -342,12 +413,14 @@ in_rtqtimo(void *rock)
        static time_t last_adjusted_timeout = 0;
        struct timeval timenow;
 
+       lck_mtx_lock(rnh_lock);
+       /* Get the timestamp after we acquire the lock for better accuracy */
        getmicrotime(&timenow);
+
        arg.found = arg.killed = 0;
        arg.rnh = rnh;
        arg.nextstop = timenow.tv_sec + rtq_timeout;
        arg.draining = arg.updating = 0;
-       lck_mtx_lock(rt_mtx);
        rnh->rnh_walktree(rnh, in_rtqkill, &arg);
 
        /*
@@ -378,7 +451,7 @@ in_rtqtimo(void *rock)
 
        atv.tv_usec = 0;
        atv.tv_sec = arg.nextstop - timenow.tv_sec;
-       lck_mtx_unlock(rt_mtx);
+       lck_mtx_unlock(rnh_lock);
        timeout(in_rtqtimo_funnel, rock, tvtohz(&atv));
 }
 
@@ -392,9 +465,9 @@ in_rtqdrain(void)
        arg.nextstop = 0;
        arg.draining = 1;
        arg.updating = 0;
-       lck_mtx_lock(rt_mtx);
+       lck_mtx_lock(rnh_lock);
        rnh->rnh_walktree(rnh, in_rtqkill, &arg);
-       lck_mtx_unlock(rt_mtx);
+       lck_mtx_unlock(rnh_lock);
 }
 
 /*
@@ -419,6 +492,7 @@ in_inithead(void **head, int off)
        rnh = *head;
        rnh->rnh_addaddr = in_addroute;
        rnh->rnh_matchaddr = in_matroute;
+       rnh->rnh_matchaddr_args = in_matroute_args;
        rnh->rnh_close = in_clsroute;
        in_rtqtimo(rnh);        /* kick off timeout first time */
        return 1;
@@ -447,6 +521,7 @@ in_ifadownkill(struct radix_node *rn, void *xap)
        struct rtentry *rt = (struct rtentry *)rn;
        int err;
 
+       RT_LOCK(rt);
        if (rt->rt_ifa == ap->ifa &&
            (ap->del || !(rt->rt_flags & RTF_STATIC))) {
                /*
@@ -455,14 +530,20 @@ in_ifadownkill(struct radix_node *rn, void *xap)
                 * away the pointers that rn_walktree() needs in order
                 * continue our descent.  We will end up deleting all
                 * the routes that rtrequest() would have in any case,
-                * so that behavior is not needed there.
+                * so that behavior is not needed there.  Safe to drop
+                * rt_lock and use rt_key, rt_gateway, since holding
+                * rnh_lock here prevents another thread from calling
+                * rt_setgate() on this route.
                 */
                rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
-               err = rtrequest_locked(RTM_DELETE, (struct sockaddr *)rt_key(rt),
-                               rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
+               RT_UNLOCK(rt);
+               err = rtrequest_locked(RTM_DELETE, rt_key(rt),
+                   rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
                if (err) {
                        log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
                }
+       } else {
+               RT_UNLOCK(rt);
        }
        return 0;
 }
@@ -473,11 +554,15 @@ in_ifadown(struct ifaddr *ifa, int delete)
        struct in_ifadown_arg arg;
        struct radix_node_head *rnh;
 
-       lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED);
+       lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
 
        if (ifa->ifa_addr->sa_family != AF_INET)
                return 1;
 
+       /* trigger route cache reevaluation */
+       if (use_routegenid)
+               routegenid_update();
+
        arg.rnh = rnh = rt_tables[AF_INET];
        arg.ifa = ifa;
        arg.del = delete;