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