Support adding multiple tethering upstreams.

Currently, when the first tethering interface pair is added, we
add MASQUERADE rules for the upstream and IPv6 counting rules.
Continue to add IPv6 counting rules when the first interface pair
is added, but change the code to add MASQUERADE rules every time
a new upstream is added.

This will allow us to support adding, say, v4-rmnet_data0 as an
upstream once we're already using rmnet_data0 as an upstream.

Bug: 38218697
Bug: 64382985
Bug: 64976379
Bug: 64995262
Bug: 64380515
Test: netd_unit_test passes, manual: IPv4 or 464xlat tethering works
(cherry-pick of aosp I84078b2241214b3b993ccaf2e590406018df00ff)
Merged-In: I7747c81c62cc9ce26768849873a76a7e7478ea66
Merged-In: If80af6b3225316c47f11ab39e7544cfb7e095d37

Change-Id: I984ccf335fbc0c630fb21e0edc752a3ebbcd7706
diff --git a/server/TetherController.h b/server/TetherController.h
index df43a7b..a34b5b7 100644
--- a/server/TetherController.h
+++ b/server/TetherController.h
@@ -33,8 +33,17 @@
 
 class TetherController {
 private:
+    struct ForwardingDownstream {
+        std::string iface;
+        bool active;
+    };
+
     std::list<std::string> mInterfaces;
 
+    // Map upstream iface -> downstream iface. A pair is in the map if forwarding was enabled at
+    // some point since the controller was initialized.
+    std::multimap<std::string, ForwardingDownstream> mFwdIfaces;
+
     // NetId to use for forwarded DNS queries. This may not be the default
     // network, e.g., in the case where we are tethering to a DUN APN.
     unsigned               mDnsNetId;
@@ -48,10 +57,6 @@
     TetherController();
     virtual ~TetherController();
 
-    // List of strings of interface pairs. Public because it's used by CommandListener.
-    // TODO: merge with mInterfaces, and make private.
-    std::list<std::string> ifacePairList;
-
     bool enableForwarding(const char* requester);
     bool disableForwarding(const char* requester);
     size_t forwardingRequestCount();
@@ -126,10 +131,17 @@
 private:
     bool setIpFwdEnabled();
 
-    int natCount;
-
+    int setupIPv6CountersChain();
     static std::string makeTetherCountingRule(const char *if1, const char *if2);
-    bool checkTetherCountingRuleExist(const std::string& pair_name);
+    ForwardingDownstream* findForwardingDownstream(const std::string& intIface,
+        const std::string& extIface);
+    void addForwardingPair(const std::string& intIface, const std::string& extIface);
+    void markForwardingPairDisabled(const std::string& intIface, const std::string& extIface);
+
+    bool isForwardingPairEnabled(const std::string& intIface, const std::string& extIface);
+    bool isAnyForwardingEnabledOnUpstream(const std::string& extIface);
+    bool isAnyForwardingPairEnabled();
+    bool tetherCountingRuleExists(const std::string& iface1, const std::string& iface2);
 
     int setDefaults();
     int setForwardRules(bool set, const char *intIface, const char *extIface);