blob: ddcf4459fb84d366332599a1a9b7354185c77bf8 [file] [log] [blame]
Wayne Ma4d692332022-01-19 16:04:04 +08001/*
Wayne Maa9716ff2022-01-12 10:37:04 +08002 * Copyright (C) 2022 The Android Open Source Project
Wayne Ma4d692332022-01-19 16:04:04 +08003 *
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
Wayne Maa9716ff2022-01-12 10:37:04 +080017#pragma once
Wayne Ma4d692332022-01-19 16:04:04 +080018
Wayne Maa9716ff2022-01-12 10:37:04 +080019#include <set>
20#include <Common.h>
Wayne Ma4d692332022-01-19 16:04:04 +080021
Wayne Ma4d692332022-01-19 16:04:04 +080022#include "android-base/thread_annotations.h"
Wayne Ma4d692332022-01-19 16:04:04 +080023#include "bpf/BpfMap.h"
24#include "bpf_shared.h"
25#include "netdutils/DumpWriter.h"
26#include "netdutils/NetlinkListener.h"
27#include "netdutils/StatusOr.h"
Wayne Ma4d692332022-01-19 16:04:04 +080028
29namespace android {
30namespace net {
31
Wayne Maa9716ff2022-01-12 10:37:04 +080032using netdutils::StatusOr;
33
Wayne Ma4d692332022-01-19 16:04:04 +080034class TrafficController {
35 public:
Patrick Rohr313bc6c2022-01-31 15:51:09 +010036 static constexpr char DUMP_KEYWORD[] = "trafficcontroller";
37
Wayne Ma4d692332022-01-19 16:04:04 +080038 /*
39 * Initialize the whole controller
40 */
41 netdutils::Status start();
Wayne Ma4d692332022-01-19 16:04:04 +080042
Wayne Ma4d692332022-01-19 16:04:04 +080043 int setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid) EXCLUDES(mMutex);
44
45 /*
46 * When deleting a tag data, the qtaguid module will grab the spinlock of each
47 * related rb_tree one by one and delete the tag information, counterSet
48 * information, iface stats information and uid stats information one by one.
49 * The new eBPF implementation is done similiarly by removing the entry on
50 * each map one by one. And deleting processes are also protected by the
51 * spinlock of the map. So no additional lock is required.
52 */
53 int deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid) EXCLUDES(mMutex);
54
55 /*
56 * Swap the stats map config from current active stats map to the idle one.
57 */
58 netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex);
59
60 /*
61 * Add the interface name and index pair into the eBPF map.
62 */
63 int addInterface(const char* name, uint32_t ifaceIndex);
64
65 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
66
67 int removeUidOwnerRule(const uid_t uid);
68
69 int replaceUidOwnerMap(const std::string& name, bool isAllowlist,
70 const std::vector<int32_t>& uids);
71
72 enum IptOp { IptOpInsert, IptOpDelete };
73
74 netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
75 FirewallType type) EXCLUDES(mMutex);
76
77 void dump(netdutils::DumpWriter& dw, bool verbose) EXCLUDES(mMutex);
78
79 netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids)
80 EXCLUDES(mMutex);
81
82 netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids)
83 EXCLUDES(mMutex);
84 netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex);
85
Wayne Maa9716ff2022-01-12 10:37:04 +080086 netdutils::Status updateUidOwnerMap(const uint32_t uid,
Wayne Ma4d692332022-01-19 16:04:04 +080087 UidOwnerMatchType matchType, IptOp op) EXCLUDES(mMutex);
Wayne Ma4d692332022-01-19 16:04:04 +080088
89 int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex);
90
91 static netdutils::StatusOr<std::unique_ptr<netdutils::NetlinkListenerInterface>>
92 makeSkDestroyListener();
93
94 void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex);
95
96 FirewallType getFirewallType(ChildChain);
97
98 static const char* LOCAL_DOZABLE;
99 static const char* LOCAL_STANDBY;
100 static const char* LOCAL_POWERSAVE;
101 static const char* LOCAL_RESTRICTED;
102
103 private:
104 /*
105 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
106 * DO NOT hold any locks when modifying this map, otherwise when the untag
107 * operation is waiting for a lock hold by other process and there are more
108 * sockets being closed than can fit in the socket buffer of the netlink socket
109 * that receives them, then the kernel will drop some of these sockets and we
110 * won't delete their tags.
111 * Map Key: uint64_t socket cookie
112 * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
113 */
114 bpf::BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
115
116 /*
117 * mUidCounterSetMap: Store the counterSet of a specific uid.
118 * Map Key: uint32 uid.
119 * Map Value: uint32 counterSet specifies if the traffic is a background
120 * or foreground traffic.
121 */
122 bpf::BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
123
124 /*
125 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
126 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
127 * API to return persistent stats for a specific uid since device boot.
128 */
129 bpf::BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
130
131 /*
132 * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
133 * combination of uid, tag, iface and counterSet. These two maps contain
134 * both tagged and untagged traffic.
135 * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
136 * information.
137 * Map Value: Stats, contains packet count and byte count of each
138 * transport protocol on egress and ingress direction.
139 */
140 bpf::BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
141
142 bpf::BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex);
143
144 /*
145 * mIfaceIndexNameMap: Store the index name pair of each interface show up
146 * on the device since boot. The interface index is used by the eBPF program
147 * to correctly match the iface name when receiving a packet.
148 */
149 bpf::BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
150
151 /*
152 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
153 * filter.
154 */
155 bpf::BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
156
157 /*
158 * mConfigurationMap: Store the current network policy about uid filtering
159 * and the current stats map in use. There are two configuration entries in
160 * the map right now:
161 * - Entry with UID_RULES_CONFIGURATION_KEY:
162 * Store the configuration for the current uid rules. It indicates the device
163 * is in doze/powersave/standby/restricted mode.
164 * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
165 * Stores the current live stats map that kernel program is writing to.
166 * Userspace can do scraping and cleaning job on the other one depending on the
167 * current configs.
168 */
169 bpf::BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mMutex);
170
171 /*
172 * mUidOwnerMap: Store uids that are used for bandwidth control uid match.
173 */
174 bpf::BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex);
175
176 /*
177 * mUidOwnerMap: Store uids that are used for INTERNET permission check.
178 */
179 bpf::BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex);
180
181 std::unique_ptr<netdutils::NetlinkListenerInterface> mSkDestroyListener;
182
183 netdutils::Status removeRule(uint32_t uid, UidOwnerMatchType match) REQUIRES(mMutex);
184
185 netdutils::Status addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif = 0)
186 REQUIRES(mMutex);
187
Wayne Ma4d692332022-01-19 16:04:04 +0800188 std::mutex mMutex;
189
Wayne Ma4d692332022-01-19 16:04:04 +0800190 netdutils::Status initMaps() EXCLUDES(mMutex);
191
192 // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
193 // need to call back to system server for permission check.
194 std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex);
195
196 bool hasUpdateDeviceStatsPermission(uid_t uid) REQUIRES(mMutex);
197
Wayne Ma4d692332022-01-19 16:04:04 +0800198 // For testing
199 friend class TrafficControllerTest;
200};
201
202} // namespace net
203} // namespace android