blob: 19c5a27b27a418492fc0270444ba28f4d2b3b574 [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 Utsumid95a0da2022-09-03 00:40:30 +0900228 native_init(!sEnableJavaBpfMap /* startSkDestroyListener */);
Ken Chenf5f51332022-01-28 10:08:16 +0800229 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800230 }
231
Motomu Utsumid95a0da2022-09-03 00:40:30 +0900232 public boolean isSkDestroyListenerRunning() {
233 return !sEnableJavaBpfMap;
234 }
235
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000236 /**
237 * Dependencies of BpfNetMaps, for injection in tests.
238 */
239 @VisibleForTesting
240 public static class Dependencies {
241 /**
242 * Get interface index.
243 */
244 public int getIfIndex(final String ifName) {
245 return Os.if_nametoindex(ifName);
246 }
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000247
248 /**
249 * Call synchronize_rcu()
250 */
251 public int synchronizeKernelRCU() {
252 return native_synchronizeKernelRCU();
253 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000254 }
255
markchien49e944c2022-03-01 15:22:20 +0800256 /** Constructor used after T that doesn't need to use netd anymore. */
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000257 public BpfNetMaps(final Context context) {
258 this(context, null);
markchien49e944c2022-03-01 15:22:20 +0800259
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000260 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800261 }
262
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000263 public BpfNetMaps(final Context context, final INetd netd) {
264 this(context, netd, new Dependencies());
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000265 }
266
267 @VisibleForTesting
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000268 public BpfNetMaps(final Context context, final INetd netd, final Dependencies deps) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000269 if (!PRE_T) {
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000270 ensureInitialized(context);
Motomu Utsumi305975f2022-06-27 09:24:32 +0000271 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800272 mNetd = netd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000273 mDeps = deps;
Wayne Ma790c83e2022-01-13 10:35:05 +0800274 }
275
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000276 /**
277 * Get corresponding match from firewall chain.
278 */
279 @VisibleForTesting
280 public long getMatchByFirewallChain(final int chain) {
Motomu Utsumi40230be2022-07-05 03:27:35 +0000281 switch (chain) {
282 case FIREWALL_CHAIN_DOZABLE:
283 return DOZABLE_MATCH;
284 case FIREWALL_CHAIN_STANDBY:
285 return STANDBY_MATCH;
286 case FIREWALL_CHAIN_POWERSAVE:
287 return POWERSAVE_MATCH;
288 case FIREWALL_CHAIN_RESTRICTED:
289 return RESTRICTED_MATCH;
290 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
291 return LOW_POWER_STANDBY_MATCH;
292 case FIREWALL_CHAIN_OEM_DENY_1:
293 return OEM_DENY_1_MATCH;
294 case FIREWALL_CHAIN_OEM_DENY_2:
295 return OEM_DENY_2_MATCH;
296 case FIREWALL_CHAIN_OEM_DENY_3:
297 return OEM_DENY_3_MATCH;
298 default:
299 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000300 }
Motomu Utsumi40230be2022-07-05 03:27:35 +0000301 }
302
303 /**
304 * Get if the chain is allow list or not.
305 *
306 * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed
307 * DENYLIST means the firewall allows all by default, uids must be explicitly denyed
308 */
309 @VisibleForTesting
310 public boolean isFirewallAllowList(final int chain) {
311 switch (chain) {
312 case FIREWALL_CHAIN_DOZABLE:
313 case FIREWALL_CHAIN_POWERSAVE:
314 case FIREWALL_CHAIN_RESTRICTED:
315 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
316 return true;
317 case FIREWALL_CHAIN_STANDBY:
318 case FIREWALL_CHAIN_OEM_DENY_1:
319 case FIREWALL_CHAIN_OEM_DENY_2:
320 case FIREWALL_CHAIN_OEM_DENY_3:
321 return false;
322 default:
323 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
324 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000325 }
326
Ken Chenf5f51332022-01-28 10:08:16 +0800327 private void maybeThrow(final int err, final String msg) {
328 if (err != 0) {
329 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
330 }
331 }
332
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000333 private void throwIfPreT(final String msg) {
334 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000335 throw new UnsupportedOperationException(msg);
336 }
337 }
338
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000339 private void removeRule(final int uid, final long match, final String caller) {
340 try {
341 synchronized (sUidOwnerMap) {
342 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
343
344 if (oldMatch == null) {
345 throw new ServiceSpecificException(ENOENT,
346 "sUidOwnerMap does not have entry for uid: " + uid);
347 }
348
349 final UidOwnerValue newMatch = new UidOwnerValue(
350 (match == IIF_MATCH) ? 0 : oldMatch.iif,
351 oldMatch.rule & ~match
352 );
353
354 if (newMatch.rule == 0) {
355 sUidOwnerMap.deleteEntry(new U32(uid));
356 } else {
357 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
358 }
359 }
360 } catch (ErrnoException e) {
361 throw new ServiceSpecificException(e.errno,
362 caller + " failed to remove rule: " + Os.strerror(e.errno));
363 }
364 }
365
Motomu Utsumi389278e2022-06-28 07:05:05 +0000366 private void addRule(final int uid, final long match, final long iif, final String caller) {
367 if (match != IIF_MATCH && iif != 0) {
368 throw new ServiceSpecificException(EINVAL,
369 "Non-interface match must have zero interface index");
370 }
371
372 try {
373 synchronized (sUidOwnerMap) {
374 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
375
376 final UidOwnerValue newMatch;
377 if (oldMatch != null) {
378 newMatch = new UidOwnerValue(
379 (match == IIF_MATCH) ? iif : oldMatch.iif,
380 oldMatch.rule | match
381 );
382 } else {
383 newMatch = new UidOwnerValue(
384 iif,
385 match
386 );
387 }
388 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
389 }
390 } catch (ErrnoException e) {
391 throw new ServiceSpecificException(e.errno,
392 caller + " failed to add rule: " + Os.strerror(e.errno));
393 }
394 }
395
396 private void addRule(final int uid, final long match, final String caller) {
397 addRule(uid, match, 0 /* iif */, caller);
398 }
399
Ken Chenf5f51332022-01-28 10:08:16 +0800400 /**
401 * Add naughty app bandwidth rule for specific app
402 *
403 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800404 * @throws ServiceSpecificException in case of failure, with an error code indicating the
405 * cause of the failure.
406 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900407 public void addNaughtyApp(final int uid) {
Motomu Utsumi389278e2022-06-28 07:05:05 +0000408 throwIfPreT("addNaughtyApp is not available on pre-T devices");
Motomu Utsumi55c282e2022-08-03 06:25:33 +0000409
410 if (sEnableJavaBpfMap) {
411 addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp");
412 } else {
413 final int err = native_addNaughtyApp(uid);
414 maybeThrow(err, "Unable to add naughty app");
415 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800416 }
417
Ken Chenf5f51332022-01-28 10:08:16 +0800418 /**
419 * Remove naughty app bandwidth rule for specific app
420 *
421 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800422 * @throws ServiceSpecificException in case of failure, with an error code indicating the
423 * cause of the failure.
424 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900425 public void removeNaughtyApp(final int uid) {
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000426 throwIfPreT("removeNaughtyApp is not available on pre-T devices");
Motomu Utsumi878ce0d2022-08-03 06:22:42 +0000427
428 if (sEnableJavaBpfMap) {
429 removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp");
430 } else {
431 final int err = native_removeNaughtyApp(uid);
432 maybeThrow(err, "Unable to remove naughty app");
433 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800434 }
435
Ken Chenf5f51332022-01-28 10:08:16 +0800436 /**
437 * Add nice app bandwidth rule for specific app
438 *
439 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800440 * @throws ServiceSpecificException in case of failure, with an error code indicating the
441 * cause of the failure.
442 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900443 public void addNiceApp(final int uid) {
Motomu Utsumi55630d02022-06-29 07:46:52 +0000444 throwIfPreT("addNiceApp is not available on pre-T devices");
Motomu Utsumi7f19df92022-08-03 06:29:59 +0000445
446 if (sEnableJavaBpfMap) {
447 addRule(uid, HAPPY_BOX_MATCH, "addNiceApp");
448 } else {
449 final int err = native_addNiceApp(uid);
450 maybeThrow(err, "Unable to add nice app");
451 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800452 }
453
Ken Chenf5f51332022-01-28 10:08:16 +0800454 /**
455 * Remove nice app bandwidth rule for specific app
456 *
457 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800458 * @throws ServiceSpecificException in case of failure, with an error code indicating the
459 * cause of the failure.
460 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900461 public void removeNiceApp(final int uid) {
Motomu Utsumi7392eb42022-06-29 03:53:03 +0000462 throwIfPreT("removeNiceApp is not available on pre-T devices");
Motomu Utsumi5f15f752022-08-03 06:27:51 +0000463
464 if (sEnableJavaBpfMap) {
465 removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp");
466 } else {
467 final int err = native_removeNiceApp(uid);
468 maybeThrow(err, "Unable to remove nice app");
469 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800470 }
471
Ken Chenf5f51332022-01-28 10:08:16 +0800472 /**
473 * Set target firewall child chain
474 *
475 * @param childChain target chain to enable
476 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000477 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800478 * @throws ServiceSpecificException in case of failure, with an error code indicating the
479 * cause of the failure.
480 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900481 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000482 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000483
Motomu Utsumie057dd42022-08-03 01:23:49 +0000484 if (sEnableJavaBpfMap) {
485 final long match = getMatchByFirewallChain(childChain);
486 try {
487 synchronized (sUidRulesConfigBpfMapLock) {
488 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
489 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
490 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
491 }
492 } catch (ErrnoException e) {
493 throw new ServiceSpecificException(e.errno,
494 "Unable to set child chain: " + Os.strerror(e.errno));
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000495 }
Motomu Utsumie057dd42022-08-03 01:23:49 +0000496 } else {
497 final int err = native_setChildChain(childChain, enable);
498 maybeThrow(err, "Unable to set child chain");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000499 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800500 }
501
502 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000503 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000504 *
505 * @param childChain target chain
506 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
507 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000508 * @throws ServiceSpecificException in case of failure, with an error code indicating the
509 * cause of the failure.
510 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000511 public boolean isChainEnabled(final int childChain) {
512 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000513
514 final long match = getMatchByFirewallChain(childChain);
515 try {
516 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000517 return (config.val & match) != 0;
518 } catch (ErrnoException e) {
519 throw new ServiceSpecificException(e.errno,
520 "Unable to get firewall chain status: " + Os.strerror(e.errno));
521 }
522 }
523
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900524 private Set<Integer> asSet(final int[] uids) {
525 final Set<Integer> uidSet = new ArraySet<>();
526 for (final int uid: uids) {
527 uidSet.add(uid);
528 }
529 return uidSet;
530 }
531
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000532 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800533 * Replaces the contents of the specified UID-based firewall chain.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000534 * Enables the chain for specified uids and disables the chain for non-specified uids.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800535 *
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000536 * @param chain Target chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800537 * @param uids The list of UIDs to allow/deny.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000538 * @throws UnsupportedOperationException if called on pre-T devices.
539 * @throws IllegalArgumentException if {@code chain} is not a valid chain.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800540 */
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000541 public void replaceUidChain(final int chain, final int[] uids) {
542 throwIfPreT("replaceUidChain is not available on pre-T devices");
543
Motomu Utsumic7c16852022-08-03 06:51:41 +0000544 if (sEnableJavaBpfMap) {
545 final long match;
546 try {
547 match = getMatchByFirewallChain(chain);
548 } catch (ServiceSpecificException e) {
549 // Throws IllegalArgumentException to keep the behavior of
550 // ConnectivityManager#replaceFirewallChain API
551 throw new IllegalArgumentException("Invalid firewall chain: " + chain);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000552 }
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900553 final Set<Integer> uidSet = asSet(uids);
554 final Set<Integer> uidSetToRemoveRule = new ArraySet<>();
Motomu Utsumic7c16852022-08-03 06:51:41 +0000555 try {
556 synchronized (sUidOwnerMap) {
557 sUidOwnerMap.forEach((uid, config) -> {
558 // config could be null if there is a concurrent entry deletion.
Motomu Utsumi8e420ea2022-08-24 18:03:30 +0900559 // http://b/220084230. But sUidOwnerMap update must be done while holding a
560 // lock, so this should not happen.
561 if (config == null) {
562 Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock");
563 } else if (!uidSet.contains((int) uid.val) && (config.rule & match) != 0) {
Motomu Utsumic7c16852022-08-03 06:51:41 +0000564 uidSetToRemoveRule.add((int) uid.val);
565 }
566 });
567
568 for (final int uid : uidSetToRemoveRule) {
569 removeRule(uid, match, "replaceUidChain");
570 }
571 for (final int uid : uids) {
572 addRule(uid, match, "replaceUidChain");
573 }
574 }
575 } catch (ErrnoException | ServiceSpecificException e) {
576 Log.e(TAG, "replaceUidChain failed: " + e);
577 }
578 } else {
579 final int err;
580 switch (chain) {
581 case FIREWALL_CHAIN_DOZABLE:
582 err = native_replaceUidChain("fw_dozable", true /* isAllowList */, uids);
583 break;
584 case FIREWALL_CHAIN_STANDBY:
585 err = native_replaceUidChain("fw_standby", false /* isAllowList */, uids);
586 break;
587 case FIREWALL_CHAIN_POWERSAVE:
588 err = native_replaceUidChain("fw_powersave", true /* isAllowList */, uids);
589 break;
590 case FIREWALL_CHAIN_RESTRICTED:
591 err = native_replaceUidChain("fw_restricted", true /* isAllowList */, uids);
592 break;
593 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
594 err = native_replaceUidChain(
595 "fw_low_power_standby", true /* isAllowList */, uids);
596 break;
597 case FIREWALL_CHAIN_OEM_DENY_1:
598 err = native_replaceUidChain("fw_oem_deny_1", false /* isAllowList */, uids);
599 break;
600 case FIREWALL_CHAIN_OEM_DENY_2:
601 err = native_replaceUidChain("fw_oem_deny_2", false /* isAllowList */, uids);
602 break;
603 case FIREWALL_CHAIN_OEM_DENY_3:
604 err = native_replaceUidChain("fw_oem_deny_3", false /* isAllowList */, uids);
605 break;
606 default:
607 throw new IllegalArgumentException("replaceFirewallChain with invalid chain: "
608 + chain);
609 }
610 if (err != 0) {
611 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
612 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800613 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800614 }
615
Ken Chenf5f51332022-01-28 10:08:16 +0800616 /**
617 * Set firewall rule for uid
618 *
619 * @param childChain target chain
620 * @param uid uid to allow/deny
621 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800622 * @throws ServiceSpecificException in case of failure, with an error code indicating the
623 * cause of the failure.
624 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900625 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi40230be2022-07-05 03:27:35 +0000626 throwIfPreT("setUidRule is not available on pre-T devices");
627
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000628 if (sEnableJavaBpfMap) {
629 final long match = getMatchByFirewallChain(childChain);
630 final boolean isAllowList = isFirewallAllowList(childChain);
631 final boolean add = (firewallRule == FIREWALL_RULE_ALLOW && isAllowList)
632 || (firewallRule == FIREWALL_RULE_DENY && !isAllowList);
Motomu Utsumi40230be2022-07-05 03:27:35 +0000633
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000634 if (add) {
635 addRule(uid, match, "setUidRule");
636 } else {
637 removeRule(uid, match, "setUidRule");
638 }
Motomu Utsumi40230be2022-07-05 03:27:35 +0000639 } else {
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000640 final int err = native_setUidRule(childChain, uid, firewallRule);
641 maybeThrow(err, "Unable to set uid rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000642 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800643 }
644
645 /**
646 * Add ingress interface filtering rules to a list of UIDs
647 *
648 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
649 * allowed interface and loopback to be sent to the list of UIDs.
650 *
651 * Calling this method on one or more UIDs with an existing filtering rule but a different
652 * interface name will result in the filtering rule being updated to allow the new interface
653 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
654 *
655 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800656 * be received.
657 * @param uids an array of UIDs which the filtering rules will be set
658 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800659 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800660 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800661 */
Ken Chenf5f51332022-01-28 10:08:16 +0800662 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000663 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800664 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800665 return;
666 }
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000667
668 if (sEnableJavaBpfMap) {
669 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and
670 // ifIndex is set to 0.
671 final int ifIndex;
672 if (ifName == null) {
673 ifIndex = 0;
674 } else {
675 ifIndex = mDeps.getIfIndex(ifName);
676 if (ifIndex == 0) {
677 throw new ServiceSpecificException(ENODEV,
678 "Failed to get index of interface " + ifName);
679 }
680 }
681 for (final int uid : uids) {
682 try {
683 addRule(uid, IIF_MATCH, ifIndex, "addUidInterfaceRules");
684 } catch (ServiceSpecificException e) {
685 Log.e(TAG, "addRule failed uid=" + uid + " ifName=" + ifName + ", " + e);
686 }
687 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000688 } else {
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000689 final int err = native_addUidInterfaceRules(ifName, uids);
690 maybeThrow(err, "Unable to add uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000691 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800692 }
693
694 /**
695 * Remove ingress interface filtering rules from a list of UIDs
696 *
697 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
698 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
699 *
700 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800701 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800702 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800703 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800704 */
Ken Chenf5f51332022-01-28 10:08:16 +0800705 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000706 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800707 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800708 return;
709 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000710
711 if (sEnableJavaBpfMap) {
712 for (final int uid : uids) {
713 try {
714 removeRule(uid, IIF_MATCH, "removeUidInterfaceRules");
715 } catch (ServiceSpecificException e) {
716 Log.e(TAG, "removeRule failed uid=" + uid + ", " + e);
717 }
Motomu Utsumi599c4e52022-06-30 03:37:18 +0000718 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000719 } else {
720 final int err = native_removeUidInterfaceRules(uids);
721 maybeThrow(err, "Unable to remove uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000722 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800723 }
724
Ken Chenf5f51332022-01-28 10:08:16 +0800725 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000726 * Update lockdown rule for uid
727 *
728 * @param uid target uid to add/remove the rule
729 * @param add {@code true} to add the rule, {@code false} to remove the rule.
730 * @throws ServiceSpecificException in case of failure, with an error code indicating the
731 * cause of the failure.
732 */
733 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi697b2992022-06-30 02:25:29 +0000734 throwIfPreT("updateUidLockdownRule is not available on pre-T devices");
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000735
736 if (sEnableJavaBpfMap) {
737 if (add) {
738 addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
739 } else {
740 removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
741 }
Motomu Utsumi697b2992022-06-30 02:25:29 +0000742 } else {
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000743 final int err = native_updateUidLockdownRule(uid, add);
744 maybeThrow(err, "Unable to update lockdown rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000745 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000746 }
747
748 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800749 * Request netd to change the current active network stats map.
750 *
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000751 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800752 * @throws ServiceSpecificException in case of failure, with an error code indicating the
753 * cause of the failure.
754 */
markchien49e944c2022-03-01 15:22:20 +0800755 public void swapActiveStatsMap() {
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000756 throwIfPreT("swapActiveStatsMap is not available on pre-T devices");
757
758 if (sEnableJavaBpfMap) {
759 try {
760 synchronized (sCurrentStatsMapConfigLock) {
761 final long config = sConfigurationMap.getValue(
762 CURRENT_STATS_MAP_CONFIGURATION_KEY).val;
763 final long newConfig = (config == STATS_SELECT_MAP_A)
764 ? STATS_SELECT_MAP_B : STATS_SELECT_MAP_A;
765 sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY,
766 new U32(newConfig));
767 }
768 } catch (ErrnoException e) {
769 throw new ServiceSpecificException(e.errno, "Failed to swap active stats map");
770 }
771
772 // After changing the config, it's needed to make sure all the current running eBPF
773 // programs are finished and all the CPUs are aware of this config change before the old
774 // map is modified. So special hack is needed here to wait for the kernel to do a
775 // synchronize_rcu(). Once the kernel called synchronize_rcu(), the updated config will
776 // be available to all cores and the next eBPF programs triggered inside the kernel will
777 // use the new map configuration. So once this function returns it is safe to modify the
778 // old stats map without concerning about race between the kernel and userspace.
779 final int err = mDeps.synchronizeKernelRCU();
780 maybeThrow(err, "synchronizeKernelRCU failed");
781 } else {
782 final int err = native_swapActiveStatsMap();
783 maybeThrow(err, "Unable to swap active stats map");
784 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800785 }
786
Ken Chenf5f51332022-01-28 10:08:16 +0800787 /**
788 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
789 * specified. Or remove all permissions from the uids.
790 *
791 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
792 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
793 * revoke all permissions for the uids.
794 * @param uids uid of users to grant permission
795 * @throws RemoteException when netd has crashed.
796 */
797 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000798 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800799 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800800 return;
801 }
Motomu Utsumi65271202022-07-05 08:21:41 +0000802
803 if (sEnableJavaBpfMap) {
804 // Remove the entry if package is uninstalled or uid has only INTERNET permission.
805 if (permissions == PERMISSION_UNINSTALLED || permissions == PERMISSION_INTERNET) {
806 for (final int uid : uids) {
807 try {
808 sUidPermissionMap.deleteEntry(new U32(uid));
809 } catch (ErrnoException e) {
810 Log.e(TAG, "Failed to remove uid " + uid + " from permission map: " + e);
811 }
812 }
813 return;
814 }
815
816 for (final int uid : uids) {
817 try {
818 sUidPermissionMap.updateEntry(new U32(uid), new U8((short) permissions));
819 } catch (ErrnoException e) {
820 Log.e(TAG, "Failed to set permission "
821 + permissions + " to uid " + uid + ": " + e);
822 }
823 }
824 } else {
825 native_setPermissionForUids(permissions, uids);
826 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800827 }
828
Ken Chene6d511f2022-01-25 11:10:42 +0800829 /**
830 * Dump BPF maps
831 *
832 * @param fd file descriptor to output
833 * @throws IOException when file descriptor is invalid.
834 * @throws ServiceSpecificException when the method is called on an unsupported device.
835 */
836 public void dump(final FileDescriptor fd, boolean verbose)
837 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000838 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800839 throw new ServiceSpecificException(
840 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
841 + " devices, use dumpsys netd trafficcontroller instead.");
842 }
843 native_dump(fd, verbose);
844 }
845
Motomu Utsumi3af8f0e2022-09-02 23:42:13 +0900846 private static native void native_init(boolean startSkDestroyListener);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800847 private native int native_addNaughtyApp(int uid);
848 private native int native_removeNaughtyApp(int uid);
849 private native int native_addNiceApp(int uid);
850 private native int native_removeNiceApp(int uid);
Motomu Utsumi114cd9c2022-08-01 02:08:35 +0000851 private native int native_setChildChain(int childChain, boolean enable);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800852 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
853 private native int native_setUidRule(int childChain, int uid, int firewallRule);
854 private native int native_addUidInterfaceRules(String ifName, int[] uids);
855 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000856 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800857 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800858 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800859 private native void native_dump(FileDescriptor fd, boolean verbose);
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000860 private static native int native_synchronizeKernelRCU();
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800861}