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