blob: 4eeef5c558cb0e896a2f266f9de567aa7a25df6d [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;
Motomu Utsumib9548862022-09-06 16:30:05 +090053import com.android.net.module.util.bpf.CookieTagMapKey;
54import com.android.net.module.util.bpf.CookieTagMapValue;
Ken Chenf5f51332022-01-28 10:08:16 +080055
Ken Chene6d511f2022-01-25 11:10:42 +080056import java.io.FileDescriptor;
57import java.io.IOException;
Motomu Utsumi9be2ea02022-07-05 06:14:59 +000058import java.util.Set;
Ken Chene6d511f2022-01-25 11:10:42 +080059
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080060/**
61 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
62 *
63 * {@hide}
64 */
65public class BpfNetMaps {
Motomu Utsumi305975f2022-06-27 09:24:32 +000066 private static final boolean PRE_T = !SdkLevel.isAtLeastT();
67 static {
68 if (!PRE_T) {
69 System.loadLibrary("service-connectivity");
70 }
71 }
72
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080073 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +080074 private final INetd mNetd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +000075 private final Dependencies mDeps;
Ken Chenf5f51332022-01-28 10:08:16 +080076 // Use legacy netd for releases before T.
Ken Chenf5f51332022-01-28 10:08:16 +080077 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080078
Motomu Utsumif688eeb2022-07-22 03:47:35 +000079 private static Boolean sEnableJavaBpfMap = null;
80 private static final String BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP =
81 "bpf_net_maps_enable_java_bpf_map";
82
Motomu Utsumi18b287d2022-06-19 10:45:30 +000083 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
84 // This entry is not accessed by others.
85 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
86 private static final Object sUidRulesConfigBpfMapLock = new Object();
87
Motomu Utsumi7abeaa42022-07-20 07:54:18 +000088 // Lock for sConfigurationMap entry for CURRENT_STATS_MAP_CONFIGURATION_KEY.
89 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
90 // BpfNetMaps is an only writer of this entry.
91 private static final Object sCurrentStatsMapConfigLock = new Object();
92
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000093 private static final String CONFIGURATION_MAP_PATH =
94 "/sys/fs/bpf/netd_shared/map_netd_configuration_map";
Motomu Utsumi5a68a212022-06-24 10:14:31 +000095 private static final String UID_OWNER_MAP_PATH =
96 "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map";
Motomu Utsumi65271202022-07-05 08:21:41 +000097 private static final String UID_PERMISSION_MAP_PATH =
98 "/sys/fs/bpf/netd_shared/map_netd_uid_permission_map";
Motomu Utsumib9548862022-09-06 16:30:05 +090099 private static final String COOKIE_TAG_MAP_PATH =
100 "/sys/fs/bpf/netd_shared/map_netd_cookie_tag_map";
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000101 private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0);
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000102 private static final U32 CURRENT_STATS_MAP_CONFIGURATION_KEY = new U32(1);
103 private static final long UID_RULES_DEFAULT_CONFIGURATION = 0;
104 private static final long STATS_SELECT_MAP_A = 0;
105 private static final long STATS_SELECT_MAP_B = 1;
106
Motomu Utsumi73599a52022-08-24 11:59:21 +0900107 private static IBpfMap<U32, U32> sConfigurationMap = null;
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000108 // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others.
Motomu Utsumi73599a52022-08-24 11:59:21 +0900109 private static IBpfMap<U32, UidOwnerValue> sUidOwnerMap = null;
110 private static IBpfMap<U32, U8> sUidPermissionMap = null;
Motomu Utsumib9548862022-09-06 16:30:05 +0900111 private static IBpfMap<CookieTagMapKey, CookieTagMapValue> sCookieTagMap = null;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000112
113 // LINT.IfChange(match_type)
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000114 @VisibleForTesting public static final long NO_MATCH = 0;
115 @VisibleForTesting public static final long HAPPY_BOX_MATCH = (1 << 0);
116 @VisibleForTesting public static final long PENALTY_BOX_MATCH = (1 << 1);
117 @VisibleForTesting public static final long DOZABLE_MATCH = (1 << 2);
118 @VisibleForTesting public static final long STANDBY_MATCH = (1 << 3);
119 @VisibleForTesting public static final long POWERSAVE_MATCH = (1 << 4);
120 @VisibleForTesting public static final long RESTRICTED_MATCH = (1 << 5);
121 @VisibleForTesting public static final long LOW_POWER_STANDBY_MATCH = (1 << 6);
122 @VisibleForTesting public static final long IIF_MATCH = (1 << 7);
123 @VisibleForTesting public static final long LOCKDOWN_VPN_MATCH = (1 << 8);
124 @VisibleForTesting public static final long OEM_DENY_1_MATCH = (1 << 9);
125 @VisibleForTesting public static final long OEM_DENY_2_MATCH = (1 << 10);
126 @VisibleForTesting public static final long OEM_DENY_3_MATCH = (1 << 11);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000127 // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h)
128
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000129 /**
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000130 * Set sEnableJavaBpfMap for test.
131 */
132 @VisibleForTesting
133 public static void setEnableJavaBpfMapForTest(boolean enable) {
134 sEnableJavaBpfMap = enable;
135 }
136
137 /**
Motomu Utsumi305975f2022-06-27 09:24:32 +0000138 * Set configurationMap for test.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000139 */
140 @VisibleForTesting
Motomu Utsumi73599a52022-08-24 11:59:21 +0900141 public static void setConfigurationMapForTest(IBpfMap<U32, U32> configurationMap) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000142 sConfigurationMap = configurationMap;
143 }
144
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000145 /**
146 * Set uidOwnerMap for test.
147 */
148 @VisibleForTesting
Motomu Utsumi73599a52022-08-24 11:59:21 +0900149 public static void setUidOwnerMapForTest(IBpfMap<U32, UidOwnerValue> uidOwnerMap) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000150 sUidOwnerMap = uidOwnerMap;
151 }
152
Motomu Utsumi65271202022-07-05 08:21:41 +0000153 /**
154 * Set uidPermissionMap for test.
155 */
156 @VisibleForTesting
Motomu Utsumi73599a52022-08-24 11:59:21 +0900157 public static void setUidPermissionMapForTest(IBpfMap<U32, U8> uidPermissionMap) {
Motomu Utsumi65271202022-07-05 08:21:41 +0000158 sUidPermissionMap = uidPermissionMap;
159 }
160
Motomu Utsumib9548862022-09-06 16:30:05 +0900161 /**
162 * Set cookieTagMap for test.
163 */
164 @VisibleForTesting
165 public static void setCookieTagMapForTest(
166 IBpfMap<CookieTagMapKey, CookieTagMapValue> cookieTagMap) {
167 sCookieTagMap = cookieTagMap;
168 }
169
Motomu Utsumi73599a52022-08-24 11:59:21 +0900170 private static IBpfMap<U32, U32> getConfigurationMap() {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000171 try {
172 return new BpfMap<>(
173 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class);
174 } catch (ErrnoException e) {
175 throw new IllegalStateException("Cannot open netd configuration map", e);
176 }
177 }
178
Motomu Utsumi73599a52022-08-24 11:59:21 +0900179 private static IBpfMap<U32, UidOwnerValue> getUidOwnerMap() {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000180 try {
181 return new BpfMap<>(
182 UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class);
183 } catch (ErrnoException e) {
184 throw new IllegalStateException("Cannot open uid owner map", e);
185 }
186 }
187
Motomu Utsumi73599a52022-08-24 11:59:21 +0900188 private static IBpfMap<U32, U8> getUidPermissionMap() {
Motomu Utsumi65271202022-07-05 08:21:41 +0000189 try {
190 return new BpfMap<>(
191 UID_PERMISSION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U8.class);
192 } catch (ErrnoException e) {
193 throw new IllegalStateException("Cannot open uid permission map", e);
194 }
195 }
196
Motomu Utsumib9548862022-09-06 16:30:05 +0900197 private static IBpfMap<CookieTagMapKey, CookieTagMapValue> getCookieTagMap() {
198 try {
199 return new BpfMap<>(COOKIE_TAG_MAP_PATH, BpfMap.BPF_F_RDWR,
200 CookieTagMapKey.class, CookieTagMapValue.class);
201 } catch (ErrnoException e) {
202 throw new IllegalStateException("Cannot open cookie tag map", e);
203 }
204 }
205
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000206 private static void initBpfMaps() {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000207 if (sConfigurationMap == null) {
208 sConfigurationMap = getConfigurationMap();
209 }
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000210 try {
211 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY,
212 new U32(UID_RULES_DEFAULT_CONFIGURATION));
213 } catch (ErrnoException e) {
214 throw new IllegalStateException("Failed to initialize uid rules configuration", e);
215 }
216 try {
217 sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY,
218 new U32(STATS_SELECT_MAP_A));
219 } catch (ErrnoException e) {
220 throw new IllegalStateException("Failed to initialize current stats configuration", e);
221 }
222
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000223 if (sUidOwnerMap == null) {
224 sUidOwnerMap = getUidOwnerMap();
225 }
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000226 try {
227 sUidOwnerMap.clear();
228 } catch (ErrnoException e) {
229 throw new IllegalStateException("Failed to initialize uid owner map", e);
230 }
Motomu Utsumi65271202022-07-05 08:21:41 +0000231
232 if (sUidPermissionMap == null) {
233 sUidPermissionMap = getUidPermissionMap();
234 }
Motomu Utsumib9548862022-09-06 16:30:05 +0900235
236 if (sCookieTagMap == null) {
237 sCookieTagMap = getCookieTagMap();
238 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000239 }
240
Ken Chenf5f51332022-01-28 10:08:16 +0800241 /**
242 * Initializes the class if it is not already initialized. This method will open maps but not
243 * cause any other effects. This method may be called multiple times on any thread.
244 */
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000245 private static synchronized void ensureInitialized(final Context context) {
Ken Chenf5f51332022-01-28 10:08:16 +0800246 if (sInitialized) return;
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000247 if (sEnableJavaBpfMap == null) {
248 sEnableJavaBpfMap = DeviceConfigUtils.isFeatureEnabled(context,
249 DeviceConfig.NAMESPACE_TETHERING, BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP,
250 SdkLevel.isAtLeastU() /* defaultValue */);
251 }
252 Log.d(TAG, "BpfNetMaps is initialized with sEnableJavaBpfMap=" + sEnableJavaBpfMap);
253
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000254 initBpfMaps();
Motomu Utsumi305975f2022-06-27 09:24:32 +0000255 native_init();
Ken Chenf5f51332022-01-28 10:08:16 +0800256 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800257 }
258
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000259 /**
260 * Dependencies of BpfNetMaps, for injection in tests.
261 */
262 @VisibleForTesting
263 public static class Dependencies {
264 /**
265 * Get interface index.
266 */
267 public int getIfIndex(final String ifName) {
268 return Os.if_nametoindex(ifName);
269 }
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000270
271 /**
272 * Call synchronize_rcu()
273 */
274 public int synchronizeKernelRCU() {
275 return native_synchronizeKernelRCU();
276 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000277 }
278
markchien49e944c2022-03-01 15:22:20 +0800279 /** Constructor used after T that doesn't need to use netd anymore. */
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000280 public BpfNetMaps(final Context context) {
281 this(context, null);
markchien49e944c2022-03-01 15:22:20 +0800282
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000283 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800284 }
285
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000286 public BpfNetMaps(final Context context, final INetd netd) {
287 this(context, netd, new Dependencies());
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000288 }
289
290 @VisibleForTesting
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000291 public BpfNetMaps(final Context context, final INetd netd, final Dependencies deps) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000292 if (!PRE_T) {
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000293 ensureInitialized(context);
Motomu Utsumi305975f2022-06-27 09:24:32 +0000294 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800295 mNetd = netd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000296 mDeps = deps;
Wayne Ma790c83e2022-01-13 10:35:05 +0800297 }
298
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000299 /**
300 * Get corresponding match from firewall chain.
301 */
302 @VisibleForTesting
303 public long getMatchByFirewallChain(final int chain) {
Motomu Utsumi40230be2022-07-05 03:27:35 +0000304 switch (chain) {
305 case FIREWALL_CHAIN_DOZABLE:
306 return DOZABLE_MATCH;
307 case FIREWALL_CHAIN_STANDBY:
308 return STANDBY_MATCH;
309 case FIREWALL_CHAIN_POWERSAVE:
310 return POWERSAVE_MATCH;
311 case FIREWALL_CHAIN_RESTRICTED:
312 return RESTRICTED_MATCH;
313 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
314 return LOW_POWER_STANDBY_MATCH;
315 case FIREWALL_CHAIN_OEM_DENY_1:
316 return OEM_DENY_1_MATCH;
317 case FIREWALL_CHAIN_OEM_DENY_2:
318 return OEM_DENY_2_MATCH;
319 case FIREWALL_CHAIN_OEM_DENY_3:
320 return OEM_DENY_3_MATCH;
321 default:
322 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000323 }
Motomu Utsumi40230be2022-07-05 03:27:35 +0000324 }
325
326 /**
327 * Get if the chain is allow list or not.
328 *
329 * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed
330 * DENYLIST means the firewall allows all by default, uids must be explicitly denyed
331 */
332 @VisibleForTesting
333 public boolean isFirewallAllowList(final int chain) {
334 switch (chain) {
335 case FIREWALL_CHAIN_DOZABLE:
336 case FIREWALL_CHAIN_POWERSAVE:
337 case FIREWALL_CHAIN_RESTRICTED:
338 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
339 return true;
340 case FIREWALL_CHAIN_STANDBY:
341 case FIREWALL_CHAIN_OEM_DENY_1:
342 case FIREWALL_CHAIN_OEM_DENY_2:
343 case FIREWALL_CHAIN_OEM_DENY_3:
344 return false;
345 default:
346 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
347 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000348 }
349
Ken Chenf5f51332022-01-28 10:08:16 +0800350 private void maybeThrow(final int err, final String msg) {
351 if (err != 0) {
352 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
353 }
354 }
355
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000356 private void throwIfPreT(final String msg) {
357 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000358 throw new UnsupportedOperationException(msg);
359 }
360 }
361
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000362 private void removeRule(final int uid, final long match, final String caller) {
363 try {
364 synchronized (sUidOwnerMap) {
365 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
366
367 if (oldMatch == null) {
368 throw new ServiceSpecificException(ENOENT,
369 "sUidOwnerMap does not have entry for uid: " + uid);
370 }
371
372 final UidOwnerValue newMatch = new UidOwnerValue(
373 (match == IIF_MATCH) ? 0 : oldMatch.iif,
374 oldMatch.rule & ~match
375 );
376
377 if (newMatch.rule == 0) {
378 sUidOwnerMap.deleteEntry(new U32(uid));
379 } else {
380 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
381 }
382 }
383 } catch (ErrnoException e) {
384 throw new ServiceSpecificException(e.errno,
385 caller + " failed to remove rule: " + Os.strerror(e.errno));
386 }
387 }
388
Motomu Utsumi389278e2022-06-28 07:05:05 +0000389 private void addRule(final int uid, final long match, final long iif, final String caller) {
390 if (match != IIF_MATCH && iif != 0) {
391 throw new ServiceSpecificException(EINVAL,
392 "Non-interface match must have zero interface index");
393 }
394
395 try {
396 synchronized (sUidOwnerMap) {
397 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
398
399 final UidOwnerValue newMatch;
400 if (oldMatch != null) {
401 newMatch = new UidOwnerValue(
402 (match == IIF_MATCH) ? iif : oldMatch.iif,
403 oldMatch.rule | match
404 );
405 } else {
406 newMatch = new UidOwnerValue(
407 iif,
408 match
409 );
410 }
411 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
412 }
413 } catch (ErrnoException e) {
414 throw new ServiceSpecificException(e.errno,
415 caller + " failed to add rule: " + Os.strerror(e.errno));
416 }
417 }
418
419 private void addRule(final int uid, final long match, final String caller) {
420 addRule(uid, match, 0 /* iif */, caller);
421 }
422
Ken Chenf5f51332022-01-28 10:08:16 +0800423 /**
424 * Add naughty app bandwidth rule for specific app
425 *
426 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800427 * @throws ServiceSpecificException in case of failure, with an error code indicating the
428 * cause of the failure.
429 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900430 public void addNaughtyApp(final int uid) {
Motomu Utsumi389278e2022-06-28 07:05:05 +0000431 throwIfPreT("addNaughtyApp is not available on pre-T devices");
Motomu Utsumi55c282e2022-08-03 06:25:33 +0000432
433 if (sEnableJavaBpfMap) {
434 addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp");
435 } else {
436 final int err = native_addNaughtyApp(uid);
437 maybeThrow(err, "Unable to add naughty app");
438 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800439 }
440
Ken Chenf5f51332022-01-28 10:08:16 +0800441 /**
442 * Remove naughty app bandwidth rule for specific app
443 *
444 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800445 * @throws ServiceSpecificException in case of failure, with an error code indicating the
446 * cause of the failure.
447 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900448 public void removeNaughtyApp(final int uid) {
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000449 throwIfPreT("removeNaughtyApp is not available on pre-T devices");
Motomu Utsumi878ce0d2022-08-03 06:22:42 +0000450
451 if (sEnableJavaBpfMap) {
452 removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp");
453 } else {
454 final int err = native_removeNaughtyApp(uid);
455 maybeThrow(err, "Unable to remove naughty app");
456 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800457 }
458
Ken Chenf5f51332022-01-28 10:08:16 +0800459 /**
460 * Add nice app bandwidth rule for specific app
461 *
462 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800463 * @throws ServiceSpecificException in case of failure, with an error code indicating the
464 * cause of the failure.
465 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900466 public void addNiceApp(final int uid) {
Motomu Utsumi55630d02022-06-29 07:46:52 +0000467 throwIfPreT("addNiceApp is not available on pre-T devices");
Motomu Utsumi7f19df92022-08-03 06:29:59 +0000468
469 if (sEnableJavaBpfMap) {
470 addRule(uid, HAPPY_BOX_MATCH, "addNiceApp");
471 } else {
472 final int err = native_addNiceApp(uid);
473 maybeThrow(err, "Unable to add nice app");
474 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800475 }
476
Ken Chenf5f51332022-01-28 10:08:16 +0800477 /**
478 * Remove nice app bandwidth rule for specific app
479 *
480 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800481 * @throws ServiceSpecificException in case of failure, with an error code indicating the
482 * cause of the failure.
483 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900484 public void removeNiceApp(final int uid) {
Motomu Utsumi7392eb42022-06-29 03:53:03 +0000485 throwIfPreT("removeNiceApp is not available on pre-T devices");
Motomu Utsumi5f15f752022-08-03 06:27:51 +0000486
487 if (sEnableJavaBpfMap) {
488 removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp");
489 } else {
490 final int err = native_removeNiceApp(uid);
491 maybeThrow(err, "Unable to remove nice app");
492 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800493 }
494
Ken Chenf5f51332022-01-28 10:08:16 +0800495 /**
496 * Set target firewall child chain
497 *
498 * @param childChain target chain to enable
499 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000500 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800501 * @throws ServiceSpecificException in case of failure, with an error code indicating the
502 * cause of the failure.
503 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900504 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000505 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000506
Motomu Utsumie057dd42022-08-03 01:23:49 +0000507 if (sEnableJavaBpfMap) {
508 final long match = getMatchByFirewallChain(childChain);
509 try {
510 synchronized (sUidRulesConfigBpfMapLock) {
511 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
512 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
513 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
514 }
515 } catch (ErrnoException e) {
516 throw new ServiceSpecificException(e.errno,
517 "Unable to set child chain: " + Os.strerror(e.errno));
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000518 }
Motomu Utsumie057dd42022-08-03 01:23:49 +0000519 } else {
520 final int err = native_setChildChain(childChain, enable);
521 maybeThrow(err, "Unable to set child chain");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000522 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800523 }
524
525 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000526 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000527 *
528 * @param childChain target chain
529 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
530 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000531 * @throws ServiceSpecificException in case of failure, with an error code indicating the
532 * cause of the failure.
533 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000534 public boolean isChainEnabled(final int childChain) {
535 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000536
537 final long match = getMatchByFirewallChain(childChain);
538 try {
539 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000540 return (config.val & match) != 0;
541 } catch (ErrnoException e) {
542 throw new ServiceSpecificException(e.errno,
543 "Unable to get firewall chain status: " + Os.strerror(e.errno));
544 }
545 }
546
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900547 private Set<Integer> asSet(final int[] uids) {
548 final Set<Integer> uidSet = new ArraySet<>();
549 for (final int uid: uids) {
550 uidSet.add(uid);
551 }
552 return uidSet;
553 }
554
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000555 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800556 * Replaces the contents of the specified UID-based firewall chain.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000557 * Enables the chain for specified uids and disables the chain for non-specified uids.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800558 *
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000559 * @param chain Target chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800560 * @param uids The list of UIDs to allow/deny.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000561 * @throws UnsupportedOperationException if called on pre-T devices.
562 * @throws IllegalArgumentException if {@code chain} is not a valid chain.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800563 */
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000564 public void replaceUidChain(final int chain, final int[] uids) {
565 throwIfPreT("replaceUidChain is not available on pre-T devices");
566
Motomu Utsumic7c16852022-08-03 06:51:41 +0000567 if (sEnableJavaBpfMap) {
568 final long match;
569 try {
570 match = getMatchByFirewallChain(chain);
571 } catch (ServiceSpecificException e) {
572 // Throws IllegalArgumentException to keep the behavior of
573 // ConnectivityManager#replaceFirewallChain API
574 throw new IllegalArgumentException("Invalid firewall chain: " + chain);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000575 }
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900576 final Set<Integer> uidSet = asSet(uids);
577 final Set<Integer> uidSetToRemoveRule = new ArraySet<>();
Motomu Utsumic7c16852022-08-03 06:51:41 +0000578 try {
579 synchronized (sUidOwnerMap) {
580 sUidOwnerMap.forEach((uid, config) -> {
581 // config could be null if there is a concurrent entry deletion.
Motomu Utsumi8e420ea2022-08-24 18:03:30 +0900582 // http://b/220084230. But sUidOwnerMap update must be done while holding a
583 // lock, so this should not happen.
584 if (config == null) {
585 Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock");
586 } else if (!uidSet.contains((int) uid.val) && (config.rule & match) != 0) {
Motomu Utsumic7c16852022-08-03 06:51:41 +0000587 uidSetToRemoveRule.add((int) uid.val);
588 }
589 });
590
591 for (final int uid : uidSetToRemoveRule) {
592 removeRule(uid, match, "replaceUidChain");
593 }
594 for (final int uid : uids) {
595 addRule(uid, match, "replaceUidChain");
596 }
597 }
598 } catch (ErrnoException | ServiceSpecificException e) {
599 Log.e(TAG, "replaceUidChain failed: " + e);
600 }
601 } else {
602 final int err;
603 switch (chain) {
604 case FIREWALL_CHAIN_DOZABLE:
605 err = native_replaceUidChain("fw_dozable", true /* isAllowList */, uids);
606 break;
607 case FIREWALL_CHAIN_STANDBY:
608 err = native_replaceUidChain("fw_standby", false /* isAllowList */, uids);
609 break;
610 case FIREWALL_CHAIN_POWERSAVE:
611 err = native_replaceUidChain("fw_powersave", true /* isAllowList */, uids);
612 break;
613 case FIREWALL_CHAIN_RESTRICTED:
614 err = native_replaceUidChain("fw_restricted", true /* isAllowList */, uids);
615 break;
616 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
617 err = native_replaceUidChain(
618 "fw_low_power_standby", true /* isAllowList */, uids);
619 break;
620 case FIREWALL_CHAIN_OEM_DENY_1:
621 err = native_replaceUidChain("fw_oem_deny_1", false /* isAllowList */, uids);
622 break;
623 case FIREWALL_CHAIN_OEM_DENY_2:
624 err = native_replaceUidChain("fw_oem_deny_2", false /* isAllowList */, uids);
625 break;
626 case FIREWALL_CHAIN_OEM_DENY_3:
627 err = native_replaceUidChain("fw_oem_deny_3", false /* isAllowList */, uids);
628 break;
629 default:
630 throw new IllegalArgumentException("replaceFirewallChain with invalid chain: "
631 + chain);
632 }
633 if (err != 0) {
634 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
635 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800636 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800637 }
638
Ken Chenf5f51332022-01-28 10:08:16 +0800639 /**
640 * Set firewall rule for uid
641 *
642 * @param childChain target chain
643 * @param uid uid to allow/deny
644 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800645 * @throws ServiceSpecificException in case of failure, with an error code indicating the
646 * cause of the failure.
647 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900648 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi40230be2022-07-05 03:27:35 +0000649 throwIfPreT("setUidRule is not available on pre-T devices");
650
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000651 if (sEnableJavaBpfMap) {
652 final long match = getMatchByFirewallChain(childChain);
653 final boolean isAllowList = isFirewallAllowList(childChain);
654 final boolean add = (firewallRule == FIREWALL_RULE_ALLOW && isAllowList)
655 || (firewallRule == FIREWALL_RULE_DENY && !isAllowList);
Motomu Utsumi40230be2022-07-05 03:27:35 +0000656
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000657 if (add) {
658 addRule(uid, match, "setUidRule");
659 } else {
660 removeRule(uid, match, "setUidRule");
661 }
Motomu Utsumi40230be2022-07-05 03:27:35 +0000662 } else {
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000663 final int err = native_setUidRule(childChain, uid, firewallRule);
664 maybeThrow(err, "Unable to set uid rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000665 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800666 }
667
668 /**
669 * Add ingress interface filtering rules to a list of UIDs
670 *
671 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
672 * allowed interface and loopback to be sent to the list of UIDs.
673 *
674 * Calling this method on one or more UIDs with an existing filtering rule but a different
675 * interface name will result in the filtering rule being updated to allow the new interface
676 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
677 *
678 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800679 * be received.
680 * @param uids an array of UIDs which the filtering rules will be set
681 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800682 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800683 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800684 */
Ken Chenf5f51332022-01-28 10:08:16 +0800685 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000686 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800687 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800688 return;
689 }
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000690
691 if (sEnableJavaBpfMap) {
692 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and
693 // ifIndex is set to 0.
694 final int ifIndex;
695 if (ifName == null) {
696 ifIndex = 0;
697 } else {
698 ifIndex = mDeps.getIfIndex(ifName);
699 if (ifIndex == 0) {
700 throw new ServiceSpecificException(ENODEV,
701 "Failed to get index of interface " + ifName);
702 }
703 }
704 for (final int uid : uids) {
705 try {
706 addRule(uid, IIF_MATCH, ifIndex, "addUidInterfaceRules");
707 } catch (ServiceSpecificException e) {
708 Log.e(TAG, "addRule failed uid=" + uid + " ifName=" + ifName + ", " + e);
709 }
710 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000711 } else {
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000712 final int err = native_addUidInterfaceRules(ifName, uids);
713 maybeThrow(err, "Unable to add uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000714 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800715 }
716
717 /**
718 * Remove ingress interface filtering rules from a list of UIDs
719 *
720 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
721 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
722 *
723 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800724 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800725 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800726 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800727 */
Ken Chenf5f51332022-01-28 10:08:16 +0800728 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000729 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800730 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800731 return;
732 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000733
734 if (sEnableJavaBpfMap) {
735 for (final int uid : uids) {
736 try {
737 removeRule(uid, IIF_MATCH, "removeUidInterfaceRules");
738 } catch (ServiceSpecificException e) {
739 Log.e(TAG, "removeRule failed uid=" + uid + ", " + e);
740 }
Motomu Utsumi599c4e52022-06-30 03:37:18 +0000741 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000742 } else {
743 final int err = native_removeUidInterfaceRules(uids);
744 maybeThrow(err, "Unable to remove uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000745 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800746 }
747
Ken Chenf5f51332022-01-28 10:08:16 +0800748 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000749 * Update lockdown rule for uid
750 *
751 * @param uid target uid to add/remove the rule
752 * @param add {@code true} to add the rule, {@code false} to remove the rule.
753 * @throws ServiceSpecificException in case of failure, with an error code indicating the
754 * cause of the failure.
755 */
756 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi697b2992022-06-30 02:25:29 +0000757 throwIfPreT("updateUidLockdownRule is not available on pre-T devices");
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000758
759 if (sEnableJavaBpfMap) {
760 if (add) {
761 addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
762 } else {
763 removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
764 }
Motomu Utsumi697b2992022-06-30 02:25:29 +0000765 } else {
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000766 final int err = native_updateUidLockdownRule(uid, add);
767 maybeThrow(err, "Unable to update lockdown rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000768 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000769 }
770
771 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800772 * Request netd to change the current active network stats map.
773 *
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000774 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800775 * @throws ServiceSpecificException in case of failure, with an error code indicating the
776 * cause of the failure.
777 */
markchien49e944c2022-03-01 15:22:20 +0800778 public void swapActiveStatsMap() {
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000779 throwIfPreT("swapActiveStatsMap is not available on pre-T devices");
780
781 if (sEnableJavaBpfMap) {
782 try {
783 synchronized (sCurrentStatsMapConfigLock) {
784 final long config = sConfigurationMap.getValue(
785 CURRENT_STATS_MAP_CONFIGURATION_KEY).val;
786 final long newConfig = (config == STATS_SELECT_MAP_A)
787 ? STATS_SELECT_MAP_B : STATS_SELECT_MAP_A;
788 sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY,
789 new U32(newConfig));
790 }
791 } catch (ErrnoException e) {
792 throw new ServiceSpecificException(e.errno, "Failed to swap active stats map");
793 }
794
795 // After changing the config, it's needed to make sure all the current running eBPF
796 // programs are finished and all the CPUs are aware of this config change before the old
797 // map is modified. So special hack is needed here to wait for the kernel to do a
798 // synchronize_rcu(). Once the kernel called synchronize_rcu(), the updated config will
799 // be available to all cores and the next eBPF programs triggered inside the kernel will
800 // use the new map configuration. So once this function returns it is safe to modify the
801 // old stats map without concerning about race between the kernel and userspace.
802 final int err = mDeps.synchronizeKernelRCU();
803 maybeThrow(err, "synchronizeKernelRCU failed");
804 } else {
805 final int err = native_swapActiveStatsMap();
806 maybeThrow(err, "Unable to swap active stats map");
807 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800808 }
809
Ken Chenf5f51332022-01-28 10:08:16 +0800810 /**
811 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
812 * specified. Or remove all permissions from the uids.
813 *
814 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
815 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
816 * revoke all permissions for the uids.
817 * @param uids uid of users to grant permission
818 * @throws RemoteException when netd has crashed.
819 */
820 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000821 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800822 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800823 return;
824 }
Motomu Utsumi65271202022-07-05 08:21:41 +0000825
826 if (sEnableJavaBpfMap) {
827 // Remove the entry if package is uninstalled or uid has only INTERNET permission.
828 if (permissions == PERMISSION_UNINSTALLED || permissions == PERMISSION_INTERNET) {
829 for (final int uid : uids) {
830 try {
831 sUidPermissionMap.deleteEntry(new U32(uid));
832 } catch (ErrnoException e) {
833 Log.e(TAG, "Failed to remove uid " + uid + " from permission map: " + e);
834 }
835 }
836 return;
837 }
838
839 for (final int uid : uids) {
840 try {
841 sUidPermissionMap.updateEntry(new U32(uid), new U8((short) permissions));
842 } catch (ErrnoException e) {
843 Log.e(TAG, "Failed to set permission "
844 + permissions + " to uid " + uid + ": " + e);
845 }
846 }
847 } else {
848 native_setPermissionForUids(permissions, uids);
849 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800850 }
851
Ken Chene6d511f2022-01-25 11:10:42 +0800852 /**
853 * Dump BPF maps
854 *
855 * @param fd file descriptor to output
856 * @throws IOException when file descriptor is invalid.
857 * @throws ServiceSpecificException when the method is called on an unsupported device.
858 */
859 public void dump(final FileDescriptor fd, boolean verbose)
860 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000861 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800862 throw new ServiceSpecificException(
863 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
864 + " devices, use dumpsys netd trafficcontroller instead.");
865 }
866 native_dump(fd, verbose);
867 }
868
Wayne Ma790c83e2022-01-13 10:35:05 +0800869 private static native void native_init();
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800870 private native int native_addNaughtyApp(int uid);
871 private native int native_removeNaughtyApp(int uid);
872 private native int native_addNiceApp(int uid);
873 private native int native_removeNiceApp(int uid);
Motomu Utsumi114cd9c2022-08-01 02:08:35 +0000874 private native int native_setChildChain(int childChain, boolean enable);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800875 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
876 private native int native_setUidRule(int childChain, int uid, int firewallRule);
877 private native int native_addUidInterfaceRules(String ifName, int[] uids);
878 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000879 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800880 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800881 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800882 private native void native_dump(FileDescriptor fd, boolean verbose);
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000883 private static native int native_synchronizeKernelRCU();
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800884}