blob: 04eee315c05676984e29723b93713c08443f9fc9 [file] [log] [blame]
Chenbo Fengf2759682017-10-10 17:31:57 -07001/*
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 Fengc10a8a42017-12-15 13:56:33 -080017#define LOG_TAG "TrafficController"
Chenbo Feng33cc1032017-10-23 15:16:37 -070018#include <inttypes.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070019#include <linux/bpf.h>
20#include <linux/if_ether.h>
21#include <linux/in.h>
22#include <linux/inet_diag.h>
Chenbo Feng116d0552017-12-04 17:25:19 -080023#include <linux/netlink.h>
24#include <linux/sock_diag.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070025#include <linux/unistd.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080026#include <net/if.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070027#include <stdlib.h>
28#include <string.h>
29#include <sys/socket.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080030#include <sys/stat.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080031#include <sys/types.h>
Chenbo Feng33cc1032017-10-23 15:16:37 -070032#include <sys/utsname.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080033#include <sys/wait.h>
Chenbo Feng89c12f12018-03-21 10:29:18 -070034#include <mutex>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080035#include <unordered_set>
36#include <vector>
37
38#include <android-base/stringprintf.h>
Chenbo Fengef297172018-03-26 10:53:33 -070039#include <android-base/strings.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080040#include <android-base/unique_fd.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080041#include <logwrap/logwrap.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080042#include <netdutils/StatusOr.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070043
Chenbo Feng116d0552017-12-04 17:25:19 -080044#include <netdutils/Misc.h>
45#include <netdutils/Syscalls.h>
Suren Baghdasaryane072a3c2019-01-16 14:36:07 -080046#include <processgroup/processgroup.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070047#include "TrafficController.h"
Chenbo Feng4f6c2372018-04-26 10:37:55 -070048#include "bpf/BpfMap.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080049
Chenbo Feng89c12f12018-03-21 10:29:18 -070050#include "FirewallController.h"
Chenbo Feng7e974052018-02-28 22:57:21 -080051#include "InterfaceController.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080052#include "NetlinkListener.h"
Luke Huangb257d612019-03-14 21:19:13 +080053#include "netdutils/DumpWriter.h"
Chenbo Feng33cc1032017-10-23 15:16:37 -070054#include "qtaguid/qtaguid.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070055
Bernie Innocenti7e25ec02018-07-02 19:32:17 +090056using namespace android::bpf; // NOLINT(google-build-using-namespace): grandfathered
Chenbo Fengf2759682017-10-10 17:31:57 -070057
58namespace android {
59namespace net {
60
Chenbo Fengc10a8a42017-12-15 13:56:33 -080061using base::StringPrintf;
62using base::unique_fd;
Luke Huangb257d612019-03-14 21:19:13 +080063using netdutils::DumpWriter;
Chenbo Feng116d0552017-12-04 17:25:19 -080064using netdutils::extract;
Luke Huangb257d612019-03-14 21:19:13 +080065using netdutils::ScopedIndent;
Chenbo Feng116d0552017-12-04 17:25:19 -080066using netdutils::Slice;
67using netdutils::sSyscalls;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080068using netdutils::Status;
69using netdutils::statusFromErrno;
70using netdutils::StatusOr;
Chenbo Feng116d0552017-12-04 17:25:19 -080071using netdutils::status::ok;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080072
Chenbo Feng116d0552017-12-04 17:25:19 -080073constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY;
74constexpr int kSockDiagDoneMsgType = NLMSG_DONE;
75
Chenbo Fengbf660aa2019-02-26 16:12:27 -080076static_assert(BPF_PERMISSION_INTERNET == INetd::PERMISSION_INTERNET,
77 "Mismatch between BPF and AIDL permissions: PERMISSION_INTERNET");
78static_assert(BPF_PERMISSION_UPDATE_DEVICE_STATS == INetd::PERMISSION_UPDATE_DEVICE_STATS,
79 "Mismatch between BPF and AIDL permissions: PERMISSION_UPDATE_DEVICE_STATS");
80
Chenbo Feng48eaed32018-12-26 17:40:21 -080081#define FLAG_MSG_TRANS(result, flag, value) \
82 do { \
83 if (value & flag) { \
84 result.append(StringPrintf(" %s", #flag)); \
85 value &= ~flag; \
86 } \
Chenbo Feng703798e2018-06-15 17:07:59 -070087 } while (0)
88
89const std::string uidMatchTypeToString(uint8_t match) {
90 std::string matchType;
Chenbo Feng48eaed32018-12-26 17:40:21 -080091 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 Xuec27ff22019-01-08 21:33:03 +000096 FLAG_MSG_TRANS(matchType, IIF_MATCH, match);
Chenbo Feng703798e2018-06-15 17:07:59 -070097 if (match) {
98 return StringPrintf("Unknown match: %u", match);
99 }
100 return matchType;
101}
102
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800103bool TrafficController::hasUpdateDeviceStatsPermission(uid_t uid) {
Chenbo Feng3969c262019-03-19 14:53:57 -0700104 // 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 Fengb4a4fa12019-01-09 17:20:45 -0800109}
110
Chenbo Feng48eaed32018-12-26 17:40:21 -0800111const std::string UidPermissionTypeToString(uint8_t permission) {
Chenbo Fengbf660aa2019-02-26 16:12:27 -0800112 if (permission == INetd::NO_PERMISSIONS) {
113 return "NO_PERMISSIONS";
114 }
Chenbo Feng527908a2019-04-02 20:14:11 -0700115 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 Feng48eaed32018-12-26 17:40:21 -0800119 std::string permissionType;
Chenbo Fengbf660aa2019-02-26 16:12:27 -0800120 FLAG_MSG_TRANS(permissionType, BPF_PERMISSION_INTERNET, permission);
121 FLAG_MSG_TRANS(permissionType, BPF_PERMISSION_UPDATE_DEVICE_STATS, permission);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800122 if (permission) {
123 return StringPrintf("Unknown permission: %u", permission);
124 }
125 return permissionType;
126}
127
Chenbo Feng49586642018-08-30 18:01:53 -0700128StatusOr<std::unique_ptr<NetlinkListenerInterface>> TrafficController::makeSkDestroyListener() {
Chenbo Feng116d0552017-12-04 17:25:19 -0800129 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 Colitti436efdb2018-07-26 17:20:57 +0900136 // 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 Feng116d0552017-12-04 17:25:19 -0800147 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 Feng46296702018-08-23 12:20:22 -0700157 std::make_unique<NetlinkListener>(std::move(event), std::move(sock), "SkDestroyListen");
Chenbo Feng116d0552017-12-04 17:25:19 -0800158
159 return listener;
160}
161
Chenbo Feng89c12f12018-03-21 10:29:18 -0700162Status 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 Fengf434e862018-06-27 14:08:39 -0700173 ret = chmod(path, S_IRWXU | S_IRGRP | S_IWGRP);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700174 }
175 if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s mode failed", debugName));
176 return netdutils::status::ok;
177}
178
Chenbo Feng47dd0732018-12-11 12:23:24 -0800179TrafficController::TrafficController() : mBpfLevel(getBpfSupportLevel()) {}
Chenbo Feng89c12f12018-03-21 10:29:18 -0700180
Chenbo Feng89c12f12018-03-21 10:29:18 -0700181Status TrafficController::initMaps() {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900182 std::lock_guard ownerMapGuard(mOwnerMatchMutex);
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700183 RETURN_IF_NOT_OK(mCookieTagMap.init(COOKIE_TAG_MAP_PATH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700184 RETURN_IF_NOT_OK(changeOwnerAndMode(COOKIE_TAG_MAP_PATH, AID_NET_BW_ACCT, "CookieTagMap",
185 false));
186
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700187 RETURN_IF_NOT_OK(mUidCounterSetMap.init(UID_COUNTERSET_MAP_PATH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700188 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_COUNTERSET_MAP_PATH, AID_NET_BW_ACCT,
189 "UidCounterSetMap", false));
190
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700191 RETURN_IF_NOT_OK(mAppUidStatsMap.init(APP_UID_STATS_MAP_PATH));
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700192 RETURN_IF_NOT_OK(
193 changeOwnerAndMode(APP_UID_STATS_MAP_PATH, AID_NET_BW_STATS, "AppUidStatsMap", false));
194
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700195 RETURN_IF_NOT_OK(mStatsMapA.init(STATS_MAP_A_PATH));
Chenbo Fengf434e862018-06-27 14:08:39 -0700196 RETURN_IF_NOT_OK(changeOwnerAndMode(STATS_MAP_A_PATH, AID_NET_BW_STATS, "StatsMapA", false));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700197
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700198 RETURN_IF_NOT_OK(mStatsMapB.init(STATS_MAP_B_PATH));
Chenbo Fengf434e862018-06-27 14:08:39 -0700199 RETURN_IF_NOT_OK(changeOwnerAndMode(STATS_MAP_B_PATH, AID_NET_BW_STATS, "StatsMapB", false));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700200
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700201 RETURN_IF_NOT_OK(mIfaceIndexNameMap.init(IFACE_INDEX_NAME_MAP_PATH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700202 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_INDEX_NAME_MAP_PATH, AID_NET_BW_STATS,
203 "IfaceIndexNameMap", false));
204
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700205 RETURN_IF_NOT_OK(mIfaceStatsMap.init(IFACE_STATS_MAP_PATH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700206 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_STATS_MAP_PATH, AID_NET_BW_STATS, "IfaceStatsMap",
207 false));
Chenbo Feng95892f32018-06-07 14:52:02 -0700208
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700209 RETURN_IF_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH));
Chenbo Fengf434e862018-06-27 14:08:39 -0700210 RETURN_IF_NOT_OK(changeOwnerAndMode(CONFIGURATION_MAP_PATH, AID_NET_BW_STATS,
211 "ConfigurationMap", false));
Chenbo Feng703798e2018-06-15 17:07:59 -0700212 RETURN_IF_NOT_OK(
Chenbo Fengf434e862018-06-27 14:08:39 -0700213 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 Feng703798e2018-06-15 17:07:59 -0700216
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700217 RETURN_IF_NOT_OK(mUidOwnerMap.init(UID_OWNER_MAP_PATH));
Chenbo Feng703798e2018-06-15 17:07:59 -0700218 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_OWNER_MAP_PATH, AID_ROOT, "UidOwnerMap", true));
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900219 RETURN_IF_NOT_OK(mUidOwnerMap.clear());
Maciej Żenczykowski02554372019-04-01 10:44:54 -0700220 RETURN_IF_NOT_OK(mUidPermissionMap.init(UID_PERMISSION_MAP_PATH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700221 return netdutils::status::ok;
222}
223
Chenbo Fengb2d213d2018-12-04 11:31:00 -0800224static 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
240static Status initPrograms() {
Suren Baghdasaryane072a3c2019-01-16 14:36:07 -0800241 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 Fengb2d213d2018-12-04 11:31:00 -0800250 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 Fenga51f4fa2019-01-15 15:03:32 -0800257
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 Fengb2d213d2018-12-04 11:31:00 -0800267 return netdutils::status::ok;
268}
269
Chenbo Feng89c12f12018-03-21 10:29:18 -0700270Status TrafficController::start() {
Chenbo Feng47dd0732018-12-11 12:23:24 -0800271 if (mBpfLevel == BpfLevel::NONE) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800272 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700273 }
274
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900275 /* When netd restarts from a crash without total system reboot, the program
Chenbo Fengf2759682017-10-10 17:31:57 -0700276 * 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 Feng89c12f12018-03-21 10:29:18 -0700283 RETURN_IF_NOT_OK(initMaps());
Chenbo Feng7e974052018-02-28 22:57:21 -0800284
Chenbo Fengb2d213d2018-12-04 11:31:00 -0800285 RETURN_IF_NOT_OK(initPrograms());
286
Chenbo Feng7e974052018-02-28 22:57:21 -0800287 // 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 Feng116d0552017-12-04 17:25:19 -0800295 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 Innocenti6f9fd902018-10-11 20:50:23 +0900305 ALOGE("Unrecognized netlink message: %s", toString(msg).c_str());
Chenbo Feng116d0552017-12-04 17:25:19 -0800306 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 Innocenti6f9fd902018-10-11 20:50:23 +0900311 Status s = mCookieTagMap.deleteValue(sock_cookie);
Chenbo Feng1d4c5db2018-10-18 15:50:46 -0700312 if (!isOk(s) && s.code() != ENOENT) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900313 ALOGE("Failed to delete cookie %" PRIx64 ": %s", sock_cookie, toString(s).c_str());
314 return;
315 }
Chenbo Feng116d0552017-12-04 17:25:19 -0800316 };
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 Feng05393d82018-01-09 15:18:43 -0800328 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700329}
330
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800331int 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 Feng47dd0732018-12-11 12:23:24 -0800336 if (mBpfLevel == BpfLevel::NONE) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700337 if (legacy_tagSocket(sockFd, tag, uid)) return -errno;
338 return 0;
339 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700340
Chenbo Fengf2759682017-10-10 17:31:57 -0700341 uint64_t sock_cookie = getSocketCookie(sockFd);
Chenbo Fengef1cab32018-04-13 19:50:49 -0700342 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Fengf2759682017-10-10 17:31:57 -0700343 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 Feng4f6c2372018-04-26 10:37:55 -0700350 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 Fengf2759682017-10-10 17:31:57 -0700354 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700355 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700356}
357
358int TrafficController::untagSocket(int sockFd) {
Chenbo Feng47dd0732018-12-11 12:23:24 -0800359 if (mBpfLevel == BpfLevel::NONE) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700360 if (legacy_untagSocket(sockFd)) return -errno;
361 return 0;
362 }
Chenbo Fengf2759682017-10-10 17:31:57 -0700363 uint64_t sock_cookie = getSocketCookie(sockFd);
364
Chenbo Fengef1cab32018-04-13 19:50:49 -0700365 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700366 Status res = mCookieTagMap.deleteValue(sock_cookie);
367 if (!isOk(res)) {
368 ALOGE("Failed to untag socket: %s\n", strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700369 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700370 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700371}
372
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800373int TrafficController::setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700374 if (counterSetNum < 0 || counterSetNum >= OVERFLOW_COUNTERSET) return -EINVAL;
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800375
376 if (!hasUpdateDeviceStatsPermission(callingUid)) return -EPERM;
377
Chenbo Feng47dd0732018-12-11 12:23:24 -0800378 if (mBpfLevel == BpfLevel::NONE) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700379 if (legacy_setCounterSet(counterSetNum, uid)) return -errno;
380 return 0;
381 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800382
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 Fengf2759682017-10-10 17:31:57 -0700385 if (counterSetNum == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700386 Status res = mUidCounterSetMap.deleteValue(uid);
387 if (isOk(res) || (!isOk(res) && res.code() == ENOENT)) {
Chenbo Fengf2759682017-10-10 17:31:57 -0700388 return 0;
389 } else {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700390 ALOGE("Failed to delete the counterSet: %s\n", strerror(res.code()));
391 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700392 }
393 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700394 uint8_t tmpCounterSetNum = (uint8_t)counterSetNum;
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800395 Status res = mUidCounterSetMap.writeValue(uid, tmpCounterSetNum, BPF_ANY);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700396 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 Fengf2759682017-10-10 17:31:57 -0700400 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700401 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700402}
403
Chenbo Fengf434e862018-06-27 14:08:39 -0700404// 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 Fengb4a4fa12019-01-09 17:20:45 -0800407int TrafficController::deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid) {
408 if (!hasUpdateDeviceStatsPermission(callingUid)) return -EPERM;
409
Chenbo Feng47dd0732018-12-11 12:23:24 -0800410 if (mBpfLevel == BpfLevel::NONE) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700411 if (legacy_deleteTagData(tag, uid)) return -errno;
412 return 0;
413 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700414
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800415 // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all
Chenbo Fengef1cab32018-04-13 19:50:49 -0700416 // the tags related to the uid if the tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700417 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 Fengf2759682017-10-10 17:31:57 -0700423 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700424 ALOGE("Failed to delete data(cookie = %" PRIu64 "): %s\n", key, strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700425 }
Chenbo Fengef1cab32018-04-13 19:50:49 -0700426 // Move forward to next cookie in the map.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700427 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700428 };
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900429 mCookieTagMap.iterateWithValue(deleteMatchedCookieEntries).ignoreError();
Chenbo Fengf2759682017-10-10 17:31:57 -0700430 // Now we go through the Tag stats map and delete the data entry with correct uid and tag
Chenbo Fengef1cab32018-04-13 19:50:49 -0700431 // combination. Or all tag stats under that uid if the target tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700432 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 Fengef1cab32018-04-13 19:50:49 -0700437 //Entry is deleted, use the current key to get a new nextKey;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700438 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700439 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700440 ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag,
441 strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700442 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700443 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700444 };
Chenbo Fengf434e862018-06-27 14:08:39 -0700445 mStatsMapB.iterate(deleteMatchedUidTagEntries).ignoreError();
446 mStatsMapA.iterate(deleteMatchedUidTagEntries).ignoreError();
Chenbo Fengf2759682017-10-10 17:31:57 -0700447 // If the tag is not zero, we already deleted all the data entry required. If tag is 0, we also
Chenbo Fenged37fea2017-12-13 19:35:01 -0800448 // need to delete the stats stored in uidStatsMap and counterSet map.
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800449 if (tag != 0) return 0;
450
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700451 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 Fengc10a8a42017-12-15 13:56:33 -0800455 }
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700456
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 Innocenti6f9fd902018-10-11 20:50:23 +0900467 mAppUidStatsMap.iterate(deleteAppUidStatsEntry).ignoreError();
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700468 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700469}
470
Chenbo Feng7e974052018-02-28 22:57:21 -0800471int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) {
Chenbo Feng47dd0732018-12-11 12:23:24 -0800472 if (mBpfLevel == BpfLevel::NONE) return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800473
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700474 IfaceValue iface;
Chenbo Feng7e974052018-02-28 22:57:21 -0800475 if (ifaceIndex == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700476 ALOGE("Unknown interface %s(%d)", name, ifaceIndex);
Chenbo Feng7e974052018-02-28 22:57:21 -0800477 return -1;
478 }
479
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700480 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 Feng7e974052018-02-28 22:57:21 -0800485 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700486 return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800487}
488
Chenbo Feng703798e2018-06-15 17:07:59 -0700489Status TrafficController::updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
490 FirewallType type) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900491 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700492 if ((rule == ALLOW && type == WHITELIST) || (rule == DENY && type == BLACKLIST)) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100493 RETURN_IF_NOT_OK(addRule(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700494 } else if ((rule == ALLOW && type == BLACKLIST) || (rule == DENY && type == WHITELIST)) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100495 RETURN_IF_NOT_OK(removeRule(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700496 } else {
497 //Cannot happen.
Rubin Xuec27ff22019-01-08 21:33:03 +0000498 return statusFromErrno(EINVAL, "");
Chenbo Feng89c12f12018-03-21 10:29:18 -0700499 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700500 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700501}
502
Chenbo Feng703798e2018-06-15 17:07:59 -0700503UidOwnerMatchType TrafficController::jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling) {
Chenbo Feng95892f32018-06-07 14:52:02 -0700504 switch (jumpHandling) {
505 case BandwidthController::IptJumpReject:
Chenbo Feng703798e2018-06-15 17:07:59 -0700506 return PENALTY_BOX_MATCH;
Chenbo Feng95892f32018-06-07 14:52:02 -0700507 case BandwidthController::IptJumpReturn:
Chenbo Feng703798e2018-06-15 17:07:59 -0700508 return HAPPY_BOX_MATCH;
Chenbo Feng95892f32018-06-07 14:52:02 -0700509 case BandwidthController::IptJumpNoAdd:
510 return NO_MATCH;
511 }
512}
513
Rubin Xu5ca8e802019-04-05 11:50:32 +0100514Status TrafficController::removeRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
515 UidOwnerMatchType match) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700516 auto oldMatch = map.readValue(uid);
517 if (isOk(oldMatch)) {
Rubin Xuec27ff22019-01-08 21:33:03 +0000518 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 Feng703798e2018-06-15 17:07:59 -0700521 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 Xu5ca8e802019-04-05 11:50:32 +0100531Status TrafficController::addRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
532 UidOwnerMatchType match, uint32_t iif) {
Rubin Xuec27ff22019-01-08 21:33:03 +0000533 // 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 Feng703798e2018-06-15 17:07:59 -0700539 auto oldMatch = map.readValue(uid);
540 if (isOk(oldMatch)) {
Rubin Xuec27ff22019-01-08 21:33:03 +0000541 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 Feng703798e2018-06-15 17:07:59 -0700544 } else {
Rubin Xuec27ff22019-01-08 21:33:03 +0000545 UidOwnerValue newMatch = {.rule = static_cast<uint8_t>(match), .iif = iif};
546 RETURN_IF_NOT_OK(map.writeValue(uid, newMatch, BPF_ANY));
Chenbo Feng703798e2018-06-15 17:07:59 -0700547 }
548 return netdutils::status::ok;
549}
550
551Status TrafficController::updateUidOwnerMap(const std::vector<std::string>& appStrUids,
552 BandwidthController::IptJumpOp jumpHandling,
553 BandwidthController::IptOp op) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900554 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700555 UidOwnerMatchType match = jumpOpToMatch(jumpHandling);
556 if (match == NO_MATCH) {
557 return statusFromErrno(
558 EINVAL, StringPrintf("invalid IptJumpOp: %d, command: %d", jumpHandling, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700559 }
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 Feng95892f32018-06-07 14:52:02 -0700568 if (op == BandwidthController::IptOpDelete) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100569 RETURN_IF_NOT_OK(removeRule(mUidOwnerMap, uid, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700570 } else if (op == BandwidthController::IptOpInsert) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100571 RETURN_IF_NOT_OK(addRule(mUidOwnerMap, uid, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700572 } else {
573 // Cannot happen.
Chenbo Feng703798e2018-06-15 17:07:59 -0700574 return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700575 }
576 }
577 return netdutils::status::ok;
578}
579
Chenbo Feng89c12f12018-03-21 10:29:18 -0700580int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule,
581 FirewallType type) {
Chenbo Feng47dd0732018-12-11 12:23:24 -0800582 if (mBpfLevel == BpfLevel::NONE) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700583 ALOGE("bpf is not set up, should use iptables rule");
584 return -ENOSYS;
585 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700586 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700587 switch (chain) {
588 case DOZABLE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700589 res = updateOwnerMapEntry(DOZABLE_MATCH, uid, rule, type);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700590 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700591 case STANDBY:
Chenbo Feng703798e2018-06-15 17:07:59 -0700592 res = updateOwnerMapEntry(STANDBY_MATCH, uid, rule, type);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700593 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700594 case POWERSAVE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700595 res = updateOwnerMapEntry(POWERSAVE_MATCH, uid, rule, type);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700596 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700597 case NONE:
598 default:
599 return -EINVAL;
600 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700601 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 Feng89c12f12018-03-21 10:29:18 -0700607}
608
Rubin Xu5ca8e802019-04-05 11:50:32 +0100609Status TrafficController::replaceRulesInMap(const UidOwnerMatchType match,
610 const std::vector<int32_t>& uids) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900611 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700612 std::set<int32_t> uidSet(uids.begin(), uids.end());
613 std::vector<uint32_t> uidsToDelete;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700614 auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key,
Rubin Xuec27ff22019-01-08 21:33:03 +0000615 const BpfMap<uint32_t, UidOwnerValue>&) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700616 if (uidSet.find((int32_t) key) == uidSet.end()) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700617 uidsToDelete.push_back(key);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700618 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700619 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700620 };
Chenbo Feng703798e2018-06-15 17:07:59 -0700621 RETURN_IF_NOT_OK(mUidOwnerMap.iterate(getUidsToDelete));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700622
623 for(auto uid : uidsToDelete) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100624 RETURN_IF_NOT_OK(removeRule(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700625 }
626
627 for (auto uid : uids) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100628 RETURN_IF_NOT_OK(addRule(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700629 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700630 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700631}
632
Rubin Xuec27ff22019-01-08 21:33:03 +0000633Status 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 Xu5ca8e802019-04-05 11:50:32 +0100645 netdutils::Status result = addRule(mUidOwnerMap, uid, IIF_MATCH, iif);
Rubin Xuec27ff22019-01-08 21:33:03 +0000646 if (!isOk(result)) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100647 ALOGW("addRule failed(%d): uid=%d iif=%d", result.code(), uid, iif);
Rubin Xuec27ff22019-01-08 21:33:03 +0000648 }
649 }
650 return netdutils::status::ok;
651}
652
653Status 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 Xu5ca8e802019-04-05 11:50:32 +0100661 netdutils::Status result = removeRule(mUidOwnerMap, uid, IIF_MATCH);
Rubin Xuec27ff22019-01-08 21:33:03 +0000662 if (!isOk(result)) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100663 ALOGW("removeRule failed(%d): uid=%d", result.code(), uid);
Rubin Xuec27ff22019-01-08 21:33:03 +0000664 }
665 }
666 return netdutils::status::ok;
667}
668
Chenbo Feng89c12f12018-03-21 10:29:18 -0700669int TrafficController::replaceUidOwnerMap(const std::string& name, bool isWhitelist,
670 const std::vector<int32_t>& uids) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700671 FirewallRule rule;
672 FirewallType type;
673 if (isWhitelist) {
674 type = WHITELIST;
675 rule = ALLOW;
676 } else {
677 type = BLACKLIST;
678 rule = DENY;
679 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700680 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700681 if (!name.compare(FirewallController::LOCAL_DOZABLE)) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100682 res = replaceRulesInMap(DOZABLE_MATCH, uids);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700683 } else if (!name.compare(FirewallController::LOCAL_STANDBY)) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100684 res = replaceRulesInMap(STANDBY_MATCH, uids);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700685 } else if (!name.compare(FirewallController::LOCAL_POWERSAVE)) {
Rubin Xu5ca8e802019-04-05 11:50:32 +0100686 res = replaceRulesInMap(POWERSAVE_MATCH, uids);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700687 } else {
688 ALOGE("unknown chain name: %s", name.c_str());
689 return -EINVAL;
690 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700691 if (!isOk(res)) {
692 ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str());
693 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700694 }
695 return 0;
696}
697
698int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900699 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Fengf434e862018-06-27 14:08:39 -0700700 uint32_t key = UID_RULES_CONFIGURATION_KEY;
Chenbo Feng703798e2018-06-15 17:07:59 -0700701 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 Feng4f6c2372018-04-26 10:37:55 -0700707 Status res;
Chenbo Feng703798e2018-06-15 17:07:59 -0700708 BpfConfig newConfiguration;
709 uint8_t match;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700710 switch (chain) {
711 case DOZABLE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700712 match = DOZABLE_MATCH;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700713 break;
714 case STANDBY:
Chenbo Feng703798e2018-06-15 17:07:59 -0700715 match = STANDBY_MATCH;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700716 break;
717 case POWERSAVE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700718 match = POWERSAVE_MATCH;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700719 break;
720 default:
721 return -EINVAL;
722 }
Chenbo Feng703798e2018-06-15 17:07:59 -0700723 newConfiguration =
724 enable ? (oldConfiguration.value() | match) : (oldConfiguration.value() & (~match));
725 res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700726 if (!isOk(res)) {
727 ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str());
Chenbo Feng89c12f12018-03-21 10:29:18 -0700728 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700729 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700730}
731
Chenbo Feng47dd0732018-12-11 12:23:24 -0800732BpfLevel TrafficController::getBpfLevel() {
733 return mBpfLevel;
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800734}
735
Chenbo Feng873ae142019-04-10 12:26:06 -0700736Status 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 Feng48eaed32018-12-26 17:40:21 -0800778void TrafficController::setPermissionForUids(int permission, const std::vector<uid_t>& uids) {
Chenbo Fengbf660aa2019-02-26 16:12:27 -0800779 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 Feng48eaed32018-12-26 17:40:21 -0800789 bool privileged = (permission & INetd::PERMISSION_UPDATE_DEVICE_STATS);
790
791 for (uid_t uid : uids) {
Chenbo Feng527908a2019-04-02 20:14:11 -0700792 // 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 Fengbf660aa2019-02-26 16:12:27 -0800795 Status ret = mUidPermissionMap.writeValue(uid, permission, BPF_ANY);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800796 if (!isOk(ret)) {
Chenbo Feng527908a2019-04-02 20:14:11 -0700797 ALOGE("Failed to set permission: %s of uid(%u) to permission map: %s",
798 UidPermissionTypeToString(permission).c_str(), uid, strerror(ret.code()));
Chenbo Feng48eaed32018-12-26 17:40:21 -0800799 }
800 } else {
801 Status ret = mUidPermissionMap.deleteValue(uid);
802 if (!isOk(ret) && ret.code() != ENOENT) {
Chenbo Feng527908a2019-04-02 20:14:11 -0700803 ALOGE("Failed to remove uid %u from permission map: %s", uid, strerror(ret.code()));
Chenbo Feng48eaed32018-12-26 17:40:21 -0800804 }
805 }
806
807 if (privileged) {
808 mPrivilegedUser.insert(uid);
809 } else {
810 mPrivilegedUser.erase(uid);
811 }
812 }
813}
814
Chenbo Fengef297172018-03-26 10:53:33 -0700815std::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 Feng4f6c2372018-04-26 10:37:55 -0700826std::string getMapStatus(const base::unique_fd& map_fd, const char* path) {
Chenbo Fengef297172018-03-26 10:53:33 -0700827 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 Innocentia5161a02019-01-30 22:40:53 +0900836// NOLINTNEXTLINE(google-runtime-references): grandfathered pass by non-const reference
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900837void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) {
Chenbo Fengef297172018-03-26 10:53:33 -0700838 dw.blankline();
839 dw.println("%s:", mapName.c_str());
Erik Klineb31fd692018-06-06 20:50:11 +0900840 if (!header.empty()) {
841 dw.println(header);
Chenbo Fengef297172018-03-26 10:53:33 -0700842 }
843}
844
845const String16 TrafficController::DUMP_KEYWORD = String16("trafficcontroller");
846
847void TrafficController::dump(DumpWriter& dw, bool verbose) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900848 std::lock_guard ownerMapGuard(mOwnerMatchMutex);
Erik Klineb31fd692018-06-06 20:50:11 +0900849 ScopedIndent indentTop(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700850 dw.println("TrafficController");
851
Erik Klineb31fd692018-06-06 20:50:11 +0900852 ScopedIndent indentPreBpfModule(dw);
Chenbo Feng47dd0732018-12-11 12:23:24 -0800853 dw.println("BPF module status: %s", BpfLevelToString(mBpfLevel).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700854
Chenbo Feng47dd0732018-12-11 12:23:24 -0800855 if (mBpfLevel == BpfLevel::NONE) {
Chenbo Fengef297172018-03-26 10:53:33 -0700856 return;
Erik Klineb31fd692018-06-06 20:50:11 +0900857 }
Chenbo Fengef297172018-03-26 10:53:33 -0700858
859 dw.blankline();
860 dw.println("mCookieTagMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700861 getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700862 dw.println("mUidCounterSetMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700863 getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str());
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700864 dw.println("mAppUidStatsMap status: %s",
865 getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str());
Chenbo Fengf434e862018-06-27 14:08:39 -0700866 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 Fengef297172018-03-26 10:53:33 -0700870 dw.println("mIfaceIndexNameMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700871 getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700872 dw.println("mIfaceStatsMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700873 getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str());
Chenbo Feng703798e2018-06-15 17:07:59 -0700874 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 Fengef297172018-03-26 10:53:33 -0700878
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 Feng95892f32018-06-07 14:52:02 -0700887 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 Fengef297172018-03-26 10:53:33 -0700891
Erik Klineb31fd692018-06-06 20:50:11 +0900892 if (!verbose) {
893 return;
894 }
Chenbo Fengef297172018-03-26 10:53:33 -0700895
896 dw.blankline();
897 dw.println("BPF map content:");
898
Erik Klineb31fd692018-06-06 20:50:11 +0900899 ScopedIndent indentForMapContent(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700900
901 // Print CookieTagMap content.
902 dumpBpfMap("mCookieTagMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700903 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 Fengef297172018-03-26 10:53:33 -0700907 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700908 Status res = mCookieTagMap.iterateWithValue(printCookieTagInfo);
909 if (!isOk(res)) {
910 dw.println("mCookieTagMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700911 }
912
913 // Print UidCounterSetMap Content
914 dumpBpfMap("mUidCounterSetMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700915 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 Fengef297172018-03-26 10:53:33 -0700919 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700920 res = mUidCounterSetMap.iterateWithValue(printUidInfo);
921 if (!isOk(res)) {
922 dw.println("mUidCounterSetMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700923 }
924
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700925 // 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 Fengef297172018-03-26 10:53:33 -0700939 // Print uidStatsMap content
940 std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes"
941 " rxPackets txBytes txPackets");
Chenbo Fengf434e862018-06-27 14:08:39 -0700942 dumpBpfMap("mStatsMapA", dw, statsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700943 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 Fengef297172018-03-26 10:53:33 -0700949 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700950 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 Fengef297172018-03-26 10:53:33 -0700954 };
Chenbo Fengf434e862018-06-27 14:08:39 -0700955 res = mStatsMapA.iterateWithValue(printStatsInfo);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700956 if (!isOk(res)) {
Chenbo Fengf434e862018-06-27 14:08:39 -0700957 dw.println("mStatsMapA print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700958 }
959
960 // Print TagStatsMap content.
Chenbo Fengf434e862018-06-27 14:08:39 -0700961 dumpBpfMap("mStatsMapB", dw, statsHeader);
962 res = mStatsMapB.iterateWithValue(printStatsInfo);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700963 if (!isOk(res)) {
Chenbo Fengf434e862018-06-27 14:08:39 -0700964 dw.println("mStatsMapB print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700965 }
966
967 // Print ifaceIndexToNameMap content.
968 dumpBpfMap("mIfaceIndexNameMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700969 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 Fengef297172018-03-26 10:53:33 -0700974 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700975 res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo);
976 if (!isOk(res)) {
977 dw.println("mIfaceIndexNameMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700978 }
979
980 // Print ifaceStatsMap content
981 std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes"
982 " txPackets");
983 dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700984 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 Fengef297172018-03-26 10:53:33 -0700989 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700990 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 Fengef297172018-03-26 10:53:33 -0700993 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700994 res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo);
995 if (!isOk(res)) {
996 dw.println("mIfaceStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700997 }
998
Chenbo Feng703798e2018-06-15 17:07:59 -0700999 dw.blankline();
Chenbo Fengef297172018-03-26 10:53:33 -07001000
Chenbo Fengf434e862018-06-27 14:08:39 -07001001 uint32_t key = UID_RULES_CONFIGURATION_KEY;
Chenbo Feng703798e2018-06-15 17:07:59 -07001002 auto configuration = mConfigurationMap.readValue(key);
1003 if (isOk(configuration)) {
Chenbo Fengf434e862018-06-27 14:08:39 -07001004 dw.println("current ownerMatch configuration: %d", configuration.value());
Chenbo Feng703798e2018-06-15 17:07:59 -07001005 } else {
Chenbo Fengf434e862018-06-27 14:08:39 -07001006 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 Fengef297172018-03-26 10:53:33 -07001016 }
Chenbo Feng703798e2018-06-15 17:07:59 -07001017 dumpBpfMap("mUidOwnerMap", dw, "");
Rubin Xuec27ff22019-01-08 21:33:03 +00001018 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 Feng703798e2018-06-15 17:07:59 -07001031 return netdutils::status::ok;
1032 };
1033 res = mUidOwnerMap.iterateWithValue(printUidMatchInfo);
Chenbo Feng4f6c2372018-04-26 10:37:55 -07001034 if (!isOk(res)) {
Chenbo Feng703798e2018-06-15 17:07:59 -07001035 dw.println("mUidOwnerMap print end with error: %s", res.msg().c_str());
Chenbo Feng95892f32018-06-07 14:52:02 -07001036 }
Chenbo Feng48eaed32018-12-26 17:40:21 -08001037 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 Fengef297172018-03-26 10:53:33 -07001052}
1053
Chenbo Fengf2759682017-10-10 17:31:57 -07001054} // namespace net
1055} // namespace android