blob: f13c68d31091ca9dab6861764b23ab6341e3c5bd [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
42static void native_init(JNIEnv* env, jobject clazz) {
Patrick Rohr2b1b2c72022-02-01 15:57:48 +010043 Status status = mTc.start();
Wayne Ma790c83e2022-01-13 10:35:05 +080044 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010045 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080046 }
47}
48
49static jint native_addNaughtyApp(JNIEnv* env, jobject clazz, jint uid) {
50 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
51 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
52 TrafficController::IptOp::IptOpInsert);
53 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010054 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080055 }
56 return (jint)status.code();
57}
58
59static jint native_removeNaughtyApp(JNIEnv* env, jobject clazz, jint uid) {
60 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
61 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
62 TrafficController::IptOp::IptOpDelete);
63 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010064 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080065 }
66 return (jint)status.code();
67}
68
69static jint native_addNiceApp(JNIEnv* env, jobject clazz, jint uid) {
70 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
71 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
72 TrafficController::IptOp::IptOpInsert);
73 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010074 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080075 }
76 return (jint)status.code();
77}
78
79static jint native_removeNiceApp(JNIEnv* env, jobject clazz, jint uid) {
80 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
81 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
82 TrafficController::IptOp::IptOpDelete);
83 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010084 ALOGD("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080085 }
86 return (jint)status.code();
87}
88
89static jint native_setChildChain(JNIEnv* env, jobject clazz, jint childChain, jboolean enable) {
90 auto chain = static_cast<ChildChain>(childChain);
91 int res = mTc.toggleUidOwnerMap(chain, enable);
92 if (res) {
93 ALOGE("%s failed, error code = %d", __func__, res);
94 }
95 return (jint)res;
96}
97
98static jint native_replaceUidChain(JNIEnv* env, jobject clazz, jstring name, jboolean isAllowlist,
99 jintArray jUids) {
100 const ScopedUtfChars chainNameUtf8(env, name);
101 if (chainNameUtf8.c_str() == nullptr) {
102 return -EINVAL;
103 }
104 const std::string chainName(chainNameUtf8.c_str());
105
106 ScopedIntArrayRO uids(env, jUids);
107 if (uids.get() == nullptr) {
108 return -EINVAL;
109 }
110
111 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800112 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800113 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
114 int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data);
115 if (res) {
116 ALOGE("%s failed, error code = %d", __func__, res);
117 }
118 return (jint)res;
119}
120
Wayne Ma790c83e2022-01-13 10:35:05 +0800121static jint native_setUidRule(JNIEnv* env, jobject clazz, jint childChain, jint uid,
122 jint firewallRule) {
123 auto chain = static_cast<ChildChain>(childChain);
124 auto rule = static_cast<FirewallRule>(firewallRule);
Wayne Ma510c2f42022-02-15 14:36:07 +0800125 FirewallType fType = mTc.getFirewallType(chain);
Wayne Ma790c83e2022-01-13 10:35:05 +0800126
127 int res = mTc.changeUidOwnerRule(chain, uid, rule, fType);
128 if (res) {
129 ALOGE("%s failed, error code = %d", __func__, res);
130 }
131 return (jint)res;
132}
133
134static jint native_addUidInterfaceRules(JNIEnv* env, jobject clazz, jstring ifName,
135 jintArray jUids) {
136 const ScopedUtfChars ifNameUtf8(env, ifName);
137 if (ifNameUtf8.c_str() == nullptr) {
138 return -EINVAL;
139 }
140 const std::string interfaceName(ifNameUtf8.c_str());
141 const int ifIndex = if_nametoindex(interfaceName.c_str());
142
143 ScopedIntArrayRO uids(env, jUids);
144 if (uids.get() == nullptr) {
145 return -EINVAL;
146 }
147
148 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800149 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800150 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
151 Status status = mTc.addUidInterfaceRules(ifIndex, data);
152 if (!isOk(status)) {
153 ALOGE("%s failed, error code = %d", __func__, status.code());
154 }
155 return (jint)status.code();
156}
157
158static jint native_removeUidInterfaceRules(JNIEnv* env, jobject clazz, jintArray jUids) {
159 ScopedIntArrayRO uids(env, jUids);
160 if (uids.get() == nullptr) {
161 return -EINVAL;
162 }
163
164 size_t size = uids.size();
Wayne Ma55452912022-02-18 14:09:04 +0800165 static_assert(sizeof(*(uids.get())) == sizeof(int32_t));
Wayne Ma790c83e2022-01-13 10:35:05 +0800166 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
167 Status status = mTc.removeUidInterfaceRules(data);
168 if (!isOk(status)) {
169 ALOGE("%s failed, error code = %d", __func__, status.code());
170 }
171 return (jint)status.code();
172}
173
174static jint native_swapActiveStatsMap(JNIEnv* env, jobject clazz) {
175 Status status = mTc.swapActiveStatsMap();
176 if (!isOk(status)) {
177 ALOGD("%s failed, error code = %d", __func__, status.code());
178 }
179 return (jint)status.code();
180}
181
182static void native_setPermissionForUids(JNIEnv* env, jobject clazz, jint permission,
183 jintArray jUids) {
184 ScopedIntArrayRO uids(env, jUids);
185 if (uids.get() == nullptr) return;
186
187 size_t size = uids.size();
188 static_assert(sizeof(*(uids.get())) == sizeof(uid_t));
189 std::vector<uid_t> data ((uid_t *)&uids[0], (uid_t*)&uids[size]);
190 mTc.setPermissionForUids(permission, data);
191}
192
Ken Chene6d511f2022-01-25 11:10:42 +0800193static void native_dump(JNIEnv* env, jobject clazz, jobject javaFd, jboolean verbose) {
194 int fd = netjniutils::GetNativeFileDescriptor(env, javaFd);
195 if (fd < 0) {
196 jniThrowExceptionFmt(env, "java/io/IOException", "Invalid file descriptor");
197 return;
198 }
199 mTc.dump(fd, verbose);
200}
201
Wayne Ma790c83e2022-01-13 10:35:05 +0800202/*
203 * JNI registration.
204 */
205// clang-format off
206static const JNINativeMethod gMethods[] = {
207 /* name, signature, funcPtr */
208 {"native_init", "()V",
209 (void*)native_init},
210 {"native_addNaughtyApp", "(I)I",
211 (void*)native_addNaughtyApp},
212 {"native_removeNaughtyApp", "(I)I",
213 (void*)native_removeNaughtyApp},
214 {"native_addNiceApp", "(I)I",
215 (void*)native_addNiceApp},
216 {"native_removeNiceApp", "(I)I",
217 (void*)native_removeNiceApp},
218 {"native_setChildChain", "(IZ)I",
219 (void*)native_setChildChain},
220 {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
221 (void*)native_replaceUidChain},
222 {"native_setUidRule", "(III)I",
223 (void*)native_setUidRule},
224 {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
225 (void*)native_addUidInterfaceRules},
226 {"native_removeUidInterfaceRules", "([I)I",
227 (void*)native_removeUidInterfaceRules},
228 {"native_swapActiveStatsMap", "()I",
229 (void*)native_swapActiveStatsMap},
230 {"native_setPermissionForUids", "(I[I)V",
231 (void*)native_setPermissionForUids},
Ken Chene6d511f2022-01-25 11:10:42 +0800232 {"native_dump", "(Ljava/io/FileDescriptor;Z)V",
233 (void*)native_dump},
Wayne Ma790c83e2022-01-13 10:35:05 +0800234};
235// clang-format on
236
237int register_com_android_server_BpfNetMaps(JNIEnv* env) {
238 return jniRegisterNativeMethods(env,
239 "com/android/server/BpfNetMaps",
240 gMethods, NELEM(gMethods));
241}
242
243}; // namespace android