blob: 2e799598cf5014cbd175d096f5858adcf5a88838 [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 /*
Chenbo Feng873ae142019-04-10 12:26:06 -070081 * Swap the stats map config from current active stats map to the idle one.
82 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -070083 netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex);
Chenbo Feng873ae142019-04-10 12:26:06 -070084
85 /*
Chenbo Feng7e974052018-02-28 22:57:21 -080086 * Add the interface name and index pair into the eBPF map.
87 */
88 int addInterface(const char* name, uint32_t ifaceIndex);
89
Chenbo Feng89c12f12018-03-21 10:29:18 -070090 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
91
92 int removeUidOwnerRule(const uid_t uid);
93
Lorenzo Colitticdd79f12020-07-30 12:03:40 +090094 int replaceUidOwnerMap(const std::string& name, bool isAllowlist,
Chenbo Feng89c12f12018-03-21 10:29:18 -070095 const std::vector<int32_t>& uids);
96
Chenbo Feng703798e2018-06-15 17:07:59 -070097 netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
Chenbo Feng84d9f9f2019-04-12 15:31:02 -070098 FirewallType type) EXCLUDES(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -070099
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700100 void dump(netdutils::DumpWriter& dw, bool verbose) EXCLUDES(mMutex);
Chenbo Fengef297172018-03-26 10:53:33 -0700101
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700102 netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids)
103 EXCLUDES(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700104
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700105 netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids)
106 EXCLUDES(mMutex);
107 netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex);
Rubin Xuec27ff22019-01-08 21:33:03 +0000108
Patrick Rohr79203972020-12-29 18:16:35 +0100109 netdutils::Status updateUidOwnerMap(const std::vector<uint32_t>& appStrUids,
Patrick Rohr7db39b92020-12-29 19:41:33 +0100110 UidOwnerMatchType matchType, BandwidthController::IptOp op)
111 EXCLUDES(mMutex);
Chenbo Fengef297172018-03-26 10:53:33 -0700112 static const String16 DUMP_KEYWORD;
113
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700114 int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700115
Chenbo Feng49586642018-08-30 18:01:53 -0700116 static netdutils::StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener();
117
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700118 void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800119
Chenbo Fengf2759682017-10-10 17:31:57 -0700120 private:
121 /*
122 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
Chenbo Fengef1cab32018-04-13 19:50:49 -0700123 * DO NOT hold any locks when modifying this map, otherwise when the untag
124 * operation is waiting for a lock hold by other process and there are more
125 * sockets being closed than can fit in the socket buffer of the netlink socket
126 * that receives them, then the kernel will drop some of these sockets and we
127 * won't delete their tags.
Chenbo Fengf2759682017-10-10 17:31:57 -0700128 * Map Key: uint64_t socket cookie
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800129 * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
Chenbo Fengf2759682017-10-10 17:31:57 -0700130 */
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800131 BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700132
133 /*
134 * mUidCounterSetMap: Store the counterSet of a specific uid.
135 * Map Key: uint32 uid.
136 * Map Value: uint32 counterSet specifies if the traffic is a background
137 * or foreground traffic.
138 */
Chenbo Fengf01f5302019-05-07 12:28:13 -0700139 BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700140
141 /*
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700142 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
143 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
144 * API to return persistent stats for a specific uid since device boot.
145 */
146 BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
147
148 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700149 * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
150 * combination of uid, tag, iface and counterSet. These two maps contain
151 * both tagged and untagged traffic.
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800152 * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
Chenbo Fengf434e862018-06-27 14:08:39 -0700153 * information.
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800154 * Map Value: Stats, contains packet count and byte count of each
Chenbo Fengf2759682017-10-10 17:31:57 -0700155 * transport protocol on egress and ingress direction.
156 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700157 BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700158
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700159 BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700160
Chenbo Feng7e974052018-02-28 22:57:21 -0800161 /*
162 * mIfaceIndexNameMap: Store the index name pair of each interface show up
163 * on the device since boot. The interface index is used by the eBPF program
164 * to correctly match the iface name when receiving a packet.
165 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700166 BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
Chenbo Feng7e974052018-02-28 22:57:21 -0800167
Chenbo Feng5ed17992018-03-13 21:30:49 -0700168 /*
169 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
170 * filter.
171 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700172 BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
Chenbo Feng5ed17992018-03-13 21:30:49 -0700173
Chenbo Feng89c12f12018-03-21 10:29:18 -0700174 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700175 * mConfigurationMap: Store the current network policy about uid filtering
176 * and the current stats map in use. There are two configuration entries in
177 * the map right now:
178 * - Entry with UID_RULES_CONFIGURATION_KEY:
179 * Store the configuration for the current uid rules. It indicates the device
Patrick Rohrfa0036f2020-12-02 16:22:28 +0100180 * is in doze/powersave/standby/restricted mode.
Chenbo Fengf434e862018-06-27 14:08:39 -0700181 * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
182 * Stores the current live stats map that kernel program is writing to.
183 * Userspace can do scraping and cleaning job on the other one depending on the
184 * current configs.
Chenbo Feng89c12f12018-03-21 10:29:18 -0700185 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700186 BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700187
Chenbo Feng95892f32018-06-07 14:52:02 -0700188 /*
Chenbo Feng703798e2018-06-15 17:07:59 -0700189 * mUidOwnerMap: Store uids that are used for bandwidth control uid match.
Chenbo Feng95892f32018-06-07 14:52:02 -0700190 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700191 BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex);
Chenbo Feng95892f32018-06-07 14:52:02 -0700192
Chenbo Feng48eaed32018-12-26 17:40:21 -0800193 /*
194 * mUidOwnerMap: Store uids that are used for INTERNET permission check.
195 */
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700196 BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800197
Chenbo Feng116d0552017-12-04 17:25:19 -0800198 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
199
Maciej Żenczykowskid2c23b42020-07-02 06:11:48 -0700200 netdutils::Status removeRule(uint32_t uid, UidOwnerMatchType match) REQUIRES(mMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700201
Maciej Żenczykowskid2c23b42020-07-02 06:11:48 -0700202 netdutils::Status addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif = 0)
203 REQUIRES(mMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700204
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700205 // mMutex guards all accesses to mConfigurationMap, mUidOwnerMap, mUidPermissionMap,
206 // mStatsMapA, mStatsMapB and mPrivilegedUser. It is designed to solve the following
207 // problems:
208 // 1. Prevent concurrent access and modification to mConfigurationMap, mUidOwnerMap,
209 // mUidPermissionMap, and mPrivilegedUser. These data members are controlled by netd but can
Chenbo Fengfd88f9d2019-05-01 12:59:09 -0700210 // be modified from different threads. TrafficController provides several APIs directly
211 // called by the binder RPC, and different binder threads can concurrently access these data
Chenbo Fengf01f5302019-05-07 12:28:13 -0700212 // members mentioned above. Some of the data members such as mUidPermissionMap and
213 // mPrivilegedUsers are also accessed from a different thread when tagging sockets or
214 // setting the counterSet through FwmarkServer
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700215 // 2. Coordinate the deletion of uid stats in mStatsMapA and mStatsMapB. The system server
216 // always call into netd to ask for a live stats map change before it pull and clean up the
217 // stats from the inactive map. The mMutex will block netd from accessing the stats map when
Chenbo Fengfd88f9d2019-05-01 12:59:09 -0700218 // the mConfigurationMap is updating the current stats map so netd will not accidentally
Chenbo Feng2fda2ed2019-04-19 12:34:41 -0700219 // read the map that system_server is cleaning up.
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700220 std::mutex mMutex;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700221
Chenbo Feng82075e52019-04-12 19:21:47 -0700222 // The limit on the number of stats entries a uid can have in the per uid stats map.
223 // TrafficController will block that specific uid from tagging new sockets after the limit is
224 // reached.
225 const uint32_t mPerUidStatsEntriesLimit;
226
227 // The limit on the total number of stats entries in the per uid stats map. TrafficController
228 // will block all tagging requests after the limit is reached.
229 const uint32_t mTotalUidStatsEntriesLimit;
230
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800231 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
232 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800233
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700234 netdutils::Status initMaps() EXCLUDES(mMutex);
Chenbo Feng95892f32018-06-07 14:52:02 -0700235
Chenbo Feng48eaed32018-12-26 17:40:21 -0800236 // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
237 // need to call back to system server for permission check.
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700238 std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex);
Chenbo Feng48eaed32018-12-26 17:40:21 -0800239
Chenbo Feng703798e2018-06-15 17:07:59 -0700240 UidOwnerMatchType jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling);
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800241
Chenbo Feng84d9f9f2019-04-12 15:31:02 -0700242 bool hasUpdateDeviceStatsPermission(uid_t uid) REQUIRES(mMutex);
Chenbo Feng82075e52019-04-12 19:21:47 -0700243
244 // For testing
245 TrafficController(uint32_t perUidLimit, uint32_t totalLimit);
246
Chenbo Fenged37fea2017-12-13 19:35:01 -0800247 // For testing
248 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700249};
250
251} // namespace net
252} // namespace android
253
254#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H