From 969d82bc82db2ab4912ae9fdbb85066a6d403d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Wed, 19 Mar 2025 16:33:05 -0700 Subject: [PATCH] LocalNetAccessKey - improve toString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit while at it, improve dump to clarify default is true meaning global, even though you could assume 0/false is the default, and that true means local... $ adb shell dumpsys connectivity trafficcontroller Change-Id: Ifcebc18c3804bc1d5d705752021bb4643ed58eb5 --- .../module/util/bpf/LocalNetAccessKey.java | 48 ++++++++++++++++--- .../src/com/android/server/BpfNetMaps.java | 2 +- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/common/src/com/android/net/module/util/bpf/LocalNetAccessKey.java b/common/src/com/android/net/module/util/bpf/LocalNetAccessKey.java index 95265b9744..48e8b067f3 100644 --- a/common/src/com/android/net/module/util/bpf/LocalNetAccessKey.java +++ b/common/src/com/android/net/module/util/bpf/LocalNetAccessKey.java @@ -61,12 +61,46 @@ public class LocalNetAccessKey extends Struct { @Override public String toString() { - return "LocalNetAccessKey{" - + "lpmBitlen=" + lpmBitlen - + ", ifIndex=" + ifIndex - + ", remoteAddress=" + remoteAddress - + ", protocol=" + protocol - + ", remotePort=" + remotePort - + "}"; + String s = "LocalNetAccessKey{lpmBitlen=" + lpmBitlen; + + long bits = lpmBitlen; + + // u32 ifIndex + if (bits <= 0 && ifIndex != 0) s += " ??"; + if (bits > 0 || ifIndex != 0) s += " ifIndex=" + ifIndex; + if (bits > 0 && bits < 32) s += "/" + bits + "[LE]"; + bits -= 32; + + // u128 remoteAddress + if (bits <= 0 && !remoteAddress.isAnyLocalAddress()) s += " ??"; + if (bits > 0 || !remoteAddress.isAnyLocalAddress()) { + s += " remoteAddress="; + String ip = remoteAddress.toString(); + if (ip.startsWith("/::ffff:")) { // technically wrong IPv4-mapped IPv6 address detection + s += ip.substring(8); + if (bits >= 96 && bits < 128) s += "/" + (bits - 96); + } else if (ip.startsWith("/")) { + s += ip.substring(1); + if (bits >= 0 && bits < 128) s += "/" + bits; + } else { // WTF, includes a hostname or what? + s += ip; + } + } + bits -= 128; + + // u16 protocol + if (bits <= 0 && protocol != 0) s += " ??"; + if (bits > 0 || protocol != 0) s += " protocol=" + protocol; + if (bits > 0 && bits < 16) s += "/" + bits + "[LE16]"; + bits -= 16; + + // be16 remotePort + if (bits <= 0 && remotePort != 0) s += " ??"; + if (bits > 0 || remotePort != 0) s += " remotePort=" + remotePort; + if (bits > 0 && bits < 16) s += "/" + bits + "[BE16]"; + bits -= 16; + + s += "}"; + return s; } } diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java index 9bd407dfd2..523ffee216 100644 --- a/service/src/com/android/server/BpfNetMaps.java +++ b/service/src/com/android/server/BpfNetMaps.java @@ -1327,7 +1327,7 @@ public class BpfNetMaps { + value.iif1 + "(" + mDeps.getIfName(value.iif1) + "), " + value.iif2 + "(" + mDeps.getIfName(value.iif2) + ")"); if (sLocalNetBlockedUidMap != null) { - BpfDump.dumpMap(sLocalNetAccessMap, pw, "sLocalNetAccessMap", + BpfDump.dumpMap(sLocalNetAccessMap, pw, "sLocalNetAccessMap (default is true meaning global)", (key, value) -> "" + key + ": " + value.val); } if (sLocalNetBlockedUidMap != null) {