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"; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 77 | |
| 78 | static_assert(BPF_PERMISSION_INTERNET == INetd::PERMISSION_INTERNET, |
| 79 | "Mismatch between BPF and AIDL permissions: PERMISSION_INTERNET"); |
| 80 | static_assert(BPF_PERMISSION_UPDATE_DEVICE_STATS == INetd::PERMISSION_UPDATE_DEVICE_STATS, |
| 81 | "Mismatch between BPF and AIDL permissions: PERMISSION_UPDATE_DEVICE_STATS"); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 82 | |
| 83 | #define FLAG_MSG_TRANS(result, flag, value) \ |
| 84 | do { \ |
| 85 | if ((value) & (flag)) { \ |
| 86 | (result).append(" " #flag); \ |
| 87 | (value) &= ~(flag); \ |
| 88 | } \ |
| 89 | } while (0) |
| 90 | |
Motomu Utsumi | 7f9c79b | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 91 | const std::string uidMatchTypeToString(uint32_t match) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 92 | std::string matchType; |
| 93 | FLAG_MSG_TRANS(matchType, HAPPY_BOX_MATCH, match); |
| 94 | FLAG_MSG_TRANS(matchType, PENALTY_BOX_MATCH, match); |
| 95 | FLAG_MSG_TRANS(matchType, DOZABLE_MATCH, match); |
| 96 | FLAG_MSG_TRANS(matchType, STANDBY_MATCH, match); |
| 97 | FLAG_MSG_TRANS(matchType, POWERSAVE_MATCH, match); |
| 98 | FLAG_MSG_TRANS(matchType, RESTRICTED_MATCH, match); |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 99 | FLAG_MSG_TRANS(matchType, LOW_POWER_STANDBY_MATCH, match); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 100 | FLAG_MSG_TRANS(matchType, IIF_MATCH, match); |
Motomu Utsumi | 966ff7f | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 101 | FLAG_MSG_TRANS(matchType, LOCKDOWN_VPN_MATCH, match); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 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, |
Motomu Utsumi | 7f9c79b | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 276 | .rule = oldMatch.value().rule & ~match, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 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) { |
Motomu Utsumi | 966ff7f | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 290 | if (match != IIF_MATCH && iif != 0) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 291 | return statusFromErrno(EINVAL, "Non-interface match must have zero interface index"); |
| 292 | } |
| 293 | auto oldMatch = mUidOwnerMap.readValue(uid); |
| 294 | if (oldMatch.ok()) { |
| 295 | UidOwnerValue newMatch = { |
Motomu Utsumi | 966ff7f | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 296 | .iif = (match == IIF_MATCH) ? iif : oldMatch.value().iif, |
Motomu Utsumi | 7f9c79b | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 297 | .rule = oldMatch.value().rule | match, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 298 | }; |
| 299 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
| 300 | } else { |
| 301 | UidOwnerValue newMatch = { |
| 302 | .iif = iif, |
Motomu Utsumi | 7f9c79b | 2022-05-12 13:57:42 +0000 | [diff] [blame] | 303 | .rule = match, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 304 | }; |
| 305 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
| 306 | } |
| 307 | return netdutils::status::ok; |
| 308 | } |
| 309 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 310 | Status TrafficController::updateUidOwnerMap(const uint32_t uid, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 311 | UidOwnerMatchType matchType, IptOp op) { |
| 312 | std::lock_guard guard(mMutex); |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 313 | if (op == IptOpDelete) { |
| 314 | RETURN_IF_NOT_OK(removeRule(uid, matchType)); |
| 315 | } else if (op == IptOpInsert) { |
| 316 | RETURN_IF_NOT_OK(addRule(uid, matchType)); |
| 317 | } else { |
| 318 | // Cannot happen. |
| 319 | return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, matchType)); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 320 | } |
| 321 | return netdutils::status::ok; |
| 322 | } |
| 323 | |
| 324 | FirewallType TrafficController::getFirewallType(ChildChain chain) { |
| 325 | switch (chain) { |
| 326 | case DOZABLE: |
| 327 | return ALLOWLIST; |
| 328 | case STANDBY: |
| 329 | return DENYLIST; |
| 330 | case POWERSAVE: |
| 331 | return ALLOWLIST; |
| 332 | case RESTRICTED: |
| 333 | return ALLOWLIST; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 334 | case LOW_POWER_STANDBY: |
| 335 | return ALLOWLIST; |
Motomu Utsumi | 966ff7f | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 336 | case LOCKDOWN: |
| 337 | return DENYLIST; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 338 | case NONE: |
| 339 | default: |
| 340 | return DENYLIST; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule, |
| 345 | FirewallType type) { |
| 346 | Status res; |
| 347 | switch (chain) { |
| 348 | case DOZABLE: |
| 349 | res = updateOwnerMapEntry(DOZABLE_MATCH, uid, rule, type); |
| 350 | break; |
| 351 | case STANDBY: |
| 352 | res = updateOwnerMapEntry(STANDBY_MATCH, uid, rule, type); |
| 353 | break; |
| 354 | case POWERSAVE: |
| 355 | res = updateOwnerMapEntry(POWERSAVE_MATCH, uid, rule, type); |
| 356 | break; |
| 357 | case RESTRICTED: |
| 358 | res = updateOwnerMapEntry(RESTRICTED_MATCH, uid, rule, type); |
| 359 | break; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 360 | case LOW_POWER_STANDBY: |
| 361 | res = updateOwnerMapEntry(LOW_POWER_STANDBY_MATCH, uid, rule, type); |
| 362 | break; |
Motomu Utsumi | 966ff7f | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 363 | case LOCKDOWN: |
| 364 | res = updateOwnerMapEntry(LOCKDOWN_VPN_MATCH, uid, rule, type); |
| 365 | break; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 366 | case NONE: |
| 367 | default: |
| 368 | ALOGW("Unknown child chain: %d", chain); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | if (!isOk(res)) { |
| 372 | ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain, |
| 373 | res.msg().c_str(), rule, type); |
| 374 | return -res.code(); |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | Status TrafficController::replaceRulesInMap(const UidOwnerMatchType match, |
| 380 | const std::vector<int32_t>& uids) { |
| 381 | std::lock_guard guard(mMutex); |
| 382 | std::set<int32_t> uidSet(uids.begin(), uids.end()); |
| 383 | std::vector<uint32_t> uidsToDelete; |
| 384 | auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key, |
| 385 | const BpfMap<uint32_t, UidOwnerValue>&) { |
| 386 | if (uidSet.find((int32_t) key) == uidSet.end()) { |
| 387 | uidsToDelete.push_back(key); |
| 388 | } |
| 389 | return base::Result<void>(); |
| 390 | }; |
| 391 | RETURN_IF_NOT_OK(mUidOwnerMap.iterate(getUidsToDelete)); |
| 392 | |
| 393 | for(auto uid : uidsToDelete) { |
| 394 | RETURN_IF_NOT_OK(removeRule(uid, match)); |
| 395 | } |
| 396 | |
| 397 | for (auto uid : uids) { |
| 398 | RETURN_IF_NOT_OK(addRule(uid, match)); |
| 399 | } |
| 400 | return netdutils::status::ok; |
| 401 | } |
| 402 | |
| 403 | Status TrafficController::addUidInterfaceRules(const int iif, |
| 404 | const std::vector<int32_t>& uidsToAdd) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 405 | std::lock_guard guard(mMutex); |
| 406 | |
| 407 | for (auto uid : uidsToAdd) { |
| 408 | netdutils::Status result = addRule(uid, IIF_MATCH, iif); |
| 409 | if (!isOk(result)) { |
| 410 | ALOGW("addRule failed(%d): uid=%d iif=%d", result.code(), uid, iif); |
| 411 | } |
| 412 | } |
| 413 | return netdutils::status::ok; |
| 414 | } |
| 415 | |
| 416 | Status TrafficController::removeUidInterfaceRules(const std::vector<int32_t>& uidsToDelete) { |
| 417 | std::lock_guard guard(mMutex); |
| 418 | |
| 419 | for (auto uid : uidsToDelete) { |
| 420 | netdutils::Status result = removeRule(uid, IIF_MATCH); |
| 421 | if (!isOk(result)) { |
| 422 | ALOGW("removeRule failed(%d): uid=%d", result.code(), uid); |
| 423 | } |
| 424 | } |
| 425 | return netdutils::status::ok; |
| 426 | } |
| 427 | |
| 428 | int TrafficController::replaceUidOwnerMap(const std::string& name, bool isAllowlist __unused, |
| 429 | const std::vector<int32_t>& uids) { |
| 430 | // FirewallRule rule = isAllowlist ? ALLOW : DENY; |
| 431 | // FirewallType type = isAllowlist ? ALLOWLIST : DENYLIST; |
| 432 | Status res; |
| 433 | if (!name.compare(LOCAL_DOZABLE)) { |
| 434 | res = replaceRulesInMap(DOZABLE_MATCH, uids); |
| 435 | } else if (!name.compare(LOCAL_STANDBY)) { |
| 436 | res = replaceRulesInMap(STANDBY_MATCH, uids); |
| 437 | } else if (!name.compare(LOCAL_POWERSAVE)) { |
| 438 | res = replaceRulesInMap(POWERSAVE_MATCH, uids); |
| 439 | } else if (!name.compare(LOCAL_RESTRICTED)) { |
| 440 | res = replaceRulesInMap(RESTRICTED_MATCH, uids); |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 441 | } else if (!name.compare(LOCAL_LOW_POWER_STANDBY)) { |
| 442 | res = replaceRulesInMap(LOW_POWER_STANDBY_MATCH, uids); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 443 | } else { |
| 444 | ALOGE("unknown chain name: %s", name.c_str()); |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | if (!isOk(res)) { |
| 448 | ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str()); |
| 449 | return -res.code(); |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) { |
| 455 | std::lock_guard guard(mMutex); |
| 456 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
| 457 | auto oldConfiguration = mConfigurationMap.readValue(key); |
| 458 | if (!oldConfiguration.ok()) { |
| 459 | ALOGE("Cannot read the old configuration from map: %s", |
| 460 | oldConfiguration.error().message().c_str()); |
| 461 | return -oldConfiguration.error().code(); |
| 462 | } |
| 463 | Status res; |
| 464 | BpfConfig newConfiguration; |
| 465 | uint8_t match; |
| 466 | switch (chain) { |
| 467 | case DOZABLE: |
| 468 | match = DOZABLE_MATCH; |
| 469 | break; |
| 470 | case STANDBY: |
| 471 | match = STANDBY_MATCH; |
| 472 | break; |
| 473 | case POWERSAVE: |
| 474 | match = POWERSAVE_MATCH; |
| 475 | break; |
| 476 | case RESTRICTED: |
| 477 | match = RESTRICTED_MATCH; |
| 478 | break; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 479 | case LOW_POWER_STANDBY: |
| 480 | match = LOW_POWER_STANDBY_MATCH; |
| 481 | break; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 482 | default: |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | newConfiguration = |
| 486 | enable ? (oldConfiguration.value() | match) : (oldConfiguration.value() & (~match)); |
| 487 | res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST); |
| 488 | if (!isOk(res)) { |
| 489 | ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str()); |
| 490 | } |
| 491 | return -res.code(); |
| 492 | } |
| 493 | |
| 494 | Status TrafficController::swapActiveStatsMap() { |
| 495 | std::lock_guard guard(mMutex); |
| 496 | |
| 497 | uint32_t key = CURRENT_STATS_MAP_CONFIGURATION_KEY; |
| 498 | auto oldConfiguration = mConfigurationMap.readValue(key); |
| 499 | if (!oldConfiguration.ok()) { |
| 500 | ALOGE("Cannot read the old configuration from map: %s", |
| 501 | oldConfiguration.error().message().c_str()); |
| 502 | return Status(oldConfiguration.error().code(), oldConfiguration.error().message()); |
| 503 | } |
| 504 | |
| 505 | // Write to the configuration map to inform the kernel eBPF program to switch |
| 506 | // from using one map to the other. Use flag BPF_EXIST here since the map should |
| 507 | // be already populated in initMaps. |
| 508 | uint8_t newConfigure = (oldConfiguration.value() == SELECT_MAP_A) ? SELECT_MAP_B : SELECT_MAP_A; |
| 509 | auto res = mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY, newConfigure, |
| 510 | BPF_EXIST); |
| 511 | if (!res.ok()) { |
| 512 | ALOGE("Failed to toggle the stats map: %s", strerror(res.error().code())); |
| 513 | return res; |
| 514 | } |
| 515 | // After changing the config, we need to make sure all the current running |
| 516 | // eBPF programs are finished and all the CPUs are aware of this config change |
| 517 | // before we modify the old map. So we do a special hack here to wait for |
| 518 | // the kernel to do a synchronize_rcu(). Once the kernel called |
| 519 | // synchronize_rcu(), the config we just updated will be available to all cores |
| 520 | // and the next eBPF programs triggered inside the kernel will use the new |
| 521 | // map configuration. So once this function returns we can safely modify the |
| 522 | // old stats map without concerning about race between the kernel and |
| 523 | // userspace. |
| 524 | int ret = synchronizeKernelRCU(); |
| 525 | if (ret) { |
| 526 | ALOGE("map swap synchronize_rcu() ended with failure: %s", strerror(-ret)); |
| 527 | return statusFromErrno(-ret, "map swap synchronize_rcu() failed"); |
| 528 | } |
| 529 | return netdutils::status::ok; |
| 530 | } |
| 531 | |
| 532 | void TrafficController::setPermissionForUids(int permission, const std::vector<uid_t>& uids) { |
| 533 | std::lock_guard guard(mMutex); |
| 534 | if (permission == INetd::PERMISSION_UNINSTALLED) { |
| 535 | for (uid_t uid : uids) { |
| 536 | // Clean up all permission information for the related uid if all the |
| 537 | // packages related to it are uninstalled. |
| 538 | mPrivilegedUser.erase(uid); |
| 539 | Status ret = mUidPermissionMap.deleteValue(uid); |
| 540 | if (!isOk(ret) && ret.code() != ENOENT) { |
| 541 | ALOGE("Failed to clean up the permission for %u: %s", uid, strerror(ret.code())); |
| 542 | } |
| 543 | } |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | bool privileged = (permission & INetd::PERMISSION_UPDATE_DEVICE_STATS); |
| 548 | |
| 549 | for (uid_t uid : uids) { |
| 550 | if (privileged) { |
| 551 | mPrivilegedUser.insert(uid); |
| 552 | } else { |
| 553 | mPrivilegedUser.erase(uid); |
| 554 | } |
| 555 | |
| 556 | // The map stores all the permissions that the UID has, except if the only permission |
| 557 | // the UID has is the INTERNET permission, then the UID should not appear in the map. |
| 558 | if (permission != INetd::PERMISSION_INTERNET) { |
| 559 | Status ret = mUidPermissionMap.writeValue(uid, permission, BPF_ANY); |
| 560 | if (!isOk(ret)) { |
| 561 | ALOGE("Failed to set permission: %s of uid(%u) to permission map: %s", |
| 562 | UidPermissionTypeToString(permission).c_str(), uid, strerror(ret.code())); |
| 563 | } |
| 564 | } else { |
| 565 | Status ret = mUidPermissionMap.deleteValue(uid); |
| 566 | if (!isOk(ret) && ret.code() != ENOENT) { |
| 567 | ALOGE("Failed to remove uid %u from permission map: %s", uid, strerror(ret.code())); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | std::string getProgramStatus(const char *path) { |
| 574 | int ret = access(path, R_OK); |
| 575 | if (ret == 0) { |
| 576 | return StringPrintf("OK"); |
| 577 | } |
| 578 | if (ret != 0 && errno == ENOENT) { |
| 579 | return StringPrintf("program is missing at: %s", path); |
| 580 | } |
| 581 | return StringPrintf("check Program %s error: %s", path, strerror(errno)); |
| 582 | } |
| 583 | |
| 584 | std::string getMapStatus(const base::unique_fd& map_fd, const char* path) { |
| 585 | if (map_fd.get() < 0) { |
| 586 | return StringPrintf("map fd lost"); |
| 587 | } |
| 588 | if (access(path, F_OK) != 0) { |
| 589 | return StringPrintf("map not pinned to location: %s", path); |
| 590 | } |
| 591 | return StringPrintf("OK"); |
| 592 | } |
| 593 | |
| 594 | // NOLINTNEXTLINE(google-runtime-references): grandfathered pass by non-const reference |
| 595 | void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) { |
| 596 | dw.blankline(); |
| 597 | dw.println("%s:", mapName.c_str()); |
| 598 | if (!header.empty()) { |
| 599 | dw.println(header); |
| 600 | } |
| 601 | } |
| 602 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 603 | void TrafficController::dump(int fd, bool verbose) { |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 604 | std::lock_guard guard(mMutex); |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 605 | DumpWriter dw(fd); |
| 606 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 607 | ScopedIndent indentTop(dw); |
| 608 | dw.println("TrafficController"); |
| 609 | |
| 610 | ScopedIndent indentPreBpfModule(dw); |
| 611 | |
| 612 | dw.blankline(); |
| 613 | dw.println("mCookieTagMap status: %s", |
| 614 | getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str()); |
| 615 | dw.println("mUidCounterSetMap status: %s", |
| 616 | getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str()); |
| 617 | dw.println("mAppUidStatsMap status: %s", |
| 618 | getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str()); |
| 619 | dw.println("mStatsMapA status: %s", |
| 620 | getMapStatus(mStatsMapA.getMap(), STATS_MAP_A_PATH).c_str()); |
| 621 | dw.println("mStatsMapB status: %s", |
| 622 | getMapStatus(mStatsMapB.getMap(), STATS_MAP_B_PATH).c_str()); |
| 623 | dw.println("mIfaceIndexNameMap status: %s", |
| 624 | getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str()); |
| 625 | dw.println("mIfaceStatsMap status: %s", |
| 626 | getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str()); |
| 627 | dw.println("mConfigurationMap status: %s", |
| 628 | getMapStatus(mConfigurationMap.getMap(), CONFIGURATION_MAP_PATH).c_str()); |
| 629 | dw.println("mUidOwnerMap status: %s", |
| 630 | getMapStatus(mUidOwnerMap.getMap(), UID_OWNER_MAP_PATH).c_str()); |
| 631 | |
| 632 | dw.blankline(); |
| 633 | dw.println("Cgroup ingress program status: %s", |
| 634 | getProgramStatus(BPF_INGRESS_PROG_PATH).c_str()); |
| 635 | dw.println("Cgroup egress program status: %s", getProgramStatus(BPF_EGRESS_PROG_PATH).c_str()); |
| 636 | dw.println("xt_bpf ingress program status: %s", |
| 637 | getProgramStatus(XT_BPF_INGRESS_PROG_PATH).c_str()); |
| 638 | dw.println("xt_bpf egress program status: %s", |
| 639 | getProgramStatus(XT_BPF_EGRESS_PROG_PATH).c_str()); |
| 640 | dw.println("xt_bpf bandwidth allowlist program status: %s", |
| 641 | getProgramStatus(XT_BPF_ALLOWLIST_PROG_PATH).c_str()); |
| 642 | dw.println("xt_bpf bandwidth denylist program status: %s", |
| 643 | getProgramStatus(XT_BPF_DENYLIST_PROG_PATH).c_str()); |
| 644 | |
| 645 | if (!verbose) { |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | dw.blankline(); |
| 650 | dw.println("BPF map content:"); |
| 651 | |
| 652 | ScopedIndent indentForMapContent(dw); |
| 653 | |
| 654 | // Print CookieTagMap content. |
| 655 | dumpBpfMap("mCookieTagMap", dw, ""); |
| 656 | const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTagValue& value, |
| 657 | const BpfMap<uint64_t, UidTagValue>&) { |
| 658 | dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid); |
| 659 | return base::Result<void>(); |
| 660 | }; |
| 661 | base::Result<void> res = mCookieTagMap.iterateWithValue(printCookieTagInfo); |
| 662 | if (!res.ok()) { |
| 663 | dw.println("mCookieTagMap print end with error: %s", res.error().message().c_str()); |
| 664 | } |
| 665 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 666 | // Print UidCounterSetMap content. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 667 | dumpBpfMap("mUidCounterSetMap", dw, ""); |
| 668 | const auto printUidInfo = [&dw](const uint32_t& key, const uint8_t& value, |
| 669 | const BpfMap<uint32_t, uint8_t>&) { |
| 670 | dw.println("%u %u", key, value); |
| 671 | return base::Result<void>(); |
| 672 | }; |
| 673 | res = mUidCounterSetMap.iterateWithValue(printUidInfo); |
| 674 | if (!res.ok()) { |
| 675 | dw.println("mUidCounterSetMap print end with error: %s", res.error().message().c_str()); |
| 676 | } |
| 677 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 678 | // Print AppUidStatsMap content. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 679 | std::string appUidStatsHeader = StringPrintf("uid rxBytes rxPackets txBytes txPackets"); |
| 680 | dumpBpfMap("mAppUidStatsMap:", dw, appUidStatsHeader); |
| 681 | auto printAppUidStatsInfo = [&dw](const uint32_t& key, const StatsValue& value, |
| 682 | const BpfMap<uint32_t, StatsValue>&) { |
| 683 | dw.println("%u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxBytes, |
| 684 | value.rxPackets, value.txBytes, value.txPackets); |
| 685 | return base::Result<void>(); |
| 686 | }; |
| 687 | res = mAppUidStatsMap.iterateWithValue(printAppUidStatsInfo); |
| 688 | if (!res.ok()) { |
| 689 | dw.println("mAppUidStatsMap print end with error: %s", res.error().message().c_str()); |
| 690 | } |
| 691 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 692 | // Print uidStatsMap content. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 693 | std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes" |
| 694 | " rxPackets txBytes txPackets"); |
| 695 | dumpBpfMap("mStatsMapA", dw, statsHeader); |
| 696 | const auto printStatsInfo = [&dw, this](const StatsKey& key, const StatsValue& value, |
| 697 | const BpfMap<StatsKey, StatsValue>&) { |
| 698 | uint32_t ifIndex = key.ifaceIndex; |
| 699 | auto ifname = mIfaceIndexNameMap.readValue(ifIndex); |
| 700 | if (!ifname.ok()) { |
| 701 | ifname = IfaceValue{"unknown"}; |
| 702 | } |
| 703 | dw.println("%u %s 0x%x %u %u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, ifIndex, |
| 704 | ifname.value().name, key.tag, key.uid, key.counterSet, value.rxBytes, |
| 705 | value.rxPackets, value.txBytes, value.txPackets); |
| 706 | return base::Result<void>(); |
| 707 | }; |
| 708 | res = mStatsMapA.iterateWithValue(printStatsInfo); |
| 709 | if (!res.ok()) { |
| 710 | dw.println("mStatsMapA print end with error: %s", res.error().message().c_str()); |
| 711 | } |
| 712 | |
| 713 | // Print TagStatsMap content. |
| 714 | dumpBpfMap("mStatsMapB", dw, statsHeader); |
| 715 | res = mStatsMapB.iterateWithValue(printStatsInfo); |
| 716 | if (!res.ok()) { |
| 717 | dw.println("mStatsMapB print end with error: %s", res.error().message().c_str()); |
| 718 | } |
| 719 | |
| 720 | // Print ifaceIndexToNameMap content. |
| 721 | dumpBpfMap("mIfaceIndexNameMap", dw, ""); |
| 722 | const auto printIfaceNameInfo = [&dw](const uint32_t& key, const IfaceValue& value, |
| 723 | const BpfMap<uint32_t, IfaceValue>&) { |
| 724 | const char* ifname = value.name; |
| 725 | dw.println("ifaceIndex=%u ifaceName=%s", key, ifname); |
| 726 | return base::Result<void>(); |
| 727 | }; |
| 728 | res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo); |
| 729 | if (!res.ok()) { |
| 730 | dw.println("mIfaceIndexNameMap print end with error: %s", res.error().message().c_str()); |
| 731 | } |
| 732 | |
| 733 | // Print ifaceStatsMap content |
| 734 | std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes" |
| 735 | " txPackets"); |
| 736 | dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader); |
| 737 | const auto printIfaceStatsInfo = [&dw, this](const uint32_t& key, const StatsValue& value, |
| 738 | const BpfMap<uint32_t, StatsValue>&) { |
| 739 | auto ifname = mIfaceIndexNameMap.readValue(key); |
| 740 | if (!ifname.ok()) { |
| 741 | ifname = IfaceValue{"unknown"}; |
| 742 | } |
| 743 | dw.println("%u %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, ifname.value().name, |
| 744 | value.rxBytes, value.rxPackets, value.txBytes, value.txPackets); |
| 745 | return base::Result<void>(); |
| 746 | }; |
| 747 | res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo); |
| 748 | if (!res.ok()) { |
| 749 | dw.println("mIfaceStatsMap print end with error: %s", res.error().message().c_str()); |
| 750 | } |
| 751 | |
| 752 | dw.blankline(); |
| 753 | |
| 754 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
| 755 | auto configuration = mConfigurationMap.readValue(key); |
| 756 | if (configuration.ok()) { |
| 757 | dw.println("current ownerMatch configuration: %d%s", configuration.value(), |
| 758 | uidMatchTypeToString(configuration.value()).c_str()); |
| 759 | } else { |
| 760 | dw.println("mConfigurationMap read ownerMatch configure failed with error: %s", |
| 761 | configuration.error().message().c_str()); |
| 762 | } |
| 763 | |
| 764 | key = CURRENT_STATS_MAP_CONFIGURATION_KEY; |
| 765 | configuration = mConfigurationMap.readValue(key); |
| 766 | if (configuration.ok()) { |
| 767 | const char* statsMapDescription = "???"; |
| 768 | switch (configuration.value()) { |
| 769 | case SELECT_MAP_A: |
| 770 | statsMapDescription = "SELECT_MAP_A"; |
| 771 | break; |
| 772 | case SELECT_MAP_B: |
| 773 | statsMapDescription = "SELECT_MAP_B"; |
| 774 | break; |
| 775 | // No default clause, so if we ever add a third map, this code will fail to build. |
| 776 | } |
| 777 | dw.println("current statsMap configuration: %d %s", configuration.value(), |
| 778 | statsMapDescription); |
| 779 | } else { |
| 780 | dw.println("mConfigurationMap read stats map configure failed with error: %s", |
| 781 | configuration.error().message().c_str()); |
| 782 | } |
| 783 | dumpBpfMap("mUidOwnerMap", dw, ""); |
| 784 | const auto printUidMatchInfo = [&dw, this](const uint32_t& key, const UidOwnerValue& value, |
| 785 | const BpfMap<uint32_t, UidOwnerValue>&) { |
| 786 | if (value.rule & IIF_MATCH) { |
| 787 | auto ifname = mIfaceIndexNameMap.readValue(value.iif); |
| 788 | if (ifname.ok()) { |
| 789 | dw.println("%u %s %s", key, uidMatchTypeToString(value.rule).c_str(), |
| 790 | ifname.value().name); |
| 791 | } else { |
| 792 | dw.println("%u %s %u", key, uidMatchTypeToString(value.rule).c_str(), value.iif); |
| 793 | } |
| 794 | } else { |
| 795 | dw.println("%u %s", key, uidMatchTypeToString(value.rule).c_str()); |
| 796 | } |
| 797 | return base::Result<void>(); |
| 798 | }; |
| 799 | res = mUidOwnerMap.iterateWithValue(printUidMatchInfo); |
| 800 | if (!res.ok()) { |
| 801 | dw.println("mUidOwnerMap print end with error: %s", res.error().message().c_str()); |
| 802 | } |
| 803 | dumpBpfMap("mUidPermissionMap", dw, ""); |
| 804 | const auto printUidPermissionInfo = [&dw](const uint32_t& key, const int& value, |
| 805 | const BpfMap<uint32_t, uint8_t>&) { |
| 806 | dw.println("%u %s", key, UidPermissionTypeToString(value).c_str()); |
| 807 | return base::Result<void>(); |
| 808 | }; |
| 809 | res = mUidPermissionMap.iterateWithValue(printUidPermissionInfo); |
| 810 | if (!res.ok()) { |
| 811 | dw.println("mUidPermissionMap print end with error: %s", res.error().message().c_str()); |
| 812 | } |
| 813 | |
| 814 | dumpBpfMap("mPrivilegedUser", dw, ""); |
| 815 | for (uid_t uid : mPrivilegedUser) { |
| 816 | dw.println("%u ALLOW_UPDATE_DEVICE_STATS", (uint32_t)uid); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | } // namespace net |
| 821 | } // namespace android |