blob: 63961cb283eb8a1cc9240a6ee41a866bf2fba258 [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();
112 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
113 int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data);
114 if (res) {
115 ALOGE("%s failed, error code = %d", __func__, res);
116 }
117 return (jint)res;
118}
119
Wayne Ma790c83e2022-01-13 10:35:05 +0800120static jint native_setUidRule(JNIEnv* env, jobject clazz, jint childChain, jint uid,
121 jint firewallRule) {
122 auto chain = static_cast<ChildChain>(childChain);
123 auto rule = static_cast<FirewallRule>(firewallRule);
Wayne Ma510c2f42022-02-15 14:36:07 +0800124 FirewallType fType = mTc.getFirewallType(chain);
Wayne Ma790c83e2022-01-13 10:35:05 +0800125
126 int res = mTc.changeUidOwnerRule(chain, uid, rule, fType);
127 if (res) {
128 ALOGE("%s failed, error code = %d", __func__, res);
129 }
130 return (jint)res;
131}
132
133static jint native_addUidInterfaceRules(JNIEnv* env, jobject clazz, jstring ifName,
134 jintArray jUids) {
135 const ScopedUtfChars ifNameUtf8(env, ifName);
136 if (ifNameUtf8.c_str() == nullptr) {
137 return -EINVAL;
138 }
139 const std::string interfaceName(ifNameUtf8.c_str());
140 const int ifIndex = if_nametoindex(interfaceName.c_str());
141
142 ScopedIntArrayRO uids(env, jUids);
143 if (uids.get() == nullptr) {
144 return -EINVAL;
145 }
146
147 size_t size = uids.size();
148 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
149 Status status = mTc.addUidInterfaceRules(ifIndex, data);
150 if (!isOk(status)) {
151 ALOGE("%s failed, error code = %d", __func__, status.code());
152 }
153 return (jint)status.code();
154}
155
156static jint native_removeUidInterfaceRules(JNIEnv* env, jobject clazz, jintArray jUids) {
157 ScopedIntArrayRO uids(env, jUids);
158 if (uids.get() == nullptr) {
159 return -EINVAL;
160 }
161
162 size_t size = uids.size();
163 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
164 Status status = mTc.removeUidInterfaceRules(data);
165 if (!isOk(status)) {
166 ALOGE("%s failed, error code = %d", __func__, status.code());
167 }
168 return (jint)status.code();
169}
170
171static jint native_swapActiveStatsMap(JNIEnv* env, jobject clazz) {
172 Status status = mTc.swapActiveStatsMap();
173 if (!isOk(status)) {
174 ALOGD("%s failed, error code = %d", __func__, status.code());
175 }
176 return (jint)status.code();
177}
178
179static void native_setPermissionForUids(JNIEnv* env, jobject clazz, jint permission,
180 jintArray jUids) {
181 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
Ken Chene6d511f2022-01-25 11:10:42 +0800190static void native_dump(JNIEnv* env, jobject clazz, jobject javaFd, jboolean verbose) {
191 int fd = netjniutils::GetNativeFileDescriptor(env, javaFd);
192 if (fd < 0) {
193 jniThrowExceptionFmt(env, "java/io/IOException", "Invalid file descriptor");
194 return;
195 }
196 mTc.dump(fd, verbose);
197}
198
Wayne Ma790c83e2022-01-13 10:35:05 +0800199/*
200 * JNI registration.
201 */
202// clang-format off
203static const JNINativeMethod gMethods[] = {
204 /* name, signature, funcPtr */
205 {"native_init", "()V",
206 (void*)native_init},
207 {"native_addNaughtyApp", "(I)I",
208 (void*)native_addNaughtyApp},
209 {"native_removeNaughtyApp", "(I)I",
210 (void*)native_removeNaughtyApp},
211 {"native_addNiceApp", "(I)I",
212 (void*)native_addNiceApp},
213 {"native_removeNiceApp", "(I)I",
214 (void*)native_removeNiceApp},
215 {"native_setChildChain", "(IZ)I",
216 (void*)native_setChildChain},
217 {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
218 (void*)native_replaceUidChain},
219 {"native_setUidRule", "(III)I",
220 (void*)native_setUidRule},
221 {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
222 (void*)native_addUidInterfaceRules},
223 {"native_removeUidInterfaceRules", "([I)I",
224 (void*)native_removeUidInterfaceRules},
225 {"native_swapActiveStatsMap", "()I",
226 (void*)native_swapActiveStatsMap},
227 {"native_setPermissionForUids", "(I[I)V",
228 (void*)native_setPermissionForUids},
Ken Chene6d511f2022-01-25 11:10:42 +0800229 {"native_dump", "(Ljava/io/FileDescriptor;Z)V",
230 (void*)native_dump},
Wayne Ma790c83e2022-01-13 10:35:05 +0800231};
232// clang-format on
233
234int register_com_android_server_BpfNetMaps(JNIEnv* env) {
235 return jniRegisterNativeMethods(env,
236 "com/android/server/BpfNetMaps",
237 gMethods, NELEM(gMethods));
238}
239
240}; // namespace android