1 /*
2  * Copyright (C) 2014 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 
17 package android.net;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 import android.os.Messenger;
24 import android.util.Log;
25 
26 import com.android.internal.util.AsyncChannel;
27 import com.android.internal.util.Protocol;
28 
29 import java.util.ArrayList;
30 import java.util.concurrent.atomic.AtomicBoolean;
31 
32 /**
33  * A Utility class for handling for communicating between bearer-specific
34  * code and ConnectivityService.
35  *
36  * A bearer may have more than one NetworkAgent if it can simultaneously
37  * support separate networks (IMS / Internet / MMS Apns on cellular, or
38  * perhaps connections with different SSID or P2P for Wi-Fi).
39  *
40  * @hide
41  */
42 public abstract class NetworkAgent extends Handler {
43     // Guaranteed to be valid (not NETID_UNSET), otherwise registerNetworkAgent() would have thrown
44     // an exception.
45     public final int netId;
46 
47     private volatile AsyncChannel mAsyncChannel;
48     private final String LOG_TAG;
49     private static final boolean DBG = true;
50     private static final boolean VDBG = false;
51     private final Context mContext;
52     private final ArrayList<Message>mPreConnectedQueue = new ArrayList<Message>();
53     private volatile long mLastBwRefreshTime = 0;
54     private static final long BW_REFRESH_MIN_WIN_MS = 500;
55     private boolean mPollLceScheduled = false;
56     private AtomicBoolean mPollLcePending = new AtomicBoolean(false);
57 
58     private static final int BASE = Protocol.BASE_NETWORK_AGENT;
59 
60     /**
61      * Sent by ConnectivityService to the NetworkAgent to inform it of
62      * suspected connectivity problems on its network.  The NetworkAgent
63      * should take steps to verify and correct connectivity.
64      */
65     public static final int CMD_SUSPECT_BAD = BASE;
66 
67     /**
68      * Sent by the NetworkAgent (note the EVENT vs CMD prefix) to
69      * ConnectivityService to pass the current NetworkInfo (connection state).
70      * Sent when the NetworkInfo changes, mainly due to change of state.
71      * obj = NetworkInfo
72      */
73     public static final int EVENT_NETWORK_INFO_CHANGED = BASE + 1;
74 
75     /**
76      * Sent by the NetworkAgent to ConnectivityService to pass the current
77      * NetworkCapabilties.
78      * obj = NetworkCapabilities
79      */
80     public static final int EVENT_NETWORK_CAPABILITIES_CHANGED = BASE + 2;
81 
82     /**
83      * Sent by the NetworkAgent to ConnectivityService to pass the current
84      * NetworkProperties.
85      * obj = NetworkProperties
86      */
87     public static final int EVENT_NETWORK_PROPERTIES_CHANGED = BASE + 3;
88 
89     /* centralize place where base network score, and network score scaling, will be
90      * stored, so as we can consistently compare apple and oranges, or wifi, ethernet and LTE
91      */
92     public static final int WIFI_BASE_SCORE = 60;
93 
94     /**
95      * Sent by the NetworkAgent to ConnectivityService to pass the current
96      * network score.
97      * obj = network score Integer
98      */
99     public static final int EVENT_NETWORK_SCORE_CHANGED = BASE + 4;
100 
101     /**
102      * Sent by the NetworkAgent to ConnectivityService to add new UID ranges
103      * to be forced into this Network.  For VPNs only.
104      * obj = UidRange[] to forward
105      */
106     public static final int EVENT_UID_RANGES_ADDED = BASE + 5;
107 
108     /**
109      * Sent by the NetworkAgent to ConnectivityService to remove UID ranges
110      * from being forced into this Network.  For VPNs only.
111      * obj = UidRange[] to stop forwarding
112      */
113     public static final int EVENT_UID_RANGES_REMOVED = BASE + 6;
114 
115     /**
116      * Sent by ConnectivityService to the NetworkAgent to inform the agent of the
117      * networks status - whether we could use the network or could not, due to
118      * either a bad network configuration (no internet link) or captive portal.
119      *
120      * arg1 = either {@code VALID_NETWORK} or {@code INVALID_NETWORK}
121      */
122     public static final int CMD_REPORT_NETWORK_STATUS = BASE + 7;
123 
124     public static final int VALID_NETWORK = 1;
125     public static final int INVALID_NETWORK = 2;
126 
127      /**
128      * Sent by the NetworkAgent to ConnectivityService to indicate this network was
129      * explicitly selected.  This should be sent before the NetworkInfo is marked
130      * CONNECTED so it can be given special treatment at that time.
131      *
132      * obj = boolean indicating whether to use this network even if unvalidated
133      */
134     public static final int EVENT_SET_EXPLICITLY_SELECTED = BASE + 8;
135 
136     /**
137      * Sent by ConnectivityService to the NetworkAgent to inform the agent of
138      * whether the network should in the future be used even if not validated.
139      * This decision is made by the user, but it is the network transport's
140      * responsibility to remember it.
141      *
142      * arg1 = 1 if true, 0 if false
143      */
144     public static final int CMD_SAVE_ACCEPT_UNVALIDATED = BASE + 9;
145 
146     /** Sent by ConnectivityService to the NetworkAgent to inform the agent to pull
147      * the underlying network connection for updated bandwidth information.
148      */
149     public static final int CMD_REQUEST_BANDWIDTH_UPDATE = BASE + 10;
150 
151     /**
152      * Sent by ConnectivityService to the NeworkAgent to inform the agent to avoid
153      * automatically reconnecting to this network (e.g. via autojoin).  Happens
154      * when user selects "No" option on the "Stay connected?" dialog box.
155      */
156     public static final int CMD_PREVENT_AUTOMATIC_RECONNECT = BASE + 11;
157 
NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, NetworkCapabilities nc, LinkProperties lp, int score)158     public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
159             NetworkCapabilities nc, LinkProperties lp, int score) {
160         this(looper, context, logTag, ni, nc, lp, score, null);
161     }
162 
NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc)163     public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
164             NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc) {
165         super(looper);
166         LOG_TAG = logTag;
167         mContext = context;
168         if (ni == null || nc == null || lp == null) {
169             throw new IllegalArgumentException();
170         }
171 
172         if (VDBG) log("Registering NetworkAgent");
173         ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
174                 Context.CONNECTIVITY_SERVICE);
175         netId = cm.registerNetworkAgent(new Messenger(this), new NetworkInfo(ni),
176                 new LinkProperties(lp), new NetworkCapabilities(nc), score, misc);
177     }
178 
179     @Override
handleMessage(Message msg)180     public void handleMessage(Message msg) {
181         switch (msg.what) {
182             case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
183                 if (mAsyncChannel != null) {
184                     log("Received new connection while already connected!");
185                 } else {
186                     if (VDBG) log("NetworkAgent fully connected");
187                     AsyncChannel ac = new AsyncChannel();
188                     ac.connected(null, this, msg.replyTo);
189                     ac.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
190                             AsyncChannel.STATUS_SUCCESSFUL);
191                     synchronized (mPreConnectedQueue) {
192                         mAsyncChannel = ac;
193                         for (Message m : mPreConnectedQueue) {
194                             ac.sendMessage(m);
195                         }
196                         mPreConnectedQueue.clear();
197                     }
198                 }
199                 break;
200             }
201             case AsyncChannel.CMD_CHANNEL_DISCONNECT: {
202                 if (VDBG) log("CMD_CHANNEL_DISCONNECT");
203                 if (mAsyncChannel != null) mAsyncChannel.disconnect();
204                 break;
205             }
206             case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
207                 if (DBG) log("NetworkAgent channel lost");
208                 // let the client know CS is done with us.
209                 unwanted();
210                 synchronized (mPreConnectedQueue) {
211                     mAsyncChannel = null;
212                 }
213                 break;
214             }
215             case CMD_SUSPECT_BAD: {
216                 log("Unhandled Message " + msg);
217                 break;
218             }
219             case CMD_REQUEST_BANDWIDTH_UPDATE: {
220                 long currentTimeMs = System.currentTimeMillis();
221                 if (VDBG) {
222                     log("CMD_REQUEST_BANDWIDTH_UPDATE request received.");
223                 }
224                 if (currentTimeMs >= (mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS)) {
225                     mPollLceScheduled = false;
226                     if (mPollLcePending.getAndSet(true) == false) {
227                         pollLceData();
228                     }
229                 } else {
230                     // deliver the request at a later time rather than discard it completely.
231                     if (!mPollLceScheduled) {
232                         long waitTime = mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS -
233                                 currentTimeMs + 1;
234                         mPollLceScheduled = sendEmptyMessageDelayed(
235                                 CMD_REQUEST_BANDWIDTH_UPDATE, waitTime);
236                     }
237                 }
238                 break;
239             }
240             case CMD_REPORT_NETWORK_STATUS: {
241                 if (VDBG) {
242                     log("CMD_REPORT_NETWORK_STATUS(" +
243                             (msg.arg1 == VALID_NETWORK ? "VALID)" : "INVALID)"));
244                 }
245                 networkStatus(msg.arg1);
246                 break;
247             }
248             case CMD_SAVE_ACCEPT_UNVALIDATED: {
249                 saveAcceptUnvalidated(msg.arg1 != 0);
250                 break;
251             }
252             case CMD_PREVENT_AUTOMATIC_RECONNECT: {
253                 preventAutomaticReconnect();
254                 break;
255             }
256         }
257     }
258 
queueOrSendMessage(int what, Object obj)259     private void queueOrSendMessage(int what, Object obj) {
260         synchronized (mPreConnectedQueue) {
261             if (mAsyncChannel != null) {
262                 mAsyncChannel.sendMessage(what, obj);
263             } else {
264                 Message msg = Message.obtain();
265                 msg.what = what;
266                 msg.obj = obj;
267                 mPreConnectedQueue.add(msg);
268             }
269         }
270     }
271 
272     /**
273      * Called by the bearer code when it has new LinkProperties data.
274      */
sendLinkProperties(LinkProperties linkProperties)275     public void sendLinkProperties(LinkProperties linkProperties) {
276         queueOrSendMessage(EVENT_NETWORK_PROPERTIES_CHANGED, new LinkProperties(linkProperties));
277     }
278 
279     /**
280      * Called by the bearer code when it has new NetworkInfo data.
281      */
sendNetworkInfo(NetworkInfo networkInfo)282     public void sendNetworkInfo(NetworkInfo networkInfo) {
283         queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, new NetworkInfo(networkInfo));
284     }
285 
286     /**
287      * Called by the bearer code when it has new NetworkCapabilities data.
288      */
sendNetworkCapabilities(NetworkCapabilities networkCapabilities)289     public void sendNetworkCapabilities(NetworkCapabilities networkCapabilities) {
290         mPollLcePending.set(false);
291         mLastBwRefreshTime = System.currentTimeMillis();
292         queueOrSendMessage(EVENT_NETWORK_CAPABILITIES_CHANGED,
293                 new NetworkCapabilities(networkCapabilities));
294     }
295 
296     /**
297      * Called by the bearer code when it has a new score for this network.
298      */
sendNetworkScore(int score)299     public void sendNetworkScore(int score) {
300         if (score < 0) {
301             throw new IllegalArgumentException("Score must be >= 0");
302         }
303         queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, new Integer(score));
304     }
305 
306     /**
307      * Called by the VPN code when it wants to add ranges of UIDs to be routed
308      * through the VPN network.
309      */
addUidRanges(UidRange[] ranges)310     public void addUidRanges(UidRange[] ranges) {
311         queueOrSendMessage(EVENT_UID_RANGES_ADDED, ranges);
312     }
313 
314     /**
315      * Called by the VPN code when it wants to remove ranges of UIDs from being routed
316      * through the VPN network.
317      */
removeUidRanges(UidRange[] ranges)318     public void removeUidRanges(UidRange[] ranges) {
319         queueOrSendMessage(EVENT_UID_RANGES_REMOVED, ranges);
320     }
321 
322     /**
323      * Called by the bearer to indicate this network was manually selected by the user.
324      * This should be called before the NetworkInfo is marked CONNECTED so that this
325      * Network can be given special treatment at that time. If {@code acceptUnvalidated} is
326      * {@code true}, then the system will switch to this network. If it is {@code false} and the
327      * network cannot be validated, the system will ask the user whether to switch to this network.
328      * If the user confirms and selects "don't ask again", then the system will call
329      * {@link #saveAcceptUnvalidated} to persist the user's choice. Thus, if the transport ever
330      * calls this method with {@code acceptUnvalidated} set to {@code false}, it must also implement
331      * {@link #saveAcceptUnvalidated} to respect the user's choice.
332      */
explicitlySelected(boolean acceptUnvalidated)333     public void explicitlySelected(boolean acceptUnvalidated) {
334         queueOrSendMessage(EVENT_SET_EXPLICITLY_SELECTED, acceptUnvalidated);
335     }
336 
337     /**
338      * Called when ConnectivityService has indicated they no longer want this network.
339      * The parent factory should (previously) have received indication of the change
340      * as well, either canceling NetworkRequests or altering their score such that this
341      * network won't be immediately requested again.
342      */
unwanted()343     abstract protected void unwanted();
344 
345     /**
346      * Called when ConnectivityService request a bandwidth update. The parent factory
347      * shall try to overwrite this method and produce a bandwidth update if capable.
348      */
pollLceData()349     protected void pollLceData() {
350     }
351 
352     /**
353      * Called when the system determines the usefulness of this network.
354      *
355      * Networks claiming internet connectivity will have their internet
356      * connectivity verified.
357      *
358      * Currently there are two possible values:
359      * {@code VALID_NETWORK} if the system is happy with the connection,
360      * {@code INVALID_NETWORK} if the system is not happy.
361      * TODO - add indications of captive portal-ness and related success/failure,
362      * ie, CAPTIVE_SUCCESS_NETWORK, CAPTIVE_NETWORK for successful login and detection
363      *
364      * This may be called multiple times as the network status changes and may
365      * generate false negatives if we lose ip connectivity before the link is torn down.
366      */
networkStatus(int status)367     protected void networkStatus(int status) {
368     }
369 
370     /**
371      * Called when the user asks to remember the choice to use this network even if unvalidated.
372      * The transport is responsible for remembering the choice, and the next time the user connects
373      * to the network, should explicitlySelected with {@code acceptUnvalidated} set to {@code true}.
374      * This method will only be called if {@link #explicitlySelected} was called with
375      * {@code acceptUnvalidated} set to {@code false}.
376      */
saveAcceptUnvalidated(boolean accept)377     protected void saveAcceptUnvalidated(boolean accept) {
378     }
379 
380     /**
381      * Called when the user asks to not stay connected to this network because it was found to not
382      * provide Internet access.  Usually followed by call to {@code unwanted}.  The transport is
383      * responsible for making sure the device does not automatically reconnect to the same network
384      * after the {@code unwanted} call.
385      */
preventAutomaticReconnect()386     protected void preventAutomaticReconnect() {
387     }
388 
log(String s)389     protected void log(String s) {
390         Log.d(LOG_TAG, "NetworkAgent: " + s);
391     }
392 }
393