blob: 3e98b68c34ad6aa2fe418981c4eef4b2fff86abf [file] [log] [blame]
Wayne Ma4d692332022-01-19 16:04:04 +08001/*
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
17#ifndef NETD_SERVER_TRAFFIC_CONTROLLER_H
18#define NETD_SERVER_TRAFFIC_CONTROLLER_H
19
20#include <linux/bpf.h>
21
Wayne Ma92d80792022-01-20 13:20:34 +080022#include "NetlinkListener.h"
Wayne Ma4d692332022-01-19 16:04:04 +080023#include "android-base/thread_annotations.h"
Wayne Ma4d692332022-01-19 16:04:04 +080024#include "bpf/BpfMap.h"
25#include "bpf_shared.h"
26#include "netdutils/DumpWriter.h"
27#include "netdutils/NetlinkListener.h"
28#include "netdutils/StatusOr.h"
29#include "utils/String16.h"
30
31namespace android {
32namespace net {
33
34class TrafficController {
35 public:
Wayne Ma4d692332022-01-19 16:04:04 +080036 /*
37 * Initialize the whole controller
38 */
39 netdutils::Status start();
Wayne Ma4d692332022-01-19 16:04:04 +080040
41 /*
42 * Similiar as above, no external lock required.
43 */
44 int setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid) EXCLUDES(mMutex);
45
46 /*
47 * When deleting a tag data, the qtaguid module will grab the spinlock of each
48 * related rb_tree one by one and delete the tag information, counterSet
49 * information, iface stats information and uid stats information one by one.
50 * The new eBPF implementation is done similiarly by removing the entry on
51 * each map one by one. And deleting processes are also protected by the
52 * spinlock of the map. So no additional lock is required.
53 */
54 int deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid) EXCLUDES(mMutex);
55
56 /*
57 * Swap the stats map config from current active stats map to the idle one.
58 */
59 netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex);
60
61 /*
62 * Add the interface name and index pair into the eBPF map.
63 */
64 int addInterface(const char* name, uint32_t ifaceIndex);
65
66 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
67
68 int removeUidOwnerRule(const uid_t uid);
69
70 int replaceUidOwnerMap(const std::string& name, bool isAllowlist,
71 const std::vector<int32_t>& uids);
72
73 enum IptOp { IptOpInsert, IptOpDelete };
74
75 netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
76 FirewallType type) EXCLUDES(mMutex);
77
78 void dump(netdutils::DumpWriter& dw, bool verbose) EXCLUDES(mMutex);
79
80 netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids)
81 EXCLUDES(mMutex);
82
83 netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids)
84 EXCLUDES(mMutex);
85 netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex);
86
87 netdutils::Status updateUidOwnerMap(const std::vector<uint32_t>& appStrUids,
88 UidOwnerMatchType matchType, IptOp op) EXCLUDES(mMutex);
89 static const String16 DUMP_KEYWORD;
90
91 int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex);
92
93 static netdutils::StatusOr<std::unique_ptr<netdutils::NetlinkListenerInterface>>
94 makeSkDestroyListener();
95
96 void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex);
97
98 FirewallType getFirewallType(ChildChain);
99
100 static const char* LOCAL_DOZABLE;
101 static const char* LOCAL_STANDBY;
102 static const char* LOCAL_POWERSAVE;
103 static const char* LOCAL_RESTRICTED;
104
105 private:
106 /*
107 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
108 * DO NOT hold any locks when modifying this map, otherwise when the untag
109 * operation is waiting for a lock hold by other process and there are more
110 * sockets being closed than can fit in the socket buffer of the netlink socket
111 * that receives them, then the kernel will drop some of these sockets and we
112 * won't delete their tags.
113 * Map Key: uint64_t socket cookie
114 * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
115 */
116 bpf::BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
117
118 /*
119 * mUidCounterSetMap: Store the counterSet of a specific uid.
120 * Map Key: uint32 uid.
121 * Map Value: uint32 counterSet specifies if the traffic is a background
122 * or foreground traffic.
123 */
124 bpf::BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
125
126 /*
127 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
128 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
129 * API to return persistent stats for a specific uid since device boot.
130 */
131 bpf::BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
132
133 /*
134 * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
135 * combination of uid, tag, iface and counterSet. These two maps contain
136 * both tagged and untagged traffic.
137 * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
138 * information.
139 * Map Value: Stats, contains packet count and byte count of each
140 * transport protocol on egress and ingress direction.
141 */
142 bpf::BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
143
144 bpf::BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex);
145
146 /*
147 * mIfaceIndexNameMap: Store the index name pair of each interface show up
148 * on the device since boot. The interface index is used by the eBPF program
149 * to correctly match the iface name when receiving a packet.
150 */
151 bpf::BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
152
153 /*
154 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
155 * filter.
156 */
157 bpf::BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
158
159 /*
160 * mConfigurationMap: Store the current network policy about uid filtering
161 * and the current stats map in use. There are two configuration entries in
162 * the map right now:
163 * - Entry with UID_RULES_CONFIGURATION_KEY:
164 * Store the configuration for the current uid rules. It indicates the device
165 * is in doze/powersave/standby/restricted mode.
166 * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
167 * Stores the current live stats map that kernel program is writing to.
168 * Userspace can do scraping and cleaning job on the other one depending on the
169 * current configs.
170 */
171 bpf::BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mMutex);
172
173 /*
174 * mUidOwnerMap: Store uids that are used for bandwidth control uid match.
175 */
176 bpf::BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex);
177
178 /*
179 * mUidOwnerMap: Store uids that are used for INTERNET permission check.
180 */
181 bpf::BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex);
182
183 std::unique_ptr<netdutils::NetlinkListenerInterface> mSkDestroyListener;
184
185 netdutils::Status removeRule(uint32_t uid, UidOwnerMatchType match) REQUIRES(mMutex);
186
187 netdutils::Status addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif = 0)
188 REQUIRES(mMutex);
189
190 // mMutex guards all accesses to mConfigurationMap, mUidOwnerMap, mUidPermissionMap,
191 // mStatsMapA, mStatsMapB and mPrivilegedUser. It is designed to solve the following
192 // problems:
193 // 1. Prevent concurrent access and modification to mConfigurationMap, mUidOwnerMap,
194 // mUidPermissionMap, and mPrivilegedUser. These data members are controlled by netd but can
195 // be modified from different threads. TrafficController provides several APIs directly
196 // called by the binder RPC, and different binder threads can concurrently access these data
197 // members mentioned above. Some of the data members such as mUidPermissionMap and
198 // mPrivilegedUsers are also accessed from a different thread when tagging sockets or
199 // setting the counterSet through FwmarkServer
200 // 2. Coordinate the deletion of uid stats in mStatsMapA and mStatsMapB. The system server
201 // always call into netd to ask for a live stats map change before it pull and clean up the
202 // stats from the inactive map. The mMutex will block netd from accessing the stats map when
203 // the mConfigurationMap is updating the current stats map so netd will not accidentally
204 // read the map that system_server is cleaning up.
205 std::mutex mMutex;
206
Wayne Ma4d692332022-01-19 16:04:04 +0800207 netdutils::Status initMaps() EXCLUDES(mMutex);
208
209 // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
210 // need to call back to system server for permission check.
211 std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex);
212
213 bool hasUpdateDeviceStatsPermission(uid_t uid) REQUIRES(mMutex);
214
Wayne Ma4d692332022-01-19 16:04:04 +0800215 // For testing
216 friend class TrafficControllerTest;
217};
218
219} // namespace net
220} // namespace android
221
222#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H