Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 1 | /* |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 2 | * Copyright (C) 2022 The Android Open Source Project |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 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 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 17 | #pragma once |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 18 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 19 | #include <set> |
| 20 | #include <Common.h> |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 21 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 22 | #include "android-base/thread_annotations.h" |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 23 | #include "bpf/BpfMap.h" |
| 24 | #include "bpf_shared.h" |
| 25 | #include "netdutils/DumpWriter.h" |
| 26 | #include "netdutils/NetlinkListener.h" |
| 27 | #include "netdutils/StatusOr.h" |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace net { |
| 31 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 32 | using netdutils::StatusOr; |
| 33 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 34 | class TrafficController { |
Patrick Rohr | 2b1b2c7 | 2022-02-01 15:57:48 +0100 | [diff] [blame] | 35 | public: |
| 36 | static constexpr char DUMP_KEYWORD[] = "trafficcontroller"; |
| 37 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 38 | /* |
| 39 | * Initialize the whole controller |
| 40 | */ |
| 41 | netdutils::Status start(); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 42 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 43 | /* |
| 44 | * Swap the stats map config from current active stats map to the idle one. |
| 45 | */ |
| 46 | netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex); |
| 47 | |
| 48 | /* |
| 49 | * Add the interface name and index pair into the eBPF map. |
| 50 | */ |
| 51 | int addInterface(const char* name, uint32_t ifaceIndex); |
| 52 | |
| 53 | int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type); |
| 54 | |
| 55 | int removeUidOwnerRule(const uid_t uid); |
| 56 | |
| 57 | int replaceUidOwnerMap(const std::string& name, bool isAllowlist, |
| 58 | const std::vector<int32_t>& uids); |
| 59 | |
| 60 | enum IptOp { IptOpInsert, IptOpDelete }; |
| 61 | |
| 62 | netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule, |
| 63 | FirewallType type) EXCLUDES(mMutex); |
| 64 | |
| 65 | void dump(netdutils::DumpWriter& dw, bool verbose) EXCLUDES(mMutex); |
| 66 | |
| 67 | netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids) |
| 68 | EXCLUDES(mMutex); |
| 69 | |
| 70 | netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids) |
| 71 | EXCLUDES(mMutex); |
| 72 | netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex); |
| 73 | |
Wayne Ma | a9716ff | 2022-01-12 10:37:04 +0800 | [diff] [blame] | 74 | netdutils::Status updateUidOwnerMap(const uint32_t uid, |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 75 | UidOwnerMatchType matchType, IptOp op) EXCLUDES(mMutex); |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 76 | |
| 77 | int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex); |
| 78 | |
| 79 | static netdutils::StatusOr<std::unique_ptr<netdutils::NetlinkListenerInterface>> |
| 80 | makeSkDestroyListener(); |
| 81 | |
| 82 | void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex); |
| 83 | |
| 84 | FirewallType getFirewallType(ChildChain); |
| 85 | |
| 86 | static const char* LOCAL_DOZABLE; |
| 87 | static const char* LOCAL_STANDBY; |
| 88 | static const char* LOCAL_POWERSAVE; |
| 89 | static const char* LOCAL_RESTRICTED; |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 90 | static const char* LOCAL_LOW_POWER_STANDBY; |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | /* |
| 94 | * mCookieTagMap: Store the corresponding tag and uid for a specific socket. |
| 95 | * DO NOT hold any locks when modifying this map, otherwise when the untag |
| 96 | * operation is waiting for a lock hold by other process and there are more |
| 97 | * sockets being closed than can fit in the socket buffer of the netlink socket |
| 98 | * that receives them, then the kernel will drop some of these sockets and we |
| 99 | * won't delete their tags. |
| 100 | * Map Key: uint64_t socket cookie |
| 101 | * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag. |
| 102 | */ |
| 103 | bpf::BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex); |
| 104 | |
| 105 | /* |
| 106 | * mUidCounterSetMap: Store the counterSet of a specific uid. |
| 107 | * Map Key: uint32 uid. |
| 108 | * Map Value: uint32 counterSet specifies if the traffic is a background |
| 109 | * or foreground traffic. |
| 110 | */ |
| 111 | bpf::BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex); |
| 112 | |
| 113 | /* |
| 114 | * mAppUidStatsMap: Store the total traffic stats for a uid regardless of |
| 115 | * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats |
| 116 | * API to return persistent stats for a specific uid since device boot. |
| 117 | */ |
| 118 | bpf::BpfMap<uint32_t, StatsValue> mAppUidStatsMap; |
| 119 | |
| 120 | /* |
| 121 | * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific |
| 122 | * combination of uid, tag, iface and counterSet. These two maps contain |
| 123 | * both tagged and untagged traffic. |
| 124 | * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex |
| 125 | * information. |
| 126 | * Map Value: Stats, contains packet count and byte count of each |
| 127 | * transport protocol on egress and ingress direction. |
| 128 | */ |
| 129 | bpf::BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex); |
| 130 | |
| 131 | bpf::BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex); |
| 132 | |
| 133 | /* |
| 134 | * mIfaceIndexNameMap: Store the index name pair of each interface show up |
| 135 | * on the device since boot. The interface index is used by the eBPF program |
| 136 | * to correctly match the iface name when receiving a packet. |
| 137 | */ |
| 138 | bpf::BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap; |
| 139 | |
| 140 | /* |
| 141 | * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf |
| 142 | * filter. |
| 143 | */ |
| 144 | bpf::BpfMap<uint32_t, StatsValue> mIfaceStatsMap; |
| 145 | |
| 146 | /* |
| 147 | * mConfigurationMap: Store the current network policy about uid filtering |
| 148 | * and the current stats map in use. There are two configuration entries in |
| 149 | * the map right now: |
| 150 | * - Entry with UID_RULES_CONFIGURATION_KEY: |
| 151 | * Store the configuration for the current uid rules. It indicates the device |
Robert Horvath | d945bf0 | 2022-01-27 19:55:16 +0100 | [diff] [blame] | 152 | * is in doze/powersave/standby/restricted/low power standby mode. |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 153 | * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY: |
| 154 | * Stores the current live stats map that kernel program is writing to. |
| 155 | * Userspace can do scraping and cleaning job on the other one depending on the |
| 156 | * current configs. |
| 157 | */ |
| 158 | bpf::BpfMap<uint32_t, uint8_t> mConfigurationMap GUARDED_BY(mMutex); |
| 159 | |
| 160 | /* |
| 161 | * mUidOwnerMap: Store uids that are used for bandwidth control uid match. |
| 162 | */ |
| 163 | bpf::BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex); |
| 164 | |
| 165 | /* |
| 166 | * mUidOwnerMap: Store uids that are used for INTERNET permission check. |
| 167 | */ |
| 168 | bpf::BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex); |
| 169 | |
| 170 | std::unique_ptr<netdutils::NetlinkListenerInterface> mSkDestroyListener; |
| 171 | |
| 172 | netdutils::Status removeRule(uint32_t uid, UidOwnerMatchType match) REQUIRES(mMutex); |
| 173 | |
| 174 | netdutils::Status addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif = 0) |
| 175 | REQUIRES(mMutex); |
| 176 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 177 | std::mutex mMutex; |
| 178 | |
Patrick Rohr | 2b1b2c7 | 2022-02-01 15:57:48 +0100 | [diff] [blame] | 179 | netdutils::Status initMaps() EXCLUDES(mMutex); |
| 180 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 181 | // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't |
| 182 | // need to call back to system server for permission check. |
| 183 | std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex); |
| 184 | |
| 185 | bool hasUpdateDeviceStatsPermission(uid_t uid) REQUIRES(mMutex); |
| 186 | |
Wayne Ma | 4d69233 | 2022-01-19 16:04:04 +0800 | [diff] [blame] | 187 | // For testing |
| 188 | friend class TrafficControllerTest; |
| 189 | }; |
| 190 | |
| 191 | } // namespace net |
| 192 | } // namespace android |