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