blob: c3302cddb4fcfda96917ab1cdf3ffbab475fe7af [file] [log] [blame]
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000019import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE;
20import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY;
21import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1;
22import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2;
23import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3;
24import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE;
25import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED;
26import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY;
Motomu Utsumi18b287d2022-06-19 10:45:30 +000027import static android.system.OsConstants.EINVAL;
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000028import static android.system.OsConstants.ENOENT;
Ken Chene6d511f2022-01-25 11:10:42 +080029import static android.system.OsConstants.EOPNOTSUPP;
30
Wayne Ma2fde98c2022-01-17 18:04:05 +080031import android.net.INetd;
32import android.os.RemoteException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080033import android.os.ServiceSpecificException;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000034import android.system.ErrnoException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080035import android.system.Os;
36import android.util.Log;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000037import android.util.SparseLongArray;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080038
Motomu Utsumi5a68a212022-06-24 10:14:31 +000039import com.android.internal.annotations.GuardedBy;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000040import com.android.internal.annotations.VisibleForTesting;
Ken Chenf5f51332022-01-28 10:08:16 +080041import com.android.modules.utils.build.SdkLevel;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000042import com.android.net.module.util.BpfMap;
43import com.android.net.module.util.Struct.U32;
Ken Chenf5f51332022-01-28 10:08:16 +080044
Ken Chene6d511f2022-01-25 11:10:42 +080045import java.io.FileDescriptor;
46import java.io.IOException;
47
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080048/**
49 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
50 *
51 * {@hide}
52 */
53public class BpfNetMaps {
Motomu Utsumi305975f2022-06-27 09:24:32 +000054 private static final boolean PRE_T = !SdkLevel.isAtLeastT();
55 static {
56 if (!PRE_T) {
57 System.loadLibrary("service-connectivity");
58 }
59 }
60
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080061 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +080062 private final INetd mNetd;
Ken Chenf5f51332022-01-28 10:08:16 +080063 // Use legacy netd for releases before T.
Ken Chenf5f51332022-01-28 10:08:16 +080064 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080065
Motomu Utsumi18b287d2022-06-19 10:45:30 +000066 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
67 // This entry is not accessed by others.
68 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
69 private static final Object sUidRulesConfigBpfMapLock = new Object();
70
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000071 private static final String CONFIGURATION_MAP_PATH =
72 "/sys/fs/bpf/netd_shared/map_netd_configuration_map";
Motomu Utsumi5a68a212022-06-24 10:14:31 +000073 private static final String UID_OWNER_MAP_PATH =
74 "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map";
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000075 private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0);
76 private static BpfMap<U32, U32> sConfigurationMap = null;
Motomu Utsumi5a68a212022-06-24 10:14:31 +000077 // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others.
78 private static BpfMap<U32, UidOwnerValue> sUidOwnerMap = null;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000079
80 // LINT.IfChange(match_type)
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000081 @VisibleForTesting public static final long NO_MATCH = 0;
82 @VisibleForTesting public static final long HAPPY_BOX_MATCH = (1 << 0);
83 @VisibleForTesting public static final long PENALTY_BOX_MATCH = (1 << 1);
84 @VisibleForTesting public static final long DOZABLE_MATCH = (1 << 2);
85 @VisibleForTesting public static final long STANDBY_MATCH = (1 << 3);
86 @VisibleForTesting public static final long POWERSAVE_MATCH = (1 << 4);
87 @VisibleForTesting public static final long RESTRICTED_MATCH = (1 << 5);
88 @VisibleForTesting public static final long LOW_POWER_STANDBY_MATCH = (1 << 6);
89 @VisibleForTesting public static final long IIF_MATCH = (1 << 7);
90 @VisibleForTesting public static final long LOCKDOWN_VPN_MATCH = (1 << 8);
91 @VisibleForTesting public static final long OEM_DENY_1_MATCH = (1 << 9);
92 @VisibleForTesting public static final long OEM_DENY_2_MATCH = (1 << 10);
93 @VisibleForTesting public static final long OEM_DENY_3_MATCH = (1 << 11);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000094 // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h)
95
96 // TODO: Use Java BpfMap instead of JNI code (TrafficController) for map update.
97 // Currently, BpfNetMaps uses TrafficController for map update and TrafficController
98 // (changeUidOwnerRule and toggleUidOwnerMap) also does conversion from "firewall chain" to
99 // "match". Migrating map update from JNI to Java BpfMap will solve this duplication.
100 private static final SparseLongArray FIREWALL_CHAIN_TO_MATCH = new SparseLongArray();
101 static {
102 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_DOZABLE, DOZABLE_MATCH);
103 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_STANDBY, STANDBY_MATCH);
104 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_POWERSAVE, POWERSAVE_MATCH);
105 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_RESTRICTED, RESTRICTED_MATCH);
106 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_LOW_POWER_STANDBY, LOW_POWER_STANDBY_MATCH);
107 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_1, OEM_DENY_1_MATCH);
108 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_2, OEM_DENY_2_MATCH);
109 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_3, OEM_DENY_3_MATCH);
110 }
111
112 /**
Motomu Utsumi305975f2022-06-27 09:24:32 +0000113 * Set configurationMap for test.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000114 */
115 @VisibleForTesting
Motomu Utsumi305975f2022-06-27 09:24:32 +0000116 public static void setConfigurationMapForTest(BpfMap<U32, U32> configurationMap) {
117 sConfigurationMap = configurationMap;
118 }
119
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000120 /**
121 * Set uidOwnerMap for test.
122 */
123 @VisibleForTesting
124 public static void setUidOwnerMapForTest(BpfMap<U32, UidOwnerValue> uidOwnerMap) {
125 sUidOwnerMap = uidOwnerMap;
126 }
127
Motomu Utsumi305975f2022-06-27 09:24:32 +0000128 private static BpfMap<U32, U32> getConfigurationMap() {
129 try {
130 return new BpfMap<>(
131 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class);
132 } catch (ErrnoException e) {
133 throw new IllegalStateException("Cannot open netd configuration map", e);
134 }
135 }
136
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000137 private static BpfMap<U32, UidOwnerValue> getUidOwnerMap() {
138 try {
139 return new BpfMap<>(
140 UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class);
141 } catch (ErrnoException e) {
142 throw new IllegalStateException("Cannot open uid owner map", e);
143 }
144 }
145
Motomu Utsumi305975f2022-06-27 09:24:32 +0000146 private static void setBpfMaps() {
147 if (sConfigurationMap == null) {
148 sConfigurationMap = getConfigurationMap();
149 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000150 if (sUidOwnerMap == null) {
151 sUidOwnerMap = getUidOwnerMap();
152 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000153 }
154
Ken Chenf5f51332022-01-28 10:08:16 +0800155 /**
156 * Initializes the class if it is not already initialized. This method will open maps but not
157 * cause any other effects. This method may be called multiple times on any thread.
158 */
159 private static synchronized void ensureInitialized() {
160 if (sInitialized) return;
Motomu Utsumi305975f2022-06-27 09:24:32 +0000161 setBpfMaps();
162 native_init();
Ken Chenf5f51332022-01-28 10:08:16 +0800163 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800164 }
165
markchien49e944c2022-03-01 15:22:20 +0800166 /** Constructor used after T that doesn't need to use netd anymore. */
167 public BpfNetMaps() {
168 this(null);
169
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000170 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800171 }
172
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000173 public BpfNetMaps(final INetd netd) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000174 if (!PRE_T) {
175 ensureInitialized();
176 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800177 mNetd = netd;
Wayne Ma790c83e2022-01-13 10:35:05 +0800178 }
179
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000180 /**
181 * Get corresponding match from firewall chain.
182 */
183 @VisibleForTesting
184 public long getMatchByFirewallChain(final int chain) {
185 final long match = FIREWALL_CHAIN_TO_MATCH.get(chain, NO_MATCH);
186 if (match == NO_MATCH) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000187 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000188 }
189 return match;
190 }
191
Ken Chenf5f51332022-01-28 10:08:16 +0800192 private void maybeThrow(final int err, final String msg) {
193 if (err != 0) {
194 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
195 }
196 }
197
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000198 private void throwIfPreT(final String msg) {
199 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000200 throw new UnsupportedOperationException(msg);
201 }
202 }
203
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000204 private void removeRule(final int uid, final long match, final String caller) {
205 try {
206 synchronized (sUidOwnerMap) {
207 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
208
209 if (oldMatch == null) {
210 throw new ServiceSpecificException(ENOENT,
211 "sUidOwnerMap does not have entry for uid: " + uid);
212 }
213
214 final UidOwnerValue newMatch = new UidOwnerValue(
215 (match == IIF_MATCH) ? 0 : oldMatch.iif,
216 oldMatch.rule & ~match
217 );
218
219 if (newMatch.rule == 0) {
220 sUidOwnerMap.deleteEntry(new U32(uid));
221 } else {
222 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
223 }
224 }
225 } catch (ErrnoException e) {
226 throw new ServiceSpecificException(e.errno,
227 caller + " failed to remove rule: " + Os.strerror(e.errno));
228 }
229 }
230
Ken Chenf5f51332022-01-28 10:08:16 +0800231 /**
232 * Add naughty app bandwidth rule for specific app
233 *
234 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800235 * @throws ServiceSpecificException in case of failure, with an error code indicating the
236 * cause of the failure.
237 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900238 public void addNaughtyApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000239 synchronized (sUidOwnerMap) {
240 final int err = native_addNaughtyApp(uid);
241 maybeThrow(err, "Unable to add naughty app");
242 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800243 }
244
Ken Chenf5f51332022-01-28 10:08:16 +0800245 /**
246 * Remove naughty app bandwidth rule for specific app
247 *
248 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800249 * @throws ServiceSpecificException in case of failure, with an error code indicating the
250 * cause of the failure.
251 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900252 public void removeNaughtyApp(final int uid) {
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000253 throwIfPreT("removeNaughtyApp is not available on pre-T devices");
254 removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800255 }
256
Ken Chenf5f51332022-01-28 10:08:16 +0800257 /**
258 * Add nice app bandwidth rule for specific app
259 *
260 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800261 * @throws ServiceSpecificException in case of failure, with an error code indicating the
262 * cause of the failure.
263 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900264 public void addNiceApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000265 synchronized (sUidOwnerMap) {
266 final int err = native_addNiceApp(uid);
267 maybeThrow(err, "Unable to add nice app");
268 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800269 }
270
Ken Chenf5f51332022-01-28 10:08:16 +0800271 /**
272 * Remove nice app bandwidth rule for specific app
273 *
274 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800275 * @throws ServiceSpecificException in case of failure, with an error code indicating the
276 * cause of the failure.
277 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900278 public void removeNiceApp(final int uid) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000279 synchronized (sUidOwnerMap) {
280 final int err = native_removeNiceApp(uid);
281 maybeThrow(err, "Unable to remove nice app");
282 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800283 }
284
Ken Chenf5f51332022-01-28 10:08:16 +0800285 /**
286 * Set target firewall child chain
287 *
288 * @param childChain target chain to enable
289 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000290 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800291 * @throws ServiceSpecificException in case of failure, with an error code indicating the
292 * cause of the failure.
293 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900294 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000295 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000296
297 final long match = getMatchByFirewallChain(childChain);
298 try {
299 synchronized (sUidRulesConfigBpfMapLock) {
300 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000301 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000302 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
303 }
304 } catch (ErrnoException e) {
305 throw new ServiceSpecificException(e.errno,
306 "Unable to set child chain: " + Os.strerror(e.errno));
307 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800308 }
309
310 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000311 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000312 *
313 * @param childChain target chain
314 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
315 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000316 * @throws ServiceSpecificException in case of failure, with an error code indicating the
317 * cause of the failure.
318 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000319 public boolean isChainEnabled(final int childChain) {
320 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000321
322 final long match = getMatchByFirewallChain(childChain);
323 try {
324 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000325 return (config.val & match) != 0;
326 } catch (ErrnoException e) {
327 throw new ServiceSpecificException(e.errno,
328 "Unable to get firewall chain status: " + Os.strerror(e.errno));
329 }
330 }
331
332 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800333 * Replaces the contents of the specified UID-based firewall chain.
334 *
335 * The chain may be an allowlist chain or a denylist chain. A denylist chain contains DROP
336 * rules for the specified UIDs and a RETURN rule at the end. An allowlist chain contains RETURN
Ken Chenf5f51332022-01-28 10:08:16 +0800337 * rules for the system UID range (0 to {@code UID_APP} - 1), RETURN rules for the specified
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800338 * UIDs, and a DROP rule at the end. The chain will be created if it does not exist.
339 *
Ken Chenf5f51332022-01-28 10:08:16 +0800340 * @param chainName The name of the chain to replace.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800341 * @param isAllowlist Whether this is an allowlist or denylist chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800342 * @param uids The list of UIDs to allow/deny.
343 * @return 0 if the chain was successfully replaced, errno otherwise.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800344 */
345 public int replaceUidChain(final String chainName, final boolean isAllowlist,
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900346 final int[] uids) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000347 synchronized (sUidOwnerMap) {
348 final int err = native_replaceUidChain(chainName, isAllowlist, uids);
349 if (err != 0) {
350 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
351 }
352 return -err;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800353 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800354 }
355
Ken Chenf5f51332022-01-28 10:08:16 +0800356 /**
357 * Set firewall rule for uid
358 *
359 * @param childChain target chain
360 * @param uid uid to allow/deny
361 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800362 * @throws ServiceSpecificException in case of failure, with an error code indicating the
363 * cause of the failure.
364 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900365 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000366 synchronized (sUidOwnerMap) {
367 final int err = native_setUidRule(childChain, uid, firewallRule);
368 maybeThrow(err, "Unable to set uid rule");
369 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800370 }
371
372 /**
373 * Add ingress interface filtering rules to a list of UIDs
374 *
375 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
376 * allowed interface and loopback to be sent to the list of UIDs.
377 *
378 * Calling this method on one or more UIDs with an existing filtering rule but a different
379 * interface name will result in the filtering rule being updated to allow the new interface
380 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
381 *
382 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800383 * be received.
384 * @param uids an array of UIDs which the filtering rules will be set
385 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800386 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800387 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800388 */
Ken Chenf5f51332022-01-28 10:08:16 +0800389 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000390 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800391 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800392 return;
393 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000394 synchronized (sUidOwnerMap) {
395 final int err = native_addUidInterfaceRules(ifName, uids);
396 maybeThrow(err, "Unable to add uid interface rules");
397 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800398 }
399
400 /**
401 * Remove ingress interface filtering rules from a list of UIDs
402 *
403 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
404 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
405 *
406 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800407 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800408 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800409 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800410 */
Ken Chenf5f51332022-01-28 10:08:16 +0800411 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000412 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800413 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800414 return;
415 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000416 synchronized (sUidOwnerMap) {
417 final int err = native_removeUidInterfaceRules(uids);
418 maybeThrow(err, "Unable to remove uid interface rules");
419 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800420 }
421
Ken Chenf5f51332022-01-28 10:08:16 +0800422 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000423 * Update lockdown rule for uid
424 *
425 * @param uid target uid to add/remove the rule
426 * @param add {@code true} to add the rule, {@code false} to remove the rule.
427 * @throws ServiceSpecificException in case of failure, with an error code indicating the
428 * cause of the failure.
429 */
430 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000431 synchronized (sUidOwnerMap) {
432 final int err = native_updateUidLockdownRule(uid, add);
433 maybeThrow(err, "Unable to update lockdown rule");
434 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000435 }
436
437 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800438 * Request netd to change the current active network stats map.
439 *
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 */
markchien49e944c2022-03-01 15:22:20 +0800443 public void swapActiveStatsMap() {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800444 final int err = native_swapActiveStatsMap();
Ken Chenf5f51332022-01-28 10:08:16 +0800445 maybeThrow(err, "Unable to swap active stats map");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800446 }
447
Ken Chenf5f51332022-01-28 10:08:16 +0800448 /**
449 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
450 * specified. Or remove all permissions from the uids.
451 *
452 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
453 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
454 * revoke all permissions for the uids.
455 * @param uids uid of users to grant permission
456 * @throws RemoteException when netd has crashed.
457 */
458 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000459 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800460 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800461 return;
462 }
463 native_setPermissionForUids(permissions, uids);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800464 }
465
Ken Chene6d511f2022-01-25 11:10:42 +0800466 /**
467 * Dump BPF maps
468 *
469 * @param fd file descriptor to output
470 * @throws IOException when file descriptor is invalid.
471 * @throws ServiceSpecificException when the method is called on an unsupported device.
472 */
473 public void dump(final FileDescriptor fd, boolean verbose)
474 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000475 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800476 throw new ServiceSpecificException(
477 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
478 + " devices, use dumpsys netd trafficcontroller instead.");
479 }
480 native_dump(fd, verbose);
481 }
482
Wayne Ma790c83e2022-01-13 10:35:05 +0800483 private static native void native_init();
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000484 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800485 private native int native_addNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000486 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800487 private native int native_removeNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000488 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800489 private native int native_addNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000490 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800491 private native int native_removeNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000492 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800493 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000494 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800495 private native int native_setUidRule(int childChain, int uid, int firewallRule);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000496 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800497 private native int native_addUidInterfaceRules(String ifName, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000498 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800499 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000500 @GuardedBy("sUidOwnerMap")
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000501 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800502 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800503 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800504 private native void native_dump(FileDescriptor fd, boolean verbose);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800505}