blob: 6ade1248853d941180a1456205c6d93bf872336f [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
Junyu Lai29b7b632023-08-23 17:35:17 +080019import static android.net.BpfNetMapsConstants.CONFIGURATION_MAP_PATH;
20import static android.net.BpfNetMapsConstants.COOKIE_TAG_MAP_PATH;
21import static android.net.BpfNetMapsConstants.CURRENT_STATS_MAP_CONFIGURATION_KEY;
Ken Chen24330172023-10-20 13:02:14 +080022import static android.net.BpfNetMapsConstants.DATA_SAVER_DISABLED;
23import static android.net.BpfNetMapsConstants.DATA_SAVER_ENABLED;
24import static android.net.BpfNetMapsConstants.DATA_SAVER_ENABLED_KEY;
25import static android.net.BpfNetMapsConstants.DATA_SAVER_ENABLED_MAP_PATH;
Junyu Lai29b7b632023-08-23 17:35:17 +080026import static android.net.BpfNetMapsConstants.HAPPY_BOX_MATCH;
27import static android.net.BpfNetMapsConstants.IIF_MATCH;
28import static android.net.BpfNetMapsConstants.LOCKDOWN_VPN_MATCH;
29import static android.net.BpfNetMapsConstants.PENALTY_BOX_MATCH;
30import static android.net.BpfNetMapsConstants.UID_OWNER_MAP_PATH;
31import static android.net.BpfNetMapsConstants.UID_PERMISSION_MAP_PATH;
32import static android.net.BpfNetMapsConstants.UID_RULES_CONFIGURATION_KEY;
Junyu Lai626045a2023-08-28 18:49:44 +080033import static android.net.BpfNetMapsUtils.PRE_T;
Junyu Lai29b7b632023-08-23 17:35:17 +080034import static android.net.BpfNetMapsUtils.getMatchByFirewallChain;
35import static android.net.BpfNetMapsUtils.matchToString;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000036import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE;
37import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY;
38import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1;
39import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2;
40import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3;
41import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE;
42import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED;
43import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY;
Motomu Utsumi40230be2022-07-05 03:27:35 +000044import static android.net.ConnectivityManager.FIREWALL_RULE_ALLOW;
45import static android.net.ConnectivityManager.FIREWALL_RULE_DENY;
Motomu Utsumi65271202022-07-05 08:21:41 +000046import static android.net.INetd.PERMISSION_INTERNET;
Motomu Utsumi310850f2022-09-02 12:48:20 +090047import static android.net.INetd.PERMISSION_NONE;
Motomu Utsumi65271202022-07-05 08:21:41 +000048import static android.net.INetd.PERMISSION_UNINSTALLED;
Motomu Utsumi310850f2022-09-02 12:48:20 +090049import static android.net.INetd.PERMISSION_UPDATE_DEVICE_STATS;
Motomu Utsumi18b287d2022-06-19 10:45:30 +000050import static android.system.OsConstants.EINVAL;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +000051import static android.system.OsConstants.ENODEV;
Motomu Utsumi60ed3be2022-06-24 10:38:57 +000052import static android.system.OsConstants.ENOENT;
Ken Chene6d511f2022-01-25 11:10:42 +080053import static android.system.OsConstants.EOPNOTSUPP;
54
Motomu Utsumi166f9662022-09-01 10:35:29 +090055import static com.android.server.ConnectivityStatsLog.NETWORK_BPF_MAP_INFO;
56
57import android.app.StatsManager;
Motomu Utsumif688eeb2022-07-22 03:47:35 +000058import android.content.Context;
Junyu Lai626045a2023-08-28 18:49:44 +080059import android.net.BpfNetMapsReader;
Wayne Ma2fde98c2022-01-17 18:04:05 +080060import android.net.INetd;
Junyu Lai626045a2023-08-28 18:49:44 +080061import android.net.UidOwnerValue;
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +000062import android.os.Build;
Wayne Ma2fde98c2022-01-17 18:04:05 +080063import android.os.RemoteException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080064import android.os.ServiceSpecificException;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000065import android.system.ErrnoException;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080066import android.system.Os;
Motomu Utsumi1a477b02022-08-23 15:14:56 +090067import android.util.ArraySet;
Motomu Utsumi310850f2022-09-02 12:48:20 +090068import android.util.IndentingPrintWriter;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080069import android.util.Log;
Motomu Utsumi310850f2022-09-02 12:48:20 +090070import android.util.Pair;
Motomu Utsumi166f9662022-09-01 10:35:29 +090071import android.util.StatsEvent;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080072
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +000073import androidx.annotation.RequiresApi;
74
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000075import com.android.internal.annotations.VisibleForTesting;
Motomu Utsumi166f9662022-09-01 10:35:29 +090076import com.android.modules.utils.BackgroundThread;
Ken Chenf5f51332022-01-28 10:08:16 +080077import com.android.modules.utils.build.SdkLevel;
Motomu Utsumi310850f2022-09-02 12:48:20 +090078import com.android.net.module.util.BpfDump;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000079import com.android.net.module.util.BpfMap;
Motomu Utsumif688eeb2022-07-22 03:47:35 +000080import com.android.net.module.util.DeviceConfigUtils;
Motomu Utsumi73599a52022-08-24 11:59:21 +090081import com.android.net.module.util.IBpfMap;
Motomu Utsumi166f9662022-09-01 10:35:29 +090082import com.android.net.module.util.Struct;
Maciej Żenczykowski785793f2022-09-17 02:35:20 +000083import com.android.net.module.util.Struct.S32;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +000084import com.android.net.module.util.Struct.U32;
Motomu Utsumi65271202022-07-05 08:21:41 +000085import com.android.net.module.util.Struct.U8;
Motomu Utsumib9548862022-09-06 16:30:05 +090086import com.android.net.module.util.bpf.CookieTagMapKey;
87import com.android.net.module.util.bpf.CookieTagMapValue;
Ken Chenf5f51332022-01-28 10:08:16 +080088
Ken Chene6d511f2022-01-25 11:10:42 +080089import java.io.FileDescriptor;
90import java.io.IOException;
Motomu Utsumi310850f2022-09-02 12:48:20 +090091import java.util.Arrays;
Motomu Utsumi166f9662022-09-01 10:35:29 +090092import java.util.List;
Motomu Utsumi9be2ea02022-07-05 06:14:59 +000093import java.util.Set;
Motomu Utsumi310850f2022-09-02 12:48:20 +090094import java.util.StringJoiner;
Ken Chene6d511f2022-01-25 11:10:42 +080095
Wayne Ma0ea3bdc2022-01-12 01:12:11 +080096/**
97 * BpfNetMaps is responsible for providing traffic controller relevant functionality.
98 *
99 * {@hide}
100 */
101public class BpfNetMaps {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000102 static {
103 if (!PRE_T) {
104 System.loadLibrary("service-connectivity");
105 }
106 }
107
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800108 private static final String TAG = "BpfNetMaps";
Wayne Ma2fde98c2022-01-17 18:04:05 +0800109 private final INetd mNetd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000110 private final Dependencies mDeps;
Ken Chenf5f51332022-01-28 10:08:16 +0800111 // Use legacy netd for releases before T.
Ken Chenf5f51332022-01-28 10:08:16 +0800112 private static boolean sInitialized = false;
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800113
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000114 private static Boolean sEnableJavaBpfMap = null;
Motomu Utsumi7628ea32023-08-14 11:09:02 +0900115 private static final String BPF_NET_MAPS_FORCE_DISABLE_JAVA_BPF_MAP =
116 "bpf_net_maps_force_disable_java_bpf_map";
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000117
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000118 // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
119 // This entry is not accessed by others.
120 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
121 private static final Object sUidRulesConfigBpfMapLock = new Object();
122
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000123 // Lock for sConfigurationMap entry for CURRENT_STATS_MAP_CONFIGURATION_KEY.
124 // BpfNetMaps acquires this lock while sequence of read, modify, and write.
125 // BpfNetMaps is an only writer of this entry.
126 private static final Object sCurrentStatsMapConfigLock = new Object();
127
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000128 private static final long UID_RULES_DEFAULT_CONFIGURATION = 0;
129 private static final long STATS_SELECT_MAP_A = 0;
130 private static final long STATS_SELECT_MAP_B = 1;
131
Maciej Żenczykowskidc886222022-09-17 06:09:22 +0000132 private static IBpfMap<S32, U32> sConfigurationMap = null;
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000133 // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others.
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000134 private static IBpfMap<S32, UidOwnerValue> sUidOwnerMap = null;
135 private static IBpfMap<S32, U8> sUidPermissionMap = null;
Motomu Utsumib9548862022-09-06 16:30:05 +0900136 private static IBpfMap<CookieTagMapKey, CookieTagMapValue> sCookieTagMap = null;
Ken Chen24330172023-10-20 13:02:14 +0800137 // TODO: Add BOOL class and replace U8?
138 private static IBpfMap<S32, U8> sDataSaverEnabledMap = null;
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000139
Motomu Utsumi310850f2022-09-02 12:48:20 +0900140 private static final List<Pair<Integer, String>> PERMISSION_LIST = Arrays.asList(
141 Pair.create(PERMISSION_INTERNET, "PERMISSION_INTERNET"),
142 Pair.create(PERMISSION_UPDATE_DEVICE_STATS, "PERMISSION_UPDATE_DEVICE_STATS")
143 );
144
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000145 /**
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000146 * Set sEnableJavaBpfMap for test.
147 */
148 @VisibleForTesting
149 public static void setEnableJavaBpfMapForTest(boolean enable) {
150 sEnableJavaBpfMap = enable;
151 }
152
153 /**
Motomu Utsumi305975f2022-06-27 09:24:32 +0000154 * Set configurationMap for test.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000155 */
156 @VisibleForTesting
Maciej Żenczykowskidc886222022-09-17 06:09:22 +0000157 public static void setConfigurationMapForTest(IBpfMap<S32, U32> configurationMap) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000158 sConfigurationMap = configurationMap;
159 }
160
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000161 /**
162 * Set uidOwnerMap for test.
163 */
164 @VisibleForTesting
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000165 public static void setUidOwnerMapForTest(IBpfMap<S32, UidOwnerValue> uidOwnerMap) {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000166 sUidOwnerMap = uidOwnerMap;
167 }
168
Motomu Utsumi65271202022-07-05 08:21:41 +0000169 /**
170 * Set uidPermissionMap for test.
171 */
172 @VisibleForTesting
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000173 public static void setUidPermissionMapForTest(IBpfMap<S32, U8> uidPermissionMap) {
Motomu Utsumi65271202022-07-05 08:21:41 +0000174 sUidPermissionMap = uidPermissionMap;
175 }
176
Motomu Utsumib9548862022-09-06 16:30:05 +0900177 /**
178 * Set cookieTagMap for test.
179 */
180 @VisibleForTesting
181 public static void setCookieTagMapForTest(
182 IBpfMap<CookieTagMapKey, CookieTagMapValue> cookieTagMap) {
183 sCookieTagMap = cookieTagMap;
184 }
185
Ken Chen24330172023-10-20 13:02:14 +0800186 /**
187 * Set dataSaverEnabledMap for test.
188 */
189 @VisibleForTesting
190 public static void setDataSaverEnabledMapForTest(IBpfMap<S32, U8> dataSaverEnabledMap) {
191 sDataSaverEnabledMap = dataSaverEnabledMap;
192 }
193
Maciej Żenczykowskidc886222022-09-17 06:09:22 +0000194 private static IBpfMap<S32, U32> getConfigurationMap() {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000195 try {
196 return new BpfMap<>(
Maciej Żenczykowskidc886222022-09-17 06:09:22 +0000197 CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U32.class);
Motomu Utsumi305975f2022-06-27 09:24:32 +0000198 } catch (ErrnoException e) {
199 throw new IllegalStateException("Cannot open netd configuration map", e);
200 }
201 }
202
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000203 private static IBpfMap<S32, UidOwnerValue> getUidOwnerMap() {
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000204 try {
205 return new BpfMap<>(
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000206 UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, UidOwnerValue.class);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000207 } catch (ErrnoException e) {
208 throw new IllegalStateException("Cannot open uid owner map", e);
209 }
210 }
211
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000212 private static IBpfMap<S32, U8> getUidPermissionMap() {
Motomu Utsumi65271202022-07-05 08:21:41 +0000213 try {
214 return new BpfMap<>(
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000215 UID_PERMISSION_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U8.class);
Motomu Utsumi65271202022-07-05 08:21:41 +0000216 } catch (ErrnoException e) {
217 throw new IllegalStateException("Cannot open uid permission map", e);
218 }
219 }
220
Motomu Utsumib9548862022-09-06 16:30:05 +0900221 private static IBpfMap<CookieTagMapKey, CookieTagMapValue> getCookieTagMap() {
222 try {
223 return new BpfMap<>(COOKIE_TAG_MAP_PATH, BpfMap.BPF_F_RDWR,
224 CookieTagMapKey.class, CookieTagMapValue.class);
225 } catch (ErrnoException e) {
226 throw new IllegalStateException("Cannot open cookie tag map", e);
227 }
228 }
229
Ken Chen24330172023-10-20 13:02:14 +0800230 private static IBpfMap<S32, U8> getDataSaverEnabledMap() {
231 try {
232 return new BpfMap<>(
233 DATA_SAVER_ENABLED_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U8.class);
234 } catch (ErrnoException e) {
235 throw new IllegalStateException("Cannot open data saver enabled map", e);
236 }
237 }
238
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000239 private static void initBpfMaps() {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000240 if (sConfigurationMap == null) {
241 sConfigurationMap = getConfigurationMap();
242 }
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000243 try {
244 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY,
245 new U32(UID_RULES_DEFAULT_CONFIGURATION));
246 } catch (ErrnoException e) {
247 throw new IllegalStateException("Failed to initialize uid rules configuration", e);
248 }
249 try {
250 sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY,
251 new U32(STATS_SELECT_MAP_A));
252 } catch (ErrnoException e) {
253 throw new IllegalStateException("Failed to initialize current stats configuration", e);
254 }
255
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000256 if (sUidOwnerMap == null) {
257 sUidOwnerMap = getUidOwnerMap();
258 }
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000259 try {
260 sUidOwnerMap.clear();
261 } catch (ErrnoException e) {
262 throw new IllegalStateException("Failed to initialize uid owner map", e);
263 }
Motomu Utsumi65271202022-07-05 08:21:41 +0000264
265 if (sUidPermissionMap == null) {
266 sUidPermissionMap = getUidPermissionMap();
267 }
Motomu Utsumib9548862022-09-06 16:30:05 +0900268
269 if (sCookieTagMap == null) {
270 sCookieTagMap = getCookieTagMap();
271 }
Ken Chen24330172023-10-20 13:02:14 +0800272
273 if (sDataSaverEnabledMap == null) {
274 sDataSaverEnabledMap = getDataSaverEnabledMap();
275 }
276 try {
277 sDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(DATA_SAVER_DISABLED));
278 } catch (ErrnoException e) {
279 throw new IllegalStateException("Failed to initialize data saver configuration", e);
280 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000281 }
282
Ken Chenf5f51332022-01-28 10:08:16 +0800283 /**
284 * Initializes the class if it is not already initialized. This method will open maps but not
285 * cause any other effects. This method may be called multiple times on any thread.
286 */
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000287 private static synchronized void ensureInitialized(final Context context) {
Ken Chenf5f51332022-01-28 10:08:16 +0800288 if (sInitialized) return;
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000289 if (sEnableJavaBpfMap == null) {
Maciej Żenczykowskie7ebb152022-12-27 10:57:38 +0000290 sEnableJavaBpfMap = SdkLevel.isAtLeastU() ||
Motomu Utsumied4e7ec2023-09-13 14:58:32 +0900291 DeviceConfigUtils.isTetheringFeatureNotChickenedOut(context,
Motomu Utsumi7628ea32023-08-14 11:09:02 +0900292 BPF_NET_MAPS_FORCE_DISABLE_JAVA_BPF_MAP);
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000293 }
294 Log.d(TAG, "BpfNetMaps is initialized with sEnableJavaBpfMap=" + sEnableJavaBpfMap);
295
Motomu Utsumiba2fa152022-07-25 01:57:23 +0000296 initBpfMaps();
Motomu Utsumid95a0da2022-09-03 00:40:30 +0900297 native_init(!sEnableJavaBpfMap /* startSkDestroyListener */);
Ken Chenf5f51332022-01-28 10:08:16 +0800298 sInitialized = true;
Wayne Ma2fde98c2022-01-17 18:04:05 +0800299 }
300
Motomu Utsumid95a0da2022-09-03 00:40:30 +0900301 public boolean isSkDestroyListenerRunning() {
302 return !sEnableJavaBpfMap;
303 }
304
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000305 /**
306 * Dependencies of BpfNetMaps, for injection in tests.
307 */
308 @VisibleForTesting
309 public static class Dependencies {
310 /**
311 * Get interface index.
312 */
313 public int getIfIndex(final String ifName) {
314 return Os.if_nametoindex(ifName);
315 }
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000316
317 /**
318 * Call synchronize_rcu()
319 */
320 public int synchronizeKernelRCU() {
321 return native_synchronizeKernelRCU();
322 }
Motomu Utsumi166f9662022-09-01 10:35:29 +0900323
324 /**
325 * Build Stats Event for NETWORK_BPF_MAP_INFO atom
326 */
327 public StatsEvent buildStatsEvent(final int cookieTagMapSize, final int uidOwnerMapSize,
328 final int uidPermissionMapSize) {
329 return ConnectivityStatsLog.buildStatsEvent(NETWORK_BPF_MAP_INFO, cookieTagMapSize,
330 uidOwnerMapSize, uidPermissionMapSize);
331 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000332 }
333
markchien49e944c2022-03-01 15:22:20 +0800334 /** Constructor used after T that doesn't need to use netd anymore. */
Junyu Lai626045a2023-08-28 18:49:44 +0800335 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000336 public BpfNetMaps(final Context context) {
337 this(context, null);
markchien49e944c2022-03-01 15:22:20 +0800338
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000339 if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T");
markchien49e944c2022-03-01 15:22:20 +0800340 }
341
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000342 public BpfNetMaps(final Context context, final INetd netd) {
343 this(context, netd, new Dependencies());
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000344 }
345
346 @VisibleForTesting
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000347 public BpfNetMaps(final Context context, final INetd netd, final Dependencies deps) {
Motomu Utsumi305975f2022-06-27 09:24:32 +0000348 if (!PRE_T) {
Motomu Utsumif688eeb2022-07-22 03:47:35 +0000349 ensureInitialized(context);
Motomu Utsumi305975f2022-06-27 09:24:32 +0000350 }
Wayne Ma2fde98c2022-01-17 18:04:05 +0800351 mNetd = netd;
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000352 mDeps = deps;
Wayne Ma790c83e2022-01-13 10:35:05 +0800353 }
354
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000355 /**
Motomu Utsumi40230be2022-07-05 03:27:35 +0000356 * Get if the chain is allow list or not.
357 *
358 * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed
359 * DENYLIST means the firewall allows all by default, uids must be explicitly denyed
360 */
Motomu Utsumi40230be2022-07-05 03:27:35 +0000361 public boolean isFirewallAllowList(final int chain) {
362 switch (chain) {
363 case FIREWALL_CHAIN_DOZABLE:
364 case FIREWALL_CHAIN_POWERSAVE:
365 case FIREWALL_CHAIN_RESTRICTED:
366 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
367 return true;
368 case FIREWALL_CHAIN_STANDBY:
369 case FIREWALL_CHAIN_OEM_DENY_1:
370 case FIREWALL_CHAIN_OEM_DENY_2:
371 case FIREWALL_CHAIN_OEM_DENY_3:
372 return false;
373 default:
374 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
375 }
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000376 }
377
Ken Chenf5f51332022-01-28 10:08:16 +0800378 private void maybeThrow(final int err, final String msg) {
379 if (err != 0) {
380 throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err));
381 }
382 }
383
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000384 private void throwIfPreT(final String msg) {
385 if (PRE_T) {
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000386 throw new UnsupportedOperationException(msg);
387 }
388 }
389
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000390 private void removeRule(final int uid, final long match, final String caller) {
391 try {
392 synchronized (sUidOwnerMap) {
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000393 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid));
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000394
395 if (oldMatch == null) {
396 throw new ServiceSpecificException(ENOENT,
397 "sUidOwnerMap does not have entry for uid: " + uid);
398 }
399
400 final UidOwnerValue newMatch = new UidOwnerValue(
401 (match == IIF_MATCH) ? 0 : oldMatch.iif,
402 oldMatch.rule & ~match
403 );
404
405 if (newMatch.rule == 0) {
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000406 sUidOwnerMap.deleteEntry(new S32(uid));
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000407 } else {
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000408 sUidOwnerMap.updateEntry(new S32(uid), newMatch);
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000409 }
410 }
411 } catch (ErrnoException e) {
412 throw new ServiceSpecificException(e.errno,
413 caller + " failed to remove rule: " + Os.strerror(e.errno));
414 }
415 }
416
Maciej Żenczykowski6c1c6bb2022-09-16 06:55:33 +0000417 private void addRule(final int uid, final long match, final int iif, final String caller) {
Motomu Utsumi389278e2022-06-28 07:05:05 +0000418 if (match != IIF_MATCH && iif != 0) {
419 throw new ServiceSpecificException(EINVAL,
420 "Non-interface match must have zero interface index");
421 }
422
423 try {
424 synchronized (sUidOwnerMap) {
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000425 final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid));
Motomu Utsumi389278e2022-06-28 07:05:05 +0000426
427 final UidOwnerValue newMatch;
428 if (oldMatch != null) {
429 newMatch = new UidOwnerValue(
430 (match == IIF_MATCH) ? iif : oldMatch.iif,
431 oldMatch.rule | match
432 );
433 } else {
434 newMatch = new UidOwnerValue(
435 iif,
436 match
437 );
438 }
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000439 sUidOwnerMap.updateEntry(new S32(uid), newMatch);
Motomu Utsumi389278e2022-06-28 07:05:05 +0000440 }
441 } catch (ErrnoException e) {
442 throw new ServiceSpecificException(e.errno,
443 caller + " failed to add rule: " + Os.strerror(e.errno));
444 }
445 }
446
447 private void addRule(final int uid, final long match, final String caller) {
448 addRule(uid, match, 0 /* iif */, caller);
449 }
450
Ken Chenf5f51332022-01-28 10:08:16 +0800451 /**
452 * Add naughty app bandwidth rule for specific app
453 *
454 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800455 * @throws ServiceSpecificException in case of failure, with an error code indicating the
456 * cause of the failure.
457 */
Junyu Lai626045a2023-08-28 18:49:44 +0800458 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900459 public void addNaughtyApp(final int uid) {
Motomu Utsumi389278e2022-06-28 07:05:05 +0000460 throwIfPreT("addNaughtyApp is not available on pre-T devices");
Motomu Utsumi55c282e2022-08-03 06:25:33 +0000461
462 if (sEnableJavaBpfMap) {
463 addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp");
464 } else {
465 final int err = native_addNaughtyApp(uid);
466 maybeThrow(err, "Unable to add naughty app");
467 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800468 }
469
Ken Chenf5f51332022-01-28 10:08:16 +0800470 /**
471 * Remove naughty app bandwidth rule for specific app
472 *
473 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800474 * @throws ServiceSpecificException in case of failure, with an error code indicating the
475 * cause of the failure.
476 */
Junyu Lai626045a2023-08-28 18:49:44 +0800477 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900478 public void removeNaughtyApp(final int uid) {
Motomu Utsumi60ed3be2022-06-24 10:38:57 +0000479 throwIfPreT("removeNaughtyApp is not available on pre-T devices");
Motomu Utsumi878ce0d2022-08-03 06:22:42 +0000480
481 if (sEnableJavaBpfMap) {
482 removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp");
483 } else {
484 final int err = native_removeNaughtyApp(uid);
485 maybeThrow(err, "Unable to remove naughty app");
486 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800487 }
488
Ken Chenf5f51332022-01-28 10:08:16 +0800489 /**
490 * Add nice app bandwidth rule for specific app
491 *
492 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800493 * @throws ServiceSpecificException in case of failure, with an error code indicating the
494 * cause of the failure.
495 */
Junyu Lai626045a2023-08-28 18:49:44 +0800496 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900497 public void addNiceApp(final int uid) {
Motomu Utsumi55630d02022-06-29 07:46:52 +0000498 throwIfPreT("addNiceApp is not available on pre-T devices");
Motomu Utsumi7f19df92022-08-03 06:29:59 +0000499
500 if (sEnableJavaBpfMap) {
501 addRule(uid, HAPPY_BOX_MATCH, "addNiceApp");
502 } else {
503 final int err = native_addNiceApp(uid);
504 maybeThrow(err, "Unable to add nice app");
505 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800506 }
507
Ken Chenf5f51332022-01-28 10:08:16 +0800508 /**
509 * Remove nice app bandwidth rule for specific app
510 *
511 * @param uid uid of target app
Ken Chenf5f51332022-01-28 10:08:16 +0800512 * @throws ServiceSpecificException in case of failure, with an error code indicating the
513 * cause of the failure.
514 */
Junyu Lai626045a2023-08-28 18:49:44 +0800515 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900516 public void removeNiceApp(final int uid) {
Motomu Utsumi7392eb42022-06-29 03:53:03 +0000517 throwIfPreT("removeNiceApp is not available on pre-T devices");
Motomu Utsumi5f15f752022-08-03 06:27:51 +0000518
519 if (sEnableJavaBpfMap) {
520 removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp");
521 } else {
522 final int err = native_removeNiceApp(uid);
523 maybeThrow(err, "Unable to remove nice app");
524 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800525 }
526
Ken Chenf5f51332022-01-28 10:08:16 +0800527 /**
528 * Set target firewall child chain
529 *
530 * @param childChain target chain to enable
531 * @param enable whether to enable or disable child chain.
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000532 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800533 * @throws ServiceSpecificException in case of failure, with an error code indicating the
534 * cause of the failure.
535 */
Junyu Lai626045a2023-08-28 18:49:44 +0800536 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900537 public void setChildChain(final int childChain, final boolean enable) {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000538 throwIfPreT("setChildChain is not available on pre-T devices");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000539
Motomu Utsumie057dd42022-08-03 01:23:49 +0000540 if (sEnableJavaBpfMap) {
541 final long match = getMatchByFirewallChain(childChain);
542 try {
543 synchronized (sUidRulesConfigBpfMapLock) {
544 final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY);
545 final long newConfig = enable ? (config.val | match) : (config.val & ~match);
546 sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig));
547 }
548 } catch (ErrnoException e) {
549 throw new ServiceSpecificException(e.errno,
550 "Unable to set child chain: " + Os.strerror(e.errno));
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000551 }
Motomu Utsumie057dd42022-08-03 01:23:49 +0000552 } else {
553 final int err = native_setChildChain(childChain, enable);
554 maybeThrow(err, "Unable to set child chain");
Motomu Utsumi18b287d2022-06-19 10:45:30 +0000555 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800556 }
557
558 /**
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000559 * Get the specified firewall chain's status.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000560 *
561 * @param childChain target chain
562 * @return {@code true} if chain is enabled, {@code false} if chain is not enabled.
563 * @throws UnsupportedOperationException if called on pre-T devices.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000564 * @throws ServiceSpecificException in case of failure, with an error code indicating the
565 * cause of the failure.
Junyu Lai626045a2023-08-28 18:49:44 +0800566 *
567 * @deprecated Use {@link BpfNetMapsReader#isChainEnabled} instead.
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000568 */
Junyu Lai626045a2023-08-28 18:49:44 +0800569 // TODO: Migrate the callers to use {@link BpfNetMapsReader#isChainEnabled} instead.
570 @Deprecated
571 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000572 public boolean isChainEnabled(final int childChain) {
Junyu Lai626045a2023-08-28 18:49:44 +0800573 return BpfNetMapsReader.isChainEnabled(sConfigurationMap, childChain);
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000574 }
575
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900576 private Set<Integer> asSet(final int[] uids) {
577 final Set<Integer> uidSet = new ArraySet<>();
578 for (final int uid: uids) {
579 uidSet.add(uid);
580 }
581 return uidSet;
582 }
583
Motomu Utsumibe3ff1e2022-06-08 10:05:07 +0000584 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800585 * Replaces the contents of the specified UID-based firewall chain.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000586 * Enables the chain for specified uids and disables the chain for non-specified uids.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800587 *
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000588 * @param chain Target chain.
Ken Chenf5f51332022-01-28 10:08:16 +0800589 * @param uids The list of UIDs to allow/deny.
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000590 * @throws UnsupportedOperationException if called on pre-T devices.
591 * @throws IllegalArgumentException if {@code chain} is not a valid chain.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800592 */
Junyu Lai626045a2023-08-28 18:49:44 +0800593 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi9be2ea02022-07-05 06:14:59 +0000594 public void replaceUidChain(final int chain, final int[] uids) {
595 throwIfPreT("replaceUidChain is not available on pre-T devices");
596
Motomu Utsumic7c16852022-08-03 06:51:41 +0000597 if (sEnableJavaBpfMap) {
598 final long match;
599 try {
600 match = getMatchByFirewallChain(chain);
601 } catch (ServiceSpecificException e) {
602 // Throws IllegalArgumentException to keep the behavior of
603 // ConnectivityManager#replaceFirewallChain API
604 throw new IllegalArgumentException("Invalid firewall chain: " + chain);
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000605 }
Motomu Utsumi1a477b02022-08-23 15:14:56 +0900606 final Set<Integer> uidSet = asSet(uids);
607 final Set<Integer> uidSetToRemoveRule = new ArraySet<>();
Motomu Utsumic7c16852022-08-03 06:51:41 +0000608 try {
609 synchronized (sUidOwnerMap) {
610 sUidOwnerMap.forEach((uid, config) -> {
611 // config could be null if there is a concurrent entry deletion.
Motomu Utsumi8e420ea2022-08-24 18:03:30 +0900612 // http://b/220084230. But sUidOwnerMap update must be done while holding a
613 // lock, so this should not happen.
614 if (config == null) {
615 Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock");
616 } else if (!uidSet.contains((int) uid.val) && (config.rule & match) != 0) {
Motomu Utsumic7c16852022-08-03 06:51:41 +0000617 uidSetToRemoveRule.add((int) uid.val);
618 }
619 });
620
621 for (final int uid : uidSetToRemoveRule) {
622 removeRule(uid, match, "replaceUidChain");
623 }
624 for (final int uid : uids) {
625 addRule(uid, match, "replaceUidChain");
626 }
627 }
628 } catch (ErrnoException | ServiceSpecificException e) {
629 Log.e(TAG, "replaceUidChain failed: " + e);
630 }
631 } else {
632 final int err;
633 switch (chain) {
634 case FIREWALL_CHAIN_DOZABLE:
635 err = native_replaceUidChain("fw_dozable", true /* isAllowList */, uids);
636 break;
637 case FIREWALL_CHAIN_STANDBY:
638 err = native_replaceUidChain("fw_standby", false /* isAllowList */, uids);
639 break;
640 case FIREWALL_CHAIN_POWERSAVE:
641 err = native_replaceUidChain("fw_powersave", true /* isAllowList */, uids);
642 break;
643 case FIREWALL_CHAIN_RESTRICTED:
644 err = native_replaceUidChain("fw_restricted", true /* isAllowList */, uids);
645 break;
646 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
647 err = native_replaceUidChain(
648 "fw_low_power_standby", true /* isAllowList */, uids);
649 break;
650 case FIREWALL_CHAIN_OEM_DENY_1:
651 err = native_replaceUidChain("fw_oem_deny_1", false /* isAllowList */, uids);
652 break;
653 case FIREWALL_CHAIN_OEM_DENY_2:
654 err = native_replaceUidChain("fw_oem_deny_2", false /* isAllowList */, uids);
655 break;
656 case FIREWALL_CHAIN_OEM_DENY_3:
657 err = native_replaceUidChain("fw_oem_deny_3", false /* isAllowList */, uids);
658 break;
659 default:
660 throw new IllegalArgumentException("replaceFirewallChain with invalid chain: "
661 + chain);
662 }
663 if (err != 0) {
664 Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err));
665 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800666 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800667 }
668
Ken Chenf5f51332022-01-28 10:08:16 +0800669 /**
670 * Set firewall rule for uid
671 *
672 * @param childChain target chain
673 * @param uid uid to allow/deny
674 * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
Ken Chenf5f51332022-01-28 10:08:16 +0800675 * @throws ServiceSpecificException in case of failure, with an error code indicating the
676 * cause of the failure.
677 */
Junyu Lai626045a2023-08-28 18:49:44 +0800678 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Lorenzo Colitti82244fd2022-03-04 23:15:00 +0900679 public void setUidRule(final int childChain, final int uid, final int firewallRule) {
Motomu Utsumi40230be2022-07-05 03:27:35 +0000680 throwIfPreT("setUidRule is not available on pre-T devices");
681
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000682 if (sEnableJavaBpfMap) {
683 final long match = getMatchByFirewallChain(childChain);
684 final boolean isAllowList = isFirewallAllowList(childChain);
685 final boolean add = (firewallRule == FIREWALL_RULE_ALLOW && isAllowList)
686 || (firewallRule == FIREWALL_RULE_DENY && !isAllowList);
Motomu Utsumi40230be2022-07-05 03:27:35 +0000687
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000688 if (add) {
689 addRule(uid, match, "setUidRule");
690 } else {
691 removeRule(uid, match, "setUidRule");
692 }
Motomu Utsumi40230be2022-07-05 03:27:35 +0000693 } else {
Motomu Utsumi381ad9e2022-08-03 06:42:47 +0000694 final int err = native_setUidRule(childChain, uid, firewallRule);
695 maybeThrow(err, "Unable to set uid rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000696 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800697 }
698
699 /**
Motomu Utsumi56c412a2023-01-19 15:58:39 +0900700 * Get firewall rule of specified firewall chain on specified uid.
701 *
702 * @param childChain target chain
703 * @param uid target uid
704 * @return either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY
705 * @throws UnsupportedOperationException if called on pre-T devices.
706 * @throws ServiceSpecificException in case of failure, with an error code indicating the
707 * cause of the failure.
Junyu Lai626045a2023-08-28 18:49:44 +0800708 *
709 * @deprecated use {@link BpfNetMapsReader#getUidRule} instead.
Motomu Utsumi56c412a2023-01-19 15:58:39 +0900710 */
Junyu Lai626045a2023-08-28 18:49:44 +0800711 // TODO: Migrate the callers to use {@link BpfNetMapsReader#getUidRule} instead.
Motomu Utsumi56c412a2023-01-19 15:58:39 +0900712 public int getUidRule(final int childChain, final int uid) {
Junyu Lai626045a2023-08-28 18:49:44 +0800713 return BpfNetMapsReader.getUidRule(sUidOwnerMap, childChain, uid);
Motomu Utsumi56c412a2023-01-19 15:58:39 +0900714 }
715
Motomu Utsumid44a33a2023-03-28 18:08:12 +0900716 private Set<Integer> getUidsMatchEnabled(final int childChain) throws ErrnoException {
717 final long match = getMatchByFirewallChain(childChain);
718 Set<Integer> uids = new ArraySet<>();
719 synchronized (sUidOwnerMap) {
720 sUidOwnerMap.forEach((uid, val) -> {
721 if (val == null) {
722 Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock");
723 } else {
724 if ((val.rule & match) != 0) {
725 uids.add(uid.val);
726 }
727 }
728 });
729 }
730 return uids;
731 }
732
733 /**
734 * Get uids that has FIREWALL_RULE_ALLOW on allowlist chain.
735 * Allowlist means the firewall denies all by default, uids must be explicitly allowed.
736 *
737 * Note that uids that has FIREWALL_RULE_DENY on allowlist chain can not be computed from the
738 * bpf map, since all the uids that does not have explicit FIREWALL_RULE_ALLOW rule in bpf map
739 * are determined to have FIREWALL_RULE_DENY.
740 *
741 * @param childChain target chain
742 * @return Set of uids
743 */
744 public Set<Integer> getUidsWithAllowRuleOnAllowListChain(final int childChain)
745 throws ErrnoException {
746 if (!isFirewallAllowList(childChain)) {
747 throw new IllegalArgumentException("getUidsWithAllowRuleOnAllowListChain is called with"
748 + " denylist chain:" + childChain);
749 }
750 // Corresponding match is enabled for uids that has FIREWALL_RULE_ALLOW on allowlist chain.
751 return getUidsMatchEnabled(childChain);
752 }
753
754 /**
755 * Get uids that has FIREWALL_RULE_DENY on denylist chain.
756 * Denylist means the firewall allows all by default, uids must be explicitly denyed
757 *
758 * Note that uids that has FIREWALL_RULE_ALLOW on denylist chain can not be computed from the
759 * bpf map, since all the uids that does not have explicit FIREWALL_RULE_DENY rule in bpf map
760 * are determined to have the FIREWALL_RULE_ALLOW.
761 *
762 * @param childChain target chain
763 * @return Set of uids
764 */
765 public Set<Integer> getUidsWithDenyRuleOnDenyListChain(final int childChain)
766 throws ErrnoException {
767 if (isFirewallAllowList(childChain)) {
768 throw new IllegalArgumentException("getUidsWithDenyRuleOnDenyListChain is called with"
769 + " allowlist chain:" + childChain);
770 }
771 // Corresponding match is enabled for uids that has FIREWALL_RULE_DENY on denylist chain.
772 return getUidsMatchEnabled(childChain);
773 }
774
Motomu Utsumi56c412a2023-01-19 15:58:39 +0900775 /**
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800776 * Add ingress interface filtering rules to a list of UIDs
777 *
778 * For a given uid, once a filtering rule is added, the kernel will only allow packets from the
779 * allowed interface and loopback to be sent to the list of UIDs.
780 *
781 * Calling this method on one or more UIDs with an existing filtering rule but a different
782 * interface name will result in the filtering rule being updated to allow the new interface
783 * instead. Otherwise calling this method will not affect existing rules set on other UIDs.
784 *
785 * @param ifName the name of the interface on which the filtering rules will allow packets to
Ken Chenf5f51332022-01-28 10:08:16 +0800786 * be received.
787 * @param uids an array of UIDs which the filtering rules will be set
788 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800789 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800790 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800791 */
Ken Chenf5f51332022-01-28 10:08:16 +0800792 public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000793 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800794 mNetd.firewallAddUidInterfaceRules(ifName, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800795 return;
796 }
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000797
798 if (sEnableJavaBpfMap) {
799 // Null ifName is a wildcard to allow apps to receive packets on all interfaces and
800 // ifIndex is set to 0.
801 final int ifIndex;
802 if (ifName == null) {
803 ifIndex = 0;
804 } else {
805 ifIndex = mDeps.getIfIndex(ifName);
806 if (ifIndex == 0) {
807 throw new ServiceSpecificException(ENODEV,
808 "Failed to get index of interface " + ifName);
809 }
810 }
811 for (final int uid : uids) {
812 try {
813 addRule(uid, IIF_MATCH, ifIndex, "addUidInterfaceRules");
814 } catch (ServiceSpecificException e) {
815 Log.e(TAG, "addRule failed uid=" + uid + " ifName=" + ifName + ", " + e);
816 }
817 }
Motomu Utsumi5f52f4f2022-06-30 03:31:09 +0000818 } else {
Motomu Utsumif794e7d2022-08-03 06:38:43 +0000819 final int err = native_addUidInterfaceRules(ifName, uids);
820 maybeThrow(err, "Unable to add uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000821 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800822 }
823
824 /**
825 * Remove ingress interface filtering rules from a list of UIDs
826 *
827 * Clear the ingress interface filtering rules from the list of UIDs which were previously set
828 * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule.
829 *
830 * @param uids an array of UIDs from which the filtering rules will be removed
Ken Chenf5f51332022-01-28 10:08:16 +0800831 * @throws RemoteException when netd has crashed.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800832 * @throws ServiceSpecificException in case of failure, with an error code indicating the
Ken Chenf5f51332022-01-28 10:08:16 +0800833 * cause of the failure.
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800834 */
Ken Chenf5f51332022-01-28 10:08:16 +0800835 public void removeUidInterfaceRules(final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000836 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800837 mNetd.firewallRemoveUidInterfaceRules(uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800838 return;
839 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000840
841 if (sEnableJavaBpfMap) {
842 for (final int uid : uids) {
843 try {
844 removeRule(uid, IIF_MATCH, "removeUidInterfaceRules");
845 } catch (ServiceSpecificException e) {
846 Log.e(TAG, "removeRule failed uid=" + uid + ", " + e);
847 }
Motomu Utsumi599c4e52022-06-30 03:37:18 +0000848 }
Motomu Utsumi7dc657d2022-08-03 06:40:46 +0000849 } else {
850 final int err = native_removeUidInterfaceRules(uids);
851 maybeThrow(err, "Unable to remove uid interface rules");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000852 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800853 }
854
Ken Chenf5f51332022-01-28 10:08:16 +0800855 /**
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000856 * Update lockdown rule for uid
857 *
858 * @param uid target uid to add/remove the rule
859 * @param add {@code true} to add the rule, {@code false} to remove the rule.
860 * @throws ServiceSpecificException in case of failure, with an error code indicating the
861 * cause of the failure.
862 */
Junyu Lai626045a2023-08-28 18:49:44 +0800863 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000864 public void updateUidLockdownRule(final int uid, final boolean add) {
Motomu Utsumi697b2992022-06-30 02:25:29 +0000865 throwIfPreT("updateUidLockdownRule is not available on pre-T devices");
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000866
867 if (sEnableJavaBpfMap) {
868 if (add) {
869 addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
870 } else {
871 removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule");
872 }
Motomu Utsumi697b2992022-06-30 02:25:29 +0000873 } else {
Motomu Utsumib2d32b72022-08-03 06:31:58 +0000874 final int err = native_updateUidLockdownRule(uid, add);
875 maybeThrow(err, "Unable to update lockdown rule");
Motomu Utsumi5a68a212022-06-24 10:14:31 +0000876 }
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +0000877 }
878
879 /**
Ken Chenf5f51332022-01-28 10:08:16 +0800880 * Request netd to change the current active network stats map.
881 *
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000882 * @throws UnsupportedOperationException if called on pre-T devices.
Ken Chenf5f51332022-01-28 10:08:16 +0800883 * @throws ServiceSpecificException in case of failure, with an error code indicating the
884 * cause of the failure.
885 */
Junyu Lai626045a2023-08-28 18:49:44 +0800886 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
markchien49e944c2022-03-01 15:22:20 +0800887 public void swapActiveStatsMap() {
Motomu Utsumi7abeaa42022-07-20 07:54:18 +0000888 throwIfPreT("swapActiveStatsMap is not available on pre-T devices");
889
890 if (sEnableJavaBpfMap) {
891 try {
892 synchronized (sCurrentStatsMapConfigLock) {
893 final long config = sConfigurationMap.getValue(
894 CURRENT_STATS_MAP_CONFIGURATION_KEY).val;
895 final long newConfig = (config == STATS_SELECT_MAP_A)
896 ? STATS_SELECT_MAP_B : STATS_SELECT_MAP_A;
897 sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY,
898 new U32(newConfig));
899 }
900 } catch (ErrnoException e) {
901 throw new ServiceSpecificException(e.errno, "Failed to swap active stats map");
902 }
903
904 // After changing the config, it's needed to make sure all the current running eBPF
905 // programs are finished and all the CPUs are aware of this config change before the old
906 // map is modified. So special hack is needed here to wait for the kernel to do a
907 // synchronize_rcu(). Once the kernel called synchronize_rcu(), the updated config will
908 // be available to all cores and the next eBPF programs triggered inside the kernel will
909 // use the new map configuration. So once this function returns it is safe to modify the
910 // old stats map without concerning about race between the kernel and userspace.
911 final int err = mDeps.synchronizeKernelRCU();
912 maybeThrow(err, "synchronizeKernelRCU failed");
913 } else {
914 final int err = native_swapActiveStatsMap();
915 maybeThrow(err, "Unable to swap active stats map");
916 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800917 }
918
Ken Chenf5f51332022-01-28 10:08:16 +0800919 /**
920 * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids
921 * specified. Or remove all permissions from the uids.
922 *
923 * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or
924 * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then
925 * revoke all permissions for the uids.
926 * @param uids uid of users to grant permission
927 * @throws RemoteException when netd has crashed.
928 */
929 public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +0000930 if (PRE_T) {
Ken Chenf5f51332022-01-28 10:08:16 +0800931 mNetd.trafficSetNetPermForUids(permissions, uids);
Wayne Ma2fde98c2022-01-17 18:04:05 +0800932 return;
933 }
Motomu Utsumi65271202022-07-05 08:21:41 +0000934
935 if (sEnableJavaBpfMap) {
936 // Remove the entry if package is uninstalled or uid has only INTERNET permission.
937 if (permissions == PERMISSION_UNINSTALLED || permissions == PERMISSION_INTERNET) {
938 for (final int uid : uids) {
939 try {
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000940 sUidPermissionMap.deleteEntry(new S32(uid));
Motomu Utsumi65271202022-07-05 08:21:41 +0000941 } catch (ErrnoException e) {
942 Log.e(TAG, "Failed to remove uid " + uid + " from permission map: " + e);
943 }
944 }
945 return;
946 }
947
948 for (final int uid : uids) {
949 try {
Maciej Żenczykowski785793f2022-09-17 02:35:20 +0000950 sUidPermissionMap.updateEntry(new S32(uid), new U8((short) permissions));
Motomu Utsumi65271202022-07-05 08:21:41 +0000951 } catch (ErrnoException e) {
952 Log.e(TAG, "Failed to set permission "
953 + permissions + " to uid " + uid + ": " + e);
954 }
955 }
956 } else {
957 native_setPermissionForUids(permissions, uids);
958 }
Wayne Ma0ea3bdc2022-01-12 01:12:11 +0800959 }
960
Ken Chen24330172023-10-20 13:02:14 +0800961 /**
962 * Set Data Saver enabled or disabled
963 *
964 * @param enable whether Data Saver is enabled or disabled.
965 * @throws UnsupportedOperationException if called on pre-T devices.
966 * @throws ServiceSpecificException in case of failure, with an error code indicating the
967 * cause of the failure.
968 */
969 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
970 public void setDataSaverEnabled(boolean enable) {
971 throwIfPreT("setDataSaverEnabled is not available on pre-T devices");
972
973 try {
974 final short config = enable ? DATA_SAVER_ENABLED : DATA_SAVER_DISABLED;
975 sDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(config));
976 } catch (ErrnoException e) {
977 throw new ServiceSpecificException(e.errno, "Unable to set data saver: "
978 + Os.strerror(e.errno));
979 }
980 }
981
Motomu Utsumi166f9662022-09-01 10:35:29 +0900982 /** Register callback for statsd to pull atom. */
Junyu Lai626045a2023-08-28 18:49:44 +0800983 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi166f9662022-09-01 10:35:29 +0900984 public void setPullAtomCallback(final Context context) {
985 throwIfPreT("setPullAtomCallback is not available on pre-T devices");
986
987 final StatsManager statsManager = context.getSystemService(StatsManager.class);
988 statsManager.setPullAtomCallback(NETWORK_BPF_MAP_INFO, null /* metadata */,
989 BackgroundThread.getExecutor(), this::pullBpfMapInfoAtom);
990 }
991
992 private <K extends Struct, V extends Struct> int getMapSize(IBpfMap<K, V> map)
993 throws ErrnoException {
994 // forEach could restart iteration from the beginning if there is a concurrent entry
995 // deletion. netd and skDestroyListener could delete CookieTagMap entry concurrently.
996 // So using Set to count the number of entry in the map.
997 Set<K> keySet = new ArraySet<>();
998 map.forEach((k, v) -> keySet.add(k));
999 return keySet.size();
1000 }
1001
1002 /** Callback for StatsManager#setPullAtomCallback */
1003 @VisibleForTesting
1004 public int pullBpfMapInfoAtom(final int atomTag, final List<StatsEvent> data) {
1005 if (atomTag != NETWORK_BPF_MAP_INFO) {
1006 Log.e(TAG, "Unexpected atom tag: " + atomTag);
1007 return StatsManager.PULL_SKIP;
1008 }
1009
1010 try {
1011 data.add(mDeps.buildStatsEvent(getMapSize(sCookieTagMap), getMapSize(sUidOwnerMap),
1012 getMapSize(sUidPermissionMap)));
1013 } catch (ErrnoException e) {
1014 Log.e(TAG, "Failed to pull NETWORK_BPF_MAP_INFO atom: " + e);
1015 return StatsManager.PULL_SKIP;
1016 }
1017 return StatsManager.PULL_SUCCESS;
1018 }
1019
Motomu Utsumi310850f2022-09-02 12:48:20 +09001020 private String permissionToString(int permissionMask) {
1021 if (permissionMask == PERMISSION_NONE) {
1022 return "PERMISSION_NONE";
1023 }
1024 if (permissionMask == PERMISSION_UNINSTALLED) {
1025 // PERMISSION_UNINSTALLED should never appear in the map
1026 return "PERMISSION_UNINSTALLED error!";
1027 }
1028
1029 final StringJoiner sj = new StringJoiner(" ");
1030 for (Pair<Integer, String> permission: PERMISSION_LIST) {
1031 final int permissionFlag = permission.first;
1032 final String permissionName = permission.second;
1033 if ((permissionMask & permissionFlag) != 0) {
1034 sj.add(permissionName);
1035 permissionMask &= ~permissionFlag;
1036 }
1037 }
1038 if (permissionMask != 0) {
1039 sj.add("PERMISSION_UNKNOWN(" + permissionMask + ")");
1040 }
1041 return sj.toString();
1042 }
1043
Motomu Utsumi372c9b42022-09-02 19:02:56 +09001044 private void dumpOwnerMatchConfig(final IndentingPrintWriter pw) {
1045 try {
1046 final long match = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val;
1047 pw.println("current ownerMatch configuration: " + match + " " + matchToString(match));
1048 } catch (ErrnoException e) {
1049 pw.println("Failed to read ownerMatch configuration: " + e);
1050 }
1051 }
1052
Motomu Utsumic675d6f2022-09-02 18:15:25 +09001053 private void dumpCurrentStatsMapConfig(final IndentingPrintWriter pw) {
1054 try {
1055 final long config = sConfigurationMap.getValue(CURRENT_STATS_MAP_CONFIGURATION_KEY).val;
1056 final String currentStatsMap =
1057 (config == STATS_SELECT_MAP_A) ? "SELECT_MAP_A" : "SELECT_MAP_B";
1058 pw.println("current statsMap configuration: " + config + " " + currentStatsMap);
1059 } catch (ErrnoException e) {
1060 pw.println("Falied to read current statsMap configuration: " + e);
1061 }
1062 }
1063
Ken Chen24330172023-10-20 13:02:14 +08001064 private void dumpDataSaverConfig(final IndentingPrintWriter pw) {
1065 try {
1066 final short config = sDataSaverEnabledMap.getValue(DATA_SAVER_ENABLED_KEY).val;
1067 // Any non-zero value converted from short to boolean is true by convention.
1068 pw.println("sDataSaverEnabledMap: " + (config != DATA_SAVER_DISABLED));
1069 } catch (ErrnoException e) {
1070 pw.println("Failed to read data saver configuration: " + e);
1071 }
1072 }
Ken Chene6d511f2022-01-25 11:10:42 +08001073 /**
1074 * Dump BPF maps
1075 *
Motomu Utsumi310850f2022-09-02 12:48:20 +09001076 * @param pw print writer
Ken Chene6d511f2022-01-25 11:10:42 +08001077 * @param fd file descriptor to output
Motomu Utsumi310850f2022-09-02 12:48:20 +09001078 * @param verbose verbose dump flag, if true dump the BpfMap contents
Ken Chene6d511f2022-01-25 11:10:42 +08001079 * @throws IOException when file descriptor is invalid.
1080 * @throws ServiceSpecificException when the method is called on an unsupported device.
1081 */
Junyu Lai626045a2023-08-28 18:49:44 +08001082 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi310850f2022-09-02 12:48:20 +09001083 public void dump(final IndentingPrintWriter pw, final FileDescriptor fd, boolean verbose)
Ken Chene6d511f2022-01-25 11:10:42 +08001084 throws IOException, ServiceSpecificException {
Motomu Utsumi25cf86f2022-06-27 08:50:19 +00001085 if (PRE_T) {
Ken Chene6d511f2022-01-25 11:10:42 +08001086 throw new ServiceSpecificException(
1087 EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T"
1088 + " devices, use dumpsys netd trafficcontroller instead.");
1089 }
Maciej Żenczykowskid70a3302023-09-06 16:45:25 +00001090
1091 pw.println("TrafficController"); // required by CTS testDumpBpfNetMaps
Motomu Utsumi310850f2022-09-02 12:48:20 +09001092
Motomu Utsumi316d0572022-09-15 13:24:48 +09001093 pw.println();
1094 pw.println("sEnableJavaBpfMap: " + sEnableJavaBpfMap);
Motomu Utsumi310850f2022-09-02 12:48:20 +09001095 if (verbose) {
Motomu Utsumief546a92022-10-05 16:42:29 +09001096 pw.println();
1097 pw.println("BPF map content:");
1098 pw.increaseIndent();
1099
Motomu Utsumi372c9b42022-09-02 19:02:56 +09001100 dumpOwnerMatchConfig(pw);
Motomu Utsumic675d6f2022-09-02 18:15:25 +09001101 dumpCurrentStatsMapConfig(pw);
1102 pw.println();
1103
Motomu Utsumief546a92022-10-05 16:42:29 +09001104 // TODO: Remove CookieTagMap content dump
1105 // NetworkStatsService also dumps CookieTagMap and NetworkStatsService is a right place
1106 // to dump CookieTagMap. But the TagSocketTest in CTS depends on this dump so the tests
1107 // need to be updated before remove the dump from BpfNetMaps.
1108 BpfDump.dumpMap(sCookieTagMap, pw, "sCookieTagMap",
1109 (key, value) -> "cookie=" + key.socketCookie
1110 + " tag=0x" + Long.toHexString(value.tag)
1111 + " uid=" + value.uid);
Motomu Utsumi956d86c2022-09-02 17:01:25 +09001112 BpfDump.dumpMap(sUidOwnerMap, pw, "sUidOwnerMap",
1113 (uid, match) -> {
1114 if ((match.rule & IIF_MATCH) != 0) {
1115 // TODO: convert interface index to interface name by IfaceIndexNameMap
1116 return uid.val + " " + matchToString(match.rule) + " " + match.iif;
1117 } else {
1118 return uid.val + " " + matchToString(match.rule);
1119 }
1120 });
Motomu Utsumi310850f2022-09-02 12:48:20 +09001121 BpfDump.dumpMap(sUidPermissionMap, pw, "sUidPermissionMap",
1122 (uid, permission) -> uid.val + " " + permissionToString(permission.val));
Ken Chen24330172023-10-20 13:02:14 +08001123
1124 dumpDataSaverConfig(pw);
Motomu Utsumief546a92022-10-05 16:42:29 +09001125 pw.decreaseIndent();
Motomu Utsumi310850f2022-09-02 12:48:20 +09001126 }
Ken Chene6d511f2022-01-25 11:10:42 +08001127 }
1128
Maciej Żenczykowski70fbfa42023-07-18 20:14:38 +00001129 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi3af8f0e2022-09-02 23:42:13 +09001130 private static native void native_init(boolean startSkDestroyListener);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001131
1132 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001133 private native int native_addNaughtyApp(int uid);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001134
1135 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001136 private native int native_removeNaughtyApp(int uid);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001137
1138 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001139 private native int native_addNiceApp(int uid);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001140
1141 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001142 private native int native_removeNiceApp(int uid);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001143
1144 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi114cd9c2022-08-01 02:08:35 +00001145 private native int native_setChildChain(int childChain, boolean enable);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001146
1147 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001148 private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001149
1150 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001151 private native int native_setUidRule(int childChain, int uid, int firewallRule);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001152
1153 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001154 private native int native_addUidInterfaceRules(String ifName, int[] uids);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001155
1156 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001157 private native int native_removeUidInterfaceRules(int[] uids);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001158
1159 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi8b42e6d2022-05-19 06:23:40 +00001160 private native int native_updateUidLockdownRule(int uid, boolean add);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001161
1162 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001163 private native int native_swapActiveStatsMap();
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001164
1165 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Wayne Ma2fde98c2022-01-17 18:04:05 +08001166 private native void native_setPermissionForUids(int permissions, int[] uids);
Maciej Żenczykowskie7da6fd2023-07-18 14:19:19 +00001167
1168 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
Motomu Utsumi7abeaa42022-07-20 07:54:18 +00001169 private static native int native_synchronizeKernelRCU();
Wayne Ma0ea3bdc2022-01-12 01:12:11 +08001170}