Original commit: https://github.com/DerpFest-11/packages_modules_NetworkStack/commit/22fd53a977eeaf4e36be7bf6358ecf2c2737fa5e From 22fd53a977eeaf4e36be7bf6358ecf2c2737fa5e Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Wed, 14 Oct 2020 16:08:13 +0100 Subject: [PATCH] TcpSocketTracker: Opt-out for TCP info parsing on legacy kernels This feature requires netlink features that are unsupported on kernel versions lower than 4.4, therefore opt-out for it in such cases and avoid constantly crashing TcpSocketTracker. Change-Id: I128ed1d0dcb6ca07eef1179f1ef036ccaad64e52 --- .../netlink/TcpSocketTracker.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/modules/NetworkStack/src/com/android/networkstack/netlink/TcpSocketTracker.java b/packages/modules/NetworkStack/src/com/android/networkstack/netlink/TcpSocketTracker.java index ef33f13..f39518f 100644 --- a/packages/modules/NetworkStack/src/com/android/networkstack/netlink/TcpSocketTracker.java +++ b/packages/modules/NetworkStack/src/com/android/networkstack/netlink/TcpSocketTracker.java @@ -57,8 +57,10 @@ import android.system.ErrnoException; import android.system.Os; import android.system.StructTimeval; +import android.text.TextUtils; import android.util.Log; import android.util.LongSparseArray; +import android.util.Pair; import android.util.SparseArray; import androidx.annotation.NonNull; @@ -77,6 +79,8 @@ import java.util.ArrayList; import java.util.Base64; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Class for NetworkStack to send a SockDiag request and parse the returned tcp info. @@ -431,6 +435,35 @@ private void log(final String str) { if (DBG) Log.d(TAG, str); } + private static boolean isTcpInfoParsingSupportedByKernel() { + final String kVersionString = Os.uname().release; + return compareMajorMinorVersion(kVersionString, "4.4") >= 0; + } + + private static int compareMajorMinorVersion(final String s1, final String s2) { + final Pair v1 = getVersionFromString(s1); + final Pair v2 = getVersionFromString(s2); + + if (v1.first == v2.first) { + return Integer.compare(v1.second, v2.second); + } else { + return Integer.compare(v1.first, v2.first); + } + } + + private static Pair getVersionFromString(String version) { + // Only gets major and minor number of the version string. + final Pattern versionPattern = Pattern.compile("^(\\d+)(\\.(\\d+))?.*"); + final Matcher m = versionPattern.matcher(version); + if (m.matches()) { + final int major = Integer.parseInt(m.group(1)); + final int minor = TextUtils.isEmpty(m.group(3)) ? 0 : Integer.parseInt(m.group(3)); + return new Pair<>(major, minor); + } else { + return new Pair<>(0, 0); + } + } + /** * Corresponds to {@code struct rtattr} from bionic/libc/kernel/uapi/linux/rtnetlink.h * @@ -578,7 +611,8 @@ public int getDeviceConfigPropertyInt(@NonNull final String namespace, public boolean isTcpInfoParsingSupported() { // Request tcp info from NetworkStack directly needs extra SELinux permission added // after Q release. - return ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q); + return ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q) && + isTcpInfoParsingSupportedByKernel(); } /**