blob: be0eb5f05563ad2435ed6a330bf8ed46e53e6a8e [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;
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000028import static android.system.OsConstants.ENOENT;
Ken Chene6d511f2022-01-25 11:10:42 +080029import static android.system.OsConstants.EOPNOTSUPP;
30
Wayne Ma2fde98c2022-01-17 18:04:05 +080031import android.net.INetd;
32import android.os.RemoteException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080033import android.os.ServiceSpecificException;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000034import android.system.ErrnoException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080035import android.system.Os;
36import android.util.Log;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000037import android.util.SparseLongArray;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080038
Motomu Utsumi5a68a212022-06-24 10:14:31 +000039import com.android.internal.annotations.GuardedBy;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000040import com.android.internal.annotations.VisibleForTesting;
Ken Chenf5f51332022-01-28 10:08:16 +080041import com.android.modules.utils.build.SdkLevel;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000042import com.android.net.module.util.BpfMap;
43import com.android.net.module.util.Struct.U32;
Ken Chenf5f51332022-01-28 10:08:16 +080044
Ken Chene6d511f2022-01-25 11:10:42 +080045import java.io.FileDescriptor;
46import java.io.IOException;
47
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080048/**
49 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
50 *
51 * {@hide}
52 */
53public class BpfNetMaps {
Motomu Utsumi305975f2022-06-27 09:24:32 +000054 private static final boolean PRE_T = !SdkLevel.isAtLeastT();
55 static {
56 if (!PRE_T) {
57 System.loadLibrary("service-connectivity");
58 }
59 }
60
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080061 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +080062 private final INetd mNetd;
Ken Chenf5f51332022-01-28 10:08:16 +080063 // Use legacy netd for releases before T.
Ken Chenf5f51332022-01-28 10:08:16 +080064 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080065
Motomu Utsumi18b287d2022-06-19 10:45:30 +000066 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
67 // This entry is not accessed by others.
68 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
69 private static final Object sUidRulesConfigBpfMapLock = new Object();
70
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000071 private static final String CONFIGURATION_MAP_PATH =
72 "/sys/fs/bpf/netd_shared/map_netd_configuration_map";
Motomu Utsumi5a68a212022-06-24 10:14:31 +000073 private static final String UID_OWNER_MAP_PATH =
74 "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map";
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000075 private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0);
76 private static BpfMap<U32, U32> sConfigurationMap = null;
Motomu Utsumi5a68a212022-06-24 10:14:31 +000077 // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others.
78 private static BpfMap<U32, UidOwnerValue> sUidOwnerMap = null;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000079
80 // LINT.IfChange(match_type)
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000081 @VisibleForTesting public static final long NO_MATCH = 0;
82 @VisibleForTesting public static final long HAPPY_BOX_MATCH = (1 << 0);
83 @VisibleForTesting public static final long PENALTY_BOX_MATCH = (1 << 1);
84 @VisibleForTesting public static final long DOZABLE_MATCH = (1 << 2);
85 @VisibleForTesting public static final long STANDBY_MATCH = (1 << 3);
86 @VisibleForTesting public static final long POWERSAVE_MATCH = (1 << 4);
87 @VisibleForTesting public static final long RESTRICTED_MATCH = (1 << 5);
88 @VisibleForTesting public static final long LOW_POWER_STANDBY_MATCH = (1 << 6);
89 @VisibleForTesting public static final long IIF_MATCH = (1 << 7);
90 @VisibleForTesting public static final long LOCKDOWN_VPN_MATCH = (1 << 8);
91 @VisibleForTesting public static final long OEM_DENY_1_MATCH = (1 << 9);
92 @VisibleForTesting public static final long OEM_DENY_2_MATCH = (1 << 10);
93 @VisibleForTesting public static final long OEM_DENY_3_MATCH = (1 << 11);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000094 // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h)
95
96 // TODO: Use Java BpfMap instead of JNI code (TrafficController) for map update.
97 // Currently, BpfNetMaps uses TrafficController for map update and TrafficController
98 // (changeUidOwnerRule and toggleUidOwnerMap) also does conversion from "firewall chain" to
99 // "match". Migrating map update from JNI to Java BpfMap will solve this duplication.
100 private static final SparseLongArray FIREWALL_CHAIN_TO_MATCH = new SparseLongArray();
101 static {
102 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_DOZABLE, DOZABLE_MATCH);
103 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_STANDBY, STANDBY_MATCH);
104 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_POWERSAVE, POWERSAVE_MATCH);
105 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_RESTRICTED, RESTRICTED_MATCH);
106 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_LOW_POWER_STANDBY, LOW_POWER_STANDBY_MATCH);
107 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_1, OEM_DENY_1_MATCH);
108 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_2, OEM_DENY_2_MATCH);
109 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_3, OEM_DENY_3_MATCH);
110 }
111
112 /**
Motomu Utsumi305975f2022-06-27 09:24:32 +0000113 * Set configurationMap for test.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000114 */
115 @VisibleForTesting
Motomu Utsumi305975f2022-06-27 09:24:32 +0000116 public static void setConfigurationMapForTest(BpfMap<U32, U32> configurationMap) {
117 sConfigurationMap = configurationMap;
118 }
119
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000120 /**
121 * Set uidOwnerMap for test.
122 */
123 @VisibleForTesting
124 public static void setUidOwnerMapForTest(BpfMap<U32, UidOwnerValue> uidOwnerMap) {
125 sUidOwnerMap = uidOwnerMap;
126 }
127
Motomu Utsumi305975f2022-06-27 09:24:32 +0000128 private static BpfMap<U32, U32> getConfigurationMap() {
129 try {
130 return new BpfMap<>(
131 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class);
132 } catch (ErrnoException e) {
133 throw new IllegalStateException("Cannot open netd configuration map", e);
134 }
135 }
136
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000137 private static BpfMap<U32, UidOwnerValue> getUidOwnerMap() {
138 try {
139 return new BpfMap<>(
140 UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class);
141 } catch (ErrnoException e) {
142 throw new IllegalStateException("Cannot open uid owner map", e);
143 }
144 }
145
Motomu Utsumi305975f2022-06-27 09:24:32 +0000146 private static void setBpfMaps() {
147 if (sConfigurationMap == null) {
148 sConfigurationMap = getConfigurationMap();
149 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000150 if (sUidOwnerMap == null) {
151 sUidOwnerMap = getUidOwnerMap();
152 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000153 }
154
Ken Chenf5f51332022-01-28 10:08:16 +0800155 /**
156 * Initializes the class if it is not already initialized. This method will open maps but not
157 * cause any other effects. This method may be called multiple times on any thread.
158 */
159 private static synchronized void ensureInitialized() {
160 if (sInitialized) return;
Motomu Utsumi305975f2022-06-27 09:24:32 +0000161 setBpfMaps();
162 native_init();
Ken Chenf5f51332022-01-28 10:08:16 +0800163 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800164 }
165
markchien49e944c2022-03-01 15:22:20 +0800166 /** Constructor used after T that doesn't need to use netd anymore. */
167 public BpfNetMaps() {
168 this(null);
169
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000170 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800171 }
172
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000173 public BpfNetMaps(final INetd netd) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000174 if (!PRE_T) {
175 ensureInitialized();
176 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800177 mNetd = netd;
Wayne Ma790c83e2022-01-13 10:35:05 +0800178 }
179
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000180 /**
181 * Get corresponding match from firewall chain.
182 */
183 @VisibleForTesting
184 public long getMatchByFirewallChain(final int chain) {
185 final long match = FIREWALL_CHAIN_TO_MATCH.get(chain, NO_MATCH);
186 if (match == NO_MATCH) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000187 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000188 }
189 return match;
190 }
191
Ken Chenf5f51332022-01-28 10:08:16 +0800192 private void maybeThrow(final int err, final String msg) {
193 if (err != 0) {
194 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
195 }
196 }
197
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000198 private void throwIfPreT(final String msg) {
199 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000200 throw new UnsupportedOperationException(msg);
201 }
202 }
203
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000204 private void removeRule(final int uid, final long match, final String caller) {
205 try {
206 synchronized (sUidOwnerMap) {
207 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
208
209 if (oldMatch == null) {
210 throw new ServiceSpecificException(ENOENT,
211 "sUidOwnerMap does not have entry for uid: " + uid);
212 }
213
214 final UidOwnerValue newMatch = new UidOwnerValue(
215 (match == IIF_MATCH) ? 0 : oldMatch.iif,
216 oldMatch.rule & ~match
217 );
218
219 if (newMatch.rule == 0) {
220 sUidOwnerMap.deleteEntry(new U32(uid));
221 } else {
222 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
223 }
224 }
225 } catch (ErrnoException e) {
226 throw new ServiceSpecificException(e.errno,
227 caller + " failed to remove rule: " + Os.strerror(e.errno));
228 }
229 }
230
Motomu Utsumi389278e2022-06-28 07:05:05 +0000231 private void addRule(final int uid, final long match, final long iif, final String caller) {
232 if (match != IIF_MATCH && iif != 0) {
233 throw new ServiceSpecificException(EINVAL,
234 "Non-interface match must have zero interface index");
235 }
236
237 try {
238 synchronized (sUidOwnerMap) {
239 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
240
241 final UidOwnerValue newMatch;
242 if (oldMatch != null) {
243 newMatch = new UidOwnerValue(
244 (match == IIF_MATCH) ? iif : oldMatch.iif,
245 oldMatch.rule | match
246 );
247 } else {
248 newMatch = new UidOwnerValue(
249 iif,
250 match
251 );
252 }
253 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
254 }
255 } catch (ErrnoException e) {
256 throw new ServiceSpecificException(e.errno,
257 caller + " failed to add rule: " + Os.strerror(e.errno));
258 }
259 }
260
261 private void addRule(final int uid, final long match, final String caller) {
262 addRule(uid, match, 0 /* iif */, caller);
263 }
264
Ken Chenf5f51332022-01-28 10:08:16 +0800265 /**
266 * Add naughty app bandwidth rule for specific app
267 *
268 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800269 * @throws ServiceSpecificException in case of failure, with an error code indicating the
270 * cause of the failure.
271 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900272 public void addNaughtyApp(final int uid) {
Motomu Utsumi389278e2022-06-28 07:05:05 +0000273 throwIfPreT("addNaughtyApp is not available on pre-T devices");
274 addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800275 }
276
Ken Chenf5f51332022-01-28 10:08:16 +0800277 /**
278 * Remove naughty app bandwidth rule for specific app
279 *
280 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800281 * @throws ServiceSpecificException in case of failure, with an error code indicating the
282 * cause of the failure.
283 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900284 public void removeNaughtyApp(final int uid) {
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000285 throwIfPreT("removeNaughtyApp is not available on pre-T devices");
286 removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800287 }
288
Ken Chenf5f51332022-01-28 10:08:16 +0800289 /**
290 * Add nice app bandwidth rule for specific app
291 *
292 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800293 * @throws ServiceSpecificException in case of failure, with an error code indicating the
294 * cause of the failure.
295 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900296 public void addNiceApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000297 synchronized (sUidOwnerMap) {
298 final int err = native_addNiceApp(uid);
299 maybeThrow(err, "Unable to add nice app");
300 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800301 }
302
Ken Chenf5f51332022-01-28 10:08:16 +0800303 /**
304 * Remove nice app bandwidth rule for specific app
305 *
306 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800307 * @throws ServiceSpecificException in case of failure, with an error code indicating the
308 * cause of the failure.
309 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900310 public void removeNiceApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000311 synchronized (sUidOwnerMap) {
312 final int err = native_removeNiceApp(uid);
313 maybeThrow(err, "Unable to remove nice app");
314 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800315 }
316
Ken Chenf5f51332022-01-28 10:08:16 +0800317 /**
318 * Set target firewall child chain
319 *
320 * @param childChain target chain to enable
321 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000322 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800323 * @throws ServiceSpecificException in case of failure, with an error code indicating the
324 * cause of the failure.
325 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900326 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000327 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000328
329 final long match = getMatchByFirewallChain(childChain);
330 try {
331 synchronized (sUidRulesConfigBpfMapLock) {
332 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000333 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000334 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
335 }
336 } catch (ErrnoException e) {
337 throw new ServiceSpecificException(e.errno,
338 "Unable to set child chain: " + Os.strerror(e.errno));
339 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800340 }
341
342 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000343 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000344 *
345 * @param childChain target chain
346 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
347 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000348 * @throws ServiceSpecificException in case of failure, with an error code indicating the
349 * cause of the failure.
350 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000351 public boolean isChainEnabled(final int childChain) {
352 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000353
354 final long match = getMatchByFirewallChain(childChain);
355 try {
356 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000357 return (config.val & match) != 0;
358 } catch (ErrnoException e) {
359 throw new ServiceSpecificException(e.errno,
360 "Unable to get firewall chain status: " + Os.strerror(e.errno));
361 }
362 }
363
364 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800365 * Replaces the contents of the specified UID-based firewall chain.
366 *
367 * The chain may be an allowlist chain or a denylist chain. A denylist chain contains DROP
368 * rules for the specified UIDs and a RETURN rule at the end. An allowlist chain contains RETURN
Ken Chenf5f51332022-01-28 10:08:16 +0800369 * rules for the system UID range (0 to {@code UID_APP} - 1), RETURN rules for the specified
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800370 * UIDs, and a DROP rule at the end. The chain will be created if it does not exist.
371 *
Ken Chenf5f51332022-01-28 10:08:16 +0800372 * @param chainName The name of the chain to replace.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800373 * @param isAllowlist Whether this is an allowlist or denylist chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800374 * @param uids The list of UIDs to allow/deny.
375 * @return 0 if the chain was successfully replaced, errno otherwise.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800376 */
377 public int replaceUidChain(final String chainName, final boolean isAllowlist,
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900378 final int[] uids) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000379 synchronized (sUidOwnerMap) {
380 final int err = native_replaceUidChain(chainName, isAllowlist, uids);
381 if (err != 0) {
382 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
383 }
384 return -err;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800385 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800386 }
387
Ken Chenf5f51332022-01-28 10:08:16 +0800388 /**
389 * Set firewall rule for uid
390 *
391 * @param childChain target chain
392 * @param uid uid to allow/deny
393 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800394 * @throws ServiceSpecificException in case of failure, with an error code indicating the
395 * cause of the failure.
396 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900397 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000398 synchronized (sUidOwnerMap) {
399 final int err = native_setUidRule(childChain, uid, firewallRule);
400 maybeThrow(err, "Unable to set uid rule");
401 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800402 }
403
404 /**
405 * Add ingress interface filtering rules to a list of UIDs
406 *
407 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
408 * allowed interface and loopback to be sent to the list of UIDs.
409 *
410 * Calling this method on one or more UIDs with an existing filtering rule but a different
411 * interface name will result in the filtering rule being updated to allow the new interface
412 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
413 *
414 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800415 * be received.
416 * @param uids an array of UIDs which the filtering rules will be set
417 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800418 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800419 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800420 */
Ken Chenf5f51332022-01-28 10:08:16 +0800421 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000422 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800423 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800424 return;
425 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000426 synchronized (sUidOwnerMap) {
427 final int err = native_addUidInterfaceRules(ifName, uids);
428 maybeThrow(err, "Unable to add uid interface rules");
429 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800430 }
431
432 /**
433 * Remove ingress interface filtering rules from a list of UIDs
434 *
435 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
436 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
437 *
438 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800439 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800440 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800441 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800442 */
Ken Chenf5f51332022-01-28 10:08:16 +0800443 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000444 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800445 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800446 return;
447 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000448 synchronized (sUidOwnerMap) {
449 final int err = native_removeUidInterfaceRules(uids);
450 maybeThrow(err, "Unable to remove uid interface rules");
451 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800452 }
453
Ken Chenf5f51332022-01-28 10:08:16 +0800454 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000455 * Update lockdown rule for uid
456 *
457 * @param uid target uid to add/remove the rule
458 * @param add {@code true} to add the rule, {@code false} to remove the rule.
459 * @throws ServiceSpecificException in case of failure, with an error code indicating the
460 * cause of the failure.
461 */
462 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000463 synchronized (sUidOwnerMap) {
464 final int err = native_updateUidLockdownRule(uid, add);
465 maybeThrow(err, "Unable to update lockdown rule");
466 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000467 }
468
469 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800470 * Request netd to change the current active network stats map.
471 *
Ken Chenf5f51332022-01-28 10:08:16 +0800472 * @throws ServiceSpecificException in case of failure, with an error code indicating the
473 * cause of the failure.
474 */
markchien49e944c2022-03-01 15:22:20 +0800475 public void swapActiveStatsMap() {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800476 final int err = native_swapActiveStatsMap();
Ken Chenf5f51332022-01-28 10:08:16 +0800477 maybeThrow(err, "Unable to swap active stats map");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800478 }
479
Ken Chenf5f51332022-01-28 10:08:16 +0800480 /**
481 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
482 * specified. Or remove all permissions from the uids.
483 *
484 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
485 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
486 * revoke all permissions for the uids.
487 * @param uids uid of users to grant permission
488 * @throws RemoteException when netd has crashed.
489 */
490 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000491 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800492 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800493 return;
494 }
495 native_setPermissionForUids(permissions, uids);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800496 }
497
Ken Chene6d511f2022-01-25 11:10:42 +0800498 /**
499 * Dump BPF maps
500 *
501 * @param fd file descriptor to output
502 * @throws IOException when file descriptor is invalid.
503 * @throws ServiceSpecificException when the method is called on an unsupported device.
504 */
505 public void dump(final FileDescriptor fd, boolean verbose)
506 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000507 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800508 throw new ServiceSpecificException(
509 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
510 + " devices, use dumpsys netd trafficcontroller instead.");
511 }
512 native_dump(fd, verbose);
513 }
514
Wayne Ma790c83e2022-01-13 10:35:05 +0800515 private static native void native_init();
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000516 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800517 private native int native_addNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000518 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800519 private native int native_removeNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000520 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800521 private native int native_addNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000522 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800523 private native int native_removeNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000524 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800525 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000526 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800527 private native int native_setUidRule(int childChain, int uid, int firewallRule);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000528 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800529 private native int native_addUidInterfaceRules(String ifName, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000530 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800531 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000532 @GuardedBy("sUidOwnerMap")
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000533 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800534 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800535 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800536 private native void native_dump(FileDescriptor fd, boolean verbose);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800537}