blob: d464e3df2c4aa62c3fcd47d17f4cec472e712733 [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
42import java.util.StringJoiner;
43
44/**
45 * The classes and the methods for BpfNetMaps utilization.
46 *
47 * @hide
48 */
49// Note that this class should be put into bootclasspath instead of static libraries.
50// Because modules could have different copies of this class if this is statically linked,
51// which would be problematic if the definitions in these modules are not synchronized.
52public class BpfNetMapsUtils {
53 // Prevent this class from being accidental instantiated.
54 private BpfNetMapsUtils() {}
55
56 /**
57 * Get corresponding match from firewall chain.
58 */
59 public static long getMatchByFirewallChain(final int chain) {
60 switch (chain) {
61 case FIREWALL_CHAIN_DOZABLE:
62 return DOZABLE_MATCH;
63 case FIREWALL_CHAIN_STANDBY:
64 return STANDBY_MATCH;
65 case FIREWALL_CHAIN_POWERSAVE:
66 return POWERSAVE_MATCH;
67 case FIREWALL_CHAIN_RESTRICTED:
68 return RESTRICTED_MATCH;
69 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
70 return LOW_POWER_STANDBY_MATCH;
71 case FIREWALL_CHAIN_OEM_DENY_1:
72 return OEM_DENY_1_MATCH;
73 case FIREWALL_CHAIN_OEM_DENY_2:
74 return OEM_DENY_2_MATCH;
75 case FIREWALL_CHAIN_OEM_DENY_3:
76 return OEM_DENY_3_MATCH;
77 default:
78 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
79 }
80 }
81
82 /**
83 * Get if the chain is allow list or not.
84 *
85 * ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed
86 * DENYLIST means the firewall allows all by default, uids must be explicitly denyed
87 */
88 public static boolean isFirewallAllowList(final int chain) {
89 switch (chain) {
90 case FIREWALL_CHAIN_DOZABLE:
91 case FIREWALL_CHAIN_POWERSAVE:
92 case FIREWALL_CHAIN_RESTRICTED:
93 case FIREWALL_CHAIN_LOW_POWER_STANDBY:
94 return true;
95 case FIREWALL_CHAIN_STANDBY:
96 case FIREWALL_CHAIN_OEM_DENY_1:
97 case FIREWALL_CHAIN_OEM_DENY_2:
98 case FIREWALL_CHAIN_OEM_DENY_3:
99 return false;
100 default:
101 throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
102 }
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 }
127}