Remove default routing rule & table before we forget table number.

We clear the interface index cache when we remove an interface from a
network, so we must only do this after we remove the default rule so
we still know the table number (calculated from interface index).

bug:16728065
Change-Id: I538673dcba49820a9e21f60407754fae30de02cf
diff --git a/server/PhysicalNetwork.cpp b/server/PhysicalNetwork.cpp
index 62343c4..495a93a 100644
--- a/server/PhysicalNetwork.cpp
+++ b/server/PhysicalNetwork.cpp
@@ -143,16 +143,20 @@
     if (!hasInterface(interface)) {
         return 0;
     }
-    if (int ret = RouteController::removeInterfaceFromPhysicalNetwork(mNetId, interface.c_str(),
-                                                                      mPermission)) {
-        ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId);
-        return ret;
-    }
     if (mIsDefault) {
         if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
             return ret;
         }
     }
+    // This step will flush the interface index from the cache in RouteController so it must be
+    // done last as further requests to the RouteController regarding this interface will fail
+    // to find the interface index in the cache in cases where the interface is already gone
+    // (e.g. bt-pan).
+    if (int ret = RouteController::removeInterfaceFromPhysicalNetwork(mNetId, interface.c_str(),
+                                                                      mPermission)) {
+        ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId);
+        return ret;
+    }
     mInterfaces.erase(interface);
     return 0;
 }