blob: 6e107c29fa56f931dcc3e1a00bb857ed6f5583d4 [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 Utsumibe3ff1e2022-06-08 10:05:07 +000038import com.android.internal.annotations.VisibleForTesting;
Ken Chenf5f51332022-01-28 10:08:16 +080039import com.android.modules.utils.build.SdkLevel;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000040import com.android.net.module.util.BpfMap;
41import com.android.net.module.util.Struct.U32;
Ken Chenf5f51332022-01-28 10:08:16 +080042
Ken Chene6d511f2022-01-25 11:10:42 +080043import java.io.FileDescriptor;
44import java.io.IOException;
45
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080046/**
47 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
48 *
49 * {@hide}
50 */
51public class BpfNetMaps {
52 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +080053 private final INetd mNetd;
Ken Chenf5f51332022-01-28 10:08:16 +080054 // Use legacy netd for releases before T.
Motomu Utsumi25cf86f2022-06-27 08:50:19 +000055 private static final boolean PRE_T = !SdkLevel.isAtLeastT();
Ken Chenf5f51332022-01-28 10:08:16 +080056 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080057
Motomu Utsumi18b287d2022-06-19 10:45:30 +000058 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
59 // This entry is not accessed by others.
60 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
61 private static final Object sUidRulesConfigBpfMapLock = new Object();
62
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000063 private static final String CONFIGURATION_MAP_PATH =
64 "/sys/fs/bpf/netd_shared/map_netd_configuration_map";
65 private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0);
66 private static BpfMap<U32, U32> sConfigurationMap = null;
67
68 // LINT.IfChange(match_type)
69 private static final long NO_MATCH = 0;
70 private static final long HAPPY_BOX_MATCH = (1 << 0);
71 private static final long PENALTY_BOX_MATCH = (1 << 1);
72 private static final long DOZABLE_MATCH = (1 << 2);
73 private static final long STANDBY_MATCH = (1 << 3);
74 private static final long POWERSAVE_MATCH = (1 << 4);
75 private static final long RESTRICTED_MATCH = (1 << 5);
76 private static final long LOW_POWER_STANDBY_MATCH = (1 << 6);
77 private static final long IIF_MATCH = (1 << 7);
78 private static final long LOCKDOWN_VPN_MATCH = (1 << 8);
79 private static final long OEM_DENY_1_MATCH = (1 << 9);
80 private static final long OEM_DENY_2_MATCH = (1 << 10);
81 private static final long OEM_DENY_3_MATCH = (1 << 11);
82 // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h)
83
84 // TODO: Use Java BpfMap instead of JNI code (TrafficController) for map update.
85 // Currently, BpfNetMaps uses TrafficController for map update and TrafficController
86 // (changeUidOwnerRule and toggleUidOwnerMap) also does conversion from "firewall chain" to
87 // "match". Migrating map update from JNI to Java BpfMap will solve this duplication.
88 private static final SparseLongArray FIREWALL_CHAIN_TO_MATCH = new SparseLongArray();
89 static {
90 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_DOZABLE, DOZABLE_MATCH);
91 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_STANDBY, STANDBY_MATCH);
92 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_POWERSAVE, POWERSAVE_MATCH);
93 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_RESTRICTED, RESTRICTED_MATCH);
94 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_LOW_POWER_STANDBY, LOW_POWER_STANDBY_MATCH);
95 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_1, OEM_DENY_1_MATCH);
96 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_2, OEM_DENY_2_MATCH);
97 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_3, OEM_DENY_3_MATCH);
98 }
99
100 /**
101 * Only tests or BpfNetMaps#ensureInitialized can call this function.
102 */
103 @VisibleForTesting
104 public static void initialize(final Dependencies deps) {
105 sConfigurationMap = deps.getConfigurationMap();
106 }
107
Ken Chenf5f51332022-01-28 10:08:16 +0800108 /**
109 * Initializes the class if it is not already initialized. This method will open maps but not
110 * cause any other effects. This method may be called multiple times on any thread.
111 */
112 private static synchronized void ensureInitialized() {
113 if (sInitialized) return;
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000114 if (!PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800115 System.loadLibrary("service-connectivity");
Wayne Ma2fde98c2022-01-17 18:04:05 +0800116 native_init();
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000117 initialize(new Dependencies());
Wayne Ma2fde98c2022-01-17 18:04:05 +0800118 }
Ken Chenf5f51332022-01-28 10:08:16 +0800119 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800120 }
121
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000122 /**
123 * Dependencies of BpfNetMaps, for injection in tests.
124 */
125 @VisibleForTesting
126 public static class Dependencies {
127 /**
128 * Get configuration BPF map.
129 */
130 public BpfMap<U32, U32> getConfigurationMap() {
131 try {
132 return new BpfMap<>(
133 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class);
134 } catch (ErrnoException e) {
135 Log.e(TAG, "Cannot open netd configuration map: " + e);
136 return null;
137 }
138 }
139 }
140
markchien49e944c2022-03-01 15:22:20 +0800141 /** Constructor used after T that doesn't need to use netd anymore. */
142 public BpfNetMaps() {
143 this(null);
144
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000145 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800146 }
147
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000148 public BpfNetMaps(final INetd netd) {
Ken Chenf5f51332022-01-28 10:08:16 +0800149 ensureInitialized();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800150 mNetd = netd;
Wayne Ma790c83e2022-01-13 10:35:05 +0800151 }
152
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000153 /**
154 * Get corresponding match from firewall chain.
155 */
156 @VisibleForTesting
157 public long getMatchByFirewallChain(final int chain) {
158 final long match = FIREWALL_CHAIN_TO_MATCH.get(chain, NO_MATCH);
159 if (match == NO_MATCH) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000160 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000161 }
162 return match;
163 }
164
Ken Chenf5f51332022-01-28 10:08:16 +0800165 private void maybeThrow(final int err, final String msg) {
166 if (err != 0) {
167 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
168 }
169 }
170
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000171 private void throwIfPreT(final String msg) {
172 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000173 throw new UnsupportedOperationException(msg);
174 }
175 }
176
Ken Chenf5f51332022-01-28 10:08:16 +0800177 /**
178 * Add naughty app bandwidth rule for specific app
179 *
180 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800181 * @throws ServiceSpecificException in case of failure, with an error code indicating the
182 * cause of the failure.
183 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900184 public void addNaughtyApp(final int uid) {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800185 final int err = native_addNaughtyApp(uid);
Ken Chenf5f51332022-01-28 10:08:16 +0800186 maybeThrow(err, "Unable to add naughty app");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800187 }
188
Ken Chenf5f51332022-01-28 10:08:16 +0800189 /**
190 * Remove naughty app bandwidth rule for specific app
191 *
192 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800193 * @throws ServiceSpecificException in case of failure, with an error code indicating the
194 * cause of the failure.
195 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900196 public void removeNaughtyApp(final int uid) {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800197 final int err = native_removeNaughtyApp(uid);
Ken Chenf5f51332022-01-28 10:08:16 +0800198 maybeThrow(err, "Unable to remove naughty app");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800199 }
200
Ken Chenf5f51332022-01-28 10:08:16 +0800201 /**
202 * Add nice app bandwidth rule for specific app
203 *
204 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800205 * @throws ServiceSpecificException in case of failure, with an error code indicating the
206 * cause of the failure.
207 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900208 public void addNiceApp(final int uid) {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800209 final int err = native_addNiceApp(uid);
Ken Chenf5f51332022-01-28 10:08:16 +0800210 maybeThrow(err, "Unable to add nice app");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800211 }
212
Ken Chenf5f51332022-01-28 10:08:16 +0800213 /**
214 * Remove nice app bandwidth rule for specific app
215 *
216 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800217 * @throws ServiceSpecificException in case of failure, with an error code indicating the
218 * cause of the failure.
219 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900220 public void removeNiceApp(final int uid) {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800221 final int err = native_removeNiceApp(uid);
Ken Chenf5f51332022-01-28 10:08:16 +0800222 maybeThrow(err, "Unable to remove nice app");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800223 }
224
Ken Chenf5f51332022-01-28 10:08:16 +0800225 /**
226 * Set target firewall child chain
227 *
228 * @param childChain target chain to enable
229 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000230 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800231 * @throws ServiceSpecificException in case of failure, with an error code indicating the
232 * cause of the failure.
233 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900234 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000235 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000236
237 final long match = getMatchByFirewallChain(childChain);
238 try {
239 synchronized (sUidRulesConfigBpfMapLock) {
240 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000241 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000242 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
243 }
244 } catch (ErrnoException e) {
245 throw new ServiceSpecificException(e.errno,
246 "Unable to set child chain: " + Os.strerror(e.errno));
247 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800248 }
249
250 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000251 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000252 *
253 * @param childChain target chain
254 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
255 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000256 * @throws ServiceSpecificException in case of failure, with an error code indicating the
257 * cause of the failure.
258 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000259 public boolean isChainEnabled(final int childChain) {
260 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000261
262 final long match = getMatchByFirewallChain(childChain);
263 try {
264 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000265 return (config.val & match) != 0;
266 } catch (ErrnoException e) {
267 throw new ServiceSpecificException(e.errno,
268 "Unable to get firewall chain status: " + Os.strerror(e.errno));
269 }
270 }
271
272 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800273 * Replaces the contents of the specified UID-based firewall chain.
274 *
275 * The chain may be an allowlist chain or a denylist chain. A denylist chain contains DROP
276 * rules for the specified UIDs and a RETURN rule at the end. An allowlist chain contains RETURN
Ken Chenf5f51332022-01-28 10:08:16 +0800277 * rules for the system UID range (0 to {@code UID_APP} - 1), RETURN rules for the specified
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800278 * UIDs, and a DROP rule at the end. The chain will be created if it does not exist.
279 *
Ken Chenf5f51332022-01-28 10:08:16 +0800280 * @param chainName The name of the chain to replace.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800281 * @param isAllowlist Whether this is an allowlist or denylist chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800282 * @param uids The list of UIDs to allow/deny.
283 * @return 0 if the chain was successfully replaced, errno otherwise.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800284 */
285 public int replaceUidChain(final String chainName, final boolean isAllowlist,
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900286 final int[] uids) {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800287 final int err = native_replaceUidChain(chainName, isAllowlist, uids);
288 if (err != 0) {
289 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
290 }
291 return -err;
292 }
293
Ken Chenf5f51332022-01-28 10:08:16 +0800294 /**
295 * Set firewall rule for uid
296 *
297 * @param childChain target chain
298 * @param uid uid to allow/deny
299 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800300 * @throws ServiceSpecificException in case of failure, with an error code indicating the
301 * cause of the failure.
302 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900303 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800304 final int err = native_setUidRule(childChain, uid, firewallRule);
Ken Chenf5f51332022-01-28 10:08:16 +0800305 maybeThrow(err, "Unable to set uid rule");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800306 }
307
308 /**
309 * Add ingress interface filtering rules to a list of UIDs
310 *
311 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
312 * allowed interface and loopback to be sent to the list of UIDs.
313 *
314 * Calling this method on one or more UIDs with an existing filtering rule but a different
315 * interface name will result in the filtering rule being updated to allow the new interface
316 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
317 *
318 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800319 * be received.
320 * @param uids an array of UIDs which the filtering rules will be set
321 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800322 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800323 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800324 */
Ken Chenf5f51332022-01-28 10:08:16 +0800325 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000326 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800327 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800328 return;
329 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800330 final int err = native_addUidInterfaceRules(ifName, uids);
Ken Chenf5f51332022-01-28 10:08:16 +0800331 maybeThrow(err, "Unable to add uid interface rules");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800332 }
333
334 /**
335 * Remove ingress interface filtering rules from a list of UIDs
336 *
337 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
338 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
339 *
340 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800341 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800342 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800343 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800344 */
Ken Chenf5f51332022-01-28 10:08:16 +0800345 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000346 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800347 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800348 return;
349 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800350 final int err = native_removeUidInterfaceRules(uids);
Ken Chenf5f51332022-01-28 10:08:16 +0800351 maybeThrow(err, "Unable to remove uid interface rules");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800352 }
353
Ken Chenf5f51332022-01-28 10:08:16 +0800354 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000355 * Update lockdown rule for uid
356 *
357 * @param uid target uid to add/remove the rule
358 * @param add {@code true} to add the rule, {@code false} to remove the rule.
359 * @throws ServiceSpecificException in case of failure, with an error code indicating the
360 * cause of the failure.
361 */
362 public void updateUidLockdownRule(final int uid, final boolean add) {
363 final int err = native_updateUidLockdownRule(uid, add);
364 maybeThrow(err, "Unable to update lockdown rule");
365 }
366
367 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800368 * Request netd to change the current active network stats map.
369 *
Ken Chenf5f51332022-01-28 10:08:16 +0800370 * @throws ServiceSpecificException in case of failure, with an error code indicating the
371 * cause of the failure.
372 */
markchien49e944c2022-03-01 15:22:20 +0800373 public void swapActiveStatsMap() {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800374 final int err = native_swapActiveStatsMap();
Ken Chenf5f51332022-01-28 10:08:16 +0800375 maybeThrow(err, "Unable to swap active stats map");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800376 }
377
Ken Chenf5f51332022-01-28 10:08:16 +0800378 /**
379 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
380 * specified. Or remove all permissions from the uids.
381 *
382 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
383 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
384 * revoke all permissions for the uids.
385 * @param uids uid of users to grant permission
386 * @throws RemoteException when netd has crashed.
387 */
388 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000389 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800390 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800391 return;
392 }
393 native_setPermissionForUids(permissions, uids);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800394 }
395
Ken Chene6d511f2022-01-25 11:10:42 +0800396 /**
397 * Dump BPF maps
398 *
399 * @param fd file descriptor to output
400 * @throws IOException when file descriptor is invalid.
401 * @throws ServiceSpecificException when the method is called on an unsupported device.
402 */
403 public void dump(final FileDescriptor fd, boolean verbose)
404 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000405 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800406 throw new ServiceSpecificException(
407 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
408 + " devices, use dumpsys netd trafficcontroller instead.");
409 }
410 native_dump(fd, verbose);
411 }
412
Wayne Ma790c83e2022-01-13 10:35:05 +0800413 private static native void native_init();
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800414 private native int native_addNaughtyApp(int uid);
415 private native int native_removeNaughtyApp(int uid);
416 private native int native_addNiceApp(int uid);
417 private native int native_removeNiceApp(int uid);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800418 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
419 private native int native_setUidRule(int childChain, int uid, int firewallRule);
420 private native int native_addUidInterfaceRules(String ifName, int[] uids);
421 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000422 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800423 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800424 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800425 private native void native_dump(FileDescriptor fd, boolean verbose);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800426}