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