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