blob: 4c50525cdd15422cc8777c1a075fdc3bbfad343b [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
17#ifndef NETD_SERVER_TRAFFIC_CONTROLLER_H
18#define NETD_SERVER_TRAFFIC_CONTROLLER_H
19
20#include <linux/bpf.h>
21
Chenbo Feng95892f32018-06-07 14:52:02 -070022#include "BandwidthController.h"
Chenbo Feng89c12f12018-03-21 10:29:18 -070023#include "FirewallController.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080024#include "NetlinkListener.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070025#include "Network.h"
Chenbo Feng89c12f12018-03-21 10:29:18 -070026#include "android-base/thread_annotations.h"
Chenbo Fengc10a8a42017-12-15 13:56:33 -080027#include "android-base/unique_fd.h"
Chenbo Feng4f6c2372018-04-26 10:37:55 -070028#include "bpf/BpfMap.h"
Chenbo Fengd6104d12018-10-16 20:29:29 -070029#include "netdbpf/bpf_shared.h"
Luke Huangb257d612019-03-14 21:19:13 +080030#include "netdutils/DumpWriter.h"
Bernie Innocenti97f388f2018-10-16 19:17:08 +090031#include "netdutils/StatusOr.h"
32#include "utils/String16.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070033
Chenbo Feng4f6c2372018-04-26 10:37:55 -070034using android::bpf::BpfMap;
Chenbo Fengf2759682017-10-10 17:31:57 -070035
36namespace android {
37namespace net {
38
Chenbo Fengf2759682017-10-10 17:31:57 -070039class TrafficController {
40 public:
Chenbo Feng89c12f12018-03-21 10:29:18 -070041 TrafficController();
Chenbo Fengf2759682017-10-10 17:31:57 -070042 /*
43 * Initialize the whole controller
44 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080045 netdutils::Status start();
Chenbo Fengf2759682017-10-10 17:31:57 -070046 /*
47 * Tag the socket with the specified tag and uid. In the qtaguid module, the
48 * first tag request that grab the spinlock of rb_tree can update the tag
49 * information first and other request need to wait until it finish. All the
50 * tag request will be addressed in the order of they obtaining the spinlock.
51 * In the eBPF implementation, the kernel will try to update the eBPF map
52 * entry with the tag request. And the hashmap update process is protected by
53 * the spinlock initialized with the map. So the behavior of two modules
54 * should be the same. No additional lock needed.
55 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -070056 int tagSocket(int sockFd, uint32_t tag, uid_t uid, uid_t callingUid) EXCLUDES(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -070057
58 /*
59 * The untag process is similiar to tag socket and both old qtaguid module and
60 * new eBPF module have spinlock inside the kernel for concurrent update. No
61 * external lock is required.
62 */
63 int untagSocket(int sockFd);
64
65 /*
66 * Similiar as above, no external lock required.
67 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -070068 int setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid) EXCLUDES(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -070069
70 /*
71 * When deleting a tag data, the qtaguid module will grab the spinlock of each
72 * related rb_tree one by one and delete the tag information, counterSet
73 * information, iface stats information and uid stats information one by one.
74 * The new eBPF implementation is done similiarly by removing the entry on
75 * each map one by one. And deleting processes are also protected by the
76 * spinlock of the map. So no additional lock is required.
77 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -070078 int deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid) EXCLUDES(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -070079
Chenbo Feng07d43fe2017-12-21 14:38:51 -080080 /*
81 * Check if the current device have the bpf traffic stats accounting service
82 * running.
83 */
Maciej Żenczykowski3d7c9662020-02-11 17:11:26 -080084 bool getBpfEnabled();
Chenbo Feng07d43fe2017-12-21 14:38:51 -080085
Chenbo Feng7e974052018-02-28 22:57:21 -080086 /*
Chenbo Feng873ae142019-04-10 12:26:06 -070087 * Swap the stats map config from current active stats map to the idle one.
88 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -070089 netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex);
Chenbo Feng873ae142019-04-10 12:26:06 -070090
91 /*
Chenbo Feng7e974052018-02-28 22:57:21 -080092 * Add the interface name and index pair into the eBPF map.
93 */
94 int addInterface(const char* name, uint32_t ifaceIndex);
95
Chenbo Feng89c12f12018-03-21 10:29:18 -070096 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
97
98 int removeUidOwnerRule(const uid_t uid);
99
100 int replaceUidOwnerMap(const std::string& name, bool isWhitelist,
101 const std::vector<int32_t>& uids);
102
Chenbo Feng703798e2018-06-15 17:07:59 -0700103 netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700104 FirewallType type) EXCLUDES(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700105
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700106 void dump(netdutils::DumpWriter& dw, bool verbose) EXCLUDES(mMutex);
Chenbo Fengef297172018-03-26 10:53:33 -0700107
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700108 netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids)
109 EXCLUDES(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700110
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700111 netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids)
112 EXCLUDES(mMutex);
113 netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex);
Rubin Xuec27ff22019-01-08 21:33:03 +0000114
Uldiniadb198ec92018-03-01 08:54:00 -0500115 netdutils::Status addUidInterfaceBlacklist(const int ifBlacklistSlot, const int iface,
116 const std::vector<std::string>& appStrUids)
117 EXCLUDES(mMutex);
118 netdutils::Status removeUidInterfaceBlacklist(const int ifBlacklistSlot,
119 const std::vector<std::string>& appStrUids)
120 EXCLUDES(mMutex);
121
Chenbo Feng703798e2018-06-15 17:07:59 -0700122 netdutils::Status updateUidOwnerMap(const std::vector<std::string>& appStrUids,
123 BandwidthController::IptJumpOp jumpHandling,
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700124 BandwidthController::IptOp op) EXCLUDES(mMutex);
Chenbo Fengef297172018-03-26 10:53:33 -0700125 static const String16 DUMP_KEYWORD;
126
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700127 int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700128
Chenbo Feng49586642018-08-30 18:01:53 -0700129 static netdutils::StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener();
130
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700131 void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800132
Chenbo Fengf2759682017-10-10 17:31:57 -0700133 private:
134 /*
135 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
Chenbo Fengef1cab32018-04-13 19:50:49 -0700136 * DO NOT hold any locks when modifying this map, otherwise when the untag
137 * operation is waiting for a lock hold by other process and there are more
138 * sockets being closed than can fit in the socket buffer of the netlink socket
139 * that receives them, then the kernel will drop some of these sockets and we
140 * won't delete their tags.
Chenbo Fengf2759682017-10-10 17:31:57 -0700141 * Map Key: uint64_t socket cookie
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800142 * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
Chenbo Fengf2759682017-10-10 17:31:57 -0700143 */
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800144 BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700145
146 /*
147 * mUidCounterSetMap: Store the counterSet of a specific uid.
148 * Map Key: uint32 uid.
149 * Map Value: uint32 counterSet specifies if the traffic is a background
150 * or foreground traffic.
151 */
Chenbo Fengf01f5302019-05-07 12:28:13 -0700152 BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700153
154 /*
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700155 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
156 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
157 * API to return persistent stats for a specific uid since device boot.
158 */
159 BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
160
161 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700162 * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
163 * combination of uid, tag, iface and counterSet. These two maps contain
164 * both tagged and untagged traffic.
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800165 * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
Chenbo Fengf434e862018-06-27 14:08:39 -0700166 * information.
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800167 * Map Value: Stats, contains packet count and byte count of each
Chenbo Fengf2759682017-10-10 17:31:57 -0700168 * transport protocol on egress and ingress direction.
169 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700170 BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700171
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700172 BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700173
Chenbo Feng7e974052018-02-28 22:57:21 -0800174 /*
175 * mIfaceIndexNameMap: Store the index name pair of each interface show up
176 * on the device since boot. The interface index is used by the eBPF program
177 * to correctly match the iface name when receiving a packet.
178 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700179 BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
Chenbo Feng7e974052018-02-28 22:57:21 -0800180
Chenbo Feng5ed17992018-03-13 21:30:49 -0700181 /*
182 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
183 * filter.
184 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700185 BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
Chenbo Feng5ed17992018-03-13 21:30:49 -0700186
Chenbo Feng89c12f12018-03-21 10:29:18 -0700187 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700188 * mConfigurationMap: Store the current network policy about uid filtering
189 * and the current stats map in use. There are two configuration entries in
190 * the map right now:
191 * - Entry with UID_RULES_CONFIGURATION_KEY:
192 * Store the configuration for the current uid rules. It indicates the device
193 * is in doze/powersave/standby mode.
194 * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
195 * Stores the current live stats map that kernel program is writing to.
196 * Userspace can do scraping and cleaning job on the other one depending on the
197 * current configs.
Chenbo Feng89c12f12018-03-21 10:29:18 -0700198 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700199 BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700200
Chenbo Feng95892f32018-06-07 14:52:02 -0700201 /*
Chenbo Feng703798e2018-06-15 17:07:59 -0700202 * mUidOwnerMap: Store uids that are used for bandwidth control uid match.
Chenbo Feng95892f32018-06-07 14:52:02 -0700203 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700204 BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex);
Chenbo Feng95892f32018-06-07 14:52:02 -0700205
Chenbo Feng48eaed32018-12-26 17:40:21 -0800206 /*
207 * mUidOwnerMap: Store uids that are used for INTERNET permission check.
208 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700209 BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800210
Chenbo Feng116d0552017-12-04 17:25:19 -0800211 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
212
Rubin Xu5ca8e802019-04-05 11:50:32 +0100213 netdutils::Status removeRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
Uldiniadb198ec92018-03-01 08:54:00 -0500214 UidOwnerMatchType match, uint32_t ifBlacklistSlot = 0)
215 REQUIRES(mMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700216
Rubin Xu5ca8e802019-04-05 11:50:32 +0100217 netdutils::Status addRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
Uldiniadb198ec92018-03-01 08:54:00 -0500218 UidOwnerMatchType match, uint32_t iif = 0,
219 uint32_t ifBlacklistSlot = 0) REQUIRES(mMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700220
Maciej Żenczykowski3d7c9662020-02-11 17:11:26 -0800221 bool mBpfEnabled;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800222
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700223 // mMutex guards all accesses to mConfigurationMap, mUidOwnerMap, mUidPermissionMap,
224 // mStatsMapA, mStatsMapB and mPrivilegedUser. It is designed to solve the following
225 // problems:
226 // 1. Prevent concurrent access and modification to mConfigurationMap, mUidOwnerMap,
227 // mUidPermissionMap, and mPrivilegedUser. These data members are controlled by netd but can
Chenbo Fengfd88f9d2019-05-01 12:59:09 -0700228 // be modified from different threads. TrafficController provides several APIs directly
229 // called by the binder RPC, and different binder threads can concurrently access these data
Chenbo Fengf01f5302019-05-07 12:28:13 -0700230 // members mentioned above. Some of the data members such as mUidPermissionMap and
231 // mPrivilegedUsers are also accessed from a different thread when tagging sockets or
232 // setting the counterSet through FwmarkServer
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700233 // 2. Coordinate the deletion of uid stats in mStatsMapA and mStatsMapB. The system server
234 // always call into netd to ask for a live stats map change before it pull and clean up the
235 // stats from the inactive map. The mMutex will block netd from accessing the stats map when
Chenbo Fengfd88f9d2019-05-01 12:59:09 -0700236 // the mConfigurationMap is updating the current stats map so netd will not accidentally
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700237 // read the map that system_server is cleaning up.
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700238 std::mutex mMutex;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700239
Chenbo Feng82075e52019-04-12 19:21:47 -0700240 // The limit on the number of stats entries a uid can have in the per uid stats map.
241 // TrafficController will block that specific uid from tagging new sockets after the limit is
242 // reached.
243 const uint32_t mPerUidStatsEntriesLimit;
244
245 // The limit on the total number of stats entries in the per uid stats map. TrafficController
246 // will block all tagging requests after the limit is reached.
247 const uint32_t mTotalUidStatsEntriesLimit;
248
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800249 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
250 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800251
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700252 netdutils::Status initMaps() EXCLUDES(mMutex);
Chenbo Feng95892f32018-06-07 14:52:02 -0700253
Chenbo Feng48eaed32018-12-26 17:40:21 -0800254 // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
255 // need to call back to system server for permission check.
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700256 std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800257
Chenbo Feng703798e2018-06-15 17:07:59 -0700258 UidOwnerMatchType jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling);
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800259
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700260 bool hasUpdateDeviceStatsPermission(uid_t uid) REQUIRES(mMutex);
Chenbo Feng82075e52019-04-12 19:21:47 -0700261
262 // For testing
263 TrafficController(uint32_t perUidLimit, uint32_t totalLimit);
264
Chenbo Fenged37fea2017-12-13 19:35:01 -0800265 // For testing
266 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700267};
268
269} // namespace net
270} // namespace android
271
272#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H