Skip to content
Snippets Groups Projects
Commit 424038fe authored by Motomu Utsumi's avatar Motomu Utsumi
Browse files

Remove TrafficController

BpfNetMaps now updates bpf map by Java library and does not use
TrafficController.

Bug: 217624062
Test: TH
Change-Id: Id727cf7b79592e913c967156e6e224de338f5f65
parent 96b6c392
No related branches found
No related tags found
No related merge requests found
......@@ -99,9 +99,6 @@
{
"name": "TetheringIntegrationTests"
},
{
"name": "traffic_controller_unit_test"
},
{
"name": "libnetworkstats_test"
},
......@@ -133,10 +130,6 @@
{
"name": "dns_helper_unit_test"
},
{
"name": "traffic_controller_unit_test",
"keywords": ["netd-device-kernel-4.9", "netd-device-kernel-4.14"]
},
{
"name": "FrameworksNetDeflakeTest"
},
......@@ -273,9 +266,6 @@
}
]
},
{
"name": "traffic_controller_unit_test[CaptivePortalLoginGoogle.apk+NetworkStackGoogle.apk+com.google.android.resolv.apex+com.google.android.tethering.apex]"
},
{
"name": "libnetworkstats_test[CaptivePortalLoginGoogle.apk+NetworkStackGoogle.apk+com.google.android.resolv.apex+com.google.android.tethering.apex]"
},
......
......@@ -124,7 +124,6 @@ cc_library_shared {
"libmodules-utils-build",
"libnetjniutils",
"libnet_utils_device_common_bpfjni",
"libtraffic_controller",
"netd_aidl_interface-lateststable-ndk",
],
shared_libs: [
......
......@@ -14,179 +14,13 @@
* limitations under the License.
*/
#define LOG_TAG "TrafficControllerJni"
#include "TrafficController.h"
#include "netd.h"
#include "bpf/BpfUtils.h"
#include <jni.h>
#include <log/log.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include <nativehelper/ScopedPrimitiveArray.h>
#include <netjniutils/netjniutils.h>
#include <net/if.h>
#include <private/android_filesystem_config.h>
#include <unistd.h>
#include <vector>
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 {
#define CHECK_LOG(status) \
do { \
if (!isOk(status)) \
ALOGE("%s failed, error code = %d", __func__, status.code()); \
} while (0)
static void native_init(JNIEnv* env, jclass clazz, jboolean startSkDestroyListener) {
Status status = mTc.start(startSkDestroyListener);
CHECK_LOG(status);
if (!isOk(status)) {
uid_t uid = getuid();
ALOGE("BpfNetMaps jni init failure as uid=%d", uid);
// We probably only ever get called from system_server (ie. AID_SYSTEM)
// or from tests, and never from network_stack (ie. AID_NETWORK_STACK).
// However, if we ever do add calls from production network_stack code
// we do want to make sure this initializes correctly.
// TODO: Fix tests to not use this jni lib, so we can unconditionally abort()
if (uid == AID_SYSTEM || uid == AID_NETWORK_STACK) abort();
}
}
static jint native_addNaughtyApp(JNIEnv* env, jobject self, jint uid) {
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
TrafficController::IptOp::IptOpInsert);
CHECK_LOG(status);
return (jint)status.code();
}
static jint native_removeNaughtyApp(JNIEnv* env, jobject self, jint uid) {
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
TrafficController::IptOp::IptOpDelete);
CHECK_LOG(status);
return (jint)status.code();
}
static jint native_addNiceApp(JNIEnv* env, jobject self, jint uid) {
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
TrafficController::IptOp::IptOpInsert);
CHECK_LOG(status);
return (jint)status.code();
}
static jint native_removeNiceApp(JNIEnv* env, jobject self, jint uid) {
const uint32_t appUids = static_cast<uint32_t>(abs(uid));
Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
TrafficController::IptOp::IptOpDelete);
CHECK_LOG(status);
return (jint)status.code();
}
static jint native_setChildChain(JNIEnv* env, jobject self, 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 self, 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();
static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
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 jint native_setUidRule(JNIEnv* env, jobject self, jint childChain, jint uid,
jint firewallRule) {
auto chain = static_cast<ChildChain>(childChain);
auto rule = static_cast<FirewallRule>(firewallRule);
FirewallType fType = mTc.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 self, jstring ifName,
jintArray jUids) {
// Null ifName is a wildcard to allow apps to receive packets on all interfaces and ifIndex is
// set to 0.
int ifIndex = 0;
if (ifName != nullptr) {
const ScopedUtfChars ifNameUtf8(env, ifName);
const std::string interfaceName(ifNameUtf8.c_str());
ifIndex = if_nametoindex(interfaceName.c_str());
}
ScopedIntArrayRO uids(env, jUids);
if (uids.get() == nullptr) return -EINVAL;
size_t size = uids.size();
static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
Status status = mTc.addUidInterfaceRules(ifIndex, data);
CHECK_LOG(status);
return (jint)status.code();
}
static jint native_removeUidInterfaceRules(JNIEnv* env, jobject self, jintArray jUids) {
ScopedIntArrayRO uids(env, jUids);
if (uids.get() == nullptr) return -EINVAL;
size_t size = uids.size();
static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
Status status = mTc.removeUidInterfaceRules(data);
CHECK_LOG(status);
return (jint)status.code();
}
static jint native_updateUidLockdownRule(JNIEnv* env, jobject self, jint uid, jboolean add) {
Status status = mTc.updateUidLockdownRule(uid, add);
CHECK_LOG(status);
return (jint)status.code();
}
static jint native_swapActiveStatsMap(JNIEnv* env, jobject self) {
Status status = mTc.swapActiveStatsMap();
CHECK_LOG(status);
return (jint)status.code();
}
static void native_setPermissionForUids(JNIEnv* env, jobject self, 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_synchronizeKernelRCU(JNIEnv* env, jobject self) {
return -bpf::synchronizeKernelRCU();
}
......@@ -197,32 +31,6 @@ static jint native_synchronizeKernelRCU(JNIEnv* env, jobject self) {
// clang-format off
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"native_init", "(Z)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_updateUidLockdownRule", "(IZ)I",
(void*)native_updateUidLockdownRule},
{"native_swapActiveStatsMap", "()I",
(void*)native_swapActiveStatsMap},
{"native_setPermissionForUids", "(I[I)V",
(void*)native_setPermissionForUids},
{"native_synchronizeKernelRCU", "()I",
(void*)native_synchronizeKernelRCU},
};
......
/*
* 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.
*/
package {
default_applicable_licenses: ["Android-Apache-2.0"],
}
cc_library {
name: "libtraffic_controller",
defaults: ["netd_defaults"],
srcs: [
"TrafficController.cpp",
],
header_libs: [
"bpf_connectivity_headers",
],
static_libs: [
// TrafficController would use the constants of INetd so that add
// netd_aidl_interface-lateststable-ndk.
"netd_aidl_interface-lateststable-ndk",
],
shared_libs: [
// TODO: Find a good way to remove libbase.
"libbase",
"libcutils",
"libnetdutils",
"libutils",
"liblog",
],
export_include_dirs: ["include"],
sanitize: {
cfi: true,
},
apex_available: [
"com.android.tethering",
],
min_sdk_version: "30",
}
cc_test {
name: "traffic_controller_unit_test",
test_suites: ["general-tests", "mts-tethering"],
test_config_template: ":net_native_test_config_template",
require_root: true,
local_include_dirs: ["include"],
header_libs: [
"bpf_connectivity_headers",
],
srcs: [
"TrafficControllerTest.cpp",
],
static_libs: [
"libbase",
"libgmock",
"liblog",
"libnetdutils",
"libtraffic_controller",
"libutils",
"libnetd_updatable",
"netd_aidl_interface-lateststable-ndk",
],
compile_multilib: "both",
multilib: {
lib32: {
suffix: "32",
},
lib64: {
suffix: "64",
},
},
}
This diff is collapsed.
This diff is collapsed.
/*
* 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.
*/
#pragma once
#include <set>
#include <Common.h>
#include "android-base/thread_annotations.h"
#include "bpf/BpfMap.h"
#include "netd.h"
#include "netdutils/NetlinkListener.h"
#include "netdutils/StatusOr.h"
namespace android {
namespace net {
using netdutils::StatusOr;
class TrafficController {
public:
/*
* Initialize the whole controller
*/
netdutils::Status start(bool startSkDestroyListener);
/*
* Swap the stats map config from current active stats map to the idle one.
*/
netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex);
int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
int removeUidOwnerRule(const uid_t uid);
int replaceUidOwnerMap(const std::string& name, bool isAllowlist,
const std::vector<int32_t>& uids);
enum IptOp { IptOpInsert, IptOpDelete };
netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
FirewallType type) EXCLUDES(mMutex);
netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids)
EXCLUDES(mMutex);
netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids)
EXCLUDES(mMutex);
netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex);
netdutils::Status updateUidLockdownRule(const uid_t uid, const bool add) EXCLUDES(mMutex);
netdutils::Status updateUidOwnerMap(const uint32_t uid,
UidOwnerMatchType matchType, IptOp op) EXCLUDES(mMutex);
int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex);
static netdutils::StatusOr<std::unique_ptr<netdutils::NetlinkListenerInterface>>
makeSkDestroyListener();
void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex);
FirewallType getFirewallType(ChildChain);
static const char* LOCAL_DOZABLE;
static const char* LOCAL_STANDBY;
static const char* LOCAL_POWERSAVE;
static const char* LOCAL_RESTRICTED;
static const char* LOCAL_LOW_POWER_STANDBY;
static const char* LOCAL_OEM_DENY_1;
static const char* LOCAL_OEM_DENY_2;
static const char* LOCAL_OEM_DENY_3;
private:
/*
* mCookieTagMap: Store the corresponding tag and uid for a specific socket.
* DO NOT hold any locks when modifying this map, otherwise when the untag
* operation is waiting for a lock hold by other process and there are more
* sockets being closed than can fit in the socket buffer of the netlink socket
* that receives them, then the kernel will drop some of these sockets and we
* won't delete their tags.
* Map Key: uint64_t socket cookie
* Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
*/
bpf::BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
/*
* mUidCounterSetMap: Store the counterSet of a specific uid.
* Map Key: uint32 uid.
* Map Value: uint32 counterSet specifies if the traffic is a background
* or foreground traffic.
*/
bpf::BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
/*
* mAppUidStatsMap: Store the total traffic stats for a uid regardless of
* tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
* API to return persistent stats for a specific uid since device boot.
*/
bpf::BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
/*
* mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
* combination of uid, tag, iface and counterSet. These two maps contain
* both tagged and untagged traffic.
* Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
* information.
* Map Value: Stats, contains packet count and byte count of each
* transport protocol on egress and ingress direction.
*/
bpf::BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
bpf::BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex);
/*
* mIfaceIndexNameMap: Store the index name pair of each interface show up
* on the device since boot. The interface index is used by the eBPF program
* to correctly match the iface name when receiving a packet.
*/
bpf::BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
/*
* mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
* filter.
*/
bpf::BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
/*
* mConfigurationMap: Store the current network policy about uid filtering
* and the current stats map in use. There are two configuration entries in
* the map right now:
* - Entry with UID_RULES_CONFIGURATION_KEY:
* Store the configuration for the current uid rules. It indicates the device
* is in doze/powersave/standby/restricted/low power standby/oem deny mode.
* - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
* Stores the current live stats map that kernel program is writing to.
* Userspace can do scraping and cleaning job on the other one depending on the
* current configs.
*/
bpf::BpfMap<uint32_t, uint32_t> mConfigurationMap GUARDED_BY(mMutex);
/*
* mUidOwnerMap: Store uids that are used for bandwidth control uid match.
*/
bpf::BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex);
/*
* mUidOwnerMap: Store uids that are used for INTERNET permission check.
*/
bpf::BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex);
std::unique_ptr<netdutils::NetlinkListenerInterface> mSkDestroyListener;
netdutils::Status removeRule(uint32_t uid, UidOwnerMatchType match) REQUIRES(mMutex);
netdutils::Status addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif = 0)
REQUIRES(mMutex);
std::mutex mMutex;
netdutils::Status initMaps() EXCLUDES(mMutex);
// Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
// need to call back to system server for permission check.
std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex);
// For testing
friend class TrafficControllerTest;
};
} // namespace net
} // namespace android
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment