blob: 2b4bc375630192f82910a0ae9f7d77ea3e27633b [file] [log] [blame]
Wayne Ma790c83e2022-01-13 10:35:05 +08001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "TrafficControllerJni"
18
Patrick Rohr313bc6c2022-01-31 15:51:09 +010019#include "TrafficController.h"
20
21#include <bpf_shared.h>
Wayne Ma790c83e2022-01-13 10:35:05 +080022#include <jni.h>
Patrick Rohr313bc6c2022-01-31 15:51:09 +010023#include <log/log.h>
Wayne Ma790c83e2022-01-13 10:35:05 +080024#include <nativehelper/JNIHelp.h>
25#include <nativehelper/ScopedUtfChars.h>
26#include <nativehelper/ScopedPrimitiveArray.h>
Ken Chene6d511f2022-01-25 11:10:42 +080027#include <netjniutils/netjniutils.h>
Wayne Ma790c83e2022-01-13 10:35:05 +080028#include <net/if.h>
Maciej Żenczykowski990635c2022-07-27 08:04:33 +000029#include <private/android_filesystem_config.h>
30#include <unistd.h>
Wayne Ma790c83e2022-01-13 10:35:05 +080031#include <vector>
32
Wayne Ma790c83e2022-01-13 10:35:05 +080033
34using android::net::TrafficController;
35using android::netdutils::Status;
36
37using UidOwnerMatchType::PENALTY_BOX_MATCH;
38using UidOwnerMatchType::HAPPY_BOX_MATCH;
39
40static android::net::TrafficController mTc;
41
42namespace android {
43
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000044#define CHECK_LOG(status) \
45 do { \
46 if (!isOk(status)) \
47 ALOGE("%s failed, error code = %d", __func__, status.code()); \
48 } while (0)
49
50static void native_init(JNIEnv* env, jclass clazz) {
Patrick Rohr2b1b2c72022-02-01 15:57:48 +010051 Status status = mTc.start();
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000052 CHECK_LOG(status);
Maciej Żenczykowski990635c2022-07-27 08:04:33 +000053 if (!isOk(status)) {
54 uid_t uid = getuid();
55 ALOGE("BpfNetMaps jni init failure as uid=%d", uid);
56 // TODO: Fix tests to not use this jni lib, so we can unconditionally abort()
57 if (uid == AID_SYSTEM || uid == AID_NETWORK_STACK) abort();
58 }
Wayne Ma790c83e2022-01-13 10:35:05 +080059}
60
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000061static jint native_addNaughtyApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080062 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
63 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
64 TrafficController::IptOp::IptOpInsert);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000065 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080066 return (jint)status.code();
67}
68
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000069static jint native_removeNaughtyApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080070 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
71 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
72 TrafficController::IptOp::IptOpDelete);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000073 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080074 return (jint)status.code();
75}
76
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000077static jint native_addNiceApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080078 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
79 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
80 TrafficController::IptOp::IptOpInsert);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000081 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080082 return (jint)status.code();
83}
84
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000085static jint native_removeNiceApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080086 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
87 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
88 TrafficController::IptOp::IptOpDelete);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000089 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080090 return (jint)status.code();
91}
92
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000093static jint native_replaceUidChain(JNIEnv* env, jobject self, jstring name, jboolean isAllowlist,
94 jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +080095 const ScopedUtfChars chainNameUtf8(env, name);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000096 if (chainNameUtf8.c_str() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +080097 const std::string chainName(chainNameUtf8.c_str());
98
99 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000100 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800101
102 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800103 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800104 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
105 int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000106 if (res) ALOGE("%s failed, error code = %d", __func__, res);
Wayne Ma790c83e2022-01-13 10:35:05 +0800107 return (jint)res;
108}
109
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000110static jint native_setUidRule(JNIEnv* env, jobject self, jint childChain, jint uid,
111 jint firewallRule) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800112 auto chain = static_cast<ChildChain>(childChain);
113 auto rule = static_cast<FirewallRule>(firewallRule);
Wayne Ma510c2f42022-02-15 14:36:07 +0800114 FirewallType fType = mTc.getFirewallType(chain);
Wayne Ma790c83e2022-01-13 10:35:05 +0800115
116 int res = mTc.changeUidOwnerRule(chain, uid, rule, fType);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000117 if (res) ALOGE("%s failed, error code = %d", __func__, res);
Wayne Ma790c83e2022-01-13 10:35:05 +0800118 return (jint)res;
119}
120
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000121static jint native_addUidInterfaceRules(JNIEnv* env, jobject self, jstring ifName,
122 jintArray jUids) {
Motomu Utsumib08654c2022-05-11 05:56:26 +0000123 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and ifIndex is
124 // set to 0.
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000125 int ifIndex = 0;
Motomu Utsumib08654c2022-05-11 05:56:26 +0000126 if (ifName != nullptr) {
127 const ScopedUtfChars ifNameUtf8(env, ifName);
128 const std::string interfaceName(ifNameUtf8.c_str());
129 ifIndex = if_nametoindex(interfaceName.c_str());
Wayne Ma790c83e2022-01-13 10:35:05 +0800130 }
Wayne Ma790c83e2022-01-13 10:35:05 +0800131
132 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000133 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800134
135 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800136 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800137 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
138 Status status = mTc.addUidInterfaceRules(ifIndex, data);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000139 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +0800140 return (jint)status.code();
141}
142
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000143static jint native_removeUidInterfaceRules(JNIEnv* env, jobject self, jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800144 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000145 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800146
147 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800148 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800149 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
150 Status status = mTc.removeUidInterfaceRules(data);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000151 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +0800152 return (jint)status.code();
153}
154
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000155static jint native_updateUidLockdownRule(JNIEnv* env, jobject self, jint uid, jboolean add) {
156 Status status = mTc.updateUidLockdownRule(uid, add);
157 CHECK_LOG(status);
158 return (jint)status.code();
159}
160
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000161static jint native_swapActiveStatsMap(JNIEnv* env, jobject self) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800162 Status status = mTc.swapActiveStatsMap();
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000163 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +0800164 return (jint)status.code();
165}
166
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000167static void native_setPermissionForUids(JNIEnv* env, jobject self, jint permission,
168 jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800169 ScopedIntArrayRO uids(env, jUids);
170 if (uids.get() == nullptr) return;
171
172 size_t size = uids.size();
173 static_assert(sizeof(*(uids.get())) == sizeof(uid_t));
174 std::vector<uid_t> data ((uid_t *)&uids[0], (uid_t*)&uids[size]);
175 mTc.setPermissionForUids(permission, data);
176}
177
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000178static void native_dump(JNIEnv* env, jobject self, jobject javaFd, jboolean verbose) {
Ken Chene6d511f2022-01-25 11:10:42 +0800179 int fd = netjniutils::GetNativeFileDescriptor(env, javaFd);
180 if (fd < 0) {
181 jniThrowExceptionFmt(env, "java/io/IOException", "Invalid file descriptor");
182 return;
183 }
184 mTc.dump(fd, verbose);
185}
186
Wayne Ma790c83e2022-01-13 10:35:05 +0800187/*
188 * JNI registration.
189 */
190// clang-format off
191static const JNINativeMethod gMethods[] = {
192 /* name, signature, funcPtr */
193 {"native_init", "()V",
194 (void*)native_init},
195 {"native_addNaughtyApp", "(I)I",
196 (void*)native_addNaughtyApp},
197 {"native_removeNaughtyApp", "(I)I",
198 (void*)native_removeNaughtyApp},
199 {"native_addNiceApp", "(I)I",
200 (void*)native_addNiceApp},
201 {"native_removeNiceApp", "(I)I",
202 (void*)native_removeNiceApp},
Wayne Ma790c83e2022-01-13 10:35:05 +0800203 {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
204 (void*)native_replaceUidChain},
205 {"native_setUidRule", "(III)I",
206 (void*)native_setUidRule},
207 {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
208 (void*)native_addUidInterfaceRules},
209 {"native_removeUidInterfaceRules", "([I)I",
210 (void*)native_removeUidInterfaceRules},
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000211 {"native_updateUidLockdownRule", "(IZ)I",
212 (void*)native_updateUidLockdownRule},
Wayne Ma790c83e2022-01-13 10:35:05 +0800213 {"native_swapActiveStatsMap", "()I",
214 (void*)native_swapActiveStatsMap},
215 {"native_setPermissionForUids", "(I[I)V",
216 (void*)native_setPermissionForUids},
Ken Chene6d511f2022-01-25 11:10:42 +0800217 {"native_dump", "(Ljava/io/FileDescriptor;Z)V",
218 (void*)native_dump},
Wayne Ma790c83e2022-01-13 10:35:05 +0800219};
220// clang-format on
221
222int register_com_android_server_BpfNetMaps(JNIEnv* env) {
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000223 return jniRegisterNativeMethods(env, "com/android/server/BpfNetMaps",
224 gMethods, NELEM(gMethods));
Wayne Ma790c83e2022-01-13 10:35:05 +0800225}
226
227}; // namespace android