From 3c228f4923c6220b0e01537c9a4dddc37864317a Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Mon, 15 Mar 2021 17:35:09 +0900 Subject: [PATCH] Do not open BPF maps when running on R. Opening the maps on R devices is unnecessary (because no code uses them) and unsafe (because the maps might actually be in a different format than expected by the module). Always return null when opening the maps, so the code will safely fall back to using netd for anything. This only affects dump(). The production code never opens the maps except via the shim, and only the S shim opens the maps. Also check isAtLeastS in dumpCounters, because it opens the map directly. Bug: 177884581 Test: builds, boots, "dumpsys tethering" shows expected output Change-Id: I5884490a5cc40fc529a12100ae5baaeae6a18f30 --- .../networkstack/tethering/BpfCoordinator.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index 8df3045cdd..0f777d56bc 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java @@ -261,6 +261,7 @@ public class BpfCoordinator { /** Get downstream4 BPF map. */ @Nullable public BpfMap getBpfDownstream4Map() { + if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_DOWNSTREAM4_MAP_PATH, BpfMap.BPF_F_RDWR, Tether4Key.class, Tether4Value.class); @@ -272,6 +273,7 @@ public class BpfCoordinator { /** Get upstream4 BPF map. */ @Nullable public BpfMap getBpfUpstream4Map() { + if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_UPSTREAM4_MAP_PATH, BpfMap.BPF_F_RDWR, Tether4Key.class, Tether4Value.class); @@ -283,6 +285,7 @@ public class BpfCoordinator { /** Get downstream6 BPF map. */ @Nullable public BpfMap getBpfDownstream6Map() { + if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR, TetherDownstream6Key.class, Tether6Value.class); @@ -294,6 +297,7 @@ public class BpfCoordinator { /** Get upstream6 BPF map. */ @Nullable public BpfMap getBpfUpstream6Map() { + if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_UPSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR, TetherUpstream6Key.class, Tether6Value.class); @@ -305,6 +309,7 @@ public class BpfCoordinator { /** Get stats BPF map. */ @Nullable public BpfMap getBpfStatsMap() { + if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_STATS_MAP_PATH, BpfMap.BPF_F_RDWR, TetherStatsKey.class, TetherStatsValue.class); @@ -316,6 +321,7 @@ public class BpfCoordinator { /** Get limit BPF map. */ @Nullable public BpfMap getBpfLimitMap() { + if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_LIMIT_MAP_PATH, BpfMap.BPF_F_RDWR, TetherLimitKey.class, TetherLimitValue.class); @@ -828,7 +834,7 @@ public class BpfCoordinator { } map.forEach((k, v) -> pw.println(ipv6UpstreamRuletoString(k, v))); } catch (ErrnoException e) { - pw.println("Error dumping IPv4 map: " + e); + pw.println("Error dumping IPv6 upstream map: " + e); } } @@ -875,6 +881,10 @@ public class BpfCoordinator { } private void dumpCounters(@NonNull IndentingPrintWriter pw) { + if (!mDeps.isAtLeastS()) { + pw.println("No counter support"); + return; + } try (BpfMap map = new BpfMap<>(TETHER_ERROR_MAP_PATH, BpfMap.BPF_F_RDONLY, U32Struct.class, U32Struct.class)) {