Fix WiFi-Direct and Tethering.

A LocalNetwork object now always exists in the NetworkController, with a fixed
NetId that's guaranteed not to collide with NetIds created by the framework.

When routes are added on an interface tracked by the LocalNetwork, they are
added to a fixed "local_network" table.

When NAT is enabled, we add a special "iif -> oif" tethering rule.

Bug: 15413694
Bug: 15413741

Change-Id: I36effc438d5ac193a77174493bf196cb68a5b97a
diff --git a/server/NetworkController.cpp b/server/NetworkController.cpp
index 44eb2ef..90b7682 100644
--- a/server/NetworkController.cpp
+++ b/server/NetworkController.cpp
@@ -51,6 +51,7 @@
 }  // namespace
 
 NetworkController::NetworkController() : mDefaultNetId(NETID_UNSET) {
+    mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
 }
 
 unsigned NetworkController::getDefaultNetwork() const {
@@ -117,29 +118,6 @@
     return network && network->getType() == Network::VIRTUAL;
 }
 
-unsigned NetworkController::getNetIdForLocalNetwork() const {
-    return MIN_NET_ID - 1;
-}
-
-int NetworkController::createLocalNetwork(unsigned netId) {
-    // TODO: Enable this check after removing the getNetIdForLocalNetwork() hack.
-    if (false) {
-        if (netId < MIN_NET_ID || netId > MAX_NET_ID) {
-            ALOGE("invalid netId %u", netId);
-            return -EINVAL;
-        }
-    }
-
-    if (isValidNetwork(netId)) {
-        ALOGE("duplicate netId %u", netId);
-        return -EEXIST;
-    }
-
-    android::RWLock::AutoWLock lock(mRWLock);
-    mNetworks[netId] = new LocalNetwork(netId);
-    return 0;
-}
-
 int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
     if (netId < MIN_NET_ID || netId > MAX_NET_ID) {
         ALOGE("invalid netId %u", netId);
@@ -180,7 +158,7 @@
 }
 
 int NetworkController::destroyNetwork(unsigned netId) {
-    if (!isValidNetwork(netId)) {
+    if (netId == LOCAL_NET_ID || !isValidNetwork(netId)) {
         ALOGE("invalid netId %u", netId);
         return -EINVAL;
     }
@@ -377,7 +355,9 @@
     }
 
     RouteController::TableType tableType;
-    if (legacy) {
+    if (netId == LOCAL_NET_ID) {
+        tableType = RouteController::LOCAL_NETWORK;
+    } else if (legacy) {
         if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
             tableType = RouteController::LEGACY_SYSTEM;
         } else {