Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 22 | #include "BandwidthController.h" |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 23 | #include "FirewallController.h" |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 24 | #include "NetlinkListener.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 25 | #include "Network.h" |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 26 | #include "android-base/thread_annotations.h" |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 27 | #include "android-base/unique_fd.h" |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 28 | #include "bpf/BpfMap.h" |
Chenbo Feng | d6104d1 | 2018-10-16 20:29:29 -0700 | [diff] [blame] | 29 | #include "netdbpf/bpf_shared.h" |
Bernie Innocenti | 97f388f | 2018-10-16 19:17:08 +0900 | [diff] [blame] | 30 | #include "netdutils/StatusOr.h" |
| 31 | #include "utils/String16.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 32 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 33 | using android::bpf::BpfMap; |
| 34 | using android::bpf::IfaceValue; |
| 35 | using android::bpf::StatsKey; |
| 36 | using android::bpf::StatsValue; |
| 37 | using android::bpf::UidTag; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace net { |
| 41 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 42 | class DumpWriter; |
| 43 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 44 | class TrafficController { |
| 45 | public: |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 46 | TrafficController(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Initialize the whole controller |
| 49 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 50 | netdutils::Status start(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Tag the socket with the specified tag and uid. In the qtaguid module, the |
| 53 | * first tag request that grab the spinlock of rb_tree can update the tag |
| 54 | * information first and other request need to wait until it finish. All the |
| 55 | * tag request will be addressed in the order of they obtaining the spinlock. |
| 56 | * In the eBPF implementation, the kernel will try to update the eBPF map |
| 57 | * entry with the tag request. And the hashmap update process is protected by |
| 58 | * the spinlock initialized with the map. So the behavior of two modules |
| 59 | * should be the same. No additional lock needed. |
| 60 | */ |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame^] | 61 | int tagSocket(int sockFd, uint32_t tag, uid_t uid, uid_t callingUid); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * The untag process is similiar to tag socket and both old qtaguid module and |
| 65 | * new eBPF module have spinlock inside the kernel for concurrent update. No |
| 66 | * external lock is required. |
| 67 | */ |
| 68 | int untagSocket(int sockFd); |
| 69 | |
| 70 | /* |
| 71 | * Similiar as above, no external lock required. |
| 72 | */ |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame^] | 73 | int setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * When deleting a tag data, the qtaguid module will grab the spinlock of each |
| 77 | * related rb_tree one by one and delete the tag information, counterSet |
| 78 | * information, iface stats information and uid stats information one by one. |
| 79 | * The new eBPF implementation is done similiarly by removing the entry on |
| 80 | * each map one by one. And deleting processes are also protected by the |
| 81 | * spinlock of the map. So no additional lock is required. |
| 82 | */ |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame^] | 83 | int deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 84 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 85 | /* |
| 86 | * Check if the current device have the bpf traffic stats accounting service |
| 87 | * running. |
| 88 | */ |
| 89 | bool checkBpfStatsEnable(); |
| 90 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Add the interface name and index pair into the eBPF map. |
| 93 | */ |
| 94 | int addInterface(const char* name, uint32_t ifaceIndex); |
| 95 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 96 | 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 Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 103 | netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule, |
| 104 | FirewallType type); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 105 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 106 | void dump(DumpWriter& dw, bool verbose); |
| 107 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 108 | netdutils::Status replaceUidsInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 109 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 110 | netdutils::Status updateUidOwnerMap(const std::vector<std::string>& appStrUids, |
| 111 | BandwidthController::IptJumpOp jumpHandling, |
| 112 | BandwidthController::IptOp op); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 113 | static const String16 DUMP_KEYWORD; |
| 114 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 115 | int toggleUidOwnerMap(ChildChain chain, bool enable); |
| 116 | |
Chenbo Feng | 4958664 | 2018-08-30 18:01:53 -0700 | [diff] [blame] | 117 | static netdutils::StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener(); |
| 118 | |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 119 | void setPermissionForUids(int permission, const std::vector<uid_t>& uids); |
| 120 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 121 | private: |
| 122 | /* |
| 123 | * mCookieTagMap: Store the corresponding tag and uid for a specific socket. |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 124 | * DO NOT hold any locks when modifying this map, otherwise when the untag |
| 125 | * operation is waiting for a lock hold by other process and there are more |
| 126 | * sockets being closed than can fit in the socket buffer of the netlink socket |
| 127 | * that receives them, then the kernel will drop some of these sockets and we |
| 128 | * won't delete their tags. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 129 | * Map Key: uint64_t socket cookie |
| 130 | * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag. |
| 131 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 132 | BpfMap<uint64_t, UidTag> mCookieTagMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * mUidCounterSetMap: Store the counterSet of a specific uid. |
| 136 | * Map Key: uint32 uid. |
| 137 | * Map Value: uint32 counterSet specifies if the traffic is a background |
| 138 | * or foreground traffic. |
| 139 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 140 | BpfMap<uint32_t, uint8_t> mUidCounterSetMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 143 | * mAppUidStatsMap: Store the total traffic stats for a uid regardless of |
| 144 | * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats |
| 145 | * API to return persistent stats for a specific uid since device boot. |
| 146 | */ |
| 147 | BpfMap<uint32_t, StatsValue> mAppUidStatsMap; |
| 148 | |
| 149 | /* |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 150 | * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific |
| 151 | * combination of uid, tag, iface and counterSet. These two maps contain |
| 152 | * both tagged and untagged traffic. |
| 153 | * Map Key: Struct StatsKey contains the uid, tag, counterSet and ifaceIndex |
| 154 | * information. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 155 | * Map Value: struct Stats, contains packet count and byte count of each |
| 156 | * transport protocol on egress and ingress direction. |
| 157 | */ |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 158 | BpfMap<StatsKey, StatsValue> mStatsMapA; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 159 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 160 | BpfMap<StatsKey, StatsValue> mStatsMapB; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 161 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 162 | /* |
| 163 | * mIfaceIndexNameMap: Store the index name pair of each interface show up |
| 164 | * on the device since boot. The interface index is used by the eBPF program |
| 165 | * to correctly match the iface name when receiving a packet. |
| 166 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 167 | BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 168 | |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 169 | /* |
| 170 | * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf |
| 171 | * filter. |
| 172 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 173 | BpfMap<uint32_t, StatsValue> mIfaceStatsMap; |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 174 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 175 | /* |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 176 | * mConfigurationMap: Store the current network policy about uid filtering |
| 177 | * and the current stats map in use. There are two configuration entries in |
| 178 | * the map right now: |
| 179 | * - Entry with UID_RULES_CONFIGURATION_KEY: |
| 180 | * Store the configuration for the current uid rules. It indicates the device |
| 181 | * is in doze/powersave/standby mode. |
| 182 | * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY: |
| 183 | * Stores the current live stats map that kernel program is writing to. |
| 184 | * Userspace can do scraping and cleaning job on the other one depending on the |
| 185 | * current configs. |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 186 | */ |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 187 | BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 188 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 189 | /* |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 190 | * mUidOwnerMap: Store uids that are used for bandwidth control uid match. |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 191 | */ |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 192 | BpfMap<uint32_t, uint8_t> mUidOwnerMap GUARDED_BY(mOwnerMatchMutex); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 193 | |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 194 | /* |
| 195 | * mUidOwnerMap: Store uids that are used for INTERNET permission check. |
| 196 | */ |
| 197 | BpfMap<uint32_t, uint8_t> mUidPermissionMap; |
| 198 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 199 | std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener; |
| 200 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 201 | netdutils::Status removeMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid, |
| 202 | UidOwnerMatchType match) REQUIRES(mOwnerMatchMutex); |
| 203 | |
| 204 | netdutils::Status addMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid, |
| 205 | UidOwnerMatchType match) REQUIRES(mOwnerMatchMutex); |
| 206 | |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 207 | bool ebpfSupported; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 208 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 209 | std::mutex mOwnerMatchMutex; |
| 210 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 211 | netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name, |
| 212 | base::unique_fd& cg_fd); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 213 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 214 | netdutils::Status initMaps(); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 215 | |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 216 | // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't |
| 217 | // need to call back to system server for permission check. |
| 218 | std::set<uid_t> mPrivilegedUser; |
| 219 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 220 | UidOwnerMatchType jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling); |
Chenbo Feng | b4a4fa1 | 2019-01-09 17:20:45 -0800 | [diff] [blame^] | 221 | |
| 222 | bool hasUpdateDeviceStatsPermission(uid_t uid); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 223 | // For testing |
| 224 | friend class TrafficControllerTest; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | } // namespace net |
| 228 | } // namespace android |
| 229 | |
| 230 | #endif // NETD_SERVER_TRAFFIC_CONTROLLER_H |