Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE; |
| 20 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY; |
| 21 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1; |
| 22 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2; |
| 23 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3; |
| 24 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE; |
| 25 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED; |
| 26 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY; |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.FIREWALL_RULE_ALLOW; |
| 28 | import static android.net.ConnectivityManager.FIREWALL_RULE_DENY; |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 29 | import static android.system.OsConstants.EINVAL; |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 30 | import static android.system.OsConstants.ENODEV; |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 31 | import static android.system.OsConstants.ENOENT; |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 32 | import static android.system.OsConstants.EOPNOTSUPP; |
| 33 | |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 34 | import android.content.Context; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 35 | import android.net.INetd; |
| 36 | import android.os.RemoteException; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 37 | import android.os.ServiceSpecificException; |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 38 | import android.provider.DeviceConfig; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 39 | import android.system.ErrnoException; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 40 | import android.system.Os; |
| 41 | import android.util.Log; |
| 42 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 43 | import com.android.internal.annotations.GuardedBy; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 44 | import com.android.internal.annotations.VisibleForTesting; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 45 | import com.android.modules.utils.build.SdkLevel; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 46 | import com.android.net.module.util.BpfMap; |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 47 | import com.android.net.module.util.DeviceConfigUtils; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 48 | import com.android.net.module.util.Struct.U32; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 49 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 50 | import java.io.FileDescriptor; |
| 51 | import java.io.IOException; |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 52 | import java.util.Arrays; |
| 53 | import java.util.HashSet; |
| 54 | import java.util.Set; |
| 55 | import java.util.stream.Collectors; |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 56 | |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 57 | /** |
| 58 | * BpfNetMaps is responsible for providing traffic controller relevant functionality. |
| 59 | * |
| 60 | * {@hide} |
| 61 | */ |
| 62 | public class BpfNetMaps { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 63 | private static final boolean PRE_T = !SdkLevel.isAtLeastT(); |
| 64 | static { |
| 65 | if (!PRE_T) { |
| 66 | System.loadLibrary("service-connectivity"); |
| 67 | } |
| 68 | } |
| 69 | |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 70 | private static final String TAG = "BpfNetMaps"; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 71 | private final INetd mNetd; |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 72 | private final Dependencies mDeps; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 73 | // Use legacy netd for releases before T. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 74 | private static boolean sInitialized = false; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 75 | |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 76 | private static Boolean sEnableJavaBpfMap = null; |
| 77 | private static final String BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP = |
| 78 | "bpf_net_maps_enable_java_bpf_map"; |
| 79 | |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 80 | // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY. |
| 81 | // This entry is not accessed by others. |
| 82 | // BpfNetMaps acquires this lock while sequence of read, modify, and write. |
| 83 | private static final Object sUidRulesConfigBpfMapLock = new Object(); |
| 84 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 85 | private static final String CONFIGURATION_MAP_PATH = |
| 86 | "/sys/fs/bpf/netd_shared/map_netd_configuration_map"; |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 87 | private static final String UID_OWNER_MAP_PATH = |
| 88 | "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map"; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 89 | private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0); |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 90 | private static final U32 CURRENT_STATS_MAP_CONFIGURATION_KEY = new U32(1); |
| 91 | private static final long UID_RULES_DEFAULT_CONFIGURATION = 0; |
| 92 | private static final long STATS_SELECT_MAP_A = 0; |
| 93 | private static final long STATS_SELECT_MAP_B = 1; |
| 94 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 95 | private static BpfMap<U32, U32> sConfigurationMap = null; |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 96 | // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others. |
| 97 | private static BpfMap<U32, UidOwnerValue> sUidOwnerMap = null; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 98 | |
| 99 | // LINT.IfChange(match_type) |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 100 | @VisibleForTesting public static final long NO_MATCH = 0; |
| 101 | @VisibleForTesting public static final long HAPPY_BOX_MATCH = (1 << 0); |
| 102 | @VisibleForTesting public static final long PENALTY_BOX_MATCH = (1 << 1); |
| 103 | @VisibleForTesting public static final long DOZABLE_MATCH = (1 << 2); |
| 104 | @VisibleForTesting public static final long STANDBY_MATCH = (1 << 3); |
| 105 | @VisibleForTesting public static final long POWERSAVE_MATCH = (1 << 4); |
| 106 | @VisibleForTesting public static final long RESTRICTED_MATCH = (1 << 5); |
| 107 | @VisibleForTesting public static final long LOW_POWER_STANDBY_MATCH = (1 << 6); |
| 108 | @VisibleForTesting public static final long IIF_MATCH = (1 << 7); |
| 109 | @VisibleForTesting public static final long LOCKDOWN_VPN_MATCH = (1 << 8); |
| 110 | @VisibleForTesting public static final long OEM_DENY_1_MATCH = (1 << 9); |
| 111 | @VisibleForTesting public static final long OEM_DENY_2_MATCH = (1 << 10); |
| 112 | @VisibleForTesting public static final long OEM_DENY_3_MATCH = (1 << 11); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 113 | // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h) |
| 114 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 115 | /** |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 116 | * Set sEnableJavaBpfMap for test. |
| 117 | */ |
| 118 | @VisibleForTesting |
| 119 | public static void setEnableJavaBpfMapForTest(boolean enable) { |
| 120 | sEnableJavaBpfMap = enable; |
| 121 | } |
| 122 | |
| 123 | /** |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 124 | * Set configurationMap for test. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 125 | */ |
| 126 | @VisibleForTesting |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 127 | public static void setConfigurationMapForTest(BpfMap<U32, U32> configurationMap) { |
| 128 | sConfigurationMap = configurationMap; |
| 129 | } |
| 130 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 131 | /** |
| 132 | * Set uidOwnerMap for test. |
| 133 | */ |
| 134 | @VisibleForTesting |
| 135 | public static void setUidOwnerMapForTest(BpfMap<U32, UidOwnerValue> uidOwnerMap) { |
| 136 | sUidOwnerMap = uidOwnerMap; |
| 137 | } |
| 138 | |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 139 | private static BpfMap<U32, U32> getConfigurationMap() { |
| 140 | try { |
| 141 | return new BpfMap<>( |
| 142 | CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class); |
| 143 | } catch (ErrnoException e) { |
| 144 | throw new IllegalStateException("Cannot open netd configuration map", e); |
| 145 | } |
| 146 | } |
| 147 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 148 | private static BpfMap<U32, UidOwnerValue> getUidOwnerMap() { |
| 149 | try { |
| 150 | return new BpfMap<>( |
| 151 | UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class); |
| 152 | } catch (ErrnoException e) { |
| 153 | throw new IllegalStateException("Cannot open uid owner map", e); |
| 154 | } |
| 155 | } |
| 156 | |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 157 | private static void initBpfMaps() { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 158 | if (sConfigurationMap == null) { |
| 159 | sConfigurationMap = getConfigurationMap(); |
| 160 | } |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 161 | try { |
| 162 | sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, |
| 163 | new U32(UID_RULES_DEFAULT_CONFIGURATION)); |
| 164 | } catch (ErrnoException e) { |
| 165 | throw new IllegalStateException("Failed to initialize uid rules configuration", e); |
| 166 | } |
| 167 | try { |
| 168 | sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY, |
| 169 | new U32(STATS_SELECT_MAP_A)); |
| 170 | } catch (ErrnoException e) { |
| 171 | throw new IllegalStateException("Failed to initialize current stats configuration", e); |
| 172 | } |
| 173 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 174 | if (sUidOwnerMap == null) { |
| 175 | sUidOwnerMap = getUidOwnerMap(); |
| 176 | } |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 177 | try { |
| 178 | sUidOwnerMap.clear(); |
| 179 | } catch (ErrnoException e) { |
| 180 | throw new IllegalStateException("Failed to initialize uid owner map", e); |
| 181 | } |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 184 | /** |
| 185 | * Initializes the class if it is not already initialized. This method will open maps but not |
| 186 | * cause any other effects. This method may be called multiple times on any thread. |
| 187 | */ |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 188 | private static synchronized void ensureInitialized(final Context context) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 189 | if (sInitialized) return; |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 190 | if (sEnableJavaBpfMap == null) { |
| 191 | sEnableJavaBpfMap = DeviceConfigUtils.isFeatureEnabled(context, |
| 192 | DeviceConfig.NAMESPACE_TETHERING, BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP, |
| 193 | SdkLevel.isAtLeastU() /* defaultValue */); |
| 194 | } |
| 195 | Log.d(TAG, "BpfNetMaps is initialized with sEnableJavaBpfMap=" + sEnableJavaBpfMap); |
| 196 | |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 197 | initBpfMaps(); |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 198 | native_init(); |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 199 | sInitialized = true; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 202 | /** |
| 203 | * Dependencies of BpfNetMaps, for injection in tests. |
| 204 | */ |
| 205 | @VisibleForTesting |
| 206 | public static class Dependencies { |
| 207 | /** |
| 208 | * Get interface index. |
| 209 | */ |
| 210 | public int getIfIndex(final String ifName) { |
| 211 | return Os.if_nametoindex(ifName); |
| 212 | } |
| 213 | } |
| 214 | |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 215 | /** Constructor used after T that doesn't need to use netd anymore. */ |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 216 | public BpfNetMaps(final Context context) { |
| 217 | this(context, null); |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 218 | |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 219 | if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T"); |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 220 | } |
| 221 | |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 222 | public BpfNetMaps(final Context context, final INetd netd) { |
| 223 | this(context, netd, new Dependencies()); |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @VisibleForTesting |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 227 | public BpfNetMaps(final Context context, final INetd netd, final Dependencies deps) { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 228 | if (!PRE_T) { |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 229 | ensureInitialized(context); |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 230 | } |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 231 | mNetd = netd; |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 232 | mDeps = deps; |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 235 | /** |
| 236 | * Get corresponding match from firewall chain. |
| 237 | */ |
| 238 | @VisibleForTesting |
| 239 | public long getMatchByFirewallChain(final int chain) { |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 240 | switch (chain) { |
| 241 | case FIREWALL_CHAIN_DOZABLE: |
| 242 | return DOZABLE_MATCH; |
| 243 | case FIREWALL_CHAIN_STANDBY: |
| 244 | return STANDBY_MATCH; |
| 245 | case FIREWALL_CHAIN_POWERSAVE: |
| 246 | return POWERSAVE_MATCH; |
| 247 | case FIREWALL_CHAIN_RESTRICTED: |
| 248 | return RESTRICTED_MATCH; |
| 249 | case FIREWALL_CHAIN_LOW_POWER_STANDBY: |
| 250 | return LOW_POWER_STANDBY_MATCH; |
| 251 | case FIREWALL_CHAIN_OEM_DENY_1: |
| 252 | return OEM_DENY_1_MATCH; |
| 253 | case FIREWALL_CHAIN_OEM_DENY_2: |
| 254 | return OEM_DENY_2_MATCH; |
| 255 | case FIREWALL_CHAIN_OEM_DENY_3: |
| 256 | return OEM_DENY_3_MATCH; |
| 257 | default: |
| 258 | throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 259 | } |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get if the chain is allow list or not. |
| 264 | * |
| 265 | * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed |
| 266 | * DENYLIST means the firewall allows all by default, uids must be explicitly denyed |
| 267 | */ |
| 268 | @VisibleForTesting |
| 269 | public boolean isFirewallAllowList(final int chain) { |
| 270 | switch (chain) { |
| 271 | case FIREWALL_CHAIN_DOZABLE: |
| 272 | case FIREWALL_CHAIN_POWERSAVE: |
| 273 | case FIREWALL_CHAIN_RESTRICTED: |
| 274 | case FIREWALL_CHAIN_LOW_POWER_STANDBY: |
| 275 | return true; |
| 276 | case FIREWALL_CHAIN_STANDBY: |
| 277 | case FIREWALL_CHAIN_OEM_DENY_1: |
| 278 | case FIREWALL_CHAIN_OEM_DENY_2: |
| 279 | case FIREWALL_CHAIN_OEM_DENY_3: |
| 280 | return false; |
| 281 | default: |
| 282 | throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain); |
| 283 | } |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 286 | private void maybeThrow(final int err, final String msg) { |
| 287 | if (err != 0) { |
| 288 | throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err)); |
| 289 | } |
| 290 | } |
| 291 | |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 292 | private void throwIfPreT(final String msg) { |
| 293 | if (PRE_T) { |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 294 | throw new UnsupportedOperationException(msg); |
| 295 | } |
| 296 | } |
| 297 | |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 298 | private void removeRule(final int uid, final long match, final String caller) { |
| 299 | try { |
| 300 | synchronized (sUidOwnerMap) { |
| 301 | final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid)); |
| 302 | |
| 303 | if (oldMatch == null) { |
| 304 | throw new ServiceSpecificException(ENOENT, |
| 305 | "sUidOwnerMap does not have entry for uid: " + uid); |
| 306 | } |
| 307 | |
| 308 | final UidOwnerValue newMatch = new UidOwnerValue( |
| 309 | (match == IIF_MATCH) ? 0 : oldMatch.iif, |
| 310 | oldMatch.rule & ~match |
| 311 | ); |
| 312 | |
| 313 | if (newMatch.rule == 0) { |
| 314 | sUidOwnerMap.deleteEntry(new U32(uid)); |
| 315 | } else { |
| 316 | sUidOwnerMap.updateEntry(new U32(uid), newMatch); |
| 317 | } |
| 318 | } |
| 319 | } catch (ErrnoException e) { |
| 320 | throw new ServiceSpecificException(e.errno, |
| 321 | caller + " failed to remove rule: " + Os.strerror(e.errno)); |
| 322 | } |
| 323 | } |
| 324 | |
Motomu Utsumi | 389278e | 2022-06-28 07:05:05 +0000 | [diff] [blame] | 325 | private void addRule(final int uid, final long match, final long iif, final String caller) { |
| 326 | if (match != IIF_MATCH && iif != 0) { |
| 327 | throw new ServiceSpecificException(EINVAL, |
| 328 | "Non-interface match must have zero interface index"); |
| 329 | } |
| 330 | |
| 331 | try { |
| 332 | synchronized (sUidOwnerMap) { |
| 333 | final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new U32(uid)); |
| 334 | |
| 335 | final UidOwnerValue newMatch; |
| 336 | if (oldMatch != null) { |
| 337 | newMatch = new UidOwnerValue( |
| 338 | (match == IIF_MATCH) ? iif : oldMatch.iif, |
| 339 | oldMatch.rule | match |
| 340 | ); |
| 341 | } else { |
| 342 | newMatch = new UidOwnerValue( |
| 343 | iif, |
| 344 | match |
| 345 | ); |
| 346 | } |
| 347 | sUidOwnerMap.updateEntry(new U32(uid), newMatch); |
| 348 | } |
| 349 | } catch (ErrnoException e) { |
| 350 | throw new ServiceSpecificException(e.errno, |
| 351 | caller + " failed to add rule: " + Os.strerror(e.errno)); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | private void addRule(final int uid, final long match, final String caller) { |
| 356 | addRule(uid, match, 0 /* iif */, caller); |
| 357 | } |
| 358 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 359 | /** |
| 360 | * Add naughty app bandwidth rule for specific app |
| 361 | * |
| 362 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 363 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 364 | * cause of the failure. |
| 365 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 366 | public void addNaughtyApp(final int uid) { |
Motomu Utsumi | 389278e | 2022-06-28 07:05:05 +0000 | [diff] [blame] | 367 | throwIfPreT("addNaughtyApp is not available on pre-T devices"); |
Motomu Utsumi | 55c282e | 2022-08-03 06:25:33 +0000 | [diff] [blame] | 368 | |
| 369 | if (sEnableJavaBpfMap) { |
| 370 | addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp"); |
| 371 | } else { |
| 372 | final int err = native_addNaughtyApp(uid); |
| 373 | maybeThrow(err, "Unable to add naughty app"); |
| 374 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 375 | } |
| 376 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 377 | /** |
| 378 | * Remove naughty app bandwidth rule for specific app |
| 379 | * |
| 380 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 381 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 382 | * cause of the failure. |
| 383 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 384 | public void removeNaughtyApp(final int uid) { |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 385 | throwIfPreT("removeNaughtyApp is not available on pre-T devices"); |
Motomu Utsumi | 878ce0d | 2022-08-03 06:22:42 +0000 | [diff] [blame] | 386 | |
| 387 | if (sEnableJavaBpfMap) { |
| 388 | removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp"); |
| 389 | } else { |
| 390 | final int err = native_removeNaughtyApp(uid); |
| 391 | maybeThrow(err, "Unable to remove naughty app"); |
| 392 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 393 | } |
| 394 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 395 | /** |
| 396 | * Add nice app bandwidth rule for specific app |
| 397 | * |
| 398 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 399 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 400 | * cause of the failure. |
| 401 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 402 | public void addNiceApp(final int uid) { |
Motomu Utsumi | 55630d0 | 2022-06-29 07:46:52 +0000 | [diff] [blame] | 403 | throwIfPreT("addNiceApp is not available on pre-T devices"); |
| 404 | addRule(uid, HAPPY_BOX_MATCH, "addNiceApp"); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 405 | } |
| 406 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 407 | /** |
| 408 | * Remove nice app bandwidth rule for specific app |
| 409 | * |
| 410 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 411 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 412 | * cause of the failure. |
| 413 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 414 | public void removeNiceApp(final int uid) { |
Motomu Utsumi | 7392eb4 | 2022-06-29 03:53:03 +0000 | [diff] [blame] | 415 | throwIfPreT("removeNiceApp is not available on pre-T devices"); |
Motomu Utsumi | 5f15f75 | 2022-08-03 06:27:51 +0000 | [diff] [blame^] | 416 | |
| 417 | if (sEnableJavaBpfMap) { |
| 418 | removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp"); |
| 419 | } else { |
| 420 | final int err = native_removeNiceApp(uid); |
| 421 | maybeThrow(err, "Unable to remove nice app"); |
| 422 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 423 | } |
| 424 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 425 | /** |
| 426 | * Set target firewall child chain |
| 427 | * |
| 428 | * @param childChain target chain to enable |
| 429 | * @param enable whether to enable or disable child chain. |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 430 | * @throws UnsupportedOperationException if called on pre-T devices. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 431 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 432 | * cause of the failure. |
| 433 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 434 | public void setChildChain(final int childChain, final boolean enable) { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 435 | throwIfPreT("setChildChain is not available on pre-T devices"); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 436 | |
| 437 | final long match = getMatchByFirewallChain(childChain); |
| 438 | try { |
| 439 | synchronized (sUidRulesConfigBpfMapLock) { |
| 440 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 441 | final long newConfig = enable ? (config.val | match) : (config.val & ~match); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 442 | sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig)); |
| 443 | } |
| 444 | } catch (ErrnoException e) { |
| 445 | throw new ServiceSpecificException(e.errno, |
| 446 | "Unable to set child chain: " + Os.strerror(e.errno)); |
| 447 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 451 | * Get the specified firewall chain's status. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 452 | * |
| 453 | * @param childChain target chain |
| 454 | * @return {@code true} if chain is enabled, {@code false} if chain is not enabled. |
| 455 | * @throws UnsupportedOperationException if called on pre-T devices. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 456 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 457 | * cause of the failure. |
| 458 | */ |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 459 | public boolean isChainEnabled(final int childChain) { |
| 460 | throwIfPreT("isChainEnabled is not available on pre-T devices"); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 461 | |
| 462 | final long match = getMatchByFirewallChain(childChain); |
| 463 | try { |
| 464 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 465 | return (config.val & match) != 0; |
| 466 | } catch (ErrnoException e) { |
| 467 | throw new ServiceSpecificException(e.errno, |
| 468 | "Unable to get firewall chain status: " + Os.strerror(e.errno)); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /** |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 473 | * Replaces the contents of the specified UID-based firewall chain. |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 474 | * Enables the chain for specified uids and disables the chain for non-specified uids. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 475 | * |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 476 | * @param chain Target chain. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 477 | * @param uids The list of UIDs to allow/deny. |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 478 | * @throws UnsupportedOperationException if called on pre-T devices. |
| 479 | * @throws IllegalArgumentException if {@code chain} is not a valid chain. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 480 | */ |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 481 | public void replaceUidChain(final int chain, final int[] uids) { |
| 482 | throwIfPreT("replaceUidChain is not available on pre-T devices"); |
| 483 | |
| 484 | final long match; |
| 485 | try { |
| 486 | match = getMatchByFirewallChain(chain); |
| 487 | } catch (ServiceSpecificException e) { |
| 488 | // Throws IllegalArgumentException to keep the behavior of |
| 489 | // ConnectivityManager#replaceFirewallChain API |
| 490 | throw new IllegalArgumentException("Invalid firewall chain: " + chain); |
| 491 | } |
| 492 | final Set<Integer> uidSet = Arrays.stream(uids).boxed().collect(Collectors.toSet()); |
| 493 | final Set<Integer> uidSetToRemoveRule = new HashSet<>(); |
| 494 | try { |
| 495 | synchronized (sUidOwnerMap) { |
| 496 | sUidOwnerMap.forEach((uid, config) -> { |
| 497 | // config could be null if there is a concurrent entry deletion. |
| 498 | // http://b/220084230. |
| 499 | if (config != null |
| 500 | && !uidSet.contains((int) uid.val) && (config.rule & match) != 0) { |
| 501 | uidSetToRemoveRule.add((int) uid.val); |
| 502 | } |
| 503 | }); |
| 504 | |
| 505 | for (final int uid : uidSetToRemoveRule) { |
| 506 | removeRule(uid, match, "replaceUidChain"); |
| 507 | } |
| 508 | for (final int uid : uids) { |
| 509 | addRule(uid, match, "replaceUidChain"); |
| 510 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 511 | } |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 512 | } catch (ErrnoException | ServiceSpecificException e) { |
| 513 | Log.e(TAG, "replaceUidChain failed: " + e); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 514 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 515 | } |
| 516 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 517 | /** |
| 518 | * Set firewall rule for uid |
| 519 | * |
| 520 | * @param childChain target chain |
| 521 | * @param uid uid to allow/deny |
| 522 | * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 523 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 524 | * cause of the failure. |
| 525 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 526 | public void setUidRule(final int childChain, final int uid, final int firewallRule) { |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 527 | throwIfPreT("setUidRule is not available on pre-T devices"); |
| 528 | |
| 529 | final long match = getMatchByFirewallChain(childChain); |
| 530 | final boolean isAllowList = isFirewallAllowList(childChain); |
| 531 | final boolean add = (firewallRule == FIREWALL_RULE_ALLOW && isAllowList) |
| 532 | || (firewallRule == FIREWALL_RULE_DENY && !isAllowList); |
| 533 | |
| 534 | if (add) { |
| 535 | addRule(uid, match, "setUidRule"); |
| 536 | } else { |
| 537 | removeRule(uid, match, "setUidRule"); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 538 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Add ingress interface filtering rules to a list of UIDs |
| 543 | * |
| 544 | * For a given uid, once a filtering rule is added, the kernel will only allow packets from the |
| 545 | * allowed interface and loopback to be sent to the list of UIDs. |
| 546 | * |
| 547 | * Calling this method on one or more UIDs with an existing filtering rule but a different |
| 548 | * interface name will result in the filtering rule being updated to allow the new interface |
| 549 | * instead. Otherwise calling this method will not affect existing rules set on other UIDs. |
| 550 | * |
| 551 | * @param ifName the name of the interface on which the filtering rules will allow packets to |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 552 | * be received. |
| 553 | * @param uids an array of UIDs which the filtering rules will be set |
| 554 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 555 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 556 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 557 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 558 | public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 559 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 560 | mNetd.firewallAddUidInterfaceRules(ifName, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 561 | return; |
| 562 | } |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 563 | // Null ifName is a wildcard to allow apps to receive packets on all interfaces and ifIndex |
| 564 | // is set to 0. |
| 565 | final int ifIndex; |
| 566 | if (ifName == null) { |
| 567 | ifIndex = 0; |
| 568 | } else { |
| 569 | ifIndex = mDeps.getIfIndex(ifName); |
| 570 | if (ifIndex == 0) { |
| 571 | throw new ServiceSpecificException(ENODEV, |
| 572 | "Failed to get index of interface " + ifName); |
| 573 | } |
| 574 | } |
| 575 | for (final int uid: uids) { |
| 576 | try { |
| 577 | addRule(uid, IIF_MATCH, ifIndex, "addUidInterfaceRules"); |
| 578 | } catch (ServiceSpecificException e) { |
| 579 | Log.e(TAG, "addRule failed uid=" + uid + " ifName=" + ifName + ", " + e); |
| 580 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 581 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Remove ingress interface filtering rules from a list of UIDs |
| 586 | * |
| 587 | * Clear the ingress interface filtering rules from the list of UIDs which were previously set |
| 588 | * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule. |
| 589 | * |
| 590 | * @param uids an array of UIDs from which the filtering rules will be removed |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 591 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 592 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 593 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 594 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 595 | public void removeUidInterfaceRules(final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 596 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 597 | mNetd.firewallRemoveUidInterfaceRules(uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 598 | return; |
| 599 | } |
Motomu Utsumi | 599c4e5 | 2022-06-30 03:37:18 +0000 | [diff] [blame] | 600 | for (final int uid: uids) { |
| 601 | try { |
| 602 | removeRule(uid, IIF_MATCH, "removeUidInterfaceRules"); |
| 603 | } catch (ServiceSpecificException e) { |
| 604 | Log.e(TAG, "removeRule failed uid=" + uid + ", " + e); |
| 605 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 606 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 607 | } |
| 608 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 609 | /** |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 610 | * Update lockdown rule for uid |
| 611 | * |
| 612 | * @param uid target uid to add/remove the rule |
| 613 | * @param add {@code true} to add the rule, {@code false} to remove the rule. |
| 614 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 615 | * cause of the failure. |
| 616 | */ |
| 617 | public void updateUidLockdownRule(final int uid, final boolean add) { |
Motomu Utsumi | 697b299 | 2022-06-30 02:25:29 +0000 | [diff] [blame] | 618 | throwIfPreT("updateUidLockdownRule is not available on pre-T devices"); |
| 619 | if (add) { |
| 620 | addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule"); |
| 621 | } else { |
| 622 | removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule"); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 623 | } |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /** |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 627 | * Request netd to change the current active network stats map. |
| 628 | * |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 629 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 630 | * cause of the failure. |
| 631 | */ |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 632 | public void swapActiveStatsMap() { |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 633 | final int err = native_swapActiveStatsMap(); |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 634 | maybeThrow(err, "Unable to swap active stats map"); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 635 | } |
| 636 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 637 | /** |
| 638 | * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids |
| 639 | * specified. Or remove all permissions from the uids. |
| 640 | * |
| 641 | * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or |
| 642 | * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then |
| 643 | * revoke all permissions for the uids. |
| 644 | * @param uids uid of users to grant permission |
| 645 | * @throws RemoteException when netd has crashed. |
| 646 | */ |
| 647 | public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 648 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 649 | mNetd.trafficSetNetPermForUids(permissions, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | native_setPermissionForUids(permissions, uids); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 653 | } |
| 654 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 655 | /** |
| 656 | * Dump BPF maps |
| 657 | * |
| 658 | * @param fd file descriptor to output |
| 659 | * @throws IOException when file descriptor is invalid. |
| 660 | * @throws ServiceSpecificException when the method is called on an unsupported device. |
| 661 | */ |
| 662 | public void dump(final FileDescriptor fd, boolean verbose) |
| 663 | throws IOException, ServiceSpecificException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 664 | if (PRE_T) { |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 665 | throw new ServiceSpecificException( |
| 666 | EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T" |
| 667 | + " devices, use dumpsys netd trafficcontroller instead."); |
| 668 | } |
| 669 | native_dump(fd, verbose); |
| 670 | } |
| 671 | |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 672 | private static native void native_init(); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 673 | private native int native_addNaughtyApp(int uid); |
| 674 | private native int native_removeNaughtyApp(int uid); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 675 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 676 | private native int native_addNiceApp(int uid); |
| 677 | private native int native_removeNiceApp(int uid); |
Motomu Utsumi | 114cd9c | 2022-08-01 02:08:35 +0000 | [diff] [blame] | 678 | private native int native_setChildChain(int childChain, boolean enable); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 679 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 680 | private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 681 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 682 | private native int native_setUidRule(int childChain, int uid, int firewallRule); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 683 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 684 | private native int native_addUidInterfaceRules(String ifName, int[] uids); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 685 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 686 | private native int native_removeUidInterfaceRules(int[] uids); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 687 | @GuardedBy("sUidOwnerMap") |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 688 | private native int native_updateUidLockdownRule(int uid, boolean add); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 689 | private native int native_swapActiveStatsMap(); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 690 | private native void native_setPermissionForUids(int permissions, int[] uids); |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 691 | private native void native_dump(FileDescriptor fd, boolean verbose); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 692 | } |