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