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