Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 17 | #define LOG_TAG "TrafficController" |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 18 | #include <inttypes.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 19 | #include <linux/bpf.h> |
| 20 | #include <linux/if_ether.h> |
| 21 | #include <linux/in.h> |
| 22 | #include <linux/inet_diag.h> |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 23 | #include <linux/netlink.h> |
| 24 | #include <linux/sock_diag.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 25 | #include <linux/unistd.h> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 26 | #include <net/if.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/socket.h> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 30 | #include <sys/stat.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 31 | #include <sys/types.h> |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 32 | #include <sys/utsname.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 33 | #include <sys/wait.h> |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 34 | #include <mutex> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 35 | #include <unordered_set> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include <android-base/stringprintf.h> |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 39 | #include <android-base/strings.h> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 40 | #include <android-base/unique_fd.h> |
| 41 | #include <netdutils/StatusOr.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 42 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 43 | #include <netdutils/Misc.h> |
| 44 | #include <netdutils/Syscalls.h> |
Suren Baghdasaryan | e072a3c | 2019-01-16 14:36:07 -0800 | [diff] [blame] | 45 | #include <processgroup/processgroup.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 46 | #include "TrafficController.h" |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 47 | #include "bpf/BpfMap.h" |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 48 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 49 | #include "FirewallController.h" |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 50 | #include "InterfaceController.h" |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 51 | #include "NetlinkListener.h" |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 52 | #include "netdutils/DumpWriter.h" |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 53 | #include "qtaguid/qtaguid.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 54 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 55 | namespace android { |
| 56 | namespace net { |
| 57 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 58 | using base::StringPrintf; |
| 59 | using base::unique_fd; |
Maciej Żenczykowski | ec36c89 | 2021-01-17 03:25:56 -0800 | [diff] [blame] | 60 | using bpf::getSocketCookie; |
| 61 | using bpf::NONEXISTENT_COOKIE; |
| 62 | using bpf::OVERFLOW_COUNTERSET; |
| 63 | using bpf::retrieveProgram; |
| 64 | using bpf::synchronizeKernelRCU; |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 65 | using netdutils::DumpWriter; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 66 | using netdutils::extract; |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 67 | using netdutils::ScopedIndent; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 68 | using netdutils::Slice; |
| 69 | using netdutils::sSyscalls; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 70 | using netdutils::Status; |
| 71 | using netdutils::statusFromErrno; |
| 72 | using netdutils::StatusOr; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 73 | using netdutils::status::ok; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 74 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 75 | constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY; |
| 76 | constexpr int kSockDiagDoneMsgType = NLMSG_DONE; |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 77 | constexpr int PER_UID_STATS_ENTRIES_LIMIT = 500; |
Chenbo Feng | 05662be | 2019-05-02 12:47:25 -0700 | [diff] [blame] | 78 | // At most 90% of the stats map may be used by tagged traffic entries. This ensures |
| 79 | // that 10% of the map is always available to count untagged traffic, one entry per UID. |
| 80 | // Otherwise, apps would be able to avoid data usage accounting entirely by filling up the |
| 81 | // map with tagged traffic entries. |
Chenbo Feng | fd88f9d | 2019-05-01 12:59:09 -0700 | [diff] [blame] | 82 | constexpr int TOTAL_UID_STATS_ENTRIES_LIMIT = STATS_MAP_SIZE * 0.9; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 83 | |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 84 | static_assert(BPF_PERMISSION_INTERNET == INetd::PERMISSION_INTERNET, |
| 85 | "Mismatch between BPF and AIDL permissions: PERMISSION_INTERNET"); |
| 86 | static_assert(BPF_PERMISSION_UPDATE_DEVICE_STATS == INetd::PERMISSION_UPDATE_DEVICE_STATS, |
| 87 | "Mismatch between BPF and AIDL permissions: PERMISSION_UPDATE_DEVICE_STATS"); |
Chenbo Feng | fd88f9d | 2019-05-01 12:59:09 -0700 | [diff] [blame] | 88 | static_assert(STATS_MAP_SIZE - TOTAL_UID_STATS_ENTRIES_LIMIT > 100, |
| 89 | "The limit for stats map is to high, stats data may be lost due to overflow"); |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 90 | |
Maciej Żenczykowski | c0eca92 | 2020-04-01 18:02:37 -0700 | [diff] [blame] | 91 | #define FLAG_MSG_TRANS(result, flag, value) \ |
| 92 | do { \ |
| 93 | if ((value) & (flag)) { \ |
| 94 | (result).append(" " #flag); \ |
| 95 | (value) &= ~(flag); \ |
| 96 | } \ |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 97 | } while (0) |
| 98 | |
| 99 | const std::string uidMatchTypeToString(uint8_t match) { |
| 100 | std::string matchType; |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 101 | FLAG_MSG_TRANS(matchType, HAPPY_BOX_MATCH, match); |
| 102 | FLAG_MSG_TRANS(matchType, PENALTY_BOX_MATCH, match); |
| 103 | FLAG_MSG_TRANS(matchType, DOZABLE_MATCH, match); |
| 104 | FLAG_MSG_TRANS(matchType, STANDBY_MATCH, match); |
| 105 | FLAG_MSG_TRANS(matchType, POWERSAVE_MATCH, match); |
Patrick Rohr | fa0036f | 2020-12-02 16:22:28 +0100 | [diff] [blame] | 106 | FLAG_MSG_TRANS(matchType, RESTRICTED_MATCH, match); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 107 | FLAG_MSG_TRANS(matchType, IIF_MATCH, match); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 108 | if (match) { |
| 109 | return StringPrintf("Unknown match: %u", match); |
| 110 | } |
| 111 | return matchType; |
| 112 | } |
| 113 | |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 114 | bool TrafficController::hasUpdateDeviceStatsPermission(uid_t uid) { |
Chenbo Feng | 3969c26 | 2019-03-19 14:53:57 -0700 | [diff] [blame] | 115 | // This implementation is the same logic as method ActivityManager#checkComponentPermission. |
| 116 | // It implies that the calling uid can never be the same as PER_USER_RANGE. |
| 117 | uint32_t appId = uid % PER_USER_RANGE; |
| 118 | return ((appId == AID_ROOT) || (appId == AID_SYSTEM) || |
| 119 | mPrivilegedUser.find(appId) != mPrivilegedUser.end()); |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Yi Kong | 3d82f38 | 2019-10-01 21:52:14 -0700 | [diff] [blame] | 122 | const std::string UidPermissionTypeToString(int permission) { |
Chenbo Feng | 84f48cd | 2019-04-22 15:34:40 -0700 | [diff] [blame] | 123 | if (permission == INetd::PERMISSION_NONE) { |
| 124 | return "PERMISSION_NONE"; |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 125 | } |
Chenbo Feng | 527908a | 2019-04-02 20:14:11 -0700 | [diff] [blame] | 126 | if (permission == INetd::PERMISSION_UNINSTALLED) { |
| 127 | // This should never appear in the map, complain loudly if it does. |
| 128 | return "PERMISSION_UNINSTALLED error!"; |
| 129 | } |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 130 | std::string permissionType; |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 131 | FLAG_MSG_TRANS(permissionType, BPF_PERMISSION_INTERNET, permission); |
| 132 | FLAG_MSG_TRANS(permissionType, BPF_PERMISSION_UPDATE_DEVICE_STATS, permission); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 133 | if (permission) { |
| 134 | return StringPrintf("Unknown permission: %u", permission); |
| 135 | } |
| 136 | return permissionType; |
| 137 | } |
| 138 | |
Chenbo Feng | 4958664 | 2018-08-30 18:01:53 -0700 | [diff] [blame] | 139 | StatusOr<std::unique_ptr<NetlinkListenerInterface>> TrafficController::makeSkDestroyListener() { |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 140 | const auto& sys = sSyscalls.get(); |
| 141 | ASSIGN_OR_RETURN(auto event, sys.eventfd(0, EFD_CLOEXEC)); |
| 142 | const int domain = AF_NETLINK; |
| 143 | const int type = SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK; |
| 144 | const int protocol = NETLINK_INET_DIAG; |
| 145 | ASSIGN_OR_RETURN(auto sock, sys.socket(domain, type, protocol)); |
| 146 | |
Lorenzo Colitti | 436efdb | 2018-07-26 17:20:57 +0900 | [diff] [blame] | 147 | // TODO: if too many sockets are closed too quickly, we can overflow the socket buffer, and |
| 148 | // some entries in mCookieTagMap will not be freed. In order to fix this we would need to |
| 149 | // periodically dump all sockets and remove the tag entries for sockets that have been closed. |
| 150 | // For now, set a large-enough buffer that we can close hundreds of sockets without getting |
| 151 | // ENOBUFS and leaking mCookieTagMap entries. |
| 152 | int rcvbuf = 512 * 1024; |
| 153 | auto ret = sys.setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); |
| 154 | if (!ret.ok()) { |
| 155 | ALOGW("Failed to set SkDestroyListener buffer size to %d: %s", rcvbuf, ret.msg().c_str()); |
| 156 | } |
| 157 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 158 | sockaddr_nl addr = { |
| 159 | .nl_family = AF_NETLINK, |
| 160 | .nl_groups = 1 << (SKNLGRP_INET_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET_UDP_DESTROY - 1) | |
| 161 | 1 << (SKNLGRP_INET6_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET6_UDP_DESTROY - 1)}; |
| 162 | RETURN_IF_NOT_OK(sys.bind(sock, addr)); |
| 163 | |
| 164 | const sockaddr_nl kernel = {.nl_family = AF_NETLINK}; |
| 165 | RETURN_IF_NOT_OK(sys.connect(sock, kernel)); |
| 166 | |
| 167 | std::unique_ptr<NetlinkListenerInterface> listener = |
Chenbo Feng | 4629670 | 2018-08-23 12:20:22 -0700 | [diff] [blame] | 168 | std::make_unique<NetlinkListener>(std::move(event), std::move(sock), "SkDestroyListen"); |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 169 | |
| 170 | return listener; |
| 171 | } |
| 172 | |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 173 | TrafficController::TrafficController() |
Maciej Żenczykowski | ec36c89 | 2021-01-17 03:25:56 -0800 | [diff] [blame] | 174 | : mPerUidStatsEntriesLimit(PER_UID_STATS_ENTRIES_LIMIT), |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 175 | mTotalUidStatsEntriesLimit(TOTAL_UID_STATS_ENTRIES_LIMIT) {} |
| 176 | |
| 177 | TrafficController::TrafficController(uint32_t perUidLimit, uint32_t totalLimit) |
Maciej Żenczykowski | ec36c89 | 2021-01-17 03:25:56 -0800 | [diff] [blame] | 178 | : mPerUidStatsEntriesLimit(perUidLimit), mTotalUidStatsEntriesLimit(totalLimit) {} |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 179 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 180 | Status TrafficController::initMaps() { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 181 | std::lock_guard guard(mMutex); |
Maciej Żenczykowski | 565b6fb | 2020-01-27 01:58:40 -0800 | [diff] [blame] | 182 | |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 183 | RETURN_IF_NOT_OK(mCookieTagMap.init(COOKIE_TAG_MAP_PATH)); |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 184 | RETURN_IF_NOT_OK(mUidCounterSetMap.init(UID_COUNTERSET_MAP_PATH)); |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 185 | RETURN_IF_NOT_OK(mAppUidStatsMap.init(APP_UID_STATS_MAP_PATH)); |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 186 | RETURN_IF_NOT_OK(mStatsMapA.init(STATS_MAP_A_PATH)); |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 187 | RETURN_IF_NOT_OK(mStatsMapB.init(STATS_MAP_B_PATH)); |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 188 | RETURN_IF_NOT_OK(mIfaceIndexNameMap.init(IFACE_INDEX_NAME_MAP_PATH)); |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 189 | RETURN_IF_NOT_OK(mIfaceStatsMap.init(IFACE_STATS_MAP_PATH)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 190 | |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 191 | RETURN_IF_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 192 | RETURN_IF_NOT_OK( |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 193 | mConfigurationMap.writeValue(UID_RULES_CONFIGURATION_KEY, DEFAULT_CONFIG, BPF_ANY)); |
| 194 | RETURN_IF_NOT_OK(mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY, SELECT_MAP_A, |
| 195 | BPF_ANY)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 196 | |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 197 | RETURN_IF_NOT_OK(mUidOwnerMap.init(UID_OWNER_MAP_PATH)); |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 198 | RETURN_IF_NOT_OK(mUidOwnerMap.clear()); |
Maciej Żenczykowski | 0255437 | 2019-04-01 10:44:54 -0700 | [diff] [blame] | 199 | RETURN_IF_NOT_OK(mUidPermissionMap.init(UID_PERMISSION_MAP_PATH)); |
mtk13799 | ae0704e | 2019-03-28 15:07:14 +0800 | [diff] [blame] | 200 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 201 | return netdutils::status::ok; |
| 202 | } |
| 203 | |
Maciej Żenczykowski | 8701785 | 2020-01-17 19:22:11 -0800 | [diff] [blame] | 204 | static Status attachProgramToCgroup(const char* programPath, const unique_fd& cgroupFd, |
Chenbo Feng | b2d213d | 2018-12-04 11:31:00 -0800 | [diff] [blame] | 205 | bpf_attach_type type) { |
Maciej Żenczykowski | 06227b2 | 2020-06-16 21:16:28 -0700 | [diff] [blame] | 206 | unique_fd cgroupProg(retrieveProgram(programPath)); |
Chenbo Feng | b2d213d | 2018-12-04 11:31:00 -0800 | [diff] [blame] | 207 | if (cgroupProg == -1) { |
| 208 | int ret = errno; |
| 209 | ALOGE("Failed to get program from %s: %s", programPath, strerror(ret)); |
| 210 | return statusFromErrno(ret, "cgroup program get failed"); |
| 211 | } |
| 212 | if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) { |
| 213 | int ret = errno; |
| 214 | ALOGE("Program from %s attach failed: %s", programPath, strerror(ret)); |
| 215 | return statusFromErrno(ret, "program attach failed"); |
| 216 | } |
| 217 | return netdutils::status::ok; |
| 218 | } |
| 219 | |
| 220 | static Status initPrograms() { |
Suren Baghdasaryan | e072a3c | 2019-01-16 14:36:07 -0800 | [diff] [blame] | 221 | std::string cg2_path; |
| 222 | |
| 223 | if (!CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path)) { |
| 224 | int ret = errno; |
| 225 | ALOGE("Failed to find cgroup v2 root"); |
| 226 | return statusFromErrno(ret, "Failed to find cgroup v2 root"); |
| 227 | } |
| 228 | |
| 229 | unique_fd cg_fd(open(cg2_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)); |
Chenbo Feng | b2d213d | 2018-12-04 11:31:00 -0800 | [diff] [blame] | 230 | if (cg_fd == -1) { |
| 231 | int ret = errno; |
| 232 | ALOGE("Failed to open the cgroup directory: %s", strerror(ret)); |
| 233 | return statusFromErrno(ret, "Open the cgroup directory failed"); |
| 234 | } |
| 235 | RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_EGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_EGRESS)); |
| 236 | RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_INGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_INGRESS)); |
Chenbo Feng | a51f4fa | 2019-01-15 15:03:32 -0800 | [diff] [blame] | 237 | |
| 238 | // For the devices that support cgroup socket filter, the socket filter |
| 239 | // should be loaded successfully by bpfloader. So we attach the filter to |
| 240 | // cgroup if the program is pinned properly. |
| 241 | // TODO: delete the if statement once all devices should support cgroup |
| 242 | // socket filter (ie. the minimum kernel version required is 4.14). |
| 243 | if (!access(CGROUP_SOCKET_PROG_PATH, F_OK)) { |
| 244 | RETURN_IF_NOT_OK( |
| 245 | attachProgramToCgroup(CGROUP_SOCKET_PROG_PATH, cg_fd, BPF_CGROUP_INET_SOCK_CREATE)); |
| 246 | } |
Chenbo Feng | b2d213d | 2018-12-04 11:31:00 -0800 | [diff] [blame] | 247 | return netdutils::status::ok; |
| 248 | } |
| 249 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 250 | Status TrafficController::start() { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 251 | /* When netd restarts from a crash without total system reboot, the program |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 252 | * is still attached to the cgroup, detach it so the program can be freed |
| 253 | * and we can load and attach new program into the target cgroup. |
| 254 | * |
| 255 | * TODO: Scrape existing socket when run-time restart and clean up the map |
| 256 | * if the socket no longer exist |
| 257 | */ |
| 258 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 259 | RETURN_IF_NOT_OK(initMaps()); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 260 | |
Chenbo Feng | b2d213d | 2018-12-04 11:31:00 -0800 | [diff] [blame] | 261 | RETURN_IF_NOT_OK(initPrograms()); |
| 262 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 263 | // Fetch the list of currently-existing interfaces. At this point NetlinkHandler is |
| 264 | // already running, so it will call addInterface() when any new interface appears. |
| 265 | std::map<std::string, uint32_t> ifacePairs; |
| 266 | ASSIGN_OR_RETURN(ifacePairs, InterfaceController::getIfaceList()); |
| 267 | for (const auto& ifacePair:ifacePairs) { |
| 268 | addInterface(ifacePair.first.c_str(), ifacePair.second); |
| 269 | } |
| 270 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 271 | auto result = makeSkDestroyListener(); |
| 272 | if (!isOk(result)) { |
| 273 | ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str()); |
| 274 | } else { |
| 275 | mSkDestroyListener = std::move(result.value()); |
| 276 | } |
| 277 | // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function. |
| 278 | const auto rxHandler = [this](const nlmsghdr&, const Slice msg) { |
Chenbo Feng | f01f530 | 2019-05-07 12:28:13 -0700 | [diff] [blame] | 279 | std::lock_guard guard(mMutex); |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 280 | inet_diag_msg diagmsg = {}; |
| 281 | if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 282 | ALOGE("Unrecognized netlink message: %s", toString(msg).c_str()); |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) | |
| 286 | (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32); |
| 287 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 288 | Status s = mCookieTagMap.deleteValue(sock_cookie); |
Chenbo Feng | 1d4c5db | 2018-10-18 15:50:46 -0700 | [diff] [blame] | 289 | if (!isOk(s) && s.code() != ENOENT) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 290 | ALOGE("Failed to delete cookie %" PRIx64 ": %s", sock_cookie, toString(s).c_str()); |
| 291 | return; |
| 292 | } |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 293 | }; |
| 294 | expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler)); |
| 295 | |
| 296 | // In case multiple netlink message comes in as a stream, we need to handle the rxDone message |
| 297 | // properly. |
| 298 | const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) { |
| 299 | // Ignore NLMSG_DONE messages |
| 300 | inet_diag_msg diagmsg = {}; |
| 301 | extract(msg, diagmsg); |
| 302 | }; |
| 303 | expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler)); |
| 304 | |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 305 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 308 | int TrafficController::tagSocket(int sockFd, uint32_t tag, uid_t uid, uid_t callingUid) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 309 | std::lock_guard guard(mMutex); |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 310 | if (uid != callingUid && !hasUpdateDeviceStatsPermission(callingUid)) { |
| 311 | return -EPERM; |
| 312 | } |
| 313 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 314 | uint64_t sock_cookie = getSocketCookie(sockFd); |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 315 | if (sock_cookie == NONEXISTENT_COOKIE) return -errno; |
Maciej Żenczykowski | 11ec78b | 2019-12-30 06:39:32 -0800 | [diff] [blame] | 316 | UidTagValue newKey = {.uid = (uint32_t)uid, .tag = tag}; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 317 | |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 318 | uint32_t totalEntryCount = 0; |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 319 | uint32_t perUidEntryCount = 0; |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 320 | // Now we go through the stats map and count how many entries are associated |
| 321 | // with target uid. If the uid entry hit the limit for each uid, we block |
| 322 | // the request to prevent the map from overflow. It is safe here to iterate |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 323 | // over the map since when mMutex is hold, system server cannot toggle |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 324 | // the live stats map and clean it. So nobody can delete entries from the map. |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 325 | const auto countUidStatsEntries = [uid, &totalEntryCount, &perUidEntryCount]( |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 326 | const StatsKey& key, |
| 327 | const BpfMap<StatsKey, StatsValue>&) { |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 328 | if (key.uid == uid) { |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 329 | perUidEntryCount++; |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 330 | } |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 331 | totalEntryCount++; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 332 | return base::Result<void>(); |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 333 | }; |
| 334 | auto configuration = mConfigurationMap.readValue(CURRENT_STATS_MAP_CONFIGURATION_KEY); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 335 | if (!configuration.ok()) { |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 336 | ALOGE("Failed to get current configuration: %s, fd: %d", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 337 | strerror(configuration.error().code()), mConfigurationMap.getMap().get()); |
| 338 | return -configuration.error().code(); |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 339 | } |
Chenbo Feng | 36a40d1 | 2019-04-23 16:47:05 -0700 | [diff] [blame] | 340 | if (configuration.value() != SELECT_MAP_A && configuration.value() != SELECT_MAP_B) { |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 341 | ALOGE("unknown configuration value: %d", configuration.value()); |
| 342 | return -EINVAL; |
| 343 | } |
Chenbo Feng | 36a40d1 | 2019-04-23 16:47:05 -0700 | [diff] [blame] | 344 | |
| 345 | BpfMap<StatsKey, StatsValue>& currentMap = |
| 346 | (configuration.value() == SELECT_MAP_A) ? mStatsMapA : mStatsMapB; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 347 | base::Result<void> res = currentMap.iterate(countUidStatsEntries); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 348 | if (!res.ok()) { |
Chenbo Feng | 36a40d1 | 2019-04-23 16:47:05 -0700 | [diff] [blame] | 349 | ALOGE("Failed to count the stats entry in map %d: %s", currentMap.getMap().get(), |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 350 | strerror(res.error().code())); |
| 351 | return -res.error().code(); |
Chenbo Feng | 36a40d1 | 2019-04-23 16:47:05 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Chenbo Feng | 82075e5 | 2019-04-12 19:21:47 -0700 | [diff] [blame] | 354 | if (totalEntryCount > mTotalUidStatsEntriesLimit || |
| 355 | perUidEntryCount > mPerUidStatsEntriesLimit) { |
| 356 | ALOGE("Too many stats entries in the map, total count: %u, uid(%u) count: %u, blocking tag" |
| 357 | " request to prevent map overflow", |
| 358 | totalEntryCount, uid, perUidEntryCount); |
Chenbo Feng | b049513 | 2019-04-10 12:54:41 -0700 | [diff] [blame] | 359 | return -EMFILE; |
| 360 | } |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 361 | // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY |
| 362 | // flag so it will insert a new entry to the map if that value doesn't exist |
| 363 | // yet. And update the tag if there is already a tag stored. Since the eBPF |
| 364 | // program in kernel only read this map, and is protected by rcu read lock. It |
| 365 | // should be fine to cocurrently update the map while eBPF program is running. |
Chenbo Feng | 36a40d1 | 2019-04-23 16:47:05 -0700 | [diff] [blame] | 366 | res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 367 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 368 | ALOGE("Failed to tag the socket: %s, fd: %d", strerror(res.error().code()), |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 369 | mCookieTagMap.getMap().get()); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 370 | return -res.error().code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 371 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 372 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | int TrafficController::untagSocket(int sockFd) { |
Chenbo Feng | f01f530 | 2019-05-07 12:28:13 -0700 | [diff] [blame] | 376 | std::lock_guard guard(mMutex); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 377 | uint64_t sock_cookie = getSocketCookie(sockFd); |
| 378 | |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 379 | if (sock_cookie == NONEXISTENT_COOKIE) return -errno; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 380 | base::Result<void> res = mCookieTagMap.deleteValue(sock_cookie); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 381 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 382 | ALOGE("Failed to untag socket: %s\n", strerror(res.error().code())); |
| 383 | return -res.error().code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 384 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 385 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 388 | int TrafficController::setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 389 | if (counterSetNum < 0 || counterSetNum >= OVERFLOW_COUNTERSET) return -EINVAL; |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 390 | |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 391 | std::lock_guard guard(mMutex); |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 392 | if (!hasUpdateDeviceStatsPermission(callingUid)) return -EPERM; |
| 393 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 394 | // The default counter set for all uid is 0, so deleting the current counterset for that uid |
| 395 | // will automatically set it to 0. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 396 | if (counterSetNum == 0) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 397 | Status res = mUidCounterSetMap.deleteValue(uid); |
| 398 | if (isOk(res) || (!isOk(res) && res.code() == ENOENT)) { |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 399 | return 0; |
| 400 | } else { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 401 | ALOGE("Failed to delete the counterSet: %s\n", strerror(res.code())); |
| 402 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 405 | uint8_t tmpCounterSetNum = (uint8_t)counterSetNum; |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 406 | Status res = mUidCounterSetMap.writeValue(uid, tmpCounterSetNum, BPF_ANY); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 407 | if (!isOk(res)) { |
| 408 | ALOGE("Failed to set the counterSet: %s, fd: %d", strerror(res.code()), |
| 409 | mUidCounterSetMap.getMap().get()); |
| 410 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 411 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 412 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 415 | // This method only get called by system_server when an app get uinstalled, it |
| 416 | // is called inside removeUidsLocked() while holding mStatsLock. So it is safe |
| 417 | // to iterate and modify the stats maps. |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 418 | int TrafficController::deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 419 | std::lock_guard guard(mMutex); |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame] | 420 | if (!hasUpdateDeviceStatsPermission(callingUid)) return -EPERM; |
| 421 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 422 | // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 423 | // the tags related to the uid if the tag is 0. |
Maciej Żenczykowski | 11ec78b | 2019-12-30 06:39:32 -0800 | [diff] [blame] | 424 | const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key, |
| 425 | const UidTagValue& value, |
| 426 | BpfMap<uint64_t, UidTagValue>& map) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 427 | if (value.uid == uid && (value.tag == tag || tag == 0)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 428 | auto res = map.deleteValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 429 | if (res.ok() || (res.error().code() == ENOENT)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 430 | return base::Result<void>(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 431 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 432 | ALOGE("Failed to delete data(cookie = %" PRIu64 "): %s\n", key, |
| 433 | strerror(res.error().code())); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 434 | } |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 435 | // Move forward to next cookie in the map. |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 436 | return base::Result<void>(); |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 437 | }; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 438 | mCookieTagMap.iterateWithValue(deleteMatchedCookieEntries); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 439 | // Now we go through the Tag stats map and delete the data entry with correct uid and tag |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 440 | // combination. Or all tag stats under that uid if the target tag is 0. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 441 | const auto deleteMatchedUidTagEntries = [uid, tag](const StatsKey& key, |
| 442 | BpfMap<StatsKey, StatsValue>& map) { |
| 443 | if (key.uid == uid && (key.tag == tag || tag == 0)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 444 | auto res = map.deleteValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 445 | if (res.ok() || (res.error().code() == ENOENT)) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 446 | //Entry is deleted, use the current key to get a new nextKey; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 447 | return base::Result<void>(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 448 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 449 | ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag, |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 450 | strerror(res.error().code())); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 451 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 452 | return base::Result<void>(); |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 453 | }; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 454 | mStatsMapB.iterate(deleteMatchedUidTagEntries); |
| 455 | mStatsMapA.iterate(deleteMatchedUidTagEntries); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 456 | // If the tag is not zero, we already deleted all the data entry required. If tag is 0, we also |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 457 | // need to delete the stats stored in uidStatsMap and counterSet map. |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 458 | if (tag != 0) return 0; |
| 459 | |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 460 | auto res = mUidCounterSetMap.deleteValue(uid); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 461 | if (!res.ok() && res.error().code() != ENOENT) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 462 | ALOGE("Failed to delete counterSet data(uid=%u, tag=%u): %s\n", uid, tag, |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 463 | strerror(res.error().code())); |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 464 | } |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 465 | |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 466 | auto deleteAppUidStatsEntry = [uid](const uint32_t& key, |
| 467 | BpfMap<uint32_t, StatsValue>& map) -> base::Result<void> { |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 468 | if (key == uid) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 469 | auto res = map.deleteValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 470 | if (res.ok() || (res.error().code() == ENOENT)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 471 | return {}; |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 472 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 473 | ALOGE("Failed to delete data(uid=%u): %s", key, strerror(res.error().code())); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 474 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 475 | return {}; |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 476 | }; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 477 | mAppUidStatsMap.iterate(deleteAppUidStatsEntry); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 478 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 481 | int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 482 | IfaceValue iface; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 483 | if (ifaceIndex == 0) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 484 | ALOGE("Unknown interface %s(%d)", name, ifaceIndex); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 485 | return -1; |
| 486 | } |
| 487 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 488 | strlcpy(iface.name, name, sizeof(IfaceValue)); |
| 489 | Status res = mIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY); |
| 490 | if (!isOk(res)) { |
| 491 | ALOGE("Failed to add iface %s(%d): %s", name, ifaceIndex, strerror(res.code())); |
| 492 | return -res.code(); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 493 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 494 | return 0; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 497 | Status TrafficController::updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule, |
| 498 | FirewallType type) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 499 | std::lock_guard guard(mMutex); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 500 | if ((rule == ALLOW && type == ALLOWLIST) || (rule == DENY && type == DENYLIST)) { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 501 | RETURN_IF_NOT_OK(addRule(uid, match)); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 502 | } else if ((rule == ALLOW && type == DENYLIST) || (rule == DENY && type == ALLOWLIST)) { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 503 | RETURN_IF_NOT_OK(removeRule(uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 504 | } else { |
| 505 | //Cannot happen. |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 506 | return statusFromErrno(EINVAL, ""); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 507 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 508 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 511 | Status TrafficController::removeRule(uint32_t uid, UidOwnerMatchType match) { |
| 512 | auto oldMatch = mUidOwnerMap.readValue(uid); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 513 | if (oldMatch.ok()) { |
Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 514 | UidOwnerValue newMatch = { |
| 515 | .iif = (match == IIF_MATCH) ? 0 : oldMatch.value().iif, |
| 516 | .rule = static_cast<uint8_t>(oldMatch.value().rule & ~match), |
| 517 | }; |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 518 | if (newMatch.rule == 0) { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 519 | RETURN_IF_NOT_OK(mUidOwnerMap.deleteValue(uid)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 520 | } else { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 521 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 522 | } |
| 523 | } else { |
| 524 | return statusFromErrno(ENOENT, StringPrintf("uid: %u does not exist in map", uid)); |
| 525 | } |
| 526 | return netdutils::status::ok; |
| 527 | } |
| 528 | |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 529 | Status TrafficController::addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif) { |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 530 | // iif should be non-zero if and only if match == MATCH_IIF |
| 531 | if (match == IIF_MATCH && iif == 0) { |
| 532 | return statusFromErrno(EINVAL, "Interface match must have nonzero interface index"); |
| 533 | } else if (match != IIF_MATCH && iif != 0) { |
| 534 | return statusFromErrno(EINVAL, "Non-interface match must have zero interface index"); |
| 535 | } |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 536 | auto oldMatch = mUidOwnerMap.readValue(uid); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 537 | if (oldMatch.ok()) { |
Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 538 | UidOwnerValue newMatch = { |
| 539 | .iif = iif ? iif : oldMatch.value().iif, |
| 540 | .rule = static_cast<uint8_t>(oldMatch.value().rule | match), |
| 541 | }; |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 542 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 543 | } else { |
Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 544 | UidOwnerValue newMatch = { |
| 545 | .iif = iif, |
| 546 | .rule = static_cast<uint8_t>(match), |
| 547 | }; |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 548 | RETURN_IF_NOT_OK(mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 549 | } |
| 550 | return netdutils::status::ok; |
| 551 | } |
| 552 | |
Patrick Rohr | 7920397 | 2020-12-29 18:16:35 +0100 | [diff] [blame] | 553 | Status TrafficController::updateUidOwnerMap(const std::vector<uint32_t>& appUids, |
Patrick Rohr | 7db39b9 | 2020-12-29 19:41:33 +0100 | [diff] [blame] | 554 | UidOwnerMatchType matchType, |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 555 | BandwidthController::IptOp op) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 556 | std::lock_guard guard(mMutex); |
Patrick Rohr | 7920397 | 2020-12-29 18:16:35 +0100 | [diff] [blame] | 557 | for (uint32_t uid : appUids) { |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 558 | if (op == BandwidthController::IptOpDelete) { |
Patrick Rohr | 7db39b9 | 2020-12-29 19:41:33 +0100 | [diff] [blame] | 559 | RETURN_IF_NOT_OK(removeRule(uid, matchType)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 560 | } else if (op == BandwidthController::IptOpInsert) { |
Patrick Rohr | 7db39b9 | 2020-12-29 19:41:33 +0100 | [diff] [blame] | 561 | RETURN_IF_NOT_OK(addRule(uid, matchType)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 562 | } else { |
| 563 | // Cannot happen. |
Patrick Rohr | 7db39b9 | 2020-12-29 19:41:33 +0100 | [diff] [blame] | 564 | return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, matchType)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | return netdutils::status::ok; |
| 568 | } |
| 569 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 570 | int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule, |
| 571 | FirewallType type) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 572 | Status res; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 573 | switch (chain) { |
| 574 | case DOZABLE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 575 | res = updateOwnerMapEntry(DOZABLE_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 576 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 577 | case STANDBY: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 578 | res = updateOwnerMapEntry(STANDBY_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 579 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 580 | case POWERSAVE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 581 | res = updateOwnerMapEntry(POWERSAVE_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 582 | break; |
Patrick Rohr | fa0036f | 2020-12-02 16:22:28 +0100 | [diff] [blame] | 583 | case RESTRICTED: |
| 584 | res = updateOwnerMapEntry(RESTRICTED_MATCH, uid, rule, type); |
| 585 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 586 | case NONE: |
| 587 | default: |
| 588 | return -EINVAL; |
| 589 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 590 | if (!isOk(res)) { |
| 591 | ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain, |
| 592 | res.msg().c_str(), rule, type); |
| 593 | return -res.code(); |
| 594 | } |
| 595 | return 0; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Rubin Xu | 5ca8e80 | 2019-04-05 11:50:32 +0100 | [diff] [blame] | 598 | Status TrafficController::replaceRulesInMap(const UidOwnerMatchType match, |
| 599 | const std::vector<int32_t>& uids) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 600 | std::lock_guard guard(mMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 601 | std::set<int32_t> uidSet(uids.begin(), uids.end()); |
| 602 | std::vector<uint32_t> uidsToDelete; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 603 | auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key, |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 604 | const BpfMap<uint32_t, UidOwnerValue>&) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 605 | if (uidSet.find((int32_t) key) == uidSet.end()) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 606 | uidsToDelete.push_back(key); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 607 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 608 | return base::Result<void>(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 609 | }; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 610 | RETURN_IF_NOT_OK(mUidOwnerMap.iterate(getUidsToDelete)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 611 | |
| 612 | for(auto uid : uidsToDelete) { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 613 | RETURN_IF_NOT_OK(removeRule(uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | for (auto uid : uids) { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 617 | RETURN_IF_NOT_OK(addRule(uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 618 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 619 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 622 | Status TrafficController::addUidInterfaceRules(const int iif, |
| 623 | const std::vector<int32_t>& uidsToAdd) { |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 624 | if (!iif) { |
| 625 | return statusFromErrno(EINVAL, "Interface rule must specify interface"); |
| 626 | } |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 627 | std::lock_guard guard(mMutex); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 628 | |
| 629 | for (auto uid : uidsToAdd) { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 630 | netdutils::Status result = addRule(uid, IIF_MATCH, iif); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 631 | if (!isOk(result)) { |
Rubin Xu | 5ca8e80 | 2019-04-05 11:50:32 +0100 | [diff] [blame] | 632 | ALOGW("addRule failed(%d): uid=%d iif=%d", result.code(), uid, iif); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | return netdutils::status::ok; |
| 636 | } |
| 637 | |
| 638 | Status TrafficController::removeUidInterfaceRules(const std::vector<int32_t>& uidsToDelete) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 639 | std::lock_guard guard(mMutex); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 640 | |
| 641 | for (auto uid : uidsToDelete) { |
Maciej Żenczykowski | d2c23b4 | 2020-07-02 06:11:48 -0700 | [diff] [blame] | 642 | netdutils::Status result = removeRule(uid, IIF_MATCH); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 643 | if (!isOk(result)) { |
Rubin Xu | 5ca8e80 | 2019-04-05 11:50:32 +0100 | [diff] [blame] | 644 | ALOGW("removeRule failed(%d): uid=%d", result.code(), uid); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | return netdutils::status::ok; |
| 648 | } |
| 649 | |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 650 | int TrafficController::replaceUidOwnerMap(const std::string& name, bool isAllowlist __unused, |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 651 | const std::vector<int32_t>& uids) { |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 652 | // FirewallRule rule = isAllowlist ? ALLOW : DENY; |
| 653 | // FirewallType type = isAllowlist ? ALLOWLIST : DENYLIST; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 654 | Status res; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 655 | if (!name.compare(FirewallController::LOCAL_DOZABLE)) { |
Rubin Xu | 5ca8e80 | 2019-04-05 11:50:32 +0100 | [diff] [blame] | 656 | res = replaceRulesInMap(DOZABLE_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 657 | } else if (!name.compare(FirewallController::LOCAL_STANDBY)) { |
Rubin Xu | 5ca8e80 | 2019-04-05 11:50:32 +0100 | [diff] [blame] | 658 | res = replaceRulesInMap(STANDBY_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 659 | } else if (!name.compare(FirewallController::LOCAL_POWERSAVE)) { |
Rubin Xu | 5ca8e80 | 2019-04-05 11:50:32 +0100 | [diff] [blame] | 660 | res = replaceRulesInMap(POWERSAVE_MATCH, uids); |
Patrick Rohr | fa0036f | 2020-12-02 16:22:28 +0100 | [diff] [blame] | 661 | } else if (!name.compare(FirewallController::LOCAL_RESTRICTED)) { |
| 662 | res = replaceRulesInMap(RESTRICTED_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 663 | } else { |
| 664 | ALOGE("unknown chain name: %s", name.c_str()); |
| 665 | return -EINVAL; |
| 666 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 667 | if (!isOk(res)) { |
| 668 | ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str()); |
| 669 | return -res.code(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 670 | } |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 675 | std::lock_guard guard(mMutex); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 676 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 677 | auto oldConfiguration = mConfigurationMap.readValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 678 | if (!oldConfiguration.ok()) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 679 | ALOGE("Cannot read the old configuration from map: %s", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 680 | oldConfiguration.error().message().c_str()); |
| 681 | return -oldConfiguration.error().code(); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 682 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 683 | Status res; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 684 | BpfConfig newConfiguration; |
| 685 | uint8_t match; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 686 | switch (chain) { |
| 687 | case DOZABLE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 688 | match = DOZABLE_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 689 | break; |
| 690 | case STANDBY: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 691 | match = STANDBY_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 692 | break; |
| 693 | case POWERSAVE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 694 | match = POWERSAVE_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 695 | break; |
Patrick Rohr | fa0036f | 2020-12-02 16:22:28 +0100 | [diff] [blame] | 696 | case RESTRICTED: |
| 697 | match = RESTRICTED_MATCH; |
| 698 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 699 | default: |
| 700 | return -EINVAL; |
| 701 | } |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 702 | newConfiguration = |
| 703 | enable ? (oldConfiguration.value() | match) : (oldConfiguration.value() & (~match)); |
| 704 | res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 705 | if (!isOk(res)) { |
| 706 | ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str()); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 707 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 708 | return -res.code(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 711 | Status TrafficController::swapActiveStatsMap() { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 712 | std::lock_guard guard(mMutex); |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 713 | |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 714 | uint32_t key = CURRENT_STATS_MAP_CONFIGURATION_KEY; |
| 715 | auto oldConfiguration = mConfigurationMap.readValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 716 | if (!oldConfiguration.ok()) { |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 717 | ALOGE("Cannot read the old configuration from map: %s", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 718 | oldConfiguration.error().message().c_str()); |
| 719 | return Status(oldConfiguration.error().code(), oldConfiguration.error().message()); |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | // Write to the configuration map to inform the kernel eBPF program to switch |
| 723 | // from using one map to the other. Use flag BPF_EXIST here since the map should |
| 724 | // be already populated in initMaps. |
| 725 | uint8_t newConfigure = (oldConfiguration.value() == SELECT_MAP_A) ? SELECT_MAP_B : SELECT_MAP_A; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 726 | auto res = mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY, newConfigure, |
| 727 | BPF_EXIST); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 728 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 729 | ALOGE("Failed to toggle the stats map: %s", strerror(res.error().code())); |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 730 | return res; |
| 731 | } |
| 732 | // After changing the config, we need to make sure all the current running |
| 733 | // eBPF programs are finished and all the CPUs are aware of this config change |
| 734 | // before we modify the old map. So we do a special hack here to wait for |
| 735 | // the kernel to do a synchronize_rcu(). Once the kernel called |
| 736 | // synchronize_rcu(), the config we just updated will be available to all cores |
| 737 | // and the next eBPF programs triggered inside the kernel will use the new |
| 738 | // map configuration. So once this function returns we can safely modify the |
| 739 | // old stats map without concerning about race between the kernel and |
| 740 | // userspace. |
| 741 | int ret = synchronizeKernelRCU(); |
| 742 | if (ret) { |
| 743 | ALOGE("map swap synchronize_rcu() ended with failure: %s", strerror(-ret)); |
| 744 | return statusFromErrno(-ret, "map swap synchronize_rcu() failed"); |
| 745 | } |
| 746 | return netdutils::status::ok; |
| 747 | } |
| 748 | |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 749 | void TrafficController::setPermissionForUids(int permission, const std::vector<uid_t>& uids) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 750 | std::lock_guard guard(mMutex); |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 751 | if (permission == INetd::PERMISSION_UNINSTALLED) { |
| 752 | for (uid_t uid : uids) { |
| 753 | // Clean up all permission information for the related uid if all the |
| 754 | // packages related to it are uninstalled. |
| 755 | mPrivilegedUser.erase(uid); |
Maciej Żenczykowski | ec36c89 | 2021-01-17 03:25:56 -0800 | [diff] [blame] | 756 | Status ret = mUidPermissionMap.deleteValue(uid); |
| 757 | if (!isOk(ret) && ret.code() != ENOENT) { |
| 758 | ALOGE("Failed to clean up the permission for %u: %s", uid, strerror(ret.code())); |
Chenbo Feng | 97c88db | 2019-04-24 12:19:16 -0700 | [diff] [blame] | 759 | } |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 760 | } |
| 761 | return; |
| 762 | } |
| 763 | |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 764 | bool privileged = (permission & INetd::PERMISSION_UPDATE_DEVICE_STATS); |
| 765 | |
| 766 | for (uid_t uid : uids) { |
Chenbo Feng | 97c88db | 2019-04-24 12:19:16 -0700 | [diff] [blame] | 767 | if (privileged) { |
| 768 | mPrivilegedUser.insert(uid); |
| 769 | } else { |
| 770 | mPrivilegedUser.erase(uid); |
| 771 | } |
| 772 | |
Chenbo Feng | 527908a | 2019-04-02 20:14:11 -0700 | [diff] [blame] | 773 | // The map stores all the permissions that the UID has, except if the only permission |
| 774 | // the UID has is the INTERNET permission, then the UID should not appear in the map. |
| 775 | if (permission != INetd::PERMISSION_INTERNET) { |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 776 | Status ret = mUidPermissionMap.writeValue(uid, permission, BPF_ANY); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 777 | if (!isOk(ret)) { |
Chenbo Feng | 527908a | 2019-04-02 20:14:11 -0700 | [diff] [blame] | 778 | ALOGE("Failed to set permission: %s of uid(%u) to permission map: %s", |
| 779 | UidPermissionTypeToString(permission).c_str(), uid, strerror(ret.code())); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 780 | } |
| 781 | } else { |
| 782 | Status ret = mUidPermissionMap.deleteValue(uid); |
| 783 | if (!isOk(ret) && ret.code() != ENOENT) { |
Chenbo Feng | 527908a | 2019-04-02 20:14:11 -0700 | [diff] [blame] | 784 | ALOGE("Failed to remove uid %u from permission map: %s", uid, strerror(ret.code())); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 785 | } |
| 786 | } |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 790 | std::string getProgramStatus(const char *path) { |
| 791 | int ret = access(path, R_OK); |
| 792 | if (ret == 0) { |
| 793 | return StringPrintf("OK"); |
| 794 | } |
| 795 | if (ret != 0 && errno == ENOENT) { |
| 796 | return StringPrintf("program is missing at: %s", path); |
| 797 | } |
| 798 | return StringPrintf("check Program %s error: %s", path, strerror(errno)); |
| 799 | } |
| 800 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 801 | std::string getMapStatus(const base::unique_fd& map_fd, const char* path) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 802 | if (map_fd.get() < 0) { |
| 803 | return StringPrintf("map fd lost"); |
| 804 | } |
| 805 | if (access(path, F_OK) != 0) { |
| 806 | return StringPrintf("map not pinned to location: %s", path); |
| 807 | } |
| 808 | return StringPrintf("OK"); |
| 809 | } |
| 810 | |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 811 | // NOLINTNEXTLINE(google-runtime-references): grandfathered pass by non-const reference |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 812 | void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 813 | dw.blankline(); |
| 814 | dw.println("%s:", mapName.c_str()); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 815 | if (!header.empty()) { |
| 816 | dw.println(header); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
| 820 | const String16 TrafficController::DUMP_KEYWORD = String16("trafficcontroller"); |
| 821 | |
| 822 | void TrafficController::dump(DumpWriter& dw, bool verbose) { |
Chenbo Feng | 84d9f9f | 2019-04-12 15:31:02 -0700 | [diff] [blame] | 823 | std::lock_guard guard(mMutex); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 824 | ScopedIndent indentTop(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 825 | dw.println("TrafficController"); |
| 826 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 827 | ScopedIndent indentPreBpfModule(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 828 | |
| 829 | dw.blankline(); |
| 830 | dw.println("mCookieTagMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 831 | getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 832 | dw.println("mUidCounterSetMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 833 | getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str()); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 834 | dw.println("mAppUidStatsMap status: %s", |
| 835 | getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str()); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 836 | dw.println("mStatsMapA status: %s", |
| 837 | getMapStatus(mStatsMapA.getMap(), STATS_MAP_A_PATH).c_str()); |
| 838 | dw.println("mStatsMapB status: %s", |
| 839 | getMapStatus(mStatsMapB.getMap(), STATS_MAP_B_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 840 | dw.println("mIfaceIndexNameMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 841 | getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 842 | dw.println("mIfaceStatsMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 843 | getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str()); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 844 | dw.println("mConfigurationMap status: %s", |
| 845 | getMapStatus(mConfigurationMap.getMap(), CONFIGURATION_MAP_PATH).c_str()); |
| 846 | dw.println("mUidOwnerMap status: %s", |
| 847 | getMapStatus(mUidOwnerMap.getMap(), UID_OWNER_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 848 | |
| 849 | dw.blankline(); |
| 850 | dw.println("Cgroup ingress program status: %s", |
| 851 | getProgramStatus(BPF_INGRESS_PROG_PATH).c_str()); |
| 852 | dw.println("Cgroup egress program status: %s", getProgramStatus(BPF_EGRESS_PROG_PATH).c_str()); |
| 853 | dw.println("xt_bpf ingress program status: %s", |
| 854 | getProgramStatus(XT_BPF_INGRESS_PROG_PATH).c_str()); |
| 855 | dw.println("xt_bpf egress program status: %s", |
| 856 | getProgramStatus(XT_BPF_EGRESS_PROG_PATH).c_str()); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 857 | dw.println("xt_bpf bandwidth allowlist program status: %s", |
| 858 | getProgramStatus(XT_BPF_ALLOWLIST_PROG_PATH).c_str()); |
| 859 | dw.println("xt_bpf bandwidth denylist program status: %s", |
| 860 | getProgramStatus(XT_BPF_DENYLIST_PROG_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 861 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 862 | if (!verbose) { |
| 863 | return; |
| 864 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 865 | |
| 866 | dw.blankline(); |
| 867 | dw.println("BPF map content:"); |
| 868 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 869 | ScopedIndent indentForMapContent(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 870 | |
| 871 | // Print CookieTagMap content. |
| 872 | dumpBpfMap("mCookieTagMap", dw, ""); |
Maciej Żenczykowski | 11ec78b | 2019-12-30 06:39:32 -0800 | [diff] [blame] | 873 | const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTagValue& value, |
| 874 | const BpfMap<uint64_t, UidTagValue>&) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 875 | dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 876 | return base::Result<void>(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 877 | }; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 878 | base::Result<void> res = mCookieTagMap.iterateWithValue(printCookieTagInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 879 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 880 | dw.println("mCookieTagMap print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | // Print UidCounterSetMap Content |
| 884 | dumpBpfMap("mUidCounterSetMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 885 | const auto printUidInfo = [&dw](const uint32_t& key, const uint8_t& value, |
| 886 | const BpfMap<uint32_t, uint8_t>&) { |
| 887 | dw.println("%u %u", key, value); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 888 | return base::Result<void>(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 889 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 890 | res = mUidCounterSetMap.iterateWithValue(printUidInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 891 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 892 | dw.println("mUidCounterSetMap print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 895 | // Print AppUidStatsMap content |
| 896 | std::string appUidStatsHeader = StringPrintf("uid rxBytes rxPackets txBytes txPackets"); |
| 897 | dumpBpfMap("mAppUidStatsMap:", dw, appUidStatsHeader); |
| 898 | auto printAppUidStatsInfo = [&dw](const uint32_t& key, const StatsValue& value, |
| 899 | const BpfMap<uint32_t, StatsValue>&) { |
| 900 | dw.println("%u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxBytes, |
| 901 | value.rxPackets, value.txBytes, value.txPackets); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 902 | return base::Result<void>(); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 903 | }; |
| 904 | res = mAppUidStatsMap.iterateWithValue(printAppUidStatsInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 905 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 906 | dw.println("mAppUidStatsMap print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 909 | // Print uidStatsMap content |
| 910 | std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes" |
| 911 | " rxPackets txBytes txPackets"); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 912 | dumpBpfMap("mStatsMapA", dw, statsHeader); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 913 | const auto printStatsInfo = [&dw, this](const StatsKey& key, const StatsValue& value, |
| 914 | const BpfMap<StatsKey, StatsValue>&) { |
| 915 | uint32_t ifIndex = key.ifaceIndex; |
| 916 | auto ifname = mIfaceIndexNameMap.readValue(ifIndex); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 917 | if (!ifname.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 918 | ifname = IfaceValue{"unknown"}; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 919 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 920 | dw.println("%u %s 0x%x %u %u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, ifIndex, |
| 921 | ifname.value().name, key.tag, key.uid, key.counterSet, value.rxBytes, |
| 922 | value.rxPackets, value.txBytes, value.txPackets); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 923 | return base::Result<void>(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 924 | }; |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 925 | res = mStatsMapA.iterateWithValue(printStatsInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 926 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 927 | dw.println("mStatsMapA print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | // Print TagStatsMap content. |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 931 | dumpBpfMap("mStatsMapB", dw, statsHeader); |
| 932 | res = mStatsMapB.iterateWithValue(printStatsInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 933 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 934 | dw.println("mStatsMapB print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | // Print ifaceIndexToNameMap content. |
| 938 | dumpBpfMap("mIfaceIndexNameMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 939 | const auto printIfaceNameInfo = [&dw](const uint32_t& key, const IfaceValue& value, |
| 940 | const BpfMap<uint32_t, IfaceValue>&) { |
| 941 | const char* ifname = value.name; |
| 942 | dw.println("ifaceIndex=%u ifaceName=%s", key, ifname); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 943 | return base::Result<void>(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 944 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 945 | res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 946 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 947 | dw.println("mIfaceIndexNameMap print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // Print ifaceStatsMap content |
| 951 | std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes" |
| 952 | " txPackets"); |
| 953 | dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 954 | const auto printIfaceStatsInfo = [&dw, this](const uint32_t& key, const StatsValue& value, |
| 955 | const BpfMap<uint32_t, StatsValue>&) { |
| 956 | auto ifname = mIfaceIndexNameMap.readValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 957 | if (!ifname.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 958 | ifname = IfaceValue{"unknown"}; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 959 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 960 | dw.println("%u %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, ifname.value().name, |
| 961 | value.rxBytes, value.rxPackets, value.txBytes, value.txPackets); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 962 | return base::Result<void>(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 963 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 964 | res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 965 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 966 | dw.println("mIfaceStatsMap print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 969 | dw.blankline(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 970 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 971 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 972 | auto configuration = mConfigurationMap.readValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 973 | if (configuration.ok()) { |
Lorenzo Colitti | c267736 | 2019-06-26 20:01:35 +0900 | [diff] [blame] | 974 | dw.println("current ownerMatch configuration: %d%s", configuration.value(), |
| 975 | uidMatchTypeToString(configuration.value()).c_str()); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 976 | } else { |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 977 | dw.println("mConfigurationMap read ownerMatch configure failed with error: %s", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 978 | configuration.error().message().c_str()); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 979 | } |
Lorenzo Colitti | c267736 | 2019-06-26 20:01:35 +0900 | [diff] [blame] | 980 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 981 | key = CURRENT_STATS_MAP_CONFIGURATION_KEY; |
| 982 | configuration = mConfigurationMap.readValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 983 | if (configuration.ok()) { |
Lorenzo Colitti | c267736 | 2019-06-26 20:01:35 +0900 | [diff] [blame] | 984 | const char* statsMapDescription = "???"; |
| 985 | switch (configuration.value()) { |
| 986 | case SELECT_MAP_A: |
| 987 | statsMapDescription = "SELECT_MAP_A"; |
| 988 | break; |
| 989 | case SELECT_MAP_B: |
| 990 | statsMapDescription = "SELECT_MAP_B"; |
| 991 | break; |
| 992 | // No default clause, so if we ever add a third map, this code will fail to build. |
| 993 | } |
| 994 | dw.println("current statsMap configuration: %d %s", configuration.value(), |
| 995 | statsMapDescription); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 996 | } else { |
| 997 | dw.println("mConfigurationMap read stats map configure failed with error: %s", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 998 | configuration.error().message().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 999 | } |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 1000 | dumpBpfMap("mUidOwnerMap", dw, ""); |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 1001 | const auto printUidMatchInfo = [&dw, this](const uint32_t& key, const UidOwnerValue& value, |
| 1002 | const BpfMap<uint32_t, UidOwnerValue>&) { |
| 1003 | if (value.rule & IIF_MATCH) { |
| 1004 | auto ifname = mIfaceIndexNameMap.readValue(value.iif); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 1005 | if (ifname.ok()) { |
Rubin Xu | ec27ff2 | 2019-01-08 21:33:03 +0000 | [diff] [blame] | 1006 | dw.println("%u %s %s", key, uidMatchTypeToString(value.rule).c_str(), |
| 1007 | ifname.value().name); |
| 1008 | } else { |
| 1009 | dw.println("%u %s %u", key, uidMatchTypeToString(value.rule).c_str(), value.iif); |
| 1010 | } |
| 1011 | } else { |
| 1012 | dw.println("%u %s", key, uidMatchTypeToString(value.rule).c_str()); |
| 1013 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 1014 | return base::Result<void>(); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 1015 | }; |
| 1016 | res = mUidOwnerMap.iterateWithValue(printUidMatchInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 1017 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 1018 | dw.println("mUidOwnerMap print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 1019 | } |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 1020 | dumpBpfMap("mUidPermissionMap", dw, ""); |
Yi Kong | 3d82f38 | 2019-10-01 21:52:14 -0700 | [diff] [blame] | 1021 | const auto printUidPermissionInfo = [&dw](const uint32_t& key, const int& value, |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 1022 | const BpfMap<uint32_t, uint8_t>&) { |
| 1023 | dw.println("%u %s", key, UidPermissionTypeToString(value).c_str()); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 1024 | return base::Result<void>(); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 1025 | }; |
| 1026 | res = mUidPermissionMap.iterateWithValue(printUidPermissionInfo); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 1027 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 1028 | dw.println("mUidPermissionMap print end with error: %s", res.error().message().c_str()); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | dumpBpfMap("mPrivilegedUser", dw, ""); |
| 1032 | for (uid_t uid : mPrivilegedUser) { |
| 1033 | dw.println("%u ALLOW_UPDATE_DEVICE_STATS", (uint32_t)uid); |
| 1034 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 1037 | } // namespace net |
| 1038 | } // namespace android |