The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 com.android.server; |
| 18 | |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
| 22 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_BLUETOOTH; |
| 24 | import static android.net.ConnectivityManager.TYPE_DUMMY; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 25 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.TYPE_MOBILE_CBS; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 28 | import static android.net.ConnectivityManager.TYPE_MOBILE_FOTA; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 29 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 30 | import static android.net.ConnectivityManager.TYPE_MOBILE_IA; |
| 31 | import static android.net.ConnectivityManager.TYPE_MOBILE_IMS; |
| 32 | import static android.net.ConnectivityManager.TYPE_MOBILE_MMS; |
| 33 | import static android.net.ConnectivityManager.TYPE_MOBILE_SUPL; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 34 | import static android.net.ConnectivityManager.TYPE_NONE; |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 35 | import static android.net.ConnectivityManager.TYPE_PROXY; |
| 36 | import static android.net.ConnectivityManager.TYPE_VPN; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 37 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 38 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
| 39 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 40 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
| 41 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 42 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 43 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 44 | import android.app.AlarmManager; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 45 | import android.app.Notification; |
| 46 | import android.app.NotificationManager; |
| 47 | import android.app.PendingIntent; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 48 | import android.content.ActivityNotFoundException; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 49 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.content.ContentResolver; |
| 51 | import android.content.Context; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 52 | import android.content.ContextWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 54 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.content.pm.PackageManager; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 56 | import android.content.res.Configuration; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 57 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 58 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.net.ConnectivityManager; |
| 60 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 61 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 62 | import android.net.INetworkPolicyListener; |
| 63 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 64 | import android.net.INetworkStatsService; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 65 | import android.net.LinkAddress; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 66 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 67 | import android.net.LinkProperties.CompareResult; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.net.MobileDataStateTracker; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 69 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 70 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 71 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 72 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 74 | import android.net.NetworkInfo.DetailedState; |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 75 | import android.net.NetworkFactory; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 76 | import android.net.NetworkMisc; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 77 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 78 | import android.net.NetworkRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 79 | import android.net.NetworkState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.net.NetworkStateTracker; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 81 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 82 | import android.net.Proxy; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 83 | import android.net.ProxyDataTracker; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 84 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 85 | import android.net.RouteInfo; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 86 | import android.net.SamplingDataTracker; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 87 | import android.net.UidRange; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 88 | import android.net.Uri; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 89 | import android.net.wimax.WimaxManagerConstants; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 90 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.os.Binder; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 92 | import android.os.Build; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 93 | import android.os.Bundle; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 94 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 96 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 97 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 98 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.os.Looper; |
| 100 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 101 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 102 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 103 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 104 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 105 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.ServiceManager; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 107 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 109 | import android.os.UserHandle; |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 110 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 112 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 113 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 114 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 115 | import android.text.TextUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 116 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 117 | import android.util.SparseArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 118 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 119 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 121 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 122 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 123 | import com.android.internal.app.IBatteryStats; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 124 | import com.android.internal.net.LegacyVpnInfo; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 125 | import com.android.internal.net.NetworkStatsFactory; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 126 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 127 | import com.android.internal.net.VpnProfile; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 128 | import com.android.internal.telephony.DctConstants; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 129 | import com.android.internal.telephony.Phone; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 130 | import com.android.internal.telephony.PhoneConstants; |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 131 | import com.android.internal.telephony.TelephonyIntents; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 132 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 133 | import com.android.internal.util.IndentingPrintWriter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 134 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 135 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 136 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 137 | import com.android.server.connectivity.Nat464Xlat; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 138 | import com.android.server.connectivity.NetworkAgentInfo; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 139 | import com.android.server.connectivity.NetworkMonitor; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 140 | import com.android.server.connectivity.PacManager; |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 141 | import com.android.server.connectivity.PermissionMonitor; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 142 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 143 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 144 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 145 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 146 | import com.google.android.collect.Lists; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 147 | import com.google.android.collect.Sets; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 148 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 149 | import dalvik.system.DexClassLoader; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 150 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 151 | import org.xmlpull.v1.XmlPullParser; |
| 152 | import org.xmlpull.v1.XmlPullParserException; |
| 153 | |
| 154 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 156 | import java.io.FileNotFoundException; |
| 157 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 158 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | import java.io.PrintWriter; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 160 | import java.lang.reflect.Constructor; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 161 | import java.net.HttpURLConnection; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 162 | import java.net.Inet4Address; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 163 | import java.net.Inet6Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 164 | import java.net.InetAddress; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 165 | import java.net.URL; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 166 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 167 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 168 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 169 | import java.util.Collection; |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 170 | import java.util.GregorianCalendar; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 171 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 172 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 173 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 174 | import java.util.Map; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 175 | import java.util.Objects; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 176 | import java.util.Random; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 177 | import java.util.concurrent.atomic.AtomicBoolean; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 178 | import java.util.concurrent.atomic.AtomicInteger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 180 | import javax.net.ssl.HostnameVerifier; |
| 181 | import javax.net.ssl.HttpsURLConnection; |
| 182 | import javax.net.ssl.SSLSession; |
| 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | /** |
| 185 | * @hide |
| 186 | */ |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 187 | public class ConnectivityService extends IConnectivityManager.Stub |
| 188 | implements PendingIntent.OnFinished { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 189 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
Robert Greenwalt | a7d31bf | 2014-07-23 12:42:47 -0700 | [diff] [blame] | 191 | private static final boolean DBG = true; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 192 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 194 | // network sampling debugging |
| 195 | private static final boolean SAMPLE_DBG = false; |
| 196 | |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 197 | private static final boolean LOGD_RULES = false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 198 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 199 | // TODO: create better separation between radio types and network types |
| 200 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 201 | // how long to wait before switching back to a radio's default network |
| 202 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 203 | // system property that can override the above value |
| 204 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 205 | "android.telephony.apn-restore"; |
| 206 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 207 | // Default value if FAIL_FAST_TIME_MS is not set |
| 208 | private static final int DEFAULT_FAIL_FAST_TIME_MS = 1 * 60 * 1000; |
| 209 | // system property that can override DEFAULT_FAIL_FAST_TIME_MS |
| 210 | private static final String FAIL_FAST_TIME_MS = |
| 211 | "persist.radio.fail_fast_time_ms"; |
| 212 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 213 | private static final String ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED = |
| 214 | "android.net.ConnectivityService.action.PKT_CNT_SAMPLE_INTERVAL_ELAPSED"; |
| 215 | |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 216 | private static final int SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE = 0; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 217 | |
| 218 | private PendingIntent mSampleIntervalElapsedIntent; |
| 219 | |
| 220 | // Set network sampling interval at 12 minutes, this way, even if the timers get |
| 221 | // aggregated, it will fire at around 15 minutes, which should allow us to |
| 222 | // aggregate this timer with other timers (specially the socket keep alive timers) |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 223 | private static final int DEFAULT_SAMPLING_INTERVAL_IN_SECONDS = (SAMPLE_DBG ? 30 : 12 * 60); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 224 | |
| 225 | // start network sampling a minute after booting ... |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 226 | private static final int DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS = (SAMPLE_DBG ? 30 : 60); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 227 | |
| 228 | AlarmManager mAlarmManager; |
| 229 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 230 | private Tethering mTethering; |
| 231 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 232 | private final PermissionMonitor mPermissionMonitor; |
| 233 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 234 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 235 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 236 | @GuardedBy("mVpns") |
| 237 | private final SparseArray<Vpn> mVpns = new SparseArray<Vpn>(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 238 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 239 | private boolean mLockdownEnabled; |
| 240 | private LockdownVpnTracker mLockdownTracker; |
| 241 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 242 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 243 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 244 | /** Currently active network rules by UID. */ |
| 245 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 246 | /** Set of ifaces that are costly. */ |
| 247 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 248 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | /** |
| 250 | * Sometimes we want to refer to the individual network state |
| 251 | * trackers separately, and sometimes we just want to treat them |
| 252 | * abstractly. |
| 253 | */ |
| 254 | private NetworkStateTracker mNetTrackers[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | private Context mContext; |
| 257 | private int mNetworkPreference; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 258 | // 0 is full bad, 100 is full good |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 259 | private int mDefaultInetConditionPublished = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 261 | private Object mDnsLock = new Object(); |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 262 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | |
| 264 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 265 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 267 | private INetworkManagementService mNetd; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 268 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 269 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 270 | private String mCurrentTcpBufferSizes; |
| 271 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 272 | private static final int ENABLED = 1; |
| 273 | private static final int DISABLED = 0; |
| 274 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 275 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 276 | * used internally to change our mobile data enabled flag |
| 277 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 278 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 279 | |
| 280 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 281 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 282 | * from one net to another. Clear happens when we get a new |
| 283 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 284 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 285 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 286 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 287 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 288 | /** |
| 289 | * used internally to reload global proxy settings |
| 290 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 291 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 292 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 293 | /** |
| 294 | * used internally to set external dependency met/unmet |
| 295 | * arg1 = ENABLED (met) or DISABLED (unmet) |
| 296 | * arg2 = NetworkType |
| 297 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 298 | private static final int EVENT_SET_DEPENDENCY_MET = 10; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 299 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 300 | /** |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 301 | * used internally to send a sticky broadcast delayed. |
| 302 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 303 | private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 11; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 304 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 305 | /** |
| 306 | * Used internally to |
| 307 | * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}. |
| 308 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 309 | private static final int EVENT_SET_POLICY_DATA_ENABLE = 12; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 310 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 311 | /** |
| 312 | * Used internally to disable fail fast of mobile data |
| 313 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 314 | private static final int EVENT_ENABLE_FAIL_FAST_MOBILE_DATA = 14; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 315 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 316 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 317 | * used internally to indicate that data sampling interval is up |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 318 | */ |
| 319 | private static final int EVENT_SAMPLE_INTERVAL_ELAPSED = 15; |
| 320 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 321 | /** |
| 322 | * PAC manager has received new port. |
| 323 | */ |
| 324 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 325 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 326 | /** |
| 327 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 328 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 329 | */ |
| 330 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 331 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 332 | /** |
| 333 | * used internally when registering NetworkAgents |
| 334 | * obj = Messenger |
| 335 | */ |
| 336 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 337 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 338 | /** |
| 339 | * used to add a network request |
| 340 | * includes a NetworkRequestInfo |
| 341 | */ |
| 342 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 343 | |
| 344 | /** |
| 345 | * indicates a timeout period is over - check if we had a network yet or not |
| 346 | * and if not, call the timeout calback (but leave the request live until they |
| 347 | * cancel it. |
| 348 | * includes a NetworkRequestInfo |
| 349 | */ |
| 350 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 351 | |
| 352 | /** |
| 353 | * used to add a network listener - no request |
| 354 | * includes a NetworkRequestInfo |
| 355 | */ |
| 356 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 357 | |
| 358 | /** |
| 359 | * used to remove a network request, either a listener or a real request |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 360 | * arg1 = UID of caller |
| 361 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 362 | */ |
| 363 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 364 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 365 | /** |
| 366 | * used internally when registering NetworkFactories |
| 367 | * obj = Messenger |
| 368 | */ |
| 369 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 370 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 371 | /** |
| 372 | * used internally to expire a wakelock when transitioning |
| 373 | * from one net to another. Expire happens when we fail to find |
| 374 | * a new network (typically after 1 minute) - |
| 375 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 376 | * a replacement network. |
| 377 | */ |
| 378 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 379 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 380 | /** |
| 381 | * Used internally to indicate the system is ready. |
| 382 | */ |
| 383 | private static final int EVENT_SYSTEM_READY = 25; |
| 384 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 385 | /** |
| 386 | * used to add a network request with a pending intent |
| 387 | * includes a NetworkRequestInfo |
| 388 | */ |
| 389 | private static final int EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT = 26; |
| 390 | |
| 391 | /** |
| 392 | * used to remove a pending intent and its associated network request. |
| 393 | * arg1 = UID of caller |
| 394 | * obj = PendingIntent |
| 395 | */ |
| 396 | private static final int EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT = 27; |
| 397 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 398 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 399 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 400 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 401 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 402 | final private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 403 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 404 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 405 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 406 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 407 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 408 | private String mNetTransitionWakeLockCausedBy = ""; |
| 409 | private int mNetTransitionWakeLockSerialNumber; |
| 410 | private int mNetTransitionWakeLockTimeout; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 411 | private final PowerManager.WakeLock mPendingIntentWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 412 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 413 | private InetAddress mDefaultDns; |
| 414 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 415 | // used in DBG mode to track inet condition reports |
| 416 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 417 | private ArrayList mInetLog; |
| 418 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 419 | // track the current default http proxy - tell the world if we get a new one (real change) |
Jason Monk | cf0f97a | 2014-10-02 15:39:38 -0400 | [diff] [blame] | 420 | private volatile ProxyInfo mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 421 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 422 | private boolean mDefaultProxyDisabled = false; |
| 423 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 424 | // track the global proxy. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 425 | private ProxyInfo mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 426 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 427 | private PacManager mPacManager = null; |
| 428 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 429 | private SettingsObserver mSettingsObserver; |
| 430 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 431 | private UserManager mUserManager; |
| 432 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 433 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 434 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 435 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 436 | // the set of network types that can only be enabled by system/sig apps |
| 437 | List mProtectedNetworks; |
| 438 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 439 | private DataConnectionStats mDataConnectionStats; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 440 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 441 | private AtomicInteger mEnableFailFastMobileDataTag = new AtomicInteger(0); |
| 442 | |
| 443 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 444 | |
Sreeram Ramachandran | 8f4d42c | 2014-09-05 16:06:34 -0700 | [diff] [blame] | 445 | // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
| 446 | private final static int MIN_NET_ID = 100; // some reserved marks |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 447 | private final static int MAX_NET_ID = 65535; |
| 448 | private int mNextNetId = MIN_NET_ID; |
| 449 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 450 | // sequence number of NetworkRequests |
| 451 | private int mNextNetworkRequestId = 1; |
| 452 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 453 | /** |
| 454 | * Implements support for the legacy "one network per network type" model. |
| 455 | * |
| 456 | * We used to have a static array of NetworkStateTrackers, one for each |
| 457 | * network type, but that doesn't work any more now that we can have, |
| 458 | * for example, more that one wifi network. This class stores all the |
| 459 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 460 | * API will only see the first one. |
| 461 | * |
| 462 | * It serves two main purposes: |
| 463 | * |
| 464 | * 1. Provide information about "the network for a given type" (since this |
| 465 | * API only supports one). |
| 466 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 467 | * the first network for a given type changes, or if the default network |
| 468 | * changes. |
| 469 | */ |
| 470 | private class LegacyTypeTracker { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 471 | |
| 472 | private static final boolean DBG = true; |
| 473 | private static final boolean VDBG = false; |
| 474 | private static final String TAG = "CSLegacyTypeTracker"; |
| 475 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 476 | /** |
| 477 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 478 | * Each list holds references to all NetworkAgentInfos that are used to |
| 479 | * satisfy requests for that network type. |
| 480 | * |
| 481 | * This array is built out at startup such that an unsupported network |
| 482 | * doesn't get an ArrayList instance, making this a tristate: |
| 483 | * unsupported, supported but not active and active. |
| 484 | * |
| 485 | * The actual lists are populated when we scan the network types that |
| 486 | * are supported on this device. |
| 487 | */ |
| 488 | private ArrayList<NetworkAgentInfo> mTypeLists[]; |
| 489 | |
| 490 | public LegacyTypeTracker() { |
| 491 | mTypeLists = (ArrayList<NetworkAgentInfo>[]) |
| 492 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 493 | } |
| 494 | |
| 495 | public void addSupportedType(int type) { |
| 496 | if (mTypeLists[type] != null) { |
| 497 | throw new IllegalStateException( |
| 498 | "legacy list for type " + type + "already initialized"); |
| 499 | } |
| 500 | mTypeLists[type] = new ArrayList<NetworkAgentInfo>(); |
| 501 | } |
| 502 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 503 | public boolean isTypeSupported(int type) { |
| 504 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 505 | } |
| 506 | |
| 507 | public NetworkAgentInfo getNetworkForType(int type) { |
| 508 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 509 | return mTypeLists[type].get(0); |
| 510 | } else { |
| 511 | return null; |
| 512 | } |
| 513 | } |
| 514 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 515 | private void maybeLogBroadcast(NetworkAgentInfo nai, boolean connected, int type) { |
| 516 | if (DBG) { |
| 517 | log("Sending " + (connected ? "connected" : "disconnected") + |
| 518 | " broadcast for type " + type + " " + nai.name() + |
| 519 | " isDefaultNetwork=" + isDefaultNetwork(nai)); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** Adds the given network to the specified legacy type list. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 524 | public void add(int type, NetworkAgentInfo nai) { |
| 525 | if (!isTypeSupported(type)) { |
| 526 | return; // Invalid network type. |
| 527 | } |
| 528 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 529 | |
| 530 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 531 | if (list.contains(nai)) { |
| 532 | loge("Attempting to register duplicate agent for type " + type + ": " + nai); |
| 533 | return; |
| 534 | } |
| 535 | |
Lorenzo Colitti | 061f415 | 2014-09-28 16:08:06 +0900 | [diff] [blame] | 536 | list.add(nai); |
| 537 | |
| 538 | // Send a broadcast if this is the first network of its type or if it's the default. |
| 539 | if (list.size() == 1 || isDefaultNetwork(nai)) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 540 | maybeLogBroadcast(nai, true, type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 541 | sendLegacyNetworkBroadcast(nai, true, type); |
| 542 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 545 | /** Removes the given network from the specified legacy type list. */ |
| 546 | public void remove(int type, NetworkAgentInfo nai) { |
| 547 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 548 | if (list == null || list.isEmpty()) { |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | boolean wasFirstNetwork = list.get(0).equals(nai); |
| 553 | |
| 554 | if (!list.remove(nai)) { |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | if (wasFirstNetwork || isDefaultNetwork(nai)) { |
| 559 | maybeLogBroadcast(nai, false, type); |
| 560 | sendLegacyNetworkBroadcast(nai, false, type); |
| 561 | } |
| 562 | |
| 563 | if (!list.isEmpty() && wasFirstNetwork) { |
| 564 | if (DBG) log("Other network available for type " + type + |
| 565 | ", sending connected broadcast"); |
| 566 | maybeLogBroadcast(list.get(0), false, type); |
| 567 | sendLegacyNetworkBroadcast(list.get(0), false, type); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /** Removes the given network from all legacy type lists. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 572 | public void remove(NetworkAgentInfo nai) { |
| 573 | if (VDBG) log("Removing agent " + nai); |
| 574 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 575 | remove(type, nai); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 578 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 579 | private String naiToString(NetworkAgentInfo nai) { |
| 580 | String name = (nai != null) ? nai.name() : "null"; |
| 581 | String state = (nai.networkInfo != null) ? |
| 582 | nai.networkInfo.getState() + "/" + nai.networkInfo.getDetailedState() : |
| 583 | "???/???"; |
| 584 | return name + " " + state; |
| 585 | } |
| 586 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 587 | public void dump(IndentingPrintWriter pw) { |
| 588 | for (int type = 0; type < mTypeLists.length; type++) { |
| 589 | if (mTypeLists[type] == null) continue; |
| 590 | pw.print(type + " "); |
| 591 | pw.increaseIndent(); |
| 592 | if (mTypeLists[type].size() == 0) pw.println("none"); |
| 593 | for (NetworkAgentInfo nai : mTypeLists[type]) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 594 | pw.println(naiToString(nai)); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 595 | } |
| 596 | pw.decreaseIndent(); |
| 597 | } |
| 598 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 599 | |
| 600 | // This class needs its own log method because it has a different TAG. |
| 601 | private void log(String s) { |
| 602 | Slog.d(TAG, s); |
| 603 | } |
| 604 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 605 | } |
| 606 | private LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(); |
| 607 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 608 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 609 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 610 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 611 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 612 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 613 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 614 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 615 | mDefaultRequest = new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 616 | NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(), |
| 617 | NetworkRequestInfo.REQUEST); |
| 618 | mNetworkRequests.put(mDefaultRequest, nri); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 619 | |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 620 | HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); |
| 621 | handlerThread.start(); |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 622 | mHandler = new InternalHandler(handlerThread.getLooper()); |
| 623 | mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 624 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 625 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 626 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 627 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 628 | Settings.Secure.ANDROID_ID); |
| 629 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 630 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 631 | SystemProperties.set("net.hostname", name); |
| 632 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 635 | // read our default dns server ip |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 636 | String dns = Settings.Global.getString(context.getContentResolver(), |
| 637 | Settings.Global.DEFAULT_DNS_SERVER); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 638 | if (dns == null || dns.length() == 0) { |
| 639 | dns = context.getResources().getString( |
| 640 | com.android.internal.R.string.config_default_dns_server); |
| 641 | } |
| 642 | try { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 643 | mDefaultDns = NetworkUtils.numericToInetAddress(dns); |
| 644 | } catch (IllegalArgumentException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 645 | loge("Error setting defaultDns using " + dns); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 648 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 649 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 650 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 651 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 652 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 653 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 654 | try { |
| 655 | mPolicyManager.registerListener(mPolicyListener); |
| 656 | } catch (RemoteException e) { |
| 657 | // ouch, no rules updates means some processes may never get network |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 658 | loge("unable to register INetworkPolicyListener" + e.toString()); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 662 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 663 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 664 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 665 | com.android.internal.R.integer.config_networkTransitionTimeout); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 666 | mPendingIntentWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 667 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 668 | mNetTrackers = new NetworkStateTracker[ |
| 669 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 670 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 671 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 672 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 673 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 674 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 675 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 676 | String[] naStrings = context.getResources().getStringArray( |
| 677 | com.android.internal.R.array.networkAttributes); |
| 678 | for (String naString : naStrings) { |
| 679 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 680 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 681 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 682 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 683 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 684 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 685 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 686 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 687 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 688 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 689 | n.type); |
| 690 | continue; |
| 691 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 692 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 693 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 694 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 695 | continue; |
| 696 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 697 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 698 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 699 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 700 | mNetworksDefined++; |
| 701 | } catch(Exception e) { |
| 702 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 705 | |
| 706 | // Forcibly add TYPE_VPN as a supported type, if it has not already been added via config. |
| 707 | if (mNetConfigs[TYPE_VPN] == null) { |
| 708 | // mNetConfigs is used only for "restore time", which isn't applicable to VPNs, so we |
| 709 | // don't need to add TYPE_VPN to mNetConfigs. |
| 710 | mLegacyTypeTracker.addSupportedType(TYPE_VPN); |
| 711 | mNetworksDefined++; // used only in the log() statement below. |
| 712 | } |
| 713 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 714 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 715 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 716 | mProtectedNetworks = new ArrayList<Integer>(); |
| 717 | int[] protectedNetworks = context.getResources().getIntArray( |
| 718 | com.android.internal.R.array.config_protectedNetworks); |
| 719 | for (int p : protectedNetworks) { |
| 720 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 721 | mProtectedNetworks.add(p); |
| 722 | } else { |
| 723 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 724 | } |
| 725 | } |
| 726 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 727 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 728 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 729 | |
Robert Greenwalt | fab50167 | 2014-07-23 11:44:01 -0700 | [diff] [blame] | 730 | mTethering = new Tethering(mContext, mNetd, statsService, mHandler.getLooper()); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 731 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 732 | mPermissionMonitor = new PermissionMonitor(mContext, mNetd); |
| 733 | |
Robert Greenwalt | bfc7634 | 2013-07-19 14:30:49 -0700 | [diff] [blame] | 734 | //set up the listener for user state for creating user VPNs |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 735 | IntentFilter intentFilter = new IntentFilter(); |
| 736 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 737 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
| 738 | mContext.registerReceiverAsUser( |
| 739 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 740 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 741 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 742 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 743 | mNetd.registerObserver(mDataActivityObserver); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 744 | } catch (RemoteException e) { |
| 745 | loge("Error registering observer :" + e); |
| 746 | } |
| 747 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 748 | if (DBG) { |
| 749 | mInetLog = new ArrayList(); |
| 750 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 751 | |
| 752 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 753 | mSettingsObserver.observe(mContext); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 754 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 755 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 756 | mDataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 757 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 758 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 759 | |
| 760 | IntentFilter filter = new IntentFilter(); |
| 761 | filter.addAction(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED); |
| 762 | mContext.registerReceiver( |
| 763 | new BroadcastReceiver() { |
| 764 | @Override |
| 765 | public void onReceive(Context context, Intent intent) { |
| 766 | String action = intent.getAction(); |
| 767 | if (action.equals(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED)) { |
| 768 | mHandler.sendMessage(mHandler.obtainMessage |
| 769 | (EVENT_SAMPLE_INTERVAL_ELAPSED)); |
| 770 | } |
| 771 | } |
| 772 | }, |
| 773 | new IntentFilter(filter)); |
| 774 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 775 | mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 776 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 777 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 779 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 780 | private synchronized int nextNetworkRequestId() { |
| 781 | return mNextNetworkRequestId++; |
| 782 | } |
| 783 | |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 784 | private void assignNextNetId(NetworkAgentInfo nai) { |
| 785 | synchronized (mNetworkForNetId) { |
| 786 | for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) { |
| 787 | int netId = mNextNetId; |
| 788 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 789 | // Make sure NetID unused. http://b/16815182 |
| 790 | if (mNetworkForNetId.get(netId) == null) { |
| 791 | nai.network = new Network(netId); |
| 792 | mNetworkForNetId.put(netId, nai); |
| 793 | return; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | throw new IllegalStateException("No free netIds"); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Erik Kline | e660e27 | 2014-12-05 20:13:08 +0900 | [diff] [blame] | 800 | private int getConnectivityChangeDelay() { |
| 801 | final ContentResolver cr = mContext.getContentResolver(); |
| 802 | |
| 803 | /** Check system properties for the default value then use secure settings value, if any. */ |
| 804 | int defaultDelay = SystemProperties.getInt( |
| 805 | "conn." + Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
| 806 | ConnectivityManager.CONNECTIVITY_CHANGE_DELAY_DEFAULT); |
| 807 | return Settings.Global.getInt(cr, Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
| 808 | defaultDelay); |
| 809 | } |
| 810 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | private boolean teardown(NetworkStateTracker netTracker) { |
| 812 | if (netTracker.teardown()) { |
| 813 | netTracker.setTeardownRequested(true); |
| 814 | return true; |
| 815 | } else { |
| 816 | return false; |
| 817 | } |
| 818 | } |
| 819 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 820 | private NetworkState getFilteredNetworkState(int networkType, int uid) { |
| 821 | NetworkInfo info = null; |
| 822 | LinkProperties lp = null; |
| 823 | NetworkCapabilities nc = null; |
| 824 | Network network = null; |
| 825 | |
| 826 | if (mLegacyTypeTracker.isTypeSupported(networkType)) { |
| 827 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 828 | if (nai != null) { |
| 829 | synchronized (nai) { |
| 830 | info = new NetworkInfo(nai.networkInfo); |
| 831 | lp = new LinkProperties(nai.linkProperties); |
| 832 | nc = new NetworkCapabilities(nai.networkCapabilities); |
| 833 | network = new Network(nai.network); |
| 834 | } |
| 835 | info.setType(networkType); |
| 836 | } else { |
| 837 | info = new NetworkInfo(networkType, 0, getNetworkTypeName(networkType), ""); |
| 838 | info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 839 | info.setIsAvailable(true); |
| 840 | lp = new LinkProperties(); |
| 841 | nc = new NetworkCapabilities(); |
| 842 | network = null; |
| 843 | } |
| 844 | info = getFilteredNetworkInfo(info, lp, uid); |
| 845 | } |
| 846 | |
| 847 | return new NetworkState(info, lp, nc, network); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 848 | } |
| 849 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 850 | private NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) { |
| 851 | if (network == null) { |
| 852 | return null; |
| 853 | } |
| 854 | synchronized (mNetworkForNetId) { |
| 855 | return mNetworkForNetId.get(network.netId); |
| 856 | } |
| 857 | }; |
| 858 | |
| 859 | private NetworkState getUnfilteredActiveNetworkState(int uid) { |
| 860 | NetworkInfo info = null; |
| 861 | LinkProperties lp = null; |
| 862 | NetworkCapabilities nc = null; |
| 863 | Network network = null; |
| 864 | |
| 865 | NetworkAgentInfo nai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 866 | |
| 867 | if (!mLockdownEnabled) { |
| 868 | int user = UserHandle.getUserId(uid); |
| 869 | synchronized (mVpns) { |
| 870 | Vpn vpn = mVpns.get(user); |
| 871 | if (vpn != null && vpn.appliesToUid(uid)) { |
| 872 | // getUnderlyingNetworks() returns: |
| 873 | // null => the VPN didn't specify anything, so we use the default. |
| 874 | // empty array => the VPN explicitly said "no default network". |
| 875 | // non-empty array => the VPN specified one or more default networks; we use the |
| 876 | // first one. |
| 877 | Network[] networks = vpn.getUnderlyingNetworks(); |
| 878 | if (networks != null) { |
| 879 | if (networks.length > 0) { |
| 880 | nai = getNetworkAgentInfoForNetwork(networks[0]); |
| 881 | } else { |
| 882 | nai = null; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 889 | if (nai != null) { |
| 890 | synchronized (nai) { |
| 891 | info = new NetworkInfo(nai.networkInfo); |
| 892 | lp = new LinkProperties(nai.linkProperties); |
| 893 | nc = new NetworkCapabilities(nai.networkCapabilities); |
| 894 | network = new Network(nai.network); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | return new NetworkState(info, lp, nc, network); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Check if UID should be blocked from using the network with the given LinkProperties. |
| 903 | */ |
| 904 | private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 905 | final boolean networkCostly; |
| 906 | final int uidRules; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 907 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 908 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 909 | synchronized (mRulesLock) { |
| 910 | networkCostly = mMeteredIfaces.contains(iface); |
| 911 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 912 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 913 | |
| 914 | if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) { |
| 915 | return true; |
| 916 | } |
| 917 | |
| 918 | // no restrictive rules; network is visible |
| 919 | return false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 923 | * Return a filtered {@link NetworkInfo}, potentially marked |
| 924 | * {@link DetailedState#BLOCKED} based on |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 925 | * {@link #isNetworkWithLinkPropertiesBlocked}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 926 | */ |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 927 | private NetworkInfo getFilteredNetworkInfo(NetworkInfo info, LinkProperties lp, int uid) { |
| 928 | if (info != null && isNetworkWithLinkPropertiesBlocked(lp, uid)) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 929 | // network is blocked; clone and override state |
| 930 | info = new NetworkInfo(info); |
| 931 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 932 | if (DBG) log("returning Blocked NetworkInfo"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 933 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 934 | if (info != null && mLockdownTracker != null) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 935 | info = mLockdownTracker.augmentNetworkInfo(info); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 936 | if (DBG) log("returning Locked NetworkInfo"); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 937 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 938 | return info; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 943 | * It is assumed that at most one network is active at a time. If more |
| 944 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 945 | * @return the info for the active network, or {@code null} if none is |
| 946 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 948 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 950 | enforceAccessPermission(); |
| 951 | final int uid = Binder.getCallingUid(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 952 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 953 | return getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 956 | /** |
| 957 | * Find the first Provisioning network. |
| 958 | * |
| 959 | * @return NetworkInfo or null if none. |
| 960 | */ |
| 961 | private NetworkInfo getProvisioningNetworkInfo() { |
| 962 | enforceAccessPermission(); |
| 963 | |
| 964 | // Find the first Provisioning Network |
| 965 | NetworkInfo provNi = null; |
| 966 | for (NetworkInfo ni : getAllNetworkInfo()) { |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 967 | if (ni.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 968 | provNi = ni; |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | if (DBG) log("getProvisioningNetworkInfo: X provNi=" + provNi); |
| 973 | return provNi; |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * Find the first Provisioning network or the ActiveDefaultNetwork |
| 978 | * if there is no Provisioning network |
| 979 | * |
| 980 | * @return NetworkInfo or null if none. |
| 981 | */ |
| 982 | @Override |
| 983 | public NetworkInfo getProvisioningOrActiveNetworkInfo() { |
| 984 | enforceAccessPermission(); |
| 985 | |
| 986 | NetworkInfo provNi = getProvisioningNetworkInfo(); |
| 987 | if (provNi == null) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 988 | provNi = getActiveNetworkInfo(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 989 | } |
| 990 | if (DBG) log("getProvisioningOrActiveNetworkInfo: X provNi=" + provNi); |
| 991 | return provNi; |
| 992 | } |
| 993 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 994 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 995 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 996 | final int uid = Binder.getCallingUid(); |
| 997 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 998 | return state.networkInfo; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1001 | @Override |
| 1002 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 1003 | enforceConnectivityInternalPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1004 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1005 | return getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1010 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1011 | final int uid = Binder.getCallingUid(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1012 | NetworkState state = getFilteredNetworkState(networkType, uid); |
| 1013 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1016 | @Override |
| 1017 | public NetworkInfo getNetworkInfoForNetwork(Network network) { |
| 1018 | enforceAccessPermission(); |
| 1019 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1020 | NetworkInfo info = null; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1021 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 1022 | if (nai != null) { |
| 1023 | synchronized (nai) { |
| 1024 | info = new NetworkInfo(nai.networkInfo); |
| 1025 | info = getFilteredNetworkInfo(info, nai.linkProperties, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | return info; |
| 1029 | } |
| 1030 | |
| 1031 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | public NetworkInfo[] getAllNetworkInfo() { |
| 1033 | enforceAccessPermission(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1034 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1035 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1036 | networkType++) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1037 | NetworkInfo info = getNetworkInfo(networkType); |
| 1038 | if (info != null) { |
| 1039 | result.add(info); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1042 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1045 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1046 | public Network getNetworkForType(int networkType) { |
| 1047 | enforceAccessPermission(); |
| 1048 | final int uid = Binder.getCallingUid(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1049 | NetworkState state = getFilteredNetworkState(networkType, uid); |
| 1050 | if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid)) { |
| 1051 | return state.network; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1052 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1053 | return null; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1057 | public Network[] getAllNetworks() { |
| 1058 | enforceAccessPermission(); |
| 1059 | final ArrayList<Network> result = new ArrayList(); |
| 1060 | synchronized (mNetworkForNetId) { |
| 1061 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
| 1062 | result.add(new Network(mNetworkForNetId.valueAt(i).network)); |
| 1063 | } |
| 1064 | } |
| 1065 | return result.toArray(new Network[result.size()]); |
| 1066 | } |
| 1067 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1068 | private NetworkCapabilities getNetworkCapabilitiesAndValidation(NetworkAgentInfo nai) { |
| 1069 | if (nai != null) { |
| 1070 | synchronized (nai) { |
| 1071 | if (nai.created) { |
| 1072 | NetworkCapabilities nc = new NetworkCapabilities(nai.networkCapabilities); |
| 1073 | if (nai.validated) { |
| 1074 | nc.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); |
| 1075 | } else { |
| 1076 | nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); |
| 1077 | } |
| 1078 | return nc; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | return null; |
| 1083 | } |
| 1084 | |
| 1085 | @Override |
| 1086 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1087 | // The basic principle is: if an app's traffic could possibly go over a |
| 1088 | // network, without the app doing anything multinetwork-specific, |
| 1089 | // (hence, by "default"), then include that network's capabilities in |
| 1090 | // the array. |
| 1091 | // |
| 1092 | // In the normal case, app traffic only goes over the system's default |
| 1093 | // network connection, so that's the only network returned. |
| 1094 | // |
| 1095 | // With a VPN in force, some app traffic may go into the VPN, and thus |
| 1096 | // over whatever underlying networks the VPN specifies, while other app |
| 1097 | // traffic may go over the system default network (e.g.: a split-tunnel |
| 1098 | // VPN, or an app disallowed by the VPN), so the set of networks |
| 1099 | // returned includes the VPN's underlying networks and the system |
| 1100 | // default. |
| 1101 | enforceAccessPermission(); |
| 1102 | |
| 1103 | HashMap<Network, NetworkCapabilities> result = new HashMap<Network, NetworkCapabilities>(); |
| 1104 | |
| 1105 | NetworkAgentInfo nai = getDefaultNetwork(); |
| 1106 | NetworkCapabilities nc = getNetworkCapabilitiesAndValidation(getDefaultNetwork()); |
| 1107 | if (nc != null) { |
| 1108 | result.put(nai.network, nc); |
| 1109 | } |
| 1110 | |
| 1111 | if (!mLockdownEnabled) { |
| 1112 | synchronized (mVpns) { |
| 1113 | Vpn vpn = mVpns.get(userId); |
| 1114 | if (vpn != null) { |
| 1115 | Network[] networks = vpn.getUnderlyingNetworks(); |
| 1116 | if (networks != null) { |
| 1117 | for (Network network : networks) { |
| 1118 | nai = getNetworkAgentInfoForNetwork(network); |
| 1119 | nc = getNetworkCapabilitiesAndValidation(nai); |
| 1120 | if (nc != null) { |
| 1121 | result.put(nai.network, nc); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | NetworkCapabilities[] out = new NetworkCapabilities[result.size()]; |
| 1130 | out = result.values().toArray(out); |
| 1131 | return out; |
| 1132 | } |
| 1133 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1134 | @Override |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1135 | public boolean isNetworkSupported(int networkType) { |
| 1136 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1137 | return mLegacyTypeTracker.isTypeSupported(networkType); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1140 | /** |
| 1141 | * Return LinkProperties for the active (i.e., connected) default |
| 1142 | * network interface. It is assumed that at most one default network |
| 1143 | * is active at a time. If more than one is active, it is indeterminate |
| 1144 | * which will be returned. |
| 1145 | * @return the ip properties for the active network, or {@code null} if |
| 1146 | * none is active |
| 1147 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1148 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1149 | public LinkProperties getActiveLinkProperties() { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1150 | enforceAccessPermission(); |
| 1151 | final int uid = Binder.getCallingUid(); |
| 1152 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1153 | return state.linkProperties; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1156 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1157 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1158 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1159 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1160 | if (nai != null) { |
| 1161 | synchronized (nai) { |
| 1162 | return new LinkProperties(nai.linkProperties); |
| 1163 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1164 | } |
| 1165 | return null; |
| 1166 | } |
| 1167 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1168 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1169 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1170 | public LinkProperties getLinkProperties(Network network) { |
| 1171 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1172 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1173 | if (nai != null) { |
| 1174 | synchronized (nai) { |
| 1175 | return new LinkProperties(nai.linkProperties); |
| 1176 | } |
| 1177 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1178 | return null; |
| 1179 | } |
| 1180 | |
| 1181 | @Override |
| 1182 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1183 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1184 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1185 | if (nai != null) { |
| 1186 | synchronized (nai) { |
| 1187 | return new NetworkCapabilities(nai.networkCapabilities); |
| 1188 | } |
| 1189 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1190 | return null; |
| 1191 | } |
| 1192 | |
| 1193 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1194 | public NetworkState[] getAllNetworkState() { |
| 1195 | enforceAccessPermission(); |
| 1196 | final int uid = Binder.getCallingUid(); |
| 1197 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1198 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1199 | networkType++) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1200 | NetworkState state = getFilteredNetworkState(networkType, uid); |
| 1201 | if (state.networkInfo != null) { |
| 1202 | result.add(state); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | return result.toArray(new NetworkState[result.size()]); |
| 1206 | } |
| 1207 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1208 | @Override |
| 1209 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1210 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1211 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1212 | final long token = Binder.clearCallingIdentity(); |
| 1213 | try { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1214 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1215 | if (state.networkInfo != null) { |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1216 | try { |
| 1217 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1218 | } catch (RemoteException e) { |
| 1219 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1220 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1221 | return null; |
| 1222 | } finally { |
| 1223 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1224 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1227 | @Override |
| 1228 | public boolean isActiveNetworkMetered() { |
| 1229 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1230 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1231 | final long token = Binder.clearCallingIdentity(); |
| 1232 | try { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1233 | return isActiveNetworkMeteredUnchecked(uid); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1234 | } finally { |
| 1235 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1236 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1239 | private boolean isActiveNetworkMeteredUnchecked(int uid) { |
| 1240 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1241 | if (state.networkInfo != null) { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1242 | try { |
| 1243 | return mPolicyManager.isNetworkMetered(state); |
| 1244 | } catch (RemoteException e) { |
| 1245 | } |
| 1246 | } |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1250 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1251 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1252 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1253 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1254 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1255 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1256 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1257 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1258 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | * Ensure that a network route exists to deliver traffic to the specified |
| 1260 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1261 | * @param networkType the type of the network over which traffic to the |
| 1262 | * specified host is to be routed |
| 1263 | * @param hostAddress the IP address of the host to which the route is |
| 1264 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | * @return {@code true} on success, {@code false} on failure |
| 1266 | */ |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1267 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1269 | if (mProtectedNetworks.contains(networkType)) { |
| 1270 | enforceConnectivityInternalPermission(); |
| 1271 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1272 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1273 | InetAddress addr; |
| 1274 | try { |
| 1275 | addr = InetAddress.getByAddress(hostAddress); |
| 1276 | } catch (UnknownHostException e) { |
| 1277 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1278 | return false; |
| 1279 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1282 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | return false; |
| 1284 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1285 | |
| 1286 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1287 | if (nai == null) { |
| 1288 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1289 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1290 | } else { |
| 1291 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1292 | } |
| 1293 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1294 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1295 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1296 | DetailedState netState; |
| 1297 | synchronized (nai) { |
| 1298 | netState = nai.networkInfo.getDetailedState(); |
| 1299 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1300 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1301 | if (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1302 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1303 | log("requestRouteToHostAddress on down network " |
| 1304 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1305 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1306 | } |
| 1307 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1309 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1310 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1311 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1312 | try { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 1313 | LinkProperties lp; |
| 1314 | int netId; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1315 | synchronized (nai) { |
| 1316 | lp = nai.linkProperties; |
| 1317 | netId = nai.network.netId; |
| 1318 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1319 | boolean ok = addLegacyRouteToHost(lp, addr, netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1320 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1321 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1322 | } finally { |
| 1323 | Binder.restoreCallingIdentity(token); |
| 1324 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1327 | private boolean addLegacyRouteToHost(LinkProperties lp, InetAddress addr, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1328 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1329 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1330 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1331 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1332 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1333 | if (bestRoute.getGateway().equals(addr)) { |
| 1334 | // if there is no better route, add the implied hostroute for our gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1335 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1336 | } else { |
| 1337 | // if we will connect to this through another route, add a direct route |
| 1338 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1339 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1342 | if (DBG) log("Adding " + bestRoute + " for interface " + bestRoute.getInterface()); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1343 | try { |
| 1344 | mNetd.addLegacyRouteForNetId(netId, bestRoute, uid); |
| 1345 | } catch (Exception e) { |
| 1346 | // never crash - catch them all |
| 1347 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1348 | return false; |
| 1349 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1350 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | } |
| 1352 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1353 | public void setDataDependency(int networkType, boolean met) { |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1354 | enforceConnectivityInternalPermission(); |
| 1355 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1356 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET, |
| 1357 | (met ? ENABLED : DISABLED), networkType)); |
| 1358 | } |
| 1359 | |
| 1360 | private void handleSetDependencyMet(int networkType, boolean met) { |
| 1361 | if (mNetTrackers[networkType] != null) { |
| 1362 | if (DBG) { |
| 1363 | log("handleSetDependencyMet(" + networkType + ", " + met + ")"); |
| 1364 | } |
| 1365 | mNetTrackers[networkType].setDependencyMet(met); |
| 1366 | } |
| 1367 | } |
| 1368 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1369 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1370 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1371 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1372 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1373 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1374 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1377 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1378 | // skip update when we've already applied rules |
| 1379 | final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 1380 | if (oldRules == uidRules) return; |
| 1381 | |
| 1382 | mUidRules.put(uid, uidRules); |
| 1383 | } |
| 1384 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1385 | // TODO: notify UID when it has requested targeted updates |
| 1386 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1387 | |
| 1388 | @Override |
| 1389 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1390 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1391 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1392 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | synchronized (mRulesLock) { |
| 1396 | mMeteredIfaces.clear(); |
| 1397 | for (String iface : meteredIfaces) { |
| 1398 | mMeteredIfaces.add(iface); |
| 1399 | } |
| 1400 | } |
| 1401 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1402 | |
| 1403 | @Override |
| 1404 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1405 | // caller is NPMS, since we only register with them |
| 1406 | if (LOGD_RULES) { |
| 1407 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1408 | } |
| 1409 | |
| 1410 | // kick off connectivity change broadcast for active network, since |
| 1411 | // global background policy change is radical. |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1412 | // TODO: Dead code; remove. |
| 1413 | // |
| 1414 | // final int networkType = mActiveDefaultNetwork; |
| 1415 | // if (isNetworkTypeValid(networkType)) { |
| 1416 | // final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1417 | // if (tracker != null) { |
| 1418 | // final NetworkInfo info = tracker.getNetworkInfo(); |
| 1419 | // if (info != null && info.isConnected()) { |
| 1420 | // sendConnectedBroadcast(info); |
| 1421 | // } |
| 1422 | // } |
| 1423 | // } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1424 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1425 | }; |
| 1426 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1427 | @Override |
| 1428 | public void setPolicyDataEnable(int networkType, boolean enabled) { |
| 1429 | // only someone like NPMS should only be calling us |
| 1430 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1431 | |
| 1432 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1433 | EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED))); |
| 1434 | } |
| 1435 | |
| 1436 | private void handleSetPolicyDataEnable(int networkType, boolean enabled) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1437 | // TODO - handle this passing to factories |
| 1438 | // if (isNetworkTypeValid(networkType)) { |
| 1439 | // final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1440 | // if (tracker != null) { |
| 1441 | // tracker.setPolicyDataEnable(enabled); |
| 1442 | // } |
| 1443 | // } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1446 | private void enforceInternetPermission() { |
| 1447 | mContext.enforceCallingOrSelfPermission( |
| 1448 | android.Manifest.permission.INTERNET, |
| 1449 | "ConnectivityService"); |
| 1450 | } |
| 1451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1453 | mContext.enforceCallingOrSelfPermission( |
| 1454 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1455 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | private void enforceChangePermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1459 | mContext.enforceCallingOrSelfPermission( |
| 1460 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1461 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1464 | private void enforceTetherAccessPermission() { |
| 1465 | mContext.enforceCallingOrSelfPermission( |
| 1466 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1467 | "ConnectivityService"); |
| 1468 | } |
| 1469 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1470 | private void enforceConnectivityInternalPermission() { |
| 1471 | mContext.enforceCallingOrSelfPermission( |
| 1472 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 1473 | "ConnectivityService"); |
| 1474 | } |
| 1475 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1476 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1477 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1478 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 1479 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Erik Kline | e660e27 | 2014-12-05 20:13:08 +0900 | [diff] [blame] | 1482 | private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { |
| 1483 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 1484 | sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs); |
| 1485 | } |
| 1486 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1487 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 1488 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 1489 | } |
| 1490 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1491 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1492 | if (mLockdownTracker != null) { |
| 1493 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 1494 | } |
| 1495 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1496 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1497 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1498 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | if (info.isFailover()) { |
| 1500 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1501 | info.setFailover(false); |
| 1502 | } |
| 1503 | if (info.getReason() != null) { |
| 1504 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 1505 | } |
| 1506 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1507 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 1508 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1510 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1511 | return intent; |
| 1512 | } |
| 1513 | |
| 1514 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 1515 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 1516 | } |
| 1517 | |
Erik Kline | e660e27 | 2014-12-05 20:13:08 +0900 | [diff] [blame] | 1518 | private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) { |
| 1519 | sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs); |
| 1520 | } |
| 1521 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1522 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1523 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 1524 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 1525 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1526 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1527 | final long ident = Binder.clearCallingIdentity(); |
| 1528 | try { |
| 1529 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 1530 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 1531 | } finally { |
| 1532 | Binder.restoreCallingIdentity(ident); |
| 1533 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1536 | private void sendStickyBroadcast(Intent intent) { |
| 1537 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1538 | if (!mSystemReady) { |
| 1539 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1540 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1541 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1542 | if (DBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1543 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1546 | final long ident = Binder.clearCallingIdentity(); |
| 1547 | try { |
| 1548 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1549 | } finally { |
| 1550 | Binder.restoreCallingIdentity(ident); |
| 1551 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
Erik Kline | e660e27 | 2014-12-05 20:13:08 +0900 | [diff] [blame] | 1555 | private void sendStickyBroadcastDelayed(Intent intent, int delayMs) { |
| 1556 | if (delayMs <= 0) { |
| 1557 | sendStickyBroadcast(intent); |
| 1558 | } else { |
| 1559 | if (VDBG) { |
| 1560 | log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action=" |
| 1561 | + intent.getAction()); |
| 1562 | } |
| 1563 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 1564 | EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); |
| 1565 | } |
| 1566 | } |
| 1567 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1568 | void systemReady() { |
Robert Greenwalt | f9b5c26 | 2014-07-27 09:41:34 -0700 | [diff] [blame] | 1569 | // start network sampling .. |
| 1570 | Intent intent = new Intent(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED); |
| 1571 | intent.setPackage(mContext.getPackageName()); |
| 1572 | |
| 1573 | mSampleIntervalElapsedIntent = PendingIntent.getBroadcast(mContext, |
| 1574 | SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE, intent, 0); |
| 1575 | setAlarm(DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS * 1000, mSampleIntervalElapsedIntent); |
| 1576 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1577 | loadGlobalProxy(); |
| 1578 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1579 | synchronized(this) { |
| 1580 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1581 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1582 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1583 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1584 | } |
| 1585 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1586 | // load the global proxy at startup |
| 1587 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1588 | |
| 1589 | // Try bringing up tracker, but if KeyStore isn't ready yet, wait |
| 1590 | // for user to unlock device. |
| 1591 | if (!updateLockdownVpn()) { |
| 1592 | final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT); |
| 1593 | mContext.registerReceiver(mUserPresentReceiver, filter); |
| 1594 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 1595 | |
| 1596 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 1597 | |
| 1598 | mPermissionMonitor.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1601 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 1602 | @Override |
| 1603 | public void onReceive(Context context, Intent intent) { |
| 1604 | // Try creating lockdown tracker, since user present usually means |
| 1605 | // unlocked keystore. |
| 1606 | if (updateLockdownVpn()) { |
| 1607 | mContext.unregisterReceiver(this); |
| 1608 | } |
| 1609 | } |
| 1610 | }; |
| 1611 | |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 1612 | /** @hide */ |
| 1613 | @Override |
| 1614 | public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) { |
| 1615 | enforceConnectivityInternalPermission(); |
| 1616 | if (DBG) log("captivePortalCheckCompleted: ni=" + info + " captive=" + isCaptivePortal); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1617 | // mNetTrackers[info.getType()].captivePortalCheckCompleted(isCaptivePortal); |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1621 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1622 | * |
| 1623 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1624 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1625 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1626 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1627 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1628 | |
| 1629 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1630 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1631 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1632 | if (networkAgent.networkCapabilities.hasTransport( |
| 1633 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1634 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1635 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 1636 | 5); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1637 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1638 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 1639 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1640 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1641 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1642 | 0); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1643 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1644 | } else { |
| 1645 | // do not track any other networks |
| 1646 | timeout = 0; |
| 1647 | } |
| 1648 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1649 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1650 | try { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1651 | mNetd.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1652 | } catch (Exception e) { |
| 1653 | // You shall not crash! |
| 1654 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | /** |
| 1660 | * Remove data activity tracking when network disconnects. |
| 1661 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1662 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1663 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 1664 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1665 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1666 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 1667 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1668 | try { |
| 1669 | // the call fails silently if no idletimer setup for this interface |
| 1670 | mNetd.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1671 | } catch (Exception e) { |
| 1672 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | /** |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1678 | * Reads the network specific MTU size from reources. |
| 1679 | * and set it on it's iface. |
| 1680 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1681 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 1682 | final String iface = newLp.getInterfaceName(); |
| 1683 | final int mtu = newLp.getMtu(); |
| 1684 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 1685 | if (VDBG) log("identical MTU - not setting"); |
| 1686 | return; |
| 1687 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1688 | |
Robert Greenwalt | 4307403 | 2014-08-15 17:53:05 -0700 | [diff] [blame] | 1689 | if (LinkProperties.isValidMtu(mtu, newLp.hasGlobalIPv6Address()) == false) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1690 | loge("Unexpected mtu value: " + mtu + ", " + iface); |
| 1691 | return; |
| 1692 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1693 | |
w19976 | 15afd81 | 2014-08-05 15:18:11 -0700 | [diff] [blame] | 1694 | // Cannot set MTU without interface name |
| 1695 | if (TextUtils.isEmpty(iface)) { |
| 1696 | loge("Setting MTU size with null iface."); |
| 1697 | return; |
| 1698 | } |
| 1699 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1700 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1701 | if (DBG) log("Setting MTU size: " + iface + ", " + mtu); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1702 | mNetd.setMtu(iface, mtu); |
| 1703 | } catch (Exception e) { |
| 1704 | Slog.e(TAG, "exception in setMtu()" + e); |
| 1705 | } |
| 1706 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1707 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1708 | private static final String DEFAULT_TCP_BUFFER_SIZES = "4096,87380,110208,4096,16384,110208"; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1709 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1710 | private void updateTcpBufferSizes(NetworkAgentInfo nai) { |
| 1711 | if (isDefaultNetwork(nai) == false) { |
| 1712 | return; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1715 | String tcpBufferSizes = nai.linkProperties.getTcpBufferSizes(); |
| 1716 | String[] values = null; |
| 1717 | if (tcpBufferSizes != null) { |
| 1718 | values = tcpBufferSizes.split(","); |
| 1719 | } |
| 1720 | |
| 1721 | if (values == null || values.length != 6) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1722 | if (DBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults"); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1723 | tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES; |
| 1724 | values = tcpBufferSizes.split(","); |
| 1725 | } |
| 1726 | |
| 1727 | if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return; |
| 1728 | |
| 1729 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1730 | if (DBG) Slog.d(TAG, "Setting tx/rx TCP buffers to " + tcpBufferSizes); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1731 | |
| 1732 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
| 1733 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 1734 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 1735 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 1736 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 1737 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 1738 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
| 1739 | mCurrentTcpBufferSizes = tcpBufferSizes; |
| 1740 | } catch (IOException e) { |
| 1741 | loge("Can't set TCP buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1742 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1743 | |
| 1744 | final String defaultRwndKey = "net.tcp.default_init_rwnd"; |
| 1745 | int defaultRwndValue = SystemProperties.getInt(defaultRwndKey, 0); |
| 1746 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
| 1747 | Settings.Global.TCP_DEFAULT_INIT_RWND, defaultRwndValue); |
| 1748 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 1749 | if (rwndValue != 0) { |
| 1750 | SystemProperties.set(sysctlKey, rwndValue.toString()); |
| 1751 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1754 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1755 | /* |
| 1756 | * Tell the VMs to toss their DNS caches |
| 1757 | */ |
| 1758 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 1759 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 1760 | /* |
| 1761 | * Connectivity events can happen before boot has completed ... |
| 1762 | */ |
| 1763 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1764 | final long ident = Binder.clearCallingIdentity(); |
| 1765 | try { |
| 1766 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1767 | } finally { |
| 1768 | Binder.restoreCallingIdentity(ident); |
| 1769 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1772 | @Override |
| 1773 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1774 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 1775 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 1776 | if(restoreDefaultNetworkDelayStr != null && |
| 1777 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 1778 | try { |
| 1779 | return Integer.valueOf(restoreDefaultNetworkDelayStr); |
| 1780 | } catch (NumberFormatException e) { |
| 1781 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 1783 | // if the system property isn't set, use the value for the apn type |
| 1784 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 1785 | |
| 1786 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 1787 | (mNetConfigs[networkType] != null)) { |
| 1788 | ret = mNetConfigs[networkType].restoreTime; |
| 1789 | } |
| 1790 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1794 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1795 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1796 | if (mContext.checkCallingOrSelfPermission( |
| 1797 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1799 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 1800 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 1801 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | return; |
| 1803 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1804 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1805 | pw.println("NetworkFactories for:"); |
| 1806 | pw.increaseIndent(); |
| 1807 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 1808 | pw.println(nfi.name); |
| 1809 | } |
| 1810 | pw.decreaseIndent(); |
| 1811 | pw.println(); |
| 1812 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1813 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 1814 | pw.print("Active default network: "); |
| 1815 | if (defaultNai == null) { |
| 1816 | pw.println("none"); |
| 1817 | } else { |
| 1818 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1819 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1820 | pw.println(); |
| 1821 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1822 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1823 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1824 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1825 | pw.println(nai.toString()); |
| 1826 | pw.increaseIndent(); |
| 1827 | pw.println("Requests:"); |
| 1828 | pw.increaseIndent(); |
| 1829 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 1830 | pw.println(nai.networkRequests.valueAt(i).toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1831 | } |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1832 | pw.decreaseIndent(); |
| 1833 | pw.println("Lingered:"); |
| 1834 | pw.increaseIndent(); |
| 1835 | for (NetworkRequest nr : nai.networkLingered) pw.println(nr.toString()); |
| 1836 | pw.decreaseIndent(); |
| 1837 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1838 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1839 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1840 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1841 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1842 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1843 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1844 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 1845 | pw.println(nri.toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1846 | } |
| 1847 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1848 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1849 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 1850 | pw.println("mLegacyTypeTracker:"); |
| 1851 | pw.increaseIndent(); |
| 1852 | mLegacyTypeTracker.dump(pw); |
| 1853 | pw.decreaseIndent(); |
| 1854 | pw.println(); |
| 1855 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1856 | synchronized (this) { |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1857 | pw.println("NetworkTransitionWakeLock is currently " + |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1858 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held."); |
| 1859 | pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy); |
| 1860 | } |
| 1861 | pw.println(); |
| 1862 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1863 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1864 | |
| 1865 | if (mInetLog != null) { |
| 1866 | pw.println(); |
| 1867 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1868 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1869 | for(int i = 0; i < mInetLog.size(); i++) { |
| 1870 | pw.println(mInetLog.get(i)); |
| 1871 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1872 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1873 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1874 | } |
| 1875 | |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1876 | private boolean isLiveNetworkAgent(NetworkAgentInfo nai, String msg) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1877 | if (nai.network == null) return false; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1878 | final NetworkAgentInfo officialNai = getNetworkAgentInfoForNetwork(nai.network); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1879 | if (officialNai != null && officialNai.equals(nai)) return true; |
| 1880 | if (officialNai != null || VDBG) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1881 | loge(msg + " - isLiveNetworkAgent found mismatched netId: " + officialNai + |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1882 | " - " + nai); |
| 1883 | } |
| 1884 | return false; |
| 1885 | } |
| 1886 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1887 | private boolean isRequest(NetworkRequest request) { |
| 1888 | return mNetworkRequests.get(request).isRequest; |
| 1889 | } |
| 1890 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1891 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 1892 | private class NetworkStateTrackerHandler extends Handler { |
| 1893 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 1894 | super(looper); |
| 1895 | } |
| 1896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1897 | @Override |
| 1898 | public void handleMessage(Message msg) { |
| 1899 | NetworkInfo info; |
| 1900 | switch (msg.what) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1901 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1902 | handleAsyncChannelHalfConnect(msg); |
| 1903 | break; |
| 1904 | } |
| 1905 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 1906 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1907 | if (nai != null) nai.asyncChannel.disconnect(); |
| 1908 | break; |
| 1909 | } |
| 1910 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 1911 | handleAsyncChannelDisconnected(msg); |
| 1912 | break; |
| 1913 | } |
| 1914 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
| 1915 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1916 | if (nai == null) { |
| 1917 | loge("EVENT_NETWORK_CAPABILITIES_CHANGED from unknown NetworkAgent"); |
| 1918 | } else { |
| 1919 | updateCapabilities(nai, (NetworkCapabilities)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1920 | } |
| 1921 | break; |
| 1922 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1923 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
| 1924 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1925 | if (nai == null) { |
| 1926 | loge("NetworkAgent not found for EVENT_NETWORK_PROPERTIES_CHANGED"); |
| 1927 | } else { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 1928 | if (VDBG) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1929 | log("Update of LinkProperties for " + nai.name() + |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 1930 | "; created=" + nai.created); |
| 1931 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1932 | LinkProperties oldLp = nai.linkProperties; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1933 | synchronized (nai) { |
| 1934 | nai.linkProperties = (LinkProperties)msg.obj; |
| 1935 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 1936 | if (nai.created) updateLinkProperties(nai, oldLp); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1937 | } |
| 1938 | break; |
| 1939 | } |
| 1940 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
| 1941 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1942 | if (nai == null) { |
| 1943 | loge("EVENT_NETWORK_INFO_CHANGED from unknown NetworkAgent"); |
| 1944 | break; |
| 1945 | } |
| 1946 | info = (NetworkInfo) msg.obj; |
| 1947 | updateNetworkInfo(nai, info); |
| 1948 | break; |
| 1949 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 1950 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
| 1951 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1952 | if (nai == null) { |
| 1953 | loge("EVENT_NETWORK_SCORE_CHANGED from unknown NetworkAgent"); |
| 1954 | break; |
| 1955 | } |
| 1956 | Integer score = (Integer) msg.obj; |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 1957 | if (score != null) updateNetworkScore(nai, score.intValue()); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 1958 | break; |
| 1959 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1960 | case NetworkAgent.EVENT_UID_RANGES_ADDED: { |
| 1961 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1962 | if (nai == null) { |
| 1963 | loge("EVENT_UID_RANGES_ADDED from unknown NetworkAgent"); |
| 1964 | break; |
| 1965 | } |
| 1966 | try { |
| 1967 | mNetd.addVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 1968 | } catch (Exception e) { |
| 1969 | // Never crash! |
| 1970 | loge("Exception in addVpnUidRanges: " + e); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1971 | } |
| 1972 | break; |
| 1973 | } |
| 1974 | case NetworkAgent.EVENT_UID_RANGES_REMOVED: { |
| 1975 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1976 | if (nai == null) { |
| 1977 | loge("EVENT_UID_RANGES_REMOVED from unknown NetworkAgent"); |
| 1978 | break; |
| 1979 | } |
| 1980 | try { |
| 1981 | mNetd.removeVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 1982 | } catch (Exception e) { |
| 1983 | // Never crash! |
| 1984 | loge("Exception in removeVpnUidRanges: " + e); |
| 1985 | } |
| 1986 | break; |
| 1987 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 1988 | case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { |
| 1989 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1990 | if (nai == null) { |
| 1991 | loge("EVENT_SET_EXPLICITLY_SELECTED from unknown NetworkAgent"); |
| 1992 | break; |
| 1993 | } |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 1994 | if (nai.created && !nai.networkMisc.explicitlySelected) { |
| 1995 | loge("ERROR: created network explicitly selected."); |
| 1996 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 1997 | nai.networkMisc.explicitlySelected = true; |
| 1998 | break; |
| 1999 | } |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2000 | case NetworkMonitor.EVENT_NETWORK_TESTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2001 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2002 | if (isLiveNetworkAgent(nai, "EVENT_NETWORK_VALIDATED")) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2003 | boolean valid = (msg.arg1 == NetworkMonitor.NETWORK_TEST_RESULT_VALID); |
| 2004 | if (valid) { |
| 2005 | if (DBG) log("Validated " + nai.name()); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2006 | final boolean previouslyValidated = nai.validated; |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2007 | final int previousScore = nai.getCurrentScore(); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2008 | nai.validated = true; |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2009 | rematchNetworkAndRequests(nai, !previouslyValidated); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2010 | // If score has changed, rebroadcast to NetworkFactories. b/17726566 |
| 2011 | if (nai.getCurrentScore() != previousScore) { |
| 2012 | sendUpdatedScoreToFactories(nai); |
| 2013 | } |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2014 | } |
| 2015 | updateInetCondition(nai, valid); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2016 | // Let the NetworkAgent know the state of its network |
| 2017 | nai.asyncChannel.sendMessage( |
| 2018 | android.net.NetworkAgent.CMD_REPORT_NETWORK_STATUS, |
| 2019 | (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), |
| 2020 | 0, null); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2021 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2022 | break; |
| 2023 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2024 | case NetworkMonitor.EVENT_NETWORK_LINGER_COMPLETE: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2025 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2026 | if (isLiveNetworkAgent(nai, "EVENT_NETWORK_LINGER_COMPLETE")) { |
| 2027 | handleLingerComplete(nai); |
| 2028 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2029 | break; |
| 2030 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2031 | case NetworkMonitor.EVENT_PROVISIONING_NOTIFICATION: { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2032 | if (msg.arg1 == 0) { |
| 2033 | setProvNotificationVisibleIntent(false, msg.arg2, 0, null, null); |
| 2034 | } else { |
Paul Jensen | 5df4bec | 2014-08-25 22:45:39 -0400 | [diff] [blame] | 2035 | NetworkAgentInfo nai = null; |
| 2036 | synchronized (mNetworkForNetId) { |
| 2037 | nai = mNetworkForNetId.get(msg.arg2); |
| 2038 | } |
| 2039 | if (nai == null) { |
| 2040 | loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor"); |
| 2041 | break; |
| 2042 | } |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2043 | setProvNotificationVisibleIntent(true, msg.arg2, nai.networkInfo.getType(), |
| 2044 | nai.networkInfo.getExtraInfo(), (PendingIntent)msg.obj); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2045 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2046 | break; |
| 2047 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2048 | case NetworkStateTracker.EVENT_STATE_CHANGED: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2049 | info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2050 | NetworkInfo.State state = info.getState(); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2051 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2052 | if (VDBG || (state == NetworkInfo.State.CONNECTED) || |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 2053 | (state == NetworkInfo.State.DISCONNECTED) || |
| 2054 | (state == NetworkInfo.State.SUSPENDED)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2055 | log("ConnectivityChange for " + |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2056 | info.getTypeName() + ": " + |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2057 | state + "/" + info.getDetailedState()); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2058 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2059 | |
Jeff Sharkey | 2528b50 | 2012-11-09 15:57:02 -0800 | [diff] [blame] | 2060 | EventLogTags.writeConnectivityStateChanged( |
| 2061 | info.getType(), info.getSubtype(), info.getDetailedState().ordinal()); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2062 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2063 | if (info.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2064 | /** |
| 2065 | * TODO: Create ConnectivityManager.TYPE_MOBILE_PROVISIONING |
| 2066 | * for now its an in between network, its a network that |
| 2067 | * is actually a default network but we don't want it to be |
| 2068 | * announced as such to keep background applications from |
| 2069 | * trying to use it. It turns out that some still try so we |
| 2070 | * take the additional step of clearing any default routes |
| 2071 | * to the link that may have incorrectly setup by the lower |
| 2072 | * levels. |
| 2073 | */ |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 2074 | LinkProperties lp = getLinkPropertiesForType(info.getType()); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2075 | if (DBG) { |
| 2076 | log("EVENT_STATE_CHANGED: connected to provisioning network, lp=" + lp); |
| 2077 | } |
| 2078 | |
| 2079 | // Clear any default routes setup by the radio so |
| 2080 | // any activity by applications trying to use this |
| 2081 | // connection will fail until the provisioning network |
| 2082 | // is enabled. |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2083 | /* |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2084 | for (RouteInfo r : lp.getRoutes()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2085 | removeRoute(lp, r, TO_DEFAULT_TABLE, |
| 2086 | mNetTrackers[info.getType()].getNetwork().netId); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2087 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2088 | */ |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2089 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2090 | } else if (state == NetworkInfo.State.SUSPENDED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2091 | } else if (state == NetworkInfo.State.CONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2092 | // handleConnect(info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 2094 | notifyLockdownVpn(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2095 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2096 | } |
| 2097 | case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED: { |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2098 | info = (NetworkInfo) msg.obj; |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2099 | // TODO: Temporary allowing network configuration |
| 2100 | // change not resetting sockets. |
| 2101 | // @see bug/4455071 |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2102 | /* |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2103 | handleConnectivityChange(info.getType(), mCurrentLinkProperties[info.getType()], |
| 2104 | false); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2105 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2106 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2107 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | } |
| 2111 | |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2112 | // Cancel any lingering so the linger timeout doesn't teardown a network. |
| 2113 | // This should be called when a network begins satisfying a NetworkRequest. |
| 2114 | // Note: depending on what state the NetworkMonitor is in (e.g., |
| 2115 | // if it's awaiting captive portal login, or if validation failed), this |
| 2116 | // may trigger a re-evaluation of the network. |
| 2117 | private void unlinger(NetworkAgentInfo nai) { |
| 2118 | if (VDBG) log("Canceling linger of " + nai.name()); |
| 2119 | nai.networkLingered.clear(); |
| 2120 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
| 2121 | } |
| 2122 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2123 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 2124 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2125 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2126 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2127 | if (VDBG) log("NetworkFactory connected"); |
| 2128 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2129 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2130 | if (nri.isRequest == false) continue; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2131 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2132 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2133 | (nai != null ? nai.getCurrentScore() : 0), 0, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2134 | } |
| 2135 | } else { |
| 2136 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2137 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2138 | } |
| 2139 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 2140 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2141 | if (VDBG) log("NetworkAgent connected"); |
| 2142 | // A network agent has requested a connection. Establish the connection. |
| 2143 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 2144 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2145 | } else { |
| 2146 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2147 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2148 | if (nai != null) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2149 | synchronized (mNetworkForNetId) { |
| 2150 | mNetworkForNetId.remove(nai.network.netId); |
| 2151 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2152 | // Just in case. |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 2153 | mLegacyTypeTracker.remove(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2154 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2158 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2159 | private void handleAsyncChannelDisconnected(Message msg) { |
| 2160 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2161 | if (nai != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2162 | if (DBG) { |
| 2163 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size()); |
| 2164 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2165 | // A network agent has disconnected. |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 2166 | if (nai.created) { |
| 2167 | // Tell netd to clean up the configuration for this network |
| 2168 | // (routing rules, DNS, etc). |
| 2169 | try { |
| 2170 | mNetd.removeNetwork(nai.network.netId); |
| 2171 | } catch (Exception e) { |
| 2172 | loge("Exception removing network: " + e); |
| 2173 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2174 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2175 | // TODO - if we move the logic to the network agent (have them disconnect |
| 2176 | // because they lost all their requests or because their score isn't good) |
| 2177 | // then they would disconnect organically, report their new state and then |
| 2178 | // disconnect the channel. |
| 2179 | if (nai.networkInfo.isConnected()) { |
| 2180 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 2181 | null, null); |
| 2182 | } |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2183 | if (isDefaultNetwork(nai)) { |
| 2184 | mDefaultInetConditionPublished = 0; |
| 2185 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2186 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2187 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_DISCONNECTED); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 2188 | mNetworkAgentInfos.remove(msg.replyTo); |
| 2189 | updateClat(null, nai.linkProperties, nai); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 2190 | mLegacyTypeTracker.remove(nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2191 | synchronized (mNetworkForNetId) { |
| 2192 | mNetworkForNetId.remove(nai.network.netId); |
| 2193 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2194 | // Since we've lost the network, go through all the requests that |
| 2195 | // it was satisfying and see if any other factory can satisfy them. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2196 | // TODO: This logic may be better replaced with a call to rematchAllNetworksAndRequests |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2197 | final ArrayList<NetworkAgentInfo> toActivate = new ArrayList<NetworkAgentInfo>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2198 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 2199 | NetworkRequest request = nai.networkRequests.valueAt(i); |
| 2200 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId); |
| 2201 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2202 | if (DBG) { |
| 2203 | log("Checking for replacement network to handle request " + request ); |
| 2204 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2205 | mNetworkForRequestId.remove(request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2206 | sendUpdatedScoreToFactories(request, 0); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2207 | NetworkAgentInfo alternative = null; |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2208 | for (NetworkAgentInfo existing : mNetworkAgentInfos.values()) { |
| 2209 | if (existing.satisfies(request) && |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2210 | (alternative == null || |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2211 | alternative.getCurrentScore() < existing.getCurrentScore())) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2212 | alternative = existing; |
| 2213 | } |
| 2214 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2215 | if (alternative != null) { |
| 2216 | if (DBG) log(" found replacement in " + alternative.name()); |
| 2217 | if (!toActivate.contains(alternative)) { |
| 2218 | toActivate.add(alternative); |
| 2219 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2220 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2221 | } |
| 2222 | } |
| 2223 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 2224 | removeDataActivityTracking(nai); |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 2225 | notifyLockdownVpn(nai); |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2226 | requestNetworkTransitionWakelock(nai.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2227 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2228 | for (NetworkAgentInfo networkToActivate : toActivate) { |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2229 | unlinger(networkToActivate); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2230 | rematchNetworkAndRequests(networkToActivate, false); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2231 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2232 | } |
| 2233 | } |
| 2234 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2235 | // If this method proves to be too slow then we can maintain a separate |
| 2236 | // pendingIntent => NetworkRequestInfo map. |
| 2237 | // This method assumes that every non-null PendingIntent maps to exactly 1 NetworkRequestInfo. |
| 2238 | private NetworkRequestInfo findExistingNetworkRequestInfo(PendingIntent pendingIntent) { |
| 2239 | Intent intent = pendingIntent.getIntent(); |
| 2240 | for (Map.Entry<NetworkRequest, NetworkRequestInfo> entry : mNetworkRequests.entrySet()) { |
| 2241 | PendingIntent existingPendingIntent = entry.getValue().mPendingIntent; |
| 2242 | if (existingPendingIntent != null && |
| 2243 | existingPendingIntent.getIntent().filterEquals(intent)) { |
| 2244 | return entry.getValue(); |
| 2245 | } |
| 2246 | } |
| 2247 | return null; |
| 2248 | } |
| 2249 | |
| 2250 | private void handleRegisterNetworkRequestWithIntent(Message msg) { |
| 2251 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 2252 | |
| 2253 | NetworkRequestInfo existingRequest = findExistingNetworkRequestInfo(nri.mPendingIntent); |
| 2254 | if (existingRequest != null) { // remove the existing request. |
| 2255 | if (DBG) log("Replacing " + existingRequest.request + " with " |
| 2256 | + nri.request + " because their intents matched."); |
| 2257 | handleReleaseNetworkRequest(existingRequest.request, getCallingUid()); |
| 2258 | } |
| 2259 | handleRegisterNetworkRequest(msg); |
| 2260 | } |
| 2261 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2262 | private void handleRegisterNetworkRequest(Message msg) { |
| 2263 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2264 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2265 | mNetworkRequests.put(nri.request, nri); |
| 2266 | |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2267 | // TODO: This logic may be better replaced with a call to rematchNetworkAndRequests |
| 2268 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2269 | // Check for the best currently alive network that satisfies this request |
| 2270 | NetworkAgentInfo bestNetwork = null; |
| 2271 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2272 | if (DBG) log("handleRegisterNetworkRequest checking " + network.name()); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2273 | if (network.satisfies(nri.request)) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2274 | if (DBG) log("apparently satisfied. currentScore=" + network.getCurrentScore()); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2275 | if (!nri.isRequest) { |
| 2276 | // Not setting bestNetwork here as a listening NetworkRequest may be |
| 2277 | // satisfied by multiple Networks. Instead the request is added to |
| 2278 | // each satisfying Network and notified about each. |
| 2279 | network.addRequest(nri.request); |
| 2280 | notifyNetworkCallback(network, nri); |
| 2281 | } else if (bestNetwork == null || |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2282 | bestNetwork.getCurrentScore() < network.getCurrentScore()) { |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2283 | bestNetwork = network; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2284 | } |
| 2285 | } |
| 2286 | } |
| 2287 | if (bestNetwork != null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2288 | if (DBG) log("using " + bestNetwork.name()); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2289 | unlinger(bestNetwork); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2290 | bestNetwork.addRequest(nri.request); |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 2291 | mNetworkForRequestId.put(nri.request.requestId, bestNetwork); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2292 | notifyNetworkCallback(bestNetwork, nri); |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 2293 | if (nri.request.legacyType != TYPE_NONE) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2294 | mLegacyTypeTracker.add(nri.request.legacyType, bestNetwork); |
| 2295 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2296 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2297 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2298 | if (nri.isRequest) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2299 | if (DBG) log("sending new NetworkRequest to factories"); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2300 | final int score = bestNetwork == null ? 0 : bestNetwork.getCurrentScore(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2301 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2302 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2303 | 0, nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2308 | private void handleReleaseNetworkRequestWithIntent(PendingIntent pendingIntent, |
| 2309 | int callingUid) { |
| 2310 | NetworkRequestInfo nri = findExistingNetworkRequestInfo(pendingIntent); |
| 2311 | if (nri != null) { |
| 2312 | handleReleaseNetworkRequest(nri.request, callingUid); |
| 2313 | } |
| 2314 | } |
| 2315 | |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2316 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) { |
| 2317 | NetworkRequestInfo nri = mNetworkRequests.get(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2318 | if (nri != null) { |
Jeff Davidson | 6f91390 | 2014-08-21 15:54:15 -0700 | [diff] [blame] | 2319 | if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2320 | if (DBG) log("Attempt to release unowned NetworkRequest " + request); |
| 2321 | return; |
| 2322 | } |
| 2323 | if (DBG) log("releasing NetworkRequest " + request); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 2324 | nri.unlinkDeathRecipient(); |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2325 | mNetworkRequests.remove(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2326 | if (nri.isRequest) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2327 | // Find all networks that are satisfying this request and remove the request |
| 2328 | // from their request lists. |
| 2329 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2330 | if (nai.networkRequests.get(nri.request.requestId) != null) { |
| 2331 | nai.networkRequests.remove(nri.request.requestId); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2332 | if (DBG) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2333 | log(" Removing from current network " + nai.name() + |
| 2334 | ", leaving " + nai.networkRequests.size() + |
| 2335 | " requests."); |
| 2336 | } |
| 2337 | // check if has any requests remaining and if not, |
| 2338 | // disconnect (unless it's a VPN). |
| 2339 | boolean keep = nai.isVPN(); |
| 2340 | for (int i = 0; i < nai.networkRequests.size() && !keep; i++) { |
| 2341 | NetworkRequest r = nai.networkRequests.valueAt(i); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2342 | if (isRequest(r)) keep = true; |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2343 | } |
| 2344 | if (!keep) { |
| 2345 | if (DBG) log("no live requests for " + nai.name() + "; disconnecting"); |
| 2346 | nai.asyncChannel.disconnect(); |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2351 | // Maintain the illusion. When this request arrived, we might have pretended |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2352 | // that a network connected to serve it, even though the network was already |
| 2353 | // connected. Now that this request has gone away, we might have to pretend |
| 2354 | // that the network disconnected. LegacyTypeTracker will generate that |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2355 | // phantom disconnect for this type. |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2356 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
| 2357 | if (nai != null) { |
| 2358 | mNetworkForRequestId.remove(nri.request.requestId); |
| 2359 | if (nri.request.legacyType != TYPE_NONE) { |
| 2360 | mLegacyTypeTracker.remove(nri.request.legacyType, nai); |
| 2361 | } |
| 2362 | } |
| 2363 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2364 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2365 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 2366 | nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2367 | } |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2368 | } else { |
| 2369 | // listens don't have a singular affectedNetwork. Check all networks to see |
| 2370 | // if this listen request applies and remove it. |
| 2371 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2372 | nai.networkRequests.remove(nri.request.requestId); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED); |
| 2376 | } |
| 2377 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2378 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2379 | private class InternalHandler extends Handler { |
| 2380 | public InternalHandler(Looper looper) { |
| 2381 | super(looper); |
| 2382 | } |
| 2383 | |
| 2384 | @Override |
| 2385 | public void handleMessage(Message msg) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2386 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2387 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2388 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2389 | String causedBy = null; |
| 2390 | synchronized (ConnectivityService.this) { |
| 2391 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 2392 | mNetTransitionWakeLock.isHeld()) { |
| 2393 | mNetTransitionWakeLock.release(); |
| 2394 | causedBy = mNetTransitionWakeLockCausedBy; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2395 | } else { |
| 2396 | break; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2397 | } |
| 2398 | } |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2399 | if (msg.what == EVENT_EXPIRE_NET_TRANSITION_WAKELOCK) { |
| 2400 | log("Failed to find a new network - expiring NetTransition Wakelock"); |
| 2401 | } else { |
| 2402 | log("NetTransition Wakelock (" + (causedBy == null ? "unknown" : causedBy) + |
| 2403 | " cleared because we found a replacement network"); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2404 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 2405 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2406 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2407 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2408 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2409 | break; |
| 2410 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2411 | case EVENT_SET_DEPENDENCY_MET: { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2412 | boolean met = (msg.arg1 == ENABLED); |
| 2413 | handleSetDependencyMet(msg.arg2, met); |
| 2414 | break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2415 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2416 | case EVENT_SEND_STICKY_BROADCAST_INTENT: { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2417 | Intent intent = (Intent)msg.obj; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2418 | sendStickyBroadcast(intent); |
| 2419 | break; |
| 2420 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2421 | case EVENT_SET_POLICY_DATA_ENABLE: { |
| 2422 | final int networkType = msg.arg1; |
| 2423 | final boolean enabled = msg.arg2 == ENABLED; |
| 2424 | handleSetPolicyDataEnable(networkType, enabled); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2425 | break; |
| 2426 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2427 | case EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: { |
| 2428 | int tag = mEnableFailFastMobileDataTag.get(); |
| 2429 | if (msg.arg1 == tag) { |
| 2430 | MobileDataStateTracker mobileDst = |
| 2431 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 2432 | if (mobileDst != null) { |
| 2433 | mobileDst.setEnableFailFastMobileData(msg.arg2); |
| 2434 | } |
| 2435 | } else { |
| 2436 | log("EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: stale arg1:" + msg.arg1 |
| 2437 | + " != tag:" + tag); |
| 2438 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2439 | break; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2440 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2441 | case EVENT_SAMPLE_INTERVAL_ELAPSED: { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2442 | handleNetworkSamplingTimeout(); |
| 2443 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2444 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2445 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2446 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2447 | break; |
| 2448 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2449 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2450 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 2451 | break; |
| 2452 | } |
| 2453 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 2454 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2455 | break; |
| 2456 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2457 | case EVENT_REGISTER_NETWORK_AGENT: { |
| 2458 | handleRegisterNetworkAgent((NetworkAgentInfo)msg.obj); |
| 2459 | break; |
| 2460 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2461 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 2462 | case EVENT_REGISTER_NETWORK_LISTENER: { |
| 2463 | handleRegisterNetworkRequest(msg); |
| 2464 | break; |
| 2465 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2466 | case EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT: { |
| 2467 | handleRegisterNetworkRequestWithIntent(msg); |
| 2468 | break; |
| 2469 | } |
| 2470 | case EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT: { |
| 2471 | handleReleaseNetworkRequestWithIntent((PendingIntent) msg.obj, msg.arg1); |
| 2472 | break; |
| 2473 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2474 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2475 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2476 | break; |
| 2477 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2478 | case EVENT_SYSTEM_READY: { |
| 2479 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2480 | nai.networkMonitor.systemReady = true; |
| 2481 | } |
| 2482 | break; |
| 2483 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2487 | |
| 2488 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2489 | public int tether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2490 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2491 | if (isTetheringSupported()) { |
| 2492 | return mTethering.tether(iface); |
| 2493 | } else { |
| 2494 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2495 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2499 | public int untether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2500 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2501 | |
| 2502 | if (isTetheringSupported()) { |
| 2503 | return mTethering.untether(iface); |
| 2504 | } else { |
| 2505 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | // javadoc from interface |
| 2510 | public int getLastTetherError(String iface) { |
| 2511 | enforceTetherAccessPermission(); |
| 2512 | |
| 2513 | if (isTetheringSupported()) { |
| 2514 | return mTethering.getLastTetherError(iface); |
| 2515 | } else { |
| 2516 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2517 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | // TODO - proper iface API for selection by property, inspection, etc |
| 2521 | public String[] getTetherableUsbRegexs() { |
| 2522 | enforceTetherAccessPermission(); |
| 2523 | if (isTetheringSupported()) { |
| 2524 | return mTethering.getTetherableUsbRegexs(); |
| 2525 | } else { |
| 2526 | return new String[0]; |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | public String[] getTetherableWifiRegexs() { |
| 2531 | enforceTetherAccessPermission(); |
| 2532 | if (isTetheringSupported()) { |
| 2533 | return mTethering.getTetherableWifiRegexs(); |
| 2534 | } else { |
| 2535 | return new String[0]; |
| 2536 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2537 | } |
| 2538 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2539 | public String[] getTetherableBluetoothRegexs() { |
| 2540 | enforceTetherAccessPermission(); |
| 2541 | if (isTetheringSupported()) { |
| 2542 | return mTethering.getTetherableBluetoothRegexs(); |
| 2543 | } else { |
| 2544 | return new String[0]; |
| 2545 | } |
| 2546 | } |
| 2547 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2548 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2549 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2550 | if (isTetheringSupported()) { |
| 2551 | return mTethering.setUsbTethering(enable); |
| 2552 | } else { |
| 2553 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2554 | } |
| 2555 | } |
| 2556 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2557 | // TODO - move iface listing, queries, etc to new module |
| 2558 | // javadoc from interface |
| 2559 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2560 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2561 | return mTethering.getTetherableIfaces(); |
| 2562 | } |
| 2563 | |
| 2564 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2565 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2566 | return mTethering.getTetheredIfaces(); |
| 2567 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2568 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2569 | public String[] getTetheringErroredIfaces() { |
| 2570 | enforceTetherAccessPermission(); |
| 2571 | return mTethering.getErroredIfaces(); |
| 2572 | } |
| 2573 | |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2574 | public String[] getTetheredDhcpRanges() { |
| 2575 | enforceConnectivityInternalPermission(); |
| 2576 | return mTethering.getTetheredDhcpRanges(); |
| 2577 | } |
| 2578 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2579 | // if ro.tether.denied = true we default to no tethering |
| 2580 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 2581 | // had previously been turned off. |
| 2582 | public boolean isTetheringSupported() { |
| 2583 | enforceTetherAccessPermission(); |
| 2584 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2585 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 2586 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0) |
| 2587 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 2588 | return tetherEnabledInSettings && ((mTethering.getTetherableUsbRegexs().length != 0 || |
| 2589 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 2590 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
| 2591 | mTethering.getUpstreamIfaceTypes().length != 0); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2592 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2593 | |
Robert Greenwalt | 17c3e0f | 2014-07-02 09:59:16 -0700 | [diff] [blame] | 2594 | // Called when we lose the default network and have no replacement yet. |
| 2595 | // This will automatically be cleared after X seconds or a new default network |
| 2596 | // becomes CONNECTED, whichever happens first. The timer is started by the |
| 2597 | // first caller and not restarted by subsequent callers. |
| 2598 | private void requestNetworkTransitionWakelock(String forWhom) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2599 | int serialNum = 0; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2600 | synchronized (this) { |
| 2601 | if (mNetTransitionWakeLock.isHeld()) return; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2602 | serialNum = ++mNetTransitionWakeLockSerialNumber; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2603 | mNetTransitionWakeLock.acquire(); |
| 2604 | mNetTransitionWakeLockCausedBy = forWhom; |
| 2605 | } |
| 2606 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2607 | EVENT_EXPIRE_NET_TRANSITION_WAKELOCK, serialNum, 0), |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2608 | mNetTransitionWakeLockTimeout); |
| 2609 | return; |
| 2610 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 2611 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2612 | // 100 percent is full good, 0 is full bad. |
| 2613 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2614 | if (percentage > 50) return; // don't handle good network reports |
| 2615 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 2616 | if (nai != null) reportBadNetwork(nai.network); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2617 | } |
| 2618 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2619 | public void reportBadNetwork(Network network) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2620 | enforceAccessPermission(); |
| 2621 | enforceInternetPermission(); |
| 2622 | |
| 2623 | if (network == null) return; |
| 2624 | |
| 2625 | final int uid = Binder.getCallingUid(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 2626 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2627 | if (nai == null) return; |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2628 | if (DBG) log("reportBadNetwork(" + nai.name() + ") by " + uid); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2629 | synchronized (nai) { |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2630 | // Validating an uncreated network could result in a call to rematchNetworkAndRequests() |
| 2631 | // which isn't meant to work on uncreated networks. |
| 2632 | if (!nai.created) return; |
| 2633 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 2634 | if (isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid)) return; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2635 | |
| 2636 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_FORCE_REEVALUATION, uid); |
| 2637 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2640 | public ProxyInfo getProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2641 | // this information is already available as a world read/writable jvm property |
| 2642 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2643 | // of proxy info to all the JVMs. |
| 2644 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2645 | synchronized (mProxyLock) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2646 | ProxyInfo ret = mGlobalProxy; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2647 | if ((ret == null) && !mDefaultProxyDisabled) ret = mDefaultProxy; |
| 2648 | return ret; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2649 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2650 | } |
| 2651 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2652 | public void setGlobalProxy(ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2653 | enforceConnectivityInternalPermission(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2654 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2655 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2656 | if (proxyProperties == mGlobalProxy) return; |
| 2657 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 2658 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 2659 | |
| 2660 | String host = ""; |
| 2661 | int port = 0; |
| 2662 | String exclList = ""; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2663 | String pacFileUrl = ""; |
| 2664 | if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2665 | !Uri.EMPTY.equals(proxyProperties.getPacFileUrl()))) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2666 | if (!proxyProperties.isValid()) { |
| 2667 | if (DBG) |
| 2668 | log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2669 | return; |
| 2670 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2671 | mGlobalProxy = new ProxyInfo(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2672 | host = mGlobalProxy.getHost(); |
| 2673 | port = mGlobalProxy.getPort(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2674 | exclList = mGlobalProxy.getExclusionListAsString(); |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2675 | if (!Uri.EMPTY.equals(proxyProperties.getPacFileUrl())) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2676 | pacFileUrl = proxyProperties.getPacFileUrl().toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2677 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2678 | } else { |
| 2679 | mGlobalProxy = null; |
| 2680 | } |
| 2681 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2682 | final long token = Binder.clearCallingIdentity(); |
| 2683 | try { |
| 2684 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 2685 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 2686 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 2687 | exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2688 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2689 | } finally { |
| 2690 | Binder.restoreCallingIdentity(token); |
| 2691 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2692 | |
Jason Monk | cf0f97a | 2014-10-02 15:39:38 -0400 | [diff] [blame] | 2693 | if (mGlobalProxy == null) { |
| 2694 | proxyProperties = mDefaultProxy; |
| 2695 | } |
| 2696 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2697 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2700 | private void loadGlobalProxy() { |
| 2701 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2702 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 2703 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 2704 | String exclList = Settings.Global.getString(res, |
| 2705 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2706 | String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC); |
| 2707 | if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2708 | ProxyInfo proxyProperties; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2709 | if (!TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2710 | proxyProperties = new ProxyInfo(pacFileUrl); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2711 | } else { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2712 | proxyProperties = new ProxyInfo(host, port, exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2713 | } |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2714 | if (!proxyProperties.isValid()) { |
| 2715 | if (DBG) log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2716 | return; |
| 2717 | } |
| 2718 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2719 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2720 | mGlobalProxy = proxyProperties; |
| 2721 | } |
| 2722 | } |
| 2723 | } |
| 2724 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2725 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2726 | // this information is already available as a world read/writable jvm property |
| 2727 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2728 | // of proxy info to all the JVMs. |
| 2729 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2730 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2731 | return mGlobalProxy; |
| 2732 | } |
| 2733 | } |
| 2734 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2735 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2736 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2737 | && Uri.EMPTY.equals(proxy.getPacFileUrl())) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2738 | proxy = null; |
| 2739 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2740 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2741 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2742 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Robert Greenwalt | a8dae99 | 2013-11-18 09:43:59 -0800 | [diff] [blame] | 2743 | if (proxy != null && !proxy.isValid()) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2744 | if (DBG) log("Invalid proxy properties, ignoring: " + proxy.toString()); |
| 2745 | return; |
| 2746 | } |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 2747 | |
| 2748 | // This call could be coming from the PacManager, containing the port of the local |
| 2749 | // proxy. If this new proxy matches the global proxy then copy this proxy to the |
| 2750 | // global (to get the correct local port), and send a broadcast. |
| 2751 | // TODO: Switch PacManager to have its own message to send back rather than |
| 2752 | // reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy. |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2753 | if ((mGlobalProxy != null) && (proxy != null) |
| 2754 | && (!Uri.EMPTY.equals(proxy.getPacFileUrl())) |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 2755 | && proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) { |
| 2756 | mGlobalProxy = proxy; |
| 2757 | sendProxyBroadcast(mGlobalProxy); |
| 2758 | return; |
| 2759 | } |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2760 | mDefaultProxy = proxy; |
| 2761 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2762 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2763 | if (!mDefaultProxyDisabled) { |
| 2764 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2765 | } |
| 2766 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2770 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 2771 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2772 | if (!TextUtils.isEmpty(proxy)) { |
| 2773 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 2774 | if (data.length == 0) { |
| 2775 | return; |
| 2776 | } |
| 2777 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2778 | String proxyHost = data[0]; |
| 2779 | int proxyPort = 8080; |
| 2780 | if (data.length > 1) { |
| 2781 | try { |
| 2782 | proxyPort = Integer.parseInt(data[1]); |
| 2783 | } catch (NumberFormatException e) { |
| 2784 | return; |
| 2785 | } |
| 2786 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2787 | ProxyInfo p = new ProxyInfo(data[0], proxyPort, ""); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2788 | setGlobalProxy(p); |
| 2789 | } |
| 2790 | } |
| 2791 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2792 | private void sendProxyBroadcast(ProxyInfo proxy) { |
| 2793 | if (proxy == null) proxy = new ProxyInfo("", 0, ""); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 2794 | if (mPacManager.setCurrentProxyScriptUrl(proxy)) return; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2795 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2796 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 2797 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 2798 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2799 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2800 | final long ident = Binder.clearCallingIdentity(); |
| 2801 | try { |
| 2802 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2803 | } finally { |
| 2804 | Binder.restoreCallingIdentity(ident); |
| 2805 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | private static class SettingsObserver extends ContentObserver { |
| 2809 | private int mWhat; |
| 2810 | private Handler mHandler; |
| 2811 | SettingsObserver(Handler handler, int what) { |
| 2812 | super(handler); |
| 2813 | mHandler = handler; |
| 2814 | mWhat = what; |
| 2815 | } |
| 2816 | |
| 2817 | void observe(Context context) { |
| 2818 | ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2819 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 2820 | Settings.Global.HTTP_PROXY), false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | @Override |
| 2824 | public void onChange(boolean selfChange) { |
| 2825 | mHandler.obtainMessage(mWhat).sendToTarget(); |
| 2826 | } |
| 2827 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2828 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2829 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2830 | Slog.d(TAG, s); |
| 2831 | } |
| 2832 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2833 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2834 | Slog.e(TAG, s); |
| 2835 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2836 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2837 | private static <T> T checkNotNull(T value, String message) { |
| 2838 | if (value == null) { |
| 2839 | throw new NullPointerException(message); |
| 2840 | } |
| 2841 | return value; |
| 2842 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2843 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2844 | /** |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 2845 | * Prepare for a VPN application. |
Jeff Davidson | 9a1da68 | 2014-11-11 13:52:58 -0800 | [diff] [blame] | 2846 | * Permissions are checked in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2847 | * @hide |
| 2848 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2849 | @Override |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 2850 | public boolean prepareVpn(String oldPackage, String newPackage) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2851 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2852 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2853 | synchronized(mVpns) { |
| 2854 | return mVpns.get(user).prepare(oldPackage, newPackage); |
| 2855 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2858 | /** |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 2859 | * Set whether the current VPN package has the ability to launch VPNs without |
Jeff Davidson | 9a1da68 | 2014-11-11 13:52:58 -0800 | [diff] [blame] | 2860 | * user intervention. This method is used by system-privileged apps. |
| 2861 | * Permissions are checked in Vpn class. |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 2862 | * @hide |
| 2863 | */ |
| 2864 | @Override |
| 2865 | public void setVpnPackageAuthorization(boolean authorized) { |
| 2866 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2867 | synchronized(mVpns) { |
| 2868 | mVpns.get(user).setPackageAuthorization(authorized); |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2873 | * Configure a TUN interface and return its file descriptor. Parameters |
| 2874 | * are encoded and opaque to this class. This method is used by VpnBuilder |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2875 | * and not available in ConnectivityManager. Permissions are checked in |
| 2876 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2877 | * @hide |
| 2878 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2879 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2880 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2881 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2882 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2883 | synchronized(mVpns) { |
| 2884 | return mVpns.get(user).establish(config); |
| 2885 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 2888 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 2889 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 2890 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 2891 | */ |
| 2892 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 2893 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2894 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 2895 | final LinkProperties egress = getActiveLinkProperties(); |
| 2896 | if (egress == null) { |
| 2897 | throw new IllegalStateException("Missing active network connection"); |
| 2898 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2899 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2900 | synchronized(mVpns) { |
| 2901 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 2902 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | /** |
| 2906 | * Return the information of the ongoing legacy VPN. This method is used |
| 2907 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 2908 | * are checked in Vpn class. |
| 2909 | * @hide |
| 2910 | */ |
| 2911 | @Override |
| 2912 | public LegacyVpnInfo getLegacyVpnInfo() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2913 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2914 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2915 | synchronized(mVpns) { |
| 2916 | return mVpns.get(user).getLegacyVpnInfo(); |
| 2917 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2920 | /** |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 2921 | * Returns the information of the ongoing VPN. This method is used by VpnDialogs and |
| 2922 | * not available in ConnectivityManager. |
| 2923 | * Permissions are checked in Vpn class. |
| 2924 | * @hide |
| 2925 | */ |
| 2926 | @Override |
| 2927 | public VpnConfig getVpnConfig() { |
| 2928 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2929 | synchronized(mVpns) { |
| 2930 | return mVpns.get(user).getVpnConfig(); |
| 2931 | } |
| 2932 | } |
| 2933 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2934 | @Override |
| 2935 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 2936 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 2937 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 2938 | return false; |
| 2939 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2940 | |
| 2941 | // Tear down existing lockdown if profile was removed |
| 2942 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 2943 | if (mLockdownEnabled) { |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 2944 | if (!mKeyStore.isUnlocked()) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2945 | Slog.w(TAG, "KeyStore locked; unable to create LockdownTracker"); |
| 2946 | return false; |
| 2947 | } |
| 2948 | |
| 2949 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 2950 | final VpnProfile profile = VpnProfile.decode( |
| 2951 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2952 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2953 | synchronized(mVpns) { |
| 2954 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, mVpns.get(user), |
| 2955 | profile)); |
| 2956 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2957 | } else { |
| 2958 | setLockdownTracker(null); |
| 2959 | } |
| 2960 | |
| 2961 | return true; |
| 2962 | } |
| 2963 | |
| 2964 | /** |
| 2965 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 2966 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 2967 | */ |
| 2968 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 2969 | // Shutdown any existing tracker |
| 2970 | final LockdownVpnTracker existing = mLockdownTracker; |
| 2971 | mLockdownTracker = null; |
| 2972 | if (existing != null) { |
| 2973 | existing.shutdown(); |
| 2974 | } |
| 2975 | |
| 2976 | try { |
| 2977 | if (tracker != null) { |
| 2978 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 2979 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2980 | mLockdownTracker = tracker; |
| 2981 | mLockdownTracker.init(); |
| 2982 | } else { |
| 2983 | mNetd.setFirewallEnabled(false); |
| 2984 | } |
| 2985 | } catch (RemoteException e) { |
| 2986 | // ignored; NMS lives inside system_server |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | private void throwIfLockdownEnabled() { |
| 2991 | if (mLockdownEnabled) { |
| 2992 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 2993 | } |
| 2994 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 2995 | |
| 2996 | public void supplyMessenger(int networkType, Messenger messenger) { |
| 2997 | enforceConnectivityInternalPermission(); |
| 2998 | |
| 2999 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
| 3000 | mNetTrackers[networkType].supplyMessenger(messenger); |
| 3001 | } |
| 3002 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 3003 | |
| 3004 | public int findConnectionTypeForIface(String iface) { |
| 3005 | enforceConnectivityInternalPermission(); |
| 3006 | |
| 3007 | if (TextUtils.isEmpty(iface)) return ConnectivityManager.TYPE_NONE; |
Lorenzo Colitti | 1c3a35b | 2014-08-19 21:31:08 -0700 | [diff] [blame] | 3008 | |
| 3009 | synchronized(mNetworkForNetId) { |
| 3010 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
| 3011 | NetworkAgentInfo nai = mNetworkForNetId.valueAt(i); |
| 3012 | LinkProperties lp = nai.linkProperties; |
| 3013 | if (lp != null && iface.equals(lp.getInterfaceName()) && nai.networkInfo != null) { |
| 3014 | return nai.networkInfo.getType(); |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | } |
| 3018 | return ConnectivityManager.TYPE_NONE; |
| 3019 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3020 | |
| 3021 | /** |
| 3022 | * Have mobile data fail fast if enabled. |
| 3023 | * |
| 3024 | * @param enabled DctConstants.ENABLED/DISABLED |
| 3025 | */ |
| 3026 | private void setEnableFailFastMobileData(int enabled) { |
| 3027 | int tag; |
| 3028 | |
| 3029 | if (enabled == DctConstants.ENABLED) { |
| 3030 | tag = mEnableFailFastMobileDataTag.incrementAndGet(); |
| 3031 | } else { |
| 3032 | tag = mEnableFailFastMobileDataTag.get(); |
| 3033 | } |
| 3034 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_ENABLE_FAIL_FAST_MOBILE_DATA, tag, |
| 3035 | enabled)); |
| 3036 | } |
| 3037 | |
| 3038 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3039 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3040 | // TODO: Remove? Any reason to trigger a provisioning check? |
| 3041 | return -1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
| 3045 | private volatile boolean mIsNotificationVisible = false; |
| 3046 | |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3047 | private void setProvNotificationVisible(boolean visible, int networkType, String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3048 | if (DBG) { |
| 3049 | log("setProvNotificationVisible: E visible=" + visible + " networkType=" + networkType |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3050 | + " action=" + action); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3051 | } |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3052 | Intent intent = new Intent(action); |
| 3053 | PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3054 | // Concatenate the range of types onto the range of NetIDs. |
| 3055 | int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3056 | setProvNotificationVisibleIntent(visible, id, networkType, null, pendingIntent); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3057 | } |
| 3058 | |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3059 | /** |
| 3060 | * Show or hide network provisioning notificaitons. |
| 3061 | * |
| 3062 | * @param id an identifier that uniquely identifies this notification. This must match |
| 3063 | * between show and hide calls. We use the NetID value but for legacy callers |
| 3064 | * we concatenate the range of types with the range of NetIDs. |
| 3065 | */ |
| 3066 | private void setProvNotificationVisibleIntent(boolean visible, int id, int networkType, |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3067 | String extraInfo, PendingIntent intent) { |
| 3068 | if (DBG) { |
| 3069 | log("setProvNotificationVisibleIntent: E visible=" + visible + " networkType=" + |
| 3070 | networkType + " extraInfo=" + extraInfo); |
| 3071 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3072 | |
| 3073 | Resources r = Resources.getSystem(); |
| 3074 | NotificationManager notificationManager = (NotificationManager) mContext |
| 3075 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 3076 | |
| 3077 | if (visible) { |
| 3078 | CharSequence title; |
| 3079 | CharSequence details; |
| 3080 | int icon; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3081 | Notification notification = new Notification(); |
| 3082 | switch (networkType) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3083 | case ConnectivityManager.TYPE_WIFI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3084 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 3085 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3086 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3087 | icon = R.drawable.stat_notify_wifi_in_range; |
| 3088 | break; |
| 3089 | case ConnectivityManager.TYPE_MOBILE: |
| 3090 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3091 | title = r.getString(R.string.network_available_sign_in, 0); |
| 3092 | // TODO: Change this to pull from NetworkInfo once a printable |
| 3093 | // name has been added to it |
| 3094 | details = mTelephonyManager.getNetworkOperatorName(); |
| 3095 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3096 | break; |
| 3097 | default: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3098 | title = r.getString(R.string.network_available_sign_in, 0); |
| 3099 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3100 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3101 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3102 | break; |
| 3103 | } |
| 3104 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3105 | notification.when = 0; |
| 3106 | notification.icon = icon; |
| 3107 | notification.flags = Notification.FLAG_AUTO_CANCEL; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3108 | notification.tickerText = title; |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 3109 | notification.color = mContext.getResources().getColor( |
| 3110 | com.android.internal.R.color.system_notification_accent_color); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3111 | notification.setLatestEventInfo(mContext, title, details, notification.contentIntent); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3112 | notification.contentIntent = intent; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3113 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3114 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3115 | notificationManager.notify(NOTIFICATION_ID, id, notification); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3116 | } catch (NullPointerException npe) { |
| 3117 | loge("setNotificaitionVisible: visible notificationManager npe=" + npe); |
| 3118 | npe.printStackTrace(); |
| 3119 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3120 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3121 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3122 | notificationManager.cancel(NOTIFICATION_ID, id); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3123 | } catch (NullPointerException npe) { |
| 3124 | loge("setNotificaitionVisible: cancel notificationManager npe=" + npe); |
| 3125 | npe.printStackTrace(); |
| 3126 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3127 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3128 | mIsNotificationVisible = visible; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3131 | /** Location to an updatable file listing carrier provisioning urls. |
| 3132 | * An example: |
| 3133 | * |
| 3134 | * <?xml version="1.0" encoding="utf-8"?> |
| 3135 | * <provisioningUrls> |
| 3136 | * <provisioningUrl mcc="310" mnc="4">http://myserver.com/foo?mdn=%3$s&iccid=%1$s&imei=%2$s</provisioningUrl> |
| 3137 | * <redirectedUrl mcc="310" mnc="4">http://www.google.com</redirectedUrl> |
| 3138 | * </provisioningUrls> |
| 3139 | */ |
| 3140 | private static final String PROVISIONING_URL_PATH = |
| 3141 | "/data/misc/radio/provisioning_urls.xml"; |
| 3142 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3143 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3144 | /** XML tag for root element. */ |
| 3145 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 3146 | /** XML tag for individual url */ |
| 3147 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
| 3148 | /** XML tag for redirected url */ |
| 3149 | private static final String TAG_REDIRECTED_URL = "redirectedUrl"; |
| 3150 | /** XML attribute for mcc */ |
| 3151 | private static final String ATTR_MCC = "mcc"; |
| 3152 | /** XML attribute for mnc */ |
| 3153 | private static final String ATTR_MNC = "mnc"; |
| 3154 | |
| 3155 | private static final int REDIRECTED_PROVISIONING = 1; |
| 3156 | private static final int PROVISIONING = 2; |
| 3157 | |
| 3158 | private String getProvisioningUrlBaseFromFile(int type) { |
| 3159 | FileReader fileReader = null; |
| 3160 | XmlPullParser parser = null; |
| 3161 | Configuration config = mContext.getResources().getConfiguration(); |
| 3162 | String tagType; |
| 3163 | |
| 3164 | switch (type) { |
| 3165 | case PROVISIONING: |
| 3166 | tagType = TAG_PROVISIONING_URL; |
| 3167 | break; |
| 3168 | case REDIRECTED_PROVISIONING: |
| 3169 | tagType = TAG_REDIRECTED_URL; |
| 3170 | break; |
| 3171 | default: |
| 3172 | throw new RuntimeException("getProvisioningUrlBaseFromFile: Unexpected parameter " + |
| 3173 | type); |
| 3174 | } |
| 3175 | |
| 3176 | try { |
| 3177 | fileReader = new FileReader(mProvisioningUrlFile); |
| 3178 | parser = Xml.newPullParser(); |
| 3179 | parser.setInput(fileReader); |
| 3180 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 3181 | |
| 3182 | while (true) { |
| 3183 | XmlUtils.nextElement(parser); |
| 3184 | |
| 3185 | String element = parser.getName(); |
| 3186 | if (element == null) break; |
| 3187 | |
| 3188 | if (element.equals(tagType)) { |
| 3189 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 3190 | try { |
| 3191 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 3192 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 3193 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 3194 | parser.next(); |
| 3195 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 3196 | return parser.getText(); |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | } catch (NumberFormatException e) { |
| 3201 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 3202 | } |
| 3203 | } |
| 3204 | } |
| 3205 | return null; |
| 3206 | } catch (FileNotFoundException e) { |
| 3207 | loge("Carrier Provisioning Urls file not found"); |
| 3208 | } catch (XmlPullParserException e) { |
| 3209 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 3210 | } catch (IOException e) { |
| 3211 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 3212 | } finally { |
| 3213 | if (fileReader != null) { |
| 3214 | try { |
| 3215 | fileReader.close(); |
| 3216 | } catch (IOException e) {} |
| 3217 | } |
| 3218 | } |
| 3219 | return null; |
| 3220 | } |
| 3221 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3222 | @Override |
| 3223 | public String getMobileRedirectedProvisioningUrl() { |
| 3224 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3225 | String url = getProvisioningUrlBaseFromFile(REDIRECTED_PROVISIONING); |
| 3226 | if (TextUtils.isEmpty(url)) { |
| 3227 | url = mContext.getResources().getString(R.string.mobile_redirected_provisioning_url); |
| 3228 | } |
| 3229 | return url; |
| 3230 | } |
| 3231 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3232 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3233 | public String getMobileProvisioningUrl() { |
| 3234 | enforceConnectivityInternalPermission(); |
| 3235 | String url = getProvisioningUrlBaseFromFile(PROVISIONING); |
| 3236 | if (TextUtils.isEmpty(url)) { |
| 3237 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3238 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3239 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3240 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3241 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3242 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3243 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3244 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 3245 | if (TextUtils.isEmpty(phoneNumber)) { |
| 3246 | phoneNumber = "0000000000"; |
| 3247 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3248 | url = String.format(url, |
| 3249 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 3250 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3251 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3254 | return url; |
| 3255 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3256 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3257 | @Override |
| 3258 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3259 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3260 | enforceConnectivityInternalPermission(); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3261 | final long ident = Binder.clearCallingIdentity(); |
| 3262 | try { |
| 3263 | setProvNotificationVisible(visible, networkType, action); |
| 3264 | } finally { |
| 3265 | Binder.restoreCallingIdentity(ident); |
| 3266 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3267 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 3268 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3269 | @Override |
| 3270 | public void setAirplaneMode(boolean enable) { |
| 3271 | enforceConnectivityInternalPermission(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3272 | final long ident = Binder.clearCallingIdentity(); |
| 3273 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 3274 | final ContentResolver cr = mContext.getContentResolver(); |
| 3275 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); |
| 3276 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 3277 | intent.putExtra("state", enable); |
xinhe | 98e25fc | 2014-11-17 11:35:01 -0800 | [diff] [blame] | 3278 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3279 | } finally { |
| 3280 | Binder.restoreCallingIdentity(ident); |
| 3281 | } |
| 3282 | } |
| 3283 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3284 | private void onUserStart(int userId) { |
| 3285 | synchronized(mVpns) { |
| 3286 | Vpn userVpn = mVpns.get(userId); |
| 3287 | if (userVpn != null) { |
| 3288 | loge("Starting user already has a VPN"); |
| 3289 | return; |
| 3290 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 3291 | userVpn = new Vpn(mHandler.getLooper(), mContext, mNetd, this, userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3292 | mVpns.put(userId, userVpn); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | private void onUserStop(int userId) { |
| 3297 | synchronized(mVpns) { |
| 3298 | Vpn userVpn = mVpns.get(userId); |
| 3299 | if (userVpn == null) { |
| 3300 | loge("Stopping user has no VPN"); |
| 3301 | return; |
| 3302 | } |
| 3303 | mVpns.delete(userId); |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 3308 | @Override |
| 3309 | public void onReceive(Context context, Intent intent) { |
| 3310 | final String action = intent.getAction(); |
| 3311 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 3312 | if (userId == UserHandle.USER_NULL) return; |
| 3313 | |
| 3314 | if (Intent.ACTION_USER_STARTING.equals(action)) { |
| 3315 | onUserStart(userId); |
| 3316 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 3317 | onUserStop(userId); |
| 3318 | } |
| 3319 | } |
| 3320 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3321 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3322 | /* Infrastructure for network sampling */ |
| 3323 | |
| 3324 | private void handleNetworkSamplingTimeout() { |
| 3325 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3326 | if (SAMPLE_DBG) log("Sampling interval elapsed, updating statistics .."); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3327 | |
| 3328 | // initialize list of interfaces .. |
| 3329 | Map<String, SamplingDataTracker.SamplingSnapshot> mapIfaceToSample = |
| 3330 | new HashMap<String, SamplingDataTracker.SamplingSnapshot>(); |
| 3331 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 3332 | if (tracker != null) { |
| 3333 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 3334 | if (ifaceName != null) { |
| 3335 | mapIfaceToSample.put(ifaceName, null); |
| 3336 | } |
| 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | // Read samples for all interfaces |
| 3341 | SamplingDataTracker.getSamplingSnapshots(mapIfaceToSample); |
| 3342 | |
| 3343 | // process samples for all networks |
| 3344 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 3345 | if (tracker != null) { |
| 3346 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 3347 | SamplingDataTracker.SamplingSnapshot ss = mapIfaceToSample.get(ifaceName); |
| 3348 | if (ss != null) { |
| 3349 | // end the previous sampling cycle |
| 3350 | tracker.stopSampling(ss); |
| 3351 | // start a new sampling cycle .. |
| 3352 | tracker.startSampling(ss); |
| 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3357 | if (SAMPLE_DBG) log("Done."); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3358 | |
| 3359 | int samplingIntervalInSeconds = Settings.Global.getInt(mContext.getContentResolver(), |
| 3360 | Settings.Global.CONNECTIVITY_SAMPLING_INTERVAL_IN_SECONDS, |
| 3361 | DEFAULT_SAMPLING_INTERVAL_IN_SECONDS); |
| 3362 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3363 | if (SAMPLE_DBG) { |
| 3364 | log("Setting timer for " + String.valueOf(samplingIntervalInSeconds) + "seconds"); |
| 3365 | } |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3366 | |
| 3367 | setAlarm(samplingIntervalInSeconds * 1000, mSampleIntervalElapsedIntent); |
| 3368 | } |
| 3369 | |
Aaron Whyte | f5e6747 | 2014-07-10 14:05:35 -0700 | [diff] [blame] | 3370 | /** |
| 3371 | * Sets a network sampling alarm. |
| 3372 | */ |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3373 | void setAlarm(int timeoutInMilliseconds, PendingIntent intent) { |
| 3374 | long wakeupTime = SystemClock.elapsedRealtime() + timeoutInMilliseconds; |
Aaron Whyte | f5e6747 | 2014-07-10 14:05:35 -0700 | [diff] [blame] | 3375 | int alarmType; |
| 3376 | if (Resources.getSystem().getBoolean( |
| 3377 | R.bool.config_networkSamplingWakesDevice)) { |
| 3378 | alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; |
| 3379 | } else { |
| 3380 | alarmType = AlarmManager.ELAPSED_REALTIME; |
| 3381 | } |
| 3382 | mAlarmManager.set(alarmType, wakeupTime, intent); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3383 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3384 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3385 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = |
| 3386 | new HashMap<Messenger, NetworkFactoryInfo>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3387 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = |
| 3388 | new HashMap<NetworkRequest, NetworkRequestInfo>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3389 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3390 | private static class NetworkFactoryInfo { |
| 3391 | public final String name; |
| 3392 | public final Messenger messenger; |
| 3393 | public final AsyncChannel asyncChannel; |
| 3394 | |
| 3395 | public NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel) { |
| 3396 | this.name = name; |
| 3397 | this.messenger = messenger; |
| 3398 | this.asyncChannel = asyncChannel; |
| 3399 | } |
| 3400 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3401 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3402 | /** |
| 3403 | * Tracks info about the requester. |
| 3404 | * Also used to notice when the calling process dies so we can self-expire |
| 3405 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3406 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
| 3407 | static final boolean REQUEST = true; |
| 3408 | static final boolean LISTEN = false; |
| 3409 | |
| 3410 | final NetworkRequest request; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3411 | final PendingIntent mPendingIntent; |
| 3412 | private final IBinder mBinder; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3413 | final int mPid; |
| 3414 | final int mUid; |
| 3415 | final Messenger messenger; |
| 3416 | final boolean isRequest; |
| 3417 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3418 | NetworkRequestInfo(NetworkRequest r, PendingIntent pi, boolean isRequest) { |
| 3419 | request = r; |
| 3420 | mPendingIntent = pi; |
| 3421 | messenger = null; |
| 3422 | mBinder = null; |
| 3423 | mPid = getCallingPid(); |
| 3424 | mUid = getCallingUid(); |
| 3425 | this.isRequest = isRequest; |
| 3426 | } |
| 3427 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3428 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder, boolean isRequest) { |
| 3429 | super(); |
| 3430 | messenger = m; |
| 3431 | request = r; |
| 3432 | mBinder = binder; |
| 3433 | mPid = getCallingPid(); |
| 3434 | mUid = getCallingUid(); |
| 3435 | this.isRequest = isRequest; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3436 | mPendingIntent = null; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3437 | |
| 3438 | try { |
| 3439 | mBinder.linkToDeath(this, 0); |
| 3440 | } catch (RemoteException e) { |
| 3441 | binderDied(); |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | void unlinkDeathRecipient() { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3446 | if (mBinder != null) { |
| 3447 | mBinder.unlinkToDeath(this, 0); |
| 3448 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | public void binderDied() { |
| 3452 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 3453 | request + ", " + mBinder + ")"); |
| 3454 | releaseNetworkRequest(request); |
| 3455 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3456 | |
| 3457 | public String toString() { |
| 3458 | return (isRequest ? "Request" : "Listen") + " from uid/pid:" + mUid + "/" + |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3459 | mPid + " for " + request + |
| 3460 | (mPendingIntent == null ? "" : " to trigger " + mPendingIntent); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3461 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | @Override |
| 3465 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3466 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3467 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3468 | enforceNetworkRequestPermissions(networkCapabilities); |
| 3469 | enforceMeteredApnPolicy(networkCapabilities); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3470 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3471 | if (timeoutMs < 0 || timeoutMs > ConnectivityManager.MAX_NETWORK_REQUEST_TIMEOUT_MS) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3472 | throw new IllegalArgumentException("Bad timeout specified"); |
| 3473 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3474 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3475 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType, |
| 3476 | nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3477 | if (DBG) log("requestNetwork for " + networkRequest); |
| 3478 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 3479 | NetworkRequestInfo.REQUEST); |
| 3480 | |
| 3481 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3482 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3483 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3484 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3485 | } |
| 3486 | return networkRequest; |
| 3487 | } |
| 3488 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3489 | private void enforceNetworkRequestPermissions(NetworkCapabilities networkCapabilities) { |
| 3490 | if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) |
| 3491 | == false) { |
| 3492 | enforceConnectivityInternalPermission(); |
| 3493 | } else { |
| 3494 | enforceChangePermission(); |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | private void enforceMeteredApnPolicy(NetworkCapabilities networkCapabilities) { |
| 3499 | // if UID is restricted, don't allow them to bring up metered APNs |
| 3500 | if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) |
| 3501 | == false) { |
| 3502 | final int uidRules; |
| 3503 | final int uid = Binder.getCallingUid(); |
| 3504 | synchronized(mRulesLock) { |
| 3505 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 3506 | } |
| 3507 | if ((uidRules & RULE_REJECT_METERED) != 0) { |
| 3508 | // we could silently fail or we can filter the available nets to only give |
| 3509 | // them those they have access to. Chose the more useful |
| 3510 | networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 3511 | } |
| 3512 | } |
| 3513 | } |
| 3514 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3515 | @Override |
| 3516 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 3517 | PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3518 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 3519 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 3520 | enforceNetworkRequestPermissions(networkCapabilities); |
| 3521 | enforceMeteredApnPolicy(networkCapabilities); |
| 3522 | |
| 3523 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, |
| 3524 | nextNetworkRequestId()); |
| 3525 | if (DBG) log("pendingRequest for " + networkRequest + " to trigger " + operation); |
| 3526 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation, |
| 3527 | NetworkRequestInfo.REQUEST); |
| 3528 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, |
| 3529 | nri)); |
| 3530 | return networkRequest; |
| 3531 | } |
| 3532 | |
| 3533 | @Override |
| 3534 | public void releasePendingNetworkRequest(PendingIntent operation) { |
| 3535 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 3536 | getCallingUid(), 0, operation)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | @Override |
| 3540 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 3541 | Messenger messenger, IBinder binder) { |
| 3542 | enforceAccessPermission(); |
| 3543 | |
| 3544 | NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3545 | networkCapabilities), TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3546 | if (DBG) log("listenForNetwork for " + networkRequest); |
| 3547 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 3548 | NetworkRequestInfo.LISTEN); |
| 3549 | |
| 3550 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 3551 | return networkRequest; |
| 3552 | } |
| 3553 | |
| 3554 | @Override |
| 3555 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 3556 | PendingIntent operation) { |
| 3557 | } |
| 3558 | |
| 3559 | @Override |
| 3560 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3561 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), |
| 3562 | 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | @Override |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3566 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3567 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3568 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel()); |
| 3569 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3570 | } |
| 3571 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3572 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3573 | if (DBG) log("Got NetworkFactory Messenger for " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3574 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 3575 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 3576 | } |
| 3577 | |
| 3578 | @Override |
| 3579 | public void unregisterNetworkFactory(Messenger messenger) { |
| 3580 | enforceConnectivityInternalPermission(); |
| 3581 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 3582 | } |
| 3583 | |
| 3584 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 3585 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 3586 | if (nfi == null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3587 | loge("Failed to find Messenger in unregisterNetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3588 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3589 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3590 | if (DBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3593 | /** |
| 3594 | * NetworkAgentInfo supporting a request by requestId. |
| 3595 | * These have already been vetted (their Capabilities satisfy the request) |
| 3596 | * and the are the highest scored network available. |
| 3597 | * the are keyed off the Requests requestId. |
| 3598 | */ |
| 3599 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = |
| 3600 | new SparseArray<NetworkAgentInfo>(); |
| 3601 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3602 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = |
| 3603 | new SparseArray<NetworkAgentInfo>(); |
| 3604 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3605 | // NetworkAgentInfo keyed off its connecting messenger |
| 3606 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
| 3607 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = |
| 3608 | new HashMap<Messenger, NetworkAgentInfo>(); |
| 3609 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 3610 | // Note: if mDefaultRequest is changed, NetworkMonitor needs to be updated. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3611 | private final NetworkRequest mDefaultRequest; |
| 3612 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 3613 | private NetworkAgentInfo getDefaultNetwork() { |
| 3614 | return mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 3615 | } |
| 3616 | |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 3617 | private boolean isDefaultNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 3618 | return nai == getDefaultNetwork(); |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3621 | public void registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
| 3622 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 3623 | int currentScore, NetworkMisc networkMisc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3624 | enforceConnectivityInternalPermission(); |
| 3625 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 3626 | // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network |
| 3627 | // satisfies mDefaultRequest. |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 3628 | NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3629 | new NetworkInfo(networkInfo), new LinkProperties(linkProperties), |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 3630 | new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler, |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 3631 | new NetworkMisc(networkMisc), mDefaultRequest); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3632 | synchronized (this) { |
| 3633 | nai.networkMonitor.systemReady = mSystemReady; |
| 3634 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3635 | if (DBG) log("registerNetworkAgent " + nai); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3636 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai)); |
| 3637 | } |
| 3638 | |
| 3639 | private void handleRegisterNetworkAgent(NetworkAgentInfo na) { |
| 3640 | if (VDBG) log("Got NetworkAgent Messenger"); |
| 3641 | mNetworkAgentInfos.put(na.messenger, na); |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 3642 | assignNextNetId(na); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3643 | na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger); |
| 3644 | NetworkInfo networkInfo = na.networkInfo; |
| 3645 | na.networkInfo = null; |
| 3646 | updateNetworkInfo(na, networkInfo); |
| 3647 | } |
| 3648 | |
| 3649 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp) { |
| 3650 | LinkProperties newLp = networkAgent.linkProperties; |
| 3651 | int netId = networkAgent.network.netId; |
| 3652 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3653 | // The NetworkAgentInfo does not know whether clatd is running on its network or not. Before |
| 3654 | // we do anything else, make sure its LinkProperties are accurate. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 3655 | if (networkAgent.clatd != null) { |
| 3656 | networkAgent.clatd.fixupLinkProperties(oldLp); |
| 3657 | } |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3658 | |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3659 | updateInterfaces(newLp, oldLp, netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3660 | updateMtu(newLp, oldLp); |
| 3661 | // TODO - figure out what to do for clat |
| 3662 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 3663 | // updateMtu(lp, null); |
| 3664 | // } |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3665 | updateTcpBufferSizes(networkAgent); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 3666 | |
| 3667 | // TODO: deprecate and remove mDefaultDns when we can do so safely. |
| 3668 | // For now, use it only when the network has Internet access. http://b/18327075 |
| 3669 | final boolean useDefaultDns = networkAgent.networkCapabilities.hasCapability( |
| 3670 | NetworkCapabilities.NET_CAPABILITY_INTERNET); |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3671 | final boolean flushDns = updateRoutes(newLp, oldLp, netId); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 3672 | updateDnses(newLp, oldLp, netId, flushDns, useDefaultDns); |
| 3673 | |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3674 | updateClat(newLp, oldLp, networkAgent); |
Robert Greenwalt | b98c598 | 2014-08-23 09:51:35 -0700 | [diff] [blame] | 3675 | if (isDefaultNetwork(networkAgent)) handleApplyDefaultProxy(newLp.getHttpProxy()); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3676 | // TODO - move this check to cover the whole function |
| 3677 | if (!Objects.equals(newLp, oldLp)) { |
| 3678 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); |
| 3679 | } |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3680 | } |
| 3681 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 3682 | private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo nai) { |
| 3683 | final boolean wasRunningClat = nai.clatd != null && nai.clatd.isStarted(); |
| 3684 | final boolean shouldRunClat = Nat464Xlat.requiresClat(nai); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3685 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3686 | if (!wasRunningClat && shouldRunClat) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 3687 | nai.clatd = new Nat464Xlat(mContext, mNetd, mTrackerHandler, nai); |
| 3688 | nai.clatd.start(); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3689 | } else if (wasRunningClat && !shouldRunClat) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 3690 | nai.clatd.stop(); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3691 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3692 | } |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3693 | |
| 3694 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 3695 | CompareResult<String> interfaceDiff = new CompareResult<String>(); |
| 3696 | if (oldLp != null) { |
| 3697 | interfaceDiff = oldLp.compareAllInterfaceNames(newLp); |
| 3698 | } else if (newLp != null) { |
| 3699 | interfaceDiff.added = newLp.getAllInterfaceNames(); |
| 3700 | } |
| 3701 | for (String iface : interfaceDiff.added) { |
| 3702 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3703 | if (DBG) log("Adding iface " + iface + " to network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3704 | mNetd.addInterfaceToNetwork(iface, netId); |
| 3705 | } catch (Exception e) { |
| 3706 | loge("Exception adding interface: " + e); |
| 3707 | } |
| 3708 | } |
| 3709 | for (String iface : interfaceDiff.removed) { |
| 3710 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3711 | if (DBG) log("Removing iface " + iface + " from network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3712 | mNetd.removeInterfaceFromNetwork(iface, netId); |
| 3713 | } catch (Exception e) { |
| 3714 | loge("Exception removing interface: " + e); |
| 3715 | } |
| 3716 | } |
| 3717 | } |
| 3718 | |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3719 | /** |
| 3720 | * Have netd update routes from oldLp to newLp. |
| 3721 | * @return true if routes changed between oldLp and newLp |
| 3722 | */ |
| 3723 | private boolean updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3724 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
| 3725 | if (oldLp != null) { |
| 3726 | routeDiff = oldLp.compareAllRoutes(newLp); |
| 3727 | } else if (newLp != null) { |
| 3728 | routeDiff.added = newLp.getAllRoutes(); |
| 3729 | } |
| 3730 | |
| 3731 | // add routes before removing old in case it helps with continuous connectivity |
| 3732 | |
| 3733 | // do this twice, adding non-nexthop routes first, then routes they are dependent on |
| 3734 | for (RouteInfo route : routeDiff.added) { |
| 3735 | if (route.hasGateway()) continue; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3736 | if (DBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3737 | try { |
| 3738 | mNetd.addRoute(netId, route); |
| 3739 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3740 | if ((route.getDestination().getAddress() instanceof Inet4Address) || VDBG) { |
| 3741 | loge("Exception in addRoute for non-gateway: " + e); |
| 3742 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3743 | } |
| 3744 | } |
| 3745 | for (RouteInfo route : routeDiff.added) { |
| 3746 | if (route.hasGateway() == false) continue; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3747 | if (DBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3748 | try { |
| 3749 | mNetd.addRoute(netId, route); |
| 3750 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3751 | if ((route.getGateway() instanceof Inet4Address) || VDBG) { |
| 3752 | loge("Exception in addRoute for gateway: " + e); |
| 3753 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | for (RouteInfo route : routeDiff.removed) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3758 | if (DBG) log("Removing Route [" + route + "] from network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3759 | try { |
| 3760 | mNetd.removeRoute(netId, route); |
| 3761 | } catch (Exception e) { |
| 3762 | loge("Exception in removeRoute: " + e); |
| 3763 | } |
| 3764 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3765 | return !routeDiff.added.isEmpty() || !routeDiff.removed.isEmpty(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3766 | } |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 3767 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId, |
| 3768 | boolean flush, boolean useDefaultDns) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3769 | if (oldLp == null || (newLp.isIdenticalDnses(oldLp) == false)) { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 3770 | Collection<InetAddress> dnses = newLp.getDnsServers(); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 3771 | if (dnses.size() == 0 && mDefaultDns != null && useDefaultDns) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3772 | dnses = new ArrayList(); |
| 3773 | dnses.add(mDefaultDns); |
| 3774 | if (DBG) { |
| 3775 | loge("no dns provided for netId " + netId + ", so using defaults"); |
| 3776 | } |
| 3777 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3778 | if (DBG) log("Setting Dns servers for network " + netId + " to " + dnses); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3779 | try { |
| 3780 | mNetd.setDnsServersForNetwork(netId, NetworkUtils.makeStrings(dnses), |
| 3781 | newLp.getDomains()); |
| 3782 | } catch (Exception e) { |
| 3783 | loge("Exception in setDnsServersForNetwork: " + e); |
| 3784 | } |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 3785 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 3786 | if (defaultNai != null && defaultNai.network.netId == netId) { |
| 3787 | setDefaultDnsSystemProperties(dnses); |
| 3788 | } |
Robert Greenwalt | 5c1c832a8 | 2014-07-28 16:32:19 -0700 | [diff] [blame] | 3789 | flushVmDnsCache(); |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3790 | } else if (flush) { |
| 3791 | try { |
| 3792 | mNetd.flushNetworkDnsCache(netId); |
| 3793 | } catch (Exception e) { |
| 3794 | loge("Exception in flushNetworkDnsCache: " + e); |
| 3795 | } |
| 3796 | flushVmDnsCache(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3797 | } |
| 3798 | } |
| 3799 | |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 3800 | private void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) { |
| 3801 | int last = 0; |
| 3802 | for (InetAddress dns : dnses) { |
| 3803 | ++last; |
| 3804 | String key = "net.dns" + last; |
| 3805 | String value = dns.getHostAddress(); |
| 3806 | SystemProperties.set(key, value); |
| 3807 | } |
| 3808 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 3809 | String key = "net.dns" + i; |
| 3810 | SystemProperties.set(key, ""); |
| 3811 | } |
| 3812 | mNumDnsEntries = last; |
| 3813 | } |
| 3814 | |
| 3815 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3816 | private void updateCapabilities(NetworkAgentInfo networkAgent, |
| 3817 | NetworkCapabilities networkCapabilities) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3818 | if (!Objects.equals(networkAgent.networkCapabilities, networkCapabilities)) { |
| 3819 | synchronized (networkAgent) { |
| 3820 | networkAgent.networkCapabilities = networkCapabilities; |
| 3821 | } |
Jeff Davidson | 9634abe | 2014-11-04 16:48:27 -0800 | [diff] [blame] | 3822 | rematchAllNetworksAndRequests(networkAgent, networkAgent.getCurrentScore()); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3823 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_CAP_CHANGED); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 3824 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3825 | } |
| 3826 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3827 | private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) { |
| 3828 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 3829 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 3830 | // Don't send listening requests to factories. b/17393458 |
| 3831 | if (!isRequest(nr)) continue; |
| 3832 | sendUpdatedScoreToFactories(nr, nai.getCurrentScore()); |
| 3833 | } |
| 3834 | } |
| 3835 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3836 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) { |
| 3837 | if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3838 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 3839 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, 0, |
| 3840 | networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3844 | private void sendPendingIntentForRequest(NetworkRequestInfo nri, NetworkAgentInfo networkAgent, |
| 3845 | int notificationType) { |
| 3846 | if (notificationType == ConnectivityManager.CALLBACK_AVAILABLE) { |
| 3847 | Intent intent = new Intent(); |
Jeremy Joslin | a68e7d7 | 2014-11-26 14:24:15 -0800 | [diff] [blame] | 3848 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, networkAgent.network); |
| 3849 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_REQUEST, nri.request); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3850 | sendIntent(nri.mPendingIntent, intent); |
| 3851 | } |
| 3852 | // else not handled |
| 3853 | } |
| 3854 | |
| 3855 | private void sendIntent(PendingIntent pendingIntent, Intent intent) { |
| 3856 | mPendingIntentWakeLock.acquire(); |
| 3857 | try { |
| 3858 | if (DBG) log("Sending " + pendingIntent); |
| 3859 | pendingIntent.send(mContext, 0, intent, this /* onFinished */, null /* Handler */); |
| 3860 | } catch (PendingIntent.CanceledException e) { |
| 3861 | if (DBG) log(pendingIntent + " was not sent, it had been canceled."); |
| 3862 | mPendingIntentWakeLock.release(); |
| 3863 | releasePendingNetworkRequest(pendingIntent); |
| 3864 | } |
| 3865 | // ...otherwise, mPendingIntentWakeLock.release() gets called by onSendFinished() |
| 3866 | } |
| 3867 | |
| 3868 | @Override |
| 3869 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 3870 | String resultData, Bundle resultExtras) { |
| 3871 | if (DBG) log("Finished sending " + pendingIntent); |
| 3872 | mPendingIntentWakeLock.release(); |
| 3873 | releasePendingNetworkRequest(pendingIntent); |
| 3874 | } |
| 3875 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3876 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3877 | NetworkAgentInfo networkAgent, int notificationType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3878 | if (nri.messenger == null) return; // Default request has no msgr |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3879 | Bundle bundle = new Bundle(); |
| 3880 | bundle.putParcelable(NetworkRequest.class.getSimpleName(), |
| 3881 | new NetworkRequest(nri.request)); |
| 3882 | Message msg = Message.obtain(); |
| 3883 | if (notificationType != ConnectivityManager.CALLBACK_UNAVAIL && |
| 3884 | notificationType != ConnectivityManager.CALLBACK_RELEASED) { |
| 3885 | bundle.putParcelable(Network.class.getSimpleName(), networkAgent.network); |
| 3886 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3887 | switch (notificationType) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3888 | case ConnectivityManager.CALLBACK_LOSING: { |
| 3889 | msg.arg1 = 30 * 1000; // TODO - read this from NetworkMonitor |
| 3890 | break; |
| 3891 | } |
| 3892 | case ConnectivityManager.CALLBACK_CAP_CHANGED: { |
| 3893 | bundle.putParcelable(NetworkCapabilities.class.getSimpleName(), |
| 3894 | new NetworkCapabilities(networkAgent.networkCapabilities)); |
| 3895 | break; |
| 3896 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3897 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3898 | bundle.putParcelable(LinkProperties.class.getSimpleName(), |
| 3899 | new LinkProperties(networkAgent.linkProperties)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3900 | break; |
| 3901 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3902 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3903 | msg.what = notificationType; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3904 | msg.setData(bundle); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3905 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3906 | if (VDBG) { |
| 3907 | log("sending notification " + notifyTypeToName(notificationType) + |
| 3908 | " for " + nri.request); |
| 3909 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3910 | nri.messenger.send(msg); |
| 3911 | } catch (RemoteException e) { |
| 3912 | // may occur naturally in the race of binder death. |
| 3913 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 3914 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3915 | } |
| 3916 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3917 | private void teardownUnneededNetwork(NetworkAgentInfo nai) { |
| 3918 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 3919 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 3920 | // Ignore listening requests. |
| 3921 | if (!isRequest(nr)) continue; |
| 3922 | loge("Dead network still had at least " + nr); |
| 3923 | break; |
| 3924 | } |
| 3925 | nai.asyncChannel.disconnect(); |
| 3926 | } |
| 3927 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3928 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 3929 | if (oldNetwork == null) { |
| 3930 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 3931 | return; |
| 3932 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3933 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
| 3934 | teardownUnneededNetwork(oldNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3935 | } |
| 3936 | |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3937 | private void makeDefault(NetworkAgentInfo newNetwork) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3938 | if (DBG) log("Switching to new default network: " + newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3939 | setupDataActivityTracking(newNetwork); |
| 3940 | try { |
| 3941 | mNetd.setDefaultNetId(newNetwork.network.netId); |
| 3942 | } catch (Exception e) { |
| 3943 | loge("Exception setting default network :" + e); |
| 3944 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 3945 | notifyLockdownVpn(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3946 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 3947 | updateTcpBufferSizes(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3948 | } |
| 3949 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3950 | // Handles a network appearing or improving its score. |
| 3951 | // |
| 3952 | // - Evaluates all current NetworkRequests that can be |
| 3953 | // satisfied by newNetwork, and reassigns to newNetwork |
| 3954 | // any such requests for which newNetwork is the best. |
| 3955 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3956 | // - Lingers any Networks that as a result are no longer |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3957 | // needed. A network is needed if it is the best network for |
| 3958 | // one or more NetworkRequests, or if it is a VPN. |
| 3959 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3960 | // - Tears down newNetwork if it just became validated |
| 3961 | // (i.e. nascent==true) but turns out to be unneeded. |
| 3962 | // Does not tear down newNetwork if it is unvalidated, |
| 3963 | // because future validation may improve newNetwork's |
| 3964 | // score enough that it is needed. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3965 | // |
| 3966 | // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, |
| 3967 | // it does not remove NetworkRequests that other Networks could better satisfy. |
| 3968 | // If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}. |
| 3969 | // This function should be used when possible instead of {@code rematchAllNetworksAndRequests} |
| 3970 | // as it performs better by a factor of the number of Networks. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3971 | // |
| 3972 | // @param nascent indicates if newNetwork just became validated, in which case it should be |
| 3973 | // torn down if unneeded. If nascent is false, no action is taken if newNetwork |
| 3974 | // is found to be unneeded by this call. Presumably, in this case, either: |
| 3975 | // - newNetwork is unvalidated (and left alive), or |
| 3976 | // - the NetworkRequests keeping newNetwork alive have been transitioned to |
| 3977 | // another higher scoring network by another call to rematchNetworkAndRequests() |
| 3978 | // and this other call also lingered newNetwork. |
| 3979 | private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, boolean nascent) { |
Paul Jensen | 57a767f | 2014-11-19 13:01:46 -0500 | [diff] [blame] | 3980 | if (!newNetwork.created) return; |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3981 | if (nascent && !newNetwork.validated) loge("ERROR: nascent network not validated."); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 3982 | boolean keep = newNetwork.isVPN(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3983 | boolean isNewDefault = false; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3984 | if (DBG) log("rematching " + newNetwork.name()); |
| 3985 | // Find and migrate to this Network any NetworkRequests for |
| 3986 | // which this network is now the best. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3987 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>(); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3988 | if (VDBG) log(" network has: " + newNetwork.networkCapabilities); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3989 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 3990 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId); |
| 3991 | if (newNetwork == currentNetwork) { |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 3992 | if (DBG) { |
| 3993 | log("Network " + newNetwork.name() + " was already satisfying" + |
| 3994 | " request " + nri.request.requestId + ". No change."); |
| 3995 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 3996 | keep = true; |
| 3997 | continue; |
| 3998 | } |
| 3999 | |
| 4000 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4001 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 4002 | if (newNetwork.satisfies(nri.request)) { |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 4003 | if (!nri.isRequest) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4004 | // This is not a request, it's a callback listener. |
| 4005 | // Add it to newNetwork regardless of score. |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 4006 | newNetwork.addRequest(nri.request); |
| 4007 | continue; |
| 4008 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4009 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4010 | // next check if it's better than any current network we're using for |
| 4011 | // this request |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4012 | if (VDBG) { |
| 4013 | log("currentScore = " + |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4014 | (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + |
| 4015 | ", newScore = " + newNetwork.getCurrentScore()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4016 | } |
| 4017 | if (currentNetwork == null || |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4018 | currentNetwork.getCurrentScore() < newNetwork.getCurrentScore()) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4019 | if (currentNetwork != null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4020 | if (DBG) log(" accepting network in place of " + currentNetwork.name()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4021 | currentNetwork.networkRequests.remove(nri.request.requestId); |
| 4022 | currentNetwork.networkLingered.add(nri.request); |
| 4023 | affectedNetworks.add(currentNetwork); |
| 4024 | } else { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4025 | if (DBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4026 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4027 | mNetworkForRequestId.put(nri.request.requestId, newNetwork); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 4028 | newNetwork.addRequest(nri.request); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4029 | if (nri.isRequest && nri.request.legacyType != TYPE_NONE) { |
| 4030 | mLegacyTypeTracker.add(nri.request.legacyType, newNetwork); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4031 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4032 | keep = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4033 | // Tell NetworkFactories about the new score, so they can stop |
| 4034 | // trying to connect if they know they cannot match it. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4035 | // TODO - this could get expensive if we have alot of requests for this |
| 4036 | // network. Think about if there is a way to reduce this. Push |
| 4037 | // netid->request mapping to each factory? |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4038 | sendUpdatedScoreToFactories(nri.request, newNetwork.getCurrentScore()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4039 | if (mDefaultRequest.requestId == nri.request.requestId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4040 | isNewDefault = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4041 | // TODO: Remove following line. It's redundant with makeDefault call. |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 4042 | if (newNetwork.linkProperties != null) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 4043 | updateTcpBufferSizes(newNetwork); |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 4044 | setDefaultDnsSystemProperties( |
| 4045 | newNetwork.linkProperties.getDnsServers()); |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 4046 | } else { |
| 4047 | setDefaultDnsSystemProperties(new ArrayList<InetAddress>()); |
| 4048 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4049 | // Maintain the illusion: since the legacy API only |
| 4050 | // understands one network at a time, we must pretend |
| 4051 | // that the current default network disconnected before |
| 4052 | // the new one connected. |
| 4053 | if (currentNetwork != null) { |
| 4054 | mLegacyTypeTracker.remove(currentNetwork.networkInfo.getType(), |
| 4055 | currentNetwork); |
| 4056 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4057 | mDefaultInetConditionPublished = newNetwork.validated ? 100 : 0; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4058 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4059 | notifyLockdownVpn(newNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4060 | } |
| 4061 | } |
| 4062 | } |
| 4063 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4064 | // Linger any networks that are no longer needed. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4065 | for (NetworkAgentInfo nai : affectedNetworks) { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4066 | boolean teardown = !nai.isVPN() && nai.validated; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4067 | for (int i = 0; i < nai.networkRequests.size() && teardown; i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4068 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 4069 | try { |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4070 | if (isRequest(nr)) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4071 | teardown = false; |
| 4072 | } |
| 4073 | } catch (Exception e) { |
| 4074 | loge("Request " + nr + " not found in mNetworkRequests."); |
| 4075 | loge(" it came from request list of " + nai.name()); |
| 4076 | } |
| 4077 | } |
| 4078 | if (teardown) { |
| 4079 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER); |
| 4080 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING); |
| 4081 | } else { |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 4082 | unlinger(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4083 | } |
| 4084 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4085 | if (keep) { |
| 4086 | if (isNewDefault) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4087 | // Notify system services that this network is up. |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 4088 | makeDefault(newNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4089 | synchronized (ConnectivityService.this) { |
| 4090 | // have a new default network, release the transition wakelock in |
| 4091 | // a second if it's held. The second pause is to allow apps |
| 4092 | // to reconnect over the new network |
| 4093 | if (mNetTransitionWakeLock.isHeld()) { |
| 4094 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 4095 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
| 4096 | mNetTransitionWakeLockSerialNumber, 0), |
| 4097 | 1000); |
| 4098 | } |
| 4099 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4100 | } |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 4101 | |
| 4102 | // Notify battery stats service about this network, both the normal |
| 4103 | // interface and any stacked links. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4104 | // TODO: Avoid redoing this; this must only be done once when a network comes online. |
Dianne Hackborn | 2932513 | 2014-05-21 15:01:03 -0700 | [diff] [blame] | 4105 | try { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 4106 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 4107 | final int type = newNetwork.networkInfo.getType(); |
| 4108 | |
| 4109 | final String baseIface = newNetwork.linkProperties.getInterfaceName(); |
| 4110 | bs.noteNetworkInterfaceType(baseIface, type); |
| 4111 | for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { |
| 4112 | final String stackedIface = stacked.getInterfaceName(); |
| 4113 | bs.noteNetworkInterfaceType(stackedIface, type); |
| 4114 | NetworkStatsFactory.noteStackedIface(stackedIface, baseIface); |
| 4115 | } |
| 4116 | } catch (RemoteException ignored) { |
| 4117 | } |
| 4118 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4119 | notifyNetworkCallbacks(newNetwork, ConnectivityManager.CALLBACK_AVAILABLE); |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 4120 | |
| 4121 | // A VPN generally won't get added to the legacy tracker in the "for (nri)" loop above, |
| 4122 | // because usually there are no NetworkRequests it satisfies (e.g., mDefaultRequest |
| 4123 | // wants the NOT_VPN capability, so it will never be satisfied by a VPN). So, add the |
| 4124 | // newNetwork to the tracker explicitly (it's a no-op if it has already been added). |
| 4125 | if (newNetwork.isVPN()) { |
| 4126 | mLegacyTypeTracker.add(TYPE_VPN, newNetwork); |
| 4127 | } |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4128 | } else if (nascent) { |
| 4129 | // Only tear down newly validated networks here. Leave unvalidated to either become |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4130 | // validated (and get evaluated against peers, one losing here) or |
| 4131 | // NetworkMonitor reports a bad network and we tear it down then. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4132 | // Networks that have been up for a while and are validated should be torn down via |
| 4133 | // the lingering process so communication on that network is given time to wrap up. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4134 | // TODO: Could teardown unvalidated networks when their NetworkCapabilities |
| 4135 | // satisfy no NetworkRequests. |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4136 | if (DBG) log("Validated network turns out to be unwanted. Tear it down."); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4137 | teardownUnneededNetwork(newNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4138 | } |
| 4139 | } |
| 4140 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4141 | // Attempt to rematch all Networks with NetworkRequests. This may result in Networks |
| 4142 | // being disconnected. |
| 4143 | // If only one Network's score or capabilities have been modified since the last time |
| 4144 | // this function was called, pass this Network in via the "changed" arugment, otherwise |
| 4145 | // pass null. |
| 4146 | // If only one Network has been changed but its NetworkCapabilities have not changed, |
| 4147 | // pass in the Network's score (from getCurrentScore()) prior to the change via |
| 4148 | // "oldScore", otherwise pass changed.getCurrentScore() or 0 if "changed" is null. |
| 4149 | private void rematchAllNetworksAndRequests(NetworkAgentInfo changed, int oldScore) { |
| 4150 | // TODO: This may get slow. The "changed" parameter is provided for future optimization |
| 4151 | // to avoid the slowness. It is not simply enough to process just "changed", for |
| 4152 | // example in the case where "changed"'s score decreases and another network should begin |
| 4153 | // satifying a NetworkRequest that "changed" currently satisfies. |
| 4154 | |
| 4155 | // Optimization: Only reprocess "changed" if its score improved. This is safe because it |
| 4156 | // can only add more NetworkRequests satisfied by "changed", and this is exactly what |
| 4157 | // rematchNetworkAndRequests() handles. |
| 4158 | if (changed != null && oldScore < changed.getCurrentScore()) { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4159 | rematchNetworkAndRequests(changed, false); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4160 | } else { |
| 4161 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4162 | rematchNetworkAndRequests(nai, false); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4163 | } |
| 4164 | } |
| 4165 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4166 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4167 | private void updateInetCondition(NetworkAgentInfo nai, boolean valid) { |
| 4168 | // Don't bother updating until we've graduated to validated at least once. |
| 4169 | if (!nai.validated) return; |
| 4170 | // For now only update icons for default connection. |
| 4171 | // TODO: Update WiFi and cellular icons separately. b/17237507 |
| 4172 | if (!isDefaultNetwork(nai)) return; |
| 4173 | |
| 4174 | int newInetCondition = valid ? 100 : 0; |
| 4175 | // Don't repeat publish. |
| 4176 | if (newInetCondition == mDefaultInetConditionPublished) return; |
| 4177 | |
| 4178 | mDefaultInetConditionPublished = newInetCondition; |
| 4179 | sendInetConditionBroadcast(nai.networkInfo); |
| 4180 | } |
| 4181 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4182 | private void notifyLockdownVpn(NetworkAgentInfo nai) { |
| 4183 | if (mLockdownTracker != null) { |
| 4184 | if (nai != null && nai.isVPN()) { |
| 4185 | mLockdownTracker.onVpnStateChanged(nai.networkInfo); |
| 4186 | } else { |
| 4187 | mLockdownTracker.onNetworkInfoChanged(); |
| 4188 | } |
| 4189 | } |
| 4190 | } |
| 4191 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4192 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
| 4193 | NetworkInfo.State state = newInfo.getState(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4194 | NetworkInfo oldInfo = null; |
| 4195 | synchronized (networkAgent) { |
| 4196 | oldInfo = networkAgent.networkInfo; |
| 4197 | networkAgent.networkInfo = newInfo; |
| 4198 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4199 | notifyLockdownVpn(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4200 | |
| 4201 | if (oldInfo != null && oldInfo.getState() == state) { |
| 4202 | if (VDBG) log("ignoring duplicate network state non-change"); |
| 4203 | return; |
| 4204 | } |
| 4205 | if (DBG) { |
| 4206 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 4207 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 4208 | " to " + state); |
| 4209 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4210 | |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4211 | if (state == NetworkInfo.State.CONNECTED && !networkAgent.created) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4212 | try { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4213 | // This should never fail. Specifying an already in use NetID will cause failure. |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4214 | if (networkAgent.isVPN()) { |
| 4215 | mNetd.createVirtualNetwork(networkAgent.network.netId, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 4216 | !networkAgent.linkProperties.getDnsServers().isEmpty(), |
| 4217 | (networkAgent.networkMisc == null || |
| 4218 | !networkAgent.networkMisc.allowBypass)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4219 | } else { |
| 4220 | mNetd.createPhysicalNetwork(networkAgent.network.netId); |
| 4221 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4222 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 4223 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 4224 | + e.getMessage()); |
| 4225 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4226 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4227 | networkAgent.created = true; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4228 | updateLinkProperties(networkAgent, null); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4229 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 4230 | networkAgent.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4231 | if (networkAgent.isVPN()) { |
| 4232 | // Temporarily disable the default proxy (not global). |
| 4233 | synchronized (mProxyLock) { |
| 4234 | if (!mDefaultProxyDisabled) { |
| 4235 | mDefaultProxyDisabled = true; |
| 4236 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4237 | sendProxyBroadcast(null); |
| 4238 | } |
| 4239 | } |
| 4240 | } |
| 4241 | // TODO: support proxy per network. |
| 4242 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4243 | // Consider network even though it is not yet validated. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4244 | rematchNetworkAndRequests(networkAgent, false); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4245 | } else if (state == NetworkInfo.State.DISCONNECTED || |
| 4246 | state == NetworkInfo.State.SUSPENDED) { |
| 4247 | networkAgent.asyncChannel.disconnect(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4248 | if (networkAgent.isVPN()) { |
| 4249 | synchronized (mProxyLock) { |
| 4250 | if (mDefaultProxyDisabled) { |
| 4251 | mDefaultProxyDisabled = false; |
| 4252 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4253 | sendProxyBroadcast(mDefaultProxy); |
| 4254 | } |
| 4255 | } |
| 4256 | } |
| 4257 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4258 | } |
| 4259 | } |
| 4260 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4261 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
| 4262 | if (DBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 4263 | if (score < 0) { |
| 4264 | loge("updateNetworkScore for " + nai.name() + " got a negative score (" + score + |
| 4265 | "). Bumping score to min of 0"); |
| 4266 | score = 0; |
| 4267 | } |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4268 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4269 | final int oldScore = nai.getCurrentScore(); |
| 4270 | nai.setCurrentScore(score); |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4271 | |
Paul Jensen | 57a767f | 2014-11-19 13:01:46 -0500 | [diff] [blame] | 4272 | rematchAllNetworksAndRequests(nai, oldScore); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4273 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4274 | sendUpdatedScoreToFactories(nai); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 4275 | } |
| 4276 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4277 | // notify only this one new request of the current state |
| 4278 | protected void notifyNetworkCallback(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
| 4279 | int notifyType = ConnectivityManager.CALLBACK_AVAILABLE; |
| 4280 | // TODO - read state from monitor to decide what to send. |
| 4281 | // if (nai.networkMonitor.isLingering()) { |
| 4282 | // notifyType = NetworkCallbacks.LOSING; |
| 4283 | // } else if (nai.networkMonitor.isEvaluating()) { |
| 4284 | // notifyType = NetworkCallbacks.callCallbackForRequest(request, nai, notifyType); |
| 4285 | // } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4286 | if (nri.mPendingIntent == null) { |
| 4287 | callCallbackForRequest(nri, nai, notifyType); |
| 4288 | } else { |
| 4289 | sendPendingIntentForRequest(nri, nai, notifyType); |
| 4290 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4291 | } |
| 4292 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4293 | private void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, boolean connected, int type) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4294 | // The NetworkInfo we actually send out has no bearing on the real |
| 4295 | // state of affairs. For example, if the default connection is mobile, |
| 4296 | // and a request for HIPRI has just gone away, we need to pretend that |
| 4297 | // HIPRI has just disconnected. So we need to set the type to HIPRI and |
| 4298 | // the state to DISCONNECTED, even though the network is of type MOBILE |
| 4299 | // and is still connected. |
| 4300 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 4301 | info.setType(type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4302 | if (connected) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4303 | info.setDetailedState(DetailedState.CONNECTED, null, info.getExtraInfo()); |
Erik Kline | e660e27 | 2014-12-05 20:13:08 +0900 | [diff] [blame] | 4304 | sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4305 | } else { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4306 | info.setDetailedState(DetailedState.DISCONNECTED, null, info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4307 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 4308 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 4309 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 4310 | if (info.isFailover()) { |
| 4311 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 4312 | nai.networkInfo.setFailover(false); |
| 4313 | } |
| 4314 | if (info.getReason() != null) { |
| 4315 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 4316 | } |
| 4317 | if (info.getExtraInfo() != null) { |
| 4318 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 4319 | } |
| 4320 | NetworkAgentInfo newDefaultAgent = null; |
| 4321 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 4322 | newDefaultAgent = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 4323 | if (newDefaultAgent != null) { |
| 4324 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 4325 | newDefaultAgent.networkInfo); |
| 4326 | } else { |
| 4327 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 4328 | } |
| 4329 | } |
| 4330 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 4331 | mDefaultInetConditionPublished); |
| 4332 | final Intent immediateIntent = new Intent(intent); |
| 4333 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 4334 | sendStickyBroadcast(immediateIntent); |
Erik Kline | e660e27 | 2014-12-05 20:13:08 +0900 | [diff] [blame] | 4335 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4336 | if (newDefaultAgent != null) { |
Erik Kline | e660e27 | 2014-12-05 20:13:08 +0900 | [diff] [blame] | 4337 | sendConnectedBroadcastDelayed(newDefaultAgent.networkInfo, |
| 4338 | getConnectivityChangeDelay()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4339 | } |
| 4340 | } |
| 4341 | } |
| 4342 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4343 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4344 | if (DBG) log("notifyType " + notifyTypeToName(notifyType) + " for " + networkAgent.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4345 | for (int i = 0; i < networkAgent.networkRequests.size(); i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4346 | NetworkRequest nr = networkAgent.networkRequests.valueAt(i); |
| 4347 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 4348 | if (VDBG) log(" sending notification for " + nr); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4349 | if (nri.mPendingIntent == null) { |
| 4350 | callCallbackForRequest(nri, networkAgent, notifyType); |
| 4351 | } else { |
| 4352 | sendPendingIntentForRequest(nri, networkAgent, notifyType); |
| 4353 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4354 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4355 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4356 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4357 | private String notifyTypeToName(int notifyType) { |
| 4358 | switch (notifyType) { |
| 4359 | case ConnectivityManager.CALLBACK_PRECHECK: return "PRECHECK"; |
| 4360 | case ConnectivityManager.CALLBACK_AVAILABLE: return "AVAILABLE"; |
| 4361 | case ConnectivityManager.CALLBACK_LOSING: return "LOSING"; |
| 4362 | case ConnectivityManager.CALLBACK_LOST: return "LOST"; |
| 4363 | case ConnectivityManager.CALLBACK_UNAVAIL: return "UNAVAILABLE"; |
| 4364 | case ConnectivityManager.CALLBACK_CAP_CHANGED: return "CAP_CHANGED"; |
| 4365 | case ConnectivityManager.CALLBACK_IP_CHANGED: return "IP_CHANGED"; |
| 4366 | case ConnectivityManager.CALLBACK_RELEASED: return "RELEASED"; |
| 4367 | } |
| 4368 | return "UNKNOWN"; |
| 4369 | } |
| 4370 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 4371 | @Override |
| 4372 | public boolean addVpnAddress(String address, int prefixLength) { |
| 4373 | throwIfLockdownEnabled(); |
| 4374 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4375 | synchronized (mVpns) { |
| 4376 | return mVpns.get(user).addAddress(address, prefixLength); |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | @Override |
| 4381 | public boolean removeVpnAddress(String address, int prefixLength) { |
| 4382 | throwIfLockdownEnabled(); |
| 4383 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4384 | synchronized (mVpns) { |
| 4385 | return mVpns.get(user).removeAddress(address, prefixLength); |
| 4386 | } |
| 4387 | } |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 4388 | |
| 4389 | @Override |
| 4390 | public boolean setUnderlyingNetworksForVpn(Network[] networks) { |
| 4391 | throwIfLockdownEnabled(); |
| 4392 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4393 | synchronized (mVpns) { |
| 4394 | return mVpns.get(user).setUnderlyingNetworks(networks); |
| 4395 | } |
| 4396 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4397 | } |