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