blob: bc70c93469f3a1942e2eb917331b1d3ce5cc7760 [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>
29#include <vector>
30
Wayne Ma790c83e2022-01-13 10:35:05 +080031
32using android::net::TrafficController;
33using android::netdutils::Status;
34
35using UidOwnerMatchType::PENALTY_BOX_MATCH;
36using UidOwnerMatchType::HAPPY_BOX_MATCH;
37
38static android::net::TrafficController mTc;
39
40namespace android {
41
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000042#define CHECK_LOG(status) \
43 do { \
44 if (!isOk(status)) \
45 ALOGE("%s failed, error code = %d", __func__, status.code()); \
46 } while (0)
47
48static void native_init(JNIEnv* env, jclass clazz) {
Patrick Rohr2b1b2c72022-02-01 15:57:48 +010049 Status status = mTc.start();
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000050 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080051}
52
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000053static jint native_addNaughtyApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080054 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
55 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
56 TrafficController::IptOp::IptOpInsert);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000057 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080058 return (jint)status.code();
59}
60
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000061static jint native_removeNaughtyApp(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::IptOpDelete);
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_addNiceApp(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, HAPPY_BOX_MATCH,
72 TrafficController::IptOp::IptOpInsert);
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_removeNiceApp(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::IptOpDelete);
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_setChildChain(JNIEnv* env, jobject self, jint childChain, jboolean enable) {
Wayne Ma790c83e2022-01-13 10:35:05 +080086 auto chain = static_cast<ChildChain>(childChain);
87 int res = mTc.toggleUidOwnerMap(chain, enable);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000088 if (res) ALOGE("%s failed, error code = %d", __func__, res);
Wayne Ma790c83e2022-01-13 10:35:05 +080089 return (jint)res;
90}
91
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000092static jint native_replaceUidChain(JNIEnv* env, jobject self, jstring name, jboolean isAllowlist,
93 jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +080094 const ScopedUtfChars chainNameUtf8(env, name);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000095 if (chainNameUtf8.c_str() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +080096 const std::string chainName(chainNameUtf8.c_str());
97
98 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000099 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800100
101 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800102 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800103 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
104 int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000105 if (res) ALOGE("%s failed, error code = %d", __func__, res);
Wayne Ma790c83e2022-01-13 10:35:05 +0800106 return (jint)res;
107}
108
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000109static jint native_setUidRule(JNIEnv* env, jobject self, jint childChain, jint uid,
110 jint firewallRule) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800111 auto chain = static_cast<ChildChain>(childChain);
112 auto rule = static_cast<FirewallRule>(firewallRule);
Wayne Ma510c2f42022-02-15 14:36:07 +0800113 FirewallType fType = mTc.getFirewallType(chain);
Wayne Ma790c83e2022-01-13 10:35:05 +0800114
115 int res = mTc.changeUidOwnerRule(chain, uid, rule, fType);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000116 if (res) ALOGE("%s failed, error code = %d", __func__, res);
Wayne Ma790c83e2022-01-13 10:35:05 +0800117 return (jint)res;
118}
119
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000120static jint native_addUidInterfaceRules(JNIEnv* env, jobject self, jstring ifName,
121 jintArray jUids) {
Motomu Utsumib08654c2022-05-11 05:56:26 +0000122 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and ifIndex is
123 // set to 0.
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000124 int ifIndex = 0;
Motomu Utsumib08654c2022-05-11 05:56:26 +0000125 if (ifName != nullptr) {
126 const ScopedUtfChars ifNameUtf8(env, ifName);
127 const std::string interfaceName(ifNameUtf8.c_str());
128 ifIndex = if_nametoindex(interfaceName.c_str());
Wayne Ma790c83e2022-01-13 10:35:05 +0800129 }
Wayne Ma790c83e2022-01-13 10:35:05 +0800130
131 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000132 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800133
134 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800135 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800136 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
137 Status status = mTc.addUidInterfaceRules(ifIndex, data);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000138 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +0800139 return (jint)status.code();
140}
141
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000142static jint native_removeUidInterfaceRules(JNIEnv* env, jobject self, jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800143 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000144 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800145
146 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800147 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800148 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
149 Status status = mTc.removeUidInterfaceRules(data);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000150 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +0800151 return (jint)status.code();
152}
153
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000154static jint native_swapActiveStatsMap(JNIEnv* env, jobject self) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800155 Status status = mTc.swapActiveStatsMap();
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000156 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +0800157 return (jint)status.code();
158}
159
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000160static void native_setPermissionForUids(JNIEnv* env, jobject self, jint permission,
161 jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800162 ScopedIntArrayRO uids(env, jUids);
163 if (uids.get() == nullptr) return;
164
165 size_t size = uids.size();
166 static_assert(sizeof(*(uids.get())) == sizeof(uid_t));
167 std::vector<uid_t> data ((uid_t *)&uids[0], (uid_t*)&uids[size]);
168 mTc.setPermissionForUids(permission, data);
169}
170
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000171static void native_dump(JNIEnv* env, jobject self, jobject javaFd, jboolean verbose) {
Ken Chene6d511f2022-01-25 11:10:42 +0800172 int fd = netjniutils::GetNativeFileDescriptor(env, javaFd);
173 if (fd < 0) {
174 jniThrowExceptionFmt(env, "java/io/IOException", "Invalid file descriptor");
175 return;
176 }
177 mTc.dump(fd, verbose);
178}
179
Wayne Ma790c83e2022-01-13 10:35:05 +0800180/*
181 * JNI registration.
182 */
183// clang-format off
184static const JNINativeMethod gMethods[] = {
185 /* name, signature, funcPtr */
186 {"native_init", "()V",
187 (void*)native_init},
188 {"native_addNaughtyApp", "(I)I",
189 (void*)native_addNaughtyApp},
190 {"native_removeNaughtyApp", "(I)I",
191 (void*)native_removeNaughtyApp},
192 {"native_addNiceApp", "(I)I",
193 (void*)native_addNiceApp},
194 {"native_removeNiceApp", "(I)I",
195 (void*)native_removeNiceApp},
196 {"native_setChildChain", "(IZ)I",
197 (void*)native_setChildChain},
198 {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
199 (void*)native_replaceUidChain},
200 {"native_setUidRule", "(III)I",
201 (void*)native_setUidRule},
202 {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
203 (void*)native_addUidInterfaceRules},
204 {"native_removeUidInterfaceRules", "([I)I",
205 (void*)native_removeUidInterfaceRules},
206 {"native_swapActiveStatsMap", "()I",
207 (void*)native_swapActiveStatsMap},
208 {"native_setPermissionForUids", "(I[I)V",
209 (void*)native_setPermissionForUids},
Ken Chene6d511f2022-01-25 11:10:42 +0800210 {"native_dump", "(Ljava/io/FileDescriptor;Z)V",
211 (void*)native_dump},
Wayne Ma790c83e2022-01-13 10:35:05 +0800212};
213// clang-format on
214
215int register_com_android_server_BpfNetMaps(JNIEnv* env) {
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000216 return jniRegisterNativeMethods(env, "com/android/server/BpfNetMaps",
217 gMethods, NELEM(gMethods));
Wayne Ma790c83e2022-01-13 10:35:05 +0800218}
219
220}; // namespace android