Merge "Fix DevSdkIgnoreRule for Q-" into main
diff --git a/staticlibs/Android.bp b/staticlibs/Android.bp
index e1b5601..d3e68b7 100644
--- a/staticlibs/Android.bp
+++ b/staticlibs/Android.bp
@@ -41,6 +41,7 @@
"device/com/android/net/module/util/PacketReader.java",
"device/com/android/net/module/util/SharedLog.java",
"device/com/android/net/module/util/SocketUtils.java",
+ "device/com/android/net/module/util/FeatureVersions.java",
// This library is used by system modules, for which the system health impact of Kotlin
// has not yet been evaluated. Annotations may need jarjar'ing.
// "src_devicecommon/**/*.kt",
diff --git a/staticlibs/device/com/android/net/module/util/DeviceConfigUtils.java b/staticlibs/device/com/android/net/module/util/DeviceConfigUtils.java
index 138c1e5..bea227d 100644
--- a/staticlibs/device/com/android/net/module/util/DeviceConfigUtils.java
+++ b/staticlibs/device/com/android/net/module/util/DeviceConfigUtils.java
@@ -17,6 +17,12 @@
package com.android.net.module.util;
import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
+import static android.provider.DeviceConfig.NAMESPACE_TETHERING;
+
+import static com.android.net.module.util.FeatureVersions.CONNECTIVITY_MODULE_ID;
+import static com.android.net.module.util.FeatureVersions.MODULE_MASK;
+import static com.android.net.module.util.FeatureVersions.NETWORK_STACK_MODULE_ID;
+import static com.android.net.module.util.FeatureVersions.VERSION_MASK;
import android.content.Context;
import android.content.Intent;
@@ -53,12 +59,14 @@
"com.android.server.connectivity.intent.action.SERVICE_CONNECTIVITY_RESOURCES_APK";
private static final String CONNECTIVITY_RES_PKG_DIR = "/apex/" + TETHERING_MODULE_NAME + "/";
- private static final long DEFAULT_PACKAGE_VERSION = 1000;
+ @VisibleForTesting
+ public static final long DEFAULT_PACKAGE_VERSION = 1000;
@VisibleForTesting
public static void resetPackageVersionCacheForTest() {
sPackageVersion = -1;
sModuleVersion = -1;
+ sNetworkStackModuleVersion = -1;
}
private static volatile long sPackageVersion = -1;
@@ -224,14 +232,7 @@
// If that fails retry by appending "go.tethering" instead
private static long resolveTetheringModuleVersion(@NonNull Context context)
throws PackageManager.NameNotFoundException {
- final String connResourcesPackage = getConnectivityResourcesPackageName(context);
- final int pkgPrefixLen = connResourcesPackage.indexOf("connectivity");
- if (pkgPrefixLen < 0) {
- throw new IllegalStateException(
- "Invalid connectivity resources package: " + connResourcesPackage);
- }
-
- final String pkgPrefix = connResourcesPackage.substring(0, pkgPrefixLen);
+ final String pkgPrefix = resolvePkgPrefix(context);
final PackageManager packageManager = context.getPackageManager();
try {
return packageManager.getPackageInfo(pkgPrefix + "tethering",
@@ -245,6 +246,17 @@
PackageManager.MATCH_APEX).getLongVersionCode();
}
+ private static String resolvePkgPrefix(Context context) {
+ final String connResourcesPackage = getConnectivityResourcesPackageName(context);
+ final int pkgPrefixLen = connResourcesPackage.indexOf("connectivity");
+ if (pkgPrefixLen < 0) {
+ throw new IllegalStateException(
+ "Invalid connectivity resources package: " + connResourcesPackage);
+ }
+
+ return connResourcesPackage.substring(0, pkgPrefixLen);
+ }
+
private static volatile long sModuleVersion = -1;
private static long getTetheringModuleVersion(@NonNull Context context) {
if (sModuleVersion >= 0) return sModuleVersion;
@@ -260,6 +272,80 @@
return sModuleVersion;
}
+ private static volatile long sNetworkStackModuleVersion = -1;
+
+ /**
+ * Get networkstack module version.
+ */
+ @VisibleForTesting
+ static long getNetworkStackModuleVersion(@NonNull Context context) {
+ if (sNetworkStackModuleVersion >= 0) return sNetworkStackModuleVersion;
+
+ try {
+ sNetworkStackModuleVersion = resolveNetworkStackModuleVersion(context);
+ } catch (PackageManager.NameNotFoundException e) {
+ Log.wtf(TAG, "Failed to resolve networkstack module version: " + e);
+ return DEFAULT_PACKAGE_VERSION;
+ }
+ return sNetworkStackModuleVersion;
+ }
+
+ private static long resolveNetworkStackModuleVersion(@NonNull Context context)
+ throws PackageManager.NameNotFoundException {
+ // TODO(b/293975546): Strictly speaking this is the prefix for connectivity and not
+ // network stack. In practice, it's the same. Read the prefix from network stack instead.
+ final String pkgPrefix = resolvePkgPrefix(context);
+ final PackageManager packageManager = context.getPackageManager();
+ try {
+ return packageManager.getPackageInfo(pkgPrefix + "networkstack",
+ PackageManager.MATCH_SYSTEM_ONLY).getLongVersionCode();
+ } catch (PackageManager.NameNotFoundException e) {
+ Log.d(TAG, "Device is using go or non-mainline modules");
+ // fall through
+ }
+
+ return packageManager.getPackageInfo(pkgPrefix + "go.networkstack",
+ PackageManager.MATCH_ALL).getLongVersionCode();
+ }
+
+ /**
+ * Check whether one specific feature is supported from the feature Id. The feature Id is
+ * composed by a module package Id and version Id from {@link FeatureVersions}.
+ *
+ * This is useful when a feature required minimal module version supported and cannot function
+ * well with a standalone newer module.
+ * @param context The global context information about an app environment.
+ * @param featureId The feature id that contains required module id and minimal module version
+ * @return true if this feature is supported, or false if not supported.
+ **/
+ public static boolean isFeatureSupported(@NonNull Context context, long featureId) {
+ final long moduleVersion;
+ final long moduleId = featureId & MODULE_MASK;
+ if (moduleId == CONNECTIVITY_MODULE_ID) {
+ moduleVersion = getTetheringModuleVersion(context);
+ } else if (moduleId == NETWORK_STACK_MODULE_ID) {
+ moduleVersion = getNetworkStackModuleVersion(context);
+ } else {
+ throw new IllegalArgumentException("Unknown module " + moduleId);
+ }
+ // Support by default if no module version is available.
+ return moduleVersion == DEFAULT_PACKAGE_VERSION
+ || moduleVersion >= (featureId & VERSION_MASK);
+ }
+
+ /**
+ * Check whether one specific experimental feature in tethering module from {@link DeviceConfig}
+ * is disabled by setting a non-zero value in the property.
+ *
+ * @param name The name of the property to look up.
+ * @return true if this feature is force disabled, or false if not disabled.
+ */
+ public static boolean isTetheringFeatureForceDisabled(String name) {
+ final int propertyVersion = getDeviceConfigPropertyInt(NAMESPACE_TETHERING, name,
+ 0 /* default value */);
+ return propertyVersion != 0;
+ }
+
/**
* Gets boolean config from resources.
*/
diff --git a/staticlibs/device/com/android/net/module/util/FeatureVersions.java b/staticlibs/device/com/android/net/module/util/FeatureVersions.java
new file mode 100644
index 0000000..4986a58
--- /dev/null
+++ b/staticlibs/device/com/android/net/module/util/FeatureVersions.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.net.module.util;
+
+/**
+ * Class to centralize feature version control that requires a specific module or a specific
+ * module version.
+ * @hide
+ */
+public class FeatureVersions {
+ public static final long MODULE_MASK = 0xFF00_000000000L;
+ public static final long VERSION_MASK = 0x0000_FFFFFFFFFL;
+ public static final long CONNECTIVITY_MODULE_ID = 0x0100_000000000L;
+ public static final long NETWORK_STACK_MODULE_ID = 0x0200_000000000L;
+ // CLAT_ADDRESS_TRANSLATE is a feature of the network stack, which doesn't throw when system
+ // try to add a NAT-T keepalive packet filter with v6 address, introduced in version
+ // M-2023-Sept on July 3rd, 2023.
+ public static final long FEATURE_CLAT_ADDRESS_TRANSLATE =
+ NETWORK_STACK_MODULE_ID + 340900000L;
+}
diff --git a/staticlibs/device/com/android/net/module/util/SharedLog.java b/staticlibs/device/com/android/net/module/util/SharedLog.java
index 17b061e..6b12c80 100644
--- a/staticlibs/device/com/android/net/module/util/SharedLog.java
+++ b/staticlibs/device/com/android/net/module/util/SharedLog.java
@@ -46,6 +46,8 @@
ERROR,
MARK,
WARN,
+ VERBOSE,
+ TERRIBLE,
}
private final LocalLog mLocalLog;
@@ -159,6 +161,41 @@
Log.w(mTag, record(Category.WARN, msg));
}
+ /**
+ * Log a verbose message.
+ *
+ * <p>The log entry will be also added to the system log.
+ */
+ public void v(String msg) {
+ Log.v(mTag, record(Category.VERBOSE, msg));
+ }
+
+ /**
+ * Log a terrible failure message.
+ *
+ * <p>The log entry will be also added to the system log and will trigger system reporting
+ * for terrible failures.
+ */
+ public void wtf(String msg) {
+ Log.wtf(mTag, record(Category.TERRIBLE, msg));
+ }
+
+ /**
+ * Log a terrible failure due to an exception, with the exception stacktrace if provided.
+ *
+ * <p>The error and exception message appear in the shared log, but the stacktrace is only
+ * logged in general log output (logcat). The log entry will be also added to the system log
+ * and will trigger system reporting for terrible failures.
+ */
+ public void wtf(@NonNull String msg, @Nullable Throwable exception) {
+ if (exception == null) {
+ e(msg);
+ return;
+ }
+ Log.wtf(mTag, record(Category.TERRIBLE, msg + ": " + exception.getMessage()), exception);
+ }
+
+
//////
// Methods that only log an entry (and do NOT emit to the system log).
//////
diff --git a/staticlibs/framework/com/android/net/module/util/NetworkStackConstants.java b/staticlibs/framework/com/android/net/module/util/NetworkStackConstants.java
index 24aabbc..9149160 100644
--- a/staticlibs/framework/com/android/net/module/util/NetworkStackConstants.java
+++ b/staticlibs/framework/com/android/net/module/util/NetworkStackConstants.java
@@ -17,6 +17,7 @@
package com.android.net.module.util;
import android.net.InetAddresses;
+import android.net.IpPrefix;
import java.net.Inet4Address;
import java.net.Inet6Address;
@@ -99,6 +100,8 @@
public static final int IPV4_ADDR_LEN = 4;
public static final int IPV4_FLAG_MF = 0x2000;
public static final int IPV4_FLAG_DF = 0x4000;
+ // getSockOpt() for v4 MTU
+ public static final int IP_MTU = 14;
public static final Inet4Address IPV4_ADDR_ALL = makeInet4Address(
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff);
public static final Inet4Address IPV4_ADDR_ANY = makeInet4Address(
@@ -108,6 +111,12 @@
(byte) 0, (byte) 0, (byte) 0, (byte) 0,
(byte) 0, (byte) 0, (byte) 0, (byte) 0,
(byte) 0, (byte) 0, (byte) 0, (byte) 0 });
+
+ /**
+ * CLAT constants
+ */
+ public static final IpPrefix CLAT_PREFIX = new IpPrefix("192.0.0.0/29");
+
/**
* IPv6 constants.
*
@@ -121,6 +130,10 @@
public static final int IPV6_SRC_ADDR_OFFSET = 8;
public static final int IPV6_DST_ADDR_OFFSET = 24;
public static final int IPV6_MIN_MTU = 1280;
+ public static final int IPV6_FRAGMENT_HEADER_LEN = 8;
+ public static final int RFC7421_PREFIX_LENGTH = 64;
+ // getSockOpt() for v6 MTU
+ public static final int IPV6_MTU = 24;
public static final Inet6Address IPV6_ADDR_ALL_NODES_MULTICAST =
(Inet6Address) InetAddresses.parseNumericAddress("ff02::1");
public static final Inet6Address IPV6_ADDR_ALL_ROUTERS_MULTICAST =
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/DeviceConfigUtilsTest.java b/staticlibs/tests/unit/src/com/android/net/module/util/DeviceConfigUtilsTest.java
index 328c39a..7946244 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/DeviceConfigUtilsTest.java
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/DeviceConfigUtilsTest.java
@@ -17,12 +17,16 @@
package com.android.net.module.util;
import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
+import static android.provider.DeviceConfig.NAMESPACE_TETHERING;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
+import static com.android.net.module.util.FeatureVersions.CONNECTIVITY_MODULE_ID;
+import static com.android.net.module.util.FeatureVersions.NETWORK_STACK_MODULE_ID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.anyInt;
@@ -84,6 +88,8 @@
private static final String TEST_GO_APEX_PACKAGE_NAME = "com.prefix.android.go.tethering";
private static final String TEST_CONNRES_PACKAGE_NAME =
"com.prefix.android.connectivity.resources";
+ private static final String TEST_NETWORKSTACK_NAME = "com.prefix.android.networkstack";
+ private static final String TEST_GO_NETWORKSTACK_NAME = "com.prefix.android.go.networkstack";
private final PackageInfo mPackageInfo = new PackageInfo();
private final PackageInfo mApexPackageInfo = new PackageInfo();
private MockitoSession mSession;
@@ -346,4 +352,82 @@
doThrow(new Resources.NotFoundException()).when(mResources).getInteger(someResId);
assertEquals(2098, DeviceConfigUtils.getResIntegerConfig(mContext, someResId, 2098));
}
+
+ @Test
+ public void testGetNetworkStackModuleVersionCaching() throws Exception {
+ final PackageInfo networkStackPackageInfo = new PackageInfo();
+ networkStackPackageInfo.setLongVersionCode(TEST_PACKAGE_VERSION);
+ doReturn(networkStackPackageInfo).when(mPm).getPackageInfo(
+ eq(TEST_NETWORKSTACK_NAME), anyInt());
+ assertEquals(TEST_PACKAGE_VERSION,
+ DeviceConfigUtils.getNetworkStackModuleVersion(mContext));
+
+ assertEquals(TEST_PACKAGE_VERSION,
+ DeviceConfigUtils.getNetworkStackModuleVersion(mContext));
+ // Package info is only queried once
+ verify(mPm, times(1)).getPackageInfo(anyString(), anyInt());
+ verify(mContext, never()).getPackageName();
+ }
+
+ @Test
+ public void testGetNetworkStackModuleVersionOnNonMainline() {
+ assertEquals(DeviceConfigUtils.DEFAULT_PACKAGE_VERSION,
+ DeviceConfigUtils.getNetworkStackModuleVersion(mContext));
+ }
+
+ @Test
+ public void testGetNetworkStackModuleVersion() throws Exception {
+ final PackageInfo networkStackPackageInfo = new PackageInfo();
+ final PackageInfo goNetworkStackPackageInfo = new PackageInfo();
+ networkStackPackageInfo.setLongVersionCode(TEST_PACKAGE_VERSION);
+ goNetworkStackPackageInfo.setLongVersionCode(TEST_PACKAGE_VERSION + 1);
+ doReturn(goNetworkStackPackageInfo).when(mPm).getPackageInfo(
+ eq(TEST_NETWORKSTACK_NAME), anyInt());
+ // Verify the returned value is go module version.
+ assertEquals(TEST_PACKAGE_VERSION + 1,
+ DeviceConfigUtils.getNetworkStackModuleVersion(mContext));
+ }
+
+ @Test
+ public void testIsFeatureSupported_networkStackFeature() throws Exception {
+ // Supported for DEFAULT_PACKAGE_VERSION
+ assertTrue(DeviceConfigUtils.isFeatureSupported(
+ mContext, TEST_PACKAGE_VERSION + NETWORK_STACK_MODULE_ID));
+
+ final PackageInfo networkStackPackageInfo = new PackageInfo();
+ networkStackPackageInfo.setLongVersionCode(TEST_PACKAGE_VERSION);
+ doReturn(networkStackPackageInfo).when(mPm).getPackageInfo(
+ eq(TEST_NETWORKSTACK_NAME), anyInt());
+
+ assertTrue(DeviceConfigUtils.isFeatureSupported(
+ mContext, TEST_PACKAGE_VERSION + NETWORK_STACK_MODULE_ID));
+ assertFalse(DeviceConfigUtils.isFeatureSupported(
+ mContext, TEST_PACKAGE_VERSION + NETWORK_STACK_MODULE_ID + 1));
+ }
+
+ @Test
+ public void testIsFeatureSupported_tetheringFeature() throws Exception {
+ assertTrue(DeviceConfigUtils.isFeatureSupported(
+ mContext, TEST_PACKAGE_VERSION + CONNECTIVITY_MODULE_ID));
+ // Return false because feature requires a future version.
+ assertFalse(DeviceConfigUtils.isFeatureSupported(
+ mContext, 889900000L + CONNECTIVITY_MODULE_ID));
+ }
+
+ @Test
+ public void testIsFeatureSupported_illegalModule() throws Exception {
+ assertThrows(IllegalArgumentException.class,
+ () -> DeviceConfigUtils.isFeatureSupported(mContext, TEST_PACKAGE_VERSION));
+ }
+
+ @Test
+ public void testIsTetheringFeatureForceDisabled() throws Exception {
+ doReturn("0").when(() -> DeviceConfig.getProperty(
+ eq(NAMESPACE_TETHERING), eq(TEST_EXPERIMENT_FLAG)));
+ assertFalse(DeviceConfigUtils.isTetheringFeatureForceDisabled(TEST_EXPERIMENT_FLAG));
+
+ doReturn(TEST_FLAG_VALUE_STRING).when(
+ () -> DeviceConfig.getProperty(eq(NAMESPACE_TETHERING), eq(TEST_EXPERIMENT_FLAG)));
+ assertTrue(DeviceConfigUtils.isTetheringFeatureForceDisabled(TEST_EXPERIMENT_FLAG));
+ }
}
diff --git a/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt b/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt
index 6bcb8fc..f1f0975 100644
--- a/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt
+++ b/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt
@@ -72,7 +72,7 @@
val cb = TestableNetworkCallback()
val cm = context.getSystemService(ConnectivityManager::class.java)
?: fail("Could not get ConnectivityManager")
- cm.registerNetworkCallback(
+ cm.requestNetwork(
NetworkRequest.Builder()
.addTransportType(TRANSPORT_CELLULAR)
.addCapability(NET_CAPABILITY_INTERNET).build(), cb)