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