blob: 11d610cbfeee7c826e83f65972122d49c8a8b38b [file] [log] [blame]
Junyu Lai29b7b632023-08-23 17:35:17 +08001/*
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
17package android.net;
18
Junyu Laie0031522023-08-29 18:32:57 +080019import static android.net.BpfNetMapsConstants.ALLOW_CHAINS;
Suprabh Shukla2d893b62023-11-06 08:47:40 -080020import static android.net.BpfNetMapsConstants.BACKGROUND_MATCH;
Junyu Laie0031522023-08-29 18:32:57 +080021import static android.net.BpfNetMapsConstants.DENY_CHAINS;
Junyu Lai29b7b632023-08-23 17:35:17 +080022import static android.net.BpfNetMapsConstants.DOZABLE_MATCH;
23import static android.net.BpfNetMapsConstants.LOW_POWER_STANDBY_MATCH;
24import static android.net.BpfNetMapsConstants.MATCH_LIST;
25import static android.net.BpfNetMapsConstants.NO_MATCH;
26import static android.net.BpfNetMapsConstants.OEM_DENY_1_MATCH;
27import static android.net.BpfNetMapsConstants.OEM_DENY_2_MATCH;
28import static android.net.BpfNetMapsConstants.OEM_DENY_3_MATCH;
29import static android.net.BpfNetMapsConstants.POWERSAVE_MATCH;
30import static android.net.BpfNetMapsConstants.RESTRICTED_MATCH;
31import static android.net.BpfNetMapsConstants.STANDBY_MATCH;
Suprabh Shukla2d893b62023-11-06 08:47:40 -080032import static android.net.ConnectivityManager.FIREWALL_CHAIN_BACKGROUND;
Junyu Lai29b7b632023-08-23 17:35:17 +080033import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE;
34import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY;
35import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1;
36import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2;
37import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3;
38import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE;
39import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED;
40import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY;
41import static android.system.OsConstants.EINVAL;
42
43import android.os.ServiceSpecificException;
44import android.util.Pair;
45
Junyu Lai626045a2023-08-28 18:49:44 +080046import com.android.modules.utils.build.SdkLevel;
47
Junyu Lai29b7b632023-08-23 17:35:17 +080048import 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.
58public 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 Shukla2d893b62023-11-06 08:47:40 -080075 case FIREWALL_CHAIN_BACKGROUND:
76 return BACKGROUND_MATCH;
Junyu Lai29b7b632023-08-23 17:35:17 +080077 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 Laie0031522023-08-29 18:32:57 +080091 * Get whether the chain is an allow-list or a deny-list.
Junyu Lai29b7b632023-08-23 17:35:17 +080092 *
93 * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed
Junyu Laie0031522023-08-29 18:32:57 +080094 * DENYLIST means the firewall allows all by default, uids must be explicitly denied
Junyu Lai29b7b632023-08-23 17:35:17 +080095 */
96 public static boolean isFirewallAllowList(final int chain) {
Junyu Laie0031522023-08-29 18:32:57 +080097 if (ALLOW_CHAINS.contains(chain)) {
98 return true;
99 } else if (DENY_CHAINS.contains(chain)) {
100 return false;
Junyu Lai29b7b632023-08-23 17:35:17 +0800101 }
Junyu Laie0031522023-08-29 18:32:57 +0800102 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
Junyu Lai29b7b632023-08-23 17:35:17 +0800103 }
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 Lai626045a2023-08-28 18:49:44 +0800127
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 Lai29b7b632023-08-23 17:35:17 +0800138}