Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [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 | |
| 22 | #include "Network.h" |
| 23 | #include "android-base/thread_annotations.h" |
| 24 | #include "android-base/unique_fd.h" |
| 25 | #include "bpf/BpfMap.h" |
| 26 | #include "bpf_shared.h" |
| 27 | #include "netdutils/DumpWriter.h" |
| 28 | #include "netdutils/NetlinkListener.h" |
| 29 | #include "netdutils/StatusOr.h" |
| 30 | #include "utils/String16.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace net { |
| 34 | |
| 35 | class TrafficController { |
| 36 | public: |
| 37 | TrafficController(); |
| 38 | /* |
| 39 | * Initialize the whole controller |
| 40 | */ |
| 41 | netdutils::Status start(); |
| 42 | /* |
| 43 | * Tag the socket with the specified tag and uid. In the qtaguid module, the |
| 44 | * first tag request that grab the spinlock of rb_tree can update the tag |
| 45 | * information first and other request need to wait until it finish. All the |
| 46 | * tag request will be addressed in the order of they obtaining the spinlock. |
| 47 | * In the eBPF implementation, the kernel will try to update the eBPF map |
| 48 | * entry with the tag request. And the hashmap update process is protected by |
| 49 | * the spinlock initialized with the map. So the behavior of two modules |
| 50 | * should be the same. No additional lock needed. |
| 51 | */ |
| 52 | int tagSocket(int sockFd, uint32_t tag, uid_t uid, uid_t callingUid) EXCLUDES(mMutex); |
| 53 | |
| 54 | /* |
| 55 | * Similar as tagSocket, but skip UPDATE_DEVICE_STATS permission check. |
| 56 | */ |
| 57 | int privilegedTagSocket(int sockFd, uint32_t tag, uid_t uid) EXCLUDES(mMutex); |
| 58 | |
| 59 | /* |
| 60 | * The untag process is similiar to tag socket and both old qtaguid module and |
| 61 | * new eBPF module have spinlock inside the kernel for concurrent update. No |
| 62 | * external lock is required. |
| 63 | */ |
| 64 | int untagSocket(int sockFd); |
| 65 | |
| 66 | /* |
| 67 | * Similiar as above, no external lock required. |
| 68 | */ |
| 69 | int setCounterSet(int counterSetNum, uid_t uid, uid_t callingUid) EXCLUDES(mMutex); |
| 70 | |
| 71 | /* |
| 72 | * When deleting a tag data, the qtaguid module will grab the spinlock of each |
| 73 | * related rb_tree one by one and delete the tag information, counterSet |
| 74 | * information, iface stats information and uid stats information one by one. |
| 75 | * The new eBPF implementation is done similiarly by removing the entry on |
| 76 | * each map one by one. And deleting processes are also protected by the |
| 77 | * spinlock of the map. So no additional lock is required. |
| 78 | */ |
| 79 | int deleteTagData(uint32_t tag, uid_t uid, uid_t callingUid) EXCLUDES(mMutex); |
| 80 | |
| 81 | /* |
| 82 | * Swap the stats map config from current active stats map to the idle one. |
| 83 | */ |
| 84 | netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex); |
| 85 | |
| 86 | /* |
| 87 | * Add the interface name and index pair into the eBPF map. |
| 88 | */ |
| 89 | int addInterface(const char* name, uint32_t ifaceIndex); |
| 90 | |
| 91 | int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type); |
| 92 | |
| 93 | int removeUidOwnerRule(const uid_t uid); |
| 94 | |
| 95 | int replaceUidOwnerMap(const std::string& name, bool isAllowlist, |
| 96 | const std::vector<int32_t>& uids); |
| 97 | |
| 98 | enum IptOp { IptOpInsert, IptOpDelete }; |
| 99 | |
| 100 | netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule, |
| 101 | FirewallType type) EXCLUDES(mMutex); |
| 102 | |
| 103 | void dump(netdutils::DumpWriter& dw, bool verbose) EXCLUDES(mMutex); |
| 104 | |
| 105 | netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids) |
| 106 | EXCLUDES(mMutex); |
| 107 | |
| 108 | netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids) |
| 109 | EXCLUDES(mMutex); |
| 110 | netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex); |
| 111 | |
| 112 | netdutils::Status updateUidOwnerMap(const std::vector<uint32_t>& appStrUids, |
| 113 | UidOwnerMatchType matchType, IptOp op) EXCLUDES(mMutex); |
| 114 | static const String16 DUMP_KEYWORD; |
| 115 | |
| 116 | int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex); |
| 117 | |
| 118 | static netdutils::StatusOr<std::unique_ptr<netdutils::NetlinkListenerInterface>> |
| 119 | makeSkDestroyListener(); |
| 120 | |
| 121 | void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex); |
| 122 | |
| 123 | FirewallType getFirewallType(ChildChain); |
| 124 | |
| 125 | static const char* LOCAL_DOZABLE; |
| 126 | static const char* LOCAL_STANDBY; |
| 127 | static const char* LOCAL_POWERSAVE; |
| 128 | static const char* LOCAL_RESTRICTED; |
| 129 | |
| 130 | private: |
| 131 | /* |
| 132 | * mCookieTagMap: Store the corresponding tag and uid for a specific socket. |
| 133 | * DO NOT hold any locks when modifying this map, otherwise when the untag |
| 134 | * operation is waiting for a lock hold by other process and there are more |
| 135 | * sockets being closed than can fit in the socket buffer of the netlink socket |
| 136 | * that receives them, then the kernel will drop some of these sockets and we |
| 137 | * won't delete their tags. |
| 138 | * Map Key: uint64_t socket cookie |
| 139 | * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag. |
| 140 | */ |
| 141 | bpf::BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex); |
| 142 | |
| 143 | /* |
| 144 | * mUidCounterSetMap: Store the counterSet of a specific uid. |
| 145 | * Map Key: uint32 uid. |
| 146 | * Map Value: uint32 counterSet specifies if the traffic is a background |
| 147 | * or foreground traffic. |
| 148 | */ |
| 149 | bpf::BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex); |
| 150 | |
| 151 | /* |
| 152 | * mAppUidStatsMap: Store the total traffic stats for a uid regardless of |
| 153 | * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats |
| 154 | * API to return persistent stats for a specific uid since device boot. |
| 155 | */ |
| 156 | bpf::BpfMap<uint32_t, StatsValue> mAppUidStatsMap; |
| 157 | |
| 158 | /* |
| 159 | * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific |
| 160 | * combination of uid, tag, iface and counterSet. These two maps contain |
| 161 | * both tagged and untagged traffic. |
| 162 | * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex |
| 163 | * information. |
| 164 | * Map Value: Stats, contains packet count and byte count of each |
| 165 | * transport protocol on egress and ingress direction. |
| 166 | */ |
| 167 | bpf::BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex); |
| 168 | |
| 169 | bpf::BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex); |
| 170 | |
| 171 | /* |
| 172 | * mIfaceIndexNameMap: Store the index name pair of each interface show up |
| 173 | * on the device since boot. The interface index is used by the eBPF program |
| 174 | * to correctly match the iface name when receiving a packet. |
| 175 | */ |
| 176 | bpf::BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap; |
| 177 | |
| 178 | /* |
| 179 | * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf |
| 180 | * filter. |
| 181 | */ |
| 182 | bpf::BpfMap<uint32_t, StatsValue> mIfaceStatsMap; |
| 183 | |
| 184 | /* |
| 185 | * mConfigurationMap: Store the current network policy about uid filtering |
| 186 | * and the current stats map in use. There are two configuration entries in |
| 187 | * the map right now: |
| 188 | * - Entry with UID_RULES_CONFIGURATION_KEY: |
| 189 | * Store the configuration for the current uid rules. It indicates the device |
| 190 | * is in doze/powersave/standby/restricted mode. |
| 191 | * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY: |
| 192 | * Stores the current live stats map that kernel program is writing to. |
| 193 | * Userspace can do scraping and cleaning job on the other one depending on the |
| 194 | * current configs. |
| 195 | */ |
| 196 | bpf::BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mMutex); |
| 197 | |
| 198 | /* |
| 199 | * mUidOwnerMap: Store uids that are used for bandwidth control uid match. |
| 200 | */ |
| 201 | bpf::BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex); |
| 202 | |
| 203 | /* |
| 204 | * mUidOwnerMap: Store uids that are used for INTERNET permission check. |
| 205 | */ |
| 206 | bpf::BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex); |
| 207 | |
| 208 | std::unique_ptr<netdutils::NetlinkListenerInterface> mSkDestroyListener; |
| 209 | |
| 210 | netdutils::Status removeRule(uint32_t uid, UidOwnerMatchType match) REQUIRES(mMutex); |
| 211 | |
| 212 | netdutils::Status addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif = 0) |
| 213 | REQUIRES(mMutex); |
| 214 | |
| 215 | // mMutex guards all accesses to mConfigurationMap, mUidOwnerMap, mUidPermissionMap, |
| 216 | // mStatsMapA, mStatsMapB and mPrivilegedUser. It is designed to solve the following |
| 217 | // problems: |
| 218 | // 1. Prevent concurrent access and modification to mConfigurationMap, mUidOwnerMap, |
| 219 | // mUidPermissionMap, and mPrivilegedUser. These data members are controlled by netd but can |
| 220 | // be modified from different threads. TrafficController provides several APIs directly |
| 221 | // called by the binder RPC, and different binder threads can concurrently access these data |
| 222 | // members mentioned above. Some of the data members such as mUidPermissionMap and |
| 223 | // mPrivilegedUsers are also accessed from a different thread when tagging sockets or |
| 224 | // setting the counterSet through FwmarkServer |
| 225 | // 2. Coordinate the deletion of uid stats in mStatsMapA and mStatsMapB. The system server |
| 226 | // always call into netd to ask for a live stats map change before it pull and clean up the |
| 227 | // stats from the inactive map. The mMutex will block netd from accessing the stats map when |
| 228 | // the mConfigurationMap is updating the current stats map so netd will not accidentally |
| 229 | // read the map that system_server is cleaning up. |
| 230 | std::mutex mMutex; |
| 231 | |
| 232 | // The limit on the number of stats entries a uid can have in the per uid stats map. |
| 233 | // TrafficController will block that specific uid from tagging new sockets after the limit is |
| 234 | // reached. |
| 235 | const uint32_t mPerUidStatsEntriesLimit; |
| 236 | |
| 237 | // The limit on the total number of stats entries in the per uid stats map. TrafficController |
| 238 | // will block all tagging requests after the limit is reached. |
| 239 | const uint32_t mTotalUidStatsEntriesLimit; |
| 240 | |
| 241 | netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name, |
| 242 | base::unique_fd& cg_fd); |
| 243 | |
| 244 | netdutils::Status initMaps() EXCLUDES(mMutex); |
| 245 | |
| 246 | // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't |
| 247 | // need to call back to system server for permission check. |
| 248 | std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex); |
| 249 | |
| 250 | bool hasUpdateDeviceStatsPermission(uid_t uid) REQUIRES(mMutex); |
| 251 | |
| 252 | int privilegedTagSocketLocked(int sockFd, uint32_t tag, uid_t uid) REQUIRES(mMutex); |
| 253 | |
| 254 | // For testing |
| 255 | TrafficController(uint32_t perUidLimit, uint32_t totalLimit); |
| 256 | |
| 257 | // For testing |
| 258 | friend class TrafficControllerTest; |
| 259 | }; |
| 260 | |
| 261 | } // namespace net |
| 262 | } // namespace android |
| 263 | |
| 264 | #endif // NETD_SERVER_TRAFFIC_CONTROLLER_H |