blob: e7b5522a8e6db9b3fbeeb89723f92e9a377fe641 [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>
27#include <net/if.h>
28#include <vector>
29
Wayne Ma790c83e2022-01-13 10:35:05 +080030
31using android::net::TrafficController;
32using android::netdutils::Status;
33
34using UidOwnerMatchType::PENALTY_BOX_MATCH;
35using UidOwnerMatchType::HAPPY_BOX_MATCH;
36
37static android::net::TrafficController mTc;
38
39namespace android {
40
41static void native_init(JNIEnv* env, jobject clazz) {
Patrick Rohr2b1b2c72022-02-01 15:57:48 +010042 Status status = mTc.start();
Wayne Ma790c83e2022-01-13 10:35:05 +080043 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010044 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080045 }
46}
47
48static jint native_addNaughtyApp(JNIEnv* env, jobject clazz, jint uid) {
49 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
50 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
51 TrafficController::IptOp::IptOpInsert);
52 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010053 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080054 }
55 return (jint)status.code();
56}
57
58static jint native_removeNaughtyApp(JNIEnv* env, jobject clazz, jint uid) {
59 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
60 Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH,
61 TrafficController::IptOp::IptOpDelete);
62 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010063 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080064 }
65 return (jint)status.code();
66}
67
68static jint native_addNiceApp(JNIEnv* env, jobject clazz, jint uid) {
69 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
70 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
71 TrafficController::IptOp::IptOpInsert);
72 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010073 ALOGE("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080074 }
75 return (jint)status.code();
76}
77
78static jint native_removeNiceApp(JNIEnv* env, jobject clazz, jint uid) {
79 const uint32_t appUids = static_cast<uint32_t>(abs(uid));
80 Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH,
81 TrafficController::IptOp::IptOpDelete);
82 if (!isOk(status)) {
Patrick Rohr83fb6742022-02-01 16:58:57 +010083 ALOGD("%s failed, error code = %d", __func__, status.code());
Wayne Ma790c83e2022-01-13 10:35:05 +080084 }
85 return (jint)status.code();
86}
87
88static jint native_setChildChain(JNIEnv* env, jobject clazz, jint childChain, jboolean enable) {
89 auto chain = static_cast<ChildChain>(childChain);
90 int res = mTc.toggleUidOwnerMap(chain, enable);
91 if (res) {
92 ALOGE("%s failed, error code = %d", __func__, res);
93 }
94 return (jint)res;
95}
96
97static jint native_replaceUidChain(JNIEnv* env, jobject clazz, jstring name, jboolean isAllowlist,
98 jintArray jUids) {
99 const ScopedUtfChars chainNameUtf8(env, name);
100 if (chainNameUtf8.c_str() == nullptr) {
101 return -EINVAL;
102 }
103 const std::string chainName(chainNameUtf8.c_str());
104
105 ScopedIntArrayRO uids(env, jUids);
106 if (uids.get() == nullptr) {
107 return -EINVAL;
108 }
109
110 size_t size = uids.size();
111 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
112 int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data);
113 if (res) {
114 ALOGE("%s failed, error code = %d", __func__, res);
115 }
116 return (jint)res;
117}
118
Wayne Ma790c83e2022-01-13 10:35:05 +0800119static jint native_setUidRule(JNIEnv* env, jobject clazz, jint childChain, jint uid,
120 jint firewallRule) {
121 auto chain = static_cast<ChildChain>(childChain);
122 auto rule = static_cast<FirewallRule>(firewallRule);
Wayne Ma510c2f42022-02-15 14:36:07 +0800123 FirewallType fType = mTc.getFirewallType(chain);
Wayne Ma790c83e2022-01-13 10:35:05 +0800124
125 int res = mTc.changeUidOwnerRule(chain, uid, rule, fType);
126 if (res) {
127 ALOGE("%s failed, error code = %d", __func__, res);
128 }
129 return (jint)res;
130}
131
132static jint native_addUidInterfaceRules(JNIEnv* env, jobject clazz, jstring ifName,
133 jintArray jUids) {
134 const ScopedUtfChars ifNameUtf8(env, ifName);
135 if (ifNameUtf8.c_str() == nullptr) {
136 return -EINVAL;
137 }
138 const std::string interfaceName(ifNameUtf8.c_str());
139 const int ifIndex = if_nametoindex(interfaceName.c_str());
140
141 ScopedIntArrayRO uids(env, jUids);
142 if (uids.get() == nullptr) {
143 return -EINVAL;
144 }
145
146 size_t size = uids.size();
147 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
148 Status status = mTc.addUidInterfaceRules(ifIndex, data);
149 if (!isOk(status)) {
150 ALOGE("%s failed, error code = %d", __func__, status.code());
151 }
152 return (jint)status.code();
153}
154
155static jint native_removeUidInterfaceRules(JNIEnv* env, jobject clazz, jintArray jUids) {
156 ScopedIntArrayRO uids(env, jUids);
157 if (uids.get() == nullptr) {
158 return -EINVAL;
159 }
160
161 size_t size = uids.size();
162 std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]);
163 Status status = mTc.removeUidInterfaceRules(data);
164 if (!isOk(status)) {
165 ALOGE("%s failed, error code = %d", __func__, status.code());
166 }
167 return (jint)status.code();
168}
169
170static jint native_swapActiveStatsMap(JNIEnv* env, jobject clazz) {
171 Status status = mTc.swapActiveStatsMap();
172 if (!isOk(status)) {
173 ALOGD("%s failed, error code = %d", __func__, status.code());
174 }
175 return (jint)status.code();
176}
177
178static void native_setPermissionForUids(JNIEnv* env, jobject clazz, jint permission,
179 jintArray jUids) {
180 ScopedIntArrayRO uids(env, jUids);
181 if (uids.get() == nullptr) return;
182
183 size_t size = uids.size();
184 static_assert(sizeof(*(uids.get())) == sizeof(uid_t));
185 std::vector<uid_t> data ((uid_t *)&uids[0], (uid_t*)&uids[size]);
186 mTc.setPermissionForUids(permission, data);
187}
188
189static jint native_setCounterSet(JNIEnv* env, jobject clazz, jint setNum, jint uid) {
190 uid_t callingUid = getuid();
191 int res = mTc.setCounterSet(setNum, (uid_t)uid, callingUid);
192 if (res) {
193 ALOGE("%s failed, error code = %d", __func__, res);
194 }
195 return (jint)res;
196}
197
198static jint native_deleteTagData(JNIEnv* env, jobject clazz, jint tagNum, jint uid) {
199 uid_t callingUid = getuid();
200 int res = mTc.deleteTagData(tagNum, (uid_t)uid, callingUid);
201 if (res) {
202 ALOGE("%s failed, error code = %d", __func__, res);
203 }
204 return (jint)res;
205}
206
207/*
208 * JNI registration.
209 */
210// clang-format off
211static const JNINativeMethod gMethods[] = {
212 /* name, signature, funcPtr */
213 {"native_init", "()V",
214 (void*)native_init},
215 {"native_addNaughtyApp", "(I)I",
216 (void*)native_addNaughtyApp},
217 {"native_removeNaughtyApp", "(I)I",
218 (void*)native_removeNaughtyApp},
219 {"native_addNiceApp", "(I)I",
220 (void*)native_addNiceApp},
221 {"native_removeNiceApp", "(I)I",
222 (void*)native_removeNiceApp},
223 {"native_setChildChain", "(IZ)I",
224 (void*)native_setChildChain},
225 {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
226 (void*)native_replaceUidChain},
227 {"native_setUidRule", "(III)I",
228 (void*)native_setUidRule},
229 {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
230 (void*)native_addUidInterfaceRules},
231 {"native_removeUidInterfaceRules", "([I)I",
232 (void*)native_removeUidInterfaceRules},
233 {"native_swapActiveStatsMap", "()I",
234 (void*)native_swapActiveStatsMap},
235 {"native_setPermissionForUids", "(I[I)V",
236 (void*)native_setPermissionForUids},
237 {"native_setCounterSet", "(II)I",
238 (void*)native_setCounterSet},
239 {"native_deleteTagData", "(II)I",
240 (void*)native_deleteTagData},
241};
242// clang-format on
243
244int register_com_android_server_BpfNetMaps(JNIEnv* env) {
245 return jniRegisterNativeMethods(env,
246 "com/android/server/BpfNetMaps",
247 gMethods, NELEM(gMethods));
248}
249
250}; // namespace android