TetherController - bpf offload should only be enabled once per upstream

Bug: 153437886
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If8588c0ebc2893ccea89db3c68e9a08fbdec06fc
Merged-In: If8588c0ebc2893ccea89db3c68e9a08fbdec06fc
diff --git a/server/TetherController.cpp b/server/TetherController.cpp
index 144d24f..dd8622b 100644
--- a/server/TetherController.cpp
+++ b/server/TetherController.cpp
@@ -606,7 +606,8 @@
     }
 
     // add this if we are the first enabled nat for this upstream
-    if (!isAnyForwardingEnabledOnUpstream(extIface)) {
+    bool firstDownstreamForThisUpstream = !isAnyForwardingEnabledOnUpstream(extIface);
+    if (firstDownstreamForThisUpstream) {
         std::vector<std::string> v4Cmds = {
             "*nat",
             StringPrintf("-A %s -o %s -j MASQUERADE", LOCAL_NAT_POSTROUTING, extIface),
@@ -632,7 +633,7 @@
         return -ENODEV;
     }
 
-    maybeStartBpf(extIface);
+    if (firstDownstreamForThisUpstream) maybeStartBpf(extIface);
     return 0;
 }
 
@@ -816,11 +817,8 @@
     }
 
     setForwardRules(false, intIface, extIface);
-    if (!isAnyForwardingPairEnabled()) {
-        setDefaults();
-    }
-
-    maybeStopBpf(extIface);
+    if (!isAnyForwardingEnabledOnUpstream(extIface)) maybeStopBpf(extIface);
+    if (!isAnyForwardingPairEnabled()) setDefaults();
     return 0;
 }