blob: f0e9ec4a49cdbc1dd3ea2fd6a0973377a8b881c1 [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 Utsumi5f52f4f2022-06-30 03:31:09 +000028import static android.system.OsConstants.ENODEV;
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000029import static android.system.OsConstants.ENOENT;
Ken Chene6d511f2022-01-25 11:10:42 +080030import static android.system.OsConstants.EOPNOTSUPP;
31
Wayne Ma2fde98c2022-01-17 18:04:05 +080032import android.net.INetd;
33import android.os.RemoteException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080034import android.os.ServiceSpecificException;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000035import android.system.ErrnoException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080036import android.system.Os;
37import android.util.Log;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000038import android.util.SparseLongArray;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080039
Motomu Utsumi5a68a212022-06-24 10:14:31 +000040import com.android.internal.annotations.GuardedBy;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000041import com.android.internal.annotations.VisibleForTesting;
Ken Chenf5f51332022-01-28 10:08:16 +080042import com.android.modules.utils.build.SdkLevel;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000043import com.android.net.module.util.BpfMap;
44import com.android.net.module.util.Struct.U32;
Ken Chenf5f51332022-01-28 10:08:16 +080045
Ken Chene6d511f2022-01-25 11:10:42 +080046import java.io.FileDescriptor;
47import java.io.IOException;
48
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080049/**
50 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
51 *
52 * {@hide}
53 */
54public class BpfNetMaps {
Motomu Utsumi305975f2022-06-27 09:24:32 +000055 private static final boolean PRE_T = !SdkLevel.isAtLeastT();
56 static {
57 if (!PRE_T) {
58 System.loadLibrary("service-connectivity");
59 }
60 }
61
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080062 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +080063 private final INetd mNetd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +000064 private final Dependencies mDeps;
Ken Chenf5f51332022-01-28 10:08:16 +080065 // Use legacy netd for releases before T.
Ken Chenf5f51332022-01-28 10:08:16 +080066 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080067
Motomu Utsumi18b287d2022-06-19 10:45:30 +000068 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
69 // This entry is not accessed by others.
70 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
71 private static final Object sUidRulesConfigBpfMapLock = new Object();
72
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000073 private static final String CONFIGURATION_MAP_PATH =
74 "/sys/fs/bpf/netd_shared/map_netd_configuration_map";
Motomu Utsumi5a68a212022-06-24 10:14:31 +000075 private static final String UID_OWNER_MAP_PATH =
76 "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map";
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000077 private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0);
78 private static BpfMap<U32, U32> sConfigurationMap = null;
Motomu Utsumi5a68a212022-06-24 10:14:31 +000079 // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others.
80 private static BpfMap<U32, UidOwnerValue> sUidOwnerMap = null;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000081
82 // LINT.IfChange(match_type)
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000083 @VisibleForTesting public static final long NO_MATCH = 0;
84 @VisibleForTesting public static final long HAPPY_BOX_MATCH = (1 << 0);
85 @VisibleForTesting public static final long PENALTY_BOX_MATCH = (1 << 1);
86 @VisibleForTesting public static final long DOZABLE_MATCH = (1 << 2);
87 @VisibleForTesting public static final long STANDBY_MATCH = (1 << 3);
88 @VisibleForTesting public static final long POWERSAVE_MATCH = (1 << 4);
89 @VisibleForTesting public static final long RESTRICTED_MATCH = (1 << 5);
90 @VisibleForTesting public static final long LOW_POWER_STANDBY_MATCH = (1 << 6);
91 @VisibleForTesting public static final long IIF_MATCH = (1 << 7);
92 @VisibleForTesting public static final long LOCKDOWN_VPN_MATCH = (1 << 8);
93 @VisibleForTesting public static final long OEM_DENY_1_MATCH = (1 << 9);
94 @VisibleForTesting public static final long OEM_DENY_2_MATCH = (1 << 10);
95 @VisibleForTesting public static final long OEM_DENY_3_MATCH = (1 << 11);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000096 // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h)
97
98 // TODO: Use Java BpfMap instead of JNI code (TrafficController) for map update.
99 // Currently, BpfNetMaps uses TrafficController for map update and TrafficController
100 // (changeUidOwnerRule and toggleUidOwnerMap) also does conversion from "firewall chain" to
101 // "match". Migrating map update from JNI to Java BpfMap will solve this duplication.
102 private static final SparseLongArray FIREWALL_CHAIN_TO_MATCH = new SparseLongArray();
103 static {
104 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_DOZABLE, DOZABLE_MATCH);
105 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_STANDBY, STANDBY_MATCH);
106 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_POWERSAVE, POWERSAVE_MATCH);
107 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_RESTRICTED, RESTRICTED_MATCH);
108 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_LOW_POWER_STANDBY, LOW_POWER_STANDBY_MATCH);
109 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_1, OEM_DENY_1_MATCH);
110 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_2, OEM_DENY_2_MATCH);
111 FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_3, OEM_DENY_3_MATCH);
112 }
113
114 /**
Motomu Utsumi305975f2022-06-27 09:24:32 +0000115 * Set configurationMap for test.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000116 */
117 @VisibleForTesting
Motomu Utsumi305975f2022-06-27 09:24:32 +0000118 public static void setConfigurationMapForTest(BpfMap<U32, U32> configurationMap) {
119 sConfigurationMap = configurationMap;
120 }
121
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000122 /**
123 * Set uidOwnerMap for test.
124 */
125 @VisibleForTesting
126 public static void setUidOwnerMapForTest(BpfMap<U32, UidOwnerValue> uidOwnerMap) {
127 sUidOwnerMap = uidOwnerMap;
128 }
129
Motomu Utsumi305975f2022-06-27 09:24:32 +0000130 private static BpfMap<U32, U32> getConfigurationMap() {
131 try {
132 return new BpfMap<>(
133 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class);
134 } catch (ErrnoException e) {
135 throw new IllegalStateException("Cannot open netd configuration map", e);
136 }
137 }
138
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000139 private static BpfMap<U32, UidOwnerValue> getUidOwnerMap() {
140 try {
141 return new BpfMap<>(
142 UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class);
143 } catch (ErrnoException e) {
144 throw new IllegalStateException("Cannot open uid owner map", e);
145 }
146 }
147
Motomu Utsumi305975f2022-06-27 09:24:32 +0000148 private static void setBpfMaps() {
149 if (sConfigurationMap == null) {
150 sConfigurationMap = getConfigurationMap();
151 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000152 if (sUidOwnerMap == null) {
153 sUidOwnerMap = getUidOwnerMap();
154 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000155 }
156
Ken Chenf5f51332022-01-28 10:08:16 +0800157 /**
158 * Initializes the class if it is not already initialized. This method will open maps but not
159 * cause any other effects. This method may be called multiple times on any thread.
160 */
161 private static synchronized void ensureInitialized() {
162 if (sInitialized) return;
Motomu Utsumi305975f2022-06-27 09:24:32 +0000163 setBpfMaps();
164 native_init();
Ken Chenf5f51332022-01-28 10:08:16 +0800165 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800166 }
167
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000168 /**
169 * Dependencies of BpfNetMaps, for injection in tests.
170 */
171 @VisibleForTesting
172 public static class Dependencies {
173 /**
174 * Get interface index.
175 */
176 public int getIfIndex(final String ifName) {
177 return Os.if_nametoindex(ifName);
178 }
179 }
180
markchien49e944c2022-03-01 15:22:20 +0800181 /** Constructor used after T that doesn't need to use netd anymore. */
182 public BpfNetMaps() {
183 this(null);
184
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000185 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800186 }
187
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000188 public BpfNetMaps(final INetd netd) {
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000189 this(netd, new Dependencies());
190 }
191
192 @VisibleForTesting
193 public BpfNetMaps(final INetd netd, final Dependencies deps) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000194 if (!PRE_T) {
195 ensureInitialized();
196 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800197 mNetd = netd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000198 mDeps = deps;
Wayne Ma790c83e2022-01-13 10:35:05 +0800199 }
200
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000201 /**
202 * Get corresponding match from firewall chain.
203 */
204 @VisibleForTesting
205 public long getMatchByFirewallChain(final int chain) {
206 final long match = FIREWALL_CHAIN_TO_MATCH.get(chain, NO_MATCH);
207 if (match == NO_MATCH) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000208 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000209 }
210 return match;
211 }
212
Ken Chenf5f51332022-01-28 10:08:16 +0800213 private void maybeThrow(final int err, final String msg) {
214 if (err != 0) {
215 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
216 }
217 }
218
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000219 private void throwIfPreT(final String msg) {
220 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000221 throw new UnsupportedOperationException(msg);
222 }
223 }
224
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000225 private void removeRule(final int uid, final long match, final String caller) {
226 try {
227 synchronized (sUidOwnerMap) {
228 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
229
230 if (oldMatch == null) {
231 throw new ServiceSpecificException(ENOENT,
232 "sUidOwnerMap does not have entry for uid: " + uid);
233 }
234
235 final UidOwnerValue newMatch = new UidOwnerValue(
236 (match == IIF_MATCH) ? 0 : oldMatch.iif,
237 oldMatch.rule & ~match
238 );
239
240 if (newMatch.rule == 0) {
241 sUidOwnerMap.deleteEntry(new U32(uid));
242 } else {
243 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
244 }
245 }
246 } catch (ErrnoException e) {
247 throw new ServiceSpecificException(e.errno,
248 caller + " failed to remove rule: " + Os.strerror(e.errno));
249 }
250 }
251
Motomu Utsumi389278e2022-06-28 07:05:05 +0000252 private void addRule(final int uid, final long match, final long iif, final String caller) {
253 if (match != IIF_MATCH && iif != 0) {
254 throw new ServiceSpecificException(EINVAL,
255 "Non-interface match must have zero interface index");
256 }
257
258 try {
259 synchronized (sUidOwnerMap) {
260 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid));
261
262 final UidOwnerValue newMatch;
263 if (oldMatch != null) {
264 newMatch = new UidOwnerValue(
265 (match == IIF_MATCH) ? iif : oldMatch.iif,
266 oldMatch.rule | match
267 );
268 } else {
269 newMatch = new UidOwnerValue(
270 iif,
271 match
272 );
273 }
274 sUidOwnerMap.updateEntry(new U32(uid), newMatch);
275 }
276 } catch (ErrnoException e) {
277 throw new ServiceSpecificException(e.errno,
278 caller + " failed to add rule: " + Os.strerror(e.errno));
279 }
280 }
281
282 private void addRule(final int uid, final long match, final String caller) {
283 addRule(uid, match, 0 /* iif */, caller);
284 }
285
Ken Chenf5f51332022-01-28 10:08:16 +0800286 /**
287 * Add naughty app bandwidth rule for specific app
288 *
289 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800290 * @throws ServiceSpecificException in case of failure, with an error code indicating the
291 * cause of the failure.
292 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900293 public void addNaughtyApp(final int uid) {
Motomu Utsumi389278e2022-06-28 07:05:05 +0000294 throwIfPreT("addNaughtyApp is not available on pre-T devices");
295 addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800296 }
297
Ken Chenf5f51332022-01-28 10:08:16 +0800298 /**
299 * Remove naughty app bandwidth rule for specific app
300 *
301 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800302 * @throws ServiceSpecificException in case of failure, with an error code indicating the
303 * cause of the failure.
304 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900305 public void removeNaughtyApp(final int uid) {
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000306 throwIfPreT("removeNaughtyApp is not available on pre-T devices");
307 removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800308 }
309
Ken Chenf5f51332022-01-28 10:08:16 +0800310 /**
311 * Add nice app bandwidth rule for specific app
312 *
313 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800314 * @throws ServiceSpecificException in case of failure, with an error code indicating the
315 * cause of the failure.
316 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900317 public void addNiceApp(final int uid) {
Motomu Utsumi55630d02022-06-29 07:46:52 +0000318 throwIfPreT("addNiceApp is not available on pre-T devices");
319 addRule(uid, HAPPY_BOX_MATCH, "addNiceApp");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800320 }
321
Ken Chenf5f51332022-01-28 10:08:16 +0800322 /**
323 * Remove nice app bandwidth rule for specific app
324 *
325 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800326 * @throws ServiceSpecificException in case of failure, with an error code indicating the
327 * cause of the failure.
328 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900329 public void removeNiceApp(final int uid) {
Motomu Utsumi7392eb42022-06-29 03:53:03 +0000330 throwIfPreT("removeNiceApp is not available on pre-T devices");
331 removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800332 }
333
Ken Chenf5f51332022-01-28 10:08:16 +0800334 /**
335 * Set target firewall child chain
336 *
337 * @param childChain target chain to enable
338 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000339 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800340 * @throws ServiceSpecificException in case of failure, with an error code indicating the
341 * cause of the failure.
342 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900343 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000344 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000345
346 final long match = getMatchByFirewallChain(childChain);
347 try {
348 synchronized (sUidRulesConfigBpfMapLock) {
349 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000350 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000351 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
352 }
353 } catch (ErrnoException e) {
354 throw new ServiceSpecificException(e.errno,
355 "Unable to set child chain: " + Os.strerror(e.errno));
356 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800357 }
358
359 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000360 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000361 *
362 * @param childChain target chain
363 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
364 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000365 * @throws ServiceSpecificException in case of failure, with an error code indicating the
366 * cause of the failure.
367 */
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000368 public boolean isChainEnabled(final int childChain) {
369 throwIfPreT("isChainEnabled is not available on pre-T devices");
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000370
371 final long match = getMatchByFirewallChain(childChain);
372 try {
373 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000374 return (config.val & match) != 0;
375 } catch (ErrnoException e) {
376 throw new ServiceSpecificException(e.errno,
377 "Unable to get firewall chain status: " + Os.strerror(e.errno));
378 }
379 }
380
381 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800382 * Replaces the contents of the specified UID-based firewall chain.
383 *
384 * The chain may be an allowlist chain or a denylist chain. A denylist chain contains DROP
385 * rules for the specified UIDs and a RETURN rule at the end. An allowlist chain contains RETURN
Ken Chenf5f51332022-01-28 10:08:16 +0800386 * rules for the system UID range (0 to {@code UID_APP} - 1), RETURN rules for the specified
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800387 * UIDs, and a DROP rule at the end. The chain will be created if it does not exist.
388 *
Ken Chenf5f51332022-01-28 10:08:16 +0800389 * @param chainName The name of the chain to replace.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800390 * @param isAllowlist Whether this is an allowlist or denylist chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800391 * @param uids The list of UIDs to allow/deny.
392 * @return 0 if the chain was successfully replaced, errno otherwise.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800393 */
394 public int replaceUidChain(final String chainName, final boolean isAllowlist,
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900395 final int[] uids) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000396 synchronized (sUidOwnerMap) {
397 final int err = native_replaceUidChain(chainName, isAllowlist, uids);
398 if (err != 0) {
399 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
400 }
401 return -err;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800402 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800403 }
404
Ken Chenf5f51332022-01-28 10:08:16 +0800405 /**
406 * Set firewall rule for uid
407 *
408 * @param childChain target chain
409 * @param uid uid to allow/deny
410 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800411 * @throws ServiceSpecificException in case of failure, with an error code indicating the
412 * cause of the failure.
413 */
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900414 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000415 synchronized (sUidOwnerMap) {
416 final int err = native_setUidRule(childChain, uid, firewallRule);
417 maybeThrow(err, "Unable to set uid rule");
418 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800419 }
420
421 /**
422 * Add ingress interface filtering rules to a list of UIDs
423 *
424 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
425 * allowed interface and loopback to be sent to the list of UIDs.
426 *
427 * Calling this method on one or more UIDs with an existing filtering rule but a different
428 * interface name will result in the filtering rule being updated to allow the new interface
429 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
430 *
431 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800432 * be received.
433 * @param uids an array of UIDs which the filtering rules will be set
434 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800435 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800436 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800437 */
Ken Chenf5f51332022-01-28 10:08:16 +0800438 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000439 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800440 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800441 return;
442 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000443 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and ifIndex
444 // is set to 0.
445 final int ifIndex;
446 if (ifName == null) {
447 ifIndex = 0;
448 } else {
449 ifIndex = mDeps.getIfIndex(ifName);
450 if (ifIndex == 0) {
451 throw new ServiceSpecificException(ENODEV,
452 "Failed to get index of interface " + ifName);
453 }
454 }
455 for (final int uid: uids) {
456 try {
457 addRule(uid, IIF_MATCH, ifIndex, "addUidInterfaceRules");
458 } catch (ServiceSpecificException e) {
459 Log.e(TAG, "addRule failed uid=" + uid + " ifName=" + ifName + ", " + e);
460 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000461 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800462 }
463
464 /**
465 * Remove ingress interface filtering rules from a list of UIDs
466 *
467 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
468 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
469 *
470 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800471 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800472 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800473 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800474 */
Ken Chenf5f51332022-01-28 10:08:16 +0800475 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000476 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800477 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800478 return;
479 }
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000480 synchronized (sUidOwnerMap) {
481 final int err = native_removeUidInterfaceRules(uids);
482 maybeThrow(err, "Unable to remove uid interface rules");
483 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800484 }
485
Ken Chenf5f51332022-01-28 10:08:16 +0800486 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000487 * Update lockdown rule for uid
488 *
489 * @param uid target uid to add/remove the rule
490 * @param add {@code true} to add the rule, {@code false} to remove the rule.
491 * @throws ServiceSpecificException in case of failure, with an error code indicating the
492 * cause of the failure.
493 */
494 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi697b2992022-06-30 02:25:29 +0000495 throwIfPreT("updateUidLockdownRule is not available on pre-T devices");
496 if (add) {
497 addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
498 } else {
499 removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000500 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000501 }
502
503 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800504 * Request netd to change the current active network stats map.
505 *
Ken Chenf5f51332022-01-28 10:08:16 +0800506 * @throws ServiceSpecificException in case of failure, with an error code indicating the
507 * cause of the failure.
508 */
markchien49e944c2022-03-01 15:22:20 +0800509 public void swapActiveStatsMap() {
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800510 final int err = native_swapActiveStatsMap();
Ken Chenf5f51332022-01-28 10:08:16 +0800511 maybeThrow(err, "Unable to swap active stats map");
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800512 }
513
Ken Chenf5f51332022-01-28 10:08:16 +0800514 /**
515 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
516 * specified. Or remove all permissions from the uids.
517 *
518 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
519 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
520 * revoke all permissions for the uids.
521 * @param uids uid of users to grant permission
522 * @throws RemoteException when netd has crashed.
523 */
524 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000525 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800526 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800527 return;
528 }
529 native_setPermissionForUids(permissions, uids);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800530 }
531
Ken Chene6d511f2022-01-25 11:10:42 +0800532 /**
533 * Dump BPF maps
534 *
535 * @param fd file descriptor to output
536 * @throws IOException when file descriptor is invalid.
537 * @throws ServiceSpecificException when the method is called on an unsupported device.
538 */
539 public void dump(final FileDescriptor fd, boolean verbose)
540 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000541 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +0800542 throw new ServiceSpecificException(
543 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
544 + " devices, use dumpsys netd trafficcontroller instead.");
545 }
546 native_dump(fd, verbose);
547 }
548
Wayne Ma790c83e2022-01-13 10:35:05 +0800549 private static native void native_init();
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000550 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800551 private native int native_addNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000552 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800553 private native int native_removeNaughtyApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000554 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800555 private native int native_addNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000556 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800557 private native int native_removeNiceApp(int uid);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000558 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800559 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000560 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800561 private native int native_setUidRule(int childChain, int uid, int firewallRule);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000562 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800563 private native int native_addUidInterfaceRules(String ifName, int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000564 @GuardedBy("sUidOwnerMap")
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800565 private native int native_removeUidInterfaceRules(int[] uids);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000566 @GuardedBy("sUidOwnerMap")
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000567 private native int native_updateUidLockdownRule(int uid, boolean add);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800568 private native int native_swapActiveStatsMap();
Wayne Ma2fde98c2022-01-17 18:04:05 +0800569 private native void native_setPermissionForUids(int permissions, int[] uids);
Ken Chene6d511f2022-01-25 11:10:42 +0800570 private native void native_dump(FileDescriptor fd, boolean verbose);
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800571}