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