diff options
author | Paul Jensen <pauljensen@google.com> | 2015-07-24 15:57:11 -0400 |
---|---|---|
committer | Paul Jensen <pauljensen@google.com> | 2015-08-04 07:24:24 -0400 |
commit | 487ffe7d3d84bf65212158f7098e8a84b5b55e09 (patch) | |
tree | 38057c7c55e92aaa1ccd9c2f74705fde6fb645b0 /core/java/android/net | |
parent | 532737df65330200dc55ae42d31140d19c4024be (diff) | |
download | frameworks_base-487ffe7d3d84bf65212158f7098e8a84b5b55e09.zip frameworks_base-487ffe7d3d84bf65212158f7098e8a84b5b55e09.tar.gz frameworks_base-487ffe7d3d84bf65212158f7098e8a84b5b55e09.tar.bz2 |
Fix NOT_RESTRICTED network capability and enforce it.
With this change:
1. NOT_RESTRICTED should be removed from NetworkRequests that bring up
special restricted carrier networks (e.g. IMS, FOTA).
2. NetworkRequests without NOT_RESTRICTED require CONNECTIVITY_INTERNAL
permission to register
3. Binding sockets to networks without NOT_RESTRICTED requires
CONNECTIVITY_INTERNAL permission
Bug:21637535
Change-Id: I5991d39facaa6b690e969fe15dcbeec52e918321
Diffstat (limited to 'core/java/android/net')
-rw-r--r-- | core/java/android/net/ConnectivityManager.java | 39 | ||||
-rw-r--r-- | core/java/android/net/NetworkCapabilities.java | 43 | ||||
-rw-r--r-- | core/java/android/net/NetworkRequest.java | 8 |
3 files changed, 48 insertions, 42 deletions
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index dc8ff8f..4d45076 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -956,41 +956,6 @@ public class ConnectivityManager { return 1; } - /** - * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given - * NetworkCapabilities object if all the capabilities it provides are - * typically provided by restricted networks. - * - * TODO: consider: - * - Moving to NetworkCapabilities - * - Renaming it to guessRestrictedCapability and make it set the - * restricted capability bit in addition to clearing it. - * @hide - */ - public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) { - for (int capability : nc.getCapabilities()) { - switch (capability) { - case NetworkCapabilities.NET_CAPABILITY_CBS: - case NetworkCapabilities.NET_CAPABILITY_DUN: - case NetworkCapabilities.NET_CAPABILITY_EIMS: - case NetworkCapabilities.NET_CAPABILITY_FOTA: - case NetworkCapabilities.NET_CAPABILITY_IA: - case NetworkCapabilities.NET_CAPABILITY_IMS: - case NetworkCapabilities.NET_CAPABILITY_RCS: - case NetworkCapabilities.NET_CAPABILITY_XCAP: - case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default - continue; - default: - // At least one capability usually provided by unrestricted - // networks. Conclude that this network is unrestricted. - return; - } - } - // All the capabilities are typically provided by restricted networks. - // Conclude that this network is restricted. - nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); - } - private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { if (networkType == TYPE_MOBILE) { int cap = -1; @@ -1013,14 +978,14 @@ public class ConnectivityManager { } NetworkCapabilities netCap = new NetworkCapabilities(); netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap); - maybeMarkCapabilitiesRestricted(netCap); + netCap.maybeMarkCapabilitiesRestricted(); return netCap; } else if (networkType == TYPE_WIFI) { if ("p2p".equals(feature)) { NetworkCapabilities netCap = new NetworkCapabilities(); netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); - maybeMarkCapabilitiesRestricted(netCap); + netCap.maybeMarkCapabilitiesRestricted(); return netCap; } } diff --git a/core/java/android/net/NetworkCapabilities.java b/core/java/android/net/NetworkCapabilities.java index 514d24a..0154eb5 100644 --- a/core/java/android/net/NetworkCapabilities.java +++ b/core/java/android/net/NetworkCapabilities.java @@ -38,10 +38,7 @@ public final class NetworkCapabilities implements Parcelable { */ public NetworkCapabilities() { clearAll(); - mNetworkCapabilities = - (1 << NET_CAPABILITY_NOT_RESTRICTED) | - (1 << NET_CAPABILITY_TRUSTED) | - (1 << NET_CAPABILITY_NOT_VPN); + mNetworkCapabilities = DEFAULT_CAPABILITIES; } public NetworkCapabilities(NetworkCapabilities nc) { @@ -187,6 +184,28 @@ public final class NetworkCapabilities implements Parcelable { private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL; /** + * Capabilities that are set by default when the object is constructed. + */ + private static final long DEFAULT_CAPABILITIES = + (1 << NET_CAPABILITY_NOT_RESTRICTED) | + (1 << NET_CAPABILITY_TRUSTED) | + (1 << NET_CAPABILITY_NOT_VPN); + + /** + * Capabilities that suggest that a network is restricted. + * {@see #maybeMarkCapabilitiesRestricted}. + */ + private static final long RESTRICTED_CAPABILITIES = + (1 << NET_CAPABILITY_CBS) | + (1 << NET_CAPABILITY_DUN) | + (1 << NET_CAPABILITY_EIMS) | + (1 << NET_CAPABILITY_FOTA) | + (1 << NET_CAPABILITY_IA) | + (1 << NET_CAPABILITY_IMS) | + (1 << NET_CAPABILITY_RCS) | + (1 << NET_CAPABILITY_XCAP); + + /** * Adds the given capability to this {@code NetworkCapability} instance. * Multiple capabilities may be applied sequentially. Note that when searching * for a network to satisfy a request, all capabilities requested must be satisfied. @@ -269,6 +288,22 @@ public final class NetworkCapabilities implements Parcelable { } /** + * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are + * typically provided by restricted networks. + * + * TODO: consider: + * - Renaming it to guessRestrictedCapability and make it set the + * restricted capability bit in addition to clearing it. + * @hide + */ + public void maybeMarkCapabilitiesRestricted() { + // If all the capabilities are typically provided by restricted networks, conclude that this + // network is restricted. + if ((mNetworkCapabilities & ~(DEFAULT_CAPABILITIES | RESTRICTED_CAPABILITIES)) == 0) + removeCapability(NET_CAPABILITY_NOT_RESTRICTED); + } + + /** * Representing the transport type. Apps should generally not care about transport. A * request for a fast internet connection could be satisfied by a number of different * transports. If any are specified here it will be satisfied a Network that matches diff --git a/core/java/android/net/NetworkRequest.java b/core/java/android/net/NetworkRequest.java index e61594c..e184ec4 100644 --- a/core/java/android/net/NetworkRequest.java +++ b/core/java/android/net/NetworkRequest.java @@ -83,7 +83,13 @@ public class NetworkRequest implements Parcelable { * Build {@link NetworkRequest} give the current set of capabilities. */ public NetworkRequest build() { - return new NetworkRequest(mNetworkCapabilities, ConnectivityManager.TYPE_NONE, + // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED + // when later an unrestricted capability could be added to mNetworkCapabilities, in + // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which + // maybeMarkCapabilitiesRestricted() doesn't add back. + final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities); + nc.maybeMarkCapabilitiesRestricted(); + return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE, ConnectivityManager.REQUEST_ID_UNSET); } |