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