VPN network stat accounting changes.

Properly account for VPN apps that make heavy use of the tun
interface. Prior to this change a VPN app could be incorrectly charged
for more data than it actually used if it sent more traffic through
the tun interface than the underlying interface.

This change excludes VPN app traffic on the tun interface from the
adjustment pool and doesn't redistribute traffic to the VPN app.
Instead all of the redistributed traffic is deducted from the VPN app
which effectively represents any overhead incurred by the VPN app.

BUG: 30557871
Change-Id: I62a75a0c0c0111e052b7903baa9f5d6d94ef57fd
This commit is contained in:
Jeremy Joslin
2016-08-08 16:07:37 -07:00
committed by Lorenzo Colitti
parent 05cca1a4cb
commit 218bfe7a33

View File

@@ -904,7 +904,8 @@ public class NetworkStats implements Parcelable {
if (pool.isEmpty()) {
return true;
}
Entry moved = addTrafficToApplications(tunIface, underlyingIface, tunIfaceTotal, pool);
Entry moved =
addTrafficToApplications(tunUid, tunIface, underlyingIface, tunIfaceTotal, pool);
deductTrafficFromVpnApp(tunUid, underlyingIface, moved);
if (!moved.isEmpty()) {
@@ -919,9 +920,9 @@ public class NetworkStats implements Parcelable {
* Initializes the data used by the migrateTun() method.
*
* This is the first pass iteration which does the following work:
* (1) Adds up all the traffic through tun0.
* (2) Adds up all the traffic through the tunUid's underlyingIface
* (1) Adds up all the traffic through the tunUid's underlyingIface
* (both foreground and background).
* (2) Adds up all the traffic through tun0 excluding traffic from the vpn app itself.
*/
private void tunAdjustmentInit(int tunUid, String tunIface, String underlyingIface,
Entry tunIfaceTotal, Entry underlyingIfaceTotal) {
@@ -941,8 +942,9 @@ public class NetworkStats implements Parcelable {
underlyingIfaceTotal.add(recycle);
}
if (recycle.tag == TAG_NONE && Objects.equals(tunIface, recycle.iface)) {
// Add up all tunIface traffic.
if (recycle.uid != tunUid && recycle.tag == TAG_NONE
&& Objects.equals(tunIface, recycle.iface)) {
// Add up all tunIface traffic excluding traffic from the vpn app itself.
tunIfaceTotal.add(recycle);
}
}
@@ -958,13 +960,15 @@ public class NetworkStats implements Parcelable {
return pool;
}
private Entry addTrafficToApplications(String tunIface, String underlyingIface,
private Entry addTrafficToApplications(int tunUid, String tunIface, String underlyingIface,
Entry tunIfaceTotal, Entry pool) {
Entry moved = new Entry();
Entry tmpEntry = new Entry();
tmpEntry.iface = underlyingIface;
for (int i = 0; i < size; i++) {
if (Objects.equals(iface[i], tunIface)) {
// the vpn app is excluded from the redistribution but all moved traffic will be
// deducted from the vpn app (see deductTrafficFromVpnApp below).
if (Objects.equals(iface[i], tunIface) && uid[i] != tunUid) {
if (tunIfaceTotal.rxBytes > 0) {
tmpEntry.rxBytes = pool.rxBytes * rxBytes[i] / tunIfaceTotal.rxBytes;
} else {