Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 android.net; |
| 18 | |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 19 | import static android.net.BpfNetMapsConstants.ALLOW_CHAINS; |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 20 | import static android.net.BpfNetMapsConstants.BACKGROUND_MATCH; |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 21 | import static android.net.BpfNetMapsConstants.DENY_CHAINS; |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 22 | import static android.net.BpfNetMapsConstants.DOZABLE_MATCH; |
| 23 | import static android.net.BpfNetMapsConstants.LOW_POWER_STANDBY_MATCH; |
| 24 | import static android.net.BpfNetMapsConstants.MATCH_LIST; |
| 25 | import static android.net.BpfNetMapsConstants.NO_MATCH; |
| 26 | import static android.net.BpfNetMapsConstants.OEM_DENY_1_MATCH; |
| 27 | import static android.net.BpfNetMapsConstants.OEM_DENY_2_MATCH; |
| 28 | import static android.net.BpfNetMapsConstants.OEM_DENY_3_MATCH; |
| 29 | import static android.net.BpfNetMapsConstants.POWERSAVE_MATCH; |
| 30 | import static android.net.BpfNetMapsConstants.RESTRICTED_MATCH; |
| 31 | import static android.net.BpfNetMapsConstants.STANDBY_MATCH; |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 32 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_BACKGROUND; |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 33 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE; |
| 34 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY; |
| 35 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1; |
| 36 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2; |
| 37 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3; |
| 38 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE; |
| 39 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED; |
| 40 | import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY; |
| 41 | import static android.system.OsConstants.EINVAL; |
| 42 | |
| 43 | import android.os.ServiceSpecificException; |
| 44 | import android.util.Pair; |
| 45 | |
Junyu Lai | 626045a | 2023-08-28 18:49:44 +0800 | [diff] [blame] | 46 | import com.android.modules.utils.build.SdkLevel; |
| 47 | |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 48 | import java.util.StringJoiner; |
| 49 | |
| 50 | /** |
| 51 | * The classes and the methods for BpfNetMaps utilization. |
| 52 | * |
| 53 | * @hide |
| 54 | */ |
| 55 | // Note that this class should be put into bootclasspath instead of static libraries. |
| 56 | // Because modules could have different copies of this class if this is statically linked, |
| 57 | // which would be problematic if the definitions in these modules are not synchronized. |
| 58 | public class BpfNetMapsUtils { |
| 59 | // Prevent this class from being accidental instantiated. |
| 60 | private BpfNetMapsUtils() {} |
| 61 | |
| 62 | /** |
| 63 | * Get corresponding match from firewall chain. |
| 64 | */ |
| 65 | public static long getMatchByFirewallChain(final int chain) { |
| 66 | switch (chain) { |
| 67 | case FIREWALL_CHAIN_DOZABLE: |
| 68 | return DOZABLE_MATCH; |
| 69 | case FIREWALL_CHAIN_STANDBY: |
| 70 | return STANDBY_MATCH; |
| 71 | case FIREWALL_CHAIN_POWERSAVE: |
| 72 | return POWERSAVE_MATCH; |
| 73 | case FIREWALL_CHAIN_RESTRICTED: |
| 74 | return RESTRICTED_MATCH; |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 75 | case FIREWALL_CHAIN_BACKGROUND: |
| 76 | return BACKGROUND_MATCH; |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 77 | case FIREWALL_CHAIN_LOW_POWER_STANDBY: |
| 78 | return LOW_POWER_STANDBY_MATCH; |
| 79 | case FIREWALL_CHAIN_OEM_DENY_1: |
| 80 | return OEM_DENY_1_MATCH; |
| 81 | case FIREWALL_CHAIN_OEM_DENY_2: |
| 82 | return OEM_DENY_2_MATCH; |
| 83 | case FIREWALL_CHAIN_OEM_DENY_3: |
| 84 | return OEM_DENY_3_MATCH; |
| 85 | default: |
| 86 | throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 91 | * Get whether the chain is an allow-list or a deny-list. |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 92 | * |
| 93 | * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 94 | * DENYLIST means the firewall allows all by default, uids must be explicitly denied |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 95 | */ |
| 96 | public static boolean isFirewallAllowList(final int chain) { |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 97 | if (ALLOW_CHAINS.contains(chain)) { |
| 98 | return true; |
| 99 | } else if (DENY_CHAINS.contains(chain)) { |
| 100 | return false; |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 101 | } |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 102 | throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain); |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get match string representation from the given match bitmap. |
| 107 | */ |
| 108 | public static String matchToString(long matchMask) { |
| 109 | if (matchMask == NO_MATCH) { |
| 110 | return "NO_MATCH"; |
| 111 | } |
| 112 | |
| 113 | final StringJoiner sj = new StringJoiner(" "); |
| 114 | for (final Pair<Long, String> match : MATCH_LIST) { |
| 115 | final long matchFlag = match.first; |
| 116 | final String matchName = match.second; |
| 117 | if ((matchMask & matchFlag) != 0) { |
| 118 | sj.add(matchName); |
| 119 | matchMask &= ~matchFlag; |
| 120 | } |
| 121 | } |
| 122 | if (matchMask != 0) { |
| 123 | sj.add("UNKNOWN_MATCH(" + matchMask + ")"); |
| 124 | } |
| 125 | return sj.toString(); |
| 126 | } |
Junyu Lai | 626045a | 2023-08-28 18:49:44 +0800 | [diff] [blame] | 127 | |
| 128 | public static final boolean PRE_T = !SdkLevel.isAtLeastT(); |
| 129 | |
| 130 | /** |
| 131 | * Throw UnsupportedOperationException if SdkLevel is before T. |
| 132 | */ |
| 133 | public static void throwIfPreT(final String msg) { |
| 134 | if (PRE_T) { |
| 135 | throw new UnsupportedOperationException(msg); |
| 136 | } |
| 137 | } |
Junyu Lai | 29b7b63 | 2023-08-23 17:35:17 +0800 | [diff] [blame] | 138 | } |