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