Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 1 | /* |
| 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 Rohr | 313bc6c | 2022-01-31 15:51:09 +0100 | [diff] [blame] | 19 | #include "TrafficController.h" |
| 20 | |
| 21 | #include <bpf_shared.h> |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 22 | #include <jni.h> |
Patrick Rohr | 313bc6c | 2022-01-31 15:51:09 +0100 | [diff] [blame] | 23 | #include <log/log.h> |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 24 | #include <nativehelper/JNIHelp.h> |
| 25 | #include <nativehelper/ScopedUtfChars.h> |
| 26 | #include <nativehelper/ScopedPrimitiveArray.h> |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 27 | #include <netjniutils/netjniutils.h> |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 28 | #include <net/if.h> |
| 29 | #include <vector> |
| 30 | |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 31 | |
| 32 | using android::net::TrafficController; |
| 33 | using android::netdutils::Status; |
| 34 | |
| 35 | using UidOwnerMatchType::PENALTY_BOX_MATCH; |
| 36 | using UidOwnerMatchType::HAPPY_BOX_MATCH; |
| 37 | |
| 38 | static android::net::TrafficController mTc; |
| 39 | |
| 40 | namespace android { |
| 41 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 42 | #define CHECK_LOG(status) \ |
| 43 | do { \ |
| 44 | if (!isOk(status)) \ |
| 45 | ALOGE("%s failed, error code = %d", __func__, status.code()); \ |
| 46 | } while (0) |
| 47 | |
| 48 | static void native_init(JNIEnv* env, jclass clazz) { |
Patrick Rohr | 2b1b2c7 | 2022-02-01 15:57:48 +0100 | [diff] [blame] | 49 | Status status = mTc.start(); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 50 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 53 | static jint native_addNaughtyApp(JNIEnv* env, jobject self, jint uid) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 54 | const uint32_t appUids = static_cast<uint32_t>(abs(uid)); |
| 55 | Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH, |
| 56 | TrafficController::IptOp::IptOpInsert); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 57 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 58 | return (jint)status.code(); |
| 59 | } |
| 60 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 61 | static jint native_removeNaughtyApp(JNIEnv* env, jobject self, jint uid) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 62 | const uint32_t appUids = static_cast<uint32_t>(abs(uid)); |
| 63 | Status status = mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH, |
| 64 | TrafficController::IptOp::IptOpDelete); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 65 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 66 | return (jint)status.code(); |
| 67 | } |
| 68 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 69 | static jint native_addNiceApp(JNIEnv* env, jobject self, jint uid) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 70 | const uint32_t appUids = static_cast<uint32_t>(abs(uid)); |
| 71 | Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, |
| 72 | TrafficController::IptOp::IptOpInsert); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 73 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 74 | return (jint)status.code(); |
| 75 | } |
| 76 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 77 | static jint native_removeNiceApp(JNIEnv* env, jobject self, jint uid) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 78 | const uint32_t appUids = static_cast<uint32_t>(abs(uid)); |
| 79 | Status status = mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, |
| 80 | TrafficController::IptOp::IptOpDelete); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 81 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 82 | return (jint)status.code(); |
| 83 | } |
| 84 | |
Motomu Utsumi | 114cd9c | 2022-08-01 02:08:35 +0000 | [diff] [blame] | 85 | static jint native_setChildChain(JNIEnv* env, jobject self, jint childChain, jboolean enable) { |
| 86 | auto chain = static_cast<ChildChain>(childChain); |
| 87 | int res = mTc.toggleUidOwnerMap(chain, enable); |
| 88 | if (res) ALOGE("%s failed, error code = %d", __func__, res); |
| 89 | return (jint)res; |
| 90 | } |
| 91 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 92 | static jint native_replaceUidChain(JNIEnv* env, jobject self, jstring name, jboolean isAllowlist, |
| 93 | jintArray jUids) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 94 | const ScopedUtfChars chainNameUtf8(env, name); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 95 | if (chainNameUtf8.c_str() == nullptr) return -EINVAL; |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 96 | const std::string chainName(chainNameUtf8.c_str()); |
| 97 | |
| 98 | ScopedIntArrayRO uids(env, jUids); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 99 | if (uids.get() == nullptr) return -EINVAL; |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 100 | |
| 101 | size_t size = uids.size(); |
Wayne Ma | 5545291 | 2022-02-18 14:09:04 +0800 | [diff] [blame] | 102 | static_assert(sizeof(*(uids.get())) == sizeof(int32_t)); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 103 | std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]); |
| 104 | int res = mTc.replaceUidOwnerMap(chainName, isAllowlist, data); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 105 | if (res) ALOGE("%s failed, error code = %d", __func__, res); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 106 | return (jint)res; |
| 107 | } |
| 108 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 109 | static jint native_setUidRule(JNIEnv* env, jobject self, jint childChain, jint uid, |
| 110 | jint firewallRule) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 111 | auto chain = static_cast<ChildChain>(childChain); |
| 112 | auto rule = static_cast<FirewallRule>(firewallRule); |
Wayne Ma | 510c2f4 | 2022-02-15 14:36:07 +0800 | [diff] [blame] | 113 | FirewallType fType = mTc.getFirewallType(chain); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 114 | |
| 115 | int res = mTc.changeUidOwnerRule(chain, uid, rule, fType); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 116 | if (res) ALOGE("%s failed, error code = %d", __func__, res); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 117 | return (jint)res; |
| 118 | } |
| 119 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 120 | static jint native_addUidInterfaceRules(JNIEnv* env, jobject self, jstring ifName, |
| 121 | jintArray jUids) { |
Motomu Utsumi | b08654c | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 122 | // Null ifName is a wildcard to allow apps to receive packets on all interfaces and ifIndex is |
| 123 | // set to 0. |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 124 | int ifIndex = 0; |
Motomu Utsumi | b08654c | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 125 | if (ifName != nullptr) { |
| 126 | const ScopedUtfChars ifNameUtf8(env, ifName); |
| 127 | const std::string interfaceName(ifNameUtf8.c_str()); |
| 128 | ifIndex = if_nametoindex(interfaceName.c_str()); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 129 | } |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 130 | |
| 131 | ScopedIntArrayRO uids(env, jUids); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 132 | if (uids.get() == nullptr) return -EINVAL; |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 133 | |
| 134 | size_t size = uids.size(); |
Wayne Ma | 5545291 | 2022-02-18 14:09:04 +0800 | [diff] [blame] | 135 | static_assert(sizeof(*(uids.get())) == sizeof(int32_t)); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 136 | std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]); |
| 137 | Status status = mTc.addUidInterfaceRules(ifIndex, data); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 138 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 139 | return (jint)status.code(); |
| 140 | } |
| 141 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 142 | static jint native_removeUidInterfaceRules(JNIEnv* env, jobject self, jintArray jUids) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 143 | ScopedIntArrayRO uids(env, jUids); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 144 | if (uids.get() == nullptr) return -EINVAL; |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 145 | |
| 146 | size_t size = uids.size(); |
Wayne Ma | 5545291 | 2022-02-18 14:09:04 +0800 | [diff] [blame] | 147 | static_assert(sizeof(*(uids.get())) == sizeof(int32_t)); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 148 | std::vector<int32_t> data ((int32_t *)&uids[0], (int32_t*)&uids[size]); |
| 149 | Status status = mTc.removeUidInterfaceRules(data); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 150 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 151 | return (jint)status.code(); |
| 152 | } |
| 153 | |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 154 | static jint native_updateUidLockdownRule(JNIEnv* env, jobject self, jint uid, jboolean add) { |
| 155 | Status status = mTc.updateUidLockdownRule(uid, add); |
| 156 | CHECK_LOG(status); |
| 157 | return (jint)status.code(); |
| 158 | } |
| 159 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 160 | static jint native_swapActiveStatsMap(JNIEnv* env, jobject self) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 161 | Status status = mTc.swapActiveStatsMap(); |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 162 | CHECK_LOG(status); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 163 | return (jint)status.code(); |
| 164 | } |
| 165 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 166 | static void native_setPermissionForUids(JNIEnv* env, jobject self, jint permission, |
| 167 | jintArray jUids) { |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 168 | ScopedIntArrayRO uids(env, jUids); |
| 169 | if (uids.get() == nullptr) return; |
| 170 | |
| 171 | size_t size = uids.size(); |
| 172 | static_assert(sizeof(*(uids.get())) == sizeof(uid_t)); |
| 173 | std::vector<uid_t> data ((uid_t *)&uids[0], (uid_t*)&uids[size]); |
| 174 | mTc.setPermissionForUids(permission, data); |
| 175 | } |
| 176 | |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 177 | static void native_dump(JNIEnv* env, jobject self, jobject javaFd, jboolean verbose) { |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 178 | int fd = netjniutils::GetNativeFileDescriptor(env, javaFd); |
| 179 | if (fd < 0) { |
| 180 | jniThrowExceptionFmt(env, "java/io/IOException", "Invalid file descriptor"); |
| 181 | return; |
| 182 | } |
| 183 | mTc.dump(fd, verbose); |
| 184 | } |
| 185 | |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 186 | /* |
| 187 | * JNI registration. |
| 188 | */ |
| 189 | // clang-format off |
| 190 | static const JNINativeMethod gMethods[] = { |
| 191 | /* name, signature, funcPtr */ |
| 192 | {"native_init", "()V", |
| 193 | (void*)native_init}, |
| 194 | {"native_addNaughtyApp", "(I)I", |
| 195 | (void*)native_addNaughtyApp}, |
| 196 | {"native_removeNaughtyApp", "(I)I", |
| 197 | (void*)native_removeNaughtyApp}, |
| 198 | {"native_addNiceApp", "(I)I", |
| 199 | (void*)native_addNiceApp}, |
| 200 | {"native_removeNiceApp", "(I)I", |
| 201 | (void*)native_removeNiceApp}, |
Motomu Utsumi | 114cd9c | 2022-08-01 02:08:35 +0000 | [diff] [blame] | 202 | {"native_setChildChain", "(IZ)I", |
| 203 | (void*)native_setChildChain}, |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 204 | {"native_replaceUidChain", "(Ljava/lang/String;Z[I)I", |
| 205 | (void*)native_replaceUidChain}, |
| 206 | {"native_setUidRule", "(III)I", |
| 207 | (void*)native_setUidRule}, |
| 208 | {"native_addUidInterfaceRules", "(Ljava/lang/String;[I)I", |
| 209 | (void*)native_addUidInterfaceRules}, |
| 210 | {"native_removeUidInterfaceRules", "([I)I", |
| 211 | (void*)native_removeUidInterfaceRules}, |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 212 | {"native_updateUidLockdownRule", "(IZ)I", |
| 213 | (void*)native_updateUidLockdownRule}, |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 214 | {"native_swapActiveStatsMap", "()I", |
| 215 | (void*)native_swapActiveStatsMap}, |
| 216 | {"native_setPermissionForUids", "(I[I)V", |
| 217 | (void*)native_setPermissionForUids}, |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 218 | {"native_dump", "(Ljava/io/FileDescriptor;Z)V", |
| 219 | (void*)native_dump}, |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 220 | }; |
| 221 | // clang-format on |
| 222 | |
| 223 | int register_com_android_server_BpfNetMaps(JNIEnv* env) { |
Maciej Żenczykowski | 932ef5b | 2022-05-24 13:36:20 +0000 | [diff] [blame] | 224 | return jniRegisterNativeMethods(env, "com/android/server/BpfNetMaps", |
| 225 | gMethods, NELEM(gMethods)); |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | }; // namespace android |