From 696a214258c5615c75cd350892beade730816ef5 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Wed, 8 Jul 2015 12:49:04 +0900 Subject: [PATCH 1/5] Make immutable NetworkCapabilities more explicit. Bug: 21405941 Change-Id: Iafd738c31747b0f5f9356bed1c97f5f282830af1 --- .../java/android/net/NetworkCapabilities.java | 122 ++++++++++++++++-- .../android/server/ConnectivityService.java | 21 +-- 2 files changed, 119 insertions(+), 24 deletions(-) diff --git a/core/java/android/net/NetworkCapabilities.java b/core/java/android/net/NetworkCapabilities.java index 658051c9eb..d69d00fdbe 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) { @@ -185,6 +182,36 @@ public final class NetworkCapabilities implements Parcelable { private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS; private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL; + /** + * Network capabilities that are expected to be mutable, i.e., can change while a particular + * network is connected. + */ + private static final long MUTABLE_CAPABILITIES = + // TRUSTED can change when user explicitly connects to an untrusted network in Settings. + // http://b/18206275 + (1 << NET_CAPABILITY_TRUSTED) | + (1 << NET_CAPABILITY_VALIDATED) | + (1 << NET_CAPABILITY_CAPTIVE_PORTAL); + + /** + * Network capabilities that are not allowed in NetworkRequests. This exists because the + * NetworkFactory / NetworkAgent model does not deal well with the situation where a + * capability's presence cannot be known in advance. If such a capability is requested, then we + * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then + * get immediately torn down because they do not have the requested capability. + */ + private static final long NON_REQUESTABLE_CAPABILITIES = + (1 << NET_CAPABILITY_VALIDATED) | + (1 << 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); + /** * Adds the given capability to this {@code NetworkCapability} instance. * Multiple capabilities may be applied sequentially. Note that when searching @@ -258,8 +285,30 @@ public final class NetworkCapabilities implements Parcelable { this.mNetworkCapabilities |= nc.mNetworkCapabilities; } - private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) { - return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities); + /** + * Convenience function that returns a human-readable description of the first mutable + * capability we find. Used to present an error message to apps that request mutable + * capabilities. + * + * @hide + */ + public String describeFirstNonRequestableCapability() { + if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED"; + if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL"; + // This cannot happen unless the preceding checks are incomplete. + if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) { + return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities); + } + if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth"; + return null; + } + + private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { + long networkCapabilities = this.mNetworkCapabilities; + if (onlyImmutable) { + networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES; + } + return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities); } /** @hide */ @@ -267,6 +316,11 @@ public final class NetworkCapabilities implements Parcelable { return (nc.mNetworkCapabilities == this.mNetworkCapabilities); } + private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) { + return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) == + (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES)); + } + /** * 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 @@ -516,7 +570,7 @@ public final class NetworkCapabilities implements Parcelable { /** * Combine a set of Capabilities to this one. Useful for coming up with the complete set - * {@hide} + * @hide */ public void combineCapabilities(NetworkCapabilities nc) { combineNetCapabilities(nc); @@ -526,15 +580,55 @@ public final class NetworkCapabilities implements Parcelable { } /** - * Check if our requirements are satisfied by the given Capabilities. - * {@hide} + * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. + * + * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. + * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link + * bandwidth, signal strength, or validation / captive portal status. + * + * @hide + */ + private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { + return (nc != null && + satisfiedByNetCapabilities(nc, onlyImmutable) && + satisfiedByTransportTypes(nc) && + (onlyImmutable || satisfiedByLinkBandwidths(nc)) && + satisfiedBySpecifier(nc)); + } + + /** + * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. + * + * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. + * + * @hide */ public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) { - return (nc != null && - satisfiedByNetCapabilities(nc) && - satisfiedByTransportTypes(nc) && - satisfiedByLinkBandwidths(nc) && - satisfiedBySpecifier(nc)); + return satisfiedByNetworkCapabilities(nc, false); + } + + /** + * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}. + * + * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. + * + * @hide + */ + public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) { + return satisfiedByNetworkCapabilities(nc, true); + } + + /** + * Checks that our immutable capabilities are the same as those of the given + * {@code NetworkCapabilities}. + * + * @hide + */ + public boolean equalImmutableCapabilities(NetworkCapabilities nc) { + if (nc == null) return false; + return (equalsNetCapabilitiesImmutable(nc) && + equalsTransportTypes(nc) && + equalsSpecifier(nc)); } @Override diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 7f124dc254..6c199cb066 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -1903,6 +1903,11 @@ public class ConnectivityService extends IConnectivityManager.Stub networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED)) { Slog.wtf(TAG, "BUG: " + nai + " has stateful capability."); } + if (nai.created && !nai.networkCapabilities.equalImmutableCapabilities( + networkCapabilities)) { + Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: " + + nai.networkCapabilities + " -> " + networkCapabilities); + } updateCapabilities(nai, networkCapabilities, NascentState.NOT_JUST_VALIDATED); } @@ -3615,14 +3620,10 @@ public class ConnectivityService extends IConnectivityManager.Stub } } - private void ensureImmutableCapabilities(NetworkCapabilities networkCapabilities) { - if (networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED)) { - throw new IllegalArgumentException( - "Cannot request network with NET_CAPABILITY_VALIDATED"); - } - if (networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) { - throw new IllegalArgumentException( - "Cannot request network with NET_CAPABILITY_CAPTIVE_PORTAL"); + private void ensureRequestableCapabilities(NetworkCapabilities networkCapabilities) { + final String badCapability = networkCapabilities.describeFirstNonRequestableCapability(); + if (badCapability != null) { + throw new IllegalArgumentException("Cannot request network with " + badCapability); } } @@ -3632,7 +3633,7 @@ public class ConnectivityService extends IConnectivityManager.Stub networkCapabilities = new NetworkCapabilities(networkCapabilities); enforceNetworkRequestPermissions(networkCapabilities); enforceMeteredApnPolicy(networkCapabilities); - ensureImmutableCapabilities(networkCapabilities); + ensureRequestableCapabilities(networkCapabilities); if (timeoutMs < 0 || timeoutMs > ConnectivityManager.MAX_NETWORK_REQUEST_TIMEOUT_MS) { throw new IllegalArgumentException("Bad timeout specified"); @@ -3701,7 +3702,7 @@ public class ConnectivityService extends IConnectivityManager.Stub networkCapabilities = new NetworkCapabilities(networkCapabilities); enforceNetworkRequestPermissions(networkCapabilities); enforceMeteredApnPolicy(networkCapabilities); - ensureImmutableCapabilities(networkCapabilities); + ensureRequestableCapabilities(networkCapabilities); NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, nextNetworkRequestId()); From 493910ac44339b7b02f5c536bb181fdc38de400b Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Mon, 15 Jun 2015 14:29:22 +0900 Subject: [PATCH 2/5] ConnectivityManager API for for packet keepalives. Bug: 21405946 Change-Id: Ie1f8f8bee684fe2bb1092a9f1bc9f5dc29b1defc --- .../java/android/net/ConnectivityManager.java | 136 +++++++ .../android/net/IConnectivityManager.aidl | 5 + core/java/android/net/NetworkAgent.java | 90 ++++- .../android/server/ConnectivityService.java | 57 ++- .../connectivity/KeepalivePacketData.java | 136 +++++++ .../server/connectivity/KeepaliveTracker.java | 372 ++++++++++++++++++ 6 files changed, 790 insertions(+), 6 deletions(-) create mode 100644 services/core/java/com/android/server/connectivity/KeepalivePacketData.java create mode 100644 services/core/java/com/android/server/connectivity/KeepaliveTracker.java diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 12cd5f1721..f75c995cf5 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -1206,6 +1206,142 @@ public class ConnectivityManager { return true; } + /** @hide */ + public static class PacketKeepaliveCallback { + /** The requested keepalive was successfully started. */ + public void onStarted() {} + /** The keepalive was successfully stopped. */ + public void onStopped() {} + /** An error occurred. */ + public void onError(int error) {} + } + + /** + * Allows applications to request that the system periodically send specific packets on their + * behalf, using hardware offload to save battery power. + * + * To request that the system send keepalives, call one of the methods that return a + * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, + * passing in a non-null callback. If the callback is successfully started, the callback's + * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, + * specifying one of the {@code ERROR_*} constants in this class. + * + * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if + * the operation was successfull or {@code onError} if an error occurred. + * + * @hide + */ + public class PacketKeepalive { + + private static final String TAG = "PacketKeepalive"; + + /** @hide */ + public static final int SUCCESS = 0; + + /** @hide */ + public static final int NO_KEEPALIVE = -1; + + /** @hide */ + public static final int BINDER_DIED = -10; + + /** The specified {@code Network} is not connected. */ + public static final int ERROR_INVALID_NETWORK = -20; + /** The specified IP addresses are invalid. For example, the specified source IP address is + * not configured on the specified {@code Network}. */ + public static final int ERROR_INVALID_IP_ADDRESS = -21; + /** The requested port is invalid. */ + public static final int ERROR_INVALID_PORT = -22; + /** The packet length is invalid (e.g., too long). */ + public static final int ERROR_INVALID_LENGTH = -23; + /** The packet transmission interval is invalid (e.g., too short). */ + public static final int ERROR_INVALID_INTERVAL = -24; + + /** The hardware does not support this request. */ + public static final int ERROR_HARDWARE_UNSUPPORTED = -30; + + public static final int NATT_PORT = 4500; + + private final Network mNetwork; + private final PacketKeepaliveCallback mCallback; + private final Looper mLooper; + private final Messenger mMessenger; + + private volatile Integer mSlot; + + void stopLooper() { + mLooper.quit(); + } + + public void stop() { + try { + mService.stopKeepalive(mNetwork, mSlot); + } catch (RemoteException e) { + Log.e(TAG, "Error stopping packet keepalive: ", e); + stopLooper(); + } + } + + private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { + checkNotNull(network, "network cannot be null"); + checkNotNull(callback, "callback cannot be null"); + mNetwork = network; + mCallback = callback; + HandlerThread thread = new HandlerThread(TAG); + thread.start(); + mLooper = thread.getLooper(); + mMessenger = new Messenger(new Handler(mLooper) { + @Override + public void handleMessage(Message message) { + switch (message.what) { + case NetworkAgent.EVENT_PACKET_KEEPALIVE: + int error = message.arg2; + try { + if (error == SUCCESS) { + if (mSlot == null) { + mSlot = message.arg1; + mCallback.onStarted(); + } else { + mSlot = null; + stopLooper(); + mCallback.onStopped(); + } + } else { + stopLooper(); + mCallback.onError(error); + } + } catch (Exception e) { + Log.e(TAG, "Exception in keepalive callback(" + error + ")", e); + } + break; + default: + Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what)); + break; + } + } + }); + } + } + + /** + * Starts an IPsec NAT-T keepalive packet with the specified parameters. + * + * @hide + */ + public PacketKeepalive startNattKeepalive( + Network network, int intervalSeconds, PacketKeepaliveCallback callback, + InetAddress srcAddr, int srcPort, InetAddress dstAddr) { + final PacketKeepalive k = new PacketKeepalive(network, callback); + try { + mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(), + srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); + } catch (RemoteException e) { + Log.e(TAG, "Error starting packet keepalive: ", e); + k.stopLooper(); + return null; + } + return k; + } + /** * Ensure that a network route exists to deliver traffic to the specified * host via the specified network interface. An attempt to add a route that diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl index 29557bb661..48dc2bbf94 100644 --- a/core/java/android/net/IConnectivityManager.aidl +++ b/core/java/android/net/IConnectivityManager.aidl @@ -162,4 +162,9 @@ interface IConnectivityManager boolean setUnderlyingNetworksForVpn(in Network[] networks); void factoryReset(); + + void startNattKeepalive(in Network network, int intervalSeconds, in Messenger messenger, + in IBinder binder, String srcAddr, int srcPort, String dstAddr); + + void stopKeepalive(in Network network, int slot); } diff --git a/core/java/android/net/NetworkAgent.java b/core/java/android/net/NetworkAgent.java index e6fc1ea293..85a584a8ca 100644 --- a/core/java/android/net/NetworkAgent.java +++ b/core/java/android/net/NetworkAgent.java @@ -25,6 +25,7 @@ import android.util.Log; import com.android.internal.util.AsyncChannel; import com.android.internal.util.Protocol; +import android.net.ConnectivityManager.PacketKeepalive; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicBoolean; @@ -143,11 +144,46 @@ public abstract class NetworkAgent extends Handler { */ public static final int CMD_SAVE_ACCEPT_UNVALIDATED = BASE + 9; - /** Sent by ConnectivityService to the NetworkAgent to inform the agent to pull + /** + * Sent by ConnectivityService to the NetworkAgent to inform the agent to pull * the underlying network connection for updated bandwidth information. */ public static final int CMD_REQUEST_BANDWIDTH_UPDATE = BASE + 10; + /** + * Sent by ConnectivityService to the NetworkAgent to request that the specified packet be sent + * periodically on the given interval. + * + * arg1 = the slot number of the keepalive to start + * arg2 = interval in seconds + * obj = KeepalivePacketData object describing the data to be sent + * + * Also used internally by ConnectivityService / KeepaliveTracker, with different semantics. + */ + public static final int CMD_START_PACKET_KEEPALIVE = BASE + 11; + + /** + * Requests that the specified keepalive packet be stopped. + * + * arg1 = slot number of the keepalive to stop. + * + * Also used internally by ConnectivityService / KeepaliveTracker, with different semantics. + */ + public static final int CMD_STOP_PACKET_KEEPALIVE = BASE + 12; + + /** + * Sent by the NetworkAgent to ConnectivityService to provide status on a packet keepalive + * request. This may either be the reply to a CMD_START_PACKET_KEEPALIVE, or an asynchronous + * error notification. + * + * This is also sent by KeepaliveTracker to the app's ConnectivityManager.PacketKeepalive to + * so that the app's PacketKeepaliveCallback methods can be called. + * + * arg1 = slot number of the keepalive + * arg2 = error code + */ + public static final int EVENT_PACKET_KEEPALIVE = BASE + 13; + public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, NetworkCapabilities nc, LinkProperties lp, int score) { this(looper, context, logTag, ni, nc, lp, score, null); @@ -240,18 +276,41 @@ public abstract class NetworkAgent extends Handler { } case CMD_SAVE_ACCEPT_UNVALIDATED: { saveAcceptUnvalidated(msg.arg1 != 0); + break; + } + case CMD_START_PACKET_KEEPALIVE: { + startPacketKeepalive(msg); + break; + } + case CMD_STOP_PACKET_KEEPALIVE: { + stopPacketKeepalive(msg); + break; } } } private void queueOrSendMessage(int what, Object obj) { + queueOrSendMessage(what, 0, 0, obj); + } + + private void queueOrSendMessage(int what, int arg1, int arg2) { + queueOrSendMessage(what, arg1, arg2, null); + } + + private void queueOrSendMessage(int what, int arg1, int arg2, Object obj) { + Message msg = Message.obtain(); + msg.what = what; + msg.arg1 = arg1; + msg.arg2 = arg2; + msg.obj = obj; + queueOrSendMessage(msg); + } + + private void queueOrSendMessage(Message msg) { synchronized (mPreConnectedQueue) { if (mAsyncChannel != null) { - mAsyncChannel.sendMessage(what, obj); + mAsyncChannel.sendMessage(msg); } else { - Message msg = Message.obtain(); - msg.what = what; - msg.obj = obj; mPreConnectedQueue.add(msg); } } @@ -365,6 +424,27 @@ public abstract class NetworkAgent extends Handler { protected void saveAcceptUnvalidated(boolean accept) { } + /** + * Requests that the network hardware send the specified packet at the specified interval. + */ + protected void startPacketKeepalive(Message msg) { + onPacketKeepaliveEvent(msg.arg1, PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED); + } + + /** + * Requests that the network hardware send the specified packet at the specified interval. + */ + protected void stopPacketKeepalive(Message msg) { + onPacketKeepaliveEvent(msg.arg1, PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED); + } + + /** + * Called by the network when a packet keepalive event occurs. + */ + public void onPacketKeepaliveEvent(int slot, int reason) { + queueOrSendMessage(EVENT_PACKET_KEEPALIVE, slot, reason); + } + protected void log(String s) { Log.d(LOG_TAG, "NetworkAgent: " + s); } diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 6c199cb066..e71bd19f3f 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -47,6 +47,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.database.ContentObserver; import android.net.ConnectivityManager; +import android.net.ConnectivityManager.PacketKeepalive; import android.net.IConnectivityManager; import android.net.INetworkManagementEventObserver; import android.net.INetworkPolicyListener; @@ -117,6 +118,7 @@ import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.XmlUtils; import com.android.server.am.BatteryStatsService; import com.android.server.connectivity.DataConnectionStats; +import com.android.server.connectivity.KeepaliveTracker; import com.android.server.connectivity.NetworkDiagnostics; import com.android.server.connectivity.Nat464Xlat; import com.android.server.connectivity.NetworkAgentInfo; @@ -409,6 +411,8 @@ public class ConnectivityService extends IConnectivityManager.Stub TelephonyManager mTelephonyManager; + private KeepaliveTracker mKeepaliveTracker; + // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp private final static int MIN_NET_ID = 100; // some reserved marks private final static int MAX_NET_ID = 65535; @@ -750,6 +754,8 @@ public class ConnectivityService extends IConnectivityManager.Stub mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); + + mKeepaliveTracker = new KeepaliveTracker(mHandler); } private NetworkRequest createInternetRequestForTransport(int transportType) { @@ -1435,6 +1441,10 @@ public class ConnectivityService extends IConnectivityManager.Stub "ConnectivityService"); } + private void enforceKeepalivePermission() { + mContext.enforceCallingPermission(KeepaliveTracker.PERMISSION, "ConnectivityService"); + } + public void sendConnectedBroadcast(NetworkInfo info) { enforceConnectivityInternalPermission(); sendGeneralBroadcast(info, CONNECTIVITY_ACTION); @@ -1826,10 +1836,13 @@ public class ConnectivityService extends IConnectivityManager.Stub pw.println(", last requested never"); } } - pw.println(); + pw.println(); mTethering.dump(fd, pw, args); + pw.println(); + mKeepaliveTracker.dump(pw); + if (mInetLog != null && mInetLog.size() > 0) { pw.println(); pw.println("Inet condition reports:"); @@ -1991,6 +2004,15 @@ public class ConnectivityService extends IConnectivityManager.Stub nai.networkMisc.acceptUnvalidated = (boolean) msg.obj; break; } + case NetworkAgent.EVENT_PACKET_KEEPALIVE: { + NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); + if (nai == null) { + loge("EVENT_PACKET_KEEPALIVE from unknown NetworkAgent"); + break; + } + mKeepaliveTracker.handleEventPacketKeepalive(nai, msg); + break; + } case NetworkMonitor.EVENT_NETWORK_TESTED: { NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; if (isLiveNetworkAgent(nai, "EVENT_NETWORK_TESTED")) { @@ -2127,6 +2149,8 @@ public class ConnectivityService extends IConnectivityManager.Stub } notifyIfacesChanged(); notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); + mKeepaliveTracker.handleStopAllKeepalives(nai, + ConnectivityManager.PacketKeepalive.ERROR_INVALID_NETWORK); nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_DISCONNECTED); mNetworkAgentInfos.remove(msg.replyTo); updateClat(null, nai.linkProperties, nai); @@ -2565,6 +2589,19 @@ public class ConnectivityService extends IConnectivityManager.Stub handleMobileDataAlwaysOn(); break; } + // Sent by KeepaliveTracker to process an app request on the state machine thread. + case NetworkAgent.CMD_START_PACKET_KEEPALIVE: { + mKeepaliveTracker.handleStartKeepalive(msg); + break; + } + // Sent by KeepaliveTracker to process an app request on the state machine thread. + case NetworkAgent.CMD_STOP_PACKET_KEEPALIVE: { + NetworkAgentInfo nai = getNetworkAgentInfoForNetwork((Network) msg.obj); + int slot = msg.arg1; + int reason = msg.arg2; + mKeepaliveTracker.handleStopKeepalive(nai, slot, reason); + break; + } case EVENT_SYSTEM_READY: { for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { nai.networkMonitor.systemReady = true; @@ -3928,6 +3965,8 @@ public class ConnectivityService extends IConnectivityManager.Stub notifyIfacesChanged(); notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); } + + mKeepaliveTracker.handleCheckKeepalivesStillValid(networkAgent); } private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo nai) { @@ -4725,6 +4764,22 @@ public class ConnectivityService extends IConnectivityManager.Stub return success; } + @Override + public void startNattKeepalive(Network network, int intervalSeconds, Messenger messenger, + IBinder binder, String srcAddr, int srcPort, String dstAddr) { + enforceKeepalivePermission(); + mKeepaliveTracker.startNattKeepalive( + getNetworkAgentInfoForNetwork(network), + intervalSeconds, messenger, binder, + srcAddr, srcPort, dstAddr, ConnectivityManager.PacketKeepalive.NATT_PORT); + } + + @Override + public void stopKeepalive(Network network, int slot) { + mHandler.sendMessage(mHandler.obtainMessage( + NetworkAgent.CMD_STOP_PACKET_KEEPALIVE, slot, PacketKeepalive.SUCCESS, network)); + } + @Override public void factoryReset() { enforceConnectivityInternalPermission(); diff --git a/services/core/java/com/android/server/connectivity/KeepalivePacketData.java b/services/core/java/com/android/server/connectivity/KeepalivePacketData.java new file mode 100644 index 0000000000..64b9399dcc --- /dev/null +++ b/services/core/java/com/android/server/connectivity/KeepalivePacketData.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2015 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 com.android.server.connectivity; + +import android.system.OsConstants; +import android.net.ConnectivityManager; +import android.net.NetworkUtils; +import android.net.util.IpUtils; + +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import static android.net.ConnectivityManager.PacketKeepalive.*; + +/** + * Represents the actual packets that are sent by the + * {@link android.net.ConnectivityManager.PacketKeepalive} API. + * + * @hide + */ +public class KeepalivePacketData { + /** Protocol of the packet to send; one of the OsConstants.ETH_P_* values. */ + public final int protocol; + + /** Source IP address */ + public final InetAddress srcAddress; + + /** Destination IP address */ + public final InetAddress dstAddress; + + /** Source port */ + public final int srcPort; + + /** Destination port */ + public final int dstPort; + + /** Destination MAC address. Can change if routing changes. */ + public byte[] dstMac; + + /** Packet data. A raw byte string of packet data, not including the link-layer header. */ + public final byte[] data; + + private static final int IPV4_HEADER_LENGTH = 20; + private static final int UDP_HEADER_LENGTH = 8; + + protected KeepalivePacketData(InetAddress srcAddress, int srcPort, + InetAddress dstAddress, int dstPort, byte[] data) throws InvalidPacketException { + this.srcAddress = srcAddress; + this.dstAddress = dstAddress; + this.srcPort = srcPort; + this.dstPort = dstPort; + this.data = data; + + // Check we have two IP addresses of the same family. + if (srcAddress == null || dstAddress == null || + !srcAddress.getClass().getName().equals(dstAddress.getClass().getName())) { + } + + // Set the protocol. + if (this.dstAddress instanceof Inet4Address) { + this.protocol = OsConstants.ETH_P_IP; + } else if (this.dstAddress instanceof Inet6Address) { + this.protocol = OsConstants.ETH_P_IPV6; + } else { + throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); + } + + // Check the ports. + if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) { + throw new InvalidPacketException(ERROR_INVALID_PORT); + } + } + + public static class InvalidPacketException extends Exception { + final public int error; + public InvalidPacketException(int error) { + this.error = error; + } + } + + /** + * Creates an IPsec NAT-T keepalive packet with the specified parameters. + */ + public static KeepalivePacketData nattKeepalivePacket( + InetAddress srcAddress, int srcPort, + InetAddress dstAddress, int dstPort) throws InvalidPacketException { + + if (!(srcAddress instanceof Inet4Address)) { + throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); + } + + if (dstPort != NATT_PORT) { + throw new InvalidPacketException(ERROR_INVALID_PORT); + } + + int length = IPV4_HEADER_LENGTH + UDP_HEADER_LENGTH + 1; + ByteBuffer buf = ByteBuffer.allocate(length); + buf.order(ByteOrder.BIG_ENDIAN); + buf.putShort((short) 0x4500); // IP version and TOS + buf.putShort((short) length); + buf.putInt(0); // ID, flags, offset + buf.put((byte) 64); // TTL + buf.put((byte) OsConstants.IPPROTO_UDP); + int ipChecksumOffset = buf.position(); + buf.putShort((short) 0); // IP checksum + buf.put(srcAddress.getAddress()); + buf.put(dstAddress.getAddress()); + buf.putShort((short) srcPort); + buf.putShort((short) dstPort); + buf.putShort((short) (length - 20)); // UDP length + int udpChecksumOffset = buf.position(); + buf.putShort((short) 0); // UDP checksum + buf.put((byte) 0xff); // NAT-T keepalive + buf.putShort(ipChecksumOffset, IpUtils.ipChecksum(buf, 0)); + buf.putShort(udpChecksumOffset, IpUtils.udpChecksum(buf, 0, IPV4_HEADER_LENGTH)); + + return new KeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, buf.array()); + } +} diff --git a/services/core/java/com/android/server/connectivity/KeepaliveTracker.java b/services/core/java/com/android/server/connectivity/KeepaliveTracker.java new file mode 100644 index 0000000000..c78f347a46 --- /dev/null +++ b/services/core/java/com/android/server/connectivity/KeepaliveTracker.java @@ -0,0 +1,372 @@ +/* + * Copyright (C) 2015 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 com.android.server.connectivity; + +import com.android.internal.util.HexDump; +import com.android.internal.util.IndentingPrintWriter; +import com.android.server.connectivity.KeepalivePacketData; +import com.android.server.connectivity.NetworkAgentInfo; +import android.net.ConnectivityManager; +import android.net.ConnectivityManager.PacketKeepalive; +import android.net.LinkAddress; +import android.net.NetworkAgent; +import android.net.NetworkUtils; +import android.net.util.IpUtils; +import android.os.Binder; +import android.os.IBinder; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; +import android.os.Process; +import android.os.RemoteException; +import android.system.OsConstants; +import android.util.Log; +import android.util.Pair; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.HashMap; + +import static android.net.ConnectivityManager.PacketKeepalive.*; +import static android.net.NetworkAgent.CMD_START_PACKET_KEEPALIVE; +import static android.net.NetworkAgent.CMD_STOP_PACKET_KEEPALIVE; +import static android.net.NetworkAgent.EVENT_PACKET_KEEPALIVE; + +/** + * Manages packet keepalive requests. + * + * Provides methods to stop and start keepalive requests, and keeps track of keepalives across all + * networks. This class is tightly coupled to ConnectivityService. It is not thread-safe and its + * methods must be called only from the ConnectivityService handler thread. + */ +public class KeepaliveTracker { + + private static final String TAG = "KeepaliveTracker"; + private static final boolean DBG = true; + + // TODO: Change this to a system-only permission. + public static final String PERMISSION = android.Manifest.permission.CHANGE_NETWORK_STATE; + + /** Keeps track of keepalive requests. */ + private final HashMap > mKeepalives = + new HashMap<> (); + private final Handler mConnectivityServiceHandler; + + public KeepaliveTracker(Handler handler) { + mConnectivityServiceHandler = handler; + } + + /** + * Tracks information about a packet keepalive. + * + * All information about this keepalive is known at construction time except the slot number, + * which is only returned when the hardware has successfully started the keepalive. + */ + class KeepaliveInfo implements IBinder.DeathRecipient { + // Bookkeping data. + private final Messenger mMessenger; + private final IBinder mBinder; + private final int mUid; + private final int mPid; + private final NetworkAgentInfo mNai; + + /** Keepalive slot. A small integer that identifies this keepalive among the ones handled + * by this network. */ + private int mSlot = PacketKeepalive.NO_KEEPALIVE; + + // Packet data. + private final KeepalivePacketData mPacket; + private final int mInterval; + + // Whether the keepalive is started or not. + public boolean isStarted; + + public KeepaliveInfo(Messenger messenger, IBinder binder, NetworkAgentInfo nai, + KeepalivePacketData packet, int interval) { + mMessenger = messenger; + mBinder = binder; + mPid = Binder.getCallingPid(); + mUid = Binder.getCallingUid(); + + mNai = nai; + mPacket = packet; + mInterval = interval; + + try { + mBinder.linkToDeath(this, 0); + } catch (RemoteException e) { + binderDied(); + } + } + + public NetworkAgentInfo getNai() { + return mNai; + } + + public String toString() { + return new StringBuffer("KeepaliveInfo [") + .append(" network=").append(mNai.network) + .append(" isStarted=").append(isStarted) + .append(" ") + .append(IpUtils.addressAndPortToString(mPacket.srcAddress, mPacket.srcPort)) + .append("->") + .append(IpUtils.addressAndPortToString(mPacket.dstAddress, mPacket.dstPort)) + .append(" interval=" + mInterval) + .append(" data=" + HexDump.toHexString(mPacket.data)) + .append(" uid=").append(mUid).append(" pid=").append(mPid) + .append(" ]") + .toString(); + } + + /** Sends a message back to the application via its PacketKeepalive.Callback. */ + void notifyMessenger(int slot, int err) { + KeepaliveTracker.this.notifyMessenger(mMessenger, slot, err); + } + + /** Called when the application process is killed. */ + public void binderDied() { + // Not called from ConnectivityService handler thread, so send it a message. + mConnectivityServiceHandler.obtainMessage( + NetworkAgent.CMD_STOP_PACKET_KEEPALIVE, + mSlot, PacketKeepalive.BINDER_DIED, mNai.network).sendToTarget(); + } + + void unlinkDeathRecipient() { + if (mBinder != null) { + mBinder.unlinkToDeath(this, 0); + } + } + + private int checkNetworkConnected() { + if (!mNai.networkInfo.isConnectedOrConnecting()) { + return ERROR_INVALID_NETWORK; + } + return SUCCESS; + } + + private int checkSourceAddress() { + // Check that we have the source address. + for (InetAddress address : mNai.linkProperties.getAddresses()) { + if (address.equals(mPacket.srcAddress)) { + return SUCCESS; + } + } + return ERROR_INVALID_IP_ADDRESS; + } + + private int checkInterval() { + return mInterval >= 20 ? SUCCESS : ERROR_INVALID_INTERVAL; + } + + private int isValid() { + synchronized (mNai) { + int error = checkInterval(); + if (error == SUCCESS) error = checkNetworkConnected(); + if (error == SUCCESS) error = checkSourceAddress(); + return error; + } + } + + void start(int slot) { + int error = isValid(); + if (error == SUCCESS) { + mSlot = slot; + Log.d(TAG, "Starting keepalive " + mSlot + " on " + mNai.name()); + mNai.asyncChannel.sendMessage(CMD_START_PACKET_KEEPALIVE, slot, mInterval, mPacket); + } else { + notifyMessenger(NO_KEEPALIVE, error); + return; + } + } + + void stop(int reason) { + int uid = Binder.getCallingUid(); + if (uid != mUid && uid != Process.SYSTEM_UID) { + if (DBG) { + Log.e(TAG, "Cannot stop unowned keepalive " + mSlot + " on " + mNai.network); + } + } + if (isStarted) { + Log.d(TAG, "Stopping keepalive " + mSlot + " on " + mNai.name()); + mNai.asyncChannel.sendMessage(CMD_STOP_PACKET_KEEPALIVE, mSlot); + } + notifyMessenger(mSlot, reason); + unlinkDeathRecipient(); + } + } + + void notifyMessenger(Messenger messenger, int slot, int err) { + Message message = Message.obtain(); + message.what = EVENT_PACKET_KEEPALIVE; + message.arg1 = slot; + message.arg2 = err; + message.obj = null; + try { + messenger.send(message); + } catch (RemoteException e) { + // Process died? + } + } + + private int findFirstFreeSlot(NetworkAgentInfo nai) { + HashMap networkKeepalives = mKeepalives.get(nai); + if (networkKeepalives == null) { + networkKeepalives = new HashMap(); + mKeepalives.put(nai, networkKeepalives); + } + + // Find the lowest-numbered free slot. + int slot; + for (slot = 0; slot < networkKeepalives.size(); slot++) { + if (networkKeepalives.get(slot) == null) { + return slot; + } + } + // No free slot, pick one at the end. + + // HACK for broadcom hardware that does not support slot 0! + if (slot == 0) slot = 1; + return slot; + } + + public void handleStartKeepalive(Message message) { + KeepaliveInfo ki = (KeepaliveInfo) message.obj; + NetworkAgentInfo nai = ki.getNai(); + int slot = findFirstFreeSlot(nai); + mKeepalives.get(nai).put(slot, ki); + ki.start(slot); + } + + public void handleStopAllKeepalives(NetworkAgentInfo nai, int reason) { + HashMap networkKeepalives = mKeepalives.get(nai); + if (networkKeepalives != null) { + for (KeepaliveInfo ki : networkKeepalives.values()) { + ki.stop(reason); + } + networkKeepalives.clear(); + mKeepalives.remove(nai); + } + } + + public void handleStopKeepalive(NetworkAgentInfo nai, int slot, int reason) { + HashMap networkKeepalives = mKeepalives.get(nai); + if (networkKeepalives == null) { + Log.e(TAG, "Attempt to stop keepalive on nonexistent network " + nai.name()); + return; + } + KeepaliveInfo ki = networkKeepalives.get(slot); + if (ki == null) { + Log.e(TAG, "Attempt to stop nonexistent keepalive " + slot + " on " + nai.name()); + return; + } + ki.stop(reason); + networkKeepalives.remove(slot); + if (networkKeepalives.isEmpty()) { + mKeepalives.remove(nai); + } + } + + public void handleCheckKeepalivesStillValid(NetworkAgentInfo nai) { + HashMap networkKeepalives = mKeepalives.get(nai); + if (networkKeepalives != null) { + ArrayList> invalidKeepalives = new ArrayList<>(); + for (int slot : networkKeepalives.keySet()) { + int error = networkKeepalives.get(slot).isValid(); + if (error != SUCCESS) { + invalidKeepalives.add(Pair.create(slot, error)); + } + } + for (Pair slotAndError: invalidKeepalives) { + handleStopKeepalive(nai, slotAndError.first, slotAndError.second); + } + } + } + + public void handleEventPacketKeepalive(NetworkAgentInfo nai, Message message) { + int slot = message.arg1; + int reason = message.arg2; + + KeepaliveInfo ki = null; + try { + ki = mKeepalives.get(nai).get(slot); + } catch(NullPointerException e) {} + if (ki == null) { + Log.e(TAG, "Event for unknown keepalive " + slot + " on " + nai.name()); + return; + } + + if (reason == SUCCESS && !ki.isStarted) { + // Keepalive successfully started. + if (DBG) Log.d(TAG, "Started keepalive " + slot + " on " + nai.name()); + ki.isStarted = true; + ki.notifyMessenger(slot, reason); + } else { + // Keepalive successfully stopped, or error. + ki.isStarted = false; + if (reason == SUCCESS) { + if (DBG) Log.d(TAG, "Successfully stopped keepalive " + slot + " on " + nai.name()); + } else { + if (DBG) Log.d(TAG, "Keepalive " + slot + " on " + nai.name() + " error " + reason); + } + handleStopKeepalive(nai, slot, reason); + } + } + + public void startNattKeepalive(NetworkAgentInfo nai, int intervalSeconds, Messenger messenger, + IBinder binder, String srcAddrString, int srcPort, String dstAddrString, int dstPort) { + InetAddress srcAddress, dstAddress; + try { + srcAddress = NetworkUtils.numericToInetAddress(srcAddrString); + dstAddress = NetworkUtils.numericToInetAddress(dstAddrString); + } catch (IllegalArgumentException e) { + notifyMessenger(messenger, NO_KEEPALIVE, ERROR_INVALID_IP_ADDRESS); + return; + } + + KeepalivePacketData packet; + try { + packet = KeepalivePacketData.nattKeepalivePacket( + srcAddress, srcPort, dstAddress, NATT_PORT); + } catch (KeepalivePacketData.InvalidPacketException e) { + notifyMessenger(messenger, NO_KEEPALIVE, e.error); + return; + } + KeepaliveInfo ki = new KeepaliveInfo(messenger, binder, nai, packet, intervalSeconds); + Log.d(TAG, "Created keepalive: " + ki.toString()); + mConnectivityServiceHandler.obtainMessage( + NetworkAgent.CMD_START_PACKET_KEEPALIVE, ki).sendToTarget(); + } + + public void dump(IndentingPrintWriter pw) { + pw.println("Packet keepalives:"); + pw.increaseIndent(); + for (NetworkAgentInfo nai : mKeepalives.keySet()) { + pw.println(nai.name()); + pw.increaseIndent(); + for (int slot : mKeepalives.get(nai).keySet()) { + KeepaliveInfo ki = mKeepalives.get(nai).get(slot); + pw.println(slot + ": " + ki.toString()); + } + pw.decreaseIndent(); + } + pw.decreaseIndent(); + } +} From 98a4c4d25b551c8f84786c8cbdfec50e32a64658 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Mon, 6 Jul 2015 23:50:27 +0900 Subject: [PATCH 3/5] Support NetworkCallbacks based on signal strength. Bug: 21405941 Change-Id: I2ed8a5aeb8dac464a4305671ed22abcacb485bc9 --- core/java/android/net/NetworkAgent.java | 20 +++++ .../java/android/net/NetworkCapabilities.java | 80 ++++++++++++++++++- core/java/android/net/NetworkRequest.java | 18 +++++ .../android/server/ConnectivityService.java | 50 +++++++++++- .../server/connectivity/NetworkAgentInfo.java | 6 ++ 5 files changed, 170 insertions(+), 4 deletions(-) diff --git a/core/java/android/net/NetworkAgent.java b/core/java/android/net/NetworkAgent.java index 85a584a8ca..0af6e7c698 100644 --- a/core/java/android/net/NetworkAgent.java +++ b/core/java/android/net/NetworkAgent.java @@ -184,6 +184,14 @@ public abstract class NetworkAgent extends Handler { */ public static final int EVENT_PACKET_KEEPALIVE = BASE + 13; + /** + * Sent by ConnectivityService to inform this network transport of signal strength thresholds + * that when crossed should trigger a system wakeup and a NetworkCapabilities update. + * + * obj = int[] describing signal strength thresholds. + */ + public static final int CMD_SET_SIGNAL_STRENGTH_THRESHOLDS = BASE + 14; + public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, NetworkCapabilities nc, LinkProperties lp, int score) { this(looper, context, logTag, ni, nc, lp, score, null); @@ -286,6 +294,11 @@ public abstract class NetworkAgent extends Handler { stopPacketKeepalive(msg); break; } + + case CMD_SET_SIGNAL_STRENGTH_THRESHOLDS: { + setSignalStrengthThresholds((int[]) msg.obj); + break; + } } } @@ -445,6 +458,13 @@ public abstract class NetworkAgent extends Handler { queueOrSendMessage(EVENT_PACKET_KEEPALIVE, slot, reason); } + /** + * Called by ConnectivityService to inform this network transport of signal strength thresholds + * that when crossed should trigger a system wakeup and a NetworkCapabilities update. + */ + protected void setSignalStrengthThresholds(int[] thresholds) { + } + protected void log(String s) { Log.d(LOG_TAG, "NetworkAgent: " + s); } diff --git a/core/java/android/net/NetworkCapabilities.java b/core/java/android/net/NetworkCapabilities.java index d69d00fdbe..847b77726e 100644 --- a/core/java/android/net/NetworkCapabilities.java +++ b/core/java/android/net/NetworkCapabilities.java @@ -48,6 +48,7 @@ public final class NetworkCapabilities implements Parcelable { mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps; mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps; mNetworkSpecifier = nc.mNetworkSpecifier; + mSignalStrength = nc.mSignalStrength; } } @@ -60,6 +61,7 @@ public final class NetworkCapabilities implements Parcelable { mNetworkCapabilities = mTransportTypes = 0; mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0; mNetworkSpecifier = null; + mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED; } /** @@ -300,6 +302,7 @@ public final class NetworkCapabilities implements Parcelable { return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities); } if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth"; + if (hasSignalStrength()) return "signalStrength"; return null; } @@ -568,6 +571,68 @@ public final class NetworkCapabilities implements Parcelable { } } + /** + * Magic value that indicates no signal strength provided. A request specifying this value is + * always satisfied. + * + * @hide + */ + public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE; + + /** + * Signal strength. This is a signed integer, and higher values indicate better signal. + * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI. + */ + private int mSignalStrength; + + /** + * Sets the signal strength. This is a signed integer, with higher values indicating a stronger + * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units + * reported by WifiManager. + *

+ * Note that when used to register a network callback, this specifies the minimum acceptable + * signal strength. When received as the state of an existing network it specifies the current + * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no + * effect when requesting a callback. + * + * @param signalStrength the bearer-specific signal strength. + * @hide + */ + public void setSignalStrength(int signalStrength) { + mSignalStrength = signalStrength; + } + + /** + * Returns {@code true} if this object specifies a signal strength. + * + * @hide + */ + public boolean hasSignalStrength() { + return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED; + } + + /** + * Retrieves the signal strength. + * + * @return The bearer-specific signal strength. + * @hide + */ + public int getSignalStrength() { + return mSignalStrength; + } + + private void combineSignalStrength(NetworkCapabilities nc) { + this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength); + } + + private boolean satisfiedBySignalStrength(NetworkCapabilities nc) { + return this.mSignalStrength <= nc.mSignalStrength; + } + + private boolean equalsSignalStrength(NetworkCapabilities nc) { + return this.mSignalStrength == nc.mSignalStrength; + } + /** * Combine a set of Capabilities to this one. Useful for coming up with the complete set * @hide @@ -577,6 +642,7 @@ public final class NetworkCapabilities implements Parcelable { combineTransportTypes(nc); combineLinkBandwidths(nc); combineSpecifiers(nc); + combineSignalStrength(nc); } /** @@ -593,7 +659,8 @@ public final class NetworkCapabilities implements Parcelable { satisfiedByNetCapabilities(nc, onlyImmutable) && satisfiedByTransportTypes(nc) && (onlyImmutable || satisfiedByLinkBandwidths(nc)) && - satisfiedBySpecifier(nc)); + satisfiedBySpecifier(nc) && + (onlyImmutable || satisfiedBySignalStrength(nc))); } /** @@ -638,6 +705,7 @@ public final class NetworkCapabilities implements Parcelable { return (equalsNetCapabilities(that) && equalsTransportTypes(that) && equalsLinkBandwidths(that) && + equalsSignalStrength(that) && equalsSpecifier(that)); } @@ -649,7 +717,8 @@ public final class NetworkCapabilities implements Parcelable { ((int)(mTransportTypes >> 32) * 7) + (mLinkUpBandwidthKbps * 11) + (mLinkDownBandwidthKbps * 13) + - (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17)); + (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17) + + (mSignalStrength * 19)); } @Override @@ -663,7 +732,9 @@ public final class NetworkCapabilities implements Parcelable { dest.writeInt(mLinkUpBandwidthKbps); dest.writeInt(mLinkDownBandwidthKbps); dest.writeString(mNetworkSpecifier); + dest.writeInt(mSignalStrength); } + public static final Creator CREATOR = new Creator() { @Override @@ -675,6 +746,7 @@ public final class NetworkCapabilities implements Parcelable { netCap.mLinkUpBandwidthKbps = in.readInt(); netCap.mLinkDownBandwidthKbps = in.readInt(); netCap.mNetworkSpecifier = in.readString(); + netCap.mSignalStrength = in.readInt(); return netCap; } @Override @@ -731,6 +803,8 @@ public final class NetworkCapabilities implements Parcelable { String specifier = (mNetworkSpecifier == null ? "" : " Specifier: <" + mNetworkSpecifier + ">"); - return "[" + transports + capabilities + upBand + dnBand + specifier + "]"; + String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : ""); + + return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]"; } } diff --git a/core/java/android/net/NetworkRequest.java b/core/java/android/net/NetworkRequest.java index e61594ce8b..4f570dcc0d 100644 --- a/core/java/android/net/NetworkRequest.java +++ b/core/java/android/net/NetworkRequest.java @@ -185,6 +185,24 @@ public class NetworkRequest implements Parcelable { mNetworkCapabilities.setNetworkSpecifier(networkSpecifier); return this; } + + /** + * Sets the signal strength. This is a signed integer, with higher values indicating a + * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same + * RSSI units reported by WifiManager. + *

+ * Note that when used to register a network callback, this specifies the minimum acceptable + * signal strength. When received as the state of an existing network it specifies the + * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when + * received and has no effect when requesting a callback. + * + * @param signalStrength the bearer-specific signal strength. + * @hide + */ + public Builder setSignalStrength(int signalStrength) { + mNetworkCapabilities.setSignalStrength(signalStrength); + return this; + } } // implement the Parcelable interface diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index e71bd19f3f..c35e4cb46a 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -150,6 +150,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -1914,7 +1916,7 @@ public class ConnectivityService extends IConnectivityManager.Stub (NetworkCapabilities)msg.obj; if (networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL) || networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED)) { - Slog.wtf(TAG, "BUG: " + nai + " has stateful capability."); + Slog.wtf(TAG, "BUG: " + nai + " has CS-managed capability."); } if (nai.created && !nai.networkCapabilities.equalImmutableCapabilities( networkCapabilities)) { @@ -2274,6 +2276,9 @@ public class ConnectivityService extends IConnectivityManager.Stub bestNetwork = network; } } + if (!nri.isRequest && network.satisfiesImmutableCapabilitiesOf(nri.request)) { + updateSignalStrengthThresholds(network); + } } if (bestNetwork != null) { if (DBG) log("using " + bestNetwork.name()); @@ -2419,6 +2424,9 @@ public class ConnectivityService extends IConnectivityManager.Stub // if this listen request applies and remove it. for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { nai.networkRequests.remove(nri.request.requestId); + if (nai.satisfiesImmutableCapabilitiesOf(nri.request)) { + updateSignalStrengthThresholds(nai); + } } } callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED); @@ -3664,6 +3672,34 @@ public class ConnectivityService extends IConnectivityManager.Stub } } + private int[] getSignalStrengthThresholds(NetworkAgentInfo nai) { + final SortedSet thresholds = new TreeSet(); + synchronized (nai) { + for (NetworkRequestInfo nri : mNetworkRequests.values()) { + if (nri.request.networkCapabilities.hasSignalStrength() && + nai.satisfiesImmutableCapabilitiesOf(nri.request)) { + thresholds.add(nri.request.networkCapabilities.getSignalStrength()); + } + } + } + // We can't just do something like: + // return thresholds.toArray(new int[thresholds.size()]); + // because autoboxing does not work for primitive arrays. + final int[] out = new int[thresholds.size()]; + int pos = 0; + for (Integer threshold : thresholds) { + out[pos] = threshold; + pos++; + } + return out; + } + + private void updateSignalStrengthThresholds(NetworkAgentInfo nai) { + nai.asyncChannel.sendMessage( + android.net.NetworkAgent.CMD_SET_SIGNAL_STRENGTH_THRESHOLDS, + 0, 0, getSignalStrengthThresholds(nai)); + } + @Override public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { @@ -4132,6 +4168,9 @@ public class ConnectivityService extends IConnectivityManager.Stub networkAgent.networkCapabilities = networkCapabilities; } rematchAllNetworksAndRequests(networkAgent, networkAgent.getCurrentScore(), nascent); + // TODO: reduce the number of callbacks where possible. For example, only send signal + // strength changes if the NetworkRequest used to register the callback specified a + // signalStrength. notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_CAP_CHANGED); } } @@ -4594,6 +4633,15 @@ public class ConnectivityService extends IConnectivityManager.Stub // TODO: support proxy per network. } + // Whether a particular NetworkRequest listen should cause signal strength thresholds to + // be communicated to a particular NetworkAgent depends only on the network's immutable, + // capabilities, so it only needs to be done once on initial connect, not every time the + // network's capabilities change. Note that we do this before rematching the network, + // so we could decide to tear it down immediately afterwards. That's fine though - on + // disconnection NetworkAgents should stop any signal strength monitoring they have been + // doing. + updateSignalStrengthThresholds(networkAgent); + // Consider network even though it is not yet validated. rematchNetworkAndRequests(networkAgent, NascentState.NOT_JUST_VALIDATED, ReapUnvalidatedNetworks.REAP); diff --git a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java index 51c6628d1f..b4fea378eb 100644 --- a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java +++ b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java @@ -128,6 +128,12 @@ public class NetworkAgentInfo { request.networkCapabilities.satisfiedByNetworkCapabilities(networkCapabilities); } + public boolean satisfiesImmutableCapabilitiesOf(NetworkRequest request) { + return created && + request.networkCapabilities.satisfiedByImmutableNetworkCapabilities( + networkCapabilities); + } + public boolean isVPN() { return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN); } From 57fc627c323838e7f5ca73a4e933e63e0594187d Mon Sep 17 00:00:00 2001 From: Erik Kline Date: Mon, 13 Jul 2015 16:37:51 +0900 Subject: [PATCH 4/5] Pass signal strength thresholds inside a Bundle Bug: 21407651 Change-Id: I2c80e89441e2eb15a246cb1fa9347f886cefa80f --- core/java/android/net/NetworkAgent.java | 11 ++++++++++- .../com/android/server/ConnectivityService.java | 17 +++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/core/java/android/net/NetworkAgent.java b/core/java/android/net/NetworkAgent.java index 0af6e7c698..f659c022c1 100644 --- a/core/java/android/net/NetworkAgent.java +++ b/core/java/android/net/NetworkAgent.java @@ -17,6 +17,7 @@ package android.net; import android.content.Context; +import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -296,7 +297,15 @@ public abstract class NetworkAgent extends Handler { } case CMD_SET_SIGNAL_STRENGTH_THRESHOLDS: { - setSignalStrengthThresholds((int[]) msg.obj); + ArrayList thresholds = + ((Bundle) msg.obj).getIntegerArrayList("thresholds"); + // TODO: Change signal strength thresholds API to use an ArrayList + // rather than convert to int[]. + int[] intThresholds = new int[(thresholds != null) ? thresholds.size() : 0]; + for (int i = 0; i < intThresholds.length; i++) { + intThresholds[i] = thresholds.get(i); + } + setSignalStrengthThresholds(intThresholds); break; } } diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index bb472552a0..d486594d13 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -3595,7 +3595,7 @@ public class ConnectivityService extends IConnectivityManager.Stub } } - private int[] getSignalStrengthThresholds(NetworkAgentInfo nai) { + private ArrayList getSignalStrengthThresholds(NetworkAgentInfo nai) { final SortedSet thresholds = new TreeSet(); synchronized (nai) { for (NetworkRequestInfo nri : mNetworkRequests.values()) { @@ -3605,22 +3605,15 @@ public class ConnectivityService extends IConnectivityManager.Stub } } } - // We can't just do something like: - // return thresholds.toArray(new int[thresholds.size()]); - // because autoboxing does not work for primitive arrays. - final int[] out = new int[thresholds.size()]; - int pos = 0; - for (Integer threshold : thresholds) { - out[pos] = threshold; - pos++; - } - return out; + return new ArrayList(thresholds); } private void updateSignalStrengthThresholds(NetworkAgentInfo nai) { + Bundle thresholds = new Bundle(); + thresholds.putIntegerArrayList("thresholds", getSignalStrengthThresholds(nai)); nai.asyncChannel.sendMessage( android.net.NetworkAgent.CMD_SET_SIGNAL_STRENGTH_THRESHOLDS, - 0, 0, getSignalStrengthThresholds(nai)); + 0, 0, thresholds); } @Override From a2957edb0cf076626e2500b01e74f32c5b5bef66 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Wed, 16 Sep 2015 11:00:43 +0900 Subject: [PATCH 5/5] Unbreak build, bring ConnectivityService in sync with mnc-dr-dev The version of ConnectivityService.java in mnc-vt-dev doesn't compile due to the presence of two similar-but-not-identical code blocks in handleRegisterNetworkAgent. The history here is a a little convoluted - this code was originally merged into mnc-vt-dev, then cherry-picked into dr-dev, and from there automerged into mnc-vt-dev, causing conflicts. This latest breakage is likely due to the automerged not detecting a conflict because the code block was subtly different. Attempt to fix this once and for all by making the mnc-vt-dev version of the file identical to the mnc-dr-dev version. Change-Id: I270739b0be6f6358045700494a1b0f25f0b365a3 --- .../java/com/android/server/ConnectivityService.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index aae7234eb4..6190a5ab35 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -2277,13 +2277,6 @@ public class ConnectivityService extends IConnectivityManager.Stub } } rematchAllNetworksAndRequests(null, 0); - if (!nri.isRequest && nri.request.networkCapabilities.hasSignalStrength()) { - for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { - if (network.satisfiesImmutableCapabilitiesOf(nri.request)) { - updateSignalStrengthThresholds(network); - } - } - } if (nri.isRequest && mNetworkForRequestId.get(nri.request.requestId) == null) { sendUpdatedScoreToFactories(nri.request, 0); } @@ -4147,9 +4140,6 @@ public class ConnectivityService extends IConnectivityManager.Stub nai.networkCapabilities = networkCapabilities; } rematchAllNetworksAndRequests(nai, oldScore); - // TODO: reduce the number of callbacks where possible. For example, only send signal - // strength changes if the NetworkRequest used to register the callback specified a - // signalStrength. notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); } }