blob: a2539a9026bcdb622f5d3756bb6186486f4f6e06 [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
Chenbo Feng703798e2018-06-15 17:07:59 -0700115 netdutils::Status updateUidOwnerMap(const std::vector<std::string>& appStrUids,
116 BandwidthController::IptJumpOp jumpHandling,
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700117 BandwidthController::IptOp op) EXCLUDES(mMutex);
Chenbo Fengef297172018-03-26 10:53:33 -0700118 static const String16 DUMP_KEYWORD;
119
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700120 int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700121
Chenbo Feng49586642018-08-30 18:01:53 -0700122 static netdutils::StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener();
123
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700124 void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800125
Chenbo Fengf2759682017-10-10 17:31:57 -0700126 private:
127 /*
128 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
Chenbo Fengef1cab32018-04-13 19:50:49 -0700129 * DO NOT hold any locks when modifying this map, otherwise when the untag
130 * operation is waiting for a lock hold by other process and there are more
131 * sockets being closed than can fit in the socket buffer of the netlink socket
132 * that receives them, then the kernel will drop some of these sockets and we
133 * won't delete their tags.
Chenbo Fengf2759682017-10-10 17:31:57 -0700134 * Map Key: uint64_t socket cookie
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800135 * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
Chenbo Fengf2759682017-10-10 17:31:57 -0700136 */
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800137 BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700138
139 /*
140 * mUidCounterSetMap: Store the counterSet of a specific uid.
141 * Map Key: uint32 uid.
142 * Map Value: uint32 counterSet specifies if the traffic is a background
143 * or foreground traffic.
144 */
Chenbo Fengf01f5302019-05-07 12:28:13 -0700145 BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700146
147 /*
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700148 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
149 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
150 * API to return persistent stats for a specific uid since device boot.
151 */
152 BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
153
154 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700155 * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
156 * combination of uid, tag, iface and counterSet. These two maps contain
157 * both tagged and untagged traffic.
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800158 * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
Chenbo Fengf434e862018-06-27 14:08:39 -0700159 * information.
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800160 * Map Value: Stats, contains packet count and byte count of each
Chenbo Fengf2759682017-10-10 17:31:57 -0700161 * transport protocol on egress and ingress direction.
162 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700163 BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700164
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700165 BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700166
Chenbo Feng7e974052018-02-28 22:57:21 -0800167 /*
168 * mIfaceIndexNameMap: Store the index name pair of each interface show up
169 * on the device since boot. The interface index is used by the eBPF program
170 * to correctly match the iface name when receiving a packet.
171 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700172 BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
Chenbo Feng7e974052018-02-28 22:57:21 -0800173
Chenbo Feng5ed17992018-03-13 21:30:49 -0700174 /*
175 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
176 * filter.
177 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700178 BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
Chenbo Feng5ed17992018-03-13 21:30:49 -0700179
Chenbo Feng89c12f12018-03-21 10:29:18 -0700180 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700181 * mConfigurationMap: Store the current network policy about uid filtering
182 * and the current stats map in use. There are two configuration entries in
183 * the map right now:
184 * - Entry with UID_RULES_CONFIGURATION_KEY:
185 * Store the configuration for the current uid rules. It indicates the device
186 * is in doze/powersave/standby mode.
187 * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
188 * Stores the current live stats map that kernel program is writing to.
189 * Userspace can do scraping and cleaning job on the other one depending on the
190 * current configs.
Chenbo Feng89c12f12018-03-21 10:29:18 -0700191 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700192 BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700193
Chenbo Feng95892f32018-06-07 14:52:02 -0700194 /*
Chenbo Feng703798e2018-06-15 17:07:59 -0700195 * mUidOwnerMap: Store uids that are used for bandwidth control uid match.
Chenbo Feng95892f32018-06-07 14:52:02 -0700196 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700197 BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex);
Chenbo Feng95892f32018-06-07 14:52:02 -0700198
Chenbo Feng48eaed32018-12-26 17:40:21 -0800199 /*
200 * mUidOwnerMap: Store uids that are used for INTERNET permission check.
201 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700202 BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800203
Chenbo Feng116d0552017-12-04 17:25:19 -0800204 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
205
Rubin Xu5ca8e802019-04-05 11:50:32 +0100206 netdutils::Status removeRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700207 UidOwnerMatchType match) REQUIRES(mMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700208
Rubin Xu5ca8e802019-04-05 11:50:32 +0100209 netdutils::Status addRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700210 UidOwnerMatchType match, uint32_t iif = 0) REQUIRES(mMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700211
Maciej Żenczykowski3d7c9662020-02-11 17:11:26 -0800212 bool mBpfEnabled;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800213
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700214 // mMutex guards all accesses to mConfigurationMap, mUidOwnerMap, mUidPermissionMap,
215 // mStatsMapA, mStatsMapB and mPrivilegedUser. It is designed to solve the following
216 // problems:
217 // 1. Prevent concurrent access and modification to mConfigurationMap, mUidOwnerMap,
218 // mUidPermissionMap, and mPrivilegedUser. These data members are controlled by netd but can
Chenbo Fengfd88f9d2019-05-01 12:59:09 -0700219 // be modified from different threads. TrafficController provides several APIs directly
220 // called by the binder RPC, and different binder threads can concurrently access these data
Chenbo Fengf01f5302019-05-07 12:28:13 -0700221 // members mentioned above. Some of the data members such as mUidPermissionMap and
222 // mPrivilegedUsers are also accessed from a different thread when tagging sockets or
223 // setting the counterSet through FwmarkServer
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700224 // 2. Coordinate the deletion of uid stats in mStatsMapA and mStatsMapB. The system server
225 // always call into netd to ask for a live stats map change before it pull and clean up the
226 // stats from the inactive map. The mMutex will block netd from accessing the stats map when
Chenbo Fengfd88f9d2019-05-01 12:59:09 -0700227 // the mConfigurationMap is updating the current stats map so netd will not accidentally
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700228 // read the map that system_server is cleaning up.
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700229 std::mutex mMutex;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700230
Chenbo Feng82075e52019-04-12 19:21:47 -0700231 // The limit on the number of stats entries a uid can have in the per uid stats map.
232 // TrafficController will block that specific uid from tagging new sockets after the limit is
233 // reached.
234 const uint32_t mPerUidStatsEntriesLimit;
235
236 // The limit on the total number of stats entries in the per uid stats map. TrafficController
237 // will block all tagging requests after the limit is reached.
238 const uint32_t mTotalUidStatsEntriesLimit;
239
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800240 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
241 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800242
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700243 netdutils::Status initMaps() EXCLUDES(mMutex);
Chenbo Feng95892f32018-06-07 14:52:02 -0700244
Chenbo Feng48eaed32018-12-26 17:40:21 -0800245 // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
246 // need to call back to system server for permission check.
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700247 std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800248
Chenbo Feng703798e2018-06-15 17:07:59 -0700249 UidOwnerMatchType jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling);
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800250
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700251 bool hasUpdateDeviceStatsPermission(uid_t uid) REQUIRES(mMutex);
Chenbo Feng82075e52019-04-12 19:21:47 -0700252
253 // For testing
254 TrafficController(uint32_t perUidLimit, uint32_t totalLimit);
255
Chenbo Fenged37fea2017-12-13 19:35:01 -0800256 // For testing
257 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700258};
259
260} // namespace net
261} // namespace android
262
263#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H