Hack and ship: NetworkStats edition.

Some devices use clatd for catching raw IPv4 traffic when running on
a pure-IPv6 carrier network.  In those situations, the per-UID
stats are accounted against the clat iface, so framework users need
to combine both the "base" and "stacked" iface usage together.

This also means that policy rules (like restricting background data
or battery saver) need to apply to the stacked ifaces.

Finally, we need to massage stats data slightly:

-- Currently xt_qtaguid double-counts the clatd traffic *leaving*
the device; both against the original UID on the clat iface, and
against UID 0 on the final egress interface.

-- All clatd traffic *arriving* at the device is missing the extra
IPv6 packet header overhead when accounted against the final UID.

Bug: 12249687, 15459248, 16296564
Change-Id: I0ee59d96831f52782de7a980e4cce9b061902fff
diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java
index 8be5cf8..47b74ab 100644
--- a/core/java/android/net/LinkProperties.java
+++ b/core/java/android/net/LinkProperties.java
@@ -16,6 +16,7 @@
 
 package android.net;
 
+import android.annotation.NonNull;
 import android.net.ProxyInfo;
 import android.os.Parcelable;
 import android.os.Parcel;
@@ -24,7 +25,6 @@
 import java.net.InetAddress;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
-
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -480,10 +480,13 @@
      * Returns all the links stacked on top of this link.
      * @hide
      */
-    public List<LinkProperties> getStackedLinks() {
+    public @NonNull List<LinkProperties> getStackedLinks() {
+        if (mStackedLinks.isEmpty()) {
+            return Collections.EMPTY_LIST;
+        }
         List<LinkProperties> stacked = new ArrayList<LinkProperties>();
         for (LinkProperties link : mStackedLinks.values()) {
-          stacked.add(new LinkProperties(link));
+            stacked.add(new LinkProperties(link));
         }
         return Collections.unmodifiableList(stacked);
     }
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index f9baccd..08cb791 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -119,7 +119,9 @@
 
 import com.android.internal.R;
 import com.android.internal.annotations.GuardedBy;
+import com.android.internal.app.IBatteryStats;
 import com.android.internal.net.LegacyVpnInfo;
+import com.android.internal.net.NetworkStatsFactory;
 import com.android.internal.net.VpnConfig;
 import com.android.internal.net.VpnProfile;
 import com.android.internal.telephony.DctConstants;
@@ -4476,12 +4478,23 @@
                 // TODO - read the tcp buffer size config string from somewhere
                 // updateNetworkSettings();
             }
-            // notify battery stats service about this network
+
+            // Notify battery stats service about this network, both the normal
+            // interface and any stacked links.
             try {
-                BatteryStatsService.getService().noteNetworkInterfaceType(
-                        newNetwork.linkProperties.getInterfaceName(),
-                        newNetwork.networkInfo.getType());
-            } catch (RemoteException e) { }
+                final IBatteryStats bs = BatteryStatsService.getService();
+                final int type = newNetwork.networkInfo.getType();
+
+                final String baseIface = newNetwork.linkProperties.getInterfaceName();
+                bs.noteNetworkInterfaceType(baseIface, type);
+                for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) {
+                    final String stackedIface = stacked.getInterfaceName();
+                    bs.noteNetworkInterfaceType(stackedIface, type);
+                    NetworkStatsFactory.noteStackedIface(stackedIface, baseIface);
+                }
+            } catch (RemoteException ignored) {
+            }
+
             notifyNetworkCallbacks(newNetwork, ConnectivityManager.CALLBACK_AVAILABLE);
         } else {
             if (DBG && newNetwork.networkRequests.size() != 0) {