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 | 55630d0 | 2022-06-29 07:46:52 +0000 | [diff] [blame] | 297 | throwIfPreT("addNiceApp is not available on pre-T devices"); |
| 298 | addRule(uid, HAPPY_BOX_MATCH, "addNiceApp"); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 299 | } |
| 300 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 301 | /** |
| 302 | * Remove nice app bandwidth rule for specific app |
| 303 | * |
| 304 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 305 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 306 | * cause of the failure. |
| 307 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 308 | public void removeNiceApp(final int uid) { |
Motomu Utsumi | 7392eb4 | 2022-06-29 03:53:03 +0000 | [diff] [blame] | 309 | throwIfPreT("removeNiceApp is not available on pre-T devices"); |
| 310 | removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp"); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 311 | } |
| 312 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 313 | /** |
| 314 | * Set target firewall child chain |
| 315 | * |
| 316 | * @param childChain target chain to enable |
| 317 | * @param enable whether to enable or disable child chain. |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 318 | * @throws UnsupportedOperationException if called on pre-T devices. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 319 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 320 | * cause of the failure. |
| 321 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 322 | public void setChildChain(final int childChain, final boolean enable) { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 323 | throwIfPreT("setChildChain is not available on pre-T devices"); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 324 | |
| 325 | final long match = getMatchByFirewallChain(childChain); |
| 326 | try { |
| 327 | synchronized (sUidRulesConfigBpfMapLock) { |
| 328 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 329 | final long newConfig = enable ? (config.val | match) : (config.val & ~match); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 330 | sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig)); |
| 331 | } |
| 332 | } catch (ErrnoException e) { |
| 333 | throw new ServiceSpecificException(e.errno, |
| 334 | "Unable to set child chain: " + Os.strerror(e.errno)); |
| 335 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 339 | * Get the specified firewall chain's status. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 340 | * |
| 341 | * @param childChain target chain |
| 342 | * @return {@code true} if chain is enabled, {@code false} if chain is not enabled. |
| 343 | * @throws UnsupportedOperationException if called on pre-T devices. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 344 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 345 | * cause of the failure. |
| 346 | */ |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 347 | public boolean isChainEnabled(final int childChain) { |
| 348 | throwIfPreT("isChainEnabled is not available on pre-T devices"); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 349 | |
| 350 | final long match = getMatchByFirewallChain(childChain); |
| 351 | try { |
| 352 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 353 | return (config.val & match) != 0; |
| 354 | } catch (ErrnoException e) { |
| 355 | throw new ServiceSpecificException(e.errno, |
| 356 | "Unable to get firewall chain status: " + Os.strerror(e.errno)); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 361 | * Replaces the contents of the specified UID-based firewall chain. |
| 362 | * |
| 363 | * The chain may be an allowlist chain or a denylist chain. A denylist chain contains DROP |
| 364 | * 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] | 365 | * 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] | 366 | * UIDs, and a DROP rule at the end. The chain will be created if it does not exist. |
| 367 | * |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 368 | * @param chainName The name of the chain to replace. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 369 | * @param isAllowlist Whether this is an allowlist or denylist chain. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 370 | * @param uids The list of UIDs to allow/deny. |
| 371 | * @return 0 if the chain was successfully replaced, errno otherwise. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 372 | */ |
| 373 | public int replaceUidChain(final String chainName, final boolean isAllowlist, |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 374 | final int[] uids) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 375 | synchronized (sUidOwnerMap) { |
| 376 | final int err = native_replaceUidChain(chainName, isAllowlist, uids); |
| 377 | if (err != 0) { |
| 378 | Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err)); |
| 379 | } |
| 380 | return -err; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 381 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 382 | } |
| 383 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 384 | /** |
| 385 | * Set firewall rule for uid |
| 386 | * |
| 387 | * @param childChain target chain |
| 388 | * @param uid uid to allow/deny |
| 389 | * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 390 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 391 | * cause of the failure. |
| 392 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 393 | public void setUidRule(final int childChain, final int uid, final int firewallRule) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 394 | synchronized (sUidOwnerMap) { |
| 395 | final int err = native_setUidRule(childChain, uid, firewallRule); |
| 396 | maybeThrow(err, "Unable to set uid rule"); |
| 397 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Add ingress interface filtering rules to a list of UIDs |
| 402 | * |
| 403 | * For a given uid, once a filtering rule is added, the kernel will only allow packets from the |
| 404 | * allowed interface and loopback to be sent to the list of UIDs. |
| 405 | * |
| 406 | * Calling this method on one or more UIDs with an existing filtering rule but a different |
| 407 | * interface name will result in the filtering rule being updated to allow the new interface |
| 408 | * instead. Otherwise calling this method will not affect existing rules set on other UIDs. |
| 409 | * |
| 410 | * @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] | 411 | * be received. |
| 412 | * @param uids an array of UIDs which the filtering rules will be set |
| 413 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 414 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 415 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 416 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 417 | public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 418 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 419 | mNetd.firewallAddUidInterfaceRules(ifName, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 420 | return; |
| 421 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 422 | synchronized (sUidOwnerMap) { |
| 423 | final int err = native_addUidInterfaceRules(ifName, uids); |
| 424 | maybeThrow(err, "Unable to add uid interface rules"); |
| 425 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Remove ingress interface filtering rules from a list of UIDs |
| 430 | * |
| 431 | * Clear the ingress interface filtering rules from the list of UIDs which were previously set |
| 432 | * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule. |
| 433 | * |
| 434 | * @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] | 435 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 436 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 437 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 438 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 439 | public void removeUidInterfaceRules(final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 440 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 441 | mNetd.firewallRemoveUidInterfaceRules(uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 442 | return; |
| 443 | } |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 444 | synchronized (sUidOwnerMap) { |
| 445 | final int err = native_removeUidInterfaceRules(uids); |
| 446 | maybeThrow(err, "Unable to remove uid interface rules"); |
| 447 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 448 | } |
| 449 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 450 | /** |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 451 | * Update lockdown rule for uid |
| 452 | * |
| 453 | * @param uid target uid to add/remove the rule |
| 454 | * @param add {@code true} to add the rule, {@code false} to remove the rule. |
| 455 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 456 | * cause of the failure. |
| 457 | */ |
| 458 | public void updateUidLockdownRule(final int uid, final boolean add) { |
Motomu Utsumi | 697b299 | 2022-06-30 02:25:29 +0000 | [diff] [blame^] | 459 | throwIfPreT("updateUidLockdownRule is not available on pre-T devices"); |
| 460 | if (add) { |
| 461 | addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule"); |
| 462 | } else { |
| 463 | removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule"); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 464 | } |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 468 | * Request netd to change the current active network stats map. |
| 469 | * |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 470 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 471 | * cause of the failure. |
| 472 | */ |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 473 | public void swapActiveStatsMap() { |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 474 | final int err = native_swapActiveStatsMap(); |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 475 | maybeThrow(err, "Unable to swap active stats map"); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 476 | } |
| 477 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 478 | /** |
| 479 | * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids |
| 480 | * specified. Or remove all permissions from the uids. |
| 481 | * |
| 482 | * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or |
| 483 | * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then |
| 484 | * revoke all permissions for the uids. |
| 485 | * @param uids uid of users to grant permission |
| 486 | * @throws RemoteException when netd has crashed. |
| 487 | */ |
| 488 | public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 489 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 490 | mNetd.trafficSetNetPermForUids(permissions, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | native_setPermissionForUids(permissions, uids); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 494 | } |
| 495 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 496 | /** |
| 497 | * Dump BPF maps |
| 498 | * |
| 499 | * @param fd file descriptor to output |
| 500 | * @throws IOException when file descriptor is invalid. |
| 501 | * @throws ServiceSpecificException when the method is called on an unsupported device. |
| 502 | */ |
| 503 | public void dump(final FileDescriptor fd, boolean verbose) |
| 504 | throws IOException, ServiceSpecificException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 505 | if (PRE_T) { |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 506 | throw new ServiceSpecificException( |
| 507 | EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T" |
| 508 | + " devices, use dumpsys netd trafficcontroller instead."); |
| 509 | } |
| 510 | native_dump(fd, verbose); |
| 511 | } |
| 512 | |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 513 | private static native void native_init(); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 514 | @GuardedBy("sUidOwnerMap") |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 515 | private native int native_addNaughtyApp(int uid); |
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_removeNaughtyApp(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_addNiceApp(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_removeNiceApp(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_replaceUidChain(String name, boolean isAllowlist, int[] uids); |
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_setUidRule(int childChain, int uid, int firewallRule); |
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_addUidInterfaceRules(String ifName, int[] uids); |
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_removeUidInterfaceRules(int[] uids); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 530 | @GuardedBy("sUidOwnerMap") |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 531 | private native int native_updateUidLockdownRule(int uid, boolean add); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 532 | private native int native_swapActiveStatsMap(); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 533 | private native void native_setPermissionForUids(int permissions, int[] uids); |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 534 | private native void native_dump(FileDescriptor fd, boolean verbose); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 535 | } |