Android12上实现双以太网卡共存同时访问外网

具体实现如下:

修改main 表优先级到9999, 作用:eth0 eth1 访问

不去teardown 低分数网线

diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
index 418e9e3..72d15d5 100644
--- a/service/src/com/android/server/ConnectivityService.java
+++ b/service/src/com/android/server/ConnectivityService.java
@@ -291,8 +291,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
     private static final String REQUEST_ARG = "requests";
 
     private static final boolean DBG = true;
-    private static final boolean DDBG = Log.isLoggable(TAG, Log.DEBUG);
-    private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE);
+    private static final boolean DDBG = true;    //Log.isLoggable(TAG, Log.DEBUG);
+    private static final boolean VDBG = true;    //Log.isLoggable(TAG, Log.VERBOSE);
 
     private static final boolean LOGD_BLOCKED_NETWORKINFO = true;
 
@@ -1587,7 +1587,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
         handleAlwaysOnNetworkRequest(mDefaultMobileDataRequest,
                 ConnectivitySettingsManager.MOBILE_DATA_ALWAYS_ON, true /* defaultValue */);
         handleAlwaysOnNetworkRequest(mDefaultWifiRequest,
-                ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED, false /* defaultValue */);
+                ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED, true/* defaultValue */);
         final boolean vehicleAlwaysRequested = mResources.get().getBoolean(
                 R.bool.config_vehicleInternalNetworkAlwaysRequested);
         handleAlwaysOnNetworkRequest(mDefaultVehicleRequest, vehicleAlwaysRequested);
@@ -3797,6 +3797,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
         // 2. If the network was inactive and there are now requests, unset inactive.
         // 3. If this network is unneeded (which implies it is not lingering), and there is at least
         //    one lingered request, set inactive.
+        /****
         nai.updateInactivityTimer();
         if (nai.isInactive() && nai.numForegroundNetworkRequests() > 0) {
             if (DBG) log("Unsetting inactive " + nai.toShortString());
@@ -3811,6 +3812,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
             logNetworkEvent(nai, NetworkEvent.NETWORK_LINGER);
             return true;
         }
+        ****/
+        loge("wade skip updateInactivityState.");
         return false;
     }
 
@@ -6853,7 +6856,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
             return;
         }
         mNetworkOffers.add(noi);
-        issueNetworkNeeds(noi);
+        //issueNetworkNeeds(noi);
     }
 
     private void handleUnregisterNetworkOffer(@NonNull final NetworkOfferInfo noi) {
@@ -7780,6 +7783,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
     }
 
     private void teardownUnneededNetwork(NetworkAgentInfo nai) {
+        /**
         if (nai.numRequestNetworkRequests() != 0) {
             for (int i = 0; i < nai.numNetworkRequests(); i++) {
                 NetworkRequest nr = nai.requestAt(i);
@@ -7790,6 +7794,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
             }
         }
         nai.disconnect();
+        **/
+        loge("wade skip teardownUnneededNetwork.");
     }
 
     private void handleLingerComplete(NetworkAgentInfo oldNetwork) {
@@ -8113,6 +8119,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
                 // Remove default networking if disallowed for managed default requests.
                 bestNetwork = mNoServiceNetwork;
             }
+            loge("wade bestNetwork = " + bestNetwork + "bestRequest = " + bestRequest);
             if (nri.getSatisfier() != bestNetwork) {
                 // bestNetwork may be null if no network can satisfy this request.
                 changes.addRequestReassignment(new NetworkReassignment.RequestReassignment(
@@ -8150,7 +8157,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
             log(changes.toString()); // Shorter form, only one line of log
         }
         applyNetworkReassignment(changes, now);
-        issueNetworkNeeds();
+        //issueNetworkNeeds();
     }
 
     private void applyNetworkReassignment(@NonNull final NetworkReassignment changes,
diff --git a/server/RouteController.cpp b/server/RouteController.cpp
index ba305e6..d38aec5 100644
--- a/server/RouteController.cpp
+++ b/server/RouteController.cpp
@@ -721,6 +721,18 @@ int RouteController::configureDummyNetwork() {
     return 0;
 }
 
+// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
+// rule, but with a lower priority. We will never create routes in the main table; it should only be
+// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
+// This is necessary, for example, when adding a route through a directly-connected gateway: in
+// order to add the route, there must already be a directly-connected route that covers the gateway.
+[[nodiscard]] static int addDirectlyConnectedRule() {
+
+    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
+                        0, 0/*, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT*/);
+}
+
+
 // Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
 // relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
 // behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
@@ -1160,6 +1172,11 @@ int RouteController::Init(unsigned localNetId) {
     if (int ret = addUnreachableRule()) {
         return ret;
     }
+
+    if (int ret = addDirectlyConnectedRule()) {
+       return ret;
+    }
+
     // Don't complain if we can't add the dummy network, since not all devices support it.
     configureDummyNetwork();
 
diff --git a/server/RouteController.h b/server/RouteController.h
index 38d2d62..43ee148 100644
--- a/server/RouteController.h
+++ b/server/RouteController.h
@@ -30,6 +30,7 @@
 namespace android::net {
 
 // clang-format off
+const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED  = 9999;
 const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM     = 10000;
 const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF        = 11000;
 const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL     = 12000;

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 20:56:03       100 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 20:56:03       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 20:56:03       90 阅读
  4. Python语言-面向对象

    2024-07-10 20:56:03       98 阅读

热门阅读

  1. c语言实战-极简扫雷

    2024-07-10 20:56:03       30 阅读
  2. 从零到一:构建股票预测模型的Python实战教程

    2024-07-10 20:56:03       25 阅读
  3. SpringBoot | 面试题

    2024-07-10 20:56:03       28 阅读
  4. Shell学习——Shell printf命令

    2024-07-10 20:56:03       26 阅读
  5. Linux实现CPU物理隔离

    2024-07-10 20:56:03       25 阅读
  6. Redis 中的跳表(Skip List)

    2024-07-10 20:56:03       30 阅读
  7. 路由器是什么?

    2024-07-10 20:56:03       26 阅读