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