Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 1 | /* |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 2 | * Copyright (C) 2022 The Android Open Source Project |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 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 "TrafficController" |
| 18 | #include <inttypes.h> |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 19 | #include <linux/if_ether.h> |
| 20 | #include <linux/in.h> |
| 21 | #include <linux/inet_diag.h> |
| 22 | #include <linux/netlink.h> |
| 23 | #include <linux/sock_diag.h> |
| 24 | #include <linux/unistd.h> |
| 25 | #include <net/if.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/socket.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/utsname.h> |
| 32 | #include <sys/wait.h> |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 33 | #include <map> |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 34 | #include <mutex> |
| 35 | #include <unordered_set> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include <android-base/stringprintf.h> |
| 39 | #include <android-base/strings.h> |
| 40 | #include <android-base/unique_fd.h> |
| 41 | #include <netdutils/StatusOr.h> |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 42 | #include <netdutils/Syscalls.h> |
Ken Chen | f426b2b | 2022-01-23 15:39:13 +0800 | [diff] [blame] | 43 | #include <netdutils/UidConstants.h> |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 44 | #include <netdutils/Utils.h> |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 45 | #include <private/android_filesystem_config.h> |
| 46 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 47 | #include "TrafficController.h" |
| 48 | #include "bpf/BpfMap.h" |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 49 | #include "netdutils/DumpWriter.h" |
| 50 | |
| 51 | namespace android { |
| 52 | namespace net { |
| 53 | |
| 54 | using base::StringPrintf; |
| 55 | using base::unique_fd; |
| 56 | using bpf::BpfMap; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 57 | using bpf::synchronizeKernelRCU; |
| 58 | using netdutils::DumpWriter; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 59 | using netdutils::getIfaceList; |
| 60 | using netdutils::NetlinkListener; |
| 61 | using netdutils::NetlinkListenerInterface; |
| 62 | using netdutils::ScopedIndent; |
| 63 | using netdutils::Slice; |
| 64 | using netdutils::sSyscalls; |
| 65 | using netdutils::Status; |
| 66 | using netdutils::statusFromErrno; |
| 67 | using netdutils::StatusOr; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 68 | |
| 69 | constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY; |
| 70 | constexpr int kSockDiagDoneMsgType = NLMSG_DONE; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 71 | |
| 72 | const char* TrafficController::LOCAL_DOZABLE = "fw_dozable"; |
| 73 | const char* TrafficController::LOCAL_STANDBY = "fw_standby"; |
| 74 | const char* TrafficController::LOCAL_POWERSAVE = "fw_powersave"; |
| 75 | const char* TrafficController::LOCAL_RESTRICTED = "fw_restricted"; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 76 | const char* TrafficController::LOCAL_LOW_POWER_STANDBY = "fw_low_power_standby"; |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 77 | const char* TrafficController::LOCAL_OEM_DENY_1 = "fw_oem_deny_1"; |
| 78 | const char* TrafficController::LOCAL_OEM_DENY_2 = "fw_oem_deny_2"; |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 79 | const char* TrafficController::LOCAL_OEM_DENY_3 = "fw_oem_deny_3"; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 80 | |
| 81 | static_assert(BPF_PERMISSION_INTERNET == INetd::PERMISSION_INTERNET, |
| 82 | "Mismatch between BPF and AIDL permissions: PERMISSION_INTERNET"); |
| 83 | static_assert(BPF_PERMISSION_UPDATE_DEVICE_STATS == INetd::PERMISSION_UPDATE_DEVICE_STATS, |
| 84 | "Mismatch between BPF and AIDL permissions: PERMISSION_UPDATE_DEVICE_STATS"); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 85 | |
| 86 | #define FLAG_MSG_TRANS(result, flag, value) \ |
| 87 | do { \ |
| 88 | if ((value) & (flag)) { \ |
| 89 | (result).append(" " #flag); \ |
| 90 | (value) &= ~(flag); \ |
| 91 | } \ |
| 92 | } while (0) |
| 93 | |
Motomu Utsumi | 42edc60 | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 94 | const std::string uidMatchTypeToString(uint32_t match) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 95 | std::string matchType; |
| 96 | FLAG_MSG_TRANS(matchType, HAPPY_BOX_MATCH, match); |
| 97 | FLAG_MSG_TRANS(matchType, PENALTY_BOX_MATCH, match); |
| 98 | FLAG_MSG_TRANS(matchType, DOZABLE_MATCH, match); |
| 99 | FLAG_MSG_TRANS(matchType, STANDBY_MATCH, match); |
| 100 | FLAG_MSG_TRANS(matchType, POWERSAVE_MATCH, match); |
| 101 | FLAG_MSG_TRANS(matchType, RESTRICTED_MATCH, match); |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 102 | FLAG_MSG_TRANS(matchType, LOW_POWER_STANDBY_MATCH, match); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 103 | FLAG_MSG_TRANS(matchType, IIF_MATCH, match); |
Motomu Utsumi | b08654c | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 104 | FLAG_MSG_TRANS(matchType, LOCKDOWN_VPN_MATCH, match); |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 105 | FLAG_MSG_TRANS(matchType, OEM_DENY_1_MATCH, match); |
| 106 | FLAG_MSG_TRANS(matchType, OEM_DENY_2_MATCH, match); |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 107 | FLAG_MSG_TRANS(matchType, OEM_DENY_3_MATCH, match); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 108 | if (match) { |
| 109 | return StringPrintf("Unknown match: %u", match); |
| 110 | } |
| 111 | return matchType; |
| 112 | } |
| 113 | |
| 114 | bool TrafficController::hasUpdateDeviceStatsPermission(uid_t uid) { |
| 115 | // This implementation is the same logic as method ActivityManager#checkComponentPermission. |
| 116 | // It implies that the calling uid can never be the same as PER_USER_RANGE. |
| 117 | uint32_t appId = uid % PER_USER_RANGE; |
| 118 | return ((appId == AID_ROOT) || (appId == AID_SYSTEM) || |
| 119 | mPrivilegedUser.find(appId) != mPrivilegedUser.end()); |
| 120 | } |
| 121 | |
| 122 | const std::string UidPermissionTypeToString(int permission) { |
| 123 | if (permission == INetd::PERMISSION_NONE) { |
| 124 | return "PERMISSION_NONE"; |
| 125 | } |
| 126 | if (permission == INetd::PERMISSION_UNINSTALLED) { |
| 127 | // This should never appear in the map, complain loudly if it does. |
| 128 | return "PERMISSION_UNINSTALLED error!"; |
| 129 | } |
| 130 | std::string permissionType; |
| 131 | FLAG_MSG_TRANS(permissionType, BPF_PERMISSION_INTERNET, permission); |
| 132 | FLAG_MSG_TRANS(permissionType, BPF_PERMISSION_UPDATE_DEVICE_STATS, permission); |
| 133 | if (permission) { |
| 134 | return StringPrintf("Unknown permission: %u", permission); |
| 135 | } |
| 136 | return permissionType; |
| 137 | } |
| 138 | |
| 139 | StatusOr<std::unique_ptr<NetlinkListenerInterface>> TrafficController::makeSkDestroyListener() { |
| 140 | const auto& sys = sSyscalls.get(); |
| 141 | ASSIGN_OR_RETURN(auto event, sys.eventfd(0, EFD_CLOEXEC)); |
| 142 | const int domain = AF_NETLINK; |
| 143 | const int type = SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK; |
| 144 | const int protocol = NETLINK_INET_DIAG; |
| 145 | ASSIGN_OR_RETURN(auto sock, sys.socket(domain, type, protocol)); |
| 146 | |
| 147 | // TODO: if too many sockets are closed too quickly, we can overflow the socket buffer, and |
| 148 | // some entries in mCookieTagMap will not be freed. In order to fix this we would need to |
| 149 | // periodically dump all sockets and remove the tag entries for sockets that have been closed. |
| 150 | // For now, set a large-enough buffer that we can close hundreds of sockets without getting |
| 151 | // ENOBUFS and leaking mCookieTagMap entries. |
| 152 | int rcvbuf = 512 * 1024; |
| 153 | auto ret = sys.setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); |
| 154 | if (!ret.ok()) { |
| 155 | ALOGW("Failed to set SkDestroyListener buffer size to %d: %s", rcvbuf, ret.msg().c_str()); |
| 156 | } |
| 157 | |
| 158 | sockaddr_nl addr = { |
| 159 | .nl_family = AF_NETLINK, |
| 160 | .nl_groups = 1 << (SKNLGRP_INET_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET_UDP_DESTROY - 1) | |
| 161 | 1 << (SKNLGRP_INET6_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET6_UDP_DESTROY - 1)}; |
| 162 | RETURN_IF_NOT_OK(sys.bind(sock, addr)); |
| 163 | |
| 164 | const sockaddr_nl kernel = {.nl_family = AF_NETLINK}; |
| 165 | RETURN_IF_NOT_OK(sys.connect(sock, kernel)); |
| 166 | |
| 167 | std::unique_ptr<NetlinkListenerInterface> listener = |
| 168 | std::make_unique<NetlinkListener>(std::move(event), std::move(sock), "SkDestroyListen"); |
| 169 | |
| 170 | return listener; |
| 171 | } |
| 172 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 173 | Status TrafficController::initMaps() { |
| 174 | std::lock_guard guard(mMutex); |
| 175 | |
| 176 | RETURN_IF_NOT_OK(mCookieTagMap.init(COOKIE_TAG_MAP_PATH)); |
| 177 | RETURN_IF_NOT_OK(mUidCounterSetMap.init(UID_COUNTERSET_MAP_PATH)); |
| 178 | RETURN_IF_NOT_OK(mAppUidStatsMap.init(APP_UID_STATS_MAP_PATH)); |
| 179 | RETURN_IF_NOT_OK(mStatsMapA.init(STATS_MAP_A_PATH)); |
| 180 | RETURN_IF_NOT_OK(mStatsMapB.init(STATS_MAP_B_PATH)); |
| 181 | RETURN_IF_NOT_OK(mIfaceIndexNameMap.init(IFACE_INDEX_NAME_MAP_PATH)); |
| 182 | RETURN_IF_NOT_OK(mIfaceStatsMap.init(IFACE_STATS_MAP_PATH)); |
| 183 | |
| 184 | RETURN_IF_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH)); |
| 185 | RETURN_IF_NOT_OK( |
| 186 | mConfigurationMap.writeValue(UID_RULES_CONFIGURATION_KEY, DEFAULT_CONFIG, BPF_ANY)); |
| 187 | RETURN_IF_NOT_OK(mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY, SELECT_MAP_A, |
| 188 | BPF_ANY)); |
| 189 | |
| 190 | RETURN_IF_NOT_OK(mUidOwnerMap.init(UID_OWNER_MAP_PATH)); |
| 191 | RETURN_IF_NOT_OK(mUidOwnerMap.clear()); |
| 192 | RETURN_IF_NOT_OK(mUidPermissionMap.init(UID_PERMISSION_MAP_PATH)); |
Ken Chen | 322ffcb | 2022-05-23 22:27:40 +0800 | [diff] [blame] | 193 | ALOGI("%s successfully", __func__); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 194 | |
| 195 | return netdutils::status::ok; |
| 196 | } |
| 197 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 198 | Status TrafficController::start() { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 199 | RETURN_IF_NOT_OK(initMaps()); |
| 200 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 201 | // Fetch the list of currently-existing interfaces. At this point NetlinkHandler is |
| 202 | // already running, so it will call addInterface() when any new interface appears. |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 203 | // TODO: Clean-up addInterface() after interface monitoring is in |
| 204 | // NetworkStatsService. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 205 | std::map<std::string, uint32_t> ifacePairs; |
| 206 | ASSIGN_OR_RETURN(ifacePairs, getIfaceList()); |
| 207 | for (const auto& ifacePair:ifacePairs) { |
| 208 | addInterface(ifacePair.first.c_str(), ifacePair.second); |
| 209 | } |
| 210 | |
| 211 | auto result = makeSkDestroyListener(); |
| 212 | if (!isOk(result)) { |
| 213 | ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str()); |
| 214 | } else { |
| 215 | mSkDestroyListener = std::move(result.value()); |
| 216 | } |
| 217 | // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function. |
| 218 | const auto rxHandler = [this](const nlmsghdr&, const Slice msg) { |
| 219 | std::lock_guard guard(mMutex); |
| 220 | inet_diag_msg diagmsg = {}; |
| 221 | if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) { |
| 222 | ALOGE("Unrecognized netlink message: %s", toString(msg).c_str()); |
| 223 | return; |
| 224 | } |
| 225 | uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) | |
| 226 | (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32); |
| 227 | |
| 228 | Status s = mCookieTagMap.deleteValue(sock_cookie); |
| 229 | if (!isOk(s) && s.code() != ENOENT) { |
| 230 | ALOGE("Failed to delete cookie %" PRIx64 ": %s", sock_cookie, toString(s).c_str()); |
| 231 | return; |
| 232 | } |
| 233 | }; |
| 234 | expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler)); |
| 235 | |
| 236 | // In case multiple netlink message comes in as a stream, we need to handle the rxDone message |
| 237 | // properly. |
| 238 | const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) { |
| 239 | // Ignore NLMSG_DONE messages |
| 240 | inet_diag_msg diagmsg = {}; |
| 241 | extract(msg, diagmsg); |
| 242 | }; |
| 243 | expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler)); |
| 244 | |
| 245 | return netdutils::status::ok; |
| 246 | } |
| 247 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 248 | int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) { |
| 249 | IfaceValue iface; |
| 250 | if (ifaceIndex == 0) { |
| 251 | ALOGE("Unknown interface %s(%d)", name, ifaceIndex); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | strlcpy(iface.name, name, sizeof(IfaceValue)); |
| 256 | Status res = mIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY); |
| 257 | if (!isOk(res)) { |
| 258 | ALOGE("Failed to add iface %s(%d): %s", name, ifaceIndex, strerror(res.code())); |
| 259 | return -res.code(); |
| 260 | } |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | Status TrafficController::updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule, |
| 265 | FirewallType type) { |
| 266 | std::lock_guard guard(mMutex); |
| 267 | if ((rule == ALLOW && type == ALLOWLIST) || (rule == DENY && type == DENYLIST)) { |
| 268 | RETURN_IF_NOT_OK(addRule(uid, match)); |
| 269 | } else if ((rule == ALLOW && type == DENYLIST) || (rule == DENY && type == ALLOWLIST)) { |
| 270 | RETURN_IF_NOT_OK(removeRule(uid, match)); |
| 271 | } else { |
| 272 | //Cannot happen. |
| 273 | return statusFromErrno(EINVAL, ""); |
| 274 | } |
| 275 | return netdutils::status::ok; |
| 276 | } |
| 277 | |
| 278 | Status TrafficController::removeRule(uint32_t uid, UidOwnerMatchType match) { |
| 279 | auto oldMatch = mUidOwnerMap.readValue(uid); |
| 280 | if (oldMatch.ok()) { |
| 281 | UidOwnerValue newMatch = { |
| 282 | .iif = (match == IIF_MATCH) ? 0 : oldMatch.value().iif, |
Motomu Utsumi | 42edc60 | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 283 | .rule = oldMatch.value().rule & ~match, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 284 | }; |
| 285 | if (newMatch.rule == 0) { |
| 286 | RETURN_IF_NOT_OK(mUidOwnerMap.deleteValue(uid)); |
| 287 | } else { |
| 288 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
| 289 | } |
| 290 | } else { |
| 291 | return statusFromErrno(ENOENT, StringPrintf("uid: %u does not exist in map", uid)); |
| 292 | } |
| 293 | return netdutils::status::ok; |
| 294 | } |
| 295 | |
| 296 | Status TrafficController::addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif) { |
Motomu Utsumi | b08654c | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 297 | if (match != IIF_MATCH && iif != 0) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 298 | return statusFromErrno(EINVAL, "Non-interface match must have zero interface index"); |
| 299 | } |
| 300 | auto oldMatch = mUidOwnerMap.readValue(uid); |
| 301 | if (oldMatch.ok()) { |
| 302 | UidOwnerValue newMatch = { |
Motomu Utsumi | b08654c | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 303 | .iif = (match == IIF_MATCH) ? iif : oldMatch.value().iif, |
Motomu Utsumi | 42edc60 | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 304 | .rule = oldMatch.value().rule | match, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 305 | }; |
| 306 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
| 307 | } else { |
| 308 | UidOwnerValue newMatch = { |
| 309 | .iif = iif, |
Motomu Utsumi | 42edc60 | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 310 | .rule = match, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 311 | }; |
| 312 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
| 313 | } |
| 314 | return netdutils::status::ok; |
| 315 | } |
| 316 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 317 | Status TrafficController::updateUidOwnerMap(const uint32_t uid, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 318 | UidOwnerMatchType matchType, IptOp op) { |
| 319 | std::lock_guard guard(mMutex); |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 320 | if (op == IptOpDelete) { |
| 321 | RETURN_IF_NOT_OK(removeRule(uid, matchType)); |
| 322 | } else if (op == IptOpInsert) { |
| 323 | RETURN_IF_NOT_OK(addRule(uid, matchType)); |
| 324 | } else { |
| 325 | // Cannot happen. |
| 326 | return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, matchType)); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 327 | } |
| 328 | return netdutils::status::ok; |
| 329 | } |
| 330 | |
| 331 | FirewallType TrafficController::getFirewallType(ChildChain chain) { |
| 332 | switch (chain) { |
| 333 | case DOZABLE: |
| 334 | return ALLOWLIST; |
| 335 | case STANDBY: |
| 336 | return DENYLIST; |
| 337 | case POWERSAVE: |
| 338 | return ALLOWLIST; |
| 339 | case RESTRICTED: |
| 340 | return ALLOWLIST; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 341 | case LOW_POWER_STANDBY: |
| 342 | return ALLOWLIST; |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 343 | case OEM_DENY_1: |
| 344 | return DENYLIST; |
| 345 | case OEM_DENY_2: |
| 346 | return DENYLIST; |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 347 | case OEM_DENY_3: |
| 348 | return DENYLIST; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 349 | case NONE: |
| 350 | default: |
| 351 | return DENYLIST; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule, |
| 356 | FirewallType type) { |
| 357 | Status res; |
| 358 | switch (chain) { |
| 359 | case DOZABLE: |
| 360 | res = updateOwnerMapEntry(DOZABLE_MATCH, uid, rule, type); |
| 361 | break; |
| 362 | case STANDBY: |
| 363 | res = updateOwnerMapEntry(STANDBY_MATCH, uid, rule, type); |
| 364 | break; |
| 365 | case POWERSAVE: |
| 366 | res = updateOwnerMapEntry(POWERSAVE_MATCH, uid, rule, type); |
| 367 | break; |
| 368 | case RESTRICTED: |
| 369 | res = updateOwnerMapEntry(RESTRICTED_MATCH, uid, rule, type); |
| 370 | break; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 371 | case LOW_POWER_STANDBY: |
| 372 | res = updateOwnerMapEntry(LOW_POWER_STANDBY_MATCH, uid, rule, type); |
| 373 | break; |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 374 | case OEM_DENY_1: |
| 375 | res = updateOwnerMapEntry(OEM_DENY_1_MATCH, uid, rule, type); |
| 376 | break; |
| 377 | case OEM_DENY_2: |
| 378 | res = updateOwnerMapEntry(OEM_DENY_2_MATCH, uid, rule, type); |
| 379 | break; |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 380 | case OEM_DENY_3: |
| 381 | res = updateOwnerMapEntry(OEM_DENY_3_MATCH, uid, rule, type); |
| 382 | break; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 383 | case NONE: |
| 384 | default: |
| 385 | ALOGW("Unknown child chain: %d", chain); |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | if (!isOk(res)) { |
| 389 | ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain, |
| 390 | res.msg().c_str(), rule, type); |
| 391 | return -res.code(); |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | Status TrafficController::replaceRulesInMap(const UidOwnerMatchType match, |
| 397 | const std::vector<int32_t>& uids) { |
| 398 | std::lock_guard guard(mMutex); |
| 399 | std::set<int32_t> uidSet(uids.begin(), uids.end()); |
| 400 | std::vector<uint32_t> uidsToDelete; |
| 401 | auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key, |
| 402 | const BpfMap<uint32_t, UidOwnerValue>&) { |
| 403 | if (uidSet.find((int32_t) key) == uidSet.end()) { |
| 404 | uidsToDelete.push_back(key); |
| 405 | } |
| 406 | return base::Result<void>(); |
| 407 | }; |
| 408 | RETURN_IF_NOT_OK(mUidOwnerMap.iterate(getUidsToDelete)); |
| 409 | |
| 410 | for(auto uid : uidsToDelete) { |
| 411 | RETURN_IF_NOT_OK(removeRule(uid, match)); |
| 412 | } |
| 413 | |
| 414 | for (auto uid : uids) { |
| 415 | RETURN_IF_NOT_OK(addRule(uid, match)); |
| 416 | } |
| 417 | return netdutils::status::ok; |
| 418 | } |
| 419 | |
| 420 | Status TrafficController::addUidInterfaceRules(const int iif, |
| 421 | const std::vector<int32_t>& uidsToAdd) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 422 | std::lock_guard guard(mMutex); |
| 423 | |
| 424 | for (auto uid : uidsToAdd) { |
| 425 | netdutils::Status result = addRule(uid, IIF_MATCH, iif); |
| 426 | if (!isOk(result)) { |
| 427 | ALOGW("addRule failed(%d): uid=%d iif=%d", result.code(), uid, iif); |
| 428 | } |
| 429 | } |
| 430 | return netdutils::status::ok; |
| 431 | } |
| 432 | |
| 433 | Status TrafficController::removeUidInterfaceRules(const std::vector<int32_t>& uidsToDelete) { |
| 434 | std::lock_guard guard(mMutex); |
| 435 | |
| 436 | for (auto uid : uidsToDelete) { |
| 437 | netdutils::Status result = removeRule(uid, IIF_MATCH); |
| 438 | if (!isOk(result)) { |
| 439 | ALOGW("removeRule failed(%d): uid=%d", result.code(), uid); |
| 440 | } |
| 441 | } |
| 442 | return netdutils::status::ok; |
| 443 | } |
| 444 | |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame^] | 445 | Status TrafficController::updateUidLockdownRule(const uid_t uid, const bool add) { |
| 446 | std::lock_guard guard(mMutex); |
| 447 | |
| 448 | netdutils::Status result = add ? addRule(uid, LOCKDOWN_VPN_MATCH) |
| 449 | : removeRule(uid, LOCKDOWN_VPN_MATCH); |
| 450 | if (!isOk(result)) { |
| 451 | ALOGW("%s Lockdown rule failed(%d): uid=%d", |
| 452 | (add ? "add": "remove"), result.code(), uid); |
| 453 | } |
| 454 | return result; |
| 455 | } |
| 456 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 457 | int TrafficController::replaceUidOwnerMap(const std::string& name, bool isAllowlist __unused, |
| 458 | const std::vector<int32_t>& uids) { |
| 459 | // FirewallRule rule = isAllowlist ? ALLOW : DENY; |
| 460 | // FirewallType type = isAllowlist ? ALLOWLIST : DENYLIST; |
| 461 | Status res; |
| 462 | if (!name.compare(LOCAL_DOZABLE)) { |
| 463 | res = replaceRulesInMap(DOZABLE_MATCH, uids); |
| 464 | } else if (!name.compare(LOCAL_STANDBY)) { |
| 465 | res = replaceRulesInMap(STANDBY_MATCH, uids); |
| 466 | } else if (!name.compare(LOCAL_POWERSAVE)) { |
| 467 | res = replaceRulesInMap(POWERSAVE_MATCH, uids); |
| 468 | } else if (!name.compare(LOCAL_RESTRICTED)) { |
| 469 | res = replaceRulesInMap(RESTRICTED_MATCH, uids); |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 470 | } else if (!name.compare(LOCAL_LOW_POWER_STANDBY)) { |
| 471 | res = replaceRulesInMap(LOW_POWER_STANDBY_MATCH, uids); |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 472 | } else if (!name.compare(LOCAL_OEM_DENY_1)) { |
| 473 | res = replaceRulesInMap(OEM_DENY_1_MATCH, uids); |
| 474 | } else if (!name.compare(LOCAL_OEM_DENY_2)) { |
| 475 | res = replaceRulesInMap(OEM_DENY_2_MATCH, uids); |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 476 | } else if (!name.compare(LOCAL_OEM_DENY_3)) { |
| 477 | res = replaceRulesInMap(OEM_DENY_3_MATCH, uids); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 478 | } else { |
| 479 | ALOGE("unknown chain name: %s", name.c_str()); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | if (!isOk(res)) { |
| 483 | ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str()); |
| 484 | return -res.code(); |
| 485 | } |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) { |
| 490 | std::lock_guard guard(mMutex); |
| 491 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
Lorenzo Colitti | 60cbed3 | 2022-03-03 17:49:01 +0900 | [diff] [blame] | 492 | auto oldConfigure = mConfigurationMap.readValue(key); |
| 493 | if (!oldConfigure.ok()) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 494 | ALOGE("Cannot read the old configuration from map: %s", |
Lorenzo Colitti | 60cbed3 | 2022-03-03 17:49:01 +0900 | [diff] [blame] | 495 | oldConfigure.error().message().c_str()); |
| 496 | return -oldConfigure.error().code(); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 497 | } |
| 498 | Status res; |
| 499 | BpfConfig newConfiguration; |
Lorenzo Colitti | 60cbed3 | 2022-03-03 17:49:01 +0900 | [diff] [blame] | 500 | uint32_t match; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 501 | switch (chain) { |
| 502 | case DOZABLE: |
| 503 | match = DOZABLE_MATCH; |
| 504 | break; |
| 505 | case STANDBY: |
| 506 | match = STANDBY_MATCH; |
| 507 | break; |
| 508 | case POWERSAVE: |
| 509 | match = POWERSAVE_MATCH; |
| 510 | break; |
| 511 | case RESTRICTED: |
| 512 | match = RESTRICTED_MATCH; |
| 513 | break; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 514 | case LOW_POWER_STANDBY: |
| 515 | match = LOW_POWER_STANDBY_MATCH; |
| 516 | break; |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 517 | case OEM_DENY_1: |
| 518 | match = OEM_DENY_1_MATCH; |
| 519 | break; |
| 520 | case OEM_DENY_2: |
| 521 | match = OEM_DENY_2_MATCH; |
| 522 | break; |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 523 | case OEM_DENY_3: |
| 524 | match = OEM_DENY_3_MATCH; |
| 525 | break; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 526 | default: |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | newConfiguration = |
Lorenzo Colitti | 60cbed3 | 2022-03-03 17:49:01 +0900 | [diff] [blame] | 530 | enable ? (oldConfigure.value() | match) : (oldConfigure.value() & (~match)); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 531 | res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST); |
| 532 | if (!isOk(res)) { |
| 533 | ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str()); |
| 534 | } |
| 535 | return -res.code(); |
| 536 | } |
| 537 | |
| 538 | Status TrafficController::swapActiveStatsMap() { |
| 539 | std::lock_guard guard(mMutex); |
| 540 | |
| 541 | uint32_t key = CURRENT_STATS_MAP_CONFIGURATION_KEY; |
Lorenzo Colitti | 60cbed3 | 2022-03-03 17:49:01 +0900 | [diff] [blame] | 542 | auto oldConfigure = mConfigurationMap.readValue(key); |
| 543 | if (!oldConfigure.ok()) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 544 | ALOGE("Cannot read the old configuration from map: %s", |
Lorenzo Colitti | 60cbed3 | 2022-03-03 17:49:01 +0900 | [diff] [blame] | 545 | oldConfigure.error().message().c_str()); |
| 546 | return Status(oldConfigure.error().code(), oldConfigure.error().message()); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | // Write to the configuration map to inform the kernel eBPF program to switch |
| 550 | // from using one map to the other. Use flag BPF_EXIST here since the map should |
| 551 | // be already populated in initMaps. |
Lorenzo Colitti | 60cbed3 | 2022-03-03 17:49:01 +0900 | [diff] [blame] | 552 | uint32_t newConfigure = (oldConfigure.value() == SELECT_MAP_A) ? SELECT_MAP_B : SELECT_MAP_A; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 553 | auto res = mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY, newConfigure, |
| 554 | BPF_EXIST); |
| 555 | if (!res.ok()) { |
| 556 | ALOGE("Failed to toggle the stats map: %s", strerror(res.error().code())); |
| 557 | return res; |
| 558 | } |
| 559 | // After changing the config, we need to make sure all the current running |
| 560 | // eBPF programs are finished and all the CPUs are aware of this config change |
| 561 | // before we modify the old map. So we do a special hack here to wait for |
| 562 | // the kernel to do a synchronize_rcu(). Once the kernel called |
| 563 | // synchronize_rcu(), the config we just updated will be available to all cores |
| 564 | // and the next eBPF programs triggered inside the kernel will use the new |
| 565 | // map configuration. So once this function returns we can safely modify the |
| 566 | // old stats map without concerning about race between the kernel and |
| 567 | // userspace. |
| 568 | int ret = synchronizeKernelRCU(); |
| 569 | if (ret) { |
| 570 | ALOGE("map swap synchronize_rcu() ended with failure: %s", strerror(-ret)); |
| 571 | return statusFromErrno(-ret, "map swap synchronize_rcu() failed"); |
| 572 | } |
| 573 | return netdutils::status::ok; |
| 574 | } |
| 575 | |
| 576 | void TrafficController::setPermissionForUids(int permission, const std::vector<uid_t>& uids) { |
| 577 | std::lock_guard guard(mMutex); |
| 578 | if (permission == INetd::PERMISSION_UNINSTALLED) { |
| 579 | for (uid_t uid : uids) { |
| 580 | // Clean up all permission information for the related uid if all the |
| 581 | // packages related to it are uninstalled. |
| 582 | mPrivilegedUser.erase(uid); |
| 583 | Status ret = mUidPermissionMap.deleteValue(uid); |
| 584 | if (!isOk(ret) && ret.code() != ENOENT) { |
| 585 | ALOGE("Failed to clean up the permission for %u: %s", uid, strerror(ret.code())); |
| 586 | } |
| 587 | } |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | bool privileged = (permission & INetd::PERMISSION_UPDATE_DEVICE_STATS); |
| 592 | |
| 593 | for (uid_t uid : uids) { |
| 594 | if (privileged) { |
| 595 | mPrivilegedUser.insert(uid); |
| 596 | } else { |
| 597 | mPrivilegedUser.erase(uid); |
| 598 | } |
| 599 | |
| 600 | // The map stores all the permissions that the UID has, except if the only permission |
| 601 | // the UID has is the INTERNET permission, then the UID should not appear in the map. |
| 602 | if (permission != INetd::PERMISSION_INTERNET) { |
| 603 | Status ret = mUidPermissionMap.writeValue(uid, permission, BPF_ANY); |
| 604 | if (!isOk(ret)) { |
| 605 | ALOGE("Failed to set permission: %s of uid(%u) to permission map: %s", |
| 606 | UidPermissionTypeToString(permission).c_str(), uid, strerror(ret.code())); |
| 607 | } |
| 608 | } else { |
| 609 | Status ret = mUidPermissionMap.deleteValue(uid); |
| 610 | if (!isOk(ret) && ret.code() != ENOENT) { |
| 611 | ALOGE("Failed to remove uid %u from permission map: %s", uid, strerror(ret.code())); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | std::string getProgramStatus(const char *path) { |
| 618 | int ret = access(path, R_OK); |
| 619 | if (ret == 0) { |
| 620 | return StringPrintf("OK"); |
| 621 | } |
| 622 | if (ret != 0 && errno == ENOENT) { |
| 623 | return StringPrintf("program is missing at: %s", path); |
| 624 | } |
| 625 | return StringPrintf("check Program %s error: %s", path, strerror(errno)); |
| 626 | } |
| 627 | |
| 628 | std::string getMapStatus(const base::unique_fd& map_fd, const char* path) { |
| 629 | if (map_fd.get() < 0) { |
| 630 | return StringPrintf("map fd lost"); |
| 631 | } |
| 632 | if (access(path, F_OK) != 0) { |
| 633 | return StringPrintf("map not pinned to location: %s", path); |
| 634 | } |
| 635 | return StringPrintf("OK"); |
| 636 | } |
| 637 | |
| 638 | // NOLINTNEXTLINE(google-runtime-references): grandfathered pass by non-const reference |
| 639 | void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) { |
| 640 | dw.blankline(); |
| 641 | dw.println("%s:", mapName.c_str()); |
| 642 | if (!header.empty()) { |
| 643 | dw.println(header); |
| 644 | } |
| 645 | } |
| 646 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 647 | void TrafficController::dump(int fd, bool verbose) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 648 | std::lock_guard guard(mMutex); |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 649 | DumpWriter dw(fd); |
| 650 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 651 | ScopedIndent indentTop(dw); |
| 652 | dw.println("TrafficController"); |
| 653 | |
| 654 | ScopedIndent indentPreBpfModule(dw); |
| 655 | |
| 656 | dw.blankline(); |
| 657 | dw.println("mCookieTagMap status: %s", |
| 658 | getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str()); |
| 659 | dw.println("mUidCounterSetMap status: %s", |
| 660 | getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str()); |
| 661 | dw.println("mAppUidStatsMap status: %s", |
| 662 | getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str()); |
| 663 | dw.println("mStatsMapA status: %s", |
| 664 | getMapStatus(mStatsMapA.getMap(), STATS_MAP_A_PATH).c_str()); |
| 665 | dw.println("mStatsMapB status: %s", |
| 666 | getMapStatus(mStatsMapB.getMap(), STATS_MAP_B_PATH).c_str()); |
| 667 | dw.println("mIfaceIndexNameMap status: %s", |
| 668 | getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str()); |
| 669 | dw.println("mIfaceStatsMap status: %s", |
| 670 | getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str()); |
| 671 | dw.println("mConfigurationMap status: %s", |
| 672 | getMapStatus(mConfigurationMap.getMap(), CONFIGURATION_MAP_PATH).c_str()); |
| 673 | dw.println("mUidOwnerMap status: %s", |
| 674 | getMapStatus(mUidOwnerMap.getMap(), UID_OWNER_MAP_PATH).c_str()); |
| 675 | |
| 676 | dw.blankline(); |
| 677 | dw.println("Cgroup ingress program status: %s", |
| 678 | getProgramStatus(BPF_INGRESS_PROG_PATH).c_str()); |
| 679 | dw.println("Cgroup egress program status: %s", getProgramStatus(BPF_EGRESS_PROG_PATH).c_str()); |
| 680 | dw.println("xt_bpf ingress program status: %s", |
| 681 | getProgramStatus(XT_BPF_INGRESS_PROG_PATH).c_str()); |
| 682 | dw.println("xt_bpf egress program status: %s", |
| 683 | getProgramStatus(XT_BPF_EGRESS_PROG_PATH).c_str()); |
| 684 | dw.println("xt_bpf bandwidth allowlist program status: %s", |
| 685 | getProgramStatus(XT_BPF_ALLOWLIST_PROG_PATH).c_str()); |
| 686 | dw.println("xt_bpf bandwidth denylist program status: %s", |
| 687 | getProgramStatus(XT_BPF_DENYLIST_PROG_PATH).c_str()); |
| 688 | |
| 689 | if (!verbose) { |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | dw.blankline(); |
| 694 | dw.println("BPF map content:"); |
| 695 | |
| 696 | ScopedIndent indentForMapContent(dw); |
| 697 | |
| 698 | // Print CookieTagMap content. |
| 699 | dumpBpfMap("mCookieTagMap", dw, ""); |
| 700 | const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTagValue& value, |
| 701 | const BpfMap<uint64_t, UidTagValue>&) { |
| 702 | dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid); |
| 703 | return base::Result<void>(); |
| 704 | }; |
| 705 | base::Result<void> res = mCookieTagMap.iterateWithValue(printCookieTagInfo); |
| 706 | if (!res.ok()) { |
| 707 | dw.println("mCookieTagMap print end with error: %s", res.error().message().c_str()); |
| 708 | } |
| 709 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 710 | // Print UidCounterSetMap content. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 711 | dumpBpfMap("mUidCounterSetMap", dw, ""); |
| 712 | const auto printUidInfo = [&dw](const uint32_t& key, const uint8_t& value, |
| 713 | const BpfMap<uint32_t, uint8_t>&) { |
| 714 | dw.println("%u %u", key, value); |
| 715 | return base::Result<void>(); |
| 716 | }; |
| 717 | res = mUidCounterSetMap.iterateWithValue(printUidInfo); |
| 718 | if (!res.ok()) { |
| 719 | dw.println("mUidCounterSetMap print end with error: %s", res.error().message().c_str()); |
| 720 | } |
| 721 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 722 | // Print AppUidStatsMap content. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 723 | std::string appUidStatsHeader = StringPrintf("uid rxBytes rxPackets txBytes txPackets"); |
| 724 | dumpBpfMap("mAppUidStatsMap:", dw, appUidStatsHeader); |
| 725 | auto printAppUidStatsInfo = [&dw](const uint32_t& key, const StatsValue& value, |
| 726 | const BpfMap<uint32_t, StatsValue>&) { |
| 727 | dw.println("%u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxBytes, |
| 728 | value.rxPackets, value.txBytes, value.txPackets); |
| 729 | return base::Result<void>(); |
| 730 | }; |
| 731 | res = mAppUidStatsMap.iterateWithValue(printAppUidStatsInfo); |
| 732 | if (!res.ok()) { |
| 733 | dw.println("mAppUidStatsMap print end with error: %s", res.error().message().c_str()); |
| 734 | } |
| 735 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 736 | // Print uidStatsMap content. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 737 | std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes" |
| 738 | " rxPackets txBytes txPackets"); |
| 739 | dumpBpfMap("mStatsMapA", dw, statsHeader); |
| 740 | const auto printStatsInfo = [&dw, this](const StatsKey& key, const StatsValue& value, |
| 741 | const BpfMap<StatsKey, StatsValue>&) { |
| 742 | uint32_t ifIndex = key.ifaceIndex; |
| 743 | auto ifname = mIfaceIndexNameMap.readValue(ifIndex); |
| 744 | if (!ifname.ok()) { |
| 745 | ifname = IfaceValue{"unknown"}; |
| 746 | } |
| 747 | dw.println("%u %s 0x%x %u %u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, ifIndex, |
| 748 | ifname.value().name, key.tag, key.uid, key.counterSet, value.rxBytes, |
| 749 | value.rxPackets, value.txBytes, value.txPackets); |
| 750 | return base::Result<void>(); |
| 751 | }; |
| 752 | res = mStatsMapA.iterateWithValue(printStatsInfo); |
| 753 | if (!res.ok()) { |
| 754 | dw.println("mStatsMapA print end with error: %s", res.error().message().c_str()); |
| 755 | } |
| 756 | |
| 757 | // Print TagStatsMap content. |
| 758 | dumpBpfMap("mStatsMapB", dw, statsHeader); |
| 759 | res = mStatsMapB.iterateWithValue(printStatsInfo); |
| 760 | if (!res.ok()) { |
| 761 | dw.println("mStatsMapB print end with error: %s", res.error().message().c_str()); |
| 762 | } |
| 763 | |
| 764 | // Print ifaceIndexToNameMap content. |
| 765 | dumpBpfMap("mIfaceIndexNameMap", dw, ""); |
| 766 | const auto printIfaceNameInfo = [&dw](const uint32_t& key, const IfaceValue& value, |
| 767 | const BpfMap<uint32_t, IfaceValue>&) { |
| 768 | const char* ifname = value.name; |
| 769 | dw.println("ifaceIndex=%u ifaceName=%s", key, ifname); |
| 770 | return base::Result<void>(); |
| 771 | }; |
| 772 | res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo); |
| 773 | if (!res.ok()) { |
| 774 | dw.println("mIfaceIndexNameMap print end with error: %s", res.error().message().c_str()); |
| 775 | } |
| 776 | |
| 777 | // Print ifaceStatsMap content |
| 778 | std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes" |
| 779 | " txPackets"); |
| 780 | dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader); |
| 781 | const auto printIfaceStatsInfo = [&dw, this](const uint32_t& key, const StatsValue& value, |
| 782 | const BpfMap<uint32_t, StatsValue>&) { |
| 783 | auto ifname = mIfaceIndexNameMap.readValue(key); |
| 784 | if (!ifname.ok()) { |
| 785 | ifname = IfaceValue{"unknown"}; |
| 786 | } |
| 787 | dw.println("%u %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, ifname.value().name, |
| 788 | value.rxBytes, value.rxPackets, value.txBytes, value.txPackets); |
| 789 | return base::Result<void>(); |
| 790 | }; |
| 791 | res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo); |
| 792 | if (!res.ok()) { |
| 793 | dw.println("mIfaceStatsMap print end with error: %s", res.error().message().c_str()); |
| 794 | } |
| 795 | |
| 796 | dw.blankline(); |
| 797 | |
| 798 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
| 799 | auto configuration = mConfigurationMap.readValue(key); |
| 800 | if (configuration.ok()) { |
| 801 | dw.println("current ownerMatch configuration: %d%s", configuration.value(), |
| 802 | uidMatchTypeToString(configuration.value()).c_str()); |
| 803 | } else { |
| 804 | dw.println("mConfigurationMap read ownerMatch configure failed with error: %s", |
| 805 | configuration.error().message().c_str()); |
| 806 | } |
| 807 | |
| 808 | key = CURRENT_STATS_MAP_CONFIGURATION_KEY; |
| 809 | configuration = mConfigurationMap.readValue(key); |
| 810 | if (configuration.ok()) { |
| 811 | const char* statsMapDescription = "???"; |
| 812 | switch (configuration.value()) { |
| 813 | case SELECT_MAP_A: |
| 814 | statsMapDescription = "SELECT_MAP_A"; |
| 815 | break; |
| 816 | case SELECT_MAP_B: |
| 817 | statsMapDescription = "SELECT_MAP_B"; |
| 818 | break; |
| 819 | // No default clause, so if we ever add a third map, this code will fail to build. |
| 820 | } |
| 821 | dw.println("current statsMap configuration: %d %s", configuration.value(), |
| 822 | statsMapDescription); |
| 823 | } else { |
| 824 | dw.println("mConfigurationMap read stats map configure failed with error: %s", |
| 825 | configuration.error().message().c_str()); |
| 826 | } |
| 827 | dumpBpfMap("mUidOwnerMap", dw, ""); |
| 828 | const auto printUidMatchInfo = [&dw, this](const uint32_t& key, const UidOwnerValue& value, |
| 829 | const BpfMap<uint32_t, UidOwnerValue>&) { |
| 830 | if (value.rule & IIF_MATCH) { |
| 831 | auto ifname = mIfaceIndexNameMap.readValue(value.iif); |
| 832 | if (ifname.ok()) { |
| 833 | dw.println("%u %s %s", key, uidMatchTypeToString(value.rule).c_str(), |
| 834 | ifname.value().name); |
| 835 | } else { |
| 836 | dw.println("%u %s %u", key, uidMatchTypeToString(value.rule).c_str(), value.iif); |
| 837 | } |
| 838 | } else { |
| 839 | dw.println("%u %s", key, uidMatchTypeToString(value.rule).c_str()); |
| 840 | } |
| 841 | return base::Result<void>(); |
| 842 | }; |
| 843 | res = mUidOwnerMap.iterateWithValue(printUidMatchInfo); |
| 844 | if (!res.ok()) { |
| 845 | dw.println("mUidOwnerMap print end with error: %s", res.error().message().c_str()); |
| 846 | } |
| 847 | dumpBpfMap("mUidPermissionMap", dw, ""); |
| 848 | const auto printUidPermissionInfo = [&dw](const uint32_t& key, const int& value, |
| 849 | const BpfMap<uint32_t, uint8_t>&) { |
| 850 | dw.println("%u %s", key, UidPermissionTypeToString(value).c_str()); |
| 851 | return base::Result<void>(); |
| 852 | }; |
| 853 | res = mUidPermissionMap.iterateWithValue(printUidPermissionInfo); |
| 854 | if (!res.ok()) { |
| 855 | dw.println("mUidPermissionMap print end with error: %s", res.error().message().c_str()); |
| 856 | } |
| 857 | |
| 858 | dumpBpfMap("mPrivilegedUser", dw, ""); |
| 859 | for (uid_t uid : mPrivilegedUser) { |
| 860 | dw.println("%u ALLOW_UPDATE_DEVICE_STATS", (uint32_t)uid); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | } // namespace net |
| 865 | } // namespace android |