Merge changes from topics "vpnmove-getconnectionowneruid", "vpnmove-systemdefaultcallback", "vpnmove-vpntransportinfo" am: 250855cb84

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1572981

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I83b4210780cf87006be5f55439ade06cd7893530
This commit is contained in:
Lorenzo Colitti
2021-02-10 06:55:56 +00:00
committed by Automerger Merge Worker
4 changed files with 84 additions and 28 deletions

View File

@@ -21,6 +21,7 @@ import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST;
import static android.net.NetworkRequest.Type.LISTEN; import static android.net.NetworkRequest.Type.LISTEN;
import static android.net.NetworkRequest.Type.REQUEST; import static android.net.NetworkRequest.Type.REQUEST;
import static android.net.NetworkRequest.Type.TRACK_DEFAULT; import static android.net.NetworkRequest.Type.TRACK_DEFAULT;
import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT;
import static android.net.QosCallback.QosCallbackRegistrationException; import static android.net.QosCallback.QosCallbackRegistrationException;
import android.annotation.CallbackExecutor; import android.annotation.CallbackExecutor;
@@ -3721,7 +3722,8 @@ public class ConnectivityManager {
printStackTrace(); printStackTrace();
checkCallbackNotNull(callback); checkCallbackNotNull(callback);
Preconditions.checkArgument( Preconditions.checkArgument(
reqType == TRACK_DEFAULT || need != null, "null NetworkCapabilities"); reqType == TRACK_DEFAULT || reqType == TRACK_SYSTEM_DEFAULT || need != null,
"null NetworkCapabilities");
final NetworkRequest request; final NetworkRequest request;
final String callingPackageName = mContext.getOpPackageName(); final String callingPackageName = mContext.getOpPackageName();
try { try {
@@ -4192,8 +4194,9 @@ public class ConnectivityManager {
} }
/** /**
* Registers to receive notifications about changes in the system default network. The callbacks * Registers to receive notifications about changes in the application's default network. This
* will continue to be called until either the application exits or * may be a physical network or a virtual network, such as a VPN that applies to the
* application. The callbacks will continue to be called until either the application exits or
* {@link #unregisterNetworkCallback(NetworkCallback)} is called. * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
* *
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
@@ -4206,7 +4209,7 @@ public class ConnectivityManager {
* {@link #unregisterNetworkCallback(NetworkCallback)}. * {@link #unregisterNetworkCallback(NetworkCallback)}.
* *
* @param networkCallback The {@link NetworkCallback} that the system will call as the * @param networkCallback The {@link NetworkCallback} that the system will call as the
* system default network changes. * application's default network changes.
* The callback is invoked on the default internal Handler. * The callback is invoked on the default internal Handler.
* @throws RuntimeException if the app already has too many callbacks registered. * @throws RuntimeException if the app already has too many callbacks registered.
*/ */
@@ -4215,11 +4218,47 @@ public class ConnectivityManager {
registerDefaultNetworkCallback(networkCallback, getDefaultHandler()); registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
} }
/**
* Registers to receive notifications about changes in the application's default network. This
* may be a physical network or a virtual network, such as a VPN that applies to the
* application. The callbacks will continue to be called until either the application exits or
* {@link #unregisterNetworkCallback(NetworkCallback)} is called.
*
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #requestNetwork} as well as
* {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}.
* Requesting a network with this method will count toward this limit. If this limit is
* exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources,
* make sure to unregister the callbacks with
* {@link #unregisterNetworkCallback(NetworkCallback)}.
*
* @param networkCallback The {@link NetworkCallback} that the system will call as the
* application's default network changes.
* @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
* @throws RuntimeException if the app already has too many callbacks registered.
*/
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
@NonNull Handler handler) {
CallbackHandler cbHandler = new CallbackHandler(handler);
sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
TRACK_DEFAULT, TYPE_NONE, cbHandler);
}
/** /**
* Registers to receive notifications about changes in the system default network. The callbacks * Registers to receive notifications about changes in the system default network. The callbacks
* will continue to be called until either the application exits or * will continue to be called until either the application exits or
* {@link #unregisterNetworkCallback(NetworkCallback)} is called. * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
* *
* This method should not be used to determine networking state seen by applications, because in
* many cases, most or even all application traffic may not use the default network directly,
* and traffic from different applications may go on different networks by default. As an
* example, if a VPN is connected, traffic from all applications might be sent through the VPN
* and not onto the system default network. Applications or system components desiring to do
* determine network state as seen by applications should use other methods such as
* {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}.
*
* <p>To avoid performance issues due to apps leaking callbacks, the system will limit the * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the
* number of outstanding requests to 100 per app (identified by their UID), shared with * number of outstanding requests to 100 per app (identified by their UID), shared with
* all variants of this method, of {@link #requestNetwork} as well as * all variants of this method, of {@link #requestNetwork} as well as
@@ -4233,20 +4272,19 @@ public class ConnectivityManager {
* system default network changes. * system default network changes.
* @param handler {@link Handler} to specify the thread upon which the callback will be invoked. * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
* @throws RuntimeException if the app already has too many callbacks registered. * @throws RuntimeException if the app already has too many callbacks registered.
*
* @hide
*/ */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @SystemApi(client = MODULE_LIBRARIES)
public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, @SuppressLint({"ExecutorRegistration", "PairedRegistration"})
@RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_SETTINGS})
public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
@NonNull Handler handler) { @NonNull Handler handler) {
// This works because if the NetworkCapabilities are null,
// ConnectivityService takes them from the default request.
//
// Since the capabilities are exactly the same as the default request's
// capabilities, this request is guaranteed, at all times, to be
// satisfied by the same network, if any, that satisfies the default
// request, i.e., the system default network.
CallbackHandler cbHandler = new CallbackHandler(handler); CallbackHandler cbHandler = new CallbackHandler(handler);
sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0, sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
TRACK_DEFAULT, TYPE_NONE, cbHandler); TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler);
} }
/** /**

View File

@@ -762,12 +762,14 @@ public final class NetworkCapabilities implements Parcelable {
final int originalSignalStrength = mSignalStrength; final int originalSignalStrength = mSignalStrength;
final int originalOwnerUid = getOwnerUid(); final int originalOwnerUid = getOwnerUid();
final int[] originalAdministratorUids = getAdministratorUids(); final int[] originalAdministratorUids = getAdministratorUids();
final TransportInfo originalTransportInfo = getTransportInfo();
clearAll(); clearAll();
mTransportTypes = (originalTransportTypes & TEST_NETWORKS_ALLOWED_TRANSPORTS) mTransportTypes = (originalTransportTypes & TEST_NETWORKS_ALLOWED_TRANSPORTS)
| (1 << TRANSPORT_TEST); | (1 << TRANSPORT_TEST);
mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES; mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES;
mNetworkSpecifier = originalSpecifier; mNetworkSpecifier = originalSpecifier;
mSignalStrength = originalSignalStrength; mSignalStrength = originalSignalStrength;
mTransportInfo = originalTransportInfo;
// Only retain the owner and administrator UIDs if they match the app registering the remote // Only retain the owner and administrator UIDs if they match the app registering the remote
// caller that registered the network. // caller that registered the network.

View File

@@ -104,17 +104,14 @@ public class NetworkRequest implements Parcelable {
* callbacks about the single, highest scoring current network * callbacks about the single, highest scoring current network
* (if any) that matches the specified NetworkCapabilities, or * (if any) that matches the specified NetworkCapabilities, or
* *
* - TRACK_DEFAULT, a hybrid of the two designed such that the * - TRACK_DEFAULT, which causes the framework to issue callbacks for
* framework will issue callbacks for the single, highest scoring * the single, highest scoring current network (if any) that will
* current network (if any) that matches the capabilities of the * be chosen for an app, but which cannot cause the framework to
* default Internet request (mDefaultRequest), but which cannot cause * either create or retain the existence of any specific network.
* the framework to either create or retain the existence of any *
* specific network. Note that from the point of view of the request * - TRACK_SYSTEM_DEFAULT, which causes the framework to send callbacks
* matching code, TRACK_DEFAULT is identical to REQUEST: its special * for the network (if any) that satisfies the default Internet
* behaviour is not due to different semantics, but to the fact that * request.
* the system will only ever create a TRACK_DEFAULT with capabilities
* that are identical to the default request's capabilities, thus
* causing it to share fate in every way with the default request.
* *
* - BACKGROUND_REQUEST, like REQUEST but does not cause any networks * - BACKGROUND_REQUEST, like REQUEST but does not cause any networks
* to retain the NET_CAPABILITY_FOREGROUND capability. A network with * to retain the NET_CAPABILITY_FOREGROUND capability. A network with
@@ -137,6 +134,7 @@ public class NetworkRequest implements Parcelable {
TRACK_DEFAULT, TRACK_DEFAULT,
REQUEST, REQUEST,
BACKGROUND_REQUEST, BACKGROUND_REQUEST,
TRACK_SYSTEM_DEFAULT,
}; };
/** /**
@@ -601,6 +599,8 @@ public class NetworkRequest implements Parcelable {
return NetworkRequestProto.TYPE_REQUEST; return NetworkRequestProto.TYPE_REQUEST;
case BACKGROUND_REQUEST: case BACKGROUND_REQUEST:
return NetworkRequestProto.TYPE_BACKGROUND_REQUEST; return NetworkRequestProto.TYPE_BACKGROUND_REQUEST;
case TRACK_SYSTEM_DEFAULT:
return NetworkRequestProto.TYPE_TRACK_SYSTEM_DEFAULT;
default: default:
return NetworkRequestProto.TYPE_UNKNOWN; return NetworkRequestProto.TYPE_UNKNOWN;
} }

View File

@@ -55,13 +55,29 @@ import java.security.GeneralSecurityException;
public class VpnManager { public class VpnManager {
/** Type representing a lack of VPN @hide */ /** Type representing a lack of VPN @hide */
public static final int TYPE_VPN_NONE = -1; public static final int TYPE_VPN_NONE = -1;
/** VPN service type code @hide */
/**
* A VPN created by an app using the {@link VpnService} API.
* @hide
*/
public static final int TYPE_VPN_SERVICE = 1; public static final int TYPE_VPN_SERVICE = 1;
/** Platform VPN type code @hide */
/**
* A VPN created using a {@link VpnManager} API such as {@link #startProvisionedVpnProfile}.
* @hide
*/
public static final int TYPE_VPN_PLATFORM = 2; public static final int TYPE_VPN_PLATFORM = 2;
/**
* An IPsec VPN created by the built-in LegacyVpnRunner.
* @deprecated new Android devices should use VPN_TYPE_PLATFORM instead.
* @hide
*/
@Deprecated
public static final int TYPE_VPN_LEGACY = 3;
/** @hide */ /** @hide */
@IntDef(value = {TYPE_VPN_NONE, TYPE_VPN_SERVICE, TYPE_VPN_PLATFORM}) @IntDef(value = {TYPE_VPN_NONE, TYPE_VPN_SERVICE, TYPE_VPN_PLATFORM, TYPE_VPN_LEGACY})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface VpnType {} public @interface VpnType {}