[NETD-TC#15] Make ConnectivityService and PermissionMonitor
calls BpfNetMaps on T
Since TrafficController moves to mainline module for T, so some netd binder
interfaces revelant to BPF are going to deprecated. Provide JNI APIs to
call TrafficController inside mainline module for T.
Bug: 209935649
Test: atest CtsHostsideNetworkTests
Change-Id: Ib3b43cf2840e02806395af9f1e019ca6fccd032e
diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java
index 9d89788..e444a12 100644
--- a/service/src/com/android/server/BpfNetMaps.java
+++ b/service/src/com/android/server/BpfNetMaps.java
@@ -16,6 +16,8 @@
package com.android.server;
+import android.net.INetd;
+import android.os.RemoteException;
import android.os.ServiceSpecificException;
import android.system.Os;
import android.util.Log;
@@ -27,10 +29,19 @@
*/
public class BpfNetMaps {
private static final String TAG = "BpfNetMaps";
+ private final INetd mNetd;
+ // TODO: change USE_JNI to SdkLevel.isAtLeastT()
+ private static final boolean USE_JNI = false;
static {
- System.loadLibrary("traffic_controller_jni");
- native_init();
+ if (USE_JNI) {
+ System.loadLibrary("traffic_controller_jni");
+ native_init();
+ }
+ }
+
+ public BpfNetMaps(INetd netd) {
+ mNetd = netd;
}
/**
@@ -41,6 +52,14 @@
* cause of the failure.
*/
public void addNaughtyApp(final int uid) {
+ if (!USE_JNI) {
+ try {
+ mNetd.bandwidthAddNaughtyApp(uid);
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return;
+ }
final int err = native_addNaughtyApp(uid);
if (err != 0) {
throw new ServiceSpecificException(err, "Unable to add naughty app: "
@@ -56,6 +75,14 @@
* cause of the failure.
*/
public void removeNaughtyApp(final int uid) {
+ if (!USE_JNI) {
+ try {
+ mNetd.bandwidthRemoveNaughtyApp(uid);
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return;
+ }
final int err = native_removeNaughtyApp(uid);
if (err != 0) {
throw new ServiceSpecificException(err, "Unable to remove naughty app: "
@@ -71,6 +98,14 @@
* cause of the failure.
*/
public void addNiceApp(final int uid) {
+ if (!USE_JNI) {
+ try {
+ mNetd.bandwidthAddNiceApp(uid);
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return;
+ }
final int err = native_addNiceApp(uid);
if (err != 0) {
throw new ServiceSpecificException(err, "Unable to add nice app: "
@@ -86,6 +121,14 @@
* cause of the failure.
*/
public void removeNiceApp(final int uid) {
+ if (!USE_JNI) {
+ try {
+ mNetd.bandwidthRemoveNiceApp(uid);
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return;
+ }
final int err = native_removeNiceApp(uid);
if (err != 0) {
throw new ServiceSpecificException(err, "Unable to remove nice app: "
@@ -102,6 +145,14 @@
* cause of the failure.
*/
public void setChildChain(final int childChain, final boolean enable) {
+ if (!USE_JNI) {
+ try {
+ mNetd.firewallEnableChildChain(childChain, enable);
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return;
+ }
final int err = native_setChildChain(childChain, enable);
if (err != 0) {
throw new ServiceSpecificException(-err, "Unable to set child chain: "
@@ -124,6 +175,14 @@
*/
public int replaceUidChain(final String chainName, final boolean isAllowlist,
final int[] uids) {
+ if (!USE_JNI) {
+ try {
+ mNetd.firewallReplaceUidChain(chainName, isAllowlist, uids);
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return 0;
+ }
final int err = native_replaceUidChain(chainName, isAllowlist, uids);
if (err != 0) {
Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
@@ -140,8 +199,15 @@
* @throws ServiceSpecificException in case of failure, with an error code indicating the
* cause of the failure.
*/
- public void setUidRule(final int childChain, final int uid,
- final int firewallRule) {
+ public void setUidRule(final int childChain, final int uid, final int firewallRule) {
+ if (!USE_JNI) {
+ try {
+ mNetd.firewallSetUidRule(childChain, uid, firewallRule);
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return;
+ }
final int err = native_setUidRule(childChain, uid, firewallRule);
if (err != 0) {
throw new ServiceSpecificException(-err, "Unable to set uid rule: "
@@ -166,6 +232,14 @@
* cause of the failure.
*/
public void addUidInterfaceRules(final String ifName, final int[] uids) {
+ if (!USE_JNI) {
+ try {
+ mNetd.firewallAddUidInterfaceRules(ifName, uids);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Exception when updating permissions: " + e);
+ }
+ return;
+ }
final int err = native_addUidInterfaceRules(ifName, uids);
if (err != 0) {
throw new ServiceSpecificException(err, "Unable to add uid interface rules: "
@@ -184,6 +258,14 @@
* cause of the failure.
*/
public void removeUidInterfaceRules(final int[] uids) {
+ if (!USE_JNI) {
+ try {
+ mNetd.firewallRemoveUidInterfaceRules(uids);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Exception when updating permissions: " + e);
+ }
+ return;
+ }
final int err = native_removeUidInterfaceRules(uids);
if (err != 0) {
throw new ServiceSpecificException(err, "Unable to remove uid interface rules: "
@@ -197,6 +279,14 @@
* cause of the failure.
*/
public void swapActiveStatsMap() {
+ if (!USE_JNI) {
+ try {
+ mNetd.trafficSwapActiveStatsMap();
+ } catch (RemoteException e) {
+ throw new IllegalStateException(e);
+ }
+ return;
+ }
final int err = native_swapActiveStatsMap();
if (err != 0) {
throw new ServiceSpecificException(err, "Unable to swap active stats map: "
@@ -213,8 +303,16 @@
* revoke all permissions for the uids.
* @param uids uid of users to grant permission
*/
- public void setNetPermForUids(final int permission, final int[] uids) {
- native_setPermissionForUids(permission, uids);
+ public void setNetPermForUids(final int permissions, final int[] uids) {
+ if (!USE_JNI) {
+ try {
+ mNetd.trafficSetNetPermForUids(permissions, uids);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Pass appId list of special permission failed." + e);
+ }
+ return;
+ }
+ native_setPermissionForUids(permissions, uids);
}
/**
@@ -255,7 +353,7 @@
private native int native_addUidInterfaceRules(String ifName, int[] uids);
private native int native_removeUidInterfaceRules(int[] uids);
private native int native_swapActiveStatsMap();
- private native void native_setPermissionForUids(int permission, int[] uids);
+ private native void native_setPermissionForUids(int permissions, int[] uids);
private native int native_setCounterSet(int counterSet, int uid);
private native int native_deleteTagData(int tag, int uid);
}