blob: dc5c4c75b0473d1aa5c3f755c61d38a079927fdb [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 Utsumi40230be2022-07-05 03:27:35 +000027import static android.net.ConnectivityManager.FIREWALL_RULE_ALLOW;
28import static android.net.ConnectivityManager.FIREWALL_RULE_DENY;
Motomu Utsumi65271202022-07-05 08:21:41 +000029import static android.net.INetd.PERMISSION_INTERNET;
30import static android.net.INetd.PERMISSION_UNINSTALLED;
Motomu Utsumi18b287d2022-06-19 10:45:30 +000031import static android.system.OsConstants.EINVAL;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +000032import static android.system.OsConstants.ENODEV;
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000033import static android.system.OsConstants.ENOENT;
Ken Chene6d511f2022-01-25 11:10:42 +080034import static android.system.OsConstants.EOPNOTSUPP;
35
Motomu Utsumif688eeb2022-07-22 03:47:35 +000036import android.content.Context;
Wayne Ma2fde98c2022-01-17 18:04:05 +080037import android.net.INetd;
38import android.os.RemoteException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080039import android.os.ServiceSpecificException;
Motomu Utsumif688eeb2022-07-22 03:47:35 +000040import android.provider.DeviceConfig;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000041import android.system.ErrnoException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080042import android.system.Os;
Motomu Utsumi1a477b02022-08-23 15:14:56 +090043import android.util.ArraySet;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080044import android.util.Log;
45
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000046import com.android.internal.annotations.VisibleForTesting;
Ken Chenf5f51332022-01-28 10:08:16 +080047import com.android.modules.utils.build.SdkLevel;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000048import com.android.net.module.util.BpfMap;
Motomu Utsumif688eeb2022-07-22 03:47:35 +000049import com.android.net.module.util.DeviceConfigUtils;
Motomu Utsumi73599a52022-08-24 11:59:21 +090050import com.android.net.module.util.IBpfMap;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000051import com.android.net.module.util.Struct.U32;
Motomu Utsumi65271202022-07-05 08:21:41 +000052import com.android.net.module.util.Struct.U8;
Ken Chenf5f51332022-01-28 10:08:16 +080053
Ken Chene6d511f2022-01-25 11:10:42 +080054import java.io.FileDescriptor;
55import java.io.IOException;
Motomu Utsumi9be2ea02022-07-05 06:14:59 +000056import java.util.Set;
Ken Chene6d511f2022-01-25 11:10:42 +080057
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080058/**
59 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
60 *
61 * {@hide}
62 */
63public class BpfNetMaps {
Motomu Utsumi305975f2022-06-27 09:24:32 +000064 private static final boolean PRE_T = !SdkLevel.isAtLeastT();
65 static {
66 if (!PRE_T) {
67 System.loadLibrary("service-connectivity");
68 }
69 }
70
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080071 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +080072 private final INetd mNetd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +000073 private final Dependencies mDeps;
Ken Chenf5f51332022-01-28 10:08:16 +080074 // Use legacy netd for releases before T.
Ken Chenf5f51332022-01-28 10:08:16 +080075 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080076
Motomu Utsumif688eeb2022-07-22 03:47:35 +000077 private static Boolean sEnableJavaBpfMap = null;
78 private static final String BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP =
79 "bpf_net_maps_enable_java_bpf_map";
80
Motomu Utsumi18b287d2022-06-19 10:45:30 +000081 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
82 // This entry is not accessed by others.
83 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
84 private static final Object sUidRulesConfigBpfMapLock = new Object();
85
Motomu Utsumi7abeaa42022-07-20 07:54:18 +000086 // Lock for sConfigurationMap entry for CURRENT_STATS_MAP_CONFIGURATION_KEY.
87 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
88 // BpfNetMaps is an only writer of this entry.
89 private static final Object sCurrentStatsMapConfigLock = new Object();
90
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000091 private static final String CONFIGURATION_MAP_PATH =
92 "/sys/fs/bpf/netd_shared/map_netd_configuration_map";
Motomu Utsumi5a68a212022-06-24 10:14:31 +000093 private static final String UID_OWNER_MAP_PATH =
94 "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map";
Motomu Utsumi65271202022-07-05 08:21:41 +000095 private static final String UID_PERMISSION_MAP_PATH =
96 "/sys/fs/bpf/netd_shared/map_netd_uid_permission_map";
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000097 private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0);
Motomu Utsumiba2fa152022-07-25 01:57:23 +000098 private static final U32 CURRENT_STATS_MAP_CONFIGURATION_KEY = new U32(1);
99 private static final long UID_RULES_DEFAULT_CONFIGURATION = 0;
100 private static final long STATS_SELECT_MAP_A = 0;
101 private static final long STATS_SELECT_MAP_B = 1;
102
Motomu Utsumi73599a52022-08-24 11:59:21 +0900103 private static IBpfMap<U32, U32> sConfigurationMap = null;
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000104 // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others.
Motomu Utsumi73599a52022-08-24 11:59:21 +0900105 private static IBpfMap<U32, UidOwnerValue> sUidOwnerMap = null;
106 private static IBpfMap<U32, U8> sUidPermissionMap = null;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000107
108 // LINT.IfChange(match_type)
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000109 @VisibleForTesting public static final long NO_MATCH = 0;
110 @VisibleForTesting public static final long HAPPY_BOX_MATCH = (1 << 0);
111 @VisibleForTesting public static final long PENALTY_BOX_MATCH = (1 << 1);
112 @VisibleForTesting public static final long DOZABLE_MATCH = (1 << 2);
113 @VisibleForTesting public static final long STANDBY_MATCH = (1 << 3);
114 @VisibleForTesting public static final long POWERSAVE_MATCH = (1 << 4);
115 @VisibleForTesting public static final long RESTRICTED_MATCH = (1 << 5);
116 @VisibleForTesting public static final long LOW_POWER_STANDBY_MATCH = (1 << 6);
117 @VisibleForTesting public static final long IIF_MATCH = (1 << 7);
118 @VisibleForTesting public static final long LOCKDOWN_VPN_MATCH = (1 << 8);
119 @VisibleForTesting public static final long OEM_DENY_1_MATCH = (1 << 9);
120 @VisibleForTesting public static final long OEM_DENY_2_MATCH = (1 << 10);
121 @VisibleForTesting public static final long OEM_DENY_3_MATCH = (1 << 11);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000122 // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h)
123
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000124 /**
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000125 * Set sEnableJavaBpfMap for test.
126 */
127 @VisibleForTesting
128 public static void setEnableJavaBpfMapForTest(boolean enable) {
129 sEnableJavaBpfMap = enable;
130 }
131
132 /**
Motomu Utsumi305975f2022-06-27 09:24:32 +0000133 * Set configurationMap for test.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000134 */
135 @VisibleForTesting
Motomu Utsumi73599a52022-08-24 11:59:21 +0900136 public static void setConfigurationMapForTest(IBpfMap<U32, U32> configurationMap) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000137 sConfigurationMap = configurationMap;
138 }
139
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000140 /**
141 * Set uidOwnerMap for test.
142 */
143 @VisibleForTesting
Motomu Utsumi73599a52022-08-24 11:59:21 +0900144 public static void setUidOwnerMapForTest(IBpfMap<U32, UidOwnerValue> uidOwnerMap) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000145 sUidOwnerMap = uidOwnerMap;
146 }
147
Motomu Utsumi65271202022-07-05 08:21:41 +0000148 /**
149 * Set uidPermissionMap for test.
150 */
151 @VisibleForTesting
Motomu Utsumi73599a52022-08-24 11:59:21 +0900152 public static void setUidPermissionMapForTest(IBpfMap<U32, U8> uidPermissionMap) {
Motomu Utsumi65271202022-07-05 08:21:41 +0000153 sUidPermissionMap = uidPermissionMap;
154 }
155
Motomu Utsumi73599a52022-08-24 11:59:21 +0900156 private static IBpfMap<U32, U32> getConfigurationMap() {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000157 try {
158 return new BpfMap<>(
159 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class);
160 } catch (ErrnoException e) {
161 throw new IllegalStateException("Cannot open netd configuration map", e);
162 }
163 }
164
Motomu Utsumi73599a52022-08-24 11:59:21 +0900165 private static IBpfMap<U32, UidOwnerValue> getUidOwnerMap() {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000166 try {
167 return new BpfMap<>(
168 UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class);
169 } catch (ErrnoException e) {
170 throw new IllegalStateException("Cannot open uid owner map", e);
171 }
172 }
173
Motomu Utsumi73599a52022-08-24 11:59:21 +0900174 private static IBpfMap<U32, U8> getUidPermissionMap() {
Motomu Utsumi65271202022-07-05 08:21:41 +0000175 try {
176 return new BpfMap<>(
177 UID_PERMISSION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U8.class);
178 } catch (ErrnoException e) {
179 throw new IllegalStateException("Cannot open uid permission map", e);
180 }
181 }
182
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000183 private static void initBpfMaps() {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000184 if (sConfigurationMap == null) {
185 sConfigurationMap = getConfigurationMap();
186 }
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000187 try {
188 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY,
189 new U32(UID_RULES_DEFAULT_CONFIGURATION));
190 } catch (ErrnoException e) {
191 throw new IllegalStateException("Failed to initialize uid rules configuration", e);
192 }
193 try {
194 sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY,
195 new U32(STATS_SELECT_MAP_A));
196 } catch (ErrnoException e) {
197 throw new IllegalStateException("Failed to initialize current stats configuration", e);
198 }
199
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000200 if (sUidOwnerMap == null) {
201 sUidOwnerMap = getUidOwnerMap();
202 }
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000203 try {
204 sUidOwnerMap.clear();
205 } catch (ErrnoException e) {
206 throw new IllegalStateException("Failed to initialize uid owner map", e);
207 }
Motomu Utsumi65271202022-07-05 08:21:41 +0000208
209 if (sUidPermissionMap == null) {
210 sUidPermissionMap = getUidPermissionMap();
211 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000212 }
213
Ken Chenf5f51332022-01-28 10:08:16 +0800214 /**
215 * Initializes the class if it is not already initialized. This method will open maps but not
216 * cause any other effects. This method may be called multiple times on any thread.
217 */
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000218 private static synchronized void ensureInitialized(final Context context) {
Ken Chenf5f51332022-01-28 10:08:16 +0800219 if (sInitialized) return;
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000220 if (sEnableJavaBpfMap == null) {
221 sEnableJavaBpfMap = DeviceConfigUtils.isFeatureEnabled(context,
222 DeviceConfig.NAMESPACE_TETHERING, BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP,
223 SdkLevel.isAtLeastU() /* defaultValue */);
224 }
225 Log.d(TAG, "BpfNetMaps is initialized with sEnableJavaBpfMap=" + sEnableJavaBpfMap);
226
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000227 initBpfMaps();
Motomu Utsumi305975f2022-06-27 09:24:32 +0000228 native_init();
Ken Chenf5f51332022-01-28 10:08:16 +0800229 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800230 }
231
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000232 /**
233 * Dependencies of BpfNetMaps, for injection in tests.
234 */
235 @VisibleForTesting
236 public static class Dependencies {
237 /**
238 * Get interface index.
239 */
240 public int getIfIndex(final String ifName) {
241 return Os.if_nametoindex(ifName);
242 }
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000243
244 /**
245 * Call synchronize_rcu()
246 */
247 public int synchronizeKernelRCU() {
248 return native_synchronizeKernelRCU();
249 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000250 }
251
markchien49e944c2022-03-01 15:22:20 +0800252 /** Constructor used after T that doesn't need to use netd anymore. */
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000253 public BpfNetMaps(final Context context) {
254 this(context, null);
markchien49e944c2022-03-01 15:22:20 +0800255
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000256 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800257 }
258
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000259 public BpfNetMaps(final Context context, final INetd netd) {
260 this(context, netd, new Dependencies());
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000261 }
262
263 @VisibleForTesting
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000264 public BpfNetMaps(final Context context, final INetd netd, final Dependencies deps) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000265 if (!PRE_T) {
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000266 ensureInitialized(context);
Motomu Utsumi305975f2022-06-27 09:24:32 +0000267 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800268 mNetd = netd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000269 mDeps = deps;
Wayne Ma790c83e2022-01-13 10:35:05 +0800270 }
271
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000272 /**
273 * Get corresponding match from firewall chain.
274 */
275 @VisibleForTesting
276 public long getMatchByFirewallChain(final int chain) {
Motomu Utsumi40230be2022-07-05 03:27:35 +0000277 switch (chain) {
278 case FIREWALL_CHAIN_DOZABLE:
279 return DOZABLE_MATCH;
280 case FIREWALL_CHAIN_STANDBY:
281 return STANDBY_MATCH;
282 case FIREWALL_CHAIN_POWERSAVE:
283 return POWERSAVE_MATCH;
284 case FIREWALL_CHAIN_RESTRICTED:
285 return RESTRICTED_MATCH;
286 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
287 return LOW_POWER_STANDBY_MATCH;
288 case FIREWALL_CHAIN_OEM_DENY_1:
289 return OEM_DENY_1_MATCH;
290 case FIREWALL_CHAIN_OEM_DENY_2:
291 return OEM_DENY_2_MATCH;
292 case FIREWALL_CHAIN_OEM_DENY_3:
293 return OEM_DENY_3_MATCH;
294 default:
295 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000296 }
Motomu Utsumi40230be2022-07-05 03:27:35 +0000297 }
298
299 /**
300 * Get if the chain is allow list or not.
301 *
302 * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed
303 * DENYLIST means the firewall allows all by default, uids must be explicitly denyed
304 */
305 @VisibleForTesting
306 public boolean isFirewallAllowList(final int chain) {
307 switch (chain) {
308 case FIREWALL_CHAIN_DOZABLE:
309 case FIREWALL_CHAIN_POWERSAVE:
310 case FIREWALL_CHAIN_RESTRICTED:
311 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
312 return true;
313 case FIREWALL_CHAIN_STANDBY:
314 case FIREWALL_CHAIN_OEM_DENY_1:
315 case FIREWALL_CHAIN_OEM_DENY_2:
316 case FIREWALL_CHAIN_OEM_DENY_3:
317 return false;
318 default:
319 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
320 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000321 }
322
Ken Chenf5f51332022-01-28 10:08:16 +0800323 private void maybeThrow(final int err, final String msg) {
324 if (err != 0) {
325 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
326 }
327 }
328
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000329 private void throwIfPreT(final String msg) {
330 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000331 throw new UnsupportedOperationException(msg);
332 }
333 }
334
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000335 private void removeRule(final int uid, final long match, final String caller) {
336 try {
337 synchronized (sUidOwnerMap) {
338 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
339
340 if (oldMatch == null) {
341 throw new ServiceSpecificException(ENOENT,
342 "sUidOwnerMap does not have entry for uid: " + uid);
343 }
344
345 final UidOwnerValue newMatch = new UidOwnerValue(
346 (match == IIF_MATCH) ? 0 : oldMatch.iif,
347 oldMatch.rule & ~match
348 );
349
350 if (newMatch.rule == 0) {
351 sUidOwnerMap.deleteEntry(new U32(uid));
352 } else {
353 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
354 }
355 }
356 } catch (ErrnoException e) {
357 throw new ServiceSpecificException(e.errno,
358 caller + " failed to remove rule: " + Os.strerror(e.errno));
359 }
360 }
361
Motomu Utsumi389278e2022-06-28 07:05:05 +0000362 private void addRule(final int uid, final long match, final long iif, final String caller) {
363 if (match != IIF_MATCH && iif != 0) {
364 throw new ServiceSpecificException(EINVAL,
365 "Non-interface match must have zero interface index");
366 }
367
368 try {
369 synchronized (sUidOwnerMap) {
370 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
371
372 final UidOwnerValue newMatch;
373 if (oldMatch != null) {
374 newMatch = new UidOwnerValue(
375 (match == IIF_MATCH) ? iif : oldMatch.iif,
376 oldMatch.rule | match
377 );
378 } else {
379 newMatch = new UidOwnerValue(
380 iif,
381 match
382 );
383 }
384 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
385 }
386 } catch (ErrnoException e) {
387 throw new ServiceSpecificException(e.errno,
388 caller + " failed to add rule: " + Os.strerror(e.errno));
389 }
390 }
391
392 private void addRule(final int uid, final long match, final String caller) {
393 addRule(uid, match, 0 /* iif */, caller);
394 }
395
Ken Chenf5f51332022-01-28 10:08:16 +0800396 /**
397 * Add naughty app bandwidth rule for specific app
398 *
399 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800400 * @throws ServiceSpecificException in case of failure, with an error code indicating the
401 * cause of the failure.
402 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900403 public void addNaughtyApp(final int uid) {
Motomu Utsumi389278e2022-06-28 07:05:05 +0000404 throwIfPreT("addNaughtyApp is not available on pre-T devices");
Motomu Utsumi55c282e2022-08-03 06:25:33 +0000405
406 if (sEnableJavaBpfMap) {
407 addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp");
408 } else {
409 final int err = native_addNaughtyApp(uid);
410 maybeThrow(err, "Unable to add naughty app");
411 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800412 }
413
Ken Chenf5f51332022-01-28 10:08:16 +0800414 /**
415 * Remove naughty app bandwidth rule for specific app
416 *
417 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800418 * @throws ServiceSpecificException in case of failure, with an error code indicating the
419 * cause of the failure.
420 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900421 public void removeNaughtyApp(final int uid) {
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000422 throwIfPreT("removeNaughtyApp is not available on pre-T devices");
Motomu Utsumi878ce0d2022-08-03 06:22:42 +0000423
424 if (sEnableJavaBpfMap) {
425 removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp");
426 } else {
427 final int err = native_removeNaughtyApp(uid);
428 maybeThrow(err, "Unable to remove naughty app");
429 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800430 }
431
Ken Chenf5f51332022-01-28 10:08:16 +0800432 /**
433 * Add nice app bandwidth rule for specific app
434 *
435 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800436 * @throws ServiceSpecificException in case of failure, with an error code indicating the
437 * cause of the failure.
438 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900439 public void addNiceApp(final int uid) {
Motomu Utsumi55630d02022-06-29 07:46:52 +0000440 throwIfPreT("addNiceApp is not available on pre-T devices");
Motomu Utsumi7f19df92022-08-03 06:29:59 +0000441
442 if (sEnableJavaBpfMap) {
443 addRule(uid, HAPPY_BOX_MATCH, "addNiceApp");
444 } else {
445 final int err = native_addNiceApp(uid);
446 maybeThrow(err, "Unable to add nice app");
447 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800448 }
449
Ken Chenf5f51332022-01-28 10:08:16 +0800450 /**
451 * Remove nice app bandwidth rule for specific app
452 *
453 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800454 * @throws ServiceSpecificException in case of failure, with an error code indicating the
455 * cause of the failure.
456 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900457 public void removeNiceApp(final int uid) {
Motomu Utsumi7392eb42022-06-29 03:53:03 +0000458 throwIfPreT("removeNiceApp is not available on pre-T devices");
Motomu Utsumi5f15f752022-08-03 06:27:51 +0000459
460 if (sEnableJavaBpfMap) {
461 removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp");
462 } else {
463 final int err = native_removeNiceApp(uid);
464 maybeThrow(err, "Unable to remove nice app");
465 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800466 }
467
Ken Chenf5f51332022-01-28 10:08:16 +0800468 /**
469 * Set target firewall child chain
470 *
471 * @param childChain target chain to enable
472 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000473 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800474 * @throws ServiceSpecificException in case of failure, with an error code indicating the
475 * cause of the failure.
476 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900477 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000478 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000479
Motomu Utsumie057dd42022-08-03 01:23:49 +0000480 if (sEnableJavaBpfMap) {
481 final long match = getMatchByFirewallChain(childChain);
482 try {
483 synchronized (sUidRulesConfigBpfMapLock) {
484 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
485 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
486 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
487 }
488 } catch (ErrnoException e) {
489 throw new ServiceSpecificException(e.errno,
490 "Unable to set child chain: " + Os.strerror(e.errno));
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000491 }
Motomu Utsumie057dd42022-08-03 01:23:49 +0000492 } else {
493 final int err = native_setChildChain(childChain, enable);
494 maybeThrow(err, "Unable to set child chain");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000495 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800496 }
497
498 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000499 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000500 *
501 * @param childChain target chain
502 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
503 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000504 * @throws ServiceSpecificException in case of failure, with an error code indicating the
505 * cause of the failure.
506 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000507 public boolean isChainEnabled(final int childChain) {
508 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000509
510 final long match = getMatchByFirewallChain(childChain);
511 try {
512 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000513 return (config.val & match) != 0;
514 } catch (ErrnoException e) {
515 throw new ServiceSpecificException(e.errno,
516 "Unable to get firewall chain status: " + Os.strerror(e.errno));
517 }
518 }
519
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900520 private Set<Integer> asSet(final int[] uids) {
521 final Set<Integer> uidSet = new ArraySet<>();
522 for (final int uid: uids) {
523 uidSet.add(uid);
524 }
525 return uidSet;
526 }
527
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000528 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800529 * Replaces the contents of the specified UID-based firewall chain.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000530 * Enables the chain for specified uids and disables the chain for non-specified uids.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800531 *
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000532 * @param chain Target chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800533 * @param uids The list of UIDs to allow/deny.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000534 * @throws UnsupportedOperationException if called on pre-T devices.
535 * @throws IllegalArgumentException if {@code chain} is not a valid chain.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800536 */
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000537 public void replaceUidChain(final int chain, final int[] uids) {
538 throwIfPreT("replaceUidChain is not available on pre-T devices");
539
Motomu Utsumic7c16852022-08-03 06:51:41 +0000540 if (sEnableJavaBpfMap) {
541 final long match;
542 try {
543 match = getMatchByFirewallChain(chain);
544 } catch (ServiceSpecificException e) {
545 // Throws IllegalArgumentException to keep the behavior of
546 // ConnectivityManager#replaceFirewallChain API
547 throw new IllegalArgumentException("Invalid firewall chain: " + chain);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000548 }
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900549 final Set<Integer> uidSet = asSet(uids);
550 final Set<Integer> uidSetToRemoveRule = new ArraySet<>();
Motomu Utsumic7c16852022-08-03 06:51:41 +0000551 try {
552 synchronized (sUidOwnerMap) {
553 sUidOwnerMap.forEach((uid, config) -> {
554 // config could be null if there is a concurrent entry deletion.
Motomu Utsumi8e420ea2022-08-24 18:03:30 +0900555 // http://b/220084230. But sUidOwnerMap update must be done while holding a
556 // lock, so this should not happen.
557 if (config == null) {
558 Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock");
559 } else if (!uidSet.contains((int) uid.val) && (config.rule & match) != 0) {
Motomu Utsumic7c16852022-08-03 06:51:41 +0000560 uidSetToRemoveRule.add((int) uid.val);
561 }
562 });
563
564 for (final int uid : uidSetToRemoveRule) {
565 removeRule(uid, match, "replaceUidChain");
566 }
567 for (final int uid : uids) {
568 addRule(uid, match, "replaceUidChain");
569 }
570 }
571 } catch (ErrnoException | ServiceSpecificException e) {
572 Log.e(TAG, "replaceUidChain failed: " + e);
573 }
574 } else {
575 final int err;
576 switch (chain) {
577 case FIREWALL_CHAIN_DOZABLE:
578 err = native_replaceUidChain("fw_dozable", true /* isAllowList */, uids);
579 break;
580 case FIREWALL_CHAIN_STANDBY:
581 err = native_replaceUidChain("fw_standby", false /* isAllowList */, uids);
582 break;
583 case FIREWALL_CHAIN_POWERSAVE:
584 err = native_replaceUidChain("fw_powersave", true /* isAllowList */, uids);
585 break;
586 case FIREWALL_CHAIN_RESTRICTED:
587 err = native_replaceUidChain("fw_restricted", true /* isAllowList */, uids);
588 break;
589 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
590 err = native_replaceUidChain(
591 "fw_low_power_standby", true /* isAllowList */, uids);
592 break;
593 case FIREWALL_CHAIN_OEM_DENY_1:
594 err = native_replaceUidChain("fw_oem_deny_1", false /* isAllowList */, uids);
595 break;
596 case FIREWALL_CHAIN_OEM_DENY_2:
597 err = native_replaceUidChain("fw_oem_deny_2", false /* isAllowList */, uids);
598 break;
599 case FIREWALL_CHAIN_OEM_DENY_3:
600 err = native_replaceUidChain("fw_oem_deny_3", false /* isAllowList */, uids);
601 break;
602 default:
603 throw new IllegalArgumentException("replaceFirewallChain with invalid chain: "
604 + chain);
605 }
606 if (err != 0) {
607 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
608 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800609 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800610 }
611
Ken Chenf5f51332022-01-28 10:08:16 +0800612 /**
613 * Set firewall rule for uid
614 *
615 * @param childChain target chain
616 * @param uid uid to allow/deny
617 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800618 * @throws ServiceSpecificException in case of failure, with an error code indicating the
619 * cause of the failure.
620 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900621 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi40230be2022-07-05 03:27:35 +0000622 throwIfPreT("setUidRule is not available on pre-T devices");
623
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000624 if (sEnableJavaBpfMap) {
625 final long match = getMatchByFirewallChain(childChain);
626 final boolean isAllowList = isFirewallAllowList(childChain);
627 final boolean add = (firewallRule == FIREWALL_RULE_ALLOW && isAllowList)
628 || (firewallRule == FIREWALL_RULE_DENY && !isAllowList);
Motomu Utsumi40230be2022-07-05 03:27:35 +0000629
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000630 if (add) {
631 addRule(uid, match, "setUidRule");
632 } else {
633 removeRule(uid, match, "setUidRule");
634 }
Motomu Utsumi40230be2022-07-05 03:27:35 +0000635 } else {
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000636 final int err = native_setUidRule(childChain, uid, firewallRule);
637 maybeThrow(err, "Unable to set uid rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000638 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800639 }
640
641 /**
642 * Add ingress interface filtering rules to a list of UIDs
643 *
644 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
645 * allowed interface and loopback to be sent to the list of UIDs.
646 *
647 * Calling this method on one or more UIDs with an existing filtering rule but a different
648 * interface name will result in the filtering rule being updated to allow the new interface
649 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
650 *
651 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800652 * be received.
653 * @param uids an array of UIDs which the filtering rules will be set
654 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800655 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800656 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800657 */
Ken Chenf5f51332022-01-28 10:08:16 +0800658 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000659 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800660 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800661 return;
662 }
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000663
664 if (sEnableJavaBpfMap) {
665 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and
666 // ifIndex is set to 0.
667 final int ifIndex;
668 if (ifName == null) {
669 ifIndex = 0;
670 } else {
671 ifIndex = mDeps.getIfIndex(ifName);
672 if (ifIndex == 0) {
673 throw new ServiceSpecificException(ENODEV,
674 "Failed to get index of interface " + ifName);
675 }
676 }
677 for (final int uid : uids) {
678 try {
679 addRule(uid, IIF_MATCH, ifIndex, "addUidInterfaceRules");
680 } catch (ServiceSpecificException e) {
681 Log.e(TAG, "addRule failed uid=" + uid + " ifName=" + ifName + ", " + e);
682 }
683 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000684 } else {
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000685 final int err = native_addUidInterfaceRules(ifName, uids);
686 maybeThrow(err, "Unable to add uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000687 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800688 }
689
690 /**
691 * Remove ingress interface filtering rules from a list of UIDs
692 *
693 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
694 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
695 *
696 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800697 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800698 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800699 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800700 */
Ken Chenf5f51332022-01-28 10:08:16 +0800701 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000702 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800703 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800704 return;
705 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000706
707 if (sEnableJavaBpfMap) {
708 for (final int uid : uids) {
709 try {
710 removeRule(uid, IIF_MATCH, "removeUidInterfaceRules");
711 } catch (ServiceSpecificException e) {
712 Log.e(TAG, "removeRule failed uid=" + uid + ", " + e);
713 }
Motomu Utsumi599c4e52022-06-30 03:37:18 +0000714 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000715 } else {
716 final int err = native_removeUidInterfaceRules(uids);
717 maybeThrow(err, "Unable to remove uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000718 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800719 }
720
Ken Chenf5f51332022-01-28 10:08:16 +0800721 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000722 * Update lockdown rule for uid
723 *
724 * @param uid target uid to add/remove the rule
725 * @param add {@code true} to add the rule, {@code false} to remove the rule.
726 * @throws ServiceSpecificException in case of failure, with an error code indicating the
727 * cause of the failure.
728 */
729 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi697b2992022-06-30 02:25:29 +0000730 throwIfPreT("updateUidLockdownRule is not available on pre-T devices");
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000731
732 if (sEnableJavaBpfMap) {
733 if (add) {
734 addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
735 } else {
736 removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
737 }
Motomu Utsumi697b2992022-06-30 02:25:29 +0000738 } else {
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000739 final int err = native_updateUidLockdownRule(uid, add);
740 maybeThrow(err, "Unable to update lockdown rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000741 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000742 }
743
744 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800745 * Request netd to change the current active network stats map.
746 *
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000747 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800748 * @throws ServiceSpecificException in case of failure, with an error code indicating the
749 * cause of the failure.
750 */
markchien49e944c2022-03-01 15:22:20 +0800751 public void swapActiveStatsMap() {
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000752 throwIfPreT("swapActiveStatsMap is not available on pre-T devices");
753
754 if (sEnableJavaBpfMap) {
755 try {
756 synchronized (sCurrentStatsMapConfigLock) {
757 final long config = sConfigurationMap.getValue(
758 CURRENT_STATS_MAP_CONFIGURATION_KEY).val;
759 final long newConfig = (config == STATS_SELECT_MAP_A)
760 ? STATS_SELECT_MAP_B : STATS_SELECT_MAP_A;
761 sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY,
762 new U32(newConfig));
763 }
764 } catch (ErrnoException e) {
765 throw new ServiceSpecificException(e.errno, "Failed to swap active stats map");
766 }
767
768 // After changing the config, it's needed to make sure all the current running eBPF
769 // programs are finished and all the CPUs are aware of this config change before the old
770 // map is modified. So special hack is needed here to wait for the kernel to do a
771 // synchronize_rcu(). Once the kernel called synchronize_rcu(), the updated config will
772 // be available to all cores and the next eBPF programs triggered inside the kernel will
773 // use the new map configuration. So once this function returns it is safe to modify the
774 // old stats map without concerning about race between the kernel and userspace.
775 final int err = mDeps.synchronizeKernelRCU();
776 maybeThrow(err, "synchronizeKernelRCU failed");
777 } else {
778 final int err = native_swapActiveStatsMap();
779 maybeThrow(err, "Unable to swap active stats map");
780 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800781 }
782
Ken Chenf5f51332022-01-28 10:08:16 +0800783 /**
784 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
785 * specified. Or remove all permissions from the uids.
786 *
787 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
788 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
789 * revoke all permissions for the uids.
790 * @param uids uid of users to grant permission
791 * @throws RemoteException when netd has crashed.
792 */
793 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000794 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800795 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800796 return;
797 }
Motomu Utsumi65271202022-07-05 08:21:41 +0000798
799 if (sEnableJavaBpfMap) {
800 // Remove the entry if package is uninstalled or uid has only INTERNET permission.
801 if (permissions == PERMISSION_UNINSTALLED || permissions == PERMISSION_INTERNET) {
802 for (final int uid : uids) {
803 try {
804 sUidPermissionMap.deleteEntry(new U32(uid));
805 } catch (ErrnoException e) {
806 Log.e(TAG, "Failed to remove uid " + uid + " from permission map: " + e);
807 }
808 }
809 return;
810 }
811
812 for (final int uid : uids) {
813 try {
814 sUidPermissionMap.updateEntry(new U32(uid), new U8((short) permissions));
815 } catch (ErrnoException e) {
816 Log.e(TAG, "Failed to set permission "
817 + permissions + " to uid " + uid + ": " + e);
818 }
819 }
820 } else {
821 native_setPermissionForUids(permissions, uids);
822 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800823 }
824
Ken Chene6d511f2022-01-25 11:10:42 +0800825 /**
826 * Dump BPF maps
827 *
828 * @param fd file descriptor to output
829 * @throws IOException when file descriptor is invalid.
830 * @throws ServiceSpecificException when the method is called on an unsupported device.
831 */
832 public void dump(final FileDescriptor fd, boolean verbose)
833 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000834 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800835 throw new ServiceSpecificException(
836 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
837 + " devices, use dumpsys netd trafficcontroller instead.");
838 }
839 native_dump(fd, verbose);
840 }
841
Wayne Ma790c83e2022-01-13 10:35:05 +0800842 private static native void native_init();
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800843 private native int native_addNaughtyApp(int uid);
844 private native int native_removeNaughtyApp(int uid);
845 private native int native_addNiceApp(int uid);
846 private native int native_removeNiceApp(int uid);
Motomu Utsumi114cd9c2022-08-01 02:08:35 +0000847 private native int native_setChildChain(int childChain, boolean enable);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800848 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
849 private native int native_setUidRule(int childChain, int uid, int firewallRule);
850 private native int native_addUidInterfaceRules(String ifName, int[] uids);
851 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000852 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800853 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800854 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800855 private native void native_dump(FileDescriptor fd, boolean verbose);
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000856 private static native int native_synchronizeKernelRCU();
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800857}