blob: 3a5fc06aaf4cd6d4a771557d3999c17c7e93738a [file] [log] [blame]
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001/*
2 * Copyright (C) 2022 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
17package com.android.server;
18
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000019import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE;
20import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY;
21import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1;
22import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2;
23import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3;
24import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE;
25import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED;
26import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY;
Motomu Utsumi18b287d2022-06-19 10:45:30 +000027import static android.system.OsConstants.EINVAL;
Ken Chene6d511f2022-01-25 11:10:42 +080028import static android.system.OsConstants.EOPNOTSUPP;
29
Wayne Ma2fde98c2022-01-17 18:04:05 +080030import android.net.INetd;
31import android.os.RemoteException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080032import android.os.ServiceSpecificException;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000033import android.system.ErrnoException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080034import android.system.Os;
35import android.util.Log;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000036import android.util.SparseLongArray;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080037
Motomu Utsumi5a68a212022-06-24 10:14:31 +000038import com.android.internal.annotations.GuardedBy;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000039import com.android.internal.annotations.VisibleForTesting;
Ken Chenf5f51332022-01-28 10:08:16 +080040import com.android.modules.utils.build.SdkLevel;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000041import com.android.net.module.util.BpfMap;
42import com.android.net.module.util.Struct.U32;
Ken Chenf5f51332022-01-28 10:08:16 +080043
Ken Chene6d511f2022-01-25 11:10:42 +080044import java.io.FileDescriptor;
45import java.io.IOException;
46
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080047/**
48 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
49 *
50 * {@hide}
51 */
52public class BpfNetMaps {
Motomu Utsumi305975f2022-06-27 09:24:32 +000053 private static final boolean PRE_T = !SdkLevel.isAtLeastT();
54 static {
55 if (!PRE_T) {
56 System.loadLibrary("service-connectivity");
57 }
58 }
59
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080060 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +080061 private final INetd mNetd;
Ken Chenf5f51332022-01-28 10:08:16 +080062 // Use legacy netd for releases before T.
Ken Chenf5f51332022-01-28 10:08:16 +080063 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080064
Motomu Utsumi18b287d2022-06-19 10:45:30 +000065 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
66 // This entry is not accessed by others.
67 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
68 private static final Object sUidRulesConfigBpfMapLock = new Object();
69
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000070 private static final String CONFIGURATION_MAP_PATH =
71 "/sys/fs/bpf/netd_shared/map_netd_configuration_map";
Motomu Utsumi5a68a212022-06-24 10:14:31 +000072 private static final String UID_OWNER_MAP_PATH =
73 "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map";
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000074 private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0);
75 private static BpfMap<U32, U32> sConfigurationMap = null;
Motomu Utsumi5a68a212022-06-24 10:14:31 +000076 // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others.
77 private static BpfMap<U32, UidOwnerValue> sUidOwnerMap = null;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000078
79 // LINT.IfChange(match_type)
80 private static final long NO_MATCH = 0;
81 private static final long HAPPY_BOX_MATCH = (1 << 0);
82 private static final long PENALTY_BOX_MATCH = (1 << 1);
83 private static final long DOZABLE_MATCH = (1 << 2);
84 private static final long STANDBY_MATCH = (1 << 3);
85 private static final long POWERSAVE_MATCH = (1 << 4);
86 private static final long RESTRICTED_MATCH = (1 << 5);
87 private static final long LOW_POWER_STANDBY_MATCH = (1 << 6);
88 private static final long IIF_MATCH = (1 << 7);
89 private static final long LOCKDOWN_VPN_MATCH = (1 << 8);
90 private static final long OEM_DENY_1_MATCH = (1 << 9);
91 private static final long OEM_DENY_2_MATCH = (1 << 10);
92 private static final long OEM_DENY_3_MATCH = (1 << 11);
93 // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h)
94
95 // TODO: Use Java BpfMap instead of JNI code (TrafficController) for map update.
96 // Currently, BpfNetMaps uses TrafficController for map update and TrafficController
97 // (changeUidOwnerRule and toggleUidOwnerMap) also does conversion from "firewall chain" to
98 // "match". Migrating map update from JNI to Java BpfMap will solve this duplication.
99 private static final SparseLongArray FIREWALL_CHAIN_TO_MATCH = new SparseLongArray();
100 static {
101 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_DOZABLE, DOZABLE_MATCH);
102 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_STANDBY, STANDBY_MATCH);
103 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_POWERSAVE, POWERSAVE_MATCH);
104 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_RESTRICTED, RESTRICTED_MATCH);
105 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_LOW_POWER_STANDBY, LOW_POWER_STANDBY_MATCH);
106 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_1, OEM_DENY_1_MATCH);
107 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_2, OEM_DENY_2_MATCH);
108 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_3, OEM_DENY_3_MATCH);
109 }
110
111 /**
Motomu Utsumi305975f2022-06-27 09:24:32 +0000112 * Set configurationMap for test.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000113 */
114 @VisibleForTesting
Motomu Utsumi305975f2022-06-27 09:24:32 +0000115 public static void setConfigurationMapForTest(BpfMap<U32, U32> configurationMap) {
116 sConfigurationMap = configurationMap;
117 }
118
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000119 /**
120 * Set uidOwnerMap for test.
121 */
122 @VisibleForTesting
123 public static void setUidOwnerMapForTest(BpfMap<U32, UidOwnerValue> uidOwnerMap) {
124 sUidOwnerMap = uidOwnerMap;
125 }
126
Motomu Utsumi305975f2022-06-27 09:24:32 +0000127 private static BpfMap<U32, U32> getConfigurationMap() {
128 try {
129 return new BpfMap<>(
130 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class);
131 } catch (ErrnoException e) {
132 throw new IllegalStateException("Cannot open netd configuration map", e);
133 }
134 }
135
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000136 private static BpfMap<U32, UidOwnerValue> getUidOwnerMap() {
137 try {
138 return new BpfMap<>(
139 UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class);
140 } catch (ErrnoException e) {
141 throw new IllegalStateException("Cannot open uid owner map", e);
142 }
143 }
144
Motomu Utsumi305975f2022-06-27 09:24:32 +0000145 private static void setBpfMaps() {
146 if (sConfigurationMap == null) {
147 sConfigurationMap = getConfigurationMap();
148 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000149 if (sUidOwnerMap == null) {
150 sUidOwnerMap = getUidOwnerMap();
151 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000152 }
153
Ken Chenf5f51332022-01-28 10:08:16 +0800154 /**
155 * Initializes the class if it is not already initialized. This method will open maps but not
156 * cause any other effects. This method may be called multiple times on any thread.
157 */
158 private static synchronized void ensureInitialized() {
159 if (sInitialized) return;
Motomu Utsumi305975f2022-06-27 09:24:32 +0000160 setBpfMaps();
161 native_init();
Ken Chenf5f51332022-01-28 10:08:16 +0800162 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800163 }
164
markchien49e944c2022-03-01 15:22:20 +0800165 /** Constructor used after T that doesn't need to use netd anymore. */
166 public BpfNetMaps() {
167 this(null);
168
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000169 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800170 }
171
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000172 public BpfNetMaps(final INetd netd) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000173 if (!PRE_T) {
174 ensureInitialized();
175 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800176 mNetd = netd;
Wayne Ma790c83e2022-01-13 10:35:05 +0800177 }
178
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000179 /**
180 * Get corresponding match from firewall chain.
181 */
182 @VisibleForTesting
183 public long getMatchByFirewallChain(final int chain) {
184 final long match = FIREWALL_CHAIN_TO_MATCH.get(chain, NO_MATCH);
185 if (match == NO_MATCH) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000186 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000187 }
188 return match;
189 }
190
Ken Chenf5f51332022-01-28 10:08:16 +0800191 private void maybeThrow(final int err, final String msg) {
192 if (err != 0) {
193 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
194 }
195 }
196
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000197 private void throwIfPreT(final String msg) {
198 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000199 throw new UnsupportedOperationException(msg);
200 }
201 }
202
Ken Chenf5f51332022-01-28 10:08:16 +0800203 /**
204 * Add naughty app bandwidth rule for specific app
205 *
206 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800207 * @throws ServiceSpecificException in case of failure, with an error code indicating the
208 * cause of the failure.
209 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900210 public void addNaughtyApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000211 synchronized (sUidOwnerMap) {
212 final int err = native_addNaughtyApp(uid);
213 maybeThrow(err, "Unable to add naughty app");
214 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800215 }
216
Ken Chenf5f51332022-01-28 10:08:16 +0800217 /**
218 * Remove naughty app bandwidth rule for specific app
219 *
220 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800221 * @throws ServiceSpecificException in case of failure, with an error code indicating the
222 * cause of the failure.
223 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900224 public void removeNaughtyApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000225 synchronized (sUidOwnerMap) {
226 final int err = native_removeNaughtyApp(uid);
227 maybeThrow(err, "Unable to remove naughty app");
228 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800229 }
230
Ken Chenf5f51332022-01-28 10:08:16 +0800231 /**
232 * Add nice app bandwidth rule for specific app
233 *
234 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800235 * @throws ServiceSpecificException in case of failure, with an error code indicating the
236 * cause of the failure.
237 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900238 public void addNiceApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000239 synchronized (sUidOwnerMap) {
240 final int err = native_addNiceApp(uid);
241 maybeThrow(err, "Unable to add nice app");
242 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800243 }
244
Ken Chenf5f51332022-01-28 10:08:16 +0800245 /**
246 * Remove nice app bandwidth rule for specific app
247 *
248 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800249 * @throws ServiceSpecificException in case of failure, with an error code indicating the
250 * cause of the failure.
251 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900252 public void removeNiceApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000253 synchronized (sUidOwnerMap) {
254 final int err = native_removeNiceApp(uid);
255 maybeThrow(err, "Unable to remove nice app");
256 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800257 }
258
Ken Chenf5f51332022-01-28 10:08:16 +0800259 /**
260 * Set target firewall child chain
261 *
262 * @param childChain target chain to enable
263 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000264 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800265 * @throws ServiceSpecificException in case of failure, with an error code indicating the
266 * cause of the failure.
267 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900268 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000269 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000270
271 final long match = getMatchByFirewallChain(childChain);
272 try {
273 synchronized (sUidRulesConfigBpfMapLock) {
274 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000275 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000276 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
277 }
278 } catch (ErrnoException e) {
279 throw new ServiceSpecificException(e.errno,
280 "Unable to set child chain: " + Os.strerror(e.errno));
281 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800282 }
283
284 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000285 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000286 *
287 * @param childChain target chain
288 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
289 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000290 * @throws ServiceSpecificException in case of failure, with an error code indicating the
291 * cause of the failure.
292 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000293 public boolean isChainEnabled(final int childChain) {
294 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000295
296 final long match = getMatchByFirewallChain(childChain);
297 try {
298 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000299 return (config.val & match) != 0;
300 } catch (ErrnoException e) {
301 throw new ServiceSpecificException(e.errno,
302 "Unable to get firewall chain status: " + Os.strerror(e.errno));
303 }
304 }
305
306 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800307 * Replaces the contents of the specified UID-based firewall chain.
308 *
309 * The chain may be an allowlist chain or a denylist chain. A denylist chain contains DROP
310 * rules for the specified UIDs and a RETURN rule at the end. An allowlist chain contains RETURN
Ken Chenf5f51332022-01-28 10:08:16 +0800311 * rules for the system UID range (0 to {@code UID_APP} - 1), RETURN rules for the specified
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800312 * UIDs, and a DROP rule at the end. The chain will be created if it does not exist.
313 *
Ken Chenf5f51332022-01-28 10:08:16 +0800314 * @param chainName The name of the chain to replace.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800315 * @param isAllowlist Whether this is an allowlist or denylist chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800316 * @param uids The list of UIDs to allow/deny.
317 * @return 0 if the chain was successfully replaced, errno otherwise.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800318 */
319 public int replaceUidChain(final String chainName, final boolean isAllowlist,
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900320 final int[] uids) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000321 synchronized (sUidOwnerMap) {
322 final int err = native_replaceUidChain(chainName, isAllowlist, uids);
323 if (err != 0) {
324 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
325 }
326 return -err;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800327 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800328 }
329
Ken Chenf5f51332022-01-28 10:08:16 +0800330 /**
331 * Set firewall rule for uid
332 *
333 * @param childChain target chain
334 * @param uid uid to allow/deny
335 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800336 * @throws ServiceSpecificException in case of failure, with an error code indicating the
337 * cause of the failure.
338 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900339 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000340 synchronized (sUidOwnerMap) {
341 final int err = native_setUidRule(childChain, uid, firewallRule);
342 maybeThrow(err, "Unable to set uid rule");
343 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800344 }
345
346 /**
347 * Add ingress interface filtering rules to a list of UIDs
348 *
349 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
350 * allowed interface and loopback to be sent to the list of UIDs.
351 *
352 * Calling this method on one or more UIDs with an existing filtering rule but a different
353 * interface name will result in the filtering rule being updated to allow the new interface
354 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
355 *
356 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800357 * be received.
358 * @param uids an array of UIDs which the filtering rules will be set
359 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800360 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800361 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800362 */
Ken Chenf5f51332022-01-28 10:08:16 +0800363 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000364 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800365 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800366 return;
367 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000368 synchronized (sUidOwnerMap) {
369 final int err = native_addUidInterfaceRules(ifName, uids);
370 maybeThrow(err, "Unable to add uid interface rules");
371 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800372 }
373
374 /**
375 * Remove ingress interface filtering rules from a list of UIDs
376 *
377 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
378 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
379 *
380 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800381 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800382 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800383 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800384 */
Ken Chenf5f51332022-01-28 10:08:16 +0800385 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000386 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800387 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800388 return;
389 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000390 synchronized (sUidOwnerMap) {
391 final int err = native_removeUidInterfaceRules(uids);
392 maybeThrow(err, "Unable to remove uid interface rules");
393 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800394 }
395
Ken Chenf5f51332022-01-28 10:08:16 +0800396 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000397 * Update lockdown rule for uid
398 *
399 * @param uid target uid to add/remove the rule
400 * @param add {@code true} to add the rule, {@code false} to remove the rule.
401 * @throws ServiceSpecificException in case of failure, with an error code indicating the
402 * cause of the failure.
403 */
404 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000405 synchronized (sUidOwnerMap) {
406 final int err = native_updateUidLockdownRule(uid, add);
407 maybeThrow(err, "Unable to update lockdown rule");
408 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000409 }
410
411 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800412 * Request netd to change the current active network stats map.
413 *
Ken Chenf5f51332022-01-28 10:08:16 +0800414 * @throws ServiceSpecificException in case of failure, with an error code indicating the
415 * cause of the failure.
416 */
markchien49e944c2022-03-01 15:22:20 +0800417 public void swapActiveStatsMap() {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800418 final int err = native_swapActiveStatsMap();
Ken Chenf5f51332022-01-28 10:08:16 +0800419 maybeThrow(err, "Unable to swap active stats map");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800420 }
421
Ken Chenf5f51332022-01-28 10:08:16 +0800422 /**
423 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
424 * specified. Or remove all permissions from the uids.
425 *
426 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
427 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
428 * revoke all permissions for the uids.
429 * @param uids uid of users to grant permission
430 * @throws RemoteException when netd has crashed.
431 */
432 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000433 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800434 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800435 return;
436 }
437 native_setPermissionForUids(permissions, uids);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800438 }
439
Ken Chene6d511f2022-01-25 11:10:42 +0800440 /**
441 * Dump BPF maps
442 *
443 * @param fd file descriptor to output
444 * @throws IOException when file descriptor is invalid.
445 * @throws ServiceSpecificException when the method is called on an unsupported device.
446 */
447 public void dump(final FileDescriptor fd, boolean verbose)
448 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000449 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800450 throw new ServiceSpecificException(
451 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
452 + " devices, use dumpsys netd trafficcontroller instead.");
453 }
454 native_dump(fd, verbose);
455 }
456
Wayne Ma790c83e2022-01-13 10:35:05 +0800457 private static native void native_init();
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000458 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800459 private native int native_addNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000460 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800461 private native int native_removeNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000462 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800463 private native int native_addNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000464 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800465 private native int native_removeNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000466 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800467 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000468 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800469 private native int native_setUidRule(int childChain, int uid, int firewallRule);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000470 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800471 private native int native_addUidInterfaceRules(String ifName, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000472 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800473 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000474 @GuardedBy("sUidOwnerMap")
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000475 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800476 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800477 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800478 private native void native_dump(FileDescriptor fd, boolean verbose);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800479}