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