blob: d30749b3ae6127c5799a264c9ac0da49f1aed31a [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;
35using android::bpf::IfaceValue;
36using android::bpf::StatsKey;
37using android::bpf::StatsValue;
38using android::bpf::UidTag;
Chenbo Fengf2759682017-10-10 17:31:57 -070039
40namespace android {
41namespace net {
42
Chenbo Fengf2759682017-10-10 17:31:57 -070043class TrafficController {
44 public:
Chenbo Feng89c12f12018-03-21 10:29:18 -070045 TrafficController();
Chenbo Fengf2759682017-10-10 17:31:57 -070046 /*
47 * Initialize the whole controller
48 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080049 netdutils::Status start();
Chenbo Fengf2759682017-10-10 17:31:57 -070050 /*
51 * Tag the socket with the specified tag and uid. In the qtaguid module, the
52 * first tag request that grab the spinlock of rb_tree can update the tag
53 * information first and other request need to wait until it finish. All the
54 * tag request will be addressed in the order of they obtaining the spinlock.
55 * In the eBPF implementation, the kernel will try to update the eBPF map
56 * entry with the tag request. And the hashmap update process is protected by
57 * the spinlock initialized with the map. So the behavior of two modules
58 * should be the same. No additional lock needed.
59 */
Chenbo Fengb4a4fa12019-01-09 17:20:45 -080060 int tagSocket(int sockFd, uint32_t tag, uid_t uid, uid_t callingUid);
Chenbo Fengf2759682017-10-10 17:31:57 -070061
62 /*
63 * The untag process is similiar to tag socket and both old qtaguid module and
64 * new eBPF module have spinlock inside the kernel for concurrent update. No
65 * external lock is required.
66 */
67 int untagSocket(int sockFd);
68
69 /*
70 * Similiar as above, no external lock required.
71 */
Chenbo Fengb4a4fa12019-01-09 17:20:45 -080072 int setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid);
Chenbo Fengf2759682017-10-10 17:31:57 -070073
74 /*
75 * When deleting a tag data, the qtaguid module will grab the spinlock of each
76 * related rb_tree one by one and delete the tag information, counterSet
77 * information, iface stats information and uid stats information one by one.
78 * The new eBPF implementation is done similiarly by removing the entry on
79 * each map one by one. And deleting processes are also protected by the
80 * spinlock of the map. So no additional lock is required.
81 */
Chenbo Fengb4a4fa12019-01-09 17:20:45 -080082 int deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid);
Chenbo Fengf2759682017-10-10 17:31:57 -070083
Chenbo Feng07d43fe2017-12-21 14:38:51 -080084 /*
85 * Check if the current device have the bpf traffic stats accounting service
86 * running.
87 */
Chenbo Feng47dd0732018-12-11 12:23:24 -080088 bpf::BpfLevel getBpfLevel();
Chenbo Feng07d43fe2017-12-21 14:38:51 -080089
Chenbo Feng7e974052018-02-28 22:57:21 -080090 /*
Chenbo Feng873ae142019-04-10 12:26:06 -070091 * Swap the stats map config from current active stats map to the idle one.
92 */
93 netdutils::Status swapActiveStatsMap();
94
95 /*
Chenbo Feng7e974052018-02-28 22:57:21 -080096 * Add the interface name and index pair into the eBPF map.
97 */
98 int addInterface(const char* name, uint32_t ifaceIndex);
99
Chenbo Feng89c12f12018-03-21 10:29:18 -0700100 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
101
102 int removeUidOwnerRule(const uid_t uid);
103
104 int replaceUidOwnerMap(const std::string& name, bool isWhitelist,
105 const std::vector<int32_t>& uids);
106
Chenbo Feng703798e2018-06-15 17:07:59 -0700107 netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
108 FirewallType type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700109
Luke Huangb257d612019-03-14 21:19:13 +0800110 void dump(netdutils::DumpWriter& dw, bool verbose);
Chenbo Fengef297172018-03-26 10:53:33 -0700111
Rubin Xu5ca8e802019-04-05 11:50:32 +0100112 netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700113
Rubin Xuec27ff22019-01-08 21:33:03 +0000114 netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids);
115 netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids);
116
Chenbo Feng703798e2018-06-15 17:07:59 -0700117 netdutils::Status updateUidOwnerMap(const std::vector<std::string>& appStrUids,
118 BandwidthController::IptJumpOp jumpHandling,
119 BandwidthController::IptOp op);
Chenbo Fengef297172018-03-26 10:53:33 -0700120 static const String16 DUMP_KEYWORD;
121
Chenbo Feng89c12f12018-03-21 10:29:18 -0700122 int toggleUidOwnerMap(ChildChain chain, bool enable);
123
Chenbo Feng49586642018-08-30 18:01:53 -0700124 static netdutils::StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener();
125
Chenbo Feng48eaed32018-12-26 17:40:21 -0800126 void setPermissionForUids(int permission, const std::vector<uid_t>& uids);
127
Chenbo Fengf2759682017-10-10 17:31:57 -0700128 private:
129 /*
130 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
Chenbo Fengef1cab32018-04-13 19:50:49 -0700131 * DO NOT hold any locks when modifying this map, otherwise when the untag
132 * operation is waiting for a lock hold by other process and there are more
133 * sockets being closed than can fit in the socket buffer of the netlink socket
134 * that receives them, then the kernel will drop some of these sockets and we
135 * won't delete their tags.
Chenbo Fengf2759682017-10-10 17:31:57 -0700136 * Map Key: uint64_t socket cookie
137 * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
138 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700139 BpfMap<uint64_t, UidTag> mCookieTagMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700140
141 /*
142 * mUidCounterSetMap: Store the counterSet of a specific uid.
143 * Map Key: uint32 uid.
144 * Map Value: uint32 counterSet specifies if the traffic is a background
145 * or foreground traffic.
146 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700147 BpfMap<uint32_t, uint8_t> mUidCounterSetMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700148
149 /*
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700150 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
151 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
152 * API to return persistent stats for a specific uid since device boot.
153 */
154 BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
155
156 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700157 * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
158 * combination of uid, tag, iface and counterSet. These two maps contain
159 * both tagged and untagged traffic.
160 * Map Key: Struct StatsKey contains the uid, tag, counterSet and ifaceIndex
161 * information.
Chenbo Fengf2759682017-10-10 17:31:57 -0700162 * Map Value: struct Stats, contains packet count and byte count of each
163 * transport protocol on egress and ingress direction.
164 */
Chenbo Fengf434e862018-06-27 14:08:39 -0700165 BpfMap<StatsKey, StatsValue> mStatsMapA;
Chenbo Fengf2759682017-10-10 17:31:57 -0700166
Chenbo Fengf434e862018-06-27 14:08:39 -0700167 BpfMap<StatsKey, StatsValue> mStatsMapB;
Chenbo Fengf2759682017-10-10 17:31:57 -0700168
Chenbo Feng7e974052018-02-28 22:57:21 -0800169 /*
170 * mIfaceIndexNameMap: Store the index name pair of each interface show up
171 * on the device since boot. The interface index is used by the eBPF program
172 * to correctly match the iface name when receiving a packet.
173 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700174 BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
Chenbo Feng7e974052018-02-28 22:57:21 -0800175
Chenbo Feng5ed17992018-03-13 21:30:49 -0700176 /*
177 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
178 * filter.
179 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700180 BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
Chenbo Feng5ed17992018-03-13 21:30:49 -0700181
Chenbo Feng89c12f12018-03-21 10:29:18 -0700182 /*
Chenbo Fengf434e862018-06-27 14:08:39 -0700183 * mConfigurationMap: Store the current network policy about uid filtering
184 * and the current stats map in use. There are two configuration entries in
185 * the map right now:
186 * - Entry with UID_RULES_CONFIGURATION_KEY:
187 * Store the configuration for the current uid rules. It indicates the device
188 * is in doze/powersave/standby mode.
189 * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
190 * Stores the current live stats map that kernel program is writing to.
191 * Userspace can do scraping and cleaning job on the other one depending on the
192 * current configs.
Chenbo Feng89c12f12018-03-21 10:29:18 -0700193 */
Chenbo Feng703798e2018-06-15 17:07:59 -0700194 BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700195
Chenbo Feng95892f32018-06-07 14:52:02 -0700196 /*
Chenbo Feng703798e2018-06-15 17:07:59 -0700197 * mUidOwnerMap: Store uids that are used for bandwidth control uid match.
Chenbo Feng95892f32018-06-07 14:52:02 -0700198 */
Rubin Xuec27ff22019-01-08 21:33:03 +0000199 BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mOwnerMatchMutex);
Chenbo Feng95892f32018-06-07 14:52:02 -0700200
Chenbo Feng48eaed32018-12-26 17:40:21 -0800201 /*
202 * mUidOwnerMap: Store uids that are used for INTERNET permission check.
203 */
204 BpfMap<uint32_t, uint8_t> mUidPermissionMap;
205
Chenbo Feng116d0552017-12-04 17:25:19 -0800206 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
207
Rubin Xu5ca8e802019-04-05 11:50:32 +0100208 netdutils::Status removeRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
209 UidOwnerMatchType match) REQUIRES(mOwnerMatchMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700210
Rubin Xu5ca8e802019-04-05 11:50:32 +0100211 netdutils::Status addRule(BpfMap<uint32_t, UidOwnerValue>& map, uint32_t uid,
212 UidOwnerMatchType match, uint32_t iif = 0) REQUIRES(mOwnerMatchMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700213
Chenbo Feng47dd0732018-12-11 12:23:24 -0800214 bpf::BpfLevel mBpfLevel;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800215
Chenbo Feng89c12f12018-03-21 10:29:18 -0700216 std::mutex mOwnerMatchMutex;
217
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800218 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
219 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800220
Chenbo Feng89c12f12018-03-21 10:29:18 -0700221 netdutils::Status initMaps();
Chenbo Feng95892f32018-06-07 14:52:02 -0700222
Chenbo Feng48eaed32018-12-26 17:40:21 -0800223 // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
224 // need to call back to system server for permission check.
225 std::set<uid_t> mPrivilegedUser;
226
Chenbo Feng703798e2018-06-15 17:07:59 -0700227 UidOwnerMatchType jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling);
Chenbo Fengb4a4fa12019-01-09 17:20:45 -0800228
229 bool hasUpdateDeviceStatsPermission(uid_t uid);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800230 // For testing
231 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700232};
233
234} // namespace net
235} // namespace android
236
237#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H