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 | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.FIREWALL_RULE_ALLOW; |
| 28 | import static android.net.ConnectivityManager.FIREWALL_RULE_DENY; |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 29 | import static android.net.INetd.PERMISSION_INTERNET; |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 30 | import static android.net.INetd.PERMISSION_NONE; |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 31 | import static android.net.INetd.PERMISSION_UNINSTALLED; |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 32 | import static android.net.INetd.PERMISSION_UPDATE_DEVICE_STATS; |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 33 | import static android.system.OsConstants.EINVAL; |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 34 | import static android.system.OsConstants.ENODEV; |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 35 | import static android.system.OsConstants.ENOENT; |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 36 | import static android.system.OsConstants.EOPNOTSUPP; |
| 37 | |
Motomu Utsumi | 166f966 | 2022-09-01 10:35:29 +0900 | [diff] [blame] | 38 | import static com.android.server.ConnectivityStatsLog.NETWORK_BPF_MAP_INFO; |
| 39 | |
| 40 | import android.app.StatsManager; |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 41 | import android.content.Context; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 42 | import android.net.INetd; |
| 43 | import android.os.RemoteException; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 44 | import android.os.ServiceSpecificException; |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 45 | import android.provider.DeviceConfig; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 46 | import android.system.ErrnoException; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 47 | import android.system.Os; |
Motomu Utsumi | 1a477b0 | 2022-08-23 15:14:56 +0900 | [diff] [blame] | 48 | import android.util.ArraySet; |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 49 | import android.util.IndentingPrintWriter; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 50 | import android.util.Log; |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 51 | import android.util.Pair; |
Motomu Utsumi | 166f966 | 2022-09-01 10:35:29 +0900 | [diff] [blame] | 52 | import android.util.StatsEvent; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 53 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 54 | import com.android.internal.annotations.VisibleForTesting; |
Motomu Utsumi | 166f966 | 2022-09-01 10:35:29 +0900 | [diff] [blame] | 55 | import com.android.modules.utils.BackgroundThread; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 56 | import com.android.modules.utils.build.SdkLevel; |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 57 | import com.android.net.module.util.BpfDump; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 58 | import com.android.net.module.util.BpfMap; |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 59 | import com.android.net.module.util.DeviceConfigUtils; |
Motomu Utsumi | 73599a5 | 2022-08-24 11:59:21 +0900 | [diff] [blame] | 60 | import com.android.net.module.util.IBpfMap; |
Motomu Utsumi | 166f966 | 2022-09-01 10:35:29 +0900 | [diff] [blame] | 61 | import com.android.net.module.util.Struct; |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 62 | import com.android.net.module.util.Struct.S32; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 63 | import com.android.net.module.util.Struct.U32; |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 64 | import com.android.net.module.util.Struct.U8; |
Motomu Utsumi | b954886 | 2022-09-06 16:30:05 +0900 | [diff] [blame] | 65 | import com.android.net.module.util.bpf.CookieTagMapKey; |
| 66 | import com.android.net.module.util.bpf.CookieTagMapValue; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 67 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 68 | import java.io.FileDescriptor; |
| 69 | import java.io.IOException; |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 70 | import java.util.Arrays; |
Motomu Utsumi | 166f966 | 2022-09-01 10:35:29 +0900 | [diff] [blame] | 71 | import java.util.List; |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 72 | import java.util.Set; |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 73 | import java.util.StringJoiner; |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 74 | |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 75 | /** |
| 76 | * BpfNetMaps is responsible for providing traffic controller relevant functionality. |
| 77 | * |
| 78 | * {@hide} |
| 79 | */ |
| 80 | public class BpfNetMaps { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 81 | private static final boolean PRE_T = !SdkLevel.isAtLeastT(); |
| 82 | static { |
| 83 | if (!PRE_T) { |
| 84 | System.loadLibrary("service-connectivity"); |
| 85 | } |
| 86 | } |
| 87 | |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 88 | private static final String TAG = "BpfNetMaps"; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 89 | private final INetd mNetd; |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 90 | private final Dependencies mDeps; |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 91 | // Use legacy netd for releases before T. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 92 | private static boolean sInitialized = false; |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 93 | |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 94 | private static Boolean sEnableJavaBpfMap = null; |
| 95 | private static final String BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP = |
| 96 | "bpf_net_maps_enable_java_bpf_map"; |
| 97 | |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 98 | // Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY. |
| 99 | // This entry is not accessed by others. |
| 100 | // BpfNetMaps acquires this lock while sequence of read, modify, and write. |
| 101 | private static final Object sUidRulesConfigBpfMapLock = new Object(); |
| 102 | |
Motomu Utsumi | 7abeaa4 | 2022-07-20 07:54:18 +0000 | [diff] [blame] | 103 | // Lock for sConfigurationMap entry for CURRENT_STATS_MAP_CONFIGURATION_KEY. |
| 104 | // BpfNetMaps acquires this lock while sequence of read, modify, and write. |
| 105 | // BpfNetMaps is an only writer of this entry. |
| 106 | private static final Object sCurrentStatsMapConfigLock = new Object(); |
| 107 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 108 | private static final String CONFIGURATION_MAP_PATH = |
| 109 | "/sys/fs/bpf/netd_shared/map_netd_configuration_map"; |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 110 | private static final String UID_OWNER_MAP_PATH = |
| 111 | "/sys/fs/bpf/netd_shared/map_netd_uid_owner_map"; |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 112 | private static final String UID_PERMISSION_MAP_PATH = |
| 113 | "/sys/fs/bpf/netd_shared/map_netd_uid_permission_map"; |
Motomu Utsumi | b954886 | 2022-09-06 16:30:05 +0900 | [diff] [blame] | 114 | private static final String COOKIE_TAG_MAP_PATH = |
| 115 | "/sys/fs/bpf/netd_shared/map_netd_cookie_tag_map"; |
Maciej Żenczykowski | dc88622 | 2022-09-17 06:09:22 +0000 | [diff] [blame] | 116 | private static final S32 UID_RULES_CONFIGURATION_KEY = new S32(0); |
| 117 | private static final S32 CURRENT_STATS_MAP_CONFIGURATION_KEY = new S32(1); |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 118 | private static final long UID_RULES_DEFAULT_CONFIGURATION = 0; |
| 119 | private static final long STATS_SELECT_MAP_A = 0; |
| 120 | private static final long STATS_SELECT_MAP_B = 1; |
| 121 | |
Maciej Żenczykowski | dc88622 | 2022-09-17 06:09:22 +0000 | [diff] [blame] | 122 | private static IBpfMap<S32, U32> sConfigurationMap = null; |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 123 | // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others. |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 124 | private static IBpfMap<S32, UidOwnerValue> sUidOwnerMap = null; |
| 125 | private static IBpfMap<S32, U8> sUidPermissionMap = null; |
Motomu Utsumi | b954886 | 2022-09-06 16:30:05 +0900 | [diff] [blame] | 126 | private static IBpfMap<CookieTagMapKey, CookieTagMapValue> sCookieTagMap = null; |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 127 | |
| 128 | // LINT.IfChange(match_type) |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 129 | @VisibleForTesting public static final long NO_MATCH = 0; |
| 130 | @VisibleForTesting public static final long HAPPY_BOX_MATCH = (1 << 0); |
| 131 | @VisibleForTesting public static final long PENALTY_BOX_MATCH = (1 << 1); |
| 132 | @VisibleForTesting public static final long DOZABLE_MATCH = (1 << 2); |
| 133 | @VisibleForTesting public static final long STANDBY_MATCH = (1 << 3); |
| 134 | @VisibleForTesting public static final long POWERSAVE_MATCH = (1 << 4); |
| 135 | @VisibleForTesting public static final long RESTRICTED_MATCH = (1 << 5); |
| 136 | @VisibleForTesting public static final long LOW_POWER_STANDBY_MATCH = (1 << 6); |
| 137 | @VisibleForTesting public static final long IIF_MATCH = (1 << 7); |
| 138 | @VisibleForTesting public static final long LOCKDOWN_VPN_MATCH = (1 << 8); |
| 139 | @VisibleForTesting public static final long OEM_DENY_1_MATCH = (1 << 9); |
| 140 | @VisibleForTesting public static final long OEM_DENY_2_MATCH = (1 << 10); |
| 141 | @VisibleForTesting public static final long OEM_DENY_3_MATCH = (1 << 11); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 142 | // LINT.ThenChange(packages/modules/Connectivity/bpf_progs/bpf_shared.h) |
| 143 | |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 144 | private static final List<Pair<Integer, String>> PERMISSION_LIST = Arrays.asList( |
| 145 | Pair.create(PERMISSION_INTERNET, "PERMISSION_INTERNET"), |
| 146 | Pair.create(PERMISSION_UPDATE_DEVICE_STATS, "PERMISSION_UPDATE_DEVICE_STATS") |
| 147 | ); |
| 148 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 149 | /** |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 150 | * Set sEnableJavaBpfMap for test. |
| 151 | */ |
| 152 | @VisibleForTesting |
| 153 | public static void setEnableJavaBpfMapForTest(boolean enable) { |
| 154 | sEnableJavaBpfMap = enable; |
| 155 | } |
| 156 | |
| 157 | /** |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 158 | * Set configurationMap for test. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 159 | */ |
| 160 | @VisibleForTesting |
Maciej Żenczykowski | dc88622 | 2022-09-17 06:09:22 +0000 | [diff] [blame] | 161 | public static void setConfigurationMapForTest(IBpfMap<S32, U32> configurationMap) { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 162 | sConfigurationMap = configurationMap; |
| 163 | } |
| 164 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 165 | /** |
| 166 | * Set uidOwnerMap for test. |
| 167 | */ |
| 168 | @VisibleForTesting |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 169 | public static void setUidOwnerMapForTest(IBpfMap<S32, UidOwnerValue> uidOwnerMap) { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 170 | sUidOwnerMap = uidOwnerMap; |
| 171 | } |
| 172 | |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 173 | /** |
| 174 | * Set uidPermissionMap for test. |
| 175 | */ |
| 176 | @VisibleForTesting |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 177 | public static void setUidPermissionMapForTest(IBpfMap<S32, U8> uidPermissionMap) { |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 178 | sUidPermissionMap = uidPermissionMap; |
| 179 | } |
| 180 | |
Motomu Utsumi | b954886 | 2022-09-06 16:30:05 +0900 | [diff] [blame] | 181 | /** |
| 182 | * Set cookieTagMap for test. |
| 183 | */ |
| 184 | @VisibleForTesting |
| 185 | public static void setCookieTagMapForTest( |
| 186 | IBpfMap<CookieTagMapKey, CookieTagMapValue> cookieTagMap) { |
| 187 | sCookieTagMap = cookieTagMap; |
| 188 | } |
| 189 | |
Maciej Żenczykowski | dc88622 | 2022-09-17 06:09:22 +0000 | [diff] [blame] | 190 | private static IBpfMap<S32, U32> getConfigurationMap() { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 191 | try { |
| 192 | return new BpfMap<>( |
Maciej Żenczykowski | dc88622 | 2022-09-17 06:09:22 +0000 | [diff] [blame] | 193 | CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U32.class); |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 194 | } catch (ErrnoException e) { |
| 195 | throw new IllegalStateException("Cannot open netd configuration map", e); |
| 196 | } |
| 197 | } |
| 198 | |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 199 | private static IBpfMap<S32, UidOwnerValue> getUidOwnerMap() { |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 200 | try { |
| 201 | return new BpfMap<>( |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 202 | UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, UidOwnerValue.class); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 203 | } catch (ErrnoException e) { |
| 204 | throw new IllegalStateException("Cannot open uid owner map", e); |
| 205 | } |
| 206 | } |
| 207 | |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 208 | private static IBpfMap<S32, U8> getUidPermissionMap() { |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 209 | try { |
| 210 | return new BpfMap<>( |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 211 | UID_PERMISSION_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U8.class); |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 212 | } catch (ErrnoException e) { |
| 213 | throw new IllegalStateException("Cannot open uid permission map", e); |
| 214 | } |
| 215 | } |
| 216 | |
Motomu Utsumi | b954886 | 2022-09-06 16:30:05 +0900 | [diff] [blame] | 217 | private static IBpfMap<CookieTagMapKey, CookieTagMapValue> getCookieTagMap() { |
| 218 | try { |
| 219 | return new BpfMap<>(COOKIE_TAG_MAP_PATH, BpfMap.BPF_F_RDWR, |
| 220 | CookieTagMapKey.class, CookieTagMapValue.class); |
| 221 | } catch (ErrnoException e) { |
| 222 | throw new IllegalStateException("Cannot open cookie tag map", e); |
| 223 | } |
| 224 | } |
| 225 | |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 226 | private static void initBpfMaps() { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 227 | if (sConfigurationMap == null) { |
| 228 | sConfigurationMap = getConfigurationMap(); |
| 229 | } |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 230 | try { |
| 231 | sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, |
| 232 | new U32(UID_RULES_DEFAULT_CONFIGURATION)); |
| 233 | } catch (ErrnoException e) { |
| 234 | throw new IllegalStateException("Failed to initialize uid rules configuration", e); |
| 235 | } |
| 236 | try { |
| 237 | sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY, |
| 238 | new U32(STATS_SELECT_MAP_A)); |
| 239 | } catch (ErrnoException e) { |
| 240 | throw new IllegalStateException("Failed to initialize current stats configuration", e); |
| 241 | } |
| 242 | |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 243 | if (sUidOwnerMap == null) { |
| 244 | sUidOwnerMap = getUidOwnerMap(); |
| 245 | } |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 246 | try { |
| 247 | sUidOwnerMap.clear(); |
| 248 | } catch (ErrnoException e) { |
| 249 | throw new IllegalStateException("Failed to initialize uid owner map", e); |
| 250 | } |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 251 | |
| 252 | if (sUidPermissionMap == null) { |
| 253 | sUidPermissionMap = getUidPermissionMap(); |
| 254 | } |
Motomu Utsumi | b954886 | 2022-09-06 16:30:05 +0900 | [diff] [blame] | 255 | |
| 256 | if (sCookieTagMap == null) { |
| 257 | sCookieTagMap = getCookieTagMap(); |
| 258 | } |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 261 | /** |
| 262 | * Initializes the class if it is not already initialized. This method will open maps but not |
| 263 | * cause any other effects. This method may be called multiple times on any thread. |
| 264 | */ |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 265 | private static synchronized void ensureInitialized(final Context context) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 266 | if (sInitialized) return; |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 267 | if (sEnableJavaBpfMap == null) { |
| 268 | sEnableJavaBpfMap = DeviceConfigUtils.isFeatureEnabled(context, |
| 269 | DeviceConfig.NAMESPACE_TETHERING, BPF_NET_MAPS_ENABLE_JAVA_BPF_MAP, |
| 270 | SdkLevel.isAtLeastU() /* defaultValue */); |
| 271 | } |
| 272 | Log.d(TAG, "BpfNetMaps is initialized with sEnableJavaBpfMap=" + sEnableJavaBpfMap); |
| 273 | |
Motomu Utsumi | ba2fa15 | 2022-07-25 01:57:23 +0000 | [diff] [blame] | 274 | initBpfMaps(); |
Motomu Utsumi | d95a0da | 2022-09-03 00:40:30 +0900 | [diff] [blame] | 275 | native_init(!sEnableJavaBpfMap /* startSkDestroyListener */); |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 276 | sInitialized = true; |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 277 | } |
| 278 | |
Motomu Utsumi | d95a0da | 2022-09-03 00:40:30 +0900 | [diff] [blame] | 279 | public boolean isSkDestroyListenerRunning() { |
| 280 | return !sEnableJavaBpfMap; |
| 281 | } |
| 282 | |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 283 | /** |
| 284 | * Dependencies of BpfNetMaps, for injection in tests. |
| 285 | */ |
| 286 | @VisibleForTesting |
| 287 | public static class Dependencies { |
| 288 | /** |
| 289 | * Get interface index. |
| 290 | */ |
| 291 | public int getIfIndex(final String ifName) { |
| 292 | return Os.if_nametoindex(ifName); |
| 293 | } |
Motomu Utsumi | 7abeaa4 | 2022-07-20 07:54:18 +0000 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Call synchronize_rcu() |
| 297 | */ |
| 298 | public int synchronizeKernelRCU() { |
| 299 | return native_synchronizeKernelRCU(); |
| 300 | } |
Motomu Utsumi | 166f966 | 2022-09-01 10:35:29 +0900 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * Build Stats Event for NETWORK_BPF_MAP_INFO atom |
| 304 | */ |
| 305 | public StatsEvent buildStatsEvent(final int cookieTagMapSize, final int uidOwnerMapSize, |
| 306 | final int uidPermissionMapSize) { |
| 307 | return ConnectivityStatsLog.buildStatsEvent(NETWORK_BPF_MAP_INFO, cookieTagMapSize, |
| 308 | uidOwnerMapSize, uidPermissionMapSize); |
| 309 | } |
Motomu Utsumi | df8d299 | 2022-09-01 17:08:27 +0900 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * Call native_dump |
| 313 | */ |
| 314 | public void nativeDump(final FileDescriptor fd, final boolean verbose) { |
| 315 | native_dump(fd, verbose); |
| 316 | } |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 317 | } |
| 318 | |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 319 | /** Constructor used after T that doesn't need to use netd anymore. */ |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 320 | public BpfNetMaps(final Context context) { |
| 321 | this(context, null); |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 322 | |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 323 | if (PRE_T) throw new IllegalArgumentException("BpfNetMaps need to use netd before T"); |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 324 | } |
| 325 | |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 326 | public BpfNetMaps(final Context context, final INetd netd) { |
| 327 | this(context, netd, new Dependencies()); |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @VisibleForTesting |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 331 | public BpfNetMaps(final Context context, final INetd netd, final Dependencies deps) { |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 332 | if (!PRE_T) { |
Motomu Utsumi | f688eeb | 2022-07-22 03:47:35 +0000 | [diff] [blame] | 333 | ensureInitialized(context); |
Motomu Utsumi | 305975f | 2022-06-27 09:24:32 +0000 | [diff] [blame] | 334 | } |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 335 | mNetd = netd; |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 336 | mDeps = deps; |
Wayne Ma | 790c83e | 2022-01-13 10:35:05 +0800 | [diff] [blame] | 337 | } |
| 338 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 339 | /** |
| 340 | * Get corresponding match from firewall chain. |
| 341 | */ |
| 342 | @VisibleForTesting |
| 343 | public long getMatchByFirewallChain(final int chain) { |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 344 | switch (chain) { |
| 345 | case FIREWALL_CHAIN_DOZABLE: |
| 346 | return DOZABLE_MATCH; |
| 347 | case FIREWALL_CHAIN_STANDBY: |
| 348 | return STANDBY_MATCH; |
| 349 | case FIREWALL_CHAIN_POWERSAVE: |
| 350 | return POWERSAVE_MATCH; |
| 351 | case FIREWALL_CHAIN_RESTRICTED: |
| 352 | return RESTRICTED_MATCH; |
| 353 | case FIREWALL_CHAIN_LOW_POWER_STANDBY: |
| 354 | return LOW_POWER_STANDBY_MATCH; |
| 355 | case FIREWALL_CHAIN_OEM_DENY_1: |
| 356 | return OEM_DENY_1_MATCH; |
| 357 | case FIREWALL_CHAIN_OEM_DENY_2: |
| 358 | return OEM_DENY_2_MATCH; |
| 359 | case FIREWALL_CHAIN_OEM_DENY_3: |
| 360 | return OEM_DENY_3_MATCH; |
| 361 | default: |
| 362 | throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 363 | } |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Get if the chain is allow list or not. |
| 368 | * |
| 369 | * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed |
| 370 | * DENYLIST means the firewall allows all by default, uids must be explicitly denyed |
| 371 | */ |
| 372 | @VisibleForTesting |
| 373 | public boolean isFirewallAllowList(final int chain) { |
| 374 | switch (chain) { |
| 375 | case FIREWALL_CHAIN_DOZABLE: |
| 376 | case FIREWALL_CHAIN_POWERSAVE: |
| 377 | case FIREWALL_CHAIN_RESTRICTED: |
| 378 | case FIREWALL_CHAIN_LOW_POWER_STANDBY: |
| 379 | return true; |
| 380 | case FIREWALL_CHAIN_STANDBY: |
| 381 | case FIREWALL_CHAIN_OEM_DENY_1: |
| 382 | case FIREWALL_CHAIN_OEM_DENY_2: |
| 383 | case FIREWALL_CHAIN_OEM_DENY_3: |
| 384 | return false; |
| 385 | default: |
| 386 | throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain); |
| 387 | } |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 390 | private void maybeThrow(final int err, final String msg) { |
| 391 | if (err != 0) { |
| 392 | throw new ServiceSpecificException(err, msg + ": " + Os.strerror(err)); |
| 393 | } |
| 394 | } |
| 395 | |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 396 | private void throwIfPreT(final String msg) { |
| 397 | if (PRE_T) { |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 398 | throw new UnsupportedOperationException(msg); |
| 399 | } |
| 400 | } |
| 401 | |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 402 | private void removeRule(final int uid, final long match, final String caller) { |
| 403 | try { |
| 404 | synchronized (sUidOwnerMap) { |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 405 | final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid)); |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 406 | |
| 407 | if (oldMatch == null) { |
| 408 | throw new ServiceSpecificException(ENOENT, |
| 409 | "sUidOwnerMap does not have entry for uid: " + uid); |
| 410 | } |
| 411 | |
| 412 | final UidOwnerValue newMatch = new UidOwnerValue( |
| 413 | (match == IIF_MATCH) ? 0 : oldMatch.iif, |
| 414 | oldMatch.rule & ~match |
| 415 | ); |
| 416 | |
| 417 | if (newMatch.rule == 0) { |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 418 | sUidOwnerMap.deleteEntry(new S32(uid)); |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 419 | } else { |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 420 | sUidOwnerMap.updateEntry(new S32(uid), newMatch); |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } catch (ErrnoException e) { |
| 424 | throw new ServiceSpecificException(e.errno, |
| 425 | caller + " failed to remove rule: " + Os.strerror(e.errno)); |
| 426 | } |
| 427 | } |
| 428 | |
Maciej Żenczykowski | 6c1c6bb | 2022-09-16 06:55:33 +0000 | [diff] [blame] | 429 | private void addRule(final int uid, final long match, final int iif, final String caller) { |
Motomu Utsumi | 389278e | 2022-06-28 07:05:05 +0000 | [diff] [blame] | 430 | if (match != IIF_MATCH && iif != 0) { |
| 431 | throw new ServiceSpecificException(EINVAL, |
| 432 | "Non-interface match must have zero interface index"); |
| 433 | } |
| 434 | |
| 435 | try { |
| 436 | synchronized (sUidOwnerMap) { |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 437 | final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid)); |
Motomu Utsumi | 389278e | 2022-06-28 07:05:05 +0000 | [diff] [blame] | 438 | |
| 439 | final UidOwnerValue newMatch; |
| 440 | if (oldMatch != null) { |
| 441 | newMatch = new UidOwnerValue( |
| 442 | (match == IIF_MATCH) ? iif : oldMatch.iif, |
| 443 | oldMatch.rule | match |
| 444 | ); |
| 445 | } else { |
| 446 | newMatch = new UidOwnerValue( |
| 447 | iif, |
| 448 | match |
| 449 | ); |
| 450 | } |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 451 | sUidOwnerMap.updateEntry(new S32(uid), newMatch); |
Motomu Utsumi | 389278e | 2022-06-28 07:05:05 +0000 | [diff] [blame] | 452 | } |
| 453 | } catch (ErrnoException e) { |
| 454 | throw new ServiceSpecificException(e.errno, |
| 455 | caller + " failed to add rule: " + Os.strerror(e.errno)); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | private void addRule(final int uid, final long match, final String caller) { |
| 460 | addRule(uid, match, 0 /* iif */, caller); |
| 461 | } |
| 462 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 463 | /** |
| 464 | * Add naughty app bandwidth rule for specific app |
| 465 | * |
| 466 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 467 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 468 | * cause of the failure. |
| 469 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 470 | public void addNaughtyApp(final int uid) { |
Motomu Utsumi | 389278e | 2022-06-28 07:05:05 +0000 | [diff] [blame] | 471 | throwIfPreT("addNaughtyApp is not available on pre-T devices"); |
Motomu Utsumi | 55c282e | 2022-08-03 06:25:33 +0000 | [diff] [blame] | 472 | |
| 473 | if (sEnableJavaBpfMap) { |
| 474 | addRule(uid, PENALTY_BOX_MATCH, "addNaughtyApp"); |
| 475 | } else { |
| 476 | final int err = native_addNaughtyApp(uid); |
| 477 | maybeThrow(err, "Unable to add naughty app"); |
| 478 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 479 | } |
| 480 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 481 | /** |
| 482 | * Remove naughty app bandwidth rule for specific app |
| 483 | * |
| 484 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 485 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 486 | * cause of the failure. |
| 487 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 488 | public void removeNaughtyApp(final int uid) { |
Motomu Utsumi | 60ed3be | 2022-06-24 10:38:57 +0000 | [diff] [blame] | 489 | throwIfPreT("removeNaughtyApp is not available on pre-T devices"); |
Motomu Utsumi | 878ce0d | 2022-08-03 06:22:42 +0000 | [diff] [blame] | 490 | |
| 491 | if (sEnableJavaBpfMap) { |
| 492 | removeRule(uid, PENALTY_BOX_MATCH, "removeNaughtyApp"); |
| 493 | } else { |
| 494 | final int err = native_removeNaughtyApp(uid); |
| 495 | maybeThrow(err, "Unable to remove naughty app"); |
| 496 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 497 | } |
| 498 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 499 | /** |
| 500 | * Add nice app bandwidth rule for specific app |
| 501 | * |
| 502 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 503 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 504 | * cause of the failure. |
| 505 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 506 | public void addNiceApp(final int uid) { |
Motomu Utsumi | 55630d0 | 2022-06-29 07:46:52 +0000 | [diff] [blame] | 507 | throwIfPreT("addNiceApp is not available on pre-T devices"); |
Motomu Utsumi | 7f19df9 | 2022-08-03 06:29:59 +0000 | [diff] [blame] | 508 | |
| 509 | if (sEnableJavaBpfMap) { |
| 510 | addRule(uid, HAPPY_BOX_MATCH, "addNiceApp"); |
| 511 | } else { |
| 512 | final int err = native_addNiceApp(uid); |
| 513 | maybeThrow(err, "Unable to add nice app"); |
| 514 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 515 | } |
| 516 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 517 | /** |
| 518 | * Remove nice app bandwidth rule for specific app |
| 519 | * |
| 520 | * @param uid uid of target app |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 521 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 522 | * cause of the failure. |
| 523 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 524 | public void removeNiceApp(final int uid) { |
Motomu Utsumi | 7392eb4 | 2022-06-29 03:53:03 +0000 | [diff] [blame] | 525 | throwIfPreT("removeNiceApp is not available on pre-T devices"); |
Motomu Utsumi | 5f15f75 | 2022-08-03 06:27:51 +0000 | [diff] [blame] | 526 | |
| 527 | if (sEnableJavaBpfMap) { |
| 528 | removeRule(uid, HAPPY_BOX_MATCH, "removeNiceApp"); |
| 529 | } else { |
| 530 | final int err = native_removeNiceApp(uid); |
| 531 | maybeThrow(err, "Unable to remove nice app"); |
| 532 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 533 | } |
| 534 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 535 | /** |
| 536 | * Set target firewall child chain |
| 537 | * |
| 538 | * @param childChain target chain to enable |
| 539 | * @param enable whether to enable or disable child chain. |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 540 | * @throws UnsupportedOperationException if called on pre-T devices. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 541 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 542 | * cause of the failure. |
| 543 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 544 | public void setChildChain(final int childChain, final boolean enable) { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 545 | throwIfPreT("setChildChain is not available on pre-T devices"); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 546 | |
Motomu Utsumi | e057dd4 | 2022-08-03 01:23:49 +0000 | [diff] [blame] | 547 | if (sEnableJavaBpfMap) { |
| 548 | final long match = getMatchByFirewallChain(childChain); |
| 549 | try { |
| 550 | synchronized (sUidRulesConfigBpfMapLock) { |
| 551 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
| 552 | final long newConfig = enable ? (config.val | match) : (config.val & ~match); |
| 553 | sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig)); |
| 554 | } |
| 555 | } catch (ErrnoException e) { |
| 556 | throw new ServiceSpecificException(e.errno, |
| 557 | "Unable to set child chain: " + Os.strerror(e.errno)); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 558 | } |
Motomu Utsumi | e057dd4 | 2022-08-03 01:23:49 +0000 | [diff] [blame] | 559 | } else { |
| 560 | final int err = native_setChildChain(childChain, enable); |
| 561 | maybeThrow(err, "Unable to set child chain"); |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 562 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /** |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 566 | * Get the specified firewall chain's status. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 567 | * |
| 568 | * @param childChain target chain |
| 569 | * @return {@code true} if chain is enabled, {@code false} if chain is not enabled. |
| 570 | * @throws UnsupportedOperationException if called on pre-T devices. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 571 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 572 | * cause of the failure. |
| 573 | */ |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 574 | public boolean isChainEnabled(final int childChain) { |
| 575 | throwIfPreT("isChainEnabled is not available on pre-T devices"); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 576 | |
| 577 | final long match = getMatchByFirewallChain(childChain); |
| 578 | try { |
| 579 | final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 580 | return (config.val & match) != 0; |
| 581 | } catch (ErrnoException e) { |
| 582 | throw new ServiceSpecificException(e.errno, |
| 583 | "Unable to get firewall chain status: " + Os.strerror(e.errno)); |
| 584 | } |
| 585 | } |
| 586 | |
Motomu Utsumi | 1a477b0 | 2022-08-23 15:14:56 +0900 | [diff] [blame] | 587 | private Set<Integer> asSet(final int[] uids) { |
| 588 | final Set<Integer> uidSet = new ArraySet<>(); |
| 589 | for (final int uid: uids) { |
| 590 | uidSet.add(uid); |
| 591 | } |
| 592 | return uidSet; |
| 593 | } |
| 594 | |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 595 | /** |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 596 | * Replaces the contents of the specified UID-based firewall chain. |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 597 | * Enables the chain for specified uids and disables the chain for non-specified uids. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 598 | * |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 599 | * @param chain Target chain. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 600 | * @param uids The list of UIDs to allow/deny. |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 601 | * @throws UnsupportedOperationException if called on pre-T devices. |
| 602 | * @throws IllegalArgumentException if {@code chain} is not a valid chain. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 603 | */ |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 604 | public void replaceUidChain(final int chain, final int[] uids) { |
| 605 | throwIfPreT("replaceUidChain is not available on pre-T devices"); |
| 606 | |
Motomu Utsumi | c7c1685 | 2022-08-03 06:51:41 +0000 | [diff] [blame] | 607 | if (sEnableJavaBpfMap) { |
| 608 | final long match; |
| 609 | try { |
| 610 | match = getMatchByFirewallChain(chain); |
| 611 | } catch (ServiceSpecificException e) { |
| 612 | // Throws IllegalArgumentException to keep the behavior of |
| 613 | // ConnectivityManager#replaceFirewallChain API |
| 614 | throw new IllegalArgumentException("Invalid firewall chain: " + chain); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 615 | } |
Motomu Utsumi | 1a477b0 | 2022-08-23 15:14:56 +0900 | [diff] [blame] | 616 | final Set<Integer> uidSet = asSet(uids); |
| 617 | final Set<Integer> uidSetToRemoveRule = new ArraySet<>(); |
Motomu Utsumi | c7c1685 | 2022-08-03 06:51:41 +0000 | [diff] [blame] | 618 | try { |
| 619 | synchronized (sUidOwnerMap) { |
| 620 | sUidOwnerMap.forEach((uid, config) -> { |
| 621 | // config could be null if there is a concurrent entry deletion. |
Motomu Utsumi | 8e420ea | 2022-08-24 18:03:30 +0900 | [diff] [blame] | 622 | // http://b/220084230. But sUidOwnerMap update must be done while holding a |
| 623 | // lock, so this should not happen. |
| 624 | if (config == null) { |
| 625 | Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock"); |
| 626 | } else if (!uidSet.contains((int) uid.val) && (config.rule & match) != 0) { |
Motomu Utsumi | c7c1685 | 2022-08-03 06:51:41 +0000 | [diff] [blame] | 627 | uidSetToRemoveRule.add((int) uid.val); |
| 628 | } |
| 629 | }); |
| 630 | |
| 631 | for (final int uid : uidSetToRemoveRule) { |
| 632 | removeRule(uid, match, "replaceUidChain"); |
| 633 | } |
| 634 | for (final int uid : uids) { |
| 635 | addRule(uid, match, "replaceUidChain"); |
| 636 | } |
| 637 | } |
| 638 | } catch (ErrnoException | ServiceSpecificException e) { |
| 639 | Log.e(TAG, "replaceUidChain failed: " + e); |
| 640 | } |
| 641 | } else { |
| 642 | final int err; |
| 643 | switch (chain) { |
| 644 | case FIREWALL_CHAIN_DOZABLE: |
| 645 | err = native_replaceUidChain("fw_dozable", true /* isAllowList */, uids); |
| 646 | break; |
| 647 | case FIREWALL_CHAIN_STANDBY: |
| 648 | err = native_replaceUidChain("fw_standby", false /* isAllowList */, uids); |
| 649 | break; |
| 650 | case FIREWALL_CHAIN_POWERSAVE: |
| 651 | err = native_replaceUidChain("fw_powersave", true /* isAllowList */, uids); |
| 652 | break; |
| 653 | case FIREWALL_CHAIN_RESTRICTED: |
| 654 | err = native_replaceUidChain("fw_restricted", true /* isAllowList */, uids); |
| 655 | break; |
| 656 | case FIREWALL_CHAIN_LOW_POWER_STANDBY: |
| 657 | err = native_replaceUidChain( |
| 658 | "fw_low_power_standby", true /* isAllowList */, uids); |
| 659 | break; |
| 660 | case FIREWALL_CHAIN_OEM_DENY_1: |
| 661 | err = native_replaceUidChain("fw_oem_deny_1", false /* isAllowList */, uids); |
| 662 | break; |
| 663 | case FIREWALL_CHAIN_OEM_DENY_2: |
| 664 | err = native_replaceUidChain("fw_oem_deny_2", false /* isAllowList */, uids); |
| 665 | break; |
| 666 | case FIREWALL_CHAIN_OEM_DENY_3: |
| 667 | err = native_replaceUidChain("fw_oem_deny_3", false /* isAllowList */, uids); |
| 668 | break; |
| 669 | default: |
| 670 | throw new IllegalArgumentException("replaceFirewallChain with invalid chain: " |
| 671 | + chain); |
| 672 | } |
| 673 | if (err != 0) { |
| 674 | Log.e(TAG, "replaceUidChain failed: " + Os.strerror(-err)); |
| 675 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 676 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 677 | } |
| 678 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 679 | /** |
| 680 | * Set firewall rule for uid |
| 681 | * |
| 682 | * @param childChain target chain |
| 683 | * @param uid uid to allow/deny |
| 684 | * @param firewallRule either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 685 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 686 | * cause of the failure. |
| 687 | */ |
Lorenzo Colitti | 82244fd | 2022-03-04 23:15:00 +0900 | [diff] [blame] | 688 | public void setUidRule(final int childChain, final int uid, final int firewallRule) { |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 689 | throwIfPreT("setUidRule is not available on pre-T devices"); |
| 690 | |
Motomu Utsumi | 381ad9e | 2022-08-03 06:42:47 +0000 | [diff] [blame] | 691 | if (sEnableJavaBpfMap) { |
| 692 | final long match = getMatchByFirewallChain(childChain); |
| 693 | final boolean isAllowList = isFirewallAllowList(childChain); |
| 694 | final boolean add = (firewallRule == FIREWALL_RULE_ALLOW && isAllowList) |
| 695 | || (firewallRule == FIREWALL_RULE_DENY && !isAllowList); |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 696 | |
Motomu Utsumi | 381ad9e | 2022-08-03 06:42:47 +0000 | [diff] [blame] | 697 | if (add) { |
| 698 | addRule(uid, match, "setUidRule"); |
| 699 | } else { |
| 700 | removeRule(uid, match, "setUidRule"); |
| 701 | } |
Motomu Utsumi | 40230be | 2022-07-05 03:27:35 +0000 | [diff] [blame] | 702 | } else { |
Motomu Utsumi | 381ad9e | 2022-08-03 06:42:47 +0000 | [diff] [blame] | 703 | final int err = native_setUidRule(childChain, uid, firewallRule); |
| 704 | maybeThrow(err, "Unable to set uid rule"); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 705 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Add ingress interface filtering rules to a list of UIDs |
| 710 | * |
| 711 | * For a given uid, once a filtering rule is added, the kernel will only allow packets from the |
| 712 | * allowed interface and loopback to be sent to the list of UIDs. |
| 713 | * |
| 714 | * Calling this method on one or more UIDs with an existing filtering rule but a different |
| 715 | * interface name will result in the filtering rule being updated to allow the new interface |
| 716 | * instead. Otherwise calling this method will not affect existing rules set on other UIDs. |
| 717 | * |
| 718 | * @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] | 719 | * be received. |
| 720 | * @param uids an array of UIDs which the filtering rules will be set |
| 721 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 722 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 723 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 724 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 725 | public void addUidInterfaceRules(final String ifName, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 726 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 727 | mNetd.firewallAddUidInterfaceRules(ifName, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 728 | return; |
| 729 | } |
Motomu Utsumi | f794e7d | 2022-08-03 06:38:43 +0000 | [diff] [blame] | 730 | |
| 731 | if (sEnableJavaBpfMap) { |
| 732 | // Null ifName is a wildcard to allow apps to receive packets on all interfaces and |
| 733 | // ifIndex is set to 0. |
| 734 | final int ifIndex; |
| 735 | if (ifName == null) { |
| 736 | ifIndex = 0; |
| 737 | } else { |
| 738 | ifIndex = mDeps.getIfIndex(ifName); |
| 739 | if (ifIndex == 0) { |
| 740 | throw new ServiceSpecificException(ENODEV, |
| 741 | "Failed to get index of interface " + ifName); |
| 742 | } |
| 743 | } |
| 744 | for (final int uid : uids) { |
| 745 | try { |
| 746 | addRule(uid, IIF_MATCH, ifIndex, "addUidInterfaceRules"); |
| 747 | } catch (ServiceSpecificException e) { |
| 748 | Log.e(TAG, "addRule failed uid=" + uid + " ifName=" + ifName + ", " + e); |
| 749 | } |
| 750 | } |
Motomu Utsumi | 5f52f4f | 2022-06-30 03:31:09 +0000 | [diff] [blame] | 751 | } else { |
Motomu Utsumi | f794e7d | 2022-08-03 06:38:43 +0000 | [diff] [blame] | 752 | final int err = native_addUidInterfaceRules(ifName, uids); |
| 753 | maybeThrow(err, "Unable to add uid interface rules"); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 754 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Remove ingress interface filtering rules from a list of UIDs |
| 759 | * |
| 760 | * Clear the ingress interface filtering rules from the list of UIDs which were previously set |
| 761 | * by addUidInterfaceRules(). Ignore any uid which does not have filtering rule. |
| 762 | * |
| 763 | * @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] | 764 | * @throws RemoteException when netd has crashed. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 765 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 766 | * cause of the failure. |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 767 | */ |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 768 | public void removeUidInterfaceRules(final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 769 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 770 | mNetd.firewallRemoveUidInterfaceRules(uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 771 | return; |
| 772 | } |
Motomu Utsumi | 7dc657d | 2022-08-03 06:40:46 +0000 | [diff] [blame] | 773 | |
| 774 | if (sEnableJavaBpfMap) { |
| 775 | for (final int uid : uids) { |
| 776 | try { |
| 777 | removeRule(uid, IIF_MATCH, "removeUidInterfaceRules"); |
| 778 | } catch (ServiceSpecificException e) { |
| 779 | Log.e(TAG, "removeRule failed uid=" + uid + ", " + e); |
| 780 | } |
Motomu Utsumi | 599c4e5 | 2022-06-30 03:37:18 +0000 | [diff] [blame] | 781 | } |
Motomu Utsumi | 7dc657d | 2022-08-03 06:40:46 +0000 | [diff] [blame] | 782 | } else { |
| 783 | final int err = native_removeUidInterfaceRules(uids); |
| 784 | maybeThrow(err, "Unable to remove uid interface rules"); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 785 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 786 | } |
| 787 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 788 | /** |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 789 | * Update lockdown rule for uid |
| 790 | * |
| 791 | * @param uid target uid to add/remove the rule |
| 792 | * @param add {@code true} to add the rule, {@code false} to remove the rule. |
| 793 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 794 | * cause of the failure. |
| 795 | */ |
| 796 | public void updateUidLockdownRule(final int uid, final boolean add) { |
Motomu Utsumi | 697b299 | 2022-06-30 02:25:29 +0000 | [diff] [blame] | 797 | throwIfPreT("updateUidLockdownRule is not available on pre-T devices"); |
Motomu Utsumi | b2d32b7 | 2022-08-03 06:31:58 +0000 | [diff] [blame] | 798 | |
| 799 | if (sEnableJavaBpfMap) { |
| 800 | if (add) { |
| 801 | addRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule"); |
| 802 | } else { |
| 803 | removeRule(uid, LOCKDOWN_VPN_MATCH, "updateUidLockdownRule"); |
| 804 | } |
Motomu Utsumi | 697b299 | 2022-06-30 02:25:29 +0000 | [diff] [blame] | 805 | } else { |
Motomu Utsumi | b2d32b7 | 2022-08-03 06:31:58 +0000 | [diff] [blame] | 806 | final int err = native_updateUidLockdownRule(uid, add); |
| 807 | maybeThrow(err, "Unable to update lockdown rule"); |
Motomu Utsumi | 5a68a21 | 2022-06-24 10:14:31 +0000 | [diff] [blame] | 808 | } |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /** |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 812 | * Request netd to change the current active network stats map. |
| 813 | * |
Motomu Utsumi | 7abeaa4 | 2022-07-20 07:54:18 +0000 | [diff] [blame] | 814 | * @throws UnsupportedOperationException if called on pre-T devices. |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 815 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 816 | * cause of the failure. |
| 817 | */ |
markchien | 49e944c | 2022-03-01 15:22:20 +0800 | [diff] [blame] | 818 | public void swapActiveStatsMap() { |
Motomu Utsumi | 7abeaa4 | 2022-07-20 07:54:18 +0000 | [diff] [blame] | 819 | throwIfPreT("swapActiveStatsMap is not available on pre-T devices"); |
| 820 | |
| 821 | if (sEnableJavaBpfMap) { |
| 822 | try { |
| 823 | synchronized (sCurrentStatsMapConfigLock) { |
| 824 | final long config = sConfigurationMap.getValue( |
| 825 | CURRENT_STATS_MAP_CONFIGURATION_KEY).val; |
| 826 | final long newConfig = (config == STATS_SELECT_MAP_A) |
| 827 | ? STATS_SELECT_MAP_B : STATS_SELECT_MAP_A; |
| 828 | sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY, |
| 829 | new U32(newConfig)); |
| 830 | } |
| 831 | } catch (ErrnoException e) { |
| 832 | throw new ServiceSpecificException(e.errno, "Failed to swap active stats map"); |
| 833 | } |
| 834 | |
| 835 | // After changing the config, it's needed to make sure all the current running eBPF |
| 836 | // programs are finished and all the CPUs are aware of this config change before the old |
| 837 | // map is modified. So special hack is needed here to wait for the kernel to do a |
| 838 | // synchronize_rcu(). Once the kernel called synchronize_rcu(), the updated config will |
| 839 | // be available to all cores and the next eBPF programs triggered inside the kernel will |
| 840 | // use the new map configuration. So once this function returns it is safe to modify the |
| 841 | // old stats map without concerning about race between the kernel and userspace. |
| 842 | final int err = mDeps.synchronizeKernelRCU(); |
| 843 | maybeThrow(err, "synchronizeKernelRCU failed"); |
| 844 | } else { |
| 845 | final int err = native_swapActiveStatsMap(); |
| 846 | maybeThrow(err, "Unable to swap active stats map"); |
| 847 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 848 | } |
| 849 | |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 850 | /** |
| 851 | * Assigns android.permission.INTERNET and/or android.permission.UPDATE_DEVICE_STATS to the uids |
| 852 | * specified. Or remove all permissions from the uids. |
| 853 | * |
| 854 | * @param permissions The permission to grant, it could be either PERMISSION_INTERNET and/or |
| 855 | * PERMISSION_UPDATE_DEVICE_STATS. If the permission is NO_PERMISSIONS, then |
| 856 | * revoke all permissions for the uids. |
| 857 | * @param uids uid of users to grant permission |
| 858 | * @throws RemoteException when netd has crashed. |
| 859 | */ |
| 860 | public void setNetPermForUids(final int permissions, final int[] uids) throws RemoteException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 861 | if (PRE_T) { |
Ken Chen | f5f5133 | 2022-01-28 10:08:16 +0800 | [diff] [blame] | 862 | mNetd.trafficSetNetPermForUids(permissions, uids); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 863 | return; |
| 864 | } |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 865 | |
| 866 | if (sEnableJavaBpfMap) { |
| 867 | // Remove the entry if package is uninstalled or uid has only INTERNET permission. |
| 868 | if (permissions == PERMISSION_UNINSTALLED || permissions == PERMISSION_INTERNET) { |
| 869 | for (final int uid : uids) { |
| 870 | try { |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 871 | sUidPermissionMap.deleteEntry(new S32(uid)); |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 872 | } catch (ErrnoException e) { |
| 873 | Log.e(TAG, "Failed to remove uid " + uid + " from permission map: " + e); |
| 874 | } |
| 875 | } |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | for (final int uid : uids) { |
| 880 | try { |
Maciej Żenczykowski | 785793f | 2022-09-17 02:35:20 +0000 | [diff] [blame] | 881 | sUidPermissionMap.updateEntry(new S32(uid), new U8((short) permissions)); |
Motomu Utsumi | 6527120 | 2022-07-05 08:21:41 +0000 | [diff] [blame] | 882 | } catch (ErrnoException e) { |
| 883 | Log.e(TAG, "Failed to set permission " |
| 884 | + permissions + " to uid " + uid + ": " + e); |
| 885 | } |
| 886 | } |
| 887 | } else { |
| 888 | native_setPermissionForUids(permissions, uids); |
| 889 | } |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 890 | } |
| 891 | |
Motomu Utsumi | 166f966 | 2022-09-01 10:35:29 +0900 | [diff] [blame] | 892 | /** Register callback for statsd to pull atom. */ |
| 893 | public void setPullAtomCallback(final Context context) { |
| 894 | throwIfPreT("setPullAtomCallback is not available on pre-T devices"); |
| 895 | |
| 896 | final StatsManager statsManager = context.getSystemService(StatsManager.class); |
| 897 | statsManager.setPullAtomCallback(NETWORK_BPF_MAP_INFO, null /* metadata */, |
| 898 | BackgroundThread.getExecutor(), this::pullBpfMapInfoAtom); |
| 899 | } |
| 900 | |
| 901 | private <K extends Struct, V extends Struct> int getMapSize(IBpfMap<K, V> map) |
| 902 | throws ErrnoException { |
| 903 | // forEach could restart iteration from the beginning if there is a concurrent entry |
| 904 | // deletion. netd and skDestroyListener could delete CookieTagMap entry concurrently. |
| 905 | // So using Set to count the number of entry in the map. |
| 906 | Set<K> keySet = new ArraySet<>(); |
| 907 | map.forEach((k, v) -> keySet.add(k)); |
| 908 | return keySet.size(); |
| 909 | } |
| 910 | |
| 911 | /** Callback for StatsManager#setPullAtomCallback */ |
| 912 | @VisibleForTesting |
| 913 | public int pullBpfMapInfoAtom(final int atomTag, final List<StatsEvent> data) { |
| 914 | if (atomTag != NETWORK_BPF_MAP_INFO) { |
| 915 | Log.e(TAG, "Unexpected atom tag: " + atomTag); |
| 916 | return StatsManager.PULL_SKIP; |
| 917 | } |
| 918 | |
| 919 | try { |
| 920 | data.add(mDeps.buildStatsEvent(getMapSize(sCookieTagMap), getMapSize(sUidOwnerMap), |
| 921 | getMapSize(sUidPermissionMap))); |
| 922 | } catch (ErrnoException e) { |
| 923 | Log.e(TAG, "Failed to pull NETWORK_BPF_MAP_INFO atom: " + e); |
| 924 | return StatsManager.PULL_SKIP; |
| 925 | } |
| 926 | return StatsManager.PULL_SUCCESS; |
| 927 | } |
| 928 | |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 929 | private String permissionToString(int permissionMask) { |
| 930 | if (permissionMask == PERMISSION_NONE) { |
| 931 | return "PERMISSION_NONE"; |
| 932 | } |
| 933 | if (permissionMask == PERMISSION_UNINSTALLED) { |
| 934 | // PERMISSION_UNINSTALLED should never appear in the map |
| 935 | return "PERMISSION_UNINSTALLED error!"; |
| 936 | } |
| 937 | |
| 938 | final StringJoiner sj = new StringJoiner(" "); |
| 939 | for (Pair<Integer, String> permission: PERMISSION_LIST) { |
| 940 | final int permissionFlag = permission.first; |
| 941 | final String permissionName = permission.second; |
| 942 | if ((permissionMask & permissionFlag) != 0) { |
| 943 | sj.add(permissionName); |
| 944 | permissionMask &= ~permissionFlag; |
| 945 | } |
| 946 | } |
| 947 | if (permissionMask != 0) { |
| 948 | sj.add("PERMISSION_UNKNOWN(" + permissionMask + ")"); |
| 949 | } |
| 950 | return sj.toString(); |
| 951 | } |
| 952 | |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 953 | /** |
| 954 | * Dump BPF maps |
| 955 | * |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 956 | * @param pw print writer |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 957 | * @param fd file descriptor to output |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 958 | * @param verbose verbose dump flag, if true dump the BpfMap contents |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 959 | * @throws IOException when file descriptor is invalid. |
| 960 | * @throws ServiceSpecificException when the method is called on an unsupported device. |
| 961 | */ |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 962 | public void dump(final IndentingPrintWriter pw, final FileDescriptor fd, boolean verbose) |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 963 | throws IOException, ServiceSpecificException { |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 964 | if (PRE_T) { |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 965 | throw new ServiceSpecificException( |
| 966 | EOPNOTSUPP, "dumpsys connectivity trafficcontroller dump not available on pre-T" |
| 967 | + " devices, use dumpsys netd trafficcontroller instead."); |
| 968 | } |
Motomu Utsumi | df8d299 | 2022-09-01 17:08:27 +0900 | [diff] [blame] | 969 | mDeps.nativeDump(fd, verbose); |
Motomu Utsumi | 310850f | 2022-09-02 12:48:20 +0900 | [diff] [blame^] | 970 | |
| 971 | if (verbose) { |
| 972 | BpfDump.dumpMap(sUidPermissionMap, pw, "sUidPermissionMap", |
| 973 | (uid, permission) -> uid.val + " " + permissionToString(permission.val)); |
| 974 | } |
Ken Chen | e6d511f | 2022-01-25 11:10:42 +0800 | [diff] [blame] | 975 | } |
| 976 | |
Motomu Utsumi | 3af8f0e | 2022-09-02 23:42:13 +0900 | [diff] [blame] | 977 | private static native void native_init(boolean startSkDestroyListener); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 978 | private native int native_addNaughtyApp(int uid); |
| 979 | private native int native_removeNaughtyApp(int uid); |
| 980 | private native int native_addNiceApp(int uid); |
| 981 | private native int native_removeNiceApp(int uid); |
Motomu Utsumi | 114cd9c | 2022-08-01 02:08:35 +0000 | [diff] [blame] | 982 | private native int native_setChildChain(int childChain, boolean enable); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 983 | private native int native_replaceUidChain(String name, boolean isAllowlist, int[] uids); |
| 984 | private native int native_setUidRule(int childChain, int uid, int firewallRule); |
| 985 | private native int native_addUidInterfaceRules(String ifName, int[] uids); |
| 986 | private native int native_removeUidInterfaceRules(int[] uids); |
Motomu Utsumi | 8b42e6d | 2022-05-19 06:23:40 +0000 | [diff] [blame] | 987 | private native int native_updateUidLockdownRule(int uid, boolean add); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 988 | private native int native_swapActiveStatsMap(); |
Wayne Ma | 2fde98c | 2022-01-17 18:04:05 +0800 | [diff] [blame] | 989 | private native void native_setPermissionForUids(int permissions, int[] uids); |
Motomu Utsumi | df8d299 | 2022-09-01 17:08:27 +0900 | [diff] [blame] | 990 | private static native void native_dump(FileDescriptor fd, boolean verbose); |
Motomu Utsumi | 7abeaa4 | 2022-07-20 07:54:18 +0000 | [diff] [blame] | 991 | private static native int native_synchronizeKernelRCU(); |
Wayne Ma | 0ea3bdc | 2022-01-12 01:12:11 +0800 | [diff] [blame] | 992 | } |