blob: 50a063507033bc6c9d3502011e3b3341c80e5d21 [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
Maciej Żenczykowski513474c2022-12-08 16:20:43 +000021#include "netd.h"
22
Wayne Ma790c83e2022-01-13 10:35:05 +080023#include <jni.h>
Patrick Rohr313bc6c2022-01-31 15:51:09 +010024#include <log/log.h>
Wayne Ma790c83e2022-01-13 10:35:05 +080025#include <nativehelper/JNIHelp.h>
26#include <nativehelper/ScopedUtfChars.h>
27#include <nativehelper/ScopedPrimitiveArray.h>
Ken Chene6d511f2022-01-25 11:10:42 +080028#include <netjniutils/netjniutils.h>
Wayne Ma790c83e2022-01-13 10:35:05 +080029#include <net/if.h>
Maciej Żenczykowski990635c2022-07-27 08:04:33 +000030#include <private/android_filesystem_config.h>
31#include <unistd.h>
Wayne Ma790c83e2022-01-13 10:35:05 +080032#include <vector>
33
Wayne Ma790c83e2022-01-13 10:35:05 +080034
35using android::net::TrafficController;
36using android::netdutils::Status;
37
38using UidOwnerMatchType::PENALTY_BOX_MATCH;
39using UidOwnerMatchType::HAPPY_BOX_MATCH;
40
41static android::net::TrafficController mTc;
42
43namespace android {
44
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000045#define CHECK_LOG(status) \
46 do { \
47 if (!isOk(status)) \
48 ALOGE("%s failed, error code = %d", __func__, status.code()); \
49 } while (0)
50
Motomu Utsumi3af8f0e2022-09-02 23:42:13 +090051static void native_init(JNIEnv* env, jclass clazz, jboolean startSkDestroyListener) {
52 Status status = mTc.start(startSkDestroyListener);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000053 CHECK_LOG(status);
Maciej Żenczykowski990635c2022-07-27 08:04:33 +000054 if (!isOk(status)) {
55 uid_t uid = getuid();
56 ALOGE("BpfNetMaps jni init failure as uid=%d", uid);
Maciej Żenczykowski9461bb12023-05-02 16:22:30 +000057 // We probably only ever get called from system_server (ie. AID_SYSTEM)
58 // or from tests, and never from network_stack (ie. AID_NETWORK_STACK).
59 // However, if we ever do add calls from production network_stack code
60 // we do want to make sure this initializes correctly.
Maciej Żenczykowski990635c2022-07-27 08:04:33 +000061 // TODO: Fix tests to not use this jni lib, so we can unconditionally abort()
62 if (uid == AID_SYSTEM || uid == AID_NETWORK_STACK) abort();
63 }
Wayne Ma790c83e2022-01-13 10:35:05 +080064}
65
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000066static jint native_addNaughtyApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080067 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
68 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
69 TrafficController::IptOp::IptOpInsert);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000070 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080071 return (jint)status.code();
72}
73
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000074static jint native_removeNaughtyApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080075 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
76 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
77 TrafficController::IptOp::IptOpDelete);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000078 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080079 return (jint)status.code();
80}
81
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000082static jint native_addNiceApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080083 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
84 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
85 TrafficController::IptOp::IptOpInsert);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000086 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080087 return (jint)status.code();
88}
89
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000090static jint native_removeNiceApp(JNIEnv* env, jobject self, jint uid) {
Wayne Ma790c83e2022-01-13 10:35:05 +080091 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
92 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
93 TrafficController::IptOp::IptOpDelete);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +000094 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +080095 return (jint)status.code();
96}
97
Motomu Utsumi114cd9c2022-08-01 02:08:35 +000098static jint native_setChildChain(JNIEnv* env, jobject self, jint childChain, jboolean enable) {
99 auto chain = static_cast<ChildChain>(childChain);
100 int res = mTc.toggleUidOwnerMap(chain, enable);
101 if (res) ALOGE("%s failed, error code = %d", __func__, res);
102 return (jint)res;
103}
104
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000105static jint native_replaceUidChain(JNIEnv* env, jobject self, jstring name, jboolean isAllowlist,
106 jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800107 const ScopedUtfChars chainNameUtf8(env, name);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000108 if (chainNameUtf8.c_str() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800109 const std::string chainName(chainNameUtf8.c_str());
110
111 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000112 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800113
114 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800115 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800116 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
117 int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000118 if (res) ALOGE("%s failed, error code = %d", __func__, res);
Wayne Ma790c83e2022-01-13 10:35:05 +0800119 return (jint)res;
120}
121
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000122static jint native_setUidRule(JNIEnv* env, jobject self, jint childChain, jint uid,
123 jint firewallRule) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800124 auto chain = static_cast<ChildChain>(childChain);
125 auto rule = static_cast<FirewallRule>(firewallRule);
Wayne Ma510c2f42022-02-15 14:36:07 +0800126 FirewallType fType = mTc.getFirewallType(chain);
Wayne Ma790c83e2022-01-13 10:35:05 +0800127
128 int res = mTc.changeUidOwnerRule(chain, uid, rule, fType);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000129 if (res) ALOGE("%s failed, error code = %d", __func__, res);
Wayne Ma790c83e2022-01-13 10:35:05 +0800130 return (jint)res;
131}
132
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000133static jint native_addUidInterfaceRules(JNIEnv* env, jobject self, jstring ifName,
134 jintArray jUids) {
Motomu Utsumib08654c2022-05-11 05:56:26 +0000135 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and ifIndex is
136 // set to 0.
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000137 int ifIndex = 0;
Motomu Utsumib08654c2022-05-11 05:56:26 +0000138 if (ifName != nullptr) {
139 const ScopedUtfChars ifNameUtf8(env, ifName);
140 const std::string interfaceName(ifNameUtf8.c_str());
141 ifIndex = if_nametoindex(interfaceName.c_str());
Wayne Ma790c83e2022-01-13 10:35:05 +0800142 }
Wayne Ma790c83e2022-01-13 10:35:05 +0800143
144 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.addUidInterfaceRules(ifIndex, 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
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000155static jint native_removeUidInterfaceRules(JNIEnv* env, jobject self, jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800156 ScopedIntArrayRO uids(env, jUids);
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000157 if (uids.get() == nullptr) return -EINVAL;
Wayne Ma790c83e2022-01-13 10:35:05 +0800158
159 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800160 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800161 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
162 Status status = mTc.removeUidInterfaceRules(data);
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
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000167static jint native_updateUidLockdownRule(JNIEnv* env, jobject self, jint uid, jboolean add) {
168 Status status = mTc.updateUidLockdownRule(uid, add);
169 CHECK_LOG(status);
170 return (jint)status.code();
171}
172
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000173static jint native_swapActiveStatsMap(JNIEnv* env, jobject self) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800174 Status status = mTc.swapActiveStatsMap();
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000175 CHECK_LOG(status);
Wayne Ma790c83e2022-01-13 10:35:05 +0800176 return (jint)status.code();
177}
178
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000179static void native_setPermissionForUids(JNIEnv* env, jobject self, jint permission,
180 jintArray jUids) {
Wayne Ma790c83e2022-01-13 10:35:05 +0800181 ScopedIntArrayRO uids(env, jUids);
182 if (uids.get() == nullptr) return;
183
184 size_t size = uids.size();
185 static_assert(sizeof(*(uids.get())) == sizeof(uid_t));
186 std::vector<uid_t> data ((uid_t *)&uids[0], (uid_t*)&uids[size]);
187 mTc.setPermissionForUids(permission, data);
188}
189
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000190static jint native_synchronizeKernelRCU(JNIEnv* env, jobject self) {
191 return -bpf::synchronizeKernelRCU();
192}
193
Wayne Ma790c83e2022-01-13 10:35:05 +0800194/*
195 * JNI registration.
196 */
197// clang-format off
198static const JNINativeMethod gMethods[] = {
199 /* name, signature, funcPtr */
Motomu Utsumi3af8f0e2022-09-02 23:42:13 +0900200 {"native_init", "(Z)V",
Wayne Ma790c83e2022-01-13 10:35:05 +0800201 (void*)native_init},
202 {"native_addNaughtyApp", "(I)I",
203 (void*)native_addNaughtyApp},
204 {"native_removeNaughtyApp", "(I)I",
205 (void*)native_removeNaughtyApp},
206 {"native_addNiceApp", "(I)I",
207 (void*)native_addNiceApp},
208 {"native_removeNiceApp", "(I)I",
209 (void*)native_removeNiceApp},
Motomu Utsumi114cd9c2022-08-01 02:08:35 +0000210 {"native_setChildChain", "(IZ)I",
211 (void*)native_setChildChain},
Wayne Ma790c83e2022-01-13 10:35:05 +0800212 {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
213 (void*)native_replaceUidChain},
214 {"native_setUidRule", "(III)I",
215 (void*)native_setUidRule},
216 {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
217 (void*)native_addUidInterfaceRules},
218 {"native_removeUidInterfaceRules", "([I)I",
219 (void*)native_removeUidInterfaceRules},
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000220 {"native_updateUidLockdownRule", "(IZ)I",
221 (void*)native_updateUidLockdownRule},
Wayne Ma790c83e2022-01-13 10:35:05 +0800222 {"native_swapActiveStatsMap", "()I",
223 (void*)native_swapActiveStatsMap},
224 {"native_setPermissionForUids", "(I[I)V",
225 (void*)native_setPermissionForUids},
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000226 {"native_synchronizeKernelRCU", "()I",
227 (void*)native_synchronizeKernelRCU},
Wayne Ma790c83e2022-01-13 10:35:05 +0800228};
229// clang-format on
230
231int register_com_android_server_BpfNetMaps(JNIEnv* env) {
Mark53e71c32023-01-13 07:00:25 +0000232 return jniRegisterNativeMethods(env, "android/net/connectivity/com/android/server/BpfNetMaps",
Maciej Żenczykowski932ef5b2022-05-24 13:36:20 +0000233 gMethods, NELEM(gMethods));
Wayne Ma790c83e2022-01-13 10:35:05 +0800234}
235
236}; // namespace android