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 | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 27 | import static android.system.OsConstants.EINVAL; |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 28 | import static android.system.OsConstants.EOPNOTSUPP; |
| 29 | |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 30 | import android.net.INetd; |
| 31 | import android.os.RemoteException; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 32 | import android.os.ServiceSpecificException; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 33 | import android.system.ErrnoException; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 34 | import android.system.Os; |
| 35 | import android.util.Log; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 36 | import android.util.SparseLongArray; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 37 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 38 | import com.android.internal.annotations.GuardedBy; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 40 | import com.android.modules.utils.build.SdkLevel; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 41 | import com.android.net.module.util.BpfMap; |
| 42 | import com.android.net.module.util.Struct.U32; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 43 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 44 | import java.io.FileDescriptor; |
| 45 | import java.io.IOException; |
| 46 | |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 47 | /** |
| 48 | * BpfNetMaps is responsible for providing traffic controller relevant functionality. |
| 49 | * |
| 50 | * {@hide} |
| 51 | */ |
| 52 | public class BpfNetMaps { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 53 | private static final boolean PRE_T = !SdkLevel.isAtLeastT(); |
| 54 | static { |
| 55 | if (!PRE_T) { |
| 56 | System.loadLibrary("service-connectivity"); |
| 57 | } |
| 58 | } |
| 59 | |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 60 | private static final String TAG = "BpfNetMaps"; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 61 | private final INetd mNetd; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 62 | // Use legacy netd for releases before T. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 63 | private static boolean sInitialized = false; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 64 | |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 65 | // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY. |
| 66 | // This entry is not accessed by others. |
| 67 | // BpfNetMaps acquires this lock while sequence of read, modify, and write. |
| 68 | private static final Object sUidRulesConfigBpfMapLock = new Object(); |
| 69 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 70 | private static final String CONFIGURATION_MAP_PATH = |
| 71 | "/sys/fs/bpf/netd_shared/map_netd_configuration_map"; |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 72 | private static final String UID_OWNER_MAP_PATH = |
| 73 | "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map"; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 74 | private static final U32 UID_RULES_CONFIGURATION_KEY = new U32(0); |
| 75 | private static BpfMap<U32, U32> sConfigurationMap = null; |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 76 | // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others. |
| 77 | private static BpfMap<U32, UidOwnerValue> sUidOwnerMap = null; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 78 | |
| 79 | // LINT.IfChange(match_type) |
| 80 | private static final long NO_MATCH = 0; |
| 81 | private static final long HAPPY_BOX_MATCH = (1 << 0); |
| 82 | private static final long PENALTY_BOX_MATCH = (1 << 1); |
| 83 | private static final long DOZABLE_MATCH = (1 << 2); |
| 84 | private static final long STANDBY_MATCH = (1 << 3); |
| 85 | private static final long POWERSAVE_MATCH = (1 << 4); |
| 86 | private static final long RESTRICTED_MATCH = (1 << 5); |
| 87 | private static final long LOW_POWER_STANDBY_MATCH = (1 << 6); |
| 88 | private static final long IIF_MATCH = (1 << 7); |
| 89 | private static final long LOCKDOWN_VPN_MATCH = (1 << 8); |
| 90 | private static final long OEM_DENY_1_MATCH = (1 << 9); |
| 91 | private static final long OEM_DENY_2_MATCH = (1 << 10); |
| 92 | private static final long OEM_DENY_3_MATCH = (1 << 11); |
| 93 | // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h) |
| 94 | |
| 95 | // TODO: Use Java BpfMap instead of JNI code (TrafficController) for map update. |
| 96 | // Currently, BpfNetMaps uses TrafficController for map update and TrafficController |
| 97 | // (changeUidOwnerRule and toggleUidOwnerMap) also does conversion from "firewall chain" to |
| 98 | // "match". Migrating map update from JNI to Java BpfMap will solve this duplication. |
| 99 | private static final SparseLongArray FIREWALL_CHAIN_TO_MATCH = new SparseLongArray(); |
| 100 | static { |
| 101 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_DOZABLE, DOZABLE_MATCH); |
| 102 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_STANDBY, STANDBY_MATCH); |
| 103 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_POWERSAVE, POWERSAVE_MATCH); |
| 104 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_RESTRICTED, RESTRICTED_MATCH); |
| 105 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_LOW_POWER_STANDBY, LOW_POWER_STANDBY_MATCH); |
| 106 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_1, OEM_DENY_1_MATCH); |
| 107 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_2, OEM_DENY_2_MATCH); |
| 108 | FIREWALL_CHAIN_TO_MATCH.put(FIREWALL_CHAIN_OEM_DENY_3, OEM_DENY_3_MATCH); |
| 109 | } |
| 110 | |
| 111 | /** |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 112 | * Set configurationMap for test. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 113 | */ |
| 114 | @VisibleForTesting |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 115 | public static void setConfigurationMapForTest(BpfMap<U32, U32> configurationMap) { |
| 116 | sConfigurationMap = configurationMap; |
| 117 | } |
| 118 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 119 | /** |
| 120 | * Set uidOwnerMap for test. |
| 121 | */ |
| 122 | @VisibleForTesting |
| 123 | public static void setUidOwnerMapForTest(BpfMap<U32, UidOwnerValue> uidOwnerMap) { |
| 124 | sUidOwnerMap = uidOwnerMap; |
| 125 | } |
| 126 | |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 127 | private static BpfMap<U32, U32> getConfigurationMap() { |
| 128 | try { |
| 129 | return new BpfMap<>( |
| 130 | CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, U32.class); |
| 131 | } catch (ErrnoException e) { |
| 132 | throw new IllegalStateException("Cannot open netd configuration map", e); |
| 133 | } |
| 134 | } |
| 135 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 136 | private static BpfMap<U32, UidOwnerValue> getUidOwnerMap() { |
| 137 | try { |
| 138 | return new BpfMap<>( |
| 139 | UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, U32.class, UidOwnerValue.class); |
| 140 | } catch (ErrnoException e) { |
| 141 | throw new IllegalStateException("Cannot open uid owner map", e); |
| 142 | } |
| 143 | } |
| 144 | |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 145 | private static void setBpfMaps() { |
| 146 | if (sConfigurationMap == null) { |
| 147 | sConfigurationMap = getConfigurationMap(); |
| 148 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 149 | if (sUidOwnerMap == null) { |
| 150 | sUidOwnerMap = getUidOwnerMap(); |
| 151 | } |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 154 | /** |
| 155 | * Initializes the class if it is not already initialized. This method will open maps but not |
| 156 | * cause any other effects. This method may be called multiple times on any thread. |
| 157 | */ |
| 158 | private static synchronized void ensureInitialized() { |
| 159 | if (sInitialized) return; |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 160 | setBpfMaps(); |
| 161 | native_init(); |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 162 | sInitialized = true; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 163 | } |
| 164 | |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 165 | /** Constructor used after T that doesn't need to use netd anymore. */ |
| 166 | public BpfNetMaps() { |
| 167 | this(null); |
| 168 | |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 169 | if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T"); |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 170 | } |
| 171 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 172 | public BpfNetMaps(final INetd netd) { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 173 | if (!PRE_T) { |
| 174 | ensureInitialized(); |
| 175 | } |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 176 | mNetd = netd; |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 179 | /** |
| 180 | * Get corresponding match from firewall chain. |
| 181 | */ |
| 182 | @VisibleForTesting |
| 183 | public long getMatchByFirewallChain(final int chain) { |
| 184 | final long match = FIREWALL_CHAIN_TO_MATCH.get(chain, NO_MATCH); |
| 185 | if (match == NO_MATCH) { |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 186 | throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 187 | } |
| 188 | return match; |
| 189 | } |
| 190 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 191 | private void maybeThrow(final int err, final String msg) { |
| 192 | if (err != 0) { |
| 193 | throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err)); |
| 194 | } |
| 195 | } |
| 196 | |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 197 | private void throwIfPreT(final String msg) { |
| 198 | if (PRE_T) { |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 199 | throw new UnsupportedOperationException(msg); |
| 200 | } |
| 201 | } |
| 202 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 203 | /** |
| 204 | * Add naughty app bandwidth rule for specific app |
| 205 | * |
| 206 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 207 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 208 | * cause of the failure. |
| 209 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 210 | public void addNaughtyApp(final int uid) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 211 | synchronized (sUidOwnerMap) { |
| 212 | final int err = native_addNaughtyApp(uid); |
| 213 | maybeThrow(err, "Unable to add naughty app"); |
| 214 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 215 | } |
| 216 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 217 | /** |
| 218 | * Remove naughty app bandwidth rule for specific app |
| 219 | * |
| 220 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 221 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 222 | * cause of the failure. |
| 223 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 224 | public void removeNaughtyApp(final int uid) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 225 | synchronized (sUidOwnerMap) { |
| 226 | final int err = native_removeNaughtyApp(uid); |
| 227 | maybeThrow(err, "Unable to remove naughty app"); |
| 228 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 231 | /** |
| 232 | * Add nice app bandwidth rule for specific app |
| 233 | * |
| 234 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 235 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 236 | * cause of the failure. |
| 237 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 238 | public void addNiceApp(final int uid) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 239 | synchronized (sUidOwnerMap) { |
| 240 | final int err = native_addNiceApp(uid); |
| 241 | maybeThrow(err, "Unable to add nice app"); |
| 242 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 243 | } |
| 244 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 245 | /** |
| 246 | * Remove nice app bandwidth rule for specific app |
| 247 | * |
| 248 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 249 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 250 | * cause of the failure. |
| 251 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 252 | public void removeNiceApp(final int uid) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 253 | synchronized (sUidOwnerMap) { |
| 254 | final int err = native_removeNiceApp(uid); |
| 255 | maybeThrow(err, "Unable to remove nice app"); |
| 256 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 257 | } |
| 258 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 259 | /** |
| 260 | * Set target firewall child chain |
| 261 | * |
| 262 | * @param childChain target chain to enable |
| 263 | * @param enable whether to enable or disable child chain. |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 264 | * @throws UnsupportedOperationException if called on pre-T devices. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 265 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 266 | * cause of the failure. |
| 267 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 268 | public void setChildChain(final int childChain, final boolean enable) { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 269 | throwIfPreT("setChildChain is not available on pre-T devices"); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 270 | |
| 271 | final long match = getMatchByFirewallChain(childChain); |
| 272 | try { |
| 273 | synchronized (sUidRulesConfigBpfMapLock) { |
| 274 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 275 | final long newConfig = enable ? (config.val | match) : (config.val & ~match); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 276 | sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig)); |
| 277 | } |
| 278 | } catch (ErrnoException e) { |
| 279 | throw new ServiceSpecificException(e.errno, |
| 280 | "Unable to set child chain: " + Os.strerror(e.errno)); |
| 281 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 285 | * Get the specified firewall chain's status. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 286 | * |
| 287 | * @param childChain target chain |
| 288 | * @return {@code true} if chain is enabled, {@code false} if chain is not enabled. |
| 289 | * @throws UnsupportedOperationException if called on pre-T devices. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 290 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 291 | * cause of the failure. |
| 292 | */ |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 293 | public boolean isChainEnabled(final int childChain) { |
| 294 | throwIfPreT("isChainEnabled is not available on pre-T devices"); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 295 | |
| 296 | final long match = getMatchByFirewallChain(childChain); |
| 297 | try { |
| 298 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 299 | return (config.val & match) != 0; |
| 300 | } catch (ErrnoException e) { |
| 301 | throw new ServiceSpecificException(e.errno, |
| 302 | "Unable to get firewall chain status: " + Os.strerror(e.errno)); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 307 | * Replaces the contents of the specified UID-based firewall chain. |
| 308 | * |
| 309 | * The chain may be an allowlist chain or a denylist chain. A denylist chain contains DROP |
| 310 | * rules for the specified UIDs and a RETURN rule at the end. An allowlist chain contains RETURN |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 311 | * rules for the system UID range (0 to {@code UID_APP} - 1), RETURN rules for the specified |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 312 | * UIDs, and a DROP rule at the end. The chain will be created if it does not exist. |
| 313 | * |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 314 | * @param chainName The name of the chain to replace. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 315 | * @param isAllowlist Whether this is an allowlist or denylist chain. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 316 | * @param uids The list of UIDs to allow/deny. |
| 317 | * @return 0 if the chain was successfully replaced, errno otherwise. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 318 | */ |
| 319 | public int replaceUidChain(final String chainName, final boolean isAllowlist, |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 320 | final int[] uids) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 321 | synchronized (sUidOwnerMap) { |
| 322 | final int err = native_replaceUidChain(chainName, isAllowlist, uids); |
| 323 | if (err != 0) { |
| 324 | Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err)); |
| 325 | } |
| 326 | return -err; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 327 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 328 | } |
| 329 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 330 | /** |
| 331 | * Set firewall rule for uid |
| 332 | * |
| 333 | * @param childChain target chain |
| 334 | * @param uid uid to allow/deny |
| 335 | * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 336 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 337 | * cause of the failure. |
| 338 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 339 | public void setUidRule(final int childChain, final int uid, final int firewallRule) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 340 | synchronized (sUidOwnerMap) { |
| 341 | final int err = native_setUidRule(childChain, uid, firewallRule); |
| 342 | maybeThrow(err, "Unable to set uid rule"); |
| 343 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Add ingress interface filtering rules to a list of UIDs |
| 348 | * |
| 349 | * For a given uid, once a filtering rule is added, the kernel will only allow packets from the |
| 350 | * allowed interface and loopback to be sent to the list of UIDs. |
| 351 | * |
| 352 | * Calling this method on one or more UIDs with an existing filtering rule but a different |
| 353 | * interface name will result in the filtering rule being updated to allow the new interface |
| 354 | * instead. Otherwise calling this method will not affect existing rules set on other UIDs. |
| 355 | * |
| 356 | * @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] | 357 | * be received. |
| 358 | * @param uids an array of UIDs which the filtering rules will be set |
| 359 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 360 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 361 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 362 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 363 | public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 364 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 365 | mNetd.firewallAddUidInterfaceRules(ifName, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 366 | return; |
| 367 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 368 | synchronized (sUidOwnerMap) { |
| 369 | final int err = native_addUidInterfaceRules(ifName, uids); |
| 370 | maybeThrow(err, "Unable to add uid interface rules"); |
| 371 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Remove ingress interface filtering rules from a list of UIDs |
| 376 | * |
| 377 | * Clear the ingress interface filtering rules from the list of UIDs which were previously set |
| 378 | * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule. |
| 379 | * |
| 380 | * @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] | 381 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 382 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 383 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 384 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 385 | public void removeUidInterfaceRules(final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 386 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 387 | mNetd.firewallRemoveUidInterfaceRules(uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 388 | return; |
| 389 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 390 | synchronized (sUidOwnerMap) { |
| 391 | final int err = native_removeUidInterfaceRules(uids); |
| 392 | maybeThrow(err, "Unable to remove uid interface rules"); |
| 393 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 394 | } |
| 395 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 396 | /** |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 397 | * Update lockdown rule for uid |
| 398 | * |
| 399 | * @param uid target uid to add/remove the rule |
| 400 | * @param add {@code true} to add the rule, {@code false} to remove the rule. |
| 401 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 402 | * cause of the failure. |
| 403 | */ |
| 404 | public void updateUidLockdownRule(final int uid, final boolean add) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 405 | synchronized (sUidOwnerMap) { |
| 406 | final int err = native_updateUidLockdownRule(uid, add); |
| 407 | maybeThrow(err, "Unable to update lockdown rule"); |
| 408 | } |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 412 | * Request netd to change the current active network stats map. |
| 413 | * |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 414 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 415 | * cause of the failure. |
| 416 | */ |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 417 | public void swapActiveStatsMap() { |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 418 | final int err = native_swapActiveStatsMap(); |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 419 | maybeThrow(err, "Unable to swap active stats map"); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 420 | } |
| 421 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 422 | /** |
| 423 | * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids |
| 424 | * specified. Or remove all permissions from the uids. |
| 425 | * |
| 426 | * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or |
| 427 | * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then |
| 428 | * revoke all permissions for the uids. |
| 429 | * @param uids uid of users to grant permission |
| 430 | * @throws RemoteException when netd has crashed. |
| 431 | */ |
| 432 | public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 433 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 434 | mNetd.trafficSetNetPermForUids(permissions, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | native_setPermissionForUids(permissions, uids); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 440 | /** |
| 441 | * Dump BPF maps |
| 442 | * |
| 443 | * @param fd file descriptor to output |
| 444 | * @throws IOException when file descriptor is invalid. |
| 445 | * @throws ServiceSpecificException when the method is called on an unsupported device. |
| 446 | */ |
| 447 | public void dump(final FileDescriptor fd, boolean verbose) |
| 448 | throws IOException, ServiceSpecificException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 449 | if (PRE_T) { |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 450 | throw new ServiceSpecificException( |
| 451 | EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T" |
| 452 | + " devices, use dumpsys netd trafficcontroller instead."); |
| 453 | } |
| 454 | native_dump(fd, verbose); |
| 455 | } |
| 456 | |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 457 | private static native void native_init(); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 458 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 459 | private native int native_addNaughtyApp(int uid); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 460 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 461 | private native int native_removeNaughtyApp(int uid); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 462 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 463 | private native int native_addNiceApp(int uid); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 464 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 465 | private native int native_removeNiceApp(int uid); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 466 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 467 | private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 468 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 469 | private native int native_setUidRule(int childChain, int uid, int firewallRule); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 470 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 471 | private native int native_addUidInterfaceRules(String ifName, int[] uids); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 472 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 473 | private native int native_removeUidInterfaceRules(int[] uids); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame^] | 474 | @GuardedBy("sUidOwnerMap") |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 475 | private native int native_updateUidLockdownRule(int uid, boolean add); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 476 | private native int native_swapActiveStatsMap(); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 477 | private native void native_setPermissionForUids(int permissions, int[] uids); |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 478 | private native void native_dump(FileDescriptor fd, boolean verbose); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 479 | } |