blob: 85cfc098779e63707e0e6433c7e590792110a2d6 [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
Wayne Ma790c83e2022-01-13 10:35:05 +0800189/*
190 * JNI registration.
191 */
192// clang-format off
193static const JNINativeMethod gMethods[] = {
194 /* name, signature, funcPtr */
195 {"native_init", "()V",
196 (void*)native_init},
197 {"native_addNaughtyApp", "(I)I",
198 (void*)native_addNaughtyApp},
199 {"native_removeNaughtyApp", "(I)I",
200 (void*)native_removeNaughtyApp},
201 {"native_addNiceApp", "(I)I",
202 (void*)native_addNiceApp},
203 {"native_removeNiceApp", "(I)I",
204 (void*)native_removeNiceApp},
205 {"native_setChildChain", "(IZ)I",
206 (void*)native_setChildChain},
207 {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I",
208 (void*)native_replaceUidChain},
209 {"native_setUidRule", "(III)I",
210 (void*)native_setUidRule},
211 {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I",
212 (void*)native_addUidInterfaceRules},
213 {"native_removeUidInterfaceRules", "([I)I",
214 (void*)native_removeUidInterfaceRules},
215 {"native_swapActiveStatsMap", "()I",
216 (void*)native_swapActiveStatsMap},
217 {"native_setPermissionForUids", "(I[I)V",
218 (void*)native_setPermissionForUids},
Wayne Ma790c83e2022-01-13 10:35:05 +0800219};
220// clang-format on
221
222int register_com_android_server_BpfNetMaps(JNIEnv* env) {
223 return jniRegisterNativeMethods(env,
224 "com/android/server/BpfNetMaps",
225 gMethods, NELEM(gMethods));
226}
227
228}; // namespace android