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