blob: 86cf50a51cbca027e10084fcee020cfbcecadb92 [file] [log] [blame]
Wayne Ma4d692332022-01-19 16:04:04 +08001/*
Wayne Maa9716ff2022-01-12 10:37:04 +08002 * Copyright (C) 2022 The Android Open Source Project
Wayne Ma4d692332022-01-19 16:04:04 +08003 *
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 Maa9716ff2022-01-12 10:37:04 +080017#pragma once
Wayne Ma4d692332022-01-19 16:04:04 +080018
Wayne Maa9716ff2022-01-12 10:37:04 +080019#include <set>
20#include <Common.h>
Wayne Ma4d692332022-01-19 16:04:04 +080021
Wayne Ma4d692332022-01-19 16:04:04 +080022#include "android-base/thread_annotations.h"
Wayne Ma4d692332022-01-19 16:04:04 +080023#include "bpf/BpfMap.h"
Maciej Żenczykowski513474c2022-12-08 16:20:43 +000024#include "netd.h"
Wayne Ma4d692332022-01-19 16:04:04 +080025#include "netdutils/NetlinkListener.h"
26#include "netdutils/StatusOr.h"
Wayne Ma4d692332022-01-19 16:04:04 +080027
28namespace android {
29namespace net {
30
Wayne Maa9716ff2022-01-12 10:37:04 +080031using netdutils::StatusOr;
32
Wayne Ma4d692332022-01-19 16:04:04 +080033class TrafficController {
Patrick Rohr2b1b2c72022-02-01 15:57:48 +010034 public:
Wayne Ma4d692332022-01-19 16:04:04 +080035 /*
36 * Initialize the whole controller
37 */
Motomu Utsumi3af8f0e2022-09-02 23:42:13 +090038 netdutils::Status start(bool startSkDestroyListener);
Wayne Ma4d692332022-01-19 16:04:04 +080039
Wayne Ma4d692332022-01-19 16:04:04 +080040 /*
41 * Swap the stats map config from current active stats map to the idle one.
42 */
43 netdutils::Status swapActiveStatsMap() EXCLUDES(mMutex);
44
Wayne Ma4d692332022-01-19 16:04:04 +080045 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
46
47 int removeUidOwnerRule(const uid_t uid);
48
49 int replaceUidOwnerMap(const std::string& name, bool isAllowlist,
50 const std::vector<int32_t>& uids);
51
52 enum IptOp { IptOpInsert, IptOpDelete };
53
54 netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
55 FirewallType type) EXCLUDES(mMutex);
56
Wayne Ma4d692332022-01-19 16:04:04 +080057 netdutils::Status replaceRulesInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids)
58 EXCLUDES(mMutex);
59
60 netdutils::Status addUidInterfaceRules(const int ifIndex, const std::vector<int32_t>& uids)
61 EXCLUDES(mMutex);
62 netdutils::Status removeUidInterfaceRules(const std::vector<int32_t>& uids) EXCLUDES(mMutex);
63
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +000064 netdutils::Status updateUidLockdownRule(const uid_t uid, const bool add) EXCLUDES(mMutex);
65
Wayne Maa9716ff2022-01-12 10:37:04 +080066 netdutils::Status updateUidOwnerMap(const uint32_t uid,
Wayne Ma4d692332022-01-19 16:04:04 +080067 UidOwnerMatchType matchType, IptOp op) EXCLUDES(mMutex);
Wayne Ma4d692332022-01-19 16:04:04 +080068
Motomu Utsumi114cd9c2022-08-01 02:08:35 +000069 int toggleUidOwnerMap(ChildChain chain, bool enable) EXCLUDES(mMutex);
70
Wayne Ma4d692332022-01-19 16:04:04 +080071 static netdutils::StatusOr<std::unique_ptr<netdutils::NetlinkListenerInterface>>
72 makeSkDestroyListener();
73
74 void setPermissionForUids(int permission, const std::vector<uid_t>& uids) EXCLUDES(mMutex);
75
76 FirewallType getFirewallType(ChildChain);
77
78 static const char* LOCAL_DOZABLE;
79 static const char* LOCAL_STANDBY;
80 static const char* LOCAL_POWERSAVE;
81 static const char* LOCAL_RESTRICTED;
Robert Horvathd945bf02022-01-27 19:55:16 +010082 static const char* LOCAL_LOW_POWER_STANDBY;
Motomu Utsumid9801492022-06-01 13:57:27 +000083 static const char* LOCAL_OEM_DENY_1;
84 static const char* LOCAL_OEM_DENY_2;
Motomu Utsumi1d9054b2022-06-06 07:44:05 +000085 static const char* LOCAL_OEM_DENY_3;
Wayne Ma4d692332022-01-19 16:04:04 +080086
87 private:
88 /*
89 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
90 * DO NOT hold any locks when modifying this map, otherwise when the untag
91 * operation is waiting for a lock hold by other process and there are more
92 * sockets being closed than can fit in the socket buffer of the netlink socket
93 * that receives them, then the kernel will drop some of these sockets and we
94 * won't delete their tags.
95 * Map Key: uint64_t socket cookie
96 * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
97 */
98 bpf::BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
99
100 /*
101 * mUidCounterSetMap: Store the counterSet of a specific uid.
102 * Map Key: uint32 uid.
103 * Map Value: uint32 counterSet specifies if the traffic is a background
104 * or foreground traffic.
105 */
106 bpf::BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
107
108 /*
109 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
110 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
111 * API to return persistent stats for a specific uid since device boot.
112 */
113 bpf::BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
114
115 /*
116 * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
117 * combination of uid, tag, iface and counterSet. These two maps contain
118 * both tagged and untagged traffic.
119 * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
120 * information.
121 * Map Value: Stats, contains packet count and byte count of each
122 * transport protocol on egress and ingress direction.
123 */
124 bpf::BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
125
126 bpf::BpfMap<StatsKey, StatsValue> mStatsMapB GUARDED_BY(mMutex);
127
128 /*
129 * mIfaceIndexNameMap: Store the index name pair of each interface show up
130 * on the device since boot. The interface index is used by the eBPF program
131 * to correctly match the iface name when receiving a packet.
132 */
133 bpf::BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
134
135 /*
136 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
137 * filter.
138 */
139 bpf::BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
140
141 /*
142 * mConfigurationMap: Store the current network policy about uid filtering
143 * and the current stats map in use. There are two configuration entries in
144 * the map right now:
145 * - Entry with UID_RULES_CONFIGURATION_KEY:
146 * Store the configuration for the current uid rules. It indicates the device
Motomu Utsumid9801492022-06-01 13:57:27 +0000147 * is in doze/powersave/standby/restricted/low power standby/oem deny mode.
Wayne Ma4d692332022-01-19 16:04:04 +0800148 * - Entry with CURRENT_STATS_MAP_CONFIGURATION_KEY:
149 * Stores the current live stats map that kernel program is writing to.
150 * Userspace can do scraping and cleaning job on the other one depending on the
151 * current configs.
152 */
Lorenzo Colitti60cbed32022-03-03 17:49:01 +0900153 bpf::BpfMap<uint32_t, uint32_t> mConfigurationMap GUARDED_BY(mMutex);
Wayne Ma4d692332022-01-19 16:04:04 +0800154
155 /*
156 * mUidOwnerMap: Store uids that are used for bandwidth control uid match.
157 */
158 bpf::BpfMap<uint32_t, UidOwnerValue> mUidOwnerMap GUARDED_BY(mMutex);
159
160 /*
161 * mUidOwnerMap: Store uids that are used for INTERNET permission check.
162 */
163 bpf::BpfMap<uint32_t, uint8_t> mUidPermissionMap GUARDED_BY(mMutex);
164
165 std::unique_ptr<netdutils::NetlinkListenerInterface> mSkDestroyListener;
166
167 netdutils::Status removeRule(uint32_t uid, UidOwnerMatchType match) REQUIRES(mMutex);
168
169 netdutils::Status addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif = 0)
170 REQUIRES(mMutex);
171
Wayne Ma4d692332022-01-19 16:04:04 +0800172 std::mutex mMutex;
173
Patrick Rohr2b1b2c72022-02-01 15:57:48 +0100174 netdutils::Status initMaps() EXCLUDES(mMutex);
175
Wayne Ma4d692332022-01-19 16:04:04 +0800176 // Keep track of uids that have permission UPDATE_DEVICE_STATS so we don't
177 // need to call back to system server for permission check.
178 std::set<uid_t> mPrivilegedUser GUARDED_BY(mMutex);
179
Wayne Ma4d692332022-01-19 16:04:04 +0800180 // For testing
181 friend class TrafficControllerTest;
182};
183
184} // namespace net
185} // namespace android