blob: 28d5891fe778eed857084b0282b39a06abe619e7 [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
19import static android.net.BpfNetMapsConstants.DOZABLE_MATCH;
20import static android.net.BpfNetMapsConstants.LOW_POWER_STANDBY_MATCH;
21import static android.net.BpfNetMapsConstants.MATCH_LIST;
22import static android.net.BpfNetMapsConstants.NO_MATCH;
23import static android.net.BpfNetMapsConstants.OEM_DENY_1_MATCH;
24import static android.net.BpfNetMapsConstants.OEM_DENY_2_MATCH;
25import static android.net.BpfNetMapsConstants.OEM_DENY_3_MATCH;
26import static android.net.BpfNetMapsConstants.POWERSAVE_MATCH;
27import static android.net.BpfNetMapsConstants.RESTRICTED_MATCH;
28import static android.net.BpfNetMapsConstants.STANDBY_MATCH;
29import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE;
30import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY;
31import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1;
32import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2;
33import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3;
34import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE;
35import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED;
36import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY;
37import static android.system.OsConstants.EINVAL;
38
39import android.os.ServiceSpecificException;
40import android.util.Pair;
41
Junyu Lai626045a2023-08-28 18:49:44 +080042import com.android.modules.utils.build.SdkLevel;
43
Junyu Lai29b7b632023-08-23 17:35:17 +080044import 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.
54public 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 Lai626045a2023-08-28 18:49:44 +0800129
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 Lai29b7b632023-08-23 17:35:17 +0800140}