[NETD-TC#14] Add traffic controller JNI to tethering module
This CL provides native methods to access BPF maps via a native library. Bug: 202086915 Test: m; flash; boot; Change-Id: I2e5d03d0c2e31ba11996d1b28ead9b552397fe29
This commit is contained in:
@@ -51,7 +51,8 @@ apex {
|
|||||||
first: {
|
first: {
|
||||||
jni_libs: [
|
jni_libs: [
|
||||||
"libservice-connectivity",
|
"libservice-connectivity",
|
||||||
"libcom_android_connectivity_com_android_net_module_util_jni"
|
"libcom_android_connectivity_com_android_net_module_util_jni",
|
||||||
|
"libtraffic_controller_jni",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
both: {
|
both: {
|
||||||
|
|||||||
@@ -48,6 +48,37 @@ cc_library {
|
|||||||
min_sdk_version: "30",
|
min_sdk_version: "30",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_library_shared {
|
||||||
|
name: "libtraffic_controller_jni",
|
||||||
|
cflags: [
|
||||||
|
"-Wall",
|
||||||
|
"-Werror",
|
||||||
|
"-Wno-unused-parameter",
|
||||||
|
"-Wthread-safety",
|
||||||
|
],
|
||||||
|
srcs: [
|
||||||
|
"jni/*.cpp",
|
||||||
|
],
|
||||||
|
header_libs: [
|
||||||
|
"bpf_connectivity_headers",
|
||||||
|
],
|
||||||
|
static_libs: [
|
||||||
|
"libnetdutils",
|
||||||
|
"libtraffic_controller",
|
||||||
|
"netd_aidl_interface-lateststable-ndk",
|
||||||
|
],
|
||||||
|
shared_libs: [
|
||||||
|
"libbase",
|
||||||
|
"liblog",
|
||||||
|
"libutils",
|
||||||
|
"libnativehelper",
|
||||||
|
],
|
||||||
|
apex_available: [
|
||||||
|
"com.android.tethering",
|
||||||
|
],
|
||||||
|
min_sdk_version: "30",
|
||||||
|
}
|
||||||
|
|
||||||
cc_test {
|
cc_test {
|
||||||
name: "traffic_controller_unit_test",
|
name: "traffic_controller_unit_test",
|
||||||
test_suites: ["general-tests"],
|
test_suites: ["general-tests"],
|
||||||
|
|||||||
266
service/native/jni/com_android_server_BpfNetMaps.cpp
Normal file
266
service/native/jni/com_android_server_BpfNetMaps.cpp
Normal file
@@ -0,0 +1,266 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define LOG_TAG "TrafficControllerJni"
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include <nativehelper/JNIHelp.h>
|
||||||
|
#include <nativehelper/ScopedUtfChars.h>
|
||||||
|
#include <nativehelper/ScopedPrimitiveArray.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "TrafficController.h"
|
||||||
|
#include "android-base/logging.h"
|
||||||
|
#include "bpf_shared.h"
|
||||||
|
#include "utils/Log.h"
|
||||||
|
|
||||||
|
using android::net::TrafficController;
|
||||||
|
using android::netdutils::Status;
|
||||||
|
|
||||||
|
using UidOwnerMatchType::PENALTY_BOX_MATCH;
|
||||||
|
using UidOwnerMatchType::HAPPY_BOX_MATCH;
|
||||||
|
|
||||||
|
static android::net::TrafficController mTc;
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
static void native_init(JNIEnv* env, jobject clazz) {
|
||||||
|
Status status = mTc.start();
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGE("%s failed", __func__);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_addNaughtyApp(JNIEnv* env, jobject clazz, jint uid) {
|
||||||
|
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
|
||||||
|
Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
|
||||||
|
TrafficController::IptOp::IptOpInsert);
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGE("%s failed, errer code = %d", __func__, status.code());
|
||||||
|
}
|
||||||
|
return (jint)status.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_removeNaughtyApp(JNIEnv* env, jobject clazz, jint uid) {
|
||||||
|
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
|
||||||
|
Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
|
||||||
|
TrafficController::IptOp::IptOpDelete);
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGE("%s failed, errer code = %d", __func__, status.code());
|
||||||
|
}
|
||||||
|
return (jint)status.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_addNiceApp(JNIEnv* env, jobject clazz, jint uid) {
|
||||||
|
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
|
||||||
|
Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
|
||||||
|
TrafficController::IptOp::IptOpInsert);
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGE("%s failed, errer code = %d", __func__, status.code());
|
||||||
|
}
|
||||||
|
return (jint)status.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_removeNiceApp(JNIEnv* env, jobject clazz, jint uid) {
|
||||||
|
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
|
||||||
|
Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
|
||||||
|
TrafficController::IptOp::IptOpDelete);
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGD("%s failed, errer code = %d", __func__, status.code());
|
||||||
|
}
|
||||||
|
return (jint)status.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_setChildChain(JNIEnv* env, jobject clazz, jint childChain, jboolean enable) {
|
||||||
|
auto chain = static_cast<ChildChain>(childChain);
|
||||||
|
int res = mTc.toggleUidOwnerMap(chain, enable);
|
||||||
|
if (res) {
|
||||||
|
ALOGE("%s failed, error code = %d", __func__, res);
|
||||||
|
}
|
||||||
|
return (jint)res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_replaceUidChain(JNIEnv* env, jobject clazz, jstring name, jboolean isAllowlist,
|
||||||
|
jintArray jUids) {
|
||||||
|
const ScopedUtfChars chainNameUtf8(env, name);
|
||||||
|
if (chainNameUtf8.c_str() == nullptr) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
const std::string chainName(chainNameUtf8.c_str());
|
||||||
|
|
||||||
|
ScopedIntArrayRO uids(env, jUids);
|
||||||
|
if (uids.get() == nullptr) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size = uids.size();
|
||||||
|
std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
|
||||||
|
int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data);
|
||||||
|
if (res) {
|
||||||
|
ALOGE("%s failed, error code = %d", __func__, res);
|
||||||
|
}
|
||||||
|
return (jint)res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static FirewallType getFirewallType(ChildChain chain) {
|
||||||
|
switch (chain) {
|
||||||
|
case DOZABLE:
|
||||||
|
return ALLOWLIST;
|
||||||
|
case STANDBY:
|
||||||
|
return DENYLIST;
|
||||||
|
case POWERSAVE:
|
||||||
|
return ALLOWLIST;
|
||||||
|
case RESTRICTED:
|
||||||
|
return ALLOWLIST;
|
||||||
|
case NONE:
|
||||||
|
default:
|
||||||
|
return DENYLIST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_setUidRule(JNIEnv* env, jobject clazz, jint childChain, jint uid,
|
||||||
|
jint firewallRule) {
|
||||||
|
auto chain = static_cast<ChildChain>(childChain);
|
||||||
|
auto rule = static_cast<FirewallRule>(firewallRule);
|
||||||
|
FirewallType fType = getFirewallType(chain);
|
||||||
|
|
||||||
|
int res = mTc.changeUidOwnerRule(chain, uid, rule, fType);
|
||||||
|
if (res) {
|
||||||
|
ALOGE("%s failed, error code = %d", __func__, res);
|
||||||
|
}
|
||||||
|
return (jint)res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_addUidInterfaceRules(JNIEnv* env, jobject clazz, jstring ifName,
|
||||||
|
jintArray jUids) {
|
||||||
|
const ScopedUtfChars ifNameUtf8(env, ifName);
|
||||||
|
if (ifNameUtf8.c_str() == nullptr) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
const std::string interfaceName(ifNameUtf8.c_str());
|
||||||
|
const int ifIndex = if_nametoindex(interfaceName.c_str());
|
||||||
|
|
||||||
|
ScopedIntArrayRO uids(env, jUids);
|
||||||
|
if (uids.get() == nullptr) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size = uids.size();
|
||||||
|
std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
|
||||||
|
Status status = mTc.addUidInterfaceRules(ifIndex, data);
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGE("%s failed, error code = %d", __func__, status.code());
|
||||||
|
}
|
||||||
|
return (jint)status.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_removeUidInterfaceRules(JNIEnv* env, jobject clazz, jintArray jUids) {
|
||||||
|
ScopedIntArrayRO uids(env, jUids);
|
||||||
|
if (uids.get() == nullptr) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size = uids.size();
|
||||||
|
std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
|
||||||
|
Status status = mTc.removeUidInterfaceRules(data);
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGE("%s failed, error code = %d", __func__, status.code());
|
||||||
|
}
|
||||||
|
return (jint)status.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_swapActiveStatsMap(JNIEnv* env, jobject clazz) {
|
||||||
|
Status status = mTc.swapActiveStatsMap();
|
||||||
|
if (!isOk(status)) {
|
||||||
|
ALOGD("%s failed, error code = %d", __func__, status.code());
|
||||||
|
}
|
||||||
|
return (jint)status.code();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void native_setPermissionForUids(JNIEnv* env, jobject clazz, jint permission,
|
||||||
|
jintArray jUids) {
|
||||||
|
ScopedIntArrayRO uids(env, jUids);
|
||||||
|
if (uids.get() == nullptr) return;
|
||||||
|
|
||||||
|
size_t size = uids.size();
|
||||||
|
static_assert(sizeof(*(uids.get())) == sizeof(uid_t));
|
||||||
|
std::vector<uid_t> data ((uid_t *)&uids[0], (uid_t*)&uids[size]);
|
||||||
|
mTc.setPermissionForUids(permission, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_setCounterSet(JNIEnv* env, jobject clazz, jint setNum, jint uid) {
|
||||||
|
uid_t callingUid = getuid();
|
||||||
|
int res = mTc.setCounterSet(setNum, (uid_t)uid, callingUid);
|
||||||
|
if (res) {
|
||||||
|
ALOGE("%s failed, error code = %d", __func__, res);
|
||||||
|
}
|
||||||
|
return (jint)res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint native_deleteTagData(JNIEnv* env, jobject clazz, jint tagNum, jint uid) {
|
||||||
|
uid_t callingUid = getuid();
|
||||||
|
int res = mTc.deleteTagData(tagNum, (uid_t)uid, callingUid);
|
||||||
|
if (res) {
|
||||||
|
ALOGE("%s failed, error code = %d", __func__, res);
|
||||||
|
}
|
||||||
|
return (jint)res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* JNI registration.
|
||||||
|
*/
|
||||||
|
// clang-format off
|
||||||
|
static const JNINativeMethod gMethods[] = {
|
||||||
|
/* name, signature, funcPtr */
|
||||||
|
{"native_init", "()V",
|
||||||
|
(void*)native_init},
|
||||||
|
{"native_addNaughtyApp", "(I)I",
|
||||||
|
(void*)native_addNaughtyApp},
|
||||||
|
{"native_removeNaughtyApp", "(I)I",
|
||||||
|
(void*)native_removeNaughtyApp},
|
||||||
|
{"native_addNiceApp", "(I)I",
|
||||||
|
(void*)native_addNiceApp},
|
||||||
|
{"native_removeNiceApp", "(I)I",
|
||||||
|
(void*)native_removeNiceApp},
|
||||||
|
{"native_setChildChain", "(IZ)I",
|
||||||
|
(void*)native_setChildChain},
|
||||||
|
{"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
|
||||||
|
(void*)native_replaceUidChain},
|
||||||
|
{"native_setUidRule", "(III)I",
|
||||||
|
(void*)native_setUidRule},
|
||||||
|
{"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
|
||||||
|
(void*)native_addUidInterfaceRules},
|
||||||
|
{"native_removeUidInterfaceRules", "([I)I",
|
||||||
|
(void*)native_removeUidInterfaceRules},
|
||||||
|
{"native_swapActiveStatsMap", "()I",
|
||||||
|
(void*)native_swapActiveStatsMap},
|
||||||
|
{"native_setPermissionForUids", "(I[I)V",
|
||||||
|
(void*)native_setPermissionForUids},
|
||||||
|
{"native_setCounterSet", "(II)I",
|
||||||
|
(void*)native_setCounterSet},
|
||||||
|
{"native_deleteTagData", "(II)I",
|
||||||
|
(void*)native_deleteTagData},
|
||||||
|
};
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
int register_com_android_server_BpfNetMaps(JNIEnv* env) {
|
||||||
|
return jniRegisterNativeMethods(env,
|
||||||
|
"com/android/server/BpfNetMaps",
|
||||||
|
gMethods, NELEM(gMethods));
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // namespace android
|
||||||
41
service/native/jni/onload.cpp
Normal file
41
service/native/jni/onload.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define LOG_TAG "TrafficControllerJni"
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include <nativehelper/JNIHelp.h>
|
||||||
|
|
||||||
|
#include "utils/Log.h"
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
int register_com_android_server_BpfNetMaps(JNIEnv* env);
|
||||||
|
|
||||||
|
extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
|
||||||
|
JNIEnv *env;
|
||||||
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||||
|
ALOGE("%s: ERROR: GetEnv failed", __func__);
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (register_com_android_server_BpfNetMaps(env) < 0)
|
||||||
|
return JNI_ERR;
|
||||||
|
|
||||||
|
return JNI_VERSION_1_6;
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // namespace android
|
||||||
@@ -28,6 +28,11 @@ import android.util.Log;
|
|||||||
public class BpfNetMaps {
|
public class BpfNetMaps {
|
||||||
private static final String TAG = "BpfNetMaps";
|
private static final String TAG = "BpfNetMaps";
|
||||||
|
|
||||||
|
static {
|
||||||
|
System.loadLibrary("traffic_controller_jni");
|
||||||
|
native_init();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add naughty app bandwidth rule for specific app
|
* Add naughty app bandwidth rule for specific app
|
||||||
*
|
*
|
||||||
@@ -239,6 +244,7 @@ public class BpfNetMaps {
|
|||||||
return -err;
|
return -err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static native void native_init();
|
||||||
private native int native_addNaughtyApp(int uid);
|
private native int native_addNaughtyApp(int uid);
|
||||||
private native int native_removeNaughtyApp(int uid);
|
private native int native_removeNaughtyApp(int uid);
|
||||||
private native int native_addNiceApp(int uid);
|
private native int native_addNiceApp(int uid);
|
||||||
|
|||||||
Reference in New Issue
Block a user