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