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