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