2023-08-23 17:35:17 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2023 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package android.net;
|
|
|
|
|
|
2023-08-29 18:32:57 +08:00
|
|
|
import static android.net.BpfNetMapsConstants.ALLOW_CHAINS;
|
2023-11-06 08:47:40 -08:00
|
|
|
import static android.net.BpfNetMapsConstants.BACKGROUND_MATCH;
|
2023-08-29 18:32:57 +08:00
|
|
|
import static android.net.BpfNetMapsConstants.DENY_CHAINS;
|
2023-08-23 17:35:17 +08:00
|
|
|
import static android.net.BpfNetMapsConstants.DOZABLE_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.LOW_POWER_STANDBY_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.MATCH_LIST;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.NO_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.OEM_DENY_1_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.OEM_DENY_2_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.OEM_DENY_3_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.POWERSAVE_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.RESTRICTED_MATCH;
|
|
|
|
|
import static android.net.BpfNetMapsConstants.STANDBY_MATCH;
|
2023-11-06 08:47:40 -08:00
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_BACKGROUND;
|
2023-08-23 17:35:17 +08:00
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE;
|
|
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY;
|
|
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1;
|
|
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2;
|
|
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_3;
|
|
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE;
|
|
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED;
|
|
|
|
|
import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY;
|
|
|
|
|
import static android.system.OsConstants.EINVAL;
|
|
|
|
|
|
|
|
|
|
import android.os.ServiceSpecificException;
|
|
|
|
|
import android.util.Pair;
|
|
|
|
|
|
2023-08-28 18:49:44 +08:00
|
|
|
import com.android.modules.utils.build.SdkLevel;
|
|
|
|
|
|
2023-08-23 17:35:17 +08:00
|
|
|
import java.util.StringJoiner;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The classes and the methods for BpfNetMaps utilization.
|
|
|
|
|
*
|
|
|
|
|
* @hide
|
|
|
|
|
*/
|
|
|
|
|
// Note that this class should be put into bootclasspath instead of static libraries.
|
|
|
|
|
// Because modules could have different copies of this class if this is statically linked,
|
|
|
|
|
// which would be problematic if the definitions in these modules are not synchronized.
|
|
|
|
|
public class BpfNetMapsUtils {
|
|
|
|
|
// Prevent this class from being accidental instantiated.
|
|
|
|
|
private BpfNetMapsUtils() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get corresponding match from firewall chain.
|
|
|
|
|
*/
|
|
|
|
|
public static long getMatchByFirewallChain(final int chain) {
|
|
|
|
|
switch (chain) {
|
|
|
|
|
case FIREWALL_CHAIN_DOZABLE:
|
|
|
|
|
return DOZABLE_MATCH;
|
|
|
|
|
case FIREWALL_CHAIN_STANDBY:
|
|
|
|
|
return STANDBY_MATCH;
|
|
|
|
|
case FIREWALL_CHAIN_POWERSAVE:
|
|
|
|
|
return POWERSAVE_MATCH;
|
|
|
|
|
case FIREWALL_CHAIN_RESTRICTED:
|
|
|
|
|
return RESTRICTED_MATCH;
|
2023-11-06 08:47:40 -08:00
|
|
|
case FIREWALL_CHAIN_BACKGROUND:
|
|
|
|
|
return BACKGROUND_MATCH;
|
2023-08-23 17:35:17 +08:00
|
|
|
case FIREWALL_CHAIN_LOW_POWER_STANDBY:
|
|
|
|
|
return LOW_POWER_STANDBY_MATCH;
|
|
|
|
|
case FIREWALL_CHAIN_OEM_DENY_1:
|
|
|
|
|
return OEM_DENY_1_MATCH;
|
|
|
|
|
case FIREWALL_CHAIN_OEM_DENY_2:
|
|
|
|
|
return OEM_DENY_2_MATCH;
|
|
|
|
|
case FIREWALL_CHAIN_OEM_DENY_3:
|
|
|
|
|
return OEM_DENY_3_MATCH;
|
|
|
|
|
default:
|
|
|
|
|
throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-29 18:32:57 +08:00
|
|
|
* Get whether the chain is an allow-list or a deny-list.
|
2023-08-23 17:35:17 +08:00
|
|
|
*
|
|
|
|
|
* ALLOWLIST means the firewall denies all by default, uids must be explicitly allowed
|
2023-08-29 18:32:57 +08:00
|
|
|
* DENYLIST means the firewall allows all by default, uids must be explicitly denied
|
2023-08-23 17:35:17 +08:00
|
|
|
*/
|
|
|
|
|
public static boolean isFirewallAllowList(final int chain) {
|
2023-08-29 18:32:57 +08:00
|
|
|
if (ALLOW_CHAINS.contains(chain)) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (DENY_CHAINS.contains(chain)) {
|
|
|
|
|
return false;
|
2023-08-23 17:35:17 +08:00
|
|
|
}
|
2023-08-29 18:32:57 +08:00
|
|
|
throw new ServiceSpecificException(EINVAL, "Invalid firewall chain: " + chain);
|
2023-08-23 17:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get match string representation from the given match bitmap.
|
|
|
|
|
*/
|
|
|
|
|
public static String matchToString(long matchMask) {
|
|
|
|
|
if (matchMask == NO_MATCH) {
|
|
|
|
|
return "NO_MATCH";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final StringJoiner sj = new StringJoiner(" ");
|
|
|
|
|
for (final Pair<Long, String> match : MATCH_LIST) {
|
|
|
|
|
final long matchFlag = match.first;
|
|
|
|
|
final String matchName = match.second;
|
|
|
|
|
if ((matchMask & matchFlag) != 0) {
|
|
|
|
|
sj.add(matchName);
|
|
|
|
|
matchMask &= ~matchFlag;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (matchMask != 0) {
|
|
|
|
|
sj.add("UNKNOWN_MATCH(" + matchMask + ")");
|
|
|
|
|
}
|
|
|
|
|
return sj.toString();
|
|
|
|
|
}
|
2023-08-28 18:49:44 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Throw UnsupportedOperationException if SdkLevel is before T.
|
|
|
|
|
*/
|
|
|
|
|
public static void throwIfPreT(final String msg) {
|
2024-01-25 15:51:23 -08:00
|
|
|
if (!SdkLevel.isAtLeastT()) {
|
2023-08-28 18:49:44 +08:00
|
|
|
throw new UnsupportedOperationException(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-23 17:35:17 +08:00
|
|
|
}
|