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> |
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 | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 49 | #include "DumpWriter.h" |
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" |
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; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 62 | using base::Join; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 63 | using netdutils::extract; |
| 64 | using netdutils::Slice; |
| 65 | using netdutils::sSyscalls; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 66 | using netdutils::Status; |
| 67 | using netdutils::statusFromErrno; |
| 68 | using netdutils::StatusOr; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 69 | using netdutils::status::ok; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 70 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 71 | constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY; |
| 72 | constexpr int kSockDiagDoneMsgType = NLMSG_DONE; |
| 73 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 74 | #define UID_MATCH_MSG_TRANS(result, target, match) \ |
| 75 | do { \ |
| 76 | if (match & target) { \ |
| 77 | result.append(StringPrintf(" %s", #target)); \ |
| 78 | match &= ~target; \ |
| 79 | } \ |
| 80 | } while (0) |
| 81 | |
| 82 | const std::string uidMatchTypeToString(uint8_t match) { |
| 83 | std::string matchType; |
| 84 | UID_MATCH_MSG_TRANS(matchType, HAPPY_BOX_MATCH, match); |
| 85 | UID_MATCH_MSG_TRANS(matchType, PENALTY_BOX_MATCH, match); |
| 86 | UID_MATCH_MSG_TRANS(matchType, DOZABLE_MATCH, match); |
| 87 | UID_MATCH_MSG_TRANS(matchType, STANDBY_MATCH, match); |
| 88 | UID_MATCH_MSG_TRANS(matchType, POWERSAVE_MATCH, match); |
| 89 | if (match) { |
| 90 | return StringPrintf("Unknown match: %u", match); |
| 91 | } |
| 92 | return matchType; |
| 93 | } |
| 94 | |
Chenbo Feng | 4958664 | 2018-08-30 18:01:53 -0700 | [diff] [blame] | 95 | StatusOr<std::unique_ptr<NetlinkListenerInterface>> TrafficController::makeSkDestroyListener() { |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 96 | const auto& sys = sSyscalls.get(); |
| 97 | ASSIGN_OR_RETURN(auto event, sys.eventfd(0, EFD_CLOEXEC)); |
| 98 | const int domain = AF_NETLINK; |
| 99 | const int type = SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK; |
| 100 | const int protocol = NETLINK_INET_DIAG; |
| 101 | ASSIGN_OR_RETURN(auto sock, sys.socket(domain, type, protocol)); |
| 102 | |
Lorenzo Colitti | 436efdb | 2018-07-26 17:20:57 +0900 | [diff] [blame] | 103 | // TODO: if too many sockets are closed too quickly, we can overflow the socket buffer, and |
| 104 | // some entries in mCookieTagMap will not be freed. In order to fix this we would need to |
| 105 | // periodically dump all sockets and remove the tag entries for sockets that have been closed. |
| 106 | // For now, set a large-enough buffer that we can close hundreds of sockets without getting |
| 107 | // ENOBUFS and leaking mCookieTagMap entries. |
| 108 | int rcvbuf = 512 * 1024; |
| 109 | auto ret = sys.setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); |
| 110 | if (!ret.ok()) { |
| 111 | ALOGW("Failed to set SkDestroyListener buffer size to %d: %s", rcvbuf, ret.msg().c_str()); |
| 112 | } |
| 113 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 114 | sockaddr_nl addr = { |
| 115 | .nl_family = AF_NETLINK, |
| 116 | .nl_groups = 1 << (SKNLGRP_INET_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET_UDP_DESTROY - 1) | |
| 117 | 1 << (SKNLGRP_INET6_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET6_UDP_DESTROY - 1)}; |
| 118 | RETURN_IF_NOT_OK(sys.bind(sock, addr)); |
| 119 | |
| 120 | const sockaddr_nl kernel = {.nl_family = AF_NETLINK}; |
| 121 | RETURN_IF_NOT_OK(sys.connect(sock, kernel)); |
| 122 | |
| 123 | std::unique_ptr<NetlinkListenerInterface> listener = |
Chenbo Feng | 4629670 | 2018-08-23 12:20:22 -0700 | [diff] [blame] | 124 | std::make_unique<NetlinkListener>(std::move(event), std::move(sock), "SkDestroyListen"); |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 125 | |
| 126 | return listener; |
| 127 | } |
| 128 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 129 | Status changeOwnerAndMode(const char* path, gid_t group, const char* debugName, bool netdOnly) { |
| 130 | int ret = chown(path, AID_ROOT, group); |
| 131 | if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s group failed", debugName)); |
| 132 | |
| 133 | if (netdOnly) { |
| 134 | ret = chmod(path, S_IRWXU); |
| 135 | } else { |
| 136 | // Allow both netd and system server to obtain map fd from the path. |
| 137 | // chmod doesn't grant permission to all processes in that group to |
| 138 | // read/write the bpf map. They still need correct sepolicy to |
| 139 | // read/write the map. |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 140 | ret = chmod(path, S_IRWXU | S_IRGRP | S_IWGRP); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 141 | } |
| 142 | if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s mode failed", debugName)); |
| 143 | return netdutils::status::ok; |
| 144 | } |
| 145 | |
| 146 | TrafficController::TrafficController() { |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 147 | ebpfSupported = hasBpfSupport(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 150 | Status TrafficController::initMaps() { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 151 | std::lock_guard ownerMapGuard(mOwnerMatchMutex); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 152 | RETURN_IF_NOT_OK( |
| 153 | mCookieTagMap.getOrCreate(COOKIE_UID_MAP_SIZE, COOKIE_TAG_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 154 | |
| 155 | RETURN_IF_NOT_OK(changeOwnerAndMode(COOKIE_TAG_MAP_PATH, AID_NET_BW_ACCT, "CookieTagMap", |
| 156 | false)); |
| 157 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 158 | RETURN_IF_NOT_OK(mUidCounterSetMap.getOrCreate(UID_COUNTERSET_MAP_SIZE, UID_COUNTERSET_MAP_PATH, |
| 159 | BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 160 | RETURN_IF_NOT_OK(changeOwnerAndMode(UID_COUNTERSET_MAP_PATH, AID_NET_BW_ACCT, |
| 161 | "UidCounterSetMap", false)); |
| 162 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 163 | RETURN_IF_NOT_OK(mAppUidStatsMap.getOrCreate(APP_STATS_MAP_SIZE, APP_UID_STATS_MAP_PATH, |
| 164 | BPF_MAP_TYPE_HASH)); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 165 | RETURN_IF_NOT_OK( |
| 166 | changeOwnerAndMode(APP_UID_STATS_MAP_PATH, AID_NET_BW_STATS, "AppUidStatsMap", false)); |
| 167 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 168 | RETURN_IF_NOT_OK(mStatsMapA.getOrCreate(STATS_MAP_SIZE, STATS_MAP_A_PATH, BPF_MAP_TYPE_HASH)); |
| 169 | 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] | 170 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 171 | RETURN_IF_NOT_OK(mStatsMapB.getOrCreate(STATS_MAP_SIZE, STATS_MAP_B_PATH, BPF_MAP_TYPE_HASH)); |
| 172 | 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] | 173 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 174 | RETURN_IF_NOT_OK(mIfaceIndexNameMap.getOrCreate(IFACE_INDEX_NAME_MAP_SIZE, |
| 175 | IFACE_INDEX_NAME_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 176 | RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_INDEX_NAME_MAP_PATH, AID_NET_BW_STATS, |
| 177 | "IfaceIndexNameMap", false)); |
| 178 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 179 | RETURN_IF_NOT_OK( |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 180 | mIfaceStatsMap.getOrCreate(IFACE_STATS_MAP_SIZE, IFACE_STATS_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 181 | RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_STATS_MAP_PATH, AID_NET_BW_STATS, "IfaceStatsMap", |
| 182 | false)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 183 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 184 | RETURN_IF_NOT_OK(mConfigurationMap.getOrCreate(CONFIGURATION_MAP_SIZE, CONFIGURATION_MAP_PATH, |
| 185 | BPF_MAP_TYPE_HASH)); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 186 | RETURN_IF_NOT_OK(changeOwnerAndMode(CONFIGURATION_MAP_PATH, AID_NET_BW_STATS, |
| 187 | "ConfigurationMap", false)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 188 | RETURN_IF_NOT_OK( |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 189 | mConfigurationMap.writeValue(UID_RULES_CONFIGURATION_KEY, DEFAULT_CONFIG, BPF_ANY)); |
| 190 | RETURN_IF_NOT_OK(mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY, SELECT_MAP_A, |
| 191 | BPF_ANY)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 192 | |
| 193 | RETURN_IF_NOT_OK( |
| 194 | mUidOwnerMap.getOrCreate(UID_OWNER_MAP_SIZE, UID_OWNER_MAP_PATH, BPF_MAP_TYPE_HASH)); |
| 195 | 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] | 196 | RETURN_IF_NOT_OK(mUidOwnerMap.clear()); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 197 | return netdutils::status::ok; |
| 198 | } |
| 199 | |
Chenbo Feng | b2d213d | 2018-12-04 11:31:00 -0800 | [diff] [blame] | 200 | static Status attachProgramToCgroup(const char* programPath, const int cgroupFd, |
| 201 | bpf_attach_type type) { |
| 202 | unique_fd cgroupProg(bpfFdGet(programPath, 0)); |
| 203 | if (cgroupProg == -1) { |
| 204 | int ret = errno; |
| 205 | ALOGE("Failed to get program from %s: %s", programPath, strerror(ret)); |
| 206 | return statusFromErrno(ret, "cgroup program get failed"); |
| 207 | } |
| 208 | if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) { |
| 209 | int ret = errno; |
| 210 | ALOGE("Program from %s attach failed: %s", programPath, strerror(ret)); |
| 211 | return statusFromErrno(ret, "program attach failed"); |
| 212 | } |
| 213 | return netdutils::status::ok; |
| 214 | } |
| 215 | |
| 216 | static Status initPrograms() { |
| 217 | unique_fd cg_fd(open(CGROUP_ROOT_PATH, O_DIRECTORY | O_RDONLY | O_CLOEXEC)); |
| 218 | if (cg_fd == -1) { |
| 219 | int ret = errno; |
| 220 | ALOGE("Failed to open the cgroup directory: %s", strerror(ret)); |
| 221 | return statusFromErrno(ret, "Open the cgroup directory failed"); |
| 222 | } |
| 223 | RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_EGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_EGRESS)); |
| 224 | RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_INGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_INGRESS)); |
| 225 | return netdutils::status::ok; |
| 226 | } |
| 227 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 228 | Status TrafficController::start() { |
| 229 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 230 | if (!ebpfSupported) { |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 231 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 234 | /* When netd restarts from a crash without total system reboot, the program |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 235 | * is still attached to the cgroup, detach it so the program can be freed |
| 236 | * and we can load and attach new program into the target cgroup. |
| 237 | * |
| 238 | * TODO: Scrape existing socket when run-time restart and clean up the map |
| 239 | * if the socket no longer exist |
| 240 | */ |
| 241 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 242 | RETURN_IF_NOT_OK(initMaps()); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 243 | |
Chenbo Feng | b2d213d | 2018-12-04 11:31:00 -0800 | [diff] [blame] | 244 | RETURN_IF_NOT_OK(initPrograms()); |
| 245 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 246 | // Fetch the list of currently-existing interfaces. At this point NetlinkHandler is |
| 247 | // already running, so it will call addInterface() when any new interface appears. |
| 248 | std::map<std::string, uint32_t> ifacePairs; |
| 249 | ASSIGN_OR_RETURN(ifacePairs, InterfaceController::getIfaceList()); |
| 250 | for (const auto& ifacePair:ifacePairs) { |
| 251 | addInterface(ifacePair.first.c_str(), ifacePair.second); |
| 252 | } |
| 253 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 254 | auto result = makeSkDestroyListener(); |
| 255 | if (!isOk(result)) { |
| 256 | ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str()); |
| 257 | } else { |
| 258 | mSkDestroyListener = std::move(result.value()); |
| 259 | } |
| 260 | // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function. |
| 261 | const auto rxHandler = [this](const nlmsghdr&, const Slice msg) { |
| 262 | inet_diag_msg diagmsg = {}; |
| 263 | if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 264 | ALOGE("Unrecognized netlink message: %s", toString(msg).c_str()); |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) | |
| 268 | (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32); |
| 269 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 270 | Status s = mCookieTagMap.deleteValue(sock_cookie); |
Chenbo Feng | 1d4c5db | 2018-10-18 15:50:46 -0700 | [diff] [blame] | 271 | if (!isOk(s) && s.code() != ENOENT) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 272 | ALOGE("Failed to delete cookie %" PRIx64 ": %s", sock_cookie, toString(s).c_str()); |
| 273 | return; |
| 274 | } |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 275 | }; |
| 276 | expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler)); |
| 277 | |
| 278 | // In case multiple netlink message comes in as a stream, we need to handle the rxDone message |
| 279 | // properly. |
| 280 | const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) { |
| 281 | // Ignore NLMSG_DONE messages |
| 282 | inet_diag_msg diagmsg = {}; |
| 283 | extract(msg, diagmsg); |
| 284 | }; |
| 285 | expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler)); |
| 286 | |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 287 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 290 | int TrafficController::tagSocket(int sockFd, uint32_t tag, uid_t uid) { |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 291 | if (!ebpfSupported) { |
| 292 | if (legacy_tagSocket(sockFd, tag, uid)) return -errno; |
| 293 | return 0; |
| 294 | } |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 295 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 296 | uint64_t sock_cookie = getSocketCookie(sockFd); |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 297 | if (sock_cookie == NONEXISTENT_COOKIE) return -errno; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 298 | UidTag newKey = {.uid = (uint32_t)uid, .tag = tag}; |
| 299 | |
| 300 | // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY |
| 301 | // flag so it will insert a new entry to the map if that value doesn't exist |
| 302 | // yet. And update the tag if there is already a tag stored. Since the eBPF |
| 303 | // program in kernel only read this map, and is protected by rcu read lock. It |
| 304 | // should be fine to cocurrently update the map while eBPF program is running. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 305 | Status res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY); |
| 306 | if (!isOk(res)) { |
| 307 | ALOGE("Failed to tag the socket: %s, fd: %d", strerror(res.code()), |
| 308 | mCookieTagMap.getMap().get()); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 309 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 310 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | int TrafficController::untagSocket(int sockFd) { |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 314 | if (!ebpfSupported) { |
| 315 | if (legacy_untagSocket(sockFd)) return -errno; |
| 316 | return 0; |
| 317 | } |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 318 | uint64_t sock_cookie = getSocketCookie(sockFd); |
| 319 | |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 320 | if (sock_cookie == NONEXISTENT_COOKIE) return -errno; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 321 | Status res = mCookieTagMap.deleteValue(sock_cookie); |
| 322 | if (!isOk(res)) { |
| 323 | ALOGE("Failed to untag socket: %s\n", strerror(res.code())); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 324 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 325 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | int TrafficController::setCounterSet(int counterSetNum, uid_t uid) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 329 | if (counterSetNum < 0 || counterSetNum >= OVERFLOW_COUNTERSET) return -EINVAL; |
| 330 | Status res; |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 331 | if (!ebpfSupported) { |
| 332 | if (legacy_setCounterSet(counterSetNum, uid)) return -errno; |
| 333 | return 0; |
| 334 | } |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 335 | |
| 336 | // The default counter set for all uid is 0, so deleting the current counterset for that uid |
| 337 | // will automatically set it to 0. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 338 | if (counterSetNum == 0) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 339 | Status res = mUidCounterSetMap.deleteValue(uid); |
| 340 | if (isOk(res) || (!isOk(res) && res.code() == ENOENT)) { |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | } else { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 343 | ALOGE("Failed to delete the counterSet: %s\n", strerror(res.code())); |
| 344 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 347 | uint8_t tmpCounterSetNum = (uint8_t)counterSetNum; |
| 348 | res = mUidCounterSetMap.writeValue(uid, tmpCounterSetNum, BPF_ANY); |
| 349 | if (!isOk(res)) { |
| 350 | ALOGE("Failed to set the counterSet: %s, fd: %d", strerror(res.code()), |
| 351 | mUidCounterSetMap.getMap().get()); |
| 352 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 353 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 354 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 357 | // This method only get called by system_server when an app get uinstalled, it |
| 358 | // is called inside removeUidsLocked() while holding mStatsLock. So it is safe |
| 359 | // to iterate and modify the stats maps. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 360 | int TrafficController::deleteTagData(uint32_t tag, uid_t uid) { |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 361 | if (!ebpfSupported) { |
| 362 | if (legacy_deleteTagData(tag, uid)) return -errno; |
| 363 | return 0; |
| 364 | } |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 365 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 366 | // 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] | 367 | // the tags related to the uid if the tag is 0. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 368 | const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key, const UidTag& value, |
| 369 | BpfMap<uint64_t, UidTag>& map) { |
| 370 | if (value.uid == uid && (value.tag == tag || tag == 0)) { |
| 371 | Status res = map.deleteValue(key); |
| 372 | if (isOk(res) || (res.code() == ENOENT)) { |
| 373 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 374 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 375 | 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] | 376 | } |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 377 | // Move forward to next cookie in the map. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 378 | return netdutils::status::ok; |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 379 | }; |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 380 | mCookieTagMap.iterateWithValue(deleteMatchedCookieEntries).ignoreError(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 381 | // 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] | 382 | // 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] | 383 | const auto deleteMatchedUidTagEntries = [uid, tag](const StatsKey& key, |
| 384 | BpfMap<StatsKey, StatsValue>& map) { |
| 385 | if (key.uid == uid && (key.tag == tag || tag == 0)) { |
| 386 | Status res = map.deleteValue(key); |
| 387 | if (isOk(res) || (res.code() == ENOENT)) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 388 | //Entry is deleted, use the current key to get a new nextKey; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 389 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 390 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 391 | ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag, |
| 392 | strerror(res.code())); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 393 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 394 | return netdutils::status::ok; |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 395 | }; |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 396 | mStatsMapB.iterate(deleteMatchedUidTagEntries).ignoreError(); |
| 397 | mStatsMapA.iterate(deleteMatchedUidTagEntries).ignoreError(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 398 | // 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] | 399 | // need to delete the stats stored in uidStatsMap and counterSet map. |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 400 | if (tag != 0) return 0; |
| 401 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 402 | Status res = mUidCounterSetMap.deleteValue(uid); |
| 403 | if (!isOk(res) && res.code() != ENOENT) { |
| 404 | ALOGE("Failed to delete counterSet data(uid=%u, tag=%u): %s\n", uid, tag, |
| 405 | strerror(res.code())); |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 406 | } |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 407 | |
| 408 | auto deleteAppUidStatsEntry = [uid](const uint32_t& key, BpfMap<uint32_t, StatsValue>& map) { |
| 409 | if (key == uid) { |
| 410 | Status res = map.deleteValue(key); |
| 411 | if (isOk(res) || (res.code() == ENOENT)) { |
| 412 | return netdutils::status::ok; |
| 413 | } |
| 414 | ALOGE("Failed to delete data(uid=%u): %s", key, strerror(res.code())); |
| 415 | } |
| 416 | return netdutils::status::ok; |
| 417 | }; |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 418 | mAppUidStatsMap.iterate(deleteAppUidStatsEntry).ignoreError(); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 419 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 422 | int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 423 | if (!ebpfSupported) return 0; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 424 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 425 | IfaceValue iface; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 426 | if (ifaceIndex == 0) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 427 | ALOGE("Unknown interface %s(%d)", name, ifaceIndex); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 428 | return -1; |
| 429 | } |
| 430 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 431 | strlcpy(iface.name, name, sizeof(IfaceValue)); |
| 432 | Status res = mIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY); |
| 433 | if (!isOk(res)) { |
| 434 | ALOGE("Failed to add iface %s(%d): %s", name, ifaceIndex, strerror(res.code())); |
| 435 | return -res.code(); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 436 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 437 | return 0; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 440 | Status TrafficController::updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule, |
| 441 | FirewallType type) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 442 | std::lock_guard guard(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 443 | if ((rule == ALLOW && type == WHITELIST) || (rule == DENY && type == BLACKLIST)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 444 | RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 445 | } else if ((rule == ALLOW && type == BLACKLIST) || (rule == DENY && type == WHITELIST)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 446 | RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 447 | } else { |
| 448 | //Cannot happen. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 449 | return statusFromErrno(-EINVAL, ""); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 450 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 451 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 454 | UidOwnerMatchType TrafficController::jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling) { |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 455 | switch (jumpHandling) { |
| 456 | case BandwidthController::IptJumpReject: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 457 | return PENALTY_BOX_MATCH; |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 458 | case BandwidthController::IptJumpReturn: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 459 | return HAPPY_BOX_MATCH; |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 460 | case BandwidthController::IptJumpNoAdd: |
| 461 | return NO_MATCH; |
| 462 | } |
| 463 | } |
| 464 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 465 | Status TrafficController::removeMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid, |
| 466 | UidOwnerMatchType match) { |
| 467 | auto oldMatch = map.readValue(uid); |
| 468 | if (isOk(oldMatch)) { |
| 469 | uint8_t newMatch = oldMatch.value() & ~match; |
| 470 | if (newMatch == 0) { |
| 471 | RETURN_IF_NOT_OK(map.deleteValue(uid)); |
| 472 | } else { |
| 473 | RETURN_IF_NOT_OK(map.writeValue(uid, newMatch, BPF_ANY)); |
| 474 | } |
| 475 | } else { |
| 476 | return statusFromErrno(ENOENT, StringPrintf("uid: %u does not exist in map", uid)); |
| 477 | } |
| 478 | return netdutils::status::ok; |
| 479 | } |
| 480 | |
| 481 | Status TrafficController::addMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid, |
| 482 | UidOwnerMatchType match) { |
| 483 | auto oldMatch = map.readValue(uid); |
| 484 | if (isOk(oldMatch)) { |
| 485 | uint8_t newMatch = oldMatch.value() | match; |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 486 | RETURN_IF_NOT_OK(map.writeValue((uint32_t) uid, newMatch, BPF_ANY)); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 487 | } else { |
| 488 | RETURN_IF_NOT_OK(map.writeValue(uid, match, BPF_ANY)); |
| 489 | } |
| 490 | return netdutils::status::ok; |
| 491 | } |
| 492 | |
| 493 | Status TrafficController::updateUidOwnerMap(const std::vector<std::string>& appStrUids, |
| 494 | BandwidthController::IptJumpOp jumpHandling, |
| 495 | BandwidthController::IptOp op) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 496 | std::lock_guard guard(mOwnerMatchMutex); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 497 | UidOwnerMatchType match = jumpOpToMatch(jumpHandling); |
| 498 | if (match == NO_MATCH) { |
| 499 | return statusFromErrno( |
| 500 | EINVAL, StringPrintf("invalid IptJumpOp: %d, command: %d", jumpHandling, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 501 | } |
| 502 | for (const auto& appStrUid : appStrUids) { |
| 503 | char* endPtr; |
| 504 | long uid = strtol(appStrUid.c_str(), &endPtr, 10); |
| 505 | if ((errno == ERANGE && (uid == LONG_MAX || uid == LONG_MIN)) || |
| 506 | (endPtr == appStrUid.c_str()) || (*endPtr != '\0')) { |
| 507 | return statusFromErrno(errno, "invalid uid string:" + appStrUid); |
| 508 | } |
| 509 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 510 | if (op == BandwidthController::IptOpDelete) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 511 | RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 512 | } else if (op == BandwidthController::IptOpInsert) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 513 | RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 514 | } else { |
| 515 | // Cannot happen. |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 516 | return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | return netdutils::status::ok; |
| 520 | } |
| 521 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 522 | int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule, |
| 523 | FirewallType type) { |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 524 | if (!ebpfSupported) { |
| 525 | ALOGE("bpf is not set up, should use iptables rule"); |
| 526 | return -ENOSYS; |
| 527 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 528 | Status res; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 529 | switch (chain) { |
| 530 | case DOZABLE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 531 | res = updateOwnerMapEntry(DOZABLE_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 532 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 533 | case STANDBY: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 534 | res = updateOwnerMapEntry(STANDBY_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 535 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 536 | case POWERSAVE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 537 | res = updateOwnerMapEntry(POWERSAVE_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 538 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 539 | case NONE: |
| 540 | default: |
| 541 | return -EINVAL; |
| 542 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 543 | if (!isOk(res)) { |
| 544 | ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain, |
| 545 | res.msg().c_str(), rule, type); |
| 546 | return -res.code(); |
| 547 | } |
| 548 | return 0; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 551 | Status TrafficController::replaceUidsInMap(const UidOwnerMatchType match, |
| 552 | const std::vector<int32_t>& uids) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 553 | std::lock_guard guard(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 554 | std::set<int32_t> uidSet(uids.begin(), uids.end()); |
| 555 | std::vector<uint32_t> uidsToDelete; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 556 | auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key, |
| 557 | const BpfMap<uint32_t, uint8_t>&) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 558 | if (uidSet.find((int32_t) key) == uidSet.end()) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 559 | uidsToDelete.push_back(key); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 560 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 561 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 562 | }; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 563 | RETURN_IF_NOT_OK(mUidOwnerMap.iterate(getUidsToDelete)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 564 | |
| 565 | for(auto uid : uidsToDelete) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 566 | RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | for (auto uid : uids) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 570 | RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 571 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 572 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | int TrafficController::replaceUidOwnerMap(const std::string& name, bool isWhitelist, |
| 576 | const std::vector<int32_t>& uids) { |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 577 | FirewallRule rule; |
| 578 | FirewallType type; |
| 579 | if (isWhitelist) { |
| 580 | type = WHITELIST; |
| 581 | rule = ALLOW; |
| 582 | } else { |
| 583 | type = BLACKLIST; |
| 584 | rule = DENY; |
| 585 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 586 | Status res; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 587 | if (!name.compare(FirewallController::LOCAL_DOZABLE)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 588 | res = replaceUidsInMap(DOZABLE_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 589 | } else if (!name.compare(FirewallController::LOCAL_STANDBY)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 590 | res = replaceUidsInMap(STANDBY_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 591 | } else if (!name.compare(FirewallController::LOCAL_POWERSAVE)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 592 | res = replaceUidsInMap(POWERSAVE_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 593 | } else { |
| 594 | ALOGE("unknown chain name: %s", name.c_str()); |
| 595 | return -EINVAL; |
| 596 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 597 | if (!isOk(res)) { |
| 598 | ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str()); |
| 599 | return -res.code(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 605 | std::lock_guard guard(mOwnerMatchMutex); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 606 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 607 | auto oldConfiguration = mConfigurationMap.readValue(key); |
| 608 | if (!isOk(oldConfiguration)) { |
| 609 | ALOGE("Cannot read the old configuration from map: %s", |
| 610 | oldConfiguration.status().msg().c_str()); |
| 611 | return -oldConfiguration.status().code(); |
| 612 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 613 | Status res; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 614 | BpfConfig newConfiguration; |
| 615 | uint8_t match; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 616 | switch (chain) { |
| 617 | case DOZABLE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 618 | match = DOZABLE_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 619 | break; |
| 620 | case STANDBY: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 621 | match = STANDBY_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 622 | break; |
| 623 | case POWERSAVE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 624 | match = POWERSAVE_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 625 | break; |
| 626 | default: |
| 627 | return -EINVAL; |
| 628 | } |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 629 | newConfiguration = |
| 630 | enable ? (oldConfiguration.value() | match) : (oldConfiguration.value() & (~match)); |
| 631 | res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 632 | if (!isOk(res)) { |
| 633 | ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str()); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 634 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 635 | return -res.code(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 638 | bool TrafficController::checkBpfStatsEnable() { |
| 639 | return ebpfSupported; |
| 640 | } |
| 641 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 642 | std::string getProgramStatus(const char *path) { |
| 643 | int ret = access(path, R_OK); |
| 644 | if (ret == 0) { |
| 645 | return StringPrintf("OK"); |
| 646 | } |
| 647 | if (ret != 0 && errno == ENOENT) { |
| 648 | return StringPrintf("program is missing at: %s", path); |
| 649 | } |
| 650 | return StringPrintf("check Program %s error: %s", path, strerror(errno)); |
| 651 | } |
| 652 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 653 | std::string getMapStatus(const base::unique_fd& map_fd, const char* path) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 654 | if (map_fd.get() < 0) { |
| 655 | return StringPrintf("map fd lost"); |
| 656 | } |
| 657 | if (access(path, F_OK) != 0) { |
| 658 | return StringPrintf("map not pinned to location: %s", path); |
| 659 | } |
| 660 | return StringPrintf("OK"); |
| 661 | } |
| 662 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 663 | void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 664 | dw.blankline(); |
| 665 | dw.println("%s:", mapName.c_str()); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 666 | if (!header.empty()) { |
| 667 | dw.println(header); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
| 671 | const String16 TrafficController::DUMP_KEYWORD = String16("trafficcontroller"); |
| 672 | |
| 673 | void TrafficController::dump(DumpWriter& dw, bool verbose) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 674 | std::lock_guard ownerMapGuard(mOwnerMatchMutex); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 675 | ScopedIndent indentTop(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 676 | dw.println("TrafficController"); |
| 677 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 678 | ScopedIndent indentPreBpfModule(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 679 | dw.println("BPF module status: %s", ebpfSupported? "ON" : "OFF"); |
| 680 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 681 | if (!ebpfSupported) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 682 | return; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 683 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 684 | |
| 685 | dw.blankline(); |
| 686 | dw.println("mCookieTagMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 687 | getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 688 | dw.println("mUidCounterSetMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 689 | getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str()); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 690 | dw.println("mAppUidStatsMap status: %s", |
| 691 | getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str()); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 692 | dw.println("mStatsMapA status: %s", |
| 693 | getMapStatus(mStatsMapA.getMap(), STATS_MAP_A_PATH).c_str()); |
| 694 | dw.println("mStatsMapB status: %s", |
| 695 | getMapStatus(mStatsMapB.getMap(), STATS_MAP_B_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 696 | dw.println("mIfaceIndexNameMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 697 | getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 698 | dw.println("mIfaceStatsMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 699 | getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str()); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 700 | dw.println("mConfigurationMap status: %s", |
| 701 | getMapStatus(mConfigurationMap.getMap(), CONFIGURATION_MAP_PATH).c_str()); |
| 702 | dw.println("mUidOwnerMap status: %s", |
| 703 | getMapStatus(mUidOwnerMap.getMap(), UID_OWNER_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 704 | |
| 705 | dw.blankline(); |
| 706 | dw.println("Cgroup ingress program status: %s", |
| 707 | getProgramStatus(BPF_INGRESS_PROG_PATH).c_str()); |
| 708 | dw.println("Cgroup egress program status: %s", getProgramStatus(BPF_EGRESS_PROG_PATH).c_str()); |
| 709 | dw.println("xt_bpf ingress program status: %s", |
| 710 | getProgramStatus(XT_BPF_INGRESS_PROG_PATH).c_str()); |
| 711 | dw.println("xt_bpf egress program status: %s", |
| 712 | getProgramStatus(XT_BPF_EGRESS_PROG_PATH).c_str()); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 713 | dw.println("xt_bpf bandwidth whitelist program status: %s", |
| 714 | getProgramStatus(XT_BPF_WHITELIST_PROG_PATH).c_str()); |
| 715 | dw.println("xt_bpf bandwidth blacklist program status: %s", |
| 716 | getProgramStatus(XT_BPF_BLACKLIST_PROG_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 717 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 718 | if (!verbose) { |
| 719 | return; |
| 720 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 721 | |
| 722 | dw.blankline(); |
| 723 | dw.println("BPF map content:"); |
| 724 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 725 | ScopedIndent indentForMapContent(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 726 | |
| 727 | // Print CookieTagMap content. |
| 728 | dumpBpfMap("mCookieTagMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 729 | const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTag& value, |
| 730 | const BpfMap<uint64_t, UidTag>&) { |
| 731 | dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid); |
| 732 | return netdutils::status::ok; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 733 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 734 | Status res = mCookieTagMap.iterateWithValue(printCookieTagInfo); |
| 735 | if (!isOk(res)) { |
| 736 | dw.println("mCookieTagMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // Print UidCounterSetMap Content |
| 740 | dumpBpfMap("mUidCounterSetMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 741 | const auto printUidInfo = [&dw](const uint32_t& key, const uint8_t& value, |
| 742 | const BpfMap<uint32_t, uint8_t>&) { |
| 743 | dw.println("%u %u", key, value); |
| 744 | return netdutils::status::ok; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 745 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 746 | res = mUidCounterSetMap.iterateWithValue(printUidInfo); |
| 747 | if (!isOk(res)) { |
| 748 | dw.println("mUidCounterSetMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 751 | // Print AppUidStatsMap content |
| 752 | std::string appUidStatsHeader = StringPrintf("uid rxBytes rxPackets txBytes txPackets"); |
| 753 | dumpBpfMap("mAppUidStatsMap:", dw, appUidStatsHeader); |
| 754 | auto printAppUidStatsInfo = [&dw](const uint32_t& key, const StatsValue& value, |
| 755 | const BpfMap<uint32_t, StatsValue>&) { |
| 756 | dw.println("%u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxBytes, |
| 757 | value.rxPackets, value.txBytes, value.txPackets); |
| 758 | return netdutils::status::ok; |
| 759 | }; |
| 760 | res = mAppUidStatsMap.iterateWithValue(printAppUidStatsInfo); |
| 761 | if (!res.ok()) { |
| 762 | dw.println("mAppUidStatsMap print end with error: %s", res.msg().c_str()); |
| 763 | } |
| 764 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 765 | // Print uidStatsMap content |
| 766 | std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes" |
| 767 | " rxPackets txBytes txPackets"); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 768 | dumpBpfMap("mStatsMapA", dw, statsHeader); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 769 | const auto printStatsInfo = [&dw, this](const StatsKey& key, const StatsValue& value, |
| 770 | const BpfMap<StatsKey, StatsValue>&) { |
| 771 | uint32_t ifIndex = key.ifaceIndex; |
| 772 | auto ifname = mIfaceIndexNameMap.readValue(ifIndex); |
| 773 | if (!isOk(ifname)) { |
| 774 | strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue)); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 775 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 776 | dw.println("%u %s 0x%x %u %u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, ifIndex, |
| 777 | ifname.value().name, key.tag, key.uid, key.counterSet, value.rxBytes, |
| 778 | value.rxPackets, value.txBytes, value.txPackets); |
| 779 | return netdutils::status::ok; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 780 | }; |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 781 | res = mStatsMapA.iterateWithValue(printStatsInfo); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 782 | if (!isOk(res)) { |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 783 | dw.println("mStatsMapA print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | // Print TagStatsMap content. |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 787 | dumpBpfMap("mStatsMapB", dw, statsHeader); |
| 788 | res = mStatsMapB.iterateWithValue(printStatsInfo); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 789 | if (!isOk(res)) { |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 790 | dw.println("mStatsMapB print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // Print ifaceIndexToNameMap content. |
| 794 | dumpBpfMap("mIfaceIndexNameMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 795 | const auto printIfaceNameInfo = [&dw](const uint32_t& key, const IfaceValue& value, |
| 796 | const BpfMap<uint32_t, IfaceValue>&) { |
| 797 | const char* ifname = value.name; |
| 798 | dw.println("ifaceIndex=%u ifaceName=%s", key, ifname); |
| 799 | return netdutils::status::ok; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 800 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 801 | res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo); |
| 802 | if (!isOk(res)) { |
| 803 | dw.println("mIfaceIndexNameMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // Print ifaceStatsMap content |
| 807 | std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes" |
| 808 | " txPackets"); |
| 809 | dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 810 | const auto printIfaceStatsInfo = [&dw, this](const uint32_t& key, const StatsValue& value, |
| 811 | const BpfMap<uint32_t, StatsValue>&) { |
| 812 | auto ifname = mIfaceIndexNameMap.readValue(key); |
| 813 | if (!isOk(ifname)) { |
| 814 | strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue)); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 815 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 816 | dw.println("%u %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, ifname.value().name, |
| 817 | value.rxBytes, value.rxPackets, value.txBytes, value.txPackets); |
| 818 | return netdutils::status::ok; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 819 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 820 | res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo); |
| 821 | if (!isOk(res)) { |
| 822 | dw.println("mIfaceStatsMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 825 | dw.blankline(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 826 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 827 | uint32_t key = UID_RULES_CONFIGURATION_KEY; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 828 | auto configuration = mConfigurationMap.readValue(key); |
| 829 | if (isOk(configuration)) { |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 830 | dw.println("current ownerMatch configuration: %d", configuration.value()); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 831 | } else { |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame^] | 832 | dw.println("mConfigurationMap read ownerMatch configure failed with error: %s", |
| 833 | configuration.status().msg().c_str()); |
| 834 | } |
| 835 | key = CURRENT_STATS_MAP_CONFIGURATION_KEY; |
| 836 | configuration = mConfigurationMap.readValue(key); |
| 837 | if (isOk(configuration)) { |
| 838 | dw.println("current statsMap configuration: %d", configuration.value()); |
| 839 | } else { |
| 840 | dw.println("mConfigurationMap read stats map configure failed with error: %s", |
| 841 | configuration.status().msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 842 | } |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 843 | dumpBpfMap("mUidOwnerMap", dw, ""); |
| 844 | const auto printUidMatchInfo = [&dw](const uint32_t& key, const uint8_t& value, |
| 845 | const BpfMap<uint32_t, uint8_t>&) { |
| 846 | dw.println("%u %s", key, uidMatchTypeToString(value).c_str()); |
| 847 | return netdutils::status::ok; |
| 848 | }; |
| 849 | res = mUidOwnerMap.iterateWithValue(printUidMatchInfo); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 850 | if (!isOk(res)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 851 | dw.println("mUidOwnerMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 852 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 855 | } // namespace net |
| 856 | } // namespace android |